From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top, Stefano Brivio <sbrivio@redhat.com>
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 07/18] fwd_rule: Move forwarding rule formatting
Date: Tue, 7 Apr 2026 13:16:19 +1000 [thread overview]
Message-ID: <20260407031630.2457081-8-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20260407031630.2457081-1-david@gibson.dropbear.id.au>
In order to be shared with the upcoming configuration client, we want to
split code which deals with forwarding rules as standalone objects from
those which deal with forwarding rules as they're actually used to
implement forwarding in passt/pasta.
Create fwd_rule.c to contain code from the first category, and start off
by moving code to format rules into text for human display into it. While
we're at it, we rework that formatting code a little to make it more
flexible.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
Makefile | 10 +++---
conf.c | 2 +-
fwd.c | 48 -----------------------------
fwd.h | 1 -
fwd_rule.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
fwd_rule.h | 12 ++++++++
6 files changed, 107 insertions(+), 55 deletions(-)
create mode 100644 fwd_rule.c
diff --git a/Makefile b/Makefile
index dd647b10..f697c12b 100644
--- a/Makefile
+++ b/Makefile
@@ -38,11 +38,11 @@ FLAGS += -DVERSION=\"$(VERSION)\"
FLAGS += -DDUAL_STACK_SOCKETS=$(DUAL_STACK_SOCKETS)
PASST_SRCS = arch.c arp.c bitmap.c checksum.c conf.c dhcp.c dhcpv6.c \
- epoll_ctl.c flow.c fwd.c icmp.c igmp.c inany.c iov.c ip.c isolation.c \
- lineread.c log.c mld.c ndp.c netlink.c migrate.c packet.c passt.c \
- pasta.c pcap.c pif.c repair.c serialise.c tap.c tcp.c tcp_buf.c \
- tcp_splice.c tcp_vu.c udp.c udp_flow.c udp_vu.c util.c vhost_user.c \
- virtio.c vu_common.c
+ epoll_ctl.c flow.c fwd.c fwd_rule.c icmp.c igmp.c inany.c iov.c ip.c \
+ isolation.c lineread.c log.c mld.c ndp.c netlink.c migrate.c packet.c \
+ passt.c pasta.c pcap.c pif.c repair.c serialise.c tap.c tcp.c \
+ tcp_buf.c tcp_splice.c tcp_vu.c udp.c udp_flow.c udp_vu.c util.c \
+ vhost_user.c virtio.c vu_common.c
QRAP_SRCS = qrap.c
PASST_REPAIR_SRCS = passt-repair.c
SRCS = $(PASST_SRCS) $(QRAP_SRCS) $(PASST_REPAIR_SRCS)
diff --git a/conf.c b/conf.c
index 870712af..a3452bc9 100644
--- a/conf.c
+++ b/conf.c
@@ -1261,7 +1261,7 @@ dns6:
dir = "Inbound";
info("%s forwarding rules (%s):", dir, pif_name(i));
- fwd_rules_print(c->fwd[i]);
+ fwd_rules_info(c->fwd[i]->rules, c->fwd[i]->count);
}
}
diff --git a/fwd.c b/fwd.c
index 7e0edc38..39a14c40 100644
--- a/fwd.c
+++ b/fwd.c
@@ -304,20 +304,6 @@ parse_err:
warn("Unable to parse %s", PORT_RANGE_SYSCTL);
}
-/**
- * fwd_rule_addr() - Return match address for a rule
- * @rule: Forwarding rule
- *
- * Return: matching address for rule, NULL if it matches all addresses
- */
-static const union inany_addr *fwd_rule_addr(const struct fwd_rule *rule)
-{
- if (rule->flags & FWD_DUAL_STACK_ANY)
- return NULL;
-
- return &rule->addr;
-}
-
/**
* fwd_port_map_ephemeral() - Mark ephemeral ports in a bitmap
* @map: Bitmap to update
@@ -487,40 +473,6 @@ const struct fwd_rule *fwd_rule_search(const struct fwd_table *fwd,
return NULL;
}
-/**
- * fwd_rules_print() - Print forwarding rules for debugging
- * @fwd: Table to print
- */
-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];
- const char *percent = *rule->ifname ? "%" : "";
- const char *weak = "", *scan = "";
- char addr[INANY_ADDRSTRLEN];
-
- inany_ntop(fwd_rule_addr(rule), addr, sizeof(addr));
- if (rule->flags & FWD_WEAK)
- weak = " (best effort)";
- if (rule->flags & FWD_SCAN)
- scan = " (auto-scan)";
-
- if (rule->first == rule->last) {
- info(" %s [%s]%s%s:%hu => %hu %s%s",
- ipproto_name(rule->proto), addr, percent,
- rule->ifname, rule->first, rule->to, weak, scan);
- } else {
- info(" %s [%s]%s%s:%hu-%hu => %hu-%hu %s%s",
- ipproto_name(rule->proto), addr, percent,
- rule->ifname, rule->first, rule->last,
- rule->to, rule->last - rule->first + rule->to,
- weak, scan);
- }
- }
-}
-
/** fwd_sync_one() - Create or remove listening sockets for a forward entry
* @c: Execution context
* @pif: Interface to create listening sockets for
diff --git a/fwd.h b/fwd.h
index 83ee9b2e..c5f6d554 100644
--- a/fwd.h
+++ b/fwd.h
@@ -89,7 +89,6 @@ void fwd_rule_add(struct fwd_table *fwd, uint8_t proto, uint8_t flags,
const struct fwd_rule *fwd_rule_search(const struct fwd_table *fwd,
const struct flowside *ini,
uint8_t proto, int hint);
-void fwd_rules_print(const struct fwd_table *fwd);
void fwd_scan_ports_init(struct ctx *c);
void fwd_scan_ports_timer(struct ctx * c, const struct timespec *now);
diff --git a/fwd_rule.c b/fwd_rule.c
new file mode 100644
index 00000000..abe9dfbf
--- /dev/null
+++ b/fwd_rule.c
@@ -0,0 +1,89 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+/* PASST - Plug A Simple Socket Transport
+ * for qemu/UNIX domain socket mode
+ *
+ * PASTA - Pack A Subtle Tap Abstraction
+ * for network namespace/tap device mode
+ *
+ * PESTO - Programmable Extensible Socket Translation Orchestrator
+ * front-end for passt(1) and pasta(1) forwarding configuration
+ *
+ * fwd_rule.c - Helpers for working with forwarding rule specifications
+ *
+ * Copyright Red Hat
+ * Author: David Gibson <david@gibson.dropbear.id.au>
+ */
+
+#include <stdio.h>
+
+#include "fwd_rule.h"
+
+/**
+ * fwd_rule_addr() - Return match address for a rule
+ * @rule: Forwarding rule
+ *
+ * Return: matching address for rule, NULL if it matches all addresses
+ */
+const union inany_addr *fwd_rule_addr(const struct fwd_rule *rule)
+{
+ if (rule->flags & FWD_DUAL_STACK_ANY)
+ return NULL;
+
+ return &rule->addr;
+}
+
+/**
+ * fwd_rule_fmt() - Prettily format forwarding rule as a string
+ * @rule: Rule to format
+ * @dst: Buffer to store output (should have FWD_RULE_STRLEN bytes)
+ * @size: Size of @dst
+ */
+static const char *fwd_rule_fmt(const struct fwd_rule *rule,
+ char *dst, size_t size)
+{
+ const char *percent = *rule->ifname ? "%" : "";
+ const char *weak = "", *scan = "";
+ char addr[INANY_ADDRSTRLEN];
+ int len;
+
+ inany_ntop(fwd_rule_addr(rule), addr, sizeof(addr));
+ if (rule->flags & FWD_WEAK)
+ weak = " (best effort)";
+ if (rule->flags & FWD_SCAN)
+ scan = " (auto-scan)";
+
+ if (rule->first == rule->last) {
+ len = snprintf(dst, size,
+ "%s [%s]%s%s:%hu => %hu %s%s",
+ ipproto_name(rule->proto), addr, percent,
+ rule->ifname, rule->first, rule->to, weak, scan);
+ } else {
+ in_port_t tolast = rule->last - rule->first + rule->to;
+ len = snprintf(dst, size,
+ "%s [%s]%s%s:%hu-%hu => %hu-%hu %s%s",
+ ipproto_name(rule->proto), addr, percent,
+ rule->ifname, rule->first, rule->last,
+ rule->to, tolast, weak, scan);
+ }
+
+ if (len < 0 || (size_t)len >= size)
+ return NULL;
+
+ return dst;
+}
+
+/**
+ * fwd_rules_info() - Print forwarding rules for debugging
+ * @fwd: Table to print
+ */
+void fwd_rules_info(const struct fwd_rule *rules, size_t count)
+{
+ unsigned i;
+
+ for (i = 0; i < count; i++) {
+ char buf[FWD_RULE_STRLEN];
+
+ info(" %s", fwd_rule_fmt(&rules[i], buf, sizeof(buf)));
+ }
+}
diff --git a/fwd_rule.h b/fwd_rule.h
index ae0e1d61..e92efb6d 100644
--- a/fwd_rule.h
+++ b/fwd_rule.h
@@ -13,7 +13,9 @@
#include <net/if.h>
#include <netinet/in.h>
+#include "ip.h"
#include "inany.h"
+#include "bitmap.h"
/**
* struct fwd_rule - Forwarding rule governing a range of ports
@@ -41,4 +43,14 @@ struct fwd_rule {
uint8_t flags;
};
+#define FWD_RULE_STRLEN \
+ (IPPROTO_STRLEN - 1 \
+ + INANY_ADDRSTRLEN - 1 \
+ + IFNAMSIZ - 1 \
+ + 4 * (UINT16_STRLEN - 1) \
+ + sizeof(" []%:- => - (best effort) (auto-scan)"))
+
+const union inany_addr *fwd_rule_addr(const struct fwd_rule *rule);
+void fwd_rules_info(const struct fwd_rule *rules, size_t count);
+
#endif /* FWD_RULE_H */
--
2.53.0
next prev parent reply other threads:[~2026-04-07 3:16 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-07 3:16 [PATCH 00/18] Rework forwarding option parsing David Gibson
2026-04-07 3:16 ` [PATCH 01/18] conf: Split parsing of port specifiers from the rest of -[tuTU] parsing David Gibson
2026-04-07 3:16 ` [PATCH 02/18] conf: Simplify handling of default forwarding mode David Gibson
2026-04-07 23:14 ` Stefano Brivio
2026-04-08 1:10 ` David Gibson
2026-04-07 3:16 ` [PATCH 03/18] conf: Move first pass handling of -[TU] next to handling of -[tu] David Gibson
2026-04-07 3:16 ` [PATCH 04/18] doc: Consolidate -[tu] option descriptions for passt and pasta David Gibson
2026-04-07 23:14 ` Stefano Brivio
2026-04-08 1:23 ` David Gibson
2026-04-07 3:16 ` [PATCH 05/18] conf: Permit -[tTuU] all in pasta mode David Gibson
2026-04-07 3:16 ` [PATCH 06/18] fwd: Better split forwarding rule specification from associated sockets David Gibson
2026-04-07 23:14 ` Stefano Brivio
2026-04-08 1:30 ` David Gibson
2026-04-08 21:39 ` Stefano Brivio
2026-04-09 0:47 ` David Gibson
2026-04-07 3:16 ` David Gibson [this message]
2026-04-07 3:16 ` [PATCH 08/18] conf: Pass protocol explicitly to conf_ports_range_except() David Gibson
2026-04-07 3:16 ` [PATCH 09/18] fwd: Split rule building from rule adding David Gibson
2026-04-07 3:16 ` [PATCH 10/18] fwd_rule: Move rule conflict checking from fwd_rule_add() to caller David Gibson
2026-04-07 23:14 ` Stefano Brivio
2026-04-08 1:37 ` David Gibson
2026-04-08 4:42 ` David Gibson
2026-04-07 3:16 ` [PATCH 11/18] fwd: Improve error handling in fwd_rule_add() David Gibson
2026-04-08 21:40 ` Stefano Brivio
2026-04-09 0:10 ` David Gibson
2026-04-07 3:16 ` [PATCH 12/18] conf: Don't be strict about exclusivity of forwarding mode David Gibson
2026-04-08 21:40 ` Stefano Brivio
2026-04-09 0:12 ` David Gibson
2026-04-07 3:16 ` [PATCH 13/18] conf: Rework stepping through chunks of port specifiers David Gibson
2026-04-08 21:40 ` Stefano Brivio
2026-04-09 0:13 ` David Gibson
2026-04-07 3:16 ` [PATCH 14/18] conf: Rework checking for garbage after a range David Gibson
2026-04-08 21:40 ` Stefano Brivio
2026-04-09 0:15 ` David Gibson
2026-04-07 3:16 ` [PATCH 15/18] conf: Move "all" handling to port specifier David Gibson
2026-04-08 21:40 ` Stefano Brivio
2026-04-07 3:16 ` [PATCH 16/18] conf: Allow user-specified auto-scanned port forwarding ranges David Gibson
2026-04-08 21:40 ` Stefano Brivio
2026-04-07 3:16 ` [PATCH 17/18] conf: Move SO_BINDTODEVICE workaround to conf_ports() David Gibson
2026-04-07 3:16 ` [PATCH 18/18] conf: Don't pass raw commandline argument to conf_ports_spec() 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=20260407031630.2457081-8-david@gibson.dropbear.id.au \
--to=david@gibson.dropbear.id.au \
--cc=passt-dev@passt.top \
--cc=sbrivio@redhat.com \
/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).