e2f11a288eb6045cd93c006ba9f3ef9acdd51975
[libnl.git] / src / nl-tctree-dump.c
1 /*
2  * src/nl-tctree-dump.c         Dump Traffic Control Tree
3  *
4  *      This library is free software; you can redistribute it and/or
5  *      modify it under the terms of the GNU Lesser General Public
6  *      License as published by the Free Software Foundation version 2.1
7  *      of the License.
8  *
9  * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
10  */
11
12 #include "utils.h"
13 #include <linux/pkt_sched.h>
14
15 static struct nl_handle *nl_handle;
16 static struct nl_cache *qdisc_cache, *class_cache;
17 static struct nl_dump_params dump_params = {
18         .dp_type = NL_DUMP_FULL,
19 };
20
21 static int ifindex;
22 static void print_qdisc(struct nl_object *, void *);
23
24 static void print_class(struct nl_object *obj, void *arg)
25 {
26         struct rtnl_qdisc *leaf;
27         struct rtnl_class *class = (struct rtnl_class *) obj;
28         struct nl_cache *cls_cache;
29         uint32_t parent = rtnl_class_get_handle(class);
30
31         dump_params.dp_prefix = (int) arg;
32         nl_object_dump(obj, &dump_params);
33
34         leaf = rtnl_class_leaf_qdisc(class, qdisc_cache);
35         if (leaf)
36                 print_qdisc((struct nl_object *) leaf, arg + 2);
37
38         rtnl_class_foreach_child(class, class_cache, &print_class, arg + 2);
39
40         cls_cache = rtnl_cls_alloc_cache(nl_handle, ifindex, parent);
41         if (!cls_cache)
42                 return;
43
44         dump_params.dp_prefix = (int) arg + 2;
45         nl_cache_dump(cls_cache, &dump_params);
46         nl_cache_free(cls_cache);
47 }
48
49 static void print_qdisc(struct nl_object *obj, void *arg)
50 {
51         struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) obj;
52         struct nl_cache *cls_cache;
53         uint32_t parent = rtnl_qdisc_get_handle(qdisc);
54
55         dump_params.dp_prefix = (int) arg;
56         nl_object_dump(obj, &dump_params);
57
58         rtnl_qdisc_foreach_child(qdisc, class_cache, &print_class, arg + 2);
59
60         cls_cache = rtnl_cls_alloc_cache(nl_handle, ifindex, parent);
61         if (!cls_cache)
62                 return;
63
64         dump_params.dp_prefix = (int) arg + 2;
65         nl_cache_dump(cls_cache, &dump_params);
66         nl_cache_free(cls_cache);
67 }
68
69 static void print_link(struct nl_object *obj, void *arg)
70 {
71         struct rtnl_link *link = (struct rtnl_link *) obj;
72         struct rtnl_qdisc *qdisc;
73
74         ifindex = rtnl_link_get_ifindex(link);
75         dump_params.dp_prefix = 0;
76         nl_object_dump(obj, &dump_params);
77
78         class_cache = rtnl_class_alloc_cache(nl_handle, ifindex);
79         if (!class_cache)
80                 return;
81
82         qdisc = rtnl_qdisc_get_by_parent(qdisc_cache, ifindex, TC_H_ROOT);
83         if (qdisc) {
84                 print_qdisc((struct nl_object *) qdisc, (void *) 2);
85                 rtnl_qdisc_put(qdisc);
86         }
87
88         qdisc = rtnl_qdisc_get_by_parent(qdisc_cache, ifindex, 0);
89         if (qdisc) {
90                 print_qdisc((struct nl_object *) qdisc, (void *) 2);
91                 rtnl_qdisc_put(qdisc);
92         }
93
94         qdisc = rtnl_qdisc_get_by_parent(qdisc_cache, ifindex, TC_H_INGRESS);
95         if (qdisc) {
96                 print_qdisc((struct nl_object *) qdisc, (void *) 2);
97                 rtnl_qdisc_put(qdisc);
98         }
99
100         nl_cache_free(class_cache);
101 }
102
103 int main(int argc, char *argv[])
104 {
105         struct nl_cache *link_cache;
106
107         if (nltool_init(argc, argv) < 0)
108                 return -1;
109
110         dump_params.dp_fd = stdout;
111
112         if (argc > 1) {
113                 if (!strcasecmp(argv[1], "brief"))
114                         dump_params.dp_type = NL_DUMP_BRIEF;
115                 else if (!strcasecmp(argv[1], "full"))
116                         dump_params.dp_type = NL_DUMP_FULL;
117                 else if (!strcasecmp(argv[1], "stats"))
118                         dump_params.dp_type = NL_DUMP_STATS;
119         }
120
121         nl_handle = nl_handle_alloc_nondefault(nltool_cbset);
122         if (!nl_handle)
123                 return 1;
124
125         if (nltool_connect(nl_handle, NETLINK_ROUTE) < 0)
126                 return 1;
127
128         link_cache = nltool_alloc_link_cache(nl_handle);
129         if (!link_cache)
130                 return 1;
131
132         qdisc_cache = nltool_alloc_qdisc_cache(nl_handle);
133         if (!qdisc_cache)
134                 return 1;
135
136         nl_cache_foreach(link_cache, &print_link, NULL);
137
138         nl_cache_free(qdisc_cache);
139         nl_cache_free(link_cache);
140
141         nl_close(nl_handle);
142         nl_handle_destroy(nl_handle);
143         return 0;
144 }