New upstream version 18.08
[deb_dpdk.git] / test / 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 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 = ETHER_MAX_LEN,
67                 .split_hdr_size = 0,
68                 .offloads = DEV_RX_OFFLOAD_CRC_STRIP,
69         },
70         .txmode = {
71                 .mq_mode = ETH_MQ_TX_NONE,
72         },
73         .lpbk_mode = 1,  /* enable loopback */
74 };
75
76 static struct rte_eth_rxconf rx_conf = {
77         .rx_thresh = {
78                 .pthresh = RX_PTHRESH,
79                 .hthresh = RX_HTHRESH,
80                 .wthresh = RX_WTHRESH,
81         },
82         .rx_free_thresh = 32,
83 };
84
85 static struct rte_eth_txconf tx_conf = {
86         .tx_thresh = {
87                 .pthresh = TX_PTHRESH,
88                 .hthresh = TX_HTHRESH,
89                 .wthresh = TX_WTHRESH,
90         },
91         .tx_free_thresh = 32, /* Use PMD default values */
92         .tx_rs_thresh = 32, /* Use PMD default values */
93 };
94
95 enum {
96         LCORE_INVALID = 0,
97         LCORE_AVAIL,
98         LCORE_USED,
99 };
100
101 struct lcore_conf {
102         uint8_t status;
103         uint8_t socketid;
104         uint16_t nb_ports;
105         uint16_t portlist[RTE_MAX_ETHPORTS];
106 } __rte_cache_aligned;
107
108 struct lcore_conf lcore_conf[RTE_MAX_LCORE];
109
110 static uint64_t link_mbps;
111
112 enum {
113         SC_CONTINUOUS = 0,
114         SC_BURST_POLL_FIRST,
115         SC_BURST_XMIT_FIRST,
116 };
117
118 static uint32_t sc_flag;
119
120 /* Check the link status of all ports in up to 3s, and print them finally */
121 static void
122 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
123 {
124 #define CHECK_INTERVAL 100 /* 100ms */
125 #define MAX_CHECK_TIME 30 /* 3s (30 * 100ms) in total */
126         uint16_t portid;
127         uint8_t count, all_ports_up, print_flag = 0;
128         struct rte_eth_link link;
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                         rte_eth_link_get_nowait(portid, &link);
139                         /* print link status if flag set */
140                         if (print_flag == 1) {
141                                 if (link.link_status) {
142                                         printf(
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"));
147                                         if (link_mbps == 0)
148                                                 link_mbps = link.link_speed;
149                                 } else
150                                         printf("Port %d Link Down\n", portid);
151                                 continue;
152                         }
153                         /* clear all_ports_up flag if any link down */
154                         if (link.link_status == ETH_LINK_DOWN) {
155                                 all_ports_up = 0;
156                                 break;
157                         }
158                 }
159                 /* after finally printing all link status, get out */
160                 if (print_flag == 1)
161                         break;
162
163                 if (all_ports_up == 0) {
164                         fflush(stdout);
165                         rte_delay_ms(CHECK_INTERVAL);
166                 }
167
168                 /* set the print_flag if all ports up or timeout */
169                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1))
170                         print_flag = 1;
171         }
172 }
173
174 static void
175 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
176 {
177         char buf[ETHER_ADDR_FMT_SIZE];
178         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
179         printf("%s%s", name, buf);
180 }
181
182 static int
183 init_traffic(struct rte_mempool *mp,
184              struct rte_mbuf **pkts_burst, uint32_t burst_size)
185 {
186         struct ether_hdr pkt_eth_hdr;
187         struct ipv4_hdr pkt_ipv4_hdr;
188         struct udp_hdr pkt_udp_hdr;
189         uint32_t pktlen;
190         static uint8_t src_mac[] = { 0x00, 0xFF, 0xAA, 0xFF, 0xAA, 0xFF };
191         static uint8_t dst_mac[] = { 0x00, 0xAA, 0xFF, 0xAA, 0xFF, 0xAA };
192
193
194         initialize_eth_header(&pkt_eth_hdr,
195                 (struct ether_addr *)src_mac,
196                 (struct ether_addr *)dst_mac, ETHER_TYPE_IPv4, 0, 0);
197
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);
202
203         pktlen = initialize_udp_header(&pkt_udp_hdr, 0, 0, 18);
204
205         printf("UDP pktlen %u\n", pktlen);
206
207         return generate_packet_burst(mp, pkts_burst, &pkt_eth_hdr,
208                                      0, &pkt_ipv4_hdr, 1,
209                                      &pkt_udp_hdr, burst_size,
210                                      PACKET_BURST_GEN_PKT_LEN, 1);
211 }
212
213 static int
214 init_lcores(void)
215 {
216         unsigned lcore_id;
217
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;
223                         continue;
224                 } else
225                         lcore_conf[lcore_id].status = LCORE_AVAIL;
226         }
227         return 0;
228 }
229
230 static int
231 init_mbufpool(unsigned nb_mbuf)
232 {
233         int socketid;
234         unsigned lcore_id;
235         char s[64];
236
237         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
238                 if (rte_lcore_is_enabled(lcore_id) == 0)
239                         continue;
240
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);
246                 }
247                 if (mbufpool[socketid] == NULL) {
248                         snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
249                         mbufpool[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",
256                                         socketid);
257                         else
258                                 printf("Allocated mbuf pool on socket %d\n",
259                                         socketid);
260                 }
261         }
262         return 0;
263 }
264
265 static uint16_t
266 alloc_lcore(uint16_t socketid)
267 {
268         unsigned lcore_id;
269
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())
274                         continue;
275                 lcore_conf[lcore_id].status = LCORE_USED;
276                 lcore_conf[lcore_id].nb_ports = 0;
277                 return lcore_id;
278         }
279
280         return (uint16_t)-1;
281 }
282
283 static volatile uint64_t stop;
284 static uint64_t count;
285 static uint64_t drop;
286 static uint64_t idle;
287
288 static void
289 reset_count(void)
290 {
291         count = 0;
292         drop = 0;
293         idle = 0;
294 }
295
296 static void
297 stats_display(uint16_t port_id)
298 {
299         struct rte_eth_stats stats;
300         rte_eth_stats_get(port_id, &stats);
301
302         printf("  RX-packets: %-10"PRIu64" RX-missed: %-10"PRIu64" RX-bytes:  "
303                "%-"PRIu64"\n",
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:  "
308                "%-"PRIu64"\n",
309                stats.opackets, stats.oerrors, stats.obytes);
310 }
311
312 static void
313 signal_handler(int signum)
314 {
315         /*  USR1 signal, stop testing */
316         if (signum == SIGUSR1) {
317                 printf("Force Stop!\n");
318                 stop = 1;
319         }
320
321         /*  USR2 signal, print stats */
322         if (signum == SIGUSR2)
323                 stats_display(0);
324 }
325
326 struct rte_mbuf **tx_burst;
327
328 uint64_t (*do_measure)(struct lcore_conf *conf,
329                        struct rte_mbuf *pkts_burst[],
330                        uint64_t total_pkts);
331
332 static uint64_t
333 measure_rxtx(struct lcore_conf *conf,
334              struct rte_mbuf *pkts_burst[],
335              uint64_t total_pkts)
336 {
337         unsigned i, portid, nb_rx, nb_tx;
338         uint64_t prev_tsc, cur_tsc;
339
340         prev_tsc = rte_rdtsc();
341
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)) {
348                                 idle++;
349                                 continue;
350                         }
351
352                         count += nb_rx;
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);
356                                 do {
357                                         rte_pktmbuf_free(pkts_burst[nb_tx]);
358                                 } while (++nb_tx < nb_rx);
359                         }
360                 }
361                 if (unlikely(count >= total_pkts))
362                         break;
363         }
364
365         cur_tsc = rte_rdtsc();
366
367         return cur_tsc - prev_tsc;
368 }
369
370 static uint64_t
371 measure_rxonly(struct lcore_conf *conf,
372                struct rte_mbuf *pkts_burst[],
373                uint64_t total_pkts)
374 {
375         unsigned i, portid, nb_rx, nb_tx;
376         uint64_t diff_tsc, cur_tsc;
377
378         diff_tsc = 0;
379         while (likely(!stop)) {
380                 for (i = 0; i < conf->nb_ports; i++) {
381                         portid = conf->portlist[i];
382
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)) {
387                                 idle++;
388                                 continue;
389                         }
390                         diff_tsc += rte_rdtsc() - cur_tsc;
391
392                         count += nb_rx;
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);
396                                 do {
397                                         rte_pktmbuf_free(pkts_burst[nb_tx]);
398                                 } while (++nb_tx < nb_rx);
399                         }
400                 }
401                 if (unlikely(count >= total_pkts))
402                         break;
403         }
404
405         return diff_tsc;
406 }
407
408 static uint64_t
409 measure_txonly(struct lcore_conf *conf,
410                struct rte_mbuf *pkts_burst[],
411                uint64_t total_pkts)
412 {
413         unsigned i, portid, nb_rx, nb_tx;
414         uint64_t diff_tsc, cur_tsc;
415
416         printf("do tx measure\n");
417         diff_tsc = 0;
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)) {
424                                 idle++;
425                                 continue;
426                         }
427
428                         count += nb_rx;
429
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);
434                                 do {
435                                         rte_pktmbuf_free(pkts_burst[nb_tx]);
436                                 } while (++nb_tx < nb_rx);
437                         }
438                         diff_tsc += rte_rdtsc() - cur_tsc;
439                 }
440                 if (unlikely(count >= total_pkts))
441                         break;
442         }
443
444         return diff_tsc;
445 }
446
447 /* main processing loop */
448 static int
449 main_loop(__rte_unused void *args)
450 {
451 #define PACKET_SIZE 64
452 #define FRAME_GAP 12
453 #define MAC_PREAMBLE 8
454         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
455         unsigned lcore_id;
456         unsigned i, portid, nb_rx = 0, nb_tx = 0;
457         struct lcore_conf *conf;
458         int pkt_per_port;
459         uint64_t diff_tsc;
460         uint64_t packets_per_second, total_packets;
461
462         lcore_id = rte_lcore_id();
463         conf = &lcore_conf[lcore_id];
464         if (conf->status != LCORE_USED)
465                 return 0;
466
467         pkt_per_port = MAX_TRAFFIC_BURST;
468
469         int idx = 0;
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);
474                 while (num) {
475                         nb_tx = RTE_MIN(MAX_PKT_BURST, num);
476                         nb_tx = rte_eth_tx_burst(portid, 0,
477                                                 &tx_burst[idx], nb_tx);
478                         num -= nb_tx;
479                         idx += nb_tx;
480                 }
481         }
482         printf("Total packets inject to prime ports = %u\n", idx);
483
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",
487                packets_per_second);
488
489         total_packets = RTE_TEST_DURATION * conf->nb_ports * packets_per_second;
490         printf("Test will stop after at least %"PRIu64" packets received\n",
491                 + total_packets);
492
493         diff_tsc = do_measure(conf, pkts_burst, total_packets);
494
495         for (i = 0; i < conf->nb_ports; i++) {
496                 portid = conf->portlist[i];
497                 int nb_free = pkt_per_port;
498                 do { /* dry out */
499                         nb_rx = rte_eth_rx_burst(portid, 0,
500                                                  pkts_burst, MAX_PKT_BURST);
501                         nb_tx = 0;
502                         while (nb_tx < nb_rx)
503                                 rte_pktmbuf_free(pkts_burst[nb_tx++]);
504                         nb_free -= nb_rx;
505                 } while (nb_free != 0);
506                 printf("free %d mbuf left in port %u\n", pkt_per_port, portid);
507         }
508
509         if (count == 0)
510                 return -1;
511
512         printf("%"PRIu64" packet, %"PRIu64" drop, %"PRIu64" idle\n",
513                count, drop, idle);
514         printf("Result: %"PRIu64" cycles per packet\n", diff_tsc / count);
515
516         return 0;
517 }
518
519 static rte_atomic64_t start;
520
521 static inline int
522 poll_burst(void *args)
523 {
524 #define MAX_IDLE           (10000)
525         unsigned lcore_id;
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;
532         uint64_t total;
533         uint64_t timeout = MAX_IDLE;
534         int num[RTE_MAX_ETHPORTS];
535
536         lcore_id = rte_lcore_id();
537         conf = &lcore_conf[lcore_id];
538         if (conf->status != LCORE_USED)
539                 return 0;
540
541         total = pkt_per_port * conf->nb_ports;
542         printf("start to receive total expect %"PRIu64"\n", total);
543
544         pkts_burst = (struct rte_mbuf **)
545                 rte_calloc_socket("poll_burst",
546                                   total, sizeof(void *),
547                                   RTE_CACHE_LINE_SIZE, conf->socketid);
548         if (!pkts_burst)
549                 return -1;
550
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;
555         }
556
557         while (!rte_atomic64_read(&start))
558                 ;
559
560         cur_tsc = rte_rdtsc();
561         while (total) {
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)) {
568                                 timeout--;
569                                 if (unlikely(timeout == 0))
570                                         goto timeout;
571                                 continue;
572                         }
573                         next[portid] += nb_rx;
574                         num[portid] -= nb_rx;
575                         total -= nb_rx;
576                 }
577         }
578 timeout:
579         diff_tsc = rte_rdtsc() - cur_tsc;
580
581         printf("%"PRIu64" packets lost, IDLE %"PRIu64" times\n",
582                total, MAX_IDLE - timeout);
583         /* clean up */
584         total = pkt_per_port * conf->nb_ports - total;
585         for (i = 0; i < total; i++)
586                 rte_pktmbuf_free(pkts_burst[i]);
587
588         rte_free(pkts_burst);
589
590         if (total > 0)
591                 return diff_tsc / total;
592         else
593                 return -1;
594 }
595
596 static int
597 exec_burst(uint32_t flags, int lcore)
598 {
599         unsigned i, portid, nb_tx = 0;
600         struct lcore_conf *conf;
601         uint32_t pkt_per_port;
602         int num, idx = 0;
603         int diff_tsc;
604
605         conf = &lcore_conf[lcore];
606
607         pkt_per_port = MAX_TRAFFIC_BURST;
608         num = pkt_per_port * conf->nb_ports;
609
610         rte_atomic64_init(&start);
611
612         /* start polling thread, but not actually poll yet */
613         rte_eal_remote_launch(poll_burst,
614                               (void *)&pkt_per_port, lcore);
615
616         /* Only when polling first */
617         if (flags == SC_BURST_POLL_FIRST)
618                 rte_atomic64_set(&start, 1);
619
620         /* start xmit */
621         while (num) {
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);
627                         idx += nb_tx;
628                         num -= nb_tx;
629                 }
630
631         }
632
633         sleep(5);
634
635         /* only when polling second  */
636         if (flags == SC_BURST_XMIT_FIRST)
637                 rte_atomic64_set(&start, 1);
638
639         /* wait for polling finished */
640         diff_tsc = rte_eal_wait_lcore(lcore);
641         if (diff_tsc < 0) {
642                 printf("exec_burst: Failed to measure cycles per packet\n");
643                 return -1;
644         }
645
646         printf("Result: %d cycles per packet\n", diff_tsc);
647
648         return 0;
649 }
650
651 static int
652 test_pmd_perf(void)
653 {
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;
657         uint16_t portid;
658         uint16_t nb_rx_queue = 1, nb_tx_queue = 1;
659         int socketid = -1;
660         int ret;
661
662         printf("Start PMD RXTX cycles cost test.\n");
663
664         signal(SIGUSR1, signal_handler);
665         signal(SIGUSR2, signal_handler);
666
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",
670                        NB_ETHPORTS_USED);
671                 return -1;
672         }
673
674         nb_lcores = rte_lcore_count();
675
676         memset(lcore_conf, 0, sizeof(lcore_conf));
677         init_lcores();
678
679         init_mbufpool(NB_MBUF);
680
681         if (sc_flag == SC_CONTINUOUS) {
682                 nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
683                 nb_txd = RTE_TEST_TX_DESC_DEFAULT;
684         }
685         printf("CONFIG RXD=%d TXD=%d\n", nb_rxd, nb_txd);
686
687         reset_count();
688         num = 0;
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");
695                                 return -1;
696                         }
697                         printf("Performance test runs on lcore %u socket %u\n",
698                                slave_id, socketid);
699                 }
700
701                 if (socketid != rte_eth_dev_socket_id(portid)) {
702                         printf("Skip port %d\n", portid);
703                         continue;
704                 }
705
706                 /* port configure */
707                 ret = rte_eth_dev_configure(portid, nb_rx_queue,
708                                             nb_tx_queue, &port_conf);
709                 if (ret < 0)
710                         rte_exit(EXIT_FAILURE,
711                                 "Cannot configure device: err=%d, port=%d\n",
712                                  ret, portid);
713
714                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
715                 printf("Port %u ", portid);
716                 print_ethaddr("Address:", &ports_eth_addr[portid]);
717                 printf("\n");
718
719                 /* tx queue setup */
720                 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
721                                              socketid, &tx_conf);
722                 if (ret < 0)
723                         rte_exit(EXIT_FAILURE,
724                                 "rte_eth_tx_queue_setup: err=%d, "
725                                 "port=%d\n", ret, portid);
726
727                 /* rx queue steup */
728                 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
729                                                 socketid, &rx_conf,
730                                                 mbufpool[socketid]);
731                 if (ret < 0)
732                         rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d,"
733                                  "port=%d\n", ret, portid);
734
735                 /* Start device */
736                 stop = 0;
737                 ret = rte_eth_dev_start(portid);
738                 if (ret < 0)
739                         rte_exit(EXIT_FAILURE,
740                                 "rte_eth_dev_start: err=%d, port=%d\n",
741                                 ret, portid);
742
743                 /* always eanble promiscuous */
744                 rte_eth_promiscuous_enable(portid);
745
746                 lcore_conf[slave_id].portlist[num++] = portid;
747                 lcore_conf[slave_id].nb_ports++;
748         }
749         check_all_ports_link_status(nb_ports, RTE_PORT_ALL);
750
751         if (tx_burst == NULL) {
752                 tx_burst = (struct rte_mbuf **)
753                         rte_calloc_socket("tx_buff",
754                                           MAX_TRAFFIC_BURST * nb_ports,
755                                           sizeof(void *),
756                                           RTE_CACHE_LINE_SIZE, socketid);
757                 if (!tx_burst)
758                         return -1;
759         }
760
761         init_traffic(mbufpool[socketid],
762                      tx_burst, MAX_TRAFFIC_BURST * nb_ports);
763
764         printf("Generate %d packets @socket %d\n",
765                MAX_TRAFFIC_BURST * nb_ports, socketid);
766
767         if (sc_flag == SC_CONTINUOUS) {
768                 /* do both rxtx by default */
769                 if (NULL == do_measure)
770                         do_measure = measure_rxtx;
771
772                 rte_eal_remote_launch(main_loop, NULL, slave_id);
773
774                 if (rte_eal_wait_lcore(slave_id) < 0)
775                         return -1;
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)
779                         return -1;
780
781         /* port tear down */
782         RTE_ETH_FOREACH_DEV(portid) {
783                 if (socketid != rte_eth_dev_socket_id(portid))
784                         continue;
785
786                 rte_eth_dev_stop(portid);
787         }
788
789         return 0;
790 }
791
792 int
793 test_set_rxtx_conf(cmdline_fixed_string_t mode)
794 {
795         printf("mode switch to %s\n", mode);
796
797         if (!strcmp(mode, "vector")) {
798                 /* vector rx, tx */
799                 tx_conf.tx_rs_thresh = 32;
800                 tx_conf.tx_free_thresh = 32;
801                 return 0;
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;
807                 return 0;
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
812                  */
813                 tx_conf.tx_rs_thresh = 32;
814                 tx_conf.tx_free_thresh = 32;
815                 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_CHECKSUM;
816                 return 0;
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;
822                 return 0;
823         }
824
825         return -1;
826 }
827
828 int
829 test_set_rxtx_anchor(cmdline_fixed_string_t type)
830 {
831         printf("type switch to %s\n", type);
832
833         if (!strcmp(type, "rxtx")) {
834                 do_measure = measure_rxtx;
835                 return 0;
836         } else if (!strcmp(type, "rxonly")) {
837                 do_measure = measure_rxonly;
838                 return 0;
839         } else if (!strcmp(type, "txonly")) {
840                 do_measure = measure_txonly;
841                 return 0;
842         }
843
844         return -1;
845 }
846
847 int
848 test_set_rxtx_sc(cmdline_fixed_string_t type)
849 {
850         printf("stream control switch to %s\n", type);
851
852         if (!strcmp(type, "continuous")) {
853                 sc_flag = SC_CONTINUOUS;
854                 return 0;
855         } else if (!strcmp(type, "poll_before_xmit")) {
856                 sc_flag = SC_BURST_POLL_FIRST;
857                 return 0;
858         } else if (!strcmp(type, "poll_after_xmit")) {
859                 sc_flag = SC_BURST_XMIT_FIRST;
860                 return 0;
861         }
862
863         return -1;
864 }
865
866 REGISTER_TEST_COMMAND(pmd_perf_autotest, test_pmd_perf);