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_vhost.h>
53 #include <rte_pause.h>
57 #include "vxlan_setup.h"
59 /* the maximum number of external ports supported */
60 #define MAX_SUP_PORTS 1
63 * Calculate the number of buffers needed per port
65 #define NUM_MBUFS_PER_PORT ((MAX_QUEUES * RTE_TEST_RX_DESC_DEFAULT) +\
66 (nb_switching_cores * MAX_PKT_BURST) +\
67 (nb_switching_cores * \
68 RTE_TEST_TX_DESC_DEFAULT) +\
69 (nb_switching_cores * MBUF_CACHE_SIZE))
71 #define MBUF_CACHE_SIZE 128
72 #define MBUF_DATA_SIZE RTE_MBUF_DEFAULT_BUF_SIZE
74 #define MAX_PKT_BURST 32 /* Max burst size for RX/TX */
75 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
77 /* Defines how long we wait between retries on RX */
78 #define BURST_RX_WAIT_US 15
80 #define BURST_RX_RETRIES 4 /* Number of retries on RX. */
82 #define JUMBO_FRAME_MAX_SIZE 0x2600
84 /* State of virtio device. */
85 #define DEVICE_MAC_LEARNING 0
87 #define DEVICE_SAFE_REMOVE 2
89 /* Config_core_flag status definitions. */
90 #define REQUEST_DEV_REMOVAL 1
91 #define ACK_DEV_REMOVAL 0
93 /* Configurable number of RX/TX ring descriptors */
94 #define RTE_TEST_RX_DESC_DEFAULT 1024
95 #define RTE_TEST_TX_DESC_DEFAULT 512
97 /* Get first 4 bytes in mbuf headroom. */
98 #define MBUF_HEADROOM_UINT32(mbuf) (*(uint32_t *)((uint8_t *)(mbuf) \
99 + sizeof(struct rte_mbuf)))
101 #define INVALID_PORT_ID 0xFFFF
103 /* Size of buffers used for snprintfs. */
104 #define MAX_PRINT_BUFF 6072
106 /* Maximum character device basename size. */
107 #define MAX_BASENAME_SZ 20
109 /* Maximum long option length for option parsing. */
110 #define MAX_LONG_OPT_SZ 64
112 /* Used to compare MAC addresses. */
113 #define MAC_ADDR_CMP 0xFFFFFFFFFFFFULL
115 #define CMD_LINE_OPT_NB_DEVICES "nb-devices"
116 #define CMD_LINE_OPT_UDP_PORT "udp-port"
117 #define CMD_LINE_OPT_TX_CHECKSUM "tx-checksum"
118 #define CMD_LINE_OPT_TSO_SEGSZ "tso-segsz"
119 #define CMD_LINE_OPT_FILTER_TYPE "filter-type"
120 #define CMD_LINE_OPT_ENCAP "encap"
121 #define CMD_LINE_OPT_DECAP "decap"
122 #define CMD_LINE_OPT_RX_RETRY "rx-retry"
123 #define CMD_LINE_OPT_RX_RETRY_DELAY "rx-retry-delay"
124 #define CMD_LINE_OPT_RX_RETRY_NUM "rx-retry-num"
125 #define CMD_LINE_OPT_STATS "stats"
126 #define CMD_LINE_OPT_DEV_BASENAME "dev-basename"
128 /* mask of enabled ports */
129 static uint32_t enabled_port_mask;
131 /*Number of switching cores enabled*/
132 static uint32_t nb_switching_cores;
134 /* number of devices/queues to support*/
135 uint16_t nb_devices = 2;
137 /* max ring descriptor, ixgbe, i40e, e1000 all are 4096. */
138 #define MAX_RING_DESC 4096
141 struct rte_mempool *pool;
142 struct rte_ring *ring;
144 } vpool_array[MAX_QUEUES+MAX_QUEUES];
146 /* UDP tunneling port */
147 uint16_t udp_port = 4789;
149 /* enable/disable inner TX checksum */
150 uint8_t tx_checksum = 0;
152 /* TCP segment size */
153 uint16_t tso_segsz = 0;
155 /* enable/disable decapsulation */
156 uint8_t rx_decap = 1;
158 /* enable/disable encapsulation */
159 uint8_t tx_encap = 1;
161 /* RX filter type for tunneling packet */
162 uint8_t filter_idx = 1;
164 /* overlay packet operation */
165 struct ol_switch_ops overlay_options = {
166 .port_configure = vxlan_port_init,
167 .tunnel_setup = vxlan_link,
168 .tunnel_destroy = vxlan_unlink,
169 .tx_handle = vxlan_tx_pkts,
170 .rx_handle = vxlan_rx_pkts,
171 .param_handle = NULL,
175 uint32_t enable_stats = 0;
176 /* Enable retries on RX. */
177 static uint32_t enable_retry = 1;
178 /* Specify timeout (in useconds) between retries on RX. */
179 static uint32_t burst_rx_delay_time = BURST_RX_WAIT_US;
180 /* Specify the number of retries on RX. */
181 static uint32_t burst_rx_retry_num = BURST_RX_RETRIES;
183 /* Character device basename. Can be set by user. */
184 static char dev_basename[MAX_BASENAME_SZ] = "vhost-net";
186 static unsigned lcore_ids[RTE_MAX_LCORE];
187 uint16_t ports[RTE_MAX_ETHPORTS];
189 static unsigned nb_ports; /**< The number of ports specified in command line */
191 /* ethernet addresses of ports */
192 struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
194 /* heads for the main used and free linked lists for the data path. */
195 static struct virtio_net_data_ll *ll_root_used;
196 static struct virtio_net_data_ll *ll_root_free;
199 * Array of data core structures containing information on
200 * individual core linked lists.
202 static struct lcore_info lcore_info[RTE_MAX_LCORE];
204 /* Used for queueing bursts of TX packets. */
208 struct rte_mbuf *m_table[MAX_PKT_BURST];
211 /* TX queue for each data core. */
212 struct mbuf_table lcore_tx_queue[RTE_MAX_LCORE];
214 struct device_statistics dev_statistics[MAX_DEVICES];
217 * Set character device basename.
220 us_vhost_parse_basename(const char *q_arg)
222 /* parse number string */
223 if (strlen(q_arg) >= MAX_BASENAME_SZ)
226 snprintf((char *)&dev_basename, MAX_BASENAME_SZ, "%s", q_arg);
232 * Parse the portmask provided at run time.
235 parse_portmask(const char *portmask)
240 /* parse hexadecimal string */
241 pm = strtoul(portmask, &end, 16);
242 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
252 * Parse num options at run time.
255 parse_num_opt(const char *q_arg, uint32_t max_valid_value)
260 /* parse unsigned int string */
261 num = strtoul(q_arg, &end, 10);
262 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
265 if (num > max_valid_value)
275 tep_termination_usage(const char *prgname)
277 RTE_LOG(INFO, VHOST_CONFIG, "%s [EAL options] -- -p PORTMASK\n"
278 " --udp-port: UDP destination port for VXLAN packet\n"
279 " --nb-devices[1-64]: The number of virtIO device\n"
280 " --tx-checksum [0|1]: inner Tx checksum offload\n"
281 " --tso-segsz [0-N]: TCP segment size\n"
282 " --decap [0|1]: tunneling packet decapsulation\n"
283 " --encap [0|1]: tunneling packet encapsulation\n"
284 " --filter-type[1-3]: filter type for tunneling packet\n"
285 " 1: Inner MAC and tenent ID\n"
286 " 2: Inner MAC and VLAN, and tenent ID\n"
287 " 3: Outer MAC, Inner MAC and tenent ID\n"
288 " -p PORTMASK: Set mask for ports to be used by application\n"
289 " --rx-retry [0|1]: disable/enable(default) retries on rx."
290 " Enable retry if destintation queue is full\n"
291 " --rx-retry-delay [0-N]: timeout(in usecond) between retries on RX."
292 " This makes effect only if retries on rx enabled\n"
293 " --rx-retry-num [0-N]: the number of retries on rx."
294 " This makes effect only if retries on rx enabled\n"
295 " --stats [0-N]: 0: Disable stats, N: Time in seconds to print stats\n"
296 " --dev-basename: The basename to be used for the character device.\n",
301 * Parse the arguments given in the command line of the application.
304 tep_termination_parse_args(int argc, char **argv)
309 const char *prgname = argv[0];
310 static struct option long_option[] = {
311 {CMD_LINE_OPT_NB_DEVICES, required_argument, NULL, 0},
312 {CMD_LINE_OPT_UDP_PORT, required_argument, NULL, 0},
313 {CMD_LINE_OPT_TX_CHECKSUM, required_argument, NULL, 0},
314 {CMD_LINE_OPT_TSO_SEGSZ, required_argument, NULL, 0},
315 {CMD_LINE_OPT_DECAP, required_argument, NULL, 0},
316 {CMD_LINE_OPT_ENCAP, required_argument, NULL, 0},
317 {CMD_LINE_OPT_FILTER_TYPE, required_argument, NULL, 0},
318 {CMD_LINE_OPT_RX_RETRY, required_argument, NULL, 0},
319 {CMD_LINE_OPT_RX_RETRY_DELAY, required_argument, NULL, 0},
320 {CMD_LINE_OPT_RX_RETRY_NUM, required_argument, NULL, 0},
321 {CMD_LINE_OPT_STATS, required_argument, NULL, 0},
322 {CMD_LINE_OPT_DEV_BASENAME, required_argument, NULL, 0},
326 /* Parse command line */
327 while ((opt = getopt_long(argc, argv, "p:",
328 long_option, &option_index)) != EOF) {
332 enabled_port_mask = parse_portmask(optarg);
333 if (enabled_port_mask == 0) {
334 RTE_LOG(INFO, VHOST_CONFIG,
335 "Invalid portmask\n");
336 tep_termination_usage(prgname);
341 if (!strncmp(long_option[option_index].name,
342 CMD_LINE_OPT_NB_DEVICES,
343 sizeof(CMD_LINE_OPT_NB_DEVICES))) {
344 ret = parse_num_opt(optarg, MAX_DEVICES);
346 RTE_LOG(INFO, VHOST_CONFIG,
347 "Invalid argument for nb-devices [0-%d]\n",
349 tep_termination_usage(prgname);
355 /* Enable/disable retries on RX. */
356 if (!strncmp(long_option[option_index].name,
357 CMD_LINE_OPT_RX_RETRY,
358 sizeof(CMD_LINE_OPT_RX_RETRY))) {
359 ret = parse_num_opt(optarg, 1);
361 RTE_LOG(INFO, VHOST_CONFIG,
362 "Invalid argument for rx-retry [0|1]\n");
363 tep_termination_usage(prgname);
369 if (!strncmp(long_option[option_index].name,
370 CMD_LINE_OPT_TSO_SEGSZ,
371 sizeof(CMD_LINE_OPT_TSO_SEGSZ))) {
372 ret = parse_num_opt(optarg, INT16_MAX);
374 RTE_LOG(INFO, VHOST_CONFIG,
375 "Invalid argument for TCP segment size [0-N]\n");
376 tep_termination_usage(prgname);
382 if (!strncmp(long_option[option_index].name,
383 CMD_LINE_OPT_UDP_PORT,
384 sizeof(CMD_LINE_OPT_UDP_PORT))) {
385 ret = parse_num_opt(optarg, INT16_MAX);
387 RTE_LOG(INFO, VHOST_CONFIG,
388 "Invalid argument for UDP port [0-N]\n");
389 tep_termination_usage(prgname);
395 /* Specify the retries delay time (in useconds) on RX.*/
396 if (!strncmp(long_option[option_index].name,
397 CMD_LINE_OPT_RX_RETRY_DELAY,
398 sizeof(CMD_LINE_OPT_RX_RETRY_DELAY))) {
399 ret = parse_num_opt(optarg, INT32_MAX);
401 RTE_LOG(INFO, VHOST_CONFIG,
402 "Invalid argument for rx-retry-delay [0-N]\n");
403 tep_termination_usage(prgname);
406 burst_rx_delay_time = ret;
409 /* Specify the retries number on RX. */
410 if (!strncmp(long_option[option_index].name,
411 CMD_LINE_OPT_RX_RETRY_NUM,
412 sizeof(CMD_LINE_OPT_RX_RETRY_NUM))) {
413 ret = parse_num_opt(optarg, INT32_MAX);
415 RTE_LOG(INFO, VHOST_CONFIG,
416 "Invalid argument for rx-retry-num [0-N]\n");
417 tep_termination_usage(prgname);
420 burst_rx_retry_num = ret;
423 if (!strncmp(long_option[option_index].name,
424 CMD_LINE_OPT_TX_CHECKSUM,
425 sizeof(CMD_LINE_OPT_TX_CHECKSUM))) {
426 ret = parse_num_opt(optarg, 1);
428 RTE_LOG(INFO, VHOST_CONFIG,
429 "Invalid argument for tx-checksum [0|1]\n");
430 tep_termination_usage(prgname);
436 if (!strncmp(long_option[option_index].name,
437 CMD_LINE_OPT_FILTER_TYPE,
438 sizeof(CMD_LINE_OPT_FILTER_TYPE))) {
439 ret = parse_num_opt(optarg, 3);
440 if ((ret == -1) || (ret == 0)) {
441 RTE_LOG(INFO, VHOST_CONFIG,
442 "Invalid argument for filter type [1-3]\n");
443 tep_termination_usage(prgname);
446 filter_idx = ret - 1;
449 /* Enable/disable encapsulation on RX. */
450 if (!strncmp(long_option[option_index].name,
452 sizeof(CMD_LINE_OPT_DECAP))) {
453 ret = parse_num_opt(optarg, 1);
455 RTE_LOG(INFO, VHOST_CONFIG,
456 "Invalid argument for decap [0|1]\n");
457 tep_termination_usage(prgname);
463 /* Enable/disable encapsulation on TX. */
464 if (!strncmp(long_option[option_index].name,
466 sizeof(CMD_LINE_OPT_ENCAP))) {
467 ret = parse_num_opt(optarg, 1);
469 RTE_LOG(INFO, VHOST_CONFIG,
470 "Invalid argument for encap [0|1]\n");
471 tep_termination_usage(prgname);
477 /* Enable/disable stats. */
478 if (!strncmp(long_option[option_index].name,
480 sizeof(CMD_LINE_OPT_STATS))) {
481 ret = parse_num_opt(optarg, INT32_MAX);
483 RTE_LOG(INFO, VHOST_CONFIG,
484 "Invalid argument for stats [0..N]\n");
485 tep_termination_usage(prgname);
491 /* Set character device basename. */
492 if (!strncmp(long_option[option_index].name,
493 CMD_LINE_OPT_DEV_BASENAME,
494 sizeof(CMD_LINE_OPT_DEV_BASENAME))) {
495 if (us_vhost_parse_basename(optarg) == -1) {
496 RTE_LOG(INFO, VHOST_CONFIG,
497 "Invalid argument for character "
498 "device basename (Max %d characters)\n",
500 tep_termination_usage(prgname);
507 /* Invalid option - print options. */
509 tep_termination_usage(prgname);
514 for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
515 if (enabled_port_mask & (1 << i))
516 ports[nb_ports++] = (uint8_t)i;
519 if ((nb_ports == 0) || (nb_ports > MAX_SUP_PORTS)) {
520 RTE_LOG(INFO, VHOST_PORT, "Current enabled port number is %u,"
521 "but only %u port can be enabled\n", nb_ports,
530 * Update the global var NB_PORTS and array PORTS
531 * according to system ports number and return valid ports number
534 check_ports_num(unsigned max_nb_ports)
536 unsigned valid_nb_ports = nb_ports;
539 if (nb_ports > max_nb_ports) {
540 RTE_LOG(INFO, VHOST_PORT, "\nSpecified port number(%u) "
541 " exceeds total system port number(%u)\n",
542 nb_ports, max_nb_ports);
543 nb_ports = max_nb_ports;
546 for (portid = 0; portid < nb_ports; portid++) {
547 if (ports[portid] >= max_nb_ports) {
548 RTE_LOG(INFO, VHOST_PORT,
549 "\nSpecified port ID(%u) exceeds max "
550 " system port ID(%u)\n",
551 ports[portid], (max_nb_ports - 1));
552 ports[portid] = INVALID_PORT_ID;
556 return valid_nb_ports;
560 * This function routes the TX packet to the correct interface. This may be a local device
561 * or the physical port.
563 static __rte_always_inline void
564 virtio_tx_route(struct vhost_dev *vdev, struct rte_mbuf *m)
566 struct mbuf_table *tx_q;
567 struct rte_mbuf **m_table;
568 unsigned len, ret = 0;
569 const uint16_t lcore_id = rte_lcore_id();
571 RTE_LOG_DP(DEBUG, VHOST_DATA, "(%d) TX: MAC address is external\n",
574 /* Add packet to the port tx queue */
575 tx_q = &lcore_tx_queue[lcore_id];
578 tx_q->m_table[len] = m;
581 dev_statistics[vdev->vid].tx_total++;
582 dev_statistics[vdev->vid].tx++;
585 if (unlikely(len == MAX_PKT_BURST)) {
586 m_table = (struct rte_mbuf **)tx_q->m_table;
587 ret = overlay_options.tx_handle(ports[0],
588 (uint16_t)tx_q->txq_id, m_table,
589 (uint16_t)tx_q->len);
591 /* Free any buffers not handled by TX and update
594 if (unlikely(ret < len)) {
596 rte_pktmbuf_free(m_table[ret]);
597 } while (++ret < len);
608 * This function is called by each data core. It handles all
609 * RX/TX registered with the core. For TX the specific lcore
610 * linked list is used. For RX, MAC addresses are compared
611 * with all devices in the main linked list.
614 switch_worker(__rte_unused void *arg)
616 struct rte_mempool *mbuf_pool = arg;
617 struct vhost_dev *vdev = NULL;
618 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
619 struct virtio_net_data_ll *dev_ll;
620 struct mbuf_table *tx_q;
621 volatile struct lcore_ll_info *lcore_ll;
622 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
623 / US_PER_S * BURST_TX_DRAIN_US;
624 uint64_t prev_tsc, diff_tsc, cur_tsc, ret_count = 0;
626 const uint16_t lcore_id = rte_lcore_id();
627 const uint16_t num_cores = (uint16_t)rte_lcore_count();
628 uint16_t rx_count = 0;
632 RTE_LOG(INFO, VHOST_DATA, "Procesing on Core %u started\n", lcore_id);
633 lcore_ll = lcore_info[lcore_id].lcore_ll;
636 tx_q = &lcore_tx_queue[lcore_id];
637 for (i = 0; i < num_cores; i++) {
638 if (lcore_ids[i] == lcore_id) {
645 cur_tsc = rte_rdtsc();
647 * TX burst queue drain
649 diff_tsc = cur_tsc - prev_tsc;
650 if (unlikely(diff_tsc > drain_tsc)) {
653 RTE_LOG_DP(DEBUG, VHOST_DATA, "TX queue drained after "
654 "timeout with burst size %u\n",
656 ret = overlay_options.tx_handle(ports[0],
657 (uint16_t)tx_q->txq_id,
658 (struct rte_mbuf **)tx_q->m_table,
659 (uint16_t)tx_q->len);
660 if (unlikely(ret < tx_q->len)) {
662 rte_pktmbuf_free(tx_q->m_table[ret]);
663 } while (++ret < tx_q->len);
673 rte_prefetch0(lcore_ll->ll_root_used);
676 * Inform the configuration core that we have exited
677 * the linked list and that no devices are
678 * in use if requested.
680 if (lcore_ll->dev_removal_flag == REQUEST_DEV_REMOVAL)
681 lcore_ll->dev_removal_flag = ACK_DEV_REMOVAL;
686 dev_ll = lcore_ll->ll_root_used;
688 while (dev_ll != NULL) {
691 if (unlikely(vdev->remove)) {
692 dev_ll = dev_ll->next;
693 overlay_options.tunnel_destroy(vdev);
694 vdev->ready = DEVICE_SAFE_REMOVE;
697 if (likely(vdev->ready == DEVICE_RX)) {
698 /* Handle guest RX */
699 rx_count = rte_eth_rx_burst(ports[0],
700 vdev->rx_q, pkts_burst, MAX_PKT_BURST);
704 * Retry is enabled and the queue is
705 * full then we wait and retry to
706 * avoid packet loss. Here MAX_PKT_BURST
707 * must be less than virtio queue size
709 if (enable_retry && unlikely(rx_count >
710 rte_vhost_avail_entries(vdev->vid, VIRTIO_RXQ))) {
711 for (retry = 0; retry < burst_rx_retry_num;
713 rte_delay_us(burst_rx_delay_time);
714 if (rx_count <= rte_vhost_avail_entries(vdev->vid, VIRTIO_RXQ))
719 ret_count = overlay_options.rx_handle(vdev->vid, pkts_burst, rx_count);
722 &dev_statistics[vdev->vid].rx_total_atomic,
725 &dev_statistics[vdev->vid].rx_atomic, ret_count);
727 while (likely(rx_count)) {
729 rte_pktmbuf_free(pkts_burst[rx_count]);
735 if (likely(!vdev->remove)) {
737 tx_count = rte_vhost_dequeue_burst(vdev->vid,
738 VIRTIO_TXQ, mbuf_pool,
739 pkts_burst, MAX_PKT_BURST);
740 /* If this is the first received packet we need to learn the MAC */
741 if (unlikely(vdev->ready == DEVICE_MAC_LEARNING) && tx_count) {
743 (overlay_options.tunnel_setup(vdev, pkts_burst[0]) == -1)) {
745 rte_pktmbuf_free(pkts_burst[--tx_count]);
749 virtio_tx_route(vdev, pkts_burst[--tx_count]);
752 /* move to the next device in the list */
753 dev_ll = dev_ll->next;
761 * Add an entry to a used linked list. A free entry must first be found
762 * in the free linked list using get_data_ll_free_entry();
765 add_data_ll_entry(struct virtio_net_data_ll **ll_root_addr,
766 struct virtio_net_data_ll *ll_dev)
768 struct virtio_net_data_ll *ll = *ll_root_addr;
770 /* Set next as NULL and use a compiler barrier to avoid reordering. */
772 rte_compiler_barrier();
774 /* If ll == NULL then this is the first device. */
776 /* Increment to the tail of the linked list. */
777 while (ll->next != NULL)
782 *ll_root_addr = ll_dev;
787 * Remove an entry from a used linked list. The entry must then be added to
788 * the free linked list using put_data_ll_free_entry().
791 rm_data_ll_entry(struct virtio_net_data_ll **ll_root_addr,
792 struct virtio_net_data_ll *ll_dev,
793 struct virtio_net_data_ll *ll_dev_last)
795 struct virtio_net_data_ll *ll = *ll_root_addr;
797 if (unlikely((ll == NULL) || (ll_dev == NULL)))
801 *ll_root_addr = ll_dev->next;
803 if (likely(ll_dev_last != NULL))
804 ll_dev_last->next = ll_dev->next;
806 RTE_LOG(ERR, VHOST_CONFIG,
807 "Remove entry form ll failed.\n");
811 * Find and return an entry from the free linked list.
813 static struct virtio_net_data_ll *
814 get_data_ll_free_entry(struct virtio_net_data_ll **ll_root_addr)
816 struct virtio_net_data_ll *ll_free = *ll_root_addr;
817 struct virtio_net_data_ll *ll_dev;
823 *ll_root_addr = ll_free->next;
829 * Place an entry back on to the free linked list.
832 put_data_ll_free_entry(struct virtio_net_data_ll **ll_root_addr,
833 struct virtio_net_data_ll *ll_dev)
835 struct virtio_net_data_ll *ll_free = *ll_root_addr;
840 ll_dev->next = ll_free;
841 *ll_root_addr = ll_dev;
845 * Creates a linked list of a given size.
847 static struct virtio_net_data_ll *
848 alloc_data_ll(uint32_t size)
850 struct virtio_net_data_ll *ll_new;
853 /* Malloc and then chain the linked list. */
854 ll_new = malloc(size * sizeof(struct virtio_net_data_ll));
855 if (ll_new == NULL) {
856 RTE_LOG(ERR, VHOST_CONFIG,
857 "Failed to allocate memory for ll_new.\n");
861 for (i = 0; i < size - 1; i++) {
862 ll_new[i].vdev = NULL;
863 ll_new[i].next = &ll_new[i+1];
865 ll_new[i].next = NULL;
871 * Create the main linked list along with each individual cores
872 * linked list. A used and a free list are created to manage entries.
879 RTE_LCORE_FOREACH_SLAVE(lcore) {
880 lcore_info[lcore].lcore_ll =
881 malloc(sizeof(struct lcore_ll_info));
882 if (lcore_info[lcore].lcore_ll == NULL) {
883 RTE_LOG(ERR, VHOST_CONFIG,
884 "Failed to allocate memory for lcore_ll.\n");
888 lcore_info[lcore].lcore_ll->device_num = 0;
889 lcore_info[lcore].lcore_ll->dev_removal_flag = ACK_DEV_REMOVAL;
890 lcore_info[lcore].lcore_ll->ll_root_used = NULL;
891 if (nb_devices % nb_switching_cores)
892 lcore_info[lcore].lcore_ll->ll_root_free =
893 alloc_data_ll((nb_devices / nb_switching_cores)
896 lcore_info[lcore].lcore_ll->ll_root_free =
897 alloc_data_ll(nb_devices / nb_switching_cores);
900 /* Allocate devices up to a maximum of MAX_DEVICES. */
901 ll_root_free = alloc_data_ll(MIN((nb_devices), MAX_DEVICES));
907 * Remove a device from the specific data core linked list and
908 * from the main linked list. Synchonization occurs through the use
909 * of the lcore dev_removal_flag.
912 destroy_device(int vid)
914 struct virtio_net_data_ll *ll_lcore_dev_cur;
915 struct virtio_net_data_ll *ll_main_dev_cur;
916 struct virtio_net_data_ll *ll_lcore_dev_last = NULL;
917 struct virtio_net_data_ll *ll_main_dev_last = NULL;
918 struct vhost_dev *vdev = NULL;
921 ll_main_dev_cur = ll_root_used;
922 while (ll_main_dev_cur != NULL) {
923 if (ll_main_dev_cur->vdev->vid == vid) {
924 vdev = ll_main_dev_cur->vdev;
931 /* set the remove flag. */
933 while (vdev->ready != DEVICE_SAFE_REMOVE)
936 /* Search for entry to be removed from lcore ll */
937 ll_lcore_dev_cur = lcore_info[vdev->coreid].lcore_ll->ll_root_used;
938 while (ll_lcore_dev_cur != NULL) {
939 if (ll_lcore_dev_cur->vdev == vdev) {
942 ll_lcore_dev_last = ll_lcore_dev_cur;
943 ll_lcore_dev_cur = ll_lcore_dev_cur->next;
947 if (ll_lcore_dev_cur == NULL) {
948 RTE_LOG(ERR, VHOST_CONFIG,
949 "(%d) Failed to find the dev to be destroy.\n", vid);
953 /* Search for entry to be removed from main ll */
954 ll_main_dev_cur = ll_root_used;
955 ll_main_dev_last = NULL;
956 while (ll_main_dev_cur != NULL) {
957 if (ll_main_dev_cur->vdev == vdev) {
960 ll_main_dev_last = ll_main_dev_cur;
961 ll_main_dev_cur = ll_main_dev_cur->next;
965 /* Remove entries from the lcore and main ll. */
966 rm_data_ll_entry(&lcore_info[vdev->coreid].lcore_ll->ll_root_used,
967 ll_lcore_dev_cur, ll_lcore_dev_last);
968 rm_data_ll_entry(&ll_root_used, ll_main_dev_cur, ll_main_dev_last);
970 /* Set the dev_removal_flag on each lcore. */
971 RTE_LCORE_FOREACH_SLAVE(lcore) {
972 lcore_info[lcore].lcore_ll->dev_removal_flag =
977 * Once each core has set the dev_removal_flag to
978 * ACK_DEV_REMOVAL we can be sure that they can no longer access
979 * the device removed from the linked lists and that the devices
980 * are no longer in use.
982 RTE_LCORE_FOREACH_SLAVE(lcore) {
983 while (lcore_info[lcore].lcore_ll->dev_removal_flag
988 /* Add the entries back to the lcore and main free ll.*/
989 put_data_ll_free_entry(&lcore_info[vdev->coreid].lcore_ll->ll_root_free,
991 put_data_ll_free_entry(&ll_root_free, ll_main_dev_cur);
993 /* Decrement number of device on the lcore. */
994 lcore_info[vdev->coreid].lcore_ll->device_num--;
996 RTE_LOG(INFO, VHOST_DATA, "(%d) Device has been removed "
997 "from data core\n", vid);
1004 * A new device is added to a data core. First the device is added
1005 * to the main linked list and the allocated to a specific data core.
1010 struct virtio_net_data_ll *ll_dev;
1011 int lcore, core_add = 0;
1012 uint32_t device_num_min = nb_devices;
1013 struct vhost_dev *vdev;
1015 vdev = rte_zmalloc("vhost device", sizeof(*vdev), RTE_CACHE_LINE_SIZE);
1017 RTE_LOG(INFO, VHOST_DATA,
1018 "(%d) Couldn't allocate memory for vhost dev\n", vid);
1022 /* Add device to main ll */
1023 ll_dev = get_data_ll_free_entry(&ll_root_free);
1024 if (ll_dev == NULL) {
1025 RTE_LOG(INFO, VHOST_DATA, "(%d) No free entry found in"
1026 " linked list Device limit of %d devices per core"
1027 " has been reached\n", vid, nb_devices);
1028 if (vdev->regions_hpa)
1029 rte_free(vdev->regions_hpa);
1033 ll_dev->vdev = vdev;
1034 add_data_ll_entry(&ll_root_used, ll_dev);
1037 /* reset ready flag */
1038 vdev->ready = DEVICE_MAC_LEARNING;
1041 /* Find a suitable lcore to add the device. */
1042 RTE_LCORE_FOREACH_SLAVE(lcore) {
1043 if (lcore_info[lcore].lcore_ll->device_num < device_num_min) {
1044 device_num_min = lcore_info[lcore].lcore_ll->device_num;
1048 /* Add device to lcore ll */
1049 ll_dev = get_data_ll_free_entry(&lcore_info[core_add].lcore_ll->ll_root_free);
1050 if (ll_dev == NULL) {
1051 RTE_LOG(INFO, VHOST_DATA,
1052 "(%d) Failed to add device to data core\n",
1054 vdev->ready = DEVICE_SAFE_REMOVE;
1055 destroy_device(vid);
1056 rte_free(vdev->regions_hpa);
1060 ll_dev->vdev = vdev;
1061 vdev->coreid = core_add;
1063 add_data_ll_entry(&lcore_info[vdev->coreid].lcore_ll->ll_root_used,
1066 /* Initialize device stats */
1067 memset(&dev_statistics[vid], 0,
1068 sizeof(struct device_statistics));
1070 /* Disable notifications. */
1071 rte_vhost_enable_guest_notification(vid, VIRTIO_RXQ, 0);
1072 rte_vhost_enable_guest_notification(vid, VIRTIO_TXQ, 0);
1073 lcore_info[vdev->coreid].lcore_ll->device_num++;
1075 RTE_LOG(INFO, VHOST_DATA, "(%d) Device has been added to data core %d\n",
1082 * These callback allow devices to be added to the data core when configuration
1083 * has been fully complete.
1085 static const struct vhost_device_ops virtio_net_device_ops = {
1086 .new_device = new_device,
1087 .destroy_device = destroy_device,
1091 * This is a thread will wake up after a period to print stats if the user has
1097 struct virtio_net_data_ll *dev_ll;
1098 uint64_t tx_dropped, rx_dropped;
1099 uint64_t tx, tx_total, rx, rx_total, rx_ip_csum, rx_l4_csum;
1101 const char clr[] = { 27, '[', '2', 'J', '\0' };
1102 const char top_left[] = { 27, '[', '1', ';', '1', 'H', '\0' };
1105 sleep(enable_stats);
1107 /* Clear screen and move to top left */
1108 printf("%s%s", clr, top_left);
1110 printf("\nDevice statistics ================================");
1112 dev_ll = ll_root_used;
1113 while (dev_ll != NULL) {
1114 vid = dev_ll->vdev->vid;
1115 tx_total = dev_statistics[vid].tx_total;
1116 tx = dev_statistics[vid].tx;
1117 tx_dropped = tx_total - tx;
1119 rx_total = rte_atomic64_read(
1120 &dev_statistics[vid].rx_total_atomic);
1121 rx = rte_atomic64_read(
1122 &dev_statistics[vid].rx_atomic);
1123 rx_dropped = rx_total - rx;
1124 rx_ip_csum = rte_atomic64_read(
1125 &dev_statistics[vid].rx_bad_ip_csum);
1126 rx_l4_csum = rte_atomic64_read(
1127 &dev_statistics[vid].rx_bad_l4_csum);
1129 printf("\nStatistics for device %d ----------"
1130 "\nTX total: %"PRIu64""
1131 "\nTX dropped: %"PRIu64""
1132 "\nTX successful: %"PRIu64""
1133 "\nRX total: %"PRIu64""
1134 "\nRX bad IP csum: %"PRIu64""
1135 "\nRX bad L4 csum: %"PRIu64""
1136 "\nRX dropped: %"PRIu64""
1137 "\nRX successful: %"PRIu64"",
1148 dev_ll = dev_ll->next;
1150 printf("\n================================================\n");
1155 * Main function, does initialisation and calls the per-lcore functions.
1158 main(int argc, char *argv[])
1160 struct rte_mempool *mbuf_pool = NULL;
1161 unsigned lcore_id, core_id = 0;
1162 unsigned nb_ports, valid_nb_ports;
1166 static pthread_t tid;
1167 char thread_name[RTE_MAX_THREAD_NAME_LEN];
1170 ret = rte_eal_init(argc, argv);
1172 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
1176 /* parse app arguments */
1177 ret = tep_termination_parse_args(argc, argv);
1179 rte_exit(EXIT_FAILURE, "Invalid argument\n");
1181 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
1182 if (rte_lcore_is_enabled(lcore_id))
1183 lcore_ids[core_id++] = lcore_id;
1185 /* set the number of swithcing cores available */
1186 nb_switching_cores = rte_lcore_count()-1;
1188 /* Get the number of physical ports. */
1189 nb_ports = rte_eth_dev_count();
1192 * Update the global var NB_PORTS and global array PORTS
1193 * and get value of var VALID_NB_PORTS according to system ports number
1195 valid_nb_ports = check_ports_num(nb_ports);
1197 if ((valid_nb_ports == 0) || (valid_nb_ports > MAX_SUP_PORTS)) {
1198 rte_exit(EXIT_FAILURE, "Current enabled port number is %u,"
1199 "but only %u port can be enabled\n", nb_ports,
1202 /* Create the mbuf pool. */
1203 mbuf_pool = rte_pktmbuf_pool_create(
1205 NUM_MBUFS_PER_PORT * valid_nb_ports,
1210 if (mbuf_pool == NULL)
1211 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
1213 for (queue_id = 0; queue_id < MAX_QUEUES + 1; queue_id++)
1214 vpool_array[queue_id].pool = mbuf_pool;
1216 /* initialize all ports */
1217 for (portid = 0; portid < nb_ports; portid++) {
1218 /* skip ports that are not enabled */
1219 if ((enabled_port_mask & (1 << portid)) == 0) {
1220 RTE_LOG(INFO, VHOST_PORT,
1221 "Skipping disabled port %d\n", portid);
1224 if (overlay_options.port_configure(portid, mbuf_pool) != 0)
1225 rte_exit(EXIT_FAILURE,
1226 "Cannot initialize network ports\n");
1229 /* Initialise all linked lists. */
1230 if (init_data_ll() == -1)
1231 rte_exit(EXIT_FAILURE, "Failed to initialize linked list\n");
1233 /* Initialize device stats */
1234 memset(&dev_statistics, 0, sizeof(dev_statistics));
1236 /* Enable stats if the user option is set. */
1238 ret = pthread_create(&tid, NULL, (void *)print_stats, NULL);
1240 rte_exit(EXIT_FAILURE, "Cannot create print-stats thread\n");
1241 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN, "print-stats");
1242 ret = rte_thread_setname(tid, thread_name);
1244 RTE_LOG(DEBUG, VHOST_CONFIG, "Cannot set print-stats name\n");
1247 /* Launch all data cores. */
1248 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1249 rte_eal_remote_launch(switch_worker,
1250 mbuf_pool, lcore_id);
1253 ret = rte_vhost_driver_register((char *)&dev_basename, 0);
1255 rte_exit(EXIT_FAILURE, "failed to register vhost driver.\n");
1257 rte_vhost_driver_disable_features(dev_basename,
1258 1ULL << VIRTIO_NET_F_MRG_RXBUF);
1260 ret = rte_vhost_driver_callback_register(dev_basename,
1261 &virtio_net_device_ops);
1263 rte_exit(EXIT_FAILURE,
1264 "failed to register vhost driver callbacks.\n");
1267 if (rte_vhost_driver_start(dev_basename) < 0) {
1268 rte_exit(EXIT_FAILURE,
1269 "failed to start vhost driver.\n");
1272 RTE_LCORE_FOREACH_SLAVE(lcore_id)
1273 rte_eal_wait_lcore(lcore_id);