1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <rte_cycles.h>
11 #include <rte_ethdev.h>
12 #include <rte_byteorder.h>
13 #include <rte_atomic.h>
14 #include <rte_malloc.h>
15 #include "packet_burst_generator.h"
18 #define NB_ETHPORTS_USED (1)
19 #define NB_SOCKETS (2)
20 #define MEMPOOL_CACHE_SIZE 250
21 #define MAX_PKT_BURST (32)
22 #define RTE_TEST_RX_DESC_DEFAULT (1024)
23 #define RTE_TEST_TX_DESC_DEFAULT (1024)
24 #define RTE_PORT_ALL (~(uint16_t)0x0)
26 /* how long test would take at full line rate */
27 #define RTE_TEST_DURATION (2)
30 * RX and TX Prefetch, Host, and Write-back threshold values should be
31 * carefully set for optimal performance. Consult the network
32 * controller's datasheet and supporting DPDK documentation for guidance
33 * on how these parameters should be set.
35 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
36 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
37 #define RX_WTHRESH 0 /**< Default values of RX write-back threshold reg. */
40 * These default values are optimized for use with the Intel(R) 82599 10 GbE
41 * Controller and the DPDK ixgbe PMD. Consider using other values for other
42 * network controllers and/or network drivers.
44 #define TX_PTHRESH 32 /**< Default values of TX prefetch threshold reg. */
45 #define TX_HTHRESH 0 /**< Default values of TX host threshold reg. */
46 #define TX_WTHRESH 0 /**< Default values of TX write-back threshold reg. */
48 #define MAX_TRAFFIC_BURST 2048
50 #define NB_MBUF RTE_MAX( \
51 (unsigned)(nb_ports*nb_rx_queue*nb_rxd + \
52 nb_ports*nb_lcores*MAX_PKT_BURST + \
53 nb_ports*nb_tx_queue*nb_txd + \
54 nb_lcores*MEMPOOL_CACHE_SIZE + \
55 nb_ports*MAX_TRAFFIC_BURST), \
59 static struct rte_mempool *mbufpool[NB_SOCKETS];
60 /* ethernet addresses of ports */
61 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
63 static struct rte_eth_conf port_conf = {
65 .mq_mode = ETH_MQ_RX_NONE,
66 .max_rx_pkt_len = ETHER_MAX_LEN,
68 .offloads = DEV_RX_OFFLOAD_CRC_STRIP,
71 .mq_mode = ETH_MQ_TX_NONE,
73 .lpbk_mode = 1, /* enable loopback */
76 static struct rte_eth_rxconf rx_conf = {
78 .pthresh = RX_PTHRESH,
79 .hthresh = RX_HTHRESH,
80 .wthresh = RX_WTHRESH,
85 static struct rte_eth_txconf tx_conf = {
87 .pthresh = TX_PTHRESH,
88 .hthresh = TX_HTHRESH,
89 .wthresh = TX_WTHRESH,
91 .tx_free_thresh = 32, /* Use PMD default values */
92 .tx_rs_thresh = 32, /* Use PMD default values */
105 uint16_t portlist[RTE_MAX_ETHPORTS];
106 } __rte_cache_aligned;
108 struct lcore_conf lcore_conf[RTE_MAX_LCORE];
110 static uint64_t link_mbps;
118 static uint32_t sc_flag;
120 /* Check the link status of all ports in up to 3s, and print them finally */
122 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
124 #define CHECK_INTERVAL 100 /* 100ms */
125 #define MAX_CHECK_TIME 30 /* 3s (30 * 100ms) in total */
127 uint8_t count, all_ports_up, print_flag = 0;
128 struct rte_eth_link link;
130 printf("Checking link statuses...\n");
132 for (count = 0; count <= MAX_CHECK_TIME; count++) {
134 for (portid = 0; portid < port_num; portid++) {
135 if ((port_mask & (1 << portid)) == 0)
137 memset(&link, 0, sizeof(link));
138 rte_eth_link_get_nowait(portid, &link);
139 /* print link status if flag set */
140 if (print_flag == 1) {
141 if (link.link_status) {
143 "Port%d Link Up. Speed %u Mbps - %s\n",
144 portid, link.link_speed,
145 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
146 ("full-duplex") : ("half-duplex\n"));
148 link_mbps = link.link_speed;
150 printf("Port %d Link Down\n", portid);
153 /* clear all_ports_up flag if any link down */
154 if (link.link_status == ETH_LINK_DOWN) {
159 /* after finally printing all link status, get out */
163 if (all_ports_up == 0) {
165 rte_delay_ms(CHECK_INTERVAL);
168 /* set the print_flag if all ports up or timeout */
169 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1))
175 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
177 char buf[ETHER_ADDR_FMT_SIZE];
178 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
179 printf("%s%s", name, buf);
183 init_traffic(struct rte_mempool *mp,
184 struct rte_mbuf **pkts_burst, uint32_t burst_size)
186 struct ether_hdr pkt_eth_hdr;
187 struct ipv4_hdr pkt_ipv4_hdr;
188 struct udp_hdr pkt_udp_hdr;
190 static uint8_t src_mac[] = { 0x00, 0xFF, 0xAA, 0xFF, 0xAA, 0xFF };
191 static uint8_t dst_mac[] = { 0x00, 0xAA, 0xFF, 0xAA, 0xFF, 0xAA };
194 initialize_eth_header(&pkt_eth_hdr,
195 (struct ether_addr *)src_mac,
196 (struct ether_addr *)dst_mac, ETHER_TYPE_IPv4, 0, 0);
198 pktlen = initialize_ipv4_header(&pkt_ipv4_hdr,
199 IPV4_ADDR(10, 0, 0, 1),
200 IPV4_ADDR(10, 0, 0, 2), 26);
201 printf("IPv4 pktlen %u\n", pktlen);
203 pktlen = initialize_udp_header(&pkt_udp_hdr, 0, 0, 18);
205 printf("UDP pktlen %u\n", pktlen);
207 return generate_packet_burst(mp, pkts_burst, &pkt_eth_hdr,
209 &pkt_udp_hdr, burst_size,
210 PACKET_BURST_GEN_PKT_LEN, 1);
218 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
219 lcore_conf[lcore_id].socketid =
220 rte_lcore_to_socket_id(lcore_id);
221 if (rte_lcore_is_enabled(lcore_id) == 0) {
222 lcore_conf[lcore_id].status = LCORE_INVALID;
225 lcore_conf[lcore_id].status = LCORE_AVAIL;
231 init_mbufpool(unsigned nb_mbuf)
237 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
238 if (rte_lcore_is_enabled(lcore_id) == 0)
241 socketid = rte_lcore_to_socket_id(lcore_id);
242 if (socketid >= NB_SOCKETS) {
243 rte_exit(EXIT_FAILURE,
244 "Socket %d of lcore %u is out of range %d\n",
245 socketid, lcore_id, NB_SOCKETS);
247 if (mbufpool[socketid] == NULL) {
248 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
250 rte_pktmbuf_pool_create(s, nb_mbuf,
251 MEMPOOL_CACHE_SIZE, 0,
252 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
253 if (mbufpool[socketid] == NULL)
254 rte_exit(EXIT_FAILURE,
255 "Cannot init mbuf pool on socket %d\n",
258 printf("Allocated mbuf pool on socket %d\n",
266 alloc_lcore(uint16_t socketid)
270 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
271 if (LCORE_AVAIL != lcore_conf[lcore_id].status ||
272 lcore_conf[lcore_id].socketid != socketid ||
273 lcore_id == rte_get_master_lcore())
275 lcore_conf[lcore_id].status = LCORE_USED;
276 lcore_conf[lcore_id].nb_ports = 0;
283 static volatile uint64_t stop;
284 static uint64_t count;
285 static uint64_t drop;
286 static uint64_t idle;
297 stats_display(uint16_t port_id)
299 struct rte_eth_stats stats;
300 rte_eth_stats_get(port_id, &stats);
302 printf(" RX-packets: %-10"PRIu64" RX-missed: %-10"PRIu64" RX-bytes: "
304 stats.ipackets, stats.imissed, stats.ibytes);
305 printf(" RX-errors: %-10"PRIu64" RX-nombuf: %-10"PRIu64"\n",
306 stats.ierrors, stats.rx_nombuf);
307 printf(" TX-packets: %-10"PRIu64" TX-errors: %-10"PRIu64" TX-bytes: "
309 stats.opackets, stats.oerrors, stats.obytes);
313 signal_handler(int signum)
315 /* USR1 signal, stop testing */
316 if (signum == SIGUSR1) {
317 printf("Force Stop!\n");
321 /* USR2 signal, print stats */
322 if (signum == SIGUSR2)
326 struct rte_mbuf **tx_burst;
328 uint64_t (*do_measure)(struct lcore_conf *conf,
329 struct rte_mbuf *pkts_burst[],
330 uint64_t total_pkts);
333 measure_rxtx(struct lcore_conf *conf,
334 struct rte_mbuf *pkts_burst[],
337 unsigned i, portid, nb_rx, nb_tx;
338 uint64_t prev_tsc, cur_tsc;
340 prev_tsc = rte_rdtsc();
342 while (likely(!stop)) {
343 for (i = 0; i < conf->nb_ports; i++) {
344 portid = conf->portlist[i];
345 nb_rx = rte_eth_rx_burst(portid, 0,
346 pkts_burst, MAX_PKT_BURST);
347 if (unlikely(nb_rx == 0)) {
353 nb_tx = rte_eth_tx_burst(portid, 0, pkts_burst, nb_rx);
354 if (unlikely(nb_tx < nb_rx)) {
355 drop += (nb_rx - nb_tx);
357 rte_pktmbuf_free(pkts_burst[nb_tx]);
358 } while (++nb_tx < nb_rx);
361 if (unlikely(count >= total_pkts))
365 cur_tsc = rte_rdtsc();
367 return cur_tsc - prev_tsc;
371 measure_rxonly(struct lcore_conf *conf,
372 struct rte_mbuf *pkts_burst[],
375 unsigned i, portid, nb_rx, nb_tx;
376 uint64_t diff_tsc, cur_tsc;
379 while (likely(!stop)) {
380 for (i = 0; i < conf->nb_ports; i++) {
381 portid = conf->portlist[i];
383 cur_tsc = rte_rdtsc();
384 nb_rx = rte_eth_rx_burst(portid, 0,
385 pkts_burst, MAX_PKT_BURST);
386 if (unlikely(nb_rx == 0)) {
390 diff_tsc += rte_rdtsc() - cur_tsc;
393 nb_tx = rte_eth_tx_burst(portid, 0, pkts_burst, nb_rx);
394 if (unlikely(nb_tx < nb_rx)) {
395 drop += (nb_rx - nb_tx);
397 rte_pktmbuf_free(pkts_burst[nb_tx]);
398 } while (++nb_tx < nb_rx);
401 if (unlikely(count >= total_pkts))
409 measure_txonly(struct lcore_conf *conf,
410 struct rte_mbuf *pkts_burst[],
413 unsigned i, portid, nb_rx, nb_tx;
414 uint64_t diff_tsc, cur_tsc;
416 printf("do tx measure\n");
418 while (likely(!stop)) {
419 for (i = 0; i < conf->nb_ports; i++) {
420 portid = conf->portlist[i];
421 nb_rx = rte_eth_rx_burst(portid, 0,
422 pkts_burst, MAX_PKT_BURST);
423 if (unlikely(nb_rx == 0)) {
430 cur_tsc = rte_rdtsc();
431 nb_tx = rte_eth_tx_burst(portid, 0, pkts_burst, nb_rx);
432 if (unlikely(nb_tx < nb_rx)) {
433 drop += (nb_rx - nb_tx);
435 rte_pktmbuf_free(pkts_burst[nb_tx]);
436 } while (++nb_tx < nb_rx);
438 diff_tsc += rte_rdtsc() - cur_tsc;
440 if (unlikely(count >= total_pkts))
447 /* main processing loop */
449 main_loop(__rte_unused void *args)
451 #define PACKET_SIZE 64
453 #define MAC_PREAMBLE 8
454 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
456 unsigned i, portid, nb_rx = 0, nb_tx = 0;
457 struct lcore_conf *conf;
460 uint64_t packets_per_second, total_packets;
462 lcore_id = rte_lcore_id();
463 conf = &lcore_conf[lcore_id];
464 if (conf->status != LCORE_USED)
467 pkt_per_port = MAX_TRAFFIC_BURST;
470 for (i = 0; i < conf->nb_ports; i++) {
471 int num = pkt_per_port;
472 portid = conf->portlist[i];
473 printf("inject %d packet to port %d\n", num, portid);
475 nb_tx = RTE_MIN(MAX_PKT_BURST, num);
476 nb_tx = rte_eth_tx_burst(portid, 0,
477 &tx_burst[idx], nb_tx);
482 printf("Total packets inject to prime ports = %u\n", idx);
484 packets_per_second = (link_mbps * 1000 * 1000) /
485 ((PACKET_SIZE + FRAME_GAP + MAC_PREAMBLE) * CHAR_BIT);
486 printf("Each port will do %"PRIu64" packets per second\n",
489 total_packets = RTE_TEST_DURATION * conf->nb_ports * packets_per_second;
490 printf("Test will stop after at least %"PRIu64" packets received\n",
493 diff_tsc = do_measure(conf, pkts_burst, total_packets);
495 for (i = 0; i < conf->nb_ports; i++) {
496 portid = conf->portlist[i];
497 int nb_free = pkt_per_port;
499 nb_rx = rte_eth_rx_burst(portid, 0,
500 pkts_burst, MAX_PKT_BURST);
502 while (nb_tx < nb_rx)
503 rte_pktmbuf_free(pkts_burst[nb_tx++]);
505 } while (nb_free != 0);
506 printf("free %d mbuf left in port %u\n", pkt_per_port, portid);
512 printf("%"PRIu64" packet, %"PRIu64" drop, %"PRIu64" idle\n",
514 printf("Result: %"PRIu64" cycles per packet\n", diff_tsc / count);
519 static rte_atomic64_t start;
522 poll_burst(void *args)
524 #define MAX_IDLE (10000)
526 struct rte_mbuf **pkts_burst;
527 uint64_t diff_tsc, cur_tsc;
528 uint16_t next[RTE_MAX_ETHPORTS];
529 struct lcore_conf *conf;
530 uint32_t pkt_per_port = *((uint32_t *)args);
531 unsigned i, portid, nb_rx = 0;
533 uint64_t timeout = MAX_IDLE;
534 int num[RTE_MAX_ETHPORTS];
536 lcore_id = rte_lcore_id();
537 conf = &lcore_conf[lcore_id];
538 if (conf->status != LCORE_USED)
541 total = pkt_per_port * conf->nb_ports;
542 printf("start to receive total expect %"PRIu64"\n", total);
544 pkts_burst = (struct rte_mbuf **)
545 rte_calloc_socket("poll_burst",
546 total, sizeof(void *),
547 RTE_CACHE_LINE_SIZE, conf->socketid);
551 for (i = 0; i < conf->nb_ports; i++) {
552 portid = conf->portlist[i];
553 next[portid] = i * pkt_per_port;
554 num[portid] = pkt_per_port;
557 while (!rte_atomic64_read(&start))
560 cur_tsc = rte_rdtsc();
562 for (i = 0; i < conf->nb_ports; i++) {
563 portid = conf->portlist[i];
564 nb_rx = rte_eth_rx_burst(portid, 0,
565 &pkts_burst[next[portid]],
566 RTE_MIN(MAX_PKT_BURST, num[portid]));
567 if (unlikely(nb_rx == 0)) {
569 if (unlikely(timeout == 0))
573 next[portid] += nb_rx;
574 num[portid] -= nb_rx;
579 diff_tsc = rte_rdtsc() - cur_tsc;
581 printf("%"PRIu64" packets lost, IDLE %"PRIu64" times\n",
582 total, MAX_IDLE - timeout);
584 total = pkt_per_port * conf->nb_ports - total;
585 for (i = 0; i < total; i++)
586 rte_pktmbuf_free(pkts_burst[i]);
588 rte_free(pkts_burst);
591 return diff_tsc / total;
597 exec_burst(uint32_t flags, int lcore)
599 unsigned i, portid, nb_tx = 0;
600 struct lcore_conf *conf;
601 uint32_t pkt_per_port;
605 conf = &lcore_conf[lcore];
607 pkt_per_port = MAX_TRAFFIC_BURST;
608 num = pkt_per_port * conf->nb_ports;
610 rte_atomic64_init(&start);
612 /* start polling thread, but not actually poll yet */
613 rte_eal_remote_launch(poll_burst,
614 (void *)&pkt_per_port, lcore);
616 /* Only when polling first */
617 if (flags == SC_BURST_POLL_FIRST)
618 rte_atomic64_set(&start, 1);
622 nb_tx = RTE_MIN(MAX_PKT_BURST, num);
623 for (i = 0; i < conf->nb_ports; i++) {
624 portid = conf->portlist[i];
625 nb_tx = rte_eth_tx_burst(portid, 0,
626 &tx_burst[idx], nb_tx);
635 /* only when polling second */
636 if (flags == SC_BURST_XMIT_FIRST)
637 rte_atomic64_set(&start, 1);
639 /* wait for polling finished */
640 diff_tsc = rte_eal_wait_lcore(lcore);
642 printf("exec_burst: Failed to measure cycles per packet\n");
646 printf("Result: %d cycles per packet\n", diff_tsc);
654 uint16_t nb_ports, num, nb_lcores, slave_id = (uint16_t)-1;
655 uint16_t nb_rxd = MAX_TRAFFIC_BURST;
656 uint16_t nb_txd = MAX_TRAFFIC_BURST;
658 uint16_t nb_rx_queue = 1, nb_tx_queue = 1;
662 printf("Start PMD RXTX cycles cost test.\n");
664 signal(SIGUSR1, signal_handler);
665 signal(SIGUSR2, signal_handler);
667 nb_ports = rte_eth_dev_count_avail();
668 if (nb_ports < NB_ETHPORTS_USED) {
669 printf("At least %u port(s) used for perf. test\n",
674 nb_lcores = rte_lcore_count();
676 memset(lcore_conf, 0, sizeof(lcore_conf));
679 init_mbufpool(NB_MBUF);
681 if (sc_flag == SC_CONTINUOUS) {
682 nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
683 nb_txd = RTE_TEST_TX_DESC_DEFAULT;
685 printf("CONFIG RXD=%d TXD=%d\n", nb_rxd, nb_txd);
689 RTE_ETH_FOREACH_DEV(portid) {
690 if (socketid == -1) {
691 socketid = rte_eth_dev_socket_id(portid);
692 slave_id = alloc_lcore(socketid);
693 if (slave_id == (uint16_t)-1) {
694 printf("No avail lcore to run test\n");
697 printf("Performance test runs on lcore %u socket %u\n",
701 if (socketid != rte_eth_dev_socket_id(portid)) {
702 printf("Skip port %d\n", portid);
707 ret = rte_eth_dev_configure(portid, nb_rx_queue,
708 nb_tx_queue, &port_conf);
710 rte_exit(EXIT_FAILURE,
711 "Cannot configure device: err=%d, port=%d\n",
714 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
715 printf("Port %u ", portid);
716 print_ethaddr("Address:", &ports_eth_addr[portid]);
720 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
723 rte_exit(EXIT_FAILURE,
724 "rte_eth_tx_queue_setup: err=%d, "
725 "port=%d\n", ret, portid);
728 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
732 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d,"
733 "port=%d\n", ret, portid);
737 ret = rte_eth_dev_start(portid);
739 rte_exit(EXIT_FAILURE,
740 "rte_eth_dev_start: err=%d, port=%d\n",
743 /* always eanble promiscuous */
744 rte_eth_promiscuous_enable(portid);
746 lcore_conf[slave_id].portlist[num++] = portid;
747 lcore_conf[slave_id].nb_ports++;
749 check_all_ports_link_status(nb_ports, RTE_PORT_ALL);
751 if (tx_burst == NULL) {
752 tx_burst = (struct rte_mbuf **)
753 rte_calloc_socket("tx_buff",
754 MAX_TRAFFIC_BURST * nb_ports,
756 RTE_CACHE_LINE_SIZE, socketid);
761 init_traffic(mbufpool[socketid],
762 tx_burst, MAX_TRAFFIC_BURST * nb_ports);
764 printf("Generate %d packets @socket %d\n",
765 MAX_TRAFFIC_BURST * nb_ports, socketid);
767 if (sc_flag == SC_CONTINUOUS) {
768 /* do both rxtx by default */
769 if (NULL == do_measure)
770 do_measure = measure_rxtx;
772 rte_eal_remote_launch(main_loop, NULL, slave_id);
774 if (rte_eal_wait_lcore(slave_id) < 0)
776 } else if (sc_flag == SC_BURST_POLL_FIRST ||
777 sc_flag == SC_BURST_XMIT_FIRST)
778 if (exec_burst(sc_flag, slave_id) < 0)
782 RTE_ETH_FOREACH_DEV(portid) {
783 if (socketid != rte_eth_dev_socket_id(portid))
786 rte_eth_dev_stop(portid);
793 test_set_rxtx_conf(cmdline_fixed_string_t mode)
795 printf("mode switch to %s\n", mode);
797 if (!strcmp(mode, "vector")) {
799 tx_conf.tx_rs_thresh = 32;
800 tx_conf.tx_free_thresh = 32;
802 } else if (!strcmp(mode, "scalar")) {
803 /* bulk alloc rx, full-featured tx */
804 tx_conf.tx_rs_thresh = 32;
805 tx_conf.tx_free_thresh = 32;
806 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_CHECKSUM;
808 } else if (!strcmp(mode, "hybrid")) {
809 /* bulk alloc rx, vector tx
810 * when vec macro not define,
811 * using the same rx/tx as scalar
813 tx_conf.tx_rs_thresh = 32;
814 tx_conf.tx_free_thresh = 32;
815 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_CHECKSUM;
817 } else if (!strcmp(mode, "full")) {
818 /* full feature rx,tx pair */
819 tx_conf.tx_rs_thresh = 32;
820 tx_conf.tx_free_thresh = 32;
821 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_SCATTER;
829 test_set_rxtx_anchor(cmdline_fixed_string_t type)
831 printf("type switch to %s\n", type);
833 if (!strcmp(type, "rxtx")) {
834 do_measure = measure_rxtx;
836 } else if (!strcmp(type, "rxonly")) {
837 do_measure = measure_rxonly;
839 } else if (!strcmp(type, "txonly")) {
840 do_measure = measure_txonly;
848 test_set_rxtx_sc(cmdline_fixed_string_t type)
850 printf("stream control switch to %s\n", type);
852 if (!strcmp(type, "continuous")) {
853 sc_flag = SC_CONTINUOUS;
855 } else if (!strcmp(type, "poll_before_xmit")) {
856 sc_flag = SC_BURST_POLL_FIRST;
858 } else if (!strcmp(type, "poll_after_xmit")) {
859 sc_flag = SC_BURST_XMIT_FIRST;
866 REGISTER_TEST_COMMAND(pmd_perf_autotest, test_pmd_perf);