Skip to content

Commit

Permalink
Merge pull request torvalds#118 from libos-nuse/upstream-refactor-tap
Browse files Browse the repository at this point in the history
lkl tools: refactor tap code from hijack library to general library
  • Loading branch information
Octavian Purdila committed Mar 8, 2016
2 parents b43930b + a38e729 commit d863e04
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 133 deletions.
14 changes: 11 additions & 3 deletions Documentation/lkl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,19 @@ You can usually use this library via a wrapper script.
There are environmental variables to configure the behavior of LKL. The followings
are the list of those variable for your environment.

* LKL_HIJACK_NET_TAP
* LKL_HIJACK_NET_IFTYPE

an interface name for tap device in host operating system to connect to LKL.
The interface type in host operating system to connect to LKL.
The following example specifies a tap interface.
```
$ LKL_HIJACK_NET_TAP=tap0 lkl-hijack.sh ip address show
$ LKL_HIJACK_NET_IFTYPE=tap LKL_HIJACK_NET_IFPARAMS=tap0 lkl-hijack.sh ip address show
```
* LKL_HIJACK_NET_IFPARAMS

Additional configuration parameters for the interface specified by LKL_HIJACK_NET_IFTYPE.
The parameters depend on the interface type (LKL_HIJACK_NET_IFTYPE).
```
$ LKL_HIJACK_NET_IFTYPE=tap LKL_HIJACK_NET_IFPARAMS=tap0 lkl-hijack.sh ip address show
```
* LKL_HIJACK_NET_IP

Expand Down
18 changes: 13 additions & 5 deletions tools/lkl/include/lkl.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,12 @@ int lkl_set_ipv4_gateway(unsigned int addr);

/**
* lkl_netdev - host network device handle
*
* @fd - TAP device or packet socket file descriptor
*/
union lkl_netdev {
int fd;
struct lkl_netdev {
struct lkl_dev_net_ops *ops;
};


/**
* lkl_netdev_add - add a new network device
*
Expand All @@ -227,7 +226,7 @@ union lkl_netdev {
* @returns a network device id (0 is valid) or a strictly negative value in
* case of error
*/
int lkl_netdev_add(union lkl_netdev nd, void *mac);
int lkl_netdev_add(struct lkl_netdev *nd, void *mac);

/**
* lkl_netdev_get_ifindex - retrieve the interface index for a given network
Expand Down Expand Up @@ -256,6 +255,15 @@ int lkl_create_syscall_thread(void);
*/
int lkl_stop_syscall_thread(void);

/**
* lkl_netdev_tap_create - create TAP net_device for the virtio net backend
*
* @ifname - interface name for the TAP device. need to be configured
* on host in advance
*/
struct lkl_netdev *lkl_netdev_tap_create(const char *ifname);


#ifdef __cplusplus
}
#endif
Expand Down
8 changes: 3 additions & 5 deletions tools/lkl/include/lkl_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,12 @@ struct lkl_dev_blk_ops {
int (*request)(union lkl_disk disk, struct lkl_blk_req *req);
};

extern struct lkl_dev_net_ops lkl_dev_net_ops;

struct lkl_dev_net_ops {
int (*tx)(union lkl_netdev nd, void *data, int len);
int (*rx)(union lkl_netdev nd, void *data, int *len);
int (*tx)(struct lkl_netdev *nd, void *data, int len);
int (*rx)(struct lkl_netdev *nd, void *data, int *len);
#define LKL_DEV_NET_POLL_RX 1
#define LKL_DEV_NET_POLL_TX 2
int (*poll)(union lkl_netdev nd, int events);
int (*poll)(struct lkl_netdev *nd, int events);
};

#ifdef __cplusplus
Expand Down
1 change: 1 addition & 0 deletions tools/lkl/lib/Build
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ lkl-y += utils.o
lkl-y += virtio_blk.o
lkl-y += virtio.o
lkl-$(CONFIG_AUTO_LKL_POSIX_HOST) += virtio_net.o
lkl-$(CONFIG_AUTO_LKL_POSIX_HOST) += virtio_net_tap.o
78 changes: 33 additions & 45 deletions tools/lkl/lib/hijack/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,65 +139,53 @@ static void mount_cmds_exec(char *_cmds, int (*callback)(char*))
free(cmds);
}

int init_tap(char *tap, char *mac_str)
{
struct ifreq ifr = {
.ifr_flags = IFF_TAP | IFF_NO_PI,
};
union lkl_netdev nd;
__lkl__u8 mac[LKL_ETH_ALEN] = {0};
int ret = -1;

strncpy(ifr.ifr_name, tap, IFNAMSIZ);

nd.fd = open("/dev/net/tun", O_RDWR|O_NONBLOCK);
if (nd.fd < 0) {
fprintf(stderr, "failed to open tap: %s\n", strerror(errno));
return -1;
}

ret = ioctl(nd.fd, TUNSETIFF, &ifr);
if (ret < 0) {
fprintf(stderr, "failed to attach to %s: %s\n",
ifr.ifr_name, strerror(errno));
return -1;
}

ret = parse_mac_str(mac_str, mac);

if (ret < 0) {
fprintf(stderr, "failed to parse mac\n");
return -1;
} else if (ret > 0) {
ret = lkl_netdev_add(nd, mac);
} else {
ret = lkl_netdev_add(nd, NULL);
}

if (ret < 0) {
fprintf(stderr, "failed to add netdev: %s\n",
lkl_strerror(ret));
return -1;
}

return ret;
}

void __attribute__((constructor(102)))
hijack_init(void)
{
int ret, i, dev_null, nd_id = -1, nd_ifindex = -1;
/* OBSOLETE: should use IFTYPE and IFPARAMS */
char *tap = getenv("LKL_HIJACK_NET_TAP");
char *iftype = getenv("LKL_HIJACK_NET_IFTYPE");
char *ifparams = getenv("LKL_HIJACK_NET_IFPARAMS");
char *mtu_str = getenv("LKL_HIJACK_NET_MTU");
__lkl__u8 mac[LKL_ETH_ALEN] = {0};
char *ip = getenv("LKL_HIJACK_NET_IP");
char *mac_str = getenv("LKL_HIJACK_NET_MAC");
char *netmask_len = getenv("LKL_HIJACK_NET_NETMASK_LEN");
char *gateway = getenv("LKL_HIJACK_NET_GATEWAY");
char *debug = getenv("LKL_HIJACK_DEBUG");
char *mount = getenv("LKL_HIJACK_MOUNT");
struct lkl_netdev *nd = NULL;

if (tap) {
nd_id = init_tap(tap, mac_str);
fprintf(stderr,
"WARN: variable LKL_HIJACK_NET_TAP is now obsoleted.\n"
" please use LKL_HIJACK_NET_IFTYPE and "
"LKL_HIJACK_NET_IFPARAMS instead.\n");
nd = lkl_netdev_tap_create(tap);
}

if (!nd && iftype && ifparams && (strncmp(iftype, "tap", 3) == 0))
nd = lkl_netdev_tap_create(ifparams);

if (nd) {
ret = parse_mac_str(mac_str, mac);

if (ret < 0) {
fprintf(stderr, "failed to parse mac\n");
return;
} else if (ret > 0) {
ret = lkl_netdev_add(nd, mac);
} else {
ret = lkl_netdev_add(nd, NULL);
}

if (ret < 0) {
fprintf(stderr, "failed to add netdev: %s\n",
lkl_strerror(ret));
return;
}
nd_id = ret;
}

if (!debug)
Expand Down
54 changes: 0 additions & 54 deletions tools/lkl/lib/posix-host.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <signal.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <time.h>
Expand Down Expand Up @@ -379,56 +378,3 @@ struct lkl_dev_blk_ops lkl_dev_blk_ops = {
.request = blk_request,
};

static int net_tx(union lkl_netdev nd, void *data, int len)
{
int ret;

ret = write(nd.fd, data, len);
if (ret <= 0 && errno == -EAGAIN)
return -1;
return 0;
}

static int net_rx(union lkl_netdev nd, void *data, int *len)
{
int ret;

ret = read(nd.fd, data, *len);
if (ret <= 0)
return -1;
*len = ret;
return 0;
}

static int net_poll(union lkl_netdev nd, int events)
{
struct pollfd pfd = {
.fd = nd.fd,
};
int ret = 0;

if (events & LKL_DEV_NET_POLL_RX)
pfd.events |= POLLIN | POLLPRI;
if (events & LKL_DEV_NET_POLL_TX)
pfd.events |= POLLOUT;

while (poll(&pfd, 1, -1) < 0 && errno == EINTR)
;

if (pfd.revents & (POLLHUP | POLLNVAL))
return -1;

if (pfd.revents & POLLIN)
ret |= LKL_DEV_NET_POLL_RX;
if (pfd.revents & POLLOUT)
ret |= LKL_DEV_NET_POLL_TX;

return ret;
}

struct lkl_dev_net_ops lkl_dev_net_ops = {
.tx = net_tx,
.rx = net_rx,
.poll = net_poll,
};

6 changes: 3 additions & 3 deletions tools/lkl/lib/virtio_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct virtio_net_dev {
struct virtio_dev dev;
struct lkl_virtio_net_config config;
struct lkl_dev_net_ops *ops;
union lkl_netdev nd;
struct lkl_netdev *nd;
struct virtio_net_poll rx_poll, tx_poll;
struct lkl_mutex_t **queue_locks;
};
Expand Down Expand Up @@ -155,7 +155,7 @@ static struct lkl_mutex_t **init_queue_locks(int num_queues)
return ret;
}

int lkl_netdev_add(union lkl_netdev nd, void *mac)
int lkl_netdev_add(struct lkl_netdev *nd, void *mac)
{
struct virtio_net_dev *dev;
static int count;
Expand All @@ -173,7 +173,7 @@ int lkl_netdev_add(union lkl_netdev nd, void *mac)
dev->dev.config_data = &dev->config;
dev->dev.config_len = sizeof(dev->config);
dev->dev.ops = &net_ops;
dev->ops = &lkl_dev_net_ops;
dev->ops = nd->ops;
dev->nd = nd;
dev->queue_locks = init_queue_locks(NUM_QUEUES);

Expand Down
Loading

0 comments on commit d863e04

Please sign in to comment.