xref: /aosp_15_r20/external/libnl/src/nl-qdisc-list.c (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2003-2010 Thomas Graf <[email protected]>
4  */
5 
6 #include "nl-default.h"
7 
8 #include <linux/pkt_sched.h>
9 #include <linux/netlink.h>
10 
11 #include <netlink/cli/utils.h>
12 #include <netlink/cli/tc.h>
13 #include <netlink/cli/qdisc.h>
14 #include <netlink/cli/class.h>
15 #include <netlink/cli/cls.h>
16 #include <netlink/cli/link.h>
17 
18 #define NUM_INDENT 4
19 
20 static struct nl_sock *sock;
21 static int recursive = 0;
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-qdisc-list [OPTION]... [QDISC]\n"
30 	"\n"
31 	"OPTIONS\n"
32 	"     --details         Show details\n"
33 	"     --stats           Show statistics\n"
34 	" -r, --recursive       Show recursive tree\n"
35 	" -h, --help            Show this help\n"
36 	" -v, --version         Show versioning information\n"
37 	"\n"
38 	" -d, --dev=DEV         Device the qdisc is attached to. (default: all)\n"
39 	" -p, --parent=ID       Identifier of parent qdisc.\n"
40 	" -i, --id=ID           Identifier.\n"
41 	" -k, --kind=NAME       Kind of qdisc (e.g. pfifo_fast)\n"
42 	"\n"
43 	"EXAMPLE\n"
44 	"    # Display statistics of all qdiscs attached to eth0\n"
45 	"    $ nl-qdisc-list --details --dev=eth0\n"
46 	"\n"
47 	);
48 	exit(0);
49 }
50 
51 static void list_classes(int ifindex, uint32_t parent);
52 static void list_qdiscs(int ifindex, uint32_t parent);
53 
list_class(struct nl_object * obj,void * arg)54 static void list_class(struct nl_object *obj, void *arg)
55 {
56 	struct rtnl_tc *tc = nl_object_priv(obj);
57 	nl_object_dump(obj, &params);
58 
59 	list_classes(rtnl_tc_get_ifindex(tc), rtnl_tc_get_handle(tc));
60 	list_qdiscs(rtnl_tc_get_ifindex(tc), rtnl_tc_get_handle(tc));
61 }
62 
list_classes(int ifindex,uint32_t parent)63 static void list_classes(int ifindex, uint32_t parent)
64 {
65 	struct nl_cache *class_cache;
66 	struct rtnl_class *filter = nl_cli_class_alloc();
67 
68 	class_cache = nl_cli_class_alloc_cache(sock, ifindex);
69 
70 	rtnl_tc_set_parent((struct rtnl_tc *) filter, parent);
71 	params.dp_prefix += NUM_INDENT;
72 	nl_cache_foreach_filter(class_cache, OBJ_CAST(filter), list_class, NULL);
73 	params.dp_prefix -= NUM_INDENT;
74 
75 	rtnl_class_put(filter);
76 	nl_cache_free(class_cache);
77 }
78 
list_cls(int ifindex,uint32_t parent)79 static void list_cls(int ifindex, uint32_t parent)
80 {
81 	struct nl_cache *cls_cache;
82 
83 	cls_cache = nl_cli_cls_alloc_cache(sock, ifindex, parent);
84 
85 	params.dp_prefix += NUM_INDENT;
86 	nl_cache_dump(cls_cache, &params);
87 	params.dp_prefix -= NUM_INDENT;
88 
89 	nl_cache_free(cls_cache);
90 }
91 
list_qdisc(struct nl_object * obj,void * arg)92 static void list_qdisc(struct nl_object *obj, void *arg)
93 {
94 	struct rtnl_qdisc *qdisc = nl_object_priv(obj);
95 	struct rtnl_tc *tc = (struct rtnl_tc *) qdisc;
96 
97 	nl_object_dump(obj, &params);
98 
99 	list_cls(rtnl_tc_get_ifindex(tc), rtnl_tc_get_handle(tc));
100 
101 	if (rtnl_tc_get_parent(tc) == TC_H_ROOT) {
102 		list_cls(rtnl_tc_get_ifindex(tc), TC_H_ROOT);
103 		list_classes(rtnl_tc_get_ifindex(tc), TC_H_ROOT);
104 	}
105 
106 	list_classes(rtnl_tc_get_ifindex(tc), rtnl_tc_get_handle(tc));
107 }
108 
list_qdiscs(int ifindex,uint32_t parent)109 static void list_qdiscs(int ifindex, uint32_t parent)
110 {
111 	struct nl_cache *qdisc_cache;
112 	struct rtnl_qdisc *filter = nl_cli_qdisc_alloc();
113 
114 	qdisc_cache = nl_cli_qdisc_alloc_cache(sock);
115 
116 	rtnl_tc_set_ifindex((struct rtnl_tc *) filter, ifindex);
117 	rtnl_tc_set_parent((struct rtnl_tc *) filter, parent);
118 	params.dp_prefix += NUM_INDENT;
119 	nl_cache_foreach_filter(qdisc_cache, OBJ_CAST(filter), list_qdisc, NULL);
120 	params.dp_prefix -= NUM_INDENT;
121 
122 	rtnl_qdisc_put(filter);
123 	nl_cache_free(qdisc_cache);
124 }
125 
main(int argc,char * argv[])126 int main(int argc, char *argv[])
127 {
128 	struct rtnl_qdisc *qdisc;
129 	struct rtnl_tc *tc;
130 	struct nl_cache *link_cache, *qdisc_cache;
131 
132 	params.dp_fd = stdout;
133 	sock = nl_cli_alloc_socket();
134 	nl_cli_connect(sock, NETLINK_ROUTE);
135 	link_cache = nl_cli_link_alloc_cache(sock);
136 	qdisc_cache = nl_cli_qdisc_alloc_cache(sock);
137 	qdisc = nl_cli_qdisc_alloc();
138 	tc = (struct rtnl_tc *) qdisc;
139 
140 	for (;;) {
141 		int c, optidx = 0;
142 		enum {
143 			ARG_DETAILS = 257,
144 			ARG_STATS = 258,
145 		};
146 		static struct option long_opts[] = {
147 			{ "details", 0, 0, ARG_DETAILS },
148 			{ "stats", 0, 0, ARG_STATS },
149 			{ "recursive", 0, 0, 'r' },
150 			{ "help", 0, 0, 'h' },
151 			{ "version", 0, 0, 'v' },
152 			{ "dev", 1, 0, 'd' },
153 			{ "parent", 1, 0, 'p' },
154 			{ "id", 1, 0, 'i' },
155 			{ "kind", 1, 0, 'k' },
156 			{ 0, 0, 0, 0 }
157 		};
158 
159 		c = getopt_long(argc, argv, "rhvd:p:i:k:", long_opts, &optidx);
160 		if (c == -1)
161 			break;
162 
163 		switch (c) {
164 		case ARG_DETAILS: params.dp_type = NL_DUMP_DETAILS; break;
165 		case ARG_STATS: params.dp_type = NL_DUMP_STATS; break;
166 		case 'r': recursive = 1; break;
167 		case 'h': print_usage(); break;
168 		case 'v': nl_cli_print_version(); break;
169 		case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
170 		case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
171 		case 'i': nl_cli_tc_parse_handle(tc, optarg, 0); break;
172 		case 'k': nl_cli_tc_parse_kind(tc, optarg); break;
173 		}
174 	}
175 
176 	if (recursive)
177 		nl_cache_foreach_filter(qdisc_cache, OBJ_CAST(qdisc), list_qdisc, NULL);
178 	else
179 		nl_cache_dump_filter(qdisc_cache, &params, OBJ_CAST(qdisc));
180 
181 	return 0;
182 }
183