]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/in6_pcbgroup.c
Merge llvm 3.5.0 release from ^/vendor/llvm/dist, resolve conflicts, and
[FreeBSD/FreeBSD.git] / sys / netinet6 / in6_pcbgroup.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_rss.h"
36
37 #include <sys/param.h>
38 #include <sys/mbuf.h>
39
40 #include <netinet/in.h>
41 #include <netinet/in_pcb.h>
42 #include <netinet/in_rss.h>
43 #ifdef INET6
44 #include <netinet6/in6_pcb.h>
45 #endif /* INET6 */
46
47 /*
48  * Given a hash of whatever the covered tuple might be, return a pcbgroup
49  * index.  Where RSS is supported, try to align bucket selection with RSS CPU
50  * affinity strategy.
51  */
52 static __inline u_int
53 in6_pcbgroup_getbucket(struct inpcbinfo *pcbinfo, uint32_t hash)
54 {
55
56 #ifdef RSS
57         return (rss_getbucket(hash));
58 #else
59         return (hash % pcbinfo->ipi_npcbgroups);
60 #endif
61 }
62
63 /*
64  * Map a (hashtype, hash) tuple into a connection group, or NULL if the hash 
65  * information is insufficient to identify the pcbgroup.  This might occur if
66  * a TCP packet turnsup with a 2-tuple hash, or if an RSS hash is present but
67  * RSS is not compiled into the kernel.
68  */
69 struct inpcbgroup *
70 in6_pcbgroup_byhash(struct inpcbinfo *pcbinfo, u_int hashtype, uint32_t hash)
71 {
72
73 #ifdef RSS
74         if ((pcbinfo->ipi_hashfields == IPI_HASHFIELDS_4TUPLE &&
75             hashtype == M_HASHTYPE_RSS_TCP_IPV6) ||
76             (pcbinfo->ipi_hashfields == IPI_HASHFIELDS_4TUPLE &&
77             hashtype == M_HASHTYPE_RSS_UDP_IPV6) ||
78             (pcbinfo->ipi_hashfields == IPI_HASHFIELDS_2TUPLE &&
79             hashtype == M_HASHTYPE_RSS_IPV6))
80                 return (&pcbinfo->ipi_pcbgroups[
81                     in6_pcbgroup_getbucket(pcbinfo, hash)]);
82 #endif
83         return (NULL);
84 }
85
86 struct inpcbgroup *
87 in6_pcbgroup_bymbuf(struct inpcbinfo *pcbinfo, struct mbuf *m)
88 {
89
90         return (in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
91             m->m_pkthdr.flowid));
92 }
93
94 struct inpcbgroup *
95 in6_pcbgroup_bytuple(struct inpcbinfo *pcbinfo, const struct in6_addr *laddrp,
96     u_short lport, const struct in6_addr *faddrp, u_short fport)
97 {
98         uint32_t hash;
99
100         /*
101          * RSS note: we pass foreign addr/port as source, and local addr/port
102          * as destination, as we want to align with what the hardware is
103          * doing.
104          */
105         switch (pcbinfo->ipi_hashfields) {
106         case IPI_HASHFIELDS_4TUPLE:
107 #ifdef RSS
108                 hash = rss_hash_ip6_4tuple(*faddrp, fport, *laddrp, lport);
109 #else
110                 hash = faddrp->s6_addr32[3] ^ fport;
111 #endif
112                 break;
113
114         case IPI_HASHFIELDS_2TUPLE:
115 #ifdef RSS
116                 hash = rss_hash_ip6_2tuple(*faddrp, *laddrp);
117 #else
118                 hash = faddrp->s6_addr32[3] ^ laddrp->s6_addr32[3];
119 #endif
120                 break;
121
122         default:
123                 hash = 0;
124         }
125         return (&pcbinfo->ipi_pcbgroups[in6_pcbgroup_getbucket(pcbinfo,
126             hash)]);
127 }
128
129 struct inpcbgroup *
130 in6_pcbgroup_byinpcb(struct inpcb *inp)
131 {
132
133 #ifdef  RSS
134         /*
135          * Listen sockets with INP_RSS_BUCKET_SET set have a pre-determined
136          * RSS bucket and thus we should use this pcbgroup, rather than
137          * using a tuple or hash.
138          *
139          * XXX should verify that there's actually pcbgroups and inp_rss_listen_bucket
140          * fits in that!
141          */
142         if (inp->inp_flags2 & INP_RSS_BUCKET_SET)
143                 return (&inp->inp_pcbinfo->ipi_pcbgroups[inp->inp_rss_listen_bucket]);
144 #endif
145
146         return (in6_pcbgroup_bytuple(inp->inp_pcbinfo, &inp->in6p_laddr,
147             inp->inp_lport, &inp->in6p_faddr, inp->inp_fport));
148 }