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