LCOV - code coverage report
Current view: top level - app/test-pmd - testpmd.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 1660 0.0 %
Date: 2025-03-01 20:23:48 Functions: 0 93 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

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

Generated by: LCOV version 1.14