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
This commit is contained in:
Thomas Graf 2010-10-20 14:57:39 +02:00
parent 18848090f9
commit 27883b0f9b
13 changed files with 418 additions and 15 deletions

View file

@ -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

View file

@ -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 *);

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

1
src/.gitignore vendored
View file

@ -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

View file

@ -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

View file

@ -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

71
src/lib/class.c Normal file
View file

@ -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);
}
/** @} */

View file

@ -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);
}

129
src/nl-class-add.c Normal file
View file

@ -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;
}

View file

@ -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 ");