1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * Copyright (c) 2010 Thomas Graf <[email protected]>
4 */
5
6 #include "nl-default.h"
7
8 #include <linux/netlink.h>
9
10 #include <netlink/cli/utils.h>
11 #include <netlink/cli/class.h>
12 #include <netlink/cli/link.h>
13
14 static int quiet = 0, default_yes = 0, deleted = 0, interactive = 0;
15 static struct nl_sock *sock;
16
print_usage(void)17 static void print_usage(void)
18 {
19 printf(
20 "Usage: nl-class-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 class 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 class (e.g. pfifo_fast)\n"
33 "\n"
34 "EXAMPLE\n"
35 " # Delete all classes on eth0 attached to parent 1:\n"
36 " $ nl-class-delete --dev eth0 --parent 1:\n"
37 "\n"
38 );
39
40 exit(0);
41 }
42
delete_cb(struct nl_object * obj,void * arg)43 static void delete_cb(struct nl_object *obj, void *arg)
44 {
45 struct rtnl_class *class = 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, ¶ms, default_yes))
53 return;
54
55 if ((err = rtnl_class_delete(sock, class)) < 0)
56 nl_cli_fatal(err, "Unable to delete class: %s\n", nl_geterror(err));
57
58 if (!quiet) {
59 printf("Deleted ");
60 nl_object_dump(obj, ¶ms);
61 }
62
63 deleted++;
64 }
65
main(int argc,char * argv[])66 int main(int argc, char *argv[])
67 {
68 struct rtnl_class *class;
69 struct rtnl_tc *tc;
70 struct nl_cache *link_cache, *class_cache;
71
72 sock = nl_cli_alloc_socket();
73 nl_cli_connect(sock, NETLINK_ROUTE);
74 link_cache = nl_cli_link_alloc_cache(sock);
75 class = nl_cli_class_alloc();
76 tc = (struct rtnl_tc *) class;
77
78 for (;;) {
79 int c, optidx = 0;
80 enum {
81 ARG_YES = 257,
82 ARG_INTERACTIVE = 258,
83 };
84 static struct option long_opts[] = {
85 { "interactive", 0, 0, ARG_INTERACTIVE },
86 { "yes", 0, 0, ARG_YES },
87 { "quiet", 0, 0, 'q' },
88 { "help", 0, 0, 'h' },
89 { "version", 0, 0, 'v' },
90 { "dev", 1, 0, 'd' },
91 { "parent", 1, 0, 'p' },
92 { "id", 1, 0, 'i' },
93 { "kind", 1, 0, 'k' },
94 { 0, 0, 0, 0 }
95 };
96
97 c = getopt_long(argc, argv, "qhvd:p:i:k:", long_opts, &optidx);
98 if (c == -1)
99 break;
100
101 switch (c) {
102 case '?': nl_cli_fatal(EINVAL, "Invalid options");
103 case ARG_INTERACTIVE: interactive = 1; break;
104 case ARG_YES: default_yes = 1; break;
105 case 'q': quiet = 1; break;
106 case 'h': print_usage(); break;
107 case 'v': nl_cli_print_version(); break;
108 case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
109 case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
110 case 'i': nl_cli_tc_parse_handle(tc, optarg, 0); break;
111 case 'k': nl_cli_tc_parse_kind(tc, optarg); break;
112 }
113 }
114
115 if (!rtnl_tc_get_ifindex(tc))
116 nl_cli_fatal(EINVAL, "You must specify a network device (--dev=XXX)");
117
118 class_cache = nl_cli_class_alloc_cache(sock, rtnl_tc_get_ifindex(tc));
119
120 nl_cache_foreach_filter(class_cache, OBJ_CAST(class), delete_cb, NULL);
121
122 if (!quiet)
123 printf("Deleted %d classs\n", deleted);
124
125 return 0;
126 }
127