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=JwghTXxl; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 8F2275A026D for ; Wed, 25 Mar 2026 05:44:28 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202602; t=1774413865; bh=PtV5ar83FDaBAKobCcamavfOzMwSOb7p8KeeQXUVDSI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=JwghTXxlfDREQcGA35ue5PEUU/f+HilH2SjcREe2NVYF7nTb4+6vu/pcuGg8kBWXl NHhERv7F7PAroG++xkz6jKcRyzBIYQb9fuyVaw78/qVtFB0FkP5gh7nlFOkf1+Bv5w gaB3xV9/TtJ3NN/W6QhWdQ1Vii0zi+6dZFM0+yDiJQ59hpl+Y2rRI2jlf521uwC40x AR9m7gJQlLu3VKRZ+S8S0+fBNGdcZZE/rwIIgTaGpfu2HaycoD8KK68WFQir9adh4C WnAmOIOeKLc9DrAiDGt5XJNelrNBDF0Gcit1wPpCMD7m8blHRNJxg8ybHg6oWNu5rd IEdYO2cgZhImA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4fgZ7Y2kh7z4wM6; Wed, 25 Mar 2026 15:44:25 +1100 (AEDT) Date: Wed, 25 Mar 2026 15:42:00 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH v3 20/25] fwd_rule: Move forwarding rule text formatting to common code Message-ID: References: <20260323073732.3158468-1-david@gibson.dropbear.id.au> <20260323073732.3158468-21-david@gibson.dropbear.id.au> <20260325015633.655d7386@elisabeth> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="xW+x4HcDSsRgBAGq" Content-Disposition: inline In-Reply-To: <20260325015633.655d7386@elisabeth> Message-ID-Hash: HRO7DF6GSWF2OATBKS7NFPC56PUU6SEP X-Message-ID-Hash: HRO7DF6GSWF2OATBKS7NFPC56PUU6SEP 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: passt-dev@passt.top 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: --xW+x4HcDSsRgBAGq Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Mar 25, 2026 at 01:56:34AM +0100, Stefano Brivio wrote: > On Mon, 23 Mar 2026 18:37:27 +1100 > David Gibson wrote: >=20 > > 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. > >=20 > > 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 > >=20 > > diff --git a/Makefile b/Makefile > > index bc325482..44c396e7 100644 > > --- a/Makefile > > +++ b/Makefile > > @@ -38,13 +38,14 @@ FLAGS +=3D -DVERSION=3D\"$(VERSION)\" > > FLAGS +=3D -DDUAL_STACK_SOCKETS=3D$(DUAL_STACK_SOCKETS) > > =20 > > PASST_SRCS =3D arch.c arp.c checksum.c conf.c dhcp.c dhcpv6.c epoll_ct= l.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 =3D qrap.c > > PASST_REPAIR_SRCS =3D passt-repair.c > > -PESTO_SRCS =3D pesto.c inany.c ip.c serialise.c > > +PESTO_SRCS =3D pesto.c fwd_rule.c inany.c ip.c serialise.c > > SRCS =3D $(PASST_SRCS) $(QRAP_SRCS) $(PASST_REPAIR_SRCS) $(PESTO_SRCS) > > =20 > > MANPAGES =3D passt.1 pasta.1 pesto.1 qrap.1 passt-repair.1 > > diff --git a/fwd.c b/fwd.c > > index a32d0a20..20409c62 100644 > > --- a/fwd.c > > +++ b/fwd.c > > @@ -304,20 +304,6 @@ parse_err: > > warn("Unable to parse %s", PORT_RANGE_SYSCTL); > > } > > =20 > > -/** > > - * 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 *ru= le) > > -{ > > - 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 > > @@ -497,28 +483,10 @@ void fwd_rules_print(const struct fwd_table *fwd) > > unsigned i; > > =20 > > for (i =3D 0; i < fwd->count; i++) { > > - const struct fwd_rule *rule =3D &fwd->rules[i].rule; > > - const char *percent =3D *rule->ifname ? "%" : ""; > > - const char *weak =3D "", *scan =3D ""; > > - char addr[INANY_ADDRSTRLEN]; > > - > > - inany_ntop(fwd_rule_addr(rule), addr, sizeof(addr)); > > - if (rule->flags & FWD_WEAK) > > - weak =3D " (best effort)"; > > - if (rule->flags & FWD_SCAN) > > - scan =3D " (auto-scan)"; > > - > > - if (rule->first =3D=3D rule->last) { > > - info(" %s [%s]%s%s:%hu =3D> %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 =3D> %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))); > > } > > } > > =20 > > 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 =3D *rule->ifname ? "%" : ""; > > + const char *weak =3D "", *scan =3D ""; > > + char addr[INANY_ADDRSTRLEN]; > > + int len; > > + > > + inany_ntop(fwd_rule_addr(rule), addr, sizeof(addr)); > > + if (rule->flags & FWD_WEAK) > > + weak =3D " (best effort)"; > > + if (rule->flags & FWD_SCAN) > > + scan =3D " (auto-scan)"; > > + > > + if (rule->first =3D=3D rule->last) { > > + len =3D snprintf(dst, size, > > + "%s [%s]%s%s:%hu =3D> %hu %s%s", > > + ipproto_name(rule->proto), addr, percent, > > + rule->ifname, rule->first, rule->to, weak, scan); > > + } else { > > + in_port_t tolast =3D rule->last - rule->first + rule->to; > > + len =3D snprintf(dst, size, > > + "%s [%s]%s%s:%hu-%hu =3D> %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 >=3D 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; > > }; > > =20 > > +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) >=20 > I'm not quite sure about the reason for the 15 here, is there some > constant we can use perhaps? It's the spacing and punctuation around the other displayed parameters, roughly the number of non-format characters in "%s [%s]%s%s:%hu-%hu =3D> %hu-%hu %s%s" (plus one for the \0). I don't love it either, but I wasn't sure how to make it less obscure. Is sizeof(" []%:- =3D> - ") an improvement? I'm not sure. Oh... that made me realise I'm missing 4 * (UINT16_STRLEN - 1). I've fixed that now. >=20 > > +const char *fwd_rule_ntop(const struct fwd_rule *rule, char *dst, size= _t size); > > + > > #endif /* FWD_RULE_H */ >=20 > --=20 > Stefano >=20 --=20 David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson --xW+x4HcDSsRgBAGq Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmnDZ5cACgkQzQJF27ox 2Gdqig/8C2C93UeRlQ81kI18ehsKrXtGd8KtILqi072NpLKAhSmeCoO5LrvpYqWM LOmFf2mFCAnD7ciVFpzlqEfZaOHO3V+dso+jEWuUgI2BS9+qc4EdDMyjYG+Cf2Se nLEQDMT+QOTXjBzCRbJMLc01YSH4Z06BTNyYj26MIK+DOWiP52AWYAyF2ZqQVi2W jdxPZll/fmVOt95/AMn8Lifh05+K1cZuJ2odKLnjKPfxblZGcIjIpzBxdcR7eClI bZ8lf97UojB1okFVbyu5hnPrahDXQIQ+JJD07eYp1wOEQZWUUabLwZaGw30kfYEN sGuNCatZJW5QlJnkFEmd3zEDiwlRwtQIz4Np7P85HAryy8hbMvOlLtppFMHPnU1v K+0mOksFPkhaKmHYQnkkw7+j9RwJT01lflxnaHBNwSaCEwb88CdBMGhQ7ywwUhjH gQNMlkOAqOpSxdGdh/NR7WvW12Qfz6FMcu7vJs2+iPhUTMQXOe+YOninShNulArA qSb2AL0oZLJ1MEMXa58/Nvxe0ExkmNYoR0Li/0Uv12948v3T+MYintgU9Xh4D/nn YefudS/HAG1rYAc9Kd16u9kXGuKSyi0arLezzTPLWJ0cbWGD7WHOJg+G2ijc8MiG rtWytNM5m15Quw+RfyoC9UXo0evvgZ7B53+OUlm/w1ZKp4D30LI= =8Jji -----END PGP SIGNATURE----- --xW+x4HcDSsRgBAGq--