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

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