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 vlib_thread_main_t *thm = vlib_get_thread_main ();
85 virtio_main_t *vim = &virtio_main;
86 tap_main_t *tm = &tap_main;
87 vnet_sw_interface_t *sw;
88 vnet_hw_interface_t *hw;
90 int old_netns_fd = -1;
93 struct vhost_memory *vhost_mem = 0;
95 clib_error_t *err = 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 unsigned int offload = 0;
179 hdrsz = sizeof (struct virtio_net_hdr_v1);
180 if (args->tap_flags & TAP_FLAG_GSO)
182 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
183 vif->gso_enabled = 1;
187 vif->gso_enabled = 0;
190 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
191 _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz);
192 _IOCTL (vif->fd, VHOST_SET_OWNER, 0);
194 /* if namespace is specified, all further netlink messages should be excuted
195 after we change our net namespace */
196 if (args->host_namespace)
198 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
199 if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1)
201 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
202 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
203 args->host_namespace);
206 args->error = vnet_netlink_set_link_netns (vif->ifindex, fd,
207 (char *) args->host_if_name);
210 args->rv = VNET_API_ERROR_NETLINK_ERROR;
213 if (setns (fd, CLONE_NEWNET) == -1)
215 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
216 args->error = clib_error_return_unix (0, "setns '%s'",
217 args->host_namespace);
220 if ((vif->ifindex = if_nametoindex ((char *) args->host_if_name)) == 0)
222 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
223 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
230 if (args->host_if_name)
232 args->error = vnet_netlink_set_link_name (vif->ifindex,
237 args->rv = VNET_API_ERROR_NETLINK_ERROR;
243 if (!ethernet_mac_address_is_zero (args->host_mac_addr))
245 args->error = vnet_netlink_set_link_addr (vif->ifindex,
246 args->host_mac_addr);
249 args->rv = VNET_API_ERROR_NETLINK_ERROR;
254 if (args->host_bridge)
256 args->error = vnet_netlink_set_link_master (vif->ifindex,
257 (char *) args->host_bridge);
260 args->rv = VNET_API_ERROR_NETLINK_ERROR;
266 if (args->host_ip4_prefix_len)
268 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
269 &args->host_ip4_addr,
270 args->host_ip4_prefix_len);
273 args->rv = VNET_API_ERROR_NETLINK_ERROR;
278 if (args->host_ip6_prefix_len)
280 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
281 &args->host_ip6_addr,
282 args->host_ip6_prefix_len);
285 args->rv = VNET_API_ERROR_NETLINK_ERROR;
290 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
293 args->rv = VNET_API_ERROR_NETLINK_ERROR;
297 if (args->host_ip4_gw_set)
299 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
302 args->rv = VNET_API_ERROR_NETLINK_ERROR;
307 if (args->host_ip6_gw_set)
309 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
312 args->rv = VNET_API_ERROR_NETLINK_ERROR;
317 /* switch back to old net namespace */
318 if (args->host_namespace)
320 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
322 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
323 args->error = clib_error_return_unix (0, "setns '%s'",
324 args->host_namespace);
329 /* Set vhost memory table */
330 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
331 vhost_mem = clib_mem_alloc (i);
332 clib_memset (vhost_mem, 0, i);
333 vhost_mem->nregions = 1;
334 vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096;
335 _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem);
337 if ((args->error = virtio_vring_init (vm, vif, 0, args->rx_ring_sz)))
339 args->rv = VNET_API_ERROR_INIT_FAILED;
343 if ((args->error = virtio_vring_init (vm, vif, 1, args->tx_ring_sz)))
345 args->rv = VNET_API_ERROR_INIT_FAILED;
349 if (!args->mac_addr_set)
350 ethernet_mac_address_generate (args->mac_addr);
352 vif->rx_ring_sz = args->rx_ring_sz != 0 ? args->rx_ring_sz : 256;
353 vif->tx_ring_sz = args->tx_ring_sz != 0 ? args->tx_ring_sz : 256;
354 clib_memcpy (vif->mac_addr, args->mac_addr, 6);
356 vif->host_if_name = args->host_if_name;
357 args->host_if_name = 0;
358 vif->net_ns = args->host_namespace;
359 args->host_namespace = 0;
360 vif->host_bridge = args->host_bridge;
361 args->host_bridge = 0;
362 clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6);
363 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
364 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
365 if (args->host_ip4_prefix_len)
366 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
367 if (args->host_ip6_prefix_len)
368 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
370 vif->type = VIRTIO_IF_TYPE_TAP;
371 args->error = ethernet_register_interface (vnm, virtio_device_class.index,
375 virtio_eth_flag_change);
378 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
382 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
383 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
384 vif->sw_if_index = sw->sw_if_index;
385 args->sw_if_index = vif->sw_if_index;
386 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
387 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
388 if (args->tap_flags & TAP_FLAG_GSO)
390 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
391 vnm->interface_main.gso_interface_count++;
393 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
394 virtio_input_node.index);
395 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
396 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
397 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
398 vif->per_interface_next_index = ~0;
399 virtio_vring_set_numa_node (vm, vif, 0);
400 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
401 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
402 VNET_HW_INTERFACE_FLAG_LINK_UP);
403 if (thm->n_vlib_mains > 1)
404 clib_spinlock_init (&vif->lockp);
410 ASSERT (args->error == 0);
412 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
414 if (vif->tap_fd != -1)
418 vec_foreach_index (i, vif->vrings) virtio_vring_free (vm, vif, i);
419 vec_free (vif->vrings);
420 clib_memset (vif, 0, sizeof (virtio_if_t));
421 pool_put (vim->interfaces, vif);
425 clib_mem_free (vhost_mem);
426 if (old_netns_fd != -1)
427 close (old_netns_fd);
433 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
435 vnet_main_t *vnm = vnet_get_main ();
436 virtio_main_t *mm = &virtio_main;
437 tap_main_t *tm = &tap_main;
440 vnet_hw_interface_t *hw;
442 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
443 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
444 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
446 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
448 if (vif->type != VIRTIO_IF_TYPE_TAP)
449 return VNET_API_ERROR_INVALID_INTERFACE;
451 /* decrement if this was a GSO interface */
452 if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
453 vnm->interface_main.gso_interface_count--;
455 /* bring down the interface */
456 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
457 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
458 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, 0);
460 ethernet_delete_interface (vnm, vif->hw_if_index);
461 vif->hw_if_index = ~0;
463 if (vif->tap_fd != -1)
468 vec_foreach_index (i, vif->vrings) virtio_vring_free (vm, vif, i);
469 vec_free (vif->vrings);
471 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
472 clib_spinlock_free (&vif->lockp);
473 clib_memset (vif, 0, sizeof (*vif));
474 pool_put (mm->interfaces, vif);
480 tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable)
482 vnet_main_t *vnm = vnet_get_main ();
483 virtio_main_t *mm = &virtio_main;
485 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
486 clib_error_t *err = 0;
488 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
489 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
491 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
493 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
494 const unsigned int gso_off = 0;
495 unsigned int offload = enable_disable ? gso_on : gso_off;
496 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
497 vif->gso_enabled = enable_disable ? 1 : 0;
500 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0)
502 vnm->interface_main.gso_interface_count++;
503 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
508 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
510 vnm->interface_main.gso_interface_count--;
511 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
518 clib_warning ("Error %s gso on sw_if_index %d",
519 enable_disable ? "enabling" : "disabling", sw_if_index);
520 return VNET_API_ERROR_SYSCALL_ERROR_3;
526 tap_dump_ifs (tap_interface_details_t ** out_tapids)
528 vnet_main_t *vnm = vnet_get_main ();
529 virtio_main_t *mm = &virtio_main;
531 vnet_hw_interface_t *hi;
532 tap_interface_details_t *r_tapids = NULL;
533 tap_interface_details_t *tapid = NULL;
536 pool_foreach (vif, mm->interfaces,
537 if (vif->type != VIRTIO_IF_TYPE_TAP)
539 vec_add2(r_tapids, tapid, 1);
540 clib_memset (tapid, 0, sizeof (*tapid));
542 tapid->sw_if_index = vif->sw_if_index;
543 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
544 clib_memcpy(tapid->dev_name, hi->name,
545 MIN (ARRAY_LEN (tapid->dev_name) - 1,
546 strlen ((const char *) hi->name)));
547 tapid->rx_ring_sz = vif->rx_ring_sz;
548 tapid->tx_ring_sz = vif->tx_ring_sz;
549 clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6);
550 if (vif->host_if_name)
552 clib_memcpy(tapid->host_if_name, vif->host_if_name,
553 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
554 strlen ((const char *) vif->host_if_name)));
558 clib_memcpy(tapid->host_namespace, vif->net_ns,
559 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
560 strlen ((const char *) vif->net_ns)));
562 if (vif->host_bridge)
564 clib_memcpy(tapid->host_bridge, vif->host_bridge,
565 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
566 strlen ((const char *) vif->host_bridge)));
568 if (vif->host_ip4_prefix_len)
569 clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4);
570 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
571 if (vif->host_ip6_prefix_len)
572 clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16);
573 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
577 *out_tapids = r_tapids;
582 static clib_error_t *
583 tap_init (vlib_main_t * vm)
585 tap_main_t *tm = &tap_main;
586 clib_error_t *error = 0;
588 tm->log_default = vlib_log_register_class ("tap", 0);
589 vlib_log_debug (tm->log_default, "initialized");
594 VLIB_INIT_FUNCTION (tap_init);
597 * fd.io coding-style-patch-verification: ON
600 * eval: (c-set-style "gnu")