app/testpmd: fix quit to stop all ports before close
[dpdk.git] / app / test-pmd / testpmd.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <signal.h>
9 #include <string.h>
10 #include <time.h>
11 #include <fcntl.h>
12 #include <sys/mman.h>
13 #include <sys/types.h>
14 #include <errno.h>
15 #include <stdbool.h>
16
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19
20 #include <stdint.h>
21 #include <unistd.h>
22 #include <inttypes.h>
23
24 #include <rte_common.h>
25 #include <rte_errno.h>
26 #include <rte_byteorder.h>
27 #include <rte_log.h>
28 #include <rte_debug.h>
29 #include <rte_cycles.h>
30 #include <rte_malloc_heap.h>
31 #include <rte_memory.h>
32 #include <rte_memcpy.h>
33 #include <rte_launch.h>
34 #include <rte_eal.h>
35 #include <rte_alarm.h>
36 #include <rte_per_lcore.h>
37 #include <rte_lcore.h>
38 #include <rte_atomic.h>
39 #include <rte_branch_prediction.h>
40 #include <rte_mempool.h>
41 #include <rte_malloc.h>
42 #include <rte_mbuf.h>
43 #include <rte_mbuf_pool_ops.h>
44 #include <rte_interrupts.h>
45 #include <rte_pci.h>
46 #include <rte_ether.h>
47 #include <rte_ethdev.h>
48 #include <rte_dev.h>
49 #include <rte_string_fns.h>
50 #ifdef RTE_LIBRTE_IXGBE_PMD
51 #include <rte_pmd_ixgbe.h>
52 #endif
53 #ifdef RTE_LIBRTE_PDUMP
54 #include <rte_pdump.h>
55 #endif
56 #include <rte_flow.h>
57 #include <rte_metrics.h>
58 #ifdef RTE_LIBRTE_BITRATE
59 #include <rte_bitrate.h>
60 #endif
61 #ifdef RTE_LIBRTE_LATENCY_STATS
62 #include <rte_latencystats.h>
63 #endif
64
65 #include "testpmd.h"
66
67 #ifndef MAP_HUGETLB
68 /* FreeBSD may not have MAP_HUGETLB (in fact, it probably doesn't) */
69 #define HUGE_FLAG (0x40000)
70 #else
71 #define HUGE_FLAG MAP_HUGETLB
72 #endif
73
74 #ifndef MAP_HUGE_SHIFT
75 /* older kernels (or FreeBSD) will not have this define */
76 #define HUGE_SHIFT (26)
77 #else
78 #define HUGE_SHIFT MAP_HUGE_SHIFT
79 #endif
80
81 #define EXTMEM_HEAP_NAME "extmem"
82
83 uint16_t verbose_level = 0; /**< Silent by default. */
84 int testpmd_logtype; /**< Log type for testpmd logs */
85
86 /* use master core for command line ? */
87 uint8_t interactive = 0;
88 uint8_t auto_start = 0;
89 uint8_t tx_first;
90 char cmdline_filename[PATH_MAX] = {0};
91
92 /*
93  * NUMA support configuration.
94  * When set, the NUMA support attempts to dispatch the allocation of the
95  * RX and TX memory rings, and of the DMA memory buffers (mbufs) for the
96  * probed ports among the CPU sockets 0 and 1.
97  * Otherwise, all memory is allocated from CPU socket 0.
98  */
99 uint8_t numa_support = 1; /**< numa enabled by default */
100
101 /*
102  * In UMA mode,all memory is allocated from socket 0 if --socket-num is
103  * not configured.
104  */
105 uint8_t socket_num = UMA_NO_CONFIG;
106
107 /*
108  * Select mempool allocation type:
109  * - native: use regular DPDK memory
110  * - anon: use regular DPDK memory to create mempool, but populate using
111  *         anonymous memory (may not be IOVA-contiguous)
112  * - xmem: use externally allocated hugepage memory
113  */
114 uint8_t mp_alloc_type = MP_ALLOC_NATIVE;
115
116 /*
117  * Store specified sockets on which memory pool to be used by ports
118  * is allocated.
119  */
120 uint8_t port_numa[RTE_MAX_ETHPORTS];
121
122 /*
123  * Store specified sockets on which RX ring to be used by ports
124  * is allocated.
125  */
126 uint8_t rxring_numa[RTE_MAX_ETHPORTS];
127
128 /*
129  * Store specified sockets on which TX ring to be used by ports
130  * is allocated.
131  */
132 uint8_t txring_numa[RTE_MAX_ETHPORTS];
133
134 /*
135  * Record the Ethernet address of peer target ports to which packets are
136  * forwarded.
137  * Must be instantiated with the ethernet addresses of peer traffic generator
138  * ports.
139  */
140 struct ether_addr peer_eth_addrs[RTE_MAX_ETHPORTS];
141 portid_t nb_peer_eth_addrs = 0;
142
143 /*
144  * Probed Target Environment.
145  */
146 struct rte_port *ports;        /**< For all probed ethernet ports. */
147 portid_t nb_ports;             /**< Number of probed ethernet ports. */
148 struct fwd_lcore **fwd_lcores; /**< For all probed logical cores. */
149 lcoreid_t nb_lcores;           /**< Number of probed logical cores. */
150
151 portid_t ports_ids[RTE_MAX_ETHPORTS]; /**< Store all port ids. */
152
153 /*
154  * Test Forwarding Configuration.
155  *    nb_fwd_lcores <= nb_cfg_lcores <= nb_lcores
156  *    nb_fwd_ports  <= nb_cfg_ports  <= nb_ports
157  */
158 lcoreid_t nb_cfg_lcores; /**< Number of configured logical cores. */
159 lcoreid_t nb_fwd_lcores; /**< Number of forwarding logical cores. */
160 portid_t  nb_cfg_ports;  /**< Number of configured ports. */
161 portid_t  nb_fwd_ports;  /**< Number of forwarding ports. */
162
163 unsigned int fwd_lcores_cpuids[RTE_MAX_LCORE]; /**< CPU ids configuration. */
164 portid_t fwd_ports_ids[RTE_MAX_ETHPORTS];      /**< Port ids configuration. */
165
166 struct fwd_stream **fwd_streams; /**< For each RX queue of each port. */
167 streamid_t nb_fwd_streams;       /**< Is equal to (nb_ports * nb_rxq). */
168
169 /*
170  * Forwarding engines.
171  */
172 struct fwd_engine * fwd_engines[] = {
173         &io_fwd_engine,
174         &mac_fwd_engine,
175         &mac_swap_engine,
176         &flow_gen_engine,
177         &rx_only_engine,
178         &tx_only_engine,
179         &csum_fwd_engine,
180         &icmp_echo_engine,
181         &noisy_vnf_engine,
182 #if defined RTE_LIBRTE_PMD_SOFTNIC
183         &softnic_fwd_engine,
184 #endif
185 #ifdef RTE_LIBRTE_IEEE1588
186         &ieee1588_fwd_engine,
187 #endif
188         NULL,
189 };
190
191 struct fwd_config cur_fwd_config;
192 struct fwd_engine *cur_fwd_eng = &io_fwd_engine; /**< IO mode by default. */
193 uint32_t retry_enabled;
194 uint32_t burst_tx_delay_time = BURST_TX_WAIT_US;
195 uint32_t burst_tx_retry_num = BURST_TX_RETRIES;
196
197 uint16_t mbuf_data_size = DEFAULT_MBUF_DATA_SIZE; /**< Mbuf data space size. */
198 uint32_t param_total_num_mbufs = 0;  /**< number of mbufs in all pools - if
199                                       * specified on command-line. */
200 uint16_t stats_period; /**< Period to show statistics (disabled by default) */
201
202 /*
203  * In container, it cannot terminate the process which running with 'stats-period'
204  * option. Set flag to exit stats period loop after received SIGINT/SIGTERM.
205  */
206 uint8_t f_quit;
207
208 /*
209  * Configuration of packet segments used by the "txonly" processing engine.
210  */
211 uint16_t tx_pkt_length = TXONLY_DEF_PACKET_LEN; /**< TXONLY packet length. */
212 uint16_t tx_pkt_seg_lengths[RTE_MAX_SEGS_PER_PKT] = {
213         TXONLY_DEF_PACKET_LEN,
214 };
215 uint8_t  tx_pkt_nb_segs = 1; /**< Number of segments in TXONLY packets */
216
217 enum tx_pkt_split tx_pkt_split = TX_PKT_SPLIT_OFF;
218 /**< Split policy for packets to TX. */
219
220 uint16_t nb_pkt_per_burst = DEF_PKT_BURST; /**< Number of packets per burst. */
221 uint16_t mb_mempool_cache = DEF_MBUF_CACHE; /**< Size of mbuf mempool cache. */
222
223 /* current configuration is in DCB or not,0 means it is not in DCB mode */
224 uint8_t dcb_config = 0;
225
226 /* Whether the dcb is in testing status */
227 uint8_t dcb_test = 0;
228
229 /*
230  * Configurable number of RX/TX queues.
231  */
232 queueid_t nb_rxq = 1; /**< Number of RX queues per port. */
233 queueid_t nb_txq = 1; /**< Number of TX queues per port. */
234
235 /*
236  * Configurable number of RX/TX ring descriptors.
237  * Defaults are supplied by drivers via ethdev.
238  */
239 #define RTE_TEST_RX_DESC_DEFAULT 0
240 #define RTE_TEST_TX_DESC_DEFAULT 0
241 uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT; /**< Number of RX descriptors. */
242 uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT; /**< Number of TX descriptors. */
243
244 #define RTE_PMD_PARAM_UNSET -1
245 /*
246  * Configurable values of RX and TX ring threshold registers.
247  */
248
249 int8_t rx_pthresh = RTE_PMD_PARAM_UNSET;
250 int8_t rx_hthresh = RTE_PMD_PARAM_UNSET;
251 int8_t rx_wthresh = RTE_PMD_PARAM_UNSET;
252
253 int8_t tx_pthresh = RTE_PMD_PARAM_UNSET;
254 int8_t tx_hthresh = RTE_PMD_PARAM_UNSET;
255 int8_t tx_wthresh = RTE_PMD_PARAM_UNSET;
256
257 /*
258  * Configurable value of RX free threshold.
259  */
260 int16_t rx_free_thresh = RTE_PMD_PARAM_UNSET;
261
262 /*
263  * Configurable value of RX drop enable.
264  */
265 int8_t rx_drop_en = RTE_PMD_PARAM_UNSET;
266
267 /*
268  * Configurable value of TX free threshold.
269  */
270 int16_t tx_free_thresh = RTE_PMD_PARAM_UNSET;
271
272 /*
273  * Configurable value of TX RS bit threshold.
274  */
275 int16_t tx_rs_thresh = RTE_PMD_PARAM_UNSET;
276
277 /*
278  * Configurable value of buffered packets before sending.
279  */
280 uint16_t noisy_tx_sw_bufsz;
281
282 /*
283  * Configurable value of packet buffer timeout.
284  */
285 uint16_t noisy_tx_sw_buf_flush_time;
286
287 /*
288  * Configurable value for size of VNF internal memory area
289  * used for simulating noisy neighbour behaviour
290  */
291 uint64_t noisy_lkup_mem_sz;
292
293 /*
294  * Configurable value of number of random writes done in
295  * VNF simulation memory area.
296  */
297 uint64_t noisy_lkup_num_writes;
298
299 /*
300  * Configurable value of number of random reads done in
301  * VNF simulation memory area.
302  */
303 uint64_t noisy_lkup_num_reads;
304
305 /*
306  * Configurable value of number of random reads/writes done in
307  * VNF simulation memory area.
308  */
309 uint64_t noisy_lkup_num_reads_writes;
310
311 /*
312  * Receive Side Scaling (RSS) configuration.
313  */
314 uint64_t rss_hf = ETH_RSS_IP; /* RSS IP by default. */
315
316 /*
317  * Port topology configuration
318  */
319 uint16_t port_topology = PORT_TOPOLOGY_PAIRED; /* Ports are paired by default */
320
321 /*
322  * Avoids to flush all the RX streams before starts forwarding.
323  */
324 uint8_t no_flush_rx = 0; /* flush by default */
325
326 /*
327  * Flow API isolated mode.
328  */
329 uint8_t flow_isolate_all;
330
331 /*
332  * Avoids to check link status when starting/stopping a port.
333  */
334 uint8_t no_link_check = 0; /* check by default */
335
336 /*
337  * Enable link status change notification
338  */
339 uint8_t lsc_interrupt = 1; /* enabled by default */
340
341 /*
342  * Enable device removal notification.
343  */
344 uint8_t rmv_interrupt = 1; /* enabled by default */
345
346 uint8_t hot_plug = 0; /**< hotplug disabled by default. */
347
348 /* After attach, port setup is called on event or by iterator */
349 bool setup_on_probe_event = true;
350
351 /* Pretty printing of ethdev events */
352 static const char * const eth_event_desc[] = {
353         [RTE_ETH_EVENT_UNKNOWN] = "unknown",
354         [RTE_ETH_EVENT_INTR_LSC] = "link state change",
355         [RTE_ETH_EVENT_QUEUE_STATE] = "queue state",
356         [RTE_ETH_EVENT_INTR_RESET] = "reset",
357         [RTE_ETH_EVENT_VF_MBOX] = "VF mbox",
358         [RTE_ETH_EVENT_IPSEC] = "IPsec",
359         [RTE_ETH_EVENT_MACSEC] = "MACsec",
360         [RTE_ETH_EVENT_INTR_RMV] = "device removal",
361         [RTE_ETH_EVENT_NEW] = "device probed",
362         [RTE_ETH_EVENT_DESTROY] = "device released",
363         [RTE_ETH_EVENT_MAX] = NULL,
364 };
365
366 /*
367  * Display or mask ether events
368  * Default to all events except VF_MBOX
369  */
370 uint32_t event_print_mask = (UINT32_C(1) << RTE_ETH_EVENT_UNKNOWN) |
371                             (UINT32_C(1) << RTE_ETH_EVENT_INTR_LSC) |
372                             (UINT32_C(1) << RTE_ETH_EVENT_QUEUE_STATE) |
373                             (UINT32_C(1) << RTE_ETH_EVENT_INTR_RESET) |
374                             (UINT32_C(1) << RTE_ETH_EVENT_IPSEC) |
375                             (UINT32_C(1) << RTE_ETH_EVENT_MACSEC) |
376                             (UINT32_C(1) << RTE_ETH_EVENT_INTR_RMV);
377 /*
378  * Decide if all memory are locked for performance.
379  */
380 int do_mlockall = 0;
381
382 /*
383  * NIC bypass mode configuration options.
384  */
385
386 #if defined RTE_LIBRTE_IXGBE_PMD && defined RTE_LIBRTE_IXGBE_BYPASS
387 /* The NIC bypass watchdog timeout. */
388 uint32_t bypass_timeout = RTE_PMD_IXGBE_BYPASS_TMT_OFF;
389 #endif
390
391
392 #ifdef RTE_LIBRTE_LATENCY_STATS
393
394 /*
395  * Set when latency stats is enabled in the commandline
396  */
397 uint8_t latencystats_enabled;
398
399 /*
400  * Lcore ID to serive latency statistics.
401  */
402 lcoreid_t latencystats_lcore_id = -1;
403
404 #endif
405
406 /*
407  * Ethernet device configuration.
408  */
409 struct rte_eth_rxmode rx_mode = {
410         .max_rx_pkt_len = ETHER_MAX_LEN, /**< Default maximum frame length. */
411 };
412
413 struct rte_eth_txmode tx_mode = {
414         .offloads = DEV_TX_OFFLOAD_MBUF_FAST_FREE,
415 };
416
417 struct rte_fdir_conf fdir_conf = {
418         .mode = RTE_FDIR_MODE_NONE,
419         .pballoc = RTE_FDIR_PBALLOC_64K,
420         .status = RTE_FDIR_REPORT_STATUS,
421         .mask = {
422                 .vlan_tci_mask = 0xFFEF,
423                 .ipv4_mask     = {
424                         .src_ip = 0xFFFFFFFF,
425                         .dst_ip = 0xFFFFFFFF,
426                 },
427                 .ipv6_mask     = {
428                         .src_ip = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
429                         .dst_ip = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
430                 },
431                 .src_port_mask = 0xFFFF,
432                 .dst_port_mask = 0xFFFF,
433                 .mac_addr_byte_mask = 0xFF,
434                 .tunnel_type_mask = 1,
435                 .tunnel_id_mask = 0xFFFFFFFF,
436         },
437         .drop_queue = 127,
438 };
439
440 volatile int test_done = 1; /* stop packet forwarding when set to 1. */
441
442 struct queue_stats_mappings tx_queue_stats_mappings_array[MAX_TX_QUEUE_STATS_MAPPINGS];
443 struct queue_stats_mappings rx_queue_stats_mappings_array[MAX_RX_QUEUE_STATS_MAPPINGS];
444
445 struct queue_stats_mappings *tx_queue_stats_mappings = tx_queue_stats_mappings_array;
446 struct queue_stats_mappings *rx_queue_stats_mappings = rx_queue_stats_mappings_array;
447
448 uint16_t nb_tx_queue_stats_mappings = 0;
449 uint16_t nb_rx_queue_stats_mappings = 0;
450
451 /*
452  * Display zero values by default for xstats
453  */
454 uint8_t xstats_hide_zero;
455
456 unsigned int num_sockets = 0;
457 unsigned int socket_ids[RTE_MAX_NUMA_NODES];
458
459 #ifdef RTE_LIBRTE_BITRATE
460 /* Bitrate statistics */
461 struct rte_stats_bitrates *bitrate_data;
462 lcoreid_t bitrate_lcore_id;
463 uint8_t bitrate_enabled;
464 #endif
465
466 struct gro_status gro_ports[RTE_MAX_ETHPORTS];
467 uint8_t gro_flush_cycles = GRO_DEFAULT_FLUSH_CYCLES;
468
469 struct vxlan_encap_conf vxlan_encap_conf = {
470         .select_ipv4 = 1,
471         .select_vlan = 0,
472         .vni = "\x00\x00\x00",
473         .udp_src = 0,
474         .udp_dst = RTE_BE16(4789),
475         .ipv4_src = IPv4(127, 0, 0, 1),
476         .ipv4_dst = IPv4(255, 255, 255, 255),
477         .ipv6_src = "\x00\x00\x00\x00\x00\x00\x00\x00"
478                 "\x00\x00\x00\x00\x00\x00\x00\x01",
479         .ipv6_dst = "\x00\x00\x00\x00\x00\x00\x00\x00"
480                 "\x00\x00\x00\x00\x00\x00\x11\x11",
481         .vlan_tci = 0,
482         .eth_src = "\x00\x00\x00\x00\x00\x00",
483         .eth_dst = "\xff\xff\xff\xff\xff\xff",
484 };
485
486 struct nvgre_encap_conf nvgre_encap_conf = {
487         .select_ipv4 = 1,
488         .select_vlan = 0,
489         .tni = "\x00\x00\x00",
490         .ipv4_src = IPv4(127, 0, 0, 1),
491         .ipv4_dst = IPv4(255, 255, 255, 255),
492         .ipv6_src = "\x00\x00\x00\x00\x00\x00\x00\x00"
493                 "\x00\x00\x00\x00\x00\x00\x00\x01",
494         .ipv6_dst = "\x00\x00\x00\x00\x00\x00\x00\x00"
495                 "\x00\x00\x00\x00\x00\x00\x11\x11",
496         .vlan_tci = 0,
497         .eth_src = "\x00\x00\x00\x00\x00\x00",
498         .eth_dst = "\xff\xff\xff\xff\xff\xff",
499 };
500
501 /* Forward function declarations */
502 static void setup_attached_port(portid_t pi);
503 static void map_port_queue_stats_mapping_registers(portid_t pi,
504                                                    struct rte_port *port);
505 static void check_all_ports_link_status(uint32_t port_mask);
506 static int eth_event_callback(portid_t port_id,
507                               enum rte_eth_event_type type,
508                               void *param, void *ret_param);
509 static void dev_event_callback(const char *device_name,
510                                 enum rte_dev_event_type type,
511                                 void *param);
512
513 /*
514  * Check if all the ports are started.
515  * If yes, return positive value. If not, return zero.
516  */
517 static int all_ports_started(void);
518
519 struct gso_status gso_ports[RTE_MAX_ETHPORTS];
520 uint16_t gso_max_segment_size = ETHER_MAX_LEN - ETHER_CRC_LEN;
521
522 /*
523  * Helper function to check if socket is already discovered.
524  * If yes, return positive value. If not, return zero.
525  */
526 int
527 new_socket_id(unsigned int socket_id)
528 {
529         unsigned int i;
530
531         for (i = 0; i < num_sockets; i++) {
532                 if (socket_ids[i] == socket_id)
533                         return 0;
534         }
535         return 1;
536 }
537
538 /*
539  * Setup default configuration.
540  */
541 static void
542 set_default_fwd_lcores_config(void)
543 {
544         unsigned int i;
545         unsigned int nb_lc;
546         unsigned int sock_num;
547
548         nb_lc = 0;
549         for (i = 0; i < RTE_MAX_LCORE; i++) {
550                 if (!rte_lcore_is_enabled(i))
551                         continue;
552                 sock_num = rte_lcore_to_socket_id(i);
553                 if (new_socket_id(sock_num)) {
554                         if (num_sockets >= RTE_MAX_NUMA_NODES) {
555                                 rte_exit(EXIT_FAILURE,
556                                          "Total sockets greater than %u\n",
557                                          RTE_MAX_NUMA_NODES);
558                         }
559                         socket_ids[num_sockets++] = sock_num;
560                 }
561                 if (i == rte_get_master_lcore())
562                         continue;
563                 fwd_lcores_cpuids[nb_lc++] = i;
564         }
565         nb_lcores = (lcoreid_t) nb_lc;
566         nb_cfg_lcores = nb_lcores;
567         nb_fwd_lcores = 1;
568 }
569
570 static void
571 set_def_peer_eth_addrs(void)
572 {
573         portid_t i;
574
575         for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
576                 peer_eth_addrs[i].addr_bytes[0] = ETHER_LOCAL_ADMIN_ADDR;
577                 peer_eth_addrs[i].addr_bytes[5] = i;
578         }
579 }
580
581 static void
582 set_default_fwd_ports_config(void)
583 {
584         portid_t pt_id;
585         int i = 0;
586
587         RTE_ETH_FOREACH_DEV(pt_id) {
588                 fwd_ports_ids[i++] = pt_id;
589
590                 /* Update sockets info according to the attached device */
591                 int socket_id = rte_eth_dev_socket_id(pt_id);
592                 if (socket_id >= 0 && new_socket_id(socket_id)) {
593                         if (num_sockets >= RTE_MAX_NUMA_NODES) {
594                                 rte_exit(EXIT_FAILURE,
595                                          "Total sockets greater than %u\n",
596                                          RTE_MAX_NUMA_NODES);
597                         }
598                         socket_ids[num_sockets++] = socket_id;
599                 }
600         }
601
602         nb_cfg_ports = nb_ports;
603         nb_fwd_ports = nb_ports;
604 }
605
606 void
607 set_def_fwd_config(void)
608 {
609         set_default_fwd_lcores_config();
610         set_def_peer_eth_addrs();
611         set_default_fwd_ports_config();
612 }
613
614 /* extremely pessimistic estimation of memory required to create a mempool */
615 static int
616 calc_mem_size(uint32_t nb_mbufs, uint32_t mbuf_sz, size_t pgsz, size_t *out)
617 {
618         unsigned int n_pages, mbuf_per_pg, leftover;
619         uint64_t total_mem, mbuf_mem, obj_sz;
620
621         /* there is no good way to predict how much space the mempool will
622          * occupy because it will allocate chunks on the fly, and some of those
623          * will come from default DPDK memory while some will come from our
624          * external memory, so just assume 128MB will be enough for everyone.
625          */
626         uint64_t hdr_mem = 128 << 20;
627
628         /* account for possible non-contiguousness */
629         obj_sz = rte_mempool_calc_obj_size(mbuf_sz, 0, NULL);
630         if (obj_sz > pgsz) {
631                 TESTPMD_LOG(ERR, "Object size is bigger than page size\n");
632                 return -1;
633         }
634
635         mbuf_per_pg = pgsz / obj_sz;
636         leftover = (nb_mbufs % mbuf_per_pg) > 0;
637         n_pages = (nb_mbufs / mbuf_per_pg) + leftover;
638
639         mbuf_mem = n_pages * pgsz;
640
641         total_mem = RTE_ALIGN(hdr_mem + mbuf_mem, pgsz);
642
643         if (total_mem > SIZE_MAX) {
644                 TESTPMD_LOG(ERR, "Memory size too big\n");
645                 return -1;
646         }
647         *out = (size_t)total_mem;
648
649         return 0;
650 }
651
652 static int
653 pagesz_flags(uint64_t page_sz)
654 {
655         /* as per mmap() manpage, all page sizes are log2 of page size
656          * shifted by MAP_HUGE_SHIFT
657          */
658         int log2 = rte_log2_u64(page_sz);
659
660         return (log2 << HUGE_SHIFT);
661 }
662
663 static void *
664 alloc_mem(size_t memsz, size_t pgsz, bool huge)
665 {
666         void *addr;
667         int flags;
668
669         /* allocate anonymous hugepages */
670         flags = MAP_ANONYMOUS | MAP_PRIVATE;
671         if (huge)
672                 flags |= HUGE_FLAG | pagesz_flags(pgsz);
673
674         addr = mmap(NULL, memsz, PROT_READ | PROT_WRITE, flags, -1, 0);
675         if (addr == MAP_FAILED)
676                 return NULL;
677
678         return addr;
679 }
680
681 struct extmem_param {
682         void *addr;
683         size_t len;
684         size_t pgsz;
685         rte_iova_t *iova_table;
686         unsigned int iova_table_len;
687 };
688
689 static int
690 create_extmem(uint32_t nb_mbufs, uint32_t mbuf_sz, struct extmem_param *param,
691                 bool huge)
692 {
693         uint64_t pgsizes[] = {RTE_PGSIZE_2M, RTE_PGSIZE_1G, /* x86_64, ARM */
694                         RTE_PGSIZE_16M, RTE_PGSIZE_16G};    /* POWER */
695         unsigned int cur_page, n_pages, pgsz_idx;
696         size_t mem_sz, cur_pgsz;
697         rte_iova_t *iovas = NULL;
698         void *addr;
699         int ret;
700
701         for (pgsz_idx = 0; pgsz_idx < RTE_DIM(pgsizes); pgsz_idx++) {
702                 /* skip anything that is too big */
703                 if (pgsizes[pgsz_idx] > SIZE_MAX)
704                         continue;
705
706                 cur_pgsz = pgsizes[pgsz_idx];
707
708                 /* if we were told not to allocate hugepages, override */
709                 if (!huge)
710                         cur_pgsz = sysconf(_SC_PAGESIZE);
711
712                 ret = calc_mem_size(nb_mbufs, mbuf_sz, cur_pgsz, &mem_sz);
713                 if (ret < 0) {
714                         TESTPMD_LOG(ERR, "Cannot calculate memory size\n");
715                         return -1;
716                 }
717
718                 /* allocate our memory */
719                 addr = alloc_mem(mem_sz, cur_pgsz, huge);
720
721                 /* if we couldn't allocate memory with a specified page size,
722                  * that doesn't mean we can't do it with other page sizes, so
723                  * try another one.
724                  */
725                 if (addr == NULL)
726                         continue;
727
728                 /* store IOVA addresses for every page in this memory area */
729                 n_pages = mem_sz / cur_pgsz;
730
731                 iovas = malloc(sizeof(*iovas) * n_pages);
732
733                 if (iovas == NULL) {
734                         TESTPMD_LOG(ERR, "Cannot allocate memory for iova addresses\n");
735                         goto fail;
736                 }
737                 /* lock memory if it's not huge pages */
738                 if (!huge)
739                         mlock(addr, mem_sz);
740
741                 /* populate IOVA addresses */
742                 for (cur_page = 0; cur_page < n_pages; cur_page++) {
743                         rte_iova_t iova;
744                         size_t offset;
745                         void *cur;
746
747                         offset = cur_pgsz * cur_page;
748                         cur = RTE_PTR_ADD(addr, offset);
749
750                         /* touch the page before getting its IOVA */
751                         *(volatile char *)cur = 0;
752
753                         iova = rte_mem_virt2iova(cur);
754
755                         iovas[cur_page] = iova;
756                 }
757
758                 break;
759         }
760         /* if we couldn't allocate anything */
761         if (iovas == NULL)
762                 return -1;
763
764         param->addr = addr;
765         param->len = mem_sz;
766         param->pgsz = cur_pgsz;
767         param->iova_table = iovas;
768         param->iova_table_len = n_pages;
769
770         return 0;
771 fail:
772         if (iovas)
773                 free(iovas);
774         if (addr)
775                 munmap(addr, mem_sz);
776
777         return -1;
778 }
779
780 static int
781 setup_extmem(uint32_t nb_mbufs, uint32_t mbuf_sz, bool huge)
782 {
783         struct extmem_param param;
784         int socket_id, ret;
785
786         memset(&param, 0, sizeof(param));
787
788         /* check if our heap exists */
789         socket_id = rte_malloc_heap_get_socket(EXTMEM_HEAP_NAME);
790         if (socket_id < 0) {
791                 /* create our heap */
792                 ret = rte_malloc_heap_create(EXTMEM_HEAP_NAME);
793                 if (ret < 0) {
794                         TESTPMD_LOG(ERR, "Cannot create heap\n");
795                         return -1;
796                 }
797         }
798
799         ret = create_extmem(nb_mbufs, mbuf_sz, &param, huge);
800         if (ret < 0) {
801                 TESTPMD_LOG(ERR, "Cannot create memory area\n");
802                 return -1;
803         }
804
805         /* we now have a valid memory area, so add it to heap */
806         ret = rte_malloc_heap_memory_add(EXTMEM_HEAP_NAME,
807                         param.addr, param.len, param.iova_table,
808                         param.iova_table_len, param.pgsz);
809
810         /* when using VFIO, memory is automatically mapped for DMA by EAL */
811
812         /* not needed any more */
813         free(param.iova_table);
814
815         if (ret < 0) {
816                 TESTPMD_LOG(ERR, "Cannot add memory to heap\n");
817                 munmap(param.addr, param.len);
818                 return -1;
819         }
820
821         /* success */
822
823         TESTPMD_LOG(DEBUG, "Allocated %zuMB of external memory\n",
824                         param.len >> 20);
825
826         return 0;
827 }
828
829 /*
830  * Configuration initialisation done once at init time.
831  */
832 static void
833 mbuf_pool_create(uint16_t mbuf_seg_size, unsigned nb_mbuf,
834                  unsigned int socket_id)
835 {
836         char pool_name[RTE_MEMPOOL_NAMESIZE];
837         struct rte_mempool *rte_mp = NULL;
838         uint32_t mb_size;
839
840         mb_size = sizeof(struct rte_mbuf) + mbuf_seg_size;
841         mbuf_poolname_build(socket_id, pool_name, sizeof(pool_name));
842
843         TESTPMD_LOG(INFO,
844                 "create a new mbuf pool <%s>: n=%u, size=%u, socket=%u\n",
845                 pool_name, nb_mbuf, mbuf_seg_size, socket_id);
846
847         switch (mp_alloc_type) {
848         case MP_ALLOC_NATIVE:
849                 {
850                         /* wrapper to rte_mempool_create() */
851                         TESTPMD_LOG(INFO, "preferred mempool ops selected: %s\n",
852                                         rte_mbuf_best_mempool_ops());
853                         rte_mp = rte_pktmbuf_pool_create(pool_name, nb_mbuf,
854                                 mb_mempool_cache, 0, mbuf_seg_size, socket_id);
855                         break;
856                 }
857         case MP_ALLOC_ANON:
858                 {
859                         rte_mp = rte_mempool_create_empty(pool_name, nb_mbuf,
860                                 mb_size, (unsigned int) mb_mempool_cache,
861                                 sizeof(struct rte_pktmbuf_pool_private),
862                                 socket_id, 0);
863                         if (rte_mp == NULL)
864                                 goto err;
865
866                         if (rte_mempool_populate_anon(rte_mp) == 0) {
867                                 rte_mempool_free(rte_mp);
868                                 rte_mp = NULL;
869                                 goto err;
870                         }
871                         rte_pktmbuf_pool_init(rte_mp, NULL);
872                         rte_mempool_obj_iter(rte_mp, rte_pktmbuf_init, NULL);
873                         break;
874                 }
875         case MP_ALLOC_XMEM:
876         case MP_ALLOC_XMEM_HUGE:
877                 {
878                         int heap_socket;
879                         bool huge = mp_alloc_type == MP_ALLOC_XMEM_HUGE;
880
881                         if (setup_extmem(nb_mbuf, mbuf_seg_size, huge) < 0)
882                                 rte_exit(EXIT_FAILURE, "Could not create external memory\n");
883
884                         heap_socket =
885                                 rte_malloc_heap_get_socket(EXTMEM_HEAP_NAME);
886                         if (heap_socket < 0)
887                                 rte_exit(EXIT_FAILURE, "Could not get external memory socket ID\n");
888
889                         TESTPMD_LOG(INFO, "preferred mempool ops selected: %s\n",
890                                         rte_mbuf_best_mempool_ops());
891                         rte_mp = rte_pktmbuf_pool_create(pool_name, nb_mbuf,
892                                         mb_mempool_cache, 0, mbuf_seg_size,
893                                         heap_socket);
894                         break;
895                 }
896         default:
897                 {
898                         rte_exit(EXIT_FAILURE, "Invalid mempool creation mode\n");
899                 }
900         }
901
902 err:
903         if (rte_mp == NULL) {
904                 rte_exit(EXIT_FAILURE,
905                         "Creation of mbuf pool for socket %u failed: %s\n",
906                         socket_id, rte_strerror(rte_errno));
907         } else if (verbose_level > 0) {
908                 rte_mempool_dump(stdout, rte_mp);
909         }
910 }
911
912 /*
913  * Check given socket id is valid or not with NUMA mode,
914  * if valid, return 0, else return -1
915  */
916 static int
917 check_socket_id(const unsigned int socket_id)
918 {
919         static int warning_once = 0;
920
921         if (new_socket_id(socket_id)) {
922                 if (!warning_once && numa_support)
923                         printf("Warning: NUMA should be configured manually by"
924                                " using --port-numa-config and"
925                                " --ring-numa-config parameters along with"
926                                " --numa.\n");
927                 warning_once = 1;
928                 return -1;
929         }
930         return 0;
931 }
932
933 /*
934  * Get the allowed maximum number of RX queues.
935  * *pid return the port id which has minimal value of
936  * max_rx_queues in all ports.
937  */
938 queueid_t
939 get_allowed_max_nb_rxq(portid_t *pid)
940 {
941         queueid_t allowed_max_rxq = MAX_QUEUE_ID;
942         portid_t pi;
943         struct rte_eth_dev_info dev_info;
944
945         RTE_ETH_FOREACH_DEV(pi) {
946                 rte_eth_dev_info_get(pi, &dev_info);
947                 if (dev_info.max_rx_queues < allowed_max_rxq) {
948                         allowed_max_rxq = dev_info.max_rx_queues;
949                         *pid = pi;
950                 }
951         }
952         return allowed_max_rxq;
953 }
954
955 /*
956  * Check input rxq is valid or not.
957  * If input rxq is not greater than any of maximum number
958  * of RX queues of all ports, it is valid.
959  * if valid, return 0, else return -1
960  */
961 int
962 check_nb_rxq(queueid_t rxq)
963 {
964         queueid_t allowed_max_rxq;
965         portid_t pid = 0;
966
967         allowed_max_rxq = get_allowed_max_nb_rxq(&pid);
968         if (rxq > allowed_max_rxq) {
969                 printf("Fail: input rxq (%u) can't be greater "
970                        "than max_rx_queues (%u) of port %u\n",
971                        rxq,
972                        allowed_max_rxq,
973                        pid);
974                 return -1;
975         }
976         return 0;
977 }
978
979 /*
980  * Get the allowed maximum number of TX queues.
981  * *pid return the port id which has minimal value of
982  * max_tx_queues in all ports.
983  */
984 queueid_t
985 get_allowed_max_nb_txq(portid_t *pid)
986 {
987         queueid_t allowed_max_txq = MAX_QUEUE_ID;
988         portid_t pi;
989         struct rte_eth_dev_info dev_info;
990
991         RTE_ETH_FOREACH_DEV(pi) {
992                 rte_eth_dev_info_get(pi, &dev_info);
993                 if (dev_info.max_tx_queues < allowed_max_txq) {
994                         allowed_max_txq = dev_info.max_tx_queues;
995                         *pid = pi;
996                 }
997         }
998         return allowed_max_txq;
999 }
1000
1001 /*
1002  * Check input txq is valid or not.
1003  * If input txq is not greater than any of maximum number
1004  * of TX queues of all ports, it is valid.
1005  * if valid, return 0, else return -1
1006  */
1007 int
1008 check_nb_txq(queueid_t txq)
1009 {
1010         queueid_t allowed_max_txq;
1011         portid_t pid = 0;
1012
1013         allowed_max_txq = get_allowed_max_nb_txq(&pid);
1014         if (txq > allowed_max_txq) {
1015                 printf("Fail: input txq (%u) can't be greater "
1016                        "than max_tx_queues (%u) of port %u\n",
1017                        txq,
1018                        allowed_max_txq,
1019                        pid);
1020                 return -1;
1021         }
1022         return 0;
1023 }
1024
1025 static void
1026 init_config(void)
1027 {
1028         portid_t pid;
1029         struct rte_port *port;
1030         struct rte_mempool *mbp;
1031         unsigned int nb_mbuf_per_pool;
1032         lcoreid_t  lc_id;
1033         uint8_t port_per_socket[RTE_MAX_NUMA_NODES];
1034         struct rte_gro_param gro_param;
1035         uint32_t gso_types;
1036         int k;
1037
1038         memset(port_per_socket,0,RTE_MAX_NUMA_NODES);
1039
1040         /* Configuration of logical cores. */
1041         fwd_lcores = rte_zmalloc("testpmd: fwd_lcores",
1042                                 sizeof(struct fwd_lcore *) * nb_lcores,
1043                                 RTE_CACHE_LINE_SIZE);
1044         if (fwd_lcores == NULL) {
1045                 rte_exit(EXIT_FAILURE, "rte_zmalloc(%d (struct fwd_lcore *)) "
1046                                                         "failed\n", nb_lcores);
1047         }
1048         for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
1049                 fwd_lcores[lc_id] = rte_zmalloc("testpmd: struct fwd_lcore",
1050                                                sizeof(struct fwd_lcore),
1051                                                RTE_CACHE_LINE_SIZE);
1052                 if (fwd_lcores[lc_id] == NULL) {
1053                         rte_exit(EXIT_FAILURE, "rte_zmalloc(struct fwd_lcore) "
1054                                                                 "failed\n");
1055                 }
1056                 fwd_lcores[lc_id]->cpuid_idx = lc_id;
1057         }
1058
1059         RTE_ETH_FOREACH_DEV(pid) {
1060                 port = &ports[pid];
1061                 /* Apply default TxRx configuration for all ports */
1062                 port->dev_conf.txmode = tx_mode;
1063                 port->dev_conf.rxmode = rx_mode;
1064                 rte_eth_dev_info_get(pid, &port->dev_info);
1065
1066                 if (!(port->dev_info.tx_offload_capa &
1067                       DEV_TX_OFFLOAD_MBUF_FAST_FREE))
1068                         port->dev_conf.txmode.offloads &=
1069                                 ~DEV_TX_OFFLOAD_MBUF_FAST_FREE;
1070                 if (!(port->dev_info.tx_offload_capa &
1071                         DEV_TX_OFFLOAD_MATCH_METADATA))
1072                         port->dev_conf.txmode.offloads &=
1073                                 ~DEV_TX_OFFLOAD_MATCH_METADATA;
1074                 if (numa_support) {
1075                         if (port_numa[pid] != NUMA_NO_CONFIG)
1076                                 port_per_socket[port_numa[pid]]++;
1077                         else {
1078                                 uint32_t socket_id = rte_eth_dev_socket_id(pid);
1079
1080                                 /*
1081                                  * if socket_id is invalid,
1082                                  * set to the first available socket.
1083                                  */
1084                                 if (check_socket_id(socket_id) < 0)
1085                                         socket_id = socket_ids[0];
1086                                 port_per_socket[socket_id]++;
1087                         }
1088                 }
1089
1090                 /* Apply Rx offloads configuration */
1091                 for (k = 0; k < port->dev_info.max_rx_queues; k++)
1092                         port->rx_conf[k].offloads =
1093                                 port->dev_conf.rxmode.offloads;
1094                 /* Apply Tx offloads configuration */
1095                 for (k = 0; k < port->dev_info.max_tx_queues; k++)
1096                         port->tx_conf[k].offloads =
1097                                 port->dev_conf.txmode.offloads;
1098
1099                 /* set flag to initialize port/queue */
1100                 port->need_reconfig = 1;
1101                 port->need_reconfig_queues = 1;
1102                 port->tx_metadata = 0;
1103         }
1104
1105         /*
1106          * Create pools of mbuf.
1107          * If NUMA support is disabled, create a single pool of mbuf in
1108          * socket 0 memory by default.
1109          * Otherwise, create a pool of mbuf in the memory of sockets 0 and 1.
1110          *
1111          * Use the maximum value of nb_rxd and nb_txd here, then nb_rxd and
1112          * nb_txd can be configured at run time.
1113          */
1114         if (param_total_num_mbufs)
1115                 nb_mbuf_per_pool = param_total_num_mbufs;
1116         else {
1117                 nb_mbuf_per_pool = RTE_TEST_RX_DESC_MAX +
1118                         (nb_lcores * mb_mempool_cache) +
1119                         RTE_TEST_TX_DESC_MAX + MAX_PKT_BURST;
1120                 nb_mbuf_per_pool *= RTE_MAX_ETHPORTS;
1121         }
1122
1123         if (numa_support) {
1124                 uint8_t i;
1125
1126                 for (i = 0; i < num_sockets; i++)
1127                         mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool,
1128                                          socket_ids[i]);
1129         } else {
1130                 if (socket_num == UMA_NO_CONFIG)
1131                         mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool, 0);
1132                 else
1133                         mbuf_pool_create(mbuf_data_size, nb_mbuf_per_pool,
1134                                                  socket_num);
1135         }
1136
1137         init_port_config();
1138
1139         gso_types = DEV_TX_OFFLOAD_TCP_TSO | DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1140                 DEV_TX_OFFLOAD_GRE_TNL_TSO | DEV_TX_OFFLOAD_UDP_TSO;
1141         /*
1142          * Records which Mbuf pool to use by each logical core, if needed.
1143          */
1144         for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
1145                 mbp = mbuf_pool_find(
1146                         rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]));
1147
1148                 if (mbp == NULL)
1149                         mbp = mbuf_pool_find(0);
1150                 fwd_lcores[lc_id]->mbp = mbp;
1151                 /* initialize GSO context */
1152                 fwd_lcores[lc_id]->gso_ctx.direct_pool = mbp;
1153                 fwd_lcores[lc_id]->gso_ctx.indirect_pool = mbp;
1154                 fwd_lcores[lc_id]->gso_ctx.gso_types = gso_types;
1155                 fwd_lcores[lc_id]->gso_ctx.gso_size = ETHER_MAX_LEN -
1156                         ETHER_CRC_LEN;
1157                 fwd_lcores[lc_id]->gso_ctx.flag = 0;
1158         }
1159
1160         /* Configuration of packet forwarding streams. */
1161         if (init_fwd_streams() < 0)
1162                 rte_exit(EXIT_FAILURE, "FAIL from init_fwd_streams()\n");
1163
1164         fwd_config_setup();
1165
1166         /* create a gro context for each lcore */
1167         gro_param.gro_types = RTE_GRO_TCP_IPV4;
1168         gro_param.max_flow_num = GRO_MAX_FLUSH_CYCLES;
1169         gro_param.max_item_per_flow = MAX_PKT_BURST;
1170         for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
1171                 gro_param.socket_id = rte_lcore_to_socket_id(
1172                                 fwd_lcores_cpuids[lc_id]);
1173                 fwd_lcores[lc_id]->gro_ctx = rte_gro_ctx_create(&gro_param);
1174                 if (fwd_lcores[lc_id]->gro_ctx == NULL) {
1175                         rte_exit(EXIT_FAILURE,
1176                                         "rte_gro_ctx_create() failed\n");
1177                 }
1178         }
1179
1180 #if defined RTE_LIBRTE_PMD_SOFTNIC
1181         if (strcmp(cur_fwd_eng->fwd_mode_name, "softnic") == 0) {
1182                 RTE_ETH_FOREACH_DEV(pid) {
1183                         port = &ports[pid];
1184                         const char *driver = port->dev_info.driver_name;
1185
1186                         if (strcmp(driver, "net_softnic") == 0)
1187                                 port->softport.fwd_lcore_arg = fwd_lcores;
1188                 }
1189         }
1190 #endif
1191
1192 }
1193
1194
1195 void
1196 reconfig(portid_t new_port_id, unsigned socket_id)
1197 {
1198         struct rte_port *port;
1199
1200         /* Reconfiguration of Ethernet ports. */
1201         port = &ports[new_port_id];
1202         rte_eth_dev_info_get(new_port_id, &port->dev_info);
1203
1204         /* set flag to initialize port/queue */
1205         port->need_reconfig = 1;
1206         port->need_reconfig_queues = 1;
1207         port->socket_id = socket_id;
1208
1209         init_port_config();
1210 }
1211
1212
1213 int
1214 init_fwd_streams(void)
1215 {
1216         portid_t pid;
1217         struct rte_port *port;
1218         streamid_t sm_id, nb_fwd_streams_new;
1219         queueid_t q;
1220
1221         /* set socket id according to numa or not */
1222         RTE_ETH_FOREACH_DEV(pid) {
1223                 port = &ports[pid];
1224                 if (nb_rxq > port->dev_info.max_rx_queues) {
1225                         printf("Fail: nb_rxq(%d) is greater than "
1226                                 "max_rx_queues(%d)\n", nb_rxq,
1227                                 port->dev_info.max_rx_queues);
1228                         return -1;
1229                 }
1230                 if (nb_txq > port->dev_info.max_tx_queues) {
1231                         printf("Fail: nb_txq(%d) is greater than "
1232                                 "max_tx_queues(%d)\n", nb_txq,
1233                                 port->dev_info.max_tx_queues);
1234                         return -1;
1235                 }
1236                 if (numa_support) {
1237                         if (port_numa[pid] != NUMA_NO_CONFIG)
1238                                 port->socket_id = port_numa[pid];
1239                         else {
1240                                 port->socket_id = rte_eth_dev_socket_id(pid);
1241
1242                                 /*
1243                                  * if socket_id is invalid,
1244                                  * set to the first available socket.
1245                                  */
1246                                 if (check_socket_id(port->socket_id) < 0)
1247                                         port->socket_id = socket_ids[0];
1248                         }
1249                 }
1250                 else {
1251                         if (socket_num == UMA_NO_CONFIG)
1252                                 port->socket_id = 0;
1253                         else
1254                                 port->socket_id = socket_num;
1255                 }
1256         }
1257
1258         q = RTE_MAX(nb_rxq, nb_txq);
1259         if (q == 0) {
1260                 printf("Fail: Cannot allocate fwd streams as number of queues is 0\n");
1261                 return -1;
1262         }
1263         nb_fwd_streams_new = (streamid_t)(nb_ports * q);
1264         if (nb_fwd_streams_new == nb_fwd_streams)
1265                 return 0;
1266         /* clear the old */
1267         if (fwd_streams != NULL) {
1268                 for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
1269                         if (fwd_streams[sm_id] == NULL)
1270                                 continue;
1271                         rte_free(fwd_streams[sm_id]);
1272                         fwd_streams[sm_id] = NULL;
1273                 }
1274                 rte_free(fwd_streams);
1275                 fwd_streams = NULL;
1276         }
1277
1278         /* init new */
1279         nb_fwd_streams = nb_fwd_streams_new;
1280         if (nb_fwd_streams) {
1281                 fwd_streams = rte_zmalloc("testpmd: fwd_streams",
1282                         sizeof(struct fwd_stream *) * nb_fwd_streams,
1283                         RTE_CACHE_LINE_SIZE);
1284                 if (fwd_streams == NULL)
1285                         rte_exit(EXIT_FAILURE, "rte_zmalloc(%d"
1286                                  " (struct fwd_stream *)) failed\n",
1287                                  nb_fwd_streams);
1288
1289                 for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
1290                         fwd_streams[sm_id] = rte_zmalloc("testpmd:"
1291                                 " struct fwd_stream", sizeof(struct fwd_stream),
1292                                 RTE_CACHE_LINE_SIZE);
1293                         if (fwd_streams[sm_id] == NULL)
1294                                 rte_exit(EXIT_FAILURE, "rte_zmalloc"
1295                                          "(struct fwd_stream) failed\n");
1296                 }
1297         }
1298
1299         return 0;
1300 }
1301
1302 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
1303 static void
1304 pkt_burst_stats_display(const char *rx_tx, struct pkt_burst_stats *pbs)
1305 {
1306         unsigned int total_burst;
1307         unsigned int nb_burst;
1308         unsigned int burst_stats[3];
1309         uint16_t pktnb_stats[3];
1310         uint16_t nb_pkt;
1311         int burst_percent[3];
1312
1313         /*
1314          * First compute the total number of packet bursts and the
1315          * two highest numbers of bursts of the same number of packets.
1316          */
1317         total_burst = 0;
1318         burst_stats[0] = burst_stats[1] = burst_stats[2] = 0;
1319         pktnb_stats[0] = pktnb_stats[1] = pktnb_stats[2] = 0;
1320         for (nb_pkt = 0; nb_pkt < MAX_PKT_BURST; nb_pkt++) {
1321                 nb_burst = pbs->pkt_burst_spread[nb_pkt];
1322                 if (nb_burst == 0)
1323                         continue;
1324                 total_burst += nb_burst;
1325                 if (nb_burst > burst_stats[0]) {
1326                         burst_stats[1] = burst_stats[0];
1327                         pktnb_stats[1] = pktnb_stats[0];
1328                         burst_stats[0] = nb_burst;
1329                         pktnb_stats[0] = nb_pkt;
1330                 } else if (nb_burst > burst_stats[1]) {
1331                         burst_stats[1] = nb_burst;
1332                         pktnb_stats[1] = nb_pkt;
1333                 }
1334         }
1335         if (total_burst == 0)
1336                 return;
1337         burst_percent[0] = (burst_stats[0] * 100) / total_burst;
1338         printf("  %s-bursts : %u [%d%% of %d pkts", rx_tx, total_burst,
1339                burst_percent[0], (int) pktnb_stats[0]);
1340         if (burst_stats[0] == total_burst) {
1341                 printf("]\n");
1342                 return;
1343         }
1344         if (burst_stats[0] + burst_stats[1] == total_burst) {
1345                 printf(" + %d%% of %d pkts]\n",
1346                        100 - burst_percent[0], pktnb_stats[1]);
1347                 return;
1348         }
1349         burst_percent[1] = (burst_stats[1] * 100) / total_burst;
1350         burst_percent[2] = 100 - (burst_percent[0] + burst_percent[1]);
1351         if ((burst_percent[1] == 0) || (burst_percent[2] == 0)) {
1352                 printf(" + %d%% of others]\n", 100 - burst_percent[0]);
1353                 return;
1354         }
1355         printf(" + %d%% of %d pkts + %d%% of others]\n",
1356                burst_percent[1], (int) pktnb_stats[1], burst_percent[2]);
1357 }
1358 #endif /* RTE_TEST_PMD_RECORD_BURST_STATS */
1359
1360 static void
1361 fwd_port_stats_display(portid_t port_id, struct rte_eth_stats *stats)
1362 {
1363         struct rte_port *port;
1364         uint8_t i;
1365
1366         static const char *fwd_stats_border = "----------------------";
1367
1368         port = &ports[port_id];
1369         printf("\n  %s Forward statistics for port %-2d %s\n",
1370                fwd_stats_border, port_id, fwd_stats_border);
1371
1372         if ((!port->rx_queue_stats_mapping_enabled) && (!port->tx_queue_stats_mapping_enabled)) {
1373                 printf("  RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64"RX-total: "
1374                        "%-"PRIu64"\n",
1375                        stats->ipackets, stats->imissed,
1376                        (uint64_t) (stats->ipackets + stats->imissed));
1377
1378                 if (cur_fwd_eng == &csum_fwd_engine)
1379                         printf("  Bad-ipcsum: %-14"PRIu64" Bad-l4csum: %-14"PRIu64"Bad-outer-l4csum: %-14"PRIu64"\n",
1380                                port->rx_bad_ip_csum, port->rx_bad_l4_csum,
1381                                port->rx_bad_outer_l4_csum);
1382                 if ((stats->ierrors + stats->rx_nombuf) > 0) {
1383                         printf("  RX-error: %-"PRIu64"\n",  stats->ierrors);
1384                         printf("  RX-nombufs: %-14"PRIu64"\n", stats->rx_nombuf);
1385                 }
1386
1387                 printf("  TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64"TX-total: "
1388                        "%-"PRIu64"\n",
1389                        stats->opackets, port->tx_dropped,
1390                        (uint64_t) (stats->opackets + port->tx_dropped));
1391         }
1392         else {
1393                 printf("  RX-packets:             %14"PRIu64"    RX-dropped:%14"PRIu64"    RX-total:"
1394                        "%14"PRIu64"\n",
1395                        stats->ipackets, stats->imissed,
1396                        (uint64_t) (stats->ipackets + stats->imissed));
1397
1398                 if (cur_fwd_eng == &csum_fwd_engine)
1399                         printf("  Bad-ipcsum:%14"PRIu64"    Bad-l4csum:%14"PRIu64"    Bad-outer-l4csum: %-14"PRIu64"\n",
1400                                port->rx_bad_ip_csum, port->rx_bad_l4_csum,
1401                                port->rx_bad_outer_l4_csum);
1402                 if ((stats->ierrors + stats->rx_nombuf) > 0) {
1403                         printf("  RX-error:%"PRIu64"\n", stats->ierrors);
1404                         printf("  RX-nombufs:             %14"PRIu64"\n",
1405                                stats->rx_nombuf);
1406                 }
1407
1408                 printf("  TX-packets:             %14"PRIu64"    TX-dropped:%14"PRIu64"    TX-total:"
1409                        "%14"PRIu64"\n",
1410                        stats->opackets, port->tx_dropped,
1411                        (uint64_t) (stats->opackets + port->tx_dropped));
1412         }
1413
1414 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
1415         if (port->rx_stream)
1416                 pkt_burst_stats_display("RX",
1417                         &port->rx_stream->rx_burst_stats);
1418         if (port->tx_stream)
1419                 pkt_burst_stats_display("TX",
1420                         &port->tx_stream->tx_burst_stats);
1421 #endif
1422
1423         if (port->rx_queue_stats_mapping_enabled) {
1424                 printf("\n");
1425                 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
1426                         printf("  Stats reg %2d RX-packets:%14"PRIu64
1427                                "     RX-errors:%14"PRIu64
1428                                "    RX-bytes:%14"PRIu64"\n",
1429                                i, stats->q_ipackets[i], stats->q_errors[i], stats->q_ibytes[i]);
1430                 }
1431                 printf("\n");
1432         }
1433         if (port->tx_queue_stats_mapping_enabled) {
1434                 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
1435                         printf("  Stats reg %2d TX-packets:%14"PRIu64
1436                                "                                 TX-bytes:%14"PRIu64"\n",
1437                                i, stats->q_opackets[i], stats->q_obytes[i]);
1438                 }
1439         }
1440
1441         printf("  %s--------------------------------%s\n",
1442                fwd_stats_border, fwd_stats_border);
1443 }
1444
1445 static void
1446 fwd_stream_stats_display(streamid_t stream_id)
1447 {
1448         struct fwd_stream *fs;
1449         static const char *fwd_top_stats_border = "-------";
1450
1451         fs = fwd_streams[stream_id];
1452         if ((fs->rx_packets == 0) && (fs->tx_packets == 0) &&
1453             (fs->fwd_dropped == 0))
1454                 return;
1455         printf("\n  %s Forward Stats for RX Port=%2d/Queue=%2d -> "
1456                "TX Port=%2d/Queue=%2d %s\n",
1457                fwd_top_stats_border, fs->rx_port, fs->rx_queue,
1458                fs->tx_port, fs->tx_queue, fwd_top_stats_border);
1459         printf("  RX-packets: %-14u TX-packets: %-14u TX-dropped: %-14u",
1460                fs->rx_packets, fs->tx_packets, fs->fwd_dropped);
1461
1462         /* if checksum mode */
1463         if (cur_fwd_eng == &csum_fwd_engine) {
1464                printf("  RX- bad IP checksum: %-14u  Rx- bad L4 checksum: "
1465                         "%-14u Rx- bad outer L4 checksum: %-14u\n",
1466                         fs->rx_bad_ip_csum, fs->rx_bad_l4_csum,
1467                         fs->rx_bad_outer_l4_csum);
1468         }
1469
1470 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
1471         pkt_burst_stats_display("RX", &fs->rx_burst_stats);
1472         pkt_burst_stats_display("TX", &fs->tx_burst_stats);
1473 #endif
1474 }
1475
1476 static void
1477 flush_fwd_rx_queues(void)
1478 {
1479         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1480         portid_t  rxp;
1481         portid_t port_id;
1482         queueid_t rxq;
1483         uint16_t  nb_rx;
1484         uint16_t  i;
1485         uint8_t   j;
1486         uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
1487         uint64_t timer_period;
1488
1489         /* convert to number of cycles */
1490         timer_period = rte_get_timer_hz(); /* 1 second timeout */
1491
1492         for (j = 0; j < 2; j++) {
1493                 for (rxp = 0; rxp < cur_fwd_config.nb_fwd_ports; rxp++) {
1494                         for (rxq = 0; rxq < nb_rxq; rxq++) {
1495                                 port_id = fwd_ports_ids[rxp];
1496                                 /**
1497                                 * testpmd can stuck in the below do while loop
1498                                 * if rte_eth_rx_burst() always returns nonzero
1499                                 * packets. So timer is added to exit this loop
1500                                 * after 1sec timer expiry.
1501                                 */
1502                                 prev_tsc = rte_rdtsc();
1503                                 do {
1504                                         nb_rx = rte_eth_rx_burst(port_id, rxq,
1505                                                 pkts_burst, MAX_PKT_BURST);
1506                                         for (i = 0; i < nb_rx; i++)
1507                                                 rte_pktmbuf_free(pkts_burst[i]);
1508
1509                                         cur_tsc = rte_rdtsc();
1510                                         diff_tsc = cur_tsc - prev_tsc;
1511                                         timer_tsc += diff_tsc;
1512                                 } while ((nb_rx > 0) &&
1513                                         (timer_tsc < timer_period));
1514                                 timer_tsc = 0;
1515                         }
1516                 }
1517                 rte_delay_ms(10); /* wait 10 milli-seconds before retrying */
1518         }
1519 }
1520
1521 static void
1522 run_pkt_fwd_on_lcore(struct fwd_lcore *fc, packet_fwd_t pkt_fwd)
1523 {
1524         struct fwd_stream **fsm;
1525         streamid_t nb_fs;
1526         streamid_t sm_id;
1527 #ifdef RTE_LIBRTE_BITRATE
1528         uint64_t tics_per_1sec;
1529         uint64_t tics_datum;
1530         uint64_t tics_current;
1531         uint16_t i, cnt_ports;
1532
1533         cnt_ports = nb_ports;
1534         tics_datum = rte_rdtsc();
1535         tics_per_1sec = rte_get_timer_hz();
1536 #endif
1537         fsm = &fwd_streams[fc->stream_idx];
1538         nb_fs = fc->stream_nb;
1539         do {
1540                 for (sm_id = 0; sm_id < nb_fs; sm_id++)
1541                         (*pkt_fwd)(fsm[sm_id]);
1542 #ifdef RTE_LIBRTE_BITRATE
1543                 if (bitrate_enabled != 0 &&
1544                                 bitrate_lcore_id == rte_lcore_id()) {
1545                         tics_current = rte_rdtsc();
1546                         if (tics_current - tics_datum >= tics_per_1sec) {
1547                                 /* Periodic bitrate calculation */
1548                                 for (i = 0; i < cnt_ports; i++)
1549                                         rte_stats_bitrate_calc(bitrate_data,
1550                                                 ports_ids[i]);
1551                                 tics_datum = tics_current;
1552                         }
1553                 }
1554 #endif
1555 #ifdef RTE_LIBRTE_LATENCY_STATS
1556                 if (latencystats_enabled != 0 &&
1557                                 latencystats_lcore_id == rte_lcore_id())
1558                         rte_latencystats_update();
1559 #endif
1560
1561         } while (! fc->stopped);
1562 }
1563
1564 static int
1565 start_pkt_forward_on_core(void *fwd_arg)
1566 {
1567         run_pkt_fwd_on_lcore((struct fwd_lcore *) fwd_arg,
1568                              cur_fwd_config.fwd_eng->packet_fwd);
1569         return 0;
1570 }
1571
1572 /*
1573  * Run the TXONLY packet forwarding engine to send a single burst of packets.
1574  * Used to start communication flows in network loopback test configurations.
1575  */
1576 static int
1577 run_one_txonly_burst_on_core(void *fwd_arg)
1578 {
1579         struct fwd_lcore *fwd_lc;
1580         struct fwd_lcore tmp_lcore;
1581
1582         fwd_lc = (struct fwd_lcore *) fwd_arg;
1583         tmp_lcore = *fwd_lc;
1584         tmp_lcore.stopped = 1;
1585         run_pkt_fwd_on_lcore(&tmp_lcore, tx_only_engine.packet_fwd);
1586         return 0;
1587 }
1588
1589 /*
1590  * Launch packet forwarding:
1591  *     - Setup per-port forwarding context.
1592  *     - launch logical cores with their forwarding configuration.
1593  */
1594 static void
1595 launch_packet_forwarding(lcore_function_t *pkt_fwd_on_lcore)
1596 {
1597         port_fwd_begin_t port_fwd_begin;
1598         unsigned int i;
1599         unsigned int lc_id;
1600         int diag;
1601
1602         port_fwd_begin = cur_fwd_config.fwd_eng->port_fwd_begin;
1603         if (port_fwd_begin != NULL) {
1604                 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
1605                         (*port_fwd_begin)(fwd_ports_ids[i]);
1606         }
1607         for (i = 0; i < cur_fwd_config.nb_fwd_lcores; i++) {
1608                 lc_id = fwd_lcores_cpuids[i];
1609                 if ((interactive == 0) || (lc_id != rte_lcore_id())) {
1610                         fwd_lcores[i]->stopped = 0;
1611                         diag = rte_eal_remote_launch(pkt_fwd_on_lcore,
1612                                                      fwd_lcores[i], lc_id);
1613                         if (diag != 0)
1614                                 printf("launch lcore %u failed - diag=%d\n",
1615                                        lc_id, diag);
1616                 }
1617         }
1618 }
1619
1620 /*
1621  * Launch packet forwarding configuration.
1622  */
1623 void
1624 start_packet_forwarding(int with_tx_first)
1625 {
1626         port_fwd_begin_t port_fwd_begin;
1627         port_fwd_end_t  port_fwd_end;
1628         struct rte_port *port;
1629         unsigned int i;
1630         portid_t   pt_id;
1631         streamid_t sm_id;
1632
1633         if (strcmp(cur_fwd_eng->fwd_mode_name, "rxonly") == 0 && !nb_rxq)
1634                 rte_exit(EXIT_FAILURE, "rxq are 0, cannot use rxonly fwd mode\n");
1635
1636         if (strcmp(cur_fwd_eng->fwd_mode_name, "txonly") == 0 && !nb_txq)
1637                 rte_exit(EXIT_FAILURE, "txq are 0, cannot use txonly fwd mode\n");
1638
1639         if ((strcmp(cur_fwd_eng->fwd_mode_name, "rxonly") != 0 &&
1640                 strcmp(cur_fwd_eng->fwd_mode_name, "txonly") != 0) &&
1641                 (!nb_rxq || !nb_txq))
1642                 rte_exit(EXIT_FAILURE,
1643                         "Either rxq or txq are 0, cannot use %s fwd mode\n",
1644                         cur_fwd_eng->fwd_mode_name);
1645
1646         if (all_ports_started() == 0) {
1647                 printf("Not all ports were started\n");
1648                 return;
1649         }
1650         if (test_done == 0) {
1651                 printf("Packet forwarding already started\n");
1652                 return;
1653         }
1654
1655
1656         if(dcb_test) {
1657                 for (i = 0; i < nb_fwd_ports; i++) {
1658                         pt_id = fwd_ports_ids[i];
1659                         port = &ports[pt_id];
1660                         if (!port->dcb_flag) {
1661                                 printf("In DCB mode, all forwarding ports must "
1662                                        "be configured in this mode.\n");
1663                                 return;
1664                         }
1665                 }
1666                 if (nb_fwd_lcores == 1) {
1667                         printf("In DCB mode,the nb forwarding cores "
1668                                "should be larger than 1.\n");
1669                         return;
1670                 }
1671         }
1672         test_done = 0;
1673
1674         fwd_config_setup();
1675
1676         if(!no_flush_rx)
1677                 flush_fwd_rx_queues();
1678
1679         pkt_fwd_config_display(&cur_fwd_config);
1680         rxtx_config_display();
1681
1682         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
1683                 pt_id = fwd_ports_ids[i];
1684                 port = &ports[pt_id];
1685                 rte_eth_stats_get(pt_id, &port->stats);
1686                 port->tx_dropped = 0;
1687
1688                 map_port_queue_stats_mapping_registers(pt_id, port);
1689         }
1690         for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
1691                 fwd_streams[sm_id]->rx_packets = 0;
1692                 fwd_streams[sm_id]->tx_packets = 0;
1693                 fwd_streams[sm_id]->fwd_dropped = 0;
1694                 fwd_streams[sm_id]->rx_bad_ip_csum = 0;
1695                 fwd_streams[sm_id]->rx_bad_l4_csum = 0;
1696                 fwd_streams[sm_id]->rx_bad_outer_l4_csum = 0;
1697
1698 #ifdef RTE_TEST_PMD_RECORD_BURST_STATS
1699                 memset(&fwd_streams[sm_id]->rx_burst_stats, 0,
1700                        sizeof(fwd_streams[sm_id]->rx_burst_stats));
1701                 memset(&fwd_streams[sm_id]->tx_burst_stats, 0,
1702                        sizeof(fwd_streams[sm_id]->tx_burst_stats));
1703 #endif
1704 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1705                 fwd_streams[sm_id]->core_cycles = 0;
1706 #endif
1707         }
1708         if (with_tx_first) {
1709                 port_fwd_begin = tx_only_engine.port_fwd_begin;
1710                 if (port_fwd_begin != NULL) {
1711                         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
1712                                 (*port_fwd_begin)(fwd_ports_ids[i]);
1713                 }
1714                 while (with_tx_first--) {
1715                         launch_packet_forwarding(
1716                                         run_one_txonly_burst_on_core);
1717                         rte_eal_mp_wait_lcore();
1718                 }
1719                 port_fwd_end = tx_only_engine.port_fwd_end;
1720                 if (port_fwd_end != NULL) {
1721                         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
1722                                 (*port_fwd_end)(fwd_ports_ids[i]);
1723                 }
1724         }
1725         launch_packet_forwarding(start_pkt_forward_on_core);
1726 }
1727
1728 void
1729 stop_packet_forwarding(void)
1730 {
1731         struct rte_eth_stats stats;
1732         struct rte_port *port;
1733         port_fwd_end_t  port_fwd_end;
1734         int i;
1735         portid_t   pt_id;
1736         streamid_t sm_id;
1737         lcoreid_t  lc_id;
1738         uint64_t total_recv;
1739         uint64_t total_xmit;
1740         uint64_t total_rx_dropped;
1741         uint64_t total_tx_dropped;
1742         uint64_t total_rx_nombuf;
1743         uint64_t tx_dropped;
1744         uint64_t rx_bad_ip_csum;
1745         uint64_t rx_bad_l4_csum;
1746 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1747         uint64_t fwd_cycles;
1748 #endif
1749
1750         static const char *acc_stats_border = "+++++++++++++++";
1751
1752         if (test_done) {
1753                 printf("Packet forwarding not started\n");
1754                 return;
1755         }
1756         printf("Telling cores to stop...");
1757         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++)
1758                 fwd_lcores[lc_id]->stopped = 1;
1759         printf("\nWaiting for lcores to finish...\n");
1760         rte_eal_mp_wait_lcore();
1761         port_fwd_end = cur_fwd_config.fwd_eng->port_fwd_end;
1762         if (port_fwd_end != NULL) {
1763                 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
1764                         pt_id = fwd_ports_ids[i];
1765                         (*port_fwd_end)(pt_id);
1766                 }
1767         }
1768 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1769         fwd_cycles = 0;
1770 #endif
1771         for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
1772                 if (cur_fwd_config.nb_fwd_streams >
1773                     cur_fwd_config.nb_fwd_ports) {
1774                         fwd_stream_stats_display(sm_id);
1775                         ports[fwd_streams[sm_id]->tx_port].tx_stream = NULL;
1776                         ports[fwd_streams[sm_id]->rx_port].rx_stream = NULL;
1777                 } else {
1778                         ports[fwd_streams[sm_id]->tx_port].tx_stream =
1779                                 fwd_streams[sm_id];
1780                         ports[fwd_streams[sm_id]->rx_port].rx_stream =
1781                                 fwd_streams[sm_id];
1782                 }
1783                 tx_dropped = ports[fwd_streams[sm_id]->tx_port].tx_dropped;
1784                 tx_dropped = (uint64_t) (tx_dropped +
1785                                          fwd_streams[sm_id]->fwd_dropped);
1786                 ports[fwd_streams[sm_id]->tx_port].tx_dropped = tx_dropped;
1787
1788                 rx_bad_ip_csum =
1789                         ports[fwd_streams[sm_id]->rx_port].rx_bad_ip_csum;
1790                 rx_bad_ip_csum = (uint64_t) (rx_bad_ip_csum +
1791                                          fwd_streams[sm_id]->rx_bad_ip_csum);
1792                 ports[fwd_streams[sm_id]->rx_port].rx_bad_ip_csum =
1793                                                         rx_bad_ip_csum;
1794
1795                 rx_bad_l4_csum =
1796                         ports[fwd_streams[sm_id]->rx_port].rx_bad_l4_csum;
1797                 rx_bad_l4_csum = (uint64_t) (rx_bad_l4_csum +
1798                                          fwd_streams[sm_id]->rx_bad_l4_csum);
1799                 ports[fwd_streams[sm_id]->rx_port].rx_bad_l4_csum =
1800                                                         rx_bad_l4_csum;
1801
1802                 ports[fwd_streams[sm_id]->rx_port].rx_bad_outer_l4_csum +=
1803                                 fwd_streams[sm_id]->rx_bad_outer_l4_csum;
1804
1805 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1806                 fwd_cycles = (uint64_t) (fwd_cycles +
1807                                          fwd_streams[sm_id]->core_cycles);
1808 #endif
1809         }
1810         total_recv = 0;
1811         total_xmit = 0;
1812         total_rx_dropped = 0;
1813         total_tx_dropped = 0;
1814         total_rx_nombuf  = 0;
1815         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
1816                 pt_id = fwd_ports_ids[i];
1817
1818                 port = &ports[pt_id];
1819                 rte_eth_stats_get(pt_id, &stats);
1820                 stats.ipackets -= port->stats.ipackets;
1821                 port->stats.ipackets = 0;
1822                 stats.opackets -= port->stats.opackets;
1823                 port->stats.opackets = 0;
1824                 stats.ibytes   -= port->stats.ibytes;
1825                 port->stats.ibytes = 0;
1826                 stats.obytes   -= port->stats.obytes;
1827                 port->stats.obytes = 0;
1828                 stats.imissed  -= port->stats.imissed;
1829                 port->stats.imissed = 0;
1830                 stats.oerrors  -= port->stats.oerrors;
1831                 port->stats.oerrors = 0;
1832                 stats.rx_nombuf -= port->stats.rx_nombuf;
1833                 port->stats.rx_nombuf = 0;
1834
1835                 total_recv += stats.ipackets;
1836                 total_xmit += stats.opackets;
1837                 total_rx_dropped += stats.imissed;
1838                 total_tx_dropped += port->tx_dropped;
1839                 total_rx_nombuf  += stats.rx_nombuf;
1840
1841                 fwd_port_stats_display(pt_id, &stats);
1842         }
1843
1844         printf("\n  %s Accumulated forward statistics for all ports"
1845                "%s\n",
1846                acc_stats_border, acc_stats_border);
1847         printf("  RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64"RX-total: "
1848                "%-"PRIu64"\n"
1849                "  TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64"TX-total: "
1850                "%-"PRIu64"\n",
1851                total_recv, total_rx_dropped, total_recv + total_rx_dropped,
1852                total_xmit, total_tx_dropped, total_xmit + total_tx_dropped);
1853         if (total_rx_nombuf > 0)
1854                 printf("  RX-nombufs: %-14"PRIu64"\n", total_rx_nombuf);
1855         printf("  %s++++++++++++++++++++++++++++++++++++++++++++++"
1856                "%s\n",
1857                acc_stats_border, acc_stats_border);
1858 #ifdef RTE_TEST_PMD_RECORD_CORE_CYCLES
1859         if (total_recv > 0)
1860                 printf("\n  CPU cycles/packet=%u (total cycles="
1861                        "%"PRIu64" / total RX packets=%"PRIu64")\n",
1862                        (unsigned int)(fwd_cycles / total_recv),
1863                        fwd_cycles, total_recv);
1864 #endif
1865         printf("\nDone.\n");
1866         test_done = 1;
1867 }
1868
1869 void
1870 dev_set_link_up(portid_t pid)
1871 {
1872         if (rte_eth_dev_set_link_up(pid) < 0)
1873                 printf("\nSet link up fail.\n");
1874 }
1875
1876 void
1877 dev_set_link_down(portid_t pid)
1878 {
1879         if (rte_eth_dev_set_link_down(pid) < 0)
1880                 printf("\nSet link down fail.\n");
1881 }
1882
1883 static int
1884 all_ports_started(void)
1885 {
1886         portid_t pi;
1887         struct rte_port *port;
1888
1889         RTE_ETH_FOREACH_DEV(pi) {
1890                 port = &ports[pi];
1891                 /* Check if there is a port which is not started */
1892                 if ((port->port_status != RTE_PORT_STARTED) &&
1893                         (port->slave_flag == 0))
1894                         return 0;
1895         }
1896
1897         /* No port is not started */
1898         return 1;
1899 }
1900
1901 int
1902 port_is_stopped(portid_t port_id)
1903 {
1904         struct rte_port *port = &ports[port_id];
1905
1906         if ((port->port_status != RTE_PORT_STOPPED) &&
1907             (port->slave_flag == 0))
1908                 return 0;
1909         return 1;
1910 }
1911
1912 int
1913 all_ports_stopped(void)
1914 {
1915         portid_t pi;
1916
1917         RTE_ETH_FOREACH_DEV(pi) {
1918                 if (!port_is_stopped(pi))
1919                         return 0;
1920         }
1921
1922         return 1;
1923 }
1924
1925 int
1926 port_is_started(portid_t port_id)
1927 {
1928         if (port_id_is_invalid(port_id, ENABLED_WARN))
1929                 return 0;
1930
1931         if (ports[port_id].port_status != RTE_PORT_STARTED)
1932                 return 0;
1933
1934         return 1;
1935 }
1936
1937 int
1938 start_port(portid_t pid)
1939 {
1940         int diag, need_check_link_status = -1;
1941         portid_t pi;
1942         queueid_t qi;
1943         struct rte_port *port;
1944         struct ether_addr mac_addr;
1945
1946         if (port_id_is_invalid(pid, ENABLED_WARN))
1947                 return 0;
1948
1949         if(dcb_config)
1950                 dcb_test = 1;
1951         RTE_ETH_FOREACH_DEV(pi) {
1952                 if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
1953                         continue;
1954
1955                 need_check_link_status = 0;
1956                 port = &ports[pi];
1957                 if (rte_atomic16_cmpset(&(port->port_status), RTE_PORT_STOPPED,
1958                                                  RTE_PORT_HANDLING) == 0) {
1959                         printf("Port %d is now not stopped\n", pi);
1960                         continue;
1961                 }
1962
1963                 if (port->need_reconfig > 0) {
1964                         port->need_reconfig = 0;
1965
1966                         if (flow_isolate_all) {
1967                                 int ret = port_flow_isolate(pi, 1);
1968                                 if (ret) {
1969                                         printf("Failed to apply isolated"
1970                                                " mode on port %d\n", pi);
1971                                         return -1;
1972                                 }
1973                         }
1974                         configure_rxtx_dump_callbacks(0);
1975                         printf("Configuring Port %d (socket %u)\n", pi,
1976                                         port->socket_id);
1977                         /* configure port */
1978                         diag = rte_eth_dev_configure(pi, nb_rxq, nb_txq,
1979                                                 &(port->dev_conf));
1980                         if (diag != 0) {
1981                                 if (rte_atomic16_cmpset(&(port->port_status),
1982                                 RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
1983                                         printf("Port %d can not be set back "
1984                                                         "to stopped\n", pi);
1985                                 printf("Fail to configure port %d\n", pi);
1986                                 /* try to reconfigure port next time */
1987                                 port->need_reconfig = 1;
1988                                 return -1;
1989                         }
1990                 }
1991                 if (port->need_reconfig_queues > 0) {
1992                         port->need_reconfig_queues = 0;
1993                         /* setup tx queues */
1994                         for (qi = 0; qi < nb_txq; qi++) {
1995                                 if ((numa_support) &&
1996                                         (txring_numa[pi] != NUMA_NO_CONFIG))
1997                                         diag = rte_eth_tx_queue_setup(pi, qi,
1998                                                 port->nb_tx_desc[qi],
1999                                                 txring_numa[pi],
2000                                                 &(port->tx_conf[qi]));
2001                                 else
2002                                         diag = rte_eth_tx_queue_setup(pi, qi,
2003                                                 port->nb_tx_desc[qi],
2004                                                 port->socket_id,
2005                                                 &(port->tx_conf[qi]));
2006
2007                                 if (diag == 0)
2008                                         continue;
2009
2010                                 /* Fail to setup tx queue, return */
2011                                 if (rte_atomic16_cmpset(&(port->port_status),
2012                                                         RTE_PORT_HANDLING,
2013                                                         RTE_PORT_STOPPED) == 0)
2014                                         printf("Port %d can not be set back "
2015                                                         "to stopped\n", pi);
2016                                 printf("Fail to configure port %d tx queues\n",
2017                                        pi);
2018                                 /* try to reconfigure queues next time */
2019                                 port->need_reconfig_queues = 1;
2020                                 return -1;
2021                         }
2022                         for (qi = 0; qi < nb_rxq; qi++) {
2023                                 /* setup rx queues */
2024                                 if ((numa_support) &&
2025                                         (rxring_numa[pi] != NUMA_NO_CONFIG)) {
2026                                         struct rte_mempool * mp =
2027                                                 mbuf_pool_find(rxring_numa[pi]);
2028                                         if (mp == NULL) {
2029                                                 printf("Failed to setup RX queue:"
2030                                                         "No mempool allocation"
2031                                                         " on the socket %d\n",
2032                                                         rxring_numa[pi]);
2033                                                 return -1;
2034                                         }
2035
2036                                         diag = rte_eth_rx_queue_setup(pi, qi,
2037                                              port->nb_rx_desc[qi],
2038                                              rxring_numa[pi],
2039                                              &(port->rx_conf[qi]),
2040                                              mp);
2041                                 } else {
2042                                         struct rte_mempool *mp =
2043                                                 mbuf_pool_find(port->socket_id);
2044                                         if (mp == NULL) {
2045                                                 printf("Failed to setup RX queue:"
2046                                                         "No mempool allocation"
2047                                                         " on the socket %d\n",
2048                                                         port->socket_id);
2049                                                 return -1;
2050                                         }
2051                                         diag = rte_eth_rx_queue_setup(pi, qi,
2052                                              port->nb_rx_desc[qi],
2053                                              port->socket_id,
2054                                              &(port->rx_conf[qi]),
2055                                              mp);
2056                                 }
2057                                 if (diag == 0)
2058                                         continue;
2059
2060                                 /* Fail to setup rx queue, return */
2061                                 if (rte_atomic16_cmpset(&(port->port_status),
2062                                                         RTE_PORT_HANDLING,
2063                                                         RTE_PORT_STOPPED) == 0)
2064                                         printf("Port %d can not be set back "
2065                                                         "to stopped\n", pi);
2066                                 printf("Fail to configure port %d rx queues\n",
2067                                        pi);
2068                                 /* try to reconfigure queues next time */
2069                                 port->need_reconfig_queues = 1;
2070                                 return -1;
2071                         }
2072                 }
2073                 configure_rxtx_dump_callbacks(verbose_level);
2074                 /* start port */
2075                 if (rte_eth_dev_start(pi) < 0) {
2076                         printf("Fail to start port %d\n", pi);
2077
2078                         /* Fail to setup rx queue, return */
2079                         if (rte_atomic16_cmpset(&(port->port_status),
2080                                 RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
2081                                 printf("Port %d can not be set back to "
2082                                                         "stopped\n", pi);
2083                         continue;
2084                 }
2085
2086                 if (rte_atomic16_cmpset(&(port->port_status),
2087                         RTE_PORT_HANDLING, RTE_PORT_STARTED) == 0)
2088                         printf("Port %d can not be set into started\n", pi);
2089
2090                 rte_eth_macaddr_get(pi, &mac_addr);
2091                 printf("Port %d: %02X:%02X:%02X:%02X:%02X:%02X\n", pi,
2092                                 mac_addr.addr_bytes[0], mac_addr.addr_bytes[1],
2093                                 mac_addr.addr_bytes[2], mac_addr.addr_bytes[3],
2094                                 mac_addr.addr_bytes[4], mac_addr.addr_bytes[5]);
2095
2096                 /* at least one port started, need checking link status */
2097                 need_check_link_status = 1;
2098         }
2099
2100         if (need_check_link_status == 1 && !no_link_check)
2101                 check_all_ports_link_status(RTE_PORT_ALL);
2102         else if (need_check_link_status == 0)
2103                 printf("Please stop the ports first\n");
2104
2105         printf("Done\n");
2106         return 0;
2107 }
2108
2109 void
2110 stop_port(portid_t pid)
2111 {
2112         portid_t pi;
2113         struct rte_port *port;
2114         int need_check_link_status = 0;
2115
2116         if (dcb_test) {
2117                 dcb_test = 0;
2118                 dcb_config = 0;
2119         }
2120
2121         if (port_id_is_invalid(pid, ENABLED_WARN))
2122                 return;
2123
2124         printf("Stopping ports...\n");
2125
2126         RTE_ETH_FOREACH_DEV(pi) {
2127                 if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
2128                         continue;
2129
2130                 if (port_is_forwarding(pi) != 0 && test_done == 0) {
2131                         printf("Please remove port %d from forwarding configuration.\n", pi);
2132                         continue;
2133                 }
2134
2135                 if (port_is_bonding_slave(pi)) {
2136                         printf("Please remove port %d from bonded device.\n", pi);
2137                         continue;
2138                 }
2139
2140                 port = &ports[pi];
2141                 if (rte_atomic16_cmpset(&(port->port_status), RTE_PORT_STARTED,
2142                                                 RTE_PORT_HANDLING) == 0)
2143                         continue;
2144
2145                 rte_eth_dev_stop(pi);
2146
2147                 if (rte_atomic16_cmpset(&(port->port_status),
2148                         RTE_PORT_HANDLING, RTE_PORT_STOPPED) == 0)
2149                         printf("Port %d can not be set into stopped\n", pi);
2150                 need_check_link_status = 1;
2151         }
2152         if (need_check_link_status && !no_link_check)
2153                 check_all_ports_link_status(RTE_PORT_ALL);
2154
2155         printf("Done\n");
2156 }
2157
2158 static void
2159 remove_invalid_ports_in(portid_t *array, portid_t *total)
2160 {
2161         portid_t i;
2162         portid_t new_total = 0;
2163
2164         for (i = 0; i < *total; i++)
2165                 if (!port_id_is_invalid(array[i], DISABLED_WARN)) {
2166                         array[new_total] = array[i];
2167                         new_total++;
2168                 }
2169         *total = new_total;
2170 }
2171
2172 static void
2173 remove_invalid_ports(void)
2174 {
2175         remove_invalid_ports_in(ports_ids, &nb_ports);
2176         remove_invalid_ports_in(fwd_ports_ids, &nb_fwd_ports);
2177         nb_cfg_ports = nb_fwd_ports;
2178 }
2179
2180 void
2181 close_port(portid_t pid)
2182 {
2183         portid_t pi;
2184         struct rte_port *port;
2185
2186         if (port_id_is_invalid(pid, ENABLED_WARN))
2187                 return;
2188
2189         printf("Closing ports...\n");
2190
2191         RTE_ETH_FOREACH_DEV(pi) {
2192                 if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
2193                         continue;
2194
2195                 if (port_is_forwarding(pi) != 0 && test_done == 0) {
2196                         printf("Please remove port %d from forwarding configuration.\n", pi);
2197                         continue;
2198                 }
2199
2200                 if (port_is_bonding_slave(pi)) {
2201                         printf("Please remove port %d from bonded device.\n", pi);
2202                         continue;
2203                 }
2204
2205                 port = &ports[pi];
2206                 if (rte_atomic16_cmpset(&(port->port_status),
2207                         RTE_PORT_CLOSED, RTE_PORT_CLOSED) == 1) {
2208                         printf("Port %d is already closed\n", pi);
2209                         continue;
2210                 }
2211
2212                 if (rte_atomic16_cmpset(&(port->port_status),
2213                         RTE_PORT_STOPPED, RTE_PORT_HANDLING) == 0) {
2214                         printf("Port %d is now not stopped\n", pi);
2215                         continue;
2216                 }
2217
2218                 if (port->flow_list)
2219                         port_flow_flush(pi);
2220                 rte_eth_dev_close(pi);
2221
2222                 remove_invalid_ports();
2223
2224                 if (rte_atomic16_cmpset(&(port->port_status),
2225                         RTE_PORT_HANDLING, RTE_PORT_CLOSED) == 0)
2226                         printf("Port %d cannot be set to closed\n", pi);
2227         }
2228
2229         printf("Done\n");
2230 }
2231
2232 void
2233 reset_port(portid_t pid)
2234 {
2235         int diag;
2236         portid_t pi;
2237         struct rte_port *port;
2238
2239         if (port_id_is_invalid(pid, ENABLED_WARN))
2240                 return;
2241
2242         printf("Resetting ports...\n");
2243
2244         RTE_ETH_FOREACH_DEV(pi) {
2245                 if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
2246                         continue;
2247
2248                 if (port_is_forwarding(pi) != 0 && test_done == 0) {
2249                         printf("Please remove port %d from forwarding "
2250                                "configuration.\n", pi);
2251                         continue;
2252                 }
2253
2254                 if (port_is_bonding_slave(pi)) {
2255                         printf("Please remove port %d from bonded device.\n",
2256                                pi);
2257                         continue;
2258                 }
2259
2260                 diag = rte_eth_dev_reset(pi);
2261                 if (diag == 0) {
2262                         port = &ports[pi];
2263                         port->need_reconfig = 1;
2264                         port->need_reconfig_queues = 1;
2265                 } else {
2266                         printf("Failed to reset port %d. diag=%d\n", pi, diag);
2267                 }
2268         }
2269
2270         printf("Done\n");
2271 }
2272
2273 void
2274 attach_port(char *identifier)
2275 {
2276         portid_t pi;
2277         struct rte_dev_iterator iterator;
2278
2279         printf("Attaching a new port...\n");
2280
2281         if (identifier == NULL) {
2282                 printf("Invalid parameters are specified\n");
2283                 return;
2284         }
2285
2286         if (rte_dev_probe(identifier) != 0) {
2287                 TESTPMD_LOG(ERR, "Failed to attach port %s\n", identifier);
2288                 return;
2289         }
2290
2291         /* first attach mode: event */
2292         if (setup_on_probe_event) {
2293                 /* new ports are detected on RTE_ETH_EVENT_NEW event */
2294                 for (pi = 0; pi < RTE_MAX_ETHPORTS; pi++)
2295                         if (ports[pi].port_status == RTE_PORT_HANDLING &&
2296                                         ports[pi].need_setup != 0)
2297                                 setup_attached_port(pi);
2298                 return;
2299         }
2300
2301         /* second attach mode: iterator */
2302         RTE_ETH_FOREACH_MATCHING_DEV(pi, identifier, &iterator) {
2303                 /* setup ports matching the devargs used for probing */
2304                 if (port_is_forwarding(pi))
2305                         continue; /* port was already attached before */
2306                 setup_attached_port(pi);
2307         }
2308 }
2309
2310 static void
2311 setup_attached_port(portid_t pi)
2312 {
2313         unsigned int socket_id;
2314
2315         socket_id = (unsigned)rte_eth_dev_socket_id(pi);
2316         /* if socket_id is invalid, set to the first available socket. */
2317         if (check_socket_id(socket_id) < 0)
2318                 socket_id = socket_ids[0];
2319         reconfig(pi, socket_id);
2320         rte_eth_promiscuous_enable(pi);
2321
2322         ports_ids[nb_ports++] = pi;
2323         fwd_ports_ids[nb_fwd_ports++] = pi;
2324         nb_cfg_ports = nb_fwd_ports;
2325         ports[pi].need_setup = 0;
2326         ports[pi].port_status = RTE_PORT_STOPPED;
2327
2328         printf("Port %d is attached. Now total ports is %d\n", pi, nb_ports);
2329         printf("Done\n");
2330 }
2331
2332 void
2333 detach_port_device(portid_t port_id)
2334 {
2335         struct rte_device *dev;
2336         portid_t sibling;
2337
2338         printf("Removing a device...\n");
2339
2340         dev = rte_eth_devices[port_id].device;
2341         if (dev == NULL) {
2342                 printf("Device already removed\n");
2343                 return;
2344         }
2345
2346         if (ports[port_id].port_status != RTE_PORT_CLOSED) {
2347                 if (ports[port_id].port_status != RTE_PORT_STOPPED) {
2348                         printf("Port not stopped\n");
2349                         return;
2350                 }
2351                 printf("Port was not closed\n");
2352                 if (ports[port_id].flow_list)
2353                         port_flow_flush(port_id);
2354         }
2355
2356         if (rte_dev_remove(dev) != 0) {
2357                 TESTPMD_LOG(ERR, "Failed to detach device %s\n", dev->name);
2358                 return;
2359         }
2360
2361         for (sibling = 0; sibling < RTE_MAX_ETHPORTS; sibling++) {
2362                 if (rte_eth_devices[sibling].device != dev)
2363                         continue;
2364                 /* reset mapping between old ports and removed device */
2365                 rte_eth_devices[sibling].device = NULL;
2366                 if (ports[sibling].port_status != RTE_PORT_CLOSED) {
2367                         /* sibling ports are forced to be closed */
2368                         ports[sibling].port_status = RTE_PORT_CLOSED;
2369                         printf("Port %u is closed\n", sibling);
2370                 }
2371         }
2372
2373         remove_invalid_ports();
2374
2375         printf("Device of port %u is detached\n", port_id);
2376         printf("Now total ports is %d\n", nb_ports);
2377         printf("Done\n");
2378         return;
2379 }
2380
2381 void
2382 pmd_test_exit(void)
2383 {
2384         struct rte_device *device;
2385         portid_t pt_id;
2386         int ret;
2387
2388         if (test_done == 0)
2389                 stop_packet_forwarding();
2390
2391         if (ports != NULL) {
2392                 no_link_check = 1;
2393                 RTE_ETH_FOREACH_DEV(pt_id) {
2394                         printf("\nStopping port %d...\n", pt_id);
2395                         fflush(stdout);
2396                         stop_port(pt_id);
2397                 }
2398                 RTE_ETH_FOREACH_DEV(pt_id) {
2399                         printf("\nShutting down port %d...\n", pt_id);
2400                         fflush(stdout);
2401                         close_port(pt_id);
2402
2403                         /*
2404                          * This is a workaround to fix a virtio-user issue that
2405                          * requires to call clean-up routine to remove existing
2406                          * socket.
2407                          * This workaround valid only for testpmd, needs a fix
2408                          * valid for all applications.
2409                          * TODO: Implement proper resource cleanup
2410                          */
2411                         device = rte_eth_devices[pt_id].device;
2412                         if (device && !strcmp(device->driver->name, "net_virtio_user"))
2413                                 detach_port_device(pt_id);
2414                 }
2415         }
2416
2417         if (hot_plug) {
2418                 ret = rte_dev_event_monitor_stop();
2419                 if (ret) {
2420                         RTE_LOG(ERR, EAL,
2421                                 "fail to stop device event monitor.");
2422                         return;
2423                 }
2424
2425                 ret = rte_dev_event_callback_unregister(NULL,
2426                         dev_event_callback, NULL);
2427                 if (ret < 0) {
2428                         RTE_LOG(ERR, EAL,
2429                                 "fail to unregister device event callback.\n");
2430                         return;
2431                 }
2432
2433                 ret = rte_dev_hotplug_handle_disable();
2434                 if (ret) {
2435                         RTE_LOG(ERR, EAL,
2436                                 "fail to disable hotplug handling.\n");
2437                         return;
2438                 }
2439         }
2440
2441         printf("\nBye...\n");
2442 }
2443
2444 typedef void (*cmd_func_t)(void);
2445 struct pmd_test_command {
2446         const char *cmd_name;
2447         cmd_func_t cmd_func;
2448 };
2449
2450 #define PMD_TEST_CMD_NB (sizeof(pmd_test_menu) / sizeof(pmd_test_menu[0]))
2451
2452 /* Check the link status of all ports in up to 9s, and print them finally */
2453 static void
2454 check_all_ports_link_status(uint32_t port_mask)
2455 {
2456 #define CHECK_INTERVAL 100 /* 100ms */
2457 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
2458         portid_t portid;
2459         uint8_t count, all_ports_up, print_flag = 0;
2460         struct rte_eth_link link;
2461
2462         printf("Checking link statuses...\n");
2463         fflush(stdout);
2464         for (count = 0; count <= MAX_CHECK_TIME; count++) {
2465                 all_ports_up = 1;
2466                 RTE_ETH_FOREACH_DEV(portid) {
2467                         if ((port_mask & (1 << portid)) == 0)
2468                                 continue;
2469                         memset(&link, 0, sizeof(link));
2470                         rte_eth_link_get_nowait(portid, &link);
2471                         /* print link status if flag set */
2472                         if (print_flag == 1) {
2473                                 if (link.link_status)
2474                                         printf(
2475                                         "Port%d Link Up. speed %u Mbps- %s\n",
2476                                         portid, link.link_speed,
2477                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
2478                                         ("full-duplex") : ("half-duplex\n"));
2479                                 else
2480                                         printf("Port %d Link Down\n", portid);
2481                                 continue;
2482                         }
2483                         /* clear all_ports_up flag if any link down */
2484                         if (link.link_status == ETH_LINK_DOWN) {
2485                                 all_ports_up = 0;
2486                                 break;
2487                         }
2488                 }
2489                 /* after finally printing all link status, get out */
2490                 if (print_flag == 1)
2491                         break;
2492
2493                 if (all_ports_up == 0) {
2494                         fflush(stdout);
2495                         rte_delay_ms(CHECK_INTERVAL);
2496                 }
2497
2498                 /* set the print_flag if all ports up or timeout */
2499                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
2500                         print_flag = 1;
2501                 }
2502
2503                 if (lsc_interrupt)
2504                         break;
2505         }
2506 }
2507
2508 /*
2509  * This callback is for remove a port for a device. It has limitation because
2510  * it is not for multiple port removal for a device.
2511  * TODO: the device detach invoke will plan to be removed from user side to
2512  * eal. And convert all PMDs to free port resources on ether device closing.
2513  */
2514 static void
2515 rmv_port_callback(void *arg)
2516 {
2517         int need_to_start = 0;
2518         int org_no_link_check = no_link_check;
2519         portid_t port_id = (intptr_t)arg;
2520
2521         RTE_ETH_VALID_PORTID_OR_RET(port_id);
2522
2523         if (!test_done && port_is_forwarding(port_id)) {
2524                 need_to_start = 1;
2525                 stop_packet_forwarding();
2526         }
2527         no_link_check = 1;
2528         stop_port(port_id);
2529         no_link_check = org_no_link_check;
2530         close_port(port_id);
2531         detach_port_device(port_id);
2532         if (need_to_start)
2533                 start_packet_forwarding(0);
2534 }
2535
2536 /* This function is used by the interrupt thread */
2537 static int
2538 eth_event_callback(portid_t port_id, enum rte_eth_event_type type, void *param,
2539                   void *ret_param)
2540 {
2541         RTE_SET_USED(param);
2542         RTE_SET_USED(ret_param);
2543
2544         if (type >= RTE_ETH_EVENT_MAX) {
2545                 fprintf(stderr, "\nPort %" PRIu16 ": %s called upon invalid event %d\n",
2546                         port_id, __func__, type);
2547                 fflush(stderr);
2548         } else if (event_print_mask & (UINT32_C(1) << type)) {
2549                 printf("\nPort %" PRIu16 ": %s event\n", port_id,
2550                         eth_event_desc[type]);
2551                 fflush(stdout);
2552         }
2553
2554         switch (type) {
2555         case RTE_ETH_EVENT_NEW:
2556                 ports[port_id].need_setup = 1;
2557                 ports[port_id].port_status = RTE_PORT_HANDLING;
2558                 break;
2559         case RTE_ETH_EVENT_INTR_RMV:
2560                 if (port_id_is_invalid(port_id, DISABLED_WARN))
2561                         break;
2562                 if (rte_eal_alarm_set(100000,
2563                                 rmv_port_callback, (void *)(intptr_t)port_id))
2564                         fprintf(stderr, "Could not set up deferred device removal\n");
2565                 break;
2566         default:
2567                 break;
2568         }
2569         return 0;
2570 }
2571
2572 static int
2573 register_eth_event_callback(void)
2574 {
2575         int ret;
2576         enum rte_eth_event_type event;
2577
2578         for (event = RTE_ETH_EVENT_UNKNOWN;
2579                         event < RTE_ETH_EVENT_MAX; event++) {
2580                 ret = rte_eth_dev_callback_register(RTE_ETH_ALL,
2581                                 event,
2582                                 eth_event_callback,
2583                                 NULL);
2584                 if (ret != 0) {
2585                         TESTPMD_LOG(ERR, "Failed to register callback for "
2586                                         "%s event\n", eth_event_desc[event]);
2587                         return -1;
2588                 }
2589         }
2590
2591         return 0;
2592 }
2593
2594 /* This function is used by the interrupt thread */
2595 static void
2596 dev_event_callback(const char *device_name, enum rte_dev_event_type type,
2597                              __rte_unused void *arg)
2598 {
2599         uint16_t port_id;
2600         int ret;
2601
2602         if (type >= RTE_DEV_EVENT_MAX) {
2603                 fprintf(stderr, "%s called upon invalid event %d\n",
2604                         __func__, type);
2605                 fflush(stderr);
2606         }
2607
2608         switch (type) {
2609         case RTE_DEV_EVENT_REMOVE:
2610                 RTE_LOG(DEBUG, EAL, "The device: %s has been removed!\n",
2611                         device_name);
2612                 ret = rte_eth_dev_get_port_by_name(device_name, &port_id);
2613                 if (ret) {
2614                         RTE_LOG(ERR, EAL, "can not get port by device %s!\n",
2615                                 device_name);
2616                         return;
2617                 }
2618                 /*
2619                  * Because the user's callback is invoked in eal interrupt
2620                  * callback, the interrupt callback need to be finished before
2621                  * it can be unregistered when detaching device. So finish
2622                  * callback soon and use a deferred removal to detach device
2623                  * is need. It is a workaround, once the device detaching be
2624                  * moved into the eal in the future, the deferred removal could
2625                  * be deleted.
2626                  */
2627                 if (rte_eal_alarm_set(100000,
2628                                 rmv_port_callback, (void *)(intptr_t)port_id))
2629                         RTE_LOG(ERR, EAL,
2630                                 "Could not set up deferred device removal\n");
2631                 break;
2632         case RTE_DEV_EVENT_ADD:
2633                 RTE_LOG(ERR, EAL, "The device: %s has been added!\n",
2634                         device_name);
2635                 /* TODO: After finish kernel driver binding,
2636                  * begin to attach port.
2637                  */
2638                 break;
2639         default:
2640                 break;
2641         }
2642 }
2643
2644 static int
2645 set_tx_queue_stats_mapping_registers(portid_t port_id, struct rte_port *port)
2646 {
2647         uint16_t i;
2648         int diag;
2649         uint8_t mapping_found = 0;
2650
2651         for (i = 0; i < nb_tx_queue_stats_mappings; i++) {
2652                 if ((tx_queue_stats_mappings[i].port_id == port_id) &&
2653                                 (tx_queue_stats_mappings[i].queue_id < nb_txq )) {
2654                         diag = rte_eth_dev_set_tx_queue_stats_mapping(port_id,
2655                                         tx_queue_stats_mappings[i].queue_id,
2656                                         tx_queue_stats_mappings[i].stats_counter_id);
2657                         if (diag != 0)
2658                                 return diag;
2659                         mapping_found = 1;
2660                 }
2661         }
2662         if (mapping_found)
2663                 port->tx_queue_stats_mapping_enabled = 1;
2664         return 0;
2665 }
2666
2667 static int
2668 set_rx_queue_stats_mapping_registers(portid_t port_id, struct rte_port *port)
2669 {
2670         uint16_t i;
2671         int diag;
2672         uint8_t mapping_found = 0;
2673
2674         for (i = 0; i < nb_rx_queue_stats_mappings; i++) {
2675                 if ((rx_queue_stats_mappings[i].port_id == port_id) &&
2676                                 (rx_queue_stats_mappings[i].queue_id < nb_rxq )) {
2677                         diag = rte_eth_dev_set_rx_queue_stats_mapping(port_id,
2678                                         rx_queue_stats_mappings[i].queue_id,
2679                                         rx_queue_stats_mappings[i].stats_counter_id);
2680                         if (diag != 0)
2681                                 return diag;
2682                         mapping_found = 1;
2683                 }
2684         }
2685         if (mapping_found)
2686                 port->rx_queue_stats_mapping_enabled = 1;
2687         return 0;
2688 }
2689
2690 static void
2691 map_port_queue_stats_mapping_registers(portid_t pi, struct rte_port *port)
2692 {
2693         int diag = 0;
2694
2695         diag = set_tx_queue_stats_mapping_registers(pi, port);
2696         if (diag != 0) {
2697                 if (diag == -ENOTSUP) {
2698                         port->tx_queue_stats_mapping_enabled = 0;
2699                         printf("TX queue stats mapping not supported port id=%d\n", pi);
2700                 }
2701                 else
2702                         rte_exit(EXIT_FAILURE,
2703                                         "set_tx_queue_stats_mapping_registers "
2704                                         "failed for port id=%d diag=%d\n",
2705                                         pi, diag);
2706         }
2707
2708         diag = set_rx_queue_stats_mapping_registers(pi, port);
2709         if (diag != 0) {
2710                 if (diag == -ENOTSUP) {
2711                         port->rx_queue_stats_mapping_enabled = 0;
2712                         printf("RX queue stats mapping not supported port id=%d\n", pi);
2713                 }
2714                 else
2715                         rte_exit(EXIT_FAILURE,
2716                                         "set_rx_queue_stats_mapping_registers "
2717                                         "failed for port id=%d diag=%d\n",
2718                                         pi, diag);
2719         }
2720 }
2721
2722 static void
2723 rxtx_port_config(struct rte_port *port)
2724 {
2725         uint16_t qid;
2726
2727         for (qid = 0; qid < nb_rxq; qid++) {
2728                 port->rx_conf[qid] = port->dev_info.default_rxconf;
2729
2730                 /* Check if any Rx parameters have been passed */
2731                 if (rx_pthresh != RTE_PMD_PARAM_UNSET)
2732                         port->rx_conf[qid].rx_thresh.pthresh = rx_pthresh;
2733
2734                 if (rx_hthresh != RTE_PMD_PARAM_UNSET)
2735                         port->rx_conf[qid].rx_thresh.hthresh = rx_hthresh;
2736
2737                 if (rx_wthresh != RTE_PMD_PARAM_UNSET)
2738                         port->rx_conf[qid].rx_thresh.wthresh = rx_wthresh;
2739
2740                 if (rx_free_thresh != RTE_PMD_PARAM_UNSET)
2741                         port->rx_conf[qid].rx_free_thresh = rx_free_thresh;
2742
2743                 if (rx_drop_en != RTE_PMD_PARAM_UNSET)
2744                         port->rx_conf[qid].rx_drop_en = rx_drop_en;
2745
2746                 port->nb_rx_desc[qid] = nb_rxd;
2747         }
2748
2749         for (qid = 0; qid < nb_txq; qid++) {
2750                 port->tx_conf[qid] = port->dev_info.default_txconf;
2751
2752                 /* Check if any Tx parameters have been passed */
2753                 if (tx_pthresh != RTE_PMD_PARAM_UNSET)
2754                         port->tx_conf[qid].tx_thresh.pthresh = tx_pthresh;
2755
2756                 if (tx_hthresh != RTE_PMD_PARAM_UNSET)
2757                         port->tx_conf[qid].tx_thresh.hthresh = tx_hthresh;
2758
2759                 if (tx_wthresh != RTE_PMD_PARAM_UNSET)
2760                         port->tx_conf[qid].tx_thresh.wthresh = tx_wthresh;
2761
2762                 if (tx_rs_thresh != RTE_PMD_PARAM_UNSET)
2763                         port->tx_conf[qid].tx_rs_thresh = tx_rs_thresh;
2764
2765                 if (tx_free_thresh != RTE_PMD_PARAM_UNSET)
2766                         port->tx_conf[qid].tx_free_thresh = tx_free_thresh;
2767
2768                 port->nb_tx_desc[qid] = nb_txd;
2769         }
2770 }
2771
2772 void
2773 init_port_config(void)
2774 {
2775         portid_t pid;
2776         struct rte_port *port;
2777
2778         RTE_ETH_FOREACH_DEV(pid) {
2779                 port = &ports[pid];
2780                 port->dev_conf.fdir_conf = fdir_conf;
2781                 rte_eth_dev_info_get(pid, &port->dev_info);
2782                 if (nb_rxq > 1) {
2783                         port->dev_conf.rx_adv_conf.rss_conf.rss_key = NULL;
2784                         port->dev_conf.rx_adv_conf.rss_conf.rss_hf =
2785                                 rss_hf & port->dev_info.flow_type_rss_offloads;
2786                 } else {
2787                         port->dev_conf.rx_adv_conf.rss_conf.rss_key = NULL;
2788                         port->dev_conf.rx_adv_conf.rss_conf.rss_hf = 0;
2789                 }
2790
2791                 if (port->dcb_flag == 0) {
2792                         if( port->dev_conf.rx_adv_conf.rss_conf.rss_hf != 0)
2793                                 port->dev_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
2794                         else
2795                                 port->dev_conf.rxmode.mq_mode = ETH_MQ_RX_NONE;
2796                 }
2797
2798                 rxtx_port_config(port);
2799
2800                 rte_eth_macaddr_get(pid, &port->eth_addr);
2801
2802                 map_port_queue_stats_mapping_registers(pid, port);
2803 #if defined RTE_LIBRTE_IXGBE_PMD && defined RTE_LIBRTE_IXGBE_BYPASS
2804                 rte_pmd_ixgbe_bypass_init(pid);
2805 #endif
2806
2807                 if (lsc_interrupt &&
2808                     (rte_eth_devices[pid].data->dev_flags &
2809                      RTE_ETH_DEV_INTR_LSC))
2810                         port->dev_conf.intr_conf.lsc = 1;
2811                 if (rmv_interrupt &&
2812                     (rte_eth_devices[pid].data->dev_flags &
2813                      RTE_ETH_DEV_INTR_RMV))
2814                         port->dev_conf.intr_conf.rmv = 1;
2815         }
2816 }
2817
2818 void set_port_slave_flag(portid_t slave_pid)
2819 {
2820         struct rte_port *port;
2821
2822         port = &ports[slave_pid];
2823         port->slave_flag = 1;
2824 }
2825
2826 void clear_port_slave_flag(portid_t slave_pid)
2827 {
2828         struct rte_port *port;
2829
2830         port = &ports[slave_pid];
2831         port->slave_flag = 0;
2832 }
2833
2834 uint8_t port_is_bonding_slave(portid_t slave_pid)
2835 {
2836         struct rte_port *port;
2837
2838         port = &ports[slave_pid];
2839         if ((rte_eth_devices[slave_pid].data->dev_flags &
2840             RTE_ETH_DEV_BONDED_SLAVE) || (port->slave_flag == 1))
2841                 return 1;
2842         return 0;
2843 }
2844
2845 const uint16_t vlan_tags[] = {
2846                 0,  1,  2,  3,  4,  5,  6,  7,
2847                 8,  9, 10, 11,  12, 13, 14, 15,
2848                 16, 17, 18, 19, 20, 21, 22, 23,
2849                 24, 25, 26, 27, 28, 29, 30, 31
2850 };
2851
2852 static  int
2853 get_eth_dcb_conf(portid_t pid, struct rte_eth_conf *eth_conf,
2854                  enum dcb_mode_enable dcb_mode,
2855                  enum rte_eth_nb_tcs num_tcs,
2856                  uint8_t pfc_en)
2857 {
2858         uint8_t i;
2859         int32_t rc;
2860         struct rte_eth_rss_conf rss_conf;
2861
2862         /*
2863          * Builds up the correct configuration for dcb+vt based on the vlan tags array
2864          * given above, and the number of traffic classes available for use.
2865          */
2866         if (dcb_mode == DCB_VT_ENABLED) {
2867                 struct rte_eth_vmdq_dcb_conf *vmdq_rx_conf =
2868                                 &eth_conf->rx_adv_conf.vmdq_dcb_conf;
2869                 struct rte_eth_vmdq_dcb_tx_conf *vmdq_tx_conf =
2870                                 &eth_conf->tx_adv_conf.vmdq_dcb_tx_conf;
2871
2872                 /* VMDQ+DCB RX and TX configurations */
2873                 vmdq_rx_conf->enable_default_pool = 0;
2874                 vmdq_rx_conf->default_pool = 0;
2875                 vmdq_rx_conf->nb_queue_pools =
2876                         (num_tcs ==  ETH_4_TCS ? ETH_32_POOLS : ETH_16_POOLS);
2877                 vmdq_tx_conf->nb_queue_pools =
2878                         (num_tcs ==  ETH_4_TCS ? ETH_32_POOLS : ETH_16_POOLS);
2879
2880                 vmdq_rx_conf->nb_pool_maps = vmdq_rx_conf->nb_queue_pools;
2881                 for (i = 0; i < vmdq_rx_conf->nb_pool_maps; i++) {
2882                         vmdq_rx_conf->pool_map[i].vlan_id = vlan_tags[i];
2883                         vmdq_rx_conf->pool_map[i].pools =
2884                                 1 << (i % vmdq_rx_conf->nb_queue_pools);
2885                 }
2886                 for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++) {
2887                         vmdq_rx_conf->dcb_tc[i] = i % num_tcs;
2888                         vmdq_tx_conf->dcb_tc[i] = i % num_tcs;
2889                 }
2890
2891                 /* set DCB mode of RX and TX of multiple queues */
2892                 eth_conf->rxmode.mq_mode = ETH_MQ_RX_VMDQ_DCB;
2893                 eth_conf->txmode.mq_mode = ETH_MQ_TX_VMDQ_DCB;
2894         } else {
2895                 struct rte_eth_dcb_rx_conf *rx_conf =
2896                                 &eth_conf->rx_adv_conf.dcb_rx_conf;
2897                 struct rte_eth_dcb_tx_conf *tx_conf =
2898                                 &eth_conf->tx_adv_conf.dcb_tx_conf;
2899
2900                 rc = rte_eth_dev_rss_hash_conf_get(pid, &rss_conf);
2901                 if (rc != 0)
2902                         return rc;
2903
2904                 rx_conf->nb_tcs = num_tcs;
2905                 tx_conf->nb_tcs = num_tcs;
2906
2907                 for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++) {
2908                         rx_conf->dcb_tc[i] = i % num_tcs;
2909                         tx_conf->dcb_tc[i] = i % num_tcs;
2910                 }
2911
2912                 eth_conf->rxmode.mq_mode = ETH_MQ_RX_DCB_RSS;
2913                 eth_conf->rx_adv_conf.rss_conf = rss_conf;
2914                 eth_conf->txmode.mq_mode = ETH_MQ_TX_DCB;
2915         }
2916
2917         if (pfc_en)
2918                 eth_conf->dcb_capability_en =
2919                                 ETH_DCB_PG_SUPPORT | ETH_DCB_PFC_SUPPORT;
2920         else
2921                 eth_conf->dcb_capability_en = ETH_DCB_PG_SUPPORT;
2922
2923         return 0;
2924 }
2925
2926 int
2927 init_port_dcb_config(portid_t pid,
2928                      enum dcb_mode_enable dcb_mode,
2929                      enum rte_eth_nb_tcs num_tcs,
2930                      uint8_t pfc_en)
2931 {
2932         struct rte_eth_conf port_conf;
2933         struct rte_port *rte_port;
2934         int retval;
2935         uint16_t i;
2936
2937         rte_port = &ports[pid];
2938
2939         memset(&port_conf, 0, sizeof(struct rte_eth_conf));
2940         /* Enter DCB configuration status */
2941         dcb_config = 1;
2942
2943         port_conf.rxmode = rte_port->dev_conf.rxmode;
2944         port_conf.txmode = rte_port->dev_conf.txmode;
2945
2946         /*set configuration of DCB in vt mode and DCB in non-vt mode*/
2947         retval = get_eth_dcb_conf(pid, &port_conf, dcb_mode, num_tcs, pfc_en);
2948         if (retval < 0)
2949                 return retval;
2950         port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_VLAN_FILTER;
2951
2952         /* re-configure the device . */
2953         rte_eth_dev_configure(pid, nb_rxq, nb_rxq, &port_conf);
2954
2955         rte_eth_dev_info_get(pid, &rte_port->dev_info);
2956
2957         /* If dev_info.vmdq_pool_base is greater than 0,
2958          * the queue id of vmdq pools is started after pf queues.
2959          */
2960         if (dcb_mode == DCB_VT_ENABLED &&
2961             rte_port->dev_info.vmdq_pool_base > 0) {
2962                 printf("VMDQ_DCB multi-queue mode is nonsensical"
2963                         " for port %d.", pid);
2964                 return -1;
2965         }
2966
2967         /* Assume the ports in testpmd have the same dcb capability
2968          * and has the same number of rxq and txq in dcb mode
2969          */
2970         if (dcb_mode == DCB_VT_ENABLED) {
2971                 if (rte_port->dev_info.max_vfs > 0) {
2972                         nb_rxq = rte_port->dev_info.nb_rx_queues;
2973                         nb_txq = rte_port->dev_info.nb_tx_queues;
2974                 } else {
2975                         nb_rxq = rte_port->dev_info.max_rx_queues;
2976                         nb_txq = rte_port->dev_info.max_tx_queues;
2977                 }
2978         } else {
2979                 /*if vt is disabled, use all pf queues */
2980                 if (rte_port->dev_info.vmdq_pool_base == 0) {
2981                         nb_rxq = rte_port->dev_info.max_rx_queues;
2982                         nb_txq = rte_port->dev_info.max_tx_queues;
2983                 } else {
2984                         nb_rxq = (queueid_t)num_tcs;
2985                         nb_txq = (queueid_t)num_tcs;
2986
2987                 }
2988         }
2989         rx_free_thresh = 64;
2990
2991         memcpy(&rte_port->dev_conf, &port_conf, sizeof(struct rte_eth_conf));
2992
2993         rxtx_port_config(rte_port);
2994         /* VLAN filter */
2995         rte_port->dev_conf.rxmode.offloads |= DEV_RX_OFFLOAD_VLAN_FILTER;
2996         for (i = 0; i < RTE_DIM(vlan_tags); i++)
2997                 rx_vft_set(pid, vlan_tags[i], 1);
2998
2999         rte_eth_macaddr_get(pid, &rte_port->eth_addr);
3000         map_port_queue_stats_mapping_registers(pid, rte_port);
3001
3002         rte_port->dcb_flag = 1;
3003
3004         return 0;
3005 }
3006
3007 static void
3008 init_port(void)
3009 {
3010         /* Configuration of Ethernet ports. */
3011         ports = rte_zmalloc("testpmd: ports",
3012                             sizeof(struct rte_port) * RTE_MAX_ETHPORTS,
3013                             RTE_CACHE_LINE_SIZE);
3014         if (ports == NULL) {
3015                 rte_exit(EXIT_FAILURE,
3016                                 "rte_zmalloc(%d struct rte_port) failed\n",
3017                                 RTE_MAX_ETHPORTS);
3018         }
3019
3020         /* Initialize ports NUMA structures */
3021         memset(port_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
3022         memset(rxring_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
3023         memset(txring_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
3024 }
3025
3026 static void
3027 force_quit(void)
3028 {
3029         pmd_test_exit();
3030         prompt_exit();
3031 }
3032
3033 static void
3034 print_stats(void)
3035 {
3036         uint8_t i;
3037         const char clr[] = { 27, '[', '2', 'J', '\0' };
3038         const char top_left[] = { 27, '[', '1', ';', '1', 'H', '\0' };
3039
3040         /* Clear screen and move to top left */
3041         printf("%s%s", clr, top_left);
3042
3043         printf("\nPort statistics ====================================");
3044         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
3045                 nic_stats_display(fwd_ports_ids[i]);
3046 }
3047
3048 static void
3049 signal_handler(int signum)
3050 {
3051         if (signum == SIGINT || signum == SIGTERM) {
3052                 printf("\nSignal %d received, preparing to exit...\n",
3053                                 signum);
3054 #ifdef RTE_LIBRTE_PDUMP
3055                 /* uninitialize packet capture framework */
3056                 rte_pdump_uninit();
3057 #endif
3058 #ifdef RTE_LIBRTE_LATENCY_STATS
3059                 rte_latencystats_uninit();
3060 #endif
3061                 force_quit();
3062                 /* Set flag to indicate the force termination. */
3063                 f_quit = 1;
3064                 /* exit with the expected status */
3065                 signal(signum, SIG_DFL);
3066                 kill(getpid(), signum);
3067         }
3068 }
3069
3070 int
3071 main(int argc, char** argv)
3072 {
3073         int diag;
3074         portid_t port_id;
3075         uint16_t count;
3076         int ret;
3077
3078         signal(SIGINT, signal_handler);
3079         signal(SIGTERM, signal_handler);
3080
3081         diag = rte_eal_init(argc, argv);
3082         if (diag < 0)
3083                 rte_panic("Cannot init EAL\n");
3084
3085         testpmd_logtype = rte_log_register("testpmd");
3086         if (testpmd_logtype < 0)
3087                 rte_panic("Cannot register log type");
3088         rte_log_set_level(testpmd_logtype, RTE_LOG_DEBUG);
3089
3090         ret = register_eth_event_callback();
3091         if (ret != 0)
3092                 rte_panic("Cannot register for ethdev events");
3093
3094 #ifdef RTE_LIBRTE_PDUMP
3095         /* initialize packet capture framework */
3096         rte_pdump_init();
3097 #endif
3098
3099         count = 0;
3100         RTE_ETH_FOREACH_DEV(port_id) {
3101                 ports_ids[count] = port_id;
3102                 count++;
3103         }
3104         nb_ports = (portid_t) count;
3105         if (nb_ports == 0)
3106                 TESTPMD_LOG(WARNING, "No probed ethernet devices\n");
3107
3108         /* allocate port structures, and init them */
3109         init_port();
3110
3111         set_def_fwd_config();
3112         if (nb_lcores == 0)
3113                 rte_panic("Empty set of forwarding logical cores - check the "
3114                           "core mask supplied in the command parameters\n");
3115
3116         /* Bitrate/latency stats disabled by default */
3117 #ifdef RTE_LIBRTE_BITRATE
3118         bitrate_enabled = 0;
3119 #endif
3120 #ifdef RTE_LIBRTE_LATENCY_STATS
3121         latencystats_enabled = 0;
3122 #endif
3123
3124         /* on FreeBSD, mlockall() is disabled by default */
3125 #ifdef RTE_EXEC_ENV_BSDAPP
3126         do_mlockall = 0;
3127 #else
3128         do_mlockall = 1;
3129 #endif
3130
3131         argc -= diag;
3132         argv += diag;
3133         if (argc > 1)
3134                 launch_args_parse(argc, argv);
3135
3136         if (do_mlockall && mlockall(MCL_CURRENT | MCL_FUTURE)) {
3137                 TESTPMD_LOG(NOTICE, "mlockall() failed with error \"%s\"\n",
3138                         strerror(errno));
3139         }
3140
3141         if (tx_first && interactive)
3142                 rte_exit(EXIT_FAILURE, "--tx-first cannot be used on "
3143                                 "interactive mode.\n");
3144
3145         if (tx_first && lsc_interrupt) {
3146                 printf("Warning: lsc_interrupt needs to be off when "
3147                                 " using tx_first. Disabling.\n");
3148                 lsc_interrupt = 0;
3149         }
3150
3151         if (!nb_rxq && !nb_txq)
3152                 printf("Warning: Either rx or tx queues should be non-zero\n");
3153
3154         if (nb_rxq > 1 && nb_rxq > nb_txq)
3155                 printf("Warning: nb_rxq=%d enables RSS configuration, "
3156                        "but nb_txq=%d will prevent to fully test it.\n",
3157                        nb_rxq, nb_txq);
3158
3159         init_config();
3160
3161         if (hot_plug) {
3162                 ret = rte_dev_hotplug_handle_enable();
3163                 if (ret) {
3164                         RTE_LOG(ERR, EAL,
3165                                 "fail to enable hotplug handling.");
3166                         return -1;
3167                 }
3168
3169                 ret = rte_dev_event_monitor_start();
3170                 if (ret) {
3171                         RTE_LOG(ERR, EAL,
3172                                 "fail to start device event monitoring.");
3173                         return -1;
3174                 }
3175
3176                 ret = rte_dev_event_callback_register(NULL,
3177                         dev_event_callback, NULL);
3178                 if (ret) {
3179                         RTE_LOG(ERR, EAL,
3180                                 "fail  to register device event callback\n");
3181                         return -1;
3182                 }
3183         }
3184
3185         if (start_port(RTE_PORT_ALL) != 0)
3186                 rte_exit(EXIT_FAILURE, "Start ports failed\n");
3187
3188         /* set all ports to promiscuous mode by default */
3189         RTE_ETH_FOREACH_DEV(port_id)
3190                 rte_eth_promiscuous_enable(port_id);
3191
3192         /* Init metrics library */
3193         rte_metrics_init(rte_socket_id());
3194
3195 #ifdef RTE_LIBRTE_LATENCY_STATS
3196         if (latencystats_enabled != 0) {
3197                 int ret = rte_latencystats_init(1, NULL);
3198                 if (ret)
3199                         printf("Warning: latencystats init()"
3200                                 " returned error %d\n", ret);
3201                 printf("Latencystats running on lcore %d\n",
3202                         latencystats_lcore_id);
3203         }
3204 #endif
3205
3206         /* Setup bitrate stats */
3207 #ifdef RTE_LIBRTE_BITRATE
3208         if (bitrate_enabled != 0) {
3209                 bitrate_data = rte_stats_bitrate_create();
3210                 if (bitrate_data == NULL)
3211                         rte_exit(EXIT_FAILURE,
3212                                 "Could not allocate bitrate data.\n");
3213                 rte_stats_bitrate_reg(bitrate_data);
3214         }
3215 #endif
3216
3217 #ifdef RTE_LIBRTE_CMDLINE
3218         if (strlen(cmdline_filename) != 0)
3219                 cmdline_read_from_file(cmdline_filename);
3220
3221         if (interactive == 1) {
3222                 if (auto_start) {
3223                         printf("Start automatic packet forwarding\n");
3224                         start_packet_forwarding(0);
3225                 }
3226                 prompt();
3227                 pmd_test_exit();
3228         } else
3229 #endif
3230         {
3231                 char c;
3232                 int rc;
3233
3234                 f_quit = 0;
3235
3236                 printf("No commandline core given, start packet forwarding\n");
3237                 start_packet_forwarding(tx_first);
3238                 if (stats_period != 0) {
3239                         uint64_t prev_time = 0, cur_time, diff_time = 0;
3240                         uint64_t timer_period;
3241
3242                         /* Convert to number of cycles */
3243                         timer_period = stats_period * rte_get_timer_hz();
3244
3245                         while (f_quit == 0) {
3246                                 cur_time = rte_get_timer_cycles();
3247                                 diff_time += cur_time - prev_time;
3248
3249                                 if (diff_time >= timer_period) {
3250                                         print_stats();
3251                                         /* Reset the timer */
3252                                         diff_time = 0;
3253                                 }
3254                                 /* Sleep to avoid unnecessary checks */
3255                                 prev_time = cur_time;
3256                                 sleep(1);
3257                         }
3258                 }
3259
3260                 printf("Press enter to exit\n");
3261                 rc = read(0, &c, 1);
3262                 pmd_test_exit();
3263                 if (rc < 0)
3264                         return 1;
3265         }
3266
3267         return 0;
3268 }