libnl  3.3.0
nl-cls-delete.c
1 /*
2  * src/nl-cls-delete.c Delete Classifier
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) 2008-2010 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #include <netlink/cli/utils.h>
13 #include <netlink/cli/cls.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-cls-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 classifer 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 classifier (e.g. basic, u32, fw)\n"
37 " --proto=PROTO Protocol to match (default: all)\n"
38 " --prio=PRIO Priority (default: 0)\n"
39 "\n"
40 "EXAMPLE\n"
41 " # Delete all classifiers on eth0 attached to parent q_root:\n"
42 " $ nl-cls-delete --dev eth0 --parent q_root:\n"
43 "\n"
44  );
45 
46  exit(0);
47 }
48 
49 static void delete_cb(struct nl_object *obj, void *arg)
50 {
51  struct rtnl_cls *cls = nl_object_priv(obj);
52  struct nl_dump_params params = {
54  .dp_fd = stdout,
55  };
56  int err;
57 
58  if (interactive && !nl_cli_confirm(obj, &params, default_yes))
59  return;
60 
61  if ((err = rtnl_cls_delete(sock, cls, 0)) < 0)
62  nl_cli_fatal(err, "Unable to delete classifier: %s\n",
63  nl_geterror(err));
64 
65  if (!quiet) {
66  printf("Deleted ");
67  nl_object_dump(obj, &params);
68  }
69 
70  deleted++;
71 }
72 
73 static void __delete_link(int ifindex, struct rtnl_cls *filter)
74 {
75  struct nl_cache *cache;
76  uint32_t parent = rtnl_tc_get_parent((struct rtnl_tc *) filter);
77 
78  cache = nl_cli_cls_alloc_cache(sock, ifindex, parent);
79  nl_cache_foreach_filter(cache, OBJ_CAST(filter), delete_cb, NULL);
80  nl_cache_free(cache);
81 }
82 
83 static void delete_link(struct nl_object *obj, void *arg)
84 {
85  struct rtnl_link *link = nl_object_priv(obj);
86 
87  __delete_link(rtnl_link_get_ifindex(link), arg);
88 }
89 
90 int main(int argc, char *argv[])
91 {
92  struct rtnl_cls *cls;
93  struct rtnl_tc *tc;
94  struct nl_cache *link_cache;
95  int ifindex;
96 
97  sock = nl_cli_alloc_socket();
98  nl_cli_connect(sock, NETLINK_ROUTE);
99  link_cache = nl_cli_link_alloc_cache(sock);
100  cls = nl_cli_cls_alloc();
101  tc = (struct rtnl_tc *) cls;
102 
103  for (;;) {
104  int c, optidx = 0;
105  enum {
106  ARG_YES = 257,
107  ARG_INTERACTIVE = 258,
108  ARG_PROTO,
109  ARG_PRIO,
110  };
111  static struct option long_opts[] = {
112  { "interactive", 0, 0, ARG_INTERACTIVE },
113  { "yes", 0, 0, ARG_YES },
114  { "quiet", 0, 0, 'q' },
115  { "help", 0, 0, 'h' },
116  { "version", 0, 0, 'v' },
117  { "dev", 1, 0, 'd' },
118  { "parent", 1, 0, 'p' },
119  { "id", 1, 0, 'i' },
120  { "kind", 1, 0, 'k' },
121  { "proto", 1, 0, ARG_PROTO },
122  { "prio", 1, 0, ARG_PRIO },
123  { 0, 0, 0, 0 }
124  };
125 
126  c = getopt_long(argc, argv, "qhvd:p:i:k:", long_opts, &optidx);
127  if (c == -1)
128  break;
129 
130  switch (c) {
131  case '?': nl_cli_fatal(EINVAL, "Invalid options");
132  case ARG_INTERACTIVE: interactive = 1; break;
133  case ARG_YES: default_yes = 1; break;
134  case 'q': quiet = 1; break;
135  case 'h': print_usage(); break;
136  case 'v': nl_cli_print_version(); break;
137  case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
138  case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
139  case 'i': nl_cli_tc_parse_handle(tc, optarg, 0); break;
140  case 'k': nl_cli_tc_parse_kind(tc, optarg); break;
141  case ARG_PROTO: nl_cli_cls_parse_proto(cls, optarg); break;
142  case ARG_PRIO:
143  rtnl_cls_set_prio(cls, nl_cli_parse_u32(optarg));
144  break;
145  }
146  }
147 
148  if ((ifindex = rtnl_tc_get_ifindex(tc)))
149  __delete_link(ifindex, cls);
150  else
151  nl_cache_foreach(link_cache, delete_link, cls);
152 
153  if (!quiet)
154  printf("Deleted %d classs\n", deleted);
155 
156  return 0;
157 }
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
int rtnl_cls_delete(struct nl_sock *sk, struct rtnl_cls *cls, int flags)
Delete classifier.
Definition: cls.c:295
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_cache_free(struct nl_cache *cache)
Free a cache.
Definition: cache.c:408
void nl_cache_foreach(struct nl_cache *cache, void(*cb)(struct nl_object *, void *), void *arg)
Call a callback on each element of the cache.
Definition: cache.c:1265
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
uint32_t rtnl_tc_get_parent(struct rtnl_tc *tc)
Return parent identifier of a traffic control object.
Definition: tc.c:499
uint32_t nl_cli_parse_u32(const char *arg)
Parse a text based 32 bit unsigned integer argument.
Definition: utils.c:36
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_tc_get_ifindex(struct rtnl_tc *tc)
Return interface index of traffic control object.
Definition: tc.c:275