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=izDZ8zrC; 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 A98145A0269 for ; Thu, 10 Sep 2026 09:04:10 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1789023849; 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=w79hpI7jtP/Ilfs8ssag91WMJahAklPvEYwlKHvu4wA=; b=izDZ8zrCR8IRMc5djkw7m7TK6ld5InwX5q8myNl6NU0n4z7fWq/CNgUgY43SuWuUmKpHLQ wVZYUjYBS7ADh37aWWlfg371cSvKti/zKW4Eo7SC5uOEvOPmsD7ftAeFimiJqIuwmvzXQA 9FO86jFabHRKa0ArJPOGA24mi2cEFRU= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-263-YubXm0UWNoSEpTWyq11oHw-1; Thu, 10 Sep 2026 03:04:07 -0400 X-MC-Unique: YubXm0UWNoSEpTWyq11oHw-1 X-Mimecast-MFC-AGG-ID: YubXm0UWNoSEpTWyq11oHw_1789023847 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-47d81cf0c4cso4483406f8f.2 for ; Thu, 10 Sep 2026 00:04:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789023847; x=1789628647; h=date:content-transfer-encoding:content-type: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:content-type; bh=w79hpI7jtP/Ilfs8ssag91WMJahAklPvEYwlKHvu4wA=; b=jTm0J5mn9uzCAGU6PwDJSZh4U3Rfc/2ZgSwf5tAXk+/bUEt5NJb+3Azwse2G3dy7Me EV0RdieQNJnNUu3Q4ioovhCy6e/hmJIwdvNFFZOYqgVRkGyk5wQxONfz9AH5ICeD05iQ ER/FU8JwUqfpVz4gSR6RRBRhnp3HXLTEyHRrCOo/ZCdQfFGj/AeJ4guPJLu44hviXRJA r4eENEYg81br148ytVyBptKqyDFgeuGR/kqX8qkSwKya3ImTDgb5oXtzqGaOMYnhCuRN JKP78ZvqAVCz1QtiBM4bXMm+sS6ZgFbEY1EnVGsj2wpb6pdGymxhLhfXOzqiQUEdiWpe XzXg== X-Gm-Message-State: AFuF++lko8AuuVzjLcHwyufu99Zzaa2ROEEIO79wxBkfzS51VeqLskDe UGV2QeD/xHUrLB45wGFcFGBmEeDPdESEGJgx4AIpNX43MhOj4nbuIR1mSjO6sIyKtakZcmwAXj/ 6YsU5PPEK/34MYaABV0TGTgs4W1CPTqK7a3uNC9ZfJd3HWgXczsU3gQ== X-Gm-Gg: AYBFou3/f8TJjNWFY0YoFs5VzyZRMOYmHy3gctjF8TnXnVRe+ny6duqK9RQWcHp6ieV +mG+ijJS2QrXnWvWsSKe27dWnjxFdvM0eyvjxNUK0u4yA976zCHU8a8Qwn3zSV5QxBqGnA4tsft JhyIIq55aK+ieQ3ZAN+dlhcvNUErqs4D3OCO9e53chKExOEFZK5jMeSfE6iVImg8zWlBOgDaFsK y7rlJkcedRn1P0fMMvwABMMdToLFoTGrX5vZ7jx/8HGnq9xOmOOcFSNKN/5v14ZXwgxoXwHsNye TpMJ8CfZcmnH/Dqpz4JXhPFl+B0I5oVvG9e9LReHzefnhLuwvPB1MON77Pumm526tQwr09nczJF +WlmesGDvFp0= X-Received: by 2002:a05:600c:3f05:b0:49c:e1f1:3dd5 with SMTP id 5b1f17b1804b1-49d1f223e83mr197396695e9.4.1789023846485; Thu, 10 Sep 2026 00:04:06 -0700 (PDT) X-Received: by 2002:a05:600c:3f05:b0:49c:e1f1:3dd5 with SMTP id 5b1f17b1804b1-49d1f223e83mr197395375e9.4.1789023845874; Thu, 10 Sep 2026 00:04:05 -0700 (PDT) Received: from maya.myfinge.rs (ifcgrfdd.trafficplex.cloud. [2a10:fc81:a806:d6a9::1]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49d26d49d57sm45805765e9.14.2026.09.10.00.04.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Sep 2026 00:04:05 -0700 (PDT) From: Stefano Brivio To: Anshu Kumari Subject: Re: [PATCH v6 2/6] dhcp: Add option state management with enum opt_state Message-ID: <20260910090402.2cd76e8f@elisabeth> In-Reply-To: <20260824134436.282300-3-anskuma@redhat.com> References: <20260824134436.282300-1-anskuma@redhat.com> <20260824134436.282300-3-anskuma@redhat.com> Organization: Red Hat X-Mailer: Claws Mail 4.2.0 (GTK 3.24.49; x86_64-pc-linux-gnu) MIME-Version: 1.0 Date: Thu, 10 Sep 2026 09:04:04 +0200 (CEST) X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: nvhT5EY7hFLS91kys5AqSSaDGDKALBv5jgY9-9Hkc-k_1789023847 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: LTJHKH6OYRYVC6JOIGA4VBGRGTZDVYLZ X-Message-ID-Hash: LTJHKH6OYRYVC6JOIGA4VBGRGTZDVYLZ 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, david@gibson.dropbear.id.au, jmaloy@redhat.com, lvivier@redhat.com 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, 24 Aug 2026 19:14:30 +0530 Anshu Kumari wrote: > Overloading slen = -1 to mean "not set" conflates the option > length with its lifecycle state, and can't express additional > states such as "set by the user via command-line" which will > be needed for --dhcp-opt support. > > Introduce enum opt_state with OPT_UNSET (option not > configured) and OPT_DEFAULT (derived from host > configuration). Replace all slen = -1 / slen != -1 checks > with state comparisons, and set state = OPT_DEFAULT for > options initialised in dhcp_init() and at reply time in > dhcp(). > > Link: https://bugs.passt.top/show_bug.cgi?id=192 > Signed-off-by: Anshu Kumari > --- > v6: > - Rewrote commit message to explain why slen = -1 overloading > is problematic and how it motivates the enum. > - Dropped explicit OPT_UNSET initialization loop inside dhcp_init(). > - Realigned opts[51] initializer for consistency. > > v5: > - New patch: introduce enum opt_state { OPT_UNSET, OPT_DEFAULT } to replace slen = -1 for tracking option state > - Replace all slen = -1 / slen != -1 checks with state = OPT_UNSET / state != OPT_UNSET > - Set OPT_DEFAULT for options initialised in dhcp_init() and at reply time > --- > dhcp.c | 58 ++++++++++++++++++++++++++++++++++++++-------------------- > 1 file changed, 38 insertions(+), 20 deletions(-) > > diff --git a/dhcp.c b/dhcp.c > index bb72b72..321968a 100644 > --- a/dhcp.c > +++ b/dhcp.c > @@ -33,13 +33,24 @@ > #include "log.h" > #include "dhcp.h" > > +/** > + * enum opt_state - DHCP option state Nit: now that I see checks in later patches on .state (to check what was configured) _and_ on .sent (to check the actual state), I wonder if this shouldn't be called .conf instead, as it represents the configuration state, rather than what one might more naturally called state (sent, not sent, etc.). That is, I would rename this to enum opt_conf, call it 'conf' in struct opt, and change this comment to "DHCP option configuration". The names of the possible values are fine as they are, I think. > + * @OPT_UNSET: Option not configured > + * @OPT_DEFAULT: Option derived from host configuration > + */ > +enum opt_state { > + OPT_UNSET = 0, > + OPT_DEFAULT, > +}; > + > /** > * struct opt - DHCP option > * @sent: Convenience flag, set while filling replies > - * @slen: Length of option defined for server, -1 if not going to be sent > + * @slen: Length of option defined for server > * @s: Option payload from server > * @clen: Length of option received from client, -1 if not received > * @c: Option payload from client > + * @state: Option state (unset or default) > */ > struct opt { > int sent; > @@ -47,6 +58,7 @@ struct opt { > uint8_t s[255]; > int clen; > uint8_t c[255]; > + enum opt_state state; > }; > > static struct opt opts[256]; > @@ -73,19 +85,17 @@ static struct opt opts[256]; > */ > void dhcp_init(void) > { > - int i; > - > - for (i = 0; i < ARRAY_SIZE(opts); i++) > - opts[i].slen = -1; > - > - opts[1] = (struct opt) { 0, 4, { 0 }, 0, { 0 }, }; /* Mask */ > - opts[3] = (struct opt) { 0, 4, { 0 }, 0, { 0 }, }; /* Router */ > - opts[51] = (struct opt) { 0, 4, { 0xff, > - 0xff, > - 0xff, > - 0xff }, 0, { 0 }, }; /* Lease time */ > - opts[53] = (struct opt) { 0, 1, { 0 }, 0, { 0 }, }; /* Type */ > - opts[54] = (struct opt) { 0, 4, { 0 }, 0, { 0 }, }; /* Server ID */ > + /* Mask */ > + opts[1] = (struct opt) { 0, 4, { 0 }, 0, { 0 }, OPT_DEFAULT, }; > + /* Router */ > + opts[3] = (struct opt) { 0, 4, { 0 }, 0, { 0 }, OPT_DEFAULT, }; > + /* Lease time */ > + opts[51] = (struct opt) { 0, 4, { 0xff, 0xff, 0xff, 0xff }, > + 0, { 0 }, OPT_DEFAULT }; > + /* Type */ > + opts[53] = (struct opt) { 0, 1, { 0 }, 0, { 0 }, OPT_DEFAULT, }; > + /* Server ID */ > + opts[54] = (struct opt) { 0, 4, { 0 }, 0, { 0 }, OPT_DEFAULT, }; > } > > /** > @@ -183,13 +193,13 @@ static int fill(struct msg *m) > > for (i = 0; i < opts[55].clen; i++) { > o = opts[55].c[i]; > - if (opts[o].slen != -1) > + if (opts[o].state != OPT_UNSET) Pre-existing, but it would be convenient to fix this here: whenever we have multiple _lines_ (regardless of statements) in the body of a conditional clause, we use curly brackets, even if not needed, to decrease the risk of mistakes later. This is the same as the "netdev" Linux kernel coding style. > if (fill_one(m->o, OPT_MAX, o, &offset)) > debug("DHCP: skipping option %i", o); > } > > for (o = 0; o < 255; o++) { > - if (opts[o].slen != -1 && !opts[o].sent) > + if (opts[o].state != OPT_UNSET && !opts[o].sent) Same here. > if (fill_one(m->o, OPT_MAX, o, &offset)) > debug("DHCP: skipping option %i", o); > } > @@ -243,6 +253,7 @@ static void opt_set_dns_search(const struct ctx *c, size_t max_len) > int i; > > opts[119].slen = 0; > + opts[119].state = OPT_DEFAULT; > > for (i = 0; i < 255; i++) > max_len -= opts[i].slen; > @@ -291,7 +302,7 @@ static void opt_set_dns_search(const struct ctx *c, size_t max_len) > } > > if (!opts[119].slen) > - opts[119].slen = -1; > + opts[119].state = OPT_UNSET; > } > > /** > @@ -389,7 +400,7 @@ int dhcp(const struct ctx *c, struct iov_tail *data) > iov_drop_header(data, *olen); > } > > - opts[80].slen = -1; > + opts[80].state = OPT_UNSET; > if (opts[53].clen > 0 && opts[53].c[0] == DHCPDISCOVER) { > if (opts[80].clen == -1) { > info("DHCP: offer to discover"); > @@ -398,6 +409,7 @@ int dhcp(const struct ctx *c, struct iov_tail *data) > info("DHCP: ack to discover (Rapid Commit)"); > opts[53].s[0] = DHCPACK; > opts[80].slen = 0; > + opts[80].state = OPT_DEFAULT; > } > } else if (opts[53].clen <= 0 || opts[53].c[0] == DHCPREQUEST) { > info("%s: ack to request", /* DHCP needs a valid message type */ > @@ -421,6 +433,7 @@ int dhcp(const struct ctx *c, struct iov_tail *data) > != (c->ip4.guest_gw.s_addr & mask.s_addr)) { > /* a.b.c.d/32:0.0.0.0, 0:a.b.c.d */ > opts[121].slen = 14; > + opts[121].state = OPT_DEFAULT; > opts[121].s[0] = 32; > memcpy(opts[121].s + 1, > &c->ip4.guest_gw, sizeof(c->ip4.guest_gw)); > @@ -430,6 +443,7 @@ int dhcp(const struct ctx *c, struct iov_tail *data) > > if (c->mtu) { > opts[26].slen = 2; > + opts[26].state = OPT_DEFAULT; > opts[26].s[0] = c->mtu / 256; > opts[26].s[1] = c->mtu % 256; > } > @@ -441,12 +455,15 @@ int dhcp(const struct ctx *c, struct iov_tail *data) > ((struct in_addr *)opts[6].s)[i] = c->ip4.dns[i]; > opts[6].slen += sizeof(uint32_t); > } > - if (!opts[6].slen) > - opts[6].slen = -1; > + if (opts[6].slen) > + opts[6].state = OPT_DEFAULT; > + else > + opts[6].state = OPT_UNSET; As Laurent mentioned for the loop setting everything to OPT_UNSET: no need for this. You already set OPT_UNSET to 0 to indicate the initial mode. > > opt_len = strlen(c->hostname); > if (opt_len > 0) { > opts[12].slen = opt_len; > + opts[12].state = OPT_DEFAULT; > memcpy(opts[12].s, &c->hostname, opt_len); > } > > @@ -463,6 +480,7 @@ int dhcp(const struct ctx *c, struct iov_tail *data) > encode_domain_name((char *)opts[81].s + 3, c->fqdn); > > opts[81].slen = opt_len; > + opts[81].state = OPT_DEFAULT; > } else { > debug("DHCP: client FQDN option doesn't fit, skipping"); > } -- Stefano