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