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
print_usage(void)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
delete_cb(struct nl_object * obj,void * arg)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, ¶ms, 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, ¶ms);
66 }
67
68 deleted++;
69 }
70
main(int argc,char * argv[])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 }
132