libnl: /data/src/libnl3/src/nl-tctree-list.c Source File

libnl 3.10.0

nl-tctree-list.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
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
16static struct nl_sock *sock;
17static struct nl_cache *qdisc_cache, *class_cache;
18static struct nl_dump_params params = {
19 .dp_type = NL_DUMP_DETAILS,
20};
21
22static int ifindex;
23static void print_qdisc(struct nl_object *, void *);
24static void print_tc_childs(struct rtnl_tc *, void *);
25
26static 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
39static 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
63static 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
77static 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
96static 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
129int 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}
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:1283
nl_cache_free
void nl_cache_free(struct nl_cache *cache)
Free a cache.
Definition cache.c:409
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:1300
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
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:277
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:312
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:331
rtnl_link_get_ifindex
int rtnl_link_get_ifindex(struct rtnl_link *link)
Return interface index of link object.
Definition link.c:2192
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:294
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:382
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:277
rtnl_tc_get_ifindex
int rtnl_tc_get_ifindex(struct rtnl_tc *tc)
Return interface index of traffic control object.
Definition tc.c:292
rtnl_tc_get_handle
uint32_t rtnl_tc_get_handle(struct rtnl_tc *tc)
Return identifier of a traffic control object.
Definition tc.c:495
TC_CAST
#define TC_CAST(ptr)
Macro to cast qdisc/class/classifier to tc object.
Definition tc.h:50
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:506
NL_DUMP_DETAILS
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition types.h:21
nl_dump_params
Dumping parameters.
Definition types.h:32
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:42
nl_dump_params::dp_type
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition types.h:36
nl_dump_params::dp_fd
FILE * dp_fd
File descriptor the dumping output should go to.
Definition types.h:81
rtnl_class
Definition class.c:23
rtnl_link
Link object.
Definition nl-route.h:29
rtnl_qdisc
Definition nl-route.h:118
rtnl_tc
Definition nl-route.h:114