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