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;
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 unsigned int offload = 0;
178 hdrsz = sizeof (struct virtio_net_hdr_v1);
179 if (args->tap_flags & TAP_FLAG_GSO)
181 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
182 vif->gso_enabled = 1;
186 vif->gso_enabled = 0;
189 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
190 _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz);
191 _IOCTL (vif->fd, VHOST_SET_OWNER, 0);
193 /* if namespace is specified, all further netlink messages should be excuted
194 after we change our net namespace */
195 if (args->host_namespace)
199 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
200 if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1)
202 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
203 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
204 args->host_namespace);
207 args->error = vnet_netlink_set_link_netns (vif->ifindex, fd,
208 (char *) args->host_if_name);
211 args->rv = VNET_API_ERROR_NETLINK_ERROR;
214 rc = setns (fd, CLONE_NEWNET);
218 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
219 args->error = clib_error_return_unix (0, "setns '%s'",
220 args->host_namespace);
223 if ((vif->ifindex = if_nametoindex ((char *) args->host_if_name)) == 0)
225 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
226 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
233 if (args->host_if_name)
235 args->error = vnet_netlink_set_link_name (vif->ifindex,
240 args->rv = VNET_API_ERROR_NETLINK_ERROR;
246 if (!ethernet_mac_address_is_zero (args->host_mac_addr))
248 args->error = vnet_netlink_set_link_addr (vif->ifindex,
249 args->host_mac_addr);
252 args->rv = VNET_API_ERROR_NETLINK_ERROR;
257 if (args->host_bridge)
259 args->error = vnet_netlink_set_link_master (vif->ifindex,
260 (char *) args->host_bridge);
263 args->rv = VNET_API_ERROR_NETLINK_ERROR;
269 if (args->host_ip4_prefix_len)
271 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
272 &args->host_ip4_addr,
273 args->host_ip4_prefix_len);
276 args->rv = VNET_API_ERROR_NETLINK_ERROR;
281 if (args->host_ip6_prefix_len)
283 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
284 &args->host_ip6_addr,
285 args->host_ip6_prefix_len);
288 args->rv = VNET_API_ERROR_NETLINK_ERROR;
293 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
296 args->rv = VNET_API_ERROR_NETLINK_ERROR;
300 if (args->host_ip4_gw_set)
302 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
305 args->rv = VNET_API_ERROR_NETLINK_ERROR;
310 if (args->host_ip6_gw_set)
312 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
315 args->rv = VNET_API_ERROR_NETLINK_ERROR;
320 /* switch back to old net namespace */
321 if (args->host_namespace)
323 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
325 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
326 args->error = clib_error_return_unix (0, "setns '%s'",
327 args->host_namespace);
332 /* Set vhost memory table */
333 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
334 vhost_mem = clib_mem_alloc (i);
335 clib_memset (vhost_mem, 0, i);
336 vhost_mem->nregions = 1;
337 vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096;
338 _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem);
340 if ((args->error = virtio_vring_init (vm, vif, 0, args->rx_ring_sz)))
342 args->rv = VNET_API_ERROR_INIT_FAILED;
346 if ((args->error = virtio_vring_init (vm, vif, 1, args->tx_ring_sz)))
348 args->rv = VNET_API_ERROR_INIT_FAILED;
352 if (!args->mac_addr_set)
354 f64 now = vlib_time_now (vm);
356 rnd = (u32) (now * 1e6);
357 rnd = random_u32 (&rnd);
359 memcpy (args->mac_addr + 2, &rnd, sizeof (rnd));
360 args->mac_addr[0] = 2;
361 args->mac_addr[1] = 0xfe;
363 vif->rx_ring_sz = args->rx_ring_sz != 0 ? args->rx_ring_sz : 256;
364 vif->tx_ring_sz = args->tx_ring_sz != 0 ? args->tx_ring_sz : 256;
365 clib_memcpy (vif->mac_addr, args->mac_addr, 6);
367 vif->host_if_name = args->host_if_name;
368 args->host_if_name = 0;
369 vif->net_ns = args->host_namespace;
370 args->host_namespace = 0;
371 vif->host_bridge = args->host_bridge;
372 args->host_bridge = 0;
373 clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6);
374 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
375 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
376 if (args->host_ip4_prefix_len)
377 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
378 if (args->host_ip6_prefix_len)
379 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
381 vif->type = VIRTIO_IF_TYPE_TAP;
382 args->error = ethernet_register_interface (vnm, virtio_device_class.index,
386 virtio_eth_flag_change);
389 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
393 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
394 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
395 vif->sw_if_index = sw->sw_if_index;
396 args->sw_if_index = vif->sw_if_index;
397 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
398 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
399 if (args->tap_flags & TAP_FLAG_GSO)
401 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
402 vnm->interface_main.gso_interface_count++;
404 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
405 virtio_input_node.index);
406 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
407 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
408 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
409 vif->per_interface_next_index = ~0;
410 virtio_vring_set_numa_node (vm, vif, 0);
411 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
412 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
413 VNET_HW_INTERFACE_FLAG_LINK_UP);
414 if (thm->n_vlib_mains > 1)
415 clib_spinlock_init (&vif->lockp);
421 ASSERT (args->error == 0);
423 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
425 if (vif->tap_fd != -1)
429 vec_foreach_index (i, vif->vrings) virtio_vring_free (vm, vif, i);
430 vec_free (vif->vrings);
431 clib_memset (vif, 0, sizeof (virtio_if_t));
432 pool_put (vim->interfaces, vif);
436 clib_mem_free (vhost_mem);
437 if (old_netns_fd != -1)
438 close (old_netns_fd);
442 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
444 vnet_main_t *vnm = vnet_get_main ();
445 virtio_main_t *mm = &virtio_main;
446 tap_main_t *tm = &tap_main;
449 vnet_hw_interface_t *hw;
451 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
452 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
453 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
455 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
457 if (vif->type != VIRTIO_IF_TYPE_TAP)
458 return VNET_API_ERROR_INVALID_INTERFACE;
460 /* decrement if this was a GSO interface */
461 if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
462 vnm->interface_main.gso_interface_count--;
464 /* bring down the interface */
465 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
466 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
467 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, 0);
469 ethernet_delete_interface (vnm, vif->hw_if_index);
470 vif->hw_if_index = ~0;
472 if (vif->tap_fd != -1)
477 vec_foreach_index (i, vif->vrings) virtio_vring_free (vm, vif, i);
478 vec_free (vif->vrings);
480 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
481 clib_spinlock_free (&vif->lockp);
482 clib_memset (vif, 0, sizeof (*vif));
483 pool_put (mm->interfaces, vif);
489 tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable)
491 vnet_main_t *vnm = vnet_get_main ();
492 virtio_main_t *mm = &virtio_main;
494 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
495 clib_error_t *err = 0;
497 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
498 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
500 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
502 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
503 const unsigned int gso_off = 0;
504 unsigned int offload = enable_disable ? gso_on : gso_off;
505 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
506 vif->gso_enabled = enable_disable ? 1 : 0;
509 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0)
511 vnm->interface_main.gso_interface_count++;
512 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
517 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
519 vnm->interface_main.gso_interface_count--;
520 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
527 clib_warning ("Error %s gso on sw_if_index %d",
528 enable_disable ? "enabling" : "disabling", sw_if_index);
529 return VNET_API_ERROR_SYSCALL_ERROR_3;
535 tap_dump_ifs (tap_interface_details_t ** out_tapids)
537 vnet_main_t *vnm = vnet_get_main ();
538 virtio_main_t *mm = &virtio_main;
540 vnet_hw_interface_t *hi;
541 tap_interface_details_t *r_tapids = NULL;
542 tap_interface_details_t *tapid = NULL;
545 pool_foreach (vif, mm->interfaces,
546 if (vif->type != VIRTIO_IF_TYPE_TAP)
548 vec_add2(r_tapids, tapid, 1);
549 clib_memset (tapid, 0, sizeof (*tapid));
551 tapid->sw_if_index = vif->sw_if_index;
552 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
553 clib_memcpy(tapid->dev_name, hi->name,
554 MIN (ARRAY_LEN (tapid->dev_name) - 1,
555 strlen ((const char *) hi->name)));
556 tapid->rx_ring_sz = vif->rx_ring_sz;
557 tapid->tx_ring_sz = vif->tx_ring_sz;
558 clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6);
559 if (vif->host_if_name)
561 clib_memcpy(tapid->host_if_name, vif->host_if_name,
562 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
563 strlen ((const char *) vif->host_if_name)));
567 clib_memcpy(tapid->host_namespace, vif->net_ns,
568 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
569 strlen ((const char *) vif->net_ns)));
571 if (vif->host_bridge)
573 clib_memcpy(tapid->host_bridge, vif->host_bridge,
574 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
575 strlen ((const char *) vif->host_bridge)));
577 if (vif->host_ip4_prefix_len)
578 clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4);
579 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
580 if (vif->host_ip6_prefix_len)
581 clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16);
582 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
586 *out_tapids = r_tapids;
591 static clib_error_t *
592 tap_init (vlib_main_t * vm)
594 tap_main_t *tm = &tap_main;
595 clib_error_t *error = 0;
597 tm->log_default = vlib_log_register_class ("tap", 0);
598 vlib_log_debug (tm->log_default, "initialized");
603 VLIB_INIT_FUNCTION (tap_init);
606 * fd.io coding-style-patch-verification: ON
609 * eval: (c-set-style "gnu")