libnl: /data/src/libnl3/src/nl-qdisc-delete.c Source File

libnl 3.7.0

nl-qdisc-delete.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2003-2010 Thomas Graf <tgraf@suug.ch>
4 */
5
6 #include <netlink/cli/utils.h>
7 #include <netlink/cli/tc.h>
8 #include <netlink/cli/qdisc.h>
9 #include <netlink/cli/link.h>
10
11 #include <linux/netlink.h>
12
13 static int quiet = 0, default_yes = 0, deleted = 0, interactive = 0;
14 static struct nl_sock *sock;
15
16 static void print_usage(void)
17 {
18 printf(
19 "Usage: nl-qdisc-delete [OPTION]... [QDISC]\n"
20 "\n"
21 "OPTIONS\n"
22 " --interactive Run interactively.\n"
23 " --yes Set default answer to yes.\n"
24 " -q, --quiet Do not print informal notifications.\n"
25 " -h, --help Show this help text and exit.\n"
26 " -v, --version Show versioning information and exit.\n"
27 "\n"
28 " -d, --dev=DEV Device the qdisc is attached to.\n"
29 " -p, --parent=ID Identifier of parent qdisc/class.\n"
30 " -i, --id=ID Identifier\n"
31 " -k, --kind=NAME Kind of qdisc (e.g. pfifo_fast)\n"
32 );
33
34 exit(0);
35 }
36
37 static void delete_cb(struct nl_object *obj, void *arg)
38 {
39 struct rtnl_qdisc *qdisc = nl_object_priv(obj);
40 struct nl_dump_params params = {
41 .dp_type = NL_DUMP_LINE,
42 .dp_fd = stdout,
43 };
44 int err;
45
46 /* Ignore default qdiscs, unable to delete */
47 if (rtnl_tc_get_handle((struct rtnl_tc *) qdisc) == 0)
48 return;
49
50 if (interactive && !nl_cli_confirm(obj, &params, default_yes))
51 return;
52
53 if ((err = rtnl_qdisc_delete(sock, qdisc)) < 0)
54 nl_cli_fatal(err, "Unable to delete qdisc: %s\n", nl_geterror(err));
55
56 if (!quiet) {
57 printf("Deleted ");
58 nl_object_dump(obj, &params);
59 }
60
61 deleted++;
62 }
63
64 int main(int argc, char *argv[])
65 {
66 struct rtnl_qdisc *qdisc;
67 struct rtnl_tc *tc;
68 struct nl_cache *link_cache, *qdisc_cache;
69 int nfilter = 0;
70
71 sock = nl_cli_alloc_socket();
72 nl_cli_connect(sock, NETLINK_ROUTE);
73 link_cache = nl_cli_link_alloc_cache(sock);
74 qdisc_cache = nl_cli_qdisc_alloc_cache(sock);
75 qdisc = nl_cli_qdisc_alloc();
76 tc = (struct rtnl_tc *) qdisc;
77
78 for (;;) {
79 int c, optidx = 0;
80 enum {
81 ARG_YES = 257,
82 ARG_INTERACTIVE = 258,
83 };
84 static struct option long_opts[] = {
85 { "interactive", 0, 0, ARG_INTERACTIVE },
86 { "yes", 0, 0, ARG_YES },
87 { "quiet", 0, 0, 'q' },
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 { 0, 0, 0, 0 }
95 };
96
97 c = getopt_long(argc, argv, "qhvd:p:i:k:", long_opts, &optidx);
98 if (c == -1)
99 break;
100
101 switch (c) {
102 case '?': nl_cli_fatal(EINVAL, "Invalid options");
103 case ARG_INTERACTIVE: interactive = 1; break;
104 case ARG_YES: default_yes = 1; break;
105 case 'q': quiet = 1; break;
106 case 'h': print_usage(); break;
107 case 'v': nl_cli_print_version(); break;
108 case 'd':
109 nfilter++;
110 nl_cli_tc_parse_dev(tc, link_cache, optarg);
111 break;
112 case 'p':
113 nfilter++;
114 nl_cli_tc_parse_parent(tc, optarg);
115 break;
116 case 'i':
117 nfilter++;
118 nl_cli_tc_parse_handle(tc, optarg, 0);
119 break;
120 case 'k':
121 nfilter++;
122 nl_cli_tc_parse_kind(tc, optarg);
123 break;
124 }
125 }
126
127 if (nfilter == 0 && !interactive && !default_yes) {
128 nl_cli_fatal(EINVAL,
129 "You are attempting to delete all qdiscs on all devices.\n"
130 "If you want to proceed, run nl-qdisc-delete --yes.\n"
131 "Aborting...");
132 }
133
134 nl_cache_foreach_filter(qdisc_cache, OBJ_CAST(qdisc), delete_cb, NULL);
135
136 if (!quiet)
137 printf("Deleted %d qdiscs\n", deleted);
138
139 return 0;
140 }
nl_cache_foreach_filter
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
nl_cli_fatal
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:71
nl_object_dump
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
rtnl_qdisc_delete
int rtnl_qdisc_delete(struct nl_sock *sk, struct rtnl_qdisc *qdisc)
Delete qdisc.
Definition: qdisc.c:334
rtnl_tc_get_handle
uint32_t rtnl_tc_get_handle(struct rtnl_tc *tc)
Return identifier of a traffic control object.
Definition: tc.c:490
NL_DUMP_LINE
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:16
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