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