From: Stefano Brivio <sbrivio@redhat.com>
To: David Gibson <david@gibson.dropbear.id.au>
Cc: passt-dev@passt.top
Subject: Re: [PATCH v3 04/14] conf, fwd: Record "auto" port forwards in forwarding table
Date: Tue, 13 Jan 2026 00:26:15 +0100 [thread overview]
Message-ID: <20260113002615.53c73c8b@elisabeth> (raw)
In-Reply-To: <20260108022948.2657573-5-david@gibson.dropbear.id.au>
On Thu, 8 Jan 2026 13:29:38 +1100
David Gibson <david@gibson.dropbear.id.au> wrote:
> Currently the forwarding table records details of explicit port forwards,
> but nothing for -[tTuU] auto. That looks a little odd on the debug output,
> and will be a problem for future changes.
>
> Extend the forward table to have rules for auto-scanned forwards,
> using a new FWD_SCAN flag. For now the mechanism of auto port
> forwarding isn't updated, and we will only create a single FWD_SCAN
> rule per protocol and direction. We'll better integrate auto scanning
> with other forward table mechanics in future.
>
> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
> ---
> conf.c | 31 ++++++++++++++++++++++++++-----
> fwd.c | 18 +++++++++++-------
> fwd.h | 2 ++
> 3 files changed, 39 insertions(+), 12 deletions(-)
>
> diff --git a/conf.c b/conf.c
> index b486fefe..0bcf80d7 100644
> --- a/conf.c
> +++ b/conf.c
> @@ -135,7 +135,7 @@ static int parse_port_range(const char *s, char **endptr,
> * @ifname: Listening interface
> * @first: First port to forward
> * @last: Last port to forward
> - * @exclude: Bitmap of ports to exclude
> + * @exclude: Bitmap of ports to exclude (may be NULL)
> * @to: Port to translate @first to when forwarding
> * @flags: Flags for forwarding entries
> */
> @@ -168,11 +168,11 @@ static void conf_ports_range_except(const struct ctx *c, char optname,
> }
>
> for (base = first; base <= last; base++) {
> - if (bitmap_isset(exclude, base))
> + if (exclude && bitmap_isset(exclude, base))
> continue;
>
> for (i = base; i <= last; i++) {
> - if (bitmap_isset(exclude, i))
> + if (exclude && bitmap_isset(exclude, i))
> break;
>
> if (bitmap_isset(fwd->map, i)) {
> @@ -180,9 +180,9 @@ static void conf_ports_range_except(const struct ctx *c, char optname,
> "Altering mapping of already mapped port number: %s", optarg);
> }
>
> - if (optname == 't')
> + if (!(flags & FWD_SCAN) && optname == 't')
> fd = tcp_listen(c, PIF_HOST, addr, ifname, i);
> - else if (optname == 'u')
> + else if (!(flags & FWD_SCAN) && optname == 'u')
> fd = udp_listen(c, PIF_HOST, addr, ifname, i);
> else
> /* No way to check in advance for -T and -U */
> @@ -2202,6 +2202,27 @@ void conf(struct ctx *c, int argc, char **argv)
> if (!c->udp.fwd_out.mode)
> c->udp.fwd_out.mode = fwd_default;
>
> + if (c->tcp.fwd_in.mode == FWD_AUTO) {
> + conf_ports_range_except(c, 't', "auto", &c->tcp.fwd_in,
> + NULL, NULL, 1, NUM_PORTS - 1,
> + NULL, 1, FWD_SCAN);
> + }
> + if (c->tcp.fwd_out.mode == FWD_AUTO) {
> + conf_ports_range_except(c, 'T', "auto", &c->tcp.fwd_out,
> + NULL, "lo", 1, NUM_PORTS - 1,
> + NULL, 1, FWD_SCAN);
> + }
> + if (c->udp.fwd_in.mode == FWD_AUTO) {
> + conf_ports_range_except(c, 'u', "auto", &c->udp.fwd_in,
> + NULL, NULL, 1, NUM_PORTS - 1,
> + NULL, 1, FWD_SCAN);
> + }
> + if (c->udp.fwd_out.mode == FWD_AUTO) {
> + conf_ports_range_except(c, 'U', "auto", &c->udp.fwd_out,
> + NULL, "lo", 1, NUM_PORTS - 1,
> + NULL, 1, FWD_SCAN);
> + }
> +
> if (!c->quiet)
> conf_print(c);
> }
> diff --git a/fwd.c b/fwd.c
> index ad398e54..69aca441 100644
> --- a/fwd.c
> +++ b/fwd.c
> @@ -344,7 +344,7 @@ void fwd_rule_add(struct fwd_ports *fwd, uint8_t flags,
> in_port_t first, in_port_t last, in_port_t to)
> {
> /* Flags which can be set from the caller */
> - const uint8_t allowed_flags = FWD_WEAK;
> + const uint8_t allowed_flags = FWD_WEAK | FWD_SCAN;
> struct fwd_rule *new;
> unsigned port;
>
> @@ -375,7 +375,8 @@ void fwd_rule_add(struct fwd_ports *fwd, uint8_t flags,
>
> for (port = new->first; port <= new->last; port++) {
> /* Fill in the legacy data structures to match the table */
> - bitmap_set(fwd->map, port);
> + if (!(new->flags & FWD_SCAN))
> + bitmap_set(fwd->map, port);
> fwd->delta[port] = new->to - new->first;
> }
> }
> @@ -391,19 +392,22 @@ void fwd_rules_print(const struct fwd_ports *fwd)
> for (i = 0; i < fwd->count; i++) {
> const struct fwd_rule *rule = &fwd->rules[i];
> const char *weak = rule->flags & FWD_WEAK ? " WEAK" : "";
> + const char *scan = rule->flags & FWD_SCAN ? " AUTO" : "";
> const char *percent = *rule->ifname ? "%" : "";
> char addr[INANY_ADDRSTRLEN];
>
> inany_ntop(fwd_rule_addr(rule), addr, sizeof(addr));
>
> if (rule->first == rule->last) {
> - info(" [%s]%s%s:%hu => %hu %s",
> + info(" [%s]%s%s:%hu => %hu %s%s",
> addr, percent, rule->ifname,
> - rule->first, rule->to, weak);
> + rule->first, rule->to, weak, scan);
> } else {
> - info(" [%s]%s%s:%hu-%hu => %hu-%hu %s",
> - addr, percent, rule->ifname, rule->first, rule->last,
> - rule->to, rule->last - rule->first + rule->to, weak);
> + info(" [%s]%s%s:%hu-%hu => %hu-%hu %s%s",
> + addr, percent, rule->ifname,
> + rule->first, rule->last,
> + rule->to, rule->last - rule->first + rule->to,
> + weak, scan);
> }
> }
> }
> diff --git a/fwd.h b/fwd.h
> index 3dfc7959..94869c2a 100644
> --- a/fwd.h
> +++ b/fwd.h
> @@ -26,6 +26,7 @@ bool fwd_port_is_ephemeral(in_port_t port);
> * @flags: Flag mask
> * FWD_DUAL_STACK_ANY - match any IPv4 or IPv6 address (@addr should be ::)
> * FWD_WEAK - Don't give an error if binds fail for some forwards
> + * FWD_SCAN - Only forward if we scan a listener on the target
Nit: I guess this could be slightly more descriptive than "scan a
listener", mentioning for example "if the corresponding port is bound
on the target".
> *
> * FIXME: @addr and @ifname currently ignored for outbound tables
> */
> @@ -35,6 +36,7 @@ struct fwd_rule {
> in_port_t first, last, to;
> #define FWD_DUAL_STACK_ANY BIT(0)
> #define FWD_WEAK BIT(1)
> +#define FWD_SCAN BIT(2)
> uint8_t flags;
> };
>
--
Stefano
next prev parent reply other threads:[~2026-01-12 23:26 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-08 2:29 [PATCH v3 00/14] Introduce " David Gibson
2026-01-08 2:29 ` [PATCH v3 01/14] inany: Extend inany_ntop() to treat NULL as a fully unspecified address David Gibson
2026-01-08 13:16 ` Laurent Vivier
2026-01-08 2:29 ` [PATCH v3 02/14] conf, fwd: Keep a table of our port forwarding configuration David Gibson
2026-01-12 23:26 ` Stefano Brivio
2026-01-13 5:12 ` David Gibson
2026-01-08 2:29 ` [PATCH v3 03/14] conf: Accurately record ifname and address for outbound forwards David Gibson
2026-01-08 2:29 ` [PATCH v3 04/14] conf, fwd: Record "auto" port forwards in forwarding table David Gibson
2026-01-12 23:26 ` Stefano Brivio [this message]
2026-01-13 5:14 ` David Gibson
2026-01-08 2:29 ` [PATCH v3 05/14] fwd: Make space to store listening sockets in forward table David Gibson
2026-01-12 23:26 ` Stefano Brivio
2026-01-13 5:28 ` David Gibson
2026-01-08 2:29 ` [PATCH v3 06/14] ip: Add ipproto_name() function David Gibson
2026-01-08 13:22 ` Laurent Vivier
2026-01-08 23:12 ` David Gibson
2026-01-08 2:29 ` [PATCH v3 07/14] fwd, tcp, udp: Set up listening sockets based on forward table David Gibson
2026-01-12 23:26 ` Stefano Brivio
2026-01-13 5:38 ` David Gibson
2026-01-08 2:29 ` [PATCH v3 08/14] tcp, udp: Remove old auto-forwarding socket arrays David Gibson
2026-01-08 2:29 ` [PATCH v3 09/14] conf, fwd: Check forwarding table for conflicting rules David Gibson
2026-01-12 23:26 ` Stefano Brivio
2026-01-13 5:41 ` David Gibson
2026-01-08 2:29 ` [PATCH v3 10/14] fwd: Generate auto-forward exclusions from socket fd tables David Gibson
2026-01-08 2:29 ` [PATCH v3 11/14] flow, fwd: Consult rules table when forwarding a new flow from socket David Gibson
2026-01-08 2:29 ` [PATCH v3 12/14] fwd: Remap ports based directly on forwarding rule David Gibson
2026-01-08 2:29 ` [PATCH v3 13/14] fwd, tcp, udp: Add forwarding rule to listening socket epoll references David Gibson
2026-01-08 2:29 ` [PATCH v3 14/14] flow, fwd: Optimise forwarding rule lookup using epoll ref when possible 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=20260113002615.53c73c8b@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).