[PATCH 00/32] Use dual stack sockets to listen for inbound TCP connections
When forwarding many ports, passt can consume a lot of kernel memory because of the many listening sockets it opens. There are not a lot of ways we can reduce that, but here's one. Currently we create separate listening sockets for each port for both IPv4 and IPv6. However in Linux (and probably other platforms), it's possible to listen for both IPv4 and IPv6 connections on an IPv6 socket. This series uses such dual stack sockets to halve the number of listening sockets needed for TCP. When forwarding all TCP and UDP ports, this reduces the kernel memory used from around 677 MiB to around 487 MiB (kernel 6.0.8 on an x86_64 Fedora 37 machine). This should also be possible for UDP, but that will require a mostly separate implementation. David Gibson (32): clang-tidy: Suppress warning about assignments in if statements style: Minor corrections to function comments tcp_splice: #include tcp_splice.h in tcp_splice.c tcp: Remove unused TCP_MAX_SOCKS constant tcp: Better helpers for converting between connection pointer and index tcp_splice: Helpers for converting from index to/from tcp_splice_conn tcp: Move connection state structures into a shared header tcp: Add connection union type tcp: Improved helpers to update connections after moving tcp: Unify spliced and non-spliced connection tables tcp: Unify tcp_defer_handler and tcp_splice_defer_handler() tcp: Partially unify tcp_timer() and tcp_splice_timer() tcp: Unify the IN_EPOLL flag tcp: Separate helpers to create ns listening sockets tcp: Unify part of spliced and non-spliced conn_from_sock path tcp: Use the same sockets to listen for spliced and non-spliced connections tcp: Remove splice from tcp_epoll_ref tcp: Don't store hash bucket in connection structures inany: Helper functions for handling addresses which could be IPv4 or IPv6 tcp: Hash IPv4 and IPv4-mapped-IPv6 addresses the same tcp: Take tcp_hash_insert() address from struct tcp_conn tcp: Simplify tcp_hash_match() to take an inany_addr tcp: Unify initial sequence number calculation for IPv4 and IPv6 tcp: Have tcp_seq_init() take its parameters from struct tcp_conn tcp: Fix small errors in tcp_seq_init() time handling tcp: Remove v6 flag from tcp_epoll_ref tcp: NAT IPv4-mapped IPv6 addresses like IPv4 addresses tcp_splice: Allow splicing of connections from IPv4-mapped loopback tcp: Consolidate tcp_sock_init[46] util: Allow sock_l4() to open dual stack sockets util: Always return -1 on error in sock_l4() tcp: Use dual stack sockets for port forwarding when possible Makefile | 11 +- conf.c | 12 +- inany.h | 94 +++++ siphash.c | 2 + tap.c | 6 +- tcp.c | 978 ++++++++++++++++++++++----------------------------- tcp.h | 11 +- tcp_conn.h | 192 ++++++++++ tcp_splice.c | 337 ++++++++---------- tcp_splice.h | 12 +- util.c | 19 +- 11 files changed, 887 insertions(+), 787 deletions(-) create mode 100644 inany.h create mode 100644 tcp_conn.h -- 2.38.1
clang-tools 15.0.0 appears to have added a new warning that will always
complain about assignments in if statements, which we use in a number of
places in passt/pasta. Encountered on Fedora 37 with
clang-tools-extra-15.0.0-3.fc37.x86_64.
Suppress the new warning so that we can compile and test.
Signed-off-by: David Gibson
On Wed, 16 Nov 2022 15:41:41 +1100
David Gibson
clang-tools 15.0.0 appears to have added a new warning that will always complain about assignments in if statements, which we use in a number of places in passt/pasta. Encountered on Fedora 37 with clang-tools-extra-15.0.0-3.fc37.x86_64.
Suppress the new warning so that we can compile and test.
Signed-off-by: David Gibson
--- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 6b22408..8bcbbc0 100644 --- a/Makefile +++ b/Makefile @@ -262,6 +262,7 @@ clang-tidy: $(SRCS) $(HEADERS) clang-tidy -checks=*,-modernize-*,\ -clang-analyzer-valist.Uninitialized,\ -cppcoreguidelines-init-variables,\ + -bugprone-assignment-in-if-condition,\
I'm trying to keep, in the comment just above, a list of clang-tidy warnings we disable and the reason. I think this could just be grouped with: # - cppcoreguidelines-init-variables # Dubious value, would kill readability -- Stefano
On Thu, Nov 17, 2022 at 12:10:48AM +0100, Stefano Brivio wrote:
On Wed, 16 Nov 2022 15:41:41 +1100 David Gibson
wrote: clang-tools 15.0.0 appears to have added a new warning that will always complain about assignments in if statements, which we use in a number of places in passt/pasta. Encountered on Fedora 37 with clang-tools-extra-15.0.0-3.fc37.x86_64.
Suppress the new warning so that we can compile and test.
Signed-off-by: David Gibson
--- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 6b22408..8bcbbc0 100644 --- a/Makefile +++ b/Makefile @@ -262,6 +262,7 @@ clang-tidy: $(SRCS) $(HEADERS) clang-tidy -checks=*,-modernize-*,\ -clang-analyzer-valist.Uninitialized,\ -cppcoreguidelines-init-variables,\ + -bugprone-assignment-in-if-condition,\
I'm trying to keep, in the comment just above, a list of clang-tidy warnings we disable and the reason. I think this could just be grouped with:
Good point, I've updated the comment.
# - cppcoreguidelines-init-variables # Dubious value, would kill readability
-- David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Some style issues and a typo.
Signed-off-by: David Gibson
On Wed, 16 Nov 2022 15:41:42 +1100
David Gibson
Some style issues and a typo.
Signed-off-by: David Gibson
--- conf.c | 6 +++--- tap.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/conf.c b/conf.c index 1adcf83..3ad247e 100644 --- a/conf.c +++ b/conf.c @@ -112,9 +112,9 @@ static int get_bound_ports_ns(void *arg) * @s: String to search * @c: Delimiter character * - * Returns: If another @c is found in @s, returns a pointer to the - * character *after* the delimiter, if no further @c is in - * @s, return NULL + * Return: If another @c is found in @s, returns a pointer to the + * character *after* the delimiter, if no further @c is in @s, + * return NULL */ static char *next_chunk(const char *s, char c) { diff --git a/tap.c b/tap.c index abeff25..707660c 100644 --- a/tap.c +++ b/tap.c @@ -90,7 +90,7 @@ int tap_send(const struct ctx *c, const void *data, size_t len) * tap_ip4_daddr() - Normal IPv4 destination address for inbound packets * @c: Execution context * - * Returns: IPv4 address, network order + * Return: IPv4 address, network order
Loosely based on kerneldoc style: single space after "Return: " is the style adopted everywhere else. Rationale: it doesn't need to be aligned with anything else.
*/ struct in_addr tap_ip4_daddr(const struct ctx *c) { @@ -98,11 +98,11 @@ struct in_addr tap_ip4_daddr(const struct ctx *c) }
/** - * tap_ip6_daddr() - Normal IPv4 destination address for inbound packets + * tap_ip6_daddr() - Normal IPv6 destination address for inbound packets * @c: Execution context * @src: Source address * - * Returns: pointer to IPv6 address + * Return: pointer to IPv6 address
Same here.
*/ const struct in6_addr *tap_ip6_daddr(const struct ctx *c, const struct in6_addr *src)
-- Stefano
On Thu, Nov 17, 2022 at 12:11:09AM +0100, Stefano Brivio wrote:
On Wed, 16 Nov 2022 15:41:42 +1100 David Gibson
wrote: Some style issues and a typo.
Signed-off-by: David Gibson
--- conf.c | 6 +++--- tap.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/conf.c b/conf.c index 1adcf83..3ad247e 100644 --- a/conf.c +++ b/conf.c @@ -112,9 +112,9 @@ static int get_bound_ports_ns(void *arg) * @s: String to search * @c: Delimiter character * - * Returns: If another @c is found in @s, returns a pointer to the - * character *after* the delimiter, if no further @c is in - * @s, return NULL + * Return: If another @c is found in @s, returns a pointer to the + * character *after* the delimiter, if no further @c is in @s, + * return NULL */ static char *next_chunk(const char *s, char c) { diff --git a/tap.c b/tap.c index abeff25..707660c 100644 --- a/tap.c +++ b/tap.c @@ -90,7 +90,7 @@ int tap_send(const struct ctx *c, const void *data, size_t len) * tap_ip4_daddr() - Normal IPv4 destination address for inbound packets * @c: Execution context * - * Returns: IPv4 address, network order + * Return: IPv4 address, network order
Loosely based on kerneldoc style: single space after "Return: " is the style adopted everywhere else. Rationale: it doesn't need to be aligned with anything else.
*/ struct in_addr tap_ip4_daddr(const struct ctx *c) { @@ -98,11 +98,11 @@ struct in_addr tap_ip4_daddr(const struct ctx *c) }
/** - * tap_ip6_daddr() - Normal IPv4 destination address for inbound packets + * tap_ip6_daddr() - Normal IPv6 destination address for inbound packets * @c: Execution context * @src: Source address * - * Returns: pointer to IPv6 address + * Return: pointer to IPv6 address
Same here.
Fixed, thanks.
*/ const struct in6_addr *tap_ip6_daddr(const struct ctx *c, const struct in6_addr *src)
-- David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
This obvious include was omitted, which means that declarations in the
header weren't checked against definitions in the .c file. This shows up
an old declaration for a function that is now static, and a duplicate
#define.
Signed-off-by: David Gibson
Presumably it meant something in the past, but it's no longer used.
Signed-off-by: David Gibson
The macro CONN_OR_NULL() is used to look up connections by index with
bounds checking. Replace it with an inline function, which means:
- Better type checking
- No danger of multiple evaluation of an @index with side effects
Also add a helper to perform the reverse translation: from connection
pointer to index. Introduce a macro for this which will make later
cleanups easier and safer.
Signed-off-by: David Gibson
On Wed, 16 Nov 2022 15:41:45 +1100
David Gibson
The macro CONN_OR_NULL() is used to look up connections by index with bounds checking. Replace it with an inline function, which means: - Better type checking - No danger of multiple evaluation of an @index with side effects
Also add a helper to perform the reverse translation: from connection pointer to index. Introduce a macro for this which will make later cleanups easier and safer.
Ah, yes, much better, agreed. Just two things here:
Signed-off-by: David Gibson
--- tcp.c | 83 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git a/tcp.c b/tcp.c index d043123..4e56a6c 100644 --- a/tcp.c +++ b/tcp.c @@ -518,14 +518,6 @@ struct tcp_conn { (conn->events & (SOCK_FIN_RCVD | TAP_FIN_RCVD))) #define CONN_HAS(conn, set) ((conn->events & (set)) == (set))
-#define CONN(index) (tc + (index)) - -/* We probably don't want to use gcc statement expressions (for portability), so - * use this only after well-defined sequence points (no pre-/post-increments). - */ -#define CONN_OR_NULL(index) \ - (((int)(index) >= 0 && (index) < TCP_MAX_CONNS) ? (tc + (index)) : NULL) - static const char *tcp_event_str[] __attribute((__unused__)) = { "SOCK_ACCEPTED", "TAP_SYN_RCVD", "ESTABLISHED", "TAP_SYN_ACK_SENT",
@@ -705,6 +697,21 @@ static size_t tcp6_l2_flags_buf_bytes; /* TCP connections */ static struct tcp_conn tc[TCP_MAX_CONNS];
+#define CONN(index) (tc + (index)) +#define CONN_IDX(conn) ((conn) - tc) + +/** conn_at_idx() - Find a connection by index, if present + * @index: Index of connection to lookup + * + * Return: Pointer to connection, or NULL if @index is out of bounds
Return: pointer [...]
+ */ +static inline struct tcp_conn *conn_at_idx(int index)
The CONN_OR_NULL name made it very explicit that the pointer obtained there could be NULL. On the other hand I find conn_at_idx() more descriptive. But maybe conn_or_null() would be "safer". I don't really have a preference. -- Stefano
On Thu, Nov 17, 2022 at 12:11:30AM +0100, Stefano Brivio wrote:
On Wed, 16 Nov 2022 15:41:45 +1100 David Gibson
wrote: The macro CONN_OR_NULL() is used to look up connections by index with bounds checking. Replace it with an inline function, which means: - Better type checking - No danger of multiple evaluation of an @index with side effects
Also add a helper to perform the reverse translation: from connection pointer to index. Introduce a macro for this which will make later cleanups easier and safer.
Ah, yes, much better, agreed. Just two things here:
Signed-off-by: David Gibson
--- tcp.c | 83 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git a/tcp.c b/tcp.c index d043123..4e56a6c 100644 --- a/tcp.c +++ b/tcp.c @@ -518,14 +518,6 @@ struct tcp_conn { (conn->events & (SOCK_FIN_RCVD | TAP_FIN_RCVD))) #define CONN_HAS(conn, set) ((conn->events & (set)) == (set))
-#define CONN(index) (tc + (index)) - -/* We probably don't want to use gcc statement expressions (for portability), so - * use this only after well-defined sequence points (no pre-/post-increments). - */ -#define CONN_OR_NULL(index) \ - (((int)(index) >= 0 && (index) < TCP_MAX_CONNS) ? (tc + (index)) : NULL) - static const char *tcp_event_str[] __attribute((__unused__)) = { "SOCK_ACCEPTED", "TAP_SYN_RCVD", "ESTABLISHED", "TAP_SYN_ACK_SENT",
@@ -705,6 +697,21 @@ static size_t tcp6_l2_flags_buf_bytes; /* TCP connections */ static struct tcp_conn tc[TCP_MAX_CONNS];
+#define CONN(index) (tc + (index)) +#define CONN_IDX(conn) ((conn) - tc) + +/** conn_at_idx() - Find a connection by index, if present + * @index: Index of connection to lookup + * + * Return: Pointer to connection, or NULL if @index is out of bounds
Return: pointer [...]
Fixed.
+ */ +static inline struct tcp_conn *conn_at_idx(int index)
The CONN_OR_NULL name made it very explicit that the pointer obtained there could be NULL. On the other hand I find conn_at_idx() more descriptive. But maybe conn_or_null() would be "safer". I don't really have a preference.
I see your point, but on balance I think I marginally prefer conn_at_idx(), so I've left it. -- David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Like we already have for non-spliced connections, create a CONN_IDX()
macro for looking up the index of spliced connection structures. Change
the name of the array of spliced connections to be different from that for
non-spliced connections (even though they're in different modules). This
will make subsequent changes a bit safer.
Signed-off-by: David Gibson
Currently spliced and non-spliced connections use completely independent
tracking structures. We want to unify these, so as a preliminary step move
the definitions for both variants into a new tcp_conn.h header, shared by
tcp.c and tcp_splice.c.
This requires renaming some #defines with the same name but different
meanings between the two cases. In the process we correct some places that
are slightly out of sync between the comments and the code for various
event bit names.
Signed-off-by: David Gibson
Currently, the tables for spliced and non-spliced connections are entirely
separate, with different types in different arrays. We want to unify them.
As a first step, create a union type which can represent either a spliced
or non-spliced connection. For them to be distinguishable, the individual
types need to have a common header added, with a bit indicating which type
this structure is.
This comes at the cost of increasing the size of tcp_tap_conn to over one
(64 byte) cacheline. This isn't ideal, but it makes things simpler for now
and we'll re-optimize this later.
Signed-off-by: David Gibson
When we compact the connection tables (both spliced and non-spliced) we
need to move entries from one slot to another. That requires some updates
in the entries themselves. Add helpers to make all the necessary updates
for the spliced and non-spliced cases. This will simplify later cleanups.
Signed-off-by: David Gibson
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
These two functions each step through non-spliced and spliced connections
respectively and clean up entries for closed connections. To avoid
scanning the connection table twice, we merge these into a single function
which scans the unified table and performs the appropriate sort of cleanup
action on each one.
Signed-off-by: David Gibson
These two functions scan all the non-splced and spliced connections
respectively and perform timed updates on them. Avoid scanning the now
unified table twice, by having tcp_timer scan it once calling the
relevant per-connection function for each one.
Signed-off-by: David Gibson
There is very little common between the tcp_tap_conn and tcp_splice_conn
structures. However, both do have an IN_EPOLL flag which has the same
meaning in each case, though it's stored in a different location.
Simplify things slightly by moving this bit into the common header of the
two structures.
Signed-off-by: David Gibson
tcp_sock_init*() can create either sockets listening on the host, or in the pasta network namespace (with @ns==1). There are, however, a number of differences in how these two cases work in practice though. "ns" sockets are only used in pasta mode, and they always lead to spliced connections only. The functions are also only ever called in "ns" mode with a NULL address and interface name, and it doesn't really make sense for them to be called any other way. Later changes will introduce further differences in behaviour between these two cases, so it makes more sense to use separate functions for creating the ns listening sockets than the regular external/host listening sockets. --- conf.c | 6 +-- tcp.c | 130 ++++++++++++++++++++++++++++++++++++++------------------- tcp.h | 4 +- 3 files changed, 92 insertions(+), 48 deletions(-) diff --git a/conf.c b/conf.c index 3ad247e..2b39d18 100644 --- a/conf.c +++ b/conf.c @@ -209,7 +209,7 @@ static int conf_ports(const struct ctx *c, char optname, const char *optarg, for (i = 0; i < PORT_EPHEMERAL_MIN; i++) { if (optname == 't') - tcp_sock_init(c, 0, AF_UNSPEC, NULL, NULL, i); + tcp_sock_init(c, AF_UNSPEC, NULL, NULL, i); else if (optname == 'u') udp_sock_init(c, 0, AF_UNSPEC, NULL, NULL, i); } @@ -287,7 +287,7 @@ static int conf_ports(const struct ctx *c, char optname, const char *optarg, bitmap_set(fwd->map, i); if (optname == 't') - tcp_sock_init(c, 0, af, addr, ifname, i); + tcp_sock_init(c, af, addr, ifname, i); else if (optname == 'u') udp_sock_init(c, 0, af, addr, ifname, i); } @@ -333,7 +333,7 @@ static int conf_ports(const struct ctx *c, char optname, const char *optarg, fwd->delta[i] = mapped_range.first - orig_range.first; if (optname == 't') - tcp_sock_init(c, 0, af, addr, ifname, i); + tcp_sock_init(c, af, addr, ifname, i); else if (optname == 'u') udp_sock_init(c, 0, af, addr, ifname, i); } diff --git a/tcp.c b/tcp.c index aac70cd..72d3b49 100644 --- a/tcp.c +++ b/tcp.c @@ -2987,15 +2987,15 @@ void tcp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events, /** * tcp_sock_init4() - Initialise listening sockets for a given IPv4 port * @c: Execution context - * @ns: In pasta mode, if set, bind with loopback address in namespace * @addr: Pointer to address for binding, NULL if not configured * @ifname: Name of interface to bind to, NULL if not configured * @port: Port, host order */ -static void tcp_sock_init4(const struct ctx *c, int ns, const struct in_addr *addr, +static void tcp_sock_init4(const struct ctx *c, const struct in_addr *addr, const char *ifname, in_port_t port) { - union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.outbound = ns }; + in_port_t idx = port + c->tcp.fwd_in.delta[port]; + union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.index = idx }; bool spliced = false, tap = true; int s; @@ -3006,14 +3006,9 @@ static void tcp_sock_init4(const struct ctx *c, int ns, const struct in_addr *ad if (!addr) addr = &c->ip4.addr; - tap = !ns && !IN4_IS_ADDR_LOOPBACK(addr); + tap = !IN4_IS_ADDR_LOOPBACK(addr); } - if (ns) - tref.tcp.index = (in_port_t)(port + c->tcp.fwd_out.delta[port]); - else - tref.tcp.index = (in_port_t)(port + c->tcp.fwd_in.delta[port]); - if (tap) { s = sock_l4(c, AF_INET, IPPROTO_TCP, addr, ifname, port, tref.u32); @@ -3039,29 +3034,25 @@ static void tcp_sock_init4(const struct ctx *c, int ns, const struct in_addr *ad else s = -1; - if (c->tcp.fwd_out.mode == FWD_AUTO) { - if (ns) - tcp_sock_ns[port][V4] = s; - else - tcp_sock_init_lo[port][V4] = s; - } + if (c->tcp.fwd_out.mode == FWD_AUTO) + tcp_sock_init_lo[port][V4] = s; } } /** * tcp_sock_init6() - Initialise listening sockets for a given IPv6 port * @c: Execution context - * @ns: In pasta mode, if set, bind with loopback address in namespace * @addr: Pointer to address for binding, NULL if not configured * @ifname: Name of interface to bind to, NULL if not configured * @port: Port, host order */ -static void tcp_sock_init6(const struct ctx *c, int ns, +static void tcp_sock_init6(const struct ctx *c, const struct in6_addr *addr, const char *ifname, in_port_t port) { - union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.outbound = ns, - .tcp.v6 = 1 }; + in_port_t idx = port + c->tcp.fwd_in.delta[port]; + union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.v6 = 1, + .tcp.index = idx }; bool spliced = false, tap = true; int s; @@ -3073,14 +3064,9 @@ static void tcp_sock_init6(const struct ctx *c, int ns, if (!addr) addr = &c->ip6.addr; - tap = !ns && !IN6_IS_ADDR_LOOPBACK(addr); + tap = !IN6_IS_ADDR_LOOPBACK(addr); } - if (ns) - tref.tcp.index = (in_port_t)(port + c->tcp.fwd_out.delta[port]); - else - tref.tcp.index = (in_port_t)(port + c->tcp.fwd_in.delta[port]); - if (tap) { s = sock_l4(c, AF_INET6, IPPROTO_TCP, addr, ifname, port, tref.u32); @@ -3105,40 +3091,99 @@ static void tcp_sock_init6(const struct ctx *c, int ns, else s = -1; - if (c->tcp.fwd_out.mode == FWD_AUTO) { - if (ns) - tcp_sock_ns[port][V6] = s; - else - tcp_sock_init_lo[port][V6] = s; - } + if (c->tcp.fwd_out.mode == FWD_AUTO) + tcp_sock_init_lo[port][V6] = s; } } /** * tcp_sock_init() - Initialise listening sockets for a given port * @c: Execution context - * @ns: In pasta mode, if set, bind with loopback address in namespace * @af: Address family to select a specific IP version, or AF_UNSPEC * @addr: Pointer to address for binding, NULL if not configured * @ifname: Name of interface to bind to, NULL if not configured * @port: Port, host order */ -void tcp_sock_init(const struct ctx *c, int ns, sa_family_t af, - const void *addr, const char *ifname, in_port_t port) +void tcp_sock_init(const struct ctx *c, sa_family_t af, const void *addr, + const char *ifname, in_port_t port) { if ((af == AF_INET || af == AF_UNSPEC) && c->ifi4) - tcp_sock_init4(c, ns, addr, ifname, port); + tcp_sock_init4(c, addr, ifname, port); if ((af == AF_INET6 || af == AF_UNSPEC) && c->ifi6) - tcp_sock_init6(c, ns, addr, ifname, port); + tcp_sock_init6(c, addr, ifname, port); +} + +/** + * tcp_ns_sock_init4() - Init socket to listen for outbound IPv4 connections + * @c: Execution context + * @port: Port, host order + */ +static void tcp_ns_sock_init4(const struct ctx *c, in_port_t port) +{ + in_port_t idx = port + c->tcp.fwd_out.delta[port]; + union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.outbound = 1, + .tcp.splice = 1, .tcp.index = idx }; + struct in_addr loopback = { htonl(INADDR_LOOPBACK) }; + int s; + + assert(c->mode == MODE_PASTA); + + s = sock_l4(c, AF_INET, IPPROTO_TCP, &loopback, NULL, port, tref.u32); + if (s >= 0) + tcp_sock_set_bufsize(c, s); + else + s = -1; + + if (c->tcp.fwd_out.mode == FWD_AUTO) + tcp_sock_ns[port][V4] = s; } /** - * tcp_sock_init_ns() - Bind sockets in namespace for outbound connections + * tcp_ns_sock_init6() - Init socket to listen for outbound IPv6 connections + * @c: Execution context + * @port: Port, host order + */ +static void tcp_ns_sock_init6(const struct ctx *c, in_port_t port) +{ + in_port_t idx = port + c->tcp.fwd_out.delta[port]; + union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.outbound = 1, + .tcp.splice = 1, .tcp.v6 = 1, + .tcp.index = idx}; + int s; + + assert(c->mode == MODE_PASTA); + + s = sock_l4(c, AF_INET6, IPPROTO_TCP, &in6addr_loopback, NULL, port, + tref.u32); + if (s >= 0) + tcp_sock_set_bufsize(c, s); + else + s = -1; + + if (c->tcp.fwd_out.mode == FWD_AUTO) + tcp_sock_ns[port][V6] = s; +} + +/** + * tcp_ns_sock_init() - Init socket to listen for spliced outbound connections + * @c: Execution context + * @port: Port, host order + */ +void tcp_ns_sock_init(const struct ctx *c, in_port_t port) +{ + if (c->ifi4) + tcp_ns_sock_init4(c, port); + if (c->ifi6) + tcp_ns_sock_init6(c, port); +} + +/** + * tcp_ns_socks_init() - Bind sockets in namespace for outbound connections * @arg: Execution context * * Return: 0 */ -static int tcp_sock_init_ns(void *arg) +static int tcp_ns_socks_init(void *arg) { struct ctx *c = (struct ctx *)arg; unsigned port; @@ -3149,7 +3194,7 @@ static int tcp_sock_init_ns(void *arg) if (!bitmap_isset(c->tcp.fwd_out.map, port)) continue; - tcp_sock_init(c, 1, AF_UNSPEC, NULL, NULL, port); + tcp_ns_sock_init(c, port); } return 0; @@ -3279,7 +3324,7 @@ int tcp_init(struct ctx *c) if (c->mode == MODE_PASTA) { tcp_splice_init(c); - NS_CALL(tcp_sock_init_ns, c); + NS_CALL(tcp_ns_socks_init, c); refill_arg.ns = 1; NS_CALL(tcp_sock_refill, &refill_arg); @@ -3364,8 +3409,7 @@ static int tcp_port_rebind(void *arg) if ((a->c->ifi4 && tcp_sock_ns[port][V4] == -1) || (a->c->ifi6 && tcp_sock_ns[port][V6] == -1)) - tcp_sock_init(a->c, 1, AF_UNSPEC, NULL, NULL, - port); + tcp_ns_sock_init(a->c, port); } } else { for (port = 0; port < NUM_PORTS; port++) { @@ -3398,7 +3442,7 @@ static int tcp_port_rebind(void *arg) if ((a->c->ifi4 && tcp_sock_init_ext[port][V4] == -1) || (a->c->ifi6 && tcp_sock_init_ext[port][V6] == -1)) - tcp_sock_init(a->c, 0, AF_UNSPEC, NULL, NULL, + tcp_sock_init(a->c, AF_UNSPEC, NULL, NULL, port); } } diff --git a/tcp.h b/tcp.h index 49738ef..f4ed298 100644 --- a/tcp.h +++ b/tcp.h @@ -19,8 +19,8 @@ void tcp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events, const struct timespec *now); int tcp_tap_handler(struct ctx *c, int af, const void *addr, const struct pool *p, const struct timespec *now); -void tcp_sock_init(const struct ctx *c, int ns, sa_family_t af, - const void *addr, const char *ifname, in_port_t port); +void tcp_sock_init(const struct ctx *c, sa_family_t af, const void *addr, + const char *ifname, in_port_t port); int tcp_init(struct ctx *c); void tcp_timer(struct ctx *c, const struct timespec *ts); void tcp_defer_handler(struct ctx *c); -- 2.38.1
On Wed, 16 Nov 2022 15:41:54 +1100
David Gibson
tcp_sock_init*() can create either sockets listening on the host, or in the pasta network namespace (with @ns==1). There are, however, a number of differences in how these two cases work in practice though. "ns" sockets are only used in pasta mode, and they always lead to spliced connections only. The functions are also only ever called in "ns" mode with a NULL address and interface name, and it doesn't really make sense for them to be called any other way.
Later changes will introduce further differences in behaviour between these two cases, so it makes more sense to use separate functions for creating the ns listening sockets than the regular external/host listening sockets. --- conf.c | 6 +-- tcp.c | 130 ++++++++++++++++++++++++++++++++++++++------------------- tcp.h | 4 +- 3 files changed, 92 insertions(+), 48 deletions(-)
diff --git a/conf.c b/conf.c index 3ad247e..2b39d18 100644 --- a/conf.c +++ b/conf.c @@ -209,7 +209,7 @@ static int conf_ports(const struct ctx *c, char optname, const char *optarg,
for (i = 0; i < PORT_EPHEMERAL_MIN; i++) { if (optname == 't') - tcp_sock_init(c, 0, AF_UNSPEC, NULL, NULL, i); + tcp_sock_init(c, AF_UNSPEC, NULL, NULL, i); else if (optname == 'u') udp_sock_init(c, 0, AF_UNSPEC, NULL, NULL, i); } @@ -287,7 +287,7 @@ static int conf_ports(const struct ctx *c, char optname, const char *optarg, bitmap_set(fwd->map, i);
if (optname == 't') - tcp_sock_init(c, 0, af, addr, ifname, i); + tcp_sock_init(c, af, addr, ifname, i); else if (optname == 'u') udp_sock_init(c, 0, af, addr, ifname, i); } @@ -333,7 +333,7 @@ static int conf_ports(const struct ctx *c, char optname, const char *optarg, fwd->delta[i] = mapped_range.first - orig_range.first;
if (optname == 't') - tcp_sock_init(c, 0, af, addr, ifname, i); + tcp_sock_init(c, af, addr, ifname, i); else if (optname == 'u') udp_sock_init(c, 0, af, addr, ifname, i); } diff --git a/tcp.c b/tcp.c index aac70cd..72d3b49 100644 --- a/tcp.c +++ b/tcp.c @@ -2987,15 +2987,15 @@ void tcp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events, /** * tcp_sock_init4() - Initialise listening sockets for a given IPv4 port * @c: Execution context - * @ns: In pasta mode, if set, bind with loopback address in namespace * @addr: Pointer to address for binding, NULL if not configured * @ifname: Name of interface to bind to, NULL if not configured * @port: Port, host order */ -static void tcp_sock_init4(const struct ctx *c, int ns, const struct in_addr *addr, +static void tcp_sock_init4(const struct ctx *c, const struct in_addr *addr, const char *ifname, in_port_t port) { - union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.outbound = ns }; + in_port_t idx = port + c->tcp.fwd_in.delta[port]; + union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.index = idx };
Usual order here...
bool spliced = false, tap = true; int s;
@@ -3006,14 +3006,9 @@ static void tcp_sock_init4(const struct ctx *c, int ns, const struct in_addr *ad if (!addr) addr = &c->ip4.addr;
- tap = !ns && !IN4_IS_ADDR_LOOPBACK(addr); + tap = !IN4_IS_ADDR_LOOPBACK(addr); }
- if (ns) - tref.tcp.index = (in_port_t)(port + c->tcp.fwd_out.delta[port]); - else - tref.tcp.index = (in_port_t)(port + c->tcp.fwd_in.delta[port]); - if (tap) { s = sock_l4(c, AF_INET, IPPROTO_TCP, addr, ifname, port, tref.u32); @@ -3039,29 +3034,25 @@ static void tcp_sock_init4(const struct ctx *c, int ns, const struct in_addr *ad else s = -1;
- if (c->tcp.fwd_out.mode == FWD_AUTO) { - if (ns) - tcp_sock_ns[port][V4] = s; - else - tcp_sock_init_lo[port][V4] = s; - } + if (c->tcp.fwd_out.mode == FWD_AUTO) + tcp_sock_init_lo[port][V4] = s; } }
/** * tcp_sock_init6() - Initialise listening sockets for a given IPv6 port * @c: Execution context - * @ns: In pasta mode, if set, bind with loopback address in namespace * @addr: Pointer to address for binding, NULL if not configured * @ifname: Name of interface to bind to, NULL if not configured * @port: Port, host order */ -static void tcp_sock_init6(const struct ctx *c, int ns, +static void tcp_sock_init6(const struct ctx *c, const struct in6_addr *addr, const char *ifname, in_port_t port) { - union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.outbound = ns, - .tcp.v6 = 1 }; + in_port_t idx = port + c->tcp.fwd_in.delta[port]; + union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.v6 = 1, + .tcp.index = idx };
Excess whitespace.
bool spliced = false, tap = true; int s;
@@ -3073,14 +3064,9 @@ static void tcp_sock_init6(const struct ctx *c, int ns, if (!addr) addr = &c->ip6.addr;
- tap = !ns && !IN6_IS_ADDR_LOOPBACK(addr); + tap = !IN6_IS_ADDR_LOOPBACK(addr); }
- if (ns) - tref.tcp.index = (in_port_t)(port + c->tcp.fwd_out.delta[port]); - else - tref.tcp.index = (in_port_t)(port + c->tcp.fwd_in.delta[port]); - if (tap) { s = sock_l4(c, AF_INET6, IPPROTO_TCP, addr, ifname, port, tref.u32); @@ -3105,40 +3091,99 @@ static void tcp_sock_init6(const struct ctx *c, int ns, else s = -1;
- if (c->tcp.fwd_out.mode == FWD_AUTO) { - if (ns) - tcp_sock_ns[port][V6] = s; - else - tcp_sock_init_lo[port][V6] = s; - } + if (c->tcp.fwd_out.mode == FWD_AUTO) + tcp_sock_init_lo[port][V6] = s; } }
/** * tcp_sock_init() - Initialise listening sockets for a given port
Maybe we should now indicate this is for "inbound" connections only ("for a given, inbound, port"?)
* @c: Execution context - * @ns: In pasta mode, if set, bind with loopback address in namespace * @af: Address family to select a specific IP version, or AF_UNSPEC * @addr: Pointer to address for binding, NULL if not configured * @ifname: Name of interface to bind to, NULL if not configured * @port: Port, host order */ -void tcp_sock_init(const struct ctx *c, int ns, sa_family_t af, - const void *addr, const char *ifname, in_port_t port) +void tcp_sock_init(const struct ctx *c, sa_family_t af, const void *addr, + const char *ifname, in_port_t port) { if ((af == AF_INET || af == AF_UNSPEC) && c->ifi4) - tcp_sock_init4(c, ns, addr, ifname, port); + tcp_sock_init4(c, addr, ifname, port); if ((af == AF_INET6 || af == AF_UNSPEC) && c->ifi6) - tcp_sock_init6(c, ns, addr, ifname, port); + tcp_sock_init6(c, addr, ifname, port); +} + +/** + * tcp_ns_sock_init4() - Init socket to listen for outbound IPv4 connections + * @c: Execution context + * @port: Port, host order + */ +static void tcp_ns_sock_init4(const struct ctx *c, in_port_t port) +{ + in_port_t idx = port + c->tcp.fwd_out.delta[port];
Move after declaration of 'loopback'.
+ union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.outbound = 1, + .tcp.splice = 1, .tcp.index = idx }; + struct in_addr loopback = { htonl(INADDR_LOOPBACK) }; + int s; + + assert(c->mode == MODE_PASTA); + + s = sock_l4(c, AF_INET, IPPROTO_TCP, &loopback, NULL, port, tref.u32); + if (s >= 0) + tcp_sock_set_bufsize(c, s); + else + s = -1; + + if (c->tcp.fwd_out.mode == FWD_AUTO) + tcp_sock_ns[port][V4] = s; }
/** - * tcp_sock_init_ns() - Bind sockets in namespace for outbound connections + * tcp_ns_sock_init6() - Init socket to listen for outbound IPv6 connections + * @c: Execution context + * @port: Port, host order + */ +static void tcp_ns_sock_init6(const struct ctx *c, in_port_t port) +{ + in_port_t idx = port + c->tcp.fwd_out.delta[port]; + union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.outbound = 1, + .tcp.splice = 1, .tcp.v6 = 1, + .tcp.index = idx};
Missing whitespace between 'idx' and };
+ int s; + + assert(c->mode == MODE_PASTA); + + s = sock_l4(c, AF_INET6, IPPROTO_TCP, &in6addr_loopback, NULL, port, + tref.u32); + if (s >= 0) + tcp_sock_set_bufsize(c, s); + else + s = -1; + + if (c->tcp.fwd_out.mode == FWD_AUTO) + tcp_sock_ns[port][V6] = s; +} + +/** + * tcp_ns_sock_init() - Init socket to listen for spliced outbound connections + * @c: Execution context + * @port: Port, host order + */ +void tcp_ns_sock_init(const struct ctx *c, in_port_t port) +{ + if (c->ifi4) + tcp_ns_sock_init4(c, port); + if (c->ifi6) + tcp_ns_sock_init6(c, port); +} + +/** + * tcp_ns_socks_init() - Bind sockets in namespace for outbound connections * @arg: Execution context * * Return: 0 */ -static int tcp_sock_init_ns(void *arg) +static int tcp_ns_socks_init(void *arg) { struct ctx *c = (struct ctx *)arg; unsigned port; @@ -3149,7 +3194,7 @@ static int tcp_sock_init_ns(void *arg) if (!bitmap_isset(c->tcp.fwd_out.map, port)) continue;
- tcp_sock_init(c, 1, AF_UNSPEC, NULL, NULL, port); + tcp_ns_sock_init(c, port); }
return 0; @@ -3279,7 +3324,7 @@ int tcp_init(struct ctx *c) if (c->mode == MODE_PASTA) { tcp_splice_init(c);
- NS_CALL(tcp_sock_init_ns, c); + NS_CALL(tcp_ns_socks_init, c);
refill_arg.ns = 1; NS_CALL(tcp_sock_refill, &refill_arg); @@ -3364,8 +3409,7 @@ static int tcp_port_rebind(void *arg)
if ((a->c->ifi4 && tcp_sock_ns[port][V4] == -1) || (a->c->ifi6 && tcp_sock_ns[port][V6] == -1)) - tcp_sock_init(a->c, 1, AF_UNSPEC, NULL, NULL, - port); + tcp_ns_sock_init(a->c, port); } } else { for (port = 0; port < NUM_PORTS; port++) { @@ -3398,7 +3442,7 @@ static int tcp_port_rebind(void *arg)
if ((a->c->ifi4 && tcp_sock_init_ext[port][V4] == -1) || (a->c->ifi6 && tcp_sock_init_ext[port][V6] == -1)) - tcp_sock_init(a->c, 0, AF_UNSPEC, NULL, NULL, + tcp_sock_init(a->c, AF_UNSPEC, NULL, NULL, port); } } diff --git a/tcp.h b/tcp.h index 49738ef..f4ed298 100644 --- a/tcp.h +++ b/tcp.h @@ -19,8 +19,8 @@ void tcp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events, const struct timespec *now); int tcp_tap_handler(struct ctx *c, int af, const void *addr, const struct pool *p, const struct timespec *now); -void tcp_sock_init(const struct ctx *c, int ns, sa_family_t af, - const void *addr, const char *ifname, in_port_t port); +void tcp_sock_init(const struct ctx *c, sa_family_t af, const void *addr, + const char *ifname, in_port_t port); int tcp_init(struct ctx *c); void tcp_timer(struct ctx *c, const struct timespec *ts); void tcp_defer_handler(struct ctx *c);
On Thu, Nov 17, 2022 at 12:51:38AM +0100, Stefano Brivio wrote:
On Wed, 16 Nov 2022 15:41:54 +1100 David Gibson
wrote: tcp_sock_init*() can create either sockets listening on the host, or in the pasta network namespace (with @ns==1). There are, however, a number of differences in how these two cases work in practice though. "ns" sockets are only used in pasta mode, and they always lead to spliced connections only. The functions are also only ever called in "ns" mode with a NULL address and interface name, and it doesn't really make sense for them to be called any other way.
Later changes will introduce further differences in behaviour between these two cases, so it makes more sense to use separate functions for creating the ns listening sockets than the regular external/host listening sockets. --- conf.c | 6 +-- tcp.c | 130 ++++++++++++++++++++++++++++++++++++++------------------- tcp.h | 4 +- 3 files changed, 92 insertions(+), 48 deletions(-)
diff --git a/conf.c b/conf.c index 3ad247e..2b39d18 100644 --- a/conf.c +++ b/conf.c @@ -209,7 +209,7 @@ static int conf_ports(const struct ctx *c, char optname, const char *optarg,
for (i = 0; i < PORT_EPHEMERAL_MIN; i++) { if (optname == 't') - tcp_sock_init(c, 0, AF_UNSPEC, NULL, NULL, i); + tcp_sock_init(c, AF_UNSPEC, NULL, NULL, i); else if (optname == 'u') udp_sock_init(c, 0, AF_UNSPEC, NULL, NULL, i); } @@ -287,7 +287,7 @@ static int conf_ports(const struct ctx *c, char optname, const char *optarg, bitmap_set(fwd->map, i);
if (optname == 't') - tcp_sock_init(c, 0, af, addr, ifname, i); + tcp_sock_init(c, af, addr, ifname, i); else if (optname == 'u') udp_sock_init(c, 0, af, addr, ifname, i); } @@ -333,7 +333,7 @@ static int conf_ports(const struct ctx *c, char optname, const char *optarg, fwd->delta[i] = mapped_range.first - orig_range.first;
if (optname == 't') - tcp_sock_init(c, 0, af, addr, ifname, i); + tcp_sock_init(c, af, addr, ifname, i); else if (optname == 'u') udp_sock_init(c, 0, af, addr, ifname, i); } diff --git a/tcp.c b/tcp.c index aac70cd..72d3b49 100644 --- a/tcp.c +++ b/tcp.c @@ -2987,15 +2987,15 @@ void tcp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events, /** * tcp_sock_init4() - Initialise listening sockets for a given IPv4 port * @c: Execution context - * @ns: In pasta mode, if set, bind with loopback address in namespace * @addr: Pointer to address for binding, NULL if not configured * @ifname: Name of interface to bind to, NULL if not configured * @port: Port, host order */ -static void tcp_sock_init4(const struct ctx *c, int ns, const struct in_addr *addr, +static void tcp_sock_init4(const struct ctx *c, const struct in_addr *addr, const char *ifname, in_port_t port) { - union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.outbound = ns }; + in_port_t idx = port + c->tcp.fwd_in.delta[port]; + union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.index = idx };
Usual order here...
You mean the reverse christmas tree thing? I can't do that here, because idx is used in the next declaration.
bool spliced = false, tap = true; int s;
@@ -3006,14 +3006,9 @@ static void tcp_sock_init4(const struct ctx *c, int ns, const struct in_addr *ad if (!addr) addr = &c->ip4.addr;
- tap = !ns && !IN4_IS_ADDR_LOOPBACK(addr); + tap = !IN4_IS_ADDR_LOOPBACK(addr); }
- if (ns) - tref.tcp.index = (in_port_t)(port + c->tcp.fwd_out.delta[port]); - else - tref.tcp.index = (in_port_t)(port + c->tcp.fwd_in.delta[port]); - if (tap) { s = sock_l4(c, AF_INET, IPPROTO_TCP, addr, ifname, port, tref.u32); @@ -3039,29 +3034,25 @@ static void tcp_sock_init4(const struct ctx *c, int ns, const struct in_addr *ad else s = -1;
- if (c->tcp.fwd_out.mode == FWD_AUTO) { - if (ns) - tcp_sock_ns[port][V4] = s; - else - tcp_sock_init_lo[port][V4] = s; - } + if (c->tcp.fwd_out.mode == FWD_AUTO) + tcp_sock_init_lo[port][V4] = s; } }
/** * tcp_sock_init6() - Initialise listening sockets for a given IPv6 port * @c: Execution context - * @ns: In pasta mode, if set, bind with loopback address in namespace * @addr: Pointer to address for binding, NULL if not configured * @ifname: Name of interface to bind to, NULL if not configured * @port: Port, host order */ -static void tcp_sock_init6(const struct ctx *c, int ns, +static void tcp_sock_init6(const struct ctx *c, const struct in6_addr *addr, const char *ifname, in_port_t port) { - union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.outbound = ns, - .tcp.v6 = 1 }; + in_port_t idx = port + c->tcp.fwd_in.delta[port]; + union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.v6 = 1, + .tcp.index = idx };
Excess whitespace.
Fixed.
bool spliced = false, tap = true; int s;
@@ -3073,14 +3064,9 @@ static void tcp_sock_init6(const struct ctx *c, int ns, if (!addr) addr = &c->ip6.addr;
- tap = !ns && !IN6_IS_ADDR_LOOPBACK(addr); + tap = !IN6_IS_ADDR_LOOPBACK(addr); }
- if (ns) - tref.tcp.index = (in_port_t)(port + c->tcp.fwd_out.delta[port]); - else - tref.tcp.index = (in_port_t)(port + c->tcp.fwd_in.delta[port]); - if (tap) { s = sock_l4(c, AF_INET6, IPPROTO_TCP, addr, ifname, port, tref.u32); @@ -3105,40 +3091,99 @@ static void tcp_sock_init6(const struct ctx *c, int ns, else s = -1;
- if (c->tcp.fwd_out.mode == FWD_AUTO) { - if (ns) - tcp_sock_ns[port][V6] = s; - else - tcp_sock_init_lo[port][V6] = s; - } + if (c->tcp.fwd_out.mode == FWD_AUTO) + tcp_sock_init_lo[port][V6] = s; } }
/** * tcp_sock_init() - Initialise listening sockets for a given port
Maybe we should now indicate this is for "inbound" connections only ("for a given, inbound, port"?)
Updated.
* @c: Execution context - * @ns: In pasta mode, if set, bind with loopback address in namespace * @af: Address family to select a specific IP version, or AF_UNSPEC * @addr: Pointer to address for binding, NULL if not configured * @ifname: Name of interface to bind to, NULL if not configured * @port: Port, host order */ -void tcp_sock_init(const struct ctx *c, int ns, sa_family_t af, - const void *addr, const char *ifname, in_port_t port) +void tcp_sock_init(const struct ctx *c, sa_family_t af, const void *addr, + const char *ifname, in_port_t port) { if ((af == AF_INET || af == AF_UNSPEC) && c->ifi4) - tcp_sock_init4(c, ns, addr, ifname, port); + tcp_sock_init4(c, addr, ifname, port); if ((af == AF_INET6 || af == AF_UNSPEC) && c->ifi6) - tcp_sock_init6(c, ns, addr, ifname, port); + tcp_sock_init6(c, addr, ifname, port); +} + +/** + * tcp_ns_sock_init4() - Init socket to listen for outbound IPv4 connections + * @c: Execution context + * @port: Port, host order + */ +static void tcp_ns_sock_init4(const struct ctx *c, in_port_t port) +{ + in_port_t idx = port + c->tcp.fwd_out.delta[port];
Move after declaration of 'loopback'.
Again, I can't do that because idx is used in the next declaration.
+ union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.outbound = 1, + .tcp.splice = 1, .tcp.index = idx }; + struct in_addr loopback = { htonl(INADDR_LOOPBACK) }; + int s; + + assert(c->mode == MODE_PASTA); + + s = sock_l4(c, AF_INET, IPPROTO_TCP, &loopback, NULL, port, tref.u32); + if (s >= 0) + tcp_sock_set_bufsize(c, s); + else + s = -1; + + if (c->tcp.fwd_out.mode == FWD_AUTO) + tcp_sock_ns[port][V4] = s; }
/** - * tcp_sock_init_ns() - Bind sockets in namespace for outbound connections + * tcp_ns_sock_init6() - Init socket to listen for outbound IPv6 connections + * @c: Execution context + * @port: Port, host order + */ +static void tcp_ns_sock_init6(const struct ctx *c, in_port_t port) +{ + in_port_t idx = port + c->tcp.fwd_out.delta[port]; + union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.outbound = 1, + .tcp.splice = 1, .tcp.v6 = 1, + .tcp.index = idx};
Missing whitespace between 'idx' and };
Fixed.
+ int s; + + assert(c->mode == MODE_PASTA); + + s = sock_l4(c, AF_INET6, IPPROTO_TCP, &in6addr_loopback, NULL, port, + tref.u32); + if (s >= 0) + tcp_sock_set_bufsize(c, s); + else + s = -1; + + if (c->tcp.fwd_out.mode == FWD_AUTO) + tcp_sock_ns[port][V6] = s; +} + +/** + * tcp_ns_sock_init() - Init socket to listen for spliced outbound connections + * @c: Execution context + * @port: Port, host order + */ +void tcp_ns_sock_init(const struct ctx *c, in_port_t port) +{ + if (c->ifi4) + tcp_ns_sock_init4(c, port); + if (c->ifi6) + tcp_ns_sock_init6(c, port); +} + +/** + * tcp_ns_socks_init() - Bind sockets in namespace for outbound connections * @arg: Execution context * * Return: 0 */ -static int tcp_sock_init_ns(void *arg) +static int tcp_ns_socks_init(void *arg) { struct ctx *c = (struct ctx *)arg; unsigned port; @@ -3149,7 +3194,7 @@ static int tcp_sock_init_ns(void *arg) if (!bitmap_isset(c->tcp.fwd_out.map, port)) continue;
- tcp_sock_init(c, 1, AF_UNSPEC, NULL, NULL, port); + tcp_ns_sock_init(c, port); }
return 0; @@ -3279,7 +3324,7 @@ int tcp_init(struct ctx *c) if (c->mode == MODE_PASTA) { tcp_splice_init(c);
- NS_CALL(tcp_sock_init_ns, c); + NS_CALL(tcp_ns_socks_init, c);
refill_arg.ns = 1; NS_CALL(tcp_sock_refill, &refill_arg); @@ -3364,8 +3409,7 @@ static int tcp_port_rebind(void *arg)
if ((a->c->ifi4 && tcp_sock_ns[port][V4] == -1) || (a->c->ifi6 && tcp_sock_ns[port][V6] == -1)) - tcp_sock_init(a->c, 1, AF_UNSPEC, NULL, NULL, - port); + tcp_ns_sock_init(a->c, port); } } else { for (port = 0; port < NUM_PORTS; port++) { @@ -3398,7 +3442,7 @@ static int tcp_port_rebind(void *arg)
if ((a->c->ifi4 && tcp_sock_init_ext[port][V4] == -1) || (a->c->ifi6 && tcp_sock_init_ext[port][V6] == -1)) - tcp_sock_init(a->c, 0, AF_UNSPEC, NULL, NULL, + tcp_sock_init(a->c, AF_UNSPEC, NULL, NULL, port); } } diff --git a/tcp.h b/tcp.h index 49738ef..f4ed298 100644 --- a/tcp.h +++ b/tcp.h @@ -19,8 +19,8 @@ void tcp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events, const struct timespec *now); int tcp_tap_handler(struct ctx *c, int af, const void *addr, const struct pool *p, const struct timespec *now); -void tcp_sock_init(const struct ctx *c, int ns, sa_family_t af, - const void *addr, const char *ifname, in_port_t port); +void tcp_sock_init(const struct ctx *c, sa_family_t af, const void *addr, + const char *ifname, in_port_t port); int tcp_init(struct ctx *c); void tcp_timer(struct ctx *c, const struct timespec *ts); void tcp_defer_handler(struct ctx *c);
-- David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
In tcp_sock_handler() we split off to handle spliced sockets before
checking anything else. However the first steps of the "new connection"
path for each case are the same: allocate a connection entry and accept()
the connection.
Remove this duplication by making tcp_conn_from_sock() handle both spliced
and non-spliced cases, with help from more specific tcp_tap_conn_from_sock
and tcp_splice_conn_from_sock functions for the later stages which differ.
Signed-off-by: David Gibson
On Wed, 16 Nov 2022 15:41:55 +1100
David Gibson
In tcp_sock_handler() we split off to handle spliced sockets before checking anything else. However the first steps of the "new connection" path for each case are the same: allocate a connection entry and accept() the connection.
Remove this duplication by making tcp_conn_from_sock() handle both spliced and non-spliced cases, with help from more specific tcp_tap_conn_from_sock and tcp_splice_conn_from_sock functions for the later stages which differ.
Signed-off-by: David Gibson
--- tcp.c | 68 ++++++++++++++++++++++++++++++++++------------------ tcp_splice.c | 58 +++++++++++++++++++++++--------------------- tcp_splice.h | 4 ++++ 3 files changed, 80 insertions(+), 50 deletions(-) diff --git a/tcp.c b/tcp.c index 72d3b49..e66a82a 100644 --- a/tcp.c +++ b/tcp.c @@ -2753,28 +2753,19 @@ static void tcp_connect_finish(struct ctx *c, struct tcp_tap_conn *conn) }
/** - * tcp_conn_from_sock() - Handle new connection request from listening socket + * tcp_tap_conn_from_sock() - Initialize state for non-spliced connection * @c: Execution context * @ref: epoll reference of listening socket + * @conn: connection structure to initialize + * @s: Accepted socket + * @sa: Peer socket address (from accept()) * @now: Current timestamp */ -static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, - const struct timespec *now) +static void tcp_tap_conn_from_sock(struct ctx *c, union epoll_ref ref, + struct tcp_tap_conn *conn, int s, + struct sockaddr *sa, + const struct timespec *now) { - struct sockaddr_storage sa; - struct tcp_tap_conn *conn; - socklen_t sl; - int s; - - if (c->tcp.conn_count >= TCP_MAX_CONNS) - return; - - sl = sizeof(sa); - s = accept4(ref.r.s, (struct sockaddr *)&sa, &sl, SOCK_NONBLOCK); - if (s < 0) - return; - - conn = CONN(c->tcp.conn_count++); conn->c.spliced = false; conn->sock = s; conn->timer = -1; @@ -2784,7 +2775,7 @@ static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, if (ref.r.p.tcp.tcp.v6) { struct sockaddr_in6 sa6;
- memcpy(&sa6, &sa, sizeof(sa6)); + memcpy(&sa6, sa, sizeof(sa6));
if (IN6_IS_ADDR_LOOPBACK(&sa6.sin6_addr) || IN6_ARE_ADDR_EQUAL(&sa6.sin6_addr, &c->ip6.addr_seen) || @@ -2813,7 +2804,7 @@ static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, } else { struct sockaddr_in sa4;
- memcpy(&sa4, &sa, sizeof(sa4)); + memcpy(&sa4, sa, sizeof(sa4));
memset(&conn->a.a4.zero, 0, sizeof(conn->a.a4.zero)); memset(&conn->a.a4.one, 0xff, sizeof(conn->a.a4.one)); @@ -2846,6 +2837,37 @@ static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, tcp_get_sndbuf(conn); }
+/** + * tcp_conn_from_sock() - Handle new connection request from listening socket + * @c: Execution context + * @ref: epoll reference of listening socket + * @now: Current timestamp + */ +static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, + const struct timespec *now) +{ + struct sockaddr_storage sa; + union tcp_conn *conn; + socklen_t sl; + int s; + + if (c->tcp.conn_count >= TCP_MAX_CONNS) + return; + + sl = sizeof(sa); + s = accept4(ref.r.s, (struct sockaddr *)&sa, &sl, SOCK_NONBLOCK);
Combined with 16/32 I'm not sure this is simplifying much -- it looks a bit unnatural there to get the peer address not "directly" from accept4(). On the other hand you drop a few lines -- I'm fine with it either way.
+ if (s < 0) + return; + + conn = tc + c->tcp.conn_count++; + + if (ref.r.p.tcp.tcp.splice) + tcp_splice_conn_from_sock(c, ref, &conn->splice, s); + else + tcp_tap_conn_from_sock(c, ref, &conn->tap, s, + (struct sockaddr *)&sa, now); +} + /** * tcp_timer_handler() - timerfd events: close, send ACK, retransmit, or reset * @c: Execution context @@ -2925,13 +2947,13 @@ void tcp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events, return; }
- if (ref.r.p.tcp.tcp.splice) { - tcp_sock_handler_splice(c, ref, events); + if (ref.r.p.tcp.tcp.listen) { + tcp_conn_from_sock(c, ref, now); return; }
- if (ref.r.p.tcp.tcp.listen) { - tcp_conn_from_sock(c, ref, now); + if (ref.r.p.tcp.tcp.splice) { + tcp_sock_handler_splice(c, ref, events); return; }
diff --git a/tcp_splice.c b/tcp_splice.c index 7a06252..7007501 100644 --- a/tcp_splice.c +++ b/tcp_splice.c @@ -501,6 +501,36 @@ static void tcp_splice_dir(struct tcp_splice_conn *conn, int ref_sock, *pipes = *from == conn->a ? conn->pipe_a_b : conn->pipe_b_a; }
+/** + * tcp_splice_conn_from_sock() - Initialize state for spliced connection + * @c: Execution context + * @ref: epoll reference of listening socket + * @conn: connection structure to initialize + * @s: Accepted socket + * + * #syscalls:pasta setsockopt + */ +void tcp_splice_conn_from_sock(struct ctx *c, union epoll_ref ref, + struct tcp_splice_conn *conn, int s) +{ + assert(c->mode == MODE_PASTA); + + if (setsockopt(s, SOL_TCP, TCP_QUICKACK, &((int){ 1 }), + sizeof(int))) { + trace("TCP (spliced): failed to set TCP_QUICKACK on %i", + s);
This could be indented sanely, now.
+ } + + conn->c.spliced = true; + c->tcp.splice_conn_count++; + conn->a = s; + conn->flags = ref.r.p.tcp.tcp.v6 ? SPLICE_V6 : 0; + + if (tcp_splice_new(c, conn, ref.r.p.tcp.tcp.index, + ref.r.p.tcp.tcp.outbound)) + conn_flag(c, conn, CLOSING); +} + /** * tcp_sock_handler_splice() - Handler for socket mapped to spliced connection * @c: Execution context @@ -517,33 +547,7 @@ void tcp_sock_handler_splice(struct ctx *c, union epoll_ref ref, uint32_t *seq_read, *seq_write; struct tcp_splice_conn *conn;
- if (ref.r.p.tcp.tcp.listen) { - int s; - - if (c->tcp.conn_count >= TCP_MAX_CONNS) - return; - - if ((s = accept4(ref.r.s, NULL, NULL, SOCK_NONBLOCK)) < 0) - return; - - if (setsockopt(s, SOL_TCP, TCP_QUICKACK, &((int){ 1 }), - sizeof(int))) { - trace("TCP (spliced): failed to set TCP_QUICKACK on %i", - s); - } - - conn = CONN(c->tcp.conn_count++); - conn->c.spliced = true; - c->tcp.splice_conn_count++; - conn->a = s; - conn->flags = ref.r.p.tcp.tcp.v6 ? SPLICE_V6 : 0; - - if (tcp_splice_new(c, conn, ref.r.p.tcp.tcp.index, - ref.r.p.tcp.tcp.outbound)) - conn_flag(c, conn, CLOSING); - - return; - } + assert(!ref.r.p.tcp.tcp.listen);
conn = CONN(ref.r.p.tcp.tcp.index);
diff --git a/tcp_splice.h b/tcp_splice.h index 22024d6..f9462ae 100644 --- a/tcp_splice.h +++ b/tcp_splice.h @@ -6,8 +6,12 @@ #ifndef TCP_SPLICE_H #define TCP_SPLICE_H
+struct tcp_splice_conn; + void tcp_sock_handler_splice(struct ctx *c, union epoll_ref ref, uint32_t events); +void tcp_splice_conn_from_sock(struct ctx *c, union epoll_ref ref, + struct tcp_splice_conn *conn, int s); void tcp_splice_init(struct ctx *c);
#endif /* TCP_SPLICE_H */
-- Stefano
On Thu, Nov 17, 2022 at 12:53:58AM +0100, Stefano Brivio wrote:
On Wed, 16 Nov 2022 15:41:55 +1100 David Gibson
wrote: In tcp_sock_handler() we split off to handle spliced sockets before checking anything else. However the first steps of the "new connection" path for each case are the same: allocate a connection entry and accept() the connection.
Remove this duplication by making tcp_conn_from_sock() handle both spliced and non-spliced cases, with help from more specific tcp_tap_conn_from_sock and tcp_splice_conn_from_sock functions for the later stages which differ.
Signed-off-by: David Gibson
--- tcp.c | 68 ++++++++++++++++++++++++++++++++++------------------ tcp_splice.c | 58 +++++++++++++++++++++++--------------------- tcp_splice.h | 4 ++++ 3 files changed, 80 insertions(+), 50 deletions(-) diff --git a/tcp.c b/tcp.c index 72d3b49..e66a82a 100644 --- a/tcp.c +++ b/tcp.c @@ -2753,28 +2753,19 @@ static void tcp_connect_finish(struct ctx *c, struct tcp_tap_conn *conn) }
/** - * tcp_conn_from_sock() - Handle new connection request from listening socket + * tcp_tap_conn_from_sock() - Initialize state for non-spliced connection * @c: Execution context * @ref: epoll reference of listening socket + * @conn: connection structure to initialize + * @s: Accepted socket + * @sa: Peer socket address (from accept()) * @now: Current timestamp */ -static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, - const struct timespec *now) +static void tcp_tap_conn_from_sock(struct ctx *c, union epoll_ref ref, + struct tcp_tap_conn *conn, int s, + struct sockaddr *sa, + const struct timespec *now) { - struct sockaddr_storage sa; - struct tcp_tap_conn *conn; - socklen_t sl; - int s; - - if (c->tcp.conn_count >= TCP_MAX_CONNS) - return; - - sl = sizeof(sa); - s = accept4(ref.r.s, (struct sockaddr *)&sa, &sl, SOCK_NONBLOCK); - if (s < 0) - return; - - conn = CONN(c->tcp.conn_count++); conn->c.spliced = false; conn->sock = s; conn->timer = -1; @@ -2784,7 +2775,7 @@ static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, if (ref.r.p.tcp.tcp.v6) { struct sockaddr_in6 sa6;
- memcpy(&sa6, &sa, sizeof(sa6)); + memcpy(&sa6, sa, sizeof(sa6));
if (IN6_IS_ADDR_LOOPBACK(&sa6.sin6_addr) || IN6_ARE_ADDR_EQUAL(&sa6.sin6_addr, &c->ip6.addr_seen) || @@ -2813,7 +2804,7 @@ static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, } else { struct sockaddr_in sa4;
- memcpy(&sa4, &sa, sizeof(sa4)); + memcpy(&sa4, sa, sizeof(sa4));
memset(&conn->a.a4.zero, 0, sizeof(conn->a.a4.zero)); memset(&conn->a.a4.one, 0xff, sizeof(conn->a.a4.one)); @@ -2846,6 +2837,37 @@ static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, tcp_get_sndbuf(conn); }
+/** + * tcp_conn_from_sock() - Handle new connection request from listening socket + * @c: Execution context + * @ref: epoll reference of listening socket + * @now: Current timestamp + */ +static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, + const struct timespec *now) +{ + struct sockaddr_storage sa; + union tcp_conn *conn; + socklen_t sl; + int s; + + if (c->tcp.conn_count >= TCP_MAX_CONNS) + return; + + sl = sizeof(sa); + s = accept4(ref.r.s, (struct sockaddr *)&sa, &sl, SOCK_NONBLOCK);
Combined with 16/32 I'm not sure this is simplifying much -- it looks a bit unnatural there to get the peer address not "directly" from accept4(). On the other hand you drop a few lines -- I'm fine with it either way.
Um.. I'm not really sure what you're getting at here.
+ if (s < 0) + return; + + conn = tc + c->tcp.conn_count++; + + if (ref.r.p.tcp.tcp.splice) + tcp_splice_conn_from_sock(c, ref, &conn->splice, s); + else + tcp_tap_conn_from_sock(c, ref, &conn->tap, s, + (struct sockaddr *)&sa, now); +} + /** * tcp_timer_handler() - timerfd events: close, send ACK, retransmit, or reset * @c: Execution context @@ -2925,13 +2947,13 @@ void tcp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events, return; }
- if (ref.r.p.tcp.tcp.splice) { - tcp_sock_handler_splice(c, ref, events); + if (ref.r.p.tcp.tcp.listen) { + tcp_conn_from_sock(c, ref, now); return; }
- if (ref.r.p.tcp.tcp.listen) { - tcp_conn_from_sock(c, ref, now); + if (ref.r.p.tcp.tcp.splice) { + tcp_sock_handler_splice(c, ref, events); return; }
diff --git a/tcp_splice.c b/tcp_splice.c index 7a06252..7007501 100644 --- a/tcp_splice.c +++ b/tcp_splice.c @@ -501,6 +501,36 @@ static void tcp_splice_dir(struct tcp_splice_conn *conn, int ref_sock, *pipes = *from == conn->a ? conn->pipe_a_b : conn->pipe_b_a; }
+/** + * tcp_splice_conn_from_sock() - Initialize state for spliced connection + * @c: Execution context + * @ref: epoll reference of listening socket + * @conn: connection structure to initialize + * @s: Accepted socket + * + * #syscalls:pasta setsockopt + */ +void tcp_splice_conn_from_sock(struct ctx *c, union epoll_ref ref, + struct tcp_splice_conn *conn, int s) +{ + assert(c->mode == MODE_PASTA); + + if (setsockopt(s, SOL_TCP, TCP_QUICKACK, &((int){ 1 }), + sizeof(int))) { + trace("TCP (spliced): failed to set TCP_QUICKACK on %i", + s);
This could be indented sanely, now.
Fixed.
+ } + + conn->c.spliced = true; + c->tcp.splice_conn_count++; + conn->a = s; + conn->flags = ref.r.p.tcp.tcp.v6 ? SPLICE_V6 : 0; + + if (tcp_splice_new(c, conn, ref.r.p.tcp.tcp.index, + ref.r.p.tcp.tcp.outbound)) + conn_flag(c, conn, CLOSING); +} + /** * tcp_sock_handler_splice() - Handler for socket mapped to spliced connection * @c: Execution context @@ -517,33 +547,7 @@ void tcp_sock_handler_splice(struct ctx *c, union epoll_ref ref, uint32_t *seq_read, *seq_write; struct tcp_splice_conn *conn;
- if (ref.r.p.tcp.tcp.listen) { - int s; - - if (c->tcp.conn_count >= TCP_MAX_CONNS) - return; - - if ((s = accept4(ref.r.s, NULL, NULL, SOCK_NONBLOCK)) < 0) - return; - - if (setsockopt(s, SOL_TCP, TCP_QUICKACK, &((int){ 1 }), - sizeof(int))) { - trace("TCP (spliced): failed to set TCP_QUICKACK on %i", - s); - } - - conn = CONN(c->tcp.conn_count++); - conn->c.spliced = true; - c->tcp.splice_conn_count++; - conn->a = s; - conn->flags = ref.r.p.tcp.tcp.v6 ? SPLICE_V6 : 0; - - if (tcp_splice_new(c, conn, ref.r.p.tcp.tcp.index, - ref.r.p.tcp.tcp.outbound)) - conn_flag(c, conn, CLOSING); - - return; - } + assert(!ref.r.p.tcp.tcp.listen);
conn = CONN(ref.r.p.tcp.tcp.index);
diff --git a/tcp_splice.h b/tcp_splice.h index 22024d6..f9462ae 100644 --- a/tcp_splice.h +++ b/tcp_splice.h @@ -6,8 +6,12 @@ #ifndef TCP_SPLICE_H #define TCP_SPLICE_H
+struct tcp_splice_conn; + void tcp_sock_handler_splice(struct ctx *c, union epoll_ref ref, uint32_t events); +void tcp_splice_conn_from_sock(struct ctx *c, union epoll_ref ref, + struct tcp_splice_conn *conn, int s); void tcp_splice_init(struct ctx *c);
#endif /* TCP_SPLICE_H */
-- David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
On Thu, 17 Nov 2022 12:37:04 +1100
David Gibson
On Thu, Nov 17, 2022 at 12:53:58AM +0100, Stefano Brivio wrote:
On Wed, 16 Nov 2022 15:41:55 +1100 David Gibson
wrote: In tcp_sock_handler() we split off to handle spliced sockets before checking anything else. However the first steps of the "new connection" path for each case are the same: allocate a connection entry and accept() the connection.
Remove this duplication by making tcp_conn_from_sock() handle both spliced and non-spliced cases, with help from more specific tcp_tap_conn_from_sock and tcp_splice_conn_from_sock functions for the later stages which differ.
Signed-off-by: David Gibson
--- tcp.c | 68 ++++++++++++++++++++++++++++++++++------------------ tcp_splice.c | 58 +++++++++++++++++++++++--------------------- tcp_splice.h | 4 ++++ 3 files changed, 80 insertions(+), 50 deletions(-) diff --git a/tcp.c b/tcp.c index 72d3b49..e66a82a 100644 --- a/tcp.c +++ b/tcp.c @@ -2753,28 +2753,19 @@ static void tcp_connect_finish(struct ctx *c, struct tcp_tap_conn *conn) }
/** - * tcp_conn_from_sock() - Handle new connection request from listening socket + * tcp_tap_conn_from_sock() - Initialize state for non-spliced connection * @c: Execution context * @ref: epoll reference of listening socket + * @conn: connection structure to initialize + * @s: Accepted socket + * @sa: Peer socket address (from accept()) * @now: Current timestamp */ -static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, - const struct timespec *now) +static void tcp_tap_conn_from_sock(struct ctx *c, union epoll_ref ref, + struct tcp_tap_conn *conn, int s, + struct sockaddr *sa, + const struct timespec *now) { - struct sockaddr_storage sa; - struct tcp_tap_conn *conn; - socklen_t sl; - int s; - - if (c->tcp.conn_count >= TCP_MAX_CONNS) - return; - - sl = sizeof(sa); - s = accept4(ref.r.s, (struct sockaddr *)&sa, &sl, SOCK_NONBLOCK); - if (s < 0) - return; - - conn = CONN(c->tcp.conn_count++); conn->c.spliced = false; conn->sock = s; conn->timer = -1; @@ -2784,7 +2775,7 @@ static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, if (ref.r.p.tcp.tcp.v6) { struct sockaddr_in6 sa6;
- memcpy(&sa6, &sa, sizeof(sa6)); + memcpy(&sa6, sa, sizeof(sa6));
if (IN6_IS_ADDR_LOOPBACK(&sa6.sin6_addr) || IN6_ARE_ADDR_EQUAL(&sa6.sin6_addr, &c->ip6.addr_seen) || @@ -2813,7 +2804,7 @@ static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, } else { struct sockaddr_in sa4;
- memcpy(&sa4, &sa, sizeof(sa4)); + memcpy(&sa4, sa, sizeof(sa4));
memset(&conn->a.a4.zero, 0, sizeof(conn->a.a4.zero)); memset(&conn->a.a4.one, 0xff, sizeof(conn->a.a4.one)); @@ -2846,6 +2837,37 @@ static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, tcp_get_sndbuf(conn); }
+/** + * tcp_conn_from_sock() - Handle new connection request from listening socket + * @c: Execution context + * @ref: epoll reference of listening socket + * @now: Current timestamp + */ +static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, + const struct timespec *now) +{ + struct sockaddr_storage sa; + union tcp_conn *conn; + socklen_t sl; + int s; + + if (c->tcp.conn_count >= TCP_MAX_CONNS) + return; + + sl = sizeof(sa); + s = accept4(ref.r.s, (struct sockaddr *)&sa, &sl, SOCK_NONBLOCK);
Combined with 16/32 I'm not sure this is simplifying much -- it looks a bit unnatural there to get the peer address not "directly" from accept4(). On the other hand you drop a few lines -- I'm fine with it either way.
Um.. I'm not really sure what you're getting at here.
By "directly" I mean assigned by accept4() in the same function, instead of accept4() being done in the caller. That is, if I now look at tcp_tap_conn_from_sock() we have 'sa' there which comes as an argument, not directly a couple of lines above from accept4(), which would be quicker to review. On the other hand the function comment says "from accept()", so it's not much effort to figure that out either. -- Stefano
On Thu, Nov 17, 2022 at 08:30:29AM +0100, Stefano Brivio wrote:
On Thu, 17 Nov 2022 12:37:04 +1100 David Gibson
wrote: On Thu, Nov 17, 2022 at 12:53:58AM +0100, Stefano Brivio wrote:
On Wed, 16 Nov 2022 15:41:55 +1100 David Gibson
wrote: In tcp_sock_handler() we split off to handle spliced sockets before checking anything else. However the first steps of the "new connection" path for each case are the same: allocate a connection entry and accept() the connection.
Remove this duplication by making tcp_conn_from_sock() handle both spliced and non-spliced cases, with help from more specific tcp_tap_conn_from_sock and tcp_splice_conn_from_sock functions for the later stages which differ.
Signed-off-by: David Gibson
--- tcp.c | 68 ++++++++++++++++++++++++++++++++++------------------ tcp_splice.c | 58 +++++++++++++++++++++++--------------------- tcp_splice.h | 4 ++++ 3 files changed, 80 insertions(+), 50 deletions(-) diff --git a/tcp.c b/tcp.c index 72d3b49..e66a82a 100644 --- a/tcp.c +++ b/tcp.c @@ -2753,28 +2753,19 @@ static void tcp_connect_finish(struct ctx *c, struct tcp_tap_conn *conn) }
/** - * tcp_conn_from_sock() - Handle new connection request from listening socket + * tcp_tap_conn_from_sock() - Initialize state for non-spliced connection * @c: Execution context * @ref: epoll reference of listening socket + * @conn: connection structure to initialize + * @s: Accepted socket + * @sa: Peer socket address (from accept()) * @now: Current timestamp */ -static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, - const struct timespec *now) +static void tcp_tap_conn_from_sock(struct ctx *c, union epoll_ref ref, + struct tcp_tap_conn *conn, int s, + struct sockaddr *sa, + const struct timespec *now) { - struct sockaddr_storage sa; - struct tcp_tap_conn *conn; - socklen_t sl; - int s; - - if (c->tcp.conn_count >= TCP_MAX_CONNS) - return; - - sl = sizeof(sa); - s = accept4(ref.r.s, (struct sockaddr *)&sa, &sl, SOCK_NONBLOCK); - if (s < 0) - return; - - conn = CONN(c->tcp.conn_count++); conn->c.spliced = false; conn->sock = s; conn->timer = -1; @@ -2784,7 +2775,7 @@ static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, if (ref.r.p.tcp.tcp.v6) { struct sockaddr_in6 sa6;
- memcpy(&sa6, &sa, sizeof(sa6)); + memcpy(&sa6, sa, sizeof(sa6));
if (IN6_IS_ADDR_LOOPBACK(&sa6.sin6_addr) || IN6_ARE_ADDR_EQUAL(&sa6.sin6_addr, &c->ip6.addr_seen) || @@ -2813,7 +2804,7 @@ static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, } else { struct sockaddr_in sa4;
- memcpy(&sa4, &sa, sizeof(sa4)); + memcpy(&sa4, sa, sizeof(sa4));
memset(&conn->a.a4.zero, 0, sizeof(conn->a.a4.zero)); memset(&conn->a.a4.one, 0xff, sizeof(conn->a.a4.one)); @@ -2846,6 +2837,37 @@ static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, tcp_get_sndbuf(conn); }
+/** + * tcp_conn_from_sock() - Handle new connection request from listening socket + * @c: Execution context + * @ref: epoll reference of listening socket + * @now: Current timestamp + */ +static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, + const struct timespec *now) +{ + struct sockaddr_storage sa; + union tcp_conn *conn; + socklen_t sl; + int s; + + if (c->tcp.conn_count >= TCP_MAX_CONNS) + return; + + sl = sizeof(sa); + s = accept4(ref.r.s, (struct sockaddr *)&sa, &sl, SOCK_NONBLOCK);
Combined with 16/32 I'm not sure this is simplifying much -- it looks a bit unnatural there to get the peer address not "directly" from accept4(). On the other hand you drop a few lines -- I'm fine with it either way.
Um.. I'm not really sure what you're getting at here.
By "directly" I mean assigned by accept4() in the same function, instead of accept4() being done in the caller.
That is, if I now look at tcp_tap_conn_from_sock() we have 'sa' there which comes as an argument, not directly a couple of lines above from accept4(), which would be quicker to review.
Right, but this is unavoidable. This patch is a preliminary to deciding whether to take the spliced or non-spliced route based on the address we get from accept4().
On the other hand the function comment says "from accept()", so it's not much effort to figure that out either.
-- David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
In pasta mode, tcp_sock_init[46]() create separate sockets to listen for
spliced connections (these are bound to localhost) and non-spliced
connections (these are bound to the host address). This introduces a
subtle behavioural difference between pasta and passt: by default, pasta
will listen only on a single host address, whereas passt will listen on
all addresses (0.0.0.0 or ::). This also prevents us using some additional
optimizations that only work with the unspecified (0.0.0.0 or ::) address.
However, it turns out we don't need to do this. We can splice a connection
if and only if it originates from the loopback address. Currently we
ensure this by having the "spliced" listening sockets listening only on
loopback. Instead, defer the decision about whether to splice a connection
until after accept(), by checking if the connection was made from the
loopback address.
Signed-off-by: David Gibson
On Wed, 16 Nov 2022 15:41:56 +1100
David Gibson
In pasta mode, tcp_sock_init[46]() create separate sockets to listen for spliced connections (these are bound to localhost) and non-spliced connections (these are bound to the host address). This introduces a subtle behavioural difference between pasta and passt: by default, pasta will listen only on a single host address, whereas passt will listen on all addresses (0.0.0.0 or ::). This also prevents us using some additional optimizations that only work with the unspecified (0.0.0.0 or ::) address.
However, it turns out we don't need to do this. We can splice a connection if and only if it originates from the loopback address. Currently we ensure this by having the "spliced" listening sockets listening only on loopback. Instead, defer the decision about whether to splice a connection until after accept(), by checking if the connection was made from the loopback address.
Signed-off-by: David Gibson
--- tcp.c | 127 +++++++++++++-------------------------------------- tcp_splice.c | 25 ++++++++-- tcp_splice.h | 5 +- 3 files changed, 55 insertions(+), 102 deletions(-) diff --git a/tcp.c b/tcp.c index e66a82a..4065da7 100644 --- a/tcp.c +++ b/tcp.c @@ -434,7 +434,6 @@ static const char *tcp_flag_str[] __attribute((__unused__)) = { };
/* Listening sockets, used for automatic port forwarding in pasta mode only */ -static int tcp_sock_init_lo [NUM_PORTS][IP_VERSIONS]; static int tcp_sock_init_ext [NUM_PORTS][IP_VERSIONS]; static int tcp_sock_ns [NUM_PORTS][IP_VERSIONS];
@@ -2851,21 +2850,31 @@ static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, socklen_t sl; int s;
+ assert(ref.r.p.tcp.tcp.listen); + assert(!ref.r.p.tcp.tcp.splice); + if (c->tcp.conn_count >= TCP_MAX_CONNS) return;
sl = sizeof(sa); + /* FIXME: Workaround clang-tidy not realizing that accept4() + * writes the socket address. See + * https://github.com/llvm/llvm-project/issues/58992 + */ + memset(&sa, 0, sizeof(struct sockaddr_in6)); s = accept4(ref.r.s, (struct sockaddr *)&sa, &sl, SOCK_NONBLOCK);
Ah, interesting. That looks new by the way -- not even valgrind complained about this.
if (s < 0) return;
conn = tc + c->tcp.conn_count++;
- if (ref.r.p.tcp.tcp.splice) - tcp_splice_conn_from_sock(c, ref, &conn->splice, s); - else - tcp_tap_conn_from_sock(c, ref, &conn->tap, s, - (struct sockaddr *)&sa, now); + if (c->mode == MODE_PASTA && + tcp_splice_conn_from_sock(c, ref, &conn->splice, + s, (struct sockaddr *)&sa)) + return; + + tcp_tap_conn_from_sock(c, ref, &conn->tap, s, + (struct sockaddr *)&sa, now); }
/** @@ -3018,47 +3027,16 @@ static void tcp_sock_init4(const struct ctx *c, const struct in_addr *addr, { in_port_t idx = port + c->tcp.fwd_in.delta[port]; union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.index = idx }; - bool spliced = false, tap = true; int s;
- if (c->mode == MODE_PASTA) { - spliced = !addr || IN4_IS_ADDR_UNSPECIFIED(addr) || - IN4_IS_ADDR_LOOPBACK(addr); - - if (!addr) - addr = &c->ip4.addr; - - tap = !IN4_IS_ADDR_LOOPBACK(addr); - } - - if (tap) { - s = sock_l4(c, AF_INET, IPPROTO_TCP, addr, ifname, port, - tref.u32); - if (s >= 0) - tcp_sock_set_bufsize(c, s); - else - s = -1; - - if (c->tcp.fwd_in.mode == FWD_AUTO) - tcp_sock_init_ext[port][V4] = s; - } - - if (spliced) { - struct in_addr loopback = { htonl(INADDR_LOOPBACK) }; - tref.tcp.splice = 1; - - addr = &loopback; - - s = sock_l4(c, AF_INET, IPPROTO_TCP, addr, ifname, port, - tref.u32); - if (s >= 0) - tcp_sock_set_bufsize(c, s); - else - s = -1; + s = sock_l4(c, AF_INET, IPPROTO_TCP, addr, ifname, port, tref.u32); + if (s >= 0) + tcp_sock_set_bufsize(c, s); + else + s = -1;
- if (c->tcp.fwd_out.mode == FWD_AUTO) - tcp_sock_init_lo[port][V4] = s; - } + if (c->tcp.fwd_in.mode == FWD_AUTO) + tcp_sock_init_ext[port][V4] = s; }
/** @@ -3075,47 +3053,16 @@ static void tcp_sock_init6(const struct ctx *c, in_port_t idx = port + c->tcp.fwd_in.delta[port]; union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.v6 = 1, .tcp.index = idx }; - bool spliced = false, tap = true; int s;
- if (c->mode == MODE_PASTA) { - spliced = !addr || - IN6_IS_ADDR_UNSPECIFIED(addr) || - IN6_IS_ADDR_LOOPBACK(addr); - - if (!addr) - addr = &c->ip6.addr; - - tap = !IN6_IS_ADDR_LOOPBACK(addr); - } - - if (tap) { - s = sock_l4(c, AF_INET6, IPPROTO_TCP, addr, ifname, port, - tref.u32); - if (s >= 0) - tcp_sock_set_bufsize(c, s); - else - s = -1; - - if (c->tcp.fwd_in.mode == FWD_AUTO) - tcp_sock_init_ext[port][V6] = s; - } - - if (spliced) { - tref.tcp.splice = 1; - - addr = &in6addr_loopback; - - s = sock_l4(c, AF_INET6, IPPROTO_TCP, addr, ifname, port, - tref.u32); - if (s >= 0) - tcp_sock_set_bufsize(c, s); - else - s = -1; + s = sock_l4(c, AF_INET6, IPPROTO_TCP, addr, ifname, port, tref.u32); + if (s >= 0) + tcp_sock_set_bufsize(c, s); + else + s = -1;
- if (c->tcp.fwd_out.mode == FWD_AUTO) - tcp_sock_init_lo[port][V6] = s; - } + if (c->tcp.fwd_in.mode == FWD_AUTO) + tcp_sock_init_ext[port][V6] = s; }
/** @@ -3144,7 +3091,7 @@ static void tcp_ns_sock_init4(const struct ctx *c, in_port_t port) { in_port_t idx = port + c->tcp.fwd_out.delta[port]; union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.outbound = 1, - .tcp.splice = 1, .tcp.index = idx }; + .tcp.index = idx }; struct in_addr loopback = { htonl(INADDR_LOOPBACK) }; int s;
@@ -3169,8 +3116,7 @@ static void tcp_ns_sock_init6(const struct ctx *c, in_port_t port) { in_port_t idx = port + c->tcp.fwd_out.delta[port]; union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.outbound = 1, - .tcp.splice = 1, .tcp.v6 = 1, - .tcp.index = idx}; + .tcp.v6 = 1, .tcp.index = idx};
Space missing here (from 14/32).
int s;
assert(c->mode == MODE_PASTA); @@ -3337,7 +3283,6 @@ int tcp_init(struct ctx *c) memset(init_sock_pool6, 0xff, sizeof(init_sock_pool6)); memset(ns_sock_pool4, 0xff, sizeof(ns_sock_pool4)); memset(ns_sock_pool6, 0xff, sizeof(ns_sock_pool6)); - memset(tcp_sock_init_lo, 0xff, sizeof(tcp_sock_init_lo)); memset(tcp_sock_init_ext, 0xff, sizeof(tcp_sock_init_ext)); memset(tcp_sock_ns, 0xff, sizeof(tcp_sock_ns));
@@ -3445,16 +3390,6 @@ static int tcp_port_rebind(void *arg) close(tcp_sock_init_ext[port][V6]); tcp_sock_init_ext[port][V6] = -1; } - - if (tcp_sock_init_lo[port][V4] >= 0) { - close(tcp_sock_init_lo[port][V4]); - tcp_sock_init_lo[port][V4] = -1; - } - - if (tcp_sock_init_lo[port][V6] >= 0) { - close(tcp_sock_init_lo[port][V6]); - tcp_sock_init_lo[port][V6] = -1; - } continue; }
diff --git a/tcp_splice.c b/tcp_splice.c index 7007501..30d49d4 100644 --- a/tcp_splice.c +++ b/tcp_splice.c @@ -502,19 +502,35 @@ static void tcp_splice_dir(struct tcp_splice_conn *conn, int ref_sock, }
/** - * tcp_splice_conn_from_sock() - Initialize state for spliced connection + * tcp_splice_conn_from_sock() - Attempt to init state for a spliced connection * @c: Execution context * @ref: epoll reference of listening socket * @conn: connection structure to initialize * @s: Accepted socket + * @sa: Peer address of connection * + * Return: true if able to create a spliced connection, false otherwise * #syscalls:pasta setsockopt */ -void tcp_splice_conn_from_sock(struct ctx *c, union epoll_ref ref, - struct tcp_splice_conn *conn, int s) +bool tcp_splice_conn_from_sock(struct ctx *c, union epoll_ref ref, + struct tcp_splice_conn *conn, int s, + const struct sockaddr *sa) { assert(c->mode == MODE_PASTA);
+ if (ref.r.p.tcp.tcp.v6) { + const struct sockaddr_in6 *sa6 + = (const struct sockaddr_in6 *)sa;
Maybe you could split declaration and assignment here.
+ if (!IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr)) + return false; + conn->flags = SPLICE_V6; + } else { + const struct sockaddr_in *sa4 = (const struct sockaddr_in *)sa; + if (!IN4_IS_ADDR_LOOPBACK(&sa4->sin_addr)) + return false; + conn->flags = 0; + } + if (setsockopt(s, SOL_TCP, TCP_QUICKACK, &((int){ 1 }), sizeof(int))) { trace("TCP (spliced): failed to set TCP_QUICKACK on %i", @@ -524,11 +540,12 @@ void tcp_splice_conn_from_sock(struct ctx *c, union epoll_ref ref, conn->c.spliced = true; c->tcp.splice_conn_count++; conn->a = s; - conn->flags = ref.r.p.tcp.tcp.v6 ? SPLICE_V6 : 0;
if (tcp_splice_new(c, conn, ref.r.p.tcp.tcp.index, ref.r.p.tcp.tcp.outbound)) conn_flag(c, conn, CLOSING); + + return true; }
/** diff --git a/tcp_splice.h b/tcp_splice.h index f9462ae..1a915dd 100644 --- a/tcp_splice.h +++ b/tcp_splice.h @@ -10,8 +10,9 @@ struct tcp_splice_conn;
void tcp_sock_handler_splice(struct ctx *c, union epoll_ref ref, uint32_t events); -void tcp_splice_conn_from_sock(struct ctx *c, union epoll_ref ref, - struct tcp_splice_conn *conn, int s); +bool tcp_splice_conn_from_sock(struct ctx *c, union epoll_ref ref, + struct tcp_splice_conn *conn, int s, + const struct sockaddr *sa); void tcp_splice_init(struct ctx *c);
#endif /* TCP_SPLICE_H */
On Thu, Nov 17, 2022 at 12:54:05AM +0100, Stefano Brivio wrote:
On Wed, 16 Nov 2022 15:41:56 +1100 David Gibson
wrote: In pasta mode, tcp_sock_init[46]() create separate sockets to listen for spliced connections (these are bound to localhost) and non-spliced connections (these are bound to the host address). This introduces a subtle behavioural difference between pasta and passt: by default, pasta will listen only on a single host address, whereas passt will listen on all addresses (0.0.0.0 or ::). This also prevents us using some additional optimizations that only work with the unspecified (0.0.0.0 or ::) address.
However, it turns out we don't need to do this. We can splice a connection if and only if it originates from the loopback address. Currently we ensure this by having the "spliced" listening sockets listening only on loopback. Instead, defer the decision about whether to splice a connection until after accept(), by checking if the connection was made from the loopback address.
Signed-off-by: David Gibson
--- tcp.c | 127 +++++++++++++-------------------------------------- tcp_splice.c | 25 ++++++++-- tcp_splice.h | 5 +- 3 files changed, 55 insertions(+), 102 deletions(-) diff --git a/tcp.c b/tcp.c index e66a82a..4065da7 100644 --- a/tcp.c +++ b/tcp.c @@ -434,7 +434,6 @@ static const char *tcp_flag_str[] __attribute((__unused__)) = { };
/* Listening sockets, used for automatic port forwarding in pasta mode only */ -static int tcp_sock_init_lo [NUM_PORTS][IP_VERSIONS]; static int tcp_sock_init_ext [NUM_PORTS][IP_VERSIONS]; static int tcp_sock_ns [NUM_PORTS][IP_VERSIONS];
@@ -2851,21 +2850,31 @@ static void tcp_conn_from_sock(struct ctx *c, union epoll_ref ref, socklen_t sl; int s;
+ assert(ref.r.p.tcp.tcp.listen); + assert(!ref.r.p.tcp.tcp.splice); + if (c->tcp.conn_count >= TCP_MAX_CONNS) return;
sl = sizeof(sa); + /* FIXME: Workaround clang-tidy not realizing that accept4() + * writes the socket address. See + * https://github.com/llvm/llvm-project/issues/58992 + */ + memset(&sa, 0, sizeof(struct sockaddr_in6)); s = accept4(ref.r.s, (struct sockaddr *)&sa, &sl, SOCK_NONBLOCK);
Ah, interesting. That looks new by the way -- not even valgrind complained about this.
Right, valgrind seems to have better modeling of the syscall here. Note that this is harder for a static tool to get right, because the amount that accept() writes is variable. I got a reply on that bug report, saying apparently clang-tidy *should* consider sa written here, and they weren't easily able to reproduce the problem. So, I'm not sure what's going on here :(.
if (s < 0) return;
conn = tc + c->tcp.conn_count++;
- if (ref.r.p.tcp.tcp.splice) - tcp_splice_conn_from_sock(c, ref, &conn->splice, s); - else - tcp_tap_conn_from_sock(c, ref, &conn->tap, s, - (struct sockaddr *)&sa, now); + if (c->mode == MODE_PASTA && + tcp_splice_conn_from_sock(c, ref, &conn->splice, + s, (struct sockaddr *)&sa)) + return; + + tcp_tap_conn_from_sock(c, ref, &conn->tap, s, + (struct sockaddr *)&sa, now); }
/** @@ -3018,47 +3027,16 @@ static void tcp_sock_init4(const struct ctx *c, const struct in_addr *addr, { in_port_t idx = port + c->tcp.fwd_in.delta[port]; union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.index = idx }; - bool spliced = false, tap = true; int s;
- if (c->mode == MODE_PASTA) { - spliced = !addr || IN4_IS_ADDR_UNSPECIFIED(addr) || - IN4_IS_ADDR_LOOPBACK(addr); - - if (!addr) - addr = &c->ip4.addr; - - tap = !IN4_IS_ADDR_LOOPBACK(addr); - } - - if (tap) { - s = sock_l4(c, AF_INET, IPPROTO_TCP, addr, ifname, port, - tref.u32); - if (s >= 0) - tcp_sock_set_bufsize(c, s); - else - s = -1; - - if (c->tcp.fwd_in.mode == FWD_AUTO) - tcp_sock_init_ext[port][V4] = s; - } - - if (spliced) { - struct in_addr loopback = { htonl(INADDR_LOOPBACK) }; - tref.tcp.splice = 1; - - addr = &loopback; - - s = sock_l4(c, AF_INET, IPPROTO_TCP, addr, ifname, port, - tref.u32); - if (s >= 0) - tcp_sock_set_bufsize(c, s); - else - s = -1; + s = sock_l4(c, AF_INET, IPPROTO_TCP, addr, ifname, port, tref.u32); + if (s >= 0) + tcp_sock_set_bufsize(c, s); + else + s = -1;
- if (c->tcp.fwd_out.mode == FWD_AUTO) - tcp_sock_init_lo[port][V4] = s; - } + if (c->tcp.fwd_in.mode == FWD_AUTO) + tcp_sock_init_ext[port][V4] = s; }
/** @@ -3075,47 +3053,16 @@ static void tcp_sock_init6(const struct ctx *c, in_port_t idx = port + c->tcp.fwd_in.delta[port]; union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.v6 = 1, .tcp.index = idx }; - bool spliced = false, tap = true; int s;
- if (c->mode == MODE_PASTA) { - spliced = !addr || - IN6_IS_ADDR_UNSPECIFIED(addr) || - IN6_IS_ADDR_LOOPBACK(addr); - - if (!addr) - addr = &c->ip6.addr; - - tap = !IN6_IS_ADDR_LOOPBACK(addr); - } - - if (tap) { - s = sock_l4(c, AF_INET6, IPPROTO_TCP, addr, ifname, port, - tref.u32); - if (s >= 0) - tcp_sock_set_bufsize(c, s); - else - s = -1; - - if (c->tcp.fwd_in.mode == FWD_AUTO) - tcp_sock_init_ext[port][V6] = s; - } - - if (spliced) { - tref.tcp.splice = 1; - - addr = &in6addr_loopback; - - s = sock_l4(c, AF_INET6, IPPROTO_TCP, addr, ifname, port, - tref.u32); - if (s >= 0) - tcp_sock_set_bufsize(c, s); - else - s = -1; + s = sock_l4(c, AF_INET6, IPPROTO_TCP, addr, ifname, port, tref.u32); + if (s >= 0) + tcp_sock_set_bufsize(c, s); + else + s = -1;
- if (c->tcp.fwd_out.mode == FWD_AUTO) - tcp_sock_init_lo[port][V6] = s; - } + if (c->tcp.fwd_in.mode == FWD_AUTO) + tcp_sock_init_ext[port][V6] = s; }
/** @@ -3144,7 +3091,7 @@ static void tcp_ns_sock_init4(const struct ctx *c, in_port_t port) { in_port_t idx = port + c->tcp.fwd_out.delta[port]; union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.outbound = 1, - .tcp.splice = 1, .tcp.index = idx }; + .tcp.index = idx }; struct in_addr loopback = { htonl(INADDR_LOOPBACK) }; int s;
@@ -3169,8 +3116,7 @@ static void tcp_ns_sock_init6(const struct ctx *c, in_port_t port) { in_port_t idx = port + c->tcp.fwd_out.delta[port]; union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.outbound = 1, - .tcp.splice = 1, .tcp.v6 = 1, - .tcp.index = idx}; + .tcp.v6 = 1, .tcp.index = idx};
Space missing here (from 14/32).
Fixed.
int s;
assert(c->mode == MODE_PASTA); @@ -3337,7 +3283,6 @@ int tcp_init(struct ctx *c) memset(init_sock_pool6, 0xff, sizeof(init_sock_pool6)); memset(ns_sock_pool4, 0xff, sizeof(ns_sock_pool4)); memset(ns_sock_pool6, 0xff, sizeof(ns_sock_pool6)); - memset(tcp_sock_init_lo, 0xff, sizeof(tcp_sock_init_lo)); memset(tcp_sock_init_ext, 0xff, sizeof(tcp_sock_init_ext)); memset(tcp_sock_ns, 0xff, sizeof(tcp_sock_ns));
@@ -3445,16 +3390,6 @@ static int tcp_port_rebind(void *arg) close(tcp_sock_init_ext[port][V6]); tcp_sock_init_ext[port][V6] = -1; } - - if (tcp_sock_init_lo[port][V4] >= 0) { - close(tcp_sock_init_lo[port][V4]); - tcp_sock_init_lo[port][V4] = -1; - } - - if (tcp_sock_init_lo[port][V6] >= 0) { - close(tcp_sock_init_lo[port][V6]); - tcp_sock_init_lo[port][V6] = -1; - } continue; }
diff --git a/tcp_splice.c b/tcp_splice.c index 7007501..30d49d4 100644 --- a/tcp_splice.c +++ b/tcp_splice.c @@ -502,19 +502,35 @@ static void tcp_splice_dir(struct tcp_splice_conn *conn, int ref_sock, }
/** - * tcp_splice_conn_from_sock() - Initialize state for spliced connection + * tcp_splice_conn_from_sock() - Attempt to init state for a spliced connection * @c: Execution context * @ref: epoll reference of listening socket * @conn: connection structure to initialize * @s: Accepted socket + * @sa: Peer address of connection * + * Return: true if able to create a spliced connection, false otherwise * #syscalls:pasta setsockopt */ -void tcp_splice_conn_from_sock(struct ctx *c, union epoll_ref ref, - struct tcp_splice_conn *conn, int s) +bool tcp_splice_conn_from_sock(struct ctx *c, union epoll_ref ref, + struct tcp_splice_conn *conn, int s, + const struct sockaddr *sa) { assert(c->mode == MODE_PASTA);
+ if (ref.r.p.tcp.tcp.v6) { + const struct sockaddr_in6 *sa6 + = (const struct sockaddr_in6 *)sa;
Maybe you could split declaration and assignment here.
Good idea, done.
+ if (!IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr)) + return false; + conn->flags = SPLICE_V6; + } else { + const struct sockaddr_in *sa4 = (const struct sockaddr_in *)sa; + if (!IN4_IS_ADDR_LOOPBACK(&sa4->sin_addr)) + return false; + conn->flags = 0; + } + if (setsockopt(s, SOL_TCP, TCP_QUICKACK, &((int){ 1 }), sizeof(int))) { trace("TCP (spliced): failed to set TCP_QUICKACK on %i", @@ -524,11 +540,12 @@ void tcp_splice_conn_from_sock(struct ctx *c, union epoll_ref ref, conn->c.spliced = true; c->tcp.splice_conn_count++; conn->a = s; - conn->flags = ref.r.p.tcp.tcp.v6 ? SPLICE_V6 : 0;
if (tcp_splice_new(c, conn, ref.r.p.tcp.tcp.index, ref.r.p.tcp.tcp.outbound)) conn_flag(c, conn, CLOSING); + + return true; }
/** diff --git a/tcp_splice.h b/tcp_splice.h index f9462ae..1a915dd 100644 --- a/tcp_splice.h +++ b/tcp_splice.h @@ -10,8 +10,9 @@ struct tcp_splice_conn;
void tcp_sock_handler_splice(struct ctx *c, union epoll_ref ref, uint32_t events); -void tcp_splice_conn_from_sock(struct ctx *c, union epoll_ref ref, - struct tcp_splice_conn *conn, int s); +bool tcp_splice_conn_from_sock(struct ctx *c, union epoll_ref ref, + struct tcp_splice_conn *conn, int s, + const struct sockaddr *sa); void tcp_splice_init(struct ctx *c);
#endif /* TCP_SPLICE_H */
-- David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Currently the epoll reference for tcp sockets includes a bit indicating
whether the socket maps to a spliced connection. However, the reference
also has the index of the connection structure which also indicates whether
it is spliced. We can therefore avoid the splice bit in the epoll_ref by
unifying the first part of the non-spliced and spliced handlers where we
look up the connection state.
Signed-off-by: David Gibson
Currently when we insert a connection into the hash table, we store its
bucket number so we can find it when removing entries. However, we can
recompute the hash value from other contents of the structure so we don't
need to store it. This brings the size of tcp_tap_conn down to 64 bytes
again, which means it will fit in a single cacheline on common machines.
This change also removes a non-obvious constraint that the hash table have
less than twice TCP_MAX_CONNS buckets, because of the way
TCP_HASH_BUCKET_BITS was constructed.
Signed-off-by: David Gibson
struct tcp_conn stores an address which could be IPv6 or IPv4 using a
union. We can do this without an additional tag by encoding IPv4 addresses
as IPv4-mapped IPv6 addresses.
This approach is useful wider than the specific place in tcp_conn, so
expose a new 'union inany_addr' like this from a new inany.h. Along with
that create a number of helper functions to make working with these "inany"
addresses easier.
Signed-off-by: David Gibson
[Reviewed until 25/32 so far]
On Wed, 16 Nov 2022 15:41:59 +1100
David Gibson
struct tcp_conn stores an address which could be IPv6 or IPv4 using a union. We can do this without an additional tag by encoding IPv4 addresses as IPv4-mapped IPv6 addresses.
This approach is useful wider than the specific place in tcp_conn, so expose a new 'union inany_addr' like this from a new inany.h. Along with that create a number of helper functions to make working with these "inany" addresses easier.
Signed-off-by: David Gibson
--- Makefile | 6 ++-- inany.h | 68 ++++++++++++++++++++++++++++++++++++++++ tcp.c | 88 +++++++++++++++++++++++++--------------------------- tcp_conn.h | 15 ++------- tcp_splice.c | 1 + 5 files changed, 117 insertions(+), 61 deletions(-) create mode 100644 inany.h diff --git a/Makefile b/Makefile index 9046b0b..ca453aa 100644 --- a/Makefile +++ b/Makefile @@ -44,9 +44,9 @@ SRCS = $(PASST_SRCS) $(QRAP_SRCS) MANPAGES = passt.1 pasta.1 qrap.1
PASST_HEADERS = arch.h arp.h checksum.h conf.h dhcp.h dhcpv6.h icmp.h \ - isolation.h lineread.h log.h ndp.h netlink.h packet.h passt.h pasta.h \ - pcap.h port_fwd.h siphash.h tap.h tcp.h tcp_conn.h tcp_splice.h udp.h \ - util.h + inany.h isolation.h lineread.h log.h ndp.h netlink.h packet.h passt.h \ + pasta.h pcap.h port_fwd.h siphash.h tap.h tcp.h tcp_conn.h \ + tcp_splice.h udp.h util.h HEADERS = $(PASST_HEADERS) seccomp.h
# On gcc 11 and 12, with -O2 and -flto, tcp_hash() and siphash_20b(), if diff --git a/inany.h b/inany.h new file mode 100644 index 0000000..4e53da9 --- /dev/null +++ b/inany.h @@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: AGPL-3.0-or-later + * Copyright Red Hat + * Author: David Gibson
+ * + * inany.h - Types and helpers for handling addresses which could be + * IPv6 or IPv4 (encoded as IPv4-mapped IPv6 addresses) + */ + +#include + +/** union inany_addr - Represents either an IPv4 or IPv6 address + * @a6: Address as an IPv6 address, may be IPv4-mapped + * @_v4._zero: All zero-bits for an IPv4 address + * @_v4._one: All one-bits for an IPv4 address + * @_v4.a4: If @a6 is an IPv4 mapped address, this is the raw IPv4 address + * + * Fields starting with _ shouldn't be accessed except via helpers. + */ +union inany_addr { + struct in6_addr a6; + struct { + uint8_t _zero[10]; + uint8_t _one[2]; + struct in_addr a4; + } _v4mapped;
I'm not sure the extra _ are really worth it. I mean, that's not really enforceable, so saying that v4mapped should only be accessed by helpers should be equivalent.
+}; + +/** inany_v4 - Extract IPv4 address, if present, from IPv[46] address + * @addr: IPv4 or IPv6 address + * + * Return: IPv4 address if @addr is IPv4, NULL otherwise + */ +static inline const struct in_addr *inany_v4(const union inany_addr *addr) +{ + if (!IN6_IS_ADDR_V4MAPPED(&addr->a6)) + return NULL; + return &addr->_v4mapped.a4; +} + +/** inany_equals - Compare two IPv[46] addresses + * @a, @b: IPv[46] addresses + * + * Return: true if @a and @b are the same address + */ +static inline bool inany_equals(const union inany_addr *a, + const union inany_addr *b) +{ + return IN6_ARE_ADDR_EQUAL(&a->a6, &b->a6); +} + +/** inany_from_af - Set IPv[46] address from IPv4 or IPv6 address + * @aa: Pointer to store IPv[46] address + * @af: Address family of @addr + * @addr: struct in_addr (IPv4) or struct in6_addr (IPv6) + */ +static inline void inany_from_af(union inany_addr *aa, int af, const void *addr) +{ + if (af == AF_INET6) { + aa->a6 = *((struct in6_addr *)addr); + } else if (af == AF_INET) { + memset(&aa->_v4mapped._zero, 0, sizeof(aa->_v4mapped._zero)); + memset(&aa->_v4mapped._one, 0xff, sizeof(aa->_v4mapped._one)); + aa->_v4mapped.a4 = *((struct in_addr *)addr); + } else { + /* Not valid to call with other address families */ + assert(0); + } +} diff --git a/tcp.c b/tcp.c index 7686766..4040198 100644 --- a/tcp.c +++ b/tcp.c @@ -301,6 +301,7 @@ #include "conf.h" #include "tcp_splice.h" #include "log.h" +#include "inany.h"
#include "tcp_conn.h"
@@ -404,7 +405,7 @@ struct tcp6_l2_head { /* For MSS6 macro: keep in sync with tcp6_l2_buf_t */ #define OPT_SACK 5 #define OPT_TS 8
-#define CONN_V4(conn) IN6_IS_ADDR_V4MAPPED(&conn->a.a6) +#define CONN_V4(conn) (!!inany_v4(&(conn)->addr)) #define CONN_V6(conn) (!CONN_V4(conn)) #define CONN_IS_CLOSING(conn) \ ((conn->events & ESTABLISHED) && \ @@ -438,7 +439,7 @@ static int tcp_sock_init_ext [NUM_PORTS][IP_VERSIONS]; static int tcp_sock_ns [NUM_PORTS][IP_VERSIONS];
/* Table of destinations with very low RTT (assumed to be local), LRU */ -static struct in6_addr low_rtt_dst[LOW_RTT_TABLE_SIZE]; +static union inany_addr low_rtt_dst[LOW_RTT_TABLE_SIZE];
/* Static buffers */
@@ -861,7 +862,7 @@ static int tcp_rtt_dst_low(const struct tcp_tap_conn *conn) int i;
for (i = 0; i < LOW_RTT_TABLE_SIZE; i++) - if (IN6_ARE_ADDR_EQUAL(&conn->a.a6, low_rtt_dst + i)) + if (inany_equals(&conn->addr, low_rtt_dst + i)) return 1;
return 0; @@ -883,7 +884,7 @@ static void tcp_rtt_dst_check(const struct tcp_tap_conn *conn, return;
for (i = 0; i < LOW_RTT_TABLE_SIZE; i++) { - if (IN6_ARE_ADDR_EQUAL(&conn->a.a6, low_rtt_dst + i)) + if (inany_equals(&conn->addr, low_rtt_dst + i)) return; if (hole == -1 && IN6_IS_ADDR_UNSPECIFIED(low_rtt_dst + i)) hole = i; @@ -895,10 +896,10 @@ static void tcp_rtt_dst_check(const struct tcp_tap_conn *conn, if (hole == -1) return;
- memcpy(low_rtt_dst + hole++, &conn->a.a6, sizeof(conn->a.a6)); + low_rtt_dst[hole++] = conn->addr; if (hole == LOW_RTT_TABLE_SIZE) hole = 0; - memcpy(low_rtt_dst + hole, &in6addr_any, sizeof(conn->a.a6)); + inany_from_af(low_rtt_dst + hole, AF_INET6, &in6addr_any); #else (void)conn; (void)tinfo; @@ -1187,13 +1188,14 @@ static int tcp_hash_match(const struct tcp_tap_conn *conn, int af, const void *addr, in_port_t tap_port, in_port_t sock_port) { - if (af == AF_INET && CONN_V4(conn) && - !memcmp(&conn->a.a4.a, addr, sizeof(conn->a.a4.a)) && + const struct in_addr *a4 = inany_v4(&conn->addr); + + if (af == AF_INET && a4 && !memcmp(a4, addr, sizeof(*a4)) && conn->tap_port == tap_port && conn->sock_port == sock_port) return 1;
if (af == AF_INET6 && - IN6_ARE_ADDR_EQUAL(&conn->a.a6, addr) && + IN6_ARE_ADDR_EQUAL(&conn->addr.a6, addr) && conn->tap_port == tap_port && conn->sock_port == sock_port) return 1;
Note to self or other reviewers: switch to inany_equals() in 22/32. -- Stefano
On Thu, Nov 17, 2022 at 12:54:08AM +0100, Stefano Brivio wrote:
[Reviewed until 25/32 so far]
On Wed, 16 Nov 2022 15:41:59 +1100 David Gibson
wrote: struct tcp_conn stores an address which could be IPv6 or IPv4 using a union. We can do this without an additional tag by encoding IPv4 addresses as IPv4-mapped IPv6 addresses.
This approach is useful wider than the specific place in tcp_conn, so expose a new 'union inany_addr' like this from a new inany.h. Along with that create a number of helper functions to make working with these "inany" addresses easier.
Signed-off-by: David Gibson
--- Makefile | 6 ++-- inany.h | 68 ++++++++++++++++++++++++++++++++++++++++ tcp.c | 88 +++++++++++++++++++++++++--------------------------- tcp_conn.h | 15 ++------- tcp_splice.c | 1 + 5 files changed, 117 insertions(+), 61 deletions(-) create mode 100644 inany.h diff --git a/Makefile b/Makefile index 9046b0b..ca453aa 100644 --- a/Makefile +++ b/Makefile @@ -44,9 +44,9 @@ SRCS = $(PASST_SRCS) $(QRAP_SRCS) MANPAGES = passt.1 pasta.1 qrap.1
PASST_HEADERS = arch.h arp.h checksum.h conf.h dhcp.h dhcpv6.h icmp.h \ - isolation.h lineread.h log.h ndp.h netlink.h packet.h passt.h pasta.h \ - pcap.h port_fwd.h siphash.h tap.h tcp.h tcp_conn.h tcp_splice.h udp.h \ - util.h + inany.h isolation.h lineread.h log.h ndp.h netlink.h packet.h passt.h \ + pasta.h pcap.h port_fwd.h siphash.h tap.h tcp.h tcp_conn.h \ + tcp_splice.h udp.h util.h HEADERS = $(PASST_HEADERS) seccomp.h
# On gcc 11 and 12, with -O2 and -flto, tcp_hash() and siphash_20b(), if diff --git a/inany.h b/inany.h new file mode 100644 index 0000000..4e53da9 --- /dev/null +++ b/inany.h @@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: AGPL-3.0-or-later + * Copyright Red Hat + * Author: David Gibson
+ * + * inany.h - Types and helpers for handling addresses which could be + * IPv6 or IPv4 (encoded as IPv4-mapped IPv6 addresses) + */ + +#include + +/** union inany_addr - Represents either an IPv4 or IPv6 address + * @a6: Address as an IPv6 address, may be IPv4-mapped + * @_v4._zero: All zero-bits for an IPv4 address + * @_v4._one: All one-bits for an IPv4 address + * @_v4.a4: If @a6 is an IPv4 mapped address, this is the raw IPv4 address + * + * Fields starting with _ shouldn't be accessed except via helpers. + */ +union inany_addr { + struct in6_addr a6; + struct { + uint8_t _zero[10]; + uint8_t _one[2]; + struct in_addr a4; + } _v4mapped; I'm not sure the extra _ are really worth it. I mean, that's not really enforceable, so saying that v4mapped should only be accessed by helpers should be equivalent.
Fair call. Adjusted.
+}; + +/** inany_v4 - Extract IPv4 address, if present, from IPv[46] address + * @addr: IPv4 or IPv6 address + * + * Return: IPv4 address if @addr is IPv4, NULL otherwise + */ +static inline const struct in_addr *inany_v4(const union inany_addr *addr) +{ + if (!IN6_IS_ADDR_V4MAPPED(&addr->a6)) + return NULL; + return &addr->_v4mapped.a4; +} + +/** inany_equals - Compare two IPv[46] addresses + * @a, @b: IPv[46] addresses + * + * Return: true if @a and @b are the same address + */ +static inline bool inany_equals(const union inany_addr *a, + const union inany_addr *b) +{ + return IN6_ARE_ADDR_EQUAL(&a->a6, &b->a6); +} + +/** inany_from_af - Set IPv[46] address from IPv4 or IPv6 address + * @aa: Pointer to store IPv[46] address + * @af: Address family of @addr + * @addr: struct in_addr (IPv4) or struct in6_addr (IPv6) + */ +static inline void inany_from_af(union inany_addr *aa, int af, const void *addr) +{ + if (af == AF_INET6) { + aa->a6 = *((struct in6_addr *)addr); + } else if (af == AF_INET) { + memset(&aa->_v4mapped._zero, 0, sizeof(aa->_v4mapped._zero)); + memset(&aa->_v4mapped._one, 0xff, sizeof(aa->_v4mapped._one)); + aa->_v4mapped.a4 = *((struct in_addr *)addr); + } else { + /* Not valid to call with other address families */ + assert(0); + } +} diff --git a/tcp.c b/tcp.c index 7686766..4040198 100644 --- a/tcp.c +++ b/tcp.c @@ -301,6 +301,7 @@ #include "conf.h" #include "tcp_splice.h" #include "log.h" +#include "inany.h"
#include "tcp_conn.h"
@@ -404,7 +405,7 @@ struct tcp6_l2_head { /* For MSS6 macro: keep in sync with tcp6_l2_buf_t */ #define OPT_SACK 5 #define OPT_TS 8
-#define CONN_V4(conn) IN6_IS_ADDR_V4MAPPED(&conn->a.a6) +#define CONN_V4(conn) (!!inany_v4(&(conn)->addr)) #define CONN_V6(conn) (!CONN_V4(conn)) #define CONN_IS_CLOSING(conn) \ ((conn->events & ESTABLISHED) && \ @@ -438,7 +439,7 @@ static int tcp_sock_init_ext [NUM_PORTS][IP_VERSIONS]; static int tcp_sock_ns [NUM_PORTS][IP_VERSIONS];
/* Table of destinations with very low RTT (assumed to be local), LRU */ -static struct in6_addr low_rtt_dst[LOW_RTT_TABLE_SIZE]; +static union inany_addr low_rtt_dst[LOW_RTT_TABLE_SIZE];
/* Static buffers */
@@ -861,7 +862,7 @@ static int tcp_rtt_dst_low(const struct tcp_tap_conn *conn) int i;
for (i = 0; i < LOW_RTT_TABLE_SIZE; i++) - if (IN6_ARE_ADDR_EQUAL(&conn->a.a6, low_rtt_dst + i)) + if (inany_equals(&conn->addr, low_rtt_dst + i)) return 1;
return 0; @@ -883,7 +884,7 @@ static void tcp_rtt_dst_check(const struct tcp_tap_conn *conn, return;
for (i = 0; i < LOW_RTT_TABLE_SIZE; i++) { - if (IN6_ARE_ADDR_EQUAL(&conn->a.a6, low_rtt_dst + i)) + if (inany_equals(&conn->addr, low_rtt_dst + i)) return; if (hole == -1 && IN6_IS_ADDR_UNSPECIFIED(low_rtt_dst + i)) hole = i; @@ -895,10 +896,10 @@ static void tcp_rtt_dst_check(const struct tcp_tap_conn *conn, if (hole == -1) return;
- memcpy(low_rtt_dst + hole++, &conn->a.a6, sizeof(conn->a.a6)); + low_rtt_dst[hole++] = conn->addr; if (hole == LOW_RTT_TABLE_SIZE) hole = 0; - memcpy(low_rtt_dst + hole, &in6addr_any, sizeof(conn->a.a6)); + inany_from_af(low_rtt_dst + hole, AF_INET6, &in6addr_any); #else (void)conn; (void)tinfo; @@ -1187,13 +1188,14 @@ static int tcp_hash_match(const struct tcp_tap_conn *conn, int af, const void *addr, in_port_t tap_port, in_port_t sock_port) { - if (af == AF_INET && CONN_V4(conn) && - !memcmp(&conn->a.a4.a, addr, sizeof(conn->a.a4.a)) && + const struct in_addr *a4 = inany_v4(&conn->addr); + + if (af == AF_INET && a4 && !memcmp(a4, addr, sizeof(*a4)) && conn->tap_port == tap_port && conn->sock_port == sock_port) return 1;
if (af == AF_INET6 && - IN6_ARE_ADDR_EQUAL(&conn->a.a6, addr) && + IN6_ARE_ADDR_EQUAL(&conn->addr.a6, addr) && conn->tap_port == tap_port && conn->sock_port == sock_port) return 1;
Note to self or other reviewers: switch to inany_equals() in 22/32.
-- David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
In the tcp_conn structure, we represent the address with an inany_addr
which could be an IPv4 or IPv6 address. However, we have different paths
which will calculate different hashes for IPv4 and equivalent IPv4-mapped
IPv6 addresses. This will cause problems for some future changes.
Make the hash function work the same for these two cases, by taking an
inany_addr directly. Since this represents IPv4 and IPv4-mapped IPv6
addresses the same way, it will trivially hash the same for both cases.
Callers are changed to construct an inany_addr from whatever they have.
Some of that will be elided in later changes.
Signed-off-by: David Gibson
tcp_hash_insert() takes an address to control which hash bucket the
connection will go into. However, an inany_addr representation of that
address is already stored in struct tcp_conn.
Now that we've made the hashing of IPv4 and IPv4-mapped IPv6 addresses
equivalent, we can simplify tcp_hash_insert() to use the address in
struct tcp_conn, rather than taking it as an extra parameter.
Signed-off-by: David Gibson
tcp_hash_match() can take either an IPv4 (struct in_addr) or IPv6 (struct
in6_addr) address. It has two different paths for each of those cases.
However, its only caller has already constructed an equivalent inany
representation of the address, so we can have tcp_hash_match take that
directly and use a simpler comparison with the inany_equals() helper.
Signed-off-by: David Gibson
tcp_seq_init() has separate paths for IPv4 and IPv6 addresses, which means
we will calculate different sequence numbers for IPv4 and equivalent
IPv4-mapped IPv6 addresses.
Change it to treat these the same by always converting the input address
into an inany_addr representation and use that to calculate the sequence
number.
Signed-off-by: David Gibson
tcp_seq_init() takes a number of parameters for the connection, but at
every call site, these are already populated in the tcp_conn structure.
Likewise we always store the result into the @seq_to_tap field.
Use this to simplify tcp_seq_init().
Signed-off-by: David Gibson
It looks like tcp_seq_init() is supposed to advance the sequence number
by one every 32ns. However we only right shift the ns part of the timespec
not the seconds part, meaning that we'll advance by an extra 32 steps on
each second.
I don't know if that's exploitable in any way, but it doesn't appear to be
the intent, nor what RFC 6528 suggests.
In addition, we convert from seconds to nanoseconds with a multiplication
by '1E9'. In C '1E9' is a floating point constant, forcing a conversion
to floating point and back for what should be an integer calculation
(confirmed with objdump and Makefile default compiler flags). Spell out
1000000000 in full to avoid that.
Signed-off-by: David Gibson
This bit in the TCP specific epoll reference indicates whether the
connection is IPv6 or IPv4. However the sites which refer to it are
already calling accept() which (optionally) returns an address for the
remote end of the connection. We can use the sa_family field in that
address to determine the connection type independent of the epoll
reference.
This does have a cost: for the spliced case, it means we now need to get
that address from accept() which introduces an extran copy_to_user().
However, in future we want to allow handling IPv4 connectons through IPv6
sockets, which means we won't be able to determine the IP version at the
time we create the listening socket and epoll reference. So, at some point
we'll have to pay this cost anyway.
Signed-off-by: David Gibson
On Wed, 16 Nov 2022 15:42:06 +1100
David Gibson
This bit in the TCP specific epoll reference indicates whether the connection is IPv6 or IPv4. However the sites which refer to it are already calling accept() which (optionally) returns an address for the remote end of the connection. We can use the sa_family field in that address to determine the connection type independent of the epoll reference.
This does have a cost: for the spliced case, it means we now need to get that address from accept() which introduces an extran copy_to_user(). However, in future we want to allow handling IPv4 connectons through IPv6 sockets, which means we won't be able to determine the IP version at the time we create the listening socket and epoll reference. So, at some point we'll have to pay this cost anyway.
Signed-off-by: David Gibson
--- tcp.c | 10 ++++------ tcp.h | 2 -- tcp_splice.c | 9 ++++----- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/tcp.c b/tcp.c index 0513b3b..b05ed6c 100644 --- a/tcp.c +++ b/tcp.c @@ -662,8 +662,7 @@ static int tcp_epoll_ctl(const struct ctx *c, struct tcp_tap_conn *conn) { int m = conn->c.in_epoll ? EPOLL_CTL_MOD : EPOLL_CTL_ADD; union epoll_ref ref = { .r.proto = IPPROTO_TCP, .r.s = conn->sock, - .r.p.tcp.tcp.index = CONN_IDX(conn), - .r.p.tcp.tcp.v6 = CONN_V6(conn) }; + .r.p.tcp.tcp.index = CONN_IDX(conn) }; struct epoll_event ev = { .data.u64 = ref.u64 };
if (conn->events == CLOSED) { @@ -2745,7 +2744,7 @@ static void tcp_tap_conn_from_sock(struct ctx *c, union epoll_ref ref, conn->ws_to_tap = conn->ws_from_tap = 0; conn_event(c, conn, SOCK_ACCEPTED);
- if (ref.r.p.tcp.tcp.v6) { + if (sa->sa_family == AF_INET6) { struct sockaddr_in6 sa6;
memcpy(&sa6, sa, sizeof(sa6)); @@ -3019,8 +3018,7 @@ static void tcp_sock_init6(const struct ctx *c, in_port_t port) { in_port_t idx = port + c->tcp.fwd_in.delta[port]; - union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.v6 = 1, - .tcp.index = idx }; + union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.index = idx };
Excess whitespace (from earlier patch).
int s;
s = sock_l4(c, AF_INET6, IPPROTO_TCP, addr, ifname, port, tref.u32); @@ -3084,7 +3082,7 @@ static void tcp_ns_sock_init6(const struct ctx *c, in_port_t port) { in_port_t idx = port + c->tcp.fwd_out.delta[port]; union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.outbound = 1, - .tcp.v6 = 1, .tcp.index = idx}; + .tcp.index = idx};
Missing whitespace (from earlier patch).
int s;
assert(c->mode == MODE_PASTA); diff --git a/tcp.h b/tcp.h index a940682..739b451 100644 --- a/tcp.h +++ b/tcp.h @@ -33,7 +33,6 @@ void tcp_update_l2_buf(const unsigned char *eth_d, const unsigned char *eth_s, * union tcp_epoll_ref - epoll reference portion for TCP connections * @listen: Set if this file descriptor is a listening socket * @outbound: Listening socket maps to outbound, spliced connection - * @v6: Set for IPv6 sockets or connections * @timer: Reference is a timerfd descriptor for connection * @index: Index of connection in table, or port for bound sockets * @u32: Opaque u32 value of reference @@ -42,7 +41,6 @@ union tcp_epoll_ref { struct { uint32_t listen:1, outbound:1, - v6:1, timer:1, index:20; } tcp; diff --git a/tcp_splice.c b/tcp_splice.c index 30ab0eb..7c2f667 100644 --- a/tcp_splice.c +++ b/tcp_splice.c @@ -167,11 +167,9 @@ static int tcp_splice_epoll_ctl(const struct ctx *c, { int m = conn->c.in_epoll ? EPOLL_CTL_MOD : EPOLL_CTL_ADD; union epoll_ref ref_a = { .r.proto = IPPROTO_TCP, .r.s = conn->a, - .r.p.tcp.tcp.index = CONN_IDX(conn), - .r.p.tcp.tcp.v6 = CONN_V6(conn) }; + .r.p.tcp.tcp.index = CONN_IDX(conn) }; union epoll_ref ref_b = { .r.proto = IPPROTO_TCP, .r.s = conn->b, - .r.p.tcp.tcp.index = CONN_IDX(conn), - .r.p.tcp.tcp.v6 = CONN_V6(conn) }; + .r.p.tcp.tcp.index = CONN_IDX(conn) }; struct epoll_event ev_a = { .data.u64 = ref_a.u64 }; struct epoll_event ev_b = { .data.u64 = ref_b.u64 }; uint32_t events_a, events_b; @@ -504,6 +502,7 @@ static void tcp_splice_dir(struct tcp_splice_conn *conn, int ref_sock, * tcp_splice_conn_from_sock() - Attempt to init state for a spliced connection * @c: Execution context * @ref: epoll reference of listening socket + * @ipv6: Should this be an IPv6 connection?
Left-over from previous idea I guess.
* @conn: connection structure to initialize * @s: Accepted socket * @sa: Peer address of connection @@ -517,7 +516,7 @@ bool tcp_splice_conn_from_sock(struct ctx *c, union epoll_ref ref, { assert(c->mode == MODE_PASTA);
- if (ref.r.p.tcp.tcp.v6) { + if (sa->sa_family == AF_INET6) { const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa; if (!IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
-- Stefano
On Thu, Nov 17, 2022 at 01:15:11AM +0100, Stefano Brivio wrote:
On Wed, 16 Nov 2022 15:42:06 +1100 David Gibson
wrote: This bit in the TCP specific epoll reference indicates whether the connection is IPv6 or IPv4. However the sites which refer to it are already calling accept() which (optionally) returns an address for the remote end of the connection. We can use the sa_family field in that address to determine the connection type independent of the epoll reference.
This does have a cost: for the spliced case, it means we now need to get that address from accept() which introduces an extran copy_to_user(). However, in future we want to allow handling IPv4 connectons through IPv6 sockets, which means we won't be able to determine the IP version at the time we create the listening socket and epoll reference. So, at some point we'll have to pay this cost anyway.
Signed-off-by: David Gibson
--- tcp.c | 10 ++++------ tcp.h | 2 -- tcp_splice.c | 9 ++++----- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/tcp.c b/tcp.c index 0513b3b..b05ed6c 100644 --- a/tcp.c +++ b/tcp.c @@ -662,8 +662,7 @@ static int tcp_epoll_ctl(const struct ctx *c, struct tcp_tap_conn *conn) { int m = conn->c.in_epoll ? EPOLL_CTL_MOD : EPOLL_CTL_ADD; union epoll_ref ref = { .r.proto = IPPROTO_TCP, .r.s = conn->sock, - .r.p.tcp.tcp.index = CONN_IDX(conn), - .r.p.tcp.tcp.v6 = CONN_V6(conn) }; + .r.p.tcp.tcp.index = CONN_IDX(conn) }; struct epoll_event ev = { .data.u64 = ref.u64 };
if (conn->events == CLOSED) { @@ -2745,7 +2744,7 @@ static void tcp_tap_conn_from_sock(struct ctx *c, union epoll_ref ref, conn->ws_to_tap = conn->ws_from_tap = 0; conn_event(c, conn, SOCK_ACCEPTED);
- if (ref.r.p.tcp.tcp.v6) { + if (sa->sa_family == AF_INET6) { struct sockaddr_in6 sa6;
memcpy(&sa6, sa, sizeof(sa6)); @@ -3019,8 +3018,7 @@ static void tcp_sock_init6(const struct ctx *c, in_port_t port) { in_port_t idx = port + c->tcp.fwd_in.delta[port]; - union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.v6 = 1, - .tcp.index = idx }; + union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.index = idx };
Excess whitespace (from earlier patch).
Fixed
int s;
s = sock_l4(c, AF_INET6, IPPROTO_TCP, addr, ifname, port, tref.u32); @@ -3084,7 +3082,7 @@ static void tcp_ns_sock_init6(const struct ctx *c, in_port_t port) { in_port_t idx = port + c->tcp.fwd_out.delta[port]; union tcp_epoll_ref tref = { .tcp.listen = 1, .tcp.outbound = 1, - .tcp.v6 = 1, .tcp.index = idx}; + .tcp.index = idx};
Missing whitespace (from earlier patch).
Fixed.
int s;
assert(c->mode == MODE_PASTA); diff --git a/tcp.h b/tcp.h index a940682..739b451 100644 --- a/tcp.h +++ b/tcp.h @@ -33,7 +33,6 @@ void tcp_update_l2_buf(const unsigned char *eth_d, const unsigned char *eth_s, * union tcp_epoll_ref - epoll reference portion for TCP connections * @listen: Set if this file descriptor is a listening socket * @outbound: Listening socket maps to outbound, spliced connection - * @v6: Set for IPv6 sockets or connections * @timer: Reference is a timerfd descriptor for connection * @index: Index of connection in table, or port for bound sockets * @u32: Opaque u32 value of reference @@ -42,7 +41,6 @@ union tcp_epoll_ref { struct { uint32_t listen:1, outbound:1, - v6:1, timer:1, index:20; } tcp; diff --git a/tcp_splice.c b/tcp_splice.c index 30ab0eb..7c2f667 100644 --- a/tcp_splice.c +++ b/tcp_splice.c @@ -167,11 +167,9 @@ static int tcp_splice_epoll_ctl(const struct ctx *c, { int m = conn->c.in_epoll ? EPOLL_CTL_MOD : EPOLL_CTL_ADD; union epoll_ref ref_a = { .r.proto = IPPROTO_TCP, .r.s = conn->a, - .r.p.tcp.tcp.index = CONN_IDX(conn), - .r.p.tcp.tcp.v6 = CONN_V6(conn) }; + .r.p.tcp.tcp.index = CONN_IDX(conn) }; union epoll_ref ref_b = { .r.proto = IPPROTO_TCP, .r.s = conn->b, - .r.p.tcp.tcp.index = CONN_IDX(conn), - .r.p.tcp.tcp.v6 = CONN_V6(conn) }; + .r.p.tcp.tcp.index = CONN_IDX(conn) }; struct epoll_event ev_a = { .data.u64 = ref_a.u64 }; struct epoll_event ev_b = { .data.u64 = ref_b.u64 }; uint32_t events_a, events_b; @@ -504,6 +502,7 @@ static void tcp_splice_dir(struct tcp_splice_conn *conn, int ref_sock, * tcp_splice_conn_from_sock() - Attempt to init state for a spliced connection * @c: Execution context * @ref: epoll reference of listening socket + * @ipv6: Should this be an IPv6 connection?
Left-over from previous idea I guess.
Yes indeed, fixed.
* @conn: connection structure to initialize * @s: Accepted socket * @sa: Peer address of connection @@ -517,7 +516,7 @@ bool tcp_splice_conn_from_sock(struct ctx *c, union epoll_ref ref, { assert(c->mode == MODE_PASTA);
- if (ref.r.p.tcp.tcp.v6) { + if (sa->sa_family == AF_INET6) { const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa; if (!IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr))
-- David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
passt usually doesn't NAT, but it does do so for the remapping of the
gateway address to refer to the host. Currently we perform this NAT with
slightly different rules on both IPv4 addresses and IPv6 addresses, but not
on IPv4-mapped IPv6 addresses. This means we won't correctly handle the
case of an IPv4 connection over an IPv6 socket, which is possible on Linux
(and probably other platforms).
Refactor tcp_conn_from_sock() to perform the NAT after converting either
address family into an inany_addr, so IPv4 and and IPv4-mapped addresses
have the same representation.
With two new helpers this lets us remove the IPv4 and IPv6 specific paths
from tcp_conn_from_sock().
Signed-off-by: David Gibson
On Wed, 16 Nov 2022 15:42:07 +1100
David Gibson
passt usually doesn't NAT, but it does do so for the remapping of the gateway address to refer to the host. Currently we perform this NAT with slightly different rules on both IPv4 addresses and IPv6 addresses, but not on IPv4-mapped IPv6 addresses. This means we won't correctly handle the case of an IPv4 connection over an IPv6 socket, which is possible on Linux (and probably other platforms).
By the way, I really think it's just Linux, I can't think of other examples.
Refactor tcp_conn_from_sock() to perform the NAT after converting either address family into an inany_addr, so IPv4 and and IPv4-mapped addresses have the same representation.
With two new helpers this lets us remove the IPv4 and IPv6 specific paths from tcp_conn_from_sock().
Signed-off-by: David Gibson
--- inany.h | 30 ++++++++++++++++++++++++++-- tcp.c | 62 ++++++++++++++++++++++++--------------------------------- 2 files changed, 54 insertions(+), 38 deletions(-) diff --git a/inany.h b/inany.h index 4e53da9..a677aa7 100644 --- a/inany.h +++ b/inany.h @@ -30,11 +30,11 @@ union inany_addr { * * Return: IPv4 address if @addr is IPv4, NULL otherwise */ -static inline const struct in_addr *inany_v4(const union inany_addr *addr) +static inline struct in_addr *inany_v4(const union inany_addr *addr)
There must be a reason, but I can't understand why this change is needed here.
{ if (!IN6_IS_ADDR_V4MAPPED(&addr->a6)) return NULL; - return &addr->_v4mapped.a4; + return (struct in_addr *)&addr->_v4mapped.a4; }
/** inany_equals - Compare two IPv[46] addresses @@ -66,3 +66,29 @@ static inline void inany_from_af(union inany_addr *aa, int af, const void *addr) assert(0); } } + +/** inany_from_sockaddr - Extract IPv[46] address and port number from sockaddr + * @a: Pointer to store IPv[46] address
This is aa below, I'm not sure why.
+ * @port: Pointer to store port number, host order + * @addr: struct sockaddr_in (IPv4) or struct sockaddr_in6 (IPv6)
This became sa_ (needless to say, addr would make more sense).
+ */ +static inline void inany_from_sockaddr(union inany_addr *aa, in_port_t *port, + const void *sa_) +{ + const struct sockaddr *sa = (const struct sockaddr *)sa_; + + if (sa->sa_family == AF_INET6) { + struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa; + + inany_from_af(aa, AF_INET6, &sa6->sin6_addr); + *port = ntohs(sa6->sin6_port); + } else if (sa->sa_family == AF_INET) { + struct sockaddr_in *sa4 = (struct sockaddr_in *)sa; + + inany_from_af(aa, AF_INET, &sa4->sin_addr); + *port = ntohs(sa4->sin_port); + } else { + /* Not valid to call with other address families */ + assert(0); + } +} diff --git a/tcp.c b/tcp.c index b05ed6c..fca5df4 100644 --- a/tcp.c +++ b/tcp.c @@ -2724,6 +2724,29 @@ static void tcp_connect_finish(struct ctx *c, struct tcp_tap_conn *conn) conn_flag(c, conn, ACK_FROM_TAP_DUE); }
+static void tcp_snat_inbound(const struct ctx *c, union inany_addr *addr)
What this does is kind of obvious, still a comment would be nice.
+{ + struct in_addr *addr4 = inany_v4(addr); + + if (addr4) { + if (IN4_IS_ADDR_LOOPBACK(addr4) || + IN4_IS_ADDR_UNSPECIFIED(addr4) || + IN4_ARE_ADDR_EQUAL(addr4, &c->ip4.addr_seen)) + *addr4 = c->ip4.gw; + } else { + struct in6_addr *addr6 = &addr->a6; + + if (IN6_IS_ADDR_LOOPBACK(addr6) || + IN6_ARE_ADDR_EQUAL(addr6, &c->ip6.addr_seen) || + IN6_ARE_ADDR_EQUAL(addr6, &c->ip6.addr)) { + if (IN6_IS_ADDR_LINKLOCAL(&c->ip6.gw)) + *addr6 = c->ip6.gw; + else + *addr6 = c->ip6.addr_ll; + } + } +} + /** * tcp_tap_conn_from_sock() - Initialize state for non-spliced connection * @c: Execution context @@ -2744,43 +2767,10 @@ static void tcp_tap_conn_from_sock(struct ctx *c, union epoll_ref ref, conn->ws_to_tap = conn->ws_from_tap = 0; conn_event(c, conn, SOCK_ACCEPTED);
- if (sa->sa_family == AF_INET6) { - struct sockaddr_in6 sa6; - - memcpy(&sa6, sa, sizeof(sa6)); - - if (IN6_IS_ADDR_LOOPBACK(&sa6.sin6_addr) || - IN6_ARE_ADDR_EQUAL(&sa6.sin6_addr, &c->ip6.addr_seen) || - IN6_ARE_ADDR_EQUAL(&sa6.sin6_addr, &c->ip6.addr)) { - struct in6_addr *src; + inany_from_sockaddr(&conn->addr, &conn->sock_port, sa); + conn->tap_port = ref.r.p.tcp.tcp.index;
- if (IN6_IS_ADDR_LINKLOCAL(&c->ip6.gw)) - src = &c->ip6.gw; - else - src = &c->ip6.addr_ll; - - memcpy(&sa6.sin6_addr, src, sizeof(*src)); - } - - inany_from_af(&conn->addr, AF_INET6, &sa6.sin6_addr); - - conn->sock_port = ntohs(sa6.sin6_port); - conn->tap_port = ref.r.p.tcp.tcp.index; - } else { - struct sockaddr_in sa4; - - memcpy(&sa4, sa, sizeof(sa4)); - - if (IN4_IS_ADDR_LOOPBACK(&sa4.sin_addr) || - IN4_IS_ADDR_UNSPECIFIED(&sa4.sin_addr) || - IN4_ARE_ADDR_EQUAL(&sa4.sin_addr, &c->ip4.addr_seen)) - sa4.sin_addr = c->ip4.gw; - - inany_from_af(&conn->addr, AF_INET, &sa4.sin_addr); - - conn->sock_port = ntohs(sa4.sin_port); - conn->tap_port = ref.r.p.tcp.tcp.index; - } + tcp_snat_inbound(c, &conn->addr);
tcp_seq_init(c, conn, now); tcp_hash_insert(c, conn);
-- Stefano
On Thu, Nov 17, 2022 at 01:15:20AM +0100, Stefano Brivio wrote:
On Wed, 16 Nov 2022 15:42:07 +1100 David Gibson
wrote: passt usually doesn't NAT, but it does do so for the remapping of the gateway address to refer to the host. Currently we perform this NAT with slightly different rules on both IPv4 addresses and IPv6 addresses, but not on IPv4-mapped IPv6 addresses. This means we won't correctly handle the case of an IPv4 connection over an IPv6 socket, which is possible on Linux (and probably other platforms).
By the way, I really think it's just Linux, I can't think of other examples.
Hmm... so descriptions I've seen of the IPv4-mapped IPv6 addresses seem to imply this is the behaviour in a number of systems. e.g. https://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses
Refactor tcp_conn_from_sock() to perform the NAT after converting either address family into an inany_addr, so IPv4 and and IPv4-mapped addresses have the same representation.
With two new helpers this lets us remove the IPv4 and IPv6 specific paths from tcp_conn_from_sock().
Signed-off-by: David Gibson
--- inany.h | 30 ++++++++++++++++++++++++++-- tcp.c | 62 ++++++++++++++++++++++++--------------------------------- 2 files changed, 54 insertions(+), 38 deletions(-) diff --git a/inany.h b/inany.h index 4e53da9..a677aa7 100644 --- a/inany.h +++ b/inany.h @@ -30,11 +30,11 @@ union inany_addr { * * Return: IPv4 address if @addr is IPv4, NULL otherwise */ -static inline const struct in_addr *inany_v4(const union inany_addr *addr) +static inline struct in_addr *inany_v4(const union inany_addr *addr)
There must be a reason, but I can't understand why this change is needed here.
Because in tcp_snat_inbound() we want to modify, not just examine the IPv4 address within the IPv6 address. Ideally the return would be const if and only if the input is, but C can't express that. This appears to be the conventional half-arsed solution (see, e.g. memchr() or strstr()).
{ if (!IN6_IS_ADDR_V4MAPPED(&addr->a6)) return NULL; - return &addr->_v4mapped.a4; + return (struct in_addr *)&addr->_v4mapped.a4; }
/** inany_equals - Compare two IPv[46] addresses @@ -66,3 +66,29 @@ static inline void inany_from_af(union inany_addr *aa, int af, const void *addr) assert(0); } } + +/** inany_from_sockaddr - Extract IPv[46] address and port number from sockaddr + * @a: Pointer to store IPv[46] address
This is aa below, I'm not sure why.
Fixed.
+ * @port: Pointer to store port number, host order + * @addr: struct sockaddr_in (IPv4) or struct sockaddr_in6 (IPv6)
This became sa_ (needless to say, addr would make more sense).
Good call, changed.
+ */ +static inline void inany_from_sockaddr(union inany_addr *aa, in_port_t *port, + const void *sa_) +{ + const struct sockaddr *sa = (const struct sockaddr *)sa_; + + if (sa->sa_family == AF_INET6) { + struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa; + + inany_from_af(aa, AF_INET6, &sa6->sin6_addr); + *port = ntohs(sa6->sin6_port); + } else if (sa->sa_family == AF_INET) { + struct sockaddr_in *sa4 = (struct sockaddr_in *)sa; + + inany_from_af(aa, AF_INET, &sa4->sin_addr); + *port = ntohs(sa4->sin_port); + } else { + /* Not valid to call with other address families */ + assert(0); + } +} diff --git a/tcp.c b/tcp.c index b05ed6c..fca5df4 100644 --- a/tcp.c +++ b/tcp.c @@ -2724,6 +2724,29 @@ static void tcp_connect_finish(struct ctx *c, struct tcp_tap_conn *conn) conn_flag(c, conn, ACK_FROM_TAP_DUE); }
+static void tcp_snat_inbound(const struct ctx *c, union inany_addr *addr)
What this does is kind of obvious, still a comment would be nice.
Good point, added. Especially since I'm hoping to share this with UDP at some later point.
+{ + struct in_addr *addr4 = inany_v4(addr); + + if (addr4) { + if (IN4_IS_ADDR_LOOPBACK(addr4) || + IN4_IS_ADDR_UNSPECIFIED(addr4) || + IN4_ARE_ADDR_EQUAL(addr4, &c->ip4.addr_seen)) + *addr4 = c->ip4.gw; + } else { + struct in6_addr *addr6 = &addr->a6; + + if (IN6_IS_ADDR_LOOPBACK(addr6) || + IN6_ARE_ADDR_EQUAL(addr6, &c->ip6.addr_seen) || + IN6_ARE_ADDR_EQUAL(addr6, &c->ip6.addr)) { + if (IN6_IS_ADDR_LINKLOCAL(&c->ip6.gw)) + *addr6 = c->ip6.gw; + else + *addr6 = c->ip6.addr_ll; + } + } +} + /** * tcp_tap_conn_from_sock() - Initialize state for non-spliced connection * @c: Execution context @@ -2744,43 +2767,10 @@ static void tcp_tap_conn_from_sock(struct ctx *c, union epoll_ref ref, conn->ws_to_tap = conn->ws_from_tap = 0; conn_event(c, conn, SOCK_ACCEPTED);
- if (sa->sa_family == AF_INET6) { - struct sockaddr_in6 sa6; - - memcpy(&sa6, sa, sizeof(sa6)); - - if (IN6_IS_ADDR_LOOPBACK(&sa6.sin6_addr) || - IN6_ARE_ADDR_EQUAL(&sa6.sin6_addr, &c->ip6.addr_seen) || - IN6_ARE_ADDR_EQUAL(&sa6.sin6_addr, &c->ip6.addr)) { - struct in6_addr *src; + inany_from_sockaddr(&conn->addr, &conn->sock_port, sa); + conn->tap_port = ref.r.p.tcp.tcp.index;
- if (IN6_IS_ADDR_LINKLOCAL(&c->ip6.gw)) - src = &c->ip6.gw; - else - src = &c->ip6.addr_ll; - - memcpy(&sa6.sin6_addr, src, sizeof(*src)); - } - - inany_from_af(&conn->addr, AF_INET6, &sa6.sin6_addr); - - conn->sock_port = ntohs(sa6.sin6_port); - conn->tap_port = ref.r.p.tcp.tcp.index; - } else { - struct sockaddr_in sa4; - - memcpy(&sa4, sa, sizeof(sa4)); - - if (IN4_IS_ADDR_LOOPBACK(&sa4.sin_addr) || - IN4_IS_ADDR_UNSPECIFIED(&sa4.sin_addr) || - IN4_ARE_ADDR_EQUAL(&sa4.sin_addr, &c->ip4.addr_seen)) - sa4.sin_addr = c->ip4.gw; - - inany_from_af(&conn->addr, AF_INET, &sa4.sin_addr); - - conn->sock_port = ntohs(sa4.sin_port); - conn->tap_port = ref.r.p.tcp.tcp.index; - } + tcp_snat_inbound(c, &conn->addr);
tcp_seq_init(c, conn, now); tcp_hash_insert(c, conn);
-- David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
For non-spliced connections we now treat IPv4-mapped IPv6 addresses the
same as the corresponding IPv4 addresses. However currently we won't
splice a connection from ::ffff:127.0.0.1 the way we would one from
127.0.0.1. Correct this so that we can splice connections from IPv4
localhost that have been received on an IPv6 dual stack socket.
Signed-off-by: David Gibson
On Wed, 16 Nov 2022 15:42:08 +1100
David Gibson
For non-spliced connections we now treat IPv4-mapped IPv6 addresses the same as the corresponding IPv4 addresses. However currently we won't splice a connection from ::ffff:127.0.0.1 the way we would one from 127.0.0.1. Correct this so that we can splice connections from IPv4 localhost that have been received on an IPv6 dual stack socket.
Signed-off-by: David Gibson
--- tcp_splice.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tcp_splice.c b/tcp_splice.c index 7c2f667..61c56be 100644 --- a/tcp_splice.c +++ b/tcp_splice.c @@ -514,19 +514,23 @@ bool tcp_splice_conn_from_sock(struct ctx *c, union epoll_ref ref, struct tcp_splice_conn *conn, int s, const struct sockaddr *sa) { + union inany_addr aany; + const struct in_addr *a4;
The usual order.
+ in_port_t port;
Is the port actually needed here? I don't see how you use it.
+ assert(c->mode == MODE_PASTA);
- if (sa->sa_family == AF_INET6) { - const struct sockaddr_in6 *sa6 - = (const struct sockaddr_in6 *)sa; - if (!IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr)) + inany_from_sockaddr(&aany, &port, sa); + a4 = inany_v4(&aany); + + if (a4) { + if (!IN4_IS_ADDR_LOOPBACK(a4)) return false; - conn->flags = SPLICE_V6; + conn->flags = 0; } else { - const struct sockaddr_in *sa4 = (const struct sockaddr_in *)sa; - if (!IN4_IS_ADDR_LOOPBACK(&sa4->sin_addr)) + if (!IN6_IS_ADDR_LOOPBACK(&aany.a6)) return false; - conn->flags = 0; + conn->flags = SPLICE_V6; }
if (setsockopt(s, SOL_TCP, TCP_QUICKACK, &((int){ 1 }),
-- Stefano
On Thu, Nov 17, 2022 at 01:15:26AM +0100, Stefano Brivio wrote:
On Wed, 16 Nov 2022 15:42:08 +1100 David Gibson
wrote: For non-spliced connections we now treat IPv4-mapped IPv6 addresses the same as the corresponding IPv4 addresses. However currently we won't splice a connection from ::ffff:127.0.0.1 the way we would one from 127.0.0.1. Correct this so that we can splice connections from IPv4 localhost that have been received on an IPv6 dual stack socket.
Signed-off-by: David Gibson
--- tcp_splice.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tcp_splice.c b/tcp_splice.c index 7c2f667..61c56be 100644 --- a/tcp_splice.c +++ b/tcp_splice.c @@ -514,19 +514,23 @@ bool tcp_splice_conn_from_sock(struct ctx *c, union epoll_ref ref, struct tcp_splice_conn *conn, int s, const struct sockaddr *sa) { + union inany_addr aany; + const struct in_addr *a4;
The usual order.
Fixed.
+ in_port_t port;
Is the port actually needed here? I don't see how you use it.
Well, inany_from_sockaddr() requires a port parameter. I could make it accept NULL, but then I'd have some noise there instead of here. Since inany_from_sockaddr() is an inline, I'm hoping the compiler will be smart enough to elide it anyway. I don't have a strong preference, I can change it if you'd like.
+ assert(c->mode == MODE_PASTA);
- if (sa->sa_family == AF_INET6) { - const struct sockaddr_in6 *sa6 - = (const struct sockaddr_in6 *)sa; - if (!IN6_IS_ADDR_LOOPBACK(&sa6->sin6_addr)) + inany_from_sockaddr(&aany, &port, sa); + a4 = inany_v4(&aany); + + if (a4) { + if (!IN4_IS_ADDR_LOOPBACK(a4)) return false; - conn->flags = SPLICE_V6; + conn->flags = 0; } else { - const struct sockaddr_in *sa4 = (const struct sockaddr_in *)sa; - if (!IN4_IS_ADDR_LOOPBACK(&sa4->sin_addr)) + if (!IN6_IS_ADDR_LOOPBACK(&aany.a6)) return false; - conn->flags = 0; + conn->flags = SPLICE_V6; }
if (setsockopt(s, SOL_TCP, TCP_QUICKACK, &((int){ 1 }),
-- David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Previous cleanups mean that tcp_sock_init4() and tcp_sock_init6() are
almost identical, and the remaining differences can be easily
parameterized. Combine both into a single tcp_sock_init_af() function.
Signed-off-by: David Gibson
Currently, when instructed to open an IPv6 socket, sock_l4() explicitly
sets the IPV6_V6ONLY socket option so that the socket will only respond to
IPv6 connections. Linux (and probably other platforms) allow "dual stack"
sockets: IPv6 sockets which can also accept IPv4 connections.
Extend sock_l4() to be able to make such sockets, by passing AF_UNSPEC as
the address family and no bind address (binding to a specific address would
defeat the purpose). We add a Makefile define 'DUAL_STACK_SOCKETS' to
indicate availability of this feature on the target platform.
Signed-off-by: David Gibson
According to its doc comments, sock_l4() returns -1 on error. It does,
except in one case where it returns -EIO. Fix this inconsistency to match
the docs and always return -1.
Signed-off-by: David Gibson
Platforms like Linux allow IPv6 sockets to listen for IPv4 connections as
well as native IPv6 connections. By doing this we halve the number of
listening sockets we need for TCP (assuming passt/pasta is listening on the
same ports for IPv4 and IPv6). When forwarding many ports (e.g. -t all)
this can significantly reduce the amount of kernel memory that passt
consumes.
When forwarding all TCP and UDP ports for both IPv4 and IPv6 (-t all
-u all), this reduces kernel memory usage from ~677MiB to ~487MiB
(kernel version 6.0.8 on Fedora 37, x86_64).
Signed-off-by: David Gibson
On Wed, 16 Nov 2022 15:42:12 +1100
David Gibson
Platforms like Linux allow IPv6 sockets to listen for IPv4 connections as well as native IPv6 connections. By doing this we halve the number of listening sockets we need for TCP (assuming passt/pasta is listening on the same ports for IPv4 and IPv6). When forwarding many ports (e.g. -t all) this can significantly reduce the amount of kernel memory that passt consumes.
When forwarding all TCP and UDP ports for both IPv4 and IPv6 (-t all -u all), this reduces kernel memory usage from ~677MiB to ~487MiB (kernel version 6.0.8 on Fedora 37, x86_64).
Oh, nice, that's quite significant.
Signed-off-by: David Gibson
--- tcp.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tcp.c b/tcp.c index 616b9d0..5860c9f 100644 --- a/tcp.c +++ b/tcp.c @@ -2991,8 +2991,12 @@ static int tcp_sock_init_af(const struct ctx *c, int af, in_port_t port,
s = sock_l4(c, af, IPPROTO_TCP, addr, ifname, port, tref.u32);
- if (c->tcp.fwd_in.mode == FWD_AUTO) - tcp_sock_init_ext[port][(af == AF_INET) ? V4 : V6] = s; + if (c->tcp.fwd_in.mode == FWD_AUTO) { + if (af == AF_INET || af == AF_UNSPEC) + tcp_sock_init_ext[port][V4] = s; + if (af == AF_INET6 || af == AF_UNSPEC)
Nit: you could align the || af == AF_UNSPEC above with an extra whitespace (as it's done in the context below).
+ tcp_sock_init_ext[port][V6] = s; + }
if (s < 0) return -1; @@ -3012,6 +3016,12 @@ static int tcp_sock_init_af(const struct ctx *c, int af, in_port_t port, void tcp_sock_init(const struct ctx *c, sa_family_t af, const void *addr, const char *ifname, in_port_t port) { + if (af == AF_UNSPEC && c->ifi4 && c->ifi6) + /* Attempt to get a dual stack socket */ + if (tcp_sock_init_af(c, AF_UNSPEC, port, addr, ifname) >= 0) + return; + + /* Otherwise create a socket per IP version */
...this looks surprisingly clean by the way, at least much cleaner than I expected.
if ((af == AF_INET || af == AF_UNSPEC) && c->ifi4) tcp_sock_init_af(c, AF_INET, port, addr, ifname); if ((af == AF_INET6 || af == AF_UNSPEC) && c->ifi6)
I just finished reviewing this series, in general it looks great to me, I would have another look (and test!) on Thursday -- either using this version or a re-spin. -- Stefano
On Thu, Nov 17, 2022 at 01:15:30AM +0100, Stefano Brivio wrote:
On Wed, 16 Nov 2022 15:42:12 +1100 David Gibson
wrote: Platforms like Linux allow IPv6 sockets to listen for IPv4 connections as well as native IPv6 connections. By doing this we halve the number of listening sockets we need for TCP (assuming passt/pasta is listening on the same ports for IPv4 and IPv6). When forwarding many ports (e.g. -t all) this can significantly reduce the amount of kernel memory that passt consumes.
When forwarding all TCP and UDP ports for both IPv4 and IPv6 (-t all -u all), this reduces kernel memory usage from ~677MiB to ~487MiB (kernel version 6.0.8 on Fedora 37, x86_64).
Oh, nice, that's quite significant.
Signed-off-by: David Gibson
--- tcp.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tcp.c b/tcp.c index 616b9d0..5860c9f 100644 --- a/tcp.c +++ b/tcp.c @@ -2991,8 +2991,12 @@ static int tcp_sock_init_af(const struct ctx *c, int af, in_port_t port,
s = sock_l4(c, af, IPPROTO_TCP, addr, ifname, port, tref.u32);
- if (c->tcp.fwd_in.mode == FWD_AUTO) - tcp_sock_init_ext[port][(af == AF_INET) ? V4 : V6] = s; + if (c->tcp.fwd_in.mode == FWD_AUTO) { + if (af == AF_INET || af == AF_UNSPEC) + tcp_sock_init_ext[port][V4] = s; + if (af == AF_INET6 || af == AF_UNSPEC)
Nit: you could align the || af == AF_UNSPEC above with an extra whitespace (as it's done in the context below).
Done.
+ tcp_sock_init_ext[port][V6] = s; + }
if (s < 0) return -1; @@ -3012,6 +3016,12 @@ static int tcp_sock_init_af(const struct ctx *c, int af, in_port_t port, void tcp_sock_init(const struct ctx *c, sa_family_t af, const void *addr, const char *ifname, in_port_t port) { + if (af == AF_UNSPEC && c->ifi4 && c->ifi6) + /* Attempt to get a dual stack socket */ + if (tcp_sock_init_af(c, AF_UNSPEC, port, addr, ifname) >= 0) + return; + + /* Otherwise create a socket per IP version */
...this looks surprisingly clean by the way, at least much cleaner than I expected.
Right. The trick is in realizing that the properties (spliced, IP version) of an established connection don't need to be tied to the properties of the listening socket which created it in the first place.
if ((af == AF_INET || af == AF_UNSPEC) && c->ifi4) tcp_sock_init_af(c, AF_INET, port, addr, ifname); if ((af == AF_INET6 || af == AF_UNSPEC) && c->ifi6)
I just finished reviewing this series, in general it looks great to me, I would have another look (and test!) on Thursday -- either using this version or a re-spin.
-- David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
participants (2)
-
David Gibson
-
Stefano Brivio