libnl 3.5.0

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