Page MenuHomeFreeBSD

D62531.1796683887.diff
No OneTemporary

Authored By
Unknown
Size
3 KB
Referenced Files
None
Subscribers
None

D62531.1796683887.diff

diff --git a/sys/net/rss_config.c b/sys/net/rss_config.c
--- a/sys/net/rss_config.c
+++ b/sys/net/rss_config.c
@@ -173,6 +173,9 @@
0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa,
};
+/* Per-byte Toeplitz hashes of rss_key, see toeplitz.h. */
+static struct toeplitz_cache rss_toeplitz;
+
#ifdef RSS
/*
* RSS hash->CPU table, which maps hashed packet headers to particular CPUs.
@@ -273,6 +276,9 @@
* XXXRW: Not yet. If nothing else, will require an rss_isbadkey()
* loop to check for "bad" RSS keys.
*/
+
+ /* Once the key is final. */
+ toeplitz_cache_init(&rss_toeplitz, sizeof(rss_key), rss_key);
}
SYSINIT(rss_init, SI_SUB_SOFTINTR, SI_ORDER_SECOND, rss_init, NULL);
@@ -297,6 +303,10 @@
switch (rss_hashalgo) {
case RSS_HASH_TOEPLITZ:
+ /* Nothing in the tree hashes more; rss_hash() is exported. */
+ if (datalen <= TOEPLITZ_CACHE_MAXLEN)
+ return (toeplitz_cache_hash(&rss_toeplitz, datalen,
+ data));
return (toeplitz_hash(sizeof(rss_key), rss_key, datalen,
data));
diff --git a/sys/net/toeplitz.h b/sys/net/toeplitz.h
--- a/sys/net/toeplitz.h
+++ b/sys/net/toeplitz.h
@@ -28,11 +28,26 @@
#define _NETINET_TOEPLITZ_H_
/*
- * Toeplitz (RSS) hash algorithm; possibly we should cache intermediate
- * results between runs, in which case we'll need explicit init/destroy and
- * state management.
+ * Toeplitz (RSS) hash algorithm.
*/
uint32_t toeplitz_hash(u_int keylen, const uint8_t *key,
u_int datalen, const uint8_t *data);
+/*
+ * The hash is linear over GF(2): the hash of an input is the XOR of the
+ * hashes of its bytes, each at its own offset. A cache holds those for one
+ * key, so hashing costs one table lookup per byte instead of a loop over
+ * every bit.
+ */
+#define TOEPLITZ_CACHE_MAXLEN 36 /* IPv6 addresses and ports */
+
+struct toeplitz_cache {
+ uint32_t tc_hash[TOEPLITZ_CACHE_MAXLEN][UINT8_MAX + 1];
+};
+
+void toeplitz_cache_init(struct toeplitz_cache *tc, u_int keylen,
+ const uint8_t *key);
+uint32_t toeplitz_cache_hash(const struct toeplitz_cache *tc,
+ u_int datalen, const uint8_t *data);
+
#endif /* !_NETINET_TOEPLITZ_H_ */
diff --git a/sys/net/toeplitz.c b/sys/net/toeplitz.c
--- a/sys/net/toeplitz.c
+++ b/sys/net/toeplitz.c
@@ -1,6 +1,7 @@
/*-
* Copyright (c) 2010 David Malone <dwmalone@FreeBSD.org>
* All rights reserved.
+ * Copyright (c) 2026 Rubicon Communications, LLC (Netgate)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -24,12 +25,11 @@
* SUCH DAMAGE.
*/
-#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/systm.h>
#include <net/toeplitz.h>
-#include <sys/systm.h>
-
uint32_t
toeplitz_hash(u_int keylen, const uint8_t *key, u_int datalen,
const uint8_t *data)
@@ -52,3 +52,45 @@
}
return (hash);
}
+
+void
+toeplitz_cache_init(struct toeplitz_cache *tc, u_int keylen,
+ const uint8_t *key)
+{
+ uint8_t data[TOEPLITZ_CACHE_MAXLEN];
+ u_int b, i, v;
+
+ memset(data, 0, sizeof(data));
+ for (i = 0; i < TOEPLITZ_CACHE_MAXLEN; i++) {
+ /* Hash each single bit of a byte at this offset. */
+ for (b = 0; b < NBBY; b++) {
+ data[i] = 1 << b;
+ tc->tc_hash[i][1 << b] = toeplitz_hash(keylen, key,
+ i + 1, data);
+ }
+ data[i] = 0;
+ /*
+ * The hash is linear, so that of any other value is the XOR
+ * of the hashes of its lowest set bit and of the rest.
+ */
+ tc->tc_hash[i][0] = 0;
+ for (v = 1; v < nitems(tc->tc_hash[i]); v++)
+ tc->tc_hash[i][v] = tc->tc_hash[i][v & -v] ^
+ tc->tc_hash[i][v & (v - 1)];
+ }
+}
+
+uint32_t
+toeplitz_cache_hash(const struct toeplitz_cache *tc, u_int datalen,
+ const uint8_t *data)
+{
+ uint32_t hash;
+ u_int i;
+
+ KASSERT(datalen <= TOEPLITZ_CACHE_MAXLEN,
+ ("%s: %u bytes", __func__, datalen));
+ hash = 0;
+ for (i = 0; i < datalen; i++)
+ hash ^= tc->tc_hash[i][data[i]];
+ return (hash);
+}

File Metadata

Mime Type
text/plain
Expires
Mon, Dec 7, 10:51 PM (23 h, 46 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42293639
Default Alt Text
D62531.1796683887.diff (3 KB)

Event Timeline