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