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