• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static struct nl_sock *sock;
18 
19 static struct nl_dump_params params = {
20 	.dp_type = NL_DUMP_LINE,
21 };
22 
print_usage(void)23 static void print_usage(void)
24 {
25 	printf(
26 "Usage: nl-cls-list [OPTION]...\n"
27 "\n"
28 "OPTIONS\n"
29 "     --details             Show details\n"
30 "     --stats               Show statistics\n"
31 " -h, --help                Show this help\n"
32 " -v, --version             Show versioning information\n"
33 "\n"
34 " -d, --dev=DEV             Device the classifier is attached to. (default: all)\n"
35 " -p, --parent=ID           Identifier of parent class.\n"
36 " -i, --id=ID               Identifier.\n"
37 " -k, --kind=NAME           Kind of classifier (e.g. basic, u32, fw)\n"
38 "     --protocol=PROTO      Protocol to match (default: all)\n"
39 "     --prio=PRIO           Priority (default: 0)\n"
40 "\n"
41 "EXAMPLE\n"
42 "    # Display statistics of all classes on eth0\n"
43 "    $ nl-cls-list --stats --dev=eth0\n"
44 "\n"
45 	);
46 	exit(0);
47 }
48 
__dump_link(int ifindex,struct rtnl_cls * filter)49 static void __dump_link(int ifindex, struct rtnl_cls *filter)
50 {
51 	struct nl_cache *cache;
52 	uint32_t parent = rtnl_tc_get_parent((struct rtnl_tc *) filter);
53 
54 	cache = nl_cli_cls_alloc_cache(sock, ifindex, parent);
55 	nl_cache_dump_filter(cache, &params, OBJ_CAST(filter));
56 	nl_cache_free(cache);
57 }
58 
dump_link(struct nl_object * obj,void * arg)59 static void dump_link(struct nl_object *obj, void *arg)
60 {
61 	struct rtnl_link *link = nl_object_priv(obj);
62 
63 	__dump_link(rtnl_link_get_ifindex(link), arg);
64 }
65 
main(int argc,char * argv[])66 int main(int argc, char *argv[])
67 {
68 	struct rtnl_cls *cls;
69 	struct rtnl_tc *tc;
70 	struct nl_cache *link_cache;
71 	int ifindex;
72 
73 	sock = nl_cli_alloc_socket();
74 	nl_cli_connect(sock, NETLINK_ROUTE);
75 	link_cache = nl_cli_link_alloc_cache(sock);
76  	cls = nl_cli_cls_alloc();
77 	tc = (struct rtnl_tc *) cls;
78 
79 	params.dp_fd = stdout;
80 
81 	for (;;) {
82 		int c, optidx = 0;
83 		enum {
84 			ARG_DETAILS = 257,
85 			ARG_STATS = 258,
86 			ARG_PROTO,
87 			ARG_PRIO,
88 		};
89 		static struct option long_opts[] = {
90 			{ "details", 0, 0, ARG_DETAILS },
91 			{ "stats", 0, 0, ARG_STATS },
92 			{ "help", 0, 0, 'h' },
93 			{ "version", 0, 0, 'v' },
94 			{ "dev", 1, 0, 'd' },
95 			{ "parent", 1, 0, 'p' },
96 			{ "id", 1, 0, 'i' },
97 			{ "kind", 1, 0, 'k' },
98 			{ "proto", 1, 0, ARG_PROTO },
99 			{ "prio", 1, 0, ARG_PRIO },
100 			{ 0, 0, 0, 0 }
101 		};
102 
103 		c = getopt_long(argc, argv, "hvd:p:i:k:", long_opts, &optidx);
104 		if (c == -1)
105 			break;
106 
107 		switch (c) {
108 		case ARG_DETAILS: params.dp_type = NL_DUMP_DETAILS; break;
109 		case ARG_STATS: params.dp_type = NL_DUMP_STATS; break;
110 		case 'h': print_usage(); break;
111 		case 'v': nl_cli_print_version(); break;
112 		case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
113 		case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
114 		case 'i': nl_cli_tc_parse_handle(tc, optarg, 0); break;
115 		case 'k': nl_cli_tc_parse_kind(tc, optarg); break;
116 		case ARG_PROTO: nl_cli_cls_parse_proto(cls, optarg); break;
117 		case ARG_PRIO:
118 			rtnl_cls_set_prio(cls, nl_cli_parse_u32(optarg));
119 			break;
120 		}
121  	}
122 
123 	if ((ifindex = rtnl_tc_get_ifindex(tc)))
124 		__dump_link(ifindex, cls);
125 	 else
126 		nl_cache_foreach(link_cache, dump_link, cls);
127 
128 	return 0;
129 }
130