]> git.proxmox.com Git - mirror_ovs.git/blame - lib/ofp-parse.c
ofproto-dpif-xlate: Fix fin_timeout to make rules expirable.
[mirror_ovs.git] / lib / ofp-parse.c
CommitLineData
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
JP
38#include "packets.h"
39#include "socket-util.h"
40#include "vconn.h"
41#include "vlog.h"
f22716dc 42
d98e6007 43VLOG_DEFINE_THIS_MODULE(ofp_parse);
f22716dc 44
bdda5aca
BP
45/* Parses 'str' as an 8-bit unsigned integer into '*valuep'.
46 *
47 * 'name' describes the value parsed in an error message, if any.
48 *
49 * Returns NULL if successful, otherwise a malloc()'d string describing the
50 * error. The caller is responsible for freeing the returned string. */
51static char * WARN_UNUSED_RESULT
52str_to_u8(const char *str, const char *name, uint8_t *valuep)
c3636ffc 53{
638a19b0 54 int value;
c3636ffc 55
bdda5aca
BP
56 if (!str_to_int(str, 0, &value) || value < 0 || value > 255) {
57 return xasprintf("invalid %s \"%s\"", name, str);
c3636ffc 58 }
bdda5aca
BP
59 *valuep = value;
60 return NULL;
c3636ffc
BP
61}
62
bdda5aca
BP
63/* Parses 'str' as a 16-bit unsigned integer into '*valuep'.
64 *
65 * 'name' describes the value parsed in an error message, if any.
66 *
67 * Returns NULL if successful, otherwise a malloc()'d string describing the
68 * error. The caller is responsible for freeing the returned string. */
69static char * WARN_UNUSED_RESULT
70str_to_u16(const char *str, const char *name, uint16_t *valuep)
c3636ffc
BP
71{
72 int value;
73
74 if (!str_to_int(str, 0, &value) || value < 0 || value > 65535) {
bdda5aca 75 return xasprintf("invalid %s \"%s\"", name, str);
c3636ffc 76 }
bdda5aca
BP
77 *valuep = value;
78 return NULL;
c3636ffc
BP
79}
80
bdda5aca
BP
81/* Parses 'str' as a 32-bit unsigned integer into '*valuep'.
82 *
83 * Returns NULL if successful, otherwise a malloc()'d string describing the
84 * error. The caller is responsible for freeing the returned string. */
85static char * WARN_UNUSED_RESULT
86str_to_u32(const char *str, uint32_t *valuep)
f22716dc
JP
87{
88 char *tail;
89 uint32_t value;
90
c4894ed4 91 if (!str[0]) {
bdda5aca 92 return xstrdup("missing required numeric argument");
ce5452cf
EJ
93 }
94
f22716dc
JP
95 errno = 0;
96 value = strtoul(str, &tail, 0);
97 if (errno == EINVAL || errno == ERANGE || *tail) {
bdda5aca 98 return xasprintf("invalid numeric format %s", str);
f22716dc 99 }
bdda5aca
BP
100 *valuep = value;
101 return NULL;
f22716dc
JP
102}
103
bdda5aca
BP
104/* Parses 'str' as an 64-bit unsigned integer into '*valuep'.
105 *
106 * Returns NULL if successful, otherwise a malloc()'d string describing the
107 * error. The caller is responsible for freeing the returned string. */
108static char * WARN_UNUSED_RESULT
109str_to_u64(const char *str, uint64_t *valuep)
f22716dc
JP
110{
111 char *tail;
112 uint64_t value;
113
c4894ed4 114 if (!str[0]) {
bdda5aca 115 return xstrdup("missing required numeric argument");
c4894ed4
BP
116 }
117
f22716dc
JP
118 errno = 0;
119 value = strtoull(str, &tail, 0);
120 if (errno == EINVAL || errno == ERANGE || *tail) {
bdda5aca 121 return xasprintf("invalid numeric format %s", str);
f22716dc 122 }
bdda5aca
BP
123 *valuep = value;
124 return NULL;
f22716dc
JP
125}
126
bdda5aca
BP
127/* Parses 'str' as an 64-bit unsigned integer in network byte order into
128 * '*valuep'.
129 *
130 * Returns NULL if successful, otherwise a malloc()'d string describing the
131 * error. The caller is responsible for freeing the returned string. */
132static char * WARN_UNUSED_RESULT
133str_to_be64(const char *str, ovs_be64 *valuep)
134{
4be17953 135 uint64_t value = 0;
bdda5aca
BP
136 char *error;
137
138 error = str_to_u64(str, &value);
139 if (!error) {
140 *valuep = htonll(value);
141 }
142 return error;
143}
144
145/* Parses 'str' as an Ethernet address into 'mac'.
146 *
147 * Returns NULL if successful, otherwise a malloc()'d string describing the
148 * error. The caller is responsible for freeing the returned string. */
149static char * WARN_UNUSED_RESULT
f22716dc
JP
150str_to_mac(const char *str, uint8_t mac[6])
151{
152 if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
153 != ETH_ADDR_SCAN_COUNT) {
bdda5aca 154 return xasprintf("invalid mac address %s", str);
f22716dc 155 }
bdda5aca 156 return NULL;
f22716dc
JP
157}
158
bdda5aca
BP
159/* Parses 'str' as an IP address into '*ip'.
160 *
161 * Returns NULL if successful, otherwise a malloc()'d string describing the
162 * error. The caller is responsible for freeing the returned string. */
163static char * WARN_UNUSED_RESULT
6a885fd0 164str_to_ip(const char *str, ovs_be32 *ip)
cb8ca532 165{
f22716dc 166 struct in_addr in_addr;
f22716dc 167
6a885fd0 168 if (lookup_ip(str, &in_addr)) {
bdda5aca 169 return xasprintf("%s: could not convert to IP address", str);
f22716dc
JP
170 }
171 *ip = in_addr.s_addr;
bdda5aca 172 return NULL;
d31f1109
JP
173}
174
bdda5aca
BP
175/* Parses 'arg' as the argument to an "enqueue" action, and appends such an
176 * action to 'ofpacts'.
177 *
178 * Returns NULL if successful, otherwise a malloc()'d string describing the
179 * error. The caller is responsible for freeing the returned string. */
180static char * WARN_UNUSED_RESULT
f25d0cf3 181parse_enqueue(char *arg, struct ofpbuf *ofpacts)
5682f723 182{
333eba21
BP
183 char *sp = NULL;
184 char *port = strtok_r(arg, ":q", &sp);
185 char *queue = strtok_r(NULL, "", &sp);
f25d0cf3 186 struct ofpact_enqueue *enqueue;
333eba21
BP
187
188 if (port == NULL || queue == NULL) {
bdda5aca 189 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
333eba21
BP
190 }
191
f25d0cf3 192 enqueue = ofpact_put_ENQUEUE(ofpacts);
bdda5aca
BP
193 if (!ofputil_port_from_string(port, &enqueue->port)) {
194 return xasprintf("%s: enqueue to unknown port", port);
195 }
196 return str_to_u32(queue, &enqueue->queue);
5682f723
BP
197}
198
bdda5aca
BP
199/* Parses 'arg' as the argument to an "output" action, and appends such an
200 * action to 'ofpacts'.
201 *
202 * Returns NULL if successful, otherwise a malloc()'d string describing the
203 * error. The caller is responsible for freeing the returned string. */
204static char * WARN_UNUSED_RESULT
205parse_output(const char *arg, struct ofpbuf *ofpacts)
f694937d
EJ
206{
207 if (strchr(arg, '[')) {
f25d0cf3 208 struct ofpact_output_reg *output_reg;
f694937d 209
f25d0cf3 210 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
f25d0cf3 211 output_reg->max_len = UINT16_MAX;
bdda5aca 212 return mf_parse_subfield(&output_reg->src, arg);
f694937d 213 } else {
f25d0cf3
BP
214 struct ofpact_output *output;
215
216 output = ofpact_put_OUTPUT(ofpacts);
f25d0cf3 217 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
bdda5aca
BP
218 if (!ofputil_port_from_string(arg, &output->port)) {
219 return xasprintf("%s: output to unknown port", arg);
220 }
221 return NULL;
f694937d
EJ
222 }
223}
224
bdda5aca
BP
225/* Parses 'arg' as the argument to an "resubmit" action, and appends such an
226 * action to 'ofpacts'.
227 *
228 * Returns NULL if successful, otherwise a malloc()'d string describing the
229 * error. The caller is responsible for freeing the returned string. */
230static char * WARN_UNUSED_RESULT
f25d0cf3 231parse_resubmit(char *arg, struct ofpbuf *ofpacts)
29901626 232{
f25d0cf3 233 struct ofpact_resubmit *resubmit;
29901626 234 char *in_port_s, *table_s;
f25d0cf3
BP
235
236 resubmit = ofpact_put_RESUBMIT(ofpacts);
29901626
BP
237
238 in_port_s = strsep(&arg, ",");
239 if (in_port_s && in_port_s[0]) {
8010100b 240 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
bdda5aca 241 return xasprintf("%s: resubmit to unknown port", in_port_s);
29901626
BP
242 }
243 } else {
f25d0cf3 244 resubmit->in_port = OFPP_IN_PORT;
29901626
BP
245 }
246
247 table_s = strsep(&arg, ",");
bdda5aca 248 if (table_s && table_s[0]) {
4be17953 249 uint32_t table_id = 0;
bdda5aca
BP
250 char *error;
251
252 error = str_to_u32(table_s, &table_id);
253 if (error) {
254 return error;
255 }
256 resubmit->table_id = table_id;
257 } else {
258 resubmit->table_id = 255;
259 }
29901626 260
f25d0cf3 261 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
bdda5aca
BP
262 return xstrdup("at least one \"in_port\" or \"table\" must be "
263 "specified on resubmit");
29901626 264 }
bdda5aca 265 return NULL;
333eba21
BP
266}
267
bdda5aca
BP
268/* Parses 'arg' as the argument to a "note" action, and appends such an action
269 * to 'ofpacts'.
270 *
271 * Returns NULL if successful, otherwise a malloc()'d string describing the
272 * error. The caller is responsible for freeing the returned string. */
273static char * WARN_UNUSED_RESULT
f25d0cf3 274parse_note(const char *arg, struct ofpbuf *ofpacts)
333eba21 275{
f25d0cf3 276 struct ofpact_note *note;
333eba21 277
f25d0cf3 278 note = ofpact_put_NOTE(ofpacts);
333eba21
BP
279 while (*arg != '\0') {
280 uint8_t byte;
281 bool ok;
282
283 if (*arg == '.') {
284 arg++;
285 }
286 if (*arg == '\0') {
287 break;
288 }
289
290 byte = hexits_value(arg, 2, &ok);
291 if (!ok) {
bdda5aca 292 return xstrdup("bad hex digit in `note' argument");
333eba21 293 }
f25d0cf3 294 ofpbuf_put(ofpacts, &byte, 1);
333eba21 295
f25d0cf3
BP
296 note = ofpacts->l2;
297 note->length++;
333eba21 298
f25d0cf3 299 arg += 2;
333eba21 300 }
f25d0cf3 301 ofpact_update_len(ofpacts, &note->ofpact);
bdda5aca 302 return NULL;
333eba21
BP
303}
304
bdda5aca
BP
305/* Parses 'arg' as the argument to a "fin_timeout" action, and appends such an
306 * action to 'ofpacts'.
307 *
308 * Returns NULL if successful, otherwise a malloc()'d string describing the
309 * error. The caller is responsible for freeing the returned string. */
310static char * WARN_UNUSED_RESULT
0e553d9c
BP
311parse_fin_timeout(struct ofpbuf *b, char *arg)
312{
f25d0cf3 313 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(b);
0e553d9c
BP
314 char *key, *value;
315
0e553d9c 316 while (ofputil_parse_key_value(&arg, &key, &value)) {
bdda5aca
BP
317 char *error;
318
0e553d9c 319 if (!strcmp(key, "idle_timeout")) {
bdda5aca 320 error = str_to_u16(value, key, &oft->fin_idle_timeout);
0e553d9c 321 } else if (!strcmp(key, "hard_timeout")) {
bdda5aca 322 error = str_to_u16(value, key, &oft->fin_hard_timeout);
0e553d9c 323 } else {
bdda5aca
BP
324 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
325 key);
326 }
327
328 if (error) {
329 return error;
0e553d9c
BP
330 }
331 }
bdda5aca 332 return NULL;
0e553d9c
BP
333}
334
bdda5aca
BP
335/* Parses 'arg' as the argument to a "controller" action, and appends such an
336 * action to 'ofpacts'.
337 *
338 * Returns NULL if successful, otherwise a malloc()'d string describing the
339 * error. The caller is responsible for freeing the returned string. */
340static char * WARN_UNUSED_RESULT
a7349929
BP
341parse_controller(struct ofpbuf *b, char *arg)
342{
343 enum ofp_packet_in_reason reason = OFPR_ACTION;
344 uint16_t controller_id = 0;
345 uint16_t max_len = UINT16_MAX;
346
347 if (!arg[0]) {
348 /* Use defaults. */
349 } else if (strspn(arg, "0123456789") == strlen(arg)) {
bdda5aca
BP
350 char *error = str_to_u16(arg, "max_len", &max_len);
351 if (error) {
352 return error;
353 }
a7349929
BP
354 } else {
355 char *name, *value;
356
357 while (ofputil_parse_key_value(&arg, &name, &value)) {
358 if (!strcmp(name, "reason")) {
359 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
bdda5aca 360 return xasprintf("unknown reason \"%s\"", value);
a7349929
BP
361 }
362 } else if (!strcmp(name, "max_len")) {
bdda5aca
BP
363 char *error = str_to_u16(value, "max_len", &max_len);
364 if (error) {
365 return error;
366 }
a7349929 367 } else if (!strcmp(name, "id")) {
bdda5aca
BP
368 char *error = str_to_u16(value, "id", &controller_id);
369 if (error) {
370 return error;
371 }
a7349929 372 } else {
bdda5aca
BP
373 return xasprintf("unknown key \"%s\" parsing controller "
374 "action", name);
a7349929
BP
375 }
376 }
377 }
378
379 if (reason == OFPR_ACTION && controller_id == 0) {
f25d0cf3
BP
380 struct ofpact_output *output;
381
382 output = ofpact_put_OUTPUT(b);
383 output->port = OFPP_CONTROLLER;
384 output->max_len = max_len;
a7349929 385 } else {
f25d0cf3 386 struct ofpact_controller *controller;
a7349929 387
f25d0cf3
BP
388 controller = ofpact_put_CONTROLLER(b);
389 controller->max_len = max_len;
390 controller->reason = reason;
391 controller->controller_id = controller_id;
a7349929 392 }
bdda5aca
BP
393
394 return NULL;
a7349929
BP
395}
396
c2d967a5 397static void
99086062 398parse_noargs_dec_ttl(struct ofpbuf *b)
c2d967a5
MM
399{
400 struct ofpact_cnt_ids *ids;
7bcb1506 401 uint16_t id = 0;
c2d967a5
MM
402
403 ids = ofpact_put_DEC_TTL(b);
7bcb1506
IY
404 ofpbuf_put(b, &id, sizeof id);
405 ids = b->l2;
406 ids->n_controllers++;
407 ofpact_update_len(b, &ids->ofpact);
408}
c2d967a5 409
bdda5aca
BP
410/* Parses 'arg' as the argument to a "dec_ttl" action, and appends such an
411 * action to 'ofpacts'.
412 *
413 * Returns NULL if successful, otherwise a malloc()'d string describing the
414 * error. The caller is responsible for freeing the returned string. */
415static char * WARN_UNUSED_RESULT
99086062 416parse_dec_ttl(struct ofpbuf *b, char *arg)
7bcb1506 417{
c2d967a5 418 if (*arg == '\0') {
99086062 419 parse_noargs_dec_ttl(b);
c2d967a5 420 } else {
7bcb1506 421 struct ofpact_cnt_ids *ids;
c2d967a5
MM
422 char *cntr;
423
7bcb1506 424 ids = ofpact_put_DEC_TTL(b);
c2d967a5
MM
425 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
426 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
427 cntr = strtok_r(NULL, ", ", &arg)) {
428 uint16_t id = atoi(cntr);
429
430 ofpbuf_put(b, &id, sizeof id);
431 ids = b->l2;
432 ids->n_controllers++;
433 }
434 if (!ids->n_controllers) {
bdda5aca
BP
435 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
436 "id.");
c2d967a5 437 }
7bcb1506 438 ofpact_update_len(b, &ids->ofpact);
c2d967a5 439 }
bdda5aca 440 return NULL;
c2d967a5
MM
441}
442
bdda5aca
BP
443/* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
444 * action to 'ofpacts'.
445 *
446 * Returns NULL if successful, otherwise a malloc()'d string describing the
447 * error. The caller is responsible for freeing the returned string. */
448static char * WARN_UNUSED_RESULT
0f3f3c3d
SH
449parse_set_mpls_ttl(struct ofpbuf *b, const char *arg)
450{
451 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(b);
452
453 if (*arg == '\0') {
bdda5aca 454 return xstrdup("parse_set_mpls_ttl: expected ttl.");
0f3f3c3d
SH
455 }
456
457 mpls_ttl->ttl = atoi(arg);
bdda5aca 458 return NULL;
0f3f3c3d
SH
459}
460
bdda5aca
BP
461/* Parses a "set_field" action with argument 'arg', appending the parsed
462 * action to 'ofpacts'.
463 *
464 * Returns NULL if successful, otherwise a malloc()'d string describing the
465 * error. The caller is responsible for freeing the returned string. */
466static char * WARN_UNUSED_RESULT
467set_field_parse__(char *arg, struct ofpbuf *ofpacts)
f5c45121 468{
f5c45121
SH
469 struct ofpact_reg_load *load = ofpact_put_REG_LOAD(ofpacts);
470 char *value;
471 char *delim;
472 char *key;
473 const struct mf_field *mf;
bdda5aca 474 char *error;
f5c45121
SH
475 union mf_value mf_value;
476
bdda5aca
BP
477 value = arg;
478 delim = strstr(arg, "->");
f5c45121 479 if (!delim) {
bdda5aca 480 return xasprintf("%s: missing `->'", arg);
f5c45121
SH
481 }
482 if (strlen(delim) <= strlen("->")) {
bdda5aca 483 return xasprintf("%s: missing field name following `->'", arg);
f5c45121
SH
484 }
485
486 key = delim + strlen("->");
487 mf = mf_from_name(key);
488 if (!mf) {
bdda5aca 489 return xasprintf("%s is not a valid OXM field name", key);
f5c45121
SH
490 }
491 if (!mf->writable) {
bdda5aca 492 return xasprintf("%s is read-only", key);
f5c45121
SH
493 }
494
495 delim[0] = '\0';
496 error = mf_parse_value(mf, value, &mf_value);
497 if (error) {
bdda5aca 498 return error;
f5c45121
SH
499 }
500 if (!mf_is_value_valid(mf, &mf_value)) {
bdda5aca 501 return xasprintf("%s is not a valid value for field %s", value, key);
f5c45121
SH
502 }
503 ofpact_set_field_init(load, mf, &mf_value);
bdda5aca
BP
504
505 return NULL;
f5c45121
SH
506}
507
bdda5aca
BP
508/* Parses 'arg' as the argument to a "set_field" action, and appends such an
509 * action to 'ofpacts'.
510 *
511 * Returns NULL if successful, otherwise a malloc()'d string describing the
512 * error. The caller is responsible for freeing the returned string. */
513static char * WARN_UNUSED_RESULT
514set_field_parse(const char *arg, struct ofpbuf *ofpacts)
515{
516 char *copy = xstrdup(arg);
517 char *error = set_field_parse__(copy, ofpacts);
518 free(copy);
519 return error;
520}
521
522/* Parses 'arg' as the argument to a "write_metadata" instruction, and appends
523 * such an action to 'ofpacts'.
524 *
525 * Returns NULL if successful, otherwise a malloc()'d string describing the
526 * error. The caller is responsible for freeing the returned string. */
527static char * WARN_UNUSED_RESULT
4cceacb9
JS
528parse_metadata(struct ofpbuf *b, char *arg)
529{
530 struct ofpact_metadata *om;
531 char *mask = strchr(arg, '/');
532
533 om = ofpact_put_WRITE_METADATA(b);
534
535 if (mask) {
bdda5aca
BP
536 char *error;
537
4cceacb9 538 *mask = '\0';
bdda5aca
BP
539 error = str_to_be64(mask + 1, &om->mask);
540 if (error) {
541 return error;
542 }
4cceacb9
JS
543 } else {
544 om->mask = htonll(UINT64_MAX);
545 }
546
bdda5aca 547 return str_to_be64(arg, &om->metadata);
4cceacb9
JS
548}
549
bdda5aca
BP
550/* Parses 'arg' as the argument to a "sample" action, and appends such an
551 * action to 'ofpacts'.
552 *
553 * Returns NULL if successful, otherwise a malloc()'d string describing the
554 * error. The caller is responsible for freeing the returned string. */
555static char * WARN_UNUSED_RESULT
29089a54
RL
556parse_sample(struct ofpbuf *b, char *arg)
557{
558 struct ofpact_sample *os = ofpact_put_SAMPLE(b);
559 char *key, *value;
560
561 while (ofputil_parse_key_value(&arg, &key, &value)) {
bdda5aca
BP
562 char *error = NULL;
563
29089a54 564 if (!strcmp(key, "probability")) {
bdda5aca
BP
565 error = str_to_u16(value, "probability", &os->probability);
566 if (!error && os->probability == 0) {
567 error = xasprintf("invalid probability value \"%s\"", value);
29089a54
RL
568 }
569 } else if (!strcmp(key, "collector_set_id")) {
bdda5aca 570 error = str_to_u32(value, &os->collector_set_id);
29089a54 571 } else if (!strcmp(key, "obs_domain_id")) {
bdda5aca 572 error = str_to_u32(value, &os->obs_domain_id);
29089a54 573 } else if (!strcmp(key, "obs_point_id")) {
bdda5aca 574 error = str_to_u32(value, &os->obs_point_id);
29089a54 575 } else {
bdda5aca
BP
576 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
577 key);
578 }
579 if (error) {
580 return error;
29089a54
RL
581 }
582 }
583 if (os->probability == 0) {
bdda5aca 584 return xstrdup("non-zero \"probability\" must be specified on sample");
29089a54 585 }
bdda5aca 586 return NULL;
29089a54
RL
587}
588
bdda5aca
BP
589/* Parses 'arg' as the argument to action 'code', and appends such an action to
590 * 'ofpacts'.
591 *
592 * Returns NULL if successful, otherwise a malloc()'d string describing the
593 * error. The caller is responsible for freeing the returned string. */
594static char * WARN_UNUSED_RESULT
dd43a558 595parse_named_action(enum ofputil_action_code code,
f25d0cf3 596 char *arg, struct ofpbuf *ofpacts)
333eba21 597{
bdda5aca 598 size_t orig_size = ofpacts->size;
f25d0cf3 599 struct ofpact_tunnel *tunnel;
bdda5aca 600 char *error = NULL;
4be17953
EJ
601 uint16_t ethertype = 0;
602 uint16_t vid = 0;
603 uint8_t tos = 0;
604 uint8_t pcp = 0;
333eba21
BP
605
606 switch (code) {
690a61c5
BP
607 case OFPUTIL_ACTION_INVALID:
608 NOT_REACHED();
609
08f94c0e 610 case OFPUTIL_OFPAT10_OUTPUT:
d01c980f 611 case OFPUTIL_OFPAT11_OUTPUT:
bdda5aca 612 error = parse_output(arg, ofpacts);
333eba21
BP
613 break;
614
08f94c0e 615 case OFPUTIL_OFPAT10_SET_VLAN_VID:
d01c980f 616 case OFPUTIL_OFPAT11_SET_VLAN_VID:
bdda5aca
BP
617 error = str_to_u16(arg, "VLAN VID", &vid);
618 if (error) {
619 return error;
620 }
621
f25d0cf3 622 if (vid & ~VLAN_VID_MASK) {
bdda5aca 623 return xasprintf("%s: not a valid VLAN VID", arg);
f25d0cf3
BP
624 }
625 ofpact_put_SET_VLAN_VID(ofpacts)->vlan_vid = vid;
333eba21
BP
626 break;
627
08f94c0e 628 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
d01c980f 629 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
bdda5aca
BP
630 error = str_to_u8(arg, "VLAN PCP", &pcp);
631 if (error) {
632 return error;
633 }
634
f25d0cf3 635 if (pcp & ~7) {
bdda5aca 636 return xasprintf("%s: not a valid VLAN PCP", arg);
f25d0cf3
BP
637 }
638 ofpact_put_SET_VLAN_PCP(ofpacts)->vlan_pcp = pcp;
333eba21
BP
639 break;
640
78a3fff6 641 case OFPUTIL_OFPAT12_SET_FIELD:
bdda5aca 642 return set_field_parse(arg, ofpacts);
78a3fff6 643
08f94c0e 644 case OFPUTIL_OFPAT10_STRIP_VLAN:
8e61c110 645 case OFPUTIL_OFPAT11_POP_VLAN:
f25d0cf3 646 ofpact_put_STRIP_VLAN(ofpacts);
333eba21
BP
647 break;
648
3e34fbdd 649 case OFPUTIL_OFPAT11_PUSH_VLAN:
bdda5aca
BP
650 error = str_to_u16(arg, "ethertype", &ethertype);
651 if (error) {
652 return error;
653 }
654
3e34fbdd 655 if (ethertype != ETH_TYPE_VLAN_8021Q) {
5dca28b5 656 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
bdda5aca 657 return xasprintf("%s: not a valid VLAN ethertype", arg);
3e34fbdd 658 }
bdda5aca 659
3e34fbdd
IY
660 ofpact_put_PUSH_VLAN(ofpacts);
661 break;
662
276c4e7a 663 case OFPUTIL_OFPAT11_SET_QUEUE:
bdda5aca 664 error = str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
276c4e7a
SH
665 break;
666
08f94c0e 667 case OFPUTIL_OFPAT10_SET_DL_SRC:
d01c980f 668 case OFPUTIL_OFPAT11_SET_DL_SRC:
bdda5aca 669 error = str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac);
f25d0cf3
BP
670 break;
671
08f94c0e 672 case OFPUTIL_OFPAT10_SET_DL_DST:
d01c980f 673 case OFPUTIL_OFPAT11_SET_DL_DST:
bdda5aca 674 error = str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac);
333eba21
BP
675 break;
676
08f94c0e 677 case OFPUTIL_OFPAT10_SET_NW_SRC:
d01c980f 678 case OFPUTIL_OFPAT11_SET_NW_SRC:
bdda5aca 679 error = str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
f25d0cf3
BP
680 break;
681
08f94c0e 682 case OFPUTIL_OFPAT10_SET_NW_DST:
d01c980f 683 case OFPUTIL_OFPAT11_SET_NW_DST:
bdda5aca 684 error = str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
333eba21
BP
685 break;
686
08f94c0e 687 case OFPUTIL_OFPAT10_SET_NW_TOS:
d01c980f 688 case OFPUTIL_OFPAT11_SET_NW_TOS:
bdda5aca
BP
689 error = str_to_u8(arg, "TOS", &tos);
690 if (error) {
691 return error;
692 }
693
f25d0cf3 694 if (tos & ~IP_DSCP_MASK) {
bdda5aca 695 return xasprintf("%s: not a valid TOS", arg);
f25d0cf3
BP
696 }
697 ofpact_put_SET_IPV4_DSCP(ofpacts)->dscp = tos;
333eba21
BP
698 break;
699
7bcb1506 700 case OFPUTIL_OFPAT11_DEC_NW_TTL:
68194f84 701 NOT_REACHED();
7bcb1506 702
08f94c0e 703 case OFPUTIL_OFPAT10_SET_TP_SRC:
d01c980f 704 case OFPUTIL_OFPAT11_SET_TP_SRC:
bdda5aca
BP
705 error = str_to_u16(arg, "source port",
706 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
f25d0cf3
BP
707 break;
708
08f94c0e 709 case OFPUTIL_OFPAT10_SET_TP_DST:
d01c980f 710 case OFPUTIL_OFPAT11_SET_TP_DST:
bdda5aca
BP
711 error = str_to_u16(arg, "destination port",
712 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
333eba21
BP
713 break;
714
08f94c0e 715 case OFPUTIL_OFPAT10_ENQUEUE:
bdda5aca 716 error = parse_enqueue(arg, ofpacts);
333eba21
BP
717 break;
718
719 case OFPUTIL_NXAST_RESUBMIT:
bdda5aca 720 error = parse_resubmit(arg, ofpacts);
333eba21
BP
721 break;
722
723 case OFPUTIL_NXAST_SET_TUNNEL:
f25d0cf3
BP
724 case OFPUTIL_NXAST_SET_TUNNEL64:
725 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
726 tunnel->ofpact.compat = code;
bdda5aca 727 error = str_to_u64(arg, &tunnel->tun_id);
333eba21
BP
728 break;
729
4cceacb9 730 case OFPUTIL_NXAST_WRITE_METADATA:
bdda5aca 731 error = parse_metadata(ofpacts, arg);
4cceacb9
JS
732 break;
733
333eba21 734 case OFPUTIL_NXAST_SET_QUEUE:
bdda5aca 735 error = str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
333eba21
BP
736 break;
737
738 case OFPUTIL_NXAST_POP_QUEUE:
f25d0cf3 739 ofpact_put_POP_QUEUE(ofpacts);
333eba21
BP
740 break;
741
742 case OFPUTIL_NXAST_REG_MOVE:
bdda5aca 743 error = nxm_parse_reg_move(ofpact_put_REG_MOVE(ofpacts), arg);
333eba21
BP
744 break;
745
746 case OFPUTIL_NXAST_REG_LOAD:
bdda5aca 747 error = nxm_parse_reg_load(ofpact_put_REG_LOAD(ofpacts), arg);
333eba21
BP
748 break;
749
750 case OFPUTIL_NXAST_NOTE:
bdda5aca 751 error = parse_note(arg, ofpacts);
93996add
BP
752 break;
753
333eba21 754 case OFPUTIL_NXAST_MULTIPATH:
bdda5aca 755 error = multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
333eba21
BP
756 break;
757
333eba21 758 case OFPUTIL_NXAST_BUNDLE:
bdda5aca 759 error = bundle_parse(arg, ofpacts);
333eba21
BP
760 break;
761
762 case OFPUTIL_NXAST_BUNDLE_LOAD:
bdda5aca 763 error = bundle_parse_load(arg, ofpacts);
333eba21
BP
764 break;
765
766 case OFPUTIL_NXAST_RESUBMIT_TABLE:
767 case OFPUTIL_NXAST_OUTPUT_REG:
c2d967a5 768 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
333eba21 769 NOT_REACHED();
75a75043
BP
770
771 case OFPUTIL_NXAST_LEARN:
bdda5aca 772 error = learn_parse(arg, ofpacts);
75a75043 773 break;
a61680c6 774
848e8809 775 case OFPUTIL_NXAST_EXIT:
f25d0cf3 776 ofpact_put_EXIT(ofpacts);
848e8809 777 break;
f0fd1a17
PS
778
779 case OFPUTIL_NXAST_DEC_TTL:
bdda5aca 780 error = parse_dec_ttl(ofpacts, arg);
f0fd1a17 781 break;
0e553d9c 782
0f3f3c3d
SH
783 case OFPUTIL_NXAST_SET_MPLS_TTL:
784 case OFPUTIL_OFPAT11_SET_MPLS_TTL:
bdda5aca 785 error = parse_set_mpls_ttl(ofpacts, arg);
0f3f3c3d
SH
786 break;
787
b676167a
SH
788 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
789 case OFPUTIL_NXAST_DEC_MPLS_TTL:
790 ofpact_put_DEC_MPLS_TTL(ofpacts);
791 break;
792
0e553d9c 793 case OFPUTIL_NXAST_FIN_TIMEOUT:
bdda5aca 794 error = parse_fin_timeout(ofpacts, arg);
0e553d9c 795 break;
a7349929
BP
796
797 case OFPUTIL_NXAST_CONTROLLER:
bdda5aca 798 error = parse_controller(ofpacts, arg);
a7349929 799 break;
b02475c5
SH
800
801 case OFPUTIL_OFPAT11_PUSH_MPLS:
802 case OFPUTIL_NXAST_PUSH_MPLS:
bdda5aca
BP
803 error = str_to_u16(arg, "push_mpls", &ethertype);
804 if (!error) {
805 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
806 }
b02475c5
SH
807 break;
808
809 case OFPUTIL_OFPAT11_POP_MPLS:
810 case OFPUTIL_NXAST_POP_MPLS:
bdda5aca
BP
811 error = str_to_u16(arg, "pop_mpls", &ethertype);
812 if (!error) {
813 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
814 }
b02475c5 815 break;
29089a54 816
bd85dac1 817 case OFPUTIL_NXAST_STACK_PUSH:
bdda5aca 818 error = nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
bd85dac1
AZ
819 break;
820 case OFPUTIL_NXAST_STACK_POP:
bdda5aca 821 error = nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
bd85dac1 822 break;
29089a54
RL
823
824 case OFPUTIL_NXAST_SAMPLE:
bdda5aca 825 error = parse_sample(ofpacts, arg);
29089a54 826 break;
333eba21 827 }
bdda5aca
BP
828
829 if (error) {
830 ofpacts->size = orig_size;
831 }
832 return error;
333eba21
BP
833}
834
bdda5aca
BP
835/* Parses action 'act', with argument 'arg', and appends a parsed version to
836 * 'ofpacts'.
837 *
838 * 'n_actions' specifies the number of actions already parsed (for proper
839 * handling of "drop" actions).
840 *
841 * Returns NULL if successful, otherwise a malloc()'d string describing the
842 * error. The caller is responsible for freeing the returned string. */
843static char * WARN_UNUSED_RESULT
dd43a558 844str_to_ofpact__(char *pos, char *act, char *arg,
8dd54666
IY
845 struct ofpbuf *ofpacts, int n_actions)
846{
847 int code = ofputil_action_code_from_name(act);
848 if (code >= 0) {
bdda5aca 849 return parse_named_action(code, arg, ofpacts);
8dd54666
IY
850 } else if (!strcasecmp(act, "drop")) {
851 if (n_actions) {
bdda5aca
BP
852 return xstrdup("Drop actions must not be preceded by other "
853 "actions");
8dd54666 854 } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
bdda5aca
BP
855 return xstrdup("Drop actions must not be followed by other "
856 "actions");
8dd54666 857 }
8dd54666 858 } else {
4e022ec0 859 ofp_port_t port;
8010100b 860 if (ofputil_port_from_string(act, &port)) {
8dd54666
IY
861 ofpact_put_OUTPUT(ofpacts)->port = port;
862 } else {
bdda5aca 863 return xasprintf("Unknown action: %s", act);
8dd54666
IY
864 }
865 }
866
bdda5aca 867 return NULL;
8dd54666
IY
868}
869
bdda5aca
BP
870/* Parses 'str' as a series of actions, and appends them to 'ofpacts'.
871 *
872 * Returns NULL if successful, otherwise a malloc()'d string describing the
873 * error. The caller is responsible for freeing the returned string. */
874static char * WARN_UNUSED_RESULT
dd43a558 875str_to_ofpacts(char *str, struct ofpbuf *ofpacts)
f22716dc 876{
bdda5aca 877 size_t orig_size = ofpacts->size;
0ff22822 878 char *pos, *act, *arg;
4cceacb9 879 enum ofperr error;
f22716dc
JP
880 int n_actions;
881
53ddd40a 882 pos = str;
d13803eb 883 n_actions = 0;
0ff22822 884 while (ofputil_parse_key_value(&pos, &act, &arg)) {
bdda5aca
BP
885 char *error = str_to_ofpact__(pos, act, arg, ofpacts, n_actions);
886 if (error) {
887 ofpacts->size = orig_size;
888 return error;
8dd54666
IY
889 }
890 n_actions++;
891 }
4cceacb9
JS
892
893 error = ofpacts_verify(ofpacts->data, ofpacts->size);
894 if (error) {
bdda5aca
BP
895 ofpacts->size = orig_size;
896 return xstrdup("Incorrect action ordering");
4cceacb9
JS
897 }
898
8dd54666 899 ofpact_pad(ofpacts);
bdda5aca 900 return NULL;
8dd54666
IY
901}
902
bdda5aca
BP
903/* Parses 'arg' as the argument to instruction 'type', and appends such an
904 * instruction to 'ofpacts'.
905 *
906 * Returns NULL if successful, otherwise a malloc()'d string describing the
907 * error. The caller is responsible for freeing the returned string. */
908static char * WARN_UNUSED_RESULT
8dd54666
IY
909parse_named_instruction(enum ovs_instruction_type type,
910 char *arg, struct ofpbuf *ofpacts)
911{
bdda5aca 912 char *error_s = NULL;
4cceacb9
JS
913 enum ofperr error;
914
8dd54666
IY
915 switch (type) {
916 case OVSINST_OFPIT11_APPLY_ACTIONS:
917 NOT_REACHED(); /* This case is handled by str_to_inst_ofpacts() */
918 break;
919
920 case OVSINST_OFPIT11_WRITE_ACTIONS:
5dca28b5 921 /* XXX */
bdda5aca 922 error_s = xstrdup("instruction write-actions is not supported yet");
8dd54666
IY
923 break;
924
925 case OVSINST_OFPIT11_CLEAR_ACTIONS:
b19e8793 926 ofpact_put_CLEAR_ACTIONS(ofpacts);
8dd54666
IY
927 break;
928
638a19b0 929 case OVSINST_OFPIT13_METER:
bdda5aca 930 error_s = str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
638a19b0
JR
931 break;
932
8dd54666 933 case OVSINST_OFPIT11_WRITE_METADATA:
bdda5aca 934 error_s = parse_metadata(ofpacts, arg);
8dd54666
IY
935 break;
936
937 case OVSINST_OFPIT11_GOTO_TABLE: {
938 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
939 char *table_s = strsep(&arg, ",");
940 if (!table_s || !table_s[0]) {
bdda5aca 941 return xstrdup("instruction goto-table needs table id");
8dd54666 942 }
bdda5aca 943 error_s = str_to_u8(table_s, "table", &ogt->table_id);
8dd54666
IY
944 break;
945 }
946 }
4cceacb9 947
bdda5aca
BP
948 if (error_s) {
949 return error_s;
950 }
951
4cceacb9
JS
952 /* If write_metadata is specified as an action AND an instruction, ofpacts
953 could be invalid. */
954 error = ofpacts_verify(ofpacts->data, ofpacts->size);
955 if (error) {
bdda5aca 956 return xstrdup("Incorrect instruction ordering");
4cceacb9 957 }
bdda5aca 958 return NULL;
8dd54666
IY
959}
960
bdda5aca
BP
961/* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
962 *
963 * Returns NULL if successful, otherwise a malloc()'d string describing the
964 * error. The caller is responsible for freeing the returned string. */
965static char * WARN_UNUSED_RESULT
dd43a558 966str_to_inst_ofpacts(char *str, struct ofpbuf *ofpacts)
8dd54666 967{
bdda5aca 968 size_t orig_size = ofpacts->size;
8dd54666
IY
969 char *pos, *inst, *arg;
970 int type;
971 const char *prev_inst = NULL;
972 int prev_type = -1;
973 int n_actions = 0;
974
975 pos = str;
976 while (ofputil_parse_key_value(&pos, &inst, &arg)) {
ba0bc9b0 977 type = ovs_instruction_type_from_name(inst);
8dd54666 978 if (type < 0) {
bdda5aca
BP
979 char *error = str_to_ofpact__(pos, inst, arg, ofpacts, n_actions);
980 if (error) {
981 ofpacts->size = orig_size;
982 return error;
8dd54666
IY
983 }
984
985 type = OVSINST_OFPIT11_APPLY_ACTIONS;
986 if (prev_type == type) {
987 n_actions++;
988 continue;
c6100d92 989 }
8dd54666 990 } else if (type == OVSINST_OFPIT11_APPLY_ACTIONS) {
bdda5aca
BP
991 ofpacts->size = orig_size;
992 return xasprintf("%s isn't supported. Just write actions then "
993 "it is interpreted as apply_actions", inst);
8dd54666 994 } else {
bdda5aca
BP
995 char *error = parse_named_instruction(type, arg, ofpacts);
996 if (error) {
997 ofpacts->size = orig_size;
998 return error;
999 }
8dd54666
IY
1000 }
1001
8dd54666 1002 if (type <= prev_type) {
bdda5aca
BP
1003 ofpacts->size = orig_size;
1004 if (type == prev_type) {
1005 return xasprintf("instruction %s may be specified only once",
1006 inst);
1007 } else {
1008 return xasprintf("instruction %s must be specified before %s",
1009 inst, prev_inst);
1010 }
8dd54666
IY
1011 }
1012
1013 prev_inst = inst;
1014 prev_type = type;
d13803eb 1015 n_actions++;
f22716dc 1016 }
f25d0cf3 1017 ofpact_pad(ofpacts);
bdda5aca
BP
1018
1019 return NULL;
f22716dc
JP
1020}
1021
1022struct protocol {
1023 const char *name;
1024 uint16_t dl_type;
1025 uint8_t nw_proto;
1026};
1027
1028static bool
1029parse_protocol(const char *name, const struct protocol **p_out)
1030{
1031 static const struct protocol protocols[] = {
1032 { "ip", ETH_TYPE_IP, 0 },
1033 { "arp", ETH_TYPE_ARP, 0 },
6767a2cc
JP
1034 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
1035 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
1036 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
0d56eaf2 1037 { "sctp", ETH_TYPE_IP, IPPROTO_SCTP },
d31f1109
JP
1038 { "ipv6", ETH_TYPE_IPV6, 0 },
1039 { "ip6", ETH_TYPE_IPV6, 0 },
1040 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
1041 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
1042 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
0d56eaf2 1043 { "sctp6", ETH_TYPE_IPV6, IPPROTO_SCTP },
8087f5ff 1044 { "rarp", ETH_TYPE_RARP, 0},
b02475c5
SH
1045 { "mpls", ETH_TYPE_MPLS, 0 },
1046 { "mplsm", ETH_TYPE_MPLS_MCAST, 0 },
1047 };
f22716dc
JP
1048 const struct protocol *p;
1049
1050 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
1051 if (!strcmp(p->name, name)) {
1052 *p_out = p;
1053 return true;
1054 }
1055 }
1056 *p_out = NULL;
1057 return false;
1058}
1059
bdda5aca
BP
1060/* Parses 's' as the (possibly masked) value of field 'mf', and updates
1061 * 'match' appropriately.
1062 *
1063 * Returns NULL if successful, otherwise a malloc()'d string describing the
1064 * error. The caller is responsible for freeing the returned string. */
1065static char * WARN_UNUSED_RESULT
81a76618 1066parse_field(const struct mf_field *mf, const char *s, struct match *match)
8050b31d 1067{
6a885fd0
BP
1068 union mf_value value, mask;
1069 char *error;
bad68a99 1070
6a885fd0 1071 error = mf_parse(mf, s, &value, &mask);
bdda5aca
BP
1072 if (!error) {
1073 mf_set(mf, &value, &mask, match);
8050b31d 1074 }
bdda5aca 1075 return error;
00b1c62f
BP
1076}
1077
bdda5aca
BP
1078static char * WARN_UNUSED_RESULT
1079parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string)
f22716dc 1080{
c821124b
BP
1081 enum {
1082 F_OUT_PORT = 1 << 0,
1083 F_ACTIONS = 1 << 1,
c821124b 1084 F_TIMEOUT = 1 << 3,
a993007b
BP
1085 F_PRIORITY = 1 << 4,
1086 F_FLAGS = 1 << 5,
c821124b 1087 } fields;
f22716dc 1088 char *save_ptr = NULL;
75a75043 1089 char *act_str = NULL;
f22716dc 1090 char *name;
f22716dc 1091
c821124b
BP
1092 switch (command) {
1093 case -1:
1094 fields = F_OUT_PORT;
1095 break;
1096
1097 case OFPFC_ADD:
a993007b 1098 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
c821124b
BP
1099 break;
1100
1101 case OFPFC_DELETE:
1102 fields = F_OUT_PORT;
1103 break;
1104
1105 case OFPFC_DELETE_STRICT:
1106 fields = F_OUT_PORT | F_PRIORITY;
1107 break;
1108
1109 case OFPFC_MODIFY:
a993007b 1110 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
c821124b
BP
1111 break;
1112
1113 case OFPFC_MODIFY_STRICT:
a993007b 1114 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
c821124b
BP
1115 break;
1116
1117 default:
1118 NOT_REACHED();
1119 }
1120
81a76618
BP
1121 match_init_catchall(&fm->match);
1122 fm->priority = OFP_DEFAULT_PRIORITY;
88ca35ee 1123 fm->cookie = htonll(0);
e729e793 1124 fm->cookie_mask = htonll(0);
623e1caf
JP
1125 if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
1126 /* For modify, by default, don't update the cookie. */
1127 fm->new_cookie = htonll(UINT64_MAX);
1128 } else{
1129 fm->new_cookie = htonll(0);
1130 }
23342857 1131 fm->modify_cookie = false;
6c1491fb 1132 fm->table_id = 0xff;
c821124b 1133 fm->command = command;
88ca35ee
BP
1134 fm->idle_timeout = OFP_FLOW_PERMANENT;
1135 fm->hard_timeout = OFP_FLOW_PERMANENT;
1136 fm->buffer_id = UINT32_MAX;
7f05e7ab 1137 fm->out_port = OFPP_ANY;
88ca35ee 1138 fm->flags = 0;
c821124b 1139 if (fields & F_ACTIONS) {
c821124b 1140 act_str = strstr(string, "action");
f22716dc 1141 if (!act_str) {
bdda5aca 1142 return xstrdup("must specify an action");
f22716dc
JP
1143 }
1144 *act_str = '\0';
1145
1146 act_str = strchr(act_str + 1, '=');
1147 if (!act_str) {
bdda5aca 1148 return xstrdup("must specify an action");
f22716dc
JP
1149 }
1150
1151 act_str++;
f22716dc 1152 }
f22716dc
JP
1153 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1154 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1155 const struct protocol *p;
bdda5aca 1156 char *error = NULL;
f22716dc
JP
1157
1158 if (parse_protocol(name, &p)) {
81a76618 1159 match_set_dl_type(&fm->match, htons(p->dl_type));
f22716dc 1160 if (p->nw_proto) {
81a76618 1161 match_set_nw_proto(&fm->match, p->nw_proto);
f22716dc 1162 }
a993007b
BP
1163 } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
1164 fm->flags |= OFPFF_SEND_FLOW_REM;
1165 } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
1166 fm->flags |= OFPFF_CHECK_OVERLAP;
2e1ae200
JR
1167 } else if (fields & F_FLAGS && !strcmp(name, "reset_counts")) {
1168 fm->flags |= OFPFF12_RESET_COUNTS;
1169 } else if (fields & F_FLAGS && !strcmp(name, "no_packet_counts")) {
1170 fm->flags |= OFPFF13_NO_PKT_COUNTS;
1171 } else if (fields & F_FLAGS && !strcmp(name, "no_byte_counts")) {
1172 fm->flags |= OFPFF13_NO_BYT_COUNTS;
f22716dc 1173 } else {
f22716dc
JP
1174 char *value;
1175
1176 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1177 if (!value) {
bdda5aca 1178 return xasprintf("field %s missing value", name);
f22716dc
JP
1179 }
1180
6c1491fb 1181 if (!strcmp(name, "table")) {
bdda5aca 1182 error = str_to_u8(value, "table", &fm->table_id);
8050b31d 1183 } else if (!strcmp(name, "out_port")) {
be3f512a 1184 if (!ofputil_port_from_string(value, &fm->out_port)) {
bdda5aca
BP
1185 error = xasprintf("%s is not a valid OpenFlow port",
1186 value);
c6100d92 1187 }
c821124b 1188 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
4be17953 1189 uint16_t priority = 0;
bdda5aca
BP
1190
1191 error = str_to_u16(value, name, &priority);
1192 fm->priority = priority;
c821124b 1193 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
bdda5aca 1194 error = str_to_u16(value, name, &fm->idle_timeout);
c821124b 1195 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
bdda5aca 1196 error = str_to_u16(value, name, &fm->hard_timeout);
e729e793
JP
1197 } else if (!strcmp(name, "cookie")) {
1198 char *mask = strchr(value, '/');
623e1caf 1199
e729e793 1200 if (mask) {
623e1caf 1201 /* A mask means we're searching for a cookie. */
e729e793 1202 if (command == OFPFC_ADD) {
bdda5aca
BP
1203 return xstrdup("flow additions cannot use "
1204 "a cookie mask");
e729e793
JP
1205 }
1206 *mask = '\0';
bdda5aca
BP
1207 error = str_to_be64(value, &fm->cookie);
1208 if (error) {
1209 return error;
1210 }
1211 error = str_to_be64(mask + 1, &fm->cookie_mask);
e729e793 1212 } else {
623e1caf
JP
1213 /* No mask means that the cookie is being set. */
1214 if (command != OFPFC_ADD && command != OFPFC_MODIFY
bdda5aca
BP
1215 && command != OFPFC_MODIFY_STRICT) {
1216 return xstrdup("cannot set cookie");
623e1caf 1217 }
bdda5aca 1218 error = str_to_be64(value, &fm->new_cookie);
23342857 1219 fm->modify_cookie = true;
e729e793 1220 }
6a885fd0 1221 } else if (mf_from_name(name)) {
bdda5aca 1222 error = parse_field(mf_from_name(name), value, &fm->match);
2c6d8411
BP
1223 } else if (!strcmp(name, "duration")
1224 || !strcmp(name, "n_packets")
146356e9
JP
1225 || !strcmp(name, "n_bytes")
1226 || !strcmp(name, "idle_age")
1227 || !strcmp(name, "hard_age")) {
2c6d8411
BP
1228 /* Ignore these, so that users can feed the output of
1229 * "ovs-ofctl dump-flows" back into commands that parse
1230 * flows. */
f22716dc 1231 } else {
bdda5aca
BP
1232 error = xasprintf("unknown keyword %s", name);
1233 }
1234
1235 if (error) {
1236 return error;
f22716dc
JP
1237 }
1238 }
1239 }
623e1caf 1240 if (!fm->cookie_mask && fm->new_cookie == htonll(UINT64_MAX)
bdda5aca 1241 && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
623e1caf
JP
1242 /* On modifies without a mask, we are supposed to add a flow if
1243 * one does not exist. If a cookie wasn't been specified, use a
1244 * default of zero. */
1245 fm->new_cookie = htonll(0);
1246 }
75a75043 1247 if (fields & F_ACTIONS) {
f25d0cf3 1248 struct ofpbuf ofpacts;
bdda5aca 1249 char *error;
75a75043 1250
f25d0cf3 1251 ofpbuf_init(&ofpacts, 32);
bdda5aca
BP
1252 error = str_to_inst_ofpacts(act_str, &ofpacts);
1253 if (!error) {
1254 enum ofperr err;
1255
1256 err = ofpacts_check(ofpacts.data, ofpacts.size, &fm->match.flow,
1257 OFPP_MAX, 0);
1258 if (err) {
1259 error = xasprintf("actions are invalid with specified match "
1260 "(%s)", ofperr_to_string(err));
1261 }
1262 }
1263 if (error) {
1264 ofpbuf_uninit(&ofpacts);
1265 return error;
b019d34d
SH
1266 }
1267
bdda5aca
BP
1268 fm->ofpacts_len = ofpacts.size;
1269 fm->ofpacts = ofpbuf_steal_data(&ofpacts);
75a75043 1270 } else {
f25d0cf3
BP
1271 fm->ofpacts_len = 0;
1272 fm->ofpacts = NULL;
75a75043 1273 }
ec610b7b 1274
bdda5aca 1275 return NULL;
f22716dc 1276}
15f1f1b6 1277
638a19b0 1278/* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
bdda5aca
BP
1279 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
1280 *
1281 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
1282 * constant for 'command'. To parse syntax for an OFPST_FLOW or
1283 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'.
1284 *
1285 * Returns NULL if successful, otherwise a malloc()'d string describing the
1286 * error. The caller is responsible for freeing the returned string. */
1287char * WARN_UNUSED_RESULT
1288parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_)
1289{
1290 char *string = xstrdup(str_);
1291 char *error;
1292
1293 error = parse_ofp_str__(fm, command, string);
1294 if (error) {
1295 fm->ofpacts = NULL;
1296 fm->ofpacts_len = 0;
1297 }
1298
1299 free(string);
1300 return error;
1301}
1302
1303static char * WARN_UNUSED_RESULT
1304parse_ofp_meter_mod_str__(struct ofputil_meter_mod *mm, char *string,
1305 struct ofpbuf *bands, int command)
638a19b0
JR
1306{
1307 enum {
1308 F_METER = 1 << 0,
1309 F_FLAGS = 1 << 1,
1310 F_BANDS = 1 << 2,
1311 } fields;
638a19b0
JR
1312 char *save_ptr = NULL;
1313 char *band_str = NULL;
1314 char *name;
1315
1316 switch (command) {
1317 case -1:
1318 fields = F_METER;
1319 break;
1320
1321 case OFPMC13_ADD:
1322 fields = F_METER | F_FLAGS | F_BANDS;
1323 break;
1324
1325 case OFPMC13_DELETE:
1326 fields = F_METER;
1327 break;
1328
1329 case OFPMC13_MODIFY:
1330 fields = F_METER | F_FLAGS | F_BANDS;
1331 break;
1332
1333 default:
1334 NOT_REACHED();
1335 }
1336
1337 mm->command = command;
1338 mm->meter.meter_id = 0;
1339 mm->meter.flags = 0;
1340 if (fields & F_BANDS) {
1341 band_str = strstr(string, "band");
1342 if (!band_str) {
bdda5aca 1343 return xstrdup("must specify bands");
638a19b0
JR
1344 }
1345 *band_str = '\0';
1346
1347 band_str = strchr(band_str + 1, '=');
1348 if (!band_str) {
bdda5aca 1349 return xstrdup("must specify bands");
638a19b0
JR
1350 }
1351
1352 band_str++;
1353 }
1354 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1355 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1356
1357 if (fields & F_FLAGS && !strcmp(name, "kbps")) {
1358 mm->meter.flags |= OFPMF13_KBPS;
1359 } else if (fields & F_FLAGS && !strcmp(name, "pktps")) {
1360 mm->meter.flags |= OFPMF13_PKTPS;
1361 } else if (fields & F_FLAGS && !strcmp(name, "burst")) {
1362 mm->meter.flags |= OFPMF13_BURST;
1363 } else if (fields & F_FLAGS && !strcmp(name, "stats")) {
1364 mm->meter.flags |= OFPMF13_STATS;
1365 } else {
1366 char *value;
1367
1368 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1369 if (!value) {
bdda5aca 1370 return xasprintf("field %s missing value", name);
638a19b0
JR
1371 }
1372
1373 if (!strcmp(name, "meter")) {
1374 if (!strcmp(value, "all")) {
1375 mm->meter.meter_id = OFPM13_ALL;
1376 } else if (!strcmp(value, "controller")) {
1377 mm->meter.meter_id = OFPM13_CONTROLLER;
1378 } else if (!strcmp(value, "slowpath")) {
1379 mm->meter.meter_id = OFPM13_SLOWPATH;
1380 } else {
bdda5aca
BP
1381 char *error = str_to_u32(value, &mm->meter.meter_id);
1382 if (error) {
1383 return error;
1384 }
638a19b0 1385 if (mm->meter.meter_id > OFPM13_MAX) {
bdda5aca 1386 return xasprintf("invalid value for %s", name);
638a19b0
JR
1387 }
1388 }
1389 } else {
bdda5aca 1390 return xasprintf("unknown keyword %s", name);
638a19b0
JR
1391 }
1392 }
1393 }
1394 if (fields & F_METER && !mm->meter.meter_id) {
bdda5aca 1395 return xstrdup("must specify 'meter'");
638a19b0
JR
1396 }
1397 if (fields & F_FLAGS && !mm->meter.flags) {
bdda5aca 1398 return xstrdup("meter must specify either 'kbps' or 'pktps'");
638a19b0
JR
1399 }
1400
1401 if (fields & F_BANDS) {
638a19b0
JR
1402 uint16_t n_bands = 0;
1403 struct ofputil_meter_band *band = NULL;
1404 int i;
1405
638a19b0
JR
1406 for (name = strtok_r(band_str, "=, \t\r\n", &save_ptr); name;
1407 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1408
1409 char *value;
1410
1411 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1412 if (!value) {
bdda5aca 1413 return xasprintf("field %s missing value", name);
638a19b0
JR
1414 }
1415
1416 if (!strcmp(name, "type")) {
1417 /* Start a new band */
bdda5aca 1418 band = ofpbuf_put_zeros(bands, sizeof *band);
638a19b0
JR
1419 n_bands++;
1420
1421 if (!strcmp(value, "drop")) {
1422 band->type = OFPMBT13_DROP;
1423 } else if (!strcmp(value, "dscp_remark")) {
1424 band->type = OFPMBT13_DSCP_REMARK;
1425 } else {
bdda5aca 1426 return xasprintf("field %s unknown value %s", name, value);
638a19b0
JR
1427 }
1428 } else if (!band || !band->type) {
bdda5aca 1429 return xstrdup("band must start with the 'type' keyword");
638a19b0 1430 } else if (!strcmp(name, "rate")) {
bdda5aca
BP
1431 char *error = str_to_u32(value, &band->rate);
1432 if (error) {
1433 return error;
1434 }
638a19b0 1435 } else if (!strcmp(name, "burst_size")) {
bdda5aca
BP
1436 char *error = str_to_u32(value, &band->burst_size);
1437 if (error) {
1438 return error;
1439 }
638a19b0 1440 } else if (!strcmp(name, "prec_level")) {
bdda5aca
BP
1441 char *error = str_to_u8(value, name, &band->prec_level);
1442 if (error) {
1443 return error;
1444 }
638a19b0 1445 } else {
bdda5aca 1446 return xasprintf("unknown keyword %s", name);
638a19b0
JR
1447 }
1448 }
1449 /* validate bands */
1450 if (!n_bands) {
bdda5aca 1451 return xstrdup("meter must have bands");
638a19b0
JR
1452 }
1453
1454 mm->meter.n_bands = n_bands;
bdda5aca 1455 mm->meter.bands = ofpbuf_steal_data(bands);
638a19b0
JR
1456
1457 for (i = 0; i < n_bands; ++i) {
1458 band = &mm->meter.bands[i];
1459
1460 if (!band->type) {
bdda5aca 1461 return xstrdup("band must have 'type'");
638a19b0
JR
1462 }
1463 if (band->type == OFPMBT13_DSCP_REMARK) {
1464 if (!band->prec_level) {
bdda5aca
BP
1465 return xstrdup("'dscp_remark' band must have"
1466 " 'prec_level'");
638a19b0
JR
1467 }
1468 } else {
1469 if (band->prec_level) {
bdda5aca
BP
1470 return xstrdup("Only 'dscp_remark' band may have"
1471 " 'prec_level'");
638a19b0
JR
1472 }
1473 }
1474 if (!band->rate) {
bdda5aca 1475 return xstrdup("band must have 'rate'");
638a19b0
JR
1476 }
1477 if (mm->meter.flags & OFPMF13_BURST) {
1478 if (!band->burst_size) {
bdda5aca
BP
1479 return xstrdup("band must have 'burst_size' "
1480 "when 'burst' flag is set");
638a19b0
JR
1481 }
1482 } else {
1483 if (band->burst_size) {
bdda5aca
BP
1484 return xstrdup("band may have 'burst_size' only "
1485 "when 'burst' flag is set");
638a19b0
JR
1486 }
1487 }
1488 }
1489 } else {
1490 mm->meter.n_bands = 0;
1491 mm->meter.bands = NULL;
1492 }
1493
bdda5aca
BP
1494 return NULL;
1495}
1496
1497/* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
1498 * page) into 'mm' for sending the specified meter_mod 'command' to a switch.
1499 *
1500 * Returns NULL if successful, otherwise a malloc()'d string describing the
1501 * error. The caller is responsible for freeing the returned string. */
1502char * WARN_UNUSED_RESULT
1503parse_ofp_meter_mod_str(struct ofputil_meter_mod *mm, const char *str_,
1504 int command)
1505{
1506 struct ofpbuf bands;
1507 char *string;
1508 char *error;
1509
1510 ofpbuf_init(&bands, 64);
1511 string = xstrdup(str_);
1512
1513 error = parse_ofp_meter_mod_str__(mm, string, &bands, command);
1514
638a19b0 1515 free(string);
bdda5aca
BP
1516 ofpbuf_uninit(&bands);
1517
1518 return error;
638a19b0
JR
1519}
1520
bdda5aca
BP
1521static char * WARN_UNUSED_RESULT
1522parse_flow_monitor_request__(struct ofputil_flow_monitor_request *fmr,
1523 const char *str_, char *string)
2b07c8b1 1524{
97be1538 1525 static atomic_uint32_t id = ATOMIC_VAR_INIT(0);
2b07c8b1
BP
1526 char *save_ptr = NULL;
1527 char *name;
1528
97be1538 1529 atomic_add(&id, 1, &fmr->id);
a944ef40 1530
2b07c8b1
BP
1531 fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY
1532 | NXFMF_OWN | NXFMF_ACTIONS);
1533 fmr->out_port = OFPP_NONE;
1534 fmr->table_id = 0xff;
81a76618 1535 match_init_catchall(&fmr->match);
2b07c8b1
BP
1536
1537 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1538 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1539 const struct protocol *p;
1540
1541 if (!strcmp(name, "!initial")) {
1542 fmr->flags &= ~NXFMF_INITIAL;
1543 } else if (!strcmp(name, "!add")) {
1544 fmr->flags &= ~NXFMF_ADD;
1545 } else if (!strcmp(name, "!delete")) {
1546 fmr->flags &= ~NXFMF_DELETE;
1547 } else if (!strcmp(name, "!modify")) {
1548 fmr->flags &= ~NXFMF_MODIFY;
1549 } else if (!strcmp(name, "!actions")) {
1550 fmr->flags &= ~NXFMF_ACTIONS;
1551 } else if (!strcmp(name, "!own")) {
1552 fmr->flags &= ~NXFMF_OWN;
1553 } else if (parse_protocol(name, &p)) {
81a76618 1554 match_set_dl_type(&fmr->match, htons(p->dl_type));
2b07c8b1 1555 if (p->nw_proto) {
81a76618 1556 match_set_nw_proto(&fmr->match, p->nw_proto);
2b07c8b1
BP
1557 }
1558 } else {
1559 char *value;
1560
1561 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1562 if (!value) {
bdda5aca 1563 return xasprintf("%s: field %s missing value", str_, name);
2b07c8b1
BP
1564 }
1565
1566 if (!strcmp(name, "table")) {
bdda5aca
BP
1567 char *error = str_to_u8(value, "table", &fmr->table_id);
1568 if (error) {
1569 return error;
1570 }
2b07c8b1 1571 } else if (!strcmp(name, "out_port")) {
4e022ec0 1572 fmr->out_port = u16_to_ofp(atoi(value));
2b07c8b1 1573 } else if (mf_from_name(name)) {
bdda5aca
BP
1574 char *error;
1575
1576 error = parse_field(mf_from_name(name), value, &fmr->match);
1577 if (error) {
1578 return error;
1579 }
2b07c8b1 1580 } else {
bdda5aca 1581 return xasprintf("%s: unknown keyword %s", str_, name);
2b07c8b1
BP
1582 }
1583 }
1584 }
bdda5aca
BP
1585 return NULL;
1586}
1587
1588/* Convert 'str_' (as described in the documentation for the "monitor" command
1589 * in the ovs-ofctl man page) into 'fmr'.
1590 *
1591 * Returns NULL if successful, otherwise a malloc()'d string describing the
1592 * error. The caller is responsible for freeing the returned string. */
1593char * WARN_UNUSED_RESULT
1594parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
1595 const char *str_)
1596{
1597 char *string = xstrdup(str_);
1598 char *error = parse_flow_monitor_request__(fmr, str_, string);
2b07c8b1 1599 free(string);
bdda5aca 1600 return error;
2b07c8b1
BP
1601}
1602
0c3d5fc8
BP
1603/* Parses 's' as a set of OpenFlow actions and appends the actions to
1604 * 'actions'.
1605 *
bdda5aca
BP
1606 * Returns NULL if successful, otherwise a malloc()'d string describing the
1607 * error. The caller is responsible for freeing the returned string. */
1608char * WARN_UNUSED_RESULT
f25d0cf3 1609parse_ofpacts(const char *s_, struct ofpbuf *ofpacts)
0c3d5fc8
BP
1610{
1611 char *s = xstrdup(s_);
bdda5aca 1612 char *error = str_to_ofpacts(s, ofpacts);
0c3d5fc8 1613 free(s);
bdda5aca
BP
1614
1615 return error;
0c3d5fc8
BP
1616}
1617
88ca35ee 1618/* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
bdda5aca
BP
1619 * (one of OFPFC_*) into 'fm'.
1620 *
1621 * Returns NULL if successful, otherwise a malloc()'d string describing the
1622 * error. The caller is responsible for freeing the returned string. */
1623char * WARN_UNUSED_RESULT
27527aa0 1624parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
bdda5aca 1625 uint16_t command)
15f1f1b6 1626{
bdda5aca
BP
1627 char *error = parse_ofp_str(fm, command, string);
1628 if (!error) {
1629 /* Normalize a copy of the match. This ensures that non-normalized
1630 * flows get logged but doesn't affect what gets sent to the switch, so
1631 * that the switch can do whatever it likes with the flow. */
1632 struct match match_copy = fm->match;
1633 ofputil_normalize_match(&match_copy);
1634 }
88ca35ee 1635
bdda5aca 1636 return error;
15f1f1b6
BP
1637}
1638
bdda5aca
BP
1639/* Opens file 'file_name' and reads each line as a flow_mod of the specified
1640 * type (one of OFPFC_*). Stores each flow_mod in '*fm', an array allocated
1641 * on the caller's behalf, and the number of flow_mods in '*n_fms'.
1642 *
1643 * Returns NULL if successful, otherwise a malloc()'d string describing the
1644 * error. The caller is responsible for freeing the returned string. */
1645char * WARN_UNUSED_RESULT
27527aa0
BP
1646parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
1647 struct ofputil_flow_mod **fms, size_t *n_fms)
15f1f1b6 1648{
27527aa0 1649 size_t allocated_fms;
bdda5aca 1650 int line_number;
27527aa0 1651 FILE *stream;
dd8101bc 1652 struct ds s;
15f1f1b6 1653
bdda5aca
BP
1654 *fms = NULL;
1655 *n_fms = 0;
1656
27527aa0
BP
1657 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
1658 if (stream == NULL) {
bdda5aca
BP
1659 return xasprintf("%s: open failed (%s)",
1660 file_name, ovs_strerror(errno));
27527aa0
BP
1661 }
1662
1663 allocated_fms = *n_fms;
dd8101bc 1664 ds_init(&s);
bdda5aca
BP
1665 line_number = 0;
1666 while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
1667 char *error;
1668
27527aa0
BP
1669 if (*n_fms >= allocated_fms) {
1670 *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
1671 }
bdda5aca
BP
1672 error = parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command);
1673 if (error) {
1674 size_t i;
1675
1676 for (i = 0; i < *n_fms; i++) {
1677 free((*fms)[i].ofpacts);
1678 }
1679 free(*fms);
1680 *fms = NULL;
1681 *n_fms = 0;
1682
1683 ds_destroy(&s);
1684 if (stream != stdin) {
1685 fclose(stream);
1686 }
1687
1688 return xasprintf("%s:%d: %s", file_name, line_number, error);
1689 }
27527aa0 1690 *n_fms += 1;
15f1f1b6 1691 }
15f1f1b6 1692
bdda5aca 1693 ds_destroy(&s);
27527aa0
BP
1694 if (stream != stdin) {
1695 fclose(stream);
1696 }
bdda5aca 1697 return NULL;
88ca35ee
BP
1698}
1699
bdda5aca 1700char * WARN_UNUSED_RESULT
81d1ea94 1701parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
a7fc1744 1702 bool aggregate, const char *string)
88ca35ee 1703{
a9a2da38 1704 struct ofputil_flow_mod fm;
bdda5aca
BP
1705 char *error;
1706
1707 error = parse_ofp_str(&fm, -1, string);
1708 if (error) {
1709 return error;
1710 }
88ca35ee 1711
88ca35ee 1712 fsr->aggregate = aggregate;
e729e793
JP
1713 fsr->cookie = fm.cookie;
1714 fsr->cookie_mask = fm.cookie_mask;
81a76618 1715 fsr->match = fm.match;
88ca35ee 1716 fsr->out_port = fm.out_port;
6c1491fb 1717 fsr->table_id = fm.table_id;
bdda5aca 1718 return NULL;
15f1f1b6 1719}
ccbe50f8
BP
1720
1721/* Parses a specification of a flow from 's' into 'flow'. 's' must take the
1722 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
1723 * mf_field. Fields must be specified in a natural order for satisfying
1724 * prerequisites.
1725 *
1726 * Returns NULL on success, otherwise a malloc()'d string that explains the
1727 * problem. */
1728char *
1729parse_ofp_exact_flow(struct flow *flow, const char *s)
1730{
1731 char *pos, *key, *value_s;
1732 char *error = NULL;
1733 char *copy;
1734
1735 memset(flow, 0, sizeof *flow);
1736
1737 pos = copy = xstrdup(s);
1738 while (ofputil_parse_key_value(&pos, &key, &value_s)) {
1739 const struct protocol *p;
1740 if (parse_protocol(key, &p)) {
1741 if (flow->dl_type) {
1742 error = xasprintf("%s: Ethernet type set multiple times", s);
1743 goto exit;
1744 }
1745 flow->dl_type = htons(p->dl_type);
1746
1747 if (p->nw_proto) {
1748 if (flow->nw_proto) {
1749 error = xasprintf("%s: network protocol set "
1750 "multiple times", s);
1751 goto exit;
1752 }
1753 flow->nw_proto = p->nw_proto;
1754 }
1755 } else {
1756 const struct mf_field *mf;
1757 union mf_value value;
1758 char *field_error;
1759
1760 mf = mf_from_name(key);
1761 if (!mf) {
1762 error = xasprintf("%s: unknown field %s", s, key);
1763 goto exit;
1764 }
1765
1766 if (!mf_are_prereqs_ok(mf, flow)) {
1767 error = xasprintf("%s: prerequisites not met for setting %s",
1768 s, key);
1769 goto exit;
1770 }
1771
1772 if (!mf_is_zero(mf, flow)) {
1773 error = xasprintf("%s: field %s set multiple times", s, key);
1774 goto exit;
1775 }
1776
1777 field_error = mf_parse_value(mf, value_s, &value);
1778 if (field_error) {
1779 error = xasprintf("%s: bad value for %s (%s)",
1780 s, key, field_error);
1781 free(field_error);
1782 goto exit;
1783 }
1784
1785 mf_set_flow_value(mf, &value, flow);
1786 }
1787 }
1788
4e022ec0
AW
1789 if (!flow->in_port.ofp_port) {
1790 flow->in_port.ofp_port = OFPP_NONE;
72d64e33
EJ
1791 }
1792
ccbe50f8
BP
1793exit:
1794 free(copy);
1795
1796 if (error) {
1797 memset(flow, 0, sizeof *flow);
1798 }
1799 return error;
1800}