]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/m_ife.c
treewide: refactor help messages
[mirror_iproute2.git] / tc / m_ife.c
1 /*
2 * m_ife.c IFE actions module
3 *
4 * This program is free software; you can distribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: J Hadi Salim (jhs@mojatatu.com)
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <string.h>
21 #include <linux/netdevice.h>
22
23 #include "rt_names.h"
24 #include "utils.h"
25 #include "tc_util.h"
26 #include <linux/tc_act/tc_ife.h>
27
28 static void ife_explain(void)
29 {
30 fprintf(stderr,
31 "Usage:... ife {decode|encode} [{ALLOW|USE} ATTR] [dst DMAC] [src SMAC] [type TYPE] [CONTROL] [index INDEX]\n"
32 "\tALLOW := Encode direction. Allows encoding specified metadata\n"
33 "\t\t e.g \"allow mark\"\n"
34 "\tUSE := Encode direction. Enforce Static encoding of specified metadata\n"
35 "\t\t e.g \"use mark 0x12\"\n"
36 "\tATTR := mark (32-bit), prio (32-bit), tcindex (16-bit)\n"
37 "\tDMAC := 6 byte Destination MAC address to encode\n"
38 "\tSMAC := optional 6 byte Source MAC address to encode\n"
39 "\tTYPE := optional 16 bit ethertype to encode\n"
40 "\tCONTROL := reclassify|pipe|drop|continue|ok\n"
41 "\tINDEX := optional IFE table index value used\n"
42 "encode is used for sending IFE packets\n"
43 "decode is used for receiving IFE packets\n");
44 }
45
46 static void ife_usage(void)
47 {
48 ife_explain();
49 exit(-1);
50 }
51
52 static int parse_ife(struct action_util *a, int *argc_p, char ***argv_p,
53 int tca_id, struct nlmsghdr *n)
54 {
55 int argc = *argc_p;
56 char **argv = *argv_p;
57 int ok = 0;
58 struct tc_ife p = { 0 };
59 struct rtattr *tail;
60 struct rtattr *tail2;
61 char dbuf[ETH_ALEN];
62 char sbuf[ETH_ALEN];
63 __u16 ife_type = 0;
64 int user_type = 0;
65 __u32 ife_prio = 0;
66 __u32 ife_prio_v = 0;
67 __u32 ife_mark = 0;
68 __u32 ife_mark_v = 0;
69 __u16 ife_tcindex = 0;
70 __u16 ife_tcindex_v = 0;
71 char *daddr = NULL;
72 char *saddr = NULL;
73
74 if (argc <= 0)
75 return -1;
76
77 while (argc > 0) {
78 if (matches(*argv, "ife") == 0) {
79 NEXT_ARG();
80 continue;
81 } else if (matches(*argv, "decode") == 0) {
82 p.flags = IFE_DECODE; /* readability aid */
83 ok++;
84 } else if (matches(*argv, "encode") == 0) {
85 p.flags = IFE_ENCODE;
86 ok++;
87 } else if (matches(*argv, "allow") == 0) {
88 NEXT_ARG();
89 if (matches(*argv, "mark") == 0) {
90 ife_mark = IFE_META_SKBMARK;
91 } else if (matches(*argv, "prio") == 0) {
92 ife_prio = IFE_META_PRIO;
93 } else if (matches(*argv, "tcindex") == 0) {
94 ife_tcindex = IFE_META_TCINDEX;
95 } else {
96 invarg("Illegal meta define", *argv);
97 }
98 } else if (matches(*argv, "use") == 0) {
99 NEXT_ARG();
100 if (matches(*argv, "mark") == 0) {
101 NEXT_ARG();
102 if (get_u32(&ife_mark_v, *argv, 0))
103 invarg("ife mark val is invalid",
104 *argv);
105 } else if (matches(*argv, "prio") == 0) {
106 NEXT_ARG();
107 if (get_u32(&ife_prio_v, *argv, 0))
108 invarg("ife prio val is invalid",
109 *argv);
110 } else if (matches(*argv, "tcindex") == 0) {
111 NEXT_ARG();
112 if (get_u16(&ife_tcindex_v, *argv, 0))
113 invarg("ife tcindex val is invalid",
114 *argv);
115 } else {
116 invarg("Illegal meta use type", *argv);
117 }
118 } else if (matches(*argv, "type") == 0) {
119 NEXT_ARG();
120 if (get_u16(&ife_type, *argv, 0))
121 invarg("ife type is invalid", *argv);
122 fprintf(stderr, "IFE type 0x%04X\n", ife_type);
123 user_type = 1;
124 } else if (matches(*argv, "dst") == 0) {
125 NEXT_ARG();
126 daddr = *argv;
127 if (sscanf(daddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
128 dbuf, dbuf + 1, dbuf + 2,
129 dbuf + 3, dbuf + 4, dbuf + 5) != 6) {
130 invarg("Invalid mac address", *argv);
131 }
132 fprintf(stderr, "dst MAC address <%s>\n", daddr);
133
134 } else if (matches(*argv, "src") == 0) {
135 NEXT_ARG();
136 saddr = *argv;
137 if (sscanf(saddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
138 sbuf, sbuf + 1, sbuf + 2,
139 sbuf + 3, sbuf + 4, sbuf + 5) != 6) {
140 invarg("Invalid mac address", *argv);
141 }
142 fprintf(stderr, "src MAC address <%s>\n", saddr);
143 } else if (matches(*argv, "help") == 0) {
144 ife_usage();
145 } else {
146 break;
147 }
148
149 argc--;
150 argv++;
151 }
152
153 parse_action_control_dflt(&argc, &argv, &p.action, false, TC_ACT_PIPE);
154
155 if (argc) {
156 if (matches(*argv, "index") == 0) {
157 NEXT_ARG();
158 if (get_u32(&p.index, *argv, 0)) {
159 fprintf(stderr, "ife: Illegal \"index\"\n");
160 return -1;
161 }
162 ok++;
163 argc--;
164 argv++;
165 }
166 }
167
168 if (!ok) {
169 fprintf(stderr, "IFE requires decode/encode specified\n");
170 ife_usage();
171 }
172
173 tail = addattr_nest(n, MAX_MSG, tca_id);
174 addattr_l(n, MAX_MSG, TCA_IFE_PARMS, &p, sizeof(p));
175
176 if (!(p.flags & IFE_ENCODE))
177 goto skip_encode;
178
179 if (daddr)
180 addattr_l(n, MAX_MSG, TCA_IFE_DMAC, dbuf, ETH_ALEN);
181 if (user_type)
182 addattr_l(n, MAX_MSG, TCA_IFE_TYPE, &ife_type, 2);
183 else
184 fprintf(stderr, "IFE type 0x%04X\n", ETH_P_IFE);
185 if (saddr)
186 addattr_l(n, MAX_MSG, TCA_IFE_SMAC, sbuf, ETH_ALEN);
187
188 tail2 = addattr_nest(n, MAX_MSG, TCA_IFE_METALST);
189 if (ife_mark || ife_mark_v) {
190 if (ife_mark_v)
191 addattr_l(n, MAX_MSG, IFE_META_SKBMARK, &ife_mark_v, 4);
192 else
193 addattr_l(n, MAX_MSG, IFE_META_SKBMARK, NULL, 0);
194 }
195 if (ife_prio || ife_prio_v) {
196 if (ife_prio_v)
197 addattr_l(n, MAX_MSG, IFE_META_PRIO, &ife_prio_v, 4);
198 else
199 addattr_l(n, MAX_MSG, IFE_META_PRIO, NULL, 0);
200 }
201 if (ife_tcindex || ife_tcindex_v) {
202 if (ife_tcindex_v)
203 addattr_l(n, MAX_MSG, IFE_META_TCINDEX, &ife_tcindex_v,
204 2);
205 else
206 addattr_l(n, MAX_MSG, IFE_META_TCINDEX, NULL, 0);
207 }
208
209 addattr_nest_end(n, tail2);
210
211 skip_encode:
212 addattr_nest_end(n, tail);
213
214 *argc_p = argc;
215 *argv_p = argv;
216 return 0;
217 }
218
219 static int print_ife(struct action_util *au, FILE *f, struct rtattr *arg)
220 {
221 struct tc_ife *p = NULL;
222 struct rtattr *tb[TCA_IFE_MAX + 1];
223 __u16 ife_type = 0;
224 __u32 mmark = 0;
225 __u16 mtcindex = 0;
226 __u32 mprio = 0;
227 int has_optional = 0;
228 SPRINT_BUF(b2);
229
230 if (arg == NULL)
231 return -1;
232
233 parse_rtattr_nested(tb, TCA_IFE_MAX, arg);
234
235 if (tb[TCA_IFE_PARMS] == NULL) {
236 print_string(PRINT_FP, NULL, "%s", "[NULL ife parameters]");
237 return -1;
238 }
239 p = RTA_DATA(tb[TCA_IFE_PARMS]);
240
241 print_string(PRINT_ANY, "kind", "%s ", "ife");
242 print_string(PRINT_ANY, "mode", "%s ",
243 p->flags & IFE_ENCODE ? "encode" : "decode");
244 print_action_control(f, "action ", p->action, " ");
245
246 if (tb[TCA_IFE_TYPE]) {
247 ife_type = rta_getattr_u16(tb[TCA_IFE_TYPE]);
248 has_optional = 1;
249 print_0xhex(PRINT_ANY, "type", "type %#llX ", ife_type);
250 }
251
252 if (has_optional)
253 print_string(PRINT_FP, NULL, "%s\t", _SL_);
254
255 if (tb[TCA_IFE_METALST]) {
256 struct rtattr *metalist[IFE_META_MAX + 1];
257 int len = 0;
258
259 parse_rtattr_nested(metalist, IFE_META_MAX,
260 tb[TCA_IFE_METALST]);
261
262 if (metalist[IFE_META_SKBMARK]) {
263 len = RTA_PAYLOAD(metalist[IFE_META_SKBMARK]);
264 if (len) {
265 mmark = rta_getattr_u32(metalist[IFE_META_SKBMARK]);
266 print_uint(PRINT_ANY, "mark", "use mark %u ",
267 mmark);
268 } else
269 print_string(PRINT_ANY, "mark", "%s mark ",
270 "allow");
271 }
272
273 if (metalist[IFE_META_TCINDEX]) {
274 len = RTA_PAYLOAD(metalist[IFE_META_TCINDEX]);
275 if (len) {
276 mtcindex =
277 rta_getattr_u16(metalist[IFE_META_TCINDEX]);
278 print_uint(PRINT_ANY, "tcindex",
279 "use tcindex %u ", mtcindex);
280 } else
281 print_string(PRINT_ANY, "tcindex",
282 "%s tcindex ", "allow");
283 }
284
285 if (metalist[IFE_META_PRIO]) {
286 len = RTA_PAYLOAD(metalist[IFE_META_PRIO]);
287 if (len) {
288 mprio = rta_getattr_u32(metalist[IFE_META_PRIO]);
289 print_uint(PRINT_ANY, "prio", "use prio %u ",
290 mprio);
291 } else
292 print_string(PRINT_ANY, "prio", "%s prio ",
293 "allow");
294 }
295
296 }
297
298 if (tb[TCA_IFE_DMAC]) {
299 has_optional = 1;
300 print_string(PRINT_ANY, "dst", "dst %s ",
301 ll_addr_n2a(RTA_DATA(tb[TCA_IFE_DMAC]),
302 RTA_PAYLOAD(tb[TCA_IFE_DMAC]), 0, b2,
303 sizeof(b2)));
304 }
305
306 if (tb[TCA_IFE_SMAC]) {
307 has_optional = 1;
308 print_string(PRINT_ANY, "src", "src %s ",
309 ll_addr_n2a(RTA_DATA(tb[TCA_IFE_SMAC]),
310 RTA_PAYLOAD(tb[TCA_IFE_SMAC]), 0, b2,
311 sizeof(b2)));
312 }
313
314 print_string(PRINT_FP, NULL, "%s", _SL_);
315 print_uint(PRINT_ANY, "index", "\t index %u", p->index);
316 print_int(PRINT_ANY, "ref", " ref %d", p->refcnt);
317 print_int(PRINT_ANY, "bind", " bind %d", p->bindcnt);
318
319 if (show_stats) {
320 if (tb[TCA_IFE_TM]) {
321 struct tcf_t *tm = RTA_DATA(tb[TCA_IFE_TM]);
322
323 print_tm(f, tm);
324 }
325 }
326
327 print_string(PRINT_FP, NULL, "%s", _SL_);
328
329 return 0;
330 }
331
332 struct action_util ife_action_util = {
333 .id = "ife",
334 .parse_aopt = parse_ife,
335 .print_aopt = print_ife,
336 };