libnl  3.2.3
/build/buildd/libnl3-3.2.3/lib/cli/cls/basic.c
00001 /*
00002  * lib/cli/cls/basic.c          basic classifier module for CLI lib
00003  *
00004  *      This library is free software; you can redistribute it and/or
00005  *      modify it under the terms of the GNU Lesser General Public
00006  *      License as published by the Free Software Foundation version 2.1
00007  *      of the License.
00008  *
00009  * Copyright (c) 2010-2011 Thomas Graf <tgraf@suug.ch>
00010  */
00011 
00012 #include <netlink/cli/utils.h>
00013 #include <netlink/cli/tc.h>
00014 #include <netlink/cli/cls.h>
00015 #include <netlink/route/cls/basic.h>
00016 
00017 static void print_usage(void)
00018 {
00019         printf(
00020 "Usage: nl-cls-add [...] basic [OPTIONS]...\n"
00021 "\n"
00022 "OPTIONS\n"
00023 " -h, --help                Show this help text.\n"
00024 " -t, --target=ID           Target class to send matching packets to\n"
00025 " -e, --ematch=EXPR         Ematch expression\n"
00026 "\n"
00027 "EXAMPLE"
00028 "    # Create a \"catch-all\" classifier, attached to \"q_root\", classyfing\n"
00029 "    # all not yet classified packets to class \"c_default\"\n"
00030 "    nl-cls-add --dev=eth0 --parent=q_root basic --target=c_default\n");
00031 }
00032 
00033 static void parse_argv(struct rtnl_tc *tc, int argc, char **argv)
00034 {
00035         struct rtnl_cls *cls = (struct rtnl_cls *) tc;
00036         struct rtnl_ematch_tree *tree;
00037         uint32_t target;
00038         int err;
00039 
00040         for (;;) {
00041                 int c, optidx = 0;
00042                 enum {
00043                         ARG_TARGET = 257,
00044                         ARG_DEFAULT = 258,
00045                 };
00046                 static struct option long_opts[] = {
00047                         { "help", 0, 0, 'h' },
00048                         { "target", 1, 0, 't' },
00049                         { "ematch", 1, 0, 'e' },
00050                         { 0, 0, 0, 0 }
00051                 };
00052         
00053                 c = getopt_long(argc, argv, "ht:e:", long_opts, &optidx);
00054                 if (c == -1)
00055                         break;
00056 
00057                 switch (c) {
00058                 case 'h':
00059                         print_usage();
00060                         exit(0);
00061 
00062                 case 't':
00063                         if ((err = rtnl_tc_str2handle(optarg, &target)) < 0)
00064                                 nl_cli_fatal(err, "Unable to parse target \"%s\":",
00065                                         optarg, nl_geterror(err));
00066 
00067                         rtnl_basic_set_target(cls, target);
00068                         break;
00069 
00070                 case 'e':
00071                         tree = nl_cli_cls_parse_ematch(cls, optarg);
00072                         rtnl_basic_set_ematch(cls, tree);
00073                         break;
00074                 }
00075         }
00076 }
00077 
00078 static struct nl_cli_tc_module basic_module =
00079 {
00080         .tm_name                = "basic",
00081         .tm_type                = RTNL_TC_TYPE_CLS,
00082         .tm_parse_argv          = parse_argv,
00083 };
00084 
00085 static void __init basic_init(void)
00086 {
00087         nl_cli_tc_register(&basic_module);
00088 }
00089 
00090 static void __exit basic_exit(void)
00091 {
00092         nl_cli_tc_unregister(&basic_module);
00093 }