|
| 1 | +#ifdef CONFIG_AUTO_LKL_VIRTIO_NET_VDE |
| 2 | + |
| 3 | +#include <stdio.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <string.h> |
| 6 | +#include <errno.h> |
| 7 | +#include <poll.h> |
| 8 | +#include <lkl.h> |
| 9 | +#include <lkl_host.h> |
| 10 | + |
| 11 | +#include <libvdeplug.h> |
| 12 | + |
| 13 | +struct lkl_netdev_vde { |
| 14 | + struct lkl_dev_net_ops *ops; |
| 15 | + VDECONN *conn; |
| 16 | +}; |
| 17 | + |
| 18 | +struct lkl_netdev *nuse_vif_vde_create(char *switch_path); |
| 19 | +static int net_vde_tx(struct lkl_netdev *nd, void *data, int len); |
| 20 | +static int net_vde_rx(struct lkl_netdev *nd, void *data, int *len); |
| 21 | +static int net_vde_poll_with_timeout(struct lkl_netdev *nd, int events, |
| 22 | + int timeout); |
| 23 | +static int net_vde_poll(struct lkl_netdev *nd, int events); |
| 24 | + |
| 25 | +struct lkl_dev_net_ops vde_net_ops = { |
| 26 | + .tx = net_vde_tx, |
| 27 | + .rx = net_vde_rx, |
| 28 | + .poll = net_vde_poll, |
| 29 | +}; |
| 30 | + |
| 31 | +int net_vde_tx(struct lkl_netdev *nd, void *data, int len) |
| 32 | +{ |
| 33 | + int ret; |
| 34 | + struct lkl_netdev_vde *nd_vde; |
| 35 | + |
| 36 | + nd_vde = (struct lkl_netdev_vde *) nd; |
| 37 | + |
| 38 | + ret = vde_send(nd_vde->conn, data, len, 0); |
| 39 | + if (ret <= 0 && errno == EAGAIN) |
| 40 | + return -1; |
| 41 | + return 0; |
| 42 | +} |
| 43 | + |
| 44 | +int net_vde_rx(struct lkl_netdev *nd, void *data, int *len) |
| 45 | +{ |
| 46 | + int ret; |
| 47 | + struct lkl_netdev_vde *nd_vde; |
| 48 | + |
| 49 | + nd_vde = (struct lkl_netdev_vde *) nd; |
| 50 | + |
| 51 | + /* |
| 52 | + * Due to a bug in libvdeplug we have to first poll to make sure |
| 53 | + * that there is data available. |
| 54 | + * The correct solution would be to just use |
| 55 | + * ret = vde_recv(nd_vde->conn, data, *len, MSG_DONTWAIT); |
| 56 | + * This should be changed once libvdeplug is fixed. |
| 57 | + */ |
| 58 | + ret = 0; |
| 59 | + if (net_vde_poll_with_timeout(nd, LKL_DEV_NET_POLL_RX, 0) & |
| 60 | + LKL_DEV_NET_POLL_RX) |
| 61 | + ret = vde_recv(nd_vde->conn, data, *len, 0); |
| 62 | + if (ret <= 0) |
| 63 | + return -1; |
| 64 | + *len = ret; |
| 65 | + return 0; |
| 66 | +} |
| 67 | + |
| 68 | +int net_vde_poll_with_timeout(struct lkl_netdev *nd, int events, int timeout) |
| 69 | +{ |
| 70 | + int ret; |
| 71 | + struct lkl_netdev_vde *nd_vde; |
| 72 | + |
| 73 | + nd_vde = (struct lkl_netdev_vde *) nd; |
| 74 | + |
| 75 | + struct pollfd pollfds[] = { |
| 76 | + { |
| 77 | + .fd = vde_datafd(nd_vde->conn), |
| 78 | + }, |
| 79 | + { |
| 80 | + .fd = vde_ctlfd(nd_vde->conn), |
| 81 | + .events = POLLHUP | POLLIN |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + if (events & LKL_DEV_NET_POLL_RX) |
| 86 | + pollfds[0].events |= POLLIN; |
| 87 | + if (events & LKL_DEV_NET_POLL_TX) |
| 88 | + pollfds[0].events |= POLLOUT; |
| 89 | + |
| 90 | + while (poll(pollfds, 2, timeout) < 0 && errno == EINTR) |
| 91 | + ; |
| 92 | + |
| 93 | + ret = 0; |
| 94 | + |
| 95 | + if (pollfds[1].revents & (POLLHUP | POLLNVAL | POLLIN)) |
| 96 | + return -1; |
| 97 | + if (pollfds[0].revents & (POLLHUP | POLLNVAL)) |
| 98 | + return -1; |
| 99 | + |
| 100 | + if (pollfds[0].revents & POLLIN) |
| 101 | + ret |= LKL_DEV_NET_POLL_RX; |
| 102 | + if (pollfds[0].revents & POLLOUT) |
| 103 | + ret |= LKL_DEV_NET_POLL_TX; |
| 104 | + |
| 105 | + return ret; |
| 106 | +} |
| 107 | + |
| 108 | +int net_vde_poll(struct lkl_netdev *nd, int events) |
| 109 | +{ |
| 110 | + return net_vde_poll_with_timeout(nd, events, -1); |
| 111 | +} |
| 112 | + |
| 113 | +struct lkl_netdev *lkl_netdev_vde_create(char const *switch_path) |
| 114 | +{ |
| 115 | + struct lkl_netdev_vde *nd; |
| 116 | + struct vde_open_args open_args = {.port = 0, .group = 0, .mode = 0700 }; |
| 117 | + char *switch_path_copy = 0; |
| 118 | + |
| 119 | + nd = (struct lkl_netdev_vde *)malloc(sizeof(*nd)); |
| 120 | + if (!nd) { |
| 121 | + fprintf(stderr, "Failed to allocate memory.\n"); |
| 122 | + /* TODO: propagate the error state, maybe use errno? */ |
| 123 | + return 0; |
| 124 | + } |
| 125 | + nd->ops = &vde_net_ops; |
| 126 | + |
| 127 | + /* vde_open() allows the null pointer as path which means |
| 128 | + * "VDE default path" |
| 129 | + */ |
| 130 | + if (switch_path != 0) { |
| 131 | + /* vde_open() takes a non-const char * which is a bug in their |
| 132 | + * function declaration. Even though the implementation does not |
| 133 | + * modify the string, we shouldn't just cast away the const. |
| 134 | + */ |
| 135 | + size_t switch_path_length = strlen(switch_path); |
| 136 | + |
| 137 | + switch_path_copy = calloc(switch_path_length + 1, sizeof(char)); |
| 138 | + if (!switch_path_copy) { |
| 139 | + fprintf(stderr, "Failed to allocate memory.\n"); |
| 140 | + /* TODO: propagate the error state, maybe use errno? */ |
| 141 | + return 0; |
| 142 | + } |
| 143 | + strncpy(switch_path_copy, switch_path, switch_path_length); |
| 144 | + } |
| 145 | + nd->conn = vde_open(switch_path_copy, "lkl-virtio-net", &open_args); |
| 146 | + free(switch_path_copy); |
| 147 | + if (nd->conn == 0) { |
| 148 | + fprintf(stderr, "Failed to connect to vde switch.\n"); |
| 149 | + /* TODO: propagate the error state, maybe use errno? */ |
| 150 | + return 0; |
| 151 | + } |
| 152 | + |
| 153 | + return (struct lkl_netdev *)nd; |
| 154 | +} |
| 155 | + |
| 156 | +#else /* CONFIG_AUTO_LKL_VIRTIO_NET_VDE */ |
| 157 | + |
| 158 | +struct lkl_netdev *lkl_netdev_vde_create(char const *switch_path) |
| 159 | +{ |
| 160 | + fprintf(stderr, "lkl: The host library was compiled without support for VDE networking. Please rebuild with VDE enabled.\n"); |
| 161 | + return 0; |
| 162 | +} |
| 163 | + |
| 164 | +#endif /* CONFIG_AUTO_LKL_VIRTIO_NET_VDE */ |
0 commit comments