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=STle/GFx; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by passt.top (Postfix) with ESMTPS id 7B2065A0272 for ; Tue, 17 Feb 2026 23:18:25 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1771366704; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KKNiQ+jaWYomeS9uUtpgnCbV2hAUBJaVOwyr9UycL3U=; b=STle/GFxUEhjmzCIsNA3r0keQlIovJNiITgKewsysLr7y5flkkqWgkLcAOMpnTfqPAFIGw HNSMJgfVqlMbM06FMZX98rVwkxujabycmA/bcZj9pzGcPSgqn2LG/CGknXu4lWU+ela6Pv wPOlpN0Bqt4a8C7EWqbk4VrnkO6zKUM= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-588-VntWfTRlPr-WVEBfPqJTLQ-1; Tue, 17 Feb 2026 17:18:20 -0500 X-MC-Unique: VntWfTRlPr-WVEBfPqJTLQ-1 X-Mimecast-MFC-AGG-ID: VntWfTRlPr-WVEBfPqJTLQ_1771366700 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0B307180025A; Tue, 17 Feb 2026 22:18:20 +0000 (UTC) Received: from jmaloy-thinkpadp16vgen1.rmtcaqc.csb (unknown [10.22.80.48]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id B31A51800349; Tue, 17 Feb 2026 22:18:18 +0000 (UTC) From: Jon Maloy To: sbrivio@redhat.com, dgibson@redhat.com, david@gibson.dropbear.id.au, jmaloy@redhat.com, passt-dev@passt.top Subject: [PATCH v4 02/12] ip: Introduce for_each_addr() macro for address iteration Date: Tue, 17 Feb 2026 17:18:04 -0500 Message-ID: <20260217221814.4053583-3-jmaloy@redhat.com> In-Reply-To: <20260217221814.4053583-1-jmaloy@redhat.com> References: <20260217221814.4053583-1-jmaloy@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: ojXtAu-NlveVgFBHSD8ZDk8rlT764xUTSF_gAzTl4es_1771366700 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: F6C2WZJCWH6J5TZAUX2I7CEZXMSAVLGT X-Message-ID-Hash: F6C2WZJCWH6J5TZAUX2I7CEZXMSAVLGT X-MailFrom: jmaloy@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 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: Add the for_each_addr() macro to iterate over addresses in the unified array. The macro supports an address family filter parameter (AF_INET, AF_INET6, or 0 for all) using a _next_addr_idx() helper function to skip non-matching entries. Signed-off-by: Jon Maloy --- v1: - Broke out as separate commit - I kept the third argument, despite David's comment, since I still find it practical. If we want to iterate the list without filter we can just use AF_UNSPEC (== 0) --- passt.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/passt.h b/passt.h index 2a78ff5..15d6596 100644 --- a/passt.h +++ b/passt.h @@ -342,6 +342,40 @@ static inline int inany_prefix4(const struct inany_addr_entry *e) return e->prefix_len - 96; } +/** + * _next_addr_idx() - Find next address index matching family filter + * @c: Pointer to struct ctx + * @i: Starting index + * @af: Address family filter: AF_INET, AF_INET6, or 0 for all + * + * Return: next matching index, or addr_count if none found + */ +static inline int _next_addr_idx(const struct ctx *c, int i, sa_family_t af) +{ + for (; i < c->addr_count; i++) { + sa_family_t entry_af; + + entry_af = inany_v4(&c->addrs[i].addr) ? AF_INET : AF_INET6; + + if (!af || af == entry_af) + return i; + } + return i; +} + +/** + * for_each_addr() - Iterate over addresses in unified array + * @e: Pointer variable for current entry (struct inany_addr_entry *) + * @c: Pointer to struct ctx + * @af: Address family filter: AF_INET, AF_INET6, or 0 for all + * + * Note: @_i is the internal loop counter, uses _next_addr_idx() helper + */ +#define for_each_addr(e, c, af) \ + for (int _i = _next_addr_idx((c), 0, (af)); \ + _i < (c)->addr_count && ((e) = &(c)->addrs[_i], true); \ + _i = _next_addr_idx((c), _i + 1, (af))) + void proto_update_l2_buf(const unsigned char *eth_d); #endif /* PASST_H */ -- 2.52.0