libnl  3.4.0
nl-qdisc-delete.c
1 /*
2  * src/nl-qdisc-delete.c Delete Queuing Disciplines
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation version 2.1
7  * of the License.
8  *
9  * Copyright (c) 2003-2010 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #include <netlink/cli/utils.h>
13 #include <netlink/cli/tc.h>
14 #include <netlink/cli/qdisc.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-qdisc-delete [OPTION]... [QDISC]\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 qdisc 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 qdisc (e.g. pfifo_fast)\n"
38  );
39 
40  exit(0);
41 }
42 
43 static void delete_cb(struct nl_object *obj, void *arg)
44 {
45  struct rtnl_qdisc *qdisc = nl_object_priv(obj);
46  struct nl_dump_params params = {
48  .dp_fd = stdout,
49  };
50  int err;
51 
52  /* Ignore default qdiscs, unable to delete */
53  if (rtnl_tc_get_handle((struct rtnl_tc *) qdisc) == 0)
54  return;
55 
56  if (interactive && !nl_cli_confirm(obj, &params, default_yes))
57  return;
58 
59  if ((err = rtnl_qdisc_delete(sock, qdisc)) < 0)
60  nl_cli_fatal(err, "Unable to delete qdisc: %s\n", nl_geterror(err));
61 
62  if (!quiet) {
63  printf("Deleted ");
64  nl_object_dump(obj, &params);
65  }
66 
67  deleted++;
68 }
69 
70 int main(int argc, char *argv[])
71 {
72  struct rtnl_qdisc *qdisc;
73  struct rtnl_tc *tc;
74  struct nl_cache *link_cache, *qdisc_cache;
75  int nfilter = 0;
76 
77  sock = nl_cli_alloc_socket();
78  nl_cli_connect(sock, NETLINK_ROUTE);
79  link_cache = nl_cli_link_alloc_cache(sock);
80  qdisc_cache = nl_cli_qdisc_alloc_cache(sock);
81  qdisc = nl_cli_qdisc_alloc();
82  tc = (struct rtnl_tc *) qdisc;
83 
84  for (;;) {
85  int c, optidx = 0;
86  enum {
87  ARG_YES = 257,
88  ARG_INTERACTIVE = 258,
89  };
90  static struct option long_opts[] = {
91  { "interactive", 0, 0, ARG_INTERACTIVE },
92  { "yes", 0, 0, ARG_YES },
93  { "quiet", 0, 0, 'q' },
94  { "help", 0, 0, 'h' },
95  { "version", 0, 0, 'v' },
96  { "dev", 1, 0, 'd' },
97  { "parent", 1, 0, 'p' },
98  { "id", 1, 0, 'i' },
99  { "kind", 1, 0, 'k' },
100  { 0, 0, 0, 0 }
101  };
102 
103  c = getopt_long(argc, argv, "qhvd:p:i:k:", long_opts, &optidx);
104  if (c == -1)
105  break;
106 
107  switch (c) {
108  case '?': nl_cli_fatal(EINVAL, "Invalid options");
109  case ARG_INTERACTIVE: interactive = 1; break;
110  case ARG_YES: default_yes = 1; break;
111  case 'q': quiet = 1; break;
112  case 'h': print_usage(); break;
113  case 'v': nl_cli_print_version(); break;
114  case 'd':
115  nfilter++;
116  nl_cli_tc_parse_dev(tc, link_cache, optarg);
117  break;
118  case 'p':
119  nfilter++;
120  nl_cli_tc_parse_parent(tc, optarg);
121  break;
122  case 'i':
123  nfilter++;
124  nl_cli_tc_parse_handle(tc, optarg, 0);
125  break;
126  case 'k':
127  nfilter++;
128  nl_cli_tc_parse_kind(tc, optarg);
129  break;
130  }
131  }
132 
133  if (nfilter == 0 && !interactive && !default_yes) {
134  nl_cli_fatal(EINVAL,
135  "You are attempting to delete all qdiscs on all devices.\n"
136  "If you want to proceed, run nl-qdisc-delete --yes.\n"
137  "Aborting...");
138  }
139 
140  nl_cache_foreach_filter(qdisc_cache, OBJ_CAST(qdisc), delete_cb, NULL);
141 
142  if (!quiet)
143  printf("Deleted %d qdiscs\n", deleted);
144 
145  return 0;
146 }
Dump object briefly on one line.
Definition: types.h:22
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:38
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:1282
uint32_t rtnl_tc_get_handle(struct rtnl_tc *tc)
Return identifier of a traffic control object.
Definition: tc.c:478
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition: object.c:288
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:77
Dumping parameters.
Definition: types.h:33
int rtnl_qdisc_delete(struct nl_sock *sk, struct rtnl_qdisc *qdisc)
Delete qdisc.
Definition: qdisc.c:340