app/testpmd: add stats per queue
[dpdk.git] / app / test-pmd / testpmd.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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 <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <signal.h>
39 #include <string.h>
40 #include <time.h>
41 #include <fcntl.h>
42 #include <sys/types.h>
43 #include <errno.h>
44
45 #include <sys/queue.h>
46 #include <sys/stat.h>
47
48 #include <stdint.h>
49 #include <unistd.h>
50 #include <inttypes.h>
51
52 #include <rte_common.h>
53 #include <rte_byteorder.h>
54 #include <rte_log.h>
55 #include <rte_debug.h>
56 #include <rte_cycles.h>
57 #include <rte_memory.h>
58 #include <rte_memcpy.h>
59 #include <rte_memzone.h>
60 #include <rte_launch.h>
61 #include <rte_tailq.h>
62 #include <rte_eal.h>
63 #include <rte_per_lcore.h>
64 #include <rte_lcore.h>
65 #include <rte_atomic.h>
66 #include <rte_branch_prediction.h>
67 #include <rte_ring.h>
68 #include <rte_mempool.h>
69 #include <rte_malloc.h>
70 #include <rte_mbuf.h>
71 #include <rte_interrupts.h>
72 #include <rte_pci.h>
73 #include <rte_ether.h>
74 #include <rte_ethdev.h>
75 #include <rte_string_fns.h>
76
77 #include "testpmd.h"
78
79 uint16_t verbose_level = 0; /**< Silent by default. */
80
81 /* use master core for command line ? */
82 uint8_t interactive = 0;
83
84 /*
85  * NUMA support configuration.
86  * When set, the NUMA support attempts to dispatch the allocation of the
87  * RX and TX memory rings, and of the DMA memory buffers (mbufs) for the
88  * probed ports among the CPU sockets 0 and 1.
89  * Otherwise, all memory is allocated from CPU socket 0.
90  */
91 uint8_t numa_support = 0; /**< No numa support by default */
92
93 /*
94  * Record the Ethernet address of peer target ports to which packets are
95  * forwarded.
96  * Must be instanciated with the ethernet addresses of peer traffic generator
97  * ports.
98  */
99 struct ether_addr peer_eth_addrs[RTE_MAX_ETHPORTS];
100 portid_t nb_peer_eth_addrs = 0;
101
102 /*
103  * Probed Target Environment.
104  */
105 struct rte_port *ports;        /**< For all probed ethernet ports. */
106 portid_t nb_ports;             /**< Number of probed ethernet ports. */
107 struct fwd_lcore **fwd_lcores; /**< For all probed logical cores. */
108 lcoreid_t nb_lcores;           /**< Number of probed logical cores. */
109
110 /*
111  * Test Forwarding Configuration.
112  *    nb_fwd_lcores <= nb_cfg_lcores <= nb_lcores
113  *    nb_fwd_ports  <= nb_cfg_ports  <= nb_ports
114  */
115 lcoreid_t nb_cfg_lcores; /**< Number of configured logical cores. */
116 lcoreid_t nb_fwd_lcores; /**< Number of forwarding logical cores. */
117 portid_t  nb_cfg_ports;  /**< Number of configured ports. */
118 portid_t  nb_fwd_ports;  /**< Number of forwarding ports. */
119
120 unsigned int fwd_lcores_cpuids[RTE_MAX_LCORE]; /**< CPU ids configuration. */
121 portid_t fwd_ports_ids[RTE_MAX_ETHPORTS];      /**< Port ids configuration. */
122
123 struct fwd_stream **fwd_streams; /**< For each RX queue of each port. */
124 streamid_t nb_fwd_streams;       /**< Is equal to (nb_ports * nb_rxq). */
125
126 /*
127  * Forwarding engines.
128  */
129 struct fwd_engine * fwd_engines[] = {
130         &io_fwd_engine,
131         &mac_fwd_engine,
132         &rx_only_engine,
133         &tx_only_engine,
134         &csum_fwd_engine,
135 #ifdef RTE_LIBRTE_IEEE1588
136         &ieee1588_fwd_engine,
137 #endif
138         NULL,
139 };
140
141 struct fwd_config cur_fwd_config;
142 struct fwd_engine *cur_fwd_eng = &io_fwd_engine; /**< IO mode by default. */
143
144 uint16_t mbuf_data_size = DEFAULT_MBUF_DATA_SIZE; /**< Mbuf data space size. */
145
146 /*
147  * Configuration of packet segments used by the "txonly" processing engine.
148  */
149 uint16_t tx_pkt_length = TXONLY_DEF_PACKET_LEN; /**< TXONLY packet length. */
150 uint16_t tx_pkt_seg_lengths[RTE_MAX_SEGS_PER_PKT] = {
151         TXONLY_DEF_PACKET_LEN,
152 };
153 uint8_t  tx_pkt_nb_segs = 1; /**< Number of segments in TXONLY packets */
154
155 uint16_t nb_pkt_per_burst = DEF_PKT_BURST; /**< Number of packets per burst. */
156 uint16_t mb_mempool_cache = DEF_PKT_BURST; /**< Size of mbuf mempool cache. */
157
158 /*
159  * Ethernet Ports Configuration.
160  */
161 int promiscuous_on = 1; /**< Ports set in promiscuous mode by default. */
162
163 /*
164  * Configurable number of RX/TX queues.
165  */
166 queueid_t nb_rxq = 1; /**< Number of RX queues per port. */
167 queueid_t nb_txq = 1; /**< Number of TX queues per port. */
168
169 /*
170  * Configurable number of RX/TX ring descriptors.
171  */
172 #define RTE_TEST_RX_DESC_DEFAULT 128
173 #define RTE_TEST_TX_DESC_DEFAULT 512
174 uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT; /**< Number of RX descriptors. */
175 uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT; /**< Number of TX descriptors. */
176
177 /*
178  * Configurable values of RX and TX ring threshold registers.
179  */
180 #define RX_PTHRESH 8 /**< Default value of RX prefetch threshold register. */
181 #define RX_HTHRESH 8 /**< Default value of RX host threshold register. */
182 #define RX_WTHRESH 4 /**< Default value of RX write-back threshold register. */
183
184 #define TX_PTHRESH 36 /**< Default value of TX prefetch threshold register. */
185 #define TX_HTHRESH 0 /**< Default value of TX host threshold register. */
186 #define TX_WTHRESH 0 /**< Default value of TX write-back threshold register. */
187
188 struct rte_eth_thresh rx_thresh = {
189         .pthresh = RX_PTHRESH,
190         .hthresh = RX_HTHRESH,
191         .wthresh = RX_WTHRESH,
192 };
193
194 struct rte_eth_thresh tx_thresh = {
195         .pthresh = TX_PTHRESH,
196         .hthresh = TX_HTHRESH,
197         .wthresh = TX_WTHRESH,
198 };
199
200 /*
201  * Configurable value of RX free threshold.
202  */
203 uint16_t rx_free_thresh = 0; /* Immediately free RX descriptors by default. */
204
205 /*
206  * Configurable value of TX free threshold.
207  */
208 uint16_t tx_free_thresh = 0; /* Use default values. */
209
210 /*
211  * Configurable value of TX RS bit threshold.
212  */
213 uint16_t tx_rs_thresh = 0; /* Use default values. */
214
215 /*
216  * Receive Side Scaling (RSS) configuration.
217  */
218 uint16_t rss_hf = ETH_RSS_IPV4 | ETH_RSS_IPV6; /* RSS IP by default. */
219
220 /*
221  * Port topology configuration
222  */
223 uint16_t port_topology = PORT_TOPOLOGY_PAIRED; /* Ports are paired by default */
224
225 /*
226  * Ethernet device configuration.
227  */
228 struct rte_eth_rxmode rx_mode = {
229         .max_rx_pkt_len = ETHER_MAX_LEN, /**< Default maximum frame length. */
230         .split_hdr_size = 0,
231         .header_split   = 0, /**< Header Split disabled. */
232         .hw_ip_checksum = 0, /**< IP checksum offload disabled. */
233         .hw_vlan_filter = 1, /**< VLAN filtering enabled. */
234         .jumbo_frame    = 0, /**< Jumbo Frame Support disabled. */
235         .hw_strip_crc   = 0, /**< CRC stripping by hardware disabled. */
236 };
237
238 struct rte_fdir_conf fdir_conf = {
239         .mode = RTE_FDIR_MODE_NONE,
240         .pballoc = RTE_FDIR_PBALLOC_64K,
241         .status = RTE_FDIR_REPORT_STATUS,
242         .flexbytes_offset = 0x6,
243         .drop_queue = 127,
244 };
245
246 static volatile int test_done = 1; /* stop packet forwarding when set to 1. */
247
248 struct queue_stats_mappings tx_queue_stats_mappings_array[MAX_TX_QUEUE_STATS_MAPPINGS];
249 struct queue_stats_mappings rx_queue_stats_mappings_array[MAX_RX_QUEUE_STATS_MAPPINGS];
250
251 struct queue_stats_mappings *tx_queue_stats_mappings = tx_queue_stats_mappings_array;
252 struct queue_stats_mappings *rx_queue_stats_mappings = rx_queue_stats_mappings_array;
253
254 uint16_t nb_tx_queue_stats_mappings = 0;
255 uint16_t nb_rx_queue_stats_mappings = 0;
256
257 /* Forward function declarations */
258 static void map_port_queue_stats_mapping_registers(uint8_t pi, struct rte_port *port);
259
260 /*
261  * Setup default configuration.
262  */
263 static void
264 set_default_fwd_lcores_config(void)
265 {
266         unsigned int i;
267         unsigned int nb_lc;
268
269         nb_lc = 0;
270         for (i = 0; i < RTE_MAX_LCORE; i++) {
271                 if (! rte_lcore_is_enabled(i))
272                         continue;
273                 if (i == rte_get_master_lcore())
274                         continue;
275                 fwd_lcores_cpuids[nb_lc++] = i;
276         }
277         nb_lcores = (lcoreid_t) nb_lc;
278         nb_cfg_lcores = nb_lcores;
279         nb_fwd_lcores = 1;
280 }
281
282 static void
283 set_def_peer_eth_addrs(void)
284 {
285         portid_t i;
286
287         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
288                 peer_eth_addrs[i].addr_bytes[0] = ETHER_LOCAL_ADMIN_ADDR;
289                 peer_eth_addrs[i].addr_bytes[5] = i;
290         }
291 }
292
293 static void
294 set_default_fwd_ports_config(void)
295 {
296         portid_t pt_id;
297
298         for (pt_id = 0; pt_id < nb_ports; pt_id++)
299                 fwd_ports_ids[pt_id] = pt_id;
300
301         nb_cfg_ports = nb_ports;
302         nb_fwd_ports = nb_ports;
303 }
304
305 void
306 set_def_fwd_config(void)
307 {
308         set_default_fwd_lcores_config();
309         set_def_peer_eth_addrs();
310         set_default_fwd_ports_config();
311 }
312
313 /*
314  * Configuration initialisation done once at init time.
315  */
316 struct mbuf_ctor_arg {
317         uint16_t seg_buf_offset; /**< offset of data in data segment of mbuf. */
318         uint16_t seg_buf_size;   /**< size of data segment in mbuf. */
319 };
320
321 struct mbuf_pool_ctor_arg {
322         uint16_t seg_buf_size; /**< size of data segment in mbuf. */
323 };
324
325 static void
326 testpmd_mbuf_ctor(struct rte_mempool *mp,
327                   void *opaque_arg,
328                   void *raw_mbuf,
329                   __attribute__((unused)) unsigned i)
330 {
331         struct mbuf_ctor_arg *mb_ctor_arg;
332         struct rte_mbuf    *mb;
333
334         mb_ctor_arg = (struct mbuf_ctor_arg *) opaque_arg;
335         mb = (struct rte_mbuf *) raw_mbuf;
336
337         mb->pool         = mp;
338         mb->buf_addr     = (void *) ((char *)mb + mb_ctor_arg->seg_buf_offset);
339         mb->buf_physaddr = (uint64_t) (rte_mempool_virt2phy(mp, mb) +
340                         mb_ctor_arg->seg_buf_offset);
341         mb->buf_len      = mb_ctor_arg->seg_buf_size;
342         mb->type         = RTE_MBUF_PKT;
343         mb->ol_flags     = 0;
344         mb->pkt.data     = (char *) mb->buf_addr + RTE_PKTMBUF_HEADROOM;
345         mb->pkt.nb_segs  = 1;
346         mb->pkt.vlan_macip.data = 0;
347         mb->pkt.hash.rss = 0;
348 }
349
350 static void
351 testpmd_mbuf_pool_ctor(struct rte_mempool *mp,
352                        void *opaque_arg)
353 {
354         struct mbuf_pool_ctor_arg      *mbp_ctor_arg;
355         struct rte_pktmbuf_pool_private *mbp_priv;
356
357         if (mp->private_data_size < sizeof(struct rte_pktmbuf_pool_private)) {
358                 printf("%s(%s) private_data_size %d < %d\n",
359                        __func__, mp->name, (int) mp->private_data_size,
360                        (int) sizeof(struct rte_pktmbuf_pool_private));
361                 return;
362         }
363         mbp_ctor_arg = (struct mbuf_pool_ctor_arg *) opaque_arg;
364         mbp_priv = (struct rte_pktmbuf_pool_private *)
365                 ((char *)mp + sizeof(struct rte_mempool));
366         mbp_priv->mbuf_data_room_size = mbp_ctor_arg->seg_buf_size;
367 }
368
369 static void
370 mbuf_pool_create(uint16_t mbuf_seg_size, unsigned nb_mbuf,
371                  unsigned int socket_id)
372 {
373         char pool_name[RTE_MEMPOOL_NAMESIZE];
374         struct rte_mempool *rte_mp;
375         struct mbuf_pool_ctor_arg mbp_ctor_arg;
376         struct mbuf_ctor_arg mb_ctor_arg;
377         uint32_t mb_size;
378
379         mbp_ctor_arg.seg_buf_size = (uint16_t) (RTE_PKTMBUF_HEADROOM +
380                                                 mbuf_seg_size);
381         mb_ctor_arg.seg_buf_offset =
382                 (uint16_t) CACHE_LINE_ROUNDUP(sizeof(struct rte_mbuf));
383         mb_ctor_arg.seg_buf_size = mbp_ctor_arg.seg_buf_size;
384         mb_size = mb_ctor_arg.seg_buf_offset + mb_ctor_arg.seg_buf_size;
385         mbuf_poolname_build(socket_id, pool_name, sizeof(pool_name));
386         rte_mp = rte_mempool_create(pool_name, nb_mbuf, (unsigned) mb_size,
387                                     (unsigned) mb_mempool_cache,
388                                     sizeof(struct rte_pktmbuf_pool_private),
389                                     testpmd_mbuf_pool_ctor, &mbp_ctor_arg,
390                                     testpmd_mbuf_ctor, &mb_ctor_arg,
391                                     socket_id, 0);
392         if (rte_mp == NULL) {
393                 rte_exit(EXIT_FAILURE, "Creation of mbuf pool for socket %u failed\n",
394                        socket_id);
395         }
396 }
397
398 static void
399 init_config(void)
400 {
401         struct rte_port *port;
402         struct rte_mempool *mbp;
403         unsigned int nb_mbuf_per_pool;
404         streamid_t sm_id;
405         lcoreid_t  lc_id;
406         portid_t   pt_id;
407
408         /* Configuration of logical cores. */
409         fwd_lcores = rte_zmalloc("testpmd: fwd_lcores",
410                                 sizeof(struct fwd_lcore *) * nb_lcores,
411                                 CACHE_LINE_SIZE);
412         if (fwd_lcores == NULL) {
413                 rte_exit(EXIT_FAILURE, "rte_zmalloc(%d (struct fwd_lcore *)) failed\n",
414                        nb_lcores);
415         }
416         for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
417                 fwd_lcores[lc_id] = rte_zmalloc("testpmd: struct fwd_lcore",
418                                                sizeof(struct fwd_lcore),
419                                                CACHE_LINE_SIZE);
420                 if (fwd_lcores[lc_id] == NULL) {
421                         rte_exit(EXIT_FAILURE, "rte_zmalloc(struct fwd_lcore) failed\n");
422                 }
423                 fwd_lcores[lc_id]->cpuid_idx = lc_id;
424         }
425
426         /*
427          * Create pools of mbuf.
428          * If NUMA support is disabled, create a single pool of mbuf in
429          * socket 0 memory.
430          * Otherwise, create a pool of mbuf in the memory of sockets 0 and 1.
431          */
432         nb_mbuf_per_pool = nb_rxd + (nb_lcores * mb_mempool_cache) +
433                 nb_txd + MAX_PKT_BURST;
434         if (numa_support) {
435                 nb_mbuf_per_pool = nb_mbuf_per_pool * (nb_ports >> 1);
436                 mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool, 0);
437                 mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool, 1);
438         } else {
439                 nb_mbuf_per_pool = (nb_mbuf_per_pool * nb_ports);
440                 mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool, 0);
441         }
442
443         /*
444          * Records which Mbuf pool to use by each logical core, if needed.
445          */
446         for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
447                 mbp = mbuf_pool_find(rte_lcore_to_socket_id(lc_id));
448                 if (mbp == NULL)
449                         mbp = mbuf_pool_find(0);
450                 fwd_lcores[lc_id]->mbp = mbp;
451         }
452
453         /* Configuration of Ethernet ports. */
454         ports = rte_zmalloc("testpmd: ports",
455                             sizeof(struct rte_port) * nb_ports,
456                             CACHE_LINE_SIZE);
457         if (ports == NULL) {
458                 rte_exit(EXIT_FAILURE, "rte_zmalloc(%d struct rte_port) failed\n",
459                        nb_ports);
460         }
461         port = ports;
462         for (pt_id = 0; pt_id < nb_ports; pt_id++, port++) {
463                 rte_eth_dev_info_get(pt_id, &port->dev_info);
464                 if (nb_rxq > port->dev_info.max_rx_queues) {
465                         rte_exit(EXIT_FAILURE, "Port %d: max RX queues %d < nb_rxq %d\n",
466                                (int) pt_id,
467                                (int) port->dev_info.max_rx_queues,
468                                (int) nb_rxq);
469                 }
470                 if (nb_txq > port->dev_info.max_tx_queues) {
471                         rte_exit(EXIT_FAILURE, "Port %d: max TX queues %d < nb_txq %d\n",
472                                (int) pt_id,
473                                (int) port->dev_info.max_tx_queues,
474                                (int) nb_txq);
475                 }
476
477                 if (numa_support)
478                         port->socket_id = (pt_id < (nb_ports >> 1)) ? 0 : 1;
479                 else
480                         port->socket_id = 0;
481         }
482
483         /* Configuration of packet forwarding streams. */
484         nb_fwd_streams = (streamid_t) (nb_ports * nb_rxq);
485         fwd_streams = rte_zmalloc("testpmd: fwd_streams",
486                                   sizeof(struct fwd_stream *) * nb_fwd_streams,
487                                   CACHE_LINE_SIZE);
488         if (fwd_streams == NULL) {
489                 rte_exit(EXIT_FAILURE, "rte_zmalloc(%d (struct fwd_stream *)) failed\n",
490                        nb_fwd_streams);
491         }
492         for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
493                 fwd_streams[sm_id] = rte_zmalloc("testpmd: struct fwd_stream",
494                                                  sizeof(struct fwd_stream),
495                                                  CACHE_LINE_SIZE);
496                 if (fwd_streams[sm_id] == NULL) {
497                         rte_exit(EXIT_FAILURE, "rte_zmalloc(struct fwd_stream) failed\n");
498                 }
499         }
500 }
501
502 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
503 static void
504 pkt_burst_stats_display(const char *rx_tx, struct pkt_burst_stats *pbs)
505 {
506         unsigned int total_burst;
507         unsigned int nb_burst;
508         unsigned int burst_stats[3];
509         uint16_t pktnb_stats[3];
510         uint16_t nb_pkt;
511         int burst_percent[3];
512
513         /*
514          * First compute the total number of packet bursts and the
515          * two highest numbers of bursts of the same number of packets.
516          */
517         total_burst = 0;
518         burst_stats[0] = burst_stats[1] = burst_stats[2] = 0;
519         pktnb_stats[0] = pktnb_stats[1] = pktnb_stats[2] = 0;
520         for (nb_pkt = 0; nb_pkt < MAX_PKT_BURST; nb_pkt++) {
521                 nb_burst = pbs->pkt_burst_spread[nb_pkt];
522                 if (nb_burst == 0)
523                         continue;
524                 total_burst += nb_burst;
525                 if (nb_burst > burst_stats[0]) {
526                         burst_stats[1] = burst_stats[0];
527                         pktnb_stats[1] = pktnb_stats[0];
528                         burst_stats[0] = nb_burst;
529                         pktnb_stats[0] = nb_pkt;
530                 }
531         }
532         if (total_burst == 0)
533                 return;
534         burst_percent[0] = (burst_stats[0] * 100) / total_burst;
535         printf("  %s-bursts : %u [%d%% of %d pkts", rx_tx, total_burst,
536                burst_percent[0], (int) pktnb_stats[0]);
537         if (burst_stats[0] == total_burst) {
538                 printf("]\n");
539                 return;
540         }
541         if (burst_stats[0] + burst_stats[1] == total_burst) {
542                 printf(" + %d%% of %d pkts]\n",
543                        100 - burst_percent[0], pktnb_stats[1]);
544                 return;
545         }
546         burst_percent[1] = (burst_stats[1] * 100) / total_burst;
547         burst_percent[2] = 100 - (burst_percent[0] + burst_percent[1]);
548         if ((burst_percent[1] == 0) || (burst_percent[2] == 0)) {
549                 printf(" + %d%% of others]\n", 100 - burst_percent[0]);
550                 return;
551         }
552         printf(" + %d%% of %d pkts + %d%% of others]\n",
553                burst_percent[1], (int) pktnb_stats[1], burst_percent[2]);
554 }
555 #endif /* RTE_TEST_PMD_RECORD_BURST_STATS */
556
557 static void
558 fwd_port_stats_display(portid_t port_id, struct rte_eth_stats *stats)
559 {
560         struct rte_port *port;
561
562         static const char *fwd_stats_border = "----------------------";
563
564         port = &ports[port_id];
565         printf("\n  %s Forward statistics for port %-2d %s\n",
566                 fwd_stats_border, port_id, fwd_stats_border);
567         printf("  RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64"RX-total: "
568                "%-"PRIu64"\n",
569                stats->ipackets, stats->ierrors,
570                (uint64_t) (stats->ipackets + stats->ierrors));
571
572         if (cur_fwd_eng == &csum_fwd_engine)
573                 printf("  Bad-ipcsum: %-14"PRIu64" Bad-l4csum: %-14"PRIu64" \n",
574                                 port->rx_bad_ip_csum, port->rx_bad_l4_csum);
575
576         printf("  TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64"TX-total: "
577                "%-"PRIu64"\n",
578                stats->opackets, port->tx_dropped,
579                (uint64_t) (stats->opackets + port->tx_dropped));
580
581         if (stats->rx_nombuf > 0)
582                 printf("  RX-nombufs: %-14"PRIu64"\n", stats->rx_nombuf);
583 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
584         if (port->rx_stream)
585                 pkt_burst_stats_display("RX", &port->rx_stream->rx_burst_stats);
586         if (port->tx_stream)
587                 pkt_burst_stats_display("TX", &port->tx_stream->tx_burst_stats);
588 #endif
589         /* stats fdir */
590         if (fdir_conf.mode != RTE_FDIR_MODE_NONE)
591                 printf("  Fdirmiss: %-14"PRIu64"   Fdirmatch: %-14"PRIu64"\n",
592                        stats->fdirmiss,
593                        stats->fdirmatch);
594
595         printf("  %s--------------------------------%s\n",
596                fwd_stats_border, fwd_stats_border);
597 }
598
599 static void
600 fwd_stream_stats_display(streamid_t stream_id)
601 {
602         struct fwd_stream *fs;
603         static const char *fwd_top_stats_border = "-------";
604
605         fs = fwd_streams[stream_id];
606         if ((fs->rx_packets == 0) && (fs->tx_packets == 0) &&
607             (fs->fwd_dropped == 0))
608                 return;
609         printf("\n  %s Forward Stats for RX Port=%2d/Queue=%2d -> "
610                "TX Port=%2d/Queue=%2d %s\n",
611                fwd_top_stats_border, fs->rx_port, fs->rx_queue,
612                fs->tx_port, fs->tx_queue, fwd_top_stats_border);
613         printf("  RX-packets: %-14u TX-packets: %-14u TX-dropped: %-14u",
614                fs->rx_packets, fs->tx_packets, fs->fwd_dropped);
615
616         /* if checksum mode */
617         if (cur_fwd_eng == &csum_fwd_engine) {
618                printf("  RX- bad IP checksum: %-14u  Rx- bad L4 checksum: %-14u\n",
619                fs->rx_bad_ip_csum, fs->rx_bad_l4_csum);
620         }
621
622 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
623         pkt_burst_stats_display("RX", &fs->rx_burst_stats);
624         pkt_burst_stats_display("TX", &fs->tx_burst_stats);
625 #endif
626 }
627
628 static void
629 flush_all_rx_queues(void)
630 {
631         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
632         portid_t  rxp;
633         queueid_t rxq;
634         uint16_t  nb_rx;
635         uint16_t  i;
636         uint8_t   j;
637
638         for (j = 0; j < 2; j++) {
639                 for (rxp = 0; rxp < nb_ports; rxp++) {
640                         for (rxq = 0; rxq < nb_rxq; rxq++) {
641                                 do {
642                                         nb_rx = rte_eth_rx_burst(rxp, rxq,
643                                                                  pkts_burst,
644                                                                  MAX_PKT_BURST);
645                                         for (i = 0; i < nb_rx; i++)
646                                                 rte_pktmbuf_free(pkts_burst[i]);
647                                 } while (nb_rx > 0);
648                         }
649                 }
650                 rte_delay_ms(10); /* wait 10 milli-seconds before retrying */
651         }
652 }
653
654 static void
655 run_pkt_fwd_on_lcore(struct fwd_lcore *fc, packet_fwd_t pkt_fwd)
656 {
657         struct fwd_stream **fsm;
658         streamid_t nb_fs;
659         streamid_t sm_id;
660
661         fsm = &fwd_streams[fc->stream_idx];
662         nb_fs = fc->stream_nb;
663         do {
664                 for (sm_id = 0; sm_id < nb_fs; sm_id++)
665                         (*pkt_fwd)(fsm[sm_id]);
666         } while (! fc->stopped);
667 }
668
669 static int
670 start_pkt_forward_on_core(void *fwd_arg)
671 {
672         run_pkt_fwd_on_lcore((struct fwd_lcore *) fwd_arg,
673                              cur_fwd_config.fwd_eng->packet_fwd);
674         return 0;
675 }
676
677 /*
678  * Run the TXONLY packet forwarding engine to send a single burst of packets.
679  * Used to start communication flows in network loopback test configurations.
680  */
681 static int
682 run_one_txonly_burst_on_core(void *fwd_arg)
683 {
684         struct fwd_lcore *fwd_lc;
685         struct fwd_lcore tmp_lcore;
686
687         fwd_lc = (struct fwd_lcore *) fwd_arg;
688         tmp_lcore = *fwd_lc;
689         tmp_lcore.stopped = 1;
690         run_pkt_fwd_on_lcore(&tmp_lcore, tx_only_engine.packet_fwd);
691         return 0;
692 }
693
694 /*
695  * Launch packet forwarding:
696  *     - Setup per-port forwarding context.
697  *     - launch logical cores with their forwarding configuration.
698  */
699 static void
700 launch_packet_forwarding(lcore_function_t *pkt_fwd_on_lcore)
701 {
702         port_fwd_begin_t port_fwd_begin;
703         unsigned int i;
704         unsigned int lc_id;
705         int diag;
706
707         port_fwd_begin = cur_fwd_config.fwd_eng->port_fwd_begin;
708         if (port_fwd_begin != NULL) {
709                 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
710                         (*port_fwd_begin)(fwd_ports_ids[i]);
711         }
712         for (i = 0; i < cur_fwd_config.nb_fwd_lcores; i++) {
713                 lc_id = fwd_lcores_cpuids[i];
714                 if ((interactive == 0) || (lc_id != rte_lcore_id())) {
715                         fwd_lcores[i]->stopped = 0;
716                         diag = rte_eal_remote_launch(pkt_fwd_on_lcore,
717                                                      fwd_lcores[i], lc_id);
718                         if (diag != 0)
719                                 printf("launch lcore %u failed - diag=%d\n",
720                                        lc_id, diag);
721                 }
722         }
723 }
724
725 /*
726  * Launch packet forwarding configuration.
727  */
728 void
729 start_packet_forwarding(int with_tx_first)
730 {
731         port_fwd_begin_t port_fwd_begin;
732         port_fwd_end_t  port_fwd_end;
733         struct rte_port *port;
734         unsigned int i;
735         portid_t   pt_id;
736         streamid_t sm_id;
737
738         if (test_done == 0) {
739                 printf("Packet forwarding already started\n");
740                 return;
741         }
742         test_done = 0;
743         flush_all_rx_queues();
744         fwd_config_setup();
745         rxtx_config_display();
746
747         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
748                 pt_id = fwd_ports_ids[i];
749                 port = &ports[pt_id];
750                 rte_eth_stats_get(pt_id, &port->stats);
751                 port->tx_dropped = 0;
752         }
753         for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
754                 fwd_streams[sm_id]->rx_packets = 0;
755                 fwd_streams[sm_id]->tx_packets = 0;
756                 fwd_streams[sm_id]->fwd_dropped = 0;
757                 fwd_streams[sm_id]->rx_bad_ip_csum = 0;
758                 fwd_streams[sm_id]->rx_bad_l4_csum = 0;
759
760 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
761                 memset(&fwd_streams[sm_id]->rx_burst_stats, 0,
762                        sizeof(fwd_streams[sm_id]->rx_burst_stats));
763                 memset(&fwd_streams[sm_id]->tx_burst_stats, 0,
764                        sizeof(fwd_streams[sm_id]->tx_burst_stats));
765 #endif
766 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
767                 fwd_streams[sm_id]->core_cycles = 0;
768 #endif
769         }
770         if (with_tx_first) {
771                 port_fwd_begin = tx_only_engine.port_fwd_begin;
772                 if (port_fwd_begin != NULL) {
773                         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
774                                 (*port_fwd_begin)(fwd_ports_ids[i]);
775                 }
776                 launch_packet_forwarding(run_one_txonly_burst_on_core);
777                 rte_eal_mp_wait_lcore();
778                 port_fwd_end = tx_only_engine.port_fwd_end;
779                 if (port_fwd_end != NULL) {
780                         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
781                                 (*port_fwd_end)(fwd_ports_ids[i]);
782                 }
783         }
784         launch_packet_forwarding(start_pkt_forward_on_core);
785 }
786
787 void
788 stop_packet_forwarding(void)
789 {
790         struct rte_eth_stats stats;
791         struct rte_port *port;
792         port_fwd_end_t  port_fwd_end;
793         int i;
794         portid_t   pt_id;
795         streamid_t sm_id;
796         lcoreid_t  lc_id;
797         uint64_t total_recv;
798         uint64_t total_xmit;
799         uint64_t total_rx_dropped;
800         uint64_t total_tx_dropped;
801         uint64_t total_rx_nombuf;
802         uint64_t tx_dropped;
803         uint64_t rx_bad_ip_csum;
804         uint64_t rx_bad_l4_csum;
805 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
806         uint64_t fwd_cycles;
807 #endif
808         static const char *acc_stats_border = "+++++++++++++++";
809
810         if (test_done) {
811                 printf("Packet forwarding not started\n");
812                 return;
813         }
814         printf("Telling cores to stop...");
815         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++)
816                 fwd_lcores[lc_id]->stopped = 1;
817         printf("\nWaiting for lcores to finish...\n");
818         rte_eal_mp_wait_lcore();
819         port_fwd_end = cur_fwd_config.fwd_eng->port_fwd_end;
820         if (port_fwd_end != NULL) {
821                 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
822                         pt_id = fwd_ports_ids[i];
823                         (*port_fwd_end)(pt_id);
824                 }
825         }
826 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
827         fwd_cycles = 0;
828 #endif
829         for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
830                 if (cur_fwd_config.nb_fwd_streams >
831                     cur_fwd_config.nb_fwd_ports) {
832                         fwd_stream_stats_display(sm_id);
833                         ports[fwd_streams[sm_id]->tx_port].tx_stream = NULL;
834                         ports[fwd_streams[sm_id]->rx_port].rx_stream = NULL;
835                 } else {
836                         ports[fwd_streams[sm_id]->tx_port].tx_stream =
837                                 fwd_streams[sm_id];
838                         ports[fwd_streams[sm_id]->rx_port].rx_stream =
839                                 fwd_streams[sm_id];
840                 }
841                 tx_dropped = ports[fwd_streams[sm_id]->tx_port].tx_dropped;
842                 tx_dropped = (uint64_t) (tx_dropped +
843                                          fwd_streams[sm_id]->fwd_dropped);
844                 ports[fwd_streams[sm_id]->tx_port].tx_dropped = tx_dropped;
845
846                 rx_bad_ip_csum = ports[fwd_streams[sm_id]->rx_port].rx_bad_ip_csum;
847                 rx_bad_ip_csum = (uint64_t) (rx_bad_ip_csum +
848                                          fwd_streams[sm_id]->rx_bad_ip_csum);
849                 ports[fwd_streams[sm_id]->rx_port].rx_bad_ip_csum = rx_bad_ip_csum;
850
851                 rx_bad_l4_csum = ports[fwd_streams[sm_id]->rx_port].rx_bad_l4_csum;
852                 rx_bad_l4_csum = (uint64_t) (rx_bad_l4_csum +
853                                          fwd_streams[sm_id]->rx_bad_l4_csum);
854                 ports[fwd_streams[sm_id]->rx_port].rx_bad_l4_csum = rx_bad_l4_csum;
855
856 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
857                 fwd_cycles = (uint64_t) (fwd_cycles +
858                                          fwd_streams[sm_id]->core_cycles);
859 #endif
860         }
861         total_recv = 0;
862         total_xmit = 0;
863         total_rx_dropped = 0;
864         total_tx_dropped = 0;
865         total_rx_nombuf  = 0;
866         for (i = 0; i < ((cur_fwd_config.nb_fwd_ports + 1) & ~0x1); i++) {
867                 pt_id = fwd_ports_ids[i];
868
869                 port = &ports[pt_id];
870                 rte_eth_stats_get(pt_id, &stats);
871                 stats.ipackets -= port->stats.ipackets;
872                 port->stats.ipackets = 0;
873                 stats.opackets -= port->stats.opackets;
874                 port->stats.opackets = 0;
875                 stats.ibytes   -= port->stats.ibytes;
876                 port->stats.ibytes = 0;
877                 stats.obytes   -= port->stats.obytes;
878                 port->stats.obytes = 0;
879                 stats.ierrors  -= port->stats.ierrors;
880                 port->stats.ierrors = 0;
881                 stats.oerrors  -= port->stats.oerrors;
882                 port->stats.oerrors = 0;
883                 stats.rx_nombuf -= port->stats.rx_nombuf;
884                 port->stats.rx_nombuf = 0;
885                 stats.fdirmatch -= port->stats.fdirmatch;
886                 port->stats.rx_nombuf = 0;
887                 stats.fdirmiss -= port->stats.fdirmiss;
888                 port->stats.rx_nombuf = 0;
889
890                 total_recv += stats.ipackets;
891                 total_xmit += stats.opackets;
892                 total_rx_dropped += stats.ierrors;
893                 total_tx_dropped += port->tx_dropped;
894                 total_rx_nombuf  += stats.rx_nombuf;
895
896                 fwd_port_stats_display(pt_id, &stats);
897         }
898         printf("\n  %s Accumulated forward statistics for all ports"
899                "%s\n",
900                acc_stats_border, acc_stats_border);
901         printf("  RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64"RX-total: "
902                "%-"PRIu64"\n"
903                "  TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64"TX-total: "
904                "%-"PRIu64"\n",
905                total_recv, total_rx_dropped, total_recv + total_rx_dropped,
906                total_xmit, total_tx_dropped, total_xmit + total_tx_dropped);
907         if (total_rx_nombuf > 0)
908                 printf("  RX-nombufs: %-14"PRIu64"\n", total_rx_nombuf);
909         printf("  %s++++++++++++++++++++++++++++++++++++++++++++++"
910                "%s\n",
911                acc_stats_border, acc_stats_border);
912 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
913         if (total_recv > 0)
914                 printf("\n  CPU cycles/packet=%u (total cycles="
915                        "%"PRIu64" / total RX packets=%"PRIu64")\n",
916                        (unsigned int)(fwd_cycles / total_recv),
917                        fwd_cycles, total_recv);
918 #endif
919         printf("\nDone.\n");
920         test_done = 1;
921 }
922
923 void
924 pmd_test_exit(void)
925 {
926         portid_t pt_id;
927
928         for (pt_id = 0; pt_id < nb_ports; pt_id++) {
929                 printf("Stopping port %d...", pt_id);
930                 fflush(stdout);
931                 rte_eth_dev_close(pt_id);
932                 printf("done\n");
933         }
934         printf("bye...\n");
935 }
936
937 typedef void (*cmd_func_t)(void);
938 struct pmd_test_command {
939         const char *cmd_name;
940         cmd_func_t cmd_func;
941 };
942
943 #define PMD_TEST_CMD_NB (sizeof(pmd_test_menu) / sizeof(pmd_test_menu[0]))
944
945 static void
946 fatal_init_error(const char *func_name, uint8_t port_id, int diag)
947 {
948         rte_panic("%s(port_id=%d) failed - diag=%d\n",
949                   func_name, port_id, diag);
950 }
951
952 static void
953 init_ports(void)
954 {
955         struct rte_eth_link   link;
956         struct rte_eth_conf   port_conf = {
957                 .intr_conf = {
958                         .lsc = 0,
959                 },
960         };
961         struct rte_eth_rxconf rx_conf;
962         struct rte_eth_txconf tx_conf;
963         struct rte_port *port;
964         unsigned int sock_id;
965         portid_t  pi;
966         queueid_t qi;
967         int diag;
968
969         port_conf.rxmode = rx_mode;
970         port_conf.fdir_conf = fdir_conf;
971
972         if (nb_rxq > 0) { /* configure RSS */
973                 port_conf.rx_adv_conf.rss_conf.rss_key = NULL;
974                 /* use default hash key */
975                 port_conf.rx_adv_conf.rss_conf.rss_hf = rss_hf;
976         } else
977                 port_conf.rx_adv_conf.rss_conf.rss_hf = 0;
978         rx_conf.rx_thresh = rx_thresh;
979         rx_conf.rx_free_thresh = rx_free_thresh;
980         tx_conf.tx_thresh = tx_thresh;
981         tx_conf.tx_rs_thresh = tx_rs_thresh;
982         tx_conf.tx_free_thresh = tx_free_thresh;
983
984         for (pi = 0; pi < nb_ports; pi++) {
985                 port = &ports[pi];
986                 memcpy(&port->dev_conf, &port_conf, sizeof(port_conf));
987                 sock_id = port->socket_id;
988                 printf("Initializing port %d... ", pi);
989                 fflush(stdout);
990                 diag = rte_eth_dev_configure(pi, nb_rxq, nb_txq, &port_conf);
991                 if (diag != 0) {
992                         fatal_init_error("rte_eth_dev_configure", pi, diag);
993                         /* NOT REACHED */
994                 }
995                 rte_eth_macaddr_get(pi, &port->eth_addr);
996                 for (qi = 0; qi < nb_txq; qi++) {
997                         diag = rte_eth_tx_queue_setup(pi, qi, nb_txd,
998                                                       sock_id,
999                                                       &tx_conf);
1000                         if (diag != 0) {
1001                                 fatal_init_error("rte_eth_tx_queue_setup",
1002                                                  pi, diag);
1003                                 /* NOT REACHED */
1004                         }
1005                 }
1006                 for (qi = 0; qi < nb_rxq; qi++) {
1007                         diag = rte_eth_rx_queue_setup(pi, qi, nb_rxd, sock_id,
1008                                                       &rx_conf,
1009                                                       mbuf_pool_find(sock_id));
1010                         if (diag != 0) {
1011                                 fatal_init_error("rte_eth_rx_queue_setup",
1012                                                  pi , diag);
1013                                 /* NOT REACHED */
1014                         }
1015                 }
1016
1017                 /* Start device */
1018                 diag = rte_eth_dev_start(pi);
1019                 if (diag != 0) {
1020                         fatal_init_error("rte_eth_dev_start", pi, diag);
1021                         /* NOT REACHED */
1022                 }
1023                 printf("done: ");
1024                 rte_eth_link_get(pi, &link);
1025                 if (link.link_status) {
1026                         printf(" Link Up - speed %u Mbps - %s\n",
1027                                (unsigned) link.link_speed,
1028                                (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1029                                ("full-duplex") : ("half-duplex\n"));
1030                 } else {
1031                         printf(" Link Down\n");
1032                 }
1033
1034                 /*
1035                  * If enabled, put device in promiscuous mode.
1036                  * This allows the PMD test in IO forwarding mode to forward
1037                  * packets to itself through 2 cross-connected  ports of the
1038                  * target machine.
1039                  */
1040                 if (promiscuous_on)
1041                         rte_eth_promiscuous_enable(pi);
1042         }
1043 }
1044
1045 #ifdef RTE_EXEC_ENV_BAREMETAL
1046 #define main _main
1047 #endif
1048
1049 int
1050 main(int argc, char** argv)
1051 {
1052         int  diag;
1053
1054         diag = rte_eal_init(argc, argv);
1055         if (diag < 0)
1056                 rte_panic("Cannot init EAL\n");
1057
1058         if (rte_pmd_init_all())
1059                 rte_panic("Cannot init PMD\n");
1060
1061         if (rte_eal_pci_probe())
1062                 rte_panic("Cannot probe PCI\n");
1063
1064         nb_ports = (portid_t) rte_eth_dev_count();
1065         if (nb_ports == 0)
1066                 rte_exit(EXIT_FAILURE, "No probed ethernet devices - check that "
1067                           "CONFIG_RTE_LIBRTE_IGB_PMD=y and that "
1068                           "CONFIG_RTE_LIBRTE_EM_PMD=y and that "
1069                           "CONFIG_RTE_LIBRTE_IXGBE_PMD=y in your "
1070                           "configuration file\n");
1071
1072         set_def_fwd_config();
1073         if (nb_lcores == 0)
1074                 rte_panic("Empty set of forwarding logical cores - check the "
1075                           "core mask supplied in the command parameters\n");
1076
1077         argc -= diag;
1078         argv += diag;
1079         if (argc > 1)
1080                 launch_args_parse(argc, argv);
1081
1082         if (nb_rxq > nb_txq)
1083                 printf("Warning: nb_rxq=%d enables RSS configuration, "
1084                        "but nb_txq=%d will prevent to fully test it.\n",
1085                        nb_rxq, nb_txq);
1086
1087         init_config();
1088
1089         init_ports();
1090
1091         if (interactive == 1)
1092                 prompt();
1093         else {
1094                 char c;
1095                 int rc;
1096
1097                 printf("No commandline core given, start packet forwarding\n");
1098                 start_packet_forwarding(0);
1099                 printf("Press enter to exit\n");
1100                 rc = read(0, &c, 1);
1101                 if (rc < 0)
1102                         return 1;
1103         }
1104
1105         return 0;
1106 }