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