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 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #include "cls/utils.h"
13 
14 static struct nl_sock *sock;
15 static struct rtnl_cls *cls;
16 static struct nl_dump_params params = {
17 	.dp_type = NL_DUMP_LINE,
18 };
19 
print_usage(void)20 static void print_usage(void)
21 {
22 	printf(
23 	"Usage: nl-cls-list [OPTION]... [CLASSIFIER]\n"
24 	"\n"
25 	"Options\n"
26 	" -f, --format=TYPE     Output format { brief | details | stats }\n"
27 	" -h, --help            Show this help text.\n"
28 	" -v, --version         Show versioning information.\n"
29 	"\n"
30 	"Classifier Options\n"
31 	" -d, --dev=DEV         Device the classifier should be assigned to.\n"
32 	" -p, --parent=HANDLE   Parent qdisc/class\n"
33 	"     --proto=PROTO     Protocol\n"
34 	"     --prio=NUM        Priority\n"
35 	"     --id=NUM          Identifier\n"
36 	);
37 	exit(0);
38 }
39 
print_cls(struct nl_object * obj,void * arg)40 static void print_cls(struct nl_object *obj, void *arg)
41 {
42 	struct nl_cache *cls_cache;
43 	int err, ifindex;
44 
45 	if (obj)
46 		ifindex = rtnl_link_get_ifindex((struct rtnl_link *) obj);
47 	else
48 		ifindex = rtnl_cls_get_ifindex(cls);
49 
50 	err = rtnl_cls_alloc_cache(sock, ifindex, rtnl_cls_get_parent(cls),
51 				   &cls_cache);
52 	if (err < 0)
53 		fatal(err, "Unable to allocate classifier cache: %s",
54 		      nl_geterror(err));
55 
56 	nl_cache_dump_filter(cls_cache, &params, OBJ_CAST(cls));
57 	nl_cache_free(cls_cache);
58 }
59 
main(int argc,char * argv[])60 int main(int argc, char *argv[])
61 {
62 	struct nl_cache *link_cache;
63 	int dev = 0;
64 
65 	params.dp_fd = stdout;
66 	sock = nlt_alloc_socket();
67 	nlt_connect(sock, NETLINK_ROUTE);
68 	link_cache = nlt_alloc_link_cache(sock);
69 	cls = nlt_alloc_cls();
70 
71 	for (;;) {
72 		int c, optidx = 0;
73 		enum {
74 			ARG_PROTO = 257,
75 			ARG_PRIO = 258,
76 			ARG_ID,
77 		};
78 		static struct option long_opts[] = {
79 			{ "format", 1, 0, 'f' },
80 			{ "help", 0, 0, 'h' },
81 			{ "version", 0, 0, 'v' },
82 			{ "dev", 1, 0, 'd' },
83 			{ "parent", 1, 0, 'p' },
84 			{ "proto", 1, 0, ARG_PROTO },
85 			{ "prio", 1, 0, ARG_PRIO },
86 			{ "id", 1, 0, ARG_ID },
87 			{ 0, 0, 0, 0 }
88 		};
89 
90 		c = getopt_long(argc, argv, "+f:qhva:d:", long_opts, &optidx);
91 		if (c == -1)
92 			break;
93 
94 		switch (c) {
95 		case '?': exit(NLE_INVAL);
96 		case 'f': params.dp_type = nlt_parse_dumptype(optarg); break;
97 		case 'h': print_usage(); break;
98 		case 'v': nlt_print_version(); break;
99 		case 'd': dev = 1; parse_dev(cls, link_cache, optarg); break;
100 		case 'p': parse_parent(cls, optarg); break;
101 		case ARG_PRIO: parse_prio(cls, optarg); break;
102 		case ARG_ID: parse_handle(cls, optarg); break;
103 		case ARG_PROTO: parse_proto(cls, optarg); break;
104 		}
105  	}
106 
107 	if (!dev)
108 		nl_cache_foreach(link_cache, print_cls, NULL);
109 	else
110 		print_cls(NULL, NULL);
111 
112 	return 0;
113 }
114