tcp: Switch hash table to linear probing instead of chaining

Currently we deal with hash collisions by letting a hash bucket contain
multiple entries, forming a linked list using an index in the connection
structure.

That's a pretty standard and simple approach, but in our case we can use
an even simpler one: linear probing.  Here if a hash bucket is occupied
we just move onto the next one until we find a feww one.  This slightly
simplifies lookup and more importantly saves some precious bytes in the
connection structure by removing the need for a link.  It does require some
additional complexity for hash removal.

This approach can perform poorly with hash table load is high.  However, we
already size our hash table of pointers larger than the connection table,
which puts an upper bound on the load.  It's relatively cheap to decrease
that bound if we find we need to.

I adapted the linear probing operations from Knuth's The Art of Computer
Programming, Volume 3, 2nd Edition.  Specifically Algorithm L and Algorithm
R in Section 6.4.  Note that there is an error in Algorithm R as printed,
see errata at [0].

[0] https://www-cs-faculty.stanford.edu/~knuth/all3-prepre.ps.gz

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
David Gibson 2023-12-07 16:53:51 +11:00 committed by Stefano Brivio
parent 89fcb563fc
commit 5913f26415
3 changed files with 82 additions and 57 deletions

109
tcp.c
View file

@ -573,22 +573,12 @@ static unsigned int tcp6_l2_flags_buf_used;
#define CONN(idx) (&(FLOW(idx)->tcp)) #define CONN(idx) (&(FLOW(idx)->tcp))
/** conn_at_idx() - Find a connection by index, if present
* @idx: Index of connection to lookup
*
* Return: pointer to connection, or NULL if @idx is out of bounds
*/
static inline struct tcp_tap_conn *conn_at_idx(unsigned idx)
{
if (idx >= FLOW_MAX)
return NULL;
ASSERT(CONN(idx)->f.type == FLOW_TCP);
return CONN(idx);
}
/* Table for lookup from remote address, local port, remote port */ /* Table for lookup from remote address, local port, remote port */
static struct tcp_tap_conn *tc_hash[TCP_HASH_TABLE_SIZE]; static struct tcp_tap_conn *tc_hash[TCP_HASH_TABLE_SIZE];
static_assert(ARRAY_SIZE(tc_hash) >= FLOW_MAX,
"Safe linear probing requires hash table larger than connection table");
/* Pools for pre-opened sockets (in init) */ /* Pools for pre-opened sockets (in init) */
int init_sock_pool4 [TCP_SOCK_POOL_SIZE]; int init_sock_pool4 [TCP_SOCK_POOL_SIZE];
int init_sock_pool6 [TCP_SOCK_POOL_SIZE]; int init_sock_pool6 [TCP_SOCK_POOL_SIZE];
@ -1197,6 +1187,26 @@ static unsigned int tcp_conn_hash(const struct ctx *c,
return tcp_hash(c, &conn->faddr, conn->eport, conn->fport); return tcp_hash(c, &conn->faddr, conn->eport, conn->fport);
} }
/**
* tcp_hash_probe() - Find hash bucket for a connection
* @c: Execution context
* @conn: Connection to find bucket for
*
* Return: If @conn is in the table, its current bucket, otherwise a suitable
* free bucket for it.
*/
static inline unsigned tcp_hash_probe(const struct ctx *c,
const struct tcp_tap_conn *conn)
{
unsigned b = tcp_conn_hash(c, conn);
/* Linear probing */
while (tc_hash[b] && tc_hash[b] != conn)
b = mod_sub(b, 1, TCP_HASH_TABLE_SIZE);
return b;
}
/** /**
* tcp_hash_insert() - Insert connection into hash table, chain link * tcp_hash_insert() - Insert connection into hash table, chain link
* @c: Execution context * @c: Execution context
@ -1204,14 +1214,10 @@ static unsigned int tcp_conn_hash(const struct ctx *c,
*/ */
static void tcp_hash_insert(const struct ctx *c, struct tcp_tap_conn *conn) static void tcp_hash_insert(const struct ctx *c, struct tcp_tap_conn *conn)
{ {
int b; unsigned b = tcp_hash_probe(c, conn);
b = tcp_hash(c, &conn->faddr, conn->eport, conn->fport);
conn->next_index = tc_hash[b] ? FLOW_IDX(tc_hash[b]) : -1U;
tc_hash[b] = conn; tc_hash[b] = conn;
flow_dbg(conn, "hash table insert: sock %i, bucket: %u", conn->sock, b);
flow_dbg(conn, "hash table insert: sock %i, bucket: %i, next: %p",
conn->sock, b, (void *)conn_at_idx(conn->next_index));
} }
/** /**
@ -1222,23 +1228,27 @@ static void tcp_hash_insert(const struct ctx *c, struct tcp_tap_conn *conn)
static void tcp_hash_remove(const struct ctx *c, static void tcp_hash_remove(const struct ctx *c,
const struct tcp_tap_conn *conn) const struct tcp_tap_conn *conn)
{ {
struct tcp_tap_conn *entry, *prev = NULL; unsigned b = tcp_hash_probe(c, conn), s;
int b = tcp_conn_hash(c, conn);
for (entry = tc_hash[b]; entry; if (!tc_hash[b])
prev = entry, entry = conn_at_idx(entry->next_index)) { return; /* Redundant remove */
if (entry == conn) {
if (prev) flow_dbg(conn, "hash table remove: sock %i, bucket: %u", conn->sock, b);
prev->next_index = conn->next_index;
else /* Scan the remainder of the cluster */
tc_hash[b] = conn_at_idx(conn->next_index); for (s = mod_sub(b, 1, TCP_HASH_TABLE_SIZE); tc_hash[s];
break; s = mod_sub(s, 1, TCP_HASH_TABLE_SIZE)) {
unsigned h = tcp_conn_hash(c, tc_hash[s]);
if (!mod_between(h, s, b, TCP_HASH_TABLE_SIZE)) {
/* tc_hash[s] can live in tc_hash[b]'s slot */
debug("hash table remove: shuffle %u -> %u", s, b);
tc_hash[b] = tc_hash[s];
b = s;
} }
} }
flow_dbg(conn, "hash table remove: sock %i, bucket: %i, new: %p", tc_hash[b] = NULL;
conn->sock, b,
(void *)(prev ? conn_at_idx(prev->next_index) : tc_hash[b]));
} }
/** /**
@ -1251,24 +1261,15 @@ void tcp_tap_conn_update(const struct ctx *c, struct tcp_tap_conn *old,
struct tcp_tap_conn *new) struct tcp_tap_conn *new)
{ {
struct tcp_tap_conn *entry, *prev = NULL; unsigned b = tcp_hash_probe(c, old);
int b = tcp_conn_hash(c, old);
for (entry = tc_hash[b]; entry; if (!tc_hash[b])
prev = entry, entry = conn_at_idx(entry->next_index)) { return; /* Not in hash table, nothing to update */
if (entry == old) {
if (prev) tc_hash[b] = new;
prev->next_index = FLOW_IDX(new);
else
tc_hash[b] = new;
break;
}
}
debug("TCP: hash table update: old index %u, new index %u, sock %i, " debug("TCP: hash table update: old index %u, new index %u, sock %i, "
"bucket: %i, old: %p, new: %p", "bucket: %u", FLOW_IDX(old), FLOW_IDX(new), new->sock, b);
FLOW_IDX(old), FLOW_IDX(new), new->sock, b,
(void *)old, (void *)new);
tcp_epoll_ctl(c, new); tcp_epoll_ctl(c, new);
} }
@ -1288,17 +1289,15 @@ static struct tcp_tap_conn *tcp_hash_lookup(const struct ctx *c,
in_port_t eport, in_port_t fport) in_port_t eport, in_port_t fport)
{ {
union inany_addr aany; union inany_addr aany;
struct tcp_tap_conn *conn; unsigned b;
int b;
inany_from_af(&aany, af, faddr); inany_from_af(&aany, af, faddr);
b = tcp_hash(c, &aany, eport, fport);
for (conn = tc_hash[b]; conn; conn = conn_at_idx(conn->next_index)) {
if (tcp_hash_match(conn, &aany, eport, fport))
return conn;
}
return NULL; b = tcp_hash(c, &aany, eport, fport);
while (tc_hash[b] && !tcp_hash_match(tc_hash[b], &aany, eport, fport))
b = mod_sub(b, 1, TCP_HASH_TABLE_SIZE);
return tc_hash[b];
} }
/** /**

View file

@ -13,7 +13,6 @@
* struct tcp_tap_conn - Descriptor for a TCP connection (not spliced) * struct tcp_tap_conn - Descriptor for a TCP connection (not spliced)
* @f: Generic flow information * @f: Generic flow information
* @in_epoll: Is the connection in the epoll set? * @in_epoll: Is the connection in the epoll set?
* @next_index: Connection index of next item in hash chain, -1 for none
* @tap_mss: MSS advertised by tap/guest, rounded to 2 ^ TCP_MSS_BITS * @tap_mss: MSS advertised by tap/guest, rounded to 2 ^ TCP_MSS_BITS
* @sock: Socket descriptor number * @sock: Socket descriptor number
* @events: Connection events, implying connection states * @events: Connection events, implying connection states
@ -40,7 +39,6 @@ struct tcp_tap_conn {
struct flow_common f; struct flow_common f;
bool in_epoll :1; bool in_epoll :1;
unsigned next_index :FLOW_INDEX_BITS + 2;
#define TCP_RETRANS_BITS 3 #define TCP_RETRANS_BITS 3
unsigned int retrans :TCP_RETRANS_BITS; unsigned int retrans :TCP_RETRANS_BITS;

28
util.h
View file

@ -227,6 +227,34 @@ int __daemon(int pidfile_fd, int devnull_fd);
int fls(unsigned long x); int fls(unsigned long x);
int write_file(const char *path, const char *buf); int write_file(const char *path, const char *buf);
/**
* mod_sub() - Modular arithmetic subtraction
* @a: Minued, unsigned value < @m
* @b: Subtrahend, unsigned value < @m
* @m: Modulus, must be less than (UINT_MAX / 2)
*
* Returns (@a - @b) mod @m, correctly handling unsigned underflows.
*/
static inline unsigned mod_sub(unsigned a, unsigned b, unsigned m)
{
if (a < b)
a += m;
return a - b;
}
/**
* mod_between() - Determine if a value is in a cyclic range
* @x, @i, @j: Unsigned values < @m
* @m: Modulus
*
* Returns true iff @x is in the cyclic range of values from @i..@j (mod @m),
* inclusive of @i, exclusive of @j.
*/
static inline bool mod_between(unsigned x, unsigned i, unsigned j, unsigned m)
{
return mod_sub(x, i, m) < mod_sub(j, i, m);
}
/* /*
* Workarounds for https://github.com/llvm/llvm-project/issues/58992 * Workarounds for https://github.com/llvm/llvm-project/issues/58992
* *