-
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 client support
Separate network client 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 client type, defaulting to TAP if unspecified. Additionally, packet transfer functions are now tailored to each client device, enabling more modular and extensible network client development.
- Loading branch information
Showing
6 changed files
with
213 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,80 @@ | ||
#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_slirp(); | ||
|
||
static int (*const net_init_fun[NET_DEV_DRIVER_MAX])(const netdev_t *) = { | ||
[NET_DEV_DRIVER_TAP] = net_init_tap, | ||
[NET_DEV_DRIVER_USER] = net_init_slirp, | ||
}; | ||
|
||
static const char *netdev_driver_lookup[] = { | ||
[NET_DEV_DRIVER_TAP] = "tap", | ||
[NET_DEV_DRIVER_USER] = "user", | ||
}; | ||
|
||
static int find_net_dev_idx(const char *net_type, const char **netlookup) | ||
{ | ||
int i; | ||
if (!net_type) | ||
return -1; | ||
for (i = 0; i < NET_DEV_DRIVER_MAX; i++) { | ||
if (!strcmp(net_type, netlookup[i])) { | ||
return 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_slirp(netdev_t *netdev) | ||
{ | ||
// TBD: 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; | ||
else | ||
netdev->type = dev_idx; | ||
|
||
net_init_fun[netdev->type](netdev); | ||
|
||
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,27 @@ | ||
#pragma once | ||
|
||
typedef enum { | ||
NET_DEV_DRIVER_TAP, | ||
NET_DEV_DRIVER_USER, | ||
NET_DEV_DRIVER_MAX, | ||
} net_dev_driver_t; | ||
|
||
typedef struct { | ||
int tap_fd; | ||
} net_tap_options; | ||
|
||
typedef struct { | ||
/* TODO: Implement user option */ | ||
int temp; | ||
} 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); |
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