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

libnl 3.7.0

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