Home | History | Annotate | Download | only in src
      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 (at) suug.ch>
     10  */
     11 
     12 #include <netlink/cli/utils.h>
     13 #include <netlink/cli/class.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-class-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 class 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 class (e.g. pfifo_fast)\n"
     35 	"\n"
     36 	"EXAMPLE\n"
     37 	"    # Delete all classes on eth0 attached to parent 1:\n"
     38 	"    $ nl-class-delete --dev eth0 --parent 1:\n"
     39 	"\n"
     40 	);
     41 
     42 	exit(0);
     43 }
     44 
     45 static void delete_cb(struct nl_object *obj, void *arg)
     46 {
     47 	struct rtnl_class *class = nl_object_priv(obj);
     48 	struct nl_dump_params params = {
     49 		.dp_type = NL_DUMP_LINE,
     50 		.dp_fd = stdout,
     51 	};
     52 	int err;
     53 
     54 	if (interactive && !nl_cli_confirm(obj, &params, default_yes))
     55 		return;
     56 
     57 	if ((err = rtnl_class_delete(sock, class)) < 0)
     58 		nl_cli_fatal(err, "Unable to delete class: %s\n", nl_geterror(err));
     59 
     60 	if (!quiet) {
     61 		printf("Deleted ");
     62 		nl_object_dump(obj, &params);
     63 	}
     64 
     65 	deleted++;
     66 }
     67 
     68 int main(int argc, char *argv[])
     69 {
     70 	struct rtnl_class *class;
     71 	struct rtnl_tc *tc;
     72 	struct nl_cache *link_cache, *class_cache;
     73 
     74 	sock = nl_cli_alloc_socket();
     75 	nl_cli_connect(sock, NETLINK_ROUTE);
     76 	link_cache = nl_cli_link_alloc_cache(sock);
     77  	class = nl_cli_class_alloc();
     78 	tc = (struct rtnl_tc *) class;
     79 
     80 	for (;;) {
     81 		int c, optidx = 0;
     82 		enum {
     83 			ARG_YES = 257,
     84 			ARG_INTERACTIVE = 258,
     85 		};
     86 		static struct option long_opts[] = {
     87 			{ "interactive", 0, 0, ARG_INTERACTIVE },
     88 			{ "yes", 0, 0, ARG_YES },
     89 			{ "quiet", 0, 0, 'q' },
     90 			{ "help", 0, 0, 'h' },
     91 			{ "version", 0, 0, 'v' },
     92 			{ "dev", 1, 0, 'd' },
     93 			{ "parent", 1, 0, 'p' },
     94 			{ "id", 1, 0, 'i' },
     95 			{ "kind", 1, 0, 'k' },
     96 			{ 0, 0, 0, 0 }
     97 		};
     98 
     99 		c = getopt_long(argc, argv, "qhvd:p:i:k:", long_opts, &optidx);
    100 		if (c == -1)
    101 			break;
    102 
    103 		switch (c) {
    104 		case '?': nl_cli_fatal(EINVAL, "Invalid options");
    105 		case ARG_INTERACTIVE: interactive = 1; break;
    106 		case ARG_YES: default_yes = 1; break;
    107 		case 'q': quiet = 1; break;
    108 		case 'h': print_usage(); break;
    109 		case 'v': nl_cli_print_version(); break;
    110 		case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
    111 		case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
    112 		case 'i': nl_cli_tc_parse_handle(tc, optarg, 0); break;
    113 		case 'k': nl_cli_tc_parse_kind(tc, optarg); break;
    114 		}
    115  	}
    116 
    117 	if (!rtnl_tc_get_ifindex(tc))
    118 		nl_cli_fatal(EINVAL, "You must specify a network device (--dev=XXX)");
    119 
    120 	class_cache = nl_cli_class_alloc_cache(sock, rtnl_tc_get_ifindex(tc));
    121 
    122 	nl_cache_foreach_filter(class_cache, OBJ_CAST(class), delete_cb, NULL);
    123 
    124 	if (!quiet)
    125 		printf("Deleted %d classs\n", deleted);
    126 
    127 	return 0;
    128 }
    129