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