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;
90 virtio_pre_input_node_disable (vm, vif);
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->tap_fds);
110 vec_free (vif->vhost_fds);
111 vec_free (vif->rxq_vrings);
112 vec_free (vif->txq_vrings);
113 vec_free (vif->host_if_name);
114 vec_free (vif->net_ns);
115 vec_free (vif->host_bridge);
116 clib_error_free (vif->error);
118 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
119 clib_memset (vif, 0, sizeof (*vif));
120 pool_put (mm->interfaces, vif);
124 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
126 vlib_thread_main_t *thm = vlib_get_thread_main ();
127 vlib_physmem_main_t *vpm = &vm->physmem_main;
128 vnet_main_t *vnm = vnet_get_main ();
129 virtio_main_t *vim = &virtio_main;
130 tap_main_t *tm = &tap_main;
131 vnet_sw_interface_t *sw;
132 vnet_hw_interface_t *hw;
133 vnet_hw_if_caps_change_t cc;
134 int i, num_vhost_queues;
135 int old_netns_fd = -1;
136 struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR };
137 struct ifreq get_ifr = {.ifr_flags = 0 };
139 vhost_memory_t *vhost_mem = 0;
140 virtio_if_t *vif = 0;
141 clib_error_t *err = 0;
142 unsigned int tap_features;
143 int tfd = -1, qfd = -1, vfd = -1, nfd = -1;
144 char *host_if_name = 0;
145 unsigned int offload = 0;
150 if (clib_bitmap_get (tm->tap_ids, args->id))
152 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
153 args->error = clib_error_return (0, "interface already exists");
159 args->id = clib_bitmap_first_clear (tm->tap_ids);
162 if (args->id > TAP_MAX_INSTANCE)
164 args->rv = VNET_API_ERROR_UNSPECIFIED;
165 args->error = clib_error_return (0, "cannot find free interface id");
169 pool_get_zero (vim->interfaces, vif);
171 if (args->tap_flags & TAP_FLAG_TUN)
173 vif->type = VIRTIO_IF_TYPE_TUN;
174 ifr.ifr_flags |= IFF_TUN;
177 * From kernel 4.20, xdp support has been added in tun_sendmsg.
178 * If sndbuf == INT_MAX, vhost batches the packet and processes
179 * them using xdp data path for tun driver. It assumes packets
180 * are ethernet frames (It needs to be fixed).
181 * To avoid xdp data path in tun driver, sndbuf value should
184 sndbuf = INT_MAX - 1;
188 vif->type = VIRTIO_IF_TYPE_TAP;
189 ifr.ifr_flags |= IFF_TAP;
193 vif->dev_instance = vif - vim->interfaces;
195 vif->num_txqs = clib_max (args->num_tx_queues, thm->n_vlib_mains);
196 vif->num_rxqs = clib_max (args->num_rx_queues, 1);
198 if (args->tap_flags & TAP_FLAG_ATTACH)
200 if (args->host_if_name == NULL)
202 args->rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
203 err = clib_error_return (0, "host_if_name is not provided");
208 /* if namespace is specified, all further netlink messages should be executed
209 * after we change our net namespace */
210 if (args->host_namespace)
212 old_netns_fd = clib_netns_open (NULL /* self */);
213 if ((nfd = clib_netns_open (args->host_namespace)) == -1)
215 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
216 args->error = clib_error_return_unix (0, "clib_netns_open '%s'",
217 args->host_namespace);
220 if (clib_setns (nfd) == -1)
222 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
224 clib_error_return_unix (0, "setns '%s'", args->host_namespace);
229 if (args->host_if_name != NULL)
231 host_if_name = (char *) args->host_if_name;
232 clib_memcpy (ifr.ifr_name, host_if_name,
233 clib_min (IFNAMSIZ, vec_len (host_if_name)));
236 if ((tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
238 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
239 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
242 vec_add1 (vif->tap_fds, tfd);
243 tap_log_dbg (vif, "open tap fd %d", tfd);
245 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
246 tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
247 if ((tap_features & IFF_VNET_HDR) == 0)
249 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
250 args->error = clib_error_return (0, "vhost-net backend not available");
254 if ((tap_features & IFF_MULTI_QUEUE) == 0)
256 if (vif->num_rxqs > 1)
258 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
259 args->error = clib_error_return (0, "multiqueue not supported");
262 vif->num_rxqs = vif->num_txqs = 1;
265 ifr.ifr_flags |= IFF_MULTI_QUEUE;
267 hdrsz = sizeof (virtio_net_hdr_v1_t);
268 if (args->tap_flags & TAP_FLAG_GSO)
270 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
271 vif->gso_enabled = 1;
273 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
275 offload = TUN_F_CSUM;
276 vif->csum_offload_enabled = 1;
279 _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
280 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
281 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
283 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
284 tap_log_dbg (vif, "ifindex %d", vif->ifindex);
286 if (!args->host_if_name)
287 host_if_name = ifr.ifr_ifrn.ifrn_name;
289 host_if_name = (char *) args->host_if_name;
292 * unset the persistence when attaching to existing
295 if (args->tap_flags & TAP_FLAG_ATTACH)
297 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 0);
298 tap_log_dbg (vif, "TUNSETPERSIST: unset");
301 /* set the persistence */
302 if (args->tap_flags & TAP_FLAG_PERSIST)
304 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 1);
305 tap_log_dbg (vif, "TUNSETPERSIST: set");
307 /* verify persistence is set, read the flags */
308 _IOCTL (tfd, TUNGETIFF, (void *) &get_ifr);
309 tap_log_dbg (vif, "TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
310 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
312 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
313 args->error = clib_error_return (0, "persistence not supported");
318 /* create additional queues on the linux side.
319 * we create as many linux queue pairs as we have rx queues
321 for (i = 1; i < vif->num_rxqs; i++)
323 if ((qfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
325 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
326 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
329 _IOCTL (qfd, TUNSETIFF, (void *) &ifr);
330 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", qfd,
331 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
332 vec_add1 (vif->tap_fds, qfd);
335 for (i = 0; i < vif->num_rxqs; i++)
337 tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
338 vif->tap_fds[i], hdrsz);
339 _IOCTL (vif->tap_fds[i], TUNSETVNETHDRSZ, &hdrsz);
341 tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", vif->tap_fds[i],
343 _IOCTL (vif->tap_fds[i], TUNSETSNDBUF, &sndbuf);
345 tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", vif->tap_fds[i],
347 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
349 if (fcntl (vif->tap_fds[i], F_SETFL, O_NONBLOCK) < 0)
351 err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
352 tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
357 /* open as many vhost-net fds as required and set ownership */
358 num_vhost_queues = clib_max (vif->num_rxqs, vif->num_txqs);
359 for (i = 0; i < num_vhost_queues; i++)
361 if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
363 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
364 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
367 vec_add1 (vif->vhost_fds, vfd);
368 virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
369 _IOCTL (vfd, VHOST_SET_OWNER, 0);
370 virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
373 _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
374 virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
375 vif->remote_features);
377 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
379 args->rv = VNET_API_ERROR_UNSUPPORTED;
380 args->error = clib_error_return (0, "vhost-net backend doesn't support "
381 "VIRTIO_NET_F_MRG_RXBUF feature");
385 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
388 args->rv = VNET_API_ERROR_UNSUPPORTED;
389 args->error = clib_error_return (0, "vhost-net backend doesn't support "
390 "VIRTIO_RING_F_INDIRECT_DESC feature");
394 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
396 args->rv = VNET_API_ERROR_UNSUPPORTED;
397 args->error = clib_error_return (0, "vhost-net backend doesn't support "
398 "VIRTIO_F_VERSION_1 features");
402 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
403 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
404 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
406 virtio_set_net_hdr_size (vif);
408 if (vif->type == VIRTIO_IF_TYPE_TAP)
410 if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes))
411 ethernet_mac_address_generate (args->host_mac_addr.bytes);
412 args->error = vnet_netlink_set_link_addr (vif->ifindex,
413 args->host_mac_addr.bytes);
416 args->rv = VNET_API_ERROR_NETLINK_ERROR;
420 if (args->host_bridge)
422 args->error = vnet_netlink_set_link_master (vif->ifindex,
427 args->rv = VNET_API_ERROR_NETLINK_ERROR;
433 if (args->host_ip4_prefix_len)
435 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
436 &args->host_ip4_addr,
437 args->host_ip4_prefix_len);
440 args->rv = VNET_API_ERROR_NETLINK_ERROR;
445 if (args->host_ip6_prefix_len)
447 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
448 &args->host_ip6_addr,
449 args->host_ip6_prefix_len);
452 args->rv = VNET_API_ERROR_NETLINK_ERROR;
457 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
460 args->rv = VNET_API_ERROR_NETLINK_ERROR;
464 if (args->host_ip4_gw_set)
466 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
469 args->rv = VNET_API_ERROR_NETLINK_ERROR;
474 if (args->host_ip6_gw_set)
476 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
479 args->rv = VNET_API_ERROR_NETLINK_ERROR;
484 if (args->host_mtu_set)
487 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
490 args->rv = VNET_API_ERROR_NETLINK_ERROR;
494 else if (tm->host_mtu_size != 0)
497 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
500 args->rv = VNET_API_ERROR_NETLINK_ERROR;
503 args->host_mtu_set = 1;
504 args->host_mtu_size = tm->host_mtu_size;
507 /* switch back to old net namespace */
508 if (args->host_namespace)
510 if (clib_setns (old_netns_fd) == -1)
512 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
513 args->error = clib_error_return_unix (0, "setns '%s'",
514 args->host_namespace);
519 for (i = 0; i < num_vhost_queues; i++)
521 if (i < vif->num_rxqs && (args->error =
522 virtio_vring_init (vm, vif, RX_QUEUE (i),
525 args->rv = VNET_API_ERROR_INIT_FAILED;
529 if (i < vif->num_txqs && (args->error =
530 virtio_vring_init (vm, vif, TX_QUEUE (i),
533 args->rv = VNET_API_ERROR_INIT_FAILED;
538 /* setup features and memtable */
539 i = sizeof (vhost_memory_t) + sizeof (vhost_memory_region_t);
540 vhost_mem = clib_mem_alloc (i);
541 clib_memset (vhost_mem, 0, i);
542 vhost_mem->nregions = 1;
543 vhost_mem->regions[0].memory_size = vpm->max_size;
544 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
545 vhost_mem->regions[0].userspace_addr =
546 vhost_mem->regions[0].guest_phys_addr;
548 for (i = 0; i < vhost_mem->nregions; i++)
549 virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
550 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
551 vhost_mem->regions[0].memory_size,
552 vhost_mem->regions[0].guest_phys_addr,
553 vhost_mem->regions[0].userspace_addr);
556 for (i = 0; i < num_vhost_queues; i++)
558 int fd = vif->vhost_fds[i];
559 _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
560 virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
562 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
563 virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
566 /* finish initializing queue pair */
567 for (i = 0; i < num_vhost_queues * 2; i++)
569 vhost_vring_addr_t addr = { 0 };
570 vhost_vring_state_t state = { 0 };
571 vhost_vring_file_t file = { 0 };
572 virtio_vring_t *vring;
574 int fd = vif->vhost_fds[qp];
578 if (qp >= vif->num_txqs)
580 vring = vec_elt_at_index (vif->txq_vrings, qp);
584 if (qp >= vif->num_rxqs)
586 vring = vec_elt_at_index (vif->rxq_vrings, qp);
589 addr.index = state.index = file.index = vring->queue_id & 1;
590 state.num = vring->size;
591 virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
592 state.index, state.num);
593 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
596 addr.desc_user_addr = pointer_to_uword (vring->desc);
597 addr.avail_user_addr = pointer_to_uword (vring->avail);
598 addr.used_user_addr = pointer_to_uword (vring->used);
600 virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
601 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
602 "used_user_addr 0x%lx", fd, addr.index,
603 addr.flags, addr.desc_user_addr, addr.avail_user_addr,
604 addr.used_user_addr);
605 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
607 file.fd = vring->call_fd;
608 virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
609 fd, file.index, file.fd);
610 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
612 file.fd = vring->kick_fd;
613 virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
614 fd, file.index, file.fd);
615 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
617 file.fd = vif->tap_fds[qp % vif->num_rxqs];
618 virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
619 fd, file.index, file.fd);
620 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
623 if (vif->type == VIRTIO_IF_TYPE_TAP)
625 if (!args->mac_addr_set)
626 ethernet_mac_address_generate (args->mac_addr.bytes);
628 clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6);
629 if (args->host_bridge)
630 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
632 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
633 if (args->host_namespace)
634 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
635 vif->host_mtu_size = args->host_mtu_size;
636 vif->tap_flags = args->tap_flags;
637 clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6);
638 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
639 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
640 if (args->host_ip4_prefix_len)
641 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
642 if (args->host_ip6_prefix_len)
643 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
645 if (vif->type != VIRTIO_IF_TYPE_TUN)
647 vnet_eth_interface_registration_t eir = {};
649 eir.dev_class_index = virtio_device_class.index;
650 eir.dev_instance = vif->dev_instance;
651 eir.address = vif->mac_addr;
652 eir.cb.flag_change = virtio_eth_flag_change;
653 vif->hw_if_index = vnet_eth_register_interface (vnm, &eir);
657 vif->hw_if_index = vnet_register_interface
658 (vnm, virtio_device_class.index,
659 vif->dev_instance /* device instance */ ,
660 tun_device_hw_interface_class.index, vif->dev_instance);
663 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
664 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
665 vif->sw_if_index = sw->sw_if_index;
666 args->sw_if_index = vif->sw_if_index;
668 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
669 cc.mask = VNET_HW_IF_CAP_INT_MODE | VNET_HW_IF_CAP_TCP_GSO |
670 VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM;
671 cc.val = VNET_HW_IF_CAP_INT_MODE;
673 if (args->tap_flags & TAP_FLAG_GSO)
674 cc.val |= VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_TX_TCP_CKSUM |
675 VNET_HW_IF_CAP_TX_UDP_CKSUM;
676 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
677 cc.val |= VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM;
679 if ((args->tap_flags & TAP_FLAG_GSO)
680 && (args->tap_flags & TAP_FLAG_GRO_COALESCE))
682 virtio_set_packet_coalesce (vif);
684 if (vif->type == VIRTIO_IF_TYPE_TUN)
686 hw->min_frame_size = TUN_MIN_PACKET_BYTES;
687 vnet_hw_interface_set_mtu (
688 vnm, hw->sw_if_index,
689 args->host_mtu_size ? args->host_mtu_size : TUN_DEFAULT_PACKET_BYTES);
692 vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc);
693 virtio_pre_input_node_enable (vm, vif);
694 virtio_vring_set_rx_queues (vm, vif);
695 virtio_vring_set_tx_queues (vm, vif);
697 vif->per_interface_next_index = ~0;
698 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
699 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
700 VNET_HW_INTERFACE_FLAG_LINK_UP);
702 * Host tun/tap driver link carrier state is "up" at creation. The
703 * driver never changes this unless the backend (VPP) changes it using
704 * TUNSETCARRIER ioctl(). See tap_set_carrier().
706 vif->host_carrier_up = 1;
713 ASSERT (args->error == 0);
715 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
718 tap_log_err (vif, "%U", format_clib_error, args->error);
722 clib_mem_free (vhost_mem);
723 if (old_netns_fd != -1)
725 /* in case we errored with a switched netns */
726 clib_setns (old_netns_fd);
727 close (old_netns_fd);
734 tap_delete_if (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;
741 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
742 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
743 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
745 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
747 if ((vif->type != VIRTIO_IF_TYPE_TAP) && (vif->type != VIRTIO_IF_TYPE_TUN))
748 return VNET_API_ERROR_INVALID_INTERFACE;
750 /* bring down the interface */
751 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
752 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
754 if (vif->type == VIRTIO_IF_TYPE_TAP)
755 ethernet_delete_interface (vnm, vif->hw_if_index);
756 else /* VIRTIO_IF_TYPE_TUN */
757 vnet_delete_hw_interface (vnm, vif->hw_if_index);
758 vif->hw_if_index = ~0;
766 tap_csum_offload_enable_disable (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;
773 vnet_hw_if_caps_change_t cc;
774 clib_error_t *err = 0;
777 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
779 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
780 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
782 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
784 const unsigned int csum_offload_on = TUN_F_CSUM;
785 const unsigned int csum_offload_off = 0;
786 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
787 vec_foreach_index (i, vif->tap_fds)
788 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
789 vif->gso_enabled = 0;
790 vif->packet_coalesce = 0;
792 cc.mask = VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_L4_TX_CKSUM;
795 cc.val = VNET_HW_IF_CAP_L4_TX_CKSUM;
796 vif->csum_offload_enabled = 1;
801 vif->csum_offload_enabled = 0;
803 vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc);
808 clib_warning ("Error %s checksum offload on sw_if_index %d",
809 enable_disable ? "enabling" : "disabling", sw_if_index);
810 return VNET_API_ERROR_SYSCALL_ERROR_3;
816 tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable,
817 int is_packet_coalesce)
819 vnet_main_t *vnm = vnet_get_main ();
820 virtio_main_t *mm = &virtio_main;
822 vnet_hw_interface_t *hw;
823 vnet_hw_if_caps_change_t cc;
824 clib_error_t *err = 0;
827 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
829 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
830 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
832 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
834 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
835 const unsigned int gso_off = 0;
836 unsigned int offload = enable_disable ? gso_on : gso_off;
837 vec_foreach_index (i, vif->tap_fds)
838 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
840 cc.mask = VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_L4_TX_CKSUM;
845 vif->gso_enabled = 1;
846 vif->csum_offload_enabled = 1;
847 if (is_packet_coalesce)
848 virtio_set_packet_coalesce (vif);
853 vif->gso_enabled = 0;
854 vif->csum_offload_enabled = 0;
855 vif->packet_coalesce = 0;
857 vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc);
862 clib_warning ("Error %s gso on sw_if_index %d",
863 enable_disable ? "enabling" : "disabling", sw_if_index);
864 return VNET_API_ERROR_SYSCALL_ERROR_3;
870 tap_dump_ifs (tap_interface_details_t ** out_tapids)
872 vnet_main_t *vnm = vnet_get_main ();
873 virtio_main_t *mm = &virtio_main;
875 virtio_vring_t *vring;
876 vnet_hw_interface_t *hi;
877 tap_interface_details_t *r_tapids = NULL;
878 tap_interface_details_t *tapid = NULL;
881 pool_foreach (vif, mm->interfaces) {
882 if ((vif->type != VIRTIO_IF_TYPE_TAP)
883 && (vif->type != VIRTIO_IF_TYPE_TUN))
885 vec_add2(r_tapids, tapid, 1);
886 clib_memset (tapid, 0, sizeof (*tapid));
888 tapid->sw_if_index = vif->sw_if_index;
889 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
890 clib_memcpy(tapid->dev_name, hi->name,
891 MIN (ARRAY_LEN (tapid->dev_name) - 1, vec_len (hi->name)));
892 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
893 tapid->rx_ring_sz = vring->size;
894 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
895 tapid->tx_ring_sz = vring->size;
896 tapid->tap_flags = vif->tap_flags;
897 clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6);
898 if (vif->host_if_name)
900 clib_memcpy(tapid->host_if_name, vif->host_if_name,
901 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
902 vec_len (vif->host_if_name)));
906 clib_memcpy(tapid->host_namespace, vif->net_ns,
907 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
908 vec_len (vif->net_ns)));
910 if (vif->host_bridge)
912 clib_memcpy(tapid->host_bridge, vif->host_bridge,
913 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
914 vec_len (vif->host_bridge)));
916 if (vif->host_ip4_prefix_len)
917 clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4);
918 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
919 if (vif->host_ip6_prefix_len)
920 clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16);
921 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
922 tapid->host_mtu_size = vif->host_mtu_size;
926 *out_tapids = r_tapids;
932 * Set host tap/tun interface carrier state so it will appear to host
933 * applications that the interface's link state changed.
935 * If the kernel we're building against does not have support for the
936 * TUNSETCARRIER ioctl command, do nothing.
939 tap_set_carrier (u32 hw_if_index, u32 carrier_up)
943 vnet_main_t *vnm = vnet_get_main ();
944 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
945 virtio_main_t *mm = &virtio_main;
949 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
950 vec_foreach (fd, vif->tap_fds)
952 ret = ioctl (*fd, TUNSETCARRIER, &carrier_up);
955 clib_warning ("ioctl (TUNSETCARRIER) returned %d", ret);
960 vif->host_carrier_up = (carrier_up != 0);
966 static clib_error_t *
967 tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
969 tap_main_t *tm = &tap_main;
971 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
973 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
976 return clib_error_return (0, "unknown input `%U'",
977 format_unformat_error, input);
984 * Set host tap/tun interface speed in Mbps.
987 tap_set_speed (u32 hw_if_index, u32 speed)
989 vnet_main_t *vnm = vnet_get_main ();
990 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
991 virtio_main_t *mm = &virtio_main;
993 int old_netns_fd = -1;
997 struct ethtool_cmd ecmd;
1000 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
1004 old_netns_fd = clib_netns_open (NULL /* self */);
1005 if ((nfd = clib_netns_open (vif->net_ns)) == -1)
1007 clib_warning ("Cannot open netns");
1010 if (clib_setns (nfd) == -1)
1012 clib_warning ("Cannot set ns");
1017 if ((ctl_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
1019 clib_warning ("Cannot open control socket");
1023 ecmd.cmd = ETHTOOL_GSET;
1024 clib_memset (&ifr, 0, sizeof (ifr));
1025 clib_memcpy (ifr.ifr_name, vif->host_if_name,
1026 strlen ((const char *) vif->host_if_name));
1027 ifr.ifr_data = (void *) &ecmd;
1028 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1030 clib_warning ("Cannot get device settings");
1034 if (ethtool_cmd_speed (&ecmd) != speed)
1036 ecmd.cmd = ETHTOOL_SSET;
1037 ethtool_cmd_speed_set (&ecmd, speed);
1038 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1040 clib_warning ("Cannot set device settings");
1046 if (old_netns_fd != -1)
1048 if (clib_setns (old_netns_fd) == -1)
1050 clib_warning ("Cannot set old ns");
1052 close (old_netns_fd);
1062 /* tap { host-mtu <size> } configuration. */
1063 VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
1065 static clib_error_t *
1066 tap_init (vlib_main_t * vm)
1068 tap_main_t *tm = &tap_main;
1069 clib_error_t *error = 0;
1071 tm->log_default = vlib_log_register_class ("tap", 0);
1072 vlib_log_debug (tm->log_default, "initialized");
1074 tm->host_mtu_size = 0;
1079 VLIB_INIT_FUNCTION (tap_init);
1082 * fd.io coding-style-patch-verification: ON
1085 * eval: (c-set-style "gnu")