1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2017 Intel Corporation
11 #include <sys/queue.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>
28 #include <rte_atomic.h>
29 #include <rte_branch_prediction.h>
30 #include <rte_string_fns.h>
31 #include <rte_metrics.h>
32 #include <rte_cycles.h>
33 #include <rte_security.h>
34 #include <rte_cryptodev.h>
36 /* Maximum long option length for option parsing. */
37 #define MAX_LONG_OPT_SZ 64
38 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
40 #define MAX_STRING_LEN 256
42 #define STATS_BDR_FMT "========================================"
43 #define STATS_BDR_STR(w, s) printf("%.*s%s%.*s\n", w, \
44 STATS_BDR_FMT, s, w, STATS_BDR_FMT)
46 /**< mask of enabled ports */
47 static uint32_t enabled_port_mask;
49 static uint32_t enable_stats;
50 /**< Enable xstats. */
51 static uint32_t enable_xstats;
52 /**< Enable collectd format*/
53 static uint32_t enable_collectd_format;
54 /**< FD to send collectd format messages to STDOUT*/
56 /**< Host id process is running on */
57 static char host_id[MAX_LONG_OPT_SZ];
58 /**< Enable metrics. */
59 static uint32_t enable_metrics;
60 /**< Enable stats reset. */
61 static uint32_t reset_stats;
62 /**< Enable xstats reset. */
63 static uint32_t reset_xstats;
64 /**< Enable memory info. */
65 static uint32_t mem_info;
66 /**< Enable displaying xstat name. */
67 static uint32_t enable_xstats_name;
68 static char *xstats_name;
70 /**< Enable xstats by ids. */
71 #define MAX_NB_XSTATS_IDS 1024
72 static uint32_t nb_xstats_ids;
73 static uint64_t xstats_ids[MAX_NB_XSTATS_IDS];
76 static char bdr_str[MAX_STRING_LEN];
78 /**< Enable show port. */
79 static uint32_t enable_shw_port;
83 proc_info_usage(const char *prgname)
85 printf("%s [EAL options] -- -p PORTMASK\n"
86 " -m to display DPDK memory zones, segments and TAILQ information\n"
87 " -p PORTMASK: hexadecimal bitmask of ports to retrieve stats for\n"
88 " --stats: to display port statistics, enabled by default\n"
89 " --xstats: to display extended port statistics, disabled by "
91 " --metrics: to display derived metrics of the ports, disabled by "
93 " --xstats-name NAME: to display single xstat id by NAME\n"
94 " --xstats-ids IDLIST: to display xstat values by id. "
95 "The argument is comma-separated list of xstat ids to print out.\n"
96 " --stats-reset: to reset port statistics\n"
97 " --xstats-reset: to reset port extended statistics\n"
98 " --collectd-format: to print statistics to STDOUT in expected by collectd format\n"
99 " --host-id STRING: host id used to identify the system process is running on\n"
100 " --show-port: to display ports information\n",
105 * Parse the portmask provided at run time.
108 parse_portmask(const char *portmask)
115 /* parse hexadecimal string */
116 pm = strtoul(portmask, &end, 16);
117 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0') ||
119 printf("%s ERROR parsing the port mask\n", __func__);
131 * Parse ids value list into array
134 parse_xstats_ids(char *list, uint64_t *ids, int limit) {
141 token = strtok_r(list, ",", &ctx);
142 while (token != NULL) {
143 ids[length] = strtoull(token, &endptr, 10);
151 token = strtok_r(NULL, ",", &ctx);
158 proc_info_preparse_args(int argc, char **argv)
160 char *prgname = argv[0];
163 for (i = 0; i < argc; i++) {
164 /* Print stats or xstats to STDOUT in collectd format */
165 if (!strncmp(argv[i], "--collectd-format", MAX_LONG_OPT_SZ)) {
166 enable_collectd_format = 1;
167 stdout_fd = dup(STDOUT_FILENO);
168 close(STDOUT_FILENO);
170 if (!strncmp(argv[i], "--host-id", MAX_LONG_OPT_SZ)) {
171 if ((i + 1) == argc) {
172 printf("Invalid host id or not specified\n");
173 proc_info_usage(prgname);
176 snprintf(host_id, sizeof(host_id), "%s", argv[i+1]);
180 if (!strlen(host_id)) {
181 int err = gethostname(host_id, MAX_LONG_OPT_SZ-1);
184 strcpy(host_id, "unknown");
190 /* Parse the argument given in the command line of the application */
192 proc_info_parse_args(int argc, char **argv)
196 char *prgname = argv[0];
197 static struct option long_option[] = {
198 {"stats", 0, NULL, 0},
199 {"stats-reset", 0, NULL, 0},
200 {"xstats", 0, NULL, 0},
201 {"metrics", 0, NULL, 0},
202 {"xstats-reset", 0, NULL, 0},
203 {"xstats-name", required_argument, NULL, 1},
204 {"collectd-format", 0, NULL, 0},
205 {"xstats-ids", 1, NULL, 1},
206 {"host-id", 0, NULL, 0},
207 {"show-port", 0, NULL, 0},
212 proc_info_usage(prgname);
214 /* Parse command line */
215 while ((opt = getopt_long(argc, argv, "p:m",
216 long_option, &option_index)) != EOF) {
220 enabled_port_mask = parse_portmask(optarg);
221 if (enabled_port_mask == 0) {
222 printf("invalid portmask\n");
223 proc_info_usage(prgname);
232 if (!strncmp(long_option[option_index].name, "stats",
236 else if (!strncmp(long_option[option_index].name, "xstats",
239 else if (!strncmp(long_option[option_index].name,
244 if (!strncmp(long_option[option_index].name, "stats-reset",
248 else if (!strncmp(long_option[option_index].name, "xstats-reset",
251 else if (!strncmp(long_option[option_index].name,
252 "show-port", MAX_LONG_OPT_SZ))
256 /* Print xstat single value given by name*/
257 if (!strncmp(long_option[option_index].name,
258 "xstats-name", MAX_LONG_OPT_SZ)) {
259 enable_xstats_name = 1;
260 xstats_name = optarg;
261 printf("name:%s:%s\n",
262 long_option[option_index].name,
264 } else if (!strncmp(long_option[option_index].name,
267 nb_xstats_ids = parse_xstats_ids(optarg,
268 xstats_ids, MAX_NB_XSTATS_IDS);
270 if (nb_xstats_ids <= 0) {
271 printf("xstats-id list parse error.\n");
278 proc_info_usage(prgname);
286 meminfo_display(void)
288 printf("----------- MEMORY_SEGMENTS -----------\n");
289 rte_dump_physmem_layout(stdout);
290 printf("--------- END_MEMORY_SEGMENTS ---------\n");
292 printf("------------ MEMORY_ZONES -------------\n");
293 rte_memzone_dump(stdout);
294 printf("---------- END_MEMORY_ZONES -----------\n");
296 printf("------------- TAIL_QUEUES -------------\n");
297 rte_dump_tailq(stdout);
298 printf("---------- END_TAIL_QUEUES ------------\n");
302 nic_stats_display(uint16_t port_id)
304 struct rte_eth_stats stats;
307 static const char *nic_stats_border = "########################";
309 rte_eth_stats_get(port_id, &stats);
310 printf("\n %s NIC statistics for port %-2d %s\n",
311 nic_stats_border, port_id, nic_stats_border);
313 printf(" RX-packets: %-10"PRIu64" RX-errors: %-10"PRIu64
314 " RX-bytes: %-10"PRIu64"\n", stats.ipackets, stats.ierrors,
316 printf(" RX-nombuf: %-10"PRIu64"\n", stats.rx_nombuf);
317 printf(" TX-packets: %-10"PRIu64" TX-errors: %-10"PRIu64
318 " TX-bytes: %-10"PRIu64"\n", stats.opackets, stats.oerrors,
322 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
323 printf(" Stats reg %2d RX-packets: %-10"PRIu64
324 " RX-errors: %-10"PRIu64
325 " RX-bytes: %-10"PRIu64"\n",
326 i, stats.q_ipackets[i], stats.q_errors[i], stats.q_ibytes[i]);
330 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
331 printf(" Stats reg %2d TX-packets: %-10"PRIu64
332 " TX-bytes: %-10"PRIu64"\n",
333 i, stats.q_opackets[i], stats.q_obytes[i]);
336 printf(" %s############################%s\n",
337 nic_stats_border, nic_stats_border);
341 nic_stats_clear(uint16_t port_id)
343 printf("\n Clearing NIC stats for port %d\n", port_id);
344 rte_eth_stats_reset(port_id);
345 printf("\n NIC statistics for port %d cleared\n", port_id);
348 static void collectd_resolve_cnt_type(char *cnt_type, size_t cnt_type_len,
349 const char *cnt_name) {
350 char *type_end = strrchr(cnt_name, '_');
352 if ((type_end != NULL) &&
353 (strncmp(cnt_name, "rx_", strlen("rx_")) == 0)) {
354 if (strncmp(type_end, "_errors", strlen("_errors")) == 0)
355 strncpy(cnt_type, "if_rx_errors", cnt_type_len);
356 else if (strncmp(type_end, "_dropped", strlen("_dropped")) == 0)
357 strncpy(cnt_type, "if_rx_dropped", cnt_type_len);
358 else if (strncmp(type_end, "_bytes", strlen("_bytes")) == 0)
359 strncpy(cnt_type, "if_rx_octets", cnt_type_len);
360 else if (strncmp(type_end, "_packets", strlen("_packets")) == 0)
361 strncpy(cnt_type, "if_rx_packets", cnt_type_len);
362 else if (strncmp(type_end, "_placement",
363 strlen("_placement")) == 0)
364 strncpy(cnt_type, "if_rx_errors", cnt_type_len);
365 else if (strncmp(type_end, "_buff", strlen("_buff")) == 0)
366 strncpy(cnt_type, "if_rx_errors", cnt_type_len);
368 /* Does not fit obvious type: use a more generic one */
369 strncpy(cnt_type, "derive", cnt_type_len);
370 } else if ((type_end != NULL) &&
371 (strncmp(cnt_name, "tx_", strlen("tx_"))) == 0) {
372 if (strncmp(type_end, "_errors", strlen("_errors")) == 0)
373 strncpy(cnt_type, "if_tx_errors", cnt_type_len);
374 else if (strncmp(type_end, "_dropped", strlen("_dropped")) == 0)
375 strncpy(cnt_type, "if_tx_dropped", cnt_type_len);
376 else if (strncmp(type_end, "_bytes", strlen("_bytes")) == 0)
377 strncpy(cnt_type, "if_tx_octets", cnt_type_len);
378 else if (strncmp(type_end, "_packets", strlen("_packets")) == 0)
379 strncpy(cnt_type, "if_tx_packets", cnt_type_len);
381 /* Does not fit obvious type: use a more generic one */
382 strncpy(cnt_type, "derive", cnt_type_len);
383 } else if ((type_end != NULL) &&
384 (strncmp(cnt_name, "flow_", strlen("flow_"))) == 0) {
385 if (strncmp(type_end, "_filters", strlen("_filters")) == 0)
386 strncpy(cnt_type, "operations", cnt_type_len);
387 else if (strncmp(type_end, "_errors", strlen("_errors")) == 0)
388 strncpy(cnt_type, "errors", cnt_type_len);
389 else if (strncmp(type_end, "_filters", strlen("_filters")) == 0)
390 strncpy(cnt_type, "filter_result", cnt_type_len);
391 } else if ((type_end != NULL) &&
392 (strncmp(cnt_name, "mac_", strlen("mac_"))) == 0) {
393 if (strncmp(type_end, "_errors", strlen("_errors")) == 0)
394 strncpy(cnt_type, "errors", cnt_type_len);
396 /* Does not fit obvious type, or strrchr error: */
397 /* use a more generic type */
398 strncpy(cnt_type, "derive", cnt_type_len);
403 nic_xstats_by_name_display(uint16_t port_id, char *name)
407 printf("###### NIC statistics for port %-2d, statistic name '%s':\n",
410 if (rte_eth_xstats_get_id_by_name(port_id, name, &id) == 0)
411 printf("%s: %"PRIu64"\n", name, id);
413 printf("Statistic not found...\n");
418 nic_xstats_by_ids_display(uint16_t port_id, uint64_t *ids, int len)
420 struct rte_eth_xstat_name *xstats_names;
423 static const char *nic_stats_border = "########################";
425 values = malloc(sizeof(*values) * len);
426 if (values == NULL) {
427 printf("Cannot allocate memory for xstats\n");
431 xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * len);
432 if (xstats_names == NULL) {
433 printf("Cannot allocate memory for xstat names\n");
438 if (len != rte_eth_xstats_get_names_by_id(
439 port_id, xstats_names, len, ids)) {
440 printf("Cannot get xstat names\n");
444 printf("###### NIC extended statistics for port %-2d #########\n",
446 printf("%s############################\n", nic_stats_border);
447 ret = rte_eth_xstats_get_by_id(port_id, ids, values, len);
448 if (ret < 0 || ret > len) {
449 printf("Cannot get xstats\n");
453 for (i = 0; i < len; i++)
454 printf("%s: %"PRIu64"\n",
455 xstats_names[i].name,
458 printf("%s############################\n", nic_stats_border);
465 nic_xstats_display(uint16_t port_id)
467 struct rte_eth_xstat_name *xstats_names;
470 static const char *nic_stats_border = "########################";
472 len = rte_eth_xstats_get_names_by_id(port_id, NULL, 0, NULL);
474 printf("Cannot get xstats count\n");
477 values = malloc(sizeof(*values) * len);
478 if (values == NULL) {
479 printf("Cannot allocate memory for xstats\n");
483 xstats_names = malloc(sizeof(struct rte_eth_xstat_name) * len);
484 if (xstats_names == NULL) {
485 printf("Cannot allocate memory for xstat names\n");
489 if (len != rte_eth_xstats_get_names_by_id(
490 port_id, xstats_names, len, NULL)) {
491 printf("Cannot get xstat names\n");
495 printf("###### NIC extended statistics for port %-2d #########\n",
497 printf("%s############################\n",
499 ret = rte_eth_xstats_get_by_id(port_id, NULL, values, len);
500 if (ret < 0 || ret > len) {
501 printf("Cannot get xstats\n");
505 for (i = 0; i < len; i++) {
506 if (enable_collectd_format) {
507 char counter_type[MAX_STRING_LEN];
508 char buf[MAX_STRING_LEN];
511 collectd_resolve_cnt_type(counter_type,
512 sizeof(counter_type),
513 xstats_names[i].name);
514 n = snprintf(buf, MAX_STRING_LEN,
515 "PUTVAL %s/dpdkstat-port.%u/%s-%s N:%"
516 PRIu64"\n", host_id, port_id, counter_type,
517 xstats_names[i].name, values[i]);
518 if (n > sizeof(buf) - 1)
520 ret = write(stdout_fd, buf, n);
524 printf("%s: %"PRIu64"\n", xstats_names[i].name,
529 printf("%s############################\n",
537 nic_xstats_clear(uint16_t port_id)
539 printf("\n Clearing NIC xstats for port %d\n", port_id);
540 rte_eth_xstats_reset(port_id);
541 printf("\n NIC extended statistics for port %d cleared\n", port_id);
545 metrics_display(int port_id)
547 struct rte_metric_value *metrics;
548 struct rte_metric_name *names;
550 static const char *nic_stats_border = "########################";
552 len = rte_metrics_get_names(NULL, 0);
554 printf("Cannot get metrics count\n");
558 printf("No metrics to display (none have been registered)\n");
562 metrics = rte_malloc("proc_info_metrics",
563 sizeof(struct rte_metric_value) * len, 0);
564 if (metrics == NULL) {
565 printf("Cannot allocate memory for metrics\n");
569 names = rte_malloc(NULL, sizeof(struct rte_metric_name) * len, 0);
571 printf("Cannot allocate memory for metrcis names\n");
576 if (len != rte_metrics_get_names(names, len)) {
577 printf("Cannot get metrics names\n");
583 if (port_id == RTE_METRICS_GLOBAL)
584 printf("###### Non port specific metrics #########\n");
586 printf("###### metrics for port %-2d #########\n", port_id);
587 printf("%s############################\n", nic_stats_border);
588 ret = rte_metrics_get_values(port_id, metrics, len);
589 if (ret < 0 || ret > len) {
590 printf("Cannot get metrics values\n");
597 for (i = 0; i < len; i++)
598 printf("%s: %"PRIu64"\n", names[i].name, metrics[i].value);
600 printf("%s############################\n", nic_stats_border);
611 snprintf(bdr_str, MAX_STRING_LEN, " show - Port PMD %"PRIu64,
613 STATS_BDR_STR(10, bdr_str);
615 RTE_ETH_FOREACH_DEV(i) {
617 struct rte_eth_link link;
618 struct rte_eth_dev_info dev_info;
619 struct rte_eth_rxq_info queue_info;
620 struct rte_eth_rss_conf rss_conf;
622 memset(&rss_conf, 0, sizeof(rss_conf));
624 snprintf(bdr_str, MAX_STRING_LEN, " Port (%u)", i);
625 STATS_BDR_STR(5, bdr_str);
626 printf(" - generic config\n");
628 printf("\t -- Socket %d\n", rte_eth_dev_socket_id(i));
629 rte_eth_link_get(i, &link);
630 printf("\t -- link speed %d duplex %d,"
631 " auto neg %d status %d\n",
636 printf("\t -- promiscuous (%d)\n",
637 rte_eth_promiscuous_get(i));
638 ret = rte_eth_dev_get_mtu(i, &mtu);
640 printf("\t -- mtu (%d)\n", mtu);
642 rte_eth_dev_info_get(i, &dev_info);
644 printf(" - queue\n");
645 for (j = 0; j < dev_info.nb_rx_queues; j++) {
646 ret = rte_eth_rx_queue_info_get(i, j, &queue_info);
648 printf("\t -- queue %d rx scatter %d"
650 " offloads 0x%"PRIx64
651 " mempool socket %d\n",
653 queue_info.scattered_rx,
655 queue_info.conf.offloads,
656 queue_info.mp->socket_id);
660 ret = rte_eth_dev_rss_hash_conf_get(i, &rss_conf);
662 if (rss_conf.rss_key) {
664 printf("\t -- RSS len %u key (hex):",
665 rss_conf.rss_key_len);
666 for (k = 0; k < rss_conf.rss_key_len; k++)
667 printf(" %x", rss_conf.rss_key[k]);
668 printf("\t -- hf 0x%"PRIx64"\n",
673 printf(" - cyrpto context\n");
674 void *p_ctx = rte_eth_dev_get_sec_ctx(i);
675 printf("\t -- security context - %p\n", p_ctx);
678 printf("\t -- size %u\n",
679 rte_security_session_get_size(p_ctx));
680 const struct rte_security_capability *s_cap =
681 rte_security_capabilities_get(p_ctx);
683 printf("\t -- action (0x%x), protocol (0x%x),"
684 " offload flags (0x%x)\n",
688 printf("\t -- capabilities - oper type %x\n",
689 s_cap->crypto_capabilities->op);
694 STATS_BDR_STR(50, "");
698 main(int argc, char **argv)
702 char c_flag[] = "-c1";
703 char n_flag[] = "-n4";
704 char mp_flag[] = "--proc-type=secondary";
705 char *argp[argc + 3];
708 /* preparse app arguments */
709 ret = proc_info_preparse_args(argc, argv);
711 printf("Failed to parse arguments\n");
720 for (i = 1; i < argc; i++)
721 argp[i + 3] = argv[i];
725 ret = rte_eal_init(argc, argp);
727 rte_panic("Cannot init EAL\n");
732 if (!rte_eal_primary_proc_alive(NULL))
733 rte_exit(EXIT_FAILURE, "No primary DPDK process is running.\n");
735 /* parse app arguments */
736 ret = proc_info_parse_args(argc, argv);
738 rte_exit(EXIT_FAILURE, "Invalid argument\n");
745 nb_ports = rte_eth_dev_count_avail();
747 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
749 /* If no port mask was specified*/
750 if (enabled_port_mask == 0)
751 enabled_port_mask = 0xffff;
753 RTE_ETH_FOREACH_DEV(i) {
754 if (enabled_port_mask & (1 << i)) {
756 nic_stats_display(i);
757 else if (enable_xstats)
758 nic_xstats_display(i);
759 else if (reset_stats)
761 else if (reset_xstats)
763 else if (enable_xstats_name)
764 nic_xstats_by_name_display(i, xstats_name);
765 else if (nb_xstats_ids > 0)
766 nic_xstats_by_ids_display(i, xstats_ids,
768 else if (enable_metrics)
773 /* print port independent stats */
775 metrics_display(RTE_METRICS_GLOBAL);
777 /* show information for PMD */
781 ret = rte_eal_cleanup();
783 printf("Error from rte_eal_cleanup(), %d\n", ret);
785 snprintf(bdr_str, MAX_STRING_LEN, " ");
786 STATS_BDR_STR(50, bdr_str);