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 | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 28 | #include <sched.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 29 | |
| 30 | #include <linux/netlink.h> |
| 31 | #include <linux/rtnetlink.h> |
| 32 | |
| 33 | #include <vlib/vlib.h> |
| 34 | #include <vlib/unix/unix.h> |
| 35 | #include <vnet/ethernet/ethernet.h> |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 36 | #include <vnet/ip/ip4_packet.h> |
| 37 | #include <vnet/ip/ip6_packet.h> |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 38 | #include <vnet/devices/netlink.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 39 | #include <vnet/devices/virtio/virtio.h> |
Damjan Marion | c99b4cd | 2017-12-04 15:25:58 +0100 | [diff] [blame] | 40 | #include <vnet/devices/tap/tap.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 41 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 42 | tap_main_t tap_main; |
| 43 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 44 | #define _IOCTL(fd,a,...) \ |
| 45 | if (ioctl (fd, a, __VA_ARGS__) < 0) \ |
| 46 | { \ |
| 47 | err = clib_error_return_unix (0, "ioctl(" #a ")"); \ |
| 48 | goto error; \ |
| 49 | } |
| 50 | |
| 51 | static u32 |
| 52 | virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, |
| 53 | u32 flags) |
| 54 | { |
| 55 | /* nothing for now */ |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 56 | //TODO On MTU change call vnet_netlink_set_if_mtu |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 60 | static int |
| 61 | open_netns_fd (char *netns) |
| 62 | { |
| 63 | u8 *s = 0; |
| 64 | int fd; |
| 65 | |
| 66 | if (strncmp (netns, "pid:", 4) == 0) |
| 67 | s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0); |
| 68 | else if (netns[0] == '/') |
| 69 | s = format (0, "%s%c", netns, 0); |
| 70 | else |
| 71 | s = format (0, "/var/run/netns/%s%c", netns, 0); |
| 72 | |
| 73 | fd = open ((char *) s, O_RDONLY); |
| 74 | vec_free (s); |
| 75 | return fd; |
| 76 | } |
| 77 | |
| 78 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 79 | void |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 80 | tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) |
| 81 | { |
| 82 | vnet_main_t *vnm = vnet_get_main (); |
Damjan Marion | 829ee53 | 2018-02-16 16:13:32 +0100 | [diff] [blame] | 83 | vlib_thread_main_t *thm = vlib_get_thread_main (); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 84 | virtio_main_t *vim = &virtio_main; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 85 | tap_main_t *tm = &tap_main; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 86 | vnet_sw_interface_t *sw; |
| 87 | vnet_hw_interface_t *hw; |
Damjan Marion | 4e671d2 | 2017-12-09 21:19:01 +0100 | [diff] [blame] | 88 | int i; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 89 | int old_netns_fd = -1; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 90 | struct ifreq ifr; |
| 91 | size_t hdrsz; |
| 92 | struct vhost_memory *vhost_mem = 0; |
| 93 | virtio_if_t *vif = 0; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 94 | clib_error_t *err = 0; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 95 | uword *p; |
| 96 | |
| 97 | if (args->id != ~0) |
| 98 | { |
| 99 | p = hash_get (tm->dev_instance_by_interface_id, args->id); |
| 100 | if (p) |
| 101 | { |
| 102 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 103 | args->error = clib_error_return (0, "interface already exists"); |
| 104 | return; |
| 105 | } |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | int tries = 1000; |
| 110 | while (--tries) |
| 111 | { |
| 112 | args->id = tm->last_used_interface_id++; |
| 113 | p = hash_get (tm->dev_instance_by_interface_id, args->id); |
| 114 | if (!p) |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | if (!tries) |
| 119 | { |
| 120 | args->rv = VNET_API_ERROR_UNSPECIFIED; |
| 121 | args->error = |
| 122 | clib_error_return (0, "cannot find free interface id"); |
| 123 | return; |
| 124 | } |
| 125 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 126 | |
| 127 | memset (&ifr, 0, sizeof (ifr)); |
| 128 | pool_get (vim->interfaces, vif); |
| 129 | vif->dev_instance = vif - vim->interfaces; |
| 130 | vif->tap_fd = -1; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 131 | vif->id = args->id; |
| 132 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 133 | if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0) |
| 134 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 135 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 136 | args->error = clib_error_return_unix (0, "open '/dev/vhost-net'"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 137 | goto error; |
| 138 | } |
| 139 | |
| 140 | _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features); |
| 141 | |
| 142 | if ((vif->remote_features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) == 0) |
| 143 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 144 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 145 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 146 | "VIRTIO_NET_F_MRG_RXBUF feature"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 147 | goto error; |
| 148 | } |
| 149 | |
| 150 | if ((vif->remote_features & (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) == 0) |
| 151 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 152 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 153 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 154 | "VIRTIO_RING_F_INDIRECT_DESC feature"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 155 | goto error; |
| 156 | } |
| 157 | |
| 158 | if ((vif->remote_features & (1ULL << VIRTIO_F_VERSION_1)) == 0) |
| 159 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 160 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 161 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 162 | "VIRTIO_F_VERSION_1 features"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 163 | goto error; |
| 164 | } |
| 165 | |
| 166 | vif->features |= 1ULL << VIRTIO_NET_F_MRG_RXBUF; |
| 167 | vif->features |= 1ULL << VIRTIO_F_VERSION_1; |
| 168 | vif->features |= 1ULL << VIRTIO_RING_F_INDIRECT_DESC; |
| 169 | |
| 170 | _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features); |
| 171 | |
| 172 | if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0) |
| 173 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 174 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 175 | args->error = clib_error_return_unix (0, "open '/dev/net/tun'"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 176 | goto error; |
| 177 | } |
| 178 | |
| 179 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 180 | _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr); |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 181 | vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 182 | |
| 183 | unsigned int offload = 0; |
| 184 | hdrsz = sizeof (struct virtio_net_hdr_v1); |
| 185 | _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload); |
| 186 | _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz); |
| 187 | _IOCTL (vif->fd, VHOST_SET_OWNER, 0); |
| 188 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 189 | /* if namespace is specified, all further netlink messages should be excuted |
| 190 | after we change our net namespace */ |
| 191 | if (args->host_namespace) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 192 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 193 | int fd; |
| 194 | old_netns_fd = open ("/proc/self/ns/net", O_RDONLY); |
| 195 | if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1) |
| 196 | { |
| 197 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 198 | args->error = clib_error_return_unix (0, "open_netns_fd '%s'", |
| 199 | args->host_namespace); |
| 200 | goto error; |
| 201 | } |
| 202 | args->error = vnet_netlink_set_link_netns (vif->ifindex, fd, |
| 203 | (char *) args->host_if_name); |
| 204 | if (args->error) |
| 205 | { |
| 206 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 207 | goto error; |
| 208 | } |
| 209 | if (setns (fd, CLONE_NEWNET) == -1) |
| 210 | { |
| 211 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 212 | args->error = clib_error_return_unix (0, "setns '%s'", |
| 213 | args->host_namespace); |
| 214 | goto error; |
| 215 | } |
| 216 | close (fd); |
| 217 | if ((vif->ifindex = if_nametoindex ((char *) args->host_if_name)) == 0) |
| 218 | { |
| 219 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 220 | args->error = clib_error_return_unix (0, "if_nametoindex '%s'", |
| 221 | args->host_if_name); |
| 222 | goto error; |
| 223 | } |
| 224 | } |
| 225 | else |
| 226 | { |
| 227 | if (args->host_if_name) |
| 228 | { |
| 229 | args->error = vnet_netlink_set_link_name (vif->ifindex, |
| 230 | (char *) |
| 231 | args->host_if_name); |
| 232 | if (args->error) |
| 233 | { |
| 234 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 235 | goto error; |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | if (!ethernet_mac_address_is_zero (args->host_mac_addr)) |
| 241 | { |
| 242 | args->error = vnet_netlink_set_link_addr (vif->ifindex, |
| 243 | args->host_mac_addr); |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 244 | if (args->error) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 245 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 246 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 247 | goto error; |
| 248 | } |
| 249 | } |
| 250 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 251 | if (args->host_bridge) |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 252 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 253 | args->error = vnet_netlink_set_link_master (vif->ifindex, |
| 254 | (char *) args->host_bridge); |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 255 | if (args->error) |
| 256 | { |
| 257 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 258 | goto error; |
| 259 | } |
| 260 | } |
| 261 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 262 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 263 | if (args->host_ip4_prefix_len) |
| 264 | { |
| 265 | args->error = vnet_netlink_add_ip4_addr (vif->ifindex, |
| 266 | &args->host_ip4_addr, |
| 267 | args->host_ip4_prefix_len); |
| 268 | if (args->error) |
| 269 | { |
| 270 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 271 | goto error; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if (args->host_ip6_prefix_len) |
| 276 | { |
| 277 | args->error = vnet_netlink_add_ip6_addr (vif->ifindex, |
| 278 | &args->host_ip6_addr, |
| 279 | args->host_ip6_prefix_len); |
| 280 | if (args->error) |
| 281 | { |
| 282 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 283 | goto error; |
| 284 | } |
| 285 | } |
| 286 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 287 | args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ ); |
| 288 | if (args->error) |
| 289 | { |
| 290 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 291 | goto error; |
| 292 | } |
| 293 | |
Damjan Marion | 7866c45 | 2018-01-18 13:35:11 +0100 | [diff] [blame] | 294 | if (args->host_ip4_gw_set) |
| 295 | { |
| 296 | args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw); |
| 297 | if (args->error) |
| 298 | { |
| 299 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 300 | goto error; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | if (args->host_ip6_gw_set) |
| 305 | { |
| 306 | args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw); |
| 307 | if (args->error) |
| 308 | { |
| 309 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 310 | goto error; |
| 311 | } |
| 312 | } |
| 313 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 314 | /* switch back to old net namespace */ |
| 315 | if (args->host_namespace) |
| 316 | { |
| 317 | if (setns (old_netns_fd, CLONE_NEWNET) == -1) |
| 318 | { |
| 319 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 320 | args->error = clib_error_return_unix (0, "setns '%s'", |
| 321 | args->host_namespace); |
| 322 | goto error; |
| 323 | } |
| 324 | } |
| 325 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 326 | /* Set vhost memory table */ |
| 327 | i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region); |
| 328 | vhost_mem = clib_mem_alloc (i); |
| 329 | memset (vhost_mem, 0, i); |
| 330 | vhost_mem->nregions = 1; |
| 331 | vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096; |
| 332 | _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem); |
| 333 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 334 | if ((args->error = virtio_vring_init (vm, vif, 0, args->rx_ring_sz))) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 335 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 336 | args->rv = VNET_API_ERROR_INIT_FAILED; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 337 | goto error; |
| 338 | } |
| 339 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 340 | if ((args->error = virtio_vring_init (vm, vif, 1, args->tx_ring_sz))) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 341 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 342 | args->rv = VNET_API_ERROR_INIT_FAILED; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 343 | goto error; |
| 344 | } |
| 345 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 346 | if (!args->mac_addr_set) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 347 | { |
| 348 | f64 now = vlib_time_now (vm); |
| 349 | u32 rnd; |
| 350 | rnd = (u32) (now * 1e6); |
| 351 | rnd = random_u32 (&rnd); |
| 352 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 353 | memcpy (args->mac_addr + 2, &rnd, sizeof (rnd)); |
| 354 | args->mac_addr[0] = 2; |
| 355 | args->mac_addr[1] = 0xfe; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 356 | } |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 357 | vif->rx_ring_sz = args->rx_ring_sz != 0 ? args->rx_ring_sz : 256; |
| 358 | vif->tx_ring_sz = args->tx_ring_sz != 0 ? args->tx_ring_sz : 256; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 359 | vif->host_if_name = args->host_if_name; |
| 360 | args->host_if_name = 0; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 361 | vif->net_ns = args->host_namespace; |
| 362 | args->host_namespace = 0; |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 363 | vif->host_bridge = args->host_bridge; |
| 364 | args->host_bridge = 0; |
| 365 | clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6); |
| 366 | vif->host_ip4_prefix_len = args->host_ip4_prefix_len; |
| 367 | vif->host_ip6_prefix_len = args->host_ip6_prefix_len; |
| 368 | if (args->host_ip4_prefix_len) |
| 369 | clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4); |
| 370 | if (args->host_ip6_prefix_len) |
| 371 | clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16); |
| 372 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 373 | args->error = ethernet_register_interface (vnm, virtio_device_class.index, |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 374 | vif->dev_instance, |
| 375 | args->mac_addr, |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 376 | &vif->hw_if_index, |
| 377 | virtio_eth_flag_change); |
| 378 | if (args->error) |
| 379 | { |
| 380 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
| 381 | goto error; |
| 382 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 383 | |
Steven | 9e63569 | 2018-03-01 09:36:01 -0800 | [diff] [blame^] | 384 | hash_set (tm->dev_instance_by_interface_id, vif->id, vif->dev_instance); |
| 385 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 386 | sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index); |
| 387 | vif->sw_if_index = sw->sw_if_index; |
| 388 | args->sw_if_index = vif->sw_if_index; |
| 389 | hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 390 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE; |
| 391 | vnet_hw_interface_set_input_node (vnm, vif->hw_if_index, |
| 392 | virtio_input_node.index); |
| 393 | vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0); |
| 394 | vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0, |
| 395 | VNET_HW_INTERFACE_RX_MODE_DEFAULT); |
| 396 | vif->per_interface_next_index = ~0; |
| 397 | vif->type = VIRTIO_IF_TYPE_TAP; |
| 398 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 399 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 400 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
Damjan Marion | 829ee53 | 2018-02-16 16:13:32 +0100 | [diff] [blame] | 401 | if (thm->n_vlib_mains > 1) |
| 402 | clib_spinlock_init (&vif->lockp); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 403 | goto done; |
| 404 | |
| 405 | error: |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 406 | if (err) |
| 407 | { |
| 408 | ASSERT (args->error == 0); |
| 409 | args->error = err; |
| 410 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 411 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 412 | if (vif->tap_fd != -1) |
| 413 | close (vif->tap_fd); |
| 414 | if (vif->fd != -1) |
| 415 | close (vif->fd); |
Steven | a624dbe | 2018-01-09 11:13:29 -0800 | [diff] [blame] | 416 | vec_foreach_index (i, vif->vrings) virtio_vring_free (vm, vif, i); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 417 | memset (vif, 0, sizeof (virtio_if_t)); |
| 418 | pool_put (vim->interfaces, vif); |
| 419 | |
| 420 | done: |
| 421 | if (vhost_mem) |
| 422 | clib_mem_free (vhost_mem); |
Damjan Marion | 4e671d2 | 2017-12-09 21:19:01 +0100 | [diff] [blame] | 423 | if (old_netns_fd != -1) |
| 424 | close (old_netns_fd); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | int |
| 428 | tap_delete_if (vlib_main_t * vm, u32 sw_if_index) |
| 429 | { |
| 430 | vnet_main_t *vnm = vnet_get_main (); |
| 431 | virtio_main_t *mm = &virtio_main; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 432 | tap_main_t *tm = &tap_main; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 433 | int i; |
| 434 | virtio_if_t *vif; |
| 435 | vnet_hw_interface_t *hw; |
| 436 | |
| 437 | hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 438 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 439 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 440 | |
| 441 | vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); |
| 442 | |
| 443 | /* bring down the interface */ |
| 444 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
| 445 | vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0); |
Steven | 0b85673 | 2018-02-28 11:00:34 -0800 | [diff] [blame] | 446 | vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, 0); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 447 | |
| 448 | ethernet_delete_interface (vnm, vif->hw_if_index); |
| 449 | vif->hw_if_index = ~0; |
| 450 | |
| 451 | if (vif->tap_fd != -1) |
| 452 | close (vif->tap_fd); |
| 453 | if (vif->fd != -1) |
| 454 | close (vif->fd); |
| 455 | |
Steven | a624dbe | 2018-01-09 11:13:29 -0800 | [diff] [blame] | 456 | vec_foreach_index (i, vif->vrings) virtio_vring_free (vm, vif, i); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 457 | vec_free (vif->vrings); |
| 458 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 459 | hash_unset (tm->dev_instance_by_interface_id, vif->id); |
Damjan Marion | 829ee53 | 2018-02-16 16:13:32 +0100 | [diff] [blame] | 460 | clib_spinlock_free (&vif->lockp); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 461 | memset (vif, 0, sizeof (*vif)); |
| 462 | pool_put (mm->interfaces, vif); |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | int |
| 468 | tap_dump_ifs (tap_interface_details_t ** out_tapids) |
| 469 | { |
| 470 | vnet_main_t *vnm = vnet_get_main (); |
| 471 | virtio_main_t *mm = &virtio_main; |
| 472 | virtio_if_t *vif; |
| 473 | vnet_hw_interface_t *hi; |
| 474 | tap_interface_details_t *r_tapids = NULL; |
| 475 | tap_interface_details_t *tapid = NULL; |
| 476 | |
| 477 | /* *INDENT-OFF* */ |
| 478 | pool_foreach (vif, mm->interfaces, |
| 479 | vec_add2(r_tapids, tapid, 1); |
| 480 | memset (tapid, 0, sizeof (*tapid)); |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 481 | tapid->id = vif->id; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 482 | tapid->sw_if_index = vif->sw_if_index; |
| 483 | hi = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 484 | clib_memcpy(tapid->dev_name, hi->name, |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 485 | MIN (ARRAY_LEN (tapid->dev_name) - 1, |
| 486 | strlen ((const char *) hi->name))); |
| 487 | tapid->rx_ring_sz = vif->rx_ring_sz; |
| 488 | tapid->tx_ring_sz = vif->tx_ring_sz; |
| 489 | clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6); |
| 490 | if (vif->host_if_name) |
| 491 | { |
| 492 | clib_memcpy(tapid->host_if_name, vif->host_if_name, |
| 493 | MIN (ARRAY_LEN (tapid->host_if_name) - 1, |
| 494 | strlen ((const char *) vif->host_if_name))); |
| 495 | } |
| 496 | if (vif->net_ns) |
| 497 | { |
| 498 | clib_memcpy(tapid->host_namespace, vif->net_ns, |
| 499 | MIN (ARRAY_LEN (tapid->host_namespace) - 1, |
| 500 | strlen ((const char *) vif->net_ns))); |
| 501 | } |
| 502 | if (vif->host_bridge) |
| 503 | { |
| 504 | clib_memcpy(tapid->host_bridge, vif->host_bridge, |
| 505 | MIN (ARRAY_LEN (tapid->host_bridge) - 1, |
| 506 | strlen ((const char *) vif->host_bridge))); |
| 507 | } |
| 508 | if (vif->host_ip4_prefix_len) |
| 509 | clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4); |
| 510 | tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len; |
| 511 | if (vif->host_ip6_prefix_len) |
| 512 | clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16); |
| 513 | tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 514 | ); |
| 515 | /* *INDENT-ON* */ |
| 516 | |
| 517 | *out_tapids = r_tapids; |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | static clib_error_t * |
| 523 | tap_init (vlib_main_t * vm) |
| 524 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 525 | tap_main_t *tm = &tap_main; |
| 526 | tm->dev_instance_by_interface_id = hash_create (0, sizeof (uword)); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | VLIB_INIT_FUNCTION (tap_init); |
| 531 | |
| 532 | /* |
| 533 | * fd.io coding-style-patch-verification: ON |
| 534 | * |
| 535 | * Local Variables: |
| 536 | * eval: (c-set-style "gnu") |
| 537 | * End: |
| 538 | */ |