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