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

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