2 * src/nl-cls-add.c Add classifier
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2 of the License.
8 * Copyright (c) 2003-2011 Thomas Graf <tgraf@suug.ch>
11 #include <netlink/cli/utils.h>
12 #include <netlink/cli/tc.h>
13 #include <netlink/cli/cls.h>
14 #include <netlink/cli/link.h>
16 #include <netlink-private/route/tc-api.h>
20 static void print_usage(void)
23 "Usage: nl-cls-add [OPTIONS]... classifier [CONFIGURATION]...\n"
26 " -q, --quiet Do not print informal notifications.\n"
27 " -h, --help Show this help text.\n"
28 " -v, --version Show versioning information.\n"
29 " --update Update classifier if it exists.\n"
30 " --update-only Only update classifier, never create it.\n"
31 " -d, --dev=DEV Network device the classifier should be attached to.\n"
32 " -i, --id=ID ID of new classifier (default: auto-generated)\n"
33 " -p, --parent=ID ID of parent { root | ingress | class-ID }\n"
34 " --protocol=PROTO Protocol to match (default: all)\n"
35 " --prio=PRIO Priority (default: 0)\n"
36 " --mtu=SIZE Overwrite MTU (default: MTU of network device)\n"
37 " --mpu=SIZE Minimum packet size on the link (default: 0).\n"
38 " --overhead=SIZE Overhead in bytes per packet (default: 0).\n"
39 " --linktype=TYPE Overwrite linktype (default: type of network device)\n"
42 " -h, --help Show help text of classifier specific options.\n"
45 " $ nl-cls-add --dev=eth1 --parent=q_root basic --target c_www\n"
51 int main(int argc, char *argv[])
56 struct nl_cache *link_cache;
57 struct nl_dump_params dp = {
58 .dp_type = NL_DUMP_DETAILS,
61 struct nl_cli_tc_module *tm;
62 struct rtnl_tc_ops *ops;
63 int err, flags = NLM_F_CREATE | NLM_F_EXCL;
64 char *kind, *id = NULL;
66 sock = nl_cli_alloc_socket();
67 nl_cli_connect(sock, NETLINK_ROUTE);
69 link_cache = nl_cli_link_alloc_cache(sock);
71 cls = nl_cli_cls_alloc();
72 tc = (struct rtnl_tc *) cls;
78 ARG_UPDATE_ONLY = 258,
86 static struct option long_opts[] = {
87 { "quiet", 0, 0, 'q' },
88 { "help", 0, 0, 'h' },
89 { "version", 0, 0, 'v' },
91 { "parent", 1, 0, 'p' },
93 { "proto", 1, 0, ARG_PROTO },
94 { "prio", 1, 0, ARG_PRIO },
95 { "update", 0, 0, ARG_UPDATE },
96 { "update-only", 0, 0, ARG_UPDATE_ONLY },
97 { "mtu", 1, 0, ARG_MTU },
98 { "mpu", 1, 0, ARG_MPU },
99 { "overhead", 1, 0, ARG_OVERHEAD },
100 { "linktype", 1, 0, ARG_LINKTYPE },
104 c = getopt_long(argc, argv, "+qhvd:p:i:",
110 case 'q': quiet = 1; break;
111 case 'h': print_usage(); break;
112 case 'v': nl_cli_print_version(); break;
113 case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
114 case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
115 case 'i': id = strdup(optarg); break;
116 case ARG_UPDATE: flags = NLM_F_CREATE; break;
117 case ARG_UPDATE_ONLY: flags = 0; break;
118 case ARG_MTU: nl_cli_tc_parse_mtu(tc, optarg); break;
119 case ARG_MPU: nl_cli_tc_parse_mpu(tc, optarg); break;
120 case ARG_OVERHEAD: nl_cli_tc_parse_overhead(tc, optarg); break;
121 case ARG_LINKTYPE: nl_cli_tc_parse_linktype(tc, optarg); break;
122 case ARG_PROTO: nl_cli_cls_parse_proto(cls, optarg); break;
124 rtnl_cls_set_prio(cls, nl_cli_parse_u32(optarg));
132 if (!rtnl_tc_get_ifindex(tc))
133 nl_cli_fatal(EINVAL, "You must specify a network device (--dev=XXX)");
135 if (!rtnl_tc_get_parent(tc))
136 nl_cli_fatal(EINVAL, "You must specify a parent (--parent=XXX)");
139 nl_cli_tc_parse_handle(tc, id, 1);
143 kind = argv[optind++];
144 rtnl_tc_set_kind(tc, kind);
146 if (!(ops = rtnl_tc_get_ops(tc)))
147 nl_cli_fatal(ENOENT, "Unknown classifier \"%s\".", kind);
149 if (!(tm = nl_cli_tc_lookup(ops)))
150 nl_cli_fatal(ENOTSUP, "Classifier type \"%s\" not supported.", kind);
152 tm->tm_parse_argv(tc, argc, argv);
156 nl_object_dump(OBJ_CAST(cls), &dp);
159 if ((err = rtnl_cls_add(sock, cls, flags)) < 0)
160 nl_cli_fatal(EINVAL, "Unable to add classifier: %s", nl_geterror(err));