diff options
Diffstat (limited to 'vnet')
-rw-r--r-- | vnet/Makefile.am | 13 | ||||
-rw-r--r-- | vnet/vnet/api_errno.h | 3 | ||||
-rw-r--r-- | vnet/vnet/devices/af_packet/af_packet.c | 279 | ||||
-rw-r--r-- | vnet/vnet/devices/af_packet/af_packet.h | 57 | ||||
-rw-r--r-- | vnet/vnet/devices/af_packet/cli.c | 89 | ||||
-rw-r--r-- | vnet/vnet/devices/af_packet/device.c | 198 | ||||
-rw-r--r-- | vnet/vnet/devices/af_packet/node.c | 279 |
7 files changed, 917 insertions, 1 deletions
diff --git a/vnet/Makefile.am b/vnet/Makefile.am index cf1fb1e0ab3..ff8a31dae26 100644 --- a/vnet/Makefile.am +++ b/vnet/Makefile.am @@ -607,6 +607,19 @@ nobase_include_HEADERS += \ vnet/devices/ssvm/ssvm_eth.h ######################################## +# Linux packet interface +######################################## + +libvnet_la_SOURCES += \ + vnet/devices/af_packet/af_packet.c \ + vnet/devices/af_packet/device.c \ + vnet/devices/af_packet/node.c \ + vnet/devices/af_packet/cli.c + +nobase_include_HEADERS += \ + vnet/devices/af_packet/af_packet.h + +######################################## # Unix kernel related ######################################## diff --git a/vnet/vnet/api_errno.h b/vnet/vnet/api_errno.h index b4b55354650..69535d3fa97 100644 --- a/vnet/vnet/api_errno.h +++ b/vnet/vnet/api_errno.h @@ -68,7 +68,8 @@ _(UNEXPECTED_INTF_STATE, -74, "Unexpected interface state") \ _(TUNNEL_EXIST, -75, "Tunnel already exists") \ _(INVALID_DECAP_NEXT, -76, "Invalid decap-next") \ _(RESPONSE_NOT_READY, -77, "Response not ready") \ -_(NOT_CONNECTED, -78, "Not connected to the data plane") +_(NOT_CONNECTED, -78, "Not connected to the data plane") \ +_(IF_ALREADY_EXISTS, -79, "Interface already exists") typedef enum { #define _(a,b,c) VNET_API_ERROR_##a = (b), diff --git a/vnet/vnet/devices/af_packet/af_packet.c b/vnet/vnet/devices/af_packet/af_packet.c new file mode 100644 index 00000000000..93c8e0c6c95 --- /dev/null +++ b/vnet/vnet/devices/af_packet/af_packet.c @@ -0,0 +1,279 @@ +/* + *------------------------------------------------------------------ + * af_packet.c - linux kernel packet interface + * + * Copyright (c) 2016 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *------------------------------------------------------------------ + */ + +#include <linux/if_ether.h> +#include <linux/if_packet.h> + +#include <vlib/vlib.h> +#include <vlib/unix/unix.h> +#include <vnet/ip/ip.h> +#include <vnet/ethernet/ethernet.h> + +#include <vnet/devices/af_packet/af_packet.h> + +#define AF_PACKET_DEBUG_SOCKET 0 + +#define AF_PACKET_TX_FRAMES_PER_BLOCK 1024 +#define AF_PACKET_TX_FRAME_SIZE (2048 * 5) +#define AF_PACKET_TX_BLOCK_NR 1 +#define AF_PACKET_TX_FRAME_NR (AF_PACKET_TX_BLOCK_NR * \ + AF_PACKET_TX_FRAMES_PER_BLOCK) +#define AF_PACKET_TX_BLOCK_SIZE (AF_PACKET_TX_FRAME_SIZE * \ + AF_PACKET_TX_FRAMES_PER_BLOCK) + +#define AF_PACKET_RX_FRAMES_PER_BLOCK 1024 +#define AF_PACKET_RX_FRAME_SIZE (2048 * 5) +#define AF_PACKET_RX_BLOCK_NR 1 +#define AF_PACKET_RX_FRAME_NR (AF_PACKET_RX_BLOCK_NR * \ + AF_PACKET_RX_FRAMES_PER_BLOCK) +#define AF_PACKET_RX_BLOCK_SIZE (AF_PACKET_RX_FRAME_SIZE * \ + AF_PACKET_RX_FRAMES_PER_BLOCK) + +#if AF_PACKET_DEBUG_SOCKET == 1 +#define DBG_SOCK(args...) clib_warning(args); +#else +#define DBG_SOCK(args...) +#endif + +/*defined in net/if.h but clashes with dpdk headers */ +unsigned int if_nametoindex(const char *ifname); + +typedef struct tpacket_req tpacket_req_t; + +static u32 +af_packet_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags) +{ + /* nothing for now */ + return 0; +} + +static clib_error_t * af_packet_fd_read_ready (unix_file_t * uf) +{ + vlib_main_t * vm = vlib_get_main(); + af_packet_main_t * apm = &af_packet_main; + u32 idx = uf->private_data; + + apm->pending_input_bitmap = clib_bitmap_set (apm->pending_input_bitmap, idx, 1); + + /* Schedule the rx node */ + vlib_node_set_interrupt_pending (vm, af_packet_input_node.index); + + return 0; +} + +static int +create_packet_v2_sock(u8 * name, tpacket_req_t * rx_req, tpacket_req_t * tx_req, + int *fd, u8 ** ring) +{ + int ret, err; + struct sockaddr_ll sll; + uint host_if_index; + int ver = TPACKET_V2; + socklen_t req_sz = sizeof(struct tpacket_req); + u32 ring_sz = rx_req->tp_block_size * rx_req->tp_block_nr + + tx_req->tp_block_size * tx_req->tp_block_nr; + + host_if_index = if_nametoindex((const char *) name); + + if (!host_if_index) + { + DBG_SOCK("Wrong host interface name"); + ret = VNET_API_ERROR_INVALID_INTERFACE; + goto error; + } + + if ((*fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) + { + DBG_SOCK("Failed to create socket"); + ret = VNET_API_ERROR_SYSCALL_ERROR_1; + goto error; + } + + if ((err = setsockopt(*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof(ver))) < 0) + { + DBG_SOCK("Failed to set rx packet interface version"); + ret = VNET_API_ERROR_SYSCALL_ERROR_1; + goto error; + } + + int opt = 1; + if ((err = setsockopt(*fd, SOL_PACKET, PACKET_LOSS, &opt, sizeof(opt))) < 0) + { + DBG_SOCK("Failed to set rx packet interface version"); + ret = VNET_API_ERROR_SYSCALL_ERROR_1; + goto error; + } + + if ((err = setsockopt(*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz)) < 0) + { + DBG_SOCK("Failed to set packet rx ring options"); + ret = VNET_API_ERROR_SYSCALL_ERROR_1; + goto error; + } + + if ((err = setsockopt(*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz)) < 0) + { + DBG_SOCK("Failed to set packet rx ring options"); + ret = VNET_API_ERROR_SYSCALL_ERROR_1; + goto error; + } + + *ring = mmap(NULL, ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, *fd, 0); + if (*ring == MAP_FAILED) + { + DBG_SOCK("mmap failure"); + ret = VNET_API_ERROR_SYSCALL_ERROR_1; + goto error; + } + + memset(&sll, 0, sizeof(sll)); + sll.sll_family = PF_PACKET; + sll.sll_protocol = htons(ETH_P_ALL); + sll.sll_ifindex = host_if_index; + + if ((err = bind(*fd, (struct sockaddr *) &sll, sizeof(sll))) < 0) + { + DBG_SOCK("Failed to bind rx packet socket (error %d)", err); + ret = VNET_API_ERROR_SYSCALL_ERROR_1; + goto error; + } + + return 0; +error: + close(*fd); + return ret; +} + +int +af_packet_create_if(vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set) +{ + af_packet_main_t * apm = &af_packet_main; + int ret, fd = -1; + struct tpacket_req * rx_req = 0; + struct tpacket_req * tx_req = 0; + u8 * ring = 0; + af_packet_if_t * apif = 0; + u8 hw_addr[6]; + clib_error_t * error; + vnet_sw_interface_t * sw; + vnet_main_t *vnm = vnet_get_main(); + uword * p; + uword if_index; + + p = mhash_get (&apm->if_index_by_host_if_name, host_if_name); + if (p) + { + return VNET_API_ERROR_SUBIF_ALREADY_EXISTS; + } + + vec_validate(rx_req, 0); + rx_req->tp_block_size = AF_PACKET_RX_BLOCK_SIZE; + rx_req->tp_frame_size = AF_PACKET_RX_FRAME_SIZE; + rx_req->tp_block_nr = AF_PACKET_RX_BLOCK_NR; + rx_req->tp_frame_nr = AF_PACKET_RX_FRAME_NR; + + vec_validate(tx_req, 0); + tx_req->tp_block_size = AF_PACKET_TX_BLOCK_SIZE; + tx_req->tp_frame_size = AF_PACKET_TX_FRAME_SIZE; + tx_req->tp_block_nr = AF_PACKET_TX_BLOCK_NR; + tx_req->tp_frame_nr = AF_PACKET_TX_FRAME_NR; + + ret = create_packet_v2_sock(host_if_name, rx_req, tx_req, &fd, &ring); + + if (ret != 0) + goto error; + + /* So far everything looks good, let's create interface */ + vec_add2 (apm->interfaces, apif, 1); + if_index = apif - apm->interfaces; + + apif->fd = fd; + apif->rx_ring = ring; + apif->tx_ring = ring + rx_req->tp_block_size * rx_req->tp_block_nr; + apif->rx_req = rx_req; + apif->tx_req = tx_req; + apif->host_if_name = host_if_name; + + { + unix_file_t template = {0}; + template.read_function = af_packet_fd_read_ready; + template.file_descriptor = fd; + template.private_data = if_index; + template.flags = UNIX_FILE_EVENT_EDGE_TRIGGERED; + apif->unix_file_index = unix_file_add (&unix_main, &template); + } + + /*use configured or generate random MAC address */ + if (hw_addr_set) + memcpy(hw_addr, hw_addr_set, 6); + else + { + f64 now = vlib_time_now(vm); + u32 rnd; + rnd = (u32) (now * 1e6); + rnd = random_u32 (&rnd); + + memcpy (hw_addr+2, &rnd, sizeof(rnd)); + hw_addr[0] = 2; + hw_addr[1] = 0xfe; + } + + error = ethernet_register_interface(vnm, af_packet_device_class.index, + if_index, hw_addr, &apif->hw_if_index, + af_packet_eth_flag_change); + + if (error) + { + memset(apif, 0, sizeof(*apif)); + _vec_len(apm->interfaces) -= 1; + clib_error_report (error); + ret = VNET_API_ERROR_SYSCALL_ERROR_1; + goto error; + } + + sw = vnet_get_hw_sw_interface (vnm, apif->hw_if_index); + apif->sw_if_index = sw->sw_if_index; + + vnet_hw_interface_set_flags (vnm, apif->hw_if_index, + VNET_HW_INTERFACE_FLAG_LINK_UP); + + mhash_set_mem (&apm->if_index_by_host_if_name, host_if_name, &if_index, 0); + + return 0; + +error: + vec_free(host_if_name); + vec_free(rx_req); + vec_free(tx_req); + return ret; +} + +static clib_error_t * +af_packet_init (vlib_main_t * vm) +{ + af_packet_main_t * apm = &af_packet_main; + + memset (apm, 0, sizeof (af_packet_main_t)); + + mhash_init_vec_string (&apm->if_index_by_host_if_name, sizeof (uword)); + + return 0; +} + +VLIB_INIT_FUNCTION (af_packet_init); diff --git a/vnet/vnet/devices/af_packet/af_packet.h b/vnet/vnet/devices/af_packet/af_packet.h new file mode 100644 index 00000000000..dd9fa148761 --- /dev/null +++ b/vnet/vnet/devices/af_packet/af_packet.h @@ -0,0 +1,57 @@ +/* + *------------------------------------------------------------------ + * af_packet.h - linux kernel packet interface header file + * + * 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. + *------------------------------------------------------------------ + */ + +typedef struct { + CLIB_CACHE_LINE_ALIGN_MARK(cacheline0); + u8 * host_if_name; + int fd; + struct tpacket_req * rx_req; + struct tpacket_req * tx_req; + u8 * rx_ring; + u8 * tx_ring; + u32 hw_if_index; + u32 sw_if_index; + u32 unix_file_index; + + u32 next_rx_frame; + u32 next_tx_frame; + + u32 per_interface_next_index; + u8 is_admin_up; +} af_packet_if_t; + +typedef struct { + CLIB_CACHE_LINE_ALIGN_MARK(cacheline0); + af_packet_if_t * interfaces; + + /* bitmap of pending rx interfaces */ + uword * pending_input_bitmap; + + /* rx buffer cache */ + u32 * rx_buffers; + + /* hash of host interface names */ + mhash_t if_index_by_host_if_name; +} af_packet_main_t; + +af_packet_main_t af_packet_main; +extern vnet_device_class_t af_packet_device_class; +extern vlib_node_registration_t af_packet_input_node; + +int af_packet_create_if(vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set); diff --git a/vnet/vnet/devices/af_packet/cli.c b/vnet/vnet/devices/af_packet/cli.c new file mode 100644 index 00000000000..d8d829f63ad --- /dev/null +++ b/vnet/vnet/devices/af_packet/cli.c @@ -0,0 +1,89 @@ +/* + *------------------------------------------------------------------ + * af_packet.c - linux kernel packet interface + * + * Copyright (c) 2016 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *------------------------------------------------------------------ + */ + +#include <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 <vlib/vlib.h> +#include <vlib/unix/unix.h> +#include <vnet/ip/ip.h> +#include <vnet/ethernet/ethernet.h> + +#include <vnet/devices/af_packet/af_packet.h> + +static clib_error_t * +af_packet_create_command_fn (vlib_main_t * vm, unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + unformat_input_t _line_input, * line_input = &_line_input; + u8 * host_if_name = NULL; + u8 hwaddr [6]; + u8 * hw_addr_ptr = 0; + int r; + + /* Get a line of input. */ + if (! unformat_user (input, unformat_line_input, line_input)) + return 0; + + while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (line_input, "name %s", &host_if_name)) + ; + else if (unformat (line_input, "hw-addr %U", unformat_ethernet_address, hwaddr)) + hw_addr_ptr = hwaddr; + else + return clib_error_return (0, "unknown input `%U'", format_unformat_error, input); + } + unformat_free (line_input); + + if (host_if_name == NULL) + return clib_error_return (0, "missing host interface name"); + + r = af_packet_create_if(vm, host_if_name, hw_addr_ptr); + + if (r == VNET_API_ERROR_SYSCALL_ERROR_1) + return clib_error_return(0, "%s (errno %d)", strerror (errno), errno); + + if (r == VNET_API_ERROR_INVALID_INTERFACE) + return clib_error_return(0, "Invalid interface name"); + + if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS) + return clib_error_return(0, "Interface elready exists"); + + return 0; +} + +VLIB_CLI_COMMAND (af_packet_create_command, static) = { + .path = "create host-interface", + .short_help = "create host-interface name <interface name> [hw-addr <mac>]", + .function = af_packet_create_command_fn, +}; + +clib_error_t * +af_packet_cli_init (vlib_main_t * vm) +{ + return 0; +} + +VLIB_INIT_FUNCTION (af_packet_cli_init); diff --git a/vnet/vnet/devices/af_packet/device.c b/vnet/vnet/devices/af_packet/device.c new file mode 100644 index 00000000000..c00c6895801 --- /dev/null +++ b/vnet/vnet/devices/af_packet/device.c @@ -0,0 +1,198 @@ +/* + *------------------------------------------------------------------ + * af_packet.c - linux kernel packet interface + * + * Copyright (c) 2016 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *------------------------------------------------------------------ + */ + +#include <linux/if_packet.h> + +#include <vlib/vlib.h> +#include <vlib/unix/unix.h> +#include <vnet/ip/ip.h> +#include <vnet/ethernet/ethernet.h> + +#include <vnet/devices/af_packet/af_packet.h> + +#define foreach_af_packet_tx_func_error \ +_(FRAME_NOT_READY, "tx frame not ready") + +typedef enum { +#define _(f,s) AF_PACKET_TX_ERROR_##f, + foreach_af_packet_tx_func_error +#undef _ + AF_PACKET_TX_N_ERROR, +} af_packet_tx_func_error_t; + +static char * af_packet_tx_func_error_strings[] = { +#define _(n,s) s, + foreach_af_packet_tx_func_error +#undef _ +}; + + +static u8 * format_af_packet_device_name (u8 * s, va_list * args) +{ + u32 i = va_arg (*args, u32); + af_packet_main_t * apm = &af_packet_main; + af_packet_if_t * apif = vec_elt_at_index (apm->interfaces, i); + + s = format (s, "host-%s", apif->host_if_name); + return s; +} + +static u8 * format_af_packet_device (u8 * s, va_list * args) +{ + s = format (s, "Linux PACKET socket interface"); + return s; +} + +static u8 * format_af_packet_tx_trace (u8 * s, va_list * args) +{ + s = format (s, "Unimplemented..."); + return s; +} + +static uword +af_packet_interface_tx (vlib_main_t * vm, + vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + af_packet_main_t * apm = &af_packet_main; + u32 * buffers = vlib_frame_args (frame); + u32 n_left = frame->n_vectors; + u32 n_sent = 0; + vnet_interface_output_runtime_t * rd = (void *) node->runtime_data; + af_packet_if_t * apif = vec_elt_at_index (apm->interfaces, rd->dev_instance); + int block = 0; + u32 block_size = apif->tx_req->tp_block_size; + u32 frame_size = apif->tx_req->tp_frame_size; + u32 frame_num = apif->tx_req->tp_frame_nr; + u8 * block_start = apif->tx_ring + block * block_size; + u32 tx_frame = apif->next_tx_frame; + struct tpacket2_hdr * tph; + u32 frame_not_ready = 0; + + while(n_left > 0) + { + u32 len; + u32 offset = 0; + vlib_buffer_t * b0; + n_left--; + u32 bi = buffers[0]; + buffers++; + + tph = (struct tpacket2_hdr *) (block_start + tx_frame * frame_size); + + if(tph->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING)) + { + frame_not_ready++; + goto next; + } + + do + { + b0 = vlib_get_buffer (vm, bi); + len = b0->current_length; + memcpy((u8 *) tph + TPACKET_ALIGN(sizeof(struct tpacket2_hdr)) + offset, + vlib_buffer_get_current(b0), len); + offset += len; + } + while ((bi = b0->next_buffer)); + + tph->tp_len = tph->tp_snaplen = offset; + tph->tp_status = TP_STATUS_SEND_REQUEST; + n_sent++; +next: + tx_frame = (tx_frame + 1) % frame_num; + } + + CLIB_MEMORY_BARRIER(); + + if (n_sent) + { + apif->next_tx_frame = tx_frame; + if (sendto(apif->fd, NULL, 0, MSG_DONTWAIT, NULL, 0) == -1) + clib_unix_error("tx sendto failure"); + } + + if (frame_not_ready) + vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_FRAME_NOT_READY, + frame_not_ready); + + vlib_buffer_free (vm, vlib_frame_args (frame), frame->n_vectors); + return frame->n_vectors; +} + +static void +af_packet_set_interface_next_node (vnet_main_t *vnm, u32 hw_if_index, + u32 node_index) +{ + af_packet_main_t * apm = &af_packet_main; + vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index); + af_packet_if_t * apif = pool_elt_at_index (apm->interfaces, hw->dev_instance); + + /* Shut off redirection */ + if (node_index == ~0) + { + apif->per_interface_next_index = node_index; + return; + } + + apif->per_interface_next_index = + vlib_node_add_next (vlib_get_main(), af_packet_input_node.index, node_index); +} + +static void af_packet_clear_hw_interface_counters (u32 instance) +{ + /* Nothing for now */ +} + +static clib_error_t * +af_packet_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) +{ + af_packet_main_t * apm = &af_packet_main; + vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index); + af_packet_if_t * apif = pool_elt_at_index (apm->interfaces, hw->dev_instance); + + apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0; + + return 0; +} + +static clib_error_t * +af_packet_subif_add_del_function (vnet_main_t * vnm, + u32 hw_if_index, + struct vnet_sw_interface_t * st, + int is_add) +{ + /* Nothing for now */ + return 0; +} + +VNET_DEVICE_CLASS (af_packet_device_class) = { + .name = "af-packet", + .tx_function = af_packet_interface_tx, + .format_device_name = format_af_packet_device_name, + .format_device = format_af_packet_device, + .format_tx_trace = format_af_packet_tx_trace, + .tx_function_n_errors = AF_PACKET_TX_N_ERROR, + .tx_function_error_strings = af_packet_tx_func_error_strings, + .rx_redirect_to_node = af_packet_set_interface_next_node, + .clear_counters = af_packet_clear_hw_interface_counters, + .admin_up_down_function = af_packet_interface_admin_up_down, + .subif_add_del_function = af_packet_subif_add_del_function, + .no_flatten_output_chains = 1, +};
\ No newline at end of file diff --git a/vnet/vnet/devices/af_packet/node.c b/vnet/vnet/devices/af_packet/node.c new file mode 100644 index 00000000000..eadb59238a7 --- /dev/null +++ b/vnet/vnet/devices/af_packet/node.c @@ -0,0 +1,279 @@ +/* + *------------------------------------------------------------------ + * af_packet.c - linux kernel packet interface + * + * Copyright (c) 2016 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *------------------------------------------------------------------ + */ + +#include <linux/if_packet.h> + +#include <vlib/vlib.h> +#include <vlib/unix/unix.h> +#include <vnet/ip/ip.h> +#include <vnet/ethernet/ethernet.h> + +#include <vnet/devices/af_packet/af_packet.h> + +#define foreach_af_packet_input_error + +typedef enum { +#define _(f,s) AF_PACKET_INPUT_ERROR_##f, + foreach_af_packet_input_error +#undef _ + AF_PACKET_INPUT_N_ERROR, +} af_packet_input_error_t; + +static char * af_packet_input_error_strings[] = { +#define _(n,s) s, + foreach_af_packet_input_error +#undef _ +}; + +enum { + AF_PACKET_INPUT_NEXT_DROP, + AF_PACKET_INPUT_NEXT_ETHERNET_INPUT, + AF_PACKET_INPUT_N_NEXT, +}; + +typedef struct { + u32 next_index; + u32 hw_if_index; + int block; + struct tpacket2_hdr tph; +} af_packet_input_trace_t; + +static u8 * format_af_packet_input_trace (u8 * s, va_list * args) +{ + CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); + CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); + af_packet_input_trace_t * t = va_arg (*args, af_packet_input_trace_t *); + uword indent = format_get_indent (s); + + s = format (s, "af_packet: hw_if_index %d next-index %d", + t->hw_if_index, t->next_index); + + s = format (s, "\n%Utpacket2_hdr:\n%Ustatus 0x%x len %u snaplen %u mac %u net %u" + "\n%Usec 0x%x nsec 0x%x vlan_tci %u" +#ifdef TP_STATUS_VLAN_TPID_VALID + " vlan_tpid %u" +#endif + , + format_white_space, indent + 2, + format_white_space, indent + 4, + t->tph.tp_status, + t->tph.tp_len, + t->tph.tp_snaplen, + t->tph.tp_mac, + t->tph.tp_net, + format_white_space, indent + 4, + t->tph.tp_sec, + t->tph.tp_nsec, + t->tph.tp_vlan_tci, +#ifdef TP_STATUS_VLAN_TPID_VALID + t->tph.tp_vlan_tpid, +#endif + t->tph.tp_net); + 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); +#if DPDK > 0 + struct rte_mbuf * mbuf = ((struct rte_mbuf *) b) - 1; + struct rte_mbuf * first_mbuf = ((struct rte_mbuf *) first_b) - 1; + struct rte_mbuf * prev_mbuf = ((struct rte_mbuf *) prev_b) - 1; +#endif + + /* 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 + 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 +} + +always_inline uword +af_packet_device_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node, + vlib_frame_t * frame, u32 device_idx) +{ + af_packet_main_t * apm = &af_packet_main; + af_packet_if_t * apif = vec_elt_at_index(apm->interfaces, device_idx); + struct tpacket2_hdr *tph; + u32 next_index = AF_PACKET_INPUT_NEXT_ETHERNET_INPUT; + u32 block = 0; + u32 rx_frame; + u32 n_free_bufs; + u32 n_rx_packets = 0; + u32 n_rx_bytes = 0; + u32 * to_next = 0; + u32 block_size = apif->rx_req->tp_block_size; + u32 frame_size = apif->rx_req->tp_frame_size; + u32 frame_num = apif->rx_req->tp_frame_nr; + u8 * block_start = apif->rx_ring + block * block_size; + uword n_trace = vlib_get_trace_count (vm, node); + u32 n_buffer_bytes = vlib_buffer_free_list_buffer_size (vm, + VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); + u32 min_bufs = apif->rx_req->tp_frame_size / n_buffer_bytes; + + n_free_bufs = vec_len (apm->rx_buffers); + if (PREDICT_FALSE(n_free_bufs < VLIB_FRAME_SIZE)) + { + vec_validate(apm->rx_buffers, VLIB_FRAME_SIZE + n_free_bufs - 1); + n_free_bufs += vlib_buffer_alloc(vm, &apm->rx_buffers[n_free_bufs], VLIB_FRAME_SIZE); + _vec_len (apm->rx_buffers) = n_free_bufs; + } + + rx_frame = apif->next_rx_frame; + tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size); + while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs)) + { + vlib_buffer_t * b0, * first_b0 = 0; + u32 next0 = AF_PACKET_INPUT_NEXT_ETHERNET_INPUT; + + u32 n_left_to_next; + vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); + while ((tph->tp_status & TP_STATUS_USER) && (n_free_bufs > min_bufs) && + n_left_to_next) + { + u32 data_len = tph->tp_snaplen; + u32 offset = 0; + u32 bi0, first_bi0 = 0, prev_bi0; + + while (data_len) + { + /* grab free buffer */ + u32 last_empty_buffer = vec_len (apm->rx_buffers) - 1; + prev_bi0 = bi0; + bi0 = apm->rx_buffers[last_empty_buffer]; + b0 = vlib_get_buffer (vm, bi0); + _vec_len (apm->rx_buffers) = last_empty_buffer; + n_free_bufs--; + + /* copy data */ + u32 bytes_to_copy = data_len > n_buffer_bytes ? n_buffer_bytes : data_len; + b0->current_data = 0; + memcpy (vlib_buffer_get_current (b0), (u8 *) tph + tph->tp_mac + offset, bytes_to_copy); + + /* fill buffer header */ + b0->clone_count = 0; + b0->current_length = bytes_to_copy; + + if (offset == 0) + { + b0->total_length_not_including_first_buffer = 0; + b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID; + vnet_buffer(b0)->sw_if_index[VLIB_RX] = apif->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); + + offset += bytes_to_copy; + data_len -= bytes_to_copy; + } + n_rx_packets++; + n_rx_bytes += tph->tp_snaplen; + to_next[0] = first_bi0; + to_next += 1; + n_left_to_next--; + + /* trace */ + VLIB_BUFFER_TRACE_TRAJECTORY_INIT(first_b0); + if (PREDICT_FALSE(n_trace > 0)) + { + af_packet_input_trace_t *tr; + vlib_trace_buffer (vm, node, next0, first_b0, /* follow_chain */ 0); + vlib_set_trace_count (vm, node, --n_trace); + tr = vlib_add_trace (vm, node, first_b0, sizeof (*tr)); + tr->next_index = next0; + tr->hw_if_index = apif->hw_if_index; + memcpy(&tr->tph, tph, sizeof(struct tpacket2_hdr)); + } + /* enque and take next packet */ + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, + n_left_to_next, first_bi0, next0); + + /* next packet */ + tph->tp_status = TP_STATUS_KERNEL; + rx_frame = (rx_frame + 1) % frame_num; + tph = (struct tpacket2_hdr *) (block_start + rx_frame * frame_size); + } + + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + + apif->next_rx_frame = rx_frame; + + vlib_increment_combined_counter + (vnet_get_main()->interface_main.combined_sw_if_counters + + VNET_INTERFACE_COUNTER_RX, + os_get_cpu_number(), + apif->hw_if_index, + n_rx_packets, n_rx_bytes); + + return n_rx_packets; +} + +static uword +af_packet_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node, + vlib_frame_t * frame) +{ + int i; + u32 n_rx_packets = 0; + + af_packet_main_t * apm = &af_packet_main; + + clib_bitmap_foreach (i, apm->pending_input_bitmap, + ({ + clib_bitmap_set (apm->pending_input_bitmap, i, 1); + n_rx_packets += af_packet_device_input_fn(vm, node, frame, i); + })); + + return n_rx_packets; +} + + +VLIB_REGISTER_NODE (af_packet_input_node) = { + .function = af_packet_input_fn, + .name = "af-packet-input", + .format_trace = format_af_packet_input_trace, + .type = VLIB_NODE_TYPE_INPUT, + .state = VLIB_NODE_STATE_INTERRUPT, + .n_errors = AF_PACKET_INPUT_N_ERROR, + .error_strings = af_packet_input_error_strings, + + .n_next_nodes = AF_PACKET_INPUT_N_NEXT, + .next_nodes = { + [AF_PACKET_INPUT_NEXT_DROP] = "error-drop", + [AF_PACKET_INPUT_NEXT_ETHERNET_INPUT] = "ethernet-input", + }, +}; |