[PATCH 1/4] net: add ethaddr sequence handling

Ahmad Fatoum a.fatoum at pengutronix.de
Tue Nov 26 07:17:41 PST 2024


Instead of storing every MAC address in full, boards may elect
to store a base address and increment it N times. Add a helper
to iterate over such an Ethernet address sequence. Example usage:

  const u8 *eth_addr;
  u8 eth_base[ETH_ALEN] = "\x00\x01\x02\x03\x04\x05";
  int i = 0, eth_count = 4;

  for_each_seq_ethaddr(eth_addr, eth_base, &eth_count)
          eth_register_ethaddr(i++, eth_addr);

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 include/net.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/include/net.h b/include/net.h
index a04ed5b0ab92..1b6995b50043 100644
--- a/include/net.h
+++ b/include/net.h
@@ -516,6 +516,18 @@ static inline void eth_addr_add(u8 *addr, long offset)
 	u64_to_ether_addr(u, addr);
 }
 
+#define for_each_seq_ethaddr(eth_addr, eth_base, eth_count) \
+	for (eth_addr = *eth_count ? eth_base : NULL; eth_addr; eth_addr = eth_addr_seq_next(eth_base, eth_count))
+
+static inline const u8 *eth_addr_seq_next(u8 eth_base[6], unsigned *eth_count)
+{
+	if (--(*eth_count) <= 0)
+		return NULL;
+
+	eth_addr_inc(eth_base);
+	return eth_base;
+}
+
 typedef void rx_handler_f(void *ctx, char *packet, unsigned int len);
 
 struct eth_device *eth_get_byname(const char *name);
-- 
2.39.5




More information about the barebox mailing list