From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: passt.top; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=LRR+6/TK; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTPS id E61595A0265 for ; Wed, 25 Mar 2026 01:56:39 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1774400198; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6zBOZcdnemFcSoXa+J551wzXapixzZirfIKw/r1YMGE=; b=LRR+6/TK2/E6OEQdEEAS3Wpts7SUEcdqxocqYkAwaR4jxFNUrF9y8lNYzTdHFYvOhBY+A+ WmZ9XykGuU6QUt509fhzgku0fLN4YdM85PF+B1eNABoYAZWTW3J6HmLEhewItmDCaS8OBw c0Wz/6qqDl062YS/Fewk+tdJXz9QOgQ= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-497-9mHTyvp7N4Coy4Yi5fvtdA-1; Tue, 24 Mar 2026 20:56:37 -0400 X-MC-Unique: 9mHTyvp7N4Coy4Yi5fvtdA-1 X-Mimecast-MFC-AGG-ID: 9mHTyvp7N4Coy4Yi5fvtdA_1774400196 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-4853b5b0fafso65936615e9.3 for ; Tue, 24 Mar 2026 17:56:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774400196; x=1775004996; h=date:content-transfer-encoding:mime-version:organization:references :in-reply-to:message-id:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=6zBOZcdnemFcSoXa+J551wzXapixzZirfIKw/r1YMGE=; b=qqcFDlq2g1ebZmY8ITgvQBEb8hdOQXbywBMlqLkIC/Ei9PVrtcADHEwjG0ODc4pTJU sHnVrKm1Y+f+KOjyqCC8L55LvWdDsaAsDJF4xhvgcDmaVTXQQjh6f3yiupojbTCkm0KJ swy15s18ilfyHHjaA6DY1aORikJeZsVJjv/VMfVqQfrS2L2KsDpIItk7x/16UjwFQzgt kOLy2fb1pcUTAux7eDxc/Q66lLoE3PE/7frqYdAELwlaBiazpjqiaVHJG6AL6r1JdDXC MD0Xt9GwJxLFbnQ04It3UzndrNvf61Q1kSoHrlgpJWyMzFqpWVF2708RJbTIhmt5YqoK jPCw== X-Gm-Message-State: AOJu0Yzh/uLjXFkM8mUvCY6NY9GBgX4+vK+IhxoBwNYgoMlEnyfzHV3V QoBGGNsuFbHre635n6o71VUrDl8v02y30NX3Nvb+eK+dk+TPuQbvgkc/zu+q0mfCwv1V13yOYOx b1ORuPXCRvpCS5ItPI/sN1HYuJuUsICnO2FuDE6B6PJVRPdMIqY+84Q== X-Gm-Gg: ATEYQzywgQ57QDJPf3XRFugKfZFUF49KvK8RT/UupcQoK3iq3u/GYb1kfWLtVHwx935 KEF9ryfwtq+pyYoeFNkck59lgF8cvNgT97WyF1Tue+O7PDGX2W9vg/4BXWa4vy7Gi5gGTwZWzGw I8Qu92mts8x5r35UZWuZjsKGKMFcNcskAu+4KvD53OPr5yfBbRJz3cbsgRp3oTm5GRxq9c7kc7b MoeXC3B16g/Fs8prViT0Yafc29oIYMcX4WxoLkwHMdfl+85GpNq4vQwVFxpKY6omAzIdXq5gLja 3O5BUvCnU0W9cmubNHa0WeobRM9fy/KfflAdRCsiJ1Oz+jmwdrGULb5LcETDzJhIr7TVgKGpHlW EeoZGkDxXa0ZPGFpXqXrvoL4U7YXkexp++RfcJ9vQMOeC5sUzSg== X-Received: by 2002:a05:600c:34ca:b0:485:3a27:a960 with SMTP id 5b1f17b1804b1-48715f0369fmr23533915e9.0.1774400195623; Tue, 24 Mar 2026 17:56:35 -0700 (PDT) X-Received: by 2002:a05:600c:34ca:b0:485:3a27:a960 with SMTP id 5b1f17b1804b1-48715f0369fmr23533755e9.0.1774400195091; Tue, 24 Mar 2026 17:56:35 -0700 (PDT) Received: from maya.myfinge.rs (ifcgrfdd.trafficplex.cloud. [176.103.220.4]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-487165feea6sm17742155e9.2.2026.03.24.17.56.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Mar 2026 17:56:34 -0700 (PDT) From: Stefano Brivio To: David Gibson Subject: Re: [PATCH v3 20/25] fwd_rule: Move forwarding rule text formatting to common code Message-ID: <20260325015633.655d7386@elisabeth> In-Reply-To: <20260323073732.3158468-21-david@gibson.dropbear.id.au> References: <20260323073732.3158468-1-david@gibson.dropbear.id.au> <20260323073732.3158468-21-david@gibson.dropbear.id.au> Organization: Red Hat X-Mailer: Claws Mail 4.2.0 (GTK 3.24.49; x86_64-pc-linux-gnu) MIME-Version: 1.0 Date: Wed, 25 Mar 2026 01:56:34 +0100 (CET) X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: OmNwf5LmDgG4nEKhrXQUWGbdZSCgjL-U08NE2-3NK6s_1774400196 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: D25EEZPRF3P4ZZV24GYILTSRT57BV7KD X-Message-ID-Hash: D25EEZPRF3P4ZZV24GYILTSRT57BV7KD X-MailFrom: sbrivio@redhat.com 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: On Mon, 23 Mar 2026 18:37:27 +1100 David Gibson wrote: > 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 a32d0a20..20409c62 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 > @@ -497,28 +483,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) I'm not quite sure about the reason for the 15 here, is there some constant we can use perhaps? > +const char *fwd_rule_ntop(const struct fwd_rule *rule, char *dst, size_t size); > + > #endif /* FWD_RULE_H */ -- Stefano