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=ZgPkh7HP; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id E88615A026E for ; Tue, 07 Apr 2026 05:16:42 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1775531792; bh=bc+RshFtsMTxgsJnSs44JiFwcfC4Ok06K4rAVid2y50=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZgPkh7HPeVxh8l3aiAue0ouJ2X7egfBcKt0aHZa6fs6000uvHUgai+EUE1CPXhOpc /bo3A5C//NQW9DC5EehqBUgNzz118EN3u1e2Jnb5ky2eyEIK50E1EhOjGHBfJajuaE GKhOAW3uPK99upAV2ngiLyKcBljHaGvwhzi6xyv2Bf5tMbXJQPpYMhlyvrW/LQiNtr yJMRceJjsLJfo4ORDjPA/s8eAZzHiex/SmioQMfpSeRwe+sZtep+EtPIfvpWUGb80v a1XIBnhkoQ0fcOv5b9a0c/4nACcMyPgmHO/1ryPYhOIScY4Wc7zDP4G/qMX+XdNNhZ edMVSb/acMUDg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4fqWZ85QCJz4wL7; Tue, 07 Apr 2026 13:16:32 +1000 (AEST) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 06/18] fwd: Better split forwarding rule specification from associated sockets Date: Tue, 7 Apr 2026 13:16:18 +1000 Message-ID: <20260407031630.2457081-7-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260407031630.2457081-1-david@gibson.dropbear.id.au> References: <20260407031630.2457081-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 3663TZZPNNY366HEQC2FUSIQXPVGT23T X-Message-ID-Hash: 3663TZZPNNY366HEQC2FUSIQXPVGT23T 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: 6dad076df037 ("fwd: Split forwarding rule specification from its implementation state") created struct fwd_rule_state with a forwarding rule plus the table of sockets used for its implementation. It turns out this is quite awkward for sharing rule parsing code between passt and the upcoming configuration client. Instead keep the index of listening sockets in a parallel array in struct fwd_table. Signed-off-by: David Gibson --- fwd.c | 73 ++++++++++++++++++++++++++++++----------------------------- fwd.h | 16 ++++--------- 2 files changed, 41 insertions(+), 48 deletions(-) diff --git a/fwd.c b/fwd.c index e09b42fe..7e0edc38 100644 --- a/fwd.c +++ b/fwd.c @@ -363,7 +363,7 @@ void fwd_rule_add(struct fwd_table *fwd, uint8_t proto, uint8_t flags, /* Flags which can be set from the caller */ const uint8_t allowed_flags = FWD_WEAK | FWD_SCAN | FWD_DUAL_STACK_ANY; unsigned num = (unsigned)last - first + 1; - struct fwd_rule_state *new; + struct fwd_rule *new; unsigned i, port; assert(!(flags & ~allowed_flags)); @@ -379,7 +379,7 @@ void fwd_rule_add(struct fwd_table *fwd, uint8_t proto, uint8_t flags, /* Check for any conflicting entries */ for (i = 0; i < fwd->count; i++) { char newstr[INANY_ADDRSTRLEN], rulestr[INANY_ADDRSTRLEN]; - const struct fwd_rule *rule = &fwd->rules[i].rule; + const struct fwd_rule *rule = &fwd->rules[i]; if (proto != rule->proto) /* Non-conflicting protocols */ @@ -399,38 +399,38 @@ void fwd_rule_add(struct fwd_table *fwd, uint8_t proto, uint8_t flags, rule->first, rule->last); } - new = &fwd->rules[fwd->count++]; - new->rule.proto = proto; - new->rule.flags = flags; + new = &fwd->rules[fwd->count]; + new->proto = proto; + new->flags = flags; if (addr) { - new->rule.addr = *addr; + new->addr = *addr; } else { - new->rule.addr = inany_any6; - new->rule.flags |= FWD_DUAL_STACK_ANY; + new->addr = inany_any6; + new->flags |= FWD_DUAL_STACK_ANY; } - memset(new->rule.ifname, 0, sizeof(new->rule.ifname)); + memset(new->ifname, 0, sizeof(new->ifname)); if (ifname) { int ret; - ret = snprintf(new->rule.ifname, sizeof(new->rule.ifname), + ret = snprintf(new->ifname, sizeof(new->ifname), "%s", ifname); - if (ret <= 0 || (size_t)ret >= sizeof(new->rule.ifname)) + if (ret <= 0 || (size_t)ret >= sizeof(new->ifname)) die("Invalid interface name: %s", ifname); } assert(first <= last); - new->rule.first = first; - new->rule.last = last; + new->first = first; + new->last = last; + new->to = to; - new->rule.to = to; + fwd->rulesocks[fwd->count] = &fwd->socks[fwd->sock_count]; + for (port = new->first; port <= new->last; port++) + fwd->rulesocks[fwd->count][port - new->first] = -1; - new->socks = &fwd->socks[fwd->sock_count]; + fwd->count++; fwd->sock_count += num; - - for (port = new->rule.first; port <= new->rule.last; port++) - new->socks[port - new->rule.first] = -1; } /** @@ -466,7 +466,7 @@ const struct fwd_rule *fwd_rule_search(const struct fwd_table *fwd, if (hint >= 0) { char ostr[INANY_ADDRSTRLEN], rstr[INANY_ADDRSTRLEN]; - const struct fwd_rule *rule = &fwd->rules[hint].rule; + const struct fwd_rule *rule = &fwd->rules[hint]; assert((unsigned)hint < fwd->count); if (fwd_rule_match(rule, ini, proto)) @@ -480,8 +480,8 @@ const struct fwd_rule *fwd_rule_search(const struct fwd_table *fwd, } for (i = 0; i < fwd->count; i++) { - if (fwd_rule_match(&fwd->rules[i].rule, ini, proto)) - return &fwd->rules[i].rule; + if (fwd_rule_match(&fwd->rules[i], ini, proto)) + return &fwd->rules[i]; } return NULL; @@ -496,7 +496,7 @@ void fwd_rules_print(const struct fwd_table *fwd) unsigned i; for (i = 0; i < fwd->count; i++) { - const struct fwd_rule *rule = &fwd->rules[i].rule; + const struct fwd_rule *rule = &fwd->rules[i]; const char *percent = *rule->ifname ? "%" : ""; const char *weak = "", *scan = ""; char addr[INANY_ADDRSTRLEN]; @@ -533,9 +533,9 @@ void fwd_rules_print(const struct fwd_table *fwd) static int fwd_sync_one(const struct ctx *c, uint8_t pif, unsigned idx, const uint8_t *tcp, const uint8_t *udp) { - const struct fwd_rule_state *rs = &c->fwd[pif]->rules[idx]; - const struct fwd_rule *rule = &rs->rule; + const struct fwd_rule *rule = &c->fwd[pif]->rules[idx]; const union inany_addr *addr = fwd_rule_addr(rule); + int *socks = c->fwd[pif]->rulesocks[idx]; const char *ifname = rule->ifname; const uint8_t *map = NULL; bool bound_one = false; @@ -555,7 +555,7 @@ static int fwd_sync_one(const struct ctx *c, uint8_t pif, unsigned idx, } for (port = rule->first; port <= rule->last; port++) { - int fd = rs->socks[port - rule->first]; + int fd = socks[port - rule->first]; if (map && !bitmap_isset(map, port)) { /* We don't want to listen on this port */ @@ -563,7 +563,7 @@ static int fwd_sync_one(const struct ctx *c, uint8_t pif, unsigned idx, /* We already are, so stop */ epoll_del(c->epollfd, fd); close(fd); - rs->socks[port - rule->first] = -1; + socks[port - rule->first] = -1; } continue; } @@ -595,7 +595,7 @@ static int fwd_sync_one(const struct ctx *c, uint8_t pif, unsigned idx, continue; } - rs->socks[port - rule->first] = fd; + socks[port - rule->first] = fd; bound_one = true; } @@ -685,11 +685,12 @@ void fwd_listen_close(const struct fwd_table *fwd) unsigned i; for (i = 0; i < fwd->count; i++) { - const struct fwd_rule_state *rs = &fwd->rules[i]; + const struct fwd_rule *rule = &fwd->rules[i]; + int *socks = fwd->rulesocks[i]; unsigned port; - for (port = rs->rule.first; port <= rs->rule.last; port++) { - int *fdp = &rs->socks[port - rs->rule.first]; + for (port = rule->first; port <= rule->last; port++) { + int *fdp = &socks[port - rule->first]; if (*fdp >= 0) { close(*fdp); *fdp = -1; @@ -769,8 +770,8 @@ static bool has_scan_rules(const struct fwd_table *fwd, uint8_t proto) unsigned i; for (i = 0; i < fwd->count; i++) { - if (fwd->rules[i].rule.proto == proto && - fwd->rules[i].rule.flags & FWD_SCAN) + if (fwd->rules[i].proto == proto && + fwd->rules[i].flags & FWD_SCAN) return true; } return false; @@ -838,14 +839,14 @@ static void current_listen_map(uint8_t *map, const struct fwd_table *fwd, memset(map, 0, PORT_BITMAP_SIZE); for (i = 0; i < fwd->count; i++) { - const struct fwd_rule_state *rs = &fwd->rules[i]; + const struct fwd_rule *rule = &fwd->rules[i]; unsigned port; - if (rs->rule.proto != proto) + if (rule->proto != proto) continue; - for (port = rs->rule.first; port <= rs->rule.last; port++) { - if (rs->socks[port - rs->rule.first] >= 0) + for (port = rule->first; port <= rule->last; port++) { + if (fwd->rulesocks[i][port - rule->first] >= 0) bitmap_set(map, port); } } diff --git a/fwd.h b/fwd.h index 33600cbf..83ee9b2e 100644 --- a/fwd.h +++ b/fwd.h @@ -26,16 +26,6 @@ struct flowside; void fwd_probe_ephemeral(void); void fwd_port_map_ephemeral(uint8_t *map); -/** - * struct fwd_rule_state - Forwarding rule and associated state - * @rule: Rule specification - * @socks: Array of listening sockets for this entry - */ -struct fwd_rule_state { - struct fwd_rule rule; - int *socks; -}; - #define FWD_RULE_BITS 8 #define MAX_FWD_RULES MAX_FROM_BITS(FWD_RULE_BITS) #define FWD_NO_HINT (-1) @@ -61,15 +51,17 @@ struct fwd_listen_ref { #define MAX_LISTEN_SOCKS (NUM_PORTS * 5) /** - * struct fwd_table - Table of forwarding rules (per initiating pif) + * struct fwd_table - Forwarding state (per initiating pif) * @count: Number of forwarding rules * @rules: Array of forwarding rules + * @rulesocks: Pointers to socket arrays per-rule * @sock_count: Number of entries used in @socks * @socks: Listening sockets for forwarding */ struct fwd_table { unsigned count; - struct fwd_rule_state rules[MAX_FWD_RULES]; + struct fwd_rule rules[MAX_FWD_RULES]; + int *rulesocks[MAX_FWD_RULES]; unsigned sock_count; int socks[MAX_LISTEN_SOCKS]; }; -- 2.53.0