libnl  3.2.24-rc1
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 static int quiet = 0, default_yes = 0, deleted = 0, interactive = 0;
17 static struct nl_sock *sock;
18 
19 static void print_usage(void)
20 {
21  printf(
22 "Usage: nl-cls-delete [OPTION]... [class]\n"
23 "\n"
24 "OPTIONS\n"
25 " --interactive Run interactively.\n"
26 " --yes Set default answer to yes.\n"
27 " -q, --quiet Do not print informal notifications.\n"
28 " -h, --help Show this help text and exit.\n"
29 " -v, --version Show versioning information and exit.\n"
30 "\n"
31 " -d, --dev=DEV Device the classifer is attached to.\n"
32 " -p, --parent=ID Identifier of parent qdisc/class.\n"
33 " -i, --id=ID Identifier\n"
34 " -k, --kind=NAME Kind of classifier (e.g. basic, u32, fw)\n"
35 " --protocol=PROTO Protocol to match (default: all)\n"
36 " --prio=PRIO Priority (default: 0)\n"
37 "\n"
38 "EXAMPLE\n"
39 " # Delete all classifiers on eth0 attached to parent q_root:\n"
40 " $ nl-cls-delete --dev eth0 --parent q_root:\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_cls *cls = 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_cls_delete(sock, cls, 0)) < 0)
60  nl_cli_fatal(err, "Unable to delete classifier: %s\n",
61  nl_geterror(err));
62 
63  if (!quiet) {
64  printf("Deleted ");
65  nl_object_dump(obj, &params);
66  }
67 
68  deleted++;
69 }
70 
71 static void __delete_link(int ifindex, struct rtnl_cls *filter)
72 {
73  struct nl_cache *cache;
74  uint32_t parent = rtnl_tc_get_parent((struct rtnl_tc *) filter);
75 
76  cache = nl_cli_cls_alloc_cache(sock, ifindex, parent);
77  nl_cache_foreach_filter(cache, OBJ_CAST(filter), delete_cb, NULL);
78  nl_cache_free(cache);
79 }
80 
81 static void delete_link(struct nl_object *obj, void *arg)
82 {
83  struct rtnl_link *link = nl_object_priv(obj);
84 
85  __delete_link(rtnl_link_get_ifindex(link), arg);
86 }
87 
88 int main(int argc, char *argv[])
89 {
90  struct rtnl_cls *cls;
91  struct rtnl_tc *tc;
92  struct nl_cache *link_cache;
93  int ifindex;
94 
95  sock = nl_cli_alloc_socket();
96  nl_cli_connect(sock, NETLINK_ROUTE);
97  link_cache = nl_cli_link_alloc_cache(sock);
98  cls = nl_cli_cls_alloc();
99  tc = (struct rtnl_tc *) cls;
100 
101  for (;;) {
102  int c, optidx = 0;
103  enum {
104  ARG_YES = 257,
105  ARG_INTERACTIVE = 258,
106  ARG_PROTO,
107  ARG_PRIO,
108  };
109  static struct option long_opts[] = {
110  { "interactive", 0, 0, ARG_INTERACTIVE },
111  { "yes", 0, 0, ARG_YES },
112  { "quiet", 0, 0, 'q' },
113  { "help", 0, 0, 'h' },
114  { "version", 0, 0, 'v' },
115  { "dev", 1, 0, 'd' },
116  { "parent", 1, 0, 'p' },
117  { "id", 1, 0, 'i' },
118  { "kind", 1, 0, 'k' },
119  { "proto", 1, 0, ARG_PROTO },
120  { "prio", 1, 0, ARG_PRIO },
121  { 0, 0, 0, 0 }
122  };
123 
124  c = getopt_long(argc, argv, "qhvd:p:i:k:", long_opts, &optidx);
125  if (c == -1)
126  break;
127 
128  switch (c) {
129  case '?': nl_cli_fatal(EINVAL, "Invalid options");
130  case ARG_INTERACTIVE: interactive = 1; break;
131  case ARG_YES: default_yes = 1; break;
132  case 'q': quiet = 1; break;
133  case 'h': print_usage(); break;
134  case 'v': nl_cli_print_version(); break;
135  case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
136  case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
137  case 'i': nl_cli_tc_parse_handle(tc, optarg, 0); break;
138  case 'k': nl_cli_tc_parse_kind(tc, optarg); break;
139  case ARG_PROTO: nl_cli_cls_parse_proto(cls, optarg); break;
140  case ARG_PRIO:
141  rtnl_cls_set_prio(cls, nl_cli_parse_u32(optarg));
142  break;
143  }
144  }
145 
146  if ((ifindex = rtnl_tc_get_ifindex(tc)))
147  __delete_link(ifindex, cls);
148  else
149  nl_cache_foreach(link_cache, delete_link, cls);
150 
151  if (!quiet)
152  printf("Deleted %d classs\n", deleted);
153 
154  return 0;
155 }