libnl: /data/src/libnl3/src/nl-link-stats.c Source File

libnl 3.10.0

nl-link-stats.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
4 */
5
6#include "nl-default.h"
7
8#include <linux/netlink.h>
9
10#include <netlink/cli/utils.h>
11#include <netlink/cli/link.h>
12
13static void print_usage(void)
14{
15 printf(
16 "Usage: nl-link-stats [OPTION]... [LINK] [ListOfStats]\n"
17 "\n"
18 "Options\n"
19 " -l, --list List available statistic names\n"
20 " -h, --help Show this help\n"
21 " -v, --version Show versioning information\n"
22 "\n"
23 "Link Options\n"
24 " -n, --name=NAME link name\n"
25 " -i, --index=NUM interface index\n"
26 );
27 exit(0);
28}
29
30static void list_stat_names(void)
31{
32 char buf[64];
33 int i;
34
35 for (i = 0; i <= RTNL_LINK_STATS_MAX; i++)
36 printf("%s\n", rtnl_link_stat2str(i, buf, sizeof(buf)));
37
38 exit(0);
39}
40
41static int gargc;
42
43static void dump_stat(struct rtnl_link *link, int id)
44{
45 uint64_t st = rtnl_link_get_stat(link, id);
46 char buf[64];
47
48 printf("%s.%s %" PRIu64 "\n", rtnl_link_get_name(link),
49 rtnl_link_stat2str(id, buf, sizeof(buf)), st);
50}
51
52static void dump_stats(struct nl_object *obj, void *arg)
53{
54 struct rtnl_link *link = (struct rtnl_link *) obj;
55 char **argv = arg;
56
57 if (optind >= gargc) {
58 int i;
59
60 for (i = 0; i <= RTNL_LINK_STATS_MAX; i++)
61 dump_stat(link, i);
62 } else {
63 while (optind < gargc) {
64 int id = rtnl_link_str2stat(argv[optind]);
65
66 if (id < 0)
67 fprintf(stderr, "Warning: Unknown statistic "
68 "\"%s\"\n", argv[optind]);
69 else
70 dump_stat(link, id);
71
72 optind++;
73 }
74 }
75}
76
77int main(int argc, char *argv[])
78{
79 struct nl_sock *sock;
80 struct nl_cache *link_cache;
81 struct rtnl_link *link;
82
83 sock = nl_cli_alloc_socket();
84 nl_cli_connect(sock, NETLINK_ROUTE);
85 link_cache = nl_cli_link_alloc_cache(sock);
86 link = nl_cli_link_alloc();
87
88 for (;;) {
89 int c, optidx = 0;
90 static struct option long_opts[] = {
91 { "list", 0, 0, 'l' },
92 { "help", 0, 0, 'h' },
93 { "version", 0, 0, 'v' },
94 { "name", 1, 0, 'n' },
95 { "index", 1, 0, 'i' },
96 { 0, 0, 0, 0 }
97 };
98
99 c = getopt_long(argc, argv, "lhvn:i:", long_opts, &optidx);
100 if (c == -1)
101 break;
102
103 switch (c) {
104 case 'l': list_stat_names(); break;
105 case 'h': print_usage(); break;
106 case 'v': nl_cli_print_version(); break;
107 case 'n': nl_cli_link_parse_name(link, optarg); break;
108 case 'i': nl_cli_link_parse_ifindex(link, optarg); break;
109 }
110 }
111
112 gargc = argc;
113 nl_cache_foreach_filter(link_cache, OBJ_CAST(link), dump_stats, argv);
114
115 return 0;
116}
117
nl_cache_foreach_filter
void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter, void(*cb)(struct nl_object *, void *), void *arg)
Call a callback on each element of the cache (filtered).
Definition cache.c:1300
rtnl_link_get_stat
uint64_t rtnl_link_get_stat(struct rtnl_link *link, rtnl_link_stat_id_t id)
Return value of link statistics counter.
Definition link.c:2516
rtnl_link_get_name
char * rtnl_link_get_name(struct rtnl_link *link)
Return name of link object.
Definition link.c:1962
rtnl_link
Link object.
Definition nl-route.h:29