From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202602 header.b=GRvEcq6N; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 7853C5A061D for ; Fri, 27 Mar 2026 05:34:43 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1774586072; bh=oWnf5N0hoTK0ICCbTJ9gQRyR71BCAbXkB5jhU/w9e/o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GRvEcq6NCC0lKuvlZagwv+srrI/zQPDzvdnHmZLTq3cPtz5lCwWoU71PCY6qgYL+d g2s6Hiy7i5JzHUv5E4Amin/QTzWdkSv6zhl0X6SQpNrWlT4d8dO00kVud2t8983KnD A5TBJQ1dfCLFKBVKyV5FVa/79FICz3JN4RH63x8U7ztJE+chL4R4ec58d3gdHugucb KfEvdDXMjxJPTR6sF0v2R4EKwvj2WYdSm9qCxXfSeD0DCub1+siXWq0Ml7mhEw9hJS IsCW05V9cbDpP5DwWECP2BnWKKFFCxUvIgaMIB1Q3XVbhcQ6c6h2Dl4ygW9+OwNsKA WbOa7Wzoq/GHw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4fhnqD5cFHz4wT6; Fri, 27 Mar 2026 15:34:32 +1100 (AEDT) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 10/18] fwd, conf: Expose ephemeral ports as bitmap rather than function Date: Fri, 27 Mar 2026 15:34:22 +1100 Message-ID: <20260327043430.1785787-11-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260327043430.1785787-1-david@gibson.dropbear.id.au> References: <20260327043430.1785787-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: BWBUZCHNDNAF6SSJVVPHLXD3SLX6ZSEM X-Message-ID-Hash: BWBUZCHNDNAF6SSJVVPHLXD3SLX6ZSEM X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: It turns out the only callers of fwd_port_is_ephemeral() use it to build a bitmap of ephemeral ports. So, replace it with fwd_port_map_ephemeral(), which directly builds that bitmap. As a bonus this allows a slightly cheaper implementation of building the map, since inside fwd.c we know that the ephemeral ports form a single range. Signed-off-by: David Gibson --- conf.c | 8 ++------ fwd.c | 15 +++++++-------- fwd.h | 2 +- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/conf.c b/conf.c index 6ca61b74..7a3085b7 100644 --- a/conf.c +++ b/conf.c @@ -282,9 +282,7 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg, *mode = FWD_MODE_ALL; /* Exclude ephemeral ports */ - for (i = 0; i < NUM_PORTS; i++) - if (fwd_port_is_ephemeral(i)) - bitmap_set(exclude, i); + fwd_port_map_ephemeral(exclude); conf_ports_range_except(c, optname, optarg, fwd, NULL, NULL, @@ -376,9 +374,7 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg, if (exclude_only) { /* Exclude ephemeral ports */ - for (i = 0; i < NUM_PORTS; i++) - if (fwd_port_is_ephemeral(i)) - bitmap_set(exclude, i); + fwd_port_map_ephemeral(exclude); conf_ports_range_except(c, optname, optarg, fwd, addr, ifname, diff --git a/fwd.c b/fwd.c index 03652b23..62d344c3 100644 --- a/fwd.c +++ b/fwd.c @@ -319,16 +319,15 @@ static const union inany_addr *fwd_rule_addr(const struct fwd_rule *rule) } /** - * fwd_port_is_ephemeral() - Is port number ephemeral? - * @port: Port number - * - * Return: true if @port is ephemeral, that is may be allocated by the kernel as - * a local port for outgoing connections or datagrams, but should not be - * used for binding services to. + * fwd_port_map_ephemeral() - Mark ephemeral ports in a bitmap + * @map: Bitmap to update */ -bool fwd_port_is_ephemeral(in_port_t port) +void fwd_port_map_ephemeral(uint8_t *map) { - return (port >= fwd_ephemeral_min) && (port <= fwd_ephemeral_max); + unsigned port; + + for (port = fwd_ephemeral_min; port <= fwd_ephemeral_max; port++) + bitmap_set(map, port); } /* Forwarding table storage, generally accessed via pointers in struct ctx */ diff --git a/fwd.h b/fwd.h index b387d926..f111e139 100644 --- a/fwd.h +++ b/fwd.h @@ -22,7 +22,7 @@ struct flowside; #define NUM_PORTS (1U << 16) void fwd_probe_ephemeral(void); -bool fwd_port_is_ephemeral(in_port_t port); +void fwd_port_map_ephemeral(uint8_t *map); /** * struct fwd_rule - Forwarding rule governing a range of ports -- 2.53.0