xref: /freebsd/sys/net/toeplitz.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1*b2bdc62aSAdrian Chadd /*-
2*b2bdc62aSAdrian Chadd  * Copyright (c) 2010 David Malone <dwmalone@FreeBSD.org>
3*b2bdc62aSAdrian Chadd  * All rights reserved.
4*b2bdc62aSAdrian Chadd  *
5*b2bdc62aSAdrian Chadd  * Redistribution and use in source and binary forms, with or without
6*b2bdc62aSAdrian Chadd  * modification, are permitted provided that the following conditions
7*b2bdc62aSAdrian Chadd  * are met:
8*b2bdc62aSAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
9*b2bdc62aSAdrian Chadd  *    notice, this list of conditions and the following disclaimer.
10*b2bdc62aSAdrian Chadd  * 2. Redistributions in binary form must reproduce the above copyright
11*b2bdc62aSAdrian Chadd  *    notice, this list of conditions and the following disclaimer in the
12*b2bdc62aSAdrian Chadd  *    documentation and/or other materials provided with the distribution.
13*b2bdc62aSAdrian Chadd  *
14*b2bdc62aSAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*b2bdc62aSAdrian Chadd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*b2bdc62aSAdrian Chadd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*b2bdc62aSAdrian Chadd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*b2bdc62aSAdrian Chadd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*b2bdc62aSAdrian Chadd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*b2bdc62aSAdrian Chadd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*b2bdc62aSAdrian Chadd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*b2bdc62aSAdrian Chadd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*b2bdc62aSAdrian Chadd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*b2bdc62aSAdrian Chadd  * SUCH DAMAGE.
25*b2bdc62aSAdrian Chadd  */
26*b2bdc62aSAdrian Chadd 
27*b2bdc62aSAdrian Chadd #include <sys/types.h>
28*b2bdc62aSAdrian Chadd 
29*b2bdc62aSAdrian Chadd #include <net/rss_config.h>
30*b2bdc62aSAdrian Chadd #include <net/toeplitz.h>
31*b2bdc62aSAdrian Chadd 
32*b2bdc62aSAdrian Chadd #include <sys/systm.h>
33*b2bdc62aSAdrian Chadd 
34*b2bdc62aSAdrian Chadd uint32_t
toeplitz_hash(u_int keylen,const uint8_t * key,u_int datalen,const uint8_t * data)35*b2bdc62aSAdrian Chadd toeplitz_hash(u_int keylen, const uint8_t *key, u_int datalen,
36*b2bdc62aSAdrian Chadd     const uint8_t *data)
37*b2bdc62aSAdrian Chadd {
38*b2bdc62aSAdrian Chadd 	uint32_t hash = 0, v;
39*b2bdc62aSAdrian Chadd 	u_int i, b;
40*b2bdc62aSAdrian Chadd 
41*b2bdc62aSAdrian Chadd 	/* XXXRW: Perhaps an assertion about key length vs. data length? */
42*b2bdc62aSAdrian Chadd 
43*b2bdc62aSAdrian Chadd 	v = (key[0]<<24) + (key[1]<<16) + (key[2] <<8) + key[3];
44*b2bdc62aSAdrian Chadd 	for (i = 0; i < datalen; i++) {
45*b2bdc62aSAdrian Chadd 		for (b = 0; b < 8; b++) {
46*b2bdc62aSAdrian Chadd 			if (data[i] & (1<<(7-b)))
47*b2bdc62aSAdrian Chadd 				hash ^= v;
48*b2bdc62aSAdrian Chadd 			v <<= 1;
49*b2bdc62aSAdrian Chadd 			if ((i + 4) < RSS_KEYSIZE &&
50*b2bdc62aSAdrian Chadd 			    (key[i+4] & (1<<(7-b))))
51*b2bdc62aSAdrian Chadd 				v |= 1;
52*b2bdc62aSAdrian Chadd 		}
53*b2bdc62aSAdrian Chadd 	}
54*b2bdc62aSAdrian Chadd 	return (hash);
55*b2bdc62aSAdrian Chadd }
56