diff --git a/drivers/slip/slip.c b/drivers/slip/slip.c index e958f5388cb..3f8cfec7cb1 100644 --- a/drivers/slip/slip.c +++ b/drivers/slip/slip.c @@ -434,10 +434,12 @@ static struct slip_context slip_context_data; #if defined(CONFIG_SLIP_TAP) && defined(CONFIG_NET_L2_ETHERNET) #define _SLIP_L2_LAYER ETHERNET_L2 +#define _SLIP_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(ETHERNET_L2) #else #define _SLIP_L2_LAYER DUMMY_L2 +#define _SLIP_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(DUMMY_L2) #endif NET_DEVICE_INIT(slip, CONFIG_SLIP_DRV_NAME, slip_init, &slip_context_data, NULL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &slip_if_api, - _SLIP_L2_LAYER, 127); + _SLIP_L2_LAYER, _SLIP_L2_CTX_TYPE, 127); diff --git a/include/linker/common-ram.ld b/include/linker/common-ram.ld index fc96c2cfafe..ac500b75ab9 100644 --- a/include/linker/common-ram.ld +++ b/include/linker/common-ram.ld @@ -173,6 +173,14 @@ __net_if_end = .; } GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION) + SECTION_DATA_PROLOGUE(net_l2_data, (OPTIONAL),) + { + __net_l2_data_start = .; + *(".net_l2.data") + KEEP(*(SORT_BY_NAME(".net_l2.data*"))) + __net_l2_data_end = .; + } GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION) + SECTION_DATA_PROLOGUE(net_nbr, (OPTIONAL),) { __net_nbr_start = .; diff --git a/include/linker/common-rom.ld b/include/linker/common-rom.ld index 873433695c1..5ce0ea063b9 100644 --- a/include/linker/common-rom.ld +++ b/include/linker/common-rom.ld @@ -33,3 +33,18 @@ __devconfig_end = .; } GROUP_LINK_IN(ROMABLE_REGION) + SECTION_PROLOGUE(net_l2_init, (OPTIONAL),) + { + __net_l2_start = .; + *(".net_l2.init") + KEEP(*(SORT_BY_NAME(".net_l2.init*"))) + __net_l2_end = .; + } GROUP_LINK_IN(ROMABLE_REGION) + + SECTION_PROLOGUE(net_l2, (OPTIONAL),) + { + __net_l2_start = .; + *(".net_l2.init") + KEEP(*(SORT_BY_NAME(".net_l2.init*"))) + __net_l2_end = .; + } GROUP_LINK_IN(ROMABLE_REGION) diff --git a/include/net/yaip/net_if.h b/include/net/yaip/net_if.h index 40137316890..4f7b9d69b49 100644 --- a/include/net/yaip/net_if.h +++ b/include/net/yaip/net_if.h @@ -150,6 +150,9 @@ struct net_if { /** Interface's L2 layer */ const struct net_l2 const *l2; + /** Interfaces's private L2 data pointer */ + void *l2_data; + /** The hardware link address */ struct net_linkaddr link_addr; @@ -264,6 +267,18 @@ static inline uint16_t net_if_get_ll_reserve(struct net_if *iface, return iface->l2->reserve(iface, (void *)dst_ip6); } +/** + * @brief Get a pointer on L2's private data + * + * @param iface a valid pointer to a network interface structure + * + * @return a pointer on the iface's l2 data + */ +static inline void *net_if_l2_data(struct net_if *iface) +{ + return iface->l2_data; +} + /** * @brief Get an network interface's device * @@ -865,22 +880,26 @@ struct net_if_api { }; #define NET_IF_GET_NAME(dev_name, sfx) (__net_if_##dev_name_##sfx) -#define NET_IF_GET(dev_name, sfx) (&NET_IF_GET_NAME(dev_name, sfx)) +#define NET_IF_GET(dev_name, sfx) \ + ((struct net_if *)&NET_IF_GET_NAME(dev_name, sfx)) #define NET_IF_INIT(dev_name, sfx, _l2, _mtu) \ static struct net_if (NET_IF_GET_NAME(dev_name, sfx)) __used \ __attribute__((__section__(".net_if.data"))) = { \ .dev = &(__device_##dev_name), \ .l2 = &(NET_L2_GET_NAME(_l2)), \ + .l2_data = &(NET_L2_GET_DATA(dev_name, sfx)), \ .mtu = _mtu, \ } /* Network device intialization macro */ #define NET_DEVICE_INIT(dev_name, drv_name, init_fn, \ - data, cfg_info, prio, api, l2, mtu) \ + data, cfg_info, prio, api, l2, \ + l2_ctx_type, mtu) \ DEVICE_AND_API_INIT(dev_name, drv_name, init_fn, data, \ cfg_info, NANOKERNEL, prio, api); \ + NET_L2_DATA_INIT(dev_name, 0, l2_ctx_type); \ NET_IF_INIT(dev_name, 0, l2, mtu) #ifdef __cplusplus diff --git a/include/net/yaip/net_l2.h b/include/net/yaip/net_l2.h index 2109beb3017..735bf497fc1 100644 --- a/include/net/yaip/net_l2.h +++ b/include/net/yaip/net_l2.h @@ -55,16 +55,19 @@ struct net_l2 { #define NET_L2_GET_NAME(_name) (__net_l2_##_name) #define NET_L2_DECLARE_PUBLIC(_name) \ extern const struct net_l2 const NET_L2_GET_NAME(_name) +#define NET_L2_GET_CTX_TYPE(_name) _name##_CTX_TYPE extern struct net_l2 __net_l2_start[]; #ifdef CONFIG_NET_L2_DUMMY -#define DUMMY_L2 dummy +#define DUMMY_L2 DUMMY +#define DUMMY_L2_CTX_TYPE void* NET_L2_DECLARE_PUBLIC(DUMMY_L2); #endif /* CONFIG_NET_L2_DUMMY */ #ifdef CONFIG_NET_L2_ETHERNET -#define ETHERNET_L2 ethernet +#define ETHERNET_L2 ETHERNET +#define ETHERNET_L2_CTX_TYPE void* NET_L2_DECLARE_PUBLIC(ETHERNET_L2); #endif /* CONFIG_NET_L2_ETHERNET */ @@ -78,6 +81,12 @@ extern struct net_l2 __net_l2_end[]; .reserve = (_reserve_fn), \ } +#define NET_L2_GET_DATA(name, sfx) (__net_l2_data_##name##sfx) + +#define NET_L2_DATA_INIT(name, sfx, ctx_type) \ + static ctx_type NET_L2_GET_DATA(name, sfx) __used \ + __attribute__((__section__(".net_l2.data"))); + #ifdef __cplusplus } #endif diff --git a/tests/net/6lo/src/main.c b/tests/net/6lo/src/main.c index f12b70f8796..2bc2fab15dd 100644 --- a/tests/net/6lo/src/main.c +++ b/tests/net/6lo/src/main.c @@ -154,7 +154,7 @@ static struct net_if_api net_6lo_if_api = { NET_DEVICE_INIT(net_6lo_test, "net_6lo_test", net_6lo_dev_init, NULL, NULL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, - &net_6lo_if_api, DUMMY_L2, 127); + &net_6lo_if_api, DUMMY_L2, NET_L2_GET_CTX_TYPE(DUMMY_L2), 127); static bool compare_data(struct net_buf *buf, struct net_6lo_data *data) { diff --git a/tests/net/arp/src/main.c b/tests/net/arp/src/main.c index b14f80254c7..9411788a50a 100644 --- a/tests/net/arp/src/main.c +++ b/tests/net/arp/src/main.c @@ -314,14 +314,16 @@ static struct net_if_api net_arp_if_api = { #if defined(CONFIG_NET_ARP) && defined(CONFIG_NET_L2_ETHERNET) #define _ETH_L2_LAYER ETHERNET_L2 +#define _ETH_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(ETHERNET_L2) #else #define _ETH_L2_LAYER DUMMY_L2 +#define _ETH_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(DUMMY_L2) #endif NET_DEVICE_INIT(net_arp_test, "net_arp_test", net_arp_dev_init, &net_arp_context_data, NULL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, - &net_arp_if_api, _ETH_L2_LAYER, 127); + &net_arp_if_api, _ETH_L2_LAYER, _ETH_L2_CTX_TYPE, 127); void main_fiber(void) { diff --git a/tests/net/ip-addr/src/main.c b/tests/net/ip-addr/src/main.c index 52048b6cb7d..e4ee33c2cb6 100644 --- a/tests/net/ip-addr/src/main.c +++ b/tests/net/ip-addr/src/main.c @@ -159,11 +159,12 @@ static struct net_if_api net_test_if_api = { }; #define _ETH_L2_LAYER DUMMY_L2 +#define _ETH_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(DUMMY_L2) NET_DEVICE_INIT(net_addr_test, "net_addr_test", net_test_init, &net_test_context_data, NULL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, - &net_test_if_api, _ETH_L2_LAYER, 127); + &net_test_if_api, _ETH_L2_LAYER, _ETH_L2_CTX_TYPE, 127); #ifdef CONFIG_MICROKERNEL void mainloop(void) diff --git a/tests/net/udp/src/main.c b/tests/net/udp/src/main.c index a93901ee61f..4421c520adf 100644 --- a/tests/net/udp/src/main.c +++ b/tests/net/udp/src/main.c @@ -124,11 +124,12 @@ static struct net_if_api net_udp_if_api = { }; #define _ETH_L2_LAYER DUMMY_L2 +#define _ETH_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(DUMMY_L2) NET_DEVICE_INIT(net_udp_test, "net_udp_test", net_udp_dev_init, &net_udp_context_data, NULL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, - &net_udp_if_api, _ETH_L2_LAYER, 127); + &net_udp_if_api, _ETH_L2_LAYER, _ETH_L2_CTX_TYPE, 127); struct ud { const struct sockaddr *remote_addr;