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