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=CXrXmc4A; 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 6C4105A0274 for ; Sun, 22 Feb 2026 18:44:55 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1771782294; 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=DZfqztm4kPKsDKR/+y92qnyYzzZ/FQdcEggXItb5re4=; b=CXrXmc4Ay6/LegOmcU/uByF/QyjFK92bXp0FNX2bwksQmumHftXaQcA3S5yLqdz3437qJs q6OoK3oXpFYFmW88iM3QluRI6h8lqnBSkfAxSWNMHkB8ZR3D3ElzvTmoihXCWWeR7wSAMc F1Pb8Ypabb6HA6QJ5H7XCaYP+Zg5Ix8= Received: from mx-prod-mc-08.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-154-m00hG4YoPSSDEBPO0q44sQ-1; Sun, 22 Feb 2026 12:44:52 -0500 X-MC-Unique: m00hG4YoPSSDEBPO0q44sQ-1 X-Mimecast-MFC-AGG-ID: m00hG4YoPSSDEBPO0q44sQ_1771782291 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-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 34E1218004A9; Sun, 22 Feb 2026 17:44:51 +0000 (UTC) Received: from jmaloy-thinkpadp16vgen1.rmtcaqc.csb (unknown [10.22.64.49]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 807A91800370; Sun, 22 Feb 2026 17:44:49 +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 v5 02/13] ip: Introduce for_each_addr() macro for address iteration Date: Sun, 22 Feb 2026 12:44:34 -0500 Message-ID: <20260222174445.743845-3-jmaloy@redhat.com> In-Reply-To: <20260222174445.743845-1-jmaloy@redhat.com> References: <20260222174445.743845-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: BvwXUrOmx9n7IZSw09XRhSbXnR0_sS9Ar-yXbrAJ3do_1771782291 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: JLGL53WTRVISALU6Y46M6DAS4VNW4EYJ X-Message-ID-Hash: JLGL53WTRVISALU6Y46M6DAS4VNW4EYJ 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 1aa71f0..bb56998 100644 --- a/passt.h +++ b/passt.h @@ -344,6 +344,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