summaryrefslogtreecommitdiffstats
path: root/src/vnet
diff options
context:
space:
mode:
authorMohsin Kazmi <sykazmi@cisco.com>2020-04-06 14:19:54 +0200
committerDamjan Marion <dmarion@me.com>2020-04-17 11:28:14 +0000
commit206acf84d6a04590e385f427edba1094c3bdf195 (patch)
treec724f0d2fedd494e163394a6a339c18dc72c0a37 /src/vnet
parent95eb01fdc65b692065ace6d76870d8cc9f3e2c84 (diff)
tap: add initial support for tun
Type: feature Change-Id: I699a01ac925fe5c475a36032edb7018618bb4dd4 Signed-off-by: Mohsin Kazmi <sykazmi@cisco.com>
Diffstat (limited to 'src/vnet')
-rw-r--r--src/vnet/devices/tap/cli.c54
-rw-r--r--src/vnet/devices/tap/tap.c84
-rw-r--r--src/vnet/devices/tap/tap.h1
-rw-r--r--src/vnet/devices/tap/tapv2.api1
-rw-r--r--src/vnet/devices/virtio/device.c4
-rw-r--r--src/vnet/devices/virtio/format.c4
-rw-r--r--src/vnet/devices/virtio/node.c59
-rw-r--r--src/vnet/devices/virtio/virtio.c18
-rw-r--r--src/vnet/devices/virtio/virtio.h12
9 files changed, 190 insertions, 47 deletions
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 <ip6-addr>] [host-ip4-gw <ip4-addr>] "
"[host-ip6-gw <ip6-addr>] [host-mac-addr <host-mac-address>] "
"[host-if-name <name>] [host-mtu-size <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 {<interface>] [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
diff --git a/src/vnet/devices/virtio/device.c b/src/vnet/devices/virtio/device.c
index 1ee94806842..9d04474ada0 100644
--- a/src/vnet/devices/virtio/device.c
+++ b/src/vnet/devices/virtio/device.c
@@ -21,6 +21,7 @@
#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
+#include <vnet/vnet.h>
#include <vnet/ethernet/ethernet.h>
#include <vnet/gso/gso.h>
#include <vnet/ip/ip4_packet.h>
@@ -343,7 +344,7 @@ add_buffer_to_slot (vlib_main_t * vm, virtio_if_t * vif,
id->len = b->current_length;
}
}
- else /* VIRTIO_IF_TYPE_TAP */
+ else /* VIRTIO_IF_TYPE_[TAP | TUN] */
{
d->addr = pointer_to_uword (id);
/* first buffer in chain */
@@ -612,6 +613,7 @@ VNET_DEVICE_CLASS (virtio_device_class) = {
.subif_add_del_function = virtio_subif_add_del_function,
.rx_mode_change_function = virtio_interface_rx_mode_change,
};
+
/* *INDENT-ON* */
/*
diff --git a/src/vnet/devices/virtio/format.c b/src/vnet/devices/virtio/format.c
index 729635de35b..fe46d92cba9 100644
--- a/src/vnet/devices/virtio/format.c
+++ b/src/vnet/devices/virtio/format.c
@@ -36,6 +36,8 @@ format_virtio_device_name (u8 * s, va_list * args)
s = format (s, "virtio-%x/%x/%x/%x", vif->pci_addr.domain,
vif->pci_addr.bus, vif->pci_addr.slot,
vif->pci_addr.function);
+ else if (vif->type == VIRTIO_IF_TYPE_TUN)
+ s = format (s, "tun%u", vif->id);
else
s = format (s, "virtio-%lu", vif->dev_instance);
@@ -49,6 +51,8 @@ format_virtio_log_name (u8 * s, va_list * args)
if (vif->type == VIRTIO_IF_TYPE_TAP)
s = format (s, "tap%u", vif->id);
+ else if (vif->type == VIRTIO_IF_TYPE_TUN)
+ s = format (s, "tun%u", vif->id);
else if (vif->type == VIRTIO_IF_TYPE_PCI)
s = format (s, "%U", format_vlib_pci_addr, &vif->pci_addr);
else
diff --git a/src/vnet/devices/virtio/node.c b/src/vnet/devices/virtio/node.c
index 3bb23662c50..8d7c9360b00 100644
--- a/src/vnet/devices/virtio/node.c
+++ b/src/vnet/devices/virtio/node.c
@@ -143,27 +143,44 @@ more:
static_always_inline void
virtio_needs_csum (vlib_buffer_t * b0, struct virtio_net_hdr_v1 *hdr,
- u8 * l4_proto, u8 * l4_hdr_sz)
+ u8 * l4_proto, u8 * l4_hdr_sz, virtio_if_type_t type)
{
if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)
{
- ethernet_header_t *eh =
- (ethernet_header_t *) vlib_buffer_get_current (b0);
- u16 ethertype = clib_net_to_host_u16 (eh->type);
- u16 l2hdr_sz = sizeof (ethernet_header_t);
-
- if (ethernet_frame_is_tagged (ethertype))
+ u16 ethertype = 0, l2hdr_sz = 0;
+ if (type != VIRTIO_IF_TYPE_TUN)
{
- ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
+ ethernet_header_t *eh =
+ (ethernet_header_t *) vlib_buffer_get_current (b0);
+ ethertype = clib_net_to_host_u16 (eh->type);
+ l2hdr_sz = sizeof (ethernet_header_t);
- ethertype = clib_net_to_host_u16 (vlan->type);
- l2hdr_sz += sizeof (*vlan);
- if (ethertype == ETHERNET_TYPE_VLAN)
+ if (ethernet_frame_is_tagged (ethertype))
{
- vlan++;
+ ethernet_vlan_header_t *vlan =
+ (ethernet_vlan_header_t *) (eh + 1);
+
ethertype = clib_net_to_host_u16 (vlan->type);
l2hdr_sz += sizeof (*vlan);
+ if (ethertype == ETHERNET_TYPE_VLAN)
+ {
+ vlan++;
+ ethertype = clib_net_to_host_u16 (vlan->type);
+ l2hdr_sz += sizeof (*vlan);
+ }
+ }
+ }
+ else
+ {
+ switch (b0->data[0] & 0xf0)
+ {
+ case 0x40:
+ ethertype = ETHERNET_TYPE_IP4;
+ break;
+ case 0x60:
+ ethertype = ETHERNET_TYPE_IP6;
+ break;
}
}
@@ -285,7 +302,7 @@ virtio_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
if (checksum_offload_enabled)
- virtio_needs_csum (b0, hdr, &l4_proto, &l4_hdr_sz);
+ virtio_needs_csum (b0, hdr, &l4_proto, &l4_hdr_sz, vif->type);
if (gso_enabled)
fill_gso_buffer_flags (b0, hdr, l4_proto, l4_hdr_sz);
@@ -321,6 +338,22 @@ virtio_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
n_left--;
}
}
+ if (PREDICT_FALSE (vif->type == VIRTIO_IF_TYPE_TUN))
+ {
+ switch (b0->data[0] & 0xf0)
+ {
+ case 0x40:
+ next0 = VNET_DEVICE_INPUT_NEXT_IP4_INPUT;
+ break;
+ case 0x60:
+ next0 = VNET_DEVICE_INPUT_NEXT_IP6_INPUT;
+ break;
+ default:
+ next0 = VNET_DEVICE_INPUT_NEXT_DROP;
+ break;
+ }
+ }
+
if (PREDICT_FALSE (vif->per_interface_next_index != ~0))
next0 = vif->per_interface_next_index;
diff --git a/src/vnet/devices/virtio/virtio.c b/src/vnet/devices/virtio/virtio.c
index d78c9114e5c..00a588af346 100644
--- a/src/vnet/devices/virtio/virtio.c
+++ b/src/vnet/devices/virtio/virtio.c
@@ -294,7 +294,7 @@ virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type)
vlib_cli_output (vm, " PCI Address: %U", format_vlib_pci_addr,
&vif->pci_addr);
}
- if (type == VIRTIO_IF_TYPE_TAP)
+ if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
{
u8 *str = 0;
if (vif->host_if_name)
@@ -304,8 +304,9 @@ virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type)
if (vif->host_mtu_size)
vlib_cli_output (vm, " host-mtu-size \"%d\"",
vif->host_mtu_size);
- vlib_cli_output (vm, " host-mac-addr: %U",
- format_ethernet_address, vif->host_mac_addr);
+ if (type == VIRTIO_IF_TYPE_TAP)
+ vlib_cli_output (vm, " host-mac-addr: %U",
+ format_ethernet_address, vif->host_mac_addr);
vec_foreach_index (i, vif->vhost_fds)
str = format (str, " %d", vif->vhost_fds[i]);
@@ -315,8 +316,9 @@ virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type)
}
vlib_cli_output (vm, " gso-enabled %d", vif->gso_enabled);
vlib_cli_output (vm, " csum-enabled %d", vif->csum_offload_enabled);
- vlib_cli_output (vm, " Mac Address: %U", format_ethernet_address,
- vif->mac_addr);
+ if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_PCI))
+ vlib_cli_output (vm, " Mac Address: %U", format_ethernet_address,
+ vif->mac_addr);
vlib_cli_output (vm, " Device instance: %u", vif->dev_instance);
vlib_cli_output (vm, " flags 0x%x", vif->flags);
flag_entry = (struct feat_struct *) &flags_array;
@@ -366,7 +368,7 @@ virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type)
" avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
vring->avail->flags, vring->avail->idx,
vring->used->flags, vring->used->idx);
- if (type == VIRTIO_IF_TYPE_TAP)
+ if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
{
vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
vring->call_fd);
@@ -401,7 +403,7 @@ virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type)
" avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
vring->avail->flags, vring->avail->idx,
vring->used->flags, vring->used->idx);
- if (type == VIRTIO_IF_TYPE_TAP)
+ if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
{
vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
vring->call_fd);
@@ -437,7 +439,7 @@ virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type)
" avail.flags 0x%x avail.idx %d used.flags 0x%x used.idx %d",
vring->avail->flags, vring->avail->idx,
vring->used->flags, vring->used->idx);
- if (type == VIRTIO_IF_TYPE_TAP)
+ if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN))
{
vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
vring->call_fd);
diff --git a/src/vnet/devices/virtio/virtio.h b/src/vnet/devices/virtio/virtio.h
index e28922bf654..3e8f9416483 100644
--- a/src/vnet/devices/virtio/virtio.h
+++ b/src/vnet/devices/virtio/virtio.h
@@ -80,11 +80,17 @@ typedef enum
#define TX_QUEUE_ACCESS(X) (X/2)
#define RX_QUEUE_ACCESS(X) (X/2)
+#define foreach_virtio_if_types \
+ _ (TAP, 1) \
+ _ (TUN, 2) \
+ _ (PCI, 3)
+
typedef enum
{
- VIRTIO_IF_TYPE_TAP = 1,
- VIRTIO_IF_TYPE_PCI,
- VIRTIO_IF_N_TYPES,
+#define _(a, b) VIRTIO_IF_TYPE_##a = (1 << b),
+ foreach_virtio_if_types
+#undef _
+ VIRTIO_IF_N_TYPES = (1 << 4),
} virtio_if_type_t;