app/testpmd: port info prints dynamically mapped flow types
[dpdk.git] / app / test-pmd / config.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   Copyright 2013-2014 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include <stdarg.h>
36 #include <errno.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdint.h>
40 #include <inttypes.h>
41
42 #include <sys/queue.h>
43
44 #include <rte_common.h>
45 #include <rte_byteorder.h>
46 #include <rte_debug.h>
47 #include <rte_log.h>
48 #include <rte_memory.h>
49 #include <rte_memcpy.h>
50 #include <rte_memzone.h>
51 #include <rte_launch.h>
52 #include <rte_eal.h>
53 #include <rte_per_lcore.h>
54 #include <rte_lcore.h>
55 #include <rte_atomic.h>
56 #include <rte_branch_prediction.h>
57 #include <rte_mempool.h>
58 #include <rte_mbuf.h>
59 #include <rte_interrupts.h>
60 #include <rte_pci.h>
61 #include <rte_ether.h>
62 #include <rte_ethdev.h>
63 #include <rte_string_fns.h>
64 #include <rte_cycles.h>
65 #include <rte_flow.h>
66 #include <rte_errno.h>
67 #ifdef RTE_LIBRTE_IXGBE_PMD
68 #include <rte_pmd_ixgbe.h>
69 #endif
70 #ifdef RTE_LIBRTE_BNXT_PMD
71 #include <rte_pmd_bnxt.h>
72 #endif
73 #include <rte_gro.h>
74
75 #include "testpmd.h"
76
77 static char *flowtype_to_str(uint16_t flow_type);
78
79 static const struct {
80         enum tx_pkt_split split;
81         const char *name;
82 } tx_split_name[] = {
83         {
84                 .split = TX_PKT_SPLIT_OFF,
85                 .name = "off",
86         },
87         {
88                 .split = TX_PKT_SPLIT_ON,
89                 .name = "on",
90         },
91         {
92                 .split = TX_PKT_SPLIT_RND,
93                 .name = "rand",
94         },
95 };
96
97 struct rss_type_info {
98         char str[32];
99         uint64_t rss_type;
100 };
101
102 static const struct rss_type_info rss_type_table[] = {
103         { "ipv4", ETH_RSS_IPV4 },
104         { "ipv4-frag", ETH_RSS_FRAG_IPV4 },
105         { "ipv4-tcp", ETH_RSS_NONFRAG_IPV4_TCP },
106         { "ipv4-udp", ETH_RSS_NONFRAG_IPV4_UDP },
107         { "ipv4-sctp", ETH_RSS_NONFRAG_IPV4_SCTP },
108         { "ipv4-other", ETH_RSS_NONFRAG_IPV4_OTHER },
109         { "ipv6", ETH_RSS_IPV6 },
110         { "ipv6-frag", ETH_RSS_FRAG_IPV6 },
111         { "ipv6-tcp", ETH_RSS_NONFRAG_IPV6_TCP },
112         { "ipv6-udp", ETH_RSS_NONFRAG_IPV6_UDP },
113         { "ipv6-sctp", ETH_RSS_NONFRAG_IPV6_SCTP },
114         { "ipv6-other", ETH_RSS_NONFRAG_IPV6_OTHER },
115         { "l2-payload", ETH_RSS_L2_PAYLOAD },
116         { "ipv6-ex", ETH_RSS_IPV6_EX },
117         { "ipv6-tcp-ex", ETH_RSS_IPV6_TCP_EX },
118         { "ipv6-udp-ex", ETH_RSS_IPV6_UDP_EX },
119         { "port", ETH_RSS_PORT },
120         { "vxlan", ETH_RSS_VXLAN },
121         { "geneve", ETH_RSS_GENEVE },
122         { "nvgre", ETH_RSS_NVGRE },
123
124 };
125
126 static void
127 print_ethaddr(const char *name, struct ether_addr *eth_addr)
128 {
129         char buf[ETHER_ADDR_FMT_SIZE];
130         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
131         printf("%s%s", name, buf);
132 }
133
134 void
135 nic_stats_display(portid_t port_id)
136 {
137         static uint64_t prev_pkts_rx[RTE_MAX_ETHPORTS];
138         static uint64_t prev_pkts_tx[RTE_MAX_ETHPORTS];
139         static uint64_t prev_cycles[RTE_MAX_ETHPORTS];
140         uint64_t diff_pkts_rx, diff_pkts_tx, diff_cycles;
141         uint64_t mpps_rx, mpps_tx;
142         struct rte_eth_stats stats;
143         struct rte_port *port = &ports[port_id];
144         uint8_t i;
145         portid_t pid;
146
147         static const char *nic_stats_border = "########################";
148
149         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
150                 printf("Valid port range is [0");
151                 RTE_ETH_FOREACH_DEV(pid)
152                         printf(", %d", pid);
153                 printf("]\n");
154                 return;
155         }
156         rte_eth_stats_get(port_id, &stats);
157         printf("\n  %s NIC statistics for port %-2d %s\n",
158                nic_stats_border, port_id, nic_stats_border);
159
160         if ((!port->rx_queue_stats_mapping_enabled) && (!port->tx_queue_stats_mapping_enabled)) {
161                 printf("  RX-packets: %-10"PRIu64" RX-missed: %-10"PRIu64" RX-bytes:  "
162                        "%-"PRIu64"\n",
163                        stats.ipackets, stats.imissed, stats.ibytes);
164                 printf("  RX-errors: %-"PRIu64"\n", stats.ierrors);
165                 printf("  RX-nombuf:  %-10"PRIu64"\n",
166                        stats.rx_nombuf);
167                 printf("  TX-packets: %-10"PRIu64" TX-errors: %-10"PRIu64" TX-bytes:  "
168                        "%-"PRIu64"\n",
169                        stats.opackets, stats.oerrors, stats.obytes);
170         }
171         else {
172                 printf("  RX-packets:              %10"PRIu64"    RX-errors: %10"PRIu64
173                        "    RX-bytes: %10"PRIu64"\n",
174                        stats.ipackets, stats.ierrors, stats.ibytes);
175                 printf("  RX-errors:  %10"PRIu64"\n", stats.ierrors);
176                 printf("  RX-nombuf:               %10"PRIu64"\n",
177                        stats.rx_nombuf);
178                 printf("  TX-packets:              %10"PRIu64"    TX-errors: %10"PRIu64
179                        "    TX-bytes: %10"PRIu64"\n",
180                        stats.opackets, stats.oerrors, stats.obytes);
181         }
182
183         if (port->rx_queue_stats_mapping_enabled) {
184                 printf("\n");
185                 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
186                         printf("  Stats reg %2d RX-packets: %10"PRIu64
187                                "    RX-errors: %10"PRIu64
188                                "    RX-bytes: %10"PRIu64"\n",
189                                i, stats.q_ipackets[i], stats.q_errors[i], stats.q_ibytes[i]);
190                 }
191         }
192         if (port->tx_queue_stats_mapping_enabled) {
193                 printf("\n");
194                 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
195                         printf("  Stats reg %2d TX-packets: %10"PRIu64
196                                "                             TX-bytes: %10"PRIu64"\n",
197                                i, stats.q_opackets[i], stats.q_obytes[i]);
198                 }
199         }
200
201         diff_cycles = prev_cycles[port_id];
202         prev_cycles[port_id] = rte_rdtsc();
203         if (diff_cycles > 0)
204                 diff_cycles = prev_cycles[port_id] - diff_cycles;
205
206         diff_pkts_rx = (stats.ipackets > prev_pkts_rx[port_id]) ?
207                 (stats.ipackets - prev_pkts_rx[port_id]) : 0;
208         diff_pkts_tx = (stats.opackets > prev_pkts_tx[port_id]) ?
209                 (stats.opackets - prev_pkts_tx[port_id]) : 0;
210         prev_pkts_rx[port_id] = stats.ipackets;
211         prev_pkts_tx[port_id] = stats.opackets;
212         mpps_rx = diff_cycles > 0 ?
213                 diff_pkts_rx * rte_get_tsc_hz() / diff_cycles : 0;
214         mpps_tx = diff_cycles > 0 ?
215                 diff_pkts_tx * rte_get_tsc_hz() / diff_cycles : 0;
216         printf("\n  Throughput (since last show)\n");
217         printf("  Rx-pps: %12"PRIu64"\n  Tx-pps: %12"PRIu64"\n",
218                         mpps_rx, mpps_tx);
219
220         printf("  %s############################%s\n",
221                nic_stats_border, nic_stats_border);
222 }
223
224 void
225 nic_stats_clear(portid_t port_id)
226 {
227         portid_t pid;
228
229         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
230                 printf("Valid port range is [0");
231                 RTE_ETH_FOREACH_DEV(pid)
232                         printf(", %d", pid);
233                 printf("]\n");
234                 return;
235         }
236         rte_eth_stats_reset(port_id);
237         printf("\n  NIC statistics for port %d cleared\n", port_id);
238 }
239
240 void
241 nic_xstats_display(portid_t port_id)
242 {
243         struct rte_eth_xstat *xstats;
244         int cnt_xstats, idx_xstat;
245         struct rte_eth_xstat_name *xstats_names;
246
247         printf("###### NIC extended statistics for port %-2d\n", port_id);
248         if (!rte_eth_dev_is_valid_port(port_id)) {
249                 printf("Error: Invalid port number %i\n", port_id);
250                 return;
251         }
252
253         /* Get count */
254         cnt_xstats = rte_eth_xstats_get_names(port_id, NULL, 0);
255         if (cnt_xstats  < 0) {
256                 printf("Error: Cannot get count of xstats\n");
257                 return;
258         }
259
260         /* Get id-name lookup table */
261         xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * cnt_xstats);
262         if (xstats_names == NULL) {
263                 printf("Cannot allocate memory for xstats lookup\n");
264                 return;
265         }
266         if (cnt_xstats != rte_eth_xstats_get_names(
267                         port_id, xstats_names, cnt_xstats)) {
268                 printf("Error: Cannot get xstats lookup\n");
269                 free(xstats_names);
270                 return;
271         }
272
273         /* Get stats themselves */
274         xstats = malloc(sizeof(struct rte_eth_xstat) * cnt_xstats);
275         if (xstats == NULL) {
276                 printf("Cannot allocate memory for xstats\n");
277                 free(xstats_names);
278                 return;
279         }
280         if (cnt_xstats != rte_eth_xstats_get(port_id, xstats, cnt_xstats)) {
281                 printf("Error: Unable to get xstats\n");
282                 free(xstats_names);
283                 free(xstats);
284                 return;
285         }
286
287         /* Display xstats */
288         for (idx_xstat = 0; idx_xstat < cnt_xstats; idx_xstat++)
289                 printf("%s: %"PRIu64"\n",
290                         xstats_names[idx_xstat].name,
291                         xstats[idx_xstat].value);
292         free(xstats_names);
293         free(xstats);
294 }
295
296 void
297 nic_xstats_clear(portid_t port_id)
298 {
299         rte_eth_xstats_reset(port_id);
300 }
301
302 void
303 nic_stats_mapping_display(portid_t port_id)
304 {
305         struct rte_port *port = &ports[port_id];
306         uint16_t i;
307         portid_t pid;
308
309         static const char *nic_stats_mapping_border = "########################";
310
311         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
312                 printf("Valid port range is [0");
313                 RTE_ETH_FOREACH_DEV(pid)
314                         printf(", %d", pid);
315                 printf("]\n");
316                 return;
317         }
318
319         if ((!port->rx_queue_stats_mapping_enabled) && (!port->tx_queue_stats_mapping_enabled)) {
320                 printf("Port id %d - either does not support queue statistic mapping or"
321                        " no queue statistic mapping set\n", port_id);
322                 return;
323         }
324
325         printf("\n  %s NIC statistics mapping for port %-2d %s\n",
326                nic_stats_mapping_border, port_id, nic_stats_mapping_border);
327
328         if (port->rx_queue_stats_mapping_enabled) {
329                 for (i = 0; i < nb_rx_queue_stats_mappings; i++) {
330                         if (rx_queue_stats_mappings[i].port_id == port_id) {
331                                 printf("  RX-queue %2d mapped to Stats Reg %2d\n",
332                                        rx_queue_stats_mappings[i].queue_id,
333                                        rx_queue_stats_mappings[i].stats_counter_id);
334                         }
335                 }
336                 printf("\n");
337         }
338
339
340         if (port->tx_queue_stats_mapping_enabled) {
341                 for (i = 0; i < nb_tx_queue_stats_mappings; i++) {
342                         if (tx_queue_stats_mappings[i].port_id == port_id) {
343                                 printf("  TX-queue %2d mapped to Stats Reg %2d\n",
344                                        tx_queue_stats_mappings[i].queue_id,
345                                        tx_queue_stats_mappings[i].stats_counter_id);
346                         }
347                 }
348         }
349
350         printf("  %s####################################%s\n",
351                nic_stats_mapping_border, nic_stats_mapping_border);
352 }
353
354 void
355 rx_queue_infos_display(portid_t port_id, uint16_t queue_id)
356 {
357         struct rte_eth_rxq_info qinfo;
358         int32_t rc;
359         static const char *info_border = "*********************";
360
361         rc = rte_eth_rx_queue_info_get(port_id, queue_id, &qinfo);
362         if (rc != 0) {
363                 printf("Failed to retrieve information for port: %hhu, "
364                         "RX queue: %hu\nerror desc: %s(%d)\n",
365                         port_id, queue_id, strerror(-rc), rc);
366                 return;
367         }
368
369         printf("\n%s Infos for port %-2u, RX queue %-2u %s",
370                info_border, port_id, queue_id, info_border);
371
372         printf("\nMempool: %s", (qinfo.mp == NULL) ? "NULL" : qinfo.mp->name);
373         printf("\nRX prefetch threshold: %hhu", qinfo.conf.rx_thresh.pthresh);
374         printf("\nRX host threshold: %hhu", qinfo.conf.rx_thresh.hthresh);
375         printf("\nRX writeback threshold: %hhu", qinfo.conf.rx_thresh.wthresh);
376         printf("\nRX free threshold: %hu", qinfo.conf.rx_free_thresh);
377         printf("\nRX drop packets: %s",
378                 (qinfo.conf.rx_drop_en != 0) ? "on" : "off");
379         printf("\nRX deferred start: %s",
380                 (qinfo.conf.rx_deferred_start != 0) ? "on" : "off");
381         printf("\nRX scattered packets: %s",
382                 (qinfo.scattered_rx != 0) ? "on" : "off");
383         printf("\nNumber of RXDs: %hu", qinfo.nb_desc);
384         printf("\n");
385 }
386
387 void
388 tx_queue_infos_display(portid_t port_id, uint16_t queue_id)
389 {
390         struct rte_eth_txq_info qinfo;
391         int32_t rc;
392         static const char *info_border = "*********************";
393
394         rc = rte_eth_tx_queue_info_get(port_id, queue_id, &qinfo);
395         if (rc != 0) {
396                 printf("Failed to retrieve information for port: %hhu, "
397                         "TX queue: %hu\nerror desc: %s(%d)\n",
398                         port_id, queue_id, strerror(-rc), rc);
399                 return;
400         }
401
402         printf("\n%s Infos for port %-2u, TX queue %-2u %s",
403                info_border, port_id, queue_id, info_border);
404
405         printf("\nTX prefetch threshold: %hhu", qinfo.conf.tx_thresh.pthresh);
406         printf("\nTX host threshold: %hhu", qinfo.conf.tx_thresh.hthresh);
407         printf("\nTX writeback threshold: %hhu", qinfo.conf.tx_thresh.wthresh);
408         printf("\nTX RS threshold: %hu", qinfo.conf.tx_rs_thresh);
409         printf("\nTX free threshold: %hu", qinfo.conf.tx_free_thresh);
410         printf("\nTX flags: %#x", qinfo.conf.txq_flags);
411         printf("\nTX deferred start: %s",
412                 (qinfo.conf.tx_deferred_start != 0) ? "on" : "off");
413         printf("\nNumber of TXDs: %hu", qinfo.nb_desc);
414         printf("\n");
415 }
416
417 void
418 port_infos_display(portid_t port_id)
419 {
420         struct rte_port *port;
421         struct ether_addr mac_addr;
422         struct rte_eth_link link;
423         struct rte_eth_dev_info dev_info;
424         int vlan_offload;
425         struct rte_mempool * mp;
426         static const char *info_border = "*********************";
427         portid_t pid;
428         uint16_t mtu;
429
430         if (port_id_is_invalid(port_id, ENABLED_WARN)) {
431                 printf("Valid port range is [0");
432                 RTE_ETH_FOREACH_DEV(pid)
433                         printf(", %d", pid);
434                 printf("]\n");
435                 return;
436         }
437         port = &ports[port_id];
438         rte_eth_link_get_nowait(port_id, &link);
439         memset(&dev_info, 0, sizeof(dev_info));
440         rte_eth_dev_info_get(port_id, &dev_info);
441         printf("\n%s Infos for port %-2d %s\n",
442                info_border, port_id, info_border);
443         rte_eth_macaddr_get(port_id, &mac_addr);
444         print_ethaddr("MAC address: ", &mac_addr);
445         printf("\nDriver name: %s", dev_info.driver_name);
446         printf("\nConnect to socket: %u", port->socket_id);
447
448         if (port_numa[port_id] != NUMA_NO_CONFIG) {
449                 mp = mbuf_pool_find(port_numa[port_id]);
450                 if (mp)
451                         printf("\nmemory allocation on the socket: %d",
452                                                         port_numa[port_id]);
453         } else
454                 printf("\nmemory allocation on the socket: %u",port->socket_id);
455
456         printf("\nLink status: %s\n", (link.link_status) ? ("up") : ("down"));
457         printf("Link speed: %u Mbps\n", (unsigned) link.link_speed);
458         printf("Link duplex: %s\n", (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
459                ("full-duplex") : ("half-duplex"));
460
461         if (!rte_eth_dev_get_mtu(port_id, &mtu))
462                 printf("MTU: %u\n", mtu);
463
464         printf("Promiscuous mode: %s\n",
465                rte_eth_promiscuous_get(port_id) ? "enabled" : "disabled");
466         printf("Allmulticast mode: %s\n",
467                rte_eth_allmulticast_get(port_id) ? "enabled" : "disabled");
468         printf("Maximum number of MAC addresses: %u\n",
469                (unsigned int)(port->dev_info.max_mac_addrs));
470         printf("Maximum number of MAC addresses of hash filtering: %u\n",
471                (unsigned int)(port->dev_info.max_hash_mac_addrs));
472
473         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
474         if (vlan_offload >= 0){
475                 printf("VLAN offload: \n");
476                 if (vlan_offload & ETH_VLAN_STRIP_OFFLOAD)
477                         printf("  strip on \n");
478                 else
479                         printf("  strip off \n");
480
481                 if (vlan_offload & ETH_VLAN_FILTER_OFFLOAD)
482                         printf("  filter on \n");
483                 else
484                         printf("  filter off \n");
485
486                 if (vlan_offload & ETH_VLAN_EXTEND_OFFLOAD)
487                         printf("  qinq(extend) on \n");
488                 else
489                         printf("  qinq(extend) off \n");
490         }
491
492         if (dev_info.hash_key_size > 0)
493                 printf("Hash key size in bytes: %u\n", dev_info.hash_key_size);
494         if (dev_info.reta_size > 0)
495                 printf("Redirection table size: %u\n", dev_info.reta_size);
496         if (!dev_info.flow_type_rss_offloads)
497                 printf("No flow type is supported.\n");
498         else {
499                 uint16_t i;
500                 char *p;
501
502                 printf("Supported flow types:\n");
503                 for (i = RTE_ETH_FLOW_UNKNOWN + 1;
504                      i < sizeof(dev_info.flow_type_rss_offloads) * CHAR_BIT; i++) {
505                         if (!(dev_info.flow_type_rss_offloads & (1ULL << i)))
506                                 continue;
507                         p = flowtype_to_str(i);
508                         if (p)
509                                 printf("  %s\n", p);
510                         else
511                                 printf("  user defined %d\n", i);
512                 }
513         }
514
515         printf("Max possible RX queues: %u\n", dev_info.max_rx_queues);
516         printf("Max possible number of RXDs per queue: %hu\n",
517                 dev_info.rx_desc_lim.nb_max);
518         printf("Min possible number of RXDs per queue: %hu\n",
519                 dev_info.rx_desc_lim.nb_min);
520         printf("RXDs number alignment: %hu\n", dev_info.rx_desc_lim.nb_align);
521
522         printf("Max possible TX queues: %u\n", dev_info.max_tx_queues);
523         printf("Max possible number of TXDs per queue: %hu\n",
524                 dev_info.tx_desc_lim.nb_max);
525         printf("Min possible number of TXDs per queue: %hu\n",
526                 dev_info.tx_desc_lim.nb_min);
527         printf("TXDs number alignment: %hu\n", dev_info.tx_desc_lim.nb_align);
528 }
529
530 void
531 port_offload_cap_display(portid_t port_id)
532 {
533         struct rte_eth_dev *dev;
534         struct rte_eth_dev_info dev_info;
535         static const char *info_border = "************";
536
537         if (port_id_is_invalid(port_id, ENABLED_WARN))
538                 return;
539
540         dev = &rte_eth_devices[port_id];
541         rte_eth_dev_info_get(port_id, &dev_info);
542
543         printf("\n%s Port %d supported offload features: %s\n",
544                 info_border, port_id, info_border);
545
546         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_VLAN_STRIP) {
547                 printf("VLAN stripped:                 ");
548                 if (dev->data->dev_conf.rxmode.hw_vlan_strip)
549                         printf("on\n");
550                 else
551                         printf("off\n");
552         }
553
554         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_QINQ_STRIP) {
555                 printf("Double VLANs stripped:         ");
556                 if (dev->data->dev_conf.rxmode.hw_vlan_extend)
557                         printf("on\n");
558                 else
559                         printf("off\n");
560         }
561
562         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_IPV4_CKSUM) {
563                 printf("RX IPv4 checksum:              ");
564                 if (dev->data->dev_conf.rxmode.hw_ip_checksum)
565                         printf("on\n");
566                 else
567                         printf("off\n");
568         }
569
570         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_UDP_CKSUM) {
571                 printf("RX UDP checksum:               ");
572                 if (dev->data->dev_conf.rxmode.hw_ip_checksum)
573                         printf("on\n");
574                 else
575                         printf("off\n");
576         }
577
578         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_TCP_CKSUM) {
579                 printf("RX TCP checksum:               ");
580                 if (dev->data->dev_conf.rxmode.hw_ip_checksum)
581                         printf("on\n");
582                 else
583                         printf("off\n");
584         }
585
586         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM)
587                 printf("RX Outer IPv4 checksum:        on");
588
589         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_TCP_LRO) {
590                 printf("Large receive offload:         ");
591                 if (dev->data->dev_conf.rxmode.enable_lro)
592                         printf("on\n");
593                 else
594                         printf("off\n");
595         }
596
597         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_VLAN_INSERT) {
598                 printf("VLAN insert:                   ");
599                 if (ports[port_id].tx_ol_flags &
600                     TESTPMD_TX_OFFLOAD_INSERT_VLAN)
601                         printf("on\n");
602                 else
603                         printf("off\n");
604         }
605
606         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_QINQ_INSERT) {
607                 printf("Double VLANs insert:           ");
608                 if (ports[port_id].tx_ol_flags &
609                     TESTPMD_TX_OFFLOAD_INSERT_QINQ)
610                         printf("on\n");
611                 else
612                         printf("off\n");
613         }
614
615         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_IPV4_CKSUM) {
616                 printf("TX IPv4 checksum:              ");
617                 if (ports[port_id].tx_ol_flags & TESTPMD_TX_OFFLOAD_IP_CKSUM)
618                         printf("on\n");
619                 else
620                         printf("off\n");
621         }
622
623         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_UDP_CKSUM) {
624                 printf("TX UDP checksum:               ");
625                 if (ports[port_id].tx_ol_flags & TESTPMD_TX_OFFLOAD_UDP_CKSUM)
626                         printf("on\n");
627                 else
628                         printf("off\n");
629         }
630
631         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_TCP_CKSUM) {
632                 printf("TX TCP checksum:               ");
633                 if (ports[port_id].tx_ol_flags & TESTPMD_TX_OFFLOAD_TCP_CKSUM)
634                         printf("on\n");
635                 else
636                         printf("off\n");
637         }
638
639         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_SCTP_CKSUM) {
640                 printf("TX SCTP checksum:              ");
641                 if (ports[port_id].tx_ol_flags & TESTPMD_TX_OFFLOAD_SCTP_CKSUM)
642                         printf("on\n");
643                 else
644                         printf("off\n");
645         }
646
647         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM) {
648                 printf("TX Outer IPv4 checksum:        ");
649                 if (ports[port_id].tx_ol_flags &
650                     TESTPMD_TX_OFFLOAD_OUTER_IP_CKSUM)
651                         printf("on\n");
652                 else
653                         printf("off\n");
654         }
655
656         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_TCP_TSO) {
657                 printf("TX TCP segmentation:           ");
658                 if (ports[port_id].tso_segsz != 0)
659                         printf("on\n");
660                 else
661                         printf("off\n");
662         }
663
664         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_UDP_TSO) {
665                 printf("TX UDP segmentation:           ");
666                 if (ports[port_id].tso_segsz != 0)
667                         printf("on\n");
668                 else
669                         printf("off\n");
670         }
671
672         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_VXLAN_TNL_TSO) {
673                 printf("TSO for VXLAN tunnel packet:   ");
674                 if (ports[port_id].tunnel_tso_segsz)
675                         printf("on\n");
676                 else
677                         printf("off\n");
678         }
679
680         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_GRE_TNL_TSO) {
681                 printf("TSO for GRE tunnel packet:     ");
682                 if (ports[port_id].tunnel_tso_segsz)
683                         printf("on\n");
684                 else
685                         printf("off\n");
686         }
687
688         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_IPIP_TNL_TSO) {
689                 printf("TSO for IPIP tunnel packet:    ");
690                 if (ports[port_id].tunnel_tso_segsz)
691                         printf("on\n");
692                 else
693                         printf("off\n");
694         }
695
696         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_GENEVE_TNL_TSO) {
697                 printf("TSO for GENEVE tunnel packet:  ");
698                 if (ports[port_id].tunnel_tso_segsz)
699                         printf("on\n");
700                 else
701                         printf("off\n");
702         }
703
704 }
705
706 int
707 port_id_is_invalid(portid_t port_id, enum print_warning warning)
708 {
709         if (port_id == (portid_t)RTE_PORT_ALL)
710                 return 0;
711
712         if (rte_eth_dev_is_valid_port(port_id))
713                 return 0;
714
715         if (warning == ENABLED_WARN)
716                 printf("Invalid port %d\n", port_id);
717
718         return 1;
719 }
720
721 static int
722 vlan_id_is_invalid(uint16_t vlan_id)
723 {
724         if (vlan_id < 4096)
725                 return 0;
726         printf("Invalid vlan_id %d (must be < 4096)\n", vlan_id);
727         return 1;
728 }
729
730 static int
731 port_reg_off_is_invalid(portid_t port_id, uint32_t reg_off)
732 {
733         uint64_t pci_len;
734
735         if (reg_off & 0x3) {
736                 printf("Port register offset 0x%X not aligned on a 4-byte "
737                        "boundary\n",
738                        (unsigned)reg_off);
739                 return 1;
740         }
741         pci_len = ports[port_id].dev_info.pci_dev->mem_resource[0].len;
742         if (reg_off >= pci_len) {
743                 printf("Port %d: register offset %u (0x%X) out of port PCI "
744                        "resource (length=%"PRIu64")\n",
745                        port_id, (unsigned)reg_off, (unsigned)reg_off,  pci_len);
746                 return 1;
747         }
748         return 0;
749 }
750
751 static int
752 reg_bit_pos_is_invalid(uint8_t bit_pos)
753 {
754         if (bit_pos <= 31)
755                 return 0;
756         printf("Invalid bit position %d (must be <= 31)\n", bit_pos);
757         return 1;
758 }
759
760 #define display_port_and_reg_off(port_id, reg_off) \
761         printf("port %d PCI register at offset 0x%X: ", (port_id), (reg_off))
762
763 static inline void
764 display_port_reg_value(portid_t port_id, uint32_t reg_off, uint32_t reg_v)
765 {
766         display_port_and_reg_off(port_id, (unsigned)reg_off);
767         printf("0x%08X (%u)\n", (unsigned)reg_v, (unsigned)reg_v);
768 }
769
770 void
771 port_reg_bit_display(portid_t port_id, uint32_t reg_off, uint8_t bit_x)
772 {
773         uint32_t reg_v;
774
775
776         if (port_id_is_invalid(port_id, ENABLED_WARN))
777                 return;
778         if (port_reg_off_is_invalid(port_id, reg_off))
779                 return;
780         if (reg_bit_pos_is_invalid(bit_x))
781                 return;
782         reg_v = port_id_pci_reg_read(port_id, reg_off);
783         display_port_and_reg_off(port_id, (unsigned)reg_off);
784         printf("bit %d=%d\n", bit_x, (int) ((reg_v & (1 << bit_x)) >> bit_x));
785 }
786
787 void
788 port_reg_bit_field_display(portid_t port_id, uint32_t reg_off,
789                            uint8_t bit1_pos, uint8_t bit2_pos)
790 {
791         uint32_t reg_v;
792         uint8_t  l_bit;
793         uint8_t  h_bit;
794
795         if (port_id_is_invalid(port_id, ENABLED_WARN))
796                 return;
797         if (port_reg_off_is_invalid(port_id, reg_off))
798                 return;
799         if (reg_bit_pos_is_invalid(bit1_pos))
800                 return;
801         if (reg_bit_pos_is_invalid(bit2_pos))
802                 return;
803         if (bit1_pos > bit2_pos)
804                 l_bit = bit2_pos, h_bit = bit1_pos;
805         else
806                 l_bit = bit1_pos, h_bit = bit2_pos;
807
808         reg_v = port_id_pci_reg_read(port_id, reg_off);
809         reg_v >>= l_bit;
810         if (h_bit < 31)
811                 reg_v &= ((1 << (h_bit - l_bit + 1)) - 1);
812         display_port_and_reg_off(port_id, (unsigned)reg_off);
813         printf("bits[%d, %d]=0x%0*X (%u)\n", l_bit, h_bit,
814                ((h_bit - l_bit) / 4) + 1, (unsigned)reg_v, (unsigned)reg_v);
815 }
816
817 void
818 port_reg_display(portid_t port_id, uint32_t reg_off)
819 {
820         uint32_t reg_v;
821
822         if (port_id_is_invalid(port_id, ENABLED_WARN))
823                 return;
824         if (port_reg_off_is_invalid(port_id, reg_off))
825                 return;
826         reg_v = port_id_pci_reg_read(port_id, reg_off);
827         display_port_reg_value(port_id, reg_off, reg_v);
828 }
829
830 void
831 port_reg_bit_set(portid_t port_id, uint32_t reg_off, uint8_t bit_pos,
832                  uint8_t bit_v)
833 {
834         uint32_t reg_v;
835
836         if (port_id_is_invalid(port_id, ENABLED_WARN))
837                 return;
838         if (port_reg_off_is_invalid(port_id, reg_off))
839                 return;
840         if (reg_bit_pos_is_invalid(bit_pos))
841                 return;
842         if (bit_v > 1) {
843                 printf("Invalid bit value %d (must be 0 or 1)\n", (int) bit_v);
844                 return;
845         }
846         reg_v = port_id_pci_reg_read(port_id, reg_off);
847         if (bit_v == 0)
848                 reg_v &= ~(1 << bit_pos);
849         else
850                 reg_v |= (1 << bit_pos);
851         port_id_pci_reg_write(port_id, reg_off, reg_v);
852         display_port_reg_value(port_id, reg_off, reg_v);
853 }
854
855 void
856 port_reg_bit_field_set(portid_t port_id, uint32_t reg_off,
857                        uint8_t bit1_pos, uint8_t bit2_pos, uint32_t value)
858 {
859         uint32_t max_v;
860         uint32_t reg_v;
861         uint8_t  l_bit;
862         uint8_t  h_bit;
863
864         if (port_id_is_invalid(port_id, ENABLED_WARN))
865                 return;
866         if (port_reg_off_is_invalid(port_id, reg_off))
867                 return;
868         if (reg_bit_pos_is_invalid(bit1_pos))
869                 return;
870         if (reg_bit_pos_is_invalid(bit2_pos))
871                 return;
872         if (bit1_pos > bit2_pos)
873                 l_bit = bit2_pos, h_bit = bit1_pos;
874         else
875                 l_bit = bit1_pos, h_bit = bit2_pos;
876
877         if ((h_bit - l_bit) < 31)
878                 max_v = (1 << (h_bit - l_bit + 1)) - 1;
879         else
880                 max_v = 0xFFFFFFFF;
881
882         if (value > max_v) {
883                 printf("Invalid value %u (0x%x) must be < %u (0x%x)\n",
884                                 (unsigned)value, (unsigned)value,
885                                 (unsigned)max_v, (unsigned)max_v);
886                 return;
887         }
888         reg_v = port_id_pci_reg_read(port_id, reg_off);
889         reg_v &= ~(max_v << l_bit); /* Keep unchanged bits */
890         reg_v |= (value << l_bit); /* Set changed bits */
891         port_id_pci_reg_write(port_id, reg_off, reg_v);
892         display_port_reg_value(port_id, reg_off, reg_v);
893 }
894
895 void
896 port_reg_set(portid_t port_id, uint32_t reg_off, uint32_t reg_v)
897 {
898         if (port_id_is_invalid(port_id, ENABLED_WARN))
899                 return;
900         if (port_reg_off_is_invalid(port_id, reg_off))
901                 return;
902         port_id_pci_reg_write(port_id, reg_off, reg_v);
903         display_port_reg_value(port_id, reg_off, reg_v);
904 }
905
906 void
907 port_mtu_set(portid_t port_id, uint16_t mtu)
908 {
909         int diag;
910
911         if (port_id_is_invalid(port_id, ENABLED_WARN))
912                 return;
913         diag = rte_eth_dev_set_mtu(port_id, mtu);
914         if (diag == 0)
915                 return;
916         printf("Set MTU failed. diag=%d\n", diag);
917 }
918
919 /* Generic flow management functions. */
920
921 /** Generate flow_item[] entry. */
922 #define MK_FLOW_ITEM(t, s) \
923         [RTE_FLOW_ITEM_TYPE_ ## t] = { \
924                 .name = # t, \
925                 .size = s, \
926         }
927
928 /** Information about known flow pattern items. */
929 static const struct {
930         const char *name;
931         size_t size;
932 } flow_item[] = {
933         MK_FLOW_ITEM(END, 0),
934         MK_FLOW_ITEM(VOID, 0),
935         MK_FLOW_ITEM(INVERT, 0),
936         MK_FLOW_ITEM(ANY, sizeof(struct rte_flow_item_any)),
937         MK_FLOW_ITEM(PF, 0),
938         MK_FLOW_ITEM(VF, sizeof(struct rte_flow_item_vf)),
939         MK_FLOW_ITEM(PORT, sizeof(struct rte_flow_item_port)),
940         MK_FLOW_ITEM(RAW, sizeof(struct rte_flow_item_raw)), /* +pattern[] */
941         MK_FLOW_ITEM(ETH, sizeof(struct rte_flow_item_eth)),
942         MK_FLOW_ITEM(VLAN, sizeof(struct rte_flow_item_vlan)),
943         MK_FLOW_ITEM(IPV4, sizeof(struct rte_flow_item_ipv4)),
944         MK_FLOW_ITEM(IPV6, sizeof(struct rte_flow_item_ipv6)),
945         MK_FLOW_ITEM(ICMP, sizeof(struct rte_flow_item_icmp)),
946         MK_FLOW_ITEM(UDP, sizeof(struct rte_flow_item_udp)),
947         MK_FLOW_ITEM(TCP, sizeof(struct rte_flow_item_tcp)),
948         MK_FLOW_ITEM(SCTP, sizeof(struct rte_flow_item_sctp)),
949         MK_FLOW_ITEM(VXLAN, sizeof(struct rte_flow_item_vxlan)),
950         MK_FLOW_ITEM(E_TAG, sizeof(struct rte_flow_item_e_tag)),
951         MK_FLOW_ITEM(NVGRE, sizeof(struct rte_flow_item_nvgre)),
952         MK_FLOW_ITEM(MPLS, sizeof(struct rte_flow_item_mpls)),
953         MK_FLOW_ITEM(GRE, sizeof(struct rte_flow_item_gre)),
954         MK_FLOW_ITEM(FUZZY, sizeof(struct rte_flow_item_fuzzy)),
955 };
956
957 /** Compute storage space needed by item specification. */
958 static void
959 flow_item_spec_size(const struct rte_flow_item *item,
960                     size_t *size, size_t *pad)
961 {
962         if (!item->spec) {
963                 *size = 0;
964                 goto empty;
965         }
966         switch (item->type) {
967                 union {
968                         const struct rte_flow_item_raw *raw;
969                 } spec;
970
971         case RTE_FLOW_ITEM_TYPE_RAW:
972                 spec.raw = item->spec;
973                 *size = offsetof(struct rte_flow_item_raw, pattern) +
974                         spec.raw->length * sizeof(*spec.raw->pattern);
975                 break;
976         default:
977                 *size = flow_item[item->type].size;
978                 break;
979         }
980 empty:
981         *pad = RTE_ALIGN_CEIL(*size, sizeof(double)) - *size;
982 }
983
984 /** Generate flow_action[] entry. */
985 #define MK_FLOW_ACTION(t, s) \
986         [RTE_FLOW_ACTION_TYPE_ ## t] = { \
987                 .name = # t, \
988                 .size = s, \
989         }
990
991 /** Information about known flow actions. */
992 static const struct {
993         const char *name;
994         size_t size;
995 } flow_action[] = {
996         MK_FLOW_ACTION(END, 0),
997         MK_FLOW_ACTION(VOID, 0),
998         MK_FLOW_ACTION(PASSTHRU, 0),
999         MK_FLOW_ACTION(MARK, sizeof(struct rte_flow_action_mark)),
1000         MK_FLOW_ACTION(FLAG, 0),
1001         MK_FLOW_ACTION(QUEUE, sizeof(struct rte_flow_action_queue)),
1002         MK_FLOW_ACTION(DROP, 0),
1003         MK_FLOW_ACTION(COUNT, 0),
1004         MK_FLOW_ACTION(DUP, sizeof(struct rte_flow_action_dup)),
1005         MK_FLOW_ACTION(RSS, sizeof(struct rte_flow_action_rss)), /* +queue[] */
1006         MK_FLOW_ACTION(PF, 0),
1007         MK_FLOW_ACTION(VF, sizeof(struct rte_flow_action_vf)),
1008 };
1009
1010 /** Compute storage space needed by action configuration. */
1011 static void
1012 flow_action_conf_size(const struct rte_flow_action *action,
1013                       size_t *size, size_t *pad)
1014 {
1015         if (!action->conf) {
1016                 *size = 0;
1017                 goto empty;
1018         }
1019         switch (action->type) {
1020                 union {
1021                         const struct rte_flow_action_rss *rss;
1022                 } conf;
1023
1024         case RTE_FLOW_ACTION_TYPE_RSS:
1025                 conf.rss = action->conf;
1026                 *size = offsetof(struct rte_flow_action_rss, queue) +
1027                         conf.rss->num * sizeof(*conf.rss->queue);
1028                 break;
1029         default:
1030                 *size = flow_action[action->type].size;
1031                 break;
1032         }
1033 empty:
1034         *pad = RTE_ALIGN_CEIL(*size, sizeof(double)) - *size;
1035 }
1036
1037 /** Generate a port_flow entry from attributes/pattern/actions. */
1038 static struct port_flow *
1039 port_flow_new(const struct rte_flow_attr *attr,
1040               const struct rte_flow_item *pattern,
1041               const struct rte_flow_action *actions)
1042 {
1043         const struct rte_flow_item *item;
1044         const struct rte_flow_action *action;
1045         struct port_flow *pf = NULL;
1046         size_t tmp;
1047         size_t pad;
1048         size_t off1 = 0;
1049         size_t off2 = 0;
1050         int err = ENOTSUP;
1051
1052 store:
1053         item = pattern;
1054         if (pf)
1055                 pf->pattern = (void *)&pf->data[off1];
1056         do {
1057                 struct rte_flow_item *dst = NULL;
1058
1059                 if ((unsigned int)item->type >= RTE_DIM(flow_item) ||
1060                     !flow_item[item->type].name)
1061                         goto notsup;
1062                 if (pf)
1063                         dst = memcpy(pf->data + off1, item, sizeof(*item));
1064                 off1 += sizeof(*item);
1065                 flow_item_spec_size(item, &tmp, &pad);
1066                 if (item->spec) {
1067                         if (pf)
1068                                 dst->spec = memcpy(pf->data + off2,
1069                                                    item->spec, tmp);
1070                         off2 += tmp + pad;
1071                 }
1072                 if (item->last) {
1073                         if (pf)
1074                                 dst->last = memcpy(pf->data + off2,
1075                                                    item->last, tmp);
1076                         off2 += tmp + pad;
1077                 }
1078                 if (item->mask) {
1079                         if (pf)
1080                                 dst->mask = memcpy(pf->data + off2,
1081                                                    item->mask, tmp);
1082                         off2 += tmp + pad;
1083                 }
1084                 off2 = RTE_ALIGN_CEIL(off2, sizeof(double));
1085         } while ((item++)->type != RTE_FLOW_ITEM_TYPE_END);
1086         off1 = RTE_ALIGN_CEIL(off1, sizeof(double));
1087         action = actions;
1088         if (pf)
1089                 pf->actions = (void *)&pf->data[off1];
1090         do {
1091                 struct rte_flow_action *dst = NULL;
1092
1093                 if ((unsigned int)action->type >= RTE_DIM(flow_action) ||
1094                     !flow_action[action->type].name)
1095                         goto notsup;
1096                 if (pf)
1097                         dst = memcpy(pf->data + off1, action, sizeof(*action));
1098                 off1 += sizeof(*action);
1099                 flow_action_conf_size(action, &tmp, &pad);
1100                 if (action->conf) {
1101                         if (pf)
1102                                 dst->conf = memcpy(pf->data + off2,
1103                                                    action->conf, tmp);
1104                         off2 += tmp + pad;
1105                 }
1106                 off2 = RTE_ALIGN_CEIL(off2, sizeof(double));
1107         } while ((action++)->type != RTE_FLOW_ACTION_TYPE_END);
1108         if (pf != NULL)
1109                 return pf;
1110         off1 = RTE_ALIGN_CEIL(off1, sizeof(double));
1111         tmp = RTE_ALIGN_CEIL(offsetof(struct port_flow, data), sizeof(double));
1112         pf = calloc(1, tmp + off1 + off2);
1113         if (pf == NULL)
1114                 err = errno;
1115         else {
1116                 *pf = (const struct port_flow){
1117                         .size = tmp + off1 + off2,
1118                         .attr = *attr,
1119                 };
1120                 tmp -= offsetof(struct port_flow, data);
1121                 off2 = tmp + off1;
1122                 off1 = tmp;
1123                 goto store;
1124         }
1125 notsup:
1126         rte_errno = err;
1127         return NULL;
1128 }
1129
1130 /** Print a message out of a flow error. */
1131 static int
1132 port_flow_complain(struct rte_flow_error *error)
1133 {
1134         static const char *const errstrlist[] = {
1135                 [RTE_FLOW_ERROR_TYPE_NONE] = "no error",
1136                 [RTE_FLOW_ERROR_TYPE_UNSPECIFIED] = "cause unspecified",
1137                 [RTE_FLOW_ERROR_TYPE_HANDLE] = "flow rule (handle)",
1138                 [RTE_FLOW_ERROR_TYPE_ATTR_GROUP] = "group field",
1139                 [RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY] = "priority field",
1140                 [RTE_FLOW_ERROR_TYPE_ATTR_INGRESS] = "ingress field",
1141                 [RTE_FLOW_ERROR_TYPE_ATTR_EGRESS] = "egress field",
1142                 [RTE_FLOW_ERROR_TYPE_ATTR] = "attributes structure",
1143                 [RTE_FLOW_ERROR_TYPE_ITEM_NUM] = "pattern length",
1144                 [RTE_FLOW_ERROR_TYPE_ITEM] = "specific pattern item",
1145                 [RTE_FLOW_ERROR_TYPE_ACTION_NUM] = "number of actions",
1146                 [RTE_FLOW_ERROR_TYPE_ACTION] = "specific action",
1147         };
1148         const char *errstr;
1149         char buf[32];
1150         int err = rte_errno;
1151
1152         if ((unsigned int)error->type >= RTE_DIM(errstrlist) ||
1153             !errstrlist[error->type])
1154                 errstr = "unknown type";
1155         else
1156                 errstr = errstrlist[error->type];
1157         printf("Caught error type %d (%s): %s%s\n",
1158                error->type, errstr,
1159                error->cause ? (snprintf(buf, sizeof(buf), "cause: %p, ",
1160                                         error->cause), buf) : "",
1161                error->message ? error->message : "(no stated reason)");
1162         return -err;
1163 }
1164
1165 /** Validate flow rule. */
1166 int
1167 port_flow_validate(portid_t port_id,
1168                    const struct rte_flow_attr *attr,
1169                    const struct rte_flow_item *pattern,
1170                    const struct rte_flow_action *actions)
1171 {
1172         struct rte_flow_error error;
1173
1174         /* Poisoning to make sure PMDs update it in case of error. */
1175         memset(&error, 0x11, sizeof(error));
1176         if (rte_flow_validate(port_id, attr, pattern, actions, &error))
1177                 return port_flow_complain(&error);
1178         printf("Flow rule validated\n");
1179         return 0;
1180 }
1181
1182 /** Create flow rule. */
1183 int
1184 port_flow_create(portid_t port_id,
1185                  const struct rte_flow_attr *attr,
1186                  const struct rte_flow_item *pattern,
1187                  const struct rte_flow_action *actions)
1188 {
1189         struct rte_flow *flow;
1190         struct rte_port *port;
1191         struct port_flow *pf;
1192         uint32_t id;
1193         struct rte_flow_error error;
1194
1195         /* Poisoning to make sure PMDs update it in case of error. */
1196         memset(&error, 0x22, sizeof(error));
1197         flow = rte_flow_create(port_id, attr, pattern, actions, &error);
1198         if (!flow)
1199                 return port_flow_complain(&error);
1200         port = &ports[port_id];
1201         if (port->flow_list) {
1202                 if (port->flow_list->id == UINT32_MAX) {
1203                         printf("Highest rule ID is already assigned, delete"
1204                                " it first");
1205                         rte_flow_destroy(port_id, flow, NULL);
1206                         return -ENOMEM;
1207                 }
1208                 id = port->flow_list->id + 1;
1209         } else
1210                 id = 0;
1211         pf = port_flow_new(attr, pattern, actions);
1212         if (!pf) {
1213                 int err = rte_errno;
1214
1215                 printf("Cannot allocate flow: %s\n", rte_strerror(err));
1216                 rte_flow_destroy(port_id, flow, NULL);
1217                 return -err;
1218         }
1219         pf->next = port->flow_list;
1220         pf->id = id;
1221         pf->flow = flow;
1222         port->flow_list = pf;
1223         printf("Flow rule #%u created\n", pf->id);
1224         return 0;
1225 }
1226
1227 /** Destroy a number of flow rules. */
1228 int
1229 port_flow_destroy(portid_t port_id, uint32_t n, const uint32_t *rule)
1230 {
1231         struct rte_port *port;
1232         struct port_flow **tmp;
1233         uint32_t c = 0;
1234         int ret = 0;
1235
1236         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1237             port_id == (portid_t)RTE_PORT_ALL)
1238                 return -EINVAL;
1239         port = &ports[port_id];
1240         tmp = &port->flow_list;
1241         while (*tmp) {
1242                 uint32_t i;
1243
1244                 for (i = 0; i != n; ++i) {
1245                         struct rte_flow_error error;
1246                         struct port_flow *pf = *tmp;
1247
1248                         if (rule[i] != pf->id)
1249                                 continue;
1250                         /*
1251                          * Poisoning to make sure PMDs update it in case
1252                          * of error.
1253                          */
1254                         memset(&error, 0x33, sizeof(error));
1255                         if (rte_flow_destroy(port_id, pf->flow, &error)) {
1256                                 ret = port_flow_complain(&error);
1257                                 continue;
1258                         }
1259                         printf("Flow rule #%u destroyed\n", pf->id);
1260                         *tmp = pf->next;
1261                         free(pf);
1262                         break;
1263                 }
1264                 if (i == n)
1265                         tmp = &(*tmp)->next;
1266                 ++c;
1267         }
1268         return ret;
1269 }
1270
1271 /** Remove all flow rules. */
1272 int
1273 port_flow_flush(portid_t port_id)
1274 {
1275         struct rte_flow_error error;
1276         struct rte_port *port;
1277         int ret = 0;
1278
1279         /* Poisoning to make sure PMDs update it in case of error. */
1280         memset(&error, 0x44, sizeof(error));
1281         if (rte_flow_flush(port_id, &error)) {
1282                 ret = port_flow_complain(&error);
1283                 if (port_id_is_invalid(port_id, DISABLED_WARN) ||
1284                     port_id == (portid_t)RTE_PORT_ALL)
1285                         return ret;
1286         }
1287         port = &ports[port_id];
1288         while (port->flow_list) {
1289                 struct port_flow *pf = port->flow_list->next;
1290
1291                 free(port->flow_list);
1292                 port->flow_list = pf;
1293         }
1294         return ret;
1295 }
1296
1297 /** Query a flow rule. */
1298 int
1299 port_flow_query(portid_t port_id, uint32_t rule,
1300                 enum rte_flow_action_type action)
1301 {
1302         struct rte_flow_error error;
1303         struct rte_port *port;
1304         struct port_flow *pf;
1305         const char *name;
1306         union {
1307                 struct rte_flow_query_count count;
1308         } query;
1309
1310         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1311             port_id == (portid_t)RTE_PORT_ALL)
1312                 return -EINVAL;
1313         port = &ports[port_id];
1314         for (pf = port->flow_list; pf; pf = pf->next)
1315                 if (pf->id == rule)
1316                         break;
1317         if (!pf) {
1318                 printf("Flow rule #%u not found\n", rule);
1319                 return -ENOENT;
1320         }
1321         if ((unsigned int)action >= RTE_DIM(flow_action) ||
1322             !flow_action[action].name)
1323                 name = "unknown";
1324         else
1325                 name = flow_action[action].name;
1326         switch (action) {
1327         case RTE_FLOW_ACTION_TYPE_COUNT:
1328                 break;
1329         default:
1330                 printf("Cannot query action type %d (%s)\n", action, name);
1331                 return -ENOTSUP;
1332         }
1333         /* Poisoning to make sure PMDs update it in case of error. */
1334         memset(&error, 0x55, sizeof(error));
1335         memset(&query, 0, sizeof(query));
1336         if (rte_flow_query(port_id, pf->flow, action, &query, &error))
1337                 return port_flow_complain(&error);
1338         switch (action) {
1339         case RTE_FLOW_ACTION_TYPE_COUNT:
1340                 printf("%s:\n"
1341                        " hits_set: %u\n"
1342                        " bytes_set: %u\n"
1343                        " hits: %" PRIu64 "\n"
1344                        " bytes: %" PRIu64 "\n",
1345                        name,
1346                        query.count.hits_set,
1347                        query.count.bytes_set,
1348                        query.count.hits,
1349                        query.count.bytes);
1350                 break;
1351         default:
1352                 printf("Cannot display result for action type %d (%s)\n",
1353                        action, name);
1354                 break;
1355         }
1356         return 0;
1357 }
1358
1359 /** List flow rules. */
1360 void
1361 port_flow_list(portid_t port_id, uint32_t n, const uint32_t group[n])
1362 {
1363         struct rte_port *port;
1364         struct port_flow *pf;
1365         struct port_flow *list = NULL;
1366         uint32_t i;
1367
1368         if (port_id_is_invalid(port_id, ENABLED_WARN) ||
1369             port_id == (portid_t)RTE_PORT_ALL)
1370                 return;
1371         port = &ports[port_id];
1372         if (!port->flow_list)
1373                 return;
1374         /* Sort flows by group, priority and ID. */
1375         for (pf = port->flow_list; pf != NULL; pf = pf->next) {
1376                 struct port_flow **tmp;
1377
1378                 if (n) {
1379                         /* Filter out unwanted groups. */
1380                         for (i = 0; i != n; ++i)
1381                                 if (pf->attr.group == group[i])
1382                                         break;
1383                         if (i == n)
1384                                 continue;
1385                 }
1386                 tmp = &list;
1387                 while (*tmp &&
1388                        (pf->attr.group > (*tmp)->attr.group ||
1389                         (pf->attr.group == (*tmp)->attr.group &&
1390                          pf->attr.priority > (*tmp)->attr.priority) ||
1391                         (pf->attr.group == (*tmp)->attr.group &&
1392                          pf->attr.priority == (*tmp)->attr.priority &&
1393                          pf->id > (*tmp)->id)))
1394                         tmp = &(*tmp)->tmp;
1395                 pf->tmp = *tmp;
1396                 *tmp = pf;
1397         }
1398         printf("ID\tGroup\tPrio\tAttr\tRule\n");
1399         for (pf = list; pf != NULL; pf = pf->tmp) {
1400                 const struct rte_flow_item *item = pf->pattern;
1401                 const struct rte_flow_action *action = pf->actions;
1402
1403                 printf("%" PRIu32 "\t%" PRIu32 "\t%" PRIu32 "\t%c%c\t",
1404                        pf->id,
1405                        pf->attr.group,
1406                        pf->attr.priority,
1407                        pf->attr.ingress ? 'i' : '-',
1408                        pf->attr.egress ? 'e' : '-');
1409                 while (item->type != RTE_FLOW_ITEM_TYPE_END) {
1410                         if (item->type != RTE_FLOW_ITEM_TYPE_VOID)
1411                                 printf("%s ", flow_item[item->type].name);
1412                         ++item;
1413                 }
1414                 printf("=>");
1415                 while (action->type != RTE_FLOW_ACTION_TYPE_END) {
1416                         if (action->type != RTE_FLOW_ACTION_TYPE_VOID)
1417                                 printf(" %s", flow_action[action->type].name);
1418                         ++action;
1419                 }
1420                 printf("\n");
1421         }
1422 }
1423
1424 /** Restrict ingress traffic to the defined flow rules. */
1425 int
1426 port_flow_isolate(portid_t port_id, int set)
1427 {
1428         struct rte_flow_error error;
1429
1430         /* Poisoning to make sure PMDs update it in case of error. */
1431         memset(&error, 0x66, sizeof(error));
1432         if (rte_flow_isolate(port_id, set, &error))
1433                 return port_flow_complain(&error);
1434         printf("Ingress traffic on port %u is %s to the defined flow rules\n",
1435                port_id,
1436                set ? "now restricted" : "not restricted anymore");
1437         return 0;
1438 }
1439
1440 /*
1441  * RX/TX ring descriptors display functions.
1442  */
1443 int
1444 rx_queue_id_is_invalid(queueid_t rxq_id)
1445 {
1446         if (rxq_id < nb_rxq)
1447                 return 0;
1448         printf("Invalid RX queue %d (must be < nb_rxq=%d)\n", rxq_id, nb_rxq);
1449         return 1;
1450 }
1451
1452 int
1453 tx_queue_id_is_invalid(queueid_t txq_id)
1454 {
1455         if (txq_id < nb_txq)
1456                 return 0;
1457         printf("Invalid TX queue %d (must be < nb_rxq=%d)\n", txq_id, nb_txq);
1458         return 1;
1459 }
1460
1461 static int
1462 rx_desc_id_is_invalid(uint16_t rxdesc_id)
1463 {
1464         if (rxdesc_id < nb_rxd)
1465                 return 0;
1466         printf("Invalid RX descriptor %d (must be < nb_rxd=%d)\n",
1467                rxdesc_id, nb_rxd);
1468         return 1;
1469 }
1470
1471 static int
1472 tx_desc_id_is_invalid(uint16_t txdesc_id)
1473 {
1474         if (txdesc_id < nb_txd)
1475                 return 0;
1476         printf("Invalid TX descriptor %d (must be < nb_txd=%d)\n",
1477                txdesc_id, nb_txd);
1478         return 1;
1479 }
1480
1481 static const struct rte_memzone *
1482 ring_dma_zone_lookup(const char *ring_name, uint8_t port_id, uint16_t q_id)
1483 {
1484         char mz_name[RTE_MEMZONE_NAMESIZE];
1485         const struct rte_memzone *mz;
1486
1487         snprintf(mz_name, sizeof(mz_name), "%s_%s_%d_%d",
1488                  ports[port_id].dev_info.driver_name, ring_name, port_id, q_id);
1489         mz = rte_memzone_lookup(mz_name);
1490         if (mz == NULL)
1491                 printf("%s ring memory zoneof (port %d, queue %d) not"
1492                        "found (zone name = %s\n",
1493                        ring_name, port_id, q_id, mz_name);
1494         return mz;
1495 }
1496
1497 union igb_ring_dword {
1498         uint64_t dword;
1499         struct {
1500 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
1501                 uint32_t lo;
1502                 uint32_t hi;
1503 #else
1504                 uint32_t hi;
1505                 uint32_t lo;
1506 #endif
1507         } words;
1508 };
1509
1510 struct igb_ring_desc_32_bytes {
1511         union igb_ring_dword lo_dword;
1512         union igb_ring_dword hi_dword;
1513         union igb_ring_dword resv1;
1514         union igb_ring_dword resv2;
1515 };
1516
1517 struct igb_ring_desc_16_bytes {
1518         union igb_ring_dword lo_dword;
1519         union igb_ring_dword hi_dword;
1520 };
1521
1522 static void
1523 ring_rxd_display_dword(union igb_ring_dword dword)
1524 {
1525         printf("    0x%08X - 0x%08X\n", (unsigned)dword.words.lo,
1526                                         (unsigned)dword.words.hi);
1527 }
1528
1529 static void
1530 ring_rx_descriptor_display(const struct rte_memzone *ring_mz,
1531 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
1532                            uint8_t port_id,
1533 #else
1534                            __rte_unused uint8_t port_id,
1535 #endif
1536                            uint16_t desc_id)
1537 {
1538         struct igb_ring_desc_16_bytes *ring =
1539                 (struct igb_ring_desc_16_bytes *)ring_mz->addr;
1540 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC
1541         struct rte_eth_dev_info dev_info;
1542
1543         memset(&dev_info, 0, sizeof(dev_info));
1544         rte_eth_dev_info_get(port_id, &dev_info);
1545         if (strstr(dev_info.driver_name, "i40e") != NULL) {
1546                 /* 32 bytes RX descriptor, i40e only */
1547                 struct igb_ring_desc_32_bytes *ring =
1548                         (struct igb_ring_desc_32_bytes *)ring_mz->addr;
1549                 ring[desc_id].lo_dword.dword =
1550                         rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
1551                 ring_rxd_display_dword(ring[desc_id].lo_dword);
1552                 ring[desc_id].hi_dword.dword =
1553                         rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
1554                 ring_rxd_display_dword(ring[desc_id].hi_dword);
1555                 ring[desc_id].resv1.dword =
1556                         rte_le_to_cpu_64(ring[desc_id].resv1.dword);
1557                 ring_rxd_display_dword(ring[desc_id].resv1);
1558                 ring[desc_id].resv2.dword =
1559                         rte_le_to_cpu_64(ring[desc_id].resv2.dword);
1560                 ring_rxd_display_dword(ring[desc_id].resv2);
1561
1562                 return;
1563         }
1564 #endif
1565         /* 16 bytes RX descriptor */
1566         ring[desc_id].lo_dword.dword =
1567                 rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
1568         ring_rxd_display_dword(ring[desc_id].lo_dword);
1569         ring[desc_id].hi_dword.dword =
1570                 rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
1571         ring_rxd_display_dword(ring[desc_id].hi_dword);
1572 }
1573
1574 static void
1575 ring_tx_descriptor_display(const struct rte_memzone *ring_mz, uint16_t desc_id)
1576 {
1577         struct igb_ring_desc_16_bytes *ring;
1578         struct igb_ring_desc_16_bytes txd;
1579
1580         ring = (struct igb_ring_desc_16_bytes *)ring_mz->addr;
1581         txd.lo_dword.dword = rte_le_to_cpu_64(ring[desc_id].lo_dword.dword);
1582         txd.hi_dword.dword = rte_le_to_cpu_64(ring[desc_id].hi_dword.dword);
1583         printf("    0x%08X - 0x%08X / 0x%08X - 0x%08X\n",
1584                         (unsigned)txd.lo_dword.words.lo,
1585                         (unsigned)txd.lo_dword.words.hi,
1586                         (unsigned)txd.hi_dword.words.lo,
1587                         (unsigned)txd.hi_dword.words.hi);
1588 }
1589
1590 void
1591 rx_ring_desc_display(portid_t port_id, queueid_t rxq_id, uint16_t rxd_id)
1592 {
1593         const struct rte_memzone *rx_mz;
1594
1595         if (port_id_is_invalid(port_id, ENABLED_WARN))
1596                 return;
1597         if (rx_queue_id_is_invalid(rxq_id))
1598                 return;
1599         if (rx_desc_id_is_invalid(rxd_id))
1600                 return;
1601         rx_mz = ring_dma_zone_lookup("rx_ring", port_id, rxq_id);
1602         if (rx_mz == NULL)
1603                 return;
1604         ring_rx_descriptor_display(rx_mz, port_id, rxd_id);
1605 }
1606
1607 void
1608 tx_ring_desc_display(portid_t port_id, queueid_t txq_id, uint16_t txd_id)
1609 {
1610         const struct rte_memzone *tx_mz;
1611
1612         if (port_id_is_invalid(port_id, ENABLED_WARN))
1613                 return;
1614         if (tx_queue_id_is_invalid(txq_id))
1615                 return;
1616         if (tx_desc_id_is_invalid(txd_id))
1617                 return;
1618         tx_mz = ring_dma_zone_lookup("tx_ring", port_id, txq_id);
1619         if (tx_mz == NULL)
1620                 return;
1621         ring_tx_descriptor_display(tx_mz, txd_id);
1622 }
1623
1624 void
1625 fwd_lcores_config_display(void)
1626 {
1627         lcoreid_t lc_id;
1628
1629         printf("List of forwarding lcores:");
1630         for (lc_id = 0; lc_id < nb_cfg_lcores; lc_id++)
1631                 printf(" %2u", fwd_lcores_cpuids[lc_id]);
1632         printf("\n");
1633 }
1634 void
1635 rxtx_config_display(void)
1636 {
1637         printf("  %s packet forwarding%s - CRC stripping %s - "
1638                "packets/burst=%d\n", cur_fwd_eng->fwd_mode_name,
1639                retry_enabled == 0 ? "" : " with retry",
1640                rx_mode.hw_strip_crc ? "enabled" : "disabled",
1641                nb_pkt_per_burst);
1642
1643         if (cur_fwd_eng == &tx_only_engine || cur_fwd_eng == &flow_gen_engine)
1644                 printf("  packet len=%u - nb packet segments=%d\n",
1645                                 (unsigned)tx_pkt_length, (int) tx_pkt_nb_segs);
1646
1647         struct rte_eth_rxconf *rx_conf = &ports[0].rx_conf;
1648         struct rte_eth_txconf *tx_conf = &ports[0].tx_conf;
1649
1650         printf("  nb forwarding cores=%d - nb forwarding ports=%d\n",
1651                nb_fwd_lcores, nb_fwd_ports);
1652         printf("  RX queues=%d - RX desc=%d - RX free threshold=%d\n",
1653                nb_rxq, nb_rxd, rx_conf->rx_free_thresh);
1654         printf("  RX threshold registers: pthresh=%d hthresh=%d wthresh=%d\n",
1655                rx_conf->rx_thresh.pthresh, rx_conf->rx_thresh.hthresh,
1656                rx_conf->rx_thresh.wthresh);
1657         printf("  TX queues=%d - TX desc=%d - TX free threshold=%d\n",
1658                nb_txq, nb_txd, tx_conf->tx_free_thresh);
1659         printf("  TX threshold registers: pthresh=%d hthresh=%d wthresh=%d\n",
1660                tx_conf->tx_thresh.pthresh, tx_conf->tx_thresh.hthresh,
1661                tx_conf->tx_thresh.wthresh);
1662         printf("  TX RS bit threshold=%d - TXQ flags=0x%"PRIx32"\n",
1663                tx_conf->tx_rs_thresh, tx_conf->txq_flags);
1664 }
1665
1666 void
1667 port_rss_reta_info(portid_t port_id,
1668                    struct rte_eth_rss_reta_entry64 *reta_conf,
1669                    uint16_t nb_entries)
1670 {
1671         uint16_t i, idx, shift;
1672         int ret;
1673
1674         if (port_id_is_invalid(port_id, ENABLED_WARN))
1675                 return;
1676
1677         ret = rte_eth_dev_rss_reta_query(port_id, reta_conf, nb_entries);
1678         if (ret != 0) {
1679                 printf("Failed to get RSS RETA info, return code = %d\n", ret);
1680                 return;
1681         }
1682
1683         for (i = 0; i < nb_entries; i++) {
1684                 idx = i / RTE_RETA_GROUP_SIZE;
1685                 shift = i % RTE_RETA_GROUP_SIZE;
1686                 if (!(reta_conf[idx].mask & (1ULL << shift)))
1687                         continue;
1688                 printf("RSS RETA configuration: hash index=%u, queue=%u\n",
1689                                         i, reta_conf[idx].reta[shift]);
1690         }
1691 }
1692
1693 /*
1694  * Displays the RSS hash functions of a port, and, optionaly, the RSS hash
1695  * key of the port.
1696  */
1697 void
1698 port_rss_hash_conf_show(portid_t port_id, char rss_info[], int show_rss_key)
1699 {
1700         struct rte_eth_rss_conf rss_conf;
1701         uint8_t rss_key[RSS_HASH_KEY_LENGTH];
1702         uint64_t rss_hf;
1703         uint8_t i;
1704         int diag;
1705         struct rte_eth_dev_info dev_info;
1706         uint8_t hash_key_size;
1707
1708         if (port_id_is_invalid(port_id, ENABLED_WARN))
1709                 return;
1710
1711         memset(&dev_info, 0, sizeof(dev_info));
1712         rte_eth_dev_info_get(port_id, &dev_info);
1713         if (dev_info.hash_key_size > 0 &&
1714                         dev_info.hash_key_size <= sizeof(rss_key))
1715                 hash_key_size = dev_info.hash_key_size;
1716         else {
1717                 printf("dev_info did not provide a valid hash key size\n");
1718                 return;
1719         }
1720
1721         rss_conf.rss_hf = 0;
1722         for (i = 0; i < RTE_DIM(rss_type_table); i++) {
1723                 if (!strcmp(rss_info, rss_type_table[i].str))
1724                         rss_conf.rss_hf = rss_type_table[i].rss_type;
1725         }
1726
1727         /* Get RSS hash key if asked to display it */
1728         rss_conf.rss_key = (show_rss_key) ? rss_key : NULL;
1729         rss_conf.rss_key_len = hash_key_size;
1730         diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf);
1731         if (diag != 0) {
1732                 switch (diag) {
1733                 case -ENODEV:
1734                         printf("port index %d invalid\n", port_id);
1735                         break;
1736                 case -ENOTSUP:
1737                         printf("operation not supported by device\n");
1738                         break;
1739                 default:
1740                         printf("operation failed - diag=%d\n", diag);
1741                         break;
1742                 }
1743                 return;
1744         }
1745         rss_hf = rss_conf.rss_hf;
1746         if (rss_hf == 0) {
1747                 printf("RSS disabled\n");
1748                 return;
1749         }
1750         printf("RSS functions:\n ");
1751         for (i = 0; i < RTE_DIM(rss_type_table); i++) {
1752                 if (rss_hf & rss_type_table[i].rss_type)
1753                         printf("%s ", rss_type_table[i].str);
1754         }
1755         printf("\n");
1756         if (!show_rss_key)
1757                 return;
1758         printf("RSS key:\n");
1759         for (i = 0; i < hash_key_size; i++)
1760                 printf("%02X", rss_key[i]);
1761         printf("\n");
1762 }
1763
1764 void
1765 port_rss_hash_key_update(portid_t port_id, char rss_type[], uint8_t *hash_key,
1766                          uint hash_key_len)
1767 {
1768         struct rte_eth_rss_conf rss_conf;
1769         int diag;
1770         unsigned int i;
1771
1772         rss_conf.rss_key = NULL;
1773         rss_conf.rss_key_len = hash_key_len;
1774         rss_conf.rss_hf = 0;
1775         for (i = 0; i < RTE_DIM(rss_type_table); i++) {
1776                 if (!strcmp(rss_type_table[i].str, rss_type))
1777                         rss_conf.rss_hf = rss_type_table[i].rss_type;
1778         }
1779         diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf);
1780         if (diag == 0) {
1781                 rss_conf.rss_key = hash_key;
1782                 diag = rte_eth_dev_rss_hash_update(port_id, &rss_conf);
1783         }
1784         if (diag == 0)
1785                 return;
1786
1787         switch (diag) {
1788         case -ENODEV:
1789                 printf("port index %d invalid\n", port_id);
1790                 break;
1791         case -ENOTSUP:
1792                 printf("operation not supported by device\n");
1793                 break;
1794         default:
1795                 printf("operation failed - diag=%d\n", diag);
1796                 break;
1797         }
1798 }
1799
1800 /*
1801  * Setup forwarding configuration for each logical core.
1802  */
1803 static void
1804 setup_fwd_config_of_each_lcore(struct fwd_config *cfg)
1805 {
1806         streamid_t nb_fs_per_lcore;
1807         streamid_t nb_fs;
1808         streamid_t sm_id;
1809         lcoreid_t  nb_extra;
1810         lcoreid_t  nb_fc;
1811         lcoreid_t  nb_lc;
1812         lcoreid_t  lc_id;
1813
1814         nb_fs = cfg->nb_fwd_streams;
1815         nb_fc = cfg->nb_fwd_lcores;
1816         if (nb_fs <= nb_fc) {
1817                 nb_fs_per_lcore = 1;
1818                 nb_extra = 0;
1819         } else {
1820                 nb_fs_per_lcore = (streamid_t) (nb_fs / nb_fc);
1821                 nb_extra = (lcoreid_t) (nb_fs % nb_fc);
1822         }
1823
1824         nb_lc = (lcoreid_t) (nb_fc - nb_extra);
1825         sm_id = 0;
1826         for (lc_id = 0; lc_id < nb_lc; lc_id++) {
1827                 fwd_lcores[lc_id]->stream_idx = sm_id;
1828                 fwd_lcores[lc_id]->stream_nb = nb_fs_per_lcore;
1829                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
1830         }
1831
1832         /*
1833          * Assign extra remaining streams, if any.
1834          */
1835         nb_fs_per_lcore = (streamid_t) (nb_fs_per_lcore + 1);
1836         for (lc_id = 0; lc_id < nb_extra; lc_id++) {
1837                 fwd_lcores[nb_lc + lc_id]->stream_idx = sm_id;
1838                 fwd_lcores[nb_lc + lc_id]->stream_nb = nb_fs_per_lcore;
1839                 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore);
1840         }
1841 }
1842
1843 static void
1844 simple_fwd_config_setup(void)
1845 {
1846         portid_t i;
1847         portid_t j;
1848         portid_t inc = 2;
1849
1850         if (port_topology == PORT_TOPOLOGY_CHAINED ||
1851             port_topology == PORT_TOPOLOGY_LOOP) {
1852                 inc = 1;
1853         } else if (nb_fwd_ports % 2) {
1854                 printf("\nWarning! Cannot handle an odd number of ports "
1855                        "with the current port topology. Configuration "
1856                        "must be changed to have an even number of ports, "
1857                        "or relaunch application with "
1858                        "--port-topology=chained\n\n");
1859         }
1860
1861         cur_fwd_config.nb_fwd_ports = (portid_t) nb_fwd_ports;
1862         cur_fwd_config.nb_fwd_streams =
1863                 (streamid_t) cur_fwd_config.nb_fwd_ports;
1864
1865         /* reinitialize forwarding streams */
1866         init_fwd_streams();
1867
1868         /*
1869          * In the simple forwarding test, the number of forwarding cores
1870          * must be lower or equal to the number of forwarding ports.
1871          */
1872         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
1873         if (cur_fwd_config.nb_fwd_lcores > cur_fwd_config.nb_fwd_ports)
1874                 cur_fwd_config.nb_fwd_lcores =
1875                         (lcoreid_t) cur_fwd_config.nb_fwd_ports;
1876         setup_fwd_config_of_each_lcore(&cur_fwd_config);
1877
1878         for (i = 0; i < cur_fwd_config.nb_fwd_ports; i = (portid_t) (i + inc)) {
1879                 if (port_topology != PORT_TOPOLOGY_LOOP)
1880                         j = (portid_t) ((i + 1) % cur_fwd_config.nb_fwd_ports);
1881                 else
1882                         j = i;
1883                 fwd_streams[i]->rx_port   = fwd_ports_ids[i];
1884                 fwd_streams[i]->rx_queue  = 0;
1885                 fwd_streams[i]->tx_port   = fwd_ports_ids[j];
1886                 fwd_streams[i]->tx_queue  = 0;
1887                 fwd_streams[i]->peer_addr = j;
1888                 fwd_streams[i]->retry_enabled = retry_enabled;
1889
1890                 if (port_topology == PORT_TOPOLOGY_PAIRED) {
1891                         fwd_streams[j]->rx_port   = fwd_ports_ids[j];
1892                         fwd_streams[j]->rx_queue  = 0;
1893                         fwd_streams[j]->tx_port   = fwd_ports_ids[i];
1894                         fwd_streams[j]->tx_queue  = 0;
1895                         fwd_streams[j]->peer_addr = i;
1896                         fwd_streams[j]->retry_enabled = retry_enabled;
1897                 }
1898         }
1899 }
1900
1901 /**
1902  * For the RSS forwarding test all streams distributed over lcores. Each stream
1903  * being composed of a RX queue to poll on a RX port for input messages,
1904  * associated with a TX queue of a TX port where to send forwarded packets.
1905  * All packets received on the RX queue of index "RxQj" of the RX port "RxPi"
1906  * are sent on the TX queue "TxQl" of the TX port "TxPk" according to the two
1907  * following rules:
1908  *    - TxPk = (RxPi + 1) if RxPi is even, (RxPi - 1) if RxPi is odd
1909  *    - TxQl = RxQj
1910  */
1911 static void
1912 rss_fwd_config_setup(void)
1913 {
1914         portid_t   rxp;
1915         portid_t   txp;
1916         queueid_t  rxq;
1917         queueid_t  nb_q;
1918         streamid_t  sm_id;
1919
1920         nb_q = nb_rxq;
1921         if (nb_q > nb_txq)
1922                 nb_q = nb_txq;
1923         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
1924         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
1925         cur_fwd_config.nb_fwd_streams =
1926                 (streamid_t) (nb_q * cur_fwd_config.nb_fwd_ports);
1927
1928         if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores)
1929                 cur_fwd_config.nb_fwd_lcores =
1930                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
1931
1932         /* reinitialize forwarding streams */
1933         init_fwd_streams();
1934
1935         setup_fwd_config_of_each_lcore(&cur_fwd_config);
1936         rxp = 0; rxq = 0;
1937         for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) {
1938                 struct fwd_stream *fs;
1939
1940                 fs = fwd_streams[sm_id];
1941
1942                 if ((rxp & 0x1) == 0)
1943                         txp = (portid_t) (rxp + 1);
1944                 else
1945                         txp = (portid_t) (rxp - 1);
1946                 /*
1947                  * if we are in loopback, simply send stuff out through the
1948                  * ingress port
1949                  */
1950                 if (port_topology == PORT_TOPOLOGY_LOOP)
1951                         txp = rxp;
1952
1953                 fs->rx_port = fwd_ports_ids[rxp];
1954                 fs->rx_queue = rxq;
1955                 fs->tx_port = fwd_ports_ids[txp];
1956                 fs->tx_queue = rxq;
1957                 fs->peer_addr = fs->tx_port;
1958                 fs->retry_enabled = retry_enabled;
1959                 rxq = (queueid_t) (rxq + 1);
1960                 if (rxq < nb_q)
1961                         continue;
1962                 /*
1963                  * rxq == nb_q
1964                  * Restart from RX queue 0 on next RX port
1965                  */
1966                 rxq = 0;
1967                 if (numa_support && (nb_fwd_ports <= (nb_ports >> 1)))
1968                         rxp = (portid_t)
1969                                 (rxp + ((nb_ports >> 1) / nb_fwd_ports));
1970                 else
1971                         rxp = (portid_t) (rxp + 1);
1972         }
1973 }
1974
1975 /**
1976  * For the DCB forwarding test, each core is assigned on each traffic class.
1977  *
1978  * Each core is assigned a multi-stream, each stream being composed of
1979  * a RX queue to poll on a RX port for input messages, associated with
1980  * a TX queue of a TX port where to send forwarded packets. All RX and
1981  * TX queues are mapping to the same traffic class.
1982  * If VMDQ and DCB co-exist, each traffic class on different POOLs share
1983  * the same core
1984  */
1985 static void
1986 dcb_fwd_config_setup(void)
1987 {
1988         struct rte_eth_dcb_info rxp_dcb_info, txp_dcb_info;
1989         portid_t txp, rxp = 0;
1990         queueid_t txq, rxq = 0;
1991         lcoreid_t  lc_id;
1992         uint16_t nb_rx_queue, nb_tx_queue;
1993         uint16_t i, j, k, sm_id = 0;
1994         uint8_t tc = 0;
1995
1996         cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
1997         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
1998         cur_fwd_config.nb_fwd_streams =
1999                 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
2000
2001         /* reinitialize forwarding streams */
2002         init_fwd_streams();
2003         sm_id = 0;
2004         txp = 1;
2005         /* get the dcb info on the first RX and TX ports */
2006         (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[rxp], &rxp_dcb_info);
2007         (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[txp], &txp_dcb_info);
2008
2009         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
2010                 fwd_lcores[lc_id]->stream_nb = 0;
2011                 fwd_lcores[lc_id]->stream_idx = sm_id;
2012                 for (i = 0; i < ETH_MAX_VMDQ_POOL; i++) {
2013                         /* if the nb_queue is zero, means this tc is
2014                          * not enabled on the POOL
2015                          */
2016                         if (rxp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue == 0)
2017                                 break;
2018                         k = fwd_lcores[lc_id]->stream_nb +
2019                                 fwd_lcores[lc_id]->stream_idx;
2020                         rxq = rxp_dcb_info.tc_queue.tc_rxq[i][tc].base;
2021                         txq = txp_dcb_info.tc_queue.tc_txq[i][tc].base;
2022                         nb_rx_queue = txp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue;
2023                         nb_tx_queue = txp_dcb_info.tc_queue.tc_txq[i][tc].nb_queue;
2024                         for (j = 0; j < nb_rx_queue; j++) {
2025                                 struct fwd_stream *fs;
2026
2027                                 fs = fwd_streams[k + j];
2028                                 fs->rx_port = fwd_ports_ids[rxp];
2029                                 fs->rx_queue = rxq + j;
2030                                 fs->tx_port = fwd_ports_ids[txp];
2031                                 fs->tx_queue = txq + j % nb_tx_queue;
2032                                 fs->peer_addr = fs->tx_port;
2033                                 fs->retry_enabled = retry_enabled;
2034                         }
2035                         fwd_lcores[lc_id]->stream_nb +=
2036                                 rxp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue;
2037                 }
2038                 sm_id = (streamid_t) (sm_id + fwd_lcores[lc_id]->stream_nb);
2039
2040                 tc++;
2041                 if (tc < rxp_dcb_info.nb_tcs)
2042                         continue;
2043                 /* Restart from TC 0 on next RX port */
2044                 tc = 0;
2045                 if (numa_support && (nb_fwd_ports <= (nb_ports >> 1)))
2046                         rxp = (portid_t)
2047                                 (rxp + ((nb_ports >> 1) / nb_fwd_ports));
2048                 else
2049                         rxp++;
2050                 if (rxp >= nb_fwd_ports)
2051                         return;
2052                 /* get the dcb information on next RX and TX ports */
2053                 if ((rxp & 0x1) == 0)
2054                         txp = (portid_t) (rxp + 1);
2055                 else
2056                         txp = (portid_t) (rxp - 1);
2057                 rte_eth_dev_get_dcb_info(fwd_ports_ids[rxp], &rxp_dcb_info);
2058                 rte_eth_dev_get_dcb_info(fwd_ports_ids[txp], &txp_dcb_info);
2059         }
2060 }
2061
2062 static void
2063 icmp_echo_config_setup(void)
2064 {
2065         portid_t  rxp;
2066         queueid_t rxq;
2067         lcoreid_t lc_id;
2068         uint16_t  sm_id;
2069
2070         if ((nb_txq * nb_fwd_ports) < nb_fwd_lcores)
2071                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t)
2072                         (nb_txq * nb_fwd_ports);
2073         else
2074                 cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores;
2075         cur_fwd_config.nb_fwd_ports = nb_fwd_ports;
2076         cur_fwd_config.nb_fwd_streams =
2077                 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports);
2078         if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores)
2079                 cur_fwd_config.nb_fwd_lcores =
2080                         (lcoreid_t)cur_fwd_config.nb_fwd_streams;
2081         if (verbose_level > 0) {
2082                 printf("%s fwd_cores=%d fwd_ports=%d fwd_streams=%d\n",
2083                        __FUNCTION__,
2084                        cur_fwd_config.nb_fwd_lcores,
2085                        cur_fwd_config.nb_fwd_ports,
2086                        cur_fwd_config.nb_fwd_streams);
2087         }
2088
2089         /* reinitialize forwarding streams */
2090         init_fwd_streams();
2091         setup_fwd_config_of_each_lcore(&cur_fwd_config);
2092         rxp = 0; rxq = 0;
2093         for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) {
2094                 if (verbose_level > 0)
2095                         printf("  core=%d: \n", lc_id);
2096                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
2097                         struct fwd_stream *fs;
2098                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
2099                         fs->rx_port = fwd_ports_ids[rxp];
2100                         fs->rx_queue = rxq;
2101                         fs->tx_port = fs->rx_port;
2102                         fs->tx_queue = rxq;
2103                         fs->peer_addr = fs->tx_port;
2104                         fs->retry_enabled = retry_enabled;
2105                         if (verbose_level > 0)
2106                                 printf("  stream=%d port=%d rxq=%d txq=%d\n",
2107                                        sm_id, fs->rx_port, fs->rx_queue,
2108                                        fs->tx_queue);
2109                         rxq = (queueid_t) (rxq + 1);
2110                         if (rxq == nb_rxq) {
2111                                 rxq = 0;
2112                                 rxp = (portid_t) (rxp + 1);
2113                         }
2114                 }
2115         }
2116 }
2117
2118 void
2119 fwd_config_setup(void)
2120 {
2121         cur_fwd_config.fwd_eng = cur_fwd_eng;
2122         if (strcmp(cur_fwd_eng->fwd_mode_name, "icmpecho") == 0) {
2123                 icmp_echo_config_setup();
2124                 return;
2125         }
2126         if ((nb_rxq > 1) && (nb_txq > 1)){
2127                 if (dcb_config)
2128                         dcb_fwd_config_setup();
2129                 else
2130                         rss_fwd_config_setup();
2131         }
2132         else
2133                 simple_fwd_config_setup();
2134 }
2135
2136 void
2137 pkt_fwd_config_display(struct fwd_config *cfg)
2138 {
2139         struct fwd_stream *fs;
2140         lcoreid_t  lc_id;
2141         streamid_t sm_id;
2142
2143         printf("%s packet forwarding%s - ports=%d - cores=%d - streams=%d - "
2144                 "NUMA support %s, MP over anonymous pages %s\n",
2145                 cfg->fwd_eng->fwd_mode_name,
2146                 retry_enabled == 0 ? "" : " with retry",
2147                 cfg->nb_fwd_ports, cfg->nb_fwd_lcores, cfg->nb_fwd_streams,
2148                 numa_support == 1 ? "enabled" : "disabled",
2149                 mp_anon != 0 ? "enabled" : "disabled");
2150
2151         if (retry_enabled)
2152                 printf("TX retry num: %u, delay between TX retries: %uus\n",
2153                         burst_tx_retry_num, burst_tx_delay_time);
2154         for (lc_id = 0; lc_id < cfg->nb_fwd_lcores; lc_id++) {
2155                 printf("Logical Core %u (socket %u) forwards packets on "
2156                        "%d streams:",
2157                        fwd_lcores_cpuids[lc_id],
2158                        rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]),
2159                        fwd_lcores[lc_id]->stream_nb);
2160                 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) {
2161                         fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id];
2162                         printf("\n  RX P=%d/Q=%d (socket %u) -> TX "
2163                                "P=%d/Q=%d (socket %u) ",
2164                                fs->rx_port, fs->rx_queue,
2165                                ports[fs->rx_port].socket_id,
2166                                fs->tx_port, fs->tx_queue,
2167                                ports[fs->tx_port].socket_id);
2168                         print_ethaddr("peer=",
2169                                       &peer_eth_addrs[fs->peer_addr]);
2170                 }
2171                 printf("\n");
2172         }
2173         printf("\n");
2174 }
2175
2176 int
2177 set_fwd_lcores_list(unsigned int *lcorelist, unsigned int nb_lc)
2178 {
2179         unsigned int i;
2180         unsigned int lcore_cpuid;
2181         int record_now;
2182
2183         record_now = 0;
2184  again:
2185         for (i = 0; i < nb_lc; i++) {
2186                 lcore_cpuid = lcorelist[i];
2187                 if (! rte_lcore_is_enabled(lcore_cpuid)) {
2188                         printf("lcore %u not enabled\n", lcore_cpuid);
2189                         return -1;
2190                 }
2191                 if (lcore_cpuid == rte_get_master_lcore()) {
2192                         printf("lcore %u cannot be masked on for running "
2193                                "packet forwarding, which is the master lcore "
2194                                "and reserved for command line parsing only\n",
2195                                lcore_cpuid);
2196                         return -1;
2197                 }
2198                 if (record_now)
2199                         fwd_lcores_cpuids[i] = lcore_cpuid;
2200         }
2201         if (record_now == 0) {
2202                 record_now = 1;
2203                 goto again;
2204         }
2205         nb_cfg_lcores = (lcoreid_t) nb_lc;
2206         if (nb_fwd_lcores != (lcoreid_t) nb_lc) {
2207                 printf("previous number of forwarding cores %u - changed to "
2208                        "number of configured cores %u\n",
2209                        (unsigned int) nb_fwd_lcores, nb_lc);
2210                 nb_fwd_lcores = (lcoreid_t) nb_lc;
2211         }
2212
2213         return 0;
2214 }
2215
2216 int
2217 set_fwd_lcores_mask(uint64_t lcoremask)
2218 {
2219         unsigned int lcorelist[64];
2220         unsigned int nb_lc;
2221         unsigned int i;
2222
2223         if (lcoremask == 0) {
2224                 printf("Invalid NULL mask of cores\n");
2225                 return -1;
2226         }
2227         nb_lc = 0;
2228         for (i = 0; i < 64; i++) {
2229                 if (! ((uint64_t)(1ULL << i) & lcoremask))
2230                         continue;
2231                 lcorelist[nb_lc++] = i;
2232         }
2233         return set_fwd_lcores_list(lcorelist, nb_lc);
2234 }
2235
2236 void
2237 set_fwd_lcores_number(uint16_t nb_lc)
2238 {
2239         if (nb_lc > nb_cfg_lcores) {
2240                 printf("nb fwd cores %u > %u (max. number of configured "
2241                        "lcores) - ignored\n",
2242                        (unsigned int) nb_lc, (unsigned int) nb_cfg_lcores);
2243                 return;
2244         }
2245         nb_fwd_lcores = (lcoreid_t) nb_lc;
2246         printf("Number of forwarding cores set to %u\n",
2247                (unsigned int) nb_fwd_lcores);
2248 }
2249
2250 void
2251 set_fwd_ports_list(unsigned int *portlist, unsigned int nb_pt)
2252 {
2253         unsigned int i;
2254         portid_t port_id;
2255         int record_now;
2256
2257         record_now = 0;
2258  again:
2259         for (i = 0; i < nb_pt; i++) {
2260                 port_id = (portid_t) portlist[i];
2261                 if (port_id_is_invalid(port_id, ENABLED_WARN))
2262                         return;
2263                 if (record_now)
2264                         fwd_ports_ids[i] = port_id;
2265         }
2266         if (record_now == 0) {
2267                 record_now = 1;
2268                 goto again;
2269         }
2270         nb_cfg_ports = (portid_t) nb_pt;
2271         if (nb_fwd_ports != (portid_t) nb_pt) {
2272                 printf("previous number of forwarding ports %u - changed to "
2273                        "number of configured ports %u\n",
2274                        (unsigned int) nb_fwd_ports, nb_pt);
2275                 nb_fwd_ports = (portid_t) nb_pt;
2276         }
2277 }
2278
2279 void
2280 set_fwd_ports_mask(uint64_t portmask)
2281 {
2282         unsigned int portlist[64];
2283         unsigned int nb_pt;
2284         unsigned int i;
2285
2286         if (portmask == 0) {
2287                 printf("Invalid NULL mask of ports\n");
2288                 return;
2289         }
2290         nb_pt = 0;
2291         RTE_ETH_FOREACH_DEV(i) {
2292                 if (! ((uint64_t)(1ULL << i) & portmask))
2293                         continue;
2294                 portlist[nb_pt++] = i;
2295         }
2296         set_fwd_ports_list(portlist, nb_pt);
2297 }
2298
2299 void
2300 set_fwd_ports_number(uint16_t nb_pt)
2301 {
2302         if (nb_pt > nb_cfg_ports) {
2303                 printf("nb fwd ports %u > %u (number of configured "
2304                        "ports) - ignored\n",
2305                        (unsigned int) nb_pt, (unsigned int) nb_cfg_ports);
2306                 return;
2307         }
2308         nb_fwd_ports = (portid_t) nb_pt;
2309         printf("Number of forwarding ports set to %u\n",
2310                (unsigned int) nb_fwd_ports);
2311 }
2312
2313 int
2314 port_is_forwarding(portid_t port_id)
2315 {
2316         unsigned int i;
2317
2318         if (port_id_is_invalid(port_id, ENABLED_WARN))
2319                 return -1;
2320
2321         for (i = 0; i < nb_fwd_ports; i++) {
2322                 if (fwd_ports_ids[i] == port_id)
2323                         return 1;
2324         }
2325
2326         return 0;
2327 }
2328
2329 void
2330 set_nb_pkt_per_burst(uint16_t nb)
2331 {
2332         if (nb > MAX_PKT_BURST) {
2333                 printf("nb pkt per burst: %u > %u (maximum packet per burst) "
2334                        " ignored\n",
2335                        (unsigned int) nb, (unsigned int) MAX_PKT_BURST);
2336                 return;
2337         }
2338         nb_pkt_per_burst = nb;
2339         printf("Number of packets per burst set to %u\n",
2340                (unsigned int) nb_pkt_per_burst);
2341 }
2342
2343 static const char *
2344 tx_split_get_name(enum tx_pkt_split split)
2345 {
2346         uint32_t i;
2347
2348         for (i = 0; i != RTE_DIM(tx_split_name); i++) {
2349                 if (tx_split_name[i].split == split)
2350                         return tx_split_name[i].name;
2351         }
2352         return NULL;
2353 }
2354
2355 void
2356 set_tx_pkt_split(const char *name)
2357 {
2358         uint32_t i;
2359
2360         for (i = 0; i != RTE_DIM(tx_split_name); i++) {
2361                 if (strcmp(tx_split_name[i].name, name) == 0) {
2362                         tx_pkt_split = tx_split_name[i].split;
2363                         return;
2364                 }
2365         }
2366         printf("unknown value: \"%s\"\n", name);
2367 }
2368
2369 void
2370 show_tx_pkt_segments(void)
2371 {
2372         uint32_t i, n;
2373         const char *split;
2374
2375         n = tx_pkt_nb_segs;
2376         split = tx_split_get_name(tx_pkt_split);
2377
2378         printf("Number of segments: %u\n", n);
2379         printf("Segment sizes: ");
2380         for (i = 0; i != n - 1; i++)
2381                 printf("%hu,", tx_pkt_seg_lengths[i]);
2382         printf("%hu\n", tx_pkt_seg_lengths[i]);
2383         printf("Split packet: %s\n", split);
2384 }
2385
2386 void
2387 set_tx_pkt_segments(unsigned *seg_lengths, unsigned nb_segs)
2388 {
2389         uint16_t tx_pkt_len;
2390         unsigned i;
2391
2392         if (nb_segs >= (unsigned) nb_txd) {
2393                 printf("nb segments per TX packets=%u >= nb_txd=%u - ignored\n",
2394                        nb_segs, (unsigned int) nb_txd);
2395                 return;
2396         }
2397
2398         /*
2399          * Check that each segment length is greater or equal than
2400          * the mbuf data sise.
2401          * Check also that the total packet length is greater or equal than the
2402          * size of an empty UDP/IP packet (sizeof(struct ether_hdr) + 20 + 8).
2403          */
2404         tx_pkt_len = 0;
2405         for (i = 0; i < nb_segs; i++) {
2406                 if (seg_lengths[i] > (unsigned) mbuf_data_size) {
2407                         printf("length[%u]=%u > mbuf_data_size=%u - give up\n",
2408                                i, seg_lengths[i], (unsigned) mbuf_data_size);
2409                         return;
2410                 }
2411                 tx_pkt_len = (uint16_t)(tx_pkt_len + seg_lengths[i]);
2412         }
2413         if (tx_pkt_len < (sizeof(struct ether_hdr) + 20 + 8)) {
2414                 printf("total packet length=%u < %d - give up\n",
2415                                 (unsigned) tx_pkt_len,
2416                                 (int)(sizeof(struct ether_hdr) + 20 + 8));
2417                 return;
2418         }
2419
2420         for (i = 0; i < nb_segs; i++)
2421                 tx_pkt_seg_lengths[i] = (uint16_t) seg_lengths[i];
2422
2423         tx_pkt_length  = tx_pkt_len;
2424         tx_pkt_nb_segs = (uint8_t) nb_segs;
2425 }
2426
2427 void
2428 setup_gro(const char *mode, uint8_t port_id)
2429 {
2430         if (!rte_eth_dev_is_valid_port(port_id)) {
2431                 printf("invalid port id %u\n", port_id);
2432                 return;
2433         }
2434         if (test_done == 0) {
2435                 printf("Before enable/disable GRO,"
2436                                 " please stop forwarding first\n");
2437                 return;
2438         }
2439         if (strcmp(mode, "on") == 0) {
2440                 if (gro_ports[port_id].enable) {
2441                         printf("port %u has enabled GRO\n", port_id);
2442                         return;
2443                 }
2444                 gro_ports[port_id].enable = 1;
2445                 gro_ports[port_id].param.gro_types = RTE_GRO_TCP_IPV4;
2446
2447                 if (gro_ports[port_id].param.max_flow_num == 0)
2448                         gro_ports[port_id].param.max_flow_num =
2449                                 GRO_DEFAULT_FLOW_NUM;
2450                 if (gro_ports[port_id].param.max_item_per_flow == 0)
2451                         gro_ports[port_id].param.max_item_per_flow =
2452                                 GRO_DEFAULT_ITEM_NUM_PER_FLOW;
2453         } else {
2454                 if (gro_ports[port_id].enable == 0) {
2455                         printf("port %u has disabled GRO\n", port_id);
2456                         return;
2457                 }
2458                 gro_ports[port_id].enable = 0;
2459         }
2460 }
2461
2462 char*
2463 list_pkt_forwarding_modes(void)
2464 {
2465         static char fwd_modes[128] = "";
2466         const char *separator = "|";
2467         struct fwd_engine *fwd_eng;
2468         unsigned i = 0;
2469
2470         if (strlen (fwd_modes) == 0) {
2471                 while ((fwd_eng = fwd_engines[i++]) != NULL) {
2472                         strncat(fwd_modes, fwd_eng->fwd_mode_name,
2473                                         sizeof(fwd_modes) - strlen(fwd_modes) - 1);
2474                         strncat(fwd_modes, separator,
2475                                         sizeof(fwd_modes) - strlen(fwd_modes) - 1);
2476                 }
2477                 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
2478         }
2479
2480         return fwd_modes;
2481 }
2482
2483 char*
2484 list_pkt_forwarding_retry_modes(void)
2485 {
2486         static char fwd_modes[128] = "";
2487         const char *separator = "|";
2488         struct fwd_engine *fwd_eng;
2489         unsigned i = 0;
2490
2491         if (strlen(fwd_modes) == 0) {
2492                 while ((fwd_eng = fwd_engines[i++]) != NULL) {
2493                         if (fwd_eng == &rx_only_engine)
2494                                 continue;
2495                         strncat(fwd_modes, fwd_eng->fwd_mode_name,
2496                                         sizeof(fwd_modes) -
2497                                         strlen(fwd_modes) - 1);
2498                         strncat(fwd_modes, separator,
2499                                         sizeof(fwd_modes) -
2500                                         strlen(fwd_modes) - 1);
2501                 }
2502                 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0';
2503         }
2504
2505         return fwd_modes;
2506 }
2507
2508 void
2509 set_pkt_forwarding_mode(const char *fwd_mode_name)
2510 {
2511         struct fwd_engine *fwd_eng;
2512         unsigned i;
2513
2514         i = 0;
2515         while ((fwd_eng = fwd_engines[i]) != NULL) {
2516                 if (! strcmp(fwd_eng->fwd_mode_name, fwd_mode_name)) {
2517                         printf("Set %s packet forwarding mode%s\n",
2518                                fwd_mode_name,
2519                                retry_enabled == 0 ? "" : " with retry");
2520                         cur_fwd_eng = fwd_eng;
2521                         return;
2522                 }
2523                 i++;
2524         }
2525         printf("Invalid %s packet forwarding mode\n", fwd_mode_name);
2526 }
2527
2528 void
2529 set_verbose_level(uint16_t vb_level)
2530 {
2531         printf("Change verbose level from %u to %u\n",
2532                (unsigned int) verbose_level, (unsigned int) vb_level);
2533         verbose_level = vb_level;
2534 }
2535
2536 void
2537 vlan_extend_set(portid_t port_id, int on)
2538 {
2539         int diag;
2540         int vlan_offload;
2541
2542         if (port_id_is_invalid(port_id, ENABLED_WARN))
2543                 return;
2544
2545         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
2546
2547         if (on)
2548                 vlan_offload |= ETH_VLAN_EXTEND_OFFLOAD;
2549         else
2550                 vlan_offload &= ~ETH_VLAN_EXTEND_OFFLOAD;
2551
2552         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
2553         if (diag < 0)
2554                 printf("rx_vlan_extend_set(port_pi=%d, on=%d) failed "
2555                "diag=%d\n", port_id, on, diag);
2556 }
2557
2558 void
2559 rx_vlan_strip_set(portid_t port_id, int on)
2560 {
2561         int diag;
2562         int vlan_offload;
2563
2564         if (port_id_is_invalid(port_id, ENABLED_WARN))
2565                 return;
2566
2567         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
2568
2569         if (on)
2570                 vlan_offload |= ETH_VLAN_STRIP_OFFLOAD;
2571         else
2572                 vlan_offload &= ~ETH_VLAN_STRIP_OFFLOAD;
2573
2574         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
2575         if (diag < 0)
2576                 printf("rx_vlan_strip_set(port_pi=%d, on=%d) failed "
2577                "diag=%d\n", port_id, on, diag);
2578 }
2579
2580 void
2581 rx_vlan_strip_set_on_queue(portid_t port_id, uint16_t queue_id, int on)
2582 {
2583         int diag;
2584
2585         if (port_id_is_invalid(port_id, ENABLED_WARN))
2586                 return;
2587
2588         diag = rte_eth_dev_set_vlan_strip_on_queue(port_id, queue_id, on);
2589         if (diag < 0)
2590                 printf("rx_vlan_strip_set_on_queue(port_pi=%d, queue_id=%d, on=%d) failed "
2591                "diag=%d\n", port_id, queue_id, on, diag);
2592 }
2593
2594 void
2595 rx_vlan_filter_set(portid_t port_id, int on)
2596 {
2597         int diag;
2598         int vlan_offload;
2599
2600         if (port_id_is_invalid(port_id, ENABLED_WARN))
2601                 return;
2602
2603         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
2604
2605         if (on)
2606                 vlan_offload |= ETH_VLAN_FILTER_OFFLOAD;
2607         else
2608                 vlan_offload &= ~ETH_VLAN_FILTER_OFFLOAD;
2609
2610         diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload);
2611         if (diag < 0)
2612                 printf("rx_vlan_filter_set(port_pi=%d, on=%d) failed "
2613                "diag=%d\n", port_id, on, diag);
2614 }
2615
2616 int
2617 rx_vft_set(portid_t port_id, uint16_t vlan_id, int on)
2618 {
2619         int diag;
2620
2621         if (port_id_is_invalid(port_id, ENABLED_WARN))
2622                 return 1;
2623         if (vlan_id_is_invalid(vlan_id))
2624                 return 1;
2625         diag = rte_eth_dev_vlan_filter(port_id, vlan_id, on);
2626         if (diag == 0)
2627                 return 0;
2628         printf("rte_eth_dev_vlan_filter(port_pi=%d, vlan_id=%d, on=%d) failed "
2629                "diag=%d\n",
2630                port_id, vlan_id, on, diag);
2631         return -1;
2632 }
2633
2634 void
2635 rx_vlan_all_filter_set(portid_t port_id, int on)
2636 {
2637         uint16_t vlan_id;
2638
2639         if (port_id_is_invalid(port_id, ENABLED_WARN))
2640                 return;
2641         for (vlan_id = 0; vlan_id < 4096; vlan_id++) {
2642                 if (rx_vft_set(port_id, vlan_id, on))
2643                         break;
2644         }
2645 }
2646
2647 void
2648 vlan_tpid_set(portid_t port_id, enum rte_vlan_type vlan_type, uint16_t tp_id)
2649 {
2650         int diag;
2651
2652         if (port_id_is_invalid(port_id, ENABLED_WARN))
2653                 return;
2654
2655         diag = rte_eth_dev_set_vlan_ether_type(port_id, vlan_type, tp_id);
2656         if (diag == 0)
2657                 return;
2658
2659         printf("tx_vlan_tpid_set(port_pi=%d, vlan_type=%d, tpid=%d) failed "
2660                "diag=%d\n",
2661                port_id, vlan_type, tp_id, diag);
2662 }
2663
2664 void
2665 tx_vlan_set(portid_t port_id, uint16_t vlan_id)
2666 {
2667         int vlan_offload;
2668         if (port_id_is_invalid(port_id, ENABLED_WARN))
2669                 return;
2670         if (vlan_id_is_invalid(vlan_id))
2671                 return;
2672
2673         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
2674         if (vlan_offload & ETH_VLAN_EXTEND_OFFLOAD) {
2675                 printf("Error, as QinQ has been enabled.\n");
2676                 return;
2677         }
2678
2679         tx_vlan_reset(port_id);
2680         ports[port_id].tx_ol_flags |= TESTPMD_TX_OFFLOAD_INSERT_VLAN;
2681         ports[port_id].tx_vlan_id = vlan_id;
2682 }
2683
2684 void
2685 tx_qinq_set(portid_t port_id, uint16_t vlan_id, uint16_t vlan_id_outer)
2686 {
2687         int vlan_offload;
2688         if (port_id_is_invalid(port_id, ENABLED_WARN))
2689                 return;
2690         if (vlan_id_is_invalid(vlan_id))
2691                 return;
2692         if (vlan_id_is_invalid(vlan_id_outer))
2693                 return;
2694
2695         vlan_offload = rte_eth_dev_get_vlan_offload(port_id);
2696         if (!(vlan_offload & ETH_VLAN_EXTEND_OFFLOAD)) {
2697                 printf("Error, as QinQ hasn't been enabled.\n");
2698                 return;
2699         }
2700
2701         tx_vlan_reset(port_id);
2702         ports[port_id].tx_ol_flags |= TESTPMD_TX_OFFLOAD_INSERT_QINQ;
2703         ports[port_id].tx_vlan_id = vlan_id;
2704         ports[port_id].tx_vlan_id_outer = vlan_id_outer;
2705 }
2706
2707 void
2708 tx_vlan_reset(portid_t port_id)
2709 {
2710         if (port_id_is_invalid(port_id, ENABLED_WARN))
2711                 return;
2712         ports[port_id].tx_ol_flags &= ~(TESTPMD_TX_OFFLOAD_INSERT_VLAN |
2713                                 TESTPMD_TX_OFFLOAD_INSERT_QINQ);
2714         ports[port_id].tx_vlan_id = 0;
2715         ports[port_id].tx_vlan_id_outer = 0;
2716 }
2717
2718 void
2719 tx_vlan_pvid_set(portid_t port_id, uint16_t vlan_id, int on)
2720 {
2721         if (port_id_is_invalid(port_id, ENABLED_WARN))
2722                 return;
2723
2724         rte_eth_dev_set_vlan_pvid(port_id, vlan_id, on);
2725 }
2726
2727 void
2728 set_qmap(portid_t port_id, uint8_t is_rx, uint16_t queue_id, uint8_t map_value)
2729 {
2730         uint16_t i;
2731         uint8_t existing_mapping_found = 0;
2732
2733         if (port_id_is_invalid(port_id, ENABLED_WARN))
2734                 return;
2735
2736         if (is_rx ? (rx_queue_id_is_invalid(queue_id)) : (tx_queue_id_is_invalid(queue_id)))
2737                 return;
2738
2739         if (map_value >= RTE_ETHDEV_QUEUE_STAT_CNTRS) {
2740                 printf("map_value not in required range 0..%d\n",
2741                                 RTE_ETHDEV_QUEUE_STAT_CNTRS - 1);
2742                 return;
2743         }
2744
2745         if (!is_rx) { /*then tx*/
2746                 for (i = 0; i < nb_tx_queue_stats_mappings; i++) {
2747                         if ((tx_queue_stats_mappings[i].port_id == port_id) &&
2748                             (tx_queue_stats_mappings[i].queue_id == queue_id)) {
2749                                 tx_queue_stats_mappings[i].stats_counter_id = map_value;
2750                                 existing_mapping_found = 1;
2751                                 break;
2752                         }
2753                 }
2754                 if (!existing_mapping_found) { /* A new additional mapping... */
2755                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].port_id = port_id;
2756                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].queue_id = queue_id;
2757                         tx_queue_stats_mappings[nb_tx_queue_stats_mappings].stats_counter_id = map_value;
2758                         nb_tx_queue_stats_mappings++;
2759                 }
2760         }
2761         else { /*rx*/
2762                 for (i = 0; i < nb_rx_queue_stats_mappings; i++) {
2763                         if ((rx_queue_stats_mappings[i].port_id == port_id) &&
2764                             (rx_queue_stats_mappings[i].queue_id == queue_id)) {
2765                                 rx_queue_stats_mappings[i].stats_counter_id = map_value;
2766                                 existing_mapping_found = 1;
2767                                 break;
2768                         }
2769                 }
2770                 if (!existing_mapping_found) { /* A new additional mapping... */
2771                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].port_id = port_id;
2772                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].queue_id = queue_id;
2773                         rx_queue_stats_mappings[nb_rx_queue_stats_mappings].stats_counter_id = map_value;
2774                         nb_rx_queue_stats_mappings++;
2775                 }
2776         }
2777 }
2778
2779 static inline void
2780 print_fdir_mask(struct rte_eth_fdir_masks *mask)
2781 {
2782         printf("\n    vlan_tci: 0x%04x", rte_be_to_cpu_16(mask->vlan_tci_mask));
2783
2784         if (fdir_conf.mode == RTE_FDIR_MODE_PERFECT_TUNNEL)
2785                 printf(", mac_addr: 0x%02x, tunnel_type: 0x%01x,"
2786                         " tunnel_id: 0x%08x",
2787                         mask->mac_addr_byte_mask, mask->tunnel_type_mask,
2788                         rte_be_to_cpu_32(mask->tunnel_id_mask));
2789         else if (fdir_conf.mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
2790                 printf(", src_ipv4: 0x%08x, dst_ipv4: 0x%08x",
2791                         rte_be_to_cpu_32(mask->ipv4_mask.src_ip),
2792                         rte_be_to_cpu_32(mask->ipv4_mask.dst_ip));
2793
2794                 printf("\n    src_port: 0x%04x, dst_port: 0x%04x",
2795                         rte_be_to_cpu_16(mask->src_port_mask),
2796                         rte_be_to_cpu_16(mask->dst_port_mask));
2797
2798                 printf("\n    src_ipv6: 0x%08x,0x%08x,0x%08x,0x%08x",
2799                         rte_be_to_cpu_32(mask->ipv6_mask.src_ip[0]),
2800                         rte_be_to_cpu_32(mask->ipv6_mask.src_ip[1]),
2801                         rte_be_to_cpu_32(mask->ipv6_mask.src_ip[2]),
2802                         rte_be_to_cpu_32(mask->ipv6_mask.src_ip[3]));
2803
2804                 printf("\n    dst_ipv6: 0x%08x,0x%08x,0x%08x,0x%08x",
2805                         rte_be_to_cpu_32(mask->ipv6_mask.dst_ip[0]),
2806                         rte_be_to_cpu_32(mask->ipv6_mask.dst_ip[1]),
2807                         rte_be_to_cpu_32(mask->ipv6_mask.dst_ip[2]),
2808                         rte_be_to_cpu_32(mask->ipv6_mask.dst_ip[3]));
2809         }
2810
2811         printf("\n");
2812 }
2813
2814 static inline void
2815 print_fdir_flex_payload(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num)
2816 {
2817         struct rte_eth_flex_payload_cfg *cfg;
2818         uint32_t i, j;
2819
2820         for (i = 0; i < flex_conf->nb_payloads; i++) {
2821                 cfg = &flex_conf->flex_set[i];
2822                 if (cfg->type == RTE_ETH_RAW_PAYLOAD)
2823                         printf("\n    RAW:  ");
2824                 else if (cfg->type == RTE_ETH_L2_PAYLOAD)
2825                         printf("\n    L2_PAYLOAD:  ");
2826                 else if (cfg->type == RTE_ETH_L3_PAYLOAD)
2827                         printf("\n    L3_PAYLOAD:  ");
2828                 else if (cfg->type == RTE_ETH_L4_PAYLOAD)
2829                         printf("\n    L4_PAYLOAD:  ");
2830                 else
2831                         printf("\n    UNKNOWN PAYLOAD(%u):  ", cfg->type);
2832                 for (j = 0; j < num; j++)
2833                         printf("  %-5u", cfg->src_offset[j]);
2834         }
2835         printf("\n");
2836 }
2837
2838 static char *
2839 flowtype_to_str(uint16_t flow_type)
2840 {
2841         struct flow_type_info {
2842                 char str[32];
2843                 uint16_t ftype;
2844         };
2845
2846         uint8_t i;
2847         static struct flow_type_info flowtype_str_table[] = {
2848                 {"raw", RTE_ETH_FLOW_RAW},
2849                 {"ipv4", RTE_ETH_FLOW_IPV4},
2850                 {"ipv4-frag", RTE_ETH_FLOW_FRAG_IPV4},
2851                 {"ipv4-tcp", RTE_ETH_FLOW_NONFRAG_IPV4_TCP},
2852                 {"ipv4-udp", RTE_ETH_FLOW_NONFRAG_IPV4_UDP},
2853                 {"ipv4-sctp", RTE_ETH_FLOW_NONFRAG_IPV4_SCTP},
2854                 {"ipv4-other", RTE_ETH_FLOW_NONFRAG_IPV4_OTHER},
2855                 {"ipv6", RTE_ETH_FLOW_IPV6},
2856                 {"ipv6-frag", RTE_ETH_FLOW_FRAG_IPV6},
2857                 {"ipv6-tcp", RTE_ETH_FLOW_NONFRAG_IPV6_TCP},
2858                 {"ipv6-udp", RTE_ETH_FLOW_NONFRAG_IPV6_UDP},
2859                 {"ipv6-sctp", RTE_ETH_FLOW_NONFRAG_IPV6_SCTP},
2860                 {"ipv6-other", RTE_ETH_FLOW_NONFRAG_IPV6_OTHER},
2861                 {"l2_payload", RTE_ETH_FLOW_L2_PAYLOAD},
2862                 {"port", RTE_ETH_FLOW_PORT},
2863                 {"vxlan", RTE_ETH_FLOW_VXLAN},
2864                 {"geneve", RTE_ETH_FLOW_GENEVE},
2865                 {"nvgre", RTE_ETH_FLOW_NVGRE},
2866         };
2867
2868         for (i = 0; i < RTE_DIM(flowtype_str_table); i++) {
2869                 if (flowtype_str_table[i].ftype == flow_type)
2870                         return flowtype_str_table[i].str;
2871         }
2872
2873         return NULL;
2874 }
2875
2876 static inline void
2877 print_fdir_flex_mask(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num)
2878 {
2879         struct rte_eth_fdir_flex_mask *mask;
2880         uint32_t i, j;
2881         char *p;
2882
2883         for (i = 0; i < flex_conf->nb_flexmasks; i++) {
2884                 mask = &flex_conf->flex_mask[i];
2885                 p = flowtype_to_str(mask->flow_type);
2886                 printf("\n    %s:\t", p ? p : "unknown");
2887                 for (j = 0; j < num; j++)
2888                         printf(" %02x", mask->mask[j]);
2889         }
2890         printf("\n");
2891 }
2892
2893 static inline void
2894 print_fdir_flow_type(uint32_t flow_types_mask)
2895 {
2896         int i;
2897         char *p;
2898
2899         for (i = RTE_ETH_FLOW_UNKNOWN; i < RTE_ETH_FLOW_MAX; i++) {
2900                 if (!(flow_types_mask & (1 << i)))
2901                         continue;
2902                 p = flowtype_to_str(i);
2903                 if (p)
2904                         printf(" %s", p);
2905                 else
2906                         printf(" unknown");
2907         }
2908         printf("\n");
2909 }
2910
2911 void
2912 fdir_get_infos(portid_t port_id)
2913 {
2914         struct rte_eth_fdir_stats fdir_stat;
2915         struct rte_eth_fdir_info fdir_info;
2916         int ret;
2917
2918         static const char *fdir_stats_border = "########################";
2919
2920         if (port_id_is_invalid(port_id, ENABLED_WARN))
2921                 return;
2922         ret = rte_eth_dev_filter_supported(port_id, RTE_ETH_FILTER_FDIR);
2923         if (ret < 0) {
2924                 printf("\n FDIR is not supported on port %-2d\n",
2925                         port_id);
2926                 return;
2927         }
2928
2929         memset(&fdir_info, 0, sizeof(fdir_info));
2930         rte_eth_dev_filter_ctrl(port_id, RTE_ETH_FILTER_FDIR,
2931                                RTE_ETH_FILTER_INFO, &fdir_info);
2932         memset(&fdir_stat, 0, sizeof(fdir_stat));
2933         rte_eth_dev_filter_ctrl(port_id, RTE_ETH_FILTER_FDIR,
2934                                RTE_ETH_FILTER_STATS, &fdir_stat);
2935         printf("\n  %s FDIR infos for port %-2d     %s\n",
2936                fdir_stats_border, port_id, fdir_stats_border);
2937         printf("  MODE: ");
2938         if (fdir_info.mode == RTE_FDIR_MODE_PERFECT)
2939                 printf("  PERFECT\n");
2940         else if (fdir_info.mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN)
2941                 printf("  PERFECT-MAC-VLAN\n");
2942         else if (fdir_info.mode == RTE_FDIR_MODE_PERFECT_TUNNEL)
2943                 printf("  PERFECT-TUNNEL\n");
2944         else if (fdir_info.mode == RTE_FDIR_MODE_SIGNATURE)
2945                 printf("  SIGNATURE\n");
2946         else
2947                 printf("  DISABLE\n");
2948         if (fdir_info.mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN
2949                 && fdir_info.mode != RTE_FDIR_MODE_PERFECT_TUNNEL) {
2950                 printf("  SUPPORTED FLOW TYPE: ");
2951                 print_fdir_flow_type(fdir_info.flow_types_mask[0]);
2952         }
2953         printf("  FLEX PAYLOAD INFO:\n");
2954         printf("  max_len:       %-10"PRIu32"  payload_limit: %-10"PRIu32"\n"
2955                "  payload_unit:  %-10"PRIu32"  payload_seg:   %-10"PRIu32"\n"
2956                "  bitmask_unit:  %-10"PRIu32"  bitmask_num:   %-10"PRIu32"\n",
2957                 fdir_info.max_flexpayload, fdir_info.flex_payload_limit,
2958                 fdir_info.flex_payload_unit,
2959                 fdir_info.max_flex_payload_segment_num,
2960                 fdir_info.flex_bitmask_unit, fdir_info.max_flex_bitmask_num);
2961         printf("  MASK: ");
2962         print_fdir_mask(&fdir_info.mask);
2963         if (fdir_info.flex_conf.nb_payloads > 0) {
2964                 printf("  FLEX PAYLOAD SRC OFFSET:");
2965                 print_fdir_flex_payload(&fdir_info.flex_conf, fdir_info.max_flexpayload);
2966         }
2967         if (fdir_info.flex_conf.nb_flexmasks > 0) {
2968                 printf("  FLEX MASK CFG:");
2969                 print_fdir_flex_mask(&fdir_info.flex_conf, fdir_info.max_flexpayload);
2970         }
2971         printf("  guarant_count: %-10"PRIu32"  best_count:    %"PRIu32"\n",
2972                fdir_stat.guarant_cnt, fdir_stat.best_cnt);
2973         printf("  guarant_space: %-10"PRIu32"  best_space:    %"PRIu32"\n",
2974                fdir_info.guarant_spc, fdir_info.best_spc);
2975         printf("  collision:     %-10"PRIu32"  free:          %"PRIu32"\n"
2976                "  maxhash:       %-10"PRIu32"  maxlen:        %"PRIu32"\n"
2977                "  add:           %-10"PRIu64"  remove:        %"PRIu64"\n"
2978                "  f_add:         %-10"PRIu64"  f_remove:      %"PRIu64"\n",
2979                fdir_stat.collision, fdir_stat.free,
2980                fdir_stat.maxhash, fdir_stat.maxlen,
2981                fdir_stat.add, fdir_stat.remove,
2982                fdir_stat.f_add, fdir_stat.f_remove);
2983         printf("  %s############################%s\n",
2984                fdir_stats_border, fdir_stats_border);
2985 }
2986
2987 void
2988 fdir_set_flex_mask(portid_t port_id, struct rte_eth_fdir_flex_mask *cfg)
2989 {
2990         struct rte_port *port;
2991         struct rte_eth_fdir_flex_conf *flex_conf;
2992         int i, idx = 0;
2993
2994         port = &ports[port_id];
2995         flex_conf = &port->dev_conf.fdir_conf.flex_conf;
2996         for (i = 0; i < RTE_ETH_FLOW_MAX; i++) {
2997                 if (cfg->flow_type == flex_conf->flex_mask[i].flow_type) {
2998                         idx = i;
2999                         break;
3000                 }
3001         }
3002         if (i >= RTE_ETH_FLOW_MAX) {
3003                 if (flex_conf->nb_flexmasks < RTE_DIM(flex_conf->flex_mask)) {
3004                         idx = flex_conf->nb_flexmasks;
3005                         flex_conf->nb_flexmasks++;
3006                 } else {
3007                         printf("The flex mask table is full. Can not set flex"
3008                                 " mask for flow_type(%u).", cfg->flow_type);
3009                         return;
3010                 }
3011         }
3012         rte_memcpy(&flex_conf->flex_mask[idx],
3013                          cfg,
3014                          sizeof(struct rte_eth_fdir_flex_mask));
3015 }
3016
3017 void
3018 fdir_set_flex_payload(portid_t port_id, struct rte_eth_flex_payload_cfg *cfg)
3019 {
3020         struct rte_port *port;
3021         struct rte_eth_fdir_flex_conf *flex_conf;
3022         int i, idx = 0;
3023
3024         port = &ports[port_id];
3025         flex_conf = &port->dev_conf.fdir_conf.flex_conf;
3026         for (i = 0; i < RTE_ETH_PAYLOAD_MAX; i++) {
3027                 if (cfg->type == flex_conf->flex_set[i].type) {
3028                         idx = i;
3029                         break;
3030                 }
3031         }
3032         if (i >= RTE_ETH_PAYLOAD_MAX) {
3033                 if (flex_conf->nb_payloads < RTE_DIM(flex_conf->flex_set)) {
3034                         idx = flex_conf->nb_payloads;
3035                         flex_conf->nb_payloads++;
3036                 } else {
3037                         printf("The flex payload table is full. Can not set"
3038                                 " flex payload for type(%u).", cfg->type);
3039                         return;
3040                 }
3041         }
3042         rte_memcpy(&flex_conf->flex_set[idx],
3043                          cfg,
3044                          sizeof(struct rte_eth_flex_payload_cfg));
3045
3046 }
3047
3048 void
3049 set_vf_traffic(portid_t port_id, uint8_t is_rx, uint16_t vf, uint8_t on)
3050 {
3051 #ifdef RTE_LIBRTE_IXGBE_PMD
3052         int diag;
3053
3054         if (is_rx)
3055                 diag = rte_pmd_ixgbe_set_vf_rx(port_id, vf, on);
3056         else
3057                 diag = rte_pmd_ixgbe_set_vf_tx(port_id, vf, on);
3058
3059         if (diag == 0)
3060                 return;
3061         printf("rte_pmd_ixgbe_set_vf_%s for port_id=%d failed diag=%d\n",
3062                         is_rx ? "rx" : "tx", port_id, diag);
3063         return;
3064 #endif
3065         printf("VF %s setting not supported for port %d\n",
3066                         is_rx ? "Rx" : "Tx", port_id);
3067         RTE_SET_USED(vf);
3068         RTE_SET_USED(on);
3069 }
3070
3071 int
3072 set_queue_rate_limit(portid_t port_id, uint16_t queue_idx, uint16_t rate)
3073 {
3074         int diag;
3075         struct rte_eth_link link;
3076
3077         if (port_id_is_invalid(port_id, ENABLED_WARN))
3078                 return 1;
3079         rte_eth_link_get_nowait(port_id, &link);
3080         if (rate > link.link_speed) {
3081                 printf("Invalid rate value:%u bigger than link speed: %u\n",
3082                         rate, link.link_speed);
3083                 return 1;
3084         }
3085         diag = rte_eth_set_queue_rate_limit(port_id, queue_idx, rate);
3086         if (diag == 0)
3087                 return diag;
3088         printf("rte_eth_set_queue_rate_limit for port_id=%d failed diag=%d\n",
3089                 port_id, diag);
3090         return diag;
3091 }
3092
3093 int
3094 set_vf_rate_limit(portid_t port_id, uint16_t vf, uint16_t rate, uint64_t q_msk)
3095 {
3096         int diag = -ENOTSUP;
3097
3098 #ifdef RTE_LIBRTE_IXGBE_PMD
3099         if (diag == -ENOTSUP)
3100                 diag = rte_pmd_ixgbe_set_vf_rate_limit(port_id, vf, rate,
3101                                                        q_msk);
3102 #endif
3103 #ifdef RTE_LIBRTE_BNXT_PMD
3104         if (diag == -ENOTSUP)
3105                 diag = rte_pmd_bnxt_set_vf_rate_limit(port_id, vf, rate, q_msk);
3106 #endif
3107         if (diag == 0)
3108                 return diag;
3109
3110         printf("set_vf_rate_limit for port_id=%d failed diag=%d\n",
3111                 port_id, diag);
3112         return diag;
3113 }
3114
3115 /*
3116  * Functions to manage the set of filtered Multicast MAC addresses.
3117  *
3118  * A pool of filtered multicast MAC addresses is associated with each port.
3119  * The pool is allocated in chunks of MCAST_POOL_INC multicast addresses.
3120  * The address of the pool and the number of valid multicast MAC addresses
3121  * recorded in the pool are stored in the fields "mc_addr_pool" and
3122  * "mc_addr_nb" of the "rte_port" data structure.
3123  *
3124  * The function "rte_eth_dev_set_mc_addr_list" of the PMDs API imposes
3125  * to be supplied a contiguous array of multicast MAC addresses.
3126  * To comply with this constraint, the set of multicast addresses recorded
3127  * into the pool are systematically compacted at the beginning of the pool.
3128  * Hence, when a multicast address is removed from the pool, all following
3129  * addresses, if any, are copied back to keep the set contiguous.
3130  */
3131 #define MCAST_POOL_INC 32
3132
3133 static int
3134 mcast_addr_pool_extend(struct rte_port *port)
3135 {
3136         struct ether_addr *mc_pool;
3137         size_t mc_pool_size;
3138
3139         /*
3140          * If a free entry is available at the end of the pool, just
3141          * increment the number of recorded multicast addresses.
3142          */
3143         if ((port->mc_addr_nb % MCAST_POOL_INC) != 0) {
3144                 port->mc_addr_nb++;
3145                 return 0;
3146         }
3147
3148         /*
3149          * [re]allocate a pool with MCAST_POOL_INC more entries.
3150          * The previous test guarantees that port->mc_addr_nb is a multiple
3151          * of MCAST_POOL_INC.
3152          */
3153         mc_pool_size = sizeof(struct ether_addr) * (port->mc_addr_nb +
3154                                                     MCAST_POOL_INC);
3155         mc_pool = (struct ether_addr *) realloc(port->mc_addr_pool,
3156                                                 mc_pool_size);
3157         if (mc_pool == NULL) {
3158                 printf("allocation of pool of %u multicast addresses failed\n",
3159                        port->mc_addr_nb + MCAST_POOL_INC);
3160                 return -ENOMEM;
3161         }
3162
3163         port->mc_addr_pool = mc_pool;
3164         port->mc_addr_nb++;
3165         return 0;
3166
3167 }
3168
3169 static void
3170 mcast_addr_pool_remove(struct rte_port *port, uint32_t addr_idx)
3171 {
3172         port->mc_addr_nb--;
3173         if (addr_idx == port->mc_addr_nb) {
3174                 /* No need to recompact the set of multicast addressses. */
3175                 if (port->mc_addr_nb == 0) {
3176                         /* free the pool of multicast addresses. */
3177                         free(port->mc_addr_pool);
3178                         port->mc_addr_pool = NULL;
3179                 }
3180                 return;
3181         }
3182         memmove(&port->mc_addr_pool[addr_idx],
3183                 &port->mc_addr_pool[addr_idx + 1],
3184                 sizeof(struct ether_addr) * (port->mc_addr_nb - addr_idx));
3185 }
3186
3187 static void
3188 eth_port_multicast_addr_list_set(uint8_t port_id)
3189 {
3190         struct rte_port *port;
3191         int diag;
3192
3193         port = &ports[port_id];
3194         diag = rte_eth_dev_set_mc_addr_list(port_id, port->mc_addr_pool,
3195                                             port->mc_addr_nb);
3196         if (diag == 0)
3197                 return;
3198         printf("rte_eth_dev_set_mc_addr_list(port=%d, nb=%u) failed. diag=%d\n",
3199                port->mc_addr_nb, port_id, -diag);
3200 }
3201
3202 void
3203 mcast_addr_add(uint8_t port_id, struct ether_addr *mc_addr)
3204 {
3205         struct rte_port *port;
3206         uint32_t i;
3207
3208         if (port_id_is_invalid(port_id, ENABLED_WARN))
3209                 return;
3210
3211         port = &ports[port_id];
3212
3213         /*
3214          * Check that the added multicast MAC address is not already recorded
3215          * in the pool of multicast addresses.
3216          */
3217         for (i = 0; i < port->mc_addr_nb; i++) {
3218                 if (is_same_ether_addr(mc_addr, &port->mc_addr_pool[i])) {
3219                         printf("multicast address already filtered by port\n");
3220                         return;
3221                 }
3222         }
3223
3224         if (mcast_addr_pool_extend(port) != 0)
3225                 return;
3226         ether_addr_copy(mc_addr, &port->mc_addr_pool[i]);
3227         eth_port_multicast_addr_list_set(port_id);
3228 }
3229
3230 void
3231 mcast_addr_remove(uint8_t port_id, struct ether_addr *mc_addr)
3232 {
3233         struct rte_port *port;
3234         uint32_t i;
3235
3236         if (port_id_is_invalid(port_id, ENABLED_WARN))
3237                 return;
3238
3239         port = &ports[port_id];
3240
3241         /*
3242          * Search the pool of multicast MAC addresses for the removed address.
3243          */
3244         for (i = 0; i < port->mc_addr_nb; i++) {
3245                 if (is_same_ether_addr(mc_addr, &port->mc_addr_pool[i]))
3246                         break;
3247         }
3248         if (i == port->mc_addr_nb) {
3249                 printf("multicast address not filtered by port %d\n", port_id);
3250                 return;
3251         }
3252
3253         mcast_addr_pool_remove(port, i);
3254         eth_port_multicast_addr_list_set(port_id);
3255 }
3256
3257 void
3258 port_dcb_info_display(uint8_t port_id)
3259 {
3260         struct rte_eth_dcb_info dcb_info;
3261         uint16_t i;
3262         int ret;
3263         static const char *border = "================";
3264
3265         if (port_id_is_invalid(port_id, ENABLED_WARN))
3266                 return;
3267
3268         ret = rte_eth_dev_get_dcb_info(port_id, &dcb_info);
3269         if (ret) {
3270                 printf("\n Failed to get dcb infos on port %-2d\n",
3271                         port_id);
3272                 return;
3273         }
3274         printf("\n  %s DCB infos for port %-2d  %s\n", border, port_id, border);
3275         printf("  TC NUMBER: %d\n", dcb_info.nb_tcs);
3276         printf("\n  TC :        ");
3277         for (i = 0; i < dcb_info.nb_tcs; i++)
3278                 printf("\t%4d", i);
3279         printf("\n  Priority :  ");
3280         for (i = 0; i < dcb_info.nb_tcs; i++)
3281                 printf("\t%4d", dcb_info.prio_tc[i]);
3282         printf("\n  BW percent :");
3283         for (i = 0; i < dcb_info.nb_tcs; i++)
3284                 printf("\t%4d%%", dcb_info.tc_bws[i]);
3285         printf("\n  RXQ base :  ");
3286         for (i = 0; i < dcb_info.nb_tcs; i++)
3287                 printf("\t%4d", dcb_info.tc_queue.tc_rxq[0][i].base);
3288         printf("\n  RXQ number :");
3289         for (i = 0; i < dcb_info.nb_tcs; i++)
3290                 printf("\t%4d", dcb_info.tc_queue.tc_rxq[0][i].nb_queue);
3291         printf("\n  TXQ base :  ");
3292         for (i = 0; i < dcb_info.nb_tcs; i++)
3293                 printf("\t%4d", dcb_info.tc_queue.tc_txq[0][i].base);
3294         printf("\n  TXQ number :");
3295         for (i = 0; i < dcb_info.nb_tcs; i++)
3296                 printf("\t%4d", dcb_info.tc_queue.tc_txq[0][i].nb_queue);
3297         printf("\n");
3298 }
3299
3300 uint8_t *
3301 open_ddp_package_file(const char *file_path, uint32_t *size)
3302 {
3303         FILE *fh = fopen(file_path, "rb");
3304         uint32_t pkg_size;
3305         uint8_t *buf = NULL;
3306         int ret = 0;
3307
3308         if (size)
3309                 *size = 0;
3310
3311         if (fh == NULL) {
3312                 printf("%s: Failed to open %s\n", __func__, file_path);
3313                 return buf;
3314         }
3315
3316         ret = fseek(fh, 0, SEEK_END);
3317         if (ret < 0) {
3318                 fclose(fh);
3319                 printf("%s: File operations failed\n", __func__);
3320                 return buf;
3321         }
3322
3323         pkg_size = ftell(fh);
3324
3325         buf = (uint8_t *)malloc(pkg_size);
3326         if (!buf) {
3327                 fclose(fh);
3328                 printf("%s: Failed to malloc memory\n", __func__);
3329                 return buf;
3330         }
3331
3332         ret = fseek(fh, 0, SEEK_SET);
3333         if (ret < 0) {
3334                 fclose(fh);
3335                 printf("%s: File seek operation failed\n", __func__);
3336                 close_ddp_package_file(buf);
3337                 return NULL;
3338         }
3339
3340         ret = fread(buf, 1, pkg_size, fh);
3341         if (ret < 0) {
3342                 fclose(fh);
3343                 printf("%s: File read operation failed\n", __func__);
3344                 close_ddp_package_file(buf);
3345                 return NULL;
3346         }
3347
3348         if (size)
3349                 *size = pkg_size;
3350
3351         fclose(fh);
3352
3353         return buf;
3354 }
3355
3356 int
3357 save_ddp_package_file(const char *file_path, uint8_t *buf, uint32_t size)
3358 {
3359         FILE *fh = fopen(file_path, "wb");
3360
3361         if (fh == NULL) {
3362                 printf("%s: Failed to open %s\n", __func__, file_path);
3363                 return -1;
3364         }
3365
3366         if (fwrite(buf, 1, size, fh) != size) {
3367                 fclose(fh);
3368                 printf("%s: File write operation failed\n", __func__);
3369                 return -1;
3370         }
3371
3372         fclose(fh);
3373
3374         return 0;
3375 }
3376
3377 int
3378 close_ddp_package_file(uint8_t *buf)
3379 {
3380         if (buf) {
3381                 free((void *)buf);
3382                 return 0;
3383         }
3384
3385         return -1;
3386 }