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

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