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); \
62 VNET_HW_INTERFACE_CLASS (tun_device_hw_interface_class, static) =
65 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
69 #define TUN_MAX_PACKET_BYTES 65355
70 #define TUN_MIN_PACKET_BYTES 64
71 #define TUN_DEFAULT_PACKET_BYTES 1500
74 virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
78 //TODO On MTU change call vnet_netlink_set_if_mtu
82 #define TAP_MAX_INSTANCE 1024
85 tap_free (vlib_main_t * vm, virtio_if_t * vif)
87 virtio_main_t *mm = &virtio_main;
88 tap_main_t *tm = &tap_main;
89 clib_error_t *err = 0;
93 vec_foreach_index (i, vif->vhost_fds) if (vif->vhost_fds[i] != -1)
94 close (vif->vhost_fds[i]);
95 vec_foreach_index (i, vif->rxq_vrings)
96 virtio_vring_free_rx (vm, vif, RX_QUEUE (i));
97 vec_foreach_index (i, vif->txq_vrings)
98 virtio_vring_free_tx (vm, vif, TX_QUEUE (i));
103 _IOCTL (vif->tap_fds[0], TUNSETPERSIST, (void *) (uintptr_t) 0);
104 tap_log_dbg (vif, "TUNSETPERSIST: unset");
107 vec_foreach_index (i, vif->tap_fds) close (vif->tap_fds[i]);
109 vec_free (vif->vhost_fds);
110 vec_free (vif->rxq_vrings);
111 vec_free (vif->txq_vrings);
112 vec_free (vif->host_if_name);
113 vec_free (vif->net_ns);
114 vec_free (vif->host_bridge);
115 clib_error_free (vif->error);
117 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
118 clib_memset (vif, 0, sizeof (*vif));
119 pool_put (mm->interfaces, vif);
123 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
125 vlib_thread_main_t *thm = vlib_get_thread_main ();
126 vlib_physmem_main_t *vpm = &vm->physmem_main;
127 vnet_main_t *vnm = vnet_get_main ();
128 virtio_main_t *vim = &virtio_main;
129 tap_main_t *tm = &tap_main;
130 vnet_sw_interface_t *sw;
131 vnet_hw_interface_t *hw;
132 int i, num_vhost_queues;
133 int old_netns_fd = -1;
134 struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR };
135 struct ifreq get_ifr = {.ifr_flags = 0 };
137 vhost_memory_t *vhost_mem = 0;
138 virtio_if_t *vif = 0;
139 clib_error_t *err = 0;
140 unsigned int tap_features;
141 int tfd = -1, qfd = -1, vfd = -1, nfd = -1;
142 char *host_if_name = 0;
143 unsigned int offload = 0;
148 if (clib_bitmap_get (tm->tap_ids, args->id))
150 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
151 args->error = clib_error_return (0, "interface already exists");
157 args->id = clib_bitmap_first_clear (tm->tap_ids);
160 if (args->id > TAP_MAX_INSTANCE)
162 args->rv = VNET_API_ERROR_UNSPECIFIED;
163 args->error = clib_error_return (0, "cannot find free interface id");
167 pool_get_zero (vim->interfaces, vif);
169 if (args->tap_flags & TAP_FLAG_TUN)
171 vif->type = VIRTIO_IF_TYPE_TUN;
172 ifr.ifr_flags |= IFF_TUN;
175 * From kernel 4.20, xdp support has been added in tun_sendmsg.
176 * If sndbuf == INT_MAX, vhost batches the packet and processes
177 * them using xdp data path for tun driver. It assumes packets
178 * are ethernet frames (It needs to be fixed).
179 * To avoid xdp data path in tun driver, sndbuf value should
182 sndbuf = INT_MAX - 1;
186 vif->type = VIRTIO_IF_TYPE_TAP;
187 ifr.ifr_flags |= IFF_TAP;
191 vif->dev_instance = vif - vim->interfaces;
193 vif->num_txqs = thm->n_vlib_mains;
194 vif->num_rxqs = clib_max (args->num_rx_queues, 1);
196 if (args->tap_flags & TAP_FLAG_ATTACH)
198 if (args->host_if_name == NULL)
200 args->rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
201 err = clib_error_return (0, "host_if_name is not provided");
204 if (args->host_namespace)
206 old_netns_fd = clib_netns_open (NULL /* self */);
207 if ((nfd = clib_netns_open (args->host_namespace)) == -1)
209 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
210 args->error = clib_error_return_unix (0, "clib_netns_open '%s'",
211 args->host_namespace);
214 if (clib_setns (nfd) == -1)
216 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
217 args->error = clib_error_return_unix (0, "setns '%s'",
218 args->host_namespace);
224 if (args->host_if_name != NULL)
226 host_if_name = (char *) args->host_if_name;
227 clib_memcpy (ifr.ifr_name, host_if_name,
228 clib_min (IFNAMSIZ, vec_len (host_if_name)));
231 if ((tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
233 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
234 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
237 vec_add1 (vif->tap_fds, tfd);
238 tap_log_dbg (vif, "open tap fd %d", tfd);
240 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
241 tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
242 if ((tap_features & IFF_VNET_HDR) == 0)
244 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
245 args->error = clib_error_return (0, "vhost-net backend not available");
249 if ((tap_features & IFF_MULTI_QUEUE) == 0)
251 if (vif->num_rxqs > 1)
253 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
254 args->error = clib_error_return (0, "multiqueue not supported");
257 vif->num_rxqs = vif->num_txqs = 1;
260 ifr.ifr_flags |= IFF_MULTI_QUEUE;
262 hdrsz = sizeof (virtio_net_hdr_v1_t);
263 if (args->tap_flags & TAP_FLAG_GSO)
265 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
266 vif->gso_enabled = 1;
268 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
270 offload = TUN_F_CSUM;
271 vif->csum_offload_enabled = 1;
274 _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
275 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
276 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
278 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
279 tap_log_dbg (vif, "ifindex %d", vif->ifindex);
281 if (!args->host_if_name)
282 host_if_name = ifr.ifr_ifrn.ifrn_name;
284 host_if_name = (char *) args->host_if_name;
287 * unset the persistence when attaching to existing
290 if (args->tap_flags & TAP_FLAG_ATTACH)
292 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 0);
293 tap_log_dbg (vif, "TUNSETPERSIST: unset");
296 /* set the persistence */
297 if (args->tap_flags & TAP_FLAG_PERSIST)
299 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 1);
300 tap_log_dbg (vif, "TUNSETPERSIST: set");
302 /* verify persistence is set, read the flags */
303 _IOCTL (tfd, TUNGETIFF, (void *) &get_ifr);
304 tap_log_dbg (vif, "TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
305 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
307 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
308 args->error = clib_error_return (0, "persistence not supported");
313 /* create additional queues on the linux side.
314 * we create as many linux queue pairs as we have rx queues
316 for (i = 1; i < vif->num_rxqs; i++)
318 if ((qfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
320 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
321 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
324 _IOCTL (qfd, TUNSETIFF, (void *) &ifr);
325 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", qfd,
326 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
327 vec_add1 (vif->tap_fds, qfd);
330 for (i = 0; i < vif->num_rxqs; i++)
332 tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
333 vif->tap_fds[i], hdrsz);
334 _IOCTL (vif->tap_fds[i], TUNSETVNETHDRSZ, &hdrsz);
336 tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", vif->tap_fds[i],
338 _IOCTL (vif->tap_fds[i], TUNSETSNDBUF, &sndbuf);
340 tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", vif->tap_fds[i],
342 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
344 if (fcntl (vif->tap_fds[i], F_SETFL, O_NONBLOCK) < 0)
346 err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
347 tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
352 /* open as many vhost-net fds as required and set ownership */
353 num_vhost_queues = clib_max (vif->num_rxqs, vif->num_txqs);
354 for (i = 0; i < num_vhost_queues; i++)
356 if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
358 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
359 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
362 vec_add1 (vif->vhost_fds, vfd);
363 virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
364 _IOCTL (vfd, VHOST_SET_OWNER, 0);
365 virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
368 _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
369 virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
370 vif->remote_features);
372 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
374 args->rv = VNET_API_ERROR_UNSUPPORTED;
375 args->error = clib_error_return (0, "vhost-net backend doesn't support "
376 "VIRTIO_NET_F_MRG_RXBUF feature");
380 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
383 args->rv = VNET_API_ERROR_UNSUPPORTED;
384 args->error = clib_error_return (0, "vhost-net backend doesn't support "
385 "VIRTIO_RING_F_INDIRECT_DESC feature");
389 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
391 args->rv = VNET_API_ERROR_UNSUPPORTED;
392 args->error = clib_error_return (0, "vhost-net backend doesn't support "
393 "VIRTIO_F_VERSION_1 features");
397 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
398 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
399 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
401 virtio_set_net_hdr_size (vif);
403 if (!(args->tap_flags & TAP_FLAG_ATTACH))
405 /* if namespace is specified, all further netlink messages should be executed
406 after we change our net namespace */
407 if (args->host_namespace)
409 old_netns_fd = clib_netns_open (NULL /* self */);
410 if ((nfd = clib_netns_open (args->host_namespace)) == -1)
412 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
413 args->error = clib_error_return_unix (0, "clib_netns_open '%s'",
414 args->host_namespace);
417 args->error = vnet_netlink_set_link_netns (vif->ifindex, nfd,
421 args->rv = VNET_API_ERROR_NETLINK_ERROR;
424 if (clib_setns (nfd) == -1)
426 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
427 args->error = clib_error_return_unix (0, "setns '%s'",
428 args->host_namespace);
431 if ((vif->ifindex = if_nametoindex (host_if_name)) == 0)
433 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
434 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
441 if (vif->type == VIRTIO_IF_TYPE_TAP)
443 if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes))
444 ethernet_mac_address_generate (args->host_mac_addr.bytes);
445 args->error = vnet_netlink_set_link_addr (vif->ifindex,
446 args->host_mac_addr.bytes);
449 args->rv = VNET_API_ERROR_NETLINK_ERROR;
453 if (args->host_bridge)
455 args->error = vnet_netlink_set_link_master (vif->ifindex,
460 args->rv = VNET_API_ERROR_NETLINK_ERROR;
466 if (args->host_ip4_prefix_len)
468 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
469 &args->host_ip4_addr,
470 args->host_ip4_prefix_len);
473 args->rv = VNET_API_ERROR_NETLINK_ERROR;
478 if (args->host_ip6_prefix_len)
480 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
481 &args->host_ip6_addr,
482 args->host_ip6_prefix_len);
485 args->rv = VNET_API_ERROR_NETLINK_ERROR;
490 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
493 args->rv = VNET_API_ERROR_NETLINK_ERROR;
497 if (args->host_ip4_gw_set)
499 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
502 args->rv = VNET_API_ERROR_NETLINK_ERROR;
507 if (args->host_ip6_gw_set)
509 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
512 args->rv = VNET_API_ERROR_NETLINK_ERROR;
517 if (args->host_mtu_set)
520 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
523 args->rv = VNET_API_ERROR_NETLINK_ERROR;
527 else if (tm->host_mtu_size != 0)
530 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
533 args->rv = VNET_API_ERROR_NETLINK_ERROR;
536 args->host_mtu_set = 1;
537 args->host_mtu_size = tm->host_mtu_size;
540 /* switch back to old net namespace */
541 if (args->host_namespace)
543 if (clib_setns (old_netns_fd) == -1)
545 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
546 args->error = clib_error_return_unix (0, "setns '%s'",
547 args->host_namespace);
552 for (i = 0; i < num_vhost_queues; i++)
554 if (i < vif->num_rxqs && (args->error =
555 virtio_vring_init (vm, vif, RX_QUEUE (i),
558 args->rv = VNET_API_ERROR_INIT_FAILED;
562 if (i < vif->num_txqs && (args->error =
563 virtio_vring_init (vm, vif, TX_QUEUE (i),
566 args->rv = VNET_API_ERROR_INIT_FAILED;
571 /* setup features and memtable */
572 i = sizeof (vhost_memory_t) + sizeof (vhost_memory_region_t);
573 vhost_mem = clib_mem_alloc (i);
574 clib_memset (vhost_mem, 0, i);
575 vhost_mem->nregions = 1;
576 vhost_mem->regions[0].memory_size = vpm->max_size;
577 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
578 vhost_mem->regions[0].userspace_addr =
579 vhost_mem->regions[0].guest_phys_addr;
581 for (i = 0; i < vhost_mem->nregions; i++)
582 virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
583 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
584 vhost_mem->regions[0].memory_size,
585 vhost_mem->regions[0].guest_phys_addr,
586 vhost_mem->regions[0].userspace_addr);
589 for (i = 0; i < num_vhost_queues; i++)
591 int fd = vif->vhost_fds[i];
592 _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
593 virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
595 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
596 virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
599 /* finish initializing queue pair */
600 for (i = 0; i < num_vhost_queues * 2; i++)
602 vhost_vring_addr_t addr = { 0 };
603 vhost_vring_state_t state = { 0 };
604 vhost_vring_file_t file = { 0 };
605 virtio_vring_t *vring;
607 int fd = vif->vhost_fds[qp];
611 if (qp >= vif->num_txqs)
613 vring = vec_elt_at_index (vif->txq_vrings, qp);
617 if (qp >= vif->num_rxqs)
619 vring = vec_elt_at_index (vif->rxq_vrings, qp);
622 addr.index = state.index = file.index = vring->queue_id & 1;
623 state.num = vring->size;
624 virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
625 state.index, state.num);
626 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
629 addr.desc_user_addr = pointer_to_uword (vring->desc);
630 addr.avail_user_addr = pointer_to_uword (vring->avail);
631 addr.used_user_addr = pointer_to_uword (vring->used);
633 virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
634 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
635 "used_user_addr 0x%lx", fd, addr.index,
636 addr.flags, addr.desc_user_addr, addr.avail_user_addr,
637 addr.used_user_addr);
638 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
640 file.fd = vring->call_fd;
641 virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
642 fd, file.index, file.fd);
643 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
645 file.fd = vring->kick_fd;
646 virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
647 fd, file.index, file.fd);
648 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
650 file.fd = vif->tap_fds[qp % vif->num_rxqs];
651 virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
652 fd, file.index, file.fd);
653 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
656 if (vif->type == VIRTIO_IF_TYPE_TAP)
658 if (!args->mac_addr_set)
659 ethernet_mac_address_generate (args->mac_addr.bytes);
661 clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6);
662 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
664 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
665 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
666 vif->host_mtu_size = args->host_mtu_size;
667 vif->tap_flags = args->tap_flags;
668 clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6);
669 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
670 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
671 if (args->host_ip4_prefix_len)
672 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
673 if (args->host_ip6_prefix_len)
674 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
676 if (vif->type != VIRTIO_IF_TYPE_TUN)
679 ethernet_register_interface (vnm, virtio_device_class.index,
680 vif->dev_instance, vif->mac_addr,
682 virtio_eth_flag_change);
685 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
692 vif->hw_if_index = vnet_register_interface
693 (vnm, virtio_device_class.index,
694 vif->dev_instance /* device instance */ ,
695 tun_device_hw_interface_class.index, vif->dev_instance);
698 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
699 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
700 vif->sw_if_index = sw->sw_if_index;
701 args->sw_if_index = vif->sw_if_index;
703 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
704 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE;
705 if (args->tap_flags & TAP_FLAG_GSO)
707 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
708 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
709 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM;
711 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
713 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
714 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM;
716 if ((args->tap_flags & TAP_FLAG_GSO)
717 && (args->tap_flags & TAP_FLAG_GRO_COALESCE))
719 virtio_set_packet_coalesce (vif);
721 if (vif->type == VIRTIO_IF_TYPE_TUN)
723 hw->max_supported_packet_bytes = TUN_MAX_PACKET_BYTES;
724 hw->min_packet_bytes = hw->min_supported_packet_bytes =
725 TUN_MIN_PACKET_BYTES;
726 hw->max_packet_bytes =
727 args->host_mtu_size ? args->host_mtu_size : TUN_DEFAULT_PACKET_BYTES;
728 vnet_sw_interface_set_mtu (vnm, hw->sw_if_index, hw->max_packet_bytes);
731 virtio_vring_set_rx_queues (vm, vif);
733 vif->per_interface_next_index = ~0;
734 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
735 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
736 VNET_HW_INTERFACE_FLAG_LINK_UP);
738 * Host tun/tap driver link carrier state is "up" at creation. The
739 * driver never changes this unless the backend (VPP) changes it using
740 * TUNSETCARRIER ioctl(). See tap_set_carrier().
742 vif->host_carrier_up = 1;
743 vif->cxq_vring = NULL;
750 ASSERT (args->error == 0);
752 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
755 tap_log_err (vif, "%U", format_clib_error, args->error);
759 clib_mem_free (vhost_mem);
760 if (old_netns_fd != -1)
761 close (old_netns_fd);
767 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
769 vnet_main_t *vnm = vnet_get_main ();
770 virtio_main_t *mm = &virtio_main;
772 vnet_hw_interface_t *hw;
774 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
775 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
776 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
778 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
780 if ((vif->type != VIRTIO_IF_TYPE_TAP) && (vif->type != VIRTIO_IF_TYPE_TUN))
781 return VNET_API_ERROR_INVALID_INTERFACE;
783 /* bring down the interface */
784 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
785 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
787 if (vif->type == VIRTIO_IF_TYPE_TAP)
788 ethernet_delete_interface (vnm, vif->hw_if_index);
789 else /* VIRTIO_IF_TYPE_TUN */
790 vnet_delete_hw_interface (vnm, vif->hw_if_index);
791 vif->hw_if_index = ~0;
799 tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index,
802 vnet_main_t *vnm = vnet_get_main ();
803 virtio_main_t *mm = &virtio_main;
805 vnet_hw_interface_t *hw;
806 clib_error_t *err = 0;
809 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
811 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
812 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
814 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
816 const unsigned int csum_offload_on = TUN_F_CSUM;
817 const unsigned int csum_offload_off = 0;
818 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
819 vec_foreach_index (i, vif->tap_fds)
820 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
821 vif->gso_enabled = 0;
822 vif->packet_coalesce = 0;
823 vif->csum_offload_enabled = enable_disable ? 1 : 0;
825 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) != 0)
827 hw->caps &= ~VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO;
832 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
836 hw->caps &= ~VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
842 clib_warning ("Error %s checksum offload on sw_if_index %d",
843 enable_disable ? "enabling" : "disabling", sw_if_index);
844 return VNET_API_ERROR_SYSCALL_ERROR_3;
850 tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable,
851 int is_packet_coalesce)
853 vnet_main_t *vnm = vnet_get_main ();
854 virtio_main_t *mm = &virtio_main;
856 vnet_hw_interface_t *hw;
857 clib_error_t *err = 0;
860 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
862 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
863 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
865 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
867 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
868 const unsigned int gso_off = 0;
869 unsigned int offload = enable_disable ? gso_on : gso_off;
870 vec_foreach_index (i, vif->tap_fds)
871 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
872 vif->gso_enabled = enable_disable ? 1 : 0;
873 vif->csum_offload_enabled = 0;
876 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) == 0)
878 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
879 VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
881 if (is_packet_coalesce)
883 virtio_set_packet_coalesce (vif);
888 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) != 0)
890 hw->caps &= ~(VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
891 VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM);
893 vif->packet_coalesce = 0;
899 clib_warning ("Error %s gso on sw_if_index %d",
900 enable_disable ? "enabling" : "disabling", sw_if_index);
901 return VNET_API_ERROR_SYSCALL_ERROR_3;
907 tap_dump_ifs (tap_interface_details_t ** out_tapids)
909 vnet_main_t *vnm = vnet_get_main ();
910 virtio_main_t *mm = &virtio_main;
912 virtio_vring_t *vring;
913 vnet_hw_interface_t *hi;
914 tap_interface_details_t *r_tapids = NULL;
915 tap_interface_details_t *tapid = NULL;
918 pool_foreach (vif, mm->interfaces) {
919 if ((vif->type != VIRTIO_IF_TYPE_TAP)
920 && (vif->type != VIRTIO_IF_TYPE_TUN))
922 vec_add2(r_tapids, tapid, 1);
923 clib_memset (tapid, 0, sizeof (*tapid));
925 tapid->sw_if_index = vif->sw_if_index;
926 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
927 clib_memcpy(tapid->dev_name, hi->name,
928 MIN (ARRAY_LEN (tapid->dev_name) - 1, vec_len (hi->name)));
929 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
930 tapid->rx_ring_sz = vring->size;
931 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
932 tapid->tx_ring_sz = vring->size;
933 tapid->tap_flags = vif->tap_flags;
934 clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6);
935 if (vif->host_if_name)
937 clib_memcpy(tapid->host_if_name, vif->host_if_name,
938 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
939 vec_len (vif->host_if_name)));
943 clib_memcpy(tapid->host_namespace, vif->net_ns,
944 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
945 vec_len (vif->net_ns)));
947 if (vif->host_bridge)
949 clib_memcpy(tapid->host_bridge, vif->host_bridge,
950 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
951 vec_len (vif->host_bridge)));
953 if (vif->host_ip4_prefix_len)
954 clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4);
955 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
956 if (vif->host_ip6_prefix_len)
957 clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16);
958 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
959 tapid->host_mtu_size = vif->host_mtu_size;
963 *out_tapids = r_tapids;
969 * Set host tap/tun interface carrier state so it will appear to host
970 * applications that the interface's link state changed.
972 * If the kernel we're building against does not have support for the
973 * TUNSETCARRIER ioctl command, do nothing.
976 tap_set_carrier (u32 hw_if_index, u32 carrier_up)
980 vnet_main_t *vnm = vnet_get_main ();
981 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
982 virtio_main_t *mm = &virtio_main;
986 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
987 vec_foreach (fd, vif->tap_fds)
989 ret = ioctl (*fd, TUNSETCARRIER, &carrier_up);
992 clib_warning ("ioctl (TUNSETCARRIER) returned %d", ret);
997 vif->host_carrier_up = (carrier_up != 0);
1003 static clib_error_t *
1004 tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
1006 tap_main_t *tm = &tap_main;
1008 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1010 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
1013 return clib_error_return (0, "unknown input `%U'",
1014 format_unformat_error, input);
1021 * Set host tap/tun interface speed in Mbps.
1024 tap_set_speed (u32 hw_if_index, u32 speed)
1026 vnet_main_t *vnm = vnet_get_main ();
1027 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1028 virtio_main_t *mm = &virtio_main;
1030 int old_netns_fd = -1;
1034 struct ethtool_cmd ecmd;
1037 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
1041 old_netns_fd = clib_netns_open (NULL /* self */);
1042 if ((nfd = clib_netns_open (vif->net_ns)) == -1)
1044 clib_warning ("Cannot open netns");
1047 if (clib_setns (nfd) == -1)
1049 clib_warning ("Cannot set ns");
1054 if ((ctl_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
1056 clib_warning ("Cannot open control socket");
1060 ecmd.cmd = ETHTOOL_GSET;
1061 clib_memset (&ifr, 0, sizeof (ifr));
1062 clib_memcpy (ifr.ifr_name, vif->host_if_name,
1063 strlen ((const char *) vif->host_if_name));
1064 ifr.ifr_data = (void *) &ecmd;
1065 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1067 clib_warning ("Cannot get device settings");
1071 if (ethtool_cmd_speed (&ecmd) != speed)
1073 ecmd.cmd = ETHTOOL_SSET;
1074 ethtool_cmd_speed_set (&ecmd, speed);
1075 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1077 clib_warning ("Cannot set device settings");
1083 if (old_netns_fd != -1)
1085 if (clib_setns (old_netns_fd) == -1)
1087 clib_warning ("Cannot set old ns");
1089 close (old_netns_fd);
1099 /* tap { host-mtu <size> } configuration. */
1100 VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
1102 static clib_error_t *
1103 tap_init (vlib_main_t * vm)
1105 tap_main_t *tm = &tap_main;
1106 clib_error_t *error = 0;
1108 tm->log_default = vlib_log_register_class ("tap", 0);
1109 vlib_log_debug (tm->log_default, "initialized");
1111 tm->host_mtu_size = 0;
1116 VLIB_INIT_FUNCTION (tap_init);
1119 * fd.io coding-style-patch-verification: ON
1122 * eval: (c-set-style "gnu")