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