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;
96 char *host_if_name = 0;
100 if (clib_bitmap_get (tm->tap_ids, args->id))
102 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
103 args->error = clib_error_return (0, "interface already exists");
109 args->id = clib_bitmap_first_clear (tm->tap_ids);
112 if (args->id > TAP_MAX_INSTANCE)
114 args->rv = VNET_API_ERROR_UNSPECIFIED;
115 args->error = clib_error_return (0, "cannot find free interface id");
119 clib_memset (&ifr, 0, sizeof (ifr));
120 pool_get (vim->interfaces, vif);
121 vif->dev_instance = vif - vim->interfaces;
125 if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
127 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
128 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
132 _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features);
134 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
136 args->rv = VNET_API_ERROR_UNSUPPORTED;
137 args->error = clib_error_return (0, "vhost-net backend doesn't support "
138 "VIRTIO_NET_F_MRG_RXBUF feature");
142 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
145 args->rv = VNET_API_ERROR_UNSUPPORTED;
146 args->error = clib_error_return (0, "vhost-net backend doesn't support "
147 "VIRTIO_RING_F_INDIRECT_DESC feature");
151 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
153 args->rv = VNET_API_ERROR_UNSUPPORTED;
154 args->error = clib_error_return (0, "vhost-net backend doesn't support "
155 "VIRTIO_F_VERSION_1 features");
159 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
160 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
161 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
163 virtio_set_net_hdr_size (vif);
165 _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features);
167 if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
169 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
170 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
174 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
175 _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr);
176 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
178 if (!args->host_if_name)
179 host_if_name = ifr.ifr_ifrn.ifrn_name;
181 host_if_name = (char *) args->host_if_name;
183 unsigned int offload = 0;
184 hdrsz = sizeof (struct virtio_net_hdr_v1);
185 if (args->tap_flags & TAP_FLAG_GSO)
187 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
188 vif->gso_enabled = 1;
192 vif->gso_enabled = 0;
195 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
196 _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz);
197 _IOCTL (vif->fd, VHOST_SET_OWNER, 0);
199 /* if namespace is specified, all further netlink messages should be excuted
200 after we change our net namespace */
201 if (args->host_namespace)
203 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
204 if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1)
206 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
207 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
208 args->host_namespace);
211 args->error = vnet_netlink_set_link_netns (vif->ifindex, fd,
215 args->rv = VNET_API_ERROR_NETLINK_ERROR;
218 if (setns (fd, CLONE_NEWNET) == -1)
220 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
221 args->error = clib_error_return_unix (0, "setns '%s'",
222 args->host_namespace);
225 if ((vif->ifindex = if_nametoindex (host_if_name)) == 0)
227 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
228 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
237 args->error = vnet_netlink_set_link_name (vif->ifindex,
241 args->rv = VNET_API_ERROR_NETLINK_ERROR;
247 if (!ethernet_mac_address_is_zero (args->host_mac_addr))
249 args->error = vnet_netlink_set_link_addr (vif->ifindex,
250 args->host_mac_addr);
253 args->rv = VNET_API_ERROR_NETLINK_ERROR;
258 if (args->host_bridge)
260 args->error = vnet_netlink_set_link_master (vif->ifindex,
261 (char *) args->host_bridge);
264 args->rv = VNET_API_ERROR_NETLINK_ERROR;
270 if (args->host_ip4_prefix_len)
272 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
273 &args->host_ip4_addr,
274 args->host_ip4_prefix_len);
277 args->rv = VNET_API_ERROR_NETLINK_ERROR;
282 if (args->host_ip6_prefix_len)
284 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
285 &args->host_ip6_addr,
286 args->host_ip6_prefix_len);
289 args->rv = VNET_API_ERROR_NETLINK_ERROR;
294 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
297 args->rv = VNET_API_ERROR_NETLINK_ERROR;
301 if (args->host_ip4_gw_set)
303 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
306 args->rv = VNET_API_ERROR_NETLINK_ERROR;
311 if (args->host_ip6_gw_set)
313 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
316 args->rv = VNET_API_ERROR_NETLINK_ERROR;
321 /* switch back to old net namespace */
322 if (args->host_namespace)
324 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
326 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
327 args->error = clib_error_return_unix (0, "setns '%s'",
328 args->host_namespace);
333 if (args->host_mtu_set)
336 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
339 args->rv = VNET_API_ERROR_NETLINK_ERROR;
343 else if (tm->host_mtu_size != 0)
346 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
349 args->rv = VNET_API_ERROR_NETLINK_ERROR;
352 args->host_mtu_set = 1;
353 args->host_mtu_size = tm->host_mtu_size;
356 /* Set vhost memory table */
357 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
358 vhost_mem = clib_mem_alloc (i);
359 clib_memset (vhost_mem, 0, i);
360 vhost_mem->nregions = 1;
361 vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096;
362 _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem);
365 virtio_vring_init (vm, vif, RX_QUEUE (0), args->rx_ring_sz)))
367 args->rv = VNET_API_ERROR_INIT_FAILED;
373 virtio_vring_init (vm, vif, TX_QUEUE (0), args->tx_ring_sz)))
375 args->rv = VNET_API_ERROR_INIT_FAILED;
380 if (!args->mac_addr_set)
381 ethernet_mac_address_generate (args->mac_addr);
383 clib_memcpy (vif->mac_addr, args->mac_addr, 6);
385 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
386 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
387 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
388 vif->host_mtu_size = args->host_mtu_size;
389 clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6);
390 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
391 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
392 if (args->host_ip4_prefix_len)
393 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
394 if (args->host_ip6_prefix_len)
395 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
397 vif->type = VIRTIO_IF_TYPE_TAP;
398 args->error = ethernet_register_interface (vnm, virtio_device_class.index,
402 virtio_eth_flag_change);
405 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
409 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
410 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
411 vif->sw_if_index = sw->sw_if_index;
412 args->sw_if_index = vif->sw_if_index;
414 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
415 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
416 if (args->tap_flags & TAP_FLAG_GSO)
418 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
419 vnm->interface_main.gso_interface_count++;
421 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
422 virtio_input_node.index);
423 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
424 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
425 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
426 vif->per_interface_next_index = ~0;
427 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (0));
428 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
429 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
430 VNET_HW_INTERFACE_FLAG_LINK_UP);
431 vif->cxq_vring = NULL;
438 ASSERT (args->error == 0);
440 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
442 if (vif->tap_fd != -1)
446 vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif,
448 vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif,
450 vec_free (vif->rxq_vrings);
451 vec_free (vif->txq_vrings);
453 vec_free (vif->host_if_name);
454 vec_free (vif->net_ns);
455 vec_free (vif->host_bridge);
457 clib_memset (vif, 0, sizeof (virtio_if_t));
458 pool_put (vim->interfaces, vif);
462 clib_mem_free (vhost_mem);
463 if (old_netns_fd != -1)
464 close (old_netns_fd);
470 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
472 vnet_main_t *vnm = vnet_get_main ();
473 virtio_main_t *mm = &virtio_main;
474 tap_main_t *tm = &tap_main;
477 vnet_hw_interface_t *hw;
479 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
480 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
481 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
483 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
485 if (vif->type != VIRTIO_IF_TYPE_TAP)
486 return VNET_API_ERROR_INVALID_INTERFACE;
488 /* decrement if this was a GSO interface */
489 if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
490 vnm->interface_main.gso_interface_count--;
492 /* bring down the interface */
493 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
494 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
495 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, RX_QUEUE (0));
497 ethernet_delete_interface (vnm, vif->hw_if_index);
498 vif->hw_if_index = ~0;
500 if (vif->tap_fd != -1)
505 vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif,
507 vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif,
509 vec_free (vif->rxq_vrings);
510 vec_free (vif->txq_vrings);
512 vec_free (vif->host_if_name);
513 vec_free (vif->net_ns);
514 vec_free (vif->host_bridge);
516 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
517 clib_memset (vif, 0, sizeof (*vif));
518 pool_put (mm->interfaces, vif);
524 tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable)
526 vnet_main_t *vnm = vnet_get_main ();
527 virtio_main_t *mm = &virtio_main;
529 vnet_hw_interface_t *hw;
530 clib_error_t *err = 0;
532 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
534 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
535 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
537 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
539 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
540 const unsigned int gso_off = 0;
541 unsigned int offload = enable_disable ? gso_on : gso_off;
542 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
543 vif->gso_enabled = enable_disable ? 1 : 0;
546 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0)
548 vnm->interface_main.gso_interface_count++;
549 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
554 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
556 vnm->interface_main.gso_interface_count--;
557 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
564 clib_warning ("Error %s gso on sw_if_index %d",
565 enable_disable ? "enabling" : "disabling", sw_if_index);
566 return VNET_API_ERROR_SYSCALL_ERROR_3;
572 tap_dump_ifs (tap_interface_details_t ** out_tapids)
574 vnet_main_t *vnm = vnet_get_main ();
575 virtio_main_t *mm = &virtio_main;
577 virtio_vring_t *vring;
578 vnet_hw_interface_t *hi;
579 tap_interface_details_t *r_tapids = NULL;
580 tap_interface_details_t *tapid = NULL;
583 pool_foreach (vif, mm->interfaces,
584 if (vif->type != VIRTIO_IF_TYPE_TAP)
586 vec_add2(r_tapids, tapid, 1);
587 clib_memset (tapid, 0, sizeof (*tapid));
589 tapid->sw_if_index = vif->sw_if_index;
590 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
591 clib_memcpy(tapid->dev_name, hi->name,
592 MIN (ARRAY_LEN (tapid->dev_name) - 1,
593 strlen ((const char *) hi->name)));
594 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
595 tapid->rx_ring_sz = vring->size;
596 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
597 tapid->tx_ring_sz = vring->size;
598 clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6);
599 if (vif->host_if_name)
601 clib_memcpy(tapid->host_if_name, vif->host_if_name,
602 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
603 strlen ((const char *) vif->host_if_name)));
607 clib_memcpy(tapid->host_namespace, vif->net_ns,
608 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
609 strlen ((const char *) vif->net_ns)));
611 if (vif->host_bridge)
613 clib_memcpy(tapid->host_bridge, vif->host_bridge,
614 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
615 strlen ((const char *) vif->host_bridge)));
617 if (vif->host_ip4_prefix_len)
618 clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4);
619 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
620 if (vif->host_ip6_prefix_len)
621 clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16);
622 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
623 tapid->host_mtu_size = vif->host_mtu_size;
627 *out_tapids = r_tapids;
632 static clib_error_t *
633 tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
635 tap_main_t *tm = &tap_main;
637 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
639 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
642 return clib_error_return (0, "unknown input `%U'",
643 format_unformat_error, input);
649 /* tap { host-mtu <size> } configuration. */
650 VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
652 static clib_error_t *
653 tap_init (vlib_main_t * vm)
655 tap_main_t *tm = &tap_main;
656 clib_error_t *error = 0;
658 tm->log_default = vlib_log_register_class ("tap", 0);
659 vlib_log_debug (tm->log_default, "initialized");
661 tm->host_mtu_size = 0;
666 VLIB_INIT_FUNCTION (tap_init);
669 * fd.io coding-style-patch-verification: ON
672 * eval: (c-set-style "gnu")