Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at: |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | *------------------------------------------------------------------ |
| 16 | */ |
| 17 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 18 | #define _GNU_SOURCE |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 19 | #include <sys/types.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <net/if.h> |
| 23 | #include <linux/if_tun.h> |
| 24 | #include <sys/ioctl.h> |
| 25 | #include <linux/virtio_net.h> |
| 26 | #include <linux/vhost.h> |
| 27 | #include <sys/eventfd.h> |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 28 | #include <net/if_arp.h> |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 29 | #include <sched.h> |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 30 | #include <limits.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 31 | |
| 32 | #include <linux/netlink.h> |
| 33 | #include <linux/rtnetlink.h> |
| 34 | |
| 35 | #include <vlib/vlib.h> |
Lijian.Zhang | ba0da57 | 2019-08-21 17:51:16 +0800 | [diff] [blame] | 36 | #include <vlib/physmem.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 37 | #include <vlib/unix/unix.h> |
| 38 | #include <vnet/ethernet/ethernet.h> |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 39 | #include <vnet/ip/ip4_packet.h> |
| 40 | #include <vnet/ip/ip6_packet.h> |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 41 | #include <vnet/devices/netlink.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 42 | #include <vnet/devices/virtio/virtio.h> |
Damjan Marion | c99b4cd | 2017-12-04 15:25:58 +0100 | [diff] [blame] | 43 | #include <vnet/devices/tap/tap.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 44 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 45 | tap_main_t tap_main; |
| 46 | |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 47 | #define tap_log_err(dev, f, ...) \ |
| 48 | vlib_log (VLIB_LOG_LEVEL_ERR, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__) |
| 49 | #define tap_log_dbg(dev, f, ...) \ |
| 50 | vlib_log (VLIB_LOG_LEVEL_DEBUG, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__) |
| 51 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 52 | #define _IOCTL(fd,a,...) \ |
| 53 | if (ioctl (fd, a, __VA_ARGS__) < 0) \ |
| 54 | { \ |
| 55 | err = clib_error_return_unix (0, "ioctl(" #a ")"); \ |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 56 | tap_log_err (vif, "%U", format_clib_error, err); \ |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 57 | goto error; \ |
| 58 | } |
| 59 | |
| 60 | static u32 |
| 61 | virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, |
| 62 | u32 flags) |
| 63 | { |
| 64 | /* nothing for now */ |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 65 | //TODO On MTU change call vnet_netlink_set_if_mtu |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 69 | static int |
| 70 | open_netns_fd (char *netns) |
| 71 | { |
| 72 | u8 *s = 0; |
| 73 | int fd; |
| 74 | |
| 75 | if (strncmp (netns, "pid:", 4) == 0) |
| 76 | s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0); |
| 77 | else if (netns[0] == '/') |
| 78 | s = format (0, "%s%c", netns, 0); |
| 79 | else |
| 80 | s = format (0, "/var/run/netns/%s%c", netns, 0); |
| 81 | |
| 82 | fd = open ((char *) s, O_RDONLY); |
| 83 | vec_free (s); |
| 84 | return fd; |
| 85 | } |
| 86 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 87 | #define TAP_MAX_INSTANCE 1024 |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 88 | |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 89 | static void |
| 90 | tap_free (vlib_main_t * vm, virtio_if_t * vif) |
| 91 | { |
| 92 | virtio_main_t *mm = &virtio_main; |
| 93 | tap_main_t *tm = &tap_main; |
| 94 | int i; |
| 95 | |
| 96 | /* *INDENT-OFF* */ |
| 97 | vec_foreach_index (i, vif->vhost_fds) if (vif->vhost_fds[i] != -1) |
| 98 | close (vif->vhost_fds[i]); |
| 99 | vec_foreach_index (i, vif->rxq_vrings) |
| 100 | virtio_vring_free_rx (vm, vif, RX_QUEUE (i)); |
| 101 | vec_foreach_index (i, vif->txq_vrings) |
| 102 | virtio_vring_free_tx (vm, vif, TX_QUEUE (i)); |
| 103 | /* *INDENT-ON* */ |
| 104 | |
| 105 | if (vif->tap_fd != -1) |
| 106 | close (vif->tap_fd); |
| 107 | |
| 108 | vec_free (vif->vhost_fds); |
| 109 | vec_free (vif->rxq_vrings); |
| 110 | vec_free (vif->txq_vrings); |
| 111 | vec_free (vif->host_if_name); |
| 112 | vec_free (vif->net_ns); |
| 113 | vec_free (vif->host_bridge); |
| 114 | clib_error_free (vif->error); |
| 115 | |
| 116 | tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0); |
| 117 | clib_memset (vif, 0, sizeof (*vif)); |
| 118 | pool_put (mm->interfaces, vif); |
| 119 | } |
| 120 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 121 | void |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 122 | tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) |
| 123 | { |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 124 | vlib_thread_main_t *thm = vlib_get_thread_main (); |
Lijian.Zhang | ba0da57 | 2019-08-21 17:51:16 +0800 | [diff] [blame] | 125 | vlib_physmem_main_t *vpm = &vm->physmem_main; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 126 | vnet_main_t *vnm = vnet_get_main (); |
| 127 | virtio_main_t *vim = &virtio_main; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 128 | tap_main_t *tm = &tap_main; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 129 | vnet_sw_interface_t *sw; |
| 130 | vnet_hw_interface_t *hw; |
Damjan Marion | 4e671d2 | 2017-12-09 21:19:01 +0100 | [diff] [blame] | 131 | int i; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 132 | int old_netns_fd = -1; |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 133 | struct ifreq ifr = {.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR }; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 134 | size_t hdrsz; |
| 135 | struct vhost_memory *vhost_mem = 0; |
| 136 | virtio_if_t *vif = 0; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 137 | clib_error_t *err = 0; |
Damjan Marion | 39807d0 | 2019-11-08 13:52:28 +0100 | [diff] [blame] | 138 | unsigned int tap_features; |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 139 | int tfd, vfd, nfd = -1; |
Mohsin Kazmi | 19b697f | 2019-07-29 13:21:17 +0200 | [diff] [blame] | 140 | char *host_if_name = 0; |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 141 | unsigned int offload = 0; |
| 142 | u16 num_q_pairs; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 143 | |
| 144 | if (args->id != ~0) |
| 145 | { |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 146 | if (clib_bitmap_get (tm->tap_ids, args->id)) |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 147 | { |
| 148 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 149 | args->error = clib_error_return (0, "interface already exists"); |
| 150 | return; |
| 151 | } |
| 152 | } |
| 153 | else |
| 154 | { |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 155 | args->id = clib_bitmap_first_clear (tm->tap_ids); |
| 156 | } |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 157 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 158 | if (args->id > TAP_MAX_INSTANCE) |
| 159 | { |
| 160 | args->rv = VNET_API_ERROR_UNSPECIFIED; |
| 161 | args->error = clib_error_return (0, "cannot find free interface id"); |
| 162 | return; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 163 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 164 | |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 165 | pool_get_zero (vim->interfaces, vif); |
| 166 | vif->type = VIRTIO_IF_TYPE_TAP; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 167 | vif->dev_instance = vif - vim->interfaces; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 168 | vif->id = args->id; |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 169 | vif->num_txqs = thm->n_vlib_mains; |
| 170 | vif->num_rxqs = args->num_rx_queues; |
| 171 | num_q_pairs = clib_max (vif->num_rxqs, vif->num_txqs); |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 172 | |
Jakub Grajciar | 5de4fb7 | 2019-09-03 10:40:01 +0200 | [diff] [blame] | 173 | if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes)) |
| 174 | ethernet_mac_address_generate (args->host_mac_addr.bytes); |
| 175 | clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6); |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 176 | |
| 177 | if ((vif->tap_fd = tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 178 | { |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 179 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 180 | args->error = clib_error_return_unix (0, "open '/dev/net/tun'"); |
| 181 | goto error; |
| 182 | } |
| 183 | tap_log_dbg (vif, "open tap fd %d", tfd); |
| 184 | |
| 185 | _IOCTL (tfd, TUNGETFEATURES, &tap_features); |
| 186 | tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features); |
| 187 | if ((tap_features & IFF_VNET_HDR) == 0) |
| 188 | { |
| 189 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 190 | args->error = clib_error_return (0, "vhost-net backend not available"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 191 | goto error; |
| 192 | } |
| 193 | |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 194 | if ((tap_features & IFF_MULTI_QUEUE) == 0) |
| 195 | { |
| 196 | if (args->num_rx_queues > 1) |
| 197 | { |
| 198 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 199 | args->error = clib_error_return (0, "multiqueue not supported"); |
| 200 | goto error; |
| 201 | } |
| 202 | vif->num_rxqs = vif->num_txqs = num_q_pairs = 1; |
| 203 | } |
| 204 | else |
| 205 | ifr.ifr_flags |= IFF_MULTI_QUEUE; |
| 206 | |
| 207 | hdrsz = sizeof (struct virtio_net_hdr_v1); |
| 208 | if (args->tap_flags & TAP_FLAG_GSO) |
| 209 | { |
| 210 | offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6; |
| 211 | vif->gso_enabled = 1; |
| 212 | } |
| 213 | |
| 214 | _IOCTL (tfd, TUNSETIFF, (void *) &ifr); |
| 215 | tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd, |
| 216 | ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags); |
| 217 | |
| 218 | vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name); |
| 219 | tap_log_dbg (vif, "ifindex %d", vif->ifindex); |
| 220 | |
| 221 | if (!args->host_if_name) |
| 222 | host_if_name = ifr.ifr_ifrn.ifrn_name; |
| 223 | else |
| 224 | host_if_name = (char *) args->host_if_name; |
| 225 | |
Andrew Yourtchenko | 0d3d482 | 2019-11-25 18:25:46 +0000 | [diff] [blame] | 226 | if (fcntl (tfd, F_SETFL, O_NONBLOCK) < 0) |
| 227 | { |
| 228 | err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)"); |
| 229 | tap_log_err (vif, "set nonblocking: %U", format_clib_error, err); |
| 230 | goto error; |
| 231 | } |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 232 | |
| 233 | tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u", tfd, hdrsz); |
| 234 | _IOCTL (tfd, TUNSETVNETHDRSZ, &hdrsz); |
| 235 | |
| 236 | i = INT_MAX; |
| 237 | tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", tfd, i); |
| 238 | _IOCTL (tfd, TUNSETSNDBUF, &i); |
| 239 | |
| 240 | tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", tfd, offload); |
| 241 | _IOCTL (tfd, TUNSETOFFLOAD, offload); |
| 242 | |
| 243 | clib_memset (&ifr, 0, sizeof (ifr)); |
| 244 | ifr.ifr_addr.sa_family = ARPHRD_ETHER; |
| 245 | clib_memcpy (ifr.ifr_hwaddr.sa_data, vif->host_mac_addr, 6); |
| 246 | tap_log_dbg (vif, "SIOCSIFHWADDR: fd %d hwaddr %U", tfd, |
| 247 | format_hex_bytes, ifr.ifr_hwaddr.sa_data, 6); |
| 248 | _IOCTL (tfd, SIOCSIFHWADDR, (void *) &ifr); |
| 249 | |
| 250 | /* open vhost-net fd for each queue pair and set ownership */ |
| 251 | for (i = 0; i < num_q_pairs; i++) |
| 252 | { |
| 253 | if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0) |
| 254 | { |
| 255 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 256 | args->error = clib_error_return_unix (0, "open '/dev/vhost-net'"); |
| 257 | goto error; |
| 258 | } |
| 259 | vec_add1 (vif->vhost_fds, vfd); |
| 260 | virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i); |
| 261 | _IOCTL (vfd, VHOST_SET_OWNER, 0); |
| 262 | virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd); |
| 263 | } |
| 264 | |
| 265 | _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features); |
| 266 | virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx", |
| 267 | vif->remote_features); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 268 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 269 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 270 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 271 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 272 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 273 | "VIRTIO_NET_F_MRG_RXBUF feature"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 274 | goto error; |
| 275 | } |
| 276 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 277 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) == |
| 278 | 0) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 279 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 280 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 281 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 282 | "VIRTIO_RING_F_INDIRECT_DESC feature"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 283 | goto error; |
| 284 | } |
| 285 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 286 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 287 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 288 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 289 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 290 | "VIRTIO_F_VERSION_1 features"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 291 | goto error; |
| 292 | } |
| 293 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 294 | vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF); |
| 295 | vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1); |
| 296 | vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC); |
| 297 | |
| 298 | virtio_set_net_hdr_size (vif); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 299 | |
Paul Vinciguerra | 97c998c | 2019-10-29 16:11:09 -0400 | [diff] [blame] | 300 | /* if namespace is specified, all further netlink messages should be executed |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 301 | after we change our net namespace */ |
| 302 | if (args->host_namespace) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 303 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 304 | old_netns_fd = open ("/proc/self/ns/net", O_RDONLY); |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 305 | if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1) |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 306 | { |
| 307 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 308 | args->error = clib_error_return_unix (0, "open_netns_fd '%s'", |
| 309 | args->host_namespace); |
| 310 | goto error; |
| 311 | } |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 312 | args->error = vnet_netlink_set_link_netns (vif->ifindex, nfd, |
Mohsin Kazmi | 19b697f | 2019-07-29 13:21:17 +0200 | [diff] [blame] | 313 | host_if_name); |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 314 | if (args->error) |
| 315 | { |
| 316 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 317 | goto error; |
| 318 | } |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 319 | if (setns (nfd, CLONE_NEWNET) == -1) |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 320 | { |
| 321 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 322 | args->error = clib_error_return_unix (0, "setns '%s'", |
| 323 | args->host_namespace); |
| 324 | goto error; |
| 325 | } |
Mohsin Kazmi | 19b697f | 2019-07-29 13:21:17 +0200 | [diff] [blame] | 326 | if ((vif->ifindex = if_nametoindex (host_if_name)) == 0) |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 327 | { |
| 328 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 329 | args->error = clib_error_return_unix (0, "if_nametoindex '%s'", |
Mohsin Kazmi | 19b697f | 2019-07-29 13:21:17 +0200 | [diff] [blame] | 330 | host_if_name); |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 331 | goto error; |
| 332 | } |
| 333 | } |
| 334 | else |
| 335 | { |
Mohsin Kazmi | 19b697f | 2019-07-29 13:21:17 +0200 | [diff] [blame] | 336 | if (host_if_name) |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 337 | { |
| 338 | args->error = vnet_netlink_set_link_name (vif->ifindex, |
Mohsin Kazmi | 19b697f | 2019-07-29 13:21:17 +0200 | [diff] [blame] | 339 | host_if_name); |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 340 | if (args->error) |
| 341 | { |
| 342 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 343 | goto error; |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
Jakub Grajciar | 5de4fb7 | 2019-09-03 10:40:01 +0200 | [diff] [blame] | 348 | if (!ethernet_mac_address_is_zero (args->host_mac_addr.bytes)) |
| 349 | { |
| 350 | args->error = vnet_netlink_set_link_addr (vif->ifindex, |
| 351 | args->host_mac_addr.bytes); |
| 352 | if (args->error) |
| 353 | { |
| 354 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 355 | goto error; |
| 356 | } |
| 357 | } |
| 358 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 359 | if (args->host_bridge) |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 360 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 361 | args->error = vnet_netlink_set_link_master (vif->ifindex, |
| 362 | (char *) args->host_bridge); |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 363 | if (args->error) |
| 364 | { |
| 365 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 366 | goto error; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | if (args->host_ip4_prefix_len) |
| 371 | { |
| 372 | args->error = vnet_netlink_add_ip4_addr (vif->ifindex, |
| 373 | &args->host_ip4_addr, |
| 374 | args->host_ip4_prefix_len); |
| 375 | if (args->error) |
| 376 | { |
| 377 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 378 | goto error; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | if (args->host_ip6_prefix_len) |
| 383 | { |
| 384 | args->error = vnet_netlink_add_ip6_addr (vif->ifindex, |
| 385 | &args->host_ip6_addr, |
| 386 | args->host_ip6_prefix_len); |
| 387 | if (args->error) |
| 388 | { |
| 389 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 390 | goto error; |
| 391 | } |
| 392 | } |
| 393 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 394 | args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ ); |
| 395 | if (args->error) |
| 396 | { |
| 397 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 398 | goto error; |
| 399 | } |
| 400 | |
Damjan Marion | 7866c45 | 2018-01-18 13:35:11 +0100 | [diff] [blame] | 401 | if (args->host_ip4_gw_set) |
| 402 | { |
| 403 | args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw); |
| 404 | if (args->error) |
| 405 | { |
| 406 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 407 | goto error; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | if (args->host_ip6_gw_set) |
| 412 | { |
| 413 | args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw); |
| 414 | if (args->error) |
| 415 | { |
| 416 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 417 | goto error; |
| 418 | } |
| 419 | } |
| 420 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 421 | /* switch back to old net namespace */ |
| 422 | if (args->host_namespace) |
| 423 | { |
| 424 | if (setns (old_netns_fd, CLONE_NEWNET) == -1) |
| 425 | { |
| 426 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 427 | args->error = clib_error_return_unix (0, "setns '%s'", |
| 428 | args->host_namespace); |
| 429 | goto error; |
| 430 | } |
| 431 | } |
| 432 | |
Mohsin Kazmi | 97d54ed | 2019-06-10 11:20:15 +0200 | [diff] [blame] | 433 | if (args->host_mtu_set) |
| 434 | { |
| 435 | args->error = |
| 436 | vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size); |
| 437 | if (args->error) |
| 438 | { |
| 439 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 440 | goto error; |
| 441 | } |
| 442 | } |
| 443 | else if (tm->host_mtu_size != 0) |
| 444 | { |
| 445 | args->error = |
| 446 | vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size); |
| 447 | if (args->error) |
| 448 | { |
| 449 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 450 | goto error; |
| 451 | } |
| 452 | args->host_mtu_set = 1; |
| 453 | args->host_mtu_size = tm->host_mtu_size; |
| 454 | } |
| 455 | |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 456 | for (i = 0; i < num_q_pairs; i++) |
| 457 | { |
| 458 | if (i < vif->num_rxqs && (args->error = |
| 459 | virtio_vring_init (vm, vif, RX_QUEUE (i), |
| 460 | args->rx_ring_sz))) |
| 461 | { |
| 462 | args->rv = VNET_API_ERROR_INIT_FAILED; |
| 463 | goto error; |
| 464 | } |
| 465 | |
| 466 | if (i < vif->num_txqs && (args->error = |
| 467 | virtio_vring_init (vm, vif, TX_QUEUE (i), |
| 468 | args->tx_ring_sz))) |
| 469 | { |
| 470 | args->rv = VNET_API_ERROR_INIT_FAILED; |
| 471 | goto error; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | /* setup features and memtable */ |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 476 | i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region); |
| 477 | vhost_mem = clib_mem_alloc (i); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 478 | clib_memset (vhost_mem, 0, i); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 479 | vhost_mem->nregions = 1; |
Lijian.Zhang | ba0da57 | 2019-08-21 17:51:16 +0800 | [diff] [blame] | 480 | vhost_mem->regions[0].memory_size = vpm->max_size; |
| 481 | vhost_mem->regions[0].guest_phys_addr = vpm->base_addr; |
| 482 | vhost_mem->regions[0].userspace_addr = |
| 483 | vhost_mem->regions[0].guest_phys_addr; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 484 | |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 485 | for (i = 0; i < vhost_mem->nregions; i++) |
| 486 | virtio_log_debug (vif, "memtable region %u memory_size 0x%lx " |
| 487 | "guest_phys_addr 0x%lx userspace_addr 0x%lx", i, |
| 488 | vhost_mem->regions[0].memory_size, |
| 489 | vhost_mem->regions[0].guest_phys_addr, |
| 490 | vhost_mem->regions[0].userspace_addr); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 491 | |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 492 | |
| 493 | for (i = 0; i < num_q_pairs; i++) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 494 | { |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 495 | int fd = vif->vhost_fds[i]; |
| 496 | _IOCTL (fd, VHOST_SET_FEATURES, &vif->features); |
| 497 | virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx", |
| 498 | fd, vif->features); |
| 499 | _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem); |
| 500 | virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 501 | } |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 502 | |
| 503 | /* finish initializing queue pair */ |
| 504 | for (i = 0; i < num_q_pairs * 2; i++) |
| 505 | { |
| 506 | struct vhost_vring_addr addr = { 0 }; |
| 507 | struct vhost_vring_state state = { 0 }; |
| 508 | struct vhost_vring_file file = { 0 }; |
| 509 | virtio_vring_t *vring; |
| 510 | u16 qp = i >> 1; |
| 511 | int fd = vif->vhost_fds[qp]; |
| 512 | |
| 513 | if (i & 1) |
| 514 | { |
| 515 | if (qp >= vif->num_txqs) |
| 516 | continue; |
| 517 | vring = vec_elt_at_index (vif->txq_vrings, qp); |
| 518 | } |
| 519 | else |
| 520 | { |
| 521 | if (qp >= vif->num_rxqs) |
| 522 | continue; |
| 523 | vring = vec_elt_at_index (vif->rxq_vrings, qp); |
| 524 | } |
| 525 | |
| 526 | addr.index = state.index = file.index = vring->queue_id & 1; |
| 527 | state.num = vring->size; |
| 528 | virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd, |
| 529 | state.index, state.num); |
| 530 | _IOCTL (fd, VHOST_SET_VRING_NUM, &state); |
| 531 | |
| 532 | addr.flags = 0; |
| 533 | addr.desc_user_addr = pointer_to_uword (vring->desc); |
| 534 | addr.avail_user_addr = pointer_to_uword (vring->avail); |
| 535 | addr.used_user_addr = pointer_to_uword (vring->used); |
| 536 | |
| 537 | virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x " |
| 538 | "desc_user_addr 0x%lx avail_user_addr 0x%lx " |
| 539 | "used_user_addr 0x%lx", fd, addr.index, |
| 540 | addr.flags, addr.desc_user_addr, addr.avail_user_addr, |
| 541 | addr.used_user_addr); |
| 542 | _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr); |
| 543 | |
| 544 | file.fd = vring->call_fd; |
| 545 | virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d", |
| 546 | fd, file.index, file.fd); |
| 547 | _IOCTL (fd, VHOST_SET_VRING_CALL, &file); |
| 548 | |
| 549 | file.fd = vring->kick_fd; |
| 550 | virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d", |
| 551 | fd, file.index, file.fd); |
| 552 | _IOCTL (fd, VHOST_SET_VRING_KICK, &file); |
| 553 | |
| 554 | file.fd = tfd; |
| 555 | virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d", |
| 556 | fd, file.index, file.fd); |
| 557 | _IOCTL (fd, VHOST_NET_SET_BACKEND, &file); |
| 558 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 559 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 560 | if (!args->mac_addr_set) |
Jakub Grajciar | 5de4fb7 | 2019-09-03 10:40:01 +0200 | [diff] [blame] | 561 | ethernet_mac_address_generate (args->mac_addr.bytes); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 562 | |
Jakub Grajciar | 5de4fb7 | 2019-09-03 10:40:01 +0200 | [diff] [blame] | 563 | clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 564 | |
Mohsin Kazmi | 19b697f | 2019-07-29 13:21:17 +0200 | [diff] [blame] | 565 | vif->host_if_name = format (0, "%s%c", host_if_name, 0); |
Benoît Ganne | c30d87e | 2019-07-15 17:16:49 +0200 | [diff] [blame] | 566 | vif->net_ns = format (0, "%s%c", args->host_namespace, 0); |
| 567 | vif->host_bridge = format (0, "%s%c", args->host_bridge, 0); |
Mohsin Kazmi | 97d54ed | 2019-06-10 11:20:15 +0200 | [diff] [blame] | 568 | vif->host_mtu_size = args->host_mtu_size; |
Jakub Grajciar | 5de4fb7 | 2019-09-03 10:40:01 +0200 | [diff] [blame] | 569 | clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6); |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 570 | vif->host_ip4_prefix_len = args->host_ip4_prefix_len; |
| 571 | vif->host_ip6_prefix_len = args->host_ip6_prefix_len; |
| 572 | if (args->host_ip4_prefix_len) |
| 573 | clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4); |
| 574 | if (args->host_ip6_prefix_len) |
| 575 | clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16); |
| 576 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 577 | args->error = ethernet_register_interface (vnm, virtio_device_class.index, |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 578 | vif->dev_instance, |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 579 | vif->mac_addr, |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 580 | &vif->hw_if_index, |
| 581 | virtio_eth_flag_change); |
| 582 | if (args->error) |
| 583 | { |
| 584 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
| 585 | goto error; |
| 586 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 587 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 588 | tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 589 | sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index); |
| 590 | vif->sw_if_index = sw->sw_if_index; |
| 591 | args->sw_if_index = vif->sw_if_index; |
Mohsin Kazmi | c5d5327 | 2019-07-01 10:26:43 +0200 | [diff] [blame] | 592 | args->rv = 0; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 593 | hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 594 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE; |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 595 | if (args->tap_flags & TAP_FLAG_GSO) |
| 596 | { |
| 597 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO; |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 598 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 599 | vnet_hw_interface_set_input_node (vnm, vif->hw_if_index, |
| 600 | virtio_input_node.index); |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 601 | |
| 602 | for (i = 0; i < vif->num_rxqs; i++) |
| 603 | { |
| 604 | vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0); |
| 605 | vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i, |
| 606 | VNET_HW_INTERFACE_RX_MODE_DEFAULT); |
| 607 | } |
| 608 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 609 | vif->per_interface_next_index = ~0; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 610 | virtio_vring_set_numa_node (vm, vif, RX_QUEUE (0)); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 611 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 612 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 613 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 614 | vif->cxq_vring = NULL; |
| 615 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 616 | goto done; |
| 617 | |
| 618 | error: |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 619 | if (err) |
| 620 | { |
| 621 | ASSERT (args->error == 0); |
| 622 | args->error = err; |
| 623 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 624 | } |
Benoît Ganne | c30d87e | 2019-07-15 17:16:49 +0200 | [diff] [blame] | 625 | |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 626 | tap_free (vm, vif); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 627 | done: |
| 628 | if (vhost_mem) |
| 629 | clib_mem_free (vhost_mem); |
Damjan Marion | 4e671d2 | 2017-12-09 21:19:01 +0100 | [diff] [blame] | 630 | if (old_netns_fd != -1) |
| 631 | close (old_netns_fd); |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 632 | if (nfd != -1) |
| 633 | close (nfd); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | int |
| 637 | tap_delete_if (vlib_main_t * vm, u32 sw_if_index) |
| 638 | { |
| 639 | vnet_main_t *vnm = vnet_get_main (); |
| 640 | virtio_main_t *mm = &virtio_main; |
| 641 | int i; |
| 642 | virtio_if_t *vif; |
| 643 | vnet_hw_interface_t *hw; |
| 644 | |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 645 | hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 646 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 647 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 648 | |
| 649 | vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); |
| 650 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 651 | if (vif->type != VIRTIO_IF_TYPE_TAP) |
| 652 | return VNET_API_ERROR_INVALID_INTERFACE; |
| 653 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 654 | /* bring down the interface */ |
| 655 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
| 656 | vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0); |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 657 | for (i = 0; i < vif->num_rxqs; i++) |
| 658 | vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 659 | |
| 660 | ethernet_delete_interface (vnm, vif->hw_if_index); |
| 661 | vif->hw_if_index = ~0; |
| 662 | |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 663 | tap_free (vm, vif); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 664 | |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | int |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 669 | tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable) |
| 670 | { |
| 671 | vnet_main_t *vnm = vnet_get_main (); |
| 672 | virtio_main_t *mm = &virtio_main; |
| 673 | virtio_if_t *vif; |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 674 | vnet_hw_interface_t *hw; |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 675 | clib_error_t *err = 0; |
| 676 | |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 677 | hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index); |
| 678 | |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 679 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 680 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 681 | |
| 682 | vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); |
| 683 | |
| 684 | const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6; |
| 685 | const unsigned int gso_off = 0; |
| 686 | unsigned int offload = enable_disable ? gso_on : gso_off; |
| 687 | _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload); |
| 688 | vif->gso_enabled = enable_disable ? 1 : 0; |
| 689 | if (enable_disable) |
| 690 | { |
| 691 | if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0) |
| 692 | { |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 693 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO; |
| 694 | } |
| 695 | } |
| 696 | else |
| 697 | { |
| 698 | if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0) |
| 699 | { |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 700 | hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO; |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | error: |
| 705 | if (err) |
| 706 | { |
| 707 | clib_warning ("Error %s gso on sw_if_index %d", |
| 708 | enable_disable ? "enabling" : "disabling", sw_if_index); |
| 709 | return VNET_API_ERROR_SYSCALL_ERROR_3; |
| 710 | } |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | int |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 715 | tap_dump_ifs (tap_interface_details_t ** out_tapids) |
| 716 | { |
| 717 | vnet_main_t *vnm = vnet_get_main (); |
| 718 | virtio_main_t *mm = &virtio_main; |
| 719 | virtio_if_t *vif; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 720 | virtio_vring_t *vring; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 721 | vnet_hw_interface_t *hi; |
| 722 | tap_interface_details_t *r_tapids = NULL; |
| 723 | tap_interface_details_t *tapid = NULL; |
| 724 | |
| 725 | /* *INDENT-OFF* */ |
| 726 | pool_foreach (vif, mm->interfaces, |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 727 | if (vif->type != VIRTIO_IF_TYPE_TAP) |
| 728 | continue; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 729 | vec_add2(r_tapids, tapid, 1); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 730 | clib_memset (tapid, 0, sizeof (*tapid)); |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 731 | tapid->id = vif->id; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 732 | tapid->sw_if_index = vif->sw_if_index; |
| 733 | hi = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 734 | clib_memcpy(tapid->dev_name, hi->name, |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 735 | MIN (ARRAY_LEN (tapid->dev_name) - 1, |
| 736 | strlen ((const char *) hi->name))); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 737 | vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0)); |
| 738 | tapid->rx_ring_sz = vring->size; |
| 739 | vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0)); |
| 740 | tapid->tx_ring_sz = vring->size; |
Jakub Grajciar | 5de4fb7 | 2019-09-03 10:40:01 +0200 | [diff] [blame] | 741 | clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6); |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 742 | if (vif->host_if_name) |
| 743 | { |
| 744 | clib_memcpy(tapid->host_if_name, vif->host_if_name, |
| 745 | MIN (ARRAY_LEN (tapid->host_if_name) - 1, |
| 746 | strlen ((const char *) vif->host_if_name))); |
| 747 | } |
| 748 | if (vif->net_ns) |
| 749 | { |
| 750 | clib_memcpy(tapid->host_namespace, vif->net_ns, |
| 751 | MIN (ARRAY_LEN (tapid->host_namespace) - 1, |
| 752 | strlen ((const char *) vif->net_ns))); |
| 753 | } |
| 754 | if (vif->host_bridge) |
| 755 | { |
| 756 | clib_memcpy(tapid->host_bridge, vif->host_bridge, |
| 757 | MIN (ARRAY_LEN (tapid->host_bridge) - 1, |
| 758 | strlen ((const char *) vif->host_bridge))); |
| 759 | } |
| 760 | if (vif->host_ip4_prefix_len) |
Jakub Grajciar | 5de4fb7 | 2019-09-03 10:40:01 +0200 | [diff] [blame] | 761 | clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4); |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 762 | tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len; |
| 763 | if (vif->host_ip6_prefix_len) |
Jakub Grajciar | 5de4fb7 | 2019-09-03 10:40:01 +0200 | [diff] [blame] | 764 | clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16); |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 765 | tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len; |
Mohsin Kazmi | 97d54ed | 2019-06-10 11:20:15 +0200 | [diff] [blame] | 766 | tapid->host_mtu_size = vif->host_mtu_size; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 767 | ); |
| 768 | /* *INDENT-ON* */ |
| 769 | |
| 770 | *out_tapids = r_tapids; |
| 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | static clib_error_t * |
Mohsin Kazmi | 97d54ed | 2019-06-10 11:20:15 +0200 | [diff] [blame] | 776 | tap_mtu_config (vlib_main_t * vm, unformat_input_t * input) |
| 777 | { |
| 778 | tap_main_t *tm = &tap_main; |
| 779 | |
| 780 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 781 | { |
| 782 | if (unformat (input, "host-mtu %d", &tm->host_mtu_size)) |
| 783 | ; |
| 784 | else |
| 785 | return clib_error_return (0, "unknown input `%U'", |
| 786 | format_unformat_error, input); |
| 787 | } |
| 788 | |
| 789 | return 0; |
| 790 | } |
| 791 | |
| 792 | /* tap { host-mtu <size> } configuration. */ |
| 793 | VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap"); |
| 794 | |
| 795 | static clib_error_t * |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 796 | tap_init (vlib_main_t * vm) |
| 797 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 798 | tap_main_t *tm = &tap_main; |
Mohsin Kazmi | a23b615 | 2018-05-17 17:21:39 +0200 | [diff] [blame] | 799 | clib_error_t *error = 0; |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 800 | |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 801 | tm->log_default = vlib_log_register_class ("tap", 0); |
Mohsin Kazmi | a23b615 | 2018-05-17 17:21:39 +0200 | [diff] [blame] | 802 | vlib_log_debug (tm->log_default, "initialized"); |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 803 | |
Mohsin Kazmi | 97d54ed | 2019-06-10 11:20:15 +0200 | [diff] [blame] | 804 | tm->host_mtu_size = 0; |
| 805 | |
Mohsin Kazmi | a23b615 | 2018-05-17 17:21:39 +0200 | [diff] [blame] | 806 | return error; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | VLIB_INIT_FUNCTION (tap_init); |
| 810 | |
| 811 | /* |
| 812 | * fd.io coding-style-patch-verification: ON |
| 813 | * |
| 814 | * Local Variables: |
| 815 | * eval: (c-set-style "gnu") |
| 816 | * End: |
| 817 | */ |