aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/devices/tap
diff options
context:
space:
mode:
authorDamjan Marion <damarion@cisco.com>2017-12-04 15:25:58 +0100
committerDamjan Marion <dmarion.lists@gmail.com>2017-12-04 17:08:28 +0000
commitc99b4cd1c3aae9ca3bda0595a5cce4ea5e6ba84f (patch)
tree7364a65a5790a43a63f2fe3f231fd0094a962e11 /src/vnet/devices/tap
parentffffb15fb5d6dcd5f0afd69652aa33a4e9894152 (diff)
tap_v2: move code to vnet/devices/tap
virtio backend stays in vnet/devices/virtio Change-Id: Idbf04f1c645a809ed408670ba330662859fe9309 Signed-off-by: Damjan Marion <damarion@cisco.com>
Diffstat (limited to 'src/vnet/devices/tap')
-rw-r--r--src/vnet/devices/tap/cli.c306
-rw-r--r--src/vnet/devices/tap/tap.c352
-rw-r--r--src/vnet/devices/tap/tap.h63
-rw-r--r--src/vnet/devices/tap/tapv2.api110
-rw-r--r--src/vnet/devices/tap/tapv2_api.c237
5 files changed, 1068 insertions, 0 deletions
diff --git a/src/vnet/devices/tap/cli.c b/src/vnet/devices/tap/cli.c
new file mode 100644
index 00000000000..f7fc1e63be7
--- /dev/null
+++ b/src/vnet/devices/tap/cli.c
@@ -0,0 +1,306 @@
+/*
+ *------------------------------------------------------------------
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *------------------------------------------------------------------
+ */
+#include <stdint.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
+#include <inttypes.h>
+
+#include <vlib/vlib.h>
+#include <vlib/unix/unix.h>
+#include <vnet/ethernet/ethernet.h>
+#include <vnet/ip/ip4_packet.h>
+#include <vnet/ip/ip6_packet.h>
+#include <vnet/ip/format.h>
+#include <linux/virtio_net.h>
+#include <linux/vhost.h>
+#include <vnet/devices/virtio/virtio.h>
+#include <vnet/devices/tap/tap.h>
+
+static clib_error_t *
+tap_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
+ vlib_cli_command_t * cmd)
+{
+ unformat_input_t _line_input, *line_input = &_line_input;
+ tap_create_if_args_t args = { 0 };
+ int ip_addr_set = 0;
+
+ /* Get a line of input. */
+ if (!unformat_user (input, unformat_line_input, line_input))
+ return clib_error_return (0, "Missing name <interface>");
+
+ while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (line_input, "name %s", &args.name))
+ ;
+ else if (unformat (line_input, "host-ns %s", &args.host_namespace))
+ ;
+ else if (unformat (line_input, "host-bridge %s", &args.host_bridge))
+ ;
+ else if (unformat (line_input, "host-ip4-addr %U/%d",
+ unformat_ip4_address, &args.host_ip4_addr,
+ &args.host_ip4_prefix_len))
+ ip_addr_set = 1;
+ else if (unformat (line_input, "host-ip6-addr %U/%d",
+ unformat_ip6_address, &args.host_ip6_addr,
+ &args.host_ip6_prefix_len))
+ ip_addr_set = 1;
+ else if (unformat (line_input, "rx-ring-size %d", &args.rx_ring_sz))
+ ;
+ else if (unformat (line_input, "tx-ring-size %d", &args.tx_ring_sz))
+ ;
+ else if (unformat (line_input, "hw-addr %U",
+ unformat_ethernet_address, args.hw_addr))
+ args.hw_addr_set = 1;
+ else
+ return clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, input);
+ }
+ unformat_free (line_input);
+
+ if (ip_addr_set && args.host_bridge)
+ return clib_error_return (0, "Please specify either host ip address or "
+ "host bridge");
+
+ tap_create_if (vm, &args);
+
+ vec_free (args.name);
+ vec_free (args.host_namespace);
+ vec_free (args.host_bridge);
+
+ return args.error;
+
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (tap_create_command, static) = {
+ .path = "create tap",
+ .short_help = "create tap {name <if-name>} [hw-addr <mac-address>] "
+ "[rx-ring-size <size>] [tx-ring-size <size>] [host-ns <netns>] "
+ "[host-bridge <bridge-name>] [host-ip4-addr <ip4addr/mask>] "
+ "[host-ip6-addr <ip6-addr]",
+ .function = tap_create_command_fn,
+};
+/* *INDENT-ON* */
+
+static clib_error_t *
+tap_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
+ vlib_cli_command_t * cmd)
+{
+ unformat_input_t _line_input, *line_input = &_line_input;
+ u32 sw_if_index = ~0;
+ vnet_main_t *vnm = vnet_get_main ();
+ int rv;
+
+ /* Get a line of input. */
+ if (!unformat_user (input, unformat_line_input, line_input))
+ return clib_error_return (0, "Missing <interface>");
+
+ while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (line_input, "sw_if_index %d", &sw_if_index))
+ ;
+ else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
+ vnm, &sw_if_index))
+ ;
+ else
+ return clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, input);
+ }
+ unformat_free (line_input);
+
+ if (sw_if_index == ~0)
+ return clib_error_return (0,
+ "please specify interface name or sw_if_index");
+
+ rv = tap_delete_if (vm, sw_if_index);
+ if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
+ return clib_error_return (0, "not a tap interface");
+ else if (rv != 0)
+ return clib_error_return (0, "error on deleting tap interface");
+
+ return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (tap_delete__command, static) =
+{
+ .path = "delete tap",
+ .short_help = "delete tap {<interface> | sw_if_index <sw_idx>}",
+ .function = tap_delete_command_fn,
+};
+/* *INDENT-ON* */
+
+static clib_error_t *
+tap_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;
+ virtio_vring_t *vring;
+ int i, j;
+ struct feat_struct
+ {
+ u8 bit;
+ char *str;
+ };
+ struct feat_struct *feat_entry;
+
+ static struct feat_struct feat_array[] = {
+#define _(s,b) { .str = #s, .bit = b, },
+ foreach_virtio_net_features
+#undef _
+ {.str = NULL}
+ };
+
+ struct feat_struct *flag_entry;
+ static struct feat_struct flags_array[] = {
+#define _(b,e,s) { .bit = b, .str = s, },
+ foreach_virtio_if_flag
+#undef _
+ {.str = NULL}
+ };
+
+ 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* */
+ }
+
+ for (hw_if_index = 0; hw_if_index < vec_len (hw_if_indices); hw_if_index++)
+ {
+ vnet_hw_interface_t *hi =
+ vnet_get_hw_interface (vnm, hw_if_indices[hw_if_index]);
+ vif = pool_elt_at_index (mm->interfaces, hi->dev_instance);
+ vlib_cli_output (vm, "interface %U", format_vnet_sw_if_index_name,
+ vnm, vif->sw_if_index);
+ if (vif->name)
+ vlib_cli_output (vm, " name \"%s\"", vif->name);
+ if (vif->net_ns)
+ vlib_cli_output (vm, " host-ns \"%s\"", vif->net_ns);
+ vlib_cli_output (vm, " flags 0x%x", vif->flags);
+ flag_entry = (struct feat_struct *) &flags_array;
+ while (flag_entry->str)
+ {
+ if (vif->flags & (1ULL << flag_entry->bit))
+ vlib_cli_output (vm, " %s (%d)", flag_entry->str,
+ flag_entry->bit);
+ flag_entry++;
+ }
+ vlib_cli_output (vm, " fd %d", vif->fd);
+ vlib_cli_output (vm, " tap-fd %d", vif->tap_fd);
+ vlib_cli_output (vm, " features 0x%lx", vif->features);
+ feat_entry = (struct feat_struct *) &feat_array;
+ while (feat_entry->str)
+ {
+ if (vif->features & (1ULL << feat_entry->bit))
+ vlib_cli_output (vm, " %s (%d)", feat_entry->str,
+ feat_entry->bit);
+ feat_entry++;
+ }
+ vlib_cli_output (vm, " remote-features 0x%lx", vif->remote_features);
+ feat_entry = (struct feat_struct *) &feat_array;
+ while (feat_entry->str)
+ {
+ if (vif->remote_features & (1ULL << feat_entry->bit))
+ vlib_cli_output (vm, " %s (%d)", feat_entry->str,
+ feat_entry->bit);
+ feat_entry++;
+ }
+ vec_foreach_index (i, vif->vrings)
+ {
+ // RX = 0, TX = 1
+ vring = vec_elt_at_index (vif->vrings, i);
+ vlib_cli_output (vm, " Virtqueue (%s)", (i & 1) ? "TX" : "RX");
+ vlib_cli_output (vm, " qsz %d, last_used_idx %d, desc_in_use %d",
+ vring->size, vring->last_used_idx,
+ vring->desc_in_use);
+ vlib_cli_output (vm,
+ " 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);
+ vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd,
+ vring->call_fd);
+ if (show_descr)
+ {
+ vlib_cli_output (vm, "\n descriptor table:\n");
+ vlib_cli_output (vm,
+ " id addr len flags next user_addr\n");
+ vlib_cli_output (vm,
+ " ===== ================== ===== ====== ===== ==================\n");
+ vring = vif->vrings;
+ for (j = 0; j < vring->size; j++)
+ {
+ struct vring_desc *desc = &vring->desc[j];
+ vlib_cli_output (vm,
+ " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n",
+ j, desc->addr,
+ desc->len,
+ desc->flags, desc->next, desc->addr);
+ }
+ }
+ }
+ }
+done:
+ vec_free (hw_if_indices);
+ return error;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (tap_show_command, static) = {
+ .path = "show tap",
+ .short_help = "show tap {<interface>] [descriptors]",
+ .function = tap_show_command_fn,
+};
+/* *INDENT-ON* */
+
+clib_error_t *
+tap_cli_init (vlib_main_t * vm)
+{
+ return 0;
+}
+
+VLIB_INIT_FUNCTION (tap_cli_init);
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/devices/tap/tap.c b/src/vnet/devices/tap/tap.c
new file mode 100644
index 00000000000..b4004f70189
--- /dev/null
+++ b/src/vnet/devices/tap/tap.c
@@ -0,0 +1,352 @@
+/*
+ *------------------------------------------------------------------
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *------------------------------------------------------------------
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <net/if.h>
+#include <linux/if_tun.h>
+#include <sys/ioctl.h>
+#include <linux/virtio_net.h>
+#include <linux/vhost.h>
+#include <sys/eventfd.h>
+
+#include <linux/netlink.h>
+#include <linux/rtnetlink.h>
+
+#include <vlib/vlib.h>
+#include <vlib/unix/unix.h>
+#include <vnet/ethernet/ethernet.h>
+#include <vnet/ip/ip4_packet.h>
+#include <vnet/ip/ip6_packet.h>
+#include <vnet/devices/netlink.h>
+#include <vnet/devices/virtio/virtio.h>
+#include <vnet/devices/tap/tap.h>
+
+#define _IOCTL(fd,a,...) \
+ if (ioctl (fd, a, __VA_ARGS__) < 0) \
+ { \
+ err = clib_error_return_unix (0, "ioctl(" #a ")"); \
+ goto error; \
+ }
+
+static u32
+virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
+ u32 flags)
+{
+ /* nothing for now */
+ //TODO On MTU change call vnet_netlink_set_if_mtu
+ return 0;
+}
+
+void
+tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
+{
+ vnet_main_t *vnm = vnet_get_main ();
+ virtio_main_t *vim = &virtio_main;
+ vnet_sw_interface_t *sw;
+ vnet_hw_interface_t *hw;
+ int i, fd = -1;
+ struct ifreq ifr;
+ size_t hdrsz;
+ struct vhost_memory *vhost_mem = 0;
+ virtio_if_t *vif = 0;
+ clib_error_t *err = 0;
+
+ memset (&ifr, 0, sizeof (ifr));
+ pool_get (vim->interfaces, vif);
+ vif->dev_instance = vif - vim->interfaces;
+ vif->tap_fd = -1;
+
+ if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
+ {
+ args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
+ args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
+ goto error;
+ }
+
+ _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features);
+
+ if ((vif->remote_features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) == 0)
+ {
+ args->rv = VNET_API_ERROR_UNSUPPORTED;
+ args->error = clib_error_return (0, "vhost-net backend doesn't support "
+ "VIRTIO_NET_F_MRG_RXBUF feature");
+ goto error;
+ }
+
+ if ((vif->remote_features & (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) == 0)
+ {
+ args->rv = VNET_API_ERROR_UNSUPPORTED;
+ args->error = clib_error_return (0, "vhost-net backend doesn't support "
+ "VIRTIO_RING_F_INDIRECT_DESC feature");
+ goto error;
+ }
+
+ if ((vif->remote_features & (1ULL << VIRTIO_F_VERSION_1)) == 0)
+ {
+ args->rv = VNET_API_ERROR_UNSUPPORTED;
+ args->error = clib_error_return (0, "vhost-net backend doesn't support "
+ "VIRTIO_F_VERSION_1 features");
+ goto error;
+ }
+
+ vif->features |= 1ULL << VIRTIO_NET_F_MRG_RXBUF;
+ vif->features |= 1ULL << VIRTIO_F_VERSION_1;
+ vif->features |= 1ULL << VIRTIO_RING_F_INDIRECT_DESC;
+
+ _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features);
+
+ if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
+ {
+ args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
+ args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
+ goto error;
+ }
+
+ ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
+ strncpy (ifr.ifr_ifrn.ifrn_name, (char *) args->name, IF_NAMESIZE - 1);
+ _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr);
+
+ vif->ifindex = if_nametoindex ((char *) args->name);
+
+ unsigned int offload = 0;
+ hdrsz = sizeof (struct virtio_net_hdr_v1);
+ _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
+ _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz);
+ _IOCTL (vif->fd, VHOST_SET_OWNER, 0);
+
+ if (args->host_bridge)
+ {
+ int master_ifindex = if_nametoindex ((char *) args->host_bridge);
+ args->error = vnet_netlink_set_if_master (vif->ifindex, master_ifindex);
+ if (args->error)
+ {
+ args->rv = VNET_API_ERROR_NETLINK_ERROR;
+ goto error;
+ }
+ }
+
+ if (args->host_namespace)
+ {
+ args->error = vnet_netlink_set_if_namespace (vif->ifindex,
+ (char *)
+ args->host_namespace);
+ if (args->error)
+ {
+ args->rv = VNET_API_ERROR_NETLINK_ERROR;
+ goto error;
+ }
+ }
+
+ if (args->host_ip4_prefix_len)
+ {
+ args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
+ &args->host_ip4_addr,
+ args->host_ip4_prefix_len);
+ if (args->error)
+ {
+ args->rv = VNET_API_ERROR_NETLINK_ERROR;
+ goto error;
+ }
+ }
+
+ if (args->host_ip6_prefix_len)
+ {
+ args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
+ &args->host_ip6_addr,
+ args->host_ip6_prefix_len);
+ if (args->error)
+ {
+ args->rv = VNET_API_ERROR_NETLINK_ERROR;
+ goto error;
+ }
+ }
+
+ /* Set vhost memory table */
+ i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
+ vhost_mem = clib_mem_alloc (i);
+ memset (vhost_mem, 0, i);
+ vhost_mem->nregions = 1;
+ vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096;
+ _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem);
+
+ if ((args->error = virtio_vring_init (vm, vif, 0, args->rx_ring_sz)))
+ {
+ args->rv = VNET_API_ERROR_INIT_FAILED;
+ goto error;
+ }
+
+ if ((args->error = virtio_vring_init (vm, vif, 1, args->tx_ring_sz)))
+ {
+ args->rv = VNET_API_ERROR_INIT_FAILED;
+ goto error;
+ }
+
+ /* set host side up */
+ if ((fd = socket (AF_INET, SOCK_STREAM, 0)) > 0)
+ {
+ memset (&ifr, 0, sizeof (struct ifreq));
+ strncpy (ifr.ifr_name, (char *) args->name, sizeof (ifr.ifr_name) - 1);
+ _IOCTL (fd, SIOCGIFFLAGS, (void *) &ifr);
+ ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
+ _IOCTL (fd, SIOCSIFFLAGS, (void *) &ifr);
+ }
+
+ if (!args->hw_addr_set)
+ {
+ f64 now = vlib_time_now (vm);
+ u32 rnd;
+ rnd = (u32) (now * 1e6);
+ rnd = random_u32 (&rnd);
+
+ memcpy (args->hw_addr + 2, &rnd, sizeof (rnd));
+ args->hw_addr[0] = 2;
+ args->hw_addr[1] = 0xfe;
+ }
+ vif->name = args->name;
+ args->name = 0;
+ vif->net_ns = args->host_namespace;
+ args->host_namespace = 0;
+ args->error = ethernet_register_interface (vnm, virtio_device_class.index,
+ vif->dev_instance, args->hw_addr,
+ &vif->hw_if_index,
+ virtio_eth_flag_change);
+ if (args->error)
+ {
+ args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
+ goto error;
+ }
+
+ sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
+ vif->sw_if_index = sw->sw_if_index;
+ args->sw_if_index = vif->sw_if_index;
+ hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
+ hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
+ vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
+ virtio_input_node.index);
+ vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
+ vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
+ VNET_HW_INTERFACE_RX_MODE_DEFAULT);
+ vif->per_interface_next_index = ~0;
+ vif->type = VIRTIO_IF_TYPE_TAP;
+ vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
+ vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
+ VNET_HW_INTERFACE_FLAG_LINK_UP);
+ goto done;
+
+error:
+ if (err)
+ {
+ ASSERT (args->error == 0);
+ args->error = err;
+ args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
+ }
+ if (vif->tap_fd != -1)
+ close (vif->tap_fd);
+ if (vif->fd != -1)
+ close (vif->fd);
+ vec_foreach_index (i, vif->vrings) virtio_vring_free (vif, i);
+ memset (vif, 0, sizeof (virtio_if_t));
+ pool_put (vim->interfaces, vif);
+
+done:
+ if (vhost_mem)
+ clib_mem_free (vhost_mem);
+ if (fd != -1)
+ close (fd);
+}
+
+int
+tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
+{
+ vnet_main_t *vnm = vnet_get_main ();
+ virtio_main_t *mm = &virtio_main;
+ int i;
+ virtio_if_t *vif;
+ vnet_hw_interface_t *hw;
+
+ hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
+ if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
+ return VNET_API_ERROR_INVALID_SW_IF_INDEX;
+
+ vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
+
+ /* bring down the interface */
+ vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
+ vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
+
+ ethernet_delete_interface (vnm, vif->hw_if_index);
+ vif->hw_if_index = ~0;
+
+ if (vif->tap_fd != -1)
+ close (vif->tap_fd);
+ if (vif->fd != -1)
+ close (vif->fd);
+
+ vec_foreach_index (i, vif->vrings) virtio_vring_free (vif, i);
+ vec_free (vif->vrings);
+
+ memset (vif, 0, sizeof (*vif));
+ pool_put (mm->interfaces, vif);
+
+ return 0;
+}
+
+int
+tap_dump_ifs (tap_interface_details_t ** out_tapids)
+{
+ vnet_main_t *vnm = vnet_get_main ();
+ virtio_main_t *mm = &virtio_main;
+ virtio_if_t *vif;
+ vnet_hw_interface_t *hi;
+ tap_interface_details_t *r_tapids = NULL;
+ tap_interface_details_t *tapid = NULL;
+
+ /* *INDENT-OFF* */
+ pool_foreach (vif, mm->interfaces,
+ vec_add2(r_tapids, tapid, 1);
+ memset (tapid, 0, sizeof (*tapid));
+ tapid->sw_if_index = vif->sw_if_index;
+ hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
+ clib_memcpy(tapid->dev_name, hi->name,
+ MIN (ARRAY_LEN (tapid->dev_name) - 1,
+ strlen ((const char *) hi->name)));
+ );
+ /* *INDENT-ON* */
+
+ *out_tapids = r_tapids;
+
+ return 0;
+}
+
+static clib_error_t *
+tap_init (vlib_main_t * vm)
+{
+
+ return 0;
+}
+
+VLIB_INIT_FUNCTION (tap_init);
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/devices/tap/tap.h b/src/vnet/devices/tap/tap.h
new file mode 100644
index 00000000000..0e0f8cb4056
--- /dev/null
+++ b/src/vnet/devices/tap/tap.h
@@ -0,0 +1,63 @@
+/*
+ *------------------------------------------------------------------
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *------------------------------------------------------------------
+ */
+
+#ifndef _VNET_DEVICES_VIRTIO_TAP_H_
+#define _VNET_DEVICES_VIRTIO_TAP_H_
+
+#ifndef MIN
+#define MIN(x,y) (((x)<(y))?(x):(y))
+#endif
+
+typedef struct
+{
+ u8 *name;
+ u8 hw_addr_set;
+ u8 hw_addr[6];
+ u16 rx_ring_sz;
+ u16 tx_ring_sz;
+ u8 *host_namespace;
+ u8 *host_bridge;
+ ip4_address_t host_ip4_addr;
+ u32 host_ip4_prefix_len;
+ ip6_address_t host_ip6_addr;
+ u32 host_ip6_prefix_len;
+ /* return */
+ u32 sw_if_index;
+ int rv;
+ clib_error_t *error;
+} tap_create_if_args_t;
+
+/** TAP interface details struct */
+typedef struct
+{
+ u32 sw_if_index;
+ u8 dev_name[64];
+} tap_interface_details_t;
+
+void tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args);
+int tap_delete_if (vlib_main_t * vm, u32 sw_if_index);
+int tap_dump_ifs (tap_interface_details_t ** out_tapids);
+
+#endif /* _VNET_DEVICES_VIRTIO_TAP_H_ */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/devices/tap/tapv2.api b/src/vnet/devices/tap/tapv2.api
new file mode 100644
index 00000000000..03788607fe5
--- /dev/null
+++ b/src/vnet/devices/tap/tapv2.api
@@ -0,0 +1,110 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/** \file
+
+ This file defines vpe control-plane API messages for
+ the Linux kernel TAP device driver
+*/
+
+vl_api_version 1.0.0
+
+/** \brief Initialize a new tap interface with the given paramters
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+ @param use_random_mac - let the system generate a unique mac address
+ @param tap_name - name to associate with the new interface
+ @param mac_address - mac addr to assign to the interface if use_radom not set
+ @param tx_ring_sz - the number of entries of TX ring
+ @param rx_ring_sz - the number of entries of RX ring
+ @param host_namespace_set - host namespece should be set
+ @param host_namespace - host namespace to attach interface to
+ @param host_bridge_set - host bridge should be set
+ @param host_bridge - host bridge to attach interface to
+ @param host_ip4_addr_set - host IPv4 ip address should be set
+ @param host_ip4_addr - host IPv4 ip address
+ @param host_ip4_prefix_len - host IPv4 ip address prefix length
+ @param host_ip6_addr_set - host IPv6 ip address should be set
+ @param host_ip6_addr - host IPv6 ip address
+ @param host_ip6_prefix_len - host IPv6 ip address prefix length
+*/
+define tap_create_v2
+{
+ u32 client_index;
+ u32 context;
+ u8 use_random_mac;
+ u8 tap_name[64];
+ u8 mac_address[6];
+ u16 tx_ring_sz; /* optional, default is 256 entries, must be power of 2 */
+ u16 rx_ring_sz; /* optional, default is 256 entries, must be power of 2 */
+ u8 host_namespace_set;
+ u8 host_namespace[64];
+ u8 host_bridge_set;
+ u8 host_bridge[64];
+ u8 host_ip4_addr_set;
+ u8 host_ip4_addr[4];
+ u8 host_ip4_prefix_len;
+ u8 host_ip6_addr_set;
+ u8 host_ip6_addr[16];
+ u8 host_ip6_prefix_len;
+};
+
+/** \brief Reply for tap create reply
+ @param context - returned sender context, to match reply w/ request
+ @param retval - return code
+ @param sw_if_index - software index allocated for the new tap interface
+*/
+define tap_create_v2_reply
+{
+ u32 context;
+ i32 retval;
+ u32 sw_if_index;
+};
+
+/** \brief Delete tap interface
+ @param client_index - opaque cookie to identify the sender
+ @param context - sender context, to match reply w/ request
+ @param sw_if_index - interface index of existing tap interface
+*/
+autoreply define tap_delete_v2
+{
+ u32 client_index;
+ u32 context;
+ u32 sw_if_index;
+};
+
+/** \brief Dump tap interfaces request */
+define sw_interface_tap_v2_dump
+{
+ u32 client_index;
+ u32 context;
+};
+
+/** \brief Reply for tap dump request
+ @param sw_if_index - software index of tap interface
+ @param dev_name - Linux tap device name
+*/
+define sw_interface_tap_v2_details
+{
+ u32 context;
+ u32 sw_if_index;
+ u8 dev_name[64];
+};
+
+/*
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/devices/tap/tapv2_api.c b/src/vnet/devices/tap/tapv2_api.c
new file mode 100644
index 00000000000..2d4d241a3dc
--- /dev/null
+++ b/src/vnet/devices/tap/tapv2_api.c
@@ -0,0 +1,237 @@
+/*
+ *------------------------------------------------------------------
+ * tap_api.c - vnet tap device driver API support
+ *
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *------------------------------------------------------------------
+ */
+
+#include <vnet/vnet.h>
+#include <vlibmemory/api.h>
+
+#include <vnet/interface.h>
+#include <vnet/api_errno.h>
+#include <vnet/ethernet/ethernet.h>
+#include <vnet/ip/ip.h>
+
+#include <vnet/vnet_msg_enum.h>
+
+#define vl_typedefs /* define message structures */
+#include <vnet/vnet_all_api_h.h>
+#undef vl_typedefs
+
+#define vl_endianfun /* define message structures */
+#include <vnet/vnet_all_api_h.h>
+#undef vl_endianfun
+
+/* instantiate all the print functions we know about */
+#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
+#define vl_printfun
+#include <vnet/vnet_all_api_h.h>
+#undef vl_printfun
+
+#include <vlibapi/api_helper_macros.h>
+#include <vnet/devices/tap/tap.h>
+
+#define foreach_tapv2_api_msg \
+_(TAP_CREATE_V2, tap_create_v2) \
+_(TAP_DELETE_V2, tap_delete_v2) \
+_(SW_INTERFACE_TAP_V2_DUMP, sw_interface_tap_v2_dump)
+
+static void
+vl_api_tap_create_v2_t_handler (vl_api_tap_create_v2_t * mp)
+{
+ vlib_main_t *vm = vlib_get_main ();
+ vl_api_tap_create_v2_reply_t *rmp;
+ unix_shared_memory_queue_t *q;
+ tap_create_if_args_t _a, *ap = &_a;
+
+ memset (ap, 0, sizeof (*ap));
+
+ ap->name = mp->tap_name;
+ if (!mp->use_random_mac)
+ {
+ clib_memcpy (ap->hw_addr, mp->mac_address, 6);
+ ap->hw_addr_set = 1;
+ }
+ ap->rx_ring_sz = mp->rx_ring_sz;
+ ap->tx_ring_sz = mp->tx_ring_sz;
+ ap->sw_if_index = (u32) ~ 0;
+
+ if (mp->host_namespace_set)
+ ap->host_namespace = mp->host_namespace;
+
+ if (mp->host_bridge_set)
+ ap->host_bridge = mp->host_bridge;
+
+ if (mp->host_ip4_addr_set)
+ {
+ clib_memcpy (&ap->host_ip4_addr.as_u8, mp->host_ip4_addr, 4);
+ ap->host_ip4_prefix_len = mp->host_ip4_prefix_len;
+ }
+
+ if (mp->host_ip6_addr_set)
+ {
+ clib_memcpy (&ap->host_ip6_addr, mp->host_ip6_addr, 16);
+ ap->host_ip6_prefix_len = mp->host_ip6_prefix_len;
+ }
+
+ tap_create_if (vm, ap);
+
+ q = vl_api_client_index_to_input_queue (mp->client_index);
+ if (!q)
+ return;
+
+ rmp = vl_msg_api_alloc (sizeof (*rmp));
+ rmp->_vl_msg_id = ntohs (VL_API_TAP_CREATE_V2_REPLY);
+ rmp->context = mp->context;
+ rmp->retval = ntohl (ap->rv);
+ rmp->sw_if_index = ntohl (ap->sw_if_index);
+
+ vl_msg_api_send_shmem (q, (u8 *) & rmp);
+}
+
+static void
+tap_send_sw_interface_event_deleted (vpe_api_main_t * am,
+ unix_shared_memory_queue_t * q,
+ u32 sw_if_index)
+{
+ vl_api_sw_interface_event_t *mp;
+
+ mp = vl_msg_api_alloc (sizeof (*mp));
+ memset (mp, 0, sizeof (*mp));
+ mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_EVENT);
+ mp->sw_if_index = ntohl (sw_if_index);
+
+ mp->admin_up_down = 0;
+ mp->link_up_down = 0;
+ mp->deleted = 1;
+ vl_msg_api_send_shmem (q, (u8 *) & mp);
+}
+
+static void
+vl_api_tap_delete_v2_t_handler (vl_api_tap_delete_v2_t * mp)
+{
+ vlib_main_t *vm = vlib_get_main ();
+ int rv;
+ vpe_api_main_t *vam = &vpe_api_main;
+ vl_api_tap_delete_v2_reply_t *rmp;
+ unix_shared_memory_queue_t *q;
+ u32 sw_if_index = ntohl (mp->sw_if_index);
+
+ rv = tap_delete_if (vm, sw_if_index);
+
+ q = vl_api_client_index_to_input_queue (mp->client_index);
+ if (!q)
+ return;
+
+ rmp = vl_msg_api_alloc (sizeof (*rmp));
+ rmp->_vl_msg_id = ntohs (VL_API_TAP_DELETE_V2_REPLY);
+ rmp->context = mp->context;
+ rmp->retval = ntohl (rv);
+
+ vl_msg_api_send_shmem (q, (u8 *) & rmp);
+
+ if (!rv)
+ tap_send_sw_interface_event_deleted (vam, q, sw_if_index);
+}
+
+static void
+tap_send_sw_interface_details (vpe_api_main_t * am,
+ unix_shared_memory_queue_t * q,
+ tap_interface_details_t * tap_if, u32 context)
+{
+ vl_api_sw_interface_tap_v2_details_t *mp;
+ mp = vl_msg_api_alloc (sizeof (*mp));
+ memset (mp, 0, sizeof (*mp));
+ mp->_vl_msg_id = ntohs (VL_API_SW_INTERFACE_TAP_V2_DETAILS);
+ mp->sw_if_index = ntohl (tap_if->sw_if_index);
+ clib_memcpy (mp->dev_name, tap_if->dev_name,
+ MIN (ARRAY_LEN (mp->dev_name) - 1,
+ strlen ((const char *) tap_if->dev_name)));
+ mp->context = context;
+
+ vl_msg_api_send_shmem (q, (u8 *) & mp);
+}
+
+static void
+vl_api_sw_interface_tap_v2_dump_t_handler (vl_api_sw_interface_tap_v2_dump_t *
+ mp)
+{
+ int rv;
+ vpe_api_main_t *am = &vpe_api_main;
+ unix_shared_memory_queue_t *q;
+ tap_interface_details_t *tapifs = NULL;
+ tap_interface_details_t *tap_if = NULL;
+
+ q = vl_api_client_index_to_input_queue (mp->client_index);
+ if (q == 0)
+ return;
+
+ rv = tap_dump_ifs (&tapifs);
+ if (rv)
+ return;
+
+ vec_foreach (tap_if, tapifs)
+ {
+ tap_send_sw_interface_details (am, q, tap_if, mp->context);
+ }
+
+ vec_free (tapifs);
+}
+
+#define vl_msg_name_crc_list
+#include <vnet/vnet_all_api_h.h>
+#undef vl_msg_name_crc_list
+
+static void
+tap_setup_message_id_table (api_main_t * am)
+{
+#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
+ foreach_vl_msg_name_crc_tapv2;
+#undef _
+}
+
+static clib_error_t *
+tapv2_api_hookup (vlib_main_t * vm)
+{
+ api_main_t *am = &api_main;
+
+#define _(N,n) \
+ vl_msg_api_set_handlers(VL_API_##N, #n, \
+ vl_api_##n##_t_handler, \
+ vl_noop_handler, \
+ vl_api_##n##_t_endian, \
+ vl_api_##n##_t_print, \
+ sizeof(vl_api_##n##_t), 1);
+ foreach_tapv2_api_msg;
+#undef _
+
+ /*
+ * Set up the (msg_name, crc, message-id) table
+ */
+ tap_setup_message_id_table (am);
+
+ return 0;
+}
+
+VLIB_API_INIT_FUNCTION (tapv2_api_hookup);
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */