Hi,

Nice work! I like the usage of ether_gen_addr for user friendliness when no MAC 
is known.

I did some work on ether_gen_addr in the past and back then I had a situation 
that an ifp was not (yet) available so I added ether_gen_addr_byname. Somebody 
(I can look it up if needed) agreed on this and said that ether_gen_addr using 
ifp is actually a layering violation.
So you might take a look if it is easy to pass in the interface name and call 
ether_gen_addr_byname instead of using the ifp.

Regards,
Ronald.


Van: Michal Meloun <[email protected]>
Datum: maandag, 13 juli 2026 10:36
Aan: [email protected], [email protected], 
[email protected]
Onderwerp: git: 920518cf2e75 - main - FDT: implement fdt_ether_get_addr()

The branch main has been updated by mmel:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=920518cf2e75012135104886d92814c048baa2fb

commit 920518cf2e75012135104886d92814c048baa2fb
Author:     Michal Meloun <[email protected]>
AuthorDate: 2026-07-13 08:34:22 +0000
Commit:     Michal Meloun <[email protected]>
CommitDate: 2026-07-13 08:35:47 +0000

    FDT: implement fdt_ether_get_addr()
Introduce fdt_ether_get_addr() in fdt_common.c/h that tries standard
    DT properties in the correct order and falls back to a random address
    when needed. This should be used by ethernet drivers instead of open-coding
    the same logic.
MFC after: 2 weeks Reviewed by: mhorne, adrian, bz, jrtc27
    Differential Revision:  https://reviews.freebsd.org/D58104
---
 sys/dev/fdt/fdt_common.c | 40 ++++++++++++++++++++++++++++++++++++++++
 sys/dev/fdt/fdt_common.h |  4 ++++
 2 files changed, 44 insertions(+)

diff --git a/sys/dev/fdt/fdt_common.c b/sys/dev/fdt/fdt_common.c
index f43551c6310e..93897b598b30 100644
--- a/sys/dev/fdt/fdt_common.c
+++ b/sys/dev/fdt/fdt_common.c
@@ -553,3 +553,43 @@ fdt_get_chosen_bootargs(char *bootargs, size_t max_size)
        return (ENXIO);
    return (0);
 }
+
+/*
+ * fdt_ether_get_addr - Get ethernet MAC address using FDT properties.
+ * Order:
+ *   1. "mac-address"
+ *   2. "local-mac-address"
+ *   3. Keep pre-initialized value (if valid)
+ *   4. random ether_gen_addr() as last resort
+ */
+void
+fdt_ether_get_addr(phandle_t node, struct ifnet *ifp, uint8_t *eaddr)
+{
+   uint8_t tmp[ETHER_ADDR_LEN];
+   int len;
+
+   /* Firstly try "mac-address" */
+   len = OF_getprop(node, "mac-address", tmp, sizeof(tmp));
+   if (len == ETHER_ADDR_LEN && !ETHER_IS_ZERO(tmp) &&
+       !ETHER_IS_MULTICAST(tmp)) {
+       bcopy(tmp, eaddr, ETHER_ADDR_LEN);
+       return;
+   }
+
+   /* Next use  "local-mac-address" */
+   len = OF_getprop(node, "local-mac-address", tmp, sizeof(tmp));
+   if (len == ETHER_ADDR_LEN && !ETHER_IS_ZERO(tmp) &&
+       !ETHER_IS_MULTICAST(tmp)) {
+       bcopy(tmp, eaddr, ETHER_ADDR_LEN);
+       return;
+   }
+
+   /* Next use existing one if set */
+   if (!ETHER_IS_ZERO(eaddr) && !ETHER_IS_MULTICAST(eaddr))
+       return;
+
+   /* Last resort - generate random one */
+   ether_gen_addr(ifp, (struct ether_addr *)eaddr);
+
+   return;
+}
diff --git a/sys/dev/fdt/fdt_common.h b/sys/dev/fdt/fdt_common.h
index f597233f9771..3eee6913ad72 100644
--- a/sys/dev/fdt/fdt_common.h
+++ b/sys/dev/fdt/fdt_common.h
@@ -33,8 +33,11 @@
#include <sys/sysctl.h>
 #include <sys/slicer.h>
+#include <sys/socket.h>
 #include <contrib/libfdt/libfdt_env.h>
 #include <dev/ofw/ofw_bus.h>
+#include <net/ethernet.h>
+#include <net/if.h>
#define FDT_MEM_REGIONS 64 @@ -70,6 +73,7 @@ typedef void (*fdt_mem_region_cb)(const struct mem_region *, void *);
 int fdt_addrsize_cells(phandle_t, int *, int *);
 u_long fdt_data_get(const void *, int);
 int fdt_data_to_res(const pcell_t *, int, int, u_long *, u_long *);
+void fdt_ether_get_addr(phandle_t node, struct ifnet *ifp, uint8_t *eaddr);
 phandle_t fdt_find_compatible(phandle_t, const char *, int);
 phandle_t fdt_depth_search_compatible(phandle_t, const char *, int);
 int fdt_foreach_mem_region(fdt_mem_region_cb, void *);



Reply via email to