1263198Srwatson/*-
2263198Srwatson * Copyright (c) 2010 David Malone <dwmalone@FreeBSD.org>
3263198Srwatson * All rights reserved.
4263198Srwatson *
5263198Srwatson * Redistribution and use in source and binary forms, with or without
6263198Srwatson * modification, are permitted provided that the following conditions
7263198Srwatson * are met:
8263198Srwatson * 1. Redistributions of source code must retain the above copyright
9263198Srwatson *    notice, this list of conditions and the following disclaimer.
10263198Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11263198Srwatson *    notice, this list of conditions and the following disclaimer in the
12263198Srwatson *    documentation and/or other materials provided with the distribution.
13263198Srwatson *
14263198Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15263198Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16263198Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17263198Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18263198Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19263198Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20263198Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21263198Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22263198Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23263198Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24263198Srwatson * SUCH DAMAGE.
25263198Srwatson */
26263198Srwatson
27263198Srwatson#include <sys/cdefs.h>
28263198Srwatson__FBSDID("$FreeBSD$");
29263198Srwatson
30263198Srwatson#include <sys/types.h>
31263198Srwatson
32277331Sadrian#include <net/rss_config.h>
33277331Sadrian#include <net/toeplitz.h>
34263198Srwatson
35263198Srwatson#include <sys/systm.h>
36263198Srwatson
37263198Srwatsonuint32_t
38263198Srwatsontoeplitz_hash(u_int keylen, const uint8_t *key, u_int datalen,
39263198Srwatson    const uint8_t *data)
40263198Srwatson{
41263198Srwatson	uint32_t hash = 0, v;
42263198Srwatson	u_int i, b;
43263198Srwatson
44263198Srwatson	/* XXXRW: Perhaps an assertion about key length vs. data length? */
45263198Srwatson
46263198Srwatson	v = (key[0]<<24) + (key[1]<<16) + (key[2] <<8) + key[3];
47263198Srwatson	for (i = 0; i < datalen; i++) {
48263198Srwatson		for (b = 0; b < 8; b++) {
49263198Srwatson			if (data[i] & (1<<(7-b)))
50263198Srwatson				hash ^= v;
51263198Srwatson			v <<= 1;
52263198Srwatson			if ((i + 4) < RSS_KEYSIZE &&
53263198Srwatson			    (key[i+4] & (1<<(7-b))))
54263198Srwatson				v |= 1;
55263198Srwatson		}
56263198Srwatson	}
57263198Srwatson	return (hash);
58263198Srwatson}
59