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