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
60 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
63 call_tap_read_ready (clib_file_t * uf)
70 tap_delete_if_cp (u32 * sw_if_index)
72 vlib_main_t *vm = vlib_get_main ();
73 tap_delete_if (vm, *sw_if_index);
77 * Tap clean-up routine:
78 * Linux side of tap interface can be deleted i.e. tap is
79 * attached to container and if someone will delete this
80 * container, will also removes tap interface. While VPP
81 * will have other side of tap. This function will RPC
82 * main thread to call the tap_delete_if to cleanup tap.
85 call_tap_error_ready (clib_file_t * uf)
87 vl_api_rpc_call_main_thread (tap_delete_if_cp, (u8 *) & uf->private_data,
88 sizeof (uf->private_data));
93 open_netns_fd (char *netns)
98 if (strncmp (netns, "pid:", 4) == 0)
99 s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0);
100 else if (netns[0] == '/')
101 s = format (0, "%s%c", netns, 0);
103 s = format (0, "/var/run/netns/%s%c", netns, 0);
105 fd = open ((char *) s, O_RDONLY);
110 #define TAP_MAX_INSTANCE 1024
113 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
115 vnet_main_t *vnm = vnet_get_main ();
116 virtio_main_t *vim = &virtio_main;
117 tap_main_t *tm = &tap_main;
118 vnet_sw_interface_t *sw;
119 vnet_hw_interface_t *hw;
121 int old_netns_fd = -1;
124 struct vhost_memory *vhost_mem = 0;
125 virtio_if_t *vif = 0;
126 clib_file_t t = { 0 };
127 clib_error_t *err = 0;
132 if (clib_bitmap_get (tm->tap_ids, args->id))
134 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
135 args->error = clib_error_return (0, "interface already exists");
141 args->id = clib_bitmap_first_clear (tm->tap_ids);
144 if (args->id > TAP_MAX_INSTANCE)
146 args->rv = VNET_API_ERROR_UNSPECIFIED;
147 args->error = clib_error_return (0, "cannot find free interface id");
151 clib_memset (&ifr, 0, sizeof (ifr));
152 pool_get (vim->interfaces, vif);
153 vif->dev_instance = vif - vim->interfaces;
157 if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
159 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
160 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
164 _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features);
166 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
168 args->rv = VNET_API_ERROR_UNSUPPORTED;
169 args->error = clib_error_return (0, "vhost-net backend doesn't support "
170 "VIRTIO_NET_F_MRG_RXBUF feature");
174 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
177 args->rv = VNET_API_ERROR_UNSUPPORTED;
178 args->error = clib_error_return (0, "vhost-net backend doesn't support "
179 "VIRTIO_RING_F_INDIRECT_DESC feature");
183 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
185 args->rv = VNET_API_ERROR_UNSUPPORTED;
186 args->error = clib_error_return (0, "vhost-net backend doesn't support "
187 "VIRTIO_F_VERSION_1 features");
191 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
192 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
193 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
195 virtio_set_net_hdr_size (vif);
197 _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features);
199 if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
201 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
202 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
206 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
207 _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr);
208 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
210 if (!args->host_if_name)
211 args->host_if_name = (void *) ifr.ifr_ifrn.ifrn_name;
213 unsigned int offload = 0;
214 hdrsz = sizeof (struct virtio_net_hdr_v1);
215 if (args->tap_flags & TAP_FLAG_GSO)
217 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
218 vif->gso_enabled = 1;
222 vif->gso_enabled = 0;
225 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
226 _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz);
227 _IOCTL (vif->fd, VHOST_SET_OWNER, 0);
229 /* if namespace is specified, all further netlink messages should be excuted
230 after we change our net namespace */
231 if (args->host_namespace)
233 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
234 if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1)
236 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
237 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
238 args->host_namespace);
241 args->error = vnet_netlink_set_link_netns (vif->ifindex, fd,
242 (char *) args->host_if_name);
245 args->rv = VNET_API_ERROR_NETLINK_ERROR;
248 if (setns (fd, CLONE_NEWNET) == -1)
250 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
251 args->error = clib_error_return_unix (0, "setns '%s'",
252 args->host_namespace);
255 if ((vif->ifindex = if_nametoindex ((char *) args->host_if_name)) == 0)
257 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
258 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
265 if (args->host_if_name)
267 args->error = vnet_netlink_set_link_name (vif->ifindex,
272 args->rv = VNET_API_ERROR_NETLINK_ERROR;
278 if (!ethernet_mac_address_is_zero (args->host_mac_addr))
280 args->error = vnet_netlink_set_link_addr (vif->ifindex,
281 args->host_mac_addr);
284 args->rv = VNET_API_ERROR_NETLINK_ERROR;
289 if (args->host_bridge)
291 args->error = vnet_netlink_set_link_master (vif->ifindex,
292 (char *) args->host_bridge);
295 args->rv = VNET_API_ERROR_NETLINK_ERROR;
301 if (args->host_ip4_prefix_len)
303 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
304 &args->host_ip4_addr,
305 args->host_ip4_prefix_len);
308 args->rv = VNET_API_ERROR_NETLINK_ERROR;
313 if (args->host_ip6_prefix_len)
315 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
316 &args->host_ip6_addr,
317 args->host_ip6_prefix_len);
320 args->rv = VNET_API_ERROR_NETLINK_ERROR;
325 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
328 args->rv = VNET_API_ERROR_NETLINK_ERROR;
332 if (args->host_ip4_gw_set)
334 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
337 args->rv = VNET_API_ERROR_NETLINK_ERROR;
342 if (args->host_ip6_gw_set)
344 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
347 args->rv = VNET_API_ERROR_NETLINK_ERROR;
352 /* switch back to old net namespace */
353 if (args->host_namespace)
355 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
357 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
358 args->error = clib_error_return_unix (0, "setns '%s'",
359 args->host_namespace);
364 if (args->host_mtu_set)
367 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
370 args->rv = VNET_API_ERROR_NETLINK_ERROR;
374 else if (tm->host_mtu_size != 0)
377 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
380 args->rv = VNET_API_ERROR_NETLINK_ERROR;
383 args->host_mtu_set = 1;
384 args->host_mtu_size = tm->host_mtu_size;
387 /* Set vhost memory table */
388 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
389 vhost_mem = clib_mem_alloc (i);
390 clib_memset (vhost_mem, 0, i);
391 vhost_mem->nregions = 1;
392 vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096;
393 _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem);
396 virtio_vring_init (vm, vif, RX_QUEUE (0), args->rx_ring_sz)))
398 args->rv = VNET_API_ERROR_INIT_FAILED;
404 virtio_vring_init (vm, vif, TX_QUEUE (0), args->tx_ring_sz)))
406 args->rv = VNET_API_ERROR_INIT_FAILED;
411 if (!args->mac_addr_set)
412 ethernet_mac_address_generate (args->mac_addr);
414 clib_memcpy (vif->mac_addr, args->mac_addr, 6);
416 vif->host_if_name = format (0, "%s%c", args->host_if_name, 0);
417 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
418 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
419 vif->host_mtu_size = args->host_mtu_size;
420 clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6);
421 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
422 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
423 if (args->host_ip4_prefix_len)
424 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
425 if (args->host_ip6_prefix_len)
426 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
428 vif->type = VIRTIO_IF_TYPE_TAP;
429 args->error = ethernet_register_interface (vnm, virtio_device_class.index,
433 virtio_eth_flag_change);
436 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
440 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
441 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
442 vif->sw_if_index = sw->sw_if_index;
443 args->sw_if_index = vif->sw_if_index;
444 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
445 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
446 if (args->tap_flags & TAP_FLAG_GSO)
448 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
449 vnm->interface_main.gso_interface_count++;
451 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
452 virtio_input_node.index);
453 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
454 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
455 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
456 vif->per_interface_next_index = ~0;
457 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (0));
458 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
459 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
460 VNET_HW_INTERFACE_FLAG_LINK_UP);
461 vif->cxq_vring = NULL;
463 t.read_function = call_tap_read_ready;
464 t.error_function = call_tap_error_ready;
465 t.file_descriptor = vif->tap_fd;
466 t.private_data = vif->sw_if_index;
467 t.description = format (0, "tap sw_if_index %u fd: %u",
468 vif->sw_if_index, vif->tap_fd);
469 vif->tap_file_index = clib_file_add (&file_main, &t);
476 ASSERT (args->error == 0);
478 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
480 if (vif->tap_fd != -1)
484 vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif,
486 vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif,
488 vec_free (vif->rxq_vrings);
489 vec_free (vif->txq_vrings);
491 vec_free (vif->host_if_name);
492 vec_free (vif->net_ns);
493 vec_free (vif->host_bridge);
495 clib_memset (vif, 0, sizeof (virtio_if_t));
496 pool_put (vim->interfaces, vif);
500 clib_mem_free (vhost_mem);
501 if (old_netns_fd != -1)
502 close (old_netns_fd);
508 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
510 vnet_main_t *vnm = vnet_get_main ();
511 virtio_main_t *mm = &virtio_main;
512 tap_main_t *tm = &tap_main;
515 vnet_hw_interface_t *hw;
517 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
518 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
519 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
521 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
523 if (vif->type != VIRTIO_IF_TYPE_TAP)
524 return VNET_API_ERROR_INVALID_INTERFACE;
526 /* decrement if this was a GSO interface */
527 if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
528 vnm->interface_main.gso_interface_count--;
530 clib_file_del_by_index (&file_main, vif->tap_file_index);
531 /* bring down the interface */
532 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
533 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
534 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, RX_QUEUE (0));
536 ethernet_delete_interface (vnm, vif->hw_if_index);
537 vif->hw_if_index = ~0;
539 if (vif->tap_fd != -1)
544 vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif,
546 vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif,
548 vec_free (vif->rxq_vrings);
549 vec_free (vif->txq_vrings);
551 vec_free (vif->host_if_name);
552 vec_free (vif->net_ns);
553 vec_free (vif->host_bridge);
555 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
556 clib_memset (vif, 0, sizeof (*vif));
557 pool_put (mm->interfaces, vif);
563 tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable)
565 vnet_main_t *vnm = vnet_get_main ();
566 virtio_main_t *mm = &virtio_main;
568 vnet_hw_interface_t *hw;
569 clib_error_t *err = 0;
571 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
573 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
574 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
576 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
578 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
579 const unsigned int gso_off = 0;
580 unsigned int offload = enable_disable ? gso_on : gso_off;
581 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
582 vif->gso_enabled = enable_disable ? 1 : 0;
585 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0)
587 vnm->interface_main.gso_interface_count++;
588 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
593 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
595 vnm->interface_main.gso_interface_count--;
596 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
603 clib_warning ("Error %s gso on sw_if_index %d",
604 enable_disable ? "enabling" : "disabling", sw_if_index);
605 return VNET_API_ERROR_SYSCALL_ERROR_3;
611 tap_dump_ifs (tap_interface_details_t ** out_tapids)
613 vnet_main_t *vnm = vnet_get_main ();
614 virtio_main_t *mm = &virtio_main;
616 virtio_vring_t *vring;
617 vnet_hw_interface_t *hi;
618 tap_interface_details_t *r_tapids = NULL;
619 tap_interface_details_t *tapid = NULL;
622 pool_foreach (vif, mm->interfaces,
623 if (vif->type != VIRTIO_IF_TYPE_TAP)
625 vec_add2(r_tapids, tapid, 1);
626 clib_memset (tapid, 0, sizeof (*tapid));
628 tapid->sw_if_index = vif->sw_if_index;
629 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
630 clib_memcpy(tapid->dev_name, hi->name,
631 MIN (ARRAY_LEN (tapid->dev_name) - 1,
632 strlen ((const char *) hi->name)));
633 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
634 tapid->rx_ring_sz = vring->size;
635 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
636 tapid->tx_ring_sz = vring->size;
637 clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6);
638 if (vif->host_if_name)
640 clib_memcpy(tapid->host_if_name, vif->host_if_name,
641 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
642 strlen ((const char *) vif->host_if_name)));
646 clib_memcpy(tapid->host_namespace, vif->net_ns,
647 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
648 strlen ((const char *) vif->net_ns)));
650 if (vif->host_bridge)
652 clib_memcpy(tapid->host_bridge, vif->host_bridge,
653 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
654 strlen ((const char *) vif->host_bridge)));
656 if (vif->host_ip4_prefix_len)
657 clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4);
658 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
659 if (vif->host_ip6_prefix_len)
660 clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16);
661 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
662 tapid->host_mtu_size = vif->host_mtu_size;
666 *out_tapids = r_tapids;
671 static clib_error_t *
672 tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
674 tap_main_t *tm = &tap_main;
676 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
678 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
681 return clib_error_return (0, "unknown input `%U'",
682 format_unformat_error, input);
688 /* tap { host-mtu <size> } configuration. */
689 VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
691 static clib_error_t *
692 tap_init (vlib_main_t * vm)
694 tap_main_t *tm = &tap_main;
695 clib_error_t *error = 0;
697 tm->log_default = vlib_log_register_class ("tap", 0);
698 vlib_log_debug (tm->log_default, "initialized");
700 tm->host_mtu_size = 0;
705 VLIB_INIT_FUNCTION (tap_init);
708 * fd.io coding-style-patch-verification: ON
711 * eval: (c-set-style "gnu")