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