2017-12-19 15:49:02 +00:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
* Copyright(c) 2010-2017 Intel Corporation
|
2014-10-20 04:38:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <linux/if_ether.h>
|
|
|
|
#include <linux/if_vlan.h>
|
|
|
|
#include <linux/virtio_net.h>
|
|
|
|
#include <linux/virtio_ring.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/eventfd.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <rte_atomic.h>
|
|
|
|
#include <rte_cycles.h>
|
|
|
|
#include <rte_ethdev.h>
|
|
|
|
#include <rte_log.h>
|
|
|
|
#include <rte_string_fns.h>
|
|
|
|
#include <rte_malloc.h>
|
2017-04-01 07:22:57 +00:00
|
|
|
#include <rte_vhost.h>
|
2016-02-05 07:31:40 +00:00
|
|
|
#include <rte_ip.h>
|
2016-02-05 07:31:41 +00:00
|
|
|
#include <rte_tcp.h>
|
2017-06-05 08:58:43 +00:00
|
|
|
#include <rte_pause.h>
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
2015-12-10 15:49:17 +00:00
|
|
|
#ifndef MAX_QUEUES
|
|
|
|
#define MAX_QUEUES 128
|
|
|
|
#endif
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
/* the maximum number of external ports supported */
|
|
|
|
#define MAX_SUP_PORTS 1
|
|
|
|
|
2015-04-29 23:31:51 +00:00
|
|
|
#define MBUF_CACHE_SIZE 128
|
|
|
|
#define MBUF_DATA_SIZE RTE_MBUF_DEFAULT_BUF_SIZE
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2015-06-04 14:43:25 +00:00
|
|
|
#define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2015-06-04 14:43:25 +00:00
|
|
|
#define BURST_RX_WAIT_US 15 /* Defines how long we wait between retries on RX */
|
2014-10-20 04:38:13 +00:00
|
|
|
#define BURST_RX_RETRIES 4 /* Number of retries on RX. */
|
|
|
|
|
|
|
|
#define JUMBO_FRAME_MAX_SIZE 0x2600
|
|
|
|
|
|
|
|
/* State of virtio device. */
|
|
|
|
#define DEVICE_MAC_LEARNING 0
|
|
|
|
#define DEVICE_RX 1
|
|
|
|
#define DEVICE_SAFE_REMOVE 2
|
|
|
|
|
|
|
|
/* Configurable number of RX/TX ring descriptors */
|
|
|
|
#define RTE_TEST_RX_DESC_DEFAULT 1024
|
|
|
|
#define RTE_TEST_TX_DESC_DEFAULT 512
|
|
|
|
|
|
|
|
#define INVALID_PORT_ID 0xFF
|
|
|
|
|
|
|
|
/* Maximum long option length for option parsing. */
|
|
|
|
#define MAX_LONG_OPT_SZ 64
|
|
|
|
|
|
|
|
/* mask of enabled ports */
|
|
|
|
static uint32_t enabled_port_mask = 0;
|
|
|
|
|
2014-11-08 04:26:16 +00:00
|
|
|
/* Promiscuous mode */
|
|
|
|
static uint32_t promiscuous;
|
|
|
|
|
2014-10-20 04:38:13 +00:00
|
|
|
/* number of devices/queues to support*/
|
|
|
|
static uint32_t num_queues = 0;
|
2014-10-20 04:38:23 +00:00
|
|
|
static uint32_t num_devices;
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2016-05-02 21:23:43 +00:00
|
|
|
static struct rte_mempool *mbuf_pool;
|
2014-10-23 09:21:13 +00:00
|
|
|
static int mergeable;
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
/* Enable VM2VM communications. If this is disabled then the MAC address compare is skipped. */
|
|
|
|
typedef enum {
|
|
|
|
VM2VM_DISABLED = 0,
|
|
|
|
VM2VM_SOFTWARE = 1,
|
|
|
|
VM2VM_HARDWARE = 2,
|
|
|
|
VM2VM_LAST
|
|
|
|
} vm2vm_type;
|
|
|
|
static vm2vm_type vm2vm_mode = VM2VM_SOFTWARE;
|
|
|
|
|
|
|
|
/* Enable stats. */
|
|
|
|
static uint32_t enable_stats = 0;
|
|
|
|
/* Enable retries on RX. */
|
|
|
|
static uint32_t enable_retry = 1;
|
2016-02-05 07:31:41 +00:00
|
|
|
|
|
|
|
/* Disable TX checksum offload */
|
|
|
|
static uint32_t enable_tx_csum;
|
|
|
|
|
|
|
|
/* Disable TSO offload */
|
|
|
|
static uint32_t enable_tso;
|
|
|
|
|
2016-05-07 05:23:40 +00:00
|
|
|
static int client_mode;
|
2016-10-09 07:27:59 +00:00
|
|
|
static int dequeue_zero_copy;
|
2016-05-07 05:23:40 +00:00
|
|
|
|
2017-04-01 07:23:00 +00:00
|
|
|
static int builtin_net_driver;
|
|
|
|
|
2014-10-20 04:38:13 +00:00
|
|
|
/* Specify timeout (in useconds) between retries on RX. */
|
|
|
|
static uint32_t burst_rx_delay_time = BURST_RX_WAIT_US;
|
|
|
|
/* Specify the number of retries on RX. */
|
|
|
|
static uint32_t burst_rx_retry_num = BURST_RX_RETRIES;
|
|
|
|
|
2016-08-20 10:11:36 +00:00
|
|
|
/* Socket file paths. Can be set by user */
|
|
|
|
static char *socket_files;
|
|
|
|
static int nb_sockets;
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
/* empty vmdq configuration structure. Filled in programatically */
|
|
|
|
static struct rte_eth_conf vmdq_conf_default = {
|
|
|
|
.rxmode = {
|
|
|
|
.mq_mode = ETH_MQ_RX_VMDQ_ONLY,
|
|
|
|
.split_hdr_size = 0,
|
|
|
|
/*
|
2017-12-26 09:23:25 +00:00
|
|
|
* VLAN strip is necessary for 1G NIC such as I350,
|
2014-10-20 04:38:13 +00:00
|
|
|
* this fixes bug of ipv4 forwarding in guest can't
|
|
|
|
* forward pakets from one virtio dev to another virtio dev.
|
|
|
|
*/
|
2018-09-04 10:12:56 +00:00
|
|
|
.offloads = DEV_RX_OFFLOAD_VLAN_STRIP,
|
2014-10-20 04:38:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
.txmode = {
|
|
|
|
.mq_mode = ETH_MQ_TX_NONE,
|
2017-12-26 09:23:25 +00:00
|
|
|
.offloads = (DEV_TX_OFFLOAD_IPV4_CKSUM |
|
|
|
|
DEV_TX_OFFLOAD_TCP_CKSUM |
|
|
|
|
DEV_TX_OFFLOAD_VLAN_INSERT |
|
|
|
|
DEV_TX_OFFLOAD_MULTI_SEGS |
|
|
|
|
DEV_TX_OFFLOAD_TCP_TSO),
|
2014-10-20 04:38:13 +00:00
|
|
|
},
|
|
|
|
.rx_adv_conf = {
|
|
|
|
/*
|
|
|
|
* should be overridden separately in code with
|
|
|
|
* appropriate values
|
|
|
|
*/
|
|
|
|
.vmdq_rx_conf = {
|
|
|
|
.nb_queue_pools = ETH_8_POOLS,
|
|
|
|
.enable_default_pool = 0,
|
|
|
|
.default_pool = 0,
|
|
|
|
.nb_pool_maps = 0,
|
|
|
|
.pool_map = {{0, 0},},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-12-26 09:23:25 +00:00
|
|
|
|
2014-10-20 04:38:13 +00:00
|
|
|
static unsigned lcore_ids[RTE_MAX_LCORE];
|
2017-09-29 07:17:24 +00:00
|
|
|
static uint16_t ports[RTE_MAX_ETHPORTS];
|
2014-10-20 04:38:13 +00:00
|
|
|
static unsigned num_ports = 0; /**< The number of ports specified in command line */
|
2014-11-12 22:34:06 +00:00
|
|
|
static uint16_t num_pf_queues, num_vmdq_queues;
|
|
|
|
static uint16_t vmdq_pool_base, vmdq_queue_base;
|
|
|
|
static uint16_t queues_per_pool;
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
const uint16_t vlan_tags[] = {
|
|
|
|
1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007,
|
|
|
|
1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015,
|
|
|
|
1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023,
|
|
|
|
1024, 1025, 1026, 1027, 1028, 1029, 1030, 1031,
|
|
|
|
1032, 1033, 1034, 1035, 1036, 1037, 1038, 1039,
|
|
|
|
1040, 1041, 1042, 1043, 1044, 1045, 1046, 1047,
|
|
|
|
1048, 1049, 1050, 1051, 1052, 1053, 1054, 1055,
|
|
|
|
1056, 1057, 1058, 1059, 1060, 1061, 1062, 1063,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* ethernet addresses of ports */
|
2019-05-21 16:13:03 +00:00
|
|
|
static struct rte_ether_addr vmdq_ports_eth_addr[RTE_MAX_ETHPORTS];
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2016-05-02 21:23:45 +00:00
|
|
|
static struct vhost_dev_tailq_list vhost_dev_list =
|
|
|
|
TAILQ_HEAD_INITIALIZER(vhost_dev_list);
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
static struct lcore_info lcore_info[RTE_MAX_LCORE];
|
|
|
|
|
|
|
|
/* Used for queueing bursts of TX packets. */
|
|
|
|
struct mbuf_table {
|
|
|
|
unsigned len;
|
|
|
|
unsigned txq_id;
|
|
|
|
struct rte_mbuf *m_table[MAX_PKT_BURST];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* TX queue for each data core. */
|
|
|
|
struct mbuf_table lcore_tx_queue[RTE_MAX_LCORE];
|
|
|
|
|
2016-05-02 21:23:49 +00:00
|
|
|
#define MBUF_TABLE_DRAIN_TSC ((rte_get_tsc_hz() + US_PER_S - 1) \
|
|
|
|
/ US_PER_S * BURST_TX_DRAIN_US)
|
2014-10-20 04:38:13 +00:00
|
|
|
#define VLAN_HLEN 4
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Builds up the correct configuration for VMDQ VLAN pool map
|
|
|
|
* according to the pool & queue limits.
|
|
|
|
*/
|
|
|
|
static inline int
|
|
|
|
get_eth_conf(struct rte_eth_conf *eth_conf, uint32_t num_devices)
|
|
|
|
{
|
|
|
|
struct rte_eth_vmdq_rx_conf conf;
|
2014-11-08 04:26:16 +00:00
|
|
|
struct rte_eth_vmdq_rx_conf *def_conf =
|
|
|
|
&vmdq_conf_default.rx_adv_conf.vmdq_rx_conf;
|
2014-10-20 04:38:13 +00:00
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
memset(&conf, 0, sizeof(conf));
|
|
|
|
conf.nb_queue_pools = (enum rte_eth_nb_pools)num_devices;
|
|
|
|
conf.nb_pool_maps = num_devices;
|
2014-11-08 04:26:16 +00:00
|
|
|
conf.enable_loop_back = def_conf->enable_loop_back;
|
|
|
|
conf.rx_mode = def_conf->rx_mode;
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
for (i = 0; i < conf.nb_pool_maps; i++) {
|
|
|
|
conf.pool_map[i].vlan_id = vlan_tags[ i ];
|
|
|
|
conf.pool_map[i].pools = (1UL << i);
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)(rte_memcpy(eth_conf, &vmdq_conf_default, sizeof(*eth_conf)));
|
|
|
|
(void)(rte_memcpy(ð_conf->rx_adv_conf.vmdq_rx_conf, &conf,
|
|
|
|
sizeof(eth_conf->rx_adv_conf.vmdq_rx_conf)));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialises a given port using global settings and with the rx buffers
|
|
|
|
* coming from the mbuf_pool passed as parameter
|
|
|
|
*/
|
|
|
|
static inline int
|
2017-09-29 07:17:24 +00:00
|
|
|
port_init(uint16_t port)
|
2014-10-20 04:38:13 +00:00
|
|
|
{
|
|
|
|
struct rte_eth_dev_info dev_info;
|
|
|
|
struct rte_eth_conf port_conf;
|
2014-11-12 22:34:07 +00:00
|
|
|
struct rte_eth_rxconf *rxconf;
|
|
|
|
struct rte_eth_txconf *txconf;
|
|
|
|
int16_t rx_rings, tx_rings;
|
2014-10-20 04:38:13 +00:00
|
|
|
uint16_t rx_ring_size, tx_ring_size;
|
|
|
|
int retval;
|
|
|
|
uint16_t q;
|
|
|
|
|
|
|
|
/* The max pool number from dev_info will be used to validate the pool number specified in cmd line */
|
|
|
|
rte_eth_dev_info_get (port, &dev_info);
|
|
|
|
|
2014-11-12 22:34:07 +00:00
|
|
|
rxconf = &dev_info.default_rxconf;
|
|
|
|
txconf = &dev_info.default_txconf;
|
|
|
|
rxconf->rx_drop_en = 1;
|
2014-12-12 04:15:26 +00:00
|
|
|
|
2014-10-20 04:38:13 +00:00
|
|
|
/*configure the number of supported virtio devices based on VMDQ limits */
|
|
|
|
num_devices = dev_info.max_vmdq_pools;
|
|
|
|
|
2016-05-02 21:23:43 +00:00
|
|
|
rx_ring_size = RTE_TEST_RX_DESC_DEFAULT;
|
|
|
|
tx_ring_size = RTE_TEST_TX_DESC_DEFAULT;
|
2016-10-09 07:27:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* When dequeue zero copy is enabled, guest Tx used vring will be
|
|
|
|
* updated only when corresponding mbuf is freed. Thus, the nb_tx_desc
|
|
|
|
* (tx_ring_size here) must be small enough so that the driver will
|
|
|
|
* hit the free threshold easily and free mbufs timely. Otherwise,
|
|
|
|
* guest Tx vring would be starved.
|
|
|
|
*/
|
|
|
|
if (dequeue_zero_copy)
|
|
|
|
tx_ring_size = 64;
|
|
|
|
|
2016-05-02 21:23:43 +00:00
|
|
|
tx_rings = (uint16_t)rte_lcore_count();
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
/* Get port configuration. */
|
|
|
|
retval = get_eth_conf(&port_conf, num_devices);
|
|
|
|
if (retval < 0)
|
|
|
|
return retval;
|
2014-11-12 22:34:06 +00:00
|
|
|
/* NIC queues are divided into pf queues and vmdq queues. */
|
|
|
|
num_pf_queues = dev_info.max_rx_queues - dev_info.vmdq_queue_num;
|
|
|
|
queues_per_pool = dev_info.vmdq_queue_num / dev_info.max_vmdq_pools;
|
|
|
|
num_vmdq_queues = num_devices * queues_per_pool;
|
|
|
|
num_queues = num_pf_queues + num_vmdq_queues;
|
|
|
|
vmdq_queue_base = dev_info.vmdq_queue_base;
|
|
|
|
vmdq_pool_base = dev_info.vmdq_pool_base;
|
|
|
|
printf("pf queue num: %u, configured vmdq pool num: %u, each vmdq pool has %u queues\n",
|
|
|
|
num_pf_queues, num_devices, queues_per_pool);
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2018-04-05 15:33:21 +00:00
|
|
|
if (!rte_eth_dev_is_valid_port(port))
|
|
|
|
return -1;
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2014-11-12 22:34:06 +00:00
|
|
|
rx_rings = (uint16_t)dev_info.max_rx_queues;
|
2017-12-26 09:23:25 +00:00
|
|
|
if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
|
|
|
|
port_conf.txmode.offloads |=
|
|
|
|
DEV_TX_OFFLOAD_MBUF_FAST_FREE;
|
2014-10-20 04:38:13 +00:00
|
|
|
/* Configure ethernet device. */
|
|
|
|
retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
|
2016-07-01 08:50:31 +00:00
|
|
|
if (retval != 0) {
|
|
|
|
RTE_LOG(ERR, VHOST_PORT, "Failed to configure port %u: %s.\n",
|
|
|
|
port, strerror(-retval));
|
2014-10-20 04:38:13 +00:00
|
|
|
return retval;
|
2016-07-01 08:50:31 +00:00
|
|
|
}
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2017-05-25 15:57:54 +00:00
|
|
|
retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &rx_ring_size,
|
|
|
|
&tx_ring_size);
|
|
|
|
if (retval != 0) {
|
|
|
|
RTE_LOG(ERR, VHOST_PORT, "Failed to adjust number of descriptors "
|
|
|
|
"for port %u: %s.\n", port, strerror(-retval));
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
if (rx_ring_size > RTE_TEST_RX_DESC_DEFAULT) {
|
|
|
|
RTE_LOG(ERR, VHOST_PORT, "Mbuf pool has an insufficient size "
|
|
|
|
"for Rx queues on port %u.\n", port);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-10-20 04:38:13 +00:00
|
|
|
/* Setup the queues. */
|
2017-12-26 09:23:25 +00:00
|
|
|
rxconf->offloads = port_conf.rxmode.offloads;
|
2014-10-20 04:38:13 +00:00
|
|
|
for (q = 0; q < rx_rings; q ++) {
|
|
|
|
retval = rte_eth_rx_queue_setup(port, q, rx_ring_size,
|
2014-11-12 22:34:07 +00:00
|
|
|
rte_eth_dev_socket_id(port),
|
|
|
|
rxconf,
|
2016-05-02 21:23:43 +00:00
|
|
|
mbuf_pool);
|
2016-07-01 08:50:31 +00:00
|
|
|
if (retval < 0) {
|
|
|
|
RTE_LOG(ERR, VHOST_PORT,
|
|
|
|
"Failed to setup rx queue %u of port %u: %s.\n",
|
|
|
|
q, port, strerror(-retval));
|
2014-10-20 04:38:13 +00:00
|
|
|
return retval;
|
2016-07-01 08:50:31 +00:00
|
|
|
}
|
2014-10-20 04:38:13 +00:00
|
|
|
}
|
2017-12-26 09:23:25 +00:00
|
|
|
txconf->offloads = port_conf.txmode.offloads;
|
2014-10-20 04:38:13 +00:00
|
|
|
for (q = 0; q < tx_rings; q ++) {
|
|
|
|
retval = rte_eth_tx_queue_setup(port, q, tx_ring_size,
|
2014-11-12 22:34:07 +00:00
|
|
|
rte_eth_dev_socket_id(port),
|
|
|
|
txconf);
|
2016-07-01 08:50:31 +00:00
|
|
|
if (retval < 0) {
|
|
|
|
RTE_LOG(ERR, VHOST_PORT,
|
|
|
|
"Failed to setup tx queue %u of port %u: %s.\n",
|
|
|
|
q, port, strerror(-retval));
|
2014-10-20 04:38:13 +00:00
|
|
|
return retval;
|
2016-07-01 08:50:31 +00:00
|
|
|
}
|
2014-10-20 04:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Start the device. */
|
|
|
|
retval = rte_eth_dev_start(port);
|
|
|
|
if (retval < 0) {
|
2016-07-01 08:50:31 +00:00
|
|
|
RTE_LOG(ERR, VHOST_PORT, "Failed to start port %u: %s\n",
|
|
|
|
port, strerror(-retval));
|
2014-10-20 04:38:13 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2014-11-08 04:26:16 +00:00
|
|
|
if (promiscuous)
|
|
|
|
rte_eth_promiscuous_enable(port);
|
|
|
|
|
2014-10-20 04:38:13 +00:00
|
|
|
rte_eth_macaddr_get(port, &vmdq_ports_eth_addr[port]);
|
|
|
|
RTE_LOG(INFO, VHOST_PORT, "Max virtio devices supported: %u\n", num_devices);
|
|
|
|
RTE_LOG(INFO, VHOST_PORT, "Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
|
|
|
|
" %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
|
2017-09-29 07:17:24 +00:00
|
|
|
port,
|
2014-10-20 04:38:13 +00:00
|
|
|
vmdq_ports_eth_addr[port].addr_bytes[0],
|
|
|
|
vmdq_ports_eth_addr[port].addr_bytes[1],
|
|
|
|
vmdq_ports_eth_addr[port].addr_bytes[2],
|
|
|
|
vmdq_ports_eth_addr[port].addr_bytes[3],
|
|
|
|
vmdq_ports_eth_addr[port].addr_bytes[4],
|
|
|
|
vmdq_ports_eth_addr[port].addr_bytes[5]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-08-20 10:10:33 +00:00
|
|
|
* Set socket file path.
|
2014-10-20 04:38:13 +00:00
|
|
|
*/
|
|
|
|
static int
|
2016-08-20 10:10:33 +00:00
|
|
|
us_vhost_parse_socket_path(const char *q_arg)
|
2014-10-20 04:38:13 +00:00
|
|
|
{
|
2019-01-15 07:13:24 +00:00
|
|
|
char *old;
|
|
|
|
|
2014-10-20 04:38:13 +00:00
|
|
|
/* parse number string */
|
2017-04-16 11:39:40 +00:00
|
|
|
if (strnlen(q_arg, PATH_MAX) == PATH_MAX)
|
2014-10-20 04:38:13 +00:00
|
|
|
return -1;
|
2016-08-20 10:11:36 +00:00
|
|
|
|
2019-01-15 07:13:24 +00:00
|
|
|
old = socket_files;
|
2016-08-20 10:11:36 +00:00
|
|
|
socket_files = realloc(socket_files, PATH_MAX * (nb_sockets + 1));
|
2019-01-15 07:13:24 +00:00
|
|
|
if (socket_files == NULL) {
|
|
|
|
free(old);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-04-03 14:45:04 +00:00
|
|
|
strlcpy(socket_files + nb_sockets * PATH_MAX, q_arg, PATH_MAX);
|
2016-08-20 10:11:36 +00:00
|
|
|
nb_sockets++;
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse the portmask provided at run time.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
parse_portmask(const char *portmask)
|
|
|
|
{
|
|
|
|
char *end = NULL;
|
|
|
|
unsigned long pm;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
/* parse hexadecimal string */
|
|
|
|
pm = strtoul(portmask, &end, 16);
|
|
|
|
if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0') || (errno != 0))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (pm == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return pm;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse num options at run time.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
parse_num_opt(const char *q_arg, uint32_t max_valid_value)
|
|
|
|
{
|
|
|
|
char *end = NULL;
|
|
|
|
unsigned long num;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
/* parse unsigned int string */
|
|
|
|
num = strtoul(q_arg, &end, 10);
|
|
|
|
if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0') || (errno != 0))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (num > max_valid_value)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return num;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display usage
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
us_vhost_usage(const char *prgname)
|
|
|
|
{
|
|
|
|
RTE_LOG(INFO, VHOST_CONFIG, "%s [EAL options] -- -p PORTMASK\n"
|
|
|
|
" --vm2vm [0|1|2]\n"
|
|
|
|
" --rx_retry [0|1] --mergeable [0|1] --stats [0-N]\n"
|
2016-08-20 10:10:33 +00:00
|
|
|
" --socket-file <path>\n"
|
2014-10-20 04:38:13 +00:00
|
|
|
" --nb-devices ND\n"
|
|
|
|
" -p PORTMASK: Set mask for ports to be used by application\n"
|
|
|
|
" --vm2vm [0|1|2]: disable/software(default)/hardware vm2vm comms\n"
|
|
|
|
" --rx-retry [0|1]: disable/enable(default) retries on rx. Enable retry if destintation queue is full\n"
|
|
|
|
" --rx-retry-delay [0-N]: timeout(in usecond) between retries on RX. This makes effect only if retries on rx enabled\n"
|
|
|
|
" --rx-retry-num [0-N]: the number of retries on rx. This makes effect only if retries on rx enabled\n"
|
|
|
|
" --mergeable [0|1]: disable(default)/enable RX mergeable buffers\n"
|
|
|
|
" --stats [0-N]: 0: Disable stats, N: Time in seconds to print stats\n"
|
2016-08-20 10:10:33 +00:00
|
|
|
" --socket-file: The path of the socket file.\n"
|
2016-02-05 07:31:41 +00:00
|
|
|
" --tx-csum [0|1] disable/enable TX checksum offload.\n"
|
2016-05-07 05:23:40 +00:00
|
|
|
" --tso [0|1] disable/enable TCP segment offload.\n"
|
2016-10-09 07:27:59 +00:00
|
|
|
" --client register a vhost-user socket as client mode.\n"
|
|
|
|
" --dequeue-zero-copy enables dequeue zero copy\n",
|
2014-10-20 04:38:13 +00:00
|
|
|
prgname);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse the arguments given in the command line of the application.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
us_vhost_parse_args(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int opt, ret;
|
|
|
|
int option_index;
|
|
|
|
unsigned i;
|
|
|
|
const char *prgname = argv[0];
|
|
|
|
static struct option long_option[] = {
|
|
|
|
{"vm2vm", required_argument, NULL, 0},
|
|
|
|
{"rx-retry", required_argument, NULL, 0},
|
|
|
|
{"rx-retry-delay", required_argument, NULL, 0},
|
|
|
|
{"rx-retry-num", required_argument, NULL, 0},
|
|
|
|
{"mergeable", required_argument, NULL, 0},
|
|
|
|
{"stats", required_argument, NULL, 0},
|
2016-08-20 10:10:33 +00:00
|
|
|
{"socket-file", required_argument, NULL, 0},
|
2016-02-05 07:31:41 +00:00
|
|
|
{"tx-csum", required_argument, NULL, 0},
|
|
|
|
{"tso", required_argument, NULL, 0},
|
2016-05-07 05:23:40 +00:00
|
|
|
{"client", no_argument, &client_mode, 1},
|
2016-10-09 07:27:59 +00:00
|
|
|
{"dequeue-zero-copy", no_argument, &dequeue_zero_copy, 1},
|
2017-04-01 07:23:00 +00:00
|
|
|
{"builtin-net-driver", no_argument, &builtin_net_driver, 1},
|
2014-10-20 04:38:13 +00:00
|
|
|
{NULL, 0, 0, 0},
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Parse command line */
|
2014-11-08 04:26:16 +00:00
|
|
|
while ((opt = getopt_long(argc, argv, "p:P",
|
|
|
|
long_option, &option_index)) != EOF) {
|
2014-10-20 04:38:13 +00:00
|
|
|
switch (opt) {
|
|
|
|
/* Portmask */
|
|
|
|
case 'p':
|
|
|
|
enabled_port_mask = parse_portmask(optarg);
|
|
|
|
if (enabled_port_mask == 0) {
|
|
|
|
RTE_LOG(INFO, VHOST_CONFIG, "Invalid portmask\n");
|
|
|
|
us_vhost_usage(prgname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-11-08 04:26:16 +00:00
|
|
|
case 'P':
|
|
|
|
promiscuous = 1;
|
|
|
|
vmdq_conf_default.rx_adv_conf.vmdq_rx_conf.rx_mode =
|
|
|
|
ETH_VMDQ_ACCEPT_BROADCAST |
|
|
|
|
ETH_VMDQ_ACCEPT_MULTICAST;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2014-10-20 04:38:13 +00:00
|
|
|
case 0:
|
|
|
|
/* Enable/disable vm2vm comms. */
|
|
|
|
if (!strncmp(long_option[option_index].name, "vm2vm",
|
|
|
|
MAX_LONG_OPT_SZ)) {
|
|
|
|
ret = parse_num_opt(optarg, (VM2VM_LAST - 1));
|
|
|
|
if (ret == -1) {
|
|
|
|
RTE_LOG(INFO, VHOST_CONFIG,
|
|
|
|
"Invalid argument for "
|
|
|
|
"vm2vm [0|1|2]\n");
|
|
|
|
us_vhost_usage(prgname);
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
vm2vm_mode = (vm2vm_type)ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable/disable retries on RX. */
|
|
|
|
if (!strncmp(long_option[option_index].name, "rx-retry", MAX_LONG_OPT_SZ)) {
|
|
|
|
ret = parse_num_opt(optarg, 1);
|
|
|
|
if (ret == -1) {
|
|
|
|
RTE_LOG(INFO, VHOST_CONFIG, "Invalid argument for rx-retry [0|1]\n");
|
|
|
|
us_vhost_usage(prgname);
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
enable_retry = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 07:31:41 +00:00
|
|
|
/* Enable/disable TX checksum offload. */
|
|
|
|
if (!strncmp(long_option[option_index].name, "tx-csum", MAX_LONG_OPT_SZ)) {
|
|
|
|
ret = parse_num_opt(optarg, 1);
|
|
|
|
if (ret == -1) {
|
|
|
|
RTE_LOG(INFO, VHOST_CONFIG, "Invalid argument for tx-csum [0|1]\n");
|
|
|
|
us_vhost_usage(prgname);
|
|
|
|
return -1;
|
|
|
|
} else
|
|
|
|
enable_tx_csum = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable/disable TSO offload. */
|
|
|
|
if (!strncmp(long_option[option_index].name, "tso", MAX_LONG_OPT_SZ)) {
|
|
|
|
ret = parse_num_opt(optarg, 1);
|
|
|
|
if (ret == -1) {
|
|
|
|
RTE_LOG(INFO, VHOST_CONFIG, "Invalid argument for tso [0|1]\n");
|
|
|
|
us_vhost_usage(prgname);
|
|
|
|
return -1;
|
|
|
|
} else
|
|
|
|
enable_tso = ret;
|
|
|
|
}
|
|
|
|
|
2014-10-20 04:38:13 +00:00
|
|
|
/* Specify the retries delay time (in useconds) on RX. */
|
|
|
|
if (!strncmp(long_option[option_index].name, "rx-retry-delay", MAX_LONG_OPT_SZ)) {
|
|
|
|
ret = parse_num_opt(optarg, INT32_MAX);
|
|
|
|
if (ret == -1) {
|
|
|
|
RTE_LOG(INFO, VHOST_CONFIG, "Invalid argument for rx-retry-delay [0-N]\n");
|
|
|
|
us_vhost_usage(prgname);
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
burst_rx_delay_time = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Specify the retries number on RX. */
|
|
|
|
if (!strncmp(long_option[option_index].name, "rx-retry-num", MAX_LONG_OPT_SZ)) {
|
|
|
|
ret = parse_num_opt(optarg, INT32_MAX);
|
|
|
|
if (ret == -1) {
|
|
|
|
RTE_LOG(INFO, VHOST_CONFIG, "Invalid argument for rx-retry-num [0-N]\n");
|
|
|
|
us_vhost_usage(prgname);
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
burst_rx_retry_num = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable/disable RX mergeable buffers. */
|
|
|
|
if (!strncmp(long_option[option_index].name, "mergeable", MAX_LONG_OPT_SZ)) {
|
|
|
|
ret = parse_num_opt(optarg, 1);
|
|
|
|
if (ret == -1) {
|
|
|
|
RTE_LOG(INFO, VHOST_CONFIG, "Invalid argument for mergeable [0|1]\n");
|
|
|
|
us_vhost_usage(prgname);
|
|
|
|
return -1;
|
|
|
|
} else {
|
2014-10-23 09:21:13 +00:00
|
|
|
mergeable = !!ret;
|
2014-10-20 04:38:13 +00:00
|
|
|
if (ret) {
|
2017-12-26 09:23:25 +00:00
|
|
|
vmdq_conf_default.rxmode.offloads |=
|
|
|
|
DEV_RX_OFFLOAD_JUMBO_FRAME;
|
2014-10-20 04:38:13 +00:00
|
|
|
vmdq_conf_default.rxmode.max_rx_pkt_len
|
|
|
|
= JUMBO_FRAME_MAX_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable/disable stats. */
|
|
|
|
if (!strncmp(long_option[option_index].name, "stats", MAX_LONG_OPT_SZ)) {
|
|
|
|
ret = parse_num_opt(optarg, INT32_MAX);
|
|
|
|
if (ret == -1) {
|
2016-08-20 10:10:33 +00:00
|
|
|
RTE_LOG(INFO, VHOST_CONFIG,
|
|
|
|
"Invalid argument for stats [0..N]\n");
|
2014-10-20 04:38:13 +00:00
|
|
|
us_vhost_usage(prgname);
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
enable_stats = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-20 10:10:33 +00:00
|
|
|
/* Set socket file path. */
|
|
|
|
if (!strncmp(long_option[option_index].name,
|
|
|
|
"socket-file", MAX_LONG_OPT_SZ)) {
|
|
|
|
if (us_vhost_parse_socket_path(optarg) == -1) {
|
|
|
|
RTE_LOG(INFO, VHOST_CONFIG,
|
|
|
|
"Invalid argument for socket name (Max %d characters)\n",
|
|
|
|
PATH_MAX);
|
2014-10-20 04:38:13 +00:00
|
|
|
us_vhost_usage(prgname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Invalid option - print options. */
|
|
|
|
default:
|
|
|
|
us_vhost_usage(prgname);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
|
|
|
|
if (enabled_port_mask & (1 << i))
|
2017-09-29 07:17:24 +00:00
|
|
|
ports[num_ports++] = i;
|
2014-10-20 04:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((num_ports == 0) || (num_ports > MAX_SUP_PORTS)) {
|
|
|
|
RTE_LOG(INFO, VHOST_PORT, "Current enabled port number is %u,"
|
|
|
|
"but only %u port can be enabled\n",num_ports, MAX_SUP_PORTS);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the global var NUM_PORTS and array PORTS according to system ports number
|
|
|
|
* and return valid ports number
|
|
|
|
*/
|
|
|
|
static unsigned check_ports_num(unsigned nb_ports)
|
|
|
|
{
|
|
|
|
unsigned valid_num_ports = num_ports;
|
|
|
|
unsigned portid;
|
|
|
|
|
|
|
|
if (num_ports > nb_ports) {
|
|
|
|
RTE_LOG(INFO, VHOST_PORT, "\nSpecified port number(%u) exceeds total system port number(%u)\n",
|
|
|
|
num_ports, nb_ports);
|
|
|
|
num_ports = nb_ports;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (portid = 0; portid < num_ports; portid ++) {
|
2018-04-05 15:33:21 +00:00
|
|
|
if (!rte_eth_dev_is_valid_port(ports[portid])) {
|
|
|
|
RTE_LOG(INFO, VHOST_PORT,
|
|
|
|
"\nSpecified port ID(%u) is not valid\n",
|
|
|
|
ports[portid]);
|
2014-10-20 04:38:13 +00:00
|
|
|
ports[portid] = INVALID_PORT_ID;
|
|
|
|
valid_num_ports--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return valid_num_ports;
|
|
|
|
}
|
|
|
|
|
2017-05-13 09:27:25 +00:00
|
|
|
static __rte_always_inline struct vhost_dev *
|
2019-05-21 16:13:03 +00:00
|
|
|
find_vhost_dev(struct rte_ether_addr *mac)
|
2016-05-02 21:23:45 +00:00
|
|
|
{
|
|
|
|
struct vhost_dev *vdev;
|
|
|
|
|
2016-06-07 03:32:56 +00:00
|
|
|
TAILQ_FOREACH(vdev, &vhost_dev_list, global_vdev_entry) {
|
2016-05-02 21:23:45 +00:00
|
|
|
if (vdev->ready == DEVICE_RX &&
|
2019-05-21 16:13:04 +00:00
|
|
|
rte_is_same_ether_addr(mac, &vdev->mac_address))
|
2016-05-02 21:23:45 +00:00
|
|
|
return vdev;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-10-20 04:38:13 +00:00
|
|
|
/*
|
|
|
|
* This function learns the MAC address of the device and registers this along with a
|
|
|
|
* vlan tag to a VMDQ.
|
|
|
|
*/
|
|
|
|
static int
|
2014-10-20 04:38:16 +00:00
|
|
|
link_vmdq(struct vhost_dev *vdev, struct rte_mbuf *m)
|
2014-10-20 04:38:13 +00:00
|
|
|
{
|
2019-05-21 16:13:03 +00:00
|
|
|
struct rte_ether_hdr *pkt_hdr;
|
2014-10-20 04:38:13 +00:00
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
/* Learn MAC address of guest device from packet */
|
2019-05-21 16:13:03 +00:00
|
|
|
pkt_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2016-05-02 21:23:45 +00:00
|
|
|
if (find_vhost_dev(&pkt_hdr->s_addr)) {
|
|
|
|
RTE_LOG(ERR, VHOST_DATA,
|
2016-04-29 20:45:51 +00:00
|
|
|
"(%d) device is using a registered MAC!\n",
|
2016-05-23 08:36:33 +00:00
|
|
|
vdev->vid);
|
2016-05-02 21:23:45 +00:00
|
|
|
return -1;
|
2014-10-20 04:38:13 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 16:13:05 +00:00
|
|
|
for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
|
2014-10-20 04:38:16 +00:00
|
|
|
vdev->mac_address.addr_bytes[i] = pkt_hdr->s_addr.addr_bytes[i];
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
/* vlan_tag currently uses the device_id. */
|
2016-05-23 08:36:33 +00:00
|
|
|
vdev->vlan_tag = vlan_tags[vdev->vid];
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
/* Print out VMDQ registration info. */
|
2016-04-29 20:45:51 +00:00
|
|
|
RTE_LOG(INFO, VHOST_DATA,
|
|
|
|
"(%d) mac %02x:%02x:%02x:%02x:%02x:%02x and vlan %d registered\n",
|
2016-05-23 08:36:33 +00:00
|
|
|
vdev->vid,
|
2014-10-20 04:38:16 +00:00
|
|
|
vdev->mac_address.addr_bytes[0], vdev->mac_address.addr_bytes[1],
|
|
|
|
vdev->mac_address.addr_bytes[2], vdev->mac_address.addr_bytes[3],
|
|
|
|
vdev->mac_address.addr_bytes[4], vdev->mac_address.addr_bytes[5],
|
|
|
|
vdev->vlan_tag);
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
/* Register the MAC address. */
|
2014-11-12 22:34:06 +00:00
|
|
|
ret = rte_eth_dev_mac_addr_add(ports[0], &vdev->mac_address,
|
2016-05-23 08:36:33 +00:00
|
|
|
(uint32_t)vdev->vid + vmdq_pool_base);
|
2014-10-20 04:38:13 +00:00
|
|
|
if (ret)
|
2016-04-29 20:45:51 +00:00
|
|
|
RTE_LOG(ERR, VHOST_DATA,
|
|
|
|
"(%d) failed to add device MAC address to VMDQ\n",
|
2016-05-23 08:36:33 +00:00
|
|
|
vdev->vid);
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2016-08-18 05:46:13 +00:00
|
|
|
rte_eth_dev_set_vlan_strip_on_queue(ports[0], vdev->vmdq_rx_q, 1);
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
/* Set device as ready for RX. */
|
2014-10-20 04:38:16 +00:00
|
|
|
vdev->ready = DEVICE_RX;
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Removes MAC address and vlan tag from VMDQ. Ensures that nothing is adding buffers to the RX
|
|
|
|
* queue before disabling RX on the device.
|
|
|
|
*/
|
|
|
|
static inline void
|
2014-10-20 04:38:16 +00:00
|
|
|
unlink_vmdq(struct vhost_dev *vdev)
|
2014-10-20 04:38:13 +00:00
|
|
|
{
|
|
|
|
unsigned i = 0;
|
|
|
|
unsigned rx_count;
|
|
|
|
struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
|
|
|
|
|
2014-10-20 04:38:16 +00:00
|
|
|
if (vdev->ready == DEVICE_RX) {
|
2014-10-20 04:38:13 +00:00
|
|
|
/*clear MAC and VLAN settings*/
|
2014-10-20 04:38:16 +00:00
|
|
|
rte_eth_dev_mac_addr_remove(ports[0], &vdev->mac_address);
|
2014-10-20 04:38:13 +00:00
|
|
|
for (i = 0; i < 6; i++)
|
2014-10-20 04:38:16 +00:00
|
|
|
vdev->mac_address.addr_bytes[i] = 0;
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2014-10-20 04:38:16 +00:00
|
|
|
vdev->vlan_tag = 0;
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
/*Clear out the receive buffers*/
|
|
|
|
rx_count = rte_eth_rx_burst(ports[0],
|
2014-10-20 04:38:16 +00:00
|
|
|
(uint16_t)vdev->vmdq_rx_q, pkts_burst, MAX_PKT_BURST);
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
while (rx_count) {
|
|
|
|
for (i = 0; i < rx_count; i++)
|
|
|
|
rte_pktmbuf_free(pkts_burst[i]);
|
|
|
|
|
|
|
|
rx_count = rte_eth_rx_burst(ports[0],
|
2014-10-20 04:38:16 +00:00
|
|
|
(uint16_t)vdev->vmdq_rx_q, pkts_burst, MAX_PKT_BURST);
|
2014-10-20 04:38:13 +00:00
|
|
|
}
|
|
|
|
|
2014-10-20 04:38:16 +00:00
|
|
|
vdev->ready = DEVICE_MAC_LEARNING;
|
2014-10-20 04:38:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-13 09:27:25 +00:00
|
|
|
static __rte_always_inline void
|
2016-05-02 21:23:50 +00:00
|
|
|
virtio_xmit(struct vhost_dev *dst_vdev, struct vhost_dev *src_vdev,
|
2016-05-02 21:23:47 +00:00
|
|
|
struct rte_mbuf *m)
|
|
|
|
{
|
|
|
|
uint16_t ret;
|
|
|
|
|
2017-04-01 07:23:00 +00:00
|
|
|
if (builtin_net_driver) {
|
|
|
|
ret = vs_enqueue_pkts(dst_vdev, VIRTIO_RXQ, &m, 1);
|
|
|
|
} else {
|
|
|
|
ret = rte_vhost_enqueue_burst(dst_vdev->vid, VIRTIO_RXQ, &m, 1);
|
|
|
|
}
|
|
|
|
|
2016-05-02 21:23:47 +00:00
|
|
|
if (enable_stats) {
|
2016-05-02 21:23:50 +00:00
|
|
|
rte_atomic64_inc(&dst_vdev->stats.rx_total_atomic);
|
|
|
|
rte_atomic64_add(&dst_vdev->stats.rx_atomic, ret);
|
|
|
|
src_vdev->stats.tx_total++;
|
|
|
|
src_vdev->stats.tx += ret;
|
2016-05-02 21:23:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-20 04:38:13 +00:00
|
|
|
/*
|
|
|
|
* Check if the packet destination MAC address is for a local device. If so then put
|
|
|
|
* the packet on that devices RX queue. If not then return.
|
|
|
|
*/
|
2017-05-13 09:27:25 +00:00
|
|
|
static __rte_always_inline int
|
2014-10-20 04:38:16 +00:00
|
|
|
virtio_tx_local(struct vhost_dev *vdev, struct rte_mbuf *m)
|
2014-10-20 04:38:13 +00:00
|
|
|
{
|
2019-05-21 16:13:03 +00:00
|
|
|
struct rte_ether_hdr *pkt_hdr;
|
2016-05-02 21:23:45 +00:00
|
|
|
struct vhost_dev *dst_vdev;
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2019-05-21 16:13:03 +00:00
|
|
|
pkt_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2016-05-02 21:23:45 +00:00
|
|
|
dst_vdev = find_vhost_dev(&pkt_hdr->d_addr);
|
|
|
|
if (!dst_vdev)
|
|
|
|
return -1;
|
|
|
|
|
2016-05-23 08:36:33 +00:00
|
|
|
if (vdev->vid == dst_vdev->vid) {
|
2016-11-23 15:34:25 +00:00
|
|
|
RTE_LOG_DP(DEBUG, VHOST_DATA,
|
2016-04-29 20:45:51 +00:00
|
|
|
"(%d) TX: src and dst MAC is same. Dropping packet.\n",
|
2016-05-23 08:36:33 +00:00
|
|
|
vdev->vid);
|
2016-05-02 21:23:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2016-11-23 15:34:25 +00:00
|
|
|
RTE_LOG_DP(DEBUG, VHOST_DATA,
|
2016-05-23 08:36:33 +00:00
|
|
|
"(%d) TX: MAC address is local\n", dst_vdev->vid);
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2016-05-02 21:23:45 +00:00
|
|
|
if (unlikely(dst_vdev->remove)) {
|
2016-11-23 15:34:25 +00:00
|
|
|
RTE_LOG_DP(DEBUG, VHOST_DATA,
|
2016-05-23 08:36:33 +00:00
|
|
|
"(%d) device is marked for removal\n", dst_vdev->vid);
|
2016-05-02 21:23:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2016-05-02 21:23:50 +00:00
|
|
|
virtio_xmit(dst_vdev, vdev, m);
|
2016-05-02 21:23:45 +00:00
|
|
|
return 0;
|
2014-10-20 04:38:13 +00:00
|
|
|
}
|
|
|
|
|
2014-11-05 07:10:34 +00:00
|
|
|
/*
|
|
|
|
* Check if the destination MAC of a packet is one local VM,
|
|
|
|
* and get its vlan tag, and offset if it is.
|
|
|
|
*/
|
2017-05-13 09:27:25 +00:00
|
|
|
static __rte_always_inline int
|
2016-05-23 08:35:43 +00:00
|
|
|
find_local_dest(struct vhost_dev *vdev, struct rte_mbuf *m,
|
2014-11-05 07:10:34 +00:00
|
|
|
uint32_t *offset, uint16_t *vlan_tag)
|
|
|
|
{
|
2016-05-02 21:23:45 +00:00
|
|
|
struct vhost_dev *dst_vdev;
|
2019-05-21 16:13:03 +00:00
|
|
|
struct rte_ether_hdr *pkt_hdr =
|
|
|
|
rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
|
2014-11-05 07:10:34 +00:00
|
|
|
|
2016-05-02 21:23:45 +00:00
|
|
|
dst_vdev = find_vhost_dev(&pkt_hdr->d_addr);
|
|
|
|
if (!dst_vdev)
|
|
|
|
return 0;
|
2014-11-05 07:10:34 +00:00
|
|
|
|
2016-05-23 08:36:33 +00:00
|
|
|
if (vdev->vid == dst_vdev->vid) {
|
2016-11-23 15:34:25 +00:00
|
|
|
RTE_LOG_DP(DEBUG, VHOST_DATA,
|
2016-04-29 20:45:51 +00:00
|
|
|
"(%d) TX: src and dst MAC is same. Dropping packet.\n",
|
2016-05-23 08:36:33 +00:00
|
|
|
vdev->vid);
|
2016-05-02 21:23:45 +00:00
|
|
|
return -1;
|
2014-11-05 07:10:34 +00:00
|
|
|
}
|
2016-05-02 21:23:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* HW vlan strip will reduce the packet length
|
|
|
|
* by minus length of vlan tag, so need restore
|
|
|
|
* the packet length by plus it.
|
|
|
|
*/
|
|
|
|
*offset = VLAN_HLEN;
|
2016-05-23 08:36:33 +00:00
|
|
|
*vlan_tag = vlan_tags[vdev->vid];
|
2016-05-02 21:23:45 +00:00
|
|
|
|
2016-11-23 15:34:25 +00:00
|
|
|
RTE_LOG_DP(DEBUG, VHOST_DATA,
|
2016-05-23 08:35:43 +00:00
|
|
|
"(%d) TX: pkt to local VM device id: (%d), vlan tag: %u.\n",
|
2016-05-23 08:36:33 +00:00
|
|
|
vdev->vid, dst_vdev->vid, *vlan_tag);
|
2016-05-02 21:23:45 +00:00
|
|
|
|
2014-11-05 07:10:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-05 07:31:41 +00:00
|
|
|
static uint16_t
|
|
|
|
get_psd_sum(void *l3_hdr, uint64_t ol_flags)
|
|
|
|
{
|
|
|
|
if (ol_flags & PKT_TX_IPV4)
|
|
|
|
return rte_ipv4_phdr_cksum(l3_hdr, ol_flags);
|
2019-05-21 16:13:05 +00:00
|
|
|
else /* assume ethertype == RTE_ETHER_TYPE_IPv6 */
|
2016-02-05 07:31:41 +00:00
|
|
|
return rte_ipv6_phdr_cksum(l3_hdr, ol_flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_tx_offload(struct rte_mbuf *m)
|
|
|
|
{
|
|
|
|
void *l3_hdr;
|
2019-05-21 16:13:10 +00:00
|
|
|
struct rte_ipv4_hdr *ipv4_hdr = NULL;
|
2019-05-21 16:13:13 +00:00
|
|
|
struct rte_tcp_hdr *tcp_hdr = NULL;
|
2019-05-21 16:13:03 +00:00
|
|
|
struct rte_ether_hdr *eth_hdr =
|
|
|
|
rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
|
2016-02-05 07:31:41 +00:00
|
|
|
|
|
|
|
l3_hdr = (char *)eth_hdr + m->l2_len;
|
|
|
|
|
2016-03-25 07:58:51 +00:00
|
|
|
if (m->ol_flags & PKT_TX_IPV4) {
|
|
|
|
ipv4_hdr = l3_hdr;
|
|
|
|
ipv4_hdr->hdr_checksum = 0;
|
|
|
|
m->ol_flags |= PKT_TX_IP_CKSUM;
|
|
|
|
}
|
|
|
|
|
2019-05-21 16:13:13 +00:00
|
|
|
tcp_hdr = (struct rte_tcp_hdr *)((char *)l3_hdr + m->l3_len);
|
2016-03-25 07:58:50 +00:00
|
|
|
tcp_hdr->cksum = get_psd_sum(l3_hdr, m->ol_flags);
|
2016-02-05 07:31:41 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 21:23:49 +00:00
|
|
|
static inline void
|
|
|
|
free_pkts(struct rte_mbuf **pkts, uint16_t n)
|
|
|
|
{
|
|
|
|
while (n--)
|
|
|
|
rte_pktmbuf_free(pkts[n]);
|
|
|
|
}
|
|
|
|
|
2017-05-13 09:27:25 +00:00
|
|
|
static __rte_always_inline void
|
2016-05-02 21:23:49 +00:00
|
|
|
do_drain_mbuf_table(struct mbuf_table *tx_q)
|
|
|
|
{
|
|
|
|
uint16_t count;
|
|
|
|
|
|
|
|
count = rte_eth_tx_burst(ports[0], tx_q->txq_id,
|
|
|
|
tx_q->m_table, tx_q->len);
|
|
|
|
if (unlikely(count < tx_q->len))
|
|
|
|
free_pkts(&tx_q->m_table[count], tx_q->len - count);
|
|
|
|
|
|
|
|
tx_q->len = 0;
|
|
|
|
}
|
|
|
|
|
2014-10-20 04:38:13 +00:00
|
|
|
/*
|
2016-05-02 21:23:49 +00:00
|
|
|
* This function routes the TX packet to the correct interface. This
|
|
|
|
* may be a local device or the physical port.
|
2014-10-20 04:38:13 +00:00
|
|
|
*/
|
2017-05-13 09:27:25 +00:00
|
|
|
static __rte_always_inline void
|
2014-10-20 04:38:19 +00:00
|
|
|
virtio_tx_route(struct vhost_dev *vdev, struct rte_mbuf *m, uint16_t vlan_tag)
|
2014-10-20 04:38:13 +00:00
|
|
|
{
|
|
|
|
struct mbuf_table *tx_q;
|
2016-05-02 21:23:49 +00:00
|
|
|
unsigned offset = 0;
|
2014-10-20 04:38:13 +00:00
|
|
|
const uint16_t lcore_id = rte_lcore_id();
|
2019-05-21 16:13:03 +00:00
|
|
|
struct rte_ether_hdr *nh;
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2016-05-02 21:23:47 +00:00
|
|
|
|
2019-05-21 16:13:03 +00:00
|
|
|
nh = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
|
2019-05-21 16:13:04 +00:00
|
|
|
if (unlikely(rte_is_broadcast_ether_addr(&nh->d_addr))) {
|
2016-05-02 21:23:47 +00:00
|
|
|
struct vhost_dev *vdev2;
|
|
|
|
|
2016-06-07 03:32:56 +00:00
|
|
|
TAILQ_FOREACH(vdev2, &vhost_dev_list, global_vdev_entry) {
|
2017-12-29 14:33:19 +00:00
|
|
|
if (vdev2 != vdev)
|
|
|
|
virtio_xmit(vdev2, vdev, m);
|
2016-05-02 21:23:47 +00:00
|
|
|
}
|
|
|
|
goto queue2nic;
|
|
|
|
}
|
|
|
|
|
2014-10-20 04:38:13 +00:00
|
|
|
/*check if destination is local VM*/
|
2014-10-20 04:38:19 +00:00
|
|
|
if ((vm2vm_mode == VM2VM_SOFTWARE) && (virtio_tx_local(vdev, m) == 0)) {
|
|
|
|
rte_pktmbuf_free(m);
|
2014-10-20 04:38:13 +00:00
|
|
|
return;
|
2014-10-20 04:38:19 +00:00
|
|
|
}
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2014-12-10 12:11:47 +00:00
|
|
|
if (unlikely(vm2vm_mode == VM2VM_HARDWARE)) {
|
2016-05-23 08:35:43 +00:00
|
|
|
if (unlikely(find_local_dest(vdev, m, &offset,
|
|
|
|
&vlan_tag) != 0)) {
|
2014-11-05 07:10:34 +00:00
|
|
|
rte_pktmbuf_free(m);
|
|
|
|
return;
|
2014-10-20 04:38:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 15:34:25 +00:00
|
|
|
RTE_LOG_DP(DEBUG, VHOST_DATA,
|
2016-05-23 08:36:33 +00:00
|
|
|
"(%d) TX: MAC address is external\n", vdev->vid);
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2016-05-02 21:23:47 +00:00
|
|
|
queue2nic:
|
|
|
|
|
2014-10-20 04:38:13 +00:00
|
|
|
/*Add packet to the port tx queue*/
|
|
|
|
tx_q = &lcore_tx_queue[lcore_id];
|
|
|
|
|
2019-05-21 16:13:03 +00:00
|
|
|
nh = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
|
2019-05-21 16:13:05 +00:00
|
|
|
if (unlikely(nh->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN))) {
|
2015-02-09 01:14:09 +00:00
|
|
|
/* Guest has inserted the vlan tag. */
|
2019-05-21 16:13:03 +00:00
|
|
|
struct rte_vlan_hdr *vh = (struct rte_vlan_hdr *) (nh + 1);
|
2015-02-09 01:14:09 +00:00
|
|
|
uint16_t vlan_tag_be = rte_cpu_to_be_16(vlan_tag);
|
|
|
|
if ((vm2vm_mode == VM2VM_HARDWARE) &&
|
|
|
|
(vh->vlan_tci != vlan_tag_be))
|
|
|
|
vh->vlan_tci = vlan_tag_be;
|
|
|
|
} else {
|
2016-02-05 07:31:41 +00:00
|
|
|
m->ol_flags |= PKT_TX_VLAN_PKT;
|
2014-11-05 07:10:33 +00:00
|
|
|
|
2015-02-09 01:14:09 +00:00
|
|
|
/*
|
|
|
|
* Find the right seg to adjust the data len when offset is
|
|
|
|
* bigger than tail room size.
|
|
|
|
*/
|
|
|
|
if (unlikely(vm2vm_mode == VM2VM_HARDWARE)) {
|
|
|
|
if (likely(offset <= rte_pktmbuf_tailroom(m)))
|
|
|
|
m->data_len += offset;
|
|
|
|
else {
|
|
|
|
struct rte_mbuf *seg = m;
|
2014-12-10 12:11:47 +00:00
|
|
|
|
2015-02-09 01:14:09 +00:00
|
|
|
while ((seg->next != NULL) &&
|
|
|
|
(offset > rte_pktmbuf_tailroom(seg)))
|
|
|
|
seg = seg->next;
|
2014-12-10 12:11:47 +00:00
|
|
|
|
2015-02-09 01:14:09 +00:00
|
|
|
seg->data_len += offset;
|
|
|
|
}
|
|
|
|
m->pkt_len += offset;
|
2014-12-10 12:11:47 +00:00
|
|
|
}
|
2014-11-05 07:10:33 +00:00
|
|
|
|
2015-02-09 01:14:09 +00:00
|
|
|
m->vlan_tci = vlan_tag;
|
|
|
|
}
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2016-03-25 07:58:50 +00:00
|
|
|
if (m->ol_flags & PKT_TX_TCP_SEG)
|
2016-02-05 07:31:41 +00:00
|
|
|
virtio_tx_offload(m);
|
|
|
|
|
2016-05-02 21:23:49 +00:00
|
|
|
tx_q->m_table[tx_q->len++] = m;
|
2014-10-20 04:38:13 +00:00
|
|
|
if (enable_stats) {
|
2016-05-02 21:23:50 +00:00
|
|
|
vdev->stats.tx_total++;
|
|
|
|
vdev->stats.tx++;
|
2014-10-20 04:38:13 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 21:23:49 +00:00
|
|
|
if (unlikely(tx_q->len == MAX_PKT_BURST))
|
|
|
|
do_drain_mbuf_table(tx_q);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-13 09:27:25 +00:00
|
|
|
static __rte_always_inline void
|
2016-05-02 21:23:49 +00:00
|
|
|
drain_mbuf_table(struct mbuf_table *tx_q)
|
|
|
|
{
|
|
|
|
static uint64_t prev_tsc;
|
|
|
|
uint64_t cur_tsc;
|
|
|
|
|
|
|
|
if (tx_q->len == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cur_tsc = rte_rdtsc();
|
|
|
|
if (unlikely(cur_tsc - prev_tsc > MBUF_TABLE_DRAIN_TSC)) {
|
|
|
|
prev_tsc = cur_tsc;
|
|
|
|
|
2016-11-23 15:34:25 +00:00
|
|
|
RTE_LOG_DP(DEBUG, VHOST_DATA,
|
2016-05-02 21:23:49 +00:00
|
|
|
"TX queue drained after timeout with burst size %u\n",
|
|
|
|
tx_q->len);
|
|
|
|
do_drain_mbuf_table(tx_q);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-13 09:27:25 +00:00
|
|
|
static __rte_always_inline void
|
2016-05-02 21:23:49 +00:00
|
|
|
drain_eth_rx(struct vhost_dev *vdev)
|
|
|
|
{
|
|
|
|
uint16_t rx_count, enqueue_count;
|
|
|
|
struct rte_mbuf *pkts[MAX_PKT_BURST];
|
|
|
|
|
|
|
|
rx_count = rte_eth_rx_burst(ports[0], vdev->vmdq_rx_q,
|
|
|
|
pkts, MAX_PKT_BURST);
|
|
|
|
if (!rx_count)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When "enable_retry" is set, here we wait and retry when there
|
|
|
|
* is no enough free slots in the queue to hold @rx_count packets,
|
|
|
|
* to diminish packet loss.
|
|
|
|
*/
|
|
|
|
if (enable_retry &&
|
2016-06-13 09:55:49 +00:00
|
|
|
unlikely(rx_count > rte_vhost_avail_entries(vdev->vid,
|
2016-05-02 21:23:49 +00:00
|
|
|
VIRTIO_RXQ))) {
|
|
|
|
uint32_t retry;
|
|
|
|
|
|
|
|
for (retry = 0; retry < burst_rx_retry_num; retry++) {
|
|
|
|
rte_delay_us(burst_rx_delay_time);
|
2016-06-13 09:55:49 +00:00
|
|
|
if (rx_count <= rte_vhost_avail_entries(vdev->vid,
|
2016-05-02 21:23:49 +00:00
|
|
|
VIRTIO_RXQ))
|
|
|
|
break;
|
2014-10-20 04:38:13 +00:00
|
|
|
}
|
2016-05-02 21:23:49 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 07:23:00 +00:00
|
|
|
if (builtin_net_driver) {
|
|
|
|
enqueue_count = vs_enqueue_pkts(vdev, VIRTIO_RXQ,
|
2016-05-02 21:23:49 +00:00
|
|
|
pkts, rx_count);
|
2017-04-01 07:23:00 +00:00
|
|
|
} else {
|
|
|
|
enqueue_count = rte_vhost_enqueue_burst(vdev->vid, VIRTIO_RXQ,
|
|
|
|
pkts, rx_count);
|
|
|
|
}
|
2016-05-02 21:23:49 +00:00
|
|
|
if (enable_stats) {
|
2016-05-02 21:23:50 +00:00
|
|
|
rte_atomic64_add(&vdev->stats.rx_total_atomic, rx_count);
|
|
|
|
rte_atomic64_add(&vdev->stats.rx_atomic, enqueue_count);
|
2016-05-02 21:23:49 +00:00
|
|
|
}
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2016-05-02 21:23:49 +00:00
|
|
|
free_pkts(pkts, rx_count);
|
|
|
|
}
|
|
|
|
|
2017-05-13 09:27:25 +00:00
|
|
|
static __rte_always_inline void
|
2016-05-02 21:23:49 +00:00
|
|
|
drain_virtio_tx(struct vhost_dev *vdev)
|
|
|
|
{
|
|
|
|
struct rte_mbuf *pkts[MAX_PKT_BURST];
|
|
|
|
uint16_t count;
|
|
|
|
uint16_t i;
|
|
|
|
|
2017-04-01 07:23:00 +00:00
|
|
|
if (builtin_net_driver) {
|
|
|
|
count = vs_dequeue_pkts(vdev, VIRTIO_TXQ, mbuf_pool,
|
2016-05-02 21:23:49 +00:00
|
|
|
pkts, MAX_PKT_BURST);
|
2017-04-01 07:23:00 +00:00
|
|
|
} else {
|
|
|
|
count = rte_vhost_dequeue_burst(vdev->vid, VIRTIO_TXQ,
|
|
|
|
mbuf_pool, pkts, MAX_PKT_BURST);
|
|
|
|
}
|
2016-05-02 21:23:49 +00:00
|
|
|
|
|
|
|
/* setup VMDq for the first packet */
|
|
|
|
if (unlikely(vdev->ready == DEVICE_MAC_LEARNING) && count) {
|
|
|
|
if (vdev->remove || link_vmdq(vdev, pkts[0]) == -1)
|
|
|
|
free_pkts(pkts, count);
|
2014-10-20 04:38:13 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 08:35:43 +00:00
|
|
|
for (i = 0; i < count; ++i)
|
2016-05-23 08:36:33 +00:00
|
|
|
virtio_tx_route(vdev, pkts[i], vlan_tags[vdev->vid]);
|
2014-10-20 04:38:13 +00:00
|
|
|
}
|
2016-05-02 21:23:49 +00:00
|
|
|
|
2014-10-20 04:38:13 +00:00
|
|
|
/*
|
2016-05-02 21:23:49 +00:00
|
|
|
* Main function of vhost-switch. It basically does:
|
|
|
|
*
|
|
|
|
* for each vhost device {
|
|
|
|
* - drain_eth_rx()
|
|
|
|
*
|
|
|
|
* Which drains the host eth Rx queue linked to the vhost device,
|
|
|
|
* and deliver all of them to guest virito Rx ring associated with
|
|
|
|
* this vhost device.
|
|
|
|
*
|
|
|
|
* - drain_virtio_tx()
|
|
|
|
*
|
|
|
|
* Which drains the guest virtio Tx queue and deliver all of them
|
|
|
|
* to the target, which could be another vhost device, or the
|
|
|
|
* physical eth dev. The route is done in function "virtio_tx_route".
|
|
|
|
* }
|
2014-10-20 04:38:13 +00:00
|
|
|
*/
|
|
|
|
static int
|
2016-05-02 21:23:49 +00:00
|
|
|
switch_worker(void *arg __rte_unused)
|
2014-10-20 04:38:13 +00:00
|
|
|
{
|
2016-05-02 21:23:49 +00:00
|
|
|
unsigned i;
|
|
|
|
unsigned lcore_id = rte_lcore_id();
|
|
|
|
struct vhost_dev *vdev;
|
2014-10-20 04:38:13 +00:00
|
|
|
struct mbuf_table *tx_q;
|
|
|
|
|
|
|
|
RTE_LOG(INFO, VHOST_DATA, "Procesing on Core %u started\n", lcore_id);
|
|
|
|
|
|
|
|
tx_q = &lcore_tx_queue[lcore_id];
|
2016-05-02 21:23:49 +00:00
|
|
|
for (i = 0; i < rte_lcore_count(); i++) {
|
2014-10-20 04:38:13 +00:00
|
|
|
if (lcore_ids[i] == lcore_id) {
|
|
|
|
tx_q->txq_id = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while(1) {
|
2016-05-02 21:23:49 +00:00
|
|
|
drain_mbuf_table(tx_q);
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
/*
|
2016-05-02 21:23:45 +00:00
|
|
|
* Inform the configuration core that we have exited the
|
|
|
|
* linked list and that no devices are in use if requested.
|
2014-10-20 04:38:13 +00:00
|
|
|
*/
|
2016-05-02 21:23:45 +00:00
|
|
|
if (lcore_info[lcore_id].dev_removal_flag == REQUEST_DEV_REMOVAL)
|
|
|
|
lcore_info[lcore_id].dev_removal_flag = ACK_DEV_REMOVAL;
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
/*
|
2016-05-02 21:23:49 +00:00
|
|
|
* Process vhost devices
|
2014-10-20 04:38:13 +00:00
|
|
|
*/
|
2016-06-07 03:32:56 +00:00
|
|
|
TAILQ_FOREACH(vdev, &lcore_info[lcore_id].vdev_list,
|
|
|
|
lcore_vdev_entry) {
|
2014-10-20 04:38:24 +00:00
|
|
|
if (unlikely(vdev->remove)) {
|
2014-10-20 04:38:16 +00:00
|
|
|
unlink_vmdq(vdev);
|
|
|
|
vdev->ready = DEVICE_SAFE_REMOVE;
|
2014-10-20 04:38:13 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-05-02 21:23:45 +00:00
|
|
|
|
2016-05-02 21:23:49 +00:00
|
|
|
if (likely(vdev->ready == DEVICE_RX))
|
|
|
|
drain_eth_rx(vdev);
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2016-05-02 21:23:49 +00:00
|
|
|
if (likely(!vdev->remove))
|
|
|
|
drain_virtio_tx(vdev);
|
2014-10-20 04:38:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-05-02 21:23:45 +00:00
|
|
|
* Remove a device from the specific data core linked list and from the
|
|
|
|
* main linked list. Synchonization occurs through the use of the
|
|
|
|
* lcore dev_removal_flag. Device is made volatile here to avoid re-ordering
|
2014-10-20 04:38:13 +00:00
|
|
|
* of dev->remove=1 which can cause an infinite loop in the rte_pause loop.
|
|
|
|
*/
|
|
|
|
static void
|
2016-06-13 09:55:49 +00:00
|
|
|
destroy_device(int vid)
|
2014-10-20 04:38:13 +00:00
|
|
|
{
|
2016-05-10 23:02:58 +00:00
|
|
|
struct vhost_dev *vdev = NULL;
|
2014-10-20 04:38:13 +00:00
|
|
|
int lcore;
|
|
|
|
|
2016-05-10 23:02:58 +00:00
|
|
|
TAILQ_FOREACH(vdev, &vhost_dev_list, global_vdev_entry) {
|
2016-06-13 09:55:49 +00:00
|
|
|
if (vdev->vid == vid)
|
2016-05-10 23:02:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!vdev)
|
|
|
|
return;
|
2014-10-20 04:38:13 +00:00
|
|
|
/*set the remove flag. */
|
2014-10-20 04:38:16 +00:00
|
|
|
vdev->remove = 1;
|
|
|
|
while(vdev->ready != DEVICE_SAFE_REMOVE) {
|
2014-10-20 04:38:13 +00:00
|
|
|
rte_pause();
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:23:00 +00:00
|
|
|
if (builtin_net_driver)
|
|
|
|
vs_vhost_net_remove(vdev);
|
|
|
|
|
2016-06-07 03:32:56 +00:00
|
|
|
TAILQ_REMOVE(&lcore_info[vdev->coreid].vdev_list, vdev,
|
|
|
|
lcore_vdev_entry);
|
|
|
|
TAILQ_REMOVE(&vhost_dev_list, vdev, global_vdev_entry);
|
|
|
|
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
/* Set the dev_removal_flag on each lcore. */
|
2016-05-02 21:23:45 +00:00
|
|
|
RTE_LCORE_FOREACH_SLAVE(lcore)
|
|
|
|
lcore_info[lcore].dev_removal_flag = REQUEST_DEV_REMOVAL;
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
/*
|
2016-05-02 21:23:45 +00:00
|
|
|
* Once each core has set the dev_removal_flag to ACK_DEV_REMOVAL
|
|
|
|
* we can be sure that they can no longer access the device removed
|
|
|
|
* from the linked lists and that the devices are no longer in use.
|
2014-10-20 04:38:13 +00:00
|
|
|
*/
|
|
|
|
RTE_LCORE_FOREACH_SLAVE(lcore) {
|
2016-05-02 21:23:45 +00:00
|
|
|
while (lcore_info[lcore].dev_removal_flag != ACK_DEV_REMOVAL)
|
2014-10-20 04:38:13 +00:00
|
|
|
rte_pause();
|
|
|
|
}
|
|
|
|
|
2016-05-02 21:23:45 +00:00
|
|
|
lcore_info[vdev->coreid].device_num--;
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2016-05-02 21:23:45 +00:00
|
|
|
RTE_LOG(INFO, VHOST_DATA,
|
2016-04-29 20:45:51 +00:00
|
|
|
"(%d) device has been removed from data core\n",
|
2016-05-23 08:36:33 +00:00
|
|
|
vdev->vid);
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2014-10-20 04:38:16 +00:00
|
|
|
rte_free(vdev);
|
2014-10-20 04:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A new device is added to a data core. First the device is added to the main linked list
|
2019-01-14 14:47:47 +00:00
|
|
|
* and then allocated to a specific data core.
|
2014-10-20 04:38:13 +00:00
|
|
|
*/
|
|
|
|
static int
|
2016-06-13 09:55:49 +00:00
|
|
|
new_device(int vid)
|
2014-10-20 04:38:13 +00:00
|
|
|
{
|
|
|
|
int lcore, core_add = 0;
|
|
|
|
uint32_t device_num_min = num_devices;
|
2014-10-20 04:38:16 +00:00
|
|
|
struct vhost_dev *vdev;
|
|
|
|
|
2014-11-19 12:26:06 +00:00
|
|
|
vdev = rte_zmalloc("vhost device", sizeof(*vdev), RTE_CACHE_LINE_SIZE);
|
2014-10-20 04:38:16 +00:00
|
|
|
if (vdev == NULL) {
|
2016-04-29 20:45:51 +00:00
|
|
|
RTE_LOG(INFO, VHOST_DATA,
|
2016-05-23 08:35:43 +00:00
|
|
|
"(%d) couldn't allocate memory for vhost dev\n",
|
2016-05-23 08:36:33 +00:00
|
|
|
vid);
|
2014-10-20 04:38:16 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2016-05-23 08:36:33 +00:00
|
|
|
vdev->vid = vid;
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2017-04-01 07:23:00 +00:00
|
|
|
if (builtin_net_driver)
|
|
|
|
vs_vhost_net_setup(vdev);
|
|
|
|
|
2016-06-07 03:32:56 +00:00
|
|
|
TAILQ_INSERT_TAIL(&vhost_dev_list, vdev, global_vdev_entry);
|
2016-05-23 08:36:33 +00:00
|
|
|
vdev->vmdq_rx_q = vid * queues_per_pool + vmdq_queue_base;
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
/*reset ready flag*/
|
2014-10-20 04:38:16 +00:00
|
|
|
vdev->ready = DEVICE_MAC_LEARNING;
|
|
|
|
vdev->remove = 0;
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
/* Find a suitable lcore to add the device. */
|
|
|
|
RTE_LCORE_FOREACH_SLAVE(lcore) {
|
2016-05-02 21:23:45 +00:00
|
|
|
if (lcore_info[lcore].device_num < device_num_min) {
|
|
|
|
device_num_min = lcore_info[lcore].device_num;
|
2014-10-20 04:38:13 +00:00
|
|
|
core_add = lcore;
|
|
|
|
}
|
|
|
|
}
|
2014-10-20 04:38:16 +00:00
|
|
|
vdev->coreid = core_add;
|
|
|
|
|
2016-06-07 03:32:56 +00:00
|
|
|
TAILQ_INSERT_TAIL(&lcore_info[vdev->coreid].vdev_list, vdev,
|
|
|
|
lcore_vdev_entry);
|
2016-05-02 21:23:45 +00:00
|
|
|
lcore_info[vdev->coreid].device_num++;
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
/* Disable notifications. */
|
2016-06-13 09:55:49 +00:00
|
|
|
rte_vhost_enable_guest_notification(vid, VIRTIO_RXQ, 0);
|
|
|
|
rte_vhost_enable_guest_notification(vid, VIRTIO_TXQ, 0);
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2016-04-29 20:45:51 +00:00
|
|
|
RTE_LOG(INFO, VHOST_DATA,
|
|
|
|
"(%d) device has been added to data core %d\n",
|
2016-05-23 08:36:33 +00:00
|
|
|
vid, vdev->coreid);
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These callback allow devices to be added to the data core when configuration
|
|
|
|
* has been fully complete.
|
|
|
|
*/
|
2017-04-01 07:22:52 +00:00
|
|
|
static const struct vhost_device_ops virtio_net_device_ops =
|
2014-10-20 04:38:13 +00:00
|
|
|
{
|
|
|
|
.new_device = new_device,
|
|
|
|
.destroy_device = destroy_device,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a thread will wake up after a period to print stats if the user has
|
|
|
|
* enabled them.
|
|
|
|
*/
|
2018-04-24 14:46:51 +00:00
|
|
|
static void *
|
|
|
|
print_stats(__rte_unused void *arg)
|
2014-10-20 04:38:13 +00:00
|
|
|
{
|
2016-05-02 21:23:45 +00:00
|
|
|
struct vhost_dev *vdev;
|
2014-10-20 04:38:13 +00:00
|
|
|
uint64_t tx_dropped, rx_dropped;
|
|
|
|
uint64_t tx, tx_total, rx, rx_total;
|
|
|
|
const char clr[] = { 27, '[', '2', 'J', '\0' };
|
|
|
|
const char top_left[] = { 27, '[', '1', ';', '1', 'H','\0' };
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
sleep(enable_stats);
|
|
|
|
|
|
|
|
/* Clear screen and move to top left */
|
2016-05-02 21:23:50 +00:00
|
|
|
printf("%s%s\n", clr, top_left);
|
|
|
|
printf("Device statistics =================================\n");
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2016-06-07 03:32:56 +00:00
|
|
|
TAILQ_FOREACH(vdev, &vhost_dev_list, global_vdev_entry) {
|
2016-05-02 21:23:50 +00:00
|
|
|
tx_total = vdev->stats.tx_total;
|
|
|
|
tx = vdev->stats.tx;
|
2014-10-20 04:38:13 +00:00
|
|
|
tx_dropped = tx_total - tx;
|
2016-05-02 21:23:50 +00:00
|
|
|
|
|
|
|
rx_total = rte_atomic64_read(&vdev->stats.rx_total_atomic);
|
|
|
|
rx = rte_atomic64_read(&vdev->stats.rx_atomic);
|
2014-10-20 04:38:13 +00:00
|
|
|
rx_dropped = rx_total - rx;
|
|
|
|
|
2016-04-29 20:45:51 +00:00
|
|
|
printf("Statistics for device %d\n"
|
2016-05-02 21:23:50 +00:00
|
|
|
"-----------------------\n"
|
|
|
|
"TX total: %" PRIu64 "\n"
|
|
|
|
"TX dropped: %" PRIu64 "\n"
|
|
|
|
"TX successful: %" PRIu64 "\n"
|
|
|
|
"RX total: %" PRIu64 "\n"
|
|
|
|
"RX dropped: %" PRIu64 "\n"
|
|
|
|
"RX successful: %" PRIu64 "\n",
|
2016-06-13 09:55:49 +00:00
|
|
|
vdev->vid,
|
2016-05-02 21:23:50 +00:00
|
|
|
tx_total, tx_dropped, tx,
|
|
|
|
rx_total, rx_dropped, rx);
|
2014-10-20 04:38:13 +00:00
|
|
|
}
|
2016-05-02 21:23:50 +00:00
|
|
|
|
|
|
|
printf("===================================================\n");
|
2014-10-20 04:38:13 +00:00
|
|
|
}
|
2018-04-24 14:46:51 +00:00
|
|
|
|
|
|
|
return NULL;
|
2014-10-20 04:38:13 +00:00
|
|
|
}
|
|
|
|
|
2016-08-20 10:11:36 +00:00
|
|
|
static void
|
|
|
|
unregister_drivers(int socket_num)
|
|
|
|
{
|
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
for (i = 0; i < socket_num; i++) {
|
|
|
|
ret = rte_vhost_driver_unregister(socket_files + i * PATH_MAX);
|
|
|
|
if (ret != 0)
|
|
|
|
RTE_LOG(ERR, VHOST_CONFIG,
|
|
|
|
"Fail to unregister vhost driver for %s.\n",
|
|
|
|
socket_files + i * PATH_MAX);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-06 02:26:53 +00:00
|
|
|
/* When we receive a INT signal, unregister vhost driver */
|
|
|
|
static void
|
|
|
|
sigint_handler(__rte_unused int signum)
|
|
|
|
{
|
|
|
|
/* Unregister vhost driver. */
|
2016-08-20 10:11:36 +00:00
|
|
|
unregister_drivers(nb_sockets);
|
|
|
|
|
2015-07-06 02:26:53 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
2014-10-20 04:38:13 +00:00
|
|
|
|
examples/vhost: fix mbuf allocation failure
It has always been a mystery (at least to me before) that how many
mbuf is enough while creating an mbuf pool. While current macro
NUM_MBUFS_PER_PORT gives your some insights, it's not that accurate:
it doesn't consider the case we may receive a big packet, say 64K
when TSO is enabled.
We actually have tried to fix it once before, with commit 5499c1fc9baa
("examples/vhost: fix mbuf allocation"), but it just workarounded it
by enlarging it a bit so that the case described in the commit log
by passes. So, while trying to fix it ultimately, I'm thinking how
big is big enough, and what are the factors need consider to figure
out a proper value.
Therefore, here you are. I introduced a helper function to create
the mbuf pool, and do the "how many mbufs are needed" calculation
there. Also, I put detailed comments how that comes, to serve as
the guidelines.
Fixes: 9fd72e3cbd29 ("examples/vhost: add virtio offload")
Fixes: 5499c1fc9baa ("examples/vhost: fix mbuf allocation")
Signed-off-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
2016-05-02 21:23:48 +00:00
|
|
|
/*
|
|
|
|
* While creating an mbuf pool, one key thing is to figure out how
|
|
|
|
* many mbuf entries is enough for our use. FYI, here are some
|
|
|
|
* guidelines:
|
|
|
|
*
|
|
|
|
* - Each rx queue would reserve @nr_rx_desc mbufs at queue setup stage
|
|
|
|
*
|
|
|
|
* - For each switch core (A CPU core does the packet switch), we need
|
|
|
|
* also make some reservation for receiving the packets from virtio
|
|
|
|
* Tx queue. How many is enough depends on the usage. It's normally
|
|
|
|
* a simple calculation like following:
|
|
|
|
*
|
|
|
|
* MAX_PKT_BURST * max packet size / mbuf size
|
|
|
|
*
|
|
|
|
* So, we definitely need allocate more mbufs when TSO is enabled.
|
|
|
|
*
|
|
|
|
* - Similarly, for each switching core, we should serve @nr_rx_desc
|
|
|
|
* mbufs for receiving the packets from physical NIC device.
|
|
|
|
*
|
|
|
|
* - We also need make sure, for each switch core, we have allocated
|
|
|
|
* enough mbufs to fill up the mbuf cache.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
create_mbuf_pool(uint16_t nr_port, uint32_t nr_switch_core, uint32_t mbuf_size,
|
|
|
|
uint32_t nr_queues, uint32_t nr_rx_desc, uint32_t nr_mbuf_cache)
|
|
|
|
{
|
|
|
|
uint32_t nr_mbufs;
|
|
|
|
uint32_t nr_mbufs_per_core;
|
|
|
|
uint32_t mtu = 1500;
|
|
|
|
|
|
|
|
if (mergeable)
|
|
|
|
mtu = 9000;
|
|
|
|
if (enable_tso)
|
|
|
|
mtu = 64 * 1024;
|
|
|
|
|
|
|
|
nr_mbufs_per_core = (mtu + mbuf_size) * MAX_PKT_BURST /
|
2017-01-12 03:52:17 +00:00
|
|
|
(mbuf_size - RTE_PKTMBUF_HEADROOM);
|
examples/vhost: fix mbuf allocation failure
It has always been a mystery (at least to me before) that how many
mbuf is enough while creating an mbuf pool. While current macro
NUM_MBUFS_PER_PORT gives your some insights, it's not that accurate:
it doesn't consider the case we may receive a big packet, say 64K
when TSO is enabled.
We actually have tried to fix it once before, with commit 5499c1fc9baa
("examples/vhost: fix mbuf allocation"), but it just workarounded it
by enlarging it a bit so that the case described in the commit log
by passes. So, while trying to fix it ultimately, I'm thinking how
big is big enough, and what are the factors need consider to figure
out a proper value.
Therefore, here you are. I introduced a helper function to create
the mbuf pool, and do the "how many mbufs are needed" calculation
there. Also, I put detailed comments how that comes, to serve as
the guidelines.
Fixes: 9fd72e3cbd29 ("examples/vhost: add virtio offload")
Fixes: 5499c1fc9baa ("examples/vhost: fix mbuf allocation")
Signed-off-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
2016-05-02 21:23:48 +00:00
|
|
|
nr_mbufs_per_core += nr_rx_desc;
|
|
|
|
nr_mbufs_per_core = RTE_MAX(nr_mbufs_per_core, nr_mbuf_cache);
|
|
|
|
|
|
|
|
nr_mbufs = nr_queues * nr_rx_desc;
|
|
|
|
nr_mbufs += nr_mbufs_per_core * nr_switch_core;
|
|
|
|
nr_mbufs *= nr_port;
|
|
|
|
|
|
|
|
mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", nr_mbufs,
|
|
|
|
nr_mbuf_cache, 0, mbuf_size,
|
|
|
|
rte_socket_id());
|
|
|
|
if (mbuf_pool == NULL)
|
|
|
|
rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
|
|
|
|
}
|
|
|
|
|
2014-10-20 04:38:13 +00:00
|
|
|
/*
|
2016-11-02 03:15:01 +00:00
|
|
|
* Main function, does initialisation and calls the per-lcore functions.
|
2014-10-20 04:38:13 +00:00
|
|
|
*/
|
|
|
|
int
|
2014-09-26 14:04:02 +00:00
|
|
|
main(int argc, char *argv[])
|
2014-10-20 04:38:13 +00:00
|
|
|
{
|
|
|
|
unsigned lcore_id, core_id = 0;
|
|
|
|
unsigned nb_ports, valid_num_ports;
|
2016-08-20 10:11:36 +00:00
|
|
|
int ret, i;
|
2017-09-29 07:17:24 +00:00
|
|
|
uint16_t portid;
|
2014-10-20 04:38:13 +00:00
|
|
|
static pthread_t tid;
|
2016-05-07 05:23:40 +00:00
|
|
|
uint64_t flags = 0;
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2015-07-06 02:26:53 +00:00
|
|
|
signal(SIGINT, sigint_handler);
|
|
|
|
|
2014-10-20 04:38:13 +00:00
|
|
|
/* init EAL */
|
|
|
|
ret = rte_eal_init(argc, argv);
|
|
|
|
if (ret < 0)
|
|
|
|
rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
|
|
|
|
argc -= ret;
|
|
|
|
argv += ret;
|
|
|
|
|
|
|
|
/* parse app arguments */
|
|
|
|
ret = us_vhost_parse_args(argc, argv);
|
|
|
|
if (ret < 0)
|
|
|
|
rte_exit(EXIT_FAILURE, "Invalid argument\n");
|
|
|
|
|
2017-01-11 08:59:46 +00:00
|
|
|
for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
|
2016-05-02 21:23:45 +00:00
|
|
|
TAILQ_INIT(&lcore_info[lcore_id].vdev_list);
|
|
|
|
|
2014-10-20 04:38:13 +00:00
|
|
|
if (rte_lcore_is_enabled(lcore_id))
|
2017-01-11 08:59:46 +00:00
|
|
|
lcore_ids[core_id++] = lcore_id;
|
|
|
|
}
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
if (rte_lcore_count() > RTE_MAX_LCORE)
|
|
|
|
rte_exit(EXIT_FAILURE,"Not enough cores\n");
|
|
|
|
|
|
|
|
/* Get the number of physical ports. */
|
2018-04-05 15:33:22 +00:00
|
|
|
nb_ports = rte_eth_dev_count_avail();
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the global var NUM_PORTS and global array PORTS
|
|
|
|
* and get value of var VALID_NUM_PORTS according to system ports number
|
|
|
|
*/
|
|
|
|
valid_num_ports = check_ports_num(nb_ports);
|
|
|
|
|
|
|
|
if ((valid_num_ports == 0) || (valid_num_ports > MAX_SUP_PORTS)) {
|
|
|
|
RTE_LOG(INFO, VHOST_PORT, "Current enabled port number is %u,"
|
|
|
|
"but only %u port can be enabled\n",num_ports, MAX_SUP_PORTS);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
examples/vhost: fix mbuf allocation failure
It has always been a mystery (at least to me before) that how many
mbuf is enough while creating an mbuf pool. While current macro
NUM_MBUFS_PER_PORT gives your some insights, it's not that accurate:
it doesn't consider the case we may receive a big packet, say 64K
when TSO is enabled.
We actually have tried to fix it once before, with commit 5499c1fc9baa
("examples/vhost: fix mbuf allocation"), but it just workarounded it
by enlarging it a bit so that the case described in the commit log
by passes. So, while trying to fix it ultimately, I'm thinking how
big is big enough, and what are the factors need consider to figure
out a proper value.
Therefore, here you are. I introduced a helper function to create
the mbuf pool, and do the "how many mbufs are needed" calculation
there. Also, I put detailed comments how that comes, to serve as
the guidelines.
Fixes: 9fd72e3cbd29 ("examples/vhost: add virtio offload")
Fixes: 5499c1fc9baa ("examples/vhost: fix mbuf allocation")
Signed-off-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
2016-05-02 21:23:48 +00:00
|
|
|
/*
|
|
|
|
* FIXME: here we are trying to allocate mbufs big enough for
|
|
|
|
* @MAX_QUEUES, but the truth is we're never going to use that
|
|
|
|
* many queues here. We probably should only do allocation for
|
|
|
|
* those queues we are going to use.
|
|
|
|
*/
|
|
|
|
create_mbuf_pool(valid_num_ports, rte_lcore_count() - 1, MBUF_DATA_SIZE,
|
|
|
|
MAX_QUEUES, RTE_TEST_RX_DESC_DEFAULT, MBUF_CACHE_SIZE);
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2016-05-02 21:23:43 +00:00
|
|
|
if (vm2vm_mode == VM2VM_HARDWARE) {
|
|
|
|
/* Enable VT loop back to let L2 switch to do it. */
|
|
|
|
vmdq_conf_default.rx_adv_conf.vmdq_rx_conf.enable_loop_back = 1;
|
|
|
|
RTE_LOG(DEBUG, VHOST_CONFIG,
|
|
|
|
"Enable loop back for L2 switch in vmdq.\n");
|
2014-10-20 04:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize all ports */
|
2018-04-05 15:33:20 +00:00
|
|
|
RTE_ETH_FOREACH_DEV(portid) {
|
2014-10-20 04:38:13 +00:00
|
|
|
/* skip ports that are not enabled */
|
|
|
|
if ((enabled_port_mask & (1 << portid)) == 0) {
|
|
|
|
RTE_LOG(INFO, VHOST_PORT,
|
|
|
|
"Skipping disabled port %d\n", portid);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (port_init(portid) != 0)
|
|
|
|
rte_exit(EXIT_FAILURE,
|
|
|
|
"Cannot initialize network ports\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable stats if the user option is set. */
|
2015-07-29 00:51:44 +00:00
|
|
|
if (enable_stats) {
|
2018-04-24 14:46:51 +00:00
|
|
|
ret = rte_ctrl_thread_create(&tid, "print-stats", NULL,
|
|
|
|
print_stats, NULL);
|
|
|
|
if (ret < 0)
|
2015-07-29 00:51:44 +00:00
|
|
|
rte_exit(EXIT_FAILURE,
|
|
|
|
"Cannot create print-stats thread\n");
|
|
|
|
}
|
2014-10-20 04:38:13 +00:00
|
|
|
|
|
|
|
/* Launch all data cores. */
|
2016-05-02 21:23:43 +00:00
|
|
|
RTE_LCORE_FOREACH_SLAVE(lcore_id)
|
|
|
|
rte_eal_remote_launch(switch_worker, NULL, lcore_id);
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2016-05-07 05:23:40 +00:00
|
|
|
if (client_mode)
|
|
|
|
flags |= RTE_VHOST_USER_CLIENT;
|
|
|
|
|
2016-10-09 07:27:59 +00:00
|
|
|
if (dequeue_zero_copy)
|
|
|
|
flags |= RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
|
|
|
|
|
2016-08-20 10:10:33 +00:00
|
|
|
/* Register vhost user driver to handle vhost messages. */
|
2016-08-20 10:11:36 +00:00
|
|
|
for (i = 0; i < nb_sockets; i++) {
|
2017-04-01 07:22:41 +00:00
|
|
|
char *file = socket_files + i * PATH_MAX;
|
|
|
|
ret = rte_vhost_driver_register(file, flags);
|
2016-08-20 10:11:36 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
unregister_drivers(i);
|
|
|
|
rte_exit(EXIT_FAILURE,
|
|
|
|
"vhost driver register failure.\n");
|
|
|
|
}
|
2017-04-01 07:23:00 +00:00
|
|
|
|
|
|
|
if (builtin_net_driver)
|
|
|
|
rte_vhost_driver_set_features(file, VIRTIO_NET_FEATURES);
|
|
|
|
|
2017-04-01 07:22:41 +00:00
|
|
|
if (mergeable == 0) {
|
|
|
|
rte_vhost_driver_disable_features(file,
|
|
|
|
1ULL << VIRTIO_NET_F_MRG_RXBUF);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enable_tx_csum == 0) {
|
|
|
|
rte_vhost_driver_disable_features(file,
|
|
|
|
1ULL << VIRTIO_NET_F_CSUM);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enable_tso == 0) {
|
|
|
|
rte_vhost_driver_disable_features(file,
|
|
|
|
1ULL << VIRTIO_NET_F_HOST_TSO4);
|
|
|
|
rte_vhost_driver_disable_features(file,
|
|
|
|
1ULL << VIRTIO_NET_F_HOST_TSO6);
|
|
|
|
rte_vhost_driver_disable_features(file,
|
|
|
|
1ULL << VIRTIO_NET_F_GUEST_TSO4);
|
|
|
|
rte_vhost_driver_disable_features(file,
|
|
|
|
1ULL << VIRTIO_NET_F_GUEST_TSO6);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (promiscuous) {
|
|
|
|
rte_vhost_driver_enable_features(file,
|
|
|
|
1ULL << VIRTIO_NET_F_CTRL_RX);
|
|
|
|
}
|
2014-10-20 04:38:13 +00:00
|
|
|
|
2017-04-01 07:22:42 +00:00
|
|
|
ret = rte_vhost_driver_callback_register(file,
|
|
|
|
&virtio_net_device_ops);
|
|
|
|
if (ret != 0) {
|
|
|
|
rte_exit(EXIT_FAILURE,
|
|
|
|
"failed to register vhost driver callbacks.\n");
|
|
|
|
}
|
vhost: introduce API to start a specific driver
We used to use rte_vhost_driver_session_start() to trigger the vhost-user
session. It takes no argument, thus it's a global trigger. And it could
be problematic.
The issue is, currently, rte_vhost_driver_register(path, flags) actually
tries to put it into the session loop (by fdset_add). However, it needs
a set of APIs to set a vhost-user driver properly:
* rte_vhost_driver_register(path, flags);
* rte_vhost_driver_set_features(path, features);
* rte_vhost_driver_callback_register(path, vhost_device_ops);
If a new vhost-user driver is registered after the trigger (think OVS-DPDK
that could add a port dynamically from cmdline), the current code will
effectively starts the session for the new driver just after the first
API rte_vhost_driver_register() is invoked, leaving later calls taking
no effect at all.
To handle the case properly, this patch introduce a new API,
rte_vhost_driver_start(path), to trigger a specific vhost-user driver.
To do that, the rte_vhost_driver_register(path, flags) is simplified
to create the socket only and let rte_vhost_driver_start(path) to
actually put it into the session loop.
Meanwhile, the rte_vhost_driver_session_start is removed: we could hide
the session thread internally (create the thread if it has not been
created). This would also simplify the application.
NOTE: the API order in prog guide is slightly adjusted for showing the
correct invoke order.
Signed-off-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
2017-04-01 07:22:56 +00:00
|
|
|
|
|
|
|
if (rte_vhost_driver_start(file) < 0) {
|
|
|
|
rte_exit(EXIT_FAILURE,
|
|
|
|
"failed to start vhost driver.\n");
|
|
|
|
}
|
2017-04-01 07:22:42 +00:00
|
|
|
}
|
2014-10-20 04:38:13 +00:00
|
|
|
|
vhost: introduce API to start a specific driver
We used to use rte_vhost_driver_session_start() to trigger the vhost-user
session. It takes no argument, thus it's a global trigger. And it could
be problematic.
The issue is, currently, rte_vhost_driver_register(path, flags) actually
tries to put it into the session loop (by fdset_add). However, it needs
a set of APIs to set a vhost-user driver properly:
* rte_vhost_driver_register(path, flags);
* rte_vhost_driver_set_features(path, features);
* rte_vhost_driver_callback_register(path, vhost_device_ops);
If a new vhost-user driver is registered after the trigger (think OVS-DPDK
that could add a port dynamically from cmdline), the current code will
effectively starts the session for the new driver just after the first
API rte_vhost_driver_register() is invoked, leaving later calls taking
no effect at all.
To handle the case properly, this patch introduce a new API,
rte_vhost_driver_start(path), to trigger a specific vhost-user driver.
To do that, the rte_vhost_driver_register(path, flags) is simplified
to create the socket only and let rte_vhost_driver_start(path) to
actually put it into the session loop.
Meanwhile, the rte_vhost_driver_session_start is removed: we could hide
the session thread internally (create the thread if it has not been
created). This would also simplify the application.
NOTE: the API order in prog guide is slightly adjusted for showing the
correct invoke order.
Signed-off-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
2017-04-01 07:22:56 +00:00
|
|
|
RTE_LCORE_FOREACH_SLAVE(lcore_id)
|
|
|
|
rte_eal_wait_lcore(lcore_id);
|
|
|
|
|
2014-10-20 04:38:13 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|