Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

netdev_tap: make 'wired' property configurable #17709

Merged
merged 3 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cpu/native/include/netdev_tap.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern "C" {
#endif

#include <stdint.h>
#include <stdbool.h>
#include "net/netdev.h"

#include "net/ethernet/hdr.h"
Expand All @@ -43,7 +44,8 @@ typedef struct netdev_tap {
char tap_name[IFNAMSIZ]; /**< host dev file name */
int tap_fd; /**< host file descriptor for the TAP */
uint8_t addr[ETHERNET_ADDR_LEN]; /**< The MAC address of the TAP */
uint8_t promiscuous; /**< Flag for promiscuous mode */
bool promiscuous; /**< Flag for promiscuous mode */
bool wired; /**< Flag for wired mode */
} netdev_tap_t;

/**
Expand All @@ -52,6 +54,8 @@ typedef struct netdev_tap {
typedef struct {
char **tap_name; /**< Name of the host system's tap
interface to bind to. */
bool wired; /**< Interface should behave like a
wired interface. */
} netdev_tap_params_t;

/**
Expand Down
17 changes: 17 additions & 0 deletions cpu/native/netdev_tap/netdev_tap.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ static inline int _set_promiscuous(netdev_t *netdev, int value)
return value;
}

static inline int _get_wired(netdev_t *netdev)
{
netdev_tap_t *dev = container_of(netdev, netdev_tap_t, netdev);
return dev->wired;
}

static inline void _isr(netdev_t *netdev)
{
if (netdev->event_callback) {
Expand Down Expand Up @@ -124,6 +130,16 @@ static int _get(netdev_t *dev, netopt_t opt, void *value, size_t max_len)
*((bool*)value) = (bool)_get_promiscuous(dev);
res = sizeof(bool);
break;
case NETOPT_IS_WIRED:
if (!_get_wired(dev)) {
res = -ENOTSUP;
} else {
if (value) {
*((bool*)value) = true;
}
res = sizeof(bool);
}
break;
default:
res = netdev_eth_get(dev, opt, value, max_len);
break;
Expand Down Expand Up @@ -294,6 +310,7 @@ void netdev_tap_setup(netdev_tap_t *dev, const netdev_tap_params_t *params, int
dev->netdev.driver = &netdev_driver_tap;
strncpy(dev->tap_name, *(params->tap_name), IFNAMSIZ - 1);
dev->tap_name[IFNAMSIZ - 1] = '\0';
dev->wired = params->wired;
netdev_register(&dev->netdev, NETDEV_TAP, index);
}

Expand Down
35 changes: 25 additions & 10 deletions cpu/native/startup.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ static const char short_opts[] = ":hi:s:deEoc:"
#endif
#ifdef MODULE_PERIPH_SPIDEV_LINUX
"p:"
#endif
#ifdef MODULE_NETDEV_TAP
"w:"
#endif
"";

Expand Down Expand Up @@ -367,6 +370,11 @@ void usage_exit(int status)
" -M <eeprom> , --eeprom=<eeprom>\n"
" Specify the file path where the EEPROM content is stored\n"
" Example: --eeprom=/tmp/riot_native.eeprom\n");
#endif
#ifdef MODULE_NETDEV_TAP
real_printf(
" -w <tap>\n"
" Add a tap interface as a wireless interface\n");
#endif
real_exit(status);
}
Expand Down Expand Up @@ -468,6 +476,10 @@ __attribute__((constructor)) static void startup(int argc, char **argv, char **e
_native_id = _native_pid;

int c, opt_idx = 0, uart = 0;
#ifdef MODULE_NETDEV_TAP
unsigned taps = 0;
memset(netdev_tap_params, 0, sizeof(netdev_tap_params));
#endif
#ifdef MODULE_SOCKET_ZEP
unsigned zeps = 0;
#endif
Expand Down Expand Up @@ -575,20 +587,19 @@ __attribute__((constructor)) static void startup(int argc, char **argv, char **e
strncpy(eeprom_file, optarg, EEPROM_FILEPATH_MAX_LEN);
break;
}
#endif
#ifdef MODULE_NETDEV_TAP
case 'w':
netdev_tap_params[taps].tap_name = &argv[optind - 1];
netdev_tap_params[taps].wired = false;
++taps;
break;
#endif
default:
usage_exit(EXIT_FAILURE);
break;
}
}
#ifdef MODULE_NETDEV_TAP
for (int i = 0; i < NETDEV_TAP_MAX; i++) {
if (argv[optind + i] == NULL) {
/* no tap parameter left */
usage_exit(EXIT_FAILURE);
}
}
#endif
#ifdef MODULE_SOCKET_ZEP
if (zeps != SOCKET_ZEP_MAX) {
/* not enough ZEPs given */
Expand Down Expand Up @@ -653,8 +664,12 @@ __attribute__((constructor)) static void startup(int argc, char **argv, char **e
native_cpu_init();
native_interrupt_init();
#ifdef MODULE_NETDEV_TAP
for (int i = 0; i < NETDEV_TAP_MAX; i++) {
netdev_tap_params[i].tap_name = &argv[optind + i];
for (unsigned i = 0; taps < NETDEV_TAP_MAX; ++taps, ++i) {
if (argv[optind + i] == NULL) {
break;
}
netdev_tap_params[taps].tap_name = &argv[optind + i];
netdev_tap_params[taps].wired = true;
}
#endif

Expand Down
3 changes: 3 additions & 0 deletions pkg/lwip/init_devs/auto_init_netdev_tap.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ static netdev_tap_t netdev_taps[NETIF_TAP_NUMOF];
static void auto_init_netdev_tap(void)
{
for (unsigned i = 0; i < NETIF_TAP_NUMOF; i++) {
if (netdev_tap_params[i].tap_name == NULL) {
continue;
}
netdev_tap_setup(&netdev_taps[i], &netdev_tap_params[i], i);
if (lwip_add_ethernet(&netif[i], &netdev_taps[i].netdev) == NULL) {
DEBUG("Could not add netdev_tap device\n");
Expand Down
4 changes: 4 additions & 0 deletions sys/net/gnrc/netif/init_devs/auto_init_netdev_tap.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ void auto_init_netdev_tap(void)
for (unsigned i = 0; i < NETDEV_TAP_MAX; i++) {
const netdev_tap_params_t *p = &netdev_tap_params[i];

if (p->tap_name == NULL) {
continue;
}

LOG_DEBUG("[auto_init_netif] initializing netdev_tap #%u on TAP %s\n",
i, *(p->tap_name));

Expand Down