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