libnl: /data/src/libnl3/src/nl-class-list.c Source File

libnl 3.7.0

nl-class-list.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2010 Thomas Graf <tgraf@suug.ch>
4 */
5
6 #include <netlink/cli/utils.h>
7 #include <netlink/cli/tc.h>
8 #include <netlink/cli/class.h>
9 #include <netlink/cli/link.h>
10
11 #include <linux/netlink.h>
12
13 static struct nl_sock *sock;
14
15 static struct nl_dump_params params = {
16 .dp_type = NL_DUMP_LINE,
17 };
18
19 static void print_usage(void)
20 {
21 printf(
22 "Usage: nl-class-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 class 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 class (e.g. pfifo_fast)\n"
34 "\n"
35 "EXAMPLE\n"
36 " # Display statistics of all classes on eth0\n"
37 " $ nl-class-list --stats --dev=eth0\n"
38 "\n"
39 );
40 exit(0);
41 }
42
43 static void __dump_class(int ifindex, struct rtnl_class *filter)
44 {
45 struct nl_cache *cache;
46
47 cache = nl_cli_class_alloc_cache(sock, ifindex);
48 nl_cache_dump_filter(cache, &params, OBJ_CAST(filter));
49 }
50
51 static void dump_class(struct nl_object *obj, void *arg)
52 {
53 struct rtnl_link *link = nl_object_priv(obj);
54
55 __dump_class(rtnl_link_get_ifindex(link), arg);
56 }
57
58 int main(int argc, char *argv[])
59 {
60 struct rtnl_class *class;
61 struct rtnl_tc *tc;
62 struct nl_cache *link_cache;
63 int ifindex;
64
65 sock = nl_cli_alloc_socket();
66 nl_cli_connect(sock, NETLINK_ROUTE);
67 link_cache = nl_cli_link_alloc_cache(sock);
68 class = nl_cli_class_alloc();
69 tc = (struct rtnl_tc *) class;
70
71 params.dp_fd = stdout;
72
73 for (;;) {
74 int c, optidx = 0;
75 enum {
76 ARG_DETAILS = 257,
77 ARG_STATS = 258,
78 };
79 static struct option long_opts[] = {
80 { "details", 0, 0, ARG_DETAILS },
81 { "stats", 0, 0, ARG_STATS },
82 { "help", 0, 0, 'h' },
83 { "version", 0, 0, 'v' },
84 { "dev", 1, 0, 'd' },
85 { "parent", 1, 0, 'p' },
86 { "id", 1, 0, 'i' },
87 { "kind", 1, 0, 'k' },
88 { 0, 0, 0, 0 }
89 };
90
91 c = getopt_long(argc, argv, "hvd:p:i:k:", long_opts, &optidx);
92 if (c == -1)
93 break;
94
95 switch (c) {
96 case ARG_DETAILS: params.dp_type = NL_DUMP_DETAILS; break;
97 case ARG_STATS: params.dp_type = NL_DUMP_STATS; break;
98 case 'h': print_usage(); break;
99 case 'v': nl_cli_print_version(); break;
100 case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
101 case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
102 case 'i': nl_cli_tc_parse_handle(tc, optarg, 0); break;
103 case 'k': nl_cli_tc_parse_kind(tc, optarg); break;
104 }
105 }
106
107 if ((ifindex = rtnl_tc_get_ifindex(tc)))
108 __dump_class(ifindex, class);
109 else
110 nl_cache_foreach(link_cache, dump_class, class);
111
112 return 0;
113 }
nl_cache_foreach
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
nl_cache_dump_filter
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
rtnl_link_get_ifindex
int rtnl_link_get_ifindex(struct rtnl_link *link)
Return interface index of link object.
Definition: link.c:2182
rtnl_tc_get_ifindex
int rtnl_tc_get_ifindex(struct rtnl_tc *tc)
Return interface index of traffic control object.
Definition: tc.c:287
NL_DUMP_STATS
@ NL_DUMP_STATS
Dump all attributes including statistics.
Definition: types.h:18
NL_DUMP_LINE
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:16
NL_DUMP_DETAILS
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition: types.h:17
nl_dump_params
Dumping parameters.
Definition: types.h:28
nl_dump_params::dp_type
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:32
nl_dump_params::dp_fd
FILE * dp_fd
File descriptor the dumping output should go to.
Definition: types.h:77
rtnl_link
Link object.