1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2010 Thomas Graf <tgraf@suug.ch>
4 */
5
6 #include <netlink/cli/utils.h>
7 #include <netlink/cli/tc.h>
8 #include <netlink/cli/class.h>
9 #include <netlink/cli/link.h>
10
11 #include <linux/netlink.h>
12
13 static struct nl_sock *sock;
14
15 static struct nl_dump_params params = {
16 .dp_type = NL_DUMP_LINE,
17 };
18
print_usage(void)19 static void print_usage(void)
20 {
21 printf(
22 "Usage: nl-class-list [OPTION]...\n"
23 "\n"
24 "OPTIONS\n"
25 " --details Show details\n"
26 " --stats Show statistics\n"
27 " -h, --help Show this help\n"
28 " -v, --version Show versioning information\n"
29 "\n"
30 " -d, --dev=DEV Device the class is attached to. (default: all)\n"
31 " -p, --parent=ID Identifier of parent class.\n"
32 " -i, --id=ID Identifier.\n"
33 " -k, --kind=NAME Kind of class (e.g. pfifo_fast)\n"
34 "\n"
35 "EXAMPLE\n"
36 " # Display statistics of all classes on eth0\n"
37 " $ nl-class-list --stats --dev=eth0\n"
38 "\n"
39 );
40 exit(0);
41 }
42
__dump_class(int ifindex,struct rtnl_class * filter)43 static void __dump_class(int ifindex, struct rtnl_class *filter)
44 {
45 struct nl_cache *cache;
46
47 cache = nl_cli_class_alloc_cache(sock, ifindex);
48 nl_cache_dump_filter(cache, ¶ms, OBJ_CAST(filter));
49 }
50
dump_class(struct nl_object * obj,void * arg)51 static void dump_class(struct nl_object *obj, void *arg)
52 {
53 struct rtnl_link *link = nl_object_priv(obj);
54
55 __dump_class(rtnl_link_get_ifindex(link), arg);
56 }
57
main(int argc,char * argv[])58 int main(int argc, char *argv[])
59 {
60 struct rtnl_class *class;
61 struct rtnl_tc *tc;
62 struct nl_cache *link_cache;
63 int ifindex;
64
65 sock = nl_cli_alloc_socket();
66 nl_cli_connect(sock, NETLINK_ROUTE);
67 link_cache = nl_cli_link_alloc_cache(sock);
68 class = nl_cli_class_alloc();
69 tc = (struct rtnl_tc *) class;
70
71 params.dp_fd = stdout;
72
73 for (;;) {
74 int c, optidx = 0;
75 enum {
76 ARG_DETAILS = 257,
77 ARG_STATS = 258,
78 };
79 static struct option long_opts[] = {
80 { "details", 0, 0, ARG_DETAILS },
81 { "stats", 0, 0, ARG_STATS },
82 { "help", 0, 0, 'h' },
83 { "version", 0, 0, 'v' },
84 { "dev", 1, 0, 'd' },
85 { "parent", 1, 0, 'p' },
86 { "id", 1, 0, 'i' },
87 { "kind", 1, 0, 'k' },
88 { 0, 0, 0, 0 }
89 };
90
91 c = getopt_long(argc, argv, "hvd:p:i:k:", long_opts, &optidx);
92 if (c == -1)
93 break;
94
95 switch (c) {
96 case ARG_DETAILS: params.dp_type = NL_DUMP_DETAILS; break;
97 case ARG_STATS: params.dp_type = NL_DUMP_STATS; break;
98 case 'h': print_usage(); break;
99 case 'v': nl_cli_print_version(); break;
100 case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
101 case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
102 case 'i': nl_cli_tc_parse_handle(tc, optarg, 0); break;
103 case 'k': nl_cli_tc_parse_kind(tc, optarg); break;
104 }
105 }
106
107 if ((ifindex = rtnl_tc_get_ifindex(tc)))
108 __dump_class(ifindex, class);
109 else
110 nl_cache_foreach(link_cache, dump_class, class);
111
112 return 0;
113 }
114