1 /*-
2 * Copyright (c) 2010-2011 Juniper Networks, Inc.
3 * All rights reserved.
4 *
5 * This software was developed by Robert N. M. Watson under contract
6 * to Juniper Networks, Inc.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31
32 __FBSDID("$FreeBSD$");
33
34 #include "opt_inet6.h"
35 #include "opt_pcbgroup.h"
36
37 #ifndef PCBGROUP
38 #error "options RSS depends on options PCBGROUP"
39 #endif
40
41 #include <sys/param.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44 #include <sys/priv.h>
45 #include <sys/kernel.h>
46 #include <sys/smp.h>
47 #include <sys/sysctl.h>
48 #include <sys/sbuf.h>
49
50 #include <net/if.h>
51 #include <net/if_var.h>
52 #include <net/netisr.h>
53 #include <net/rss_config.h>
54
55 #include <netinet/in.h>
56 #include <netinet/in_pcb.h>
57 #include <netinet6/in6_rss.h>
58 #include <netinet/in_var.h>
59
60 /* for software rss hash support */
61 #include <netinet/ip6.h>
62 #include <netinet6/ip6_var.h>
63 #include <netinet/tcp.h>
64 #include <netinet/udp.h>
65
66 /*
67 * Hash an IPv6 2-tuple.
68 */
69 uint32_t
70 rss_hash_ip6_2tuple(const struct in6_addr *src, const struct in6_addr *dst)
71 {
72 uint8_t data[sizeof(*src) + sizeof(*dst)];
73 u_int datalen;
74
75 datalen = 0;
76 bcopy(src, &data[datalen], sizeof(*src));
77 datalen += sizeof(*src);
78 bcopy(dst, &data[datalen], sizeof(*dst));
79 datalen += sizeof(*dst);
80 return (rss_hash(datalen, data));
81 }
82
83 /*
84 * Hash an IPv6 4-tuple.
85 */
86 uint32_t
87 rss_hash_ip6_4tuple(const struct in6_addr *src, u_short srcport,
88 const struct in6_addr *dst, u_short dstport)
89 {
90 uint8_t data[sizeof(*src) + sizeof(*dst) + sizeof(srcport) +
91 sizeof(dstport)];
92 u_int datalen;
93
94 datalen = 0;
95 bcopy(src, &data[datalen], sizeof(*src));
96 datalen += sizeof(*src);
97 bcopy(dst, &data[datalen], sizeof(*dst));
98 datalen += sizeof(*dst);
99 bcopy(&srcport, &data[datalen], sizeof(srcport));
100 datalen += sizeof(srcport);
101 bcopy(&dstport, &data[datalen], sizeof(dstport));
102 datalen += sizeof(dstport);
103 return (rss_hash(datalen, data));
104 }
105
106 /*
107 * Calculate an appropriate ipv6 2-tuple or 4-tuple given the given
108 * IPv6 source/destination address, UDP or TCP source/destination ports
109 * and the protocol type.
110 *
111 * The protocol code may wish to do a software hash of the given
112 * tuple. This depends upon the currently configured RSS hash types.
113 *
114 * This assumes that the packet in question isn't a fragment.
115 *
116 * It also assumes the packet source/destination address
117 * are in "incoming" packet order (ie, source is "far" address.)
118 */
119 int
120 rss_proto_software_hash_v6(const struct in6_addr *s, const struct in6_addr *d,
121 u_short sp, u_short dp, int proto,
122 uint32_t *hashval, uint32_t *hashtype)
123 {
124 uint32_t hash;
125
126 /*
127 * Next, choose the hash type depending upon the protocol
128 * identifier.
129 */
130 if ((proto == IPPROTO_TCP) &&
131 (rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV6)) {
132 hash = rss_hash_ip6_4tuple(s, sp, d, dp);
133 *hashval = hash;
134 *hashtype = M_HASHTYPE_RSS_TCP_IPV6;
135 return (0);
136 } else if ((proto == IPPROTO_UDP) &&
137 (rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV6)) {
138 hash = rss_hash_ip6_4tuple(s, sp, d, dp);
139 *hashval = hash;
140 *hashtype = M_HASHTYPE_RSS_UDP_IPV6;
141 return (0);
142 } else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV6) {
143 /* RSS doesn't hash on other protocols like SCTP; so 2-tuple */
144 hash = rss_hash_ip6_2tuple(s, d);
145 *hashval = hash;
146 *hashtype = M_HASHTYPE_RSS_IPV6;
147 return (0);
148 }
149
150 /* No configured available hashtypes! */
151 RSS_DEBUG("no available hashtypes!\n");
152 return (-1);
153 }
154
155 /*
156 * Calculate an appropriate ipv6 2-tuple or 4-tuple given the given
157 * IPv6 source/destination address, UDP or TCP source/destination ports
158 * and the protocol type.
159 *
160 * The protocol code may wish to do a software hash of the given
161 * tuple. This depends upon the currently configured RSS hash types.
162 *
163 * It assumes the packet source/destination address
164 * are in "outgoin" packet order (ie, destination is "far" address.)
165 */
166 uint32_t
167 xps_proto_software_hash_v6(const struct in6_addr *s, const struct in6_addr *d,
168 u_short sp, u_short dp, int proto, uint32_t *hashtype)
169 {
170
171 uint32_t hash;
172
173 /*
174 * Next, choose the hash type depending upon the protocol
175 * identifier.
176 */
177 if ((proto == IPPROTO_TCP) &&
178 (rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV6)) {
179 hash = rss_hash_ip6_4tuple(d, dp, s, sp);
180 *hashtype = M_HASHTYPE_RSS_TCP_IPV6;
181 return (hash);
182 } else if ((proto == IPPROTO_UDP) &&
183 (rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV6)) {
184 hash = rss_hash_ip6_4tuple(d, dp, s, sp);
185 *hashtype = M_HASHTYPE_RSS_UDP_IPV6;
186 return (hash);
187 } else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV6) {
188 /* RSS doesn't hash on other protocols like SCTP; so 2-tuple */
189 hash = rss_hash_ip6_2tuple(d, s);
190 *hashtype = M_HASHTYPE_RSS_IPV6;
191 return (hash);
192 }
193
194 *hashtype = M_HASHTYPE_NONE;
195 return (0);
196 }
197
198
199 /*
200 * Do a software calculation of the RSS for the given mbuf.
201 *
202 * This is typically used by the input path to recalculate the RSS after
203 * some form of packet processing (eg de-capsulation, IP fragment reassembly.)
204 *
205 * dir is the packet direction - RSS_HASH_PKT_INGRESS for incoming and
206 * RSS_HASH_PKT_EGRESS for outgoing.
207 *
208 * Returns 0 if a hash was done, -1 if no hash was done, +1 if
209 * the mbuf already had a valid RSS flowid.
210 *
211 * This function doesn't modify the mbuf. It's up to the caller to
212 * assign flowid/flowtype as appropriate.
213 */
214 int
215 rss_mbuf_software_hash_v6(const struct mbuf *m, int dir, uint32_t *hashval,
216 uint32_t *hashtype)
217 {
218 const struct ip6_hdr *ip6;
219 const struct ip6_frag *ip6f;
220 const struct tcphdr *th;
221 const struct udphdr *uh;
222 uint32_t flowtype;
223 uint8_t proto;
224 int off, newoff;
225 int nxt;
226
227 /*
228 * XXX For now this only handles hashing on incoming mbufs.
229 */
230 if (dir != RSS_HASH_PKT_INGRESS) {
231 RSS_DEBUG("called on EGRESS packet!\n");
232 return (-1);
233 }
234
235 off = sizeof(struct ip6_hdr);
236
237 /*
238 * First, validate that the mbuf we have is long enough
239 * to have an IPv6 header in it.
240 */
241 if (m->m_pkthdr.len < off) {
242 RSS_DEBUG("short mbuf pkthdr\n");
243 return (-1);
244 }
245 if (m->m_len < off) {
246 RSS_DEBUG("short mbuf len\n");
247 return (-1);
248 }
249
250 /* Ok, let's dereference that */
251 ip6 = mtod(m, struct ip6_hdr *);
252 proto = ip6->ip6_nxt;
253
254 /*
255 * Find the beginning of the TCP/UDP header.
256 *
257 * If this is a fragment then it shouldn't be four-tuple
258 * hashed just yet. Once it's reassembled into a full
259 * frame it should be re-hashed.
260 */
261 while (proto != IPPROTO_FRAGMENT) {
262 newoff = ip6_nexthdr(m, off, proto, &nxt);
263 if (newoff < 0)
264 break;
265 off = newoff;
266 proto = nxt;
267 }
268
269 /*
270 * Ignore the fragment header if this is an "atomic" fragment
271 * (offset and m bit set to 0)
272 */
273 if (proto == IPPROTO_FRAGMENT) {
274 if (m->m_len < off + sizeof(struct ip6_frag)) {
275 RSS_DEBUG("short fragment frame?\n");
276 return (-1);
277 }
278 ip6f = (const struct ip6_frag *)((c_caddr_t)ip6 + off);
279 if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) {
280 off = ip6_lasthdr(m, off, proto, &nxt);
281 if (off < 0) {
282 RSS_DEBUG("invalid extension header\n");
283 return (-1);
284 }
285 proto = nxt;
286 }
287 }
288
289 /*
290 * If the mbuf flowid/flowtype matches the packet type,
291 * and we don't support the 4-tuple version of the given protocol,
292 * then signal to the owner that it can trust the flowid/flowtype
293 * details.
294 *
295 * This is a little picky - eg, if TCPv6 / UDPv6 hashing
296 * is supported but we got a TCP/UDP frame only 2-tuple hashed,
297 * then we shouldn't just "trust" the 2-tuple hash. We need
298 * a 4-tuple hash.
299 */
300 flowtype = M_HASHTYPE_GET(m);
301
302 if (flowtype != M_HASHTYPE_NONE) {
303 switch (proto) {
304 case IPPROTO_UDP:
305 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV6) &&
306 (flowtype == M_HASHTYPE_RSS_UDP_IPV6)) {
307 return (1);
308 }
309 /*
310 * Only allow 2-tuple for UDP frames if we don't also
311 * support 4-tuple for UDP.
312 */
313 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV6) &&
314 ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV6) == 0) &&
315 flowtype == M_HASHTYPE_RSS_IPV6) {
316 return (1);
317 }
318 break;
319 case IPPROTO_TCP:
320 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV6) &&
321 (flowtype == M_HASHTYPE_RSS_TCP_IPV6)) {
322 return (1);
323 }
324 /*
325 * Only allow 2-tuple for TCP frames if we don't also
326 * support 4-tuple for TCP.
327 */
328 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV6) &&
329 ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV6) == 0) &&
330 flowtype == M_HASHTYPE_RSS_IPV6) {
331 return (1);
332 }
333 break;
334 default:
335 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV6) &&
336 flowtype == M_HASHTYPE_RSS_IPV6) {
337 return (1);
338 }
339 break;
340 }
341 }
342
343 /*
344 * Decode enough information to make a hash decision.
345 */
346 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV6) &&
347 (proto == IPPROTO_TCP)) {
348 if (m->m_len < off + sizeof(struct tcphdr)) {
349 RSS_DEBUG("short TCP frame?\n");
350 return (-1);
351 }
352 th = (const struct tcphdr *)((c_caddr_t)ip6 + off);
353 return rss_proto_software_hash_v6(&ip6->ip6_src, &ip6->ip6_dst,
354 th->th_sport,
355 th->th_dport,
356 proto,
357 hashval,
358 hashtype);
359 } else if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV6) &&
360 (proto == IPPROTO_UDP)) {
361 if (m->m_len < off + sizeof(struct udphdr)) {
362 RSS_DEBUG("short UDP frame?\n");
363 return (-1);
364 }
365 uh = (const struct udphdr *)((c_caddr_t)ip6 + off);
366 return rss_proto_software_hash_v6(&ip6->ip6_src, &ip6->ip6_dst,
367 uh->uh_sport,
368 uh->uh_dport,
369 proto,
370 hashval,
371 hashtype);
372 } else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV6) {
373 /* Default to 2-tuple hash */
374 return rss_proto_software_hash_v6(&ip6->ip6_src, &ip6->ip6_dst,
375 0, /* source port */
376 0, /* destination port */
377 0, /* IPPROTO_IP */
378 hashval,
379 hashtype);
380 } else {
381 RSS_DEBUG("no available hashtypes!\n");
382 return (-1);
383 }
384 }
385
386 /*
387 * Similar to rss_m2cpuid, but designed to be used by the IPv6 NETISR
388 * on incoming frames.
389 *
390 * If an existing RSS hash exists and it matches what the configured
391 * hashing is, then use it.
392 *
393 * If there's an existing RSS hash but the desired hash is different,
394 * or if there's no useful RSS hash, then calculate it via
395 * the software path.
396 *
397 * XXX TODO: definitely want statistics here!
398 */
399 struct mbuf *
400 rss_soft_m2cpuid_v6(struct mbuf *m, uintptr_t source, u_int *cpuid)
401 {
402 uint32_t hash_val, hash_type;
403 int ret;
404
405 M_ASSERTPKTHDR(m);
406
407 ret = rss_mbuf_software_hash_v6(m, RSS_HASH_PKT_INGRESS,
408 &hash_val, &hash_type);
409 if (ret > 0) {
410 /* mbuf has a valid hash already; don't need to modify it */
411 *cpuid = rss_hash2cpuid(m->m_pkthdr.flowid, M_HASHTYPE_GET(m));
412 } else if (ret == 0) {
413 /* hash was done; update */
414 m->m_pkthdr.flowid = hash_val;
415 M_HASHTYPE_SET(m, hash_type);
416 *cpuid = rss_hash2cpuid(m->m_pkthdr.flowid, M_HASHTYPE_GET(m));
417 } else { /* ret < 0 */
418 /* no hash was done */
419 *cpuid = NETISR_CPUID_NONE;
420 }
421 return (m);
422 }
Cache object: 9d276d8ffb0cba5527e5b7fb1ba1486f
|