FreeBSD/Linux Kernel Cross Reference
sys/netinet/in_rss.c
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 <netinet/in_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 IPv4 2-tuple.
67 */
68 uint32_t
69 rss_hash_ip4_2tuple(struct in_addr src, struct in_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 IPv4 4-tuple.
84 */
85 uint32_t
86 rss_hash_ip4_4tuple(struct in_addr src, u_short srcport, struct in_addr dst,
87 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 ipv4 2-tuple or 4-tuple given the given
107 * IPv4 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_v4(struct in_addr s, struct in_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_IPV4)) {
131 hash = rss_hash_ip4_4tuple(s, sp, d, dp);
132 *hashval = hash;
133 *hashtype = M_HASHTYPE_RSS_TCP_IPV4;
134 return (0);
135 } else if ((proto == IPPROTO_UDP) &&
136 (rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4)) {
137 hash = rss_hash_ip4_4tuple(s, sp, d, dp);
138 *hashval = hash;
139 *hashtype = M_HASHTYPE_RSS_UDP_IPV4;
140 return (0);
141 } else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) {
142 /* RSS doesn't hash on other protocols like SCTP; so 2-tuple */
143 hash = rss_hash_ip4_2tuple(s, d);
144 *hashval = hash;
145 *hashtype = M_HASHTYPE_RSS_IPV4;
146 return (0);
147 }
148
149 /* No configured available hashtypes! */
150 RSS_DEBUG("no available hashtypes!\n");
151 return (-1);
152 }
153
154 /*
155 * Do a software calculation of the RSS for the given mbuf.
156 *
157 * This is typically used by the input path to recalculate the RSS after
158 * some form of packet processing (eg de-capsulation, IP fragment reassembly.)
159 *
160 * dir is the packet direction - RSS_HASH_PKT_INGRESS for incoming and
161 * RSS_HASH_PKT_EGRESS for outgoing.
162 *
163 * Returns 0 if a hash was done, -1 if no hash was done, +1 if
164 * the mbuf already had a valid RSS flowid.
165 *
166 * This function doesn't modify the mbuf. It's up to the caller to
167 * assign flowid/flowtype as appropriate.
168 */
169 int
170 rss_mbuf_software_hash_v4(const struct mbuf *m, int dir, uint32_t *hashval,
171 uint32_t *hashtype)
172 {
173 const struct ip *ip;
174 const struct tcphdr *th;
175 const struct udphdr *uh;
176 uint32_t flowtype;
177 uint8_t proto;
178 int iphlen;
179 int is_frag = 0;
180
181 /*
182 * XXX For now this only handles hashing on incoming mbufs.
183 */
184 if (dir != RSS_HASH_PKT_INGRESS) {
185 RSS_DEBUG("called on EGRESS packet!\n");
186 return (-1);
187 }
188
189 /*
190 * First, validate that the mbuf we have is long enough
191 * to have an IPv4 header in it.
192 */
193 if (m->m_pkthdr.len < (sizeof(struct ip))) {
194 RSS_DEBUG("short mbuf pkthdr\n");
195 return (-1);
196 }
197 if (m->m_len < (sizeof(struct ip))) {
198 RSS_DEBUG("short mbuf len\n");
199 return (-1);
200 }
201
202 /* Ok, let's dereference that */
203 ip = mtod(m, struct ip *);
204 proto = ip->ip_p;
205 iphlen = ip->ip_hl << 2;
206
207 /*
208 * If this is a fragment then it shouldn't be four-tuple
209 * hashed just yet. Once it's reassembled into a full
210 * frame it should be re-hashed.
211 */
212 if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
213 is_frag = 1;
214
215 /*
216 * If the mbuf flowid/flowtype matches the packet type,
217 * and we don't support the 4-tuple version of the given protocol,
218 * then signal to the owner that it can trust the flowid/flowtype
219 * details.
220 *
221 * This is a little picky - eg, if TCPv4 / UDPv4 hashing
222 * is supported but we got a TCP/UDP frame only 2-tuple hashed,
223 * then we shouldn't just "trust" the 2-tuple hash. We need
224 * a 4-tuple hash.
225 */
226 flowtype = M_HASHTYPE_GET(m);
227
228 if (flowtype != M_HASHTYPE_NONE) {
229 switch (proto) {
230 case IPPROTO_UDP:
231 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4) &&
232 (flowtype == M_HASHTYPE_RSS_UDP_IPV4) &&
233 (is_frag == 0)) {
234 return (1);
235 }
236 /*
237 * Only allow 2-tuple for UDP frames if we don't also
238 * support 4-tuple for UDP.
239 */
240 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) &&
241 ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4) == 0) &&
242 flowtype == M_HASHTYPE_RSS_IPV4) {
243 return (1);
244 }
245 break;
246 case IPPROTO_TCP:
247 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4) &&
248 (flowtype == M_HASHTYPE_RSS_TCP_IPV4) &&
249 (is_frag == 0)) {
250 return (1);
251 }
252 /*
253 * Only allow 2-tuple for TCP frames if we don't also
254 * support 2-tuple for TCP.
255 */
256 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) &&
257 ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4) == 0) &&
258 flowtype == M_HASHTYPE_RSS_IPV4) {
259 return (1);
260 }
261 break;
262 default:
263 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) &&
264 flowtype == M_HASHTYPE_RSS_IPV4) {
265 return (1);
266 }
267 break;
268 }
269 }
270
271 /*
272 * Decode enough information to make a hash decision.
273 *
274 * XXX TODO: does the hardware hash on 4-tuple if IP
275 * options are present?
276 */
277 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4) &&
278 (proto == IPPROTO_TCP) &&
279 (is_frag == 0)) {
280 if (m->m_len < iphlen + sizeof(struct tcphdr)) {
281 RSS_DEBUG("short TCP frame?\n");
282 return (-1);
283 }
284 th = (const struct tcphdr *)((c_caddr_t)ip + iphlen);
285 return rss_proto_software_hash_v4(ip->ip_src, ip->ip_dst,
286 th->th_sport,
287 th->th_dport,
288 proto,
289 hashval,
290 hashtype);
291 } else if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4) &&
292 (proto == IPPROTO_UDP) &&
293 (is_frag == 0)) {
294 uh = (const struct udphdr *)((c_caddr_t)ip + iphlen);
295 if (m->m_len < iphlen + sizeof(struct udphdr)) {
296 RSS_DEBUG("short UDP frame?\n");
297 return (-1);
298 }
299 return rss_proto_software_hash_v4(ip->ip_src, ip->ip_dst,
300 uh->uh_sport,
301 uh->uh_dport,
302 proto,
303 hashval,
304 hashtype);
305 } else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) {
306 /* Default to 2-tuple hash */
307 return rss_proto_software_hash_v4(ip->ip_src, ip->ip_dst,
308 0, /* source port */
309 0, /* destination port */
310 0, /* IPPROTO_IP */
311 hashval,
312 hashtype);
313 } else {
314 RSS_DEBUG("no available hashtypes!\n");
315 return (-1);
316 }
317 }
318
319 /*
320 * Similar to rss_m2cpuid, but designed to be used by the IP NETISR
321 * on incoming frames.
322 *
323 * If an existing RSS hash exists and it matches what the configured
324 * hashing is, then use it.
325 *
326 * If there's an existing RSS hash but the desired hash is different,
327 * or if there's no useful RSS hash, then calculate it via
328 * the software path.
329 *
330 * XXX TODO: definitely want statistics here!
331 */
332 struct mbuf *
333 rss_soft_m2cpuid_v4(struct mbuf *m, uintptr_t source, u_int *cpuid)
334 {
335 uint32_t hash_val, hash_type;
336 int ret;
337
338 M_ASSERTPKTHDR(m);
339
340 ret = rss_mbuf_software_hash_v4(m, RSS_HASH_PKT_INGRESS,
341 &hash_val, &hash_type);
342 if (ret > 0) {
343 /* mbuf has a valid hash already; don't need to modify it */
344 *cpuid = rss_hash2cpuid(m->m_pkthdr.flowid, M_HASHTYPE_GET(m));
345 } else if (ret == 0) {
346 /* hash was done; update */
347 m->m_pkthdr.flowid = hash_val;
348 M_HASHTYPE_SET(m, hash_type);
349 *cpuid = rss_hash2cpuid(m->m_pkthdr.flowid, M_HASHTYPE_GET(m));
350 } else { /* ret < 0 */
351 /* no hash was done */
352 *cpuid = NETISR_CPUID_NONE;
353 }
354 return (m);
355 }
Cache object: 00c8df3c081aff24652a4c2962fbe241
|