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,
70 .mq_mode = ETH_MQ_TX_NONE,
72 .lpbk_mode = 1, /* enable loopback */
75 static struct rte_eth_rxconf rx_conf = {
77 .pthresh = RX_PTHRESH,
78 .hthresh = RX_HTHRESH,
79 .wthresh = RX_WTHRESH,
84 static struct rte_eth_txconf tx_conf = {
86 .pthresh = TX_PTHRESH,
87 .hthresh = TX_HTHRESH,
88 .wthresh = TX_WTHRESH,
90 .tx_free_thresh = 32, /* Use PMD default values */
91 .tx_rs_thresh = 32, /* Use PMD default values */
104 uint16_t portlist[RTE_MAX_ETHPORTS];
105 } __rte_cache_aligned;
107 struct lcore_conf lcore_conf[RTE_MAX_LCORE];
109 static uint64_t link_mbps;
117 static uint32_t sc_flag;
119 /* Check the link status of all ports in up to 3s, and print them finally */
121 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
123 #define CHECK_INTERVAL 100 /* 100ms */
124 #define MAX_CHECK_TIME 30 /* 3s (30 * 100ms) in total */
126 uint8_t count, all_ports_up, print_flag = 0;
127 struct rte_eth_link link;
129 printf("Checking link statuses...\n");
131 for (count = 0; count <= MAX_CHECK_TIME; count++) {
133 for (portid = 0; portid < port_num; portid++) {
134 if ((port_mask & (1 << portid)) == 0)
136 memset(&link, 0, sizeof(link));
137 rte_eth_link_get_nowait(portid, &link);
138 /* print link status if flag set */
139 if (print_flag == 1) {
140 if (link.link_status) {
142 "Port%d Link Up. Speed %u Mbps - %s\n",
143 portid, link.link_speed,
144 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
145 ("full-duplex") : ("half-duplex\n"));
147 link_mbps = link.link_speed;
149 printf("Port %d Link Down\n", portid);
152 /* clear all_ports_up flag if any link down */
153 if (link.link_status == ETH_LINK_DOWN) {
158 /* after finally printing all link status, get out */
162 if (all_ports_up == 0) {
164 rte_delay_ms(CHECK_INTERVAL);
167 /* set the print_flag if all ports up or timeout */
168 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1))
174 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
176 char buf[ETHER_ADDR_FMT_SIZE];
177 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
178 printf("%s%s", name, buf);
182 init_traffic(struct rte_mempool *mp,
183 struct rte_mbuf **pkts_burst, uint32_t burst_size)
185 struct ether_hdr pkt_eth_hdr;
186 struct ipv4_hdr pkt_ipv4_hdr;
187 struct udp_hdr pkt_udp_hdr;
189 static uint8_t src_mac[] = { 0x00, 0xFF, 0xAA, 0xFF, 0xAA, 0xFF };
190 static uint8_t dst_mac[] = { 0x00, 0xAA, 0xFF, 0xAA, 0xFF, 0xAA };
193 initialize_eth_header(&pkt_eth_hdr,
194 (struct ether_addr *)src_mac,
195 (struct ether_addr *)dst_mac, ETHER_TYPE_IPv4, 0, 0);
197 pktlen = initialize_ipv4_header(&pkt_ipv4_hdr,
198 IPV4_ADDR(10, 0, 0, 1),
199 IPV4_ADDR(10, 0, 0, 2), 26);
200 printf("IPv4 pktlen %u\n", pktlen);
202 pktlen = initialize_udp_header(&pkt_udp_hdr, 0, 0, 18);
204 printf("UDP pktlen %u\n", pktlen);
206 return generate_packet_burst(mp, pkts_burst, &pkt_eth_hdr,
208 &pkt_udp_hdr, burst_size,
209 PACKET_BURST_GEN_PKT_LEN, 1);
217 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
218 lcore_conf[lcore_id].socketid =
219 rte_lcore_to_socket_id(lcore_id);
220 if (rte_lcore_is_enabled(lcore_id) == 0) {
221 lcore_conf[lcore_id].status = LCORE_INVALID;
224 lcore_conf[lcore_id].status = LCORE_AVAIL;
230 init_mbufpool(unsigned nb_mbuf)
236 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
237 if (rte_lcore_is_enabled(lcore_id) == 0)
240 socketid = rte_lcore_to_socket_id(lcore_id);
241 if (socketid >= NB_SOCKETS) {
242 rte_exit(EXIT_FAILURE,
243 "Socket %d of lcore %u is out of range %d\n",
244 socketid, lcore_id, NB_SOCKETS);
246 if (mbufpool[socketid] == NULL) {
247 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
249 rte_pktmbuf_pool_create(s, nb_mbuf,
250 MEMPOOL_CACHE_SIZE, 0,
251 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
252 if (mbufpool[socketid] == NULL)
253 rte_exit(EXIT_FAILURE,
254 "Cannot init mbuf pool on socket %d\n",
257 printf("Allocated mbuf pool on socket %d\n",
265 alloc_lcore(uint16_t socketid)
269 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
270 if (LCORE_AVAIL != lcore_conf[lcore_id].status ||
271 lcore_conf[lcore_id].socketid != socketid ||
272 lcore_id == rte_get_master_lcore())
274 lcore_conf[lcore_id].status = LCORE_USED;
275 lcore_conf[lcore_id].nb_ports = 0;
282 static volatile uint64_t stop;
283 static uint64_t count;
284 static uint64_t drop;
285 static uint64_t idle;
296 stats_display(uint16_t port_id)
298 struct rte_eth_stats stats;
299 rte_eth_stats_get(port_id, &stats);
301 printf(" RX-packets: %-10"PRIu64" RX-missed: %-10"PRIu64" RX-bytes: "
303 stats.ipackets, stats.imissed, stats.ibytes);
304 printf(" RX-errors: %-10"PRIu64" RX-nombuf: %-10"PRIu64"\n",
305 stats.ierrors, stats.rx_nombuf);
306 printf(" TX-packets: %-10"PRIu64" TX-errors: %-10"PRIu64" TX-bytes: "
308 stats.opackets, stats.oerrors, stats.obytes);
312 signal_handler(int signum)
314 /* USR1 signal, stop testing */
315 if (signum == SIGUSR1) {
316 printf("Force Stop!\n");
320 /* USR2 signal, print stats */
321 if (signum == SIGUSR2)
325 struct rte_mbuf **tx_burst;
327 uint64_t (*do_measure)(struct lcore_conf *conf,
328 struct rte_mbuf *pkts_burst[],
329 uint64_t total_pkts);
332 measure_rxtx(struct lcore_conf *conf,
333 struct rte_mbuf *pkts_burst[],
336 unsigned i, portid, nb_rx, nb_tx;
337 uint64_t prev_tsc, cur_tsc;
339 prev_tsc = rte_rdtsc();
341 while (likely(!stop)) {
342 for (i = 0; i < conf->nb_ports; i++) {
343 portid = conf->portlist[i];
344 nb_rx = rte_eth_rx_burst(portid, 0,
345 pkts_burst, MAX_PKT_BURST);
346 if (unlikely(nb_rx == 0)) {
352 nb_tx = rte_eth_tx_burst(portid, 0, pkts_burst, nb_rx);
353 if (unlikely(nb_tx < nb_rx)) {
354 drop += (nb_rx - nb_tx);
356 rte_pktmbuf_free(pkts_burst[nb_tx]);
357 } while (++nb_tx < nb_rx);
360 if (unlikely(count >= total_pkts))
364 cur_tsc = rte_rdtsc();
366 return cur_tsc - prev_tsc;
370 measure_rxonly(struct lcore_conf *conf,
371 struct rte_mbuf *pkts_burst[],
374 unsigned i, portid, nb_rx, nb_tx;
375 uint64_t diff_tsc, cur_tsc;
378 while (likely(!stop)) {
379 for (i = 0; i < conf->nb_ports; i++) {
380 portid = conf->portlist[i];
382 cur_tsc = rte_rdtsc();
383 nb_rx = rte_eth_rx_burst(portid, 0,
384 pkts_burst, MAX_PKT_BURST);
385 if (unlikely(nb_rx == 0)) {
389 diff_tsc += rte_rdtsc() - cur_tsc;
392 nb_tx = rte_eth_tx_burst(portid, 0, pkts_burst, nb_rx);
393 if (unlikely(nb_tx < nb_rx)) {
394 drop += (nb_rx - nb_tx);
396 rte_pktmbuf_free(pkts_burst[nb_tx]);
397 } while (++nb_tx < nb_rx);
400 if (unlikely(count >= total_pkts))
408 measure_txonly(struct lcore_conf *conf,
409 struct rte_mbuf *pkts_burst[],
412 unsigned i, portid, nb_rx, nb_tx;
413 uint64_t diff_tsc, cur_tsc;
415 printf("do tx measure\n");
417 while (likely(!stop)) {
418 for (i = 0; i < conf->nb_ports; i++) {
419 portid = conf->portlist[i];
420 nb_rx = rte_eth_rx_burst(portid, 0,
421 pkts_burst, MAX_PKT_BURST);
422 if (unlikely(nb_rx == 0)) {
429 cur_tsc = rte_rdtsc();
430 nb_tx = rte_eth_tx_burst(portid, 0, pkts_burst, nb_rx);
431 if (unlikely(nb_tx < nb_rx)) {
432 drop += (nb_rx - nb_tx);
434 rte_pktmbuf_free(pkts_burst[nb_tx]);
435 } while (++nb_tx < nb_rx);
437 diff_tsc += rte_rdtsc() - cur_tsc;
439 if (unlikely(count >= total_pkts))
446 /* main processing loop */
448 main_loop(__rte_unused void *args)
450 #define PACKET_SIZE 64
452 #define MAC_PREAMBLE 8
453 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
455 unsigned i, portid, nb_rx = 0, nb_tx = 0;
456 struct lcore_conf *conf;
459 uint64_t packets_per_second, total_packets;
461 lcore_id = rte_lcore_id();
462 conf = &lcore_conf[lcore_id];
463 if (conf->status != LCORE_USED)
466 pkt_per_port = MAX_TRAFFIC_BURST;
469 for (i = 0; i < conf->nb_ports; i++) {
470 int num = pkt_per_port;
471 portid = conf->portlist[i];
472 printf("inject %d packet to port %d\n", num, portid);
474 nb_tx = RTE_MIN(MAX_PKT_BURST, num);
475 nb_tx = rte_eth_tx_burst(portid, 0,
476 &tx_burst[idx], nb_tx);
481 printf("Total packets inject to prime ports = %u\n", idx);
483 packets_per_second = (link_mbps * 1000 * 1000) /
484 ((PACKET_SIZE + FRAME_GAP + MAC_PREAMBLE) * CHAR_BIT);
485 printf("Each port will do %"PRIu64" packets per second\n",
488 total_packets = RTE_TEST_DURATION * conf->nb_ports * packets_per_second;
489 printf("Test will stop after at least %"PRIu64" packets received\n",
492 diff_tsc = do_measure(conf, pkts_burst, total_packets);
494 for (i = 0; i < conf->nb_ports; i++) {
495 portid = conf->portlist[i];
496 int nb_free = pkt_per_port;
498 nb_rx = rte_eth_rx_burst(portid, 0,
499 pkts_burst, MAX_PKT_BURST);
501 while (nb_tx < nb_rx)
502 rte_pktmbuf_free(pkts_burst[nb_tx++]);
504 } while (nb_free != 0);
505 printf("free %d mbuf left in port %u\n", pkt_per_port, portid);
511 printf("%"PRIu64" packet, %"PRIu64" drop, %"PRIu64" idle\n",
513 printf("Result: %"PRIu64" cycles per packet\n", diff_tsc / count);
518 static rte_atomic64_t start;
521 poll_burst(void *args)
523 #define MAX_IDLE (10000)
525 struct rte_mbuf **pkts_burst;
526 uint64_t diff_tsc, cur_tsc;
527 uint16_t next[RTE_MAX_ETHPORTS];
528 struct lcore_conf *conf;
529 uint32_t pkt_per_port = *((uint32_t *)args);
530 unsigned i, portid, nb_rx = 0;
532 uint64_t timeout = MAX_IDLE;
533 int num[RTE_MAX_ETHPORTS];
535 lcore_id = rte_lcore_id();
536 conf = &lcore_conf[lcore_id];
537 if (conf->status != LCORE_USED)
540 total = pkt_per_port * conf->nb_ports;
541 printf("start to receive total expect %"PRIu64"\n", total);
543 pkts_burst = (struct rte_mbuf **)
544 rte_calloc_socket("poll_burst",
545 total, sizeof(void *),
546 RTE_CACHE_LINE_SIZE, conf->socketid);
550 for (i = 0; i < conf->nb_ports; i++) {
551 portid = conf->portlist[i];
552 next[portid] = i * pkt_per_port;
553 num[portid] = pkt_per_port;
556 while (!rte_atomic64_read(&start))
559 cur_tsc = rte_rdtsc();
561 for (i = 0; i < conf->nb_ports; i++) {
562 portid = conf->portlist[i];
563 nb_rx = rte_eth_rx_burst(portid, 0,
564 &pkts_burst[next[portid]],
565 RTE_MIN(MAX_PKT_BURST, num[portid]));
566 if (unlikely(nb_rx == 0)) {
568 if (unlikely(timeout == 0))
572 next[portid] += nb_rx;
573 num[portid] -= nb_rx;
578 diff_tsc = rte_rdtsc() - cur_tsc;
580 printf("%"PRIu64" packets lost, IDLE %"PRIu64" times\n",
581 total, MAX_IDLE - timeout);
583 total = pkt_per_port * conf->nb_ports - total;
584 for (i = 0; i < total; i++)
585 rte_pktmbuf_free(pkts_burst[i]);
587 rte_free(pkts_burst);
590 return diff_tsc / total;
596 exec_burst(uint32_t flags, int lcore)
598 unsigned i, portid, nb_tx = 0;
599 struct lcore_conf *conf;
600 uint32_t pkt_per_port;
604 conf = &lcore_conf[lcore];
606 pkt_per_port = MAX_TRAFFIC_BURST;
607 num = pkt_per_port * conf->nb_ports;
609 rte_atomic64_init(&start);
611 /* start polling thread, but not actually poll yet */
612 rte_eal_remote_launch(poll_burst,
613 (void *)&pkt_per_port, lcore);
615 /* Only when polling first */
616 if (flags == SC_BURST_POLL_FIRST)
617 rte_atomic64_set(&start, 1);
621 nb_tx = RTE_MIN(MAX_PKT_BURST, num);
622 for (i = 0; i < conf->nb_ports; i++) {
623 portid = conf->portlist[i];
624 nb_tx = rte_eth_tx_burst(portid, 0,
625 &tx_burst[idx], nb_tx);
634 /* only when polling second */
635 if (flags == SC_BURST_XMIT_FIRST)
636 rte_atomic64_set(&start, 1);
638 /* wait for polling finished */
639 diff_tsc = rte_eal_wait_lcore(lcore);
641 printf("exec_burst: Failed to measure cycles per packet\n");
645 printf("Result: %d cycles per packet\n", diff_tsc);
653 uint16_t nb_ports, num, nb_lcores, slave_id = (uint16_t)-1;
654 uint16_t nb_rxd = MAX_TRAFFIC_BURST;
655 uint16_t nb_txd = MAX_TRAFFIC_BURST;
657 uint16_t nb_rx_queue = 1, nb_tx_queue = 1;
661 printf("Start PMD RXTX cycles cost test.\n");
663 signal(SIGUSR1, signal_handler);
664 signal(SIGUSR2, signal_handler);
666 nb_ports = rte_eth_dev_count_avail();
667 if (nb_ports < NB_ETHPORTS_USED) {
668 printf("At least %u port(s) used for perf. test\n",
673 nb_lcores = rte_lcore_count();
675 memset(lcore_conf, 0, sizeof(lcore_conf));
678 init_mbufpool(NB_MBUF);
680 if (sc_flag == SC_CONTINUOUS) {
681 nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
682 nb_txd = RTE_TEST_TX_DESC_DEFAULT;
684 printf("CONFIG RXD=%d TXD=%d\n", nb_rxd, nb_txd);
688 RTE_ETH_FOREACH_DEV(portid) {
689 if (socketid == -1) {
690 socketid = rte_eth_dev_socket_id(portid);
691 slave_id = alloc_lcore(socketid);
692 if (slave_id == (uint16_t)-1) {
693 printf("No avail lcore to run test\n");
696 printf("Performance test runs on lcore %u socket %u\n",
700 if (socketid != rte_eth_dev_socket_id(portid)) {
701 printf("Skip port %d\n", portid);
706 ret = rte_eth_dev_configure(portid, nb_rx_queue,
707 nb_tx_queue, &port_conf);
709 rte_exit(EXIT_FAILURE,
710 "Cannot configure device: err=%d, port=%d\n",
713 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
714 printf("Port %u ", portid);
715 print_ethaddr("Address:", &ports_eth_addr[portid]);
719 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
722 rte_exit(EXIT_FAILURE,
723 "rte_eth_tx_queue_setup: err=%d, "
724 "port=%d\n", ret, portid);
727 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
731 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d,"
732 "port=%d\n", ret, portid);
736 ret = rte_eth_dev_start(portid);
738 rte_exit(EXIT_FAILURE,
739 "rte_eth_dev_start: err=%d, port=%d\n",
742 /* always eanble promiscuous */
743 rte_eth_promiscuous_enable(portid);
745 lcore_conf[slave_id].portlist[num++] = portid;
746 lcore_conf[slave_id].nb_ports++;
748 check_all_ports_link_status(nb_ports, RTE_PORT_ALL);
750 if (tx_burst == NULL) {
751 tx_burst = (struct rte_mbuf **)
752 rte_calloc_socket("tx_buff",
753 MAX_TRAFFIC_BURST * nb_ports,
755 RTE_CACHE_LINE_SIZE, socketid);
760 init_traffic(mbufpool[socketid],
761 tx_burst, MAX_TRAFFIC_BURST * nb_ports);
763 printf("Generate %d packets @socket %d\n",
764 MAX_TRAFFIC_BURST * nb_ports, socketid);
766 if (sc_flag == SC_CONTINUOUS) {
767 /* do both rxtx by default */
768 if (NULL == do_measure)
769 do_measure = measure_rxtx;
771 rte_eal_remote_launch(main_loop, NULL, slave_id);
773 if (rte_eal_wait_lcore(slave_id) < 0)
775 } else if (sc_flag == SC_BURST_POLL_FIRST ||
776 sc_flag == SC_BURST_XMIT_FIRST)
777 if (exec_burst(sc_flag, slave_id) < 0)
781 RTE_ETH_FOREACH_DEV(portid) {
782 if (socketid != rte_eth_dev_socket_id(portid))
785 rte_eth_dev_stop(portid);
792 test_set_rxtx_conf(cmdline_fixed_string_t mode)
794 printf("mode switch to %s\n", mode);
796 if (!strcmp(mode, "vector")) {
798 tx_conf.tx_rs_thresh = 32;
799 tx_conf.tx_free_thresh = 32;
801 } else if (!strcmp(mode, "scalar")) {
802 /* bulk alloc rx, full-featured tx */
803 tx_conf.tx_rs_thresh = 32;
804 tx_conf.tx_free_thresh = 32;
805 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_CHECKSUM;
807 } else if (!strcmp(mode, "hybrid")) {
808 /* bulk alloc rx, vector tx
809 * when vec macro not define,
810 * using the same rx/tx as scalar
812 tx_conf.tx_rs_thresh = 32;
813 tx_conf.tx_free_thresh = 32;
814 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_CHECKSUM;
816 } else if (!strcmp(mode, "full")) {
817 /* full feature rx,tx pair */
818 tx_conf.tx_rs_thresh = 32;
819 tx_conf.tx_free_thresh = 32;
820 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_SCATTER;
828 test_set_rxtx_anchor(cmdline_fixed_string_t type)
830 printf("type switch to %s\n", type);
832 if (!strcmp(type, "rxtx")) {
833 do_measure = measure_rxtx;
835 } else if (!strcmp(type, "rxonly")) {
836 do_measure = measure_rxonly;
838 } else if (!strcmp(type, "txonly")) {
839 do_measure = measure_txonly;
847 test_set_rxtx_sc(cmdline_fixed_string_t type)
849 printf("stream control switch to %s\n", type);
851 if (!strcmp(type, "continuous")) {
852 sc_flag = SC_CONTINUOUS;
854 } else if (!strcmp(type, "poll_before_xmit")) {
855 sc_flag = SC_BURST_POLL_FIRST;
857 } else if (!strcmp(type, "poll_after_xmit")) {
858 sc_flag = SC_BURST_XMIT_FIRST;
865 REGISTER_TEST_COMMAND(pmd_perf_autotest, test_pmd_perf);