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
17static struct nl_sock *sock;
18
19static struct nl_dump_params params = {
20	.dp_type = NL_DUMP_LINE,
21};
22
23static void print_usage(void)
24{
25	printf(
26	"Usage: nl-class-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 class 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 class (e.g. pfifo_fast)\n"
38	"\n"
39	"EXAMPLE\n"
40	"    # Display statistics of all classes on eth0\n"
41	"    $ nl-class-list --stats --dev=eth0\n"
42	"\n"
43	);
44	exit(0);
45}
46
47static void __dump_class(int ifindex, struct rtnl_class *filter)
48{
49	struct nl_cache *cache;
50
51	cache = nl_cli_class_alloc_cache(sock, ifindex);
52	nl_cache_dump_filter(cache, &params, OBJ_CAST(filter));
53}
54
55static void dump_class(struct nl_object *obj, void *arg)
56{
57	struct rtnl_link *link = nl_object_priv(obj);
58
59	__dump_class(rtnl_link_get_ifindex(link), arg);
60}
61
62int main(int argc, char *argv[])
63{
64	struct rtnl_class *class;
65	struct rtnl_tc *tc;
66	struct nl_cache *link_cache;
67	int ifindex;
68
69	sock = nl_cli_alloc_socket();
70	nl_cli_connect(sock, NETLINK_ROUTE);
71	link_cache = nl_cli_link_alloc_cache(sock);
72 	class = nl_cli_class_alloc();
73	tc = (struct rtnl_tc *) class;
74
75	params.dp_fd = stdout;
76
77	for (;;) {
78		int c, optidx = 0;
79		enum {
80			ARG_DETAILS = 257,
81			ARG_STATS = 258,
82		};
83		static struct option long_opts[] = {
84			{ "details", 0, 0, ARG_DETAILS },
85			{ "stats", 0, 0, ARG_STATS },
86			{ "help", 0, 0, 'h' },
87			{ "version", 0, 0, 'v' },
88			{ "dev", 1, 0, 'd' },
89			{ "parent", 1, 0, 'p' },
90			{ "id", 1, 0, 'i' },
91			{ "kind", 1, 0, 'k' },
92			{ 0, 0, 0, 0 }
93		};
94
95		c = getopt_long(argc, argv, "hvd:p:i:k:", long_opts, &optidx);
96		if (c == -1)
97			break;
98
99		switch (c) {
100		case ARG_DETAILS: params.dp_type = NL_DUMP_DETAILS; break;
101		case ARG_STATS: params.dp_type = NL_DUMP_STATS; break;
102		case 'h': print_usage(); break;
103		case 'v': nl_cli_print_version(); break;
104		case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
105		case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
106		case 'i': nl_cli_tc_parse_handle(tc, optarg, 0); break;
107		case 'k': nl_cli_tc_parse_kind(tc, optarg); break;
108		}
109 	}
110
111	if ((ifindex = rtnl_tc_get_ifindex(tc)))
112		__dump_class(ifindex, class);
113	 else
114		nl_cache_foreach(link_cache, dump_class, class);
115
116	return 0;
117}
118