From 77a7aed6e6bb51a248fa826585e837abf1df58a2 Mon Sep 17 00:00:00 2001 From: Jose Alamos Date: Mon, 22 Jul 2019 13:32:39 +0200 Subject: [PATCH 1/3] netif: introduce descriptor based netif --- makefiles/pseudomodules.inc.mk | 1 - sys/Makefile | 3 ++ sys/include/net/netif.h | 62 +++++++++++++++++++++++----------- sys/net/netif/Makefile | 3 ++ sys/net/netif/netif.c | 60 ++++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 21 deletions(-) create mode 100644 sys/net/netif/Makefile create mode 100644 sys/net/netif/netif.c diff --git a/makefiles/pseudomodules.inc.mk b/makefiles/pseudomodules.inc.mk index c7beabb713..b1b2d046d6 100644 --- a/makefiles/pseudomodules.inc.mk +++ b/makefiles/pseudomodules.inc.mk @@ -53,7 +53,6 @@ PSEUDOMODULES += lora PSEUDOMODULES += mpu_stack_guard PSEUDOMODULES += nanocoap_% PSEUDOMODULES += netdev_default -PSEUDOMODULES += netif PSEUDOMODULES += netstats PSEUDOMODULES += netstats_l2 PSEUDOMODULES += netstats_ipv6 diff --git a/sys/Makefile b/sys/Makefile index 3cb3056212..91a09e7b1f 100644 --- a/sys/Makefile +++ b/sys/Makefile @@ -157,6 +157,9 @@ endif ifneq (,$(filter suit%,$(USEMODULE))) DIRS += suit endif +ifneq (,$(filter netif,$(USEMODULE))) + DIRS += net/netif +endif DIRS += $(dir $(wildcard $(addsuffix /Makefile, $(USEMODULE)))) diff --git a/sys/include/net/netif.h b/sys/include/net/netif.h index e0fedf0a65..f4593548df 100644 --- a/sys/include/net/netif.h +++ b/sys/include/net/netif.h @@ -13,11 +13,13 @@ * @brief Common network interface API * * This allows access to network interfaces regardless of the network stack - * implementation. @anchor NETIF_INVALID The network stack must provide + * implementation. The network stack must provide * - * - Both a definition for the type `netif_t` and the - * value `NETIF_INVALID` of type `netif_t` in a file `netif_types.h` and - * - implementation of all the functions defined in @ref net/netif.h + * - A definition for @p netif_get_name + * - A definition for @p netif_get_opt + * - A definition for @p netif_set_opt + * + * The network stack should also register each interface via @p netif_register. * * @{ * @@ -26,13 +28,15 @@ * * @author Martine Lenders * @author Kaspar Schleiser + * @author José Ignacio Alamos */ #ifndef NET_NETIF_H #define NET_NETIF_H -#include "net/netopt.h" +#include -#include "netif_types.h" +#include "list.h" +#include "net/netopt.h" #ifdef __cplusplus extern "C" { @@ -45,20 +49,27 @@ extern "C" { #define NETIF_NAMELENMAX (8U) #endif +/** + * @brief Network interface descriptor. + * + * @note All network interfaces should inherit from this structure. + */ +typedef struct { + list_node_t node; /**< Pointer to the next interface */ +} netif_t; + /** * @brief Iterator for the interfaces * - * Returns interface after @p last. To start use `last == NETIF_INVALID`. + * Returns interface after @p last. To start use `last == NULL`. * - * @param[in] last The previous interface. Usen `NETIF_INVALID` to start + * @param[in] last The previous interface. Use `NULL` to start * iteration. * - * @note Supposed to be implemented by the networking module - * * @return next network interface. - * @return @ref NETIF_INVALID, if there is no interface after @p last + * @return NULL, if there is no interface after @p last */ -netif_t netif_iter(netif_t last); +netif_t *netif_iter(netif_t *last); /** * @brief Gets name of an interface @@ -74,25 +85,23 @@ netif_t netif_iter(netif_t last); * hold @ref NETIF_NAMELENMAX bytes. * * @return length of @p name on success - * @return 0, if @p netif was not a valid interface. */ -int netif_get_name(netif_t netif, char *name); + +int netif_get_name(netif_t *netif, char *name); /** * @brief Gets interface by name * * @pre `name != NULL` * - * @note Supposed to be implemented by the networking module. - * * @param[in] name The name of an interface as a zero-terminated. Must not be * `NULL`. * * @return The identifier of the interface on success. - * @return @ref NETIF_INVALID if no interface is named @p name. + * @return NULL if no interface is named @p name. */ -netif_t netif_get_by_name(const char *name); +netif_t *netif_get_by_name(const char *name); /** * @brief Gets option from an interface @@ -108,7 +117,7 @@ netif_t netif_get_by_name(const char *name); * @return Number of bytes written to @p value. * @return `< 0` on error, 0 on success. */ -int netif_get_opt(netif_t netif, netopt_t opt, uint16_t context, +int netif_get_opt(netif_t *netif, netopt_t opt, uint16_t context, void *value, size_t max_len); /** @@ -125,9 +134,22 @@ int netif_get_opt(netif_t netif, netopt_t opt, uint16_t context, * @return Number of bytes used from @p value. * @return `< 0` on error, 0 on success. */ -int netif_set_opt(netif_t netif, netopt_t opt, uint16_t context, +int netif_set_opt(netif_t *netif, netopt_t opt, uint16_t context, void *value, size_t value_len); + +/** + * @brief Registers a network interface in the global interface list. + * + * @note This functions should be called when initializing an interface. + * + * @param[in] netif Interface to be registered + * + * @return 0 on success + * @return -EINVAL if @p netif is NULL. + */ +int netif_register(netif_t *netif); + #ifdef __cplusplus } #endif diff --git a/sys/net/netif/Makefile b/sys/net/netif/Makefile new file mode 100644 index 0000000000..805279bbe8 --- /dev/null +++ b/sys/net/netif/Makefile @@ -0,0 +1,3 @@ +MODULE = netif + +include $(RIOTBASE)/Makefile.base diff --git a/sys/net/netif/netif.c b/sys/net/netif/netif.c new file mode 100644 index 0000000000..d7062d81ac --- /dev/null +++ b/sys/net/netif/netif.c @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2019 HAW Hamburg + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @{ + * + * @file + * @author Jose I. Alamos + */ + +#include + +#include "errno.h" +#include "net/netif.h" +#include "utlist.h" + +static list_node_t netif_list; + +int netif_register(netif_t *netif) +{ + if(netif == NULL) { + return -EINVAL; + } + + list_add(&netif_list, &netif->node); + return 0; +} + +netif_t *netif_iter(netif_t *last) +{ + if (last == NULL) { + return (netif_t *)netif_list.next; + } + + return (netif_t *)last->node.next; +} + +netif_t *netif_get_by_name(const char *name) +{ + assert(name); + list_node_t *node = netif_list.next; + + char tmp[NETIF_NAMELENMAX]; + + while(node) { + netif_get_name((netif_t *)node, tmp); + if(strncmp(name, tmp, NETIF_NAMELENMAX) == 0) { + return (netif_t *)node; + } + node = node->next; + } + + return NULL; +} +/** @} */ From 8fd0c2b60f3bda314c3065b0086119868c0d0e27 Mon Sep 17 00:00:00 2001 From: Jose Alamos Date: Mon, 22 Jul 2019 13:35:52 +0200 Subject: [PATCH 2/3] gnrc_netif: adapt to new netif_t abstraction --- sys/Makefile.include | 4 +- sys/include/net/gnrc/netif.h | 2 + sys/net/gnrc/netif/_netif.c | 57 +++++------------------- sys/net/gnrc/netif/gnrc_netif.c | 1 + sys/net/gnrc/netif/include/netif_types.h | 36 --------------- 5 files changed, 15 insertions(+), 85 deletions(-) delete mode 100644 sys/net/gnrc/netif/include/netif_types.h diff --git a/sys/Makefile.include b/sys/Makefile.include index 5f727c4bdd..cde195f739 100644 --- a/sys/Makefile.include +++ b/sys/Makefile.include @@ -2,12 +2,10 @@ ifneq (,$(filter nhdp,$(USEMODULE))) USEMODULE_INCLUDES += $(RIOTBASE)/sys/net/routing/nhdp endif -ifneq (,$(filter gnrc_netif,$(USEMODULE))) - USEMODULE_INCLUDES += $(RIOTBASE)/sys/net/gnrc/netif/include -endif ifneq (,$(filter gnrc_sixlowpan_frag_rb,$(USEMODULE))) USEMODULE_INCLUDES += $(RIOTBASE)/sys/net/gnrc/network_layer/sixlowpan/frag endif + ifneq (,$(filter gnrc_sock,$(USEMODULE))) USEMODULE_INCLUDES += $(RIOTBASE)/sys/net/gnrc/sock/include ifneq (,$(filter gnrc_ipv6,$(USEMODULE))) diff --git a/sys/include/net/gnrc/netif.h b/sys/include/net/gnrc/netif.h index c4554deace..456b43c915 100644 --- a/sys/include/net/gnrc/netif.h +++ b/sys/include/net/gnrc/netif.h @@ -54,6 +54,7 @@ #include "net/netstats.h" #endif #include "rmutex.h" +#include "net/netif.h" #ifdef __cplusplus extern "C" { @@ -68,6 +69,7 @@ typedef struct gnrc_netif_ops gnrc_netif_ops_t; * @brief Representation of a network interface */ typedef struct { + netif_t netif; /**< network interface descriptor */ const gnrc_netif_ops_t *ops; /**< Operations of the network interface */ netdev_t *dev; /**< Network device of the network interface */ rmutex_t mutex; /**< Mutex of the interface */ diff --git a/sys/net/gnrc/netif/_netif.c b/sys/net/gnrc/netif/_netif.c index 4c534b797c..2cc0641587 100644 --- a/sys/net/gnrc/netif/_netif.c +++ b/sys/net/gnrc/netif/_netif.c @@ -23,64 +23,29 @@ #include "net/netif.h" -netif_t netif_iter(netif_t last) +int netif_get_name(netif_t *iface, char *name) { - gnrc_netif_t *netif; + gnrc_netif_t *netif = (gnrc_netif_t*) iface; - if (last == NETIF_INVALID) { - netif = gnrc_netif_iter(NULL); - } - else if ((netif = gnrc_netif_get_by_pid((kernel_pid_t)last)) != NULL) { - netif = gnrc_netif_iter(netif); - } - if (netif != NULL) { - return netif->pid; - } - else { - return NETIF_INVALID; - } -} - -int netif_get_name(netif_t iface, char *name) -{ - gnrc_netif_t *netif = gnrc_netif_get_by_pid((kernel_pid_t)iface); int res = 0; - - if (netif != NULL) { - res += fmt_str(name, "if"); - res += fmt_u16_dec(&name[res], netif->pid); - name[res] = '\0'; - } + res += fmt_str(name, "if"); + res += fmt_u16_dec(&name[res], netif->pid); + name[res] = '\0'; return res; } -netif_t netif_get_by_name(const char *name) -{ - if ((strncmp(name, "if", 2) == 0)) { - kernel_pid_t _name_pid = (kernel_pid_t)scn_u32_dec(&name[2], 2); - if (_name_pid > 0) { - gnrc_netif_t *netif = NULL; - - while ((netif = gnrc_netif_iter(netif)) != NULL) { - if (netif->pid == _name_pid) { - return netif->pid; - } - } - } - } - return NETIF_INVALID; -} - -int netif_get_opt(netif_t netif, netopt_t opt, uint16_t context, +int netif_get_opt(netif_t *netif, netopt_t opt, uint16_t context, void *value, size_t max_len) { - return gnrc_netapi_get(netif, opt, context, value, max_len); + gnrc_netif_t *iface = (gnrc_netif_t*) netif; + return gnrc_netapi_get(iface->pid, opt, context, value, max_len); } -int netif_set_opt(netif_t netif, netopt_t opt, uint16_t context, +int netif_set_opt(netif_t *netif, netopt_t opt, uint16_t context, void *value, size_t value_len) { - return gnrc_netapi_set(netif, opt, context, value, value_len); + gnrc_netif_t *iface = (gnrc_netif_t*) netif; + return gnrc_netapi_set(iface->pid, opt, context, value, value_len); } /** @} */ diff --git a/sys/net/gnrc/netif/gnrc_netif.c b/sys/net/gnrc/netif/gnrc_netif.c index 97f82f765d..357645c8af 100644 --- a/sys/net/gnrc/netif/gnrc_netif.c +++ b/sys/net/gnrc/netif/gnrc_netif.c @@ -64,6 +64,7 @@ gnrc_netif_t *gnrc_netif_create(char *stack, int stacksize, char priority, assert(netif != NULL); rmutex_init(&netif->mutex); netif->ops = ops; + netif_register((netif_t*) netif); assert(netif->dev == NULL); netif->dev = netdev; res = thread_create(stack, stacksize, priority, THREAD_CREATE_STACKTEST, diff --git a/sys/net/gnrc/netif/include/netif_types.h b/sys/net/gnrc/netif/include/netif_types.h deleted file mode 100644 index 96101e1d10..0000000000 --- a/sys/net/gnrc/netif/include/netif_types.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (C) 2018 Freie Universität Berlin - * - * This file is subject to the terms and conditions of the GNU Lesser - * General Public License v2.1. See the file LICENSE in the top level - * directory for more details. - */ - -/** - * @ingroup net_netif - * @{ - * - * @file - * @brief GNRC-specfic type definitions for @ref net_netif - * - * @author Martine Lenders - */ -#ifndef NETIF_TYPES_H -#define NETIF_TYPES_H - -#include "kernel_types.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define NETIF_INVALID (KERNEL_PID_UNDEF) /**< Invalid interface */ - -typedef kernel_pid_t netif_t; /**< GNRC-representation of a network interface */ - -#ifdef __cplusplus -} -#endif - -#endif /* NETIF_TYPES_H */ -/** @} */ From 9727f325ff6dbea8f7571eb99e99a231295c47bd Mon Sep 17 00:00:00 2001 From: Jose Alamos Date: Mon, 22 Jul 2019 13:42:35 +0200 Subject: [PATCH 3/3] tests/gnrc_netif: adapt to new netif_t representation --- tests/gnrc_ipv6_ext/Makefile | 2 +- tests/gnrc_netif/main.c | 25 ++++++++++++------------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/tests/gnrc_ipv6_ext/Makefile b/tests/gnrc_ipv6_ext/Makefile index 64a63a0c54..73c4d3af66 100644 --- a/tests/gnrc_ipv6_ext/Makefile +++ b/tests/gnrc_ipv6_ext/Makefile @@ -9,7 +9,7 @@ BOARD_INSUFFICIENT_MEMORY := arduino-duemilanove arduino-leonardo \ nucleo-f042k6 nucleo-f070rb nucleo-f072rb \ nucleo-f303k8 nucleo-f334r8 nucleo-l031k6 \ nucleo-l053r8 stm32f0discovery stm32l0538-disco \ - telosb thingy52 \ + saml10-xpro saml11-xpro telosb thingy52 \ waspmote-pro wsn430-v1_3b wsn430-v1_4 z1 # chronos, hamilton and ruuvitag boards don't support ethos BOARD_BLACKLIST := chronos hamilton ruuvitag diff --git a/tests/gnrc_netif/main.c b/tests/gnrc_netif/main.c index aa4b557a87..eeff14c860 100644 --- a/tests/gnrc_netif/main.c +++ b/tests/gnrc_netif/main.c @@ -1057,10 +1057,10 @@ static void test_netapi_set__SRC_LEN(void) static void test_netif_iter(void) { - netif_t netif = NETIF_INVALID; + netif_t *netif = NULL; int netif_count = 0; - while ((netif = netif_iter(netif)) != NETIF_INVALID) { + while ((netif = netif_iter(netif)) != NULL) { netif_count++; } TEST_ASSERT_EQUAL_INT(gnrc_netif_numof(), netif_count); @@ -1071,27 +1071,26 @@ static void test_netif_get_name(void) char exp_name[NETIF_NAMELENMAX + 1]; char name[NETIF_NAMELENMAX]; int res; - netif_t netif = netif_iter(NETIF_INVALID); + netif_t *netif = netif_iter(NULL); /* there must be at least one interface */ - TEST_ASSERT(NETIF_INVALID != netif); + TEST_ASSERT_NOT_NULL(netif); res = netif_get_name(netif, name); - sprintf(exp_name, "if%d", (int)netif); + sprintf(exp_name, "if%d", (int) ((gnrc_netif_t *)netif)->pid); TEST_ASSERT_EQUAL_INT(strlen(exp_name), res); TEST_ASSERT_EQUAL_STRING(&exp_name[0], &name[0]); - TEST_ASSERT_EQUAL_INT(0, netif_get_name(INT16_MAX, name)); } static void test_netif_get_by_name(void) { char name[NETIF_NAMELENMAX] = "6nPRK28"; - netif_t netif = netif_iter(NETIF_INVALID); + netif_t *netif = netif_iter(NULL); - TEST_ASSERT_EQUAL_INT(NETIF_INVALID, netif_get_by_name(name)); + TEST_ASSERT(netif_get_by_name(name) == NULL); /* there must be at least one interface */ - TEST_ASSERT(NETIF_INVALID != netif); + TEST_ASSERT_NOT_NULL(netif); TEST_ASSERT(netif_get_name(netif, name) > 0); - TEST_ASSERT_EQUAL_INT(netif, netif_get_by_name(name)); + TEST_ASSERT(netif == netif_get_by_name(name)); } static void test_netif_get_opt(void) @@ -1101,7 +1100,7 @@ static void test_netif_get_opt(void) uint8_t value[GNRC_NETIF_L2ADDR_MAXLEN]; TEST_ASSERT_EQUAL_INT(sizeof(exp_ethernet), - netif_get_opt((netif_t)ethernet_netif->pid, + netif_get_opt((netif_t *)ethernet_netif, NETOPT_ADDRESS, 0, &value, sizeof(value))); TEST_ASSERT_EQUAL_INT(0, memcmp(exp_ethernet, value, sizeof(exp_ethernet))); @@ -1114,7 +1113,7 @@ static void test_netif_set_opt(void) uint8_t value[] = { LA1 + 1, LA2 + 2, LA3 + 3, LA4 + 4, LA5 + 5, LA6 + 6 }; TEST_ASSERT_EQUAL_INT(sizeof(exp_ethernet), - netif_set_opt((netif_t)ethernet_netif->pid, + netif_set_opt((netif_t *)ethernet_netif, NETOPT_ADDRESS, 0, &value, sizeof(value))); TEST_ASSERT_EQUAL_INT(sizeof(value), ethernet_netif->l2addr_len); @@ -1123,7 +1122,7 @@ static void test_netif_set_opt(void) /* return addresses to previous state for further testing */ memcpy(value, exp_ethernet, sizeof(exp_ethernet)); TEST_ASSERT_EQUAL_INT(sizeof(exp_ethernet), - netif_set_opt(ethernet_netif->pid, + netif_set_opt((netif_t *)ethernet_netif, NETOPT_ADDRESS, 0, &value, sizeof(value))); TEST_ASSERT_EQUAL_INT(sizeof(value), ethernet_netif->l2addr_len);