diff options
-rw-r--r-- | include/netlink/cli/class.h | 29 | ||||
-rw-r--r-- | include/netlink/cli/qdisc.h | 7 | ||||
-rw-r--r-- | lib/cli/qdisc/bfifo.c | 4 | ||||
-rw-r--r-- | lib/cli/qdisc/blackhole.c | 4 | ||||
-rw-r--r-- | lib/cli/qdisc/htb.c | 143 | ||||
-rw-r--r-- | lib/cli/qdisc/pfifo.c | 4 | ||||
-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 |
13 files changed, 418 insertions, 15 deletions
diff --git a/include/netlink/cli/class.h b/include/netlink/cli/class.h new file mode 100644 index 0000000..9c17761 --- /dev/null +++ b/include/netlink/cli/class.h @@ -0,0 +1,29 @@ +/* + * netlink/cli/class.h 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> + */ + +#ifndef __NETLINK_CLI_CLASS_H_ +#define __NETLINK_CLI_CLASS_H_ + +#include <netlink/route/class.h> +#include <netlink/route/class-modules.h> + +#define nl_cli_class_alloc_cache(sk) \ + nl_cli_alloc_cache((sk), "traffic classes", \ + rtnl_class_alloc_cache) + +extern struct rtnl_class *nl_cli_class_alloc(void); + +extern void nl_cli_class_parse_dev(struct rtnl_class *, struct nl_cache *, char *); +extern void nl_cli_class_parse_parent(struct rtnl_class *, char *); +extern void nl_cli_class_parse_handle(struct rtnl_class *, char *); +extern void nl_cli_class_parse_kind(struct rtnl_class *, char *); + +#endif diff --git a/include/netlink/cli/qdisc.h b/include/netlink/cli/qdisc.h index 78b08dc..537e1a9 100644 --- a/include/netlink/cli/qdisc.h +++ b/include/netlink/cli/qdisc.h @@ -6,7 +6,7 @@ * License as published by the Free Software Foundation version 2.1 * of the License. * - * Copyright (c) 2008-2009 Thomas Graf <tgraf@suug.ch> + * Copyright (c) 2008-2010 Thomas Graf <tgraf@suug.ch> */ #ifndef __NETLINK_CLI_QDISC_H_ @@ -23,11 +23,14 @@ struct nl_cli_qdisc_module { const char * qm_name; struct rtnl_qdisc_ops * qm_ops; - void (*qm_parse_argv)(struct rtnl_qdisc *, int, char **); + struct rtnl_class_ops * qm_class_ops; + void (*qm_parse_qdisc_argv)(struct rtnl_qdisc *, int, char **); + void (*qm_parse_class_argv)(struct rtnl_class *, int, char **); struct nl_list_head qm_list; }; extern struct nl_cli_qdisc_module *nl_cli_qdisc_lookup(struct rtnl_qdisc_ops *); +extern struct nl_cli_qdisc_module *nl_cli_qdisc_lookup_by_class(struct rtnl_class_ops *); extern void nl_cli_qdisc_register(struct nl_cli_qdisc_module *); extern void nl_cli_qdisc_unregister(struct nl_cli_qdisc_module *); diff --git a/lib/cli/qdisc/bfifo.c b/lib/cli/qdisc/bfifo.c index 7354728..2f74659 100644 --- a/lib/cli/qdisc/bfifo.c +++ b/lib/cli/qdisc/bfifo.c @@ -66,8 +66,8 @@ static void bfifo_parse_argv(struct rtnl_qdisc *qdisc, int argc, char **argv) static struct nl_cli_qdisc_module bfifo_module = { - .qm_name = "bfifo", - .qm_parse_argv = bfifo_parse_argv, + .qm_name = "bfifo", + .qm_parse_qdisc_argv = bfifo_parse_argv, }; static void __init bfifo_init(void) diff --git a/lib/cli/qdisc/blackhole.c b/lib/cli/qdisc/blackhole.c index 176f463..1eebb32 100644 --- a/lib/cli/qdisc/blackhole.c +++ b/lib/cli/qdisc/blackhole.c @@ -48,8 +48,8 @@ static void blackhole_parse_argv(struct rtnl_qdisc *qdisc, int argc, char **argv static struct nl_cli_qdisc_module blackhole_module = { - .qm_name = "blackhole", - .qm_parse_argv = blackhole_parse_argv, + .qm_name = "blackhole", + .qm_parse_qdisc_argv = blackhole_parse_argv, }; static void __init blackhole_init(void) diff --git a/lib/cli/qdisc/htb.c b/lib/cli/qdisc/htb.c index 9ce8e25..6b913a8 100644 --- a/lib/cli/qdisc/htb.c +++ b/lib/cli/qdisc/htb.c @@ -11,8 +11,9 @@ #include <netlink/cli/utils.h> #include <netlink/cli/qdisc.h> +#include <netlink/route/sch/htb.h> -static void print_usage(void) +static void print_qdisc_usage(void) { printf( "Usage: nl-qdisc-add [...] htb [OPTIONS]...\n" @@ -27,7 +28,7 @@ static void print_usage(void) " nl-qdisc-add --dev=eth1 --parent=root --handle=1: htb --default=10\n"); } -static void htb_parse_argv(struct rtnl_qdisc *qdisc, int argc, char **argv) +static void htb_parse_qdisc_argv(struct rtnl_qdisc *qdisc, int argc, char **argv) { for (;;) { int c, optidx = 0; @@ -48,7 +49,7 @@ static void htb_parse_argv(struct rtnl_qdisc *qdisc, int argc, char **argv) switch (c) { case 'h': - print_usage(); + print_qdisc_usage(); return; case ARG_R2Q: @@ -62,10 +63,142 @@ static void htb_parse_argv(struct rtnl_qdisc *qdisc, int argc, char **argv) } } +static void print_class_usage(void) +{ + printf( +"Usage: nl-class-add [...] htb [OPTIONS]...\n" +"\n" +"OPTIONS\n" +" --help Show this help text.\n" +" --rate=RATE Rate limit.\n" +" --ceil=RATE Rate limit while borrowing (default: equal to --rate).\n" +" --prio=PRIO Priority, lower is served first (default: 0).\n" +" --mtu=MTU Maximum packet size on the link (default: 1600).\n" +" --mpu=MPU Minimum packet size on the link (default: 0).\n" +" --overhead=OVERHEAD Overhead in bytes per packet (default: 0).\n" +" --quantum=SIZE Amount of bytes to serve at once (default: rate/r2q).\n" +" --burst=SIZE Max charge size of rate burst buffer (default: auto).\n" +" --cburst=SIZE Max charge size of ceil rate burst buffer (default: auto)\n" +"\n" +"EXAMPLE" +" # Attach class 1:1 to htb qdisc 1: and rate limit it to 20mbit\n" +" nl-class-add --dev=eth1 --parent=1: --classid=1:1 htb --rate=20mbit\n"); +} + +static void htb_parse_class_argv(struct rtnl_class *class, int argc, char **argv) +{ + long rate; + + for (;;) { + int c, optidx = 0; + enum { + ARG_RATE = 257, + ARG_QUANTUM = 258, + ARG_CEIL, + ARG_PRIO, + ARG_MTU, + ARG_MPU, + ARG_OVERHEAD, + ARG_BURST, + ARG_CBURST, + }; + static struct option long_opts[] = { + { "help", 0, 0, 'h' }, + { "rate", 1, 0, ARG_RATE }, + { "quantum", 1, 0, ARG_QUANTUM }, + { "ceil", 1, 0, ARG_CEIL }, + { "prio", 1, 0, ARG_PRIO }, + { "mtu", 1, 0, ARG_MTU }, + { "mpu", 1, 0, ARG_MPU }, + { "overhead", 1, 0, ARG_OVERHEAD }, + { "burst", 1, 0, ARG_BURST }, + { "cburst", 1, 0, ARG_CBURST }, + { 0, 0, 0, 0 } + }; + + c = getopt_long(argc, argv, "h", long_opts, &optidx); + if (c == -1) + break; + + switch (c) { + case 'h': + print_class_usage(); + return; + + case ARG_RATE: + rate = nl_size2int(optarg); + if (rate < 0) { + nl_cli_fatal(rate, "Unable to parse htb rate " + "\"%s\": Invalid format.", optarg); + } + + rtnl_htb_set_rate(class, rate); + break; + + case ARG_CEIL: + rate = nl_size2int(optarg); + if (rate < 0) { + nl_cli_fatal(rate, "Unable to parse htb ceil rate " + "\"%s\": Invalid format.", optarg); + } + + rtnl_htb_set_ceil(class, rate); + break; + + case ARG_MTU: + rtnl_htb_set_mtu(class, nl_cli_parse_u32(optarg)); + break; + + case ARG_MPU: + rtnl_htb_set_mpu(class, nl_cli_parse_u32(optarg)); + break; + + case ARG_OVERHEAD: + rtnl_htb_set_overhead(class, nl_cli_parse_u32(optarg)); + break; + + case ARG_PRIO: + rtnl_htb_set_prio(class, nl_cli_parse_u32(optarg)); + break; + + case ARG_QUANTUM: + rate = nl_size2int(optarg); + if (rate < 0) { + nl_cli_fatal(rate, "Unable to parse quantum " + "\"%s\": Invalid format.", optarg); + } + + rtnl_htb_set_quantum(class, rate); + break; + + case ARG_BURST: + rate = nl_size2int(optarg); + if (rate < 0) { + nl_cli_fatal(rate, "Unable to parse burst " + "\"%s\": Invalid format.", optarg); + } + + rtnl_htb_set_rbuffer(class, rate); + break; + + case ARG_CBURST: + rate = nl_size2int(optarg); + if (rate < 0) { + nl_cli_fatal(rate, "Unable to parse cburst " + "\"%s\": Invalid format.", optarg); + } + + rtnl_htb_set_cbuffer(class, rate); + break; + } + } +} + static struct nl_cli_qdisc_module htb_module = { - .qm_name = "htb", - .qm_parse_argv = htb_parse_argv, + .qm_name = "htb", + .qm_parse_qdisc_argv = htb_parse_qdisc_argv, + .qm_parse_class_argv = htb_parse_class_argv, }; static void __init htb_init(void) diff --git a/lib/cli/qdisc/pfifo.c b/lib/cli/qdisc/pfifo.c index 4d900d8..38cc97a 100644 --- a/lib/cli/qdisc/pfifo.c +++ b/lib/cli/qdisc/pfifo.c @@ -59,8 +59,8 @@ static void pfifo_parse_argv(struct rtnl_qdisc *qdisc, int argc, char **argv) static struct nl_cli_qdisc_module pfifo_module = { - .qm_name = "pfifo", - .qm_parse_argv = pfifo_parse_argv, + .qm_name = "pfifo", + .qm_parse_qdisc_argv = pfifo_parse_argv, }; static void __init pfifo_init(void) 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 "); |