net/af_xdp: use libxdp if available
[dpdk.git] / app / test / test_pmd_perf.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5
6 #include <stdio.h>
7 #include <inttypes.h>
8 #include <signal.h>
9 #include <unistd.h>
10 #include <rte_cycles.h>
11 #include <rte_ethdev.h>
12 #include <rte_byteorder.h>
13 #include <rte_malloc.h>
14 #include "packet_burst_generator.h"
15 #include "test.h"
16
17 #define NB_ETHPORTS_USED                (1)
18 #define NB_SOCKETS                      (2)
19 #define MEMPOOL_CACHE_SIZE 250
20 #define MAX_PKT_BURST                   (32)
21 #define RTE_TEST_RX_DESC_DEFAULT        (1024)
22 #define RTE_TEST_TX_DESC_DEFAULT        (1024)
23 #define RTE_PORT_ALL            (~(uint16_t)0x0)
24
25 /* how long test would take at full line rate */
26 #define RTE_TEST_DURATION                (2)
27
28 /*
29  * RX and TX Prefetch, Host, and Write-back threshold values should be
30  * carefully set for optimal performance. Consult the network
31  * controller's datasheet and supporting DPDK documentation for guidance
32  * on how these parameters should be set.
33  */
34 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
35 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
36 #define RX_WTHRESH 0 /**< Default values of RX write-back threshold reg. */
37
38 /*
39  * These default values are optimized for use with the Intel(R) 82599 10 GbE
40  * Controller and the DPDK ixgbe PMD. Consider using other values for other
41  * network controllers and/or network drivers.
42  */
43 #define TX_PTHRESH 32 /**< Default values of TX prefetch threshold reg. */
44 #define TX_HTHRESH 0  /**< Default values of TX host threshold reg. */
45 #define TX_WTHRESH 0  /**< Default values of TX write-back threshold reg. */
46
47 #define MAX_TRAFFIC_BURST              2048
48
49 #define NB_MBUF RTE_MAX(                                                \
50                 (unsigned)(nb_ports*nb_rx_queue*nb_rxd +                \
51                            nb_ports*nb_lcores*MAX_PKT_BURST +           \
52                            nb_ports*nb_tx_queue*nb_txd +                \
53                            nb_lcores*MEMPOOL_CACHE_SIZE +               \
54                            nb_ports*MAX_TRAFFIC_BURST),                 \
55                         (unsigned)8192)
56
57
58 static struct rte_mempool *mbufpool[NB_SOCKETS];
59 /* ethernet addresses of ports */
60 static struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
61
62 static struct rte_eth_conf port_conf = {
63         .rxmode = {
64                 .mq_mode = RTE_ETH_MQ_RX_NONE,
65                 .split_hdr_size = 0,
66         },
67         .txmode = {
68                 .mq_mode = RTE_ETH_MQ_TX_NONE,
69         },
70         .lpbk_mode = 1,  /* enable loopback */
71 };
72
73 static struct rte_eth_rxconf rx_conf = {
74         .rx_thresh = {
75                 .pthresh = RX_PTHRESH,
76                 .hthresh = RX_HTHRESH,
77                 .wthresh = RX_WTHRESH,
78         },
79         .rx_free_thresh = 32,
80 };
81
82 static struct rte_eth_txconf tx_conf = {
83         .tx_thresh = {
84                 .pthresh = TX_PTHRESH,
85                 .hthresh = TX_HTHRESH,
86                 .wthresh = TX_WTHRESH,
87         },
88         .tx_free_thresh = 32, /* Use PMD default values */
89         .tx_rs_thresh = 32, /* Use PMD default values */
90 };
91
92 enum {
93         LCORE_INVALID = 0,
94         LCORE_AVAIL,
95         LCORE_USED,
96 };
97
98 struct lcore_conf {
99         uint8_t status;
100         uint8_t socketid;
101         uint16_t nb_ports;
102         uint16_t portlist[RTE_MAX_ETHPORTS];
103 } __rte_cache_aligned;
104
105 struct lcore_conf lcore_conf[RTE_MAX_LCORE];
106
107 static uint64_t link_mbps;
108
109 enum {
110         SC_CONTINUOUS = 0,
111         SC_BURST_POLL_FIRST,
112         SC_BURST_XMIT_FIRST,
113 };
114
115 static uint32_t sc_flag;
116
117 /* Check the link status of all ports in up to 3s, and print them finally */
118 static void
119 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
120 {
121 #define CHECK_INTERVAL 100 /* 100ms */
122 #define MAX_CHECK_TIME 30 /* 3s (30 * 100ms) in total */
123         uint16_t portid;
124         uint8_t count, all_ports_up, print_flag = 0;
125         struct rte_eth_link link;
126         int ret;
127         char link_status[RTE_ETH_LINK_MAX_STR_LEN];
128
129         printf("Checking link statuses...\n");
130         fflush(stdout);
131         for (count = 0; count <= MAX_CHECK_TIME; count++) {
132                 all_ports_up = 1;
133                 for (portid = 0; portid < port_num; portid++) {
134                         if ((port_mask & (1 << portid)) == 0)
135                                 continue;
136                         memset(&link, 0, sizeof(link));
137                         ret = rte_eth_link_get_nowait(portid, &link);
138                         if (ret < 0) {
139                                 all_ports_up = 0;
140                                 if (print_flag == 1)
141                                         printf("Port %u link get failed: %s\n",
142                                                 portid, rte_strerror(-ret));
143                                 continue;
144                         }
145
146                         /* print link status if flag set */
147                         if (print_flag == 1) {
148                                 if (link.link_status && link_mbps == 0)
149                                         link_mbps = link.link_speed;
150
151                                 rte_eth_link_to_str(link_status,
152                                         sizeof(link_status), &link);
153                                 printf("Port %d %s\n", portid, link_status);
154                                 continue;
155                         }
156                         /* clear all_ports_up flag if any link down */
157                         if (link.link_status == RTE_ETH_LINK_DOWN) {
158                                 all_ports_up = 0;
159                                 break;
160                         }
161                 }
162                 /* after finally printing all link status, get out */
163                 if (print_flag == 1)
164                         break;
165
166                 if (all_ports_up == 0) {
167                         fflush(stdout);
168                         rte_delay_ms(CHECK_INTERVAL);
169                 }
170
171                 /* set the print_flag if all ports up or timeout */
172                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1))
173                         print_flag = 1;
174         }
175 }
176
177 static void
178 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
179 {
180         char buf[RTE_ETHER_ADDR_FMT_SIZE];
181         rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
182         printf("%s%s", name, buf);
183 }
184
185 static int
186 init_traffic(struct rte_mempool *mp,
187              struct rte_mbuf **pkts_burst, uint32_t burst_size)
188 {
189         struct rte_ether_hdr pkt_eth_hdr;
190         struct rte_ipv4_hdr pkt_ipv4_hdr;
191         struct rte_udp_hdr pkt_udp_hdr;
192         uint32_t pktlen;
193         static uint8_t src_mac[] = { 0x00, 0xFF, 0xAA, 0xFF, 0xAA, 0xFF };
194         static uint8_t dst_mac[] = { 0x00, 0xAA, 0xFF, 0xAA, 0xFF, 0xAA };
195
196
197         initialize_eth_header(&pkt_eth_hdr,
198                 (struct rte_ether_addr *)src_mac,
199                 (struct rte_ether_addr *)dst_mac, RTE_ETHER_TYPE_IPV4, 0, 0);
200
201         pktlen = initialize_ipv4_header(&pkt_ipv4_hdr,
202                                         IPV4_ADDR(10, 0, 0, 1),
203                                         IPV4_ADDR(10, 0, 0, 2), 26);
204         printf("IPv4 pktlen %u\n", pktlen);
205
206         pktlen = initialize_udp_header(&pkt_udp_hdr, 0, 0, 18);
207
208         printf("UDP pktlen %u\n", pktlen);
209
210         return generate_packet_burst(mp, pkts_burst, &pkt_eth_hdr,
211                                      0, &pkt_ipv4_hdr, 1,
212                                      &pkt_udp_hdr, burst_size,
213                                      PACKET_BURST_GEN_PKT_LEN, 1);
214 }
215
216 static int
217 init_lcores(void)
218 {
219         unsigned lcore_id;
220
221         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
222                 lcore_conf[lcore_id].socketid =
223                         rte_lcore_to_socket_id(lcore_id);
224                 if (rte_lcore_is_enabled(lcore_id) == 0) {
225                         lcore_conf[lcore_id].status = LCORE_INVALID;
226                         continue;
227                 } else
228                         lcore_conf[lcore_id].status = LCORE_AVAIL;
229         }
230         return 0;
231 }
232
233 static int
234 init_mbufpool(unsigned nb_mbuf)
235 {
236         int socketid;
237         unsigned lcore_id;
238         char s[64];
239
240         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
241                 if (rte_lcore_is_enabled(lcore_id) == 0)
242                         continue;
243
244                 socketid = rte_lcore_to_socket_id(lcore_id);
245                 if (socketid >= NB_SOCKETS) {
246                         rte_exit(EXIT_FAILURE,
247                                 "Socket %d of lcore %u is out of range %d\n",
248                                 socketid, lcore_id, NB_SOCKETS);
249                 }
250                 if (mbufpool[socketid] == NULL) {
251                         snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
252                         mbufpool[socketid] =
253                                 rte_pktmbuf_pool_create(s, nb_mbuf,
254                                         MEMPOOL_CACHE_SIZE, 0,
255                                         RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
256                         if (mbufpool[socketid] == NULL)
257                                 rte_exit(EXIT_FAILURE,
258                                         "Cannot init mbuf pool on socket %d\n",
259                                         socketid);
260                         else
261                                 printf("Allocated mbuf pool on socket %d\n",
262                                         socketid);
263                 }
264         }
265         return 0;
266 }
267
268 static uint16_t
269 alloc_lcore(uint16_t socketid)
270 {
271         unsigned lcore_id;
272
273         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
274                 if (LCORE_AVAIL != lcore_conf[lcore_id].status ||
275                     lcore_conf[lcore_id].socketid != socketid ||
276                     lcore_id == rte_get_main_lcore())
277                         continue;
278                 lcore_conf[lcore_id].status = LCORE_USED;
279                 lcore_conf[lcore_id].nb_ports = 0;
280                 return lcore_id;
281         }
282
283         return (uint16_t)-1;
284 }
285
286 static volatile uint64_t stop;
287 static uint64_t count;
288 static uint64_t drop;
289 static uint64_t idle;
290
291 static void
292 reset_count(void)
293 {
294         count = 0;
295         drop = 0;
296         idle = 0;
297 }
298
299 #ifndef RTE_EXEC_ENV_WINDOWS
300 static void
301 stats_display(uint16_t port_id)
302 {
303         struct rte_eth_stats stats;
304         rte_eth_stats_get(port_id, &stats);
305
306         printf("  RX-packets: %-10"PRIu64" RX-missed: %-10"PRIu64" RX-bytes:  "
307                "%-"PRIu64"\n",
308                stats.ipackets, stats.imissed, stats.ibytes);
309         printf("  RX-errors: %-10"PRIu64" RX-nombuf:  %-10"PRIu64"\n",
310                stats.ierrors, stats.rx_nombuf);
311         printf("  TX-packets: %-10"PRIu64" TX-errors: %-10"PRIu64" TX-bytes:  "
312                "%-"PRIu64"\n",
313                stats.opackets, stats.oerrors, stats.obytes);
314 }
315
316 static void
317 signal_handler(int signum)
318 {
319         /*  USR1 signal, stop testing */
320         if (signum == SIGUSR1) {
321                 printf("Force Stop!\n");
322                 stop = 1;
323         }
324
325         /*  USR2 signal, print stats */
326         if (signum == SIGUSR2)
327                 stats_display(0);
328 }
329 #endif
330
331 struct rte_mbuf **tx_burst;
332
333 uint64_t (*do_measure)(struct lcore_conf *conf,
334                        struct rte_mbuf *pkts_burst[],
335                        uint64_t total_pkts);
336
337 static uint64_t
338 measure_rxtx(struct lcore_conf *conf,
339              struct rte_mbuf *pkts_burst[],
340              uint64_t total_pkts)
341 {
342         unsigned i, portid, nb_rx, nb_tx;
343         uint64_t prev_tsc, cur_tsc;
344
345         prev_tsc = rte_rdtsc();
346
347         while (likely(!stop)) {
348                 for (i = 0; i < conf->nb_ports; i++) {
349                         portid = conf->portlist[i];
350                         nb_rx = rte_eth_rx_burst(portid, 0,
351                                                  pkts_burst, MAX_PKT_BURST);
352                         if (unlikely(nb_rx == 0)) {
353                                 idle++;
354                                 continue;
355                         }
356
357                         count += nb_rx;
358                         nb_tx = rte_eth_tx_burst(portid, 0, pkts_burst, nb_rx);
359                         if (unlikely(nb_tx < nb_rx)) {
360                                 drop += (nb_rx - nb_tx);
361                                 do {
362                                         rte_pktmbuf_free(pkts_burst[nb_tx]);
363                                 } while (++nb_tx < nb_rx);
364                         }
365                 }
366                 if (unlikely(count >= total_pkts))
367                         break;
368         }
369
370         cur_tsc = rte_rdtsc();
371
372         return cur_tsc - prev_tsc;
373 }
374
375 static uint64_t
376 measure_rxonly(struct lcore_conf *conf,
377                struct rte_mbuf *pkts_burst[],
378                uint64_t total_pkts)
379 {
380         unsigned i, portid, nb_rx, nb_tx;
381         uint64_t diff_tsc, cur_tsc;
382
383         diff_tsc = 0;
384         while (likely(!stop)) {
385                 for (i = 0; i < conf->nb_ports; i++) {
386                         portid = conf->portlist[i];
387
388                         cur_tsc = rte_rdtsc();
389                         nb_rx = rte_eth_rx_burst(portid, 0,
390                                                  pkts_burst, MAX_PKT_BURST);
391                         if (unlikely(nb_rx == 0)) {
392                                 idle++;
393                                 continue;
394                         }
395                         diff_tsc += rte_rdtsc() - cur_tsc;
396
397                         count += nb_rx;
398                         nb_tx = rte_eth_tx_burst(portid, 0, pkts_burst, nb_rx);
399                         if (unlikely(nb_tx < nb_rx)) {
400                                 drop += (nb_rx - nb_tx);
401                                 do {
402                                         rte_pktmbuf_free(pkts_burst[nb_tx]);
403                                 } while (++nb_tx < nb_rx);
404                         }
405                 }
406                 if (unlikely(count >= total_pkts))
407                         break;
408         }
409
410         return diff_tsc;
411 }
412
413 static uint64_t
414 measure_txonly(struct lcore_conf *conf,
415                struct rte_mbuf *pkts_burst[],
416                uint64_t total_pkts)
417 {
418         unsigned i, portid, nb_rx, nb_tx;
419         uint64_t diff_tsc, cur_tsc;
420
421         printf("do tx measure\n");
422         diff_tsc = 0;
423         while (likely(!stop)) {
424                 for (i = 0; i < conf->nb_ports; i++) {
425                         portid = conf->portlist[i];
426                         nb_rx = rte_eth_rx_burst(portid, 0,
427                                                  pkts_burst, MAX_PKT_BURST);
428                         if (unlikely(nb_rx == 0)) {
429                                 idle++;
430                                 continue;
431                         }
432
433                         count += nb_rx;
434
435                         cur_tsc = rte_rdtsc();
436                         nb_tx = rte_eth_tx_burst(portid, 0, pkts_burst, nb_rx);
437                         if (unlikely(nb_tx < nb_rx)) {
438                                 drop += (nb_rx - nb_tx);
439                                 do {
440                                         rte_pktmbuf_free(pkts_burst[nb_tx]);
441                                 } while (++nb_tx < nb_rx);
442                         }
443                         diff_tsc += rte_rdtsc() - cur_tsc;
444                 }
445                 if (unlikely(count >= total_pkts))
446                         break;
447         }
448
449         return diff_tsc;
450 }
451
452 /* main processing loop */
453 static int
454 main_loop(__rte_unused void *args)
455 {
456 #define PACKET_SIZE 64
457 #define FRAME_GAP 12
458 #define MAC_PREAMBLE 8
459         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
460         unsigned lcore_id;
461         unsigned i, portid, nb_rx = 0, nb_tx = 0;
462         struct lcore_conf *conf;
463         int pkt_per_port;
464         uint64_t diff_tsc;
465         uint64_t packets_per_second, total_packets;
466
467         lcore_id = rte_lcore_id();
468         conf = &lcore_conf[lcore_id];
469         if (conf->status != LCORE_USED)
470                 return 0;
471
472         pkt_per_port = MAX_TRAFFIC_BURST;
473
474         int idx = 0;
475         for (i = 0; i < conf->nb_ports; i++) {
476                 int num = pkt_per_port;
477                 portid = conf->portlist[i];
478                 printf("inject %d packet to port %d\n", num, portid);
479                 while (num) {
480                         nb_tx = RTE_MIN(MAX_PKT_BURST, num);
481                         nb_tx = rte_eth_tx_burst(portid, 0,
482                                                 &tx_burst[idx], nb_tx);
483                         num -= nb_tx;
484                         idx += nb_tx;
485                 }
486         }
487         printf("Total packets inject to prime ports = %u\n", idx);
488
489         packets_per_second = (link_mbps * 1000 * 1000) /
490                 ((PACKET_SIZE + FRAME_GAP + MAC_PREAMBLE) * CHAR_BIT);
491         printf("Each port will do %"PRIu64" packets per second\n",
492                packets_per_second);
493
494         total_packets = RTE_TEST_DURATION * conf->nb_ports * packets_per_second;
495         printf("Test will stop after at least %"PRIu64" packets received\n",
496                 + total_packets);
497
498         diff_tsc = do_measure(conf, pkts_burst, total_packets);
499
500         for (i = 0; i < conf->nb_ports; i++) {
501                 portid = conf->portlist[i];
502                 int nb_free = 0;
503                 uint64_t timeout = 10000;
504                 do { /* dry out */
505                         nb_rx = rte_eth_rx_burst(portid, 0,
506                                                  pkts_burst, MAX_PKT_BURST);
507                         nb_tx = 0;
508                         while (nb_tx < nb_rx)
509                                 rte_pktmbuf_free(pkts_burst[nb_tx++]);
510                         nb_free += nb_rx;
511
512                         if (unlikely(nb_rx == 0))
513                                 timeout--;
514                 } while (nb_free != pkt_per_port && timeout != 0);
515                 printf("free %d (expected %d) mbuf left in port %u\n", nb_free,
516                        pkt_per_port, portid);
517         }
518
519         if (count == 0)
520                 return -1;
521
522         printf("%"PRIu64" packet, %"PRIu64" drop, %"PRIu64" idle\n",
523                count, drop, idle);
524         printf("Result: %"PRIu64" cycles per packet\n", diff_tsc / count);
525
526         return 0;
527 }
528
529 static uint64_t start;
530
531 static inline int
532 poll_burst(void *args)
533 {
534 #define MAX_IDLE           (10000)
535         unsigned lcore_id;
536         struct rte_mbuf **pkts_burst;
537         uint64_t diff_tsc, cur_tsc;
538         uint16_t next[RTE_MAX_ETHPORTS];
539         struct lcore_conf *conf;
540         uint32_t pkt_per_port = *((uint32_t *)args);
541         unsigned i, portid, nb_rx = 0;
542         uint64_t total;
543         uint64_t timeout = MAX_IDLE;
544         int num[RTE_MAX_ETHPORTS];
545
546         lcore_id = rte_lcore_id();
547         conf = &lcore_conf[lcore_id];
548         if (conf->status != LCORE_USED)
549                 return 0;
550
551         total = pkt_per_port * conf->nb_ports;
552         printf("start to receive total expect %"PRIu64"\n", total);
553
554         pkts_burst = (struct rte_mbuf **)
555                 rte_calloc_socket("poll_burst",
556                                   total, sizeof(void *),
557                                   RTE_CACHE_LINE_SIZE, conf->socketid);
558         if (!pkts_burst)
559                 return -1;
560
561         for (i = 0; i < conf->nb_ports; i++) {
562                 portid = conf->portlist[i];
563                 next[portid] = i * pkt_per_port;
564                 num[portid] = pkt_per_port;
565         }
566
567         rte_wait_until_equal_64(&start, 1, __ATOMIC_ACQUIRE);
568
569         cur_tsc = rte_rdtsc();
570         while (total) {
571                 for (i = 0; i < conf->nb_ports; i++) {
572                         portid = conf->portlist[i];
573                         nb_rx = rte_eth_rx_burst(portid, 0,
574                                         &pkts_burst[next[portid]],
575                                         RTE_MIN(MAX_PKT_BURST, num[portid]));
576                         if (unlikely(nb_rx == 0)) {
577                                 timeout--;
578                                 if (unlikely(timeout == 0))
579                                         goto timeout;
580                                 continue;
581                         }
582                         next[portid] += nb_rx;
583                         num[portid] -= nb_rx;
584                         total -= nb_rx;
585                 }
586         }
587 timeout:
588         diff_tsc = rte_rdtsc() - cur_tsc;
589
590         printf("%"PRIu64" packets lost, IDLE %"PRIu64" times\n",
591                total, MAX_IDLE - timeout);
592         /* clean up */
593         total = pkt_per_port * conf->nb_ports - total;
594         for (i = 0; i < total; i++)
595                 rte_pktmbuf_free(pkts_burst[i]);
596
597         rte_free(pkts_burst);
598
599         if (total > 0)
600                 return diff_tsc / total;
601         else
602                 return -1;
603 }
604
605 static int
606 exec_burst(uint32_t flags, int lcore)
607 {
608         unsigned int portid, nb_tx = 0;
609         struct lcore_conf *conf;
610         uint32_t pkt_per_port;
611         int num, i, idx = 0;
612         int diff_tsc;
613
614         conf = &lcore_conf[lcore];
615
616         pkt_per_port = MAX_TRAFFIC_BURST;
617         num = pkt_per_port * conf->nb_ports;
618
619         /* only when polling first */
620         if (flags == SC_BURST_POLL_FIRST)
621                 __atomic_store_n(&start, 1, __ATOMIC_RELAXED);
622         else
623                 __atomic_store_n(&start, 0, __ATOMIC_RELAXED);
624
625         /* start polling thread
626          * if in POLL_FIRST mode, poll once launched;
627          * otherwise, not actually poll yet
628          */
629         rte_eal_remote_launch(poll_burst,
630                               (void *)&pkt_per_port, lcore);
631
632         /* start xmit */
633         i = 0;
634         while (num) {
635                 nb_tx = RTE_MIN(MAX_PKT_BURST, num);
636                 portid = conf->portlist[i];
637                 nb_tx = rte_eth_tx_burst(portid, 0, &tx_burst[idx], nb_tx);
638                 idx += nb_tx;
639                 num -= nb_tx;
640                 i = (i >= conf->nb_ports - 1) ? 0 : (i + 1);
641         }
642
643         rte_delay_us(5 * US_PER_S);
644
645         /* only when polling second  */
646         if (flags == SC_BURST_XMIT_FIRST)
647                 __atomic_store_n(&start, 1, __ATOMIC_RELEASE);
648
649         /* wait for polling finished */
650         diff_tsc = rte_eal_wait_lcore(lcore);
651         if (diff_tsc < 0) {
652                 printf("exec_burst: Failed to measure cycles per packet\n");
653                 return -1;
654         }
655
656         printf("Result: %d cycles per packet\n", diff_tsc);
657
658         return 0;
659 }
660
661 static int
662 test_pmd_perf(void)
663 {
664         uint16_t nb_ports, num, nb_lcores, worker_id = (uint16_t)-1;
665         uint16_t nb_rxd = MAX_TRAFFIC_BURST;
666         uint16_t nb_txd = MAX_TRAFFIC_BURST;
667         uint16_t portid;
668         uint16_t nb_rx_queue = 1, nb_tx_queue = 1;
669         int socketid = -1;
670         int ret;
671
672         printf("Start PMD RXTX cycles cost test.\n");
673
674 #ifndef RTE_EXEC_ENV_WINDOWS
675         signal(SIGUSR1, signal_handler);
676         signal(SIGUSR2, signal_handler);
677 #endif
678
679         nb_ports = rte_eth_dev_count_avail();
680         if (nb_ports < NB_ETHPORTS_USED) {
681                 printf("At least %u port(s) used for perf. test\n",
682                        NB_ETHPORTS_USED);
683                 return -1;
684         }
685
686         nb_lcores = rte_lcore_count();
687
688         memset(lcore_conf, 0, sizeof(lcore_conf));
689         init_lcores();
690
691         init_mbufpool(NB_MBUF);
692
693         if (sc_flag == SC_CONTINUOUS) {
694                 nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
695                 nb_txd = RTE_TEST_TX_DESC_DEFAULT;
696         }
697         printf("CONFIG RXD=%d TXD=%d\n", nb_rxd, nb_txd);
698
699         reset_count();
700         num = 0;
701         RTE_ETH_FOREACH_DEV(portid) {
702                 if (socketid == -1) {
703                         socketid = rte_eth_dev_socket_id(portid);
704                         worker_id = alloc_lcore(socketid);
705                         if (worker_id == (uint16_t)-1) {
706                                 printf("No avail lcore to run test\n");
707                                 return -1;
708                         }
709                         printf("Performance test runs on lcore %u socket %u\n",
710                                worker_id, socketid);
711                 }
712
713                 if (socketid != rte_eth_dev_socket_id(portid)) {
714                         printf("Skip port %d\n", portid);
715                         continue;
716                 }
717
718                 /* port configure */
719                 ret = rte_eth_dev_configure(portid, nb_rx_queue,
720                                             nb_tx_queue, &port_conf);
721                 if (ret < 0)
722                         rte_exit(EXIT_FAILURE,
723                                 "Cannot configure device: err=%d, port=%d\n",
724                                  ret, portid);
725
726                 ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
727                 if (ret < 0)
728                         rte_exit(EXIT_FAILURE,
729                                 "Cannot get mac address: err=%d, port=%d\n",
730                                  ret, portid);
731
732                 printf("Port %u ", portid);
733                 print_ethaddr("Address:", &ports_eth_addr[portid]);
734                 printf("\n");
735
736                 /* tx queue setup */
737                 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
738                                              socketid, &tx_conf);
739                 if (ret < 0)
740                         rte_exit(EXIT_FAILURE,
741                                 "rte_eth_tx_queue_setup: err=%d, "
742                                 "port=%d\n", ret, portid);
743
744                 /* rx queue steup */
745                 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
746                                                 socketid, &rx_conf,
747                                                 mbufpool[socketid]);
748                 if (ret < 0)
749                         rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d,"
750                                  "port=%d\n", ret, portid);
751
752                 /* Start device */
753                 stop = 0;
754                 ret = rte_eth_dev_start(portid);
755                 if (ret < 0)
756                         rte_exit(EXIT_FAILURE,
757                                 "rte_eth_dev_start: err=%d, port=%d\n",
758                                 ret, portid);
759
760                 /* always enable promiscuous */
761                 ret = rte_eth_promiscuous_enable(portid);
762                 if (ret != 0)
763                         rte_exit(EXIT_FAILURE,
764                                  "rte_eth_promiscuous_enable: err=%s, port=%d\n",
765                                  rte_strerror(-ret), portid);
766
767                 lcore_conf[worker_id].portlist[num++] = portid;
768                 lcore_conf[worker_id].nb_ports++;
769         }
770         check_all_ports_link_status(nb_ports, RTE_PORT_ALL);
771
772         if (tx_burst == NULL) {
773                 tx_burst = (struct rte_mbuf **)
774                         rte_calloc_socket("tx_buff",
775                                           MAX_TRAFFIC_BURST * nb_ports,
776                                           sizeof(void *),
777                                           RTE_CACHE_LINE_SIZE, socketid);
778                 if (!tx_burst)
779                         return -1;
780         }
781
782         init_traffic(mbufpool[socketid],
783                      tx_burst, MAX_TRAFFIC_BURST * nb_ports);
784
785         printf("Generate %d packets @socket %d\n",
786                MAX_TRAFFIC_BURST * nb_ports, socketid);
787
788         if (sc_flag == SC_CONTINUOUS) {
789                 /* do both rxtx by default */
790                 if (NULL == do_measure)
791                         do_measure = measure_rxtx;
792
793                 rte_eal_remote_launch(main_loop, NULL, worker_id);
794
795                 if (rte_eal_wait_lcore(worker_id) < 0)
796                         return -1;
797         } else if (sc_flag == SC_BURST_POLL_FIRST ||
798                    sc_flag == SC_BURST_XMIT_FIRST)
799                 if (exec_burst(sc_flag, worker_id) < 0)
800                         return -1;
801
802         /* port tear down */
803         RTE_ETH_FOREACH_DEV(portid) {
804                 if (socketid != rte_eth_dev_socket_id(portid))
805                         continue;
806
807                 ret = rte_eth_dev_stop(portid);
808                 if (ret != 0)
809                         printf("rte_eth_dev_stop: err=%s, port=%u\n",
810                                rte_strerror(-ret), portid);
811         }
812
813         return 0;
814 }
815
816 int
817 test_set_rxtx_conf(cmdline_fixed_string_t mode)
818 {
819         printf("mode switch to %s\n", mode);
820
821         if (!strcmp(mode, "vector")) {
822                 /* vector rx, tx */
823                 tx_conf.tx_rs_thresh = 32;
824                 tx_conf.tx_free_thresh = 32;
825                 return 0;
826         } else if (!strcmp(mode, "scalar")) {
827                 /* bulk alloc rx, full-featured tx */
828                 tx_conf.tx_rs_thresh = 32;
829                 tx_conf.tx_free_thresh = 32;
830                 port_conf.rxmode.offloads |= RTE_ETH_RX_OFFLOAD_CHECKSUM;
831                 return 0;
832         } else if (!strcmp(mode, "hybrid")) {
833                 /* bulk alloc rx, vector tx
834                  * when vec macro not define,
835                  * using the same rx/tx as scalar
836                  */
837                 tx_conf.tx_rs_thresh = 32;
838                 tx_conf.tx_free_thresh = 32;
839                 port_conf.rxmode.offloads |= RTE_ETH_RX_OFFLOAD_CHECKSUM;
840                 return 0;
841         } else if (!strcmp(mode, "full")) {
842                 /* full feature rx,tx pair */
843                 tx_conf.tx_rs_thresh = 32;
844                 tx_conf.tx_free_thresh = 32;
845                 port_conf.rxmode.offloads |= RTE_ETH_RX_OFFLOAD_SCATTER;
846                 return 0;
847         }
848
849         return -1;
850 }
851
852 int
853 test_set_rxtx_anchor(cmdline_fixed_string_t type)
854 {
855         printf("type switch to %s\n", type);
856
857         if (!strcmp(type, "rxtx")) {
858                 do_measure = measure_rxtx;
859                 return 0;
860         } else if (!strcmp(type, "rxonly")) {
861                 do_measure = measure_rxonly;
862                 return 0;
863         } else if (!strcmp(type, "txonly")) {
864                 do_measure = measure_txonly;
865                 return 0;
866         }
867
868         return -1;
869 }
870
871 int
872 test_set_rxtx_sc(cmdline_fixed_string_t type)
873 {
874         printf("stream control switch to %s\n", type);
875
876         if (!strcmp(type, "continuous")) {
877                 sc_flag = SC_CONTINUOUS;
878                 return 0;
879         } else if (!strcmp(type, "poll_before_xmit")) {
880                 sc_flag = SC_BURST_POLL_FIRST;
881                 return 0;
882         } else if (!strcmp(type, "poll_after_xmit")) {
883                 sc_flag = SC_BURST_XMIT_FIRST;
884                 return 0;
885         }
886
887         return -1;
888 }
889
890 REGISTER_TEST_COMMAND(pmd_perf_autotest, test_pmd_perf);