2 * Copyright (c) 2010-2011 Juniper Networks, Inc.
5 * This software was developed by Robert N. M. Watson under contract
6 * to Juniper Networks, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
31 #include "opt_inet6.h"
32 #include "opt_pcbgroup.h"
35 #error "options RSS depends on options PCBGROUP"
38 #include <sys/param.h>
40 #include <sys/socket.h>
42 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
48 #include <net/if_var.h>
49 #include <net/netisr.h>
50 #include <net/rss_config.h>
52 #include <netinet/in.h>
53 #include <netinet/in_pcb.h>
54 #include <netinet/in_rss.h>
55 #include <netinet/in_var.h>
57 /* for software rss hash support */
58 #include <netinet/ip.h>
59 #include <netinet/tcp.h>
60 #include <netinet/udp.h>
63 * Hash an IPv4 2-tuple.
66 rss_hash_ip4_2tuple(struct in_addr src, struct in_addr dst)
68 uint8_t data[sizeof(src) + sizeof(dst)];
72 bcopy(&src, &data[datalen], sizeof(src));
73 datalen += sizeof(src);
74 bcopy(&dst, &data[datalen], sizeof(dst));
75 datalen += sizeof(dst);
76 return (rss_hash(datalen, data));
80 * Hash an IPv4 4-tuple.
83 rss_hash_ip4_4tuple(struct in_addr src, u_short srcport, struct in_addr dst,
86 uint8_t data[sizeof(src) + sizeof(dst) + sizeof(srcport) +
91 bcopy(&src, &data[datalen], sizeof(src));
92 datalen += sizeof(src);
93 bcopy(&dst, &data[datalen], sizeof(dst));
94 datalen += sizeof(dst);
95 bcopy(&srcport, &data[datalen], sizeof(srcport));
96 datalen += sizeof(srcport);
97 bcopy(&dstport, &data[datalen], sizeof(dstport));
98 datalen += sizeof(dstport);
99 return (rss_hash(datalen, data));
103 * Calculate an appropriate ipv4 2-tuple or 4-tuple given the given
104 * IPv4 source/destination address, UDP or TCP source/destination ports
105 * and the protocol type.
107 * The protocol code may wish to do a software hash of the given
108 * tuple. This depends upon the currently configured RSS hash types.
110 * This assumes that the packet in question isn't a fragment.
112 * It also assumes the packet source/destination address
113 * are in "incoming" packet order (ie, source is "far" address.)
116 rss_proto_software_hash_v4(struct in_addr s, struct in_addr d,
117 u_short sp, u_short dp, int proto,
118 uint32_t *hashval, uint32_t *hashtype)
123 * Next, choose the hash type depending upon the protocol
126 if ((proto == IPPROTO_TCP) &&
127 (rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4)) {
128 hash = rss_hash_ip4_4tuple(s, sp, d, dp);
130 *hashtype = M_HASHTYPE_RSS_TCP_IPV4;
132 } else if ((proto == IPPROTO_UDP) &&
133 (rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4)) {
134 hash = rss_hash_ip4_4tuple(s, sp, d, dp);
136 *hashtype = M_HASHTYPE_RSS_UDP_IPV4;
138 } else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) {
139 /* RSS doesn't hash on other protocols like SCTP; so 2-tuple */
140 hash = rss_hash_ip4_2tuple(s, d);
142 *hashtype = M_HASHTYPE_RSS_IPV4;
146 /* No configured available hashtypes! */
147 RSS_DEBUG("no available hashtypes!\n");
152 * Calculate an appropriate ipv4 2-tuple or 4-tuple given the given
153 * IPv4 source/destination address, UDP or TCP source/destination ports
154 * and the protocol type.
156 * The protocol code may wish to do a software hash of the given
157 * tuple. This depends upon the currently configured RSS hash types.
159 * It assumes the packet source/destination address
160 * are in "outgoing" packet order (ie, destination is "far" address.)
163 xps_proto_software_hash_v4(struct in_addr s, struct in_addr d,
164 u_short sp, u_short dp, int proto, uint32_t *hashtype)
169 * Next, choose the hash type depending upon the protocol
172 if ((proto == IPPROTO_TCP) &&
173 (rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4)) {
174 hash = rss_hash_ip4_4tuple(d, dp, s, sp);
175 *hashtype = M_HASHTYPE_RSS_TCP_IPV4;
177 } else if ((proto == IPPROTO_UDP) &&
178 (rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4)) {
179 hash = rss_hash_ip4_4tuple(d, dp, s, sp);
180 *hashtype = M_HASHTYPE_RSS_UDP_IPV4;
182 } else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) {
183 /* RSS doesn't hash on other protocols like SCTP; so 2-tuple */
184 hash = rss_hash_ip4_2tuple(d, s);
185 *hashtype = M_HASHTYPE_RSS_IPV4;
189 *hashtype = M_HASHTYPE_NONE;
194 * Do a software calculation of the RSS for the given mbuf.
196 * This is typically used by the input path to recalculate the RSS after
197 * some form of packet processing (eg de-capsulation, IP fragment reassembly.)
199 * dir is the packet direction - RSS_HASH_PKT_INGRESS for incoming and
200 * RSS_HASH_PKT_EGRESS for outgoing.
202 * Returns 0 if a hash was done, -1 if no hash was done, +1 if
203 * the mbuf already had a valid RSS flowid.
205 * This function doesn't modify the mbuf. It's up to the caller to
206 * assign flowid/flowtype as appropriate.
209 rss_mbuf_software_hash_v4(const struct mbuf *m, int dir, uint32_t *hashval,
213 const struct tcphdr *th;
214 const struct udphdr *uh;
221 * XXX For now this only handles hashing on incoming mbufs.
223 if (dir != RSS_HASH_PKT_INGRESS) {
224 RSS_DEBUG("called on EGRESS packet!\n");
229 * First, validate that the mbuf we have is long enough
230 * to have an IPv4 header in it.
232 if (m->m_pkthdr.len < (sizeof(struct ip))) {
233 RSS_DEBUG("short mbuf pkthdr\n");
236 if (m->m_len < (sizeof(struct ip))) {
237 RSS_DEBUG("short mbuf len\n");
241 /* Ok, let's dereference that */
242 ip = mtod(m, struct ip *);
244 iphlen = ip->ip_hl << 2;
247 * If this is a fragment then it shouldn't be four-tuple
248 * hashed just yet. Once it's reassembled into a full
249 * frame it should be re-hashed.
251 if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
255 * If the mbuf flowid/flowtype matches the packet type,
256 * and we don't support the 4-tuple version of the given protocol,
257 * then signal to the owner that it can trust the flowid/flowtype
260 * This is a little picky - eg, if TCPv4 / UDPv4 hashing
261 * is supported but we got a TCP/UDP frame only 2-tuple hashed,
262 * then we shouldn't just "trust" the 2-tuple hash. We need
265 flowtype = M_HASHTYPE_GET(m);
267 if (flowtype != M_HASHTYPE_NONE) {
270 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4) &&
271 (flowtype == M_HASHTYPE_RSS_UDP_IPV4) &&
276 * Only allow 2-tuple for UDP frames if we don't also
277 * support 4-tuple for UDP.
279 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) &&
280 ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4) == 0) &&
281 flowtype == M_HASHTYPE_RSS_IPV4) {
286 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4) &&
287 (flowtype == M_HASHTYPE_RSS_TCP_IPV4) &&
292 * Only allow 2-tuple for TCP frames if we don't also
293 * support 2-tuple for TCP.
295 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) &&
296 ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4) == 0) &&
297 flowtype == M_HASHTYPE_RSS_IPV4) {
302 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) &&
303 flowtype == M_HASHTYPE_RSS_IPV4) {
311 * Decode enough information to make a hash decision.
313 * XXX TODO: does the hardware hash on 4-tuple if IP
314 * options are present?
316 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4) &&
317 (proto == IPPROTO_TCP) &&
319 if (m->m_len < iphlen + sizeof(struct tcphdr)) {
320 RSS_DEBUG("short TCP frame?\n");
323 th = (const struct tcphdr *)((c_caddr_t)ip + iphlen);
324 return rss_proto_software_hash_v4(ip->ip_src, ip->ip_dst,
330 } else if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4) &&
331 (proto == IPPROTO_UDP) &&
333 uh = (const struct udphdr *)((c_caddr_t)ip + iphlen);
334 if (m->m_len < iphlen + sizeof(struct udphdr)) {
335 RSS_DEBUG("short UDP frame?\n");
338 return rss_proto_software_hash_v4(ip->ip_src, ip->ip_dst,
344 } else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) {
345 /* Default to 2-tuple hash */
346 return rss_proto_software_hash_v4(ip->ip_src, ip->ip_dst,
348 0, /* destination port */
353 RSS_DEBUG("no available hashtypes!\n");
359 * Similar to rss_m2cpuid, but designed to be used by the IP NETISR
360 * on incoming frames.
362 * If an existing RSS hash exists and it matches what the configured
363 * hashing is, then use it.
365 * If there's an existing RSS hash but the desired hash is different,
366 * or if there's no useful RSS hash, then calculate it via
369 * XXX TODO: definitely want statistics here!
372 rss_soft_m2cpuid_v4(struct mbuf *m, uintptr_t source, u_int *cpuid)
374 uint32_t hash_val, hash_type;
379 ret = rss_mbuf_software_hash_v4(m, RSS_HASH_PKT_INGRESS,
380 &hash_val, &hash_type);
382 /* mbuf has a valid hash already; don't need to modify it */
383 *cpuid = rss_hash2cpuid(m->m_pkthdr.flowid, M_HASHTYPE_GET(m));
384 } else if (ret == 0) {
385 /* hash was done; update */
386 m->m_pkthdr.flowid = hash_val;
387 M_HASHTYPE_SET(m, hash_type);
388 *cpuid = rss_hash2cpuid(m->m_pkthdr.flowid, M_HASHTYPE_GET(m));
389 } else { /* ret < 0 */
390 /* no hash was done */
391 *cpuid = NETISR_CPUID_NONE;