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 <jukka.rissanen@nordicsemi.no>
This commit is contained in:
Jukka Rissanen 2023-11-15 17:42:21 +02:00 committed by Carles Cufí
parent 3f891ced3a
commit dd2a222086
2 changed files with 24 additions and 0 deletions

View file

@ -3022,6 +3022,20 @@ struct net_if_api {
#define NET_DEVICE_DT_INST_OFFLOAD_DEFINE(inst, ...) \ #define NET_DEVICE_DT_INST_OFFLOAD_DEFINE(inst, ...) \
NET_DEVICE_DT_OFFLOAD_DEFINE(DT_DRV_INST(inst), __VA_ARGS__) 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 #ifdef __cplusplus
} }
#endif #endif

View file

@ -4876,6 +4876,16 @@ void net_if_init(void)
goto out; 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_ipv6_init(if_count);
iface_ipv4_init(if_count); iface_ipv4_init(if_count);
iface_router_init(); iface_router_init();