xref: /dragonfly/sys/net/toeplitz2.h (revision 36f372d9)
1cc85a685SSepherosa Ziehau /*
2cc85a685SSepherosa Ziehau  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3cc85a685SSepherosa Ziehau  *
4cc85a685SSepherosa Ziehau  * This code is derived from software contributed to The DragonFly Project
5cc85a685SSepherosa Ziehau  * by Sepherosa Ziehau <sepherosa@gmail.com>
6cc85a685SSepherosa Ziehau  *
7cc85a685SSepherosa Ziehau  * Redistribution and use in source and binary forms, with or without
8cc85a685SSepherosa Ziehau  * modification, are permitted provided that the following conditions
9cc85a685SSepherosa Ziehau  * are met:
10cc85a685SSepherosa Ziehau  *
11cc85a685SSepherosa Ziehau  * 1. Redistributions of source code must retain the above copyright
12cc85a685SSepherosa Ziehau  *    notice, this list of conditions and the following disclaimer.
13cc85a685SSepherosa Ziehau  * 2. Redistributions in binary form must reproduce the above copyright
14cc85a685SSepherosa Ziehau  *    notice, this list of conditions and the following disclaimer in
15cc85a685SSepherosa Ziehau  *    the documentation and/or other materials provided with the
16cc85a685SSepherosa Ziehau  *    distribution.
17cc85a685SSepherosa Ziehau  * 3. Neither the name of The DragonFly Project nor the names of its
18cc85a685SSepherosa Ziehau  *    contributors may be used to endorse or promote products derived
19cc85a685SSepherosa Ziehau  *    from this software without specific, prior written permission.
20cc85a685SSepherosa Ziehau  *
21cc85a685SSepherosa Ziehau  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22cc85a685SSepherosa Ziehau  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23cc85a685SSepherosa Ziehau  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24cc85a685SSepherosa Ziehau  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25cc85a685SSepherosa Ziehau  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26cc85a685SSepherosa Ziehau  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27cc85a685SSepherosa Ziehau  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28cc85a685SSepherosa Ziehau  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29cc85a685SSepherosa Ziehau  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30cc85a685SSepherosa Ziehau  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31cc85a685SSepherosa Ziehau  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32cc85a685SSepherosa Ziehau  * SUCH DAMAGE.
33cc85a685SSepherosa Ziehau  */
34cc85a685SSepherosa Ziehau 
35cc85a685SSepherosa Ziehau #ifndef _NET_TOEPLITZ2_H_
36cc85a685SSepherosa Ziehau #define _NET_TOEPLITZ2_H_
37cc85a685SSepherosa Ziehau 
38cc85a685SSepherosa Ziehau #ifndef _KERNEL
39cc85a685SSepherosa Ziehau #error "kernel only header file"
40cc85a685SSepherosa Ziehau #endif
41cc85a685SSepherosa Ziehau 
42cc85a685SSepherosa Ziehau #define TOEPLITZ_KEYSEED_CNT	2
43cc85a685SSepherosa Ziehau 
44cc85a685SSepherosa Ziehau extern uint32_t	toeplitz_cache[TOEPLITZ_KEYSEED_CNT][256];
45cc85a685SSepherosa Ziehau 
46cc85a685SSepherosa Ziehau static __inline uint32_t
toeplitz_rawhash_addrport(in_addr_t _faddr,in_addr_t _laddr,in_port_t _fport,in_port_t _lport)47b73d4152SSepherosa Ziehau toeplitz_rawhash_addrport(in_addr_t _faddr, in_addr_t _laddr,
48cc85a685SSepherosa Ziehau 			  in_port_t _fport, in_port_t _lport)
49cc85a685SSepherosa Ziehau {
50cc85a685SSepherosa Ziehau 	uint32_t _res;
51cc85a685SSepherosa Ziehau 
52cc85a685SSepherosa Ziehau 	_res =  toeplitz_cache[0][_faddr & 0xff];
53cc85a685SSepherosa Ziehau 	_res ^= toeplitz_cache[0][(_faddr >> 16) & 0xff];
54cc85a685SSepherosa Ziehau 	_res ^= toeplitz_cache[0][_laddr & 0xff];
55cc85a685SSepherosa Ziehau 	_res ^= toeplitz_cache[0][(_laddr >> 16) & 0xff];
56cc85a685SSepherosa Ziehau 	_res ^= toeplitz_cache[0][_fport & 0xff];
57cc85a685SSepherosa Ziehau 	_res ^= toeplitz_cache[0][_lport & 0xff];
58cc85a685SSepherosa Ziehau 
59cc85a685SSepherosa Ziehau 	_res ^= toeplitz_cache[1][(_faddr >> 8) & 0xff];
60cc85a685SSepherosa Ziehau 	_res ^= toeplitz_cache[1][(_faddr >> 24) & 0xff];
61cc85a685SSepherosa Ziehau 	_res ^= toeplitz_cache[1][(_laddr >> 8) & 0xff];
62cc85a685SSepherosa Ziehau 	_res ^= toeplitz_cache[1][(_laddr >> 24) & 0xff];
63cc85a685SSepherosa Ziehau 	_res ^= toeplitz_cache[1][(_fport >> 8) & 0xff];
64cc85a685SSepherosa Ziehau 	_res ^= toeplitz_cache[1][(_lport >> 8) & 0xff];
65cc85a685SSepherosa Ziehau 
66cc85a685SSepherosa Ziehau 	return _res;
67cc85a685SSepherosa Ziehau }
68cc85a685SSepherosa Ziehau 
69cc85a685SSepherosa Ziehau static __inline uint32_t
toeplitz_rawhash_addr(in_addr_t _faddr,in_addr_t _laddr)70b73d4152SSepherosa Ziehau toeplitz_rawhash_addr(in_addr_t _faddr, in_addr_t _laddr)
71cc85a685SSepherosa Ziehau {
72cc85a685SSepherosa Ziehau 	uint32_t _res;
73cc85a685SSepherosa Ziehau 
74cc85a685SSepherosa Ziehau 	_res =  toeplitz_cache[0][_faddr & 0xff];
75cc85a685SSepherosa Ziehau 	_res ^= toeplitz_cache[0][(_faddr >> 16) & 0xff];
76cc85a685SSepherosa Ziehau 	_res ^= toeplitz_cache[0][_laddr & 0xff];
77cc85a685SSepherosa Ziehau 	_res ^= toeplitz_cache[0][(_laddr >> 16) & 0xff];
78cc85a685SSepherosa Ziehau 
79cc85a685SSepherosa Ziehau 	_res ^= toeplitz_cache[1][(_faddr >> 8) & 0xff];
80cc85a685SSepherosa Ziehau 	_res ^= toeplitz_cache[1][(_faddr >> 24) & 0xff];
81cc85a685SSepherosa Ziehau 	_res ^= toeplitz_cache[1][(_laddr >> 8) & 0xff];
82cc85a685SSepherosa Ziehau 	_res ^= toeplitz_cache[1][(_laddr >> 24) & 0xff];
83cc85a685SSepherosa Ziehau 
84cc85a685SSepherosa Ziehau 	return _res;
85cc85a685SSepherosa Ziehau }
86cc85a685SSepherosa Ziehau 
87b73d4152SSepherosa Ziehau static __inline int
toeplitz_hash(uint32_t _rawhash)88b73d4152SSepherosa Ziehau toeplitz_hash(uint32_t _rawhash)
89b73d4152SSepherosa Ziehau {
90a3878651SSepherosa Ziehau 	return (_rawhash & 0xffff);
91b73d4152SSepherosa Ziehau }
92b73d4152SSepherosa Ziehau 
93*36f372d9SMatthew Dillon static __inline uint32_t
toeplitz_piecemeal_addr(in_addr_t _faddr)94*36f372d9SMatthew Dillon toeplitz_piecemeal_addr(in_addr_t _faddr)
95*36f372d9SMatthew Dillon {
96*36f372d9SMatthew Dillon 	uint32_t _res;
97*36f372d9SMatthew Dillon 
98*36f372d9SMatthew Dillon 	_res =  toeplitz_cache[0][_faddr & 0xff];
99*36f372d9SMatthew Dillon 	_res ^= toeplitz_cache[0][(_faddr >> 16) & 0xff];
100*36f372d9SMatthew Dillon 	_res ^= toeplitz_cache[1][(_faddr >> 8) & 0xff];
101*36f372d9SMatthew Dillon 	_res ^= toeplitz_cache[1][(_faddr >> 24) & 0xff];
102*36f372d9SMatthew Dillon 	return _res;
103*36f372d9SMatthew Dillon }
104*36f372d9SMatthew Dillon 
105*36f372d9SMatthew Dillon static __inline uint32_t
toeplitz_piecemeal_port(in_port_t _fport)106*36f372d9SMatthew Dillon toeplitz_piecemeal_port(in_port_t _fport)
107*36f372d9SMatthew Dillon {
108*36f372d9SMatthew Dillon 	uint32_t _res;
109*36f372d9SMatthew Dillon 
110*36f372d9SMatthew Dillon 	_res  = toeplitz_cache[0][_fport & 0xff];
111*36f372d9SMatthew Dillon 	_res ^= toeplitz_cache[1][(_fport >> 8) & 0xff];
112*36f372d9SMatthew Dillon 
113*36f372d9SMatthew Dillon 	return _res;
114*36f372d9SMatthew Dillon }
115*36f372d9SMatthew Dillon 
116cc85a685SSepherosa Ziehau #endif	/* !_NET_TOEPLITZ2_H_ */
117