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