libnl 3.5.0

nl-tctree-list.c
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
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
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, &params);
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, &params);
65 nl_cache_free(cls_cache);
66 }
67
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
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, &params);
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, &params);
98 nl_cache_free(cls_cache);
99 }
100
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, &params);
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
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 }
rtnl_cls_alloc_cache
int rtnl_cls_alloc_cache(struct nl_sock *sk, int ifindex, uint32_t parent, struct nl_cache **result)
Allocate a cache and fill it with all configured classifiers.
Definition: cls.c:328
rtnl_qdisc_get_by_parent
struct rtnl_qdisc * rtnl_qdisc_get_by_parent(struct nl_cache *cache, int ifindex, uint32_t parent)
Search qdisc by interface index and parent.
Definition: qdisc.c:388
nl_dump_params::dp_fd
FILE * dp_fd
File descriptor the dumping output should go to.
Definition: types.h:83
nl_dump_params::dp_type
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:38
rtnl_class_leaf_qdisc
struct rtnl_qdisc * rtnl_class_leaf_qdisc(struct rtnl_class *class, struct nl_cache *cache)
Lookup the leaf qdisc of a traffic class.
Definition: class.c:278
rtnl_class_alloc_cache
int rtnl_class_alloc_cache(struct nl_sock *sk, int ifindex, struct nl_cache **result)
Allocate a cache and fill it with all configured traffic classes.
Definition: class.c:313
rtnl_tc_set_parent
void rtnl_tc_set_parent(struct rtnl_tc *tc, uint32_t parent)
Set the parent identifier of a traffic control object.
Definition: tc.c:508
rtnl_link
Link object.
nl_cache_foreach_filter
void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter, void(*cb)(struct nl_object *, void *), void *arg)
Call a callback on each element of the cache (filtered).
Definition: cache.c:1283
NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition: types.h:23
nl_cache_free
void nl_cache_free(struct nl_cache *cache)
Free a cache.
Definition: cache.c:409
rtnl_tc_get_handle
uint32_t rtnl_tc_get_handle(struct rtnl_tc *tc)
Return identifier of a traffic control object.
Definition: tc.c:497
nl_cache_foreach
void nl_cache_foreach(struct nl_cache *cache, void(*cb)(struct nl_object *, void *), void *arg)
Call a callback on each element of the cache.
Definition: cache.c:1266
rtnl_tc_set_ifindex
void rtnl_tc_set_ifindex(struct rtnl_tc *tc, int ifindex)
Set interface index of traffic control object.
Definition: tc.c:279
TC_CAST
#define TC_CAST(ptr)
Macro to cast qdisc/class/classifier to tc object.
Definition: tc.h:56
nl_object_dump
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition: object.c:289
nl_cache_dump
void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params)
Dump all elements of a cache.
Definition: cache.c:1203
nl_dump_params::dp_prefix
int dp_prefix
Specifies the number of whitespaces to be put in front of every new line (indentation).
Definition: types.h:44
nl_dump_params
Dumping parameters.
Definition: types.h:33
rtnl_link_get_ifindex
int rtnl_link_get_ifindex(struct rtnl_link *link)
Return interface index of link object.
Definition: link.c:2202
rtnl_tc_get_ifindex
int rtnl_tc_get_ifindex(struct rtnl_tc *tc)
Return interface index of traffic control object.
Definition: tc.c:294