libnl: /data/src/libnl3/src/nl-cls-delete.c Source File

libnl 3.7.0

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