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
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "opt_inet6.h"
35 #include "opt_pcbgroup.h"
38 #error "options RSS depends on options PCBGROUP"
41 #include <sys/param.h>
43 #include <sys/socket.h>
45 #include <sys/kernel.h>
47 #include <sys/sysctl.h>
51 #include <net/if_var.h>
52 #include <net/netisr.h>
53 #include <net/rss_config.h>
55 #include <netinet/in.h>
56 #include <netinet/in_pcb.h>
57 #include <netinet/in_rss.h>
58 #include <netinet/in_var.h>
60 /* for software rss hash support */
61 #include <netinet/ip.h>
62 #include <netinet/tcp.h>
63 #include <netinet/udp.h>
66 * Hash an IPv4 2-tuple.
69 rss_hash_ip4_2tuple(struct in_addr src, struct in_addr dst)
71 uint8_t data[sizeof(src) + sizeof(dst)];
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));
83 * Hash an IPv4 4-tuple.
86 rss_hash_ip4_4tuple(struct in_addr src, u_short srcport, struct in_addr dst,
89 uint8_t data[sizeof(src) + sizeof(dst) + sizeof(srcport) +
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));
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.
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.
113 * This assumes that the packet in question isn't a fragment.
115 * It also assumes the packet source/destination address
116 * are in "incoming" packet order (ie, source is "far" address.)
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)
126 * Next, choose the hash type depending upon the protocol
129 if ((proto == IPPROTO_TCP) &&
130 (rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4)) {
131 hash = rss_hash_ip4_4tuple(s, sp, d, dp);
133 *hashtype = M_HASHTYPE_RSS_TCP_IPV4;
135 } else if ((proto == IPPROTO_UDP) &&
136 (rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4)) {
137 hash = rss_hash_ip4_4tuple(s, sp, d, dp);
139 *hashtype = M_HASHTYPE_RSS_UDP_IPV4;
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);
145 *hashtype = M_HASHTYPE_RSS_IPV4;
149 /* No configured available hashtypes! */
150 RSS_DEBUG("no available hashtypes!\n");
155 * Calculate an appropriate ipv4 2-tuple or 4-tuple given the given
156 * IPv4 source/destination address, UDP or TCP source/destination ports
157 * and the protocol type.
159 * The protocol code may wish to do a software hash of the given
160 * tuple. This depends upon the currently configured RSS hash types.
162 * It assumes the packet source/destination address
163 * are in "outgoing" packet order (ie, destination is "far" address.)
166 xps_proto_software_hash_v4(struct in_addr s, struct in_addr d,
167 u_short sp, u_short dp, int proto, uint32_t *hashtype)
172 * Next, choose the hash type depending upon the protocol
175 if ((proto == IPPROTO_TCP) &&
176 (rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4)) {
177 hash = rss_hash_ip4_4tuple(d, dp, s, sp);
178 *hashtype = M_HASHTYPE_RSS_TCP_IPV4;
180 } else if ((proto == IPPROTO_UDP) &&
181 (rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4)) {
182 hash = rss_hash_ip4_4tuple(d, dp, s, sp);
183 *hashtype = M_HASHTYPE_RSS_UDP_IPV4;
185 } else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) {
186 /* RSS doesn't hash on other protocols like SCTP; so 2-tuple */
187 hash = rss_hash_ip4_2tuple(d, s);
188 *hashtype = M_HASHTYPE_RSS_IPV4;
192 *hashtype = M_HASHTYPE_NONE;
197 * Do a software calculation of the RSS for the given mbuf.
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.)
202 * dir is the packet direction - RSS_HASH_PKT_INGRESS for incoming and
203 * RSS_HASH_PKT_EGRESS for outgoing.
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.
208 * This function doesn't modify the mbuf. It's up to the caller to
209 * assign flowid/flowtype as appropriate.
212 rss_mbuf_software_hash_v4(const struct mbuf *m, int dir, uint32_t *hashval,
216 const struct tcphdr *th;
217 const struct udphdr *uh;
224 * XXX For now this only handles hashing on incoming mbufs.
226 if (dir != RSS_HASH_PKT_INGRESS) {
227 RSS_DEBUG("called on EGRESS packet!\n");
232 * First, validate that the mbuf we have is long enough
233 * to have an IPv4 header in it.
235 if (m->m_pkthdr.len < (sizeof(struct ip))) {
236 RSS_DEBUG("short mbuf pkthdr\n");
239 if (m->m_len < (sizeof(struct ip))) {
240 RSS_DEBUG("short mbuf len\n");
244 /* Ok, let's dereference that */
245 ip = mtod(m, struct ip *);
247 iphlen = ip->ip_hl << 2;
250 * If this is a fragment then it shouldn't be four-tuple
251 * hashed just yet. Once it's reassembled into a full
252 * frame it should be re-hashed.
254 if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
258 * If the mbuf flowid/flowtype matches the packet type,
259 * and we don't support the 4-tuple version of the given protocol,
260 * then signal to the owner that it can trust the flowid/flowtype
263 * This is a little picky - eg, if TCPv4 / UDPv4 hashing
264 * is supported but we got a TCP/UDP frame only 2-tuple hashed,
265 * then we shouldn't just "trust" the 2-tuple hash. We need
268 flowtype = M_HASHTYPE_GET(m);
270 if (flowtype != M_HASHTYPE_NONE) {
273 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4) &&
274 (flowtype == M_HASHTYPE_RSS_UDP_IPV4) &&
279 * Only allow 2-tuple for UDP frames if we don't also
280 * support 4-tuple for UDP.
282 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) &&
283 ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4) == 0) &&
284 flowtype == M_HASHTYPE_RSS_IPV4) {
289 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4) &&
290 (flowtype == M_HASHTYPE_RSS_TCP_IPV4) &&
295 * Only allow 2-tuple for TCP frames if we don't also
296 * support 2-tuple for TCP.
298 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) &&
299 ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4) == 0) &&
300 flowtype == M_HASHTYPE_RSS_IPV4) {
305 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) &&
306 flowtype == M_HASHTYPE_RSS_IPV4) {
314 * Decode enough information to make a hash decision.
316 * XXX TODO: does the hardware hash on 4-tuple if IP
317 * options are present?
319 if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4) &&
320 (proto == IPPROTO_TCP) &&
322 if (m->m_len < iphlen + sizeof(struct tcphdr)) {
323 RSS_DEBUG("short TCP frame?\n");
326 th = (const struct tcphdr *)((c_caddr_t)ip + iphlen);
327 return rss_proto_software_hash_v4(ip->ip_src, ip->ip_dst,
333 } else if ((rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4) &&
334 (proto == IPPROTO_UDP) &&
336 uh = (const struct udphdr *)((c_caddr_t)ip + iphlen);
337 if (m->m_len < iphlen + sizeof(struct udphdr)) {
338 RSS_DEBUG("short UDP frame?\n");
341 return rss_proto_software_hash_v4(ip->ip_src, ip->ip_dst,
347 } else if (rss_gethashconfig() & RSS_HASHTYPE_RSS_IPV4) {
348 /* Default to 2-tuple hash */
349 return rss_proto_software_hash_v4(ip->ip_src, ip->ip_dst,
351 0, /* destination port */
356 RSS_DEBUG("no available hashtypes!\n");
362 * Similar to rss_m2cpuid, but designed to be used by the IP NETISR
363 * on incoming frames.
365 * If an existing RSS hash exists and it matches what the configured
366 * hashing is, then use it.
368 * If there's an existing RSS hash but the desired hash is different,
369 * or if there's no useful RSS hash, then calculate it via
372 * XXX TODO: definitely want statistics here!
375 rss_soft_m2cpuid_v4(struct mbuf *m, uintptr_t source, u_int *cpuid)
377 uint32_t hash_val, hash_type;
382 ret = rss_mbuf_software_hash_v4(m, RSS_HASH_PKT_INGRESS,
383 &hash_val, &hash_type);
385 /* mbuf has a valid hash already; don't need to modify it */
386 *cpuid = rss_hash2cpuid(m->m_pkthdr.flowid, M_HASHTYPE_GET(m));
387 } else if (ret == 0) {
388 /* hash was done; update */
389 m->m_pkthdr.flowid = hash_val;
390 M_HASHTYPE_SET(m, hash_type);
391 *cpuid = rss_hash2cpuid(m->m_pkthdr.flowid, M_HASHTYPE_GET(m));
392 } else { /* ret < 0 */
393 /* no hash was done */
394 *cpuid = NETISR_CPUID_NONE;