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