libnl  3.3.0
nl-class-delete.c
1 /*
2  * src/nl-class-delete.c Delete Traffic Classes
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) 2010 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #include <netlink/cli/utils.h>
13 #include <netlink/cli/class.h>
14 #include <netlink/cli/link.h>
15 
16 #include <linux/netlink.h>
17 
18 static int quiet = 0, default_yes = 0, deleted = 0, interactive = 0;
19 static struct nl_sock *sock;
20 
21 static void print_usage(void)
22 {
23  printf(
24  "Usage: nl-class-delete [OPTION]... [class]\n"
25  "\n"
26  "OPTIONS\n"
27  " --interactive Run interactively.\n"
28  " --yes Set default answer to yes.\n"
29  " -q, --quiet Do not print informal notifications.\n"
30  " -h, --help Show this help text and exit.\n"
31  " -v, --version Show versioning information and exit.\n"
32  "\n"
33  " -d, --dev=DEV Device the class is attached to.\n"
34  " -p, --parent=ID Identifier of parent qdisc/class.\n"
35  " -i, --id=ID Identifier\n"
36  " -k, --kind=NAME Kind of class (e.g. pfifo_fast)\n"
37  "\n"
38  "EXAMPLE\n"
39  " # Delete all classes on eth0 attached to parent 1:\n"
40  " $ nl-class-delete --dev eth0 --parent 1:\n"
41  "\n"
42  );
43 
44  exit(0);
45 }
46 
47 static void delete_cb(struct nl_object *obj, void *arg)
48 {
49  struct rtnl_class *class = nl_object_priv(obj);
50  struct nl_dump_params params = {
52  .dp_fd = stdout,
53  };
54  int err;
55 
56  if (interactive && !nl_cli_confirm(obj, &params, default_yes))
57  return;
58 
59  if ((err = rtnl_class_delete(sock, class)) < 0)
60  nl_cli_fatal(err, "Unable to delete class: %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_class *class;
73  struct rtnl_tc *tc;
74  struct nl_cache *link_cache, *class_cache;
75 
76  sock = nl_cli_alloc_socket();
77  nl_cli_connect(sock, NETLINK_ROUTE);
78  link_cache = nl_cli_link_alloc_cache(sock);
79  class = nl_cli_class_alloc();
80  tc = (struct rtnl_tc *) class;
81 
82  for (;;) {
83  int c, optidx = 0;
84  enum {
85  ARG_YES = 257,
86  ARG_INTERACTIVE = 258,
87  };
88  static struct option long_opts[] = {
89  { "interactive", 0, 0, ARG_INTERACTIVE },
90  { "yes", 0, 0, ARG_YES },
91  { "quiet", 0, 0, 'q' },
92  { "help", 0, 0, 'h' },
93  { "version", 0, 0, 'v' },
94  { "dev", 1, 0, 'd' },
95  { "parent", 1, 0, 'p' },
96  { "id", 1, 0, 'i' },
97  { "kind", 1, 0, 'k' },
98  { 0, 0, 0, 0 }
99  };
100 
101  c = getopt_long(argc, argv, "qhvd:p:i:k:", long_opts, &optidx);
102  if (c == -1)
103  break;
104 
105  switch (c) {
106  case '?': nl_cli_fatal(EINVAL, "Invalid options");
107  case ARG_INTERACTIVE: interactive = 1; break;
108  case ARG_YES: default_yes = 1; break;
109  case 'q': quiet = 1; break;
110  case 'h': print_usage(); break;
111  case 'v': nl_cli_print_version(); break;
112  case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
113  case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
114  case 'i': nl_cli_tc_parse_handle(tc, optarg, 0); break;
115  case 'k': nl_cli_tc_parse_kind(tc, optarg); break;
116  }
117  }
118 
119  if (!rtnl_tc_get_ifindex(tc))
120  nl_cli_fatal(EINVAL, "You must specify a network device (--dev=XXX)");
121 
122  class_cache = nl_cli_class_alloc_cache(sock, rtnl_tc_get_ifindex(tc));
123 
124  nl_cache_foreach_filter(class_cache, OBJ_CAST(class), delete_cb, NULL);
125 
126  if (!quiet)
127  printf("Deleted %d classs\n", deleted);
128 
129  return 0;
130 }
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
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:71
Dumping parameters.
Definition: types.h:33
int rtnl_class_delete(struct nl_sock *sk, struct rtnl_class *class)
Delete traffic class.
Definition: class.c:252
int rtnl_tc_get_ifindex(struct rtnl_tc *tc)
Return interface index of traffic control object.
Definition: tc.c:275