diff options
author | Thomas Graf <tgraf@suug.ch> | 2010-10-20 12:57:39 (GMT) |
---|---|---|
committer | Thomas Graf <tgraf@suug.ch> | 2010-10-20 12:57:39 (GMT) |
commit | 27883b0f9b0da6bb33ccc185107a2870df25d030 (patch) | |
tree | fbda57a6026fbc6653648ecb9d588d2d9e938b5e /src | |
parent | 18848090f99cae37ac1a3052369ab4d26ed9ada0 (diff) | |
download | libnl-27883b0f9b0da6bb33ccc185107a2870df25d030.zip libnl-27883b0f9b0da6bb33ccc185107a2870df25d030.tar.gz libnl-27883b0f9b0da6bb33ccc185107a2870df25d030.tar.bz2 |
nl-class-add tool
Adds a cli based tool to add/update traffic classes. This tool requires
each class to be supported via the respetive qdisc module in
pkglibdir/cli/qdisc/$name.so.
Syntax:
nl-class-add --dev eth2 --parent 1: --id 1:1 htb --rate 100mbit
nl-class-add --update --dev eth2 --id 1:1 htb --rate 200mbit
Diffstat (limited to 'src')
-rw-r--r-- | src/.gitignore | 1 | ||||
-rw-r--r-- | src/Makefile.am | 4 | ||||
-rw-r--r-- | src/lib/Makefile.am | 2 | ||||
-rw-r--r-- | src/lib/class.c | 71 | ||||
-rw-r--r-- | src/lib/qdisc.c | 33 | ||||
-rw-r--r-- | src/nl-class-add.c | 129 | ||||
-rw-r--r-- | src/nl-qdisc-add.c | 2 |
7 files changed, 240 insertions, 2 deletions
diff --git a/src/.gitignore b/src/.gitignore index 12859cb..cacaa3c 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -21,6 +21,7 @@ nl-neightbl-list nl-qdisc-add nl-qdisc-delete nl-qdisc-list +nl-class-add nl-route-add nl-route-delete nl-route-list diff --git a/src/Makefile.am b/src/Makefile.am index 4a7f98f..1a6f080 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -7,6 +7,7 @@ AM_LDFLAGS = -L${top_builddir}/lib -L${top_builddir}/src/lib -lnl-cli sbin_PROGRAMS = \ nl-qdisc-add \ + nl-class-add \ nl-classid-lookup noinst_PROGRAMS = \ @@ -77,6 +78,9 @@ nl_qdisc_delete_LDADD = -lnl-route nl_qdisc_list_SOURCES = nl-qdisc-list.c nl_qdisc_list_LDADD = -lnl-route +nl_class_add_SOURCES = nl-class-add.c +nl_class_add_LDADD = -lnl-route + nl_route_add_SOURCES = nl-route-add.c nl_route_add_LDADD = -lnl-route nl_route_delete_SOURCES = nl-route-delete.c diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 2b48876..1ed5912 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -34,6 +34,6 @@ libnl_cli_la_LIBADD = ${top_builddir}/lib/libnl.la \ ${top_builddir}/lib/libnl-genl.la libnl_cli_la_SOURCES = \ - utils.c addr.c ct.c link.c neigh.c qdisc.c rule.c route.c + utils.c addr.c ct.c link.c neigh.c qdisc.c class.c rule.c route.c # cls/ematch_syntax.c cls/ematch_grammar.c cls/ematch.c # cls/pktloc_syntax.c cls/pktloc_grammar.c cls/utils.c diff --git a/src/lib/class.c b/src/lib/class.c new file mode 100644 index 0000000..93346d5 --- /dev/null +++ b/src/lib/class.c @@ -0,0 +1,71 @@ +/* + * src/lib/class.c CLI Class Helpers + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * Copyright (c) 2010 Thomas Graf <tgraf@suug.ch> + */ + +/** + * @ingroup cli + * @defgroup cli_class Traffic Classes + * @{ + */ + +#include <netlink/cli/utils.h> +#include <netlink/cli/class.h> + +struct rtnl_class *nl_cli_class_alloc(void) +{ + struct rtnl_class *class; + + class = rtnl_class_alloc(); + if (!class) + nl_cli_fatal(ENOMEM, "Unable to allocate class object"); + + return class; +} + +void nl_cli_class_parse_dev(struct rtnl_class *class, struct nl_cache *link_cache, char *arg) +{ + int ival; + + if (!(ival = rtnl_link_name2i(link_cache, arg))) + nl_cli_fatal(ENOENT, "Link \"%s\" does not exist", arg); + + rtnl_class_set_ifindex(class, ival); +} + +void nl_cli_class_parse_parent(struct rtnl_class *class, char *arg) +{ + uint32_t parent; + int err; + + if ((err = rtnl_tc_str2handle(arg, &parent)) < 0) + nl_cli_fatal(err, "Unable to parse handle \"%s\": %s", + arg, nl_geterror(err)); + + rtnl_class_set_parent(class, parent); +} + +void nl_cli_class_parse_handle(struct rtnl_class *class, char *arg) +{ + uint32_t handle; + int err; + + if ((err = rtnl_tc_str2handle(arg, &handle)) < 0) + nl_cli_fatal(err, "Unable to parse classid \"%s\": %s", + arg, nl_geterror(err)); + + rtnl_class_set_handle(class, handle); +} + +void nl_cli_class_parse_kind(struct rtnl_class *class, char *arg) +{ + rtnl_class_set_kind(class, arg); +} + +/** @} */ diff --git a/src/lib/qdisc.c b/src/lib/qdisc.c index 52b7a6a..8b16f3f 100644 --- a/src/lib/qdisc.c +++ b/src/lib/qdisc.c @@ -17,6 +17,8 @@ #include <netlink/cli/utils.h> #include <netlink/cli/qdisc.h> +#include <netlink/route/class.h> +#include <netlink/route/class-modules.h> struct rtnl_qdisc *nl_cli_qdisc_alloc(void) { @@ -81,6 +83,17 @@ struct nl_cli_qdisc_module *__nl_cli_qdisc_lookup(struct rtnl_qdisc_ops *ops) return NULL; } +struct nl_cli_qdisc_module *__nl_cli_class_lookup(struct rtnl_class_ops *ops) +{ + struct nl_cli_qdisc_module *qm; + + nl_list_for_each_entry(qm, &qdisc_modules, qm_list) + if (qm->qm_class_ops == ops) + return qm; + + return NULL; +} + struct nl_cli_qdisc_module *nl_cli_qdisc_lookup(struct rtnl_qdisc_ops *ops) { struct nl_cli_qdisc_module *qm; @@ -99,6 +112,24 @@ struct nl_cli_qdisc_module *nl_cli_qdisc_lookup(struct rtnl_qdisc_ops *ops) return qm; } +struct nl_cli_qdisc_module *nl_cli_qdisc_lookup_by_class(struct rtnl_class_ops *ops) +{ + struct nl_cli_qdisc_module *qm; + + if ((qm = __nl_cli_class_lookup(ops))) + return qm; + + nl_cli_load_module("cli/qdisc", ops->co_kind); + + if (!(qm = __nl_cli_class_lookup(ops))) { + nl_cli_fatal(EINVAL, "Application bug: The shared library for " + "the class \"%s\" was successfully loaded but it " + "seems that module did not register itself"); + } + + return qm; +} + void nl_cli_qdisc_register(struct nl_cli_qdisc_module *qm) { struct rtnl_qdisc_ops *ops; @@ -114,6 +145,8 @@ void nl_cli_qdisc_register(struct nl_cli_qdisc_module *qm) } qm->qm_ops = ops; + qm->qm_class_ops = __rtnl_class_lookup_ops(qm->qm_name); + nl_list_add_tail(&qm->qm_list, &qdisc_modules); } diff --git a/src/nl-class-add.c b/src/nl-class-add.c new file mode 100644 index 0000000..6e1c402 --- /dev/null +++ b/src/nl-class-add.c @@ -0,0 +1,129 @@ +/* + * src/nl-class-add.c Add/Update/Replace Traffic Class + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * Copyright (c) 2010 Thomas Graf <tgraf@suug.ch> + */ + +#include <netlink/cli/utils.h> +#include <netlink/cli/qdisc.h> +#include <netlink/cli/class.h> +#include <netlink/cli/link.h> + +static int quiet = 0; + +static void print_usage(void) +{ + printf( +"Usage: nl-class-add [OPTIONS]... class [CONFIGURATION]...\n" +"\n" +"OPTIONS\n" +" -q, --quiet Do not print informal notifications.\n" +" -h, --help Show this help text.\n" +" -v, --version Show versioning information.\n" +" --update Update class if it exists.\n" +" --update-only Only update class, never create it.\n" +" -d, --dev=DEV Network device the class should be attached to.\n" +" -i, --id=ID ID of new class (default: auto-generated)r\n" +" -p, --parent=ID ID of parent { root | ingress | class-ID }\n" +"\n" +"CONFIGURATION\n" +" -h, --help Show help text of class specific options.\n" +"\n" +"EXAMPLE\n" +" $ nl-class-add --dev=eth1 --parent=root htb --rate=100mbit\n" +"\n" + ); + exit(0); +} + +int main(int argc, char *argv[]) +{ + struct nl_sock *sock; + struct rtnl_class *class; + struct nl_cache *link_cache; + struct nl_dump_params dp = { + .dp_type = NL_DUMP_DETAILS, + .dp_fd = stdout, + }; + struct nl_cli_qdisc_module *qm; + struct rtnl_class_ops *ops; + int err, flags = NLM_F_CREATE | NLM_F_EXCL; + char *kind; + + sock = nl_cli_alloc_socket(); + nl_cli_connect(sock, NETLINK_ROUTE); + + link_cache = nl_cli_link_alloc_cache(sock); + + class = nl_cli_class_alloc(); + + for (;;) { + int c, optidx = 0; + enum { + ARG_UPDATE = 257, + ARG_UPDATE_ONLY = 258, + }; + static struct option long_opts[] = { + { "quiet", 0, 0, 'q' }, + { "help", 0, 0, 'h' }, + { "version", 0, 0, 'v' }, + { "dev", 1, 0, 'd' }, + { "parent", 1, 0, 'p' }, + { "id", 1, 0, 'i' }, + { "update", 0, 0, ARG_UPDATE }, + { "update-only", 0, 0, ARG_UPDATE_ONLY }, + { 0, 0, 0, 0 } + }; + + c = getopt_long(argc, argv, "+qhvd:p:i:", + long_opts, &optidx); + if (c == -1) + break; + + switch (c) { + case 'q': quiet = 1; break; + case 'h': print_usage(); break; + case 'v': nl_cli_print_version(); break; + case 'd': nl_cli_class_parse_dev(class, link_cache, optarg); break; + case 'p': nl_cli_class_parse_parent(class, optarg); break; + case 'i': nl_cli_class_parse_handle(class, optarg); break; + case ARG_UPDATE: flags = NLM_F_CREATE; break; + case ARG_UPDATE_ONLY: flags = 0; break; + } + } + + if (optind >= argc) + print_usage(); + + if (!rtnl_class_get_ifindex(class)) + nl_cli_fatal(EINVAL, "You must specify a network device (--dev=XXX)"); + + if (!rtnl_class_get_parent(class)) + nl_cli_fatal(EINVAL, "You must specify a parent (--parent=XXX)"); + + kind = argv[optind++]; + rtnl_class_set_kind(class, kind); + + if (!(ops = rtnl_class_lookup_ops(class))) + nl_cli_fatal(ENOENT, "Unknown class \"%s\"", kind); + + if (!(qm = nl_cli_qdisc_lookup_by_class(ops))) + nl_cli_fatal(ENOTSUP, "class type \"%s\" not supported.", kind); + + qm->qm_parse_class_argv(class, argc, argv); + + if (!quiet) { + printf("Adding "); + nl_object_dump(OBJ_CAST(class), &dp); + } + + if ((err = rtnl_class_add(sock, class, flags)) < 0) + nl_cli_fatal(EINVAL, "Unable to add class: %s", nl_geterror(err)); + + return 0; +} diff --git a/src/nl-qdisc-add.c b/src/nl-qdisc-add.c index cb2b129..739a6ed 100644 --- a/src/nl-qdisc-add.c +++ b/src/nl-qdisc-add.c @@ -122,7 +122,7 @@ int main(int argc, char *argv[]) if (!(qm = nl_cli_qdisc_lookup(ops))) nl_cli_fatal(ENOTSUP, "Qdisc type \"%s\" not supported.", kind); - qm->qm_parse_argv(qdisc, argc, argv); + qm->qm_parse_qdisc_argv(qdisc, argc, argv); if (!quiet) { printf("Adding "); |