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>
23 #include <linux/if_tun.h>
24 #include <sys/ioctl.h>
25 #include <linux/virtio_net.h>
26 #include <linux/vhost.h>
27 #include <sys/eventfd.h>
28 #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 <vnet/ethernet/ethernet.h>
39 #include <vnet/ip/ip4_packet.h>
40 #include <vnet/ip/ip6_packet.h>
41 #include <vnet/devices/netlink.h>
42 #include <vnet/devices/virtio/virtio.h>
43 #include <vnet/devices/tap/tap.h>
47 #define tap_log_err(dev, f, ...) \
48 vlib_log (VLIB_LOG_LEVEL_ERR, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
49 #define tap_log_dbg(dev, f, ...) \
50 vlib_log (VLIB_LOG_LEVEL_DEBUG, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
52 #define _IOCTL(fd,a,...) \
53 if (ioctl (fd, a, __VA_ARGS__) < 0) \
55 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
56 tap_log_err (vif, "%U", format_clib_error, err); \
61 VNET_HW_INTERFACE_CLASS (tun_device_hw_interface_class, static) =
64 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
69 virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
73 //TODO On MTU change call vnet_netlink_set_if_mtu
78 open_netns_fd (char *netns)
83 if (strncmp (netns, "pid:", 4) == 0)
84 s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0);
85 else if (netns[0] == '/')
86 s = format (0, "%s%c", netns, 0);
88 s = format (0, "/var/run/netns/%s%c", netns, 0);
90 fd = open ((char *) s, O_RDONLY);
95 #define TAP_MAX_INSTANCE 1024
98 tap_free (vlib_main_t * vm, virtio_if_t * vif)
100 virtio_main_t *mm = &virtio_main;
101 tap_main_t *tm = &tap_main;
102 clib_error_t *err = 0;
106 vec_foreach_index (i, vif->vhost_fds) if (vif->vhost_fds[i] != -1)
107 close (vif->vhost_fds[i]);
108 vec_foreach_index (i, vif->rxq_vrings)
109 virtio_vring_free_rx (vm, vif, RX_QUEUE (i));
110 vec_foreach_index (i, vif->txq_vrings)
111 virtio_vring_free_tx (vm, vif, TX_QUEUE (i));
114 _IOCTL (vif->tap_fds[0], TUNSETPERSIST, (void *) (uintptr_t) 0);
115 tap_log_dbg (vif, "TUNSETPERSIST: unset");
117 vec_foreach_index (i, vif->tap_fds) close (vif->tap_fds[i]);
119 vec_free (vif->vhost_fds);
120 vec_free (vif->rxq_vrings);
121 vec_free (vif->txq_vrings);
122 vec_free (vif->host_if_name);
123 vec_free (vif->net_ns);
124 vec_free (vif->host_bridge);
125 clib_error_free (vif->error);
127 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
128 clib_memset (vif, 0, sizeof (*vif));
129 pool_put (mm->interfaces, vif);
133 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
135 vlib_thread_main_t *thm = vlib_get_thread_main ();
136 vlib_physmem_main_t *vpm = &vm->physmem_main;
137 vnet_main_t *vnm = vnet_get_main ();
138 virtio_main_t *vim = &virtio_main;
139 tap_main_t *tm = &tap_main;
140 vnet_sw_interface_t *sw;
141 vnet_hw_interface_t *hw;
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 struct vhost_memory *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 = 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 host_if_name = (char *) args->host_if_name;
211 clib_memcpy (ifr.ifr_name, host_if_name,
212 clib_min (IFNAMSIZ, strlen (host_if_name)));
216 args->rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
217 err = clib_error_return (0, "host_if_name is not provided");
220 if (args->host_namespace)
222 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
223 if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1)
225 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
226 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
227 args->host_namespace);
230 if (setns (nfd, CLONE_NEWNET) == -1)
232 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
233 args->error = clib_error_return_unix (0, "setns '%s'",
234 args->host_namespace);
240 if ((tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
242 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
243 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
246 vec_add1 (vif->tap_fds, tfd);
247 tap_log_dbg (vif, "open tap fd %d", tfd);
249 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
250 tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
251 if ((tap_features & IFF_VNET_HDR) == 0)
253 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
254 args->error = clib_error_return (0, "vhost-net backend not available");
258 if ((tap_features & IFF_MULTI_QUEUE) == 0)
260 if (vif->num_rxqs > 1)
262 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
263 args->error = clib_error_return (0, "multiqueue not supported");
266 vif->num_rxqs = vif->num_txqs = 1;
269 ifr.ifr_flags |= IFF_MULTI_QUEUE;
271 hdrsz = sizeof (struct virtio_net_hdr_v1);
272 if (args->tap_flags & TAP_FLAG_GSO)
274 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
275 vif->gso_enabled = 1;
277 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
279 offload = TUN_F_CSUM;
280 vif->csum_offload_enabled = 1;
283 _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
284 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
285 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
287 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
288 tap_log_dbg (vif, "ifindex %d", vif->ifindex);
290 if (!args->host_if_name)
291 host_if_name = ifr.ifr_ifrn.ifrn_name;
293 host_if_name = (char *) args->host_if_name;
296 * unset the persistence when attaching to existing
299 if (args->tap_flags & TAP_FLAG_ATTACH)
301 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 0);
302 tap_log_dbg (vif, "TUNSETPERSIST: unset");
305 /* set the persistence */
306 if (args->tap_flags & TAP_FLAG_PERSIST)
308 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 1);
309 tap_log_dbg (vif, "TUNSETPERSIST: set");
311 /* verify persistence is set, read the flags */
312 _IOCTL (tfd, TUNGETIFF, (void *) &get_ifr);
313 tap_log_dbg (vif, "TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
314 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
316 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
317 args->error = clib_error_return (0, "persistence not supported");
322 /* create additional queues on the linux side.
323 * we create as many linux queue pairs as we have rx queues
325 for (i = 1; i < vif->num_rxqs; i++)
327 if ((qfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
329 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
330 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
333 _IOCTL (qfd, TUNSETIFF, (void *) &ifr);
334 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", qfd,
335 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
336 vec_add1 (vif->tap_fds, qfd);
339 for (i = 0; i < vif->num_rxqs; i++)
341 tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
342 vif->tap_fds[i], hdrsz);
343 _IOCTL (vif->tap_fds[i], TUNSETVNETHDRSZ, &hdrsz);
345 tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", vif->tap_fds[i],
347 _IOCTL (vif->tap_fds[i], TUNSETSNDBUF, &sndbuf);
349 tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", vif->tap_fds[i],
351 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
353 if (fcntl (vif->tap_fds[i], F_SETFL, O_NONBLOCK) < 0)
355 err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
356 tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
361 /* open as many vhost-net fds as required and set ownership */
362 num_vhost_queues = clib_max (vif->num_rxqs, vif->num_txqs);
363 for (i = 0; i < num_vhost_queues; i++)
365 if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
367 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
368 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
371 vec_add1 (vif->vhost_fds, vfd);
372 virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
373 _IOCTL (vfd, VHOST_SET_OWNER, 0);
374 virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
377 _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
378 virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
379 vif->remote_features);
381 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
383 args->rv = VNET_API_ERROR_UNSUPPORTED;
384 args->error = clib_error_return (0, "vhost-net backend doesn't support "
385 "VIRTIO_NET_F_MRG_RXBUF feature");
389 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
392 args->rv = VNET_API_ERROR_UNSUPPORTED;
393 args->error = clib_error_return (0, "vhost-net backend doesn't support "
394 "VIRTIO_RING_F_INDIRECT_DESC feature");
398 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
400 args->rv = VNET_API_ERROR_UNSUPPORTED;
401 args->error = clib_error_return (0, "vhost-net backend doesn't support "
402 "VIRTIO_F_VERSION_1 features");
406 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
407 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
408 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
410 virtio_set_net_hdr_size (vif);
412 if (!(args->tap_flags & TAP_FLAG_ATTACH))
414 /* if namespace is specified, all further netlink messages should be executed
415 after we change our net namespace */
416 if (args->host_namespace)
418 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
419 if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1)
421 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
422 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
423 args->host_namespace);
426 args->error = vnet_netlink_set_link_netns (vif->ifindex, nfd,
430 args->rv = VNET_API_ERROR_NETLINK_ERROR;
433 if (setns (nfd, CLONE_NEWNET) == -1)
435 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
436 args->error = clib_error_return_unix (0, "setns '%s'",
437 args->host_namespace);
440 if ((vif->ifindex = if_nametoindex (host_if_name)) == 0)
442 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
443 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
448 else if (host_if_name)
451 vnet_netlink_set_link_name (vif->ifindex, host_if_name);
454 args->rv = VNET_API_ERROR_NETLINK_ERROR;
460 if (vif->type == VIRTIO_IF_TYPE_TAP)
462 if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes))
463 ethernet_mac_address_generate (args->host_mac_addr.bytes);
464 args->error = vnet_netlink_set_link_addr (vif->ifindex,
465 args->host_mac_addr.bytes);
468 args->rv = VNET_API_ERROR_NETLINK_ERROR;
472 if (args->host_bridge)
474 args->error = vnet_netlink_set_link_master (vif->ifindex,
479 args->rv = VNET_API_ERROR_NETLINK_ERROR;
485 if (args->host_ip4_prefix_len)
487 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
488 &args->host_ip4_addr,
489 args->host_ip4_prefix_len);
492 args->rv = VNET_API_ERROR_NETLINK_ERROR;
497 if (args->host_ip6_prefix_len)
499 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
500 &args->host_ip6_addr,
501 args->host_ip6_prefix_len);
504 args->rv = VNET_API_ERROR_NETLINK_ERROR;
509 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
512 args->rv = VNET_API_ERROR_NETLINK_ERROR;
516 if (args->host_ip4_gw_set)
518 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
521 args->rv = VNET_API_ERROR_NETLINK_ERROR;
526 if (args->host_ip6_gw_set)
528 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
531 args->rv = VNET_API_ERROR_NETLINK_ERROR;
536 if (args->host_mtu_set)
539 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
542 args->rv = VNET_API_ERROR_NETLINK_ERROR;
546 else if (tm->host_mtu_size != 0)
549 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
552 args->rv = VNET_API_ERROR_NETLINK_ERROR;
555 args->host_mtu_set = 1;
556 args->host_mtu_size = tm->host_mtu_size;
559 /* switch back to old net namespace */
560 if (args->host_namespace)
562 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
564 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
565 args->error = clib_error_return_unix (0, "setns '%s'",
566 args->host_namespace);
571 for (i = 0; i < num_vhost_queues; i++)
573 if (i < vif->num_rxqs && (args->error =
574 virtio_vring_init (vm, vif, RX_QUEUE (i),
577 args->rv = VNET_API_ERROR_INIT_FAILED;
581 if (i < vif->num_txqs && (args->error =
582 virtio_vring_init (vm, vif, TX_QUEUE (i),
585 args->rv = VNET_API_ERROR_INIT_FAILED;
590 /* setup features and memtable */
591 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
592 vhost_mem = clib_mem_alloc (i);
593 clib_memset (vhost_mem, 0, i);
594 vhost_mem->nregions = 1;
595 vhost_mem->regions[0].memory_size = vpm->max_size;
596 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
597 vhost_mem->regions[0].userspace_addr =
598 vhost_mem->regions[0].guest_phys_addr;
600 for (i = 0; i < vhost_mem->nregions; i++)
601 virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
602 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
603 vhost_mem->regions[0].memory_size,
604 vhost_mem->regions[0].guest_phys_addr,
605 vhost_mem->regions[0].userspace_addr);
608 for (i = 0; i < num_vhost_queues; i++)
610 int fd = vif->vhost_fds[i];
611 _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
612 virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
614 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
615 virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
618 /* finish initializing queue pair */
619 for (i = 0; i < num_vhost_queues * 2; i++)
621 struct vhost_vring_addr addr = { 0 };
622 struct vhost_vring_state state = { 0 };
623 struct vhost_vring_file file = { 0 };
624 virtio_vring_t *vring;
626 int fd = vif->vhost_fds[qp];
630 if (qp >= vif->num_txqs)
632 vring = vec_elt_at_index (vif->txq_vrings, qp);
636 if (qp >= vif->num_rxqs)
638 vring = vec_elt_at_index (vif->rxq_vrings, qp);
641 addr.index = state.index = file.index = vring->queue_id & 1;
642 state.num = vring->size;
643 virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
644 state.index, state.num);
645 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
648 addr.desc_user_addr = pointer_to_uword (vring->desc);
649 addr.avail_user_addr = pointer_to_uword (vring->avail);
650 addr.used_user_addr = pointer_to_uword (vring->used);
652 virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
653 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
654 "used_user_addr 0x%lx", fd, addr.index,
655 addr.flags, addr.desc_user_addr, addr.avail_user_addr,
656 addr.used_user_addr);
657 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
659 file.fd = vring->call_fd;
660 virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
661 fd, file.index, file.fd);
662 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
664 file.fd = vring->kick_fd;
665 virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
666 fd, file.index, file.fd);
667 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
669 file.fd = vif->tap_fds[qp % vif->num_rxqs];
670 virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
671 fd, file.index, file.fd);
672 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
675 if (vif->type == VIRTIO_IF_TYPE_TAP)
677 if (!args->mac_addr_set)
678 ethernet_mac_address_generate (args->mac_addr.bytes);
680 clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6);
681 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
683 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
684 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
685 vif->host_mtu_size = args->host_mtu_size;
686 vif->tap_flags = args->tap_flags;
687 clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6);
688 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
689 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
690 if (args->host_ip4_prefix_len)
691 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
692 if (args->host_ip6_prefix_len)
693 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
695 if (vif->type != VIRTIO_IF_TYPE_TUN)
698 ethernet_register_interface (vnm, virtio_device_class.index,
699 vif->dev_instance, vif->mac_addr,
701 virtio_eth_flag_change);
704 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
711 vif->hw_if_index = vnet_register_interface
712 (vnm, virtio_device_class.index,
713 vif->dev_instance /* device instance */ ,
714 tun_device_hw_interface_class.index, vif->dev_instance);
717 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
718 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
719 vif->sw_if_index = sw->sw_if_index;
720 args->sw_if_index = vif->sw_if_index;
722 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
723 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
724 if (args->tap_flags & TAP_FLAG_GSO)
726 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
727 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
729 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
731 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
733 if ((args->tap_flags & TAP_FLAG_GSO)
734 && (args->tap_flags & TAP_FLAG_GRO_COALESCE))
736 vif->packet_coalesce = 1;
737 virtio_set_packet_coalesce (vif);
739 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
740 virtio_input_node.index);
742 for (i = 0; i < vif->num_rxqs; i++)
744 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
745 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
746 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
747 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
750 vif->per_interface_next_index = ~0;
751 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
752 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
753 VNET_HW_INTERFACE_FLAG_LINK_UP);
754 vif->cxq_vring = NULL;
761 ASSERT (args->error == 0);
763 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
766 tap_log_err (vif, "%U", format_clib_error, args->error);
770 clib_mem_free (vhost_mem);
771 if (old_netns_fd != -1)
772 close (old_netns_fd);
778 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
780 vnet_main_t *vnm = vnet_get_main ();
781 virtio_main_t *mm = &virtio_main;
784 vnet_hw_interface_t *hw;
786 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 if ((vif->type != VIRTIO_IF_TYPE_TAP) && (vif->type != VIRTIO_IF_TYPE_TUN))
793 return VNET_API_ERROR_INVALID_INTERFACE;
795 /* bring down the interface */
796 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
797 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
798 for (i = 0; i < vif->num_rxqs; i++)
799 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
801 if (vif->type == VIRTIO_IF_TYPE_TAP)
802 ethernet_delete_interface (vnm, vif->hw_if_index);
803 else /* VIRTIO_IF_TYPE_TUN */
804 vnet_delete_hw_interface (vnm, vif->hw_if_index);
805 vif->hw_if_index = ~0;
813 tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index,
816 vnet_main_t *vnm = vnet_get_main ();
817 virtio_main_t *mm = &virtio_main;
819 vnet_hw_interface_t *hw;
820 clib_error_t *err = 0;
823 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
825 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
826 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
828 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
830 const unsigned int csum_offload_on = TUN_F_CSUM;
831 const unsigned int csum_offload_off = 0;
832 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
833 vec_foreach_index (i, vif->tap_fds)
834 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
835 vif->gso_enabled = 0;
836 vif->packet_coalesce = 0;
837 vif->csum_offload_enabled = enable_disable ? 1 : 0;
839 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
841 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
846 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD) ==
849 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
854 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD) !=
857 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
864 clib_warning ("Error %s checksum offload on sw_if_index %d",
865 enable_disable ? "enabling" : "disabling", sw_if_index);
866 return VNET_API_ERROR_SYSCALL_ERROR_3;
872 tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable,
873 int is_packet_coalesce)
875 vnet_main_t *vnm = vnet_get_main ();
876 virtio_main_t *mm = &virtio_main;
878 vnet_hw_interface_t *hw;
879 clib_error_t *err = 0;
882 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
884 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
885 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
887 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
889 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
890 const unsigned int gso_off = 0;
891 unsigned int offload = enable_disable ? gso_on : gso_off;
892 vec_foreach_index (i, vif->tap_fds)
893 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
894 vif->gso_enabled = enable_disable ? 1 : 0;
895 vif->csum_offload_enabled = 0;
898 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0)
900 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
901 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
903 if (is_packet_coalesce)
905 vif->packet_coalesce = 1;
906 virtio_set_packet_coalesce (vif);
911 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
913 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
914 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
916 vif->packet_coalesce = 0;
922 clib_warning ("Error %s gso on sw_if_index %d",
923 enable_disable ? "enabling" : "disabling", sw_if_index);
924 return VNET_API_ERROR_SYSCALL_ERROR_3;
930 tap_dump_ifs (tap_interface_details_t ** out_tapids)
932 vnet_main_t *vnm = vnet_get_main ();
933 virtio_main_t *mm = &virtio_main;
935 virtio_vring_t *vring;
936 vnet_hw_interface_t *hi;
937 tap_interface_details_t *r_tapids = NULL;
938 tap_interface_details_t *tapid = NULL;
941 pool_foreach (vif, mm->interfaces,
942 if ((vif->type != VIRTIO_IF_TYPE_TAP)
943 && (vif->type != VIRTIO_IF_TYPE_TUN))
945 vec_add2(r_tapids, tapid, 1);
946 clib_memset (tapid, 0, sizeof (*tapid));
948 tapid->sw_if_index = vif->sw_if_index;
949 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
950 clib_memcpy(tapid->dev_name, hi->name,
951 MIN (ARRAY_LEN (tapid->dev_name) - 1,
952 strlen ((const char *) hi->name)));
953 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
954 tapid->rx_ring_sz = vring->size;
955 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
956 tapid->tx_ring_sz = vring->size;
957 tapid->tap_flags = vif->tap_flags;
958 clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6);
959 if (vif->host_if_name)
961 clib_memcpy(tapid->host_if_name, vif->host_if_name,
962 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
963 strlen ((const char *) vif->host_if_name)));
967 clib_memcpy(tapid->host_namespace, vif->net_ns,
968 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
969 strlen ((const char *) vif->net_ns)));
971 if (vif->host_bridge)
973 clib_memcpy(tapid->host_bridge, vif->host_bridge,
974 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
975 strlen ((const char *) vif->host_bridge)));
977 if (vif->host_ip4_prefix_len)
978 clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4);
979 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
980 if (vif->host_ip6_prefix_len)
981 clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16);
982 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
983 tapid->host_mtu_size = vif->host_mtu_size;
987 *out_tapids = r_tapids;
992 static clib_error_t *
993 tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
995 tap_main_t *tm = &tap_main;
997 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
999 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
1002 return clib_error_return (0, "unknown input `%U'",
1003 format_unformat_error, input);
1009 /* tap { host-mtu <size> } configuration. */
1010 VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
1012 static clib_error_t *
1013 tap_init (vlib_main_t * vm)
1015 tap_main_t *tm = &tap_main;
1016 clib_error_t *error = 0;
1018 tm->log_default = vlib_log_register_class ("tap", 0);
1019 vlib_log_debug (tm->log_default, "initialized");
1021 tm->host_mtu_size = 0;
1026 VLIB_INIT_FUNCTION (tap_init);
1029 * fd.io coding-style-patch-verification: ON
1032 * eval: (c-set-style "gnu")