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

libnl 3.12.0

nl-class-delete.c
1/* SPDX-License-Identifier: LGPL-2.1-only */
2/*
3 * Copyright (c) 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/class.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-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
43static 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, &params, 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, &params);
61 }
62
63 deleted++;
64}
65
66int 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}
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
rtnl_class_delete
int rtnl_class_delete(struct nl_sock *sk, struct rtnl_class *class)
Delete traffic class.
Definition class.c:252
nl_cli_fatal
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition utils.c:71
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_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
rtnl_class
Definition class.c:23
rtnl_tc
Definition nl-route.h:115