1b2bdc62aSAdrian Chadd /*- 2b2bdc62aSAdrian Chadd * Copyright (c) 2010-2011 Juniper Networks, Inc. 3b2bdc62aSAdrian Chadd * All rights reserved. 4b2bdc62aSAdrian Chadd * 5b2bdc62aSAdrian Chadd * This software was developed by Robert N. M. Watson under contract 6b2bdc62aSAdrian Chadd * to Juniper Networks, Inc. 7b2bdc62aSAdrian Chadd * 8b2bdc62aSAdrian Chadd * Redistribution and use in source and binary forms, with or without 9b2bdc62aSAdrian Chadd * modification, are permitted provided that the following conditions 10b2bdc62aSAdrian Chadd * are met: 11b2bdc62aSAdrian Chadd * 1. Redistributions of source code must retain the above copyright 12b2bdc62aSAdrian Chadd * notice, this list of conditions and the following disclaimer. 13b2bdc62aSAdrian Chadd * 2. Redistributions in binary form must reproduce the above copyright 14b2bdc62aSAdrian Chadd * notice, this list of conditions and the following disclaimer in the 15b2bdc62aSAdrian Chadd * documentation and/or other materials provided with the distribution. 16b2bdc62aSAdrian Chadd * 17b2bdc62aSAdrian Chadd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18b2bdc62aSAdrian Chadd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19b2bdc62aSAdrian Chadd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20b2bdc62aSAdrian Chadd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21b2bdc62aSAdrian Chadd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22b2bdc62aSAdrian Chadd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23b2bdc62aSAdrian Chadd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24b2bdc62aSAdrian Chadd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25b2bdc62aSAdrian Chadd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26b2bdc62aSAdrian Chadd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27b2bdc62aSAdrian Chadd * SUCH DAMAGE. 28b2bdc62aSAdrian Chadd */ 29b2bdc62aSAdrian Chadd 30b2bdc62aSAdrian Chadd #ifndef _NET_RSS_CONFIG_H_ 31b2bdc62aSAdrian Chadd #define _NET_RSS_CONFIG_H_ 32b2bdc62aSAdrian Chadd 33b2bdc62aSAdrian Chadd #include <netinet/in.h> /* in_addr_t */ 34b2bdc62aSAdrian Chadd 35b2bdc62aSAdrian Chadd /* 36b2bdc62aSAdrian Chadd * Supported RSS hash functions. 37b2bdc62aSAdrian Chadd */ 38b2bdc62aSAdrian Chadd #define RSS_HASH_NAIVE 0x00000001 /* Poor but fast hash. */ 39b2bdc62aSAdrian Chadd #define RSS_HASH_TOEPLITZ 0x00000002 /* Required by RSS. */ 40b2bdc62aSAdrian Chadd #define RSS_HASH_CRC32 0x00000004 /* Future; some NICs do it. */ 41b2bdc62aSAdrian Chadd 42b2bdc62aSAdrian Chadd #define RSS_HASH_MASK (RSS_HASH_NAIVE | RSS_HASH_TOEPLITZ) 43b2bdc62aSAdrian Chadd 44b2bdc62aSAdrian Chadd /* 45b2bdc62aSAdrian Chadd * Instances of struct inpcbinfo declare an RSS hash type indicating what 46b2bdc62aSAdrian Chadd * header fields are covered. 47b2bdc62aSAdrian Chadd */ 48b2bdc62aSAdrian Chadd #define RSS_HASHFIELDS_NONE 0 49b2bdc62aSAdrian Chadd #define RSS_HASHFIELDS_4TUPLE 1 50b2bdc62aSAdrian Chadd #define RSS_HASHFIELDS_2TUPLE 2 51b2bdc62aSAdrian Chadd 52b2bdc62aSAdrian Chadd /* 53b2bdc62aSAdrian Chadd * Define RSS representations of the M_HASHTYPE_* values, representing 54b2bdc62aSAdrian Chadd * which particular bits are supported. The NICs can then use this to 55b2bdc62aSAdrian Chadd * calculate which hash types to enable and which not to enable. 56b2bdc62aSAdrian Chadd * 57b2bdc62aSAdrian Chadd * The fact that these line up with M_HASHTYPE_* is not to be relied 58b2bdc62aSAdrian Chadd * upon. 59b2bdc62aSAdrian Chadd */ 60b2bdc62aSAdrian Chadd #define RSS_HASHTYPE_RSS_IPV4 (1 << 1) /* IPv4 2-tuple */ 61b2bdc62aSAdrian Chadd #define RSS_HASHTYPE_RSS_TCP_IPV4 (1 << 2) /* TCPv4 4-tuple */ 62b2bdc62aSAdrian Chadd #define RSS_HASHTYPE_RSS_IPV6 (1 << 3) /* IPv6 2-tuple */ 63b2bdc62aSAdrian Chadd #define RSS_HASHTYPE_RSS_TCP_IPV6 (1 << 4) /* TCPv6 4-tuple */ 64b2bdc62aSAdrian Chadd #define RSS_HASHTYPE_RSS_IPV6_EX (1 << 5) /* IPv6 2-tuple + ext hdrs */ 65b2bdc62aSAdrian Chadd #define RSS_HASHTYPE_RSS_TCP_IPV6_EX (1 << 6) /* TCPv6 4-tiple + ext hdrs */ 66b2bdc62aSAdrian Chadd #define RSS_HASHTYPE_RSS_UDP_IPV4 (1 << 7) /* IPv4 UDP 4-tuple */ 67b2bdc62aSAdrian Chadd #define RSS_HASHTYPE_RSS_UDP_IPV6 (1 << 9) /* IPv6 UDP 4-tuple */ 68b2bdc62aSAdrian Chadd #define RSS_HASHTYPE_RSS_UDP_IPV6_EX (1 << 10) /* IPv6 UDP 4-tuple + ext hdrs */ 69b2bdc62aSAdrian Chadd 70b2bdc62aSAdrian Chadd /* 71b2bdc62aSAdrian Chadd * Compile-time limits on the size of the indirection table. 72b2bdc62aSAdrian Chadd */ 73b2bdc62aSAdrian Chadd #define RSS_MAXBITS 7 74b2bdc62aSAdrian Chadd #define RSS_TABLE_MAXLEN (1 << RSS_MAXBITS) 75b2bdc62aSAdrian Chadd 76b2bdc62aSAdrian Chadd /* 77b2bdc62aSAdrian Chadd * Maximum key size used throughout. It's OK for hardware to use only the 78b2bdc62aSAdrian Chadd * first 16 bytes, which is all that's required for IPv4. 79b2bdc62aSAdrian Chadd */ 80b2bdc62aSAdrian Chadd #define RSS_KEYSIZE 40 81b2bdc62aSAdrian Chadd 82b2bdc62aSAdrian Chadd /* 83b2bdc62aSAdrian Chadd * For RSS hash methods that do a software hash on an mbuf, the packet 84b2bdc62aSAdrian Chadd * direction (ingress / egress) is required. 85b2bdc62aSAdrian Chadd * 86b2bdc62aSAdrian Chadd * The default direction (INGRESS) is the "receive into the NIC" - ie, 87b2bdc62aSAdrian Chadd * what the hardware is hashing on. 88b2bdc62aSAdrian Chadd */ 89b2bdc62aSAdrian Chadd #define RSS_HASH_PKT_INGRESS 0 90b2bdc62aSAdrian Chadd #define RSS_HASH_PKT_EGRESS 1 91b2bdc62aSAdrian Chadd 92b2bdc62aSAdrian Chadd /* 93*e5562eb9SAdrian Chadd * Rate limited debugging routines. 94*e5562eb9SAdrian Chadd */ 95*e5562eb9SAdrian Chadd #define RSS_DEBUG(format, ...) do { \ 96*e5562eb9SAdrian Chadd if (rss_debug) { \ 97*e5562eb9SAdrian Chadd static struct timeval lastfail; \ 98*e5562eb9SAdrian Chadd static int curfail; \ 99*e5562eb9SAdrian Chadd if (ppsratecheck(&lastfail, &curfail, 5)) \ 100*e5562eb9SAdrian Chadd printf("RSS (%s:%u): " format, __func__, __LINE__,\ 101*e5562eb9SAdrian Chadd ##__VA_ARGS__); \ 102*e5562eb9SAdrian Chadd } \ 103*e5562eb9SAdrian Chadd } while (0) 104*e5562eb9SAdrian Chadd 105*e5562eb9SAdrian Chadd extern int rss_debug; 106*e5562eb9SAdrian Chadd 107*e5562eb9SAdrian Chadd /* 108b2bdc62aSAdrian Chadd * Device driver interfaces to query RSS properties that must be programmed 109b2bdc62aSAdrian Chadd * into hardware. 110b2bdc62aSAdrian Chadd */ 111b2bdc62aSAdrian Chadd u_int rss_getbits(void); 112b2bdc62aSAdrian Chadd u_int rss_getbucket(u_int hash); 113b2bdc62aSAdrian Chadd u_int rss_get_indirection_to_bucket(u_int index); 114b2bdc62aSAdrian Chadd u_int rss_getcpu(u_int bucket); 115b2bdc62aSAdrian Chadd void rss_getkey(uint8_t *key); 116b2bdc62aSAdrian Chadd u_int rss_gethashalgo(void); 117b2bdc62aSAdrian Chadd u_int rss_getnumbuckets(void); 118b2bdc62aSAdrian Chadd u_int rss_getnumcpus(void); 119b2bdc62aSAdrian Chadd u_int rss_gethashconfig(void); 120b2bdc62aSAdrian Chadd 121b2bdc62aSAdrian Chadd /* 122b2bdc62aSAdrian Chadd * Hash calculation functions. 123b2bdc62aSAdrian Chadd */ 124b2bdc62aSAdrian Chadd uint32_t rss_hash(u_int datalen, const uint8_t *data); 125b2bdc62aSAdrian Chadd 126b2bdc62aSAdrian Chadd /* 127b2bdc62aSAdrian Chadd * Network stack interface to query desired CPU affinity of a packet. 128b2bdc62aSAdrian Chadd */ 129b2bdc62aSAdrian Chadd struct mbuf * rss_m2cpuid(struct mbuf *m, uintptr_t source, u_int *cpuid); 130b2bdc62aSAdrian Chadd u_int rss_hash2cpuid(uint32_t hash_val, uint32_t hash_type); 131b2bdc62aSAdrian Chadd int rss_hash2bucket(uint32_t hash_val, uint32_t hash_type, 132b2bdc62aSAdrian Chadd uint32_t *bucket_id); 133b2bdc62aSAdrian Chadd int rss_m2bucket(struct mbuf *m, uint32_t *bucket_id); 134b2bdc62aSAdrian Chadd 135b2bdc62aSAdrian Chadd #endif /* !_NET_RSS_CONFIG_H_ */ 136