libnl  3.4.0
nl-cls-list.c
1 /*
2  * src/nl-cls-list.c List classifiers
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) 2008-2010 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #include <netlink/cli/utils.h>
13 #include <netlink/cli/tc.h>
14 #include <netlink/cli/cls.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-cls-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 classifier 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 classifier (e.g. basic, u32, fw)\n"
40 " --proto=PROTO Protocol to match (default: all)\n"
41 " --prio=PRIO Priority (default: 0)\n"
42 "\n"
43 "EXAMPLE\n"
44 " # Display statistics of all classes on eth0\n"
45 " $ nl-cls-list --stats --dev=eth0\n"
46 "\n"
47  );
48  exit(0);
49 }
50 
51 static void __dump_link(int ifindex, struct rtnl_cls *filter)
52 {
53  struct nl_cache *cache;
54  uint32_t parent = rtnl_tc_get_parent((struct rtnl_tc *) filter);
55 
56  cache = nl_cli_cls_alloc_cache(sock, ifindex, parent);
57  nl_cache_dump_filter(cache, &params, OBJ_CAST(filter));
58  nl_cache_free(cache);
59 }
60 
61 static void dump_link(struct nl_object *obj, void *arg)
62 {
63  struct rtnl_link *link = nl_object_priv(obj);
64 
65  __dump_link(rtnl_link_get_ifindex(link), arg);
66 }
67 
68 int main(int argc, char *argv[])
69 {
70  struct rtnl_cls *cls;
71  struct rtnl_tc *tc;
72  struct nl_cache *link_cache;
73  int ifindex;
74 
75  sock = nl_cli_alloc_socket();
76  nl_cli_connect(sock, NETLINK_ROUTE);
77  link_cache = nl_cli_link_alloc_cache(sock);
78  cls = nl_cli_cls_alloc();
79  tc = (struct rtnl_tc *) cls;
80 
81  params.dp_fd = stdout;
82 
83  for (;;) {
84  int c, optidx = 0;
85  enum {
86  ARG_DETAILS = 257,
87  ARG_STATS = 258,
88  ARG_PROTO,
89  ARG_PRIO,
90  };
91  static struct option long_opts[] = {
92  { "details", 0, 0, ARG_DETAILS },
93  { "stats", 0, 0, ARG_STATS },
94  { "help", 0, 0, 'h' },
95  { "version", 0, 0, 'v' },
96  { "dev", 1, 0, 'd' },
97  { "parent", 1, 0, 'p' },
98  { "id", 1, 0, 'i' },
99  { "kind", 1, 0, 'k' },
100  { "proto", 1, 0, ARG_PROTO },
101  { "prio", 1, 0, ARG_PRIO },
102  { 0, 0, 0, 0 }
103  };
104 
105  c = getopt_long(argc, argv, "hvd:p:i:k:", long_opts, &optidx);
106  if (c == -1)
107  break;
108 
109  switch (c) {
110  case ARG_DETAILS: params.dp_type = NL_DUMP_DETAILS; break;
111  case ARG_STATS: params.dp_type = NL_DUMP_STATS; break;
112  case 'h': print_usage(); break;
113  case 'v': nl_cli_print_version(); break;
114  case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
115  case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
116  case 'i': nl_cli_tc_parse_handle(tc, optarg, 0); break;
117  case 'k': nl_cli_tc_parse_kind(tc, optarg); break;
118  case ARG_PROTO: nl_cli_cls_parse_proto(cls, optarg); break;
119  case ARG_PRIO:
120  rtnl_cls_set_prio(cls, nl_cli_parse_u32(optarg));
121  break;
122  }
123  }
124 
125  if ((ifindex = rtnl_tc_get_ifindex(tc)))
126  __dump_link(ifindex, cls);
127  else
128  nl_cache_foreach(link_cache, dump_link, cls);
129 
130  return 0;
131 }
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_free(struct nl_cache *cache)
Free a cache.
Definition: cache.c:408
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
uint32_t rtnl_tc_get_parent(struct rtnl_tc *tc)
Return parent identifier of a traffic control object.
Definition: tc.c:499
uint32_t nl_cli_parse_u32(const char *arg)
Parse a text based 32 bit unsigned integer argument.
Definition: utils.c:42
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