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