• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * src/nl-cls-delete.c     Delete Classifier
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) 2008-2010 Thomas Graf <tgraf@suug.ch>
11  */
12 
13 #include <netlink/cli/utils.h>
14 #include <netlink/cli/cls.h>
15 #include <netlink/cli/link.h>
16 
17 #include <linux/netlink.h>
18 
19 static int quiet = 0, default_yes = 0, deleted = 0, interactive = 0;
20 static struct nl_sock *sock;
21 
print_usage(void)22 static void print_usage(void)
23 {
24 	printf(
25 "Usage: nl-cls-delete [OPTION]... [class]\n"
26 "\n"
27 "OPTIONS\n"
28 "     --interactive         Run interactively.\n"
29 "     --yes                 Set default answer to yes.\n"
30 " -q, --quiet               Do not print informal notifications.\n"
31 " -h, --help                Show this help text and exit.\n"
32 " -v, --version             Show versioning information and exit.\n"
33 "\n"
34 " -d, --dev=DEV             Device the classifer is attached to.\n"
35 " -p, --parent=ID           Identifier of parent qdisc/class.\n"
36 " -i, --id=ID               Identifier\n"
37 " -k, --kind=NAME           Kind of classifier (e.g. basic, u32, fw)\n"
38 "     --proto=PROTO         Protocol to match (default: all)\n"
39 "     --prio=PRIO           Priority (default: 0)\n"
40 "\n"
41 "EXAMPLE\n"
42 "    # Delete all classifiers on eth0 attached to parent q_root:\n"
43 "    $ nl-cls-delete --dev eth0 --parent q_root:\n"
44 "\n"
45 	);
46 
47 	exit(0);
48 }
49 
delete_cb(struct nl_object * obj,void * arg)50 static void delete_cb(struct nl_object *obj, void *arg)
51 {
52 	struct rtnl_cls *cls = nl_object_priv(obj);
53 	struct nl_dump_params params = {
54 		.dp_type = NL_DUMP_LINE,
55 		.dp_fd = stdout,
56 	};
57 	int err;
58 
59 	if (interactive && !nl_cli_confirm(obj, &params, default_yes))
60 		return;
61 
62 	if ((err = rtnl_cls_delete(sock, cls, 0)) < 0)
63 		nl_cli_fatal(err, "Unable to delete classifier: %s\n",
64 				nl_geterror(err));
65 
66 	if (!quiet) {
67 		printf("Deleted ");
68 		nl_object_dump(obj, &params);
69 	}
70 
71 	deleted++;
72 }
73 
__delete_link(int ifindex,struct rtnl_cls * filter)74 static void __delete_link(int ifindex, struct rtnl_cls *filter)
75 {
76 	struct nl_cache *cache;
77 	uint32_t parent = rtnl_tc_get_parent((struct rtnl_tc *) filter);
78 
79 	cache = nl_cli_cls_alloc_cache(sock, ifindex, parent);
80 	nl_cache_foreach_filter(cache, OBJ_CAST(filter), delete_cb, NULL);
81 	nl_cache_free(cache);
82 }
83 
delete_link(struct nl_object * obj,void * arg)84 static void delete_link(struct nl_object *obj, void *arg)
85 {
86 	struct rtnl_link *link = nl_object_priv(obj);
87 
88 	__delete_link(rtnl_link_get_ifindex(link), arg);
89 }
90 
main(int argc,char * argv[])91 int main(int argc, char *argv[])
92 {
93 	struct rtnl_cls *cls;
94 	struct rtnl_tc *tc;
95 	struct nl_cache *link_cache;
96 	int ifindex;
97 
98 	sock = nl_cli_alloc_socket();
99 	nl_cli_connect(sock, NETLINK_ROUTE);
100 	link_cache = nl_cli_link_alloc_cache(sock);
101 	cls = nl_cli_cls_alloc();
102 	tc = (struct rtnl_tc *) cls;
103 
104 	for (;;) {
105 		int c, optidx = 0;
106 		enum {
107 			ARG_YES = 257,
108 			ARG_INTERACTIVE = 258,
109 			ARG_PROTO,
110 			ARG_PRIO,
111 		};
112 		static struct option long_opts[] = {
113 			{ "interactive", 0, 0, ARG_INTERACTIVE },
114 			{ "yes", 0, 0, ARG_YES },
115 			{ "quiet", 0, 0, 'q' },
116 			{ "help", 0, 0, 'h' },
117 			{ "version", 0, 0, 'v' },
118 			{ "dev", 1, 0, 'd' },
119 			{ "parent", 1, 0, 'p' },
120 			{ "id", 1, 0, 'i' },
121 			{ "kind", 1, 0, 'k' },
122 			{ "proto", 1, 0, ARG_PROTO },
123 			{ "prio", 1, 0, ARG_PRIO },
124 			{ 0, 0, 0, 0 }
125 		};
126 
127 		c = getopt_long(argc, argv, "qhvd:p:i:k:", long_opts, &optidx);
128 		if (c == -1)
129 			break;
130 
131 		switch (c) {
132 		case '?': nl_cli_fatal(EINVAL, "Invalid options");
133 		case ARG_INTERACTIVE: interactive = 1; break;
134 		case ARG_YES: default_yes = 1; break;
135 		case 'q': quiet = 1; break;
136 		case 'h': print_usage(); break;
137 		case 'v': nl_cli_print_version(); break;
138 		case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
139 		case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
140 		case 'i': nl_cli_tc_parse_handle(tc, optarg, 0); break;
141 		case 'k': nl_cli_tc_parse_kind(tc, optarg); break;
142 		case ARG_PROTO: nl_cli_cls_parse_proto(cls, optarg); break;
143 		case ARG_PRIO:
144 			rtnl_cls_set_prio(cls, nl_cli_parse_u32(optarg));
145 			break;
146 		}
147 	}
148 
149 	if ((ifindex = rtnl_tc_get_ifindex(tc)))
150 		__delete_link(ifindex, cls);
151 	else
152 		nl_cache_foreach(link_cache, delete_link, cls);
153 
154 	if (!quiet)
155 		printf("Deleted %d classs\n", deleted);
156 
157 	return 0;
158 }
159