Home | History | Annotate | Download | only in src
      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-2009 Thomas Graf <tgraf (at) suug.ch>
     10  */
     11 
     12 #include <netlink/cli/utils.h>
     13 #include <netlink/cli/qdisc.h>
     14 #include <netlink/cli/link.h>
     15 
     16 static int quiet = 0, default_yes = 0, deleted = 0, interactive = 0;
     17 struct nl_sock *sock;
     18 
     19 static void print_usage(void)
     20 {
     21 	printf(
     22 	"Usage: nl-qdisc-delete [OPTION]... [QDISC]\n"
     23 	"\n"
     24 	"Options\n"
     25 	" -i, --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\n"
     29 	" -v, --version         Show versioning information\n"
     30 	"\n"
     31 	"QDisc Options\n"
     32 	" -d, --dev=DEV         Device the qdisc is attached to\n"
     33 	" -p, --parent=HANDLE   Identifier of parent qdisc\n"
     34 	" -H, --handle=HANDLE   Identifier\n"
     35 	" -k, --kind=NAME       Kind of qdisc (e.g. pfifo_fast)\n"
     36 	);
     37 
     38 	exit(0);
     39 }
     40 
     41 static void delete_cb(struct nl_object *obj, void *arg)
     42 {
     43 	struct rtnl_qdisc *qdisc = nl_object_priv(obj);
     44 	struct nl_dump_params params = {
     45 		.dp_type = NL_DUMP_LINE,
     46 		.dp_fd = stdout,
     47 	};
     48 	int err;
     49 
     50 	if (interactive && !nl_cli_confirm(obj, &params, default_yes))
     51 		return;
     52 
     53 	if ((err = rtnl_qdisc_delete(sock, qdisc)) < 0)
     54 		nl_cli_fatal(err, "Unable to delete qdisc: %s\n", nl_geterror(err));
     55 
     56 	if (!quiet) {
     57 		printf("Deleted ");
     58 		nl_object_dump(obj, &params);
     59 	}
     60 
     61 	deleted++;
     62 }
     63 
     64 int main(int argc, char *argv[])
     65 {
     66 	struct rtnl_qdisc *qdisc;
     67 	struct nl_cache *link_cache, *qdisc_cache;
     68 
     69 	sock = nl_cli_alloc_socket();
     70 	nl_cli_connect(sock, NETLINK_ROUTE);
     71 	link_cache = nl_cli_link_alloc_cache(sock);
     72 	qdisc_cache = nl_cli_qdisc_alloc_cache(sock);
     73  	qdisc = nl_cli_qdisc_alloc();
     74 
     75 	for (;;) {
     76 		int c, optidx = 0;
     77 		enum {
     78 			ARG_YES = 257,
     79 		};
     80 		static struct option long_opts[] = {
     81 			{ "interactive", 0, 0, 'i' },
     82 			{ "yes", 0, 0, ARG_YES },
     83 			{ "quiet", 0, 0, 'q' },
     84 			{ "help", 0, 0, 'h' },
     85 			{ "version", 0, 0, 'v' },
     86 			{ "dev", 1, 0, 'd' },
     87 			{ "parent", 1, 0, 'p' },
     88 			{ "handle", 1, 0, 'H' },
     89 			{ "kind", 1, 0, 'k' },
     90 			{ 0, 0, 0, 0 }
     91 		};
     92 
     93 		c = getopt_long(argc, argv, "iqhvd:p:H:k:", long_opts, &optidx);
     94 		if (c == -1)
     95 			break;
     96 
     97 		switch (c) {
     98 		case 'i': interactive = 1; break;
     99 		case ARG_YES: default_yes = 1; break;
    100 		case 'q': quiet = 1; break;
    101 		case 'h': print_usage(); break;
    102 		case 'v': nl_cli_print_version(); break;
    103 		case 'd': nl_cli_qdisc_parse_dev(qdisc, link_cache, optarg); break;
    104 		case 'p': nl_cli_qdisc_parse_parent(qdisc, optarg); break;
    105 		case 'H': nl_cli_qdisc_parse_handle(qdisc, optarg); break;
    106 		case 'k': nl_cli_qdisc_parse_kind(qdisc, optarg); break;
    107 		}
    108  	}
    109 
    110 	nl_cache_foreach_filter(qdisc_cache, OBJ_CAST(qdisc), delete_cb, NULL);
    111 
    112 	if (!quiet)
    113 		printf("Deleted %d qdiscs\n", deleted);
    114 
    115 	return 0;
    116 }
    117