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