1263198Srwatson/*-
2263198Srwatson * Copyright (c) 2010-2011 Juniper Networks, Inc.
3263198Srwatson * All rights reserved.
4263198Srwatson *
5263198Srwatson * This software was developed by Robert N. M. Watson under contract
6263198Srwatson * to Juniper Networks, Inc.
7263198Srwatson *
8263198Srwatson * Redistribution and use in source and binary forms, with or without
9263198Srwatson * modification, are permitted provided that the following conditions
10263198Srwatson * are met:
11263198Srwatson * 1. Redistributions of source code must retain the above copyright
12263198Srwatson *    notice, this list of conditions and the following disclaimer.
13263198Srwatson * 2. Redistributions in binary form must reproduce the above copyright
14263198Srwatson *    notice, this list of conditions and the following disclaimer in the
15263198Srwatson *    documentation and/or other materials provided with the distribution.
16263198Srwatson *
17263198Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18263198Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19263198Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20263198Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21263198Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22263198Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23263198Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24263198Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25263198Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26263198Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27263198Srwatson * SUCH DAMAGE.
28263198Srwatson *
29263198Srwatson * $FreeBSD$
30263198Srwatson */
31263198Srwatson
32277331Sadrian#ifndef _NET_RSS_CONFIG_H_
33277331Sadrian#define	_NET_RSS_CONFIG_H_
34263198Srwatson
35263198Srwatson#include <netinet/in.h>		/* in_addr_t */
36263198Srwatson
37263198Srwatson/*
38263198Srwatson * Supported RSS hash functions.
39263198Srwatson */
40263198Srwatson#define	RSS_HASH_NAIVE		0x00000001	/* Poor but fast hash. */
41263198Srwatson#define	RSS_HASH_TOEPLITZ	0x00000002	/* Required by RSS. */
42263198Srwatson#define	RSS_HASH_CRC32		0x00000004	/* Future; some NICs do it. */
43263198Srwatson
44263198Srwatson#define	RSS_HASH_MASK		(RSS_HASH_NAIVE | RSS_HASH_TOEPLITZ)
45263198Srwatson
46263198Srwatson/*
47263198Srwatson * Instances of struct inpcbinfo declare an RSS hash type indicating what
48263198Srwatson * header fields are covered.
49263198Srwatson */
50263198Srwatson#define	RSS_HASHFIELDS_NONE		0
51263198Srwatson#define	RSS_HASHFIELDS_4TUPLE		1
52263198Srwatson#define	RSS_HASHFIELDS_2TUPLE		2
53263198Srwatson
54263198Srwatson/*
55268911Sadrian * Define RSS representations of the M_HASHTYPE_* values, representing
56268911Sadrian * which particular bits are supported.  The NICs can then use this to
57268911Sadrian * calculate which hash types to enable and which not to enable.
58268911Sadrian *
59268911Sadrian * The fact that these line up with M_HASHTYPE_* is not to be relied
60268911Sadrian * upon.
61268911Sadrian */
62268911Sadrian#define	RSS_HASHTYPE_RSS_IPV4		(1 << 1)	/* IPv4 2-tuple */
63268911Sadrian#define	RSS_HASHTYPE_RSS_TCP_IPV4	(1 << 2)	/* TCPv4 4-tuple */
64268911Sadrian#define	RSS_HASHTYPE_RSS_IPV6		(1 << 3)	/* IPv6 2-tuple */
65268911Sadrian#define	RSS_HASHTYPE_RSS_TCP_IPV6	(1 << 4)	/* TCPv6 4-tuple */
66268911Sadrian#define	RSS_HASHTYPE_RSS_IPV6_EX	(1 << 5)	/* IPv6 2-tuple + ext hdrs */
67268911Sadrian#define	RSS_HASHTYPE_RSS_TCP_IPV6_EX	(1 << 6)	/* TCPv6 4-tiple + ext hdrs */
68268911Sadrian#define	RSS_HASHTYPE_RSS_UDP_IPV4	(1 << 7)	/* IPv4 UDP 4-tuple */
69268911Sadrian#define	RSS_HASHTYPE_RSS_UDP_IPV4_EX	(1 << 8)	/* IPv4 UDP 4-tuple + ext hdrs */
70268911Sadrian#define	RSS_HASHTYPE_RSS_UDP_IPV6	(1 << 9)	/* IPv6 UDP 4-tuple */
71268911Sadrian#define	RSS_HASHTYPE_RSS_UDP_IPV6_EX	(1 << 10)	/* IPv6 UDP 4-tuple + ext hdrs */
72268911Sadrian
73268911Sadrian/*
74263198Srwatson * Compile-time limits on the size of the indirection table.
75263198Srwatson */
76263198Srwatson#define	RSS_MAXBITS	7
77263198Srwatson#define	RSS_TABLE_MAXLEN	(1 << RSS_MAXBITS)
78263198Srwatson
79263198Srwatson/*
80263198Srwatson * Maximum key size used throughout.  It's OK for hardware to use only the
81263198Srwatson * first 16 bytes, which is all that's required for IPv4.
82263198Srwatson */
83263198Srwatson#define	RSS_KEYSIZE	40
84263198Srwatson
85263198Srwatson/*
86271297Sadrian * For RSS hash methods that do a software hash on an mbuf, the packet
87271297Sadrian * direction (ingress / egress) is required.
88271297Sadrian *
89271297Sadrian * The default direction (INGRESS) is the "receive into the NIC" - ie,
90271297Sadrian * what the hardware is hashing on.
91271297Sadrian */
92271297Sadrian#define	RSS_HASH_PKT_INGRESS	0
93271297Sadrian#define	RSS_HASH_PKT_EGRESS	1
94271297Sadrian
95271297Sadrian/*
96287245Sadrian * Rate limited debugging routines.
97287245Sadrian */
98287245Sadrian#define	RSS_DEBUG(format, ...)	do {					\
99287245Sadrian	if (rss_debug) {						\
100287245Sadrian		static struct timeval lastfail;				\
101287245Sadrian		static int curfail;					\
102287245Sadrian		if (ppsratecheck(&lastfail, &curfail, 5))		\
103287245Sadrian			printf("RSS (%s:%u): " format, __func__, __LINE__,\
104287245Sadrian			    ##__VA_ARGS__);				\
105287245Sadrian	}								\
106287245Sadrian} while (0)
107287245Sadrian
108287245Sadrianextern int	rss_debug;
109287245Sadrian
110287245Sadrian/*
111263198Srwatson * Device driver interfaces to query RSS properties that must be programmed
112263198Srwatson * into hardware.
113263198Srwatson */
114263198Srwatsonu_int	rss_getbits(void);
115263198Srwatsonu_int	rss_getbucket(u_int hash);
116267891Sadrianu_int	rss_get_indirection_to_bucket(u_int index);
117263198Srwatsonu_int	rss_getcpu(u_int bucket);
118263198Srwatsonvoid	rss_getkey(uint8_t *key);
119263198Srwatsonu_int	rss_gethashalgo(void);
120263198Srwatsonu_int	rss_getnumbuckets(void);
121263198Srwatsonu_int	rss_getnumcpus(void);
122268911Sadrianu_int	rss_gethashconfig(void);
123263198Srwatson
124263198Srwatson/*
125277331Sadrian * Hash calculation functions.
126263198Srwatson */
127277331Sadrianuint32_t	rss_hash(u_int datalen, const uint8_t *data);
128263198Srwatson
129263198Srwatson/*
130263198Srwatson * Network stack interface to query desired CPU affinity of a packet.
131263198Srwatson */
132277331Sadrianstruct mbuf * rss_m2cpuid(struct mbuf *m, uintptr_t source, u_int *cpuid);
133277331Sadrianu_int	rss_hash2cpuid(uint32_t hash_val, uint32_t hash_type);
134277331Sadrianint	rss_hash2bucket(uint32_t hash_val, uint32_t hash_type,
135277331Sadrian	    uint32_t *bucket_id);
136277331Sadrianint	rss_m2bucket(struct mbuf *m, uint32_t *bucket_id);
137263198Srwatson
138277331Sadrian#endif /* !_NET_RSS_CONFIG_H_ */
139