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>
21 #include <sys/socket.h>
24 #include <linux/if_tun.h>
25 #include <sys/ioctl.h>
26 #include <linux/ethtool.h>
27 #include <linux/sockios.h>
28 #include <sys/eventfd.h>
29 #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 <vppinfra/linux/netns.h>
39 #include <vnet/ethernet/ethernet.h>
40 #include <vnet/ip/ip4_packet.h>
41 #include <vnet/ip/ip6_packet.h>
42 #include <vnet/devices/netlink.h>
43 #include <vnet/devices/virtio/virtio.h>
44 #include <vnet/devices/tap/tap.h>
48 #define tap_log_err(dev, f, ...) \
49 vlib_log (VLIB_LOG_LEVEL_ERR, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
50 #define tap_log_dbg(dev, f, ...) \
51 vlib_log (VLIB_LOG_LEVEL_DEBUG, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
53 #define _IOCTL(fd,a,...) \
54 if (ioctl (fd, a, __VA_ARGS__) < 0) \
56 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
57 tap_log_err (vif, "%U", format_clib_error, err); \
61 VNET_HW_INTERFACE_CLASS (tun_device_hw_interface_class, static) = {
63 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
64 .tx_hash_fn_type = VNET_HASH_FN_TYPE_IP,
67 #define TUN_MAX_PACKET_BYTES 65355
68 #define TUN_MIN_PACKET_BYTES 64
69 #define TUN_DEFAULT_PACKET_BYTES 1500
72 virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
76 //TODO On MTU change call vnet_netlink_set_if_mtu
80 #define TAP_MAX_INSTANCE 1024
83 tap_free (vlib_main_t * vm, virtio_if_t * vif)
85 virtio_main_t *mm = &virtio_main;
86 tap_main_t *tm = &tap_main;
87 clib_error_t *err = 0;
91 vec_foreach_index (i, vif->vhost_fds) if (vif->vhost_fds[i] != -1)
92 close (vif->vhost_fds[i]);
93 vec_foreach_index (i, vif->rxq_vrings)
94 virtio_vring_free_rx (vm, vif, RX_QUEUE (i));
95 vec_foreach_index (i, vif->txq_vrings)
96 virtio_vring_free_tx (vm, vif, TX_QUEUE (i));
101 _IOCTL (vif->tap_fds[0], TUNSETPERSIST, (void *) (uintptr_t) 0);
102 tap_log_dbg (vif, "TUNSETPERSIST: unset");
105 vec_foreach_index (i, vif->tap_fds) close (vif->tap_fds[i]);
107 vec_free (vif->tap_fds);
108 vec_free (vif->vhost_fds);
109 vec_free (vif->rxq_vrings);
110 vec_free (vif->txq_vrings);
111 vec_free (vif->host_if_name);
112 vec_free (vif->net_ns);
113 vec_free (vif->host_bridge);
114 clib_error_free (vif->error);
116 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
117 clib_memset (vif, 0, sizeof (*vif));
118 pool_put (mm->interfaces, vif);
122 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
124 vlib_thread_main_t *thm = vlib_get_thread_main ();
125 vlib_physmem_main_t *vpm = &vm->physmem_main;
126 vnet_main_t *vnm = vnet_get_main ();
127 virtio_main_t *vim = &virtio_main;
128 tap_main_t *tm = &tap_main;
129 vnet_sw_interface_t *sw;
130 vnet_hw_interface_t *hw;
131 int i, num_vhost_queues;
132 int old_netns_fd = -1;
133 struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR };
134 struct ifreq get_ifr = {.ifr_flags = 0 };
136 vhost_memory_t *vhost_mem = 0;
137 virtio_if_t *vif = 0;
138 clib_error_t *err = 0;
139 unsigned int tap_features;
140 int tfd = -1, qfd = -1, vfd = -1, nfd = -1;
141 char *host_if_name = 0;
142 unsigned int offload = 0;
147 if (clib_bitmap_get (tm->tap_ids, args->id))
149 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
150 args->error = clib_error_return (0, "interface already exists");
156 args->id = clib_bitmap_first_clear (tm->tap_ids);
159 if (args->id > TAP_MAX_INSTANCE)
161 args->rv = VNET_API_ERROR_UNSPECIFIED;
162 args->error = clib_error_return (0, "cannot find free interface id");
166 pool_get_zero (vim->interfaces, vif);
168 if (args->tap_flags & TAP_FLAG_TUN)
170 vif->type = VIRTIO_IF_TYPE_TUN;
171 ifr.ifr_flags |= IFF_TUN;
174 * From kernel 4.20, xdp support has been added in tun_sendmsg.
175 * If sndbuf == INT_MAX, vhost batches the packet and processes
176 * them using xdp data path for tun driver. It assumes packets
177 * are ethernet frames (It needs to be fixed).
178 * To avoid xdp data path in tun driver, sndbuf value should
181 sndbuf = INT_MAX - 1;
185 vif->type = VIRTIO_IF_TYPE_TAP;
186 ifr.ifr_flags |= IFF_TAP;
190 vif->dev_instance = vif - vim->interfaces;
192 vif->num_txqs = thm->n_vlib_mains;
193 vif->num_rxqs = clib_max (args->num_rx_queues, 1);
195 if (args->tap_flags & TAP_FLAG_ATTACH)
197 if (args->host_if_name == NULL)
199 args->rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
200 err = clib_error_return (0, "host_if_name is not provided");
205 /* if namespace is specified, all further netlink messages should be executed
206 * after we change our net namespace */
207 if (args->host_namespace)
209 old_netns_fd = clib_netns_open (NULL /* self */);
210 if ((nfd = clib_netns_open (args->host_namespace)) == -1)
212 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
213 args->error = clib_error_return_unix (0, "clib_netns_open '%s'",
214 args->host_namespace);
217 if (clib_setns (nfd) == -1)
219 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
221 clib_error_return_unix (0, "setns '%s'", args->host_namespace);
226 if (args->host_if_name != NULL)
228 host_if_name = (char *) args->host_if_name;
229 clib_memcpy (ifr.ifr_name, host_if_name,
230 clib_min (IFNAMSIZ, vec_len (host_if_name)));
233 if ((tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
235 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
236 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
239 vec_add1 (vif->tap_fds, tfd);
240 tap_log_dbg (vif, "open tap fd %d", tfd);
242 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
243 tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
244 if ((tap_features & IFF_VNET_HDR) == 0)
246 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
247 args->error = clib_error_return (0, "vhost-net backend not available");
251 if ((tap_features & IFF_MULTI_QUEUE) == 0)
253 if (vif->num_rxqs > 1)
255 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
256 args->error = clib_error_return (0, "multiqueue not supported");
259 vif->num_rxqs = vif->num_txqs = 1;
262 ifr.ifr_flags |= IFF_MULTI_QUEUE;
264 hdrsz = sizeof (virtio_net_hdr_v1_t);
265 if (args->tap_flags & TAP_FLAG_GSO)
267 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
268 vif->gso_enabled = 1;
270 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
272 offload = TUN_F_CSUM;
273 vif->csum_offload_enabled = 1;
276 _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
277 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
278 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
280 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
281 tap_log_dbg (vif, "ifindex %d", vif->ifindex);
283 if (!args->host_if_name)
284 host_if_name = ifr.ifr_ifrn.ifrn_name;
286 host_if_name = (char *) args->host_if_name;
289 * unset the persistence when attaching to existing
292 if (args->tap_flags & TAP_FLAG_ATTACH)
294 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 0);
295 tap_log_dbg (vif, "TUNSETPERSIST: unset");
298 /* set the persistence */
299 if (args->tap_flags & TAP_FLAG_PERSIST)
301 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 1);
302 tap_log_dbg (vif, "TUNSETPERSIST: set");
304 /* verify persistence is set, read the flags */
305 _IOCTL (tfd, TUNGETIFF, (void *) &get_ifr);
306 tap_log_dbg (vif, "TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
307 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
309 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
310 args->error = clib_error_return (0, "persistence not supported");
315 /* create additional queues on the linux side.
316 * we create as many linux queue pairs as we have rx queues
318 for (i = 1; i < vif->num_rxqs; i++)
320 if ((qfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
322 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
323 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
326 _IOCTL (qfd, TUNSETIFF, (void *) &ifr);
327 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", qfd,
328 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
329 vec_add1 (vif->tap_fds, qfd);
332 for (i = 0; i < vif->num_rxqs; i++)
334 tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
335 vif->tap_fds[i], hdrsz);
336 _IOCTL (vif->tap_fds[i], TUNSETVNETHDRSZ, &hdrsz);
338 tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", vif->tap_fds[i],
340 _IOCTL (vif->tap_fds[i], TUNSETSNDBUF, &sndbuf);
342 tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", vif->tap_fds[i],
344 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
346 if (fcntl (vif->tap_fds[i], F_SETFL, O_NONBLOCK) < 0)
348 err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
349 tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
354 /* open as many vhost-net fds as required and set ownership */
355 num_vhost_queues = clib_max (vif->num_rxqs, vif->num_txqs);
356 for (i = 0; i < num_vhost_queues; i++)
358 if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
360 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
361 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
364 vec_add1 (vif->vhost_fds, vfd);
365 virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
366 _IOCTL (vfd, VHOST_SET_OWNER, 0);
367 virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
370 _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
371 virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
372 vif->remote_features);
374 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
376 args->rv = VNET_API_ERROR_UNSUPPORTED;
377 args->error = clib_error_return (0, "vhost-net backend doesn't support "
378 "VIRTIO_NET_F_MRG_RXBUF feature");
382 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
385 args->rv = VNET_API_ERROR_UNSUPPORTED;
386 args->error = clib_error_return (0, "vhost-net backend doesn't support "
387 "VIRTIO_RING_F_INDIRECT_DESC feature");
391 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
393 args->rv = VNET_API_ERROR_UNSUPPORTED;
394 args->error = clib_error_return (0, "vhost-net backend doesn't support "
395 "VIRTIO_F_VERSION_1 features");
399 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
400 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
401 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
403 virtio_set_net_hdr_size (vif);
405 if (vif->type == VIRTIO_IF_TYPE_TAP)
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,
424 args->rv = VNET_API_ERROR_NETLINK_ERROR;
430 if (args->host_ip4_prefix_len)
432 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
433 &args->host_ip4_addr,
434 args->host_ip4_prefix_len);
437 args->rv = VNET_API_ERROR_NETLINK_ERROR;
442 if (args->host_ip6_prefix_len)
444 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
445 &args->host_ip6_addr,
446 args->host_ip6_prefix_len);
449 args->rv = VNET_API_ERROR_NETLINK_ERROR;
454 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
457 args->rv = VNET_API_ERROR_NETLINK_ERROR;
461 if (args->host_ip4_gw_set)
463 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
466 args->rv = VNET_API_ERROR_NETLINK_ERROR;
471 if (args->host_ip6_gw_set)
473 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
476 args->rv = VNET_API_ERROR_NETLINK_ERROR;
481 if (args->host_mtu_set)
484 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
487 args->rv = VNET_API_ERROR_NETLINK_ERROR;
491 else if (tm->host_mtu_size != 0)
494 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
497 args->rv = VNET_API_ERROR_NETLINK_ERROR;
500 args->host_mtu_set = 1;
501 args->host_mtu_size = tm->host_mtu_size;
504 /* switch back to old net namespace */
505 if (args->host_namespace)
507 if (clib_setns (old_netns_fd) == -1)
509 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
510 args->error = clib_error_return_unix (0, "setns '%s'",
511 args->host_namespace);
516 for (i = 0; i < num_vhost_queues; i++)
518 if (i < vif->num_rxqs && (args->error =
519 virtio_vring_init (vm, vif, RX_QUEUE (i),
522 args->rv = VNET_API_ERROR_INIT_FAILED;
526 if (i < vif->num_txqs && (args->error =
527 virtio_vring_init (vm, vif, TX_QUEUE (i),
530 args->rv = VNET_API_ERROR_INIT_FAILED;
535 /* setup features and memtable */
536 i = sizeof (vhost_memory_t) + sizeof (vhost_memory_region_t);
537 vhost_mem = clib_mem_alloc (i);
538 clib_memset (vhost_mem, 0, i);
539 vhost_mem->nregions = 1;
540 vhost_mem->regions[0].memory_size = vpm->max_size;
541 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
542 vhost_mem->regions[0].userspace_addr =
543 vhost_mem->regions[0].guest_phys_addr;
545 for (i = 0; i < vhost_mem->nregions; i++)
546 virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
547 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
548 vhost_mem->regions[0].memory_size,
549 vhost_mem->regions[0].guest_phys_addr,
550 vhost_mem->regions[0].userspace_addr);
553 for (i = 0; i < num_vhost_queues; i++)
555 int fd = vif->vhost_fds[i];
556 _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
557 virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
559 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
560 virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
563 /* finish initializing queue pair */
564 for (i = 0; i < num_vhost_queues * 2; i++)
566 vhost_vring_addr_t addr = { 0 };
567 vhost_vring_state_t state = { 0 };
568 vhost_vring_file_t file = { 0 };
569 virtio_vring_t *vring;
571 int fd = vif->vhost_fds[qp];
575 if (qp >= vif->num_txqs)
577 vring = vec_elt_at_index (vif->txq_vrings, qp);
581 if (qp >= vif->num_rxqs)
583 vring = vec_elt_at_index (vif->rxq_vrings, qp);
586 addr.index = state.index = file.index = vring->queue_id & 1;
587 state.num = vring->size;
588 virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
589 state.index, state.num);
590 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
593 addr.desc_user_addr = pointer_to_uword (vring->desc);
594 addr.avail_user_addr = pointer_to_uword (vring->avail);
595 addr.used_user_addr = pointer_to_uword (vring->used);
597 virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
598 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
599 "used_user_addr 0x%lx", fd, addr.index,
600 addr.flags, addr.desc_user_addr, addr.avail_user_addr,
601 addr.used_user_addr);
602 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
604 file.fd = vring->call_fd;
605 virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
606 fd, file.index, file.fd);
607 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
609 file.fd = vring->kick_fd;
610 virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
611 fd, file.index, file.fd);
612 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
614 file.fd = vif->tap_fds[qp % vif->num_rxqs];
615 virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
616 fd, file.index, file.fd);
617 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
620 if (vif->type == VIRTIO_IF_TYPE_TAP)
622 if (!args->mac_addr_set)
623 ethernet_mac_address_generate (args->mac_addr.bytes);
625 clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6);
626 if (args->host_bridge)
627 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
629 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
630 if (args->host_namespace)
631 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
632 vif->host_mtu_size = args->host_mtu_size;
633 vif->tap_flags = args->tap_flags;
634 clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6);
635 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
636 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
637 if (args->host_ip4_prefix_len)
638 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
639 if (args->host_ip6_prefix_len)
640 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
642 if (vif->type != VIRTIO_IF_TYPE_TUN)
645 ethernet_register_interface (vnm, virtio_device_class.index,
646 vif->dev_instance, vif->mac_addr,
648 virtio_eth_flag_change);
651 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
658 vif->hw_if_index = vnet_register_interface
659 (vnm, virtio_device_class.index,
660 vif->dev_instance /* device instance */ ,
661 tun_device_hw_interface_class.index, vif->dev_instance);
664 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
665 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
666 vif->sw_if_index = sw->sw_if_index;
667 args->sw_if_index = vif->sw_if_index;
669 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
670 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE;
671 if (args->tap_flags & TAP_FLAG_GSO)
673 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
674 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
675 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM;
677 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
679 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
680 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM;
682 if ((args->tap_flags & TAP_FLAG_GSO)
683 && (args->tap_flags & TAP_FLAG_GRO_COALESCE))
685 virtio_set_packet_coalesce (vif);
687 if (vif->type == VIRTIO_IF_TYPE_TUN)
689 hw->max_supported_packet_bytes = TUN_MAX_PACKET_BYTES;
690 hw->min_packet_bytes = hw->min_supported_packet_bytes =
691 TUN_MIN_PACKET_BYTES;
692 hw->max_packet_bytes =
693 args->host_mtu_size ? args->host_mtu_size : TUN_DEFAULT_PACKET_BYTES;
694 vnet_sw_interface_set_mtu (vnm, hw->sw_if_index, hw->max_packet_bytes);
697 virtio_vring_set_rx_queues (vm, vif);
699 vif->per_interface_next_index = ~0;
700 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
701 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
702 VNET_HW_INTERFACE_FLAG_LINK_UP);
704 * Host tun/tap driver link carrier state is "up" at creation. The
705 * driver never changes this unless the backend (VPP) changes it using
706 * TUNSETCARRIER ioctl(). See tap_set_carrier().
708 vif->host_carrier_up = 1;
715 ASSERT (args->error == 0);
717 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
720 tap_log_err (vif, "%U", format_clib_error, args->error);
724 clib_mem_free (vhost_mem);
725 if (old_netns_fd != -1)
727 /* in case we errored with a switched netns */
728 clib_setns (old_netns_fd);
729 close (old_netns_fd);
736 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
738 vnet_main_t *vnm = vnet_get_main ();
739 virtio_main_t *mm = &virtio_main;
741 vnet_hw_interface_t *hw;
743 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 if ((vif->type != VIRTIO_IF_TYPE_TAP) && (vif->type != VIRTIO_IF_TYPE_TUN))
750 return VNET_API_ERROR_INVALID_INTERFACE;
752 /* bring down the interface */
753 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
754 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
756 if (vif->type == VIRTIO_IF_TYPE_TAP)
757 ethernet_delete_interface (vnm, vif->hw_if_index);
758 else /* VIRTIO_IF_TYPE_TUN */
759 vnet_delete_hw_interface (vnm, vif->hw_if_index);
760 vif->hw_if_index = ~0;
768 tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index,
771 vnet_main_t *vnm = vnet_get_main ();
772 virtio_main_t *mm = &virtio_main;
774 vnet_hw_interface_t *hw;
775 clib_error_t *err = 0;
778 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
780 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
781 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
783 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
785 const unsigned int csum_offload_on = TUN_F_CSUM;
786 const unsigned int csum_offload_off = 0;
787 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
788 vec_foreach_index (i, vif->tap_fds)
789 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
790 vif->gso_enabled = 0;
791 vif->packet_coalesce = 0;
792 vif->csum_offload_enabled = enable_disable ? 1 : 0;
794 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) != 0)
796 hw->caps &= ~VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO;
801 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
805 hw->caps &= ~VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
811 clib_warning ("Error %s checksum offload on sw_if_index %d",
812 enable_disable ? "enabling" : "disabling", sw_if_index);
813 return VNET_API_ERROR_SYSCALL_ERROR_3;
819 tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable,
820 int is_packet_coalesce)
822 vnet_main_t *vnm = vnet_get_main ();
823 virtio_main_t *mm = &virtio_main;
825 vnet_hw_interface_t *hw;
826 clib_error_t *err = 0;
829 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
831 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
832 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
834 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
836 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
837 const unsigned int gso_off = 0;
838 unsigned int offload = enable_disable ? gso_on : gso_off;
839 vec_foreach_index (i, vif->tap_fds)
840 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
841 vif->gso_enabled = enable_disable ? 1 : 0;
842 vif->csum_offload_enabled = 0;
845 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) == 0)
847 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
848 VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
850 if (is_packet_coalesce)
852 virtio_set_packet_coalesce (vif);
857 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) != 0)
859 hw->caps &= ~(VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
860 VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM);
862 vif->packet_coalesce = 0;
868 clib_warning ("Error %s gso on sw_if_index %d",
869 enable_disable ? "enabling" : "disabling", sw_if_index);
870 return VNET_API_ERROR_SYSCALL_ERROR_3;
876 tap_dump_ifs (tap_interface_details_t ** out_tapids)
878 vnet_main_t *vnm = vnet_get_main ();
879 virtio_main_t *mm = &virtio_main;
881 virtio_vring_t *vring;
882 vnet_hw_interface_t *hi;
883 tap_interface_details_t *r_tapids = NULL;
884 tap_interface_details_t *tapid = NULL;
887 pool_foreach (vif, mm->interfaces) {
888 if ((vif->type != VIRTIO_IF_TYPE_TAP)
889 && (vif->type != VIRTIO_IF_TYPE_TUN))
891 vec_add2(r_tapids, tapid, 1);
892 clib_memset (tapid, 0, sizeof (*tapid));
894 tapid->sw_if_index = vif->sw_if_index;
895 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
896 clib_memcpy(tapid->dev_name, hi->name,
897 MIN (ARRAY_LEN (tapid->dev_name) - 1, vec_len (hi->name)));
898 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
899 tapid->rx_ring_sz = vring->size;
900 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
901 tapid->tx_ring_sz = vring->size;
902 tapid->tap_flags = vif->tap_flags;
903 clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6);
904 if (vif->host_if_name)
906 clib_memcpy(tapid->host_if_name, vif->host_if_name,
907 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
908 vec_len (vif->host_if_name)));
912 clib_memcpy(tapid->host_namespace, vif->net_ns,
913 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
914 vec_len (vif->net_ns)));
916 if (vif->host_bridge)
918 clib_memcpy(tapid->host_bridge, vif->host_bridge,
919 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
920 vec_len (vif->host_bridge)));
922 if (vif->host_ip4_prefix_len)
923 clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4);
924 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
925 if (vif->host_ip6_prefix_len)
926 clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16);
927 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
928 tapid->host_mtu_size = vif->host_mtu_size;
932 *out_tapids = r_tapids;
938 * Set host tap/tun interface carrier state so it will appear to host
939 * applications that the interface's link state changed.
941 * If the kernel we're building against does not have support for the
942 * TUNSETCARRIER ioctl command, do nothing.
945 tap_set_carrier (u32 hw_if_index, u32 carrier_up)
949 vnet_main_t *vnm = vnet_get_main ();
950 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
951 virtio_main_t *mm = &virtio_main;
955 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
956 vec_foreach (fd, vif->tap_fds)
958 ret = ioctl (*fd, TUNSETCARRIER, &carrier_up);
961 clib_warning ("ioctl (TUNSETCARRIER) returned %d", ret);
966 vif->host_carrier_up = (carrier_up != 0);
972 static clib_error_t *
973 tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
975 tap_main_t *tm = &tap_main;
977 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
979 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
982 return clib_error_return (0, "unknown input `%U'",
983 format_unformat_error, input);
990 * Set host tap/tun interface speed in Mbps.
993 tap_set_speed (u32 hw_if_index, u32 speed)
995 vnet_main_t *vnm = vnet_get_main ();
996 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
997 virtio_main_t *mm = &virtio_main;
999 int old_netns_fd = -1;
1003 struct ethtool_cmd ecmd;
1006 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
1010 old_netns_fd = clib_netns_open (NULL /* self */);
1011 if ((nfd = clib_netns_open (vif->net_ns)) == -1)
1013 clib_warning ("Cannot open netns");
1016 if (clib_setns (nfd) == -1)
1018 clib_warning ("Cannot set ns");
1023 if ((ctl_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
1025 clib_warning ("Cannot open control socket");
1029 ecmd.cmd = ETHTOOL_GSET;
1030 clib_memset (&ifr, 0, sizeof (ifr));
1031 clib_memcpy (ifr.ifr_name, vif->host_if_name,
1032 strlen ((const char *) vif->host_if_name));
1033 ifr.ifr_data = (void *) &ecmd;
1034 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1036 clib_warning ("Cannot get device settings");
1040 if (ethtool_cmd_speed (&ecmd) != speed)
1042 ecmd.cmd = ETHTOOL_SSET;
1043 ethtool_cmd_speed_set (&ecmd, speed);
1044 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1046 clib_warning ("Cannot set device settings");
1052 if (old_netns_fd != -1)
1054 if (clib_setns (old_netns_fd) == -1)
1056 clib_warning ("Cannot set old ns");
1058 close (old_netns_fd);
1068 /* tap { host-mtu <size> } configuration. */
1069 VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
1071 static clib_error_t *
1072 tap_init (vlib_main_t * vm)
1074 tap_main_t *tm = &tap_main;
1075 clib_error_t *error = 0;
1077 tm->log_default = vlib_log_register_class ("tap", 0);
1078 vlib_log_debug (tm->log_default, "initialized");
1080 tm->host_mtu_size = 0;
1085 VLIB_INIT_FUNCTION (tap_init);
1088 * fd.io coding-style-patch-verification: ON
1091 * eval: (c-set-style "gnu")