6#include <netlink/cli/utils.h>
7#include <netlink/cli/tc.h>
8#include <netlink/cli/cls.h>
9#include <netlink/cli/link.h>
11#include <netlink-private/route/tc-api.h>
13#include <linux/netlink.h>
17static void print_usage(
void)
20"Usage: nl-cls-add [OPTIONS]... classifier [CONFIGURATION]...\n"
23" -q, --quiet Do not print informal notifications.\n"
24" -h, --help Show this help text.\n"
25" -v, --version Show versioning information.\n"
26" --update Update classifier if it exists.\n"
27" --update-only Only update classifier, never create it.\n"
28" -d, --dev=DEV Network device the classifier should be attached to.\n"
29" -i, --id=ID ID of new classifier (default: auto-generated)\n"
30" -p, --parent=ID ID of parent { root | ingress | class-ID }\n"
31" --proto=PROTO Protocol to match (default: all)\n"
32" --prio=PRIO Priority (default: 0)\n"
33" --mtu=SIZE Overwrite MTU (default: MTU of network device)\n"
34" --mpu=SIZE Minimum packet size on the link (default: 0).\n"
35" --overhead=SIZE Overhead in bytes per packet (default: 0).\n"
36" --linktype=TYPE Overwrite linktype (default: type of network device)\n"
39" -h, --help Show help text of classifier specific options.\n"
42" $ nl-cls-add --dev=eth1 --parent=q_root basic --target c_www\n"
48int main(
int argc,
char *argv[])
53 struct nl_cache *link_cache;
59 struct rtnl_tc_ops *ops;
60 int err, flags = NLM_F_CREATE | NLM_F_EXCL;
61 char *kind, *
id = NULL;
63 sock = nl_cli_alloc_socket();
64 nl_cli_connect(sock, NETLINK_ROUTE);
66 link_cache = nl_cli_link_alloc_cache(sock);
68 cls = nl_cli_cls_alloc();
69 tc = (
struct rtnl_tc *) cls;
75 ARG_UPDATE_ONLY = 258,
83 static struct option long_opts[] = {
84 {
"quiet", 0, 0,
'q' },
85 {
"help", 0, 0,
'h' },
86 {
"version", 0, 0,
'v' },
88 {
"parent", 1, 0,
'p' },
90 {
"proto", 1, 0, ARG_PROTO },
91 {
"prio", 1, 0, ARG_PRIO },
92 {
"update", 0, 0, ARG_UPDATE },
93 {
"update-only", 0, 0, ARG_UPDATE_ONLY },
94 {
"mtu", 1, 0, ARG_MTU },
95 {
"mpu", 1, 0, ARG_MPU },
96 {
"overhead", 1, 0, ARG_OVERHEAD },
97 {
"linktype", 1, 0, ARG_LINKTYPE },
101 c = getopt_long(argc, argv,
"+qhvd:p:i:",
107 case 'q': quiet = 1;
break;
108 case 'h': print_usage();
break;
109 case 'v': nl_cli_print_version();
break;
110 case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg);
break;
111 case 'p': nl_cli_tc_parse_parent(tc, optarg);
break;
112 case 'i':
id = strdup(optarg);
break;
113 case ARG_UPDATE: flags = NLM_F_CREATE;
break;
114 case ARG_UPDATE_ONLY: flags = 0;
break;
115 case ARG_MTU: nl_cli_tc_parse_mtu(tc, optarg);
break;
116 case ARG_MPU: nl_cli_tc_parse_mpu(tc, optarg);
break;
117 case ARG_OVERHEAD: nl_cli_tc_parse_overhead(tc, optarg);
break;
118 case ARG_LINKTYPE: nl_cli_tc_parse_linktype(tc, optarg);
break;
119 case ARG_PROTO: nl_cli_cls_parse_proto(cls, optarg);
break;
130 nl_cli_fatal(EINVAL,
"You must specify a network device (--dev=XXX)");
133 nl_cli_fatal(EINVAL,
"You must specify a parent (--parent=XXX)");
136 nl_cli_tc_parse_handle(tc,
id, 1);
140 kind = argv[optind++];
143 if (!(ops = rtnl_tc_get_ops(tc)))
144 nl_cli_fatal(ENOENT,
"Unknown classifier \"%s\".", kind);
146 if (!(tm = nl_cli_tc_lookup(ops)))
147 nl_cli_fatal(ENOTSUP,
"Classifier type \"%s\" not supported.", kind);
149 tm->tm_parse_argv(tc, argc, argv);
157 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.