From 206acf84d6a04590e385f427edba1094c3bdf195 Mon Sep 17 00:00:00 2001 From: Mohsin Kazmi Date: Mon, 6 Apr 2020 14:19:54 +0200 Subject: tap: add initial support for tun Type: feature Change-Id: I699a01ac925fe5c475a36032edb7018618bb4dd4 Signed-off-by: Mohsin Kazmi --- src/vnet/devices/tap/cli.c | 54 ++++++++++++++++++++++++++- src/vnet/devices/tap/tap.c | 84 +++++++++++++++++++++++++++++++----------- src/vnet/devices/tap/tap.h | 1 + src/vnet/devices/tap/tapv2.api | 1 + 4 files changed, 118 insertions(+), 22 deletions(-) (limited to 'src/vnet/devices/tap') diff --git a/src/vnet/devices/tap/cli.c b/src/vnet/devices/tap/cli.c index 68b8cef2a21..40086a588aa 100644 --- a/src/vnet/devices/tap/cli.c +++ b/src/vnet/devices/tap/cli.c @@ -96,6 +96,8 @@ tap_create_command_fn (vlib_main_t * vm, unformat_input_t * input, args.tap_flags |= TAP_FLAG_PERSIST; else if (unformat (line_input, "attach")) args.tap_flags |= TAP_FLAG_ATTACH; + else if (unformat (line_input, "tun")) + args.tap_flags |= TAP_FLAG_TUN; else if (unformat (line_input, "hw-addr %U", unformat_ethernet_address, args.mac_addr.bytes)) args.mac_addr_set = 1; @@ -136,7 +138,7 @@ VLIB_CLI_COMMAND (tap_create_command, static) = { "[host-ip6-addr ] [host-ip4-gw ] " "[host-ip6-gw ] [host-mac-addr ] " "[host-if-name ] [host-mtu-size ] [no-gso|gso|csum-offload] " - "[persist] [attach]", + "[persist] [attach] [tun]", .function = tap_create_command_fn, }; /* *INDENT-ON* */ @@ -306,6 +308,56 @@ VLIB_CLI_COMMAND (tap_show_command, static) = { }; /* *INDENT-ON* */ +static clib_error_t * +tun_show_command_fn (vlib_main_t * vm, unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + virtio_main_t *mm = &virtio_main; + virtio_if_t *vif; + vnet_main_t *vnm = vnet_get_main (); + int show_descr = 0; + clib_error_t *error = 0; + u32 hw_if_index, *hw_if_indices = 0; + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + if (unformat + (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index)) + vec_add1 (hw_if_indices, hw_if_index); + else if (unformat (input, "descriptors")) + show_descr = 1; + else + { + error = clib_error_return (0, "unknown input `%U'", + format_unformat_error, input); + goto done; + } + } + + if (vec_len (hw_if_indices) == 0) + { + /* *INDENT-OFF* */ + pool_foreach (vif, mm->interfaces, + vec_add1 (hw_if_indices, vif->hw_if_index); + ); + /* *INDENT-ON* */ + } + + virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_TUN); + +done: + vec_free (hw_if_indices); + return error; +} + +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (tun_show_command, static) = { + .path = "show tun", + .short_help = "show tun {] [descriptors]", + .function = tun_show_command_fn, +}; +/* *INDENT-ON* */ + clib_error_t * tap_cli_init (vlib_main_t * vm) { diff --git a/src/vnet/devices/tap/tap.c b/src/vnet/devices/tap/tap.c index 59c1531a0d2..3b67b53de4a 100644 --- a/src/vnet/devices/tap/tap.c +++ b/src/vnet/devices/tap/tap.c @@ -57,6 +57,14 @@ tap_main_t tap_main; goto error; \ } + /* *INDENT-OFF* */ +VNET_HW_INTERFACE_CLASS (tun_device_hw_interface_class, static) = +{ + .name = "tun-device", + .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, +}; + /* *INDENT-ON* */ + static u32 virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags) @@ -134,7 +142,7 @@ tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) vnet_hw_interface_t *hw; int i; int old_netns_fd = -1; - struct ifreq ifr = {.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR }; + struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR }; struct ifreq get_ifr = {.ifr_flags = 0 }; size_t hdrsz; struct vhost_memory *vhost_mem = 0; @@ -168,7 +176,19 @@ tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) } pool_get_zero (vim->interfaces, vif); - vif->type = VIRTIO_IF_TYPE_TAP; + + if (args->tap_flags & TAP_FLAG_TUN) + { + vif->type = VIRTIO_IF_TYPE_TUN; + ifr.ifr_flags |= IFF_TUN; + args->tap_flags &= ~(TAP_FLAG_GSO | TAP_FLAG_CSUM_OFFLOAD); + } + else + { + vif->type = VIRTIO_IF_TYPE_TAP; + ifr.ifr_flags |= IFF_TAP; + } + vif->dev_instance = vif - vim->interfaces; vif->id = args->id; vif->num_txqs = thm->n_vlib_mains; @@ -404,14 +424,17 @@ tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) } } - if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes)) - ethernet_mac_address_generate (args->host_mac_addr.bytes); - args->error = vnet_netlink_set_link_addr (vif->ifindex, - args->host_mac_addr.bytes); - if (args->error) + if (vif->type == VIRTIO_IF_TYPE_TAP) { - args->rv = VNET_API_ERROR_NETLINK_ERROR; - goto error; + if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes)) + ethernet_mac_address_generate (args->host_mac_addr.bytes); + args->error = vnet_netlink_set_link_addr (vif->ifindex, + args->host_mac_addr.bytes); + if (args->error) + { + args->rv = VNET_API_ERROR_NETLINK_ERROR; + goto error; + } } if (args->host_bridge) @@ -615,11 +638,13 @@ tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) _IOCTL (fd, VHOST_NET_SET_BACKEND, &file); } - if (!args->mac_addr_set) - ethernet_mac_address_generate (args->mac_addr.bytes); - - clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6); + if (vif->type == VIRTIO_IF_TYPE_TAP) + { + if (!args->mac_addr_set) + ethernet_mac_address_generate (args->mac_addr.bytes); + clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6); + } vif->host_if_name = format (0, "%s%c", host_if_name, 0); vif->net_ns = format (0, "%s%c", args->host_namespace, 0); vif->host_bridge = format (0, "%s%c", args->host_bridge, 0); @@ -632,17 +657,28 @@ tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) if (args->host_ip6_prefix_len) clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16); - args->error = ethernet_register_interface (vnm, virtio_device_class.index, - vif->dev_instance, - vif->mac_addr, - &vif->hw_if_index, - virtio_eth_flag_change); - if (args->error) + if (vif->type != VIRTIO_IF_TYPE_TUN) { - args->rv = VNET_API_ERROR_INVALID_REGISTRATION; - goto error; + args->error = + ethernet_register_interface (vnm, virtio_device_class.index, + vif->dev_instance, vif->mac_addr, + &vif->hw_if_index, + virtio_eth_flag_change); + if (args->error) + { + args->rv = VNET_API_ERROR_INVALID_REGISTRATION; + goto error; + } + } + else + { + vif->hw_if_index = vnet_register_interface + (vnm, virtio_device_class.index, + vif->dev_instance /* device instance */ , + tun_device_hw_interface_class.index, vif->dev_instance); + } tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1); sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index); vif->sw_if_index = sw->sw_if_index; @@ -746,6 +782,9 @@ tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index, vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); + if (vif->type == VIRTIO_IF_TYPE_TUN) + return VNET_API_ERROR_UNIMPLEMENTED; + const unsigned int csum_offload_on = TUN_F_CSUM; const unsigned int csum_offload_off = 0; unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off; @@ -801,6 +840,9 @@ tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable) vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); + if (vif->type == VIRTIO_IF_TYPE_TUN) + return VNET_API_ERROR_UNIMPLEMENTED; + const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6; const unsigned int gso_off = 0; unsigned int offload = enable_disable ? gso_on : gso_off; diff --git a/src/vnet/devices/tap/tap.h b/src/vnet/devices/tap/tap.h index 5d087ad9da5..c1ac7a61229 100644 --- a/src/vnet/devices/tap/tap.h +++ b/src/vnet/devices/tap/tap.h @@ -35,6 +35,7 @@ typedef struct #define TAP_FLAG_CSUM_OFFLOAD (1 << 1) #define TAP_FLAG_PERSIST (1 << 2) #define TAP_FLAG_ATTACH (1 << 3) +#define TAP_FLAG_TUN (1 << 4) u8 *host_namespace; u8 *host_if_name; mac_address_t host_mac_addr; diff --git a/src/vnet/devices/tap/tapv2.api b/src/vnet/devices/tap/tapv2.api index ccbbe158160..9113458852f 100644 --- a/src/vnet/devices/tap/tapv2.api +++ b/src/vnet/devices/tap/tapv2.api @@ -30,6 +30,7 @@ enum tap_flags { TAP_FLAG_CSUM_OFFLOAD = 2, TAP_FLAG_PERSIST = 4, TAP_FLAG_ATTACH = 8, + TAP_FLAG_TUN = 16, }; /** \brief Initialize a new tap interface with the given parameters -- cgit 1.2.3-korg