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>
33 #include <linux/netlink.h>
34 #include <linux/rtnetlink.h>
36 #include <vlib/vlib.h>
37 #include <vlib/physmem.h>
38 #include <vlib/unix/unix.h>
39 #include <vnet/ethernet/ethernet.h>
40 #include <vnet/ip/ip4_packet.h>
41 #include <vnet/ip/ip6_packet.h>
42 #include <vnet/devices/netlink.h>
43 #include <vnet/devices/virtio/virtio.h>
44 #include <vnet/devices/tap/tap.h>
48 #define tap_log_err(dev, f, ...) \
49 vlib_log (VLIB_LOG_LEVEL_ERR, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
50 #define tap_log_dbg(dev, f, ...) \
51 vlib_log (VLIB_LOG_LEVEL_DEBUG, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
53 #define _IOCTL(fd,a,...) \
54 if (ioctl (fd, a, __VA_ARGS__) < 0) \
56 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
57 tap_log_err (vif, "%U", format_clib_error, err); \
62 VNET_HW_INTERFACE_CLASS (tun_device_hw_interface_class, static) =
65 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
69 #define TUN_MAX_PACKET_BYTES 65355
70 #define TUN_MIN_PACKET_BYTES 64
71 #define TUN_DEFAULT_PACKET_BYTES 1500
74 virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
78 //TODO On MTU change call vnet_netlink_set_if_mtu
83 open_netns_fd (char *netns)
88 if (strncmp (netns, "pid:", 4) == 0)
89 s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0);
90 else if (netns[0] == '/')
91 s = format (0, "%s%c", netns, 0);
93 s = format (0, "/var/run/netns/%s%c", netns, 0);
95 fd = open ((char *) s, O_RDONLY);
100 #define TAP_MAX_INSTANCE 1024
103 tap_free (vlib_main_t * vm, virtio_if_t * vif)
105 virtio_main_t *mm = &virtio_main;
106 tap_main_t *tm = &tap_main;
107 clib_error_t *err = 0;
111 vec_foreach_index (i, vif->vhost_fds) if (vif->vhost_fds[i] != -1)
112 close (vif->vhost_fds[i]);
113 vec_foreach_index (i, vif->rxq_vrings)
114 virtio_vring_free_rx (vm, vif, RX_QUEUE (i));
115 vec_foreach_index (i, vif->txq_vrings)
116 virtio_vring_free_tx (vm, vif, TX_QUEUE (i));
121 _IOCTL (vif->tap_fds[0], TUNSETPERSIST, (void *) (uintptr_t) 0);
122 tap_log_dbg (vif, "TUNSETPERSIST: unset");
125 vec_foreach_index (i, vif->tap_fds) close (vif->tap_fds[i]);
127 vec_free (vif->vhost_fds);
128 vec_free (vif->rxq_vrings);
129 vec_free (vif->txq_vrings);
130 vec_free (vif->host_if_name);
131 vec_free (vif->net_ns);
132 vec_free (vif->host_bridge);
133 clib_error_free (vif->error);
135 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
136 clib_memset (vif, 0, sizeof (*vif));
137 pool_put (mm->interfaces, vif);
141 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
143 vlib_thread_main_t *thm = vlib_get_thread_main ();
144 vlib_physmem_main_t *vpm = &vm->physmem_main;
145 vnet_main_t *vnm = vnet_get_main ();
146 virtio_main_t *vim = &virtio_main;
147 tap_main_t *tm = &tap_main;
148 vnet_sw_interface_t *sw;
149 vnet_hw_interface_t *hw;
150 int i, num_vhost_queues;
151 int old_netns_fd = -1;
152 struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR };
153 struct ifreq get_ifr = {.ifr_flags = 0 };
155 vhost_memory_t *vhost_mem = 0;
156 virtio_if_t *vif = 0;
157 clib_error_t *err = 0;
158 unsigned int tap_features;
159 int tfd = -1, qfd = -1, vfd = -1, nfd = -1;
160 char *host_if_name = 0;
161 unsigned int offload = 0;
166 if (clib_bitmap_get (tm->tap_ids, args->id))
168 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
169 args->error = clib_error_return (0, "interface already exists");
175 args->id = clib_bitmap_first_clear (tm->tap_ids);
178 if (args->id > TAP_MAX_INSTANCE)
180 args->rv = VNET_API_ERROR_UNSPECIFIED;
181 args->error = clib_error_return (0, "cannot find free interface id");
185 pool_get_zero (vim->interfaces, vif);
187 if (args->tap_flags & TAP_FLAG_TUN)
189 vif->type = VIRTIO_IF_TYPE_TUN;
190 ifr.ifr_flags |= IFF_TUN;
193 * From kernel 4.20, xdp support has been added in tun_sendmsg.
194 * If sndbuf == INT_MAX, vhost batches the packet and processes
195 * them using xdp data path for tun driver. It assumes packets
196 * are ethernet frames (It needs to be fixed).
197 * To avoid xdp data path in tun driver, sndbuf value should
200 sndbuf = INT_MAX - 1;
204 vif->type = VIRTIO_IF_TYPE_TAP;
205 ifr.ifr_flags |= IFF_TAP;
209 vif->dev_instance = vif - vim->interfaces;
211 vif->num_txqs = thm->n_vlib_mains;
212 vif->num_rxqs = clib_max (args->num_rx_queues, 1);
214 if (args->tap_flags & TAP_FLAG_ATTACH)
216 if (args->host_if_name != NULL)
218 host_if_name = (char *) args->host_if_name;
219 clib_memcpy (ifr.ifr_name, host_if_name,
220 clib_min (IFNAMSIZ, vec_len (host_if_name)));
224 args->rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
225 err = clib_error_return (0, "host_if_name is not provided");
228 if (args->host_namespace)
230 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
231 if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1)
233 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
234 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
235 args->host_namespace);
238 if (setns (nfd, CLONE_NEWNET) == -1)
240 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
241 args->error = clib_error_return_unix (0, "setns '%s'",
242 args->host_namespace);
248 if ((tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
250 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
251 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
254 vec_add1 (vif->tap_fds, tfd);
255 tap_log_dbg (vif, "open tap fd %d", tfd);
257 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
258 tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
259 if ((tap_features & IFF_VNET_HDR) == 0)
261 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
262 args->error = clib_error_return (0, "vhost-net backend not available");
266 if ((tap_features & IFF_MULTI_QUEUE) == 0)
268 if (vif->num_rxqs > 1)
270 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
271 args->error = clib_error_return (0, "multiqueue not supported");
274 vif->num_rxqs = vif->num_txqs = 1;
277 ifr.ifr_flags |= IFF_MULTI_QUEUE;
279 hdrsz = sizeof (virtio_net_hdr_v1_t);
280 if (args->tap_flags & TAP_FLAG_GSO)
282 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
283 vif->gso_enabled = 1;
285 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
287 offload = TUN_F_CSUM;
288 vif->csum_offload_enabled = 1;
291 _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
292 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
293 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
295 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
296 tap_log_dbg (vif, "ifindex %d", vif->ifindex);
298 if (!args->host_if_name)
299 host_if_name = ifr.ifr_ifrn.ifrn_name;
301 host_if_name = (char *) args->host_if_name;
304 * unset the persistence when attaching to existing
307 if (args->tap_flags & TAP_FLAG_ATTACH)
309 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 0);
310 tap_log_dbg (vif, "TUNSETPERSIST: unset");
313 /* set the persistence */
314 if (args->tap_flags & TAP_FLAG_PERSIST)
316 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 1);
317 tap_log_dbg (vif, "TUNSETPERSIST: set");
319 /* verify persistence is set, read the flags */
320 _IOCTL (tfd, TUNGETIFF, (void *) &get_ifr);
321 tap_log_dbg (vif, "TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
322 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
324 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
325 args->error = clib_error_return (0, "persistence not supported");
330 /* create additional queues on the linux side.
331 * we create as many linux queue pairs as we have rx queues
333 for (i = 1; i < vif->num_rxqs; i++)
335 if ((qfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
337 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
338 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
341 _IOCTL (qfd, TUNSETIFF, (void *) &ifr);
342 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", qfd,
343 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
344 vec_add1 (vif->tap_fds, qfd);
347 for (i = 0; i < vif->num_rxqs; i++)
349 tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
350 vif->tap_fds[i], hdrsz);
351 _IOCTL (vif->tap_fds[i], TUNSETVNETHDRSZ, &hdrsz);
353 tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", vif->tap_fds[i],
355 _IOCTL (vif->tap_fds[i], TUNSETSNDBUF, &sndbuf);
357 tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", vif->tap_fds[i],
359 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
361 if (fcntl (vif->tap_fds[i], F_SETFL, O_NONBLOCK) < 0)
363 err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
364 tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
369 /* open as many vhost-net fds as required and set ownership */
370 num_vhost_queues = clib_max (vif->num_rxqs, vif->num_txqs);
371 for (i = 0; i < num_vhost_queues; i++)
373 if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
375 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
376 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
379 vec_add1 (vif->vhost_fds, vfd);
380 virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
381 _IOCTL (vfd, VHOST_SET_OWNER, 0);
382 virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
385 _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
386 virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
387 vif->remote_features);
389 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
391 args->rv = VNET_API_ERROR_UNSUPPORTED;
392 args->error = clib_error_return (0, "vhost-net backend doesn't support "
393 "VIRTIO_NET_F_MRG_RXBUF feature");
397 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
400 args->rv = VNET_API_ERROR_UNSUPPORTED;
401 args->error = clib_error_return (0, "vhost-net backend doesn't support "
402 "VIRTIO_RING_F_INDIRECT_DESC feature");
406 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
408 args->rv = VNET_API_ERROR_UNSUPPORTED;
409 args->error = clib_error_return (0, "vhost-net backend doesn't support "
410 "VIRTIO_F_VERSION_1 features");
414 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
415 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
416 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
418 virtio_set_net_hdr_size (vif);
420 if (!(args->tap_flags & TAP_FLAG_ATTACH))
422 /* if namespace is specified, all further netlink messages should be executed
423 after we change our net namespace */
424 if (args->host_namespace)
426 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
427 if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1)
429 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
430 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
431 args->host_namespace);
434 args->error = vnet_netlink_set_link_netns (vif->ifindex, nfd,
438 args->rv = VNET_API_ERROR_NETLINK_ERROR;
441 if (setns (nfd, CLONE_NEWNET) == -1)
443 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
444 args->error = clib_error_return_unix (0, "setns '%s'",
445 args->host_namespace);
448 if ((vif->ifindex = if_nametoindex (host_if_name)) == 0)
450 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
451 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
456 else if (host_if_name)
459 vnet_netlink_set_link_name (vif->ifindex, host_if_name);
462 args->rv = VNET_API_ERROR_NETLINK_ERROR;
468 if (vif->type == VIRTIO_IF_TYPE_TAP)
470 if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes))
471 ethernet_mac_address_generate (args->host_mac_addr.bytes);
472 args->error = vnet_netlink_set_link_addr (vif->ifindex,
473 args->host_mac_addr.bytes);
476 args->rv = VNET_API_ERROR_NETLINK_ERROR;
480 if (args->host_bridge)
482 args->error = vnet_netlink_set_link_master (vif->ifindex,
487 args->rv = VNET_API_ERROR_NETLINK_ERROR;
493 if (args->host_ip4_prefix_len)
495 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
496 &args->host_ip4_addr,
497 args->host_ip4_prefix_len);
500 args->rv = VNET_API_ERROR_NETLINK_ERROR;
505 if (args->host_ip6_prefix_len)
507 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
508 &args->host_ip6_addr,
509 args->host_ip6_prefix_len);
512 args->rv = VNET_API_ERROR_NETLINK_ERROR;
517 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
520 args->rv = VNET_API_ERROR_NETLINK_ERROR;
524 if (args->host_ip4_gw_set)
526 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
529 args->rv = VNET_API_ERROR_NETLINK_ERROR;
534 if (args->host_ip6_gw_set)
536 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
539 args->rv = VNET_API_ERROR_NETLINK_ERROR;
544 if (args->host_mtu_set)
547 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
550 args->rv = VNET_API_ERROR_NETLINK_ERROR;
554 else if (tm->host_mtu_size != 0)
557 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
560 args->rv = VNET_API_ERROR_NETLINK_ERROR;
563 args->host_mtu_set = 1;
564 args->host_mtu_size = tm->host_mtu_size;
567 /* switch back to old net namespace */
568 if (args->host_namespace)
570 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
572 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
573 args->error = clib_error_return_unix (0, "setns '%s'",
574 args->host_namespace);
579 for (i = 0; i < num_vhost_queues; i++)
581 if (i < vif->num_rxqs && (args->error =
582 virtio_vring_init (vm, vif, RX_QUEUE (i),
585 args->rv = VNET_API_ERROR_INIT_FAILED;
589 if (i < vif->num_txqs && (args->error =
590 virtio_vring_init (vm, vif, TX_QUEUE (i),
593 args->rv = VNET_API_ERROR_INIT_FAILED;
598 /* setup features and memtable */
599 i = sizeof (vhost_memory_t) + sizeof (vhost_memory_region_t);
600 vhost_mem = clib_mem_alloc (i);
601 clib_memset (vhost_mem, 0, i);
602 vhost_mem->nregions = 1;
603 vhost_mem->regions[0].memory_size = vpm->max_size;
604 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
605 vhost_mem->regions[0].userspace_addr =
606 vhost_mem->regions[0].guest_phys_addr;
608 for (i = 0; i < vhost_mem->nregions; i++)
609 virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
610 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
611 vhost_mem->regions[0].memory_size,
612 vhost_mem->regions[0].guest_phys_addr,
613 vhost_mem->regions[0].userspace_addr);
616 for (i = 0; i < num_vhost_queues; i++)
618 int fd = vif->vhost_fds[i];
619 _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
620 virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
622 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
623 virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
626 /* finish initializing queue pair */
627 for (i = 0; i < num_vhost_queues * 2; i++)
629 vhost_vring_addr_t addr = { 0 };
630 vhost_vring_state_t state = { 0 };
631 vhost_vring_file_t file = { 0 };
632 virtio_vring_t *vring;
634 int fd = vif->vhost_fds[qp];
638 if (qp >= vif->num_txqs)
640 vring = vec_elt_at_index (vif->txq_vrings, qp);
644 if (qp >= vif->num_rxqs)
646 vring = vec_elt_at_index (vif->rxq_vrings, qp);
649 addr.index = state.index = file.index = vring->queue_id & 1;
650 state.num = vring->size;
651 virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
652 state.index, state.num);
653 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
656 addr.desc_user_addr = pointer_to_uword (vring->desc);
657 addr.avail_user_addr = pointer_to_uword (vring->avail);
658 addr.used_user_addr = pointer_to_uword (vring->used);
660 virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
661 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
662 "used_user_addr 0x%lx", fd, addr.index,
663 addr.flags, addr.desc_user_addr, addr.avail_user_addr,
664 addr.used_user_addr);
665 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
667 file.fd = vring->call_fd;
668 virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
669 fd, file.index, file.fd);
670 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
672 file.fd = vring->kick_fd;
673 virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
674 fd, file.index, file.fd);
675 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
677 file.fd = vif->tap_fds[qp % vif->num_rxqs];
678 virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
679 fd, file.index, file.fd);
680 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
683 if (vif->type == VIRTIO_IF_TYPE_TAP)
685 if (!args->mac_addr_set)
686 ethernet_mac_address_generate (args->mac_addr.bytes);
688 clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6);
689 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
691 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
692 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
693 vif->host_mtu_size = args->host_mtu_size;
694 vif->tap_flags = args->tap_flags;
695 clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6);
696 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
697 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
698 if (args->host_ip4_prefix_len)
699 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
700 if (args->host_ip6_prefix_len)
701 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
703 if (vif->type != VIRTIO_IF_TYPE_TUN)
706 ethernet_register_interface (vnm, virtio_device_class.index,
707 vif->dev_instance, vif->mac_addr,
709 virtio_eth_flag_change);
712 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
719 vif->hw_if_index = vnet_register_interface
720 (vnm, virtio_device_class.index,
721 vif->dev_instance /* device instance */ ,
722 tun_device_hw_interface_class.index, vif->dev_instance);
725 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
726 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
727 vif->sw_if_index = sw->sw_if_index;
728 args->sw_if_index = vif->sw_if_index;
730 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
731 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE;
732 if (args->tap_flags & TAP_FLAG_GSO)
734 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
735 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
736 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM;
738 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
740 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM |
741 VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM;
743 if ((args->tap_flags & TAP_FLAG_GSO)
744 && (args->tap_flags & TAP_FLAG_GRO_COALESCE))
746 virtio_set_packet_coalesce (vif);
748 if (vif->type == VIRTIO_IF_TYPE_TUN)
750 hw->max_supported_packet_bytes = TUN_MAX_PACKET_BYTES;
751 hw->min_packet_bytes = hw->min_supported_packet_bytes =
752 TUN_MIN_PACKET_BYTES;
753 hw->max_packet_bytes =
754 args->host_mtu_size ? args->host_mtu_size : TUN_DEFAULT_PACKET_BYTES;
755 vnet_sw_interface_set_mtu (vnm, hw->sw_if_index, hw->max_packet_bytes);
758 virtio_vring_set_rx_queues (vm, vif);
760 vif->per_interface_next_index = ~0;
761 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
762 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
763 VNET_HW_INTERFACE_FLAG_LINK_UP);
765 * Host tun/tap driver link carrier state is "up" at creation. The
766 * driver never changes this unless the backend (VPP) changes it using
767 * TUNSETCARRIER ioctl(). See tap_set_carrier().
769 vif->host_carrier_up = 1;
770 vif->cxq_vring = NULL;
777 ASSERT (args->error == 0);
779 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
782 tap_log_err (vif, "%U", format_clib_error, args->error);
786 clib_mem_free (vhost_mem);
787 if (old_netns_fd != -1)
788 close (old_netns_fd);
794 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
796 vnet_main_t *vnm = vnet_get_main ();
797 virtio_main_t *mm = &virtio_main;
799 vnet_hw_interface_t *hw;
801 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
802 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
803 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
805 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
807 if ((vif->type != VIRTIO_IF_TYPE_TAP) && (vif->type != VIRTIO_IF_TYPE_TUN))
808 return VNET_API_ERROR_INVALID_INTERFACE;
810 /* bring down the interface */
811 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
812 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
814 if (vif->type == VIRTIO_IF_TYPE_TAP)
815 ethernet_delete_interface (vnm, vif->hw_if_index);
816 else /* VIRTIO_IF_TYPE_TUN */
817 vnet_delete_hw_interface (vnm, vif->hw_if_index);
818 vif->hw_if_index = ~0;
826 tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index,
829 vnet_main_t *vnm = vnet_get_main ();
830 virtio_main_t *mm = &virtio_main;
832 vnet_hw_interface_t *hw;
833 clib_error_t *err = 0;
836 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
838 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
839 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
841 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
843 const unsigned int csum_offload_on = TUN_F_CSUM;
844 const unsigned int csum_offload_off = 0;
845 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
846 vec_foreach_index (i, vif->tap_fds)
847 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
848 vif->gso_enabled = 0;
849 vif->packet_coalesce = 0;
850 vif->csum_offload_enabled = enable_disable ? 1 : 0;
852 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) != 0)
854 hw->caps &= ~VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO;
859 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
863 hw->caps &= ~VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
869 clib_warning ("Error %s checksum offload on sw_if_index %d",
870 enable_disable ? "enabling" : "disabling", sw_if_index);
871 return VNET_API_ERROR_SYSCALL_ERROR_3;
877 tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable,
878 int is_packet_coalesce)
880 vnet_main_t *vnm = vnet_get_main ();
881 virtio_main_t *mm = &virtio_main;
883 vnet_hw_interface_t *hw;
884 clib_error_t *err = 0;
887 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
889 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
890 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
892 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
894 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
895 const unsigned int gso_off = 0;
896 unsigned int offload = enable_disable ? gso_on : gso_off;
897 vec_foreach_index (i, vif->tap_fds)
898 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
899 vif->gso_enabled = enable_disable ? 1 : 0;
900 vif->csum_offload_enabled = 0;
903 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) == 0)
905 hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
906 VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM;
908 if (is_packet_coalesce)
910 virtio_set_packet_coalesce (vif);
915 if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) != 0)
917 hw->caps &= ~(VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO |
918 VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM);
920 vif->packet_coalesce = 0;
926 clib_warning ("Error %s gso on sw_if_index %d",
927 enable_disable ? "enabling" : "disabling", sw_if_index);
928 return VNET_API_ERROR_SYSCALL_ERROR_3;
934 tap_dump_ifs (tap_interface_details_t ** out_tapids)
936 vnet_main_t *vnm = vnet_get_main ();
937 virtio_main_t *mm = &virtio_main;
939 virtio_vring_t *vring;
940 vnet_hw_interface_t *hi;
941 tap_interface_details_t *r_tapids = NULL;
942 tap_interface_details_t *tapid = NULL;
945 pool_foreach (vif, mm->interfaces) {
946 if ((vif->type != VIRTIO_IF_TYPE_TAP)
947 && (vif->type != VIRTIO_IF_TYPE_TUN))
949 vec_add2(r_tapids, tapid, 1);
950 clib_memset (tapid, 0, sizeof (*tapid));
952 tapid->sw_if_index = vif->sw_if_index;
953 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
954 clib_memcpy(tapid->dev_name, hi->name,
955 MIN (ARRAY_LEN (tapid->dev_name) - 1, vec_len (hi->name)));
956 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
957 tapid->rx_ring_sz = vring->size;
958 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
959 tapid->tx_ring_sz = vring->size;
960 tapid->tap_flags = vif->tap_flags;
961 clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6);
962 if (vif->host_if_name)
964 clib_memcpy(tapid->host_if_name, vif->host_if_name,
965 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
966 vec_len (vif->host_if_name)));
970 clib_memcpy(tapid->host_namespace, vif->net_ns,
971 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
972 vec_len (vif->net_ns)));
974 if (vif->host_bridge)
976 clib_memcpy(tapid->host_bridge, vif->host_bridge,
977 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
978 vec_len (vif->host_bridge)));
980 if (vif->host_ip4_prefix_len)
981 clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4);
982 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
983 if (vif->host_ip6_prefix_len)
984 clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16);
985 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
986 tapid->host_mtu_size = vif->host_mtu_size;
990 *out_tapids = r_tapids;
996 * Set host tap/tun interface carrier state so it will appear to host
997 * applications that the interface's link state changed.
999 * If the kernel we're building against does not have support for the
1000 * TUNSETCARRIER ioctl command, do nothing.
1003 tap_set_carrier (u32 hw_if_index, u32 carrier_up)
1006 #ifdef TUNSETCARRIER
1007 vnet_main_t *vnm = vnet_get_main ();
1008 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1009 virtio_main_t *mm = &virtio_main;
1013 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
1014 vec_foreach (fd, vif->tap_fds)
1016 ret = ioctl (*fd, TUNSETCARRIER, &carrier_up);
1019 clib_warning ("ioctl (TUNSETCARRIER) returned %d", ret);
1024 vif->host_carrier_up = (carrier_up != 0);
1030 static clib_error_t *
1031 tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
1033 tap_main_t *tm = &tap_main;
1035 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1037 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
1040 return clib_error_return (0, "unknown input `%U'",
1041 format_unformat_error, input);
1048 * Set host tap/tun interface speed in Mbps.
1051 tap_set_speed (u32 hw_if_index, u32 speed)
1053 vnet_main_t *vnm = vnet_get_main ();
1054 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1055 virtio_main_t *mm = &virtio_main;
1057 int old_netns_fd = -1;
1061 struct ethtool_cmd ecmd;
1064 vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
1068 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
1069 if ((nfd = open_netns_fd ((char *) vif->net_ns)) == -1)
1071 clib_warning ("Cannot open netns");
1074 if (setns (nfd, CLONE_NEWNET) == -1)
1076 clib_warning ("Cannot set ns");
1081 if ((ctl_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
1083 clib_warning ("Cannot open control socket");
1087 ecmd.cmd = ETHTOOL_GSET;
1088 clib_memset (&ifr, 0, sizeof (ifr));
1089 clib_memcpy (ifr.ifr_name, vif->host_if_name,
1090 strlen ((const char *) vif->host_if_name));
1091 ifr.ifr_data = (void *) &ecmd;
1092 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1094 clib_warning ("Cannot get device settings");
1098 if (ethtool_cmd_speed (&ecmd) != speed)
1100 ecmd.cmd = ETHTOOL_SSET;
1101 ethtool_cmd_speed_set (&ecmd, speed);
1102 if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0)
1104 clib_warning ("Cannot set device settings");
1110 if (old_netns_fd != -1)
1112 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
1114 clib_warning ("Cannot set old ns");
1116 close (old_netns_fd);
1126 /* tap { host-mtu <size> } configuration. */
1127 VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
1129 static clib_error_t *
1130 tap_init (vlib_main_t * vm)
1132 tap_main_t *tm = &tap_main;
1133 clib_error_t *error = 0;
1135 tm->log_default = vlib_log_register_class ("tap", 0);
1136 vlib_log_debug (tm->log_default, "initialized");
1138 tm->host_mtu_size = 0;
1143 VLIB_INIT_FUNCTION (tap_init);
1146 * fd.io coding-style-patch-verification: ON
1149 * eval: (c-set-style "gnu")