libnl  3.2.24-rc1
basic.c
1 /*
2  * lib/cli/cls/basic.c basic classifier module for CLI lib
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) 2010-2011 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #include <netlink/cli/utils.h>
13 #include <netlink/cli/tc.h>
14 #include <netlink/cli/cls.h>
15 #include <netlink/route/cls/basic.h>
16 
17 static void print_usage(void)
18 {
19  printf(
20 "Usage: nl-cls-add [...] basic [OPTIONS]...\n"
21 "\n"
22 "OPTIONS\n"
23 " -h, --help Show this help text.\n"
24 " -t, --target=ID Target class to send matching packets to\n"
25 " -e, --ematch=EXPR Ematch expression\n"
26 "\n"
27 "EXAMPLE"
28 " # Create a \"catch-all\" classifier, attached to \"q_root\", classyfing\n"
29 " # all not yet classified packets to class \"c_default\"\n"
30 " nl-cls-add --dev=eth0 --parent=q_root basic --target=c_default\n");
31 }
32 
33 static void parse_argv(struct rtnl_tc *tc, int argc, char **argv)
34 {
35  struct rtnl_cls *cls = (struct rtnl_cls *) tc;
36  struct rtnl_ematch_tree *tree;
37  uint32_t target;
38  int err;
39 
40  for (;;) {
41  int c, optidx = 0;
42  enum {
43  ARG_TARGET = 257,
44  ARG_DEFAULT = 258,
45  };
46  static struct option long_opts[] = {
47  { "help", 0, 0, 'h' },
48  { "target", 1, 0, 't' },
49  { "ematch", 1, 0, 'e' },
50  { 0, 0, 0, 0 }
51  };
52 
53  c = getopt_long(argc, argv, "ht:e:", long_opts, &optidx);
54  if (c == -1)
55  break;
56 
57  switch (c) {
58  case 'h':
59  print_usage();
60  exit(0);
61 
62  case 't':
63  if ((err = rtnl_tc_str2handle(optarg, &target)) < 0)
64  nl_cli_fatal(err, "Unable to parse target \"%s\":",
65  optarg, nl_geterror(err));
66 
67  rtnl_basic_set_target(cls, target);
68  break;
69 
70  case 'e':
71  tree = nl_cli_cls_parse_ematch(cls, optarg);
72  rtnl_basic_set_ematch(cls, tree);
73  break;
74  }
75  }
76 }
77 
78 static struct nl_cli_tc_module basic_module =
79 {
80  .tm_name = "basic",
81  .tm_type = RTNL_TC_TYPE_CLS,
82  .tm_parse_argv = parse_argv,
83 };
84 
85 static void __init basic_init(void)
86 {
87  nl_cli_tc_register(&basic_module);
88 }
89 
90 static void __exit basic_exit(void)
91 {
92  nl_cli_tc_unregister(&basic_module);
93 }