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