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>
28 #include <net/if_arp.h>
32 #include <linux/netlink.h>
33 #include <linux/rtnetlink.h>
35 #include <vlib/vlib.h>
36 #include <vlib/physmem.h>
37 #include <vlib/unix/unix.h>
38 #include <vnet/ethernet/ethernet.h>
39 #include <vnet/ip/ip4_packet.h>
40 #include <vnet/ip/ip6_packet.h>
41 #include <vnet/devices/netlink.h>
42 #include <vnet/devices/virtio/virtio.h>
43 #include <vnet/devices/tap/tap.h>
47 #define tap_log_err(dev, f, ...) \
48 vlib_log (VLIB_LOG_LEVEL_ERR, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
49 #define tap_log_dbg(dev, f, ...) \
50 vlib_log (VLIB_LOG_LEVEL_DEBUG, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
52 #define _IOCTL(fd,a,...) \
53 if (ioctl (fd, a, __VA_ARGS__) < 0) \
55 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
56 tap_log_err (vif, "%U", format_clib_error, err); \
61 virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
65 //TODO On MTU change call vnet_netlink_set_if_mtu
70 open_netns_fd (char *netns)
75 if (strncmp (netns, "pid:", 4) == 0)
76 s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0);
77 else if (netns[0] == '/')
78 s = format (0, "%s%c", netns, 0);
80 s = format (0, "/var/run/netns/%s%c", netns, 0);
82 fd = open ((char *) s, O_RDONLY);
87 #define TAP_MAX_INSTANCE 1024
90 tap_free (vlib_main_t * vm, virtio_if_t * vif)
92 virtio_main_t *mm = &virtio_main;
93 tap_main_t *tm = &tap_main;
94 clib_error_t *err = 0;
98 vec_foreach_index (i, vif->vhost_fds) if (vif->vhost_fds[i] != -1)
99 close (vif->vhost_fds[i]);
100 vec_foreach_index (i, vif->rxq_vrings)
101 virtio_vring_free_rx (vm, vif, RX_QUEUE (i));
102 vec_foreach_index (i, vif->txq_vrings)
103 virtio_vring_free_tx (vm, vif, TX_QUEUE (i));
106 _IOCTL (vif->tap_fd, TUNSETPERSIST, (void *) (uintptr_t) 0);
107 tap_log_dbg (vif, "TUNSETPERSIST: unset");
109 if (vif->tap_fd != -1)
112 vec_free (vif->vhost_fds);
113 vec_free (vif->rxq_vrings);
114 vec_free (vif->txq_vrings);
115 vec_free (vif->host_if_name);
116 vec_free (vif->net_ns);
117 vec_free (vif->host_bridge);
118 clib_error_free (vif->error);
120 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
121 clib_memset (vif, 0, sizeof (*vif));
122 pool_put (mm->interfaces, vif);
126 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
128 vlib_thread_main_t *thm = vlib_get_thread_main ();
129 vlib_physmem_main_t *vpm = &vm->physmem_main;
130 vnet_main_t *vnm = vnet_get_main ();
131 virtio_main_t *vim = &virtio_main;
132 tap_main_t *tm = &tap_main;
133 vnet_sw_interface_t *sw;
134 vnet_hw_interface_t *hw;
136 int old_netns_fd = -1;
137 struct ifreq ifr = {.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR };
138 struct ifreq get_ifr = {.ifr_flags = 0 };
140 struct vhost_memory *vhost_mem = 0;
141 virtio_if_t *vif = 0;
142 clib_error_t *err = 0;
143 unsigned int tap_features;
144 int tfd, vfd, nfd = -1;
145 char *host_if_name = 0;
146 unsigned int offload = 0;
151 if (clib_bitmap_get (tm->tap_ids, args->id))
153 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
154 args->error = clib_error_return (0, "interface already exists");
160 args->id = clib_bitmap_first_clear (tm->tap_ids);
163 if (args->id > TAP_MAX_INSTANCE)
165 args->rv = VNET_API_ERROR_UNSPECIFIED;
166 args->error = clib_error_return (0, "cannot find free interface id");
170 pool_get_zero (vim->interfaces, vif);
171 vif->type = VIRTIO_IF_TYPE_TAP;
172 vif->dev_instance = vif - vim->interfaces;
174 vif->num_txqs = thm->n_vlib_mains;
175 vif->num_rxqs = args->num_rx_queues;
176 num_q_pairs = clib_max (vif->num_rxqs, vif->num_txqs);
178 if (args->tap_flags & TAP_FLAG_ATTACH)
180 if (args->host_if_name != NULL)
182 host_if_name = (char *) args->host_if_name;
183 clib_memcpy (ifr.ifr_name, host_if_name,
184 clib_min (IFNAMSIZ, strlen (host_if_name)));
188 args->rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
189 err = clib_error_return (0, "host_if_name is not provided");
192 if (args->host_namespace)
194 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
195 if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1)
197 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
198 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
199 args->host_namespace);
202 if (setns (nfd, CLONE_NEWNET) == -1)
204 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
205 args->error = clib_error_return_unix (0, "setns '%s'",
206 args->host_namespace);
211 if ((vif->tap_fd = tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
213 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
214 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
217 tap_log_dbg (vif, "open tap fd %d", tfd);
219 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
220 tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
221 if ((tap_features & IFF_VNET_HDR) == 0)
223 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
224 args->error = clib_error_return (0, "vhost-net backend not available");
228 if ((tap_features & IFF_MULTI_QUEUE) == 0)
230 if (args->num_rx_queues > 1)
232 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
233 args->error = clib_error_return (0, "multiqueue not supported");
236 vif->num_rxqs = vif->num_txqs = num_q_pairs = 1;
239 ifr.ifr_flags |= IFF_MULTI_QUEUE;
241 hdrsz = sizeof (struct virtio_net_hdr_v1);
242 if (args->tap_flags & TAP_FLAG_GSO)
244 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
245 vif->gso_enabled = 1;
247 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
249 offload = TUN_F_CSUM;
250 vif->csum_offload_enabled = 1;
253 _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
254 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
255 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
257 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
258 tap_log_dbg (vif, "ifindex %d", vif->ifindex);
260 if (!args->host_if_name)
261 host_if_name = ifr.ifr_ifrn.ifrn_name;
263 host_if_name = (char *) args->host_if_name;
265 if (fcntl (tfd, F_SETFL, O_NONBLOCK) < 0)
267 err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
268 tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
273 * unset the persistence when attaching to existing
276 if (args->tap_flags & TAP_FLAG_ATTACH)
278 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 0);
279 tap_log_dbg (vif, "TUNSETPERSIST: unset");
282 /* set the persistence */
283 if (args->tap_flags & TAP_FLAG_PERSIST)
285 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 1);
286 tap_log_dbg (vif, "TUNSETPERSIST: set");
288 /* verify persistence is set, read the flags */
289 _IOCTL (tfd, TUNGETIFF, (void *) &get_ifr);
290 tap_log_dbg (vif, "TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
291 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
293 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
294 args->error = clib_error_return (0, "persistence not supported");
299 tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u", tfd, hdrsz);
300 _IOCTL (tfd, TUNSETVNETHDRSZ, &hdrsz);
303 tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", tfd, i);
304 _IOCTL (tfd, TUNSETSNDBUF, &i);
306 tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", tfd, offload);
307 _IOCTL (tfd, TUNSETOFFLOAD, offload);
309 /* open vhost-net fd for each queue pair and set ownership */
310 for (i = 0; i < num_q_pairs; i++)
312 if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
314 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
315 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
318 vec_add1 (vif->vhost_fds, vfd);
319 virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
320 _IOCTL (vfd, VHOST_SET_OWNER, 0);
321 virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
324 _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
325 virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
326 vif->remote_features);
328 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
330 args->rv = VNET_API_ERROR_UNSUPPORTED;
331 args->error = clib_error_return (0, "vhost-net backend doesn't support "
332 "VIRTIO_NET_F_MRG_RXBUF feature");
336 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
339 args->rv = VNET_API_ERROR_UNSUPPORTED;
340 args->error = clib_error_return (0, "vhost-net backend doesn't support "
341 "VIRTIO_RING_F_INDIRECT_DESC feature");
345 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
347 args->rv = VNET_API_ERROR_UNSUPPORTED;
348 args->error = clib_error_return (0, "vhost-net backend doesn't support "
349 "VIRTIO_F_VERSION_1 features");
353 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
354 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
355 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
357 virtio_set_net_hdr_size (vif);
359 if (!(args->tap_flags & TAP_FLAG_ATTACH))
361 /* if namespace is specified, all further netlink messages should be executed
362 after we change our net namespace */
363 if (args->host_namespace)
365 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
366 if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1)
368 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
369 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
370 args->host_namespace);
373 args->error = vnet_netlink_set_link_netns (vif->ifindex, nfd,
377 args->rv = VNET_API_ERROR_NETLINK_ERROR;
380 if (setns (nfd, CLONE_NEWNET) == -1)
382 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
383 args->error = clib_error_return_unix (0, "setns '%s'",
384 args->host_namespace);
387 if ((vif->ifindex = if_nametoindex (host_if_name)) == 0)
389 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
390 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
395 else if (host_if_name)
398 vnet_netlink_set_link_name (vif->ifindex, host_if_name);
401 args->rv = VNET_API_ERROR_NETLINK_ERROR;
407 if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes))
408 ethernet_mac_address_generate (args->host_mac_addr.bytes);
409 args->error = vnet_netlink_set_link_addr (vif->ifindex,
410 args->host_mac_addr.bytes);
413 args->rv = VNET_API_ERROR_NETLINK_ERROR;
417 if (args->host_bridge)
419 args->error = vnet_netlink_set_link_master (vif->ifindex,
420 (char *) args->host_bridge);
423 args->rv = VNET_API_ERROR_NETLINK_ERROR;
428 if (args->host_ip4_prefix_len)
430 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
431 &args->host_ip4_addr,
432 args->host_ip4_prefix_len);
435 args->rv = VNET_API_ERROR_NETLINK_ERROR;
440 if (args->host_ip6_prefix_len)
442 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
443 &args->host_ip6_addr,
444 args->host_ip6_prefix_len);
447 args->rv = VNET_API_ERROR_NETLINK_ERROR;
452 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
455 args->rv = VNET_API_ERROR_NETLINK_ERROR;
459 if (args->host_ip4_gw_set)
461 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
464 args->rv = VNET_API_ERROR_NETLINK_ERROR;
469 if (args->host_ip6_gw_set)
471 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
474 args->rv = VNET_API_ERROR_NETLINK_ERROR;
479 if (args->host_mtu_set)
482 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
485 args->rv = VNET_API_ERROR_NETLINK_ERROR;
489 else if (tm->host_mtu_size != 0)
492 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
495 args->rv = VNET_API_ERROR_NETLINK_ERROR;
498 args->host_mtu_set = 1;
499 args->host_mtu_size = tm->host_mtu_size;
502 /* switch back to old net namespace */
503 if (args->host_namespace)
505 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
507 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
508 args->error = clib_error_return_unix (0, "setns '%s'",
509 args->host_namespace);
514 for (i = 0; i < num_q_pairs; i++)
516 if (i < vif->num_rxqs && (args->error =
517 virtio_vring_init (vm, vif, RX_QUEUE (i),
520 args->rv = VNET_API_ERROR_INIT_FAILED;
524 if (i < vif->num_txqs && (args->error =
525 virtio_vring_init (vm, vif, TX_QUEUE (i),
528 args->rv = VNET_API_ERROR_INIT_FAILED;
533 /* setup features and memtable */
534 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
535 vhost_mem = clib_mem_alloc (i);
536 clib_memset (vhost_mem, 0, i);
537 vhost_mem->nregions = 1;
538 vhost_mem->regions[0].memory_size = vpm->max_size;
539 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
540 vhost_mem->regions[0].userspace_addr =
541 vhost_mem->regions[0].guest_phys_addr;
543 for (i = 0; i < vhost_mem->nregions; i++)
544 virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
545 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
546 vhost_mem->regions[0].memory_size,
547 vhost_mem->regions[0].guest_phys_addr,
548 vhost_mem->regions[0].userspace_addr);
551 for (i = 0; i < num_q_pairs; i++)
553 int fd = vif->vhost_fds[i];
554 _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
555 virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
557 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
558 virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
561 /* finish initializing queue pair */
562 for (i = 0; i < num_q_pairs * 2; i++)
564 struct vhost_vring_addr addr = { 0 };
565 struct vhost_vring_state state = { 0 };
566 struct vhost_vring_file file = { 0 };
567 virtio_vring_t *vring;
569 int fd = vif->vhost_fds[qp];
573 if (qp >= vif->num_txqs)
575 vring = vec_elt_at_index (vif->txq_vrings, qp);
579 if (qp >= vif->num_rxqs)
581 vring = vec_elt_at_index (vif->rxq_vrings, qp);
584 addr.index = state.index = file.index = vring->queue_id & 1;
585 state.num = vring->size;
586 virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
587 state.index, state.num);
588 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
591 addr.desc_user_addr = pointer_to_uword (vring->desc);
592 addr.avail_user_addr = pointer_to_uword (vring->avail);
593 addr.used_user_addr = pointer_to_uword (vring->used);
595 virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
596 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
597 "used_user_addr 0x%lx", fd, addr.index,
598 addr.flags, addr.desc_user_addr, addr.avail_user_addr,
599 addr.used_user_addr);
600 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
602 file.fd = vring->call_fd;
603 virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
604 fd, file.index, file.fd);
605 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
607 file.fd = vring->kick_fd;
608 virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
609 fd, file.index, file.fd);
610 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
613 virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
614 fd, file.index, file.fd);
615 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
618 if (!args->mac_addr_set)
619 ethernet_mac_address_generate (args->mac_addr.bytes);
621 clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6);
623 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
624 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
625 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
626 vif->host_mtu_size = args->host_mtu_size;
627 clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6);
628 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
629 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
630 if (args->host_ip4_prefix_len)
631 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
632 if (args->host_ip6_prefix_len)
633 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
635 args->error = ethernet_register_interface (vnm, virtio_device_class.index,
639 virtio_eth_flag_change);
642 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
646 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
647 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
648 vif->sw_if_index = sw->sw_if_index;
649 args->sw_if_index = vif->sw_if_index;
651 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
652 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
653 if (args->tap_flags & TAP_FLAG_GSO)
655 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
656 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
658 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
660 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
662 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
663 virtio_input_node.index);
665 for (i = 0; i < vif->num_rxqs; i++)
667 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
668 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
669 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
670 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
673 vif->per_interface_next_index = ~0;
674 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
675 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
676 VNET_HW_INTERFACE_FLAG_LINK_UP);
677 vif->cxq_vring = NULL;
684 ASSERT (args->error == 0);
686 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
689 tap_log_err (vif, "%U", format_clib_error, args->error);
693 clib_mem_free (vhost_mem);
694 if (old_netns_fd != -1)
695 close (old_netns_fd);
701 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
703 vnet_main_t *vnm = vnet_get_main ();
704 virtio_main_t *mm = &virtio_main;
707 vnet_hw_interface_t *hw;
709 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
710 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
711 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
713 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
715 if (vif->type != VIRTIO_IF_TYPE_TAP)
716 return VNET_API_ERROR_INVALID_INTERFACE;
718 /* bring down the interface */
719 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
720 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
721 for (i = 0; i < vif->num_rxqs; i++)
722 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
724 ethernet_delete_interface (vnm, vif->hw_if_index);
725 vif->hw_if_index = ~0;
733 tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index,
736 vnet_main_t *vnm = vnet_get_main ();
737 virtio_main_t *mm = &virtio_main;
739 vnet_hw_interface_t *hw;
740 clib_error_t *err = 0;
742 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
744 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
745 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
747 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
749 const unsigned int csum_offload_on = TUN_F_CSUM;
750 const unsigned int csum_offload_off = 0;
751 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
752 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
753 vif->gso_enabled = 0;
754 vif->csum_offload_enabled = enable_disable ? 1 : 0;
756 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
758 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
763 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD) ==
766 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
771 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD) !=
774 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
781 clib_warning ("Error %s checksum offload on sw_if_index %d",
782 enable_disable ? "enabling" : "disabling", sw_if_index);
783 return VNET_API_ERROR_SYSCALL_ERROR_3;
789 tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable)
791 vnet_main_t *vnm = vnet_get_main ();
792 virtio_main_t *mm = &virtio_main;
794 vnet_hw_interface_t *hw;
795 clib_error_t *err = 0;
797 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
799 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
800 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
802 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
804 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
805 const unsigned int gso_off = 0;
806 unsigned int offload = enable_disable ? gso_on : gso_off;
807 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
808 vif->gso_enabled = enable_disable ? 1 : 0;
809 vif->csum_offload_enabled = 0;
812 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0)
814 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
815 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
820 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
822 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
823 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
830 clib_warning ("Error %s gso on sw_if_index %d",
831 enable_disable ? "enabling" : "disabling", sw_if_index);
832 return VNET_API_ERROR_SYSCALL_ERROR_3;
838 tap_dump_ifs (tap_interface_details_t ** out_tapids)
840 vnet_main_t *vnm = vnet_get_main ();
841 virtio_main_t *mm = &virtio_main;
843 virtio_vring_t *vring;
844 vnet_hw_interface_t *hi;
845 tap_interface_details_t *r_tapids = NULL;
846 tap_interface_details_t *tapid = NULL;
849 pool_foreach (vif, mm->interfaces,
850 if (vif->type != VIRTIO_IF_TYPE_TAP)
852 vec_add2(r_tapids, tapid, 1);
853 clib_memset (tapid, 0, sizeof (*tapid));
855 tapid->sw_if_index = vif->sw_if_index;
856 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
857 clib_memcpy(tapid->dev_name, hi->name,
858 MIN (ARRAY_LEN (tapid->dev_name) - 1,
859 strlen ((const char *) hi->name)));
860 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
861 tapid->rx_ring_sz = vring->size;
862 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
863 tapid->tx_ring_sz = vring->size;
864 clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6);
865 if (vif->host_if_name)
867 clib_memcpy(tapid->host_if_name, vif->host_if_name,
868 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
869 strlen ((const char *) vif->host_if_name)));
873 clib_memcpy(tapid->host_namespace, vif->net_ns,
874 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
875 strlen ((const char *) vif->net_ns)));
877 if (vif->host_bridge)
879 clib_memcpy(tapid->host_bridge, vif->host_bridge,
880 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
881 strlen ((const char *) vif->host_bridge)));
883 if (vif->host_ip4_prefix_len)
884 clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4);
885 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
886 if (vif->host_ip6_prefix_len)
887 clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16);
888 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
889 tapid->host_mtu_size = vif->host_mtu_size;
893 *out_tapids = r_tapids;
898 static clib_error_t *
899 tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
901 tap_main_t *tm = &tap_main;
903 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
905 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
908 return clib_error_return (0, "unknown input `%U'",
909 format_unformat_error, input);
915 /* tap { host-mtu <size> } configuration. */
916 VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
918 static clib_error_t *
919 tap_init (vlib_main_t * vm)
921 tap_main_t *tm = &tap_main;
922 clib_error_t *error = 0;
924 tm->log_default = vlib_log_register_class ("tap", 0);
925 vlib_log_debug (tm->log_default, "initialized");
927 tm->host_mtu_size = 0;
932 VLIB_INIT_FUNCTION (tap_init);
935 * fd.io coding-style-patch-verification: ON
938 * eval: (c-set-style "gnu")