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_pcbgroup.h" 36 37 #ifndef PCBGROUP 38 #error "options RSS depends on options PCBGROUP" 39 #endif 40 41 #include <sys/param.h> 42 #include <sys/mbuf.h> 43 #include <sys/socket.h> 44 #include <sys/priv.h> 45 #include <sys/kernel.h> 46 #include <sys/smp.h> 47 #include <sys/sysctl.h> 48 49 #include <net/if.h> 50 #include <net/if_var.h> 51 #include <net/netisr.h> 52 53 #include <netinet/in.h> 54 #include <netinet/in_pcb.h> 55 #include <netinet/in_rss.h> 56 #include <netinet/in_var.h> 57 #include <netinet/toeplitz.h> 58 59 /*- 60 * Operating system parts of receiver-side scaling (RSS), which allows 61 * network cards to direct flows to particular receive queues based on hashes 62 * of header tuples. This implementation aligns RSS buckets with connection 63 * groups at the TCP/IP layer, so each bucket is associated with exactly one 64 * group. As a result, the group lookup structures (and lock) should have an 65 * effective affinity with exactly one CPU. 66 * 67 * Network device drivers needing to configure RSS will query this framework 68 * for parameters, such as the current RSS key, hashing policies, number of 69 * bits, and indirection table mapping hashes to buckets and CPUs. They may 70 * provide their own supplementary information, such as queue<->CPU bindings. 71 * It is the responsibility of the network device driver to inject packets 72 * into the stack on as close to the right CPU as possible, if playing by RSS 73 * rules. 74 * 75 * TODO: 76 * 77 * - Synchronization for rss_key and other future-configurable parameters. 78 * - Event handler drivers can register to pick up RSS configuration changes. 79 * - Should we allow rss_basecpu to be configured? 80 * - Randomize key on boot. 81 * - IPv6 support. 82 * - Statistics on how often there's a misalignment between hardware 83 * placement and pcbgroup expectations. 84 */ 85 86 SYSCTL_NODE(_net_inet, OID_AUTO, rss, CTLFLAG_RW, 0, "Receive-side steering"); 87 88 /* 89 * Toeplitz is the only required hash function in the RSS spec, so use it by 90 * default. 91 */ 92 static u_int rss_hashalgo = RSS_HASH_TOEPLITZ; 93 SYSCTL_INT(_net_inet_rss, OID_AUTO, hashalgo, CTLFLAG_RD, &rss_hashalgo, 0, 94 "RSS hash algorithm"); 95 TUNABLE_INT("net.inet.rss.hashalgo", &rss_hashalgo); 96 97 /* 98 * Size of the indirection table; at most 128 entries per the RSS spec. We 99 * size it to at least 2 times the number of CPUs by default to allow useful 100 * rebalancing. If not set explicitly with a loader tunable, we tune based 101 * on the number of CPUs present. 102 * 103 * XXXRW: buckets might be better to use for the tunable than bits. 104 */ 105 static u_int rss_bits; 106 SYSCTL_INT(_net_inet_rss, OID_AUTO, bits, CTLFLAG_RD, &rss_bits, 0, 107 "RSS bits"); 108 TUNABLE_INT("net.inet.rss.bits", &rss_bits); 109 110 static u_int rss_mask; 111 SYSCTL_INT(_net_inet_rss, OID_AUTO, mask, CTLFLAG_RD, &rss_mask, 0, 112 "RSS mask"); 113 114 static const u_int rss_maxbits = RSS_MAXBITS; 115 SYSCTL_INT(_net_inet_rss, OID_AUTO, maxbits, CTLFLAG_RD, 116 __DECONST(int *, &rss_maxbits), 0, "RSS maximum bits"); 117 118 /* 119 * RSS's own count of the number of CPUs it could be using for processing. 120 * Bounded to 64 by RSS constants. 121 */ 122 static u_int rss_ncpus; 123 SYSCTL_INT(_net_inet_rss, OID_AUTO, ncpus, CTLFLAG_RD, &rss_ncpus, 0, 124 "Number of CPUs available to RSS"); 125 126 #define RSS_MAXCPUS (1 << (RSS_MAXBITS - 1)) 127 static const u_int rss_maxcpus = RSS_MAXCPUS; 128 SYSCTL_INT(_net_inet_rss, OID_AUTO, maxcpus, CTLFLAG_RD, 129 __DECONST(int *, &rss_maxcpus), 0, "RSS maximum CPUs that can be used"); 130 131 /* 132 * Variable exists just for reporting rss_bits in a user-friendly way. 133 */ 134 static u_int rss_buckets; 135 SYSCTL_INT(_net_inet_rss, OID_AUTO, buckets, CTLFLAG_RD, &rss_buckets, 0, 136 "RSS buckets"); 137 138 /* 139 * Base CPU number; devices will add this to all CPU numbers returned by the 140 * RSS indirection table. Currently unmodifable in FreeBSD. 141 */ 142 static const u_int rss_basecpu; 143 SYSCTL_INT(_net_inet_rss, OID_AUTO, basecpu, CTLFLAG_RD, 144 __DECONST(int *, &rss_basecpu), 0, "RSS base CPU"); 145 146 /* 147 * RSS secret key, intended to prevent attacks on load-balancing. Its 148 * effectiveness may be limited by algorithm choice and available entropy 149 * during the boot. 150 * 151 * XXXRW: And that we don't randomize it yet! 152 * 153 * XXXRW: This default is actually the default key from Chelsio T3 cards, as 154 * it offers reasonable distribution, unlike all-0 keys which always 155 * generate a hash of 0 (upsettingly). 156 */ 157 static uint8_t rss_key[RSS_KEYSIZE] = { 158 0x43, 0xa3, 0x8f, 0xb0, 0x41, 0x67, 0x25, 0x3d, 159 0x25, 0x5b, 0x0e, 0xc2, 0x6d, 0x5a, 0x56, 0xda, 160 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 161 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 162 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 163 }; 164 165 /* 166 * RSS hash->CPU table, which maps hashed packet headers to particular CPUs. 167 * Drivers may supplement this table with a seperate CPU<->queue table when 168 * programming devices. 169 */ 170 struct rss_table_entry { 171 uint8_t rte_cpu; /* CPU affinity of bucket. */ 172 }; 173 static struct rss_table_entry rss_table[RSS_TABLE_MAXLEN]; 174 175 static void 176 rss_init(__unused void *arg) 177 { 178 u_int i; 179 u_int cpuid; 180 181 /* 182 * Validate tunables, coerce to sensible values. 183 */ 184 switch (rss_hashalgo) { 185 case RSS_HASH_TOEPLITZ: 186 case RSS_HASH_NAIVE: 187 break; 188 189 default: 190 printf("%s: invalid RSS hashalgo %u, coercing to %u", 191 __func__, rss_hashalgo, RSS_HASH_TOEPLITZ); 192 rss_hashalgo = RSS_HASH_TOEPLITZ; 193 } 194 195 /* 196 * Count available CPUs. 197 * 198 * XXXRW: Note incorrect assumptions regarding contiguity of this set 199 * elsewhere. 200 */ 201 rss_ncpus = 0; 202 for (i = 0; i <= mp_maxid; i++) { 203 if (CPU_ABSENT(i)) 204 continue; 205 rss_ncpus++; 206 } 207 if (rss_ncpus > RSS_MAXCPUS) 208 rss_ncpus = RSS_MAXCPUS; 209 210 /* 211 * Tune RSS table entries to be no less than 2x the number of CPUs 212 * -- unless we're running uniprocessor, in which case there's not 213 * much point in having buckets to rearrange for load-balancing! 214 */ 215 if (rss_ncpus > 1) { 216 if (rss_bits == 0) 217 rss_bits = fls(rss_ncpus - 1) + 1; 218 219 /* 220 * Microsoft limits RSS table entries to 128, so apply that 221 * limit to both auto-detected CPU counts and user-configured 222 * ones. 223 */ 224 if (rss_bits == 0 || rss_bits > RSS_MAXBITS) { 225 printf("%s: RSS bits %u not valid, coercing to %u", 226 __func__, rss_bits, RSS_MAXBITS); 227 rss_bits = RSS_MAXBITS; 228 } 229 230 /* 231 * Figure out how many buckets to use; warn if less than the 232 * number of configured CPUs, although this is not a fatal 233 * problem. 234 */ 235 rss_buckets = (1 << rss_bits); 236 if (rss_buckets < rss_ncpus) 237 printf("%s: WARNING: rss_buckets (%u) less than " 238 "rss_ncpus (%u)\n", __func__, rss_buckets, 239 rss_ncpus); 240 rss_mask = rss_buckets - 1; 241 } else { 242 rss_bits = 0; 243 rss_buckets = 1; 244 rss_mask = 0; 245 } 246 247 /* 248 * Set up initial CPU assignments: round-robin by default. 249 */ 250 cpuid = CPU_FIRST(); 251 for (i = 0; i < rss_buckets; i++) { 252 rss_table[i].rte_cpu = cpuid; 253 cpuid = CPU_NEXT(cpuid); 254 } 255 256 /* 257 * Randomize rrs_key. 258 * 259 * XXXRW: Not yet. If nothing else, will require an rss_isbadkey() 260 * loop to check for "bad" RSS keys. 261 */ 262 } 263 SYSINIT(rss_init, SI_SUB_SOFTINTR, SI_ORDER_SECOND, rss_init, NULL); 264 265 static uint32_t 266 rss_naive_hash(u_int keylen, const uint8_t *key, u_int datalen, 267 const uint8_t *data) 268 { 269 uint32_t v; 270 u_int i; 271 272 v = 0; 273 for (i = 0; i < keylen; i++) 274 v += key[i]; 275 for (i = 0; i < datalen; i++) 276 v += data[i]; 277 return (v); 278 } 279 280 static uint32_t 281 rss_hash(u_int datalen, const uint8_t *data) 282 { 283 284 switch (rss_hashalgo) { 285 case RSS_HASH_TOEPLITZ: 286 return (toeplitz_hash(sizeof(rss_key), rss_key, datalen, 287 data)); 288 289 case RSS_HASH_NAIVE: 290 return (rss_naive_hash(sizeof(rss_key), rss_key, datalen, 291 data)); 292 293 default: 294 panic("%s: unsupported/unknown hashalgo %d", __func__, 295 rss_hashalgo); 296 } 297 } 298 299 /* 300 * Hash an IPv4 2-tuple. 301 */ 302 uint32_t 303 rss_hash_ip4_2tuple(struct in_addr src, struct in_addr dst) 304 { 305 uint8_t data[sizeof(src) + sizeof(dst)]; 306 u_int datalen; 307 308 datalen = 0; 309 bcopy(&src, &data[datalen], sizeof(src)); 310 datalen += sizeof(src); 311 bcopy(&dst, &data[datalen], sizeof(dst)); 312 datalen += sizeof(dst); 313 return (rss_hash(datalen, data)); 314 } 315 316 /* 317 * Hash an IPv4 4-tuple. 318 */ 319 uint32_t 320 rss_hash_ip4_4tuple(struct in_addr src, u_short srcport, struct in_addr dst, 321 u_short dstport) 322 { 323 uint8_t data[sizeof(src) + sizeof(dst) + sizeof(srcport) + 324 sizeof(dstport)]; 325 u_int datalen; 326 327 datalen = 0; 328 bcopy(&src, &data[datalen], sizeof(src)); 329 datalen += sizeof(src); 330 bcopy(&dst, &data[datalen], sizeof(dst)); 331 datalen += sizeof(dst); 332 bcopy(&srcport, &data[datalen], sizeof(srcport)); 333 datalen += sizeof(srcport); 334 bcopy(&dstport, &data[datalen], sizeof(dstport)); 335 datalen += sizeof(dstport); 336 return (rss_hash(datalen, data)); 337 } 338 339 #ifdef INET6 340 /* 341 * Hash an IPv6 2-tuple. 342 */ 343 uint32_t 344 rss_hash_ip6_2tuple(struct in6_addr src, struct in6_addr dst) 345 { 346 uint8_t data[sizeof(src) + sizeof(dst)]; 347 u_int datalen; 348 349 datalen = 0; 350 bcopy(&src, &data[datalen], sizeof(src)); 351 datalen += sizeof(src); 352 bcopy(&dst, &data[datalen], sizeof(dst)); 353 datalen += sizeof(dst); 354 return (rss_hash(datalen, data)); 355 } 356 357 /* 358 * Hash an IPv6 4-tuple. 359 */ 360 uint32_t 361 rss_hash_ip6_4tuple(struct in6_addr src, u_short srcport, 362 struct in6_addr dst, u_short dstport) 363 { 364 uint8_t data[sizeof(src) + sizeof(dst) + sizeof(srcport) + 365 sizeof(dstport)]; 366 u_int datalen; 367 368 datalen = 0; 369 bcopy(&src, &data[datalen], sizeof(src)); 370 datalen += sizeof(src); 371 bcopy(&dst, &data[datalen], sizeof(dst)); 372 datalen += sizeof(dst); 373 bcopy(&srcport, &data[datalen], sizeof(srcport)); 374 datalen += sizeof(srcport); 375 bcopy(&dstport, &data[datalen], sizeof(dstport)); 376 datalen += sizeof(dstport); 377 return (rss_hash(datalen, data)); 378 } 379 #endif /* INET6 */ 380 381 /* 382 * Query the number of RSS bits in use. 383 */ 384 u_int 385 rss_getbits(void) 386 { 387 388 return (rss_bits); 389 } 390 391 /* 392 * Query the RSS bucket associated with an RSS hash. 393 */ 394 u_int 395 rss_getbucket(u_int hash) 396 { 397 398 return (hash & rss_mask); 399 } 400 401 /* 402 * Query the RSS CPU associated with an RSS bucket. 403 */ 404 u_int 405 rss_getcpu(u_int bucket) 406 { 407 408 return (rss_table[bucket].rte_cpu); 409 } 410 411 /* 412 * netisr CPU affinity lookup given just the hash and hashtype. 413 */ 414 u_int 415 rss_hash2cpuid(uint32_t hash_val, uint32_t hash_type) 416 { 417 418 switch (hash_type) { 419 case M_HASHTYPE_RSS_IPV4: 420 case M_HASHTYPE_RSS_TCP_IPV4: 421 return (rss_getcpu(rss_getbucket(hash_val))); 422 default: 423 return (NETISR_CPUID_NONE); 424 } 425 } 426 427 /* 428 * netisr CPU affinity lookup routine for use by protocols. 429 */ 430 struct mbuf * 431 rss_m2cpuid(struct mbuf *m, uintptr_t source, u_int *cpuid) 432 { 433 434 M_ASSERTPKTHDR(m); 435 *cpuid = rss_hash2cpuid(m->m_pkthdr.flowid, M_HASHTYPE_GET(m)); 436 return (m); 437 } 438 439 /* 440 * Query the RSS hash algorithm. 441 */ 442 u_int 443 rss_gethashalgo(void) 444 { 445 446 return (rss_hashalgo); 447 } 448 449 /* 450 * Query the current RSS key; likely to be used by device drivers when 451 * configuring hardware RSS. Caller must pass an array of size RSS_KEYSIZE. 452 * 453 * XXXRW: Perhaps we should do the accept-a-length-and-truncate thing? 454 */ 455 void 456 rss_getkey(uint8_t *key) 457 { 458 459 bcopy(rss_key, key, sizeof(rss_key)); 460 } 461 462 /* 463 * Query the number of buckets; this may be used by both network device 464 * drivers, which will need to populate hardware shadows of the software 465 * indirection table, and the network stack itself (such as when deciding how 466 * many connection groups to allocate). 467 */ 468 u_int 469 rss_getnumbuckets(void) 470 { 471 472 return (rss_buckets); 473 } 474 475 /* 476 * Query the number of CPUs in use by RSS; may be useful to device drivers 477 * trying to figure out how to map a larger number of CPUs into a smaller 478 * number of receive queues. 479 */ 480 u_int 481 rss_getnumcpus(void) 482 { 483 484 return (rss_ncpus); 485 } 486 487 /* 488 * XXXRW: Confirm that sysctl -a won't dump this keying material, don't want 489 * it appearing in debugging output unnecessarily. 490 */ 491 static int 492 sysctl_rss_key(SYSCTL_HANDLER_ARGS) 493 { 494 uint8_t temp_rss_key[RSS_KEYSIZE]; 495 int error; 496 497 error = priv_check(req->td, PRIV_NETINET_HASHKEY); 498 if (error) 499 return (error); 500 501 bcopy(rss_key, temp_rss_key, sizeof(temp_rss_key)); 502 error = sysctl_handle_opaque(oidp, temp_rss_key, 503 sizeof(temp_rss_key), req); 504 if (error) 505 return (error); 506 if (req->newptr != NULL) { 507 /* XXXRW: Not yet. */ 508 return (EINVAL); 509 } 510 return (0); 511 } 512 SYSCTL_PROC(_net_inet_rss, OID_AUTO, key, 513 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, sysctl_rss_key, 514 "", "RSS keying material"); 515