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