diff options
author | David Gibson <david@gibson.dropbear.id.au> | 2022-11-17 16:58:46 +1100 |
---|---|---|
committer | Stefano Brivio <sbrivio@redhat.com> | 2022-11-25 01:34:51 +0100 |
commit | ee8f8e9564c5bd0ca25a9492e3102655d9ce0620 (patch) | |
tree | 3f92b16d5e4ea22bbc2698008f7bd8475e794732 /tcp.c | |
parent | 181ce83d9bc52487f99401afe39a73499bd128fe (diff) | |
download | passt-ee8f8e9564c5bd0ca25a9492e3102655d9ce0620.tar passt-ee8f8e9564c5bd0ca25a9492e3102655d9ce0620.tar.gz passt-ee8f8e9564c5bd0ca25a9492e3102655d9ce0620.tar.bz2 passt-ee8f8e9564c5bd0ca25a9492e3102655d9ce0620.tar.lz passt-ee8f8e9564c5bd0ca25a9492e3102655d9ce0620.tar.xz passt-ee8f8e9564c5bd0ca25a9492e3102655d9ce0620.tar.zst passt-ee8f8e9564c5bd0ca25a9492e3102655d9ce0620.zip |
tcp: Unify spliced and non-spliced connection tables
Currently spliced and non-spliced connections are stored in completely
separate tables, so there are completely independent limits on the number
of spliced and non-spliced connections. This is a bit counter-intuitive.
More importantly, the fact that the tables are separate prevents us from
unifying some other logic between the two cases. So, merge these two
tables into one, using the 'c.spliced' common field to distinguish between
them when necessary.
For now we keep a common limit of 128k connections, whether they're spliced
or non-spliced, which means we save memory overall. If necessary we could
increase this to a 256k or higher total, which would cost memory but give
some more flexibility.
For now, the code paths which need to step through all extant connections
are still separate for the two cases, just skipping over entries which
aren't for them. We'll improve that in later patches.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'tcp.c')
-rw-r--r-- | tcp.c | 46 |
1 files changed, 26 insertions, 20 deletions
@@ -98,11 +98,11 @@ * Connection tracking and storage * ------------------------------- * - * Connections are tracked by the @tc array of struct tcp_tap_conn, containing - * addresses, ports, TCP states and parameters. This is statically allocated and - * indexed by an arbitrary connection number. The array is compacted whenever a - * connection is closed, by remapping the highest connection index in use to the - * one freed up. + * Connections are tracked by struct tcp_tap_conn entries in the @tc + * array, containing addresses, ports, TCP states and parameters. This + * is statically allocated and indexed by an arbitrary connection + * number. The array is compacted whenever a connection is closed, by + * remapping the highest connection index in use to the one freed up. * * References used for the epoll interface report the connection index used for * the @tc array. @@ -588,10 +588,10 @@ static unsigned int tcp6_l2_flags_buf_used; static size_t tcp6_l2_flags_buf_bytes; /* TCP connections */ -static struct tcp_tap_conn tc[TCP_MAX_CONNS]; +union tcp_conn tc[TCP_MAX_CONNS]; -#define CONN(index) (tc + (index)) -#define CONN_IDX(conn) ((conn) - tc) +#define CONN(index) (&tc[(index)].tap) +#define CONN_IDX(conn) ((union tcp_conn *)(conn) - tc) /** conn_at_idx() - Find a connection by index, if present * @index: Index of connection to lookup @@ -1351,26 +1351,28 @@ static struct tcp_tap_conn *tcp_hash_lookup(const struct ctx *c, * @c: Execution context * @hole: Pointer to recently closed connection */ -static void tcp_table_compact(struct ctx *c, struct tcp_tap_conn *hole) +void tcp_table_compact(struct ctx *c, union tcp_conn *hole) { - struct tcp_tap_conn *from, *to; + union tcp_conn *from; if (CONN_IDX(hole) == --c->tcp.conn_count) { - debug("TCP: hash table compaction: maximum index was %li (%p)", + debug("TCP: table compaction: maximum index was %li (%p)", CONN_IDX(hole), hole); memset(hole, 0, sizeof(*hole)); return; } - from = CONN(c->tcp.conn_count); + from = tc + c->tcp.conn_count; memcpy(hole, from, sizeof(*hole)); - to = hole; - tcp_tap_conn_update(c, from, to); + if (from->c.spliced) + tcp_splice_conn_update(c, &hole->splice); + else + tcp_tap_conn_update(c, &from->tap, &hole->tap); - debug("TCP: hash table compaction: old index %li, new index %li, " - "sock %i, from: %p, to: %p", - CONN_IDX(from), CONN_IDX(to), from->sock, from, to); + debug("TCP: table compaction (spliced=%d): old index %li, new index %li, " + "from: %p, to: %p", + from->c.spliced, CONN_IDX(from), CONN_IDX(hole), from, hole); memset(from, 0, sizeof(*from)); } @@ -1387,7 +1389,7 @@ static void tcp_conn_destroy(struct ctx *c, struct tcp_tap_conn *conn) close(conn->timer); tcp_hash_remove(conn); - tcp_table_compact(c, conn); + tcp_table_compact(c, (union tcp_conn *)conn); } static void tcp_rst_do(struct ctx *c, struct tcp_tap_conn *conn); @@ -1535,7 +1537,9 @@ void tcp_defer_handler(struct ctx *c) if (c->tcp.conn_count < MIN(max_files, max_conns)) return; - for (conn = CONN(c->tcp.conn_count - 1); conn >= tc; conn--) { + for (conn = CONN(c->tcp.conn_count - 1); conn >= CONN(0); conn--) { + if (conn->c.spliced) + continue; if (conn->events == CLOSED) tcp_conn_destroy(c, conn); } @@ -3433,7 +3437,9 @@ void tcp_timer(struct ctx *c, const struct timespec *ts) } } - for (conn = CONN(c->tcp.conn_count - 1); conn >= tc; conn--) { + for (conn = CONN(c->tcp.conn_count - 1); conn >= CONN(0); conn--) { + if (conn->c.spliced) + continue; if (conn->events == CLOSED) tcp_conn_destroy(c, conn); } |