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 virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
65 //TODO On MTU change call vnet_netlink_set_if_mtu
70 open_netns_fd (char *netns)
75 if (strncmp (netns, "pid:", 4) == 0)
76 s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0);
77 else if (netns[0] == '/')
78 s = format (0, "%s%c", netns, 0);
80 s = format (0, "/var/run/netns/%s%c", netns, 0);
82 fd = open ((char *) s, O_RDONLY);
87 #define TAP_MAX_INSTANCE 1024
90 tap_free (vlib_main_t * vm, virtio_if_t * vif)
92 virtio_main_t *mm = &virtio_main;
93 tap_main_t *tm = &tap_main;
97 vec_foreach_index (i, vif->vhost_fds) if (vif->vhost_fds[i] != -1)
98 close (vif->vhost_fds[i]);
99 vec_foreach_index (i, vif->rxq_vrings)
100 virtio_vring_free_rx (vm, vif, RX_QUEUE (i));
101 vec_foreach_index (i, vif->txq_vrings)
102 virtio_vring_free_tx (vm, vif, TX_QUEUE (i));
105 if (vif->tap_fd != -1)
108 vec_free (vif->vhost_fds);
109 vec_free (vif->rxq_vrings);
110 vec_free (vif->txq_vrings);
111 vec_free (vif->host_if_name);
112 vec_free (vif->net_ns);
113 vec_free (vif->host_bridge);
114 clib_error_free (vif->error);
116 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
117 clib_memset (vif, 0, sizeof (*vif));
118 pool_put (mm->interfaces, vif);
122 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
124 vlib_thread_main_t *thm = vlib_get_thread_main ();
125 vlib_physmem_main_t *vpm = &vm->physmem_main;
126 vnet_main_t *vnm = vnet_get_main ();
127 virtio_main_t *vim = &virtio_main;
128 tap_main_t *tm = &tap_main;
129 vnet_sw_interface_t *sw;
130 vnet_hw_interface_t *hw;
132 int old_netns_fd = -1;
133 struct ifreq ifr = {.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR };
135 struct vhost_memory *vhost_mem = 0;
136 virtio_if_t *vif = 0;
137 clib_error_t *err = 0;
138 unsigned int tap_features;
139 int tfd, vfd, nfd = -1;
140 char *host_if_name = 0;
141 unsigned int offload = 0;
146 if (clib_bitmap_get (tm->tap_ids, args->id))
148 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
149 args->error = clib_error_return (0, "interface already exists");
155 args->id = clib_bitmap_first_clear (tm->tap_ids);
158 if (args->id > TAP_MAX_INSTANCE)
160 args->rv = VNET_API_ERROR_UNSPECIFIED;
161 args->error = clib_error_return (0, "cannot find free interface id");
165 pool_get_zero (vim->interfaces, vif);
166 vif->type = VIRTIO_IF_TYPE_TAP;
167 vif->dev_instance = vif - vim->interfaces;
169 vif->num_txqs = thm->n_vlib_mains;
170 vif->num_rxqs = args->num_rx_queues;
171 num_q_pairs = clib_max (vif->num_rxqs, vif->num_txqs);
173 if ((vif->tap_fd = tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
175 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
176 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
179 tap_log_dbg (vif, "open tap fd %d", tfd);
181 _IOCTL (tfd, TUNGETFEATURES, &tap_features);
182 tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features);
183 if ((tap_features & IFF_VNET_HDR) == 0)
185 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
186 args->error = clib_error_return (0, "vhost-net backend not available");
190 if ((tap_features & IFF_MULTI_QUEUE) == 0)
192 if (args->num_rx_queues > 1)
194 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
195 args->error = clib_error_return (0, "multiqueue not supported");
198 vif->num_rxqs = vif->num_txqs = num_q_pairs = 1;
201 ifr.ifr_flags |= IFF_MULTI_QUEUE;
203 hdrsz = sizeof (struct virtio_net_hdr_v1);
204 if (args->tap_flags & TAP_FLAG_GSO)
206 offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
207 vif->gso_enabled = 1;
209 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
211 offload = TUN_F_CSUM;
212 vif->csum_offload_enabled = 1;
215 _IOCTL (tfd, TUNSETIFF, (void *) &ifr);
216 tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd,
217 ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags);
219 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
220 tap_log_dbg (vif, "ifindex %d", vif->ifindex);
222 if (!args->host_if_name)
223 host_if_name = ifr.ifr_ifrn.ifrn_name;
225 host_if_name = (char *) args->host_if_name;
227 if (fcntl (tfd, F_SETFL, O_NONBLOCK) < 0)
229 err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)");
230 tap_log_err (vif, "set nonblocking: %U", format_clib_error, err);
234 tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u", tfd, hdrsz);
235 _IOCTL (tfd, TUNSETVNETHDRSZ, &hdrsz);
238 tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", tfd, i);
239 _IOCTL (tfd, TUNSETSNDBUF, &i);
241 tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", tfd, offload);
242 _IOCTL (tfd, TUNSETOFFLOAD, offload);
244 /* open vhost-net fd for each queue pair and set ownership */
245 for (i = 0; i < num_q_pairs; i++)
247 if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
249 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
250 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
253 vec_add1 (vif->vhost_fds, vfd);
254 virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i);
255 _IOCTL (vfd, VHOST_SET_OWNER, 0);
256 virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd);
259 _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features);
260 virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx",
261 vif->remote_features);
263 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0)
265 args->rv = VNET_API_ERROR_UNSUPPORTED;
266 args->error = clib_error_return (0, "vhost-net backend doesn't support "
267 "VIRTIO_NET_F_MRG_RXBUF feature");
271 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) ==
274 args->rv = VNET_API_ERROR_UNSUPPORTED;
275 args->error = clib_error_return (0, "vhost-net backend doesn't support "
276 "VIRTIO_RING_F_INDIRECT_DESC feature");
280 if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0)
282 args->rv = VNET_API_ERROR_UNSUPPORTED;
283 args->error = clib_error_return (0, "vhost-net backend doesn't support "
284 "VIRTIO_F_VERSION_1 features");
288 vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF);
289 vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1);
290 vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC);
292 virtio_set_net_hdr_size (vif);
294 /* if namespace is specified, all further netlink messages should be executed
295 after we change our net namespace */
296 if (args->host_namespace)
298 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
299 if ((nfd = open_netns_fd ((char *) args->host_namespace)) == -1)
301 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
302 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
303 args->host_namespace);
306 args->error = vnet_netlink_set_link_netns (vif->ifindex, nfd,
310 args->rv = VNET_API_ERROR_NETLINK_ERROR;
313 if (setns (nfd, CLONE_NEWNET) == -1)
315 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
316 args->error = clib_error_return_unix (0, "setns '%s'",
317 args->host_namespace);
320 if ((vif->ifindex = if_nametoindex (host_if_name)) == 0)
322 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
323 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
332 args->error = vnet_netlink_set_link_name (vif->ifindex,
336 args->rv = VNET_API_ERROR_NETLINK_ERROR;
342 if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes))
343 ethernet_mac_address_generate (args->host_mac_addr.bytes);
344 args->error = vnet_netlink_set_link_addr (vif->ifindex,
345 args->host_mac_addr.bytes);
348 args->rv = VNET_API_ERROR_NETLINK_ERROR;
352 if (args->host_bridge)
354 args->error = vnet_netlink_set_link_master (vif->ifindex,
355 (char *) args->host_bridge);
358 args->rv = VNET_API_ERROR_NETLINK_ERROR;
363 if (args->host_ip4_prefix_len)
365 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
366 &args->host_ip4_addr,
367 args->host_ip4_prefix_len);
370 args->rv = VNET_API_ERROR_NETLINK_ERROR;
375 if (args->host_ip6_prefix_len)
377 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
378 &args->host_ip6_addr,
379 args->host_ip6_prefix_len);
382 args->rv = VNET_API_ERROR_NETLINK_ERROR;
387 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
390 args->rv = VNET_API_ERROR_NETLINK_ERROR;
394 if (args->host_ip4_gw_set)
396 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
399 args->rv = VNET_API_ERROR_NETLINK_ERROR;
404 if (args->host_ip6_gw_set)
406 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
409 args->rv = VNET_API_ERROR_NETLINK_ERROR;
414 /* switch back to old net namespace */
415 if (args->host_namespace)
417 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
419 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
420 args->error = clib_error_return_unix (0, "setns '%s'",
421 args->host_namespace);
426 if (args->host_mtu_set)
429 vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size);
432 args->rv = VNET_API_ERROR_NETLINK_ERROR;
436 else if (tm->host_mtu_size != 0)
439 vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size);
442 args->rv = VNET_API_ERROR_NETLINK_ERROR;
445 args->host_mtu_set = 1;
446 args->host_mtu_size = tm->host_mtu_size;
449 for (i = 0; i < num_q_pairs; i++)
451 if (i < vif->num_rxqs && (args->error =
452 virtio_vring_init (vm, vif, RX_QUEUE (i),
455 args->rv = VNET_API_ERROR_INIT_FAILED;
459 if (i < vif->num_txqs && (args->error =
460 virtio_vring_init (vm, vif, TX_QUEUE (i),
463 args->rv = VNET_API_ERROR_INIT_FAILED;
468 /* setup features and memtable */
469 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
470 vhost_mem = clib_mem_alloc (i);
471 clib_memset (vhost_mem, 0, i);
472 vhost_mem->nregions = 1;
473 vhost_mem->regions[0].memory_size = vpm->max_size;
474 vhost_mem->regions[0].guest_phys_addr = vpm->base_addr;
475 vhost_mem->regions[0].userspace_addr =
476 vhost_mem->regions[0].guest_phys_addr;
478 for (i = 0; i < vhost_mem->nregions; i++)
479 virtio_log_debug (vif, "memtable region %u memory_size 0x%lx "
480 "guest_phys_addr 0x%lx userspace_addr 0x%lx", i,
481 vhost_mem->regions[0].memory_size,
482 vhost_mem->regions[0].guest_phys_addr,
483 vhost_mem->regions[0].userspace_addr);
486 for (i = 0; i < num_q_pairs; i++)
488 int fd = vif->vhost_fds[i];
489 _IOCTL (fd, VHOST_SET_FEATURES, &vif->features);
490 virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx",
492 _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem);
493 virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd);
496 /* finish initializing queue pair */
497 for (i = 0; i < num_q_pairs * 2; i++)
499 struct vhost_vring_addr addr = { 0 };
500 struct vhost_vring_state state = { 0 };
501 struct vhost_vring_file file = { 0 };
502 virtio_vring_t *vring;
504 int fd = vif->vhost_fds[qp];
508 if (qp >= vif->num_txqs)
510 vring = vec_elt_at_index (vif->txq_vrings, qp);
514 if (qp >= vif->num_rxqs)
516 vring = vec_elt_at_index (vif->rxq_vrings, qp);
519 addr.index = state.index = file.index = vring->queue_id & 1;
520 state.num = vring->size;
521 virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd,
522 state.index, state.num);
523 _IOCTL (fd, VHOST_SET_VRING_NUM, &state);
526 addr.desc_user_addr = pointer_to_uword (vring->desc);
527 addr.avail_user_addr = pointer_to_uword (vring->avail);
528 addr.used_user_addr = pointer_to_uword (vring->used);
530 virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x "
531 "desc_user_addr 0x%lx avail_user_addr 0x%lx "
532 "used_user_addr 0x%lx", fd, addr.index,
533 addr.flags, addr.desc_user_addr, addr.avail_user_addr,
534 addr.used_user_addr);
535 _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr);
537 file.fd = vring->call_fd;
538 virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d",
539 fd, file.index, file.fd);
540 _IOCTL (fd, VHOST_SET_VRING_CALL, &file);
542 file.fd = vring->kick_fd;
543 virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d",
544 fd, file.index, file.fd);
545 _IOCTL (fd, VHOST_SET_VRING_KICK, &file);
548 virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d",
549 fd, file.index, file.fd);
550 _IOCTL (fd, VHOST_NET_SET_BACKEND, &file);
553 if (!args->mac_addr_set)
554 ethernet_mac_address_generate (args->mac_addr.bytes);
556 clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6);
558 vif->host_if_name = format (0, "%s%c", host_if_name, 0);
559 vif->net_ns = format (0, "%s%c", args->host_namespace, 0);
560 vif->host_bridge = format (0, "%s%c", args->host_bridge, 0);
561 vif->host_mtu_size = args->host_mtu_size;
562 clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6);
563 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
564 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
565 if (args->host_ip4_prefix_len)
566 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
567 if (args->host_ip6_prefix_len)
568 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
570 args->error = ethernet_register_interface (vnm, virtio_device_class.index,
574 virtio_eth_flag_change);
577 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
581 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
582 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
583 vif->sw_if_index = sw->sw_if_index;
584 args->sw_if_index = vif->sw_if_index;
586 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
587 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
588 if (args->tap_flags & TAP_FLAG_GSO)
590 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
591 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
593 else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD)
595 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
597 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
598 virtio_input_node.index);
600 for (i = 0; i < vif->num_rxqs; i++)
602 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, i, ~0);
603 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, i,
604 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
607 vif->per_interface_next_index = ~0;
608 virtio_vring_set_numa_node (vm, vif, RX_QUEUE (0));
609 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
610 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
611 VNET_HW_INTERFACE_FLAG_LINK_UP);
612 vif->cxq_vring = NULL;
619 ASSERT (args->error == 0);
621 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
627 clib_mem_free (vhost_mem);
628 if (old_netns_fd != -1)
629 close (old_netns_fd);
635 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
637 vnet_main_t *vnm = vnet_get_main ();
638 virtio_main_t *mm = &virtio_main;
641 vnet_hw_interface_t *hw;
643 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
644 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
645 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
647 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
649 if (vif->type != VIRTIO_IF_TYPE_TAP)
650 return VNET_API_ERROR_INVALID_INTERFACE;
652 /* bring down the interface */
653 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
654 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
655 for (i = 0; i < vif->num_rxqs; i++)
656 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, i);
658 ethernet_delete_interface (vnm, vif->hw_if_index);
659 vif->hw_if_index = ~0;
667 tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index,
670 vnet_main_t *vnm = vnet_get_main ();
671 virtio_main_t *mm = &virtio_main;
673 vnet_hw_interface_t *hw;
674 clib_error_t *err = 0;
676 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
678 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
679 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
681 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
683 const unsigned int csum_offload_on = TUN_F_CSUM;
684 const unsigned int csum_offload_off = 0;
685 unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off;
686 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
687 vif->gso_enabled = 0;
688 vif->csum_offload_enabled = enable_disable ? 1 : 0;
690 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
692 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO;
697 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD) ==
700 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
705 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD) !=
708 hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
715 clib_warning ("Error %s checksum offload on sw_if_index %d",
716 enable_disable ? "enabling" : "disabling", sw_if_index);
717 return VNET_API_ERROR_SYSCALL_ERROR_3;
723 tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable)
725 vnet_main_t *vnm = vnet_get_main ();
726 virtio_main_t *mm = &virtio_main;
728 vnet_hw_interface_t *hw;
729 clib_error_t *err = 0;
731 hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
733 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
734 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
736 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
738 const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6;
739 const unsigned int gso_off = 0;
740 unsigned int offload = enable_disable ? gso_on : gso_off;
741 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
742 vif->gso_enabled = enable_disable ? 1 : 0;
743 vif->csum_offload_enabled = 0;
746 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0)
748 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
749 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
754 if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0)
756 hw->flags &= ~(VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO |
757 VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD);
764 clib_warning ("Error %s gso on sw_if_index %d",
765 enable_disable ? "enabling" : "disabling", sw_if_index);
766 return VNET_API_ERROR_SYSCALL_ERROR_3;
772 tap_dump_ifs (tap_interface_details_t ** out_tapids)
774 vnet_main_t *vnm = vnet_get_main ();
775 virtio_main_t *mm = &virtio_main;
777 virtio_vring_t *vring;
778 vnet_hw_interface_t *hi;
779 tap_interface_details_t *r_tapids = NULL;
780 tap_interface_details_t *tapid = NULL;
783 pool_foreach (vif, mm->interfaces,
784 if (vif->type != VIRTIO_IF_TYPE_TAP)
786 vec_add2(r_tapids, tapid, 1);
787 clib_memset (tapid, 0, sizeof (*tapid));
789 tapid->sw_if_index = vif->sw_if_index;
790 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
791 clib_memcpy(tapid->dev_name, hi->name,
792 MIN (ARRAY_LEN (tapid->dev_name) - 1,
793 strlen ((const char *) hi->name)));
794 vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0));
795 tapid->rx_ring_sz = vring->size;
796 vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0));
797 tapid->tx_ring_sz = vring->size;
798 clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6);
799 if (vif->host_if_name)
801 clib_memcpy(tapid->host_if_name, vif->host_if_name,
802 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
803 strlen ((const char *) vif->host_if_name)));
807 clib_memcpy(tapid->host_namespace, vif->net_ns,
808 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
809 strlen ((const char *) vif->net_ns)));
811 if (vif->host_bridge)
813 clib_memcpy(tapid->host_bridge, vif->host_bridge,
814 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
815 strlen ((const char *) vif->host_bridge)));
817 if (vif->host_ip4_prefix_len)
818 clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4);
819 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
820 if (vif->host_ip6_prefix_len)
821 clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16);
822 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
823 tapid->host_mtu_size = vif->host_mtu_size;
827 *out_tapids = r_tapids;
832 static clib_error_t *
833 tap_mtu_config (vlib_main_t * vm, unformat_input_t * input)
835 tap_main_t *tm = &tap_main;
837 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
839 if (unformat (input, "host-mtu %d", &tm->host_mtu_size))
842 return clib_error_return (0, "unknown input `%U'",
843 format_unformat_error, input);
849 /* tap { host-mtu <size> } configuration. */
850 VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap");
852 static clib_error_t *
853 tap_init (vlib_main_t * vm)
855 tap_main_t *tm = &tap_main;
856 clib_error_t *error = 0;
858 tm->log_default = vlib_log_register_class ("tap", 0);
859 vlib_log_debug (tm->log_default, "initialized");
861 tm->host_mtu_size = 0;
866 VLIB_INIT_FUNCTION (tap_init);
869 * fd.io coding-style-patch-verification: ON
872 * eval: (c-set-style "gnu")