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