diff options
Diffstat (limited to 'src/vnet/devices')
45 files changed, 1108 insertions, 9387 deletions
diff --git a/src/vnet/devices/af_packet/FEATURE.yaml b/src/vnet/devices/af_packet/FEATURE.yaml deleted file mode 100644 index 25d8b2b5964..00000000000 --- a/src/vnet/devices/af_packet/FEATURE.yaml +++ /dev/null @@ -1,15 +0,0 @@ ---- -name: host-interface Device AF_PACKET -maintainer: Damjan Marion <damarion@cisco.com> -features: - - L4 checksum offload -description: "Create a host interface that will attach to a linux AF_PACKET - interface, one side of a veth pair. The veth pair must - already exist. Once created, a new host interface will - exist in VPP with the name 'host-<ifname>', where '<ifname>' - is the name of the specified veth pair. Use the 'show interface' - command to display host interface details." -missing: - - API dump details beyond sw_if_index and name -state: production -properties: [API, CLI, STATS, MULTITHREAD] diff --git a/src/vnet/devices/af_packet/af_packet.api b/src/vnet/devices/af_packet/af_packet.api deleted file mode 100644 index 035096e17a8..00000000000 --- a/src/vnet/devices/af_packet/af_packet.api +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (c) 2015-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. - */ - -option version = "2.0.0"; - -import "vnet/interface_types.api"; -import "vnet/ethernet/ethernet_types.api"; - -/** \brief Create host-interface - @param client_index - opaque cookie to identify the sender - @param context - sender context, to match reply w/ request - @param hw_addr - interface MAC - @param use_random_hw_addr - use random generated MAC - @param host_if_name - interface name -*/ -define af_packet_create -{ - u32 client_index; - u32 context; - - vl_api_mac_address_t hw_addr; - bool use_random_hw_addr; - string host_if_name[64]; -}; - -/** \brief Create host-interface response - @param context - sender context, to match reply w/ request - @param retval - return value for request -*/ -define af_packet_create_reply -{ - u32 context; - i32 retval; - vl_api_interface_index_t sw_if_index; -}; - -/** \brief Delete host-interface - @param client_index - opaque cookie to identify the sender - @param context - sender context, to match reply w/ request - @param host_if_name - interface name -*/ -autoreply define af_packet_delete -{ - u32 client_index; - u32 context; - - string host_if_name[64]; -}; - -/** \brief Set l4 offload checksum calculation - @param client_index - opaque cookie to identify the sender - @param context - sender context, to match reply w/ request -*/ -autoreply define af_packet_set_l4_cksum_offload -{ - u32 client_index; - u32 context; - - vl_api_interface_index_t sw_if_index; - bool set; -}; - -/** \brief Dump af_packet interfaces request */ -define af_packet_dump -{ - u32 client_index; - u32 context; -}; - -/** \brief Reply for af_packet dump request - @param sw_if_index - software index of af_packet interface - @param host_if_name - interface name -*/ -define af_packet_details -{ - u32 context; - vl_api_interface_index_t sw_if_index; - string host_if_name[64]; -}; - -/* - * Local Variables: - * eval: (c-set-style "gnu") - * End: - */ diff --git a/src/vnet/devices/af_packet/af_packet.c b/src/vnet/devices/af_packet/af_packet.c deleted file mode 100644 index cace3450e3e..00000000000 --- a/src/vnet/devices/af_packet/af_packet.c +++ /dev/null @@ -1,587 +0,0 @@ -/* - *------------------------------------------------------------------ - * 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 <sys/ioctl.h> -#include <net/if.h> -#include <dirent.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <fcntl.h> - -#include <vppinfra/linux/sysfs.h> -#include <vlib/vlib.h> -#include <vlib/unix/unix.h> -#include <vnet/ip/ip.h> -#include <vnet/devices/netlink.h> -#include <vnet/ethernet/ethernet.h> -#include <vnet/interface/rx_queue_funcs.h> - -#include <vnet/devices/af_packet/af_packet.h> - -af_packet_main_t af_packet_main; - -#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) - -/*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) -{ - clib_error_t *error; - af_packet_main_t *apm = &af_packet_main; - af_packet_if_t *apif = - pool_elt_at_index (apm->interfaces, hi->dev_instance); - - if (flags == ETHERNET_INTERFACE_FLAG_MTU) - { - error = - vnet_netlink_set_link_mtu (apif->host_if_index, hi->max_packet_bytes); - - if (error) - { - vlib_log_err (apm->log_class, "netlink failed to change MTU: %U", - format_clib_error, error); - clib_error_free (error); - return VNET_API_ERROR_SYSCALL_ERROR_1; - } - else - apif->host_mtu = hi->max_packet_bytes; - } - - return 0; -} - -static int -af_packet_read_mtu (af_packet_if_t *apif) -{ - af_packet_main_t *apm = &af_packet_main; - clib_error_t *error; - error = vnet_netlink_get_link_mtu (apif->host_if_index, &apif->host_mtu); - if (error) - { - vlib_log_err (apm->log_class, "netlink failed to get MTU: %U", - format_clib_error, error); - clib_error_free (error); - return VNET_API_ERROR_SYSCALL_ERROR_1; - } - return 0; -} - -static clib_error_t * -af_packet_fd_read_ready (clib_file_t * uf) -{ - af_packet_main_t *apm = &af_packet_main; - vnet_main_t *vnm = vnet_get_main (); - u32 idx = uf->private_data; - af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, idx); - - apm->pending_input_bitmap = - clib_bitmap_set (apm->pending_input_bitmap, idx, 1); - - /* Schedule the rx node */ - vnet_hw_if_rx_queue_set_int_pending (vnm, apif->queue_index); - return 0; -} - -static int -is_bridge (const u8 * host_if_name) -{ - u8 *s; - DIR *dir = NULL; - - s = format (0, "/sys/class/net/%s/bridge%c", host_if_name, 0); - dir = opendir ((char *) s); - vec_free (s); - - if (dir) - { - closedir (dir); - return 0; - } - - return -1; -} - -static int -create_packet_v2_sock (int host_if_index, tpacket_req_t * rx_req, - tpacket_req_t * tx_req, int *fd, u8 ** ring) -{ - af_packet_main_t *apm = &af_packet_main; - int ret; - struct sockaddr_ll sll; - 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; - - if ((*fd = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0) - { - vlib_log_debug (apm->log_class, - "Failed to create AF_PACKET socket: %s (errno %d)", - strerror (errno), errno); - ret = VNET_API_ERROR_SYSCALL_ERROR_1; - goto error; - } - - /* bind before rx ring is cfged so we don't receive packets from other interfaces */ - clib_memset (&sll, 0, sizeof (sll)); - sll.sll_family = PF_PACKET; - sll.sll_protocol = htons (ETH_P_ALL); - sll.sll_ifindex = host_if_index; - if (bind (*fd, (struct sockaddr *) &sll, sizeof (sll)) < 0) - { - vlib_log_debug (apm->log_class, - "Failed to bind rx packet socket: %s (errno %d)", - strerror (errno), errno); - ret = VNET_API_ERROR_SYSCALL_ERROR_1; - goto error; - } - - if (setsockopt (*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof (ver)) < 0) - { - vlib_log_debug (apm->log_class, - "Failed to set rx packet interface version: %s (errno %d)", - strerror (errno), errno); - ret = VNET_API_ERROR_SYSCALL_ERROR_1; - goto error; - } - - int opt = 1; - if (setsockopt (*fd, SOL_PACKET, PACKET_LOSS, &opt, sizeof (opt)) < 0) - { - vlib_log_debug (apm->log_class, - "Failed to set packet tx ring error handling option: %s (errno %d)", - strerror (errno), errno); - ret = VNET_API_ERROR_SYSCALL_ERROR_1; - goto error; - } - - if (setsockopt (*fd, SOL_PACKET, PACKET_QDISC_BYPASS, &opt, sizeof (opt)) < - 0) - { - vlib_log_debug (apm->log_class, - "Failed to set qdisc bypass error " - "handling option: %s (errno %d)", - strerror (errno), errno); - } - - if (setsockopt (*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz) < 0) - { - vlib_log_debug (apm->log_class, - "Failed to set packet rx ring options: %s (errno %d)", - strerror (errno), errno); - ret = VNET_API_ERROR_SYSCALL_ERROR_1; - goto error; - } - - if (setsockopt (*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz) < 0) - { - vlib_log_debug (apm->log_class, - "Failed to set packet tx ring options: %s (errno %d)", - strerror (errno), errno); - 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) - { - vlib_log_debug (apm->log_class, "mmap failure: %s (errno %d)", - strerror (errno), errno); - ret = VNET_API_ERROR_SYSCALL_ERROR_1; - goto error; - } - - return 0; -error: - if (*fd >= 0) - { - close (*fd); - *fd = -1; - } - return ret; -} - -int -af_packet_create_if (vlib_main_t * vm, u8 * host_if_name, u8 * hw_addr_set, - u32 * sw_if_index) -{ - af_packet_main_t *apm = &af_packet_main; - int ret, fd = -1, fd2 = -1; - struct tpacket_req *rx_req = 0; - struct tpacket_req *tx_req = 0; - struct ifreq ifr; - u8 *ring = 0; - af_packet_if_t *apif = 0; - u8 hw_addr[6]; - clib_error_t *error; - vnet_sw_interface_t *sw; - vnet_hw_interface_t *hw; - vlib_thread_main_t *tm = vlib_get_thread_main (); - vnet_main_t *vnm = vnet_get_main (); - uword *p; - uword if_index; - u8 *host_if_name_dup = 0; - int host_if_index = -1; - - p = mhash_get (&apm->if_index_by_host_if_name, host_if_name); - if (p) - { - apif = vec_elt_at_index (apm->interfaces, p[0]); - *sw_if_index = apif->sw_if_index; - return VNET_API_ERROR_IF_ALREADY_EXISTS; - } - - host_if_name_dup = vec_dup (host_if_name); - - 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; - - /* - * make sure host side of interface is 'UP' before binding AF_PACKET - * socket on it. - */ - if ((fd2 = socket (AF_UNIX, SOCK_DGRAM, 0)) < 0) - { - vlib_log_debug (apm->log_class, - "Failed to create AF_UNIX socket: %s (errno %d)", - strerror (errno), errno); - ret = VNET_API_ERROR_SYSCALL_ERROR_1; - goto error; - } - - clib_memcpy (ifr.ifr_name, (const char *) host_if_name, - vec_len (host_if_name)); - if (ioctl (fd2, SIOCGIFINDEX, &ifr) < 0) - { - vlib_log_debug (apm->log_class, - "Failed to retrieve the interface (%s) index: %s (errno %d)", - host_if_name, strerror (errno), errno); - ret = VNET_API_ERROR_INVALID_INTERFACE; - goto error; - } - - host_if_index = ifr.ifr_ifindex; - if (ioctl (fd2, SIOCGIFFLAGS, &ifr) < 0) - { - vlib_log_debug (apm->log_class, - "Failed to get the active flag: %s (errno %d)", - strerror (errno), errno); - ret = VNET_API_ERROR_SYSCALL_ERROR_1; - goto error; - } - - if (!(ifr.ifr_flags & IFF_UP)) - { - ifr.ifr_flags |= IFF_UP; - if (ioctl (fd2, SIOCSIFFLAGS, &ifr) < 0) - { - vlib_log_debug (apm->log_class, - "Failed to set the active flag: %s (errno %d)", - strerror (errno), errno); - ret = VNET_API_ERROR_SYSCALL_ERROR_1; - goto error; - } - } - - if (fd2 > -1) - { - close (fd2); - fd2 = -1; - } - - ret = create_packet_v2_sock (host_if_index, rx_req, tx_req, &fd, &ring); - - if (ret != 0) - goto error; - - ret = is_bridge (host_if_name); - - if (ret == 0) /* is a bridge, ignore state */ - host_if_index = -1; - - /* So far everything looks good, let's create interface */ - pool_get (apm->interfaces, apif); - if_index = apif - apm->interfaces; - - apif->host_if_index = host_if_index; - 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_dup; - apif->per_interface_next_index = ~0; - apif->next_tx_frame = 0; - apif->next_rx_frame = 0; - - ret = af_packet_read_mtu (apif); - if (ret != 0) - goto error; - - if (tm->n_vlib_mains > 1) - clib_spinlock_init (&apif->lockp); - - /*use configured or generate random MAC address */ - if (hw_addr_set) - clib_memcpy (hw_addr, hw_addr_set, 6); - else - { - f64 now = vlib_time_now (vm); - u32 rnd; - rnd = (u32) (now * 1e6); - rnd = random_u32 (&rnd); - - clib_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) - { - clib_memset (apif, 0, sizeof (*apif)); - pool_put (apm->interfaces, apif); - vlib_log_err (apm->log_class, "Unable to register interface: %U", - format_clib_error, error); - clib_error_free (error); - ret = VNET_API_ERROR_SYSCALL_ERROR_1; - goto error; - } - - sw = vnet_get_hw_sw_interface (vnm, apif->hw_if_index); - hw = vnet_get_hw_interface (vnm, apif->hw_if_index); - apif->sw_if_index = sw->sw_if_index; - vnet_hw_if_set_input_node (vnm, apif->hw_if_index, - af_packet_input_node.index); - apif->queue_index = vnet_hw_if_register_rx_queue (vnm, apif->hw_if_index, 0, - VNET_HW_IF_RXQ_THREAD_ANY); - - hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE; - vnet_hw_interface_set_flags (vnm, apif->hw_if_index, - VNET_HW_INTERFACE_FLAG_LINK_UP); - - vnet_hw_if_set_rx_queue_mode (vnm, apif->queue_index, - VNET_HW_IF_RX_MODE_INTERRUPT); - vnet_hw_if_update_runtime_data (vnm, apif->hw_if_index); - { - clib_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; - template.description = - format (0, "%U", format_af_packet_device_name, if_index); - apif->clib_file_index = clib_file_add (&file_main, &template); - } - vnet_hw_if_set_rx_queue_file_index (vnm, apif->queue_index, - apif->clib_file_index); - - mhash_set_mem (&apm->if_index_by_host_if_name, host_if_name_dup, &if_index, - 0); - if (sw_if_index) - *sw_if_index = apif->sw_if_index; - - return 0; - -error: - if (fd2 > -1) - { - close (fd2); - fd2 = -1; - } - vec_free (host_if_name_dup); - vec_free (rx_req); - vec_free (tx_req); - return ret; -} - -int -af_packet_delete_if (vlib_main_t * vm, u8 * host_if_name) -{ - vnet_main_t *vnm = vnet_get_main (); - af_packet_main_t *apm = &af_packet_main; - af_packet_if_t *apif; - uword *p; - uword if_index; - u32 ring_sz; - - p = mhash_get (&apm->if_index_by_host_if_name, host_if_name); - if (p == NULL) - { - vlib_log_warn (apm->log_class, "Host interface %s does not exist", - host_if_name); - return VNET_API_ERROR_SYSCALL_ERROR_1; - } - apif = pool_elt_at_index (apm->interfaces, p[0]); - if_index = apif - apm->interfaces; - - /* bring down the interface */ - vnet_hw_interface_set_flags (vnm, apif->hw_if_index, 0); - - /* clean up */ - if (apif->clib_file_index != ~0) - { - clib_file_del (&file_main, file_main.file_pool + apif->clib_file_index); - apif->clib_file_index = ~0; - } - else - close (apif->fd); - - ring_sz = apif->rx_req->tp_block_size * apif->rx_req->tp_block_nr + - apif->tx_req->tp_block_size * apif->tx_req->tp_block_nr; - if (munmap (apif->rx_ring, ring_sz)) - vlib_log_warn (apm->log_class, - "Host interface %s could not free rx/tx ring", - host_if_name); - apif->rx_ring = NULL; - apif->tx_ring = NULL; - apif->fd = -1; - - vec_free (apif->rx_req); - apif->rx_req = NULL; - vec_free (apif->tx_req); - apif->tx_req = NULL; - - vec_free (apif->host_if_name); - apif->host_if_name = NULL; - apif->host_if_index = -1; - - mhash_unset (&apm->if_index_by_host_if_name, host_if_name, &if_index); - - ethernet_delete_interface (vnm, apif->hw_if_index); - - pool_put (apm->interfaces, apif); - - return 0; -} - -int -af_packet_set_l4_cksum_offload (vlib_main_t * vm, u32 sw_if_index, u8 set) -{ - vnet_main_t *vnm = vnet_get_main (); - vnet_hw_interface_t *hw; - - hw = vnet_get_sup_hw_interface (vnm, sw_if_index); - - if (hw->dev_class_index != af_packet_device_class.index) - return VNET_API_ERROR_INVALID_INTERFACE; - - if (set) - { - hw->caps &= ~(VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM | - VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM); - } - else - { - hw->caps |= (VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM | - VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM); - } - return 0; -} - -int -af_packet_dump_ifs (af_packet_if_detail_t ** out_af_packet_ifs) -{ - af_packet_main_t *apm = &af_packet_main; - af_packet_if_t *apif; - af_packet_if_detail_t *r_af_packet_ifs = NULL; - af_packet_if_detail_t *af_packet_if = NULL; - - /* *INDENT-OFF* */ - pool_foreach (apif, apm->interfaces) - { - vec_add2 (r_af_packet_ifs, af_packet_if, 1); - af_packet_if->sw_if_index = apif->sw_if_index; - if (apif->host_if_name) - { - clib_memcpy (af_packet_if->host_if_name, apif->host_if_name, - MIN (ARRAY_LEN (af_packet_if->host_if_name) - 1, - strlen ((const char *) apif->host_if_name))); - } - } - /* *INDENT-ON* */ - - *out_af_packet_ifs = r_af_packet_ifs; - - return 0; -} - -static clib_error_t * -af_packet_init (vlib_main_t * vm) -{ - af_packet_main_t *apm = &af_packet_main; - vlib_thread_main_t *tm = vlib_get_thread_main (); - - clib_memset (apm, 0, sizeof (af_packet_main_t)); - - mhash_init_vec_string (&apm->if_index_by_host_if_name, sizeof (uword)); - - vec_validate_aligned (apm->rx_buffers, tm->n_vlib_mains - 1, - CLIB_CACHE_LINE_BYTES); - - apm->log_class = vlib_log_register_class ("af_packet", 0); - vlib_log_debug (apm->log_class, "initialized"); - - return 0; -} - -VLIB_INIT_FUNCTION (af_packet_init); - -/* - * fd.io coding-style-patch-verification: ON - * - * Local Variables: - * eval: (c-set-style "gnu") - * End: - */ diff --git a/src/vnet/devices/af_packet/af_packet.h b/src/vnet/devices/af_packet/af_packet.h deleted file mode 100644 index fc35b48b85e..00000000000 --- a/src/vnet/devices/af_packet/af_packet.h +++ /dev/null @@ -1,93 +0,0 @@ -/* - *------------------------------------------------------------------ - * 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. - *------------------------------------------------------------------ - */ - -#include <vppinfra/lock.h> - -#include <vlib/log.h> - -typedef struct -{ - u32 sw_if_index; - u8 host_if_name[64]; -} af_packet_if_detail_t; - -typedef struct -{ - CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); - clib_spinlock_t lockp; - u8 *host_if_name; - int host_if_index; - 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 clib_file_index; - - u32 next_rx_frame; - u32 next_tx_frame; - - u32 per_interface_next_index; - u8 is_admin_up; - u32 queue_index; - u32 host_mtu; -} 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; - - /** log class */ - vlib_log_class_t log_class; -} af_packet_main_t; - -extern 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, u32 * sw_if_index); -int af_packet_delete_if (vlib_main_t * vm, u8 * host_if_name); -int af_packet_set_l4_cksum_offload (vlib_main_t * vm, u32 sw_if_index, - u8 set); -int af_packet_dump_ifs (af_packet_if_detail_t ** out_af_packet_ifs); - -format_function_t format_af_packet_device_name; - -#define MIN(x,y) (((x)<(y))?(x):(y)) - -/* - * fd.io coding-style-patch-verification: ON - * - * Local Variables: - * eval: (c-set-style "gnu") - * End: - */ diff --git a/src/vnet/devices/af_packet/af_packet_api.c b/src/vnet/devices/af_packet/af_packet_api.c deleted file mode 100644 index d4d041ffb3c..00000000000 --- a/src/vnet/devices/af_packet/af_packet_api.c +++ /dev/null @@ -1,156 +0,0 @@ -/* - *------------------------------------------------------------------ - * af_packet_api.c - af-packet api - * - * 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 <vnet/vnet.h> -#include <vlibmemory/api.h> - -#include <vnet/interface.h> -#include <vnet/api_errno.h> -#include <vnet/devices/af_packet/af_packet.h> - -#include <vnet/format_fns.h> -#include <vnet/devices/af_packet/af_packet.api_enum.h> -#include <vnet/devices/af_packet/af_packet.api_types.h> - -#define REPLY_MSG_ID_BASE msg_id_base -#include <vlibapi/api_helper_macros.h> - -static u16 msg_id_base; - -static void -vl_api_af_packet_create_t_handler (vl_api_af_packet_create_t * mp) -{ - vlib_main_t *vm = vlib_get_main (); - vl_api_af_packet_create_reply_t *rmp; - int rv = 0; - u8 *host_if_name = NULL; - u32 sw_if_index; - - host_if_name = format (0, "%s", mp->host_if_name); - vec_add1 (host_if_name, 0); - - rv = af_packet_create_if (vm, host_if_name, - mp->use_random_hw_addr ? 0 : mp->hw_addr, - &sw_if_index); - - vec_free (host_if_name); - - /* *INDENT-OFF* */ - REPLY_MACRO2(VL_API_AF_PACKET_CREATE_REPLY, - ({ - rmp->sw_if_index = clib_host_to_net_u32(sw_if_index); - })); - /* *INDENT-ON* */ -} - -static void -vl_api_af_packet_delete_t_handler (vl_api_af_packet_delete_t * mp) -{ - vlib_main_t *vm = vlib_get_main (); - vl_api_af_packet_delete_reply_t *rmp; - int rv = 0; - u8 *host_if_name = NULL; - - host_if_name = format (0, "%s", mp->host_if_name); - vec_add1 (host_if_name, 0); - - rv = af_packet_delete_if (vm, host_if_name); - - vec_free (host_if_name); - - REPLY_MACRO (VL_API_AF_PACKET_DELETE_REPLY); -} - -static void - vl_api_af_packet_set_l4_cksum_offload_t_handler - (vl_api_af_packet_set_l4_cksum_offload_t * mp) -{ - vlib_main_t *vm = vlib_get_main (); - vl_api_af_packet_delete_reply_t *rmp; - int rv = 0; - - rv = af_packet_set_l4_cksum_offload (vm, ntohl (mp->sw_if_index), mp->set); - REPLY_MACRO (VL_API_AF_PACKET_SET_L4_CKSUM_OFFLOAD_REPLY); -} - -static void -af_packet_send_details (vpe_api_main_t * am, - vl_api_registration_t * reg, - af_packet_if_detail_t * af_packet_if, u32 context) -{ - vl_api_af_packet_details_t *mp; - mp = vl_msg_api_alloc (sizeof (*mp)); - clib_memset (mp, 0, sizeof (*mp)); - mp->_vl_msg_id = htons (REPLY_MSG_ID_BASE + VL_API_AF_PACKET_DETAILS); - mp->sw_if_index = htonl (af_packet_if->sw_if_index); - clib_memcpy (mp->host_if_name, af_packet_if->host_if_name, - MIN (ARRAY_LEN (mp->host_if_name) - 1, - strlen ((const char *) af_packet_if->host_if_name))); - - mp->context = context; - vl_api_send_msg (reg, (u8 *) mp); -} - - -static void -vl_api_af_packet_dump_t_handler (vl_api_af_packet_dump_t * mp) -{ - int rv; - vpe_api_main_t *am = &vpe_api_main; - vl_api_registration_t *reg; - af_packet_if_detail_t *out_af_packet_ifs = NULL; - af_packet_if_detail_t *af_packet_if = NULL; - - reg = vl_api_client_index_to_registration (mp->client_index); - if (!reg) - return; - - rv = af_packet_dump_ifs (&out_af_packet_ifs); - if (rv) - return; - - vec_foreach (af_packet_if, out_af_packet_ifs) - { - af_packet_send_details (am, reg, af_packet_if, mp->context); - } - - vec_free (out_af_packet_ifs); -} - -#include <vnet/devices/af_packet/af_packet.api.c> -static clib_error_t * -af_packet_api_hookup (vlib_main_t * vm) -{ - /* - * Set up the (msg_name, crc, message-id) table - */ - REPLY_MSG_ID_BASE = setup_message_id_table (); - - return 0; -} - -VLIB_API_INIT_FUNCTION (af_packet_api_hookup); - -/* - * fd.io coding-style-patch-verification: ON - * - * Local Variables: - * eval: (c-set-style "gnu") - * End: - */ diff --git a/src/vnet/devices/af_packet/cli.c b/src/vnet/devices/af_packet/cli.c deleted file mode 100644 index e5466c9599d..00000000000 --- a/src/vnet/devices/af_packet/cli.c +++ /dev/null @@ -1,268 +0,0 @@ -/* - *------------------------------------------------------------------ - * 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> - -/** - * @file - * @brief CLI for Host Interface Device Driver. - * - * This file contains the source code for CLI for the host interface. - */ - -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; - u32 sw_if_index; - int r; - clib_error_t *error = NULL; - - /* 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 - { - error = clib_error_return (0, "unknown input `%U'", - format_unformat_error, line_input); - goto done; - } - } - - if (host_if_name == NULL) - { - error = clib_error_return (0, "missing host interface name"); - goto done; - } - - r = af_packet_create_if (vm, host_if_name, hw_addr_ptr, &sw_if_index); - - if (r == VNET_API_ERROR_SYSCALL_ERROR_1) - { - error = clib_error_return (0, "%s (errno %d)", strerror (errno), errno); - goto done; - } - - if (r == VNET_API_ERROR_INVALID_INTERFACE) - { - error = clib_error_return (0, "Invalid interface name"); - goto done; - } - - if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS) - { - error = clib_error_return (0, "Interface already exists"); - goto done; - } - - vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (), - sw_if_index); - -done: - vec_free (host_if_name); - unformat_free (line_input); - - return error; -} - -/*? - * Create a host interface that will attach to a linux AF_PACKET - * interface, one side of a veth pair. The veth pair must already - * exist. Once created, a new host interface will exist in VPP - * with the name '<em>host-<ifname></em>', where '<em><ifname></em>' - * is the name of the specified veth pair. Use the - * '<em>show interface</em>' command to display host interface details. - * - * This command has the following optional parameters: - * - * - <b>hw-addr <mac-addr></b> - Optional ethernet address, can be in either - * X:X:X:X:X:X unix or X.X.X cisco format. - * - * @cliexpar - * Example of how to create a host interface tied to one side of an - * existing linux veth pair named vpp1: - * @cliexstart{create host-interface name vpp1} - * host-vpp1 - * @cliexend - * Once the host interface is created, enable the interface using: - * @cliexcmd{set interface state host-vpp1 up} -?*/ -/* *INDENT-OFF* */ -VLIB_CLI_COMMAND (af_packet_create_command, static) = { - .path = "create host-interface", - .short_help = "create host-interface name <ifname> [hw-addr <mac-addr>]", - .function = af_packet_create_command_fn, -}; -/* *INDENT-ON* */ - -static clib_error_t * -af_packet_delete_command_fn (vlib_main_t * vm, unformat_input_t * input, - vlib_cli_command_t * cmd) -{ - unformat_input_t _line_input, *line_input = &_line_input; - u8 *host_if_name = NULL; - clib_error_t *error = NULL; - - /* 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 - { - error = clib_error_return (0, "unknown input `%U'", - format_unformat_error, line_input); - goto done; - } - } - - if (host_if_name == NULL) - { - error = clib_error_return (0, "missing host interface name"); - goto done; - } - - af_packet_delete_if (vm, host_if_name); - -done: - vec_free (host_if_name); - unformat_free (line_input); - - return error; -} - -/*? - * Delete a host interface. Use the linux interface name to identify - * the host interface to be deleted. In VPP, host interfaces are - * named as '<em>host-<ifname></em>', where '<em><ifname></em>' - * is the name of the linux interface. - * - * @cliexpar - * Example of how to delete a host interface named host-vpp1: - * @cliexcmd{delete host-interface name vpp1} -?*/ -/* *INDENT-OFF* */ -VLIB_CLI_COMMAND (af_packet_delete_command, static) = { - .path = "delete host-interface", - .short_help = "delete host-interface name <ifname>", - .function = af_packet_delete_command_fn, -}; -/* *INDENT-ON* */ - -static clib_error_t * -af_packet_set_l4_cksum_offload_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 set = 0; - clib_error_t *error = NULL; - vnet_main_t *vnm = vnet_get_main (); - u32 sw_if_index; - - 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, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index)) - ; - else if (unformat (line_input, "on")) - set = 1; - else if (unformat (line_input, "off")) - set = 0; - else - { - error = clib_error_return (0, "unknown input '%U'", - format_unformat_error, line_input); - goto done; - } - } - - if (af_packet_set_l4_cksum_offload (vm, sw_if_index, set) < 0) - error = clib_error_return (0, "not an af_packet interface"); - -done: - unformat_free (line_input); - return error; -} - -/*? - * Set TCP/UDP offload checksum calculation. Use interface - * name to identify the interface to set TCP/UDP offload checksum - * calculation. - * - * @cliexpar - * Example of how to set TCP/UDP offload checksum calculation on host-vpp0: - * @cliexcmd{set host-interface l4-cksum-offload host-vpp0 off} - * @cliexcmd{set host-interface l4-cksum-offload host-vpp0 on} -?*/ -/* *INDENT-OFF* */ -VLIB_CLI_COMMAND (af_packet_set_l4_cksum_offload_command, static) = { - .path = "set host-interface l4-cksum-offload", - .short_help = "set host-interface l4-cksum-offload <host-if-name> <on|off>", - .function = af_packet_set_l4_cksum_offload_command_fn, -}; -/* *INDENT-ON* */ - -clib_error_t * -af_packet_cli_init (vlib_main_t * vm) -{ - return 0; -} - -VLIB_INIT_FUNCTION (af_packet_cli_init); - -/* - * fd.io coding-style-patch-verification: ON - * - * Local Variables: - * eval: (c-set-style "gnu") - * End: - */ diff --git a/src/vnet/devices/af_packet/device.c b/src/vnet/devices/af_packet/device.c deleted file mode 100644 index 8a6ff1de649..00000000000 --- a/src/vnet/devices/af_packet/device.c +++ /dev/null @@ -1,396 +0,0 @@ -/* - *------------------------------------------------------------------ - * 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 <sys/socket.h> -#include <sys/ioctl.h> -#include <net/if.h> -#include <net/if_arp.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") \ -_(TXRING_EAGAIN, "tx sendto temporary failure") \ -_(TXRING_FATAL, "tx sendto fatal failure") \ -_(TXRING_OVERRUN, "tx ring overrun") - -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 _ -}; - - -#ifndef CLIB_MARCH_VARIANT -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 = pool_elt_at_index (apm->interfaces, i); - - s = format (s, "host-%s", apif->host_if_name); - return s; -} -#endif /* CLIB_MARCH_VARIANT */ - -static u8 * -format_af_packet_device (u8 * s, va_list * args) -{ - u32 dev_instance = va_arg (*args, u32); - u32 indent = format_get_indent (s); - int __clib_unused verbose = va_arg (*args, int); - - af_packet_main_t *apm = &af_packet_main; - af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, dev_instance); - clib_spinlock_lock_if_init (&apif->lockp); - 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; - int block = 0; - u8 *block_start = apif->tx_ring + block * block_size; - u32 tx_frame = apif->next_tx_frame; - struct tpacket2_hdr *tph; - - s = format (s, "Linux PACKET socket interface\n"); - s = format (s, "%Ublock:%d frame:%d\n", format_white_space, indent, - block_size, frame_size); - s = format (s, "%Unext frame:%d\n", format_white_space, indent, - apif->next_tx_frame); - - int n_send_req = 0, n_avail = 0, n_sending = 0, n_tot = 0, n_wrong = 0; - do - { - tph = (struct tpacket2_hdr *) (block_start + tx_frame * frame_size); - tx_frame = (tx_frame + 1) % frame_num; - if (tph->tp_status == 0) - n_avail++; - else if (tph->tp_status & TP_STATUS_SEND_REQUEST) - n_send_req++; - else if (tph->tp_status & TP_STATUS_SENDING) - n_sending++; - else - n_wrong++; - n_tot++; - } - while (tx_frame != apif->next_tx_frame); - s = format (s, "%Uavailable:%d request:%d sending:%d wrong:%d total:%d\n", - format_white_space, indent, n_avail, n_send_req, n_sending, - n_wrong, n_tot); - - clib_spinlock_unlock_if_init (&apif->lockp); - return s; -} - -static u8 * -format_af_packet_tx_trace (u8 * s, va_list * args) -{ - s = format (s, "Unimplemented..."); - return s; -} - -VNET_DEVICE_CLASS_TX_FN (af_packet_device_class) (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_vector_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 = - pool_elt_at_index (apm->interfaces, rd->dev_instance); - clib_spinlock_lock_if_init (&apif->lockp); - 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) - { - 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 (PREDICT_FALSE (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; - clib_memcpy_fast ((u8 *) tph + - TPACKET_ALIGN (sizeof (struct tpacket2_hdr)) + - offset, vlib_buffer_get_current (b0), len); - offset += len; - } - while ((bi = - (b0->flags & VLIB_BUFFER_NEXT_PRESENT) ? b0->next_buffer : 0)); - - tph->tp_len = tph->tp_snaplen = offset; - tph->tp_status = TP_STATUS_SEND_REQUEST; - n_sent++; - - tx_frame = (tx_frame + 1) % frame_num; - - next: - /* check if we've exhausted the ring */ - if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num)) - break; - } - - CLIB_MEMORY_BARRIER (); - - if (PREDICT_TRUE (n_sent)) - { - apif->next_tx_frame = tx_frame; - - if (PREDICT_FALSE (sendto (apif->fd, NULL, 0, MSG_DONTWAIT, NULL, 0) == - -1)) - { - /* Uh-oh, drop & move on, but count whether it was fatal or not. - * Note that we have no reliable way to properly determine the - * disposition of the packets we just enqueued for delivery. - */ - vlib_error_count (vm, node->node_index, - unix_error_is_fatal (errno) ? - AF_PACKET_TX_ERROR_TXRING_FATAL : - AF_PACKET_TX_ERROR_TXRING_EAGAIN, - n_sent); - } - } - - clib_spinlock_unlock_if_init (&apif->lockp); - - if (PREDICT_FALSE (frame_not_ready)) - vlib_error_count (vm, node->node_index, - AF_PACKET_TX_ERROR_FRAME_NOT_READY, frame_not_ready); - - if (PREDICT_FALSE (frame_not_ready + n_sent == frame_num)) - vlib_error_count (vm, node->node_index, AF_PACKET_TX_ERROR_TXRING_OVERRUN, - n_left); - - vlib_buffer_free (vm, vlib_frame_vector_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); - u32 hw_flags; - int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0); - struct ifreq ifr; - - if (0 > fd) - { - vlib_log_warn (apm->log_class, "af_packet_%s could not open socket", - apif->host_if_name); - return 0; - } - - /* if interface is a bridge ignore */ - if (apif->host_if_index < 0) - goto error; /* no error */ - - /* use host_if_index in case host name has changed */ - ifr.ifr_ifindex = apif->host_if_index; - if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0) - { - vlib_log_warn (apm->log_class, - "af_packet_%s ioctl could not retrieve eth name", - apif->host_if_name); - goto error; - } - - apif->is_admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0; - - if ((rv = ioctl (fd, SIOCGIFFLAGS, &ifr)) < 0) - { - vlib_log_warn (apm->log_class, "af_packet_%s error: %d", - apif->is_admin_up ? "up" : "down", rv); - goto error; - } - - if (apif->is_admin_up) - { - hw_flags = VNET_HW_INTERFACE_FLAG_LINK_UP; - ifr.ifr_flags |= IFF_UP; - } - else - { - hw_flags = 0; - ifr.ifr_flags &= ~IFF_UP; - } - - if ((rv = ioctl (fd, SIOCSIFFLAGS, &ifr)) < 0) - { - vlib_log_warn (apm->log_class, "af_packet_%s error: %d", - apif->is_admin_up ? "up" : "down", rv); - goto error; - } - - vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags); - -error: - if (0 <= fd) - close (fd); - - return 0; /* no error */ -} - -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; -} - -static clib_error_t *af_packet_set_mac_address_function - (struct vnet_hw_interface_t *hi, const u8 * old_address, const u8 * address) -{ - af_packet_main_t *apm = &af_packet_main; - af_packet_if_t *apif = - pool_elt_at_index (apm->interfaces, hi->dev_instance); - int rv, fd = socket (AF_UNIX, SOCK_DGRAM, 0); - struct ifreq ifr; - - if (0 > fd) - { - vlib_log_warn (apm->log_class, "af_packet_%s could not open socket", - apif->host_if_name); - return 0; - } - - /* if interface is a bridge ignore */ - if (apif->host_if_index < 0) - goto error; /* no error */ - - /* use host_if_index in case host name has changed */ - ifr.ifr_ifindex = apif->host_if_index; - if ((rv = ioctl (fd, SIOCGIFNAME, &ifr)) < 0) - { - vlib_log_warn - (apm->log_class, - "af_packet_%s ioctl could not retrieve eth name, error: %d", - apif->host_if_name, rv); - goto error; - } - - clib_memcpy (ifr.ifr_hwaddr.sa_data, address, 6); - ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER; - - if ((rv = ioctl (fd, SIOCSIFHWADDR, &ifr)) < 0) - { - vlib_log_warn (apm->log_class, - "af_packet_%s ioctl could not set mac, error: %d", - apif->host_if_name, rv); - goto error; - } - -error: - - if (0 <= fd) - close (fd); - - return 0; /* no error */ -} - -/* *INDENT-OFF* */ -VNET_DEVICE_CLASS (af_packet_device_class) = { - .name = "af-packet", - .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, - .mac_addr_change_function = af_packet_set_mac_address_function, -}; -/* *INDENT-ON* */ - -/* - * fd.io coding-style-patch-verification: ON - * - * Local Variables: - * eval: (c-set-style "gnu") - * End: - */ diff --git a/src/vnet/devices/af_packet/dir.dox b/src/vnet/devices/af_packet/dir.dox deleted file mode 100644 index 78991c6d97f..00000000000 --- a/src/vnet/devices/af_packet/dir.dox +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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. - */ - -/* Doxygen directory documentation */ - -/** -@dir -@brief Host Interface Implementation. - -This directory contains the source code for Host Interface driver. The -Host Interface driver leverages the DPDK AF_PACKET driver. - - -*/ -/*? %%clicmd:group_label Host Interface %% ?*/ -/*? %%syscfg:group_label Host Interface %% ?*/ diff --git a/src/vnet/devices/af_packet/node.c b/src/vnet/devices/af_packet/node.c deleted file mode 100644 index 9fd115f9509..00000000000 --- a/src/vnet/devices/af_packet/node.c +++ /dev/null @@ -1,410 +0,0 @@ -/* - *------------------------------------------------------------------ - * 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/interface/rx_queue_funcs.h> -#include <vnet/feature/feature.h> -#include <vnet/ethernet/packet.h> - -#include <vnet/devices/af_packet/af_packet.h> - -#define foreach_af_packet_input_error \ - _(PARTIAL_PKT, "partial packet") - -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 _ -}; - -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 *); - u32 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 %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, format_ethernet_vlan_tci, t->tph.tp_vlan_tci -#ifdef TP_STATUS_VLAN_TPID_VALID - , t->tph.tp_vlan_tpid -#endif - ); - 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; -} - -static_always_inline void -fill_gso_buffer_flags (vlib_buffer_t *b, u32 gso_size, u8 l4_hdr_sz) -{ - b->flags |= VNET_BUFFER_F_GSO; - vnet_buffer2 (b)->gso_size = gso_size; - vnet_buffer2 (b)->gso_l4_hdr_sz = l4_hdr_sz; -} - -static_always_inline void -mark_tcp_udp_cksum_calc (vlib_buffer_t *b, u8 *l4_hdr_sz) -{ - ethernet_header_t *eth = vlib_buffer_get_current (b); - vnet_buffer_oflags_t oflags = 0; - if (clib_net_to_host_u16 (eth->type) == ETHERNET_TYPE_IP4) - { - ip4_header_t *ip4 = - (vlib_buffer_get_current (b) + sizeof (ethernet_header_t)); - b->flags |= VNET_BUFFER_F_IS_IP4; - if (ip4->protocol == IP_PROTOCOL_TCP) - { - oflags |= VNET_BUFFER_OFFLOAD_F_TCP_CKSUM; - tcp_header_t *tcp = (tcp_header_t *) (vlib_buffer_get_current (b) + - sizeof (ethernet_header_t) + - ip4_header_bytes (ip4)); - tcp->checksum = 0; - *l4_hdr_sz = tcp_header_bytes (tcp); - } - else if (ip4->protocol == IP_PROTOCOL_UDP) - { - oflags |= VNET_BUFFER_OFFLOAD_F_UDP_CKSUM; - udp_header_t *udp = (udp_header_t *) (vlib_buffer_get_current (b) + - sizeof (ethernet_header_t) + - ip4_header_bytes (ip4)); - udp->checksum = 0; - *l4_hdr_sz = sizeof (*udp); - } - vnet_buffer (b)->l3_hdr_offset = sizeof (ethernet_header_t); - vnet_buffer (b)->l4_hdr_offset = - sizeof (ethernet_header_t) + ip4_header_bytes (ip4); - if (oflags) - vnet_buffer_offload_flags_set (b, oflags); - } - else if (clib_net_to_host_u16 (eth->type) == ETHERNET_TYPE_IP6) - { - ip6_header_t *ip6 = - (vlib_buffer_get_current (b) + sizeof (ethernet_header_t)); - b->flags |= VNET_BUFFER_F_IS_IP6; - u16 ip6_hdr_len = sizeof (ip6_header_t); - if (ip6_ext_hdr (ip6->protocol)) - { - ip6_ext_header_t *p = (void *) (ip6 + 1); - ip6_hdr_len += ip6_ext_header_len (p); - while (ip6_ext_hdr (p->next_hdr)) - { - ip6_hdr_len += ip6_ext_header_len (p); - p = ip6_ext_next_header (p); - } - } - if (ip6->protocol == IP_PROTOCOL_TCP) - { - oflags |= VNET_BUFFER_OFFLOAD_F_TCP_CKSUM; - tcp_header_t *tcp = - (tcp_header_t *) (vlib_buffer_get_current (b) + - sizeof (ethernet_header_t) + ip6_hdr_len); - tcp->checksum = 0; - *l4_hdr_sz = tcp_header_bytes (tcp); - } - else if (ip6->protocol == IP_PROTOCOL_UDP) - { - oflags |= VNET_BUFFER_OFFLOAD_F_UDP_CKSUM; - udp_header_t *udp = - (udp_header_t *) (vlib_buffer_get_current (b) + - sizeof (ethernet_header_t) + ip6_hdr_len); - udp->checksum = 0; - *l4_hdr_sz = sizeof (*udp); - } - vnet_buffer (b)->l3_hdr_offset = sizeof (ethernet_header_t); - vnet_buffer (b)->l4_hdr_offset = - sizeof (ethernet_header_t) + ip6_hdr_len; - if (oflags) - vnet_buffer_offload_flags_set (b, oflags); - } -} - -always_inline uword -af_packet_device_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node, - vlib_frame_t * frame, af_packet_if_t * apif) -{ - af_packet_main_t *apm = &af_packet_main; - struct tpacket2_hdr *tph; - u32 next_index = VNET_DEVICE_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 thread_index = vm->thread_index; - u32 n_buffer_bytes = vlib_buffer_get_default_data_size (vm); - u32 min_bufs = apif->rx_req->tp_frame_size / n_buffer_bytes; - - n_free_bufs = vec_len (apm->rx_buffers[thread_index]); - if (PREDICT_FALSE (n_free_bufs < VLIB_FRAME_SIZE)) - { - vec_validate (apm->rx_buffers[thread_index], - VLIB_FRAME_SIZE + n_free_bufs - 1); - n_free_bufs += - vlib_buffer_alloc (vm, &apm->rx_buffers[thread_index][n_free_bufs], - VLIB_FRAME_SIZE); - _vec_len (apm->rx_buffers[thread_index]) = 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 = 0, *first_b0 = 0; - u32 next0 = next_index; - - 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 = 0, first_bi0 = 0, prev_bi0; - u8 l4_hdr_sz = 0; - - while (data_len) - { - /* grab free buffer */ - u32 last_empty_buffer = - vec_len (apm->rx_buffers[thread_index]) - 1; - prev_bi0 = bi0; - bi0 = apm->rx_buffers[thread_index][last_empty_buffer]; - b0 = vlib_get_buffer (vm, bi0); - _vec_len (apm->rx_buffers[thread_index]) = last_empty_buffer; - n_free_bufs--; - - /* copy data */ - u32 bytes_to_copy = - data_len > n_buffer_bytes ? n_buffer_bytes : data_len; - u32 vlan_len = 0; - u32 bytes_copied = 0; - b0->current_data = 0; - /* Kernel removes VLAN headers, so reconstruct VLAN */ - if (PREDICT_FALSE (tph->tp_status & TP_STATUS_VLAN_VALID)) - { - if (PREDICT_TRUE (offset == 0)) - { - clib_memcpy_fast (vlib_buffer_get_current (b0), - (u8 *) tph + tph->tp_mac, - sizeof (ethernet_header_t)); - ethernet_header_t *eth = vlib_buffer_get_current (b0); - ethernet_vlan_header_t *vlan = - (ethernet_vlan_header_t *) (eth + 1); - vlan->priority_cfi_and_id = - clib_host_to_net_u16 (tph->tp_vlan_tci); - vlan->type = eth->type; - eth->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN); - vlan_len = sizeof (ethernet_vlan_header_t); - bytes_copied = sizeof (ethernet_header_t); - } - } - clib_memcpy_fast (((u8 *) vlib_buffer_get_current (b0)) + - bytes_copied + vlan_len, - (u8 *) tph + tph->tp_mac + offset + - bytes_copied, (bytes_to_copy - bytes_copied)); - - /* fill buffer header */ - b0->current_length = bytes_to_copy + vlan_len; - - 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); - if (tph->tp_status & TP_STATUS_CSUMNOTREADY) - mark_tcp_udp_cksum_calc (first_b0, &l4_hdr_sz); - if (tph->tp_snaplen > apif->host_mtu) - fill_gso_buffer_flags (first_b0, apif->host_mtu, - l4_hdr_sz); - } - 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--; - - /* drop partial packets */ - if (PREDICT_FALSE (tph->tp_len != tph->tp_snaplen)) - { - next0 = VNET_DEVICE_INPUT_NEXT_DROP; - first_b0->error = - node->errors[AF_PACKET_INPUT_ERROR_PARTIAL_PKT]; - } - else - { - next0 = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT; - - if (PREDICT_FALSE (apif->per_interface_next_index != ~0)) - next0 = apif->per_interface_next_index; - - /* redirect if feature path enabled */ - vnet_feature_start_device_input_x1 (apif->sw_if_index, &next0, - first_b0); - } - - /* trace */ - if (PREDICT_FALSE - (n_trace > 0 && vlib_trace_buffer (vm, node, next0, first_b0, - /* follow_chain */ 0))) - { - af_packet_input_trace_t *tr; - 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; - clib_memcpy_fast (&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, - vlib_get_thread_index (), apif->hw_if_index, n_rx_packets, n_rx_bytes); - - vnet_device_increment_rx_packets (thread_index, n_rx_packets); - return n_rx_packets; -} - -VLIB_NODE_FN (af_packet_input_node) (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame) -{ - u32 n_rx_packets = 0; - af_packet_main_t *apm = &af_packet_main; - vnet_hw_if_rxq_poll_vector_t *pv; - pv = vnet_hw_if_get_rxq_poll_vector (vm, node); - for (int i = 0; i < vec_len (pv); i++) - { - af_packet_if_t *apif; - apif = vec_elt_at_index (apm->interfaces, pv[i].dev_instance); - if (apif->is_admin_up) - n_rx_packets += af_packet_device_input_fn (vm, node, frame, apif); - } - - return n_rx_packets; -} - -/* *INDENT-OFF* */ -VLIB_REGISTER_NODE (af_packet_input_node) = { - .name = "af-packet-input", - .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED, - .sibling_of = "device-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, -}; -/* *INDENT-ON* */ - - -/* - * fd.io coding-style-patch-verification: ON - * - * Local Variables: - * eval: (c-set-style "gnu") - * End: - */ diff --git a/src/vnet/devices/devices.c b/src/vnet/devices/devices.c index 5c28cadc03c..ee380bebbde 100644 --- a/src/vnet/devices/devices.c +++ b/src/vnet/devices/devices.c @@ -18,6 +18,7 @@ #include <vnet/feature/feature.h> #include <vnet/ip/ip.h> #include <vnet/ethernet/ethernet.h> +#include <vlib/stats/stats.h> vnet_device_main_t vnet_device_main; @@ -28,7 +29,6 @@ device_input_fn (vlib_main_t * vm, vlib_node_runtime_t * node, return 0; } -/* *INDENT-OFF* */ VLIB_REGISTER_NODE (device_input_node) = { .function = device_input_fn, .name = "device-input", @@ -39,29 +39,6 @@ VLIB_REGISTER_NODE (device_input_node) = { .next_nodes = VNET_DEVICE_INPUT_NEXT_NODES, }; -/* Table defines how much we need to advance current data pointer - in the buffer if we shortcut to l3 nodes */ - -const u32 __attribute__((aligned (CLIB_CACHE_LINE_BYTES))) -device_input_next_node_advance[((VNET_DEVICE_INPUT_N_NEXT_NODES / - CLIB_CACHE_LINE_BYTES) +1) * CLIB_CACHE_LINE_BYTES] = -{ - [VNET_DEVICE_INPUT_NEXT_IP4_INPUT] = sizeof (ethernet_header_t), - [VNET_DEVICE_INPUT_NEXT_IP4_NCS_INPUT] = sizeof (ethernet_header_t), - [VNET_DEVICE_INPUT_NEXT_IP6_INPUT] = sizeof (ethernet_header_t), - [VNET_DEVICE_INPUT_NEXT_MPLS_INPUT] = sizeof (ethernet_header_t), -}; - -const u32 __attribute__((aligned (CLIB_CACHE_LINE_BYTES))) -device_input_next_node_flags[((VNET_DEVICE_INPUT_N_NEXT_NODES / - CLIB_CACHE_LINE_BYTES) +1) * CLIB_CACHE_LINE_BYTES] = -{ - [VNET_DEVICE_INPUT_NEXT_IP4_INPUT] = VNET_BUFFER_F_L3_HDR_OFFSET_VALID, - [VNET_DEVICE_INPUT_NEXT_IP4_NCS_INPUT] = VNET_BUFFER_F_L3_HDR_OFFSET_VALID, - [VNET_DEVICE_INPUT_NEXT_IP6_INPUT] = VNET_BUFFER_F_L3_HDR_OFFSET_VALID, - [VNET_DEVICE_INPUT_NEXT_MPLS_INPUT] = VNET_BUFFER_F_L3_HDR_OFFSET_VALID, -}; - VNET_FEATURE_ARC_INIT (device_input, static) = { .arc_name = "device-input", @@ -99,7 +76,23 @@ VNET_FEATURE_INIT (ethernet_input, static) = { .node_name = "ethernet-input", .runs_before = 0, /* not before any other features */ }; -/* *INDENT-ON* */ + +static void +input_rate_collector_fn (vlib_stats_collector_data_t *d) +{ + vlib_stats_segment_t *sm = vlib_stats_get_segment (); + vlib_stats_entry_t *e2 = sm->directory_vector + d->private_data; + static u64 last_input_packets = 0; + f64 dt, now; + + now = vlib_time_now (vlib_get_main ()); + u64 input_packets = vnet_get_aggregate_rx_packets (); + + dt = now - e2->value; + d->entry->value = (f64) (input_packets - last_input_packets) / dt; + last_input_packets = input_packets; + e2->value = now; +} static clib_error_t * vnet_device_init (vlib_main_t * vm) @@ -107,6 +100,7 @@ vnet_device_init (vlib_main_t * vm) vnet_device_main_t *vdm = &vnet_device_main; vlib_thread_main_t *tm = vlib_get_thread_main (); vlib_thread_registration_t *tr; + vlib_stats_collector_reg_t reg = {}; uword *p; vec_validate_aligned (vdm->workers, tm->n_vlib_mains - 1, @@ -120,6 +114,12 @@ vnet_device_init (vlib_main_t * vm) vdm->next_worker_thread_index = tr->first_index; vdm->last_worker_thread_index = tr->first_index + tr->count - 1; } + + reg.private_data = vlib_stats_add_timestamp ("/sys/last_update"); + reg.entry_index = vlib_stats_add_gauge ("/sys/input_rate"); + reg.collect_fn = input_rate_collector_fn; + vlib_stats_register_collector_fn (®); + return 0; } diff --git a/src/vnet/devices/devices.h b/src/vnet/devices/devices.h index e54c7a29130..cadf1f857a6 100644 --- a/src/vnet/devices/devices.h +++ b/src/vnet/devices/devices.h @@ -67,8 +67,6 @@ typedef struct extern vnet_device_main_t vnet_device_main; extern vlib_node_registration_t device_input_node; -extern const u32 device_input_next_node_advance[]; -extern const u32 device_input_next_node_flags[]; static inline u64 vnet_get_aggregate_rx_packets (void) diff --git a/src/vnet/devices/netlink.c b/src/vnet/devices/netlink.c index 9aae205c54f..3fd3e13bf77 100644 --- a/src/vnet/devices/netlink.c +++ b/src/vnet/devices/netlink.c @@ -20,8 +20,13 @@ #include <fcntl.h> #include <net/if.h> +#ifdef __linux__ #include <linux/netlink.h> #include <linux/rtnetlink.h> +#elif __FreeBSD__ +#include <netlink/netlink.h> +#include <netlink/netlink_route.h> +#endif #include <vlib/vlib.h> #include <vlib/unix/unix.h> @@ -273,7 +278,6 @@ vnet_netlink_get_link_mtu (int ifindex, u32 *mtu) *mtu = clib_net_to_host_u32 (msg_mtu); else *mtu = msg_mtu; - clib_warning ("mtu: %d", *mtu); goto done; } offset = NLA_ALIGN (attr->nla_len); @@ -409,6 +413,50 @@ vnet_netlink_add_ip6_route (void *dst, u8 dst_len, void *gw) return err; } +clib_error_t * +vnet_netlink_del_ip4_addr (int ifindex, void *addr, int pfx_len) +{ + vnet_netlink_msg_t m; + struct ifaddrmsg ifa = { 0 }; + clib_error_t *err = 0; + + ifa.ifa_family = AF_INET; + ifa.ifa_prefixlen = pfx_len; + ifa.ifa_index = ifindex; + + vnet_netlink_msg_init (&m, RTM_DELADDR, NLM_F_REQUEST, &ifa, + sizeof (struct ifaddrmsg)); + + vnet_netlink_msg_add_rtattr (&m, IFA_LOCAL, addr, 4); + vnet_netlink_msg_add_rtattr (&m, IFA_ADDRESS, addr, 4); + err = vnet_netlink_msg_send (&m, NULL); + if (err) + err = clib_error_return (0, "del ip4 addr %U", format_clib_error, err); + return err; +} + +clib_error_t * +vnet_netlink_del_ip6_addr (int ifindex, void *addr, int pfx_len) +{ + vnet_netlink_msg_t m; + struct ifaddrmsg ifa = { 0 }; + clib_error_t *err = 0; + + ifa.ifa_family = AF_INET6; + ifa.ifa_prefixlen = pfx_len; + ifa.ifa_index = ifindex; + + vnet_netlink_msg_init (&m, RTM_DELADDR, NLM_F_REQUEST, &ifa, + sizeof (struct ifaddrmsg)); + + vnet_netlink_msg_add_rtattr (&m, IFA_LOCAL, addr, 16); + vnet_netlink_msg_add_rtattr (&m, IFA_ADDRESS, addr, 16); + err = vnet_netlink_msg_send (&m, NULL); + if (err) + err = clib_error_return (0, "del ip6 addr %U", format_clib_error, err); + return err; +} + /* * fd.io coding-style-patch-verification: ON * diff --git a/src/vnet/devices/netlink.h b/src/vnet/devices/netlink.h index f1c42609cbf..086781fdbff 100644 --- a/src/vnet/devices/netlink.h +++ b/src/vnet/devices/netlink.h @@ -26,8 +26,10 @@ clib_error_t *vnet_netlink_get_link_mtu (int ifindex, u32 *mtu); clib_error_t *vnet_netlink_set_link_mtu (int ifindex, int mtu); clib_error_t *vnet_netlink_add_ip4_addr (int ifindex, void *addr, int pfx_len); +clib_error_t *vnet_netlink_del_ip4_addr (int ifindex, void *addr, int pfx_len); clib_error_t *vnet_netlink_add_ip6_addr (int ifindex, void *addr, int pfx_len); +clib_error_t *vnet_netlink_del_ip6_addr (int ifindex, void *addr, int pfx_len); clib_error_t *vnet_netlink_add_ip4_route (void *dst, u8 dst_len, void *gw); clib_error_t *vnet_netlink_add_ip6_route (void *dst, u8 dst_len, void *gw); diff --git a/src/vnet/devices/pipe/pipe.c b/src/vnet/devices/pipe/pipe.c index eb92b3c788a..9caee2a55cb 100644 --- a/src/vnet/devices/pipe/pipe.c +++ b/src/vnet/devices/pipe/pipe.c @@ -83,13 +83,11 @@ pipe_build_rewrite (vnet_main_t * vnm, return (rewrite); } -/* *INDENT-OFF* */ VNET_HW_INTERFACE_CLASS (pipe_hw_interface_class) = { .name = "Pipe", .build_rewrite = pipe_build_rewrite, .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, }; -/* *INDENT-ON* */ pipe_t * pipe_get (u32 sw_if_index) @@ -131,7 +129,7 @@ pipe_tx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { u32 n_left_from, n_left_to_next, n_copy, *from, *to_next; u32 next_index = VNET_PIPE_TX_NEXT_ETHERNET_INPUT; - u32 i, sw_if_index = 0, n_pkts = 0, n_bytes = 0; + u32 i, sw_if_index = 0; vlib_buffer_t *b; pipe_t *pipe; @@ -159,8 +157,6 @@ pipe_tx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0; i++; - n_pkts++; - n_bytes += vlib_buffer_length_in_chain (vm, b); } from += n_copy; @@ -187,25 +183,21 @@ pipe_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) VNET_HW_INTERFACE_FLAG_LINK_UP : 0); vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags); - /* *INDENT-OFF* */ hi = vnet_get_hw_interface (vnm, hw_if_index); hash_foreach (id, sw_if_index, hi->sub_interface_sw_if_index_by_id, ({ vnet_sw_interface_set_flags (vnm, sw_if_index, flags); })); - /* *INDENT-ON* */ return (NULL); } -/* *INDENT-OFF* */ VNET_DEVICE_CLASS (pipe_device_class) = { .name = "Pipe", .format_device_name = format_pipe_name, .tx_function = pipe_tx, .admin_up_down_function = pipe_admin_up_down, }; -/* *INDENT-ON* */ #define foreach_pipe_rx_next \ _ (DROP, "error-drop") @@ -434,7 +426,6 @@ pipe_rx (vlib_main_t * vm, return from_frame->n_vectors; } -/* *INDENT-OFF* */ VLIB_REGISTER_NODE (pipe_rx_node) = { .function = pipe_rx, .name = "pipe-rx", @@ -444,7 +435,6 @@ VLIB_REGISTER_NODE (pipe_rx_node) = { .sibling_of = "ethernet-input", }; -/* *INDENT-ON* */ /* * Maintain a bitmap of allocated pipe instance numbers. @@ -534,6 +524,7 @@ vnet_create_pipe_interface (u8 is_specified, { vnet_main_t *vnm = vnet_get_main (); vlib_main_t *vm = vlib_get_main (); + vnet_eth_interface_registration_t eir = {}; u8 address[6] = { [0] = 0x22, [1] = 0x22, @@ -564,15 +555,10 @@ vnet_create_pipe_interface (u8 is_specified, */ address[5] = instance; - error = ethernet_register_interface (vnm, pipe_device_class.index, - instance, address, &hw_if_index, - /* flag change */ 0); - - if (error) - { - rv = VNET_API_ERROR_INVALID_REGISTRATION; - goto oops; - } + eir.dev_class_index = pipe_device_class.index; + eir.dev_instance = instance; + eir.address = address; + hw_if_index = vnet_eth_register_interface (vnm, &eir); hi = vnet_get_hw_interface (vnm, hw_if_index); *parent_sw_if_index = hi->sw_if_index; @@ -631,13 +617,11 @@ pipe_hw_walk (vnet_main_t * vnm, u32 hw_if_index, void *args) { u32 pipe_sw_if_index[2], id, sw_if_index; - /* *INDENT-OFF* */ hash_foreach (id, sw_if_index, hi->sub_interface_sw_if_index_by_id, ({ ASSERT(id < 2); pipe_sw_if_index[id] = sw_if_index; })); - /* *INDENT-ON* */ ctx->cb (hi->sw_if_index, pipe_sw_if_index, hi->dev_instance, ctx->ctx); } @@ -696,13 +680,11 @@ create_pipe_interfaces (vlib_main_t * vm, * Example of how to create a pipe interface: * @cliexcmd{pipe create} ?*/ -/* *INDENT-OFF* */ VLIB_CLI_COMMAND (pipe_create_interface_command, static) = { .path = "pipe create", .short_help = "pipe create [instance <instance>]", .function = create_pipe_interfaces, }; -/* *INDENT-ON* */ int vnet_delete_pipe_interface (u32 sw_if_index) @@ -726,13 +708,11 @@ vnet_delete_pipe_interface (u32 sw_if_index) return VNET_API_ERROR_INVALID_SW_IF_INDEX; } - /* *INDENT-OFF* */ hash_foreach (id, sw_if_index, hi->sub_interface_sw_if_index_by_id, ({ vnet_delete_sub_interface(sw_if_index); pipe_main.pipes[sw_if_index] = PIPE_INVALID; })); - /* *INDENT-ON* */ ethernet_delete_interface (vnm, hw_if_index); @@ -776,13 +756,11 @@ delete_pipe_interfaces (vlib_main_t * vm, * Example of how to delete a pipe interface: * @cliexcmd{pipe delete-interface intfc loop0} ?*/ -/* *INDENT-OFF* */ VLIB_CLI_COMMAND (pipe_delete_interface_command, static) = { .path = "pipe delete", .short_help = "pipe delete <interface>", .function = delete_pipe_interfaces, }; -/* *INDENT-ON* */ /* * fd.io coding-style-patch-verification: ON diff --git a/src/vnet/devices/pipe/pipe_api.c b/src/vnet/devices/pipe/pipe_api.c index 1f0faef7c1e..79a4377de83 100644 --- a/src/vnet/devices/pipe/pipe_api.c +++ b/src/vnet/devices/pipe/pipe_api.c @@ -42,14 +42,12 @@ vl_api_pipe_create_t_handler (vl_api_pipe_create_t * mp) rv = vnet_create_pipe_interface (is_specified, user_instance, &parent_sw_if_index, pipe_sw_if_index); - /* *INDENT-OFF* */ REPLY_MACRO2(VL_API_PIPE_CREATE_REPLY, ({ rmp->sw_if_index = ntohl (parent_sw_if_index); rmp->pipe_sw_if_index[0] = ntohl (pipe_sw_if_index[0]); rmp->pipe_sw_if_index[1] = ntohl (pipe_sw_if_index[1]); })); - /* *INDENT-ON* */ } static void diff --git a/src/vnet/devices/tap/FEATURE.yaml b/src/vnet/devices/tap/FEATURE.yaml index 35ee4885b02..1a774fb0e74 100644 --- a/src/vnet/devices/tap/FEATURE.yaml +++ b/src/vnet/devices/tap/FEATURE.yaml @@ -1,6 +1,6 @@ --- name: Tap Device -maintainer: damarion@cisco.com sluong@cisco.com sykazmi@cisco.com +maintainer: damarion@cisco.com sluong@cisco.com mohsin.kazmi14@gmail.com features: - Virtio - Persistence diff --git a/src/vnet/devices/tap/cli.c b/src/vnet/devices/tap/cli.c index 10f4bb0ee2e..5c676d32d60 100644 --- a/src/vnet/devices/tap/cli.c +++ b/src/vnet/devices/tap/cli.c @@ -41,6 +41,7 @@ tap_create_command_fn (vlib_main_t * vm, unformat_input_t * input, args.tap_flags = 0; args.rv = -1; args.num_rx_queues = 1; + args.num_tx_queues = 1; /* Get a line of input. */ if (unformat_user (input, unformat_line_input, line_input)) @@ -76,6 +77,8 @@ tap_create_command_fn (vlib_main_t * vm, unformat_input_t * input, args.host_ip6_gw_set = 1; else if (unformat (line_input, "num-rx-queues %d", &tmp)) args.num_rx_queues = tmp; + else if (unformat (line_input, "num-tx-queues %d", &tmp)) + args.num_tx_queues = tmp; else if (unformat (line_input, "rx-ring-size %d", &tmp)) args.rx_ring_sz = tmp; else if (unformat (line_input, "tx-ring-size %d", &tmp)) @@ -133,12 +136,12 @@ tap_create_command_fn (vlib_main_t * vm, unformat_input_t * input, } -/* *INDENT-OFF* */ VLIB_CLI_COMMAND (tap_create_command, static) = { .path = "create tap", - .short_help = "create tap {id <if-id>} [hw-addr <mac-address>] " - "[num-rx-queues <n>] [rx-ring-size <size>] [tx-ring-size <size>] " - "[host-ns <netns>] [host-bridge <bridge-name>] " + .short_help = + "create tap {id <if-id>} [hw-addr <mac-address>] " + "[num-rx-queues <n>] [num-tx-queues <n>] [rx-ring-size <size>] " + "[tx-ring-size <size>] [host-ns <netns>] [host-bridge <bridge-name>] " "[host-ip4-addr <ip4addr/mask>] [host-ip6-addr <ip6-addr>] " "[host-ip4-gw <ip4-addr>] [host-ip6-gw <ip6-addr>] " "[host-mac-addr <host-mac-address>] [host-if-name <name>] " @@ -146,7 +149,6 @@ VLIB_CLI_COMMAND (tap_create_command, static) = { "[persist] [attach] [tun] [packed] [in-order]", .function = tap_create_command_fn, }; -/* *INDENT-ON* */ static clib_error_t * tap_delete_command_fn (vlib_main_t * vm, unformat_input_t * input, @@ -187,14 +189,12 @@ tap_delete_command_fn (vlib_main_t * vm, unformat_input_t * input, return 0; } -/* *INDENT-OFF* */ VLIB_CLI_COMMAND (tap_delete__command, static) = { .path = "delete tap", .short_help = "delete tap {<interface> | sw_if_index <sw_idx>}", .function = tap_delete_command_fn, }; -/* *INDENT-ON* */ static clib_error_t * tap_offload_command_fn (vlib_main_t * vm, unformat_input_t * input, @@ -257,7 +257,6 @@ tap_offload_command_fn (vlib_main_t * vm, unformat_input_t * input, return 0; } -/* *INDENT-OFF* */ VLIB_CLI_COMMAND (tap_offload_command, static) = { .path = "set tap offload", @@ -266,7 +265,6 @@ VLIB_CLI_COMMAND (tap_offload_command, static) = "csum-offload-disable>", .function = tap_offload_command_fn, }; -/* *INDENT-ON* */ static clib_error_t * tap_show_command_fn (vlib_main_t * vm, unformat_input_t * input, @@ -296,10 +294,8 @@ tap_show_command_fn (vlib_main_t * vm, unformat_input_t * input, if (vec_len (hw_if_indices) == 0) { - /* *INDENT-OFF* */ pool_foreach (vif, mm->interfaces) vec_add1 (hw_if_indices, vif->hw_if_index); - /* *INDENT-ON* */ } virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_TAP); @@ -309,13 +305,11 @@ done: return error; } -/* *INDENT-OFF* */ VLIB_CLI_COMMAND (tap_show_command, static) = { .path = "show tap", .short_help = "show tap {<interface>] [descriptors]", .function = tap_show_command_fn, }; -/* *INDENT-ON* */ static clib_error_t * tun_show_command_fn (vlib_main_t * vm, unformat_input_t * input, @@ -345,10 +339,8 @@ tun_show_command_fn (vlib_main_t * vm, unformat_input_t * input, if (vec_len (hw_if_indices) == 0) { - /* *INDENT-OFF* */ pool_foreach (vif, mm->interfaces) vec_add1 (hw_if_indices, vif->hw_if_index); - /* *INDENT-ON* */ } virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_TUN); @@ -358,13 +350,11 @@ done: return error; } -/* *INDENT-OFF* */ VLIB_CLI_COMMAND (tun_show_command, static) = { .path = "show tun", .short_help = "show tun {<interface>] [descriptors]", .function = tun_show_command_fn, }; -/* *INDENT-ON* */ clib_error_t * tap_cli_init (vlib_main_t * vm) diff --git a/src/vnet/devices/tap/tap.c b/src/vnet/devices/tap/tap.c index 8fca35df202..1e2ee87041d 100644 --- a/src/vnet/devices/tap/tap.c +++ b/src/vnet/devices/tap/tap.c @@ -58,13 +58,11 @@ tap_main_t tap_main; goto error; \ } - /* *INDENT-OFF* */ -VNET_HW_INTERFACE_CLASS (tun_device_hw_interface_class, static) = -{ +VNET_HW_INTERFACE_CLASS (tun_device_hw_interface_class, static) = { .name = "tun-device", .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, + .tx_hash_fn_type = VNET_HASH_FN_TYPE_IP, }; - /* *INDENT-ON* */ #define TUN_MAX_PACKET_BYTES 65355 #define TUN_MIN_PACKET_BYTES 64 @@ -79,6 +77,14 @@ virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, return 0; } +static clib_error_t * +virtio_eth_set_max_frame_size (vnet_main_t *vnm, vnet_hw_interface_t *hi, + u32 frame_size) +{ + /* nothing for now */ + return 0; +} + #define TAP_MAX_INSTANCE 1024 static void @@ -89,14 +95,14 @@ tap_free (vlib_main_t * vm, virtio_if_t * vif) clib_error_t *err = 0; int i; - /* *INDENT-OFF* */ + virtio_pre_input_node_disable (vm, vif); + vec_foreach_index (i, vif->vhost_fds) if (vif->vhost_fds[i] != -1) close (vif->vhost_fds[i]); vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif, RX_QUEUE (i)); vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif, TX_QUEUE (i)); - /* *INDENT-ON* */ if (vif->tap_fds) { @@ -106,6 +112,7 @@ tap_free (vlib_main_t * vm, virtio_if_t * vif) error: vec_foreach_index (i, vif->tap_fds) close (vif->tap_fds[i]); + vec_free (vif->tap_fds); vec_free (vif->vhost_fds); vec_free (vif->rxq_vrings); vec_free (vif->txq_vrings); @@ -129,6 +136,7 @@ tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) tap_main_t *tm = &tap_main; vnet_sw_interface_t *sw; vnet_hw_interface_t *hw; + vnet_hw_if_caps_change_t cc; int i, num_vhost_queues; int old_netns_fd = -1; struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR }; @@ -190,7 +198,7 @@ tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) vif->dev_instance = vif - vim->interfaces; vif->id = args->id; - vif->num_txqs = thm->n_vlib_mains; + vif->num_txqs = clib_max (args->num_tx_queues, thm->n_vlib_mains); vif->num_rxqs = clib_max (args->num_rx_queues, 1); if (args->tap_flags & TAP_FLAG_ATTACH) @@ -201,23 +209,26 @@ tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) err = clib_error_return (0, "host_if_name is not provided"); goto error; } - if (args->host_namespace) + } + + /* if namespace is specified, all further netlink messages should be executed + * after we change our net namespace */ + if (args->host_namespace) + { + old_netns_fd = clib_netns_open (NULL /* self */); + if ((nfd = clib_netns_open (args->host_namespace)) == -1) { - old_netns_fd = clib_netns_open (NULL /* self */); - if ((nfd = clib_netns_open (args->host_namespace)) == -1) - { - args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; - args->error = clib_error_return_unix (0, "clib_netns_open '%s'", - args->host_namespace); - goto error; - } - if (clib_setns (nfd) == -1) - { - args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; - args->error = clib_error_return_unix (0, "setns '%s'", - args->host_namespace); - goto error; - } + args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; + args->error = clib_error_return_unix (0, "clib_netns_open '%s'", + args->host_namespace); + goto error; + } + if (clib_setns (nfd) == -1) + { + args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; + args->error = + clib_error_return_unix (0, "setns '%s'", args->host_namespace); + goto error; } } @@ -259,7 +270,7 @@ tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) else ifr.ifr_flags |= IFF_MULTI_QUEUE; - hdrsz = sizeof (virtio_net_hdr_v1_t); + hdrsz = sizeof (vnet_virtio_net_hdr_v1_t); if (args->tap_flags & TAP_FLAG_GSO) { offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6; @@ -321,10 +332,10 @@ tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) args->error = clib_error_return_unix (0, "open '/dev/net/tun'"); goto error; } + vec_add1 (vif->tap_fds, qfd); _IOCTL (qfd, TUNSETIFF, (void *) &ifr); tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", qfd, ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags); - vec_add1 (vif->tap_fds, qfd); } for (i = 0; i < vif->num_rxqs; i++) @@ -400,44 +411,6 @@ tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) virtio_set_net_hdr_size (vif); - if (!(args->tap_flags & TAP_FLAG_ATTACH)) - { - /* if namespace is specified, all further netlink messages should be executed - after we change our net namespace */ - if (args->host_namespace) - { - old_netns_fd = clib_netns_open (NULL /* self */); - if ((nfd = clib_netns_open (args->host_namespace)) == -1) - { - args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; - args->error = clib_error_return_unix (0, "clib_netns_open '%s'", - args->host_namespace); - goto error; - } - args->error = vnet_netlink_set_link_netns (vif->ifindex, nfd, - host_if_name); - if (args->error) - { - args->rv = VNET_API_ERROR_NETLINK_ERROR; - goto error; - } - if (clib_setns (nfd) == -1) - { - args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; - args->error = clib_error_return_unix (0, "setns '%s'", - args->host_namespace); - goto error; - } - if ((vif->ifindex = if_nametoindex (host_if_name)) == 0) - { - args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; - args->error = clib_error_return_unix (0, "if_nametoindex '%s'", - host_if_name); - goto error; - } - } - } - if (vif->type == VIRTIO_IF_TYPE_TAP) { if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes)) @@ -602,7 +575,7 @@ tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) vhost_vring_addr_t addr = { 0 }; vhost_vring_state_t state = { 0 }; vhost_vring_file_t file = { 0 }; - virtio_vring_t *vring; + vnet_virtio_vring_t *vring; u16 qp = i >> 1; int fd = vif->vhost_fds[qp]; @@ -620,7 +593,7 @@ tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) } addr.index = state.index = file.index = vring->queue_id & 1; - state.num = vring->size; + state.num = vring->queue_size; virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd, state.index, state.num); _IOCTL (fd, VHOST_SET_VRING_NUM, &state); @@ -659,10 +632,12 @@ tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) ethernet_mac_address_generate (args->mac_addr.bytes); clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6); - vif->host_bridge = format (0, "%s%c", args->host_bridge, 0); + if (args->host_bridge) + vif->host_bridge = format (0, "%s%c", args->host_bridge, 0); } vif->host_if_name = format (0, "%s%c", host_if_name, 0); - vif->net_ns = format (0, "%s%c", args->host_namespace, 0); + if (args->host_namespace) + vif->net_ns = format (0, "%s%c", args->host_namespace, 0); vif->host_mtu_size = args->host_mtu_size; vif->tap_flags = args->tap_flags; clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6); @@ -675,17 +650,14 @@ tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) if (vif->type != VIRTIO_IF_TYPE_TUN) { - args->error = - ethernet_register_interface (vnm, virtio_device_class.index, - vif->dev_instance, vif->mac_addr, - &vif->hw_if_index, - virtio_eth_flag_change); - if (args->error) - { - args->rv = VNET_API_ERROR_INVALID_REGISTRATION; - goto error; - } - + vnet_eth_interface_registration_t eir = {}; + + eir.dev_class_index = virtio_device_class.index; + eir.dev_instance = vif->dev_instance; + eir.address = vif->mac_addr; + eir.cb.flag_change = virtio_eth_flag_change; + eir.cb.set_max_frame_size = virtio_eth_set_max_frame_size; + vif->hw_if_index = vnet_eth_register_interface (vnm, &eir); } else { @@ -701,18 +673,16 @@ tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) args->sw_if_index = vif->sw_if_index; args->rv = 0; hw = vnet_get_hw_interface (vnm, vif->hw_if_index); - hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE; + cc.mask = VNET_HW_IF_CAP_INT_MODE | VNET_HW_IF_CAP_TCP_GSO | + VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM; + cc.val = VNET_HW_IF_CAP_INT_MODE; + if (args->tap_flags & TAP_FLAG_GSO) - { - hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO | - VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM | - VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM; - } + cc.val |= VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_TX_TCP_CKSUM | + VNET_HW_IF_CAP_TX_UDP_CKSUM; else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD) - { - hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM | - VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM; - } + cc.val |= VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM; + if ((args->tap_flags & TAP_FLAG_GSO) && (args->tap_flags & TAP_FLAG_GRO_COALESCE)) { @@ -720,18 +690,18 @@ tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) } if (vif->type == VIRTIO_IF_TYPE_TUN) { - hw->max_supported_packet_bytes = TUN_MAX_PACKET_BYTES; - hw->min_packet_bytes = hw->min_supported_packet_bytes = - TUN_MIN_PACKET_BYTES; - hw->max_packet_bytes = - args->host_mtu_size ? args->host_mtu_size : TUN_DEFAULT_PACKET_BYTES; - vnet_sw_interface_set_mtu (vnm, hw->sw_if_index, hw->max_packet_bytes); + hw->min_frame_size = TUN_MIN_PACKET_BYTES; + vnet_hw_interface_set_mtu ( + vnm, hw->hw_if_index, + args->host_mtu_size ? args->host_mtu_size : TUN_DEFAULT_PACKET_BYTES); } + vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc); + virtio_pre_input_node_enable (vm, vif); virtio_vring_set_rx_queues (vm, vif); + virtio_vring_set_tx_queues (vm, vif); vif->per_interface_next_index = ~0; - vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; vnet_hw_interface_set_flags (vnm, vif->hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP); /* @@ -740,7 +710,6 @@ tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) * TUNSETCARRIER ioctl(). See tap_set_carrier(). */ vif->host_carrier_up = 1; - vif->cxq_vring = NULL; goto done; @@ -758,7 +727,11 @@ done: if (vhost_mem) clib_mem_free (vhost_mem); if (old_netns_fd != -1) - close (old_netns_fd); + { + /* in case we errored with a switched netns */ + clib_setns (old_netns_fd); + close (old_netns_fd); + } if (nfd != -1) close (nfd); } @@ -803,6 +776,7 @@ tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index, virtio_main_t *mm = &virtio_main; virtio_if_t *vif; vnet_hw_interface_t *hw; + vnet_hw_if_caps_change_t cc; clib_error_t *err = 0; int i = 0; @@ -820,21 +794,19 @@ tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index, _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload); vif->gso_enabled = 0; vif->packet_coalesce = 0; - vif->csum_offload_enabled = enable_disable ? 1 : 0; - - if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) != 0) - { - hw->caps &= ~VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO; - } + cc.mask = VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_L4_TX_CKSUM; if (enable_disable) { - hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM; + cc.val = VNET_HW_IF_CAP_L4_TX_CKSUM; + vif->csum_offload_enabled = 1; } else { - hw->caps &= ~VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM; + cc.val = 0; + vif->csum_offload_enabled = 0; } + vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc); error: if (err) @@ -854,6 +826,7 @@ tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable, virtio_main_t *mm = &virtio_main; virtio_if_t *vif; vnet_hw_interface_t *hw; + vnet_hw_if_caps_change_t cc; clib_error_t *err = 0; int i = 0; @@ -869,29 +842,25 @@ tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable, unsigned int offload = enable_disable ? gso_on : gso_off; vec_foreach_index (i, vif->tap_fds) _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload); - vif->gso_enabled = enable_disable ? 1 : 0; - vif->csum_offload_enabled = 0; + + cc.mask = VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_L4_TX_CKSUM; + if (enable_disable) { - if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) == 0) - { - hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO | - VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM; - } + cc.val = cc.mask; + vif->gso_enabled = 1; + vif->csum_offload_enabled = 1; if (is_packet_coalesce) - { - virtio_set_packet_coalesce (vif); - } + virtio_set_packet_coalesce (vif); } else { - if ((hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) != 0) - { - hw->caps &= ~(VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO | - VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM); - } + cc.val = 0; + vif->gso_enabled = 0; + vif->csum_offload_enabled = 0; vif->packet_coalesce = 0; } + vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc); error: if (err) @@ -909,12 +878,11 @@ tap_dump_ifs (tap_interface_details_t ** out_tapids) vnet_main_t *vnm = vnet_get_main (); virtio_main_t *mm = &virtio_main; virtio_if_t *vif; - virtio_vring_t *vring; + vnet_virtio_vring_t *vring; vnet_hw_interface_t *hi; tap_interface_details_t *r_tapids = NULL; tap_interface_details_t *tapid = NULL; - /* *INDENT-OFF* */ pool_foreach (vif, mm->interfaces) { if ((vif->type != VIRTIO_IF_TYPE_TAP) && (vif->type != VIRTIO_IF_TYPE_TUN)) @@ -927,9 +895,9 @@ tap_dump_ifs (tap_interface_details_t ** out_tapids) clib_memcpy(tapid->dev_name, hi->name, MIN (ARRAY_LEN (tapid->dev_name) - 1, vec_len (hi->name))); vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0)); - tapid->rx_ring_sz = vring->size; + tapid->rx_ring_sz = vring->queue_size; vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0)); - tapid->tx_ring_sz = vring->size; + tapid->tx_ring_sz = vring->queue_size; tapid->tap_flags = vif->tap_flags; clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6); if (vif->host_if_name) @@ -958,7 +926,6 @@ tap_dump_ifs (tap_interface_details_t ** out_tapids) tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len; tapid->host_mtu_size = vif->host_mtu_size; } - /* *INDENT-ON* */ *out_tapids = r_tapids; diff --git a/src/vnet/devices/tap/tap.h b/src/vnet/devices/tap/tap.h index 2efaa511a49..6b88c34fe41 100644 --- a/src/vnet/devices/tap/tap.h +++ b/src/vnet/devices/tap/tap.h @@ -44,7 +44,8 @@ typedef struct u32 id; u8 mac_addr_set; mac_address_t mac_addr; - u8 num_rx_queues; + u16 num_rx_queues; + u16 num_tx_queues; u16 rx_ring_sz; u16 tx_ring_sz; u32 tap_flags; @@ -98,8 +99,10 @@ typedef struct /* host mtu size, configurable through startup.conf */ int host_mtu_size; + u16 msg_id_base; } tap_main_t; +extern tap_main_t tap_main; void tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args); int tap_delete_if (vlib_main_t * vm, u32 sw_if_index); int tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, diff --git a/src/vnet/devices/tap/tapv2.api b/src/vnet/devices/tap/tapv2.api index 6b6618411a6..bf53d1bc6fe 100644 --- a/src/vnet/devices/tap/tapv2.api +++ b/src/vnet/devices/tap/tapv2.api @@ -43,6 +43,82 @@ enum tap_flags { @param use_random_mac - let the system generate a unique mac address @param mac_address - mac addr to assign to the interface if use_random not set @param num_rx_queues - number of rx queues + @param num_tx_queues - number of tx queues + @param tx_ring_sz - the number of entries of TX ring, optional, default is 256 entries, must be power of 2 + @param rx_ring_sz - the number of entries of RX ring, optional, default is 256 entries, must be power of 2 + @param host_mtu_set - host MTU should be set + @param host_mtu_size - host MTU size + @param host_mac_addr_set - host side interface mac address should be set + @param host_mac_addr - host side interface mac address + @param host_ip4_prefix_set - host IPv4 ip address should be set + @param host_ip4_prefix - host IPv4 ip address + @param host_ip6_prefix_set - host IPv6 ip address should be set + @param host_ip6_prefix - host IPv6 ip address + @param host_ip4_gw_set - host IPv4 default gateway should be set + @param host_ip4_gw - host IPv4 default gateway + @param host_ip6_gw_set - host IPv6 default gateway should be set + @param host_ip6_gw - host IPv6 default gateway + @param tap_flags - flags for the TAP interface creation + @param host_if_name_set - host side interface name should be set + @param host_if_name - host side interface name + @param host_namespace_set - host namespace should be set + @param host_namespace - host namespace to attach interface to + @param host_bridge_set - host bridge should be set + @param host_bridge - host bridge to attach interface to + @param tag - tag +*/ +autoendian define tap_create_v3 +{ + u32 client_index; + u32 context; + u32 id [default=0xffffffff]; + bool use_random_mac [default=true]; + vl_api_mac_address_t mac_address; + u16 num_rx_queues [default=1]; + u16 num_tx_queues [default=1]; + u16 tx_ring_sz [default=256]; + u16 rx_ring_sz [default=256]; + bool host_mtu_set; + u32 host_mtu_size; + bool host_mac_addr_set; + vl_api_mac_address_t host_mac_addr; + bool host_ip4_prefix_set; + vl_api_ip4_address_with_prefix_t host_ip4_prefix; + bool host_ip6_prefix_set; + vl_api_ip6_address_with_prefix_t host_ip6_prefix; + bool host_ip4_gw_set; + vl_api_ip4_address_t host_ip4_gw; + bool host_ip6_gw_set; + vl_api_ip6_address_t host_ip6_gw; + vl_api_tap_flags_t tap_flags; + bool host_namespace_set; + string host_namespace[64]; + bool host_if_name_set; + string host_if_name[64]; + bool host_bridge_set; + string host_bridge[64]; + string tag[]; +}; + +/** \brief Reply for tap create reply + @param context - returned sender context, to match reply w/ request + @param retval - return code + @param sw_if_index - software index allocated for the new tap interface +*/ +autoendian define tap_create_v3_reply +{ + u32 context; + i32 retval; + vl_api_interface_index_t sw_if_index; +}; + +/** \brief Initialize a new tap interface with the given parameters + @param client_index - opaque cookie to identify the sender + @param context - sender context, to match reply w/ request + @param id - interface id, 0xffffffff means auto + @param use_random_mac - let the system generate a unique mac address + @param mac_address - mac addr to assign to the interface if use_random not set + @param num_rx_queues - number of rx queues @param tx_ring_sz - the number of entries of TX ring, optional, default is 256 entries, must be power of 2 @param rx_ring_sz - the number of entries of RX ring, optional, default is 256 entries, must be power of 2 @param host_mtu_set - host MTU should be set @@ -68,6 +144,8 @@ enum tap_flags { */ define tap_create_v2 { + option deprecated; + u32 client_index; u32 context; u32 id [default=0xffffffff]; @@ -105,6 +183,8 @@ define tap_create_v2 */ define tap_create_v2_reply { + option deprecated; + u32 context; i32 retval; vl_api_interface_index_t sw_if_index; diff --git a/src/vnet/devices/tap/tapv2_api.c b/src/vnet/devices/tap/tapv2_api.c index 08dca0dc92b..ab4189ab607 100644 --- a/src/vnet/devices/tap/tapv2_api.c +++ b/src/vnet/devices/tap/tapv2_api.c @@ -32,10 +32,102 @@ #include <vnet/devices/tap/tapv2.api_enum.h> #include <vnet/devices/tap/tapv2.api_types.h> -#define REPLY_MSG_ID_BASE msg_id_base +#define REPLY_MSG_ID_BASE tap_main.msg_id_base #include <vlibapi/api_helper_macros.h> -static u16 msg_id_base; +static void +vl_api_tap_create_v3_t_handler (vl_api_tap_create_v3_t *mp) +{ + vl_api_registration_t *reg; + int rv; + + reg = vl_api_client_index_to_registration (mp->client_index); + if (!reg) + return; + + vnet_main_t *vnm = vnet_get_main (); + vlib_main_t *vm = vlib_get_main (); + vl_api_tap_create_v3_reply_t *rmp; + + tap_create_if_args_t _a, *ap = &_a; + + clib_memset (ap, 0, sizeof (*ap)); + + ap->id = mp->id; + if (!mp->use_random_mac) + { + mac_address_decode (mp->mac_address, &ap->mac_addr); + ap->mac_addr_set = 1; + } + ap->rx_ring_sz = mp->rx_ring_sz; + ap->tx_ring_sz = mp->tx_ring_sz; + ap->sw_if_index = (u32) ~0; + ap->num_rx_queues = clib_max (1, mp->num_rx_queues); + ap->num_tx_queues = mp->num_tx_queues; + + if (mp->host_if_name_set) + ap->host_if_name = format (0, "%s%c", mp->host_if_name, 0); + + if (mp->host_mac_addr_set) + { + mac_address_decode (mp->host_mac_addr, &ap->host_mac_addr); + } + + if (mp->host_namespace_set) + ap->host_namespace = format (0, "%s%c", mp->host_namespace, 0); + + if (mp->host_bridge_set) + ap->host_bridge = format (0, "%s%c", mp->host_bridge, 0); + + if (mp->host_ip4_prefix_set) + { + ip4_address_decode (mp->host_ip4_prefix.address, &ap->host_ip4_addr); + ap->host_ip4_prefix_len = mp->host_ip4_prefix.len; + } + + if (mp->host_ip6_prefix_set) + { + ip6_address_decode (mp->host_ip6_prefix.address, &ap->host_ip6_addr); + ap->host_ip6_prefix_len = mp->host_ip6_prefix.len; + } + + if (mp->host_ip4_gw_set) + { + ip4_address_decode (mp->host_ip4_gw, &ap->host_ip4_gw); + ap->host_ip4_gw_set = 1; + } + + if (mp->host_ip6_gw_set) + { + ip6_address_decode (mp->host_ip6_gw, &ap->host_ip6_gw); + ap->host_ip6_gw_set = 1; + } + + if (mp->host_mtu_set) + { + ap->host_mtu_size = mp->host_mtu_size; + ap->host_mtu_set = 1; + } + + ap->tap_flags = mp->tap_flags; + + tap_create_if (vm, ap); + + /* If a tag was supplied... */ + if (vl_api_string_len (&mp->tag)) + { + u8 *tag = vl_api_from_api_to_new_vec (mp, &mp->tag); + vnet_set_sw_interface_tag (vnm, tag, ap->sw_if_index); + } + + vec_free (ap->host_if_name); + vec_free (ap->host_namespace); + vec_free (ap->host_bridge); + + rv = ap->rv; + REPLY_MACRO2_END (VL_API_TAP_CREATE_V3_REPLY, + ({ rmp->sw_if_index = ap->sw_if_index; })); +} static void vl_api_tap_create_v2_t_handler (vl_api_tap_create_v2_t * mp) @@ -63,6 +155,7 @@ vl_api_tap_create_v2_t_handler (vl_api_tap_create_v2_t * mp) ap->tx_ring_sz = ntohs (mp->tx_ring_sz); ap->sw_if_index = (u32) ~ 0; ap->num_rx_queues = 1; + ap->num_tx_queues = 1; if (mp->num_rx_queues > 1) ap->num_rx_queues = mp->num_rx_queues; diff --git a/src/vnet/devices/virtio/FEATURE.yaml b/src/vnet/devices/virtio/FEATURE.yaml index 7b2fb59e1ad..446a45b61a3 100644 --- a/src/vnet/devices/virtio/FEATURE.yaml +++ b/src/vnet/devices/virtio/FEATURE.yaml @@ -1,6 +1,6 @@ --- name: Virtio PCI Device -maintainer: sykazmi@cisco.com sluong@cisco.com +maintainer: mohsin.kazmi14@gmail.com sluong@cisco.com features: - Driver mode to emulate PCI interface presented to VPP from the host interface. @@ -11,6 +11,8 @@ features: - Support multi-queue, GSO, checksum offload, indirect descriptor, jumbo frame, and packed ring. - Support virtio 1.1 packed ring in vhost + - Support for tx queue size configuration (tested on host kernel 5.15 + and qemu version 6.2.0) description: "Virtio implementation" missing: - API dump filtering by sw_if_index diff --git a/src/vnet/devices/virtio/cli.c b/src/vnet/devices/virtio/cli.c index a78336997e2..c1b6c8be065 100644 --- a/src/vnet/devices/virtio/cli.c +++ b/src/vnet/devices/virtio/cli.c @@ -31,6 +31,7 @@ virtio_pci_create_command_fn (vlib_main_t * vm, unformat_input_t * input, virtio_pci_create_if_args_t args; u64 feature_mask = (u64) ~ (0ULL); u32 buffering_size = 0; + u32 txq_size = 0; /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) @@ -43,6 +44,8 @@ virtio_pci_create_command_fn (vlib_main_t * vm, unformat_input_t * input, ; else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask)) args.features = feature_mask; + else if (unformat (line_input, "tx-queue-size %u", &txq_size)) + args.tx_queue_size = txq_size; else if (unformat (line_input, "gso-enabled")) args.gso_enabled = 1; else if (unformat (line_input, "csum-enabled")) @@ -55,6 +58,10 @@ virtio_pci_create_command_fn (vlib_main_t * vm, unformat_input_t * input, } else if (unformat (line_input, "packed")) args.virtio_flags |= VIRTIO_FLAG_PACKED; + else if (unformat (line_input, "bind force")) + args.bind = VIRTIO_BIND_FORCE; + else if (unformat (line_input, "bind")) + args.bind = VIRTIO_BIND_DEFAULT; else return clib_error_return (0, "unknown input `%U'", format_unformat_error, input); @@ -66,15 +73,14 @@ virtio_pci_create_command_fn (vlib_main_t * vm, unformat_input_t * input, return args.error; } -/* *INDENT-OFF* */ VLIB_CLI_COMMAND (virtio_pci_create_command, static) = { .path = "create interface virtio", .short_help = "create interface virtio <pci-address> " - "[feature-mask <hex-mask>] [gso-enabled] [csum-enabled] " - "[buffering [size <buffering-szie>]] [packed]", + "[feature-mask <hex-mask>] [tx-queue-size <size>] " + "[gso-enabled] [csum-enabled] " + "[buffering [size <buffering-szie>]] [packed] [bind [force]]", .function = virtio_pci_create_command_fn, }; -/* *INDENT-ON* */ static clib_error_t * virtio_pci_delete_command_fn (vlib_main_t * vm, unformat_input_t * input, @@ -120,14 +126,12 @@ virtio_pci_delete_command_fn (vlib_main_t * vm, unformat_input_t * input, return 0; } -/* *INDENT-OFF* */ VLIB_CLI_COMMAND (virtio_pci_delete_command, static) = { .path = "delete interface virtio", .short_help = "delete interface virtio " "{<interface> | sw_if_index <sw_idx>}", .function = virtio_pci_delete_command_fn, }; -/* *INDENT-ON* */ static clib_error_t * virtio_pci_enable_command_fn (vlib_main_t * vm, unformat_input_t * input, @@ -182,14 +186,12 @@ virtio_pci_enable_command_fn (vlib_main_t * vm, unformat_input_t * input, return 0; } -/* *INDENT-OFF* */ VLIB_CLI_COMMAND (virtio_pci_enable_command, static) = { .path = "set virtio pci", .short_help = "set virtio pci {<interface> | sw_if_index <sw_idx>}" " [gso-enabled | csum-offload-enabled | offloads-disabled]", .function = virtio_pci_enable_command_fn, }; -/* *INDENT-ON* */ static clib_error_t * show_virtio_pci_fn (vlib_main_t * vm, unformat_input_t * input, @@ -248,13 +250,11 @@ done: return error; } -/* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_virtio_pci_command, static) = { .path = "show virtio pci", .short_help = "show virtio pci [<interface>] [descriptors | desc] [debug-device]", .function = show_virtio_pci_fn, }; -/* *INDENT-ON* */ clib_error_t * virtio_pci_cli_init (vlib_main_t * vm) diff --git a/src/vnet/devices/virtio/device.c b/src/vnet/devices/virtio/device.c index 6c2fe34abf0..112f77e7065 100644 --- a/src/vnet/devices/virtio/device.c +++ b/src/vnet/devices/virtio/device.c @@ -27,6 +27,7 @@ #include <vnet/gso/hdr_offset_parser.h> #include <vnet/ip/ip4_packet.h> #include <vnet/ip/ip6_packet.h> +#include <vnet/ip/ip_psh_cksum.h> #include <vnet/tcp/tcp_packet.h> #include <vnet/udp/udp_packet.h> #include <vnet/devices/virtio/virtio.h> @@ -62,13 +63,31 @@ format_virtio_device (u8 * s, va_list * args) u32 dev_instance = va_arg (*args, u32); int verbose = va_arg (*args, int); u32 indent = format_get_indent (s); + virtio_main_t *vim = &virtio_main; + virtio_if_t *vif = vec_elt_at_index (vim->interfaces, dev_instance); + vnet_virtio_vring_t *vring = 0; s = format (s, "VIRTIO interface"); if (verbose) { s = format (s, "\n%U instance %u", format_white_space, indent + 2, dev_instance); + s = format (s, "\n%U RX QUEUE : Total Packets", format_white_space, + indent + 4); + vec_foreach (vring, vif->rxq_vrings) + { + s = format (s, "\n%U %8u : %llu", format_white_space, indent + 4, + RX_QUEUE_ACCESS (vring->queue_id), vring->total_packets); + } + s = format (s, "\n%U TX QUEUE : Total Packets", format_white_space, + indent + 4); + vec_foreach (vring, vif->txq_vrings) + { + s = format (s, "\n%U %8u : %llu", format_white_space, indent + 4, + TX_QUEUE_ACCESS (vring->queue_id), vring->total_packets); + } } + return s; } @@ -108,6 +127,7 @@ virtio_tx_trace (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_buffer_t *b0, t = vlib_add_trace (vm, node, b0, sizeof (t[0])); t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX]; t->buffer_index = bi; + clib_memset (&t->gho, 0, sizeof (t->gho)); if (is_tun) { int is_ip4 = 0, is_ip6 = 0; @@ -165,11 +185,12 @@ virtio_memset_ring_u32 (u32 *ring, u32 start, u32 ring_size, u32 n_buffers) } static void -virtio_free_used_device_desc_split (vlib_main_t *vm, virtio_vring_t *vring, +virtio_free_used_device_desc_split (vlib_main_t *vm, + vnet_virtio_vring_t *vring, uword node_index) { u16 used = vring->desc_in_use; - u16 sz = vring->size; + u16 sz = vring->queue_size; u16 mask = sz - 1; u16 last = vring->last_used_idx; u16 n_left = vring->used->idx - last; @@ -180,7 +201,7 @@ virtio_free_used_device_desc_split (vlib_main_t *vm, virtio_vring_t *vring, while (n_left) { - vring_used_elem_t *e = &vring->used->ring[last & mask]; + vnet_virtio_vring_used_elem_t *e = &vring->used->ring[last & mask]; u16 slot, n_buffers; slot = n_buffers = e->id; @@ -189,7 +210,7 @@ virtio_free_used_device_desc_split (vlib_main_t *vm, virtio_vring_t *vring, n_left--; last++; n_buffers++; - vring_desc_t *d = &vring->desc[e->id]; + vnet_virtio_vring_desc_t *d = &vring->desc[e->id]; u16 next; while (d->flags & VRING_DESC_F_NEXT) { @@ -231,11 +252,12 @@ virtio_free_used_device_desc_split (vlib_main_t *vm, virtio_vring_t *vring, } static void -virtio_free_used_device_desc_packed (vlib_main_t *vm, virtio_vring_t *vring, +virtio_free_used_device_desc_packed (vlib_main_t *vm, + vnet_virtio_vring_t *vring, uword node_index) { - vring_packed_desc_t *d; - u16 sz = vring->size; + vnet_virtio_vring_packed_desc_t *d; + u16 sz = vring->queue_size; u16 last = vring->last_used_idx; u16 n_buffers = 0, start; u16 flags; @@ -272,7 +294,7 @@ virtio_free_used_device_desc_packed (vlib_main_t *vm, virtio_vring_t *vring, } static void -virtio_free_used_device_desc (vlib_main_t *vm, virtio_vring_t *vring, +virtio_free_used_device_desc (vlib_main_t *vm, vnet_virtio_vring_t *vring, uword node_index, int packed) { if (packed) @@ -283,74 +305,89 @@ virtio_free_used_device_desc (vlib_main_t *vm, virtio_vring_t *vring, } static void -set_checksum_offsets (vlib_buffer_t *b, virtio_net_hdr_v1_t *hdr, +set_checksum_offsets (vlib_buffer_t *b, vnet_virtio_net_hdr_v1_t *hdr, const int is_l2) { vnet_buffer_oflags_t oflags = vnet_buffer (b)->oflags; - + i16 l4_hdr_offset = vnet_buffer (b)->l4_hdr_offset - b->current_data; if (b->flags & VNET_BUFFER_F_IS_IP4) { ip4_header_t *ip4; - generic_header_offset_t gho = { 0 }; - vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ , - 0 /* ip6 */ ); hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; - hdr->csum_start = gho.l4_hdr_offset; // 0x22; + hdr->csum_start = l4_hdr_offset; // 0x22; + + /* + * virtio devices do not support IP4 checksum offload. So driver takes + * care of it while doing tx. + */ + ip4 = (ip4_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset); + if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM) + ip4->checksum = ip4_header_checksum (ip4); + + /* + * virtio devices assume the l4 header is set to the checksum of the + * l3 pseudo-header, so we compute it before tx-ing + */ if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM) { + tcp_header_t *tcp = + (tcp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset); + tcp->checksum = ip4_pseudo_header_cksum (ip4); hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum); } else if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM) { + udp_header_t *udp = + (udp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset); + udp->checksum = ip4_pseudo_header_cksum (ip4); hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum); } - - /* - * virtio devices do not support IP4 checksum offload. So driver takes care - * of it while doing tx. - */ - ip4 = - (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset); - if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM) - ip4->checksum = ip4_header_checksum (ip4); } else if (b->flags & VNET_BUFFER_F_IS_IP6) { - generic_header_offset_t gho = { 0 }; - vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ , - 1 /* ip6 */ ); + ip6_header_t *ip6; hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; - hdr->csum_start = gho.l4_hdr_offset; // 0x36; + hdr->csum_start = l4_hdr_offset; // 0x36; + ip6 = (ip6_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset); + + /* + * virtio devices assume the l4 header is set to the checksum of the + * l3 pseudo-header, so we compute it before tx-ing + */ if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM) { + tcp_header_t *tcp = + (tcp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset); + tcp->checksum = ip6_pseudo_header_cksum (ip6); hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum); } else if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM) { + udp_header_t *udp = + (udp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset); + udp->checksum = ip6_pseudo_header_cksum (ip6); hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum); } } } static void -set_gso_offsets (vlib_buffer_t *b, virtio_net_hdr_v1_t *hdr, const int is_l2) +set_gso_offsets (vlib_buffer_t *b, vnet_virtio_net_hdr_v1_t *hdr, + const int is_l2) { vnet_buffer_oflags_t oflags = vnet_buffer (b)->oflags; + i16 l4_hdr_offset = vnet_buffer (b)->l4_hdr_offset - b->current_data; if (b->flags & VNET_BUFFER_F_IS_IP4) { ip4_header_t *ip4; - generic_header_offset_t gho = { 0 }; - vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ , - 0 /* ip6 */ ); hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; hdr->gso_size = vnet_buffer2 (b)->gso_size; - hdr->hdr_len = gho.hdr_sz; + hdr->hdr_len = l4_hdr_offset + vnet_buffer2 (b)->gso_l4_hdr_sz; hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; - hdr->csum_start = gho.l4_hdr_offset; // 0x22; + hdr->csum_start = l4_hdr_offset; // 0x22; hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum); - ip4 = - (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset); + ip4 = (ip4_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset); /* * virtio devices do not support IP4 checksum offload. So driver takes care * of it while doing tx. @@ -360,35 +397,33 @@ set_gso_offsets (vlib_buffer_t *b, virtio_net_hdr_v1_t *hdr, const int is_l2) } else if (b->flags & VNET_BUFFER_F_IS_IP6) { - generic_header_offset_t gho = { 0 }; - vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ , - 1 /* ip6 */ ); hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; hdr->gso_size = vnet_buffer2 (b)->gso_size; - hdr->hdr_len = gho.hdr_sz; + hdr->hdr_len = l4_hdr_offset + vnet_buffer2 (b)->gso_l4_hdr_sz; hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; - hdr->csum_start = gho.l4_hdr_offset; // 0x36; + hdr->csum_start = l4_hdr_offset; // 0x36; hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum); } } static u16 add_buffer_to_slot (vlib_main_t *vm, vlib_node_runtime_t *node, - virtio_if_t *vif, virtio_vring_t *vring, u32 bi, + virtio_if_t *vif, vnet_virtio_vring_t *vring, u32 bi, u16 free_desc_count, u16 avail, u16 next, u16 mask, int hdr_sz, int do_gso, int csum_offload, int is_pci, int is_tun, int is_indirect, int is_any_layout) { u16 n_added = 0; - vring_desc_t *d; + vnet_virtio_vring_desc_t *d; int is_l2 = !is_tun; d = &vring->desc[next]; vlib_buffer_t *b = vlib_get_buffer (vm, bi); - virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz; + vnet_virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz; u32 drop_inline = ~0; clib_memset_u8 (hdr, 0, hdr_sz); + vring->total_packets++; if (b->flags & VNET_BUFFER_F_GSO) { if (do_gso) @@ -445,8 +480,8 @@ add_buffer_to_slot (vlib_main_t *vm, vlib_node_runtime_t *node, indirect_desc->next_buffer = bi; bi = indirect_buffer; - vring_desc_t *id = - (vring_desc_t *) vlib_buffer_get_current (indirect_desc); + vnet_virtio_vring_desc_t *id = + (vnet_virtio_vring_desc_t *) vlib_buffer_get_current (indirect_desc); u32 count = 1; if (is_pci) { @@ -515,7 +550,7 @@ add_buffer_to_slot (vlib_main_t *vm, vlib_node_runtime_t *node, } id->flags = 0; id->next = 0; - d->len = count * sizeof (vring_desc_t); + d->len = count * sizeof (vnet_virtio_vring_desc_t); d->flags = VRING_DESC_F_INDIRECT; } else if (is_pci) @@ -581,20 +616,22 @@ done: static u16 add_buffer_to_slot_packed (vlib_main_t *vm, vlib_node_runtime_t *node, - virtio_if_t *vif, virtio_vring_t *vring, u32 bi, - u16 next, int hdr_sz, int do_gso, int csum_offload, - int is_pci, int is_tun, int is_indirect, - int is_any_layout) + virtio_if_t *vif, vnet_virtio_vring_t *vring, + u32 bi, u16 next, int hdr_sz, int do_gso, + int csum_offload, int is_pci, int is_tun, + int is_indirect, int is_any_layout) { u16 n_added = 0, flags = 0; int is_l2 = !is_tun; - vring_packed_desc_t *d = &vring->packed_desc[next]; + vnet_virtio_vring_packed_desc_t *d = &vring->packed_desc[next]; vlib_buffer_t *b = vlib_get_buffer (vm, bi); - virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz; + vnet_virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz; u32 drop_inline = ~0; clib_memset (hdr, 0, hdr_sz); + vring->total_packets++; + if (b->flags & VNET_BUFFER_F_GSO) { if (do_gso) @@ -651,8 +688,9 @@ add_buffer_to_slot_packed (vlib_main_t *vm, vlib_node_runtime_t *node, indirect_desc->next_buffer = bi; bi = indirect_buffer; - vring_packed_desc_t *id = - (vring_packed_desc_t *) vlib_buffer_get_current (indirect_desc); + vnet_virtio_vring_packed_desc_t *id = + (vnet_virtio_vring_packed_desc_t *) vlib_buffer_get_current ( + indirect_desc); u32 count = 1; if (is_pci) { @@ -696,7 +734,7 @@ add_buffer_to_slot_packed (vlib_main_t *vm, vlib_node_runtime_t *node, } id->flags = 0; id->id = 0; - d->len = count * sizeof (vring_packed_desc_t); + d->len = count * sizeof (vnet_virtio_vring_packed_desc_t); flags = VRING_DESC_F_INDIRECT; } else @@ -728,12 +766,10 @@ done: } static uword -virtio_interface_tx_packed_gso_inline (vlib_main_t *vm, - vlib_node_runtime_t *node, - virtio_if_t *vif, virtio_if_type_t type, - virtio_vring_t *vring, u32 *buffers, - u16 n_left, const int do_gso, - const int csum_offload) +virtio_interface_tx_packed_gso_inline ( + vlib_main_t *vm, vlib_node_runtime_t *node, virtio_if_t *vif, + virtio_if_type_t type, vnet_virtio_vring_t *vring, u32 *buffers, u16 n_left, + const int do_gso, const int csum_offload) { int is_pci = (type == VIRTIO_IF_TYPE_PCI); int is_tun = (type == VIRTIO_IF_TYPE_TUN); @@ -742,7 +778,7 @@ virtio_interface_tx_packed_gso_inline (vlib_main_t *vm, int is_any_layout = ((vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) != 0); const int hdr_sz = vif->virtio_net_hdr_sz; - u16 sz = vring->size; + u16 sz = vring->queue_size; u16 used, next, n_buffers = 0, n_buffers_left = 0; u16 n_vectors = n_left; @@ -776,6 +812,7 @@ virtio_interface_tx_packed_gso_inline (vlib_main_t *vm, vring->avail_wrap_counter ^= 1; } } + virtio_txq_clear_scheduled (vring); } while (n_left && used < sz) @@ -813,7 +850,7 @@ virtio_interface_tx_packed_gso_inline (vlib_main_t *vm, } static void -virtio_find_free_desc (virtio_vring_t *vring, u16 size, u16 mask, u16 req, +virtio_find_free_desc (vnet_virtio_vring_t *vring, u16 size, u16 mask, u16 req, u16 next, u32 *first_free_desc_index, u16 *free_desc_count) { @@ -852,7 +889,7 @@ static u16 virtio_interface_tx_split_gso_inline (vlib_main_t *vm, vlib_node_runtime_t *node, virtio_if_t *vif, virtio_if_type_t type, - virtio_vring_t *vring, u32 *buffers, + vnet_virtio_vring_t *vring, u32 *buffers, u16 n_left, int do_gso, int csum_offload) { u16 used, next, avail, n_buffers = 0, n_buffers_left = 0; @@ -862,7 +899,7 @@ virtio_interface_tx_split_gso_inline (vlib_main_t *vm, ((vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) != 0); int is_any_layout = ((vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) != 0); - u16 sz = vring->size; + u16 sz = vring->queue_size; int hdr_sz = vif->virtio_net_hdr_sz; u16 mask = sz - 1; u16 n_vectors = n_left; @@ -916,6 +953,7 @@ virtio_interface_tx_split_gso_inline (vlib_main_t *vm, n_buffers_left--; free_desc_count -= n_added; } + virtio_txq_clear_scheduled (vring); } while (n_left && free_desc_count) @@ -960,7 +998,7 @@ virtio_interface_tx_split_gso_inline (vlib_main_t *vm, static u16 virtio_interface_tx_gso_inline (vlib_main_t *vm, vlib_node_runtime_t *node, virtio_if_t *vif, virtio_if_type_t type, - virtio_vring_t *vring, u32 *buffers, + vnet_virtio_vring_t *vring, u32 *buffers, u16 n_left, int packed, int do_gso, int csum_offload) { @@ -976,19 +1014,19 @@ virtio_interface_tx_gso_inline (vlib_main_t *vm, vlib_node_runtime_t *node, static u16 virtio_interface_tx_inline (vlib_main_t *vm, vlib_node_runtime_t *node, - virtio_if_t *vif, virtio_vring_t *vring, + virtio_if_t *vif, vnet_virtio_vring_t *vring, virtio_if_type_t type, u32 *buffers, u16 n_left, int packed) { vnet_main_t *vnm = vnet_get_main (); vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index); - if (hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO) + if (hw->caps & VNET_HW_IF_CAP_TCP_GSO) return virtio_interface_tx_gso_inline (vm, node, vif, type, vring, buffers, n_left, packed, 1 /* do_gso */ , 1 /* checksum offload */ ); - else if (hw->caps & VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM) + else if (hw->caps & VNET_HW_IF_CAP_L4_TX_CKSUM) return virtio_interface_tx_gso_inline (vm, node, vif, type, vring, buffers, n_left, packed, 0 /* no do_gso */ , @@ -1007,21 +1045,24 @@ VNET_DEVICE_CLASS_TX_FN (virtio_device_class) (vlib_main_t * vm, virtio_main_t *nm = &virtio_main; vnet_interface_output_runtime_t *rund = (void *) node->runtime_data; virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance); - u16 qid = vm->thread_index % vif->num_txqs; - virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, qid); + vnet_hw_if_tx_frame_t *tf = vlib_frame_scalar_args (frame); + u16 qid = tf->queue_id; + vnet_virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, qid); u16 n_left = frame->n_vectors; u32 *buffers = vlib_frame_vector_args (frame); u32 to[GRO_TO_VECTOR_SIZE (n_left)]; int packed = vif->is_packed; u16 n_vectors = frame->n_vectors; - clib_spinlock_lock_if_init (&vring->lockp); + if (tf->shared_queue) + clib_spinlock_lock (&vring->lockp); if (vif->packet_coalesce) { n_vectors = n_left = vnet_gro_inline (vm, vring->flow_table, buffers, n_left, to); buffers = to; + virtio_txq_clear_scheduled (vring); } u16 retry_count = 2; @@ -1065,7 +1106,8 @@ retry: &buffers[n_vectors - n_left], n_left, VIRTIO_TX_ERROR_NO_FREE_SLOTS); - clib_spinlock_unlock_if_init (&vring->lockp); + if (tf->shared_queue) + clib_spinlock_unlock (&vring->lockp); return frame->n_vectors - n_left; } @@ -1097,7 +1139,7 @@ virtio_clear_hw_interface_counters (u32 instance) } static void -virtio_set_rx_interrupt (virtio_if_t *vif, virtio_vring_t *vring) +virtio_set_rx_interrupt (virtio_if_t *vif, vnet_virtio_vring_t *vring) { if (vif->is_packed) vring->driver_event->flags &= ~VRING_EVENT_F_DISABLE; @@ -1106,7 +1148,7 @@ virtio_set_rx_interrupt (virtio_if_t *vif, virtio_vring_t *vring) } static void -virtio_set_rx_polling (virtio_if_t *vif, virtio_vring_t *vring) +virtio_set_rx_polling (virtio_if_t *vif, vnet_virtio_vring_t *vring) { if (vif->is_packed) vring->driver_event->flags |= VRING_EVENT_F_DISABLE; @@ -1118,11 +1160,10 @@ static clib_error_t * virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid, vnet_hw_if_rx_mode mode) { - vlib_main_t *vm = vnm->vlib_main; virtio_main_t *mm = &virtio_main; vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index); virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); - virtio_vring_t *rx_vring = vec_elt_at_index (vif->rxq_vrings, qid); + vnet_virtio_vring_t *rx_vring = vec_elt_at_index (vif->rxq_vrings, qid); if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode)) { @@ -1131,30 +1172,9 @@ virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid, } if (mode == VNET_HW_IF_RX_MODE_POLLING) - { - if (vif->packet_coalesce || vif->packet_buffering) - { - if (mm->interrupt_queues_count > 0) - mm->interrupt_queues_count--; - if (mm->interrupt_queues_count == 0) - vlib_process_signal_event (vm, - virtio_send_interrupt_node.index, - VIRTIO_EVENT_STOP_TIMER, 0); - } virtio_set_rx_polling (vif, rx_vring); - } else - { - if (vif->packet_coalesce || vif->packet_buffering) - { - mm->interrupt_queues_count++; - if (mm->interrupt_queues_count == 1) - vlib_process_signal_event (vm, - virtio_send_interrupt_node.index, - VIRTIO_EVENT_START_TIMER, 0); - } virtio_set_rx_interrupt (vif, rx_vring); - } rx_vring->mode = mode; @@ -1182,16 +1202,6 @@ virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) return 0; } -static clib_error_t * -virtio_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; -} - -/* *INDENT-OFF* */ VNET_DEVICE_CLASS (virtio_device_class) = { .name = "virtio", .format_device_name = format_virtio_device_name, @@ -1202,11 +1212,9 @@ VNET_DEVICE_CLASS (virtio_device_class) = { .rx_redirect_to_node = virtio_set_interface_next_node, .clear_counters = virtio_clear_hw_interface_counters, .admin_up_down_function = virtio_interface_admin_up_down, - .subif_add_del_function = virtio_subif_add_del_function, .rx_mode_change_function = virtio_interface_rx_mode_change, }; -/* *INDENT-ON* */ /* * fd.io coding-style-patch-verification: ON diff --git a/src/vnet/devices/virtio/node.c b/src/vnet/devices/virtio/node.c index c36c0807de0..8c837575cf8 100644 --- a/src/vnet/devices/virtio/node.c +++ b/src/vnet/devices/virtio/node.c @@ -19,7 +19,11 @@ #include <sys/stat.h> #include <fcntl.h> #include <net/if.h> +#ifdef __linux__ #include <linux/if_tun.h> +#elif __FreeBSD__ +#include <net/if_tun.h> +#endif /* __linux */ #include <sys/ioctl.h> #include <sys/eventfd.h> @@ -27,11 +31,11 @@ #include <vlib/unix/unix.h> #include <vnet/ethernet/ethernet.h> #include <vnet/feature/feature.h> -#include <vnet/gso/gro_func.h> #include <vnet/interface/rx_queue_funcs.h> #include <vnet/ip/ip4_packet.h> #include <vnet/ip/ip6_packet.h> #include <vnet/udp/udp_packet.h> +#include <vnet/tcp/tcp_packet.h> #include <vnet/devices/virtio/virtio.h> #include <vnet/devices/virtio/virtio_inline.h> @@ -47,7 +51,7 @@ typedef struct u32 hw_if_index; u16 ring; u16 len; - virtio_net_hdr_v1_t hdr; + vnet_virtio_net_hdr_v1_t hdr; } virtio_input_trace_t; static u8 * @@ -69,8 +73,8 @@ format_virtio_input_trace (u8 * s, va_list * args) } static_always_inline void -virtio_needs_csum (vlib_buffer_t * b0, virtio_net_hdr_v1_t * hdr, - u8 * l4_proto, u8 * l4_hdr_sz, virtio_if_type_t type) +virtio_needs_csum (vlib_buffer_t *b0, vnet_virtio_net_hdr_v1_t *hdr, + u8 *l4_proto, u8 *l4_hdr_sz, virtio_if_type_t type) { if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { @@ -91,8 +95,7 @@ virtio_needs_csum (vlib_buffer_t * b0, virtio_net_hdr_v1_t * hdr, } else { - ethernet_header_t *eh = - (ethernet_header_t *) vlib_buffer_get_current (b0); + ethernet_header_t *eh = (ethernet_header_t *) b0->data; ethertype = clib_net_to_host_u16 (eh->type); l2hdr_sz = sizeof (ethernet_header_t); @@ -117,8 +120,7 @@ virtio_needs_csum (vlib_buffer_t * b0, virtio_net_hdr_v1_t * hdr, if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP4)) { - ip4_header_t *ip4 = - (ip4_header_t *) (vlib_buffer_get_current (b0) + l2hdr_sz); + ip4_header_t *ip4 = (ip4_header_t *) (b0->data + l2hdr_sz); vnet_buffer (b0)->l4_hdr_offset = l2hdr_sz + ip4_header_bytes (ip4); *l4_proto = ip4->protocol; oflags |= VNET_BUFFER_OFFLOAD_F_IP_CKSUM; @@ -129,8 +131,7 @@ virtio_needs_csum (vlib_buffer_t * b0, virtio_net_hdr_v1_t * hdr, } else if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP6)) { - ip6_header_t *ip6 = - (ip6_header_t *) (vlib_buffer_get_current (b0) + l2hdr_sz); + ip6_header_t *ip6 = (ip6_header_t *) (b0->data + l2hdr_sz); vnet_buffer (b0)->l4_hdr_offset = l2hdr_sz + sizeof (ip6_header_t); /* FIXME IPv6 EH traversal */ *l4_proto = ip6->protocol; @@ -142,18 +143,14 @@ virtio_needs_csum (vlib_buffer_t * b0, virtio_net_hdr_v1_t * hdr, if (*l4_proto == IP_PROTOCOL_TCP) { oflags |= VNET_BUFFER_OFFLOAD_F_TCP_CKSUM; - tcp_header_t *tcp = (tcp_header_t *) (vlib_buffer_get_current (b0) + - vnet_buffer - (b0)->l4_hdr_offset); + tcp_header_t *tcp = + (tcp_header_t *) (b0->data + vnet_buffer (b0)->l4_hdr_offset); *l4_hdr_sz = tcp_header_bytes (tcp); } else if (*l4_proto == IP_PROTOCOL_UDP) { oflags |= VNET_BUFFER_OFFLOAD_F_UDP_CKSUM; - udp_header_t *udp = (udp_header_t *) (vlib_buffer_get_current (b0) + - vnet_buffer - (b0)->l4_hdr_offset); - *l4_hdr_sz = sizeof (*udp); + *l4_hdr_sz = sizeof (udp_header_t); } if (oflags) vnet_buffer_offload_flags_set (b0, oflags); @@ -161,7 +158,7 @@ virtio_needs_csum (vlib_buffer_t * b0, virtio_net_hdr_v1_t * hdr, } static_always_inline void -fill_gso_buffer_flags (vlib_buffer_t * b0, virtio_net_hdr_v1_t * hdr, +fill_gso_buffer_flags (vlib_buffer_t *b0, vnet_virtio_net_hdr_v1_t *hdr, u8 l4_proto, u8 l4_hdr_sz) { if (hdr->gso_type == VIRTIO_NET_HDR_GSO_TCPV4) @@ -181,7 +178,7 @@ fill_gso_buffer_flags (vlib_buffer_t * b0, virtio_net_hdr_v1_t * hdr, } static_always_inline u16 -virtio_n_left_to_process (virtio_vring_t * vring, const int packed) +virtio_n_left_to_process (vnet_virtio_vring_t *vring, const int packed) { if (packed) return vring->desc_in_use; @@ -190,7 +187,7 @@ virtio_n_left_to_process (virtio_vring_t * vring, const int packed) } static_always_inline u16 -virtio_get_slot_id (virtio_vring_t * vring, const int packed, u16 last, +virtio_get_slot_id (vnet_virtio_vring_t *vring, const int packed, u16 last, u16 mask) { if (packed) @@ -200,7 +197,7 @@ virtio_get_slot_id (virtio_vring_t * vring, const int packed, u16 last, } static_always_inline u16 -virtio_get_len (virtio_vring_t * vring, const int packed, const int hdr_sz, +virtio_get_len (vnet_virtio_vring_t *vring, const int packed, const int hdr_sz, u16 last, u16 mask) { if (packed) @@ -209,22 +206,60 @@ virtio_get_len (virtio_vring_t * vring, const int packed, const int hdr_sz, return vring->used->ring[last & mask].len - hdr_sz; } -#define increment_last(last, packed, vring) \ - do { \ - last++; \ - if (packed && last >= vring->size) \ - { \ - last = 0; \ - vring->used_wrap_counter ^= 1; \ - } \ - } while (0) +#define virtio_packed_check_n_left(vring, last) \ + do \ + { \ + vnet_virtio_vring_packed_desc_t *d = &vring->packed_desc[last]; \ + u16 flags = d->flags; \ + if ((flags & VRING_DESC_F_AVAIL) != (vring->used_wrap_counter << 7) || \ + (flags & VRING_DESC_F_USED) != (vring->used_wrap_counter << 15)) \ + { \ + n_left = 0; \ + } \ + } \ + while (0) + +#define increment_last(last, packed, vring) \ + do \ + { \ + last++; \ + if (packed && last >= vring->queue_size) \ + { \ + last = 0; \ + vring->used_wrap_counter ^= 1; \ + } \ + } \ + while (0) + +static_always_inline void +virtio_device_input_ethernet (vlib_main_t *vm, vlib_node_runtime_t *node, + const u32 next_index, const u32 sw_if_index, + const u32 hw_if_index) +{ + vlib_next_frame_t *nf; + vlib_frame_t *f; + ethernet_input_frame_t *ef; + + if (PREDICT_FALSE (VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT != next_index)) + return; + + nf = vlib_node_runtime_get_next_frame ( + vm, node, VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT); + f = vlib_get_frame (vm, nf->frame); + f->flags = ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX; + + ef = vlib_frame_scalar_args (f); + ef->sw_if_index = sw_if_index; + ef->hw_if_index = hw_if_index; + vlib_frame_no_append (f); +} static_always_inline uword -virtio_device_input_gso_inline (vlib_main_t * vm, vlib_node_runtime_t * node, - vlib_frame_t * frame, virtio_if_t * vif, - virtio_vring_t * vring, virtio_if_type_t type, - int gso_enabled, int checksum_offload_enabled, - int packed) +virtio_device_input_gso_inline (vlib_main_t *vm, vlib_node_runtime_t *node, + vlib_frame_t *frame, virtio_if_t *vif, + vnet_virtio_vring_t *vring, + virtio_if_type_t type, int gso_enabled, + int checksum_offload_enabled, int packed) { vnet_main_t *vnm = vnet_get_main (); u32 thread_index = vm->thread_index; @@ -234,10 +269,15 @@ virtio_device_input_gso_inline (vlib_main_t * vm, vlib_node_runtime_t * node, u32 *to_next = 0; u32 n_rx_packets = 0; u32 n_rx_bytes = 0; - u16 mask = vring->size - 1; + u16 mask = vring->queue_size - 1; u16 last = vring->last_used_idx; u16 n_left = virtio_n_left_to_process (vring, packed); - vlib_buffer_t bt; + vlib_buffer_t bt = {}; + + if (packed) + { + virtio_packed_check_n_left (vring, last); + } if (n_left == 0) return 0; @@ -253,7 +293,7 @@ virtio_device_input_gso_inline (vlib_main_t * vm, vlib_node_runtime_t * node, next_index = vif->per_interface_next_index; /* only for l2, redirect if feature path enabled */ - vnet_feature_start_device_input_x1 (vif->sw_if_index, &next_index, &bt); + vnet_feature_start_device_input (vif->sw_if_index, &next_index, &bt); } while (n_left) @@ -261,13 +301,13 @@ virtio_device_input_gso_inline (vlib_main_t * vm, vlib_node_runtime_t * node, u32 n_left_to_next; u32 next0 = next_index; - vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); + vlib_get_new_next_frame (vm, node, next_index, to_next, n_left_to_next); while (n_left && n_left_to_next) { if (packed) { - vring_packed_desc_t *d = &vring->packed_desc[last]; + vnet_virtio_vring_packed_desc_t *d = &vring->packed_desc[last]; u16 flags = d->flags; if ((flags & VRING_DESC_F_AVAIL) != (vring->used_wrap_counter << 7) @@ -280,13 +320,13 @@ virtio_device_input_gso_inline (vlib_main_t * vm, vlib_node_runtime_t * node, } u8 l4_proto = 0, l4_hdr_sz = 0; u16 num_buffers = 1; - virtio_net_hdr_v1_t *hdr; + vnet_virtio_net_hdr_v1_t *hdr; u16 slot = virtio_get_slot_id (vring, packed, last, mask); u16 len = virtio_get_len (vring, packed, hdr_sz, last, mask); u32 bi0 = vring->buffers[slot]; vlib_buffer_t *b0 = vlib_get_buffer (vm, bi0); hdr = vlib_buffer_get_current (b0); - if (hdr_sz == sizeof (virtio_net_hdr_v1_t)) + if (hdr_sz == sizeof (vnet_virtio_net_hdr_v1_t)) num_buffers = hdr->num_buffers; b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID; @@ -371,7 +411,7 @@ virtio_device_input_gso_inline (vlib_main_t * vm, vlib_node_runtime_t * node, tr->next_index = next0; tr->hw_if_index = vif->hw_if_index; tr->len = len; - clib_memcpy_fast (&tr->hdr, hdr, hdr_sz); + clib_memcpy_fast (&tr->hdr, hdr, (hdr_sz == 12) ? 12 : 10); } /* enqueue buffer */ @@ -391,10 +431,13 @@ virtio_device_input_gso_inline (vlib_main_t * vm, vlib_node_runtime_t * node, n_rx_packets++; n_rx_bytes += len; } + virtio_device_input_ethernet (vm, node, next_index, vif->sw_if_index, + vif->hw_if_index); vlib_put_next_frame (vm, node, next_index, n_left_to_next); } vring->last_used_idx = last; + vring->total_packets += n_rx_packets; vlib_increment_combined_counter (vnm->interface_main.combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX, thread_index, vif->sw_if_index, n_rx_packets, @@ -408,23 +451,10 @@ virtio_device_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame, virtio_if_t * vif, u16 qid, virtio_if_type_t type) { - virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, qid); + vnet_virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, qid); const int hdr_sz = vif->virtio_net_hdr_sz; - u16 txq_id = vm->thread_index % vif->num_txqs; - virtio_vring_t *txq_vring = vec_elt_at_index (vif->txq_vrings, txq_id); uword rv; - if (clib_spinlock_trylock_if_init (&txq_vring->lockp)) - { - if (vif->packet_coalesce) - vnet_gro_flow_table_schedule_node_on_dispatcher - (vm, txq_vring->flow_table); - else if (vif->packet_buffering) - virtio_vring_buffering_schedule_node_on_dispatcher - (vm, txq_vring->buffering); - clib_spinlock_unlock_if_init (&txq_vring->lockp); - } - if (vif->is_packed) { if (vif->gso_enabled) @@ -494,7 +524,6 @@ VLIB_NODE_FN (virtio_input_node) (vlib_main_t * vm, return n_rx; } -/* *INDENT-OFF* */ VLIB_REGISTER_NODE (virtio_input_node) = { .name = "virtio-input", .sibling_of = "device-input", @@ -505,7 +534,6 @@ VLIB_REGISTER_NODE (virtio_input_node) = { .n_errors = VIRTIO_INPUT_N_ERROR, .error_strings = virtio_input_error_strings, }; -/* *INDENT-ON* */ /* * fd.io coding-style-patch-verification: ON diff --git a/src/vnet/devices/virtio/pci.c b/src/vnet/devices/virtio/pci.c index 7ef4b2a52dd..6234f64fcfb 100644 --- a/src/vnet/devices/virtio/pci.c +++ b/src/vnet/devices/virtio/pci.c @@ -116,7 +116,7 @@ virtio_pci_irq_queue_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, line--; u16 qid = line; - virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, qid); + vnet_virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, qid); vnet_hw_if_rx_queue_set_int_pending (vnm, vring->queue_index); } @@ -131,13 +131,11 @@ virtio_pci_irq_config_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h, if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP) { - vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; vnet_hw_interface_set_flags (vnm, vif->hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP); } else { - vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP; vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); } } @@ -200,18 +198,18 @@ static int virtio_pci_send_ctrl_msg_packed (vlib_main_t * vm, virtio_if_t * vif, virtio_ctrl_msg_t * data, u32 len) { - virtio_vring_t *vring = vif->cxq_vring; + vnet_virtio_vring_t *vring = vif->cxq_vring; virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR; virtio_ctrl_msg_t result; u32 buffer_index; vlib_buffer_t *b; u16 used, next; - u16 sz = vring->size; + u16 sz = vring->queue_size; u16 flags = 0, first_desc_flags = 0; used = vring->desc_in_use; next = vring->desc_next; - vring_packed_desc_t *d = &vring->packed_desc[next]; + vnet_virtio_vring_packed_desc_t *d = &vring->packed_desc[next]; if (vlib_buffer_alloc (vm, &buffer_index, 1)) b = vlib_get_buffer (vm, buffer_index); @@ -319,9 +317,9 @@ virtio_pci_send_ctrl_msg_packed (vlib_main_t * vm, virtio_if_t * vif, || (flags & VRING_DESC_F_USED) != (vring->used_wrap_counter << 15)); last += 3; - if (last >= vring->size) + if (last >= vring->queue_size) { - last = last - vring->size; + last = last - vring->queue_size; vring->used_wrap_counter ^= 1; } vring->desc_in_use -= 3; @@ -340,19 +338,19 @@ static int virtio_pci_send_ctrl_msg_split (vlib_main_t * vm, virtio_if_t * vif, virtio_ctrl_msg_t * data, u32 len) { - virtio_vring_t *vring = vif->cxq_vring; + vnet_virtio_vring_t *vring = vif->cxq_vring; virtio_net_ctrl_ack_t status = VIRTIO_NET_ERR; virtio_ctrl_msg_t result; u32 buffer_index; vlib_buffer_t *b; u16 used, next, avail; - u16 sz = vring->size; + u16 sz = vring->queue_size; u16 mask = sz - 1; used = vring->desc_in_use; next = vring->desc_next; avail = vring->avail->idx; - vring_desc_t *d = &vring->desc[next]; + vnet_virtio_vring_desc_t *d = &vring->desc[next]; if (vlib_buffer_alloc (vm, &buffer_index, 1)) b = vlib_get_buffer (vm, buffer_index); @@ -405,7 +403,7 @@ virtio_pci_send_ctrl_msg_split (vlib_main_t * vm, virtio_if_t * vif, while (n_left) { - vring_used_elem_t *e = &vring->used->ring[last & mask]; + vnet_virtio_vring_used_elem_t *e = &vring->used->ring[last & mask]; u16 slot = e->id; d = &vring->desc[slot]; @@ -508,7 +506,7 @@ virtio_pci_offloads (vlib_main_t * vm, virtio_if_t * vif, int gso_enabled, int csum_offload_enabled) { vnet_main_t *vnm = vnet_get_main (); - vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index); + vnet_hw_if_caps_change_t cc = {}; if ((vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) && (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))) @@ -524,10 +522,10 @@ virtio_pci_offloads (vlib_main_t * vm, virtio_if_t * vif, int gso_enabled, else { vif->gso_enabled = 1; - vif->csum_offload_enabled = 0; - hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO | - VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM | - VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM; + vif->csum_offload_enabled = 1; + cc.val = cc.mask = VNET_HW_IF_CAP_TCP_GSO | + VNET_HW_IF_CAP_TX_TCP_CKSUM | + VNET_HW_IF_CAP_TX_UDP_CKSUM; } } else if (csum_offload_enabled @@ -541,9 +539,10 @@ virtio_pci_offloads (vlib_main_t * vm, virtio_if_t * vif, int gso_enabled, { vif->csum_offload_enabled = 1; vif->gso_enabled = 0; - hw->caps &= ~VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO; - hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM | - VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM; + cc.val = + VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM; + cc.mask = VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_TX_TCP_CKSUM | + VNET_HW_IF_CAP_TX_UDP_CKSUM; } } else @@ -556,12 +555,15 @@ virtio_pci_offloads (vlib_main_t * vm, virtio_if_t * vif, int gso_enabled, { vif->csum_offload_enabled = 0; vif->gso_enabled = 0; - hw->caps &= ~(VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM | - VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO); + cc.val = 0; + cc.mask = VNET_HW_IF_CAP_L4_TX_CKSUM | VNET_HW_IF_CAP_TCP_GSO; } } } + if (cc.mask) + vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc); + return 0; } @@ -598,7 +600,7 @@ virtio_pci_control_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif, { clib_error_t *error = 0; u16 queue_size = 0; - virtio_vring_t *vring; + vnet_virtio_vring_t *vring; u32 i = 0; void *ptr = NULL; @@ -613,34 +615,36 @@ virtio_pci_control_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif, vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES); vring = vec_elt_at_index (vif->cxq_vring, 0); - i = - (((queue_size * sizeof (vring_packed_desc_t)) + - sizeof (vring_desc_event_t) + VIRTIO_PCI_VRING_ALIGN - - 1) & ~(VIRTIO_PCI_VRING_ALIGN - 1)) + sizeof (vring_desc_event_t); + i = (((queue_size * sizeof (vnet_virtio_vring_packed_desc_t)) + + sizeof (vnet_virtio_vring_desc_event_t) + VNET_VIRTIO_PCI_VRING_ALIGN - + 1) & + ~(VNET_VIRTIO_PCI_VRING_ALIGN - 1)) + + sizeof (vnet_virtio_vring_desc_event_t); - ptr = - vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN, - vif->numa_node); + ptr = vlib_physmem_alloc_aligned_on_numa (vm, i, VNET_VIRTIO_PCI_VRING_ALIGN, + vif->numa_node); if (!ptr) return vlib_physmem_last_error (vm); clib_memset (ptr, 0, i); vring->packed_desc = ptr; - vring->driver_event = ptr + (queue_size * sizeof (vring_packed_desc_t)); + vring->driver_event = + ptr + (queue_size * sizeof (vnet_virtio_vring_packed_desc_t)); vring->driver_event->off_wrap = 0; vring->driver_event->flags = VRING_EVENT_F_DISABLE; vring->device_event = - ptr + - (((queue_size * sizeof (vring_packed_desc_t)) + - sizeof (vring_desc_event_t) + VIRTIO_PCI_VRING_ALIGN - - 1) & ~(VIRTIO_PCI_VRING_ALIGN - 1)); + ptr + (((queue_size * sizeof (vnet_virtio_vring_packed_desc_t)) + + sizeof (vnet_virtio_vring_desc_event_t) + + VNET_VIRTIO_PCI_VRING_ALIGN - 1) & + ~(VNET_VIRTIO_PCI_VRING_ALIGN - 1)); vring->device_event->off_wrap = 0; vring->device_event->flags = 0; + vring->total_packets = 0; vring->queue_id = queue_num; - vring->size = queue_size; + vring->queue_size = queue_size; vring->avail_wrap_counter = 1; vring->used_wrap_counter = 1; @@ -648,7 +652,7 @@ virtio_pci_control_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif, virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num, queue_size); - vif->virtio_pci_func->setup_queue (vm, vif, queue_num, (void *) vring); + vif->virtio_pci_func->setup_queue (vm, vif, queue_num, vring); vring->queue_notify_offset = vif->notify_off_multiplier * vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num); @@ -663,8 +667,7 @@ virtio_pci_control_vring_split_init (vlib_main_t * vm, virtio_if_t * vif, { clib_error_t *error = 0; u16 queue_size = 0; - virtio_vring_t *vring; - vring_t vr; + vnet_virtio_vring_t *vring; u32 i = 0; void *ptr = NULL; @@ -683,27 +686,21 @@ virtio_pci_control_vring_split_init (vlib_main_t * vm, virtio_if_t * vif, vec_validate_aligned (vif->cxq_vring, 0, CLIB_CACHE_LINE_BYTES); vring = vec_elt_at_index (vif->cxq_vring, 0); - i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN); - i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN); - ptr = - vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN, - vif->numa_node); + i = vnet_virtio_vring_size (queue_size, VNET_VIRTIO_PCI_VRING_ALIGN); + i = round_pow2 (i, VNET_VIRTIO_PCI_VRING_ALIGN); + ptr = vlib_physmem_alloc_aligned_on_numa (vm, i, VNET_VIRTIO_PCI_VRING_ALIGN, + vif->numa_node); if (!ptr) return vlib_physmem_last_error (vm); clib_memset (ptr, 0, i); - vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN); - vring->desc = vr.desc; - vring->avail = vr.avail; - vring->used = vr.used; + vnet_virtio_vring_init (vring, queue_size, ptr, VNET_VIRTIO_PCI_VRING_ALIGN); vring->queue_id = queue_num; - vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT; + vring->total_packets = 0; ASSERT (vring->buffers == 0); - - vring->size = queue_size; virtio_log_debug (vif, "control-queue: number %u, size %u", queue_num, queue_size); - vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr); + vif->virtio_pci_func->setup_queue (vm, vif, queue_num, vring); vring->queue_notify_offset = vif->notify_off_multiplier * vif->virtio_pci_func->get_queue_notify_off (vm, vif, queue_num); @@ -724,14 +721,12 @@ virtio_pci_control_vring_init (vlib_main_t * vm, virtio_if_t * vif, } clib_error_t * -virtio_pci_vring_split_init (vlib_main_t * vm, virtio_if_t * vif, - u16 queue_num) +virtio_pci_vring_split_init (vlib_main_t *vm, virtio_if_t *vif, u16 queue_num, + u16 txq_size) { - vlib_thread_main_t *vtm = vlib_get_thread_main (); clib_error_t *error = 0; u16 queue_size = 0; - virtio_vring_t *vring; - vring_t vr; + vnet_virtio_vring_t *vring; u32 i = 0; void *ptr = NULL; @@ -750,11 +745,20 @@ virtio_pci_vring_split_init (vlib_main_t * vm, virtio_if_t * vif, if (queue_num % 2) { + if (txq_size) + { + virtio_log_debug (vif, "tx-queue: number %u, default-size %u", + queue_num, queue_size); + vif->virtio_pci_func->set_queue_size (vm, vif, queue_num, txq_size); + queue_size = + vif->virtio_pci_func->get_queue_size (vm, vif, queue_num); + virtio_log_debug (vif, "tx-queue: number %u, new size %u", queue_num, + queue_size); + } vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num), CLIB_CACHE_LINE_BYTES); vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num)); - if (vif->max_queue_pairs < vtm->n_vlib_mains) - clib_spinlock_init (&vring->lockp); + clib_spinlock_init (&vring->lockp); } else { @@ -762,21 +766,18 @@ virtio_pci_vring_split_init (vlib_main_t * vm, virtio_if_t * vif, CLIB_CACHE_LINE_BYTES); vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num)); } - i = vring_size (queue_size, VIRTIO_PCI_VRING_ALIGN); - i = round_pow2 (i, VIRTIO_PCI_VRING_ALIGN); - ptr = - vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN, - vif->numa_node); + i = vnet_virtio_vring_size (queue_size, VNET_VIRTIO_PCI_VRING_ALIGN); + i = round_pow2 (i, VNET_VIRTIO_PCI_VRING_ALIGN); + ptr = vlib_physmem_alloc_aligned_on_numa (vm, i, VNET_VIRTIO_PCI_VRING_ALIGN, + vif->numa_node); if (!ptr) return vlib_physmem_last_error (vm); clib_memset (ptr, 0, i); - vring_init (&vr, queue_size, ptr, VIRTIO_PCI_VRING_ALIGN); - vring->desc = vr.desc; - vring->avail = vr.avail; - vring->used = vr.used; + vnet_virtio_vring_init (vring, queue_size, ptr, VNET_VIRTIO_PCI_VRING_ALIGN); vring->queue_id = queue_num; vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT; vring->flow_table = 0; + vring->total_packets = 0; ASSERT (vring->buffers == 0); vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES); @@ -791,8 +792,8 @@ virtio_pci_vring_split_init (vlib_main_t * vm, virtio_if_t * vif, virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num, queue_size); } - vring->size = queue_size; - if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, ptr)) + vring->queue_size = queue_size; + if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, vring)) return clib_error_return (0, "error in queue address setup"); vring->queue_notify_offset = @@ -807,10 +808,9 @@ clib_error_t * virtio_pci_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num) { - vlib_thread_main_t *vtm = vlib_get_thread_main (); clib_error_t *error = 0; u16 queue_size = 0; - virtio_vring_t *vring; + vnet_virtio_vring_t *vring; u32 i = 0; void *ptr = NULL; @@ -827,8 +827,7 @@ virtio_pci_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif, vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num), CLIB_CACHE_LINE_BYTES); vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (queue_num)); - if (vif->max_queue_pairs < vtm->n_vlib_mains) - clib_spinlock_init (&vring->lockp); + clib_spinlock_init (&vring->lockp); } else { @@ -837,29 +836,30 @@ virtio_pci_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif, vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (queue_num)); } - i = - (((queue_size * sizeof (vring_packed_desc_t)) + - sizeof (vring_desc_event_t) + VIRTIO_PCI_VRING_ALIGN - - 1) & ~(VIRTIO_PCI_VRING_ALIGN - 1)) + sizeof (vring_desc_event_t); + i = (((queue_size * sizeof (vnet_virtio_vring_packed_desc_t)) + + sizeof (vnet_virtio_vring_desc_event_t) + VNET_VIRTIO_PCI_VRING_ALIGN - + 1) & + ~(VNET_VIRTIO_PCI_VRING_ALIGN - 1)) + + sizeof (vnet_virtio_vring_desc_event_t); - ptr = - vlib_physmem_alloc_aligned_on_numa (vm, i, VIRTIO_PCI_VRING_ALIGN, - vif->numa_node); + ptr = vlib_physmem_alloc_aligned_on_numa (vm, i, VNET_VIRTIO_PCI_VRING_ALIGN, + vif->numa_node); if (!ptr) return vlib_physmem_last_error (vm); clib_memset (ptr, 0, i); vring->packed_desc = ptr; - vring->driver_event = ptr + (queue_size * sizeof (vring_packed_desc_t)); + vring->driver_event = + ptr + (queue_size * sizeof (vnet_virtio_vring_packed_desc_t)); vring->driver_event->off_wrap = 0; vring->driver_event->flags = VRING_EVENT_F_DISABLE; vring->device_event = - ptr + - (((queue_size * sizeof (vring_packed_desc_t)) + - sizeof (vring_desc_event_t) + VIRTIO_PCI_VRING_ALIGN - - 1) & ~(VIRTIO_PCI_VRING_ALIGN - 1)); + ptr + (((queue_size * sizeof (vnet_virtio_vring_packed_desc_t)) + + sizeof (vnet_virtio_vring_desc_event_t) + + VNET_VIRTIO_PCI_VRING_ALIGN - 1) & + ~(VNET_VIRTIO_PCI_VRING_ALIGN - 1)); vring->device_event->off_wrap = 0; vring->device_event->flags = 0; @@ -867,6 +867,7 @@ virtio_pci_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif, vring->avail_wrap_counter = 1; vring->used_wrap_counter = 1; + vring->total_packets = 0; ASSERT (vring->buffers == 0); vec_validate_aligned (vring->buffers, queue_size, CLIB_CACHE_LINE_BYTES); @@ -881,8 +882,8 @@ virtio_pci_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif, virtio_log_debug (vif, "rx-queue: number %u, size %u", queue_num, queue_size); } - vring->size = queue_size; - if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, (void *) vring)) + vring->queue_size = queue_size; + if (vif->virtio_pci_func->setup_queue (vm, vif, queue_num, vring)) return clib_error_return (0, "error in queue address setup"); vring->queue_notify_offset = @@ -895,12 +896,13 @@ virtio_pci_vring_packed_init (vlib_main_t * vm, virtio_if_t * vif, } clib_error_t * -virtio_pci_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 queue_num) +virtio_pci_vring_init (vlib_main_t *vm, virtio_if_t *vif, u16 queue_num, + u16 txq_size) { if (vif->is_packed) return virtio_pci_vring_packed_init (vm, vif, queue_num); else - return virtio_pci_vring_split_init (vm, vif, queue_num); + return virtio_pci_vring_split_init (vm, vif, queue_num, txq_size); } static void @@ -1147,7 +1149,6 @@ virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif, virtio_pci_create_if_args_t * args, void **bar) { clib_error_t *error = 0; - vlib_thread_main_t *vtm = vlib_get_thread_main (); u8 status = 0; if ((error = virtio_pci_read_caps (vm, vif, bar))) @@ -1239,7 +1240,7 @@ virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif, for (int i = 0; i < vif->max_queue_pairs; i++) { - if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i)))) + if ((error = virtio_pci_vring_init (vm, vif, RX_QUEUE (i), 0))) { args->rv = VNET_API_ERROR_INIT_FAILED; virtio_log_error (vif, "%s (%u) %s", "error in rxq-queue", @@ -1254,23 +1255,8 @@ virtio_pci_device_init (vlib_main_t * vm, virtio_if_t * vif, vif->num_rxqs++; } - if (i >= vtm->n_vlib_mains) - { - /* - * There is 1:1 mapping between tx queue and vpp worker thread. - * tx queue 0 is bind with thread index 0, tx queue 1 on thread - * index 1 and so on. - * Multiple worker threads can poll same tx queue when number of - * workers are more than tx queues. In this case, 1:N mapping - * between tx queue and vpp worker thread. - */ - virtio_log_debug (vif, "%s %u, %s", "tx-queue: number", - TX_QUEUE (i), - "no VPP worker thread is available"); - continue; - } - - if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i)))) + if ((error = virtio_pci_vring_init (vm, vif, TX_QUEUE (i), + args->tx_queue_size))) { args->rv = VNET_API_ERROR_INIT_FAILED; virtio_log_error (vif, "%s (%u) %s", "error in txq-queue", @@ -1354,7 +1340,6 @@ virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args) clib_error_t *error = 0; u32 interrupt_count = 0; - /* *INDENT-OFF* */ pool_foreach (vif, vim->interfaces) { if (vif->pci_addr.as_u32 == args->addr) { @@ -1367,7 +1352,24 @@ virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args) return; } } - /* *INDENT-ON* */ + + if (args->bind) + { + vlib_pci_addr_t pci = { .as_u32 = args->addr }; + error = vlib_pci_bind_to_uio (vm, &pci, (char *) "auto", + VIRTIO_BIND_FORCE == args->bind); + if (error) + { + args->rv = VNET_API_ERROR_INVALID_INTERFACE; + args->error = + clib_error_return (error, "%U: %s", format_vlib_pci_addr, &pci, + "error encountered on binding pci device"); + vlib_log (VLIB_LOG_LEVEL_ERR, vim->log_default, "%U: %s", + format_vlib_pci_addr, &pci, + "error encountered on binding pci devicee"); + return; + } + } pool_get (vim->interfaces, vif); vif->dev_instance = vif - vim->interfaces; @@ -1483,25 +1485,18 @@ virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args) } /* create interface */ - error = ethernet_register_interface (vnm, virtio_device_class.index, - vif->dev_instance, vif->mac_addr, - &vif->hw_if_index, - virtio_pci_flag_change); - - if (error) - { - args->rv = VNET_API_ERROR_INVALID_REGISTRATION; - virtio_log_error (vif, - "error encountered on ethernet register interface"); - goto error; - } + vnet_eth_interface_registration_t eir = {}; + eir.dev_class_index = virtio_device_class.index; + eir.dev_instance = vif->dev_instance; + eir.address = vif->mac_addr; + eir.cb.flag_change = virtio_pci_flag_change; + vif->hw_if_index = vnet_eth_register_interface (vnm, &eir); vnet_sw_interface_t *sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index); vif->sw_if_index = sw->sw_if_index; args->sw_if_index = sw->sw_if_index; - vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index); - hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE; + vnet_hw_if_set_caps (vnm, vif->hw_if_index, VNET_HW_IF_CAP_INT_MODE); if (args->virtio_flags & VIRTIO_FLAG_BUFFERING) { @@ -1513,13 +1508,22 @@ virtio_pci_create_if (vlib_main_t * vm, virtio_pci_create_if_args_t * args) "error encountered during packet buffering init"); goto error; } + /* + * packet buffering flag needs to be set 1 before calling the + * virtio_pre_input_node_enable but after the successful initialization + * of buffering queues above. + * Packet buffering flag set to 0 if there will be any error during + * buffering initialization. + */ + vif->packet_buffering = 1; + virtio_pre_input_node_enable (vm, vif); } virtio_vring_set_rx_queues (vm, vif); + virtio_vring_set_tx_queues (vm, vif); if (virtio_pci_is_link_up (vm, vif) & VIRTIO_NET_S_LINK_UP) { - vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; vnet_hw_interface_set_flags (vnm, vif->hw_if_index, VNET_HW_INTERFACE_FLAG_LINK_UP); } @@ -1556,17 +1560,19 @@ virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif) vlib_pci_intr_disable (vm, vif->pci_dev_handle); - for (i = 0; i < vif->max_queue_pairs; i++) + if (vif->virtio_pci_func) { - vif->virtio_pci_func->del_queue (vm, vif, RX_QUEUE (i)); - vif->virtio_pci_func->del_queue (vm, vif, TX_QUEUE (i)); - } + for (i = 0; i < vif->max_queue_pairs; i++) + { + vif->virtio_pci_func->del_queue (vm, vif, RX_QUEUE (i)); + vif->virtio_pci_func->del_queue (vm, vif, TX_QUEUE (i)); + } - if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) - vif->virtio_pci_func->del_queue (vm, vif, vif->max_queue_pairs * 2); + if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) + vif->virtio_pci_func->del_queue (vm, vif, vif->max_queue_pairs * 2); - if (vif->virtio_pci_func) - vif->virtio_pci_func->device_reset (vm, vif); + vif->virtio_pci_func->device_reset (vm, vif); + } if (vif->hw_if_index) { @@ -1578,7 +1584,7 @@ virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif) vec_foreach_index (i, vif->rxq_vrings) { - virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i); + vnet_virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, i); if (vring->used) { virtio_free_buffers (vm, vring); @@ -1587,9 +1593,12 @@ virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * vif) vlib_physmem_free (vm, vring->desc); } + if (vif->packet_buffering) + virtio_pre_input_node_disable (vm, vif); + vec_foreach_index (i, vif->txq_vrings) { - virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i); + vnet_virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, i); if (vring->used) { virtio_free_buffers (vm, vring); diff --git a/src/vnet/devices/virtio/pci.h b/src/vnet/devices/virtio/pci.h index 70aa9833c2d..5eb80f823be 100644 --- a/src/vnet/devices/virtio/pci.h +++ b/src/vnet/devices/virtio/pci.h @@ -87,7 +87,7 @@ typedef enum #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12 -#define VIRTIO_PCI_VRING_ALIGN 4096 +#define VNET_VIRTIO_PCI_VRING_ALIGN 4096 typedef enum { @@ -154,13 +154,11 @@ typedef struct * and an ack/status response in the last entry. Data for the * command goes in between. */ -/* *INDENT-OFF* */ typedef CLIB_PACKED (struct { u8 class; u8 cmd; }) virtio_net_ctrl_hdr_t; -/* *INDENT-ON* */ typedef u8 virtio_net_ctrl_ack_t; @@ -192,8 +190,8 @@ typedef struct _virtio_pci_func u16 (*get_queue_size) (vlib_main_t * vm, virtio_if_t * vif, u16 queue_id); void (*set_queue_size) (vlib_main_t * vm, virtio_if_t * vif, u16 queue_id, u16 queue_size); - u8 (*setup_queue) (vlib_main_t * vm, virtio_if_t * vif, u16 queue_id, - void *p); + u8 (*setup_queue) (vlib_main_t *vm, virtio_if_t *vif, u16 queue_id, + vnet_virtio_vring_t *vring); void (*del_queue) (vlib_main_t * vm, virtio_if_t * vif, u16 queue_id); u16 (*get_queue_notify_off) (vlib_main_t * vm, virtio_if_t * vif, u16 queue_id); @@ -227,6 +225,13 @@ typedef enum #undef _ } virtio_flag_t; +typedef enum +{ + VIRTIO_BIND_NONE = 0, + VIRTIO_BIND_DEFAULT = 1, + VIRTIO_BIND_FORCE = 2, +} __clib_packed virtio_bind_t; + typedef struct { u32 addr; @@ -238,6 +243,8 @@ typedef struct u64 features; u8 gso_enabled; u8 checksum_offload_enabled; + u32 tx_queue_size; + virtio_bind_t bind; u32 buffering_size; u32 virtio_flags; clib_error_t *error; diff --git a/src/vnet/devices/virtio/vhost_user.api b/src/vnet/devices/virtio/vhost_user.api deleted file mode 100644 index b026ba768a9..00000000000 --- a/src/vnet/devices/virtio/vhost_user.api +++ /dev/null @@ -1,201 +0,0 @@ -/* - * Copyright (c) 2015-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. - */ - -option version = "4.1.1"; - -import "vnet/interface_types.api"; -import "vnet/ethernet/ethernet_types.api"; -import "vnet/devices/virtio/virtio_types.api"; - -/** \brief vhost-user interface create request - @param client_index - opaque cookie to identify the sender - @param is_server - our side is socket server - @param sock_filename - unix socket filename, used to speak with frontend - @param use_custom_mac - enable or disable the use of the provided hardware address - @param disable_mrg_rxbuf - disable the use of merge receive buffers - @param disable_indirect_desc - disable the use of indirect descriptors which driver can use - @param enable_gso - enable gso support (default 0) - @param enable_packed - enable packed ring support (default 0) - @param mac_address - hardware address to use if 'use_custom_mac' is set -*/ -define create_vhost_user_if -{ - option deprecated; - u32 client_index; - u32 context; - bool is_server; - string sock_filename[256]; - bool renumber; - bool disable_mrg_rxbuf; - bool disable_indirect_desc; - bool enable_gso; - bool enable_packed; - u32 custom_dev_instance; - bool use_custom_mac; - vl_api_mac_address_t mac_address; - string tag[64]; -}; - -/** \brief vhost-user interface create response - @param context - sender context, to match reply w/ request - @param retval - return code for the request - @param sw_if_index - interface the operation is applied to -*/ -define create_vhost_user_if_reply -{ - option deprecated; - u32 context; - i32 retval; - vl_api_interface_index_t sw_if_index; -}; - -/** \brief vhost-user interface modify request - @param client_index - opaque cookie to identify the sender - @param is_server - our side is socket server - @param sock_filename - unix socket filename, used to speak with frontend - @param enable_gso - enable gso support (default 0) - @param enable_packed - enable packed ring support (default 0) -*/ -autoreply define modify_vhost_user_if -{ - option deprecated; - u32 client_index; - u32 context; - vl_api_interface_index_t sw_if_index; - bool is_server; - string sock_filename[256]; - bool renumber; - bool enable_gso; - bool enable_packed; - u32 custom_dev_instance; -}; - -/** \brief vhost-user interface create request - @param client_index - opaque cookie to identify the sender - @param is_server - our side is socket server - @param sock_filename - unix socket filename, used to speak with frontend - @param use_custom_mac - enable or disable the use of the provided hardware address - @param disable_mrg_rxbuf - disable the use of merge receive buffers - @param disable_indirect_desc - disable the use of indirect descriptors which driver can use - @param enable_gso - enable gso support (default 0) - @param enable_packed - enable packed ring support (default 0) - @param enable_event_idx - enable event_idx support (default 0) - @param mac_address - hardware address to use if 'use_custom_mac' is set - @param renumber - if true, use custom_dev_instance is valid - @param custom_dev_instance - custom device instance number -*/ -define create_vhost_user_if_v2 -{ - u32 client_index; - u32 context; - bool is_server; - string sock_filename[256]; - bool renumber; - bool disable_mrg_rxbuf; - bool disable_indirect_desc; - bool enable_gso; - bool enable_packed; - bool enable_event_idx; - u32 custom_dev_instance; - bool use_custom_mac; - vl_api_mac_address_t mac_address; - string tag[64]; -}; - -/** \brief vhost-user interface create response - @param context - sender context, to match reply w/ request - @param retval - return code for the request - @param sw_if_index - interface the operation is applied to -*/ -define create_vhost_user_if_v2_reply -{ - u32 context; - i32 retval; - vl_api_interface_index_t sw_if_index; -}; - -/** \brief vhost-user interface modify request - @param client_index - opaque cookie to identify the sender - @param is_server - our side is socket server - @param sock_filename - unix socket filename, used to speak with frontend - @param enable_gso - enable gso support (default 0) - @param enable_packed - enable packed ring support (default 0) - @param enable_event_idx - enable event idx support (default 0) - @param renumber - if true, use custom_dev_instance is valid - @param custom_dev_instance - custom device instance number -*/ -autoreply define modify_vhost_user_if_v2 -{ - u32 client_index; - u32 context; - vl_api_interface_index_t sw_if_index; - bool is_server; - string sock_filename[256]; - bool renumber; - bool enable_gso; - bool enable_packed; - bool enable_event_idx; - u32 custom_dev_instance; -}; - -/** \brief vhost-user interface delete request - @param client_index - opaque cookie to identify the sender -*/ -autoreply define delete_vhost_user_if -{ - u32 client_index; - u32 context; - vl_api_interface_index_t sw_if_index; -}; - -/** \brief Vhost-user interface details structure (fix this) - @param sw_if_index - index of the interface - @param interface_name - name of interface - @param virtio_net_hdr_sz - net header size - @param features_first_32 - interface features, first 32 bits - @param features_last_32 - interface features, last 32 bits - @param is_server - vhost-user server socket - @param sock_filename - socket filename - @param num_regions - number of used memory regions - @param sock_errno - socket errno -*/ -define sw_interface_vhost_user_details -{ - u32 context; - vl_api_interface_index_t sw_if_index; - string interface_name[64]; - u32 virtio_net_hdr_sz; - vl_api_virtio_net_features_first_32_t features_first_32; - vl_api_virtio_net_features_last_32_t features_last_32; - bool is_server; - string sock_filename[256]; - u32 num_regions; - i32 sock_errno; -}; - -/** \brief Vhost-user interface dump request - @param sw_if_index - filter by sw_if_index -*/ -define sw_interface_vhost_user_dump -{ - u32 client_index; - u32 context; - vl_api_interface_index_t sw_if_index [default=0xffffffff]; -}; -/* - * Local Variables: - * eval: (c-set-style "gnu") - * End: - */ diff --git a/src/vnet/devices/virtio/vhost_user.c b/src/vnet/devices/virtio/vhost_user.c deleted file mode 100644 index 089e08aabb4..00000000000 --- a/src/vnet/devices/virtio/vhost_user.c +++ /dev/null @@ -1,2609 +0,0 @@ -/* - *------------------------------------------------------------------ - * vhost.c - vhost-user - * - * Copyright (c) 2014-2018 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/un.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <sys/uio.h> /* for iovec */ -#include <netinet/in.h> -#include <sys/vfs.h> - -#include <linux/if_arp.h> -#include <linux/if_tun.h> - -#include <vlib/vlib.h> -#include <vlib/unix/unix.h> - -#include <vnet/ethernet/ethernet.h> -#include <vnet/devices/devices.h> -#include <vnet/feature/feature.h> -#include <vnet/interface/rx_queue_funcs.h> -#include <vnet/interface/tx_queue_funcs.h> - -#include <vnet/devices/virtio/vhost_user.h> -#include <vnet/devices/virtio/vhost_user_inline.h> - -/** - * @file - * @brief vHost User Device Driver. - * - * This file contains the source code for vHost User interface. - */ - - -vlib_node_registration_t vhost_user_send_interrupt_node; - -/* *INDENT-OFF* */ -vhost_user_main_t vhost_user_main = { - .mtu_bytes = 1518, -}; - -VNET_HW_INTERFACE_CLASS (vhost_interface_class, static) = { - .name = "vhost-user", -}; -/* *INDENT-ON* */ - -static long -get_huge_page_size (int fd) -{ - struct statfs s; - fstatfs (fd, &s); - return s.f_bsize; -} - -static void -unmap_all_mem_regions (vhost_user_intf_t * vui) -{ - int i, r, q; - vhost_user_vring_t *vq; - - for (i = 0; i < vui->nregions; i++) - { - if (vui->region_mmap_addr[i] != MAP_FAILED) - { - - long page_sz = get_huge_page_size (vui->region_mmap_fd[i]); - - ssize_t map_sz = (vui->regions[i].memory_size + - vui->regions[i].mmap_offset + - page_sz - 1) & ~(page_sz - 1); - - r = - munmap (vui->region_mmap_addr[i] - vui->regions[i].mmap_offset, - map_sz); - - vu_log_debug (vui, "unmap memory region %d addr 0x%lx len 0x%lx " - "page_sz 0x%x", i, vui->region_mmap_addr[i], map_sz, - page_sz); - - vui->region_mmap_addr[i] = MAP_FAILED; - - if (r == -1) - { - vu_log_err (vui, "failed to unmap memory region (errno %d)", - errno); - } - close (vui->region_mmap_fd[i]); - } - } - vui->nregions = 0; - - FOR_ALL_VHOST_RX_TXQ (q, vui) - { - vq = &vui->vrings[q]; - vq->avail = 0; - vq->used = 0; - vq->desc = 0; - } -} - -static_always_inline void -vhost_user_tx_thread_placement (vhost_user_intf_t *vui, u32 qid) -{ - vnet_main_t *vnm = vnet_get_main (); - vhost_user_vring_t *rxvq = &vui->vrings[qid]; - u32 q = qid >> 1, rxvq_count; - - ASSERT ((qid & 1) == 0); - if (!rxvq->started || !rxvq->enabled) - return; - - rxvq_count = (qid >> 1) + 1; - if (rxvq->queue_index == ~0) - { - rxvq->queue_index = - vnet_hw_if_register_tx_queue (vnm, vui->hw_if_index, q); - rxvq->qid = q; - } - - FOR_ALL_VHOST_RXQ (q, vui) - { - vhost_user_vring_t *rxvq = &vui->vrings[q]; - u32 qi = rxvq->queue_index; - - if (rxvq->queue_index == ~0) - break; - for (u32 i = 0; i < vlib_get_n_threads (); i++) - vnet_hw_if_tx_queue_unassign_thread (vnm, qi, i); - } - - for (u32 i = 0; i < vlib_get_n_threads (); i++) - { - vhost_user_vring_t *rxvq = - &vui->vrings[VHOST_VRING_IDX_RX (i % rxvq_count)]; - u32 qi = rxvq->queue_index; - - vnet_hw_if_tx_queue_assign_thread (vnm, qi, i); - } - - vnet_hw_if_update_runtime_data (vnm, vui->hw_if_index); -} - -/** - * @brief Unassign existing interface/queue to thread mappings and re-assign - * new interface/queue to thread mappings - */ -static_always_inline void -vhost_user_rx_thread_placement (vhost_user_intf_t * vui, u32 qid) -{ - vhost_user_vring_t *txvq = &vui->vrings[qid]; - vnet_main_t *vnm = vnet_get_main (); - int rv; - u32 q = qid >> 1; - vhost_user_main_t *vum = &vhost_user_main; - - ASSERT ((qid & 1) == 1); // should be odd - // Assign new queue mappings for the interface - if (txvq->queue_index != ~0) - return; - vnet_hw_if_set_input_node (vnm, vui->hw_if_index, - vhost_user_input_node.index); - txvq->queue_index = vnet_hw_if_register_rx_queue (vnm, vui->hw_if_index, q, - VNET_HW_IF_RXQ_THREAD_ANY); - txvq->thread_index = - vnet_hw_if_get_rx_queue_thread_index (vnm, txvq->queue_index); - - if (txvq->mode == VNET_HW_IF_RX_MODE_UNKNOWN) - /* Set polling as the default */ - txvq->mode = VNET_HW_IF_RX_MODE_POLLING; - if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING) - { - vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, txvq->thread_index); - /* Keep a polling queue count for each thread */ - cpu->polling_q_count++; - } - txvq->qid = q; - rv = vnet_hw_if_set_rx_queue_mode (vnm, txvq->queue_index, txvq->mode); - if (rv) - vu_log_warn (vui, "unable to set rx mode for interface %d, " - "queue %d: rc=%d", vui->hw_if_index, q, rv); - vnet_hw_if_update_runtime_data (vnm, vui->hw_if_index); -} - -/** @brief Returns whether at least one TX and one RX vring are enabled */ -static_always_inline int -vhost_user_intf_ready (vhost_user_intf_t * vui) -{ - int i, found[2] = { }; //RX + TX - - for (i = 0; i < vui->num_qid; i++) - if (vui->vrings[i].started && vui->vrings[i].enabled) - found[i & 1] = 1; - - return found[0] && found[1]; -} - -static_always_inline void -vhost_user_update_iface_state (vhost_user_intf_t * vui) -{ - /* if we have pointers to descriptor table, go up */ - int is_ready = vhost_user_intf_ready (vui); - if (is_ready != vui->is_ready) - { - vu_log_debug (vui, "interface %d %s", vui->sw_if_index, - is_ready ? "ready" : "down"); - if (vui->admin_up) - vnet_hw_interface_set_flags (vnet_get_main (), vui->hw_if_index, - is_ready ? VNET_HW_INTERFACE_FLAG_LINK_UP - : 0); - vui->is_ready = is_ready; - } -} - -static clib_error_t * -vhost_user_callfd_read_ready (clib_file_t * uf) -{ - __attribute__ ((unused)) int n; - u8 buff[8]; - - n = read (uf->file_descriptor, ((char *) &buff), 8); - - return 0; -} - -static_always_inline void -vhost_user_thread_placement (vhost_user_intf_t * vui, u32 qid) -{ - if (qid & 1) // RX is odd, TX is even - { - if (vui->vrings[qid].queue_index == ~0) - vhost_user_rx_thread_placement (vui, qid); - } - else - vhost_user_tx_thread_placement (vui, qid); -} - -static clib_error_t * -vhost_user_kickfd_read_ready (clib_file_t * uf) -{ - __attribute__ ((unused)) ssize_t n; - u8 buff[8]; - vhost_user_main_t *vum = &vhost_user_main; - vhost_user_intf_t *vui = - pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data >> 8); - u32 qid = uf->private_data & 0xff; - u32 is_txq = qid & 1; - vhost_user_vring_t *vq = &vui->vrings[qid]; - vnet_main_t *vnm = vnet_get_main (); - - n = read (uf->file_descriptor, buff, 8); - if (vq->started == 0) - { - vq->started = 1; - vhost_user_thread_placement (vui, qid); - vhost_user_update_iface_state (vui); - if (is_txq) - vnet_hw_if_set_rx_queue_file_index (vnm, vq->queue_index, - vq->kickfd_idx); - } - - if (is_txq && (vq->mode != VNET_HW_IF_RX_MODE_POLLING) && - vhost_user_intf_ready (vui)) - { - vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, vq->thread_index); - /* - * If the thread has more than 1 queue and the other queue is in polling - * mode, there is no need to trigger an interrupt - */ - if (cpu->polling_q_count == 0) - vnet_hw_if_rx_queue_set_int_pending (vnm, vq->queue_index); - } - - return 0; -} - -static_always_inline void -vhost_user_vring_init (vhost_user_intf_t * vui, u32 qid) -{ - vhost_user_vring_t *vring = &vui->vrings[qid]; - - clib_memset (vring, 0, sizeof (*vring)); - vring->kickfd_idx = ~0; - vring->callfd_idx = ~0; - vring->errfd = -1; - vring->qid = -1; - vring->queue_index = ~0; - vring->thread_index = ~0; - vring->mode = VNET_HW_IF_RX_MODE_POLLING; - - clib_spinlock_init (&vring->vring_lock); - - /* - * We have a bug with some qemu 2.5, and this may be a fix. - * Feel like interpretation holy text, but this is from vhost-user.txt. - * " - * One queue pair is enabled initially. More queues are enabled - * dynamically, by sending message VHOST_USER_SET_VRING_ENABLE. - * " - * Don't know who's right, but this is what DPDK does. - */ - if (qid == 0 || qid == 1) - vring->enabled = 1; -} - -static_always_inline void -vhost_user_vring_close (vhost_user_intf_t * vui, u32 qid) -{ - vhost_user_vring_t *vring = &vui->vrings[qid]; - - if (vring->kickfd_idx != ~0) - { - clib_file_t *uf = pool_elt_at_index (file_main.file_pool, - vring->kickfd_idx); - clib_file_del (&file_main, uf); - vring->kickfd_idx = ~0; - } - if (vring->callfd_idx != ~0) - { - clib_file_t *uf = pool_elt_at_index (file_main.file_pool, - vring->callfd_idx); - clib_file_del (&file_main, uf); - vring->callfd_idx = ~0; - } - if (vring->errfd != -1) - { - close (vring->errfd); - vring->errfd = -1; - } - - clib_spinlock_free (&vring->vring_lock); - - // save the needed information in vrings prior to being wiped out - u16 q = vui->vrings[qid].qid; - u32 queue_index = vui->vrings[qid].queue_index; - u32 mode = vui->vrings[qid].mode; - u32 thread_index = vui->vrings[qid].thread_index; - vhost_user_vring_init (vui, qid); - vui->vrings[qid].qid = q; - vui->vrings[qid].queue_index = queue_index; - vui->vrings[qid].mode = mode; - vui->vrings[qid].thread_index = thread_index; -} - -static_always_inline void -vhost_user_if_disconnect (vhost_user_intf_t * vui) -{ - vnet_main_t *vnm = vnet_get_main (); - int q; - - vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0); - - if (vui->clib_file_index != ~0) - { - clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index); - vui->clib_file_index = ~0; - } - - vui->is_ready = 0; - - FOR_ALL_VHOST_RX_TXQ (q, vui) { vhost_user_vring_close (vui, q); } - - unmap_all_mem_regions (vui); - vu_log_debug (vui, "interface ifindex %d disconnected", vui->sw_if_index); -} - -void -vhost_user_set_operation_mode (vhost_user_intf_t *vui, - vhost_user_vring_t *txvq) -{ - if (vhost_user_is_packed_ring_supported (vui)) - { - if (txvq->used_event) - { - if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING) - txvq->used_event->flags = VRING_EVENT_F_DISABLE; - else - txvq->used_event->flags = 0; - } - } - else - { - if (txvq->used) - { - if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING) - txvq->used->flags = VRING_USED_F_NO_NOTIFY; - else - txvq->used->flags = 0; - } - } -} - -static clib_error_t * -vhost_user_socket_read (clib_file_t * uf) -{ - int n, i, j; - int fd, number_of_fds = 0; - int fds[VHOST_MEMORY_MAX_NREGIONS]; - vhost_user_msg_t msg; - struct msghdr mh; - struct iovec iov[1]; - vhost_user_main_t *vum = &vhost_user_main; - vhost_user_intf_t *vui; - struct cmsghdr *cmsg; - u8 q; - clib_file_t template = { 0 }; - vnet_main_t *vnm = vnet_get_main (); - vlib_main_t *vm = vlib_get_main (); - - vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data); - - char control[CMSG_SPACE (VHOST_MEMORY_MAX_NREGIONS * sizeof (int))]; - - clib_memset (&mh, 0, sizeof (mh)); - clib_memset (control, 0, sizeof (control)); - - for (i = 0; i < VHOST_MEMORY_MAX_NREGIONS; i++) - fds[i] = -1; - - /* set the payload */ - iov[0].iov_base = (void *) &msg; - iov[0].iov_len = VHOST_USER_MSG_HDR_SZ; - - mh.msg_iov = iov; - mh.msg_iovlen = 1; - mh.msg_control = control; - mh.msg_controllen = sizeof (control); - - n = recvmsg (uf->file_descriptor, &mh, 0); - - if (n != VHOST_USER_MSG_HDR_SZ) - { - if (n == -1) - { - vu_log_debug (vui, "recvmsg returned error %d %s", errno, - strerror (errno)); - } - else - { - vu_log_debug (vui, "n (%d) != VHOST_USER_MSG_HDR_SZ (%d)", - n, VHOST_USER_MSG_HDR_SZ); - } - goto close_socket; - } - - if (mh.msg_flags & MSG_CTRUNC) - { - vu_log_debug (vui, "MSG_CTRUNC is set"); - goto close_socket; - } - - cmsg = CMSG_FIRSTHDR (&mh); - - if (cmsg && (cmsg->cmsg_len > 0) && (cmsg->cmsg_level == SOL_SOCKET) && - (cmsg->cmsg_type == SCM_RIGHTS) && - (cmsg->cmsg_len - CMSG_LEN (0) <= - VHOST_MEMORY_MAX_NREGIONS * sizeof (int))) - { - number_of_fds = (cmsg->cmsg_len - CMSG_LEN (0)) / sizeof (int); - clib_memcpy_fast (fds, CMSG_DATA (cmsg), number_of_fds * sizeof (int)); - } - - /* version 1, no reply bit set */ - if ((msg.flags & 7) != 1) - { - vu_log_debug (vui, "malformed message received. closing socket"); - goto close_socket; - } - - { - int rv; - rv = - read (uf->file_descriptor, ((char *) &msg) + VHOST_USER_MSG_HDR_SZ, - msg.size); - if (rv < 0) - { - vu_log_debug (vui, "read failed %s", strerror (errno)); - goto close_socket; - } - else if (rv != msg.size) - { - vu_log_debug (vui, "message too short (read %dB should be %dB)", rv, - msg.size); - goto close_socket; - } - } - - switch (msg.request) - { - case VHOST_USER_GET_FEATURES: - msg.flags |= 4; - msg.u64 = VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) | - VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ) | - VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT) | - VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC) | - VIRTIO_FEATURE (VHOST_F_LOG_ALL) | - VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_ANNOUNCE) | - VIRTIO_FEATURE (VIRTIO_NET_F_MQ) | - VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES) | - VIRTIO_FEATURE (VIRTIO_F_VERSION_1); - msg.u64 &= vui->feature_mask; - - if (vui->enable_event_idx) - msg.u64 |= VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX); - if (vui->enable_gso) - msg.u64 |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS; - if (vui->enable_packed) - msg.u64 |= VIRTIO_FEATURE (VIRTIO_F_RING_PACKED); - - msg.size = sizeof (msg.u64); - vu_log_debug (vui, "if %d msg VHOST_USER_GET_FEATURES - reply " - "0x%016llx", vui->hw_if_index, msg.u64); - n = - send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0); - if (n != (msg.size + VHOST_USER_MSG_HDR_SZ)) - { - vu_log_debug (vui, "could not send message response"); - goto close_socket; - } - break; - - case VHOST_USER_SET_FEATURES: - vu_log_debug (vui, "if %d msg VHOST_USER_SET_FEATURES features " - "0x%016llx", vui->hw_if_index, msg.u64); - - vui->features = msg.u64; - - if (vui->features & - (VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) | - VIRTIO_FEATURE (VIRTIO_F_VERSION_1))) - vui->virtio_net_hdr_sz = 12; - else - vui->virtio_net_hdr_sz = 10; - - vui->is_any_layout = - (vui->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)) ? 1 : 0; - - ASSERT (vui->virtio_net_hdr_sz < VLIB_BUFFER_PRE_DATA_SIZE); - vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vui->hw_if_index); - if (vui->enable_gso && - ((vui->features & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS) - == FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS)) - { - hw->caps |= (VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO | - VNET_HW_INTERFACE_CAP_SUPPORTS_TX_TCP_CKSUM | - VNET_HW_INTERFACE_CAP_SUPPORTS_TX_UDP_CKSUM); - } - else - { - hw->caps &= ~(VNET_HW_INTERFACE_CAP_SUPPORTS_TCP_GSO | - VNET_HW_INTERFACE_CAP_SUPPORTS_L4_TX_CKSUM); - } - vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0); - vui->is_ready = 0; - vhost_user_update_iface_state (vui); - break; - - case VHOST_USER_SET_MEM_TABLE: - vu_log_debug (vui, "if %d msg VHOST_USER_SET_MEM_TABLE nregions %d", - vui->hw_if_index, msg.memory.nregions); - - if ((msg.memory.nregions < 1) || - (msg.memory.nregions > VHOST_MEMORY_MAX_NREGIONS)) - { - vu_log_debug (vui, "number of mem regions must be between 1 and %i", - VHOST_MEMORY_MAX_NREGIONS); - goto close_socket; - } - - if (msg.memory.nregions != number_of_fds) - { - vu_log_debug (vui, "each memory region must have FD"); - goto close_socket; - } - - /* Do the mmap without barrier sync */ - void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS]; - for (i = 0; i < msg.memory.nregions; i++) - { - long page_sz = get_huge_page_size (fds[i]); - - /* align size to page */ - ssize_t map_sz = (msg.memory.regions[i].memory_size + - msg.memory.regions[i].mmap_offset + - page_sz - 1) & ~(page_sz - 1); - - region_mmap_addr[i] = mmap (0, map_sz, PROT_READ | PROT_WRITE, - MAP_SHARED, fds[i], 0); - if (region_mmap_addr[i] == MAP_FAILED) - { - vu_log_err (vui, "failed to map memory. errno is %d", errno); - for (j = 0; j < i; j++) - munmap (region_mmap_addr[j], map_sz); - goto close_socket; - } - vu_log_debug (vui, "map memory region %d addr 0 len 0x%lx fd %d " - "mapped 0x%lx page_sz 0x%x", i, map_sz, fds[i], - region_mmap_addr[i], page_sz); - } - - vlib_worker_thread_barrier_sync (vm); - unmap_all_mem_regions (vui); - for (i = 0; i < msg.memory.nregions; i++) - { - clib_memcpy_fast (&(vui->regions[i]), &msg.memory.regions[i], - sizeof (vhost_user_memory_region_t)); - - vui->region_mmap_addr[i] = region_mmap_addr[i]; - vui->region_guest_addr_lo[i] = vui->regions[i].guest_phys_addr; - vui->region_guest_addr_hi[i] = vui->regions[i].guest_phys_addr + - vui->regions[i].memory_size; - - vui->region_mmap_addr[i] += vui->regions[i].mmap_offset; - vui->region_mmap_fd[i] = fds[i]; - - vui->nregions++; - } - - /* - * Re-compute desc, used, and avail descriptor table if vring address - * is set. - */ - FOR_ALL_VHOST_RX_TXQ (q, vui) - { - if (vui->vrings[q].desc_user_addr && vui->vrings[q].used_user_addr && - vui->vrings[q].avail_user_addr) - { - vui->vrings[q].desc = - map_user_mem (vui, vui->vrings[q].desc_user_addr); - vui->vrings[q].used = - map_user_mem (vui, vui->vrings[q].used_user_addr); - vui->vrings[q].avail = - map_user_mem (vui, vui->vrings[q].avail_user_addr); - } - } - vlib_worker_thread_barrier_release (vm); - break; - - case VHOST_USER_SET_VRING_NUM: - vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_NUM idx %d num %d", - vui->hw_if_index, msg.state.index, msg.state.num); - - if ((msg.state.num > 32768) || /* maximum ring size is 32768 */ - (msg.state.num == 0) || /* it cannot be zero */ - ((msg.state.num - 1) & msg.state.num) || /* must be power of 2 */ - (msg.state.index >= vui->num_qid)) - { - vu_log_debug (vui, "invalid VHOST_USER_SET_VRING_NUM: msg.state.num" - " %d, msg.state.index %d, curruent max q %d", - msg.state.num, msg.state.index, vui->num_qid); - goto close_socket; - } - vui->vrings[msg.state.index].qsz_mask = msg.state.num - 1; - break; - - case VHOST_USER_SET_VRING_ADDR: - vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ADDR idx %d", - vui->hw_if_index, msg.state.index); - - if (msg.state.index >= vui->num_qid) - { - vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:" - " %u >= %u", msg.state.index, vui->num_qid); - goto close_socket; - } - - if (msg.size < sizeof (msg.addr)) - { - vu_log_debug (vui, "vhost message is too short (%d < %d)", - msg.size, sizeof (msg.addr)); - goto close_socket; - } - - vring_desc_t *desc = map_user_mem (vui, msg.addr.desc_user_addr); - vring_used_t *used = map_user_mem (vui, msg.addr.used_user_addr); - vring_avail_t *avail = map_user_mem (vui, msg.addr.avail_user_addr); - - if ((desc == NULL) || (used == NULL) || (avail == NULL)) - { - vu_log_debug (vui, "failed to map user memory for hw_if_index %d", - vui->hw_if_index); - goto close_socket; - } - - vui->vrings[msg.state.index].desc_user_addr = msg.addr.desc_user_addr; - vui->vrings[msg.state.index].used_user_addr = msg.addr.used_user_addr; - vui->vrings[msg.state.index].avail_user_addr = msg.addr.avail_user_addr; - - vlib_worker_thread_barrier_sync (vm); - vui->vrings[msg.state.index].desc = desc; - vui->vrings[msg.state.index].used = used; - vui->vrings[msg.state.index].avail = avail; - - vui->vrings[msg.state.index].log_guest_addr = msg.addr.log_guest_addr; - vui->vrings[msg.state.index].log_used = - (msg.addr.flags & (1 << VHOST_VRING_F_LOG)) ? 1 : 0; - - /* Spec says: If VHOST_USER_F_PROTOCOL_FEATURES has not been negotiated, - the ring is initialized in an enabled state. */ - if (!(vui->features & VIRTIO_FEATURE (VHOST_USER_F_PROTOCOL_FEATURES))) - vui->vrings[msg.state.index].enabled = 1; - - vui->vrings[msg.state.index].last_used_idx = - vui->vrings[msg.state.index].last_avail_idx = - vui->vrings[msg.state.index].used->idx; - vui->vrings[msg.state.index].last_kick = - vui->vrings[msg.state.index].last_used_idx; - - /* tell driver that we want interrupts or not */ - vhost_user_set_operation_mode (vui, &vui->vrings[msg.state.index]); - vlib_worker_thread_barrier_release (vm); - vhost_user_update_iface_state (vui); - break; - - case VHOST_USER_SET_OWNER: - vu_log_debug (vui, "if %d msg VHOST_USER_SET_OWNER", vui->hw_if_index); - break; - - case VHOST_USER_RESET_OWNER: - vu_log_debug (vui, "if %d msg VHOST_USER_RESET_OWNER", - vui->hw_if_index); - break; - - case VHOST_USER_SET_VRING_CALL: - vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_CALL %d", - vui->hw_if_index, msg.u64); - - q = (u8) (msg.u64 & 0xFF); - if (vui->num_qid > q) - { - /* if there is old fd, delete and close it */ - if (vui->vrings[q].callfd_idx != ~0) - { - clib_file_t *uf = pool_elt_at_index (file_main.file_pool, - vui->vrings[q].callfd_idx); - clib_file_del (&file_main, uf); - vui->vrings[q].callfd_idx = ~0; - } - } - else if (vec_len (vui->vrings) > q) - { - /* grow vrings by pair (RX + TX) */ - vui->num_qid = (q & 1) ? (q + 1) : (q + 2); - } - else - { - u32 i, new_max_q, old_max_q = vec_len (vui->vrings); - - /* - * Double the array size if it is less than 64 entries. - * Slow down thereafter. - */ - if (vec_len (vui->vrings) < (VHOST_VRING_INIT_MQ_PAIR_SZ << 3)) - new_max_q = vec_len (vui->vrings) << 1; - else - new_max_q = vec_len (vui->vrings) + - (VHOST_VRING_INIT_MQ_PAIR_SZ << 2); - if (new_max_q > (VHOST_VRING_MAX_MQ_PAIR_SZ << 1)) - new_max_q = (VHOST_VRING_MAX_MQ_PAIR_SZ << 1); - - /* sync with the worker threads, vrings may move due to realloc */ - vlib_worker_thread_barrier_sync (vm); - vec_validate_aligned (vui->vrings, new_max_q - 1, - CLIB_CACHE_LINE_BYTES); - vlib_worker_thread_barrier_release (vm); - - for (i = old_max_q; i < vec_len (vui->vrings); i++) - vhost_user_vring_init (vui, i); - - /* grow vrings by pair (RX + TX) */ - vui->num_qid = (q & 1) ? (q + 1) : (q + 2); - } - - if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK)) - { - if (number_of_fds != 1) - { - vu_log_debug (vui, "More than one fd received !"); - goto close_socket; - } - - template.read_function = vhost_user_callfd_read_ready; - template.file_descriptor = fds[0]; - template.private_data = - ((vui - vhost_user_main.vhost_user_interfaces) << 8) + q; - template.description = format (0, "vhost user"); - vui->vrings[q].callfd_idx = clib_file_add (&file_main, &template); - } - else - vui->vrings[q].callfd_idx = ~0; - break; - - case VHOST_USER_SET_VRING_KICK: - vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_KICK %d", - vui->hw_if_index, msg.u64); - - q = (u8) (msg.u64 & 0xFF); - if (q >= vui->num_qid) - { - vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_KICK:" - " %u >= %u", q, vui->num_qid); - goto close_socket; - } - - if (vui->vrings[q].kickfd_idx != ~0) - { - clib_file_t *uf = pool_elt_at_index (file_main.file_pool, - vui->vrings[q].kickfd_idx); - clib_file_del (&file_main, uf); - vui->vrings[q].kickfd_idx = ~0; - } - - if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK)) - { - if (number_of_fds != 1) - { - vu_log_debug (vui, "More than one fd received !"); - goto close_socket; - } - - template.read_function = vhost_user_kickfd_read_ready; - template.file_descriptor = fds[0]; - template.private_data = - (((uword) (vui - vhost_user_main.vhost_user_interfaces)) << 8) + - q; - vui->vrings[q].kickfd_idx = clib_file_add (&file_main, &template); - } - else - { - //When no kickfd is set, the queue is initialized as started - vui->vrings[q].kickfd_idx = ~0; - vui->vrings[q].started = 1; - vhost_user_thread_placement (vui, q); - } - vhost_user_update_iface_state (vui); - break; - - case VHOST_USER_SET_VRING_ERR: - vu_log_debug (vui, "if %d msg VHOST_USER_SET_VRING_ERR %d", - vui->hw_if_index, msg.u64); - - q = (u8) (msg.u64 & 0xFF); - if (q >= vui->num_qid) - { - vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ERR:" - " %u >= %u", q, vui->num_qid); - goto close_socket; - } - - if (vui->vrings[q].errfd != -1) - close (vui->vrings[q].errfd); - - if (!(msg.u64 & VHOST_USER_VRING_NOFD_MASK)) - { - if (number_of_fds != 1) - goto close_socket; - - vui->vrings[q].errfd = fds[0]; - } - else - vui->vrings[q].errfd = -1; - break; - - case VHOST_USER_SET_VRING_BASE: - vu_log_debug (vui, - "if %d msg VHOST_USER_SET_VRING_BASE idx %d num 0x%x", - vui->hw_if_index, msg.state.index, msg.state.num); - if (msg.state.index >= vui->num_qid) - { - vu_log_debug (vui, "invalid vring index VHOST_USER_SET_VRING_ADDR:" - " %u >= %u", msg.state.index, vui->num_qid); - goto close_socket; - } - vlib_worker_thread_barrier_sync (vm); - vui->vrings[msg.state.index].last_avail_idx = msg.state.num; - if (vhost_user_is_packed_ring_supported (vui)) - { - /* - * 0 1 2 3 - * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * | last avail idx | | last used idx | | - * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - * ^ ^ - * | | - * avail wrap counter used wrap counter - */ - /* last avail idx at bit 0-14. */ - vui->vrings[msg.state.index].last_avail_idx = - msg.state.num & 0x7fff; - /* avail wrap counter at bit 15 */ - vui->vrings[msg.state.index].avail_wrap_counter = - ! !(msg.state.num & (1 << 15)); - - /* - * Although last_used_idx is passed in the upper 16 bits in qemu - * implementation, in practice, last_avail_idx and last_used_idx are - * usually the same. As a result, DPDK does not bother to pass us - * last_used_idx. The spec is not clear on thex coding. I figured it - * out by reading the qemu code. So let's just read last_avail_idx - * and set last_used_idx equals to last_avail_idx. - */ - vui->vrings[msg.state.index].last_used_idx = - vui->vrings[msg.state.index].last_avail_idx; - vui->vrings[msg.state.index].last_kick = - vui->vrings[msg.state.index].last_used_idx; - vui->vrings[msg.state.index].used_wrap_counter = - vui->vrings[msg.state.index].avail_wrap_counter; - - if (vui->vrings[msg.state.index].avail_wrap_counter == 1) - vui->vrings[msg.state.index].avail_wrap_counter = - VRING_DESC_F_AVAIL; - } - vlib_worker_thread_barrier_release (vm); - break; - - case VHOST_USER_GET_VRING_BASE: - if (msg.state.index >= vui->num_qid) - { - vu_log_debug (vui, "invalid vring index VHOST_USER_GET_VRING_BASE:" - " %u >= %u", msg.state.index, vui->num_qid); - goto close_socket; - } - - /* protection is needed to prevent rx/tx from changing last_avail_idx */ - vlib_worker_thread_barrier_sync (vm); - /* - * Copy last_avail_idx from the vring before closing it because - * closing the vring also initializes the vring last_avail_idx - */ - msg.state.num = vui->vrings[msg.state.index].last_avail_idx; - if (vhost_user_is_packed_ring_supported (vui)) - { - msg.state.num = - (vui->vrings[msg.state.index].last_avail_idx & 0x7fff) | - (! !vui->vrings[msg.state.index].avail_wrap_counter << 15); - msg.state.num |= - ((vui->vrings[msg.state.index].last_used_idx & 0x7fff) | - (! !vui->vrings[msg.state.index].used_wrap_counter << 15)) << 16; - } - msg.flags |= 4; - msg.size = sizeof (msg.state); - - /* - * Spec says: Client must [...] stop ring upon receiving - * VHOST_USER_GET_VRING_BASE - */ - vhost_user_vring_close (vui, msg.state.index); - vlib_worker_thread_barrier_release (vm); - vu_log_debug (vui, - "if %d msg VHOST_USER_GET_VRING_BASE idx %d num 0x%x", - vui->hw_if_index, msg.state.index, msg.state.num); - n = - send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0); - if (n != (msg.size + VHOST_USER_MSG_HDR_SZ)) - { - vu_log_debug (vui, "could not send message response"); - goto close_socket; - } - vhost_user_update_iface_state (vui); - break; - - case VHOST_USER_NONE: - vu_log_debug (vui, "if %d msg VHOST_USER_NONE", vui->hw_if_index); - break; - - case VHOST_USER_SET_LOG_BASE: - vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_BASE", - vui->hw_if_index); - - if (msg.size != sizeof (msg.log)) - { - vu_log_debug (vui, "invalid msg size for VHOST_USER_SET_LOG_BASE:" - " %d instead of %d", msg.size, sizeof (msg.log)); - goto close_socket; - } - - if (!(vui->protocol_features & (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD))) - { - vu_log_debug (vui, "VHOST_USER_PROTOCOL_F_LOG_SHMFD not set but " - "VHOST_USER_SET_LOG_BASE received"); - goto close_socket; - } - - fd = fds[0]; - /* align size to page */ - long page_sz = get_huge_page_size (fd); - ssize_t map_sz = - (msg.log.size + msg.log.offset + page_sz - 1) & ~(page_sz - 1); - - void *log_base_addr = mmap (0, map_sz, PROT_READ | PROT_WRITE, - MAP_SHARED, fd, 0); - - vu_log_debug (vui, "map log region addr 0 len 0x%lx off 0x%lx fd %d " - "mapped 0x%lx", map_sz, msg.log.offset, fd, - log_base_addr); - - if (log_base_addr == MAP_FAILED) - { - vu_log_err (vui, "failed to map memory. errno is %d", errno); - goto close_socket; - } - - vlib_worker_thread_barrier_sync (vm); - vui->log_base_addr = log_base_addr; - vui->log_base_addr += msg.log.offset; - vui->log_size = msg.log.size; - vlib_worker_thread_barrier_release (vm); - - msg.flags |= 4; - msg.size = sizeof (msg.u64); - n = - send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0); - if (n != (msg.size + VHOST_USER_MSG_HDR_SZ)) - { - vu_log_debug (vui, "could not send message response"); - goto close_socket; - } - break; - - case VHOST_USER_SET_LOG_FD: - vu_log_debug (vui, "if %d msg VHOST_USER_SET_LOG_FD", vui->hw_if_index); - break; - - case VHOST_USER_GET_PROTOCOL_FEATURES: - msg.flags |= 4; - msg.u64 = (1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD) | - (1 << VHOST_USER_PROTOCOL_F_MQ); - msg.size = sizeof (msg.u64); - vu_log_debug (vui, "if %d msg VHOST_USER_GET_PROTOCOL_FEATURES - " - "reply 0x%016llx", vui->hw_if_index, msg.u64); - n = - send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0); - if (n != (msg.size + VHOST_USER_MSG_HDR_SZ)) - { - vu_log_debug (vui, "could not send message response"); - goto close_socket; - } - break; - - case VHOST_USER_SET_PROTOCOL_FEATURES: - vu_log_debug (vui, "if %d msg VHOST_USER_SET_PROTOCOL_FEATURES " - "features 0x%016llx", vui->hw_if_index, msg.u64); - vui->protocol_features = msg.u64; - break; - - case VHOST_USER_GET_QUEUE_NUM: - msg.flags |= 4; - msg.u64 = VHOST_VRING_MAX_MQ_PAIR_SZ; - msg.size = sizeof (msg.u64); - vu_log_debug (vui, "if %d msg VHOST_USER_GET_QUEUE_NUM - reply %d", - vui->hw_if_index, msg.u64); - n = - send (uf->file_descriptor, &msg, VHOST_USER_MSG_HDR_SZ + msg.size, 0); - if (n != (msg.size + VHOST_USER_MSG_HDR_SZ)) - { - vu_log_debug (vui, "could not send message response"); - goto close_socket; - } - break; - - case VHOST_USER_SET_VRING_ENABLE: - vu_log_debug (vui, "if %d VHOST_USER_SET_VRING_ENABLE: %s queue %d", - vui->hw_if_index, msg.state.num ? "enable" : "disable", - msg.state.index); - if (msg.state.index >= vui->num_qid) - { - vu_log_debug (vui, "invalid vring idx VHOST_USER_SET_VRING_ENABLE:" - " %u >= %u", msg.state.index, vui->num_qid); - goto close_socket; - } - - vui->vrings[msg.state.index].enabled = msg.state.num; - vhost_user_thread_placement (vui, msg.state.index); - vhost_user_update_iface_state (vui); - break; - - default: - vu_log_debug (vui, "unknown vhost-user message %d received. " - "closing socket", msg.request); - goto close_socket; - } - - return 0; - -close_socket: - vlib_worker_thread_barrier_sync (vm); - vhost_user_if_disconnect (vui); - vlib_worker_thread_barrier_release (vm); - vhost_user_update_iface_state (vui); - return 0; -} - -static clib_error_t * -vhost_user_socket_error (clib_file_t * uf) -{ - vlib_main_t *vm = vlib_get_main (); - vhost_user_main_t *vum = &vhost_user_main; - vhost_user_intf_t *vui = - pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data); - - vu_log_debug (vui, "socket error on if %d", vui->sw_if_index); - vlib_worker_thread_barrier_sync (vm); - vhost_user_if_disconnect (vui); - vlib_worker_thread_barrier_release (vm); - return 0; -} - -static clib_error_t * -vhost_user_socksvr_accept_ready (clib_file_t * uf) -{ - int client_fd, client_len; - struct sockaddr_un client; - clib_file_t template = { 0 }; - vhost_user_main_t *vum = &vhost_user_main; - vhost_user_intf_t *vui; - - vui = pool_elt_at_index (vum->vhost_user_interfaces, uf->private_data); - - client_len = sizeof (client); - client_fd = accept (uf->file_descriptor, - (struct sockaddr *) &client, - (socklen_t *) & client_len); - - if (client_fd < 0) - return clib_error_return_unix (0, "accept"); - - if (vui->clib_file_index != ~0) - { - vu_log_debug (vui, "Close client socket for vhost interface %d, fd %d", - vui->sw_if_index, UNIX_GET_FD (vui->clib_file_index)); - clib_file_del (&file_main, file_main.file_pool + vui->clib_file_index); - } - - vu_log_debug (vui, "New client socket for vhost interface %d, fd %d", - vui->sw_if_index, client_fd); - template.read_function = vhost_user_socket_read; - template.error_function = vhost_user_socket_error; - template.file_descriptor = client_fd; - template.private_data = vui - vhost_user_main.vhost_user_interfaces; - template.description = format (0, "vhost interface %d", vui->sw_if_index); - vui->clib_file_index = clib_file_add (&file_main, &template); - vui->num_qid = 2; - return 0; -} - -static clib_error_t * -vhost_user_init (vlib_main_t * vm) -{ - vhost_user_main_t *vum = &vhost_user_main; - vlib_thread_main_t *tm = vlib_get_thread_main (); - - vum->log_default = vlib_log_register_class ("vhost-user", 0); - - vum->coalesce_frames = 32; - vum->coalesce_time = 1e-3; - - vec_validate (vum->cpus, tm->n_vlib_mains - 1); - - vhost_cpu_t *cpu; - vec_foreach (cpu, vum->cpus) - { - /* This is actually not necessary as validate already zeroes it - * Just keeping the loop here for later because I am lazy. */ - cpu->rx_buffers_len = 0; - } - - vum->random = random_default_seed (); - - mhash_init_c_string (&vum->if_index_by_sock_name, sizeof (uword)); - - return 0; -} - -/* *INDENT-OFF* */ -VLIB_INIT_FUNCTION (vhost_user_init) = -{ - .runs_after = VLIB_INITS("ip4_init"), -}; -/* *INDENT-ON* */ - -static uword -vhost_user_send_interrupt_process (vlib_main_t * vm, - vlib_node_runtime_t * rt, vlib_frame_t * f) -{ - vhost_user_intf_t *vui; - f64 timeout = 3153600000.0 /* 100 years */ ; - uword event_type, *event_data = 0; - vhost_user_main_t *vum = &vhost_user_main; - u16 qid; - f64 now, poll_time_remaining; - f64 next_timeout; - u8 stop_timer = 0; - - while (1) - { - poll_time_remaining = - vlib_process_wait_for_event_or_clock (vm, timeout); - event_type = vlib_process_get_events (vm, &event_data); - vec_reset_length (event_data); - - /* - * Use the remaining timeout if it is less than coalesce time to avoid - * resetting the existing timer in the middle of expiration - */ - timeout = poll_time_remaining; - if (vlib_process_suspend_time_is_zero (timeout) || - (timeout > vum->coalesce_time)) - timeout = vum->coalesce_time; - - now = vlib_time_now (vm); - switch (event_type) - { - case VHOST_USER_EVENT_STOP_TIMER: - stop_timer = 1; - break; - - case VHOST_USER_EVENT_START_TIMER: - stop_timer = 0; - timeout = 1e-3; - if (!vlib_process_suspend_time_is_zero (poll_time_remaining)) - break; - /* fall through */ - - case ~0: - /* *INDENT-OFF* */ - pool_foreach (vui, vum->vhost_user_interfaces) { - next_timeout = timeout; - FOR_ALL_VHOST_RX_TXQ (qid, vui) - { - vhost_user_vring_t *vq = &vui->vrings[qid]; - - if (vq->started == 0) - continue; - if (vq->n_since_last_int) - { - if (now >= vq->int_deadline) - vhost_user_send_call (vm, vui, vq); - else - next_timeout = vq->int_deadline - now; - } - - if ((next_timeout < timeout) && (next_timeout > 0.0)) - timeout = next_timeout; - } - } - /* *INDENT-ON* */ - break; - - default: - clib_warning ("BUG: unhandled event type %d", event_type); - break; - } - /* No less than 1 millisecond */ - if (timeout < 1e-3) - timeout = 1e-3; - if (stop_timer) - timeout = 3153600000.0; - } - return 0; -} - -/* *INDENT-OFF* */ -VLIB_REGISTER_NODE (vhost_user_send_interrupt_node) = { - .function = vhost_user_send_interrupt_process, - .type = VLIB_NODE_TYPE_PROCESS, - .name = "vhost-user-send-interrupt-process", -}; -/* *INDENT-ON* */ - -static uword -vhost_user_process (vlib_main_t * vm, - vlib_node_runtime_t * rt, vlib_frame_t * f) -{ - vhost_user_main_t *vum = &vhost_user_main; - vhost_user_intf_t *vui; - struct sockaddr_un sun; - int sockfd; - clib_file_t template = { 0 }; - f64 timeout = 3153600000.0 /* 100 years */ ; - uword *event_data = 0; - - sockfd = -1; - sun.sun_family = AF_UNIX; - template.read_function = vhost_user_socket_read; - template.error_function = vhost_user_socket_error; - - while (1) - { - vlib_process_wait_for_event_or_clock (vm, timeout); - vlib_process_get_events (vm, &event_data); - vec_reset_length (event_data); - - timeout = 3.0; - - /* *INDENT-OFF* */ - pool_foreach (vui, vum->vhost_user_interfaces) { - - if (vui->unix_server_index == ~0) { //Nothing to do for server sockets - if (vui->clib_file_index == ~0) - { - if ((sockfd < 0) && - ((sockfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)) - { - /* - * 1st time error or new error for this interface, - * spit out the message and record the error - */ - if (!vui->sock_errno || (vui->sock_errno != errno)) - { - clib_unix_warning - ("Error: Could not open unix socket for %s", - vui->sock_filename); - vui->sock_errno = errno; - } - continue; - } - - /* try to connect */ - strncpy (sun.sun_path, (char *) vui->sock_filename, - sizeof (sun.sun_path) - 1); - sun.sun_path[sizeof (sun.sun_path) - 1] = 0; - - /* Avoid hanging VPP if the other end does not accept */ - if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0) - clib_unix_warning ("fcntl"); - - if (connect (sockfd, (struct sockaddr *) &sun, - sizeof (struct sockaddr_un)) == 0) - { - /* Set the socket to blocking as it was before */ - if (fcntl(sockfd, F_SETFL, 0) < 0) - clib_unix_warning ("fcntl2"); - - vui->sock_errno = 0; - template.file_descriptor = sockfd; - template.private_data = - vui - vhost_user_main.vhost_user_interfaces; - template.description = format (0, "vhost user process"); - vui->clib_file_index = clib_file_add (&file_main, &template); - vui->num_qid = 2; - - /* This sockfd is considered consumed */ - sockfd = -1; - } - else - { - vui->sock_errno = errno; - } - } - else - { - /* check if socket is alive */ - int error = 0; - socklen_t len = sizeof (error); - int fd = UNIX_GET_FD(vui->clib_file_index); - int retval = - getsockopt (fd, SOL_SOCKET, SO_ERROR, &error, &len); - - if (retval) - { - vu_log_debug (vui, "getsockopt returned %d", retval); - vhost_user_if_disconnect (vui); - } - } - } - } - /* *INDENT-ON* */ - } - return 0; -} - -/* *INDENT-OFF* */ -VLIB_REGISTER_NODE (vhost_user_process_node,static) = { - .function = vhost_user_process, - .type = VLIB_NODE_TYPE_PROCESS, - .name = "vhost-user-process", -}; -/* *INDENT-ON* */ - -/** - * Disables and reset interface structure. - * It can then be either init again, or removed from used interfaces. - */ -static void -vhost_user_term_if (vhost_user_intf_t * vui) -{ - int q; - vhost_user_main_t *vum = &vhost_user_main; - - // disconnect interface sockets - vhost_user_if_disconnect (vui); - vhost_user_update_gso_interface_count (vui, 0 /* delete */ ); - vhost_user_update_iface_state (vui); - - FOR_ALL_VHOST_RX_TXQ (q, vui) - { - clib_spinlock_free (&vui->vrings[q].vring_lock); - } - - if (vui->unix_server_index != ~0) - { - //Close server socket - clib_file_t *uf = pool_elt_at_index (file_main.file_pool, - vui->unix_server_index); - clib_file_del (&file_main, uf); - vui->unix_server_index = ~0; - unlink (vui->sock_filename); - } - - mhash_unset (&vum->if_index_by_sock_name, vui->sock_filename, - &vui->if_index); -} - -int -vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index) -{ - vhost_user_main_t *vum = &vhost_user_main; - vhost_user_intf_t *vui; - int rv = 0; - vnet_hw_interface_t *hwif; - u16 qid; - - if (! - (hwif = - vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index)) - || hwif->dev_class_index != vhost_user_device_class.index) - return VNET_API_ERROR_INVALID_SW_IF_INDEX; - - vui = pool_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance); - - vu_log_debug (vui, "Deleting vhost-user interface %s (instance %d)", - hwif->name, hwif->dev_instance); - - FOR_ALL_VHOST_TXQ (qid, vui) - { - vhost_user_vring_t *txvq = &vui->vrings[qid]; - - if ((txvq->mode == VNET_HW_IF_RX_MODE_POLLING) && - (txvq->thread_index != ~0)) - { - vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, txvq->thread_index); - ASSERT (cpu->polling_q_count != 0); - cpu->polling_q_count--; - } - - if ((vum->ifq_count > 0) && - ((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) || - (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE))) - { - vum->ifq_count--; - // Stop the timer if there is no more interrupt interface/queue - if (vum->ifq_count == 0) - { - vlib_process_signal_event (vm, - vhost_user_send_interrupt_node.index, - VHOST_USER_EVENT_STOP_TIMER, 0); - break; - } - } - } - - // Disable and reset interface - vhost_user_term_if (vui); - - // Reset renumbered iface - if (hwif->dev_instance < - vec_len (vum->show_dev_instance_by_real_dev_instance)) - vum->show_dev_instance_by_real_dev_instance[hwif->dev_instance] = ~0; - - // Delete ethernet interface - ethernet_delete_interface (vnm, vui->hw_if_index); - - // free vrings - vec_free (vui->vrings); - - // Back to pool - pool_put (vum->vhost_user_interfaces, vui); - - return rv; -} - -static clib_error_t * -vhost_user_exit (vlib_main_t * vm) -{ - vnet_main_t *vnm = vnet_get_main (); - vhost_user_main_t *vum = &vhost_user_main; - vhost_user_intf_t *vui; - - vlib_worker_thread_barrier_sync (vlib_get_main ()); - /* *INDENT-OFF* */ - pool_foreach (vui, vum->vhost_user_interfaces) { - vhost_user_delete_if (vnm, vm, vui->sw_if_index); - } - /* *INDENT-ON* */ - vlib_worker_thread_barrier_release (vlib_get_main ()); - return 0; -} - -VLIB_MAIN_LOOP_EXIT_FUNCTION (vhost_user_exit); - -/** - * Open server unix socket on specified sock_filename. - */ -static int -vhost_user_init_server_sock (const char *sock_filename, int *sock_fd) -{ - int rv = 0; - struct sockaddr_un un = { }; - int fd; - /* create listening socket */ - if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) - return VNET_API_ERROR_SYSCALL_ERROR_1; - - un.sun_family = AF_UNIX; - strncpy ((char *) un.sun_path, (char *) sock_filename, - sizeof (un.sun_path) - 1); - - /* remove if exists */ - unlink ((char *) sock_filename); - - if (bind (fd, (struct sockaddr *) &un, sizeof (un)) == -1) - { - rv = VNET_API_ERROR_SYSCALL_ERROR_2; - goto error; - } - - if (listen (fd, 1) == -1) - { - rv = VNET_API_ERROR_SYSCALL_ERROR_3; - goto error; - } - - *sock_fd = fd; - return 0; - -error: - close (fd); - return rv; -} - -/** - * Create ethernet interface for vhost user interface. - */ -static void -vhost_user_create_ethernet (vnet_main_t *vnm, vlib_main_t *vm, - vhost_user_intf_t *vui, - vhost_user_create_if_args_t *args) -{ - vhost_user_main_t *vum = &vhost_user_main; - u8 hwaddr[6]; - clib_error_t *error; - - /* create hw and sw interface */ - if (args->use_custom_mac) - { - clib_memcpy (hwaddr, args->hwaddr, 6); - } - else - { - random_u32 (&vum->random); - clib_memcpy (hwaddr + 2, &vum->random, sizeof (vum->random)); - hwaddr[0] = 2; - hwaddr[1] = 0xfe; - } - - error = ethernet_register_interface - (vnm, - vhost_user_device_class.index, - vui - vum->vhost_user_interfaces /* device instance */ , - hwaddr /* ethernet address */ , - &vui->hw_if_index, 0 /* flag change */ ); - - if (error) - clib_error_report (error); -} - -/* - * Initialize vui with specified attributes - */ -static void -vhost_user_vui_init (vnet_main_t * vnm, vhost_user_intf_t * vui, - int server_sock_fd, vhost_user_create_if_args_t * args, - u32 * sw_if_index) -{ - vnet_sw_interface_t *sw; - int q; - vhost_user_main_t *vum = &vhost_user_main; - vnet_hw_interface_t *hw; - - hw = vnet_get_hw_interface (vnm, vui->hw_if_index); - sw = vnet_get_hw_sw_interface (vnm, vui->hw_if_index); - if (server_sock_fd != -1) - { - clib_file_t template = { 0 }; - template.read_function = vhost_user_socksvr_accept_ready; - template.file_descriptor = server_sock_fd; - template.private_data = vui - vum->vhost_user_interfaces; //hw index - template.description = format (0, "vhost user %d", sw); - vui->unix_server_index = clib_file_add (&file_main, &template); - } - else - { - vui->unix_server_index = ~0; - } - - vui->sw_if_index = sw->sw_if_index; - strncpy (vui->sock_filename, args->sock_filename, - ARRAY_LEN (vui->sock_filename) - 1); - vui->sock_errno = 0; - vui->is_ready = 0; - vui->feature_mask = args->feature_mask; - vui->clib_file_index = ~0; - vui->log_base_addr = 0; - vui->if_index = vui - vum->vhost_user_interfaces; - vui->enable_gso = args->enable_gso; - vui->enable_event_idx = args->enable_event_idx; - vui->enable_packed = args->enable_packed; - /* - * enable_gso takes precedence over configurable feature mask if there - * is a clash. - * if feature mask disables gso, but enable_gso is configured, - * then gso is enable - * if feature mask enables gso, but enable_gso is not configured, - * then gso is enable - * - * if gso is enable via feature mask, it must enable both host and guest - * gso feature mask, we don't support one sided GSO or partial GSO. - */ - if ((vui->enable_gso == 0) && - ((args->feature_mask & FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS) - == (FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS))) - vui->enable_gso = 1; - vhost_user_update_gso_interface_count (vui, 1 /* add */ ); - mhash_set_mem (&vum->if_index_by_sock_name, vui->sock_filename, - &vui->if_index, 0); - - vec_validate_aligned (vui->vrings, (VHOST_VRING_INIT_MQ_PAIR_SZ << 1) - 1, - CLIB_CACHE_LINE_BYTES); - vui->num_qid = 2; - for (q = 0; q < vec_len (vui->vrings); q++) - vhost_user_vring_init (vui, q); - - hw->caps |= VNET_HW_INTERFACE_CAP_SUPPORTS_INT_MODE; - vnet_hw_interface_set_flags (vnm, vui->hw_if_index, 0); - - if (sw_if_index) - *sw_if_index = vui->sw_if_index; -} - -int -vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm, - vhost_user_create_if_args_t * args) -{ - vhost_user_intf_t *vui = NULL; - u32 sw_if_idx = ~0; - int rv = 0; - int server_sock_fd = -1; - vhost_user_main_t *vum = &vhost_user_main; - uword *if_index; - - if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0)) - { - return VNET_API_ERROR_INVALID_ARGUMENT; - } - - if_index = mhash_get (&vum->if_index_by_sock_name, - (void *) args->sock_filename); - if (if_index) - { - vui = &vum->vhost_user_interfaces[*if_index]; - args->sw_if_index = vui->sw_if_index; - return VNET_API_ERROR_IF_ALREADY_EXISTS; - } - - if (args->is_server) - { - if ((rv = - vhost_user_init_server_sock (args->sock_filename, - &server_sock_fd)) != 0) - { - return rv; - } - } - - /* Protect the uninitialized vui from being dispatched by rx/tx */ - vlib_worker_thread_barrier_sync (vm); - pool_get (vhost_user_main.vhost_user_interfaces, vui); - vhost_user_create_ethernet (vnm, vm, vui, args); - vlib_worker_thread_barrier_release (vm); - - vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx); - vnet_sw_interface_set_mtu (vnm, vui->sw_if_index, 9000); - vhost_user_rx_thread_placement (vui, 1); - - if (args->renumber) - vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance); - - args->sw_if_index = sw_if_idx; - - // Process node must connect - vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0); - - return rv; -} - -int -vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm, - vhost_user_create_if_args_t * args) -{ - vhost_user_main_t *vum = &vhost_user_main; - vhost_user_intf_t *vui = NULL; - u32 sw_if_idx = ~0; - int server_sock_fd = -1; - int rv = 0; - vnet_hw_interface_t *hwif; - uword *if_index; - - if (!(hwif = vnet_get_sup_hw_interface_api_visible_or_null (vnm, - args->sw_if_index)) - || hwif->dev_class_index != vhost_user_device_class.index) - return VNET_API_ERROR_INVALID_SW_IF_INDEX; - - if (args->sock_filename == NULL || !(strlen (args->sock_filename) > 0)) - return VNET_API_ERROR_INVALID_ARGUMENT; - - vui = vec_elt_at_index (vum->vhost_user_interfaces, hwif->dev_instance); - - /* - * Disallow changing the interface to have the same path name - * as other interface - */ - if_index = mhash_get (&vum->if_index_by_sock_name, - (void *) args->sock_filename); - if (if_index && (*if_index != vui->if_index)) - return VNET_API_ERROR_IF_ALREADY_EXISTS; - - // First try to open server socket - if (args->is_server) - if ((rv = vhost_user_init_server_sock (args->sock_filename, - &server_sock_fd)) != 0) - return rv; - - vhost_user_term_if (vui); - vhost_user_vui_init (vnm, vui, server_sock_fd, args, &sw_if_idx); - - if (args->renumber) - vnet_interface_name_renumber (sw_if_idx, args->custom_dev_instance); - - // Process node must connect - vlib_process_signal_event (vm, vhost_user_process_node.index, 0, 0); - - return rv; -} - -clib_error_t * -vhost_user_connect_command_fn (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) -{ - vnet_main_t *vnm = vnet_get_main (); - unformat_input_t _line_input, *line_input = &_line_input; - clib_error_t *error = NULL; - vhost_user_create_if_args_t args = { 0 }; - int rv; - - /* Get a line of input. */ - if (!unformat_user (input, unformat_line_input, line_input)) - return 0; - - args.feature_mask = (u64) ~ (0ULL); - args.custom_dev_instance = ~0; - /* GSO feature is disable by default */ - args.feature_mask &= ~FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS; - /* packed-ring feature is disable by default */ - args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_F_RING_PACKED); - /* event_idx feature is disable by default */ - args.feature_mask &= ~VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX); - - while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) - { - if (unformat (line_input, "socket %s", &args.sock_filename)) - ; - else if (unformat (line_input, "server")) - args.is_server = 1; - else if (unformat (line_input, "gso")) - args.enable_gso = 1; - else if (unformat (line_input, "packed")) - args.enable_packed = 1; - else if (unformat (line_input, "event-idx")) - args.enable_event_idx = 1; - else if (unformat (line_input, "feature-mask 0x%llx", - &args.feature_mask)) - ; - else if (unformat (line_input, "hwaddr %U", unformat_ethernet_address, - args.hwaddr)) - args.use_custom_mac = 1; - else if (unformat (line_input, "renumber %d", - &args.custom_dev_instance)) - args.renumber = 1; - else - { - error = clib_error_return (0, "unknown input `%U'", - format_unformat_error, line_input); - goto done; - } - } - - if ((rv = vhost_user_create_if (vnm, vm, &args))) - { - error = clib_error_return (0, "vhost_user_create_if returned %d", rv); - goto done; - } - - vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnm, - args.sw_if_index); - -done: - vec_free (args.sock_filename); - unformat_free (line_input); - - return error; -} - -clib_error_t * -vhost_user_delete_command_fn (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) -{ - unformat_input_t _line_input, *line_input = &_line_input; - u32 sw_if_index = ~0; - vnet_main_t *vnm = vnet_get_main (); - clib_error_t *error = NULL; - - /* 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, "sw_if_index %d", &sw_if_index)) - ; - else if (unformat - (line_input, "%U", unformat_vnet_sw_interface, vnm, - &sw_if_index)) - { - vnet_hw_interface_t *hwif = - vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index); - if (hwif == NULL || - vhost_user_device_class.index != hwif->dev_class_index) - { - error = clib_error_return (0, "Not a vhost interface"); - goto done; - } - } - else - { - error = clib_error_return (0, "unknown input `%U'", - format_unformat_error, line_input); - goto done; - } - } - - vhost_user_delete_if (vnm, vm, sw_if_index); - -done: - unformat_free (line_input); - - return error; -} - -int -vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm, - vhost_user_intf_details_t ** out_vuids) -{ - int rv = 0; - vhost_user_main_t *vum = &vhost_user_main; - vhost_user_intf_t *vui; - vhost_user_intf_details_t *r_vuids = NULL; - vhost_user_intf_details_t *vuid = NULL; - u32 *hw_if_indices = 0; - vnet_hw_interface_t *hi; - int i; - - if (!out_vuids) - return -1; - - pool_foreach (vui, vum->vhost_user_interfaces) - vec_add1 (hw_if_indices, vui->hw_if_index); - - for (i = 0; i < vec_len (hw_if_indices); i++) - { - hi = vnet_get_hw_interface (vnm, hw_if_indices[i]); - vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance); - - vec_add2 (r_vuids, vuid, 1); - vuid->sw_if_index = vui->sw_if_index; - vuid->virtio_net_hdr_sz = vui->virtio_net_hdr_sz; - vuid->features = vui->features; - vuid->num_regions = vui->nregions; - vuid->is_server = vui->unix_server_index != ~0; - vuid->sock_errno = vui->sock_errno; - snprintf ((char *) vuid->sock_filename, sizeof (vuid->sock_filename), - "%s", vui->sock_filename); - memcpy_s (vuid->if_name, sizeof (vuid->if_name), hi->name, - clib_min (vec_len (hi->name), sizeof (vuid->if_name) - 1)); - vuid->if_name[sizeof (vuid->if_name) - 1] = 0; - } - - vec_free (hw_if_indices); - - *out_vuids = r_vuids; - - return rv; -} - -static u8 * -format_vhost_user_desc (u8 * s, va_list * args) -{ - char *fmt = va_arg (*args, char *); - vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *); - vring_desc_t *desc_table = va_arg (*args, vring_desc_t *); - int idx = va_arg (*args, int); - u32 *mem_hint = va_arg (*args, u32 *); - - s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len, - desc_table[idx].flags, desc_table[idx].next, - pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr, - mem_hint))); - return s; -} - -static void -vhost_user_show_fds (vlib_main_t * vm, vhost_user_vring_t * vq) -{ - int kickfd = UNIX_GET_FD (vq->kickfd_idx); - int callfd = UNIX_GET_FD (vq->callfd_idx); - - vlib_cli_output (vm, " kickfd %d callfd %d errfd %d\n", kickfd, callfd, - vq->errfd); -} - -static void -vhost_user_show_desc (vlib_main_t * vm, vhost_user_intf_t * vui, int q, - int show_descr, int show_verbose) -{ - int j; - u32 mem_hint = 0; - u32 idx; - u32 n_entries; - vring_desc_t *desc_table; - vhost_user_vring_t *vq = &vui->vrings[q]; - - if (vq->avail && vq->used) - vlib_cli_output (vm, - " avail.flags %x avail event idx %u avail.idx %d " - "used.flags %x used event idx %u used.idx %d\n", - vq->avail->flags, vhost_user_avail_event_idx (vq), - vq->avail->idx, vq->used->flags, - vhost_user_used_event_idx (vq), vq->used->idx); - - vhost_user_show_fds (vm, vq); - - if (show_descr) - { - vlib_cli_output (vm, "\n descriptor table:\n"); - vlib_cli_output (vm, - " slot addr len flags next " - "user_addr\n"); - vlib_cli_output (vm, - " ===== ================== ===== ====== ===== " - "==================\n"); - for (j = 0; j < vq->qsz_mask + 1; j++) - { - desc_table = vq->desc; - vlib_cli_output (vm, "%U", format_vhost_user_desc, - " %-5d 0x%016lx %-5d 0x%04x %-5d 0x%016lx\n", vui, - desc_table, j, &mem_hint); - if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT)) - { - n_entries = desc_table[j].len / sizeof (vring_desc_t); - desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint); - if (desc_table) - { - for (idx = 0; idx < clib_min (20, n_entries); idx++) - { - vlib_cli_output - (vm, "%U", format_vhost_user_desc, - "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui, - desc_table, idx, &mem_hint); - } - if (n_entries >= 20) - vlib_cli_output (vm, "Skip displaying entries 20...%u\n", - n_entries); - } - } - } - } -} - -static u8 * -format_vhost_user_packed_desc (u8 * s, va_list * args) -{ - char *fmt = va_arg (*args, char *); - vhost_user_intf_t *vui = va_arg (*args, vhost_user_intf_t *); - vring_packed_desc_t *desc_table = va_arg (*args, vring_packed_desc_t *); - int idx = va_arg (*args, int); - u32 *mem_hint = va_arg (*args, u32 *); - - s = format (s, fmt, idx, desc_table[idx].addr, desc_table[idx].len, - desc_table[idx].flags, desc_table[idx].id, - pointer_to_uword (map_guest_mem (vui, desc_table[idx].addr, - mem_hint))); - return s; -} - -static u8 * -format_vhost_user_event_idx_flags (u8 * s, va_list * args) -{ - u32 flags = va_arg (*args, u32); - typedef struct - { - u8 value; - char *str; - } event_idx_flags; - static event_idx_flags event_idx_array[] = { -#define _(s,v) { .str = #s, .value = v, }, - foreach_virtio_event_idx_flags -#undef _ - }; - u32 num_entries = sizeof (event_idx_array) / sizeof (event_idx_flags); - - if (flags < num_entries) - s = format (s, "%s", event_idx_array[flags].str); - else - s = format (s, "%u", flags); - return s; -} - -static void -vhost_user_show_desc_packed (vlib_main_t * vm, vhost_user_intf_t * vui, int q, - int show_descr, int show_verbose) -{ - int j; - u32 mem_hint = 0; - u32 idx; - u32 n_entries; - vring_packed_desc_t *desc_table; - vhost_user_vring_t *vq = &vui->vrings[q]; - u16 off_wrap, event_idx; - - off_wrap = vq->avail_event->off_wrap; - event_idx = off_wrap & 0x7fff; - vlib_cli_output (vm, " avail_event.flags %U avail_event.off_wrap %u " - "avail event idx %u\n", format_vhost_user_event_idx_flags, - (u32) vq->avail_event->flags, off_wrap, event_idx); - - off_wrap = vq->used_event->off_wrap; - event_idx = off_wrap & 0x7fff; - vlib_cli_output (vm, " used_event.flags %U used_event.off_wrap %u " - "used event idx %u\n", format_vhost_user_event_idx_flags, - (u32) vq->used_event->flags, off_wrap, event_idx); - - vlib_cli_output (vm, " avail wrap counter %u, used wrap counter %u\n", - vq->avail_wrap_counter, vq->used_wrap_counter); - - vhost_user_show_fds (vm, vq); - - if (show_descr) - { - vlib_cli_output (vm, "\n descriptor table:\n"); - vlib_cli_output (vm, - " slot addr len flags id " - "user_addr\n"); - vlib_cli_output (vm, - " ===== ================== ===== ====== ===== " - "==================\n"); - for (j = 0; j < vq->qsz_mask + 1; j++) - { - desc_table = vq->packed_desc; - vlib_cli_output (vm, "%U", format_vhost_user_packed_desc, - " %-5u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui, - desc_table, j, &mem_hint); - if (show_verbose && (desc_table[j].flags & VRING_DESC_F_INDIRECT)) - { - n_entries = desc_table[j].len >> 4; - desc_table = map_guest_mem (vui, desc_table[j].addr, &mem_hint); - if (desc_table) - { - for (idx = 0; idx < clib_min (20, n_entries); idx++) - { - vlib_cli_output - (vm, "%U", format_vhost_user_packed_desc, - "> %-4u 0x%016lx %-5u 0x%04x %-5u 0x%016lx\n", vui, - desc_table, idx, &mem_hint); - } - if (n_entries >= 20) - vlib_cli_output (vm, "Skip displaying entries 20...%u\n", - n_entries); - } - } - } - } -} - -clib_error_t * -show_vhost_user_command_fn (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) -{ - clib_error_t *error = 0; - vnet_main_t *vnm = vnet_get_main (); - vhost_user_main_t *vum = &vhost_user_main; - vhost_user_intf_t *vui; - u32 hw_if_index, *hw_if_indices = 0; - vnet_hw_interface_t *hi; - u16 qid; - int i, j, q; - int show_descr = 0; - int show_verbose = 0; - struct feat_struct - { - u8 bit; - char *str; - }; - struct feat_struct *feat_entry; - - static struct feat_struct feat_array[] = { -#define _(s,b) { .str = #s, .bit = b, }, - foreach_virtio_net_features -#undef _ - {.str = NULL} - }; - -#define foreach_protocol_feature \ - _(VHOST_USER_PROTOCOL_F_MQ) \ - _(VHOST_USER_PROTOCOL_F_LOG_SHMFD) - - static struct feat_struct proto_feat_array[] = { -#define _(s) { .str = #s, .bit = s}, - foreach_protocol_feature -#undef _ - {.str = NULL} - }; - - while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) - { - if (unformat - (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index)) - { - hi = vnet_get_hw_interface (vnm, hw_if_index); - if (vhost_user_device_class.index != hi->dev_class_index) - { - error = clib_error_return (0, "unknown input `%U'", - format_unformat_error, input); - goto done; - } - vec_add1 (hw_if_indices, hw_if_index); - } - else if (unformat (input, "descriptors") || unformat (input, "desc")) - show_descr = 1; - else if (unformat (input, "verbose")) - show_verbose = 1; - else - { - error = clib_error_return (0, "unknown input `%U'", - format_unformat_error, input); - goto done; - } - } - if (vec_len (hw_if_indices) == 0) - { - pool_foreach (vui, vum->vhost_user_interfaces) - vec_add1 (hw_if_indices, vui->hw_if_index); - } - vlib_cli_output (vm, "Virtio vhost-user interfaces"); - vlib_cli_output (vm, "Global:\n coalesce frames %d time %e", - vum->coalesce_frames, vum->coalesce_time); - vlib_cli_output (vm, " Number of rx virtqueues in interrupt mode: %d", - vum->ifq_count); - vlib_cli_output (vm, " Number of GSO interfaces: %d", vum->gso_count); - for (u32 tid = 0; tid <= vlib_num_workers (); tid++) - { - vhost_cpu_t *cpu = vec_elt_at_index (vum->cpus, tid); - vlib_cli_output (vm, " Thread %u: Polling queue count %u", tid, - cpu->polling_q_count); - } - - for (i = 0; i < vec_len (hw_if_indices); i++) - { - hi = vnet_get_hw_interface (vnm, hw_if_indices[i]); - vui = pool_elt_at_index (vum->vhost_user_interfaces, hi->dev_instance); - vlib_cli_output (vm, "Interface: %U (ifindex %d)", - format_vnet_hw_if_index_name, vnm, hw_if_indices[i], - hw_if_indices[i]); - vlib_cli_output (vm, " Number of qids %u", vui->num_qid); - if (vui->enable_gso) - vlib_cli_output (vm, " GSO enable"); - if (vui->enable_packed) - vlib_cli_output (vm, " Packed ring enable"); - if (vui->enable_event_idx) - vlib_cli_output (vm, " Event index enable"); - - vlib_cli_output (vm, "virtio_net_hdr_sz %d\n" - " features mask (0x%llx): \n" - " features (0x%llx): \n", - vui->virtio_net_hdr_sz, vui->feature_mask, - vui->features); - - feat_entry = (struct feat_struct *) &feat_array; - while (feat_entry->str) - { - if (vui->features & (1ULL << feat_entry->bit)) - vlib_cli_output (vm, " %s (%d)", feat_entry->str, - feat_entry->bit); - feat_entry++; - } - - vlib_cli_output (vm, " protocol features (0x%llx)", - vui->protocol_features); - feat_entry = (struct feat_struct *) &proto_feat_array; - while (feat_entry->str) - { - if (vui->protocol_features & (1ULL << feat_entry->bit)) - vlib_cli_output (vm, " %s (%d)", feat_entry->str, - feat_entry->bit); - feat_entry++; - } - - vlib_cli_output (vm, "\n"); - - vlib_cli_output (vm, " socket filename %s type %s errno \"%s\"\n\n", - vui->sock_filename, - (vui->unix_server_index != ~0) ? "server" : "client", - strerror (vui->sock_errno)); - - vlib_cli_output (vm, " rx placement: "); - - FOR_ALL_VHOST_TXQ (qid, vui) - { - vhost_user_vring_t *txvq = &vui->vrings[qid]; - - if (txvq->qid == -1) - continue; - vlib_cli_output (vm, " thread %d on vring %d, %U\n", - txvq->thread_index, qid, format_vnet_hw_if_rx_mode, - txvq->mode); - } - - vlib_cli_output (vm, " tx placement\n"); - - FOR_ALL_VHOST_RXQ (qid, vui) - { - vhost_user_vring_t *rxvq = &vui->vrings[qid]; - vnet_hw_if_tx_queue_t *txq; - - if (rxvq->queue_index == ~0) - continue; - txq = vnet_hw_if_get_tx_queue (vnm, rxvq->queue_index); - if (txq->threads) - vlib_cli_output (vm, " threads %U on vring %u: %s\n", - format_bitmap_list, txq->threads, qid, - txq->shared_queue ? "spin-lock" : "lock-free"); - } - - vlib_cli_output (vm, "\n"); - - vlib_cli_output (vm, " Memory regions (total %d)\n", vui->nregions); - - if (vui->nregions) - { - vlib_cli_output (vm, - " region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr\n"); - vlib_cli_output (vm, - " ====== ===== ================== ================== ================== ================== ==================\n"); - } - for (j = 0; j < vui->nregions; j++) - { - vlib_cli_output (vm, - " %d %-5d 0x%016lx 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n", - j, vui->region_mmap_fd[j], - vui->regions[j].guest_phys_addr, - vui->regions[j].memory_size, - vui->regions[j].userspace_addr, - vui->regions[j].mmap_offset, - pointer_to_uword (vui->region_mmap_addr[j])); - } - FOR_ALL_VHOST_RX_TXQ (q, vui) - { - if (!vui->vrings[q].started) - continue; - - vlib_cli_output (vm, "\n Virtqueue %d (%s%s)\n", q, - (q & 1) ? "RX" : "TX", - vui->vrings[q].enabled ? "" : " disabled"); - vlib_cli_output (vm, " global %s queue index %u\n", - (q & 1) ? "RX" : "TX", vui->vrings[q].queue_index); - - vlib_cli_output ( - vm, - " qsz %d last_avail_idx %d last_used_idx %d" - " last_kick %u\n", - vui->vrings[q].qsz_mask + 1, vui->vrings[q].last_avail_idx, - vui->vrings[q].last_used_idx, vui->vrings[q].last_kick); - - if (vhost_user_is_packed_ring_supported (vui)) - vhost_user_show_desc_packed (vm, vui, q, show_descr, show_verbose); - else - vhost_user_show_desc (vm, vui, q, show_descr, show_verbose); - } - vlib_cli_output (vm, "\n"); - } -done: - vec_free (hw_if_indices); - return error; -} - -/* - * CLI functions - */ - -/*? - * Create a vHost User interface. Once created, a new virtual interface - * will exist with the name '<em>VirtualEthernet0/0/x</em>', where '<em>x</em>' - * is the next free index. - * - * There are several parameters associated with a vHost interface: - * - * - <b>socket <socket-filename></b> - Name of the linux socket used by hypervisor - * and VPP to manage the vHost interface. If in '<em>server</em>' mode, VPP will - * create the socket if it does not already exist. If in '<em>client</em>' mode, - * hypervisor will create the socket if it does not already exist. The VPP code - * is indifferent to the file location. However, if SELinux is enabled, then the - * socket needs to be created in '<em>/var/run/vpp/</em>'. - * - * - <b>server</b> - Optional flag to indicate that VPP should be the server for - * the linux socket. If not provided, VPP will be the client. In '<em>server</em>' - * mode, the VM can be reset without tearing down the vHost Interface. In - * '<em>client</em>' mode, VPP can be reset without bringing down the VM and - * tearing down the vHost Interface. - * - * - <b>feature-mask <hex></b> - Optional virtio/vhost feature set negotiated at - * startup. <b>This is intended for degugging only.</b> It is recommended that this - * parameter not be used except by experienced users. By default, all supported - * features will be advertised. Otherwise, provide the set of features desired. - * - 0x000008000 (15) - VIRTIO_NET_F_MRG_RXBUF - * - 0x000020000 (17) - VIRTIO_NET_F_CTRL_VQ - * - 0x000200000 (21) - VIRTIO_NET_F_GUEST_ANNOUNCE - * - 0x000400000 (22) - VIRTIO_NET_F_MQ - * - 0x004000000 (26) - VHOST_F_LOG_ALL - * - 0x008000000 (27) - VIRTIO_F_ANY_LAYOUT - * - 0x010000000 (28) - VIRTIO_F_INDIRECT_DESC - * - 0x040000000 (30) - VHOST_USER_F_PROTOCOL_FEATURES - * - 0x100000000 (32) - VIRTIO_F_VERSION_1 - * - * - <b>hwaddr <mac-addr></b> - Optional ethernet address, can be in either - * X:X:X:X:X:X unix or X.X.X cisco format. - * - * - <b>renumber <dev_instance></b> - Optional parameter which allows the instance - * in the name to be specified. If instance already exists, name will be used - * anyway and multiple instances will have the same name. Use with caution. - * - * @cliexpar - * Example of how to create a vhost interface with VPP as the client and all features enabled: - * @cliexstart{create vhost-user socket /var/run/vpp/vhost1.sock} - * VirtualEthernet0/0/0 - * @cliexend - * Example of how to create a vhost interface with VPP as the server and with just - * multiple queues enabled: - * @cliexstart{create vhost-user socket /var/run/vpp/vhost2.sock server feature-mask 0x40400000} - * VirtualEthernet0/0/1 - * @cliexend - * Once the vHost interface is created, enable the interface using: - * @cliexcmd{set interface state VirtualEthernet0/0/0 up} -?*/ -/* *INDENT-OFF* */ -VLIB_CLI_COMMAND (vhost_user_connect_command, static) = { - .path = "create vhost-user", - .short_help = "create vhost-user socket <socket-filename> [server] " - "[feature-mask <hex>] [hwaddr <mac-addr>] [renumber <dev_instance>] [gso] " - "[packed] [event-idx]", - .function = vhost_user_connect_command_fn, - .is_mp_safe = 1, -}; -/* *INDENT-ON* */ - -/*? - * Delete a vHost User interface using the interface name or the - * software interface index. Use the '<em>show interface</em>' - * command to determine the software interface index. On deletion, - * the linux socket will not be deleted. - * - * @cliexpar - * Example of how to delete a vhost interface by name: - * @cliexcmd{delete vhost-user VirtualEthernet0/0/1} - * Example of how to delete a vhost interface by software interface index: - * @cliexcmd{delete vhost-user sw_if_index 1} -?*/ -/* *INDENT-OFF* */ -VLIB_CLI_COMMAND (vhost_user_delete_command, static) = { - .path = "delete vhost-user", - .short_help = "delete vhost-user {<interface> | sw_if_index <sw_idx>}", - .function = vhost_user_delete_command_fn, -}; - -/*? - * Display the attributes of a single vHost User interface (provide interface - * name), multiple vHost User interfaces (provide a list of interface names seperated - * by spaces) or all Vhost User interfaces (omit an interface name to display all - * vHost interfaces). - * - * @cliexpar - * @parblock - * Example of how to display a vhost interface: - * @cliexstart{show vhost-user VirtualEthernet0/0/0} - * Virtio vhost-user interfaces - * Global: - * coalesce frames 32 time 1e-3 - * Interface: VirtualEthernet0/0/0 (ifindex 1) - * virtio_net_hdr_sz 12 - * features mask (0xffffffffffffffff): - * features (0x50408000): - * VIRTIO_NET_F_MRG_RXBUF (15) - * VIRTIO_NET_F_MQ (22) - * VIRTIO_F_INDIRECT_DESC (28) - * VHOST_USER_F_PROTOCOL_FEATURES (30) - * protocol features (0x3) - * VHOST_USER_PROTOCOL_F_MQ (0) - * VHOST_USER_PROTOCOL_F_LOG_SHMFD (1) - * - * socket filename /var/run/vpp/vhost1.sock type client errno "Success" - * - * rx placement: - * thread 1 on vring 1 - * thread 1 on vring 5 - * thread 2 on vring 3 - * thread 2 on vring 7 - * tx placement: spin-lock - * thread 0 on vring 0 - * thread 1 on vring 2 - * thread 2 on vring 0 - * - * Memory regions (total 2) - * region fd guest_phys_addr memory_size userspace_addr mmap_offset mmap_addr - * ====== ===== ================== ================== ================== ================== ================== - * 0 60 0x0000000000000000 0x00000000000a0000 0x00002aaaaac00000 0x0000000000000000 0x00002aab2b400000 - * 1 61 0x00000000000c0000 0x000000003ff40000 0x00002aaaaacc0000 0x00000000000c0000 0x00002aababcc0000 - * - * Virtqueue 0 (TX) - * qsz 256 last_avail_idx 0 last_used_idx 0 - * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0 - * kickfd 62 callfd 64 errfd -1 - * - * Virtqueue 1 (RX) - * qsz 256 last_avail_idx 0 last_used_idx 0 - * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0 - * kickfd 65 callfd 66 errfd -1 - * - * Virtqueue 2 (TX) - * qsz 256 last_avail_idx 0 last_used_idx 0 - * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0 - * kickfd 63 callfd 70 errfd -1 - * - * Virtqueue 3 (RX) - * qsz 256 last_avail_idx 0 last_used_idx 0 - * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0 - * kickfd 72 callfd 74 errfd -1 - * - * Virtqueue 4 (TX disabled) - * qsz 256 last_avail_idx 0 last_used_idx 0 - * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0 - * kickfd 76 callfd 78 errfd -1 - * - * Virtqueue 5 (RX disabled) - * qsz 256 last_avail_idx 0 last_used_idx 0 - * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0 - * kickfd 80 callfd 82 errfd -1 - * - * Virtqueue 6 (TX disabled) - * qsz 256 last_avail_idx 0 last_used_idx 0 - * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0 - * kickfd 84 callfd 86 errfd -1 - * - * Virtqueue 7 (RX disabled) - * qsz 256 last_avail_idx 0 last_used_idx 0 - * avail.flags 1 avail.idx 0 used.flags 1 used.idx 0 - * kickfd 88 callfd 90 errfd -1 - * - * @cliexend - * - * The optional '<em>descriptors</em>' parameter will display the same output as - * the previous example but will include the descriptor table for each queue. - * The output is truncated below: - * @cliexstart{show vhost-user VirtualEthernet0/0/0 descriptors} - * Virtio vhost-user interfaces - * Global: - * coalesce frames 32 time 1e-3 - * Interface: VirtualEthernet0/0/0 (ifindex 1) - * virtio_net_hdr_sz 12 - * features mask (0xffffffffffffffff): - * features (0x50408000): - * VIRTIO_NET_F_MRG_RXBUF (15) - * VIRTIO_NET_F_MQ (22) - * : - * Virtqueue 0 (TX) - * qsz 256 last_avail_idx 0 last_used_idx 0 - * avail.flags 1 avail.idx 128 used.flags 1 used.idx 0 - * kickfd 62 callfd 64 errfd -1 - * - * descriptor table: - * id addr len flags next user_addr - * ===== ================== ===== ====== ===== ================== - * 0 0x0000000010b6e974 2060 0x0002 1 0x00002aabbc76e974 - * 1 0x0000000010b6e034 2060 0x0002 2 0x00002aabbc76e034 - * 2 0x0000000010b6d6f4 2060 0x0002 3 0x00002aabbc76d6f4 - * 3 0x0000000010b6cdb4 2060 0x0002 4 0x00002aabbc76cdb4 - * 4 0x0000000010b6c474 2060 0x0002 5 0x00002aabbc76c474 - * 5 0x0000000010b6bb34 2060 0x0002 6 0x00002aabbc76bb34 - * 6 0x0000000010b6b1f4 2060 0x0002 7 0x00002aabbc76b1f4 - * 7 0x0000000010b6a8b4 2060 0x0002 8 0x00002aabbc76a8b4 - * 8 0x0000000010b69f74 2060 0x0002 9 0x00002aabbc769f74 - * 9 0x0000000010b69634 2060 0x0002 10 0x00002aabbc769634 - * 10 0x0000000010b68cf4 2060 0x0002 11 0x00002aabbc768cf4 - * : - * 249 0x0000000000000000 0 0x0000 250 0x00002aab2b400000 - * 250 0x0000000000000000 0 0x0000 251 0x00002aab2b400000 - * 251 0x0000000000000000 0 0x0000 252 0x00002aab2b400000 - * 252 0x0000000000000000 0 0x0000 253 0x00002aab2b400000 - * 253 0x0000000000000000 0 0x0000 254 0x00002aab2b400000 - * 254 0x0000000000000000 0 0x0000 255 0x00002aab2b400000 - * 255 0x0000000000000000 0 0x0000 32768 0x00002aab2b400000 - * - * Virtqueue 1 (RX) - * qsz 256 last_avail_idx 0 last_used_idx 0 - * : - * @cliexend - * @endparblock -?*/ -/* *INDENT-OFF* */ -VLIB_CLI_COMMAND (show_vhost_user_command, static) = { - .path = "show vhost-user", - .short_help = "show vhost-user [<interface> [<interface> [..]]] " - "[[descriptors] [verbose]]", - .function = show_vhost_user_command_fn, -}; -/* *INDENT-ON* */ - - -static clib_error_t * -vhost_user_config (vlib_main_t * vm, unformat_input_t * input) -{ - vhost_user_main_t *vum = &vhost_user_main; - - while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) - { - if (unformat (input, "coalesce-frames %d", &vum->coalesce_frames)) - ; - else if (unformat (input, "coalesce-time %f", &vum->coalesce_time)) - ; - else if (unformat (input, "dont-dump-memory")) - vum->dont_dump_vhost_user_memory = 1; - else - return clib_error_return (0, "unknown input `%U'", - format_unformat_error, input); - } - - return 0; -} - -/* vhost-user { ... } configuration. */ -VLIB_CONFIG_FUNCTION (vhost_user_config, "vhost-user"); - -void -vhost_user_unmap_all (void) -{ - vhost_user_main_t *vum = &vhost_user_main; - vhost_user_intf_t *vui; - - if (vum->dont_dump_vhost_user_memory) - { - pool_foreach (vui, vum->vhost_user_interfaces) - unmap_all_mem_regions (vui); - } -} - -/* - * fd.io coding-style-patch-verification: ON - * - * Local Variables: - * eval: (c-set-style "gnu") - * End: - */ diff --git a/src/vnet/devices/virtio/vhost_user.h b/src/vnet/devices/virtio/vhost_user.h deleted file mode 100644 index 59db5b4c592..00000000000 --- a/src/vnet/devices/virtio/vhost_user.h +++ /dev/null @@ -1,388 +0,0 @@ -/* - * 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. - */ -#ifndef __VIRTIO_VHOST_USER_H__ -#define __VIRTIO_VHOST_USER_H__ - -#include <vnet/devices/virtio/virtio_std.h> -#include <vnet/devices/virtio/vhost_std.h> - -/* vhost-user data structures */ - -#define VHOST_MEMORY_MAX_NREGIONS 8 -#define VHOST_USER_MSG_HDR_SZ 12 -#define VHOST_VRING_INIT_MQ_PAIR_SZ 8 //8TX + 8RX - -/* - * qid is one byte in size in the spec. Please see VHOST_USER_SET_VRING_CALL, - * VHOST_USER_SET_VRING_KICK, and VHOST_USER_SET_VRING_ERR. - * The max number for q pair is naturally 128. - */ -#define VHOST_VRING_MAX_MQ_PAIR_SZ 128 -#define VHOST_VRING_IDX_RX(qid) (2 * (qid)) -#define VHOST_VRING_IDX_TX(qid) (2 * (qid) + 1) - -#define VHOST_USER_VRING_NOFD_MASK 0x100 - -#define VHOST_USER_PROTOCOL_F_MQ 0 -#define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1 -#define VHOST_VRING_F_LOG 0 - -#define VHOST_USER_PROTOCOL_FEATURES ((1ULL << VHOST_USER_PROTOCOL_F_MQ) | \ - (1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD)) - -#define vu_log_debug(dev, f, ...) \ -{ \ - vlib_log(VLIB_LOG_LEVEL_DEBUG, vhost_user_main.log_default, "%U: " f, \ - format_vnet_hw_if_index_name, vnet_get_main(), \ - dev->hw_if_index, ##__VA_ARGS__); \ -}; - -#define vu_log_warn(dev, f, ...) \ -{ \ - vlib_log(VLIB_LOG_LEVEL_WARNING, vhost_user_main.log_default, "%U: " f, \ - format_vnet_hw_if_index_name, vnet_get_main(), \ - dev->hw_if_index, ##__VA_ARGS__); \ -}; -#define vu_log_err(dev, f, ...) \ -{ \ - vlib_log(VLIB_LOG_LEVEL_ERR, vhost_user_main.log_default, "%U: " f, \ - format_vnet_hw_if_index_name, vnet_get_main(), \ - dev->hw_if_index, ##__VA_ARGS__); \ -}; - -#define UNIX_GET_FD(unixfd_idx) ({ \ - typeof(unixfd_idx) __unixfd_idx = (unixfd_idx); \ - (__unixfd_idx != ~0) ? \ - pool_elt_at_index (file_main.file_pool, \ - __unixfd_idx)->file_descriptor : -1; }) - -#define foreach_virtio_trace_flags \ - _ (SIMPLE_CHAINED, 0, "Simple descriptor chaining") \ - _ (SINGLE_DESC, 1, "Single descriptor packet") \ - _ (INDIRECT, 2, "Indirect descriptor") \ - _ (MAP_ERROR, 4, "Memory mapping error") - -typedef enum -{ -#define _(n,i,s) VIRTIO_TRACE_F_##n, - foreach_virtio_trace_flags -#undef _ -} virtio_trace_flag_t; - -#define FEATURE_VIRTIO_NET_F_HOST_TSO_FEATURE_BITS \ - (VIRTIO_FEATURE (VIRTIO_NET_F_CSUM) | \ - VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO) | \ - VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) | \ - VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6)) - -#define FEATURE_VIRTIO_NET_F_GUEST_TSO_FEATURE_BITS \ - (VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM) | \ - VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO) | \ - VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4) | \ - VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6)) - -#define FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS \ - (FEATURE_VIRTIO_NET_F_HOST_TSO_FEATURE_BITS | \ - FEATURE_VIRTIO_NET_F_GUEST_TSO_FEATURE_BITS) - - -typedef struct -{ - char *sock_filename; - u64 feature_mask; - u32 custom_dev_instance; - u8 hwaddr[6]; - u8 renumber; - u8 is_server; - u8 enable_gso; - u8 enable_packed; - u8 enable_event_idx; - u8 use_custom_mac; - - /* return */ - u32 sw_if_index; -} vhost_user_create_if_args_t; - -int vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm, - vhost_user_create_if_args_t * args); -int vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm, - vhost_user_create_if_args_t * args); -int vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm, - u32 sw_if_index); - -/* *INDENT-OFF* */ -typedef struct vhost_user_memory_region -{ - u64 guest_phys_addr; - u64 memory_size; - u64 userspace_addr; - u64 mmap_offset; -} __attribute ((packed)) vhost_user_memory_region_t; - -typedef struct vhost_user_memory -{ - u32 nregions; - u32 padding; - vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS]; -} __attribute ((packed)) vhost_user_memory_t; - -typedef enum vhost_user_req -{ - VHOST_USER_NONE = 0, - VHOST_USER_GET_FEATURES = 1, - VHOST_USER_SET_FEATURES = 2, - VHOST_USER_SET_OWNER = 3, - VHOST_USER_RESET_OWNER = 4, - VHOST_USER_SET_MEM_TABLE = 5, - VHOST_USER_SET_LOG_BASE = 6, - VHOST_USER_SET_LOG_FD = 7, - VHOST_USER_SET_VRING_NUM = 8, - VHOST_USER_SET_VRING_ADDR = 9, - VHOST_USER_SET_VRING_BASE = 10, - VHOST_USER_GET_VRING_BASE = 11, - VHOST_USER_SET_VRING_KICK = 12, - VHOST_USER_SET_VRING_CALL = 13, - VHOST_USER_SET_VRING_ERR = 14, - VHOST_USER_GET_PROTOCOL_FEATURES = 15, - VHOST_USER_SET_PROTOCOL_FEATURES = 16, - VHOST_USER_GET_QUEUE_NUM = 17, - VHOST_USER_SET_VRING_ENABLE = 18, - VHOST_USER_MAX -} vhost_user_req_t; - -typedef struct vhost_user_msg { - vhost_user_req_t request; - u32 flags; - u32 size; - union - { - u64 u64; - vhost_vring_state_t state; - vhost_vring_addr_t addr; - vhost_user_memory_t memory; - vhost_user_log_t log; - }; -} __attribute ((packed)) vhost_user_msg_t; -/* *INDENT-ON* */ - -typedef struct -{ - CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); - u16 qsz_mask; - u16 last_avail_idx; - u16 last_used_idx; - u16 n_since_last_int; - union - { - vring_desc_t *desc; - vring_packed_desc_t *packed_desc; - }; - union - { - vring_avail_t *avail; - vring_desc_event_t *avail_event; - }; - union - { - vring_used_t *used; - vring_desc_event_t *used_event; - }; - uword desc_user_addr; - uword used_user_addr; - uword avail_user_addr; - f64 int_deadline; - u8 started; - u8 enabled; - u8 log_used; - clib_spinlock_t vring_lock; - - //Put non-runtime in a different cache line - CLIB_CACHE_LINE_ALIGN_MARK (cacheline1); - int errfd; - u32 callfd_idx; - u32 kickfd_idx; - u64 log_guest_addr; - - /* The rx queue policy (interrupt/adaptive/polling) for this queue */ - u32 mode; - - /* - * It contains the device queue number. -1 if it does not. The idea is - * to not invoke vnet_hw_interface_assign_rx_thread and - * vnet_hw_interface_unassign_rx_thread more than once for the duration of - * the interface even if it is disconnected and reconnected. - */ - i16 qid; - - u16 used_wrap_counter; - u16 avail_wrap_counter; - u16 last_kick; - u8 first_kick; - u32 queue_index; - u32 thread_index; -} vhost_user_vring_t; - -#define VHOST_USER_EVENT_START_TIMER 1 -#define VHOST_USER_EVENT_STOP_TIMER 2 - -typedef struct -{ - CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); - u32 is_ready; - u32 admin_up; - u32 unix_server_index; - u32 clib_file_index; - char sock_filename[256]; - int sock_errno; - uword if_index; - u32 hw_if_index, sw_if_index; - - //Feature negotiation - u64 features; - u64 feature_mask; - u64 protocol_features; - - //Memory region information - u32 nregions; - vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS]; - void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS]; - u64 region_guest_addr_lo[VHOST_MEMORY_MAX_NREGIONS]; - u64 region_guest_addr_hi[VHOST_MEMORY_MAX_NREGIONS]; - u32 region_mmap_fd[VHOST_MEMORY_MAX_NREGIONS]; - - //Virtual rings - vhost_user_vring_t *vrings; - - /* - * vrings is a dynamic array. It may have more elements than it is - * currently used. num_qid indicates the current total qid's in the - * vrings. For example, vec_len(vrings) = 64, num_qid = 60, so the - * current valid/used qid is (0, 59) in the vrings array. - */ - u32 num_qid; - - int virtio_net_hdr_sz; - int is_any_layout; - - void *log_base_addr; - u64 log_size; - - u8 enable_gso; - - /* Packed ring configured */ - u8 enable_packed; - - u8 enable_event_idx; -} vhost_user_intf_t; - -#define FOR_ALL_VHOST_TXQ(qid, vui) for (qid = 1; qid < vui->num_qid; qid += 2) - -#define FOR_ALL_VHOST_RXQ(qid, vui) for (qid = 0; qid < vui->num_qid; qid += 2) - -#define FOR_ALL_VHOST_RX_TXQ(qid, vui) for (qid = 0; qid < vui->num_qid; qid++) - -typedef struct -{ - uword dst; - uword src; - u32 len; -} vhost_copy_t; - -typedef struct -{ - u16 qid; /** The interface queue index (Not the virtio vring idx) */ - u16 device_index; /** The device index */ - u32 virtio_ring_flags; /** Runtime queue flags **/ - u16 first_desc_len; /** Length of the first data descriptor **/ - virtio_net_hdr_mrg_rxbuf_t hdr; /** Virtio header **/ -} vhost_trace_t; - -#define VHOST_USER_RX_BUFFERS_N (2 * VLIB_FRAME_SIZE + 2) -#define VHOST_USER_COPY_ARRAY_N (4 * VLIB_FRAME_SIZE) - -typedef struct -{ - u32 rx_buffers_len; - u32 rx_buffers[VHOST_USER_RX_BUFFERS_N]; - - virtio_net_hdr_mrg_rxbuf_t tx_headers[VLIB_FRAME_SIZE]; - vhost_copy_t copy[VHOST_USER_COPY_ARRAY_N]; - - /* This is here so it doesn't end-up - * using stack or registers. */ - vhost_trace_t *current_trace; - - u32 *to_next_list; - vlib_buffer_t **rx_buffers_pdesc; - u32 polling_q_count; -} vhost_cpu_t; - -typedef struct -{ - mhash_t if_index_by_sock_name; - u32 mtu_bytes; - vhost_user_intf_t *vhost_user_interfaces; - u32 *show_dev_instance_by_real_dev_instance; - u32 coalesce_frames; - f64 coalesce_time; - int dont_dump_vhost_user_memory; - - /** Per-CPU data for vhost-user */ - vhost_cpu_t *cpus; - - /** Pseudo random iterator */ - u32 random; - - /* The number of rx interface/queue pairs in interrupt mode */ - u32 ifq_count; - - /* logging */ - vlib_log_class_t log_default; - - /* gso interface count */ - u32 gso_count; -} vhost_user_main_t; - -typedef struct -{ - u8 if_name[64]; - u32 sw_if_index; - u32 virtio_net_hdr_sz; - u64 features; - u8 is_server; - u8 sock_filename[256]; - u32 num_regions; - int sock_errno; -} vhost_user_intf_details_t; - -int vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm, - vhost_user_intf_details_t ** out_vuids); -void vhost_user_set_operation_mode (vhost_user_intf_t *vui, - vhost_user_vring_t *txvq); - -extern vlib_node_registration_t vhost_user_send_interrupt_node; -extern vnet_device_class_t vhost_user_device_class; -extern vlib_node_registration_t vhost_user_input_node; -extern vhost_user_main_t vhost_user_main; - -#endif - -/* - * fd.io coding-style-patch-verification: ON - * - * Local Variables: - * eval: (c-set-style "gnu") - * End: - */ diff --git a/src/vnet/devices/virtio/vhost_user_api.c b/src/vnet/devices/virtio/vhost_user_api.c deleted file mode 100644 index df6768d4cde..00000000000 --- a/src/vnet/devices/virtio/vhost_user_api.c +++ /dev/null @@ -1,351 +0,0 @@ -/* - *------------------------------------------------------------------ - * vhost-user_api.c - vhost-user api - * - * 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 <vnet/vnet.h> -#include <vlibmemory/api.h> - -#include <vnet/interface.h> -#include <vnet/api_errno.h> -#include <vnet/devices/virtio/vhost_user.h> -#include <vnet/ethernet/ethernet.h> -#include <vnet/ethernet/ethernet_types_api.h> -#include <vnet/devices/virtio/virtio_types_api.h> - -#include <vnet/format_fns.h> -#include <vnet/devices/virtio/vhost_user.api_enum.h> -#include <vnet/devices/virtio/vhost_user.api_types.h> - -#define REPLY_MSG_ID_BASE msg_id_base -#include <vlibapi/api_helper_macros.h> - -static u16 msg_id_base; - -static void -vl_api_create_vhost_user_if_t_handler (vl_api_create_vhost_user_if_t * mp) -{ - int rv = 0; - vl_api_create_vhost_user_if_reply_t *rmp; - vnet_main_t *vnm = vnet_get_main (); - vlib_main_t *vm = vlib_get_main (); - u64 disabled_features = (u64) (0ULL); - vhost_user_create_if_args_t args = { 0 }; - - args.sw_if_index = (u32) ~ 0; - args.feature_mask = (u64) ~ (0ULL); - if (mp->disable_mrg_rxbuf) - disabled_features = VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF); - - if (mp->disable_indirect_desc) - disabled_features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC); - - /* - * GSO and PACKED are not supported by feature mask via binary API. We - * disable GSO and PACKED feature in the feature mask. They may be enabled - * explicitly via enable_gso and enable_packed argument - */ - disabled_features |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS | - VIRTIO_FEATURE (VIRTIO_F_RING_PACKED); - - /* EVENT_IDX is disabled by default */ - disabled_features |= VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX); - args.feature_mask &= ~disabled_features; - - if (mp->use_custom_mac) - mac_address_decode (mp->mac_address, (mac_address_t *) args.hwaddr); - - args.use_custom_mac = mp->use_custom_mac; - args.is_server = mp->is_server; - args.sock_filename = (char *) mp->sock_filename; - args.renumber = mp->renumber; - args.custom_dev_instance = ntohl (mp->custom_dev_instance); - args.enable_gso = mp->enable_gso; - args.enable_packed = mp->enable_packed; - rv = vhost_user_create_if (vnm, vm, &args); - - /* Remember an interface tag for the new interface */ - if (rv == 0) - { - /* If a tag was supplied... */ - if (mp->tag[0]) - { - /* Make sure it's a proper C-string */ - mp->tag[ARRAY_LEN (mp->tag) - 1] = 0; - u8 *tag = format (0, "%s%c", mp->tag, 0); - vnet_set_sw_interface_tag (vnm, tag, args.sw_if_index); - } - } - - /* *INDENT-OFF* */ - REPLY_MACRO2(VL_API_CREATE_VHOST_USER_IF_REPLY, - ({ - rmp->sw_if_index = ntohl (args.sw_if_index); - })); - /* *INDENT-ON* */ -} - -static void -vl_api_modify_vhost_user_if_t_handler (vl_api_modify_vhost_user_if_t * mp) -{ - int rv = 0; - vl_api_modify_vhost_user_if_reply_t *rmp; - u64 disabled_features = (u64) (0ULL); - vhost_user_create_if_args_t args = { 0 }; - vnet_main_t *vnm = vnet_get_main (); - vlib_main_t *vm = vlib_get_main (); - - args.feature_mask = (u64) ~ (0ULL); - /* - * GSO and PACKED are not supported by feature mask via binary API. We - * disable GSO and PACKED feature in the feature mask. They may be enabled - * explicitly via enable_gso and enable_packed argument - */ - disabled_features |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS | - VIRTIO_FEATURE (VIRTIO_F_RING_PACKED); - - /* EVENT_IDX is disabled by default */ - disabled_features |= VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX); - args.feature_mask &= ~disabled_features; - - args.sw_if_index = ntohl (mp->sw_if_index); - args.sock_filename = (char *) mp->sock_filename; - args.is_server = mp->is_server; - args.renumber = mp->renumber; - args.custom_dev_instance = ntohl (mp->custom_dev_instance); - args.enable_gso = mp->enable_gso; - args.enable_packed = mp->enable_packed; - rv = vhost_user_modify_if (vnm, vm, &args); - - REPLY_MACRO (VL_API_MODIFY_VHOST_USER_IF_REPLY); -} - -static void -vl_api_create_vhost_user_if_v2_t_handler (vl_api_create_vhost_user_if_v2_t * - mp) -{ - int rv = 0; - vl_api_create_vhost_user_if_v2_reply_t *rmp; - vnet_main_t *vnm = vnet_get_main (); - vlib_main_t *vm = vlib_get_main (); - u64 disabled_features = (u64) (0ULL); - vhost_user_create_if_args_t args = { 0 }; - - args.sw_if_index = (u32) ~ 0; - args.feature_mask = (u64) ~ (0ULL); - if (mp->disable_mrg_rxbuf) - disabled_features = VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF); - - if (mp->disable_indirect_desc) - disabled_features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC); - - /* - * GSO and PACKED are not supported by feature mask via binary API. We - * disable GSO and PACKED feature in the feature mask. They may be enabled - * explicitly via enable_gso and enable_packed argument - */ - disabled_features |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS | - VIRTIO_FEATURE (VIRTIO_F_RING_PACKED); - - /* EVENT_IDX is disabled by default */ - disabled_features |= VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX); - args.feature_mask &= ~disabled_features; - - if (mp->use_custom_mac) - mac_address_decode (mp->mac_address, (mac_address_t *) args.hwaddr); - - args.is_server = mp->is_server; - args.sock_filename = (char *) mp->sock_filename; - args.renumber = mp->renumber; - args.custom_dev_instance = ntohl (mp->custom_dev_instance); - args.enable_gso = mp->enable_gso; - args.enable_packed = mp->enable_packed; - args.enable_event_idx = mp->enable_event_idx; - rv = vhost_user_create_if (vnm, vm, &args); - - /* Remember an interface tag for the new interface */ - if (rv == 0) - { - /* If a tag was supplied... */ - if (mp->tag[0]) - { - /* Make sure it's a proper C-string */ - mp->tag[ARRAY_LEN (mp->tag) - 1] = 0; - u8 *tag = format (0, "%s%c", mp->tag, 0); - vnet_set_sw_interface_tag (vnm, tag, args.sw_if_index); - } - } - - /* *INDENT-OFF* */ - REPLY_MACRO2(VL_API_CREATE_VHOST_USER_IF_V2_REPLY, - ({ - rmp->sw_if_index = ntohl (args.sw_if_index); - })); - /* *INDENT-ON* */ -} - -static void -vl_api_modify_vhost_user_if_v2_t_handler (vl_api_modify_vhost_user_if_v2_t * - mp) -{ - int rv = 0; - vl_api_modify_vhost_user_if_v2_reply_t *rmp; - u64 disabled_features = (u64) (0ULL); - vhost_user_create_if_args_t args = { 0 }; - vnet_main_t *vnm = vnet_get_main (); - vlib_main_t *vm = vlib_get_main (); - - args.feature_mask = (u64) ~ (0ULL); - /* - * GSO and PACKED are not supported by feature mask via binary API. We - * disable GSO and PACKED feature in the feature mask. They may be enabled - * explicitly via enable_gso and enable_packed argument - */ - disabled_features |= FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS | - VIRTIO_FEATURE (VIRTIO_F_RING_PACKED); - - /* EVENT_IDX is disabled by default */ - disabled_features |= VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX); - args.feature_mask &= ~disabled_features; - - args.sw_if_index = ntohl (mp->sw_if_index); - args.sock_filename = (char *) mp->sock_filename; - args.is_server = mp->is_server; - args.renumber = mp->renumber; - args.custom_dev_instance = ntohl (mp->custom_dev_instance); - args.enable_gso = mp->enable_gso; - args.enable_packed = mp->enable_packed; - args.enable_event_idx = mp->enable_event_idx; - rv = vhost_user_modify_if (vnm, vm, &args); - - REPLY_MACRO (VL_API_MODIFY_VHOST_USER_IF_V2_REPLY); -} - -static void -vl_api_delete_vhost_user_if_t_handler (vl_api_delete_vhost_user_if_t * mp) -{ - int rv = 0; - vl_api_delete_vhost_user_if_reply_t *rmp; - u32 sw_if_index = ntohl (mp->sw_if_index); - vl_api_registration_t *reg; - - vnet_main_t *vnm = vnet_get_main (); - vlib_main_t *vm = vlib_get_main (); - - rv = vhost_user_delete_if (vnm, vm, sw_if_index); - - REPLY_MACRO (VL_API_DELETE_VHOST_USER_IF_REPLY); - if (!rv) - { - reg = vl_api_client_index_to_registration (mp->client_index); - if (!reg) - return; - - vnet_clear_sw_interface_tag (vnm, sw_if_index); - } -} - -static void -send_sw_interface_vhost_user_details (vpe_api_main_t * am, - vl_api_registration_t * reg, - vhost_user_intf_details_t * vui, - u32 context) -{ - vl_api_sw_interface_vhost_user_details_t *mp; - - mp = vl_msg_api_alloc (sizeof (*mp)); - clib_memset (mp, 0, sizeof (*mp)); - mp->_vl_msg_id = - ntohs (REPLY_MSG_ID_BASE + VL_API_SW_INTERFACE_VHOST_USER_DETAILS); - mp->sw_if_index = ntohl (vui->sw_if_index); - mp->virtio_net_hdr_sz = ntohl (vui->virtio_net_hdr_sz); - virtio_features_encode (vui->features, (u32 *) & mp->features_first_32, - (u32 *) & mp->features_last_32); - mp->is_server = vui->is_server; - mp->num_regions = ntohl (vui->num_regions); - mp->sock_errno = ntohl (vui->sock_errno); - mp->context = context; - - strncpy ((char *) mp->sock_filename, - (char *) vui->sock_filename, ARRAY_LEN (mp->sock_filename) - 1); - strncpy ((char *) mp->interface_name, - (char *) vui->if_name, ARRAY_LEN (mp->interface_name) - 1); - - vl_api_send_msg (reg, (u8 *) mp); -} - -static void - vl_api_sw_interface_vhost_user_dump_t_handler - (vl_api_sw_interface_vhost_user_dump_t * mp) -{ - int rv = 0; - vpe_api_main_t *am = &vpe_api_main; - vnet_main_t *vnm = vnet_get_main (); - vlib_main_t *vm = vlib_get_main (); - vhost_user_intf_details_t *ifaces = NULL; - vhost_user_intf_details_t *vuid = NULL; - vl_api_registration_t *reg; - u32 filter_sw_if_index; - - reg = vl_api_client_index_to_registration (mp->client_index); - if (!reg) - return; - - filter_sw_if_index = htonl (mp->sw_if_index); - if (filter_sw_if_index != ~0) - VALIDATE_SW_IF_INDEX (mp); - - rv = vhost_user_dump_ifs (vnm, vm, &ifaces); - if (rv) - return; - - vec_foreach (vuid, ifaces) - { - if ((filter_sw_if_index == ~0) || - (vuid->sw_if_index == filter_sw_if_index)) - send_sw_interface_vhost_user_details (am, reg, vuid, mp->context); - } - BAD_SW_IF_INDEX_LABEL; - vec_free (ifaces); -} - -#include <vnet/devices/virtio/vhost_user.api.c> -static clib_error_t * -vhost_user_api_hookup (vlib_main_t * vm) -{ - api_main_t *am = vlibapi_get_main (); - /* Mark CREATE_VHOST_USER_IF as mp safe */ - am->is_mp_safe[VL_API_CREATE_VHOST_USER_IF] = 1; - am->is_mp_safe[VL_API_CREATE_VHOST_USER_IF_V2] = 1; - - /* - * Set up the (msg_name, crc, message-id) table - */ - REPLY_MSG_ID_BASE = setup_message_id_table (); - - return 0; -} - -VLIB_API_INIT_FUNCTION (vhost_user_api_hookup); - -/* - * fd.io coding-style-patch-verification: ON - * - * Local Variables: - * eval: (c-set-style "gnu") - * End: - */ diff --git a/src/vnet/devices/virtio/vhost_user_inline.h b/src/vnet/devices/virtio/vhost_user_inline.h deleted file mode 100644 index 5297453c317..00000000000 --- a/src/vnet/devices/virtio/vhost_user_inline.h +++ /dev/null @@ -1,493 +0,0 @@ -/* - * Copyright (c) 2018 Cisco and/or its affiliates. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef __VIRTIO_VHOST_USER_INLINE_H__ -#define __VIRTIO_VHOST_USER_INLINE_H__ -/* vhost-user inline functions */ -#include <vppinfra/elog.h> - -static_always_inline void * -map_guest_mem (vhost_user_intf_t * vui, uword addr, u32 * hint) -{ - int i = *hint; - if (PREDICT_TRUE ((vui->regions[i].guest_phys_addr <= addr) && - ((vui->regions[i].guest_phys_addr + - vui->regions[i].memory_size) > addr))) - { - return (void *) (vui->region_mmap_addr[i] + addr - - vui->regions[i].guest_phys_addr); - } -#if __SSE4_2__ - __m128i rl, rh, al, ah, r; - al = _mm_set1_epi64x (addr + 1); - ah = _mm_set1_epi64x (addr); - - rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[0]); - rl = _mm_cmpgt_epi64 (al, rl); - rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[0]); - rh = _mm_cmpgt_epi64 (rh, ah); - r = _mm_and_si128 (rl, rh); - - rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[2]); - rl = _mm_cmpgt_epi64 (al, rl); - rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[2]); - rh = _mm_cmpgt_epi64 (rh, ah); - r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x22); - - rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[4]); - rl = _mm_cmpgt_epi64 (al, rl); - rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[4]); - rh = _mm_cmpgt_epi64 (rh, ah); - r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x44); - - rl = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_lo[6]); - rl = _mm_cmpgt_epi64 (al, rl); - rh = _mm_loadu_si128 ((__m128i *) & vui->region_guest_addr_hi[6]); - rh = _mm_cmpgt_epi64 (rh, ah); - r = _mm_blend_epi16 (r, _mm_and_si128 (rl, rh), 0x88); - - r = _mm_shuffle_epi8 (r, _mm_set_epi64x (0, 0x0e060c040a020800)); - i = count_trailing_zeros (_mm_movemask_epi8 (r) | - (1 << VHOST_MEMORY_MAX_NREGIONS)); - - if (i < vui->nregions) - { - *hint = i; - return (void *) (vui->region_mmap_addr[i] + addr - - vui->regions[i].guest_phys_addr); - } -#elif __aarch64__ && __ARM_NEON - uint64x2_t al, ah, rl, rh, r; - uint32_t u32 = 0; - - al = vdupq_n_u64 (addr + 1); - ah = vdupq_n_u64 (addr); - - /*First Iteration */ - rl = vld1q_u64 (&vui->region_guest_addr_lo[0]); - rl = vcgtq_u64 (al, rl); - rh = vld1q_u64 (&vui->region_guest_addr_hi[0]); - rh = vcgtq_u64 (rh, ah); - r = vandq_u64 (rl, rh); - u32 |= (vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 0) & 0x1); - u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 8) & 0x1) << 1); - - if (u32) - { - i = count_trailing_zeros (u32); - goto vhost_map_guest_mem_done; - } - - /*Second Iteration */ - rl = vld1q_u64 (&vui->region_guest_addr_lo[2]); - rl = vcgtq_u64 (al, rl); - rh = vld1q_u64 (&vui->region_guest_addr_hi[2]); - rh = vcgtq_u64 (rh, ah); - r = vandq_u64 (rl, rh); - u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 0) & 0x1) << 2); - u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 8) & 0x1) << 3); - - if (u32) - { - i = count_trailing_zeros (u32); - goto vhost_map_guest_mem_done; - } - - /*Third Iteration */ - rl = vld1q_u64 (&vui->region_guest_addr_lo[4]); - rl = vcgtq_u64 (al, rl); - rh = vld1q_u64 (&vui->region_guest_addr_hi[4]); - rh = vcgtq_u64 (rh, ah); - r = vandq_u64 (rl, rh); - u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 0) & 0x1) << 6); - u32 |= ((vgetq_lane_u8 (vreinterpretq_u8_u64 (r), 8) & 0x1) << 7); - - i = count_trailing_zeros (u32 | (1 << VHOST_MEMORY_MAX_NREGIONS)); - -vhost_map_guest_mem_done: - if (i < vui->nregions) - { - *hint = i; - return (void *) (vui->region_mmap_addr[i] + addr - - vui->regions[i].guest_phys_addr); - } -#else - for (i = 0; i < vui->nregions; i++) - { - if ((vui->regions[i].guest_phys_addr <= addr) && - ((vui->regions[i].guest_phys_addr + vui->regions[i].memory_size) > - addr)) - { - *hint = i; - return (void *) (vui->region_mmap_addr[i] + addr - - vui->regions[i].guest_phys_addr); - } - } -#endif - /* *INDENT-OFF* */ - ELOG_TYPE_DECLARE (el) = - { - .format = "failed to map guest mem addr %lx", - .format_args = "i8", - }; - /* *INDENT-ON* */ - struct - { - uword addr; - } *ed; - ed = ELOG_DATA (&vlib_global_main.elog_main, el); - ed->addr = addr; - *hint = 0; - return 0; -} - -static_always_inline void * -map_user_mem (vhost_user_intf_t * vui, uword addr) -{ - int i; - for (i = 0; i < vui->nregions; i++) - { - if ((vui->regions[i].userspace_addr <= addr) && - ((vui->regions[i].userspace_addr + vui->regions[i].memory_size) > - addr)) - { - return (void *) (vui->region_mmap_addr[i] + addr - - vui->regions[i].userspace_addr); - } - } - return 0; -} - -#define VHOST_LOG_PAGE 0x1000 - -static_always_inline void -vhost_user_log_dirty_pages_2 (vhost_user_intf_t * vui, - u64 addr, u64 len, u8 is_host_address) -{ - if (PREDICT_TRUE (vui->log_base_addr == 0 - || !(vui->features & VIRTIO_FEATURE (VHOST_F_LOG_ALL)))) - { - return; - } - if (is_host_address) - { - addr = pointer_to_uword (map_user_mem (vui, (uword) addr)); - } - if (PREDICT_FALSE ((addr + len - 1) / VHOST_LOG_PAGE / 8 >= vui->log_size)) - { - vu_log_debug (vui, "vhost_user_log_dirty_pages(): out of range\n"); - return; - } - - CLIB_MEMORY_BARRIER (); - u64 page = addr / VHOST_LOG_PAGE; - while (page * VHOST_LOG_PAGE < addr + len) - { - ((u8 *) vui->log_base_addr)[page / 8] |= 1 << page % 8; - page++; - } -} - - -#define vhost_user_log_dirty_ring(vui, vq, member) \ - if (PREDICT_FALSE(vq->log_used)) { \ - vhost_user_log_dirty_pages_2(vui, vq->log_guest_addr + STRUCT_OFFSET_OF(vring_used_t, member), \ - sizeof(vq->used->member), 0); \ - } - -static_always_inline u8 * -format_vhost_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 *); - CLIB_UNUSED (vnet_main_t * vnm) = vnet_get_main (); - vhost_user_main_t *vum = &vhost_user_main; - vhost_trace_t *t = va_arg (*va, vhost_trace_t *); - vhost_user_intf_t *vui = vum->vhost_user_interfaces + t->device_index; - vnet_sw_interface_t *sw; - u32 indent; - - if (pool_is_free (vum->vhost_user_interfaces, vui)) - { - s = format (s, "vhost-user interface is deleted"); - return s; - } - sw = vnet_get_sw_interface (vnm, vui->sw_if_index); - indent = format_get_indent (s); - s = format (s, "%U %U queue %d\n", format_white_space, indent, - format_vnet_sw_interface_name, vnm, sw, t->qid); - - s = format (s, "%U virtio flags:\n", format_white_space, indent); -#define _(n,i,st) \ - if (t->virtio_ring_flags & (1 << VIRTIO_TRACE_F_##n)) \ - s = format (s, "%U %s %s\n", format_white_space, indent, #n, st); - foreach_virtio_trace_flags -#undef _ - s = format (s, "%U virtio_net_hdr first_desc_len %u\n", - format_white_space, indent, t->first_desc_len); - - s = format (s, "%U flags 0x%02x gso_type %u\n", - format_white_space, indent, - t->hdr.hdr.flags, t->hdr.hdr.gso_type); - - if (vui->virtio_net_hdr_sz == 12) - s = format (s, "%U num_buff %u", - format_white_space, indent, t->hdr.num_buffers); - - return s; -} - -static_always_inline u64 -vhost_user_is_packed_ring_supported (vhost_user_intf_t * vui) -{ - return (vui->features & VIRTIO_FEATURE (VIRTIO_F_RING_PACKED)); -} - -static_always_inline u64 -vhost_user_is_event_idx_supported (vhost_user_intf_t * vui) -{ - return (vui->features & VIRTIO_FEATURE (VIRTIO_RING_F_EVENT_IDX)); -} - -static_always_inline void -vhost_user_kick (vlib_main_t * vm, vhost_user_vring_t * vq) -{ - vhost_user_main_t *vum = &vhost_user_main; - u64 x = 1; - int fd = UNIX_GET_FD (vq->callfd_idx); - int rv; - - rv = write (fd, &x, sizeof (x)); - if (PREDICT_FALSE (rv <= 0)) - { - clib_unix_warning - ("Error: Could not write to unix socket for callfd %d", fd); - return; - } - - vq->n_since_last_int = 0; - vq->int_deadline = vlib_time_now (vm) + vum->coalesce_time; -} - -static_always_inline u16 -vhost_user_avail_event_idx (vhost_user_vring_t * vq) -{ - volatile u16 *event_idx = (u16 *) & (vq->used->ring[vq->qsz_mask + 1]); - - return *event_idx; -} - -static_always_inline u16 -vhost_user_used_event_idx (vhost_user_vring_t * vq) -{ - volatile u16 *event_idx = (u16 *) & (vq->avail->ring[vq->qsz_mask + 1]); - - return *event_idx; -} - -static_always_inline u16 -vhost_user_need_event (u16 event_idx, u16 new_idx, u16 old_idx) -{ - return ((u16) (new_idx - event_idx - 1) < (u16) (new_idx - old_idx)); -} - -static_always_inline void -vhost_user_send_call_event_idx (vlib_main_t * vm, vhost_user_vring_t * vq) -{ - vhost_user_main_t *vum = &vhost_user_main; - u8 first_kick = vq->first_kick; - u16 event_idx = vhost_user_used_event_idx (vq); - - vq->first_kick = 1; - if (vhost_user_need_event (event_idx, vq->last_used_idx, vq->last_kick) || - PREDICT_FALSE (!first_kick)) - { - vhost_user_kick (vm, vq); - vq->last_kick = event_idx; - } - else - { - vq->n_since_last_int = 0; - vq->int_deadline = vlib_time_now (vm) + vum->coalesce_time; - } -} - -static_always_inline void -vhost_user_send_call_event_idx_packed (vlib_main_t * vm, - vhost_user_vring_t * vq) -{ - vhost_user_main_t *vum = &vhost_user_main; - u8 first_kick = vq->first_kick; - u16 off_wrap; - u16 event_idx; - u16 new_idx = vq->last_used_idx; - u16 old_idx = vq->last_kick; - - if (PREDICT_TRUE (vq->avail_event->flags == VRING_EVENT_F_DESC)) - { - CLIB_COMPILER_BARRIER (); - off_wrap = vq->avail_event->off_wrap; - event_idx = off_wrap & 0x7fff; - if (vq->used_wrap_counter != (off_wrap >> 15)) - event_idx -= (vq->qsz_mask + 1); - - if (new_idx <= old_idx) - old_idx -= (vq->qsz_mask + 1); - - vq->first_kick = 1; - vq->last_kick = event_idx; - if (vhost_user_need_event (event_idx, new_idx, old_idx) || - PREDICT_FALSE (!first_kick)) - vhost_user_kick (vm, vq); - else - { - vq->n_since_last_int = 0; - vq->int_deadline = vlib_time_now (vm) + vum->coalesce_time; - } - } - else - vhost_user_kick (vm, vq); -} - -static_always_inline void -vhost_user_send_call (vlib_main_t * vm, vhost_user_intf_t * vui, - vhost_user_vring_t * vq) -{ - if (vhost_user_is_event_idx_supported (vui)) - { - if (vhost_user_is_packed_ring_supported (vui)) - vhost_user_send_call_event_idx_packed (vm, vq); - else - vhost_user_send_call_event_idx (vm, vq); - } - else - vhost_user_kick (vm, vq); -} - -static_always_inline u8 -vui_is_link_up (vhost_user_intf_t * vui) -{ - return vui->admin_up && vui->is_ready; -} - -static_always_inline void -vhost_user_update_gso_interface_count (vhost_user_intf_t * vui, u8 add) -{ - vhost_user_main_t *vum = &vhost_user_main; - - if (vui->enable_gso) - { - if (add) - { - vum->gso_count++; - } - else - { - ASSERT (vum->gso_count > 0); - vum->gso_count--; - } - } -} - -static_always_inline u8 -vhost_user_packed_desc_available (vhost_user_vring_t * vring, u16 idx) -{ - return (((vring->packed_desc[idx].flags & VRING_DESC_F_AVAIL) == - vring->avail_wrap_counter)); -} - -static_always_inline void -vhost_user_advance_last_avail_idx (vhost_user_vring_t * vring) -{ - vring->last_avail_idx++; - if (PREDICT_FALSE ((vring->last_avail_idx & vring->qsz_mask) == 0)) - { - vring->avail_wrap_counter ^= VRING_DESC_F_AVAIL; - vring->last_avail_idx = 0; - } -} - -static_always_inline void -vhost_user_advance_last_avail_table_idx (vhost_user_intf_t * vui, - vhost_user_vring_t * vring, - u8 chained) -{ - if (chained) - { - vring_packed_desc_t *desc_table = vring->packed_desc; - - /* pick up the slot of the next avail idx */ - while (desc_table[vring->last_avail_idx & vring->qsz_mask].flags & - VRING_DESC_F_NEXT) - vhost_user_advance_last_avail_idx (vring); - } - - vhost_user_advance_last_avail_idx (vring); -} - -static_always_inline void -vhost_user_undo_advanced_last_avail_idx (vhost_user_vring_t * vring) -{ - if (PREDICT_FALSE ((vring->last_avail_idx & vring->qsz_mask) == 0)) - vring->avail_wrap_counter ^= VRING_DESC_F_AVAIL; - - if (PREDICT_FALSE (vring->last_avail_idx == 0)) - vring->last_avail_idx = vring->qsz_mask; - else - vring->last_avail_idx--; -} - -static_always_inline void -vhost_user_dequeue_descs (vhost_user_vring_t * rxvq, - virtio_net_hdr_mrg_rxbuf_t * hdr, - u16 * n_descs_processed) -{ - u16 i; - - *n_descs_processed -= (hdr->num_buffers - 1); - for (i = 0; i < hdr->num_buffers - 1; i++) - vhost_user_undo_advanced_last_avail_idx (rxvq); -} - -static_always_inline void -vhost_user_dequeue_chained_descs (vhost_user_vring_t * rxvq, - u16 * n_descs_processed) -{ - while (*n_descs_processed) - { - vhost_user_undo_advanced_last_avail_idx (rxvq); - (*n_descs_processed)--; - } -} - -static_always_inline void -vhost_user_advance_last_used_idx (vhost_user_vring_t * vring) -{ - vring->last_used_idx++; - if (PREDICT_FALSE ((vring->last_used_idx & vring->qsz_mask) == 0)) - { - vring->used_wrap_counter ^= 1; - vring->last_used_idx = 0; - } -} - -#endif - -/* - * fd.io coding-style-patch-verification: ON - * - * Local Variables: - * eval: (c-set-style "gnu") - * End: - */ diff --git a/src/vnet/devices/virtio/vhost_user_input.c b/src/vnet/devices/virtio/vhost_user_input.c deleted file mode 100644 index bdb3d27245b..00000000000 --- a/src/vnet/devices/virtio/vhost_user_input.c +++ /dev/null @@ -1,1473 +0,0 @@ -/* - *------------------------------------------------------------------ - * vhost-user-input - * - * Copyright (c) 2014-2018 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/un.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <sys/uio.h> /* for iovec */ -#include <netinet/in.h> -#include <sys/vfs.h> - -#include <linux/if_arp.h> -#include <linux/if_tun.h> - -#include <vlib/vlib.h> -#include <vlib/unix/unix.h> - -#include <vnet/ethernet/ethernet.h> -#include <vnet/devices/devices.h> -#include <vnet/feature/feature.h> -#include <vnet/udp/udp_packet.h> -#include <vnet/interface/rx_queue_funcs.h> - -#include <vnet/devices/virtio/vhost_user.h> -#include <vnet/devices/virtio/vhost_user_inline.h> - -#include <vnet/ip/ip4_packet.h> -#include <vnet/ip/ip6_packet.h> - -/* - * When an RX queue is down but active, received packets - * must be discarded. This value controls up to how many - * packets will be discarded during each round. - */ -#define VHOST_USER_DOWN_DISCARD_COUNT 256 - -/* - * When the number of available buffers gets under this threshold, - * RX node will start discarding packets. - */ -#define VHOST_USER_RX_BUFFER_STARVATION 32 - -/* - * On the receive side, the host should free descriptors as soon - * as possible in order to avoid TX drop in the VM. - * This value controls the number of copy operations that are stacked - * before copy is done for all and descriptors are given back to - * the guest. - * The value 64 was obtained by testing (48 and 128 were not as good). - */ -#define VHOST_USER_RX_COPY_THRESHOLD 64 - -extern vlib_node_registration_t vhost_user_input_node; - -#define foreach_vhost_user_input_func_error \ - _(NO_ERROR, "no error") \ - _(NO_BUFFER, "no available buffer") \ - _(MMAP_FAIL, "mmap failure") \ - _(INDIRECT_OVERFLOW, "indirect descriptor overflows table") \ - _(UNDERSIZED_FRAME, "undersized ethernet frame received (< 14 bytes)") \ - _(NOT_READY, "vhost interface not ready or down") \ - _(FULL_RX_QUEUE, "full rx queue (possible driver tx drop)") - -typedef enum -{ -#define _(f,s) VHOST_USER_INPUT_FUNC_ERROR_##f, - foreach_vhost_user_input_func_error -#undef _ - VHOST_USER_INPUT_FUNC_N_ERROR, -} vhost_user_input_func_error_t; - -static __clib_unused char *vhost_user_input_func_error_strings[] = { -#define _(n,s) s, - foreach_vhost_user_input_func_error -#undef _ -}; - -static_always_inline void -vhost_user_rx_trace (vhost_trace_t * t, - vhost_user_intf_t * vui, u16 qid, - vlib_buffer_t * b, vhost_user_vring_t * txvq, - u16 last_avail_idx) -{ - vhost_user_main_t *vum = &vhost_user_main; - u32 desc_current = txvq->avail->ring[last_avail_idx & txvq->qsz_mask]; - vring_desc_t *hdr_desc = 0; - virtio_net_hdr_mrg_rxbuf_t *hdr; - u32 hint = 0; - - clib_memset (t, 0, sizeof (*t)); - t->device_index = vui - vum->vhost_user_interfaces; - t->qid = qid; - - hdr_desc = &txvq->desc[desc_current]; - if (txvq->desc[desc_current].flags & VRING_DESC_F_INDIRECT) - { - t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT; - /* Header is the first here */ - hdr_desc = map_guest_mem (vui, txvq->desc[desc_current].addr, &hint); - } - if (txvq->desc[desc_current].flags & VRING_DESC_F_NEXT) - { - t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED; - } - if (!(txvq->desc[desc_current].flags & VRING_DESC_F_NEXT) && - !(txvq->desc[desc_current].flags & VRING_DESC_F_INDIRECT)) - { - t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC; - } - - t->first_desc_len = hdr_desc ? hdr_desc->len : 0; - - if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint))) - { - t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR; - } - else - { - u32 len = vui->virtio_net_hdr_sz; - memcpy (&t->hdr, hdr, len > hdr_desc->len ? hdr_desc->len : len); - } -} - -static_always_inline u32 -vhost_user_input_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy, - u16 copy_len, u32 * map_hint) -{ - void *src0, *src1, *src2, *src3; - if (PREDICT_TRUE (copy_len >= 4)) - { - if (PREDICT_FALSE (!(src2 = map_guest_mem (vui, cpy[0].src, map_hint)))) - return 1; - if (PREDICT_FALSE (!(src3 = map_guest_mem (vui, cpy[1].src, map_hint)))) - return 1; - - while (PREDICT_TRUE (copy_len >= 4)) - { - src0 = src2; - src1 = src3; - - if (PREDICT_FALSE - (!(src2 = map_guest_mem (vui, cpy[2].src, map_hint)))) - return 1; - if (PREDICT_FALSE - (!(src3 = map_guest_mem (vui, cpy[3].src, map_hint)))) - return 1; - - clib_prefetch_load (src2); - clib_prefetch_load (src3); - - clib_memcpy_fast ((void *) cpy[0].dst, src0, cpy[0].len); - clib_memcpy_fast ((void *) cpy[1].dst, src1, cpy[1].len); - copy_len -= 2; - cpy += 2; - } - } - while (copy_len) - { - if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint)))) - return 1; - clib_memcpy_fast ((void *) cpy->dst, src0, cpy->len); - copy_len -= 1; - cpy += 1; - } - return 0; -} - -/** - * Try to discard packets from the tx ring (VPP RX path). - * Returns the number of discarded packets. - */ -static_always_inline u32 -vhost_user_rx_discard_packet (vlib_main_t * vm, - vhost_user_intf_t * vui, - vhost_user_vring_t * txvq, u32 discard_max) -{ - /* - * On the RX side, each packet corresponds to one descriptor - * (it is the same whether it is a shallow descriptor, chained, or indirect). - * Therefore, discarding a packet is like discarding a descriptor. - */ - u32 discarded_packets = 0; - u32 avail_idx = txvq->avail->idx; - u16 mask = txvq->qsz_mask; - u16 last_avail_idx = txvq->last_avail_idx; - u16 last_used_idx = txvq->last_used_idx; - while (discarded_packets != discard_max) - { - if (avail_idx == last_avail_idx) - goto out; - - u16 desc_chain_head = txvq->avail->ring[last_avail_idx & mask]; - last_avail_idx++; - txvq->used->ring[last_used_idx & mask].id = desc_chain_head; - txvq->used->ring[last_used_idx & mask].len = 0; - vhost_user_log_dirty_ring (vui, txvq, ring[last_used_idx & mask]); - last_used_idx++; - discarded_packets++; - } - -out: - txvq->last_avail_idx = last_avail_idx; - txvq->last_used_idx = last_used_idx; - CLIB_MEMORY_STORE_BARRIER (); - txvq->used->idx = txvq->last_used_idx; - vhost_user_log_dirty_ring (vui, txvq, idx); - return discarded_packets; -} - -/* - * In case of overflow, we need to rewind the array of allocated buffers. - */ -static_always_inline void -vhost_user_input_rewind_buffers (vlib_main_t * vm, - vhost_cpu_t * cpu, vlib_buffer_t * b_head) -{ - u32 bi_current = cpu->rx_buffers[cpu->rx_buffers_len]; - vlib_buffer_t *b_current = vlib_get_buffer (vm, bi_current); - b_current->current_length = 0; - b_current->flags = 0; - while (b_current != b_head) - { - cpu->rx_buffers_len++; - bi_current = cpu->rx_buffers[cpu->rx_buffers_len]; - b_current = vlib_get_buffer (vm, bi_current); - b_current->current_length = 0; - b_current->flags = 0; - } - cpu->rx_buffers_len++; -} - -static_always_inline void -vhost_user_handle_rx_offload (vlib_buffer_t * b0, u8 * b0_data, - virtio_net_hdr_t * hdr) -{ - u8 l4_hdr_sz = 0; - u8 l4_proto = 0; - ethernet_header_t *eh = (ethernet_header_t *) b0_data; - u16 ethertype = clib_net_to_host_u16 (eh->type); - u16 l2hdr_sz = sizeof (ethernet_header_t); - vnet_buffer_oflags_t oflags = 0; - - if (ethernet_frame_is_tagged (ethertype)) - { - ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1); - - ethertype = clib_net_to_host_u16 (vlan->type); - l2hdr_sz += sizeof (*vlan); - if (ethertype == ETHERNET_TYPE_VLAN) - { - vlan++; - ethertype = clib_net_to_host_u16 (vlan->type); - l2hdr_sz += sizeof (*vlan); - } - } - vnet_buffer (b0)->l2_hdr_offset = 0; - vnet_buffer (b0)->l3_hdr_offset = l2hdr_sz; - vnet_buffer (b0)->l4_hdr_offset = hdr->csum_start; - b0->flags |= (VNET_BUFFER_F_L2_HDR_OFFSET_VALID | - VNET_BUFFER_F_L3_HDR_OFFSET_VALID | - VNET_BUFFER_F_L4_HDR_OFFSET_VALID); - - if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP4)) - { - ip4_header_t *ip4 = (ip4_header_t *) (b0_data + l2hdr_sz); - l4_proto = ip4->protocol; - b0->flags |= VNET_BUFFER_F_IS_IP4; - oflags |= VNET_BUFFER_OFFLOAD_F_IP_CKSUM; - } - else if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP6)) - { - ip6_header_t *ip6 = (ip6_header_t *) (b0_data + l2hdr_sz); - l4_proto = ip6->protocol; - b0->flags |= VNET_BUFFER_F_IS_IP6; - } - - if (l4_proto == IP_PROTOCOL_TCP) - { - tcp_header_t *tcp = (tcp_header_t *) - (b0_data + vnet_buffer (b0)->l4_hdr_offset); - l4_hdr_sz = tcp_header_bytes (tcp); - oflags |= VNET_BUFFER_OFFLOAD_F_TCP_CKSUM; - } - else if (l4_proto == IP_PROTOCOL_UDP) - { - l4_hdr_sz = sizeof (udp_header_t); - oflags |= VNET_BUFFER_OFFLOAD_F_UDP_CKSUM; - } - - if (hdr->gso_type == VIRTIO_NET_HDR_GSO_UDP) - { - vnet_buffer2 (b0)->gso_size = hdr->gso_size; - vnet_buffer2 (b0)->gso_l4_hdr_sz = l4_hdr_sz; - b0->flags |= VNET_BUFFER_F_GSO; - } - else if (hdr->gso_type == VIRTIO_NET_HDR_GSO_TCPV4) - { - vnet_buffer2 (b0)->gso_size = hdr->gso_size; - vnet_buffer2 (b0)->gso_l4_hdr_sz = l4_hdr_sz; - b0->flags |= (VNET_BUFFER_F_GSO | VNET_BUFFER_F_IS_IP4); - } - else if (hdr->gso_type == VIRTIO_NET_HDR_GSO_TCPV6) - { - vnet_buffer2 (b0)->gso_size = hdr->gso_size; - vnet_buffer2 (b0)->gso_l4_hdr_sz = l4_hdr_sz; - b0->flags |= (VNET_BUFFER_F_GSO | VNET_BUFFER_F_IS_IP6); - } - - if (oflags) - vnet_buffer_offload_flags_set (b0, oflags); -} - -static_always_inline void -vhost_user_input_do_interrupt (vlib_main_t * vm, vhost_user_intf_t * vui, - vhost_user_vring_t * txvq, - vhost_user_vring_t * rxvq) -{ - f64 now = vlib_time_now (vm); - - if ((txvq->n_since_last_int) && (txvq->int_deadline < now)) - vhost_user_send_call (vm, vui, txvq); - - if ((rxvq->n_since_last_int) && (rxvq->int_deadline < now)) - vhost_user_send_call (vm, vui, rxvq); -} - -static_always_inline void -vhost_user_input_setup_frame (vlib_main_t * vm, vlib_node_runtime_t * node, - vhost_user_intf_t * vui, - u32 * current_config_index, u32 * next_index, - u32 ** to_next, u32 * n_left_to_next) -{ - vnet_feature_main_t *fm = &feature_main; - u8 feature_arc_idx = fm->device_input_feature_arc_index; - - if (PREDICT_FALSE (vnet_have_features (feature_arc_idx, vui->sw_if_index))) - { - vnet_feature_config_main_t *cm; - cm = &fm->feature_config_mains[feature_arc_idx]; - *current_config_index = vec_elt (cm->config_index_by_sw_if_index, - vui->sw_if_index); - vnet_get_config_data (&cm->config_main, current_config_index, - next_index, 0); - } - - vlib_get_new_next_frame (vm, node, *next_index, *to_next, *n_left_to_next); - - if (*next_index == VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT) - { - /* give some hints to ethernet-input */ - vlib_next_frame_t *nf; - vlib_frame_t *f; - ethernet_input_frame_t *ef; - nf = vlib_node_runtime_get_next_frame (vm, node, *next_index); - f = vlib_get_frame (vm, nf->frame); - f->flags = ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX; - - ef = vlib_frame_scalar_args (f); - ef->sw_if_index = vui->sw_if_index; - ef->hw_if_index = vui->hw_if_index; - vlib_frame_no_append (f); - } -} - -static_always_inline u32 -vhost_user_if_input (vlib_main_t *vm, vhost_user_main_t *vum, - vhost_user_intf_t *vui, u16 qid, - vlib_node_runtime_t *node, u8 enable_csum) -{ - vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)]; - vnet_feature_main_t *fm = &feature_main; - u16 n_rx_packets = 0; - u32 n_rx_bytes = 0; - u16 n_left; - u32 n_left_to_next, *to_next; - u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT; - u32 n_trace = vlib_get_trace_count (vm, node); - u32 buffer_data_size = vlib_buffer_get_default_data_size (vm); - u32 map_hint = 0; - vhost_cpu_t *cpu = &vum->cpus[vm->thread_index]; - u16 copy_len = 0; - u8 feature_arc_idx = fm->device_input_feature_arc_index; - u32 current_config_index = ~(u32) 0; - u16 mask = txvq->qsz_mask; - - /* The descriptor table is not ready yet */ - if (PREDICT_FALSE (txvq->avail == 0)) - goto done; - - { - /* do we have pending interrupts ? */ - vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)]; - vhost_user_input_do_interrupt (vm, vui, txvq, rxvq); - } - - /* - * For adaptive mode, it is optimized to reduce interrupts. - * If the scheduler switches the input node to polling due - * to burst of traffic, we tell the driver no interrupt. - * When the traffic subsides, the scheduler switches the node back to - * interrupt mode. We must tell the driver we want interrupt. - */ - if (PREDICT_FALSE (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)) - { - if ((node->flags & - VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE) || - !(node->flags & - VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE)) - /* Tell driver we want notification */ - txvq->used->flags = 0; - else - /* Tell driver we don't want notification */ - txvq->used->flags = VRING_USED_F_NO_NOTIFY; - } - - if (PREDICT_FALSE (txvq->avail->flags & 0xFFFE)) - goto done; - - n_left = (u16) (txvq->avail->idx - txvq->last_avail_idx); - - /* nothing to do */ - if (PREDICT_FALSE (n_left == 0)) - goto done; - - if (PREDICT_FALSE (!vui->admin_up || !(txvq->enabled))) - { - /* - * Discard input packet if interface is admin down or vring is not - * enabled. - * "For example, for a networking device, in the disabled state - * client must not supply any new RX packets, but must process - * and discard any TX packets." - */ - vhost_user_rx_discard_packet (vm, vui, txvq, - VHOST_USER_DOWN_DISCARD_COUNT); - goto done; - } - - if (PREDICT_FALSE (n_left == (mask + 1))) - { - /* - * Informational error logging when VPP is not - * receiving packets fast enough. - */ - vlib_error_count (vm, node->node_index, - VHOST_USER_INPUT_FUNC_ERROR_FULL_RX_QUEUE, 1); - } - - if (n_left > VLIB_FRAME_SIZE) - n_left = VLIB_FRAME_SIZE; - - /* - * For small packets (<2kB), we will not need more than one vlib buffer - * per packet. In case packets are bigger, we will just yield at some point - * in the loop and come back later. This is not an issue as for big packet, - * processing cost really comes from the memory copy. - * The assumption is that big packets will fit in 40 buffers. - */ - if (PREDICT_FALSE (cpu->rx_buffers_len < n_left + 1 || - cpu->rx_buffers_len < 40)) - { - u32 curr_len = cpu->rx_buffers_len; - cpu->rx_buffers_len += - vlib_buffer_alloc (vm, cpu->rx_buffers + curr_len, - VHOST_USER_RX_BUFFERS_N - curr_len); - - if (PREDICT_FALSE - (cpu->rx_buffers_len < VHOST_USER_RX_BUFFER_STARVATION)) - { - /* In case of buffer starvation, discard some packets from the queue - * and log the event. - * We keep doing best effort for the remaining packets. */ - u32 flush = (n_left + 1 > cpu->rx_buffers_len) ? - n_left + 1 - cpu->rx_buffers_len : 1; - flush = vhost_user_rx_discard_packet (vm, vui, txvq, flush); - - n_left -= flush; - vlib_increment_simple_counter (vnet_main. - interface_main.sw_if_counters + - VNET_INTERFACE_COUNTER_DROP, - vm->thread_index, vui->sw_if_index, - flush); - - vlib_error_count (vm, vhost_user_input_node.index, - VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, flush); - } - } - - vhost_user_input_setup_frame (vm, node, vui, ¤t_config_index, - &next_index, &to_next, &n_left_to_next); - - u16 last_avail_idx = txvq->last_avail_idx; - u16 last_used_idx = txvq->last_used_idx; - - while (n_left > 0) - { - vlib_buffer_t *b_head, *b_current; - u32 bi_current; - u16 desc_current; - u32 desc_data_offset; - vring_desc_t *desc_table = txvq->desc; - - if (PREDICT_FALSE (cpu->rx_buffers_len <= 1)) - { - /* Not enough rx_buffers - * Note: We yeld on 1 so we don't need to do an additional - * check for the next buffer prefetch. - */ - n_left = 0; - break; - } - - desc_current = txvq->avail->ring[last_avail_idx & mask]; - cpu->rx_buffers_len--; - bi_current = cpu->rx_buffers[cpu->rx_buffers_len]; - b_head = b_current = vlib_get_buffer (vm, bi_current); - to_next[0] = bi_current; //We do that now so we can forget about bi_current - to_next++; - n_left_to_next--; - - vlib_prefetch_buffer_with_index - (vm, cpu->rx_buffers[cpu->rx_buffers_len - 1], LOAD); - - /* Just preset the used descriptor id and length for later */ - txvq->used->ring[last_used_idx & mask].id = desc_current; - txvq->used->ring[last_used_idx & mask].len = 0; - vhost_user_log_dirty_ring (vui, txvq, ring[last_used_idx & mask]); - - /* The buffer should already be initialized */ - b_head->total_length_not_including_first_buffer = 0; - b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; - - if (PREDICT_FALSE - (n_trace > 0 && vlib_trace_buffer (vm, node, next_index, b_head, - /* follow_chain */ 0))) - { - vhost_trace_t *t0 = - vlib_add_trace (vm, node, b_head, sizeof (t0[0])); - vhost_user_rx_trace (t0, vui, qid, b_head, txvq, last_avail_idx); - n_trace--; - vlib_set_trace_count (vm, node, n_trace); - } - - /* This depends on the setup but is very consistent - * So I think the CPU branch predictor will make a pretty good job - * at optimizing the decision. */ - if (txvq->desc[desc_current].flags & VRING_DESC_F_INDIRECT) - { - desc_table = map_guest_mem (vui, txvq->desc[desc_current].addr, - &map_hint); - desc_current = 0; - if (PREDICT_FALSE (desc_table == 0)) - { - vlib_error_count (vm, node->node_index, - VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1); - goto out; - } - } - - desc_data_offset = vui->virtio_net_hdr_sz; - - if (enable_csum) - { - virtio_net_hdr_mrg_rxbuf_t *hdr; - u8 *b_data; - u16 current; - - hdr = map_guest_mem (vui, desc_table[desc_current].addr, &map_hint); - if (PREDICT_FALSE (hdr == 0)) - { - vlib_error_count (vm, node->node_index, - VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1); - goto out; - } - if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) - { - if ((desc_data_offset == desc_table[desc_current].len) && - (desc_table[desc_current].flags & VRING_DESC_F_NEXT)) - { - current = desc_table[desc_current].next; - b_data = map_guest_mem (vui, desc_table[current].addr, - &map_hint); - if (PREDICT_FALSE (b_data == 0)) - { - vlib_error_count (vm, node->node_index, - VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, - 1); - goto out; - } - } - else - b_data = (u8 *) hdr + desc_data_offset; - - vhost_user_handle_rx_offload (b_head, b_data, &hdr->hdr); - } - } - - while (1) - { - /* Get more input if necessary. Or end of packet. */ - if (desc_data_offset == desc_table[desc_current].len) - { - if (PREDICT_FALSE (desc_table[desc_current].flags & - VRING_DESC_F_NEXT)) - { - desc_current = desc_table[desc_current].next; - desc_data_offset = 0; - } - else - { - goto out; - } - } - - /* Get more output if necessary. Or end of packet. */ - if (PREDICT_FALSE (b_current->current_length == buffer_data_size)) - { - if (PREDICT_FALSE (cpu->rx_buffers_len == 0)) - { - /* Cancel speculation */ - to_next--; - n_left_to_next++; - - /* - * Checking if there are some left buffers. - * If not, just rewind the used buffers and stop. - * Note: Scheduled copies are not cancelled. This is - * not an issue as they would still be valid. Useless, - * but valid. - */ - vhost_user_input_rewind_buffers (vm, cpu, b_head); - n_left = 0; - goto stop; - } - - /* Get next output */ - cpu->rx_buffers_len--; - u32 bi_next = cpu->rx_buffers[cpu->rx_buffers_len]; - b_current->next_buffer = bi_next; - b_current->flags |= VLIB_BUFFER_NEXT_PRESENT; - bi_current = bi_next; - b_current = vlib_get_buffer (vm, bi_current); - } - - /* Prepare a copy order executed later for the data */ - ASSERT (copy_len < VHOST_USER_COPY_ARRAY_N); - vhost_copy_t *cpy = &cpu->copy[copy_len]; - copy_len++; - u32 desc_data_l = desc_table[desc_current].len - desc_data_offset; - cpy->len = buffer_data_size - b_current->current_length; - cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len; - cpy->dst = (uword) (vlib_buffer_get_current (b_current) + - b_current->current_length); - cpy->src = desc_table[desc_current].addr + desc_data_offset; - - desc_data_offset += cpy->len; - - b_current->current_length += cpy->len; - b_head->total_length_not_including_first_buffer += cpy->len; - } - - out: - - n_rx_bytes += b_head->total_length_not_including_first_buffer; - n_rx_packets++; - - b_head->total_length_not_including_first_buffer -= - b_head->current_length; - - /* consume the descriptor and return it as used */ - last_avail_idx++; - last_used_idx++; - - vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index; - vnet_buffer (b_head)->sw_if_index[VLIB_TX] = (u32) ~ 0; - b_head->error = 0; - - if (current_config_index != ~(u32) 0) - { - b_head->current_config_index = current_config_index; - vnet_buffer (b_head)->feature_arc_index = feature_arc_idx; - } - - n_left--; - - /* - * Although separating memory copies from virtio ring parsing - * is beneficial, we can offer to perform the copies from time - * to time in order to free some space in the ring. - */ - if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD)) - { - if (PREDICT_FALSE (vhost_user_input_copy (vui, cpu->copy, - copy_len, &map_hint))) - { - vlib_error_count (vm, node->node_index, - VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1); - } - copy_len = 0; - - /* give buffers back to driver */ - CLIB_MEMORY_STORE_BARRIER (); - txvq->used->idx = last_used_idx; - vhost_user_log_dirty_ring (vui, txvq, idx); - } - } -stop: - vlib_put_next_frame (vm, node, next_index, n_left_to_next); - - txvq->last_used_idx = last_used_idx; - txvq->last_avail_idx = last_avail_idx; - - /* Do the memory copies */ - if (PREDICT_FALSE (vhost_user_input_copy (vui, cpu->copy, copy_len, - &map_hint))) - { - vlib_error_count (vm, node->node_index, - VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1); - } - - /* give buffers back to driver */ - CLIB_MEMORY_STORE_BARRIER (); - txvq->used->idx = txvq->last_used_idx; - vhost_user_log_dirty_ring (vui, txvq, idx); - - /* interrupt (call) handling */ - if ((txvq->callfd_idx != ~0) && - !(txvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) - { - txvq->n_since_last_int += n_rx_packets; - - if (txvq->n_since_last_int > vum->coalesce_frames) - vhost_user_send_call (vm, vui, txvq); - } - - /* increase rx counters */ - vlib_increment_combined_counter - (vnet_main.interface_main.combined_sw_if_counters - + VNET_INTERFACE_COUNTER_RX, vm->thread_index, vui->sw_if_index, - n_rx_packets, n_rx_bytes); - - vnet_device_increment_rx_packets (vm->thread_index, n_rx_packets); - -done: - return n_rx_packets; -} - -static_always_inline void -vhost_user_mark_desc_consumed (vhost_user_intf_t * vui, - vhost_user_vring_t * txvq, u16 desc_head, - u16 n_descs_processed) -{ - vring_packed_desc_t *desc_table = txvq->packed_desc; - u16 desc_idx; - u16 mask = txvq->qsz_mask; - - for (desc_idx = 0; desc_idx < n_descs_processed; desc_idx++) - { - if (txvq->used_wrap_counter) - desc_table[(desc_head + desc_idx) & mask].flags |= - (VRING_DESC_F_AVAIL | VRING_DESC_F_USED); - else - desc_table[(desc_head + desc_idx) & mask].flags &= - ~(VRING_DESC_F_AVAIL | VRING_DESC_F_USED); - vhost_user_advance_last_used_idx (txvq); - } -} - -static_always_inline void -vhost_user_rx_trace_packed (vhost_trace_t * t, vhost_user_intf_t * vui, - u16 qid, vhost_user_vring_t * txvq, - u16 desc_current) -{ - vhost_user_main_t *vum = &vhost_user_main; - vring_packed_desc_t *hdr_desc; - virtio_net_hdr_mrg_rxbuf_t *hdr; - u32 hint = 0; - - clib_memset (t, 0, sizeof (*t)); - t->device_index = vui - vum->vhost_user_interfaces; - t->qid = qid; - - hdr_desc = &txvq->packed_desc[desc_current]; - if (txvq->packed_desc[desc_current].flags & VRING_DESC_F_INDIRECT) - { - t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT; - /* Header is the first here */ - hdr_desc = map_guest_mem (vui, txvq->packed_desc[desc_current].addr, - &hint); - } - if (txvq->packed_desc[desc_current].flags & VRING_DESC_F_NEXT) - t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED; - - if (!(txvq->packed_desc[desc_current].flags & VRING_DESC_F_NEXT) && - !(txvq->packed_desc[desc_current].flags & VRING_DESC_F_INDIRECT)) - t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC; - - t->first_desc_len = hdr_desc ? hdr_desc->len : 0; - - if (!hdr_desc || !(hdr = map_guest_mem (vui, hdr_desc->addr, &hint))) - t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_MAP_ERROR; - else - { - u32 len = vui->virtio_net_hdr_sz; - clib_memcpy_fast (&t->hdr, hdr, - len > hdr_desc->len ? hdr_desc->len : len); - } -} - -static_always_inline u32 -vhost_user_rx_discard_packet_packed (vlib_main_t * vm, - vhost_user_intf_t * vui, - vhost_user_vring_t * txvq, - u32 discard_max) -{ - u32 discarded_packets = 0; - u16 mask = txvq->qsz_mask; - u16 desc_current, desc_head; - - desc_head = desc_current = txvq->last_used_idx & mask; - - /* - * On the RX side, each packet corresponds to one descriptor - * (it is the same whether it is a shallow descriptor, chained, or indirect). - * Therefore, discarding a packet is like discarding a descriptor. - */ - while ((discarded_packets != discard_max) && - vhost_user_packed_desc_available (txvq, desc_current)) - { - vhost_user_advance_last_avail_idx (txvq); - discarded_packets++; - desc_current = (desc_current + 1) & mask; - } - - if (PREDICT_TRUE (discarded_packets)) - vhost_user_mark_desc_consumed (vui, txvq, desc_head, discarded_packets); - return (discarded_packets); -} - -static_always_inline u32 -vhost_user_input_copy_packed (vhost_user_intf_t * vui, vhost_copy_t * cpy, - u16 copy_len, u32 * map_hint) -{ - void *src0, *src1, *src2, *src3, *src4, *src5, *src6, *src7; - u8 bad; - u32 rc = VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR; - - if (PREDICT_TRUE (copy_len >= 8)) - { - src4 = map_guest_mem (vui, cpy[0].src, map_hint); - src5 = map_guest_mem (vui, cpy[1].src, map_hint); - src6 = map_guest_mem (vui, cpy[2].src, map_hint); - src7 = map_guest_mem (vui, cpy[3].src, map_hint); - bad = (src4 == 0) + (src5 == 0) + (src6 == 0) + (src7 == 0); - if (PREDICT_FALSE (bad)) - goto one_by_one; - clib_prefetch_load (src4); - clib_prefetch_load (src5); - clib_prefetch_load (src6); - clib_prefetch_load (src7); - - while (PREDICT_TRUE (copy_len >= 8)) - { - src0 = src4; - src1 = src5; - src2 = src6; - src3 = src7; - - src4 = map_guest_mem (vui, cpy[4].src, map_hint); - src5 = map_guest_mem (vui, cpy[5].src, map_hint); - src6 = map_guest_mem (vui, cpy[6].src, map_hint); - src7 = map_guest_mem (vui, cpy[7].src, map_hint); - bad = (src4 == 0) + (src5 == 0) + (src6 == 0) + (src7 == 0); - if (PREDICT_FALSE (bad)) - break; - - clib_prefetch_load (src4); - clib_prefetch_load (src5); - clib_prefetch_load (src6); - clib_prefetch_load (src7); - - clib_memcpy_fast ((void *) cpy[0].dst, src0, cpy[0].len); - clib_memcpy_fast ((void *) cpy[1].dst, src1, cpy[1].len); - clib_memcpy_fast ((void *) cpy[2].dst, src2, cpy[2].len); - clib_memcpy_fast ((void *) cpy[3].dst, src3, cpy[3].len); - copy_len -= 4; - cpy += 4; - } - } - -one_by_one: - while (copy_len) - { - if (PREDICT_FALSE (!(src0 = map_guest_mem (vui, cpy->src, map_hint)))) - { - rc = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL; - break; - } - clib_memcpy_fast ((void *) cpy->dst, src0, cpy->len); - copy_len -= 1; - cpy += 1; - } - return rc; -} - -static_always_inline u32 -vhost_user_do_offload (vhost_user_intf_t * vui, - vring_packed_desc_t * desc_table, u16 desc_current, - u16 mask, vlib_buffer_t * b_head, u32 * map_hint) -{ - u32 rc = VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR; - virtio_net_hdr_mrg_rxbuf_t *hdr; - u8 *b_data; - u32 desc_data_offset = vui->virtio_net_hdr_sz; - - hdr = map_guest_mem (vui, desc_table[desc_current].addr, map_hint); - if (PREDICT_FALSE (hdr == 0)) - rc = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL; - else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) - { - if (desc_data_offset == desc_table[desc_current].len) - { - desc_current = (desc_current + 1) & mask; - b_data = - map_guest_mem (vui, desc_table[desc_current].addr, map_hint); - if (PREDICT_FALSE (b_data == 0)) - rc = VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL; - else - vhost_user_handle_rx_offload (b_head, b_data, &hdr->hdr); - } - else - { - b_data = (u8 *) hdr + desc_data_offset; - vhost_user_handle_rx_offload (b_head, b_data, &hdr->hdr); - } - } - - return rc; -} - -static_always_inline u32 -vhost_user_compute_buffers_required (u32 desc_len, u32 buffer_data_size) -{ - div_t result; - u32 buffers_required; - - if (PREDICT_TRUE (buffer_data_size == 2048)) - { - buffers_required = desc_len >> 11; - if ((desc_len & 2047) != 0) - buffers_required++; - return (buffers_required); - } - - if (desc_len < buffer_data_size) - return 1; - - result = div (desc_len, buffer_data_size); - if (result.rem) - buffers_required = result.quot + 1; - else - buffers_required = result.quot; - - return (buffers_required); -} - -static_always_inline u32 -vhost_user_compute_indirect_desc_len (vhost_user_intf_t * vui, - vhost_user_vring_t * txvq, - u32 buffer_data_size, u16 desc_current, - u32 * map_hint) -{ - vring_packed_desc_t *desc_table = txvq->packed_desc; - u32 desc_len = 0; - u16 desc_data_offset = vui->virtio_net_hdr_sz; - u16 desc_idx = desc_current; - u32 n_descs; - - n_descs = desc_table[desc_idx].len >> 4; - desc_table = map_guest_mem (vui, desc_table[desc_idx].addr, map_hint); - if (PREDICT_FALSE (desc_table == 0)) - return 0; - - for (desc_idx = 0; desc_idx < n_descs; desc_idx++) - desc_len += desc_table[desc_idx].len; - - if (PREDICT_TRUE (desc_len > desc_data_offset)) - desc_len -= desc_data_offset; - - return vhost_user_compute_buffers_required (desc_len, buffer_data_size); -} - -static_always_inline u32 -vhost_user_compute_chained_desc_len (vhost_user_intf_t * vui, - vhost_user_vring_t * txvq, - u32 buffer_data_size, u16 * current, - u16 * n_left) -{ - vring_packed_desc_t *desc_table = txvq->packed_desc; - u32 desc_len = 0; - u16 mask = txvq->qsz_mask; - - while (desc_table[*current].flags & VRING_DESC_F_NEXT) - { - desc_len += desc_table[*current].len; - (*n_left)++; - *current = (*current + 1) & mask; - vhost_user_advance_last_avail_idx (txvq); - } - desc_len += desc_table[*current].len; - (*n_left)++; - *current = (*current + 1) & mask; - vhost_user_advance_last_avail_idx (txvq); - - if (PREDICT_TRUE (desc_len > vui->virtio_net_hdr_sz)) - desc_len -= vui->virtio_net_hdr_sz; - - return vhost_user_compute_buffers_required (desc_len, buffer_data_size); -} - -static_always_inline void -vhost_user_assemble_packet (vring_packed_desc_t * desc_table, - u16 * desc_idx, vlib_buffer_t * b_head, - vlib_buffer_t ** b_current, u32 ** next, - vlib_buffer_t *** b, u32 * bi_current, - vhost_cpu_t * cpu, u16 * copy_len, - u32 * buffers_used, u32 buffers_required, - u32 * desc_data_offset, u32 buffer_data_size, - u16 mask) -{ - u32 desc_data_l; - - while (*desc_data_offset < desc_table[*desc_idx].len) - { - /* Get more output if necessary. Or end of packet. */ - if (PREDICT_FALSE ((*b_current)->current_length == buffer_data_size)) - { - /* Get next output */ - u32 bi_next = **next; - (*next)++; - (*b_current)->next_buffer = bi_next; - (*b_current)->flags |= VLIB_BUFFER_NEXT_PRESENT; - *bi_current = bi_next; - *b_current = **b; - (*b)++; - (*buffers_used)++; - ASSERT (*buffers_used <= buffers_required); - } - - /* Prepare a copy order executed later for the data */ - ASSERT (*copy_len < VHOST_USER_COPY_ARRAY_N); - vhost_copy_t *cpy = &cpu->copy[*copy_len]; - (*copy_len)++; - desc_data_l = desc_table[*desc_idx].len - *desc_data_offset; - cpy->len = buffer_data_size - (*b_current)->current_length; - cpy->len = (cpy->len > desc_data_l) ? desc_data_l : cpy->len; - cpy->dst = (uword) (vlib_buffer_get_current (*b_current) + - (*b_current)->current_length); - cpy->src = desc_table[*desc_idx].addr + *desc_data_offset; - - *desc_data_offset += cpy->len; - - (*b_current)->current_length += cpy->len; - b_head->total_length_not_including_first_buffer += cpy->len; - } - *desc_idx = (*desc_idx + 1) & mask;; - *desc_data_offset = 0; -} - -static_always_inline u32 -vhost_user_if_input_packed (vlib_main_t *vm, vhost_user_main_t *vum, - vhost_user_intf_t *vui, u16 qid, - vlib_node_runtime_t *node, u8 enable_csum) -{ - vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)]; - vnet_feature_main_t *fm = &feature_main; - u8 feature_arc_idx = fm->device_input_feature_arc_index; - u16 n_rx_packets = 0; - u32 n_rx_bytes = 0; - u16 n_left = 0; - u32 buffers_required = 0; - u32 n_left_to_next, *to_next; - u32 next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT; - u32 n_trace = vlib_get_trace_count (vm, node); - u32 buffer_data_size = vlib_buffer_get_default_data_size (vm); - u32 map_hint = 0; - vhost_cpu_t *cpu = &vum->cpus[vm->thread_index]; - u16 copy_len = 0; - u32 current_config_index = ~0; - u16 mask = txvq->qsz_mask; - u16 desc_current, desc_head, last_used_idx; - vring_packed_desc_t *desc_table = 0; - u32 n_descs_processed = 0; - u32 rv; - vlib_buffer_t **b; - u32 *next; - u32 buffers_used = 0; - u16 current, n_descs_to_process; - - /* The descriptor table is not ready yet */ - if (PREDICT_FALSE (txvq->packed_desc == 0)) - goto done; - - /* do we have pending interrupts ? */ - vhost_user_vring_t *rxvq = &vui->vrings[VHOST_VRING_IDX_RX (qid)]; - vhost_user_input_do_interrupt (vm, vui, txvq, rxvq); - - /* - * For adaptive mode, it is optimized to reduce interrupts. - * If the scheduler switches the input node to polling due - * to burst of traffic, we tell the driver no interrupt. - * When the traffic subsides, the scheduler switches the node back to - * interrupt mode. We must tell the driver we want interrupt. - */ - if (PREDICT_FALSE (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)) - { - if ((node->flags & - VLIB_NODE_FLAG_SWITCH_FROM_POLLING_TO_INTERRUPT_MODE) || - !(node->flags & - VLIB_NODE_FLAG_SWITCH_FROM_INTERRUPT_TO_POLLING_MODE)) - /* Tell driver we want notification */ - txvq->used_event->flags = 0; - else - /* Tell driver we don't want notification */ - txvq->used_event->flags = VRING_EVENT_F_DISABLE; - } - - last_used_idx = txvq->last_used_idx & mask; - desc_head = desc_current = last_used_idx; - - if (vhost_user_packed_desc_available (txvq, desc_current) == 0) - goto done; - - if (PREDICT_FALSE (!vui->admin_up || !vui->is_ready || !(txvq->enabled))) - { - /* - * Discard input packet if interface is admin down or vring is not - * enabled. - * "For example, for a networking device, in the disabled state - * client must not supply any new RX packets, but must process - * and discard any TX packets." - */ - rv = vhost_user_rx_discard_packet_packed (vm, vui, txvq, - VHOST_USER_DOWN_DISCARD_COUNT); - vlib_error_count (vm, vhost_user_input_node.index, - VHOST_USER_INPUT_FUNC_ERROR_NOT_READY, rv); - goto done; - } - - vhost_user_input_setup_frame (vm, node, vui, ¤t_config_index, - &next_index, &to_next, &n_left_to_next); - - /* - * Compute n_left and total buffers needed - */ - desc_table = txvq->packed_desc; - current = desc_current; - while (vhost_user_packed_desc_available (txvq, current) && - (n_left < VLIB_FRAME_SIZE)) - { - if (desc_table[current].flags & VRING_DESC_F_INDIRECT) - { - buffers_required += - vhost_user_compute_indirect_desc_len (vui, txvq, buffer_data_size, - current, &map_hint); - n_left++; - current = (current + 1) & mask; - vhost_user_advance_last_avail_idx (txvq); - } - else - { - buffers_required += - vhost_user_compute_chained_desc_len (vui, txvq, buffer_data_size, - ¤t, &n_left); - } - } - - /* Something is broken if we need more than 10000 buffers */ - if (PREDICT_FALSE ((buffers_required == 0) || (buffers_required > 10000))) - { - rv = vhost_user_rx_discard_packet_packed (vm, vui, txvq, n_left); - vlib_error_count (vm, vhost_user_input_node.index, - VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, rv); - goto done; - } - - vec_validate (cpu->to_next_list, buffers_required); - rv = vlib_buffer_alloc (vm, cpu->to_next_list, buffers_required); - if (PREDICT_FALSE (rv != buffers_required)) - { - vlib_buffer_free (vm, cpu->to_next_list, rv); - rv = vhost_user_rx_discard_packet_packed (vm, vui, txvq, n_left); - vlib_error_count (vm, vhost_user_input_node.index, - VHOST_USER_INPUT_FUNC_ERROR_NO_BUFFER, rv); - goto done; - } - - next = cpu->to_next_list; - vec_validate (cpu->rx_buffers_pdesc, buffers_required); - vlib_get_buffers (vm, next, cpu->rx_buffers_pdesc, buffers_required); - b = cpu->rx_buffers_pdesc; - n_descs_processed = n_left; - - while (n_left) - { - vlib_buffer_t *b_head, *b_current; - u32 bi_current; - u32 desc_data_offset; - u16 desc_idx = desc_current; - u32 n_descs; - - desc_table = txvq->packed_desc; - to_next[0] = bi_current = next[0]; - b_head = b_current = b[0]; - b++; - buffers_used++; - ASSERT (buffers_used <= buffers_required); - to_next++; - next++; - n_left_to_next--; - - /* The buffer should already be initialized */ - b_head->total_length_not_including_first_buffer = 0; - b_head->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; - desc_data_offset = vui->virtio_net_hdr_sz; - n_descs_to_process = 1; - - if (desc_table[desc_idx].flags & VRING_DESC_F_INDIRECT) - { - n_descs = desc_table[desc_idx].len >> 4; - desc_table = map_guest_mem (vui, desc_table[desc_idx].addr, - &map_hint); - desc_idx = 0; - if (PREDICT_FALSE (desc_table == 0) || - (enable_csum && - (PREDICT_FALSE - (vhost_user_do_offload - (vui, desc_table, desc_idx, mask, b_head, - &map_hint) != VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR)))) - { - vlib_error_count (vm, node->node_index, - VHOST_USER_INPUT_FUNC_ERROR_MMAP_FAIL, 1); - to_next--; - next--; - n_left_to_next++; - buffers_used--; - b--; - goto out; - } - while (n_descs) - { - vhost_user_assemble_packet (desc_table, &desc_idx, b_head, - &b_current, &next, &b, &bi_current, - cpu, ©_len, &buffers_used, - buffers_required, &desc_data_offset, - buffer_data_size, mask); - n_descs--; - } - } - else - { - if (enable_csum) - { - rv = vhost_user_do_offload (vui, desc_table, desc_idx, mask, - b_head, &map_hint); - if (PREDICT_FALSE (rv != VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR)) - { - vlib_error_count (vm, node->node_index, rv, 1); - to_next--; - next--; - n_left_to_next++; - buffers_used--; - b--; - goto out; - } - } - /* - * For chained descriptor, we process all chains in a single while - * loop. So count how many descriptors in the chain. - */ - n_descs_to_process = 1; - while (desc_table[desc_idx].flags & VRING_DESC_F_NEXT) - { - vhost_user_assemble_packet (desc_table, &desc_idx, b_head, - &b_current, &next, &b, &bi_current, - cpu, ©_len, &buffers_used, - buffers_required, &desc_data_offset, - buffer_data_size, mask); - n_descs_to_process++; - } - vhost_user_assemble_packet (desc_table, &desc_idx, b_head, - &b_current, &next, &b, &bi_current, - cpu, ©_len, &buffers_used, - buffers_required, &desc_data_offset, - buffer_data_size, mask); - } - - n_rx_bytes += b_head->total_length_not_including_first_buffer; - n_rx_packets++; - - b_head->total_length_not_including_first_buffer -= - b_head->current_length; - - vnet_buffer (b_head)->sw_if_index[VLIB_RX] = vui->sw_if_index; - vnet_buffer (b_head)->sw_if_index[VLIB_TX] = ~0; - b_head->error = 0; - - if (current_config_index != ~0) - { - b_head->current_config_index = current_config_index; - vnet_buffer (b_head)->feature_arc_index = feature_arc_idx; - } - - out: - ASSERT (n_left >= n_descs_to_process); - n_left -= n_descs_to_process; - - /* advance to next descrptor */ - desc_current = (desc_current + n_descs_to_process) & mask; - - /* - * Although separating memory copies from virtio ring parsing - * is beneficial, we can offer to perform the copies from time - * to time in order to free some space in the ring. - */ - if (PREDICT_FALSE (copy_len >= VHOST_USER_RX_COPY_THRESHOLD)) - { - rv = vhost_user_input_copy_packed (vui, cpu->copy, copy_len, - &map_hint); - if (PREDICT_FALSE (rv != VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR)) - vlib_error_count (vm, node->node_index, rv, 1); - copy_len = 0; - } - } - vlib_put_next_frame (vm, node, next_index, n_left_to_next); - - /* Do the memory copies */ - rv = vhost_user_input_copy_packed (vui, cpu->copy, copy_len, &map_hint); - if (PREDICT_FALSE (rv != VHOST_USER_INPUT_FUNC_ERROR_NO_ERROR)) - vlib_error_count (vm, node->node_index, rv, 1); - - /* Must do the tracing before giving buffers back to driver */ - if (PREDICT_FALSE (n_trace)) - { - u32 left = n_rx_packets; - - b = cpu->rx_buffers_pdesc; - while (n_trace && left) - { - if (PREDICT_TRUE - (vlib_trace_buffer - (vm, node, next_index, b[0], /* follow_chain */ 0))) - { - vhost_trace_t *t0; - t0 = vlib_add_trace (vm, node, b[0], sizeof (t0[0])); - vhost_user_rx_trace_packed (t0, vui, qid, txvq, last_used_idx); - last_used_idx = (last_used_idx + 1) & mask; - n_trace--; - vlib_set_trace_count (vm, node, n_trace); - } - left--; - b++; - } - } - - /* - * Give buffers back to driver. - */ - vhost_user_mark_desc_consumed (vui, txvq, desc_head, n_descs_processed); - - /* interrupt (call) handling */ - if ((txvq->callfd_idx != ~0) && - (txvq->avail_event->flags != VRING_EVENT_F_DISABLE)) - { - txvq->n_since_last_int += n_rx_packets; - if (txvq->n_since_last_int > vum->coalesce_frames) - vhost_user_send_call (vm, vui, txvq); - } - - /* increase rx counters */ - vlib_increment_combined_counter - (vnet_main.interface_main.combined_sw_if_counters - + VNET_INTERFACE_COUNTER_RX, vm->thread_index, vui->sw_if_index, - n_rx_packets, n_rx_bytes); - - vnet_device_increment_rx_packets (vm->thread_index, n_rx_packets); - - if (PREDICT_FALSE (buffers_used < buffers_required)) - vlib_buffer_free (vm, next, buffers_required - buffers_used); - -done: - return n_rx_packets; -} - -VLIB_NODE_FN (vhost_user_input_node) (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame) -{ - vhost_user_main_t *vum = &vhost_user_main; - uword n_rx_packets = 0; - vhost_user_intf_t *vui; - vnet_hw_if_rxq_poll_vector_t *pv = vnet_hw_if_get_rxq_poll_vector (vm, node); - vnet_hw_if_rxq_poll_vector_t *pve; - - vec_foreach (pve, pv) - { - vui = pool_elt_at_index (vum->vhost_user_interfaces, pve->dev_instance); - if (vhost_user_is_packed_ring_supported (vui)) - { - if (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)) - n_rx_packets += vhost_user_if_input_packed ( - vm, vum, vui, pve->queue_id, node, 1); - else - n_rx_packets += vhost_user_if_input_packed ( - vm, vum, vui, pve->queue_id, node, 0); - } - else - { - if (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_CSUM)) - n_rx_packets += - vhost_user_if_input (vm, vum, vui, pve->queue_id, node, 1); - else - n_rx_packets += - vhost_user_if_input (vm, vum, vui, pve->queue_id, node, 0); - } - } - - return n_rx_packets; -} - -/* *INDENT-OFF* */ -VLIB_REGISTER_NODE (vhost_user_input_node) = { - .type = VLIB_NODE_TYPE_INPUT, - .name = "vhost-user-input", - .sibling_of = "device-input", - .flags = VLIB_NODE_FLAG_TRACE_SUPPORTED, - - /* Will be enabled if/when hardware is detected. */ - .state = VLIB_NODE_STATE_DISABLED, - - .format_buffer = format_ethernet_header_with_length, - .format_trace = format_vhost_trace, - - .n_errors = VHOST_USER_INPUT_FUNC_N_ERROR, - .error_strings = vhost_user_input_func_error_strings, -}; -/* *INDENT-ON* */ - -/* - * fd.io coding-style-patch-verification: ON - * - * Local Variables: - * eval: (c-set-style "gnu") - * End: - */ diff --git a/src/vnet/devices/virtio/vhost_user_output.c b/src/vnet/devices/virtio/vhost_user_output.c deleted file mode 100644 index 4efafa85333..00000000000 --- a/src/vnet/devices/virtio/vhost_user_output.c +++ /dev/null @@ -1,1130 +0,0 @@ -/* - *------------------------------------------------------------------ - * vhost-user-output - * - * Copyright (c) 2014-2018 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 <stddef.h> -#include <fcntl.h> /* for open */ -#include <sys/ioctl.h> -#include <sys/socket.h> -#include <sys/un.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <sys/uio.h> /* for iovec */ -#include <netinet/in.h> -#include <sys/vfs.h> - -#include <linux/if_arp.h> -#include <linux/if_tun.h> - -#include <vlib/vlib.h> -#include <vlib/unix/unix.h> - -#include <vnet/ethernet/ethernet.h> -#include <vnet/devices/devices.h> -#include <vnet/feature/feature.h> - -#include <vnet/devices/virtio/vhost_user.h> -#include <vnet/devices/virtio/vhost_user_inline.h> - -#include <vnet/gso/hdr_offset_parser.h> -/* - * On the transmit side, we keep processing the buffers from vlib in the while - * loop and prepare the copy order to be executed later. However, the static - * array which we keep the copy order is limited to VHOST_USER_COPY_ARRAY_N - * entries. In order to not corrupt memory, we have to do the copy when the - * static array reaches the copy threshold. We subtract 40 in case the code - * goes into the inner loop for a maximum of 64k frames which may require - * more array entries. We subtract 200 because our default buffer size is - * 2048 and the default desc len is likely 1536. While it takes less than 40 - * vlib buffers for the jumbo frame, it may take twice as much descriptors - * for the same jumbo frame. Use 200 for the extra head room. - */ -#define VHOST_USER_TX_COPY_THRESHOLD (VHOST_USER_COPY_ARRAY_N - 200) - -extern vnet_device_class_t vhost_user_device_class; - -#define foreach_vhost_user_tx_func_error \ - _(NONE, "no error") \ - _(NOT_READY, "vhost vring not ready") \ - _(DOWN, "vhost interface is down") \ - _(PKT_DROP_NOBUF, "tx packet drops (no available descriptors)") \ - _(PKT_DROP_NOMRG, "tx packet drops (cannot merge descriptors)") \ - _(MMAP_FAIL, "mmap failure") \ - _(INDIRECT_OVERFLOW, "indirect descriptor table overflow") - -typedef enum -{ -#define _(f,s) VHOST_USER_TX_FUNC_ERROR_##f, - foreach_vhost_user_tx_func_error -#undef _ - VHOST_USER_TX_FUNC_N_ERROR, -} vhost_user_tx_func_error_t; - -static __clib_unused char *vhost_user_tx_func_error_strings[] = { -#define _(n,s) s, - foreach_vhost_user_tx_func_error -#undef _ -}; - -static __clib_unused u8 * -format_vhost_user_interface_name (u8 * s, va_list * args) -{ - u32 i = va_arg (*args, u32); - u32 show_dev_instance = ~0; - vhost_user_main_t *vum = &vhost_user_main; - - if (i < vec_len (vum->show_dev_instance_by_real_dev_instance)) - show_dev_instance = vum->show_dev_instance_by_real_dev_instance[i]; - - if (show_dev_instance != ~0) - i = show_dev_instance; - - s = format (s, "VirtualEthernet0/0/%d", i); - return s; -} - -static __clib_unused int -vhost_user_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance) -{ - // FIXME: check if the new dev instance is already used - vhost_user_main_t *vum = &vhost_user_main; - vhost_user_intf_t *vui = pool_elt_at_index (vum->vhost_user_interfaces, - hi->dev_instance); - - vec_validate_init_empty (vum->show_dev_instance_by_real_dev_instance, - hi->dev_instance, ~0); - - vum->show_dev_instance_by_real_dev_instance[hi->dev_instance] = - new_dev_instance; - - vu_log_debug (vui, "renumbered vhost-user interface dev_instance %d to %d", - hi->dev_instance, new_dev_instance); - - return 0; -} - -static_always_inline void -vhost_user_tx_trace (vhost_trace_t * t, - vhost_user_intf_t * vui, u16 qid, - vlib_buffer_t * b, vhost_user_vring_t * rxvq) -{ - vhost_user_main_t *vum = &vhost_user_main; - u32 last_avail_idx = rxvq->last_avail_idx; - u32 desc_current = rxvq->avail->ring[last_avail_idx & rxvq->qsz_mask]; - vring_desc_t *hdr_desc = 0; - u32 hint = 0; - - clib_memset (t, 0, sizeof (*t)); - t->device_index = vui - vum->vhost_user_interfaces; - t->qid = qid; - - hdr_desc = &rxvq->desc[desc_current]; - if (rxvq->desc[desc_current].flags & VRING_DESC_F_INDIRECT) - { - t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT; - /* Header is the first here */ - hdr_desc = map_guest_mem (vui, rxvq->desc[desc_current].addr, &hint); - } - if (rxvq->desc[desc_current].flags & VRING_DESC_F_NEXT) - { - t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED; - } - if (!(rxvq->desc[desc_current].flags & VRING_DESC_F_NEXT) && - !(rxvq->desc[desc_current].flags & VRING_DESC_F_INDIRECT)) - { - t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC; - } - - t->first_desc_len = hdr_desc ? hdr_desc->len : 0; -} - -static_always_inline u32 -vhost_user_tx_copy (vhost_user_intf_t * vui, vhost_copy_t * cpy, - u16 copy_len, u32 * map_hint) -{ - void *dst0, *dst1, *dst2, *dst3; - if (PREDICT_TRUE (copy_len >= 4)) - { - if (PREDICT_FALSE (!(dst2 = map_guest_mem (vui, cpy[0].dst, map_hint)))) - return 1; - if (PREDICT_FALSE (!(dst3 = map_guest_mem (vui, cpy[1].dst, map_hint)))) - return 1; - while (PREDICT_TRUE (copy_len >= 4)) - { - dst0 = dst2; - dst1 = dst3; - - if (PREDICT_FALSE - (!(dst2 = map_guest_mem (vui, cpy[2].dst, map_hint)))) - return 1; - if (PREDICT_FALSE - (!(dst3 = map_guest_mem (vui, cpy[3].dst, map_hint)))) - return 1; - - clib_prefetch_load ((void *) cpy[2].src); - clib_prefetch_load ((void *) cpy[3].src); - - clib_memcpy_fast (dst0, (void *) cpy[0].src, cpy[0].len); - clib_memcpy_fast (dst1, (void *) cpy[1].src, cpy[1].len); - - vhost_user_log_dirty_pages_2 (vui, cpy[0].dst, cpy[0].len, 1); - vhost_user_log_dirty_pages_2 (vui, cpy[1].dst, cpy[1].len, 1); - copy_len -= 2; - cpy += 2; - } - } - while (copy_len) - { - if (PREDICT_FALSE (!(dst0 = map_guest_mem (vui, cpy->dst, map_hint)))) - return 1; - clib_memcpy_fast (dst0, (void *) cpy->src, cpy->len); - vhost_user_log_dirty_pages_2 (vui, cpy->dst, cpy->len, 1); - copy_len -= 1; - cpy += 1; - } - return 0; -} - -static_always_inline void -vhost_user_handle_tx_offload (vhost_user_intf_t * vui, vlib_buffer_t * b, - virtio_net_hdr_t * hdr) -{ - generic_header_offset_t gho = { 0 }; - int is_ip4 = b->flags & VNET_BUFFER_F_IS_IP4; - int is_ip6 = b->flags & VNET_BUFFER_F_IS_IP6; - vnet_buffer_oflags_t oflags = vnet_buffer (b)->oflags; - - ASSERT (!(is_ip4 && is_ip6)); - vnet_generic_header_offset_parser (b, &gho, 1 /* l2 */ , is_ip4, is_ip6); - if (oflags & VNET_BUFFER_OFFLOAD_F_IP_CKSUM) - { - ip4_header_t *ip4; - - ip4 = - (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset); - ip4->checksum = ip4_header_checksum (ip4); - } - - /* checksum offload */ - if (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM) - { - hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; - hdr->csum_start = gho.l4_hdr_offset; - hdr->csum_offset = offsetof (udp_header_t, checksum); - } - else if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM) - { - hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; - hdr->csum_start = gho.l4_hdr_offset; - hdr->csum_offset = offsetof (tcp_header_t, checksum); - } - - /* GSO offload */ - if (b->flags & VNET_BUFFER_F_GSO) - { - if (oflags & VNET_BUFFER_OFFLOAD_F_TCP_CKSUM) - { - if (is_ip4 && - (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4))) - { - hdr->gso_size = vnet_buffer2 (b)->gso_size; - hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; - } - else if (is_ip6 && - (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6))) - { - hdr->gso_size = vnet_buffer2 (b)->gso_size; - hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; - } - } - else if ((vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO)) && - (oflags & VNET_BUFFER_OFFLOAD_F_UDP_CKSUM)) - { - hdr->gso_size = vnet_buffer2 (b)->gso_size; - hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP; - } - } -} - -static_always_inline void -vhost_user_mark_desc_available (vlib_main_t * vm, vhost_user_intf_t * vui, - vhost_user_vring_t * rxvq, - u16 * n_descs_processed, u8 chained, - vlib_frame_t * frame, u32 n_left) -{ - u16 desc_idx, flags; - vring_packed_desc_t *desc_table = rxvq->packed_desc; - u16 last_used_idx = rxvq->last_used_idx; - - if (PREDICT_FALSE (*n_descs_processed == 0)) - return; - - if (rxvq->used_wrap_counter) - flags = desc_table[last_used_idx & rxvq->qsz_mask].flags | - (VRING_DESC_F_AVAIL | VRING_DESC_F_USED); - else - flags = desc_table[last_used_idx & rxvq->qsz_mask].flags & - ~(VRING_DESC_F_AVAIL | VRING_DESC_F_USED); - - vhost_user_advance_last_used_idx (rxvq); - - for (desc_idx = 1; desc_idx < *n_descs_processed; desc_idx++) - { - if (rxvq->used_wrap_counter) - desc_table[rxvq->last_used_idx & rxvq->qsz_mask].flags |= - (VRING_DESC_F_AVAIL | VRING_DESC_F_USED); - else - desc_table[rxvq->last_used_idx & rxvq->qsz_mask].flags &= - ~(VRING_DESC_F_AVAIL | VRING_DESC_F_USED); - vhost_user_advance_last_used_idx (rxvq); - } - - desc_table[last_used_idx & rxvq->qsz_mask].flags = flags; - - *n_descs_processed = 0; - - if (chained) - { - vring_packed_desc_t *desc_table = rxvq->packed_desc; - - while (desc_table[rxvq->last_used_idx & rxvq->qsz_mask].flags & - VRING_DESC_F_NEXT) - vhost_user_advance_last_used_idx (rxvq); - - /* Advance past the current chained table entries */ - vhost_user_advance_last_used_idx (rxvq); - } - - /* interrupt (call) handling */ - if ((rxvq->callfd_idx != ~0) && - (rxvq->avail_event->flags != VRING_EVENT_F_DISABLE)) - { - vhost_user_main_t *vum = &vhost_user_main; - - rxvq->n_since_last_int += frame->n_vectors - n_left; - if (rxvq->n_since_last_int > vum->coalesce_frames) - vhost_user_send_call (vm, vui, rxvq); - } -} - -static_always_inline void -vhost_user_tx_trace_packed (vhost_trace_t * t, vhost_user_intf_t * vui, - u16 qid, vlib_buffer_t * b, - vhost_user_vring_t * rxvq) -{ - vhost_user_main_t *vum = &vhost_user_main; - u32 last_avail_idx = rxvq->last_avail_idx; - u32 desc_current = last_avail_idx & rxvq->qsz_mask; - vring_packed_desc_t *hdr_desc = 0; - u32 hint = 0; - - clib_memset (t, 0, sizeof (*t)); - t->device_index = vui - vum->vhost_user_interfaces; - t->qid = qid; - - hdr_desc = &rxvq->packed_desc[desc_current]; - if (rxvq->packed_desc[desc_current].flags & VRING_DESC_F_INDIRECT) - { - t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_INDIRECT; - /* Header is the first here */ - hdr_desc = map_guest_mem (vui, rxvq->packed_desc[desc_current].addr, - &hint); - } - if (rxvq->packed_desc[desc_current].flags & VRING_DESC_F_NEXT) - { - t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SIMPLE_CHAINED; - } - if (!(rxvq->packed_desc[desc_current].flags & VRING_DESC_F_NEXT) && - !(rxvq->packed_desc[desc_current].flags & VRING_DESC_F_INDIRECT)) - { - t->virtio_ring_flags |= 1 << VIRTIO_TRACE_F_SINGLE_DESC; - } - - t->first_desc_len = hdr_desc ? hdr_desc->len : 0; -} - -static_always_inline uword -vhost_user_device_class_packed (vlib_main_t *vm, vlib_node_runtime_t *node, - vlib_frame_t *frame, vhost_user_intf_t *vui, - vhost_user_vring_t *rxvq) -{ - u32 *buffers = vlib_frame_vector_args (frame); - u32 n_left = frame->n_vectors; - vhost_user_main_t *vum = &vhost_user_main; - u32 qid = rxvq->qid; - u8 error; - u32 thread_index = vm->thread_index; - vhost_cpu_t *cpu = &vum->cpus[thread_index]; - u32 map_hint = 0; - u8 retry = 8; - u16 copy_len; - u16 tx_headers_len; - vring_packed_desc_t *desc_table; - u32 or_flags; - u16 desc_head, desc_index, desc_len; - u16 n_descs_processed; - u8 indirect, chained; - -retry: - error = VHOST_USER_TX_FUNC_ERROR_NONE; - tx_headers_len = 0; - copy_len = 0; - n_descs_processed = 0; - - while (n_left > 0) - { - vlib_buffer_t *b0, *current_b0; - uword buffer_map_addr; - u32 buffer_len; - u16 bytes_left; - u32 total_desc_len = 0; - u16 n_entries = 0; - - indirect = 0; - chained = 0; - if (PREDICT_TRUE (n_left > 1)) - vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD); - - b0 = vlib_get_buffer (vm, buffers[0]); - if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) - { - cpu->current_trace = vlib_add_trace (vm, node, b0, - sizeof (*cpu->current_trace)); - vhost_user_tx_trace_packed (cpu->current_trace, vui, qid / 2, b0, - rxvq); - } - - desc_table = rxvq->packed_desc; - desc_head = desc_index = rxvq->last_avail_idx & rxvq->qsz_mask; - if (PREDICT_FALSE (!vhost_user_packed_desc_available (rxvq, desc_head))) - { - error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF; - goto done; - } - /* - * Go deeper in case of indirect descriptor. - * To test it, turn off mrg_rxbuf. - */ - if (desc_table[desc_head].flags & VRING_DESC_F_INDIRECT) - { - indirect = 1; - if (PREDICT_FALSE (desc_table[desc_head].len < - sizeof (vring_packed_desc_t))) - { - error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW; - goto done; - } - n_entries = desc_table[desc_head].len >> 4; - desc_table = map_guest_mem (vui, desc_table[desc_index].addr, - &map_hint); - if (PREDICT_FALSE (desc_table == 0)) - { - error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL; - goto done; - } - desc_index = 0; - } - else if (rxvq->packed_desc[desc_head].flags & VRING_DESC_F_NEXT) - chained = 1; - - desc_len = vui->virtio_net_hdr_sz; - buffer_map_addr = desc_table[desc_index].addr; - buffer_len = desc_table[desc_index].len; - - /* Get a header from the header array */ - virtio_net_hdr_mrg_rxbuf_t *hdr = &cpu->tx_headers[tx_headers_len]; - tx_headers_len++; - hdr->hdr.flags = 0; - hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE; - hdr->num_buffers = 1; - - or_flags = (b0->flags & VNET_BUFFER_F_OFFLOAD); - - /* Guest supports csum offload and buffer requires checksum offload? */ - if (or_flags && - (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM))) - vhost_user_handle_tx_offload (vui, b0, &hdr->hdr); - - /* Prepare a copy order executed later for the header */ - ASSERT (copy_len < VHOST_USER_COPY_ARRAY_N); - vhost_copy_t *cpy = &cpu->copy[copy_len]; - copy_len++; - cpy->len = vui->virtio_net_hdr_sz; - cpy->dst = buffer_map_addr; - cpy->src = (uword) hdr; - - buffer_map_addr += vui->virtio_net_hdr_sz; - buffer_len -= vui->virtio_net_hdr_sz; - bytes_left = b0->current_length; - current_b0 = b0; - while (1) - { - if (buffer_len == 0) - { - /* Get new output */ - if (chained) - { - /* - * Next one is chained - * Test it with both indirect and mrg_rxbuf off - */ - if (PREDICT_FALSE (!(desc_table[desc_index].flags & - VRING_DESC_F_NEXT))) - { - /* - * Last descriptor in chain. - * Dequeue queued descriptors for this packet - */ - vhost_user_dequeue_chained_descs (rxvq, - &n_descs_processed); - error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF; - goto done; - } - vhost_user_advance_last_avail_idx (rxvq); - desc_index = rxvq->last_avail_idx & rxvq->qsz_mask; - n_descs_processed++; - buffer_map_addr = desc_table[desc_index].addr; - buffer_len = desc_table[desc_index].len; - total_desc_len += desc_len; - desc_len = 0; - } - else if (indirect) - { - /* - * Indirect table - * Test it with mrg_rxnuf off - */ - if (PREDICT_TRUE (n_entries > 0)) - n_entries--; - else - { - /* Dequeue queued descriptors for this packet */ - vhost_user_dequeue_chained_descs (rxvq, - &n_descs_processed); - error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW; - goto done; - } - total_desc_len += desc_len; - desc_index = (desc_index + 1) & rxvq->qsz_mask; - buffer_map_addr = desc_table[desc_index].addr; - buffer_len = desc_table[desc_index].len; - desc_len = 0; - } - else if (vui->virtio_net_hdr_sz == 12) - { - /* - * MRG is available - * This is the default setting for the guest VM - */ - virtio_net_hdr_mrg_rxbuf_t *hdr = - &cpu->tx_headers[tx_headers_len - 1]; - - desc_table[desc_index].len = desc_len; - vhost_user_advance_last_avail_idx (rxvq); - desc_head = desc_index = - rxvq->last_avail_idx & rxvq->qsz_mask; - hdr->num_buffers++; - n_descs_processed++; - desc_len = 0; - - if (PREDICT_FALSE (!vhost_user_packed_desc_available - (rxvq, desc_index))) - { - /* Dequeue queued descriptors for this packet */ - vhost_user_dequeue_descs (rxvq, hdr, - &n_descs_processed); - error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF; - goto done; - } - - buffer_map_addr = desc_table[desc_index].addr; - buffer_len = desc_table[desc_index].len; - } - else - { - error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG; - goto done; - } - } - - ASSERT (copy_len < VHOST_USER_COPY_ARRAY_N); - vhost_copy_t *cpy = &cpu->copy[copy_len]; - copy_len++; - cpy->len = bytes_left; - cpy->len = (cpy->len > buffer_len) ? buffer_len : cpy->len; - cpy->dst = buffer_map_addr; - cpy->src = (uword) vlib_buffer_get_current (current_b0) + - current_b0->current_length - bytes_left; - - bytes_left -= cpy->len; - buffer_len -= cpy->len; - buffer_map_addr += cpy->len; - desc_len += cpy->len; - - clib_prefetch_load (&rxvq->packed_desc); - - /* Check if vlib buffer has more data. If not, get more or break */ - if (PREDICT_TRUE (!bytes_left)) - { - if (PREDICT_FALSE - (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT)) - { - current_b0 = vlib_get_buffer (vm, current_b0->next_buffer); - bytes_left = current_b0->current_length; - } - else - { - /* End of packet */ - break; - } - } - } - - /* Move from available to used ring */ - total_desc_len += desc_len; - rxvq->packed_desc[desc_head].len = total_desc_len; - - vhost_user_advance_last_avail_table_idx (vui, rxvq, chained); - n_descs_processed++; - - if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) - cpu->current_trace->hdr = cpu->tx_headers[tx_headers_len - 1]; - - n_left--; - - /* - * Do the copy periodically to prevent - * cpu->copy array overflow and corrupt memory - */ - if (PREDICT_FALSE (copy_len >= VHOST_USER_TX_COPY_THRESHOLD) || chained) - { - if (PREDICT_FALSE (vhost_user_tx_copy (vui, cpu->copy, copy_len, - &map_hint))) - vlib_error_count (vm, node->node_index, - VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1); - copy_len = 0; - - /* give buffers back to driver */ - vhost_user_mark_desc_available (vm, vui, rxvq, &n_descs_processed, - chained, frame, n_left); - } - - buffers++; - } - -done: - if (PREDICT_TRUE (copy_len)) - { - if (PREDICT_FALSE (vhost_user_tx_copy (vui, cpu->copy, copy_len, - &map_hint))) - vlib_error_count (vm, node->node_index, - VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1); - - vhost_user_mark_desc_available (vm, vui, rxvq, &n_descs_processed, - chained, frame, n_left); - } - - /* - * When n_left is set, error is always set to something too. - * In case error is due to lack of remaining buffers, we go back up and - * retry. - * The idea is that it is better to waste some time on packets - * that have been processed already than dropping them and get - * more fresh packets with a good likelyhood that they will be dropped too. - * This technique also gives more time to VM driver to pick-up packets. - * In case the traffic flows from physical to virtual interfaces, this - * technique will end-up leveraging the physical NIC buffer in order to - * absorb the VM's CPU jitter. - */ - if (n_left && (error == VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF) && retry) - { - retry--; - goto retry; - } - - clib_spinlock_unlock (&rxvq->vring_lock); - - if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE)) - { - vlib_error_count (vm, node->node_index, error, n_left); - vlib_increment_simple_counter - (vnet_main.interface_main.sw_if_counters + - VNET_INTERFACE_COUNTER_DROP, thread_index, vui->sw_if_index, n_left); - } - - vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors); - return frame->n_vectors; -} - -VNET_DEVICE_CLASS_TX_FN (vhost_user_device_class) (vlib_main_t * vm, - vlib_node_runtime_t * - node, vlib_frame_t * frame) -{ - u32 *buffers = vlib_frame_vector_args (frame); - u32 n_left = frame->n_vectors; - vhost_user_main_t *vum = &vhost_user_main; - vnet_interface_output_runtime_t *rd = (void *) node->runtime_data; - vhost_user_intf_t *vui = - pool_elt_at_index (vum->vhost_user_interfaces, rd->dev_instance); - u32 qid; - vhost_user_vring_t *rxvq; - u8 error; - u32 thread_index = vm->thread_index; - vhost_cpu_t *cpu = &vum->cpus[thread_index]; - u32 map_hint = 0; - u8 retry = 8; - u16 copy_len; - u16 tx_headers_len; - u32 or_flags; - vnet_hw_if_tx_frame_t *tf = vlib_frame_scalar_args (frame); - - if (PREDICT_FALSE (!vui->admin_up)) - { - error = VHOST_USER_TX_FUNC_ERROR_DOWN; - goto done3; - } - - if (PREDICT_FALSE (!vui->is_ready)) - { - error = VHOST_USER_TX_FUNC_ERROR_NOT_READY; - goto done3; - } - - qid = VHOST_VRING_IDX_RX (tf->queue_id); - rxvq = &vui->vrings[qid]; - ASSERT (tf->queue_id == rxvq->qid); - - if (PREDICT_FALSE (rxvq->avail == 0)) - { - error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL; - goto done3; - } - if (tf->shared_queue) - clib_spinlock_lock (&rxvq->vring_lock); - - if (vhost_user_is_packed_ring_supported (vui)) - return (vhost_user_device_class_packed (vm, node, frame, vui, rxvq)); - -retry: - error = VHOST_USER_TX_FUNC_ERROR_NONE; - tx_headers_len = 0; - copy_len = 0; - while (n_left > 0) - { - vlib_buffer_t *b0, *current_b0; - u16 desc_head, desc_index, desc_len; - vring_desc_t *desc_table; - uword buffer_map_addr; - u32 buffer_len; - u16 bytes_left; - - if (PREDICT_TRUE (n_left > 1)) - vlib_prefetch_buffer_with_index (vm, buffers[1], LOAD); - - b0 = vlib_get_buffer (vm, buffers[0]); - - if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) - { - cpu->current_trace = vlib_add_trace (vm, node, b0, - sizeof (*cpu->current_trace)); - vhost_user_tx_trace (cpu->current_trace, vui, qid / 2, b0, rxvq); - } - - if (PREDICT_FALSE (rxvq->last_avail_idx == rxvq->avail->idx)) - { - error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF; - goto done; - } - - desc_table = rxvq->desc; - desc_head = desc_index = - rxvq->avail->ring[rxvq->last_avail_idx & rxvq->qsz_mask]; - - /* Go deeper in case of indirect descriptor - * I don't know of any driver providing indirect for RX. */ - if (PREDICT_FALSE (rxvq->desc[desc_head].flags & VRING_DESC_F_INDIRECT)) - { - if (PREDICT_FALSE - (rxvq->desc[desc_head].len < sizeof (vring_desc_t))) - { - error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW; - goto done; - } - if (PREDICT_FALSE - (!(desc_table = - map_guest_mem (vui, rxvq->desc[desc_index].addr, - &map_hint)))) - { - error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL; - goto done; - } - desc_index = 0; - } - - desc_len = vui->virtio_net_hdr_sz; - buffer_map_addr = desc_table[desc_index].addr; - buffer_len = desc_table[desc_index].len; - - { - // Get a header from the header array - virtio_net_hdr_mrg_rxbuf_t *hdr = &cpu->tx_headers[tx_headers_len]; - tx_headers_len++; - hdr->hdr.flags = 0; - hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE; - hdr->num_buffers = 1; //This is local, no need to check - - or_flags = (b0->flags & VNET_BUFFER_F_OFFLOAD); - - /* Guest supports csum offload and buffer requires checksum offload? */ - if (or_flags - && (vui->features & VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM))) - vhost_user_handle_tx_offload (vui, b0, &hdr->hdr); - - // Prepare a copy order executed later for the header - ASSERT (copy_len < VHOST_USER_COPY_ARRAY_N); - vhost_copy_t *cpy = &cpu->copy[copy_len]; - copy_len++; - cpy->len = vui->virtio_net_hdr_sz; - cpy->dst = buffer_map_addr; - cpy->src = (uword) hdr; - } - - buffer_map_addr += vui->virtio_net_hdr_sz; - buffer_len -= vui->virtio_net_hdr_sz; - bytes_left = b0->current_length; - current_b0 = b0; - while (1) - { - if (buffer_len == 0) - { //Get new output - if (desc_table[desc_index].flags & VRING_DESC_F_NEXT) - { - //Next one is chained - desc_index = desc_table[desc_index].next; - buffer_map_addr = desc_table[desc_index].addr; - buffer_len = desc_table[desc_index].len; - } - else if (vui->virtio_net_hdr_sz == 12) //MRG is available - { - virtio_net_hdr_mrg_rxbuf_t *hdr = - &cpu->tx_headers[tx_headers_len - 1]; - - //Move from available to used buffer - rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].id = - desc_head; - rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].len = - desc_len; - vhost_user_log_dirty_ring (vui, rxvq, - ring[rxvq->last_used_idx & - rxvq->qsz_mask]); - - rxvq->last_avail_idx++; - rxvq->last_used_idx++; - hdr->num_buffers++; - desc_len = 0; - - if (PREDICT_FALSE - (rxvq->last_avail_idx == rxvq->avail->idx)) - { - //Dequeue queued descriptors for this packet - rxvq->last_used_idx -= hdr->num_buffers - 1; - rxvq->last_avail_idx -= hdr->num_buffers - 1; - error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF; - goto done; - } - - desc_table = rxvq->desc; - desc_head = desc_index = - rxvq->avail->ring[rxvq->last_avail_idx & rxvq->qsz_mask]; - if (PREDICT_FALSE - (rxvq->desc[desc_head].flags & VRING_DESC_F_INDIRECT)) - { - //It is seriously unlikely that a driver will put indirect descriptor - //after non-indirect descriptor. - if (PREDICT_FALSE - (rxvq->desc[desc_head].len < sizeof (vring_desc_t))) - { - error = VHOST_USER_TX_FUNC_ERROR_INDIRECT_OVERFLOW; - goto done; - } - if (PREDICT_FALSE - (!(desc_table = - map_guest_mem (vui, - rxvq->desc[desc_index].addr, - &map_hint)))) - { - error = VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL; - goto done; - } - desc_index = 0; - } - buffer_map_addr = desc_table[desc_index].addr; - buffer_len = desc_table[desc_index].len; - } - else - { - error = VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOMRG; - goto done; - } - } - - { - ASSERT (copy_len < VHOST_USER_COPY_ARRAY_N); - vhost_copy_t *cpy = &cpu->copy[copy_len]; - copy_len++; - cpy->len = bytes_left; - cpy->len = (cpy->len > buffer_len) ? buffer_len : cpy->len; - cpy->dst = buffer_map_addr; - cpy->src = (uword) vlib_buffer_get_current (current_b0) + - current_b0->current_length - bytes_left; - - bytes_left -= cpy->len; - buffer_len -= cpy->len; - buffer_map_addr += cpy->len; - desc_len += cpy->len; - - clib_prefetch_load (&rxvq->desc); - } - - // Check if vlib buffer has more data. If not, get more or break. - if (PREDICT_TRUE (!bytes_left)) - { - if (PREDICT_FALSE - (current_b0->flags & VLIB_BUFFER_NEXT_PRESENT)) - { - current_b0 = vlib_get_buffer (vm, current_b0->next_buffer); - bytes_left = current_b0->current_length; - } - else - { - //End of packet - break; - } - } - } - - //Move from available to used ring - rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].id = desc_head; - rxvq->used->ring[rxvq->last_used_idx & rxvq->qsz_mask].len = desc_len; - vhost_user_log_dirty_ring (vui, rxvq, - ring[rxvq->last_used_idx & rxvq->qsz_mask]); - rxvq->last_avail_idx++; - rxvq->last_used_idx++; - - if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) - { - cpu->current_trace->hdr = cpu->tx_headers[tx_headers_len - 1]; - } - - n_left--; //At the end for error counting when 'goto done' is invoked - - /* - * Do the copy periodically to prevent - * cpu->copy array overflow and corrupt memory - */ - if (PREDICT_FALSE (copy_len >= VHOST_USER_TX_COPY_THRESHOLD)) - { - if (PREDICT_FALSE (vhost_user_tx_copy (vui, cpu->copy, copy_len, - &map_hint))) - { - vlib_error_count (vm, node->node_index, - VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1); - } - copy_len = 0; - - /* give buffers back to driver */ - CLIB_MEMORY_BARRIER (); - rxvq->used->idx = rxvq->last_used_idx; - vhost_user_log_dirty_ring (vui, rxvq, idx); - } - buffers++; - } - -done: - //Do the memory copies - if (PREDICT_FALSE (vhost_user_tx_copy (vui, cpu->copy, copy_len, - &map_hint))) - { - vlib_error_count (vm, node->node_index, - VHOST_USER_TX_FUNC_ERROR_MMAP_FAIL, 1); - } - - CLIB_MEMORY_BARRIER (); - rxvq->used->idx = rxvq->last_used_idx; - vhost_user_log_dirty_ring (vui, rxvq, idx); - - /* - * When n_left is set, error is always set to something too. - * In case error is due to lack of remaining buffers, we go back up and - * retry. - * The idea is that it is better to waste some time on packets - * that have been processed already than dropping them and get - * more fresh packets with a good likelihood that they will be dropped too. - * This technique also gives more time to VM driver to pick-up packets. - * In case the traffic flows from physical to virtual interfaces, this - * technique will end-up leveraging the physical NIC buffer in order to - * absorb the VM's CPU jitter. - */ - if (n_left && (error == VHOST_USER_TX_FUNC_ERROR_PKT_DROP_NOBUF) && retry) - { - retry--; - goto retry; - } - - /* interrupt (call) handling */ - if ((rxvq->callfd_idx != ~0) && - !(rxvq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) - { - rxvq->n_since_last_int += frame->n_vectors - n_left; - - if (rxvq->n_since_last_int > vum->coalesce_frames) - vhost_user_send_call (vm, vui, rxvq); - } - - clib_spinlock_unlock (&rxvq->vring_lock); - -done3: - if (PREDICT_FALSE (n_left && error != VHOST_USER_TX_FUNC_ERROR_NONE)) - { - vlib_error_count (vm, node->node_index, error, n_left); - vlib_increment_simple_counter - (vnet_main.interface_main.sw_if_counters - + VNET_INTERFACE_COUNTER_DROP, - thread_index, vui->sw_if_index, n_left); - } - - vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors); - return frame->n_vectors; -} - -static __clib_unused clib_error_t * -vhost_user_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, - u32 qid, vnet_hw_if_rx_mode mode) -{ - vlib_main_t *vm = vnm->vlib_main; - vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index); - vhost_user_main_t *vum = &vhost_user_main; - vhost_user_intf_t *vui = - pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance); - vhost_user_vring_t *txvq = &vui->vrings[VHOST_VRING_IDX_TX (qid)]; - vhost_cpu_t *cpu; - - if (mode == txvq->mode) - return 0; - - if ((mode != VNET_HW_IF_RX_MODE_POLLING) && - (mode != VNET_HW_IF_RX_MODE_ADAPTIVE) && - (mode != VNET_HW_IF_RX_MODE_INTERRUPT)) - { - vu_log_err (vui, "unhandled mode %d changed for if %d queue %d", mode, - hw_if_index, qid); - return clib_error_return (0, "unsupported"); - } - - if (txvq->thread_index == ~0) - return clib_error_return (0, "Queue initialization is not finished yet"); - - cpu = vec_elt_at_index (vum->cpus, txvq->thread_index); - if ((mode == VNET_HW_IF_RX_MODE_INTERRUPT) || - (mode == VNET_HW_IF_RX_MODE_ADAPTIVE)) - { - if (txvq->kickfd_idx == ~0) - { - // We cannot support interrupt mode if the driver opts out - return clib_error_return (0, "Driver does not support interrupt"); - } - if (txvq->mode == VNET_HW_IF_RX_MODE_POLLING) - { - ASSERT (cpu->polling_q_count != 0); - if (cpu->polling_q_count) - cpu->polling_q_count--; - vum->ifq_count++; - // Start the timer if this is the first encounter on interrupt - // interface/queue - if ((vum->ifq_count == 1) && - ((vum->coalesce_time > 0.0) || (vum->coalesce_frames > 0))) - vlib_process_signal_event (vm, - vhost_user_send_interrupt_node.index, - VHOST_USER_EVENT_START_TIMER, 0); - } - } - else if (mode == VNET_HW_IF_RX_MODE_POLLING) - { - if (((txvq->mode == VNET_HW_IF_RX_MODE_INTERRUPT) || - (txvq->mode == VNET_HW_IF_RX_MODE_ADAPTIVE)) && vum->ifq_count) - { - cpu->polling_q_count++; - vum->ifq_count--; - // Stop the timer if there is no more interrupt interface/queue - if (vum->ifq_count == 0) - vlib_process_signal_event (vm, - vhost_user_send_interrupt_node.index, - VHOST_USER_EVENT_STOP_TIMER, 0); - } - } - - txvq->mode = mode; - vhost_user_set_operation_mode (vui, txvq); - - return 0; -} - -static __clib_unused clib_error_t * -vhost_user_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, - u32 flags) -{ - vnet_hw_interface_t *hif = vnet_get_hw_interface (vnm, hw_if_index); - vhost_user_main_t *vum = &vhost_user_main; - vhost_user_intf_t *vui = - pool_elt_at_index (vum->vhost_user_interfaces, hif->dev_instance); - u8 link_old, link_new; - - link_old = vui_is_link_up (vui); - - vui->admin_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0; - - link_new = vui_is_link_up (vui); - - if (link_old != link_new) - vnet_hw_interface_set_flags (vnm, vui->hw_if_index, link_new ? - VNET_HW_INTERFACE_FLAG_LINK_UP : 0); - - return /* no error */ 0; -} - -/* *INDENT-OFF* */ -VNET_DEVICE_CLASS (vhost_user_device_class) = { - .name = "vhost-user", - .tx_function_n_errors = VHOST_USER_TX_FUNC_N_ERROR, - .tx_function_error_strings = vhost_user_tx_func_error_strings, - .format_device_name = format_vhost_user_interface_name, - .name_renumber = vhost_user_name_renumber, - .admin_up_down_function = vhost_user_interface_admin_up_down, - .rx_mode_change_function = vhost_user_interface_rx_mode_change, - .format_tx_trace = format_vhost_trace, -}; - -/* *INDENT-ON* */ - -/* - * fd.io coding-style-patch-verification: ON - * - * Local Variables: - * eval: (c-set-style "gnu") - * End: - */ diff --git a/src/vnet/devices/virtio/virtio.api b/src/vnet/devices/virtio/virtio.api index bbe2341a001..a11492ec258 100644 --- a/src/vnet/devices/virtio/virtio.api +++ b/src/vnet/devices/virtio/virtio.api @@ -56,7 +56,7 @@ define virtio_pci_create_reply vl_api_interface_index_t sw_if_index; }; -enum virtio_flags { +enumflag virtio_flags { VIRTIO_API_FLAG_GSO = 1, /* enable gso on the interface */ VIRTIO_API_FLAG_CSUM_OFFLOAD = 2, /* enable checksum offload without gso on the interface */ VIRTIO_API_FLAG_GRO_COALESCE = 4, /* enable packet coalescing on tx side, provided gso enabled */ diff --git a/src/vnet/devices/virtio/virtio.c b/src/vnet/devices/virtio/virtio.c index b8054d13e5b..d2302fa1dc4 100644 --- a/src/vnet/devices/virtio/virtio.c +++ b/src/vnet/devices/virtio/virtio.c @@ -19,7 +19,11 @@ #include <sys/stat.h> #include <fcntl.h> #include <net/if.h> +#ifdef __linux__ #include <linux/if_tun.h> +#elif __FreeBSD__ +#include <net/if_tun.h> +#endif /* __linux__ */ #include <sys/ioctl.h> #include <sys/eventfd.h> @@ -33,6 +37,7 @@ #include <vnet/devices/virtio/virtio_inline.h> #include <vnet/devices/virtio/pci.h> #include <vnet/interface/rx_queue_funcs.h> +#include <vnet/interface/tx_queue_funcs.h> virtio_main_t virtio_main; @@ -59,7 +64,7 @@ call_read_ready (clib_file_t * uf) clib_error_t * virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx, u16 sz) { - virtio_vring_t *vring; + vnet_virtio_vring_t *vring; int i; if (!is_pow2 (sz)) @@ -73,12 +78,10 @@ virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx, u16 sz) if (idx % 2) { - vlib_thread_main_t *thm = vlib_get_thread_main (); vec_validate_aligned (vif->txq_vrings, TX_QUEUE_ACCESS (idx), CLIB_CACHE_LINE_BYTES); vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx)); - if (thm->n_vlib_mains > vif->num_txqs) - clib_spinlock_init (&vring->lockp); + clib_spinlock_init (&vring->lockp); } else { @@ -86,19 +89,20 @@ virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx, u16 sz) CLIB_CACHE_LINE_BYTES); vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx)); } - i = sizeof (vring_desc_t) * sz; + i = sizeof (vnet_virtio_vring_desc_t) * sz; i = round_pow2 (i, CLIB_CACHE_LINE_BYTES); vring->desc = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES); clib_memset (vring->desc, 0, i); - i = sizeof (vring_avail_t) + sz * sizeof (vring->avail->ring[0]); + i = sizeof (vnet_virtio_vring_avail_t) + sz * sizeof (vring->avail->ring[0]); i = round_pow2 (i, CLIB_CACHE_LINE_BYTES); vring->avail = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES); clib_memset (vring->avail, 0, i); // tell kernel that we don't need interrupt vring->avail->flags = VRING_AVAIL_F_NO_INTERRUPT; - i = sizeof (vring_used_t) + sz * sizeof (vring_used_elem_t); + i = sizeof (vnet_virtio_vring_used_t) + + sz * sizeof (vnet_virtio_vring_used_elem_t); i = round_pow2 (i, CLIB_CACHE_LINE_BYTES); vring->used = clib_mem_alloc_aligned (i, CLIB_CACHE_LINE_BYTES); clib_memset (vring->used, 0, i); @@ -116,20 +120,21 @@ virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx, u16 sz) else vring->call_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); - vring->size = sz; + vring->total_packets = 0; + vring->queue_size = sz; vring->kick_fd = eventfd (0, EFD_NONBLOCK | EFD_CLOEXEC); virtio_log_debug (vif, "vring %u size %u call_fd %d kick_fd %d", idx, - vring->size, vring->call_fd, vring->kick_fd); + vring->queue_size, vring->call_fd, vring->kick_fd); return 0; } inline void -virtio_free_buffers (vlib_main_t * vm, virtio_vring_t * vring) +virtio_free_buffers (vlib_main_t *vm, vnet_virtio_vring_t *vring) { u16 used = vring->desc_in_use; u16 last = vring->last_used_idx; - u16 mask = vring->size - 1; + u16 mask = vring->queue_size - 1; while (used) { @@ -142,7 +147,7 @@ virtio_free_buffers (vlib_main_t * vm, virtio_vring_t * vring) clib_error_t * virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif, u32 idx) { - virtio_vring_t *vring = + vnet_virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS (idx)); clib_file_del_by_index (&file_main, vring->call_file_index); @@ -164,7 +169,7 @@ virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif, u32 idx) clib_error_t * virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif, u32 idx) { - virtio_vring_t *vring = + vnet_virtio_vring_t *vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS (idx)); close (vring->kick_fd); @@ -189,7 +194,7 @@ virtio_set_packet_coalesce (virtio_if_t * vif) { vnet_main_t *vnm = vnet_get_main (); vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index); - virtio_vring_t *vring; + vnet_virtio_vring_t *vring; vif->packet_coalesce = 1; vec_foreach (vring, vif->txq_vrings) { @@ -204,9 +209,8 @@ virtio_set_packet_buffering (virtio_if_t * vif, u16 buffering_size) { vnet_main_t *vnm = vnet_get_main (); vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index); - virtio_vring_t *vring; + vnet_virtio_vring_t *vring; clib_error_t *error = 0; - vif->packet_buffering = 1; vec_foreach (vring, vif->txq_vrings) { @@ -222,7 +226,8 @@ virtio_set_packet_buffering (virtio_if_t * vif, u16 buffering_size) } static void -virtio_vring_fill (vlib_main_t *vm, virtio_if_t *vif, virtio_vring_t *vring) +virtio_vring_fill (vlib_main_t *vm, virtio_if_t *vif, + vnet_virtio_vring_t *vring) { if (vif->is_packed) virtio_refill_vring_packed (vm, vif, vif->type, vring, @@ -238,7 +243,7 @@ void virtio_vring_set_rx_queues (vlib_main_t *vm, virtio_if_t *vif) { vnet_main_t *vnm = vnet_get_main (); - virtio_vring_t *vring; + vnet_virtio_vring_t *vring; u32 i = 0; vnet_hw_if_set_input_node (vnm, vif->hw_if_index, virtio_input_node.index); @@ -284,24 +289,53 @@ virtio_vring_set_rx_queues (vlib_main_t *vm, virtio_if_t *vif) vnet_hw_if_update_runtime_data (vnm, vif->hw_if_index); } +void +virtio_vring_set_tx_queues (vlib_main_t *vm, virtio_if_t *vif) +{ + vnet_main_t *vnm = vnet_get_main (); + vnet_virtio_vring_t *vring; + + vec_foreach (vring, vif->txq_vrings) + { + vring->queue_index = vnet_hw_if_register_tx_queue ( + vnm, vif->hw_if_index, TX_QUEUE_ACCESS (vring->queue_id)); + } + + if (vif->num_txqs == 0) + { + virtio_log_error (vif, "Interface %U has 0 txq", + format_vnet_hw_if_index_name, vnm, vif->hw_if_index); + return; + } + + for (u32 j = 0; j < vlib_get_n_threads (); j++) + { + u32 qi = vif->txq_vrings[j % vif->num_txqs].queue_index; + vnet_hw_if_tx_queue_assign_thread (vnm, qi, j); + } + + vnet_hw_if_update_runtime_data (vnm, vif->hw_if_index); +} + inline void virtio_set_net_hdr_size (virtio_if_t * vif) { if (vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF) || vif->features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) - vif->virtio_net_hdr_sz = sizeof (virtio_net_hdr_v1_t); + vif->virtio_net_hdr_sz = sizeof (vnet_virtio_net_hdr_v1_t); else - vif->virtio_net_hdr_sz = sizeof (virtio_net_hdr_t); + vif->virtio_net_hdr_sz = sizeof (vnet_virtio_net_hdr_t); } inline void -virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type) +virtio_show (vlib_main_t *vm, u32 *hw_if_indices, u8 show_descr, + virtio_if_type_t type) { u32 i, j, hw_if_index; virtio_if_t *vif; vnet_main_t *vnm = &vnet_main; virtio_main_t *mm = &virtio_main; - virtio_vring_t *vring; + vnet_virtio_vring_t *vring; struct feat_struct { u8 bit; @@ -407,17 +441,17 @@ virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type) } vlib_cli_output (vm, " Number of RX Virtqueue %u", vif->num_rxqs); vlib_cli_output (vm, " Number of TX Virtqueue %u", vif->num_txqs); - if (vif->cxq_vring != NULL - && vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) + if (type == VIRTIO_IF_TYPE_PCI && vif->cxq_vring != NULL && + vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) vlib_cli_output (vm, " Number of CTRL Virtqueue 1"); vec_foreach_index (i, vif->rxq_vrings) { vring = vec_elt_at_index (vif->rxq_vrings, i); vlib_cli_output (vm, " Virtqueue (RX) %d", vring->queue_id); - vlib_cli_output (vm, - " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d", - vring->size, vring->last_used_idx, vring->desc_next, - vring->desc_in_use); + vlib_cli_output ( + vm, " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d", + vring->queue_size, vring->last_used_idx, vring->desc_next, + vring->desc_in_use); if (vif->is_packed) { vlib_cli_output (vm, @@ -448,11 +482,12 @@ virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type) " id addr len flags next/id user_addr\n"); vlib_cli_output (vm, " ===== ================== ===== ====== ======= ==================\n"); - for (j = 0; j < vring->size; j++) + for (j = 0; j < vring->queue_size; j++) { if (vif->is_packed) { - vring_packed_desc_t *desc = &vring->packed_desc[j]; + vnet_virtio_vring_packed_desc_t *desc = + &vring->packed_desc[j]; vlib_cli_output (vm, " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n", j, desc->addr, @@ -461,7 +496,7 @@ virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type) } else { - vring_desc_t *desc = &vring->desc[j]; + vnet_virtio_vring_desc_t *desc = &vring->desc[j]; vlib_cli_output (vm, " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n", j, desc->addr, @@ -475,10 +510,10 @@ virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type) { vring = vec_elt_at_index (vif->txq_vrings, i); vlib_cli_output (vm, " Virtqueue (TX) %d", vring->queue_id); - vlib_cli_output (vm, - " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d", - vring->size, vring->last_used_idx, vring->desc_next, - vring->desc_in_use); + vlib_cli_output ( + vm, " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d", + vring->queue_size, vring->last_used_idx, vring->desc_next, + vring->desc_in_use); if (vif->is_packed) { vlib_cli_output (vm, @@ -519,11 +554,12 @@ virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type) " id addr len flags next/id user_addr\n"); vlib_cli_output (vm, " ===== ================== ===== ====== ======== ==================\n"); - for (j = 0; j < vring->size; j++) + for (j = 0; j < vring->queue_size; j++) { if (vif->is_packed) { - vring_packed_desc_t *desc = &vring->packed_desc[j]; + vnet_virtio_vring_packed_desc_t *desc = + &vring->packed_desc[j]; vlib_cli_output (vm, " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n", j, desc->addr, @@ -532,7 +568,7 @@ virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type) } else { - vring_desc_t *desc = &vring->desc[j]; + vnet_virtio_vring_desc_t *desc = &vring->desc[j]; vlib_cli_output (vm, " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n", j, desc->addr, @@ -542,15 +578,15 @@ virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type) } } } - if (vif->cxq_vring != NULL - && vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) + if (type == VIRTIO_IF_TYPE_PCI && vif->cxq_vring != NULL && + vif->features & VIRTIO_FEATURE (VIRTIO_NET_F_CTRL_VQ)) { vring = vif->cxq_vring; vlib_cli_output (vm, " Virtqueue (CTRL) %d", vring->queue_id); - vlib_cli_output (vm, - " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d", - vring->size, vring->last_used_idx, - vring->desc_next, vring->desc_in_use); + vlib_cli_output ( + vm, " qsz %d, last_used_idx %d, desc_next %d, desc_in_use %d", + vring->queue_size, vring->last_used_idx, vring->desc_next, + vring->desc_in_use); if (vif->is_packed) { vlib_cli_output (vm, @@ -571,11 +607,6 @@ virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type) vring->avail->flags, vring->avail->idx, vring->used->flags, vring->used->idx); } - if (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_TUN)) - { - vlib_cli_output (vm, " kickfd %d, callfd %d", vring->kick_fd, - vring->call_fd); - } if (show_descr) { vlib_cli_output (vm, "\n descriptor table:\n"); @@ -583,11 +614,12 @@ virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type) " id addr len flags next/id user_addr\n"); vlib_cli_output (vm, " ===== ================== ===== ====== ======== ==================\n"); - for (j = 0; j < vring->size; j++) + for (j = 0; j < vring->queue_size; j++) { if (vif->is_packed) { - vring_packed_desc_t *desc = &vring->packed_desc[j]; + vnet_virtio_vring_packed_desc_t *desc = + &vring->packed_desc[j]; vlib_cli_output (vm, " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n", j, desc->addr, @@ -596,7 +628,7 @@ virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type) } else { - vring_desc_t *desc = &vring->desc[j]; + vnet_virtio_vring_desc_t *desc = &vring->desc[j]; vlib_cli_output (vm, " %-5d 0x%016lx %-5d 0x%04x %-8d 0x%016lx\n", j, desc->addr, @@ -606,7 +638,6 @@ virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, u32 type) } } } - } } diff --git a/src/vnet/devices/virtio/virtio.h b/src/vnet/devices/virtio/virtio.h index 48996052e2b..431b1d25c26 100644 --- a/src/vnet/devices/virtio/virtio.h +++ b/src/vnet/devices/virtio/virtio.h @@ -22,6 +22,7 @@ #include <vnet/devices/virtio/vhost_std.h> #include <vnet/devices/virtio/virtio_buffering.h> #include <vnet/gso/gro.h> +#include <vnet/interface.h> #define foreach_virtio_if_flag \ _(0, ADMIN_UP, "admin-up") \ @@ -68,19 +69,19 @@ typedef struct { struct { - vring_desc_t *desc; - vring_used_t *used; - vring_avail_t *avail; + vnet_virtio_vring_desc_t *desc; + vnet_virtio_vring_used_t *used; + vnet_virtio_vring_avail_t *avail; }; struct { - vring_packed_desc_t *packed_desc; - vring_desc_event_t *driver_event; - vring_desc_event_t *device_event; + vnet_virtio_vring_packed_desc_t *packed_desc; + vnet_virtio_vring_desc_event_t *driver_event; + vnet_virtio_vring_desc_event_t *device_event; }; }; u32 *buffers; - u16 size; + u16 queue_size; u16 queue_id; u32 queue_index; u16 desc_in_use; @@ -103,12 +104,14 @@ typedef struct }; }; #define VRING_TX_OUT_OF_ORDER 1 +#define VRING_TX_SCHEDULED 2 u16 flags; u8 buffer_pool_index; vnet_hw_if_rx_mode mode; virtio_vring_buffering_t *buffering; gro_flow_table_t *flow_table; -} virtio_vring_t; + u64 total_packets; +} vnet_virtio_vring_t; typedef union { @@ -133,8 +136,8 @@ typedef struct u32 per_interface_next_index; u16 num_rxqs; u16 num_txqs; - virtio_vring_t *rxq_vrings; - virtio_vring_t *txq_vrings; + vnet_virtio_vring_t *rxq_vrings; + vnet_virtio_vring_t *txq_vrings; int gso_enabled; int csum_offload_enabled; union @@ -192,7 +195,7 @@ typedef struct struct /* native virtio */ { void *bar; - virtio_vring_t *cxq_vring; + vnet_virtio_vring_t *cxq_vring; pci_addr_t pci_addr; u32 bar_id; u32 notify_off_multiplier; @@ -213,7 +216,7 @@ typedef struct typedef struct { - u32 interrupt_queues_count; + u32 gro_or_buffering_if_count; /* logging */ vlib_log_class_t log_default; @@ -224,7 +227,6 @@ typedef struct extern virtio_main_t virtio_main; extern vnet_device_class_t virtio_device_class; extern vlib_node_registration_t virtio_input_node; -extern vlib_node_registration_t virtio_send_interrupt_node; clib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx, u16 sz); @@ -233,10 +235,11 @@ clib_error_t *virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif, clib_error_t *virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif, u32 idx); void virtio_vring_set_rx_queues (vlib_main_t *vm, virtio_if_t *vif); -extern void virtio_free_buffers (vlib_main_t * vm, virtio_vring_t * vring); +void virtio_vring_set_tx_queues (vlib_main_t *vm, virtio_if_t *vif); +extern void virtio_free_buffers (vlib_main_t *vm, vnet_virtio_vring_t *vring); extern void virtio_set_net_hdr_size (virtio_if_t * vif); -extern void virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, - u32 type); +extern void virtio_show (vlib_main_t *vm, u32 *hw_if_indices, u8 show_descr, + virtio_if_type_t type); extern void virtio_set_packet_coalesce (virtio_if_t * vif); clib_error_t *virtio_set_packet_buffering (virtio_if_t * vif, u16 size); extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm, @@ -245,11 +248,14 @@ extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm, extern void virtio_pci_modern_notify_queue (vlib_main_t * vm, virtio_if_t * vif, u16 queue_id, u16 queue_notify_offset); +extern void virtio_pre_input_node_enable (vlib_main_t *vm, virtio_if_t *vif); +extern void virtio_pre_input_node_disable (vlib_main_t *vm, virtio_if_t *vif); + format_function_t format_virtio_device_name; format_function_t format_virtio_log_name; static_always_inline void -virtio_kick (vlib_main_t * vm, virtio_vring_t * vring, virtio_if_t * vif) +virtio_kick (vlib_main_t *vm, vnet_virtio_vring_t *vring, virtio_if_t *vif) { if (vif->type == VIRTIO_IF_TYPE_PCI) { @@ -270,6 +276,56 @@ virtio_kick (vlib_main_t * vm, virtio_vring_t * vring, virtio_if_t * vif) } } +static_always_inline u8 +virtio_txq_is_scheduled (vnet_virtio_vring_t *vring) +{ + if (vring) + return (vring->flags & VRING_TX_SCHEDULED); + return 1; +} + +static_always_inline void +virtio_txq_set_scheduled (vnet_virtio_vring_t *vring) +{ + if (vring) + vring->flags |= VRING_TX_SCHEDULED; +} + +static_always_inline void +virtio_txq_clear_scheduled (vnet_virtio_vring_t *vring) +{ + if (vring) + vring->flags &= ~VRING_TX_SCHEDULED; +} + +static_always_inline void +vnet_virtio_vring_init (vnet_virtio_vring_t *vring, u16 queue_size, void *p, + u32 align) +{ + vring->queue_size = queue_size; + vring->desc = p; + vring->avail = + (vnet_virtio_vring_avail_t *) ((char *) p + + queue_size * + sizeof (vnet_virtio_vring_desc_t)); + vring->used = + (vnet_virtio_vring_used_t + *) ((char *) p + ((sizeof (vnet_virtio_vring_desc_t) * queue_size + + sizeof (u16) * (3 + queue_size) + align - 1) & + ~(align - 1))); + vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT; +} + +static_always_inline u16 +vnet_virtio_vring_size (u16 queue_size, u32 align) +{ + return ((sizeof (vnet_virtio_vring_desc_t) * queue_size + + sizeof (u16) * (3 + queue_size) + align - 1) & + ~(align - 1)) + + sizeof (u16) * 3 + + sizeof (vnet_virtio_vring_used_elem_t) * queue_size; +} + #define virtio_log_debug(vif, f, ...) \ { \ vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default, \ diff --git a/src/vnet/devices/virtio/virtio_api.c b/src/vnet/devices/virtio/virtio_api.c index 11514c75c59..3197a2fab6d 100644 --- a/src/vnet/devices/virtio/virtio_api.c +++ b/src/vnet/devices/virtio/virtio_api.c @@ -193,10 +193,10 @@ virtio_pci_send_sw_interface_details (vpe_api_main_t * am, pci_address_encode ((vlib_pci_addr_t *) & vif->pci_addr.as_u32, &mp->pci_addr); mp->sw_if_index = htonl (vif->sw_if_index); - virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, 0); - mp->rx_ring_sz = htons (vring->size); + vnet_virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, 0); + mp->rx_ring_sz = htons (vring->queue_size); vring = vec_elt_at_index (vif->txq_vrings, 0); - mp->tx_ring_sz = htons (vring->size); + mp->tx_ring_sz = htons (vring->queue_size); clib_memcpy (mp->mac_addr, vif->mac_addr, 6); mp->features = clib_host_to_net_u64 (vif->features); diff --git a/src/vnet/devices/virtio/virtio_buffering.h b/src/vnet/devices/virtio/virtio_buffering.h index ef3d9d27652..6f13a1f5c36 100644 --- a/src/vnet/devices/virtio/virtio_buffering.h +++ b/src/vnet/devices/virtio/virtio_buffering.h @@ -18,6 +18,8 @@ #ifndef _VNET_DEVICES_VIRTIO_VIRTIO_BUFFERING_H_ #define _VNET_DEVICES_VIRTIO_VIRTIO_BUFFERING_H_ +#include <vnet/interface.h> + #define VIRTIO_BUFFERING_DEFAULT_SIZE 1024 #define VIRTIO_BUFFERING_TIMEOUT 1e-5 @@ -205,15 +207,18 @@ virtio_vring_buffering_read_from_back (virtio_vring_buffering_t * buffering) } static_always_inline void -virtio_vring_buffering_schedule_node_on_dispatcher (vlib_main_t * vm, - virtio_vring_buffering_t * - buffering) +virtio_vring_buffering_schedule_node_on_dispatcher ( + vlib_main_t *vm, vnet_hw_if_tx_queue_t *txq, + virtio_vring_buffering_t *buffering) { if (buffering && virtio_vring_buffering_is_timeout (vm, buffering) && virtio_vring_n_buffers (buffering)) { vlib_frame_t *f = vlib_get_frame_to_node (vm, buffering->node_index); + vnet_hw_if_tx_frame_t *ft = vlib_frame_scalar_args (f); u32 *f_to = vlib_frame_vector_args (f); + ft->shared_queue = txq->shared_queue; + ft->queue_id = txq->queue_id; f_to[f->n_vectors] = virtio_vring_buffering_read_from_back (buffering); f->n_vectors++; vlib_put_frame_to_node (vm, buffering->node_index, f); diff --git a/src/vnet/devices/virtio/virtio_inline.h b/src/vnet/devices/virtio/virtio_inline.h index 209817d48c7..179f319aa4c 100644 --- a/src/vnet/devices/virtio/virtio_inline.h +++ b/src/vnet/devices/virtio/virtio_inline.h @@ -29,11 +29,11 @@ typedef enum static_always_inline void virtio_refill_vring_split (vlib_main_t *vm, virtio_if_t *vif, - virtio_if_type_t type, virtio_vring_t *vring, + virtio_if_type_t type, vnet_virtio_vring_t *vring, const int hdr_sz, u32 node_index) { u16 used, next, avail, n_slots, n_refill; - u16 sz = vring->size; + u16 sz = vring->queue_size; u16 mask = sz - 1; more: @@ -47,8 +47,9 @@ more: next = vring->desc_next; avail = vring->avail->idx; - n_slots = vlib_buffer_alloc_to_ring_from_pool ( - vm, vring->buffers, next, vring->size, n_refill, vring->buffer_pool_index); + n_slots = vlib_buffer_alloc_to_ring_from_pool (vm, vring->buffers, next, + vring->queue_size, n_refill, + vring->buffer_pool_index); if (PREDICT_FALSE (n_slots != n_refill)) { @@ -60,7 +61,7 @@ more: while (n_slots) { - vring_desc_t *d = &vring->desc[next]; + vnet_virtio_vring_desc_t *d = &vring->desc[next]; ; vlib_buffer_t *b = vlib_get_buffer (vm, vring->buffers[next]); /* @@ -94,11 +95,11 @@ more: static_always_inline void virtio_refill_vring_packed (vlib_main_t *vm, virtio_if_t *vif, - virtio_if_type_t type, virtio_vring_t *vring, + virtio_if_type_t type, vnet_virtio_vring_t *vring, const int hdr_sz, u32 node_index) { u16 used, next, n_slots, n_refill, flags = 0, first_desc_flags; - u16 sz = vring->size; + u16 sz = vring->queue_size; more: used = vring->desc_in_use; @@ -124,7 +125,7 @@ more: while (n_slots) { - vring_packed_desc_t *d = &vring->packed_desc[next]; + vnet_virtio_vring_packed_desc_t *d = &vring->packed_desc[next]; vlib_buffer_t *b = vlib_get_buffer (vm, vring->buffers[next]); /* * current_data may not be initialized with 0 and may contain diff --git a/src/vnet/devices/virtio/virtio_pci_legacy.c b/src/vnet/devices/virtio/virtio_pci_legacy.c index 1426a7035a2..d7a1c982413 100644 --- a/src/vnet/devices/virtio/virtio_pci_legacy.c +++ b/src/vnet/devices/virtio/virtio_pci_legacy.c @@ -176,10 +176,11 @@ virtio_pci_legacy_set_queue_num (vlib_main_t * vm, virtio_if_t * vif, } static u8 -virtio_pci_legacy_setup_queue (vlib_main_t * vm, virtio_if_t * vif, - u16 queue_id, void *p) +virtio_pci_legacy_setup_queue (vlib_main_t *vm, virtio_if_t *vif, u16 queue_id, + vnet_virtio_vring_t *vring) { - u64 addr = vlib_physmem_get_pa (vm, p) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT; + u64 addr = + vlib_physmem_get_pa (vm, vring->desc) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT; u32 addr2 = 0, a = (u32) addr; vlib_pci_write_io_u16 (vm, vif->pci_dev_handle, VIRTIO_PCI_QUEUE_SEL, &queue_id); diff --git a/src/vnet/devices/virtio/virtio_pci_modern.c b/src/vnet/devices/virtio/virtio_pci_modern.c index 8e090ffed3a..50a7b392367 100644 --- a/src/vnet/devices/virtio/virtio_pci_modern.c +++ b/src/vnet/devices/virtio/virtio_pci_modern.c @@ -164,9 +164,7 @@ virtio_pci_modern_set_queue_size (vlib_main_t * vm, virtio_if_t * vif, return; } - if (virtio_pci_modern_get_queue_size (vm, vif, queue_id) > queue_size) - virtio_pci_reg_write_u16 (vif, VIRTIO_QUEUE_SIZE_OFFSET (vif), - queue_size); + virtio_pci_reg_write_u16 (vif, VIRTIO_QUEUE_SIZE_OFFSET (vif), queue_size); } static u16 @@ -265,32 +263,24 @@ virtio_pci_modern_set_queue_device (virtio_if_t * vif, u64 queue_device) } static u8 -virtio_pci_modern_setup_queue (vlib_main_t * vm, virtio_if_t * vif, - u16 queue_id, void *p) +virtio_pci_modern_setup_queue (vlib_main_t *vm, virtio_if_t *vif, u16 queue_id, + vnet_virtio_vring_t *vring) { u64 desc, avail, used; - u16 queue_size = 0; virtio_pci_modern_set_queue_select (vif, queue_id); - queue_size = virtio_pci_modern_get_queue_size (vm, vif, queue_id); if (vif->is_packed) { - virtio_vring_t *vring = (virtio_vring_t *) p; - desc = vlib_physmem_get_pa (vm, vring->packed_desc); avail = vlib_physmem_get_pa (vm, vring->driver_event); used = vlib_physmem_get_pa (vm, vring->device_event); } else { - vring_t vr; - - vring_init (&vr, queue_size, p, VIRTIO_PCI_VRING_ALIGN); - - desc = vlib_physmem_get_pa (vm, vr.desc); - avail = vlib_physmem_get_pa (vm, vr.avail); - used = vlib_physmem_get_pa (vm, vr.used); + desc = vlib_physmem_get_pa (vm, vring->desc); + avail = vlib_physmem_get_pa (vm, vring->avail); + used = vlib_physmem_get_pa (vm, vring->used); } virtio_pci_modern_set_queue_desc (vif, desc); diff --git a/src/vnet/devices/virtio/virtio_pre_input.c b/src/vnet/devices/virtio/virtio_pre_input.c new file mode 100644 index 00000000000..80cc8d6edb0 --- /dev/null +++ b/src/vnet/devices/virtio/virtio_pre_input.c @@ -0,0 +1,160 @@ +/* + *------------------------------------------------------------------ + * Copyright (c) 2021 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 <vlib/vlib.h> +#include <vnet/gso/gro_func.h> +#include <vnet/interface/tx_queue_funcs.h> +#include <vnet/devices/virtio/virtio.h> +#include <vnet/devices/virtio/virtio_inline.h> + +static_always_inline uword +virtio_pre_input_inline (vlib_main_t *vm, vnet_virtio_vring_t *txq_vring, + vnet_hw_if_tx_queue_t *txq, u8 packet_coalesce, + u8 packet_buffering) +{ + if (txq->shared_queue) + { + if (clib_spinlock_trylock (&txq_vring->lockp)) + { + if (virtio_txq_is_scheduled (txq_vring)) + goto unlock; + if (packet_coalesce) + vnet_gro_flow_table_schedule_node_on_dispatcher ( + vm, txq, txq_vring->flow_table); + else if (packet_buffering) + virtio_vring_buffering_schedule_node_on_dispatcher ( + vm, txq, txq_vring->buffering); + virtio_txq_set_scheduled (txq_vring); + unlock: + clib_spinlock_unlock (&txq_vring->lockp); + } + } + else + { + if (packet_coalesce) + vnet_gro_flow_table_schedule_node_on_dispatcher ( + vm, txq, txq_vring->flow_table); + else if (packet_buffering) + virtio_vring_buffering_schedule_node_on_dispatcher ( + vm, txq, txq_vring->buffering); + } + return 0; +} + +static uword +virtio_pre_input (vlib_main_t *vm, vlib_node_runtime_t *node, + vlib_frame_t *frame) +{ + virtio_main_t *vim = &virtio_main; + vnet_main_t *vnm = vnet_get_main (); + virtio_if_t *vif; + + pool_foreach (vif, vim->interfaces) + { + if (vif->packet_coalesce || vif->packet_buffering) + { + vnet_virtio_vring_t *txq_vring; + vec_foreach (txq_vring, vif->txq_vrings) + { + vnet_hw_if_tx_queue_t *txq = + vnet_hw_if_get_tx_queue (vnm, txq_vring->queue_index); + if (clib_bitmap_get (txq->threads, vm->thread_index) == 1) + virtio_pre_input_inline (vm, txq_vring, txq, + vif->packet_coalesce, + vif->packet_buffering); + } + } + } + + return 0; +} + +/** + * virtio interfaces support packet coalescing and buffering which + * depends on timer expiry to flush the stored packets periodically. + * Previously, virtio input node checked timer expiry and scheduled + * tx queue accordingly. + * + * In poll mode, timer expiry was handled naturally, as input node + * runs periodically. In interrupt mode, virtio input node was dependent + * on the interrupts send from backend. Stored packets could starve, + * if there would not be interrupts to input node. + * + * This problem had been solved through a dedicated process node which + * periodically sends interrupt to virtio input node given coalescing + * or buffering feature were enabled on an interface. + * + * But that approach worked with following limitations: + * 1) Each VPP thread should have (atleast) 1 rx queue of an interface + * (with buffering enabled). And rxqs and txqs should be placed on the + * same thread. + * + * New design provides solution to above problem(s) without any limitation + * through (dedicated) pre-input node running on each VPP thread when + * atleast 1 virtio interface is enabled with coalescing or buffering. + */ +VLIB_REGISTER_NODE (virtio_pre_input_node) = { + .function = virtio_pre_input, + .type = VLIB_NODE_TYPE_PRE_INPUT, + .name = "virtio-pre-input", + .state = VLIB_NODE_STATE_DISABLED, +}; + +void +virtio_pre_input_node_enable (vlib_main_t *vm, virtio_if_t *vif) +{ + virtio_main_t *vim = &virtio_main; + if (vif->packet_coalesce || vif->packet_buffering) + { + vim->gro_or_buffering_if_count++; + if (vim->gro_or_buffering_if_count == 1) + { + foreach_vlib_main () + { + vlib_node_set_state (this_vlib_main, virtio_pre_input_node.index, + VLIB_NODE_STATE_POLLING); + } + } + } +} + +void +virtio_pre_input_node_disable (vlib_main_t *vm, virtio_if_t *vif) +{ + virtio_main_t *vim = &virtio_main; + if (vif->packet_coalesce || vif->packet_buffering) + { + if (vim->gro_or_buffering_if_count > 0) + vim->gro_or_buffering_if_count--; + if (vim->gro_or_buffering_if_count == 0) + { + foreach_vlib_main () + { + vlib_node_set_state (this_vlib_main, virtio_pre_input_node.index, + VLIB_NODE_STATE_DISABLED); + } + } + } +} + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/devices/virtio/virtio_process.c b/src/vnet/devices/virtio/virtio_process.c index f347ef2ab57..13ba590659c 100644 --- a/src/vnet/devices/virtio/virtio_process.c +++ b/src/vnet/devices/virtio/virtio_process.c @@ -50,7 +50,7 @@ virtio_send_interrupt_process (vlib_main_t * vm, { if (vif->packet_coalesce || vif->packet_buffering) { - virtio_vring_t *vring; + vnet_virtio_vring_t *vring; vec_foreach (vring, vif->rxq_vrings) { if (vring->mode == VNET_HW_IF_RX_MODE_INTERRUPT || @@ -70,13 +70,11 @@ virtio_send_interrupt_process (vlib_main_t * vm, return 0; } -/* *INDENT-OFF* */ VLIB_REGISTER_NODE (virtio_send_interrupt_node) = { .function = virtio_send_interrupt_process, .type = VLIB_NODE_TYPE_PROCESS, .name = "virtio-send-interrupt-process", }; -/* *INDENT-ON* */ /* * fd.io coding-style-patch-verification: ON diff --git a/src/vnet/devices/virtio/virtio_std.h b/src/vnet/devices/virtio/virtio_std.h index 619dd66d5ed..ec988c08dbb 100644 --- a/src/vnet/devices/virtio/virtio_std.h +++ b/src/vnet/devices/virtio/virtio_std.h @@ -64,7 +64,7 @@ typedef enum #define _(f,n) f = n, foreach_virtio_net_features #undef _ -} virtio_net_feature_t; +} vnet_virtio_net_feature_t; #define VIRTIO_FEATURE(X) (1ULL << X) @@ -87,7 +87,7 @@ typedef enum #define _(f,n) f = n, foreach_virtio_event_idx_flags #undef _ -} virtio_event_idx_flags_t; +} vnet_virtio_event_idx_flags_t; #define VRING_USED_F_NO_NOTIFY 1 #define VRING_AVAIL_F_NO_INTERRUPT 1 @@ -98,7 +98,7 @@ typedef struct u32 len; u16 flags; u16 next; -} vring_desc_t; +} vnet_virtio_vring_desc_t; typedef struct { @@ -106,38 +106,35 @@ typedef struct u16 idx; u16 ring[0]; /* u16 used_event; */ -} vring_avail_t; +} vnet_virtio_vring_avail_t; typedef struct { u32 id; u32 len; -} vring_used_elem_t; +} vnet_virtio_vring_used_elem_t; typedef struct { u16 flags; u16 idx; - vring_used_elem_t ring[0]; + vnet_virtio_vring_used_elem_t ring[0]; /* u16 avail_event; */ -} vring_used_t; +} vnet_virtio_vring_used_t; -/* *INDENT-OFF* */ -typedef CLIB_PACKED (struct -{ - u64 addr; // packet data buffer address - u32 len; // packet data buffer size - u16 id; // buffer id - u16 flags; // flags -}) vring_packed_desc_t; +typedef CLIB_PACKED (struct { + u64 addr; // packet data buffer address + u32 len; // packet data buffer size + u16 id; // buffer id + u16 flags; // flags +}) vnet_virtio_vring_packed_desc_t; -STATIC_ASSERT_SIZEOF (vring_packed_desc_t, 16); +STATIC_ASSERT_SIZEOF (vnet_virtio_vring_packed_desc_t, 16); -typedef CLIB_PACKED (struct -{ +typedef CLIB_PACKED (struct { u16 off_wrap; u16 flags; -}) vring_desc_event_t; +}) vnet_virtio_vring_desc_event_t; #define VIRTIO_NET_HDR_F_NEEDS_CSUM 1 /* Use csum_start, csum_offset */ #define VIRTIO_NET_HDR_F_DATA_VALID 2 /* Csum is valid */ @@ -148,8 +145,7 @@ typedef CLIB_PACKED (struct #define VIRTIO_NET_HDR_GSO_TCPV6 4 /* GSO frame, IPv6 TCP */ #define VIRTIO_NET_HDR_GSO_ECN 0x80 /* TCP has ECN set */ -typedef CLIB_PACKED (struct -{ +typedef CLIB_PACKED (struct { u8 flags; u8 gso_type; u16 hdr_len; /* Ethernet + IP + tcp/udp hdrs */ @@ -157,54 +153,22 @@ typedef CLIB_PACKED (struct u16 csum_start; /* Position to start checksumming from */ u16 csum_offset; /* Offset after that to place checksum */ u16 num_buffers; /* Number of merged rx buffers */ -}) virtio_net_hdr_v1_t; +}) vnet_virtio_net_hdr_v1_t; -typedef CLIB_PACKED (struct -{ +typedef CLIB_PACKED (struct { u8 flags; u8 gso_type; u16 hdr_len; u16 gso_size; u16 csum_start; u16 csum_offset; -}) virtio_net_hdr_t; +}) vnet_virtio_net_hdr_t; -typedef CLIB_PACKED (struct -{ - virtio_net_hdr_t hdr; +typedef CLIB_PACKED (struct { + vnet_virtio_net_hdr_t hdr; u16 num_buffers; -}) virtio_net_hdr_mrg_rxbuf_t; +}) vnet_virtio_net_hdr_mrg_rxbuf_t; -/* *INDENT-ON* */ - -typedef struct -{ - u16 num; - vring_desc_t *desc; - vring_avail_t *avail; - vring_used_t *used; -} vring_t; - -static_always_inline void -vring_init (vring_t * vr, u32 num, void *p, u32 align) -{ - vr->num = num; - vr->desc = p; - vr->avail = (vring_avail_t *) ((char *) p + num * sizeof (vring_desc_t)); - vr->used = - (vring_used_t *) ((char *) p + - ((sizeof (vring_desc_t) * num + - sizeof (u16) * (3 + num) + align - 1) & ~(align - - 1))); -} - -static_always_inline u16 -vring_size (u32 num, u32 align) -{ - return ((sizeof (vring_desc_t) * num + sizeof (u16) * (3 + num) - + align - 1) & ~(align - 1)) - + sizeof (u16) * 3 + sizeof (vring_used_elem_t) * num; -} #endif /* |