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