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, 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 flow = rte_flow_async_create(port_id, queue_id, &op_attr, pt->table, 2689 pattern, pattern_idx, actions, actions_idx, job, &error); 2690 if (!flow) { 2691 uint32_t flow_id = pf->id; 2692 port_queue_flow_destroy(port_id, queue_id, true, 1, &flow_id); 2693 free(job); 2694 return port_flow_complain(&error); 2695 } 2696 2697 pf->next = port->flow_list; 2698 pf->id = id; 2699 pf->flow = flow; 2700 job->pf = pf; 2701 port->flow_list = pf; 2702 printf("Flow rule #%u creation enqueued\n", pf->id); 2703 return 0; 2704 } 2705 2706 /** Enqueue number of destroy flow rules operations. */ 2707 int 2708 port_queue_flow_destroy(portid_t port_id, queueid_t queue_id, 2709 bool postpone, uint32_t n, const uint32_t *rule) 2710 { 2711 struct rte_flow_op_attr op_attr = { .postpone = postpone }; 2712 struct rte_port *port; 2713 struct port_flow **tmp; 2714 int ret = 0; 2715 struct queue_job *job; 2716 2717 if (port_id_is_invalid(port_id, ENABLED_WARN) || 2718 port_id == (portid_t)RTE_PORT_ALL) 2719 return -EINVAL; 2720 port = &ports[port_id]; 2721 2722 if (queue_id >= port->queue_nb) { 2723 printf("Queue #%u is invalid\n", queue_id); 2724 return -EINVAL; 2725 } 2726 2727 tmp = &port->flow_list; 2728 while (*tmp) { 2729 uint32_t i; 2730 2731 for (i = 0; i != n; ++i) { 2732 struct rte_flow_error error; 2733 struct port_flow *pf = *tmp; 2734 2735 if (rule[i] != pf->id) 2736 continue; 2737 /* 2738 * Poisoning to make sure PMD 2739 * update it in case of error. 2740 */ 2741 memset(&error, 0x33, sizeof(error)); 2742 job = calloc(1, sizeof(*job)); 2743 if (!job) { 2744 printf("Queue flow destroy job allocate failed\n"); 2745 return -ENOMEM; 2746 } 2747 job->type = QUEUE_JOB_TYPE_FLOW_DESTROY; 2748 job->pf = pf; 2749 2750 if (rte_flow_async_destroy(port_id, queue_id, &op_attr, 2751 pf->flow, job, &error)) { 2752 free(job); 2753 ret = port_flow_complain(&error); 2754 continue; 2755 } 2756 printf("Flow rule #%u destruction enqueued\n", pf->id); 2757 *tmp = pf->next; 2758 break; 2759 } 2760 if (i == n) 2761 tmp = &(*tmp)->next; 2762 } 2763 return ret; 2764 } 2765 2766 /** Enqueue indirect action create operation. */ 2767 int 2768 port_queue_action_handle_create(portid_t port_id, uint32_t queue_id, 2769 bool postpone, uint32_t id, 2770 const struct rte_flow_indir_action_conf *conf, 2771 const struct rte_flow_action *action) 2772 { 2773 const struct rte_flow_op_attr attr = { .postpone = postpone}; 2774 struct rte_port *port; 2775 struct port_indirect_action *pia; 2776 int ret; 2777 struct rte_flow_error error; 2778 struct queue_job *job; 2779 2780 ret = action_alloc(port_id, id, &pia); 2781 if (ret) 2782 return ret; 2783 2784 port = &ports[port_id]; 2785 if (queue_id >= port->queue_nb) { 2786 printf("Queue #%u is invalid\n", queue_id); 2787 return -EINVAL; 2788 } 2789 job = calloc(1, sizeof(*job)); 2790 if (!job) { 2791 printf("Queue action create job allocate failed\n"); 2792 return -ENOMEM; 2793 } 2794 job->type = QUEUE_JOB_TYPE_ACTION_CREATE; 2795 job->pia = pia; 2796 2797 if (action->type == RTE_FLOW_ACTION_TYPE_AGE) { 2798 struct rte_flow_action_age *age = 2799 (struct rte_flow_action_age *)(uintptr_t)(action->conf); 2800 2801 pia->age_type = ACTION_AGE_CONTEXT_TYPE_INDIRECT_ACTION; 2802 age->context = &pia->age_type; 2803 } 2804 /* Poisoning to make sure PMDs update it in case of error. */ 2805 memset(&error, 0x88, sizeof(error)); 2806 pia->handle = rte_flow_async_action_handle_create(port_id, queue_id, 2807 &attr, conf, action, job, &error); 2808 if (!pia->handle) { 2809 uint32_t destroy_id = pia->id; 2810 port_queue_action_handle_destroy(port_id, queue_id, 2811 postpone, 1, &destroy_id); 2812 free(job); 2813 return port_flow_complain(&error); 2814 } 2815 pia->type = action->type; 2816 printf("Indirect action #%u creation queued\n", pia->id); 2817 return 0; 2818 } 2819 2820 /** Enqueue indirect action destroy operation. */ 2821 int 2822 port_queue_action_handle_destroy(portid_t port_id, 2823 uint32_t queue_id, bool postpone, 2824 uint32_t n, const uint32_t *actions) 2825 { 2826 const struct rte_flow_op_attr attr = { .postpone = postpone}; 2827 struct rte_port *port; 2828 struct port_indirect_action **tmp; 2829 int ret = 0; 2830 struct queue_job *job; 2831 2832 if (port_id_is_invalid(port_id, ENABLED_WARN) || 2833 port_id == (portid_t)RTE_PORT_ALL) 2834 return -EINVAL; 2835 port = &ports[port_id]; 2836 2837 if (queue_id >= port->queue_nb) { 2838 printf("Queue #%u is invalid\n", queue_id); 2839 return -EINVAL; 2840 } 2841 2842 tmp = &port->actions_list; 2843 while (*tmp) { 2844 uint32_t i; 2845 2846 for (i = 0; i != n; ++i) { 2847 struct rte_flow_error error; 2848 struct port_indirect_action *pia = *tmp; 2849 2850 if (actions[i] != pia->id) 2851 continue; 2852 /* 2853 * Poisoning to make sure PMDs update it in case 2854 * of error. 2855 */ 2856 memset(&error, 0x99, sizeof(error)); 2857 job = calloc(1, sizeof(*job)); 2858 if (!job) { 2859 printf("Queue action destroy job allocate failed\n"); 2860 return -ENOMEM; 2861 } 2862 job->type = QUEUE_JOB_TYPE_ACTION_DESTROY; 2863 job->pia = pia; 2864 2865 if (pia->handle && 2866 rte_flow_async_action_handle_destroy(port_id, 2867 queue_id, &attr, pia->handle, job, &error)) { 2868 ret = port_flow_complain(&error); 2869 continue; 2870 } 2871 *tmp = pia->next; 2872 printf("Indirect action #%u destruction queued\n", 2873 pia->id); 2874 break; 2875 } 2876 if (i == n) 2877 tmp = &(*tmp)->next; 2878 } 2879 return ret; 2880 } 2881 2882 /** Enqueue indirect action update operation. */ 2883 int 2884 port_queue_action_handle_update(portid_t port_id, 2885 uint32_t queue_id, bool postpone, uint32_t id, 2886 const struct rte_flow_action *action) 2887 { 2888 const struct rte_flow_op_attr attr = { .postpone = postpone}; 2889 struct rte_port *port; 2890 struct rte_flow_error error; 2891 struct rte_flow_action_handle *action_handle; 2892 struct queue_job *job; 2893 struct port_indirect_action *pia; 2894 struct rte_flow_update_meter_mark mtr_update; 2895 const void *update; 2896 2897 action_handle = port_action_handle_get_by_id(port_id, id); 2898 if (!action_handle) 2899 return -EINVAL; 2900 2901 port = &ports[port_id]; 2902 if (queue_id >= port->queue_nb) { 2903 printf("Queue #%u is invalid\n", queue_id); 2904 return -EINVAL; 2905 } 2906 2907 job = calloc(1, sizeof(*job)); 2908 if (!job) { 2909 printf("Queue action update job allocate failed\n"); 2910 return -ENOMEM; 2911 } 2912 job->type = QUEUE_JOB_TYPE_ACTION_UPDATE; 2913 2914 pia = action_get_by_id(port_id, id); 2915 if (!pia) { 2916 free(job); 2917 return -EINVAL; 2918 } 2919 2920 switch (pia->type) { 2921 case RTE_FLOW_ACTION_TYPE_AGE: 2922 update = action->conf; 2923 break; 2924 case RTE_FLOW_ACTION_TYPE_METER_MARK: 2925 rte_memcpy(&mtr_update.meter_mark, action->conf, 2926 sizeof(struct rte_flow_action_meter_mark)); 2927 mtr_update.profile_valid = 1; 2928 mtr_update.policy_valid = 1; 2929 mtr_update.color_mode_valid = 1; 2930 mtr_update.init_color_valid = 1; 2931 mtr_update.state_valid = 1; 2932 update = &mtr_update; 2933 break; 2934 default: 2935 update = action; 2936 break; 2937 } 2938 2939 if (rte_flow_async_action_handle_update(port_id, queue_id, &attr, 2940 action_handle, update, job, &error)) { 2941 free(job); 2942 return port_flow_complain(&error); 2943 } 2944 printf("Indirect action #%u update queued\n", id); 2945 return 0; 2946 } 2947 2948 /** Enqueue indirect action query operation. */ 2949 int 2950 port_queue_action_handle_query(portid_t port_id, 2951 uint32_t queue_id, bool postpone, uint32_t id) 2952 { 2953 const struct rte_flow_op_attr attr = { .postpone = postpone}; 2954 struct rte_port *port; 2955 struct rte_flow_error error; 2956 struct rte_flow_action_handle *action_handle; 2957 struct port_indirect_action *pia; 2958 struct queue_job *job; 2959 2960 pia = action_get_by_id(port_id, id); 2961 action_handle = pia ? pia->handle : NULL; 2962 if (!action_handle) 2963 return -EINVAL; 2964 2965 port = &ports[port_id]; 2966 if (queue_id >= port->queue_nb) { 2967 printf("Queue #%u is invalid\n", queue_id); 2968 return -EINVAL; 2969 } 2970 2971 job = calloc(1, sizeof(*job)); 2972 if (!job) { 2973 printf("Queue action update job allocate failed\n"); 2974 return -ENOMEM; 2975 } 2976 job->type = QUEUE_JOB_TYPE_ACTION_QUERY; 2977 job->pia = pia; 2978 2979 if (rte_flow_async_action_handle_query(port_id, queue_id, &attr, 2980 action_handle, &job->query, job, &error)) { 2981 free(job); 2982 return port_flow_complain(&error); 2983 } 2984 printf("Indirect action #%u update queued\n", id); 2985 return 0; 2986 } 2987 2988 /** Push all the queue operations in the queue to the NIC. */ 2989 int 2990 port_queue_flow_push(portid_t port_id, queueid_t queue_id) 2991 { 2992 struct rte_port *port; 2993 struct rte_flow_error error; 2994 int ret = 0; 2995 2996 if (port_id_is_invalid(port_id, ENABLED_WARN) || 2997 port_id == (portid_t)RTE_PORT_ALL) 2998 return -EINVAL; 2999 port = &ports[port_id]; 3000 3001 if (queue_id >= port->queue_nb) { 3002 printf("Queue #%u is invalid\n", queue_id); 3003 return -EINVAL; 3004 } 3005 3006 memset(&error, 0x55, sizeof(error)); 3007 ret = rte_flow_push(port_id, queue_id, &error); 3008 if (ret < 0) { 3009 printf("Failed to push operations in the queue\n"); 3010 return -EINVAL; 3011 } 3012 printf("Queue #%u operations pushed\n", queue_id); 3013 return ret; 3014 } 3015 3016 /** Pull queue operation results from the queue. */ 3017 static int 3018 port_queue_aged_flow_destroy(portid_t port_id, queueid_t queue_id, 3019 const uint32_t *rule, int nb_flows) 3020 { 3021 struct rte_port *port = &ports[port_id]; 3022 struct rte_flow_op_result *res; 3023 struct rte_flow_error error; 3024 uint32_t n = nb_flows; 3025 int ret = 0; 3026 int i; 3027 3028 res = calloc(port->queue_sz, sizeof(struct rte_flow_op_result)); 3029 if (!res) { 3030 printf("Failed to allocate memory for pulled results\n"); 3031 return -ENOMEM; 3032 } 3033 3034 memset(&error, 0x66, sizeof(error)); 3035 while (nb_flows > 0) { 3036 int success = 0; 3037 3038 if (n > port->queue_sz) 3039 n = port->queue_sz; 3040 ret = port_queue_flow_destroy(port_id, queue_id, true, n, rule); 3041 if (ret < 0) { 3042 free(res); 3043 return ret; 3044 } 3045 ret = rte_flow_push(port_id, queue_id, &error); 3046 if (ret < 0) { 3047 printf("Failed to push operations in the queue: %s\n", 3048 strerror(-ret)); 3049 free(res); 3050 return ret; 3051 } 3052 while (success < nb_flows) { 3053 ret = rte_flow_pull(port_id, queue_id, res, 3054 port->queue_sz, &error); 3055 if (ret < 0) { 3056 printf("Failed to pull a operation results: %s\n", 3057 strerror(-ret)); 3058 free(res); 3059 return ret; 3060 } 3061 3062 for (i = 0; i < ret; i++) { 3063 if (res[i].status == RTE_FLOW_OP_SUCCESS) 3064 success++; 3065 } 3066 } 3067 rule += n; 3068 nb_flows -= n; 3069 n = nb_flows; 3070 } 3071 3072 free(res); 3073 return ret; 3074 } 3075 3076 /** List simply and destroy all aged flows per queue. */ 3077 void 3078 port_queue_flow_aged(portid_t port_id, uint32_t queue_id, uint8_t destroy) 3079 { 3080 void **contexts; 3081 int nb_context, total = 0, idx; 3082 uint32_t *rules = NULL; 3083 struct rte_port *port; 3084 struct rte_flow_error error; 3085 enum age_action_context_type *type; 3086 union { 3087 struct port_flow *pf; 3088 struct port_indirect_action *pia; 3089 } ctx; 3090 3091 if (port_id_is_invalid(port_id, ENABLED_WARN) || 3092 port_id == (portid_t)RTE_PORT_ALL) 3093 return; 3094 port = &ports[port_id]; 3095 if (queue_id >= port->queue_nb) { 3096 printf("Error: queue #%u is invalid\n", queue_id); 3097 return; 3098 } 3099 total = rte_flow_get_q_aged_flows(port_id, queue_id, NULL, 0, &error); 3100 if (total < 0) { 3101 port_flow_complain(&error); 3102 return; 3103 } 3104 printf("Port %u queue %u total aged flows: %d\n", 3105 port_id, queue_id, total); 3106 if (total == 0) 3107 return; 3108 contexts = calloc(total, sizeof(void *)); 3109 if (contexts == NULL) { 3110 printf("Cannot allocate contexts for aged flow\n"); 3111 return; 3112 } 3113 printf("%-20s\tID\tGroup\tPrio\tAttr\n", "Type"); 3114 nb_context = rte_flow_get_q_aged_flows(port_id, queue_id, contexts, 3115 total, &error); 3116 if (nb_context > total) { 3117 printf("Port %u queue %u get aged flows count(%d) > total(%d)\n", 3118 port_id, queue_id, nb_context, total); 3119 free(contexts); 3120 return; 3121 } 3122 if (destroy) { 3123 rules = malloc(sizeof(uint32_t) * nb_context); 3124 if (rules == NULL) 3125 printf("Cannot allocate memory for destroy aged flow\n"); 3126 } 3127 total = 0; 3128 for (idx = 0; idx < nb_context; idx++) { 3129 if (!contexts[idx]) { 3130 printf("Error: get Null context in port %u queue %u\n", 3131 port_id, queue_id); 3132 continue; 3133 } 3134 type = (enum age_action_context_type *)contexts[idx]; 3135 switch (*type) { 3136 case ACTION_AGE_CONTEXT_TYPE_FLOW: 3137 ctx.pf = container_of(type, struct port_flow, age_type); 3138 printf("%-20s\t%" PRIu32 "\t%" PRIu32 "\t%" PRIu32 3139 "\t%c%c%c\t\n", 3140 "Flow", 3141 ctx.pf->id, 3142 ctx.pf->rule.attr->group, 3143 ctx.pf->rule.attr->priority, 3144 ctx.pf->rule.attr->ingress ? 'i' : '-', 3145 ctx.pf->rule.attr->egress ? 'e' : '-', 3146 ctx.pf->rule.attr->transfer ? 't' : '-'); 3147 if (rules != NULL) { 3148 rules[total] = ctx.pf->id; 3149 total++; 3150 } 3151 break; 3152 case ACTION_AGE_CONTEXT_TYPE_INDIRECT_ACTION: 3153 ctx.pia = container_of(type, 3154 struct port_indirect_action, 3155 age_type); 3156 printf("%-20s\t%" PRIu32 "\n", "Indirect action", 3157 ctx.pia->id); 3158 break; 3159 default: 3160 printf("Error: invalid context type %u\n", port_id); 3161 break; 3162 } 3163 } 3164 if (rules != NULL) { 3165 port_queue_aged_flow_destroy(port_id, queue_id, rules, total); 3166 free(rules); 3167 } 3168 printf("\n%d flows destroyed\n", total); 3169 free(contexts); 3170 } 3171 3172 /** Pull queue operation results from the queue. */ 3173 int 3174 port_queue_flow_pull(portid_t port_id, queueid_t queue_id) 3175 { 3176 struct rte_port *port; 3177 struct rte_flow_op_result *res; 3178 struct rte_flow_error error; 3179 int ret = 0; 3180 int success = 0; 3181 int i; 3182 struct queue_job *job; 3183 3184 if (port_id_is_invalid(port_id, ENABLED_WARN) || 3185 port_id == (portid_t)RTE_PORT_ALL) 3186 return -EINVAL; 3187 port = &ports[port_id]; 3188 3189 if (queue_id >= port->queue_nb) { 3190 printf("Queue #%u is invalid\n", queue_id); 3191 return -EINVAL; 3192 } 3193 3194 res = calloc(port->queue_sz, sizeof(struct rte_flow_op_result)); 3195 if (!res) { 3196 printf("Failed to allocate memory for pulled results\n"); 3197 return -ENOMEM; 3198 } 3199 3200 memset(&error, 0x66, sizeof(error)); 3201 ret = rte_flow_pull(port_id, queue_id, res, 3202 port->queue_sz, &error); 3203 if (ret < 0) { 3204 printf("Failed to pull a operation results\n"); 3205 free(res); 3206 return -EINVAL; 3207 } 3208 3209 for (i = 0; i < ret; i++) { 3210 if (res[i].status == RTE_FLOW_OP_SUCCESS) 3211 success++; 3212 job = (struct queue_job *)res[i].user_data; 3213 if (job->type == QUEUE_JOB_TYPE_FLOW_DESTROY) 3214 free(job->pf); 3215 else if (job->type == QUEUE_JOB_TYPE_ACTION_DESTROY) 3216 free(job->pia); 3217 else if (job->type == QUEUE_JOB_TYPE_ACTION_QUERY) 3218 port_action_handle_query_dump(job->pia->type, &job->query); 3219 free(job); 3220 } 3221 printf("Queue #%u pulled %u operations (%u failed, %u succeeded)\n", 3222 queue_id, ret, ret - success, success); 3223 free(res); 3224 return ret; 3225 } 3226 3227 /** Create flow rule. */ 3228 int 3229 port_flow_create(portid_t port_id, 3230 const struct rte_flow_attr *attr, 3231 const struct rte_flow_item *pattern, 3232 const struct rte_flow_action *actions, 3233 const struct tunnel_ops *tunnel_ops) 3234 { 3235 struct rte_flow *flow; 3236 struct rte_port *port; 3237 struct port_flow *pf; 3238 uint32_t id = 0; 3239 struct rte_flow_error error; 3240 struct port_flow_tunnel *pft = NULL; 3241 struct rte_flow_action_age *age = age_action_get(actions); 3242 3243 port = &ports[port_id]; 3244 if (port->flow_list) { 3245 if (port->flow_list->id == UINT32_MAX) { 3246 fprintf(stderr, 3247 "Highest rule ID is already assigned, delete it first"); 3248 return -ENOMEM; 3249 } 3250 id = port->flow_list->id + 1; 3251 } 3252 if (tunnel_ops->enabled) { 3253 pft = port_flow_tunnel_offload_cmd_prep(port_id, pattern, 3254 actions, tunnel_ops); 3255 if (!pft) 3256 return -ENOENT; 3257 if (pft->items) 3258 pattern = pft->items; 3259 if (pft->actions) 3260 actions = pft->actions; 3261 } 3262 pf = port_flow_new(attr, pattern, actions, &error); 3263 if (!pf) 3264 return port_flow_complain(&error); 3265 if (age) { 3266 pf->age_type = ACTION_AGE_CONTEXT_TYPE_FLOW; 3267 age->context = &pf->age_type; 3268 } 3269 /* Poisoning to make sure PMDs update it in case of error. */ 3270 memset(&error, 0x22, sizeof(error)); 3271 flow = rte_flow_create(port_id, attr, pattern, actions, &error); 3272 if (!flow) { 3273 if (tunnel_ops->enabled) 3274 port_flow_tunnel_offload_cmd_release(port_id, 3275 tunnel_ops, pft); 3276 free(pf); 3277 return port_flow_complain(&error); 3278 } 3279 pf->next = port->flow_list; 3280 pf->id = id; 3281 pf->flow = flow; 3282 port->flow_list = pf; 3283 if (tunnel_ops->enabled) 3284 port_flow_tunnel_offload_cmd_release(port_id, tunnel_ops, pft); 3285 printf("Flow rule #%u created\n", pf->id); 3286 return 0; 3287 } 3288 3289 /** Destroy a number of flow rules. */ 3290 int 3291 port_flow_destroy(portid_t port_id, uint32_t n, const uint32_t *rule) 3292 { 3293 struct rte_port *port; 3294 struct port_flow **tmp; 3295 int ret = 0; 3296 3297 if (port_id_is_invalid(port_id, ENABLED_WARN) || 3298 port_id == (portid_t)RTE_PORT_ALL) 3299 return -EINVAL; 3300 port = &ports[port_id]; 3301 tmp = &port->flow_list; 3302 while (*tmp) { 3303 uint32_t i; 3304 3305 for (i = 0; i != n; ++i) { 3306 struct rte_flow_error error; 3307 struct port_flow *pf = *tmp; 3308 3309 if (rule[i] != pf->id) 3310 continue; 3311 /* 3312 * Poisoning to make sure PMDs update it in case 3313 * of error. 3314 */ 3315 memset(&error, 0x33, sizeof(error)); 3316 if (rte_flow_destroy(port_id, pf->flow, &error)) { 3317 ret = port_flow_complain(&error); 3318 continue; 3319 } 3320 printf("Flow rule #%u destroyed\n", pf->id); 3321 *tmp = pf->next; 3322 free(pf); 3323 break; 3324 } 3325 if (i == n) 3326 tmp = &(*tmp)->next; 3327 } 3328 return ret; 3329 } 3330 3331 /** Remove all flow rules. */ 3332 int 3333 port_flow_flush(portid_t port_id) 3334 { 3335 struct rte_flow_error error; 3336 struct rte_port *port; 3337 int ret = 0; 3338 3339 if (port_id_is_invalid(port_id, ENABLED_WARN) || 3340 port_id == (portid_t)RTE_PORT_ALL) 3341 return -EINVAL; 3342 3343 port = &ports[port_id]; 3344 3345 if (port->flow_list == NULL) 3346 return ret; 3347 3348 /* Poisoning to make sure PMDs update it in case of error. */ 3349 memset(&error, 0x44, sizeof(error)); 3350 if (rte_flow_flush(port_id, &error)) { 3351 port_flow_complain(&error); 3352 } 3353 3354 while (port->flow_list) { 3355 struct port_flow *pf = port->flow_list->next; 3356 3357 free(port->flow_list); 3358 port->flow_list = pf; 3359 } 3360 return ret; 3361 } 3362 3363 /** Dump flow rules. */ 3364 int 3365 port_flow_dump(portid_t port_id, bool dump_all, uint32_t rule_id, 3366 const char *file_name) 3367 { 3368 int ret = 0; 3369 FILE *file = stdout; 3370 struct rte_flow_error error; 3371 struct rte_port *port; 3372 struct port_flow *pflow; 3373 struct rte_flow *tmpFlow = NULL; 3374 bool found = false; 3375 3376 if (port_id_is_invalid(port_id, ENABLED_WARN) || 3377 port_id == (portid_t)RTE_PORT_ALL) 3378 return -EINVAL; 3379 3380 if (!dump_all) { 3381 port = &ports[port_id]; 3382 pflow = port->flow_list; 3383 while (pflow) { 3384 if (rule_id != pflow->id) { 3385 pflow = pflow->next; 3386 } else { 3387 tmpFlow = pflow->flow; 3388 if (tmpFlow) 3389 found = true; 3390 break; 3391 } 3392 } 3393 if (found == false) { 3394 fprintf(stderr, "Failed to dump to flow %d\n", rule_id); 3395 return -EINVAL; 3396 } 3397 } 3398 3399 if (file_name && strlen(file_name)) { 3400 file = fopen(file_name, "w"); 3401 if (!file) { 3402 fprintf(stderr, "Failed to create file %s: %s\n", 3403 file_name, strerror(errno)); 3404 return -errno; 3405 } 3406 } 3407 3408 if (!dump_all) 3409 ret = rte_flow_dev_dump(port_id, tmpFlow, file, &error); 3410 else 3411 ret = rte_flow_dev_dump(port_id, NULL, file, &error); 3412 if (ret) { 3413 port_flow_complain(&error); 3414 fprintf(stderr, "Failed to dump flow: %s\n", strerror(-ret)); 3415 } else 3416 printf("Flow dump finished\n"); 3417 if (file_name && strlen(file_name)) 3418 fclose(file); 3419 return ret; 3420 } 3421 3422 /** Query a flow rule. */ 3423 int 3424 port_flow_query(portid_t port_id, uint32_t rule, 3425 const struct rte_flow_action *action) 3426 { 3427 struct rte_flow_error error; 3428 struct rte_port *port; 3429 struct port_flow *pf; 3430 const char *name; 3431 union { 3432 struct rte_flow_query_count count; 3433 struct rte_flow_action_rss rss_conf; 3434 struct rte_flow_query_age age; 3435 } query; 3436 int ret; 3437 3438 if (port_id_is_invalid(port_id, ENABLED_WARN) || 3439 port_id == (portid_t)RTE_PORT_ALL) 3440 return -EINVAL; 3441 port = &ports[port_id]; 3442 for (pf = port->flow_list; pf; pf = pf->next) 3443 if (pf->id == rule) 3444 break; 3445 if (!pf) { 3446 fprintf(stderr, "Flow rule #%u not found\n", rule); 3447 return -ENOENT; 3448 } 3449 ret = rte_flow_conv(RTE_FLOW_CONV_OP_ACTION_NAME_PTR, 3450 &name, sizeof(name), 3451 (void *)(uintptr_t)action->type, &error); 3452 if (ret < 0) 3453 return port_flow_complain(&error); 3454 switch (action->type) { 3455 case RTE_FLOW_ACTION_TYPE_COUNT: 3456 case RTE_FLOW_ACTION_TYPE_RSS: 3457 case RTE_FLOW_ACTION_TYPE_AGE: 3458 break; 3459 default: 3460 fprintf(stderr, "Cannot query action type %d (%s)\n", 3461 action->type, name); 3462 return -ENOTSUP; 3463 } 3464 /* Poisoning to make sure PMDs update it in case of error. */ 3465 memset(&error, 0x55, sizeof(error)); 3466 memset(&query, 0, sizeof(query)); 3467 if (rte_flow_query(port_id, pf->flow, action, &query, &error)) 3468 return port_flow_complain(&error); 3469 switch (action->type) { 3470 case RTE_FLOW_ACTION_TYPE_COUNT: 3471 printf("%s:\n" 3472 " hits_set: %u\n" 3473 " bytes_set: %u\n" 3474 " hits: %" PRIu64 "\n" 3475 " bytes: %" PRIu64 "\n", 3476 name, 3477 query.count.hits_set, 3478 query.count.bytes_set, 3479 query.count.hits, 3480 query.count.bytes); 3481 break; 3482 case RTE_FLOW_ACTION_TYPE_RSS: 3483 rss_config_display(&query.rss_conf); 3484 break; 3485 case RTE_FLOW_ACTION_TYPE_AGE: 3486 printf("%s:\n" 3487 " aged: %u\n" 3488 " sec_since_last_hit_valid: %u\n" 3489 " sec_since_last_hit: %" PRIu32 "\n", 3490 name, 3491 query.age.aged, 3492 query.age.sec_since_last_hit_valid, 3493 query.age.sec_since_last_hit); 3494 break; 3495 default: 3496 fprintf(stderr, 3497 "Cannot display result for action type %d (%s)\n", 3498 action->type, name); 3499 break; 3500 } 3501 return 0; 3502 } 3503 3504 /** List simply and destroy all aged flows. */ 3505 void 3506 port_flow_aged(portid_t port_id, uint8_t destroy) 3507 { 3508 void **contexts; 3509 int nb_context, total = 0, idx; 3510 struct rte_flow_error error; 3511 enum age_action_context_type *type; 3512 union { 3513 struct port_flow *pf; 3514 struct port_indirect_action *pia; 3515 } ctx; 3516 3517 if (port_id_is_invalid(port_id, ENABLED_WARN) || 3518 port_id == (portid_t)RTE_PORT_ALL) 3519 return; 3520 total = rte_flow_get_aged_flows(port_id, NULL, 0, &error); 3521 printf("Port %u total aged flows: %d\n", port_id, total); 3522 if (total < 0) { 3523 port_flow_complain(&error); 3524 return; 3525 } 3526 if (total == 0) 3527 return; 3528 contexts = malloc(sizeof(void *) * total); 3529 if (contexts == NULL) { 3530 fprintf(stderr, "Cannot allocate contexts for aged flow\n"); 3531 return; 3532 } 3533 printf("%-20s\tID\tGroup\tPrio\tAttr\n", "Type"); 3534 nb_context = rte_flow_get_aged_flows(port_id, contexts, total, &error); 3535 if (nb_context != total) { 3536 fprintf(stderr, 3537 "Port:%d get aged flows count(%d) != total(%d)\n", 3538 port_id, nb_context, total); 3539 free(contexts); 3540 return; 3541 } 3542 total = 0; 3543 for (idx = 0; idx < nb_context; idx++) { 3544 if (!contexts[idx]) { 3545 fprintf(stderr, "Error: get Null context in port %u\n", 3546 port_id); 3547 continue; 3548 } 3549 type = (enum age_action_context_type *)contexts[idx]; 3550 switch (*type) { 3551 case ACTION_AGE_CONTEXT_TYPE_FLOW: 3552 ctx.pf = container_of(type, struct port_flow, age_type); 3553 printf("%-20s\t%" PRIu32 "\t%" PRIu32 "\t%" PRIu32 3554 "\t%c%c%c\t\n", 3555 "Flow", 3556 ctx.pf->id, 3557 ctx.pf->rule.attr->group, 3558 ctx.pf->rule.attr->priority, 3559 ctx.pf->rule.attr->ingress ? 'i' : '-', 3560 ctx.pf->rule.attr->egress ? 'e' : '-', 3561 ctx.pf->rule.attr->transfer ? 't' : '-'); 3562 if (destroy && !port_flow_destroy(port_id, 1, 3563 &ctx.pf->id)) 3564 total++; 3565 break; 3566 case ACTION_AGE_CONTEXT_TYPE_INDIRECT_ACTION: 3567 ctx.pia = container_of(type, 3568 struct port_indirect_action, age_type); 3569 printf("%-20s\t%" PRIu32 "\n", "Indirect action", 3570 ctx.pia->id); 3571 break; 3572 default: 3573 fprintf(stderr, "Error: invalid context type %u\n", 3574 port_id); 3575 break; 3576 } 3577 } 3578 printf("\n%d flows destroyed\n", total); 3579 free(contexts); 3580 } 3581 3582 /** List flow rules. */ 3583 void 3584 port_flow_list(portid_t port_id, uint32_t n, const uint32_t *group) 3585 { 3586 struct rte_port *port; 3587 struct port_flow *pf; 3588 struct port_flow *list = NULL; 3589 uint32_t i; 3590 3591 if (port_id_is_invalid(port_id, ENABLED_WARN) || 3592 port_id == (portid_t)RTE_PORT_ALL) 3593 return; 3594 port = &ports[port_id]; 3595 if (!port->flow_list) 3596 return; 3597 /* Sort flows by group, priority and ID. */ 3598 for (pf = port->flow_list; pf != NULL; pf = pf->next) { 3599 struct port_flow **tmp; 3600 const struct rte_flow_attr *curr = pf->rule.attr; 3601 3602 if (n) { 3603 /* Filter out unwanted groups. */ 3604 for (i = 0; i != n; ++i) 3605 if (curr->group == group[i]) 3606 break; 3607 if (i == n) 3608 continue; 3609 } 3610 for (tmp = &list; *tmp; tmp = &(*tmp)->tmp) { 3611 const struct rte_flow_attr *comp = (*tmp)->rule.attr; 3612 3613 if (curr->group > comp->group || 3614 (curr->group == comp->group && 3615 curr->priority > comp->priority) || 3616 (curr->group == comp->group && 3617 curr->priority == comp->priority && 3618 pf->id > (*tmp)->id)) 3619 continue; 3620 break; 3621 } 3622 pf->tmp = *tmp; 3623 *tmp = pf; 3624 } 3625 printf("ID\tGroup\tPrio\tAttr\tRule\n"); 3626 for (pf = list; pf != NULL; pf = pf->tmp) { 3627 const struct rte_flow_item *item = pf->rule.pattern; 3628 const struct rte_flow_action *action = pf->rule.actions; 3629 const char *name; 3630 3631 printf("%" PRIu32 "\t%" PRIu32 "\t%" PRIu32 "\t%c%c%c\t", 3632 pf->id, 3633 pf->rule.attr->group, 3634 pf->rule.attr->priority, 3635 pf->rule.attr->ingress ? 'i' : '-', 3636 pf->rule.attr->egress ? 'e' : '-', 3637 pf->rule.attr->transfer ? 't' : '-'); 3638 while (item->type != RTE_FLOW_ITEM_TYPE_END) { 3639 if ((uint32_t)item->type > INT_MAX) 3640 name = "PMD_INTERNAL"; 3641 else if (rte_flow_conv(RTE_FLOW_CONV_OP_ITEM_NAME_PTR, 3642 &name, sizeof(name), 3643 (void *)(uintptr_t)item->type, 3644 NULL) <= 0) 3645 name = "[UNKNOWN]"; 3646 if (item->type != RTE_FLOW_ITEM_TYPE_VOID) 3647 printf("%s ", name); 3648 ++item; 3649 } 3650 printf("=>"); 3651 while (action->type != RTE_FLOW_ACTION_TYPE_END) { 3652 if ((uint32_t)action->type > INT_MAX) 3653 name = "PMD_INTERNAL"; 3654 else if (rte_flow_conv(RTE_FLOW_CONV_OP_ACTION_NAME_PTR, 3655 &name, sizeof(name), 3656 (void *)(uintptr_t)action->type, 3657 NULL) <= 0) 3658 name = "[UNKNOWN]"; 3659 if (action->type != RTE_FLOW_ACTION_TYPE_VOID) 3660 printf(" %s", name); 3661 ++action; 3662 } 3663 printf("\n"); 3664 } 3665 } 3666 3667 /** Restrict ingress traffic to the defined flow rules. */ 3668 int 3669 port_flow_isolate(portid_t port_id, int set) 3670 { 3671 struct rte_flow_error error; 3672 3673 /* Poisoning to make sure PMDs update it in case of error. */ 3674 memset(&error, 0x66, sizeof(error)); 3675 if (rte_flow_isolate(port_id, set, &error)) 3676 return port_flow_complain(&error); 3677 printf("Ingress traffic on port %u is %s to the defined flow rules\n", 3678 port_id, 3679 set ? "now restricted" : "not restricted anymore"); 3680 return 0; 3681 } 3682 3683 /* 3684 * RX/TX ring descriptors display functions. 3685 */ 3686 int 3687 rx_queue_id_is_invalid(queueid_t rxq_id) 3688 { 3689 if (rxq_id < nb_rxq) 3690 return 0; 3691 fprintf(stderr, "Invalid RX queue %d (must be < nb_rxq=%d)\n", 3692 rxq_id, nb_rxq); 3693 return 1; 3694 } 3695 3696 int 3697 tx_queue_id_is_invalid(queueid_t txq_id) 3698 { 3699 if (txq_id < nb_txq) 3700 return 0; 3701 fprintf(stderr, "Invalid TX queue %d (must be < nb_txq=%d)\n", 3702 txq_id, nb_txq); 3703 return 1; 3704 } 3705 3706 static int 3707 get_rx_ring_size(portid_t port_id, queueid_t rxq_id, uint16_t *ring_size) 3708 { 3709 struct rte_port *port = &ports[port_id]; 3710 struct rte_eth_rxq_info rx_qinfo; 3711 int ret; 3712 3713 ret = rte_eth_rx_queue_info_get(port_id, rxq_id, &rx_qinfo); 3714 if (ret == 0) { 3715 *ring_size = rx_qinfo.nb_desc; 3716 return ret; 3717 } 3718 3719 if (ret != -ENOTSUP) 3720 return ret; 3721 /* 3722 * If the rte_eth_rx_queue_info_get is not support for this PMD, 3723 * ring_size stored in testpmd will be used for validity verification. 3724 * When configure the rxq by rte_eth_rx_queue_setup with nb_rx_desc 3725 * being 0, it will use a default value provided by PMDs to setup this 3726 * rxq. If the default value is 0, it will use the 3727 * RTE_ETH_DEV_FALLBACK_RX_RINGSIZE to setup this rxq. 3728 */ 3729 if (port->nb_rx_desc[rxq_id]) 3730 *ring_size = port->nb_rx_desc[rxq_id]; 3731 else if (port->dev_info.default_rxportconf.ring_size) 3732 *ring_size = port->dev_info.default_rxportconf.ring_size; 3733 else 3734 *ring_size = RTE_ETH_DEV_FALLBACK_RX_RINGSIZE; 3735 return 0; 3736 } 3737 3738 static int 3739 get_tx_ring_size(portid_t port_id, queueid_t txq_id, uint16_t *ring_size) 3740 { 3741 struct rte_port *port = &ports[port_id]; 3742 struct rte_eth_txq_info tx_qinfo; 3743 int ret; 3744 3745 ret = rte_eth_tx_queue_info_get(port_id, txq_id, &tx_qinfo); 3746 if (ret == 0) { 3747 *ring_size = tx_qinfo.nb_desc; 3748 return ret; 3749 } 3750 3751 if (ret != -ENOTSUP) 3752 return ret; 3753 /* 3754 * If the rte_eth_tx_queue_info_get is not support for this PMD, 3755 * ring_size stored in testpmd will be used for validity verification. 3756 * When configure the txq by rte_eth_tx_queue_setup with nb_tx_desc 3757 * being 0, it will use a default value provided by PMDs to setup this 3758 * txq. If the default value is 0, it will use the 3759 * RTE_ETH_DEV_FALLBACK_TX_RINGSIZE to setup this txq. 3760 */ 3761 if (port->nb_tx_desc[txq_id]) 3762 *ring_size = port->nb_tx_desc[txq_id]; 3763 else if (port->dev_info.default_txportconf.ring_size) 3764 *ring_size = port->dev_info.default_txportconf.ring_size; 3765 else 3766 *ring_size = RTE_ETH_DEV_FALLBACK_TX_RINGSIZE; 3767 return 0; 3768 } 3769 3770 static int 3771 rx_desc_id_is_invalid(portid_t port_id, queueid_t rxq_id, uint16_t rxdesc_id) 3772 { 3773 uint16_t ring_size; 3774 int ret; 3775 3776 ret = get_rx_ring_size(port_id, rxq_id, &ring_size); 3777 if (ret) 3778 return 1; 3779 3780 if (rxdesc_id < ring_size) 3781 return 0; 3782 3783 fprintf(stderr, "Invalid RX descriptor %u (must be < ring_size=%u)\n", 3784 rxdesc_id, ring_size); 3785 return 1; 3786 } 3787 3788 static int 3789 tx_desc_id_is_invalid(portid_t port_id, queueid_t txq_id, uint16_t txdesc_id) 3790 { 3791 uint16_t ring_size; 3792 int ret; 3793 3794 ret = get_tx_ring_size(port_id, txq_id, &ring_size); 3795 if (ret) 3796 return 1; 3797 3798 if (txdesc_id < ring_size) 3799 return 0; 3800 3801 fprintf(stderr, "Invalid TX descriptor %u (must be < ring_size=%u)\n", 3802 txdesc_id, ring_size); 3803 return 1; 3804 } 3805 3806 static const struct rte_memzone * 3807 ring_dma_zone_lookup(const char *ring_name, portid_t port_id, uint16_t q_id) 3808 { 3809 char mz_name[RTE_MEMZONE_NAMESIZE]; 3810 const struct rte_memzone *mz; 3811 3812 snprintf(mz_name, sizeof(mz_name), "eth_p%d_q%d_%s", 3813 port_id, q_id, ring_name); 3814 mz = rte_memzone_lookup(mz_name); 3815 if (mz == NULL) 3816 fprintf(stderr, 3817 "%s ring memory zoneof (port %d, queue %d) not found (zone name = %s\n", 3818 ring_name, port_id, q_id, mz_name); 3819 return mz; 3820 } 3821 3822 union igb_ring_dword { 3823 uint64_t dword; 3824 struct { 3825 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN 3826 uint32_t lo; 3827 uint32_t hi; 3828 #else 3829 uint32_t hi; 3830 uint32_t lo; 3831 #endif 3832 } words; 3833 }; 3834 3835 struct igb_ring_desc_32_bytes { 3836 union igb_ring_dword lo_dword; 3837 union igb_ring_dword hi_dword; 3838 union igb_ring_dword resv1; 3839 union igb_ring_dword resv2; 3840 }; 3841 3842 struct igb_ring_desc_16_bytes { 3843 union igb_ring_dword lo_dword; 3844 union igb_ring_dword hi_dword; 3845 }; 3846 3847 static void 3848 ring_rxd_display_dword(union igb_ring_dword dword) 3849 { 3850 printf(" 0x%08X - 0x%08X\n", (unsigned)dword.words.lo, 3851 (unsigned)dword.words.hi); 3852 } 3853 3854 static void 3855 ring_rx_descriptor_display(const struct rte_memzone *ring_mz, 3856 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC 3857 portid_t port_id, 3858 #else 3859 __rte_unused portid_t port_id, 3860 #endif 3861 uint16_t desc_id) 3862 { 3863 struct igb_ring_desc_16_bytes *ring = 3864 (struct igb_ring_desc_16_bytes *)ring_mz->addr; 3865 #ifndef RTE_LIBRTE_I40E_16BYTE_RX_DESC 3866 int ret; 3867 struct rte_eth_dev_info dev_info; 3868 3869 ret = eth_dev_info_get_print_err(port_id, &dev_info); 3870 if (ret != 0) 3871 return; 3872 3873 if (strstr(dev_info.driver_name, "i40e") != NULL) { 3874 /* 32 bytes RX descriptor, i40e only */ 3875 struct igb_ring_desc_32_bytes *ring = 3876 (struct igb_ring_desc_32_bytes *)ring_mz->addr; 3877 ring[desc_id].lo_dword.dword = 3878 rte_le_to_cpu_64(ring[desc_id].lo_dword.dword); 3879 ring_rxd_display_dword(ring[desc_id].lo_dword); 3880 ring[desc_id].hi_dword.dword = 3881 rte_le_to_cpu_64(ring[desc_id].hi_dword.dword); 3882 ring_rxd_display_dword(ring[desc_id].hi_dword); 3883 ring[desc_id].resv1.dword = 3884 rte_le_to_cpu_64(ring[desc_id].resv1.dword); 3885 ring_rxd_display_dword(ring[desc_id].resv1); 3886 ring[desc_id].resv2.dword = 3887 rte_le_to_cpu_64(ring[desc_id].resv2.dword); 3888 ring_rxd_display_dword(ring[desc_id].resv2); 3889 3890 return; 3891 } 3892 #endif 3893 /* 16 bytes RX descriptor */ 3894 ring[desc_id].lo_dword.dword = 3895 rte_le_to_cpu_64(ring[desc_id].lo_dword.dword); 3896 ring_rxd_display_dword(ring[desc_id].lo_dword); 3897 ring[desc_id].hi_dword.dword = 3898 rte_le_to_cpu_64(ring[desc_id].hi_dword.dword); 3899 ring_rxd_display_dword(ring[desc_id].hi_dword); 3900 } 3901 3902 static void 3903 ring_tx_descriptor_display(const struct rte_memzone *ring_mz, uint16_t desc_id) 3904 { 3905 struct igb_ring_desc_16_bytes *ring; 3906 struct igb_ring_desc_16_bytes txd; 3907 3908 ring = (struct igb_ring_desc_16_bytes *)ring_mz->addr; 3909 txd.lo_dword.dword = rte_le_to_cpu_64(ring[desc_id].lo_dword.dword); 3910 txd.hi_dword.dword = rte_le_to_cpu_64(ring[desc_id].hi_dword.dword); 3911 printf(" 0x%08X - 0x%08X / 0x%08X - 0x%08X\n", 3912 (unsigned)txd.lo_dword.words.lo, 3913 (unsigned)txd.lo_dword.words.hi, 3914 (unsigned)txd.hi_dword.words.lo, 3915 (unsigned)txd.hi_dword.words.hi); 3916 } 3917 3918 void 3919 rx_ring_desc_display(portid_t port_id, queueid_t rxq_id, uint16_t rxd_id) 3920 { 3921 const struct rte_memzone *rx_mz; 3922 3923 if (rx_desc_id_is_invalid(port_id, rxq_id, rxd_id)) 3924 return; 3925 rx_mz = ring_dma_zone_lookup("rx_ring", port_id, rxq_id); 3926 if (rx_mz == NULL) 3927 return; 3928 ring_rx_descriptor_display(rx_mz, port_id, rxd_id); 3929 } 3930 3931 void 3932 tx_ring_desc_display(portid_t port_id, queueid_t txq_id, uint16_t txd_id) 3933 { 3934 const struct rte_memzone *tx_mz; 3935 3936 if (tx_desc_id_is_invalid(port_id, txq_id, txd_id)) 3937 return; 3938 tx_mz = ring_dma_zone_lookup("tx_ring", port_id, txq_id); 3939 if (tx_mz == NULL) 3940 return; 3941 ring_tx_descriptor_display(tx_mz, txd_id); 3942 } 3943 3944 void 3945 fwd_lcores_config_display(void) 3946 { 3947 lcoreid_t lc_id; 3948 3949 printf("List of forwarding lcores:"); 3950 for (lc_id = 0; lc_id < nb_cfg_lcores; lc_id++) 3951 printf(" %2u", fwd_lcores_cpuids[lc_id]); 3952 printf("\n"); 3953 } 3954 void 3955 rxtx_config_display(void) 3956 { 3957 portid_t pid; 3958 queueid_t qid; 3959 3960 printf(" %s packet forwarding%s packets/burst=%d\n", 3961 cur_fwd_eng->fwd_mode_name, 3962 retry_enabled == 0 ? "" : " with retry", 3963 nb_pkt_per_burst); 3964 3965 if (cur_fwd_eng == &tx_only_engine || cur_fwd_eng == &flow_gen_engine) 3966 printf(" packet len=%u - nb packet segments=%d\n", 3967 (unsigned)tx_pkt_length, (int) tx_pkt_nb_segs); 3968 3969 printf(" nb forwarding cores=%d - nb forwarding ports=%d\n", 3970 nb_fwd_lcores, nb_fwd_ports); 3971 3972 RTE_ETH_FOREACH_DEV(pid) { 3973 struct rte_eth_rxconf *rx_conf = &ports[pid].rxq[0].conf; 3974 struct rte_eth_txconf *tx_conf = &ports[pid].txq[0].conf; 3975 uint16_t *nb_rx_desc = &ports[pid].nb_rx_desc[0]; 3976 uint16_t *nb_tx_desc = &ports[pid].nb_tx_desc[0]; 3977 struct rte_eth_rxq_info rx_qinfo; 3978 struct rte_eth_txq_info tx_qinfo; 3979 uint16_t rx_free_thresh_tmp; 3980 uint16_t tx_free_thresh_tmp; 3981 uint16_t tx_rs_thresh_tmp; 3982 uint16_t nb_rx_desc_tmp; 3983 uint16_t nb_tx_desc_tmp; 3984 uint64_t offloads_tmp; 3985 uint8_t pthresh_tmp; 3986 uint8_t hthresh_tmp; 3987 uint8_t wthresh_tmp; 3988 int32_t rc; 3989 3990 /* per port config */ 3991 printf(" port %d: RX queue number: %d Tx queue number: %d\n", 3992 (unsigned int)pid, nb_rxq, nb_txq); 3993 3994 printf(" Rx offloads=0x%"PRIx64" Tx offloads=0x%"PRIx64"\n", 3995 ports[pid].dev_conf.rxmode.offloads, 3996 ports[pid].dev_conf.txmode.offloads); 3997 3998 /* per rx queue config only for first queue to be less verbose */ 3999 for (qid = 0; qid < 1; qid++) { 4000 rc = rte_eth_rx_queue_info_get(pid, qid, &rx_qinfo); 4001 if (rc) { 4002 nb_rx_desc_tmp = nb_rx_desc[qid]; 4003 rx_free_thresh_tmp = 4004 rx_conf[qid].rx_free_thresh; 4005 pthresh_tmp = rx_conf[qid].rx_thresh.pthresh; 4006 hthresh_tmp = rx_conf[qid].rx_thresh.hthresh; 4007 wthresh_tmp = rx_conf[qid].rx_thresh.wthresh; 4008 offloads_tmp = rx_conf[qid].offloads; 4009 } else { 4010 nb_rx_desc_tmp = rx_qinfo.nb_desc; 4011 rx_free_thresh_tmp = 4012 rx_qinfo.conf.rx_free_thresh; 4013 pthresh_tmp = rx_qinfo.conf.rx_thresh.pthresh; 4014 hthresh_tmp = rx_qinfo.conf.rx_thresh.hthresh; 4015 wthresh_tmp = rx_qinfo.conf.rx_thresh.wthresh; 4016 offloads_tmp = rx_qinfo.conf.offloads; 4017 } 4018 4019 printf(" RX queue: %d\n", qid); 4020 printf(" RX desc=%d - RX free threshold=%d\n", 4021 nb_rx_desc_tmp, rx_free_thresh_tmp); 4022 printf(" RX threshold registers: pthresh=%d hthresh=%d " 4023 " wthresh=%d\n", 4024 pthresh_tmp, hthresh_tmp, wthresh_tmp); 4025 printf(" RX Offloads=0x%"PRIx64, offloads_tmp); 4026 if (rx_conf->share_group > 0) 4027 printf(" share_group=%u share_qid=%u", 4028 rx_conf->share_group, 4029 rx_conf->share_qid); 4030 printf("\n"); 4031 } 4032 4033 /* per tx queue config only for first queue to be less verbose */ 4034 for (qid = 0; qid < 1; qid++) { 4035 rc = rte_eth_tx_queue_info_get(pid, qid, &tx_qinfo); 4036 if (rc) { 4037 nb_tx_desc_tmp = nb_tx_desc[qid]; 4038 tx_free_thresh_tmp = 4039 tx_conf[qid].tx_free_thresh; 4040 pthresh_tmp = tx_conf[qid].tx_thresh.pthresh; 4041 hthresh_tmp = tx_conf[qid].tx_thresh.hthresh; 4042 wthresh_tmp = tx_conf[qid].tx_thresh.wthresh; 4043 offloads_tmp = tx_conf[qid].offloads; 4044 tx_rs_thresh_tmp = tx_conf[qid].tx_rs_thresh; 4045 } else { 4046 nb_tx_desc_tmp = tx_qinfo.nb_desc; 4047 tx_free_thresh_tmp = 4048 tx_qinfo.conf.tx_free_thresh; 4049 pthresh_tmp = tx_qinfo.conf.tx_thresh.pthresh; 4050 hthresh_tmp = tx_qinfo.conf.tx_thresh.hthresh; 4051 wthresh_tmp = tx_qinfo.conf.tx_thresh.wthresh; 4052 offloads_tmp = tx_qinfo.conf.offloads; 4053 tx_rs_thresh_tmp = tx_qinfo.conf.tx_rs_thresh; 4054 } 4055 4056 printf(" TX queue: %d\n", qid); 4057 printf(" TX desc=%d - TX free threshold=%d\n", 4058 nb_tx_desc_tmp, tx_free_thresh_tmp); 4059 printf(" TX threshold registers: pthresh=%d hthresh=%d " 4060 " wthresh=%d\n", 4061 pthresh_tmp, hthresh_tmp, wthresh_tmp); 4062 printf(" TX offloads=0x%"PRIx64" - TX RS bit threshold=%d\n", 4063 offloads_tmp, tx_rs_thresh_tmp); 4064 } 4065 } 4066 } 4067 4068 void 4069 port_rss_reta_info(portid_t port_id, 4070 struct rte_eth_rss_reta_entry64 *reta_conf, 4071 uint16_t nb_entries) 4072 { 4073 uint16_t i, idx, shift; 4074 int ret; 4075 4076 if (port_id_is_invalid(port_id, ENABLED_WARN)) 4077 return; 4078 4079 ret = rte_eth_dev_rss_reta_query(port_id, reta_conf, nb_entries); 4080 if (ret != 0) { 4081 fprintf(stderr, 4082 "Failed to get RSS RETA info, return code = %d\n", 4083 ret); 4084 return; 4085 } 4086 4087 for (i = 0; i < nb_entries; i++) { 4088 idx = i / RTE_ETH_RETA_GROUP_SIZE; 4089 shift = i % RTE_ETH_RETA_GROUP_SIZE; 4090 if (!(reta_conf[idx].mask & (1ULL << shift))) 4091 continue; 4092 printf("RSS RETA configuration: hash index=%u, queue=%u\n", 4093 i, reta_conf[idx].reta[shift]); 4094 } 4095 } 4096 4097 /* 4098 * Displays the RSS hash functions of a port, and, optionally, the RSS hash 4099 * key of the port. 4100 */ 4101 void 4102 port_rss_hash_conf_show(portid_t port_id, int show_rss_key) 4103 { 4104 struct rte_eth_rss_conf rss_conf = {0}; 4105 uint8_t rss_key[RSS_HASH_KEY_LENGTH]; 4106 uint64_t rss_hf; 4107 uint8_t i; 4108 int diag; 4109 struct rte_eth_dev_info dev_info; 4110 uint8_t hash_key_size; 4111 int ret; 4112 4113 if (port_id_is_invalid(port_id, ENABLED_WARN)) 4114 return; 4115 4116 ret = eth_dev_info_get_print_err(port_id, &dev_info); 4117 if (ret != 0) 4118 return; 4119 4120 if (dev_info.hash_key_size > 0 && 4121 dev_info.hash_key_size <= sizeof(rss_key)) 4122 hash_key_size = dev_info.hash_key_size; 4123 else { 4124 fprintf(stderr, 4125 "dev_info did not provide a valid hash key size\n"); 4126 return; 4127 } 4128 4129 /* Get RSS hash key if asked to display it */ 4130 rss_conf.rss_key = (show_rss_key) ? rss_key : NULL; 4131 rss_conf.rss_key_len = hash_key_size; 4132 diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf); 4133 if (diag != 0) { 4134 switch (diag) { 4135 case -ENODEV: 4136 fprintf(stderr, "port index %d invalid\n", port_id); 4137 break; 4138 case -ENOTSUP: 4139 fprintf(stderr, "operation not supported by device\n"); 4140 break; 4141 default: 4142 fprintf(stderr, "operation failed - diag=%d\n", diag); 4143 break; 4144 } 4145 return; 4146 } 4147 rss_hf = rss_conf.rss_hf; 4148 if (rss_hf == 0) { 4149 printf("RSS disabled\n"); 4150 return; 4151 } 4152 printf("RSS functions:\n"); 4153 rss_types_display(rss_hf, TESTPMD_RSS_TYPES_CHAR_NUM_PER_LINE); 4154 if (!show_rss_key) 4155 return; 4156 printf("RSS key:\n"); 4157 for (i = 0; i < hash_key_size; i++) 4158 printf("%02X", rss_key[i]); 4159 printf("\n"); 4160 } 4161 4162 void 4163 port_rss_hash_key_update(portid_t port_id, char rss_type[], uint8_t *hash_key, 4164 uint8_t hash_key_len) 4165 { 4166 struct rte_eth_rss_conf rss_conf; 4167 int diag; 4168 4169 rss_conf.rss_key = NULL; 4170 rss_conf.rss_key_len = 0; 4171 rss_conf.rss_hf = str_to_rsstypes(rss_type); 4172 diag = rte_eth_dev_rss_hash_conf_get(port_id, &rss_conf); 4173 if (diag == 0) { 4174 rss_conf.rss_key = hash_key; 4175 rss_conf.rss_key_len = hash_key_len; 4176 diag = rte_eth_dev_rss_hash_update(port_id, &rss_conf); 4177 } 4178 if (diag == 0) 4179 return; 4180 4181 switch (diag) { 4182 case -ENODEV: 4183 fprintf(stderr, "port index %d invalid\n", port_id); 4184 break; 4185 case -ENOTSUP: 4186 fprintf(stderr, "operation not supported by device\n"); 4187 break; 4188 default: 4189 fprintf(stderr, "operation failed - diag=%d\n", diag); 4190 break; 4191 } 4192 } 4193 4194 /* 4195 * Check whether a shared rxq scheduled on other lcores. 4196 */ 4197 static bool 4198 fwd_stream_on_other_lcores(uint16_t domain_id, lcoreid_t src_lc, 4199 portid_t src_port, queueid_t src_rxq, 4200 uint32_t share_group, queueid_t share_rxq) 4201 { 4202 streamid_t sm_id; 4203 streamid_t nb_fs_per_lcore; 4204 lcoreid_t nb_fc; 4205 lcoreid_t lc_id; 4206 struct fwd_stream *fs; 4207 struct rte_port *port; 4208 struct rte_eth_dev_info *dev_info; 4209 struct rte_eth_rxconf *rxq_conf; 4210 4211 nb_fc = cur_fwd_config.nb_fwd_lcores; 4212 /* Check remaining cores. */ 4213 for (lc_id = src_lc + 1; lc_id < nb_fc; lc_id++) { 4214 sm_id = fwd_lcores[lc_id]->stream_idx; 4215 nb_fs_per_lcore = fwd_lcores[lc_id]->stream_nb; 4216 for (; sm_id < fwd_lcores[lc_id]->stream_idx + nb_fs_per_lcore; 4217 sm_id++) { 4218 fs = fwd_streams[sm_id]; 4219 port = &ports[fs->rx_port]; 4220 dev_info = &port->dev_info; 4221 rxq_conf = &port->rxq[fs->rx_queue].conf; 4222 if ((dev_info->dev_capa & RTE_ETH_DEV_CAPA_RXQ_SHARE) 4223 == 0 || rxq_conf->share_group == 0) 4224 /* Not shared rxq. */ 4225 continue; 4226 if (domain_id != port->dev_info.switch_info.domain_id) 4227 continue; 4228 if (rxq_conf->share_group != share_group) 4229 continue; 4230 if (rxq_conf->share_qid != share_rxq) 4231 continue; 4232 printf("Shared Rx queue group %u queue %hu can't be scheduled on different cores:\n", 4233 share_group, share_rxq); 4234 printf(" lcore %hhu Port %hu queue %hu\n", 4235 src_lc, src_port, src_rxq); 4236 printf(" lcore %hhu Port %hu queue %hu\n", 4237 lc_id, fs->rx_port, fs->rx_queue); 4238 printf("Please use --nb-cores=%hu to limit number of forwarding cores\n", 4239 nb_rxq); 4240 return true; 4241 } 4242 } 4243 return false; 4244 } 4245 4246 /* 4247 * Check shared rxq configuration. 4248 * 4249 * Shared group must not being scheduled on different core. 4250 */ 4251 bool 4252 pkt_fwd_shared_rxq_check(void) 4253 { 4254 streamid_t sm_id; 4255 streamid_t nb_fs_per_lcore; 4256 lcoreid_t nb_fc; 4257 lcoreid_t lc_id; 4258 struct fwd_stream *fs; 4259 uint16_t domain_id; 4260 struct rte_port *port; 4261 struct rte_eth_dev_info *dev_info; 4262 struct rte_eth_rxconf *rxq_conf; 4263 4264 if (rxq_share == 0) 4265 return true; 4266 nb_fc = cur_fwd_config.nb_fwd_lcores; 4267 /* 4268 * Check streams on each core, make sure the same switch domain + 4269 * group + queue doesn't get scheduled on other cores. 4270 */ 4271 for (lc_id = 0; lc_id < nb_fc; lc_id++) { 4272 sm_id = fwd_lcores[lc_id]->stream_idx; 4273 nb_fs_per_lcore = fwd_lcores[lc_id]->stream_nb; 4274 for (; sm_id < fwd_lcores[lc_id]->stream_idx + nb_fs_per_lcore; 4275 sm_id++) { 4276 fs = fwd_streams[sm_id]; 4277 /* Update lcore info stream being scheduled. */ 4278 fs->lcore = fwd_lcores[lc_id]; 4279 port = &ports[fs->rx_port]; 4280 dev_info = &port->dev_info; 4281 rxq_conf = &port->rxq[fs->rx_queue].conf; 4282 if ((dev_info->dev_capa & RTE_ETH_DEV_CAPA_RXQ_SHARE) 4283 == 0 || rxq_conf->share_group == 0) 4284 /* Not shared rxq. */ 4285 continue; 4286 /* Check shared rxq not scheduled on remaining cores. */ 4287 domain_id = port->dev_info.switch_info.domain_id; 4288 if (fwd_stream_on_other_lcores(domain_id, lc_id, 4289 fs->rx_port, 4290 fs->rx_queue, 4291 rxq_conf->share_group, 4292 rxq_conf->share_qid)) 4293 return false; 4294 } 4295 } 4296 return true; 4297 } 4298 4299 /* 4300 * Setup forwarding configuration for each logical core. 4301 */ 4302 static void 4303 setup_fwd_config_of_each_lcore(struct fwd_config *cfg) 4304 { 4305 streamid_t nb_fs_per_lcore; 4306 streamid_t nb_fs; 4307 streamid_t sm_id; 4308 lcoreid_t nb_extra; 4309 lcoreid_t nb_fc; 4310 lcoreid_t nb_lc; 4311 lcoreid_t lc_id; 4312 4313 nb_fs = cfg->nb_fwd_streams; 4314 nb_fc = cfg->nb_fwd_lcores; 4315 if (nb_fs <= nb_fc) { 4316 nb_fs_per_lcore = 1; 4317 nb_extra = 0; 4318 } else { 4319 nb_fs_per_lcore = (streamid_t) (nb_fs / nb_fc); 4320 nb_extra = (lcoreid_t) (nb_fs % nb_fc); 4321 } 4322 4323 nb_lc = (lcoreid_t) (nb_fc - nb_extra); 4324 sm_id = 0; 4325 for (lc_id = 0; lc_id < nb_lc; lc_id++) { 4326 fwd_lcores[lc_id]->stream_idx = sm_id; 4327 fwd_lcores[lc_id]->stream_nb = nb_fs_per_lcore; 4328 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore); 4329 } 4330 4331 /* 4332 * Assign extra remaining streams, if any. 4333 */ 4334 nb_fs_per_lcore = (streamid_t) (nb_fs_per_lcore + 1); 4335 for (lc_id = 0; lc_id < nb_extra; lc_id++) { 4336 fwd_lcores[nb_lc + lc_id]->stream_idx = sm_id; 4337 fwd_lcores[nb_lc + lc_id]->stream_nb = nb_fs_per_lcore; 4338 sm_id = (streamid_t) (sm_id + nb_fs_per_lcore); 4339 } 4340 } 4341 4342 static portid_t 4343 fwd_topology_tx_port_get(portid_t rxp) 4344 { 4345 static int warning_once = 1; 4346 4347 RTE_ASSERT(rxp < cur_fwd_config.nb_fwd_ports); 4348 4349 switch (port_topology) { 4350 default: 4351 case PORT_TOPOLOGY_PAIRED: 4352 if ((rxp & 0x1) == 0) { 4353 if (rxp + 1 < cur_fwd_config.nb_fwd_ports) 4354 return rxp + 1; 4355 if (warning_once) { 4356 fprintf(stderr, 4357 "\nWarning! port-topology=paired and odd forward ports number, the last port will pair with itself.\n\n"); 4358 warning_once = 0; 4359 } 4360 return rxp; 4361 } 4362 return rxp - 1; 4363 case PORT_TOPOLOGY_CHAINED: 4364 return (rxp + 1) % cur_fwd_config.nb_fwd_ports; 4365 case PORT_TOPOLOGY_LOOP: 4366 return rxp; 4367 } 4368 } 4369 4370 static void 4371 simple_fwd_config_setup(void) 4372 { 4373 portid_t i; 4374 4375 cur_fwd_config.nb_fwd_ports = (portid_t) nb_fwd_ports; 4376 cur_fwd_config.nb_fwd_streams = 4377 (streamid_t) cur_fwd_config.nb_fwd_ports; 4378 4379 /* reinitialize forwarding streams */ 4380 init_fwd_streams(); 4381 4382 /* 4383 * In the simple forwarding test, the number of forwarding cores 4384 * must be lower or equal to the number of forwarding ports. 4385 */ 4386 cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores; 4387 if (cur_fwd_config.nb_fwd_lcores > cur_fwd_config.nb_fwd_ports) 4388 cur_fwd_config.nb_fwd_lcores = 4389 (lcoreid_t) cur_fwd_config.nb_fwd_ports; 4390 setup_fwd_config_of_each_lcore(&cur_fwd_config); 4391 4392 for (i = 0; i < cur_fwd_config.nb_fwd_ports; i++) { 4393 fwd_streams[i]->rx_port = fwd_ports_ids[i]; 4394 fwd_streams[i]->rx_queue = 0; 4395 fwd_streams[i]->tx_port = 4396 fwd_ports_ids[fwd_topology_tx_port_get(i)]; 4397 fwd_streams[i]->tx_queue = 0; 4398 fwd_streams[i]->peer_addr = fwd_streams[i]->tx_port; 4399 fwd_streams[i]->retry_enabled = retry_enabled; 4400 } 4401 } 4402 4403 /** 4404 * For the RSS forwarding test all streams distributed over lcores. Each stream 4405 * being composed of a RX queue to poll on a RX port for input messages, 4406 * associated with a TX queue of a TX port where to send forwarded packets. 4407 */ 4408 static void 4409 rss_fwd_config_setup(void) 4410 { 4411 portid_t rxp; 4412 portid_t txp; 4413 queueid_t rxq; 4414 queueid_t nb_q; 4415 streamid_t sm_id; 4416 int start; 4417 int end; 4418 4419 nb_q = nb_rxq; 4420 if (nb_q > nb_txq) 4421 nb_q = nb_txq; 4422 cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores; 4423 cur_fwd_config.nb_fwd_ports = nb_fwd_ports; 4424 cur_fwd_config.nb_fwd_streams = 4425 (streamid_t) (nb_q * cur_fwd_config.nb_fwd_ports); 4426 4427 if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores) 4428 cur_fwd_config.nb_fwd_lcores = 4429 (lcoreid_t)cur_fwd_config.nb_fwd_streams; 4430 4431 /* reinitialize forwarding streams */ 4432 init_fwd_streams(); 4433 4434 setup_fwd_config_of_each_lcore(&cur_fwd_config); 4435 4436 if (proc_id > 0 && nb_q % num_procs != 0) 4437 printf("Warning! queue numbers should be multiple of processes, or packet loss will happen.\n"); 4438 4439 /** 4440 * In multi-process, All queues are allocated to different 4441 * processes based on num_procs and proc_id. For example: 4442 * if supports 4 queues(nb_q), 2 processes(num_procs), 4443 * the 0~1 queue for primary process. 4444 * the 2~3 queue for secondary process. 4445 */ 4446 start = proc_id * nb_q / num_procs; 4447 end = start + nb_q / num_procs; 4448 rxp = 0; 4449 rxq = start; 4450 for (sm_id = 0; sm_id < cur_fwd_config.nb_fwd_streams; sm_id++) { 4451 struct fwd_stream *fs; 4452 4453 fs = fwd_streams[sm_id]; 4454 txp = fwd_topology_tx_port_get(rxp); 4455 fs->rx_port = fwd_ports_ids[rxp]; 4456 fs->rx_queue = rxq; 4457 fs->tx_port = fwd_ports_ids[txp]; 4458 fs->tx_queue = rxq; 4459 fs->peer_addr = fs->tx_port; 4460 fs->retry_enabled = retry_enabled; 4461 rxp++; 4462 if (rxp < nb_fwd_ports) 4463 continue; 4464 rxp = 0; 4465 rxq++; 4466 if (rxq >= end) 4467 rxq = start; 4468 } 4469 } 4470 4471 static uint16_t 4472 get_fwd_port_total_tc_num(void) 4473 { 4474 struct rte_eth_dcb_info dcb_info; 4475 uint16_t total_tc_num = 0; 4476 unsigned int i; 4477 4478 for (i = 0; i < nb_fwd_ports; i++) { 4479 (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[i], &dcb_info); 4480 total_tc_num += dcb_info.nb_tcs; 4481 } 4482 4483 return total_tc_num; 4484 } 4485 4486 /** 4487 * For the DCB forwarding test, each core is assigned on each traffic class. 4488 * 4489 * Each core is assigned a multi-stream, each stream being composed of 4490 * a RX queue to poll on a RX port for input messages, associated with 4491 * a TX queue of a TX port where to send forwarded packets. All RX and 4492 * TX queues are mapping to the same traffic class. 4493 * If VMDQ and DCB co-exist, each traffic class on different POOLs share 4494 * the same core 4495 */ 4496 static void 4497 dcb_fwd_config_setup(void) 4498 { 4499 struct rte_eth_dcb_info rxp_dcb_info, txp_dcb_info; 4500 portid_t txp, rxp = 0; 4501 queueid_t txq, rxq = 0; 4502 lcoreid_t lc_id; 4503 uint16_t nb_rx_queue, nb_tx_queue; 4504 uint16_t i, j, k, sm_id = 0; 4505 uint16_t total_tc_num; 4506 struct rte_port *port; 4507 uint8_t tc = 0; 4508 portid_t pid; 4509 int ret; 4510 4511 /* 4512 * The fwd_config_setup() is called when the port is RTE_PORT_STARTED 4513 * or RTE_PORT_STOPPED. 4514 * 4515 * Re-configure ports to get updated mapping between tc and queue in 4516 * case the queue number of the port is changed. Skip for started ports 4517 * since modifying queue number and calling dev_configure need to stop 4518 * ports first. 4519 */ 4520 for (pid = 0; pid < nb_fwd_ports; pid++) { 4521 if (port_is_started(pid) == 1) 4522 continue; 4523 4524 port = &ports[pid]; 4525 ret = rte_eth_dev_configure(pid, nb_rxq, nb_txq, 4526 &port->dev_conf); 4527 if (ret < 0) { 4528 fprintf(stderr, 4529 "Failed to re-configure port %d, ret = %d.\n", 4530 pid, ret); 4531 return; 4532 } 4533 } 4534 4535 cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores; 4536 cur_fwd_config.nb_fwd_ports = nb_fwd_ports; 4537 cur_fwd_config.nb_fwd_streams = 4538 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports); 4539 total_tc_num = get_fwd_port_total_tc_num(); 4540 if (cur_fwd_config.nb_fwd_lcores > total_tc_num) 4541 cur_fwd_config.nb_fwd_lcores = total_tc_num; 4542 4543 /* reinitialize forwarding streams */ 4544 init_fwd_streams(); 4545 sm_id = 0; 4546 txp = 1; 4547 /* get the dcb info on the first RX and TX ports */ 4548 (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[rxp], &rxp_dcb_info); 4549 (void)rte_eth_dev_get_dcb_info(fwd_ports_ids[txp], &txp_dcb_info); 4550 4551 for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) { 4552 fwd_lcores[lc_id]->stream_nb = 0; 4553 fwd_lcores[lc_id]->stream_idx = sm_id; 4554 for (i = 0; i < RTE_ETH_MAX_VMDQ_POOL; i++) { 4555 /* if the nb_queue is zero, means this tc is 4556 * not enabled on the POOL 4557 */ 4558 if (rxp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue == 0) 4559 break; 4560 k = fwd_lcores[lc_id]->stream_nb + 4561 fwd_lcores[lc_id]->stream_idx; 4562 rxq = rxp_dcb_info.tc_queue.tc_rxq[i][tc].base; 4563 txq = txp_dcb_info.tc_queue.tc_txq[i][tc].base; 4564 nb_rx_queue = txp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue; 4565 nb_tx_queue = txp_dcb_info.tc_queue.tc_txq[i][tc].nb_queue; 4566 for (j = 0; j < nb_rx_queue; j++) { 4567 struct fwd_stream *fs; 4568 4569 fs = fwd_streams[k + j]; 4570 fs->rx_port = fwd_ports_ids[rxp]; 4571 fs->rx_queue = rxq + j; 4572 fs->tx_port = fwd_ports_ids[txp]; 4573 fs->tx_queue = txq + j % nb_tx_queue; 4574 fs->peer_addr = fs->tx_port; 4575 fs->retry_enabled = retry_enabled; 4576 } 4577 fwd_lcores[lc_id]->stream_nb += 4578 rxp_dcb_info.tc_queue.tc_rxq[i][tc].nb_queue; 4579 } 4580 sm_id = (streamid_t) (sm_id + fwd_lcores[lc_id]->stream_nb); 4581 4582 tc++; 4583 if (tc < rxp_dcb_info.nb_tcs) 4584 continue; 4585 /* Restart from TC 0 on next RX port */ 4586 tc = 0; 4587 if (numa_support && (nb_fwd_ports <= (nb_ports >> 1))) 4588 rxp = (portid_t) 4589 (rxp + ((nb_ports >> 1) / nb_fwd_ports)); 4590 else 4591 rxp++; 4592 if (rxp >= nb_fwd_ports) 4593 return; 4594 /* get the dcb information on next RX and TX ports */ 4595 if ((rxp & 0x1) == 0) 4596 txp = (portid_t) (rxp + 1); 4597 else 4598 txp = (portid_t) (rxp - 1); 4599 rte_eth_dev_get_dcb_info(fwd_ports_ids[rxp], &rxp_dcb_info); 4600 rte_eth_dev_get_dcb_info(fwd_ports_ids[txp], &txp_dcb_info); 4601 } 4602 } 4603 4604 static void 4605 icmp_echo_config_setup(void) 4606 { 4607 portid_t rxp; 4608 queueid_t rxq; 4609 lcoreid_t lc_id; 4610 uint16_t sm_id; 4611 4612 if ((nb_txq * nb_fwd_ports) < nb_fwd_lcores) 4613 cur_fwd_config.nb_fwd_lcores = (lcoreid_t) 4614 (nb_txq * nb_fwd_ports); 4615 else 4616 cur_fwd_config.nb_fwd_lcores = (lcoreid_t) nb_fwd_lcores; 4617 cur_fwd_config.nb_fwd_ports = nb_fwd_ports; 4618 cur_fwd_config.nb_fwd_streams = 4619 (streamid_t) (nb_rxq * cur_fwd_config.nb_fwd_ports); 4620 if (cur_fwd_config.nb_fwd_streams < cur_fwd_config.nb_fwd_lcores) 4621 cur_fwd_config.nb_fwd_lcores = 4622 (lcoreid_t)cur_fwd_config.nb_fwd_streams; 4623 if (verbose_level > 0) { 4624 printf("%s fwd_cores=%d fwd_ports=%d fwd_streams=%d\n", 4625 __FUNCTION__, 4626 cur_fwd_config.nb_fwd_lcores, 4627 cur_fwd_config.nb_fwd_ports, 4628 cur_fwd_config.nb_fwd_streams); 4629 } 4630 4631 /* reinitialize forwarding streams */ 4632 init_fwd_streams(); 4633 setup_fwd_config_of_each_lcore(&cur_fwd_config); 4634 rxp = 0; rxq = 0; 4635 for (lc_id = 0; lc_id < cur_fwd_config.nb_fwd_lcores; lc_id++) { 4636 if (verbose_level > 0) 4637 printf(" core=%d: \n", lc_id); 4638 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) { 4639 struct fwd_stream *fs; 4640 fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id]; 4641 fs->rx_port = fwd_ports_ids[rxp]; 4642 fs->rx_queue = rxq; 4643 fs->tx_port = fs->rx_port; 4644 fs->tx_queue = rxq; 4645 fs->peer_addr = fs->tx_port; 4646 fs->retry_enabled = retry_enabled; 4647 if (verbose_level > 0) 4648 printf(" stream=%d port=%d rxq=%d txq=%d\n", 4649 sm_id, fs->rx_port, fs->rx_queue, 4650 fs->tx_queue); 4651 rxq = (queueid_t) (rxq + 1); 4652 if (rxq == nb_rxq) { 4653 rxq = 0; 4654 rxp = (portid_t) (rxp + 1); 4655 } 4656 } 4657 } 4658 } 4659 4660 void 4661 fwd_config_setup(void) 4662 { 4663 struct rte_port *port; 4664 portid_t pt_id; 4665 unsigned int i; 4666 4667 cur_fwd_config.fwd_eng = cur_fwd_eng; 4668 if (strcmp(cur_fwd_eng->fwd_mode_name, "icmpecho") == 0) { 4669 icmp_echo_config_setup(); 4670 return; 4671 } 4672 4673 if ((nb_rxq > 1) && (nb_txq > 1)){ 4674 if (dcb_config) { 4675 for (i = 0; i < nb_fwd_ports; i++) { 4676 pt_id = fwd_ports_ids[i]; 4677 port = &ports[pt_id]; 4678 if (!port->dcb_flag) { 4679 fprintf(stderr, 4680 "In DCB mode, all forwarding ports must be configured in this mode.\n"); 4681 return; 4682 } 4683 } 4684 if (nb_fwd_lcores == 1) { 4685 fprintf(stderr, 4686 "In DCB mode,the nb forwarding cores should be larger than 1.\n"); 4687 return; 4688 } 4689 4690 dcb_fwd_config_setup(); 4691 } else 4692 rss_fwd_config_setup(); 4693 } 4694 else 4695 simple_fwd_config_setup(); 4696 } 4697 4698 static const char * 4699 mp_alloc_to_str(uint8_t mode) 4700 { 4701 switch (mode) { 4702 case MP_ALLOC_NATIVE: 4703 return "native"; 4704 case MP_ALLOC_ANON: 4705 return "anon"; 4706 case MP_ALLOC_XMEM: 4707 return "xmem"; 4708 case MP_ALLOC_XMEM_HUGE: 4709 return "xmemhuge"; 4710 case MP_ALLOC_XBUF: 4711 return "xbuf"; 4712 default: 4713 return "invalid"; 4714 } 4715 } 4716 4717 void 4718 pkt_fwd_config_display(struct fwd_config *cfg) 4719 { 4720 struct fwd_stream *fs; 4721 lcoreid_t lc_id; 4722 streamid_t sm_id; 4723 4724 printf("%s packet forwarding%s - ports=%d - cores=%d - streams=%d - " 4725 "NUMA support %s, MP allocation mode: %s\n", 4726 cfg->fwd_eng->fwd_mode_name, 4727 retry_enabled == 0 ? "" : " with retry", 4728 cfg->nb_fwd_ports, cfg->nb_fwd_lcores, cfg->nb_fwd_streams, 4729 numa_support == 1 ? "enabled" : "disabled", 4730 mp_alloc_to_str(mp_alloc_type)); 4731 4732 if (retry_enabled) 4733 printf("TX retry num: %u, delay between TX retries: %uus\n", 4734 burst_tx_retry_num, burst_tx_delay_time); 4735 for (lc_id = 0; lc_id < cfg->nb_fwd_lcores; lc_id++) { 4736 printf("Logical Core %u (socket %u) forwards packets on " 4737 "%d streams:", 4738 fwd_lcores_cpuids[lc_id], 4739 rte_lcore_to_socket_id(fwd_lcores_cpuids[lc_id]), 4740 fwd_lcores[lc_id]->stream_nb); 4741 for (sm_id = 0; sm_id < fwd_lcores[lc_id]->stream_nb; sm_id++) { 4742 fs = fwd_streams[fwd_lcores[lc_id]->stream_idx + sm_id]; 4743 printf("\n RX P=%d/Q=%d (socket %u) -> TX " 4744 "P=%d/Q=%d (socket %u) ", 4745 fs->rx_port, fs->rx_queue, 4746 ports[fs->rx_port].socket_id, 4747 fs->tx_port, fs->tx_queue, 4748 ports[fs->tx_port].socket_id); 4749 print_ethaddr("peer=", 4750 &peer_eth_addrs[fs->peer_addr]); 4751 } 4752 printf("\n"); 4753 } 4754 printf("\n"); 4755 } 4756 4757 void 4758 set_fwd_eth_peer(portid_t port_id, char *peer_addr) 4759 { 4760 struct rte_ether_addr new_peer_addr; 4761 if (!rte_eth_dev_is_valid_port(port_id)) { 4762 fprintf(stderr, "Error: Invalid port number %i\n", port_id); 4763 return; 4764 } 4765 if (rte_ether_unformat_addr(peer_addr, &new_peer_addr) < 0) { 4766 fprintf(stderr, "Error: Invalid ethernet address: %s\n", 4767 peer_addr); 4768 return; 4769 } 4770 peer_eth_addrs[port_id] = new_peer_addr; 4771 } 4772 4773 int 4774 set_fwd_lcores_list(unsigned int *lcorelist, unsigned int nb_lc) 4775 { 4776 unsigned int i; 4777 unsigned int lcore_cpuid; 4778 int record_now; 4779 4780 record_now = 0; 4781 again: 4782 for (i = 0; i < nb_lc; i++) { 4783 lcore_cpuid = lcorelist[i]; 4784 if (! rte_lcore_is_enabled(lcore_cpuid)) { 4785 fprintf(stderr, "lcore %u not enabled\n", lcore_cpuid); 4786 return -1; 4787 } 4788 if (lcore_cpuid == rte_get_main_lcore()) { 4789 fprintf(stderr, 4790 "lcore %u cannot be masked on for running packet forwarding, which is the main lcore and reserved for command line parsing only\n", 4791 lcore_cpuid); 4792 return -1; 4793 } 4794 if (record_now) 4795 fwd_lcores_cpuids[i] = lcore_cpuid; 4796 } 4797 if (record_now == 0) { 4798 record_now = 1; 4799 goto again; 4800 } 4801 nb_cfg_lcores = (lcoreid_t) nb_lc; 4802 if (nb_fwd_lcores != (lcoreid_t) nb_lc) { 4803 printf("previous number of forwarding cores %u - changed to " 4804 "number of configured cores %u\n", 4805 (unsigned int) nb_fwd_lcores, nb_lc); 4806 nb_fwd_lcores = (lcoreid_t) nb_lc; 4807 } 4808 4809 return 0; 4810 } 4811 4812 int 4813 set_fwd_lcores_mask(uint64_t lcoremask) 4814 { 4815 unsigned int lcorelist[64]; 4816 unsigned int nb_lc; 4817 unsigned int i; 4818 4819 if (lcoremask == 0) { 4820 fprintf(stderr, "Invalid NULL mask of cores\n"); 4821 return -1; 4822 } 4823 nb_lc = 0; 4824 for (i = 0; i < 64; i++) { 4825 if (! ((uint64_t)(1ULL << i) & lcoremask)) 4826 continue; 4827 lcorelist[nb_lc++] = i; 4828 } 4829 return set_fwd_lcores_list(lcorelist, nb_lc); 4830 } 4831 4832 void 4833 set_fwd_lcores_number(uint16_t nb_lc) 4834 { 4835 if (test_done == 0) { 4836 fprintf(stderr, "Please stop forwarding first\n"); 4837 return; 4838 } 4839 if (nb_lc > nb_cfg_lcores) { 4840 fprintf(stderr, 4841 "nb fwd cores %u > %u (max. number of configured lcores) - ignored\n", 4842 (unsigned int) nb_lc, (unsigned int) nb_cfg_lcores); 4843 return; 4844 } 4845 nb_fwd_lcores = (lcoreid_t) nb_lc; 4846 printf("Number of forwarding cores set to %u\n", 4847 (unsigned int) nb_fwd_lcores); 4848 } 4849 4850 void 4851 set_fwd_ports_list(unsigned int *portlist, unsigned int nb_pt) 4852 { 4853 unsigned int i; 4854 portid_t port_id; 4855 int record_now; 4856 4857 record_now = 0; 4858 again: 4859 for (i = 0; i < nb_pt; i++) { 4860 port_id = (portid_t) portlist[i]; 4861 if (port_id_is_invalid(port_id, ENABLED_WARN)) 4862 return; 4863 if (record_now) 4864 fwd_ports_ids[i] = port_id; 4865 } 4866 if (record_now == 0) { 4867 record_now = 1; 4868 goto again; 4869 } 4870 nb_cfg_ports = (portid_t) nb_pt; 4871 if (nb_fwd_ports != (portid_t) nb_pt) { 4872 printf("previous number of forwarding ports %u - changed to " 4873 "number of configured ports %u\n", 4874 (unsigned int) nb_fwd_ports, nb_pt); 4875 nb_fwd_ports = (portid_t) nb_pt; 4876 } 4877 } 4878 4879 /** 4880 * Parse the user input and obtain the list of forwarding ports 4881 * 4882 * @param[in] list 4883 * String containing the user input. User can specify 4884 * in these formats 1,3,5 or 1-3 or 1-2,5 or 3,5-6. 4885 * For example, if the user wants to use all the available 4886 * 4 ports in his system, then the input can be 0-3 or 0,1,2,3. 4887 * If the user wants to use only the ports 1,2 then the input 4888 * is 1,2. 4889 * valid characters are '-' and ',' 4890 * @param[out] values 4891 * This array will be filled with a list of port IDs 4892 * based on the user input 4893 * Note that duplicate entries are discarded and only the first 4894 * count entries in this array are port IDs and all the rest 4895 * will contain default values 4896 * @param[in] maxsize 4897 * This parameter denotes 2 things 4898 * 1) Number of elements in the values array 4899 * 2) Maximum value of each element in the values array 4900 * @return 4901 * On success, returns total count of parsed port IDs 4902 * On failure, returns 0 4903 */ 4904 static unsigned int 4905 parse_port_list(const char *list, unsigned int *values, unsigned int maxsize) 4906 { 4907 unsigned int count = 0; 4908 char *end = NULL; 4909 int min, max; 4910 int value, i; 4911 unsigned int marked[maxsize]; 4912 4913 if (list == NULL || values == NULL) 4914 return 0; 4915 4916 for (i = 0; i < (int)maxsize; i++) 4917 marked[i] = 0; 4918 4919 min = INT_MAX; 4920 4921 do { 4922 /*Remove the blank spaces if any*/ 4923 while (isblank(*list)) 4924 list++; 4925 if (*list == '\0') 4926 break; 4927 errno = 0; 4928 value = strtol(list, &end, 10); 4929 if (errno || end == NULL) 4930 return 0; 4931 if (value < 0 || value >= (int)maxsize) 4932 return 0; 4933 while (isblank(*end)) 4934 end++; 4935 if (*end == '-' && min == INT_MAX) { 4936 min = value; 4937 } else if ((*end == ',') || (*end == '\0')) { 4938 max = value; 4939 if (min == INT_MAX) 4940 min = value; 4941 for (i = min; i <= max; i++) { 4942 if (count < maxsize) { 4943 if (marked[i]) 4944 continue; 4945 values[count] = i; 4946 marked[i] = 1; 4947 count++; 4948 } 4949 } 4950 min = INT_MAX; 4951 } else 4952 return 0; 4953 list = end + 1; 4954 } while (*end != '\0'); 4955 4956 return count; 4957 } 4958 4959 void 4960 parse_fwd_portlist(const char *portlist) 4961 { 4962 unsigned int portcount; 4963 unsigned int portindex[RTE_MAX_ETHPORTS]; 4964 unsigned int i, valid_port_count = 0; 4965 4966 portcount = parse_port_list(portlist, portindex, RTE_MAX_ETHPORTS); 4967 if (!portcount) 4968 rte_exit(EXIT_FAILURE, "Invalid fwd port list\n"); 4969 4970 /* 4971 * Here we verify the validity of the ports 4972 * and thereby calculate the total number of 4973 * valid ports 4974 */ 4975 for (i = 0; i < portcount && i < RTE_DIM(portindex); i++) { 4976 if (rte_eth_dev_is_valid_port(portindex[i])) { 4977 portindex[valid_port_count] = portindex[i]; 4978 valid_port_count++; 4979 } 4980 } 4981 4982 set_fwd_ports_list(portindex, valid_port_count); 4983 } 4984 4985 void 4986 set_fwd_ports_mask(uint64_t portmask) 4987 { 4988 unsigned int portlist[64]; 4989 unsigned int nb_pt; 4990 unsigned int i; 4991 4992 if (portmask == 0) { 4993 fprintf(stderr, "Invalid NULL mask of ports\n"); 4994 return; 4995 } 4996 nb_pt = 0; 4997 RTE_ETH_FOREACH_DEV(i) { 4998 if (! ((uint64_t)(1ULL << i) & portmask)) 4999 continue; 5000 portlist[nb_pt++] = i; 5001 } 5002 set_fwd_ports_list(portlist, nb_pt); 5003 } 5004 5005 void 5006 set_fwd_ports_number(uint16_t nb_pt) 5007 { 5008 if (nb_pt > nb_cfg_ports) { 5009 fprintf(stderr, 5010 "nb fwd ports %u > %u (number of configured ports) - ignored\n", 5011 (unsigned int) nb_pt, (unsigned int) nb_cfg_ports); 5012 return; 5013 } 5014 nb_fwd_ports = (portid_t) nb_pt; 5015 printf("Number of forwarding ports set to %u\n", 5016 (unsigned int) nb_fwd_ports); 5017 } 5018 5019 int 5020 port_is_forwarding(portid_t port_id) 5021 { 5022 unsigned int i; 5023 5024 if (port_id_is_invalid(port_id, ENABLED_WARN)) 5025 return -1; 5026 5027 for (i = 0; i < nb_fwd_ports; i++) { 5028 if (fwd_ports_ids[i] == port_id) 5029 return 1; 5030 } 5031 5032 return 0; 5033 } 5034 5035 void 5036 set_nb_pkt_per_burst(uint16_t nb) 5037 { 5038 if (nb > MAX_PKT_BURST) { 5039 fprintf(stderr, 5040 "nb pkt per burst: %u > %u (maximum packet per burst) ignored\n", 5041 (unsigned int) nb, (unsigned int) MAX_PKT_BURST); 5042 return; 5043 } 5044 nb_pkt_per_burst = nb; 5045 printf("Number of packets per burst set to %u\n", 5046 (unsigned int) nb_pkt_per_burst); 5047 } 5048 5049 static const char * 5050 tx_split_get_name(enum tx_pkt_split split) 5051 { 5052 uint32_t i; 5053 5054 for (i = 0; i != RTE_DIM(tx_split_name); i++) { 5055 if (tx_split_name[i].split == split) 5056 return tx_split_name[i].name; 5057 } 5058 return NULL; 5059 } 5060 5061 void 5062 set_tx_pkt_split(const char *name) 5063 { 5064 uint32_t i; 5065 5066 for (i = 0; i != RTE_DIM(tx_split_name); i++) { 5067 if (strcmp(tx_split_name[i].name, name) == 0) { 5068 tx_pkt_split = tx_split_name[i].split; 5069 return; 5070 } 5071 } 5072 fprintf(stderr, "unknown value: \"%s\"\n", name); 5073 } 5074 5075 int 5076 parse_fec_mode(const char *name, uint32_t *fec_capa) 5077 { 5078 uint8_t i; 5079 5080 for (i = 0; i < RTE_DIM(fec_mode_name); i++) { 5081 if (strcmp(fec_mode_name[i].name, name) == 0) { 5082 *fec_capa = 5083 RTE_ETH_FEC_MODE_TO_CAPA(fec_mode_name[i].mode); 5084 return 0; 5085 } 5086 } 5087 return -1; 5088 } 5089 5090 void 5091 show_fec_capability(unsigned int num, struct rte_eth_fec_capa *speed_fec_capa) 5092 { 5093 unsigned int i, j; 5094 5095 printf("FEC capabilities:\n"); 5096 5097 for (i = 0; i < num; i++) { 5098 printf("%s : ", 5099 rte_eth_link_speed_to_str(speed_fec_capa[i].speed)); 5100 5101 for (j = 0; j < RTE_DIM(fec_mode_name); j++) { 5102 if (RTE_ETH_FEC_MODE_TO_CAPA(j) & 5103 speed_fec_capa[i].capa) 5104 printf("%s ", fec_mode_name[j].name); 5105 } 5106 printf("\n"); 5107 } 5108 } 5109 5110 void 5111 show_rx_pkt_offsets(void) 5112 { 5113 uint32_t i, n; 5114 5115 n = rx_pkt_nb_offs; 5116 printf("Number of offsets: %u\n", n); 5117 if (n) { 5118 printf("Segment offsets: "); 5119 for (i = 0; i != n - 1; i++) 5120 printf("%hu,", rx_pkt_seg_offsets[i]); 5121 printf("%hu\n", rx_pkt_seg_lengths[i]); 5122 } 5123 } 5124 5125 void 5126 set_rx_pkt_offsets(unsigned int *seg_offsets, unsigned int nb_offs) 5127 { 5128 unsigned int i; 5129 5130 if (nb_offs >= MAX_SEGS_BUFFER_SPLIT) { 5131 printf("nb segments per RX packets=%u >= " 5132 "MAX_SEGS_BUFFER_SPLIT - ignored\n", nb_offs); 5133 return; 5134 } 5135 5136 /* 5137 * No extra check here, the segment length will be checked by PMD 5138 * in the extended queue setup. 5139 */ 5140 for (i = 0; i < nb_offs; i++) { 5141 if (seg_offsets[i] >= UINT16_MAX) { 5142 printf("offset[%u]=%u > UINT16_MAX - give up\n", 5143 i, seg_offsets[i]); 5144 return; 5145 } 5146 } 5147 5148 for (i = 0; i < nb_offs; i++) 5149 rx_pkt_seg_offsets[i] = (uint16_t) seg_offsets[i]; 5150 5151 rx_pkt_nb_offs = (uint8_t) nb_offs; 5152 } 5153 5154 void 5155 show_rx_pkt_segments(void) 5156 { 5157 uint32_t i, n; 5158 5159 n = rx_pkt_nb_segs; 5160 printf("Number of segments: %u\n", n); 5161 if (n) { 5162 printf("Segment sizes: "); 5163 for (i = 0; i != n - 1; i++) 5164 printf("%hu,", rx_pkt_seg_lengths[i]); 5165 printf("%hu\n", rx_pkt_seg_lengths[i]); 5166 } 5167 } 5168 5169 static const char *get_ptype_str(uint32_t ptype) 5170 { 5171 const char *str; 5172 5173 switch (ptype) { 5174 case RTE_PTYPE_L2_ETHER: 5175 str = "eth"; 5176 break; 5177 case RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN: 5178 str = "ipv4"; 5179 break; 5180 case RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN: 5181 str = "ipv6"; 5182 break; 5183 case RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_L4_TCP: 5184 str = "ipv4-tcp"; 5185 break; 5186 case RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_L4_UDP: 5187 str = "ipv4-udp"; 5188 break; 5189 case RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_L4_SCTP: 5190 str = "ipv4-sctp"; 5191 break; 5192 case RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_L4_TCP: 5193 str = "ipv6-tcp"; 5194 break; 5195 case RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_L4_UDP: 5196 str = "ipv6-udp"; 5197 break; 5198 case RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_L4_SCTP: 5199 str = "ipv6-sctp"; 5200 break; 5201 case RTE_PTYPE_TUNNEL_GRENAT: 5202 str = "grenat"; 5203 break; 5204 case RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER: 5205 str = "inner-eth"; 5206 break; 5207 case RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER 5208 | RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN: 5209 str = "inner-ipv4"; 5210 break; 5211 case RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER 5212 | RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN: 5213 str = "inner-ipv6"; 5214 break; 5215 case RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER | 5216 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN | RTE_PTYPE_INNER_L4_TCP: 5217 str = "inner-ipv4-tcp"; 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_UDP: 5221 str = "inner-ipv4-udp"; 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_SCTP: 5225 str = "inner-ipv4-sctp"; 5226 break; 5227 case RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER | 5228 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN | RTE_PTYPE_INNER_L4_TCP: 5229 str = "inner-ipv6-tcp"; 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_UDP: 5233 str = "inner-ipv6-udp"; 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_SCTP: 5237 str = "inner-ipv6-sctp"; 5238 break; 5239 default: 5240 str = "unsupported"; 5241 } 5242 5243 return str; 5244 } 5245 5246 void 5247 show_rx_pkt_hdrs(void) 5248 { 5249 uint32_t i, n; 5250 5251 n = rx_pkt_nb_segs; 5252 printf("Number of segments: %u\n", n); 5253 if (n) { 5254 printf("Packet segs: "); 5255 for (i = 0; i < n - 1; i++) 5256 printf("%s, ", get_ptype_str(rx_pkt_hdr_protos[i])); 5257 printf("payload\n"); 5258 } 5259 } 5260 5261 void 5262 set_rx_pkt_hdrs(unsigned int *seg_hdrs, unsigned int nb_segs) 5263 { 5264 unsigned int i; 5265 5266 if (nb_segs + 1 > MAX_SEGS_BUFFER_SPLIT) { 5267 printf("nb segments per RX packets=%u > " 5268 "MAX_SEGS_BUFFER_SPLIT - ignored\n", nb_segs + 1); 5269 return; 5270 } 5271 5272 memset(rx_pkt_hdr_protos, 0, sizeof(rx_pkt_hdr_protos)); 5273 5274 for (i = 0; i < nb_segs; i++) 5275 rx_pkt_hdr_protos[i] = (uint32_t)seg_hdrs[i]; 5276 /* 5277 * We calculate the number of hdrs, but payload is not included, 5278 * so rx_pkt_nb_segs would increase 1. 5279 */ 5280 rx_pkt_nb_segs = nb_segs + 1; 5281 } 5282 5283 void 5284 set_rx_pkt_segments(unsigned int *seg_lengths, unsigned int nb_segs) 5285 { 5286 unsigned int i; 5287 5288 if (nb_segs >= MAX_SEGS_BUFFER_SPLIT) { 5289 printf("nb segments per RX packets=%u >= " 5290 "MAX_SEGS_BUFFER_SPLIT - ignored\n", nb_segs); 5291 return; 5292 } 5293 5294 /* 5295 * No extra check here, the segment length will be checked by PMD 5296 * in the extended queue setup. 5297 */ 5298 for (i = 0; i < nb_segs; i++) { 5299 if (seg_lengths[i] >= UINT16_MAX) { 5300 printf("length[%u]=%u > UINT16_MAX - give up\n", 5301 i, seg_lengths[i]); 5302 return; 5303 } 5304 } 5305 5306 for (i = 0; i < nb_segs; i++) 5307 rx_pkt_seg_lengths[i] = (uint16_t) seg_lengths[i]; 5308 5309 rx_pkt_nb_segs = (uint8_t) nb_segs; 5310 } 5311 5312 void 5313 show_tx_pkt_segments(void) 5314 { 5315 uint32_t i, n; 5316 const char *split; 5317 5318 n = tx_pkt_nb_segs; 5319 split = tx_split_get_name(tx_pkt_split); 5320 5321 printf("Number of segments: %u\n", n); 5322 printf("Segment sizes: "); 5323 for (i = 0; i != n - 1; i++) 5324 printf("%hu,", tx_pkt_seg_lengths[i]); 5325 printf("%hu\n", tx_pkt_seg_lengths[i]); 5326 printf("Split packet: %s\n", split); 5327 } 5328 5329 static bool 5330 nb_segs_is_invalid(unsigned int nb_segs) 5331 { 5332 uint16_t ring_size; 5333 uint16_t queue_id; 5334 uint16_t port_id; 5335 int ret; 5336 5337 RTE_ETH_FOREACH_DEV(port_id) { 5338 for (queue_id = 0; queue_id < nb_txq; queue_id++) { 5339 ret = get_tx_ring_size(port_id, queue_id, &ring_size); 5340 if (ret) { 5341 /* Port may not be initialized yet, can't say 5342 * the port is invalid in this stage. 5343 */ 5344 continue; 5345 } 5346 if (ring_size < nb_segs) { 5347 printf("nb segments per TX packets=%u >= TX " 5348 "queue(%u) ring_size=%u - txpkts ignored\n", 5349 nb_segs, queue_id, ring_size); 5350 return true; 5351 } 5352 } 5353 } 5354 5355 return false; 5356 } 5357 5358 void 5359 set_tx_pkt_segments(unsigned int *seg_lengths, unsigned int nb_segs) 5360 { 5361 uint16_t tx_pkt_len; 5362 unsigned int i; 5363 5364 /* 5365 * For single segment settings failed check is ignored. 5366 * It is a very basic capability to send the single segment 5367 * packets, suppose it is always supported. 5368 */ 5369 if (nb_segs > 1 && nb_segs_is_invalid(nb_segs)) { 5370 fprintf(stderr, 5371 "Tx segment size(%u) is not supported - txpkts ignored\n", 5372 nb_segs); 5373 return; 5374 } 5375 5376 if (nb_segs > RTE_MAX_SEGS_PER_PKT) { 5377 fprintf(stderr, 5378 "Tx segment size(%u) is bigger than max number of segment(%u)\n", 5379 nb_segs, RTE_MAX_SEGS_PER_PKT); 5380 return; 5381 } 5382 5383 /* 5384 * Check that each segment length is greater or equal than 5385 * the mbuf data size. 5386 * Check also that the total packet length is greater or equal than the 5387 * size of an empty UDP/IP packet (sizeof(struct rte_ether_hdr) + 5388 * 20 + 8). 5389 */ 5390 tx_pkt_len = 0; 5391 for (i = 0; i < nb_segs; i++) { 5392 if (seg_lengths[i] > mbuf_data_size[0]) { 5393 fprintf(stderr, 5394 "length[%u]=%u > mbuf_data_size=%u - give up\n", 5395 i, seg_lengths[i], mbuf_data_size[0]); 5396 return; 5397 } 5398 tx_pkt_len = (uint16_t)(tx_pkt_len + seg_lengths[i]); 5399 } 5400 if (tx_pkt_len < (sizeof(struct rte_ether_hdr) + 20 + 8)) { 5401 fprintf(stderr, "total packet length=%u < %d - give up\n", 5402 (unsigned) tx_pkt_len, 5403 (int)(sizeof(struct rte_ether_hdr) + 20 + 8)); 5404 return; 5405 } 5406 5407 for (i = 0; i < nb_segs; i++) 5408 tx_pkt_seg_lengths[i] = (uint16_t) seg_lengths[i]; 5409 5410 tx_pkt_length = tx_pkt_len; 5411 tx_pkt_nb_segs = (uint8_t) nb_segs; 5412 } 5413 5414 void 5415 show_tx_pkt_times(void) 5416 { 5417 printf("Interburst gap: %u\n", tx_pkt_times_inter); 5418 printf("Intraburst gap: %u\n", tx_pkt_times_intra); 5419 } 5420 5421 void 5422 set_tx_pkt_times(unsigned int *tx_times) 5423 { 5424 tx_pkt_times_inter = tx_times[0]; 5425 tx_pkt_times_intra = tx_times[1]; 5426 } 5427 5428 #ifdef RTE_LIB_GRO 5429 void 5430 setup_gro(const char *onoff, portid_t port_id) 5431 { 5432 if (!rte_eth_dev_is_valid_port(port_id)) { 5433 fprintf(stderr, "invalid port id %u\n", port_id); 5434 return; 5435 } 5436 if (test_done == 0) { 5437 fprintf(stderr, 5438 "Before enable/disable GRO, please stop forwarding first\n"); 5439 return; 5440 } 5441 if (strcmp(onoff, "on") == 0) { 5442 if (gro_ports[port_id].enable != 0) { 5443 fprintf(stderr, 5444 "Port %u has enabled GRO. Please disable GRO first\n", 5445 port_id); 5446 return; 5447 } 5448 if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) { 5449 gro_ports[port_id].param.gro_types = RTE_GRO_TCP_IPV4; 5450 gro_ports[port_id].param.max_flow_num = 5451 GRO_DEFAULT_FLOW_NUM; 5452 gro_ports[port_id].param.max_item_per_flow = 5453 GRO_DEFAULT_ITEM_NUM_PER_FLOW; 5454 } 5455 gro_ports[port_id].enable = 1; 5456 } else { 5457 if (gro_ports[port_id].enable == 0) { 5458 fprintf(stderr, "Port %u has disabled GRO\n", port_id); 5459 return; 5460 } 5461 gro_ports[port_id].enable = 0; 5462 } 5463 } 5464 5465 void 5466 setup_gro_flush_cycles(uint8_t cycles) 5467 { 5468 if (test_done == 0) { 5469 fprintf(stderr, 5470 "Before change flush interval for GRO, please stop forwarding first.\n"); 5471 return; 5472 } 5473 5474 if (cycles > GRO_MAX_FLUSH_CYCLES || cycles < 5475 GRO_DEFAULT_FLUSH_CYCLES) { 5476 fprintf(stderr, 5477 "The flushing cycle be in the range of 1 to %u. Revert to the default value %u.\n", 5478 GRO_MAX_FLUSH_CYCLES, GRO_DEFAULT_FLUSH_CYCLES); 5479 cycles = GRO_DEFAULT_FLUSH_CYCLES; 5480 } 5481 5482 gro_flush_cycles = cycles; 5483 } 5484 5485 void 5486 show_gro(portid_t port_id) 5487 { 5488 struct rte_gro_param *param; 5489 uint32_t max_pkts_num; 5490 5491 param = &gro_ports[port_id].param; 5492 5493 if (!rte_eth_dev_is_valid_port(port_id)) { 5494 fprintf(stderr, "Invalid port id %u.\n", port_id); 5495 return; 5496 } 5497 if (gro_ports[port_id].enable) { 5498 printf("GRO type: TCP/IPv4\n"); 5499 if (gro_flush_cycles == GRO_DEFAULT_FLUSH_CYCLES) { 5500 max_pkts_num = param->max_flow_num * 5501 param->max_item_per_flow; 5502 } else 5503 max_pkts_num = MAX_PKT_BURST * GRO_MAX_FLUSH_CYCLES; 5504 printf("Max number of packets to perform GRO: %u\n", 5505 max_pkts_num); 5506 printf("Flushing cycles: %u\n", gro_flush_cycles); 5507 } else 5508 printf("Port %u doesn't enable GRO.\n", port_id); 5509 } 5510 #endif /* RTE_LIB_GRO */ 5511 5512 #ifdef RTE_LIB_GSO 5513 void 5514 setup_gso(const char *mode, portid_t port_id) 5515 { 5516 if (!rte_eth_dev_is_valid_port(port_id)) { 5517 fprintf(stderr, "invalid port id %u\n", port_id); 5518 return; 5519 } 5520 if (strcmp(mode, "on") == 0) { 5521 if (test_done == 0) { 5522 fprintf(stderr, 5523 "before enabling GSO, please stop forwarding first\n"); 5524 return; 5525 } 5526 gso_ports[port_id].enable = 1; 5527 } else if (strcmp(mode, "off") == 0) { 5528 if (test_done == 0) { 5529 fprintf(stderr, 5530 "before disabling GSO, please stop forwarding first\n"); 5531 return; 5532 } 5533 gso_ports[port_id].enable = 0; 5534 } 5535 } 5536 #endif /* RTE_LIB_GSO */ 5537 5538 char* 5539 list_pkt_forwarding_modes(void) 5540 { 5541 static char fwd_modes[128] = ""; 5542 const char *separator = "|"; 5543 struct fwd_engine *fwd_eng; 5544 unsigned i = 0; 5545 5546 if (strlen (fwd_modes) == 0) { 5547 while ((fwd_eng = fwd_engines[i++]) != NULL) { 5548 strncat(fwd_modes, fwd_eng->fwd_mode_name, 5549 sizeof(fwd_modes) - strlen(fwd_modes) - 1); 5550 strncat(fwd_modes, separator, 5551 sizeof(fwd_modes) - strlen(fwd_modes) - 1); 5552 } 5553 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0'; 5554 } 5555 5556 return fwd_modes; 5557 } 5558 5559 char* 5560 list_pkt_forwarding_retry_modes(void) 5561 { 5562 static char fwd_modes[128] = ""; 5563 const char *separator = "|"; 5564 struct fwd_engine *fwd_eng; 5565 unsigned i = 0; 5566 5567 if (strlen(fwd_modes) == 0) { 5568 while ((fwd_eng = fwd_engines[i++]) != NULL) { 5569 if (fwd_eng == &rx_only_engine) 5570 continue; 5571 strncat(fwd_modes, fwd_eng->fwd_mode_name, 5572 sizeof(fwd_modes) - 5573 strlen(fwd_modes) - 1); 5574 strncat(fwd_modes, separator, 5575 sizeof(fwd_modes) - 5576 strlen(fwd_modes) - 1); 5577 } 5578 fwd_modes[strlen(fwd_modes) - strlen(separator)] = '\0'; 5579 } 5580 5581 return fwd_modes; 5582 } 5583 5584 void 5585 set_pkt_forwarding_mode(const char *fwd_mode_name) 5586 { 5587 struct fwd_engine *fwd_eng; 5588 unsigned i; 5589 5590 i = 0; 5591 while ((fwd_eng = fwd_engines[i]) != NULL) { 5592 if (! strcmp(fwd_eng->fwd_mode_name, fwd_mode_name)) { 5593 printf("Set %s packet forwarding mode%s\n", 5594 fwd_mode_name, 5595 retry_enabled == 0 ? "" : " with retry"); 5596 cur_fwd_eng = fwd_eng; 5597 return; 5598 } 5599 i++; 5600 } 5601 fprintf(stderr, "Invalid %s packet forwarding mode\n", fwd_mode_name); 5602 } 5603 5604 void 5605 add_rx_dump_callbacks(portid_t portid) 5606 { 5607 struct rte_eth_dev_info dev_info; 5608 uint16_t queue; 5609 int ret; 5610 5611 if (port_id_is_invalid(portid, ENABLED_WARN)) 5612 return; 5613 5614 ret = eth_dev_info_get_print_err(portid, &dev_info); 5615 if (ret != 0) 5616 return; 5617 5618 for (queue = 0; queue < dev_info.nb_rx_queues; queue++) 5619 if (!ports[portid].rx_dump_cb[queue]) 5620 ports[portid].rx_dump_cb[queue] = 5621 rte_eth_add_rx_callback(portid, queue, 5622 dump_rx_pkts, NULL); 5623 } 5624 5625 void 5626 add_tx_dump_callbacks(portid_t portid) 5627 { 5628 struct rte_eth_dev_info dev_info; 5629 uint16_t queue; 5630 int ret; 5631 5632 if (port_id_is_invalid(portid, ENABLED_WARN)) 5633 return; 5634 5635 ret = eth_dev_info_get_print_err(portid, &dev_info); 5636 if (ret != 0) 5637 return; 5638 5639 for (queue = 0; queue < dev_info.nb_tx_queues; queue++) 5640 if (!ports[portid].tx_dump_cb[queue]) 5641 ports[portid].tx_dump_cb[queue] = 5642 rte_eth_add_tx_callback(portid, queue, 5643 dump_tx_pkts, NULL); 5644 } 5645 5646 void 5647 remove_rx_dump_callbacks(portid_t portid) 5648 { 5649 struct rte_eth_dev_info dev_info; 5650 uint16_t queue; 5651 int ret; 5652 5653 if (port_id_is_invalid(portid, ENABLED_WARN)) 5654 return; 5655 5656 ret = eth_dev_info_get_print_err(portid, &dev_info); 5657 if (ret != 0) 5658 return; 5659 5660 for (queue = 0; queue < dev_info.nb_rx_queues; queue++) 5661 if (ports[portid].rx_dump_cb[queue]) { 5662 rte_eth_remove_rx_callback(portid, queue, 5663 ports[portid].rx_dump_cb[queue]); 5664 ports[portid].rx_dump_cb[queue] = NULL; 5665 } 5666 } 5667 5668 void 5669 remove_tx_dump_callbacks(portid_t portid) 5670 { 5671 struct rte_eth_dev_info dev_info; 5672 uint16_t queue; 5673 int ret; 5674 5675 if (port_id_is_invalid(portid, ENABLED_WARN)) 5676 return; 5677 5678 ret = eth_dev_info_get_print_err(portid, &dev_info); 5679 if (ret != 0) 5680 return; 5681 5682 for (queue = 0; queue < dev_info.nb_tx_queues; queue++) 5683 if (ports[portid].tx_dump_cb[queue]) { 5684 rte_eth_remove_tx_callback(portid, queue, 5685 ports[portid].tx_dump_cb[queue]); 5686 ports[portid].tx_dump_cb[queue] = NULL; 5687 } 5688 } 5689 5690 void 5691 configure_rxtx_dump_callbacks(uint16_t verbose) 5692 { 5693 portid_t portid; 5694 5695 #ifndef RTE_ETHDEV_RXTX_CALLBACKS 5696 TESTPMD_LOG(ERR, "setting rxtx callbacks is not enabled\n"); 5697 return; 5698 #endif 5699 5700 RTE_ETH_FOREACH_DEV(portid) 5701 { 5702 if (verbose == 1 || verbose > 2) 5703 add_rx_dump_callbacks(portid); 5704 else 5705 remove_rx_dump_callbacks(portid); 5706 if (verbose >= 2) 5707 add_tx_dump_callbacks(portid); 5708 else 5709 remove_tx_dump_callbacks(portid); 5710 } 5711 } 5712 5713 void 5714 set_verbose_level(uint16_t vb_level) 5715 { 5716 printf("Change verbose level from %u to %u\n", 5717 (unsigned int) verbose_level, (unsigned int) vb_level); 5718 verbose_level = vb_level; 5719 configure_rxtx_dump_callbacks(verbose_level); 5720 } 5721 5722 void 5723 vlan_extend_set(portid_t port_id, int on) 5724 { 5725 int diag; 5726 int vlan_offload; 5727 uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads; 5728 5729 if (port_id_is_invalid(port_id, ENABLED_WARN)) 5730 return; 5731 5732 vlan_offload = rte_eth_dev_get_vlan_offload(port_id); 5733 5734 if (on) { 5735 vlan_offload |= RTE_ETH_VLAN_EXTEND_OFFLOAD; 5736 port_rx_offloads |= RTE_ETH_RX_OFFLOAD_VLAN_EXTEND; 5737 } else { 5738 vlan_offload &= ~RTE_ETH_VLAN_EXTEND_OFFLOAD; 5739 port_rx_offloads &= ~RTE_ETH_RX_OFFLOAD_VLAN_EXTEND; 5740 } 5741 5742 diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload); 5743 if (diag < 0) { 5744 fprintf(stderr, 5745 "rx_vlan_extend_set(port_pi=%d, on=%d) failed diag=%d\n", 5746 port_id, on, diag); 5747 return; 5748 } 5749 ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads; 5750 } 5751 5752 void 5753 rx_vlan_strip_set(portid_t port_id, int on) 5754 { 5755 int diag; 5756 int vlan_offload; 5757 uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads; 5758 5759 if (port_id_is_invalid(port_id, ENABLED_WARN)) 5760 return; 5761 5762 vlan_offload = rte_eth_dev_get_vlan_offload(port_id); 5763 5764 if (on) { 5765 vlan_offload |= RTE_ETH_VLAN_STRIP_OFFLOAD; 5766 port_rx_offloads |= RTE_ETH_RX_OFFLOAD_VLAN_STRIP; 5767 } else { 5768 vlan_offload &= ~RTE_ETH_VLAN_STRIP_OFFLOAD; 5769 port_rx_offloads &= ~RTE_ETH_RX_OFFLOAD_VLAN_STRIP; 5770 } 5771 5772 diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload); 5773 if (diag < 0) { 5774 fprintf(stderr, 5775 "%s(port_pi=%d, on=%d) failed diag=%d\n", 5776 __func__, port_id, on, diag); 5777 return; 5778 } 5779 ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads; 5780 } 5781 5782 void 5783 rx_vlan_strip_set_on_queue(portid_t port_id, uint16_t queue_id, int on) 5784 { 5785 int diag; 5786 5787 if (port_id_is_invalid(port_id, ENABLED_WARN)) 5788 return; 5789 5790 diag = rte_eth_dev_set_vlan_strip_on_queue(port_id, queue_id, on); 5791 if (diag < 0) 5792 fprintf(stderr, 5793 "%s(port_pi=%d, queue_id=%d, on=%d) failed diag=%d\n", 5794 __func__, port_id, queue_id, on, diag); 5795 } 5796 5797 void 5798 rx_vlan_filter_set(portid_t port_id, int on) 5799 { 5800 int diag; 5801 int vlan_offload; 5802 uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads; 5803 5804 if (port_id_is_invalid(port_id, ENABLED_WARN)) 5805 return; 5806 5807 vlan_offload = rte_eth_dev_get_vlan_offload(port_id); 5808 5809 if (on) { 5810 vlan_offload |= RTE_ETH_VLAN_FILTER_OFFLOAD; 5811 port_rx_offloads |= RTE_ETH_RX_OFFLOAD_VLAN_FILTER; 5812 } else { 5813 vlan_offload &= ~RTE_ETH_VLAN_FILTER_OFFLOAD; 5814 port_rx_offloads &= ~RTE_ETH_RX_OFFLOAD_VLAN_FILTER; 5815 } 5816 5817 diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload); 5818 if (diag < 0) { 5819 fprintf(stderr, 5820 "%s(port_pi=%d, on=%d) failed diag=%d\n", 5821 __func__, port_id, on, diag); 5822 return; 5823 } 5824 ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads; 5825 } 5826 5827 void 5828 rx_vlan_qinq_strip_set(portid_t port_id, int on) 5829 { 5830 int diag; 5831 int vlan_offload; 5832 uint64_t port_rx_offloads = ports[port_id].dev_conf.rxmode.offloads; 5833 5834 if (port_id_is_invalid(port_id, ENABLED_WARN)) 5835 return; 5836 5837 vlan_offload = rte_eth_dev_get_vlan_offload(port_id); 5838 5839 if (on) { 5840 vlan_offload |= RTE_ETH_QINQ_STRIP_OFFLOAD; 5841 port_rx_offloads |= RTE_ETH_RX_OFFLOAD_QINQ_STRIP; 5842 } else { 5843 vlan_offload &= ~RTE_ETH_QINQ_STRIP_OFFLOAD; 5844 port_rx_offloads &= ~RTE_ETH_RX_OFFLOAD_QINQ_STRIP; 5845 } 5846 5847 diag = rte_eth_dev_set_vlan_offload(port_id, vlan_offload); 5848 if (diag < 0) { 5849 fprintf(stderr, "%s(port_pi=%d, on=%d) failed diag=%d\n", 5850 __func__, port_id, on, diag); 5851 return; 5852 } 5853 ports[port_id].dev_conf.rxmode.offloads = port_rx_offloads; 5854 } 5855 5856 int 5857 rx_vft_set(portid_t port_id, uint16_t vlan_id, int on) 5858 { 5859 int diag; 5860 5861 if (port_id_is_invalid(port_id, ENABLED_WARN)) 5862 return 1; 5863 if (vlan_id_is_invalid(vlan_id)) 5864 return 1; 5865 diag = rte_eth_dev_vlan_filter(port_id, vlan_id, on); 5866 if (diag == 0) 5867 return 0; 5868 fprintf(stderr, 5869 "rte_eth_dev_vlan_filter(port_pi=%d, vlan_id=%d, on=%d) failed diag=%d\n", 5870 port_id, vlan_id, on, diag); 5871 return -1; 5872 } 5873 5874 void 5875 rx_vlan_all_filter_set(portid_t port_id, int on) 5876 { 5877 uint16_t vlan_id; 5878 5879 if (port_id_is_invalid(port_id, ENABLED_WARN)) 5880 return; 5881 for (vlan_id = 0; vlan_id < 4096; vlan_id++) { 5882 if (rx_vft_set(port_id, vlan_id, on)) 5883 break; 5884 } 5885 } 5886 5887 void 5888 vlan_tpid_set(portid_t port_id, enum rte_vlan_type vlan_type, uint16_t tp_id) 5889 { 5890 int diag; 5891 5892 if (port_id_is_invalid(port_id, ENABLED_WARN)) 5893 return; 5894 5895 diag = rte_eth_dev_set_vlan_ether_type(port_id, vlan_type, tp_id); 5896 if (diag == 0) 5897 return; 5898 5899 fprintf(stderr, 5900 "tx_vlan_tpid_set(port_pi=%d, vlan_type=%d, tpid=%d) failed diag=%d\n", 5901 port_id, vlan_type, tp_id, diag); 5902 } 5903 5904 void 5905 tx_vlan_set(portid_t port_id, uint16_t vlan_id) 5906 { 5907 struct rte_eth_dev_info dev_info; 5908 int ret; 5909 5910 if (vlan_id_is_invalid(vlan_id)) 5911 return; 5912 5913 if (ports[port_id].dev_conf.txmode.offloads & 5914 RTE_ETH_TX_OFFLOAD_QINQ_INSERT) { 5915 fprintf(stderr, "Error, as QinQ has been enabled.\n"); 5916 return; 5917 } 5918 5919 ret = eth_dev_info_get_print_err(port_id, &dev_info); 5920 if (ret != 0) 5921 return; 5922 5923 if ((dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_VLAN_INSERT) == 0) { 5924 fprintf(stderr, 5925 "Error: vlan insert is not supported by port %d\n", 5926 port_id); 5927 return; 5928 } 5929 5930 tx_vlan_reset(port_id); 5931 ports[port_id].dev_conf.txmode.offloads |= RTE_ETH_TX_OFFLOAD_VLAN_INSERT; 5932 ports[port_id].tx_vlan_id = vlan_id; 5933 } 5934 5935 void 5936 tx_qinq_set(portid_t port_id, uint16_t vlan_id, uint16_t vlan_id_outer) 5937 { 5938 struct rte_eth_dev_info dev_info; 5939 int ret; 5940 5941 if (vlan_id_is_invalid(vlan_id)) 5942 return; 5943 if (vlan_id_is_invalid(vlan_id_outer)) 5944 return; 5945 5946 ret = eth_dev_info_get_print_err(port_id, &dev_info); 5947 if (ret != 0) 5948 return; 5949 5950 if ((dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_QINQ_INSERT) == 0) { 5951 fprintf(stderr, 5952 "Error: qinq insert not supported by port %d\n", 5953 port_id); 5954 return; 5955 } 5956 5957 tx_vlan_reset(port_id); 5958 ports[port_id].dev_conf.txmode.offloads |= (RTE_ETH_TX_OFFLOAD_VLAN_INSERT | 5959 RTE_ETH_TX_OFFLOAD_QINQ_INSERT); 5960 ports[port_id].tx_vlan_id = vlan_id; 5961 ports[port_id].tx_vlan_id_outer = vlan_id_outer; 5962 } 5963 5964 void 5965 tx_vlan_reset(portid_t port_id) 5966 { 5967 ports[port_id].dev_conf.txmode.offloads &= 5968 ~(RTE_ETH_TX_OFFLOAD_VLAN_INSERT | 5969 RTE_ETH_TX_OFFLOAD_QINQ_INSERT); 5970 ports[port_id].tx_vlan_id = 0; 5971 ports[port_id].tx_vlan_id_outer = 0; 5972 } 5973 5974 void 5975 tx_vlan_pvid_set(portid_t port_id, uint16_t vlan_id, int on) 5976 { 5977 if (port_id_is_invalid(port_id, ENABLED_WARN)) 5978 return; 5979 5980 rte_eth_dev_set_vlan_pvid(port_id, vlan_id, on); 5981 } 5982 5983 void 5984 set_qmap(portid_t port_id, uint8_t is_rx, uint16_t queue_id, uint8_t map_value) 5985 { 5986 int ret; 5987 5988 if (port_id_is_invalid(port_id, ENABLED_WARN)) 5989 return; 5990 5991 if (is_rx ? (rx_queue_id_is_invalid(queue_id)) : (tx_queue_id_is_invalid(queue_id))) 5992 return; 5993 5994 if (map_value >= RTE_ETHDEV_QUEUE_STAT_CNTRS) { 5995 fprintf(stderr, "map_value not in required range 0..%d\n", 5996 RTE_ETHDEV_QUEUE_STAT_CNTRS - 1); 5997 return; 5998 } 5999 6000 if (!is_rx) { /* tx */ 6001 ret = rte_eth_dev_set_tx_queue_stats_mapping(port_id, queue_id, 6002 map_value); 6003 if (ret) { 6004 fprintf(stderr, 6005 "failed to set tx queue stats mapping.\n"); 6006 return; 6007 } 6008 } else { /* rx */ 6009 ret = rte_eth_dev_set_rx_queue_stats_mapping(port_id, queue_id, 6010 map_value); 6011 if (ret) { 6012 fprintf(stderr, 6013 "failed to set rx queue stats mapping.\n"); 6014 return; 6015 } 6016 } 6017 } 6018 6019 void 6020 set_xstats_hide_zero(uint8_t on_off) 6021 { 6022 xstats_hide_zero = on_off; 6023 } 6024 6025 void 6026 set_record_core_cycles(uint8_t on_off) 6027 { 6028 record_core_cycles = on_off; 6029 } 6030 6031 void 6032 set_record_burst_stats(uint8_t on_off) 6033 { 6034 record_burst_stats = on_off; 6035 } 6036 6037 uint16_t 6038 str_to_flowtype(const char *string) 6039 { 6040 uint8_t i; 6041 6042 for (i = 0; i < RTE_DIM(flowtype_str_table); i++) { 6043 if (!strcmp(flowtype_str_table[i].str, string)) 6044 return flowtype_str_table[i].ftype; 6045 } 6046 6047 if (isdigit(string[0])) { 6048 int val = atoi(string); 6049 if (val > 0 && val < 64) 6050 return (uint16_t)val; 6051 } 6052 6053 return RTE_ETH_FLOW_UNKNOWN; 6054 } 6055 6056 const char* 6057 flowtype_to_str(uint16_t flow_type) 6058 { 6059 uint8_t i; 6060 6061 for (i = 0; i < RTE_DIM(flowtype_str_table); i++) { 6062 if (flowtype_str_table[i].ftype == flow_type) 6063 return flowtype_str_table[i].str; 6064 } 6065 6066 return NULL; 6067 } 6068 6069 #if defined(RTE_NET_I40E) || defined(RTE_NET_IXGBE) 6070 6071 static inline void 6072 print_fdir_flex_payload(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num) 6073 { 6074 struct rte_eth_flex_payload_cfg *cfg; 6075 uint32_t i, j; 6076 6077 for (i = 0; i < flex_conf->nb_payloads; i++) { 6078 cfg = &flex_conf->flex_set[i]; 6079 if (cfg->type == RTE_ETH_RAW_PAYLOAD) 6080 printf("\n RAW: "); 6081 else if (cfg->type == RTE_ETH_L2_PAYLOAD) 6082 printf("\n L2_PAYLOAD: "); 6083 else if (cfg->type == RTE_ETH_L3_PAYLOAD) 6084 printf("\n L3_PAYLOAD: "); 6085 else if (cfg->type == RTE_ETH_L4_PAYLOAD) 6086 printf("\n L4_PAYLOAD: "); 6087 else 6088 printf("\n UNKNOWN PAYLOAD(%u): ", cfg->type); 6089 for (j = 0; j < num; j++) 6090 printf(" %-5u", cfg->src_offset[j]); 6091 } 6092 printf("\n"); 6093 } 6094 6095 static inline void 6096 print_fdir_flex_mask(struct rte_eth_fdir_flex_conf *flex_conf, uint32_t num) 6097 { 6098 struct rte_eth_fdir_flex_mask *mask; 6099 uint32_t i, j; 6100 const char *p; 6101 6102 for (i = 0; i < flex_conf->nb_flexmasks; i++) { 6103 mask = &flex_conf->flex_mask[i]; 6104 p = flowtype_to_str(mask->flow_type); 6105 printf("\n %s:\t", p ? p : "unknown"); 6106 for (j = 0; j < num; j++) 6107 printf(" %02x", mask->mask[j]); 6108 } 6109 printf("\n"); 6110 } 6111 6112 static inline void 6113 print_fdir_flow_type(uint32_t flow_types_mask) 6114 { 6115 int i; 6116 const char *p; 6117 6118 for (i = RTE_ETH_FLOW_UNKNOWN; i < RTE_ETH_FLOW_MAX; i++) { 6119 if (!(flow_types_mask & (1 << i))) 6120 continue; 6121 p = flowtype_to_str(i); 6122 if (p) 6123 printf(" %s", p); 6124 else 6125 printf(" unknown"); 6126 } 6127 printf("\n"); 6128 } 6129 6130 static int 6131 get_fdir_info(portid_t port_id, struct rte_eth_fdir_info *fdir_info, 6132 struct rte_eth_fdir_stats *fdir_stat) 6133 { 6134 int ret = -ENOTSUP; 6135 6136 #ifdef RTE_NET_I40E 6137 if (ret == -ENOTSUP) { 6138 ret = rte_pmd_i40e_get_fdir_info(port_id, fdir_info); 6139 if (!ret) 6140 ret = rte_pmd_i40e_get_fdir_stats(port_id, fdir_stat); 6141 } 6142 #endif 6143 #ifdef RTE_NET_IXGBE 6144 if (ret == -ENOTSUP) { 6145 ret = rte_pmd_ixgbe_get_fdir_info(port_id, fdir_info); 6146 if (!ret) 6147 ret = rte_pmd_ixgbe_get_fdir_stats(port_id, fdir_stat); 6148 } 6149 #endif 6150 switch (ret) { 6151 case 0: 6152 break; 6153 case -ENOTSUP: 6154 fprintf(stderr, "\n FDIR is not supported on port %-2d\n", 6155 port_id); 6156 break; 6157 default: 6158 fprintf(stderr, "programming error: (%s)\n", strerror(-ret)); 6159 break; 6160 } 6161 return ret; 6162 } 6163 6164 void 6165 fdir_get_infos(portid_t port_id) 6166 { 6167 struct rte_eth_fdir_stats fdir_stat; 6168 struct rte_eth_fdir_info fdir_info; 6169 6170 static const char *fdir_stats_border = "########################"; 6171 6172 if (port_id_is_invalid(port_id, ENABLED_WARN)) 6173 return; 6174 6175 memset(&fdir_info, 0, sizeof(fdir_info)); 6176 memset(&fdir_stat, 0, sizeof(fdir_stat)); 6177 if (get_fdir_info(port_id, &fdir_info, &fdir_stat)) 6178 return; 6179 6180 printf("\n %s FDIR infos for port %-2d %s\n", 6181 fdir_stats_border, port_id, fdir_stats_border); 6182 printf(" MODE: "); 6183 if (fdir_info.mode == RTE_FDIR_MODE_PERFECT) 6184 printf(" PERFECT\n"); 6185 else if (fdir_info.mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN) 6186 printf(" PERFECT-MAC-VLAN\n"); 6187 else if (fdir_info.mode == RTE_FDIR_MODE_PERFECT_TUNNEL) 6188 printf(" PERFECT-TUNNEL\n"); 6189 else if (fdir_info.mode == RTE_FDIR_MODE_SIGNATURE) 6190 printf(" SIGNATURE\n"); 6191 else 6192 printf(" DISABLE\n"); 6193 if (fdir_info.mode != RTE_FDIR_MODE_PERFECT_MAC_VLAN 6194 && fdir_info.mode != RTE_FDIR_MODE_PERFECT_TUNNEL) { 6195 printf(" SUPPORTED FLOW TYPE: "); 6196 print_fdir_flow_type(fdir_info.flow_types_mask[0]); 6197 } 6198 printf(" FLEX PAYLOAD INFO:\n"); 6199 printf(" max_len: %-10"PRIu32" payload_limit: %-10"PRIu32"\n" 6200 " payload_unit: %-10"PRIu32" payload_seg: %-10"PRIu32"\n" 6201 " bitmask_unit: %-10"PRIu32" bitmask_num: %-10"PRIu32"\n", 6202 fdir_info.max_flexpayload, fdir_info.flex_payload_limit, 6203 fdir_info.flex_payload_unit, 6204 fdir_info.max_flex_payload_segment_num, 6205 fdir_info.flex_bitmask_unit, fdir_info.max_flex_bitmask_num); 6206 if (fdir_info.flex_conf.nb_payloads > 0) { 6207 printf(" FLEX PAYLOAD SRC OFFSET:"); 6208 print_fdir_flex_payload(&fdir_info.flex_conf, fdir_info.max_flexpayload); 6209 } 6210 if (fdir_info.flex_conf.nb_flexmasks > 0) { 6211 printf(" FLEX MASK CFG:"); 6212 print_fdir_flex_mask(&fdir_info.flex_conf, fdir_info.max_flexpayload); 6213 } 6214 printf(" guarant_count: %-10"PRIu32" best_count: %"PRIu32"\n", 6215 fdir_stat.guarant_cnt, fdir_stat.best_cnt); 6216 printf(" guarant_space: %-10"PRIu32" best_space: %"PRIu32"\n", 6217 fdir_info.guarant_spc, fdir_info.best_spc); 6218 printf(" collision: %-10"PRIu32" free: %"PRIu32"\n" 6219 " maxhash: %-10"PRIu32" maxlen: %"PRIu32"\n" 6220 " add: %-10"PRIu64" remove: %"PRIu64"\n" 6221 " f_add: %-10"PRIu64" f_remove: %"PRIu64"\n", 6222 fdir_stat.collision, fdir_stat.free, 6223 fdir_stat.maxhash, fdir_stat.maxlen, 6224 fdir_stat.add, fdir_stat.remove, 6225 fdir_stat.f_add, fdir_stat.f_remove); 6226 printf(" %s############################%s\n", 6227 fdir_stats_border, fdir_stats_border); 6228 } 6229 6230 #endif /* RTE_NET_I40E || RTE_NET_IXGBE */ 6231 6232 void 6233 set_vf_traffic(portid_t port_id, uint8_t is_rx, uint16_t vf, uint8_t on) 6234 { 6235 #ifdef RTE_NET_IXGBE 6236 int diag; 6237 6238 if (is_rx) 6239 diag = rte_pmd_ixgbe_set_vf_rx(port_id, vf, on); 6240 else 6241 diag = rte_pmd_ixgbe_set_vf_tx(port_id, vf, on); 6242 6243 if (diag == 0) 6244 return; 6245 fprintf(stderr, 6246 "rte_pmd_ixgbe_set_vf_%s for port_id=%d failed diag=%d\n", 6247 is_rx ? "rx" : "tx", port_id, diag); 6248 return; 6249 #endif 6250 fprintf(stderr, "VF %s setting not supported for port %d\n", 6251 is_rx ? "Rx" : "Tx", port_id); 6252 RTE_SET_USED(vf); 6253 RTE_SET_USED(on); 6254 } 6255 6256 int 6257 set_queue_rate_limit(portid_t port_id, uint16_t queue_idx, uint32_t rate) 6258 { 6259 int diag; 6260 struct rte_eth_link link; 6261 int ret; 6262 6263 if (port_id_is_invalid(port_id, ENABLED_WARN)) 6264 return 1; 6265 ret = eth_link_get_nowait_print_err(port_id, &link); 6266 if (ret < 0) 6267 return 1; 6268 if (link.link_speed != RTE_ETH_SPEED_NUM_UNKNOWN && 6269 rate > link.link_speed) { 6270 fprintf(stderr, 6271 "Invalid rate value:%u bigger than link speed: %u\n", 6272 rate, link.link_speed); 6273 return 1; 6274 } 6275 diag = rte_eth_set_queue_rate_limit(port_id, queue_idx, rate); 6276 if (diag == 0) 6277 return diag; 6278 fprintf(stderr, 6279 "rte_eth_set_queue_rate_limit for port_id=%d failed diag=%d\n", 6280 port_id, diag); 6281 return diag; 6282 } 6283 6284 int 6285 set_vf_rate_limit(portid_t port_id, uint16_t vf, uint32_t rate, uint64_t q_msk) 6286 { 6287 int diag = -ENOTSUP; 6288 6289 RTE_SET_USED(vf); 6290 RTE_SET_USED(rate); 6291 RTE_SET_USED(q_msk); 6292 6293 #ifdef RTE_NET_IXGBE 6294 if (diag == -ENOTSUP) 6295 diag = rte_pmd_ixgbe_set_vf_rate_limit(port_id, vf, rate, 6296 q_msk); 6297 #endif 6298 #ifdef RTE_NET_BNXT 6299 if (diag == -ENOTSUP) 6300 diag = rte_pmd_bnxt_set_vf_rate_limit(port_id, vf, rate, q_msk); 6301 #endif 6302 if (diag == 0) 6303 return diag; 6304 6305 fprintf(stderr, 6306 "%s for port_id=%d failed diag=%d\n", 6307 __func__, port_id, diag); 6308 return diag; 6309 } 6310 6311 int 6312 set_rxq_avail_thresh(portid_t port_id, uint16_t queue_id, uint8_t avail_thresh) 6313 { 6314 if (port_id_is_invalid(port_id, ENABLED_WARN)) 6315 return -EINVAL; 6316 6317 return rte_eth_rx_avail_thresh_set(port_id, queue_id, avail_thresh); 6318 } 6319 6320 /* 6321 * Functions to manage the set of filtered Multicast MAC addresses. 6322 * 6323 * A pool of filtered multicast MAC addresses is associated with each port. 6324 * The pool is allocated in chunks of MCAST_POOL_INC multicast addresses. 6325 * The address of the pool and the number of valid multicast MAC addresses 6326 * recorded in the pool are stored in the fields "mc_addr_pool" and 6327 * "mc_addr_nb" of the "rte_port" data structure. 6328 * 6329 * The function "rte_eth_dev_set_mc_addr_list" of the PMDs API imposes 6330 * to be supplied a contiguous array of multicast MAC addresses. 6331 * To comply with this constraint, the set of multicast addresses recorded 6332 * into the pool are systematically compacted at the beginning of the pool. 6333 * Hence, when a multicast address is removed from the pool, all following 6334 * addresses, if any, are copied back to keep the set contiguous. 6335 */ 6336 #define MCAST_POOL_INC 32 6337 6338 static int 6339 mcast_addr_pool_extend(struct rte_port *port) 6340 { 6341 struct rte_ether_addr *mc_pool; 6342 size_t mc_pool_size; 6343 6344 /* 6345 * If a free entry is available at the end of the pool, just 6346 * increment the number of recorded multicast addresses. 6347 */ 6348 if ((port->mc_addr_nb % MCAST_POOL_INC) != 0) { 6349 port->mc_addr_nb++; 6350 return 0; 6351 } 6352 6353 /* 6354 * [re]allocate a pool with MCAST_POOL_INC more entries. 6355 * The previous test guarantees that port->mc_addr_nb is a multiple 6356 * of MCAST_POOL_INC. 6357 */ 6358 mc_pool_size = sizeof(struct rte_ether_addr) * (port->mc_addr_nb + 6359 MCAST_POOL_INC); 6360 mc_pool = (struct rte_ether_addr *) realloc(port->mc_addr_pool, 6361 mc_pool_size); 6362 if (mc_pool == NULL) { 6363 fprintf(stderr, 6364 "allocation of pool of %u multicast addresses failed\n", 6365 port->mc_addr_nb + MCAST_POOL_INC); 6366 return -ENOMEM; 6367 } 6368 6369 port->mc_addr_pool = mc_pool; 6370 port->mc_addr_nb++; 6371 return 0; 6372 6373 } 6374 6375 static void 6376 mcast_addr_pool_append(struct rte_port *port, struct rte_ether_addr *mc_addr) 6377 { 6378 if (mcast_addr_pool_extend(port) != 0) 6379 return; 6380 rte_ether_addr_copy(mc_addr, &port->mc_addr_pool[port->mc_addr_nb - 1]); 6381 } 6382 6383 static void 6384 mcast_addr_pool_remove(struct rte_port *port, uint32_t addr_idx) 6385 { 6386 port->mc_addr_nb--; 6387 if (addr_idx == port->mc_addr_nb) { 6388 /* No need to recompact the set of multicast addresses. */ 6389 if (port->mc_addr_nb == 0) { 6390 /* free the pool of multicast addresses. */ 6391 free(port->mc_addr_pool); 6392 port->mc_addr_pool = NULL; 6393 } 6394 return; 6395 } 6396 memmove(&port->mc_addr_pool[addr_idx], 6397 &port->mc_addr_pool[addr_idx + 1], 6398 sizeof(struct rte_ether_addr) * (port->mc_addr_nb - addr_idx)); 6399 } 6400 6401 int 6402 mcast_addr_pool_destroy(portid_t port_id) 6403 { 6404 struct rte_port *port; 6405 6406 if (port_id_is_invalid(port_id, ENABLED_WARN) || 6407 port_id == (portid_t)RTE_PORT_ALL) 6408 return -EINVAL; 6409 port = &ports[port_id]; 6410 6411 if (port->mc_addr_nb != 0) { 6412 /* free the pool of multicast addresses. */ 6413 free(port->mc_addr_pool); 6414 port->mc_addr_pool = NULL; 6415 port->mc_addr_nb = 0; 6416 } 6417 return 0; 6418 } 6419 6420 static int 6421 eth_port_multicast_addr_list_set(portid_t port_id) 6422 { 6423 struct rte_port *port; 6424 int diag; 6425 6426 port = &ports[port_id]; 6427 diag = rte_eth_dev_set_mc_addr_list(port_id, port->mc_addr_pool, 6428 port->mc_addr_nb); 6429 if (diag < 0) 6430 fprintf(stderr, 6431 "rte_eth_dev_set_mc_addr_list(port=%d, nb=%u) failed. diag=%d\n", 6432 port_id, port->mc_addr_nb, diag); 6433 6434 return diag; 6435 } 6436 6437 void 6438 mcast_addr_add(portid_t port_id, struct rte_ether_addr *mc_addr) 6439 { 6440 struct rte_port *port; 6441 uint32_t i; 6442 6443 if (port_id_is_invalid(port_id, ENABLED_WARN)) 6444 return; 6445 6446 port = &ports[port_id]; 6447 6448 /* 6449 * Check that the added multicast MAC address is not already recorded 6450 * in the pool of multicast addresses. 6451 */ 6452 for (i = 0; i < port->mc_addr_nb; i++) { 6453 if (rte_is_same_ether_addr(mc_addr, &port->mc_addr_pool[i])) { 6454 fprintf(stderr, 6455 "multicast address already filtered by port\n"); 6456 return; 6457 } 6458 } 6459 6460 mcast_addr_pool_append(port, mc_addr); 6461 if (eth_port_multicast_addr_list_set(port_id) < 0) 6462 /* Rollback on failure, remove the address from the pool */ 6463 mcast_addr_pool_remove(port, i); 6464 } 6465 6466 void 6467 mcast_addr_remove(portid_t port_id, struct rte_ether_addr *mc_addr) 6468 { 6469 struct rte_port *port; 6470 uint32_t i; 6471 6472 if (port_id_is_invalid(port_id, ENABLED_WARN)) 6473 return; 6474 6475 port = &ports[port_id]; 6476 6477 /* 6478 * Search the pool of multicast MAC addresses for the removed address. 6479 */ 6480 for (i = 0; i < port->mc_addr_nb; i++) { 6481 if (rte_is_same_ether_addr(mc_addr, &port->mc_addr_pool[i])) 6482 break; 6483 } 6484 if (i == port->mc_addr_nb) { 6485 fprintf(stderr, "multicast address not filtered by port %d\n", 6486 port_id); 6487 return; 6488 } 6489 6490 mcast_addr_pool_remove(port, i); 6491 if (eth_port_multicast_addr_list_set(port_id) < 0) 6492 /* Rollback on failure, add the address back into the pool */ 6493 mcast_addr_pool_append(port, mc_addr); 6494 } 6495 6496 void 6497 port_dcb_info_display(portid_t port_id) 6498 { 6499 struct rte_eth_dcb_info dcb_info; 6500 uint16_t i; 6501 int ret; 6502 static const char *border = "================"; 6503 6504 if (port_id_is_invalid(port_id, ENABLED_WARN)) 6505 return; 6506 6507 ret = rte_eth_dev_get_dcb_info(port_id, &dcb_info); 6508 if (ret) { 6509 fprintf(stderr, "\n Failed to get dcb infos on port %-2d\n", 6510 port_id); 6511 return; 6512 } 6513 printf("\n %s DCB infos for port %-2d %s\n", border, port_id, border); 6514 printf(" TC NUMBER: %d\n", dcb_info.nb_tcs); 6515 printf("\n TC : "); 6516 for (i = 0; i < dcb_info.nb_tcs; i++) 6517 printf("\t%4d", i); 6518 printf("\n Priority : "); 6519 for (i = 0; i < dcb_info.nb_tcs; i++) 6520 printf("\t%4d", dcb_info.prio_tc[i]); 6521 printf("\n BW percent :"); 6522 for (i = 0; i < dcb_info.nb_tcs; i++) 6523 printf("\t%4d%%", dcb_info.tc_bws[i]); 6524 printf("\n RXQ base : "); 6525 for (i = 0; i < dcb_info.nb_tcs; i++) 6526 printf("\t%4d", dcb_info.tc_queue.tc_rxq[0][i].base); 6527 printf("\n RXQ number :"); 6528 for (i = 0; i < dcb_info.nb_tcs; i++) 6529 printf("\t%4d", dcb_info.tc_queue.tc_rxq[0][i].nb_queue); 6530 printf("\n TXQ base : "); 6531 for (i = 0; i < dcb_info.nb_tcs; i++) 6532 printf("\t%4d", dcb_info.tc_queue.tc_txq[0][i].base); 6533 printf("\n TXQ number :"); 6534 for (i = 0; i < dcb_info.nb_tcs; i++) 6535 printf("\t%4d", dcb_info.tc_queue.tc_txq[0][i].nb_queue); 6536 printf("\n"); 6537 } 6538 6539 uint8_t * 6540 open_file(const char *file_path, uint32_t *size) 6541 { 6542 int fd = open(file_path, O_RDONLY); 6543 off_t pkg_size; 6544 uint8_t *buf = NULL; 6545 int ret = 0; 6546 struct stat st_buf; 6547 6548 if (size) 6549 *size = 0; 6550 6551 if (fd == -1) { 6552 fprintf(stderr, "%s: Failed to open %s\n", __func__, file_path); 6553 return buf; 6554 } 6555 6556 if ((fstat(fd, &st_buf) != 0) || (!S_ISREG(st_buf.st_mode))) { 6557 close(fd); 6558 fprintf(stderr, "%s: File operations failed\n", __func__); 6559 return buf; 6560 } 6561 6562 pkg_size = st_buf.st_size; 6563 if (pkg_size < 0) { 6564 close(fd); 6565 fprintf(stderr, "%s: File operations failed\n", __func__); 6566 return buf; 6567 } 6568 6569 buf = (uint8_t *)malloc(pkg_size); 6570 if (!buf) { 6571 close(fd); 6572 fprintf(stderr, "%s: Failed to malloc memory\n", __func__); 6573 return buf; 6574 } 6575 6576 ret = read(fd, buf, pkg_size); 6577 if (ret < 0) { 6578 close(fd); 6579 fprintf(stderr, "%s: File read operation failed\n", __func__); 6580 close_file(buf); 6581 return NULL; 6582 } 6583 6584 if (size) 6585 *size = pkg_size; 6586 6587 close(fd); 6588 6589 return buf; 6590 } 6591 6592 int 6593 save_file(const char *file_path, uint8_t *buf, uint32_t size) 6594 { 6595 FILE *fh = fopen(file_path, "wb"); 6596 6597 if (fh == NULL) { 6598 fprintf(stderr, "%s: Failed to open %s\n", __func__, file_path); 6599 return -1; 6600 } 6601 6602 if (fwrite(buf, 1, size, fh) != size) { 6603 fclose(fh); 6604 fprintf(stderr, "%s: File write operation failed\n", __func__); 6605 return -1; 6606 } 6607 6608 fclose(fh); 6609 6610 return 0; 6611 } 6612 6613 int 6614 close_file(uint8_t *buf) 6615 { 6616 if (buf) { 6617 free((void *)buf); 6618 return 0; 6619 } 6620 6621 return -1; 6622 } 6623 6624 void 6625 show_macs(portid_t port_id) 6626 { 6627 char buf[RTE_ETHER_ADDR_FMT_SIZE]; 6628 struct rte_eth_dev_info dev_info; 6629 int32_t i, rc, num_macs = 0; 6630 6631 if (eth_dev_info_get_print_err(port_id, &dev_info)) 6632 return; 6633 6634 struct rte_ether_addr addr[dev_info.max_mac_addrs]; 6635 rc = rte_eth_macaddrs_get(port_id, addr, dev_info.max_mac_addrs); 6636 if (rc < 0) 6637 return; 6638 6639 for (i = 0; i < rc; i++) { 6640 6641 /* skip zero address */ 6642 if (rte_is_zero_ether_addr(&addr[i])) 6643 continue; 6644 6645 num_macs++; 6646 } 6647 6648 printf("Number of MAC address added: %d\n", num_macs); 6649 6650 for (i = 0; i < rc; i++) { 6651 6652 /* skip zero address */ 6653 if (rte_is_zero_ether_addr(&addr[i])) 6654 continue; 6655 6656 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, &addr[i]); 6657 printf(" %s\n", buf); 6658 } 6659 } 6660 6661 void 6662 show_mcast_macs(portid_t port_id) 6663 { 6664 char buf[RTE_ETHER_ADDR_FMT_SIZE]; 6665 struct rte_ether_addr *addr; 6666 struct rte_port *port; 6667 uint32_t i; 6668 6669 port = &ports[port_id]; 6670 6671 printf("Number of Multicast MAC address added: %d\n", port->mc_addr_nb); 6672 6673 for (i = 0; i < port->mc_addr_nb; i++) { 6674 addr = &port->mc_addr_pool[i]; 6675 6676 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, addr); 6677 printf(" %s\n", buf); 6678 } 6679 } 6680