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