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