-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor network device handling for flexible device support
Separate network device handling to support various network backends, such as Slirp, instead of hard-coding the TAP device. The '-netdev' flag can now be used to specify the network device type, defaulting to TAP if unspecified. Additionally, packet transfer functions are now tailored to each network device, enabling more modular and extensible network device development.
- Loading branch information
Showing
6 changed files
with
229 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#include <assert.h> | ||
#include <errno.h> | ||
#include <fcntl.h> | ||
#include <linux/if.h> | ||
#include <linux/if_tun.h> | ||
#include <stdbool.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <sys/ioctl.h> | ||
|
||
#include "netdev.h" | ||
|
||
static int net_init_tap(); | ||
static int net_init_user(); | ||
|
||
static const char *netdev_driver_lookup[] = { | ||
#define _(dev) [NET_DEV_DRIVER_##dev] = #dev, | ||
SUPPORTED_DEVICES | ||
#undef _ | ||
NULL, | ||
}; | ||
|
||
static int find_net_dev_idx(const char *net_type, const char **netlookup) | ||
{ | ||
if (!net_type) | ||
return -1; | ||
int i = 0; | ||
while (netlookup[i]) { | ||
if (!strcmp(net_type, netlookup[i])) { | ||
return i; | ||
} | ||
i++; | ||
} | ||
return -1; | ||
} | ||
|
||
static int net_init_tap(netdev_t *netdev) | ||
{ | ||
net_tap_options *tap; | ||
tap = &netdev->op.tap; | ||
tap->tap_fd = open("/dev/net/tun", O_RDWR); | ||
if (tap->tap_fd < 0) { | ||
fprintf(stderr, "failed to open TAP device: %s\n", strerror(errno)); | ||
return false; | ||
} | ||
|
||
/* Specify persistent tap device */ | ||
struct ifreq ifreq = {.ifr_flags = IFF_TAP | IFF_NO_PI}; | ||
strncpy(ifreq.ifr_name, "tap%d", sizeof(ifreq.ifr_name)); | ||
if (ioctl(tap->tap_fd, TUNSETIFF, &ifreq) < 0) { | ||
fprintf(stderr, "failed to allocate TAP device: %s\n", strerror(errno)); | ||
return false; | ||
} | ||
|
||
fprintf(stderr, "allocated TAP interface: %s\n", ifreq.ifr_name); | ||
assert(fcntl(tap->tap_fd, F_SETFL, | ||
fcntl(tap->tap_fd, F_GETFL, 0) | O_NONBLOCK) >= 0); | ||
return 0; | ||
} | ||
|
||
static int net_init_user(netdev_t *netdev) | ||
{ | ||
/* TODO: create slirp dev */ | ||
return 0; | ||
} | ||
|
||
int netdev_init(netdev_t *netdev, const char *net_type) | ||
{ | ||
int dev_idx = find_net_dev_idx(net_type, netdev_driver_lookup); | ||
if (dev_idx == -1) | ||
return false; | ||
netdev->type = dev_idx; | ||
|
||
switch (dev_idx) { | ||
#define _(dev) \ | ||
case NET_DEV_DRIVER_##dev: \ | ||
net_init_##dev(netdev); \ | ||
break; | ||
SUPPORTED_DEVICES | ||
#undef _ | ||
default: | ||
fprintf(stderr, "unknown network device\n"); | ||
break; | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
/* clang-format off */ | ||
#define SUPPORTED_DEVICES \ | ||
_(tap) \ | ||
_(user) | ||
/* clang-format on */ | ||
|
||
typedef enum { | ||
#define _(dev) NET_DEV_DRIVER_##dev, | ||
SUPPORTED_DEVICES | ||
#undef _ | ||
} net_dev_driver_t; | ||
|
||
typedef struct { | ||
int tap_fd; | ||
} net_tap_options; | ||
|
||
typedef struct { | ||
/* TODO: Implement user option */ | ||
} net_user_options; | ||
|
||
typedef struct { | ||
char *name; | ||
net_dev_driver_t type; | ||
union { | ||
net_tap_options tap; | ||
net_user_options user; | ||
} op; | ||
} netdev_t; | ||
|
||
int netdev_init(netdev_t *nedtev, const char *net_type); |
Oops, something went wrong.