4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <arpa/inet.h>
36 #include <linux/if_ether.h>
37 #include <linux/if_vlan.h>
38 #include <linux/virtio_net.h>
39 #include <linux/virtio_ring.h>
42 #include <sys/eventfd.h>
43 #include <sys/param.h>
46 #include <rte_atomic.h>
47 #include <rte_cycles.h>
48 #include <rte_ethdev.h>
50 #include <rte_string_fns.h>
51 #include <rte_malloc.h>
52 #include <rte_virtio_net.h>
56 #include "vxlan_setup.h"
58 /* the maximum number of external ports supported */
59 #define MAX_SUP_PORTS 1
62 * Calculate the number of buffers needed per port
64 #define NUM_MBUFS_PER_PORT ((MAX_QUEUES * RTE_TEST_RX_DESC_DEFAULT) +\
65 (nb_switching_cores * MAX_PKT_BURST) +\
66 (nb_switching_cores * \
67 RTE_TEST_TX_DESC_DEFAULT) +\
68 (nb_switching_cores * MBUF_CACHE_SIZE))
70 #define MBUF_CACHE_SIZE 128
71 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
73 #define MAX_PKT_BURST 32 /* Max burst size for RX/TX */
74 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
76 /* Defines how long we wait between retries on RX */
77 #define BURST_RX_WAIT_US 15
79 #define BURST_RX_RETRIES 4 /* Number of retries on RX. */
81 #define JUMBO_FRAME_MAX_SIZE 0x2600
83 /* State of virtio device. */
84 #define DEVICE_MAC_LEARNING 0
86 #define DEVICE_SAFE_REMOVE 2
88 /* Config_core_flag status definitions. */
89 #define REQUEST_DEV_REMOVAL 1
90 #define ACK_DEV_REMOVAL 0
92 /* Configurable number of RX/TX ring descriptors */
93 #define RTE_TEST_RX_DESC_DEFAULT 1024
94 #define RTE_TEST_TX_DESC_DEFAULT 512
96 /* Get first 4 bytes in mbuf headroom. */
97 #define MBUF_HEADROOM_UINT32(mbuf) (*(uint32_t *)((uint8_t *)(mbuf) \
98 + sizeof(struct rte_mbuf)))
100 #define INVALID_PORT_ID 0xFF
102 /* Size of buffers used for snprintfs. */
103 #define MAX_PRINT_BUFF 6072
105 /* Maximum character device basename size. */
106 #define MAX_BASENAME_SZ 20
108 /* Maximum long option length for option parsing. */
109 #define MAX_LONG_OPT_SZ 64
111 /* Used to compare MAC addresses. */
112 #define MAC_ADDR_CMP 0xFFFFFFFFFFFFULL
114 #define CMD_LINE_OPT_NB_DEVICES "nb-devices"
115 #define CMD_LINE_OPT_UDP_PORT "udp-port"
116 #define CMD_LINE_OPT_RX_RETRY "rx-retry"
117 #define CMD_LINE_OPT_RX_RETRY_DELAY "rx-retry-delay"
118 #define CMD_LINE_OPT_RX_RETRY_NUM "rx-retry-num"
119 #define CMD_LINE_OPT_STATS "stats"
120 #define CMD_LINE_OPT_DEV_BASENAME "dev-basename"
122 /* mask of enabled ports */
123 static uint32_t enabled_port_mask;
125 /*Number of switching cores enabled*/
126 static uint32_t nb_switching_cores;
128 /* number of devices/queues to support*/
129 uint16_t nb_devices = 2;
131 /* max ring descriptor, ixgbe, i40e, e1000 all are 4096. */
132 #define MAX_RING_DESC 4096
135 struct rte_mempool *pool;
136 struct rte_ring *ring;
138 } vpool_array[MAX_QUEUES+MAX_QUEUES];
140 /* UDP tunneling port */
141 uint16_t udp_port = 4789;
143 /* overlay packet operation */
144 struct ol_switch_ops overlay_options = {
145 .port_configure = vxlan_port_init,
146 .tunnel_setup = vxlan_link,
147 .tunnel_destroy = vxlan_unlink,
148 .tx_handle = vxlan_tx_pkts,
149 .rx_handle = vxlan_rx_pkts,
150 .param_handle = NULL,
154 uint32_t enable_stats = 0;
155 /* Enable retries on RX. */
156 static uint32_t enable_retry = 1;
157 /* Specify timeout (in useconds) between retries on RX. */
158 static uint32_t burst_rx_delay_time = BURST_RX_WAIT_US;
159 /* Specify the number of retries on RX. */
160 static uint32_t burst_rx_retry_num = BURST_RX_RETRIES;
162 /* Character device basename. Can be set by user. */
163 static char dev_basename[MAX_BASENAME_SZ] = "vhost-net";
165 static unsigned lcore_ids[RTE_MAX_LCORE];
166 uint8_t ports[RTE_MAX_ETHPORTS];
168 static unsigned nb_ports; /**< The number of ports specified in command line */
170 /* ethernet addresses of ports */
171 struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
173 /* heads for the main used and free linked lists for the data path. */
174 static struct virtio_net_data_ll *ll_root_used;
175 static struct virtio_net_data_ll *ll_root_free;
178 * Array of data core structures containing information on
179 * individual core linked lists.
181 static struct lcore_info lcore_info[RTE_MAX_LCORE];
183 /* Used for queueing bursts of TX packets. */
187 struct rte_mbuf *m_table[MAX_PKT_BURST];
190 /* TX queue for each data core. */
191 struct mbuf_table lcore_tx_queue[RTE_MAX_LCORE];
193 struct device_statistics dev_statistics[MAX_DEVICES];
196 * Set character device basename.
199 us_vhost_parse_basename(const char *q_arg)
201 /* parse number string */
202 if (strlen(q_arg) >= MAX_BASENAME_SZ)
205 snprintf((char *)&dev_basename, MAX_BASENAME_SZ, "%s", q_arg);
211 * Parse the portmask provided at run time.
214 parse_portmask(const char *portmask)
219 /* parse hexadecimal string */
220 pm = strtoul(portmask, &end, 16);
221 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
231 * Parse num options at run time.
234 parse_num_opt(const char *q_arg, uint32_t max_valid_value)
239 /* parse unsigned int string */
240 num = strtoul(q_arg, &end, 10);
241 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
244 if (num > max_valid_value)
254 tep_termination_usage(const char *prgname)
256 RTE_LOG(INFO, VHOST_CONFIG, "%s [EAL options] -- -p PORTMASK\n"
257 " --udp-port: UDP destination port for VXLAN packet\n"
258 " --nb-devices[1-64]: The number of virtIO device\n"
259 " -p PORTMASK: Set mask for ports to be used by application\n"
260 " --rx-retry [0|1]: disable/enable(default) retries on rx."
261 " Enable retry if destintation queue is full\n"
262 " --rx-retry-delay [0-N]: timeout(in usecond) between retries on RX."
263 " This makes effect only if retries on rx enabled\n"
264 " --rx-retry-num [0-N]: the number of retries on rx."
265 " This makes effect only if retries on rx enabled\n"
266 " --stats [0-N]: 0: Disable stats, N: Time in seconds to print stats\n"
267 " --dev-basename: The basename to be used for the character device.\n",
272 * Parse the arguments given in the command line of the application.
275 tep_termination_parse_args(int argc, char **argv)
280 const char *prgname = argv[0];
281 static struct option long_option[] = {
282 {CMD_LINE_OPT_NB_DEVICES, required_argument, NULL, 0},
283 {CMD_LINE_OPT_UDP_PORT, required_argument, NULL, 0},
284 {CMD_LINE_OPT_RX_RETRY, required_argument, NULL, 0},
285 {CMD_LINE_OPT_RX_RETRY_DELAY, required_argument, NULL, 0},
286 {CMD_LINE_OPT_RX_RETRY_NUM, required_argument, NULL, 0},
287 {CMD_LINE_OPT_STATS, required_argument, NULL, 0},
288 {CMD_LINE_OPT_DEV_BASENAME, required_argument, NULL, 0},
292 /* Parse command line */
293 while ((opt = getopt_long(argc, argv, "p:",
294 long_option, &option_index)) != EOF) {
298 enabled_port_mask = parse_portmask(optarg);
299 if (enabled_port_mask == 0) {
300 RTE_LOG(INFO, VHOST_CONFIG,
301 "Invalid portmask\n");
302 tep_termination_usage(prgname);
307 if (!strncmp(long_option[option_index].name,
308 CMD_LINE_OPT_NB_DEVICES,
309 sizeof(CMD_LINE_OPT_NB_DEVICES))) {
310 ret = parse_num_opt(optarg, MAX_DEVICES);
312 RTE_LOG(INFO, VHOST_CONFIG,
313 "Invalid argument for nb-devices [0-%d]\n",
315 tep_termination_usage(prgname);
321 /* Enable/disable retries on RX. */
322 if (!strncmp(long_option[option_index].name,
323 CMD_LINE_OPT_RX_RETRY,
324 sizeof(CMD_LINE_OPT_RX_RETRY))) {
325 ret = parse_num_opt(optarg, 1);
327 RTE_LOG(INFO, VHOST_CONFIG,
328 "Invalid argument for rx-retry [0|1]\n");
329 tep_termination_usage(prgname);
335 if (!strncmp(long_option[option_index].name,
336 CMD_LINE_OPT_UDP_PORT,
337 sizeof(CMD_LINE_OPT_UDP_PORT))) {
338 ret = parse_num_opt(optarg, INT16_MAX);
340 RTE_LOG(INFO, VHOST_CONFIG,
341 "Invalid argument for UDP port [0-N]\n");
342 tep_termination_usage(prgname);
348 /* Specify the retries delay time (in useconds) on RX.*/
349 if (!strncmp(long_option[option_index].name,
350 CMD_LINE_OPT_RX_RETRY_DELAY,
351 sizeof(CMD_LINE_OPT_RX_RETRY_DELAY))) {
352 ret = parse_num_opt(optarg, INT32_MAX);
354 RTE_LOG(INFO, VHOST_CONFIG,
355 "Invalid argument for rx-retry-delay [0-N]\n");
356 tep_termination_usage(prgname);
359 burst_rx_delay_time = ret;
362 /* Specify the retries number on RX. */
363 if (!strncmp(long_option[option_index].name,
364 CMD_LINE_OPT_RX_RETRY_NUM,
365 sizeof(CMD_LINE_OPT_RX_RETRY_NUM))) {
366 ret = parse_num_opt(optarg, INT32_MAX);
368 RTE_LOG(INFO, VHOST_CONFIG,
369 "Invalid argument for rx-retry-num [0-N]\n");
370 tep_termination_usage(prgname);
373 burst_rx_retry_num = ret;
376 /* Enable/disable stats. */
377 if (!strncmp(long_option[option_index].name,
379 sizeof(CMD_LINE_OPT_STATS))) {
380 ret = parse_num_opt(optarg, INT32_MAX);
382 RTE_LOG(INFO, VHOST_CONFIG,
383 "Invalid argument for stats [0..N]\n");
384 tep_termination_usage(prgname);
390 /* Set character device basename. */
391 if (!strncmp(long_option[option_index].name,
392 CMD_LINE_OPT_DEV_BASENAME,
393 sizeof(CMD_LINE_OPT_DEV_BASENAME))) {
394 if (us_vhost_parse_basename(optarg) == -1) {
395 RTE_LOG(INFO, VHOST_CONFIG,
396 "Invalid argument for character "
397 "device basename (Max %d characters)\n",
399 tep_termination_usage(prgname);
406 /* Invalid option - print options. */
408 tep_termination_usage(prgname);
413 for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
414 if (enabled_port_mask & (1 << i))
415 ports[nb_ports++] = (uint8_t)i;
418 if ((nb_ports == 0) || (nb_ports > MAX_SUP_PORTS)) {
419 RTE_LOG(INFO, VHOST_PORT, "Current enabled port number is %u,"
420 "but only %u port can be enabled\n", nb_ports,
429 * Update the global var NB_PORTS and array PORTS
430 * according to system ports number and return valid ports number
433 check_ports_num(unsigned max_nb_ports)
435 unsigned valid_nb_ports = nb_ports;
438 if (nb_ports > max_nb_ports) {
439 RTE_LOG(INFO, VHOST_PORT, "\nSpecified port number(%u) "
440 " exceeds total system port number(%u)\n",
441 nb_ports, max_nb_ports);
442 nb_ports = max_nb_ports;
445 for (portid = 0; portid < nb_ports; portid++) {
446 if (ports[portid] >= max_nb_ports) {
447 RTE_LOG(INFO, VHOST_PORT,
448 "\nSpecified port ID(%u) exceeds max "
449 " system port ID(%u)\n",
450 ports[portid], (max_nb_ports - 1));
451 ports[portid] = INVALID_PORT_ID;
455 return valid_nb_ports;
459 * This function routes the TX packet to the correct interface. This may be a local device
460 * or the physical port.
462 static inline void __attribute__((always_inline))
463 virtio_tx_route(struct vhost_dev *vdev, struct rte_mbuf *m)
465 struct mbuf_table *tx_q;
466 struct rte_mbuf **m_table;
467 unsigned len, ret = 0;
468 const uint16_t lcore_id = rte_lcore_id();
469 struct virtio_net *dev = vdev->dev;
471 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") TX: MAC address is external\n",
474 /* Add packet to the port tx queue */
475 tx_q = &lcore_tx_queue[lcore_id];
478 tx_q->m_table[len] = m;
481 dev_statistics[dev->device_fh].tx_total++;
482 dev_statistics[dev->device_fh].tx++;
485 if (unlikely(len == MAX_PKT_BURST)) {
486 m_table = (struct rte_mbuf **)tx_q->m_table;
487 ret = overlay_options.tx_handle(ports[0],
488 (uint16_t)tx_q->txq_id, m_table,
489 (uint16_t)tx_q->len);
491 /* Free any buffers not handled by TX and update
494 if (unlikely(ret < len)) {
496 rte_pktmbuf_free(m_table[ret]);
497 } while (++ret < len);
508 * This function is called by each data core. It handles all
509 * RX/TX registered with the core. For TX the specific lcore
510 * linked list is used. For RX, MAC addresses are compared
511 * with all devices in the main linked list.
514 switch_worker(__rte_unused void *arg)
516 struct rte_mempool *mbuf_pool = arg;
517 struct virtio_net *dev = NULL;
518 struct vhost_dev *vdev = NULL;
519 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
520 struct virtio_net_data_ll *dev_ll;
521 struct mbuf_table *tx_q;
522 volatile struct lcore_ll_info *lcore_ll;
523 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
524 / US_PER_S * BURST_TX_DRAIN_US;
525 uint64_t prev_tsc, diff_tsc, cur_tsc, ret_count = 0;
527 const uint16_t lcore_id = rte_lcore_id();
528 const uint16_t num_cores = (uint16_t)rte_lcore_count();
529 uint16_t rx_count = 0;
533 RTE_LOG(INFO, VHOST_DATA, "Procesing on Core %u started\n", lcore_id);
534 lcore_ll = lcore_info[lcore_id].lcore_ll;
537 tx_q = &lcore_tx_queue[lcore_id];
538 for (i = 0; i < num_cores; i++) {
539 if (lcore_ids[i] == lcore_id) {
546 cur_tsc = rte_rdtsc();
548 * TX burst queue drain
550 diff_tsc = cur_tsc - prev_tsc;
551 if (unlikely(diff_tsc > drain_tsc)) {
554 LOG_DEBUG(VHOST_DATA, "TX queue drained after "
555 "timeout with burst size %u\n",
557 ret = overlay_options.tx_handle(ports[0],
558 (uint16_t)tx_q->txq_id,
559 (struct rte_mbuf **)tx_q->m_table,
560 (uint16_t)tx_q->len);
561 if (unlikely(ret < tx_q->len)) {
563 rte_pktmbuf_free(tx_q->m_table[ret]);
564 } while (++ret < tx_q->len);
574 rte_prefetch0(lcore_ll->ll_root_used);
577 * Inform the configuration core that we have exited
578 * the linked list and that no devices are
579 * in use if requested.
581 if (lcore_ll->dev_removal_flag == REQUEST_DEV_REMOVAL)
582 lcore_ll->dev_removal_flag = ACK_DEV_REMOVAL;
587 dev_ll = lcore_ll->ll_root_used;
589 while (dev_ll != NULL) {
593 if (unlikely(vdev->remove)) {
594 dev_ll = dev_ll->next;
595 overlay_options.tunnel_destroy(vdev);
596 vdev->ready = DEVICE_SAFE_REMOVE;
599 if (likely(vdev->ready == DEVICE_RX)) {
600 /* Handle guest RX */
601 rx_count = rte_eth_rx_burst(ports[0],
602 vdev->rx_q, pkts_burst, MAX_PKT_BURST);
606 * Retry is enabled and the queue is
607 * full then we wait and retry to
608 * avoid packet loss. Here MAX_PKT_BURST
609 * must be less than virtio queue size
611 if (enable_retry && unlikely(rx_count >
612 rte_vring_available_entries(dev, VIRTIO_RXQ))) {
613 for (retry = 0; retry < burst_rx_retry_num;
615 rte_delay_us(burst_rx_delay_time);
616 if (rx_count <= rte_vring_available_entries(dev, VIRTIO_RXQ))
621 ret_count = overlay_options.rx_handle(dev, pkts_burst, rx_count);
624 &dev_statistics[dev->device_fh].rx_total_atomic,
627 &dev_statistics[dev->device_fh].rx_atomic, ret_count);
629 while (likely(rx_count)) {
631 rte_pktmbuf_free(pkts_burst[rx_count]);
637 if (likely(!vdev->remove)) {
639 tx_count = rte_vhost_dequeue_burst(dev,
640 VIRTIO_TXQ, mbuf_pool,
641 pkts_burst, MAX_PKT_BURST);
642 /* If this is the first received packet we need to learn the MAC */
643 if (unlikely(vdev->ready == DEVICE_MAC_LEARNING) && tx_count) {
645 (overlay_options.tunnel_setup(vdev, pkts_burst[0]) == -1)) {
647 rte_pktmbuf_free(pkts_burst[--tx_count]);
651 virtio_tx_route(vdev, pkts_burst[--tx_count]);
654 /* move to the next device in the list */
655 dev_ll = dev_ll->next;
663 * Add an entry to a used linked list. A free entry must first be found
664 * in the free linked list using get_data_ll_free_entry();
667 add_data_ll_entry(struct virtio_net_data_ll **ll_root_addr,
668 struct virtio_net_data_ll *ll_dev)
670 struct virtio_net_data_ll *ll = *ll_root_addr;
672 /* Set next as NULL and use a compiler barrier to avoid reordering. */
674 rte_compiler_barrier();
676 /* If ll == NULL then this is the first device. */
678 /* Increment to the tail of the linked list. */
679 while (ll->next != NULL)
684 *ll_root_addr = ll_dev;
689 * Remove an entry from a used linked list. The entry must then be added to
690 * the free linked list using put_data_ll_free_entry().
693 rm_data_ll_entry(struct virtio_net_data_ll **ll_root_addr,
694 struct virtio_net_data_ll *ll_dev,
695 struct virtio_net_data_ll *ll_dev_last)
697 struct virtio_net_data_ll *ll = *ll_root_addr;
699 if (unlikely((ll == NULL) || (ll_dev == NULL)))
703 *ll_root_addr = ll_dev->next;
705 if (likely(ll_dev_last != NULL))
706 ll_dev_last->next = ll_dev->next;
708 RTE_LOG(ERR, VHOST_CONFIG,
709 "Remove entry form ll failed.\n");
713 * Find and return an entry from the free linked list.
715 static struct virtio_net_data_ll *
716 get_data_ll_free_entry(struct virtio_net_data_ll **ll_root_addr)
718 struct virtio_net_data_ll *ll_free = *ll_root_addr;
719 struct virtio_net_data_ll *ll_dev;
725 *ll_root_addr = ll_free->next;
731 * Place an entry back on to the free linked list.
734 put_data_ll_free_entry(struct virtio_net_data_ll **ll_root_addr,
735 struct virtio_net_data_ll *ll_dev)
737 struct virtio_net_data_ll *ll_free = *ll_root_addr;
742 ll_dev->next = ll_free;
743 *ll_root_addr = ll_dev;
747 * Creates a linked list of a given size.
749 static struct virtio_net_data_ll *
750 alloc_data_ll(uint32_t size)
752 struct virtio_net_data_ll *ll_new;
755 /* Malloc and then chain the linked list. */
756 ll_new = malloc(size * sizeof(struct virtio_net_data_ll));
757 if (ll_new == NULL) {
758 RTE_LOG(ERR, VHOST_CONFIG,
759 "Failed to allocate memory for ll_new.\n");
763 for (i = 0; i < size - 1; i++) {
764 ll_new[i].vdev = NULL;
765 ll_new[i].next = &ll_new[i+1];
767 ll_new[i].next = NULL;
773 * Create the main linked list along with each individual cores
774 * linked list. A used and a free list are created to manage entries.
781 RTE_LCORE_FOREACH_SLAVE(lcore) {
782 lcore_info[lcore].lcore_ll =
783 malloc(sizeof(struct lcore_ll_info));
784 if (lcore_info[lcore].lcore_ll == NULL) {
785 RTE_LOG(ERR, VHOST_CONFIG,
786 "Failed to allocate memory for lcore_ll.\n");
790 lcore_info[lcore].lcore_ll->device_num = 0;
791 lcore_info[lcore].lcore_ll->dev_removal_flag = ACK_DEV_REMOVAL;
792 lcore_info[lcore].lcore_ll->ll_root_used = NULL;
793 if (nb_devices % nb_switching_cores)
794 lcore_info[lcore].lcore_ll->ll_root_free =
795 alloc_data_ll((nb_devices / nb_switching_cores)
798 lcore_info[lcore].lcore_ll->ll_root_free =
799 alloc_data_ll(nb_devices / nb_switching_cores);
802 /* Allocate devices up to a maximum of MAX_DEVICES. */
803 ll_root_free = alloc_data_ll(MIN((nb_devices), MAX_DEVICES));
809 * Remove a device from the specific data core linked list and
810 * from the main linked list. Synchonization occurs through the use
811 * of the lcore dev_removal_flag. Device is made volatile here
812 * to avoid re-ordering of dev->remove=1 which can cause an infinite
813 * loop in the rte_pause loop.
816 destroy_device(volatile struct virtio_net *dev)
818 struct virtio_net_data_ll *ll_lcore_dev_cur;
819 struct virtio_net_data_ll *ll_main_dev_cur;
820 struct virtio_net_data_ll *ll_lcore_dev_last = NULL;
821 struct virtio_net_data_ll *ll_main_dev_last = NULL;
822 struct vhost_dev *vdev;
825 dev->flags &= ~VIRTIO_DEV_RUNNING;
827 vdev = (struct vhost_dev *)dev->priv;
829 /* set the remove flag. */
831 while (vdev->ready != DEVICE_SAFE_REMOVE)
834 /* Search for entry to be removed from lcore ll */
835 ll_lcore_dev_cur = lcore_info[vdev->coreid].lcore_ll->ll_root_used;
836 while (ll_lcore_dev_cur != NULL) {
837 if (ll_lcore_dev_cur->vdev == vdev) {
840 ll_lcore_dev_last = ll_lcore_dev_cur;
841 ll_lcore_dev_cur = ll_lcore_dev_cur->next;
845 if (ll_lcore_dev_cur == NULL) {
846 RTE_LOG(ERR, VHOST_CONFIG,
847 "(%"PRIu64") Failed to find the dev to be destroy.\n",
852 /* Search for entry to be removed from main ll */
853 ll_main_dev_cur = ll_root_used;
854 ll_main_dev_last = NULL;
855 while (ll_main_dev_cur != NULL) {
856 if (ll_main_dev_cur->vdev == vdev) {
859 ll_main_dev_last = ll_main_dev_cur;
860 ll_main_dev_cur = ll_main_dev_cur->next;
864 /* Remove entries from the lcore and main ll. */
865 rm_data_ll_entry(&lcore_info[vdev->coreid].lcore_ll->ll_root_used,
866 ll_lcore_dev_cur, ll_lcore_dev_last);
867 rm_data_ll_entry(&ll_root_used, ll_main_dev_cur, ll_main_dev_last);
869 /* Set the dev_removal_flag on each lcore. */
870 RTE_LCORE_FOREACH_SLAVE(lcore) {
871 lcore_info[lcore].lcore_ll->dev_removal_flag =
876 * Once each core has set the dev_removal_flag to
877 * ACK_DEV_REMOVAL we can be sure that they can no longer access
878 * the device removed from the linked lists and that the devices
879 * are no longer in use.
881 RTE_LCORE_FOREACH_SLAVE(lcore) {
882 while (lcore_info[lcore].lcore_ll->dev_removal_flag
887 /* Add the entries back to the lcore and main free ll.*/
888 put_data_ll_free_entry(&lcore_info[vdev->coreid].lcore_ll->ll_root_free,
890 put_data_ll_free_entry(&ll_root_free, ll_main_dev_cur);
892 /* Decrement number of device on the lcore. */
893 lcore_info[vdev->coreid].lcore_ll->device_num--;
895 RTE_LOG(INFO, VHOST_DATA, "(%"PRIu64") Device has been removed "
896 "from data core\n", dev->device_fh);
903 * A new device is added to a data core. First the device is added
904 * to the main linked list and the allocated to a specific data core.
907 new_device(struct virtio_net *dev)
909 struct virtio_net_data_ll *ll_dev;
910 int lcore, core_add = 0;
911 uint32_t device_num_min = nb_devices;
912 struct vhost_dev *vdev;
914 vdev = rte_zmalloc("vhost device", sizeof(*vdev), RTE_CACHE_LINE_SIZE);
916 RTE_LOG(INFO, VHOST_DATA,
917 "(%"PRIu64") Couldn't allocate memory for vhost dev\n",
923 /* Add device to main ll */
924 ll_dev = get_data_ll_free_entry(&ll_root_free);
925 if (ll_dev == NULL) {
926 RTE_LOG(INFO, VHOST_DATA, "(%"PRIu64") No free entry found in"
927 " linked list Device limit of %d devices per core"
928 " has been reached\n", dev->device_fh, nb_devices);
929 if (vdev->regions_hpa)
930 rte_free(vdev->regions_hpa);
935 add_data_ll_entry(&ll_root_used, ll_dev);
936 vdev->rx_q = dev->device_fh;
938 /* reset ready flag */
939 vdev->ready = DEVICE_MAC_LEARNING;
942 /* Find a suitable lcore to add the device. */
943 RTE_LCORE_FOREACH_SLAVE(lcore) {
944 if (lcore_info[lcore].lcore_ll->device_num < device_num_min) {
945 device_num_min = lcore_info[lcore].lcore_ll->device_num;
949 /* Add device to lcore ll */
950 ll_dev = get_data_ll_free_entry(&lcore_info[core_add].lcore_ll->ll_root_free);
951 if (ll_dev == NULL) {
952 RTE_LOG(INFO, VHOST_DATA,
953 "(%"PRIu64") Failed to add device to data core\n",
955 vdev->ready = DEVICE_SAFE_REMOVE;
957 rte_free(vdev->regions_hpa);
962 vdev->coreid = core_add;
964 add_data_ll_entry(&lcore_info[vdev->coreid].lcore_ll->ll_root_used,
967 /* Initialize device stats */
968 memset(&dev_statistics[dev->device_fh], 0,
969 sizeof(struct device_statistics));
971 /* Disable notifications. */
972 rte_vhost_enable_guest_notification(dev, VIRTIO_RXQ, 0);
973 rte_vhost_enable_guest_notification(dev, VIRTIO_TXQ, 0);
974 lcore_info[vdev->coreid].lcore_ll->device_num++;
975 dev->flags |= VIRTIO_DEV_RUNNING;
977 RTE_LOG(INFO, VHOST_DATA, "(%"PRIu64") Device has been added to data core %d\n",
978 dev->device_fh, vdev->coreid);
984 * These callback allow devices to be added to the data core when configuration
985 * has been fully complete.
987 static const struct virtio_net_device_ops virtio_net_device_ops = {
988 .new_device = new_device,
989 .destroy_device = destroy_device,
993 * This is a thread will wake up after a period to print stats if the user has
999 struct virtio_net_data_ll *dev_ll;
1000 uint64_t tx_dropped, rx_dropped;
1001 uint64_t tx, tx_total, rx, rx_total;
1003 const char clr[] = { 27, '[', '2', 'J', '\0' };
1004 const char top_left[] = { 27, '[', '1', ';', '1', 'H', '\0' };
1007 sleep(enable_stats);
1009 /* Clear screen and move to top left */
1010 printf("%s%s", clr, top_left);
1012 printf("\nDevice statistics ================================");
1014 dev_ll = ll_root_used;
1015 while (dev_ll != NULL) {
1016 device_fh = (uint32_t)dev_ll->vdev->dev->device_fh;
1017 tx_total = dev_statistics[device_fh].tx_total;
1018 tx = dev_statistics[device_fh].tx;
1019 tx_dropped = tx_total - tx;
1021 rx_total = rte_atomic64_read(
1022 &dev_statistics[device_fh].rx_total_atomic);
1023 rx = rte_atomic64_read(
1024 &dev_statistics[device_fh].rx_atomic);
1025 rx_dropped = rx_total - rx;
1027 printf("\nStatistics for device %"PRIu32" ----------"
1028 "\nTX total: %"PRIu64""
1029 "\nTX dropped: %"PRIu64""
1030 "\nTX successful: %"PRIu64""
1031 "\nRX total: %"PRIu64""
1032 "\nRX dropped: %"PRIu64""
1033 "\nRX successful: %"PRIu64"",
1042 dev_ll = dev_ll->next;
1044 printf("\n================================================\n");
1049 * Main function, does initialisation and calls the per-lcore functions. The CUSE
1050 * device is also registered here to handle the IOCTLs.
1053 main(int argc, char *argv[])
1055 struct rte_mempool *mbuf_pool = NULL;
1056 unsigned lcore_id, core_id = 0;
1057 unsigned nb_ports, valid_nb_ports;
1061 static pthread_t tid;
1064 ret = rte_eal_init(argc, argv);
1066 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
1070 /* parse app arguments */
1071 ret = tep_termination_parse_args(argc, argv);
1073 rte_exit(EXIT_FAILURE, "Invalid argument\n");
1075 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
1076 if (rte_lcore_is_enabled(lcore_id))
1077 lcore_ids[core_id++] = lcore_id;
1079 /* set the number of swithcing cores available */
1080 nb_switching_cores = rte_lcore_count()-1;
1082 /* Get the number of physical ports. */
1083 nb_ports = rte_eth_dev_count();
1084 if (nb_ports > RTE_MAX_ETHPORTS)
1085 nb_ports = RTE_MAX_ETHPORTS;
1088 * Update the global var NB_PORTS and global array PORTS
1089 * and get value of var VALID_NB_PORTS according to system ports number
1091 valid_nb_ports = check_ports_num(nb_ports);
1093 if ((valid_nb_ports == 0) || (valid_nb_ports > MAX_SUP_PORTS)) {
1094 rte_exit(EXIT_FAILURE, "Current enabled port number is %u,"
1095 "but only %u port can be enabled\n", nb_ports,
1098 /* Create the mbuf pool. */
1099 mbuf_pool = rte_mempool_create(
1103 MBUF_SIZE, MBUF_CACHE_SIZE,
1104 sizeof(struct rte_pktmbuf_pool_private),
1105 rte_pktmbuf_pool_init, NULL,
1106 rte_pktmbuf_init, NULL,
1107 rte_socket_id(), 0);
1108 if (mbuf_pool == NULL)
1109 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
1111 for (queue_id = 0; queue_id < MAX_QUEUES + 1; queue_id++)
1112 vpool_array[queue_id].pool = mbuf_pool;
1114 /* Set log level. */
1115 rte_set_log_level(LOG_LEVEL);
1117 /* initialize all ports */
1118 for (portid = 0; portid < nb_ports; portid++) {
1119 /* skip ports that are not enabled */
1120 if ((enabled_port_mask & (1 << portid)) == 0) {
1121 RTE_LOG(INFO, VHOST_PORT,
1122 "Skipping disabled port %d\n", portid);
1125 if (overlay_options.port_configure(portid, mbuf_pool) != 0)
1126 rte_exit(EXIT_FAILURE,
1127 "Cannot initialize network ports\n");
1130 /* Initialise all linked lists. */
1131 if (init_data_ll() == -1)
1132 rte_exit(EXIT_FAILURE, "Failed to initialize linked list\n");
1134 /* Initialize device stats */
1135 memset(&dev_statistics, 0, sizeof(dev_statistics));
1137 /* Enable stats if the user option is set. */
1139 pthread_create(&tid, NULL, (void *)print_stats, NULL);
1141 /* Launch all data cores. */
1142 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1143 rte_eal_remote_launch(switch_worker,
1144 mbuf_pool, lcore_id);
1146 rte_vhost_feature_disable(1ULL << VIRTIO_NET_F_MRG_RXBUF);
1148 /* Register CUSE device to handle IOCTLs. */
1149 ret = rte_vhost_driver_register((char *)&dev_basename);
1151 rte_exit(EXIT_FAILURE, "CUSE device setup failure.\n");
1153 rte_vhost_driver_callback_register(&virtio_net_device_ops);
1155 /* Start CUSE session. */
1156 rte_vhost_driver_session_start();