LCOV - code coverage report
Current view: top level - app/test-pmd - config.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 3015 0.0 %
Date: 2025-01-02 22:41:34 Functions: 0 199 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-2016 Intel Corporation.
       3                 :            :  * Copyright 2013-2014 6WIND S.A.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include <ctype.h>
       7                 :            : #include <stdarg.h>
       8                 :            : #include <errno.h>
       9                 :            : #include <stdbool.h>
      10                 :            : #include <stdio.h>
      11                 :            : #include <stdlib.h>
      12                 :            : #include <string.h>
      13                 :            : #include <stdint.h>
      14                 :            : #include <inttypes.h>
      15                 :            : 
      16                 :            : #include <sys/queue.h>
      17                 :            : #include <sys/types.h>
      18                 :            : #include <sys/stat.h>
      19                 :            : #include <fcntl.h>
      20                 :            : #include <unistd.h>
      21                 :            : 
      22                 :            : #include <rte_common.h>
      23                 :            : #include <rte_byteorder.h>
      24                 :            : #include <rte_debug.h>
      25                 :            : #include <rte_log.h>
      26                 :            : #include <rte_memory.h>
      27                 :            : #include <rte_memcpy.h>
      28                 :            : #include <rte_memzone.h>
      29                 :            : #include <rte_launch.h>
      30                 :            : #include <rte_bus.h>
      31                 :            : #include <rte_eal.h>
      32                 :            : #include <rte_per_lcore.h>
      33                 :            : #include <rte_lcore.h>
      34                 :            : #include <rte_branch_prediction.h>
      35                 :            : #include <rte_mempool.h>
      36                 :            : #include <rte_mbuf.h>
      37                 :            : #include <rte_interrupts.h>
      38                 :            : #include <rte_ether.h>
      39                 :            : #include <rte_ethdev.h>
      40                 :            : #include <rte_string_fns.h>
      41                 :            : #include <rte_cycles.h>
      42                 :            : #include <rte_flow.h>
      43                 :            : #include <rte_mtr.h>
      44                 :            : #include <rte_errno.h>
      45                 :            : #ifdef RTE_NET_IXGBE
      46                 :            : #include <rte_pmd_ixgbe.h>
      47                 :            : #endif
      48                 :            : #ifdef RTE_NET_I40E
      49                 :            : #include <rte_pmd_i40e.h>
      50                 :            : #endif
      51                 :            : #ifdef RTE_NET_BNXT
      52                 :            : #include <rte_pmd_bnxt.h>
      53                 :            : #endif
      54                 :            : #ifdef RTE_LIB_GRO
      55                 :            : #include <rte_gro.h>
      56                 :            : #endif
      57                 :            : #include <rte_hexdump.h>
      58                 :            : 
      59                 :            : #include "testpmd.h"
      60                 :            : #include "cmdline_mtr.h"
      61                 :            : 
      62                 :            : #define ETHDEV_FWVERS_LEN 32
      63                 :            : 
      64                 :            : #ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */
      65                 :            : #define CLOCK_TYPE_ID CLOCK_MONOTONIC_RAW
      66                 :            : #else
      67                 :            : #define CLOCK_TYPE_ID CLOCK_MONOTONIC
      68                 :            : #endif
      69                 :            : 
      70                 :            : #define NS_PER_SEC 1E9
      71                 :            : 
      72                 :            : static const struct {
      73                 :            :         enum tx_pkt_split split;
      74                 :            :         const char *name;
      75                 :            : } tx_split_name[] = {
      76                 :            :         {
      77                 :            :                 .split = TX_PKT_SPLIT_OFF,
      78                 :            :                 .name = "off",
      79                 :            :         },
      80                 :            :         {
      81                 :            :                 .split = TX_PKT_SPLIT_ON,
      82                 :            :                 .name = "on",
      83                 :            :         },
      84                 :            :         {
      85                 :            :                 .split = TX_PKT_SPLIT_RND,
      86                 :            :                 .name = "rand",
      87                 :            :         },
      88                 :            : };
      89                 :            : 
      90                 :            : const struct rss_type_info rss_type_table[] = {
      91                 :            :         /* Group types */
      92                 :            :         { "all", RTE_ETH_RSS_ETH | RTE_ETH_RSS_VLAN | RTE_ETH_RSS_IP | RTE_ETH_RSS_TCP |
      93                 :            :                 RTE_ETH_RSS_UDP | RTE_ETH_RSS_SCTP | RTE_ETH_RSS_L2_PAYLOAD |
      94                 :            :                 RTE_ETH_RSS_L2TPV3 | RTE_ETH_RSS_ESP | RTE_ETH_RSS_AH | RTE_ETH_RSS_PFCP |
      95                 :            :                 RTE_ETH_RSS_GTPU | RTE_ETH_RSS_ECPRI | RTE_ETH_RSS_MPLS | RTE_ETH_RSS_L2TPV2},
      96                 :            :         { "none", 0 },
      97                 :            :         { "ip", RTE_ETH_RSS_IP },
      98                 :            :         { "udp", RTE_ETH_RSS_UDP },
      99                 :            :         { "tcp", RTE_ETH_RSS_TCP },
     100                 :            :         { "sctp", RTE_ETH_RSS_SCTP },
     101                 :            :         { "tunnel", RTE_ETH_RSS_TUNNEL },
     102                 :            :         { "vlan", RTE_ETH_RSS_VLAN },
     103                 :            : 
     104                 :            :         /* Individual type */
     105                 :            :         { "ipv4", RTE_ETH_RSS_IPV4 },
     106                 :            :         { "ipv4-frag", RTE_ETH_RSS_FRAG_IPV4 },
     107                 :            :         { "ipv4-tcp", RTE_ETH_RSS_NONFRAG_IPV4_TCP },
     108                 :            :         { "ipv4-udp", RTE_ETH_RSS_NONFRAG_IPV4_UDP },
     109                 :            :         { "ipv4-sctp", RTE_ETH_RSS_NONFRAG_IPV4_SCTP },
     110                 :            :         { "ipv4-other", RTE_ETH_RSS_NONFRAG_IPV4_OTHER },
     111                 :            :         { "ipv6", RTE_ETH_RSS_IPV6 },
     112                 :            :         { "ipv6-frag", RTE_ETH_RSS_FRAG_IPV6 },
     113                 :            :         { "ipv6-tcp", RTE_ETH_RSS_NONFRAG_IPV6_TCP },
     114                 :            :         { "ipv6-udp", RTE_ETH_RSS_NONFRAG_IPV6_UDP },
     115                 :            :         { "ipv6-sctp", RTE_ETH_RSS_NONFRAG_IPV6_SCTP },
     116                 :            :         { "ipv6-other", RTE_ETH_RSS_NONFRAG_IPV6_OTHER },
     117                 :            :         { "l2-payload", RTE_ETH_RSS_L2_PAYLOAD },
     118                 :            :         { "ipv6-ex", RTE_ETH_RSS_IPV6_EX },
     119                 :            :         { "ipv6-tcp-ex", RTE_ETH_RSS_IPV6_TCP_EX },
     120                 :            :         { "ipv6-udp-ex", RTE_ETH_RSS_IPV6_UDP_EX },
     121                 :            :         { "port", RTE_ETH_RSS_PORT },
     122                 :            :         { "vxlan", RTE_ETH_RSS_VXLAN },
     123                 :            :         { "geneve", RTE_ETH_RSS_GENEVE },
     124                 :            :         { "nvgre", RTE_ETH_RSS_NVGRE },
     125                 :            :         { "gtpu", RTE_ETH_RSS_GTPU },
     126                 :            :         { "eth", RTE_ETH_RSS_ETH },
     127                 :            :         { "s-vlan", RTE_ETH_RSS_S_VLAN },
     128                 :            :         { "c-vlan", RTE_ETH_RSS_C_VLAN },
     129                 :            :         { "esp", RTE_ETH_RSS_ESP },
     130                 :            :         { "ah", RTE_ETH_RSS_AH },
     131                 :            :         { "l2tpv3", RTE_ETH_RSS_L2TPV3 },
     132                 :            :         { "pfcp", RTE_ETH_RSS_PFCP },
     133                 :            :         { "pppoe", RTE_ETH_RSS_PPPOE },
     134                 :            :         { "ecpri", RTE_ETH_RSS_ECPRI },
     135                 :            :         { "mpls", RTE_ETH_RSS_MPLS },
     136                 :            :         { "ipv4-chksum", RTE_ETH_RSS_IPV4_CHKSUM },
     137                 :            :         { "l4-chksum", RTE_ETH_RSS_L4_CHKSUM },
     138                 :            :         { "l2tpv2", RTE_ETH_RSS_L2TPV2 },
     139                 :            :         { "l3-pre96", RTE_ETH_RSS_L3_PRE96 },
     140                 :            :         { "l3-pre64", RTE_ETH_RSS_L3_PRE64 },
     141                 :            :         { "l3-pre56", RTE_ETH_RSS_L3_PRE56 },
     142                 :            :         { "l3-pre48", RTE_ETH_RSS_L3_PRE48 },
     143                 :            :         { "l3-pre40", RTE_ETH_RSS_L3_PRE40 },
     144                 :            :         { "l3-pre32", RTE_ETH_RSS_L3_PRE32 },
     145                 :            :         { "l2-dst-only", RTE_ETH_RSS_L2_DST_ONLY },
     146                 :            :         { "l2-src-only", RTE_ETH_RSS_L2_SRC_ONLY },
     147                 :            :         { "l4-dst-only", RTE_ETH_RSS_L4_DST_ONLY },
     148                 :            :         { "l4-src-only", RTE_ETH_RSS_L4_SRC_ONLY },
     149                 :            :         { "l3-dst-only", RTE_ETH_RSS_L3_DST_ONLY },
     150                 :            :         { "l3-src-only", RTE_ETH_RSS_L3_SRC_ONLY },
     151                 :            :         { "ipv6-flow-label", RTE_ETH_RSS_IPV6_FLOW_LABEL },
     152                 :            :         { NULL, 0},
     153                 :            : };
     154                 :            : 
     155                 :            : static const struct {
     156                 :            :         enum rte_eth_fec_mode mode;
     157                 :            :         const char *name;
     158                 :            : } fec_mode_name[] = {
     159                 :            :         {
     160                 :            :                 .mode = RTE_ETH_FEC_NOFEC,
     161                 :            :                 .name = "off",
     162                 :            :         },
     163                 :            :         {
     164                 :            :                 .mode = RTE_ETH_FEC_AUTO,
     165                 :            :                 .name = "auto",
     166                 :            :         },
     167                 :            :         {
     168                 :            :                 .mode = RTE_ETH_FEC_BASER,
     169                 :            :                 .name = "baser",
     170                 :            :         },
     171                 :            :         {
     172                 :            :                 .mode = RTE_ETH_FEC_RS,
     173                 :            :                 .name = "rs",
     174                 :            :         },
     175                 :            :         {
     176                 :            :                 .mode = RTE_ETH_FEC_LLRS,
     177                 :            :                 .name = "llrs",
     178                 :            :         },
     179                 :            : };
     180                 :            : 
     181                 :            : static const struct {
     182                 :            :         char str[32];
     183                 :            :         uint16_t ftype;
     184                 :            : } flowtype_str_table[] = {
     185                 :            :         {"raw", RTE_ETH_FLOW_RAW},
     186                 :            :         {"ipv4", RTE_ETH_FLOW_IPV4},
     187                 :            :         {"ipv4-frag", RTE_ETH_FLOW_FRAG_IPV4},
     188                 :            :         {"ipv4-tcp", RTE_ETH_FLOW_NONFRAG_IPV4_TCP},
     189                 :            :         {"ipv4-udp", RTE_ETH_FLOW_NONFRAG_IPV4_UDP},
     190                 :            :         {"ipv4-sctp", RTE_ETH_FLOW_NONFRAG_IPV4_SCTP},
     191                 :            :         {"ipv4-other", RTE_ETH_FLOW_NONFRAG_IPV4_OTHER},
     192                 :            :         {"ipv6", RTE_ETH_FLOW_IPV6},
     193                 :            :         {"ipv6-frag", RTE_ETH_FLOW_FRAG_IPV6},
     194                 :            :         {"ipv6-tcp", RTE_ETH_FLOW_NONFRAG_IPV6_TCP},
     195                 :            :         {"ipv6-udp", RTE_ETH_FLOW_NONFRAG_IPV6_UDP},
     196                 :            :         {"ipv6-sctp", RTE_ETH_FLOW_NONFRAG_IPV6_SCTP},
     197                 :            :         {"ipv6-other", RTE_ETH_FLOW_NONFRAG_IPV6_OTHER},
     198                 :            :         {"l2_payload", RTE_ETH_FLOW_L2_PAYLOAD},
     199                 :            :         {"ipv6-ex", RTE_ETH_FLOW_IPV6_EX},
     200                 :            :         {"ipv6-tcp-ex", RTE_ETH_FLOW_IPV6_TCP_EX},
     201                 :            :         {"ipv6-udp-ex", RTE_ETH_FLOW_IPV6_UDP_EX},
     202                 :            :         {"port", RTE_ETH_FLOW_PORT},
     203                 :            :         {"vxlan", RTE_ETH_FLOW_VXLAN},
     204                 :            :         {"geneve", RTE_ETH_FLOW_GENEVE},
     205                 :            :         {"nvgre", RTE_ETH_FLOW_NVGRE},
     206                 :            :         {"vxlan-gpe", RTE_ETH_FLOW_VXLAN_GPE},
     207                 :            :         {"gtpu", RTE_ETH_FLOW_GTPU},
     208                 :            : };
     209                 :            : 
     210                 :            : static void
     211                 :          0 : print_ethaddr(const char *name, struct rte_ether_addr *eth_addr)
     212                 :            : {
     213                 :            :         char buf[RTE_ETHER_ADDR_FMT_SIZE];
     214                 :          0 :         rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
     215                 :            :         printf("%s%s", name, buf);
     216                 :          0 : }
     217                 :            : 
     218                 :            : static void
     219                 :          0 : nic_xstats_display_periodic(portid_t port_id)
     220                 :            : {
     221                 :            :         struct xstat_display_info *xstats_info;
     222                 :            :         uint64_t *prev_values, *curr_values;
     223                 :            :         uint64_t diff_value, value_rate;
     224                 :            :         struct timespec cur_time;
     225                 :            :         uint64_t *ids_supp;
     226                 :            :         size_t ids_supp_sz;
     227                 :            :         uint64_t diff_ns;
     228                 :            :         unsigned int i;
     229                 :            :         int rc;
     230                 :            : 
     231                 :          0 :         xstats_info = &ports[port_id].xstats_info;
     232                 :            : 
     233                 :          0 :         ids_supp_sz = xstats_info->ids_supp_sz;
     234                 :          0 :         if (ids_supp_sz == 0)
     235                 :          0 :                 return;
     236                 :            : 
     237                 :            :         printf("\n");
     238                 :            : 
     239                 :          0 :         ids_supp = xstats_info->ids_supp;
     240                 :          0 :         prev_values = xstats_info->prev_values;
     241                 :          0 :         curr_values = xstats_info->curr_values;
     242                 :            : 
     243                 :          0 :         rc = rte_eth_xstats_get_by_id(port_id, ids_supp, curr_values,
     244                 :            :                                       ids_supp_sz);
     245                 :          0 :         if (rc != (int)ids_supp_sz) {
     246                 :          0 :                 fprintf(stderr,
     247                 :            :                         "Failed to get values of %zu xstats for port %u - return code %d\n",
     248                 :            :                         ids_supp_sz, port_id, rc);
     249                 :          0 :                 return;
     250                 :            :         }
     251                 :            : 
     252                 :            :         diff_ns = 0;
     253                 :          0 :         if (clock_gettime(CLOCK_TYPE_ID, &cur_time) == 0) {
     254                 :            :                 uint64_t ns;
     255                 :            : 
     256                 :          0 :                 ns = cur_time.tv_sec * NS_PER_SEC;
     257                 :          0 :                 ns += cur_time.tv_nsec;
     258                 :            : 
     259                 :          0 :                 if (xstats_info->prev_ns != 0)
     260                 :          0 :                         diff_ns = ns - xstats_info->prev_ns;
     261                 :          0 :                 xstats_info->prev_ns = ns;
     262                 :            :         }
     263                 :            : 
     264                 :            :         printf("%-31s%-17s%s\n", " ", "Value", "Rate (since last show)");
     265                 :          0 :         for (i = 0; i < ids_supp_sz; i++) {
     266                 :          0 :                 diff_value = (curr_values[i] > prev_values[i]) ?
     267                 :          0 :                              (curr_values[i] - prev_values[i]) : 0;
     268                 :          0 :                 prev_values[i] = curr_values[i];
     269                 :          0 :                 value_rate = diff_ns > 0 ?
     270                 :          0 :                                 (double)diff_value / diff_ns * NS_PER_SEC : 0;
     271                 :            : 
     272                 :            :                 printf("  %-25s%12"PRIu64" %15"PRIu64"\n",
     273                 :          0 :                        xstats_display[i].name, curr_values[i], value_rate);
     274                 :            :         }
     275                 :            : }
     276                 :            : 
     277                 :            : void
     278                 :          0 : nic_stats_display(portid_t port_id)
     279                 :            : {
     280                 :            :         static uint64_t prev_pkts_rx[RTE_MAX_ETHPORTS];
     281                 :            :         static uint64_t prev_pkts_tx[RTE_MAX_ETHPORTS];
     282                 :            :         static uint64_t prev_bytes_rx[RTE_MAX_ETHPORTS];
     283                 :            :         static uint64_t prev_bytes_tx[RTE_MAX_ETHPORTS];
     284                 :            :         static uint64_t prev_ns[RTE_MAX_ETHPORTS];
     285                 :            :         struct timespec cur_time;
     286                 :            :         uint64_t diff_pkts_rx, diff_pkts_tx, diff_bytes_rx, diff_bytes_tx,
     287                 :            :                                                                 diff_ns;
     288                 :            :         uint64_t mpps_rx, mpps_tx, mbps_rx, mbps_tx;
     289                 :            :         struct rte_eth_stats stats;
     290                 :            :         static const char *nic_stats_border = "########################";
     291                 :            :         int ret;
     292                 :            : 
     293                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
     294                 :          0 :                 print_valid_ports();
     295                 :          0 :                 return;
     296                 :            :         }
     297                 :          0 :         ret = rte_eth_stats_get(port_id, &stats);
     298                 :          0 :         if (ret != 0) {
     299                 :          0 :                 fprintf(stderr,
     300                 :            :                         "%s: Error: failed to get stats (port %u): %d",
     301                 :            :                         __func__, port_id, ret);
     302                 :          0 :                 return;
     303                 :            :         }
     304                 :          0 :         printf("\n  %s NIC statistics for port %-2d %s\n",
     305                 :            :                nic_stats_border, port_id, nic_stats_border);
     306                 :            : 
     307                 :          0 :         printf("  RX-packets: %-10"PRIu64" RX-missed: %-10"PRIu64" RX-bytes:  "
     308                 :            :                "%-"PRIu64"\n", stats.ipackets, stats.imissed, stats.ibytes);
     309                 :          0 :         printf("  RX-errors: %-"PRIu64"\n", stats.ierrors);
     310                 :          0 :         printf("  RX-nombuf:  %-10"PRIu64"\n", stats.rx_nombuf);
     311                 :          0 :         printf("  TX-packets: %-10"PRIu64" TX-errors: %-10"PRIu64" TX-bytes:  "
     312                 :            :                "%-"PRIu64"\n", stats.opackets, stats.oerrors, stats.obytes);
     313                 :            : 
     314                 :            :         diff_ns = 0;
     315                 :          0 :         if (clock_gettime(CLOCK_TYPE_ID, &cur_time) == 0) {
     316                 :            :                 uint64_t ns;
     317                 :            : 
     318                 :          0 :                 ns = cur_time.tv_sec * NS_PER_SEC;
     319                 :          0 :                 ns += cur_time.tv_nsec;
     320                 :            : 
     321                 :          0 :                 if (prev_ns[port_id] != 0)
     322                 :          0 :                         diff_ns = ns - prev_ns[port_id];
     323                 :          0 :                 prev_ns[port_id] = ns;
     324                 :            :         }
     325                 :            : 
     326                 :          0 :         diff_pkts_rx = (stats.ipackets > prev_pkts_rx[port_id]) ?
     327                 :          0 :                 (stats.ipackets - prev_pkts_rx[port_id]) : 0;
     328                 :          0 :         diff_pkts_tx = (stats.opackets > prev_pkts_tx[port_id]) ?
     329                 :          0 :                 (stats.opackets - prev_pkts_tx[port_id]) : 0;
     330                 :          0 :         prev_pkts_rx[port_id] = stats.ipackets;
     331                 :          0 :         prev_pkts_tx[port_id] = stats.opackets;
     332                 :          0 :         mpps_rx = diff_ns > 0 ?
     333                 :          0 :                 (double)diff_pkts_rx / diff_ns * NS_PER_SEC : 0;
     334                 :          0 :         mpps_tx = diff_ns > 0 ?
     335                 :          0 :                 (double)diff_pkts_tx / diff_ns * NS_PER_SEC : 0;
     336                 :            : 
     337                 :          0 :         diff_bytes_rx = (stats.ibytes > prev_bytes_rx[port_id]) ?
     338                 :          0 :                 (stats.ibytes - prev_bytes_rx[port_id]) : 0;
     339                 :          0 :         diff_bytes_tx = (stats.obytes > prev_bytes_tx[port_id]) ?
     340                 :          0 :                 (stats.obytes - prev_bytes_tx[port_id]) : 0;
     341                 :          0 :         prev_bytes_rx[port_id] = stats.ibytes;
     342                 :          0 :         prev_bytes_tx[port_id] = stats.obytes;
     343                 :          0 :         mbps_rx = diff_ns > 0 ?
     344                 :          0 :                 (double)diff_bytes_rx / diff_ns * NS_PER_SEC : 0;
     345                 :          0 :         mbps_tx = diff_ns > 0 ?
     346                 :          0 :                 (double)diff_bytes_tx / diff_ns * NS_PER_SEC : 0;
     347                 :            : 
     348                 :            :         printf("\n  Throughput (since last show)\n");
     349                 :          0 :         printf("  Rx-pps: %12"PRIu64"          Rx-bps: %12"PRIu64"\n  Tx-pps: %12"
     350                 :            :                PRIu64"          Tx-bps: %12"PRIu64"\n", mpps_rx, mbps_rx * 8,
     351                 :            :                mpps_tx, mbps_tx * 8);
     352                 :            : 
     353                 :          0 :         if (xstats_display_num > 0)
     354                 :          0 :                 nic_xstats_display_periodic(port_id);
     355                 :            : 
     356                 :          0 :         printf("  %s############################%s\n",
     357                 :            :                nic_stats_border, nic_stats_border);
     358                 :            : }
     359                 :            : 
     360                 :            : void
     361                 :          0 : nic_stats_clear(portid_t port_id)
     362                 :            : {
     363                 :            :         int ret;
     364                 :            : 
     365                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
     366                 :          0 :                 print_valid_ports();
     367                 :          0 :                 return;
     368                 :            :         }
     369                 :            : 
     370                 :          0 :         ret = rte_eth_stats_reset(port_id);
     371                 :          0 :         if (ret != 0) {
     372                 :          0 :                 fprintf(stderr,
     373                 :            :                         "%s: Error: failed to reset stats (port %u): %s",
     374                 :            :                         __func__, port_id, strerror(-ret));
     375                 :          0 :                 return;
     376                 :            :         }
     377                 :            : 
     378                 :          0 :         ret = rte_eth_stats_get(port_id, &ports[port_id].stats);
     379                 :          0 :         if (ret != 0) {
     380                 :            :                 if (ret < 0)
     381                 :            :                         ret = -ret;
     382                 :          0 :                 fprintf(stderr,
     383                 :            :                         "%s: Error: failed to get stats (port %u): %s",
     384                 :            :                         __func__, port_id, strerror(ret));
     385                 :          0 :                 return;
     386                 :            :         }
     387                 :            :         printf("\n  NIC statistics for port %d cleared\n", port_id);
     388                 :            : }
     389                 :            : 
     390                 :            : void
     391                 :          0 : nic_xstats_display(portid_t port_id)
     392                 :            : {
     393                 :            :         struct rte_eth_xstat *xstats;
     394                 :            :         int cnt_xstats, idx_xstat;
     395                 :            :         struct rte_eth_xstat_name *xstats_names;
     396                 :            : 
     397                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
     398                 :          0 :                 print_valid_ports();
     399                 :          0 :                 return;
     400                 :            :         }
     401                 :            :         printf("###### NIC extended statistics for port %-2d\n", port_id);
     402                 :          0 :         if (!rte_eth_dev_is_valid_port(port_id)) {
     403                 :          0 :                 fprintf(stderr, "Error: Invalid port number %i\n", port_id);
     404                 :          0 :                 return;
     405                 :            :         }
     406                 :            : 
     407                 :            :         /* Get count */
     408                 :          0 :         cnt_xstats = rte_eth_xstats_get_names(port_id, NULL, 0);
     409                 :          0 :         if (cnt_xstats  < 0) {
     410                 :          0 :                 fprintf(stderr, "Error: Cannot get count of xstats\n");
     411                 :          0 :                 return;
     412                 :            :         }
     413                 :            : 
     414                 :            :         /* Get id-name lookup table */
     415                 :          0 :         xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * cnt_xstats);
     416                 :          0 :         if (xstats_names == NULL) {
     417                 :          0 :                 fprintf(stderr, "Cannot allocate memory for xstats lookup\n");
     418                 :          0 :                 return;
     419                 :            :         }
     420                 :          0 :         if (cnt_xstats != rte_eth_xstats_get_names(
     421                 :            :                         port_id, xstats_names, cnt_xstats)) {
     422                 :          0 :                 fprintf(stderr, "Error: Cannot get xstats lookup\n");
     423                 :          0 :                 free(xstats_names);
     424                 :          0 :                 return;
     425                 :            :         }
     426                 :            : 
     427                 :            :         /* Get stats themselves */
     428                 :          0 :         xstats = malloc(sizeof(struct rte_eth_xstat) * cnt_xstats);
     429                 :          0 :         if (xstats == NULL) {
     430                 :          0 :                 fprintf(stderr, "Cannot allocate memory for xstats\n");
     431                 :          0 :                 free(xstats_names);
     432                 :          0 :                 return;
     433                 :            :         }
     434                 :          0 :         if (cnt_xstats != rte_eth_xstats_get(port_id, xstats, cnt_xstats)) {
     435                 :          0 :                 fprintf(stderr, "Error: Unable to get xstats\n");
     436                 :          0 :                 free(xstats_names);
     437                 :          0 :                 free(xstats);
     438                 :          0 :                 return;
     439                 :            :         }
     440                 :            : 
     441                 :            :         /* Display xstats */
     442                 :          0 :         for (idx_xstat = 0; idx_xstat < cnt_xstats; idx_xstat++) {
     443                 :          0 :                 if (xstats_hide_zero && !xstats[idx_xstat].value)
     444                 :          0 :                         continue;
     445                 :          0 :                 printf("%s: %"PRIu64"\n",
     446                 :          0 :                         xstats_names[idx_xstat].name,
     447                 :          0 :                         xstats[idx_xstat].value);
     448                 :            :         }
     449                 :          0 :         free(xstats_names);
     450                 :          0 :         free(xstats);
     451                 :            : }
     452                 :            : 
     453                 :            : void
     454                 :          0 : nic_xstats_clear(portid_t port_id)
     455                 :            : {
     456                 :            :         int ret;
     457                 :            : 
     458                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
     459                 :          0 :                 print_valid_ports();
     460                 :          0 :                 return;
     461                 :            :         }
     462                 :            : 
     463                 :          0 :         ret = rte_eth_xstats_reset(port_id);
     464                 :          0 :         if (ret != 0) {
     465                 :          0 :                 fprintf(stderr,
     466                 :            :                         "%s: Error: failed to reset xstats (port %u): %s\n",
     467                 :            :                         __func__, port_id, strerror(-ret));
     468                 :          0 :                 return;
     469                 :            :         }
     470                 :            : 
     471                 :          0 :         ret = rte_eth_stats_get(port_id, &ports[port_id].stats);
     472                 :          0 :         if (ret != 0) {
     473                 :            :                 if (ret < 0)
     474                 :            :                         ret = -ret;
     475                 :          0 :                 fprintf(stderr, "%s: Error: failed to get stats (port %u): %s",
     476                 :            :                         __func__, port_id, strerror(ret));
     477                 :          0 :                 return;
     478                 :            :         }
     479                 :            : }
     480                 :            : 
     481                 :            : static const char *
     482                 :            : get_queue_state_name(uint8_t queue_state)
     483                 :            : {
     484                 :          0 :         if (queue_state == RTE_ETH_QUEUE_STATE_STOPPED)
     485                 :            :                 return "stopped";
     486                 :          0 :         else if (queue_state == RTE_ETH_QUEUE_STATE_STARTED)
     487                 :            :                 return "started";
     488                 :          0 :         else if (queue_state == RTE_ETH_QUEUE_STATE_HAIRPIN)
     489                 :            :                 return "hairpin";
     490                 :            :         else
     491                 :          0 :                 return "unknown";
     492                 :            : }
     493                 :            : 
     494                 :            : void
     495                 :          0 : rx_queue_infos_display(portid_t port_id, uint16_t queue_id)
     496                 :            : {
     497                 :            :         struct rte_eth_burst_mode mode;
     498                 :            :         struct rte_eth_rxq_info qinfo;
     499                 :            :         int32_t rc;
     500                 :            :         static const char *info_border = "*********************";
     501                 :            : 
     502                 :          0 :         rc = rte_eth_rx_queue_info_get(port_id, queue_id, &qinfo);
     503                 :          0 :         if (rc != 0) {
     504                 :          0 :                 fprintf(stderr,
     505                 :            :                         "Failed to retrieve information for port: %u, RX queue: %hu\nerror desc: %s(%d)\n",
     506                 :            :                         port_id, queue_id, strerror(-rc), rc);
     507                 :          0 :                 return;
     508                 :            :         }
     509                 :            : 
     510                 :          0 :         printf("\n%s Infos for port %-2u, RX queue %-2u %s",
     511                 :            :                info_border, port_id, queue_id, info_border);
     512                 :            : 
     513                 :          0 :         printf("\nMempool: %s", (qinfo.mp == NULL) ? "NULL" : qinfo.mp->name);
     514                 :          0 :         printf("\nRX prefetch threshold: %hhu", qinfo.conf.rx_thresh.pthresh);
     515                 :          0 :         printf("\nRX host threshold: %hhu", qinfo.conf.rx_thresh.hthresh);
     516                 :          0 :         printf("\nRX writeback threshold: %hhu", qinfo.conf.rx_thresh.wthresh);
     517                 :          0 :         printf("\nRX free threshold: %hu", qinfo.conf.rx_free_thresh);
     518                 :          0 :         printf("\nRX drop packets: %s",
     519                 :          0 :                 (qinfo.conf.rx_drop_en != 0) ? "on" : "off");
     520                 :          0 :         printf("\nRX deferred start: %s",
     521                 :          0 :                 (qinfo.conf.rx_deferred_start != 0) ? "on" : "off");
     522                 :          0 :         printf("\nRX scattered packets: %s",
     523                 :          0 :                 (qinfo.scattered_rx != 0) ? "on" : "off");
     524                 :          0 :         printf("\nRx queue state: %s", get_queue_state_name(qinfo.queue_state));
     525                 :          0 :         if (qinfo.rx_buf_size != 0)
     526                 :          0 :                 printf("\nRX buffer size: %hu", qinfo.rx_buf_size);
     527                 :          0 :         printf("\nNumber of RXDs: %hu", qinfo.nb_desc);
     528                 :            : 
     529                 :          0 :         if (rte_eth_rx_burst_mode_get(port_id, queue_id, &mode) == 0)
     530                 :          0 :                 printf("\nBurst mode: %s%s",
     531                 :            :                        mode.info,
     532                 :          0 :                        mode.flags & RTE_ETH_BURST_FLAG_PER_QUEUE ?
     533                 :            :                                 " (per queue)" : "");
     534                 :            : 
     535                 :            :         printf("\n");
     536                 :            : }
     537                 :            : 
     538                 :            : void
     539                 :          0 : tx_queue_infos_display(portid_t port_id, uint16_t queue_id)
     540                 :            : {
     541                 :            :         struct rte_eth_burst_mode mode;
     542                 :            :         struct rte_eth_txq_info qinfo;
     543                 :            :         int32_t rc;
     544                 :            :         static const char *info_border = "*********************";
     545                 :            : 
     546                 :          0 :         rc = rte_eth_tx_queue_info_get(port_id, queue_id, &qinfo);
     547                 :          0 :         if (rc != 0) {
     548                 :          0 :                 fprintf(stderr,
     549                 :            :                         "Failed to retrieve information for port: %u, TX queue: %hu\nerror desc: %s(%d)\n",
     550                 :            :                         port_id, queue_id, strerror(-rc), rc);
     551                 :          0 :                 return;
     552                 :            :         }
     553                 :            : 
     554                 :          0 :         printf("\n%s Infos for port %-2u, TX queue %-2u %s",
     555                 :            :                info_border, port_id, queue_id, info_border);
     556                 :            : 
     557                 :          0 :         printf("\nTX prefetch threshold: %hhu", qinfo.conf.tx_thresh.pthresh);
     558                 :          0 :         printf("\nTX host threshold: %hhu", qinfo.conf.tx_thresh.hthresh);
     559                 :          0 :         printf("\nTX writeback threshold: %hhu", qinfo.conf.tx_thresh.wthresh);
     560                 :          0 :         printf("\nTX RS threshold: %hu", qinfo.conf.tx_rs_thresh);
     561                 :          0 :         printf("\nTX free threshold: %hu", qinfo.conf.tx_free_thresh);
     562                 :          0 :         printf("\nTX deferred start: %s",
     563                 :          0 :                 (qinfo.conf.tx_deferred_start != 0) ? "on" : "off");
     564                 :          0 :         printf("\nNumber of TXDs: %hu", qinfo.nb_desc);
     565                 :          0 :         printf("\nTx queue state: %s", get_queue_state_name(qinfo.queue_state));
     566                 :            : 
     567                 :          0 :         if (rte_eth_tx_burst_mode_get(port_id, queue_id, &mode) == 0)
     568                 :          0 :                 printf("\nBurst mode: %s%s",
     569                 :            :                        mode.info,
     570                 :          0 :                        mode.flags & RTE_ETH_BURST_FLAG_PER_QUEUE ?
     571                 :            :                                 " (per queue)" : "");
     572                 :            : 
     573                 :            :         printf("\n");
     574                 :            : }
     575                 :            : 
     576                 :          0 : static int bus_match_all(const struct rte_bus *bus, const void *data)
     577                 :            : {
     578                 :            :         RTE_SET_USED(bus);
     579                 :            :         RTE_SET_USED(data);
     580                 :          0 :         return 0;
     581                 :            : }
     582                 :            : 
     583                 :            : static void
     584                 :          0 : device_infos_display_speeds(uint32_t speed_capa)
     585                 :            : {
     586                 :            :         printf("\n\tDevice speed capability:");
     587                 :          0 :         if (speed_capa == RTE_ETH_LINK_SPEED_AUTONEG)
     588                 :            :                 printf(" Autonegotiate (all speeds)");
     589                 :          0 :         if (speed_capa & RTE_ETH_LINK_SPEED_FIXED)
     590                 :            :                 printf(" Disable autonegotiate (fixed speed)  ");
     591                 :          0 :         if (speed_capa & RTE_ETH_LINK_SPEED_10M_HD)
     592                 :            :                 printf(" 10 Mbps half-duplex  ");
     593                 :          0 :         if (speed_capa & RTE_ETH_LINK_SPEED_10M)
     594                 :            :                 printf(" 10 Mbps full-duplex  ");
     595                 :          0 :         if (speed_capa & RTE_ETH_LINK_SPEED_100M_HD)
     596                 :            :                 printf(" 100 Mbps half-duplex  ");
     597                 :          0 :         if (speed_capa & RTE_ETH_LINK_SPEED_100M)
     598                 :            :                 printf(" 100 Mbps full-duplex  ");
     599                 :          0 :         if (speed_capa & RTE_ETH_LINK_SPEED_1G)
     600                 :            :                 printf(" 1 Gbps  ");
     601                 :          0 :         if (speed_capa & RTE_ETH_LINK_SPEED_2_5G)
     602                 :            :                 printf(" 2.5 Gbps  ");
     603                 :          0 :         if (speed_capa & RTE_ETH_LINK_SPEED_5G)
     604                 :            :                 printf(" 5 Gbps  ");
     605                 :          0 :         if (speed_capa & RTE_ETH_LINK_SPEED_10G)
     606                 :            :                 printf(" 10 Gbps  ");
     607                 :          0 :         if (speed_capa & RTE_ETH_LINK_SPEED_20G)
     608                 :            :                 printf(" 20 Gbps  ");
     609                 :          0 :         if (speed_capa & RTE_ETH_LINK_SPEED_25G)
     610                 :            :                 printf(" 25 Gbps  ");
     611                 :          0 :         if (speed_capa & RTE_ETH_LINK_SPEED_40G)
     612                 :            :                 printf(" 40 Gbps  ");
     613                 :          0 :         if (speed_capa & RTE_ETH_LINK_SPEED_50G)
     614                 :            :                 printf(" 50 Gbps  ");
     615                 :          0 :         if (speed_capa & RTE_ETH_LINK_SPEED_56G)
     616                 :            :                 printf(" 56 Gbps  ");
     617                 :          0 :         if (speed_capa & RTE_ETH_LINK_SPEED_100G)
     618                 :            :                 printf(" 100 Gbps  ");
     619                 :          0 :         if (speed_capa & RTE_ETH_LINK_SPEED_200G)
     620                 :            :                 printf(" 200 Gbps  ");
     621                 :          0 :         if (speed_capa & RTE_ETH_LINK_SPEED_400G)
     622                 :            :                 printf(" 400 Gbps  ");
     623                 :          0 : }
     624                 :            : 
     625                 :            : void
     626                 :          0 : device_infos_display(const char *identifier)
     627                 :            : {
     628                 :            :         static const char *info_border = "*********************";
     629                 :            :         struct rte_bus *start = NULL, *next;
     630                 :            :         struct rte_dev_iterator dev_iter;
     631                 :            :         char name[RTE_ETH_NAME_MAX_LEN];
     632                 :            :         struct rte_ether_addr mac_addr;
     633                 :            :         struct rte_device *dev;
     634                 :            :         struct rte_devargs da;
     635                 :            :         portid_t port_id;
     636                 :            :         struct rte_eth_dev_info dev_info;
     637                 :            :         char devstr[128];
     638                 :            : 
     639                 :            :         memset(&da, 0, sizeof(da));
     640                 :          0 :         if (!identifier)
     641                 :          0 :                 goto skip_parse;
     642                 :            : 
     643                 :          0 :         if (rte_devargs_parsef(&da, "%s", identifier)) {
     644                 :          0 :                 fprintf(stderr, "cannot parse identifier\n");
     645                 :          0 :                 return;
     646                 :            :         }
     647                 :            : 
     648                 :          0 : skip_parse:
     649                 :          0 :         while ((next = rte_bus_find(start, bus_match_all, NULL)) != NULL) {
     650                 :            : 
     651                 :            :                 start = next;
     652                 :          0 :                 if (identifier && da.bus != next)
     653                 :          0 :                         continue;
     654                 :            : 
     655                 :          0 :                 snprintf(devstr, sizeof(devstr), "bus=%s", rte_bus_name(next));
     656                 :          0 :                 RTE_DEV_FOREACH(dev, devstr, &dev_iter) {
     657                 :            : 
     658                 :          0 :                         if (rte_dev_driver(dev) == NULL)
     659                 :          0 :                                 continue;
     660                 :            :                         /* Check for matching device if identifier is present */
     661                 :          0 :                         if (identifier &&
     662                 :          0 :                             strncmp(da.name, rte_dev_name(dev), strlen(rte_dev_name(dev))))
     663                 :          0 :                                 continue;
     664                 :          0 :                         printf("\n%s Infos for device %s %s\n",
     665                 :            :                                info_border, rte_dev_name(dev), info_border);
     666                 :          0 :                         printf("Bus name: %s", rte_bus_name(rte_dev_bus(dev)));
     667                 :          0 :                         printf("\nBus information: %s",
     668                 :          0 :                                 rte_dev_bus_info(dev) ? rte_dev_bus_info(dev) : "");
     669                 :          0 :                         printf("\nDriver name: %s", rte_driver_name(rte_dev_driver(dev)));
     670                 :          0 :                         printf("\nDevargs: %s",
     671                 :          0 :                                rte_dev_devargs(dev) ? rte_dev_devargs(dev)->args : "");
     672                 :          0 :                         printf("\nConnect to socket: %d", rte_dev_numa_node(dev));
     673                 :            :                         printf("\n");
     674                 :            : 
     675                 :            :                         /* List ports with matching device name */
     676                 :          0 :                         RTE_ETH_FOREACH_DEV_OF(port_id, dev) {
     677                 :          0 :                                 printf("\n\tPort id: %-2d", port_id);
     678                 :          0 :                                 if (eth_macaddr_get_print_err(port_id,
     679                 :            :                                                               &mac_addr) == 0)
     680                 :          0 :                                         print_ethaddr("\n\tMAC address: ",
     681                 :            :                                                       &mac_addr);
     682                 :          0 :                                 rte_eth_dev_get_name_by_port(port_id, name);
     683                 :            :                                 printf("\n\tDevice name: %s", name);
     684                 :          0 :                                 if (rte_eth_dev_info_get(port_id, &dev_info) == 0)
     685                 :          0 :                                         device_infos_display_speeds(dev_info.speed_capa);
     686                 :            :                                 printf("\n");
     687                 :            :                         }
     688                 :            :                 }
     689                 :            :         };
     690                 :          0 :         rte_devargs_reset(&da);
     691                 :            : }
     692                 :            : 
     693                 :            : static void
     694                 :          0 : print_dev_capabilities(uint64_t capabilities)
     695                 :            : {
     696                 :            :         uint64_t single_capa;
     697                 :            :         int begin;
     698                 :            :         int end;
     699                 :            :         int bit;
     700                 :            : 
     701                 :          0 :         if (capabilities == 0)
     702                 :            :                 return;
     703                 :            : 
     704                 :            :         begin = rte_ctz64(capabilities);
     705                 :          0 :         end = sizeof(capabilities) * CHAR_BIT - rte_clz64(capabilities);
     706                 :            : 
     707                 :          0 :         single_capa = 1ULL << begin;
     708                 :          0 :         for (bit = begin; bit < end; bit++) {
     709                 :          0 :                 if (capabilities & single_capa)
     710                 :          0 :                         printf(" %s",
     711                 :            :                                rte_eth_dev_capability_name(single_capa));
     712                 :          0 :                 single_capa <<= 1;
     713                 :            :         }
     714                 :            : }
     715                 :            : 
     716                 :            : uint64_t
     717                 :          0 : str_to_rsstypes(const char *str)
     718                 :            : {
     719                 :            :         uint16_t i;
     720                 :            : 
     721                 :          0 :         for (i = 0; rss_type_table[i].str != NULL; i++) {
     722                 :          0 :                 if (strcmp(rss_type_table[i].str, str) == 0)
     723                 :          0 :                         return rss_type_table[i].rss_type;
     724                 :            :         }
     725                 :            : 
     726                 :            :         return 0;
     727                 :            : }
     728                 :            : 
     729                 :            : const char *
     730                 :          0 : rsstypes_to_str(uint64_t rss_type)
     731                 :            : {
     732                 :            :         uint16_t i;
     733                 :            : 
     734                 :          0 :         for (i = 0; rss_type_table[i].str != NULL; i++) {
     735                 :          0 :                 if (rss_type_table[i].rss_type == rss_type)
     736                 :          0 :                         return rss_type_table[i].str;
     737                 :            :         }
     738                 :            : 
     739                 :            :         return NULL;
     740                 :            : }
     741                 :            : 
     742                 :            : static void
     743                 :          0 : rss_offload_types_display(uint64_t offload_types, uint16_t char_num_per_line)
     744                 :            : {
     745                 :            :         uint16_t user_defined_str_len;
     746                 :            :         uint16_t total_len = 0;
     747                 :            :         uint16_t str_len = 0;
     748                 :            :         uint64_t rss_offload;
     749                 :            :         uint16_t i;
     750                 :            : 
     751                 :          0 :         for (i = 0; i < sizeof(offload_types) * CHAR_BIT; i++) {
     752                 :          0 :                 rss_offload = RTE_BIT64(i);
     753                 :          0 :                 if ((offload_types & rss_offload) != 0) {
     754                 :            :                         const char *p = rsstypes_to_str(rss_offload);
     755                 :            : 
     756                 :          0 :                         user_defined_str_len =
     757                 :          0 :                                 strlen("user-defined-") + (i / 10 + 1);
     758                 :          0 :                         str_len = p ? strlen(p) : user_defined_str_len;
     759                 :          0 :                         str_len += 2; /* add two spaces */
     760                 :          0 :                         if (total_len + str_len >= char_num_per_line) {
     761                 :            :                                 total_len = 0;
     762                 :            :                                 printf("\n");
     763                 :            :                         }
     764                 :            : 
     765                 :          0 :                         if (p)
     766                 :            :                                 printf("  %s", p);
     767                 :            :                         else
     768                 :            :                                 printf("  user-defined-%u", i);
     769                 :          0 :                         total_len += str_len;
     770                 :            :                 }
     771                 :            :         }
     772                 :            :         printf("\n");
     773                 :          0 : }
     774                 :            : 
     775                 :            : void
     776                 :          0 : port_infos_display(portid_t port_id)
     777                 :            : {
     778                 :            :         struct rte_port *port;
     779                 :            :         struct rte_ether_addr mac_addr;
     780                 :            :         struct rte_eth_link link;
     781                 :            :         struct rte_eth_dev_info dev_info;
     782                 :            :         int vlan_offload;
     783                 :            :         struct rte_mempool * mp;
     784                 :            :         static const char *info_border = "*********************";
     785                 :            :         uint16_t mtu;
     786                 :            :         char name[RTE_ETH_NAME_MAX_LEN];
     787                 :            :         int ret;
     788                 :            :         char fw_version[ETHDEV_FWVERS_LEN];
     789                 :            :         uint32_t lanes;
     790                 :            : 
     791                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
     792                 :          0 :                 print_valid_ports();
     793                 :          0 :                 return;
     794                 :            :         }
     795                 :          0 :         port = &ports[port_id];
     796                 :          0 :         ret = eth_link_get_nowait_print_err(port_id, &link);
     797                 :          0 :         if (ret < 0)
     798                 :            :                 return;
     799                 :            : 
     800                 :          0 :         ret = eth_dev_info_get_print_err(port_id, &dev_info);
     801                 :          0 :         if (ret != 0)
     802                 :            :                 return;
     803                 :            : 
     804                 :          0 :         printf("\n%s Infos for port %-2d %s\n",
     805                 :            :                info_border, port_id, info_border);
     806                 :          0 :         if (eth_macaddr_get_print_err(port_id, &mac_addr) == 0)
     807                 :          0 :                 print_ethaddr("MAC address: ", &mac_addr);
     808                 :          0 :         rte_eth_dev_get_name_by_port(port_id, name);
     809                 :            :         printf("\nDevice name: %s", name);
     810                 :          0 :         printf("\nDriver name: %s", dev_info.driver_name);
     811                 :            : 
     812                 :          0 :         if (rte_eth_dev_fw_version_get(port_id, fw_version,
     813                 :            :                                                 ETHDEV_FWVERS_LEN) == 0)
     814                 :            :                 printf("\nFirmware-version: %s", fw_version);
     815                 :            :         else
     816                 :            :                 printf("\nFirmware-version: %s", "not available");
     817                 :            : 
     818                 :          0 :         if (rte_dev_devargs(dev_info.device) && rte_dev_devargs(dev_info.device)->args)
     819                 :          0 :                 printf("\nDevargs: %s", rte_dev_devargs(dev_info.device)->args);
     820                 :          0 :         printf("\nConnect to socket: %u", port->socket_id);
     821                 :            : 
     822                 :          0 :         if (port_numa[port_id] != NUMA_NO_CONFIG) {
     823                 :          0 :                 mp = mbuf_pool_find(port_numa[port_id], 0);
     824                 :          0 :                 if (mp)
     825                 :          0 :                         printf("\nmemory allocation on the socket: %d",
     826                 :          0 :                                                         port_numa[port_id]);
     827                 :            :         } else
     828                 :          0 :                 printf("\nmemory allocation on the socket: %u",port->socket_id);
     829                 :            : 
     830                 :          0 :         printf("\nLink status: %s\n", (link.link_status) ? ("up") : ("down"));
     831                 :          0 :         printf("Link speed: %s\n", rte_eth_link_speed_to_str(link.link_speed));
     832                 :          0 :         if (rte_eth_speed_lanes_get(port_id, &lanes) == 0)
     833                 :          0 :                 printf("Active Lanes: %d\n", lanes);
     834                 :          0 :         printf("Link duplex: %s\n", (link.link_duplex == RTE_ETH_LINK_FULL_DUPLEX) ?
     835                 :            :                ("full-duplex") : ("half-duplex"));
     836                 :          0 :         printf("Autoneg status: %s\n", (link.link_autoneg == RTE_ETH_LINK_AUTONEG) ?
     837                 :            :                ("On") : ("Off"));
     838                 :            : 
     839                 :          0 :         if (!rte_eth_dev_get_mtu(port_id, &mtu))
     840                 :          0 :                 printf("MTU: %u\n", mtu);
     841                 :            : 
     842                 :          0 :         printf("Promiscuous mode: %s\n",
     843                 :          0 :                rte_eth_promiscuous_get(port_id) ? "enabled" : "disabled");
     844                 :          0 :         printf("Allmulticast mode: %s\n",
     845                 :          0 :                rte_eth_allmulticast_get(port_id) ? "enabled" : "disabled");
     846                 :            :         printf("Maximum number of MAC addresses: %u\n",
     847                 :          0 :                (unsigned int)(port->dev_info.max_mac_addrs));
     848                 :            :         printf("Maximum number of MAC addresses of hash filtering: %u\n",
     849                 :          0 :                (unsigned int)(port->dev_info.max_hash_mac_addrs));
     850                 :            : 
     851                 :          0 :         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
     852                 :          0 :         if (vlan_offload >= 0){
     853                 :            :                 printf("VLAN offload: \n");
     854                 :          0 :                 if (vlan_offload & RTE_ETH_VLAN_STRIP_OFFLOAD)
     855                 :            :                         printf("  strip on, ");
     856                 :            :                 else
     857                 :            :                         printf("  strip off, ");
     858                 :            : 
     859                 :          0 :                 if (vlan_offload & RTE_ETH_VLAN_FILTER_OFFLOAD)
     860                 :            :                         printf("filter on, ");
     861                 :            :                 else
     862                 :            :                         printf("filter off, ");
     863                 :            : 
     864                 :          0 :                 if (vlan_offload & RTE_ETH_VLAN_EXTEND_OFFLOAD)
     865                 :            :                         printf("extend on, ");
     866                 :            :                 else
     867                 :            :                         printf("extend off, ");
     868                 :            : 
     869                 :          0 :                 if (vlan_offload & RTE_ETH_QINQ_STRIP_OFFLOAD)
     870                 :            :                         printf("qinq strip on\n");
     871                 :            :                 else
     872                 :            :                         printf("qinq strip off\n");
     873                 :            :         }
     874                 :            : 
     875                 :          0 :         if (dev_info.hash_key_size > 0)
     876                 :          0 :                 printf("Hash key size in bytes: %u\n", dev_info.hash_key_size);
     877                 :          0 :         if (dev_info.reta_size > 0)
     878                 :          0 :                 printf("Redirection table size: %u\n", dev_info.reta_size);
     879                 :          0 :         if (!dev_info.flow_type_rss_offloads)
     880                 :            :                 printf("No RSS offload flow type is supported.\n");
     881                 :            :         else {
     882                 :            :                 printf("Supported RSS offload flow types:\n");
     883                 :          0 :                 rss_offload_types_display(dev_info.flow_type_rss_offloads,
     884                 :            :                                 TESTPMD_RSS_TYPES_CHAR_NUM_PER_LINE);
     885                 :            :         }
     886                 :            : 
     887                 :          0 :         printf("Minimum size of RX buffer: %u\n", dev_info.min_rx_bufsize);
     888                 :          0 :         if (dev_info.max_rx_bufsize != UINT32_MAX)
     889                 :            :                 printf("Maximum size of RX buffer: %u\n", dev_info.max_rx_bufsize);
     890                 :          0 :         printf("Maximum configurable length of RX packet: %u\n",
     891                 :            :                 dev_info.max_rx_pktlen);
     892                 :          0 :         printf("Maximum configurable size of LRO aggregated packet: %u\n",
     893                 :            :                 dev_info.max_lro_pkt_size);
     894                 :          0 :         if (dev_info.max_vfs)
     895                 :          0 :                 printf("Maximum number of VFs: %u\n", dev_info.max_vfs);
     896                 :          0 :         if (dev_info.max_vmdq_pools)
     897                 :          0 :                 printf("Maximum number of VMDq pools: %u\n",
     898                 :            :                         dev_info.max_vmdq_pools);
     899                 :            : 
     900                 :          0 :         printf("Current number of RX queues: %u\n", dev_info.nb_rx_queues);
     901                 :          0 :         printf("Max possible RX queues: %u\n", dev_info.max_rx_queues);
     902                 :          0 :         printf("Max possible number of RXDs per queue: %hu\n",
     903                 :          0 :                 dev_info.rx_desc_lim.nb_max);
     904                 :          0 :         printf("Min possible number of RXDs per queue: %hu\n",
     905                 :          0 :                 dev_info.rx_desc_lim.nb_min);
     906                 :          0 :         printf("RXDs number alignment: %hu\n", dev_info.rx_desc_lim.nb_align);
     907                 :            : 
     908                 :          0 :         printf("Current number of TX queues: %u\n", dev_info.nb_tx_queues);
     909                 :          0 :         printf("Max possible TX queues: %u\n", dev_info.max_tx_queues);
     910                 :          0 :         printf("Max possible number of TXDs per queue: %hu\n",
     911                 :          0 :                 dev_info.tx_desc_lim.nb_max);
     912                 :          0 :         printf("Min possible number of TXDs per queue: %hu\n",
     913                 :          0 :                 dev_info.tx_desc_lim.nb_min);
     914                 :          0 :         printf("TXDs number alignment: %hu\n", dev_info.tx_desc_lim.nb_align);
     915                 :          0 :         printf("Max segment number per packet: %hu\n",
     916                 :          0 :                 dev_info.tx_desc_lim.nb_seg_max);
     917                 :          0 :         printf("Max segment number per MTU/TSO: %hu\n",
     918                 :          0 :                 dev_info.tx_desc_lim.nb_mtu_seg_max);
     919                 :            : 
     920                 :          0 :         printf("Device capabilities: 0x%"PRIx64"(", dev_info.dev_capa);
     921                 :          0 :         print_dev_capabilities(dev_info.dev_capa);
     922                 :            :         printf(" )\n");
     923                 :            :         /* Show switch info only if valid switch domain and port id is set */
     924                 :          0 :         if (dev_info.switch_info.domain_id !=
     925                 :            :                 RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
     926                 :          0 :                 if (dev_info.switch_info.name)
     927                 :            :                         printf("Switch name: %s\n", dev_info.switch_info.name);
     928                 :            : 
     929                 :          0 :                 printf("Switch domain Id: %u\n",
     930                 :          0 :                         dev_info.switch_info.domain_id);
     931                 :          0 :                 printf("Switch Port Id: %u\n",
     932                 :          0 :                         dev_info.switch_info.port_id);
     933                 :          0 :                 if ((dev_info.dev_capa & RTE_ETH_DEV_CAPA_RXQ_SHARE) != 0)
     934                 :          0 :                         printf("Switch Rx domain: %u\n",
     935                 :          0 :                                dev_info.switch_info.rx_domain);
     936                 :            :         }
     937                 :            :         printf("Device error handling mode: ");
     938                 :          0 :         switch (dev_info.err_handle_mode) {
     939                 :            :         case RTE_ETH_ERROR_HANDLE_MODE_NONE:
     940                 :            :                 printf("none\n");
     941                 :            :                 break;
     942                 :            :         case RTE_ETH_ERROR_HANDLE_MODE_PASSIVE:
     943                 :            :                 printf("passive\n");
     944                 :            :                 break;
     945                 :            :         case RTE_ETH_ERROR_HANDLE_MODE_PROACTIVE:
     946                 :            :                 printf("proactive\n");
     947                 :            :                 break;
     948                 :            :         default:
     949                 :            :                 printf("unknown\n");
     950                 :            :                 break;
     951                 :            :         }
     952                 :            :         printf("Device private info:\n");
     953                 :          0 :         ret = rte_eth_dev_priv_dump(port_id, stdout);
     954                 :          0 :         if (ret == -ENOTSUP)
     955                 :            :                 printf("  none\n");
     956                 :          0 :         else if (ret < 0)
     957                 :          0 :                 fprintf(stderr, "  Failed to dump private info with error (%d): %s\n",
     958                 :            :                         ret, strerror(-ret));
     959                 :            : }
     960                 :            : 
     961                 :            : void
     962                 :          0 : port_summary_header_display(void)
     963                 :            : {
     964                 :            :         uint16_t port_number;
     965                 :            : 
     966                 :          0 :         port_number = rte_eth_dev_count_avail();
     967                 :          0 :         printf("Number of available ports: %i\n", port_number);
     968                 :            :         printf("%-4s %-17s %-12s %-14s %-8s %s\n", "Port", "MAC Address", "Name",
     969                 :            :                         "Driver", "Status", "Link");
     970                 :          0 : }
     971                 :            : 
     972                 :            : void
     973                 :          0 : port_summary_display(portid_t port_id)
     974                 :            : {
     975                 :            :         struct rte_ether_addr mac_addr;
     976                 :            :         struct rte_eth_link link;
     977                 :            :         struct rte_eth_dev_info dev_info;
     978                 :            :         char name[RTE_ETH_NAME_MAX_LEN];
     979                 :            :         int ret;
     980                 :            : 
     981                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
     982                 :          0 :                 print_valid_ports();
     983                 :          0 :                 return;
     984                 :            :         }
     985                 :            : 
     986                 :          0 :         ret = eth_link_get_nowait_print_err(port_id, &link);
     987                 :          0 :         if (ret < 0)
     988                 :            :                 return;
     989                 :            : 
     990                 :          0 :         ret = eth_dev_info_get_print_err(port_id, &dev_info);
     991                 :          0 :         if (ret != 0)
     992                 :            :                 return;
     993                 :            : 
     994                 :          0 :         rte_eth_dev_get_name_by_port(port_id, name);
     995                 :          0 :         ret = eth_macaddr_get_print_err(port_id, &mac_addr);
     996                 :          0 :         if (ret != 0)
     997                 :            :                 return;
     998                 :            : 
     999                 :          0 :         printf("%-4d " RTE_ETHER_ADDR_PRT_FMT " %-12s %-14s %-8s %s\n",
    1000                 :          0 :                 port_id, RTE_ETHER_ADDR_BYTES(&mac_addr), name,
    1001                 :          0 :                 dev_info.driver_name, (link.link_status) ? ("up") : ("down"),
    1002                 :            :                 rte_eth_link_speed_to_str(link.link_speed));
    1003                 :            : }
    1004                 :            : 
    1005                 :            : void
    1006                 :          0 : port_eeprom_display(portid_t port_id)
    1007                 :            : {
    1008                 :            :         struct rte_dev_eeprom_info einfo;
    1009                 :            :         int ret;
    1010                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
    1011                 :          0 :                 print_valid_ports();
    1012                 :          0 :                 return;
    1013                 :            :         }
    1014                 :            : 
    1015                 :          0 :         int len_eeprom = rte_eth_dev_get_eeprom_length(port_id);
    1016                 :          0 :         if (len_eeprom < 0) {
    1017                 :          0 :                 switch (len_eeprom) {
    1018                 :          0 :                 case -ENODEV:
    1019                 :          0 :                         fprintf(stderr, "port index %d invalid\n", port_id);
    1020                 :            :                         break;
    1021                 :          0 :                 case -ENOTSUP:
    1022                 :          0 :                         fprintf(stderr, "operation not supported by device\n");
    1023                 :            :                         break;
    1024                 :          0 :                 case -EIO:
    1025                 :          0 :                         fprintf(stderr, "device is removed\n");
    1026                 :            :                         break;
    1027                 :          0 :                 default:
    1028                 :          0 :                         fprintf(stderr, "Unable to get EEPROM: %d\n",
    1029                 :            :                                 len_eeprom);
    1030                 :            :                         break;
    1031                 :            :                 }
    1032                 :          0 :                 return;
    1033                 :            :         }
    1034                 :            : 
    1035                 :          0 :         einfo.offset = 0;
    1036                 :          0 :         einfo.length = len_eeprom;
    1037                 :          0 :         einfo.data = calloc(1, len_eeprom);
    1038                 :          0 :         if (!einfo.data) {
    1039                 :          0 :                 fprintf(stderr,
    1040                 :            :                         "Allocation of port %u eeprom data failed\n",
    1041                 :            :                         port_id);
    1042                 :          0 :                 return;
    1043                 :            :         }
    1044                 :            : 
    1045                 :          0 :         ret = rte_eth_dev_get_eeprom(port_id, &einfo);
    1046                 :          0 :         if (ret != 0) {
    1047                 :          0 :                 switch (ret) {
    1048                 :          0 :                 case -ENODEV:
    1049                 :          0 :                         fprintf(stderr, "port index %d invalid\n", port_id);
    1050                 :            :                         break;
    1051                 :          0 :                 case -ENOTSUP:
    1052                 :          0 :                         fprintf(stderr, "operation not supported by device\n");
    1053                 :            :                         break;
    1054                 :          0 :                 case -EIO:
    1055                 :          0 :                         fprintf(stderr, "device is removed\n");
    1056                 :            :                         break;
    1057                 :          0 :                 default:
    1058                 :          0 :                         fprintf(stderr, "Unable to get EEPROM: %d\n", ret);
    1059                 :            :                         break;
    1060                 :            :                 }
    1061                 :          0 :                 free(einfo.data);
    1062                 :          0 :                 return;
    1063                 :            :         }
    1064                 :          0 :         rte_hexdump(stdout, "hexdump", einfo.data, einfo.length);
    1065                 :            :         printf("Finish -- Port: %d EEPROM length: %d bytes\n", port_id, len_eeprom);
    1066                 :          0 :         free(einfo.data);
    1067                 :            : }
    1068                 :            : 
    1069                 :            : void
    1070                 :          0 : port_eeprom_set(portid_t port_id,
    1071                 :            :                 uint32_t magic,
    1072                 :            :                 uint32_t offset,
    1073                 :            :                 uint32_t length,
    1074                 :            :                 uint8_t *value)
    1075                 :            : {
    1076                 :            :         struct rte_dev_eeprom_info einfo;
    1077                 :            :         int len_eeprom;
    1078                 :            :         int ret;
    1079                 :            : 
    1080                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
    1081                 :          0 :                 print_valid_ports();
    1082                 :          0 :                 return;
    1083                 :            :         }
    1084                 :            : 
    1085                 :          0 :         len_eeprom = rte_eth_dev_get_eeprom_length(port_id);
    1086                 :          0 :         if (len_eeprom < 0) {
    1087                 :          0 :                 fprintf(stderr, "Unable to get EEPROM length: %s\n",
    1088                 :            :                         rte_strerror(-len_eeprom));
    1089                 :          0 :                 return;
    1090                 :            :         }
    1091                 :            : 
    1092                 :          0 :         einfo.data = value;
    1093                 :          0 :         einfo.magic = magic;
    1094                 :          0 :         einfo.length = length;
    1095                 :          0 :         einfo.offset = offset;
    1096                 :            : 
    1097                 :          0 :         if (einfo.offset + einfo.length > (uint32_t)(len_eeprom)) {
    1098                 :          0 :                 fprintf(stderr, "offset and length exceed capabilities of EEPROM length: %d\n",
    1099                 :            :                         len_eeprom);
    1100                 :          0 :                 return;
    1101                 :            :         }
    1102                 :            : 
    1103                 :          0 :         ret = rte_eth_dev_set_eeprom(port_id, &einfo);
    1104                 :          0 :         if (ret != 0)
    1105                 :          0 :                 fprintf(stderr, "Unable to set EEPROM: %s\n", rte_strerror(-ret));
    1106                 :            : }
    1107                 :            : 
    1108                 :            : void
    1109                 :          0 : port_module_eeprom_display(portid_t port_id)
    1110                 :            : {
    1111                 :            :         struct rte_eth_dev_module_info minfo;
    1112                 :            :         struct rte_dev_eeprom_info einfo;
    1113                 :            :         int ret;
    1114                 :            : 
    1115                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
    1116                 :          0 :                 print_valid_ports();
    1117                 :          0 :                 return;
    1118                 :            :         }
    1119                 :            : 
    1120                 :            : 
    1121                 :          0 :         ret = rte_eth_dev_get_module_info(port_id, &minfo);
    1122                 :          0 :         if (ret != 0) {
    1123                 :          0 :                 switch (ret) {
    1124                 :          0 :                 case -ENODEV:
    1125                 :          0 :                         fprintf(stderr, "port index %d invalid\n", port_id);
    1126                 :            :                         break;
    1127                 :          0 :                 case -ENOTSUP:
    1128                 :          0 :                         fprintf(stderr, "operation not supported by device\n");
    1129                 :            :                         break;
    1130                 :          0 :                 case -EIO:
    1131                 :          0 :                         fprintf(stderr, "device is removed\n");
    1132                 :            :                         break;
    1133                 :          0 :                 default:
    1134                 :          0 :                         fprintf(stderr, "Unable to get module EEPROM: %d\n",
    1135                 :            :                                 ret);
    1136                 :            :                         break;
    1137                 :            :                 }
    1138                 :          0 :                 return;
    1139                 :            :         }
    1140                 :            : 
    1141                 :          0 :         einfo.offset = 0;
    1142                 :          0 :         einfo.length = minfo.eeprom_len;
    1143                 :          0 :         einfo.data = calloc(1, minfo.eeprom_len);
    1144                 :          0 :         if (!einfo.data) {
    1145                 :          0 :                 fprintf(stderr,
    1146                 :            :                         "Allocation of port %u eeprom data failed\n",
    1147                 :            :                         port_id);
    1148                 :          0 :                 return;
    1149                 :            :         }
    1150                 :            : 
    1151                 :          0 :         ret = rte_eth_dev_get_module_eeprom(port_id, &einfo);
    1152                 :          0 :         if (ret != 0) {
    1153                 :          0 :                 switch (ret) {
    1154                 :          0 :                 case -ENODEV:
    1155                 :          0 :                         fprintf(stderr, "port index %d invalid\n", port_id);
    1156                 :            :                         break;
    1157                 :          0 :                 case -ENOTSUP:
    1158                 :          0 :                         fprintf(stderr, "operation not supported by device\n");
    1159                 :            :                         break;
    1160                 :          0 :                 case -EIO:
    1161                 :          0 :                         fprintf(stderr, "device is removed\n");
    1162                 :            :                         break;
    1163                 :          0 :                 default:
    1164                 :          0 :                         fprintf(stderr, "Unable to get module EEPROM: %d\n",
    1165                 :            :                                 ret);
    1166                 :            :                         break;
    1167                 :            :                 }
    1168                 :          0 :                 free(einfo.data);
    1169                 :          0 :                 return;
    1170                 :            :         }
    1171                 :            : 
    1172                 :          0 :         rte_hexdump(stdout, "hexdump", einfo.data, einfo.length);
    1173                 :          0 :         printf("Finish -- Port: %d MODULE EEPROM length: %d bytes\n", port_id, einfo.length);
    1174                 :          0 :         free(einfo.data);
    1175                 :            : }
    1176                 :            : 
    1177                 :            : int
    1178                 :          0 : port_id_is_invalid(portid_t port_id, enum print_warning warning)
    1179                 :            : {
    1180                 :            :         uint16_t pid;
    1181                 :            : 
    1182                 :          0 :         if (port_id == (portid_t)RTE_PORT_ALL)
    1183                 :            :                 return 0;
    1184                 :            : 
    1185                 :          0 :         RTE_ETH_FOREACH_DEV(pid)
    1186                 :          0 :                 if (port_id == pid)
    1187                 :            :                         return 0;
    1188                 :            : 
    1189                 :          0 :         if (warning == ENABLED_WARN)
    1190                 :          0 :                 fprintf(stderr, "Invalid port %d\n", port_id);
    1191                 :            : 
    1192                 :            :         return 1;
    1193                 :            : }
    1194                 :            : 
    1195                 :          0 : void print_valid_ports(void)
    1196                 :            : {
    1197                 :            :         portid_t pid;
    1198                 :            : 
    1199                 :            :         printf("The valid ports array is [");
    1200                 :          0 :         RTE_ETH_FOREACH_DEV(pid) {
    1201                 :          0 :                 printf(" %d", pid);
    1202                 :            :         }
    1203                 :            :         printf(" ]\n");
    1204                 :          0 : }
    1205                 :            : 
    1206                 :            : static int
    1207                 :            : vlan_id_is_invalid(uint16_t vlan_id)
    1208                 :            : {
    1209                 :          0 :         if (vlan_id < 4096)
    1210                 :            :                 return 0;
    1211                 :          0 :         fprintf(stderr, "Invalid vlan_id %d (must be < 4096)\n", vlan_id);
    1212                 :            :         return 1;
    1213                 :            : }
    1214                 :            : 
    1215                 :            : static uint32_t
    1216                 :            : eth_dev_get_overhead_len(uint32_t max_rx_pktlen, uint16_t max_mtu)
    1217                 :            : {
    1218                 :            :         uint32_t overhead_len;
    1219                 :            : 
    1220                 :          0 :         if (max_mtu != UINT16_MAX && max_rx_pktlen > max_mtu)
    1221                 :          0 :                 overhead_len = max_rx_pktlen - max_mtu;
    1222                 :            :         else
    1223                 :            :                 overhead_len = RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN;
    1224                 :            : 
    1225                 :            :         return overhead_len;
    1226                 :            : }
    1227                 :            : 
    1228                 :            : static int
    1229                 :          0 : eth_dev_validate_mtu(uint16_t port_id, uint16_t mtu)
    1230                 :            : {
    1231                 :            :         struct rte_eth_dev_info dev_info;
    1232                 :            :         uint32_t overhead_len;
    1233                 :            :         uint32_t frame_size;
    1234                 :            :         int ret;
    1235                 :            : 
    1236                 :          0 :         ret = rte_eth_dev_info_get(port_id, &dev_info);
    1237                 :          0 :         if (ret != 0)
    1238                 :            :                 return ret;
    1239                 :            : 
    1240                 :          0 :         if (mtu < dev_info.min_mtu) {
    1241                 :          0 :                 fprintf(stderr,
    1242                 :            :                         "MTU (%u) < device min MTU (%u) for port_id %u\n",
    1243                 :            :                         mtu, dev_info.min_mtu, port_id);
    1244                 :          0 :                 return -EINVAL;
    1245                 :            :         }
    1246                 :          0 :         if (mtu > dev_info.max_mtu) {
    1247                 :          0 :                 fprintf(stderr,
    1248                 :            :                         "MTU (%u) > device max MTU (%u) for port_id %u\n",
    1249                 :            :                         mtu, dev_info.max_mtu, port_id);
    1250                 :          0 :                 return -EINVAL;
    1251                 :            :         }
    1252                 :            : 
    1253                 :          0 :         overhead_len = eth_dev_get_overhead_len(dev_info.max_rx_pktlen,
    1254                 :            :                         dev_info.max_mtu);
    1255                 :          0 :         frame_size = mtu + overhead_len;
    1256                 :          0 :         if (frame_size > dev_info.max_rx_pktlen) {
    1257                 :          0 :                 fprintf(stderr,
    1258                 :            :                         "Frame size (%u) > device max frame size (%u) for port_id %u\n",
    1259                 :            :                         frame_size, dev_info.max_rx_pktlen, port_id);
    1260                 :          0 :                 return -EINVAL;
    1261                 :            :         }
    1262                 :            : 
    1263                 :            :         return 0;
    1264                 :            : }
    1265                 :            : 
    1266                 :            : void
    1267                 :          0 : port_mtu_set(portid_t port_id, uint16_t mtu)
    1268                 :            : {
    1269                 :          0 :         struct rte_port *port = &ports[port_id];
    1270                 :            :         int diag;
    1271                 :            : 
    1272                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    1273                 :            :                 return;
    1274                 :            : 
    1275                 :          0 :         diag = eth_dev_validate_mtu(port_id, mtu);
    1276                 :          0 :         if (diag != 0)
    1277                 :            :                 return;
    1278                 :            : 
    1279                 :          0 :         if (port->need_reconfig == 0) {
    1280                 :          0 :                 diag = rte_eth_dev_set_mtu(port_id, mtu);
    1281                 :          0 :                 if (diag != 0) {
    1282                 :          0 :                         fprintf(stderr, "Set MTU failed. diag=%d\n", diag);
    1283                 :          0 :                         return;
    1284                 :            :                 }
    1285                 :            :         }
    1286                 :            : 
    1287                 :          0 :         port->dev_conf.rxmode.mtu = mtu;
    1288                 :            : }
    1289                 :            : 
    1290                 :            : /* Generic flow management functions. */
    1291                 :            : 
    1292                 :            : static struct port_flow_tunnel *
    1293                 :            : port_flow_locate_tunnel_id(struct rte_port *port, uint32_t port_tunnel_id)
    1294                 :            : {
    1295                 :            :         struct port_flow_tunnel *flow_tunnel;
    1296                 :            : 
    1297                 :          0 :         LIST_FOREACH(flow_tunnel, &port->flow_tunnel_list, chain) {
    1298                 :          0 :                 if (flow_tunnel->id == port_tunnel_id)
    1299                 :          0 :                         goto out;
    1300                 :            :         }
    1301                 :            :         flow_tunnel = NULL;
    1302                 :            : 
    1303                 :          0 : out:
    1304                 :            :         return flow_tunnel;
    1305                 :            : }
    1306                 :            : 
    1307                 :            : const char *
    1308                 :          0 : port_flow_tunnel_type(struct rte_flow_tunnel *tunnel)
    1309                 :            : {
    1310                 :            :         const char *type;
    1311                 :          0 :         switch (tunnel->type) {
    1312                 :            :         default:
    1313                 :            :                 type = "unknown";
    1314                 :            :                 break;
    1315                 :          0 :         case RTE_FLOW_ITEM_TYPE_VXLAN:
    1316                 :            :                 type = "vxlan";
    1317                 :          0 :                 break;
    1318                 :          0 :         case RTE_FLOW_ITEM_TYPE_GRE:
    1319                 :            :                 type = "gre";
    1320                 :          0 :                 break;
    1321                 :          0 :         case RTE_FLOW_ITEM_TYPE_NVGRE:
    1322                 :            :                 type = "nvgre";
    1323                 :          0 :                 break;
    1324                 :          0 :         case RTE_FLOW_ITEM_TYPE_GENEVE:
    1325                 :            :                 type = "geneve";
    1326                 :          0 :                 break;
    1327                 :            :         }
    1328                 :            : 
    1329                 :          0 :         return type;
    1330                 :            : }
    1331                 :            : 
    1332                 :            : struct port_flow_tunnel *
    1333                 :          0 : port_flow_locate_tunnel(uint16_t port_id, struct rte_flow_tunnel *tun)
    1334                 :            : {
    1335                 :          0 :         struct rte_port *port = &ports[port_id];
    1336                 :            :         struct port_flow_tunnel *flow_tunnel;
    1337                 :            : 
    1338                 :          0 :         LIST_FOREACH(flow_tunnel, &port->flow_tunnel_list, chain) {
    1339                 :          0 :                 if (!memcmp(&flow_tunnel->tunnel, tun, sizeof(*tun)))
    1340                 :          0 :                         goto out;
    1341                 :            :         }
    1342                 :            :         flow_tunnel = NULL;
    1343                 :            : 
    1344                 :          0 : out:
    1345                 :          0 :         return flow_tunnel;
    1346                 :            : }
    1347                 :            : 
    1348                 :          0 : void port_flow_tunnel_list(portid_t port_id)
    1349                 :            : {
    1350                 :          0 :         struct rte_port *port = &ports[port_id];
    1351                 :            :         struct port_flow_tunnel *flt;
    1352                 :            : 
    1353                 :          0 :         LIST_FOREACH(flt, &port->flow_tunnel_list, chain) {
    1354                 :          0 :                 printf("port %u tunnel #%u type=%s",
    1355                 :            :                         port_id, flt->id, port_flow_tunnel_type(&flt->tunnel));
    1356                 :          0 :                 if (flt->tunnel.tun_id)
    1357                 :            :                         printf(" id=%" PRIu64, flt->tunnel.tun_id);
    1358                 :            :                 printf("\n");
    1359                 :            :         }
    1360                 :          0 : }
    1361                 :            : 
    1362                 :          0 : void port_flow_tunnel_destroy(portid_t port_id, uint32_t tunnel_id)
    1363                 :            : {
    1364                 :          0 :         struct rte_port *port = &ports[port_id];
    1365                 :            :         struct port_flow_tunnel *flt;
    1366                 :            : 
    1367                 :          0 :         LIST_FOREACH(flt, &port->flow_tunnel_list, chain) {
    1368                 :          0 :                 if (flt->id == tunnel_id)
    1369                 :            :                         break;
    1370                 :            :         }
    1371                 :          0 :         if (flt) {
    1372                 :          0 :                 LIST_REMOVE(flt, chain);
    1373                 :          0 :                 free(flt);
    1374                 :          0 :                 printf("port %u: flow tunnel #%u destroyed\n",
    1375                 :            :                         port_id, tunnel_id);
    1376                 :            :         }
    1377                 :          0 : }
    1378                 :            : 
    1379                 :          0 : void port_flow_tunnel_create(portid_t port_id, const struct tunnel_ops *ops)
    1380                 :            : {
    1381                 :          0 :         struct rte_port *port = &ports[port_id];
    1382                 :            :         enum rte_flow_item_type type;
    1383                 :            :         struct port_flow_tunnel *flt;
    1384                 :            : 
    1385                 :          0 :         if (!strcmp(ops->type, "vxlan"))
    1386                 :            :                 type = RTE_FLOW_ITEM_TYPE_VXLAN;
    1387                 :          0 :         else if (!strcmp(ops->type, "gre"))
    1388                 :            :                 type = RTE_FLOW_ITEM_TYPE_GRE;
    1389                 :          0 :         else if (!strcmp(ops->type, "nvgre"))
    1390                 :            :                 type = RTE_FLOW_ITEM_TYPE_NVGRE;
    1391                 :          0 :         else if (!strcmp(ops->type, "geneve"))
    1392                 :            :                 type = RTE_FLOW_ITEM_TYPE_GENEVE;
    1393                 :            :         else {
    1394                 :          0 :                 fprintf(stderr, "cannot offload \"%s\" tunnel type\n",
    1395                 :            :                         ops->type);
    1396                 :          0 :                 return;
    1397                 :            :         }
    1398                 :          0 :         LIST_FOREACH(flt, &port->flow_tunnel_list, chain) {
    1399                 :          0 :                 if (flt->tunnel.type == type)
    1400                 :            :                         break;
    1401                 :            :         }
    1402                 :          0 :         if (!flt) {
    1403                 :          0 :                 flt = calloc(1, sizeof(*flt));
    1404                 :          0 :                 if (!flt) {
    1405                 :          0 :                         fprintf(stderr, "failed to allocate port flt object\n");
    1406                 :          0 :                         return;
    1407                 :            :                 }
    1408                 :          0 :                 flt->tunnel.type = type;
    1409                 :          0 :                 flt->id = LIST_EMPTY(&port->flow_tunnel_list) ? 1 :
    1410                 :          0 :                                   LIST_FIRST(&port->flow_tunnel_list)->id + 1;
    1411                 :          0 :                 LIST_INSERT_HEAD(&port->flow_tunnel_list, flt, chain);
    1412                 :            :         }
    1413                 :          0 :         printf("port %d: flow tunnel #%u type %s\n",
    1414                 :            :                 port_id, flt->id, ops->type);
    1415                 :            : }
    1416                 :            : 
    1417                 :            : /** Generate a port_flow entry from attributes/pattern/actions. */
    1418                 :            : static struct port_flow *
    1419                 :          0 : port_flow_new(const struct rte_flow_attr *attr,
    1420                 :            :               const struct rte_flow_item *pattern,
    1421                 :            :               const struct rte_flow_action *actions,
    1422                 :            :               struct rte_flow_error *error)
    1423                 :            : {
    1424                 :          0 :         const struct rte_flow_conv_rule rule = {
    1425                 :            :                 .attr_ro = attr,
    1426                 :            :                 .pattern_ro = pattern,
    1427                 :            :                 .actions_ro = actions,
    1428                 :            :         };
    1429                 :            :         struct port_flow *pf;
    1430                 :            :         int ret;
    1431                 :            : 
    1432                 :          0 :         ret = rte_flow_conv(RTE_FLOW_CONV_OP_RULE, NULL, 0, &rule, error);
    1433                 :          0 :         if (ret < 0)
    1434                 :            :                 return NULL;
    1435                 :          0 :         pf = calloc(1, offsetof(struct port_flow, rule) + ret);
    1436                 :          0 :         if (!pf) {
    1437                 :          0 :                 rte_flow_error_set
    1438                 :          0 :                         (error, errno, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
    1439                 :            :                          "calloc() failed");
    1440                 :          0 :                 return NULL;
    1441                 :            :         }
    1442                 :          0 :         if (rte_flow_conv(RTE_FLOW_CONV_OP_RULE, &pf->rule, ret, &rule,
    1443                 :            :                           error) >= 0)
    1444                 :            :                 return pf;
    1445                 :          0 :         free(pf);
    1446                 :          0 :         return NULL;
    1447                 :            : }
    1448                 :            : 
    1449                 :            : static struct port_flow *
    1450                 :            : port_flow_locate(struct port_flow *flows_list, uint32_t flow_id)
    1451                 :            : {
    1452                 :            :         struct port_flow *pf = flows_list;
    1453                 :            : 
    1454                 :          0 :         while (pf) {
    1455                 :          0 :                 if (pf->id == flow_id)
    1456                 :            :                         break;
    1457                 :          0 :                 pf = pf->next;
    1458                 :            :         }
    1459                 :            :         return pf;
    1460                 :            : }
    1461                 :            : 
    1462                 :            : /** Print a message out of a flow error. */
    1463                 :            : static int
    1464                 :          0 : port_flow_complain(struct rte_flow_error *error)
    1465                 :            : {
    1466                 :            :         static const char *const errstrlist[] = {
    1467                 :            :                 [RTE_FLOW_ERROR_TYPE_NONE] = "no error",
    1468                 :            :                 [RTE_FLOW_ERROR_TYPE_UNSPECIFIED] = "cause unspecified",
    1469                 :            :                 [RTE_FLOW_ERROR_TYPE_HANDLE] = "flow rule (handle)",
    1470                 :            :                 [RTE_FLOW_ERROR_TYPE_ATTR_GROUP] = "group field",
    1471                 :            :                 [RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY] = "priority field",
    1472                 :            :                 [RTE_FLOW_ERROR_TYPE_ATTR_INGRESS] = "ingress field",
    1473                 :            :                 [RTE_FLOW_ERROR_TYPE_ATTR_EGRESS] = "egress field",
    1474                 :            :                 [RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER] = "transfer field",
    1475                 :            :                 [RTE_FLOW_ERROR_TYPE_ATTR] = "attributes structure",
    1476                 :            :                 [RTE_FLOW_ERROR_TYPE_ITEM_NUM] = "pattern length",
    1477                 :            :                 [RTE_FLOW_ERROR_TYPE_ITEM_SPEC] = "item specification",
    1478                 :            :                 [RTE_FLOW_ERROR_TYPE_ITEM_LAST] = "item specification range",
    1479                 :            :                 [RTE_FLOW_ERROR_TYPE_ITEM_MASK] = "item specification mask",
    1480                 :            :                 [RTE_FLOW_ERROR_TYPE_ITEM] = "specific pattern item",
    1481                 :            :                 [RTE_FLOW_ERROR_TYPE_ACTION_NUM] = "number of actions",
    1482                 :            :                 [RTE_FLOW_ERROR_TYPE_ACTION_CONF] = "action configuration",
    1483                 :            :                 [RTE_FLOW_ERROR_TYPE_ACTION] = "specific action",
    1484                 :            :         };
    1485                 :            :         const char *errstr;
    1486                 :            :         char buf[32];
    1487                 :          0 :         int err = rte_errno;
    1488                 :            : 
    1489                 :          0 :         if ((unsigned int)error->type >= RTE_DIM(errstrlist) ||
    1490                 :          0 :             !errstrlist[error->type])
    1491                 :            :                 errstr = "unknown type";
    1492                 :            :         else
    1493                 :            :                 errstr = errstrlist[error->type];
    1494                 :          0 :         fprintf(stderr, "%s(): Caught PMD error type %d (%s): %s%s: %s\n",
    1495                 :          0 :                 __func__, error->type, errstr,
    1496                 :          0 :                 error->cause ? (snprintf(buf, sizeof(buf), "cause: %p, ",
    1497                 :            :                                          error->cause), buf) : "",
    1498                 :          0 :                 error->message ? error->message : "(no stated reason)",
    1499                 :            :                 rte_strerror(err));
    1500                 :            : 
    1501                 :          0 :         switch (error->type) {
    1502                 :          0 :         case RTE_FLOW_ERROR_TYPE_ATTR_TRANSFER:
    1503                 :          0 :                 fprintf(stderr, "The status suggests the use of \"transfer\" "
    1504                 :            :                                 "as the possible cause of the failure. Make "
    1505                 :            :                                 "sure that the flow in question and its "
    1506                 :            :                                 "indirect components (if any) are managed "
    1507                 :            :                                 "via \"transfer\" proxy port. Use command "
    1508                 :            :                                 "\"show port (port_id) flow transfer proxy\" "
    1509                 :            :                                 "to figure out the proxy port ID\n");
    1510                 :            :                 break;
    1511                 :            :         default:
    1512                 :            :                 break;
    1513                 :            :         }
    1514                 :            : 
    1515                 :          0 :         return -err;
    1516                 :            : }
    1517                 :            : 
    1518                 :            : static void
    1519                 :          0 : rss_types_display(uint64_t rss_types, uint16_t char_num_per_line)
    1520                 :            : {
    1521                 :            :         uint16_t total_len = 0;
    1522                 :            :         uint16_t str_len;
    1523                 :            :         uint16_t i;
    1524                 :            : 
    1525                 :          0 :         if (rss_types == 0)
    1526                 :            :                 return;
    1527                 :            : 
    1528                 :          0 :         for (i = 0; rss_type_table[i].str; i++) {
    1529                 :          0 :                 if (rss_type_table[i].rss_type == 0)
    1530                 :          0 :                         continue;
    1531                 :            : 
    1532                 :          0 :                 if ((rss_types & rss_type_table[i].rss_type) ==
    1533                 :            :                                         rss_type_table[i].rss_type) {
    1534                 :            :                         /* Contain two spaces */
    1535                 :          0 :                         str_len = strlen(rss_type_table[i].str) + 2;
    1536                 :          0 :                         if (total_len + str_len > char_num_per_line) {
    1537                 :            :                                 printf("\n");
    1538                 :            :                                 total_len = 0;
    1539                 :            :                         }
    1540                 :            :                         printf("  %s", rss_type_table[i].str);
    1541                 :          0 :                         total_len += str_len;
    1542                 :            :                 }
    1543                 :            :         }
    1544                 :            :         printf("\n");
    1545                 :            : }
    1546                 :            : 
    1547                 :            : static void
    1548                 :          0 : rss_config_display(struct rte_flow_action_rss *rss_conf)
    1549                 :            : {
    1550                 :            :         uint8_t i;
    1551                 :            : 
    1552                 :          0 :         if (rss_conf == NULL) {
    1553                 :          0 :                 fprintf(stderr, "Invalid rule\n");
    1554                 :          0 :                 return;
    1555                 :            :         }
    1556                 :            : 
    1557                 :            :         printf("RSS:\n"
    1558                 :            :                " queues:");
    1559                 :          0 :         if (rss_conf->queue_num == 0)
    1560                 :            :                 printf(" none");
    1561                 :          0 :         for (i = 0; i < rss_conf->queue_num; i++)
    1562                 :          0 :                 printf(" %d", rss_conf->queue[i]);
    1563                 :            :         printf("\n");
    1564                 :            : 
    1565                 :          0 :         printf(" function: %s\n", rte_eth_dev_rss_algo_name(rss_conf->func));
    1566                 :            : 
    1567                 :            :         printf(" RSS key:\n");
    1568                 :          0 :         if (rss_conf->key_len == 0) {
    1569                 :            :                 printf("  none");
    1570                 :            :         } else {
    1571                 :            :                 printf("  key_len: %u\n", rss_conf->key_len);
    1572                 :            :                 printf("  key: ");
    1573                 :          0 :                 if (rss_conf->key == NULL) {
    1574                 :            :                         printf("none");
    1575                 :            :                 } else {
    1576                 :          0 :                         for (i = 0; i < rss_conf->key_len; i++)
    1577                 :          0 :                                 printf("%02X", rss_conf->key[i]);
    1578                 :            :                 }
    1579                 :            :         }
    1580                 :            :         printf("\n");
    1581                 :            : 
    1582                 :            :         printf(" types:\n");
    1583                 :          0 :         if (rss_conf->types == 0) {
    1584                 :            :                 printf("  none\n");
    1585                 :          0 :                 return;
    1586                 :            :         }
    1587                 :          0 :         rss_types_display(rss_conf->types, TESTPMD_RSS_TYPES_CHAR_NUM_PER_LINE);
    1588                 :            : }
    1589                 :            : 
    1590                 :            : static struct port_indirect_action *
    1591                 :          0 : action_get_by_id(portid_t port_id, uint32_t id)
    1592                 :            : {
    1593                 :            :         struct rte_port *port;
    1594                 :            :         struct port_indirect_action **ppia;
    1595                 :            :         struct port_indirect_action *pia = NULL;
    1596                 :            : 
    1597                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    1598                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    1599                 :            :                 return NULL;
    1600                 :          0 :         port = &ports[port_id];
    1601                 :          0 :         ppia = &port->actions_list;
    1602                 :          0 :         while (*ppia) {
    1603                 :          0 :                 if ((*ppia)->id == id) {
    1604                 :            :                         pia = *ppia;
    1605                 :            :                         break;
    1606                 :            :                 }
    1607                 :          0 :                 ppia = &(*ppia)->next;
    1608                 :            :         }
    1609                 :          0 :         if (!pia)
    1610                 :          0 :                 fprintf(stderr,
    1611                 :            :                         "Failed to find indirect action #%u on port %u\n",
    1612                 :            :                         id, port_id);
    1613                 :            :         return pia;
    1614                 :            : }
    1615                 :            : 
    1616                 :            : static int
    1617                 :          0 : action_alloc(portid_t port_id, uint32_t id,
    1618                 :            :              struct port_indirect_action **action)
    1619                 :            : {
    1620                 :            :         struct rte_port *port;
    1621                 :            :         struct port_indirect_action **ppia;
    1622                 :            :         struct port_indirect_action *pia = NULL;
    1623                 :            : 
    1624                 :          0 :         *action = NULL;
    1625                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    1626                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    1627                 :            :                 return -EINVAL;
    1628                 :          0 :         port = &ports[port_id];
    1629                 :          0 :         if (id == UINT32_MAX) {
    1630                 :            :                 /* taking first available ID */
    1631                 :          0 :                 if (port->actions_list) {
    1632                 :          0 :                         if (port->actions_list->id == UINT32_MAX - 1) {
    1633                 :          0 :                                 fprintf(stderr,
    1634                 :            :                                         "Highest indirect action ID is already assigned, delete it first\n");
    1635                 :          0 :                                 return -ENOMEM;
    1636                 :            :                         }
    1637                 :          0 :                         id = port->actions_list->id + 1;
    1638                 :            :                 } else {
    1639                 :            :                         id = 0;
    1640                 :            :                 }
    1641                 :            :         }
    1642                 :          0 :         pia = calloc(1, sizeof(*pia));
    1643                 :          0 :         if (!pia) {
    1644                 :          0 :                 fprintf(stderr,
    1645                 :            :                         "Allocation of port %u indirect action failed\n",
    1646                 :            :                         port_id);
    1647                 :          0 :                 return -ENOMEM;
    1648                 :            :         }
    1649                 :          0 :         ppia = &port->actions_list;
    1650                 :          0 :         while (*ppia && (*ppia)->id > id)
    1651                 :          0 :                 ppia = &(*ppia)->next;
    1652                 :          0 :         if (*ppia && (*ppia)->id == id) {
    1653                 :          0 :                 fprintf(stderr,
    1654                 :            :                         "Indirect action #%u is already assigned, delete it first\n",
    1655                 :            :                         id);
    1656                 :          0 :                 free(pia);
    1657                 :          0 :                 return -EINVAL;
    1658                 :            :         }
    1659                 :          0 :         pia->next = *ppia;
    1660                 :          0 :         pia->id = id;
    1661                 :          0 :         *ppia = pia;
    1662                 :          0 :         *action = pia;
    1663                 :          0 :         return 0;
    1664                 :            : }
    1665                 :            : 
    1666                 :            : static int
    1667                 :          0 : template_alloc(uint32_t id, struct port_template **template,
    1668                 :            :                struct port_template **list)
    1669                 :            : {
    1670                 :          0 :         struct port_template *lst = *list;
    1671                 :            :         struct port_template **ppt;
    1672                 :            :         struct port_template *pt = NULL;
    1673                 :            : 
    1674                 :          0 :         *template = NULL;
    1675                 :          0 :         if (id == UINT32_MAX) {
    1676                 :            :                 /* taking first available ID */
    1677                 :          0 :                 if (lst) {
    1678                 :          0 :                         if (lst->id == UINT32_MAX - 1) {
    1679                 :            :                                 printf("Highest template ID is already"
    1680                 :            :                                 " assigned, delete it first\n");
    1681                 :          0 :                                 return -ENOMEM;
    1682                 :            :                         }
    1683                 :          0 :                         id = lst->id + 1;
    1684                 :            :                 } else {
    1685                 :            :                         id = 0;
    1686                 :            :                 }
    1687                 :            :         }
    1688                 :          0 :         pt = calloc(1, sizeof(*pt));
    1689                 :          0 :         if (!pt) {
    1690                 :            :                 printf("Allocation of port template failed\n");
    1691                 :          0 :                 return -ENOMEM;
    1692                 :            :         }
    1693                 :            :         ppt = list;
    1694                 :          0 :         while (*ppt && (*ppt)->id > id)
    1695                 :          0 :                 ppt = &(*ppt)->next;
    1696                 :          0 :         if (*ppt && (*ppt)->id == id) {
    1697                 :            :                 printf("Template #%u is already assigned,"
    1698                 :            :                         " delete it first\n", id);
    1699                 :          0 :                 free(pt);
    1700                 :          0 :                 return -EINVAL;
    1701                 :            :         }
    1702                 :          0 :         pt->next = *ppt;
    1703                 :          0 :         pt->id = id;
    1704                 :          0 :         *ppt = pt;
    1705                 :          0 :         *template = pt;
    1706                 :          0 :         return 0;
    1707                 :            : }
    1708                 :            : 
    1709                 :            : static int
    1710                 :          0 : table_alloc(uint32_t id, struct port_table **table,
    1711                 :            :             struct port_table **list)
    1712                 :            : {
    1713                 :          0 :         struct port_table *lst = *list;
    1714                 :            :         struct port_table **ppt;
    1715                 :            :         struct port_table *pt = NULL;
    1716                 :            : 
    1717                 :          0 :         *table = NULL;
    1718                 :          0 :         if (id == UINT32_MAX) {
    1719                 :            :                 /* taking first available ID */
    1720                 :          0 :                 if (lst) {
    1721                 :          0 :                         if (lst->id == UINT32_MAX - 1) {
    1722                 :            :                                 printf("Highest table ID is already"
    1723                 :            :                                 " assigned, delete it first\n");
    1724                 :          0 :                                 return -ENOMEM;
    1725                 :            :                         }
    1726                 :          0 :                         id = lst->id + 1;
    1727                 :            :                 } else {
    1728                 :            :                         id = 0;
    1729                 :            :                 }
    1730                 :            :         }
    1731                 :          0 :         pt = calloc(1, sizeof(*pt));
    1732                 :          0 :         if (!pt) {
    1733                 :            :                 printf("Allocation of table failed\n");
    1734                 :          0 :                 return -ENOMEM;
    1735                 :            :         }
    1736                 :            :         ppt = list;
    1737                 :          0 :         while (*ppt && (*ppt)->id > id)
    1738                 :          0 :                 ppt = &(*ppt)->next;
    1739                 :          0 :         if (*ppt && (*ppt)->id == id) {
    1740                 :            :                 printf("Table #%u is already assigned,"
    1741                 :            :                         " delete it first\n", id);
    1742                 :          0 :                 free(pt);
    1743                 :          0 :                 return -EINVAL;
    1744                 :            :         }
    1745                 :          0 :         pt->next = *ppt;
    1746                 :          0 :         pt->id = id;
    1747                 :          0 :         *ppt = pt;
    1748                 :          0 :         *table = pt;
    1749                 :          0 :         return 0;
    1750                 :            : }
    1751                 :            : 
    1752                 :            : static struct port_table *
    1753                 :            : port_table_locate(struct port_table *tables_list, uint32_t table_id)
    1754                 :            : {
    1755                 :            :         struct port_table *pt = tables_list;
    1756                 :            : 
    1757                 :          0 :         while (pt) {
    1758                 :          0 :                 if (pt->id == table_id)
    1759                 :            :                         break;
    1760                 :          0 :                 pt = pt->next;
    1761                 :            :         }
    1762                 :            :         return pt;
    1763                 :            : }
    1764                 :            : 
    1765                 :            : /** Get info about flow management resources. */
    1766                 :            : int
    1767                 :          0 : port_flow_get_info(portid_t port_id)
    1768                 :            : {
    1769                 :            :         struct rte_flow_port_info port_info;
    1770                 :            :         struct rte_flow_queue_info queue_info;
    1771                 :            :         struct rte_flow_error error;
    1772                 :            : 
    1773                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    1774                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    1775                 :            :                 return -EINVAL;
    1776                 :            :         /* Poisoning to make sure PMDs update it in case of error. */
    1777                 :            :         memset(&error, 0x99, sizeof(error));
    1778                 :            :         memset(&port_info, 0, sizeof(port_info));
    1779                 :            :         memset(&queue_info, 0, sizeof(queue_info));
    1780                 :          0 :         if (rte_flow_info_get(port_id, &port_info, &queue_info, &error))
    1781                 :          0 :                 return port_flow_complain(&error);
    1782                 :          0 :         printf("Flow engine resources on port %u:\n"
    1783                 :            :                "Number of queues: %d\n"
    1784                 :            :                    "Size of queues: %d\n"
    1785                 :            :                "Number of counters: %d\n"
    1786                 :            :                "Number of aging objects: %d\n"
    1787                 :            :                "Number of meter actions: %d\n",
    1788                 :            :                port_id, port_info.max_nb_queues,
    1789                 :            :                    queue_info.max_size,
    1790                 :            :                port_info.max_nb_counters,
    1791                 :            :                port_info.max_nb_aging_objects,
    1792                 :            :                port_info.max_nb_meters);
    1793                 :          0 :         return 0;
    1794                 :            : }
    1795                 :            : 
    1796                 :            : /** Configure flow management resources. */
    1797                 :            : int
    1798                 :          0 : port_flow_configure(portid_t port_id,
    1799                 :            :         const struct rte_flow_port_attr *port_attr,
    1800                 :            :         uint16_t nb_queue,
    1801                 :            :         const struct rte_flow_queue_attr *queue_attr)
    1802                 :          0 : {
    1803                 :            :         struct rte_port *port;
    1804                 :            :         struct rte_flow_error error;
    1805                 :          0 :         const struct rte_flow_queue_attr *attr_list[nb_queue];
    1806                 :            :         int std_queue;
    1807                 :            : 
    1808                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    1809                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    1810                 :            :                 return -EINVAL;
    1811                 :          0 :         port = &ports[port_id];
    1812                 :          0 :         port->queue_nb = nb_queue;
    1813                 :          0 :         port->queue_sz = queue_attr->size;
    1814                 :          0 :         for (std_queue = 0; std_queue < nb_queue; std_queue++)
    1815                 :          0 :                 attr_list[std_queue] = queue_attr;
    1816                 :            :         /* Poisoning to make sure PMDs update it in case of error. */
    1817                 :            :         memset(&error, 0x66, sizeof(error));
    1818                 :          0 :         if (rte_flow_configure(port_id, port_attr, nb_queue, attr_list, &error))
    1819                 :          0 :                 return port_flow_complain(&error);
    1820                 :            :         printf("Configure flows on port %u: "
    1821                 :            :                "number of queues %d with %d elements\n",
    1822                 :          0 :                port_id, nb_queue, queue_attr->size);
    1823                 :          0 :         return 0;
    1824                 :            : }
    1825                 :            : 
    1826                 :            : static int
    1827                 :          0 : action_handle_create(portid_t port_id,
    1828                 :            :                      struct port_indirect_action *pia,
    1829                 :            :                      const struct rte_flow_indir_action_conf *conf,
    1830                 :            :                      const struct rte_flow_action *action,
    1831                 :            :                      struct rte_flow_error *error)
    1832                 :            : {
    1833                 :          0 :         if (action->type == RTE_FLOW_ACTION_TYPE_AGE) {
    1834                 :          0 :                 struct rte_flow_action_age *age =
    1835                 :            :                         (struct rte_flow_action_age *)(uintptr_t)(action->conf);
    1836                 :            : 
    1837                 :          0 :                 pia->age_type = ACTION_AGE_CONTEXT_TYPE_INDIRECT_ACTION;
    1838                 :          0 :                 age->context = &pia->age_type;
    1839                 :          0 :         } else if (action->type == RTE_FLOW_ACTION_TYPE_CONNTRACK) {
    1840                 :          0 :                 struct rte_flow_action_conntrack *ct =
    1841                 :            :                         (struct rte_flow_action_conntrack *)(uintptr_t)(action->conf);
    1842                 :            : 
    1843                 :            :                 memcpy(ct, &conntrack_context, sizeof(*ct));
    1844                 :            :         }
    1845                 :          0 :         pia->type = action->type;
    1846                 :          0 :         pia->handle = rte_flow_action_handle_create(port_id, conf, action,
    1847                 :            :                                                     error);
    1848                 :          0 :         return pia->handle ? 0 : -1;
    1849                 :            : }
    1850                 :            : 
    1851                 :            : static int
    1852                 :            : action_list_handle_create(portid_t port_id,
    1853                 :            :                           struct port_indirect_action *pia,
    1854                 :            :                           const struct rte_flow_indir_action_conf *conf,
    1855                 :            :                           const struct rte_flow_action *actions,
    1856                 :            :                           struct rte_flow_error *error)
    1857                 :            : {
    1858                 :          0 :         pia->type = RTE_FLOW_ACTION_TYPE_INDIRECT_LIST;
    1859                 :          0 :         pia->list_handle =
    1860                 :          0 :                 rte_flow_action_list_handle_create(port_id, conf,
    1861                 :            :                                                    actions, error);
    1862                 :          0 :         return pia->list_handle ? 0 : -1;
    1863                 :            : }
    1864                 :            : /** Create indirect action */
    1865                 :            : int
    1866                 :          0 : port_action_handle_create(portid_t port_id, uint32_t id, bool indirect_list,
    1867                 :            :                           const struct rte_flow_indir_action_conf *conf,
    1868                 :            :                           const struct rte_flow_action *action)
    1869                 :            : {
    1870                 :            :         struct port_indirect_action *pia;
    1871                 :            :         int ret;
    1872                 :            :         struct rte_flow_error error;
    1873                 :            : 
    1874                 :          0 :         ret = action_alloc(port_id, id, &pia);
    1875                 :          0 :         if (ret)
    1876                 :            :                 return ret;
    1877                 :            :         /* Poisoning to make sure PMDs update it in case of error. */
    1878                 :            :         memset(&error, 0x22, sizeof(error));
    1879                 :            :         ret = indirect_list ?
    1880                 :          0 :                action_list_handle_create(port_id, pia, conf, action, &error) :
    1881                 :          0 :                action_handle_create(port_id, pia, conf, action, &error);
    1882                 :          0 :         if (ret) {
    1883                 :          0 :                 uint32_t destroy_id = pia->id;
    1884                 :          0 :                 port_action_handle_destroy(port_id, 1, &destroy_id);
    1885                 :          0 :                 return port_flow_complain(&error);
    1886                 :            :         }
    1887                 :          0 :         printf("Indirect action #%u created\n", pia->id);
    1888                 :          0 :         return 0;
    1889                 :            : }
    1890                 :            : 
    1891                 :            : /** Destroy indirect action */
    1892                 :            : int
    1893                 :          0 : port_action_handle_destroy(portid_t port_id,
    1894                 :            :                            uint32_t n,
    1895                 :            :                            const uint32_t *actions)
    1896                 :            : {
    1897                 :            :         struct rte_port *port;
    1898                 :            :         struct port_indirect_action **tmp;
    1899                 :            :         int ret = 0;
    1900                 :            : 
    1901                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    1902                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    1903                 :            :                 return -EINVAL;
    1904                 :          0 :         port = &ports[port_id];
    1905                 :          0 :         tmp = &port->actions_list;
    1906                 :          0 :         while (*tmp) {
    1907                 :            :                 uint32_t i;
    1908                 :            : 
    1909                 :          0 :                 for (i = 0; i != n; ++i) {
    1910                 :            :                         struct rte_flow_error error;
    1911                 :          0 :                         struct port_indirect_action *pia = *tmp;
    1912                 :            : 
    1913                 :          0 :                         if (actions[i] != pia->id)
    1914                 :          0 :                                 continue;
    1915                 :            :                         /*
    1916                 :            :                          * Poisoning to make sure PMDs update it in case
    1917                 :            :                          * of error.
    1918                 :            :                          */
    1919                 :            :                         memset(&error, 0x33, sizeof(error));
    1920                 :            : 
    1921                 :          0 :                         if (pia->handle) {
    1922                 :          0 :                                 ret = pia->type ==
    1923                 :            :                                       RTE_FLOW_ACTION_TYPE_INDIRECT_LIST ?
    1924                 :          0 :                                         rte_flow_action_list_handle_destroy
    1925                 :          0 :                                         (port_id, pia->list_handle, &error) :
    1926                 :          0 :                                         rte_flow_action_handle_destroy
    1927                 :            :                                         (port_id, pia->handle, &error);
    1928                 :          0 :                                 if (ret) {
    1929                 :          0 :                                         ret = port_flow_complain(&error);
    1930                 :          0 :                                         continue;
    1931                 :            :                                 }
    1932                 :            :                         }
    1933                 :          0 :                         *tmp = pia->next;
    1934                 :          0 :                         printf("Indirect action #%u destroyed\n", pia->id);
    1935                 :          0 :                         free(pia);
    1936                 :          0 :                         break;
    1937                 :            :                 }
    1938                 :          0 :                 if (i == n)
    1939                 :          0 :                         tmp = &(*tmp)->next;
    1940                 :            :         }
    1941                 :            :         return ret;
    1942                 :            : }
    1943                 :            : 
    1944                 :            : int
    1945                 :          0 : port_action_handle_flush(portid_t port_id)
    1946                 :            : {
    1947                 :            :         struct rte_port *port;
    1948                 :            :         struct port_indirect_action **tmp;
    1949                 :            :         int ret = 0;
    1950                 :            : 
    1951                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    1952                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    1953                 :            :                 return -EINVAL;
    1954                 :          0 :         port = &ports[port_id];
    1955                 :            :         tmp = &port->actions_list;
    1956                 :          0 :         while (*tmp != NULL) {
    1957                 :            :                 struct rte_flow_error error;
    1958                 :            :                 struct port_indirect_action *pia = *tmp;
    1959                 :            : 
    1960                 :            :                 /* Poisoning to make sure PMDs update it in case of error. */
    1961                 :            :                 memset(&error, 0x44, sizeof(error));
    1962                 :          0 :                 if (pia->handle != NULL) {
    1963                 :          0 :                         ret = pia->type == RTE_FLOW_ACTION_TYPE_INDIRECT_LIST ?
    1964                 :          0 :                               rte_flow_action_list_handle_destroy
    1965                 :          0 :                                       (port_id, pia->list_handle, &error) :
    1966                 :          0 :                               rte_flow_action_handle_destroy
    1967                 :            :                                       (port_id, pia->handle, &error);
    1968                 :          0 :                         if (ret) {
    1969                 :          0 :                                 printf("Indirect action #%u not destroyed\n",
    1970                 :            :                                        pia->id);
    1971                 :          0 :                                 ret = port_flow_complain(&error);
    1972                 :            :                         }
    1973                 :            :                 }
    1974                 :          0 :                 *tmp = pia->next;
    1975                 :          0 :                 free(pia);
    1976                 :            :         }
    1977                 :            :         return ret;
    1978                 :            : }
    1979                 :            : 
    1980                 :            : /** Get indirect action by port + id */
    1981                 :            : struct rte_flow_action_handle *
    1982                 :          0 : port_action_handle_get_by_id(portid_t port_id, uint32_t id)
    1983                 :            : {
    1984                 :            : 
    1985                 :          0 :         struct port_indirect_action *pia = action_get_by_id(port_id, id);
    1986                 :            : 
    1987                 :          0 :         return (pia) ? pia->handle : NULL;
    1988                 :            : }
    1989                 :            : 
    1990                 :            : /** Update indirect action */
    1991                 :            : int
    1992                 :          0 : port_action_handle_update(portid_t port_id, uint32_t id,
    1993                 :            :                           const struct rte_flow_action *action)
    1994                 :            : {
    1995                 :            :         struct rte_flow_error error;
    1996                 :            :         struct rte_flow_action_handle *action_handle;
    1997                 :            :         struct port_indirect_action *pia;
    1998                 :            :         struct rte_flow_update_meter_mark mtr_update;
    1999                 :            :         const void *update;
    2000                 :            : 
    2001                 :          0 :         action_handle = port_action_handle_get_by_id(port_id, id);
    2002                 :          0 :         if (!action_handle)
    2003                 :            :                 return -EINVAL;
    2004                 :          0 :         pia = action_get_by_id(port_id, id);
    2005                 :          0 :         if (!pia)
    2006                 :            :                 return -EINVAL;
    2007                 :          0 :         switch (pia->type) {
    2008                 :          0 :         case RTE_FLOW_ACTION_TYPE_AGE:
    2009                 :            :         case RTE_FLOW_ACTION_TYPE_CONNTRACK:
    2010                 :          0 :                 update = action->conf;
    2011                 :          0 :                 break;
    2012                 :          0 :         case RTE_FLOW_ACTION_TYPE_METER_MARK:
    2013                 :          0 :                 memcpy(&mtr_update.meter_mark, action->conf,
    2014                 :            :                        sizeof(struct rte_flow_action_meter_mark));
    2015                 :          0 :                 if (mtr_update.meter_mark.profile)
    2016                 :          0 :                         mtr_update.profile_valid = 1;
    2017                 :          0 :                 if (mtr_update.meter_mark.policy)
    2018                 :          0 :                         mtr_update.policy_valid = 1;
    2019                 :          0 :                 mtr_update.color_mode_valid = 1;
    2020                 :          0 :                 mtr_update.state_valid = 1;
    2021                 :            :                 update = &mtr_update;
    2022                 :          0 :                 break;
    2023                 :            :         default:
    2024                 :            :                 update = action;
    2025                 :            :                 break;
    2026                 :            :         }
    2027                 :          0 :         if (rte_flow_action_handle_update(port_id, action_handle, update,
    2028                 :            :                                           &error)) {
    2029                 :          0 :                 return port_flow_complain(&error);
    2030                 :            :         }
    2031                 :            :         printf("Indirect action #%u updated\n", id);
    2032                 :          0 :         return 0;
    2033                 :            : }
    2034                 :            : 
    2035                 :            : static void
    2036                 :          0 : port_action_handle_query_dump(portid_t port_id,
    2037                 :            :                               const struct port_indirect_action *pia,
    2038                 :            :                               union port_action_query *query)
    2039                 :            : {
    2040                 :          0 :         if (!pia || !query)
    2041                 :            :                 return;
    2042                 :          0 :         switch (pia->type) {
    2043                 :          0 :         case RTE_FLOW_ACTION_TYPE_AGE:
    2044                 :          0 :                 printf("Indirect AGE action:\n"
    2045                 :            :                        " aged: %u\n"
    2046                 :            :                        " sec_since_last_hit_valid: %u\n"
    2047                 :            :                        " sec_since_last_hit: %" PRIu32 "\n",
    2048                 :          0 :                        query->age.aged,
    2049                 :          0 :                        query->age.sec_since_last_hit_valid,
    2050                 :          0 :                        query->age.sec_since_last_hit);
    2051                 :            :                 break;
    2052                 :          0 :         case RTE_FLOW_ACTION_TYPE_COUNT:
    2053                 :          0 :                 printf("Indirect COUNT action:\n"
    2054                 :            :                        " hits_set: %u\n"
    2055                 :            :                        " bytes_set: %u\n"
    2056                 :            :                        " hits: %" PRIu64 "\n"
    2057                 :            :                        " bytes: %" PRIu64 "\n",
    2058                 :          0 :                        query->count.hits_set,
    2059                 :          0 :                        query->count.bytes_set,
    2060                 :            :                        query->count.hits,
    2061                 :            :                        query->count.bytes);
    2062                 :            :                 break;
    2063                 :          0 :         case RTE_FLOW_ACTION_TYPE_CONNTRACK:
    2064                 :          0 :                 printf("Conntrack Context:\n"
    2065                 :            :                        "  Peer: %u, Flow dir: %s, Enable: %u\n"
    2066                 :            :                        "  Live: %u, SACK: %u, CACK: %u\n"
    2067                 :            :                        "  Packet dir: %s, Liberal: %u, State: %u\n"
    2068                 :            :                        "  Factor: %u, Retrans: %u, TCP flags: %u\n"
    2069                 :            :                        "  Last Seq: %u, Last ACK: %u\n"
    2070                 :            :                        "  Last Win: %u, Last End: %u\n",
    2071                 :          0 :                        query->ct.peer_port,
    2072                 :          0 :                        query->ct.is_original_dir ? "Original" : "Reply",
    2073                 :          0 :                        query->ct.enable, query->ct.live_connection,
    2074                 :          0 :                        query->ct.selective_ack, query->ct.challenge_ack_passed,
    2075                 :          0 :                        query->ct.last_direction ? "Original" : "Reply",
    2076                 :          0 :                        query->ct.liberal_mode, query->ct.state,
    2077                 :          0 :                        query->ct.max_ack_window, query->ct.retransmission_limit,
    2078                 :          0 :                        query->ct.last_index, query->ct.last_seq,
    2079                 :          0 :                        query->ct.last_ack, query->ct.last_window,
    2080                 :            :                        query->ct.last_end);
    2081                 :          0 :                 printf("  Original Dir:\n"
    2082                 :            :                        "    scale: %u, fin: %u, ack seen: %u\n"
    2083                 :            :                        " unacked data: %u\n    Sent end: %u,"
    2084                 :            :                        "    Reply end: %u, Max win: %u, Max ACK: %u\n",
    2085                 :          0 :                        query->ct.original_dir.scale,
    2086                 :          0 :                        query->ct.original_dir.close_initiated,
    2087                 :          0 :                        query->ct.original_dir.last_ack_seen,
    2088                 :          0 :                        query->ct.original_dir.data_unacked,
    2089                 :            :                        query->ct.original_dir.sent_end,
    2090                 :            :                        query->ct.original_dir.reply_end,
    2091                 :            :                        query->ct.original_dir.max_win,
    2092                 :            :                        query->ct.original_dir.max_ack);
    2093                 :          0 :                 printf("  Reply Dir:\n"
    2094                 :            :                        "    scale: %u, fin: %u, ack seen: %u\n"
    2095                 :            :                        " unacked data: %u\n    Sent end: %u,"
    2096                 :            :                        "    Reply end: %u, Max win: %u, Max ACK: %u\n",
    2097                 :          0 :                        query->ct.reply_dir.scale,
    2098                 :          0 :                        query->ct.reply_dir.close_initiated,
    2099                 :          0 :                        query->ct.reply_dir.last_ack_seen,
    2100                 :          0 :                        query->ct.reply_dir.data_unacked,
    2101                 :            :                        query->ct.reply_dir.sent_end,
    2102                 :            :                        query->ct.reply_dir.reply_end,
    2103                 :            :                        query->ct.reply_dir.max_win,
    2104                 :            :                        query->ct.reply_dir.max_ack);
    2105                 :            :                 break;
    2106                 :          0 :         case RTE_FLOW_ACTION_TYPE_QUOTA:
    2107                 :          0 :                 printf("Indirect QUOTA action %u\n"
    2108                 :            :                        " unused quota: %" PRId64 "\n",
    2109                 :          0 :                        pia->id, query->quota.quota);
    2110                 :            :                 break;
    2111                 :          0 :         default:
    2112                 :          0 :                 printf("port-%u: indirect action %u (type: %d) doesn't support query\n",
    2113                 :          0 :                        pia->type, pia->id, port_id);
    2114                 :            :                 break;
    2115                 :            :         }
    2116                 :            : 
    2117                 :            : }
    2118                 :            : 
    2119                 :            : void
    2120                 :          0 : port_action_handle_query_update(portid_t port_id, uint32_t id,
    2121                 :            :                                 enum rte_flow_query_update_mode qu_mode,
    2122                 :            :                                 const struct rte_flow_action *action)
    2123                 :            : {
    2124                 :            :         int ret;
    2125                 :            :         struct rte_flow_error error;
    2126                 :            :         struct port_indirect_action *pia;
    2127                 :            :         union port_action_query query;
    2128                 :            : 
    2129                 :          0 :         pia = action_get_by_id(port_id, id);
    2130                 :          0 :         if (!pia || !pia->handle)
    2131                 :          0 :                 return;
    2132                 :          0 :         ret = rte_flow_action_handle_query_update(port_id, pia->handle, action,
    2133                 :            :                                                   &query, qu_mode, &error);
    2134                 :          0 :         if (ret)
    2135                 :          0 :                 port_flow_complain(&error);
    2136                 :            :         else
    2137                 :          0 :                 port_action_handle_query_dump(port_id, pia, &query);
    2138                 :            : 
    2139                 :            : }
    2140                 :            : 
    2141                 :            : int
    2142                 :          0 : port_action_handle_query(portid_t port_id, uint32_t id)
    2143                 :            : {
    2144                 :            :         struct rte_flow_error error;
    2145                 :            :         struct port_indirect_action *pia;
    2146                 :            :         union port_action_query query;
    2147                 :            : 
    2148                 :          0 :         pia = action_get_by_id(port_id, id);
    2149                 :          0 :         if (!pia)
    2150                 :            :                 return -EINVAL;
    2151                 :          0 :         switch (pia->type) {
    2152                 :            :         case RTE_FLOW_ACTION_TYPE_AGE:
    2153                 :            :         case RTE_FLOW_ACTION_TYPE_COUNT:
    2154                 :            :         case RTE_FLOW_ACTION_TYPE_QUOTA:
    2155                 :            :                 break;
    2156                 :          0 :         default:
    2157                 :          0 :                 fprintf(stderr,
    2158                 :            :                         "Indirect action %u (type: %d) on port %u doesn't support query\n",
    2159                 :            :                         id, pia->type, port_id);
    2160                 :          0 :                 return -ENOTSUP;
    2161                 :            :         }
    2162                 :            :         /* Poisoning to make sure PMDs update it in case of error. */
    2163                 :            :         memset(&error, 0x55, sizeof(error));
    2164                 :            :         memset(&query, 0, sizeof(query));
    2165                 :          0 :         if (rte_flow_action_handle_query(port_id, pia->handle, &query, &error))
    2166                 :          0 :                 return port_flow_complain(&error);
    2167                 :          0 :         port_action_handle_query_dump(port_id, pia, &query);
    2168                 :          0 :         return 0;
    2169                 :            : }
    2170                 :            : 
    2171                 :            : static struct port_flow_tunnel *
    2172                 :          0 : port_flow_tunnel_offload_cmd_prep(portid_t port_id,
    2173                 :            :                                   const struct rte_flow_item *pattern,
    2174                 :            :                                   const struct rte_flow_action *actions,
    2175                 :            :                                   const struct tunnel_ops *tunnel_ops)
    2176                 :            : {
    2177                 :            :         int ret;
    2178                 :            :         struct rte_port *port;
    2179                 :            :         struct port_flow_tunnel *pft;
    2180                 :            :         struct rte_flow_error error;
    2181                 :            : 
    2182                 :          0 :         port = &ports[port_id];
    2183                 :          0 :         pft = port_flow_locate_tunnel_id(port, tunnel_ops->id);
    2184                 :          0 :         if (!pft) {
    2185                 :          0 :                 fprintf(stderr, "failed to locate port flow tunnel #%u\n",
    2186                 :            :                         tunnel_ops->id);
    2187                 :          0 :                 return NULL;
    2188                 :            :         }
    2189                 :          0 :         if (tunnel_ops->actions) {
    2190                 :            :                 uint32_t num_actions;
    2191                 :            :                 const struct rte_flow_action *aptr;
    2192                 :            : 
    2193                 :          0 :                 ret = rte_flow_tunnel_decap_set(port_id, &pft->tunnel,
    2194                 :            :                                                 &pft->pmd_actions,
    2195                 :            :                                                 &pft->num_pmd_actions,
    2196                 :            :                                                 &error);
    2197                 :          0 :                 if (ret) {
    2198                 :          0 :                         port_flow_complain(&error);
    2199                 :          0 :                         return NULL;
    2200                 :            :                 }
    2201                 :            :                 for (aptr = actions, num_actions = 1;
    2202                 :          0 :                      aptr->type != RTE_FLOW_ACTION_TYPE_END;
    2203                 :          0 :                      aptr++, num_actions++);
    2204                 :          0 :                 pft->actions = malloc(
    2205                 :          0 :                                 (num_actions +  pft->num_pmd_actions) *
    2206                 :            :                                 sizeof(actions[0]));
    2207                 :          0 :                 if (!pft->actions) {
    2208                 :          0 :                         rte_flow_tunnel_action_decap_release(
    2209                 :            :                                         port_id, pft->actions,
    2210                 :            :                                         pft->num_pmd_actions, &error);
    2211                 :          0 :                         return NULL;
    2212                 :            :                 }
    2213                 :          0 :                 rte_memcpy(pft->actions, pft->pmd_actions,
    2214                 :          0 :                            pft->num_pmd_actions * sizeof(actions[0]));
    2215                 :          0 :                 rte_memcpy(pft->actions + pft->num_pmd_actions, actions,
    2216                 :            :                            num_actions * sizeof(actions[0]));
    2217                 :            :         }
    2218                 :          0 :         if (tunnel_ops->items) {
    2219                 :            :                 uint32_t num_items;
    2220                 :            :                 const struct rte_flow_item *iptr;
    2221                 :            : 
    2222                 :          0 :                 ret = rte_flow_tunnel_match(port_id, &pft->tunnel,
    2223                 :            :                                             &pft->pmd_items,
    2224                 :            :                                             &pft->num_pmd_items,
    2225                 :            :                                             &error);
    2226                 :          0 :                 if (ret) {
    2227                 :          0 :                         port_flow_complain(&error);
    2228                 :          0 :                         return NULL;
    2229                 :            :                 }
    2230                 :            :                 for (iptr = pattern, num_items = 1;
    2231                 :          0 :                      iptr->type != RTE_FLOW_ITEM_TYPE_END;
    2232                 :          0 :                      iptr++, num_items++);
    2233                 :          0 :                 pft->items = malloc((num_items + pft->num_pmd_items) *
    2234                 :            :                                     sizeof(pattern[0]));
    2235                 :          0 :                 if (!pft->items) {
    2236                 :          0 :                         rte_flow_tunnel_item_release(
    2237                 :            :                                         port_id, pft->pmd_items,
    2238                 :            :                                         pft->num_pmd_items, &error);
    2239                 :          0 :                         return NULL;
    2240                 :            :                 }
    2241                 :          0 :                 rte_memcpy(pft->items, pft->pmd_items,
    2242                 :          0 :                            pft->num_pmd_items * sizeof(pattern[0]));
    2243                 :          0 :                 rte_memcpy(pft->items + pft->num_pmd_items, pattern,
    2244                 :            :                            num_items * sizeof(pattern[0]));
    2245                 :            :         }
    2246                 :            : 
    2247                 :            :         return pft;
    2248                 :            : }
    2249                 :            : 
    2250                 :            : static void
    2251                 :          0 : port_flow_tunnel_offload_cmd_release(portid_t port_id,
    2252                 :            :                                      const struct tunnel_ops *tunnel_ops,
    2253                 :            :                                      struct port_flow_tunnel *pft)
    2254                 :            : {
    2255                 :            :         struct rte_flow_error error;
    2256                 :            : 
    2257                 :          0 :         if (tunnel_ops->actions) {
    2258                 :          0 :                 free(pft->actions);
    2259                 :          0 :                 rte_flow_tunnel_action_decap_release(
    2260                 :            :                         port_id, pft->pmd_actions,
    2261                 :            :                         pft->num_pmd_actions, &error);
    2262                 :          0 :                 pft->actions = NULL;
    2263                 :          0 :                 pft->pmd_actions = NULL;
    2264                 :            :         }
    2265                 :          0 :         if (tunnel_ops->items) {
    2266                 :          0 :                 free(pft->items);
    2267                 :          0 :                 rte_flow_tunnel_item_release(port_id, pft->pmd_items,
    2268                 :            :                                              pft->num_pmd_items,
    2269                 :            :                                              &error);
    2270                 :          0 :                 pft->items = NULL;
    2271                 :          0 :                 pft->pmd_items = NULL;
    2272                 :            :         }
    2273                 :          0 : }
    2274                 :            : 
    2275                 :            : /** Add port meter policy */
    2276                 :            : int
    2277                 :          0 : port_meter_policy_add(portid_t port_id, uint32_t policy_id,
    2278                 :            :                         const struct rte_flow_action *actions)
    2279                 :            : {
    2280                 :            :         struct rte_mtr_error error;
    2281                 :            :         const struct rte_flow_action *act = actions;
    2282                 :            :         const struct rte_flow_action *start;
    2283                 :            :         struct rte_mtr_meter_policy_params policy;
    2284                 :            :         uint32_t i = 0, act_n;
    2285                 :            :         int ret;
    2286                 :            : 
    2287                 :          0 :         for (i = 0; i < RTE_COLORS; i++) {
    2288                 :            :                 for (act_n = 0, start = act;
    2289                 :          0 :                         act->type != RTE_FLOW_ACTION_TYPE_END; act++)
    2290                 :          0 :                         act_n++;
    2291                 :          0 :                 if (act_n > 0)
    2292                 :          0 :                         policy.actions[i] = start;
    2293                 :            :                 else
    2294                 :          0 :                         policy.actions[i] = NULL;
    2295                 :          0 :                 act++;
    2296                 :            :         }
    2297                 :          0 :         ret = rte_mtr_meter_policy_add(port_id,
    2298                 :            :                         policy_id,
    2299                 :            :                         &policy, &error);
    2300                 :          0 :         if (ret)
    2301                 :          0 :                 print_mtr_err_msg(&error);
    2302                 :          0 :         return ret;
    2303                 :            : }
    2304                 :            : 
    2305                 :            : struct rte_flow_meter_profile *
    2306                 :          0 : port_meter_profile_get_by_id(portid_t port_id, uint32_t id)
    2307                 :            : {
    2308                 :            :         struct rte_mtr_error error;
    2309                 :            :         struct rte_flow_meter_profile *profile;
    2310                 :            : 
    2311                 :          0 :         profile = rte_mtr_meter_profile_get(port_id, id, &error);
    2312                 :          0 :         if (!profile)
    2313                 :          0 :                 print_mtr_err_msg(&error);
    2314                 :          0 :         return profile;
    2315                 :            : }
    2316                 :            : struct rte_flow_meter_policy *
    2317                 :          0 : port_meter_policy_get_by_id(portid_t port_id, uint32_t id)
    2318                 :            : {
    2319                 :            :         struct rte_mtr_error error;
    2320                 :            :         struct rte_flow_meter_policy *policy;
    2321                 :            : 
    2322                 :          0 :         policy = rte_mtr_meter_policy_get(port_id, id, &error);
    2323                 :          0 :         if (!policy)
    2324                 :          0 :                 print_mtr_err_msg(&error);
    2325                 :          0 :         return policy;
    2326                 :            : }
    2327                 :            : 
    2328                 :            : /** Validate flow rule. */
    2329                 :            : int
    2330                 :          0 : port_flow_validate(portid_t port_id,
    2331                 :            :                    const struct rte_flow_attr *attr,
    2332                 :            :                    const struct rte_flow_item *pattern,
    2333                 :            :                    const struct rte_flow_action *actions,
    2334                 :            :                    const struct tunnel_ops *tunnel_ops)
    2335                 :            : {
    2336                 :            :         struct rte_flow_error error;
    2337                 :            :         struct port_flow_tunnel *pft = NULL;
    2338                 :            :         int ret;
    2339                 :            : 
    2340                 :            :         /* Poisoning to make sure PMDs update it in case of error. */
    2341                 :            :         memset(&error, 0x11, sizeof(error));
    2342                 :          0 :         if (tunnel_ops->enabled) {
    2343                 :          0 :                 pft = port_flow_tunnel_offload_cmd_prep(port_id, pattern,
    2344                 :            :                                                         actions, tunnel_ops);
    2345                 :          0 :                 if (!pft)
    2346                 :            :                         return -ENOENT;
    2347                 :          0 :                 if (pft->items)
    2348                 :            :                         pattern = pft->items;
    2349                 :          0 :                 if (pft->actions)
    2350                 :            :                         actions = pft->actions;
    2351                 :            :         }
    2352                 :          0 :         ret = rte_flow_validate(port_id, attr, pattern, actions, &error);
    2353                 :          0 :         if (tunnel_ops->enabled)
    2354                 :          0 :                 port_flow_tunnel_offload_cmd_release(port_id, tunnel_ops, pft);
    2355                 :          0 :         if (ret)
    2356                 :          0 :                 return port_flow_complain(&error);
    2357                 :            :         printf("Flow rule validated\n");
    2358                 :          0 :         return 0;
    2359                 :            : }
    2360                 :            : 
    2361                 :            : /** Return age action structure if exists, otherwise NULL. */
    2362                 :            : static struct rte_flow_action_age *
    2363                 :            : age_action_get(const struct rte_flow_action *actions)
    2364                 :            : {
    2365                 :          0 :         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
    2366                 :          0 :                 switch (actions->type) {
    2367                 :          0 :                 case RTE_FLOW_ACTION_TYPE_AGE:
    2368                 :          0 :                         return (struct rte_flow_action_age *)
    2369                 :            :                                 (uintptr_t)actions->conf;
    2370                 :            :                 default:
    2371                 :            :                         break;
    2372                 :            :                 }
    2373                 :            :         }
    2374                 :            :         return NULL;
    2375                 :            : }
    2376                 :            : 
    2377                 :            : /** Create pattern template */
    2378                 :            : int
    2379                 :          0 : port_flow_pattern_template_create(portid_t port_id, uint32_t id,
    2380                 :            :                                   const struct rte_flow_pattern_template_attr *attr,
    2381                 :            :                                   const struct rte_flow_item *pattern)
    2382                 :            : {
    2383                 :            :         struct rte_port *port;
    2384                 :            :         struct port_template *pit;
    2385                 :            :         int ret;
    2386                 :            :         struct rte_flow_error error;
    2387                 :            : 
    2388                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    2389                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    2390                 :            :                 return -EINVAL;
    2391                 :          0 :         port = &ports[port_id];
    2392                 :          0 :         ret = template_alloc(id, &pit, &port->pattern_templ_list);
    2393                 :          0 :         if (ret)
    2394                 :            :                 return ret;
    2395                 :            :         /* Poisoning to make sure PMDs update it in case of error. */
    2396                 :            :         memset(&error, 0x22, sizeof(error));
    2397                 :          0 :         pit->template.pattern_template = rte_flow_pattern_template_create(port_id,
    2398                 :            :                                                 attr, pattern, &error);
    2399                 :          0 :         if (!pit->template.pattern_template) {
    2400                 :          0 :                 uint32_t destroy_id = pit->id;
    2401                 :          0 :                 port_flow_pattern_template_destroy(port_id, 1, &destroy_id);
    2402                 :          0 :                 return port_flow_complain(&error);
    2403                 :            :         }
    2404                 :          0 :         printf("Pattern template #%u created\n", pit->id);
    2405                 :          0 :         return 0;
    2406                 :            : }
    2407                 :            : 
    2408                 :            : /** Destroy pattern template */
    2409                 :            : int
    2410                 :          0 : port_flow_pattern_template_destroy(portid_t port_id, uint32_t n,
    2411                 :            :                                    const uint32_t *template)
    2412                 :            : {
    2413                 :            :         struct rte_port *port;
    2414                 :            :         struct port_template **tmp;
    2415                 :            :         int ret = 0;
    2416                 :            : 
    2417                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    2418                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    2419                 :            :                 return -EINVAL;
    2420                 :          0 :         port = &ports[port_id];
    2421                 :          0 :         tmp = &port->pattern_templ_list;
    2422                 :          0 :         while (*tmp) {
    2423                 :            :                 uint32_t i;
    2424                 :            : 
    2425                 :          0 :                 for (i = 0; i != n; ++i) {
    2426                 :            :                         struct rte_flow_error error;
    2427                 :          0 :                         struct port_template *pit = *tmp;
    2428                 :            : 
    2429                 :          0 :                         if (template[i] != pit->id)
    2430                 :          0 :                                 continue;
    2431                 :            :                         /*
    2432                 :            :                          * Poisoning to make sure PMDs update it in case
    2433                 :            :                          * of error.
    2434                 :            :                          */
    2435                 :            :                         memset(&error, 0x33, sizeof(error));
    2436                 :            : 
    2437                 :          0 :                         if (pit->template.pattern_template &&
    2438                 :          0 :                             rte_flow_pattern_template_destroy(port_id,
    2439                 :            :                                                            pit->template.pattern_template,
    2440                 :            :                                                            &error)) {
    2441                 :          0 :                                 ret = port_flow_complain(&error);
    2442                 :          0 :                                 continue;
    2443                 :            :                         }
    2444                 :          0 :                         *tmp = pit->next;
    2445                 :          0 :                         printf("Pattern template #%u destroyed\n", pit->id);
    2446                 :          0 :                         free(pit);
    2447                 :          0 :                         break;
    2448                 :            :                 }
    2449                 :          0 :                 if (i == n)
    2450                 :          0 :                         tmp = &(*tmp)->next;
    2451                 :            :         }
    2452                 :            :         return ret;
    2453                 :            : }
    2454                 :            : 
    2455                 :            : /** Flush pattern template */
    2456                 :            : int
    2457                 :          0 : port_flow_pattern_template_flush(portid_t port_id)
    2458                 :            : {
    2459                 :            :         struct rte_port *port;
    2460                 :            :         struct port_template **tmp;
    2461                 :            :         int ret = 0;
    2462                 :            : 
    2463                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    2464                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    2465                 :            :                 return -EINVAL;
    2466                 :          0 :         port = &ports[port_id];
    2467                 :          0 :         tmp = &port->pattern_templ_list;
    2468                 :          0 :         while (*tmp) {
    2469                 :            :                 struct rte_flow_error error;
    2470                 :            :                 struct port_template *pit = *tmp;
    2471                 :            : 
    2472                 :            :                 /*
    2473                 :            :                  * Poisoning to make sure PMDs update it in case
    2474                 :            :                  * of error.
    2475                 :            :                  */
    2476                 :            :                 memset(&error, 0x33, sizeof(error));
    2477                 :          0 :                 if (pit->template.pattern_template &&
    2478                 :          0 :                     rte_flow_pattern_template_destroy(port_id,
    2479                 :            :                         pit->template.pattern_template, &error)) {
    2480                 :          0 :                         printf("Pattern template #%u not destroyed\n", pit->id);
    2481                 :          0 :                         ret = port_flow_complain(&error);
    2482                 :          0 :                         tmp = &pit->next;
    2483                 :            :                 } else {
    2484                 :          0 :                         *tmp = pit->next;
    2485                 :          0 :                         free(pit);
    2486                 :            :                 }
    2487                 :            :         }
    2488                 :            :         return ret;
    2489                 :            : }
    2490                 :            : 
    2491                 :            : /** Create actions template */
    2492                 :            : int
    2493                 :          0 : port_flow_actions_template_create(portid_t port_id, uint32_t id,
    2494                 :            :                                   const struct rte_flow_actions_template_attr *attr,
    2495                 :            :                                   const struct rte_flow_action *actions,
    2496                 :            :                                   const struct rte_flow_action *masks)
    2497                 :            : {
    2498                 :            :         struct rte_port *port;
    2499                 :            :         struct port_template *pat;
    2500                 :            :         int ret;
    2501                 :            :         struct rte_flow_error error;
    2502                 :            : 
    2503                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    2504                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    2505                 :            :                 return -EINVAL;
    2506                 :          0 :         port = &ports[port_id];
    2507                 :          0 :         ret = template_alloc(id, &pat, &port->actions_templ_list);
    2508                 :          0 :         if (ret)
    2509                 :            :                 return ret;
    2510                 :            :         /* Poisoning to make sure PMDs update it in case of error. */
    2511                 :            :         memset(&error, 0x22, sizeof(error));
    2512                 :          0 :         pat->template.actions_template = rte_flow_actions_template_create(port_id,
    2513                 :            :                                                 attr, actions, masks, &error);
    2514                 :          0 :         if (!pat->template.actions_template) {
    2515                 :          0 :                 uint32_t destroy_id = pat->id;
    2516                 :          0 :                 port_flow_actions_template_destroy(port_id, 1, &destroy_id);
    2517                 :          0 :                 return port_flow_complain(&error);
    2518                 :            :         }
    2519                 :          0 :         printf("Actions template #%u created\n", pat->id);
    2520                 :          0 :         return 0;
    2521                 :            : }
    2522                 :            : 
    2523                 :            : /** Destroy actions template */
    2524                 :            : int
    2525                 :          0 : port_flow_actions_template_destroy(portid_t port_id, uint32_t n,
    2526                 :            :                                    const uint32_t *template)
    2527                 :            : {
    2528                 :            :         struct rte_port *port;
    2529                 :            :         struct port_template **tmp;
    2530                 :            :         int ret = 0;
    2531                 :            : 
    2532                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    2533                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    2534                 :            :                 return -EINVAL;
    2535                 :          0 :         port = &ports[port_id];
    2536                 :          0 :         tmp = &port->actions_templ_list;
    2537                 :          0 :         while (*tmp) {
    2538                 :            :                 uint32_t i;
    2539                 :            : 
    2540                 :          0 :                 for (i = 0; i != n; ++i) {
    2541                 :            :                         struct rte_flow_error error;
    2542                 :          0 :                         struct port_template *pat = *tmp;
    2543                 :            : 
    2544                 :          0 :                         if (template[i] != pat->id)
    2545                 :          0 :                                 continue;
    2546                 :            :                         /*
    2547                 :            :                          * Poisoning to make sure PMDs update it in case
    2548                 :            :                          * of error.
    2549                 :            :                          */
    2550                 :            :                         memset(&error, 0x33, sizeof(error));
    2551                 :            : 
    2552                 :          0 :                         if (pat->template.actions_template &&
    2553                 :          0 :                             rte_flow_actions_template_destroy(port_id,
    2554                 :            :                                         pat->template.actions_template, &error)) {
    2555                 :          0 :                                 ret = port_flow_complain(&error);
    2556                 :          0 :                                 continue;
    2557                 :            :                         }
    2558                 :          0 :                         *tmp = pat->next;
    2559                 :          0 :                         printf("Actions template #%u destroyed\n", pat->id);
    2560                 :          0 :                         free(pat);
    2561                 :          0 :                         break;
    2562                 :            :                 }
    2563                 :          0 :                 if (i == n)
    2564                 :          0 :                         tmp = &(*tmp)->next;
    2565                 :            :         }
    2566                 :            :         return ret;
    2567                 :            : }
    2568                 :            : 
    2569                 :            : /** Flush actions template */
    2570                 :            : int
    2571                 :          0 : port_flow_actions_template_flush(portid_t port_id)
    2572                 :            : {
    2573                 :            :         struct rte_port *port;
    2574                 :            :         struct port_template **tmp;
    2575                 :            :         int ret = 0;
    2576                 :            : 
    2577                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    2578                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    2579                 :            :                 return -EINVAL;
    2580                 :          0 :         port = &ports[port_id];
    2581                 :          0 :         tmp = &port->actions_templ_list;
    2582                 :          0 :         while (*tmp) {
    2583                 :            :                 struct rte_flow_error error;
    2584                 :            :                 struct port_template *pat = *tmp;
    2585                 :            : 
    2586                 :            :                 /*
    2587                 :            :                  * Poisoning to make sure PMDs update it in case
    2588                 :            :                  * of error.
    2589                 :            :                  */
    2590                 :            :                 memset(&error, 0x33, sizeof(error));
    2591                 :            : 
    2592                 :          0 :                 if (pat->template.actions_template &&
    2593                 :          0 :                     rte_flow_actions_template_destroy(port_id,
    2594                 :            :                         pat->template.actions_template, &error)) {
    2595                 :          0 :                         ret = port_flow_complain(&error);
    2596                 :          0 :                         printf("Actions template #%u not destroyed\n", pat->id);
    2597                 :          0 :                         tmp = &pat->next;
    2598                 :            :                 } else {
    2599                 :          0 :                         *tmp = pat->next;
    2600                 :          0 :                         free(pat);
    2601                 :            :                 }
    2602                 :            :         }
    2603                 :            :         return ret;
    2604                 :            : }
    2605                 :            : 
    2606                 :            : /** Create table */
    2607                 :            : int
    2608                 :          0 : port_flow_template_table_create(portid_t port_id, uint32_t id,
    2609                 :            :                 const struct rte_flow_template_table_attr *table_attr,
    2610                 :            :                 uint32_t nb_pattern_templates, uint32_t *pattern_templates,
    2611                 :            :                 uint32_t nb_actions_templates, uint32_t *actions_templates)
    2612                 :          0 : {
    2613                 :            :         struct rte_port *port;
    2614                 :            :         struct port_table *pt;
    2615                 :            :         struct port_template *temp = NULL;
    2616                 :            :         int ret;
    2617                 :            :         uint32_t i;
    2618                 :            :         struct rte_flow_error error;
    2619                 :            :         struct rte_flow_pattern_template
    2620                 :          0 :                         *flow_pattern_templates[nb_pattern_templates];
    2621                 :            :         struct rte_flow_actions_template
    2622                 :          0 :                         *flow_actions_templates[nb_actions_templates];
    2623                 :            : 
    2624                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    2625                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    2626                 :            :                 return -EINVAL;
    2627                 :          0 :         port = &ports[port_id];
    2628                 :          0 :         for (i = 0; i < nb_pattern_templates; ++i) {
    2629                 :            :                 bool found = false;
    2630                 :          0 :                 temp = port->pattern_templ_list;
    2631                 :          0 :                 while (temp) {
    2632                 :          0 :                         if (pattern_templates[i] == temp->id) {
    2633                 :          0 :                                 flow_pattern_templates[i] =
    2634                 :          0 :                                         temp->template.pattern_template;
    2635                 :            :                                 found = true;
    2636                 :            :                                 break;
    2637                 :            :                         }
    2638                 :          0 :                         temp = temp->next;
    2639                 :            :                 }
    2640                 :            :                 if (!found) {
    2641                 :          0 :                         printf("Pattern template #%u is invalid\n",
    2642                 :          0 :                                pattern_templates[i]);
    2643                 :          0 :                         return -EINVAL;
    2644                 :            :                 }
    2645                 :            :         }
    2646                 :          0 :         for (i = 0; i < nb_actions_templates; ++i) {
    2647                 :            :                 bool found = false;
    2648                 :          0 :                 temp = port->actions_templ_list;
    2649                 :          0 :                 while (temp) {
    2650                 :          0 :                         if (actions_templates[i] == temp->id) {
    2651                 :          0 :                                 flow_actions_templates[i] =
    2652                 :          0 :                                         temp->template.actions_template;
    2653                 :            :                                 found = true;
    2654                 :            :                                 break;
    2655                 :            :                         }
    2656                 :          0 :                         temp = temp->next;
    2657                 :            :                 }
    2658                 :            :                 if (!found) {
    2659                 :          0 :                         printf("Actions template #%u is invalid\n",
    2660                 :          0 :                                actions_templates[i]);
    2661                 :          0 :                         return -EINVAL;
    2662                 :            :                 }
    2663                 :            :         }
    2664                 :          0 :         ret = table_alloc(id, &pt, &port->table_list);
    2665                 :          0 :         if (ret)
    2666                 :            :                 return ret;
    2667                 :            :         /* Poisoning to make sure PMDs update it in case of error. */
    2668                 :            :         memset(&error, 0x22, sizeof(error));
    2669                 :          0 :         pt->table = rte_flow_template_table_create(port_id, table_attr,
    2670                 :            :                       flow_pattern_templates, nb_pattern_templates,
    2671                 :            :                       flow_actions_templates, nb_actions_templates,
    2672                 :            :                       &error);
    2673                 :            : 
    2674                 :          0 :         if (!pt->table) {
    2675                 :          0 :                 uint32_t destroy_id = pt->id;
    2676                 :          0 :                 port_flow_template_table_destroy(port_id, 1, &destroy_id);
    2677                 :          0 :                 return port_flow_complain(&error);
    2678                 :            :         }
    2679                 :          0 :         pt->nb_pattern_templates = nb_pattern_templates;
    2680                 :          0 :         pt->nb_actions_templates = nb_actions_templates;
    2681                 :          0 :         rte_memcpy(&pt->attr, table_attr,
    2682                 :            :                    sizeof(struct rte_flow_template_table_attr));
    2683                 :          0 :         printf("Template table #%u created\n", pt->id);
    2684                 :          0 :         return 0;
    2685                 :            : }
    2686                 :            : 
    2687                 :            : /** Destroy table */
    2688                 :            : int
    2689                 :          0 : port_flow_template_table_destroy(portid_t port_id,
    2690                 :            :                                  uint32_t n, const uint32_t *table)
    2691                 :            : {
    2692                 :            :         struct rte_port *port;
    2693                 :            :         struct port_table **tmp;
    2694                 :            :         int ret = 0;
    2695                 :            : 
    2696                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    2697                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    2698                 :            :                 return -EINVAL;
    2699                 :          0 :         port = &ports[port_id];
    2700                 :          0 :         tmp = &port->table_list;
    2701                 :          0 :         while (*tmp) {
    2702                 :            :                 uint32_t i;
    2703                 :            : 
    2704                 :          0 :                 for (i = 0; i != n; ++i) {
    2705                 :            :                         struct rte_flow_error error;
    2706                 :          0 :                         struct port_table *pt = *tmp;
    2707                 :            : 
    2708                 :          0 :                         if (table[i] != pt->id)
    2709                 :          0 :                                 continue;
    2710                 :            :                         /*
    2711                 :            :                          * Poisoning to make sure PMDs update it in case
    2712                 :            :                          * of error.
    2713                 :            :                          */
    2714                 :            :                         memset(&error, 0x33, sizeof(error));
    2715                 :            : 
    2716                 :          0 :                         if (pt->table &&
    2717                 :          0 :                             rte_flow_template_table_destroy(port_id,
    2718                 :            :                                                             pt->table,
    2719                 :            :                                                             &error)) {
    2720                 :          0 :                                 ret = port_flow_complain(&error);
    2721                 :          0 :                                 continue;
    2722                 :            :                         }
    2723                 :          0 :                         *tmp = pt->next;
    2724                 :          0 :                         printf("Template table #%u destroyed\n", pt->id);
    2725                 :          0 :                         free(pt);
    2726                 :          0 :                         break;
    2727                 :            :                 }
    2728                 :          0 :                 if (i == n)
    2729                 :          0 :                         tmp = &(*tmp)->next;
    2730                 :            :         }
    2731                 :            :         return ret;
    2732                 :            : }
    2733                 :            : 
    2734                 :            : int
    2735                 :          0 : port_flow_template_table_resize_complete(portid_t port_id, uint32_t table_id)
    2736                 :            : {
    2737                 :            :         struct rte_port *port;
    2738                 :            :         struct port_table *pt;
    2739                 :          0 :         struct rte_flow_error error = { 0, };
    2740                 :            :         int ret;
    2741                 :            : 
    2742                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    2743                 :            :                 return -EINVAL;
    2744                 :          0 :         port = &ports[port_id];
    2745                 :          0 :         pt = port_table_locate(port->table_list, table_id);
    2746                 :          0 :         if (!pt)
    2747                 :            :                 return -EINVAL;
    2748                 :          0 :         ret = rte_flow_template_table_resize_complete(port_id,
    2749                 :            :                                                       pt->table, &error);
    2750                 :          0 :         return !ret ? 0 : port_flow_complain(&error);
    2751                 :            : }
    2752                 :            : 
    2753                 :            : int
    2754                 :          0 : port_flow_template_table_resize(portid_t port_id,
    2755                 :            :                                 uint32_t table_id, uint32_t flows_num)
    2756                 :            : {
    2757                 :            :         struct rte_port *port;
    2758                 :            :         struct port_table *pt;
    2759                 :          0 :         struct rte_flow_error error = { 0, };
    2760                 :            :         int ret;
    2761                 :            : 
    2762                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    2763                 :            :                 return -EINVAL;
    2764                 :          0 :         port = &ports[port_id];
    2765                 :          0 :         pt = port_table_locate(port->table_list, table_id);
    2766                 :          0 :         if (!pt)
    2767                 :            :                 return -EINVAL;
    2768                 :          0 :         ret = rte_flow_template_table_resize(port_id, pt->table, flows_num, &error);
    2769                 :          0 :         if (ret)
    2770                 :          0 :                 return port_flow_complain(&error);
    2771                 :            :         return 0;
    2772                 :            : }
    2773                 :            : 
    2774                 :            : /** Flush table */
    2775                 :            : int
    2776                 :          0 : port_flow_template_table_flush(portid_t port_id)
    2777                 :            : {
    2778                 :            :         struct rte_port *port;
    2779                 :            :         struct port_table **tmp;
    2780                 :            :         int ret = 0;
    2781                 :            : 
    2782                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    2783                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    2784                 :            :                 return -EINVAL;
    2785                 :          0 :         port = &ports[port_id];
    2786                 :          0 :         tmp = &port->table_list;
    2787                 :          0 :         while (*tmp) {
    2788                 :            :                 struct rte_flow_error error;
    2789                 :            :                 struct port_table *pt = *tmp;
    2790                 :            : 
    2791                 :            :                 /*
    2792                 :            :                  * Poisoning to make sure PMDs update it in case
    2793                 :            :                  * of error.
    2794                 :            :                  */
    2795                 :            :                 memset(&error, 0x33, sizeof(error));
    2796                 :            : 
    2797                 :          0 :                 if (pt->table &&
    2798                 :          0 :                     rte_flow_template_table_destroy(port_id,
    2799                 :            :                                                    pt->table,
    2800                 :            :                                                    &error)) {
    2801                 :          0 :                         ret = port_flow_complain(&error);
    2802                 :          0 :                         printf("Template table #%u not destroyed\n", pt->id);
    2803                 :          0 :                         tmp = &pt->next;
    2804                 :            :                 } else {
    2805                 :          0 :                         *tmp = pt->next;
    2806                 :          0 :                         free(pt);
    2807                 :            :                 }
    2808                 :            :         }
    2809                 :            :         return ret;
    2810                 :            : }
    2811                 :            : 
    2812                 :            : /** Enqueue create flow rule operation. */
    2813                 :            : int
    2814                 :          0 : port_queue_flow_create(portid_t port_id, queueid_t queue_id,
    2815                 :            :                        bool postpone, uint32_t table_id, uint32_t rule_idx,
    2816                 :            :                        uint32_t pattern_idx, uint32_t actions_idx,
    2817                 :            :                        const struct rte_flow_item *pattern,
    2818                 :            :                        const struct rte_flow_action *actions)
    2819                 :            : {
    2820                 :          0 :         struct rte_flow_op_attr op_attr = { .postpone = postpone };
    2821                 :            :         struct rte_flow *flow;
    2822                 :            :         struct rte_port *port;
    2823                 :            :         struct port_flow *pf;
    2824                 :            :         struct port_table *pt;
    2825                 :            :         uint32_t id = 0;
    2826                 :            :         bool found;
    2827                 :          0 :         struct rte_flow_error error = { RTE_FLOW_ERROR_TYPE_NONE, NULL, NULL };
    2828                 :            :         struct rte_flow_action_age *age = age_action_get(actions);
    2829                 :            :         struct queue_job *job;
    2830                 :            : 
    2831                 :          0 :         port = &ports[port_id];
    2832                 :          0 :         if (port->flow_list) {
    2833                 :          0 :                 if (port->flow_list->id == UINT32_MAX) {
    2834                 :            :                         printf("Highest rule ID is already assigned,"
    2835                 :            :                                " delete it first");
    2836                 :          0 :                         return -ENOMEM;
    2837                 :            :                 }
    2838                 :          0 :                 id = port->flow_list->id + 1;
    2839                 :            :         }
    2840                 :            : 
    2841                 :          0 :         if (queue_id >= port->queue_nb) {
    2842                 :          0 :                 printf("Queue #%u is invalid\n", queue_id);
    2843                 :          0 :                 return -EINVAL;
    2844                 :            :         }
    2845                 :            : 
    2846                 :            :         found = false;
    2847                 :          0 :         pt = port->table_list;
    2848                 :          0 :         while (pt) {
    2849                 :          0 :                 if (table_id == pt->id) {
    2850                 :            :                         found = true;
    2851                 :            :                         break;
    2852                 :            :                 }
    2853                 :          0 :                 pt = pt->next;
    2854                 :            :         }
    2855                 :          0 :         if (!found) {
    2856                 :            :                 printf("Table #%u is invalid\n", table_id);
    2857                 :          0 :                 return -EINVAL;
    2858                 :            :         }
    2859                 :            : 
    2860                 :          0 :         if (pattern_idx >= pt->nb_pattern_templates) {
    2861                 :            :                 printf("Pattern template index #%u is invalid,"
    2862                 :            :                        " %u templates present in the table\n",
    2863                 :            :                        pattern_idx, pt->nb_pattern_templates);
    2864                 :          0 :                 return -EINVAL;
    2865                 :            :         }
    2866                 :          0 :         if (actions_idx >= pt->nb_actions_templates) {
    2867                 :            :                 printf("Actions template index #%u is invalid,"
    2868                 :            :                        " %u templates present in the table\n",
    2869                 :            :                        actions_idx, pt->nb_actions_templates);
    2870                 :          0 :                 return -EINVAL;
    2871                 :            :         }
    2872                 :            : 
    2873                 :          0 :         job = calloc(1, sizeof(*job));
    2874                 :          0 :         if (!job) {
    2875                 :            :                 printf("Queue flow create job allocate failed\n");
    2876                 :          0 :                 return -ENOMEM;
    2877                 :            :         }
    2878                 :            :         job->type = QUEUE_JOB_TYPE_FLOW_CREATE;
    2879                 :            : 
    2880                 :          0 :         pf = port_flow_new(&pt->attr.flow_attr, pattern, actions, &error);
    2881                 :          0 :         if (!pf) {
    2882                 :          0 :                 free(job);
    2883                 :          0 :                 return port_flow_complain(&error);
    2884                 :            :         }
    2885                 :          0 :         if (age) {
    2886                 :          0 :                 pf->age_type = ACTION_AGE_CONTEXT_TYPE_FLOW;
    2887                 :          0 :                 age->context = &pf->age_type;
    2888                 :            :         }
    2889                 :            :         /* Poisoning to make sure PMDs update it in case of error. */
    2890                 :            :         memset(&error, 0x11, sizeof(error));
    2891                 :          0 :         if (pt->attr.insertion_type == RTE_FLOW_TABLE_INSERTION_TYPE_PATTERN)
    2892                 :          0 :                 flow = rte_flow_async_create(port_id, queue_id, &op_attr, pt->table,
    2893                 :            :                         pattern, pattern_idx, actions, actions_idx, job, &error);
    2894                 :          0 :         else if (pt->attr.insertion_type == RTE_FLOW_TABLE_INSERTION_TYPE_INDEX)
    2895                 :          0 :                 flow = rte_flow_async_create_by_index(port_id, queue_id, &op_attr, pt->table,
    2896                 :            :                         rule_idx, actions, actions_idx, job, &error);
    2897                 :          0 :         else if (pt->attr.insertion_type == RTE_FLOW_TABLE_INSERTION_TYPE_INDEX_WITH_PATTERN)
    2898                 :          0 :                 flow = rte_flow_async_create_by_index_with_pattern(port_id, queue_id, &op_attr,
    2899                 :            :                         pt->table, rule_idx, pattern, pattern_idx, actions, actions_idx, job,
    2900                 :            :                         &error);
    2901                 :            :         else {
    2902                 :          0 :                 free(pf);
    2903                 :          0 :                 free(job);
    2904                 :          0 :                 printf("Insertion type %d is invalid\n", pt->attr.insertion_type);
    2905                 :          0 :                 return -EINVAL;
    2906                 :            :         }
    2907                 :          0 :         if (!flow) {
    2908                 :          0 :                 free(pf);
    2909                 :          0 :                 free(job);
    2910                 :          0 :                 return port_flow_complain(&error);
    2911                 :            :         }
    2912                 :            : 
    2913                 :          0 :         pf->next = port->flow_list;
    2914                 :          0 :         pf->id = id;
    2915                 :          0 :         pf->table = pt;
    2916                 :          0 :         pf->flow = flow;
    2917                 :          0 :         job->pf = pf;
    2918                 :          0 :         port->flow_list = pf;
    2919                 :            :         printf("Flow rule #%"PRIu64" creation enqueued\n", pf->id);
    2920                 :          0 :         return 0;
    2921                 :            : }
    2922                 :            : 
    2923                 :            : int
    2924                 :          0 : port_queue_flow_update_resized(portid_t port_id, queueid_t queue_id,
    2925                 :            :                                bool postpone, uint32_t flow_id)
    2926                 :            : {
    2927                 :          0 :         const struct rte_flow_op_attr op_attr = { .postpone = postpone };
    2928                 :          0 :         struct rte_flow_error error = { 0, };
    2929                 :            :         struct port_flow *pf;
    2930                 :            :         struct rte_port *port;
    2931                 :            :         struct queue_job *job;
    2932                 :            :         int ret;
    2933                 :            : 
    2934                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    2935                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    2936                 :            :                 return -EINVAL;
    2937                 :          0 :         port = &ports[port_id];
    2938                 :          0 :         if (queue_id >= port->queue_nb) {
    2939                 :          0 :                 printf("Queue #%u is invalid\n", queue_id);
    2940                 :          0 :                 return -EINVAL;
    2941                 :            :         }
    2942                 :          0 :         pf = port_flow_locate(port->flow_list, flow_id);
    2943                 :          0 :         if (!pf)
    2944                 :            :                 return -EINVAL;
    2945                 :          0 :         job = calloc(1, sizeof(*job));
    2946                 :          0 :         if (!job)
    2947                 :            :                 return -ENOMEM;
    2948                 :          0 :         job->type = QUEUE_JOB_TYPE_FLOW_TRANSFER;
    2949                 :          0 :         job->pf = pf;
    2950                 :          0 :         ret = rte_flow_async_update_resized(port_id, queue_id, &op_attr,
    2951                 :            :                                             pf->flow, job, &error);
    2952                 :          0 :         if (ret) {
    2953                 :          0 :                 free(job);
    2954                 :          0 :                 return port_flow_complain(&error);
    2955                 :            :         }
    2956                 :            :         return 0;
    2957                 :            : }
    2958                 :            : 
    2959                 :            : /** Enqueue number of destroy flow rules operations. */
    2960                 :            : int
    2961                 :          0 : port_queue_flow_destroy(portid_t port_id, queueid_t queue_id,
    2962                 :            :                         bool postpone, uint32_t n, const uint64_t *rule)
    2963                 :            : {
    2964                 :          0 :         struct rte_flow_op_attr op_attr = { .postpone = postpone };
    2965                 :            :         struct rte_port *port;
    2966                 :            :         struct port_flow **tmp;
    2967                 :            :         int ret = 0;
    2968                 :            :         struct queue_job *job;
    2969                 :            : 
    2970                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    2971                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    2972                 :            :                 return -EINVAL;
    2973                 :          0 :         port = &ports[port_id];
    2974                 :            : 
    2975                 :          0 :         if (queue_id >= port->queue_nb) {
    2976                 :          0 :                 printf("Queue #%u is invalid\n", queue_id);
    2977                 :          0 :                 return -EINVAL;
    2978                 :            :         }
    2979                 :            : 
    2980                 :          0 :         tmp = &port->flow_list;
    2981                 :          0 :         while (*tmp) {
    2982                 :            :                 uint32_t i;
    2983                 :            : 
    2984                 :          0 :                 for (i = 0; i != n; ++i) {
    2985                 :            :                         struct rte_flow_error error;
    2986                 :          0 :                         struct port_flow *pf = *tmp;
    2987                 :            : 
    2988                 :          0 :                         if (rule[i] != pf->id)
    2989                 :          0 :                                 continue;
    2990                 :            :                         /*
    2991                 :            :                          * Poisoning to make sure PMD
    2992                 :            :                          * update it in case of error.
    2993                 :            :                          */
    2994                 :            :                         memset(&error, 0x33, sizeof(error));
    2995                 :          0 :                         job = calloc(1, sizeof(*job));
    2996                 :          0 :                         if (!job) {
    2997                 :            :                                 printf("Queue flow destroy job allocate failed\n");
    2998                 :          0 :                                 return -ENOMEM;
    2999                 :            :                         }
    3000                 :          0 :                         job->type = QUEUE_JOB_TYPE_FLOW_DESTROY;
    3001                 :          0 :                         job->pf = pf;
    3002                 :            : 
    3003                 :          0 :                         if (rte_flow_async_destroy(port_id, queue_id, &op_attr,
    3004                 :            :                                                    pf->flow, job, &error)) {
    3005                 :          0 :                                 free(job);
    3006                 :          0 :                                 ret = port_flow_complain(&error);
    3007                 :          0 :                                 continue;
    3008                 :            :                         }
    3009                 :          0 :                         printf("Flow rule #%"PRIu64" destruction enqueued\n",
    3010                 :            :                                pf->id);
    3011                 :          0 :                         *tmp = pf->next;
    3012                 :          0 :                         break;
    3013                 :            :                 }
    3014                 :          0 :                 if (i == n)
    3015                 :          0 :                         tmp = &(*tmp)->next;
    3016                 :            :         }
    3017                 :            :         return ret;
    3018                 :            : }
    3019                 :            : 
    3020                 :            : static void
    3021                 :            : queue_action_handle_create(portid_t port_id, uint32_t queue_id,
    3022                 :            :                            struct port_indirect_action *pia,
    3023                 :            :                            struct queue_job *job,
    3024                 :            :                            const struct rte_flow_op_attr *attr,
    3025                 :            :                            const struct rte_flow_indir_action_conf *conf,
    3026                 :            :                            const struct rte_flow_action *action,
    3027                 :            :                            struct rte_flow_error *error)
    3028                 :            : {
    3029                 :          0 :         if (action->type == RTE_FLOW_ACTION_TYPE_AGE) {
    3030                 :          0 :                 struct rte_flow_action_age *age =
    3031                 :            :                         (struct rte_flow_action_age *)(uintptr_t)(action->conf);
    3032                 :            : 
    3033                 :          0 :                 pia->age_type = ACTION_AGE_CONTEXT_TYPE_INDIRECT_ACTION;
    3034                 :          0 :                 age->context = &pia->age_type;
    3035                 :            :         }
    3036                 :            :         /* Poisoning to make sure PMDs update it in case of error. */
    3037                 :          0 :         pia->handle = rte_flow_async_action_handle_create(port_id, queue_id,
    3038                 :            :                                                           attr, conf, action,
    3039                 :            :                                                           job, error);
    3040                 :          0 :         pia->type = action->type;
    3041                 :          0 : }
    3042                 :            : 
    3043                 :            : static void
    3044                 :            : queue_action_list_handle_create(portid_t port_id, uint32_t queue_id,
    3045                 :            :                                 struct port_indirect_action *pia,
    3046                 :            :                                 struct queue_job *job,
    3047                 :            :                                 const struct rte_flow_op_attr *attr,
    3048                 :            :                                 const struct rte_flow_indir_action_conf *conf,
    3049                 :            :                                 const struct rte_flow_action *action,
    3050                 :            :                                 struct rte_flow_error *error)
    3051                 :            : {
    3052                 :            :         /* Poisoning to make sure PMDs update it in case of error. */
    3053                 :          0 :         pia->type = RTE_FLOW_ACTION_TYPE_INDIRECT_LIST;
    3054                 :          0 :         pia->list_handle = rte_flow_async_action_list_handle_create
    3055                 :            :                 (port_id, queue_id, attr, conf, action,
    3056                 :            :                  job, error);
    3057                 :          0 : }
    3058                 :            : 
    3059                 :            : /** Enqueue update flow rule operation. */
    3060                 :            : int
    3061                 :          0 : port_queue_flow_update(portid_t port_id, queueid_t queue_id,
    3062                 :            :                        bool postpone, uint32_t rule_idx, uint32_t actions_idx,
    3063                 :            :                        const struct rte_flow_action *actions)
    3064                 :            : {
    3065                 :          0 :         struct rte_flow_op_attr op_attr = { .postpone = postpone };
    3066                 :            :         struct rte_port *port;
    3067                 :            :         struct port_flow *pf, *uf;
    3068                 :            :         struct port_flow **tmp;
    3069                 :            :         struct port_table *pt;
    3070                 :            :         bool found;
    3071                 :          0 :         struct rte_flow_error error = { RTE_FLOW_ERROR_TYPE_NONE, NULL, NULL };
    3072                 :            :         struct rte_flow_action_age *age = age_action_get(actions);
    3073                 :            :         struct queue_job *job;
    3074                 :            : 
    3075                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    3076                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    3077                 :            :                 return -EINVAL;
    3078                 :          0 :         port = &ports[port_id];
    3079                 :            : 
    3080                 :          0 :         if (queue_id >= port->queue_nb) {
    3081                 :          0 :                 printf("Queue #%u is invalid\n", queue_id);
    3082                 :          0 :                 return -EINVAL;
    3083                 :            :         }
    3084                 :            : 
    3085                 :            :         found = false;
    3086                 :          0 :         tmp = &port->flow_list;
    3087                 :          0 :         while (*tmp) {
    3088                 :            :                 pf = *tmp;
    3089                 :          0 :                 if (rule_idx == pf->id) {
    3090                 :            :                         found = true;
    3091                 :            :                         break;
    3092                 :            :                 }
    3093                 :          0 :                 tmp = &(*tmp)->next;
    3094                 :            :         }
    3095                 :          0 :         if (!found) {
    3096                 :            :                 printf("Flow rule #%u is invalid\n", rule_idx);
    3097                 :          0 :                 return -EINVAL;
    3098                 :            :         }
    3099                 :            : 
    3100                 :          0 :         pt = pf->table;
    3101                 :          0 :         if (actions_idx >= pt->nb_actions_templates) {
    3102                 :            :                 printf("Actions template index #%u is invalid,"
    3103                 :            :                        " %u templates present in the table\n",
    3104                 :            :                        actions_idx, pt->nb_actions_templates);
    3105                 :          0 :                 return -EINVAL;
    3106                 :            :         }
    3107                 :            : 
    3108                 :          0 :         job = calloc(1, sizeof(*job));
    3109                 :          0 :         if (!job) {
    3110                 :            :                 printf("Queue flow create job allocate failed\n");
    3111                 :          0 :                 return -ENOMEM;
    3112                 :            :         }
    3113                 :          0 :         job->type = QUEUE_JOB_TYPE_FLOW_UPDATE;
    3114                 :            : 
    3115                 :          0 :         uf = port_flow_new(&pt->attr.flow_attr, pf->rule.pattern_ro, actions, &error);
    3116                 :          0 :         if (!uf) {
    3117                 :          0 :                 free(job);
    3118                 :          0 :                 return port_flow_complain(&error);
    3119                 :            :         }
    3120                 :            : 
    3121                 :          0 :         if (age) {
    3122                 :          0 :                 uf->age_type = ACTION_AGE_CONTEXT_TYPE_FLOW;
    3123                 :          0 :                 age->context = &uf->age_type;
    3124                 :            :         }
    3125                 :            : 
    3126                 :            :         /*
    3127                 :            :          * Poisoning to make sure PMD update it in case of error.
    3128                 :            :          */
    3129                 :            :         memset(&error, 0x44, sizeof(error));
    3130                 :          0 :         if (rte_flow_async_actions_update(port_id, queue_id, &op_attr, pf->flow,
    3131                 :            :                                           actions, actions_idx, job, &error)) {
    3132                 :          0 :                 free(uf);
    3133                 :          0 :                 free(job);
    3134                 :          0 :                 return port_flow_complain(&error);
    3135                 :            :         }
    3136                 :          0 :         uf->next = pf->next;
    3137                 :          0 :         uf->id = pf->id;
    3138                 :          0 :         uf->table = pt;
    3139                 :          0 :         uf->flow = pf->flow;
    3140                 :          0 :         *tmp = uf;
    3141                 :          0 :         job->pf = pf;
    3142                 :            : 
    3143                 :          0 :         printf("Flow rule #%"PRIu64" update enqueued\n", pf->id);
    3144                 :          0 :         return 0;
    3145                 :            : }
    3146                 :            : 
    3147                 :            : /** Enqueue indirect action create operation. */
    3148                 :            : int
    3149                 :          0 : port_queue_action_handle_create(portid_t port_id, uint32_t queue_id,
    3150                 :            :                                 bool postpone, uint32_t id,
    3151                 :            :                                 bool indirect_list,
    3152                 :            :                                 const struct rte_flow_indir_action_conf *conf,
    3153                 :            :                                 const struct rte_flow_action *action)
    3154                 :            : {
    3155                 :          0 :         const struct rte_flow_op_attr attr = { .postpone = postpone};
    3156                 :            :         struct rte_port *port;
    3157                 :            :         struct port_indirect_action *pia;
    3158                 :            :         int ret;
    3159                 :            :         struct rte_flow_error error;
    3160                 :            :         struct queue_job *job;
    3161                 :            : 
    3162                 :          0 :         ret = action_alloc(port_id, id, &pia);
    3163                 :          0 :         if (ret)
    3164                 :            :                 return ret;
    3165                 :            : 
    3166                 :          0 :         port = &ports[port_id];
    3167                 :          0 :         if (queue_id >= port->queue_nb) {
    3168                 :            :                 printf("Queue #%u is invalid\n", queue_id);
    3169                 :          0 :                 return -EINVAL;
    3170                 :            :         }
    3171                 :          0 :         job = calloc(1, sizeof(*job));
    3172                 :          0 :         if (!job) {
    3173                 :            :                 printf("Queue action create job allocate failed\n");
    3174                 :          0 :                 return -ENOMEM;
    3175                 :            :         }
    3176                 :          0 :         job->type = QUEUE_JOB_TYPE_ACTION_CREATE;
    3177                 :          0 :         job->pia = pia;
    3178                 :            : 
    3179                 :            :         /* Poisoning to make sure PMDs update it in case of error. */
    3180                 :            :         memset(&error, 0x88, sizeof(error));
    3181                 :            : 
    3182                 :          0 :         if (indirect_list)
    3183                 :            :                 queue_action_list_handle_create(port_id, queue_id, pia, job,
    3184                 :            :                                                 &attr, conf, action, &error);
    3185                 :            :         else
    3186                 :            :                 queue_action_handle_create(port_id, queue_id, pia, job, &attr,
    3187                 :            :                                            conf, action, &error);
    3188                 :            : 
    3189                 :          0 :         if (!pia->handle) {
    3190                 :          0 :                 uint32_t destroy_id = pia->id;
    3191                 :          0 :                 port_queue_action_handle_destroy(port_id, queue_id,
    3192                 :            :                                                  postpone, 1, &destroy_id);
    3193                 :          0 :                 free(job);
    3194                 :          0 :                 return port_flow_complain(&error);
    3195                 :            :         }
    3196                 :          0 :         printf("Indirect action #%u creation queued\n", pia->id);
    3197                 :          0 :         return 0;
    3198                 :            : }
    3199                 :            : 
    3200                 :            : /** Enqueue indirect action destroy operation. */
    3201                 :            : int
    3202                 :          0 : port_queue_action_handle_destroy(portid_t port_id,
    3203                 :            :                                  uint32_t queue_id, bool postpone,
    3204                 :            :                                  uint32_t n, const uint32_t *actions)
    3205                 :            : {
    3206                 :          0 :         const struct rte_flow_op_attr attr = { .postpone = postpone};
    3207                 :            :         struct rte_port *port;
    3208                 :            :         struct port_indirect_action **tmp;
    3209                 :            :         int ret = 0;
    3210                 :            :         struct queue_job *job;
    3211                 :            : 
    3212                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    3213                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    3214                 :            :                 return -EINVAL;
    3215                 :          0 :         port = &ports[port_id];
    3216                 :            : 
    3217                 :          0 :         if (queue_id >= port->queue_nb) {
    3218                 :            :                 printf("Queue #%u is invalid\n", queue_id);
    3219                 :          0 :                 return -EINVAL;
    3220                 :            :         }
    3221                 :            : 
    3222                 :          0 :         tmp = &port->actions_list;
    3223                 :          0 :         while (*tmp) {
    3224                 :            :                 uint32_t i;
    3225                 :            : 
    3226                 :          0 :                 for (i = 0; i != n; ++i) {
    3227                 :            :                         struct rte_flow_error error;
    3228                 :          0 :                         struct port_indirect_action *pia = *tmp;
    3229                 :            : 
    3230                 :          0 :                         if (actions[i] != pia->id)
    3231                 :          0 :                                 continue;
    3232                 :            :                         /*
    3233                 :            :                          * Poisoning to make sure PMDs update it in case
    3234                 :            :                          * of error.
    3235                 :            :                          */
    3236                 :            :                         memset(&error, 0x99, sizeof(error));
    3237                 :          0 :                         job = calloc(1, sizeof(*job));
    3238                 :          0 :                         if (!job) {
    3239                 :            :                                 printf("Queue action destroy job allocate failed\n");
    3240                 :          0 :                                 return -ENOMEM;
    3241                 :            :                         }
    3242                 :          0 :                         job->type = QUEUE_JOB_TYPE_ACTION_DESTROY;
    3243                 :          0 :                         job->pia = pia;
    3244                 :          0 :                         ret = pia->type == RTE_FLOW_ACTION_TYPE_INDIRECT_LIST ?
    3245                 :          0 :                               rte_flow_async_action_list_handle_destroy
    3246                 :            :                                       (port_id, queue_id,
    3247                 :            :                                        &attr, pia->list_handle,
    3248                 :          0 :                                        job, &error) :
    3249                 :          0 :                               rte_flow_async_action_handle_destroy
    3250                 :            :                                       (port_id, queue_id, &attr, pia->handle,
    3251                 :            :                                        job, &error);
    3252                 :          0 :                         if (ret) {
    3253                 :          0 :                                 free(job);
    3254                 :          0 :                                 ret = port_flow_complain(&error);
    3255                 :          0 :                                 continue;
    3256                 :            :                         }
    3257                 :          0 :                         *tmp = pia->next;
    3258                 :          0 :                         printf("Indirect action #%u destruction queued\n",
    3259                 :            :                                pia->id);
    3260                 :          0 :                         break;
    3261                 :            :                 }
    3262                 :          0 :                 if (i == n)
    3263                 :          0 :                         tmp = &(*tmp)->next;
    3264                 :            :         }
    3265                 :            :         return ret;
    3266                 :            : }
    3267                 :            : 
    3268                 :            : /** Enqueue indirect action update operation. */
    3269                 :            : int
    3270                 :          0 : port_queue_action_handle_update(portid_t port_id,
    3271                 :            :                                 uint32_t queue_id, bool postpone, uint32_t id,
    3272                 :            :                                 const struct rte_flow_action *action)
    3273                 :            : {
    3274                 :          0 :         const struct rte_flow_op_attr attr = { .postpone = postpone};
    3275                 :            :         struct rte_port *port;
    3276                 :            :         struct rte_flow_error error;
    3277                 :            :         struct rte_flow_action_handle *action_handle;
    3278                 :            :         struct queue_job *job;
    3279                 :            :         struct port_indirect_action *pia;
    3280                 :            :         struct rte_flow_update_meter_mark mtr_update;
    3281                 :            :         const void *update;
    3282                 :            : 
    3283                 :          0 :         action_handle = port_action_handle_get_by_id(port_id, id);
    3284                 :          0 :         if (!action_handle)
    3285                 :            :                 return -EINVAL;
    3286                 :            : 
    3287                 :          0 :         port = &ports[port_id];
    3288                 :          0 :         if (queue_id >= port->queue_nb) {
    3289                 :            :                 printf("Queue #%u is invalid\n", queue_id);
    3290                 :          0 :                 return -EINVAL;
    3291                 :            :         }
    3292                 :            : 
    3293                 :          0 :         job = calloc(1, sizeof(*job));
    3294                 :          0 :         if (!job) {
    3295                 :            :                 printf("Queue action update job allocate failed\n");
    3296                 :          0 :                 return -ENOMEM;
    3297                 :            :         }
    3298                 :          0 :         job->type = QUEUE_JOB_TYPE_ACTION_UPDATE;
    3299                 :            : 
    3300                 :          0 :         pia = action_get_by_id(port_id, id);
    3301                 :          0 :         if (!pia) {
    3302                 :          0 :                 free(job);
    3303                 :          0 :                 return -EINVAL;
    3304                 :            :         }
    3305                 :            : 
    3306                 :          0 :         switch (pia->type) {
    3307                 :          0 :         case RTE_FLOW_ACTION_TYPE_AGE:
    3308                 :          0 :                 update = action->conf;
    3309                 :          0 :                 break;
    3310                 :          0 :         case RTE_FLOW_ACTION_TYPE_METER_MARK:
    3311                 :          0 :                 rte_memcpy(&mtr_update.meter_mark, action->conf,
    3312                 :            :                         sizeof(struct rte_flow_action_meter_mark));
    3313                 :          0 :                 if (mtr_update.meter_mark.profile)
    3314                 :          0 :                         mtr_update.profile_valid = 1;
    3315                 :          0 :                 if (mtr_update.meter_mark.policy)
    3316                 :          0 :                         mtr_update.policy_valid = 1;
    3317                 :          0 :                 mtr_update.color_mode_valid = 1;
    3318                 :          0 :                 mtr_update.state_valid = 1;
    3319                 :            :                 update = &mtr_update;
    3320                 :          0 :                 break;
    3321                 :            :         default:
    3322                 :            :                 update = action;
    3323                 :            :                 break;
    3324                 :            :         }
    3325                 :            : 
    3326                 :          0 :         if (rte_flow_async_action_handle_update(port_id, queue_id, &attr,
    3327                 :            :                                     action_handle, update, job, &error)) {
    3328                 :          0 :                 free(job);
    3329                 :          0 :                 return port_flow_complain(&error);
    3330                 :            :         }
    3331                 :            :         printf("Indirect action #%u update queued\n", id);
    3332                 :          0 :         return 0;
    3333                 :            : }
    3334                 :            : 
    3335                 :            : void
    3336                 :          0 : port_queue_action_handle_query_update(portid_t port_id,
    3337                 :            :                                       uint32_t queue_id, bool postpone,
    3338                 :            :                                       uint32_t id,
    3339                 :            :                                       enum rte_flow_query_update_mode qu_mode,
    3340                 :            :                                       const struct rte_flow_action *action)
    3341                 :            : {
    3342                 :            :         int ret;
    3343                 :            :         struct rte_flow_error error;
    3344                 :          0 :         struct port_indirect_action *pia = action_get_by_id(port_id, id);
    3345                 :          0 :         const struct rte_flow_op_attr attr = { .postpone = postpone};
    3346                 :            :         struct queue_job *job;
    3347                 :            : 
    3348                 :          0 :         if (!pia || !pia->handle)
    3349                 :          0 :                 return;
    3350                 :          0 :         job = calloc(1, sizeof(*job));
    3351                 :          0 :         if (!job)
    3352                 :            :                 return;
    3353                 :          0 :         job->type = QUEUE_JOB_TYPE_ACTION_QUERY;
    3354                 :          0 :         job->pia = pia;
    3355                 :            : 
    3356                 :          0 :         ret = rte_flow_async_action_handle_query_update(port_id, queue_id,
    3357                 :            :                                                         &attr, pia->handle,
    3358                 :            :                                                         action,
    3359                 :          0 :                                                         &job->query,
    3360                 :            :                                                         qu_mode, job,
    3361                 :            :                                                         &error);
    3362                 :          0 :         if (ret) {
    3363                 :          0 :                 port_flow_complain(&error);
    3364                 :          0 :                 free(job);
    3365                 :            :         } else {
    3366                 :            :                 printf("port-%u: indirect action #%u update-and-query queued\n",
    3367                 :            :                        port_id, id);
    3368                 :            :         }
    3369                 :            : }
    3370                 :            : 
    3371                 :            : /** Enqueue indirect action query operation. */
    3372                 :            : int
    3373                 :          0 : port_queue_action_handle_query(portid_t port_id,
    3374                 :            :                                uint32_t queue_id, bool postpone, uint32_t id)
    3375                 :            : {
    3376                 :          0 :         const struct rte_flow_op_attr attr = { .postpone = postpone};
    3377                 :            :         struct rte_port *port;
    3378                 :            :         struct rte_flow_error error;
    3379                 :            :         struct rte_flow_action_handle *action_handle;
    3380                 :            :         struct port_indirect_action *pia;
    3381                 :            :         struct queue_job *job;
    3382                 :            : 
    3383                 :          0 :         pia = action_get_by_id(port_id, id);
    3384                 :          0 :         action_handle = pia ? pia->handle : NULL;
    3385                 :          0 :         if (!action_handle)
    3386                 :            :                 return -EINVAL;
    3387                 :            : 
    3388                 :          0 :         port = &ports[port_id];
    3389                 :          0 :         if (queue_id >= port->queue_nb) {
    3390                 :            :                 printf("Queue #%u is invalid\n", queue_id);
    3391                 :          0 :                 return -EINVAL;
    3392                 :            :         }
    3393                 :            : 
    3394                 :          0 :         job = calloc(1, sizeof(*job));
    3395                 :          0 :         if (!job) {
    3396                 :            :                 printf("Queue action update job allocate failed\n");
    3397                 :          0 :                 return -ENOMEM;
    3398                 :            :         }
    3399                 :          0 :         job->type = QUEUE_JOB_TYPE_ACTION_QUERY;
    3400                 :          0 :         job->pia = pia;
    3401                 :            : 
    3402                 :          0 :         if (rte_flow_async_action_handle_query(port_id, queue_id, &attr,
    3403                 :          0 :                                     action_handle, &job->query, job, &error)) {
    3404                 :          0 :                 free(job);
    3405                 :          0 :                 return port_flow_complain(&error);
    3406                 :            :         }
    3407                 :            :         printf("Indirect action #%u update queued\n", id);
    3408                 :          0 :         return 0;
    3409                 :            : }
    3410                 :            : 
    3411                 :            : /** Push all the queue operations in the queue to the NIC. */
    3412                 :            : int
    3413                 :          0 : port_queue_flow_push(portid_t port_id, queueid_t queue_id)
    3414                 :            : {
    3415                 :            :         struct rte_port *port;
    3416                 :            :         struct rte_flow_error error;
    3417                 :            :         int ret = 0;
    3418                 :            : 
    3419                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    3420                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    3421                 :            :                 return -EINVAL;
    3422                 :          0 :         port = &ports[port_id];
    3423                 :            : 
    3424                 :          0 :         if (queue_id >= port->queue_nb) {
    3425                 :          0 :                 printf("Queue #%u is invalid\n", queue_id);
    3426                 :          0 :                 return -EINVAL;
    3427                 :            :         }
    3428                 :            : 
    3429                 :            :         memset(&error, 0x55, sizeof(error));
    3430                 :          0 :         ret = rte_flow_push(port_id, queue_id, &error);
    3431                 :          0 :         if (ret < 0) {
    3432                 :            :                 printf("Failed to push operations in the queue\n");
    3433                 :          0 :                 return -EINVAL;
    3434                 :            :         }
    3435                 :          0 :         printf("Queue #%u operations pushed\n", queue_id);
    3436                 :          0 :         return ret;
    3437                 :            : }
    3438                 :            : 
    3439                 :            : /** Calculate the hash result for a given pattern in a given table. */
    3440                 :            : int
    3441                 :          0 : port_flow_hash_calc(portid_t port_id, uint32_t table_id,
    3442                 :            :                     uint8_t pattern_template_index, const struct rte_flow_item pattern[])
    3443                 :            : {
    3444                 :            :         uint32_t hash;
    3445                 :            :         bool found;
    3446                 :            :         struct port_table *pt;
    3447                 :            :         struct rte_port *port;
    3448                 :            :         struct rte_flow_error error;
    3449                 :            :         int ret = 0;
    3450                 :            : 
    3451                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    3452                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    3453                 :            :                 return -EINVAL;
    3454                 :          0 :         port = &ports[port_id];
    3455                 :            : 
    3456                 :            :         found = false;
    3457                 :          0 :         pt = port->table_list;
    3458                 :          0 :         while (pt) {
    3459                 :          0 :                 if (table_id == pt->id) {
    3460                 :            :                         found = true;
    3461                 :            :                         break;
    3462                 :            :                 }
    3463                 :          0 :                 pt = pt->next;
    3464                 :            :         }
    3465                 :          0 :         if (!found) {
    3466                 :            :                 printf("Table #%u is invalid\n", table_id);
    3467                 :          0 :                 return -EINVAL;
    3468                 :            :         }
    3469                 :            : 
    3470                 :            :         memset(&error, 0x55, sizeof(error));
    3471                 :          0 :         ret = rte_flow_calc_table_hash(port_id, pt->table, pattern,
    3472                 :            :                                        pattern_template_index, &hash, &error);
    3473                 :          0 :         if (ret < 0) {
    3474                 :            :                 printf("Failed to calculate hash ");
    3475                 :          0 :                 switch (abs(ret)) {
    3476                 :            :                 case ENODEV:
    3477                 :            :                         printf("no such device\n");
    3478                 :            :                         break;
    3479                 :            :                 case ENOTSUP:
    3480                 :            :                         printf("device doesn't support this operation\n");
    3481                 :            :                         break;
    3482                 :            :                 default:
    3483                 :            :                         printf("\n");
    3484                 :            :                         break;
    3485                 :            :                 }
    3486                 :          0 :                 return ret;
    3487                 :            :         }
    3488                 :          0 :         printf("Hash results 0x%x\n", hash);
    3489                 :          0 :         return 0;
    3490                 :            : }
    3491                 :            : 
    3492                 :            : /** Calculate the encap hash result for a given pattern. */
    3493                 :            : int
    3494                 :          0 : port_flow_hash_calc_encap(portid_t port_id,
    3495                 :            :                           enum rte_flow_encap_hash_field encap_hash_field,
    3496                 :            :                           const struct rte_flow_item pattern[])
    3497                 :            : {
    3498                 :            :         struct rte_flow_error error;
    3499                 :            :         int ret = 0;
    3500                 :          0 :         uint16_t hash = 0;
    3501                 :          0 :         uint8_t len = encap_hash_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT ? 2 : 1;
    3502                 :            : 
    3503                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    3504                 :            :             port_id == (portid_t)RTE_PORT_ALL) {
    3505                 :            :                 printf("Failed to calculate encap hash - not a valid port");
    3506                 :          0 :                 return -EINVAL;
    3507                 :            :         }
    3508                 :            : 
    3509                 :          0 :         ret = rte_flow_calc_encap_hash(port_id, pattern, encap_hash_field, len,
    3510                 :            :                                        (uint8_t *)&hash, &error);
    3511                 :          0 :         if (ret < 0) {
    3512                 :            :                 printf("Failed to calculate encap hash");
    3513                 :          0 :                 return ret;
    3514                 :            :         }
    3515                 :          0 :         if (encap_hash_field == RTE_FLOW_ENCAP_HASH_FIELD_SRC_PORT)
    3516                 :          0 :                 printf("encap hash result %#x\n", hash);
    3517                 :            :         else
    3518                 :          0 :                 printf("encap hash result %#x\n", *(uint8_t *)&hash);
    3519                 :            :         return 0;
    3520                 :            : }
    3521                 :            : 
    3522                 :            : /** Pull queue operation results from the queue. */
    3523                 :            : static int
    3524                 :          0 : port_queue_aged_flow_destroy(portid_t port_id, queueid_t queue_id,
    3525                 :            :                              const uint64_t *rule, int nb_flows)
    3526                 :            : {
    3527                 :          0 :         struct rte_port *port = &ports[port_id];
    3528                 :            :         struct rte_flow_op_result *res;
    3529                 :            :         struct rte_flow_error error;
    3530                 :          0 :         uint32_t n = nb_flows;
    3531                 :            :         int ret = 0;
    3532                 :            :         int i;
    3533                 :            : 
    3534                 :          0 :         res = calloc(port->queue_sz, sizeof(struct rte_flow_op_result));
    3535                 :          0 :         if (!res) {
    3536                 :            :                 printf("Failed to allocate memory for pulled results\n");
    3537                 :          0 :                 return -ENOMEM;
    3538                 :            :         }
    3539                 :            : 
    3540                 :            :         memset(&error, 0x66, sizeof(error));
    3541                 :          0 :         while (nb_flows > 0) {
    3542                 :            :                 int success = 0;
    3543                 :            : 
    3544                 :          0 :                 if (n > port->queue_sz)
    3545                 :            :                         n = port->queue_sz;
    3546                 :          0 :                 ret = port_queue_flow_destroy(port_id, queue_id, true, n, rule);
    3547                 :          0 :                 if (ret < 0) {
    3548                 :          0 :                         free(res);
    3549                 :          0 :                         return ret;
    3550                 :            :                 }
    3551                 :          0 :                 ret = rte_flow_push(port_id, queue_id, &error);
    3552                 :          0 :                 if (ret < 0) {
    3553                 :          0 :                         printf("Failed to push operations in the queue: %s\n",
    3554                 :            :                                strerror(-ret));
    3555                 :          0 :                         free(res);
    3556                 :          0 :                         return ret;
    3557                 :            :                 }
    3558                 :          0 :                 while (success < nb_flows) {
    3559                 :          0 :                         ret = rte_flow_pull(port_id, queue_id, res,
    3560                 :          0 :                                             port->queue_sz, &error);
    3561                 :          0 :                         if (ret < 0) {
    3562                 :          0 :                                 printf("Failed to pull a operation results: %s\n",
    3563                 :            :                                        strerror(-ret));
    3564                 :          0 :                                 free(res);
    3565                 :          0 :                                 return ret;
    3566                 :            :                         }
    3567                 :            : 
    3568                 :          0 :                         for (i = 0; i < ret; i++) {
    3569                 :          0 :                                 if (res[i].status == RTE_FLOW_OP_SUCCESS)
    3570                 :          0 :                                         success++;
    3571                 :            :                         }
    3572                 :            :                 }
    3573                 :          0 :                 rule += n;
    3574                 :          0 :                 nb_flows -= n;
    3575                 :            :                 n = nb_flows;
    3576                 :            :         }
    3577                 :            : 
    3578                 :          0 :         free(res);
    3579                 :          0 :         return ret;
    3580                 :            : }
    3581                 :            : 
    3582                 :            : /** List simply and destroy all aged flows per queue. */
    3583                 :            : void
    3584                 :          0 : port_queue_flow_aged(portid_t port_id, uint32_t queue_id, uint8_t destroy)
    3585                 :            : {
    3586                 :            :         void **contexts;
    3587                 :            :         int nb_context, total = 0, idx;
    3588                 :            :         uint64_t *rules = NULL;
    3589                 :            :         struct rte_port *port;
    3590                 :            :         struct rte_flow_error error;
    3591                 :            :         enum age_action_context_type *type;
    3592                 :            :         union {
    3593                 :            :                 struct port_flow *pf;
    3594                 :            :                 struct port_indirect_action *pia;
    3595                 :            :         } ctx;
    3596                 :            : 
    3597                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    3598                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    3599                 :          0 :                 return;
    3600                 :          0 :         port = &ports[port_id];
    3601                 :          0 :         if (queue_id >= port->queue_nb) {
    3602                 :            :                 printf("Error: queue #%u is invalid\n", queue_id);
    3603                 :          0 :                 return;
    3604                 :            :         }
    3605                 :          0 :         total = rte_flow_get_q_aged_flows(port_id, queue_id, NULL, 0, &error);
    3606                 :          0 :         if (total < 0) {
    3607                 :          0 :                 port_flow_complain(&error);
    3608                 :          0 :                 return;
    3609                 :            :         }
    3610                 :            :         printf("Port %u queue %u total aged flows: %d\n",
    3611                 :            :                port_id, queue_id, total);
    3612                 :          0 :         if (total == 0)
    3613                 :            :                 return;
    3614                 :          0 :         contexts = calloc(total, sizeof(void *));
    3615                 :          0 :         if (contexts == NULL) {
    3616                 :            :                 printf("Cannot allocate contexts for aged flow\n");
    3617                 :          0 :                 return;
    3618                 :            :         }
    3619                 :            :         printf("%-20s\tID\tGroup\tPrio\tAttr\n", "Type");
    3620                 :          0 :         nb_context = rte_flow_get_q_aged_flows(port_id, queue_id, contexts,
    3621                 :            :                                                total, &error);
    3622                 :          0 :         if (nb_context > total) {
    3623                 :            :                 printf("Port %u queue %u get aged flows count(%d) > total(%d)\n",
    3624                 :            :                        port_id, queue_id, nb_context, total);
    3625                 :          0 :                 free(contexts);
    3626                 :          0 :                 return;
    3627                 :            :         }
    3628                 :          0 :         if (destroy) {
    3629                 :          0 :                 rules = malloc(sizeof(uint32_t) * nb_context);
    3630                 :          0 :                 if (rules == NULL)
    3631                 :            :                         printf("Cannot allocate memory for destroy aged flow\n");
    3632                 :            :         }
    3633                 :            :         total = 0;
    3634                 :          0 :         for (idx = 0; idx < nb_context; idx++) {
    3635                 :          0 :                 if (!contexts[idx]) {
    3636                 :            :                         printf("Error: get Null context in port %u queue %u\n",
    3637                 :            :                                port_id, queue_id);
    3638                 :          0 :                         continue;
    3639                 :            :                 }
    3640                 :            :                 type = (enum age_action_context_type *)contexts[idx];
    3641                 :          0 :                 switch (*type) {
    3642                 :          0 :                 case ACTION_AGE_CONTEXT_TYPE_FLOW:
    3643                 :          0 :                         ctx.pf = container_of(type, struct port_flow, age_type);
    3644                 :          0 :                         printf("%-20s\t%" PRIu64 "\t%" PRIu32 "\t%" PRIu32
    3645                 :            :                                                                  "\t%c%c%c\t\n",
    3646                 :            :                                "Flow",
    3647                 :            :                                ctx.pf->id,
    3648                 :            :                                ctx.pf->rule.attr->group,
    3649                 :            :                                ctx.pf->rule.attr->priority,
    3650                 :          0 :                                ctx.pf->rule.attr->ingress ? 'i' : '-',
    3651                 :          0 :                                ctx.pf->rule.attr->egress ? 'e' : '-',
    3652                 :          0 :                                ctx.pf->rule.attr->transfer ? 't' : '-');
    3653                 :          0 :                         if (rules != NULL) {
    3654                 :          0 :                                 rules[total] = ctx.pf->id;
    3655                 :          0 :                                 total++;
    3656                 :            :                         }
    3657                 :            :                         break;
    3658                 :          0 :                 case ACTION_AGE_CONTEXT_TYPE_INDIRECT_ACTION:
    3659                 :          0 :                         ctx.pia = container_of(type,
    3660                 :            :                                                struct port_indirect_action,
    3661                 :            :                                                age_type);
    3662                 :          0 :                         printf("%-20s\t%" PRIu32 "\n", "Indirect action",
    3663                 :            :                                ctx.pia->id);
    3664                 :            :                         break;
    3665                 :          0 :                 default:
    3666                 :            :                         printf("Error: invalid context type %u\n", port_id);
    3667                 :            :                         break;
    3668                 :            :                 }
    3669                 :            :         }
    3670                 :          0 :         if (rules != NULL) {
    3671                 :          0 :                 port_queue_aged_flow_destroy(port_id, queue_id, rules, total);
    3672                 :          0 :                 free(rules);
    3673                 :            :         }
    3674                 :            :         printf("\n%d flows destroyed\n", total);
    3675                 :          0 :         free(contexts);
    3676                 :            : }
    3677                 :            : 
    3678                 :            : /** Pull queue operation results from the queue. */
    3679                 :            : int
    3680                 :          0 : port_queue_flow_pull(portid_t port_id, queueid_t queue_id)
    3681                 :            : {
    3682                 :            :         struct rte_port *port;
    3683                 :            :         struct rte_flow_op_result *res;
    3684                 :            :         struct rte_flow_error error;
    3685                 :            :         int ret = 0;
    3686                 :            :         int success = 0;
    3687                 :            :         int i;
    3688                 :            :         struct queue_job *job;
    3689                 :            : 
    3690                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    3691                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    3692                 :            :                 return -EINVAL;
    3693                 :          0 :         port = &ports[port_id];
    3694                 :            : 
    3695                 :          0 :         if (queue_id >= port->queue_nb) {
    3696                 :          0 :                 printf("Queue #%u is invalid\n", queue_id);
    3697                 :          0 :                 return -EINVAL;
    3698                 :            :         }
    3699                 :            : 
    3700                 :          0 :         res = calloc(port->queue_sz, sizeof(struct rte_flow_op_result));
    3701                 :          0 :         if (!res) {
    3702                 :            :                 printf("Failed to allocate memory for pulled results\n");
    3703                 :          0 :                 return -ENOMEM;
    3704                 :            :         }
    3705                 :            : 
    3706                 :            :         memset(&error, 0x66, sizeof(error));
    3707                 :          0 :         ret = rte_flow_pull(port_id, queue_id, res,
    3708                 :            :                                  port->queue_sz, &error);
    3709                 :          0 :         if (ret < 0) {
    3710                 :            :                 printf("Failed to pull a operation results\n");
    3711                 :          0 :                 free(res);
    3712                 :          0 :                 return -EINVAL;
    3713                 :            :         }
    3714                 :            : 
    3715                 :          0 :         for (i = 0; i < ret; i++) {
    3716                 :          0 :                 if (res[i].status == RTE_FLOW_OP_SUCCESS)
    3717                 :          0 :                         success++;
    3718                 :          0 :                 job = (struct queue_job *)res[i].user_data;
    3719                 :          0 :                 if (job->type == QUEUE_JOB_TYPE_FLOW_DESTROY ||
    3720                 :            :                     job->type == QUEUE_JOB_TYPE_FLOW_UPDATE)
    3721                 :          0 :                         free(job->pf);
    3722                 :          0 :                 else if (job->type == QUEUE_JOB_TYPE_ACTION_DESTROY)
    3723                 :          0 :                         free(job->pia);
    3724                 :          0 :                 else if (job->type == QUEUE_JOB_TYPE_ACTION_QUERY)
    3725                 :          0 :                         port_action_handle_query_dump(port_id, job->pia,
    3726                 :            :                                                       &job->query);
    3727                 :          0 :                 free(job);
    3728                 :            :         }
    3729                 :          0 :         printf("Queue #%u pulled %u operations (%u failed, %u succeeded)\n",
    3730                 :            :                queue_id, ret, ret - success, success);
    3731                 :          0 :         free(res);
    3732                 :          0 :         return ret;
    3733                 :            : }
    3734                 :            : 
    3735                 :            : /* Set group miss actions */
    3736                 :            : int
    3737                 :          0 : port_queue_group_set_miss_actions(portid_t port_id, const struct rte_flow_attr *attr,
    3738                 :            :                                   const struct rte_flow_action *actions)
    3739                 :            : {
    3740                 :          0 :         struct rte_flow_group_attr gattr = {
    3741                 :          0 :                 .ingress = attr->ingress,
    3742                 :          0 :                 .egress = attr->egress,
    3743                 :          0 :                 .transfer = attr->transfer,
    3744                 :            :         };
    3745                 :            :         struct rte_flow_error error;
    3746                 :            :         int ret = 0;
    3747                 :            : 
    3748                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    3749                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    3750                 :            :                 return -EINVAL;
    3751                 :            : 
    3752                 :            :         memset(&error, 0x66, sizeof(error));
    3753                 :          0 :         ret = rte_flow_group_set_miss_actions(port_id, attr->group, &gattr, actions, &error);
    3754                 :            : 
    3755                 :          0 :         if (ret < 0)
    3756                 :          0 :                 return port_flow_complain(&error);
    3757                 :            : 
    3758                 :          0 :         printf("Group #%u set miss actions succeeded\n", attr->group);
    3759                 :          0 :         return ret;
    3760                 :            : }
    3761                 :            : 
    3762                 :            : /** Create flow rule. */
    3763                 :            : int
    3764                 :          0 : port_flow_create(portid_t port_id,
    3765                 :            :                  const struct rte_flow_attr *attr,
    3766                 :            :                  const struct rte_flow_item *pattern,
    3767                 :            :                  const struct rte_flow_action *actions,
    3768                 :            :                  const struct tunnel_ops *tunnel_ops,
    3769                 :            :                  uintptr_t user_id)
    3770                 :            : {
    3771                 :            :         struct rte_flow *flow;
    3772                 :            :         struct rte_port *port;
    3773                 :            :         struct port_flow *pf;
    3774                 :            :         uint32_t id = 0;
    3775                 :            :         struct rte_flow_error error;
    3776                 :            :         struct port_flow_tunnel *pft = NULL;
    3777                 :            :         struct rte_flow_action_age *age = age_action_get(actions);
    3778                 :            : 
    3779                 :          0 :         port = &ports[port_id];
    3780                 :          0 :         if (port->flow_list) {
    3781                 :          0 :                 if (port->flow_list->id == UINT32_MAX) {
    3782                 :          0 :                         fprintf(stderr,
    3783                 :            :                                 "Highest rule ID is already assigned, delete it first");
    3784                 :          0 :                         return -ENOMEM;
    3785                 :            :                 }
    3786                 :          0 :                 id = port->flow_list->id + 1;
    3787                 :            :         }
    3788                 :          0 :         if (tunnel_ops->enabled) {
    3789                 :          0 :                 pft = port_flow_tunnel_offload_cmd_prep(port_id, pattern,
    3790                 :            :                                                         actions, tunnel_ops);
    3791                 :          0 :                 if (!pft)
    3792                 :            :                         return -ENOENT;
    3793                 :          0 :                 if (pft->items)
    3794                 :            :                         pattern = pft->items;
    3795                 :          0 :                 if (pft->actions)
    3796                 :            :                         actions = pft->actions;
    3797                 :            :         }
    3798                 :          0 :         pf = port_flow_new(attr, pattern, actions, &error);
    3799                 :          0 :         if (!pf)
    3800                 :          0 :                 return port_flow_complain(&error);
    3801                 :          0 :         if (age) {
    3802                 :          0 :                 pf->age_type = ACTION_AGE_CONTEXT_TYPE_FLOW;
    3803                 :          0 :                 age->context = &pf->age_type;
    3804                 :            :         }
    3805                 :            :         /* Poisoning to make sure PMDs update it in case of error. */
    3806                 :            :         memset(&error, 0x22, sizeof(error));
    3807                 :          0 :         flow = rte_flow_create(port_id, attr, pattern, actions, &error);
    3808                 :          0 :         if (!flow) {
    3809                 :          0 :                 if (tunnel_ops->enabled)
    3810                 :          0 :                         port_flow_tunnel_offload_cmd_release(port_id,
    3811                 :            :                                                              tunnel_ops, pft);
    3812                 :          0 :                 free(pf);
    3813                 :          0 :                 return port_flow_complain(&error);
    3814                 :            :         }
    3815                 :          0 :         pf->next = port->flow_list;
    3816                 :          0 :         pf->id = id;
    3817                 :          0 :         pf->user_id = user_id;
    3818                 :          0 :         pf->flow = flow;
    3819                 :          0 :         port->flow_list = pf;
    3820                 :          0 :         if (tunnel_ops->enabled)
    3821                 :          0 :                 port_flow_tunnel_offload_cmd_release(port_id, tunnel_ops, pft);
    3822                 :          0 :         if (user_id)
    3823                 :          0 :                 printf("Flow rule #%"PRIu64" created, user-id 0x%"PRIx64"\n",
    3824                 :            :                        pf->id, pf->user_id);
    3825                 :            :         else
    3826                 :          0 :                 printf("Flow rule #%"PRIu64" created\n", pf->id);
    3827                 :            :         return 0;
    3828                 :            : }
    3829                 :            : 
    3830                 :            : /** Destroy a number of flow rules. */
    3831                 :            : int
    3832                 :          0 : port_flow_destroy(portid_t port_id, uint32_t n, const uint64_t *rule,
    3833                 :            :                   bool is_user_id)
    3834                 :            : {
    3835                 :            :         struct rte_port *port;
    3836                 :            :         struct port_flow **tmp;
    3837                 :            :         int ret = 0;
    3838                 :            : 
    3839                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    3840                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    3841                 :            :                 return -EINVAL;
    3842                 :          0 :         port = &ports[port_id];
    3843                 :          0 :         tmp = &port->flow_list;
    3844                 :          0 :         while (*tmp) {
    3845                 :            :                 uint32_t i;
    3846                 :            : 
    3847                 :          0 :                 for (i = 0; i != n; ++i) {
    3848                 :            :                         struct rte_flow_error error;
    3849                 :          0 :                         struct port_flow *pf = *tmp;
    3850                 :            : 
    3851                 :          0 :                         if (rule[i] != (is_user_id ? pf->user_id : pf->id))
    3852                 :          0 :                                 continue;
    3853                 :            :                         /*
    3854                 :            :                          * Poisoning to make sure PMDs update it in case
    3855                 :            :                          * of error.
    3856                 :            :                          */
    3857                 :            :                         memset(&error, 0x33, sizeof(error));
    3858                 :          0 :                         if (rte_flow_destroy(port_id, pf->flow, &error)) {
    3859                 :          0 :                                 ret = port_flow_complain(&error);
    3860                 :          0 :                                 continue;
    3861                 :            :                         }
    3862                 :          0 :                         if (is_user_id)
    3863                 :          0 :                                 printf("Flow rule #%"PRIu64" destroyed, "
    3864                 :            :                                        "user-id 0x%"PRIx64"\n",
    3865                 :            :                                        pf->id, pf->user_id);
    3866                 :            :                         else
    3867                 :          0 :                                 printf("Flow rule #%"PRIu64" destroyed\n",
    3868                 :            :                                        pf->id);
    3869                 :          0 :                         *tmp = pf->next;
    3870                 :          0 :                         free(pf);
    3871                 :          0 :                         break;
    3872                 :            :                 }
    3873                 :          0 :                 if (i == n)
    3874                 :          0 :                         tmp = &(*tmp)->next;
    3875                 :            :         }
    3876                 :            :         return ret;
    3877                 :            : }
    3878                 :            : 
    3879                 :            : /** Update a flow rule with new actions. */
    3880                 :            : int
    3881                 :          0 : port_flow_update(portid_t port_id, uint32_t rule_id,
    3882                 :            :                  const struct rte_flow_action *actions, bool is_user_id)
    3883                 :            : {
    3884                 :            :         struct rte_port *port;
    3885                 :            :         struct port_flow **flow_list, *uf;
    3886                 :            :         struct rte_flow_action_age *age = age_action_get(actions);
    3887                 :            : 
    3888                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    3889                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    3890                 :            :                 return -EINVAL;
    3891                 :          0 :         port = &ports[port_id];
    3892                 :          0 :         flow_list = &port->flow_list;
    3893                 :          0 :         while (*flow_list) {
    3894                 :            :                 struct port_flow *flow = *flow_list;
    3895                 :            :                 struct rte_flow_error error;
    3896                 :            : 
    3897                 :          0 :                 if (rule_id != (is_user_id ? flow->user_id : flow->id)) {
    3898                 :          0 :                         flow_list = &flow->next;
    3899                 :          0 :                         continue;
    3900                 :            :                 }
    3901                 :            : 
    3902                 :            :                 /* Update flow action(s) with new action(s) */
    3903                 :          0 :                 uf = port_flow_new(flow->rule.attr_ro, flow->rule.pattern_ro, actions, &error);
    3904                 :          0 :                 if (!uf)
    3905                 :          0 :                         return port_flow_complain(&error);
    3906                 :          0 :                 if (age) {
    3907                 :          0 :                         flow->age_type = ACTION_AGE_CONTEXT_TYPE_FLOW;
    3908                 :          0 :                         age->context = &flow->age_type;
    3909                 :            :                 }
    3910                 :            : 
    3911                 :            :                 /*
    3912                 :            :                  * Poisoning to make sure PMDs update it in case
    3913                 :            :                  * of error.
    3914                 :            :                  */
    3915                 :            :                 memset(&error, 0x33, sizeof(error));
    3916                 :          0 :                 if (rte_flow_actions_update(port_id, flow->flow, actions,
    3917                 :            :                                             &error))
    3918                 :          0 :                         return port_flow_complain(&error);
    3919                 :          0 :                 if (is_user_id)
    3920                 :          0 :                         printf("Flow rule #%"PRIu64" updated with new actions,"
    3921                 :            :                                " user-id 0x%"PRIx64"\n",
    3922                 :            :                                flow->id, flow->user_id);
    3923                 :            :                 else
    3924                 :          0 :                         printf("Flow rule #%"PRIu64
    3925                 :            :                                " updated with new actions\n",
    3926                 :            :                                flow->id);
    3927                 :            : 
    3928                 :          0 :                 uf->next = flow->next;
    3929                 :          0 :                 uf->id = flow->id;
    3930                 :          0 :                 uf->user_id = flow->user_id;
    3931                 :          0 :                 uf->flow = flow->flow;
    3932                 :          0 :                 *flow_list = uf;
    3933                 :            : 
    3934                 :          0 :                 free(flow);
    3935                 :          0 :                 return 0;
    3936                 :            :         }
    3937                 :            :         printf("Failed to find flow %"PRIu32"\n", rule_id);
    3938                 :          0 :         return -EINVAL;
    3939                 :            : }
    3940                 :            : 
    3941                 :            : /** Remove all flow rules. */
    3942                 :            : int
    3943                 :          0 : port_flow_flush(portid_t port_id)
    3944                 :            : {
    3945                 :            :         struct rte_flow_error error;
    3946                 :            :         struct rte_port *port;
    3947                 :            :         int ret = 0;
    3948                 :            : 
    3949                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    3950                 :            :                 port_id == (portid_t)RTE_PORT_ALL)
    3951                 :            :                 return -EINVAL;
    3952                 :            : 
    3953                 :          0 :         port = &ports[port_id];
    3954                 :            : 
    3955                 :          0 :         if (port->flow_list == NULL)
    3956                 :            :                 return ret;
    3957                 :            : 
    3958                 :            :         /* Poisoning to make sure PMDs update it in case of error. */
    3959                 :            :         memset(&error, 0x44, sizeof(error));
    3960                 :          0 :         if (rte_flow_flush(port_id, &error)) {
    3961                 :          0 :                 port_flow_complain(&error);
    3962                 :            :         }
    3963                 :            : 
    3964                 :          0 :         while (port->flow_list) {
    3965                 :          0 :                 struct port_flow *pf = port->flow_list->next;
    3966                 :            : 
    3967                 :          0 :                 free(port->flow_list);
    3968                 :          0 :                 port->flow_list = pf;
    3969                 :            :         }
    3970                 :            :         return ret;
    3971                 :            : }
    3972                 :            : 
    3973                 :            : /** Dump flow rules. */
    3974                 :            : int
    3975                 :          0 : port_flow_dump(portid_t port_id, bool dump_all, uint64_t rule_id,
    3976                 :            :                 const char *file_name, bool is_user_id)
    3977                 :            : {
    3978                 :            :         int ret = 0;
    3979                 :          0 :         FILE *file = stdout;
    3980                 :            :         struct rte_flow_error error;
    3981                 :            :         struct rte_port *port;
    3982                 :            :         struct port_flow *pflow;
    3983                 :            :         struct rte_flow *tmpFlow = NULL;
    3984                 :            :         bool found = false;
    3985                 :            : 
    3986                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    3987                 :            :                 port_id == (portid_t)RTE_PORT_ALL)
    3988                 :            :                 return -EINVAL;
    3989                 :            : 
    3990                 :          0 :         if (!dump_all) {
    3991                 :          0 :                 port = &ports[port_id];
    3992                 :          0 :                 pflow = port->flow_list;
    3993                 :          0 :                 while (pflow) {
    3994                 :          0 :                         if (rule_id !=
    3995                 :          0 :                             (is_user_id ? pflow->user_id : pflow->id)) {
    3996                 :          0 :                                 pflow = pflow->next;
    3997                 :            :                         } else {
    3998                 :          0 :                                 tmpFlow = pflow->flow;
    3999                 :          0 :                                 if (tmpFlow)
    4000                 :            :                                         found = true;
    4001                 :            :                                 break;
    4002                 :            :                         }
    4003                 :            :                 }
    4004                 :          0 :                 if (found == false) {
    4005                 :          0 :                         fprintf(stderr, "Failed to dump to flow %"PRIu64"\n",
    4006                 :            :                                 rule_id);
    4007                 :          0 :                         return -EINVAL;
    4008                 :            :                 }
    4009                 :            :         }
    4010                 :            : 
    4011                 :          0 :         if (file_name && strlen(file_name)) {
    4012                 :          0 :                 file = fopen(file_name, "w");
    4013                 :          0 :                 if (!file) {
    4014                 :          0 :                         fprintf(stderr, "Failed to create file %s: %s\n",
    4015                 :          0 :                                 file_name, strerror(errno));
    4016                 :          0 :                         return -errno;
    4017                 :            :                 }
    4018                 :            :         }
    4019                 :            : 
    4020                 :          0 :         if (!dump_all)
    4021                 :          0 :                 ret = rte_flow_dev_dump(port_id, tmpFlow, file, &error);
    4022                 :            :         else
    4023                 :          0 :                 ret = rte_flow_dev_dump(port_id, NULL, file, &error);
    4024                 :          0 :         if (ret) {
    4025                 :          0 :                 port_flow_complain(&error);
    4026                 :          0 :                 fprintf(stderr, "Failed to dump flow: %s\n", strerror(-ret));
    4027                 :            :         } else
    4028                 :            :                 printf("Flow dump finished\n");
    4029                 :          0 :         if (file_name && strlen(file_name))
    4030                 :          0 :                 fclose(file);
    4031                 :            :         return ret;
    4032                 :            : }
    4033                 :            : 
    4034                 :            : /** Query a flow rule. */
    4035                 :            : int
    4036                 :          0 : port_flow_query(portid_t port_id, uint64_t rule,
    4037                 :            :                 const struct rte_flow_action *action, bool is_user_id)
    4038                 :            : {
    4039                 :            :         struct rte_flow_error error;
    4040                 :            :         struct rte_port *port;
    4041                 :            :         struct port_flow *pf;
    4042                 :            :         const char *name;
    4043                 :            :         union {
    4044                 :            :                 struct rte_flow_query_count count;
    4045                 :            :                 struct rte_flow_action_rss rss_conf;
    4046                 :            :                 struct rte_flow_query_age age;
    4047                 :            :         } query;
    4048                 :            :         int ret;
    4049                 :            : 
    4050                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    4051                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    4052                 :            :                 return -EINVAL;
    4053                 :          0 :         port = &ports[port_id];
    4054                 :          0 :         for (pf = port->flow_list; pf; pf = pf->next)
    4055                 :          0 :                 if ((is_user_id ? pf->user_id : pf->id) == rule)
    4056                 :            :                         break;
    4057                 :          0 :         if (!pf) {
    4058                 :          0 :                 fprintf(stderr, "Flow rule #%"PRIu64" not found\n", rule);
    4059                 :          0 :                 return -ENOENT;
    4060                 :            :         }
    4061                 :          0 :         ret = rte_flow_conv(RTE_FLOW_CONV_OP_ACTION_NAME_PTR,
    4062                 :            :                             &name, sizeof(name),
    4063                 :          0 :                             (void *)(uintptr_t)action->type, &error);
    4064                 :          0 :         if (ret < 0)
    4065                 :          0 :                 return port_flow_complain(&error);
    4066                 :          0 :         switch (action->type) {
    4067                 :            :         case RTE_FLOW_ACTION_TYPE_COUNT:
    4068                 :            :         case RTE_FLOW_ACTION_TYPE_RSS:
    4069                 :            :         case RTE_FLOW_ACTION_TYPE_AGE:
    4070                 :            :                 break;
    4071                 :          0 :         default:
    4072                 :          0 :                 fprintf(stderr, "Cannot query action type %d (%s)\n",
    4073                 :            :                         action->type, name);
    4074                 :          0 :                 return -ENOTSUP;
    4075                 :            :         }
    4076                 :            :         /* Poisoning to make sure PMDs update it in case of error. */
    4077                 :            :         memset(&error, 0x55, sizeof(error));
    4078                 :            :         memset(&query, 0, sizeof(query));
    4079                 :          0 :         if (rte_flow_query(port_id, pf->flow, action, &query, &error))
    4080                 :          0 :                 return port_flow_complain(&error);
    4081                 :          0 :         switch (action->type) {
    4082                 :          0 :         case RTE_FLOW_ACTION_TYPE_COUNT:
    4083                 :          0 :                 printf("%s:\n"
    4084                 :            :                        " hits_set: %u\n"
    4085                 :            :                        " bytes_set: %u\n"
    4086                 :            :                        " hits: %" PRIu64 "\n"
    4087                 :            :                        " bytes: %" PRIu64 "\n",
    4088                 :            :                        name,
    4089                 :          0 :                        query.count.hits_set,
    4090                 :          0 :                        query.count.bytes_set,
    4091                 :            :                        query.count.hits,
    4092                 :            :                        query.count.bytes);
    4093                 :            :                 break;
    4094                 :          0 :         case RTE_FLOW_ACTION_TYPE_RSS:
    4095                 :          0 :                 rss_config_display(&query.rss_conf);
    4096                 :          0 :                 break;
    4097                 :          0 :         case RTE_FLOW_ACTION_TYPE_AGE:
    4098                 :          0 :                 printf("%s:\n"
    4099                 :            :                        " aged: %u\n"
    4100                 :            :                        " sec_since_last_hit_valid: %u\n"
    4101                 :            :                        " sec_since_last_hit: %" PRIu32 "\n",
    4102                 :            :                        name,
    4103                 :          0 :                        query.age.aged,
    4104                 :          0 :                        query.age.sec_since_last_hit_valid,
    4105                 :          0 :                        query.age.sec_since_last_hit);
    4106                 :            :                 break;
    4107                 :          0 :         default:
    4108                 :          0 :                 fprintf(stderr,
    4109                 :            :                         "Cannot display result for action type %d (%s)\n",
    4110                 :            :                         action->type, name);
    4111                 :            :                 break;
    4112                 :            :         }
    4113                 :            :         return 0;
    4114                 :            : }
    4115                 :            : 
    4116                 :            : /** List simply and destroy all aged flows. */
    4117                 :            : void
    4118                 :          0 : port_flow_aged(portid_t port_id, uint8_t destroy)
    4119                 :            : {
    4120                 :            :         void **contexts;
    4121                 :            :         int nb_context, total = 0, idx;
    4122                 :            :         struct rte_flow_error error;
    4123                 :            :         enum age_action_context_type *type;
    4124                 :            :         union {
    4125                 :            :                 struct port_flow *pf;
    4126                 :            :                 struct port_indirect_action *pia;
    4127                 :            :         } ctx;
    4128                 :            : 
    4129                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    4130                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    4131                 :          0 :                 return;
    4132                 :          0 :         total = rte_flow_get_aged_flows(port_id, NULL, 0, &error);
    4133                 :            :         printf("Port %u total aged flows: %d\n", port_id, total);
    4134                 :          0 :         if (total < 0) {
    4135                 :          0 :                 port_flow_complain(&error);
    4136                 :          0 :                 return;
    4137                 :            :         }
    4138                 :          0 :         if (total == 0)
    4139                 :            :                 return;
    4140                 :          0 :         contexts = malloc(sizeof(void *) * total);
    4141                 :          0 :         if (contexts == NULL) {
    4142                 :          0 :                 fprintf(stderr, "Cannot allocate contexts for aged flow\n");
    4143                 :          0 :                 return;
    4144                 :            :         }
    4145                 :            :         printf("%-20s\tID\tGroup\tPrio\tAttr\n", "Type");
    4146                 :          0 :         nb_context = rte_flow_get_aged_flows(port_id, contexts, total, &error);
    4147                 :          0 :         if (nb_context != total) {
    4148                 :          0 :                 fprintf(stderr,
    4149                 :            :                         "Port:%d get aged flows count(%d) != total(%d)\n",
    4150                 :            :                         port_id, nb_context, total);
    4151                 :          0 :                 free(contexts);
    4152                 :          0 :                 return;
    4153                 :            :         }
    4154                 :            :         total = 0;
    4155                 :          0 :         for (idx = 0; idx < nb_context; idx++) {
    4156                 :          0 :                 if (!contexts[idx]) {
    4157                 :          0 :                         fprintf(stderr, "Error: get Null context in port %u\n",
    4158                 :            :                                 port_id);
    4159                 :          0 :                         continue;
    4160                 :            :                 }
    4161                 :            :                 type = (enum age_action_context_type *)contexts[idx];
    4162                 :          0 :                 switch (*type) {
    4163                 :          0 :                 case ACTION_AGE_CONTEXT_TYPE_FLOW: {
    4164                 :            :                         uint64_t flow_id;
    4165                 :          0 :                         ctx.pf = container_of(type, struct port_flow, age_type);
    4166                 :          0 :                         flow_id = ctx.pf->id;
    4167                 :          0 :                         printf("%-20s\t%" PRIu64 "\t%" PRIu32 "\t%" PRIu32
    4168                 :            :                                                                  "\t%c%c%c\t\n",
    4169                 :            :                                "Flow",
    4170                 :            :                                ctx.pf->id,
    4171                 :            :                                ctx.pf->rule.attr->group,
    4172                 :            :                                ctx.pf->rule.attr->priority,
    4173                 :          0 :                                ctx.pf->rule.attr->ingress ? 'i' : '-',
    4174                 :          0 :                                ctx.pf->rule.attr->egress ? 'e' : '-',
    4175                 :          0 :                                ctx.pf->rule.attr->transfer ? 't' : '-');
    4176                 :          0 :                         if (destroy && !port_flow_destroy(port_id, 1,
    4177                 :            :                                                           &flow_id, false))
    4178                 :          0 :                                 total++;
    4179                 :            :                         break;
    4180                 :            :                 }
    4181                 :          0 :                 case ACTION_AGE_CONTEXT_TYPE_INDIRECT_ACTION:
    4182                 :          0 :                         ctx.pia = container_of(type,
    4183                 :            :                                         struct port_indirect_action, age_type);
    4184                 :          0 :                         printf("%-20s\t%" PRIu32 "\n", "Indirect action",
    4185                 :            :                                ctx.pia->id);
    4186                 :            :                         break;
    4187                 :          0 :                 default:
    4188                 :          0 :                         fprintf(stderr, "Error: invalid context type %u\n",
    4189                 :            :                                 port_id);
    4190                 :            :                         break;
    4191                 :            :                 }
    4192                 :            :         }
    4193                 :            :         printf("\n%d flows destroyed\n", total);
    4194                 :          0 :         free(contexts);
    4195                 :            : }
    4196                 :            : 
    4197                 :            : /** List flow rules. */
    4198                 :            : void
    4199                 :          0 : port_flow_list(portid_t port_id, uint32_t n, const uint32_t *group)
    4200                 :            : {
    4201                 :            :         struct rte_port *port;
    4202                 :            :         struct port_flow *pf;
    4203                 :          0 :         struct port_flow *list = NULL;
    4204                 :            :         uint32_t i;
    4205                 :            : 
    4206                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    4207                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    4208                 :          0 :                 return;
    4209                 :          0 :         port = &ports[port_id];
    4210                 :          0 :         if (!port->flow_list)
    4211                 :            :                 return;
    4212                 :            :         /* Sort flows by group, priority and ID. */
    4213                 :          0 :         for (pf = port->flow_list; pf != NULL; pf = pf->next) {
    4214                 :            :                 struct port_flow **tmp;
    4215                 :          0 :                 const struct rte_flow_attr *curr = pf->rule.attr;
    4216                 :            : 
    4217                 :          0 :                 if (n) {
    4218                 :            :                         /* Filter out unwanted groups. */
    4219                 :          0 :                         for (i = 0; i != n; ++i)
    4220                 :          0 :                                 if (curr->group == group[i])
    4221                 :            :                                         break;
    4222                 :          0 :                         if (i == n)
    4223                 :          0 :                                 continue;
    4224                 :            :                 }
    4225                 :          0 :                 for (tmp = &list; *tmp; tmp = &(*tmp)->tmp) {
    4226                 :          0 :                         const struct rte_flow_attr *comp = (*tmp)->rule.attr;
    4227                 :            : 
    4228                 :          0 :                         if (curr->group > comp->group ||
    4229                 :          0 :                             (curr->group == comp->group &&
    4230                 :          0 :                              curr->priority > comp->priority) ||
    4231                 :          0 :                             (curr->group == comp->group &&
    4232                 :          0 :                              curr->priority == comp->priority &&
    4233                 :          0 :                              pf->id > (*tmp)->id))
    4234                 :            :                                 continue;
    4235                 :            :                         break;
    4236                 :            :                 }
    4237                 :          0 :                 pf->tmp = *tmp;
    4238                 :          0 :                 *tmp = pf;
    4239                 :            :         }
    4240                 :            :         printf("ID\tGroup\tPrio\tAttr\tRule\n");
    4241                 :          0 :         for (pf = list; pf != NULL; pf = pf->tmp) {
    4242                 :          0 :                 const struct rte_flow_item *item = pf->rule.pattern;
    4243                 :          0 :                 const struct rte_flow_action *action = pf->rule.actions;
    4244                 :            :                 const char *name;
    4245                 :            : 
    4246                 :          0 :                 printf("%" PRIu64 "\t%" PRIu32 "\t%" PRIu32 "\t%c%c%c\t",
    4247                 :            :                        pf->id,
    4248                 :            :                        pf->rule.attr->group,
    4249                 :            :                        pf->rule.attr->priority,
    4250                 :          0 :                        pf->rule.attr->ingress ? 'i' : '-',
    4251                 :          0 :                        pf->rule.attr->egress ? 'e' : '-',
    4252                 :          0 :                        pf->rule.attr->transfer ? 't' : '-');
    4253                 :          0 :                 while (item->type != RTE_FLOW_ITEM_TYPE_END) {
    4254                 :          0 :                         if ((uint32_t)item->type > INT_MAX)
    4255                 :          0 :                                 name = "PMD_INTERNAL";
    4256                 :          0 :                         else if (rte_flow_conv(RTE_FLOW_CONV_OP_ITEM_NAME_PTR,
    4257                 :            :                                           &name, sizeof(name),
    4258                 :          0 :                                           (void *)(uintptr_t)item->type,
    4259                 :            :                                           NULL) <= 0)
    4260                 :          0 :                                 name = "[UNKNOWN]";
    4261                 :          0 :                         if (item->type != RTE_FLOW_ITEM_TYPE_VOID)
    4262                 :          0 :                                 printf("%s ", name);
    4263                 :          0 :                         ++item;
    4264                 :            :                 }
    4265                 :            :                 printf("=>");
    4266                 :          0 :                 while (action->type != RTE_FLOW_ACTION_TYPE_END) {
    4267                 :          0 :                         if ((uint32_t)action->type > INT_MAX)
    4268                 :          0 :                                 name = "PMD_INTERNAL";
    4269                 :          0 :                         else if (rte_flow_conv(RTE_FLOW_CONV_OP_ACTION_NAME_PTR,
    4270                 :            :                                           &name, sizeof(name),
    4271                 :          0 :                                           (void *)(uintptr_t)action->type,
    4272                 :            :                                           NULL) <= 0)
    4273                 :          0 :                                 name = "[UNKNOWN]";
    4274                 :          0 :                         if (action->type != RTE_FLOW_ACTION_TYPE_VOID)
    4275                 :          0 :                                 printf(" %s", name);
    4276                 :          0 :                         ++action;
    4277                 :            :                 }
    4278                 :            :                 printf("\n");
    4279                 :            :         }
    4280                 :            : }
    4281                 :            : 
    4282                 :            : /** Restrict ingress traffic to the defined flow rules. */
    4283                 :            : int
    4284                 :          0 : port_flow_isolate(portid_t port_id, int set)
    4285                 :            : {
    4286                 :            :         struct rte_flow_error error;
    4287                 :            : 
    4288                 :            :         /* Poisoning to make sure PMDs update it in case of error. */
    4289                 :            :         memset(&error, 0x66, sizeof(error));
    4290                 :          0 :         if (rte_flow_isolate(port_id, set, &error))
    4291                 :          0 :                 return port_flow_complain(&error);
    4292                 :          0 :         printf("Ingress traffic on port %u is %s to the defined flow rules\n",
    4293                 :            :                port_id,
    4294                 :            :                set ? "now restricted" : "not restricted anymore");
    4295                 :          0 :         return 0;
    4296                 :            : }
    4297                 :            : 
    4298                 :            : /*
    4299                 :            :  * RX/TX ring descriptors display functions.
    4300                 :            :  */
    4301                 :            : int
    4302                 :          0 : rx_queue_id_is_invalid(queueid_t rxq_id)
    4303                 :            : {
    4304                 :          0 :         if (rxq_id < nb_rxq)
    4305                 :            :                 return 0;
    4306                 :          0 :         fprintf(stderr, "Invalid RX queue %d (must be < nb_rxq=%d)\n",
    4307                 :            :                 rxq_id, nb_rxq);
    4308                 :          0 :         return 1;
    4309                 :            : }
    4310                 :            : 
    4311                 :            : int
    4312                 :          0 : tx_queue_id_is_invalid(queueid_t txq_id)
    4313                 :            : {
    4314                 :          0 :         if (txq_id < nb_txq)
    4315                 :            :                 return 0;
    4316                 :          0 :         fprintf(stderr, "Invalid TX queue %d (must be < nb_txq=%d)\n",
    4317                 :            :                 txq_id, nb_txq);
    4318                 :          0 :         return 1;
    4319                 :            : }
    4320                 :            : 
    4321                 :            : static int
    4322                 :          0 : get_rx_ring_size(portid_t port_id, queueid_t rxq_id, uint16_t *ring_size)
    4323                 :            : {
    4324                 :          0 :         struct rte_port *port = &ports[port_id];
    4325                 :            :         struct rte_eth_rxq_info rx_qinfo;
    4326                 :            :         int ret;
    4327                 :            : 
    4328                 :          0 :         ret = rte_eth_rx_queue_info_get(port_id, rxq_id, &rx_qinfo);
    4329                 :          0 :         if (ret == 0) {
    4330                 :          0 :                 *ring_size = rx_qinfo.nb_desc;
    4331                 :          0 :                 return ret;
    4332                 :            :         }
    4333                 :            : 
    4334                 :          0 :         if (ret != -ENOTSUP)
    4335                 :            :                 return ret;
    4336                 :            :         /*
    4337                 :            :          * If the rte_eth_rx_queue_info_get is not support for this PMD,
    4338                 :            :          * ring_size stored in testpmd will be used for validity verification.
    4339                 :            :          * When configure the rxq by rte_eth_rx_queue_setup with nb_rx_desc
    4340                 :            :          * being 0, it will use a default value provided by PMDs to setup this
    4341                 :            :          * rxq. If the default value is 0, it will use the
    4342                 :            :          * RTE_ETH_DEV_FALLBACK_RX_RINGSIZE to setup this rxq.
    4343                 :            :          */
    4344                 :          0 :         if (port->nb_rx_desc[rxq_id])
    4345                 :          0 :                 *ring_size = port->nb_rx_desc[rxq_id];
    4346                 :          0 :         else if (port->dev_info.default_rxportconf.ring_size)
    4347                 :          0 :                 *ring_size = port->dev_info.default_rxportconf.ring_size;
    4348                 :            :         else
    4349                 :          0 :                 *ring_size = RTE_ETH_DEV_FALLBACK_RX_RINGSIZE;
    4350                 :            :         return 0;
    4351                 :            : }
    4352                 :            : 
    4353                 :            : static int
    4354                 :          0 : get_tx_ring_size(portid_t port_id, queueid_t txq_id, uint16_t *ring_size)
    4355                 :            : {
    4356                 :          0 :         struct rte_port *port = &ports[port_id];
    4357                 :            :         struct rte_eth_txq_info tx_qinfo;
    4358                 :            :         int ret;
    4359                 :            : 
    4360                 :          0 :         ret = rte_eth_tx_queue_info_get(port_id, txq_id, &tx_qinfo);
    4361                 :          0 :         if (ret == 0) {
    4362                 :          0 :                 *ring_size = tx_qinfo.nb_desc;
    4363                 :          0 :                 return ret;
    4364                 :            :         }
    4365                 :            : 
    4366                 :          0 :         if (ret != -ENOTSUP)
    4367                 :            :                 return ret;
    4368                 :            :         /*
    4369                 :            :          * If the rte_eth_tx_queue_info_get is not support for this PMD,
    4370                 :            :          * ring_size stored in testpmd will be used for validity verification.
    4371                 :            :          * When configure the txq by rte_eth_tx_queue_setup with nb_tx_desc
    4372                 :            :          * being 0, it will use a default value provided by PMDs to setup this
    4373                 :            :          * txq. If the default value is 0, it will use the
    4374                 :            :          * RTE_ETH_DEV_FALLBACK_TX_RINGSIZE to setup this txq.
    4375                 :            :          */
    4376                 :          0 :         if (port->nb_tx_desc[txq_id])
    4377                 :          0 :                 *ring_size = port->nb_tx_desc[txq_id];
    4378                 :          0 :         else if (port->dev_info.default_txportconf.ring_size)
    4379                 :          0 :                 *ring_size = port->dev_info.default_txportconf.ring_size;
    4380                 :            :         else
    4381                 :          0 :                 *ring_size = RTE_ETH_DEV_FALLBACK_TX_RINGSIZE;
    4382                 :            :         return 0;
    4383                 :            : }
    4384                 :            : 
    4385                 :            : static int
    4386                 :          0 : rx_desc_id_is_invalid(portid_t port_id, queueid_t rxq_id, uint16_t rxdesc_id)
    4387                 :            : {
    4388                 :            :         uint16_t ring_size;
    4389                 :            :         int ret;
    4390                 :            : 
    4391                 :          0 :         ret = get_rx_ring_size(port_id, rxq_id, &ring_size);
    4392                 :          0 :         if (ret)
    4393                 :            :                 return 1;
    4394                 :            : 
    4395                 :          0 :         if (rxdesc_id < ring_size)
    4396                 :            :                 return 0;
    4397                 :            : 
    4398                 :          0 :         fprintf(stderr, "Invalid RX descriptor %u (must be < ring_size=%u)\n",
    4399                 :            :                 rxdesc_id, ring_size);
    4400                 :          0 :         return 1;
    4401                 :            : }
    4402                 :            : 
    4403                 :            : static int
    4404                 :          0 : tx_desc_id_is_invalid(portid_t port_id, queueid_t txq_id, uint16_t txdesc_id)
    4405                 :            : {
    4406                 :            :         uint16_t ring_size;
    4407                 :            :         int ret;
    4408                 :            : 
    4409                 :          0 :         ret = get_tx_ring_size(port_id, txq_id, &ring_size);
    4410                 :          0 :         if (ret)
    4411                 :            :                 return 1;
    4412                 :            : 
    4413                 :          0 :         if (txdesc_id < ring_size)
    4414                 :            :                 return 0;
    4415                 :            : 
    4416                 :          0 :         fprintf(stderr, "Invalid TX descriptor %u (must be < ring_size=%u)\n",
    4417                 :            :                 txdesc_id, ring_size);
    4418                 :          0 :         return 1;
    4419                 :            : }
    4420                 :            : 
    4421                 :            : static const struct rte_memzone *
    4422                 :          0 : ring_dma_zone_lookup(const char *ring_name, portid_t port_id, uint16_t q_id)
    4423                 :            : {
    4424                 :            :         char mz_name[RTE_MEMZONE_NAMESIZE];
    4425                 :            :         const struct rte_memzone *mz;
    4426                 :            : 
    4427                 :          0 :         snprintf(mz_name, sizeof(mz_name), "eth_p%d_q%d_%s",
    4428                 :            :                         port_id, q_id, ring_name);
    4429                 :          0 :         mz = rte_memzone_lookup(mz_name);
    4430                 :          0 :         if (mz == NULL)
    4431                 :          0 :                 fprintf(stderr,
    4432                 :            :                         "%s ring memory zoneof (port %d, queue %d) not found (zone name = %s\n",
    4433                 :            :                         ring_name, port_id, q_id, mz_name);
    4434                 :          0 :         return mz;
    4435                 :            : }
    4436                 :            : 
    4437                 :            : union igb_ring_dword {
    4438                 :            :         uint64_t dword;
    4439                 :            :         struct {
    4440                 :            : #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
    4441                 :            :                 uint32_t lo;
    4442                 :            :                 uint32_t hi;
    4443                 :            : #else
    4444                 :            :                 uint32_t hi;
    4445                 :            :                 uint32_t lo;
    4446                 :            : #endif
    4447                 :            :         } words;
    4448                 :            : };
    4449                 :            : 
    4450                 :            : struct igb_ring_desc_32_bytes {
    4451                 :            :         union igb_ring_dword lo_dword;
    4452                 :            :         union igb_ring_dword hi_dword;
    4453                 :            :         union igb_ring_dword resv1;
    4454                 :            :         union igb_ring_dword resv2;
    4455                 :            : };
    4456                 :            : 
    4457                 :            : struct igb_ring_desc_16_bytes {
    4458                 :            :         union igb_ring_dword lo_dword;
    4459                 :            :         union igb_ring_dword hi_dword;
    4460                 :            : };
    4461                 :            : 
    4462                 :            : static void
    4463                 :            : ring_rxd_display_dword(union igb_ring_dword dword)
    4464                 :            : {
    4465                 :            :         printf("    0x%08X - 0x%08X\n", (unsigned)dword.words.lo,
    4466                 :            :                                         (unsigned)dword.words.hi);
    4467                 :            : }
    4468                 :            : 
    4469                 :            : static void
    4470                 :          0 : ring_rx_descriptor_display(const struct rte_memzone *ring_mz,
    4471                 :            : #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
    4472                 :            :                            portid_t port_id,
    4473                 :            : #else
    4474                 :            :                            __rte_unused portid_t port_id,
    4475                 :            : #endif
    4476                 :            :                            uint16_t desc_id)
    4477                 :            : {
    4478                 :          0 :         struct igb_ring_desc_16_bytes *ring =
    4479                 :            :                 (struct igb_ring_desc_16_bytes *)ring_mz->addr;
    4480                 :            : #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
    4481                 :            :         int ret;
    4482                 :            :         struct rte_eth_dev_info dev_info;
    4483                 :            : 
    4484                 :          0 :         ret = eth_dev_info_get_print_err(port_id, &dev_info);
    4485                 :          0 :         if (ret != 0)
    4486                 :          0 :                 return;
    4487                 :            : 
    4488                 :          0 :         if (strstr(dev_info.driver_name, "i40e") != NULL) {
    4489                 :            :                 /* 32 bytes RX descriptor, i40e only */
    4490                 :          0 :                 struct igb_ring_desc_32_bytes *ring =
    4491                 :            :                         (struct igb_ring_desc_32_bytes *)ring_mz->addr;
    4492                 :            :                 ring[desc_id].lo_dword.dword =
    4493                 :          0 :                         rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
    4494                 :            :                 ring_rxd_display_dword(ring[desc_id].lo_dword);
    4495                 :            :                 ring[desc_id].hi_dword.dword =
    4496                 :            :                         rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
    4497                 :            :                 ring_rxd_display_dword(ring[desc_id].hi_dword);
    4498                 :            :                 ring[desc_id].resv1.dword =
    4499                 :            :                         rte_le_to_cpu_64(ring[desc_id].resv1.dword);
    4500                 :            :                 ring_rxd_display_dword(ring[desc_id].resv1);
    4501                 :            :                 ring[desc_id].resv2.dword =
    4502                 :            :                         rte_le_to_cpu_64(ring[desc_id].resv2.dword);
    4503                 :            :                 ring_rxd_display_dword(ring[desc_id].resv2);
    4504                 :            : 
    4505                 :          0 :                 return;
    4506                 :            :         }
    4507                 :            : #endif
    4508                 :            :         /* 16 bytes RX descriptor */
    4509                 :            :         ring[desc_id].lo_dword.dword =
    4510                 :          0 :                 rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
    4511                 :            :         ring_rxd_display_dword(ring[desc_id].lo_dword);
    4512                 :            :         ring[desc_id].hi_dword.dword =
    4513                 :            :                 rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
    4514                 :            :         ring_rxd_display_dword(ring[desc_id].hi_dword);
    4515                 :            : }
    4516                 :            : 
    4517                 :            : static void
    4518                 :          0 : ring_tx_descriptor_display(const struct rte_memzone *ring_mz, uint16_t desc_id)
    4519                 :            : {
    4520                 :            :         struct igb_ring_desc_16_bytes *ring;
    4521                 :            :         struct igb_ring_desc_16_bytes txd;
    4522                 :            : 
    4523                 :          0 :         ring = (struct igb_ring_desc_16_bytes *)ring_mz->addr;
    4524                 :          0 :         txd.lo_dword.dword = rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
    4525                 :          0 :         txd.hi_dword.dword = rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
    4526                 :            :         printf("    0x%08X - 0x%08X / 0x%08X - 0x%08X\n",
    4527                 :            :                         (unsigned)txd.lo_dword.words.lo,
    4528                 :            :                         (unsigned)txd.lo_dword.words.hi,
    4529                 :            :                         (unsigned)txd.hi_dword.words.lo,
    4530                 :            :                         (unsigned)txd.hi_dword.words.hi);
    4531                 :          0 : }
    4532                 :            : 
    4533                 :            : void
    4534                 :          0 : rx_ring_desc_display(portid_t port_id, queueid_t rxq_id, uint16_t rxd_id)
    4535                 :            : {
    4536                 :            :         const struct rte_memzone *rx_mz;
    4537                 :            : 
    4538                 :          0 :         if (rx_desc_id_is_invalid(port_id, rxq_id, rxd_id))
    4539                 :            :                 return;
    4540                 :          0 :         rx_mz = ring_dma_zone_lookup("rx_ring", port_id, rxq_id);
    4541                 :          0 :         if (rx_mz == NULL)
    4542                 :            :                 return;
    4543                 :          0 :         ring_rx_descriptor_display(rx_mz, port_id, rxd_id);
    4544                 :            : }
    4545                 :            : 
    4546                 :            : void
    4547                 :          0 : tx_ring_desc_display(portid_t port_id, queueid_t txq_id, uint16_t txd_id)
    4548                 :            : {
    4549                 :            :         const struct rte_memzone *tx_mz;
    4550                 :            : 
    4551                 :          0 :         if (tx_desc_id_is_invalid(port_id, txq_id, txd_id))
    4552                 :            :                 return;
    4553                 :          0 :         tx_mz = ring_dma_zone_lookup("tx_ring", port_id, txq_id);
    4554                 :          0 :         if (tx_mz == NULL)
    4555                 :            :                 return;
    4556                 :          0 :         ring_tx_descriptor_display(tx_mz, txd_id);
    4557                 :            : }
    4558                 :            : 
    4559                 :            : void
    4560                 :          0 : fwd_lcores_config_display(void)
    4561                 :            : {
    4562                 :            :         lcoreid_t lc_id;
    4563                 :            : 
    4564                 :            :         printf("List of forwarding lcores:");
    4565                 :          0 :         for (lc_id = 0; lc_id < nb_cfg_lcores; lc_id++)
    4566                 :          0 :                 printf(" %2u", fwd_lcores_cpuids[lc_id]);
    4567                 :            :         printf("\n");
    4568                 :          0 : }
    4569                 :            : void
    4570                 :          0 : rxtx_config_display(void)
    4571                 :            : {
    4572                 :            :         portid_t pid;
    4573                 :            :         queueid_t qid;
    4574                 :            : 
    4575                 :          0 :         printf("  %s%s%s packet forwarding%s packets/burst=%d\n",
    4576                 :            :                cur_fwd_eng->fwd_mode_name,
    4577                 :            :                cur_fwd_eng->status ? "-" : "",
    4578                 :          0 :                cur_fwd_eng->status ? cur_fwd_eng->status : "",
    4579                 :          0 :                retry_enabled == 0 ? "" : " with retry",
    4580                 :            :                nb_pkt_per_burst);
    4581                 :            : 
    4582                 :          0 :         if (cur_fwd_eng == &tx_only_engine || cur_fwd_eng == &flow_gen_engine)
    4583                 :          0 :                 printf("  packet len=%u - nb packet segments=%d\n",
    4584                 :            :                                 (unsigned)tx_pkt_length, (int) tx_pkt_nb_segs);
    4585                 :            : 
    4586                 :          0 :         printf("  nb forwarding cores=%d - nb forwarding ports=%d\n",
    4587                 :            :                nb_fwd_lcores, nb_fwd_ports);
    4588                 :            : 
    4589                 :          0 :         RTE_ETH_FOREACH_DEV(pid) {
    4590                 :          0 :                 struct rte_eth_rxconf *rx_conf = &ports[pid].rxq[0].conf;
    4591                 :          0 :                 struct rte_eth_txconf *tx_conf = &ports[pid].txq[0].conf;
    4592                 :          0 :                 uint16_t *nb_rx_desc = &ports[pid].nb_rx_desc[0];
    4593                 :          0 :                 uint16_t *nb_tx_desc = &ports[pid].nb_tx_desc[0];
    4594                 :            :                 struct rte_eth_rxq_info rx_qinfo;
    4595                 :            :                 struct rte_eth_txq_info tx_qinfo;
    4596                 :            :                 uint16_t rx_free_thresh_tmp;
    4597                 :            :                 uint16_t tx_free_thresh_tmp;
    4598                 :            :                 uint16_t tx_rs_thresh_tmp;
    4599                 :            :                 uint16_t nb_rx_desc_tmp;
    4600                 :            :                 uint16_t nb_tx_desc_tmp;
    4601                 :            :                 uint64_t offloads_tmp;
    4602                 :            :                 uint8_t pthresh_tmp;
    4603                 :            :                 uint8_t hthresh_tmp;
    4604                 :            :                 uint8_t wthresh_tmp;
    4605                 :            :                 int32_t rc;
    4606                 :            : 
    4607                 :            :                 /* per port config */
    4608                 :          0 :                 printf("  port %d: RX queue number: %d Tx queue number: %d\n",
    4609                 :            :                                 (unsigned int)pid, nb_rxq, nb_txq);
    4610                 :            : 
    4611                 :          0 :                 printf("    Rx offloads=0x%"PRIx64" Tx offloads=0x%"PRIx64"\n",
    4612                 :            :                                 ports[pid].dev_conf.rxmode.offloads,
    4613                 :          0 :                                 ports[pid].dev_conf.txmode.offloads);
    4614                 :            : 
    4615                 :            :                 /* per rx queue config only for first queue to be less verbose */
    4616                 :          0 :                 for (qid = 0; qid < 1; qid++) {
    4617                 :          0 :                         rc = rte_eth_rx_queue_info_get(pid, qid, &rx_qinfo);
    4618                 :          0 :                         if (rc) {
    4619                 :          0 :                                 nb_rx_desc_tmp = nb_rx_desc[qid];
    4620                 :          0 :                                 rx_free_thresh_tmp =
    4621                 :          0 :                                         rx_conf[qid].rx_free_thresh;
    4622                 :          0 :                                 pthresh_tmp = rx_conf[qid].rx_thresh.pthresh;
    4623                 :          0 :                                 hthresh_tmp = rx_conf[qid].rx_thresh.hthresh;
    4624                 :          0 :                                 wthresh_tmp = rx_conf[qid].rx_thresh.wthresh;
    4625                 :          0 :                                 offloads_tmp = rx_conf[qid].offloads;
    4626                 :            :                         } else {
    4627                 :          0 :                                 nb_rx_desc_tmp = rx_qinfo.nb_desc;
    4628                 :          0 :                                 rx_free_thresh_tmp =
    4629                 :            :                                                 rx_qinfo.conf.rx_free_thresh;
    4630                 :          0 :                                 pthresh_tmp = rx_qinfo.conf.rx_thresh.pthresh;
    4631                 :          0 :                                 hthresh_tmp = rx_qinfo.conf.rx_thresh.hthresh;
    4632                 :          0 :                                 wthresh_tmp = rx_qinfo.conf.rx_thresh.wthresh;
    4633                 :          0 :                                 offloads_tmp = rx_qinfo.conf.offloads;
    4634                 :            :                         }
    4635                 :            : 
    4636                 :            :                         printf("    RX queue: %d\n", qid);
    4637                 :          0 :                         printf("      RX desc=%d - RX free threshold=%d\n",
    4638                 :            :                                 nb_rx_desc_tmp, rx_free_thresh_tmp);
    4639                 :          0 :                         printf("      RX threshold registers: pthresh=%d hthresh=%d "
    4640                 :            :                                 " wthresh=%d\n",
    4641                 :            :                                 pthresh_tmp, hthresh_tmp, wthresh_tmp);
    4642                 :            :                         printf("      RX Offloads=0x%"PRIx64, offloads_tmp);
    4643                 :          0 :                         if (rx_conf->share_group > 0)
    4644                 :          0 :                                 printf(" share_group=%u share_qid=%u",
    4645                 :            :                                        rx_conf->share_group,
    4646                 :          0 :                                        rx_conf->share_qid);
    4647                 :            :                         printf("\n");
    4648                 :            :                 }
    4649                 :            : 
    4650                 :            :                 /* per tx queue config only for first queue to be less verbose */
    4651                 :          0 :                 for (qid = 0; qid < 1; qid++) {
    4652                 :          0 :                         rc = rte_eth_tx_queue_info_get(pid, qid, &tx_qinfo);
    4653                 :          0 :                         if (rc) {
    4654                 :          0 :                                 nb_tx_desc_tmp = nb_tx_desc[qid];
    4655                 :          0 :                                 tx_free_thresh_tmp =
    4656                 :          0 :                                         tx_conf[qid].tx_free_thresh;
    4657                 :          0 :                                 pthresh_tmp = tx_conf[qid].tx_thresh.pthresh;
    4658                 :          0 :                                 hthresh_tmp = tx_conf[qid].tx_thresh.hthresh;
    4659                 :          0 :                                 wthresh_tmp = tx_conf[qid].tx_thresh.wthresh;
    4660                 :          0 :                                 offloads_tmp = tx_conf[qid].offloads;
    4661                 :          0 :                                 tx_rs_thresh_tmp = tx_conf[qid].tx_rs_thresh;
    4662                 :            :                         } else {
    4663                 :          0 :                                 nb_tx_desc_tmp = tx_qinfo.nb_desc;
    4664                 :          0 :                                 tx_free_thresh_tmp =
    4665                 :            :                                                 tx_qinfo.conf.tx_free_thresh;
    4666                 :          0 :                                 pthresh_tmp = tx_qinfo.conf.tx_thresh.pthresh;
    4667                 :          0 :                                 hthresh_tmp = tx_qinfo.conf.tx_thresh.hthresh;
    4668                 :          0 :                                 wthresh_tmp = tx_qinfo.conf.tx_thresh.wthresh;
    4669                 :          0 :                                 offloads_tmp = tx_qinfo.conf.offloads;
    4670                 :          0 :                                 tx_rs_thresh_tmp = tx_qinfo.conf.tx_rs_thresh;
    4671                 :            :                         }
    4672                 :            : 
    4673                 :            :                         printf("    TX queue: %d\n", qid);
    4674                 :          0 :                         printf("      TX desc=%d - TX free threshold=%d\n",
    4675                 :            :                                 nb_tx_desc_tmp, tx_free_thresh_tmp);
    4676                 :          0 :                         printf("      TX threshold registers: pthresh=%d hthresh=%d "
    4677                 :            :                                 " wthresh=%d\n",
    4678                 :            :                                 pthresh_tmp, hthresh_tmp, wthresh_tmp);
    4679                 :          0 :                         printf("      TX offloads=0x%"PRIx64" - TX RS bit threshold=%d\n",
    4680                 :            :                                 offloads_tmp, tx_rs_thresh_tmp);
    4681                 :            :                 }
    4682                 :            :         }
    4683                 :          0 : }
    4684                 :            : 
    4685                 :            : void
    4686                 :          0 : port_rss_reta_info(portid_t port_id,
    4687                 :            :                    struct rte_eth_rss_reta_entry64 *reta_conf,
    4688                 :            :                    uint16_t nb_entries)
    4689                 :            : {
    4690                 :            :         uint16_t i, idx, shift;
    4691                 :            :         int ret;
    4692                 :            : 
    4693                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    4694                 :            :                 return;
    4695                 :            : 
    4696                 :          0 :         ret = rte_eth_dev_rss_reta_query(port_id, reta_conf, nb_entries);
    4697                 :          0 :         if (ret != 0) {
    4698                 :          0 :                 fprintf(stderr,
    4699                 :            :                         "Failed to get RSS RETA info, return code = %d\n",
    4700                 :            :                         ret);
    4701                 :          0 :                 return;
    4702                 :            :         }
    4703                 :            : 
    4704                 :          0 :         for (i = 0; i < nb_entries; i++) {
    4705                 :          0 :                 idx = i / RTE_ETH_RETA_GROUP_SIZE;
    4706                 :          0 :                 shift = i % RTE_ETH_RETA_GROUP_SIZE;
    4707                 :          0 :                 if (!(reta_conf[idx].mask & (1ULL << shift)))
    4708                 :          0 :                         continue;
    4709                 :          0 :                 printf("RSS RETA configuration: hash index=%u, queue=%u\n",
    4710                 :          0 :                                         i, reta_conf[idx].reta[shift]);
    4711                 :            :         }
    4712                 :            : }
    4713                 :            : 
    4714                 :            : /*
    4715                 :            :  * Displays the RSS hash functions of a port, and, optionally, the RSS hash
    4716                 :            :  * key of the port.
    4717                 :            :  */
    4718                 :            : void
    4719                 :          0 : port_rss_hash_conf_show(portid_t port_id, int show_rss_key, int show_rss_algo)
    4720                 :            : {
    4721                 :          0 :         struct rte_eth_rss_conf rss_conf = {0};
    4722                 :            :         uint8_t rss_key[RSS_HASH_KEY_LENGTH];
    4723                 :            :         uint64_t rss_hf;
    4724                 :            :         uint8_t i;
    4725                 :            :         int diag;
    4726                 :            :         struct rte_eth_dev_info dev_info;
    4727                 :            :         uint8_t hash_key_size;
    4728                 :            :         int ret;
    4729                 :            : 
    4730                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    4731                 :          0 :                 return;
    4732                 :            : 
    4733                 :          0 :         ret = eth_dev_info_get_print_err(port_id, &dev_info);
    4734                 :          0 :         if (ret != 0)
    4735                 :            :                 return;
    4736                 :            : 
    4737                 :          0 :         if (dev_info.hash_key_size > 0 &&
    4738                 :            :                         dev_info.hash_key_size <= sizeof(rss_key))
    4739                 :            :                 hash_key_size = dev_info.hash_key_size;
    4740                 :            :         else {
    4741                 :          0 :                 fprintf(stderr,
    4742                 :            :                         "dev_info did not provide a valid hash key size\n");
    4743                 :          0 :                 return;
    4744                 :            :         }
    4745                 :            : 
    4746                 :            :         /* Get RSS hash key if asked to display it */
    4747                 :          0 :         rss_conf.rss_key = (show_rss_key) ? rss_key : NULL;
    4748                 :          0 :         rss_conf.rss_key_len = hash_key_size;
    4749                 :          0 :         diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf);
    4750                 :          0 :         if (diag != 0) {
    4751                 :          0 :                 switch (diag) {
    4752                 :          0 :                 case -ENODEV:
    4753                 :          0 :                         fprintf(stderr, "port index %d invalid\n", port_id);
    4754                 :            :                         break;
    4755                 :          0 :                 case -ENOTSUP:
    4756                 :          0 :                         fprintf(stderr, "operation not supported by device\n");
    4757                 :            :                         break;
    4758                 :          0 :                 default:
    4759                 :          0 :                         fprintf(stderr, "operation failed - diag=%d\n", diag);
    4760                 :            :                         break;
    4761                 :            :                 }
    4762                 :          0 :                 return;
    4763                 :            :         }
    4764                 :          0 :         rss_hf = rss_conf.rss_hf;
    4765                 :          0 :         if (rss_hf == 0) {
    4766                 :            :                 printf("RSS disabled\n");
    4767                 :          0 :                 return;
    4768                 :            :         }
    4769                 :            : 
    4770                 :          0 :         if (show_rss_algo) {
    4771                 :          0 :                 printf("RSS algorithm:\n  %s\n",
    4772                 :            :                         rte_eth_dev_rss_algo_name(rss_conf.algorithm));
    4773                 :          0 :                 return;
    4774                 :            :         }
    4775                 :            : 
    4776                 :            :         printf("RSS functions:\n");
    4777                 :          0 :         rss_types_display(rss_hf, TESTPMD_RSS_TYPES_CHAR_NUM_PER_LINE);
    4778                 :            : 
    4779                 :          0 :         if (!show_rss_key)
    4780                 :            :                 return;
    4781                 :            :         printf("RSS key:\n");
    4782                 :          0 :         for (i = 0; i < hash_key_size; i++)
    4783                 :          0 :                 printf("%02X", rss_key[i]);
    4784                 :            :         printf("\n");
    4785                 :            : }
    4786                 :            : 
    4787                 :            : void
    4788                 :          0 : port_rss_hash_key_update(portid_t port_id, char rss_type[], uint8_t *hash_key,
    4789                 :            :                          uint8_t hash_key_len)
    4790                 :            : {
    4791                 :            :         struct rte_eth_rss_conf rss_conf;
    4792                 :            :         int diag;
    4793                 :            : 
    4794                 :          0 :         rss_conf.rss_key = NULL;
    4795                 :          0 :         rss_conf.rss_key_len = 0;
    4796                 :          0 :         rss_conf.rss_hf = str_to_rsstypes(rss_type);
    4797                 :          0 :         diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf);
    4798                 :          0 :         if (diag == 0) {
    4799                 :          0 :                 rss_conf.rss_key = hash_key;
    4800                 :          0 :                 rss_conf.rss_key_len = hash_key_len;
    4801                 :          0 :                 diag = rte_eth_dev_rss_hash_update(port_id, &rss_conf);
    4802                 :            :         }
    4803                 :          0 :         if (diag == 0)
    4804                 :          0 :                 return;
    4805                 :            : 
    4806                 :          0 :         switch (diag) {
    4807                 :          0 :         case -ENODEV:
    4808                 :          0 :                 fprintf(stderr, "port index %d invalid\n", port_id);
    4809                 :            :                 break;
    4810                 :          0 :         case -ENOTSUP:
    4811                 :          0 :                 fprintf(stderr, "operation not supported by device\n");
    4812                 :            :                 break;
    4813                 :          0 :         default:
    4814                 :          0 :                 fprintf(stderr, "operation failed - diag=%d\n", diag);
    4815                 :            :                 break;
    4816                 :            :         }
    4817                 :            : }
    4818                 :            : 
    4819                 :            : /*
    4820                 :            :  * Check whether a shared rxq scheduled on other lcores.
    4821                 :            :  */
    4822                 :            : static bool
    4823                 :          0 : fwd_stream_on_other_lcores(uint16_t domain_id, lcoreid_t src_lc,
    4824                 :            :                            portid_t src_port, queueid_t src_rxq,
    4825                 :            :                            uint32_t share_group, queueid_t share_rxq)
    4826                 :            : {
    4827                 :            :         streamid_t sm_id;
    4828                 :            :         streamid_t nb_fs_per_lcore;
    4829                 :            :         lcoreid_t  nb_fc;
    4830                 :            :         lcoreid_t  lc_id;
    4831                 :            :         struct fwd_stream *fs;
    4832                 :            :         struct rte_port *port;
    4833                 :            :         struct rte_eth_dev_info *dev_info;
    4834                 :            :         struct rte_eth_rxconf *rxq_conf;
    4835                 :            : 
    4836                 :          0 :         nb_fc = cur_fwd_config.nb_fwd_lcores;
    4837                 :            :         /* Check remaining cores. */
    4838                 :          0 :         for (lc_id = src_lc + 1; lc_id < nb_fc; lc_id++) {
    4839                 :          0 :                 sm_id = fwd_lcores[lc_id]->stream_idx;
    4840                 :          0 :                 nb_fs_per_lcore = fwd_lcores[lc_id]->stream_nb;
    4841                 :          0 :                 for (; sm_id < fwd_lcores[lc_id]->stream_idx + nb_fs_per_lcore;
    4842                 :          0 :                      sm_id++) {
    4843                 :          0 :                         fs = fwd_streams[sm_id];
    4844                 :          0 :                         port = &ports[fs->rx_port];
    4845                 :            :                         dev_info = &port->dev_info;
    4846                 :          0 :                         rxq_conf = &port->rxq[fs->rx_queue].conf;
    4847                 :          0 :                         if ((dev_info->dev_capa & RTE_ETH_DEV_CAPA_RXQ_SHARE)
    4848                 :          0 :                             == 0 || rxq_conf->share_group == 0)
    4849                 :            :                                 /* Not shared rxq. */
    4850                 :          0 :                                 continue;
    4851                 :          0 :                         if (domain_id != port->dev_info.switch_info.domain_id)
    4852                 :          0 :                                 continue;
    4853                 :          0 :                         if (rxq_conf->share_group != share_group)
    4854                 :          0 :                                 continue;
    4855                 :          0 :                         if (rxq_conf->share_qid != share_rxq)
    4856                 :          0 :                                 continue;
    4857                 :          0 :                         printf("Shared Rx queue group %u queue %hu can't be scheduled on different cores:\n",
    4858                 :            :                                share_group, share_rxq);
    4859                 :          0 :                         printf("  lcore %u Port %hu queue %hu\n",
    4860                 :            :                                src_lc, src_port, src_rxq);
    4861                 :          0 :                         printf("  lcore %u Port %hu queue %hu\n",
    4862                 :          0 :                                lc_id, fs->rx_port, fs->rx_queue);
    4863                 :          0 :                         printf("Please use --nb-cores=%hu to limit number of forwarding cores\n",
    4864                 :            :                                nb_rxq);
    4865                 :          0 :                         return true;
    4866                 :            :                 }
    4867                 :            :         }
    4868                 :            :         return false;
    4869                 :            : }
    4870                 :            : 
    4871                 :            : /*
    4872                 :            :  * Check shared rxq configuration.
    4873                 :            :  *
    4874                 :            :  * Shared group must not being scheduled on different core.
    4875                 :            :  */
    4876                 :            : bool
    4877                 :          0 : pkt_fwd_shared_rxq_check(void)
    4878                 :            : {
    4879                 :            :         streamid_t sm_id;
    4880                 :            :         streamid_t nb_fs_per_lcore;
    4881                 :            :         lcoreid_t  nb_fc;
    4882                 :            :         lcoreid_t  lc_id;
    4883                 :            :         struct fwd_stream *fs;
    4884                 :            :         uint16_t domain_id;
    4885                 :            :         struct rte_port *port;
    4886                 :            :         struct rte_eth_dev_info *dev_info;
    4887                 :            :         struct rte_eth_rxconf *rxq_conf;
    4888                 :            : 
    4889                 :          0 :         if (rxq_share == 0)
    4890                 :            :                 return true;
    4891                 :          0 :         nb_fc = cur_fwd_config.nb_fwd_lcores;
    4892                 :            :         /*
    4893                 :            :          * Check streams on each core, make sure the same switch domain +
    4894                 :            :          * group + queue doesn't get scheduled on other cores.
    4895                 :            :          */
    4896                 :          0 :         for (lc_id = 0; lc_id < nb_fc; lc_id++) {
    4897                 :          0 :                 sm_id = fwd_lcores[lc_id]->stream_idx;
    4898                 :          0 :                 nb_fs_per_lcore = fwd_lcores[lc_id]->stream_nb;
    4899                 :          0 :                 for (; sm_id < fwd_lcores[lc_id]->stream_idx + nb_fs_per_lcore;
    4900                 :          0 :                      sm_id++) {
    4901                 :          0 :                         fs = fwd_streams[sm_id];
    4902                 :            :                         /* Update lcore info stream being scheduled. */
    4903                 :          0 :                         fs->lcore = fwd_lcores[lc_id];
    4904                 :          0 :                         port = &ports[fs->rx_port];
    4905                 :            :                         dev_info = &port->dev_info;
    4906                 :          0 :                         rxq_conf = &port->rxq[fs->rx_queue].conf;
    4907                 :          0 :                         if ((dev_info->dev_capa & RTE_ETH_DEV_CAPA_RXQ_SHARE)
    4908                 :          0 :                             == 0 || rxq_conf->share_group == 0)
    4909                 :            :                                 /* Not shared rxq. */
    4910                 :          0 :                                 continue;
    4911                 :            :                         /* Check shared rxq not scheduled on remaining cores. */
    4912                 :          0 :                         domain_id = port->dev_info.switch_info.domain_id;
    4913                 :          0 :                         if (fwd_stream_on_other_lcores(domain_id, lc_id,
    4914                 :            :                                                        fs->rx_port,
    4915                 :            :                                                        fs->rx_queue,
    4916                 :            :                                                        rxq_conf->share_group,
    4917                 :          0 :                                                        rxq_conf->share_qid))
    4918                 :            :                                 return false;
    4919                 :            :                 }
    4920                 :            :         }
    4921                 :            :         return true;
    4922                 :            : }
    4923                 :            : 
    4924                 :            : /*
    4925                 :            :  * Setup forwarding configuration for each logical core.
    4926                 :            :  */
    4927                 :            : static void
    4928                 :          0 : setup_fwd_config_of_each_lcore(struct fwd_config *cfg)
    4929                 :            : {
    4930                 :            :         streamid_t nb_fs_per_lcore;
    4931                 :            :         streamid_t nb_fs;
    4932                 :            :         streamid_t sm_id;
    4933                 :            :         lcoreid_t  nb_extra;
    4934                 :            :         lcoreid_t  nb_fc;
    4935                 :            :         lcoreid_t  nb_lc;
    4936                 :            :         lcoreid_t  lc_id;
    4937                 :            : 
    4938                 :          0 :         nb_fs = cfg->nb_fwd_streams;
    4939                 :          0 :         nb_fc = cfg->nb_fwd_lcores;
    4940                 :          0 :         if (nb_fs <= nb_fc) {
    4941                 :            :                 nb_fs_per_lcore = 1;
    4942                 :            :                 nb_extra = 0;
    4943                 :            :         } else {
    4944                 :          0 :                 nb_fs_per_lcore = (streamid_t) (nb_fs / nb_fc);
    4945                 :          0 :                 nb_extra = (lcoreid_t) (nb_fs % nb_fc);
    4946                 :            :         }
    4947                 :            : 
    4948                 :          0 :         nb_lc = (lcoreid_t) (nb_fc - nb_extra);
    4949                 :            :         sm_id = 0;
    4950                 :          0 :         for (lc_id = 0; lc_id < nb_lc; lc_id++) {
    4951                 :          0 :                 fwd_lcores[lc_id]->stream_idx = sm_id;
    4952                 :          0 :                 fwd_lcores[lc_id]->stream_nb = nb_fs_per_lcore;
    4953                 :          0 :                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
    4954                 :            :         }
    4955                 :            : 
    4956                 :            :         /*
    4957                 :            :          * Assign extra remaining streams, if any.
    4958                 :            :          */
    4959                 :          0 :         nb_fs_per_lcore = (streamid_t) (nb_fs_per_lcore + 1);
    4960                 :          0 :         for (lc_id = 0; lc_id < nb_extra; lc_id++) {
    4961                 :          0 :                 fwd_lcores[nb_lc + lc_id]->stream_idx = sm_id;
    4962                 :          0 :                 fwd_lcores[nb_lc + lc_id]->stream_nb = nb_fs_per_lcore;
    4963                 :          0 :                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
    4964                 :            :         }
    4965                 :          0 : }
    4966                 :            : 
    4967                 :            : static portid_t
    4968                 :          0 : fwd_topology_tx_port_get(portid_t rxp)
    4969                 :            : {
    4970                 :            :         static int warning_once = 1;
    4971                 :            : 
    4972                 :            :         RTE_ASSERT(rxp < cur_fwd_config.nb_fwd_ports);
    4973                 :            : 
    4974                 :          0 :         switch (port_topology) {
    4975                 :          0 :         default:
    4976                 :            :         case PORT_TOPOLOGY_PAIRED:
    4977                 :          0 :                 if ((rxp & 0x1) == 0) {
    4978                 :          0 :                         if (rxp + 1 < cur_fwd_config.nb_fwd_ports)
    4979                 :          0 :                                 return rxp + 1;
    4980                 :          0 :                         if (warning_once) {
    4981                 :          0 :                                 fprintf(stderr,
    4982                 :            :                                         "\nWarning! port-topology=paired and odd forward ports number, the last port will pair with itself.\n\n");
    4983                 :          0 :                                 warning_once = 0;
    4984                 :            :                         }
    4985                 :          0 :                         return rxp;
    4986                 :            :                 }
    4987                 :          0 :                 return rxp - 1;
    4988                 :          0 :         case PORT_TOPOLOGY_CHAINED:
    4989                 :          0 :                 return (rxp + 1) % cur_fwd_config.nb_fwd_ports;
    4990                 :            :         case PORT_TOPOLOGY_LOOP:
    4991                 :            :                 return rxp;
    4992                 :            :         }
    4993                 :            : }
    4994                 :            : 
    4995                 :            : static void
    4996                 :          0 : simple_fwd_config_setup(void)
    4997                 :            : {
    4998                 :            :         portid_t i;
    4999                 :            : 
    5000                 :          0 :         cur_fwd_config.nb_fwd_ports = (portid_t) nb_fwd_ports;
    5001                 :          0 :         cur_fwd_config.nb_fwd_streams =
    5002                 :            :                 (streamid_t) cur_fwd_config.nb_fwd_ports;
    5003                 :            : 
    5004                 :            :         /* reinitialize forwarding streams */
    5005                 :          0 :         init_fwd_streams();
    5006                 :            : 
    5007                 :            :         /*
    5008                 :            :          * In the simple forwarding test, the number of forwarding cores
    5009                 :            :          * must be lower or equal to the number of forwarding ports.
    5010                 :            :          */
    5011                 :          0 :         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
    5012                 :          0 :         if (cur_fwd_config.nb_fwd_lcores > cur_fwd_config.nb_fwd_ports)
    5013                 :          0 :                 cur_fwd_config.nb_fwd_lcores =
    5014                 :            :                         (lcoreid_t) cur_fwd_config.nb_fwd_ports;
    5015                 :          0 :         setup_fwd_config_of_each_lcore(&cur_fwd_config);
    5016                 :            : 
    5017                 :          0 :         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) {
    5018                 :          0 :                 fwd_streams[i]->rx_port   = fwd_ports_ids[i];
    5019                 :          0 :                 fwd_streams[i]->rx_queue  = 0;
    5020                 :          0 :                 fwd_streams[i]->tx_port   =
    5021                 :          0 :                                 fwd_ports_ids[fwd_topology_tx_port_get(i)];
    5022                 :          0 :                 fwd_streams[i]->tx_queue  = 0;
    5023                 :          0 :                 fwd_streams[i]->peer_addr = fwd_streams[i]->tx_port;
    5024                 :          0 :                 fwd_streams[i]->retry_enabled = retry_enabled;
    5025                 :            :         }
    5026                 :          0 : }
    5027                 :            : 
    5028                 :            : /**
    5029                 :            :  * For the RSS forwarding test all streams distributed over lcores. Each stream
    5030                 :            :  * being composed of a RX queue to poll on a RX port for input messages,
    5031                 :            :  * associated with a TX queue of a TX port where to send forwarded packets.
    5032                 :            :  */
    5033                 :            : static void
    5034                 :          0 : rss_fwd_config_setup(void)
    5035                 :            : {
    5036                 :            :         portid_t   rxp;
    5037                 :            :         portid_t   txp;
    5038                 :            :         queueid_t  rxq;
    5039                 :            :         queueid_t  nb_q;
    5040                 :            :         streamid_t  sm_id;
    5041                 :            :         int start;
    5042                 :            : 
    5043                 :          0 :         nb_q = nb_rxq;
    5044                 :          0 :         if (nb_q > nb_txq)
    5045                 :            :                 nb_q = nb_txq;
    5046                 :          0 :         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
    5047                 :          0 :         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
    5048                 :          0 :         cur_fwd_config.nb_fwd_streams =
    5049                 :          0 :                 (streamid_t) (nb_q / num_procs * cur_fwd_config.nb_fwd_ports);
    5050                 :            : 
    5051                 :          0 :         if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores)
    5052                 :          0 :                 cur_fwd_config.nb_fwd_lcores =
    5053                 :            :                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
    5054                 :            : 
    5055                 :            :         /* reinitialize forwarding streams */
    5056                 :          0 :         init_fwd_streams();
    5057                 :            : 
    5058                 :          0 :         setup_fwd_config_of_each_lcore(&cur_fwd_config);
    5059                 :            : 
    5060                 :          0 :         if (proc_id > 0 && nb_q % num_procs != 0)
    5061                 :            :                 printf("Warning! queue numbers should be multiple of processes, or packet loss will happen.\n");
    5062                 :            : 
    5063                 :            :         /**
    5064                 :            :          * In multi-process, All queues are allocated to different
    5065                 :            :          * processes based on num_procs and proc_id. For example:
    5066                 :            :          * if supports 4 queues(nb_q), 2 processes(num_procs),
    5067                 :            :          * the 0~1 queue for primary process.
    5068                 :            :          * the 2~3 queue for secondary process.
    5069                 :            :          */
    5070                 :          0 :         start = proc_id * nb_q / num_procs;
    5071                 :            :         rxp = 0;
    5072                 :          0 :         rxq = start;
    5073                 :          0 :         for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
    5074                 :            :                 struct fwd_stream *fs;
    5075                 :            : 
    5076                 :          0 :                 fs = fwd_streams[sm_id];
    5077                 :          0 :                 txp = fwd_topology_tx_port_get(rxp);
    5078                 :          0 :                 fs->rx_port = fwd_ports_ids[rxp];
    5079                 :          0 :                 fs->rx_queue = rxq;
    5080                 :          0 :                 fs->tx_port = fwd_ports_ids[txp];
    5081                 :          0 :                 fs->tx_queue = rxq;
    5082                 :          0 :                 fs->peer_addr = fs->tx_port;
    5083                 :          0 :                 fs->retry_enabled = retry_enabled;
    5084                 :          0 :                 rxp++;
    5085                 :          0 :                 if (rxp < nb_fwd_ports)
    5086                 :          0 :                         continue;
    5087                 :            :                 rxp = 0;
    5088                 :          0 :                 rxq++;
    5089                 :            :         }
    5090                 :          0 : }
    5091                 :            : 
    5092                 :            : static uint16_t
    5093                 :          0 : get_fwd_port_total_tc_num(void)
    5094                 :            : {
    5095                 :            :         struct rte_eth_dcb_info dcb_info;
    5096                 :            :         uint16_t total_tc_num = 0;
    5097                 :            :         unsigned int i;
    5098                 :            : 
    5099                 :          0 :         for (i = 0; i < nb_fwd_ports; i++) {
    5100                 :          0 :                 (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[i], &dcb_info);
    5101                 :          0 :                 total_tc_num += dcb_info.nb_tcs;
    5102                 :            :         }
    5103                 :            : 
    5104                 :          0 :         return total_tc_num;
    5105                 :            : }
    5106                 :            : 
    5107                 :            : /**
    5108                 :            :  * For the DCB forwarding test, each core is assigned on each traffic class.
    5109                 :            :  *
    5110                 :            :  * Each core is assigned a multi-stream, each stream being composed of
    5111                 :            :  * a RX queue to poll on a RX port for input messages, associated with
    5112                 :            :  * a TX queue of a TX port where to send forwarded packets. All RX and
    5113                 :            :  * TX queues are mapping to the same traffic class.
    5114                 :            :  * If VMDQ and DCB co-exist, each traffic class on different POOLs share
    5115                 :            :  * the same core
    5116                 :            :  */
    5117                 :            : static void
    5118                 :          0 : dcb_fwd_config_setup(void)
    5119                 :            : {
    5120                 :            :         struct rte_eth_dcb_info rxp_dcb_info, txp_dcb_info;
    5121                 :            :         portid_t txp, rxp = 0;
    5122                 :            :         queueid_t txq, rxq = 0;
    5123                 :            :         lcoreid_t  lc_id;
    5124                 :            :         uint16_t nb_rx_queue, nb_tx_queue;
    5125                 :            :         uint16_t i, j, k, sm_id = 0;
    5126                 :            :         uint16_t total_tc_num;
    5127                 :            :         struct rte_port *port;
    5128                 :            :         uint8_t tc = 0;
    5129                 :            :         portid_t pid;
    5130                 :            :         int ret;
    5131                 :            : 
    5132                 :            :         /*
    5133                 :            :          * The fwd_config_setup() is called when the port is RTE_PORT_STARTED
    5134                 :            :          * or RTE_PORT_STOPPED.
    5135                 :            :          *
    5136                 :            :          * Re-configure ports to get updated mapping between tc and queue in
    5137                 :            :          * case the queue number of the port is changed. Skip for started ports
    5138                 :            :          * since modifying queue number and calling dev_configure need to stop
    5139                 :            :          * ports first.
    5140                 :            :          */
    5141                 :          0 :         for (pid = 0; pid < nb_fwd_ports; pid++) {
    5142                 :          0 :                 if (port_is_started(pid) == 1)
    5143                 :          0 :                         continue;
    5144                 :            : 
    5145                 :          0 :                 port = &ports[pid];
    5146                 :          0 :                 ret = rte_eth_dev_configure(pid, nb_rxq, nb_txq,
    5147                 :          0 :                                             &port->dev_conf);
    5148                 :          0 :                 if (ret < 0) {
    5149                 :          0 :                         fprintf(stderr,
    5150                 :            :                                 "Failed to re-configure port %d, ret = %d.\n",
    5151                 :            :                                 pid, ret);
    5152                 :          0 :                         return;
    5153                 :            :                 }
    5154                 :            :         }
    5155                 :            : 
    5156                 :          0 :         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
    5157                 :          0 :         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
    5158                 :          0 :         cur_fwd_config.nb_fwd_streams =
    5159                 :          0 :                 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
    5160                 :          0 :         total_tc_num = get_fwd_port_total_tc_num();
    5161                 :          0 :         if (cur_fwd_config.nb_fwd_lcores > total_tc_num)
    5162                 :          0 :                 cur_fwd_config.nb_fwd_lcores = total_tc_num;
    5163                 :            : 
    5164                 :            :         /* reinitialize forwarding streams */
    5165                 :          0 :         init_fwd_streams();
    5166                 :            :         sm_id = 0;
    5167                 :            :         txp = 1;
    5168                 :            :         /* get the dcb info on the first RX and TX ports */
    5169                 :          0 :         (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[rxp], &rxp_dcb_info);
    5170                 :          0 :         (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[txp], &txp_dcb_info);
    5171                 :            : 
    5172                 :          0 :         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
    5173                 :          0 :                 fwd_lcores[lc_id]->stream_nb = 0;
    5174                 :          0 :                 fwd_lcores[lc_id]->stream_idx = sm_id;
    5175                 :          0 :                 for (i = 0; i < RTE_ETH_MAX_VMDQ_POOL; i++) {
    5176                 :            :                         /* if the nb_queue is zero, means this tc is
    5177                 :            :                          * not enabled on the POOL
    5178                 :            :                          */
    5179                 :          0 :                         if (rxp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue == 0)
    5180                 :            :                                 break;
    5181                 :          0 :                         k = fwd_lcores[lc_id]->stream_nb +
    5182                 :            :                                 fwd_lcores[lc_id]->stream_idx;
    5183                 :          0 :                         rxq = rxp_dcb_info.tc_queue.tc_rxq[i][tc].base;
    5184                 :          0 :                         txq = txp_dcb_info.tc_queue.tc_txq[i][tc].base;
    5185                 :          0 :                         nb_rx_queue = txp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue;
    5186                 :          0 :                         nb_tx_queue = txp_dcb_info.tc_queue.tc_txq[i][tc].nb_queue;
    5187                 :          0 :                         for (j = 0; j < nb_rx_queue; j++) {
    5188                 :            :                                 struct fwd_stream *fs;
    5189                 :            : 
    5190                 :          0 :                                 fs = fwd_streams[k + j];
    5191                 :          0 :                                 fs->rx_port = fwd_ports_ids[rxp];
    5192                 :          0 :                                 fs->rx_queue = rxq + j;
    5193                 :          0 :                                 fs->tx_port = fwd_ports_ids[txp];
    5194                 :          0 :                                 fs->tx_queue = txq + j % nb_tx_queue;
    5195                 :          0 :                                 fs->peer_addr = fs->tx_port;
    5196                 :          0 :                                 fs->retry_enabled = retry_enabled;
    5197                 :            :                         }
    5198                 :          0 :                         fwd_lcores[lc_id]->stream_nb +=
    5199                 :            :                                 rxp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue;
    5200                 :            :                 }
    5201                 :          0 :                 sm_id = (streamid_t) (sm_id + fwd_lcores[lc_id]->stream_nb);
    5202                 :            : 
    5203                 :          0 :                 tc++;
    5204                 :          0 :                 if (tc < rxp_dcb_info.nb_tcs)
    5205                 :          0 :                         continue;
    5206                 :            :                 /* Restart from TC 0 on next RX port */
    5207                 :            :                 tc = 0;
    5208                 :          0 :                 if (numa_support && (nb_fwd_ports <= (nb_ports >> 1)))
    5209                 :          0 :                         rxp = (portid_t)
    5210                 :          0 :                                 (rxp + ((nb_ports >> 1) / nb_fwd_ports));
    5211                 :            :                 else
    5212                 :          0 :                         rxp++;
    5213                 :          0 :                 if (rxp >= nb_fwd_ports)
    5214                 :            :                         return;
    5215                 :            :                 /* get the dcb information on next RX and TX ports */
    5216                 :          0 :                 if ((rxp & 0x1) == 0)
    5217                 :          0 :                         txp = (portid_t) (rxp + 1);
    5218                 :            :                 else
    5219                 :          0 :                         txp = (portid_t) (rxp - 1);
    5220                 :          0 :                 rte_eth_dev_get_dcb_info(fwd_ports_ids[rxp], &rxp_dcb_info);
    5221                 :          0 :                 rte_eth_dev_get_dcb_info(fwd_ports_ids[txp], &txp_dcb_info);
    5222                 :            :         }
    5223                 :            : }
    5224                 :            : 
    5225                 :            : static void
    5226                 :          0 : icmp_echo_config_setup(void)
    5227                 :            : {
    5228                 :            :         portid_t  rxp;
    5229                 :            :         queueid_t rxq;
    5230                 :            :         lcoreid_t lc_id;
    5231                 :            :         uint16_t  sm_id;
    5232                 :            : 
    5233                 :          0 :         if ((lcoreid_t)(nb_txq * nb_fwd_ports) < nb_fwd_lcores)
    5234                 :          0 :                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t)
    5235                 :            :                         (nb_txq * nb_fwd_ports);
    5236                 :            :         else
    5237                 :          0 :                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
    5238                 :          0 :         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
    5239                 :          0 :         cur_fwd_config.nb_fwd_streams =
    5240                 :          0 :                 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
    5241                 :          0 :         if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores)
    5242                 :          0 :                 cur_fwd_config.nb_fwd_lcores =
    5243                 :            :                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
    5244                 :          0 :         if (verbose_level > 0) {
    5245                 :          0 :                 printf("%s fwd_cores=%d fwd_ports=%d fwd_streams=%d\n",
    5246                 :            :                        __FUNCTION__,
    5247                 :            :                        cur_fwd_config.nb_fwd_lcores,
    5248                 :            :                        cur_fwd_config.nb_fwd_ports,
    5249                 :            :                        cur_fwd_config.nb_fwd_streams);
    5250                 :            :         }
    5251                 :            : 
    5252                 :            :         /* reinitialize forwarding streams */
    5253                 :          0 :         init_fwd_streams();
    5254                 :          0 :         setup_fwd_config_of_each_lcore(&cur_fwd_config);
    5255                 :            :         rxp = 0; rxq = 0;
    5256                 :          0 :         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
    5257                 :          0 :                 if (verbose_level > 0)
    5258                 :            :                         printf("  core=%d: \n", lc_id);
    5259                 :          0 :                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
    5260                 :            :                         struct fwd_stream *fs;
    5261                 :          0 :                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
    5262                 :          0 :                         fs->rx_port = fwd_ports_ids[rxp];
    5263                 :          0 :                         fs->rx_queue = rxq;
    5264                 :          0 :                         fs->tx_port = fs->rx_port;
    5265                 :          0 :                         fs->tx_queue = rxq;
    5266                 :          0 :                         fs->peer_addr = fs->tx_port;
    5267                 :          0 :                         fs->retry_enabled = retry_enabled;
    5268                 :          0 :                         if (verbose_level > 0)
    5269                 :          0 :                                 printf("  stream=%d port=%d rxq=%d txq=%d\n",
    5270                 :            :                                        sm_id, fs->rx_port, fs->rx_queue,
    5271                 :            :                                        fs->tx_queue);
    5272                 :          0 :                         rxq = (queueid_t) (rxq + 1);
    5273                 :          0 :                         if (rxq == nb_rxq) {
    5274                 :            :                                 rxq = 0;
    5275                 :          0 :                                 rxp = (portid_t) (rxp + 1);
    5276                 :            :                         }
    5277                 :            :                 }
    5278                 :            :         }
    5279                 :          0 : }
    5280                 :            : 
    5281                 :            : void
    5282                 :          0 : fwd_config_setup(void)
    5283                 :            : {
    5284                 :            :         struct rte_port *port;
    5285                 :            :         portid_t pt_id;
    5286                 :            :         unsigned int i;
    5287                 :            : 
    5288                 :          0 :         cur_fwd_config.fwd_eng = cur_fwd_eng;
    5289                 :          0 :         if (strcmp(cur_fwd_eng->fwd_mode_name, "icmpecho") == 0) {
    5290                 :          0 :                 icmp_echo_config_setup();
    5291                 :          0 :                 return;
    5292                 :            :         }
    5293                 :            : 
    5294                 :          0 :         if ((nb_rxq > 1) && (nb_txq > 1)){
    5295                 :          0 :                 if (dcb_config) {
    5296                 :          0 :                         for (i = 0; i < nb_fwd_ports; i++) {
    5297                 :          0 :                                 pt_id = fwd_ports_ids[i];
    5298                 :          0 :                                 port = &ports[pt_id];
    5299                 :          0 :                                 if (!port->dcb_flag) {
    5300                 :          0 :                                         fprintf(stderr,
    5301                 :            :                                                 "In DCB mode, all forwarding ports must be configured in this mode.\n");
    5302                 :          0 :                                         return;
    5303                 :            :                                 }
    5304                 :            :                         }
    5305                 :          0 :                         if (nb_fwd_lcores == 1) {
    5306                 :          0 :                                 fprintf(stderr,
    5307                 :            :                                         "In DCB mode,the nb forwarding cores should be larger than 1.\n");
    5308                 :          0 :                                 return;
    5309                 :            :                         }
    5310                 :            : 
    5311                 :          0 :                         dcb_fwd_config_setup();
    5312                 :            :                 } else
    5313                 :          0 :                         rss_fwd_config_setup();
    5314                 :            :         }
    5315                 :            :         else
    5316                 :          0 :                 simple_fwd_config_setup();
    5317                 :            : }
    5318                 :            : 
    5319                 :            : static const char *
    5320                 :            : mp_alloc_to_str(uint8_t mode)
    5321                 :            : {
    5322                 :          0 :         switch (mode) {
    5323                 :            :         case MP_ALLOC_NATIVE:
    5324                 :            :                 return "native";
    5325                 :          0 :         case MP_ALLOC_ANON:
    5326                 :          0 :                 return "anon";
    5327                 :          0 :         case MP_ALLOC_XMEM:
    5328                 :          0 :                 return "xmem";
    5329                 :          0 :         case MP_ALLOC_XMEM_HUGE:
    5330                 :          0 :                 return "xmemhuge";
    5331                 :          0 :         case MP_ALLOC_XBUF:
    5332                 :          0 :                 return "xbuf";
    5333                 :          0 :         default:
    5334                 :          0 :                 return "invalid";
    5335                 :            :         }
    5336                 :            : }
    5337                 :            : 
    5338                 :            : void
    5339                 :          0 : pkt_fwd_config_display(struct fwd_config *cfg)
    5340                 :            : {
    5341                 :            :         struct fwd_stream *fs;
    5342                 :            :         lcoreid_t  lc_id;
    5343                 :            :         streamid_t sm_id;
    5344                 :            : 
    5345                 :          0 :         printf("%s%s%s packet forwarding%s - ports=%d - cores=%d - streams=%d - "
    5346                 :            :                 "NUMA support %s, MP allocation mode: %s\n",
    5347                 :            :                 cfg->fwd_eng->fwd_mode_name,
    5348                 :            :                 cfg->fwd_eng->status ? "-" : "",
    5349                 :          0 :                 cfg->fwd_eng->status ? cfg->fwd_eng->status : "",
    5350                 :          0 :                 retry_enabled == 0 ? "" : " with retry",
    5351                 :          0 :                 cfg->nb_fwd_ports, cfg->nb_fwd_lcores, cfg->nb_fwd_streams,
    5352                 :          0 :                 numa_support == 1 ? "enabled" : "disabled",
    5353                 :            :                 mp_alloc_to_str(mp_alloc_type));
    5354                 :            : 
    5355                 :          0 :         if (retry_enabled)
    5356                 :          0 :                 printf("TX retry num: %u, delay between TX retries: %uus\n",
    5357                 :            :                         burst_tx_retry_num, burst_tx_delay_time);
    5358                 :          0 :         for (lc_id = 0; lc_id < cfg->nb_fwd_lcores; lc_id++) {
    5359                 :          0 :                 printf("Logical Core %u (socket %u) forwards packets on "
    5360                 :            :                        "%d streams:",
    5361                 :            :                        fwd_lcores_cpuids[lc_id],
    5362                 :            :                        rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]),
    5363                 :          0 :                        fwd_lcores[lc_id]->stream_nb);
    5364                 :          0 :                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
    5365                 :          0 :                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
    5366                 :          0 :                         printf("\n  RX P=%d/Q=%d (socket %u) -> TX "
    5367                 :            :                                "P=%d/Q=%d (socket %u) ",
    5368                 :          0 :                                fs->rx_port, fs->rx_queue,
    5369                 :          0 :                                ports[fs->rx_port].socket_id,
    5370                 :          0 :                                fs->tx_port, fs->tx_queue,
    5371                 :          0 :                                ports[fs->tx_port].socket_id);
    5372                 :          0 :                         print_ethaddr("peer=",
    5373                 :          0 :                                       &peer_eth_addrs[fs->peer_addr]);
    5374                 :            :                 }
    5375                 :            :                 printf("\n");
    5376                 :            :         }
    5377                 :            :         printf("\n");
    5378                 :          0 : }
    5379                 :            : 
    5380                 :            : void
    5381                 :          0 : set_fwd_eth_peer(portid_t port_id, char *peer_addr)
    5382                 :            : {
    5383                 :            :         struct rte_ether_addr new_peer_addr;
    5384                 :          0 :         if (!rte_eth_dev_is_valid_port(port_id)) {
    5385                 :          0 :                 fprintf(stderr, "Error: Invalid port number %i\n", port_id);
    5386                 :          0 :                 return;
    5387                 :            :         }
    5388                 :          0 :         if (rte_ether_unformat_addr(peer_addr, &new_peer_addr) < 0) {
    5389                 :          0 :                 fprintf(stderr, "Error: Invalid ethernet address: %s\n",
    5390                 :            :                         peer_addr);
    5391                 :          0 :                 return;
    5392                 :            :         }
    5393                 :          0 :         peer_eth_addrs[port_id] = new_peer_addr;
    5394                 :            : }
    5395                 :            : 
    5396                 :            : void
    5397                 :          0 : set_dev_led(portid_t port_id, bool active)
    5398                 :            : {
    5399                 :            :         int ret;
    5400                 :            : 
    5401                 :          0 :         if (!rte_eth_dev_is_valid_port(port_id)) {
    5402                 :          0 :                 fprintf(stderr, "Error: Invalid port number %u\n", port_id);
    5403                 :          0 :                 return;
    5404                 :            :         }
    5405                 :            : 
    5406                 :          0 :         if (active)
    5407                 :          0 :                 ret = rte_eth_led_on(port_id);
    5408                 :            :         else
    5409                 :          0 :                 ret = rte_eth_led_off(port_id);
    5410                 :            : 
    5411                 :          0 :         if (ret < 0)
    5412                 :          0 :                 fprintf(stderr, "Error: Unable to change LED state for port %u: %s\n",
    5413                 :            :                         port_id, rte_strerror(-ret));
    5414                 :            : }
    5415                 :            : 
    5416                 :            : int
    5417                 :          0 : set_fwd_lcores_list(unsigned int *lcorelist, unsigned int nb_lc)
    5418                 :            : {
    5419                 :            :         unsigned int i;
    5420                 :            :         unsigned int lcore_cpuid;
    5421                 :            :         int record_now;
    5422                 :            : 
    5423                 :            :         record_now = 0;
    5424                 :          0 :  again:
    5425                 :          0 :         for (i = 0; i < nb_lc; i++) {
    5426                 :          0 :                 lcore_cpuid = lcorelist[i];
    5427                 :          0 :                 if (! rte_lcore_is_enabled(lcore_cpuid)) {
    5428                 :          0 :                         fprintf(stderr, "lcore %u not enabled\n", lcore_cpuid);
    5429                 :          0 :                         return -1;
    5430                 :            :                 }
    5431                 :          0 :                 if (lcore_cpuid == rte_get_main_lcore()) {
    5432                 :          0 :                         fprintf(stderr,
    5433                 :            :                                 "lcore %u cannot be masked on for running packet forwarding, which is the main lcore and reserved for command line parsing only\n",
    5434                 :            :                                 lcore_cpuid);
    5435                 :          0 :                         return -1;
    5436                 :            :                 }
    5437                 :          0 :                 if (record_now)
    5438                 :          0 :                         fwd_lcores_cpuids[i] = lcore_cpuid;
    5439                 :            :         }
    5440                 :          0 :         if (record_now == 0) {
    5441                 :            :                 record_now = 1;
    5442                 :          0 :                 goto again;
    5443                 :            :         }
    5444                 :          0 :         nb_cfg_lcores = (lcoreid_t) nb_lc;
    5445                 :          0 :         if (nb_fwd_lcores != (lcoreid_t) nb_lc) {
    5446                 :            :                 printf("previous number of forwarding cores %u - changed to "
    5447                 :            :                        "number of configured cores %u\n",
    5448                 :            :                        (unsigned int) nb_fwd_lcores, nb_lc);
    5449                 :          0 :                 nb_fwd_lcores = (lcoreid_t) nb_lc;
    5450                 :            :         }
    5451                 :            : 
    5452                 :            :         return 0;
    5453                 :            : }
    5454                 :            : 
    5455                 :            : int
    5456                 :          0 : set_fwd_lcores_mask(uint64_t lcoremask)
    5457                 :            : {
    5458                 :            :         unsigned int lcorelist[64];
    5459                 :            :         unsigned int nb_lc;
    5460                 :            :         unsigned int i;
    5461                 :            : 
    5462                 :          0 :         if (lcoremask == 0) {
    5463                 :          0 :                 fprintf(stderr, "Invalid NULL mask of cores\n");
    5464                 :          0 :                 return -1;
    5465                 :            :         }
    5466                 :            :         nb_lc = 0;
    5467                 :          0 :         for (i = 0; i < 64; i++) {
    5468                 :          0 :                 if (! ((uint64_t)(1ULL << i) & lcoremask))
    5469                 :          0 :                         continue;
    5470                 :          0 :                 lcorelist[nb_lc++] = i;
    5471                 :            :         }
    5472                 :          0 :         return set_fwd_lcores_list(lcorelist, nb_lc);
    5473                 :            : }
    5474                 :            : 
    5475                 :            : void
    5476                 :          0 : set_fwd_lcores_number(uint16_t nb_lc)
    5477                 :            : {
    5478                 :          0 :         if (test_done == 0) {
    5479                 :          0 :                 fprintf(stderr, "Please stop forwarding first\n");
    5480                 :          0 :                 return;
    5481                 :            :         }
    5482                 :          0 :         if (nb_lc > nb_cfg_lcores) {
    5483                 :          0 :                 fprintf(stderr,
    5484                 :            :                         "nb fwd cores %u > %u (max. number of configured lcores) - ignored\n",
    5485                 :            :                         (unsigned int) nb_lc, (unsigned int) nb_cfg_lcores);
    5486                 :          0 :                 return;
    5487                 :            :         }
    5488                 :          0 :         nb_fwd_lcores = (lcoreid_t) nb_lc;
    5489                 :            :         printf("Number of forwarding cores set to %u\n",
    5490                 :            :                (unsigned int) nb_fwd_lcores);
    5491                 :            : }
    5492                 :            : 
    5493                 :            : void
    5494                 :          0 : set_fwd_ports_list(unsigned int *portlist, unsigned int nb_pt)
    5495                 :            : {
    5496                 :            :         unsigned int i;
    5497                 :            :         portid_t port_id;
    5498                 :            :         int record_now;
    5499                 :            : 
    5500                 :            :         record_now = 0;
    5501                 :          0 :  again:
    5502                 :          0 :         for (i = 0; i < nb_pt; i++) {
    5503                 :          0 :                 port_id = (portid_t) portlist[i];
    5504                 :          0 :                 if (port_id_is_invalid(port_id, ENABLED_WARN))
    5505                 :            :                         return;
    5506                 :          0 :                 if (record_now)
    5507                 :          0 :                         fwd_ports_ids[i] = port_id;
    5508                 :            :         }
    5509                 :          0 :         if (record_now == 0) {
    5510                 :            :                 record_now = 1;
    5511                 :          0 :                 goto again;
    5512                 :            :         }
    5513                 :          0 :         nb_cfg_ports = (portid_t) nb_pt;
    5514                 :          0 :         if (nb_fwd_ports != (portid_t) nb_pt) {
    5515                 :          0 :                 printf("previous number of forwarding ports %u - changed to "
    5516                 :            :                        "number of configured ports %u\n",
    5517                 :            :                        (unsigned int) nb_fwd_ports, nb_pt);
    5518                 :          0 :                 nb_fwd_ports = (portid_t) nb_pt;
    5519                 :            :         }
    5520                 :            : }
    5521                 :            : 
    5522                 :            : /**
    5523                 :            :  * Parse the user input and obtain the list of forwarding ports
    5524                 :            :  *
    5525                 :            :  * @param[in] list
    5526                 :            :  *   String containing the user input. User can specify
    5527                 :            :  *   in these formats 1,3,5 or 1-3 or 1-2,5 or 3,5-6.
    5528                 :            :  *   For example, if the user wants to use all the available
    5529                 :            :  *   4 ports in his system, then the input can be 0-3 or 0,1,2,3.
    5530                 :            :  *   If the user wants to use only the ports 1,2 then the input
    5531                 :            :  *   is 1,2.
    5532                 :            :  *   valid characters are '-' and ','
    5533                 :            :  * @param[out] values
    5534                 :            :  *   This array will be filled with a list of port IDs
    5535                 :            :  *   based on the user input
    5536                 :            :  *   Note that duplicate entries are discarded and only the first
    5537                 :            :  *   count entries in this array are port IDs and all the rest
    5538                 :            :  *   will contain default values
    5539                 :            :  * @param[in] maxsize
    5540                 :            :  *   This parameter denotes 2 things
    5541                 :            :  *   1) Number of elements in the values array
    5542                 :            :  *   2) Maximum value of each element in the values array
    5543                 :            :  * @return
    5544                 :            :  *   On success, returns total count of parsed port IDs
    5545                 :            :  *   On failure, returns 0
    5546                 :            :  */
    5547                 :            : static unsigned int
    5548                 :          0 : parse_port_list(const char *list, unsigned int *values, unsigned int maxsize)
    5549                 :          0 : {
    5550                 :            :         unsigned int count = 0;
    5551                 :          0 :         char *end = NULL;
    5552                 :            :         int min, max;
    5553                 :            :         int value, i;
    5554                 :          0 :         unsigned int marked[maxsize];
    5555                 :            : 
    5556                 :          0 :         if (list == NULL || values == NULL)
    5557                 :            :                 return 0;
    5558                 :            : 
    5559                 :          0 :         for (i = 0; i < (int)maxsize; i++)
    5560                 :          0 :                 marked[i] = 0;
    5561                 :            : 
    5562                 :            :         min = INT_MAX;
    5563                 :            : 
    5564                 :            :         do {
    5565                 :            :                 /*Remove the blank spaces if any*/
    5566                 :          0 :                 while (isblank(*list))
    5567                 :          0 :                         list++;
    5568                 :          0 :                 if (*list == '\0')
    5569                 :            :                         break;
    5570                 :          0 :                 errno = 0;
    5571                 :          0 :                 value = strtol(list, &end, 10);
    5572                 :          0 :                 if (errno || end == NULL)
    5573                 :            :                         return 0;
    5574                 :          0 :                 if (value < 0 || value >= (int)maxsize)
    5575                 :            :                         return 0;
    5576                 :          0 :                 while (isblank(*end))
    5577                 :          0 :                         end++;
    5578                 :          0 :                 if (*end == '-' && min == INT_MAX) {
    5579                 :            :                         min = value;
    5580                 :          0 :                 } else if ((*end == ',') || (*end == '\0')) {
    5581                 :            :                         max = value;
    5582                 :          0 :                         if (min == INT_MAX)
    5583                 :            :                                 min = value;
    5584                 :          0 :                         for (i = min; i <= max; i++) {
    5585                 :          0 :                                 if (count < maxsize) {
    5586                 :          0 :                                         if (marked[i])
    5587                 :          0 :                                                 continue;
    5588                 :          0 :                                         values[count] = i;
    5589                 :          0 :                                         marked[i] = 1;
    5590                 :          0 :                                         count++;
    5591                 :            :                                 }
    5592                 :            :                         }
    5593                 :            :                         min = INT_MAX;
    5594                 :            :                 } else
    5595                 :            :                         return 0;
    5596                 :          0 :                 list = end + 1;
    5597                 :          0 :         } while (*end != '\0');
    5598                 :            : 
    5599                 :            :         return count;
    5600                 :            : }
    5601                 :            : 
    5602                 :            : void
    5603                 :          0 : parse_fwd_portlist(const char *portlist)
    5604                 :            : {
    5605                 :            :         unsigned int portcount;
    5606                 :            :         unsigned int portindex[RTE_MAX_ETHPORTS];
    5607                 :            :         unsigned int i, valid_port_count = 0;
    5608                 :            : 
    5609                 :          0 :         portcount = parse_port_list(portlist, portindex, RTE_MAX_ETHPORTS);
    5610                 :          0 :         if (!portcount)
    5611                 :          0 :                 rte_exit(EXIT_FAILURE, "Invalid fwd port list\n");
    5612                 :            : 
    5613                 :            :         /*
    5614                 :            :          * Here we verify the validity of the ports
    5615                 :            :          * and thereby calculate the total number of
    5616                 :            :          * valid ports
    5617                 :            :          */
    5618                 :          0 :         for (i = 0; i < portcount && i < RTE_DIM(portindex); i++) {
    5619                 :          0 :                 if (rte_eth_dev_is_valid_port(portindex[i])) {
    5620                 :          0 :                         portindex[valid_port_count] = portindex[i];
    5621                 :          0 :                         valid_port_count++;
    5622                 :            :                 }
    5623                 :            :         }
    5624                 :            : 
    5625                 :          0 :         set_fwd_ports_list(portindex, valid_port_count);
    5626                 :          0 : }
    5627                 :            : 
    5628                 :            : void
    5629                 :          0 : set_fwd_ports_mask(uint64_t portmask)
    5630                 :            : {
    5631                 :            :         unsigned int portlist[64];
    5632                 :            :         unsigned int nb_pt;
    5633                 :            :         unsigned int i;
    5634                 :            : 
    5635                 :          0 :         if (portmask == 0) {
    5636                 :          0 :                 fprintf(stderr, "Invalid NULL mask of ports\n");
    5637                 :          0 :                 return;
    5638                 :            :         }
    5639                 :            :         nb_pt = 0;
    5640                 :          0 :         RTE_ETH_FOREACH_DEV(i) {
    5641                 :          0 :                 if (! ((uint64_t)(1ULL << i) & portmask))
    5642                 :          0 :                         continue;
    5643                 :          0 :                 portlist[nb_pt++] = i;
    5644                 :            :         }
    5645                 :          0 :         set_fwd_ports_list(portlist, nb_pt);
    5646                 :            : }
    5647                 :            : 
    5648                 :            : void
    5649                 :          0 : set_fwd_ports_number(uint16_t nb_pt)
    5650                 :            : {
    5651                 :          0 :         if (nb_pt > nb_cfg_ports) {
    5652                 :          0 :                 fprintf(stderr,
    5653                 :            :                         "nb fwd ports %u > %u (number of configured ports) - ignored\n",
    5654                 :            :                         (unsigned int) nb_pt, (unsigned int) nb_cfg_ports);
    5655                 :          0 :                 return;
    5656                 :            :         }
    5657                 :          0 :         nb_fwd_ports = (portid_t) nb_pt;
    5658                 :          0 :         printf("Number of forwarding ports set to %u\n",
    5659                 :            :                (unsigned int) nb_fwd_ports);
    5660                 :            : }
    5661                 :            : 
    5662                 :            : int
    5663                 :          0 : port_is_forwarding(portid_t port_id)
    5664                 :            : {
    5665                 :            :         unsigned int i;
    5666                 :            : 
    5667                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    5668                 :            :                 return -1;
    5669                 :            : 
    5670                 :          0 :         for (i = 0; i < nb_fwd_ports; i++) {
    5671                 :          0 :                 if (fwd_ports_ids[i] == port_id)
    5672                 :            :                         return 1;
    5673                 :            :         }
    5674                 :            : 
    5675                 :            :         return 0;
    5676                 :            : }
    5677                 :            : 
    5678                 :            : void
    5679                 :          0 : set_nb_pkt_per_burst(uint16_t nb)
    5680                 :            : {
    5681                 :          0 :         if (nb > MAX_PKT_BURST) {
    5682                 :          0 :                 fprintf(stderr,
    5683                 :            :                         "nb pkt per burst: %u > %u (maximum packet per burst)  ignored\n",
    5684                 :            :                         (unsigned int) nb, (unsigned int) MAX_PKT_BURST);
    5685                 :          0 :                 return;
    5686                 :            :         }
    5687                 :          0 :         nb_pkt_per_burst = nb;
    5688                 :          0 :         printf("Number of packets per burst set to %u\n",
    5689                 :            :                (unsigned int) nb_pkt_per_burst);
    5690                 :            : }
    5691                 :            : 
    5692                 :            : static const char *
    5693                 :            : tx_split_get_name(enum tx_pkt_split split)
    5694                 :            : {
    5695                 :            :         uint32_t i;
    5696                 :            : 
    5697                 :          0 :         for (i = 0; i != RTE_DIM(tx_split_name); i++) {
    5698                 :          0 :                 if (tx_split_name[i].split == split)
    5699                 :          0 :                         return tx_split_name[i].name;
    5700                 :            :         }
    5701                 :            :         return NULL;
    5702                 :            : }
    5703                 :            : 
    5704                 :            : void
    5705                 :          0 : set_tx_pkt_split(const char *name)
    5706                 :            : {
    5707                 :            :         uint32_t i;
    5708                 :            : 
    5709                 :          0 :         for (i = 0; i != RTE_DIM(tx_split_name); i++) {
    5710                 :          0 :                 if (strcmp(tx_split_name[i].name, name) == 0) {
    5711                 :          0 :                         tx_pkt_split = tx_split_name[i].split;
    5712                 :          0 :                         return;
    5713                 :            :                 }
    5714                 :            :         }
    5715                 :          0 :         fprintf(stderr, "unknown value: \"%s\"\n", name);
    5716                 :            : }
    5717                 :            : 
    5718                 :            : int
    5719                 :          0 : parse_fec_mode(const char *name, uint32_t *fec_capa)
    5720                 :            : {
    5721                 :            :         uint8_t i;
    5722                 :            : 
    5723                 :          0 :         for (i = 0; i < RTE_DIM(fec_mode_name); i++) {
    5724                 :          0 :                 if (strcmp(fec_mode_name[i].name, name) == 0) {
    5725                 :          0 :                         *fec_capa =
    5726                 :          0 :                                 RTE_ETH_FEC_MODE_TO_CAPA(fec_mode_name[i].mode);
    5727                 :          0 :                         return 0;
    5728                 :            :                 }
    5729                 :            :         }
    5730                 :            :         return -1;
    5731                 :            : }
    5732                 :            : 
    5733                 :            : void
    5734                 :          0 : show_fec_capability(unsigned int num, struct rte_eth_fec_capa *speed_fec_capa)
    5735                 :            : {
    5736                 :            :         unsigned int i, j;
    5737                 :            : 
    5738                 :            :         printf("FEC capabilities:\n");
    5739                 :            : 
    5740                 :          0 :         for (i = 0; i < num; i++) {
    5741                 :          0 :                 printf("%s : ",
    5742                 :          0 :                         rte_eth_link_speed_to_str(speed_fec_capa[i].speed));
    5743                 :            : 
    5744                 :          0 :                 for (j = 0; j < RTE_DIM(fec_mode_name); j++) {
    5745                 :          0 :                         if (RTE_ETH_FEC_MODE_TO_CAPA(j) &
    5746                 :          0 :                                                 speed_fec_capa[i].capa)
    5747                 :          0 :                                 printf("%s ", fec_mode_name[j].name);
    5748                 :            :                 }
    5749                 :            :                 printf("\n");
    5750                 :            :         }
    5751                 :          0 : }
    5752                 :            : 
    5753                 :            : void
    5754                 :          0 : show_rx_pkt_offsets(void)
    5755                 :            : {
    5756                 :            :         uint32_t i, n;
    5757                 :            : 
    5758                 :          0 :         n = rx_pkt_nb_offs;
    5759                 :            :         printf("Number of offsets: %u\n", n);
    5760                 :          0 :         if (n) {
    5761                 :            :                 printf("Segment offsets: ");
    5762                 :          0 :                 for (i = 0; i != n - 1; i++)
    5763                 :          0 :                         printf("%hu,", rx_pkt_seg_offsets[i]);
    5764                 :          0 :                 printf("%hu\n", rx_pkt_seg_lengths[i]);
    5765                 :            :         }
    5766                 :          0 : }
    5767                 :            : 
    5768                 :            : void
    5769                 :          0 : set_rx_pkt_offsets(unsigned int *seg_offsets, unsigned int nb_offs)
    5770                 :            : {
    5771                 :            :         unsigned int i;
    5772                 :            : 
    5773                 :          0 :         if (nb_offs >= MAX_SEGS_BUFFER_SPLIT) {
    5774                 :            :                 printf("nb segments per RX packets=%u >= "
    5775                 :            :                        "MAX_SEGS_BUFFER_SPLIT - ignored\n", nb_offs);
    5776                 :          0 :                 return;
    5777                 :            :         }
    5778                 :            : 
    5779                 :            :         /*
    5780                 :            :          * No extra check here, the segment length will be checked by PMD
    5781                 :            :          * in the extended queue setup.
    5782                 :            :          */
    5783                 :          0 :         for (i = 0; i < nb_offs; i++) {
    5784                 :          0 :                 if (seg_offsets[i] >= UINT16_MAX) {
    5785                 :            :                         printf("offset[%u]=%u > UINT16_MAX - give up\n",
    5786                 :            :                                i, seg_offsets[i]);
    5787                 :          0 :                         return;
    5788                 :            :                 }
    5789                 :            :         }
    5790                 :            : 
    5791                 :          0 :         for (i = 0; i < nb_offs; i++)
    5792                 :          0 :                 rx_pkt_seg_offsets[i] = (uint16_t) seg_offsets[i];
    5793                 :            : 
    5794                 :          0 :         rx_pkt_nb_offs = (uint8_t) nb_offs;
    5795                 :            : }
    5796                 :            : 
    5797                 :            : void
    5798                 :          0 : show_rx_pkt_segments(void)
    5799                 :            : {
    5800                 :            :         uint32_t i, n;
    5801                 :            : 
    5802                 :          0 :         n = rx_pkt_nb_segs;
    5803                 :            :         printf("Number of segments: %u\n", n);
    5804                 :          0 :         if (n) {
    5805                 :            :                 printf("Segment sizes: ");
    5806                 :          0 :                 for (i = 0; i != n - 1; i++)
    5807                 :          0 :                         printf("%hu,", rx_pkt_seg_lengths[i]);
    5808                 :          0 :                 printf("%hu\n", rx_pkt_seg_lengths[i]);
    5809                 :            :         }
    5810                 :          0 : }
    5811                 :            : 
    5812                 :          0 : static const char *get_ptype_str(uint32_t ptype)
    5813                 :            : {
    5814                 :            :         const char *str;
    5815                 :            : 
    5816                 :          0 :         switch (ptype) {
    5817                 :            :         case RTE_PTYPE_L2_ETHER:
    5818                 :            :                 str = "eth";
    5819                 :            :                 break;
    5820                 :          0 :         case RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN:
    5821                 :            :                 str = "ipv4";
    5822                 :          0 :                 break;
    5823                 :          0 :         case RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN:
    5824                 :            :                 str = "ipv6";
    5825                 :          0 :                 break;
    5826                 :          0 :         case RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_L4_TCP:
    5827                 :            :                 str = "ipv4-tcp";
    5828                 :          0 :                 break;
    5829                 :          0 :         case RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_L4_UDP:
    5830                 :            :                 str = "ipv4-udp";
    5831                 :          0 :                 break;
    5832                 :          0 :         case RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_L4_SCTP:
    5833                 :            :                 str = "ipv4-sctp";
    5834                 :          0 :                 break;
    5835                 :          0 :         case RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_L4_TCP:
    5836                 :            :                 str = "ipv6-tcp";
    5837                 :          0 :                 break;
    5838                 :          0 :         case RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_L4_UDP:
    5839                 :            :                 str = "ipv6-udp";
    5840                 :          0 :                 break;
    5841                 :          0 :         case RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_L4_SCTP:
    5842                 :            :                 str = "ipv6-sctp";
    5843                 :          0 :                 break;
    5844                 :          0 :         case RTE_PTYPE_TUNNEL_GRENAT:
    5845                 :            :                 str = "grenat";
    5846                 :          0 :                 break;
    5847                 :          0 :         case RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER:
    5848                 :            :                 str = "inner-eth";
    5849                 :          0 :                 break;
    5850                 :          0 :         case RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER
    5851                 :            :                         | RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN:
    5852                 :            :                 str = "inner-ipv4";
    5853                 :          0 :                 break;
    5854                 :          0 :         case RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER
    5855                 :            :                         | RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN:
    5856                 :            :                 str = "inner-ipv6";
    5857                 :          0 :                 break;
    5858                 :          0 :         case RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
    5859                 :            :                         RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_INNER_L4_TCP:
    5860                 :            :                 str = "inner-ipv4-tcp";
    5861                 :          0 :                 break;
    5862                 :          0 :         case RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
    5863                 :            :                         RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_INNER_L4_UDP:
    5864                 :            :                 str = "inner-ipv4-udp";
    5865                 :          0 :                 break;
    5866                 :          0 :         case RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
    5867                 :            :                         RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_INNER_L4_SCTP:
    5868                 :            :                 str = "inner-ipv4-sctp";
    5869                 :          0 :                 break;
    5870                 :          0 :         case RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
    5871                 :            :                         RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_INNER_L4_TCP:
    5872                 :            :                 str = "inner-ipv6-tcp";
    5873                 :          0 :                 break;
    5874                 :          0 :         case RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
    5875                 :            :                         RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_INNER_L4_UDP:
    5876                 :            :                 str = "inner-ipv6-udp";
    5877                 :          0 :                 break;
    5878                 :          0 :         case RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
    5879                 :            :                         RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_INNER_L4_SCTP:
    5880                 :            :                 str = "inner-ipv6-sctp";
    5881                 :          0 :                 break;
    5882                 :          0 :         default:
    5883                 :            :                 str = "unsupported";
    5884                 :            :         }
    5885                 :            : 
    5886                 :          0 :         return str;
    5887                 :            : }
    5888                 :            : 
    5889                 :            : void
    5890                 :          0 : show_rx_pkt_hdrs(void)
    5891                 :            : {
    5892                 :            :         uint32_t i, n;
    5893                 :            : 
    5894                 :          0 :         n = rx_pkt_nb_segs;
    5895                 :            :         printf("Number of segments: %u\n", n);
    5896                 :          0 :         if (n) {
    5897                 :            :                 printf("Packet segs: ");
    5898                 :          0 :                 for (i = 0; i < n - 1; i++)
    5899                 :          0 :                         printf("%s, ", get_ptype_str(rx_pkt_hdr_protos[i]));
    5900                 :            :                 printf("payload\n");
    5901                 :            :         }
    5902                 :          0 : }
    5903                 :            : 
    5904                 :            : void
    5905                 :          0 : set_rx_pkt_hdrs(unsigned int *seg_hdrs, unsigned int nb_segs)
    5906                 :            : {
    5907                 :            :         unsigned int i;
    5908                 :            : 
    5909                 :          0 :         if (nb_segs + 1 > MAX_SEGS_BUFFER_SPLIT) {
    5910                 :            :                 printf("nb segments per RX packets=%u > "
    5911                 :            :                        "MAX_SEGS_BUFFER_SPLIT - ignored\n", nb_segs + 1);
    5912                 :          0 :                 return;
    5913                 :            :         }
    5914                 :            : 
    5915                 :            :         memset(rx_pkt_hdr_protos, 0, sizeof(rx_pkt_hdr_protos));
    5916                 :            : 
    5917                 :          0 :         for (i = 0; i < nb_segs; i++)
    5918                 :          0 :                 rx_pkt_hdr_protos[i] = (uint32_t)seg_hdrs[i];
    5919                 :            :         /*
    5920                 :            :          * We calculate the number of hdrs, but payload is not included,
    5921                 :            :          * so rx_pkt_nb_segs would increase 1.
    5922                 :            :          */
    5923                 :          0 :         rx_pkt_nb_segs = nb_segs + 1;
    5924                 :            : }
    5925                 :            : 
    5926                 :            : void
    5927                 :          0 : set_rx_pkt_segments(unsigned int *seg_lengths, unsigned int nb_segs)
    5928                 :            : {
    5929                 :            :         unsigned int i;
    5930                 :            : 
    5931                 :          0 :         if (nb_segs >= MAX_SEGS_BUFFER_SPLIT) {
    5932                 :            :                 printf("nb segments per RX packets=%u >= "
    5933                 :            :                        "MAX_SEGS_BUFFER_SPLIT - ignored\n", nb_segs);
    5934                 :          0 :                 return;
    5935                 :            :         }
    5936                 :            : 
    5937                 :            :         /*
    5938                 :            :          * No extra check here, the segment length will be checked by PMD
    5939                 :            :          * in the extended queue setup.
    5940                 :            :          */
    5941                 :          0 :         for (i = 0; i < nb_segs; i++) {
    5942                 :          0 :                 if (seg_lengths[i] >= UINT16_MAX) {
    5943                 :            :                         printf("length[%u]=%u > UINT16_MAX - give up\n",
    5944                 :            :                                i, seg_lengths[i]);
    5945                 :          0 :                         return;
    5946                 :            :                 }
    5947                 :            :         }
    5948                 :            : 
    5949                 :          0 :         for (i = 0; i < nb_segs; i++)
    5950                 :          0 :                 rx_pkt_seg_lengths[i] = (uint16_t) seg_lengths[i];
    5951                 :            : 
    5952                 :          0 :         rx_pkt_nb_segs = (uint8_t) nb_segs;
    5953                 :            : }
    5954                 :            : 
    5955                 :            : void
    5956                 :          0 : show_tx_pkt_segments(void)
    5957                 :            : {
    5958                 :            :         uint32_t i, n;
    5959                 :            :         const char *split;
    5960                 :            : 
    5961                 :          0 :         n = tx_pkt_nb_segs;
    5962                 :          0 :         split = tx_split_get_name(tx_pkt_split);
    5963                 :            : 
    5964                 :            :         printf("Number of segments: %u\n", n);
    5965                 :            :         printf("Segment sizes: ");
    5966                 :          0 :         for (i = 0; i != n - 1; i++)
    5967                 :          0 :                 printf("%hu,", tx_pkt_seg_lengths[i]);
    5968                 :          0 :         printf("%hu\n", tx_pkt_seg_lengths[i]);
    5969                 :            :         printf("Split packet: %s\n", split);
    5970                 :          0 : }
    5971                 :            : 
    5972                 :            : static bool
    5973                 :          0 : nb_segs_is_invalid(unsigned int nb_segs)
    5974                 :            : {
    5975                 :            :         uint16_t ring_size;
    5976                 :            :         uint16_t queue_id;
    5977                 :            :         uint16_t port_id;
    5978                 :            :         int ret;
    5979                 :            : 
    5980                 :          0 :         RTE_ETH_FOREACH_DEV(port_id) {
    5981                 :          0 :                 for (queue_id = 0; queue_id < nb_txq; queue_id++) {
    5982                 :          0 :                         ret = get_tx_ring_size(port_id, queue_id, &ring_size);
    5983                 :          0 :                         if (ret) {
    5984                 :            :                                 /* Port may not be initialized yet, can't say
    5985                 :            :                                  * the port is invalid in this stage.
    5986                 :            :                                  */
    5987                 :          0 :                                 continue;
    5988                 :            :                         }
    5989                 :          0 :                         if (ring_size < nb_segs) {
    5990                 :          0 :                                 printf("nb segments per TX packets=%u >= TX "
    5991                 :            :                                        "queue(%u) ring_size=%u - txpkts ignored\n",
    5992                 :            :                                        nb_segs, queue_id, ring_size);
    5993                 :          0 :                                 return true;
    5994                 :            :                         }
    5995                 :            :                 }
    5996                 :            :         }
    5997                 :            : 
    5998                 :            :         return false;
    5999                 :            : }
    6000                 :            : 
    6001                 :            : void
    6002                 :          0 : set_tx_pkt_segments(unsigned int *seg_lengths, unsigned int nb_segs)
    6003                 :            : {
    6004                 :            :         uint16_t tx_pkt_len;
    6005                 :            :         unsigned int i;
    6006                 :            : 
    6007                 :            :         /*
    6008                 :            :          * For single segment settings failed check is ignored.
    6009                 :            :          * It is a very basic capability to send the single segment
    6010                 :            :          * packets, suppose it is always supported.
    6011                 :            :          */
    6012                 :          0 :         if (nb_segs > 1 && nb_segs_is_invalid(nb_segs)) {
    6013                 :          0 :                 fprintf(stderr,
    6014                 :            :                         "Tx segment size(%u) is not supported - txpkts ignored\n",
    6015                 :            :                         nb_segs);
    6016                 :          0 :                 return;
    6017                 :            :         }
    6018                 :            : 
    6019                 :          0 :         if (nb_segs > RTE_MAX_SEGS_PER_PKT) {
    6020                 :          0 :                 fprintf(stderr,
    6021                 :            :                         "Tx segment size(%u) is bigger than max number of segment(%u)\n",
    6022                 :            :                         nb_segs, RTE_MAX_SEGS_PER_PKT);
    6023                 :          0 :                 return;
    6024                 :            :         }
    6025                 :            : 
    6026                 :            :         /*
    6027                 :            :          * Check that each segment length is greater or equal than
    6028                 :            :          * the mbuf data size.
    6029                 :            :          * Check also that the total packet length is greater or equal than the
    6030                 :            :          * size of an empty UDP/IP packet (sizeof(struct rte_ether_hdr) +
    6031                 :            :          * 20 + 8).
    6032                 :            :          */
    6033                 :            :         tx_pkt_len = 0;
    6034                 :          0 :         for (i = 0; i < nb_segs; i++) {
    6035                 :          0 :                 if (seg_lengths[i] > mbuf_data_size[0]) {
    6036                 :          0 :                         fprintf(stderr,
    6037                 :            :                                 "length[%u]=%u > mbuf_data_size=%u - give up\n",
    6038                 :            :                                 i, seg_lengths[i], mbuf_data_size[0]);
    6039                 :          0 :                         return;
    6040                 :            :                 }
    6041                 :          0 :                 tx_pkt_len = (uint16_t)(tx_pkt_len + seg_lengths[i]);
    6042                 :            :         }
    6043                 :          0 :         if (tx_pkt_len < (sizeof(struct rte_ether_hdr) + 20 + 8)) {
    6044                 :          0 :                 fprintf(stderr, "total packet length=%u < %d - give up\n",
    6045                 :            :                                 (unsigned) tx_pkt_len,
    6046                 :            :                                 (int)(sizeof(struct rte_ether_hdr) + 20 + 8));
    6047                 :          0 :                 return;
    6048                 :            :         }
    6049                 :            : 
    6050                 :          0 :         for (i = 0; i < nb_segs; i++)
    6051                 :          0 :                 tx_pkt_seg_lengths[i] = (uint16_t) seg_lengths[i];
    6052                 :            : 
    6053                 :          0 :         tx_pkt_length  = tx_pkt_len;
    6054                 :          0 :         tx_pkt_nb_segs = (uint8_t) nb_segs;
    6055                 :            : }
    6056                 :            : 
    6057                 :            : void
    6058                 :          0 : show_tx_pkt_times(void)
    6059                 :            : {
    6060                 :          0 :         printf("Interburst gap: %u\n", tx_pkt_times_inter);
    6061                 :          0 :         printf("Intraburst gap: %u\n", tx_pkt_times_intra);
    6062                 :          0 : }
    6063                 :            : 
    6064                 :            : void
    6065                 :          0 : set_tx_pkt_times(unsigned int *tx_times)
    6066                 :            : {
    6067                 :          0 :         tx_pkt_times_inter = tx_times[0];
    6068                 :          0 :         tx_pkt_times_intra = tx_times[1];
    6069                 :          0 : }
    6070                 :            : 
    6071                 :            : #ifdef RTE_LIB_GRO
    6072                 :            : void
    6073                 :          0 : setup_gro(const char *onoff, portid_t port_id)
    6074                 :            : {
    6075                 :          0 :         if (!rte_eth_dev_is_valid_port(port_id)) {
    6076                 :          0 :                 fprintf(stderr, "invalid port id %u\n", port_id);
    6077                 :          0 :                 return;
    6078                 :            :         }
    6079                 :          0 :         if (test_done == 0) {
    6080                 :          0 :                 fprintf(stderr,
    6081                 :            :                         "Before enable/disable GRO, please stop forwarding first\n");
    6082                 :          0 :                 return;
    6083                 :            :         }
    6084                 :          0 :         if (strcmp(onoff, "on") == 0) {
    6085                 :          0 :                 if (gro_ports[port_id].enable != 0) {
    6086                 :          0 :                         fprintf(stderr,
    6087                 :            :                                 "Port %u has enabled GRO. Please disable GRO first\n",
    6088                 :            :                                 port_id);
    6089                 :          0 :                         return;
    6090                 :            :                 }
    6091                 :          0 :                 if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) {
    6092                 :          0 :                         gro_ports[port_id].param.gro_types = RTE_GRO_TCP_IPV4;
    6093                 :          0 :                         gro_ports[port_id].param.max_flow_num =
    6094                 :            :                                 GRO_DEFAULT_FLOW_NUM;
    6095                 :          0 :                         gro_ports[port_id].param.max_item_per_flow =
    6096                 :            :                                 GRO_DEFAULT_ITEM_NUM_PER_FLOW;
    6097                 :            :                 }
    6098                 :          0 :                 gro_ports[port_id].enable = 1;
    6099                 :            :         } else {
    6100                 :          0 :                 if (gro_ports[port_id].enable == 0) {
    6101                 :          0 :                         fprintf(stderr, "Port %u has disabled GRO\n", port_id);
    6102                 :          0 :                         return;
    6103                 :            :                 }
    6104                 :          0 :                 gro_ports[port_id].enable = 0;
    6105                 :            :         }
    6106                 :            : }
    6107                 :            : 
    6108                 :            : void
    6109                 :          0 : setup_gro_flush_cycles(uint8_t cycles)
    6110                 :            : {
    6111                 :          0 :         if (test_done == 0) {
    6112                 :          0 :                 fprintf(stderr,
    6113                 :            :                         "Before change flush interval for GRO, please stop forwarding first.\n");
    6114                 :          0 :                 return;
    6115                 :            :         }
    6116                 :            : 
    6117                 :          0 :         if (cycles > GRO_MAX_FLUSH_CYCLES || cycles <
    6118                 :            :                         GRO_DEFAULT_FLUSH_CYCLES) {
    6119                 :          0 :                 fprintf(stderr,
    6120                 :            :                         "The flushing cycle be in the range of 1 to %u. Revert to the default value %u.\n",
    6121                 :            :                         GRO_MAX_FLUSH_CYCLES, GRO_DEFAULT_FLUSH_CYCLES);
    6122                 :            :                 cycles = GRO_DEFAULT_FLUSH_CYCLES;
    6123                 :            :         }
    6124                 :            : 
    6125                 :          0 :         gro_flush_cycles = cycles;
    6126                 :            : }
    6127                 :            : 
    6128                 :            : void
    6129                 :          0 : show_gro(portid_t port_id)
    6130                 :            : {
    6131                 :            :         struct rte_gro_param *param;
    6132                 :            :         uint32_t max_pkts_num;
    6133                 :            : 
    6134                 :          0 :         param = &gro_ports[port_id].param;
    6135                 :            : 
    6136                 :          0 :         if (!rte_eth_dev_is_valid_port(port_id)) {
    6137                 :          0 :                 fprintf(stderr, "Invalid port id %u.\n", port_id);
    6138                 :          0 :                 return;
    6139                 :            :         }
    6140                 :          0 :         if (gro_ports[port_id].enable) {
    6141                 :            :                 printf("GRO type: TCP/IPv4\n");
    6142                 :          0 :                 if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) {
    6143                 :          0 :                         max_pkts_num = param->max_flow_num *
    6144                 :          0 :                                 param->max_item_per_flow;
    6145                 :            :                 } else
    6146                 :            :                         max_pkts_num = MAX_PKT_BURST * GRO_MAX_FLUSH_CYCLES;
    6147                 :            :                 printf("Max number of packets to perform GRO: %u\n",
    6148                 :            :                                 max_pkts_num);
    6149                 :          0 :                 printf("Flushing cycles: %u\n", gro_flush_cycles);
    6150                 :            :         } else
    6151                 :            :                 printf("Port %u doesn't enable GRO.\n", port_id);
    6152                 :            : }
    6153                 :            : #endif /* RTE_LIB_GRO */
    6154                 :            : 
    6155                 :            : #ifdef RTE_LIB_GSO
    6156                 :            : void
    6157                 :          0 : setup_gso(const char *mode, portid_t port_id)
    6158                 :            : {
    6159                 :          0 :         if (!rte_eth_dev_is_valid_port(port_id)) {
    6160                 :          0 :                 fprintf(stderr, "invalid port id %u\n", port_id);
    6161                 :          0 :                 return;
    6162                 :            :         }
    6163                 :          0 :         if (strcmp(mode, "on") == 0) {
    6164                 :          0 :                 if (test_done == 0) {
    6165                 :          0 :                         fprintf(stderr,
    6166                 :            :                                 "before enabling GSO, please stop forwarding first\n");
    6167                 :          0 :                         return;
    6168                 :            :                 }
    6169                 :          0 :                 gso_ports[port_id].enable = 1;
    6170                 :          0 :         } else if (strcmp(mode, "off") == 0) {
    6171                 :          0 :                 if (test_done == 0) {
    6172                 :          0 :                         fprintf(stderr,
    6173                 :            :                                 "before disabling GSO, please stop forwarding first\n");
    6174                 :          0 :                         return;
    6175                 :            :                 }
    6176                 :          0 :                 gso_ports[port_id].enable = 0;
    6177                 :            :         }
    6178                 :            : }
    6179                 :            : #endif /* RTE_LIB_GSO */
    6180                 :            : 
    6181                 :            : char*
    6182                 :          0 : list_pkt_forwarding_modes(void)
    6183                 :            : {
    6184                 :            :         static char fwd_modes[128] = "";
    6185                 :            :         const char *separator = "|";
    6186                 :            :         struct fwd_engine *fwd_eng;
    6187                 :            :         unsigned i = 0;
    6188                 :            : 
    6189                 :          0 :         if (strlen (fwd_modes) == 0) {
    6190                 :          0 :                 while ((fwd_eng = fwd_engines[i++]) != NULL) {
    6191                 :          0 :                         strncat(fwd_modes, fwd_eng->fwd_mode_name,
    6192                 :          0 :                                         sizeof(fwd_modes) - strlen(fwd_modes) - 1);
    6193                 :          0 :                         strncat(fwd_modes, separator,
    6194                 :          0 :                                         sizeof(fwd_modes) - strlen(fwd_modes) - 1);
    6195                 :            :                 }
    6196                 :          0 :                 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
    6197                 :            :         }
    6198                 :            : 
    6199                 :          0 :         return fwd_modes;
    6200                 :            : }
    6201                 :            : 
    6202                 :            : char*
    6203                 :          0 : list_pkt_forwarding_retry_modes(void)
    6204                 :            : {
    6205                 :            :         static char fwd_modes[128] = "";
    6206                 :            :         const char *separator = "|";
    6207                 :            :         struct fwd_engine *fwd_eng;
    6208                 :            :         unsigned i = 0;
    6209                 :            : 
    6210                 :          0 :         if (strlen(fwd_modes) == 0) {
    6211                 :          0 :                 while ((fwd_eng = fwd_engines[i++]) != NULL) {
    6212                 :          0 :                         if (fwd_eng == &rx_only_engine)
    6213                 :          0 :                                 continue;
    6214                 :          0 :                         strncat(fwd_modes, fwd_eng->fwd_mode_name,
    6215                 :            :                                         sizeof(fwd_modes) -
    6216                 :          0 :                                         strlen(fwd_modes) - 1);
    6217                 :          0 :                         strncat(fwd_modes, separator,
    6218                 :            :                                         sizeof(fwd_modes) -
    6219                 :          0 :                                         strlen(fwd_modes) - 1);
    6220                 :            :                 }
    6221                 :          0 :                 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
    6222                 :            :         }
    6223                 :            : 
    6224                 :          0 :         return fwd_modes;
    6225                 :            : }
    6226                 :            : 
    6227                 :            : void
    6228                 :          0 : set_pkt_forwarding_mode(const char *fwd_mode_name)
    6229                 :            : {
    6230                 :            :         struct fwd_engine *fwd_eng;
    6231                 :            :         unsigned i;
    6232                 :            : 
    6233                 :            :         i = 0;
    6234                 :          0 :         while ((fwd_eng = fwd_engines[i]) != NULL) {
    6235                 :          0 :                 if (! strcmp(fwd_eng->fwd_mode_name, fwd_mode_name)) {
    6236                 :          0 :                         printf("Set %s packet forwarding mode%s\n",
    6237                 :            :                                fwd_mode_name,
    6238                 :          0 :                                retry_enabled == 0 ? "" : " with retry");
    6239                 :          0 :                         cur_fwd_eng = fwd_eng;
    6240                 :          0 :                         return;
    6241                 :            :                 }
    6242                 :          0 :                 i++;
    6243                 :            :         }
    6244                 :          0 :         fprintf(stderr, "Invalid %s packet forwarding mode\n", fwd_mode_name);
    6245                 :            : }
    6246                 :            : 
    6247                 :            : void
    6248                 :          0 : add_rx_dump_callbacks(portid_t portid)
    6249                 :            : {
    6250                 :            :         struct rte_eth_dev_info dev_info;
    6251                 :            :         uint16_t queue;
    6252                 :            :         int ret;
    6253                 :            : 
    6254                 :          0 :         if (port_id_is_invalid(portid, ENABLED_WARN))
    6255                 :          0 :                 return;
    6256                 :            : 
    6257                 :          0 :         ret = eth_dev_info_get_print_err(portid, &dev_info);
    6258                 :          0 :         if (ret != 0)
    6259                 :            :                 return;
    6260                 :            : 
    6261                 :          0 :         for (queue = 0; queue < dev_info.nb_rx_queues; queue++)
    6262                 :          0 :                 if (!ports[portid].rx_dump_cb[queue])
    6263                 :          0 :                         ports[portid].rx_dump_cb[queue] =
    6264                 :          0 :                                 rte_eth_add_rx_callback(portid, queue,
    6265                 :            :                                         dump_rx_pkts, NULL);
    6266                 :            : }
    6267                 :            : 
    6268                 :            : void
    6269                 :          0 : add_tx_dump_callbacks(portid_t portid)
    6270                 :            : {
    6271                 :            :         struct rte_eth_dev_info dev_info;
    6272                 :            :         uint16_t queue;
    6273                 :            :         int ret;
    6274                 :            : 
    6275                 :          0 :         if (port_id_is_invalid(portid, ENABLED_WARN))
    6276                 :          0 :                 return;
    6277                 :            : 
    6278                 :          0 :         ret = eth_dev_info_get_print_err(portid, &dev_info);
    6279                 :          0 :         if (ret != 0)
    6280                 :            :                 return;
    6281                 :            : 
    6282                 :          0 :         for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
    6283                 :          0 :                 if (!ports[portid].tx_dump_cb[queue])
    6284                 :          0 :                         ports[portid].tx_dump_cb[queue] =
    6285                 :          0 :                                 rte_eth_add_tx_callback(portid, queue,
    6286                 :            :                                                         dump_tx_pkts, NULL);
    6287                 :            : }
    6288                 :            : 
    6289                 :            : void
    6290                 :          0 : remove_rx_dump_callbacks(portid_t portid)
    6291                 :            : {
    6292                 :            :         struct rte_eth_dev_info dev_info;
    6293                 :            :         uint16_t queue;
    6294                 :            :         int ret;
    6295                 :            : 
    6296                 :          0 :         if (port_id_is_invalid(portid, ENABLED_WARN))
    6297                 :          0 :                 return;
    6298                 :            : 
    6299                 :          0 :         ret = eth_dev_info_get_print_err(portid, &dev_info);
    6300                 :          0 :         if (ret != 0)
    6301                 :            :                 return;
    6302                 :            : 
    6303                 :          0 :         for (queue = 0; queue < dev_info.nb_rx_queues; queue++)
    6304                 :          0 :                 if (ports[portid].rx_dump_cb[queue]) {
    6305                 :          0 :                         rte_eth_remove_rx_callback(portid, queue,
    6306                 :            :                                 ports[portid].rx_dump_cb[queue]);
    6307                 :          0 :                         ports[portid].rx_dump_cb[queue] = NULL;
    6308                 :            :                 }
    6309                 :            : }
    6310                 :            : 
    6311                 :            : void
    6312                 :          0 : remove_tx_dump_callbacks(portid_t portid)
    6313                 :            : {
    6314                 :            :         struct rte_eth_dev_info dev_info;
    6315                 :            :         uint16_t queue;
    6316                 :            :         int ret;
    6317                 :            : 
    6318                 :          0 :         if (port_id_is_invalid(portid, ENABLED_WARN))
    6319                 :          0 :                 return;
    6320                 :            : 
    6321                 :          0 :         ret = eth_dev_info_get_print_err(portid, &dev_info);
    6322                 :          0 :         if (ret != 0)
    6323                 :            :                 return;
    6324                 :            : 
    6325                 :          0 :         for (queue = 0; queue < dev_info.nb_tx_queues; queue++)
    6326                 :          0 :                 if (ports[portid].tx_dump_cb[queue]) {
    6327                 :          0 :                         rte_eth_remove_tx_callback(portid, queue,
    6328                 :            :                                 ports[portid].tx_dump_cb[queue]);
    6329                 :          0 :                         ports[portid].tx_dump_cb[queue] = NULL;
    6330                 :            :                 }
    6331                 :            : }
    6332                 :            : 
    6333                 :            : void
    6334                 :          0 : configure_rxtx_dump_callbacks(uint16_t verbose)
    6335                 :            : {
    6336                 :            :         portid_t portid;
    6337                 :            : 
    6338                 :            : #ifndef RTE_ETHDEV_RXTX_CALLBACKS
    6339                 :            :                 TESTPMD_LOG(ERR, "setting rxtx callbacks is not enabled\n");
    6340                 :            :                 return;
    6341                 :            : #endif
    6342                 :            : 
    6343                 :          0 :         RTE_ETH_FOREACH_DEV(portid)
    6344                 :            :         {
    6345                 :          0 :                 if (verbose == 1 || verbose > 2)
    6346                 :          0 :                         add_rx_dump_callbacks(portid);
    6347                 :            :                 else
    6348                 :          0 :                         remove_rx_dump_callbacks(portid);
    6349                 :          0 :                 if (verbose >= 2)
    6350                 :          0 :                         add_tx_dump_callbacks(portid);
    6351                 :            :                 else
    6352                 :          0 :                         remove_tx_dump_callbacks(portid);
    6353                 :            :         }
    6354                 :          0 : }
    6355                 :            : 
    6356                 :            : void
    6357                 :          0 : set_verbose_level(uint16_t vb_level)
    6358                 :            : {
    6359                 :          0 :         printf("Change verbose level from %u to %u\n",
    6360                 :            :                (unsigned int) verbose_level, (unsigned int) vb_level);
    6361                 :          0 :         verbose_level = vb_level;
    6362                 :          0 :         configure_rxtx_dump_callbacks(verbose_level);
    6363                 :          0 : }
    6364                 :            : 
    6365                 :            : void
    6366                 :          0 : vlan_extend_set(portid_t port_id, int on)
    6367                 :            : {
    6368                 :            :         int diag;
    6369                 :            :         int vlan_offload;
    6370                 :          0 :         uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads;
    6371                 :            : 
    6372                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    6373                 :            :                 return;
    6374                 :            : 
    6375                 :          0 :         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
    6376                 :            : 
    6377                 :          0 :         if (on) {
    6378                 :          0 :                 vlan_offload |= RTE_ETH_VLAN_EXTEND_OFFLOAD;
    6379                 :          0 :                 port_rx_offloads |= RTE_ETH_RX_OFFLOAD_VLAN_EXTEND;
    6380                 :            :         } else {
    6381                 :          0 :                 vlan_offload &= ~RTE_ETH_VLAN_EXTEND_OFFLOAD;
    6382                 :          0 :                 port_rx_offloads &= ~RTE_ETH_RX_OFFLOAD_VLAN_EXTEND;
    6383                 :            :         }
    6384                 :            : 
    6385                 :          0 :         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
    6386                 :          0 :         if (diag < 0) {
    6387                 :          0 :                 fprintf(stderr,
    6388                 :            :                         "rx_vlan_extend_set(port_pi=%d, on=%d) failed diag=%d\n",
    6389                 :            :                         port_id, on, diag);
    6390                 :          0 :                 return;
    6391                 :            :         }
    6392                 :          0 :         ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads;
    6393                 :            : }
    6394                 :            : 
    6395                 :            : void
    6396                 :          0 : rx_vlan_strip_set(portid_t port_id, int on)
    6397                 :            : {
    6398                 :            :         int diag;
    6399                 :            :         int vlan_offload;
    6400                 :          0 :         uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads;
    6401                 :            : 
    6402                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    6403                 :            :                 return;
    6404                 :            : 
    6405                 :          0 :         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
    6406                 :            : 
    6407                 :          0 :         if (on) {
    6408                 :          0 :                 vlan_offload |= RTE_ETH_VLAN_STRIP_OFFLOAD;
    6409                 :          0 :                 port_rx_offloads |= RTE_ETH_RX_OFFLOAD_VLAN_STRIP;
    6410                 :            :         } else {
    6411                 :          0 :                 vlan_offload &= ~RTE_ETH_VLAN_STRIP_OFFLOAD;
    6412                 :          0 :                 port_rx_offloads &= ~RTE_ETH_RX_OFFLOAD_VLAN_STRIP;
    6413                 :            :         }
    6414                 :            : 
    6415                 :          0 :         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
    6416                 :          0 :         if (diag < 0) {
    6417                 :          0 :                 fprintf(stderr,
    6418                 :            :                         "%s(port_pi=%d, on=%d) failed diag=%d\n",
    6419                 :            :                         __func__, port_id, on, diag);
    6420                 :          0 :                 return;
    6421                 :            :         }
    6422                 :          0 :         ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads;
    6423                 :            : }
    6424                 :            : 
    6425                 :            : void
    6426                 :          0 : rx_vlan_strip_set_on_queue(portid_t port_id, uint16_t queue_id, int on)
    6427                 :            : {
    6428                 :            :         int diag;
    6429                 :            : 
    6430                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    6431                 :            :                 return;
    6432                 :            : 
    6433                 :          0 :         diag = rte_eth_dev_set_vlan_strip_on_queue(port_id, queue_id, on);
    6434                 :          0 :         if (diag < 0)
    6435                 :          0 :                 fprintf(stderr,
    6436                 :            :                         "%s(port_pi=%d, queue_id=%d, on=%d) failed diag=%d\n",
    6437                 :            :                         __func__, port_id, queue_id, on, diag);
    6438                 :            : }
    6439                 :            : 
    6440                 :            : void
    6441                 :          0 : rx_vlan_filter_set(portid_t port_id, int on)
    6442                 :            : {
    6443                 :            :         int diag;
    6444                 :            :         int vlan_offload;
    6445                 :          0 :         uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads;
    6446                 :            : 
    6447                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    6448                 :            :                 return;
    6449                 :            : 
    6450                 :          0 :         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
    6451                 :            : 
    6452                 :          0 :         if (on) {
    6453                 :          0 :                 vlan_offload |= RTE_ETH_VLAN_FILTER_OFFLOAD;
    6454                 :          0 :                 port_rx_offloads |= RTE_ETH_RX_OFFLOAD_VLAN_FILTER;
    6455                 :            :         } else {
    6456                 :          0 :                 vlan_offload &= ~RTE_ETH_VLAN_FILTER_OFFLOAD;
    6457                 :          0 :                 port_rx_offloads &= ~RTE_ETH_RX_OFFLOAD_VLAN_FILTER;
    6458                 :            :         }
    6459                 :            : 
    6460                 :          0 :         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
    6461                 :          0 :         if (diag < 0) {
    6462                 :          0 :                 fprintf(stderr,
    6463                 :            :                         "%s(port_pi=%d, on=%d) failed diag=%d\n",
    6464                 :            :                         __func__, port_id, on, diag);
    6465                 :          0 :                 return;
    6466                 :            :         }
    6467                 :          0 :         ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads;
    6468                 :            : }
    6469                 :            : 
    6470                 :            : void
    6471                 :          0 : rx_vlan_qinq_strip_set(portid_t port_id, int on)
    6472                 :            : {
    6473                 :            :         int diag;
    6474                 :            :         int vlan_offload;
    6475                 :          0 :         uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads;
    6476                 :            : 
    6477                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    6478                 :            :                 return;
    6479                 :            : 
    6480                 :          0 :         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
    6481                 :            : 
    6482                 :          0 :         if (on) {
    6483                 :          0 :                 vlan_offload |= RTE_ETH_QINQ_STRIP_OFFLOAD;
    6484                 :          0 :                 port_rx_offloads |= RTE_ETH_RX_OFFLOAD_QINQ_STRIP;
    6485                 :            :         } else {
    6486                 :          0 :                 vlan_offload &= ~RTE_ETH_QINQ_STRIP_OFFLOAD;
    6487                 :          0 :                 port_rx_offloads &= ~RTE_ETH_RX_OFFLOAD_QINQ_STRIP;
    6488                 :            :         }
    6489                 :            : 
    6490                 :          0 :         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
    6491                 :          0 :         if (diag < 0) {
    6492                 :          0 :                 fprintf(stderr, "%s(port_pi=%d, on=%d) failed diag=%d\n",
    6493                 :            :                         __func__, port_id, on, diag);
    6494                 :          0 :                 return;
    6495                 :            :         }
    6496                 :          0 :         ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads;
    6497                 :            : }
    6498                 :            : 
    6499                 :            : int
    6500                 :          0 : rx_vft_set(portid_t port_id, uint16_t vlan_id, int on)
    6501                 :            : {
    6502                 :            :         int diag;
    6503                 :            : 
    6504                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    6505                 :            :                 return 1;
    6506                 :          0 :         if (vlan_id_is_invalid(vlan_id))
    6507                 :          0 :                 return 1;
    6508                 :          0 :         diag = rte_eth_dev_vlan_filter(port_id, vlan_id, on);
    6509                 :          0 :         if (diag == 0)
    6510                 :            :                 return 0;
    6511                 :          0 :         fprintf(stderr,
    6512                 :            :                 "rte_eth_dev_vlan_filter(port_pi=%d, vlan_id=%d, on=%d) failed diag=%d\n",
    6513                 :            :                 port_id, vlan_id, on, diag);
    6514                 :          0 :         return -1;
    6515                 :            : }
    6516                 :            : 
    6517                 :            : void
    6518                 :          0 : rx_vlan_all_filter_set(portid_t port_id, int on)
    6519                 :            : {
    6520                 :            :         uint16_t vlan_id;
    6521                 :            : 
    6522                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    6523                 :            :                 return;
    6524                 :          0 :         for (vlan_id = 0; vlan_id < 4096; vlan_id++) {
    6525                 :          0 :                 if (rx_vft_set(port_id, vlan_id, on))
    6526                 :            :                         break;
    6527                 :            :         }
    6528                 :            : }
    6529                 :            : 
    6530                 :            : void
    6531                 :          0 : vlan_tpid_set(portid_t port_id, enum rte_vlan_type vlan_type, uint16_t tp_id)
    6532                 :            : {
    6533                 :            :         int diag;
    6534                 :            : 
    6535                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    6536                 :            :                 return;
    6537                 :            : 
    6538                 :          0 :         diag = rte_eth_dev_set_vlan_ether_type(port_id, vlan_type, tp_id);
    6539                 :          0 :         if (diag == 0)
    6540                 :            :                 return;
    6541                 :            : 
    6542                 :          0 :         fprintf(stderr,
    6543                 :            :                 "tx_vlan_tpid_set(port_pi=%d, vlan_type=%d, tpid=%d) failed diag=%d\n",
    6544                 :            :                 port_id, vlan_type, tp_id, diag);
    6545                 :            : }
    6546                 :            : 
    6547                 :            : void
    6548                 :          0 : tx_vlan_set(portid_t port_id, uint16_t vlan_id)
    6549                 :            : {
    6550                 :            :         struct rte_eth_dev_info dev_info;
    6551                 :            :         int ret;
    6552                 :            : 
    6553                 :          0 :         if (vlan_id_is_invalid(vlan_id))
    6554                 :          0 :                 return;
    6555                 :            : 
    6556                 :          0 :         if (ports[port_id].dev_conf.txmode.offloads &
    6557                 :            :             RTE_ETH_TX_OFFLOAD_QINQ_INSERT) {
    6558                 :          0 :                 fprintf(stderr, "Error, as QinQ has been enabled.\n");
    6559                 :          0 :                 return;
    6560                 :            :         }
    6561                 :            : 
    6562                 :          0 :         ret = eth_dev_info_get_print_err(port_id, &dev_info);
    6563                 :          0 :         if (ret != 0)
    6564                 :            :                 return;
    6565                 :            : 
    6566                 :          0 :         if ((dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_VLAN_INSERT) == 0) {
    6567                 :          0 :                 fprintf(stderr,
    6568                 :            :                         "Error: vlan insert is not supported by port %d\n",
    6569                 :            :                         port_id);
    6570                 :          0 :                 return;
    6571                 :            :         }
    6572                 :            : 
    6573                 :            :         tx_vlan_reset(port_id);
    6574                 :          0 :         ports[port_id].dev_conf.txmode.offloads |= RTE_ETH_TX_OFFLOAD_VLAN_INSERT;
    6575                 :          0 :         ports[port_id].tx_vlan_id = vlan_id;
    6576                 :            : }
    6577                 :            : 
    6578                 :            : void
    6579                 :          0 : tx_qinq_set(portid_t port_id, uint16_t vlan_id, uint16_t vlan_id_outer)
    6580                 :            : {
    6581                 :            :         struct rte_eth_dev_info dev_info;
    6582                 :            :         int ret;
    6583                 :            : 
    6584                 :          0 :         if (vlan_id_is_invalid(vlan_id))
    6585                 :          0 :                 return;
    6586                 :          0 :         if (vlan_id_is_invalid(vlan_id_outer))
    6587                 :          0 :                 return;
    6588                 :            : 
    6589                 :          0 :         ret = eth_dev_info_get_print_err(port_id, &dev_info);
    6590                 :          0 :         if (ret != 0)
    6591                 :            :                 return;
    6592                 :            : 
    6593                 :          0 :         if ((dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_QINQ_INSERT) == 0) {
    6594                 :          0 :                 fprintf(stderr,
    6595                 :            :                         "Error: qinq insert not supported by port %d\n",
    6596                 :            :                         port_id);
    6597                 :          0 :                 return;
    6598                 :            :         }
    6599                 :            : 
    6600                 :            :         tx_vlan_reset(port_id);
    6601                 :          0 :         ports[port_id].dev_conf.txmode.offloads |= (RTE_ETH_TX_OFFLOAD_VLAN_INSERT |
    6602                 :            :                                                     RTE_ETH_TX_OFFLOAD_QINQ_INSERT);
    6603                 :          0 :         ports[port_id].tx_vlan_id = vlan_id;
    6604                 :          0 :         ports[port_id].tx_vlan_id_outer = vlan_id_outer;
    6605                 :            : }
    6606                 :            : 
    6607                 :            : void
    6608                 :          0 : tx_vlan_reset(portid_t port_id)
    6609                 :            : {
    6610                 :          0 :         ports[port_id].dev_conf.txmode.offloads &=
    6611                 :            :                                 ~(RTE_ETH_TX_OFFLOAD_VLAN_INSERT |
    6612                 :            :                                   RTE_ETH_TX_OFFLOAD_QINQ_INSERT);
    6613                 :          0 :         ports[port_id].tx_vlan_id = 0;
    6614                 :          0 :         ports[port_id].tx_vlan_id_outer = 0;
    6615                 :          0 : }
    6616                 :            : 
    6617                 :            : void
    6618                 :          0 : tx_vlan_pvid_set(portid_t port_id, uint16_t vlan_id, int on)
    6619                 :            : {
    6620                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    6621                 :            :                 return;
    6622                 :            : 
    6623                 :          0 :         rte_eth_dev_set_vlan_pvid(port_id, vlan_id, on);
    6624                 :            : }
    6625                 :            : 
    6626                 :            : void
    6627                 :          0 : set_qmap(portid_t port_id, uint8_t is_rx, uint16_t queue_id, uint8_t map_value)
    6628                 :            : {
    6629                 :            :         int ret;
    6630                 :            : 
    6631                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    6632                 :            :                 return;
    6633                 :            : 
    6634                 :          0 :         if (is_rx ? (rx_queue_id_is_invalid(queue_id)) : (tx_queue_id_is_invalid(queue_id)))
    6635                 :            :                 return;
    6636                 :            : 
    6637                 :          0 :         if (map_value >= RTE_ETHDEV_QUEUE_STAT_CNTRS) {
    6638                 :          0 :                 fprintf(stderr, "map_value not in required range 0..%d\n",
    6639                 :            :                         RTE_ETHDEV_QUEUE_STAT_CNTRS - 1);
    6640                 :          0 :                 return;
    6641                 :            :         }
    6642                 :            : 
    6643                 :          0 :         if (!is_rx) { /* tx */
    6644                 :          0 :                 ret = rte_eth_dev_set_tx_queue_stats_mapping(port_id, queue_id,
    6645                 :            :                                                              map_value);
    6646                 :          0 :                 if (ret) {
    6647                 :          0 :                         fprintf(stderr,
    6648                 :            :                                 "failed to set tx queue stats mapping.\n");
    6649                 :          0 :                         return;
    6650                 :            :                 }
    6651                 :            :         } else { /* rx */
    6652                 :          0 :                 ret = rte_eth_dev_set_rx_queue_stats_mapping(port_id, queue_id,
    6653                 :            :                                                              map_value);
    6654                 :          0 :                 if (ret) {
    6655                 :          0 :                         fprintf(stderr,
    6656                 :            :                                 "failed to set rx queue stats mapping.\n");
    6657                 :          0 :                         return;
    6658                 :            :                 }
    6659                 :            :         }
    6660                 :            : }
    6661                 :            : 
    6662                 :            : void
    6663                 :          0 : set_xstats_hide_zero(uint8_t on_off)
    6664                 :            : {
    6665                 :          0 :         xstats_hide_zero = on_off;
    6666                 :          0 : }
    6667                 :            : 
    6668                 :            : void
    6669                 :          0 : set_record_core_cycles(uint8_t on_off)
    6670                 :            : {
    6671                 :          0 :         record_core_cycles = on_off;
    6672                 :          0 : }
    6673                 :            : 
    6674                 :            : void
    6675                 :          0 : set_record_burst_stats(uint8_t on_off)
    6676                 :            : {
    6677                 :          0 :         record_burst_stats = on_off;
    6678                 :          0 : }
    6679                 :            : 
    6680                 :            : uint16_t
    6681                 :          0 : str_to_flowtype(const char *string)
    6682                 :            : {
    6683                 :            :         uint8_t i;
    6684                 :            : 
    6685                 :          0 :         for (i = 0; i < RTE_DIM(flowtype_str_table); i++) {
    6686                 :          0 :                 if (!strcmp(flowtype_str_table[i].str, string))
    6687                 :          0 :                         return flowtype_str_table[i].ftype;
    6688                 :            :         }
    6689                 :            : 
    6690                 :          0 :         if (isdigit(string[0])) {
    6691                 :            :                 int val = atoi(string);
    6692                 :          0 :                 if (val > 0 && val < 64)
    6693                 :          0 :                         return (uint16_t)val;
    6694                 :            :         }
    6695                 :            : 
    6696                 :            :         return RTE_ETH_FLOW_UNKNOWN;
    6697                 :            : }
    6698                 :            : 
    6699                 :            : const char*
    6700                 :          0 : flowtype_to_str(uint16_t flow_type)
    6701                 :            : {
    6702                 :            :         uint8_t i;
    6703                 :            : 
    6704                 :          0 :         for (i = 0; i < RTE_DIM(flowtype_str_table); i++) {
    6705                 :          0 :                 if (flowtype_str_table[i].ftype == flow_type)
    6706                 :          0 :                         return flowtype_str_table[i].str;
    6707                 :            :         }
    6708                 :            : 
    6709                 :            :         return NULL;
    6710                 :            : }
    6711                 :            : 
    6712                 :            : #if defined(RTE_NET_I40E) || defined(RTE_NET_IXGBE)
    6713                 :            : 
    6714                 :            : static inline void
    6715                 :          0 : print_fdir_flex_payload(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num)
    6716                 :            : {
    6717                 :            :         struct rte_eth_flex_payload_cfg *cfg;
    6718                 :            :         uint32_t i, j;
    6719                 :            : 
    6720                 :          0 :         for (i = 0; i < flex_conf->nb_payloads; i++) {
    6721                 :            :                 cfg = &flex_conf->flex_set[i];
    6722                 :          0 :                 if (cfg->type == RTE_ETH_RAW_PAYLOAD)
    6723                 :            :                         printf("\n    RAW:  ");
    6724                 :          0 :                 else if (cfg->type == RTE_ETH_L2_PAYLOAD)
    6725                 :            :                         printf("\n    L2_PAYLOAD:  ");
    6726                 :          0 :                 else if (cfg->type == RTE_ETH_L3_PAYLOAD)
    6727                 :            :                         printf("\n    L3_PAYLOAD:  ");
    6728                 :          0 :                 else if (cfg->type == RTE_ETH_L4_PAYLOAD)
    6729                 :            :                         printf("\n    L4_PAYLOAD:  ");
    6730                 :            :                 else
    6731                 :            :                         printf("\n    UNKNOWN PAYLOAD(%u):  ", cfg->type);
    6732                 :          0 :                 for (j = 0; j < num; j++)
    6733                 :          0 :                         printf("  %-5u", cfg->src_offset[j]);
    6734                 :            :         }
    6735                 :            :         printf("\n");
    6736                 :          0 : }
    6737                 :            : 
    6738                 :            : static inline void
    6739                 :          0 : print_fdir_flex_mask(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num)
    6740                 :            : {
    6741                 :            :         struct rte_eth_fdir_flex_mask *mask;
    6742                 :            :         uint32_t i, j;
    6743                 :            :         const char *p;
    6744                 :            : 
    6745                 :          0 :         for (i = 0; i < flex_conf->nb_flexmasks; i++) {
    6746                 :            :                 mask = &flex_conf->flex_mask[i];
    6747                 :          0 :                 p = flowtype_to_str(mask->flow_type);
    6748                 :          0 :                 printf("\n    %s:\t", p ? p : "unknown");
    6749                 :          0 :                 for (j = 0; j < num; j++)
    6750                 :          0 :                         printf(" %02x", mask->mask[j]);
    6751                 :            :         }
    6752                 :            :         printf("\n");
    6753                 :          0 : }
    6754                 :            : 
    6755                 :            : static inline void
    6756                 :          0 : print_fdir_flow_type(uint32_t flow_types_mask)
    6757                 :            : {
    6758                 :            :         int i;
    6759                 :            :         const char *p;
    6760                 :            : 
    6761                 :          0 :         for (i = RTE_ETH_FLOW_UNKNOWN; i < RTE_ETH_FLOW_MAX; i++) {
    6762                 :          0 :                 if (!(flow_types_mask & (1 << i)))
    6763                 :          0 :                         continue;
    6764                 :          0 :                 p = flowtype_to_str(i);
    6765                 :          0 :                 if (p)
    6766                 :            :                         printf(" %s", p);
    6767                 :            :                 else
    6768                 :            :                         printf(" unknown");
    6769                 :            :         }
    6770                 :            :         printf("\n");
    6771                 :          0 : }
    6772                 :            : 
    6773                 :            : static int
    6774                 :          0 : get_fdir_info(portid_t port_id, struct rte_eth_fdir_info *fdir_info,
    6775                 :            :                     struct rte_eth_fdir_stats *fdir_stat)
    6776                 :            : {
    6777                 :            :         int ret = -ENOTSUP;
    6778                 :            : 
    6779                 :            : #ifdef RTE_NET_I40E
    6780                 :            :         if (ret == -ENOTSUP) {
    6781                 :          0 :                 ret = rte_pmd_i40e_get_fdir_info(port_id, fdir_info);
    6782                 :          0 :                 if (!ret)
    6783                 :          0 :                         ret = rte_pmd_i40e_get_fdir_stats(port_id, fdir_stat);
    6784                 :            :         }
    6785                 :            : #endif
    6786                 :            : #ifdef RTE_NET_IXGBE
    6787                 :          0 :         if (ret == -ENOTSUP) {
    6788                 :          0 :                 ret = rte_pmd_ixgbe_get_fdir_info(port_id, fdir_info);
    6789                 :          0 :                 if (!ret)
    6790                 :          0 :                         ret = rte_pmd_ixgbe_get_fdir_stats(port_id, fdir_stat);
    6791                 :            :         }
    6792                 :            : #endif
    6793                 :          0 :         switch (ret) {
    6794                 :            :         case 0:
    6795                 :            :                 break;
    6796                 :          0 :         case -ENOTSUP:
    6797                 :          0 :                 fprintf(stderr, "\n FDIR is not supported on port %-2d\n",
    6798                 :            :                         port_id);
    6799                 :            :                 break;
    6800                 :          0 :         default:
    6801                 :          0 :                 fprintf(stderr, "programming error: (%s)\n", strerror(-ret));
    6802                 :            :                 break;
    6803                 :            :         }
    6804                 :          0 :         return ret;
    6805                 :            : }
    6806                 :            : 
    6807                 :            : void
    6808                 :          0 : fdir_get_infos(portid_t port_id)
    6809                 :            : {
    6810                 :            :         struct rte_eth_fdir_stats fdir_stat;
    6811                 :            :         struct rte_eth_fdir_info fdir_info;
    6812                 :            : 
    6813                 :            :         static const char *fdir_stats_border = "########################";
    6814                 :            : 
    6815                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    6816                 :          0 :                 return;
    6817                 :            : 
    6818                 :            :         memset(&fdir_info, 0, sizeof(fdir_info));
    6819                 :            :         memset(&fdir_stat, 0, sizeof(fdir_stat));
    6820                 :          0 :         if (get_fdir_info(port_id, &fdir_info, &fdir_stat))
    6821                 :            :                 return;
    6822                 :            : 
    6823                 :          0 :         printf("\n  %s FDIR infos for port %-2d     %s\n",
    6824                 :            :                fdir_stats_border, port_id, fdir_stats_border);
    6825                 :            :         printf("  MODE: ");
    6826                 :          0 :         if (fdir_info.mode == RTE_FDIR_MODE_PERFECT)
    6827                 :            :                 printf("  PERFECT\n");
    6828                 :          0 :         else if (fdir_info.mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN)
    6829                 :            :                 printf("  PERFECT-MAC-VLAN\n");
    6830                 :          0 :         else if (fdir_info.mode == RTE_FDIR_MODE_PERFECT_TUNNEL)
    6831                 :            :                 printf("  PERFECT-TUNNEL\n");
    6832                 :          0 :         else if (fdir_info.mode == RTE_FDIR_MODE_SIGNATURE)
    6833                 :            :                 printf("  SIGNATURE\n");
    6834                 :            :         else
    6835                 :            :                 printf("  DISABLE\n");
    6836                 :          0 :         if (fdir_info.mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN
    6837                 :          0 :                 && fdir_info.mode != RTE_FDIR_MODE_PERFECT_TUNNEL) {
    6838                 :            :                 printf("  SUPPORTED FLOW TYPE: ");
    6839                 :          0 :                 print_fdir_flow_type(fdir_info.flow_types_mask[0]);
    6840                 :            :         }
    6841                 :            :         printf("  FLEX PAYLOAD INFO:\n");
    6842                 :          0 :         printf("  max_len:       %-10"PRIu32"  payload_limit: %-10"PRIu32"\n"
    6843                 :            :                "  payload_unit:  %-10"PRIu32"  payload_seg:   %-10"PRIu32"\n"
    6844                 :            :                "  bitmask_unit:  %-10"PRIu32"  bitmask_num:   %-10"PRIu32"\n",
    6845                 :          0 :                 fdir_info.max_flexpayload, fdir_info.flex_payload_limit,
    6846                 :            :                 fdir_info.flex_payload_unit,
    6847                 :            :                 fdir_info.max_flex_payload_segment_num,
    6848                 :            :                 fdir_info.flex_bitmask_unit, fdir_info.max_flex_bitmask_num);
    6849                 :          0 :         if (fdir_info.flex_conf.nb_payloads > 0) {
    6850                 :            :                 printf("  FLEX PAYLOAD SRC OFFSET:");
    6851                 :          0 :                 print_fdir_flex_payload(&fdir_info.flex_conf, fdir_info.max_flexpayload);
    6852                 :            :         }
    6853                 :          0 :         if (fdir_info.flex_conf.nb_flexmasks > 0) {
    6854                 :            :                 printf("  FLEX MASK CFG:");
    6855                 :          0 :                 print_fdir_flex_mask(&fdir_info.flex_conf, fdir_info.max_flexpayload);
    6856                 :            :         }
    6857                 :          0 :         printf("  guarant_count: %-10"PRIu32"  best_count:    %"PRIu32"\n",
    6858                 :            :                fdir_stat.guarant_cnt, fdir_stat.best_cnt);
    6859                 :          0 :         printf("  guarant_space: %-10"PRIu32"  best_space:    %"PRIu32"\n",
    6860                 :            :                fdir_info.guarant_spc, fdir_info.best_spc);
    6861                 :          0 :         printf("  collision:     %-10"PRIu32"  free:          %"PRIu32"\n"
    6862                 :            :                "  maxhash:       %-10"PRIu32"  maxlen:        %"PRIu32"\n"
    6863                 :            :                "  add:              %-10"PRIu64"  remove:        %"PRIu64"\n"
    6864                 :            :                "  f_add:         %-10"PRIu64"  f_remove:      %"PRIu64"\n",
    6865                 :            :                fdir_stat.collision, fdir_stat.free,
    6866                 :            :                fdir_stat.maxhash, fdir_stat.maxlen,
    6867                 :            :                fdir_stat.add, fdir_stat.remove,
    6868                 :            :                fdir_stat.f_add, fdir_stat.f_remove);
    6869                 :          0 :         printf("  %s############################%s\n",
    6870                 :            :                fdir_stats_border, fdir_stats_border);
    6871                 :            : }
    6872                 :            : 
    6873                 :            : #endif /* RTE_NET_I40E || RTE_NET_IXGBE */
    6874                 :            : 
    6875                 :            : void
    6876                 :          0 : set_vf_traffic(portid_t port_id, uint8_t is_rx, uint16_t vf, uint8_t on)
    6877                 :            : {
    6878                 :            : #ifdef RTE_NET_IXGBE
    6879                 :            :         int diag;
    6880                 :            : 
    6881                 :          0 :         if (is_rx)
    6882                 :          0 :                 diag = rte_pmd_ixgbe_set_vf_rx(port_id, vf, on);
    6883                 :            :         else
    6884                 :          0 :                 diag = rte_pmd_ixgbe_set_vf_tx(port_id, vf, on);
    6885                 :            : 
    6886                 :          0 :         if (diag == 0)
    6887                 :            :                 return;
    6888                 :          0 :         fprintf(stderr,
    6889                 :            :                 "rte_pmd_ixgbe_set_vf_%s for port_id=%d failed diag=%d\n",
    6890                 :            :                 is_rx ? "rx" : "tx", port_id, diag);
    6891                 :            :         return;
    6892                 :            : #endif
    6893                 :            :         fprintf(stderr, "VF %s setting not supported for port %d\n",
    6894                 :            :                 is_rx ? "Rx" : "Tx", port_id);
    6895                 :            :         RTE_SET_USED(vf);
    6896                 :            :         RTE_SET_USED(on);
    6897                 :            : }
    6898                 :            : 
    6899                 :            : int
    6900                 :          0 : set_queue_rate_limit(portid_t port_id, uint16_t queue_idx, uint32_t rate)
    6901                 :            : {
    6902                 :            :         int diag;
    6903                 :            :         struct rte_eth_link link;
    6904                 :            :         int ret;
    6905                 :            : 
    6906                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    6907                 :            :                 return 1;
    6908                 :          0 :         ret = eth_link_get_nowait_print_err(port_id, &link);
    6909                 :          0 :         if (ret < 0)
    6910                 :            :                 return 1;
    6911                 :          0 :         if (link.link_speed != RTE_ETH_SPEED_NUM_UNKNOWN &&
    6912                 :            :             rate > link.link_speed) {
    6913                 :          0 :                 fprintf(stderr,
    6914                 :            :                         "Invalid rate value:%u bigger than link speed: %u\n",
    6915                 :            :                         rate, link.link_speed);
    6916                 :          0 :                 return 1;
    6917                 :            :         }
    6918                 :          0 :         diag = rte_eth_set_queue_rate_limit(port_id, queue_idx, rate);
    6919                 :          0 :         if (diag == 0)
    6920                 :            :                 return diag;
    6921                 :          0 :         fprintf(stderr,
    6922                 :            :                 "rte_eth_set_queue_rate_limit for port_id=%d failed diag=%d\n",
    6923                 :            :                 port_id, diag);
    6924                 :          0 :         return diag;
    6925                 :            : }
    6926                 :            : 
    6927                 :            : int
    6928                 :          0 : set_vf_rate_limit(portid_t port_id, uint16_t vf, uint32_t rate, uint64_t q_msk)
    6929                 :            : {
    6930                 :            :         int diag = -ENOTSUP;
    6931                 :            : 
    6932                 :            :         RTE_SET_USED(vf);
    6933                 :            :         RTE_SET_USED(rate);
    6934                 :            :         RTE_SET_USED(q_msk);
    6935                 :            : 
    6936                 :            : #ifdef RTE_NET_IXGBE
    6937                 :            :         if (diag == -ENOTSUP)
    6938                 :          0 :                 diag = rte_pmd_ixgbe_set_vf_rate_limit(port_id, vf, rate,
    6939                 :            :                                                        q_msk);
    6940                 :            : #endif
    6941                 :            : #ifdef RTE_NET_BNXT
    6942                 :          0 :         if (diag == -ENOTSUP)
    6943                 :          0 :                 diag = rte_pmd_bnxt_set_vf_rate_limit(port_id, vf, rate, q_msk);
    6944                 :            : #endif
    6945                 :          0 :         if (diag == 0)
    6946                 :            :                 return diag;
    6947                 :            : 
    6948                 :          0 :         fprintf(stderr,
    6949                 :            :                 "%s for port_id=%d failed diag=%d\n",
    6950                 :            :                 __func__, port_id, diag);
    6951                 :          0 :         return diag;
    6952                 :            : }
    6953                 :            : 
    6954                 :            : int
    6955                 :          0 : set_rxq_avail_thresh(portid_t port_id, uint16_t queue_id, uint8_t avail_thresh)
    6956                 :            : {
    6957                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    6958                 :            :                 return -EINVAL;
    6959                 :            : 
    6960                 :          0 :         return rte_eth_rx_avail_thresh_set(port_id, queue_id, avail_thresh);
    6961                 :            : }
    6962                 :            : 
    6963                 :            : /*
    6964                 :            :  * Functions to manage the set of filtered Multicast MAC addresses.
    6965                 :            :  *
    6966                 :            :  * A pool of filtered multicast MAC addresses is associated with each port.
    6967                 :            :  * The pool is allocated in chunks of MCAST_POOL_INC multicast addresses.
    6968                 :            :  * The address of the pool and the number of valid multicast MAC addresses
    6969                 :            :  * recorded in the pool are stored in the fields "mc_addr_pool" and
    6970                 :            :  * "mc_addr_nb" of the "rte_port" data structure.
    6971                 :            :  *
    6972                 :            :  * The function "rte_eth_dev_set_mc_addr_list" of the PMDs API imposes
    6973                 :            :  * to be supplied a contiguous array of multicast MAC addresses.
    6974                 :            :  * To comply with this constraint, the set of multicast addresses recorded
    6975                 :            :  * into the pool are systematically compacted at the beginning of the pool.
    6976                 :            :  * Hence, when a multicast address is removed from the pool, all following
    6977                 :            :  * addresses, if any, are copied back to keep the set contiguous.
    6978                 :            :  */
    6979                 :            : #define MCAST_POOL_INC 32
    6980                 :            : 
    6981                 :            : static int
    6982                 :          0 : mcast_addr_pool_extend(struct rte_port *port)
    6983                 :            : {
    6984                 :            :         struct rte_ether_addr *mc_pool;
    6985                 :            :         size_t mc_pool_size;
    6986                 :            : 
    6987                 :            :         /*
    6988                 :            :          * If a free entry is available at the end of the pool, just
    6989                 :            :          * increment the number of recorded multicast addresses.
    6990                 :            :          */
    6991                 :          0 :         if ((port->mc_addr_nb % MCAST_POOL_INC) != 0) {
    6992                 :          0 :                 port->mc_addr_nb++;
    6993                 :          0 :                 return 0;
    6994                 :            :         }
    6995                 :            : 
    6996                 :            :         /*
    6997                 :            :          * [re]allocate a pool with MCAST_POOL_INC more entries.
    6998                 :            :          * The previous test guarantees that port->mc_addr_nb is a multiple
    6999                 :            :          * of MCAST_POOL_INC.
    7000                 :            :          */
    7001                 :          0 :         mc_pool_size = sizeof(struct rte_ether_addr) * (port->mc_addr_nb +
    7002                 :            :                                                     MCAST_POOL_INC);
    7003                 :          0 :         mc_pool = (struct rte_ether_addr *) realloc(port->mc_addr_pool,
    7004                 :            :                                                 mc_pool_size);
    7005                 :          0 :         if (mc_pool == NULL) {
    7006                 :          0 :                 fprintf(stderr,
    7007                 :            :                         "allocation of pool of %u multicast addresses failed\n",
    7008                 :          0 :                         port->mc_addr_nb + MCAST_POOL_INC);
    7009                 :          0 :                 return -ENOMEM;
    7010                 :            :         }
    7011                 :            : 
    7012                 :          0 :         port->mc_addr_pool = mc_pool;
    7013                 :          0 :         port->mc_addr_nb++;
    7014                 :          0 :         return 0;
    7015                 :            : 
    7016                 :            : }
    7017                 :            : 
    7018                 :            : static void
    7019                 :          0 : mcast_addr_pool_append(struct rte_port *port, struct rte_ether_addr *mc_addr)
    7020                 :            : {
    7021                 :          0 :         if (mcast_addr_pool_extend(port) != 0)
    7022                 :            :                 return;
    7023                 :          0 :         rte_ether_addr_copy(mc_addr, &port->mc_addr_pool[port->mc_addr_nb - 1]);
    7024                 :            : }
    7025                 :            : 
    7026                 :            : static void
    7027                 :          0 : mcast_addr_pool_remove(struct rte_port *port, uint32_t addr_idx)
    7028                 :            : {
    7029                 :          0 :         port->mc_addr_nb--;
    7030                 :          0 :         if (addr_idx == port->mc_addr_nb) {
    7031                 :            :                 /* No need to recompact the set of multicast addresses. */
    7032                 :          0 :                 if (port->mc_addr_nb == 0) {
    7033                 :            :                         /* free the pool of multicast addresses. */
    7034                 :          0 :                         free(port->mc_addr_pool);
    7035                 :          0 :                         port->mc_addr_pool = NULL;
    7036                 :            :                 }
    7037                 :          0 :                 return;
    7038                 :            :         }
    7039                 :          0 :         memmove(&port->mc_addr_pool[addr_idx],
    7040                 :          0 :                 &port->mc_addr_pool[addr_idx + 1],
    7041                 :          0 :                 sizeof(struct rte_ether_addr) * (port->mc_addr_nb - addr_idx));
    7042                 :            : }
    7043                 :            : 
    7044                 :            : int
    7045                 :          0 : mcast_addr_pool_destroy(portid_t port_id)
    7046                 :            : {
    7047                 :            :         struct rte_port *port;
    7048                 :            : 
    7049                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
    7050                 :            :             port_id == (portid_t)RTE_PORT_ALL)
    7051                 :            :                 return -EINVAL;
    7052                 :          0 :         port = &ports[port_id];
    7053                 :            : 
    7054                 :          0 :         if (port->mc_addr_nb != 0) {
    7055                 :            :                 /* free the pool of multicast addresses. */
    7056                 :          0 :                 free(port->mc_addr_pool);
    7057                 :          0 :                 port->mc_addr_pool = NULL;
    7058                 :          0 :                 port->mc_addr_nb = 0;
    7059                 :            :         }
    7060                 :            :         return 0;
    7061                 :            : }
    7062                 :            : 
    7063                 :            : static int
    7064                 :          0 : eth_port_multicast_addr_list_set(portid_t port_id)
    7065                 :            : {
    7066                 :            :         struct rte_port *port;
    7067                 :            :         int diag;
    7068                 :            : 
    7069                 :          0 :         port = &ports[port_id];
    7070                 :          0 :         diag = rte_eth_dev_set_mc_addr_list(port_id, port->mc_addr_pool,
    7071                 :            :                                             port->mc_addr_nb);
    7072                 :          0 :         if (diag < 0)
    7073                 :          0 :                 fprintf(stderr,
    7074                 :            :                         "rte_eth_dev_set_mc_addr_list(port=%d, nb=%u) failed. diag=%d\n",
    7075                 :            :                         port_id, port->mc_addr_nb, diag);
    7076                 :            : 
    7077                 :          0 :         return diag;
    7078                 :            : }
    7079                 :            : 
    7080                 :            : void
    7081                 :          0 : mcast_addr_add(portid_t port_id, struct rte_ether_addr *mc_addr)
    7082                 :            : {
    7083                 :            :         struct rte_port *port;
    7084                 :            :         uint32_t i;
    7085                 :            : 
    7086                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    7087                 :            :                 return;
    7088                 :            : 
    7089                 :          0 :         port = &ports[port_id];
    7090                 :            : 
    7091                 :            :         /*
    7092                 :            :          * Check that the added multicast MAC address is not already recorded
    7093                 :            :          * in the pool of multicast addresses.
    7094                 :            :          */
    7095                 :          0 :         for (i = 0; i < port->mc_addr_nb; i++) {
    7096                 :          0 :                 if (rte_is_same_ether_addr(mc_addr, &port->mc_addr_pool[i])) {
    7097                 :          0 :                         fprintf(stderr,
    7098                 :            :                                 "multicast address already filtered by port\n");
    7099                 :          0 :                         return;
    7100                 :            :                 }
    7101                 :            :         }
    7102                 :            : 
    7103                 :          0 :         mcast_addr_pool_append(port, mc_addr);
    7104                 :          0 :         if (eth_port_multicast_addr_list_set(port_id) < 0)
    7105                 :            :                 /* Rollback on failure, remove the address from the pool */
    7106                 :          0 :                 mcast_addr_pool_remove(port, i);
    7107                 :            : }
    7108                 :            : 
    7109                 :            : void
    7110                 :          0 : mcast_addr_remove(portid_t port_id, struct rte_ether_addr *mc_addr)
    7111                 :            : {
    7112                 :            :         struct rte_port *port;
    7113                 :            :         uint32_t i;
    7114                 :            : 
    7115                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    7116                 :            :                 return;
    7117                 :            : 
    7118                 :          0 :         port = &ports[port_id];
    7119                 :            : 
    7120                 :            :         /*
    7121                 :            :          * Search the pool of multicast MAC addresses for the removed address.
    7122                 :            :          */
    7123                 :          0 :         for (i = 0; i < port->mc_addr_nb; i++) {
    7124                 :          0 :                 if (rte_is_same_ether_addr(mc_addr, &port->mc_addr_pool[i]))
    7125                 :            :                         break;
    7126                 :            :         }
    7127                 :          0 :         if (i == port->mc_addr_nb) {
    7128                 :          0 :                 fprintf(stderr, "multicast address not filtered by port %d\n",
    7129                 :            :                         port_id);
    7130                 :          0 :                 return;
    7131                 :            :         }
    7132                 :            : 
    7133                 :          0 :         mcast_addr_pool_remove(port, i);
    7134                 :          0 :         if (eth_port_multicast_addr_list_set(port_id) < 0)
    7135                 :            :                 /* Rollback on failure, add the address back into the pool */
    7136                 :          0 :                 mcast_addr_pool_append(port, mc_addr);
    7137                 :            : }
    7138                 :            : 
    7139                 :            : void
    7140                 :          0 : mcast_addr_flush(portid_t port_id)
    7141                 :            : {
    7142                 :            :         int ret;
    7143                 :            : 
    7144                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    7145                 :            :                 return;
    7146                 :            : 
    7147                 :          0 :         ret = rte_eth_dev_set_mc_addr_list(port_id, NULL, 0);
    7148                 :          0 :         if (ret != 0) {
    7149                 :          0 :                 fprintf(stderr,
    7150                 :            :                         "Failed to flush all multicast MAC addresses on port_id %u\n",
    7151                 :            :                         port_id);
    7152                 :          0 :                 return;
    7153                 :            :         }
    7154                 :          0 :         mcast_addr_pool_destroy(port_id);
    7155                 :            : }
    7156                 :            : 
    7157                 :            : void
    7158                 :          0 : port_dcb_info_display(portid_t port_id)
    7159                 :            : {
    7160                 :            :         struct rte_eth_dcb_info dcb_info;
    7161                 :            :         uint16_t i;
    7162                 :            :         int ret;
    7163                 :            :         static const char *border = "================";
    7164                 :            : 
    7165                 :          0 :         if (port_id_is_invalid(port_id, ENABLED_WARN))
    7166                 :          0 :                 return;
    7167                 :            : 
    7168                 :          0 :         ret = rte_eth_dev_get_dcb_info(port_id, &dcb_info);
    7169                 :          0 :         if (ret) {
    7170                 :          0 :                 fprintf(stderr, "\n Failed to get dcb infos on port %-2d\n",
    7171                 :            :                         port_id);
    7172                 :          0 :                 return;
    7173                 :            :         }
    7174                 :          0 :         printf("\n  %s DCB infos for port %-2d  %s\n", border, port_id, border);
    7175                 :          0 :         printf("  TC NUMBER: %d\n", dcb_info.nb_tcs);
    7176                 :            :         printf("\n  TC :        ");
    7177                 :          0 :         for (i = 0; i < dcb_info.nb_tcs; i++)
    7178                 :          0 :                 printf("\t%4d", i);
    7179                 :            :         printf("\n  Priority :  ");
    7180                 :          0 :         for (i = 0; i < dcb_info.nb_tcs; i++)
    7181                 :          0 :                 printf("\t%4d", dcb_info.prio_tc[i]);
    7182                 :            :         printf("\n  BW percent :");
    7183                 :          0 :         for (i = 0; i < dcb_info.nb_tcs; i++)
    7184                 :          0 :                 printf("\t%4d%%", dcb_info.tc_bws[i]);
    7185                 :            :         printf("\n  RXQ base :  ");
    7186                 :          0 :         for (i = 0; i < dcb_info.nb_tcs; i++)
    7187                 :          0 :                 printf("\t%4d", dcb_info.tc_queue.tc_rxq[0][i].base);
    7188                 :            :         printf("\n  RXQ number :");
    7189                 :          0 :         for (i = 0; i < dcb_info.nb_tcs; i++)
    7190                 :          0 :                 printf("\t%4d", dcb_info.tc_queue.tc_rxq[0][i].nb_queue);
    7191                 :            :         printf("\n  TXQ base :  ");
    7192                 :          0 :         for (i = 0; i < dcb_info.nb_tcs; i++)
    7193                 :          0 :                 printf("\t%4d", dcb_info.tc_queue.tc_txq[0][i].base);
    7194                 :            :         printf("\n  TXQ number :");
    7195                 :          0 :         for (i = 0; i < dcb_info.nb_tcs; i++)
    7196                 :          0 :                 printf("\t%4d", dcb_info.tc_queue.tc_txq[0][i].nb_queue);
    7197                 :            :         printf("\n");
    7198                 :            : }
    7199                 :            : 
    7200                 :            : uint8_t *
    7201                 :          0 : open_file(const char *file_path, uint32_t *size)
    7202                 :            : {
    7203                 :            :         int fd = open(file_path, O_RDONLY);
    7204                 :            :         off_t pkg_size;
    7205                 :            :         uint8_t *buf = NULL;
    7206                 :            :         int ret = 0;
    7207                 :            :         struct stat st_buf;
    7208                 :            : 
    7209                 :          0 :         if (size)
    7210                 :          0 :                 *size = 0;
    7211                 :            : 
    7212                 :          0 :         if (fd == -1) {
    7213                 :          0 :                 fprintf(stderr, "%s: Failed to open %s\n", __func__, file_path);
    7214                 :          0 :                 return buf;
    7215                 :            :         }
    7216                 :            : 
    7217                 :          0 :         if ((fstat(fd, &st_buf) != 0) || (!S_ISREG(st_buf.st_mode))) {
    7218                 :          0 :                 close(fd);
    7219                 :          0 :                 fprintf(stderr, "%s: File operations failed\n", __func__);
    7220                 :          0 :                 return buf;
    7221                 :            :         }
    7222                 :            : 
    7223                 :          0 :         pkg_size = st_buf.st_size;
    7224                 :          0 :         if (pkg_size < 0) {
    7225                 :          0 :                 close(fd);
    7226                 :          0 :                 fprintf(stderr, "%s: File operations failed\n", __func__);
    7227                 :          0 :                 return buf;
    7228                 :            :         }
    7229                 :            : 
    7230                 :          0 :         buf = (uint8_t *)malloc(pkg_size);
    7231                 :          0 :         if (!buf) {
    7232                 :          0 :                 close(fd);
    7233                 :          0 :                 fprintf(stderr, "%s: Failed to malloc memory\n", __func__);
    7234                 :          0 :                 return buf;
    7235                 :            :         }
    7236                 :            : 
    7237                 :          0 :         ret = read(fd, buf, pkg_size);
    7238                 :          0 :         if (ret < 0) {
    7239                 :          0 :                 close(fd);
    7240                 :          0 :                 fprintf(stderr, "%s: File read operation failed\n", __func__);
    7241                 :            :                 close_file(buf);
    7242                 :          0 :                 return NULL;
    7243                 :            :         }
    7244                 :            : 
    7245                 :          0 :         if (size)
    7246                 :          0 :                 *size = pkg_size;
    7247                 :            : 
    7248                 :          0 :         close(fd);
    7249                 :            : 
    7250                 :          0 :         return buf;
    7251                 :            : }
    7252                 :            : 
    7253                 :            : int
    7254                 :          0 : save_file(const char *file_path, uint8_t *buf, uint32_t size)
    7255                 :            : {
    7256                 :          0 :         FILE *fh = fopen(file_path, "wb");
    7257                 :            : 
    7258                 :          0 :         if (fh == NULL) {
    7259                 :          0 :                 fprintf(stderr, "%s: Failed to open %s\n", __func__, file_path);
    7260                 :          0 :                 return -1;
    7261                 :            :         }
    7262                 :            : 
    7263                 :          0 :         if (fwrite(buf, 1, size, fh) != size) {
    7264                 :          0 :                 fclose(fh);
    7265                 :          0 :                 fprintf(stderr, "%s: File write operation failed\n", __func__);
    7266                 :          0 :                 return -1;
    7267                 :            :         }
    7268                 :            : 
    7269                 :          0 :         fclose(fh);
    7270                 :            : 
    7271                 :          0 :         return 0;
    7272                 :            : }
    7273                 :            : 
    7274                 :            : int
    7275                 :          0 : close_file(uint8_t *buf)
    7276                 :            : {
    7277                 :          0 :         if (buf) {
    7278                 :          0 :                 free((void *)buf);
    7279                 :          0 :                 return 0;
    7280                 :            :         }
    7281                 :            : 
    7282                 :            :         return -1;
    7283                 :            : }
    7284                 :            : 
    7285                 :            : void
    7286                 :          0 : show_macs(portid_t port_id)
    7287                 :          0 : {
    7288                 :            :         char buf[RTE_ETHER_ADDR_FMT_SIZE];
    7289                 :            :         struct rte_eth_dev_info dev_info;
    7290                 :            :         int32_t i, rc, num_macs = 0;
    7291                 :            : 
    7292                 :          0 :         if (eth_dev_info_get_print_err(port_id, &dev_info))
    7293                 :          0 :                 return;
    7294                 :            : 
    7295                 :          0 :         struct rte_ether_addr addr[dev_info.max_mac_addrs];
    7296                 :          0 :         rc = rte_eth_macaddrs_get(port_id, addr, dev_info.max_mac_addrs);
    7297                 :          0 :         if (rc < 0)
    7298                 :            :                 return;
    7299                 :            : 
    7300                 :          0 :         for (i = 0; i < rc; i++) {
    7301                 :            : 
    7302                 :            :                 /* skip zero address */
    7303                 :          0 :                 if (rte_is_zero_ether_addr(&addr[i]))
    7304                 :          0 :                         continue;
    7305                 :            : 
    7306                 :          0 :                 num_macs++;
    7307                 :            :         }
    7308                 :            : 
    7309                 :            :         printf("Number of MAC address added: %d\n", num_macs);
    7310                 :            : 
    7311                 :          0 :         for (i = 0; i < rc; i++) {
    7312                 :            : 
    7313                 :            :                 /* skip zero address */
    7314                 :          0 :                 if (rte_is_zero_ether_addr(&addr[i]))
    7315                 :          0 :                         continue;
    7316                 :            : 
    7317                 :          0 :                 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, &addr[i]);
    7318                 :            :                 printf("  %s\n", buf);
    7319                 :            :         }
    7320                 :            : }
    7321                 :            : 
    7322                 :            : void
    7323                 :          0 : show_mcast_macs(portid_t port_id)
    7324                 :            : {
    7325                 :            :         char buf[RTE_ETHER_ADDR_FMT_SIZE];
    7326                 :            :         struct rte_ether_addr *addr;
    7327                 :            :         struct rte_port *port;
    7328                 :            :         uint32_t i;
    7329                 :            : 
    7330                 :          0 :         port = &ports[port_id];
    7331                 :            : 
    7332                 :          0 :         printf("Number of Multicast MAC address added: %d\n", port->mc_addr_nb);
    7333                 :            : 
    7334                 :          0 :         for (i = 0; i < port->mc_addr_nb; i++) {
    7335                 :          0 :                 addr = &port->mc_addr_pool[i];
    7336                 :            : 
    7337                 :          0 :                 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, addr);
    7338                 :            :                 printf("  %s\n", buf);
    7339                 :            :         }
    7340                 :          0 : }

Generated by: LCOV version 1.14