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