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