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 <sys/eventfd.h>
26 #include <net/if_arp.h>
30 #include <linux/netlink.h>
31 #include <linux/rtnetlink.h>
33 #include <vlib/vlib.h>
34 #include <vlib/physmem.h>
35 #include <vlib/unix/unix.h>
36 #include <vnet/ethernet/ethernet.h>
37 #include <vnet/ip/ip4_packet.h>
38 #include <vnet/ip/ip6_packet.h>
39 #include <vnet/devices/netlink.h>
40 #include <vnet/devices/virtio/virtio.h>
41 #include <vnet/devices/tap/tap.h>
45 #define tap_log_err(dev, f, ...) \
46 vlib_log (VLIB_LOG_LEVEL_ERR, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
47 #define tap_log_dbg(dev, f, ...) \
48 vlib_log (VLIB_LOG_LEVEL_DEBUG, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__)
50 #define _IOCTL(fd,a,...) \
51 if (ioctl (fd, a, __VA_ARGS__) < 0) \
53 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
54 tap_log_err (vif, "%U", format_clib_error, err); \
59 VNET_HW_INTERFACE_CLASS (tun_device_hw_interface_class, static) =
62 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
67 virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
71 //TODO On MTU change call vnet_netlink_set_if_mtu
76 open_netns_fd (char *netns)
81 if (strncmp (netns, "pid:", 4) == 0)
82 s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0);
83 else if (netns[0] == '/')
84 s = format (0, "%s%c", netns, 0);
86 s = format (0, "/var/run/netns/%s%c", netns, 0);
88 fd = open ((char *) s, O_RDONLY);
93 #define TAP_MAX_INSTANCE 1024
96 tap_free (vlib_main_t * vm, virtio_if_t * vif)
98 virtio_main_t *mm = &virtio_main;
99 tap_main_t *tm = &tap_main;
100 clib_error_t *err = 0;
104 vec_foreach_index (i, vif->vhost_fds) if (vif->vhost_fds[i] != -1)
105 close (vif->vhost_fds[i]);
106 vec_foreach_index (i, vif->rxq_vrings)
107 virtio_vring_free_rx (vm, vif, RX_QUEUE (i));
108 vec_foreach_index (i, vif->txq_vrings)
109 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");
118 vec_foreach_index (i, vif->tap_fds) close (vif->tap_fds[i]);
120 vec_free (vif->vhost_fds);
121 vec_free (vif->rxq_vrings);
122 vec_free (vif->txq_vrings);
123 vec_free (vif->host_if_name);
124 vec_free (vif->net_ns);
125 vec_free (vif->host_bridge);
126 clib_error_free (vif->error);
128 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
129 clib_memset (vif, 0, sizeof (*vif));
130 pool_put (mm->interfaces, vif);
134 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
136 vlib_thread_main_t *thm = vlib_get_thread_main ();
137 vlib_physmem_main_t *vpm = &vm->physmem_main;
138 vnet_main_t *vnm = vnet_get_main ();
139 virtio_main_t *vim = &virtio_main;
140 tap_main_t *tm = &tap_main;
141 vnet_sw_interface_t *sw;
142 vnet_hw_interface_t *hw;
143 int i, num_vhost_queues;
144 int old_netns_fd = -1;
145 struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR };
146 struct ifreq get_ifr = {.ifr_flags = 0 };
148 vhost_memory_t *vhost_mem = 0;
149 virtio_if_t *vif = 0;
150 clib_error_t *err = 0;
151 unsigned int tap_features;
152 int tfd = -1, qfd = -1, vfd = -1, nfd = -1;
153 char *host_if_name = 0;
154 unsigned int offload = 0;
159 if (clib_bitmap_get (tm->tap_ids, args->id))
161 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
162 args->error = clib_error_return (0, "interface already exists");
168 args->id = clib_bitmap_first_clear (tm->tap_ids);
171 if (args->id > TAP_MAX_INSTANCE)
173 args->rv = VNET_API_ERROR_UNSPECIFIED;
174 args->error = clib_error_return (0, "cannot find free interface id");
178 pool_get_zero (vim->interfaces, vif);
180 if (args->tap_flags & TAP_FLAG_TUN)
182 vif->type = VIRTIO_IF_TYPE_TUN;
183 ifr.ifr_flags |= IFF_TUN;
186 * From kernel 4.20, xdp support has been added in tun_sendmsg.
187 * If sndbuf == INT_MAX, vhost batches the packet and processes
188 * them using xdp data path for tun driver. It assumes packets
189 * are ethernet frames (It needs to be fixed).
190 * To avoid xdp data path in tun driver, sndbuf value should
193 sndbuf = INT_MAX - 1;
197 vif->type = VIRTIO_IF_TYPE_TAP;
198 ifr.ifr_flags |= IFF_TAP;
202 vif->dev_instance = vif - vim->interfaces;
204 vif->num_txqs = thm->n_vlib_mains;
205 vif->num_rxqs = clib_max (args->num_rx_queues, 1);
207 if (args->tap_flags & TAP_FLAG_ATTACH)
209 if (args->host_if_name != NULL)
211 host_if_name = (char *) args->host_if_name;
212 clib_memcpy (ifr.ifr_name, host_if_name,
213 clib_min (IFNAMSIZ, vec_len (host_if_name)));
217 args->rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
218 err = clib_error_return (0, "host_if_name is not provided");
221 if (args->host_namespace)
223 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
224 if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1)
226 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
227 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
228 args->host_namespace);
231 if (setns (nfd, CLONE_NEWNET) == -1)
233 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
234 args->error = clib_error_return_unix (0, "setns '%s'",
235 args->host_namespace);
241 if ((tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
243 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
244 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
247 vec_add1 (vif->tap_fds, tfd);
248 tap_log_dbg (vif, "open tap fd %d", tfd);
250 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
251 tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
252 if ((tap_features & IFF_VNET_HDR) == 0)
254 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
255 args->error = clib_error_return (0, "vhost-net backend not available");
259 if ((tap_features & IFF_MULTI_QUEUE) == 0)
261 if (vif->num_rxqs > 1)
263 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
264 args->error = clib_error_return (0, "multiqueue not supported");
267 vif->num_rxqs = vif->num_txqs = 1;
270 ifr.ifr_flags |= IFF_MULTI_QUEUE;
272 hdrsz = sizeof (virtio_net_hdr_v1_t);
273 if (args->tap_flags & TAP_FLAG_GSO)
275 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
276 vif->gso_enabled = 1;
278 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
280 offload = TUN_F_CSUM;
281 vif->csum_offload_enabled = 1;
284 _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
285 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
286 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
288 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
289 tap_log_dbg (vif, "ifindex %d", vif->ifindex);
291 if (!args->host_if_name)
292 host_if_name = ifr.ifr_ifrn.ifrn_name;
294 host_if_name = (char *) args->host_if_name;
297 * unset the persistence when attaching to existing
300 if (args->tap_flags & TAP_FLAG_ATTACH)
302 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 0);
303 tap_log_dbg (vif, "TUNSETPERSIST: unset");
306 /* set the persistence */
307 if (args->tap_flags & TAP_FLAG_PERSIST)
309 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 1);
310 tap_log_dbg (vif, "TUNSETPERSIST: set");
312 /* verify persistence is set, read the flags */
313 _IOCTL (tfd, TUNGETIFF, (void *) &get_ifr);
314 tap_log_dbg (vif, "TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
315 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
317 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
318 args->error = clib_error_return (0, "persistence not supported");
323 /* create additional queues on the linux side.
324 * we create as many linux queue pairs as we have rx queues
326 for (i = 1; i < vif->num_rxqs; i++)
328 if ((qfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
330 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
331 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
334 _IOCTL (qfd, TUNSETIFF, (void *) &ifr);
335 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", qfd,
336 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
337 vec_add1 (vif->tap_fds, qfd);
340 for (i = 0; i < vif->num_rxqs; i++)
342 tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u",
343 vif->tap_fds[i], hdrsz);
344 _IOCTL (vif->tap_fds[i], TUNSETVNETHDRSZ, &hdrsz);
346 tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", vif->tap_fds[i],
348 _IOCTL (vif->tap_fds[i], TUNSETSNDBUF, &sndbuf);
350 tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", vif->tap_fds[i],
352 _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload);
354 if (fcntl (vif->tap_fds[i], F_SETFL, O_NONBLOCK) < 0)
356 err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
357 tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
362 /* open as many vhost-net fds as required and set ownership */
363 num_vhost_queues = clib_max (vif->num_rxqs, vif->num_txqs);
364 for (i = 0; i < num_vhost_queues; i++)
366 if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
368 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
369 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
372 vec_add1 (vif->vhost_fds, vfd);
373 virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
374 _IOCTL (vfd, VHOST_SET_OWNER, 0);
375 virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
378 _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
379 virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
380 vif->remote_features);
382 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
384 args->rv = VNET_API_ERROR_UNSUPPORTED;
385 args->error = clib_error_return (0, "vhost-net backend doesn't support "
386 "VIRTIO_NET_F_MRG_RXBUF feature");
390 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
393 args->rv = VNET_API_ERROR_UNSUPPORTED;
394 args->error = clib_error_return (0, "vhost-net backend doesn't support "
395 "VIRTIO_RING_F_INDIRECT_DESC feature");
399 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
401 args->rv = VNET_API_ERROR_UNSUPPORTED;
402 args->error = clib_error_return (0, "vhost-net backend doesn't support "
403 "VIRTIO_F_VERSION_1 features");
407 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
408 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
409 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
411 virtio_set_net_hdr_size (vif);
413 if (!(args->tap_flags & TAP_FLAG_ATTACH))
415 /* if namespace is specified, all further netlink messages should be executed
416 after we change our net namespace */
417 if (args->host_namespace)
419 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
420 if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1)
422 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
423 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
424 args->host_namespace);
427 args->error = vnet_netlink_set_link_netns (vif->ifindex, nfd,
431 args->rv = VNET_API_ERROR_NETLINK_ERROR;
434 if (setns (nfd, CLONE_NEWNET) == -1)
436 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
437 args->error = clib_error_return_unix (0, "setns '%s'",
438 args->host_namespace);
441 if ((vif->ifindex = if_nametoindex (host_if_name)) == 0)
443 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
444 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
449 else if (host_if_name)
452 vnet_netlink_set_link_name (vif->ifindex, host_if_name);
455 args->rv = VNET_API_ERROR_NETLINK_ERROR;
461 if (vif->type == VIRTIO_IF_TYPE_TAP)
463 if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes))
464 ethernet_mac_address_generate (args->host_mac_addr.bytes);
465 args->error = vnet_netlink_set_link_addr (vif->ifindex,
466 args->host_mac_addr.bytes);
469 args->rv = VNET_API_ERROR_NETLINK_ERROR;
473 if (args->host_bridge)
475 args->error = vnet_netlink_set_link_master (vif->ifindex,
480 args->rv = VNET_API_ERROR_NETLINK_ERROR;
486 if (args->host_ip4_prefix_len)
488 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
489 &args->host_ip4_addr,
490 args->host_ip4_prefix_len);
493 args->rv = VNET_API_ERROR_NETLINK_ERROR;
498 if (args->host_ip6_prefix_len)
500 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
501 &args->host_ip6_addr,
502 args->host_ip6_prefix_len);
505 args->rv = VNET_API_ERROR_NETLINK_ERROR;
510 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
513 args->rv = VNET_API_ERROR_NETLINK_ERROR;
517 if (args->host_ip4_gw_set)
519 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
522 args->rv = VNET_API_ERROR_NETLINK_ERROR;
527 if (args->host_ip6_gw_set)
529 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
532 args->rv = VNET_API_ERROR_NETLINK_ERROR;
537 if (args->host_mtu_set)
540 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
543 args->rv = VNET_API_ERROR_NETLINK_ERROR;
547 else if (tm->host_mtu_size != 0)
550 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
553 args->rv = VNET_API_ERROR_NETLINK_ERROR;
556 args->host_mtu_set = 1;
557 args->host_mtu_size = tm->host_mtu_size;
560 /* switch back to old net namespace */
561 if (args->host_namespace)
563 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
565 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
566 args->error = clib_error_return_unix (0, "setns '%s'",
567 args->host_namespace);
572 for (i = 0; i < num_vhost_queues; i++)
574 if (i < vif->num_rxqs && (args->error =
575 virtio_vring_init (vm, vif, RX_QUEUE (i),
578 args->rv = VNET_API_ERROR_INIT_FAILED;
582 if (i < vif->num_txqs && (args->error =
583 virtio_vring_init (vm, vif, TX_QUEUE (i),
586 args->rv = VNET_API_ERROR_INIT_FAILED;
591 /* setup features and memtable */
592 i = sizeof (vhost_memory_t) + sizeof (vhost_memory_region_t);
593 vhost_mem = clib_mem_alloc (i);
594 clib_memset (vhost_mem, 0, i);
595 vhost_mem->nregions = 1;
596 vhost_mem->regions[0].memory_size = vpm->max_size;
597 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
598 vhost_mem->regions[0].userspace_addr =
599 vhost_mem->regions[0].guest_phys_addr;
601 for (i = 0; i < vhost_mem->nregions; i++)
602 virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
603 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
604 vhost_mem->regions[0].memory_size,
605 vhost_mem->regions[0].guest_phys_addr,
606 vhost_mem->regions[0].userspace_addr);
609 for (i = 0; i < num_vhost_queues; i++)
611 int fd = vif->vhost_fds[i];
612 _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
613 virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
615 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
616 virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
619 /* finish initializing queue pair */
620 for (i = 0; i < num_vhost_queues * 2; i++)
622 vhost_vring_addr_t addr = { 0 };
623 vhost_vring_state_t state = { 0 };
624 vhost_vring_file_t file = { 0 };
625 virtio_vring_t *vring;
627 int fd = vif->vhost_fds[qp];
631 if (qp >= vif->num_txqs)
633 vring = vec_elt_at_index (vif->txq_vrings, qp);
637 if (qp >= vif->num_rxqs)
639 vring = vec_elt_at_index (vif->rxq_vrings, qp);
642 addr.index = state.index = file.index = vring->queue_id & 1;
643 state.num = vring->size;
644 virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
645 state.index, state.num);
646 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
649 addr.desc_user_addr = pointer_to_uword (vring->desc);
650 addr.avail_user_addr = pointer_to_uword (vring->avail);
651 addr.used_user_addr = pointer_to_uword (vring->used);
653 virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
654 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
655 "used_user_addr 0x%lx", fd, addr.index,
656 addr.flags, addr.desc_user_addr, addr.avail_user_addr,
657 addr.used_user_addr);
658 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
660 file.fd = vring->call_fd;
661 virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
662 fd, file.index, file.fd);
663 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
665 file.fd = vring->kick_fd;
666 virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
667 fd, file.index, file.fd);
668 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
670 file.fd = vif->tap_fds[qp % vif->num_rxqs];
671 virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
672 fd, file.index, file.fd);
673 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
676 if (vif->type == VIRTIO_IF_TYPE_TAP)
678 if (!args->mac_addr_set)
679 ethernet_mac_address_generate (args->mac_addr.bytes);
681 clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6);
682 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
684 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
685 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
686 vif->host_mtu_size = args->host_mtu_size;
687 vif->tap_flags = args->tap_flags;
688 clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6);
689 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
690 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
691 if (args->host_ip4_prefix_len)
692 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
693 if (args->host_ip6_prefix_len)
694 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
696 if (vif->type != VIRTIO_IF_TYPE_TUN)
699 ethernet_register_interface (vnm, virtio_device_class.index,
700 vif->dev_instance, vif->mac_addr,
702 virtio_eth_flag_change);
705 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
712 vif->hw_if_index = vnet_register_interface
713 (vnm, virtio_device_class.index,
714 vif->dev_instance /* device instance */ ,
715 tun_device_hw_interface_class.index, vif->dev_instance);
718 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
719 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
720 vif->sw_if_index = sw->sw_if_index;
721 args->sw_if_index = vif->sw_if_index;
723 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
724 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
725 if (args->tap_flags & TAP_FLAG_GSO)
727 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
728 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
730 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
732 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
734 if ((args->tap_flags & TAP_FLAG_GSO)
735 && (args->tap_flags & TAP_FLAG_GRO_COALESCE))
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_IF_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 virtio_set_packet_coalesce (vif);
910 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
912 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
913 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
915 vif->packet_coalesce = 0;
921 clib_warning ("Error %s gso on sw_if_index %d",
922 enable_disable ? "enabling" : "disabling", sw_if_index);
923 return VNET_API_ERROR_SYSCALL_ERROR_3;
929 tap_dump_ifs (tap_interface_details_t ** out_tapids)
931 vnet_main_t *vnm = vnet_get_main ();
932 virtio_main_t *mm = &virtio_main;
934 virtio_vring_t *vring;
935 vnet_hw_interface_t *hi;
936 tap_interface_details_t *r_tapids = NULL;
937 tap_interface_details_t *tapid = NULL;
940 pool_foreach (vif, mm->interfaces,
941 if ((vif->type != VIRTIO_IF_TYPE_TAP)
942 && (vif->type != VIRTIO_IF_TYPE_TUN))
944 vec_add2(r_tapids, tapid, 1);
945 clib_memset (tapid, 0, sizeof (*tapid));
947 tapid->sw_if_index = vif->sw_if_index;
948 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
949 clib_memcpy(tapid->dev_name, hi->name,
950 MIN (ARRAY_LEN (tapid->dev_name) - 1, vec_len (hi->name)));
951 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
952 tapid->rx_ring_sz = vring->size;
953 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
954 tapid->tx_ring_sz = vring->size;
955 tapid->tap_flags = vif->tap_flags;
956 clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6);
957 if (vif->host_if_name)
959 clib_memcpy(tapid->host_if_name, vif->host_if_name,
960 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
961 vec_len (vif->host_if_name)));
965 clib_memcpy(tapid->host_namespace, vif->net_ns,
966 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
967 vec_len (vif->net_ns)));
969 if (vif->host_bridge)
971 clib_memcpy(tapid->host_bridge, vif->host_bridge,
972 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
973 vec_len (vif->host_bridge)));
975 if (vif->host_ip4_prefix_len)
976 clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4);
977 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
978 if (vif->host_ip6_prefix_len)
979 clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16);
980 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
981 tapid->host_mtu_size = vif->host_mtu_size;
985 *out_tapids = r_tapids;
990 static clib_error_t *
991 tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
993 tap_main_t *tm = &tap_main;
995 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
997 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
1000 return clib_error_return (0, "unknown input `%U'",
1001 format_unformat_error, input);
1007 /* tap { host-mtu <size> } configuration. */
1008 VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
1010 static clib_error_t *
1011 tap_init (vlib_main_t * vm)
1013 tap_main_t *tm = &tap_main;
1014 clib_error_t *error = 0;
1016 tm->log_default = vlib_log_register_class ("tap", 0);
1017 vlib_log_debug (tm->log_default, "initialized");
1019 tm->host_mtu_size = 0;
1024 VLIB_INIT_FUNCTION (tap_init);
1027 * fd.io coding-style-patch-verification: ON
1030 * eval: (c-set-style "gnu")