public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Jon Maloy <jmaloy@redhat.com>
To: sbrivio@redhat.com, david@gibson.dropbear.id.au,
	jmaloy@redhat.com, passt-dev@passt.top
Subject: [PATCH v8 11/14] dhcp: Select address for DHCP distribution
Date: Thu, 25 Jun 2026 22:45:16 -0400	[thread overview]
Message-ID: <20260626024519.3701556-12-jmaloy@redhat.com> (raw)
In-Reply-To: <20260626024519.3701556-1-jmaloy@redhat.com>

We introduce a CONF_ADDR_DHCPOFFER flag to mark if an added address is
eligible for DHCP advertisement. By doing this once and for all
in the fwd_set_addr() function, the DHCP code only needs to check
for this flag to know that all criteria for advertisement are
fulfilled. Hence, we update the code in dhcp.c correspondingly.

We also let the conf_print() function use this flag to determine
and print the selected address.

Signed-off-by: Jon Maloy <jmaloy@redhat.com>

---
v6: -Split off from a commit handling both DHCP and DHCPv6

v7: -Modified DHCP advertisement eligibility criteria IPv4 addresses:
     We now permit link-local addresses to be eligible if they were
     configured by the user.
    -Adapted to previous changes in this series

v8: - Adapted to previous changes in this series
    - Simplified DHCP eligibility branch in fwd_set_addr() (David)
    - Moved this commit to earlier in the series to avoid
      a technical migration protocol change (David).
    - Renamed CONF_ADDR_DHCP to CONF_ADDR_DHCPOFFER (Stefano)
---
 conf.c  |  5 +++--
 dhcp.c  | 12 ++++++------
 fwd.c   |  9 +++++++++
 passt.h |  1 +
 4 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/conf.c b/conf.c
index 69c1c439..f30c238e 100644
--- a/conf.c
+++ b/conf.c
@@ -47,6 +47,7 @@
 #include "lineread.h"
 #include "isolation.h"
 #include "log.h"
+#include "fwd.h"
 #include "vhost_user.h"
 #include "epoll_ctl.h"
 #include "conf.h"
@@ -832,8 +833,8 @@ static void conf_print(const struct ctx *c)
 			     inet_ntop(AF_INET, &c->ip4.map_host_loopback,
 				       buf, sizeof(buf)));
 
-		a = fwd_get_addr(c, AF_INET, CONF_ADDR_ANY, 0);
-		if (a && !c->no_dhcp) {
+		a = fwd_get_addr(c, AF_INET, CONF_ADDR_DHCPOFFER, 0);
+		if (a) {
 			uint32_t mask;
 			int plen = 0;
 
diff --git a/dhcp.c b/dhcp.c
index 727485f7..a7a9765a 100644
--- a/dhcp.c
+++ b/dhcp.c
@@ -31,6 +31,8 @@
 #include "passt.h"
 #include "tap.h"
 #include "log.h"
+#include "fwd.h"
+#include "conf.h"
 #include "dhcp.h"
 
 /**
@@ -302,19 +304,18 @@ static void opt_set_dns_search(const struct ctx *c, size_t max_len)
  */
 int dhcp(const struct ctx *c, struct iov_tail *data)
 {
+	struct in_addr addr, mask, dst;
 	char macstr[ETH_ADDRSTRLEN];
 	const struct guest_addr *a;
 	size_t mlen, dlen, opt_len;
-	struct in_addr mask, dst;
 	struct ethhdr eh_storage;
 	struct iphdr iph_storage;
 	struct udphdr uh_storage;
+	const struct udphdr *uh;
 	const struct ethhdr *eh;
 	const struct iphdr *iph;
-	const struct udphdr *uh;
 	struct msg m_storage;
 	struct msg const *m;
-	struct in_addr addr;
 	struct msg reply;
 	unsigned int i;
 	int plen;
@@ -347,9 +348,8 @@ int dhcp(const struct ctx *c, struct iov_tail *data)
 	    m->op != BOOTREQUEST)
 		return -1;
 
-	a = fwd_get_addr(c, AF_INET, CONF_ADDR_USER, 0);
-	if (!a)
-		a = fwd_get_addr(c, AF_INET, CONF_ADDR_ANY, 0);
+	/* Select address to offer */
+	a = fwd_get_addr(c, AF_INET, CONF_ADDR_DHCPOFFER, 0);
 	if (!a)
 		return -1;
 
diff --git a/fwd.c b/fwd.c
index e4090f4d..669fc237 100644
--- a/fwd.c
+++ b/fwd.c
@@ -292,6 +292,15 @@ void fwd_set_addr(struct ctx *c, const union inany_addr *addr,
 		return;
 	}
 
+	/* Determine advertisement eligibility */
+	if ((flags & CONF_ADDR_USER) ||
+	    (flags & CONF_ADDR_HOST && !(flags & CONF_ADDR_LINKLOCAL))) {
+		if (inany_v4(addr)) {
+			if (!c->no_dhcp)
+				flags |= CONF_ADDR_DHCPOFFER;
+		}
+	}
+
 	/* Add to head or tail, depending on flag */
 	if (flags & CONF_ADDR_OBSERVED) {
 		memmove(&head[1], &head[0], c->addr_count * sizeof(*a));
diff --git a/passt.h b/passt.h
index 005f7631..b323bd48 100644
--- a/passt.h
+++ b/passt.h
@@ -82,6 +82,7 @@ struct guest_addr {
 #define CONF_ADDR_GENERATED	BIT(2)		/* Generated by PASST/PASTA */
 #define CONF_ADDR_LINKLOCAL	BIT(3)		/* Link-local address */
 #define CONF_ADDR_OBSERVED	BIT(4)		/* Seen in guest traffic */
+#define CONF_ADDR_DHCPOFFER	BIT(5)		/* Advertise via DHCP (IPv4) */
 #define CONF_ADDR_ANY		0xff		/* Match any flag */
 };
 
-- 
2.52.0


  parent reply	other threads:[~2026-06-26  2:45 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-26  2:45 [PATCH v8 00/14] Introduce multiple addresses Jon Maloy
2026-06-26  2:45 ` [PATCH v8 01/14] dhcpv6: Fix reply destination to match client's source address Jon Maloy
2026-06-26  2:45 ` [PATCH v8 02/14] passt, pasta: Introduce unified multi-address data structures Jon Maloy
2026-06-26  2:45 ` [PATCH v8 03/14] tap, conf: Replace addr_fixed with CONF_ADDR_USER flag check Jon Maloy
2026-06-26  2:45 ` [PATCH v8 04/14] fwd: Unify guest accessibility checks with unified address array Jon Maloy
2026-06-26  2:45 ` [PATCH v8 05/14] arp: Check all configured addresses in ARP filtering Jon Maloy
2026-06-26  2:45 ` [PATCH v8 06/14] conf: Allow multiple -a/--address options per address family Jon Maloy
2026-06-26  2:45 ` [PATCH v8 07/14] netlink, conf: Read all addresses from template interface at startup Jon Maloy
2026-06-26  2:45 ` [PATCH v8 08/14] netlink, pasta: refactor function pasta_ns_conf() Jon Maloy
2026-06-26  2:45 ` [PATCH v8 09/14] conf, pasta: Track observed guest IPv4 addresses in unified address array Jon Maloy
2026-06-26  2:45 ` [PATCH v8 10/14] conf, pasta: Track observed guest IPv6 " Jon Maloy
2026-06-26  2:45 ` Jon Maloy [this message]
2026-06-26  2:45 ` [PATCH v8 12/14] dhcpv6: Select addresses for DHCPv6 distribution Jon Maloy
2026-06-26  2:45 ` [PATCH v8 13/14] ndp: Support advertising multiple prefixes in Router Advertisements Jon Maloy
2026-06-26  2:45 ` [PATCH v8 14/14] migrate: Update protocol to v3 for multi-address support Jon Maloy

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=20260626024519.3701556-12-jmaloy@redhat.com \
    --to=jmaloy@redhat.com \
    --cc=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).