Branch data Line data Source code
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 : : #ifndef RTE_EXEC_ENV_WINDOWS
13 : : #include <sys/mman.h>
14 : : #endif
15 : : #include <sys/types.h>
16 : : #include <errno.h>
17 : : #include <stdbool.h>
18 : :
19 : : #include <sys/queue.h>
20 : : #include <sys/stat.h>
21 : :
22 : : #include <stdint.h>
23 : : #include <unistd.h>
24 : : #include <inttypes.h>
25 : :
26 : : #include <rte_common.h>
27 : : #include <rte_errno.h>
28 : : #include <rte_byteorder.h>
29 : : #include <rte_log.h>
30 : : #include <rte_debug.h>
31 : : #include <rte_cycles.h>
32 : : #include <rte_memory.h>
33 : : #include <rte_memcpy.h>
34 : : #include <rte_launch.h>
35 : : #include <rte_bus.h>
36 : : #include <rte_eal.h>
37 : : #include <rte_alarm.h>
38 : : #include <rte_per_lcore.h>
39 : : #include <rte_lcore.h>
40 : : #include <rte_branch_prediction.h>
41 : : #include <rte_mempool.h>
42 : : #include <rte_malloc.h>
43 : : #include <rte_mbuf.h>
44 : : #include <rte_mbuf_pool_ops.h>
45 : : #include <rte_interrupts.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_NET_IXGBE
51 : : #include <rte_pmd_ixgbe.h>
52 : : #endif
53 : : #ifdef RTE_LIB_PDUMP
54 : : #include <rte_pdump.h>
55 : : #endif
56 : : #include <rte_flow.h>
57 : : #ifdef RTE_LIB_METRICS
58 : : #include <rte_metrics.h>
59 : : #endif
60 : : #ifdef RTE_LIB_BITRATESTATS
61 : : #include <rte_bitrate.h>
62 : : #endif
63 : : #ifdef RTE_LIB_LATENCYSTATS
64 : : #include <rte_latencystats.h>
65 : : #endif
66 : : #ifdef RTE_EXEC_ENV_WINDOWS
67 : : #include <process.h>
68 : : #endif
69 : : #ifdef RTE_NET_BOND
70 : : #include <rte_eth_bond.h>
71 : : #endif
72 : : #ifdef RTE_NET_MLX5
73 : : #include "mlx5_testpmd.h"
74 : : #endif
75 : :
76 : : #include "testpmd.h"
77 : :
78 : : #ifndef MAP_HUGETLB
79 : : /* FreeBSD may not have MAP_HUGETLB (in fact, it probably doesn't) */
80 : : #define HUGE_FLAG (0x40000)
81 : : #else
82 : : #define HUGE_FLAG MAP_HUGETLB
83 : : #endif
84 : :
85 : : #ifndef MAP_HUGE_SHIFT
86 : : /* older kernels (or FreeBSD) will not have this define */
87 : : #define HUGE_SHIFT (26)
88 : : #else
89 : : #define HUGE_SHIFT MAP_HUGE_SHIFT
90 : : #endif
91 : :
92 : : #define EXTMEM_HEAP_NAME "extmem"
93 : : /*
94 : : * Zone size with the malloc overhead (max of debug and release variants)
95 : : * must fit into the smallest supported hugepage size (2M),
96 : : * so that an IOVA-contiguous zone of this size can always be allocated
97 : : * if there are free 2M hugepages.
98 : : */
99 : : #define EXTBUF_ZONE_SIZE (RTE_PGSIZE_2M - 4 * RTE_CACHE_LINE_SIZE)
100 : :
101 : : uint16_t verbose_level = 0; /**< Silent by default. */
102 : : int testpmd_logtype; /**< Log type for testpmd logs */
103 : :
104 : : /* use main core for command line ? */
105 : : uint8_t interactive = 0;
106 : : uint8_t auto_start = 0;
107 : : uint8_t tx_first;
108 : : char cmdline_filename[PATH_MAX] = {0};
109 : : bool echo_cmdline_file;
110 : :
111 : : /*
112 : : * NUMA support configuration.
113 : : * When set, the NUMA support attempts to dispatch the allocation of the
114 : : * RX and TX memory rings, and of the DMA memory buffers (mbufs) for the
115 : : * probed ports among the CPU sockets 0 and 1.
116 : : * Otherwise, all memory is allocated from CPU socket 0.
117 : : */
118 : : uint8_t numa_support = 1; /**< numa enabled by default */
119 : :
120 : : /*
121 : : * In UMA mode,all memory is allocated from socket 0 if --socket-num is
122 : : * not configured.
123 : : */
124 : : uint8_t socket_num = UMA_NO_CONFIG;
125 : :
126 : : /*
127 : : * Select mempool allocation type:
128 : : * - native: use regular DPDK memory
129 : : * - anon: use regular DPDK memory to create mempool, but populate using
130 : : * anonymous memory (may not be IOVA-contiguous)
131 : : * - xmem: use externally allocated hugepage memory
132 : : */
133 : : uint8_t mp_alloc_type = MP_ALLOC_NATIVE;
134 : :
135 : : /*
136 : : * Store specified sockets on which memory pool to be used by ports
137 : : * is allocated.
138 : : */
139 : : uint8_t port_numa[RTE_MAX_ETHPORTS];
140 : :
141 : : /*
142 : : * Store specified sockets on which RX ring to be used by ports
143 : : * is allocated.
144 : : */
145 : : uint8_t rxring_numa[RTE_MAX_ETHPORTS];
146 : :
147 : : /*
148 : : * Store specified sockets on which TX ring to be used by ports
149 : : * is allocated.
150 : : */
151 : : uint8_t txring_numa[RTE_MAX_ETHPORTS];
152 : :
153 : : /*
154 : : * Record the Ethernet address of peer target ports to which packets are
155 : : * forwarded.
156 : : * Must be instantiated with the ethernet addresses of peer traffic generator
157 : : * ports.
158 : : */
159 : : struct rte_ether_addr peer_eth_addrs[RTE_MAX_ETHPORTS];
160 : : portid_t nb_peer_eth_addrs = 0;
161 : :
162 : : /*
163 : : * Probed Target Environment.
164 : : */
165 : : struct rte_port *ports; /**< For all probed ethernet ports. */
166 : : portid_t nb_ports; /**< Number of probed ethernet ports. */
167 : : struct fwd_lcore **fwd_lcores; /**< For all probed logical cores. */
168 : : lcoreid_t nb_lcores; /**< Number of probed logical cores. */
169 : :
170 : : portid_t ports_ids[RTE_MAX_ETHPORTS]; /**< Store all port ids. */
171 : :
172 : : /*
173 : : * Test Forwarding Configuration.
174 : : * nb_fwd_lcores <= nb_cfg_lcores <= nb_lcores
175 : : * nb_fwd_ports <= nb_cfg_ports <= nb_ports
176 : : */
177 : : lcoreid_t nb_cfg_lcores; /**< Number of configured logical cores. */
178 : : lcoreid_t nb_fwd_lcores; /**< Number of forwarding logical cores. */
179 : : portid_t nb_cfg_ports; /**< Number of configured ports. */
180 : : portid_t nb_fwd_ports; /**< Number of forwarding ports. */
181 : :
182 : : unsigned int fwd_lcores_cpuids[RTE_MAX_LCORE]; /**< CPU ids configuration. */
183 : : portid_t fwd_ports_ids[RTE_MAX_ETHPORTS]; /**< Port ids configuration. */
184 : :
185 : : struct fwd_stream **fwd_streams; /**< For each RX queue of each port. */
186 : : streamid_t nb_fwd_streams; /**< Is equal to (nb_ports * nb_rxq). */
187 : :
188 : : /*
189 : : * Forwarding engines.
190 : : */
191 : : struct fwd_engine * fwd_engines[] = {
192 : : &io_fwd_engine,
193 : : &mac_fwd_engine,
194 : : &mac_swap_engine,
195 : : &flow_gen_engine,
196 : : &rx_only_engine,
197 : : &tx_only_engine,
198 : : &csum_fwd_engine,
199 : : &icmp_echo_engine,
200 : : &noisy_vnf_engine,
201 : : &five_tuple_swap_fwd_engine,
202 : : &recycle_mbufs_engine,
203 : : #ifdef RTE_LIBRTE_IEEE1588
204 : : &ieee1588_fwd_engine,
205 : : #endif
206 : : &shared_rxq_engine,
207 : : NULL,
208 : : };
209 : :
210 : : struct rte_mempool *mempools[RTE_MAX_NUMA_NODES * MAX_SEGS_BUFFER_SPLIT];
211 : : uint16_t mempool_flags;
212 : :
213 : : struct fwd_config cur_fwd_config;
214 : : struct fwd_engine *cur_fwd_eng = &io_fwd_engine; /**< IO mode by default. */
215 : : uint32_t retry_enabled;
216 : : uint32_t burst_tx_delay_time = BURST_TX_WAIT_US;
217 : : uint32_t burst_tx_retry_num = BURST_TX_RETRIES;
218 : :
219 : : uint32_t mbuf_data_size_n = 1; /* Number of specified mbuf sizes. */
220 : : uint16_t mbuf_data_size[MAX_SEGS_BUFFER_SPLIT] = {
221 : : DEFAULT_MBUF_DATA_SIZE
222 : : }; /**< Mbuf data space size. */
223 : : uint32_t param_total_num_mbufs = 0; /**< number of mbufs in all pools - if
224 : : * specified on command-line. */
225 : : uint16_t stats_period; /**< Period to show statistics (disabled by default) */
226 : :
227 : : /** Extended statistics to show. */
228 : : struct rte_eth_xstat_name *xstats_display;
229 : :
230 : : unsigned int xstats_display_num; /**< Size of extended statistics to show */
231 : :
232 : : /*
233 : : * In container, it cannot terminate the process which running with 'stats-period'
234 : : * option. Set flag to exit stats period loop after received SIGINT/SIGTERM.
235 : : */
236 : : volatile uint8_t f_quit;
237 : : uint8_t cl_quit; /* Quit testpmd from cmdline. */
238 : :
239 : : /*
240 : : * Max Rx frame size, set by '--max-pkt-len' parameter.
241 : : */
242 : : uint32_t max_rx_pkt_len;
243 : :
244 : : /*
245 : : * Configuration of packet segments used to scatter received packets
246 : : * if some of split features is configured.
247 : : */
248 : : uint16_t rx_pkt_seg_lengths[MAX_SEGS_BUFFER_SPLIT];
249 : : uint8_t rx_pkt_nb_segs; /**< Number of segments to split */
250 : : uint16_t rx_pkt_seg_offsets[MAX_SEGS_BUFFER_SPLIT];
251 : : uint8_t rx_pkt_nb_offs; /**< Number of specified offsets */
252 : : uint32_t rx_pkt_hdr_protos[MAX_SEGS_BUFFER_SPLIT];
253 : :
254 : : uint8_t multi_rx_mempool; /**< Enables multi-rx-mempool feature */
255 : :
256 : : /*
257 : : * Configuration of packet segments used by the "txonly" processing engine.
258 : : */
259 : : uint16_t tx_pkt_length = TXONLY_DEF_PACKET_LEN; /**< TXONLY packet length. */
260 : : uint16_t tx_pkt_seg_lengths[RTE_MAX_SEGS_PER_PKT] = {
261 : : TXONLY_DEF_PACKET_LEN,
262 : : };
263 : : uint8_t tx_pkt_nb_segs = 1; /**< Number of segments in TXONLY packets */
264 : :
265 : : enum tx_pkt_split tx_pkt_split = TX_PKT_SPLIT_OFF;
266 : : /**< Split policy for packets to TX. */
267 : :
268 : : uint8_t txonly_multi_flow;
269 : : /**< Whether multiple flows are generated in TXONLY mode. */
270 : :
271 : : uint32_t tx_pkt_times_inter;
272 : : /**< Timings for send scheduling in TXONLY mode, time between bursts. */
273 : :
274 : : uint32_t tx_pkt_times_intra;
275 : : /**< Timings for send scheduling in TXONLY mode, time between packets. */
276 : :
277 : : uint16_t nb_pkt_per_burst = DEF_PKT_BURST; /**< Number of packets per burst. */
278 : : uint16_t nb_pkt_flowgen_clones; /**< Number of Tx packet clones to send in flowgen mode. */
279 : : int nb_flows_flowgen = 1024; /**< Number of flows in flowgen mode. */
280 : : uint16_t mb_mempool_cache = DEF_MBUF_CACHE; /**< Size of mbuf mempool cache. */
281 : :
282 : : /* current configuration is in DCB or not,0 means it is not in DCB mode */
283 : : uint8_t dcb_config = 0;
284 : :
285 : : /*
286 : : * Configurable number of RX/TX queues.
287 : : */
288 : : queueid_t nb_rxq = 1; /**< Number of RX queues per port. */
289 : : queueid_t nb_txq = 1; /**< Number of TX queues per port. */
290 : :
291 : : /*
292 : : * Configurable number of RX/TX ring descriptors.
293 : : * Defaults are supplied by drivers via ethdev.
294 : : */
295 : : #define RX_DESC_DEFAULT 0
296 : : #define TX_DESC_DEFAULT 0
297 : : uint16_t nb_rxd = RX_DESC_DEFAULT; /**< Number of RX descriptors. */
298 : : uint16_t nb_txd = TX_DESC_DEFAULT; /**< Number of TX descriptors. */
299 : :
300 : : #define RTE_PMD_PARAM_UNSET -1
301 : : /*
302 : : * Configurable values of RX and TX ring threshold registers.
303 : : */
304 : :
305 : : int8_t rx_pthresh = RTE_PMD_PARAM_UNSET;
306 : : int8_t rx_hthresh = RTE_PMD_PARAM_UNSET;
307 : : int8_t rx_wthresh = RTE_PMD_PARAM_UNSET;
308 : :
309 : : int8_t tx_pthresh = RTE_PMD_PARAM_UNSET;
310 : : int8_t tx_hthresh = RTE_PMD_PARAM_UNSET;
311 : : int8_t tx_wthresh = RTE_PMD_PARAM_UNSET;
312 : :
313 : : /*
314 : : * Configurable value of RX free threshold.
315 : : */
316 : : int16_t rx_free_thresh = RTE_PMD_PARAM_UNSET;
317 : :
318 : : /*
319 : : * Configurable value of RX drop enable.
320 : : */
321 : : int8_t rx_drop_en = RTE_PMD_PARAM_UNSET;
322 : :
323 : : /*
324 : : * Configurable value of TX free threshold.
325 : : */
326 : : int16_t tx_free_thresh = RTE_PMD_PARAM_UNSET;
327 : :
328 : : /*
329 : : * Configurable value of TX RS bit threshold.
330 : : */
331 : : int16_t tx_rs_thresh = RTE_PMD_PARAM_UNSET;
332 : :
333 : : /*
334 : : * Configurable sub-forwarding mode for the noisy_vnf forwarding mode.
335 : : */
336 : : enum noisy_fwd_mode noisy_fwd_mode;
337 : :
338 : : /* String version of enum noisy_fwd_mode */
339 : : const char * const noisy_fwd_mode_desc[] = {
340 : : [NOISY_FWD_MODE_IO] = "io",
341 : : [NOISY_FWD_MODE_MAC] = "mac",
342 : : [NOISY_FWD_MODE_MACSWAP] = "macswap",
343 : : [NOISY_FWD_MODE_5TSWAP] = "5tswap",
344 : : [NOISY_FWD_MODE_MAX] = NULL,
345 : : };
346 : :
347 : : /*
348 : : * Configurable value of buffered packets before sending.
349 : : */
350 : : uint16_t noisy_tx_sw_bufsz;
351 : :
352 : : /*
353 : : * Configurable value of packet buffer timeout.
354 : : */
355 : : uint16_t noisy_tx_sw_buf_flush_time;
356 : :
357 : : /*
358 : : * Configurable value for size of VNF internal memory area
359 : : * used for simulating noisy neighbour behaviour
360 : : */
361 : : uint64_t noisy_lkup_mem_sz;
362 : :
363 : : /*
364 : : * Configurable value of number of random writes done in
365 : : * VNF simulation memory area.
366 : : */
367 : : uint64_t noisy_lkup_num_writes;
368 : :
369 : : /*
370 : : * Configurable value of number of random reads done in
371 : : * VNF simulation memory area.
372 : : */
373 : : uint64_t noisy_lkup_num_reads;
374 : :
375 : : /*
376 : : * Configurable value of number of random reads/writes done in
377 : : * VNF simulation memory area.
378 : : */
379 : : uint64_t noisy_lkup_num_reads_writes;
380 : :
381 : : /*
382 : : * Receive Side Scaling (RSS) configuration.
383 : : */
384 : : uint64_t rss_hf = RTE_ETH_RSS_IP; /* RSS IP by default. */
385 : :
386 : : /*
387 : : * Port topology configuration
388 : : */
389 : : uint16_t port_topology = PORT_TOPOLOGY_PAIRED; /* Ports are paired by default */
390 : :
391 : : /*
392 : : * Avoids to flush all the RX streams before starts forwarding.
393 : : */
394 : : uint8_t no_flush_rx = 0; /* flush by default */
395 : :
396 : : /*
397 : : * Flow API isolated mode.
398 : : */
399 : : uint8_t flow_isolate_all;
400 : :
401 : : /*
402 : : * Disable port flow flush when stop port.
403 : : */
404 : : uint8_t no_flow_flush = 0; /* do flow flush by default */
405 : :
406 : : /*
407 : : * Avoids to check link status when starting/stopping a port.
408 : : */
409 : : uint8_t no_link_check = 0; /* check by default */
410 : :
411 : : /*
412 : : * Don't automatically start all ports in interactive mode.
413 : : */
414 : : uint8_t no_device_start = 0;
415 : :
416 : : /*
417 : : * Enable link status change notification
418 : : */
419 : : uint8_t lsc_interrupt = 1; /* enabled by default */
420 : :
421 : : /*
422 : : * Enable device removal notification.
423 : : */
424 : : uint8_t rmv_interrupt = 1; /* enabled by default */
425 : :
426 : : uint8_t hot_plug = 0; /**< hotplug disabled by default. */
427 : :
428 : : /* After attach, port setup is called on event or by iterator */
429 : : bool setup_on_probe_event = true;
430 : :
431 : : /* Clear ptypes on port initialization. */
432 : : uint8_t clear_ptypes = true;
433 : :
434 : : /* Pretty printing of ethdev events */
435 : : static const char * const eth_event_desc[] = {
436 : : [RTE_ETH_EVENT_UNKNOWN] = "unknown",
437 : : [RTE_ETH_EVENT_INTR_LSC] = "link state change",
438 : : [RTE_ETH_EVENT_QUEUE_STATE] = "queue state",
439 : : [RTE_ETH_EVENT_INTR_RESET] = "reset",
440 : : [RTE_ETH_EVENT_VF_MBOX] = "VF mbox",
441 : : [RTE_ETH_EVENT_IPSEC] = "IPsec",
442 : : [RTE_ETH_EVENT_MACSEC] = "MACsec",
443 : : [RTE_ETH_EVENT_INTR_RMV] = "device removal",
444 : : [RTE_ETH_EVENT_NEW] = "device probed",
445 : : [RTE_ETH_EVENT_DESTROY] = "device released",
446 : : [RTE_ETH_EVENT_FLOW_AGED] = "flow aged",
447 : : [RTE_ETH_EVENT_RX_AVAIL_THRESH] = "RxQ available descriptors threshold reached",
448 : : [RTE_ETH_EVENT_ERR_RECOVERING] = "error recovering",
449 : : [RTE_ETH_EVENT_RECOVERY_SUCCESS] = "error recovery successful",
450 : : [RTE_ETH_EVENT_RECOVERY_FAILED] = "error recovery failed",
451 : : [RTE_ETH_EVENT_MAX] = NULL,
452 : : };
453 : :
454 : : /*
455 : : * Display or mask ether events
456 : : * Default to all events except VF_MBOX
457 : : */
458 : : uint32_t event_print_mask = (UINT32_C(1) << RTE_ETH_EVENT_UNKNOWN) |
459 : : (UINT32_C(1) << RTE_ETH_EVENT_INTR_LSC) |
460 : : (UINT32_C(1) << RTE_ETH_EVENT_QUEUE_STATE) |
461 : : (UINT32_C(1) << RTE_ETH_EVENT_INTR_RESET) |
462 : : (UINT32_C(1) << RTE_ETH_EVENT_IPSEC) |
463 : : (UINT32_C(1) << RTE_ETH_EVENT_MACSEC) |
464 : : (UINT32_C(1) << RTE_ETH_EVENT_INTR_RMV) |
465 : : (UINT32_C(1) << RTE_ETH_EVENT_FLOW_AGED) |
466 : : (UINT32_C(1) << RTE_ETH_EVENT_ERR_RECOVERING) |
467 : : (UINT32_C(1) << RTE_ETH_EVENT_RECOVERY_SUCCESS) |
468 : : (UINT32_C(1) << RTE_ETH_EVENT_RECOVERY_FAILED);
469 : : /*
470 : : * Decide if all memory are locked for performance.
471 : : */
472 : : int do_mlockall = 0;
473 : :
474 : : #ifdef RTE_LIB_LATENCYSTATS
475 : :
476 : : /*
477 : : * Set when latency stats is enabled in the commandline
478 : : */
479 : : uint8_t latencystats_enabled;
480 : :
481 : : /*
482 : : * Lcore ID to service latency statistics.
483 : : */
484 : : lcoreid_t latencystats_lcore_id = -1;
485 : :
486 : : #endif
487 : :
488 : : /*
489 : : * Ethernet device configuration.
490 : : */
491 : : struct rte_eth_rxmode rx_mode;
492 : :
493 : : struct rte_eth_txmode tx_mode = {
494 : : .offloads = RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE,
495 : : };
496 : :
497 : : volatile int test_done = 1; /* stop packet forwarding when set to 1. */
498 : :
499 : : /*
500 : : * Display zero values by default for xstats
501 : : */
502 : : uint8_t xstats_hide_zero;
503 : :
504 : : /*
505 : : * Display of xstats without their state disabled by default
506 : : */
507 : : uint8_t xstats_show_state;
508 : :
509 : : /*
510 : : * Display disabled xstat by default for xstats
511 : : */
512 : : uint8_t xstats_hide_disabled;
513 : :
514 : : /*
515 : : * Measure of CPU cycles disabled by default
516 : : */
517 : : uint8_t record_core_cycles;
518 : :
519 : : /*
520 : : * Display of RX and TX bursts disabled by default
521 : : */
522 : : uint8_t record_burst_stats;
523 : :
524 : : /*
525 : : * Number of ports per shared Rx queue group, 0 disable.
526 : : */
527 : : uint32_t rxq_share;
528 : :
529 : : unsigned int num_sockets = 0;
530 : : unsigned int socket_ids[RTE_MAX_NUMA_NODES];
531 : :
532 : : #ifdef RTE_LIB_BITRATESTATS
533 : : /* Bitrate statistics */
534 : : struct rte_stats_bitrates *bitrate_data;
535 : : lcoreid_t bitrate_lcore_id;
536 : : uint8_t bitrate_enabled;
537 : : #endif
538 : :
539 : : #ifdef RTE_LIB_GRO
540 : : struct gro_status gro_ports[RTE_MAX_ETHPORTS];
541 : : uint8_t gro_flush_cycles = GRO_DEFAULT_FLUSH_CYCLES;
542 : : #endif
543 : :
544 : : /*
545 : : * hexadecimal bitmask of RX mq mode can be enabled.
546 : : */
547 : : enum rte_eth_rx_mq_mode rx_mq_mode = RTE_ETH_MQ_RX_VMDQ_DCB_RSS;
548 : :
549 : : /*
550 : : * Used to set forced link speed
551 : : */
552 : : uint32_t eth_link_speed;
553 : :
554 : : /*
555 : : * ID of the current process in multi-process, used to
556 : : * configure the queues to be polled.
557 : : */
558 : : int proc_id;
559 : :
560 : : /*
561 : : * Number of processes in multi-process, used to
562 : : * configure the queues to be polled.
563 : : */
564 : : unsigned int num_procs = 1;
565 : :
566 : : static void
567 : 0 : eth_rx_metadata_negotiate_mp(uint16_t port_id)
568 : : {
569 : 0 : uint64_t rx_meta_features = 0;
570 : : int ret;
571 : :
572 : 0 : if (!is_proc_primary())
573 : 0 : return;
574 : :
575 : 0 : rx_meta_features |= RTE_ETH_RX_METADATA_USER_FLAG;
576 : 0 : rx_meta_features |= RTE_ETH_RX_METADATA_USER_MARK;
577 : 0 : rx_meta_features |= RTE_ETH_RX_METADATA_TUNNEL_ID;
578 : :
579 : 0 : ret = rte_eth_rx_metadata_negotiate(port_id, &rx_meta_features);
580 : 0 : if (ret == 0) {
581 : 0 : if (!(rx_meta_features & RTE_ETH_RX_METADATA_USER_FLAG)) {
582 : 0 : TESTPMD_LOG(DEBUG, "Flow action FLAG will not affect Rx mbufs on port %u\n",
583 : : port_id);
584 : : }
585 : :
586 : 0 : if (!(rx_meta_features & RTE_ETH_RX_METADATA_USER_MARK)) {
587 : 0 : TESTPMD_LOG(DEBUG, "Flow action MARK will not affect Rx mbufs on port %u\n",
588 : : port_id);
589 : : }
590 : :
591 : 0 : if (!(rx_meta_features & RTE_ETH_RX_METADATA_TUNNEL_ID)) {
592 : 0 : TESTPMD_LOG(DEBUG, "Flow tunnel offload support might be limited or unavailable on port %u\n",
593 : : port_id);
594 : : }
595 : 0 : } else if (ret != -ENOTSUP) {
596 : 0 : rte_exit(EXIT_FAILURE, "Error when negotiating Rx meta features on port %u: %s\n",
597 : : port_id, rte_strerror(-ret));
598 : : }
599 : : }
600 : :
601 : : static int
602 : : eth_dev_configure_mp(uint16_t port_id, uint16_t nb_rx_q, uint16_t nb_tx_q,
603 : : const struct rte_eth_conf *dev_conf)
604 : : {
605 : 0 : if (is_proc_primary())
606 : 0 : return rte_eth_dev_configure(port_id, nb_rx_q, nb_tx_q,
607 : : dev_conf);
608 : : return 0;
609 : : }
610 : :
611 : : static int
612 : 0 : change_bonding_member_port_status(portid_t bond_pid, bool is_stop)
613 : : {
614 : : #ifdef RTE_NET_BOND
615 : :
616 : : portid_t member_pids[RTE_MAX_ETHPORTS];
617 : : struct rte_port *port;
618 : : int num_members;
619 : : portid_t member_pid;
620 : : int i;
621 : :
622 : 0 : num_members = rte_eth_bond_members_get(bond_pid, member_pids,
623 : : RTE_MAX_ETHPORTS);
624 : 0 : if (num_members < 0) {
625 : 0 : fprintf(stderr, "Failed to get member list for port = %u\n",
626 : : bond_pid);
627 : 0 : return num_members;
628 : : }
629 : :
630 : 0 : for (i = 0; i < num_members; i++) {
631 : 0 : member_pid = member_pids[i];
632 : 0 : port = &ports[member_pid];
633 : 0 : port->port_status =
634 : 0 : is_stop ? RTE_PORT_STOPPED : RTE_PORT_STARTED;
635 : : }
636 : : #else
637 : : RTE_SET_USED(bond_pid);
638 : : RTE_SET_USED(is_stop);
639 : : #endif
640 : : return 0;
641 : : }
642 : :
643 : : static int
644 : 0 : eth_dev_start_mp(uint16_t port_id)
645 : : {
646 : : int ret;
647 : :
648 : 0 : if (is_proc_primary()) {
649 : 0 : ret = rte_eth_dev_start(port_id);
650 : 0 : if (ret != 0)
651 : : return ret;
652 : :
653 : 0 : struct rte_port *port = &ports[port_id];
654 : :
655 : : /*
656 : : * Starting a bonding port also starts all members under the bonding
657 : : * device. So if this port is bond device, we need to modify the
658 : : * port status of these members.
659 : : */
660 : 0 : if (port->bond_flag == 1)
661 : 0 : return change_bonding_member_port_status(port_id, false);
662 : : }
663 : :
664 : : return 0;
665 : : }
666 : :
667 : : static int
668 : 0 : eth_dev_stop_mp(uint16_t port_id)
669 : : {
670 : : int ret;
671 : :
672 : 0 : if (is_proc_primary()) {
673 : 0 : ret = rte_eth_dev_stop(port_id);
674 : 0 : if (ret != 0)
675 : : return ret;
676 : :
677 : 0 : struct rte_port *port = &ports[port_id];
678 : :
679 : : /*
680 : : * Stopping a bonding port also stops all members under the bonding
681 : : * device. So if this port is bond device, we need to modify the
682 : : * port status of these members.
683 : : */
684 : 0 : if (port->bond_flag == 1)
685 : 0 : return change_bonding_member_port_status(port_id, true);
686 : : }
687 : :
688 : : return 0;
689 : : }
690 : :
691 : : static void
692 : : mempool_free_mp(struct rte_mempool *mp)
693 : : {
694 : 0 : if (is_proc_primary())
695 : 0 : rte_mempool_free(mp);
696 : : }
697 : :
698 : : static int
699 : : eth_dev_set_mtu_mp(uint16_t port_id, uint16_t mtu)
700 : : {
701 : 0 : if (is_proc_primary())
702 : 0 : return rte_eth_dev_set_mtu(port_id, mtu);
703 : :
704 : : return 0;
705 : : }
706 : :
707 : : /* Forward function declarations */
708 : : static void setup_attached_port(void *arg);
709 : : static void check_all_ports_link_status(uint32_t port_mask);
710 : : static int eth_event_callback(portid_t port_id,
711 : : enum rte_eth_event_type type,
712 : : void *param, void *ret_param);
713 : : static void dev_event_callback(const char *device_name,
714 : : enum rte_dev_event_type type,
715 : : void *param);
716 : : static void fill_xstats_display_info(void);
717 : :
718 : : /*
719 : : * Check if all the ports are started.
720 : : * If yes, return positive value. If not, return zero.
721 : : */
722 : : static int all_ports_started(void);
723 : :
724 : : #ifdef RTE_LIB_GSO
725 : : struct gso_status gso_ports[RTE_MAX_ETHPORTS];
726 : : uint16_t gso_max_segment_size = RTE_ETHER_MAX_LEN - RTE_ETHER_CRC_LEN;
727 : : #endif
728 : :
729 : : /* Holds the registered mbuf dynamic flags names. */
730 : : char dynf_names[64][RTE_MBUF_DYN_NAMESIZE];
731 : :
732 : :
733 : : /*
734 : : * Helper function to check if socket is already discovered.
735 : : * If yes, return positive value. If not, return zero.
736 : : */
737 : : int
738 : 0 : new_socket_id(unsigned int socket_id)
739 : : {
740 : : unsigned int i;
741 : :
742 : 0 : for (i = 0; i < num_sockets; i++) {
743 : 0 : if (socket_ids[i] == socket_id)
744 : : return 0;
745 : : }
746 : : return 1;
747 : : }
748 : :
749 : : /*
750 : : * Setup default configuration.
751 : : */
752 : : static void
753 : 0 : set_default_fwd_lcores_config(void)
754 : : {
755 : : unsigned int i;
756 : : unsigned int nb_lc;
757 : : unsigned int sock_num;
758 : :
759 : : nb_lc = 0;
760 : 0 : for (i = 0; i < RTE_MAX_LCORE; i++) {
761 : 0 : if (!rte_lcore_is_enabled(i))
762 : 0 : continue;
763 : 0 : sock_num = rte_lcore_to_socket_id(i);
764 : 0 : if (new_socket_id(sock_num)) {
765 : 0 : if (num_sockets >= RTE_MAX_NUMA_NODES) {
766 : 0 : rte_exit(EXIT_FAILURE,
767 : : "Total sockets greater than %u\n",
768 : : RTE_MAX_NUMA_NODES);
769 : : }
770 : 0 : socket_ids[num_sockets++] = sock_num;
771 : : }
772 : 0 : if (i == rte_get_main_lcore())
773 : 0 : continue;
774 : 0 : fwd_lcores_cpuids[nb_lc++] = i;
775 : : }
776 : 0 : nb_lcores = (lcoreid_t) nb_lc;
777 : 0 : nb_cfg_lcores = nb_lcores;
778 : 0 : nb_fwd_lcores = 1;
779 : 0 : }
780 : :
781 : : static void
782 : : set_def_peer_eth_addrs(void)
783 : : {
784 : : portid_t i;
785 : :
786 : 0 : for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
787 : 0 : peer_eth_addrs[i].addr_bytes[0] = RTE_ETHER_LOCAL_ADMIN_ADDR;
788 : 0 : peer_eth_addrs[i].addr_bytes[5] = i;
789 : : }
790 : : }
791 : :
792 : : static void
793 : 0 : set_default_fwd_ports_config(void)
794 : : {
795 : : portid_t pt_id;
796 : : int i = 0;
797 : :
798 : 0 : RTE_ETH_FOREACH_DEV(pt_id) {
799 : 0 : fwd_ports_ids[i++] = pt_id;
800 : :
801 : : /* Update sockets info according to the attached device */
802 : 0 : int socket_id = rte_eth_dev_socket_id(pt_id);
803 : 0 : if (socket_id >= 0 && new_socket_id(socket_id)) {
804 : 0 : if (num_sockets >= RTE_MAX_NUMA_NODES) {
805 : 0 : rte_exit(EXIT_FAILURE,
806 : : "Total sockets greater than %u\n",
807 : : RTE_MAX_NUMA_NODES);
808 : : }
809 : 0 : socket_ids[num_sockets++] = socket_id;
810 : : }
811 : : }
812 : :
813 : 0 : nb_cfg_ports = nb_ports;
814 : 0 : nb_fwd_ports = nb_ports;
815 : 0 : }
816 : :
817 : : void
818 : 0 : set_def_fwd_config(void)
819 : : {
820 : 0 : set_default_fwd_lcores_config();
821 : : set_def_peer_eth_addrs();
822 : 0 : set_default_fwd_ports_config();
823 : 0 : }
824 : :
825 : : #ifndef RTE_EXEC_ENV_WINDOWS
826 : : /* extremely pessimistic estimation of memory required to create a mempool */
827 : : static int
828 : 0 : calc_mem_size(uint32_t nb_mbufs, uint32_t mbuf_sz, size_t pgsz, size_t *out)
829 : : {
830 : : unsigned int n_pages, mbuf_per_pg, leftover;
831 : : uint64_t total_mem, mbuf_mem, obj_sz;
832 : :
833 : : /* there is no good way to predict how much space the mempool will
834 : : * occupy because it will allocate chunks on the fly, and some of those
835 : : * will come from default DPDK memory while some will come from our
836 : : * external memory, so just assume 128MB will be enough for everyone.
837 : : */
838 : : uint64_t hdr_mem = 128 << 20;
839 : :
840 : : /* account for possible non-contiguousness */
841 : 0 : obj_sz = rte_mempool_calc_obj_size(mbuf_sz, 0, NULL);
842 : 0 : if (obj_sz > pgsz) {
843 : 0 : TESTPMD_LOG(ERR, "Object size is bigger than page size\n");
844 : 0 : return -1;
845 : : }
846 : :
847 : 0 : mbuf_per_pg = pgsz / obj_sz;
848 : 0 : leftover = (nb_mbufs % mbuf_per_pg) > 0;
849 : 0 : n_pages = (nb_mbufs / mbuf_per_pg) + leftover;
850 : :
851 : 0 : mbuf_mem = n_pages * pgsz;
852 : :
853 : 0 : total_mem = RTE_ALIGN(hdr_mem + mbuf_mem, pgsz);
854 : :
855 : : if (total_mem > SIZE_MAX) {
856 : : TESTPMD_LOG(ERR, "Memory size too big\n");
857 : : return -1;
858 : : }
859 : 0 : *out = (size_t)total_mem;
860 : :
861 : 0 : return 0;
862 : : }
863 : :
864 : : static int
865 : 0 : pagesz_flags(uint64_t page_sz)
866 : : {
867 : : /* as per mmap() manpage, all page sizes are log2 of page size
868 : : * shifted by MAP_HUGE_SHIFT
869 : : */
870 : 0 : int log2 = rte_log2_u64(page_sz);
871 : :
872 : 0 : return (log2 << HUGE_SHIFT);
873 : : }
874 : :
875 : : static void *
876 : 0 : alloc_mem(size_t memsz, size_t pgsz, bool huge)
877 : : {
878 : : void *addr;
879 : : int flags;
880 : :
881 : : /* allocate anonymous hugepages */
882 : : flags = MAP_ANONYMOUS | MAP_PRIVATE;
883 : 0 : if (huge)
884 : 0 : flags |= HUGE_FLAG | pagesz_flags(pgsz);
885 : :
886 : 0 : addr = mmap(NULL, memsz, PROT_READ | PROT_WRITE, flags, -1, 0);
887 : 0 : if (addr == MAP_FAILED)
888 : 0 : return NULL;
889 : :
890 : : return addr;
891 : : }
892 : :
893 : : struct extmem_param {
894 : : void *addr;
895 : : size_t len;
896 : : size_t pgsz;
897 : : rte_iova_t *iova_table;
898 : : unsigned int iova_table_len;
899 : : };
900 : :
901 : : static int
902 : 0 : create_extmem(uint32_t nb_mbufs, uint32_t mbuf_sz, struct extmem_param *param,
903 : : bool huge)
904 : : {
905 : 0 : uint64_t pgsizes[] = {RTE_PGSIZE_2M, RTE_PGSIZE_1G, /* x86_64, ARM */
906 : : RTE_PGSIZE_16M, RTE_PGSIZE_16G}; /* POWER */
907 : : unsigned int cur_page, n_pages, pgsz_idx;
908 : : size_t mem_sz, cur_pgsz;
909 : : rte_iova_t *iovas = NULL;
910 : : void *addr;
911 : : int ret;
912 : :
913 : 0 : for (pgsz_idx = 0; pgsz_idx < RTE_DIM(pgsizes); pgsz_idx++) {
914 : : /* skip anything that is too big */
915 : : if (pgsizes[pgsz_idx] > SIZE_MAX)
916 : : continue;
917 : :
918 : 0 : cur_pgsz = pgsizes[pgsz_idx];
919 : :
920 : : /* if we were told not to allocate hugepages, override */
921 : 0 : if (!huge)
922 : 0 : cur_pgsz = sysconf(_SC_PAGESIZE);
923 : :
924 : 0 : ret = calc_mem_size(nb_mbufs, mbuf_sz, cur_pgsz, &mem_sz);
925 : 0 : if (ret < 0) {
926 : 0 : TESTPMD_LOG(ERR, "Cannot calculate memory size\n");
927 : 0 : return -1;
928 : : }
929 : :
930 : : /* allocate our memory */
931 : 0 : addr = alloc_mem(mem_sz, cur_pgsz, huge);
932 : :
933 : : /* if we couldn't allocate memory with a specified page size,
934 : : * that doesn't mean we can't do it with other page sizes, so
935 : : * try another one.
936 : : */
937 : 0 : if (addr == NULL)
938 : : continue;
939 : :
940 : : /* store IOVA addresses for every page in this memory area */
941 : 0 : n_pages = mem_sz / cur_pgsz;
942 : :
943 : 0 : iovas = malloc(sizeof(*iovas) * n_pages);
944 : :
945 : 0 : if (iovas == NULL) {
946 : 0 : TESTPMD_LOG(ERR, "Cannot allocate memory for iova addresses\n");
947 : 0 : goto fail;
948 : : }
949 : : /* lock memory if it's not huge pages */
950 : 0 : if (!huge)
951 : 0 : mlock(addr, mem_sz);
952 : :
953 : : /* populate IOVA addresses */
954 : 0 : for (cur_page = 0; cur_page < n_pages; cur_page++) {
955 : : rte_iova_t iova;
956 : : size_t offset;
957 : : void *cur;
958 : :
959 : 0 : offset = cur_pgsz * cur_page;
960 : 0 : cur = RTE_PTR_ADD(addr, offset);
961 : :
962 : : /* touch the page before getting its IOVA */
963 : 0 : *(volatile char *)cur = 0;
964 : :
965 : 0 : iova = rte_mem_virt2iova(cur);
966 : :
967 : 0 : iovas[cur_page] = iova;
968 : : }
969 : :
970 : : break;
971 : : }
972 : : /* if we couldn't allocate anything */
973 : 0 : if (iovas == NULL)
974 : : return -1;
975 : :
976 : 0 : param->addr = addr;
977 : 0 : param->len = mem_sz;
978 : 0 : param->pgsz = cur_pgsz;
979 : 0 : param->iova_table = iovas;
980 : 0 : param->iova_table_len = n_pages;
981 : :
982 : 0 : return 0;
983 : : fail:
984 : : free(iovas);
985 : : if (addr)
986 : 0 : munmap(addr, mem_sz);
987 : :
988 : 0 : return -1;
989 : : }
990 : :
991 : : static int
992 : 0 : setup_extmem(uint32_t nb_mbufs, uint32_t mbuf_sz, bool huge)
993 : : {
994 : : struct extmem_param param;
995 : : int socket_id, ret;
996 : :
997 : : memset(¶m, 0, sizeof(param));
998 : :
999 : : /* check if our heap exists */
1000 : 0 : socket_id = rte_malloc_heap_get_socket(EXTMEM_HEAP_NAME);
1001 : 0 : if (socket_id < 0) {
1002 : : /* create our heap */
1003 : 0 : ret = rte_malloc_heap_create(EXTMEM_HEAP_NAME);
1004 : 0 : if (ret < 0) {
1005 : 0 : TESTPMD_LOG(ERR, "Cannot create heap\n");
1006 : 0 : return -1;
1007 : : }
1008 : : }
1009 : :
1010 : 0 : ret = create_extmem(nb_mbufs, mbuf_sz, ¶m, huge);
1011 : 0 : if (ret < 0) {
1012 : 0 : TESTPMD_LOG(ERR, "Cannot create memory area\n");
1013 : 0 : return -1;
1014 : : }
1015 : :
1016 : : /* we now have a valid memory area, so add it to heap */
1017 : 0 : ret = rte_malloc_heap_memory_add(EXTMEM_HEAP_NAME,
1018 : : param.addr, param.len, param.iova_table,
1019 : : param.iova_table_len, param.pgsz);
1020 : :
1021 : : /* when using VFIO, memory is automatically mapped for DMA by EAL */
1022 : :
1023 : : /* not needed any more */
1024 : 0 : free(param.iova_table);
1025 : :
1026 : 0 : if (ret < 0) {
1027 : 0 : TESTPMD_LOG(ERR, "Cannot add memory to heap\n");
1028 : 0 : munmap(param.addr, param.len);
1029 : 0 : return -1;
1030 : : }
1031 : :
1032 : : /* success */
1033 : :
1034 : 0 : TESTPMD_LOG(DEBUG, "Allocated %zuMB of external memory\n",
1035 : : param.len >> 20);
1036 : :
1037 : 0 : return 0;
1038 : : }
1039 : : static void
1040 : 0 : dma_unmap_cb(struct rte_mempool *mp __rte_unused, void *opaque __rte_unused,
1041 : : struct rte_mempool_memhdr *memhdr, unsigned mem_idx __rte_unused)
1042 : : {
1043 : : uint16_t pid = 0;
1044 : : int ret;
1045 : :
1046 : 0 : RTE_ETH_FOREACH_DEV(pid) {
1047 : : struct rte_eth_dev_info dev_info;
1048 : :
1049 : 0 : ret = eth_dev_info_get_print_err(pid, &dev_info);
1050 : 0 : if (ret != 0) {
1051 : 0 : TESTPMD_LOG(DEBUG,
1052 : : "unable to get device info for port %d on addr 0x%p,"
1053 : : "mempool unmapping will not be performed\n",
1054 : : pid, memhdr->addr);
1055 : 0 : continue;
1056 : : }
1057 : :
1058 : 0 : ret = rte_dev_dma_unmap(dev_info.device, memhdr->addr, 0, memhdr->len);
1059 : 0 : if (ret) {
1060 : 0 : TESTPMD_LOG(DEBUG,
1061 : : "unable to DMA unmap addr 0x%p "
1062 : : "for device %s\n",
1063 : : memhdr->addr, rte_dev_name(dev_info.device));
1064 : : }
1065 : : }
1066 : 0 : ret = rte_extmem_unregister(memhdr->addr, memhdr->len);
1067 : 0 : if (ret) {
1068 : 0 : TESTPMD_LOG(DEBUG,
1069 : : "unable to un-register addr 0x%p\n", memhdr->addr);
1070 : : }
1071 : 0 : }
1072 : :
1073 : : static void
1074 : 0 : dma_map_cb(struct rte_mempool *mp __rte_unused, void *opaque __rte_unused,
1075 : : struct rte_mempool_memhdr *memhdr, unsigned mem_idx __rte_unused)
1076 : : {
1077 : : uint16_t pid = 0;
1078 : 0 : size_t page_size = sysconf(_SC_PAGESIZE);
1079 : : int ret;
1080 : :
1081 : 0 : ret = rte_extmem_register(memhdr->addr, memhdr->len, NULL, 0,
1082 : : page_size);
1083 : 0 : if (ret) {
1084 : 0 : TESTPMD_LOG(DEBUG,
1085 : : "unable to register addr 0x%p\n", memhdr->addr);
1086 : 0 : return;
1087 : : }
1088 : 0 : RTE_ETH_FOREACH_DEV(pid) {
1089 : : struct rte_eth_dev_info dev_info;
1090 : :
1091 : 0 : ret = eth_dev_info_get_print_err(pid, &dev_info);
1092 : 0 : if (ret != 0) {
1093 : 0 : TESTPMD_LOG(DEBUG,
1094 : : "unable to get device info for port %d on addr 0x%p,"
1095 : : "mempool mapping will not be performed\n",
1096 : : pid, memhdr->addr);
1097 : 0 : continue;
1098 : : }
1099 : 0 : ret = rte_dev_dma_map(dev_info.device, memhdr->addr, 0, memhdr->len);
1100 : 0 : if (ret) {
1101 : 0 : TESTPMD_LOG(DEBUG,
1102 : : "unable to DMA map addr 0x%p "
1103 : : "for device %s\n",
1104 : : memhdr->addr, rte_dev_name(dev_info.device));
1105 : : }
1106 : : }
1107 : : }
1108 : : #endif
1109 : :
1110 : : static unsigned int
1111 : 0 : setup_extbuf(uint32_t nb_mbufs, uint16_t mbuf_sz, unsigned int socket_id,
1112 : : char *pool_name, struct rte_pktmbuf_extmem **ext_mem)
1113 : : {
1114 : : struct rte_pktmbuf_extmem *xmem;
1115 : : unsigned int ext_num, zone_num, elt_num;
1116 : : uint16_t elt_size;
1117 : :
1118 : 0 : elt_size = RTE_ALIGN_CEIL(mbuf_sz, RTE_CACHE_LINE_SIZE);
1119 : 0 : elt_num = EXTBUF_ZONE_SIZE / elt_size;
1120 : 0 : zone_num = (nb_mbufs + elt_num - 1) / elt_num;
1121 : :
1122 : 0 : xmem = malloc(sizeof(struct rte_pktmbuf_extmem) * zone_num);
1123 : 0 : if (xmem == NULL) {
1124 : 0 : TESTPMD_LOG(ERR, "Cannot allocate memory for "
1125 : : "external buffer descriptors\n");
1126 : 0 : *ext_mem = NULL;
1127 : 0 : return 0;
1128 : : }
1129 : 0 : for (ext_num = 0; ext_num < zone_num; ext_num++) {
1130 : 0 : struct rte_pktmbuf_extmem *xseg = xmem + ext_num;
1131 : : const struct rte_memzone *mz;
1132 : : char mz_name[RTE_MEMZONE_NAMESIZE];
1133 : : int ret;
1134 : :
1135 : : ret = snprintf(mz_name, sizeof(mz_name),
1136 : : RTE_MEMPOOL_MZ_FORMAT "_xb_%u", pool_name, ext_num);
1137 : 0 : if (ret < 0 || ret >= (int)sizeof(mz_name)) {
1138 : 0 : errno = ENAMETOOLONG;
1139 : : ext_num = 0;
1140 : 0 : break;
1141 : : }
1142 : 0 : mz = rte_memzone_reserve(mz_name, EXTBUF_ZONE_SIZE,
1143 : : socket_id,
1144 : : RTE_MEMZONE_IOVA_CONTIG |
1145 : : RTE_MEMZONE_1GB |
1146 : : RTE_MEMZONE_SIZE_HINT_ONLY);
1147 : 0 : if (mz == NULL) {
1148 : : /*
1149 : : * The caller exits on external buffer creation
1150 : : * error, so there is no need to free memzones.
1151 : : */
1152 : 0 : errno = ENOMEM;
1153 : : ext_num = 0;
1154 : 0 : break;
1155 : : }
1156 : 0 : xseg->buf_ptr = mz->addr;
1157 : 0 : xseg->buf_iova = mz->iova;
1158 : 0 : xseg->buf_len = EXTBUF_ZONE_SIZE;
1159 : 0 : xseg->elt_size = elt_size;
1160 : : }
1161 : 0 : if (ext_num == 0 && xmem != NULL) {
1162 : 0 : free(xmem);
1163 : : xmem = NULL;
1164 : : }
1165 : 0 : *ext_mem = xmem;
1166 : 0 : return ext_num;
1167 : : }
1168 : :
1169 : : /*
1170 : : * Configuration initialisation done once at init time.
1171 : : */
1172 : : static struct rte_mempool *
1173 : 0 : mbuf_pool_create(uint16_t mbuf_seg_size, unsigned nb_mbuf,
1174 : : unsigned int socket_id, uint16_t size_idx)
1175 : : {
1176 : : char pool_name[RTE_MEMPOOL_NAMESIZE];
1177 : : struct rte_mempool *rte_mp = NULL;
1178 : : #ifndef RTE_EXEC_ENV_WINDOWS
1179 : : uint32_t mb_size;
1180 : :
1181 : 0 : mb_size = sizeof(struct rte_mbuf) + mbuf_seg_size;
1182 : : #endif
1183 : 0 : mbuf_poolname_build(socket_id, pool_name, sizeof(pool_name), size_idx);
1184 : 0 : if (!is_proc_primary()) {
1185 : 0 : rte_mp = rte_mempool_lookup(pool_name);
1186 : 0 : if (rte_mp == NULL)
1187 : 0 : rte_exit(EXIT_FAILURE,
1188 : : "Get mbuf pool for socket %u failed: %s\n",
1189 : : socket_id, rte_strerror(rte_errno));
1190 : : return rte_mp;
1191 : : }
1192 : :
1193 : 0 : TESTPMD_LOG(INFO,
1194 : : "create a new mbuf pool <%s>: n=%u, size=%u, socket=%u\n",
1195 : : pool_name, nb_mbuf, mbuf_seg_size, socket_id);
1196 : :
1197 : 0 : switch (mp_alloc_type) {
1198 : 0 : case MP_ALLOC_NATIVE:
1199 : : {
1200 : : /* wrapper to rte_mempool_create() */
1201 : 0 : TESTPMD_LOG(INFO, "preferred mempool ops selected: %s\n",
1202 : : rte_mbuf_best_mempool_ops());
1203 : 0 : rte_mp = rte_pktmbuf_pool_create(pool_name, nb_mbuf,
1204 : : mb_mempool_cache, 0, mbuf_seg_size, socket_id);
1205 : 0 : break;
1206 : : }
1207 : : #ifndef RTE_EXEC_ENV_WINDOWS
1208 : 0 : case MP_ALLOC_ANON:
1209 : : {
1210 : 0 : rte_mp = rte_mempool_create_empty(pool_name, nb_mbuf,
1211 : : mb_size, (unsigned int) mb_mempool_cache,
1212 : : sizeof(struct rte_pktmbuf_pool_private),
1213 : : socket_id, mempool_flags);
1214 : 0 : if (rte_mp == NULL)
1215 : 0 : goto err;
1216 : :
1217 : 0 : if (rte_mempool_populate_anon(rte_mp) == 0) {
1218 : 0 : rte_mempool_free(rte_mp);
1219 : : rte_mp = NULL;
1220 : 0 : goto err;
1221 : : }
1222 : 0 : rte_pktmbuf_pool_init(rte_mp, NULL);
1223 : 0 : rte_mempool_obj_iter(rte_mp, rte_pktmbuf_init, NULL);
1224 : 0 : rte_mempool_mem_iter(rte_mp, dma_map_cb, NULL);
1225 : 0 : break;
1226 : : }
1227 : 0 : case MP_ALLOC_XMEM:
1228 : : case MP_ALLOC_XMEM_HUGE:
1229 : : {
1230 : : int heap_socket;
1231 : 0 : bool huge = mp_alloc_type == MP_ALLOC_XMEM_HUGE;
1232 : :
1233 : 0 : if (setup_extmem(nb_mbuf, mbuf_seg_size, huge) < 0)
1234 : 0 : rte_exit(EXIT_FAILURE, "Could not create external memory\n");
1235 : :
1236 : : heap_socket =
1237 : 0 : rte_malloc_heap_get_socket(EXTMEM_HEAP_NAME);
1238 : 0 : if (heap_socket < 0)
1239 : 0 : rte_exit(EXIT_FAILURE, "Could not get external memory socket ID\n");
1240 : :
1241 : 0 : TESTPMD_LOG(INFO, "preferred mempool ops selected: %s\n",
1242 : : rte_mbuf_best_mempool_ops());
1243 : 0 : rte_mp = rte_pktmbuf_pool_create(pool_name, nb_mbuf,
1244 : : mb_mempool_cache, 0, mbuf_seg_size,
1245 : : heap_socket);
1246 : 0 : break;
1247 : : }
1248 : : #endif
1249 : 0 : case MP_ALLOC_XBUF:
1250 : : {
1251 : : struct rte_pktmbuf_extmem *ext_mem;
1252 : : unsigned int ext_num;
1253 : :
1254 : 0 : ext_num = setup_extbuf(nb_mbuf, mbuf_seg_size,
1255 : : socket_id, pool_name, &ext_mem);
1256 : 0 : if (ext_num == 0)
1257 : 0 : rte_exit(EXIT_FAILURE,
1258 : : "Can't create pinned data buffers\n");
1259 : :
1260 : 0 : TESTPMD_LOG(INFO, "preferred mempool ops selected: %s\n",
1261 : : rte_mbuf_best_mempool_ops());
1262 : 0 : rte_mp = rte_pktmbuf_pool_create_extbuf
1263 : : (pool_name, nb_mbuf, mb_mempool_cache,
1264 : : 0, mbuf_seg_size, socket_id,
1265 : : ext_mem, ext_num);
1266 : 0 : free(ext_mem);
1267 : : break;
1268 : : }
1269 : 0 : default:
1270 : : {
1271 : 0 : rte_exit(EXIT_FAILURE, "Invalid mempool creation mode\n");
1272 : : }
1273 : : }
1274 : :
1275 : : #ifndef RTE_EXEC_ENV_WINDOWS
1276 : 0 : err:
1277 : : #endif
1278 : 0 : if (rte_mp == NULL) {
1279 : 0 : rte_exit(EXIT_FAILURE,
1280 : : "Creation of mbuf pool for socket %u failed: %s\n",
1281 : : socket_id, rte_strerror(rte_errno));
1282 : 0 : } else if (verbose_level > 0) {
1283 : 0 : rte_mempool_dump(stdout, rte_mp);
1284 : : }
1285 : : return rte_mp;
1286 : : }
1287 : :
1288 : : /*
1289 : : * Check given socket id is valid or not with NUMA mode,
1290 : : * if valid, return 0, else return -1
1291 : : */
1292 : : static int
1293 : 0 : check_socket_id(const unsigned int socket_id)
1294 : : {
1295 : : static int warning_once = 0;
1296 : :
1297 : 0 : if (new_socket_id(socket_id)) {
1298 : 0 : if (!warning_once && numa_support)
1299 : 0 : fprintf(stderr,
1300 : : "Warning: NUMA should be configured manually by using --port-numa-config and --ring-numa-config parameters along with --numa.\n");
1301 : 0 : warning_once = 1;
1302 : 0 : return -1;
1303 : : }
1304 : : return 0;
1305 : : }
1306 : :
1307 : : /*
1308 : : * Get the allowed maximum number of RX queues.
1309 : : * *pid return the port id which has minimal value of
1310 : : * max_rx_queues in all ports.
1311 : : */
1312 : : queueid_t
1313 : 0 : get_allowed_max_nb_rxq(portid_t *pid)
1314 : : {
1315 : : queueid_t allowed_max_rxq = RTE_MAX_QUEUES_PER_PORT;
1316 : : bool max_rxq_valid = false;
1317 : : portid_t pi;
1318 : : struct rte_eth_dev_info dev_info;
1319 : :
1320 : 0 : RTE_ETH_FOREACH_DEV(pi) {
1321 : 0 : if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1322 : 0 : continue;
1323 : :
1324 : : max_rxq_valid = true;
1325 : 0 : if (dev_info.max_rx_queues < allowed_max_rxq) {
1326 : : allowed_max_rxq = dev_info.max_rx_queues;
1327 : 0 : *pid = pi;
1328 : : }
1329 : : }
1330 : 0 : return max_rxq_valid ? allowed_max_rxq : 0;
1331 : : }
1332 : :
1333 : : /*
1334 : : * Check input rxq is valid or not.
1335 : : * If input rxq is not greater than any of maximum number
1336 : : * of RX queues of all ports, it is valid.
1337 : : * if valid, return 0, else return -1
1338 : : */
1339 : : int
1340 : 0 : check_nb_rxq(queueid_t rxq)
1341 : : {
1342 : : queueid_t allowed_max_rxq;
1343 : 0 : portid_t pid = 0;
1344 : :
1345 : 0 : allowed_max_rxq = get_allowed_max_nb_rxq(&pid);
1346 : 0 : if (rxq > allowed_max_rxq) {
1347 : 0 : fprintf(stderr,
1348 : : "Fail: input rxq (%u) can't be greater than max_rx_queues (%u) of port %u\n",
1349 : : rxq, allowed_max_rxq, pid);
1350 : 0 : return -1;
1351 : : }
1352 : : return 0;
1353 : : }
1354 : :
1355 : : /*
1356 : : * Get the allowed maximum number of TX queues.
1357 : : * *pid return the port id which has minimal value of
1358 : : * max_tx_queues in all ports.
1359 : : */
1360 : : queueid_t
1361 : 0 : get_allowed_max_nb_txq(portid_t *pid)
1362 : : {
1363 : : queueid_t allowed_max_txq = RTE_MAX_QUEUES_PER_PORT;
1364 : : bool max_txq_valid = false;
1365 : : portid_t pi;
1366 : : struct rte_eth_dev_info dev_info;
1367 : :
1368 : 0 : RTE_ETH_FOREACH_DEV(pi) {
1369 : 0 : if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1370 : 0 : continue;
1371 : :
1372 : : max_txq_valid = true;
1373 : 0 : if (dev_info.max_tx_queues < allowed_max_txq) {
1374 : : allowed_max_txq = dev_info.max_tx_queues;
1375 : 0 : *pid = pi;
1376 : : }
1377 : : }
1378 : 0 : return max_txq_valid ? allowed_max_txq : 0;
1379 : : }
1380 : :
1381 : : /*
1382 : : * Check input txq is valid or not.
1383 : : * If input txq is not greater than any of maximum number
1384 : : * of TX queues of all ports, it is valid.
1385 : : * if valid, return 0, else return -1
1386 : : */
1387 : : int
1388 : 0 : check_nb_txq(queueid_t txq)
1389 : : {
1390 : : queueid_t allowed_max_txq;
1391 : 0 : portid_t pid = 0;
1392 : :
1393 : 0 : allowed_max_txq = get_allowed_max_nb_txq(&pid);
1394 : 0 : if (txq > allowed_max_txq) {
1395 : 0 : fprintf(stderr,
1396 : : "Fail: input txq (%u) can't be greater than max_tx_queues (%u) of port %u\n",
1397 : : txq, allowed_max_txq, pid);
1398 : 0 : return -1;
1399 : : }
1400 : : return 0;
1401 : : }
1402 : :
1403 : : /*
1404 : : * Get the allowed maximum number of RXDs of every rx queue.
1405 : : * *pid return the port id which has minimal value of
1406 : : * max_rxd in all queues of all ports.
1407 : : */
1408 : : static uint16_t
1409 : 0 : get_allowed_max_nb_rxd(portid_t *pid)
1410 : : {
1411 : : uint16_t allowed_max_rxd = UINT16_MAX;
1412 : : portid_t pi;
1413 : : struct rte_eth_dev_info dev_info;
1414 : :
1415 : 0 : RTE_ETH_FOREACH_DEV(pi) {
1416 : 0 : if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1417 : 0 : continue;
1418 : :
1419 : 0 : if (dev_info.rx_desc_lim.nb_max < allowed_max_rxd) {
1420 : : allowed_max_rxd = dev_info.rx_desc_lim.nb_max;
1421 : 0 : *pid = pi;
1422 : : }
1423 : : }
1424 : 0 : return allowed_max_rxd;
1425 : : }
1426 : :
1427 : : /*
1428 : : * Get the allowed minimal number of RXDs of every rx queue.
1429 : : * *pid return the port id which has minimal value of
1430 : : * min_rxd in all queues of all ports.
1431 : : */
1432 : : static uint16_t
1433 : 0 : get_allowed_min_nb_rxd(portid_t *pid)
1434 : : {
1435 : : uint16_t allowed_min_rxd = 0;
1436 : : portid_t pi;
1437 : : struct rte_eth_dev_info dev_info;
1438 : :
1439 : 0 : RTE_ETH_FOREACH_DEV(pi) {
1440 : 0 : if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1441 : 0 : continue;
1442 : :
1443 : 0 : if (dev_info.rx_desc_lim.nb_min > allowed_min_rxd) {
1444 : : allowed_min_rxd = dev_info.rx_desc_lim.nb_min;
1445 : 0 : *pid = pi;
1446 : : }
1447 : : }
1448 : :
1449 : 0 : return allowed_min_rxd;
1450 : : }
1451 : :
1452 : : /*
1453 : : * Check input rxd is valid or not.
1454 : : * If input rxd is not greater than any of maximum number
1455 : : * of RXDs of every Rx queues and is not less than any of
1456 : : * minimal number of RXDs of every Rx queues, it is valid.
1457 : : * if valid, return 0, else return -1
1458 : : */
1459 : : int
1460 : 0 : check_nb_rxd(queueid_t rxd)
1461 : : {
1462 : : uint16_t allowed_max_rxd;
1463 : : uint16_t allowed_min_rxd;
1464 : 0 : portid_t pid = 0;
1465 : :
1466 : 0 : allowed_max_rxd = get_allowed_max_nb_rxd(&pid);
1467 : 0 : if (rxd > allowed_max_rxd) {
1468 : 0 : fprintf(stderr,
1469 : : "Fail: input rxd (%u) can't be greater than max_rxds (%u) of port %u\n",
1470 : : rxd, allowed_max_rxd, pid);
1471 : 0 : return -1;
1472 : : }
1473 : :
1474 : 0 : allowed_min_rxd = get_allowed_min_nb_rxd(&pid);
1475 : 0 : if (rxd < allowed_min_rxd) {
1476 : 0 : fprintf(stderr,
1477 : : "Fail: input rxd (%u) can't be less than min_rxds (%u) of port %u\n",
1478 : : rxd, allowed_min_rxd, pid);
1479 : 0 : return -1;
1480 : : }
1481 : :
1482 : : return 0;
1483 : : }
1484 : :
1485 : : /*
1486 : : * Get the allowed maximum number of TXDs of every rx queues.
1487 : : * *pid return the port id which has minimal value of
1488 : : * max_txd in every tx queue.
1489 : : */
1490 : : static uint16_t
1491 : 0 : get_allowed_max_nb_txd(portid_t *pid)
1492 : : {
1493 : : uint16_t allowed_max_txd = UINT16_MAX;
1494 : : portid_t pi;
1495 : : struct rte_eth_dev_info dev_info;
1496 : :
1497 : 0 : RTE_ETH_FOREACH_DEV(pi) {
1498 : 0 : if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1499 : 0 : continue;
1500 : :
1501 : 0 : if (dev_info.tx_desc_lim.nb_max < allowed_max_txd) {
1502 : : allowed_max_txd = dev_info.tx_desc_lim.nb_max;
1503 : 0 : *pid = pi;
1504 : : }
1505 : : }
1506 : 0 : return allowed_max_txd;
1507 : : }
1508 : :
1509 : : /*
1510 : : * Get the allowed maximum number of TXDs of every tx queues.
1511 : : * *pid return the port id which has minimal value of
1512 : : * min_txd in every tx queue.
1513 : : */
1514 : : static uint16_t
1515 : 0 : get_allowed_min_nb_txd(portid_t *pid)
1516 : : {
1517 : : uint16_t allowed_min_txd = 0;
1518 : : portid_t pi;
1519 : : struct rte_eth_dev_info dev_info;
1520 : :
1521 : 0 : RTE_ETH_FOREACH_DEV(pi) {
1522 : 0 : if (eth_dev_info_get_print_err(pi, &dev_info) != 0)
1523 : 0 : continue;
1524 : :
1525 : 0 : if (dev_info.tx_desc_lim.nb_min > allowed_min_txd) {
1526 : : allowed_min_txd = dev_info.tx_desc_lim.nb_min;
1527 : 0 : *pid = pi;
1528 : : }
1529 : : }
1530 : :
1531 : 0 : return allowed_min_txd;
1532 : : }
1533 : :
1534 : : /*
1535 : : * Check input txd is valid or not.
1536 : : * If input txd is not greater than any of maximum number
1537 : : * of TXDs of every Rx queues, it is valid.
1538 : : * if valid, return 0, else return -1
1539 : : */
1540 : : int
1541 : 0 : check_nb_txd(queueid_t txd)
1542 : : {
1543 : : uint16_t allowed_max_txd;
1544 : : uint16_t allowed_min_txd;
1545 : 0 : portid_t pid = 0;
1546 : :
1547 : 0 : allowed_max_txd = get_allowed_max_nb_txd(&pid);
1548 : 0 : if (txd > allowed_max_txd) {
1549 : 0 : fprintf(stderr,
1550 : : "Fail: input txd (%u) can't be greater than max_txds (%u) of port %u\n",
1551 : : txd, allowed_max_txd, pid);
1552 : 0 : return -1;
1553 : : }
1554 : :
1555 : 0 : allowed_min_txd = get_allowed_min_nb_txd(&pid);
1556 : 0 : if (txd < allowed_min_txd) {
1557 : 0 : fprintf(stderr,
1558 : : "Fail: input txd (%u) can't be less than min_txds (%u) of port %u\n",
1559 : : txd, allowed_min_txd, pid);
1560 : 0 : return -1;
1561 : : }
1562 : : return 0;
1563 : : }
1564 : :
1565 : : static int
1566 : : get_eth_overhead(struct rte_eth_dev_info *dev_info)
1567 : : {
1568 : : uint32_t eth_overhead;
1569 : :
1570 : 0 : if (dev_info->max_mtu != UINT16_MAX &&
1571 : 0 : dev_info->max_rx_pktlen > dev_info->max_mtu)
1572 : 0 : eth_overhead = dev_info->max_rx_pktlen - dev_info->max_mtu;
1573 : : else
1574 : : eth_overhead = RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN;
1575 : :
1576 : : return eth_overhead;
1577 : : }
1578 : :
1579 : : static void
1580 : 0 : init_config_port_offloads(portid_t pid, uint32_t socket_id)
1581 : : {
1582 : 0 : struct rte_port *port = &ports[pid];
1583 : : int ret;
1584 : : int i;
1585 : :
1586 : 0 : eth_rx_metadata_negotiate_mp(pid);
1587 : :
1588 : 0 : port->dev_conf.txmode = tx_mode;
1589 : 0 : port->dev_conf.rxmode = rx_mode;
1590 : :
1591 : 0 : ret = eth_dev_info_get_print_err(pid, &port->dev_info);
1592 : 0 : if (ret != 0)
1593 : 0 : rte_exit(EXIT_FAILURE, "rte_eth_dev_info_get() failed\n");
1594 : :
1595 : 0 : if (!(port->dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE))
1596 : 0 : port->dev_conf.txmode.offloads &=
1597 : : ~RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
1598 : :
1599 : : /* Apply Rx offloads configuration */
1600 : 0 : for (i = 0; i < port->dev_info.max_rx_queues; i++)
1601 : 0 : port->rxq[i].conf.offloads = port->dev_conf.rxmode.offloads;
1602 : : /* Apply Tx offloads configuration */
1603 : 0 : for (i = 0; i < port->dev_info.max_tx_queues; i++)
1604 : 0 : port->txq[i].conf.offloads = port->dev_conf.txmode.offloads;
1605 : :
1606 : 0 : if (eth_link_speed)
1607 : 0 : port->dev_conf.link_speeds = eth_link_speed;
1608 : :
1609 : 0 : if (max_rx_pkt_len)
1610 : 0 : port->dev_conf.rxmode.mtu = max_rx_pkt_len -
1611 : : get_eth_overhead(&port->dev_info);
1612 : :
1613 : : /* set flag to initialize port/queue */
1614 : 0 : port->need_reconfig = 1;
1615 : 0 : port->need_reconfig_queues = 1;
1616 : 0 : port->socket_id = socket_id;
1617 : 0 : port->tx_metadata = 0;
1618 : :
1619 : : /*
1620 : : * Check for maximum number of segments per MTU.
1621 : : * Accordingly update the mbuf data size.
1622 : : */
1623 : 0 : if (port->dev_info.rx_desc_lim.nb_mtu_seg_max != UINT16_MAX &&
1624 : : port->dev_info.rx_desc_lim.nb_mtu_seg_max != 0) {
1625 : : uint32_t eth_overhead = get_eth_overhead(&port->dev_info);
1626 : : uint16_t mtu;
1627 : :
1628 : 0 : if (rte_eth_dev_get_mtu(pid, &mtu) == 0) {
1629 : 0 : uint16_t data_size = (mtu + eth_overhead) /
1630 : 0 : port->dev_info.rx_desc_lim.nb_mtu_seg_max;
1631 : 0 : uint16_t buffer_size = data_size + RTE_PKTMBUF_HEADROOM;
1632 : :
1633 : 0 : if (buffer_size > mbuf_data_size[0]) {
1634 : 0 : mbuf_data_size[0] = buffer_size;
1635 : 0 : TESTPMD_LOG(WARNING,
1636 : : "Configured mbuf size of the first segment %hu\n",
1637 : : mbuf_data_size[0]);
1638 : : }
1639 : : }
1640 : : }
1641 : 0 : }
1642 : :
1643 : : static void
1644 : 0 : init_config(void)
1645 : : {
1646 : : portid_t pid;
1647 : : struct rte_mempool *mbp;
1648 : : unsigned int nb_mbuf_per_pool;
1649 : : lcoreid_t lc_id;
1650 : : #ifdef RTE_LIB_GRO
1651 : : struct rte_gro_param gro_param;
1652 : : #endif
1653 : : #ifdef RTE_LIB_GSO
1654 : : uint32_t gso_types;
1655 : : #endif
1656 : :
1657 : : /* Configuration of logical cores. */
1658 : 0 : fwd_lcores = rte_zmalloc("testpmd: fwd_lcores",
1659 : : sizeof(struct fwd_lcore *) * nb_lcores,
1660 : : RTE_CACHE_LINE_SIZE);
1661 : 0 : if (fwd_lcores == NULL) {
1662 : 0 : rte_exit(EXIT_FAILURE, "rte_zmalloc(%d (struct fwd_lcore *)) "
1663 : : "failed\n", nb_lcores);
1664 : : }
1665 : 0 : for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
1666 : 0 : fwd_lcores[lc_id] = rte_zmalloc("testpmd: struct fwd_lcore",
1667 : : sizeof(struct fwd_lcore),
1668 : : RTE_CACHE_LINE_SIZE);
1669 : 0 : if (fwd_lcores[lc_id] == NULL) {
1670 : 0 : rte_exit(EXIT_FAILURE, "rte_zmalloc(struct fwd_lcore) "
1671 : : "failed\n");
1672 : : }
1673 : 0 : fwd_lcores[lc_id]->cpuid_idx = lc_id;
1674 : : }
1675 : :
1676 : 0 : RTE_ETH_FOREACH_DEV(pid) {
1677 : : uint32_t socket_id;
1678 : :
1679 : 0 : if (numa_support) {
1680 : 0 : socket_id = port_numa[pid];
1681 : 0 : if (port_numa[pid] == NUMA_NO_CONFIG) {
1682 : 0 : socket_id = rte_eth_dev_socket_id(pid);
1683 : :
1684 : : /*
1685 : : * if socket_id is invalid,
1686 : : * set to the first available socket.
1687 : : */
1688 : 0 : if (check_socket_id(socket_id) < 0)
1689 : 0 : socket_id = socket_ids[0];
1690 : : }
1691 : : } else {
1692 : 0 : socket_id = (socket_num == UMA_NO_CONFIG) ?
1693 : 0 : 0 : socket_num;
1694 : : }
1695 : : /* Apply default TxRx configuration for all ports */
1696 : 0 : init_config_port_offloads(pid, socket_id);
1697 : : }
1698 : : /*
1699 : : * Create pools of mbuf.
1700 : : * If NUMA support is disabled, create a single pool of mbuf in
1701 : : * socket 0 memory by default.
1702 : : * Otherwise, create a pool of mbuf in the memory of sockets 0 and 1.
1703 : : *
1704 : : * Use the maximum value of nb_rxd and nb_txd here, then nb_rxd and
1705 : : * nb_txd can be configured at run time.
1706 : : */
1707 : 0 : if (param_total_num_mbufs)
1708 : : nb_mbuf_per_pool = param_total_num_mbufs;
1709 : : else {
1710 : 0 : nb_mbuf_per_pool = RX_DESC_MAX +
1711 : 0 : (nb_lcores * mb_mempool_cache) +
1712 : : TX_DESC_MAX + MAX_PKT_BURST;
1713 : 0 : nb_mbuf_per_pool *= RTE_MAX_ETHPORTS;
1714 : : }
1715 : :
1716 : 0 : if (numa_support) {
1717 : : uint8_t i, j;
1718 : :
1719 : 0 : for (i = 0; i < num_sockets; i++)
1720 : 0 : for (j = 0; j < mbuf_data_size_n; j++)
1721 : 0 : mempools[i * MAX_SEGS_BUFFER_SPLIT + j] =
1722 : 0 : mbuf_pool_create(mbuf_data_size[j],
1723 : : nb_mbuf_per_pool,
1724 : : socket_ids[i], j);
1725 : : } else {
1726 : : uint8_t i;
1727 : :
1728 : 0 : for (i = 0; i < mbuf_data_size_n; i++)
1729 : 0 : mempools[i] = mbuf_pool_create
1730 : 0 : (mbuf_data_size[i],
1731 : : nb_mbuf_per_pool,
1732 : 0 : socket_num == UMA_NO_CONFIG ?
1733 : : 0 : socket_num, i);
1734 : : }
1735 : :
1736 : 0 : init_port_config();
1737 : :
1738 : : #ifdef RTE_LIB_GSO
1739 : : gso_types = RTE_ETH_TX_OFFLOAD_TCP_TSO | RTE_ETH_TX_OFFLOAD_VXLAN_TNL_TSO |
1740 : : RTE_ETH_TX_OFFLOAD_GRE_TNL_TSO | RTE_ETH_TX_OFFLOAD_UDP_TSO;
1741 : : #endif
1742 : : /*
1743 : : * Records which Mbuf pool to use by each logical core, if needed.
1744 : : */
1745 : 0 : for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
1746 : 0 : mbp = mbuf_pool_find(
1747 : : rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]), 0);
1748 : :
1749 : 0 : if (mbp == NULL)
1750 : : mbp = mbuf_pool_find(0, 0);
1751 : 0 : fwd_lcores[lc_id]->mbp = mbp;
1752 : : #ifdef RTE_LIB_GSO
1753 : : /* initialize GSO context */
1754 : 0 : fwd_lcores[lc_id]->gso_ctx.direct_pool = mbp;
1755 : 0 : fwd_lcores[lc_id]->gso_ctx.indirect_pool = mbp;
1756 : 0 : fwd_lcores[lc_id]->gso_ctx.gso_types = gso_types;
1757 : 0 : fwd_lcores[lc_id]->gso_ctx.gso_size = RTE_ETHER_MAX_LEN -
1758 : : RTE_ETHER_CRC_LEN;
1759 : 0 : fwd_lcores[lc_id]->gso_ctx.flag = 0;
1760 : : #endif
1761 : : }
1762 : :
1763 : 0 : fwd_config_setup();
1764 : :
1765 : : #ifdef RTE_LIB_GRO
1766 : : /* create a gro context for each lcore */
1767 : 0 : gro_param.gro_types = RTE_GRO_TCP_IPV4;
1768 : 0 : gro_param.max_flow_num = GRO_MAX_FLUSH_CYCLES;
1769 : 0 : gro_param.max_item_per_flow = MAX_PKT_BURST;
1770 : 0 : for (lc_id = 0; lc_id < nb_lcores; lc_id++) {
1771 : 0 : gro_param.socket_id = rte_lcore_to_socket_id(
1772 : : fwd_lcores_cpuids[lc_id]);
1773 : 0 : fwd_lcores[lc_id]->gro_ctx = rte_gro_ctx_create(&gro_param);
1774 : 0 : if (fwd_lcores[lc_id]->gro_ctx == NULL) {
1775 : 0 : rte_exit(EXIT_FAILURE,
1776 : : "rte_gro_ctx_create() failed\n");
1777 : : }
1778 : : }
1779 : : #endif
1780 : 0 : }
1781 : :
1782 : :
1783 : : void
1784 : 0 : reconfig(portid_t new_port_id, unsigned socket_id)
1785 : : {
1786 : : /* Reconfiguration of Ethernet ports. */
1787 : 0 : init_config_port_offloads(new_port_id, socket_id);
1788 : 0 : init_port_config();
1789 : 0 : }
1790 : :
1791 : : int
1792 : 0 : init_fwd_streams(void)
1793 : : {
1794 : : portid_t pid;
1795 : : struct rte_port *port;
1796 : : streamid_t sm_id, nb_fwd_streams_new;
1797 : : queueid_t q;
1798 : :
1799 : : /* set socket id according to numa or not */
1800 : 0 : RTE_ETH_FOREACH_DEV(pid) {
1801 : 0 : port = &ports[pid];
1802 : 0 : if (nb_rxq > port->dev_info.max_rx_queues) {
1803 : 0 : fprintf(stderr,
1804 : : "Fail: nb_rxq(%d) is greater than max_rx_queues(%d)\n",
1805 : : nb_rxq, port->dev_info.max_rx_queues);
1806 : 0 : return -1;
1807 : : }
1808 : 0 : if (nb_txq > port->dev_info.max_tx_queues) {
1809 : 0 : fprintf(stderr,
1810 : : "Fail: nb_txq(%d) is greater than max_tx_queues(%d)\n",
1811 : : nb_txq, port->dev_info.max_tx_queues);
1812 : 0 : return -1;
1813 : : }
1814 : 0 : if (numa_support) {
1815 : 0 : if (port_numa[pid] != NUMA_NO_CONFIG)
1816 : 0 : port->socket_id = port_numa[pid];
1817 : : else {
1818 : 0 : port->socket_id = rte_eth_dev_socket_id(pid);
1819 : :
1820 : : /*
1821 : : * if socket_id is invalid,
1822 : : * set to the first available socket.
1823 : : */
1824 : 0 : if (check_socket_id(port->socket_id) < 0)
1825 : 0 : port->socket_id = socket_ids[0];
1826 : : }
1827 : : }
1828 : : else {
1829 : 0 : if (socket_num == UMA_NO_CONFIG)
1830 : 0 : port->socket_id = 0;
1831 : : else
1832 : 0 : port->socket_id = socket_num;
1833 : : }
1834 : : }
1835 : :
1836 : 0 : q = RTE_MAX(nb_rxq, nb_txq);
1837 : 0 : if (q == 0) {
1838 : 0 : fprintf(stderr,
1839 : : "Fail: Cannot allocate fwd streams as number of queues is 0\n");
1840 : 0 : return -1;
1841 : : }
1842 : 0 : nb_fwd_streams_new = (streamid_t)(nb_ports * q);
1843 : 0 : if (nb_fwd_streams_new == nb_fwd_streams)
1844 : : return 0;
1845 : : /* clear the old */
1846 : 0 : if (fwd_streams != NULL) {
1847 : 0 : for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
1848 : 0 : if (fwd_streams[sm_id] == NULL)
1849 : 0 : continue;
1850 : 0 : rte_free(fwd_streams[sm_id]);
1851 : 0 : fwd_streams[sm_id] = NULL;
1852 : : }
1853 : 0 : rte_free(fwd_streams);
1854 : 0 : fwd_streams = NULL;
1855 : : }
1856 : :
1857 : : /* init new */
1858 : 0 : nb_fwd_streams = nb_fwd_streams_new;
1859 : 0 : if (nb_fwd_streams) {
1860 : 0 : fwd_streams = rte_zmalloc("testpmd: fwd_streams",
1861 : : sizeof(struct fwd_stream *) * nb_fwd_streams,
1862 : : RTE_CACHE_LINE_SIZE);
1863 : 0 : if (fwd_streams == NULL)
1864 : 0 : rte_exit(EXIT_FAILURE, "rte_zmalloc(%d"
1865 : : " (struct fwd_stream *)) failed\n",
1866 : : nb_fwd_streams);
1867 : :
1868 : 0 : for (sm_id = 0; sm_id < nb_fwd_streams; sm_id++) {
1869 : 0 : fwd_streams[sm_id] = rte_zmalloc("testpmd:"
1870 : : " struct fwd_stream", sizeof(struct fwd_stream),
1871 : : RTE_CACHE_LINE_SIZE);
1872 : 0 : if (fwd_streams[sm_id] == NULL)
1873 : 0 : rte_exit(EXIT_FAILURE, "rte_zmalloc"
1874 : : "(struct fwd_stream) failed\n");
1875 : : }
1876 : : }
1877 : :
1878 : : return 0;
1879 : : }
1880 : :
1881 : : static void
1882 : 0 : pkt_burst_stats_display(const char *rx_tx, struct pkt_burst_stats *pbs)
1883 : : {
1884 : : uint64_t total_burst, sburst;
1885 : : uint64_t nb_burst;
1886 : : uint64_t burst_stats[4];
1887 : : uint16_t pktnb_stats[4];
1888 : : uint16_t nb_pkt;
1889 : : int burst_percent[4], sburstp;
1890 : : int i;
1891 : :
1892 : : /*
1893 : : * First compute the total number of packet bursts and the
1894 : : * two highest numbers of bursts of the same number of packets.
1895 : : */
1896 : : memset(&burst_stats, 0x0, sizeof(burst_stats));
1897 : : memset(&pktnb_stats, 0x0, sizeof(pktnb_stats));
1898 : :
1899 : : /* Show stats for 0 burst size always */
1900 : 0 : total_burst = pbs->pkt_burst_spread[0];
1901 : 0 : burst_stats[0] = pbs->pkt_burst_spread[0];
1902 : : pktnb_stats[0] = 0;
1903 : :
1904 : : /* Find the next 2 burst sizes with highest occurrences. */
1905 : 0 : for (nb_pkt = 1; nb_pkt < MAX_PKT_BURST + 1; nb_pkt++) {
1906 : 0 : nb_burst = pbs->pkt_burst_spread[nb_pkt];
1907 : :
1908 : 0 : if (nb_burst == 0)
1909 : 0 : continue;
1910 : :
1911 : 0 : total_burst += nb_burst;
1912 : :
1913 : 0 : if (nb_burst > burst_stats[1]) {
1914 : 0 : burst_stats[2] = burst_stats[1];
1915 : 0 : pktnb_stats[2] = pktnb_stats[1];
1916 : 0 : burst_stats[1] = nb_burst;
1917 : 0 : pktnb_stats[1] = nb_pkt;
1918 : 0 : } else if (nb_burst > burst_stats[2]) {
1919 : 0 : burst_stats[2] = nb_burst;
1920 : 0 : pktnb_stats[2] = nb_pkt;
1921 : : }
1922 : : }
1923 : 0 : if (total_burst == 0)
1924 : 0 : return;
1925 : :
1926 : : printf(" %s-bursts : %"PRIu64" [", rx_tx, total_burst);
1927 : 0 : for (i = 0, sburst = 0, sburstp = 0; i < 4; i++) {
1928 : 0 : if (i == 3) {
1929 : 0 : printf("%d%% of other]\n", 100 - sburstp);
1930 : 0 : return;
1931 : : }
1932 : :
1933 : 0 : sburst += burst_stats[i];
1934 : 0 : if (sburst == total_burst) {
1935 : 0 : printf("%d%% of %d pkts]\n",
1936 : 0 : 100 - sburstp, (int) pktnb_stats[i]);
1937 : 0 : return;
1938 : : }
1939 : :
1940 : 0 : burst_percent[i] =
1941 : 0 : (double)burst_stats[i] / total_burst * 100;
1942 : 0 : printf("%d%% of %d pkts + ",
1943 : 0 : burst_percent[i], (int) pktnb_stats[i]);
1944 : 0 : sburstp += burst_percent[i];
1945 : : }
1946 : : }
1947 : :
1948 : : static void
1949 : 0 : fwd_stream_stats_display(streamid_t stream_id)
1950 : : {
1951 : : struct fwd_stream *fs;
1952 : : static const char *fwd_top_stats_border = "-------";
1953 : :
1954 : 0 : fs = fwd_streams[stream_id];
1955 : 0 : if ((fs->rx_packets == 0) && (fs->tx_packets == 0) &&
1956 : 0 : (fs->fwd_dropped == 0))
1957 : : return;
1958 : 0 : printf("\n %s Forward Stats for RX Port=%2d/Queue=%2d -> "
1959 : : "TX Port=%2d/Queue=%2d %s\n",
1960 : 0 : fwd_top_stats_border, fs->rx_port, fs->rx_queue,
1961 : 0 : fs->tx_port, fs->tx_queue, fwd_top_stats_border);
1962 : 0 : printf(" RX-packets: %-14"PRIu64" TX-packets: %-14"PRIu64
1963 : : " TX-dropped: %-14"PRIu64,
1964 : : fs->rx_packets, fs->tx_packets, fs->fwd_dropped);
1965 : :
1966 : : /* if checksum mode */
1967 : 0 : if (cur_fwd_eng == &csum_fwd_engine) {
1968 : 0 : printf(" RX- bad IP checksum: %-14"PRIu64
1969 : : " Rx- bad L4 checksum: %-14"PRIu64
1970 : : " Rx- bad outer L4 checksum: %-14"PRIu64"\n",
1971 : : fs->rx_bad_ip_csum, fs->rx_bad_l4_csum,
1972 : : fs->rx_bad_outer_l4_csum);
1973 : 0 : printf(" RX- bad outer IP checksum: %-14"PRIu64"\n",
1974 : : fs->rx_bad_outer_ip_csum);
1975 : : } else {
1976 : : printf("\n");
1977 : : }
1978 : :
1979 : 0 : if (record_burst_stats) {
1980 : 0 : pkt_burst_stats_display("RX", &fs->rx_burst_stats);
1981 : 0 : pkt_burst_stats_display("TX", &fs->tx_burst_stats);
1982 : : }
1983 : : }
1984 : :
1985 : : void
1986 : 0 : fwd_stats_display(void)
1987 : : {
1988 : : static const char *fwd_stats_border = "----------------------";
1989 : : static const char *acc_stats_border = "+++++++++++++++";
1990 : : struct {
1991 : : struct fwd_stream *rx_stream;
1992 : : struct fwd_stream *tx_stream;
1993 : : uint64_t tx_dropped;
1994 : : uint64_t rx_bad_ip_csum;
1995 : : uint64_t rx_bad_l4_csum;
1996 : : uint64_t rx_bad_outer_l4_csum;
1997 : : uint64_t rx_bad_outer_ip_csum;
1998 : : } ports_stats[RTE_MAX_ETHPORTS];
1999 : : uint64_t total_rx_dropped = 0;
2000 : : uint64_t total_tx_dropped = 0;
2001 : : uint64_t total_rx_nombuf = 0;
2002 : : struct rte_eth_stats stats;
2003 : : uint64_t fwd_cycles = 0;
2004 : : uint64_t total_recv = 0;
2005 : : uint64_t total_xmit = 0;
2006 : : struct rte_port *port;
2007 : : streamid_t sm_id;
2008 : : portid_t pt_id;
2009 : : int ret;
2010 : : int i;
2011 : :
2012 : : memset(ports_stats, 0, sizeof(ports_stats));
2013 : :
2014 : 0 : for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
2015 : 0 : struct fwd_stream *fs = fwd_streams[sm_id];
2016 : :
2017 : 0 : if (cur_fwd_config.nb_fwd_streams >
2018 : 0 : cur_fwd_config.nb_fwd_ports) {
2019 : 0 : fwd_stream_stats_display(sm_id);
2020 : : } else {
2021 : 0 : ports_stats[fs->tx_port].tx_stream = fs;
2022 : 0 : ports_stats[fs->rx_port].rx_stream = fs;
2023 : : }
2024 : :
2025 : 0 : ports_stats[fs->tx_port].tx_dropped += fs->fwd_dropped;
2026 : :
2027 : 0 : ports_stats[fs->rx_port].rx_bad_ip_csum += fs->rx_bad_ip_csum;
2028 : 0 : ports_stats[fs->rx_port].rx_bad_l4_csum += fs->rx_bad_l4_csum;
2029 : 0 : ports_stats[fs->rx_port].rx_bad_outer_l4_csum +=
2030 : 0 : fs->rx_bad_outer_l4_csum;
2031 : 0 : ports_stats[fs->rx_port].rx_bad_outer_ip_csum +=
2032 : 0 : fs->rx_bad_outer_ip_csum;
2033 : :
2034 : 0 : if (record_core_cycles)
2035 : 0 : fwd_cycles += fs->busy_cycles;
2036 : : }
2037 : 0 : for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
2038 : : uint64_t tx_dropped = 0;
2039 : :
2040 : 0 : pt_id = fwd_ports_ids[i];
2041 : 0 : port = &ports[pt_id];
2042 : :
2043 : 0 : ret = rte_eth_stats_get(pt_id, &stats);
2044 : 0 : if (ret != 0) {
2045 : 0 : fprintf(stderr,
2046 : : "%s: Error: failed to get stats (port %u): %d",
2047 : : __func__, pt_id, ret);
2048 : 0 : continue;
2049 : : }
2050 : 0 : stats.ipackets -= port->stats.ipackets;
2051 : 0 : stats.opackets -= port->stats.opackets;
2052 : 0 : stats.ibytes -= port->stats.ibytes;
2053 : 0 : stats.obytes -= port->stats.obytes;
2054 : 0 : stats.imissed -= port->stats.imissed;
2055 : 0 : stats.oerrors -= port->stats.oerrors;
2056 : 0 : stats.rx_nombuf -= port->stats.rx_nombuf;
2057 : :
2058 : 0 : total_recv += stats.ipackets;
2059 : 0 : total_xmit += stats.opackets;
2060 : 0 : total_rx_dropped += stats.imissed;
2061 : 0 : tx_dropped += ports_stats[pt_id].tx_dropped;
2062 : 0 : tx_dropped += stats.oerrors;
2063 : 0 : total_tx_dropped += tx_dropped;
2064 : 0 : total_rx_nombuf += stats.rx_nombuf;
2065 : :
2066 : 0 : printf("\n %s Forward statistics for port %-2d %s\n",
2067 : : fwd_stats_border, pt_id, fwd_stats_border);
2068 : :
2069 : 0 : printf(" RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64
2070 : : "RX-total: %-"PRIu64"\n", stats.ipackets, stats.imissed,
2071 : 0 : stats.ipackets + stats.imissed);
2072 : :
2073 : 0 : if (cur_fwd_eng == &csum_fwd_engine) {
2074 : 0 : printf(" Bad-ipcsum: %-14"PRIu64
2075 : : " Bad-l4csum: %-14"PRIu64
2076 : : "Bad-outer-l4csum: %-14"PRIu64"\n",
2077 : : ports_stats[pt_id].rx_bad_ip_csum,
2078 : : ports_stats[pt_id].rx_bad_l4_csum,
2079 : : ports_stats[pt_id].rx_bad_outer_l4_csum);
2080 : 0 : printf(" Bad-outer-ipcsum: %-14"PRIu64"\n",
2081 : : ports_stats[pt_id].rx_bad_outer_ip_csum);
2082 : : }
2083 : 0 : if (stats.ierrors + stats.rx_nombuf > 0) {
2084 : : printf(" RX-error: %-"PRIu64"\n", stats.ierrors);
2085 : 0 : printf(" RX-nombufs: %-14"PRIu64"\n", stats.rx_nombuf);
2086 : : }
2087 : :
2088 : 0 : printf(" TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64
2089 : : "TX-total: %-"PRIu64"\n",
2090 : : stats.opackets, tx_dropped,
2091 : 0 : stats.opackets + tx_dropped);
2092 : :
2093 : 0 : if (record_burst_stats) {
2094 : 0 : if (ports_stats[pt_id].rx_stream)
2095 : 0 : pkt_burst_stats_display("RX",
2096 : : &ports_stats[pt_id].rx_stream->rx_burst_stats);
2097 : 0 : if (ports_stats[pt_id].tx_stream)
2098 : 0 : pkt_burst_stats_display("TX",
2099 : : &ports_stats[pt_id].tx_stream->tx_burst_stats);
2100 : : }
2101 : :
2102 : 0 : printf(" %s--------------------------------%s\n",
2103 : : fwd_stats_border, fwd_stats_border);
2104 : : }
2105 : :
2106 : 0 : printf("\n %s Accumulated forward statistics for all ports"
2107 : : "%s\n",
2108 : : acc_stats_border, acc_stats_border);
2109 : 0 : printf(" RX-packets: %-14"PRIu64" RX-dropped: %-14"PRIu64"RX-total: "
2110 : : "%-"PRIu64"\n"
2111 : : " TX-packets: %-14"PRIu64" TX-dropped: %-14"PRIu64"TX-total: "
2112 : : "%-"PRIu64"\n",
2113 : : total_recv, total_rx_dropped, total_recv + total_rx_dropped,
2114 : : total_xmit, total_tx_dropped, total_xmit + total_tx_dropped);
2115 : 0 : if (total_rx_nombuf > 0)
2116 : : printf(" RX-nombufs: %-14"PRIu64"\n", total_rx_nombuf);
2117 : 0 : printf(" %s++++++++++++++++++++++++++++++++++++++++++++++"
2118 : : "%s\n",
2119 : : acc_stats_border, acc_stats_border);
2120 : 0 : if (record_core_cycles) {
2121 : : #define CYC_PER_MHZ 1E6
2122 : 0 : if (total_recv > 0 || total_xmit > 0) {
2123 : : uint64_t total_pkts = 0;
2124 : 0 : if (strcmp(cur_fwd_eng->fwd_mode_name, "txonly") == 0 ||
2125 : 0 : strcmp(cur_fwd_eng->fwd_mode_name, "flowgen") == 0)
2126 : : total_pkts = total_xmit;
2127 : : else
2128 : : total_pkts = total_recv;
2129 : :
2130 : 0 : printf("\n CPU cycles/packet=%.2F (busy cycles="
2131 : : "%"PRIu64" / total %s packets=%"PRIu64") at %"PRIu64
2132 : : " MHz Clock\n",
2133 : 0 : (double) fwd_cycles / total_pkts,
2134 : 0 : fwd_cycles, cur_fwd_eng->fwd_mode_name, total_pkts,
2135 : 0 : (uint64_t)(rte_get_tsc_hz() / CYC_PER_MHZ));
2136 : : }
2137 : : }
2138 : 0 : }
2139 : :
2140 : : void
2141 : 0 : fwd_stats_reset(void)
2142 : : {
2143 : : streamid_t sm_id;
2144 : : portid_t pt_id;
2145 : : int ret;
2146 : : int i;
2147 : :
2148 : 0 : for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
2149 : 0 : pt_id = fwd_ports_ids[i];
2150 : 0 : ret = rte_eth_stats_get(pt_id, &ports[pt_id].stats);
2151 : 0 : if (ret != 0)
2152 : 0 : fprintf(stderr,
2153 : : "%s: Error: failed to clear stats (port %u):%d",
2154 : : __func__, pt_id, ret);
2155 : : }
2156 : 0 : for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
2157 : 0 : struct fwd_stream *fs = fwd_streams[sm_id];
2158 : :
2159 : 0 : fs->rx_packets = 0;
2160 : 0 : fs->tx_packets = 0;
2161 : 0 : fs->fwd_dropped = 0;
2162 : 0 : fs->rx_bad_ip_csum = 0;
2163 : 0 : fs->rx_bad_l4_csum = 0;
2164 : 0 : fs->rx_bad_outer_l4_csum = 0;
2165 : 0 : fs->rx_bad_outer_ip_csum = 0;
2166 : :
2167 : 0 : memset(&fs->rx_burst_stats, 0, sizeof(fs->rx_burst_stats));
2168 : 0 : memset(&fs->tx_burst_stats, 0, sizeof(fs->tx_burst_stats));
2169 : 0 : fs->busy_cycles = 0;
2170 : : }
2171 : 0 : }
2172 : :
2173 : : static void
2174 : 0 : flush_fwd_rx_queues(void)
2175 : : {
2176 : : struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2177 : : portid_t rxp;
2178 : : portid_t port_id;
2179 : : queueid_t rxq;
2180 : : uint16_t nb_rx;
2181 : : uint8_t j;
2182 : : uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
2183 : : uint64_t timer_period;
2184 : :
2185 : 0 : if (num_procs > 1) {
2186 : : printf("multi-process not support for flushing fwd Rx queues, skip the below lines and return.\n");
2187 : 0 : return;
2188 : : }
2189 : :
2190 : : /* convert to number of cycles */
2191 : : timer_period = rte_get_timer_hz(); /* 1 second timeout */
2192 : :
2193 : 0 : for (j = 0; j < 2; j++) {
2194 : 0 : for (rxp = 0; rxp < cur_fwd_config.nb_fwd_ports; rxp++) {
2195 : 0 : for (rxq = 0; rxq < nb_rxq; rxq++) {
2196 : 0 : port_id = fwd_ports_ids[rxp];
2197 : :
2198 : : /* Polling stopped queues is prohibited. */
2199 : 0 : if (ports[port_id].rxq[rxq].state ==
2200 : : RTE_ETH_QUEUE_STATE_STOPPED)
2201 : 0 : continue;
2202 : :
2203 : : /**
2204 : : * testpmd can stuck in the below do while loop
2205 : : * if rte_eth_rx_burst() always returns nonzero
2206 : : * packets. So timer is added to exit this loop
2207 : : * after 1sec timer expiry.
2208 : : */
2209 : : prev_tsc = rte_rdtsc();
2210 : : do {
2211 : 0 : nb_rx = rte_eth_rx_burst(port_id, rxq,
2212 : : pkts_burst, MAX_PKT_BURST);
2213 : 0 : rte_pktmbuf_free_bulk(pkts_burst, nb_rx);
2214 : :
2215 : : cur_tsc = rte_rdtsc();
2216 : 0 : diff_tsc = cur_tsc - prev_tsc;
2217 : 0 : timer_tsc += diff_tsc;
2218 : 0 : } while ((nb_rx > 0) &&
2219 : 0 : (timer_tsc < timer_period));
2220 : : timer_tsc = 0;
2221 : : }
2222 : : }
2223 : : rte_delay_ms(10); /* wait 10 milli-seconds before retrying */
2224 : : }
2225 : : }
2226 : :
2227 : : static void
2228 : 0 : run_pkt_fwd_on_lcore(struct fwd_lcore *fc, packet_fwd_t pkt_fwd)
2229 : : {
2230 : : struct fwd_stream **fsm;
2231 : : uint64_t prev_tsc;
2232 : : streamid_t nb_fs;
2233 : : streamid_t sm_id;
2234 : : #ifdef RTE_LIB_BITRATESTATS
2235 : : uint64_t tics_per_1sec;
2236 : : uint64_t tics_datum;
2237 : : uint64_t tics_current;
2238 : : uint16_t i, cnt_ports;
2239 : :
2240 : 0 : cnt_ports = nb_ports;
2241 : : tics_datum = rte_rdtsc();
2242 : : tics_per_1sec = rte_get_timer_hz();
2243 : : #endif
2244 : 0 : fsm = &fwd_streams[fc->stream_idx];
2245 : 0 : nb_fs = fc->stream_nb;
2246 : : prev_tsc = rte_rdtsc();
2247 : : do {
2248 : 0 : for (sm_id = 0; sm_id < nb_fs; sm_id++) {
2249 : 0 : struct fwd_stream *fs = fsm[sm_id];
2250 : : uint64_t start_fs_tsc = 0;
2251 : : bool busy;
2252 : :
2253 : 0 : if (fs->disabled)
2254 : 0 : continue;
2255 : 0 : if (record_core_cycles)
2256 : : start_fs_tsc = rte_rdtsc();
2257 : 0 : busy = (*pkt_fwd)(fs);
2258 : 0 : if (record_core_cycles && busy)
2259 : 0 : fs->busy_cycles += rte_rdtsc() - start_fs_tsc;
2260 : : }
2261 : : #ifdef RTE_LIB_BITRATESTATS
2262 : 0 : if (bitrate_enabled != 0 &&
2263 : 0 : bitrate_lcore_id == rte_lcore_id()) {
2264 : : tics_current = rte_rdtsc();
2265 : 0 : if (tics_current - tics_datum >= tics_per_1sec) {
2266 : : /* Periodic bitrate calculation */
2267 : 0 : for (i = 0; i < cnt_ports; i++)
2268 : 0 : rte_stats_bitrate_calc(bitrate_data,
2269 : 0 : ports_ids[i]);
2270 : : tics_datum = tics_current;
2271 : : }
2272 : : }
2273 : : #endif
2274 : : #ifdef RTE_LIB_LATENCYSTATS
2275 : 0 : if (latencystats_enabled != 0 &&
2276 : 0 : latencystats_lcore_id == rte_lcore_id())
2277 : 0 : rte_latencystats_update();
2278 : : #endif
2279 : 0 : if (record_core_cycles) {
2280 : : uint64_t tsc = rte_rdtsc();
2281 : :
2282 : 0 : fc->total_cycles += tsc - prev_tsc;
2283 : : prev_tsc = tsc;
2284 : : }
2285 : 0 : } while (! fc->stopped);
2286 : 0 : }
2287 : :
2288 : : static int
2289 : 0 : lcore_usage_callback(unsigned int lcore_id, struct rte_lcore_usage *usage)
2290 : : {
2291 : : struct fwd_stream **fsm;
2292 : : struct fwd_lcore *fc;
2293 : : streamid_t nb_fs;
2294 : : streamid_t sm_id;
2295 : :
2296 : : fc = lcore_to_fwd_lcore(lcore_id);
2297 : 0 : if (fc == NULL)
2298 : : return -1;
2299 : :
2300 : 0 : fsm = &fwd_streams[fc->stream_idx];
2301 : 0 : nb_fs = fc->stream_nb;
2302 : 0 : usage->busy_cycles = 0;
2303 : 0 : usage->total_cycles = fc->total_cycles;
2304 : :
2305 : 0 : for (sm_id = 0; sm_id < nb_fs; sm_id++) {
2306 : 0 : if (!fsm[sm_id]->disabled)
2307 : 0 : usage->busy_cycles += fsm[sm_id]->busy_cycles;
2308 : : }
2309 : :
2310 : : return 0;
2311 : : }
2312 : :
2313 : : static int
2314 : 0 : start_pkt_forward_on_core(void *fwd_arg)
2315 : : {
2316 : 0 : run_pkt_fwd_on_lcore((struct fwd_lcore *) fwd_arg,
2317 : 0 : cur_fwd_config.fwd_eng->packet_fwd);
2318 : 0 : return 0;
2319 : : }
2320 : :
2321 : : /*
2322 : : * Run the TXONLY packet forwarding engine to send a single burst of packets.
2323 : : * Used to start communication flows in network loopback test configurations.
2324 : : */
2325 : : static int
2326 : 0 : run_one_txonly_burst_on_core(void *fwd_arg)
2327 : : {
2328 : : struct fwd_lcore *fwd_lc;
2329 : : struct fwd_lcore tmp_lcore;
2330 : :
2331 : : fwd_lc = (struct fwd_lcore *) fwd_arg;
2332 : 0 : tmp_lcore = *fwd_lc;
2333 : 0 : tmp_lcore.stopped = 1;
2334 : 0 : run_pkt_fwd_on_lcore(&tmp_lcore, tx_only_engine.packet_fwd);
2335 : 0 : return 0;
2336 : : }
2337 : :
2338 : : /*
2339 : : * Launch packet forwarding:
2340 : : * - Setup per-port forwarding context.
2341 : : * - launch logical cores with their forwarding configuration.
2342 : : */
2343 : : static void
2344 : 0 : launch_packet_forwarding(lcore_function_t *pkt_fwd_on_lcore)
2345 : : {
2346 : : unsigned int i;
2347 : : unsigned int lc_id;
2348 : : int diag;
2349 : :
2350 : 0 : for (i = 0; i < cur_fwd_config.nb_fwd_lcores; i++) {
2351 : 0 : lc_id = fwd_lcores_cpuids[i];
2352 : 0 : if ((interactive == 0) || (lc_id != rte_lcore_id())) {
2353 : 0 : fwd_lcores[i]->stopped = 0;
2354 : 0 : diag = rte_eal_remote_launch(pkt_fwd_on_lcore,
2355 : : fwd_lcores[i], lc_id);
2356 : 0 : if (diag != 0)
2357 : 0 : fprintf(stderr,
2358 : : "launch lcore %u failed - diag=%d\n",
2359 : : lc_id, diag);
2360 : : }
2361 : : }
2362 : 0 : }
2363 : :
2364 : : void
2365 : 0 : common_fwd_stream_init(struct fwd_stream *fs)
2366 : : {
2367 : : bool rx_stopped, tx_stopped;
2368 : :
2369 : 0 : rx_stopped = (ports[fs->rx_port].rxq[fs->rx_queue].state == RTE_ETH_QUEUE_STATE_STOPPED);
2370 : 0 : tx_stopped = (ports[fs->tx_port].txq[fs->tx_queue].state == RTE_ETH_QUEUE_STATE_STOPPED);
2371 : 0 : fs->disabled = rx_stopped || tx_stopped;
2372 : 0 : }
2373 : :
2374 : : static void
2375 : 0 : update_rx_queue_state(uint16_t port_id, uint16_t queue_id)
2376 : : {
2377 : : struct rte_eth_rxq_info rx_qinfo;
2378 : : int32_t rc;
2379 : :
2380 : 0 : rc = rte_eth_rx_queue_info_get(port_id,
2381 : : queue_id, &rx_qinfo);
2382 : 0 : if (rc == 0) {
2383 : 0 : ports[port_id].rxq[queue_id].state =
2384 : 0 : rx_qinfo.queue_state;
2385 : 0 : } else if (rc == -ENOTSUP) {
2386 : : /*
2387 : : * Do not change the rxq state for primary process
2388 : : * to ensure that the PMDs do not implement
2389 : : * rte_eth_rx_queue_info_get can forward as before.
2390 : : */
2391 : 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY)
2392 : 0 : return;
2393 : : /*
2394 : : * Set the rxq state to RTE_ETH_QUEUE_STATE_STARTED
2395 : : * to ensure that the PMDs do not implement
2396 : : * rte_eth_rx_queue_info_get can forward.
2397 : : */
2398 : 0 : ports[port_id].rxq[queue_id].state =
2399 : : RTE_ETH_QUEUE_STATE_STARTED;
2400 : : } else {
2401 : 0 : TESTPMD_LOG(WARNING,
2402 : : "Failed to get rx queue info\n");
2403 : : }
2404 : : }
2405 : :
2406 : : static void
2407 : 0 : update_tx_queue_state(uint16_t port_id, uint16_t queue_id)
2408 : : {
2409 : : struct rte_eth_txq_info tx_qinfo;
2410 : : int32_t rc;
2411 : :
2412 : 0 : rc = rte_eth_tx_queue_info_get(port_id,
2413 : : queue_id, &tx_qinfo);
2414 : 0 : if (rc == 0) {
2415 : 0 : ports[port_id].txq[queue_id].state =
2416 : 0 : tx_qinfo.queue_state;
2417 : 0 : } else if (rc == -ENOTSUP) {
2418 : : /*
2419 : : * Do not change the txq state for primary process
2420 : : * to ensure that the PMDs do not implement
2421 : : * rte_eth_tx_queue_info_get can forward as before.
2422 : : */
2423 : 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY)
2424 : 0 : return;
2425 : : /*
2426 : : * Set the txq state to RTE_ETH_QUEUE_STATE_STARTED
2427 : : * to ensure that the PMDs do not implement
2428 : : * rte_eth_tx_queue_info_get can forward.
2429 : : */
2430 : 0 : ports[port_id].txq[queue_id].state =
2431 : : RTE_ETH_QUEUE_STATE_STARTED;
2432 : : } else {
2433 : 0 : TESTPMD_LOG(WARNING,
2434 : : "Failed to get tx queue info\n");
2435 : : }
2436 : : }
2437 : :
2438 : : static void
2439 : 0 : update_queue_state(portid_t pid)
2440 : : {
2441 : : portid_t pi;
2442 : : queueid_t qi;
2443 : :
2444 : 0 : RTE_ETH_FOREACH_DEV(pi) {
2445 : 0 : if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
2446 : 0 : continue;
2447 : :
2448 : 0 : for (qi = 0; qi < nb_rxq; qi++)
2449 : 0 : update_rx_queue_state(pi, qi);
2450 : 0 : for (qi = 0; qi < nb_txq; qi++)
2451 : 0 : update_tx_queue_state(pi, qi);
2452 : : }
2453 : 0 : }
2454 : :
2455 : : /*
2456 : : * Launch packet forwarding configuration.
2457 : : */
2458 : : void
2459 : 0 : start_packet_forwarding(int with_tx_first)
2460 : : {
2461 : : port_fwd_begin_t port_fwd_begin;
2462 : : port_fwd_end_t port_fwd_end;
2463 : 0 : stream_init_t stream_init = cur_fwd_eng->stream_init;
2464 : : unsigned int i;
2465 : :
2466 : 0 : if (strcmp(cur_fwd_eng->fwd_mode_name, "rxonly") == 0 && !nb_rxq)
2467 : 0 : rte_exit(EXIT_FAILURE, "rxq are 0, cannot use rxonly fwd mode\n");
2468 : :
2469 : 0 : if (strcmp(cur_fwd_eng->fwd_mode_name, "txonly") == 0 && !nb_txq)
2470 : 0 : rte_exit(EXIT_FAILURE, "txq are 0, cannot use txonly fwd mode\n");
2471 : :
2472 : 0 : if ((strcmp(cur_fwd_eng->fwd_mode_name, "rxonly") != 0 &&
2473 : 0 : strcmp(cur_fwd_eng->fwd_mode_name, "txonly") != 0) &&
2474 : 0 : (!nb_rxq || !nb_txq))
2475 : 0 : rte_exit(EXIT_FAILURE,
2476 : : "Either rxq or txq are 0, cannot use %s fwd mode\n",
2477 : : cur_fwd_eng->fwd_mode_name);
2478 : :
2479 : 0 : if (all_ports_started() == 0) {
2480 : 0 : fprintf(stderr, "Not all ports were started\n");
2481 : 0 : return;
2482 : : }
2483 : 0 : if (test_done == 0) {
2484 : 0 : fprintf(stderr, "Packet forwarding already started\n");
2485 : 0 : return;
2486 : : }
2487 : :
2488 : 0 : fwd_config_setup();
2489 : :
2490 : 0 : pkt_fwd_config_display(&cur_fwd_config);
2491 : 0 : if (!pkt_fwd_shared_rxq_check())
2492 : : return;
2493 : :
2494 : 0 : if (stream_init != NULL) {
2495 : 0 : update_queue_state(RTE_PORT_ALL);
2496 : 0 : for (i = 0; i < cur_fwd_config.nb_fwd_streams; i++)
2497 : 0 : stream_init(fwd_streams[i]);
2498 : : }
2499 : :
2500 : 0 : port_fwd_begin = cur_fwd_config.fwd_eng->port_fwd_begin;
2501 : 0 : if (port_fwd_begin != NULL) {
2502 : 0 : for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
2503 : 0 : if (port_fwd_begin(fwd_ports_ids[i])) {
2504 : 0 : fprintf(stderr,
2505 : : "Packet forwarding is not ready\n");
2506 : 0 : return;
2507 : : }
2508 : : }
2509 : : }
2510 : :
2511 : 0 : if (with_tx_first) {
2512 : 0 : port_fwd_begin = tx_only_engine.port_fwd_begin;
2513 : 0 : if (port_fwd_begin != NULL) {
2514 : 0 : for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
2515 : 0 : if (port_fwd_begin(fwd_ports_ids[i])) {
2516 : 0 : fprintf(stderr,
2517 : : "Packet forwarding is not ready\n");
2518 : 0 : return;
2519 : : }
2520 : : }
2521 : : }
2522 : : }
2523 : :
2524 : 0 : test_done = 0;
2525 : :
2526 : 0 : if(!no_flush_rx)
2527 : 0 : flush_fwd_rx_queues();
2528 : :
2529 : 0 : rxtx_config_display();
2530 : :
2531 : 0 : fwd_stats_reset();
2532 : 0 : if (with_tx_first) {
2533 : 0 : while (with_tx_first--) {
2534 : 0 : launch_packet_forwarding(
2535 : : run_one_txonly_burst_on_core);
2536 : 0 : rte_eal_mp_wait_lcore();
2537 : : }
2538 : 0 : port_fwd_end = tx_only_engine.port_fwd_end;
2539 : 0 : if (port_fwd_end != NULL) {
2540 : 0 : for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
2541 : 0 : (*port_fwd_end)(fwd_ports_ids[i]);
2542 : : }
2543 : : }
2544 : 0 : launch_packet_forwarding(start_pkt_forward_on_core);
2545 : : }
2546 : :
2547 : : void
2548 : 0 : stop_packet_forwarding(void)
2549 : : {
2550 : : port_fwd_end_t port_fwd_end;
2551 : : lcoreid_t lc_id;
2552 : : portid_t pt_id;
2553 : : int i;
2554 : :
2555 : 0 : if (test_done) {
2556 : 0 : fprintf(stderr, "Packet forwarding not started\n");
2557 : 0 : return;
2558 : : }
2559 : : printf("Telling cores to stop...");
2560 : 0 : for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++)
2561 : 0 : fwd_lcores[lc_id]->stopped = 1;
2562 : : printf("\nWaiting for lcores to finish...\n");
2563 : 0 : rte_eal_mp_wait_lcore();
2564 : 0 : port_fwd_end = cur_fwd_config.fwd_eng->port_fwd_end;
2565 : 0 : if (port_fwd_end != NULL) {
2566 : 0 : for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
2567 : 0 : pt_id = fwd_ports_ids[i];
2568 : 0 : (*port_fwd_end)(pt_id);
2569 : : }
2570 : : }
2571 : :
2572 : 0 : fwd_stats_display();
2573 : :
2574 : : printf("\nDone.\n");
2575 : 0 : test_done = 1;
2576 : : }
2577 : :
2578 : : void
2579 : 0 : dev_set_link_up(portid_t pid)
2580 : : {
2581 : 0 : if (rte_eth_dev_set_link_up(pid) < 0)
2582 : 0 : fprintf(stderr, "\nSet link up fail.\n");
2583 : 0 : }
2584 : :
2585 : : void
2586 : 0 : dev_set_link_down(portid_t pid)
2587 : : {
2588 : 0 : if (rte_eth_dev_set_link_down(pid) < 0)
2589 : 0 : fprintf(stderr, "\nSet link down fail.\n");
2590 : 0 : }
2591 : :
2592 : : static int
2593 : 0 : all_ports_started(void)
2594 : : {
2595 : : portid_t pi;
2596 : : struct rte_port *port;
2597 : :
2598 : 0 : RTE_ETH_FOREACH_DEV(pi) {
2599 : 0 : port = &ports[pi];
2600 : : /* Check if there is a port which is not started */
2601 : 0 : if ((port->port_status != RTE_PORT_STARTED) &&
2602 : 0 : (port->member_flag == 0))
2603 : : return 0;
2604 : : }
2605 : :
2606 : : /* No port is not started */
2607 : : return 1;
2608 : : }
2609 : :
2610 : : int
2611 : 0 : port_is_stopped(portid_t port_id)
2612 : : {
2613 : 0 : struct rte_port *port = &ports[port_id];
2614 : :
2615 : 0 : if ((port->port_status != RTE_PORT_STOPPED) &&
2616 : 0 : (port->member_flag == 0))
2617 : 0 : return 0;
2618 : : return 1;
2619 : : }
2620 : :
2621 : : int
2622 : 0 : all_ports_stopped(void)
2623 : : {
2624 : : portid_t pi;
2625 : :
2626 : 0 : RTE_ETH_FOREACH_DEV(pi) {
2627 : : if (!port_is_stopped(pi))
2628 : : return 0;
2629 : : }
2630 : :
2631 : : return 1;
2632 : : }
2633 : :
2634 : : int
2635 : 0 : port_is_started(portid_t port_id)
2636 : : {
2637 : 0 : if (port_id_is_invalid(port_id, ENABLED_WARN))
2638 : : return 0;
2639 : :
2640 : 0 : if (ports[port_id].port_status != RTE_PORT_STARTED)
2641 : 0 : return 0;
2642 : :
2643 : : return 1;
2644 : : }
2645 : :
2646 : : /* Configure the Rx with optional split. */
2647 : : int
2648 : 0 : rx_queue_setup(uint16_t port_id, uint16_t rx_queue_id,
2649 : : uint16_t nb_rx_desc, unsigned int socket_id,
2650 : : struct rte_eth_rxconf *rx_conf, struct rte_mempool *mp)
2651 : : {
2652 : 0 : union rte_eth_rxseg rx_useg[MAX_SEGS_BUFFER_SPLIT] = {};
2653 : 0 : struct rte_mempool *rx_mempool[MAX_MEMPOOL] = {};
2654 : : struct rte_mempool *mpx;
2655 : : unsigned int i, mp_n;
2656 : : uint32_t prev_hdrs = 0;
2657 : : int ret;
2658 : :
2659 : :
2660 : 0 : if ((rx_pkt_nb_segs > 1) &&
2661 : 0 : (rx_conf->offloads & RTE_ETH_RX_OFFLOAD_BUFFER_SPLIT)) {
2662 : : /* multi-segment configuration */
2663 : 0 : for (i = 0; i < rx_pkt_nb_segs; i++) {
2664 : : struct rte_eth_rxseg_split *rx_seg = &rx_useg[i].split;
2665 : : /*
2666 : : * Use last valid pool for the segments with number
2667 : : * exceeding the pool index.
2668 : : */
2669 : 0 : mp_n = (i >= mbuf_data_size_n) ? mbuf_data_size_n - 1 : i;
2670 : 0 : mpx = mbuf_pool_find(socket_id, mp_n);
2671 : : /* Handle zero as mbuf data buffer size. */
2672 : 0 : rx_seg->offset = i < rx_pkt_nb_offs ?
2673 : : rx_pkt_seg_offsets[i] : 0;
2674 : 0 : rx_seg->mp = mpx ? mpx : mp;
2675 : 0 : if (rx_pkt_hdr_protos[i] != 0 && rx_pkt_seg_lengths[i] == 0) {
2676 : 0 : rx_seg->proto_hdr = rx_pkt_hdr_protos[i] & ~prev_hdrs;
2677 : 0 : prev_hdrs |= rx_seg->proto_hdr;
2678 : : } else {
2679 : 0 : rx_seg->length = rx_pkt_seg_lengths[i] ?
2680 : : rx_pkt_seg_lengths[i] :
2681 : : mbuf_data_size[mp_n];
2682 : : }
2683 : : }
2684 : 0 : rx_conf->rx_nseg = rx_pkt_nb_segs;
2685 : 0 : rx_conf->rx_seg = rx_useg;
2686 : 0 : rx_conf->rx_mempools = NULL;
2687 : 0 : rx_conf->rx_nmempool = 0;
2688 : 0 : ret = rte_eth_rx_queue_setup(port_id, rx_queue_id, nb_rx_desc,
2689 : : socket_id, rx_conf, NULL);
2690 : 0 : rx_conf->rx_seg = NULL;
2691 : 0 : rx_conf->rx_nseg = 0;
2692 : 0 : } else if (multi_rx_mempool == 1) {
2693 : : /* multi-pool configuration */
2694 : : struct rte_eth_dev_info dev_info;
2695 : :
2696 : 0 : if (mbuf_data_size_n <= 1) {
2697 : 0 : fprintf(stderr, "Invalid number of mempools %u\n",
2698 : : mbuf_data_size_n);
2699 : 0 : return -EINVAL;
2700 : : }
2701 : 0 : ret = rte_eth_dev_info_get(port_id, &dev_info);
2702 : 0 : if (ret != 0)
2703 : : return ret;
2704 : 0 : if (dev_info.max_rx_mempools == 0) {
2705 : 0 : fprintf(stderr,
2706 : : "Port %u doesn't support requested multi-rx-mempool configuration.\n",
2707 : : port_id);
2708 : 0 : return -ENOTSUP;
2709 : : }
2710 : 0 : for (i = 0; i < mbuf_data_size_n; i++) {
2711 : 0 : mpx = mbuf_pool_find(socket_id, i);
2712 : 0 : rx_mempool[i] = mpx ? mpx : mp;
2713 : : }
2714 : 0 : rx_conf->rx_mempools = rx_mempool;
2715 : 0 : rx_conf->rx_nmempool = mbuf_data_size_n;
2716 : 0 : rx_conf->rx_seg = NULL;
2717 : 0 : rx_conf->rx_nseg = 0;
2718 : 0 : ret = rte_eth_rx_queue_setup(port_id, rx_queue_id, nb_rx_desc,
2719 : : socket_id, rx_conf, NULL);
2720 : 0 : rx_conf->rx_mempools = NULL;
2721 : 0 : rx_conf->rx_nmempool = 0;
2722 : : } else {
2723 : : /* Single pool/segment configuration */
2724 : 0 : rx_conf->rx_seg = NULL;
2725 : 0 : rx_conf->rx_nseg = 0;
2726 : 0 : rx_conf->rx_mempools = NULL;
2727 : 0 : rx_conf->rx_nmempool = 0;
2728 : 0 : ret = rte_eth_rx_queue_setup(port_id, rx_queue_id, nb_rx_desc,
2729 : : socket_id, rx_conf, mp);
2730 : : }
2731 : :
2732 : 0 : ports[port_id].rxq[rx_queue_id].state = rx_conf->rx_deferred_start ?
2733 : 0 : RTE_ETH_QUEUE_STATE_STOPPED :
2734 : : RTE_ETH_QUEUE_STATE_STARTED;
2735 : 0 : return ret;
2736 : : }
2737 : :
2738 : : static int
2739 : 0 : alloc_xstats_display_info(portid_t pi)
2740 : : {
2741 : 0 : uint64_t **ids_supp = &ports[pi].xstats_info.ids_supp;
2742 : : uint64_t **prev_values = &ports[pi].xstats_info.prev_values;
2743 : : uint64_t **curr_values = &ports[pi].xstats_info.curr_values;
2744 : :
2745 : 0 : if (xstats_display_num == 0)
2746 : : return 0;
2747 : :
2748 : 0 : *ids_supp = calloc(xstats_display_num, sizeof(**ids_supp));
2749 : 0 : if (*ids_supp == NULL)
2750 : 0 : goto fail_ids_supp;
2751 : :
2752 : 0 : *prev_values = calloc(xstats_display_num,
2753 : : sizeof(**prev_values));
2754 : 0 : if (*prev_values == NULL)
2755 : 0 : goto fail_prev_values;
2756 : :
2757 : 0 : *curr_values = calloc(xstats_display_num,
2758 : : sizeof(**curr_values));
2759 : 0 : if (*curr_values == NULL)
2760 : 0 : goto fail_curr_values;
2761 : :
2762 : 0 : ports[pi].xstats_info.allocated = true;
2763 : :
2764 : 0 : return 0;
2765 : :
2766 : : fail_curr_values:
2767 : 0 : free(*prev_values);
2768 : 0 : fail_prev_values:
2769 : 0 : free(*ids_supp);
2770 : : fail_ids_supp:
2771 : : return -ENOMEM;
2772 : : }
2773 : :
2774 : : static void
2775 : 0 : free_xstats_display_info(portid_t pi)
2776 : : {
2777 : 0 : if (!ports[pi].xstats_info.allocated)
2778 : : return;
2779 : 0 : free(ports[pi].xstats_info.ids_supp);
2780 : 0 : free(ports[pi].xstats_info.prev_values);
2781 : 0 : free(ports[pi].xstats_info.curr_values);
2782 : 0 : ports[pi].xstats_info.allocated = false;
2783 : : }
2784 : :
2785 : : /** Fill helper structures for specified port to show extended statistics. */
2786 : : static void
2787 : 0 : fill_xstats_display_info_for_port(portid_t pi)
2788 : : {
2789 : : unsigned int stat, stat_supp;
2790 : : const char *xstat_name;
2791 : : struct rte_port *port;
2792 : : uint64_t *ids_supp;
2793 : : int rc;
2794 : :
2795 : 0 : if (xstats_display_num == 0)
2796 : : return;
2797 : :
2798 : 0 : if (pi == (portid_t)RTE_PORT_ALL) {
2799 : 0 : fill_xstats_display_info();
2800 : 0 : return;
2801 : : }
2802 : :
2803 : 0 : port = &ports[pi];
2804 : 0 : if (port->port_status != RTE_PORT_STARTED)
2805 : : return;
2806 : :
2807 : 0 : if (!port->xstats_info.allocated && alloc_xstats_display_info(pi) != 0)
2808 : 0 : rte_exit(EXIT_FAILURE,
2809 : : "Failed to allocate xstats display memory\n");
2810 : :
2811 : 0 : ids_supp = port->xstats_info.ids_supp;
2812 : 0 : for (stat = stat_supp = 0; stat < xstats_display_num; stat++) {
2813 : 0 : xstat_name = xstats_display[stat].name;
2814 : 0 : rc = rte_eth_xstats_get_id_by_name(pi, xstat_name,
2815 : 0 : ids_supp + stat_supp);
2816 : 0 : if (rc != 0) {
2817 : 0 : fprintf(stderr, "No xstat '%s' on port %u - skip it %u\n",
2818 : : xstat_name, pi, stat);
2819 : 0 : continue;
2820 : : }
2821 : 0 : stat_supp++;
2822 : : }
2823 : :
2824 : 0 : port->xstats_info.ids_supp_sz = stat_supp;
2825 : : }
2826 : :
2827 : : /** Fill helper structures for all ports to show extended statistics. */
2828 : : static void
2829 : 0 : fill_xstats_display_info(void)
2830 : : {
2831 : : portid_t pi;
2832 : :
2833 : 0 : if (xstats_display_num == 0)
2834 : : return;
2835 : :
2836 : 0 : RTE_ETH_FOREACH_DEV(pi)
2837 : 0 : fill_xstats_display_info_for_port(pi);
2838 : : }
2839 : :
2840 : : /*
2841 : : * Some capabilities (like, rx_offload_capa and tx_offload_capa) of bonding
2842 : : * device in dev_info is zero when no member is added. And its capability
2843 : : * will be updated when add a new member device. So adding a member device need
2844 : : * to update the port configurations of bonding device.
2845 : : */
2846 : : static void
2847 : 0 : update_bonding_port_dev_conf(portid_t bond_pid)
2848 : : {
2849 : : #ifdef RTE_NET_BOND
2850 : 0 : struct rte_port *port = &ports[bond_pid];
2851 : : uint16_t i;
2852 : : int ret;
2853 : :
2854 : 0 : ret = eth_dev_info_get_print_err(bond_pid, &port->dev_info);
2855 : 0 : if (ret != 0) {
2856 : 0 : fprintf(stderr, "Failed to get dev info for port = %u\n",
2857 : : bond_pid);
2858 : 0 : return;
2859 : : }
2860 : :
2861 : 0 : if (port->dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
2862 : 0 : port->dev_conf.txmode.offloads |=
2863 : : RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
2864 : : /* Apply Tx offloads configuration */
2865 : 0 : for (i = 0; i < port->dev_info.max_tx_queues; i++)
2866 : 0 : port->txq[i].conf.offloads = port->dev_conf.txmode.offloads;
2867 : :
2868 : 0 : port->dev_conf.rx_adv_conf.rss_conf.rss_hf &=
2869 : 0 : port->dev_info.flow_type_rss_offloads;
2870 : : #else
2871 : : RTE_SET_USED(bond_pid);
2872 : : #endif
2873 : : }
2874 : :
2875 : : int
2876 : 0 : start_port(portid_t pid)
2877 : : {
2878 : : int diag;
2879 : : portid_t pi;
2880 : : portid_t p_pi = RTE_MAX_ETHPORTS;
2881 : : portid_t pl[RTE_MAX_ETHPORTS];
2882 : : portid_t peer_pl[RTE_MAX_ETHPORTS];
2883 : : uint16_t cnt_pi = 0;
2884 : : uint16_t cfg_pi = 0;
2885 : : queueid_t qi;
2886 : : struct rte_port *port;
2887 : : struct rte_eth_hairpin_cap cap;
2888 : : bool at_least_one_port_exist = false;
2889 : : bool all_ports_already_started = true;
2890 : : bool at_least_one_port_successfully_started = false;
2891 : :
2892 : 0 : if (port_id_is_invalid(pid, ENABLED_WARN))
2893 : : return 0;
2894 : :
2895 : 0 : RTE_ETH_FOREACH_DEV(pi) {
2896 : 0 : if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
2897 : 0 : continue;
2898 : :
2899 : 0 : if (port_is_bonding_member(pi)) {
2900 : 0 : fprintf(stderr,
2901 : : "Please remove port %d from bonding device.\n",
2902 : : pi);
2903 : 0 : continue;
2904 : : }
2905 : :
2906 : : at_least_one_port_exist = true;
2907 : :
2908 : 0 : port = &ports[pi];
2909 : :
2910 : 0 : if (port->port_status == RTE_PORT_STOPPED) {
2911 : 0 : port->port_status = RTE_PORT_HANDLING;
2912 : : all_ports_already_started = false;
2913 : : } else {
2914 : 0 : fprintf(stderr, "Port %d is now not stopped\n", pi);
2915 : 0 : continue;
2916 : : }
2917 : :
2918 : 0 : if (port->need_reconfig > 0) {
2919 : : struct rte_eth_conf dev_conf;
2920 : : int k;
2921 : :
2922 : 0 : port->need_reconfig = 0;
2923 : :
2924 : 0 : if (flow_isolate_all) {
2925 : 0 : int ret = port_flow_isolate(pi, 1);
2926 : 0 : if (ret) {
2927 : 0 : fprintf(stderr,
2928 : : "Failed to apply isolated mode on port %d\n",
2929 : : pi);
2930 : 0 : return -1;
2931 : : }
2932 : : }
2933 : 0 : configure_rxtx_dump_callbacks(0);
2934 : 0 : printf("Configuring Port %d (socket %u)\n", pi,
2935 : : port->socket_id);
2936 : 0 : if (nb_hairpinq > 0 &&
2937 : 0 : rte_eth_dev_hairpin_capability_get(pi, &cap)) {
2938 : 0 : fprintf(stderr,
2939 : : "Port %d doesn't support hairpin queues\n",
2940 : : pi);
2941 : 0 : return -1;
2942 : : }
2943 : :
2944 : 0 : if (port->bond_flag == 1 && port->update_conf == 1) {
2945 : 0 : update_bonding_port_dev_conf(pi);
2946 : 0 : port->update_conf = 0;
2947 : : }
2948 : :
2949 : : /* configure port */
2950 : 0 : diag = eth_dev_configure_mp(pi, nb_rxq + nb_hairpinq,
2951 : 0 : nb_txq + nb_hairpinq,
2952 : 0 : &(port->dev_conf));
2953 : 0 : if (diag != 0) {
2954 : 0 : if (port->port_status == RTE_PORT_HANDLING)
2955 : 0 : port->port_status = RTE_PORT_STOPPED;
2956 : : else
2957 : 0 : fprintf(stderr,
2958 : : "Port %d can not be set back to stopped\n",
2959 : : pi);
2960 : 0 : fprintf(stderr, "Fail to configure port %d\n",
2961 : : pi);
2962 : : /* try to reconfigure port next time */
2963 : 0 : port->need_reconfig = 1;
2964 : 0 : return -1;
2965 : : }
2966 : : /* get device configuration*/
2967 : 0 : if (0 !=
2968 : 0 : eth_dev_conf_get_print_err(pi, &dev_conf)) {
2969 : 0 : fprintf(stderr,
2970 : : "port %d can not get device configuration\n",
2971 : : pi);
2972 : 0 : return -1;
2973 : : }
2974 : : /* Apply Rx offloads configuration */
2975 : 0 : if (dev_conf.rxmode.offloads !=
2976 : 0 : port->dev_conf.rxmode.offloads) {
2977 : 0 : port->dev_conf.rxmode.offloads |=
2978 : : dev_conf.rxmode.offloads;
2979 : 0 : for (k = 0;
2980 : 0 : k < port->dev_info.max_rx_queues;
2981 : 0 : k++)
2982 : 0 : port->rxq[k].conf.offloads |=
2983 : : dev_conf.rxmode.offloads;
2984 : : }
2985 : : /* Apply Tx offloads configuration */
2986 : 0 : if (dev_conf.txmode.offloads !=
2987 : 0 : port->dev_conf.txmode.offloads) {
2988 : 0 : port->dev_conf.txmode.offloads |=
2989 : : dev_conf.txmode.offloads;
2990 : 0 : for (k = 0;
2991 : 0 : k < port->dev_info.max_tx_queues;
2992 : 0 : k++)
2993 : 0 : port->txq[k].conf.offloads |=
2994 : : dev_conf.txmode.offloads;
2995 : : }
2996 : : }
2997 : 0 : if (port->need_reconfig_queues > 0 && is_proc_primary()) {
2998 : 0 : port->need_reconfig_queues = 0;
2999 : : /* setup tx queues */
3000 : 0 : for (qi = 0; qi < nb_txq; qi++) {
3001 : : struct rte_eth_txconf *conf =
3002 : 0 : &port->txq[qi].conf;
3003 : :
3004 : 0 : if ((numa_support) &&
3005 : 0 : (txring_numa[pi] != NUMA_NO_CONFIG))
3006 : 0 : diag = rte_eth_tx_queue_setup(pi, qi,
3007 : 0 : port->nb_tx_desc[qi],
3008 : : txring_numa[pi],
3009 : 0 : &(port->txq[qi].conf));
3010 : : else
3011 : 0 : diag = rte_eth_tx_queue_setup(pi, qi,
3012 : 0 : port->nb_tx_desc[qi],
3013 : : port->socket_id,
3014 : 0 : &(port->txq[qi].conf));
3015 : :
3016 : 0 : if (diag == 0) {
3017 : 0 : port->txq[qi].state =
3018 : 0 : conf->tx_deferred_start ?
3019 : 0 : RTE_ETH_QUEUE_STATE_STOPPED :
3020 : : RTE_ETH_QUEUE_STATE_STARTED;
3021 : : continue;
3022 : : }
3023 : :
3024 : : /* Fail to setup tx queue, return */
3025 : 0 : if (port->port_status == RTE_PORT_HANDLING)
3026 : 0 : port->port_status = RTE_PORT_STOPPED;
3027 : : else
3028 : 0 : fprintf(stderr,
3029 : : "Port %d can not be set back to stopped\n",
3030 : : pi);
3031 : 0 : fprintf(stderr,
3032 : : "Fail to configure port %d tx queues\n",
3033 : : pi);
3034 : : /* try to reconfigure queues next time */
3035 : 0 : port->need_reconfig_queues = 1;
3036 : 0 : return -1;
3037 : : }
3038 : 0 : for (qi = 0; qi < nb_rxq; qi++) {
3039 : : /* setup rx queues */
3040 : 0 : if ((numa_support) &&
3041 : 0 : (rxring_numa[pi] != NUMA_NO_CONFIG)) {
3042 : : struct rte_mempool * mp =
3043 : 0 : mbuf_pool_find
3044 : : (rxring_numa[pi], 0);
3045 : 0 : if (mp == NULL) {
3046 : 0 : fprintf(stderr,
3047 : : "Failed to setup RX queue: No mempool allocation on the socket %d\n",
3048 : 0 : rxring_numa[pi]);
3049 : 0 : return -1;
3050 : : }
3051 : :
3052 : 0 : diag = rx_queue_setup(pi, qi,
3053 : 0 : port->nb_rx_desc[qi],
3054 : 0 : rxring_numa[pi],
3055 : 0 : &(port->rxq[qi].conf),
3056 : : mp);
3057 : : } else {
3058 : : struct rte_mempool *mp =
3059 : 0 : mbuf_pool_find
3060 : : (port->socket_id, 0);
3061 : 0 : if (mp == NULL) {
3062 : 0 : fprintf(stderr,
3063 : : "Failed to setup RX queue: No mempool allocation on the socket %d\n",
3064 : : port->socket_id);
3065 : 0 : return -1;
3066 : : }
3067 : 0 : diag = rx_queue_setup(pi, qi,
3068 : 0 : port->nb_rx_desc[qi],
3069 : : port->socket_id,
3070 : 0 : &(port->rxq[qi].conf),
3071 : : mp);
3072 : : }
3073 : 0 : if (diag == 0)
3074 : : continue;
3075 : :
3076 : : /* Fail to setup rx queue, return */
3077 : 0 : if (port->port_status == RTE_PORT_HANDLING)
3078 : 0 : port->port_status = RTE_PORT_STOPPED;
3079 : : else
3080 : 0 : fprintf(stderr,
3081 : : "Port %d can not be set back to stopped\n",
3082 : : pi);
3083 : 0 : fprintf(stderr,
3084 : : "Fail to configure port %d rx queues\n",
3085 : : pi);
3086 : : /* try to reconfigure queues next time */
3087 : 0 : port->need_reconfig_queues = 1;
3088 : 0 : return -1;
3089 : : }
3090 : : /* setup hairpin queues */
3091 : 0 : if (setup_hairpin_queues(pi, p_pi, cnt_pi) != 0)
3092 : : return -1;
3093 : : }
3094 : 0 : configure_rxtx_dump_callbacks(verbose_level);
3095 : 0 : if (clear_ptypes) {
3096 : 0 : diag = rte_eth_dev_set_ptypes(pi, RTE_PTYPE_UNKNOWN,
3097 : : NULL, 0);
3098 : 0 : if (diag < 0)
3099 : 0 : fprintf(stderr,
3100 : : "Port %d: Failed to disable Ptype parsing\n",
3101 : : pi);
3102 : : }
3103 : :
3104 : : p_pi = pi;
3105 : 0 : cnt_pi++;
3106 : :
3107 : : /* start port */
3108 : 0 : diag = eth_dev_start_mp(pi);
3109 : 0 : if (diag < 0) {
3110 : 0 : fprintf(stderr, "Fail to start port %d: %s\n",
3111 : : pi, rte_strerror(-diag));
3112 : :
3113 : : /* Fail to setup rx queue, return */
3114 : 0 : if (port->port_status == RTE_PORT_HANDLING)
3115 : 0 : port->port_status = RTE_PORT_STOPPED;
3116 : : else
3117 : 0 : fprintf(stderr,
3118 : : "Port %d can not be set back to stopped\n",
3119 : : pi);
3120 : 0 : continue;
3121 : : }
3122 : :
3123 : 0 : if (port->port_status == RTE_PORT_HANDLING)
3124 : 0 : port->port_status = RTE_PORT_STARTED;
3125 : : else
3126 : 0 : fprintf(stderr, "Port %d can not be set into started\n",
3127 : : pi);
3128 : :
3129 : 0 : if (eth_macaddr_get_print_err(pi, &port->eth_addr) == 0)
3130 : 0 : printf("Port %d: " RTE_ETHER_ADDR_PRT_FMT "\n", pi,
3131 : 0 : RTE_ETHER_ADDR_BYTES(&port->eth_addr));
3132 : :
3133 : : at_least_one_port_successfully_started = true;
3134 : :
3135 : 0 : pl[cfg_pi++] = pi;
3136 : : }
3137 : :
3138 : 0 : update_queue_state(pi);
3139 : :
3140 : 0 : if (at_least_one_port_successfully_started && !no_link_check)
3141 : 0 : check_all_ports_link_status(RTE_PORT_ALL);
3142 : 0 : else if (at_least_one_port_exist & all_ports_already_started)
3143 : 0 : fprintf(stderr, "Please stop the ports first\n");
3144 : :
3145 : 0 : if (hairpin_mode & 0xf) {
3146 : 0 : diag = hairpin_bind(cfg_pi, pl, peer_pl);
3147 : 0 : if (diag < 0)
3148 : : return -1;
3149 : : }
3150 : :
3151 : 0 : fill_xstats_display_info_for_port(pid);
3152 : :
3153 : : printf("Done\n");
3154 : 0 : return 0;
3155 : : }
3156 : :
3157 : : void
3158 : 0 : stop_port(portid_t pid)
3159 : : {
3160 : : portid_t pi;
3161 : : struct rte_port *port;
3162 : : int need_check_link_status = 0;
3163 : : portid_t peer_pl[RTE_MAX_ETHPORTS];
3164 : : int peer_pi;
3165 : : int ret;
3166 : :
3167 : 0 : if (port_id_is_invalid(pid, ENABLED_WARN))
3168 : 0 : return;
3169 : :
3170 : : printf("Stopping ports...\n");
3171 : :
3172 : 0 : RTE_ETH_FOREACH_DEV(pi) {
3173 : 0 : if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
3174 : 0 : continue;
3175 : :
3176 : 0 : if (port_is_forwarding(pi) != 0 && test_done == 0) {
3177 : 0 : fprintf(stderr,
3178 : : "Please remove port %d from forwarding configuration.\n",
3179 : : pi);
3180 : 0 : continue;
3181 : : }
3182 : :
3183 : 0 : if (port_is_bonding_member(pi)) {
3184 : 0 : fprintf(stderr,
3185 : : "Please remove port %d from bonding device.\n",
3186 : : pi);
3187 : 0 : continue;
3188 : : }
3189 : :
3190 : 0 : port = &ports[pi];
3191 : 0 : if (port->port_status == RTE_PORT_STARTED)
3192 : 0 : port->port_status = RTE_PORT_HANDLING;
3193 : : else
3194 : 0 : continue;
3195 : :
3196 : 0 : if (hairpin_mode & 0xf) {
3197 : : int j;
3198 : :
3199 : 0 : rte_eth_hairpin_unbind(pi, RTE_MAX_ETHPORTS);
3200 : : /* unbind all peer Tx from current Rx */
3201 : 0 : peer_pi = rte_eth_hairpin_get_peer_ports(pi, peer_pl,
3202 : : RTE_MAX_ETHPORTS, 0);
3203 : 0 : if (peer_pi < 0)
3204 : 0 : continue;
3205 : 0 : for (j = 0; j < peer_pi; j++) {
3206 : 0 : if (!port_is_started(peer_pl[j]))
3207 : 0 : continue;
3208 : 0 : rte_eth_hairpin_unbind(peer_pl[j], pi);
3209 : : }
3210 : : }
3211 : :
3212 : 0 : if (port->flow_list && !no_flow_flush)
3213 : 0 : port_flow_flush(pi);
3214 : :
3215 : 0 : ret = eth_dev_stop_mp(pi);
3216 : 0 : if (ret != 0) {
3217 : 0 : TESTPMD_LOG(ERR,
3218 : : "rte_eth_dev_stop failed for port %u\n", pi);
3219 : : /* Allow to retry stopping the port. */
3220 : 0 : port->port_status = RTE_PORT_STARTED;
3221 : 0 : continue;
3222 : : }
3223 : :
3224 : 0 : if (port->port_status == RTE_PORT_HANDLING)
3225 : 0 : port->port_status = RTE_PORT_STOPPED;
3226 : : else
3227 : 0 : fprintf(stderr, "Port %d can not be set into stopped\n",
3228 : : pi);
3229 : : need_check_link_status = 1;
3230 : : }
3231 : 0 : if (need_check_link_status && !no_link_check)
3232 : 0 : check_all_ports_link_status(RTE_PORT_ALL);
3233 : :
3234 : : printf("Done\n");
3235 : : }
3236 : :
3237 : : static void
3238 : 0 : remove_invalid_ports_in(portid_t *array, portid_t *total)
3239 : : {
3240 : : portid_t i;
3241 : : portid_t new_total = 0;
3242 : :
3243 : 0 : for (i = 0; i < *total; i++)
3244 : 0 : if (!port_id_is_invalid(array[i], DISABLED_WARN)) {
3245 : 0 : array[new_total] = array[i];
3246 : 0 : new_total++;
3247 : : }
3248 : 0 : *total = new_total;
3249 : 0 : }
3250 : :
3251 : : static void
3252 : 0 : remove_invalid_ports(void)
3253 : : {
3254 : 0 : remove_invalid_ports_in(ports_ids, &nb_ports);
3255 : 0 : remove_invalid_ports_in(fwd_ports_ids, &nb_fwd_ports);
3256 : 0 : nb_cfg_ports = nb_fwd_ports;
3257 : 0 : printf("Now total ports is %d\n", nb_ports);
3258 : 0 : }
3259 : :
3260 : : static void
3261 : 0 : flush_port_owned_resources(portid_t pi)
3262 : : {
3263 : 0 : mcast_addr_pool_destroy(pi);
3264 : 0 : port_flow_flush(pi);
3265 : 0 : port_flow_template_table_flush(pi);
3266 : 0 : port_flow_pattern_template_flush(pi);
3267 : 0 : port_flow_actions_template_flush(pi);
3268 : 0 : port_flex_item_flush(pi);
3269 : 0 : port_action_handle_flush(pi);
3270 : 0 : }
3271 : :
3272 : : static void
3273 : 0 : clear_bonding_member_device(portid_t *member_pids, uint16_t num_members)
3274 : : {
3275 : : struct rte_port *port;
3276 : : portid_t member_pid;
3277 : : uint16_t i;
3278 : :
3279 : 0 : for (i = 0; i < num_members; i++) {
3280 : 0 : member_pid = member_pids[i];
3281 : 0 : if (port_is_started(member_pid) == 1) {
3282 : 0 : if (rte_eth_dev_stop(member_pid) != 0)
3283 : 0 : fprintf(stderr, "rte_eth_dev_stop failed for port %u\n",
3284 : : member_pid);
3285 : :
3286 : 0 : port = &ports[member_pid];
3287 : 0 : port->port_status = RTE_PORT_STOPPED;
3288 : : }
3289 : :
3290 : : clear_port_member_flag(member_pid);
3291 : :
3292 : : /* Close member device when testpmd quit or is killed. */
3293 : 0 : if (cl_quit == 1 || f_quit == 1)
3294 : 0 : rte_eth_dev_close(member_pid);
3295 : : }
3296 : 0 : }
3297 : :
3298 : : void
3299 : 0 : close_port(portid_t pid)
3300 : : {
3301 : : portid_t pi;
3302 : : struct rte_port *port;
3303 : : portid_t member_pids[RTE_MAX_ETHPORTS];
3304 : : int num_members = 0;
3305 : :
3306 : 0 : if (port_id_is_invalid(pid, ENABLED_WARN))
3307 : 0 : return;
3308 : :
3309 : : printf("Closing ports...\n");
3310 : :
3311 : 0 : RTE_ETH_FOREACH_DEV(pi) {
3312 : 0 : if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
3313 : 0 : continue;
3314 : :
3315 : 0 : if (port_is_forwarding(pi) != 0 && test_done == 0) {
3316 : 0 : fprintf(stderr,
3317 : : "Please remove port %d from forwarding configuration.\n",
3318 : : pi);
3319 : 0 : continue;
3320 : : }
3321 : :
3322 : 0 : if (port_is_bonding_member(pi)) {
3323 : 0 : fprintf(stderr,
3324 : : "Please remove port %d from bonding device.\n",
3325 : : pi);
3326 : 0 : continue;
3327 : : }
3328 : :
3329 : 0 : port = &ports[pi];
3330 : 0 : if (port->port_status == RTE_PORT_CLOSED) {
3331 : 0 : fprintf(stderr, "Port %d is already closed\n", pi);
3332 : 0 : continue;
3333 : : }
3334 : :
3335 : 0 : if (is_proc_primary()) {
3336 : 0 : flush_port_owned_resources(pi);
3337 : : #ifdef RTE_NET_BOND
3338 : 0 : if (port->bond_flag == 1)
3339 : 0 : num_members = rte_eth_bond_members_get(pi,
3340 : : member_pids, RTE_MAX_ETHPORTS);
3341 : : #endif
3342 : 0 : rte_eth_dev_close(pi);
3343 : : /*
3344 : : * If this port is bonding device, all members under the
3345 : : * device need to be removed or closed.
3346 : : */
3347 : 0 : if (port->bond_flag == 1 && num_members > 0)
3348 : 0 : clear_bonding_member_device(member_pids,
3349 : : num_members);
3350 : : }
3351 : :
3352 : 0 : free_xstats_display_info(pi);
3353 : : }
3354 : :
3355 : 0 : remove_invalid_ports();
3356 : : printf("Done\n");
3357 : : }
3358 : :
3359 : : void
3360 : 0 : reset_port(portid_t pid)
3361 : : {
3362 : : int diag;
3363 : : portid_t pi;
3364 : : struct rte_port *port;
3365 : :
3366 : 0 : if (port_id_is_invalid(pid, ENABLED_WARN))
3367 : : return;
3368 : :
3369 : 0 : if ((pid == (portid_t)RTE_PORT_ALL && !all_ports_stopped()) ||
3370 : : (pid != (portid_t)RTE_PORT_ALL && !port_is_stopped(pid))) {
3371 : 0 : fprintf(stderr,
3372 : : "Can not reset port(s), please stop port(s) first.\n");
3373 : 0 : return;
3374 : : }
3375 : :
3376 : : printf("Resetting ports...\n");
3377 : :
3378 : 0 : RTE_ETH_FOREACH_DEV(pi) {
3379 : 0 : if (pid != pi && pid != (portid_t)RTE_PORT_ALL)
3380 : 0 : continue;
3381 : :
3382 : 0 : if (port_is_forwarding(pi) != 0 && test_done == 0) {
3383 : 0 : fprintf(stderr,
3384 : : "Please remove port %d from forwarding configuration.\n",
3385 : : pi);
3386 : 0 : continue;
3387 : : }
3388 : :
3389 : 0 : if (port_is_bonding_member(pi)) {
3390 : 0 : fprintf(stderr,
3391 : : "Please remove port %d from bonding device.\n",
3392 : : pi);
3393 : 0 : continue;
3394 : : }
3395 : :
3396 : 0 : if (is_proc_primary()) {
3397 : 0 : diag = rte_eth_dev_reset(pi);
3398 : 0 : if (diag == 0) {
3399 : 0 : port = &ports[pi];
3400 : 0 : port->need_reconfig = 1;
3401 : 0 : port->need_reconfig_queues = 1;
3402 : : } else {
3403 : 0 : fprintf(stderr, "Failed to reset port %d. diag=%d\n",
3404 : : pi, diag);
3405 : : }
3406 : : }
3407 : : }
3408 : :
3409 : : printf("Done\n");
3410 : : }
3411 : :
3412 : : void
3413 : 0 : attach_port(char *identifier)
3414 : : {
3415 : : portid_t pi;
3416 : : struct rte_dev_iterator iterator;
3417 : :
3418 : : printf("Attaching a new port...\n");
3419 : :
3420 : 0 : if (identifier == NULL) {
3421 : 0 : fprintf(stderr, "Invalid parameters are specified\n");
3422 : 0 : return;
3423 : : }
3424 : :
3425 : 0 : if (rte_dev_probe(identifier) < 0) {
3426 : 0 : TESTPMD_LOG(ERR, "Failed to attach port %s\n", identifier);
3427 : 0 : return;
3428 : : }
3429 : :
3430 : : /* First attach mode: event
3431 : : * New port flag is updated on RTE_ETH_EVENT_NEW event
3432 : : */
3433 : 0 : if (setup_on_probe_event) {
3434 : 0 : goto out;
3435 : : }
3436 : :
3437 : : /* second attach mode: iterator */
3438 : 0 : RTE_ETH_FOREACH_MATCHING_DEV(pi, identifier, &iterator) {
3439 : : /* setup ports matching the devargs used for probing */
3440 : 0 : if (port_is_forwarding(pi))
3441 : 0 : continue; /* port was already attached before */
3442 : 0 : setup_attached_port((void *)(intptr_t)pi);
3443 : : }
3444 : 0 : out:
3445 : : printf("Port %s is attached.\n", identifier);
3446 : : printf("Done\n");
3447 : : }
3448 : :
3449 : : static void
3450 : 0 : setup_attached_port(void *arg)
3451 : : {
3452 : 0 : portid_t pi = (intptr_t)arg;
3453 : : unsigned int socket_id;
3454 : : int ret;
3455 : :
3456 : 0 : socket_id = (unsigned)rte_eth_dev_socket_id(pi);
3457 : : /* if socket_id is invalid, set to the first available socket. */
3458 : 0 : if (check_socket_id(socket_id) < 0)
3459 : 0 : socket_id = socket_ids[0];
3460 : : reconfig(pi, socket_id);
3461 : 0 : ret = rte_eth_promiscuous_enable(pi);
3462 : 0 : if (ret != 0)
3463 : 0 : fprintf(stderr,
3464 : : "Error during enabling promiscuous mode for port %u: %s - ignore\n",
3465 : : pi, rte_strerror(-ret));
3466 : :
3467 : 0 : ports[pi].need_setup = 0;
3468 : 0 : ports[pi].port_status = RTE_PORT_STOPPED;
3469 : 0 : }
3470 : :
3471 : : static void
3472 : 0 : detach_device(struct rte_device *dev)
3473 : : {
3474 : : portid_t sibling;
3475 : :
3476 : 0 : if (dev == NULL) {
3477 : 0 : fprintf(stderr, "Device already removed\n");
3478 : 0 : return;
3479 : : }
3480 : :
3481 : : printf("Removing a device...\n");
3482 : :
3483 : 0 : RTE_ETH_FOREACH_DEV_OF(sibling, dev) {
3484 : 0 : if (ports[sibling].port_status != RTE_PORT_CLOSED) {
3485 : 0 : if (ports[sibling].port_status != RTE_PORT_STOPPED) {
3486 : 0 : fprintf(stderr, "Port %u not stopped\n",
3487 : : sibling);
3488 : 0 : return;
3489 : : }
3490 : 0 : flush_port_owned_resources(sibling);
3491 : : }
3492 : : }
3493 : :
3494 : 0 : if (rte_dev_remove(dev) < 0) {
3495 : 0 : TESTPMD_LOG(ERR, "Failed to detach device %s\n", rte_dev_name(dev));
3496 : 0 : return;
3497 : : }
3498 : :
3499 : : printf("Device is detached\n");
3500 : : printf("Done\n");
3501 : : return;
3502 : : }
3503 : :
3504 : : void
3505 : 0 : detach_port_device(portid_t port_id)
3506 : : {
3507 : : int ret;
3508 : : struct rte_eth_dev_info dev_info;
3509 : :
3510 : 0 : if (port_id_is_invalid(port_id, ENABLED_WARN))
3511 : 0 : return;
3512 : :
3513 : 0 : if (ports[port_id].port_status != RTE_PORT_CLOSED) {
3514 : 0 : if (ports[port_id].port_status != RTE_PORT_STOPPED) {
3515 : 0 : fprintf(stderr, "Port not stopped\n");
3516 : 0 : return;
3517 : : }
3518 : 0 : fprintf(stderr, "Port was not closed\n");
3519 : : }
3520 : :
3521 : 0 : ret = eth_dev_info_get_print_err(port_id, &dev_info);
3522 : 0 : if (ret != 0) {
3523 : 0 : TESTPMD_LOG(ERR,
3524 : : "Failed to get device info for port %d, not detaching\n",
3525 : : port_id);
3526 : 0 : return;
3527 : : }
3528 : 0 : detach_device(dev_info.device);
3529 : : }
3530 : :
3531 : : void
3532 : 0 : detach_devargs(char *identifier)
3533 : : {
3534 : : struct rte_dev_iterator iterator;
3535 : : struct rte_devargs da;
3536 : : portid_t port_id;
3537 : :
3538 : : printf("Removing a device...\n");
3539 : :
3540 : : memset(&da, 0, sizeof(da));
3541 : 0 : if (rte_devargs_parsef(&da, "%s", identifier)) {
3542 : 0 : fprintf(stderr, "cannot parse identifier\n");
3543 : 0 : return;
3544 : : }
3545 : :
3546 : 0 : RTE_ETH_FOREACH_MATCHING_DEV(port_id, identifier, &iterator) {
3547 : 0 : if (ports[port_id].port_status != RTE_PORT_CLOSED) {
3548 : 0 : if (ports[port_id].port_status != RTE_PORT_STOPPED) {
3549 : 0 : fprintf(stderr, "Port %u not stopped\n",
3550 : : port_id);
3551 : 0 : rte_eth_iterator_cleanup(&iterator);
3552 : 0 : rte_devargs_reset(&da);
3553 : 0 : return;
3554 : : }
3555 : 0 : flush_port_owned_resources(port_id);
3556 : : }
3557 : : }
3558 : :
3559 : 0 : if (rte_eal_hotplug_remove(rte_bus_name(da.bus), da.name) != 0) {
3560 : 0 : TESTPMD_LOG(ERR, "Failed to detach device %s(%s)\n",
3561 : : da.name, rte_bus_name(da.bus));
3562 : 0 : rte_devargs_reset(&da);
3563 : 0 : return;
3564 : : }
3565 : :
3566 : 0 : remove_invalid_ports();
3567 : :
3568 : : printf("Device %s is detached\n", identifier);
3569 : 0 : printf("Now total ports is %d\n", nb_ports);
3570 : : printf("Done\n");
3571 : 0 : rte_devargs_reset(&da);
3572 : : }
3573 : :
3574 : : void
3575 : 0 : pmd_test_exit(void)
3576 : : {
3577 : : portid_t pt_id;
3578 : : unsigned int i;
3579 : : int ret;
3580 : :
3581 : 0 : if (test_done == 0)
3582 : 0 : stop_packet_forwarding();
3583 : :
3584 : : #ifndef RTE_EXEC_ENV_WINDOWS
3585 : 0 : for (i = 0 ; i < RTE_DIM(mempools) ; i++) {
3586 : 0 : if (mempools[i]) {
3587 : 0 : if (mp_alloc_type == MP_ALLOC_ANON)
3588 : 0 : rte_mempool_mem_iter(mempools[i], dma_unmap_cb,
3589 : : NULL);
3590 : : }
3591 : : }
3592 : : #endif
3593 : 0 : if (ports != NULL) {
3594 : 0 : no_link_check = 1;
3595 : 0 : RTE_ETH_FOREACH_DEV(pt_id) {
3596 : 0 : printf("\nStopping port %d...\n", pt_id);
3597 : 0 : fflush(stdout);
3598 : 0 : stop_port(pt_id);
3599 : : }
3600 : 0 : RTE_ETH_FOREACH_DEV(pt_id) {
3601 : 0 : printf("\nShutting down port %d...\n", pt_id);
3602 : 0 : fflush(stdout);
3603 : 0 : close_port(pt_id);
3604 : : }
3605 : : }
3606 : :
3607 : 0 : if (hot_plug) {
3608 : 0 : ret = rte_dev_event_monitor_stop();
3609 : 0 : if (ret) {
3610 : 0 : TESTPMD_LOG(ERR, "fail to stop device event monitor.");
3611 : 0 : return;
3612 : : }
3613 : :
3614 : 0 : ret = rte_dev_event_callback_unregister(NULL,
3615 : : dev_event_callback, NULL);
3616 : 0 : if (ret < 0) {
3617 : 0 : TESTPMD_LOG(ERR, "fail to unregister device event callback.\n");
3618 : 0 : return;
3619 : : }
3620 : :
3621 : 0 : ret = rte_dev_hotplug_handle_disable();
3622 : 0 : if (ret) {
3623 : 0 : TESTPMD_LOG(ERR, "fail to disable hotplug handling.\n");
3624 : 0 : return;
3625 : : }
3626 : : }
3627 : 0 : for (i = 0 ; i < RTE_DIM(mempools) ; i++) {
3628 : 0 : if (mempools[i])
3629 : : mempool_free_mp(mempools[i]);
3630 : : }
3631 : 0 : free(xstats_display);
3632 : :
3633 : : printf("\nBye...\n");
3634 : : }
3635 : :
3636 : : typedef void (*cmd_func_t)(void);
3637 : : struct pmd_test_command {
3638 : : const char *cmd_name;
3639 : : cmd_func_t cmd_func;
3640 : : };
3641 : :
3642 : : /* Check the link status of all ports in up to 9s, and print them finally */
3643 : : static void
3644 : 0 : check_all_ports_link_status(uint32_t port_mask)
3645 : : {
3646 : : #define CHECK_INTERVAL 100 /* 100ms */
3647 : : #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3648 : : portid_t portid;
3649 : : uint8_t count, all_ports_up, print_flag = 0;
3650 : : struct rte_eth_link link;
3651 : : int ret;
3652 : : char link_status[RTE_ETH_LINK_MAX_STR_LEN];
3653 : :
3654 : : printf("Checking link statuses...\n");
3655 : 0 : fflush(stdout);
3656 : 0 : for (count = 0; count <= MAX_CHECK_TIME; count++) {
3657 : : all_ports_up = 1;
3658 : 0 : RTE_ETH_FOREACH_DEV(portid) {
3659 : 0 : if ((port_mask & (1 << portid)) == 0)
3660 : 0 : continue;
3661 : : memset(&link, 0, sizeof(link));
3662 : 0 : ret = rte_eth_link_get_nowait(portid, &link);
3663 : 0 : if (ret < 0) {
3664 : : all_ports_up = 0;
3665 : 0 : if (print_flag == 1)
3666 : 0 : fprintf(stderr,
3667 : : "Port %u link get failed: %s\n",
3668 : : portid, rte_strerror(-ret));
3669 : 0 : continue;
3670 : : }
3671 : : /* print link status if flag set */
3672 : 0 : if (print_flag == 1) {
3673 : 0 : rte_eth_link_to_str(link_status,
3674 : : sizeof(link_status), &link);
3675 : : printf("Port %d %s\n", portid, link_status);
3676 : 0 : continue;
3677 : : }
3678 : : /* clear all_ports_up flag if any link down */
3679 : 0 : if (link.link_status == RTE_ETH_LINK_DOWN) {
3680 : : all_ports_up = 0;
3681 : : break;
3682 : : }
3683 : : }
3684 : : /* after finally printing all link status, get out */
3685 : 0 : if (print_flag == 1)
3686 : : break;
3687 : :
3688 : 0 : if (all_ports_up == 0) {
3689 : 0 : fflush(stdout);
3690 : : rte_delay_ms(CHECK_INTERVAL);
3691 : : }
3692 : :
3693 : : /* set the print_flag if all ports up or timeout */
3694 : 0 : if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3695 : : print_flag = 1;
3696 : : }
3697 : :
3698 : 0 : if (lsc_interrupt)
3699 : : break;
3700 : : }
3701 : 0 : }
3702 : :
3703 : : static void
3704 : 0 : rmv_port_callback(void *arg)
3705 : : {
3706 : : int need_to_start = 0;
3707 : 0 : int org_no_link_check = no_link_check;
3708 : 0 : portid_t port_id = (intptr_t)arg;
3709 : : struct rte_eth_dev_info dev_info;
3710 : : int ret;
3711 : :
3712 : 0 : RTE_ETH_VALID_PORTID_OR_RET(port_id);
3713 : :
3714 : 0 : if (!test_done && port_is_forwarding(port_id)) {
3715 : : need_to_start = 1;
3716 : 0 : stop_packet_forwarding();
3717 : : }
3718 : 0 : no_link_check = 1;
3719 : 0 : stop_port(port_id);
3720 : 0 : no_link_check = org_no_link_check;
3721 : :
3722 : 0 : ret = eth_dev_info_get_print_err(port_id, &dev_info);
3723 : 0 : if (ret != 0)
3724 : 0 : TESTPMD_LOG(ERR,
3725 : : "Failed to get device info for port %d, not detaching\n",
3726 : : port_id);
3727 : : else {
3728 : 0 : struct rte_device *device = dev_info.device;
3729 : 0 : close_port(port_id);
3730 : 0 : detach_device(device); /* might be already removed or have more ports */
3731 : 0 : remove_invalid_ports();
3732 : : }
3733 : 0 : if (need_to_start)
3734 : 0 : start_packet_forwarding(0);
3735 : : }
3736 : :
3737 : : static void
3738 : 0 : remove_invalid_ports_callback(void *arg)
3739 : : {
3740 : 0 : portid_t port_id = (intptr_t)arg;
3741 : : int need_to_start = 0;
3742 : :
3743 : 0 : if (!test_done && port_is_forwarding(port_id)) {
3744 : : need_to_start = 1;
3745 : 0 : stop_packet_forwarding();
3746 : : }
3747 : :
3748 : 0 : remove_invalid_ports();
3749 : :
3750 : : if (need_to_start)
3751 : 0 : start_packet_forwarding(0);
3752 : 0 : }
3753 : :
3754 : : /* This function is used by the interrupt thread */
3755 : : static int
3756 : 0 : eth_event_callback(portid_t port_id, enum rte_eth_event_type type, void *param,
3757 : : void *ret_param)
3758 : : {
3759 : : RTE_SET_USED(param);
3760 : : RTE_SET_USED(ret_param);
3761 : :
3762 : 0 : if (type >= RTE_ETH_EVENT_MAX) {
3763 : 0 : fprintf(stderr,
3764 : : "\nPort %" PRIu16 ": %s called upon invalid event %d\n",
3765 : : port_id, __func__, type);
3766 : 0 : fflush(stderr);
3767 : 0 : } else if (event_print_mask & (UINT32_C(1) << type)) {
3768 : 0 : printf("\nPort %" PRIu16 ": %s event\n", port_id,
3769 : 0 : eth_event_desc[type]);
3770 : 0 : fflush(stdout);
3771 : : }
3772 : :
3773 : 0 : switch (type) {
3774 : 0 : case RTE_ETH_EVENT_NEW:
3775 : : /* The port in ports_id and fwd_ports_ids is always valid
3776 : : * from index 0 ~ (nb_ports - 1) due to updating their
3777 : : * position when one port is detached or removed.
3778 : : */
3779 : 0 : ports_ids[nb_ports++] = port_id;
3780 : 0 : fwd_ports_ids[nb_fwd_ports++] = port_id;
3781 : 0 : nb_cfg_ports = nb_fwd_ports;
3782 : 0 : printf("Port %d is probed. Now total ports is %d\n", port_id, nb_ports);
3783 : :
3784 : 0 : if (setup_on_probe_event) {
3785 : 0 : ports[port_id].need_setup = 1;
3786 : 0 : ports[port_id].port_status = RTE_PORT_HANDLING;
3787 : : }
3788 : : /* Can't initialize port directly in new event. */
3789 : 0 : if (rte_eal_alarm_set(100000, setup_attached_port,
3790 : 0 : (void *)(intptr_t)port_id))
3791 : 0 : fprintf(stderr, "Could not set up deferred task to setup this attached port.\n");
3792 : : break;
3793 : 0 : case RTE_ETH_EVENT_INTR_RMV:
3794 : 0 : if (port_id_is_invalid(port_id, DISABLED_WARN))
3795 : : break;
3796 : 0 : if (rte_eal_alarm_set(100000,
3797 : 0 : rmv_port_callback, (void *)(intptr_t)port_id))
3798 : 0 : fprintf(stderr,
3799 : : "Could not set up deferred device removal\n");
3800 : : break;
3801 : 0 : case RTE_ETH_EVENT_DESTROY:
3802 : 0 : ports[port_id].port_status = RTE_PORT_CLOSED;
3803 : 0 : printf("Port %u is closed\n", port_id);
3804 : : /*
3805 : : * Defer to remove port id due to the reason that the ethdev
3806 : : * state is changed from 'ATTACHED' to 'UNUSED' only after the
3807 : : * event callback finished. Otherwise this port id can not be
3808 : : * removed.
3809 : : */
3810 : 0 : if (rte_eal_alarm_set(100000, remove_invalid_ports_callback,
3811 : 0 : (void *)(intptr_t)port_id))
3812 : 0 : fprintf(stderr, "Could not set up deferred task to remove this port id.\n");
3813 : : break;
3814 : 0 : case RTE_ETH_EVENT_RX_AVAIL_THRESH: {
3815 : : uint16_t rxq_id;
3816 : : int ret;
3817 : :
3818 : : /* avail_thresh query API rewinds rxq_id, no need to check max RxQ num */
3819 : 0 : for (rxq_id = 0; ; rxq_id++) {
3820 : 0 : ret = rte_eth_rx_avail_thresh_query(port_id, &rxq_id,
3821 : : NULL);
3822 : 0 : if (ret <= 0)
3823 : : break;
3824 : 0 : printf("Received avail_thresh event, port: %u, rxq_id: %u\n",
3825 : : port_id, rxq_id);
3826 : :
3827 : : #ifdef RTE_NET_MLX5
3828 : 0 : mlx5_test_avail_thresh_event_handler(port_id, rxq_id);
3829 : : #endif
3830 : : }
3831 : : break;
3832 : : }
3833 : : default:
3834 : : break;
3835 : : }
3836 : 0 : return 0;
3837 : : }
3838 : :
3839 : : static int
3840 : 0 : register_eth_event_callback(void)
3841 : : {
3842 : : int ret;
3843 : : enum rte_eth_event_type event;
3844 : :
3845 : 0 : for (event = RTE_ETH_EVENT_UNKNOWN;
3846 : 0 : event < RTE_ETH_EVENT_MAX; event++) {
3847 : 0 : ret = rte_eth_dev_callback_register(RTE_ETH_ALL,
3848 : : event,
3849 : : eth_event_callback,
3850 : : NULL);
3851 : 0 : if (ret != 0) {
3852 : 0 : TESTPMD_LOG(ERR, "Failed to register callback for "
3853 : : "%s event\n", eth_event_desc[event]);
3854 : 0 : return -1;
3855 : : }
3856 : : }
3857 : :
3858 : : return 0;
3859 : : }
3860 : :
3861 : : static int
3862 : 0 : unregister_eth_event_callback(void)
3863 : : {
3864 : : int ret;
3865 : : enum rte_eth_event_type event;
3866 : :
3867 : 0 : for (event = RTE_ETH_EVENT_UNKNOWN;
3868 : 0 : event < RTE_ETH_EVENT_MAX; event++) {
3869 : 0 : ret = rte_eth_dev_callback_unregister(RTE_ETH_ALL,
3870 : : event,
3871 : : eth_event_callback,
3872 : : NULL);
3873 : 0 : if (ret != 0) {
3874 : 0 : TESTPMD_LOG(ERR, "Failed to unregister callback for "
3875 : : "%s event\n", eth_event_desc[event]);
3876 : 0 : return -1;
3877 : : }
3878 : : }
3879 : :
3880 : : return 0;
3881 : : }
3882 : :
3883 : : /* This function is used by the interrupt thread */
3884 : : static void
3885 : 0 : dev_event_callback(const char *device_name, enum rte_dev_event_type type,
3886 : : __rte_unused void *arg)
3887 : : {
3888 : : uint16_t port_id;
3889 : : int ret;
3890 : :
3891 : 0 : switch (type) {
3892 : 0 : case RTE_DEV_EVENT_REMOVE:
3893 : 0 : TESTPMD_LOG(INFO, "The device: %s has been removed!\n", device_name);
3894 : 0 : ret = rte_eth_dev_get_port_by_name(device_name, &port_id);
3895 : 0 : if (ret) {
3896 : 0 : TESTPMD_LOG(ERR,
3897 : : "Can not get port for device %s!\n", device_name);
3898 : 0 : return;
3899 : : }
3900 : : /*
3901 : : * Because the user's callback is invoked in eal interrupt
3902 : : * callback, the interrupt callback need to be finished before
3903 : : * it can be unregistered when detaching device. So finish
3904 : : * callback soon and use a deferred removal to detach device
3905 : : * is need. It is a workaround, once the device detaching be
3906 : : * moved into the eal in the future, the deferred removal could
3907 : : * be deleted.
3908 : : */
3909 : 0 : if (rte_eal_alarm_set(100000,
3910 : 0 : rmv_port_callback, (void *)(intptr_t)port_id))
3911 : 0 : TESTPMD_LOG(ERR, "Could not set up deferred device removal\n");
3912 : : break;
3913 : :
3914 : 0 : case RTE_DEV_EVENT_ADD:
3915 : 0 : TESTPMD_LOG(INFO, "The device: %s has been added!\n", device_name);
3916 : : /* TODO: After finish kernel driver binding,
3917 : : * begin to attach port.
3918 : : */
3919 : 0 : break;
3920 : :
3921 : : default:
3922 : : if (type >= RTE_DEV_EVENT_MAX)
3923 : 0 : TESTPMD_LOG(ERR, "%s called upon invalid event %d\n",
3924 : : __func__, type);
3925 : : break;
3926 : : }
3927 : : }
3928 : :
3929 : : static void
3930 : 0 : rxtx_port_config(portid_t pid)
3931 : : {
3932 : : uint16_t qid;
3933 : : uint64_t offloads;
3934 : 0 : struct rte_port *port = &ports[pid];
3935 : :
3936 : 0 : for (qid = 0; qid < nb_rxq; qid++) {
3937 : 0 : offloads = port->rxq[qid].conf.offloads;
3938 : 0 : port->rxq[qid].conf = port->dev_info.default_rxconf;
3939 : :
3940 : 0 : if (rxq_share > 0 &&
3941 : 0 : (port->dev_info.dev_capa & RTE_ETH_DEV_CAPA_RXQ_SHARE)) {
3942 : : /* Non-zero share group to enable RxQ share. */
3943 : 0 : port->rxq[qid].conf.share_group = pid / rxq_share + 1;
3944 : 0 : port->rxq[qid].conf.share_qid = qid; /* Equal mapping. */
3945 : : }
3946 : :
3947 : 0 : if (offloads != 0)
3948 : 0 : port->rxq[qid].conf.offloads = offloads;
3949 : :
3950 : : /* Check if any Rx parameters have been passed */
3951 : 0 : if (rx_pthresh != RTE_PMD_PARAM_UNSET)
3952 : 0 : port->rxq[qid].conf.rx_thresh.pthresh = rx_pthresh;
3953 : :
3954 : 0 : if (rx_hthresh != RTE_PMD_PARAM_UNSET)
3955 : 0 : port->rxq[qid].conf.rx_thresh.hthresh = rx_hthresh;
3956 : :
3957 : 0 : if (rx_wthresh != RTE_PMD_PARAM_UNSET)
3958 : 0 : port->rxq[qid].conf.rx_thresh.wthresh = rx_wthresh;
3959 : :
3960 : 0 : if (rx_free_thresh != RTE_PMD_PARAM_UNSET)
3961 : 0 : port->rxq[qid].conf.rx_free_thresh = rx_free_thresh;
3962 : :
3963 : 0 : if (rx_drop_en != RTE_PMD_PARAM_UNSET)
3964 : 0 : port->rxq[qid].conf.rx_drop_en = rx_drop_en;
3965 : :
3966 : 0 : port->nb_rx_desc[qid] = nb_rxd;
3967 : : }
3968 : :
3969 : 0 : for (qid = 0; qid < nb_txq; qid++) {
3970 : 0 : offloads = port->txq[qid].conf.offloads;
3971 : 0 : port->txq[qid].conf = port->dev_info.default_txconf;
3972 : 0 : if (offloads != 0)
3973 : 0 : port->txq[qid].conf.offloads = offloads;
3974 : :
3975 : : /* Check if any Tx parameters have been passed */
3976 : 0 : if (tx_pthresh != RTE_PMD_PARAM_UNSET)
3977 : 0 : port->txq[qid].conf.tx_thresh.pthresh = tx_pthresh;
3978 : :
3979 : 0 : if (tx_hthresh != RTE_PMD_PARAM_UNSET)
3980 : 0 : port->txq[qid].conf.tx_thresh.hthresh = tx_hthresh;
3981 : :
3982 : 0 : if (tx_wthresh != RTE_PMD_PARAM_UNSET)
3983 : 0 : port->txq[qid].conf.tx_thresh.wthresh = tx_wthresh;
3984 : :
3985 : 0 : if (tx_rs_thresh != RTE_PMD_PARAM_UNSET)
3986 : 0 : port->txq[qid].conf.tx_rs_thresh = tx_rs_thresh;
3987 : :
3988 : 0 : if (tx_free_thresh != RTE_PMD_PARAM_UNSET)
3989 : 0 : port->txq[qid].conf.tx_free_thresh = tx_free_thresh;
3990 : :
3991 : 0 : port->nb_tx_desc[qid] = nb_txd;
3992 : : }
3993 : 0 : }
3994 : :
3995 : : /*
3996 : : * Helper function to set MTU from frame size
3997 : : *
3998 : : * port->dev_info should be set before calling this function.
3999 : : *
4000 : : * return 0 on success, negative on error
4001 : : */
4002 : : int
4003 : 0 : update_mtu_from_frame_size(portid_t portid, uint32_t max_rx_pktlen)
4004 : : {
4005 : 0 : struct rte_port *port = &ports[portid];
4006 : : uint32_t eth_overhead;
4007 : : uint16_t mtu, new_mtu;
4008 : :
4009 : : eth_overhead = get_eth_overhead(&port->dev_info);
4010 : :
4011 : 0 : if (rte_eth_dev_get_mtu(portid, &mtu) != 0) {
4012 : : printf("Failed to get MTU for port %u\n", portid);
4013 : 0 : return -1;
4014 : : }
4015 : :
4016 : 0 : new_mtu = max_rx_pktlen - eth_overhead;
4017 : :
4018 : 0 : if (mtu == new_mtu)
4019 : : return 0;
4020 : :
4021 : 0 : if (eth_dev_set_mtu_mp(portid, new_mtu) != 0) {
4022 : 0 : fprintf(stderr,
4023 : : "Failed to set MTU to %u for port %u\n",
4024 : : new_mtu, portid);
4025 : 0 : return -1;
4026 : : }
4027 : :
4028 : 0 : port->dev_conf.rxmode.mtu = new_mtu;
4029 : :
4030 : 0 : return 0;
4031 : : }
4032 : :
4033 : : void
4034 : 0 : init_port_config(void)
4035 : : {
4036 : : portid_t pid;
4037 : : struct rte_port *port;
4038 : : int ret, i;
4039 : :
4040 : 0 : RTE_ETH_FOREACH_DEV(pid) {
4041 : 0 : port = &ports[pid];
4042 : :
4043 : 0 : ret = eth_dev_info_get_print_err(pid, &port->dev_info);
4044 : 0 : if (ret != 0)
4045 : : return;
4046 : :
4047 : 0 : if (nb_rxq > 1) {
4048 : 0 : port->dev_conf.rx_adv_conf.rss_conf.rss_key = NULL;
4049 : 0 : port->dev_conf.rx_adv_conf.rss_conf.rss_hf =
4050 : 0 : rss_hf & port->dev_info.flow_type_rss_offloads;
4051 : : } else {
4052 : 0 : port->dev_conf.rx_adv_conf.rss_conf.rss_key = NULL;
4053 : 0 : port->dev_conf.rx_adv_conf.rss_conf.rss_hf = 0;
4054 : : }
4055 : :
4056 : 0 : if (port->dcb_flag == 0) {
4057 : 0 : if (port->dev_conf.rx_adv_conf.rss_conf.rss_hf != 0) {
4058 : 0 : port->dev_conf.rxmode.mq_mode =
4059 : 0 : (enum rte_eth_rx_mq_mode)
4060 : 0 : (rx_mq_mode & RTE_ETH_MQ_RX_RSS);
4061 : : } else {
4062 : 0 : port->dev_conf.rxmode.mq_mode = RTE_ETH_MQ_RX_NONE;
4063 : 0 : port->dev_conf.rxmode.offloads &=
4064 : : ~RTE_ETH_RX_OFFLOAD_RSS_HASH;
4065 : :
4066 : 0 : for (i = 0;
4067 : 0 : i < port->dev_info.nb_rx_queues;
4068 : 0 : i++)
4069 : 0 : port->rxq[i].conf.offloads &=
4070 : : ~RTE_ETH_RX_OFFLOAD_RSS_HASH;
4071 : : }
4072 : : }
4073 : :
4074 : 0 : rxtx_port_config(pid);
4075 : :
4076 : 0 : ret = eth_macaddr_get_print_err(pid, &port->eth_addr);
4077 : 0 : if (ret != 0)
4078 : : return;
4079 : :
4080 : 0 : if (lsc_interrupt && (*port->dev_info.dev_flags & RTE_ETH_DEV_INTR_LSC))
4081 : 0 : port->dev_conf.intr_conf.lsc = 1;
4082 : 0 : if (rmv_interrupt && (*port->dev_info.dev_flags & RTE_ETH_DEV_INTR_RMV))
4083 : 0 : port->dev_conf.intr_conf.rmv = 1;
4084 : : }
4085 : : }
4086 : :
4087 : 0 : void set_port_member_flag(portid_t member_pid)
4088 : : {
4089 : : struct rte_port *port;
4090 : :
4091 : 0 : port = &ports[member_pid];
4092 : 0 : port->member_flag = 1;
4093 : 0 : }
4094 : :
4095 : 0 : void clear_port_member_flag(portid_t member_pid)
4096 : : {
4097 : : struct rte_port *port;
4098 : :
4099 : 0 : port = &ports[member_pid];
4100 : 0 : port->member_flag = 0;
4101 : 0 : }
4102 : :
4103 : 0 : uint8_t port_is_bonding_member(portid_t member_pid)
4104 : : {
4105 : : struct rte_port *port;
4106 : : struct rte_eth_dev_info dev_info;
4107 : : int ret;
4108 : :
4109 : 0 : port = &ports[member_pid];
4110 : 0 : ret = eth_dev_info_get_print_err(member_pid, &dev_info);
4111 : 0 : if (ret != 0) {
4112 : 0 : TESTPMD_LOG(ERR,
4113 : : "Failed to get device info for port id %d,"
4114 : : "cannot determine if the port is a bonding member",
4115 : : member_pid);
4116 : 0 : return 0;
4117 : : }
4118 : :
4119 : 0 : if ((*dev_info.dev_flags & RTE_ETH_DEV_BONDING_MEMBER) || (port->member_flag == 1))
4120 : 0 : return 1;
4121 : : return 0;
4122 : : }
4123 : :
4124 : : const uint16_t vlan_tags[] = {
4125 : : 0, 1, 2, 3, 4, 5, 6, 7,
4126 : : 8, 9, 10, 11, 12, 13, 14, 15,
4127 : : 16, 17, 18, 19, 20, 21, 22, 23,
4128 : : 24, 25, 26, 27, 28, 29, 30, 31
4129 : : };
4130 : :
4131 : : static void
4132 : 0 : get_eth_dcb_conf(struct rte_eth_conf *eth_conf, enum dcb_mode_enable dcb_mode,
4133 : : enum rte_eth_nb_tcs num_tcs, uint8_t pfc_en)
4134 : : {
4135 : : uint8_t i;
4136 : :
4137 : : /*
4138 : : * Builds up the correct configuration for dcb+vt based on the vlan tags array
4139 : : * given above, and the number of traffic classes available for use.
4140 : : */
4141 : 0 : if (dcb_mode == DCB_VT_ENABLED) {
4142 : : struct rte_eth_vmdq_dcb_conf *vmdq_rx_conf =
4143 : : ð_conf->rx_adv_conf.vmdq_dcb_conf;
4144 : : struct rte_eth_vmdq_dcb_tx_conf *vmdq_tx_conf =
4145 : : ð_conf->tx_adv_conf.vmdq_dcb_tx_conf;
4146 : :
4147 : : /* VMDQ+DCB RX and TX configurations */
4148 : 0 : vmdq_rx_conf->enable_default_pool = 0;
4149 : 0 : vmdq_rx_conf->default_pool = 0;
4150 : 0 : vmdq_rx_conf->nb_queue_pools =
4151 : 0 : (num_tcs == RTE_ETH_4_TCS ? RTE_ETH_32_POOLS : RTE_ETH_16_POOLS);
4152 : 0 : vmdq_tx_conf->nb_queue_pools =
4153 : : (num_tcs == RTE_ETH_4_TCS ? RTE_ETH_32_POOLS : RTE_ETH_16_POOLS);
4154 : :
4155 : 0 : vmdq_rx_conf->nb_pool_maps = vmdq_rx_conf->nb_queue_pools;
4156 : 0 : for (i = 0; i < vmdq_rx_conf->nb_pool_maps; i++) {
4157 : 0 : vmdq_rx_conf->pool_map[i].vlan_id = vlan_tags[i];
4158 : 0 : vmdq_rx_conf->pool_map[i].pools =
4159 : 0 : 1 << (i % vmdq_rx_conf->nb_queue_pools);
4160 : : }
4161 : 0 : for (i = 0; i < RTE_ETH_DCB_NUM_USER_PRIORITIES; i++) {
4162 : 0 : vmdq_rx_conf->dcb_tc[i] = i % num_tcs;
4163 : 0 : vmdq_tx_conf->dcb_tc[i] = i % num_tcs;
4164 : : }
4165 : :
4166 : : /* set DCB mode of RX and TX of multiple queues */
4167 : 0 : eth_conf->rxmode.mq_mode =
4168 : 0 : (enum rte_eth_rx_mq_mode)
4169 : 0 : (rx_mq_mode & RTE_ETH_MQ_RX_VMDQ_DCB);
4170 : 0 : eth_conf->txmode.mq_mode = RTE_ETH_MQ_TX_VMDQ_DCB;
4171 : : } else {
4172 : : struct rte_eth_dcb_rx_conf *rx_conf =
4173 : : ð_conf->rx_adv_conf.dcb_rx_conf;
4174 : : struct rte_eth_dcb_tx_conf *tx_conf =
4175 : : ð_conf->tx_adv_conf.dcb_tx_conf;
4176 : :
4177 : 0 : rx_conf->nb_tcs = num_tcs;
4178 : 0 : tx_conf->nb_tcs = num_tcs;
4179 : :
4180 : 0 : for (i = 0; i < RTE_ETH_DCB_NUM_USER_PRIORITIES; i++) {
4181 : 0 : rx_conf->dcb_tc[i] = i % num_tcs;
4182 : 0 : tx_conf->dcb_tc[i] = i % num_tcs;
4183 : : }
4184 : :
4185 : 0 : eth_conf->rxmode.mq_mode =
4186 : 0 : (enum rte_eth_rx_mq_mode)
4187 : 0 : (rx_mq_mode & RTE_ETH_MQ_RX_DCB_RSS);
4188 : 0 : eth_conf->txmode.mq_mode = RTE_ETH_MQ_TX_DCB;
4189 : : }
4190 : :
4191 : 0 : if (pfc_en)
4192 : 0 : eth_conf->dcb_capability_en =
4193 : : RTE_ETH_DCB_PG_SUPPORT | RTE_ETH_DCB_PFC_SUPPORT;
4194 : : else
4195 : 0 : eth_conf->dcb_capability_en = RTE_ETH_DCB_PG_SUPPORT;
4196 : 0 : }
4197 : :
4198 : : int
4199 : 0 : init_port_dcb_config(portid_t pid,
4200 : : enum dcb_mode_enable dcb_mode,
4201 : : enum rte_eth_nb_tcs num_tcs,
4202 : : uint8_t pfc_en)
4203 : : {
4204 : : struct rte_eth_conf port_conf;
4205 : : struct rte_port *rte_port;
4206 : : int retval;
4207 : : uint16_t i;
4208 : :
4209 : 0 : if (num_procs > 1) {
4210 : : printf("The multi-process feature doesn't support dcb.\n");
4211 : 0 : return -ENOTSUP;
4212 : : }
4213 : 0 : rte_port = &ports[pid];
4214 : :
4215 : : /* retain the original device configuration. */
4216 : 0 : memcpy(&port_conf, &rte_port->dev_conf, sizeof(struct rte_eth_conf));
4217 : :
4218 : : /* set configuration of DCB in vt mode and DCB in non-vt mode */
4219 : 0 : get_eth_dcb_conf(&port_conf, dcb_mode, num_tcs, pfc_en);
4220 : :
4221 : 0 : port_conf.rxmode.offloads |= RTE_ETH_RX_OFFLOAD_VLAN_FILTER;
4222 : : /* remove RSS HASH offload for DCB in vt mode */
4223 : 0 : if (port_conf.rxmode.mq_mode == RTE_ETH_MQ_RX_VMDQ_DCB) {
4224 : 0 : port_conf.rxmode.offloads &= ~RTE_ETH_RX_OFFLOAD_RSS_HASH;
4225 : 0 : for (i = 0; i < nb_rxq; i++)
4226 : 0 : rte_port->rxq[i].conf.offloads &=
4227 : : ~RTE_ETH_RX_OFFLOAD_RSS_HASH;
4228 : : }
4229 : :
4230 : : /* re-configure the device . */
4231 : 0 : retval = rte_eth_dev_configure(pid, nb_rxq, nb_rxq, &port_conf);
4232 : 0 : if (retval < 0)
4233 : : return retval;
4234 : :
4235 : 0 : retval = eth_dev_info_get_print_err(pid, &rte_port->dev_info);
4236 : 0 : if (retval != 0)
4237 : : return retval;
4238 : :
4239 : : /* If dev_info.vmdq_pool_base is greater than 0,
4240 : : * the queue id of vmdq pools is started after pf queues.
4241 : : */
4242 : 0 : if (dcb_mode == DCB_VT_ENABLED &&
4243 : 0 : rte_port->dev_info.vmdq_pool_base > 0) {
4244 : 0 : fprintf(stderr,
4245 : : "VMDQ_DCB multi-queue mode is nonsensical for port %d.\n",
4246 : : pid);
4247 : 0 : return -1;
4248 : : }
4249 : :
4250 : : /* Assume the ports in testpmd have the same dcb capability
4251 : : * and has the same number of rxq and txq in dcb mode
4252 : : */
4253 : 0 : if (dcb_mode == DCB_VT_ENABLED) {
4254 : 0 : if (rte_port->dev_info.max_vfs > 0) {
4255 : 0 : nb_rxq = rte_port->dev_info.nb_rx_queues;
4256 : 0 : nb_txq = rte_port->dev_info.nb_tx_queues;
4257 : : } else {
4258 : 0 : nb_rxq = rte_port->dev_info.max_rx_queues;
4259 : 0 : nb_txq = rte_port->dev_info.max_tx_queues;
4260 : : }
4261 : : } else {
4262 : : /*if vt is disabled, use all pf queues */
4263 : 0 : if (rte_port->dev_info.vmdq_pool_base == 0) {
4264 : 0 : nb_rxq = rte_port->dev_info.max_rx_queues;
4265 : 0 : nb_txq = rte_port->dev_info.max_tx_queues;
4266 : : } else {
4267 : 0 : nb_rxq = (queueid_t)num_tcs;
4268 : 0 : nb_txq = (queueid_t)num_tcs;
4269 : :
4270 : : }
4271 : : }
4272 : 0 : rx_free_thresh = 64;
4273 : :
4274 : : memcpy(&rte_port->dev_conf, &port_conf, sizeof(struct rte_eth_conf));
4275 : :
4276 : 0 : rxtx_port_config(pid);
4277 : : /* VLAN filter */
4278 : 0 : rte_port->dev_conf.rxmode.offloads |= RTE_ETH_RX_OFFLOAD_VLAN_FILTER;
4279 : 0 : for (i = 0; i < RTE_DIM(vlan_tags); i++)
4280 : 0 : rx_vft_set(pid, vlan_tags[i], 1);
4281 : :
4282 : 0 : retval = eth_macaddr_get_print_err(pid, &rte_port->eth_addr);
4283 : 0 : if (retval != 0)
4284 : : return retval;
4285 : :
4286 : 0 : rte_port->dcb_flag = 1;
4287 : :
4288 : : /* Enter DCB configuration status */
4289 : 0 : dcb_config = 1;
4290 : :
4291 : 0 : return 0;
4292 : : }
4293 : :
4294 : : static void
4295 : 0 : init_port(void)
4296 : : {
4297 : : int i;
4298 : :
4299 : : /* Configuration of Ethernet ports. */
4300 : 0 : ports = rte_zmalloc("testpmd: ports",
4301 : : sizeof(struct rte_port) * RTE_MAX_ETHPORTS,
4302 : : RTE_CACHE_LINE_SIZE);
4303 : 0 : if (ports == NULL) {
4304 : 0 : rte_exit(EXIT_FAILURE,
4305 : : "rte_zmalloc(%d struct rte_port) failed\n",
4306 : : RTE_MAX_ETHPORTS);
4307 : : }
4308 : 0 : for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
4309 : 0 : ports[i].fwd_mac_swap = 1;
4310 : 0 : ports[i].xstats_info.allocated = false;
4311 : 0 : LIST_INIT(&ports[i].flow_tunnel_list);
4312 : : }
4313 : : /* Initialize ports NUMA structures */
4314 : : memset(port_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
4315 : : memset(rxring_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
4316 : : memset(txring_numa, NUMA_NO_CONFIG, RTE_MAX_ETHPORTS);
4317 : 0 : }
4318 : :
4319 : : static void
4320 : 0 : print_stats(void)
4321 : : {
4322 : : uint8_t i;
4323 : 0 : const char clr[] = { 27, '[', '2', 'J', '\0' };
4324 : 0 : const char top_left[] = { 27, '[', '1', ';', '1', 'H', '\0' };
4325 : :
4326 : : /* Clear screen and move to top left */
4327 : : printf("%s%s", clr, top_left);
4328 : :
4329 : : printf("\nPort statistics ====================================");
4330 : 0 : for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++)
4331 : 0 : nic_stats_display(fwd_ports_ids[i]);
4332 : :
4333 : 0 : fflush(stdout);
4334 : 0 : }
4335 : :
4336 : : static void
4337 : 0 : signal_handler(int signum __rte_unused)
4338 : : {
4339 : 0 : f_quit = 1;
4340 : 0 : prompt_exit();
4341 : 0 : }
4342 : :
4343 : : int
4344 : 0 : main(int argc, char** argv)
4345 : : {
4346 : : int diag;
4347 : : portid_t port_id;
4348 : : uint16_t count;
4349 : : int ret;
4350 : :
4351 : : #ifdef RTE_EXEC_ENV_WINDOWS
4352 : : signal(SIGINT, signal_handler);
4353 : : signal(SIGTERM, signal_handler);
4354 : : #else
4355 : : /* Want read() not to be restarted on signal */
4356 : 0 : struct sigaction action = {
4357 : : .sa_handler = signal_handler,
4358 : : };
4359 : :
4360 : 0 : sigaction(SIGINT, &action, NULL);
4361 : 0 : sigaction(SIGTERM, &action, NULL);
4362 : : #endif
4363 : :
4364 : 0 : testpmd_logtype = rte_log_register("testpmd");
4365 : 0 : if (testpmd_logtype < 0)
4366 : 0 : rte_exit(EXIT_FAILURE, "Cannot register log type");
4367 : 0 : rte_log_set_level(testpmd_logtype, RTE_LOG_DEBUG);
4368 : :
4369 : 0 : diag = rte_eal_init(argc, argv);
4370 : 0 : if (diag < 0)
4371 : 0 : rte_exit(EXIT_FAILURE, "Cannot init EAL: %s\n",
4372 : : rte_strerror(rte_errno));
4373 : :
4374 : : /* allocate port structures, and init them */
4375 : 0 : init_port();
4376 : :
4377 : 0 : ret = register_eth_event_callback();
4378 : 0 : if (ret != 0)
4379 : 0 : rte_exit(EXIT_FAILURE, "Cannot register for ethdev events");
4380 : :
4381 : : #ifdef RTE_LIB_PDUMP
4382 : : /* initialize packet capture framework */
4383 : 0 : rte_pdump_init();
4384 : : #endif
4385 : :
4386 : : count = 0;
4387 : 0 : RTE_ETH_FOREACH_DEV(port_id) {
4388 : 0 : ports_ids[count] = port_id;
4389 : 0 : count++;
4390 : : }
4391 : 0 : nb_ports = (portid_t) count;
4392 : 0 : if (nb_ports == 0)
4393 : 0 : TESTPMD_LOG(WARNING, "No probed ethernet devices\n");
4394 : :
4395 : 0 : set_def_fwd_config();
4396 : 0 : if (nb_lcores == 0)
4397 : 0 : rte_exit(EXIT_FAILURE, "No cores defined for forwarding\n"
4398 : : "Check the core mask argument\n");
4399 : :
4400 : : /* Bitrate/latency stats disabled by default */
4401 : : #ifdef RTE_LIB_BITRATESTATS
4402 : 0 : bitrate_enabled = 0;
4403 : : #endif
4404 : : #ifdef RTE_LIB_LATENCYSTATS
4405 : 0 : latencystats_enabled = 0;
4406 : : #endif
4407 : :
4408 : : /* on FreeBSD, mlockall() is disabled by default */
4409 : : #ifdef RTE_EXEC_ENV_FREEBSD
4410 : : do_mlockall = 0;
4411 : : #else
4412 : 0 : do_mlockall = 1;
4413 : : #endif
4414 : :
4415 : 0 : argc -= diag;
4416 : 0 : argv += diag;
4417 : 0 : if (argc > 1)
4418 : 0 : launch_args_parse(argc, argv);
4419 : :
4420 : : #ifndef RTE_EXEC_ENV_WINDOWS
4421 : 0 : if (do_mlockall && mlockall(MCL_CURRENT | MCL_FUTURE)) {
4422 : 0 : TESTPMD_LOG(NOTICE, "mlockall() failed with error \"%s\"\n",
4423 : : strerror(errno));
4424 : : }
4425 : : #endif
4426 : :
4427 : 0 : if (tx_first && interactive)
4428 : 0 : rte_exit(EXIT_FAILURE, "--tx-first cannot be used on "
4429 : : "interactive mode.\n");
4430 : :
4431 : 0 : if (tx_first && lsc_interrupt) {
4432 : 0 : fprintf(stderr,
4433 : : "Warning: lsc_interrupt needs to be off when using tx_first. Disabling.\n");
4434 : 0 : lsc_interrupt = 0;
4435 : : }
4436 : :
4437 : 0 : if (!nb_rxq && !nb_txq)
4438 : 0 : rte_exit(EXIT_FAILURE, "Either rx or tx queues should be non-zero\n");
4439 : :
4440 : 0 : if (nb_rxq > 1 && nb_rxq > nb_txq)
4441 : 0 : fprintf(stderr,
4442 : : "Warning: nb_rxq=%d enables RSS configuration, but nb_txq=%d will prevent to fully test it.\n",
4443 : : nb_rxq, nb_txq);
4444 : :
4445 : 0 : init_config();
4446 : :
4447 : 0 : if (hot_plug) {
4448 : 0 : ret = rte_dev_hotplug_handle_enable();
4449 : 0 : if (ret) {
4450 : 0 : TESTPMD_LOG(ERR, "fail to enable hotplug handling.");
4451 : 0 : return -1;
4452 : : }
4453 : :
4454 : 0 : ret = rte_dev_event_monitor_start();
4455 : 0 : if (ret) {
4456 : 0 : TESTPMD_LOG(ERR, "fail to start device event monitoring.");
4457 : 0 : return -1;
4458 : : }
4459 : :
4460 : 0 : ret = rte_dev_event_callback_register(NULL, dev_event_callback, NULL);
4461 : 0 : if (ret) {
4462 : 0 : TESTPMD_LOG(ERR, "fail to register device event callback\n");
4463 : 0 : return -1;
4464 : : }
4465 : : }
4466 : :
4467 : 0 : if (!no_device_start && start_port(RTE_PORT_ALL) != 0) {
4468 : 0 : if (!interactive) {
4469 : 0 : rte_eal_cleanup();
4470 : 0 : rte_exit(EXIT_FAILURE, "Start ports failed\n");
4471 : : }
4472 : 0 : fprintf(stderr, "Start ports failed\n");
4473 : : }
4474 : :
4475 : : /* set all ports to promiscuous mode by default */
4476 : 0 : RTE_ETH_FOREACH_DEV(port_id) {
4477 : 0 : ret = rte_eth_promiscuous_enable(port_id);
4478 : 0 : if (ret != 0)
4479 : 0 : fprintf(stderr,
4480 : : "Error during enabling promiscuous mode for port %u: %s - ignore\n",
4481 : : port_id, rte_strerror(-ret));
4482 : : }
4483 : :
4484 : : #ifdef RTE_LIB_METRICS
4485 : : /* Init metrics library */
4486 : 0 : rte_metrics_init(rte_socket_id());
4487 : : #endif
4488 : :
4489 : : #ifdef RTE_LIB_LATENCYSTATS
4490 : 0 : if (latencystats_enabled != 0) {
4491 : 0 : int ret = rte_latencystats_init(1, NULL);
4492 : 0 : if (ret)
4493 : 0 : fprintf(stderr,
4494 : : "Warning: latencystats init() returned error %d\n",
4495 : : ret);
4496 : 0 : fprintf(stderr, "Latencystats running on lcore %d\n",
4497 : : latencystats_lcore_id);
4498 : : }
4499 : : #endif
4500 : :
4501 : : /* Setup bitrate stats */
4502 : : #ifdef RTE_LIB_BITRATESTATS
4503 : 0 : if (bitrate_enabled != 0) {
4504 : 0 : bitrate_data = rte_stats_bitrate_create();
4505 : 0 : if (bitrate_data == NULL)
4506 : 0 : rte_exit(EXIT_FAILURE,
4507 : : "Could not allocate bitrate data.\n");
4508 : 0 : rte_stats_bitrate_reg(bitrate_data);
4509 : : }
4510 : : #endif
4511 : :
4512 : 0 : if (record_core_cycles)
4513 : 0 : rte_lcore_register_usage_cb(lcore_usage_callback);
4514 : :
4515 : 0 : if (init_cmdline() != 0)
4516 : 0 : rte_exit(EXIT_FAILURE,
4517 : : "Could not initialise cmdline context.\n");
4518 : :
4519 : 0 : if (strlen(cmdline_filename) != 0)
4520 : 0 : cmdline_read_from_file(cmdline_filename);
4521 : :
4522 : 0 : if (interactive == 1) {
4523 : 0 : if (auto_start) {
4524 : : printf("Start automatic packet forwarding\n");
4525 : 0 : start_packet_forwarding(0);
4526 : : }
4527 : 0 : prompt();
4528 : : } else {
4529 : : printf("No commandline core given, start packet forwarding\n");
4530 : 0 : start_packet_forwarding(tx_first);
4531 : 0 : if (stats_period != 0) {
4532 : : uint64_t prev_time = 0, cur_time, diff_time = 0;
4533 : : uint64_t timer_period;
4534 : :
4535 : : /* Convert to number of cycles */
4536 : 0 : timer_period = stats_period * rte_get_timer_hz();
4537 : :
4538 : 0 : while (f_quit == 0) {
4539 : : cur_time = rte_get_timer_cycles();
4540 : 0 : diff_time += cur_time - prev_time;
4541 : :
4542 : 0 : if (diff_time >= timer_period) {
4543 : 0 : print_stats();
4544 : : /* Reset the timer */
4545 : : diff_time = 0;
4546 : : }
4547 : : /* Sleep to avoid unnecessary checks */
4548 : : prev_time = cur_time;
4549 : 0 : rte_delay_us_sleep(US_PER_S);
4550 : : }
4551 : : } else {
4552 : : char c;
4553 : :
4554 : : printf("Press enter to exit\n");
4555 : 0 : while (f_quit == 0) {
4556 : : /* end-of-file or any character exits loop */
4557 : 0 : if (read(0, &c, 1) >= 0)
4558 : : break;
4559 : 0 : if (errno == EINTR)
4560 : 0 : continue;
4561 : 0 : rte_exit(EXIT_FAILURE, "Read failed: %s\n",
4562 : : strerror(errno));
4563 : : }
4564 : : }
4565 : : }
4566 : :
4567 : 0 : pmd_test_exit();
4568 : :
4569 : : #ifdef RTE_LIB_PDUMP
4570 : : /* uninitialize packet capture framework */
4571 : 0 : rte_pdump_uninit();
4572 : : #endif
4573 : : #ifdef RTE_LIB_LATENCYSTATS
4574 : 0 : if (latencystats_enabled != 0)
4575 : 0 : rte_latencystats_uninit();
4576 : : #endif
4577 : :
4578 : 0 : ret = unregister_eth_event_callback();
4579 : 0 : if (ret != 0)
4580 : 0 : rte_exit(EXIT_FAILURE, "Cannot unregister for ethdev events");
4581 : :
4582 : :
4583 : 0 : ret = rte_eal_cleanup();
4584 : 0 : if (ret != 0)
4585 : 0 : rte_exit(EXIT_FAILURE,
4586 : : "EAL cleanup failed: %s\n", strerror(-ret));
4587 : :
4588 : : return EXIT_SUCCESS;
4589 : : }
|