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