1/*
2 * src/nl-cls-delete.c     Delete Classifier
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) 2008 Thomas Graf <tgraf@suug.ch>
10 */
11
12#include "cls/utils.h"
13
14static int interactive = 0, default_yes = 0, quiet = 0;
15static int deleted = 0;
16static struct nl_sock *sock;
17
18static void print_usage(void)
19{
20	printf(
21	"Usage: nl-cls-list [OPTION]... [CLASSIFIER]\n"
22	"\n"
23	"Options\n"
24	" -i, --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\n"
28	" -v, --version		Show versioning information\n"
29	"\n"
30	"Classifier Options\n"
31	" -d, --dev=DEV         Device the classifier should be assigned to.\n"
32	" -p, --parent=HANDLE   Parent qdisc/class\n"
33	"     --proto=PROTO     Protocol\n"
34	"     --prio=NUM        Priority (0..256)\n"
35	"     --id=HANDLE       Unique identifier\n"
36	);
37	exit(0);
38}
39
40static void delete_cb(struct nl_object *obj, void *arg)
41{
42	struct rtnl_cls *cls = (struct rtnl_cls *) obj;
43	struct nl_dump_params params = {
44		.dp_type = NL_DUMP_LINE,
45		.dp_fd = stdout,
46	};
47	int err;
48
49	if (interactive && !nlt_confirm(obj, &params, default_yes))
50		return;
51
52	if ((err = rtnl_cls_delete(sock, cls, 0)) < 0)
53		fatal(err, "Unable to delete classifier: %s",
54		      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 nl_cache *link_cache, *cls_cache;
67	struct rtnl_cls *cls;
68	int nf = 0, err;
69
70	sock = nlt_alloc_socket();
71	nlt_connect(sock, NETLINK_ROUTE);
72	link_cache = nlt_alloc_link_cache(sock);
73	cls = nlt_alloc_cls();
74
75	for (;;) {
76		int c, optidx = 0;
77		enum {
78			ARG_PRIO = 257,
79			ARG_PROTO = 258,
80			ARG_ID,
81			ARG_YES,
82		};
83		static struct option long_opts[] = {
84			{ "interactive", 0, 0, 'i' },
85			{ "yes", 0, 0, ARG_YES },
86			{ "quiet", 0, 0, 'q' },
87			{ "help", 0, 0, 'h' },
88			{ "version", 0, 0, 'v' },
89			{ "dev", 1, 0, 'd' },
90			{ "parent", 1, 0, 'p' },
91			{ "proto", 1, 0, ARG_PROTO },
92			{ "prio", 1, 0, ARG_PRIO },
93			{ "id", 1, 0, ARG_ID },
94			{ 0, 0, 0, 0 }
95		};
96
97		c = getopt_long(argc, argv, "iqhvd:p:", long_opts, &optidx);
98		if (c == -1)
99			break;
100
101		switch (c) {
102		case 'i': interactive = 1; break;
103		case ARG_YES: default_yes = 1; break;
104		case 'q': quiet = 1; break;
105		case 'h': print_usage(); break;
106		case 'v': nlt_print_version(); break;
107		case 'd': nf++; parse_dev(cls, link_cache, optarg); break;
108		case 'p': nf++; parse_parent(cls, optarg); break;
109		case ARG_PRIO: nf++; parse_prio(cls, optarg); break;
110		case ARG_ID: nf++; parse_handle(cls, optarg); break;
111		case ARG_PROTO: nf++; parse_proto(cls, optarg); break;
112		}
113	}
114
115	if (nf == 0 && !interactive && !default_yes) {
116		fprintf(stderr, "You attempted to delete all classifiers in "
117			"non-interactive mode, aborting.\n");
118		exit(0);
119	}
120
121	err = rtnl_cls_alloc_cache(sock, rtnl_cls_get_ifindex(cls),
122				   rtnl_cls_get_parent(cls), &cls_cache);
123	if (err < 0)
124		fatal(err, "Unable to allocate classifier cache: %s",
125		      nl_geterror(err));
126
127	nl_cache_foreach_filter(cls_cache, OBJ_CAST(cls), delete_cb, NULL);
128
129	if (!quiet)
130		printf("Deleted %d classifiers\n", deleted);
131
132	return 0;
133}
134