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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
19 #include <sys/types.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>
30 #include <linux/netlink.h>
31 #include <linux/rtnetlink.h>
33 #include <vlib/vlib.h>
34 #include <vlib/unix/unix.h>
35 #include <vnet/ethernet/ethernet.h>
36 #include <vnet/ip/ip4_packet.h>
37 #include <vnet/ip/ip6_packet.h>
38 #include <vnet/devices/netlink.h>
39 #include <vnet/devices/virtio/virtio.h>
40 #include <vnet/devices/tap/tap.h>
44 #define _IOCTL(fd,a,...) \
45 if (ioctl (fd, a, __VA_ARGS__) < 0) \
47 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
52 virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
56 //TODO On MTU change call vnet_netlink_set_if_mtu
61 open_netns_fd (char *netns)
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);
71 s = format (0, "/var/run/netns/%s%c", netns, 0);
73 fd = open ((char *) s, O_RDONLY);
78 #define TAP_MAX_INSTANCE 1024
81 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
83 vnet_main_t *vnm = vnet_get_main ();
84 virtio_main_t *vim = &virtio_main;
85 tap_main_t *tm = &tap_main;
86 vnet_sw_interface_t *sw;
87 vnet_hw_interface_t *hw;
89 int old_netns_fd = -1;
92 struct vhost_memory *vhost_mem = 0;
94 clib_error_t *err = 0;
99 if (clib_bitmap_get (tm->tap_ids, args->id))
101 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
102 args->error = clib_error_return (0, "interface already exists");
108 args->id = clib_bitmap_first_clear (tm->tap_ids);
111 if (args->id > TAP_MAX_INSTANCE)
113 args->rv = VNET_API_ERROR_UNSPECIFIED;
114 args->error = clib_error_return (0, "cannot find free interface id");
118 clib_memset (&ifr, 0, sizeof (ifr));
119 pool_get (vim->interfaces, vif);
120 vif->dev_instance = vif - vim->interfaces;
124 if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
126 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
127 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
131 _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features);
133 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
135 args->rv = VNET_API_ERROR_UNSUPPORTED;
136 args->error = clib_error_return (0, "vhost-net backend doesn't support "
137 "VIRTIO_NET_F_MRG_RXBUF feature");
141 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
144 args->rv = VNET_API_ERROR_UNSUPPORTED;
145 args->error = clib_error_return (0, "vhost-net backend doesn't support "
146 "VIRTIO_RING_F_INDIRECT_DESC feature");
150 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
152 args->rv = VNET_API_ERROR_UNSUPPORTED;
153 args->error = clib_error_return (0, "vhost-net backend doesn't support "
154 "VIRTIO_F_VERSION_1 features");
158 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
159 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
160 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
162 virtio_set_net_hdr_size (vif);
164 _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features);
166 if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
168 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
169 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
173 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
174 _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr);
175 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
177 if (!args->host_if_name)
178 args->host_if_name = (u8 *) ifr.ifr_ifrn.ifrn_name;
180 unsigned int offload = 0;
181 hdrsz = sizeof (struct virtio_net_hdr_v1);
182 if (args->tap_flags & TAP_FLAG_GSO)
184 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
185 vif->gso_enabled = 1;
189 vif->gso_enabled = 0;
192 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
193 _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz);
194 _IOCTL (vif->fd, VHOST_SET_OWNER, 0);
196 /* if namespace is specified, all further netlink messages should be excuted
197 after we change our net namespace */
198 if (args->host_namespace)
200 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
201 if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1)
203 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
204 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
205 args->host_namespace);
208 args->error = vnet_netlink_set_link_netns (vif->ifindex, fd,
209 (char *) args->host_if_name);
212 args->rv = VNET_API_ERROR_NETLINK_ERROR;
215 if (setns (fd, CLONE_NEWNET) == -1)
217 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
218 args->error = clib_error_return_unix (0, "setns '%s'",
219 args->host_namespace);
222 if ((vif->ifindex = if_nametoindex ((char *) args->host_if_name)) == 0)
224 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
225 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
232 if (args->host_if_name)
234 args->error = vnet_netlink_set_link_name (vif->ifindex,
239 args->rv = VNET_API_ERROR_NETLINK_ERROR;
245 if (!ethernet_mac_address_is_zero (args->host_mac_addr))
247 args->error = vnet_netlink_set_link_addr (vif->ifindex,
248 args->host_mac_addr);
251 args->rv = VNET_API_ERROR_NETLINK_ERROR;
256 if (args->host_bridge)
258 args->error = vnet_netlink_set_link_master (vif->ifindex,
259 (char *) args->host_bridge);
262 args->rv = VNET_API_ERROR_NETLINK_ERROR;
268 if (args->host_ip4_prefix_len)
270 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
271 &args->host_ip4_addr,
272 args->host_ip4_prefix_len);
275 args->rv = VNET_API_ERROR_NETLINK_ERROR;
280 if (args->host_ip6_prefix_len)
282 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
283 &args->host_ip6_addr,
284 args->host_ip6_prefix_len);
287 args->rv = VNET_API_ERROR_NETLINK_ERROR;
292 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
295 args->rv = VNET_API_ERROR_NETLINK_ERROR;
299 if (args->host_ip4_gw_set)
301 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
304 args->rv = VNET_API_ERROR_NETLINK_ERROR;
309 if (args->host_ip6_gw_set)
311 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
314 args->rv = VNET_API_ERROR_NETLINK_ERROR;
319 /* switch back to old net namespace */
320 if (args->host_namespace)
322 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
324 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
325 args->error = clib_error_return_unix (0, "setns '%s'",
326 args->host_namespace);
331 /* Set vhost memory table */
332 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
333 vhost_mem = clib_mem_alloc (i);
334 clib_memset (vhost_mem, 0, i);
335 vhost_mem->nregions = 1;
336 vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096;
337 _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem);
340 virtio_vring_init (vm, vif, RX_QUEUE (0), args->rx_ring_sz)))
342 args->rv = VNET_API_ERROR_INIT_FAILED;
348 virtio_vring_init (vm, vif, TX_QUEUE (0), args->tx_ring_sz)))
350 args->rv = VNET_API_ERROR_INIT_FAILED;
355 if (!args->mac_addr_set)
356 ethernet_mac_address_generate (args->mac_addr);
358 clib_memcpy (vif->mac_addr, args->mac_addr, 6);
360 vif->host_if_name = args->host_if_name;
361 args->host_if_name = 0;
362 vif->net_ns = args->host_namespace;
363 args->host_namespace = 0;
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);
374 vif->type = VIRTIO_IF_TYPE_TAP;
375 args->error = ethernet_register_interface (vnm, virtio_device_class.index,
379 virtio_eth_flag_change);
382 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
386 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
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 if (args->tap_flags & TAP_FLAG_GSO)
394 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
395 vnm->interface_main.gso_interface_count++;
397 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
398 virtio_input_node.index);
399 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
400 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
401 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
402 vif->per_interface_next_index = ~0;
403 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (0));
404 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
405 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
406 VNET_HW_INTERFACE_FLAG_LINK_UP);
407 vif->cxq_vring = NULL;
414 ASSERT (args->error == 0);
416 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
418 if (vif->tap_fd != -1)
422 vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif,
424 vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif,
426 vec_free (vif->rxq_vrings);
427 vec_free (vif->txq_vrings);
428 clib_memset (vif, 0, sizeof (virtio_if_t));
429 pool_put (vim->interfaces, vif);
433 clib_mem_free (vhost_mem);
434 if (old_netns_fd != -1)
435 close (old_netns_fd);
441 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
443 vnet_main_t *vnm = vnet_get_main ();
444 virtio_main_t *mm = &virtio_main;
445 tap_main_t *tm = &tap_main;
448 vnet_hw_interface_t *hw;
450 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
451 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
452 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
454 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
456 if (vif->type != VIRTIO_IF_TYPE_TAP)
457 return VNET_API_ERROR_INVALID_INTERFACE;
459 /* decrement if this was a GSO interface */
460 if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
461 vnm->interface_main.gso_interface_count--;
463 /* bring down the interface */
464 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
465 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
466 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, RX_QUEUE (0));
468 ethernet_delete_interface (vnm, vif->hw_if_index);
469 vif->hw_if_index = ~0;
471 if (vif->tap_fd != -1)
476 vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif,
478 vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif,
480 vec_free (vif->rxq_vrings);
481 vec_free (vif->txq_vrings);
483 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
484 clib_memset (vif, 0, sizeof (*vif));
485 pool_put (mm->interfaces, vif);
491 tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable)
493 vnet_main_t *vnm = vnet_get_main ();
494 virtio_main_t *mm = &virtio_main;
496 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
497 clib_error_t *err = 0;
499 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
500 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
502 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
504 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
505 const unsigned int gso_off = 0;
506 unsigned int offload = enable_disable ? gso_on : gso_off;
507 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
508 vif->gso_enabled = enable_disable ? 1 : 0;
511 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0)
513 vnm->interface_main.gso_interface_count++;
514 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
519 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
521 vnm->interface_main.gso_interface_count--;
522 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
529 clib_warning ("Error %s gso on sw_if_index %d",
530 enable_disable ? "enabling" : "disabling", sw_if_index);
531 return VNET_API_ERROR_SYSCALL_ERROR_3;
537 tap_dump_ifs (tap_interface_details_t ** out_tapids)
539 vnet_main_t *vnm = vnet_get_main ();
540 virtio_main_t *mm = &virtio_main;
542 virtio_vring_t *vring;
543 vnet_hw_interface_t *hi;
544 tap_interface_details_t *r_tapids = NULL;
545 tap_interface_details_t *tapid = NULL;
548 pool_foreach (vif, mm->interfaces,
549 if (vif->type != VIRTIO_IF_TYPE_TAP)
551 vec_add2(r_tapids, tapid, 1);
552 clib_memset (tapid, 0, sizeof (*tapid));
554 tapid->sw_if_index = vif->sw_if_index;
555 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
556 clib_memcpy(tapid->dev_name, hi->name,
557 MIN (ARRAY_LEN (tapid->dev_name) - 1,
558 strlen ((const char *) hi->name)));
559 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
560 tapid->rx_ring_sz = vring->size;
561 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
562 tapid->tx_ring_sz = vring->size;
563 clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6);
564 if (vif->host_if_name)
566 clib_memcpy(tapid->host_if_name, vif->host_if_name,
567 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
568 strlen ((const char *) vif->host_if_name)));
572 clib_memcpy(tapid->host_namespace, vif->net_ns,
573 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
574 strlen ((const char *) vif->net_ns)));
576 if (vif->host_bridge)
578 clib_memcpy(tapid->host_bridge, vif->host_bridge,
579 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
580 strlen ((const char *) vif->host_bridge)));
582 if (vif->host_ip4_prefix_len)
583 clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4);
584 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
585 if (vif->host_ip6_prefix_len)
586 clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16);
587 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
591 *out_tapids = r_tapids;
596 static clib_error_t *
597 tap_init (vlib_main_t * vm)
599 tap_main_t *tm = &tap_main;
600 clib_error_t *error = 0;
602 tm->log_default = vlib_log_register_class ("tap", 0);
603 vlib_log_debug (tm->log_default, "initialized");
608 VLIB_INIT_FUNCTION (tap_init);
611 * fd.io coding-style-patch-verification: ON
614 * eval: (c-set-style "gnu")