2 * src/nl-cls-delete.c Delete Classifier
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
9 * Copyright (c) 2008-2010 Thomas Graf <tgraf@suug.ch>
12 #include <netlink/cli/utils.h>
13 #include <netlink/cli/cls.h>
14 #include <netlink/cli/link.h>
16 static int quiet = 0, default_yes = 0, deleted = 0, interactive = 0;
17 static struct nl_sock *sock;
19 static void print_usage(void)
22 "Usage: nl-cls-delete [OPTION]... [class]\n"
25 " --interactive Run interactively.\n"
26 " --yes Set default answer to yes.\n"
27 " -q, --quiet Do not print informal notifications.\n"
28 " -h, --help Show this help text and exit.\n"
29 " -v, --version Show versioning information and exit.\n"
31 " -d, --dev=DEV Device the classifer is attached to.\n"
32 " -p, --parent=ID Identifier of parent qdisc/class.\n"
33 " -i, --id=ID Identifier\n"
34 " -k, --kind=NAME Kind of classifier (e.g. basic, u32, fw)\n"
35 " --protocol=PROTO Protocol to match (default: all)\n"
36 " --prio=PRIO Priority (default: 0)\n"
39 " # Delete all classifiers on eth0 attached to parent q_root:\n"
40 " $ nl-cls-delete --dev eth0 --parent q_root:\n"
47 static void delete_cb(struct nl_object *obj, void *arg)
49 struct rtnl_cls *cls = nl_object_priv(obj);
50 struct nl_dump_params params = {
51 .dp_type = NL_DUMP_LINE,
56 if (interactive && !nl_cli_confirm(obj, ¶ms, default_yes))
59 if ((err = rtnl_cls_delete(sock, cls, 0)) < 0)
60 nl_cli_fatal(err, "Unable to delete classifier: %s\n",
65 nl_object_dump(obj, ¶ms);
71 static void __delete_link(int ifindex, struct rtnl_cls *filter)
73 struct nl_cache *cache;
74 uint32_t parent = rtnl_tc_get_parent((struct rtnl_tc *) filter);
76 cache = nl_cli_cls_alloc_cache(sock, ifindex, parent);
77 nl_cache_foreach_filter(cache, OBJ_CAST(filter), delete_cb, NULL);
81 static void delete_link(struct nl_object *obj, void *arg)
83 struct rtnl_link *link = nl_object_priv(obj);
85 __delete_link(rtnl_link_get_ifindex(link), arg);
88 int main(int argc, char *argv[])
92 struct nl_cache *link_cache;
95 sock = nl_cli_alloc_socket();
96 nl_cli_connect(sock, NETLINK_ROUTE);
97 link_cache = nl_cli_link_alloc_cache(sock);
98 cls = nl_cli_cls_alloc();
99 tc = (struct rtnl_tc *) cls;
105 ARG_INTERACTIVE = 258,
109 static struct option long_opts[] = {
110 { "interactive", 0, 0, ARG_INTERACTIVE },
111 { "yes", 0, 0, ARG_YES },
112 { "quiet", 0, 0, 'q' },
113 { "help", 0, 0, 'h' },
114 { "version", 0, 0, 'v' },
115 { "dev", 1, 0, 'd' },
116 { "parent", 1, 0, 'p' },
118 { "kind", 1, 0, 'k' },
119 { "proto", 1, 0, ARG_PROTO },
120 { "prio", 1, 0, ARG_PRIO },
124 c = getopt_long(argc, argv, "qhvd:p:i:k:", long_opts, &optidx);
129 case '?': nl_cli_fatal(EINVAL, "Invalid options");
130 case ARG_INTERACTIVE: interactive = 1; break;
131 case ARG_YES: default_yes = 1; break;
132 case 'q': quiet = 1; break;
133 case 'h': print_usage(); break;
134 case 'v': nl_cli_print_version(); break;
135 case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
136 case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
137 case 'i': nl_cli_tc_parse_handle(tc, optarg, 0); break;
138 case 'k': nl_cli_tc_parse_kind(tc, optarg); break;
139 case ARG_PROTO: nl_cli_cls_parse_proto(cls, optarg); break;
141 rtnl_cls_set_prio(cls, nl_cli_parse_u32(optarg));
146 if ((ifindex = rtnl_tc_get_ifindex(tc)))
147 __delete_link(ifindex, cls);
149 nl_cache_foreach(link_cache, delete_link, cls);
152 printf("Deleted %d classs\n", deleted);