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
81 virtio_eth_set_max_frame_size (vnet_main_t *vnm, vnet_hw_interface_t *hi,
88 #define TAP_MAX_INSTANCE 1024
91 tap_free (vlib_main_t * vm, virtio_if_t * vif)
93 virtio_main_t *mm = &virtio_main;
94 tap_main_t *tm = &tap_main;
95 clib_error_t *err = 0;
98 virtio_pre_input_node_disable (vm, vif);
100 vec_foreach_index (i, vif->vhost_fds) if (vif->vhost_fds[i] != -1)
101 close (vif->vhost_fds[i]);
102 vec_foreach_index (i, vif->rxq_vrings)
103 virtio_vring_free_rx (vm, vif, RX_QUEUE (i));
104 vec_foreach_index (i, vif->txq_vrings)
105 virtio_vring_free_tx (vm, vif, TX_QUEUE (i));
109 _IOCTL (vif->tap_fds[0], TUNSETPERSIST, (void *) (uintptr_t) 0);
110 tap_log_dbg (vif, "TUNSETPERSIST: unset");
113 vec_foreach_index (i, vif->tap_fds) close (vif->tap_fds[i]);
115 vec_free (vif->tap_fds);
116 vec_free (vif->vhost_fds);
117 vec_free (vif->rxq_vrings);
118 vec_free (vif->txq_vrings);
119 vec_free (vif->host_if_name);
120 vec_free (vif->net_ns);
121 vec_free (vif->host_bridge);
122 clib_error_free (vif->error);
124 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
125 clib_memset (vif, 0, sizeof (*vif));
126 pool_put (mm->interfaces, vif);
130 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
132 vlib_thread_main_t *thm = vlib_get_thread_main ();
133 vlib_physmem_main_t *vpm = &vm->physmem_main;
134 vnet_main_t *vnm = vnet_get_main ();
135 virtio_main_t *vim = &virtio_main;
136 tap_main_t *tm = &tap_main;
137 vnet_sw_interface_t *sw;
138 vnet_hw_interface_t *hw;
139 vnet_hw_if_caps_change_t cc;
140 int i, num_vhost_queues;
141 int old_netns_fd = -1;
142 struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR };
143 struct ifreq get_ifr = {.ifr_flags = 0 };
145 vhost_memory_t *vhost_mem = 0;
146 virtio_if_t *vif = 0;
147 clib_error_t *err = 0;
148 unsigned int tap_features;
149 int tfd = -1, qfd = -1, vfd = -1, nfd = -1;
150 char *host_if_name = 0;
151 unsigned int offload = 0;
156 if (clib_bitmap_get (tm->tap_ids, args->id))
158 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
159 args->error = clib_error_return (0, "interface already exists");
165 args->id = clib_bitmap_first_clear (tm->tap_ids);
168 if (args->id > TAP_MAX_INSTANCE)
170 args->rv = VNET_API_ERROR_UNSPECIFIED;
171 args->error = clib_error_return (0, "cannot find free interface id");
175 pool_get_zero (vim->interfaces, vif);
177 if (args->tap_flags & TAP_FLAG_TUN)
179 vif->type = VIRTIO_IF_TYPE_TUN;
180 ifr.ifr_flags |= IFF_TUN;
183 * From kernel 4.20, xdp support has been added in tun_sendmsg.
184 * If sndbuf == INT_MAX, vhost batches the packet and processes
185 * them using xdp data path for tun driver. It assumes packets
186 * are ethernet frames (It needs to be fixed).
187 * To avoid xdp data path in tun driver, sndbuf value should
190 sndbuf = INT_MAX - 1;
194 vif->type = VIRTIO_IF_TYPE_TAP;
195 ifr.ifr_flags |= IFF_TAP;
199 vif->dev_instance = vif - vim->interfaces;
201 vif->num_txqs = clib_max (args->num_tx_queues, thm->n_vlib_mains);
202 vif->num_rxqs = clib_max (args->num_rx_queues, 1);
204 if (args->tap_flags & TAP_FLAG_ATTACH)
206 if (args->host_if_name == NULL)
208 args->rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
209 err = clib_error_return (0, "host_if_name is not provided");
214 /* if namespace is specified, all further netlink messages should be executed
215 * after we change our net namespace */
216 if (args->host_namespace)
218 old_netns_fd = clib_netns_open (NULL /* self */);
219 if ((nfd = clib_netns_open (args->host_namespace)) == -1)
221 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
222 args->error = clib_error_return_unix (0, "clib_netns_open '%s'",
223 args->host_namespace);
226 if (clib_setns (nfd) == -1)
228 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
230 clib_error_return_unix (0, "setns '%s'", args->host_namespace);
235 if (args->host_if_name != NULL)
237 host_if_name = (char *) args->host_if_name;
238 clib_memcpy (ifr.ifr_name, host_if_name,
239 clib_min (IFNAMSIZ, vec_len (host_if_name)));
242 if ((tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
244 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
245 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
248 vec_add1 (vif->tap_fds, tfd);
249 tap_log_dbg (vif, "open tap fd %d", tfd);
251 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
252 tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
253 if ((tap_features & IFF_VNET_HDR) == 0)
255 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
256 args->error = clib_error_return (0, "vhost-net backend not available");
260 if ((tap_features & IFF_MULTI_QUEUE) == 0)
262 if (vif->num_rxqs > 1)
264 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
265 args->error = clib_error_return (0, "multiqueue not supported");
268 vif->num_rxqs = vif->num_txqs = 1;
271 ifr.ifr_flags |= IFF_MULTI_QUEUE;
273 hdrsz = sizeof (vnet_virtio_net_hdr_v1_t);
274 if (args->tap_flags & TAP_FLAG_GSO)
276 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
277 vif->gso_enabled = 1;
279 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
281 offload = TUN_F_CSUM;
282 vif->csum_offload_enabled = 1;
285 _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
286 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
287 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
289 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
290 tap_log_dbg (vif, "ifindex %d", vif->ifindex);
292 if (!args->host_if_name)
293 host_if_name = ifr.ifr_ifrn.ifrn_name;
295 host_if_name = (char *) args->host_if_name;
298 * unset the persistence when attaching to existing
301 if (args->tap_flags & TAP_FLAG_ATTACH)
303 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 0);
304 tap_log_dbg (vif, "TUNSETPERSIST: unset");
307 /* set the persistence */
308 if (args->tap_flags & TAP_FLAG_PERSIST)
310 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 1);
311 tap_log_dbg (vif, "TUNSETPERSIST: set");
313 /* verify persistence is set, read the flags */
314 _IOCTL (tfd, TUNGETIFF, (void *) &get_ifr);
315 tap_log_dbg (vif, "TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
316 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
318 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
319 args->error = clib_error_return (0, "persistence not supported");
324 /* create additional queues on the linux side.
325 * we create as many linux queue pairs as we have rx queues
327 for (i = 1; i < vif->num_rxqs; i++)
329 if ((qfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
331 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
332 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
335 vec_add1 (vif->tap_fds, qfd);
336 _IOCTL (qfd, TUNSETIFF, (void *) &ifr);
337 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", qfd,
338 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
341 for (i = 0; i < vif->num_rxqs; i++)
343 tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
344 vif->tap_fds[i], hdrsz);
345 _IOCTL (vif->tap_fds[i], TUNSETVNETHDRSZ, &hdrsz);
347 tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", vif->tap_fds[i],
349 _IOCTL (vif->tap_fds[i], TUNSETSNDBUF, &sndbuf);
351 tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", vif->tap_fds[i],
353 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
355 if (fcntl (vif->tap_fds[i], F_SETFL, O_NONBLOCK) < 0)
357 err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
358 tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
363 /* open as many vhost-net fds as required and set ownership */
364 num_vhost_queues = clib_max (vif->num_rxqs, vif->num_txqs);
365 for (i = 0; i < num_vhost_queues; i++)
367 if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
369 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
370 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
373 vec_add1 (vif->vhost_fds, vfd);
374 virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
375 _IOCTL (vfd, VHOST_SET_OWNER, 0);
376 virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
379 _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
380 virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
381 vif->remote_features);
383 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
385 args->rv = VNET_API_ERROR_UNSUPPORTED;
386 args->error = clib_error_return (0, "vhost-net backend doesn't support "
387 "VIRTIO_NET_F_MRG_RXBUF feature");
391 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
394 args->rv = VNET_API_ERROR_UNSUPPORTED;
395 args->error = clib_error_return (0, "vhost-net backend doesn't support "
396 "VIRTIO_RING_F_INDIRECT_DESC feature");
400 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
402 args->rv = VNET_API_ERROR_UNSUPPORTED;
403 args->error = clib_error_return (0, "vhost-net backend doesn't support "
404 "VIRTIO_F_VERSION_1 features");
408 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
409 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
410 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
412 virtio_set_net_hdr_size (vif);
414 if (vif->type == VIRTIO_IF_TYPE_TAP)
416 if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes))
417 ethernet_mac_address_generate (args->host_mac_addr.bytes);
418 args->error = vnet_netlink_set_link_addr (vif->ifindex,
419 args->host_mac_addr.bytes);
422 args->rv = VNET_API_ERROR_NETLINK_ERROR;
426 if (args->host_bridge)
428 args->error = vnet_netlink_set_link_master (vif->ifindex,
433 args->rv = VNET_API_ERROR_NETLINK_ERROR;
439 if (args->host_ip4_prefix_len)
441 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
442 &args->host_ip4_addr,
443 args->host_ip4_prefix_len);
446 args->rv = VNET_API_ERROR_NETLINK_ERROR;
451 if (args->host_ip6_prefix_len)
453 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
454 &args->host_ip6_addr,
455 args->host_ip6_prefix_len);
458 args->rv = VNET_API_ERROR_NETLINK_ERROR;
463 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
466 args->rv = VNET_API_ERROR_NETLINK_ERROR;
470 if (args->host_ip4_gw_set)
472 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
475 args->rv = VNET_API_ERROR_NETLINK_ERROR;
480 if (args->host_ip6_gw_set)
482 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
485 args->rv = VNET_API_ERROR_NETLINK_ERROR;
490 if (args->host_mtu_set)
493 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
496 args->rv = VNET_API_ERROR_NETLINK_ERROR;
500 else if (tm->host_mtu_size != 0)
503 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
506 args->rv = VNET_API_ERROR_NETLINK_ERROR;
509 args->host_mtu_set = 1;
510 args->host_mtu_size = tm->host_mtu_size;
513 /* switch back to old net namespace */
514 if (args->host_namespace)
516 if (clib_setns (old_netns_fd) == -1)
518 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
519 args->error = clib_error_return_unix (0, "setns '%s'",
520 args->host_namespace);
525 for (i = 0; i < num_vhost_queues; i++)
527 if (i < vif->num_rxqs && (args->error =
528 virtio_vring_init (vm, vif, RX_QUEUE (i),
531 args->rv = VNET_API_ERROR_INIT_FAILED;
535 if (i < vif->num_txqs && (args->error =
536 virtio_vring_init (vm, vif, TX_QUEUE (i),
539 args->rv = VNET_API_ERROR_INIT_FAILED;
544 /* setup features and memtable */
545 i = sizeof (vhost_memory_t) + sizeof (vhost_memory_region_t);
546 vhost_mem = clib_mem_alloc (i);
547 clib_memset (vhost_mem, 0, i);
548 vhost_mem->nregions = 1;
549 vhost_mem->regions[0].memory_size = vpm->max_size;
550 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
551 vhost_mem->regions[0].userspace_addr =
552 vhost_mem->regions[0].guest_phys_addr;
554 for (i = 0; i < vhost_mem->nregions; i++)
555 virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
556 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
557 vhost_mem->regions[0].memory_size,
558 vhost_mem->regions[0].guest_phys_addr,
559 vhost_mem->regions[0].userspace_addr);
562 for (i = 0; i < num_vhost_queues; i++)
564 int fd = vif->vhost_fds[i];
565 _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
566 virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
568 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
569 virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
572 /* finish initializing queue pair */
573 for (i = 0; i < num_vhost_queues * 2; i++)
575 vhost_vring_addr_t addr = { 0 };
576 vhost_vring_state_t state = { 0 };
577 vhost_vring_file_t file = { 0 };
578 vnet_virtio_vring_t *vring;
580 int fd = vif->vhost_fds[qp];
584 if (qp >= vif->num_txqs)
586 vring = vec_elt_at_index (vif->txq_vrings, qp);
590 if (qp >= vif->num_rxqs)
592 vring = vec_elt_at_index (vif->rxq_vrings, qp);
595 addr.index = state.index = file.index = vring->queue_id & 1;
596 state.num = vring->queue_size;
597 virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
598 state.index, state.num);
599 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
602 addr.desc_user_addr = pointer_to_uword (vring->desc);
603 addr.avail_user_addr = pointer_to_uword (vring->avail);
604 addr.used_user_addr = pointer_to_uword (vring->used);
606 virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
607 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
608 "used_user_addr 0x%lx", fd, addr.index,
609 addr.flags, addr.desc_user_addr, addr.avail_user_addr,
610 addr.used_user_addr);
611 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
613 file.fd = vring->call_fd;
614 virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
615 fd, file.index, file.fd);
616 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
618 file.fd = vring->kick_fd;
619 virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
620 fd, file.index, file.fd);
621 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
623 file.fd = vif->tap_fds[qp % vif->num_rxqs];
624 virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
625 fd, file.index, file.fd);
626 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
629 if (vif->type == VIRTIO_IF_TYPE_TAP)
631 if (!args->mac_addr_set)
632 ethernet_mac_address_generate (args->mac_addr.bytes);
634 clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6);
635 if (args->host_bridge)
636 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
638 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
639 if (args->host_namespace)
640 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
641 vif->host_mtu_size = args->host_mtu_size;
642 vif->tap_flags = args->tap_flags;
643 clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6);
644 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
645 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
646 if (args->host_ip4_prefix_len)
647 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
648 if (args->host_ip6_prefix_len)
649 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
651 if (vif->type != VIRTIO_IF_TYPE_TUN)
653 vnet_eth_interface_registration_t eir = {};
655 eir.dev_class_index = virtio_device_class.index;
656 eir.dev_instance = vif->dev_instance;
657 eir.address = vif->mac_addr;
658 eir.cb.flag_change = virtio_eth_flag_change;
659 eir.cb.set_max_frame_size = virtio_eth_set_max_frame_size;
660 vif->hw_if_index = vnet_eth_register_interface (vnm, &eir);
664 vif->hw_if_index = vnet_register_interface
665 (vnm, virtio_device_class.index,
666 vif->dev_instance /* device instance */ ,
667 tun_device_hw_interface_class.index, vif->dev_instance);
670 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
671 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
672 vif->sw_if_index = sw->sw_if_index;
673 args->sw_if_index = vif->sw_if_index;
675 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
676 cc.mask = VNET_HW_IF_CAP_INT_MODE | VNET_HW_IF_CAP_TCP_GSO |
677 VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM;
678 cc.val = VNET_HW_IF_CAP_INT_MODE;
680 if (args->tap_flags & TAP_FLAG_GSO)
681 cc.val |= VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_TX_TCP_CKSUM |
682 VNET_HW_IF_CAP_TX_UDP_CKSUM;
683 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
684 cc.val |= VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM;
686 if ((args->tap_flags & TAP_FLAG_GSO)
687 && (args->tap_flags & TAP_FLAG_GRO_COALESCE))
689 virtio_set_packet_coalesce (vif);
691 if (vif->type == VIRTIO_IF_TYPE_TUN)
693 hw->min_frame_size = TUN_MIN_PACKET_BYTES;
694 vnet_hw_interface_set_mtu (
695 vnm, hw->hw_if_index,
696 args->host_mtu_size ? args->host_mtu_size : TUN_DEFAULT_PACKET_BYTES);
699 vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc);
700 virtio_pre_input_node_enable (vm, vif);
701 virtio_vring_set_rx_queues (vm, vif);
702 virtio_vring_set_tx_queues (vm, vif);
704 vif->per_interface_next_index = ~0;
705 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
706 VNET_HW_INTERFACE_FLAG_LINK_UP);
708 * Host tun/tap driver link carrier state is "up" at creation. The
709 * driver never changes this unless the backend (VPP) changes it using
710 * TUNSETCARRIER ioctl(). See tap_set_carrier().
712 vif->host_carrier_up = 1;
719 ASSERT (args->error == 0);
721 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
724 tap_log_err (vif, "%U", format_clib_error, args->error);
728 clib_mem_free (vhost_mem);
729 if (old_netns_fd != -1)
731 /* in case we errored with a switched netns */
732 clib_setns (old_netns_fd);
733 close (old_netns_fd);
740 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
742 vnet_main_t *vnm = vnet_get_main ();
743 virtio_main_t *mm = &virtio_main;
745 vnet_hw_interface_t *hw;
747 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
748 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
749 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
751 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
753 if ((vif->type != VIRTIO_IF_TYPE_TAP) && (vif->type != VIRTIO_IF_TYPE_TUN))
754 return VNET_API_ERROR_INVALID_INTERFACE;
756 /* bring down the interface */
757 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
758 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
760 if (vif->type == VIRTIO_IF_TYPE_TAP)
761 ethernet_delete_interface (vnm, vif->hw_if_index);
762 else /* VIRTIO_IF_TYPE_TUN */
763 vnet_delete_hw_interface (vnm, vif->hw_if_index);
764 vif->hw_if_index = ~0;
772 tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index,
775 vnet_main_t *vnm = vnet_get_main ();
776 virtio_main_t *mm = &virtio_main;
778 vnet_hw_interface_t *hw;
779 vnet_hw_if_caps_change_t cc;
780 clib_error_t *err = 0;
783 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
785 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
786 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
788 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
790 const unsigned int csum_offload_on = TUN_F_CSUM;
791 const unsigned int csum_offload_off = 0;
792 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
793 vec_foreach_index (i, vif->tap_fds)
794 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
795 vif->gso_enabled = 0;
796 vif->packet_coalesce = 0;
798 cc.mask = VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_L4_TX_CKSUM;
801 cc.val = VNET_HW_IF_CAP_L4_TX_CKSUM;
802 vif->csum_offload_enabled = 1;
807 vif->csum_offload_enabled = 0;
809 vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc);
814 clib_warning ("Error %s checksum offload on sw_if_index %d",
815 enable_disable ? "enabling" : "disabling", sw_if_index);
816 return VNET_API_ERROR_SYSCALL_ERROR_3;
822 tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable,
823 int is_packet_coalesce)
825 vnet_main_t *vnm = vnet_get_main ();
826 virtio_main_t *mm = &virtio_main;
828 vnet_hw_interface_t *hw;
829 vnet_hw_if_caps_change_t cc;
830 clib_error_t *err = 0;
833 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
835 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
836 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
838 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
840 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
841 const unsigned int gso_off = 0;
842 unsigned int offload = enable_disable ? gso_on : gso_off;
843 vec_foreach_index (i, vif->tap_fds)
844 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
846 cc.mask = VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_L4_TX_CKSUM;
851 vif->gso_enabled = 1;
852 vif->csum_offload_enabled = 1;
853 if (is_packet_coalesce)
854 virtio_set_packet_coalesce (vif);
859 vif->gso_enabled = 0;
860 vif->csum_offload_enabled = 0;
861 vif->packet_coalesce = 0;
863 vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc);
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 vnet_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;
886 pool_foreach (vif, mm->interfaces) {
887 if ((vif->type != VIRTIO_IF_TYPE_TAP)
888 && (vif->type != VIRTIO_IF_TYPE_TUN))
890 vec_add2(r_tapids, tapid, 1);
891 clib_memset (tapid, 0, sizeof (*tapid));
893 tapid->sw_if_index = vif->sw_if_index;
894 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
895 clib_memcpy(tapid->dev_name, hi->name,
896 MIN (ARRAY_LEN (tapid->dev_name) - 1, vec_len (hi->name)));
897 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
898 tapid->rx_ring_sz = vring->queue_size;
899 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
900 tapid->tx_ring_sz = vring->queue_size;
901 tapid->tap_flags = vif->tap_flags;
902 clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6);
903 if (vif->host_if_name)
905 clib_memcpy(tapid->host_if_name, vif->host_if_name,
906 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
907 vec_len (vif->host_if_name)));
911 clib_memcpy(tapid->host_namespace, vif->net_ns,
912 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
913 vec_len (vif->net_ns)));
915 if (vif->host_bridge)
917 clib_memcpy(tapid->host_bridge, vif->host_bridge,
918 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
919 vec_len (vif->host_bridge)));
921 if (vif->host_ip4_prefix_len)
922 clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4);
923 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
924 if (vif->host_ip6_prefix_len)
925 clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16);
926 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
927 tapid->host_mtu_size = vif->host_mtu_size;
930 *out_tapids = r_tapids;
936 * Set host tap/tun interface carrier state so it will appear to host
937 * applications that the interface's link state changed.
939 * If the kernel we're building against does not have support for the
940 * TUNSETCARRIER ioctl command, do nothing.
943 tap_set_carrier (u32 hw_if_index, u32 carrier_up)
947 vnet_main_t *vnm = vnet_get_main ();
948 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
949 virtio_main_t *mm = &virtio_main;
953 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
954 vec_foreach (fd, vif->tap_fds)
956 ret = ioctl (*fd, TUNSETCARRIER, &carrier_up);
959 clib_warning ("ioctl (TUNSETCARRIER) returned %d", ret);
964 vif->host_carrier_up = (carrier_up != 0);
970 static clib_error_t *
971 tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
973 tap_main_t *tm = &tap_main;
975 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
977 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
980 return clib_error_return (0, "unknown input `%U'",
981 format_unformat_error, input);
988 * Set host tap/tun interface speed in Mbps.
991 tap_set_speed (u32 hw_if_index, u32 speed)
993 vnet_main_t *vnm = vnet_get_main ();
994 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
995 virtio_main_t *mm = &virtio_main;
997 int old_netns_fd = -1;
1001 struct ethtool_cmd ecmd;
1004 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
1008 old_netns_fd = clib_netns_open (NULL /* self */);
1009 if ((nfd = clib_netns_open (vif->net_ns)) == -1)
1011 clib_warning ("Cannot open netns");
1014 if (clib_setns (nfd) == -1)
1016 clib_warning ("Cannot set ns");
1021 if ((ctl_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
1023 clib_warning ("Cannot open control socket");
1027 ecmd.cmd = ETHTOOL_GSET;
1028 clib_memset (&ifr, 0, sizeof (ifr));
1029 clib_memcpy (ifr.ifr_name, vif->host_if_name,
1030 strlen ((const char *) vif->host_if_name));
1031 ifr.ifr_data = (void *) &ecmd;
1032 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1034 clib_warning ("Cannot get device settings");
1038 if (ethtool_cmd_speed (&ecmd) != speed)
1040 ecmd.cmd = ETHTOOL_SSET;
1041 ethtool_cmd_speed_set (&ecmd, speed);
1042 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1044 clib_warning ("Cannot set device settings");
1050 if (old_netns_fd != -1)
1052 if (clib_setns (old_netns_fd) == -1)
1054 clib_warning ("Cannot set old ns");
1056 close (old_netns_fd);
1066 /* tap { host-mtu <size> } configuration. */
1067 VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
1069 static clib_error_t *
1070 tap_init (vlib_main_t * vm)
1072 tap_main_t *tm = &tap_main;
1073 clib_error_t *error = 0;
1075 tm->log_default = vlib_log_register_class ("tap", 0);
1076 vlib_log_debug (tm->log_default, "initialized");
1078 tm->host_mtu_size = 0;
1083 VLIB_INIT_FUNCTION (tap_init);
1086 * fd.io coding-style-patch-verification: ON
1089 * eval: (c-set-style "gnu")