libnl  3.4.0
nl-tctree-list.c
1 /*
2  * src/nl-tctree-list.c List Traffic Control Tree
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-2009 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #include <netlink/cli/utils.h>
13 #include <netlink/cli/link.h>
14 #include <netlink/cli/qdisc.h>
15 #include <netlink/cli/class.h>
16 
17 #include <linux/netlink.h>
18 #include <linux/pkt_sched.h>
19 
20 static struct nl_sock *sock;
21 static struct nl_cache *qdisc_cache, *class_cache;
22 static struct nl_dump_params params = {
24 };
25 
26 static int ifindex;
27 static void print_qdisc(struct nl_object *, void *);
28 static void print_tc_childs(struct rtnl_tc *, void *);
29 
30 static void print_usage(void)
31 {
32  printf(
33  "Usage: nl-tctree-list [OPTION]...\n"
34  "\n"
35  "Options\n"
36  " -f, --format=TYPE Output format { brief | details | stats }\n"
37  " -h, --help Show this help\n"
38  " -v, --version Show versioning information\n"
39  );
40  exit(0);
41 }
42 
43 static void print_class(struct nl_object *obj, void *arg)
44 {
45  struct rtnl_qdisc *leaf;
46  struct rtnl_class *class = (struct rtnl_class *) obj;
47  struct nl_cache *cls_cache;
48  uint32_t parent = rtnl_tc_get_handle((struct rtnl_tc *) class);
49 
50  params.dp_prefix = (int)(long) arg;
51  nl_object_dump(obj, &params);
52 
53  leaf = rtnl_class_leaf_qdisc(class, qdisc_cache);
54  if (leaf)
55  print_qdisc((struct nl_object *) leaf, arg + 2);
56 
57  print_tc_childs(TC_CAST(class), arg + 2);
58 
59  if (rtnl_cls_alloc_cache(sock, ifindex, parent, &cls_cache) < 0)
60  return;
61 
62  params.dp_prefix = (int)(long) arg + 2;
63  nl_cache_dump(cls_cache, &params);
64  nl_cache_free(cls_cache);
65 }
66 
67 static void print_tc_childs(struct rtnl_tc *tc, void *arg)
68 {
69  struct rtnl_class *filter;
70 
71  filter = nl_cli_class_alloc();
72 
75 
76  nl_cache_foreach_filter(class_cache, OBJ_CAST(filter), &print_class, arg);
77 
78  rtnl_class_put(filter);
79 }
80 
81 static void print_qdisc(struct nl_object *obj, void *arg)
82 {
83  struct rtnl_qdisc *qdisc = (struct rtnl_qdisc *) obj;
84  struct nl_cache *cls_cache;
85  uint32_t parent = rtnl_tc_get_handle((struct rtnl_tc *) qdisc);
86 
87  params.dp_prefix = (int)(long) arg;
88  nl_object_dump(obj, &params);
89 
90  print_tc_childs(TC_CAST(qdisc), arg + 2);
91 
92  if (rtnl_cls_alloc_cache(sock, ifindex, parent, &cls_cache) < 0)
93  return;
94 
95  params.dp_prefix = (int)(long) arg + 2;
96  nl_cache_dump(cls_cache, &params);
97  nl_cache_free(cls_cache);
98 }
99 
100 static void print_link(struct nl_object *obj, void *arg)
101 {
102  struct rtnl_link *link = (struct rtnl_link *) obj;
103  struct rtnl_qdisc *qdisc;
104 
105  ifindex = rtnl_link_get_ifindex(link);
106  params.dp_prefix = 0;
107  nl_object_dump(obj, &params);
108 
109  if (rtnl_class_alloc_cache(sock, ifindex, &class_cache) < 0)
110  return;
111 
112  qdisc = rtnl_qdisc_get_by_parent(qdisc_cache, ifindex, TC_H_ROOT);
113  if (qdisc) {
114  print_qdisc((struct nl_object *) qdisc, (void *) 2);
115  rtnl_qdisc_put(qdisc);
116  }
117 
118  qdisc = rtnl_qdisc_get_by_parent(qdisc_cache, ifindex, 0);
119  if (qdisc) {
120  print_qdisc((struct nl_object *) qdisc, (void *) 2);
121  rtnl_qdisc_put(qdisc);
122  }
123 
124  qdisc = rtnl_qdisc_get_by_parent(qdisc_cache, ifindex, TC_H_INGRESS);
125  if (qdisc) {
126  print_qdisc((struct nl_object *) qdisc, (void *) 2);
127  rtnl_qdisc_put(qdisc);
128  }
129 
130  nl_cache_free(class_cache);
131 }
132 
133 int main(int argc, char *argv[])
134 {
135  struct nl_cache *link_cache;
136 
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 
142  params.dp_fd = stdout;
143 
144  for (;;) {
145  int c, optidx = 0;
146  static struct option long_opts[] = {
147  { "format", 1, 0, 'f' },
148  { "help", 0, 0, 'h' },
149  { "version", 0, 0, 'v' },
150  { 0, 0, 0, 0 }
151  };
152 
153  c = getopt_long(argc, argv, "f:hv", long_opts, &optidx);
154  if (c == -1)
155  break;
156 
157  switch (c) {
158  case 'f': params.dp_type = nl_cli_parse_dumptype(optarg); break;
159  case 'h': print_usage(); break;
160  case 'v': nl_cli_print_version(); break;
161  }
162  }
163 
164  nl_cache_foreach(link_cache, &print_link, NULL);
165 
166  return 0;
167 }
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:327
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:387
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
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
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
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 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:1265
void rtnl_tc_set_ifindex(struct rtnl_tc *tc, int ifindex)
Set interface index of traffic control object.
Definition: tc.c:260
#define TC_CAST(ptr)
Macro to cast qdisc/class/classifier to tc object.
Definition: tc.h:56
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
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