libnl 3.7.0
nl-class-delete.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/class.h>
8#include <netlink/cli/link.h>
9
10#include <linux/netlink.h>
11
12static int quiet = 0, default_yes = 0, deleted = 0, interactive = 0;
13static struct nl_sock *sock;
14
15static void print_usage(void)
16{
17 printf(
18 "Usage: nl-class-delete [OPTION]... [class]\n"
19 "\n"
20 "OPTIONS\n"
21 " --interactive Run interactively.\n"
22 " --yes Set default answer to yes.\n"
23 " -q, --quiet Do not print informal notifications.\n"
24 " -h, --help Show this help text and exit.\n"
25 " -v, --version Show versioning information and exit.\n"
26 "\n"
27 " -d, --dev=DEV Device the class is attached to.\n"
28 " -p, --parent=ID Identifier of parent qdisc/class.\n"
29 " -i, --id=ID Identifier\n"
30 " -k, --kind=NAME Kind of class (e.g. pfifo_fast)\n"
31 "\n"
32 "EXAMPLE\n"
33 " # Delete all classes on eth0 attached to parent 1:\n"
34 " $ nl-class-delete --dev eth0 --parent 1:\n"
35 "\n"
36 );
37
38 exit(0);
39}
40
41static void delete_cb(struct nl_object *obj, void *arg)
42{
43 struct rtnl_class *class = nl_object_priv(obj);
44 struct nl_dump_params params = {
46 .dp_fd = stdout,
47 };
48 int err;
49
50 if (interactive && !nl_cli_confirm(obj, &params, default_yes))
51 return;
52
53 if ((err = rtnl_class_delete(sock, class)) < 0)
54 nl_cli_fatal(err, "Unable to delete class: %s\n", nl_geterror(err));
55
56 if (!quiet) {
57 printf("Deleted ");
58 nl_object_dump(obj, &params);
59 }
60
61 deleted++;
62}
63
64int main(int argc, char *argv[])
65{
66 struct rtnl_class *class;
67 struct rtnl_tc *tc;
68 struct nl_cache *link_cache, *class_cache;
69
70 sock = nl_cli_alloc_socket();
71 nl_cli_connect(sock, NETLINK_ROUTE);
72 link_cache = nl_cli_link_alloc_cache(sock);
73 class = nl_cli_class_alloc();
74 tc = (struct rtnl_tc *) class;
75
76 for (;;) {
77 int c, optidx = 0;
78 enum {
79 ARG_YES = 257,
80 ARG_INTERACTIVE = 258,
81 };
82 static struct option long_opts[] = {
83 { "interactive", 0, 0, ARG_INTERACTIVE },
84 { "yes", 0, 0, ARG_YES },
85 { "quiet", 0, 0, 'q' },
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, "qhvd:p:i:k:", long_opts, &optidx);
96 if (c == -1)
97 break;
98
99 switch (c) {
100 case '?': nl_cli_fatal(EINVAL, "Invalid options");
101 case ARG_INTERACTIVE: interactive = 1; break;
102 case ARG_YES: default_yes = 1; break;
103 case 'q': quiet = 1; break;
104 case 'h': print_usage(); break;
105 case 'v': nl_cli_print_version(); break;
106 case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
107 case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
108 case 'i': nl_cli_tc_parse_handle(tc, optarg, 0); break;
109 case 'k': nl_cli_tc_parse_kind(tc, optarg); break;
110 }
111 }
112
113 if (!rtnl_tc_get_ifindex(tc))
114 nl_cli_fatal(EINVAL, "You must specify a network device (--dev=XXX)");
115
116 class_cache = nl_cli_class_alloc_cache(sock, rtnl_tc_get_ifindex(tc));
117
118 nl_cache_foreach_filter(class_cache, OBJ_CAST(class), delete_cb, NULL);
119
120 if (!quiet)
121 printf("Deleted %d classs\n", deleted);
122
123 return 0;
124}
void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter, void(*cb)(struct nl_object *, void *), void *arg)
Call a callback on each element of the cache (filtered).
Definition: cache.c:1294
int rtnl_class_delete(struct nl_sock *sk, struct rtnl_class *class)
Delete traffic class.
Definition: class.c:246
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:71
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition: object.c:287
int rtnl_tc_get_ifindex(struct rtnl_tc *tc)
Return interface index of traffic control object.
Definition: tc.c:287
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:16
Dumping parameters.
Definition: types.h:28
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:32