libnl 3.5.0

nl-class-delete.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * src/nl-class-delete.c Delete Traffic Classes
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) 2010 Thomas Graf <tgraf@suug.ch>
11 */
12
13 #include <netlink/cli/utils.h>
14 #include <netlink/cli/class.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
22 static void print_usage(void)
23 {
24 printf(
25 "Usage: nl-class-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 class 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 class (e.g. pfifo_fast)\n"
38 "\n"
39 "EXAMPLE\n"
40 " # Delete all classes on eth0 attached to parent 1:\n"
41 " $ nl-class-delete --dev eth0 --parent 1:\n"
42 "\n"
43 );
44
45 exit(0);
46 }
47
48 static void delete_cb(struct nl_object *obj, void *arg)
49 {
50 struct rtnl_class *class = nl_object_priv(obj);
51 struct nl_dump_params params = {
52 .dp_type = NL_DUMP_LINE,
53 .dp_fd = stdout,
54 };
55 int err;
56
57 if (interactive && !nl_cli_confirm(obj, &params, default_yes))
58 return;
59
60 if ((err = rtnl_class_delete(sock, class)) < 0)
61 nl_cli_fatal(err, "Unable to delete class: %s\n", nl_geterror(err));
62
63 if (!quiet) {
64 printf("Deleted ");
65 nl_object_dump(obj, &params);
66 }
67
68 deleted++;
69 }
70
71 int main(int argc, char *argv[])
72 {
73 struct rtnl_class *class;
74 struct rtnl_tc *tc;
75 struct nl_cache *link_cache, *class_cache;
76
77 sock = nl_cli_alloc_socket();
78 nl_cli_connect(sock, NETLINK_ROUTE);
79 link_cache = nl_cli_link_alloc_cache(sock);
80 class = nl_cli_class_alloc();
81 tc = (struct rtnl_tc *) class;
82
83 for (;;) {
84 int c, optidx = 0;
85 enum {
86 ARG_YES = 257,
87 ARG_INTERACTIVE = 258,
88 };
89 static struct option long_opts[] = {
90 { "interactive", 0, 0, ARG_INTERACTIVE },
91 { "yes", 0, 0, ARG_YES },
92 { "quiet", 0, 0, 'q' },
93 { "help", 0, 0, 'h' },
94 { "version", 0, 0, 'v' },
95 { "dev", 1, 0, 'd' },
96 { "parent", 1, 0, 'p' },
97 { "id", 1, 0, 'i' },
98 { "kind", 1, 0, 'k' },
99 { 0, 0, 0, 0 }
100 };
101
102 c = getopt_long(argc, argv, "qhvd:p:i:k:", long_opts, &optidx);
103 if (c == -1)
104 break;
105
106 switch (c) {
107 case '?': nl_cli_fatal(EINVAL, "Invalid options");
108 case ARG_INTERACTIVE: interactive = 1; break;
109 case ARG_YES: default_yes = 1; break;
110 case 'q': quiet = 1; break;
111 case 'h': print_usage(); break;
112 case 'v': nl_cli_print_version(); break;
113 case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
114 case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
115 case 'i': nl_cli_tc_parse_handle(tc, optarg, 0); break;
116 case 'k': nl_cli_tc_parse_kind(tc, optarg); break;
117 }
118 }
119
120 if (!rtnl_tc_get_ifindex(tc))
121 nl_cli_fatal(EINVAL, "You must specify a network device (--dev=XXX)");
122
123 class_cache = nl_cli_class_alloc_cache(sock, rtnl_tc_get_ifindex(tc));
124
125 nl_cache_foreach_filter(class_cache, OBJ_CAST(class), delete_cb, NULL);
126
127 if (!quiet)
128 printf("Deleted %d classs\n", deleted);
129
130 return 0;
131 }
NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:22
nl_dump_params::dp_type
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:38
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:1283
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:289
nl_cli_fatal
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:78
nl_dump_params
Dumping parameters.
Definition: types.h:33
rtnl_class_delete
int rtnl_class_delete(struct nl_sock *sk, struct rtnl_class *class)
Delete traffic class.
Definition: class.c:253
rtnl_tc_get_ifindex
int rtnl_tc_get_ifindex(struct rtnl_tc *tc)
Return interface index of traffic control object.
Definition: tc.c:294