8#include <linux/netlink.h>
10#include <netlink/cli/utils.h>
11#include <netlink/cli/tc.h>
12#include <netlink/cli/cls.h>
13#include <netlink/cli/link.h>
15#include "nl-priv-dynamic-route/nl-priv-dynamic-route.h"
19static void print_usage(
void)
22"Usage: nl-cls-add [OPTIONS]... classifier [CONFIGURATION]...\n"
25" -q, --quiet Do not print informal notifications.\n"
26" -h, --help Show this help text.\n"
27" -v, --version Show versioning information.\n"
28" --update Update classifier if it exists.\n"
29" --update-only Only update classifier, never create it.\n"
30" -d, --dev=DEV Network device the classifier should be attached to.\n"
31" -i, --id=ID ID of new classifier (default: auto-generated)\n"
32" -p, --parent=ID ID of parent { root | ingress | class-ID }\n"
33" --proto=PROTO Protocol to match (default: all)\n"
34" --prio=PRIO Priority (default: 0)\n"
35" --mtu=SIZE Overwrite MTU (default: MTU of network device)\n"
36" --mpu=SIZE Minimum packet size on the link (default: 0).\n"
37" --overhead=SIZE Overhead in bytes per packet (default: 0).\n"
38" --linktype=TYPE Overwrite linktype (default: type of network device)\n"
41" -h, --help Show help text of classifier specific options.\n"
44" $ nl-cls-add --dev=eth1 --parent=q_root basic --target c_www\n"
50int main(
int argc,
char *argv[])
55 struct nl_cache *link_cache;
61 struct rtnl_tc_ops *ops;
62 int err, flags = NLM_F_CREATE | NLM_F_EXCL;
63 char *kind, *
id = NULL;
65 sock = nl_cli_alloc_socket();
66 nl_cli_connect(sock, NETLINK_ROUTE);
68 link_cache = nl_cli_link_alloc_cache(sock);
70 cls = nl_cli_cls_alloc();
77 ARG_UPDATE_ONLY = 258,
85 static struct option long_opts[] = {
86 {
"quiet", 0, 0,
'q' },
87 {
"help", 0, 0,
'h' },
88 {
"version", 0, 0,
'v' },
90 {
"parent", 1, 0,
'p' },
92 {
"proto", 1, 0, ARG_PROTO },
93 {
"prio", 1, 0, ARG_PRIO },
94 {
"update", 0, 0, ARG_UPDATE },
95 {
"update-only", 0, 0, ARG_UPDATE_ONLY },
96 {
"mtu", 1, 0, ARG_MTU },
97 {
"mpu", 1, 0, ARG_MPU },
98 {
"overhead", 1, 0, ARG_OVERHEAD },
99 {
"linktype", 1, 0, ARG_LINKTYPE },
103 c = getopt_long(argc, argv,
"+qhvd:p:i:",
109 case 'q': quiet = 1;
break;
110 case 'h': print_usage();
break;
111 case 'v': nl_cli_print_version();
break;
112 case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg);
break;
113 case 'p': nl_cli_tc_parse_parent(tc, optarg);
break;
114 case 'i': free(
id);
id = strdup(optarg);
break;
115 case ARG_UPDATE: flags = NLM_F_CREATE;
break;
116 case ARG_UPDATE_ONLY: flags = 0;
break;
117 case ARG_MTU: nl_cli_tc_parse_mtu(tc, optarg);
break;
118 case ARG_MPU: nl_cli_tc_parse_mpu(tc, optarg);
break;
119 case ARG_OVERHEAD: nl_cli_tc_parse_overhead(tc, optarg);
break;
120 case ARG_LINKTYPE: nl_cli_tc_parse_linktype(tc, optarg);
break;
121 case ARG_PROTO: nl_cli_cls_parse_proto(cls, optarg);
break;
132 nl_cli_fatal(EINVAL,
"You must specify a network device (--dev=XXX)");
135 nl_cli_fatal(EINVAL,
"You must specify a parent (--parent=XXX)");
138 nl_cli_tc_parse_handle(tc,
id, 1);
142 kind = argv[optind++];
145 if (!(ops = rtnl_tc_get_ops(tc)))
146 nl_cli_fatal(ENOENT,
"Unknown classifier \"%s\".", kind);
148 if (!(tm = nl_cli_tc_lookup(ops)))
149 nl_cli_fatal(ENOTSUP,
"Classifier type \"%s\" not supported.", kind);
151 tm->tm_parse_argv(tc, argc, argv);
159 nl_cli_fatal(EINVAL,
"Unable to add classifier: %s", nl_geterror(err));
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
uint32_t nl_cli_parse_u32(const char *arg)
Parse a text based 32 bit unsigned integer argument.
int rtnl_cls_add(struct nl_sock *sk, struct rtnl_cls *cls, int flags)
Add/Update classifier.
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
uint32_t rtnl_tc_get_parent(struct rtnl_tc *tc)
Return parent identifier of a traffic control object.
int rtnl_tc_set_kind(struct rtnl_tc *tc, const char *kind)
Define the type of traffic control object.
int rtnl_tc_get_ifindex(struct rtnl_tc *tc)
Return interface index of traffic control object.
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
enum nl_dump_type dp_type
Specifies the type of dump that is requested.