1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation
7 #include <linux/if_ether.h>
8 #include <linux/if_vlan.h>
9 #include <linux/virtio_net.h>
10 #include <linux/virtio_ring.h>
13 #include <sys/eventfd.h>
14 #include <sys/param.h>
17 #include <rte_atomic.h>
18 #include <rte_cycles.h>
19 #include <rte_ethdev.h>
21 #include <rte_string_fns.h>
22 #include <rte_malloc.h>
23 #include <rte_vhost.h>
24 #include <rte_pause.h>
28 #include "vxlan_setup.h"
30 /* the maximum number of external ports supported */
31 #define MAX_SUP_PORTS 1
34 * Calculate the number of buffers needed per port
36 #define NUM_MBUFS_PER_PORT ((MAX_QUEUES * RTE_TEST_RX_DESC_DEFAULT) +\
37 (nb_switching_cores * MAX_PKT_BURST) +\
38 (nb_switching_cores * \
39 RTE_TEST_TX_DESC_DEFAULT) +\
40 (nb_switching_cores * MBUF_CACHE_SIZE))
42 #define MBUF_CACHE_SIZE 128
43 #define MBUF_DATA_SIZE RTE_MBUF_DEFAULT_BUF_SIZE
45 #define MAX_PKT_BURST 32 /* Max burst size for RX/TX */
46 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
48 /* Defines how long we wait between retries on RX */
49 #define BURST_RX_WAIT_US 15
51 #define BURST_RX_RETRIES 4 /* Number of retries on RX. */
53 #define JUMBO_FRAME_MAX_SIZE 0x2600
55 /* Config_core_flag status definitions. */
56 #define REQUEST_DEV_REMOVAL 1
57 #define ACK_DEV_REMOVAL 0
59 /* Configurable number of RX/TX ring descriptors */
60 #define RTE_TEST_RX_DESC_DEFAULT 1024
61 #define RTE_TEST_TX_DESC_DEFAULT 512
63 /* Get first 4 bytes in mbuf headroom. */
64 #define MBUF_HEADROOM_UINT32(mbuf) (*(uint32_t *)((uint8_t *)(mbuf) \
65 + sizeof(struct rte_mbuf)))
67 #define INVALID_PORT_ID 0xFFFF
69 /* Maximum character device basename size. */
70 #define MAX_BASENAME_SZ 20
72 /* Maximum long option length for option parsing. */
73 #define MAX_LONG_OPT_SZ 64
75 /* Used to compare MAC addresses. */
76 #define MAC_ADDR_CMP 0xFFFFFFFFFFFFULL
78 #define CMD_LINE_OPT_NB_DEVICES "nb-devices"
79 #define CMD_LINE_OPT_UDP_PORT "udp-port"
80 #define CMD_LINE_OPT_TX_CHECKSUM "tx-checksum"
81 #define CMD_LINE_OPT_TSO_SEGSZ "tso-segsz"
82 #define CMD_LINE_OPT_FILTER_TYPE "filter-type"
83 #define CMD_LINE_OPT_ENCAP "encap"
84 #define CMD_LINE_OPT_DECAP "decap"
85 #define CMD_LINE_OPT_RX_RETRY "rx-retry"
86 #define CMD_LINE_OPT_RX_RETRY_DELAY "rx-retry-delay"
87 #define CMD_LINE_OPT_RX_RETRY_NUM "rx-retry-num"
88 #define CMD_LINE_OPT_STATS "stats"
89 #define CMD_LINE_OPT_DEV_BASENAME "dev-basename"
91 /* mask of enabled ports */
92 static uint32_t enabled_port_mask;
94 /*Number of switching cores enabled*/
95 static uint32_t nb_switching_cores;
97 /* number of devices/queues to support*/
98 uint16_t nb_devices = 2;
100 /* max ring descriptor, ixgbe, i40e, e1000 all are 4096. */
101 #define MAX_RING_DESC 4096
104 struct rte_mempool *pool;
105 struct rte_ring *ring;
107 } vpool_array[MAX_QUEUES+MAX_QUEUES];
109 /* UDP tunneling port */
110 uint16_t udp_port = 4789;
112 /* enable/disable inner TX checksum */
113 uint8_t tx_checksum = 0;
115 /* TCP segment size */
116 uint16_t tso_segsz = 0;
118 /* enable/disable decapsulation */
119 uint8_t rx_decap = 1;
121 /* enable/disable encapsulation */
122 uint8_t tx_encap = 1;
124 /* RX filter type for tunneling packet */
125 uint8_t filter_idx = 1;
127 /* overlay packet operation */
128 struct ol_switch_ops overlay_options = {
129 .port_configure = vxlan_port_init,
130 .tunnel_setup = vxlan_link,
131 .tunnel_destroy = vxlan_unlink,
132 .tx_handle = vxlan_tx_pkts,
133 .rx_handle = vxlan_rx_pkts,
134 .param_handle = NULL,
138 uint32_t enable_stats = 0;
139 /* Enable retries on RX. */
140 static uint32_t enable_retry = 1;
141 /* Specify timeout (in useconds) between retries on RX. */
142 static uint32_t burst_rx_delay_time = BURST_RX_WAIT_US;
143 /* Specify the number of retries on RX. */
144 static uint32_t burst_rx_retry_num = BURST_RX_RETRIES;
146 /* Character device basename. Can be set by user. */
147 static char dev_basename[MAX_BASENAME_SZ] = "vhost-net";
149 static unsigned lcore_ids[RTE_MAX_LCORE];
150 uint16_t ports[RTE_MAX_ETHPORTS];
152 static unsigned nb_ports; /**< The number of ports specified in command line */
154 /* ethernet addresses of ports */
155 struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
157 /* heads for the main used and free linked lists for the data path. */
158 static struct virtio_net_data_ll *ll_root_used;
159 static struct virtio_net_data_ll *ll_root_free;
162 * Array of data core structures containing information on
163 * individual core linked lists.
165 static struct lcore_info lcore_info[RTE_MAX_LCORE];
167 /* Used for queueing bursts of TX packets. */
171 struct rte_mbuf *m_table[MAX_PKT_BURST];
174 /* TX queue for each data core. */
175 struct mbuf_table lcore_tx_queue[RTE_MAX_LCORE];
177 struct device_statistics dev_statistics[MAX_DEVICES];
180 * Set character device basename.
183 us_vhost_parse_basename(const char *q_arg)
185 /* parse number string */
186 if (strlen(q_arg) >= MAX_BASENAME_SZ)
189 strlcpy((char *)&dev_basename, q_arg, MAX_BASENAME_SZ);
195 * Parse the portmask provided at run time.
198 parse_portmask(const char *portmask)
203 /* parse hexadecimal string */
204 pm = strtoul(portmask, &end, 16);
205 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
212 * Parse num options at run time.
215 parse_num_opt(const char *q_arg, uint32_t max_valid_value)
220 /* parse unsigned int string */
221 num = strtoul(q_arg, &end, 10);
222 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
225 if (num > max_valid_value)
235 tep_termination_usage(const char *prgname)
237 RTE_LOG(INFO, VHOST_CONFIG, "%s [EAL options] -- -p PORTMASK\n"
238 " --udp-port: UDP destination port for VXLAN packet\n"
239 " --nb-devices[1-64]: The number of virtIO device\n"
240 " --tx-checksum [0|1]: inner Tx checksum offload\n"
241 " --tso-segsz [0-N]: TCP segment size\n"
242 " --decap [0|1]: tunneling packet decapsulation\n"
243 " --encap [0|1]: tunneling packet encapsulation\n"
244 " --filter-type[1-3]: filter type for tunneling packet\n"
245 " 1: Inner MAC and tenent ID\n"
246 " 2: Inner MAC and VLAN, and tenent ID\n"
247 " 3: Outer MAC, Inner MAC and tenent ID\n"
248 " -p PORTMASK: Set mask for ports to be used by application\n"
249 " --rx-retry [0|1]: disable/enable(default) retries on rx."
250 " Enable retry if destintation queue is full\n"
251 " --rx-retry-delay [0-N]: timeout(in usecond) between retries on RX."
252 " This makes effect only if retries on rx enabled\n"
253 " --rx-retry-num [0-N]: the number of retries on rx."
254 " This makes effect only if retries on rx enabled\n"
255 " --stats [0-N]: 0: Disable stats, N: Time in seconds to print stats\n"
256 " --dev-basename: The basename to be used for the character device.\n",
261 * Parse the arguments given in the command line of the application.
264 tep_termination_parse_args(int argc, char **argv)
269 const char *prgname = argv[0];
270 static struct option long_option[] = {
271 {CMD_LINE_OPT_NB_DEVICES, required_argument, NULL, 0},
272 {CMD_LINE_OPT_UDP_PORT, required_argument, NULL, 0},
273 {CMD_LINE_OPT_TX_CHECKSUM, required_argument, NULL, 0},
274 {CMD_LINE_OPT_TSO_SEGSZ, required_argument, NULL, 0},
275 {CMD_LINE_OPT_DECAP, required_argument, NULL, 0},
276 {CMD_LINE_OPT_ENCAP, required_argument, NULL, 0},
277 {CMD_LINE_OPT_FILTER_TYPE, required_argument, NULL, 0},
278 {CMD_LINE_OPT_RX_RETRY, required_argument, NULL, 0},
279 {CMD_LINE_OPT_RX_RETRY_DELAY, required_argument, NULL, 0},
280 {CMD_LINE_OPT_RX_RETRY_NUM, required_argument, NULL, 0},
281 {CMD_LINE_OPT_STATS, required_argument, NULL, 0},
282 {CMD_LINE_OPT_DEV_BASENAME, required_argument, NULL, 0},
286 /* Parse command line */
287 while ((opt = getopt_long(argc, argv, "p:",
288 long_option, &option_index)) != EOF) {
292 enabled_port_mask = parse_portmask(optarg);
293 if (enabled_port_mask == 0) {
294 RTE_LOG(INFO, VHOST_CONFIG,
295 "Invalid portmask\n");
296 tep_termination_usage(prgname);
301 if (!strncmp(long_option[option_index].name,
302 CMD_LINE_OPT_NB_DEVICES,
303 sizeof(CMD_LINE_OPT_NB_DEVICES))) {
304 ret = parse_num_opt(optarg, MAX_DEVICES);
306 RTE_LOG(INFO, VHOST_CONFIG,
307 "Invalid argument for nb-devices [0-%d]\n",
309 tep_termination_usage(prgname);
315 /* Enable/disable retries on RX. */
316 if (!strncmp(long_option[option_index].name,
317 CMD_LINE_OPT_RX_RETRY,
318 sizeof(CMD_LINE_OPT_RX_RETRY))) {
319 ret = parse_num_opt(optarg, 1);
321 RTE_LOG(INFO, VHOST_CONFIG,
322 "Invalid argument for rx-retry [0|1]\n");
323 tep_termination_usage(prgname);
329 if (!strncmp(long_option[option_index].name,
330 CMD_LINE_OPT_TSO_SEGSZ,
331 sizeof(CMD_LINE_OPT_TSO_SEGSZ))) {
332 ret = parse_num_opt(optarg, INT16_MAX);
334 RTE_LOG(INFO, VHOST_CONFIG,
335 "Invalid argument for TCP segment size [0-N]\n");
336 tep_termination_usage(prgname);
342 if (!strncmp(long_option[option_index].name,
343 CMD_LINE_OPT_UDP_PORT,
344 sizeof(CMD_LINE_OPT_UDP_PORT))) {
345 ret = parse_num_opt(optarg, INT16_MAX);
347 RTE_LOG(INFO, VHOST_CONFIG,
348 "Invalid argument for UDP port [0-N]\n");
349 tep_termination_usage(prgname);
355 /* Specify the retries delay time (in useconds) on RX.*/
356 if (!strncmp(long_option[option_index].name,
357 CMD_LINE_OPT_RX_RETRY_DELAY,
358 sizeof(CMD_LINE_OPT_RX_RETRY_DELAY))) {
359 ret = parse_num_opt(optarg, INT32_MAX);
361 RTE_LOG(INFO, VHOST_CONFIG,
362 "Invalid argument for rx-retry-delay [0-N]\n");
363 tep_termination_usage(prgname);
366 burst_rx_delay_time = ret;
369 /* Specify the retries number on RX. */
370 if (!strncmp(long_option[option_index].name,
371 CMD_LINE_OPT_RX_RETRY_NUM,
372 sizeof(CMD_LINE_OPT_RX_RETRY_NUM))) {
373 ret = parse_num_opt(optarg, INT32_MAX);
375 RTE_LOG(INFO, VHOST_CONFIG,
376 "Invalid argument for rx-retry-num [0-N]\n");
377 tep_termination_usage(prgname);
380 burst_rx_retry_num = ret;
383 if (!strncmp(long_option[option_index].name,
384 CMD_LINE_OPT_TX_CHECKSUM,
385 sizeof(CMD_LINE_OPT_TX_CHECKSUM))) {
386 ret = parse_num_opt(optarg, 1);
388 RTE_LOG(INFO, VHOST_CONFIG,
389 "Invalid argument for tx-checksum [0|1]\n");
390 tep_termination_usage(prgname);
396 if (!strncmp(long_option[option_index].name,
397 CMD_LINE_OPT_FILTER_TYPE,
398 sizeof(CMD_LINE_OPT_FILTER_TYPE))) {
399 ret = parse_num_opt(optarg, 3);
400 if ((ret == -1) || (ret == 0)) {
401 RTE_LOG(INFO, VHOST_CONFIG,
402 "Invalid argument for filter type [1-3]\n");
403 tep_termination_usage(prgname);
406 filter_idx = ret - 1;
409 /* Enable/disable encapsulation on RX. */
410 if (!strncmp(long_option[option_index].name,
412 sizeof(CMD_LINE_OPT_DECAP))) {
413 ret = parse_num_opt(optarg, 1);
415 RTE_LOG(INFO, VHOST_CONFIG,
416 "Invalid argument for decap [0|1]\n");
417 tep_termination_usage(prgname);
423 /* Enable/disable encapsulation on TX. */
424 if (!strncmp(long_option[option_index].name,
426 sizeof(CMD_LINE_OPT_ENCAP))) {
427 ret = parse_num_opt(optarg, 1);
429 RTE_LOG(INFO, VHOST_CONFIG,
430 "Invalid argument for encap [0|1]\n");
431 tep_termination_usage(prgname);
437 /* Enable/disable stats. */
438 if (!strncmp(long_option[option_index].name,
440 sizeof(CMD_LINE_OPT_STATS))) {
441 ret = parse_num_opt(optarg, INT32_MAX);
443 RTE_LOG(INFO, VHOST_CONFIG,
444 "Invalid argument for stats [0..N]\n");
445 tep_termination_usage(prgname);
451 /* Set character device basename. */
452 if (!strncmp(long_option[option_index].name,
453 CMD_LINE_OPT_DEV_BASENAME,
454 sizeof(CMD_LINE_OPT_DEV_BASENAME))) {
455 if (us_vhost_parse_basename(optarg) == -1) {
456 RTE_LOG(INFO, VHOST_CONFIG,
457 "Invalid argument for character "
458 "device basename (Max %d characters)\n",
460 tep_termination_usage(prgname);
467 /* Invalid option - print options. */
469 tep_termination_usage(prgname);
474 for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
475 if (enabled_port_mask & (1 << i))
476 ports[nb_ports++] = (uint8_t)i;
479 if ((nb_ports == 0) || (nb_ports > MAX_SUP_PORTS)) {
480 RTE_LOG(INFO, VHOST_PORT, "Current enabled port number is %u,"
481 "but only %u port can be enabled\n", nb_ports,
490 * Update the global var NB_PORTS and array PORTS
491 * according to system ports number and return valid ports number
494 check_ports_num(unsigned max_nb_ports)
496 unsigned valid_nb_ports = nb_ports;
499 if (nb_ports > max_nb_ports) {
500 RTE_LOG(INFO, VHOST_PORT, "\nSpecified port number(%u) "
501 " exceeds total system port number(%u)\n",
502 nb_ports, max_nb_ports);
503 nb_ports = max_nb_ports;
506 for (portid = 0; portid < nb_ports; portid++) {
507 if (!rte_eth_dev_is_valid_port(ports[portid])) {
508 RTE_LOG(INFO, VHOST_PORT,
509 "\nSpecified port ID(%u) is not valid\n",
511 ports[portid] = INVALID_PORT_ID;
515 return valid_nb_ports;
519 * This function routes the TX packet to the correct interface. This may be a local device
520 * or the physical port.
522 static __rte_always_inline void
523 virtio_tx_route(struct vhost_dev *vdev, struct rte_mbuf *m)
525 struct mbuf_table *tx_q;
526 struct rte_mbuf **m_table;
527 unsigned len, ret = 0;
528 const uint16_t lcore_id = rte_lcore_id();
530 RTE_LOG_DP(DEBUG, VHOST_DATA, "(%d) TX: MAC address is external\n",
533 /* Add packet to the port tx queue */
534 tx_q = &lcore_tx_queue[lcore_id];
537 tx_q->m_table[len] = m;
540 dev_statistics[vdev->vid].tx_total++;
541 dev_statistics[vdev->vid].tx++;
544 if (unlikely(len == MAX_PKT_BURST)) {
545 m_table = (struct rte_mbuf **)tx_q->m_table;
546 ret = overlay_options.tx_handle(ports[0],
547 (uint16_t)tx_q->txq_id, m_table,
548 (uint16_t)tx_q->len);
550 /* Free any buffers not handled by TX and update
553 if (unlikely(ret < len)) {
555 rte_pktmbuf_free(m_table[ret]);
556 } while (++ret < len);
567 * This function is called by each data core. It handles all
568 * RX/TX registered with the core. For TX the specific lcore
569 * linked list is used. For RX, MAC addresses are compared
570 * with all devices in the main linked list.
573 switch_worker(__rte_unused void *arg)
575 struct rte_mempool *mbuf_pool = arg;
576 struct vhost_dev *vdev = NULL;
577 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
578 struct virtio_net_data_ll *dev_ll;
579 struct mbuf_table *tx_q;
580 volatile struct lcore_ll_info *lcore_ll;
581 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
582 / US_PER_S * BURST_TX_DRAIN_US;
583 uint64_t prev_tsc, diff_tsc, cur_tsc, ret_count = 0;
585 const uint16_t lcore_id = rte_lcore_id();
586 const uint16_t num_cores = (uint16_t)rte_lcore_count();
587 uint16_t rx_count = 0;
591 RTE_LOG(INFO, VHOST_DATA, "Procesing on Core %u started\n", lcore_id);
592 lcore_ll = lcore_info[lcore_id].lcore_ll;
595 tx_q = &lcore_tx_queue[lcore_id];
596 for (i = 0; i < num_cores; i++) {
597 if (lcore_ids[i] == lcore_id) {
604 cur_tsc = rte_rdtsc();
606 * TX burst queue drain
608 diff_tsc = cur_tsc - prev_tsc;
609 if (unlikely(diff_tsc > drain_tsc)) {
612 RTE_LOG_DP(DEBUG, VHOST_DATA, "TX queue drained after "
613 "timeout with burst size %u\n",
615 ret = overlay_options.tx_handle(ports[0],
616 (uint16_t)tx_q->txq_id,
617 (struct rte_mbuf **)tx_q->m_table,
618 (uint16_t)tx_q->len);
619 if (unlikely(ret < tx_q->len)) {
621 rte_pktmbuf_free(tx_q->m_table[ret]);
622 } while (++ret < tx_q->len);
632 rte_prefetch0(lcore_ll->ll_root_used);
635 * Inform the configuration core that we have exited
636 * the linked list and that no devices are
637 * in use if requested.
639 if (lcore_ll->dev_removal_flag == REQUEST_DEV_REMOVAL)
640 lcore_ll->dev_removal_flag = ACK_DEV_REMOVAL;
645 dev_ll = lcore_ll->ll_root_used;
647 while (dev_ll != NULL) {
650 if (unlikely(vdev->remove)) {
651 dev_ll = dev_ll->next;
652 overlay_options.tunnel_destroy(vdev);
653 vdev->ready = DEVICE_SAFE_REMOVE;
656 if (likely(vdev->ready == DEVICE_RX)) {
657 /* Handle guest RX */
658 rx_count = rte_eth_rx_burst(ports[0],
659 vdev->rx_q, pkts_burst, MAX_PKT_BURST);
663 * Retry is enabled and the queue is
664 * full then we wait and retry to
665 * avoid packet loss. Here MAX_PKT_BURST
666 * must be less than virtio queue size
668 if (enable_retry && unlikely(rx_count >
669 rte_vhost_avail_entries(vdev->vid, VIRTIO_RXQ))) {
670 for (retry = 0; retry < burst_rx_retry_num;
672 rte_delay_us(burst_rx_delay_time);
673 if (rx_count <= rte_vhost_avail_entries(vdev->vid, VIRTIO_RXQ))
678 ret_count = overlay_options.rx_handle(vdev->vid, pkts_burst, rx_count);
681 &dev_statistics[vdev->vid].rx_total_atomic,
684 &dev_statistics[vdev->vid].rx_atomic, ret_count);
686 while (likely(rx_count)) {
688 rte_pktmbuf_free(pkts_burst[rx_count]);
694 if (likely(!vdev->remove)) {
696 tx_count = rte_vhost_dequeue_burst(vdev->vid,
697 VIRTIO_TXQ, mbuf_pool,
698 pkts_burst, MAX_PKT_BURST);
699 /* If this is the first received packet we need to learn the MAC */
700 if (unlikely(vdev->ready == DEVICE_MAC_LEARNING) && tx_count) {
702 (overlay_options.tunnel_setup(vdev, pkts_burst[0]) == -1)) {
704 rte_pktmbuf_free(pkts_burst[--tx_count]);
708 virtio_tx_route(vdev, pkts_burst[--tx_count]);
711 /* move to the next device in the list */
712 dev_ll = dev_ll->next;
720 * Add an entry to a used linked list. A free entry must first be found
721 * in the free linked list using get_data_ll_free_entry();
724 add_data_ll_entry(struct virtio_net_data_ll **ll_root_addr,
725 struct virtio_net_data_ll *ll_dev)
727 struct virtio_net_data_ll *ll = *ll_root_addr;
729 /* Set next as NULL and use a compiler barrier to avoid reordering. */
731 rte_compiler_barrier();
733 /* If ll == NULL then this is the first device. */
735 /* Increment to the tail of the linked list. */
736 while (ll->next != NULL)
741 *ll_root_addr = ll_dev;
746 * Remove an entry from a used linked list. The entry must then be added to
747 * the free linked list using put_data_ll_free_entry().
750 rm_data_ll_entry(struct virtio_net_data_ll **ll_root_addr,
751 struct virtio_net_data_ll *ll_dev,
752 struct virtio_net_data_ll *ll_dev_last)
754 struct virtio_net_data_ll *ll = *ll_root_addr;
756 if (unlikely((ll == NULL) || (ll_dev == NULL)))
760 *ll_root_addr = ll_dev->next;
762 if (likely(ll_dev_last != NULL))
763 ll_dev_last->next = ll_dev->next;
765 RTE_LOG(ERR, VHOST_CONFIG,
766 "Remove entry form ll failed.\n");
770 * Find and return an entry from the free linked list.
772 static struct virtio_net_data_ll *
773 get_data_ll_free_entry(struct virtio_net_data_ll **ll_root_addr)
775 struct virtio_net_data_ll *ll_free = *ll_root_addr;
776 struct virtio_net_data_ll *ll_dev;
782 *ll_root_addr = ll_free->next;
788 * Place an entry back on to the free linked list.
791 put_data_ll_free_entry(struct virtio_net_data_ll **ll_root_addr,
792 struct virtio_net_data_ll *ll_dev)
794 struct virtio_net_data_ll *ll_free = *ll_root_addr;
799 ll_dev->next = ll_free;
800 *ll_root_addr = ll_dev;
804 * Creates a linked list of a given size.
806 static struct virtio_net_data_ll *
807 alloc_data_ll(uint32_t size)
809 struct virtio_net_data_ll *ll_new;
812 /* Malloc and then chain the linked list. */
813 ll_new = malloc(size * sizeof(struct virtio_net_data_ll));
814 if (ll_new == NULL) {
815 RTE_LOG(ERR, VHOST_CONFIG,
816 "Failed to allocate memory for ll_new.\n");
820 for (i = 0; i < size - 1; i++) {
821 ll_new[i].vdev = NULL;
822 ll_new[i].next = &ll_new[i+1];
824 ll_new[i].next = NULL;
830 * Create the main linked list along with each individual cores
831 * linked list. A used and a free list are created to manage entries.
838 RTE_LCORE_FOREACH_SLAVE(lcore) {
839 lcore_info[lcore].lcore_ll =
840 malloc(sizeof(struct lcore_ll_info));
841 if (lcore_info[lcore].lcore_ll == NULL) {
842 RTE_LOG(ERR, VHOST_CONFIG,
843 "Failed to allocate memory for lcore_ll.\n");
847 lcore_info[lcore].lcore_ll->device_num = 0;
848 lcore_info[lcore].lcore_ll->dev_removal_flag = ACK_DEV_REMOVAL;
849 lcore_info[lcore].lcore_ll->ll_root_used = NULL;
850 if (nb_devices % nb_switching_cores)
851 lcore_info[lcore].lcore_ll->ll_root_free =
852 alloc_data_ll((nb_devices / nb_switching_cores)
855 lcore_info[lcore].lcore_ll->ll_root_free =
856 alloc_data_ll(nb_devices / nb_switching_cores);
859 /* Allocate devices up to a maximum of MAX_DEVICES. */
860 ll_root_free = alloc_data_ll(MIN((nb_devices), MAX_DEVICES));
866 * Remove a device from the specific data core linked list and
867 * from the main linked list. Synchonization occurs through the use
868 * of the lcore dev_removal_flag.
871 destroy_device(int vid)
873 struct virtio_net_data_ll *ll_lcore_dev_cur;
874 struct virtio_net_data_ll *ll_main_dev_cur;
875 struct virtio_net_data_ll *ll_lcore_dev_last = NULL;
876 struct virtio_net_data_ll *ll_main_dev_last = NULL;
877 struct vhost_dev *vdev = NULL;
880 ll_main_dev_cur = ll_root_used;
881 while (ll_main_dev_cur != NULL) {
882 if (ll_main_dev_cur->vdev->vid == vid) {
883 vdev = ll_main_dev_cur->vdev;
890 /* set the remove flag. */
892 while (vdev->ready != DEVICE_SAFE_REMOVE)
895 /* Search for entry to be removed from lcore ll */
896 ll_lcore_dev_cur = lcore_info[vdev->coreid].lcore_ll->ll_root_used;
897 while (ll_lcore_dev_cur != NULL) {
898 if (ll_lcore_dev_cur->vdev == vdev) {
901 ll_lcore_dev_last = ll_lcore_dev_cur;
902 ll_lcore_dev_cur = ll_lcore_dev_cur->next;
906 if (ll_lcore_dev_cur == NULL) {
907 RTE_LOG(ERR, VHOST_CONFIG,
908 "(%d) Failed to find the dev to be destroy.\n", vid);
912 /* Search for entry to be removed from main ll */
913 ll_main_dev_cur = ll_root_used;
914 ll_main_dev_last = NULL;
915 while (ll_main_dev_cur != NULL) {
916 if (ll_main_dev_cur->vdev == vdev) {
919 ll_main_dev_last = ll_main_dev_cur;
920 ll_main_dev_cur = ll_main_dev_cur->next;
924 /* Remove entries from the lcore and main ll. */
925 rm_data_ll_entry(&lcore_info[vdev->coreid].lcore_ll->ll_root_used,
926 ll_lcore_dev_cur, ll_lcore_dev_last);
927 rm_data_ll_entry(&ll_root_used, ll_main_dev_cur, ll_main_dev_last);
929 /* Set the dev_removal_flag on each lcore. */
930 RTE_LCORE_FOREACH_SLAVE(lcore) {
931 lcore_info[lcore].lcore_ll->dev_removal_flag =
936 * Once each core has set the dev_removal_flag to
937 * ACK_DEV_REMOVAL we can be sure that they can no longer access
938 * the device removed from the linked lists and that the devices
939 * are no longer in use.
941 RTE_LCORE_FOREACH_SLAVE(lcore) {
942 while (lcore_info[lcore].lcore_ll->dev_removal_flag
947 /* Add the entries back to the lcore and main free ll.*/
948 put_data_ll_free_entry(&lcore_info[vdev->coreid].lcore_ll->ll_root_free,
950 put_data_ll_free_entry(&ll_root_free, ll_main_dev_cur);
952 /* Decrement number of device on the lcore. */
953 lcore_info[vdev->coreid].lcore_ll->device_num--;
955 RTE_LOG(INFO, VHOST_DATA, "(%d) Device has been removed "
956 "from data core\n", vid);
963 * A new device is added to a data core. First the device is added
964 * to the main linked list and the allocated to a specific data core.
969 struct virtio_net_data_ll *ll_dev;
970 int lcore, core_add = 0;
971 uint32_t device_num_min = nb_devices;
972 struct vhost_dev *vdev;
974 vdev = rte_zmalloc("vhost device", sizeof(*vdev), RTE_CACHE_LINE_SIZE);
976 RTE_LOG(INFO, VHOST_DATA,
977 "(%d) Couldn't allocate memory for vhost dev\n", vid);
981 /* Add device to main ll */
982 ll_dev = get_data_ll_free_entry(&ll_root_free);
983 if (ll_dev == NULL) {
984 RTE_LOG(INFO, VHOST_DATA, "(%d) No free entry found in"
985 " linked list Device limit of %d devices per core"
986 " has been reached\n", vid, nb_devices);
987 if (vdev->regions_hpa)
988 rte_free(vdev->regions_hpa);
993 add_data_ll_entry(&ll_root_used, ll_dev);
996 /* reset ready flag */
997 vdev->ready = DEVICE_MAC_LEARNING;
1000 /* Find a suitable lcore to add the device. */
1001 RTE_LCORE_FOREACH_SLAVE(lcore) {
1002 if (lcore_info[lcore].lcore_ll->device_num < device_num_min) {
1003 device_num_min = lcore_info[lcore].lcore_ll->device_num;
1007 /* Add device to lcore ll */
1008 ll_dev = get_data_ll_free_entry(&lcore_info[core_add].lcore_ll->ll_root_free);
1009 if (ll_dev == NULL) {
1010 RTE_LOG(INFO, VHOST_DATA,
1011 "(%d) Failed to add device to data core\n",
1013 vdev->ready = DEVICE_SAFE_REMOVE;
1014 destroy_device(vid);
1015 rte_free(vdev->regions_hpa);
1019 ll_dev->vdev = vdev;
1020 vdev->coreid = core_add;
1022 add_data_ll_entry(&lcore_info[vdev->coreid].lcore_ll->ll_root_used,
1025 /* Initialize device stats */
1026 memset(&dev_statistics[vid], 0,
1027 sizeof(struct device_statistics));
1029 /* Disable notifications. */
1030 rte_vhost_enable_guest_notification(vid, VIRTIO_RXQ, 0);
1031 rte_vhost_enable_guest_notification(vid, VIRTIO_TXQ, 0);
1032 lcore_info[vdev->coreid].lcore_ll->device_num++;
1034 RTE_LOG(INFO, VHOST_DATA, "(%d) Device has been added to data core %d\n",
1041 * These callback allow devices to be added to the data core when configuration
1042 * has been fully complete.
1044 static const struct vhost_device_ops virtio_net_device_ops = {
1045 .new_device = new_device,
1046 .destroy_device = destroy_device,
1050 * This is a thread will wake up after a period to print stats if the user has
1054 print_stats(__rte_unused void *arg)
1056 struct virtio_net_data_ll *dev_ll;
1057 uint64_t tx_dropped, rx_dropped;
1058 uint64_t tx, tx_total, rx, rx_total, rx_ip_csum, rx_l4_csum;
1060 const char clr[] = { 27, '[', '2', 'J', '\0' };
1061 const char top_left[] = { 27, '[', '1', ';', '1', 'H', '\0' };
1064 sleep(enable_stats);
1066 /* Clear screen and move to top left */
1067 printf("%s%s", clr, top_left);
1069 printf("\nDevice statistics ================================");
1071 dev_ll = ll_root_used;
1072 while (dev_ll != NULL) {
1073 vid = dev_ll->vdev->vid;
1074 tx_total = dev_statistics[vid].tx_total;
1075 tx = dev_statistics[vid].tx;
1076 tx_dropped = tx_total - tx;
1078 rx_total = rte_atomic64_read(
1079 &dev_statistics[vid].rx_total_atomic);
1080 rx = rte_atomic64_read(
1081 &dev_statistics[vid].rx_atomic);
1082 rx_dropped = rx_total - rx;
1083 rx_ip_csum = rte_atomic64_read(
1084 &dev_statistics[vid].rx_bad_ip_csum);
1085 rx_l4_csum = rte_atomic64_read(
1086 &dev_statistics[vid].rx_bad_l4_csum);
1088 printf("\nStatistics for device %d ----------"
1089 "\nTX total: %"PRIu64""
1090 "\nTX dropped: %"PRIu64""
1091 "\nTX successful: %"PRIu64""
1092 "\nRX total: %"PRIu64""
1093 "\nRX bad IP csum: %"PRIu64""
1094 "\nRX bad L4 csum: %"PRIu64""
1095 "\nRX dropped: %"PRIu64""
1096 "\nRX successful: %"PRIu64"",
1107 dev_ll = dev_ll->next;
1109 printf("\n================================================\n");
1118 * Main function, does initialisation and calls the per-lcore functions.
1121 main(int argc, char *argv[])
1123 struct rte_mempool *mbuf_pool = NULL;
1124 unsigned lcore_id, core_id = 0;
1125 unsigned nb_ports, valid_nb_ports;
1129 static pthread_t tid;
1132 ret = rte_eal_init(argc, argv);
1134 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
1138 /* parse app arguments */
1139 ret = tep_termination_parse_args(argc, argv);
1141 rte_exit(EXIT_FAILURE, "Invalid argument\n");
1143 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
1144 if (rte_lcore_is_enabled(lcore_id))
1145 lcore_ids[core_id++] = lcore_id;
1147 /* set the number of swithcing cores available */
1148 nb_switching_cores = rte_lcore_count()-1;
1150 /* Get the number of physical ports. */
1151 nb_ports = rte_eth_dev_count_avail();
1154 * Update the global var NB_PORTS and global array PORTS
1155 * and get value of var VALID_NB_PORTS according to system ports number
1157 valid_nb_ports = check_ports_num(nb_ports);
1159 if ((valid_nb_ports == 0) || (valid_nb_ports > MAX_SUP_PORTS)) {
1160 rte_exit(EXIT_FAILURE, "Current enabled port number is %u,"
1161 "but only %u port can be enabled\n", nb_ports,
1164 /* Create the mbuf pool. */
1165 mbuf_pool = rte_pktmbuf_pool_create(
1167 NUM_MBUFS_PER_PORT * valid_nb_ports,
1172 if (mbuf_pool == NULL)
1173 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
1175 for (queue_id = 0; queue_id < MAX_QUEUES + 1; queue_id++)
1176 vpool_array[queue_id].pool = mbuf_pool;
1178 /* initialize all ports */
1179 RTE_ETH_FOREACH_DEV(portid) {
1180 /* skip ports that are not enabled */
1181 if ((enabled_port_mask & (1 << portid)) == 0) {
1182 RTE_LOG(INFO, VHOST_PORT,
1183 "Skipping disabled port %d\n", portid);
1186 if (overlay_options.port_configure(portid, mbuf_pool) != 0)
1187 rte_exit(EXIT_FAILURE,
1188 "Cannot initialize network ports\n");
1191 /* Initialise all linked lists. */
1192 if (init_data_ll() == -1)
1193 rte_exit(EXIT_FAILURE, "Failed to initialize linked list\n");
1195 /* Initialize device stats */
1196 memset(&dev_statistics, 0, sizeof(dev_statistics));
1198 /* Enable stats if the user option is set. */
1200 ret = rte_ctrl_thread_create(&tid, "print-stats", NULL,
1203 rte_exit(EXIT_FAILURE, "Cannot create print-stats thread\n");
1206 /* Launch all data cores. */
1207 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1208 rte_eal_remote_launch(switch_worker,
1209 mbuf_pool, lcore_id);
1212 ret = rte_vhost_driver_register((char *)&dev_basename, 0);
1214 rte_exit(EXIT_FAILURE, "failed to register vhost driver.\n");
1216 rte_vhost_driver_disable_features(dev_basename,
1217 1ULL << VIRTIO_NET_F_MRG_RXBUF);
1219 ret = rte_vhost_driver_callback_register(dev_basename,
1220 &virtio_net_device_ops);
1222 rte_exit(EXIT_FAILURE,
1223 "failed to register vhost driver callbacks.\n");
1226 if (rte_vhost_driver_start(dev_basename) < 0) {
1227 rte_exit(EXIT_FAILURE,
1228 "failed to start vhost driver.\n");
1231 RTE_LCORE_FOREACH_SLAVE(lcore_id)
1232 rte_eal_wait_lcore(lcore_id);