1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2017 Intel Corporation 3 */ 4 5 #include <stdio.h> 6 #include <string.h> 7 #include <stdint.h> 8 #include <errno.h> 9 #include <stdarg.h> 10 #include <inttypes.h> 11 #include <sys/queue.h> 12 #include <stdlib.h> 13 #include <getopt.h> 14 #include <unistd.h> 15 16 #include <rte_eal.h> 17 #include <rte_common.h> 18 #include <rte_debug.h> 19 #include <rte_ethdev.h> 20 #include <rte_malloc.h> 21 #include <rte_memory.h> 22 #include <rte_memzone.h> 23 #include <rte_launch.h> 24 #include <rte_tailq.h> 25 #include <rte_per_lcore.h> 26 #include <rte_lcore.h> 27 #include <rte_log.h> 28 #include <rte_atomic.h> 29 #include <rte_branch_prediction.h> 30 #include <rte_string_fns.h> 31 #include <rte_metrics.h> 32 33 /* Maximum long option length for option parsing. */ 34 #define MAX_LONG_OPT_SZ 64 35 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1 36 37 #define MAX_STRING_LEN 256 38 39 /**< mask of enabled ports */ 40 static uint32_t enabled_port_mask; 41 /**< Enable stats. */ 42 static uint32_t enable_stats; 43 /**< Enable xstats. */ 44 static uint32_t enable_xstats; 45 /**< Enable collectd format*/ 46 static uint32_t enable_collectd_format; 47 /**< FD to send collectd format messages to STDOUT*/ 48 static int stdout_fd; 49 /**< Host id process is running on */ 50 static char host_id[MAX_LONG_OPT_SZ]; 51 /**< Enable metrics. */ 52 static uint32_t enable_metrics; 53 /**< Enable stats reset. */ 54 static uint32_t reset_stats; 55 /**< Enable xstats reset. */ 56 static uint32_t reset_xstats; 57 /**< Enable memory info. */ 58 static uint32_t mem_info; 59 /**< Enable displaying xstat name. */ 60 static uint32_t enable_xstats_name; 61 static char *xstats_name; 62 63 /**< Enable xstats by ids. */ 64 #define MAX_NB_XSTATS_IDS 1024 65 static uint32_t nb_xstats_ids; 66 static uint64_t xstats_ids[MAX_NB_XSTATS_IDS]; 67 68 /**< display usage */ 69 static void 70 proc_info_usage(const char *prgname) 71 { 72 printf("%s [EAL options] -- -p PORTMASK\n" 73 " -m to display DPDK memory zones, segments and TAILQ information\n" 74 " -p PORTMASK: hexadecimal bitmask of ports to retrieve stats for\n" 75 " --stats: to display port statistics, enabled by default\n" 76 " --xstats: to display extended port statistics, disabled by " 77 "default\n" 78 " --metrics: to display derived metrics of the ports, disabled by " 79 "default\n" 80 " --xstats-name NAME: to display single xstat id by NAME\n" 81 " --xstats-ids IDLIST: to display xstat values by id. " 82 "The argument is comma-separated list of xstat ids to print out.\n" 83 " --stats-reset: to reset port statistics\n" 84 " --xstats-reset: to reset port extended statistics\n" 85 " --collectd-format: to print statistics to STDOUT in expected by collectd format\n" 86 " --host-id STRING: host id used to identify the system process is running on\n", 87 prgname); 88 } 89 90 /* 91 * Parse the portmask provided at run time. 92 */ 93 static int 94 parse_portmask(const char *portmask) 95 { 96 char *end = NULL; 97 unsigned long pm; 98 99 errno = 0; 100 101 /* parse hexadecimal string */ 102 pm = strtoul(portmask, &end, 16); 103 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0') || 104 (errno != 0)) { 105 printf("%s ERROR parsing the port mask\n", __func__); 106 return -1; 107 } 108 109 if (pm == 0) 110 return -1; 111 112 return pm; 113 114 } 115 116 /* 117 * Parse ids value list into array 118 */ 119 static int 120 parse_xstats_ids(char *list, uint64_t *ids, int limit) { 121 int length; 122 char *token; 123 char *ctx = NULL; 124 char *endptr; 125 126 length = 0; 127 token = strtok_r(list, ",", &ctx); 128 while (token != NULL) { 129 ids[length] = strtoull(token, &endptr, 10); 130 if (*endptr != '\0') 131 return -EINVAL; 132 133 length++; 134 if (length >= limit) 135 return -E2BIG; 136 137 token = strtok_r(NULL, ",", &ctx); 138 } 139 140 return length; 141 } 142 143 static int 144 proc_info_preparse_args(int argc, char **argv) 145 { 146 char *prgname = argv[0]; 147 int i; 148 149 for (i = 0; i < argc; i++) { 150 /* Print stats or xstats to STDOUT in collectd format */ 151 if (!strncmp(argv[i], "--collectd-format", MAX_LONG_OPT_SZ)) { 152 enable_collectd_format = 1; 153 stdout_fd = dup(STDOUT_FILENO); 154 close(STDOUT_FILENO); 155 } 156 if (!strncmp(argv[i], "--host-id", MAX_LONG_OPT_SZ)) { 157 if ((i + 1) == argc) { 158 printf("Invalid host id or not specified\n"); 159 proc_info_usage(prgname); 160 return -1; 161 } 162 snprintf(host_id, sizeof(host_id), "%s", argv[i+1]); 163 } 164 } 165 166 if (!strlen(host_id)) { 167 int err = gethostname(host_id, MAX_LONG_OPT_SZ-1); 168 169 if (err) 170 strcpy(host_id, "unknown"); 171 } 172 173 return 0; 174 } 175 176 /* Parse the argument given in the command line of the application */ 177 static int 178 proc_info_parse_args(int argc, char **argv) 179 { 180 int opt; 181 int option_index; 182 char *prgname = argv[0]; 183 static struct option long_option[] = { 184 {"stats", 0, NULL, 0}, 185 {"stats-reset", 0, NULL, 0}, 186 {"xstats", 0, NULL, 0}, 187 {"metrics", 0, NULL, 0}, 188 {"xstats-reset", 0, NULL, 0}, 189 {"xstats-name", required_argument, NULL, 1}, 190 {"collectd-format", 0, NULL, 0}, 191 {"xstats-ids", 1, NULL, 1}, 192 {"host-id", 0, NULL, 0}, 193 {NULL, 0, 0, 0} 194 }; 195 196 if (argc == 1) 197 proc_info_usage(prgname); 198 199 /* Parse command line */ 200 while ((opt = getopt_long(argc, argv, "p:m", 201 long_option, &option_index)) != EOF) { 202 switch (opt) { 203 /* portmask */ 204 case 'p': 205 enabled_port_mask = parse_portmask(optarg); 206 if (enabled_port_mask == 0) { 207 printf("invalid portmask\n"); 208 proc_info_usage(prgname); 209 return -1; 210 } 211 break; 212 case 'm': 213 mem_info = 1; 214 break; 215 case 0: 216 /* Print stats */ 217 if (!strncmp(long_option[option_index].name, "stats", 218 MAX_LONG_OPT_SZ)) 219 enable_stats = 1; 220 /* Print xstats */ 221 else if (!strncmp(long_option[option_index].name, "xstats", 222 MAX_LONG_OPT_SZ)) 223 enable_xstats = 1; 224 else if (!strncmp(long_option[option_index].name, 225 "metrics", 226 MAX_LONG_OPT_SZ)) 227 enable_metrics = 1; 228 /* Reset stats */ 229 if (!strncmp(long_option[option_index].name, "stats-reset", 230 MAX_LONG_OPT_SZ)) 231 reset_stats = 1; 232 /* Reset xstats */ 233 else if (!strncmp(long_option[option_index].name, "xstats-reset", 234 MAX_LONG_OPT_SZ)) 235 reset_xstats = 1; 236 break; 237 case 1: 238 /* Print xstat single value given by name*/ 239 if (!strncmp(long_option[option_index].name, 240 "xstats-name", MAX_LONG_OPT_SZ)) { 241 enable_xstats_name = 1; 242 xstats_name = optarg; 243 printf("name:%s:%s\n", 244 long_option[option_index].name, 245 optarg); 246 } else if (!strncmp(long_option[option_index].name, 247 "xstats-ids", 248 MAX_LONG_OPT_SZ)) { 249 nb_xstats_ids = parse_xstats_ids(optarg, 250 xstats_ids, MAX_NB_XSTATS_IDS); 251 252 if (nb_xstats_ids <= 0) { 253 printf("xstats-id list parse error.\n"); 254 return -1; 255 } 256 257 } 258 break; 259 default: 260 proc_info_usage(prgname); 261 return -1; 262 } 263 } 264 return 0; 265 } 266 267 static void 268 meminfo_display(void) 269 { 270 printf("----------- MEMORY_SEGMENTS -----------\n"); 271 rte_dump_physmem_layout(stdout); 272 printf("--------- END_MEMORY_SEGMENTS ---------\n"); 273 274 printf("------------ MEMORY_ZONES -------------\n"); 275 rte_memzone_dump(stdout); 276 printf("---------- END_MEMORY_ZONES -----------\n"); 277 278 printf("------------- TAIL_QUEUES -------------\n"); 279 rte_dump_tailq(stdout); 280 printf("---------- END_TAIL_QUEUES ------------\n"); 281 } 282 283 static void 284 nic_stats_display(uint16_t port_id) 285 { 286 struct rte_eth_stats stats; 287 uint8_t i; 288 289 static const char *nic_stats_border = "########################"; 290 291 rte_eth_stats_get(port_id, &stats); 292 printf("\n %s NIC statistics for port %-2d %s\n", 293 nic_stats_border, port_id, nic_stats_border); 294 295 printf(" RX-packets: %-10"PRIu64" RX-errors: %-10"PRIu64 296 " RX-bytes: %-10"PRIu64"\n", stats.ipackets, stats.ierrors, 297 stats.ibytes); 298 printf(" RX-nombuf: %-10"PRIu64"\n", stats.rx_nombuf); 299 printf(" TX-packets: %-10"PRIu64" TX-errors: %-10"PRIu64 300 " TX-bytes: %-10"PRIu64"\n", stats.opackets, stats.oerrors, 301 stats.obytes); 302 303 printf("\n"); 304 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) { 305 printf(" Stats reg %2d RX-packets: %-10"PRIu64 306 " RX-errors: %-10"PRIu64 307 " RX-bytes: %-10"PRIu64"\n", 308 i, stats.q_ipackets[i], stats.q_errors[i], stats.q_ibytes[i]); 309 } 310 311 printf("\n"); 312 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) { 313 printf(" Stats reg %2d TX-packets: %-10"PRIu64 314 " TX-bytes: %-10"PRIu64"\n", 315 i, stats.q_opackets[i], stats.q_obytes[i]); 316 } 317 318 printf(" %s############################%s\n", 319 nic_stats_border, nic_stats_border); 320 } 321 322 static void 323 nic_stats_clear(uint16_t port_id) 324 { 325 printf("\n Clearing NIC stats for port %d\n", port_id); 326 rte_eth_stats_reset(port_id); 327 printf("\n NIC statistics for port %d cleared\n", port_id); 328 } 329 330 static void collectd_resolve_cnt_type(char *cnt_type, size_t cnt_type_len, 331 const char *cnt_name) { 332 char *type_end = strrchr(cnt_name, '_'); 333 334 if ((type_end != NULL) && 335 (strncmp(cnt_name, "rx_", strlen("rx_")) == 0)) { 336 if (strncmp(type_end, "_errors", strlen("_errors")) == 0) 337 strncpy(cnt_type, "if_rx_errors", cnt_type_len); 338 else if (strncmp(type_end, "_dropped", strlen("_dropped")) == 0) 339 strncpy(cnt_type, "if_rx_dropped", cnt_type_len); 340 else if (strncmp(type_end, "_bytes", strlen("_bytes")) == 0) 341 strncpy(cnt_type, "if_rx_octets", cnt_type_len); 342 else if (strncmp(type_end, "_packets", strlen("_packets")) == 0) 343 strncpy(cnt_type, "if_rx_packets", cnt_type_len); 344 else if (strncmp(type_end, "_placement", 345 strlen("_placement")) == 0) 346 strncpy(cnt_type, "if_rx_errors", cnt_type_len); 347 else if (strncmp(type_end, "_buff", strlen("_buff")) == 0) 348 strncpy(cnt_type, "if_rx_errors", cnt_type_len); 349 else 350 /* Does not fit obvious type: use a more generic one */ 351 strncpy(cnt_type, "derive", cnt_type_len); 352 } else if ((type_end != NULL) && 353 (strncmp(cnt_name, "tx_", strlen("tx_"))) == 0) { 354 if (strncmp(type_end, "_errors", strlen("_errors")) == 0) 355 strncpy(cnt_type, "if_tx_errors", cnt_type_len); 356 else if (strncmp(type_end, "_dropped", strlen("_dropped")) == 0) 357 strncpy(cnt_type, "if_tx_dropped", cnt_type_len); 358 else if (strncmp(type_end, "_bytes", strlen("_bytes")) == 0) 359 strncpy(cnt_type, "if_tx_octets", cnt_type_len); 360 else if (strncmp(type_end, "_packets", strlen("_packets")) == 0) 361 strncpy(cnt_type, "if_tx_packets", cnt_type_len); 362 else 363 /* Does not fit obvious type: use a more generic one */ 364 strncpy(cnt_type, "derive", cnt_type_len); 365 } else if ((type_end != NULL) && 366 (strncmp(cnt_name, "flow_", strlen("flow_"))) == 0) { 367 if (strncmp(type_end, "_filters", strlen("_filters")) == 0) 368 strncpy(cnt_type, "operations", cnt_type_len); 369 else if (strncmp(type_end, "_errors", strlen("_errors")) == 0) 370 strncpy(cnt_type, "errors", cnt_type_len); 371 else if (strncmp(type_end, "_filters", strlen("_filters")) == 0) 372 strncpy(cnt_type, "filter_result", cnt_type_len); 373 } else if ((type_end != NULL) && 374 (strncmp(cnt_name, "mac_", strlen("mac_"))) == 0) { 375 if (strncmp(type_end, "_errors", strlen("_errors")) == 0) 376 strncpy(cnt_type, "errors", cnt_type_len); 377 } else { 378 /* Does not fit obvious type, or strrchr error: */ 379 /* use a more generic type */ 380 strncpy(cnt_type, "derive", cnt_type_len); 381 } 382 } 383 384 static void 385 nic_xstats_by_name_display(uint16_t port_id, char *name) 386 { 387 uint64_t id; 388 389 printf("###### NIC statistics for port %-2d, statistic name '%s':\n", 390 port_id, name); 391 392 if (rte_eth_xstats_get_id_by_name(port_id, name, &id) == 0) 393 printf("%s: %"PRIu64"\n", name, id); 394 else 395 printf("Statistic not found...\n"); 396 397 } 398 399 static void 400 nic_xstats_by_ids_display(uint16_t port_id, uint64_t *ids, int len) 401 { 402 struct rte_eth_xstat_name *xstats_names; 403 uint64_t *values; 404 int ret, i; 405 static const char *nic_stats_border = "########################"; 406 407 values = malloc(sizeof(*values) * len); 408 if (values == NULL) { 409 printf("Cannot allocate memory for xstats\n"); 410 return; 411 } 412 413 xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * len); 414 if (xstats_names == NULL) { 415 printf("Cannot allocate memory for xstat names\n"); 416 free(values); 417 return; 418 } 419 420 if (len != rte_eth_xstats_get_names_by_id( 421 port_id, xstats_names, len, ids)) { 422 printf("Cannot get xstat names\n"); 423 goto err; 424 } 425 426 printf("###### NIC extended statistics for port %-2d #########\n", 427 port_id); 428 printf("%s############################\n", nic_stats_border); 429 ret = rte_eth_xstats_get_by_id(port_id, ids, values, len); 430 if (ret < 0 || ret > len) { 431 printf("Cannot get xstats\n"); 432 goto err; 433 } 434 435 for (i = 0; i < len; i++) 436 printf("%s: %"PRIu64"\n", 437 xstats_names[i].name, 438 values[i]); 439 440 printf("%s############################\n", nic_stats_border); 441 err: 442 free(values); 443 free(xstats_names); 444 } 445 446 static void 447 nic_xstats_display(uint16_t port_id) 448 { 449 struct rte_eth_xstat_name *xstats_names; 450 uint64_t *values; 451 int len, ret, i; 452 static const char *nic_stats_border = "########################"; 453 454 len = rte_eth_xstats_get_names_by_id(port_id, NULL, 0, NULL); 455 if (len < 0) { 456 printf("Cannot get xstats count\n"); 457 return; 458 } 459 values = malloc(sizeof(*values) * len); 460 if (values == NULL) { 461 printf("Cannot allocate memory for xstats\n"); 462 return; 463 } 464 465 xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * len); 466 if (xstats_names == NULL) { 467 printf("Cannot allocate memory for xstat names\n"); 468 free(values); 469 return; 470 } 471 if (len != rte_eth_xstats_get_names_by_id( 472 port_id, xstats_names, len, NULL)) { 473 printf("Cannot get xstat names\n"); 474 goto err; 475 } 476 477 printf("###### NIC extended statistics for port %-2d #########\n", 478 port_id); 479 printf("%s############################\n", 480 nic_stats_border); 481 ret = rte_eth_xstats_get_by_id(port_id, NULL, values, len); 482 if (ret < 0 || ret > len) { 483 printf("Cannot get xstats\n"); 484 goto err; 485 } 486 487 for (i = 0; i < len; i++) { 488 if (enable_collectd_format) { 489 char counter_type[MAX_STRING_LEN]; 490 char buf[MAX_STRING_LEN]; 491 492 collectd_resolve_cnt_type(counter_type, 493 sizeof(counter_type), 494 xstats_names[i].name); 495 sprintf(buf, "PUTVAL %s/dpdkstat-port.%u/%s-%s N:%" 496 PRIu64"\n", host_id, port_id, counter_type, 497 xstats_names[i].name, values[i]); 498 ret = write(stdout_fd, buf, strlen(buf)); 499 if (ret < 0) 500 goto err; 501 } else { 502 printf("%s: %"PRIu64"\n", xstats_names[i].name, 503 values[i]); 504 } 505 } 506 507 printf("%s############################\n", 508 nic_stats_border); 509 err: 510 free(values); 511 free(xstats_names); 512 } 513 514 static void 515 nic_xstats_clear(uint16_t port_id) 516 { 517 printf("\n Clearing NIC xstats for port %d\n", port_id); 518 rte_eth_xstats_reset(port_id); 519 printf("\n NIC extended statistics for port %d cleared\n", port_id); 520 } 521 522 static void 523 metrics_display(int port_id) 524 { 525 struct rte_metric_value *metrics; 526 struct rte_metric_name *names; 527 int len, ret; 528 static const char *nic_stats_border = "########################"; 529 530 len = rte_metrics_get_names(NULL, 0); 531 if (len < 0) { 532 printf("Cannot get metrics count\n"); 533 return; 534 } 535 if (len == 0) { 536 printf("No metrics to display (none have been registered)\n"); 537 return; 538 } 539 540 metrics = rte_malloc("proc_info_metrics", 541 sizeof(struct rte_metric_value) * len, 0); 542 if (metrics == NULL) { 543 printf("Cannot allocate memory for metrics\n"); 544 return; 545 } 546 547 names = rte_malloc(NULL, sizeof(struct rte_metric_name) * len, 0); 548 if (names == NULL) { 549 printf("Cannot allocate memory for metrcis names\n"); 550 rte_free(metrics); 551 return; 552 } 553 554 if (len != rte_metrics_get_names(names, len)) { 555 printf("Cannot get metrics names\n"); 556 rte_free(metrics); 557 rte_free(names); 558 return; 559 } 560 561 if (port_id == RTE_METRICS_GLOBAL) 562 printf("###### Non port specific metrics #########\n"); 563 else 564 printf("###### metrics for port %-2d #########\n", port_id); 565 printf("%s############################\n", nic_stats_border); 566 ret = rte_metrics_get_values(port_id, metrics, len); 567 if (ret < 0 || ret > len) { 568 printf("Cannot get metrics values\n"); 569 rte_free(metrics); 570 rte_free(names); 571 return; 572 } 573 574 int i; 575 for (i = 0; i < len; i++) 576 printf("%s: %"PRIu64"\n", names[i].name, metrics[i].value); 577 578 printf("%s############################\n", nic_stats_border); 579 rte_free(metrics); 580 rte_free(names); 581 } 582 583 int 584 main(int argc, char **argv) 585 { 586 int ret; 587 int i; 588 char c_flag[] = "-c1"; 589 char n_flag[] = "-n4"; 590 char mp_flag[] = "--proc-type=secondary"; 591 char *argp[argc + 3]; 592 uint16_t nb_ports; 593 594 /* preparse app arguments */ 595 ret = proc_info_preparse_args(argc, argv); 596 if (ret < 0) { 597 printf("Failed to parse arguments\n"); 598 return -1; 599 } 600 601 argp[0] = argv[0]; 602 argp[1] = c_flag; 603 argp[2] = n_flag; 604 argp[3] = mp_flag; 605 606 for (i = 1; i < argc; i++) 607 argp[i + 3] = argv[i]; 608 609 argc += 3; 610 611 ret = rte_eal_init(argc, argp); 612 if (ret < 0) 613 rte_panic("Cannot init EAL\n"); 614 615 argc -= ret; 616 argv += (ret - 3); 617 618 if (!rte_eal_primary_proc_alive(NULL)) 619 rte_exit(EXIT_FAILURE, "No primary DPDK process is running.\n"); 620 621 /* parse app arguments */ 622 ret = proc_info_parse_args(argc, argv); 623 if (ret < 0) 624 rte_exit(EXIT_FAILURE, "Invalid argument\n"); 625 626 if (mem_info) { 627 meminfo_display(); 628 return 0; 629 } 630 631 nb_ports = rte_eth_dev_count(); 632 if (nb_ports == 0) 633 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n"); 634 635 /* If no port mask was specified*/ 636 if (enabled_port_mask == 0) 637 enabled_port_mask = 0xffff; 638 639 for (i = 0; i < nb_ports; i++) { 640 if (enabled_port_mask & (1 << i)) { 641 if (enable_stats) 642 nic_stats_display(i); 643 else if (enable_xstats) 644 nic_xstats_display(i); 645 else if (reset_stats) 646 nic_stats_clear(i); 647 else if (reset_xstats) 648 nic_xstats_clear(i); 649 else if (enable_xstats_name) 650 nic_xstats_by_name_display(i, xstats_name); 651 else if (nb_xstats_ids > 0) 652 nic_xstats_by_ids_display(i, xstats_ids, 653 nb_xstats_ids); 654 else if (enable_metrics) 655 metrics_display(i); 656 } 657 } 658 659 /* print port independent stats */ 660 if (enable_metrics) 661 metrics_display(RTE_METRICS_GLOBAL); 662 663 ret = rte_eal_cleanup(); 664 if (ret) 665 printf("Error from rte_eal_cleanup(), %d\n", ret); 666 667 return 0; 668 } 669