From dd2a222086bcd6a633d4247ab066da3a659b699f Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Wed, 15 Nov 2023 17:42:21 +0200 Subject: [PATCH] net: if: Add helper to calculate number of interfaces Add a helper macro that can be used at runtime to return the number of network interfaces in the system. Signed-off-by: Jukka Rissanen --- include/zephyr/net/net_if.h | 14 ++++++++++++++ subsys/net/ip/net_if.c | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/zephyr/net/net_if.h b/include/zephyr/net/net_if.h index ab056d05e15..788c3316391 100644 --- a/include/zephyr/net/net_if.h +++ b/include/zephyr/net/net_if.h @@ -3022,6 +3022,20 @@ struct net_if_api { #define NET_DEVICE_DT_INST_OFFLOAD_DEFINE(inst, ...) \ NET_DEVICE_DT_OFFLOAD_DEFINE(DT_DRV_INST(inst), __VA_ARGS__) +/** + * @brief Count the number of network interfaces. + * + * @param[out] _dst Pointer to location where result is written. + */ +#define NET_IFACE_COUNT(_dst) \ + do { \ + extern struct net_if _net_if_list_start[]; \ + extern struct net_if _net_if_list_end[]; \ + *(_dst) = ((uintptr_t)_net_if_list_end - \ + (uintptr_t)_net_if_list_start) / \ + sizeof(struct net_if); \ + } while (0) + #ifdef __cplusplus } #endif diff --git a/subsys/net/ip/net_if.c b/subsys/net/ip/net_if.c index dbf20af3cc0..00c9c74495d 100644 --- a/subsys/net/ip/net_if.c +++ b/subsys/net/ip/net_if.c @@ -4876,6 +4876,16 @@ void net_if_init(void) goto out; } +#if defined(CONFIG_ASSERT) + /* Do extra check that verifies that interface count is properly + * done. + */ + int count_if; + + NET_IFACE_COUNT(&count_if); + NET_ASSERT(count_if == if_count); +#endif + iface_ipv6_init(if_count); iface_ipv4_init(if_count); iface_router_init();