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