xref: /freebsd/sys/netinet6/in6_rss.c (revision bd81e07d2761cf1c13063eb49a5c0cb4a6951318)
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/ip.h>
62 #include <netinet/tcp.h>
63 #include <netinet/udp.h>
64 
65 /*
66  * Hash an IPv6 2-tuple.
67  */
68 uint32_t
69 rss_hash_ip6_2tuple(const struct in6_addr *src, const struct in6_addr *dst)
70 {
71 	uint8_t data[sizeof(*src) + sizeof(*dst)];
72 	u_int datalen;
73 
74 	datalen = 0;
75 	bcopy(src, &data[datalen], sizeof(*src));
76 	datalen += sizeof(*src);
77 	bcopy(dst, &data[datalen], sizeof(*dst));
78 	datalen += sizeof(*dst);
79 	return (rss_hash(datalen, data));
80 }
81 
82 /*
83  * Hash an IPv6 4-tuple.
84  */
85 uint32_t
86 rss_hash_ip6_4tuple(const struct in6_addr *src, u_short srcport,
87     const struct in6_addr *dst, u_short dstport)
88 {
89 	uint8_t data[sizeof(*src) + sizeof(*dst) + sizeof(srcport) +
90 	    sizeof(dstport)];
91 	u_int datalen;
92 
93 	datalen = 0;
94 	bcopy(src, &data[datalen], sizeof(*src));
95 	datalen += sizeof(*src);
96 	bcopy(dst, &data[datalen], sizeof(*dst));
97 	datalen += sizeof(*dst);
98 	bcopy(&srcport, &data[datalen], sizeof(srcport));
99 	datalen += sizeof(srcport);
100 	bcopy(&dstport, &data[datalen], sizeof(dstport));
101 	datalen += sizeof(dstport);
102 	return (rss_hash(datalen, data));
103 }
104 
105 /*
106  * Calculate an appropriate ipv6 2-tuple or 4-tuple given the given
107  * IPv6 source/destination address, UDP or TCP source/destination ports
108  * and the protocol type.
109  *
110  * The protocol code may wish to do a software hash of the given
111  * tuple.  This depends upon the currently configured RSS hash types.
112  *
113  * This assumes that the packet in question isn't a fragment.
114  *
115  * It also assumes the packet source/destination address
116  * are in "incoming" packet order (ie, source is "far" address.)
117  */
118 int
119 rss_proto_software_hash_v6(const struct in6_addr *s, const struct in6_addr *d,
120     u_short sp, u_short dp, int proto,
121     uint32_t *hashval, uint32_t *hashtype)
122 {
123 	uint32_t hash;
124 
125 	/*
126 	 * Next, choose the hash type depending upon the protocol
127 	 * identifier.
128 	 */
129 	if ((proto == IPPROTO_TCP) &&
130 	    (rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV6)) {
131 		hash = rss_hash_ip6_4tuple(s, sp, d, dp);
132 		*hashval = hash;
133 		*hashtype = M_HASHTYPE_RSS_TCP_IPV6;
134 		return (0);
135 	} else if ((proto == IPPROTO_UDP) &&
136 	    (rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV6)) {
137 		hash = rss_hash_ip6_4tuple(s, sp, d, dp);
138 		*hashval = hash;
139 		*hashtype = M_HASHTYPE_RSS_UDP_IPV6;
140 		return (0);
141 	} else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV6) {
142 		/* RSS doesn't hash on other protocols like SCTP; so 2-tuple */
143 		hash = rss_hash_ip6_2tuple(s, d);
144 		*hashval = hash;
145 		*hashtype = M_HASHTYPE_RSS_IPV6;
146 		return (0);
147 	}
148 
149 	/* No configured available hashtypes! */
150 	printf("%s: no available hashtypes!\n", __func__);
151 	return (-1);
152 }
153