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=XwUznka4; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 18C4D5A0653 for ; Thu, 19 Mar 2026 07:12:13 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1773900722; bh=UyRe58WHmASpxlNCxkEy+OlgA82ZGAW/QfmFa1/SZAk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XwUznka4+wFz5avRnhczwEjWlH14hF8GSpum5YFL0tRUyYeKT+dnHV0RT0sTDRg+P HUfa9dGrxsIQW8Y1nw9emSB1gJixpS+4swLMrae2wX11NNs/z1kGaRZAdo8FSieHIM 3vox7qXEUlLQB8SsVyIrdkR0WwgQr7XdIkYgQ01RMDYyWzvNILZY09Gj5/99YP8pcm 6fTz5Zd+uhXnBDRihbxF4Nc+OXCkNeyj4+aDihRlS4v6Z9gj+Fs6OO23cSscFRenNp rnLNztWRYgsxPSPSiOlocD12FYX7p3RiQ3tZ4Ym4z5DekySnKCdBQS/NnSEKJOX+jR FdcMmb5uPqlJA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4fbwMQ1D1tz4wT1; Thu, 19 Mar 2026 17:12:02 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v2 14/15] fwd_rule: Move forwarding rule text formatting to common code Date: Thu, 19 Mar 2026 17:11:56 +1100 Message-ID: <20260319061157.1983818-15-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260319061157.1983818-1-david@gibson.dropbear.id.au> References: <20260319061157.1983818-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: ACIX4AWACBJIBZLN2VGT3S7MKIUXMT45 X-Message-ID-Hash: ACIX4AWACBJIBZLN2VGT3S7MKIUXMT45 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: Move the logic for formatting forwarding rules into strings from fwd_rules_print() into fwd_rule.c where it can be shared with pesto. We also make the function explicitly construct a string, rather than directly printing with info(), for greater flexibility. Signed-off-by: David Gibson --- Makefile | 11 ++++---- fwd.c | 40 +++--------------------------- fwd_rule.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ fwd_rule.h | 11 ++++++++ 4 files changed, 94 insertions(+), 41 deletions(-) create mode 100644 fwd_rule.c diff --git a/Makefile b/Makefile index bc325482..44c396e7 100644 --- a/Makefile +++ b/Makefile @@ -38,13 +38,14 @@ FLAGS += -DVERSION=\"$(VERSION)\" FLAGS += -DDUAL_STACK_SOCKETS=$(DUAL_STACK_SOCKETS) PASST_SRCS = arch.c arp.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 + 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 -PESTO_SRCS = pesto.c inany.c ip.c serialise.c +PESTO_SRCS = pesto.c fwd_rule.c inany.c ip.c serialise.c SRCS = $(PASST_SRCS) $(QRAP_SRCS) $(PASST_REPAIR_SRCS) $(PESTO_SRCS) MANPAGES = passt.1 pasta.1 pesto.1 qrap.1 passt-repair.1 diff --git a/fwd.c b/fwd.c index f157eeb6..72028e70 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_is_ephemeral() - Is port number ephemeral? * @port: Port number @@ -495,28 +481,10 @@ 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 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); - } + char rulestr[FWD_RULE_STRLEN]; + + info(" %s", fwd_rule_ntop(&fwd->rules[i].rule, + rulestr, sizeof(rulestr))); } } diff --git a/fwd_rule.c b/fwd_rule.c new file mode 100644 index 00000000..dfbdf683 --- /dev/null +++ b/fwd_rule.c @@ -0,0 +1,73 @@ +// 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 + */ + +#include + +#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_ntop() - 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 + */ +const char *fwd_rule_ntop(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; +} diff --git a/fwd_rule.h b/fwd_rule.h index 84ec5cbe..59db0e95 100644 --- a/fwd_rule.h +++ b/fwd_rule.h @@ -42,4 +42,15 @@ struct fwd_rule { uint8_t flags; }; +const union inany_addr *fwd_rule_addr(const struct fwd_rule *rule); + +#define FWD_RULE_STRLEN \ + (IPPROTO_STRLEN - 1 \ + + INANY_ADDRSTRLEN - 1 \ + + IFNAMSIZ - 1 \ + + sizeof(" (best effort)") - 1 \ + + sizeof(" (auto-scan)") - 1 \ + + 15) +const char *fwd_rule_ntop(const struct fwd_rule *rule, char *dst, size_t size); + #endif /* FWD_RULE_H */ -- 2.53.0