public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Stefano Brivio <sbrivio@redhat.com>
To: David Gibson <david@gibson.dropbear.id.au>
Cc: passt-dev@passt.top
Subject: Re: [PATCH 1/3] tcp: Merge tcp_ns_sock_init[46]() into tcp_sock_init_one()
Date: Mon, 20 Oct 2025 08:08:39 +0200	[thread overview]
Message-ID: <20251020080839.0b4d4f82@elisabeth> (raw)
In-Reply-To: <20251017003447.414103-2-david@gibson.dropbear.id.au>

On Fri, 17 Oct 2025 11:34:45 +1100
David Gibson <david@gibson.dropbear.id.au> wrote:

> Surprisingly little logic is shared between the path for creating a
> listen()ing socket in the guest namespace versus in the host namespace.
> Improve this, by extending tcp_sock_init_one() to take a pif parameter
> indicating where it should open the socket.  This allows
> tcp_ns_sock_init[46]() to be removed entirely.
> 
> We generalise tcp_sock_init() in the same way, although we don't use it
> yet, due to some subtle differences in how we bind for -t versus -T.
> 
> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
> ---
>  conf.c |  2 +-
>  tcp.c  | 96 ++++++++++++++++++----------------------------------------
>  tcp.h  |  5 +--
>  3 files changed, 33 insertions(+), 70 deletions(-)
> 
> diff --git a/conf.c b/conf.c
> index 66b9e634..26f1bcc0 100644
> --- a/conf.c
> +++ b/conf.c
> @@ -169,7 +169,7 @@ static void conf_ports_range_except(const struct ctx *c, char optname,
>  		fwd->delta[i] = to - first;
>  
>  		if (optname == 't')
> -			ret = tcp_sock_init(c, addr, ifname, i);
> +			ret = tcp_sock_init(c, PIF_HOST, addr, ifname, i);
>  		else if (optname == 'u')
>  			ret = udp_sock_init(c, 0, addr, ifname, i);
>  		else
> diff --git a/tcp.c b/tcp.c
> index 0f9e9b3f..15c012d7 100644
> --- a/tcp.c
> +++ b/tcp.c
> @@ -2515,29 +2515,38 @@ void tcp_sock_handler(const struct ctx *c, union epoll_ref ref,
>  /**
>   * tcp_sock_init_one() - Initialise listening socket for address and port
>   * @c:		Execution context
> + * @pif:	Interface to open the socket for (PIF_HOST or PIF_SPLICE)
>   * @addr:	Pointer to address for binding, NULL for dual stack any
>   * @ifname:	Name of interface to bind to, NULL if not configured
>   * @port:	Port, host order
>   *
>   * Return: fd for the new listening socket, negative error code on failure
> + *
> + * If pif == PIF_SPLICE, must have already entered the namespace.
>   */
> -static int tcp_sock_init_one(const struct ctx *c, const union inany_addr *addr,
> -			     const char *ifname, in_port_t port)
> +static int tcp_sock_init_one(const struct ctx *c, uint8_t pif,
> +			     const union inany_addr *addr, const char *ifname,
> +			     in_port_t port)
>  {
> +	const struct fwd_ports *fwd = pif == PIF_HOST ?
> +		&c->tcp.fwd_in : &c->tcp.fwd_out;

While I appreciate the resulting brevity, I wonder if it would make
more sense to have this as an explicit if / else clause, for
readability. Same for similar occurrences in the next patches (which I
didn't fully review, yet).

Another alternative is:

	const struct fwd_ports *fwd;

	fwd = (pif == PIF_HOST) ? &c->tcp.fwd_in : &c->tcp.fwd_out;

...still two lines of code, perhaps just slightly less readable than
the five obvious ones:

	const struct fwd_ports *fwd;

	if (pif == PIF_HOST)
		fwd = &c->tcp.fwd_in;
	else
		fwd = &c->tcp.fwd_out;

-- 
Stefano


  reply	other threads:[~2025-10-20  6:08 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-17  0:34 [PATCH 0/3] RFC: Reduce differences between inbound and outbound socket binding David Gibson
2025-10-17  0:34 ` [PATCH 1/3] tcp: Merge tcp_ns_sock_init[46]() into tcp_sock_init_one() David Gibson
2025-10-20  6:08   ` Stefano Brivio [this message]
2025-10-20  9:24     ` David Gibson
2025-10-20  6:09   ` Stefano Brivio
2025-10-20  9:25     ` David Gibson
2025-10-17  0:34 ` [PATCH 2/3] udp: Unify some more inbound/outbound parts of udp_sock_init() David Gibson
2025-10-21 21:51   ` Stefano Brivio
2025-10-22  0:08     ` David Gibson
2025-10-17  0:34 ` [PATCH 3/3] tcp, udp: Bind outbound listening sockets by interface instead of address David Gibson
2025-10-21 21:51   ` Stefano Brivio
2025-10-22  0:34     ` David Gibson
2025-10-22  8:59       ` Stefano Brivio
2025-10-23  1:18         ` David Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251020080839.0b4d4f82@elisabeth \
    --to=sbrivio@redhat.com \
    --cc=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).