libnl 3.7.0
nl-cls-list.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 2008-2010 Thomas Graf <tgraf@suug.ch>
4 */
5
6#include <netlink/cli/utils.h>
7#include <netlink/cli/tc.h>
8#include <netlink/cli/cls.h>
9#include <netlink/cli/link.h>
10
11#include <linux/netlink.h>
12
13static struct nl_sock *sock;
14
15static struct nl_dump_params params = {
17};
18
19static void print_usage(void)
20{
21 printf(
22"Usage: nl-cls-list [OPTION]...\n"
23"\n"
24"OPTIONS\n"
25" --details Show details\n"
26" --stats Show statistics\n"
27" -h, --help Show this help\n"
28" -v, --version Show versioning information\n"
29"\n"
30" -d, --dev=DEV Device the classifier is attached to. (default: all)\n"
31" -p, --parent=ID Identifier of parent class.\n"
32" -i, --id=ID Identifier.\n"
33" -k, --kind=NAME Kind of classifier (e.g. basic, u32, fw)\n"
34" --proto=PROTO Protocol to match (default: all)\n"
35" --prio=PRIO Priority (default: 0)\n"
36"\n"
37"EXAMPLE\n"
38" # Display statistics of all classes on eth0\n"
39" $ nl-cls-list --stats --dev=eth0\n"
40"\n"
41 );
42 exit(0);
43}
44
45static void __dump_link(int ifindex, struct rtnl_cls *filter)
46{
47 struct nl_cache *cache;
48 uint32_t parent = rtnl_tc_get_parent((struct rtnl_tc *) filter);
49
50 cache = nl_cli_cls_alloc_cache(sock, ifindex, parent);
51 nl_cache_dump_filter(cache, &params, OBJ_CAST(filter));
52 nl_cache_free(cache);
53}
54
55static void dump_link(struct nl_object *obj, void *arg)
56{
57 struct rtnl_link *link = nl_object_priv(obj);
58
59 __dump_link(rtnl_link_get_ifindex(link), arg);
60}
61
62int main(int argc, char *argv[])
63{
64 struct rtnl_cls *cls;
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 cls = nl_cli_cls_alloc();
73 tc = (struct rtnl_tc *) cls;
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 ARG_PROTO,
83 ARG_PRIO,
84 };
85 static struct option long_opts[] = {
86 { "details", 0, 0, ARG_DETAILS },
87 { "stats", 0, 0, ARG_STATS },
88 { "help", 0, 0, 'h' },
89 { "version", 0, 0, 'v' },
90 { "dev", 1, 0, 'd' },
91 { "parent", 1, 0, 'p' },
92 { "id", 1, 0, 'i' },
93 { "kind", 1, 0, 'k' },
94 { "proto", 1, 0, ARG_PROTO },
95 { "prio", 1, 0, ARG_PRIO },
96 { 0, 0, 0, 0 }
97 };
98
99 c = getopt_long(argc, argv, "hvd:p:i:k:", long_opts, &optidx);
100 if (c == -1)
101 break;
102
103 switch (c) {
104 case ARG_DETAILS: params.dp_type = NL_DUMP_DETAILS; break;
105 case ARG_STATS: params.dp_type = NL_DUMP_STATS; break;
106 case 'h': print_usage(); break;
107 case 'v': nl_cli_print_version(); break;
108 case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
109 case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
110 case 'i': nl_cli_tc_parse_handle(tc, optarg, 0); break;
111 case 'k': nl_cli_tc_parse_kind(tc, optarg); break;
112 case ARG_PROTO: nl_cli_cls_parse_proto(cls, optarg); break;
113 case ARG_PRIO:
114 rtnl_cls_set_prio(cls, nl_cli_parse_u32(optarg));
115 break;
116 }
117 }
118
119 if ((ifindex = rtnl_tc_get_ifindex(tc)))
120 __dump_link(ifindex, cls);
121 else
122 nl_cache_foreach(link_cache, dump_link, cls);
123
124 return 0;
125}
void nl_cache_foreach(struct nl_cache *cache, void(*cb)(struct nl_object *, void *), void *arg)
Call a callback on each element of the cache.
Definition: cache.c:1277
void nl_cache_free(struct nl_cache *cache)
Free a cache.
Definition: cache.c:403
void nl_cache_dump_filter(struct nl_cache *cache, struct nl_dump_params *params, struct nl_object *filter)
Dump all elements of a cache (filtered).
Definition: cache.c:1211
uint32_t nl_cli_parse_u32(const char *arg)
Parse a text based 32 bit unsigned integer argument.
Definition: utils.c:36
uint32_t rtnl_tc_get_parent(struct rtnl_tc *tc)
Return parent identifier of a traffic control object.
Definition: tc.c:511
int rtnl_tc_get_ifindex(struct rtnl_tc *tc)
Return interface index of traffic control object.
Definition: tc.c:287
@ NL_DUMP_STATS
Dump all attributes including statistics.
Definition: types.h:18
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:16
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition: types.h:17
Dumping parameters.
Definition: types.h:28
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:32
FILE * dp_fd
File descriptor the dumping output should go to.
Definition: types.h:77