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