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_fd, TUNSETPERSIST, (void *) (uintptr_t) 0);
115 tap_log_dbg (vif, "TUNSETPERSIST: unset");
117 if (vif->tap_fd != -1)
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;
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 struct vhost_memory *vhost_mem = 0;
149 virtio_if_t *vif = 0;
150 clib_error_t *err = 0;
151 unsigned int tap_features;
152 int tfd, vfd, 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;
184 args->tap_flags &= ~(TAP_FLAG_GSO | TAP_FLAG_CSUM_OFFLOAD);
188 vif->type = VIRTIO_IF_TYPE_TAP;
189 ifr.ifr_flags |= IFF_TAP;
192 vif->dev_instance = vif - vim->interfaces;
194 vif->num_txqs = thm->n_vlib_mains;
195 vif->num_rxqs = args->num_rx_queues;
196 num_q_pairs = clib_max (vif->num_rxqs, vif->num_txqs);
198 if (args->tap_flags & TAP_FLAG_ATTACH)
200 if (args->host_if_name != NULL)
202 host_if_name = (char *) args->host_if_name;
203 clib_memcpy (ifr.ifr_name, host_if_name,
204 clib_min (IFNAMSIZ, strlen (host_if_name)));
208 args->rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
209 err = clib_error_return (0, "host_if_name is not provided");
212 if (args->host_namespace)
214 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
215 if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1)
217 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
218 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
219 args->host_namespace);
222 if (setns (nfd, CLONE_NEWNET) == -1)
224 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
225 args->error = clib_error_return_unix (0, "setns '%s'",
226 args->host_namespace);
231 if ((vif->tap_fd = tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
233 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
234 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
237 tap_log_dbg (vif, "open tap fd %d", tfd);
239 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
240 tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
241 if ((tap_features & IFF_VNET_HDR) == 0)
243 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
244 args->error = clib_error_return (0, "vhost-net backend not available");
248 if ((tap_features & IFF_MULTI_QUEUE) == 0)
250 if (args->num_rx_queues > 1)
252 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
253 args->error = clib_error_return (0, "multiqueue not supported");
256 vif->num_rxqs = vif->num_txqs = num_q_pairs = 1;
259 ifr.ifr_flags |= IFF_MULTI_QUEUE;
261 hdrsz = sizeof (struct virtio_net_hdr_v1);
262 if (args->tap_flags & TAP_FLAG_GSO)
264 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
265 vif->gso_enabled = 1;
267 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
269 offload = TUN_F_CSUM;
270 vif->csum_offload_enabled = 1;
273 _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
274 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
275 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
277 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
278 tap_log_dbg (vif, "ifindex %d", vif->ifindex);
280 if (!args->host_if_name)
281 host_if_name = ifr.ifr_ifrn.ifrn_name;
283 host_if_name = (char *) args->host_if_name;
285 if (fcntl (tfd, F_SETFL, O_NONBLOCK) < 0)
287 err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
288 tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
293 * unset the persistence when attaching to existing
296 if (args->tap_flags & TAP_FLAG_ATTACH)
298 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 0);
299 tap_log_dbg (vif, "TUNSETPERSIST: unset");
302 /* set the persistence */
303 if (args->tap_flags & TAP_FLAG_PERSIST)
305 _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 1);
306 tap_log_dbg (vif, "TUNSETPERSIST: set");
308 /* verify persistence is set, read the flags */
309 _IOCTL (tfd, TUNGETIFF, (void *) &get_ifr);
310 tap_log_dbg (vif, "TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags);
311 if ((get_ifr.ifr_flags & IFF_PERSIST) == 0)
313 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
314 args->error = clib_error_return (0, "persistence not supported");
319 tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u", tfd, hdrsz);
320 _IOCTL (tfd, TUNSETVNETHDRSZ, &hdrsz);
323 tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", tfd, i);
324 _IOCTL (tfd, TUNSETSNDBUF, &i);
326 tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", tfd, offload);
327 _IOCTL (tfd, TUNSETOFFLOAD, offload);
329 /* open vhost-net fd for each queue pair and set ownership */
330 for (i = 0; i < num_q_pairs; i++)
332 if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
334 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
335 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
338 vec_add1 (vif->vhost_fds, vfd);
339 virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
340 _IOCTL (vfd, VHOST_SET_OWNER, 0);
341 virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
344 _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
345 virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
346 vif->remote_features);
348 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
350 args->rv = VNET_API_ERROR_UNSUPPORTED;
351 args->error = clib_error_return (0, "vhost-net backend doesn't support "
352 "VIRTIO_NET_F_MRG_RXBUF feature");
356 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
359 args->rv = VNET_API_ERROR_UNSUPPORTED;
360 args->error = clib_error_return (0, "vhost-net backend doesn't support "
361 "VIRTIO_RING_F_INDIRECT_DESC feature");
365 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
367 args->rv = VNET_API_ERROR_UNSUPPORTED;
368 args->error = clib_error_return (0, "vhost-net backend doesn't support "
369 "VIRTIO_F_VERSION_1 features");
373 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
374 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
375 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
377 virtio_set_net_hdr_size (vif);
379 if (!(args->tap_flags & TAP_FLAG_ATTACH))
381 /* if namespace is specified, all further netlink messages should be executed
382 after we change our net namespace */
383 if (args->host_namespace)
385 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
386 if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1)
388 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
389 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
390 args->host_namespace);
393 args->error = vnet_netlink_set_link_netns (vif->ifindex, nfd,
397 args->rv = VNET_API_ERROR_NETLINK_ERROR;
400 if (setns (nfd, CLONE_NEWNET) == -1)
402 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
403 args->error = clib_error_return_unix (0, "setns '%s'",
404 args->host_namespace);
407 if ((vif->ifindex = if_nametoindex (host_if_name)) == 0)
409 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
410 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
415 else if (host_if_name)
418 vnet_netlink_set_link_name (vif->ifindex, host_if_name);
421 args->rv = VNET_API_ERROR_NETLINK_ERROR;
427 if (vif->type == VIRTIO_IF_TYPE_TAP)
429 if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes))
430 ethernet_mac_address_generate (args->host_mac_addr.bytes);
431 args->error = vnet_netlink_set_link_addr (vif->ifindex,
432 args->host_mac_addr.bytes);
435 args->rv = VNET_API_ERROR_NETLINK_ERROR;
440 if (args->host_bridge)
442 args->error = vnet_netlink_set_link_master (vif->ifindex,
443 (char *) args->host_bridge);
446 args->rv = VNET_API_ERROR_NETLINK_ERROR;
451 if (args->host_ip4_prefix_len)
453 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
454 &args->host_ip4_addr,
455 args->host_ip4_prefix_len);
458 args->rv = VNET_API_ERROR_NETLINK_ERROR;
463 if (args->host_ip6_prefix_len)
465 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
466 &args->host_ip6_addr,
467 args->host_ip6_prefix_len);
470 args->rv = VNET_API_ERROR_NETLINK_ERROR;
475 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
478 args->rv = VNET_API_ERROR_NETLINK_ERROR;
482 if (args->host_ip4_gw_set)
484 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
487 args->rv = VNET_API_ERROR_NETLINK_ERROR;
492 if (args->host_ip6_gw_set)
494 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
497 args->rv = VNET_API_ERROR_NETLINK_ERROR;
502 if (args->host_mtu_set)
505 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
508 args->rv = VNET_API_ERROR_NETLINK_ERROR;
512 else if (tm->host_mtu_size != 0)
515 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
518 args->rv = VNET_API_ERROR_NETLINK_ERROR;
521 args->host_mtu_set = 1;
522 args->host_mtu_size = tm->host_mtu_size;
525 /* switch back to old net namespace */
526 if (args->host_namespace)
528 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
530 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
531 args->error = clib_error_return_unix (0, "setns '%s'",
532 args->host_namespace);
537 for (i = 0; i < num_q_pairs; i++)
539 if (i < vif->num_rxqs && (args->error =
540 virtio_vring_init (vm, vif, RX_QUEUE (i),
543 args->rv = VNET_API_ERROR_INIT_FAILED;
547 if (i < vif->num_txqs && (args->error =
548 virtio_vring_init (vm, vif, TX_QUEUE (i),
551 args->rv = VNET_API_ERROR_INIT_FAILED;
556 /* setup features and memtable */
557 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
558 vhost_mem = clib_mem_alloc (i);
559 clib_memset (vhost_mem, 0, i);
560 vhost_mem->nregions = 1;
561 vhost_mem->regions[0].memory_size = vpm->max_size;
562 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
563 vhost_mem->regions[0].userspace_addr =
564 vhost_mem->regions[0].guest_phys_addr;
566 for (i = 0; i < vhost_mem->nregions; i++)
567 virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
568 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
569 vhost_mem->regions[0].memory_size,
570 vhost_mem->regions[0].guest_phys_addr,
571 vhost_mem->regions[0].userspace_addr);
574 for (i = 0; i < num_q_pairs; i++)
576 int fd = vif->vhost_fds[i];
577 _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
578 virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
580 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
581 virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
584 /* finish initializing queue pair */
585 for (i = 0; i < num_q_pairs * 2; i++)
587 struct vhost_vring_addr addr = { 0 };
588 struct vhost_vring_state state = { 0 };
589 struct vhost_vring_file file = { 0 };
590 virtio_vring_t *vring;
592 int fd = vif->vhost_fds[qp];
596 if (qp >= vif->num_txqs)
598 vring = vec_elt_at_index (vif->txq_vrings, qp);
602 if (qp >= vif->num_rxqs)
604 vring = vec_elt_at_index (vif->rxq_vrings, qp);
607 addr.index = state.index = file.index = vring->queue_id & 1;
608 state.num = vring->size;
609 virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
610 state.index, state.num);
611 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
614 addr.desc_user_addr = pointer_to_uword (vring->desc);
615 addr.avail_user_addr = pointer_to_uword (vring->avail);
616 addr.used_user_addr = pointer_to_uword (vring->used);
618 virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
619 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
620 "used_user_addr 0x%lx", fd, addr.index,
621 addr.flags, addr.desc_user_addr, addr.avail_user_addr,
622 addr.used_user_addr);
623 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
625 file.fd = vring->call_fd;
626 virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
627 fd, file.index, file.fd);
628 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
630 file.fd = vring->kick_fd;
631 virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
632 fd, file.index, file.fd);
633 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
636 virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
637 fd, file.index, file.fd);
638 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
641 if (vif->type == VIRTIO_IF_TYPE_TAP)
643 if (!args->mac_addr_set)
644 ethernet_mac_address_generate (args->mac_addr.bytes);
646 clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6);
648 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
649 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
650 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
651 vif->host_mtu_size = args->host_mtu_size;
652 clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6);
653 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
654 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
655 if (args->host_ip4_prefix_len)
656 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
657 if (args->host_ip6_prefix_len)
658 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
660 if (vif->type != VIRTIO_IF_TYPE_TUN)
663 ethernet_register_interface (vnm, virtio_device_class.index,
664 vif->dev_instance, vif->mac_addr,
666 virtio_eth_flag_change);
669 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
676 vif->hw_if_index = vnet_register_interface
677 (vnm, virtio_device_class.index,
678 vif->dev_instance /* device instance */ ,
679 tun_device_hw_interface_class.index, vif->dev_instance);
682 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
683 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
684 vif->sw_if_index = sw->sw_if_index;
685 args->sw_if_index = vif->sw_if_index;
687 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
688 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
689 if (args->tap_flags & TAP_FLAG_GSO)
691 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
692 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
694 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
696 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
698 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
699 virtio_input_node.index);
701 for (i = 0; i < vif->num_rxqs; i++)
703 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
704 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
705 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
706 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (i));
709 vif->per_interface_next_index = ~0;
710 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
711 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
712 VNET_HW_INTERFACE_FLAG_LINK_UP);
713 vif->cxq_vring = NULL;
720 ASSERT (args->error == 0);
722 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
725 tap_log_err (vif, "%U", format_clib_error, args->error);
729 clib_mem_free (vhost_mem);
730 if (old_netns_fd != -1)
731 close (old_netns_fd);
737 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
739 vnet_main_t *vnm = vnet_get_main ();
740 virtio_main_t *mm = &virtio_main;
743 vnet_hw_interface_t *hw;
745 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
746 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
747 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
749 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
751 if (vif->type != VIRTIO_IF_TYPE_TAP)
752 return VNET_API_ERROR_INVALID_INTERFACE;
754 /* bring down the interface */
755 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
756 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
757 for (i = 0; i < vif->num_rxqs; i++)
758 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
760 ethernet_delete_interface (vnm, vif->hw_if_index);
761 vif->hw_if_index = ~0;
769 tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index,
772 vnet_main_t *vnm = vnet_get_main ();
773 virtio_main_t *mm = &virtio_main;
775 vnet_hw_interface_t *hw;
776 clib_error_t *err = 0;
778 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
780 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
781 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
783 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
785 if (vif->type == VIRTIO_IF_TYPE_TUN)
786 return VNET_API_ERROR_UNIMPLEMENTED;
788 const unsigned int csum_offload_on = TUN_F_CSUM;
789 const unsigned int csum_offload_off = 0;
790 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
791 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
792 vif->gso_enabled = 0;
793 vif->csum_offload_enabled = enable_disable ? 1 : 0;
795 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
797 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
802 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD) ==
805 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
810 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD) !=
813 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
820 clib_warning ("Error %s checksum offload on sw_if_index %d",
821 enable_disable ? "enabling" : "disabling", sw_if_index);
822 return VNET_API_ERROR_SYSCALL_ERROR_3;
828 tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable)
830 vnet_main_t *vnm = vnet_get_main ();
831 virtio_main_t *mm = &virtio_main;
833 vnet_hw_interface_t *hw;
834 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 if (vif->type == VIRTIO_IF_TYPE_TUN)
844 return VNET_API_ERROR_UNIMPLEMENTED;
846 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
847 const unsigned int gso_off = 0;
848 unsigned int offload = enable_disable ? gso_on : gso_off;
849 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
850 vif->gso_enabled = enable_disable ? 1 : 0;
851 vif->csum_offload_enabled = 0;
854 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0)
856 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
857 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
862 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
864 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
865 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
872 clib_warning ("Error %s gso on sw_if_index %d",
873 enable_disable ? "enabling" : "disabling", sw_if_index);
874 return VNET_API_ERROR_SYSCALL_ERROR_3;
880 tap_dump_ifs (tap_interface_details_t ** out_tapids)
882 vnet_main_t *vnm = vnet_get_main ();
883 virtio_main_t *mm = &virtio_main;
885 virtio_vring_t *vring;
886 vnet_hw_interface_t *hi;
887 tap_interface_details_t *r_tapids = NULL;
888 tap_interface_details_t *tapid = NULL;
891 pool_foreach (vif, mm->interfaces,
892 if (vif->type != VIRTIO_IF_TYPE_TAP)
894 vec_add2(r_tapids, tapid, 1);
895 clib_memset (tapid, 0, sizeof (*tapid));
897 tapid->sw_if_index = vif->sw_if_index;
898 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
899 clib_memcpy(tapid->dev_name, hi->name,
900 MIN (ARRAY_LEN (tapid->dev_name) - 1,
901 strlen ((const char *) hi->name)));
902 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
903 tapid->rx_ring_sz = vring->size;
904 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
905 tapid->tx_ring_sz = vring->size;
906 clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6);
907 if (vif->host_if_name)
909 clib_memcpy(tapid->host_if_name, vif->host_if_name,
910 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
911 strlen ((const char *) vif->host_if_name)));
915 clib_memcpy(tapid->host_namespace, vif->net_ns,
916 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
917 strlen ((const char *) vif->net_ns)));
919 if (vif->host_bridge)
921 clib_memcpy(tapid->host_bridge, vif->host_bridge,
922 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
923 strlen ((const char *) vif->host_bridge)));
925 if (vif->host_ip4_prefix_len)
926 clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4);
927 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
928 if (vif->host_ip6_prefix_len)
929 clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16);
930 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
931 tapid->host_mtu_size = vif->host_mtu_size;
935 *out_tapids = r_tapids;
940 static clib_error_t *
941 tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
943 tap_main_t *tm = &tap_main;
945 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
947 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
950 return clib_error_return (0, "unknown input `%U'",
951 format_unformat_error, input);
957 /* tap { host-mtu <size> } configuration. */
958 VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
960 static clib_error_t *
961 tap_init (vlib_main_t * vm)
963 tap_main_t *tm = &tap_main;
964 clib_error_t *error = 0;
966 tm->log_default = vlib_log_register_class ("tap", 0);
967 vlib_log_debug (tm->log_default, "initialized");
969 tm->host_mtu_size = 0;
974 VLIB_INIT_FUNCTION (tap_init);
977 * fd.io coding-style-patch-verification: ON
980 * eval: (c-set-style "gnu")