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);
101 vec_foreach_index (i, vif->vhost_fds) if (vif->vhost_fds[i] != -1)
102 close (vif->vhost_fds[i]);
103 vec_foreach_index (i, vif->rxq_vrings)
104 virtio_vring_free_rx (vm, vif, RX_QUEUE (i));
105 vec_foreach_index (i, vif->txq_vrings)
106 virtio_vring_free_tx (vm, vif, TX_QUEUE (i));
111 _IOCTL (vif->tap_fds[0], TUNSETPERSIST, (void *) (uintptr_t) 0);
112 tap_log_dbg (vif, "TUNSETPERSIST: unset");
115 vec_foreach_index (i, vif->tap_fds) close (vif->tap_fds[i]);
117 vec_free (vif->tap_fds);
118 vec_free (vif->vhost_fds);
119 vec_free (vif->rxq_vrings);
120 vec_free (vif->txq_vrings);
121 vec_free (vif->host_if_name);
122 vec_free (vif->net_ns);
123 vec_free (vif->host_bridge);
124 clib_error_free (vif->error);
126 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
127 clib_memset (vif, 0, sizeof (*vif));
128 pool_put (mm->interfaces, vif);
132 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
134 vlib_thread_main_t *thm = vlib_get_thread_main ();
135 vlib_physmem_main_t *vpm = &vm->physmem_main;
136 vnet_main_t *vnm = vnet_get_main ();
137 virtio_main_t *vim = &virtio_main;
138 tap_main_t *tm = &tap_main;
139 vnet_sw_interface_t *sw;
140 vnet_hw_interface_t *hw;
141 vnet_hw_if_caps_change_t cc;
142 int i, num_vhost_queues;
143 int old_netns_fd = -1;
144 struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR };
145 struct ifreq get_ifr = {.ifr_flags = 0 };
147 vhost_memory_t *vhost_mem = 0;
148 virtio_if_t *vif = 0;
149 clib_error_t *err = 0;
150 unsigned int tap_features;
151 int tfd = -1, qfd = -1, vfd = -1, nfd = -1;
152 char *host_if_name = 0;
153 unsigned int offload = 0;
158 if (clib_bitmap_get (tm->tap_ids, args->id))
160 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
161 args->error = clib_error_return (0, "interface already exists");
167 args->id = clib_bitmap_first_clear (tm->tap_ids);
170 if (args->id > TAP_MAX_INSTANCE)
172 args->rv = VNET_API_ERROR_UNSPECIFIED;
173 args->error = clib_error_return (0, "cannot find free interface id");
177 pool_get_zero (vim->interfaces, vif);
179 if (args->tap_flags & TAP_FLAG_TUN)
181 vif->type = VIRTIO_IF_TYPE_TUN;
182 ifr.ifr_flags |= IFF_TUN;
185 * From kernel 4.20, xdp support has been added in tun_sendmsg.
186 * If sndbuf == INT_MAX, vhost batches the packet and processes
187 * them using xdp data path for tun driver. It assumes packets
188 * are ethernet frames (It needs to be fixed).
189 * To avoid xdp data path in tun driver, sndbuf value should
192 sndbuf = INT_MAX - 1;
196 vif->type = VIRTIO_IF_TYPE_TAP;
197 ifr.ifr_flags |= IFF_TAP;
201 vif->dev_instance = vif - vim->interfaces;
203 vif->num_txqs = clib_max (args->num_tx_queues, thm->n_vlib_mains);
204 vif->num_rxqs = clib_max (args->num_rx_queues, 1);
206 if (args->tap_flags & TAP_FLAG_ATTACH)
208 if (args->host_if_name == NULL)
210 args->rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
211 err = clib_error_return (0, "host_if_name is not provided");
216 /* if namespace is specified, all further netlink messages should be executed
217 * after we change our net namespace */
218 if (args->host_namespace)
220 old_netns_fd = clib_netns_open (NULL /* self */);
221 if ((nfd = clib_netns_open (args->host_namespace)) == -1)
223 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
224 args->error = clib_error_return_unix (0, "clib_netns_open '%s'",
225 args->host_namespace);
228 if (clib_setns (nfd) == -1)
230 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
232 clib_error_return_unix (0, "setns '%s'", args->host_namespace);
237 if (args->host_if_name != NULL)
239 host_if_name = (char *) args->host_if_name;
240 clib_memcpy (ifr.ifr_name, host_if_name,
241 clib_min (IFNAMSIZ, vec_len (host_if_name)));
244 if ((tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
246 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
247 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
250 vec_add1 (vif->tap_fds, tfd);
251 tap_log_dbg (vif, "open tap fd %d", tfd);
253 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
254 tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
255 if ((tap_features & IFF_VNET_HDR) == 0)
257 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
258 args->error = clib_error_return (0, "vhost-net backend not available");
262 if ((tap_features & IFF_MULTI_QUEUE) == 0)
264 if (vif->num_rxqs > 1)
266 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
267 args->error = clib_error_return (0, "multiqueue not supported");
270 vif->num_rxqs = vif->num_txqs = 1;
273 ifr.ifr_flags |= IFF_MULTI_QUEUE;
275 hdrsz = sizeof (vnet_virtio_net_hdr_v1_t);
276 if (args->tap_flags & TAP_FLAG_GSO)
278 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
279 vif->gso_enabled = 1;
281 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
283 offload = TUN_F_CSUM;
284 vif->csum_offload_enabled = 1;
287 _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
288 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
289 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
291 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
292 tap_log_dbg (vif, "ifindex %d", vif->ifindex);
294 if (!args->host_if_name)
295 host_if_name = ifr.ifr_ifrn.ifrn_name;
297 host_if_name = (char *) args->host_if_name;
300 * unset the persistence when attaching to existing
303 if (args->tap_flags & TAP_FLAG_ATTACH)
305 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 0);
306 tap_log_dbg (vif, "TUNSETPERSIST: unset");
309 /* set the persistence */
310 if (args->tap_flags & TAP_FLAG_PERSIST)
312 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 1);
313 tap_log_dbg (vif, "TUNSETPERSIST: set");
315 /* verify persistence is set, read the flags */
316 _IOCTL (tfd, TUNGETIFF, (void *) &get_ifr);
317 tap_log_dbg (vif, "TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
318 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
320 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
321 args->error = clib_error_return (0, "persistence not supported");
326 /* create additional queues on the linux side.
327 * we create as many linux queue pairs as we have rx queues
329 for (i = 1; i < vif->num_rxqs; i++)
331 if ((qfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
333 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
334 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
337 _IOCTL (qfd, TUNSETIFF, (void *) &ifr);
338 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", qfd,
339 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
340 vec_add1 (vif->tap_fds, qfd);
343 for (i = 0; i < vif->num_rxqs; i++)
345 tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
346 vif->tap_fds[i], hdrsz);
347 _IOCTL (vif->tap_fds[i], TUNSETVNETHDRSZ, &hdrsz);
349 tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", vif->tap_fds[i],
351 _IOCTL (vif->tap_fds[i], TUNSETSNDBUF, &sndbuf);
353 tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", vif->tap_fds[i],
355 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
357 if (fcntl (vif->tap_fds[i], F_SETFL, O_NONBLOCK) < 0)
359 err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
360 tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
365 /* open as many vhost-net fds as required and set ownership */
366 num_vhost_queues = clib_max (vif->num_rxqs, vif->num_txqs);
367 for (i = 0; i < num_vhost_queues; i++)
369 if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
371 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
372 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
375 vec_add1 (vif->vhost_fds, vfd);
376 virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
377 _IOCTL (vfd, VHOST_SET_OWNER, 0);
378 virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
381 _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
382 virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
383 vif->remote_features);
385 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
387 args->rv = VNET_API_ERROR_UNSUPPORTED;
388 args->error = clib_error_return (0, "vhost-net backend doesn't support "
389 "VIRTIO_NET_F_MRG_RXBUF feature");
393 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
396 args->rv = VNET_API_ERROR_UNSUPPORTED;
397 args->error = clib_error_return (0, "vhost-net backend doesn't support "
398 "VIRTIO_RING_F_INDIRECT_DESC feature");
402 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
404 args->rv = VNET_API_ERROR_UNSUPPORTED;
405 args->error = clib_error_return (0, "vhost-net backend doesn't support "
406 "VIRTIO_F_VERSION_1 features");
410 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
411 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
412 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
414 virtio_set_net_hdr_size (vif);
416 if (vif->type == VIRTIO_IF_TYPE_TAP)
418 if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes))
419 ethernet_mac_address_generate (args->host_mac_addr.bytes);
420 args->error = vnet_netlink_set_link_addr (vif->ifindex,
421 args->host_mac_addr.bytes);
424 args->rv = VNET_API_ERROR_NETLINK_ERROR;
428 if (args->host_bridge)
430 args->error = vnet_netlink_set_link_master (vif->ifindex,
435 args->rv = VNET_API_ERROR_NETLINK_ERROR;
441 if (args->host_ip4_prefix_len)
443 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
444 &args->host_ip4_addr,
445 args->host_ip4_prefix_len);
448 args->rv = VNET_API_ERROR_NETLINK_ERROR;
453 if (args->host_ip6_prefix_len)
455 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
456 &args->host_ip6_addr,
457 args->host_ip6_prefix_len);
460 args->rv = VNET_API_ERROR_NETLINK_ERROR;
465 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
468 args->rv = VNET_API_ERROR_NETLINK_ERROR;
472 if (args->host_ip4_gw_set)
474 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
477 args->rv = VNET_API_ERROR_NETLINK_ERROR;
482 if (args->host_ip6_gw_set)
484 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
487 args->rv = VNET_API_ERROR_NETLINK_ERROR;
492 if (args->host_mtu_set)
495 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
498 args->rv = VNET_API_ERROR_NETLINK_ERROR;
502 else if (tm->host_mtu_size != 0)
505 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
508 args->rv = VNET_API_ERROR_NETLINK_ERROR;
511 args->host_mtu_set = 1;
512 args->host_mtu_size = tm->host_mtu_size;
515 /* switch back to old net namespace */
516 if (args->host_namespace)
518 if (clib_setns (old_netns_fd) == -1)
520 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
521 args->error = clib_error_return_unix (0, "setns '%s'",
522 args->host_namespace);
527 for (i = 0; i < num_vhost_queues; i++)
529 if (i < vif->num_rxqs && (args->error =
530 virtio_vring_init (vm, vif, RX_QUEUE (i),
533 args->rv = VNET_API_ERROR_INIT_FAILED;
537 if (i < vif->num_txqs && (args->error =
538 virtio_vring_init (vm, vif, TX_QUEUE (i),
541 args->rv = VNET_API_ERROR_INIT_FAILED;
546 /* setup features and memtable */
547 i = sizeof (vhost_memory_t) + sizeof (vhost_memory_region_t);
548 vhost_mem = clib_mem_alloc (i);
549 clib_memset (vhost_mem, 0, i);
550 vhost_mem->nregions = 1;
551 vhost_mem->regions[0].memory_size = vpm->max_size;
552 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
553 vhost_mem->regions[0].userspace_addr =
554 vhost_mem->regions[0].guest_phys_addr;
556 for (i = 0; i < vhost_mem->nregions; i++)
557 virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
558 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
559 vhost_mem->regions[0].memory_size,
560 vhost_mem->regions[0].guest_phys_addr,
561 vhost_mem->regions[0].userspace_addr);
564 for (i = 0; i < num_vhost_queues; i++)
566 int fd = vif->vhost_fds[i];
567 _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
568 virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
570 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
571 virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
574 /* finish initializing queue pair */
575 for (i = 0; i < num_vhost_queues * 2; i++)
577 vhost_vring_addr_t addr = { 0 };
578 vhost_vring_state_t state = { 0 };
579 vhost_vring_file_t file = { 0 };
580 vnet_virtio_vring_t *vring;
582 int fd = vif->vhost_fds[qp];
586 if (qp >= vif->num_txqs)
588 vring = vec_elt_at_index (vif->txq_vrings, qp);
592 if (qp >= vif->num_rxqs)
594 vring = vec_elt_at_index (vif->rxq_vrings, qp);
597 addr.index = state.index = file.index = vring->queue_id & 1;
598 state.num = vring->queue_size;
599 virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
600 state.index, state.num);
601 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
604 addr.desc_user_addr = pointer_to_uword (vring->desc);
605 addr.avail_user_addr = pointer_to_uword (vring->avail);
606 addr.used_user_addr = pointer_to_uword (vring->used);
608 virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
609 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
610 "used_user_addr 0x%lx", fd, addr.index,
611 addr.flags, addr.desc_user_addr, addr.avail_user_addr,
612 addr.used_user_addr);
613 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
615 file.fd = vring->call_fd;
616 virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
617 fd, file.index, file.fd);
618 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
620 file.fd = vring->kick_fd;
621 virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
622 fd, file.index, file.fd);
623 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
625 file.fd = vif->tap_fds[qp % vif->num_rxqs];
626 virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
627 fd, file.index, file.fd);
628 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
631 if (vif->type == VIRTIO_IF_TYPE_TAP)
633 if (!args->mac_addr_set)
634 ethernet_mac_address_generate (args->mac_addr.bytes);
636 clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6);
637 if (args->host_bridge)
638 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
640 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
641 if (args->host_namespace)
642 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
643 vif->host_mtu_size = args->host_mtu_size;
644 vif->tap_flags = args->tap_flags;
645 clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6);
646 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
647 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
648 if (args->host_ip4_prefix_len)
649 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
650 if (args->host_ip6_prefix_len)
651 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
653 if (vif->type != VIRTIO_IF_TYPE_TUN)
655 vnet_eth_interface_registration_t eir = {};
657 eir.dev_class_index = virtio_device_class.index;
658 eir.dev_instance = vif->dev_instance;
659 eir.address = vif->mac_addr;
660 eir.cb.flag_change = virtio_eth_flag_change;
661 eir.cb.set_max_frame_size = virtio_eth_set_max_frame_size;
662 vif->hw_if_index = vnet_eth_register_interface (vnm, &eir);
666 vif->hw_if_index = vnet_register_interface
667 (vnm, virtio_device_class.index,
668 vif->dev_instance /* device instance */ ,
669 tun_device_hw_interface_class.index, vif->dev_instance);
672 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
673 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
674 vif->sw_if_index = sw->sw_if_index;
675 args->sw_if_index = vif->sw_if_index;
677 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
678 cc.mask = VNET_HW_IF_CAP_INT_MODE | VNET_HW_IF_CAP_TCP_GSO |
679 VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM;
680 cc.val = VNET_HW_IF_CAP_INT_MODE;
682 if (args->tap_flags & TAP_FLAG_GSO)
683 cc.val |= VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_TX_TCP_CKSUM |
684 VNET_HW_IF_CAP_TX_UDP_CKSUM;
685 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
686 cc.val |= VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM;
688 if ((args->tap_flags & TAP_FLAG_GSO)
689 && (args->tap_flags & TAP_FLAG_GRO_COALESCE))
691 virtio_set_packet_coalesce (vif);
693 if (vif->type == VIRTIO_IF_TYPE_TUN)
695 hw->min_frame_size = TUN_MIN_PACKET_BYTES;
696 vnet_hw_interface_set_mtu (
697 vnm, hw->sw_if_index,
698 args->host_mtu_size ? args->host_mtu_size : TUN_DEFAULT_PACKET_BYTES);
701 vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc);
702 virtio_pre_input_node_enable (vm, vif);
703 virtio_vring_set_rx_queues (vm, vif);
704 virtio_vring_set_tx_queues (vm, vif);
706 vif->per_interface_next_index = ~0;
707 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
708 VNET_HW_INTERFACE_FLAG_LINK_UP);
710 * Host tun/tap driver link carrier state is "up" at creation. The
711 * driver never changes this unless the backend (VPP) changes it using
712 * TUNSETCARRIER ioctl(). See tap_set_carrier().
714 vif->host_carrier_up = 1;
721 ASSERT (args->error == 0);
723 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
726 tap_log_err (vif, "%U", format_clib_error, args->error);
730 clib_mem_free (vhost_mem);
731 if (old_netns_fd != -1)
733 /* in case we errored with a switched netns */
734 clib_setns (old_netns_fd);
735 close (old_netns_fd);
742 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
744 vnet_main_t *vnm = vnet_get_main ();
745 virtio_main_t *mm = &virtio_main;
747 vnet_hw_interface_t *hw;
749 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
750 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
751 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
753 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
755 if ((vif->type != VIRTIO_IF_TYPE_TAP) && (vif->type != VIRTIO_IF_TYPE_TUN))
756 return VNET_API_ERROR_INVALID_INTERFACE;
758 /* bring down the interface */
759 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
760 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
762 if (vif->type == VIRTIO_IF_TYPE_TAP)
763 ethernet_delete_interface (vnm, vif->hw_if_index);
764 else /* VIRTIO_IF_TYPE_TUN */
765 vnet_delete_hw_interface (vnm, vif->hw_if_index);
766 vif->hw_if_index = ~0;
774 tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index,
777 vnet_main_t *vnm = vnet_get_main ();
778 virtio_main_t *mm = &virtio_main;
780 vnet_hw_interface_t *hw;
781 vnet_hw_if_caps_change_t cc;
782 clib_error_t *err = 0;
785 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
787 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
788 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
790 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
792 const unsigned int csum_offload_on = TUN_F_CSUM;
793 const unsigned int csum_offload_off = 0;
794 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
795 vec_foreach_index (i, vif->tap_fds)
796 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
797 vif->gso_enabled = 0;
798 vif->packet_coalesce = 0;
800 cc.mask = VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_L4_TX_CKSUM;
803 cc.val = VNET_HW_IF_CAP_L4_TX_CKSUM;
804 vif->csum_offload_enabled = 1;
809 vif->csum_offload_enabled = 0;
811 vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc);
816 clib_warning ("Error %s checksum offload on sw_if_index %d",
817 enable_disable ? "enabling" : "disabling", sw_if_index);
818 return VNET_API_ERROR_SYSCALL_ERROR_3;
824 tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable,
825 int is_packet_coalesce)
827 vnet_main_t *vnm = vnet_get_main ();
828 virtio_main_t *mm = &virtio_main;
830 vnet_hw_interface_t *hw;
831 vnet_hw_if_caps_change_t cc;
832 clib_error_t *err = 0;
835 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
837 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
838 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
840 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
842 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
843 const unsigned int gso_off = 0;
844 unsigned int offload = enable_disable ? gso_on : gso_off;
845 vec_foreach_index (i, vif->tap_fds)
846 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
848 cc.mask = VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_L4_TX_CKSUM;
853 vif->gso_enabled = 1;
854 vif->csum_offload_enabled = 1;
855 if (is_packet_coalesce)
856 virtio_set_packet_coalesce (vif);
861 vif->gso_enabled = 0;
862 vif->csum_offload_enabled = 0;
863 vif->packet_coalesce = 0;
865 vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc);
870 clib_warning ("Error %s gso on sw_if_index %d",
871 enable_disable ? "enabling" : "disabling", sw_if_index);
872 return VNET_API_ERROR_SYSCALL_ERROR_3;
878 tap_dump_ifs (tap_interface_details_t ** out_tapids)
880 vnet_main_t *vnm = vnet_get_main ();
881 virtio_main_t *mm = &virtio_main;
883 vnet_virtio_vring_t *vring;
884 vnet_hw_interface_t *hi;
885 tap_interface_details_t *r_tapids = NULL;
886 tap_interface_details_t *tapid = NULL;
889 pool_foreach (vif, mm->interfaces) {
890 if ((vif->type != VIRTIO_IF_TYPE_TAP)
891 && (vif->type != VIRTIO_IF_TYPE_TUN))
893 vec_add2(r_tapids, tapid, 1);
894 clib_memset (tapid, 0, sizeof (*tapid));
896 tapid->sw_if_index = vif->sw_if_index;
897 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
898 clib_memcpy(tapid->dev_name, hi->name,
899 MIN (ARRAY_LEN (tapid->dev_name) - 1, vec_len (hi->name)));
900 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
901 tapid->rx_ring_sz = vring->queue_size;
902 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
903 tapid->tx_ring_sz = vring->queue_size;
904 tapid->tap_flags = vif->tap_flags;
905 clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6);
906 if (vif->host_if_name)
908 clib_memcpy(tapid->host_if_name, vif->host_if_name,
909 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
910 vec_len (vif->host_if_name)));
914 clib_memcpy(tapid->host_namespace, vif->net_ns,
915 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
916 vec_len (vif->net_ns)));
918 if (vif->host_bridge)
920 clib_memcpy(tapid->host_bridge, vif->host_bridge,
921 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
922 vec_len (vif->host_bridge)));
924 if (vif->host_ip4_prefix_len)
925 clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4);
926 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
927 if (vif->host_ip6_prefix_len)
928 clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16);
929 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
930 tapid->host_mtu_size = vif->host_mtu_size;
934 *out_tapids = r_tapids;
940 * Set host tap/tun interface carrier state so it will appear to host
941 * applications that the interface's link state changed.
943 * If the kernel we're building against does not have support for the
944 * TUNSETCARRIER ioctl command, do nothing.
947 tap_set_carrier (u32 hw_if_index, u32 carrier_up)
951 vnet_main_t *vnm = vnet_get_main ();
952 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
953 virtio_main_t *mm = &virtio_main;
957 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
958 vec_foreach (fd, vif->tap_fds)
960 ret = ioctl (*fd, TUNSETCARRIER, &carrier_up);
963 clib_warning ("ioctl (TUNSETCARRIER) returned %d", ret);
968 vif->host_carrier_up = (carrier_up != 0);
974 static clib_error_t *
975 tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
977 tap_main_t *tm = &tap_main;
979 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
981 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
984 return clib_error_return (0, "unknown input `%U'",
985 format_unformat_error, input);
992 * Set host tap/tun interface speed in Mbps.
995 tap_set_speed (u32 hw_if_index, u32 speed)
997 vnet_main_t *vnm = vnet_get_main ();
998 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
999 virtio_main_t *mm = &virtio_main;
1001 int old_netns_fd = -1;
1005 struct ethtool_cmd ecmd;
1008 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
1012 old_netns_fd = clib_netns_open (NULL /* self */);
1013 if ((nfd = clib_netns_open (vif->net_ns)) == -1)
1015 clib_warning ("Cannot open netns");
1018 if (clib_setns (nfd) == -1)
1020 clib_warning ("Cannot set ns");
1025 if ((ctl_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
1027 clib_warning ("Cannot open control socket");
1031 ecmd.cmd = ETHTOOL_GSET;
1032 clib_memset (&ifr, 0, sizeof (ifr));
1033 clib_memcpy (ifr.ifr_name, vif->host_if_name,
1034 strlen ((const char *) vif->host_if_name));
1035 ifr.ifr_data = (void *) &ecmd;
1036 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1038 clib_warning ("Cannot get device settings");
1042 if (ethtool_cmd_speed (&ecmd) != speed)
1044 ecmd.cmd = ETHTOOL_SSET;
1045 ethtool_cmd_speed_set (&ecmd, speed);
1046 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1048 clib_warning ("Cannot set device settings");
1054 if (old_netns_fd != -1)
1056 if (clib_setns (old_netns_fd) == -1)
1058 clib_warning ("Cannot set old ns");
1060 close (old_netns_fd);
1070 /* tap { host-mtu <size> } configuration. */
1071 VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
1073 static clib_error_t *
1074 tap_init (vlib_main_t * vm)
1076 tap_main_t *tm = &tap_main;
1077 clib_error_t *error = 0;
1079 tm->log_default = vlib_log_register_class ("tap", 0);
1080 vlib_log_debug (tm->log_default, "initialized");
1082 tm->host_mtu_size = 0;
1087 VLIB_INIT_FUNCTION (tap_init);
1090 * fd.io coding-style-patch-verification: ON
1093 * eval: (c-set-style "gnu")