aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMohsin KAZMI <sykazmi@cisco.com>2016-08-10 15:51:17 +0200
committerMohsin KAZMI <sykazmi@cisco.com>2016-08-10 17:33:56 +0200
commitadff8bfb431798fc1d4e4571d53074dc3438d14f (patch)
treeac73cd352be4eba5d1dac60596a39f4bc72b711d
parent5d7ae6ad224dab352219e3a60d40f0ed5af2be22 (diff)
turbotap: A plugin for turbo tap interfaces
This patch implements a plugin for tap interfaces using socket API. It has advantage of reduced context switching over traditional tap interfaces using "sendmmsg" or "recvmmsg" system calls to send/receive multiple packets using single system call. Change-Id: I5b98e403692ac47d119c03174a21fbd9ff24de82 Signed-off-by: Mohsin KAZMI <sykazmi@cisco.com>
-rw-r--r--turbotap/LICENSE202
-rw-r--r--turbotap/Makefile.am18
-rw-r--r--turbotap/README.md73
-rw-r--r--turbotap/configure.ac26
-rw-r--r--turbotap/turbotap.mk31
-rw-r--r--turbotap/turbotap/cli.c169
-rw-r--r--turbotap/turbotap/device.c227
-rw-r--r--turbotap/turbotap/node.c362
-rw-r--r--turbotap/turbotap/turbotap.c474
-rw-r--r--turbotap/turbotap/turbotap.h114
10 files changed, 1696 insertions, 0 deletions
diff --git a/turbotap/LICENSE b/turbotap/LICENSE
new file mode 100644
index 0000000..7a4a3ea
--- /dev/null
+++ b/turbotap/LICENSE
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ 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. \ No newline at end of file
diff --git a/turbotap/Makefile.am b/turbotap/Makefile.am
new file mode 100644
index 0000000..8ea9c3f
--- /dev/null
+++ b/turbotap/Makefile.am
@@ -0,0 +1,18 @@
+AUTOMAKE_OPTIONS = foreign subdir-objects
+
+AM_CFLAGS = -Wall -I@TOOLKIT_INCLUDE@ @DPDK@
+AM_LDFLAGS = -module -shared -avoid-version
+
+lib_LTLIBRARIES = libturbotap.la
+libturbotap_la_SOURCES = turbotap/turbotap.c\
+ turbotap/device.c \
+ turbotap/node.c \
+ turbotap/cli.c
+
+nobase_include_HEADERS = turbotap/turbotap.h
+
+if WITH_PLUGIN_TOOLKIT
+install-data-hook:
+ mkdir /usr/lib/vpp_plugins || true
+ cp $(prefix)/lib/turbotap.so.*.*.* /usr/lib/vpp_plugins
+endif
diff --git a/turbotap/README.md b/turbotap/README.md
new file mode 100644
index 0000000..4debcd9
--- /dev/null
+++ b/turbotap/README.md
@@ -0,0 +1,73 @@
+# A plugin to provide turbotap driver
+
+This repository provides an Experimental work, a plugin to use tap
+interfaces using socket API system calls "sendmmsg" or "recvmmsg"
+which allows to send/receive multiple packets using one single system
+call. It is a replacement for tapcli driver in VPP, which uses one
+system call per packet. Hence save the time for 'context switching'
+between userspace and kernel space.
+
+The linux kernel doesn't support socket API for tap interfaces. Therefore,
+a separate turbotap 'LINUX KERNEL MODULE' has been implemented to support
+send and receive socket system calls.
+
+More information can be found on the wiki page:
+- http://wiki.fd.io/view/VPP_Sandbox/turbotap
+
+Source code, build and install turbotap kernel module:
+- https://github.com/vpp-dev/turbotap
+
+## Build/Install
+
+The turbotap driver is implemented as a plugin to send/receive packets from
+kernel tap interfaces. To use it, you must BUILD and INSTALL turbotap kernel
+module at first.
+Then you must build plugin and put it in VPPs runtime plugin directory.
+The plugin depends on vpp. This README assumes familiarity with the build
+environment for both projects.
+
+Build vpp and turbotap both at once by creating symbolic links in the
+top level vpp directory to the turbotap directory as well as
+symbolic links to the respective .mk files in 'build-data/packages'.
+
+```
+$ cd /git/vpp
+$ ln -sf /git/vppsb/turbotap
+$ ln -sf ../../turbotap/turbotap.mk build-data/packages/
+```
+
+Now build everything and create a link to the plugin in vpp's plugin path.
+
+```
+$ cd build-root
+$ ./bootstrap.sh
+$ make V=0 PLATFORM=vpp TAG=vpp_debug turbotap-install
+$ ln -sf /git/vpp/build-root/install-vpp_debug-native/turbotap/lib64/turbotap.so.0.0.0 \
+ /usr/lib/vpp_plugins/
+```
+
+Once VPP is running and the plugin is loaded, turbotap interfaces can be created or deleted.
+
+```
+$ vppctl turbotap connect turbotap0
+$ vppctl turbotap delete turbotap0
+```
+
+The host operating system should see a turbotap named 'turbotap0'.
+
+## Administrative
+
+### Current status
+
+Currently the turbotap driver plugin uses socket API system calls. Most of the
+code is borrowed from tapcli driver in VPP. One can extend it to multi-queue driver.
+
+### Objective
+
+The objective of this project is to continue to build out better integration
+with host operating system and for providing a basis to enable completely
+or partially unmodified applications to take advantage of a fast datapath.
+
+### Main contributors
+
+Mohsin KAZMI - LF-ID:sykazmi
diff --git a/turbotap/configure.ac b/turbotap/configure.ac
new file mode 100644
index 0000000..a1d4311
--- /dev/null
+++ b/turbotap/configure.ac
@@ -0,0 +1,26 @@
+AC_INIT(turbotap, 1.0)
+LT_INIT
+AM_INIT_AUTOMAKE
+
+AM_PROG_AS
+AC_PROG_CC
+AM_PROG_CC_C_O
+
+AC_ARG_WITH(dpdk,
+ AC_HELP_STRING([--with-dpdk],[Use the Intel dpdk]),
+ [with_dpdk=1],
+ [with_dpdk=0])
+
+AC_ARG_WITH(plugin-toolkit,
+ AC_HELP_STRING([--with-plugin-toolkit],
+ [build using the vpp toolkit]),
+ [with_plugin_toolkit=${prefix}/include],
+ [with_plugin_toolkit=.])
+
+AM_CONDITIONAL(WITH_DPDK, test "$with_dpdk" = "1")
+AC_SUBST(DPDK,["-DDPDK=1"])
+
+AC_SUBST(TOOLKIT_INCLUDE,[${with_plugin_toolkit}])
+AM_CONDITIONAL(WITH_PLUGIN_TOOLKIT, test "$with_plugin_toolkit" != ".")
+
+AC_OUTPUT([Makefile])
diff --git a/turbotap/turbotap.mk b/turbotap/turbotap.mk
new file mode 100644
index 0000000..4ffa74e
--- /dev/null
+++ b/turbotap/turbotap.mk
@@ -0,0 +1,31 @@
+turbotap_configure_depend = \
+ vppinfra-install \
+ dpdk-install \
+ svm-install \
+ vlib-api-install \
+ vlib-install \
+ vnet-install \
+ vpp-install \
+ vpp-api-test-install
+
+turbotap_CPPFLAGS = $(call installed_includes_fn, \
+ vppinfra \
+ dpdk \
+ openssl \
+ svm \
+ vlib \
+ vlib-api \
+ vnet \
+ vpp \
+ vpp-api-test)
+
+turbotap_LDFLAGS = $(call installed_libs_fn, \
+ vppinfra \
+ dpdk \
+ openssl \
+ svm \
+ vlib \
+ vlib-api \
+ vnet \
+ vpp \
+ vpp-api-test)
diff --git a/turbotap/turbotap/cli.c b/turbotap/turbotap/cli.c
new file mode 100644
index 0000000..bdd5c8c
--- /dev/null
+++ b/turbotap/turbotap/cli.c
@@ -0,0 +1,169 @@
+/*
+ *------------------------------------------------------------------
+ * 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.
+ *------------------------------------------------------------------
+ */
+#define _GNU_SOURCE
+#include <fcntl.h> /* for open */
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/uio.h> /* for iovec */
+#include <netinet/in.h>
+
+#include <linux/socket.h>
+#include <linux/if_arp.h>
+#include <linux/if_tun.h>
+
+#include <vlib/vlib.h>
+#include <vnet/vnet.h>
+
+#include <vlib/unix/unix.h>
+#include <vnet/ethernet/ethernet.h>
+
+#if DPDK == 1
+#include <vnet/devices/dpdk/dpdk.h>
+#endif
+
+#include "turbotap.h"
+
+static clib_error_t *
+turbotap_delete_command_fn (vlib_main_t * vm,
+ unformat_input_t * input,
+ vlib_cli_command_t * cmd)
+{
+ turbotap_main_t * tr = &turbotap_main;
+ u32 sw_if_index = ~0;
+
+ if (tr->is_disabled)
+ {
+ return clib_error_return (0, "device disabled...");
+ }
+
+ if (unformat (input, "%U", unformat_vnet_sw_interface, tr->vnet_main,
+ &sw_if_index))
+ ;
+ else
+ return clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, input);
+
+ int rc = vnet_turbotap_delete (vm, sw_if_index);
+
+ if (!rc) {
+ vlib_cli_output (vm, "Deleted.");
+ } else {
+ vlib_cli_output (vm, "Error during deletion of tap interface. (rc: %d)", rc);
+ }
+
+ return 0;
+}
+
+VLIB_CLI_COMMAND (turbotap_delete_command, static) = {
+ .path = "turbotap delete",
+ .short_help = "turbotap delete <vpp-tap-intfc-name>",
+ .function = turbotap_delete_command_fn,
+};
+
+static clib_error_t *
+turbotap_connect_command_fn (vlib_main_t * vm,
+ unformat_input_t * input,
+ vlib_cli_command_t * cmd)
+{
+ u8 * intfc_name;
+ turbotap_main_t * tr = &turbotap_main;
+ u8 hwaddr[6];
+ u8 *hwaddr_arg = 0;
+ u32 sw_if_index;
+
+ if (tr->is_disabled)
+ {
+ return clib_error_return (0, "device disabled...");
+ }
+
+ if (unformat (input, "%s", &intfc_name))
+ ;
+ else
+ return clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, input);
+
+ if (unformat(input, "hwaddr %U", unformat_ethernet_address,
+ &hwaddr))
+ hwaddr_arg = hwaddr;
+
+ int rv = vnet_turbotap_connect(vm, intfc_name, hwaddr_arg, &sw_if_index);
+ if (rv) {
+ switch (rv) {
+ case VNET_API_ERROR_SYSCALL_ERROR_1:
+ vlib_cli_output (vm, "Couldn't open /dev/net/turbotap");
+ break;
+
+ case VNET_API_ERROR_SYSCALL_ERROR_2:
+ vlib_cli_output (vm, "Error setting flags on '%s'", intfc_name);
+ break;
+
+ case VNET_API_ERROR_SYSCALL_ERROR_3:
+ vlib_cli_output (vm, "Couldn't open provisioning socket");
+ break;
+
+ case VNET_API_ERROR_SYSCALL_ERROR_4:
+ vlib_cli_output (vm, "Couldn't get if_index");
+ break;
+
+ case VNET_API_ERROR_SYSCALL_ERROR_5:
+ vlib_cli_output (vm, "Couldn't bind provisioning socket");
+ break;
+
+ case VNET_API_ERROR_SYSCALL_ERROR_6:
+ vlib_cli_output (0, "Couldn't set device non-blocking flag");
+ break;
+
+ case VNET_API_ERROR_SYSCALL_ERROR_7:
+ vlib_cli_output (0, "Couldn't set device MTU");
+ break;
+
+ case VNET_API_ERROR_SYSCALL_ERROR_8:
+ vlib_cli_output (0, "Couldn't get interface flags");
+ break;
+ case VNET_API_ERROR_SYSCALL_ERROR_9:
+ vlib_cli_output (0, "Couldn't set intfc admin state up");
+ break;
+
+ case VNET_API_ERROR_INVALID_REGISTRATION:
+ vlib_cli_output (0, "Invalid registration");
+ break;
+ default:
+ vlib_cli_output (0, "Unknown error: %d", rv);
+ break;
+ }
+ return 0;
+ }
+
+ vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
+ return 0;
+}
+
+VLIB_CLI_COMMAND (turbotap_connect_command, static) = {
+ .path = "turbotap connect",
+ .short_help = "turbotap connect <intfc-name> [hwaddr <addr>]",
+ .function = turbotap_connect_command_fn,
+};
+
+clib_error_t *
+turbotap_cli_init (vlib_main_t * vm)
+{
+ return 0;
+}
+
+VLIB_INIT_FUNCTION (turbotap_cli_init);
diff --git a/turbotap/turbotap/device.c b/turbotap/turbotap/device.c
new file mode 100644
index 0000000..df14c79
--- /dev/null
+++ b/turbotap/turbotap/device.c
@@ -0,0 +1,227 @@
+/*
+ *------------------------------------------------------------------
+ * 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.
+ *------------------------------------------------------------------
+ */
+
+#define _GNU_SOURCE
+
+#include <stdint.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/uio.h> /* for iovec */
+#include <netinet/in.h>
+
+#include <vlib/vlib.h>
+#include <vnet/ip/ip.h>
+#include <vlib/unix/unix.h>
+#include <vnet/ethernet/ethernet.h>
+
+#if DPDK == 1
+#include <vnet/devices/dpdk/dpdk.h>
+#endif
+
+#include "turbotap.h"
+
+vnet_device_class_t turbotap_dev_class;
+
+static u8 * format_turbotap_interface_name (u8 * s, va_list * args)
+{
+ u32 i = va_arg (*args, u32);
+ u32 show_dev_instance = ~0;
+ turbotap_main_t * tr = &turbotap_main;
+
+ if (i < vec_len (tr->show_dev_instance_by_real_dev_instance))
+ show_dev_instance = tr->show_dev_instance_by_real_dev_instance[i];
+
+ if (show_dev_instance != ~0)
+ i = show_dev_instance;
+
+ s = format (s, "turbotap-%d", i);
+ return s;
+}
+
+static void turbotap_set_interface_next_node (vnet_main_t *vnm,
+ u32 hw_if_index,
+ u32 node_index)
+{
+ turbotap_main_t *tr = &turbotap_main;
+ turbotap_interface_t *ti;
+ vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
+
+ ti = vec_elt_at_index (tr->turbotap_interfaces, hw->dev_instance);
+
+ /* Shut off redirection */
+ if (node_index == ~0)
+ {
+ ti->per_interface_next_index = node_index;
+ return;
+ }
+
+ ti->per_interface_next_index =
+ vlib_node_add_next (tr->vlib_main, turbotap_rx_node.index, node_index);
+}
+
+static_always_inline uword
+turbotap_tx_iface(vlib_main_t * vm,
+ vlib_node_runtime_t * node,
+ vlib_frame_t * frame,
+ turbotap_interface_t * ti)
+{
+ u32 * buffers = vlib_frame_args (frame);
+ uword n_packets = frame->n_vectors;
+ vlib_buffer_t * b;
+ int i = 0;
+
+ vnet_sw_interface_t *si = vnet_get_sw_interface (vnet_get_main(), ti->sw_if_index);
+ if (PREDICT_FALSE(!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))) {
+ //Drop if interface is down
+ vlib_buffer_free(vm, vlib_frame_vector_args(frame), frame->n_vectors);
+ return 0;
+ }
+
+ u32 n_tx = (n_packets > MAX_SEND)?MAX_SEND:n_packets;
+ u32 total_bytes = 0;
+ for (i = 0; i < n_tx; i++) {
+ struct iovec * iov;
+ b = vlib_get_buffer(vm, buffers[i]);
+
+ if (ti->tx_msg[i].msg_hdr.msg_iov)
+ _vec_len(ti->tx_msg[i].msg_hdr.msg_iov) = 0; //Reset vector
+
+ /* VLIB buffer chain -> Unix iovec(s). */
+ vec_add2 (ti->tx_msg[i].msg_hdr.msg_iov, iov, 1);
+ iov->iov_base = b->data + b->current_data;
+ iov->iov_len = b->current_length;
+ ti->tx_msg[i].msg_len = b->current_length;
+ if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT)) {
+ do {
+ b = vlib_get_buffer (vm, b->next_buffer);
+ vec_add2 (ti->tx_msg[i].msg_hdr.msg_iov, iov, 1);
+ iov->iov_base = b->data + b->current_data;
+ iov->iov_len = b->current_length;
+ ti->tx_msg[i].msg_len += b->current_length;
+ } while (b->flags & VLIB_BUFFER_NEXT_PRESENT);
+ }
+
+ ti->tx_msg[i].msg_hdr.msg_name = NULL;
+ ti->tx_msg[i].msg_hdr.msg_namelen = 0;
+ ti->tx_msg[i].msg_hdr.msg_iovlen = _vec_len(ti->tx_msg[i].msg_hdr.msg_iov);
+ ti->tx_msg[i].msg_hdr.msg_control = NULL;
+ ti->tx_msg[i].msg_hdr.msg_controllen = 0;
+ ti->tx_msg[i].msg_hdr.msg_flags = MSG_DONTWAIT;
+ total_bytes += ti->tx_msg[i].msg_len;
+ }
+
+ if (n_tx) {
+ int tx;
+ if ((tx = sendmmsg(ti->sock_fd, ti->tx_msg, n_tx, MSG_DONTWAIT)) < 1) {
+ vlib_increment_simple_counter
+ (vnet_main.interface_main.sw_if_counters
+ + VNET_INTERFACE_COUNTER_TX_ERROR, os_get_cpu_number(),
+ ti->sw_if_index, n_tx);
+ } else {
+ vlib_increment_combined_counter(
+ vnet_main.interface_main.combined_sw_if_counters
+ + VNET_INTERFACE_COUNTER_TX,
+ os_get_cpu_number(), ti->sw_if_index,
+ tx, total_bytes);
+ }
+ }
+
+ vlib_buffer_free(vm, vlib_frame_vector_args(frame), frame->n_vectors);
+ return n_packets;
+}
+
+/*
+ * turbotap_tx
+ * Output node, writes the buffers comprising the incoming frame
+ * to the tun/tap device, aka hands them to the Linux kernel stack.
+ *
+ */
+static uword
+turbotap_tx (vlib_main_t * vm,
+ vlib_node_runtime_t * node,
+ vlib_frame_t * frame)
+{
+ u32 * buffers = vlib_frame_args (frame);
+ turbotap_main_t * tr = &turbotap_main;
+ turbotap_interface_t * ti;
+
+ if (!frame->n_vectors)
+ return 0;
+
+ vlib_buffer_t *b = vlib_get_buffer(vm, buffers[0]);
+ u32 tx_sw_if_index = vnet_buffer(b)->sw_if_index[VLIB_TX];
+ if (tx_sw_if_index == (u32)~0)
+ tx_sw_if_index = vnet_buffer(b)->sw_if_index[VLIB_RX];
+
+ ASSERT(tx_sw_if_index != (u32)~0);
+
+ /* Use the sup intfc to finesse vlan subifs */
+ vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (tr->vnet_main, tx_sw_if_index);
+ tx_sw_if_index = hw->sw_if_index;
+
+ uword * p = hash_get (tr->turbotap_interface_index_by_sw_if_index,
+ tx_sw_if_index);
+ if (p == 0) {
+ clib_warning ("sw_if_index %d unknown", tx_sw_if_index);
+ return 0;
+ } else {
+ ti = vec_elt_at_index (tr->turbotap_interfaces, p[0]);
+ }
+
+ return turbotap_tx_iface(vm, node, frame, ti);
+}
+
+VLIB_REGISTER_NODE (turbotap_tx_node,static) = {
+ .function = turbotap_tx,
+ .name = "turbotap-tx",
+ .type = VLIB_NODE_TYPE_INTERNAL,
+ .vector_size = 4,
+};
+
+/*
+ * Mainly exists to set link_state == admin_state
+ * otherwise, e.g. ip6 neighbor discovery breaks
+ */
+static clib_error_t *
+turbotap_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
+{
+ uword is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
+ u32 hw_flags;
+ u32 speed_duplex = VNET_HW_INTERFACE_FLAG_FULL_DUPLEX
+ | VNET_HW_INTERFACE_FLAG_SPEED_40G;
+
+ if (is_admin_up)
+ hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP | speed_duplex;
+ else
+ hw_flags = speed_duplex;
+
+ vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
+ return 0;
+}
+
+VNET_DEVICE_CLASS (turbotap_dev_class) = {
+ .name = "turbotap",
+ .tx_function = turbotap_tx,
+ .format_device_name = format_turbotap_interface_name,
+ .rx_redirect_to_node = turbotap_set_interface_next_node,
+ .admin_up_down_function = turbotap_interface_admin_up_down,
+ .no_flatten_output_chains = 1,
+};
+
diff --git a/turbotap/turbotap/node.c b/turbotap/turbotap/node.c
new file mode 100644
index 0000000..3616d46
--- /dev/null
+++ b/turbotap/turbotap/node.c
@@ -0,0 +1,362 @@
+/*
+ *------------------------------------------------------------------
+ * 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.
+ *------------------------------------------------------------------
+ */
+
+#define _GNU_SOURCE
+
+#include <stdint.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/uio.h> /* for iovec */
+#include <netinet/in.h>
+
+#include <vlib/vlib.h>
+#include <vlib/unix/unix.h>
+#include <vnet/ethernet/ethernet.h>
+#include <vnet/vnet.h>
+
+#include <vnet/ip/ip.h>
+
+#if DPDK == 1
+#include <vnet/devices/dpdk/dpdk.h>
+#endif
+
+#include <turbotap/turbotap.h>
+
+vlib_node_registration_t turbotap_rx_node;
+
+enum {
+ TURBOTAP_RX_NEXT_IP4_INPUT,
+ TURBOTAP_RX_NEXT_IP6_INPUT,
+ TURBOTAP_RX_NEXT_ETHERNET_INPUT,
+ TURBOTAP_RX_NEXT_DROP,
+ TURBOTAP_RX_N_NEXT,
+};
+
+typedef struct {
+ u16 sw_if_index;
+} turbotap_rx_trace_t;
+
+u8 * format_turbotap_rx_trace (u8 * s, va_list * va)
+{
+ CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
+ CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
+ vnet_main_t * vnm = vnet_get_main();
+ turbotap_rx_trace_t * t = va_arg (*va, turbotap_rx_trace_t *);
+ s = format (s, "%U", format_vnet_sw_if_index_name,
+ vnm, t->sw_if_index);
+ return s;
+}
+
+always_inline void
+buffer_add_to_chain(vlib_main_t *vm, u32 bi, u32 first_bi, u32 prev_bi)
+{
+ vlib_buffer_t * b = vlib_get_buffer (vm, bi);
+ vlib_buffer_t * first_b = vlib_get_buffer (vm, first_bi);
+ vlib_buffer_t * prev_b = vlib_get_buffer (vm, prev_bi);
+
+ /* update first buffer */
+ first_b->total_length_not_including_first_buffer += b->current_length;
+
+ /* update previous buffer */
+ prev_b->next_buffer = bi;
+ prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
+
+ /* update current buffer */
+ b->next_buffer = 0;
+
+#if DPDK > 0
+ struct rte_mbuf * mbuf = rte_mbuf_from_vlib_buffer(b);
+ struct rte_mbuf * first_mbuf = rte_mbuf_from_vlib_buffer(first_b);
+ struct rte_mbuf * prev_mbuf = rte_mbuf_from_vlib_buffer(prev_b);
+ first_mbuf->nb_segs++;
+ prev_mbuf->next = mbuf;
+ mbuf->data_len = b->current_length;
+ mbuf->data_off = RTE_PKTMBUF_HEADROOM + b->current_data;
+ mbuf->next = 0;
+#endif
+}
+
+static uword
+turbotap_rx_iface(vlib_main_t * vm,
+ vlib_node_runtime_t * node,
+ turbotap_interface_t * ti)
+{
+ turbotap_main_t * tr = &turbotap_main;
+ const uword buffer_size = vlib_buffer_free_list_buffer_size ( vm,
+ VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
+ u32 n_trace = vlib_get_trace_count (vm, node);
+ u8 set_trace = 0;
+ vnet_main_t *vnm;
+ vnet_sw_interface_t * si;
+ u8 admin_down;
+ uword len = 0;
+ u32 next_index = TURBOTAP_RX_NEXT_ETHERNET_INPUT;
+ u32 *to_next;
+
+ vnm = vnet_get_main();
+ si = vnet_get_sw_interface (vnm, ti->sw_if_index);
+ admin_down = !(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP);
+
+ if (ti->per_interface_next_index != ~0)
+ next_index = ti->per_interface_next_index;
+
+ /* Buffer Allocation */
+ u32 desired_allocation = ti->rx_ready * ti->mtu_buffers + 32;
+ if (PREDICT_TRUE(vec_len(tr->rx_buffers) < ti->rx_ready * ti->mtu_buffers))
+ {
+ len = vec_len(tr->rx_buffers);
+ vec_validate(tr->rx_buffers, desired_allocation - 1);
+ vec_validate(tr->unused_buffer_list, desired_allocation - 1);
+ _vec_len(tr->unused_buffer_list) = 0;
+ _vec_len(tr->rx_buffers) = len +
+ vlib_buffer_alloc(vm, &tr->rx_buffers[len], desired_allocation - len);
+ if (PREDICT_FALSE(vec_len(tr->rx_buffers) < ti->rx_ready * ti->mtu_buffers))
+ {
+ vlib_node_increment_counter(vm, turbotap_rx_node.index, TURBOTAP_ERROR_BUFFER_ALLOCATION, 1);
+ }
+ }
+
+ /* Filling msgs */
+ u32 i = 0;
+ len = vec_len(tr->rx_buffers);
+ while (i < ti->rx_ready && len > ti->mtu_buffers)
+ {
+ u32 j = 0;
+ vec_validate(ti->rx_msg[i].msg_hdr.msg_iov, ti->mtu_buffers - 1);
+ while (j < ti->mtu_buffers)
+ {
+ vlib_buffer_t *b = vlib_get_buffer(vm, tr->rx_buffers[len - 1]);
+ ti->rx_msg[i].msg_hdr.msg_iov[j].iov_base = b->data;
+ ti->rx_msg[i].msg_hdr.msg_iov[j].iov_len = buffer_size;
+ len--;
+ j++;
+ }
+
+ ti->rx_msg[i].msg_hdr.msg_iovlen = ti->mtu_buffers;
+ ti->rx_msg[i].msg_hdr.msg_flags = MSG_DONTWAIT;
+ ti->rx_msg[i].msg_hdr.msg_name = NULL;
+ ti->rx_msg[i].msg_hdr.msg_namelen = 0;
+ ti->rx_msg[i].msg_hdr.msg_control = NULL;
+ ti->rx_msg[i].msg_hdr.msg_controllen = 0;
+ ti->rx_msg[i].msg_len = 0;
+ i++;
+ }
+
+ /*
+ * Be careful here
+ *
+ * Experiments show that we need to set the time according
+ * to the number of msgs receive from kernel even if the call
+ * is NON-BLOCKING. If timeout is so small, then recvmmsg
+ * call gets as many packets as it can in that time period.
+ */
+ struct timespec timeout = {.tv_sec = 0, .tv_nsec = 500000};
+ int num_rx_msgs = recvmmsg(ti->sock_fd, ti->rx_msg, i, MSG_DONTWAIT, &timeout);
+ if (num_rx_msgs <= 0) {
+ if (errno != EAGAIN) {
+ vlib_node_increment_counter(vm, turbotap_rx_node.index,
+ TURBOTAP_ERROR_READ, 1);
+ }
+ return 0;
+ }
+
+ u32 next = next_index;
+ u32 n_left_to_next = 0;
+
+ i = 0;
+ len = vec_len(tr->rx_buffers);
+ vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
+
+ while (i != num_rx_msgs && n_left_to_next)
+ {
+ vlib_buffer_t *b0, *first_b0;
+ u32 bi0 = 0, first_bi0 = 0, prev_bi0, j = 0;
+ u32 bytes_to_put = 0, bytes_already_put = 0;
+ u32 remain_len = ti->rx_msg[i].msg_len;
+
+ while (remain_len && len)
+ {
+ /* grab free buffer */
+ prev_bi0 = bi0;
+ bi0 = tr->rx_buffers[len - 1];
+ b0 = vlib_get_buffer(vm, bi0);
+
+ bytes_to_put = remain_len > buffer_size ? buffer_size : remain_len;
+ b0->current_length = bytes_to_put;
+
+ if (bytes_already_put == 0)
+ {
+#if DPDK > 0
+ struct rte_mbuf * mb = rte_mbuf_from_vlib_buffer(b0);
+ rte_pktmbuf_data_len (mb) = b0->current_length;
+ rte_pktmbuf_pkt_len (mb) = b0->current_length;
+#endif
+ b0->total_length_not_including_first_buffer = 0;
+ b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID;
+ vnet_buffer(b0)->sw_if_index[VLIB_RX] = ti->sw_if_index;
+ vnet_buffer(b0)->sw_if_index[VLIB_TX] = (u32)~0;
+ first_bi0 = bi0;
+ first_b0 = vlib_get_buffer(vm, first_bi0);
+ }
+ else
+ buffer_add_to_chain(vm, bi0, first_bi0, prev_bi0);
+
+
+ bytes_already_put += bytes_to_put;
+ remain_len -= bytes_to_put;
+ j++;
+ len--;
+ }
+
+ /* record unused buffers */
+ while (j < ti->mtu_buffers)
+ {
+ u32 vec_len_unused = vec_len(tr->unused_buffer_list);
+ tr->unused_buffer_list[vec_len_unused] = tr->rx_buffers[len - 1];
+ len--;
+ j++;
+ _vec_len(tr->unused_buffer_list) = vec_len_unused + 1;
+ }
+
+ /* trace */
+ VLIB_BUFFER_TRACE_TRAJECTORY_INIT(first_b0);
+
+ first_b0->error = node->errors[TURBOTAP_ERROR_NONE];
+
+ /* Interface counters and tracing. */
+ if (PREDICT_TRUE(!admin_down))
+ {
+ vlib_increment_combined_counter (
+ vnet_main.interface_main.combined_sw_if_counters
+ + VNET_INTERFACE_COUNTER_RX,
+ os_get_cpu_number(), ti->sw_if_index,
+ 1, ti->rx_msg[i].msg_len);
+
+ if (PREDICT_FALSE(n_trace > 0))
+ {
+ vlib_trace_buffer (vm, node, next_index,
+ first_b0, /* follow_chain */ 1);
+ n_trace--;
+ set_trace = 1;
+ turbotap_rx_trace_t *t0 = vlib_add_trace (vm, node, first_b0, sizeof (*t0));
+ t0->sw_if_index = si->sw_if_index;
+ }
+ } else {
+ next = TURBOTAP_RX_NEXT_DROP;
+ }
+
+ /* next packet */
+ to_next[0] = first_bi0;
+ n_left_to_next -= 1;
+ to_next +=1;
+
+ /* enque and take next packet */
+ vlib_validate_buffer_enqueue_x1(vm, node, next_index , to_next,
+ n_left_to_next, first_bi0, next);
+
+ i++;
+ }
+
+ _vec_len(tr->rx_buffers) = len;
+ vlib_put_next_frame(vm, node, next_index, n_left_to_next);
+
+ /* put unused buffers back */
+ while (vec_len(tr->unused_buffer_list) > 0)
+ {
+ u32 vec_len_unused = vec_len(tr->unused_buffer_list);
+ u32 vec_len_rx = vec_len(tr->rx_buffers);
+ tr->rx_buffers[vec_len_rx] = tr->unused_buffer_list[vec_len_unused - 1];
+ _vec_len(tr->unused_buffer_list) -= 1;
+ _vec_len(tr->rx_buffers) += 1;
+ }
+
+ if (ti->rx_ready - i > 0 )
+ {
+ ti->rx_ready -= i;
+ if (ti->rx_ready < i)
+ ti->rx_ready = i;
+ }
+ else if (ti->rx_ready + i > MAX_RECV)
+ ti->rx_ready = MAX_RECV;
+ else
+ ti->rx_ready += i;
+
+ if (set_trace)
+ vlib_set_trace_count (vm, node, n_trace);
+ return i;
+}
+
+static uword
+turbotap_rx (vlib_main_t * vm,
+ vlib_node_runtime_t * node,
+ vlib_frame_t * frame)
+{
+ turbotap_main_t * tr = &turbotap_main;
+ static u32 * ready_interface_indices;
+ turbotap_interface_t * ti;
+ int i;
+ u32 total_count = 0;
+
+ vec_reset_length (ready_interface_indices);
+ clib_bitmap_foreach (i, tr->pending_read_bitmap,
+ ({
+ vec_add1 (ready_interface_indices, i);
+ }));
+
+ if (vec_len (ready_interface_indices) == 0)
+ return 0;
+
+ for (i = 0; i < vec_len(ready_interface_indices); i++)
+ {
+ tr->pending_read_bitmap =
+ clib_bitmap_set (tr->pending_read_bitmap,
+ ready_interface_indices[i], 0);
+
+ ti = vec_elt_at_index (tr->turbotap_interfaces, ready_interface_indices[i]);
+ total_count += turbotap_rx_iface(vm, node, ti);
+ }
+ return total_count; //This might return more than 256.
+}
+
+static char * turbotap_rx_error_strings[] = {
+#define _(sym,string) string,
+ foreach_turbotap_error
+#undef _
+};
+
+VLIB_REGISTER_NODE (turbotap_rx_node) = {
+ .function = turbotap_rx,
+ .name = "turbotap-rx",
+ .type = VLIB_NODE_TYPE_INPUT,
+ .state = VLIB_NODE_STATE_INTERRUPT,
+ .vector_size = 4,
+ .n_errors = TURBOTAP_N_ERROR,
+ .error_strings = turbotap_rx_error_strings,
+ .format_trace = format_turbotap_rx_trace,
+
+ .n_next_nodes = TURBOTAP_RX_N_NEXT,
+ .next_nodes = {
+ [TURBOTAP_RX_NEXT_IP4_INPUT] = "ip4-input-no-checksum",
+ [TURBOTAP_RX_NEXT_IP6_INPUT] = "ip6-input",
+ [TURBOTAP_RX_NEXT_DROP] = "error-drop",
+ [TURBOTAP_RX_NEXT_ETHERNET_INPUT] = "ethernet-input",
+ },
+};
+
diff --git a/turbotap/turbotap/turbotap.c b/turbotap/turbotap/turbotap.c
new file mode 100644
index 0000000..734020b
--- /dev/null
+++ b/turbotap/turbotap/turbotap.c
@@ -0,0 +1,474 @@
+/*
+ *------------------------------------------------------------------
+ * turbotap.c - fast dynamic tap interface hookup
+ *
+ * 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.
+ *------------------------------------------------------------------
+ */
+
+#define _GNU_SOURCE
+
+#include <fcntl.h> /* for open */
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/uio.h> /* for iovec */
+#include <netinet/in.h>
+
+#include <linux/if_arp.h>
+#include <linux/if_tun.h>
+
+#include <vlib/vlib.h>
+#include <vlib/unix/unix.h>
+
+#include <vnet/ip/ip.h>
+#include <vnet/plugin/plugin.h>
+#include <vnet/ethernet/ethernet.h>
+
+#if DPDK == 1
+#include <vnet/devices/dpdk/dpdk.h>
+#endif
+
+#include "turbotap.h"
+
+turbotap_main_t turbotap_main;
+
+static void
+turbotap_nopunt_frame (vlib_main_t * vm,
+ vlib_node_runtime_t * node,
+ vlib_frame_t * frame)
+{
+ u32 * buffers = vlib_frame_args (frame);
+ uword n_packets = frame->n_vectors;
+ vlib_buffer_free (vm, buffers, n_packets);
+ vlib_frame_free (vm, node, frame);
+}
+
+/* Gets called when file descriptor is ready from epoll. */
+static clib_error_t * turbotap_read_ready (unix_file_t * uf)
+{
+ vlib_main_t * vm = vlib_get_main();
+ turbotap_main_t * tr = &turbotap_main;
+ uword * p;
+
+ /* Schedule the rx node */
+ vlib_node_set_interrupt_pending (vm, turbotap_rx_node.index);
+
+ p = hash_get (tr->turbotap_interface_index_by_unix_fd, uf->file_descriptor);
+
+ /* Mark the specific tap interface ready-to-read */
+ if (p)
+ tr->pending_read_bitmap = clib_bitmap_set (tr->pending_read_bitmap,
+ p[0], 1);
+ else
+ clib_warning ("fd %d not in hash table", uf->file_descriptor);
+
+ return 0;
+}
+
+static clib_error_t *
+turbotap_config (vlib_main_t * vm, unformat_input_t * input)
+{
+ turbotap_main_t *tr = &turbotap_main;
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (input, "mtu %d", &tr->mtu_bytes))
+ ;
+ else if (unformat (input, "disable"))
+ tr->is_disabled = 1;
+ else
+ return clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, input);
+ }
+
+ if (tr->is_disabled)
+ return 0;
+
+ if (geteuid())
+ {
+ clib_warning ("turbotap disabled: must be superuser");
+ tr->is_disabled = 1;
+ return 0;
+ }
+
+ return 0;
+}
+
+VLIB_CONFIG_FUNCTION (turbotap_config, "turbotap");
+
+static u32 turbotap_flag_change (vnet_main_t * vnm,
+ vnet_hw_interface_t * hw,
+ u32 flags)
+{
+ turbotap_main_t *tr = &turbotap_main;
+ turbotap_interface_t *ti;
+
+ ti = vec_elt_at_index (tr->turbotap_interfaces, hw->dev_instance);
+
+ if (flags & ETHERNET_INTERFACE_FLAG_MTU)
+ {
+ const uword buffer_size = vlib_buffer_free_list_buffer_size ( vlib_get_main(),
+ VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
+ ti->mtu_bytes = hw->max_packet_bytes;
+ ti->mtu_buffers = (hw->max_packet_bytes + (buffer_size - 1)) / buffer_size;
+ }
+ else
+ {
+ struct ifreq ifr;
+ u32 want_promisc;
+
+ memcpy (&ifr, &ti->ifr, sizeof (ifr));
+
+ /* get flags, modify to bring up interface... */
+ if (ioctl (ti->provision_fd, SIOCGIFFLAGS, &ifr) < 0)
+ {
+ clib_unix_warning ("Couldn't get interface flags for %s", hw->name);
+ return 0;
+ }
+
+ want_promisc = (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) != 0;
+
+ if (want_promisc == ti->is_promisc)
+ return 0;
+
+ if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL)
+ ifr.ifr_flags |= IFF_PROMISC;
+ else
+ ifr.ifr_flags &= ~(IFF_PROMISC);
+
+ /* get flags, modify to bring up interface... */
+ if (ioctl (ti->provision_fd, SIOCSIFFLAGS, &ifr) < 0)
+ {
+ clib_unix_warning ("Couldn't set interface flags for %s", hw->name);
+ return 0;
+ }
+
+ ti->is_promisc = want_promisc;
+ }
+
+ return 0;
+}
+
+/* get tap interface from inactive interfaces or create new */
+static turbotap_interface_t *turbotap_get_new_tapif()
+{
+ turbotap_main_t * tr = &turbotap_main;
+ turbotap_interface_t *ti = NULL;
+
+ int inactive_cnt = vec_len(tr->turbotap_inactive_interfaces);
+ // if there are any inactive ifaces
+ if (inactive_cnt > 0) {
+ // take last
+ u32 ti_idx = tr->turbotap_inactive_interfaces[inactive_cnt - 1];
+ if (vec_len(tr->turbotap_interfaces) > ti_idx) {
+ ti = vec_elt_at_index (tr->turbotap_interfaces, ti_idx);
+ clib_warning("reusing tap interface");
+ }
+ // "remove" from inactive list
+ _vec_len(tr->turbotap_inactive_interfaces) -= 1;
+ }
+
+ // ti was not retrieved from inactive ifaces - create new
+ if (!ti)
+ {
+ vec_add2 (tr->turbotap_interfaces, ti, 1);
+ u32 i;
+
+ for (i = 0; i < MAX_RECV; i++)
+ {
+ ti->rx_msg[i].msg_hdr.msg_name = NULL;
+ ti->rx_msg[i].msg_hdr.msg_namelen = 0;
+ ti->rx_msg[i].msg_hdr.msg_control = NULL;
+ ti->rx_msg[i].msg_hdr.msg_controllen = 0;
+ }
+ }
+ return ti;
+}
+
+int vnet_turbotap_connect (vlib_main_t * vm, u8 * intfc_name, u8 *hwaddr_arg,
+ u32 * sw_if_indexp)
+{
+ turbotap_main_t * tr = &turbotap_main;
+ turbotap_interface_t * ti = NULL;
+ struct ifreq ifr;
+ int flags;
+ int dev_net_tun_fd;
+ int dev_tap_fd = -1;
+ int turbotap_fd = -1;
+ int sock_fd = -1;
+ clib_error_t * error;
+ u8 hwaddr [6];
+ int rv = 0;
+
+ if (tr->is_disabled)
+ {
+ return VNET_API_ERROR_FEATURE_DISABLED;
+ }
+
+ flags = IFF_TAP | IFF_NO_PI;
+
+ if ((turbotap_fd = open ("/dev/net/turbotap", O_RDWR)) < 0)
+ return VNET_API_ERROR_SYSCALL_ERROR_1;
+
+ if ((dev_net_tun_fd = open ("/dev/net/tun", O_RDWR)) < 0)
+ return VNET_API_ERROR_SYSCALL_ERROR_1;
+
+ memset (&ifr, 0, sizeof (ifr));
+ strncpy(ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
+ ifr.ifr_flags = flags;
+ if (ioctl (dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0)
+ {
+ rv = VNET_API_ERROR_SYSCALL_ERROR_2;
+ goto error;
+ }
+
+ /* Open a provisioning socket */
+ if ((dev_tap_fd = socket(PF_PACKET, SOCK_RAW,
+ htons(ETH_P_ALL))) < 0 )
+ {
+ rv = VNET_API_ERROR_SYSCALL_ERROR_3;
+ goto error;
+ }
+
+ /* Find the interface index. */
+ {
+ struct ifreq ifr;
+ struct sockaddr_ll sll;
+
+ memset (&ifr, 0, sizeof(ifr));
+ strncpy (ifr.ifr_name, (char *) intfc_name, sizeof (ifr.ifr_name)-1);
+ if (ioctl (dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 )
+ {
+ rv = VNET_API_ERROR_SYSCALL_ERROR_4;
+ goto error;
+ }
+
+ /* Bind the provisioning socket to the interface. */
+ memset(&sll, 0, sizeof(sll));
+ sll.sll_family = AF_PACKET;
+ sll.sll_ifindex = ifr.ifr_ifindex;
+ sll.sll_protocol = htons(ETH_P_ALL);
+
+ if (bind(dev_tap_fd, (struct sockaddr*) &sll, sizeof(sll)) < 0)
+ {
+ rv = VNET_API_ERROR_SYSCALL_ERROR_5;
+ goto error;
+ }
+ }
+
+ /* non-blocking I/O on /dev/tapX */
+ {
+ int one = 1;
+ if (ioctl (dev_net_tun_fd, FIONBIO, &one) < 0)
+ {
+ rv = VNET_API_ERROR_SYSCALL_ERROR_6;
+ goto error;
+ }
+ }
+ ifr.ifr_mtu = tr->mtu_bytes;
+ if (ioctl (dev_tap_fd, SIOCSIFMTU, &ifr) < 0)
+ {
+ rv = VNET_API_ERROR_SYSCALL_ERROR_7;
+ goto error;
+ }
+
+ /* get flags, modify to bring up interface... */
+ if (ioctl (dev_tap_fd, SIOCGIFFLAGS, &ifr) < 0)
+ {
+ rv = VNET_API_ERROR_SYSCALL_ERROR_8;
+ goto error;
+ }
+
+ ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
+
+ if (ioctl (dev_tap_fd, SIOCSIFFLAGS, &ifr) < 0)
+ {
+ rv = VNET_API_ERROR_SYSCALL_ERROR_9;
+ goto error;
+ }
+
+ ti = turbotap_get_new_tapif();
+ ti->per_interface_next_index = ~0;
+
+ if (hwaddr_arg != 0)
+ clib_memcpy(hwaddr, hwaddr_arg, 6);
+ else
+ {
+ f64 now = vlib_time_now(vm);
+ u32 rnd;
+ rnd = (u32) (now * 1e6);
+ rnd = random_u32 (&rnd);
+
+ memcpy (hwaddr+2, &rnd, sizeof(rnd));
+ hwaddr[0] = 2;
+ hwaddr[1] = 0xfe;
+ }
+
+ if ((sock_fd = ioctl (turbotap_fd, TUNGETSOCKFD, (void *)&dev_net_tun_fd) ) < 0)
+ {
+ rv = VNET_API_ERROR_SYSCALL_ERROR_2;
+ goto error;
+ }
+
+ error = ethernet_register_interface
+ (tr->vnet_main,
+ turbotap_dev_class.index,
+ ti - tr->turbotap_interfaces /* device instance */,
+ hwaddr /* ethernet address */,
+ &ti->hw_if_index,
+ turbotap_flag_change);
+
+ if (error)
+ {
+ clib_error_report (error);
+ rv = VNET_API_ERROR_INVALID_REGISTRATION;
+ goto error;
+ }
+
+ {
+ const uword buffer_size = vlib_buffer_free_list_buffer_size ( vlib_get_main(),
+ VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
+ unix_file_t template = {0};
+ template.read_function = turbotap_read_ready;
+ template.file_descriptor = dev_net_tun_fd;
+ ti->unix_file_index = unix_file_add (&unix_main, &template);
+ ti->unix_fd = dev_net_tun_fd;
+ ti->provision_fd = dev_tap_fd;
+ ti->turbotap_fd = turbotap_fd;
+ ti->sock_fd = sock_fd;
+ ti->rx_ready = MAX_RECV;
+ ti->mtu_bytes = tr->mtu_bytes;
+ ti->mtu_buffers = (tr->mtu_bytes + (buffer_size - 1)) / buffer_size;
+ clib_memcpy (&ti->ifr, &ifr, sizeof (ifr));
+ }
+
+ {
+ vnet_hw_interface_t * hw;
+ hw = vnet_get_hw_interface (tr->vnet_main, ti->hw_if_index);
+ hw->min_supported_packet_bytes = TAP_MTU_MIN;
+ hw->max_supported_packet_bytes = TAP_MTU_MAX;
+ hw->max_packet_bytes = ti->mtu_bytes;
+ hw->max_l3_packet_bytes[VLIB_RX] = hw->max_l3_packet_bytes[VLIB_TX] = hw->max_supported_packet_bytes - sizeof(ethernet_header_t);
+ ti->sw_if_index = hw->sw_if_index;
+ if (sw_if_indexp)
+ *sw_if_indexp = hw->sw_if_index;
+ }
+
+ ti->active = 1;
+
+ hash_set (tr->turbotap_interface_index_by_sw_if_index, ti->sw_if_index,
+ ti - tr->turbotap_interfaces);
+
+ hash_set (tr->turbotap_interface_index_by_unix_fd, ti->unix_fd,
+ ti - tr->turbotap_interfaces);
+
+ return rv;
+
+ error:
+ close (dev_net_tun_fd);
+ close (dev_tap_fd);
+ close (turbotap_fd);
+ close (sock_fd);
+
+ return rv;
+}
+
+static int turbotap_tap_disconnect (turbotap_interface_t *ti)
+{
+ int rv = 0;
+ turbotap_main_t * tr = &turbotap_main;
+ vnet_main_t * vnm = tr->vnet_main;
+ u32 sw_if_index = ti->sw_if_index;
+
+ // bring interface down
+ vnet_sw_interface_set_flags (vnm, sw_if_index, 0);
+
+ if (ti->unix_file_index != ~0) {
+ unix_file_del (&unix_main, unix_main.file_pool + ti->unix_file_index);
+ ti->unix_file_index = ~0;
+ }
+
+ hash_unset (tr->turbotap_interface_index_by_unix_fd, ti->unix_fd);
+ hash_unset (tr->turbotap_interface_index_by_sw_if_index, ti->sw_if_index);
+ close(ti->unix_fd);
+ close(ti->provision_fd);
+ close(ti->turbotap_fd);
+ close(ti->sock_fd);
+ ti->unix_fd = -1;
+ ti->provision_fd = -1;
+ ti->turbotap_fd = -1;
+ ti->sock_fd = -1;
+
+ return rv;
+}
+
+int vnet_turbotap_delete(vlib_main_t *vm, u32 sw_if_index)
+{
+ int rv = 0;
+ turbotap_main_t * tr = &turbotap_main;
+ turbotap_interface_t *ti;
+ uword *p = NULL;
+
+ p = hash_get (tr->turbotap_interface_index_by_sw_if_index,
+ sw_if_index);
+ if (p == 0) {
+ clib_warning ("sw_if_index %d unknown", sw_if_index);
+ return VNET_API_ERROR_INVALID_SW_IF_INDEX;
+ }
+ ti = vec_elt_at_index (tr->turbotap_interfaces, p[0]);
+
+ // inactive
+ ti->active = 0;
+ turbotap_tap_disconnect(ti);
+ // add to inactive list
+ vec_add1(tr->turbotap_inactive_interfaces, ti - tr->turbotap_interfaces);
+
+ // reset renumbered iface
+ if (p[0] < vec_len (tr->show_dev_instance_by_real_dev_instance))
+ tr->show_dev_instance_by_real_dev_instance[p[0]] = ~0;
+
+ ethernet_delete_interface (tr->vnet_main, ti->hw_if_index);
+ return rv;
+}
+
+clib_error_t *
+vlib_plugin_register(vlib_main_t *m, vnet_plugin_handoff_t *h, int f)
+{
+ clib_error_t * error = 0;
+ return error;
+}
+
+clib_error_t *turbotap_init(vlib_main_t *vm)
+{
+ clib_error_t * error = 0;
+ turbotap_main_t * tr = &turbotap_main;
+
+ tr->vlib_main = vm;
+ tr->vnet_main = vnet_get_main();
+ tr->unix_main = &unix_main;
+ tr->mtu_bytes = TAP_MTU_DEFAULT;
+ tr->turbotap_interface_index_by_sw_if_index = hash_create (0, sizeof(uword));
+ tr->turbotap_interface_index_by_unix_fd = hash_create (0, sizeof (uword));
+ tr->rx_buffers = 0;
+ tr->unused_buffer_list = 0;
+ vec_alloc(tr->rx_buffers, VLIB_FRAME_SIZE);
+ vec_reset_length(tr->rx_buffers);
+ vec_alloc(tr->unused_buffer_list, VLIB_FRAME_SIZE);
+ vec_reset_length(tr->unused_buffer_list);
+ vm->os_punt_frame = turbotap_nopunt_frame;
+ return error;
+}
+VLIB_INIT_FUNCTION(turbotap_init);
diff --git a/turbotap/turbotap/turbotap.h b/turbotap/turbotap/turbotap.h
new file mode 100644
index 0000000..d1b0eda
--- /dev/null
+++ b/turbotap/turbotap/turbotap.h
@@ -0,0 +1,114 @@
+/*
+ * Copyright (c) 2015 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.
+ */
+
+#define _GNU_SOURCE
+
+#include <sys/socket.h>
+#include <vlib/vlib.h>
+
+#ifndef __TURBOTAP_H__
+#define __TURBOTAP_H__
+
+#define TUNGETSOCKFD _IOR('T', 224, int)
+#define MAX_SEND 256
+#define MAX_RECV 256
+
+#define TAP_MTU_DEFAULT 1500
+#define TAP_MTU_MAX 65535
+#define TAP_MTU_MIN 68
+
+#define foreach_turbotap_error \
+ /* Must be first. */ \
+ _(NONE, "no error") \
+ _(READ, "read error") \
+ _(BUFFER_ALLOCATION, "buffer allocation error") \
+ _(UNKNOWN, "unknown error")
+
+typedef enum {
+#define _(sym,str) TURBOTAP_ERROR_##sym,
+ foreach_turbotap_error
+#undef _
+ TURBOTAP_N_ERROR,
+ } turbotap_error_t;
+
+typedef struct {
+ u32 unix_fd;
+ u32 unix_file_index;
+ u32 provision_fd;
+ u32 sw_if_index; /* for counters */
+ u32 hw_if_index;
+ u32 is_promisc;
+ struct ifreq ifr;
+ u32 per_interface_next_index;
+ u8 active; /* for delete */
+
+ /* mtu count, mtu buffers */
+ u32 mtu_bytes;
+ u32 mtu_buffers;
+
+ /* turbotap */
+ int turbotap_fd;
+ int sock_fd;
+ int rx_ready;
+ struct mmsghdr rx_msg[MAX_RECV];
+ struct mmsghdr tx_msg[MAX_SEND];
+} turbotap_interface_t;
+
+typedef struct {
+ /* Vector of VLIB rx buffers to use. */
+ u32 * rx_buffers;
+
+ /* record and put back unused rx buffers */
+ u32 * unused_buffer_list;
+
+ /* Default MTU for newly created turbotap interface. */
+ u32 mtu_bytes;
+
+ /* Vector of tap interfaces */
+ turbotap_interface_t * turbotap_interfaces;
+
+ /* Vector of deleted tap interfaces */
+ u32 * turbotap_inactive_interfaces;
+
+ /* Bitmap of tap interfaces with pending reads */
+ uword * pending_read_bitmap;
+
+ /* Hash table to find turbotap interface given hw_if_index */
+ uword * turbotap_interface_index_by_sw_if_index;
+
+ /* Hash table to find turbotap interface given unix fd */
+ uword * turbotap_interface_index_by_unix_fd;
+
+ /* renumbering table */
+ u32 * show_dev_instance_by_real_dev_instance;
+
+ /* 1 => disable CLI */
+ int is_disabled;
+
+ /* convenience */
+ vlib_main_t * vlib_main;
+ vnet_main_t * vnet_main;
+ unix_main_t * unix_main;
+} turbotap_main_t;
+
+extern vnet_device_class_t turbotap_dev_class;
+extern vlib_node_registration_t turbotap_rx_node;
+extern turbotap_main_t turbotap_main;
+
+int vnet_turbotap_connect(vlib_main_t * vm, u8 * intfc_name, u8 *hwaddr_arg,
+ u32 * sw_if_indexp);
+int vnet_turbotap_delete(vlib_main_t *vm, u32 sw_if_index);
+
+#endif