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