1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-30 00:41:17 +01:00

Merge pull request #11879 from jia200x/pr/netif_new_interface

netif: introduce generic network interface descriptor
This commit is contained in:
Leandro Lanzieri 2019-10-11 12:05:12 +02:00 committed by GitHub
commit 760da795a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 136 additions and 120 deletions

View File

@ -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

View File

@ -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))))

View File

@ -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)))

View File

@ -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 */

View File

@ -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 <m.lenders@fu-berlin.de>
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @author José Ignacio Alamos <jose.alamos@haw-hamburg.de>
*/
#ifndef NET_NETIF_H
#define NET_NETIF_H
#include "net/netopt.h"
#include <stdint.h>
#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

View File

@ -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);
}
/** @} */

View File

@ -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,

View File

@ -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 <m.lenders@fu-berlin.de>
*/
#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 */
/** @} */

3
sys/net/netif/Makefile Normal file
View File

@ -0,0 +1,3 @@
MODULE = netif
include $(RIOTBASE)/Makefile.base

60
sys/net/netif/netif.c Normal file
View File

@ -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 <jose.alamos@haw-hamburg.de>
*/
#include <string.h>
#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;
}
/** @} */

View File

@ -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

View File

@ -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);