]>
Commit | Line | Data |
---|---|---|
f22716dc | 1 | /* |
be3f512a | 2 | * Copyright (c) 2010, 2011, 2012, 2013 Nicira, Inc. |
f22716dc JP |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include <config.h> | |
18 | ||
19 | #include "ofp-parse.h" | |
20 | ||
00b1c62f | 21 | #include <ctype.h> |
f22716dc JP |
22 | #include <errno.h> |
23 | #include <stdlib.h> | |
24 | ||
daff3353 | 25 | #include "bundle.h" |
10a24935 | 26 | #include "byte-order.h" |
15f1f1b6 | 27 | #include "dynamic-string.h" |
75a75043 | 28 | #include "learn.h" |
6a885fd0 | 29 | #include "meta-flow.h" |
53ddd40a | 30 | #include "multipath.h" |
f25d0cf3 | 31 | #include "netdev.h" |
f393f81e | 32 | #include "nx-match.h" |
f25d0cf3 | 33 | #include "ofp-actions.h" |
f22716dc JP |
34 | #include "ofp-util.h" |
35 | #include "ofpbuf.h" | |
36 | #include "openflow/openflow.h" | |
a944ef40 | 37 | #include "ovs-thread.h" |
f22716dc | 38 | #include "packets.h" |
5a0a5702 | 39 | #include "simap.h" |
f22716dc JP |
40 | #include "socket-util.h" |
41 | #include "vconn.h" | |
f22716dc | 42 | |
bdda5aca BP |
43 | /* Parses 'str' as an 8-bit unsigned integer into '*valuep'. |
44 | * | |
45 | * 'name' describes the value parsed in an error message, if any. | |
46 | * | |
47 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
48 | * error. The caller is responsible for freeing the returned string. */ | |
49 | static char * WARN_UNUSED_RESULT | |
50 | str_to_u8(const char *str, const char *name, uint8_t *valuep) | |
c3636ffc | 51 | { |
638a19b0 | 52 | int value; |
c3636ffc | 53 | |
bdda5aca BP |
54 | if (!str_to_int(str, 0, &value) || value < 0 || value > 255) { |
55 | return xasprintf("invalid %s \"%s\"", name, str); | |
c3636ffc | 56 | } |
bdda5aca BP |
57 | *valuep = value; |
58 | return NULL; | |
c3636ffc BP |
59 | } |
60 | ||
bdda5aca BP |
61 | /* Parses 'str' as a 16-bit unsigned integer into '*valuep'. |
62 | * | |
63 | * 'name' describes the value parsed in an error message, if any. | |
64 | * | |
65 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
66 | * error. The caller is responsible for freeing the returned string. */ | |
67 | static char * WARN_UNUSED_RESULT | |
68 | str_to_u16(const char *str, const char *name, uint16_t *valuep) | |
c3636ffc BP |
69 | { |
70 | int value; | |
71 | ||
72 | if (!str_to_int(str, 0, &value) || value < 0 || value > 65535) { | |
bdda5aca | 73 | return xasprintf("invalid %s \"%s\"", name, str); |
c3636ffc | 74 | } |
bdda5aca BP |
75 | *valuep = value; |
76 | return NULL; | |
c3636ffc BP |
77 | } |
78 | ||
bdda5aca BP |
79 | /* Parses 'str' as a 32-bit unsigned integer into '*valuep'. |
80 | * | |
81 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
82 | * error. The caller is responsible for freeing the returned string. */ | |
83 | static char * WARN_UNUSED_RESULT | |
84 | str_to_u32(const char *str, uint32_t *valuep) | |
f22716dc JP |
85 | { |
86 | char *tail; | |
87 | uint32_t value; | |
88 | ||
c4894ed4 | 89 | if (!str[0]) { |
bdda5aca | 90 | return xstrdup("missing required numeric argument"); |
ce5452cf EJ |
91 | } |
92 | ||
f22716dc JP |
93 | errno = 0; |
94 | value = strtoul(str, &tail, 0); | |
95 | if (errno == EINVAL || errno == ERANGE || *tail) { | |
bdda5aca | 96 | return xasprintf("invalid numeric format %s", str); |
f22716dc | 97 | } |
bdda5aca BP |
98 | *valuep = value; |
99 | return NULL; | |
f22716dc JP |
100 | } |
101 | ||
bdda5aca BP |
102 | /* Parses 'str' as an 64-bit unsigned integer into '*valuep'. |
103 | * | |
104 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
105 | * error. The caller is responsible for freeing the returned string. */ | |
106 | static char * WARN_UNUSED_RESULT | |
107 | str_to_u64(const char *str, uint64_t *valuep) | |
f22716dc JP |
108 | { |
109 | char *tail; | |
110 | uint64_t value; | |
111 | ||
c4894ed4 | 112 | if (!str[0]) { |
bdda5aca | 113 | return xstrdup("missing required numeric argument"); |
c4894ed4 BP |
114 | } |
115 | ||
f22716dc JP |
116 | errno = 0; |
117 | value = strtoull(str, &tail, 0); | |
118 | if (errno == EINVAL || errno == ERANGE || *tail) { | |
bdda5aca | 119 | return xasprintf("invalid numeric format %s", str); |
f22716dc | 120 | } |
bdda5aca BP |
121 | *valuep = value; |
122 | return NULL; | |
f22716dc JP |
123 | } |
124 | ||
bdda5aca BP |
125 | /* Parses 'str' as an 64-bit unsigned integer in network byte order into |
126 | * '*valuep'. | |
127 | * | |
128 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
129 | * error. The caller is responsible for freeing the returned string. */ | |
130 | static char * WARN_UNUSED_RESULT | |
131 | str_to_be64(const char *str, ovs_be64 *valuep) | |
132 | { | |
4be17953 | 133 | uint64_t value = 0; |
bdda5aca BP |
134 | char *error; |
135 | ||
136 | error = str_to_u64(str, &value); | |
137 | if (!error) { | |
138 | *valuep = htonll(value); | |
139 | } | |
140 | return error; | |
141 | } | |
142 | ||
143 | /* Parses 'str' as an Ethernet address into 'mac'. | |
144 | * | |
145 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
146 | * error. The caller is responsible for freeing the returned string. */ | |
147 | static char * WARN_UNUSED_RESULT | |
f22716dc JP |
148 | str_to_mac(const char *str, uint8_t mac[6]) |
149 | { | |
150 | if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac)) | |
151 | != ETH_ADDR_SCAN_COUNT) { | |
bdda5aca | 152 | return xasprintf("invalid mac address %s", str); |
f22716dc | 153 | } |
bdda5aca | 154 | return NULL; |
f22716dc JP |
155 | } |
156 | ||
bdda5aca BP |
157 | /* Parses 'str' as an IP address into '*ip'. |
158 | * | |
159 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
160 | * error. The caller is responsible for freeing the returned string. */ | |
161 | static char * WARN_UNUSED_RESULT | |
6a885fd0 | 162 | str_to_ip(const char *str, ovs_be32 *ip) |
cb8ca532 | 163 | { |
f22716dc | 164 | struct in_addr in_addr; |
f22716dc | 165 | |
6a885fd0 | 166 | if (lookup_ip(str, &in_addr)) { |
bdda5aca | 167 | return xasprintf("%s: could not convert to IP address", str); |
f22716dc JP |
168 | } |
169 | *ip = in_addr.s_addr; | |
bdda5aca | 170 | return NULL; |
d31f1109 JP |
171 | } |
172 | ||
bdda5aca BP |
173 | /* Parses 'arg' as the argument to an "enqueue" action, and appends such an |
174 | * action to 'ofpacts'. | |
175 | * | |
176 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
177 | * error. The caller is responsible for freeing the returned string. */ | |
178 | static char * WARN_UNUSED_RESULT | |
f25d0cf3 | 179 | parse_enqueue(char *arg, struct ofpbuf *ofpacts) |
5682f723 | 180 | { |
333eba21 BP |
181 | char *sp = NULL; |
182 | char *port = strtok_r(arg, ":q", &sp); | |
183 | char *queue = strtok_r(NULL, "", &sp); | |
f25d0cf3 | 184 | struct ofpact_enqueue *enqueue; |
333eba21 BP |
185 | |
186 | if (port == NULL || queue == NULL) { | |
bdda5aca | 187 | return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\""); |
333eba21 BP |
188 | } |
189 | ||
f25d0cf3 | 190 | enqueue = ofpact_put_ENQUEUE(ofpacts); |
bdda5aca BP |
191 | if (!ofputil_port_from_string(port, &enqueue->port)) { |
192 | return xasprintf("%s: enqueue to unknown port", port); | |
193 | } | |
194 | return str_to_u32(queue, &enqueue->queue); | |
5682f723 BP |
195 | } |
196 | ||
bdda5aca BP |
197 | /* Parses 'arg' as the argument to an "output" action, and appends such an |
198 | * action to 'ofpacts'. | |
199 | * | |
200 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
201 | * error. The caller is responsible for freeing the returned string. */ | |
202 | static char * WARN_UNUSED_RESULT | |
203 | parse_output(const char *arg, struct ofpbuf *ofpacts) | |
f694937d EJ |
204 | { |
205 | if (strchr(arg, '[')) { | |
f25d0cf3 | 206 | struct ofpact_output_reg *output_reg; |
f694937d | 207 | |
f25d0cf3 | 208 | output_reg = ofpact_put_OUTPUT_REG(ofpacts); |
f25d0cf3 | 209 | output_reg->max_len = UINT16_MAX; |
bdda5aca | 210 | return mf_parse_subfield(&output_reg->src, arg); |
f694937d | 211 | } else { |
f25d0cf3 BP |
212 | struct ofpact_output *output; |
213 | ||
214 | output = ofpact_put_OUTPUT(ofpacts); | |
f25d0cf3 | 215 | output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0; |
bdda5aca BP |
216 | if (!ofputil_port_from_string(arg, &output->port)) { |
217 | return xasprintf("%s: output to unknown port", arg); | |
218 | } | |
219 | return NULL; | |
f694937d EJ |
220 | } |
221 | } | |
222 | ||
bdda5aca BP |
223 | /* Parses 'arg' as the argument to an "resubmit" action, and appends such an |
224 | * action to 'ofpacts'. | |
225 | * | |
226 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
227 | * error. The caller is responsible for freeing the returned string. */ | |
228 | static char * WARN_UNUSED_RESULT | |
f25d0cf3 | 229 | parse_resubmit(char *arg, struct ofpbuf *ofpacts) |
29901626 | 230 | { |
f25d0cf3 | 231 | struct ofpact_resubmit *resubmit; |
29901626 | 232 | char *in_port_s, *table_s; |
f25d0cf3 BP |
233 | |
234 | resubmit = ofpact_put_RESUBMIT(ofpacts); | |
29901626 BP |
235 | |
236 | in_port_s = strsep(&arg, ","); | |
237 | if (in_port_s && in_port_s[0]) { | |
8010100b | 238 | if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) { |
bdda5aca | 239 | return xasprintf("%s: resubmit to unknown port", in_port_s); |
29901626 BP |
240 | } |
241 | } else { | |
f25d0cf3 | 242 | resubmit->in_port = OFPP_IN_PORT; |
29901626 BP |
243 | } |
244 | ||
245 | table_s = strsep(&arg, ","); | |
bdda5aca | 246 | if (table_s && table_s[0]) { |
4be17953 | 247 | uint32_t table_id = 0; |
bdda5aca BP |
248 | char *error; |
249 | ||
250 | error = str_to_u32(table_s, &table_id); | |
251 | if (error) { | |
252 | return error; | |
253 | } | |
254 | resubmit->table_id = table_id; | |
255 | } else { | |
256 | resubmit->table_id = 255; | |
257 | } | |
29901626 | 258 | |
f25d0cf3 | 259 | if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) { |
bdda5aca BP |
260 | return xstrdup("at least one \"in_port\" or \"table\" must be " |
261 | "specified on resubmit"); | |
29901626 | 262 | } |
bdda5aca | 263 | return NULL; |
333eba21 BP |
264 | } |
265 | ||
bdda5aca BP |
266 | /* Parses 'arg' as the argument to a "note" action, and appends such an action |
267 | * to 'ofpacts'. | |
268 | * | |
269 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
270 | * error. The caller is responsible for freeing the returned string. */ | |
271 | static char * WARN_UNUSED_RESULT | |
f25d0cf3 | 272 | parse_note(const char *arg, struct ofpbuf *ofpacts) |
333eba21 | 273 | { |
f25d0cf3 | 274 | struct ofpact_note *note; |
333eba21 | 275 | |
f25d0cf3 | 276 | note = ofpact_put_NOTE(ofpacts); |
333eba21 BP |
277 | while (*arg != '\0') { |
278 | uint8_t byte; | |
279 | bool ok; | |
280 | ||
281 | if (*arg == '.') { | |
282 | arg++; | |
283 | } | |
284 | if (*arg == '\0') { | |
285 | break; | |
286 | } | |
287 | ||
288 | byte = hexits_value(arg, 2, &ok); | |
289 | if (!ok) { | |
bdda5aca | 290 | return xstrdup("bad hex digit in `note' argument"); |
333eba21 | 291 | } |
f25d0cf3 | 292 | ofpbuf_put(ofpacts, &byte, 1); |
333eba21 | 293 | |
f25d0cf3 BP |
294 | note = ofpacts->l2; |
295 | note->length++; | |
333eba21 | 296 | |
f25d0cf3 | 297 | arg += 2; |
333eba21 | 298 | } |
f25d0cf3 | 299 | ofpact_update_len(ofpacts, ¬e->ofpact); |
bdda5aca | 300 | return NULL; |
333eba21 BP |
301 | } |
302 | ||
bdda5aca BP |
303 | /* Parses 'arg' as the argument to a "fin_timeout" action, and appends such an |
304 | * action to 'ofpacts'. | |
305 | * | |
306 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
307 | * error. The caller is responsible for freeing the returned string. */ | |
308 | static char * WARN_UNUSED_RESULT | |
0e553d9c BP |
309 | parse_fin_timeout(struct ofpbuf *b, char *arg) |
310 | { | |
f25d0cf3 | 311 | struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(b); |
0e553d9c BP |
312 | char *key, *value; |
313 | ||
0e553d9c | 314 | while (ofputil_parse_key_value(&arg, &key, &value)) { |
bdda5aca BP |
315 | char *error; |
316 | ||
0e553d9c | 317 | if (!strcmp(key, "idle_timeout")) { |
bdda5aca | 318 | error = str_to_u16(value, key, &oft->fin_idle_timeout); |
0e553d9c | 319 | } else if (!strcmp(key, "hard_timeout")) { |
bdda5aca | 320 | error = str_to_u16(value, key, &oft->fin_hard_timeout); |
0e553d9c | 321 | } else { |
bdda5aca BP |
322 | error = xasprintf("invalid key '%s' in 'fin_timeout' argument", |
323 | key); | |
324 | } | |
325 | ||
326 | if (error) { | |
327 | return error; | |
0e553d9c BP |
328 | } |
329 | } | |
bdda5aca | 330 | return NULL; |
0e553d9c BP |
331 | } |
332 | ||
bdda5aca BP |
333 | /* Parses 'arg' as the argument to a "controller" action, and appends such an |
334 | * action to 'ofpacts'. | |
335 | * | |
336 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
337 | * error. The caller is responsible for freeing the returned string. */ | |
338 | static char * WARN_UNUSED_RESULT | |
a7349929 BP |
339 | parse_controller(struct ofpbuf *b, char *arg) |
340 | { | |
341 | enum ofp_packet_in_reason reason = OFPR_ACTION; | |
342 | uint16_t controller_id = 0; | |
343 | uint16_t max_len = UINT16_MAX; | |
344 | ||
345 | if (!arg[0]) { | |
346 | /* Use defaults. */ | |
347 | } else if (strspn(arg, "0123456789") == strlen(arg)) { | |
bdda5aca BP |
348 | char *error = str_to_u16(arg, "max_len", &max_len); |
349 | if (error) { | |
350 | return error; | |
351 | } | |
a7349929 BP |
352 | } else { |
353 | char *name, *value; | |
354 | ||
355 | while (ofputil_parse_key_value(&arg, &name, &value)) { | |
356 | if (!strcmp(name, "reason")) { | |
357 | if (!ofputil_packet_in_reason_from_string(value, &reason)) { | |
bdda5aca | 358 | return xasprintf("unknown reason \"%s\"", value); |
a7349929 BP |
359 | } |
360 | } else if (!strcmp(name, "max_len")) { | |
bdda5aca BP |
361 | char *error = str_to_u16(value, "max_len", &max_len); |
362 | if (error) { | |
363 | return error; | |
364 | } | |
a7349929 | 365 | } else if (!strcmp(name, "id")) { |
bdda5aca BP |
366 | char *error = str_to_u16(value, "id", &controller_id); |
367 | if (error) { | |
368 | return error; | |
369 | } | |
a7349929 | 370 | } else { |
bdda5aca BP |
371 | return xasprintf("unknown key \"%s\" parsing controller " |
372 | "action", name); | |
a7349929 BP |
373 | } |
374 | } | |
375 | } | |
376 | ||
377 | if (reason == OFPR_ACTION && controller_id == 0) { | |
f25d0cf3 BP |
378 | struct ofpact_output *output; |
379 | ||
380 | output = ofpact_put_OUTPUT(b); | |
381 | output->port = OFPP_CONTROLLER; | |
382 | output->max_len = max_len; | |
a7349929 | 383 | } else { |
f25d0cf3 | 384 | struct ofpact_controller *controller; |
a7349929 | 385 | |
f25d0cf3 BP |
386 | controller = ofpact_put_CONTROLLER(b); |
387 | controller->max_len = max_len; | |
388 | controller->reason = reason; | |
389 | controller->controller_id = controller_id; | |
a7349929 | 390 | } |
bdda5aca BP |
391 | |
392 | return NULL; | |
a7349929 BP |
393 | } |
394 | ||
c2d967a5 | 395 | static void |
99086062 | 396 | parse_noargs_dec_ttl(struct ofpbuf *b) |
c2d967a5 MM |
397 | { |
398 | struct ofpact_cnt_ids *ids; | |
7bcb1506 | 399 | uint16_t id = 0; |
c2d967a5 MM |
400 | |
401 | ids = ofpact_put_DEC_TTL(b); | |
7bcb1506 IY |
402 | ofpbuf_put(b, &id, sizeof id); |
403 | ids = b->l2; | |
404 | ids->n_controllers++; | |
405 | ofpact_update_len(b, &ids->ofpact); | |
406 | } | |
c2d967a5 | 407 | |
bdda5aca BP |
408 | /* Parses 'arg' as the argument to a "dec_ttl" action, and appends such an |
409 | * action to 'ofpacts'. | |
410 | * | |
411 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
412 | * error. The caller is responsible for freeing the returned string. */ | |
413 | static char * WARN_UNUSED_RESULT | |
99086062 | 414 | parse_dec_ttl(struct ofpbuf *b, char *arg) |
7bcb1506 | 415 | { |
c2d967a5 | 416 | if (*arg == '\0') { |
99086062 | 417 | parse_noargs_dec_ttl(b); |
c2d967a5 | 418 | } else { |
7bcb1506 | 419 | struct ofpact_cnt_ids *ids; |
c2d967a5 MM |
420 | char *cntr; |
421 | ||
7bcb1506 | 422 | ids = ofpact_put_DEC_TTL(b); |
c2d967a5 MM |
423 | ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS; |
424 | for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL; | |
425 | cntr = strtok_r(NULL, ", ", &arg)) { | |
426 | uint16_t id = atoi(cntr); | |
427 | ||
428 | ofpbuf_put(b, &id, sizeof id); | |
429 | ids = b->l2; | |
430 | ids->n_controllers++; | |
431 | } | |
432 | if (!ids->n_controllers) { | |
bdda5aca BP |
433 | return xstrdup("dec_ttl_cnt_ids: expected at least one controller " |
434 | "id."); | |
c2d967a5 | 435 | } |
7bcb1506 | 436 | ofpact_update_len(b, &ids->ofpact); |
c2d967a5 | 437 | } |
bdda5aca | 438 | return NULL; |
c2d967a5 MM |
439 | } |
440 | ||
bdda5aca BP |
441 | /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an |
442 | * action to 'ofpacts'. | |
443 | * | |
444 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
445 | * error. The caller is responsible for freeing the returned string. */ | |
446 | static char * WARN_UNUSED_RESULT | |
0f3f3c3d SH |
447 | parse_set_mpls_ttl(struct ofpbuf *b, const char *arg) |
448 | { | |
449 | struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(b); | |
450 | ||
451 | if (*arg == '\0') { | |
bdda5aca | 452 | return xstrdup("parse_set_mpls_ttl: expected ttl."); |
0f3f3c3d SH |
453 | } |
454 | ||
455 | mpls_ttl->ttl = atoi(arg); | |
bdda5aca | 456 | return NULL; |
0f3f3c3d SH |
457 | } |
458 | ||
bdda5aca BP |
459 | /* Parses a "set_field" action with argument 'arg', appending the parsed |
460 | * action to 'ofpacts'. | |
461 | * | |
462 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
463 | * error. The caller is responsible for freeing the returned string. */ | |
464 | static char * WARN_UNUSED_RESULT | |
db0b6c29 JR |
465 | set_field_parse__(char *arg, struct ofpbuf *ofpacts, |
466 | enum ofputil_protocol *usable_protocols) | |
f5c45121 | 467 | { |
f5c45121 SH |
468 | struct ofpact_reg_load *load = ofpact_put_REG_LOAD(ofpacts); |
469 | char *value; | |
470 | char *delim; | |
471 | char *key; | |
472 | const struct mf_field *mf; | |
bdda5aca | 473 | char *error; |
f5c45121 SH |
474 | union mf_value mf_value; |
475 | ||
bdda5aca BP |
476 | value = arg; |
477 | delim = strstr(arg, "->"); | |
f5c45121 | 478 | if (!delim) { |
bdda5aca | 479 | return xasprintf("%s: missing `->'", arg); |
f5c45121 SH |
480 | } |
481 | if (strlen(delim) <= strlen("->")) { | |
bdda5aca | 482 | return xasprintf("%s: missing field name following `->'", arg); |
f5c45121 SH |
483 | } |
484 | ||
485 | key = delim + strlen("->"); | |
486 | mf = mf_from_name(key); | |
487 | if (!mf) { | |
bdda5aca | 488 | return xasprintf("%s is not a valid OXM field name", key); |
f5c45121 SH |
489 | } |
490 | if (!mf->writable) { | |
bdda5aca | 491 | return xasprintf("%s is read-only", key); |
f5c45121 SH |
492 | } |
493 | ||
494 | delim[0] = '\0'; | |
495 | error = mf_parse_value(mf, value, &mf_value); | |
496 | if (error) { | |
bdda5aca | 497 | return error; |
f5c45121 SH |
498 | } |
499 | if (!mf_is_value_valid(mf, &mf_value)) { | |
bdda5aca | 500 | return xasprintf("%s is not a valid value for field %s", value, key); |
f5c45121 SH |
501 | } |
502 | ofpact_set_field_init(load, mf, &mf_value); | |
bdda5aca | 503 | |
db0b6c29 | 504 | *usable_protocols &= mf->usable_protocols; |
bdda5aca | 505 | return NULL; |
f5c45121 SH |
506 | } |
507 | ||
bdda5aca BP |
508 | /* Parses 'arg' as the argument to a "set_field" action, and appends such an |
509 | * action to 'ofpacts'. | |
510 | * | |
511 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
512 | * error. The caller is responsible for freeing the returned string. */ | |
513 | static char * WARN_UNUSED_RESULT | |
db0b6c29 JR |
514 | set_field_parse(const char *arg, struct ofpbuf *ofpacts, |
515 | enum ofputil_protocol *usable_protocols) | |
bdda5aca BP |
516 | { |
517 | char *copy = xstrdup(arg); | |
db0b6c29 | 518 | char *error = set_field_parse__(copy, ofpacts, usable_protocols); |
bdda5aca BP |
519 | free(copy); |
520 | return error; | |
521 | } | |
522 | ||
523 | /* Parses 'arg' as the argument to a "write_metadata" instruction, and appends | |
524 | * such an action to 'ofpacts'. | |
525 | * | |
526 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
527 | * error. The caller is responsible for freeing the returned string. */ | |
528 | static char * WARN_UNUSED_RESULT | |
4cceacb9 JS |
529 | parse_metadata(struct ofpbuf *b, char *arg) |
530 | { | |
531 | struct ofpact_metadata *om; | |
532 | char *mask = strchr(arg, '/'); | |
533 | ||
534 | om = ofpact_put_WRITE_METADATA(b); | |
535 | ||
536 | if (mask) { | |
bdda5aca BP |
537 | char *error; |
538 | ||
4cceacb9 | 539 | *mask = '\0'; |
bdda5aca BP |
540 | error = str_to_be64(mask + 1, &om->mask); |
541 | if (error) { | |
542 | return error; | |
543 | } | |
4cceacb9 | 544 | } else { |
b8266395 | 545 | om->mask = OVS_BE64_MAX; |
4cceacb9 JS |
546 | } |
547 | ||
bdda5aca | 548 | return str_to_be64(arg, &om->metadata); |
4cceacb9 JS |
549 | } |
550 | ||
bdda5aca BP |
551 | /* Parses 'arg' as the argument to a "sample" action, and appends such an |
552 | * action to 'ofpacts'. | |
553 | * | |
554 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
555 | * error. The caller is responsible for freeing the returned string. */ | |
556 | static char * WARN_UNUSED_RESULT | |
29089a54 RL |
557 | parse_sample(struct ofpbuf *b, char *arg) |
558 | { | |
559 | struct ofpact_sample *os = ofpact_put_SAMPLE(b); | |
560 | char *key, *value; | |
561 | ||
562 | while (ofputil_parse_key_value(&arg, &key, &value)) { | |
bdda5aca BP |
563 | char *error = NULL; |
564 | ||
29089a54 | 565 | if (!strcmp(key, "probability")) { |
bdda5aca BP |
566 | error = str_to_u16(value, "probability", &os->probability); |
567 | if (!error && os->probability == 0) { | |
568 | error = xasprintf("invalid probability value \"%s\"", value); | |
29089a54 RL |
569 | } |
570 | } else if (!strcmp(key, "collector_set_id")) { | |
bdda5aca | 571 | error = str_to_u32(value, &os->collector_set_id); |
29089a54 | 572 | } else if (!strcmp(key, "obs_domain_id")) { |
bdda5aca | 573 | error = str_to_u32(value, &os->obs_domain_id); |
29089a54 | 574 | } else if (!strcmp(key, "obs_point_id")) { |
bdda5aca | 575 | error = str_to_u32(value, &os->obs_point_id); |
29089a54 | 576 | } else { |
bdda5aca BP |
577 | error = xasprintf("invalid key \"%s\" in \"sample\" argument", |
578 | key); | |
579 | } | |
580 | if (error) { | |
581 | return error; | |
29089a54 RL |
582 | } |
583 | } | |
584 | if (os->probability == 0) { | |
bdda5aca | 585 | return xstrdup("non-zero \"probability\" must be specified on sample"); |
29089a54 | 586 | } |
bdda5aca | 587 | return NULL; |
29089a54 RL |
588 | } |
589 | ||
bdda5aca BP |
590 | /* Parses 'arg' as the argument to action 'code', and appends such an action to |
591 | * 'ofpacts'. | |
592 | * | |
593 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
594 | * error. The caller is responsible for freeing the returned string. */ | |
595 | static char * WARN_UNUSED_RESULT | |
dd43a558 | 596 | parse_named_action(enum ofputil_action_code code, |
db0b6c29 JR |
597 | char *arg, struct ofpbuf *ofpacts, |
598 | enum ofputil_protocol *usable_protocols) | |
333eba21 | 599 | { |
bdda5aca | 600 | size_t orig_size = ofpacts->size; |
f25d0cf3 | 601 | struct ofpact_tunnel *tunnel; |
bdda5aca | 602 | char *error = NULL; |
4be17953 EJ |
603 | uint16_t ethertype = 0; |
604 | uint16_t vid = 0; | |
0c20dbe4 | 605 | uint8_t tos = 0, ecn, ttl; |
4be17953 | 606 | uint8_t pcp = 0; |
333eba21 BP |
607 | |
608 | switch (code) { | |
690a61c5 BP |
609 | case OFPUTIL_ACTION_INVALID: |
610 | NOT_REACHED(); | |
611 | ||
08f94c0e | 612 | case OFPUTIL_OFPAT10_OUTPUT: |
d01c980f | 613 | case OFPUTIL_OFPAT11_OUTPUT: |
bdda5aca | 614 | error = parse_output(arg, ofpacts); |
333eba21 BP |
615 | break; |
616 | ||
08f94c0e | 617 | case OFPUTIL_OFPAT10_SET_VLAN_VID: |
d01c980f | 618 | case OFPUTIL_OFPAT11_SET_VLAN_VID: |
bdda5aca BP |
619 | error = str_to_u16(arg, "VLAN VID", &vid); |
620 | if (error) { | |
621 | return error; | |
622 | } | |
623 | ||
f25d0cf3 | 624 | if (vid & ~VLAN_VID_MASK) { |
bdda5aca | 625 | return xasprintf("%s: not a valid VLAN VID", arg); |
f25d0cf3 BP |
626 | } |
627 | ofpact_put_SET_VLAN_VID(ofpacts)->vlan_vid = vid; | |
333eba21 BP |
628 | break; |
629 | ||
08f94c0e | 630 | case OFPUTIL_OFPAT10_SET_VLAN_PCP: |
d01c980f | 631 | case OFPUTIL_OFPAT11_SET_VLAN_PCP: |
bdda5aca BP |
632 | error = str_to_u8(arg, "VLAN PCP", &pcp); |
633 | if (error) { | |
634 | return error; | |
635 | } | |
636 | ||
f25d0cf3 | 637 | if (pcp & ~7) { |
bdda5aca | 638 | return xasprintf("%s: not a valid VLAN PCP", arg); |
f25d0cf3 BP |
639 | } |
640 | ofpact_put_SET_VLAN_PCP(ofpacts)->vlan_pcp = pcp; | |
333eba21 BP |
641 | break; |
642 | ||
78a3fff6 | 643 | case OFPUTIL_OFPAT12_SET_FIELD: |
db0b6c29 | 644 | return set_field_parse(arg, ofpacts, usable_protocols); |
78a3fff6 | 645 | |
08f94c0e | 646 | case OFPUTIL_OFPAT10_STRIP_VLAN: |
8e61c110 | 647 | case OFPUTIL_OFPAT11_POP_VLAN: |
f25d0cf3 | 648 | ofpact_put_STRIP_VLAN(ofpacts); |
333eba21 BP |
649 | break; |
650 | ||
3e34fbdd | 651 | case OFPUTIL_OFPAT11_PUSH_VLAN: |
db0b6c29 | 652 | *usable_protocols &= OFPUTIL_P_OF11_UP; |
bdda5aca BP |
653 | error = str_to_u16(arg, "ethertype", ðertype); |
654 | if (error) { | |
655 | return error; | |
656 | } | |
657 | ||
3e34fbdd | 658 | if (ethertype != ETH_TYPE_VLAN_8021Q) { |
5dca28b5 | 659 | /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */ |
bdda5aca | 660 | return xasprintf("%s: not a valid VLAN ethertype", arg); |
3e34fbdd | 661 | } |
bdda5aca | 662 | |
3e34fbdd IY |
663 | ofpact_put_PUSH_VLAN(ofpacts); |
664 | break; | |
665 | ||
276c4e7a | 666 | case OFPUTIL_OFPAT11_SET_QUEUE: |
bdda5aca | 667 | error = str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id); |
276c4e7a SH |
668 | break; |
669 | ||
08f94c0e | 670 | case OFPUTIL_OFPAT10_SET_DL_SRC: |
d01c980f | 671 | case OFPUTIL_OFPAT11_SET_DL_SRC: |
bdda5aca | 672 | error = str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac); |
f25d0cf3 BP |
673 | break; |
674 | ||
08f94c0e | 675 | case OFPUTIL_OFPAT10_SET_DL_DST: |
d01c980f | 676 | case OFPUTIL_OFPAT11_SET_DL_DST: |
bdda5aca | 677 | error = str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac); |
333eba21 BP |
678 | break; |
679 | ||
08f94c0e | 680 | case OFPUTIL_OFPAT10_SET_NW_SRC: |
d01c980f | 681 | case OFPUTIL_OFPAT11_SET_NW_SRC: |
bdda5aca | 682 | error = str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4); |
f25d0cf3 BP |
683 | break; |
684 | ||
08f94c0e | 685 | case OFPUTIL_OFPAT10_SET_NW_DST: |
d01c980f | 686 | case OFPUTIL_OFPAT11_SET_NW_DST: |
bdda5aca | 687 | error = str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4); |
333eba21 BP |
688 | break; |
689 | ||
08f94c0e | 690 | case OFPUTIL_OFPAT10_SET_NW_TOS: |
d01c980f | 691 | case OFPUTIL_OFPAT11_SET_NW_TOS: |
bdda5aca BP |
692 | error = str_to_u8(arg, "TOS", &tos); |
693 | if (error) { | |
694 | return error; | |
695 | } | |
696 | ||
f25d0cf3 | 697 | if (tos & ~IP_DSCP_MASK) { |
bdda5aca | 698 | return xasprintf("%s: not a valid TOS", arg); |
f25d0cf3 | 699 | } |
04f01c24 | 700 | ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos; |
333eba21 BP |
701 | break; |
702 | ||
ff14eb7a JR |
703 | case OFPUTIL_OFPAT11_SET_NW_ECN: |
704 | error = str_to_u8(arg, "ECN", &ecn); | |
705 | if (error) { | |
706 | return error; | |
707 | } | |
708 | ||
709 | if (ecn & ~IP_ECN_MASK) { | |
710 | return xasprintf("%s: not a valid ECN", arg); | |
711 | } | |
712 | ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn; | |
713 | break; | |
714 | ||
0c20dbe4 JR |
715 | case OFPUTIL_OFPAT11_SET_NW_TTL: |
716 | error = str_to_u8(arg, "TTL", &ttl); | |
717 | if (error) { | |
718 | return error; | |
719 | } | |
720 | ||
721 | ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl; | |
722 | break; | |
723 | ||
7bcb1506 | 724 | case OFPUTIL_OFPAT11_DEC_NW_TTL: |
68194f84 | 725 | NOT_REACHED(); |
7bcb1506 | 726 | |
08f94c0e | 727 | case OFPUTIL_OFPAT10_SET_TP_SRC: |
d01c980f | 728 | case OFPUTIL_OFPAT11_SET_TP_SRC: |
bdda5aca BP |
729 | error = str_to_u16(arg, "source port", |
730 | &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port); | |
f25d0cf3 BP |
731 | break; |
732 | ||
08f94c0e | 733 | case OFPUTIL_OFPAT10_SET_TP_DST: |
d01c980f | 734 | case OFPUTIL_OFPAT11_SET_TP_DST: |
bdda5aca BP |
735 | error = str_to_u16(arg, "destination port", |
736 | &ofpact_put_SET_L4_DST_PORT(ofpacts)->port); | |
333eba21 BP |
737 | break; |
738 | ||
08f94c0e | 739 | case OFPUTIL_OFPAT10_ENQUEUE: |
bdda5aca | 740 | error = parse_enqueue(arg, ofpacts); |
333eba21 BP |
741 | break; |
742 | ||
743 | case OFPUTIL_NXAST_RESUBMIT: | |
bdda5aca | 744 | error = parse_resubmit(arg, ofpacts); |
333eba21 BP |
745 | break; |
746 | ||
747 | case OFPUTIL_NXAST_SET_TUNNEL: | |
f25d0cf3 BP |
748 | case OFPUTIL_NXAST_SET_TUNNEL64: |
749 | tunnel = ofpact_put_SET_TUNNEL(ofpacts); | |
750 | tunnel->ofpact.compat = code; | |
bdda5aca | 751 | error = str_to_u64(arg, &tunnel->tun_id); |
333eba21 BP |
752 | break; |
753 | ||
4cceacb9 | 754 | case OFPUTIL_NXAST_WRITE_METADATA: |
bdda5aca | 755 | error = parse_metadata(ofpacts, arg); |
4cceacb9 JS |
756 | break; |
757 | ||
333eba21 | 758 | case OFPUTIL_NXAST_SET_QUEUE: |
bdda5aca | 759 | error = str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id); |
333eba21 BP |
760 | break; |
761 | ||
762 | case OFPUTIL_NXAST_POP_QUEUE: | |
f25d0cf3 | 763 | ofpact_put_POP_QUEUE(ofpacts); |
333eba21 BP |
764 | break; |
765 | ||
766 | case OFPUTIL_NXAST_REG_MOVE: | |
bdda5aca | 767 | error = nxm_parse_reg_move(ofpact_put_REG_MOVE(ofpacts), arg); |
333eba21 BP |
768 | break; |
769 | ||
770 | case OFPUTIL_NXAST_REG_LOAD: | |
bdda5aca | 771 | error = nxm_parse_reg_load(ofpact_put_REG_LOAD(ofpacts), arg); |
333eba21 BP |
772 | break; |
773 | ||
774 | case OFPUTIL_NXAST_NOTE: | |
bdda5aca | 775 | error = parse_note(arg, ofpacts); |
93996add BP |
776 | break; |
777 | ||
333eba21 | 778 | case OFPUTIL_NXAST_MULTIPATH: |
bdda5aca | 779 | error = multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg); |
333eba21 BP |
780 | break; |
781 | ||
333eba21 | 782 | case OFPUTIL_NXAST_BUNDLE: |
bdda5aca | 783 | error = bundle_parse(arg, ofpacts); |
333eba21 BP |
784 | break; |
785 | ||
786 | case OFPUTIL_NXAST_BUNDLE_LOAD: | |
bdda5aca | 787 | error = bundle_parse_load(arg, ofpacts); |
333eba21 BP |
788 | break; |
789 | ||
790 | case OFPUTIL_NXAST_RESUBMIT_TABLE: | |
791 | case OFPUTIL_NXAST_OUTPUT_REG: | |
c2d967a5 | 792 | case OFPUTIL_NXAST_DEC_TTL_CNT_IDS: |
333eba21 | 793 | NOT_REACHED(); |
75a75043 BP |
794 | |
795 | case OFPUTIL_NXAST_LEARN: | |
bdda5aca | 796 | error = learn_parse(arg, ofpacts); |
75a75043 | 797 | break; |
a61680c6 | 798 | |
848e8809 | 799 | case OFPUTIL_NXAST_EXIT: |
f25d0cf3 | 800 | ofpact_put_EXIT(ofpacts); |
848e8809 | 801 | break; |
f0fd1a17 PS |
802 | |
803 | case OFPUTIL_NXAST_DEC_TTL: | |
bdda5aca | 804 | error = parse_dec_ttl(ofpacts, arg); |
f0fd1a17 | 805 | break; |
0e553d9c | 806 | |
0f3f3c3d SH |
807 | case OFPUTIL_NXAST_SET_MPLS_TTL: |
808 | case OFPUTIL_OFPAT11_SET_MPLS_TTL: | |
bdda5aca | 809 | error = parse_set_mpls_ttl(ofpacts, arg); |
0f3f3c3d SH |
810 | break; |
811 | ||
b676167a SH |
812 | case OFPUTIL_OFPAT11_DEC_MPLS_TTL: |
813 | case OFPUTIL_NXAST_DEC_MPLS_TTL: | |
814 | ofpact_put_DEC_MPLS_TTL(ofpacts); | |
815 | break; | |
816 | ||
0e553d9c | 817 | case OFPUTIL_NXAST_FIN_TIMEOUT: |
bdda5aca | 818 | error = parse_fin_timeout(ofpacts, arg); |
0e553d9c | 819 | break; |
a7349929 BP |
820 | |
821 | case OFPUTIL_NXAST_CONTROLLER: | |
bdda5aca | 822 | error = parse_controller(ofpacts, arg); |
a7349929 | 823 | break; |
b02475c5 SH |
824 | |
825 | case OFPUTIL_OFPAT11_PUSH_MPLS: | |
826 | case OFPUTIL_NXAST_PUSH_MPLS: | |
bdda5aca BP |
827 | error = str_to_u16(arg, "push_mpls", ðertype); |
828 | if (!error) { | |
829 | ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype); | |
830 | } | |
b02475c5 SH |
831 | break; |
832 | ||
833 | case OFPUTIL_OFPAT11_POP_MPLS: | |
834 | case OFPUTIL_NXAST_POP_MPLS: | |
bdda5aca BP |
835 | error = str_to_u16(arg, "pop_mpls", ðertype); |
836 | if (!error) { | |
837 | ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype); | |
838 | } | |
b02475c5 | 839 | break; |
29089a54 | 840 | |
7395c052 NZ |
841 | case OFPUTIL_OFPAT11_GROUP: |
842 | error = str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id); | |
843 | break; | |
844 | ||
bd85dac1 | 845 | case OFPUTIL_NXAST_STACK_PUSH: |
bdda5aca | 846 | error = nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg); |
bd85dac1 AZ |
847 | break; |
848 | case OFPUTIL_NXAST_STACK_POP: | |
bdda5aca | 849 | error = nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg); |
bd85dac1 | 850 | break; |
29089a54 RL |
851 | |
852 | case OFPUTIL_NXAST_SAMPLE: | |
bdda5aca | 853 | error = parse_sample(ofpacts, arg); |
29089a54 | 854 | break; |
333eba21 | 855 | } |
bdda5aca BP |
856 | |
857 | if (error) { | |
858 | ofpacts->size = orig_size; | |
859 | } | |
860 | return error; | |
333eba21 BP |
861 | } |
862 | ||
bdda5aca BP |
863 | /* Parses action 'act', with argument 'arg', and appends a parsed version to |
864 | * 'ofpacts'. | |
865 | * | |
866 | * 'n_actions' specifies the number of actions already parsed (for proper | |
867 | * handling of "drop" actions). | |
868 | * | |
869 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
870 | * error. The caller is responsible for freeing the returned string. */ | |
871 | static char * WARN_UNUSED_RESULT | |
dd43a558 | 872 | str_to_ofpact__(char *pos, char *act, char *arg, |
db0b6c29 JR |
873 | struct ofpbuf *ofpacts, int n_actions, |
874 | enum ofputil_protocol *usable_protocols) | |
8dd54666 IY |
875 | { |
876 | int code = ofputil_action_code_from_name(act); | |
877 | if (code >= 0) { | |
db0b6c29 | 878 | return parse_named_action(code, arg, ofpacts, usable_protocols); |
8dd54666 IY |
879 | } else if (!strcasecmp(act, "drop")) { |
880 | if (n_actions) { | |
bdda5aca BP |
881 | return xstrdup("Drop actions must not be preceded by other " |
882 | "actions"); | |
8dd54666 | 883 | } else if (ofputil_parse_key_value(&pos, &act, &arg)) { |
bdda5aca BP |
884 | return xstrdup("Drop actions must not be followed by other " |
885 | "actions"); | |
8dd54666 | 886 | } |
8dd54666 | 887 | } else { |
4e022ec0 | 888 | ofp_port_t port; |
8010100b | 889 | if (ofputil_port_from_string(act, &port)) { |
8dd54666 IY |
890 | ofpact_put_OUTPUT(ofpacts)->port = port; |
891 | } else { | |
bdda5aca | 892 | return xasprintf("Unknown action: %s", act); |
8dd54666 IY |
893 | } |
894 | } | |
895 | ||
bdda5aca | 896 | return NULL; |
8dd54666 IY |
897 | } |
898 | ||
bdda5aca BP |
899 | /* Parses 'str' as a series of actions, and appends them to 'ofpacts'. |
900 | * | |
901 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
902 | * error. The caller is responsible for freeing the returned string. */ | |
903 | static char * WARN_UNUSED_RESULT | |
7fdb60a7 SH |
904 | str_to_ofpacts__(char *str, struct ofpbuf *ofpacts, |
905 | enum ofputil_protocol *usable_protocols) | |
f22716dc | 906 | { |
bdda5aca | 907 | size_t orig_size = ofpacts->size; |
0ff22822 | 908 | char *pos, *act, *arg; |
f22716dc JP |
909 | int n_actions; |
910 | ||
53ddd40a | 911 | pos = str; |
d13803eb | 912 | n_actions = 0; |
0ff22822 | 913 | while (ofputil_parse_key_value(&pos, &act, &arg)) { |
db0b6c29 JR |
914 | char *error = str_to_ofpact__(pos, act, arg, ofpacts, n_actions, |
915 | usable_protocols); | |
bdda5aca BP |
916 | if (error) { |
917 | ofpacts->size = orig_size; | |
918 | return error; | |
8dd54666 IY |
919 | } |
920 | n_actions++; | |
921 | } | |
4cceacb9 | 922 | |
7fdb60a7 SH |
923 | ofpact_pad(ofpacts); |
924 | return NULL; | |
925 | } | |
926 | ||
927 | ||
928 | /* Parses 'str' as a series of actions, and appends them to 'ofpacts'. | |
929 | * | |
930 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
931 | * error. The caller is responsible for freeing the returned string. */ | |
932 | static char * WARN_UNUSED_RESULT | |
933 | str_to_ofpacts(char *str, struct ofpbuf *ofpacts, | |
934 | enum ofputil_protocol *usable_protocols) | |
935 | { | |
936 | size_t orig_size = ofpacts->size; | |
937 | char *error_s; | |
938 | enum ofperr error; | |
939 | ||
940 | error_s = str_to_ofpacts__(str, ofpacts, usable_protocols); | |
941 | if (error_s) { | |
942 | return error_s; | |
943 | } | |
944 | ||
4cceacb9 JS |
945 | error = ofpacts_verify(ofpacts->data, ofpacts->size); |
946 | if (error) { | |
bdda5aca BP |
947 | ofpacts->size = orig_size; |
948 | return xstrdup("Incorrect action ordering"); | |
4cceacb9 JS |
949 | } |
950 | ||
bdda5aca | 951 | return NULL; |
8dd54666 IY |
952 | } |
953 | ||
bdda5aca BP |
954 | /* Parses 'arg' as the argument to instruction 'type', and appends such an |
955 | * instruction to 'ofpacts'. | |
956 | * | |
957 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
958 | * error. The caller is responsible for freeing the returned string. */ | |
959 | static char * WARN_UNUSED_RESULT | |
8dd54666 | 960 | parse_named_instruction(enum ovs_instruction_type type, |
db0b6c29 JR |
961 | char *arg, struct ofpbuf *ofpacts, |
962 | enum ofputil_protocol *usable_protocols) | |
8dd54666 | 963 | { |
bdda5aca | 964 | char *error_s = NULL; |
4cceacb9 JS |
965 | enum ofperr error; |
966 | ||
db0b6c29 JR |
967 | *usable_protocols &= OFPUTIL_P_OF11_UP; |
968 | ||
8dd54666 IY |
969 | switch (type) { |
970 | case OVSINST_OFPIT11_APPLY_ACTIONS: | |
971 | NOT_REACHED(); /* This case is handled by str_to_inst_ofpacts() */ | |
972 | break; | |
973 | ||
7fdb60a7 SH |
974 | case OVSINST_OFPIT11_WRITE_ACTIONS: { |
975 | struct ofpact_nest *on; | |
918c10ae | 976 | size_t ofs; |
7fdb60a7 | 977 | |
918c10ae YT |
978 | ofpact_pad(ofpacts); |
979 | ofs = ofpacts->size; | |
7fdb60a7 SH |
980 | on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS, |
981 | offsetof(struct ofpact_nest, actions)); | |
982 | error_s = str_to_ofpacts__(arg, ofpacts, usable_protocols); | |
983 | ||
984 | on = ofpbuf_at_assert(ofpacts, ofs, sizeof *on); | |
985 | on->ofpact.len = ofpacts->size - ofs; | |
986 | ||
987 | if (error_s) { | |
988 | ofpacts->size = ofs; | |
989 | } | |
8dd54666 | 990 | break; |
7fdb60a7 | 991 | } |
8dd54666 IY |
992 | |
993 | case OVSINST_OFPIT11_CLEAR_ACTIONS: | |
b19e8793 | 994 | ofpact_put_CLEAR_ACTIONS(ofpacts); |
8dd54666 IY |
995 | break; |
996 | ||
638a19b0 | 997 | case OVSINST_OFPIT13_METER: |
db0b6c29 | 998 | *usable_protocols &= OFPUTIL_P_OF13_UP; |
bdda5aca | 999 | error_s = str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id); |
638a19b0 JR |
1000 | break; |
1001 | ||
8dd54666 | 1002 | case OVSINST_OFPIT11_WRITE_METADATA: |
db0b6c29 | 1003 | *usable_protocols &= OFPUTIL_P_NXM_OF11_UP; |
bdda5aca | 1004 | error_s = parse_metadata(ofpacts, arg); |
8dd54666 IY |
1005 | break; |
1006 | ||
1007 | case OVSINST_OFPIT11_GOTO_TABLE: { | |
1008 | struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts); | |
1009 | char *table_s = strsep(&arg, ","); | |
1010 | if (!table_s || !table_s[0]) { | |
bdda5aca | 1011 | return xstrdup("instruction goto-table needs table id"); |
8dd54666 | 1012 | } |
bdda5aca | 1013 | error_s = str_to_u8(table_s, "table", &ogt->table_id); |
8dd54666 IY |
1014 | break; |
1015 | } | |
1016 | } | |
4cceacb9 | 1017 | |
bdda5aca BP |
1018 | if (error_s) { |
1019 | return error_s; | |
1020 | } | |
1021 | ||
4cceacb9 JS |
1022 | /* If write_metadata is specified as an action AND an instruction, ofpacts |
1023 | could be invalid. */ | |
1024 | error = ofpacts_verify(ofpacts->data, ofpacts->size); | |
1025 | if (error) { | |
bdda5aca | 1026 | return xstrdup("Incorrect instruction ordering"); |
4cceacb9 | 1027 | } |
bdda5aca | 1028 | return NULL; |
8dd54666 IY |
1029 | } |
1030 | ||
bdda5aca BP |
1031 | /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'. |
1032 | * | |
1033 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
1034 | * error. The caller is responsible for freeing the returned string. */ | |
1035 | static char * WARN_UNUSED_RESULT | |
db0b6c29 JR |
1036 | str_to_inst_ofpacts(char *str, struct ofpbuf *ofpacts, |
1037 | enum ofputil_protocol *usable_protocols) | |
8dd54666 | 1038 | { |
bdda5aca | 1039 | size_t orig_size = ofpacts->size; |
8dd54666 IY |
1040 | char *pos, *inst, *arg; |
1041 | int type; | |
1042 | const char *prev_inst = NULL; | |
1043 | int prev_type = -1; | |
1044 | int n_actions = 0; | |
1045 | ||
1046 | pos = str; | |
1047 | while (ofputil_parse_key_value(&pos, &inst, &arg)) { | |
ba0bc9b0 | 1048 | type = ovs_instruction_type_from_name(inst); |
8dd54666 | 1049 | if (type < 0) { |
db0b6c29 JR |
1050 | char *error = str_to_ofpact__(pos, inst, arg, ofpacts, n_actions, |
1051 | usable_protocols); | |
bdda5aca BP |
1052 | if (error) { |
1053 | ofpacts->size = orig_size; | |
1054 | return error; | |
8dd54666 IY |
1055 | } |
1056 | ||
1057 | type = OVSINST_OFPIT11_APPLY_ACTIONS; | |
1058 | if (prev_type == type) { | |
1059 | n_actions++; | |
1060 | continue; | |
c6100d92 | 1061 | } |
8dd54666 | 1062 | } else if (type == OVSINST_OFPIT11_APPLY_ACTIONS) { |
bdda5aca BP |
1063 | ofpacts->size = orig_size; |
1064 | return xasprintf("%s isn't supported. Just write actions then " | |
1065 | "it is interpreted as apply_actions", inst); | |
8dd54666 | 1066 | } else { |
db0b6c29 JR |
1067 | char *error = parse_named_instruction(type, arg, ofpacts, |
1068 | usable_protocols); | |
bdda5aca BP |
1069 | if (error) { |
1070 | ofpacts->size = orig_size; | |
1071 | return error; | |
1072 | } | |
8dd54666 IY |
1073 | } |
1074 | ||
8dd54666 | 1075 | if (type <= prev_type) { |
bdda5aca BP |
1076 | ofpacts->size = orig_size; |
1077 | if (type == prev_type) { | |
1078 | return xasprintf("instruction %s may be specified only once", | |
1079 | inst); | |
1080 | } else { | |
1081 | return xasprintf("instruction %s must be specified before %s", | |
1082 | inst, prev_inst); | |
1083 | } | |
8dd54666 IY |
1084 | } |
1085 | ||
1086 | prev_inst = inst; | |
1087 | prev_type = type; | |
d13803eb | 1088 | n_actions++; |
f22716dc | 1089 | } |
f25d0cf3 | 1090 | ofpact_pad(ofpacts); |
bdda5aca BP |
1091 | |
1092 | return NULL; | |
f22716dc JP |
1093 | } |
1094 | ||
1095 | struct protocol { | |
1096 | const char *name; | |
1097 | uint16_t dl_type; | |
1098 | uint8_t nw_proto; | |
1099 | }; | |
1100 | ||
1101 | static bool | |
1102 | parse_protocol(const char *name, const struct protocol **p_out) | |
1103 | { | |
1104 | static const struct protocol protocols[] = { | |
1105 | { "ip", ETH_TYPE_IP, 0 }, | |
1106 | { "arp", ETH_TYPE_ARP, 0 }, | |
6767a2cc JP |
1107 | { "icmp", ETH_TYPE_IP, IPPROTO_ICMP }, |
1108 | { "tcp", ETH_TYPE_IP, IPPROTO_TCP }, | |
1109 | { "udp", ETH_TYPE_IP, IPPROTO_UDP }, | |
0d56eaf2 | 1110 | { "sctp", ETH_TYPE_IP, IPPROTO_SCTP }, |
d31f1109 JP |
1111 | { "ipv6", ETH_TYPE_IPV6, 0 }, |
1112 | { "ip6", ETH_TYPE_IPV6, 0 }, | |
1113 | { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 }, | |
1114 | { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP }, | |
1115 | { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP }, | |
0d56eaf2 | 1116 | { "sctp6", ETH_TYPE_IPV6, IPPROTO_SCTP }, |
8087f5ff | 1117 | { "rarp", ETH_TYPE_RARP, 0}, |
b02475c5 SH |
1118 | { "mpls", ETH_TYPE_MPLS, 0 }, |
1119 | { "mplsm", ETH_TYPE_MPLS_MCAST, 0 }, | |
1120 | }; | |
f22716dc JP |
1121 | const struct protocol *p; |
1122 | ||
1123 | for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) { | |
1124 | if (!strcmp(p->name, name)) { | |
1125 | *p_out = p; | |
1126 | return true; | |
1127 | } | |
1128 | } | |
1129 | *p_out = NULL; | |
1130 | return false; | |
1131 | } | |
1132 | ||
bdda5aca | 1133 | /* Parses 's' as the (possibly masked) value of field 'mf', and updates |
db0b6c29 JR |
1134 | * 'match' appropriately. Restricts the set of usable protocols to ones |
1135 | * supporting the parsed field. | |
bdda5aca BP |
1136 | * |
1137 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
1138 | * error. The caller is responsible for freeing the returned string. */ | |
1139 | static char * WARN_UNUSED_RESULT | |
db0b6c29 JR |
1140 | parse_field(const struct mf_field *mf, const char *s, struct match *match, |
1141 | enum ofputil_protocol *usable_protocols) | |
8050b31d | 1142 | { |
6a885fd0 BP |
1143 | union mf_value value, mask; |
1144 | char *error; | |
bad68a99 | 1145 | |
6a885fd0 | 1146 | error = mf_parse(mf, s, &value, &mask); |
bdda5aca | 1147 | if (!error) { |
db0b6c29 | 1148 | *usable_protocols &= mf_set(mf, &value, &mask, match); |
8050b31d | 1149 | } |
bdda5aca | 1150 | return error; |
00b1c62f BP |
1151 | } |
1152 | ||
bdda5aca | 1153 | static char * WARN_UNUSED_RESULT |
db0b6c29 JR |
1154 | parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string, |
1155 | enum ofputil_protocol *usable_protocols) | |
f22716dc | 1156 | { |
c821124b BP |
1157 | enum { |
1158 | F_OUT_PORT = 1 << 0, | |
1159 | F_ACTIONS = 1 << 1, | |
c821124b | 1160 | F_TIMEOUT = 1 << 3, |
a993007b BP |
1161 | F_PRIORITY = 1 << 4, |
1162 | F_FLAGS = 1 << 5, | |
c821124b | 1163 | } fields; |
f22716dc | 1164 | char *save_ptr = NULL; |
75a75043 | 1165 | char *act_str = NULL; |
f22716dc | 1166 | char *name; |
f22716dc | 1167 | |
db0b6c29 JR |
1168 | *usable_protocols = OFPUTIL_P_ANY; |
1169 | ||
c821124b BP |
1170 | switch (command) { |
1171 | case -1: | |
1172 | fields = F_OUT_PORT; | |
1173 | break; | |
1174 | ||
1175 | case OFPFC_ADD: | |
a993007b | 1176 | fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS; |
c821124b BP |
1177 | break; |
1178 | ||
1179 | case OFPFC_DELETE: | |
1180 | fields = F_OUT_PORT; | |
1181 | break; | |
1182 | ||
1183 | case OFPFC_DELETE_STRICT: | |
1184 | fields = F_OUT_PORT | F_PRIORITY; | |
1185 | break; | |
1186 | ||
1187 | case OFPFC_MODIFY: | |
a993007b | 1188 | fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS; |
c821124b BP |
1189 | break; |
1190 | ||
1191 | case OFPFC_MODIFY_STRICT: | |
a993007b | 1192 | fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS; |
c821124b BP |
1193 | break; |
1194 | ||
1195 | default: | |
1196 | NOT_REACHED(); | |
1197 | } | |
1198 | ||
81a76618 BP |
1199 | match_init_catchall(&fm->match); |
1200 | fm->priority = OFP_DEFAULT_PRIORITY; | |
88ca35ee | 1201 | fm->cookie = htonll(0); |
e729e793 | 1202 | fm->cookie_mask = htonll(0); |
623e1caf JP |
1203 | if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) { |
1204 | /* For modify, by default, don't update the cookie. */ | |
b8266395 | 1205 | fm->new_cookie = OVS_BE64_MAX; |
623e1caf JP |
1206 | } else{ |
1207 | fm->new_cookie = htonll(0); | |
1208 | } | |
23342857 | 1209 | fm->modify_cookie = false; |
6c1491fb | 1210 | fm->table_id = 0xff; |
c821124b | 1211 | fm->command = command; |
88ca35ee BP |
1212 | fm->idle_timeout = OFP_FLOW_PERMANENT; |
1213 | fm->hard_timeout = OFP_FLOW_PERMANENT; | |
1214 | fm->buffer_id = UINT32_MAX; | |
7f05e7ab | 1215 | fm->out_port = OFPP_ANY; |
88ca35ee | 1216 | fm->flags = 0; |
7395c052 | 1217 | fm->out_group = OFPG11_ANY; |
c821124b | 1218 | if (fields & F_ACTIONS) { |
c821124b | 1219 | act_str = strstr(string, "action"); |
f22716dc | 1220 | if (!act_str) { |
bdda5aca | 1221 | return xstrdup("must specify an action"); |
f22716dc JP |
1222 | } |
1223 | *act_str = '\0'; | |
1224 | ||
1225 | act_str = strchr(act_str + 1, '='); | |
1226 | if (!act_str) { | |
bdda5aca | 1227 | return xstrdup("must specify an action"); |
f22716dc JP |
1228 | } |
1229 | ||
1230 | act_str++; | |
f22716dc | 1231 | } |
f22716dc JP |
1232 | for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name; |
1233 | name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) { | |
1234 | const struct protocol *p; | |
bdda5aca | 1235 | char *error = NULL; |
f22716dc JP |
1236 | |
1237 | if (parse_protocol(name, &p)) { | |
81a76618 | 1238 | match_set_dl_type(&fm->match, htons(p->dl_type)); |
f22716dc | 1239 | if (p->nw_proto) { |
81a76618 | 1240 | match_set_nw_proto(&fm->match, p->nw_proto); |
f22716dc | 1241 | } |
a993007b | 1242 | } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) { |
0fb88c18 | 1243 | fm->flags |= OFPUTIL_FF_SEND_FLOW_REM; |
a993007b | 1244 | } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) { |
0fb88c18 | 1245 | fm->flags |= OFPUTIL_FF_CHECK_OVERLAP; |
2e1ae200 | 1246 | } else if (fields & F_FLAGS && !strcmp(name, "reset_counts")) { |
0fb88c18 | 1247 | fm->flags |= OFPUTIL_FF_RESET_COUNTS; |
db0b6c29 | 1248 | *usable_protocols &= OFPUTIL_P_OF12_UP; |
2e1ae200 | 1249 | } else if (fields & F_FLAGS && !strcmp(name, "no_packet_counts")) { |
0fb88c18 | 1250 | fm->flags |= OFPUTIL_FF_NO_PKT_COUNTS; |
db0b6c29 | 1251 | *usable_protocols &= OFPUTIL_P_OF13_UP; |
2e1ae200 | 1252 | } else if (fields & F_FLAGS && !strcmp(name, "no_byte_counts")) { |
0fb88c18 | 1253 | fm->flags |= OFPUTIL_FF_NO_BYT_COUNTS; |
db0b6c29 | 1254 | *usable_protocols &= OFPUTIL_P_OF13_UP; |
f22716dc | 1255 | } else { |
f22716dc JP |
1256 | char *value; |
1257 | ||
1258 | value = strtok_r(NULL, ", \t\r\n", &save_ptr); | |
1259 | if (!value) { | |
bdda5aca | 1260 | return xasprintf("field %s missing value", name); |
f22716dc JP |
1261 | } |
1262 | ||
6c1491fb | 1263 | if (!strcmp(name, "table")) { |
bdda5aca | 1264 | error = str_to_u8(value, "table", &fm->table_id); |
db0b6c29 JR |
1265 | if (fm->table_id != 0xff) { |
1266 | *usable_protocols &= OFPUTIL_P_TID; | |
1267 | } | |
8050b31d | 1268 | } else if (!strcmp(name, "out_port")) { |
be3f512a | 1269 | if (!ofputil_port_from_string(value, &fm->out_port)) { |
bdda5aca BP |
1270 | error = xasprintf("%s is not a valid OpenFlow port", |
1271 | value); | |
c6100d92 | 1272 | } |
c821124b | 1273 | } else if (fields & F_PRIORITY && !strcmp(name, "priority")) { |
4be17953 | 1274 | uint16_t priority = 0; |
bdda5aca BP |
1275 | |
1276 | error = str_to_u16(value, name, &priority); | |
1277 | fm->priority = priority; | |
c821124b | 1278 | } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) { |
bdda5aca | 1279 | error = str_to_u16(value, name, &fm->idle_timeout); |
c821124b | 1280 | } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) { |
bdda5aca | 1281 | error = str_to_u16(value, name, &fm->hard_timeout); |
e729e793 JP |
1282 | } else if (!strcmp(name, "cookie")) { |
1283 | char *mask = strchr(value, '/'); | |
623e1caf | 1284 | |
e729e793 | 1285 | if (mask) { |
623e1caf | 1286 | /* A mask means we're searching for a cookie. */ |
e729e793 | 1287 | if (command == OFPFC_ADD) { |
bdda5aca BP |
1288 | return xstrdup("flow additions cannot use " |
1289 | "a cookie mask"); | |
e729e793 JP |
1290 | } |
1291 | *mask = '\0'; | |
bdda5aca BP |
1292 | error = str_to_be64(value, &fm->cookie); |
1293 | if (error) { | |
1294 | return error; | |
1295 | } | |
1296 | error = str_to_be64(mask + 1, &fm->cookie_mask); | |
db0b6c29 JR |
1297 | |
1298 | /* Matching of the cookie is only supported through NXM or | |
1299 | * OF1.1+. */ | |
1300 | if (fm->cookie_mask != htonll(0)) { | |
1301 | *usable_protocols &= OFPUTIL_P_NXM_OF11_UP; | |
1302 | } | |
e729e793 | 1303 | } else { |
623e1caf JP |
1304 | /* No mask means that the cookie is being set. */ |
1305 | if (command != OFPFC_ADD && command != OFPFC_MODIFY | |
bdda5aca BP |
1306 | && command != OFPFC_MODIFY_STRICT) { |
1307 | return xstrdup("cannot set cookie"); | |
623e1caf | 1308 | } |
bdda5aca | 1309 | error = str_to_be64(value, &fm->new_cookie); |
23342857 | 1310 | fm->modify_cookie = true; |
e729e793 | 1311 | } |
6a885fd0 | 1312 | } else if (mf_from_name(name)) { |
db0b6c29 JR |
1313 | error = parse_field(mf_from_name(name), value, &fm->match, |
1314 | usable_protocols); | |
2c6d8411 BP |
1315 | } else if (!strcmp(name, "duration") |
1316 | || !strcmp(name, "n_packets") | |
146356e9 JP |
1317 | || !strcmp(name, "n_bytes") |
1318 | || !strcmp(name, "idle_age") | |
1319 | || !strcmp(name, "hard_age")) { | |
2c6d8411 BP |
1320 | /* Ignore these, so that users can feed the output of |
1321 | * "ovs-ofctl dump-flows" back into commands that parse | |
1322 | * flows. */ | |
f22716dc | 1323 | } else { |
bdda5aca BP |
1324 | error = xasprintf("unknown keyword %s", name); |
1325 | } | |
1326 | ||
1327 | if (error) { | |
1328 | return error; | |
f22716dc JP |
1329 | } |
1330 | } | |
1331 | } | |
db0b6c29 JR |
1332 | /* Check for usable protocol interdependencies between match fields. */ |
1333 | if (fm->match.flow.dl_type == htons(ETH_TYPE_IPV6)) { | |
1334 | const struct flow_wildcards *wc = &fm->match.wc; | |
1335 | /* Only NXM and OXM support matching L3 and L4 fields within IPv6. | |
1336 | * | |
1337 | * (IPv6 specific fields as well as arp_sha, arp_tha, nw_frag, and | |
1338 | * nw_ttl are covered elsewhere so they don't need to be included in | |
1339 | * this test too.) | |
1340 | */ | |
1341 | if (wc->masks.nw_proto || wc->masks.nw_tos | |
1342 | || wc->masks.tp_src || wc->masks.tp_dst) { | |
1343 | *usable_protocols &= OFPUTIL_P_NXM_OXM_ANY; | |
1344 | } | |
1345 | } | |
b8266395 | 1346 | if (!fm->cookie_mask && fm->new_cookie == OVS_BE64_MAX |
bdda5aca | 1347 | && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) { |
623e1caf JP |
1348 | /* On modifies without a mask, we are supposed to add a flow if |
1349 | * one does not exist. If a cookie wasn't been specified, use a | |
1350 | * default of zero. */ | |
1351 | fm->new_cookie = htonll(0); | |
1352 | } | |
75a75043 | 1353 | if (fields & F_ACTIONS) { |
f25d0cf3 | 1354 | struct ofpbuf ofpacts; |
bdda5aca | 1355 | char *error; |
75a75043 | 1356 | |
f25d0cf3 | 1357 | ofpbuf_init(&ofpacts, 32); |
db0b6c29 | 1358 | error = str_to_inst_ofpacts(act_str, &ofpacts, usable_protocols); |
bdda5aca BP |
1359 | if (!error) { |
1360 | enum ofperr err; | |
1361 | ||
1362 | err = ofpacts_check(ofpacts.data, ofpacts.size, &fm->match.flow, | |
1363 | OFPP_MAX, 0); | |
1364 | if (err) { | |
1365 | error = xasprintf("actions are invalid with specified match " | |
1366 | "(%s)", ofperr_to_string(err)); | |
1367 | } | |
1368 | } | |
1369 | if (error) { | |
1370 | ofpbuf_uninit(&ofpacts); | |
1371 | return error; | |
b019d34d SH |
1372 | } |
1373 | ||
bdda5aca BP |
1374 | fm->ofpacts_len = ofpacts.size; |
1375 | fm->ofpacts = ofpbuf_steal_data(&ofpacts); | |
75a75043 | 1376 | } else { |
f25d0cf3 BP |
1377 | fm->ofpacts_len = 0; |
1378 | fm->ofpacts = NULL; | |
75a75043 | 1379 | } |
ec610b7b | 1380 | |
bdda5aca | 1381 | return NULL; |
f22716dc | 1382 | } |
15f1f1b6 | 1383 | |
638a19b0 | 1384 | /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man |
bdda5aca | 1385 | * page) into 'fm' for sending the specified flow_mod 'command' to a switch. |
db0b6c29 | 1386 | * Returns the set of usable protocols in '*usable_protocols'. |
bdda5aca BP |
1387 | * |
1388 | * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_* | |
1389 | * constant for 'command'. To parse syntax for an OFPST_FLOW or | |
1390 | * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. | |
1391 | * | |
1392 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
1393 | * error. The caller is responsible for freeing the returned string. */ | |
1394 | char * WARN_UNUSED_RESULT | |
db0b6c29 JR |
1395 | parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_, |
1396 | enum ofputil_protocol *usable_protocols) | |
bdda5aca BP |
1397 | { |
1398 | char *string = xstrdup(str_); | |
1399 | char *error; | |
1400 | ||
db0b6c29 | 1401 | error = parse_ofp_str__(fm, command, string, usable_protocols); |
bdda5aca BP |
1402 | if (error) { |
1403 | fm->ofpacts = NULL; | |
1404 | fm->ofpacts_len = 0; | |
1405 | } | |
1406 | ||
1407 | free(string); | |
1408 | return error; | |
1409 | } | |
1410 | ||
1411 | static char * WARN_UNUSED_RESULT | |
1412 | parse_ofp_meter_mod_str__(struct ofputil_meter_mod *mm, char *string, | |
db0b6c29 JR |
1413 | struct ofpbuf *bands, int command, |
1414 | enum ofputil_protocol *usable_protocols) | |
638a19b0 JR |
1415 | { |
1416 | enum { | |
1417 | F_METER = 1 << 0, | |
1418 | F_FLAGS = 1 << 1, | |
1419 | F_BANDS = 1 << 2, | |
1420 | } fields; | |
638a19b0 JR |
1421 | char *save_ptr = NULL; |
1422 | char *band_str = NULL; | |
1423 | char *name; | |
1424 | ||
db0b6c29 | 1425 | /* Meters require at least OF 1.3. */ |
040f4db8 | 1426 | *usable_protocols = OFPUTIL_P_OF13_UP; |
db0b6c29 | 1427 | |
638a19b0 JR |
1428 | switch (command) { |
1429 | case -1: | |
1430 | fields = F_METER; | |
1431 | break; | |
1432 | ||
1433 | case OFPMC13_ADD: | |
1434 | fields = F_METER | F_FLAGS | F_BANDS; | |
1435 | break; | |
1436 | ||
1437 | case OFPMC13_DELETE: | |
1438 | fields = F_METER; | |
1439 | break; | |
1440 | ||
1441 | case OFPMC13_MODIFY: | |
1442 | fields = F_METER | F_FLAGS | F_BANDS; | |
1443 | break; | |
1444 | ||
1445 | default: | |
1446 | NOT_REACHED(); | |
1447 | } | |
1448 | ||
1449 | mm->command = command; | |
1450 | mm->meter.meter_id = 0; | |
1451 | mm->meter.flags = 0; | |
1452 | if (fields & F_BANDS) { | |
1453 | band_str = strstr(string, "band"); | |
1454 | if (!band_str) { | |
bdda5aca | 1455 | return xstrdup("must specify bands"); |
638a19b0 JR |
1456 | } |
1457 | *band_str = '\0'; | |
1458 | ||
1459 | band_str = strchr(band_str + 1, '='); | |
1460 | if (!band_str) { | |
bdda5aca | 1461 | return xstrdup("must specify bands"); |
638a19b0 JR |
1462 | } |
1463 | ||
1464 | band_str++; | |
1465 | } | |
1466 | for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name; | |
1467 | name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) { | |
1468 | ||
1469 | if (fields & F_FLAGS && !strcmp(name, "kbps")) { | |
1470 | mm->meter.flags |= OFPMF13_KBPS; | |
1471 | } else if (fields & F_FLAGS && !strcmp(name, "pktps")) { | |
1472 | mm->meter.flags |= OFPMF13_PKTPS; | |
1473 | } else if (fields & F_FLAGS && !strcmp(name, "burst")) { | |
1474 | mm->meter.flags |= OFPMF13_BURST; | |
1475 | } else if (fields & F_FLAGS && !strcmp(name, "stats")) { | |
1476 | mm->meter.flags |= OFPMF13_STATS; | |
1477 | } else { | |
1478 | char *value; | |
1479 | ||
1480 | value = strtok_r(NULL, ", \t\r\n", &save_ptr); | |
1481 | if (!value) { | |
bdda5aca | 1482 | return xasprintf("field %s missing value", name); |
638a19b0 JR |
1483 | } |
1484 | ||
1485 | if (!strcmp(name, "meter")) { | |
1486 | if (!strcmp(value, "all")) { | |
1487 | mm->meter.meter_id = OFPM13_ALL; | |
1488 | } else if (!strcmp(value, "controller")) { | |
1489 | mm->meter.meter_id = OFPM13_CONTROLLER; | |
1490 | } else if (!strcmp(value, "slowpath")) { | |
1491 | mm->meter.meter_id = OFPM13_SLOWPATH; | |
1492 | } else { | |
bdda5aca BP |
1493 | char *error = str_to_u32(value, &mm->meter.meter_id); |
1494 | if (error) { | |
1495 | return error; | |
1496 | } | |
638a19b0 | 1497 | if (mm->meter.meter_id > OFPM13_MAX) { |
bdda5aca | 1498 | return xasprintf("invalid value for %s", name); |
638a19b0 JR |
1499 | } |
1500 | } | |
1501 | } else { | |
bdda5aca | 1502 | return xasprintf("unknown keyword %s", name); |
638a19b0 JR |
1503 | } |
1504 | } | |
1505 | } | |
1506 | if (fields & F_METER && !mm->meter.meter_id) { | |
bdda5aca | 1507 | return xstrdup("must specify 'meter'"); |
638a19b0 JR |
1508 | } |
1509 | if (fields & F_FLAGS && !mm->meter.flags) { | |
bdda5aca | 1510 | return xstrdup("meter must specify either 'kbps' or 'pktps'"); |
638a19b0 JR |
1511 | } |
1512 | ||
1513 | if (fields & F_BANDS) { | |
638a19b0 JR |
1514 | uint16_t n_bands = 0; |
1515 | struct ofputil_meter_band *band = NULL; | |
1516 | int i; | |
1517 | ||
638a19b0 JR |
1518 | for (name = strtok_r(band_str, "=, \t\r\n", &save_ptr); name; |
1519 | name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) { | |
1520 | ||
1521 | char *value; | |
1522 | ||
1523 | value = strtok_r(NULL, ", \t\r\n", &save_ptr); | |
1524 | if (!value) { | |
bdda5aca | 1525 | return xasprintf("field %s missing value", name); |
638a19b0 JR |
1526 | } |
1527 | ||
1528 | if (!strcmp(name, "type")) { | |
1529 | /* Start a new band */ | |
bdda5aca | 1530 | band = ofpbuf_put_zeros(bands, sizeof *band); |
638a19b0 JR |
1531 | n_bands++; |
1532 | ||
1533 | if (!strcmp(value, "drop")) { | |
1534 | band->type = OFPMBT13_DROP; | |
1535 | } else if (!strcmp(value, "dscp_remark")) { | |
1536 | band->type = OFPMBT13_DSCP_REMARK; | |
1537 | } else { | |
bdda5aca | 1538 | return xasprintf("field %s unknown value %s", name, value); |
638a19b0 JR |
1539 | } |
1540 | } else if (!band || !band->type) { | |
bdda5aca | 1541 | return xstrdup("band must start with the 'type' keyword"); |
638a19b0 | 1542 | } else if (!strcmp(name, "rate")) { |
bdda5aca BP |
1543 | char *error = str_to_u32(value, &band->rate); |
1544 | if (error) { | |
1545 | return error; | |
1546 | } | |
638a19b0 | 1547 | } else if (!strcmp(name, "burst_size")) { |
bdda5aca BP |
1548 | char *error = str_to_u32(value, &band->burst_size); |
1549 | if (error) { | |
1550 | return error; | |
1551 | } | |
638a19b0 | 1552 | } else if (!strcmp(name, "prec_level")) { |
bdda5aca BP |
1553 | char *error = str_to_u8(value, name, &band->prec_level); |
1554 | if (error) { | |
1555 | return error; | |
1556 | } | |
638a19b0 | 1557 | } else { |
bdda5aca | 1558 | return xasprintf("unknown keyword %s", name); |
638a19b0 JR |
1559 | } |
1560 | } | |
1561 | /* validate bands */ | |
1562 | if (!n_bands) { | |
bdda5aca | 1563 | return xstrdup("meter must have bands"); |
638a19b0 JR |
1564 | } |
1565 | ||
1566 | mm->meter.n_bands = n_bands; | |
bdda5aca | 1567 | mm->meter.bands = ofpbuf_steal_data(bands); |
638a19b0 JR |
1568 | |
1569 | for (i = 0; i < n_bands; ++i) { | |
1570 | band = &mm->meter.bands[i]; | |
1571 | ||
1572 | if (!band->type) { | |
bdda5aca | 1573 | return xstrdup("band must have 'type'"); |
638a19b0 JR |
1574 | } |
1575 | if (band->type == OFPMBT13_DSCP_REMARK) { | |
1576 | if (!band->prec_level) { | |
bdda5aca BP |
1577 | return xstrdup("'dscp_remark' band must have" |
1578 | " 'prec_level'"); | |
638a19b0 JR |
1579 | } |
1580 | } else { | |
1581 | if (band->prec_level) { | |
bdda5aca BP |
1582 | return xstrdup("Only 'dscp_remark' band may have" |
1583 | " 'prec_level'"); | |
638a19b0 JR |
1584 | } |
1585 | } | |
1586 | if (!band->rate) { | |
bdda5aca | 1587 | return xstrdup("band must have 'rate'"); |
638a19b0 JR |
1588 | } |
1589 | if (mm->meter.flags & OFPMF13_BURST) { | |
1590 | if (!band->burst_size) { | |
bdda5aca BP |
1591 | return xstrdup("band must have 'burst_size' " |
1592 | "when 'burst' flag is set"); | |
638a19b0 JR |
1593 | } |
1594 | } else { | |
1595 | if (band->burst_size) { | |
bdda5aca BP |
1596 | return xstrdup("band may have 'burst_size' only " |
1597 | "when 'burst' flag is set"); | |
638a19b0 JR |
1598 | } |
1599 | } | |
1600 | } | |
1601 | } else { | |
1602 | mm->meter.n_bands = 0; | |
1603 | mm->meter.bands = NULL; | |
1604 | } | |
1605 | ||
bdda5aca BP |
1606 | return NULL; |
1607 | } | |
1608 | ||
1609 | /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man | |
1610 | * page) into 'mm' for sending the specified meter_mod 'command' to a switch. | |
1611 | * | |
1612 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
1613 | * error. The caller is responsible for freeing the returned string. */ | |
1614 | char * WARN_UNUSED_RESULT | |
1615 | parse_ofp_meter_mod_str(struct ofputil_meter_mod *mm, const char *str_, | |
db0b6c29 | 1616 | int command, enum ofputil_protocol *usable_protocols) |
bdda5aca BP |
1617 | { |
1618 | struct ofpbuf bands; | |
1619 | char *string; | |
1620 | char *error; | |
1621 | ||
1622 | ofpbuf_init(&bands, 64); | |
1623 | string = xstrdup(str_); | |
1624 | ||
db0b6c29 JR |
1625 | error = parse_ofp_meter_mod_str__(mm, string, &bands, command, |
1626 | usable_protocols); | |
bdda5aca | 1627 | |
638a19b0 | 1628 | free(string); |
bdda5aca BP |
1629 | ofpbuf_uninit(&bands); |
1630 | ||
1631 | return error; | |
638a19b0 JR |
1632 | } |
1633 | ||
bdda5aca BP |
1634 | static char * WARN_UNUSED_RESULT |
1635 | parse_flow_monitor_request__(struct ofputil_flow_monitor_request *fmr, | |
db0b6c29 JR |
1636 | const char *str_, char *string, |
1637 | enum ofputil_protocol *usable_protocols) | |
2b07c8b1 | 1638 | { |
97be1538 | 1639 | static atomic_uint32_t id = ATOMIC_VAR_INIT(0); |
2b07c8b1 BP |
1640 | char *save_ptr = NULL; |
1641 | char *name; | |
1642 | ||
97be1538 | 1643 | atomic_add(&id, 1, &fmr->id); |
a944ef40 | 1644 | |
2b07c8b1 BP |
1645 | fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY |
1646 | | NXFMF_OWN | NXFMF_ACTIONS); | |
1647 | fmr->out_port = OFPP_NONE; | |
1648 | fmr->table_id = 0xff; | |
81a76618 | 1649 | match_init_catchall(&fmr->match); |
2b07c8b1 BP |
1650 | |
1651 | for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name; | |
1652 | name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) { | |
1653 | const struct protocol *p; | |
1654 | ||
1655 | if (!strcmp(name, "!initial")) { | |
1656 | fmr->flags &= ~NXFMF_INITIAL; | |
1657 | } else if (!strcmp(name, "!add")) { | |
1658 | fmr->flags &= ~NXFMF_ADD; | |
1659 | } else if (!strcmp(name, "!delete")) { | |
1660 | fmr->flags &= ~NXFMF_DELETE; | |
1661 | } else if (!strcmp(name, "!modify")) { | |
1662 | fmr->flags &= ~NXFMF_MODIFY; | |
1663 | } else if (!strcmp(name, "!actions")) { | |
1664 | fmr->flags &= ~NXFMF_ACTIONS; | |
1665 | } else if (!strcmp(name, "!own")) { | |
1666 | fmr->flags &= ~NXFMF_OWN; | |
1667 | } else if (parse_protocol(name, &p)) { | |
81a76618 | 1668 | match_set_dl_type(&fmr->match, htons(p->dl_type)); |
2b07c8b1 | 1669 | if (p->nw_proto) { |
81a76618 | 1670 | match_set_nw_proto(&fmr->match, p->nw_proto); |
2b07c8b1 BP |
1671 | } |
1672 | } else { | |
1673 | char *value; | |
1674 | ||
1675 | value = strtok_r(NULL, ", \t\r\n", &save_ptr); | |
1676 | if (!value) { | |
bdda5aca | 1677 | return xasprintf("%s: field %s missing value", str_, name); |
2b07c8b1 BP |
1678 | } |
1679 | ||
1680 | if (!strcmp(name, "table")) { | |
bdda5aca BP |
1681 | char *error = str_to_u8(value, "table", &fmr->table_id); |
1682 | if (error) { | |
1683 | return error; | |
1684 | } | |
2b07c8b1 | 1685 | } else if (!strcmp(name, "out_port")) { |
4e022ec0 | 1686 | fmr->out_port = u16_to_ofp(atoi(value)); |
2b07c8b1 | 1687 | } else if (mf_from_name(name)) { |
bdda5aca BP |
1688 | char *error; |
1689 | ||
db0b6c29 JR |
1690 | error = parse_field(mf_from_name(name), value, &fmr->match, |
1691 | usable_protocols); | |
bdda5aca BP |
1692 | if (error) { |
1693 | return error; | |
1694 | } | |
2b07c8b1 | 1695 | } else { |
bdda5aca | 1696 | return xasprintf("%s: unknown keyword %s", str_, name); |
2b07c8b1 BP |
1697 | } |
1698 | } | |
1699 | } | |
bdda5aca BP |
1700 | return NULL; |
1701 | } | |
1702 | ||
1703 | /* Convert 'str_' (as described in the documentation for the "monitor" command | |
1704 | * in the ovs-ofctl man page) into 'fmr'. | |
1705 | * | |
1706 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
1707 | * error. The caller is responsible for freeing the returned string. */ | |
1708 | char * WARN_UNUSED_RESULT | |
1709 | parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr, | |
db0b6c29 JR |
1710 | const char *str_, |
1711 | enum ofputil_protocol *usable_protocols) | |
bdda5aca BP |
1712 | { |
1713 | char *string = xstrdup(str_); | |
db0b6c29 JR |
1714 | char *error = parse_flow_monitor_request__(fmr, str_, string, |
1715 | usable_protocols); | |
2b07c8b1 | 1716 | free(string); |
bdda5aca | 1717 | return error; |
2b07c8b1 BP |
1718 | } |
1719 | ||
0c3d5fc8 BP |
1720 | /* Parses 's' as a set of OpenFlow actions and appends the actions to |
1721 | * 'actions'. | |
1722 | * | |
bdda5aca BP |
1723 | * Returns NULL if successful, otherwise a malloc()'d string describing the |
1724 | * error. The caller is responsible for freeing the returned string. */ | |
1725 | char * WARN_UNUSED_RESULT | |
db0b6c29 JR |
1726 | parse_ofpacts(const char *s_, struct ofpbuf *ofpacts, |
1727 | enum ofputil_protocol *usable_protocols) | |
0c3d5fc8 BP |
1728 | { |
1729 | char *s = xstrdup(s_); | |
db0b6c29 JR |
1730 | char *error; |
1731 | ||
1732 | *usable_protocols = OFPUTIL_P_ANY; | |
1733 | ||
1734 | error = str_to_ofpacts(s, ofpacts, usable_protocols); | |
0c3d5fc8 | 1735 | free(s); |
bdda5aca BP |
1736 | |
1737 | return error; | |
0c3d5fc8 BP |
1738 | } |
1739 | ||
88ca35ee | 1740 | /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command' |
bdda5aca BP |
1741 | * (one of OFPFC_*) into 'fm'. |
1742 | * | |
1743 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
1744 | * error. The caller is responsible for freeing the returned string. */ | |
1745 | char * WARN_UNUSED_RESULT | |
27527aa0 | 1746 | parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string, |
db0b6c29 JR |
1747 | uint16_t command, |
1748 | enum ofputil_protocol *usable_protocols) | |
15f1f1b6 | 1749 | { |
db0b6c29 | 1750 | char *error = parse_ofp_str(fm, command, string, usable_protocols); |
bdda5aca BP |
1751 | if (!error) { |
1752 | /* Normalize a copy of the match. This ensures that non-normalized | |
1753 | * flows get logged but doesn't affect what gets sent to the switch, so | |
1754 | * that the switch can do whatever it likes with the flow. */ | |
1755 | struct match match_copy = fm->match; | |
1756 | ofputil_normalize_match(&match_copy); | |
1757 | } | |
88ca35ee | 1758 | |
bdda5aca | 1759 | return error; |
15f1f1b6 BP |
1760 | } |
1761 | ||
918f2b82 AZ |
1762 | /* Convert 'table_id' and 'flow_miss_handling' (as described for the |
1763 | * "mod-table" command in the ovs-ofctl man page) into 'tm' for sending the | |
1764 | * specified table_mod 'command' to a switch. | |
1765 | * | |
1766 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
1767 | * error. The caller is responsible for freeing the returned string. */ | |
1768 | char * WARN_UNUSED_RESULT | |
1769 | parse_ofp_table_mod(struct ofputil_table_mod *tm, const char *table_id, | |
1770 | const char *flow_miss_handling, | |
1771 | enum ofputil_protocol *usable_protocols) | |
1772 | { | |
1773 | /* Table mod requires at least OF 1.1. */ | |
1774 | *usable_protocols = OFPUTIL_P_OF11_UP; | |
1775 | ||
1776 | if (!strcasecmp(table_id, "all")) { | |
1777 | tm->table_id = 255; | |
1778 | } else { | |
1779 | char *error = str_to_u8(table_id, "table_id", &tm->table_id); | |
1780 | if (error) { | |
1781 | return error; | |
1782 | } | |
1783 | } | |
1784 | ||
1785 | if (strcmp(flow_miss_handling, "controller") == 0) { | |
1786 | tm->config = OFPTC11_TABLE_MISS_CONTROLLER; | |
1787 | } else if (strcmp(flow_miss_handling, "continue") == 0) { | |
1788 | tm->config = OFPTC11_TABLE_MISS_CONTINUE; | |
1789 | } else if (strcmp(flow_miss_handling, "drop") == 0) { | |
1790 | tm->config = OFPTC11_TABLE_MISS_DROP; | |
1791 | } else { | |
1792 | return xasprintf("invalid flow_miss_handling %s", flow_miss_handling); | |
1793 | } | |
1794 | ||
1795 | if (tm->table_id == 0xfe && tm->config == OFPTC11_TABLE_MISS_CONTINUE) { | |
1796 | return xstrdup("last table's flow miss handling can not be continue"); | |
1797 | } | |
1798 | ||
1799 | return NULL; | |
1800 | } | |
1801 | ||
1802 | ||
bdda5aca BP |
1803 | /* Opens file 'file_name' and reads each line as a flow_mod of the specified |
1804 | * type (one of OFPFC_*). Stores each flow_mod in '*fm', an array allocated | |
1805 | * on the caller's behalf, and the number of flow_mods in '*n_fms'. | |
1806 | * | |
1807 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
1808 | * error. The caller is responsible for freeing the returned string. */ | |
1809 | char * WARN_UNUSED_RESULT | |
27527aa0 | 1810 | parse_ofp_flow_mod_file(const char *file_name, uint16_t command, |
db0b6c29 JR |
1811 | struct ofputil_flow_mod **fms, size_t *n_fms, |
1812 | enum ofputil_protocol *usable_protocols) | |
15f1f1b6 | 1813 | { |
27527aa0 | 1814 | size_t allocated_fms; |
bdda5aca | 1815 | int line_number; |
27527aa0 | 1816 | FILE *stream; |
dd8101bc | 1817 | struct ds s; |
15f1f1b6 | 1818 | |
db0b6c29 JR |
1819 | *usable_protocols = OFPUTIL_P_ANY; |
1820 | ||
bdda5aca BP |
1821 | *fms = NULL; |
1822 | *n_fms = 0; | |
1823 | ||
27527aa0 BP |
1824 | stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r"); |
1825 | if (stream == NULL) { | |
bdda5aca BP |
1826 | return xasprintf("%s: open failed (%s)", |
1827 | file_name, ovs_strerror(errno)); | |
27527aa0 BP |
1828 | } |
1829 | ||
1830 | allocated_fms = *n_fms; | |
dd8101bc | 1831 | ds_init(&s); |
bdda5aca BP |
1832 | line_number = 0; |
1833 | while (!ds_get_preprocessed_line(&s, stream, &line_number)) { | |
1834 | char *error; | |
db0b6c29 | 1835 | enum ofputil_protocol usable; |
bdda5aca | 1836 | |
27527aa0 BP |
1837 | if (*n_fms >= allocated_fms) { |
1838 | *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms); | |
1839 | } | |
db0b6c29 JR |
1840 | error = parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command, |
1841 | &usable); | |
bdda5aca BP |
1842 | if (error) { |
1843 | size_t i; | |
1844 | ||
1845 | for (i = 0; i < *n_fms; i++) { | |
1846 | free((*fms)[i].ofpacts); | |
1847 | } | |
1848 | free(*fms); | |
1849 | *fms = NULL; | |
1850 | *n_fms = 0; | |
1851 | ||
1852 | ds_destroy(&s); | |
1853 | if (stream != stdin) { | |
1854 | fclose(stream); | |
1855 | } | |
1856 | ||
1857 | return xasprintf("%s:%d: %s", file_name, line_number, error); | |
1858 | } | |
db0b6c29 | 1859 | *usable_protocols &= usable; /* Each line can narrow the set. */ |
27527aa0 | 1860 | *n_fms += 1; |
15f1f1b6 | 1861 | } |
15f1f1b6 | 1862 | |
bdda5aca | 1863 | ds_destroy(&s); |
27527aa0 BP |
1864 | if (stream != stdin) { |
1865 | fclose(stream); | |
1866 | } | |
bdda5aca | 1867 | return NULL; |
88ca35ee BP |
1868 | } |
1869 | ||
bdda5aca | 1870 | char * WARN_UNUSED_RESULT |
81d1ea94 | 1871 | parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr, |
db0b6c29 JR |
1872 | bool aggregate, const char *string, |
1873 | enum ofputil_protocol *usable_protocols) | |
88ca35ee | 1874 | { |
a9a2da38 | 1875 | struct ofputil_flow_mod fm; |
bdda5aca BP |
1876 | char *error; |
1877 | ||
db0b6c29 | 1878 | error = parse_ofp_str(&fm, -1, string, usable_protocols); |
bdda5aca BP |
1879 | if (error) { |
1880 | return error; | |
1881 | } | |
88ca35ee | 1882 | |
db0b6c29 JR |
1883 | /* Special table ID support not required for stats requests. */ |
1884 | if (*usable_protocols & OFPUTIL_P_OF10_STD_TID) { | |
1885 | *usable_protocols |= OFPUTIL_P_OF10_STD; | |
1886 | } | |
1887 | if (*usable_protocols & OFPUTIL_P_OF10_NXM_TID) { | |
1888 | *usable_protocols |= OFPUTIL_P_OF10_NXM; | |
1889 | } | |
1890 | ||
88ca35ee | 1891 | fsr->aggregate = aggregate; |
e729e793 JP |
1892 | fsr->cookie = fm.cookie; |
1893 | fsr->cookie_mask = fm.cookie_mask; | |
81a76618 | 1894 | fsr->match = fm.match; |
88ca35ee | 1895 | fsr->out_port = fm.out_port; |
7395c052 | 1896 | fsr->out_group = fm.out_group; |
6c1491fb | 1897 | fsr->table_id = fm.table_id; |
bdda5aca | 1898 | return NULL; |
15f1f1b6 | 1899 | } |
ccbe50f8 BP |
1900 | |
1901 | /* Parses a specification of a flow from 's' into 'flow'. 's' must take the | |
1902 | * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a | |
1903 | * mf_field. Fields must be specified in a natural order for satisfying | |
5a0a5702 GS |
1904 | * prerequisites. If 'mask' is specified, fills the mask field for each of the |
1905 | * field specified in flow. If the map, 'names_portno' is specfied, converts | |
1906 | * the in_port name into port no while setting the 'flow'. | |
ccbe50f8 BP |
1907 | * |
1908 | * Returns NULL on success, otherwise a malloc()'d string that explains the | |
1909 | * problem. */ | |
1910 | char * | |
5a0a5702 GS |
1911 | parse_ofp_exact_flow(struct flow *flow, struct flow *mask, const char *s, |
1912 | const struct simap *portno_names) | |
ccbe50f8 BP |
1913 | { |
1914 | char *pos, *key, *value_s; | |
1915 | char *error = NULL; | |
1916 | char *copy; | |
1917 | ||
1918 | memset(flow, 0, sizeof *flow); | |
5a0a5702 GS |
1919 | if (mask) { |
1920 | memset(mask, 0, sizeof *mask); | |
1921 | } | |
ccbe50f8 BP |
1922 | |
1923 | pos = copy = xstrdup(s); | |
1924 | while (ofputil_parse_key_value(&pos, &key, &value_s)) { | |
1925 | const struct protocol *p; | |
1926 | if (parse_protocol(key, &p)) { | |
1927 | if (flow->dl_type) { | |
1928 | error = xasprintf("%s: Ethernet type set multiple times", s); | |
1929 | goto exit; | |
1930 | } | |
1931 | flow->dl_type = htons(p->dl_type); | |
5a0a5702 GS |
1932 | if (mask) { |
1933 | mask->dl_type = OVS_BE16_MAX; | |
1934 | } | |
ccbe50f8 BP |
1935 | |
1936 | if (p->nw_proto) { | |
1937 | if (flow->nw_proto) { | |
1938 | error = xasprintf("%s: network protocol set " | |
1939 | "multiple times", s); | |
1940 | goto exit; | |
1941 | } | |
1942 | flow->nw_proto = p->nw_proto; | |
5a0a5702 GS |
1943 | if (mask) { |
1944 | mask->nw_proto = UINT8_MAX; | |
1945 | } | |
ccbe50f8 BP |
1946 | } |
1947 | } else { | |
1948 | const struct mf_field *mf; | |
1949 | union mf_value value; | |
1950 | char *field_error; | |
1951 | ||
1952 | mf = mf_from_name(key); | |
1953 | if (!mf) { | |
1954 | error = xasprintf("%s: unknown field %s", s, key); | |
1955 | goto exit; | |
1956 | } | |
1957 | ||
1958 | if (!mf_are_prereqs_ok(mf, flow)) { | |
1959 | error = xasprintf("%s: prerequisites not met for setting %s", | |
1960 | s, key); | |
1961 | goto exit; | |
1962 | } | |
1963 | ||
1964 | if (!mf_is_zero(mf, flow)) { | |
1965 | error = xasprintf("%s: field %s set multiple times", s, key); | |
1966 | goto exit; | |
1967 | } | |
1968 | ||
5a0a5702 GS |
1969 | if (!strcmp(key, "in_port") |
1970 | && portno_names | |
1971 | && simap_contains(portno_names, value_s)) { | |
1972 | flow->in_port.ofp_port = u16_to_ofp( | |
1973 | simap_get(portno_names, value_s)); | |
1974 | if (mask) { | |
1975 | mask->in_port.ofp_port = u16_to_ofp(ntohs(OVS_BE16_MAX)); | |
1976 | } | |
1977 | } else { | |
1978 | field_error = mf_parse_value(mf, value_s, &value); | |
1979 | if (field_error) { | |
1980 | error = xasprintf("%s: bad value for %s (%s)", | |
1981 | s, key, field_error); | |
1982 | free(field_error); | |
1983 | goto exit; | |
1984 | } | |
ccbe50f8 | 1985 | |
5a0a5702 GS |
1986 | mf_set_flow_value(mf, &value, flow); |
1987 | if (mask) { | |
1988 | mf_mask_field(mf, mask); | |
1989 | } | |
1990 | } | |
ccbe50f8 BP |
1991 | } |
1992 | } | |
1993 | ||
4e022ec0 AW |
1994 | if (!flow->in_port.ofp_port) { |
1995 | flow->in_port.ofp_port = OFPP_NONE; | |
72d64e33 EJ |
1996 | } |
1997 | ||
ccbe50f8 BP |
1998 | exit: |
1999 | free(copy); | |
2000 | ||
2001 | if (error) { | |
2002 | memset(flow, 0, sizeof *flow); | |
5a0a5702 GS |
2003 | if (mask) { |
2004 | memset(mask, 0, sizeof *mask); | |
2005 | } | |
ccbe50f8 BP |
2006 | } |
2007 | return error; | |
2008 | } | |
7395c052 NZ |
2009 | |
2010 | static char * WARN_UNUSED_RESULT | |
2011 | parse_bucket_str(struct ofputil_bucket *bucket, char *str_, | |
2012 | enum ofputil_protocol *usable_protocols) | |
2013 | { | |
2014 | struct ofpbuf ofpacts; | |
2015 | char *pos, *act, *arg; | |
2016 | int n_actions; | |
2017 | ||
2018 | bucket->weight = 1; | |
2019 | bucket->watch_port = OFPP_ANY; | |
2020 | bucket->watch_group = OFPG11_ANY; | |
2021 | ||
2022 | pos = str_; | |
2023 | n_actions = 0; | |
2024 | ofpbuf_init(&ofpacts, 64); | |
2025 | while (ofputil_parse_key_value(&pos, &act, &arg)) { | |
2026 | char *error = NULL; | |
2027 | ||
2028 | if (!strcasecmp(act, "weight")) { | |
2029 | error = str_to_u16(arg, "weight", &bucket->weight); | |
2030 | } else if (!strcasecmp(act, "watch_port")) { | |
2031 | if (!ofputil_port_from_string(arg, &bucket->watch_port) | |
2032 | || (ofp_to_u16(bucket->watch_port) >= ofp_to_u16(OFPP_MAX) | |
2033 | && bucket->watch_port != OFPP_ANY)) { | |
2034 | error = xasprintf("%s: invalid watch_port", arg); | |
2035 | } | |
2036 | } else if (!strcasecmp(act, "watch_group")) { | |
2037 | error = str_to_u32(arg, &bucket->watch_group); | |
2038 | if (!error && bucket->watch_group > OFPG_MAX) { | |
2039 | error = xasprintf("invalid watch_group id %"PRIu32, | |
2040 | bucket->watch_group); | |
2041 | } | |
2042 | } else { | |
2043 | error = str_to_ofpact__(pos, act, arg, &ofpacts, n_actions, | |
2044 | usable_protocols); | |
2045 | n_actions++; | |
2046 | } | |
2047 | ||
2048 | if (error) { | |
2049 | ofpbuf_uninit(&ofpacts); | |
2050 | return error; | |
2051 | } | |
2052 | } | |
2053 | ||
2054 | ofpact_pad(&ofpacts); | |
2055 | bucket->ofpacts = ofpacts.data; | |
2056 | bucket->ofpacts_len = ofpacts.size; | |
2057 | ||
2058 | return NULL; | |
2059 | } | |
2060 | ||
2061 | static char * WARN_UNUSED_RESULT | |
2062 | parse_ofp_group_mod_str__(struct ofputil_group_mod *gm, uint16_t command, | |
2063 | char *string, | |
2064 | enum ofputil_protocol *usable_protocols) | |
2065 | { | |
2066 | enum { | |
2067 | F_GROUP_TYPE = 1 << 0, | |
2068 | F_BUCKETS = 1 << 1, | |
2069 | } fields; | |
2070 | char *save_ptr = NULL; | |
2071 | bool had_type = false; | |
2072 | char *name; | |
2073 | struct ofputil_bucket *bucket; | |
2074 | char *error = NULL; | |
2075 | ||
2076 | *usable_protocols = OFPUTIL_P_OF11_UP; | |
2077 | ||
2078 | switch (command) { | |
2079 | case OFPGC11_ADD: | |
2080 | fields = F_GROUP_TYPE | F_BUCKETS; | |
2081 | break; | |
2082 | ||
2083 | case OFPGC11_DELETE: | |
2084 | fields = 0; | |
2085 | break; | |
2086 | ||
2087 | case OFPGC11_MODIFY: | |
2088 | fields = F_GROUP_TYPE | F_BUCKETS; | |
2089 | break; | |
2090 | ||
2091 | default: | |
2092 | NOT_REACHED(); | |
2093 | } | |
2094 | ||
2095 | memset(gm, 0, sizeof *gm); | |
2096 | gm->command = command; | |
2097 | gm->group_id = OFPG_ANY; | |
2098 | list_init(&gm->buckets); | |
2099 | if (command == OFPGC11_DELETE && string[0] == '\0') { | |
2100 | gm->group_id = OFPG_ALL; | |
2101 | return NULL; | |
2102 | } | |
2103 | ||
2104 | *usable_protocols = OFPUTIL_P_OF11_UP; | |
2105 | ||
2106 | if (fields & F_BUCKETS) { | |
2107 | char *bkt_str = strstr(string, "bucket"); | |
2108 | ||
2109 | if (bkt_str) { | |
2110 | *bkt_str = '\0'; | |
2111 | } | |
2112 | ||
2113 | while (bkt_str) { | |
2114 | char *next_bkt_str; | |
2115 | ||
2116 | bkt_str = strchr(bkt_str + 1, '='); | |
2117 | if (!bkt_str) { | |
2118 | error = xstrdup("must specify bucket content"); | |
2119 | goto out; | |
2120 | } | |
2121 | bkt_str++; | |
2122 | ||
2123 | next_bkt_str = strstr(bkt_str, "bucket"); | |
2124 | if (next_bkt_str) { | |
2125 | *next_bkt_str = '\0'; | |
2126 | } | |
2127 | ||
2128 | bucket = xzalloc(sizeof(struct ofputil_bucket)); | |
2129 | error = parse_bucket_str(bucket, bkt_str, usable_protocols); | |
2130 | if (error) { | |
2131 | free(bucket); | |
2132 | goto out; | |
2133 | } | |
2134 | list_push_back(&gm->buckets, &bucket->list_node); | |
2135 | ||
2136 | bkt_str = next_bkt_str; | |
2137 | } | |
2138 | } | |
2139 | ||
2140 | for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name; | |
2141 | name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) { | |
2142 | char *value; | |
2143 | ||
2144 | value = strtok_r(NULL, ", \t\r\n", &save_ptr); | |
2145 | if (!value) { | |
2146 | error = xasprintf("field %s missing value", name); | |
2147 | goto out; | |
2148 | } | |
2149 | ||
2150 | if (!strcmp(name, "group_id")) { | |
2151 | if(!strcmp(value, "all")) { | |
2152 | gm->group_id = OFPG_ALL; | |
2153 | } else { | |
2154 | char *error = str_to_u32(value, &gm->group_id); | |
2155 | if (error) { | |
2156 | goto out; | |
2157 | } | |
2158 | if (gm->group_id != OFPG_ALL && gm->group_id > OFPG_MAX) { | |
2159 | error = xasprintf("invalid group id %"PRIu32, | |
2160 | gm->group_id); | |
2161 | goto out; | |
2162 | } | |
2163 | } | |
2164 | } else if (!strcmp(name, "type")){ | |
2165 | if (!(fields & F_GROUP_TYPE)) { | |
2166 | error = xstrdup("type is not needed"); | |
2167 | goto out; | |
2168 | } | |
2169 | if (!strcmp(value, "all")) { | |
2170 | gm->type = OFPGT11_ALL; | |
2171 | } else if (!strcmp(value, "select")) { | |
2172 | gm->type = OFPGT11_SELECT; | |
2173 | } else if (!strcmp(value, "indirect")) { | |
2174 | gm->type = OFPGT11_INDIRECT; | |
2175 | } else if (!strcmp(value, "ff") || | |
2176 | !strcmp(value, "fast_failover")) { | |
2177 | gm->type = OFPGT11_FF; | |
2178 | } else { | |
2179 | error = xasprintf("invalid group type %s", value); | |
2180 | goto out; | |
2181 | } | |
2182 | had_type = true; | |
2183 | } else if (!strcmp(name, "bucket")) { | |
2184 | error = xstrdup("bucket is not needed"); | |
2185 | goto out; | |
2186 | } else { | |
2187 | error = xasprintf("unknown keyword %s", name); | |
2188 | goto out; | |
2189 | } | |
2190 | } | |
2191 | if (gm->group_id == OFPG_ANY) { | |
2192 | error = xstrdup("must specify a group_id"); | |
2193 | goto out; | |
2194 | } | |
2195 | if (fields & F_GROUP_TYPE && !had_type) { | |
2196 | error = xstrdup("must specify a type"); | |
2197 | goto out; | |
2198 | } | |
2199 | ||
2200 | /* Validate buckets. */ | |
2201 | LIST_FOR_EACH (bucket, list_node, &gm->buckets) { | |
2202 | if (bucket->weight != 1 && gm->type != OFPGT11_SELECT) { | |
2203 | error = xstrdup("Only select groups can have bucket weights."); | |
2204 | goto out; | |
2205 | } | |
2206 | } | |
2207 | if (gm->type == OFPGT11_INDIRECT && !list_is_short(&gm->buckets)) { | |
2208 | error = xstrdup("Indirect groups can have at most one bucket."); | |
2209 | goto out; | |
2210 | } | |
2211 | ||
2212 | return NULL; | |
2213 | out: | |
2214 | ofputil_bucket_list_destroy(&gm->buckets); | |
2215 | return error; | |
2216 | } | |
2217 | ||
2218 | char * WARN_UNUSED_RESULT | |
2219 | parse_ofp_group_mod_str(struct ofputil_group_mod *gm, uint16_t command, | |
2220 | const char *str_, | |
2221 | enum ofputil_protocol *usable_protocols) | |
2222 | { | |
2223 | char *string = xstrdup(str_); | |
2224 | char *error = parse_ofp_group_mod_str__(gm, command, string, | |
2225 | usable_protocols); | |
2226 | free(string); | |
2227 | ||
2228 | if (error) { | |
2229 | ofputil_bucket_list_destroy(&gm->buckets); | |
2230 | } | |
2231 | return error; | |
2232 | } | |
2233 | ||
2234 | char * WARN_UNUSED_RESULT | |
2235 | parse_ofp_group_mod_file(const char *file_name, uint16_t command, | |
2236 | struct ofputil_group_mod **gms, size_t *n_gms, | |
2237 | enum ofputil_protocol *usable_protocols) | |
2238 | { | |
2239 | size_t allocated_gms; | |
2240 | int line_number; | |
2241 | FILE *stream; | |
2242 | struct ds s; | |
2243 | ||
2244 | *gms = NULL; | |
2245 | *n_gms = 0; | |
2246 | ||
2247 | stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r"); | |
2248 | if (stream == NULL) { | |
2249 | return xasprintf("%s: open failed (%s)", | |
2250 | file_name, ovs_strerror(errno)); | |
2251 | } | |
2252 | ||
2253 | allocated_gms = *n_gms; | |
2254 | ds_init(&s); | |
2255 | line_number = 0; | |
2256 | *usable_protocols = OFPUTIL_P_OF11_UP; | |
2257 | while (!ds_get_preprocessed_line(&s, stream, &line_number)) { | |
2258 | enum ofputil_protocol usable; | |
2259 | char *error; | |
2260 | ||
2261 | if (*n_gms >= allocated_gms) { | |
2262 | *gms = x2nrealloc(*gms, &allocated_gms, sizeof **gms); | |
2263 | } | |
2264 | error = parse_ofp_group_mod_str(&(*gms)[*n_gms], command, ds_cstr(&s), | |
2265 | &usable); | |
2266 | if (error) { | |
2267 | size_t i; | |
2268 | ||
2269 | for (i = 0; i < *n_gms; i++) { | |
2270 | ofputil_bucket_list_destroy(&(*gms)[i].buckets); | |
2271 | } | |
2272 | free(*gms); | |
2273 | *gms = NULL; | |
2274 | *n_gms = 0; | |
2275 | ||
2276 | ds_destroy(&s); | |
2277 | if (stream != stdin) { | |
2278 | fclose(stream); | |
2279 | } | |
2280 | ||
2281 | return xasprintf("%s:%d: %s", file_name, line_number, error); | |
2282 | } | |
2283 | *usable_protocols &= usable; | |
2284 | *n_gms += 1; | |
2285 | } | |
2286 | ||
2287 | ds_destroy(&s); | |
2288 | if (stream != stdin) { | |
2289 | fclose(stream); | |
2290 | } | |
2291 | return NULL; | |
2292 | } |