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/physmem.h>
35 #include <vlib/unix/unix.h>
36 #include <vnet/ethernet/ethernet.h>
37 #include <vnet/ip/ip4_packet.h>
38 #include <vnet/ip/ip6_packet.h>
39 #include <vnet/devices/netlink.h>
40 #include <vnet/devices/virtio/virtio.h>
41 #include <vnet/devices/tap/tap.h>
45 #define _IOCTL(fd,a,...) \
46 if (ioctl (fd, a, __VA_ARGS__) < 0) \
48 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
53 virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
57 //TODO On MTU change call vnet_netlink_set_if_mtu
62 open_netns_fd (char *netns)
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);
72 s = format (0, "/var/run/netns/%s%c", netns, 0);
74 fd = open ((char *) s, O_RDONLY);
79 #define TAP_MAX_INSTANCE 1024
82 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
84 vlib_physmem_main_t *vpm = &vm->physmem_main;
85 vnet_main_t *vnm = vnet_get_main ();
86 virtio_main_t *vim = &virtio_main;
87 tap_main_t *tm = &tap_main;
88 vnet_sw_interface_t *sw;
89 vnet_hw_interface_t *hw;
91 int old_netns_fd = -1;
94 struct vhost_memory *vhost_mem = 0;
96 clib_error_t *err = 0;
98 char *host_if_name = 0;
102 if (clib_bitmap_get (tm->tap_ids, args->id))
104 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
105 args->error = clib_error_return (0, "interface already exists");
111 args->id = clib_bitmap_first_clear (tm->tap_ids);
114 if (args->id > TAP_MAX_INSTANCE)
116 args->rv = VNET_API_ERROR_UNSPECIFIED;
117 args->error = clib_error_return (0, "cannot find free interface id");
121 clib_memset (&ifr, 0, sizeof (ifr));
122 pool_get (vim->interfaces, vif);
123 vif->dev_instance = vif - vim->interfaces;
127 if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
129 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
130 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
134 _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features);
136 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
138 args->rv = VNET_API_ERROR_UNSUPPORTED;
139 args->error = clib_error_return (0, "vhost-net backend doesn't support "
140 "VIRTIO_NET_F_MRG_RXBUF feature");
144 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
147 args->rv = VNET_API_ERROR_UNSUPPORTED;
148 args->error = clib_error_return (0, "vhost-net backend doesn't support "
149 "VIRTIO_RING_F_INDIRECT_DESC feature");
153 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
155 args->rv = VNET_API_ERROR_UNSUPPORTED;
156 args->error = clib_error_return (0, "vhost-net backend doesn't support "
157 "VIRTIO_F_VERSION_1 features");
161 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
162 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
163 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
165 virtio_set_net_hdr_size (vif);
167 _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features);
169 if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
171 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
172 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
176 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
177 _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr);
178 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
180 if (!args->host_if_name)
181 host_if_name = ifr.ifr_ifrn.ifrn_name;
183 host_if_name = (char *) args->host_if_name;
185 unsigned int offload = 0;
186 hdrsz = sizeof (struct virtio_net_hdr_v1);
187 if (args->tap_flags & TAP_FLAG_GSO)
189 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
190 vif->gso_enabled = 1;
194 vif->gso_enabled = 0;
197 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
198 _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz);
199 _IOCTL (vif->fd, VHOST_SET_OWNER, 0);
201 /* if namespace is specified, all further netlink messages should be executed
202 after we change our net namespace */
203 if (args->host_namespace)
205 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
206 if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1)
208 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
209 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
210 args->host_namespace);
213 args->error = vnet_netlink_set_link_netns (vif->ifindex, fd,
217 args->rv = VNET_API_ERROR_NETLINK_ERROR;
220 if (setns (fd, CLONE_NEWNET) == -1)
222 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
223 args->error = clib_error_return_unix (0, "setns '%s'",
224 args->host_namespace);
227 if ((vif->ifindex = if_nametoindex (host_if_name)) == 0)
229 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
230 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
239 args->error = vnet_netlink_set_link_name (vif->ifindex,
243 args->rv = VNET_API_ERROR_NETLINK_ERROR;
249 if (!ethernet_mac_address_is_zero (args->host_mac_addr))
251 args->error = vnet_netlink_set_link_addr (vif->ifindex,
252 args->host_mac_addr);
255 args->rv = VNET_API_ERROR_NETLINK_ERROR;
260 if (args->host_bridge)
262 args->error = vnet_netlink_set_link_master (vif->ifindex,
263 (char *) args->host_bridge);
266 args->rv = VNET_API_ERROR_NETLINK_ERROR;
272 if (args->host_ip4_prefix_len)
274 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
275 &args->host_ip4_addr,
276 args->host_ip4_prefix_len);
279 args->rv = VNET_API_ERROR_NETLINK_ERROR;
284 if (args->host_ip6_prefix_len)
286 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
287 &args->host_ip6_addr,
288 args->host_ip6_prefix_len);
291 args->rv = VNET_API_ERROR_NETLINK_ERROR;
296 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
299 args->rv = VNET_API_ERROR_NETLINK_ERROR;
303 if (args->host_ip4_gw_set)
305 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
308 args->rv = VNET_API_ERROR_NETLINK_ERROR;
313 if (args->host_ip6_gw_set)
315 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
318 args->rv = VNET_API_ERROR_NETLINK_ERROR;
323 /* switch back to old net namespace */
324 if (args->host_namespace)
326 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
328 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
329 args->error = clib_error_return_unix (0, "setns '%s'",
330 args->host_namespace);
335 if (args->host_mtu_set)
338 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
341 args->rv = VNET_API_ERROR_NETLINK_ERROR;
345 else if (tm->host_mtu_size != 0)
348 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
351 args->rv = VNET_API_ERROR_NETLINK_ERROR;
354 args->host_mtu_set = 1;
355 args->host_mtu_size = tm->host_mtu_size;
358 /* Set vhost memory table */
359 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
360 vhost_mem = clib_mem_alloc (i);
361 clib_memset (vhost_mem, 0, i);
362 vhost_mem->nregions = 1;
363 vhost_mem->regions[0].memory_size = vpm->max_size;
364 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
365 vhost_mem->regions[0].userspace_addr =
366 vhost_mem->regions[0].guest_phys_addr;
367 _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem);
370 virtio_vring_init (vm, vif, RX_QUEUE (0), args->rx_ring_sz)))
372 args->rv = VNET_API_ERROR_INIT_FAILED;
378 virtio_vring_init (vm, vif, TX_QUEUE (0), args->tx_ring_sz)))
380 args->rv = VNET_API_ERROR_INIT_FAILED;
385 if (!args->mac_addr_set)
386 ethernet_mac_address_generate (args->mac_addr);
388 clib_memcpy (vif->mac_addr, args->mac_addr, 6);
390 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
391 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
392 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
393 vif->host_mtu_size = args->host_mtu_size;
394 clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6);
395 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
396 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
397 if (args->host_ip4_prefix_len)
398 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
399 if (args->host_ip6_prefix_len)
400 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
402 vif->type = VIRTIO_IF_TYPE_TAP;
403 args->error = ethernet_register_interface (vnm, virtio_device_class.index,
407 virtio_eth_flag_change);
410 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
414 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
415 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
416 vif->sw_if_index = sw->sw_if_index;
417 args->sw_if_index = vif->sw_if_index;
419 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
420 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
421 if (args->tap_flags & TAP_FLAG_GSO)
423 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
424 vnm->interface_main.gso_interface_count++;
426 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
427 virtio_input_node.index);
428 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
429 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
430 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
431 vif->per_interface_next_index = ~0;
432 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (0));
433 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
434 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
435 VNET_HW_INTERFACE_FLAG_LINK_UP);
436 vif->cxq_vring = NULL;
443 ASSERT (args->error == 0);
445 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
447 if (vif->tap_fd != -1)
451 vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif,
453 vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif,
455 vec_free (vif->rxq_vrings);
456 vec_free (vif->txq_vrings);
458 vec_free (vif->host_if_name);
459 vec_free (vif->net_ns);
460 vec_free (vif->host_bridge);
462 clib_memset (vif, 0, sizeof (virtio_if_t));
463 pool_put (vim->interfaces, vif);
467 clib_mem_free (vhost_mem);
468 if (old_netns_fd != -1)
469 close (old_netns_fd);
475 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
477 vnet_main_t *vnm = vnet_get_main ();
478 virtio_main_t *mm = &virtio_main;
479 tap_main_t *tm = &tap_main;
482 vnet_hw_interface_t *hw;
484 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
485 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
486 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
488 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
490 if (vif->type != VIRTIO_IF_TYPE_TAP)
491 return VNET_API_ERROR_INVALID_INTERFACE;
493 /* decrement if this was a GSO interface */
494 if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
495 vnm->interface_main.gso_interface_count--;
497 /* bring down the interface */
498 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
499 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
500 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, RX_QUEUE (0));
502 ethernet_delete_interface (vnm, vif->hw_if_index);
503 vif->hw_if_index = ~0;
505 if (vif->tap_fd != -1)
510 vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif,
512 vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif,
514 vec_free (vif->rxq_vrings);
515 vec_free (vif->txq_vrings);
517 vec_free (vif->host_if_name);
518 vec_free (vif->net_ns);
519 vec_free (vif->host_bridge);
521 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
522 clib_memset (vif, 0, sizeof (*vif));
523 pool_put (mm->interfaces, vif);
529 tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable)
531 vnet_main_t *vnm = vnet_get_main ();
532 virtio_main_t *mm = &virtio_main;
534 vnet_hw_interface_t *hw;
535 clib_error_t *err = 0;
537 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
539 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
540 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
542 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
544 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
545 const unsigned int gso_off = 0;
546 unsigned int offload = enable_disable ? gso_on : gso_off;
547 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
548 vif->gso_enabled = enable_disable ? 1 : 0;
551 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0)
553 vnm->interface_main.gso_interface_count++;
554 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
559 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
561 vnm->interface_main.gso_interface_count--;
562 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
569 clib_warning ("Error %s gso on sw_if_index %d",
570 enable_disable ? "enabling" : "disabling", sw_if_index);
571 return VNET_API_ERROR_SYSCALL_ERROR_3;
577 tap_dump_ifs (tap_interface_details_t ** out_tapids)
579 vnet_main_t *vnm = vnet_get_main ();
580 virtio_main_t *mm = &virtio_main;
582 virtio_vring_t *vring;
583 vnet_hw_interface_t *hi;
584 tap_interface_details_t *r_tapids = NULL;
585 tap_interface_details_t *tapid = NULL;
588 pool_foreach (vif, mm->interfaces,
589 if (vif->type != VIRTIO_IF_TYPE_TAP)
591 vec_add2(r_tapids, tapid, 1);
592 clib_memset (tapid, 0, sizeof (*tapid));
594 tapid->sw_if_index = vif->sw_if_index;
595 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
596 clib_memcpy(tapid->dev_name, hi->name,
597 MIN (ARRAY_LEN (tapid->dev_name) - 1,
598 strlen ((const char *) hi->name)));
599 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
600 tapid->rx_ring_sz = vring->size;
601 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
602 tapid->tx_ring_sz = vring->size;
603 clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6);
604 if (vif->host_if_name)
606 clib_memcpy(tapid->host_if_name, vif->host_if_name,
607 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
608 strlen ((const char *) vif->host_if_name)));
612 clib_memcpy(tapid->host_namespace, vif->net_ns,
613 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
614 strlen ((const char *) vif->net_ns)));
616 if (vif->host_bridge)
618 clib_memcpy(tapid->host_bridge, vif->host_bridge,
619 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
620 strlen ((const char *) vif->host_bridge)));
622 if (vif->host_ip4_prefix_len)
623 clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4);
624 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
625 if (vif->host_ip6_prefix_len)
626 clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16);
627 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
628 tapid->host_mtu_size = vif->host_mtu_size;
632 *out_tapids = r_tapids;
637 static clib_error_t *
638 tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
640 tap_main_t *tm = &tap_main;
642 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
644 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
647 return clib_error_return (0, "unknown input `%U'",
648 format_unformat_error, input);
654 /* tap { host-mtu <size> } configuration. */
655 VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
657 static clib_error_t *
658 tap_init (vlib_main_t * vm)
660 tap_main_t *tm = &tap_main;
661 clib_error_t *error = 0;
663 tm->log_default = vlib_log_register_class ("tap", 0);
664 vlib_log_debug (tm->log_default, "initialized");
666 tm->host_mtu_size = 0;
671 VLIB_INIT_FUNCTION (tap_init);
674 * fd.io coding-style-patch-verification: ON
677 * eval: (c-set-style "gnu")