1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * src/nl-tctree-list.c List Traffic Control Tree
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 #include <netlink/cli/qdisc.h>
16 #include <netlink/cli/class.h>
17
18 #include <linux/netlink.h>
19 #include <linux/pkt_sched.h>
20
21 static struct nl_sock *sock;
22 static struct nl_cache *qdisc_cache, *class_cache;
23 static struct nl_dump_params params = {
24 .dp_type = NL_DUMP_DETAILS,
25 };
26
27 static int ifindex;
28 static void print_qdisc(struct nl_object *, void *);
29 static void print_tc_childs(struct rtnl_tc *, void *);
30
print_usage(void)31 static void print_usage(void)
32 {
33 printf(
34 "Usage: nl-tctree-list [OPTION]...\n"
35 "\n"
36 "Options\n"
37 " -f, --format=TYPE Output format { brief | details | stats }\n"
38 " -h, --help Show this help\n"
39 " -v, --version Show versioning information\n"
40 );
41 exit(0);
42 }
43
print_class(struct nl_object * obj,void * arg)44 static void print_class(struct nl_object *obj, void *arg)
45 {
46 struct rtnl_qdisc *leaf;
47 struct rtnl_class *class = (struct rtnl_class *) obj;
48 struct nl_cache *cls_cache;
49 uint32_t parent = rtnl_tc_get_handle((struct rtnl_tc *) class);
50
51 params.dp_prefix = (int)(long) arg;
52 nl_object_dump(obj, ¶ms);
53
54 leaf = rtnl_class_leaf_qdisc(class, qdisc_cache);
55 if (leaf)
56 print_qdisc((struct nl_object *) leaf, (char *) arg + 2);
57
58 print_tc_childs(TC_CAST(class), (char *) arg + 2);
59
60 if (rtnl_cls_alloc_cache(sock, ifindex, parent, &cls_cache) < 0)
61 return;
62
63 params.dp_prefix = (int)(long) arg + 2;
64 nl_cache_dump(cls_cache, ¶ms);
65 nl_cache_free(cls_cache);
66 }
67
print_tc_childs(struct rtnl_tc * tc,void * arg)68 static void print_tc_childs(struct rtnl_tc *tc, void *arg)
69 {
70 struct rtnl_class *filter;
71
72 filter = nl_cli_class_alloc();
73
74 rtnl_tc_set_parent(TC_CAST(filter), rtnl_tc_get_handle(tc));
75 rtnl_tc_set_ifindex(TC_CAST(filter), rtnl_tc_get_ifindex(tc));
76
77 nl_cache_foreach_filter(class_cache, OBJ_CAST(filter), &print_class, arg);
78
79 rtnl_class_put(filter);
80 }
81
print_qdisc(struct nl_object * obj,void * arg)82 static void print_qdisc(struct nl_object *obj, void *arg)
83 {
84 struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) obj;
85 struct nl_cache *cls_cache;
86 uint32_t parent = rtnl_tc_get_handle((struct rtnl_tc *) qdisc);
87
88 params.dp_prefix = (int)(long) arg;
89 nl_object_dump(obj, ¶ms);
90
91 print_tc_childs(TC_CAST(qdisc), (char *) arg + 2);
92
93 if (rtnl_cls_alloc_cache(sock, ifindex, parent, &cls_cache) < 0)
94 return;
95
96 params.dp_prefix = (int)(long) arg + 2;
97 nl_cache_dump(cls_cache, ¶ms);
98 nl_cache_free(cls_cache);
99 }
100
print_link(struct nl_object * obj,void * arg)101 static void print_link(struct nl_object *obj, void *arg)
102 {
103 struct rtnl_link *link = (struct rtnl_link *) obj;
104 struct rtnl_qdisc *qdisc;
105
106 ifindex = rtnl_link_get_ifindex(link);
107 params.dp_prefix = 0;
108 nl_object_dump(obj, ¶ms);
109
110 if (rtnl_class_alloc_cache(sock, ifindex, &class_cache) < 0)
111 return;
112
113 qdisc = rtnl_qdisc_get_by_parent(qdisc_cache, ifindex, TC_H_ROOT);
114 if (qdisc) {
115 print_qdisc((struct nl_object *) qdisc, (void *) 2);
116 rtnl_qdisc_put(qdisc);
117 }
118
119 qdisc = rtnl_qdisc_get_by_parent(qdisc_cache, ifindex, 0);
120 if (qdisc) {
121 print_qdisc((struct nl_object *) qdisc, (void *) 2);
122 rtnl_qdisc_put(qdisc);
123 }
124
125 qdisc = rtnl_qdisc_get_by_parent(qdisc_cache, ifindex, TC_H_INGRESS);
126 if (qdisc) {
127 print_qdisc((struct nl_object *) qdisc, (void *) 2);
128 rtnl_qdisc_put(qdisc);
129 }
130
131 nl_cache_free(class_cache);
132 }
133
main(int argc,char * argv[])134 int main(int argc, char *argv[])
135 {
136 struct nl_cache *link_cache;
137
138 sock = nl_cli_alloc_socket();
139 nl_cli_connect(sock, NETLINK_ROUTE);
140 link_cache = nl_cli_link_alloc_cache(sock);
141 qdisc_cache = nl_cli_qdisc_alloc_cache(sock);
142
143 params.dp_fd = stdout;
144
145 for (;;) {
146 int c, optidx = 0;
147 static struct option long_opts[] = {
148 { "format", 1, 0, 'f' },
149 { "help", 0, 0, 'h' },
150 { "version", 0, 0, 'v' },
151 { 0, 0, 0, 0 }
152 };
153
154 c = getopt_long(argc, argv, "f:hv", long_opts, &optidx);
155 if (c == -1)
156 break;
157
158 switch (c) {
159 case 'f': params.dp_type = nl_cli_parse_dumptype(optarg); break;
160 case 'h': print_usage(); break;
161 case 'v': nl_cli_print_version(); break;
162 }
163 }
164
165 nl_cache_foreach(link_cache, &print_link, NULL);
166
167 return 0;
168 }
169