* [PATCH 1/4] net: add ethaddr sequence handling
2024-11-26 15:17 [PATCH 0/4] net: ethernet address helpers for board code Ahmad Fatoum
@ 2024-11-26 15:17 ` Ahmad Fatoum
2024-11-26 15:17 ` [PATCH 2/4] net: factor out eth_of_get_fixup_node Ahmad Fatoum
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2024-11-26 15:17 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
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, ð_count)
eth_register_ethaddr(i++, eth_addr);
Signed-off-by: Ahmad Fatoum <a.fatoum@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
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/4] net: factor out eth_of_get_fixup_node
2024-11-26 15:17 [PATCH 0/4] net: ethernet address helpers for board code Ahmad Fatoum
2024-11-26 15:17 ` [PATCH 1/4] net: add ethaddr sequence handling Ahmad Fatoum
@ 2024-11-26 15:17 ` Ahmad Fatoum
2024-11-26 15:17 ` [PATCH 3/4] net: export list of registered ethernet addresses Ahmad Fatoum
2024-11-26 15:17 ` [PATCH 4/4] net: implement ethaddr_string_cmp() Ahmad Fatoum
3 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2024-11-26 15:17 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
We only know at fixup time, which MAC addresses were actually assigned
to interfaces. Factor out eth_of_get_fixup_node from the MAC address
assignment code, so board code can use this to retrace which MAC
addresses would be fixed up.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
include/net.h | 12 ++++++++++++
net/eth.c | 29 +++++++++++++++++++----------
2 files changed, 31 insertions(+), 10 deletions(-)
diff --git a/include/net.h b/include/net.h
index 1b6995b50043..4d4996c720ef 100644
--- a/include/net.h
+++ b/include/net.h
@@ -128,6 +128,18 @@ static inline void of_eth_register_ethaddr(struct device_node *node,
void eth_register_ethaddr(int ethid, const char *ethaddr);
void of_eth_register_ethaddr(struct device_node *node, const char *ethaddr);
#endif
+
+#ifdef CONFIG_OFTREE
+struct device_node *eth_of_get_fixup_node(struct device_node *root,
+ const char *node_path, int ethid);
+#else
+static inline struct device_node *eth_of_get_fixup_node(struct device_node *root,
+ const char *node_path, int ethid)
+{
+ return NULL;
+}
+#endif
+
/*
* Ethernet header
*/
diff --git a/net/eth.c b/net/eth.c
index 7229530c055b..a1d6860be830 100644
--- a/net/eth.c
+++ b/net/eth.c
@@ -319,19 +319,11 @@ static int eth_param_set_ethaddr(struct param_d *param, void *priv)
}
#ifdef CONFIG_OFTREE
-static void eth_of_fixup_node(struct device_node *root,
- const char *node_path, int ethid,
- const u8 ethaddr[ETH_ALEN])
+struct device_node *eth_of_get_fixup_node(struct device_node *root,
+ const char *node_path, int ethid)
{
struct device_node *bb_node, *fixup_node;
char *name;
- int ret;
-
- if (!is_valid_ether_addr(ethaddr)) {
- pr_debug("%s: no valid mac address, cannot fixup\n",
- __func__);
- return;
- }
if (node_path) {
bb_node = of_find_node_by_path_from(0, node_path);
@@ -344,6 +336,23 @@ static void eth_of_fixup_node(struct device_node *root,
fixup_node = of_find_node_by_alias(root, eth);
}
+ return fixup_node;
+}
+
+static void eth_of_fixup_node(struct device_node *root,
+ const char *node_path, int ethid,
+ const u8 ethaddr[ETH_ALEN])
+{
+ struct device_node *fixup_node;
+ int ret;
+
+ if (!is_valid_ether_addr(ethaddr)) {
+ pr_debug("%s: no valid mac address, cannot fixup\n",
+ __func__);
+ return;
+ }
+
+ fixup_node = eth_of_get_fixup_node(root, node_path, ethid);
if (!fixup_node) {
pr_debug("%s: no node to fixup\n", __func__);
return;
--
2.39.5
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 3/4] net: export list of registered ethernet addresses
2024-11-26 15:17 [PATCH 0/4] net: ethernet address helpers for board code Ahmad Fatoum
2024-11-26 15:17 ` [PATCH 1/4] net: add ethaddr sequence handling Ahmad Fatoum
2024-11-26 15:17 ` [PATCH 2/4] net: factor out eth_of_get_fixup_node Ahmad Fatoum
@ 2024-11-26 15:17 ` Ahmad Fatoum
2024-11-26 15:17 ` [PATCH 4/4] net: implement ethaddr_string_cmp() Ahmad Fatoum
3 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2024-11-26 15:17 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
Network stack keeps an ethaddr_list of Ethernet addresses that were
registered by drivers and board code.
The list currently lacks ethernet devices for which a net_device already
existed. If a net_device is added later on, the list is consulted, but
the element is not removed.
This means ethaddr_list is "the list of registered Ethernet addresses
that were added before the relevant device has been probed". Make it more
useful by genericising it to be "the list of registered Ethernet addresses".
This introduces no functional change (only consumer is fixup code that
handles both cases already), but it allows future board code to consult
the list to check which Ethernet addresses were actually assigned.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
include/net.h | 9 +++++++++
net/eth.c | 11 ++---------
2 files changed, 11 insertions(+), 9 deletions(-)
diff --git a/include/net.h b/include/net.h
index 4d4996c720ef..d9b7953a3c33 100644
--- a/include/net.h
+++ b/include/net.h
@@ -625,4 +625,13 @@ void ifdown_all(void);
extern struct class eth_class;
+struct eth_ethaddr {
+ struct list_head list;
+ u8 ethaddr[ETH_ALEN];
+ int ethid;
+ struct device_node *node;
+};
+
+extern struct list_head ethaddr_list;
+
#endif /* __NET_H__ */
diff --git a/net/eth.c b/net/eth.c
index a1d6860be830..3714dde44026 100644
--- a/net/eth.c
+++ b/net/eth.c
@@ -23,14 +23,7 @@
DEFINE_DEV_CLASS(eth_class, "eth");
-struct eth_ethaddr {
- struct list_head list;
- u8 ethaddr[ETH_ALEN];
- int ethid;
- struct device_node *node;
-};
-
-static LIST_HEAD(ethaddr_list);
+LIST_HEAD(ethaddr_list);
int eth_set_promisc(struct eth_device *edev, bool enable)
{
@@ -102,7 +95,7 @@ void eth_register_ethaddr(int ethid, const char *ethaddr)
for_each_netdev(edev) {
if (edev->dev.id == ethid) {
register_preset_mac_address(edev, ethaddr);
- return;
+ break;
}
}
--
2.39.5
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 4/4] net: implement ethaddr_string_cmp()
2024-11-26 15:17 [PATCH 0/4] net: ethernet address helpers for board code Ahmad Fatoum
` (2 preceding siblings ...)
2024-11-26 15:17 ` [PATCH 3/4] net: export list of registered ethernet addresses Ahmad Fatoum
@ 2024-11-26 15:17 ` Ahmad Fatoum
3 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2024-11-26 15:17 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
We keep ethernet addresses either in binary or in text form at a couple
of places, e.g. device tree, device parameters, struct net_device,
... etc. We have memcmp() and strcmp() respectively to compare each, add
one more function to compare a text string with binary. This will be
used in a follow up commit.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
include/net.h | 2 ++
net/lib.c | 17 +++++++++++++++++
2 files changed, 19 insertions(+)
diff --git a/include/net.h b/include/net.h
index d9b7953a3c33..dbc45b806a51 100644
--- a/include/net.h
+++ b/include/net.h
@@ -366,6 +366,8 @@ IPaddr_t getenv_ip(const char *name);
int setenv_ip(const char *name, IPaddr_t ip);
int string_to_ethaddr(const char *str, u8 enetaddr[6]);
+void ethaddr_to_string(const u8 enetaddr[6], char *str);
+int ethaddr_string_cmp(const u8 enetaddr_a[6], const char *str_b);
#ifdef CONFIG_NET_RESOLV
int resolv(const char *host, IPaddr_t *ip);
diff --git a/net/lib.c b/net/lib.c
index dc6e138f392c..59bd4c280caf 100644
--- a/net/lib.c
+++ b/net/lib.c
@@ -37,6 +37,23 @@ int string_to_ethaddr(const char *str, u8 enetaddr[ETH_ALEN])
return 0;
}
+void ethaddr_to_string(const u8 enetaddr[ETH_ALEN], char *str)
+{
+ sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
+ enetaddr[0], enetaddr[1], enetaddr[2], enetaddr[3],
+ enetaddr[4], enetaddr[5]);
+}
+
+int ethaddr_string_cmp(const u8 enetaddr_a[ETH_ALEN], const char *str_b)
+{
+ u8 enetaddr_b[ETH_ALEN];
+
+ if (string_to_ethaddr(str_b, enetaddr_b))
+ return -EINVAL;
+
+ return memcmp(enetaddr_a, enetaddr_b, ETH_ALEN);
+}
+
int string_to_ip(const char *s, IPaddr_t *ip)
{
IPaddr_t addr = 0;
--
2.39.5
^ permalink raw reply [flat|nested] 5+ messages in thread