libnl  3.3.0
nl-class-list.c
1 /*
2  * src/nl-class-list.c List Traffic Classes
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) 2010 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #include <netlink/cli/utils.h>
13 #include <netlink/cli/tc.h>
14 #include <netlink/cli/class.h>
15 #include <netlink/cli/link.h>
16 
17 #include <linux/netlink.h>
18 
19 static struct nl_sock *sock;
20 
21 static struct nl_dump_params params = {
23 };
24 
25 static void print_usage(void)
26 {
27  printf(
28  "Usage: nl-class-list [OPTION]...\n"
29  "\n"
30  "OPTIONS\n"
31  " --details Show details\n"
32  " --stats Show statistics\n"
33  " -h, --help Show this help\n"
34  " -v, --version Show versioning information\n"
35  "\n"
36  " -d, --dev=DEV Device the class is attached to. (default: all)\n"
37  " -p, --parent=ID Identifier of parent class.\n"
38  " -i, --id=ID Identifier.\n"
39  " -k, --kind=NAME Kind of class (e.g. pfifo_fast)\n"
40  "\n"
41  "EXAMPLE\n"
42  " # Display statistics of all classes on eth0\n"
43  " $ nl-class-list --stats --dev=eth0\n"
44  "\n"
45  );
46  exit(0);
47 }
48 
49 static void __dump_class(int ifindex, struct rtnl_class *filter)
50 {
51  struct nl_cache *cache;
52 
53  cache = nl_cli_class_alloc_cache(sock, ifindex);
54  nl_cache_dump_filter(cache, &params, OBJ_CAST(filter));
55 }
56 
57 static void dump_class(struct nl_object *obj, void *arg)
58 {
59  struct rtnl_link *link = nl_object_priv(obj);
60 
61  __dump_class(rtnl_link_get_ifindex(link), arg);
62 }
63 
64 int main(int argc, char *argv[])
65 {
66  struct rtnl_class *class;
67  struct rtnl_tc *tc;
68  struct nl_cache *link_cache;
69  int ifindex;
70 
71  sock = nl_cli_alloc_socket();
72  nl_cli_connect(sock, NETLINK_ROUTE);
73  link_cache = nl_cli_link_alloc_cache(sock);
74  class = nl_cli_class_alloc();
75  tc = (struct rtnl_tc *) class;
76 
77  params.dp_fd = stdout;
78 
79  for (;;) {
80  int c, optidx = 0;
81  enum {
82  ARG_DETAILS = 257,
83  ARG_STATS = 258,
84  };
85  static struct option long_opts[] = {
86  { "details", 0, 0, ARG_DETAILS },
87  { "stats", 0, 0, ARG_STATS },
88  { "help", 0, 0, 'h' },
89  { "version", 0, 0, 'v' },
90  { "dev", 1, 0, 'd' },
91  { "parent", 1, 0, 'p' },
92  { "id", 1, 0, 'i' },
93  { "kind", 1, 0, 'k' },
94  { 0, 0, 0, 0 }
95  };
96 
97  c = getopt_long(argc, argv, "hvd:p:i:k:", long_opts, &optidx);
98  if (c == -1)
99  break;
100 
101  switch (c) {
102  case ARG_DETAILS: params.dp_type = NL_DUMP_DETAILS; break;
103  case ARG_STATS: params.dp_type = NL_DUMP_STATS; break;
104  case 'h': print_usage(); break;
105  case 'v': nl_cli_print_version(); break;
106  case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
107  case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
108  case 'i': nl_cli_tc_parse_handle(tc, optarg, 0); break;
109  case 'k': nl_cli_tc_parse_kind(tc, optarg); break;
110  }
111  }
112 
113  if ((ifindex = rtnl_tc_get_ifindex(tc)))
114  __dump_class(ifindex, class);
115  else
116  nl_cache_foreach(link_cache, dump_class, class);
117 
118  return 0;
119 }
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
Dump all attributes but no statistics.
Definition: types.h:23
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
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