diff options
author | Mohsin KAZMI <sykazmi@cisco.com> | 2016-08-10 15:51:17 +0200 |
---|---|---|
committer | Mohsin KAZMI <sykazmi@cisco.com> | 2016-08-10 17:33:56 +0200 |
commit | adff8bfb431798fc1d4e4571d53074dc3438d14f (patch) | |
tree | ac73cd352be4eba5d1dac60596a39f4bc72b711d /turbotap/turbotap | |
parent | 5d7ae6ad224dab352219e3a60d40f0ed5af2be22 (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>
Diffstat (limited to 'turbotap/turbotap')
-rw-r--r-- | turbotap/turbotap/cli.c | 169 | ||||
-rw-r--r-- | turbotap/turbotap/device.c | 227 | ||||
-rw-r--r-- | turbotap/turbotap/node.c | 362 | ||||
-rw-r--r-- | turbotap/turbotap/turbotap.c | 474 | ||||
-rw-r--r-- | turbotap/turbotap/turbotap.h | 114 |
5 files changed, 1346 insertions, 0 deletions
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 |