libnl 3.7.0
nl-cls-add.c
1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * Copyright (c) 2003-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/cli/link.h>
10
11#include <netlink-private/route/tc-api.h>
12
13#include <linux/netlink.h>
14
15static int quiet = 0;
16
17static void print_usage(void)
18{
19 printf(
20"Usage: nl-cls-add [OPTIONS]... classifier [CONFIGURATION]...\n"
21"\n"
22"OPTIONS\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"
37"\n"
38"CONFIGURATION\n"
39" -h, --help Show help text of classifier specific options.\n"
40"\n"
41"EXAMPLE\n"
42" $ nl-cls-add --dev=eth1 --parent=q_root basic --target c_www\n"
43"\n"
44 );
45 exit(0);
46}
47
48int main(int argc, char *argv[])
49{
50 struct nl_sock *sock;
51 struct rtnl_cls *cls;
52 struct rtnl_tc *tc;
53 struct nl_cache *link_cache;
54 struct nl_dump_params dp = {
56 .dp_fd = stdout,
57 };
58 struct nl_cli_tc_module *tm;
59 struct rtnl_tc_ops *ops;
60 int err, flags = NLM_F_CREATE | NLM_F_EXCL;
61 char *kind, *id = NULL;
62
63 sock = nl_cli_alloc_socket();
64 nl_cli_connect(sock, NETLINK_ROUTE);
65
66 link_cache = nl_cli_link_alloc_cache(sock);
67
68 cls = nl_cli_cls_alloc();
69 tc = (struct rtnl_tc *) cls;
70
71 for (;;) {
72 int c, optidx = 0;
73 enum {
74 ARG_UPDATE = 257,
75 ARG_UPDATE_ONLY = 258,
76 ARG_MTU,
77 ARG_MPU,
78 ARG_OVERHEAD,
79 ARG_LINKTYPE,
80 ARG_PROTO,
81 ARG_PRIO,
82 };
83 static struct option long_opts[] = {
84 { "quiet", 0, 0, 'q' },
85 { "help", 0, 0, 'h' },
86 { "version", 0, 0, 'v' },
87 { "dev", 1, 0, 'd' },
88 { "parent", 1, 0, 'p' },
89 { "id", 1, 0, 'i' },
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 },
98 { 0, 0, 0, 0 }
99 };
100
101 c = getopt_long(argc, argv, "+qhvd:p:i:",
102 long_opts, &optidx);
103 if (c == -1)
104 break;
105
106 switch (c) {
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;
120 case ARG_PRIO:
121 rtnl_cls_set_prio(cls, nl_cli_parse_u32(optarg));
122 break;
123 }
124 }
125
126 if (optind >= argc)
127 print_usage();
128
129 if (!rtnl_tc_get_ifindex(tc))
130 nl_cli_fatal(EINVAL, "You must specify a network device (--dev=XXX)");
131
132 if (!rtnl_tc_get_parent(tc))
133 nl_cli_fatal(EINVAL, "You must specify a parent (--parent=XXX)");
134
135 if (id) {
136 nl_cli_tc_parse_handle(tc, id, 1);
137 free(id);
138 }
139
140 kind = argv[optind++];
141 rtnl_tc_set_kind(tc, kind);
142
143 if (!(ops = rtnl_tc_get_ops(tc)))
144 nl_cli_fatal(ENOENT, "Unknown classifier \"%s\".", kind);
145
146 if (!(tm = nl_cli_tc_lookup(ops)))
147 nl_cli_fatal(ENOTSUP, "Classifier type \"%s\" not supported.", kind);
148
149 tm->tm_parse_argv(tc, argc, argv);
150
151 if (!quiet) {
152 printf("Adding ");
153 nl_object_dump(OBJ_CAST(cls), &dp);
154 }
155
156 if ((err = rtnl_cls_add(sock, cls, flags)) < 0)
157 nl_cli_fatal(EINVAL, "Unable to add classifier: %s", nl_geterror(err));
158
159 return 0;
160}
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:71
uint32_t nl_cli_parse_u32(const char *arg)
Parse a text based 32 bit unsigned integer argument.
Definition: utils.c:36
int rtnl_cls_add(struct nl_sock *sk, struct rtnl_cls *cls, int flags)
Add/Update classifier.
Definition: cls.c:178
void nl_object_dump(struct nl_object *obj, struct nl_dump_params *params)
Dump this object according to the specified parameters.
Definition: object.c:287
uint32_t rtnl_tc_get_parent(struct rtnl_tc *tc)
Return parent identifier of a traffic control object.
Definition: tc.c:511
int rtnl_tc_set_kind(struct rtnl_tc *tc, const char *kind)
Define the type of traffic control object.
Definition: tc.c:523
int rtnl_tc_get_ifindex(struct rtnl_tc *tc)
Return interface index of traffic control object.
Definition: tc.c:287
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition: types.h:17
Dumping parameters.
Definition: types.h:28
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:32