ofp-parse: Properly report error for invalid bucket ID.
[cascardo/ovs.git] / lib / ofp-parse.c
1 /*
2  * Copyright (c) 2010, 2011, 2012, 2013, 2014, 2015 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 "byte-order.h"
26 #include "dynamic-string.h"
27 #include "learn.h"
28 #include "meta-flow.h"
29 #include "multipath.h"
30 #include "netdev.h"
31 #include "nx-match.h"
32 #include "ofp-actions.h"
33 #include "ofp-util.h"
34 #include "ofpbuf.h"
35 #include "openflow/openflow.h"
36 #include "ovs-thread.h"
37 #include "packets.h"
38 #include "simap.h"
39 #include "socket-util.h"
40 #include "openvswitch/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 char * OVS_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 char * OVS_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 char * OVS_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 char * OVS_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 char * OVS_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 char * OVS_WARN_UNUSED_RESULT
147 str_to_mac(const char *str, uint8_t mac[ETH_ADDR_LEN])
148 {
149     if (!ovs_scan(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))) {
150         return xasprintf("invalid mac address %s", str);
151     }
152     return NULL;
153 }
154
155 /* Parses 'str' as an IP address into '*ip'.
156  *
157  * Returns NULL if successful, otherwise a malloc()'d string describing the
158  * error.  The caller is responsible for freeing the returned string. */
159 char * OVS_WARN_UNUSED_RESULT
160 str_to_ip(const char *str, ovs_be32 *ip)
161 {
162     struct in_addr in_addr;
163
164     if (lookup_ip(str, &in_addr)) {
165         return xasprintf("%s: could not convert to IP address", str);
166     }
167     *ip = in_addr.s_addr;
168     return NULL;
169 }
170
171 struct protocol {
172     const char *name;
173     uint16_t dl_type;
174     uint8_t nw_proto;
175 };
176
177 static bool
178 parse_protocol(const char *name, const struct protocol **p_out)
179 {
180     static const struct protocol protocols[] = {
181         { "ip", ETH_TYPE_IP, 0 },
182         { "arp", ETH_TYPE_ARP, 0 },
183         { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
184         { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
185         { "udp", ETH_TYPE_IP, IPPROTO_UDP },
186         { "sctp", ETH_TYPE_IP, IPPROTO_SCTP },
187         { "ipv6", ETH_TYPE_IPV6, 0 },
188         { "ip6", ETH_TYPE_IPV6, 0 },
189         { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
190         { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
191         { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
192         { "sctp6", ETH_TYPE_IPV6, IPPROTO_SCTP },
193         { "rarp", ETH_TYPE_RARP, 0},
194         { "mpls", ETH_TYPE_MPLS, 0 },
195         { "mplsm", ETH_TYPE_MPLS_MCAST, 0 },
196     };
197     const struct protocol *p;
198
199     for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
200         if (!strcmp(p->name, name)) {
201             *p_out = p;
202             return true;
203         }
204     }
205     *p_out = NULL;
206     return false;
207 }
208
209 /* Parses 's' as the (possibly masked) value of field 'mf', and updates
210  * 'match' appropriately.  Restricts the set of usable protocols to ones
211  * supporting the parsed field.
212  *
213  * Returns NULL if successful, otherwise a malloc()'d string describing the
214  * error.  The caller is responsible for freeing the returned string. */
215 static char * OVS_WARN_UNUSED_RESULT
216 parse_field(const struct mf_field *mf, const char *s, struct match *match,
217             enum ofputil_protocol *usable_protocols)
218 {
219     union mf_value value, mask;
220     char *error;
221
222     error = mf_parse(mf, s, &value, &mask);
223     if (!error) {
224         *usable_protocols &= mf_set(mf, &value, &mask, match);
225     }
226     return error;
227 }
228
229 static char *
230 extract_actions(char *s)
231 {
232     s = strstr(s, "action");
233     if (s) {
234         *s = '\0';
235         s = strchr(s + 1, '=');
236         return s ? s + 1 : NULL;
237     } else {
238         return NULL;
239     }
240 }
241
242
243 static char * OVS_WARN_UNUSED_RESULT
244 parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string,
245                 enum ofputil_protocol *usable_protocols)
246 {
247     enum {
248         F_OUT_PORT = 1 << 0,
249         F_ACTIONS = 1 << 1,
250         F_IMPORTANCE = 1 << 2,
251         F_TIMEOUT = 1 << 3,
252         F_PRIORITY = 1 << 4,
253         F_FLAGS = 1 << 5,
254     } fields;
255     char *save_ptr = NULL;
256     char *act_str = NULL;
257     char *name;
258
259     *usable_protocols = OFPUTIL_P_ANY;
260
261     switch (command) {
262     case -1:
263         fields = F_OUT_PORT;
264         break;
265
266     case OFPFC_ADD:
267         fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS | F_IMPORTANCE;
268         break;
269
270     case OFPFC_DELETE:
271         fields = F_OUT_PORT;
272         break;
273
274     case OFPFC_DELETE_STRICT:
275         fields = F_OUT_PORT | F_PRIORITY;
276         break;
277
278     case OFPFC_MODIFY:
279         fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
280         break;
281
282     case OFPFC_MODIFY_STRICT:
283         fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
284         break;
285
286     default:
287         OVS_NOT_REACHED();
288     }
289
290     match_init_catchall(&fm->match);
291     fm->priority = OFP_DEFAULT_PRIORITY;
292     fm->cookie = htonll(0);
293     fm->cookie_mask = htonll(0);
294     if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
295         /* For modify, by default, don't update the cookie. */
296         fm->new_cookie = OVS_BE64_MAX;
297     } else{
298         fm->new_cookie = htonll(0);
299     }
300     fm->modify_cookie = false;
301     fm->table_id = 0xff;
302     fm->command = command;
303     fm->idle_timeout = OFP_FLOW_PERMANENT;
304     fm->hard_timeout = OFP_FLOW_PERMANENT;
305     fm->buffer_id = UINT32_MAX;
306     fm->out_port = OFPP_ANY;
307     fm->flags = 0;
308     fm->importance = 0;
309     fm->out_group = OFPG11_ANY;
310     fm->delete_reason = OFPRR_DELETE;
311     if (fields & F_ACTIONS) {
312         act_str = extract_actions(string);
313         if (!act_str) {
314             return xstrdup("must specify an action");
315         }
316     }
317     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
318          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
319         const struct protocol *p;
320         char *error = NULL;
321
322         if (parse_protocol(name, &p)) {
323             match_set_dl_type(&fm->match, htons(p->dl_type));
324             if (p->nw_proto) {
325                 match_set_nw_proto(&fm->match, p->nw_proto);
326             }
327         } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
328             fm->flags |= OFPUTIL_FF_SEND_FLOW_REM;
329         } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
330             fm->flags |= OFPUTIL_FF_CHECK_OVERLAP;
331         } else if (fields & F_FLAGS && !strcmp(name, "reset_counts")) {
332             fm->flags |= OFPUTIL_FF_RESET_COUNTS;
333             *usable_protocols &= OFPUTIL_P_OF12_UP;
334         } else if (fields & F_FLAGS && !strcmp(name, "no_packet_counts")) {
335             fm->flags |= OFPUTIL_FF_NO_PKT_COUNTS;
336             *usable_protocols &= OFPUTIL_P_OF13_UP;
337         } else if (fields & F_FLAGS && !strcmp(name, "no_byte_counts")) {
338             fm->flags |= OFPUTIL_FF_NO_BYT_COUNTS;
339             *usable_protocols &= OFPUTIL_P_OF13_UP;
340         } else if (!strcmp(name, "no_readonly_table")
341                    || !strcmp(name, "allow_hidden_fields")) {
342              /* ignore these fields. */
343         } else {
344             char *value;
345
346             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
347             if (!value) {
348                 return xasprintf("field %s missing value", name);
349             }
350
351             if (!strcmp(name, "table")) {
352                 error = str_to_u8(value, "table", &fm->table_id);
353                 if (fm->table_id != 0xff) {
354                     *usable_protocols &= OFPUTIL_P_TID;
355                 }
356             } else if (!strcmp(name, "out_port")) {
357                 if (!ofputil_port_from_string(value, &fm->out_port)) {
358                     error = xasprintf("%s is not a valid OpenFlow port",
359                                       value);
360                 }
361             } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
362                 uint16_t priority = 0;
363
364                 error = str_to_u16(value, name, &priority);
365                 fm->priority = priority;
366             } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
367                 error = str_to_u16(value, name, &fm->idle_timeout);
368             } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
369                 error = str_to_u16(value, name, &fm->hard_timeout);
370             } else if (fields & F_IMPORTANCE && !strcmp(name, "importance")) {
371                 error = str_to_u16(value, name, &fm->importance);
372             } else if (!strcmp(name, "cookie")) {
373                 char *mask = strchr(value, '/');
374
375                 if (mask) {
376                     /* A mask means we're searching for a cookie. */
377                     if (command == OFPFC_ADD) {
378                         return xstrdup("flow additions cannot use "
379                                        "a cookie mask");
380                     }
381                     *mask = '\0';
382                     error = str_to_be64(value, &fm->cookie);
383                     if (error) {
384                         return error;
385                     }
386                     error = str_to_be64(mask + 1, &fm->cookie_mask);
387
388                     /* Matching of the cookie is only supported through NXM or
389                      * OF1.1+. */
390                     if (fm->cookie_mask != htonll(0)) {
391                         *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
392                     }
393                 } else {
394                     /* No mask means that the cookie is being set. */
395                     if (command != OFPFC_ADD && command != OFPFC_MODIFY
396                         && command != OFPFC_MODIFY_STRICT) {
397                         return xstrdup("cannot set cookie");
398                     }
399                     error = str_to_be64(value, &fm->new_cookie);
400                     fm->modify_cookie = true;
401                 }
402             } else if (mf_from_name(name)) {
403                 error = parse_field(mf_from_name(name), value, &fm->match,
404                                     usable_protocols);
405             } else if (!strcmp(name, "duration")
406                        || !strcmp(name, "n_packets")
407                        || !strcmp(name, "n_bytes")
408                        || !strcmp(name, "idle_age")
409                        || !strcmp(name, "hard_age")) {
410                 /* Ignore these, so that users can feed the output of
411                  * "ovs-ofctl dump-flows" back into commands that parse
412                  * flows. */
413             } else {
414                 error = xasprintf("unknown keyword %s", name);
415             }
416
417             if (error) {
418                 return error;
419             }
420         }
421     }
422     /* Check for usable protocol interdependencies between match fields. */
423     if (fm->match.flow.dl_type == htons(ETH_TYPE_IPV6)) {
424         const struct flow_wildcards *wc = &fm->match.wc;
425         /* Only NXM and OXM support matching L3 and L4 fields within IPv6.
426          *
427          * (IPv6 specific fields as well as arp_sha, arp_tha, nw_frag, and
428          *  nw_ttl are covered elsewhere so they don't need to be included in
429          *  this test too.)
430          */
431         if (wc->masks.nw_proto || wc->masks.nw_tos
432             || wc->masks.tp_src || wc->masks.tp_dst) {
433             *usable_protocols &= OFPUTIL_P_NXM_OXM_ANY;
434         }
435     }
436     if (!fm->cookie_mask && fm->new_cookie == OVS_BE64_MAX
437         && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
438         /* On modifies without a mask, we are supposed to add a flow if
439          * one does not exist.  If a cookie wasn't been specified, use a
440          * default of zero. */
441         fm->new_cookie = htonll(0);
442     }
443     if (fields & F_ACTIONS) {
444         enum ofputil_protocol action_usable_protocols;
445         struct ofpbuf ofpacts;
446         char *error;
447
448         ofpbuf_init(&ofpacts, 32);
449         error = ofpacts_parse_instructions(act_str, &ofpacts,
450                                            &action_usable_protocols);
451         *usable_protocols &= action_usable_protocols;
452         if (!error) {
453             enum ofperr err;
454
455             err = ofpacts_check(ofpacts.data, ofpacts.size, &fm->match.flow,
456                                 OFPP_MAX, fm->table_id, 255, usable_protocols);
457             if (!err && !usable_protocols) {
458                 err = OFPERR_OFPBAC_MATCH_INCONSISTENT;
459             }
460             if (err) {
461                 error = xasprintf("actions are invalid with specified match "
462                                   "(%s)", ofperr_to_string(err));
463             }
464
465         }
466         if (error) {
467             ofpbuf_uninit(&ofpacts);
468             return error;
469         }
470
471         fm->ofpacts_len = ofpacts.size;
472         fm->ofpacts = ofpbuf_steal_data(&ofpacts);
473     } else {
474         fm->ofpacts_len = 0;
475         fm->ofpacts = NULL;
476     }
477
478     return NULL;
479 }
480
481 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
482  * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
483  * Returns the set of usable protocols in '*usable_protocols'.
484  *
485  * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
486  * constant for 'command'.  To parse syntax for an OFPST_FLOW or
487  * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'.
488  *
489  * Returns NULL if successful, otherwise a malloc()'d string describing the
490  * error.  The caller is responsible for freeing the returned string. */
491 char * OVS_WARN_UNUSED_RESULT
492 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
493               enum ofputil_protocol *usable_protocols)
494 {
495     char *string = xstrdup(str_);
496     char *error;
497
498     error = parse_ofp_str__(fm, command, string, usable_protocols);
499     if (error) {
500         fm->ofpacts = NULL;
501         fm->ofpacts_len = 0;
502     }
503
504     free(string);
505     return error;
506 }
507
508 static char * OVS_WARN_UNUSED_RESULT
509 parse_ofp_meter_mod_str__(struct ofputil_meter_mod *mm, char *string,
510                           struct ofpbuf *bands, int command,
511                           enum ofputil_protocol *usable_protocols)
512 {
513     enum {
514         F_METER = 1 << 0,
515         F_FLAGS = 1 << 1,
516         F_BANDS = 1 << 2,
517     } fields;
518     char *save_ptr = NULL;
519     char *band_str = NULL;
520     char *name;
521
522     /* Meters require at least OF 1.3. */
523     *usable_protocols = OFPUTIL_P_OF13_UP;
524
525     switch (command) {
526     case -1:
527         fields = F_METER;
528         break;
529
530     case OFPMC13_ADD:
531         fields = F_METER | F_FLAGS | F_BANDS;
532         break;
533
534     case OFPMC13_DELETE:
535         fields = F_METER;
536         break;
537
538     case OFPMC13_MODIFY:
539         fields = F_METER | F_FLAGS | F_BANDS;
540         break;
541
542     default:
543         OVS_NOT_REACHED();
544     }
545
546     mm->command = command;
547     mm->meter.meter_id = 0;
548     mm->meter.flags = 0;
549     if (fields & F_BANDS) {
550         band_str = strstr(string, "band");
551         if (!band_str) {
552             return xstrdup("must specify bands");
553         }
554         *band_str = '\0';
555
556         band_str = strchr(band_str + 1, '=');
557         if (!band_str) {
558             return xstrdup("must specify bands");
559         }
560
561         band_str++;
562     }
563     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
564          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
565
566         if (fields & F_FLAGS && !strcmp(name, "kbps")) {
567             mm->meter.flags |= OFPMF13_KBPS;
568         } else if (fields & F_FLAGS && !strcmp(name, "pktps")) {
569             mm->meter.flags |= OFPMF13_PKTPS;
570         } else if (fields & F_FLAGS && !strcmp(name, "burst")) {
571             mm->meter.flags |= OFPMF13_BURST;
572         } else if (fields & F_FLAGS && !strcmp(name, "stats")) {
573             mm->meter.flags |= OFPMF13_STATS;
574         } else {
575             char *value;
576
577             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
578             if (!value) {
579                 return xasprintf("field %s missing value", name);
580             }
581
582             if (!strcmp(name, "meter")) {
583                 if (!strcmp(value, "all")) {
584                     mm->meter.meter_id = OFPM13_ALL;
585                 } else if (!strcmp(value, "controller")) {
586                     mm->meter.meter_id = OFPM13_CONTROLLER;
587                 } else if (!strcmp(value, "slowpath")) {
588                     mm->meter.meter_id = OFPM13_SLOWPATH;
589                 } else {
590                     char *error = str_to_u32(value, &mm->meter.meter_id);
591                     if (error) {
592                         return error;
593                     }
594                     if (mm->meter.meter_id > OFPM13_MAX
595                         || !mm->meter.meter_id) {
596                         return xasprintf("invalid value for %s", name);
597                     }
598                 }
599             } else {
600                 return xasprintf("unknown keyword %s", name);
601             }
602         }
603     }
604     if (fields & F_METER && !mm->meter.meter_id) {
605         return xstrdup("must specify 'meter'");
606     }
607     if (fields & F_FLAGS && !mm->meter.flags) {
608         return xstrdup("meter must specify either 'kbps' or 'pktps'");
609     }
610
611     if (fields & F_BANDS) {
612         uint16_t n_bands = 0;
613         struct ofputil_meter_band *band = NULL;
614         int i;
615
616         for (name = strtok_r(band_str, "=, \t\r\n", &save_ptr); name;
617              name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
618
619             char *value;
620
621             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
622             if (!value) {
623                 return xasprintf("field %s missing value", name);
624             }
625
626             if (!strcmp(name, "type")) {
627                 /* Start a new band */
628                 band = ofpbuf_put_zeros(bands, sizeof *band);
629                 n_bands++;
630
631                 if (!strcmp(value, "drop")) {
632                     band->type = OFPMBT13_DROP;
633                 } else if (!strcmp(value, "dscp_remark")) {
634                     band->type = OFPMBT13_DSCP_REMARK;
635                 } else {
636                     return xasprintf("field %s unknown value %s", name, value);
637                 }
638             } else if (!band || !band->type) {
639                 return xstrdup("band must start with the 'type' keyword");
640             } else if (!strcmp(name, "rate")) {
641                 char *error = str_to_u32(value, &band->rate);
642                 if (error) {
643                     return error;
644                 }
645             } else if (!strcmp(name, "burst_size")) {
646                 char *error = str_to_u32(value, &band->burst_size);
647                 if (error) {
648                     return error;
649                 }
650             } else if (!strcmp(name, "prec_level")) {
651                 char *error = str_to_u8(value, name, &band->prec_level);
652                 if (error) {
653                     return error;
654                 }
655             } else {
656                 return xasprintf("unknown keyword %s", name);
657             }
658         }
659         /* validate bands */
660         if (!n_bands) {
661             return xstrdup("meter must have bands");
662         }
663
664         mm->meter.n_bands = n_bands;
665         mm->meter.bands = ofpbuf_steal_data(bands);
666
667         for (i = 0; i < n_bands; ++i) {
668             band = &mm->meter.bands[i];
669
670             if (!band->type) {
671                 return xstrdup("band must have 'type'");
672             }
673             if (band->type == OFPMBT13_DSCP_REMARK) {
674                 if (!band->prec_level) {
675                     return xstrdup("'dscp_remark' band must have"
676                                    " 'prec_level'");
677                 }
678             } else {
679                 if (band->prec_level) {
680                     return xstrdup("Only 'dscp_remark' band may have"
681                                    " 'prec_level'");
682                 }
683             }
684             if (!band->rate) {
685                 return xstrdup("band must have 'rate'");
686             }
687             if (mm->meter.flags & OFPMF13_BURST) {
688                 if (!band->burst_size) {
689                     return xstrdup("band must have 'burst_size' "
690                                    "when 'burst' flag is set");
691                 }
692             } else {
693                 if (band->burst_size) {
694                     return xstrdup("band may have 'burst_size' only "
695                                    "when 'burst' flag is set");
696                 }
697             }
698         }
699     } else {
700         mm->meter.n_bands = 0;
701         mm->meter.bands = NULL;
702     }
703
704     return NULL;
705 }
706
707 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
708  * page) into 'mm' for sending the specified meter_mod 'command' to a switch.
709  *
710  * Returns NULL if successful, otherwise a malloc()'d string describing the
711  * error.  The caller is responsible for freeing the returned string. */
712 char * OVS_WARN_UNUSED_RESULT
713 parse_ofp_meter_mod_str(struct ofputil_meter_mod *mm, const char *str_,
714                         int command, enum ofputil_protocol *usable_protocols)
715 {
716     struct ofpbuf bands;
717     char *string;
718     char *error;
719
720     ofpbuf_init(&bands, 64);
721     string = xstrdup(str_);
722
723     error = parse_ofp_meter_mod_str__(mm, string, &bands, command,
724                                       usable_protocols);
725
726     free(string);
727     ofpbuf_uninit(&bands);
728
729     return error;
730 }
731
732 static char * OVS_WARN_UNUSED_RESULT
733 parse_flow_monitor_request__(struct ofputil_flow_monitor_request *fmr,
734                              const char *str_, char *string,
735                              enum ofputil_protocol *usable_protocols)
736 {
737     static atomic_count id = ATOMIC_COUNT_INIT(0);
738     char *save_ptr = NULL;
739     char *name;
740
741     fmr->id = atomic_count_inc(&id);
742
743     fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY
744                   | NXFMF_OWN | NXFMF_ACTIONS);
745     fmr->out_port = OFPP_NONE;
746     fmr->table_id = 0xff;
747     match_init_catchall(&fmr->match);
748
749     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
750          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
751         const struct protocol *p;
752
753         if (!strcmp(name, "!initial")) {
754             fmr->flags &= ~NXFMF_INITIAL;
755         } else if (!strcmp(name, "!add")) {
756             fmr->flags &= ~NXFMF_ADD;
757         } else if (!strcmp(name, "!delete")) {
758             fmr->flags &= ~NXFMF_DELETE;
759         } else if (!strcmp(name, "!modify")) {
760             fmr->flags &= ~NXFMF_MODIFY;
761         } else if (!strcmp(name, "!actions")) {
762             fmr->flags &= ~NXFMF_ACTIONS;
763         } else if (!strcmp(name, "!own")) {
764             fmr->flags &= ~NXFMF_OWN;
765         } else if (parse_protocol(name, &p)) {
766             match_set_dl_type(&fmr->match, htons(p->dl_type));
767             if (p->nw_proto) {
768                 match_set_nw_proto(&fmr->match, p->nw_proto);
769             }
770         } else {
771             char *value;
772
773             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
774             if (!value) {
775                 return xasprintf("%s: field %s missing value", str_, name);
776             }
777
778             if (!strcmp(name, "table")) {
779                 char *error = str_to_u8(value, "table", &fmr->table_id);
780                 if (error) {
781                     return error;
782                 }
783             } else if (!strcmp(name, "out_port")) {
784                 fmr->out_port = u16_to_ofp(atoi(value));
785             } else if (mf_from_name(name)) {
786                 char *error;
787
788                 error = parse_field(mf_from_name(name), value, &fmr->match,
789                                     usable_protocols);
790                 if (error) {
791                     return error;
792                 }
793             } else {
794                 return xasprintf("%s: unknown keyword %s", str_, name);
795             }
796         }
797     }
798     return NULL;
799 }
800
801 /* Convert 'str_' (as described in the documentation for the "monitor" command
802  * in the ovs-ofctl man page) into 'fmr'.
803  *
804  * Returns NULL if successful, otherwise a malloc()'d string describing the
805  * error.  The caller is responsible for freeing the returned string. */
806 char * OVS_WARN_UNUSED_RESULT
807 parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
808                            const char *str_,
809                            enum ofputil_protocol *usable_protocols)
810 {
811     char *string = xstrdup(str_);
812     char *error = parse_flow_monitor_request__(fmr, str_, string,
813                                                usable_protocols);
814     free(string);
815     return error;
816 }
817
818 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
819  * (one of OFPFC_*) into 'fm'.
820  *
821  * Returns NULL if successful, otherwise a malloc()'d string describing the
822  * error.  The caller is responsible for freeing the returned string. */
823 char * OVS_WARN_UNUSED_RESULT
824 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
825                        uint16_t command,
826                        enum ofputil_protocol *usable_protocols)
827 {
828     char *error = parse_ofp_str(fm, command, string, usable_protocols);
829     if (!error) {
830         /* Normalize a copy of the match.  This ensures that non-normalized
831          * flows get logged but doesn't affect what gets sent to the switch, so
832          * that the switch can do whatever it likes with the flow. */
833         struct match match_copy = fm->match;
834         ofputil_normalize_match(&match_copy);
835     }
836
837     return error;
838 }
839
840 /* Convert 'table_id' and 'flow_miss_handling' (as described for the
841  * "mod-table" command in the ovs-ofctl man page) into 'tm' for sending the
842  * specified table_mod 'command' to a switch.
843  *
844  * Returns NULL if successful, otherwise a malloc()'d string describing the
845  * error.  The caller is responsible for freeing the returned string. */
846 char * OVS_WARN_UNUSED_RESULT
847 parse_ofp_table_mod(struct ofputil_table_mod *tm, const char *table_id,
848                     const char *flow_miss_handling,
849                     enum ofputil_protocol *usable_protocols)
850 {
851     /* Table mod requires at least OF 1.1. */
852     *usable_protocols = OFPUTIL_P_OF11_UP;
853
854     if (!strcasecmp(table_id, "all")) {
855         tm->table_id = OFPTT_ALL;
856     } else {
857         char *error = str_to_u8(table_id, "table_id", &tm->table_id);
858         if (error) {
859             return error;
860         }
861     }
862
863     if (strcmp(flow_miss_handling, "controller") == 0) {
864         tm->miss_config = OFPUTIL_TABLE_MISS_CONTROLLER;
865     } else if (strcmp(flow_miss_handling, "continue") == 0) {
866         tm->miss_config = OFPUTIL_TABLE_MISS_CONTINUE;
867     } else if (strcmp(flow_miss_handling, "drop") == 0) {
868         tm->miss_config = OFPUTIL_TABLE_MISS_DROP;
869     } else {
870         return xasprintf("invalid flow_miss_handling %s", flow_miss_handling);
871     }
872
873     if (tm->table_id == 0xfe
874         && tm->miss_config == OFPUTIL_TABLE_MISS_CONTINUE) {
875         return xstrdup("last table's flow miss handling can not be continue");
876     }
877
878     return NULL;
879 }
880
881
882 /* Opens file 'file_name' and reads each line as a flow_mod of the specified
883  * type (one of OFPFC_*).  Stores each flow_mod in '*fm', an array allocated
884  * on the caller's behalf, and the number of flow_mods in '*n_fms'.
885  *
886  * Returns NULL if successful, otherwise a malloc()'d string describing the
887  * error.  The caller is responsible for freeing the returned string. */
888 char * OVS_WARN_UNUSED_RESULT
889 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
890                         struct ofputil_flow_mod **fms, size_t *n_fms,
891                         enum ofputil_protocol *usable_protocols)
892 {
893     size_t allocated_fms;
894     int line_number;
895     FILE *stream;
896     struct ds s;
897
898     *usable_protocols = OFPUTIL_P_ANY;
899
900     *fms = NULL;
901     *n_fms = 0;
902
903     stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
904     if (stream == NULL) {
905         return xasprintf("%s: open failed (%s)",
906                          file_name, ovs_strerror(errno));
907     }
908
909     allocated_fms = *n_fms;
910     ds_init(&s);
911     line_number = 0;
912     while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
913         char *error;
914         enum ofputil_protocol usable;
915
916         if (*n_fms >= allocated_fms) {
917             *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
918         }
919         error = parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command,
920                                        &usable);
921         if (error) {
922             size_t i;
923
924             for (i = 0; i < *n_fms; i++) {
925                 free(CONST_CAST(struct ofpact *, (*fms)[i].ofpacts));
926             }
927             free(*fms);
928             *fms = NULL;
929             *n_fms = 0;
930
931             ds_destroy(&s);
932             if (stream != stdin) {
933                 fclose(stream);
934             }
935
936             return xasprintf("%s:%d: %s", file_name, line_number, error);
937         }
938         *usable_protocols &= usable; /* Each line can narrow the set. */
939         *n_fms += 1;
940     }
941
942     ds_destroy(&s);
943     if (stream != stdin) {
944         fclose(stream);
945     }
946     return NULL;
947 }
948
949 char * OVS_WARN_UNUSED_RESULT
950 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
951                                  bool aggregate, const char *string,
952                                  enum ofputil_protocol *usable_protocols)
953 {
954     struct ofputil_flow_mod fm;
955     char *error;
956
957     error = parse_ofp_str(&fm, -1, string, usable_protocols);
958     if (error) {
959         return error;
960     }
961
962     /* Special table ID support not required for stats requests. */
963     if (*usable_protocols & OFPUTIL_P_OF10_STD_TID) {
964         *usable_protocols |= OFPUTIL_P_OF10_STD;
965     }
966     if (*usable_protocols & OFPUTIL_P_OF10_NXM_TID) {
967         *usable_protocols |= OFPUTIL_P_OF10_NXM;
968     }
969
970     fsr->aggregate = aggregate;
971     fsr->cookie = fm.cookie;
972     fsr->cookie_mask = fm.cookie_mask;
973     fsr->match = fm.match;
974     fsr->out_port = fm.out_port;
975     fsr->out_group = fm.out_group;
976     fsr->table_id = fm.table_id;
977     return NULL;
978 }
979
980 /* Parses a specification of a flow from 's' into 'flow'.  's' must take the
981  * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
982  * mf_field.  Fields must be specified in a natural order for satisfying
983  * prerequisites. If 'mask' is specified, fills the mask field for each of the
984  * field specified in flow. If the map, 'names_portno' is specfied, converts
985  * the in_port name into port no while setting the 'flow'.
986  *
987  * Returns NULL on success, otherwise a malloc()'d string that explains the
988  * problem. */
989 char *
990 parse_ofp_exact_flow(struct flow *flow, struct flow *mask, const char *s,
991                      const struct simap *portno_names)
992 {
993     char *pos, *key, *value_s;
994     char *error = NULL;
995     char *copy;
996
997     memset(flow, 0, sizeof *flow);
998     if (mask) {
999         memset(mask, 0, sizeof *mask);
1000     }
1001
1002     pos = copy = xstrdup(s);
1003     while (ofputil_parse_key_value(&pos, &key, &value_s)) {
1004         const struct protocol *p;
1005         if (parse_protocol(key, &p)) {
1006             if (flow->dl_type) {
1007                 error = xasprintf("%s: Ethernet type set multiple times", s);
1008                 goto exit;
1009             }
1010             flow->dl_type = htons(p->dl_type);
1011             if (mask) {
1012                 mask->dl_type = OVS_BE16_MAX;
1013             }
1014
1015             if (p->nw_proto) {
1016                 if (flow->nw_proto) {
1017                     error = xasprintf("%s: network protocol set "
1018                                       "multiple times", s);
1019                     goto exit;
1020                 }
1021                 flow->nw_proto = p->nw_proto;
1022                 if (mask) {
1023                     mask->nw_proto = UINT8_MAX;
1024                 }
1025             }
1026         } else {
1027             const struct mf_field *mf;
1028             union mf_value value;
1029             char *field_error;
1030
1031             mf = mf_from_name(key);
1032             if (!mf) {
1033                 error = xasprintf("%s: unknown field %s", s, key);
1034                 goto exit;
1035             }
1036
1037             if (!mf_are_prereqs_ok(mf, flow)) {
1038                 error = xasprintf("%s: prerequisites not met for setting %s",
1039                                   s, key);
1040                 goto exit;
1041             }
1042
1043             if (!mf_is_zero(mf, flow)) {
1044                 error = xasprintf("%s: field %s set multiple times", s, key);
1045                 goto exit;
1046             }
1047
1048             if (!strcmp(key, "in_port")
1049                 && portno_names
1050                 && simap_contains(portno_names, value_s)) {
1051                 flow->in_port.ofp_port = u16_to_ofp(
1052                     simap_get(portno_names, value_s));
1053                 if (mask) {
1054                     mask->in_port.ofp_port = u16_to_ofp(ntohs(OVS_BE16_MAX));
1055                 }
1056             } else {
1057                 field_error = mf_parse_value(mf, value_s, &value);
1058                 if (field_error) {
1059                     error = xasprintf("%s: bad value for %s (%s)",
1060                                       s, key, field_error);
1061                     free(field_error);
1062                     goto exit;
1063                 }
1064
1065                 mf_set_flow_value(mf, &value, flow);
1066                 if (mask) {
1067                     mf_mask_field(mf, mask);
1068                 }
1069             }
1070         }
1071     }
1072
1073     if (!flow->in_port.ofp_port) {
1074         flow->in_port.ofp_port = OFPP_NONE;
1075     }
1076
1077 exit:
1078     free(copy);
1079
1080     if (error) {
1081         memset(flow, 0, sizeof *flow);
1082         if (mask) {
1083             memset(mask, 0, sizeof *mask);
1084         }
1085     }
1086     return error;
1087 }
1088
1089 static char * OVS_WARN_UNUSED_RESULT
1090 parse_bucket_str(struct ofputil_bucket *bucket, char *str_,
1091                   enum ofputil_protocol *usable_protocols)
1092 {
1093     char *pos, *key, *value;
1094     struct ofpbuf ofpacts;
1095     struct ds actions;
1096     char *error;
1097
1098     bucket->weight = 1;
1099     bucket->bucket_id = OFPG15_BUCKET_ALL;
1100     bucket->watch_port = OFPP_ANY;
1101     bucket->watch_group = OFPG11_ANY;
1102
1103     ds_init(&actions);
1104
1105     pos = str_;
1106     error = NULL;
1107     while (ofputil_parse_key_value(&pos, &key, &value)) {
1108         if (!strcasecmp(key, "weight")) {
1109             error = str_to_u16(value, "weight", &bucket->weight);
1110         } else if (!strcasecmp(key, "watch_port")) {
1111             if (!ofputil_port_from_string(value, &bucket->watch_port)
1112                 || (ofp_to_u16(bucket->watch_port) >= ofp_to_u16(OFPP_MAX)
1113                     && bucket->watch_port != OFPP_ANY)) {
1114                 error = xasprintf("%s: invalid watch_port", value);
1115             }
1116         } else if (!strcasecmp(key, "watch_group")) {
1117             error = str_to_u32(value, &bucket->watch_group);
1118             if (!error && bucket->watch_group > OFPG_MAX) {
1119                 error = xasprintf("invalid watch_group id %"PRIu32,
1120                                   bucket->watch_group);
1121             }
1122         } else if (!strcasecmp(key, "bucket_id")) {
1123             error = str_to_u32(value, &bucket->bucket_id);
1124             if (!error && bucket->bucket_id > OFPG15_BUCKET_MAX) {
1125                 error = xasprintf("invalid bucket_id id %"PRIu32,
1126                                   bucket->bucket_id);
1127             }
1128             *usable_protocols &= OFPUTIL_P_OF15_UP;
1129         } else if (!strcasecmp(key, "action") || !strcasecmp(key, "actions")) {
1130             ds_put_format(&actions, "%s,", value);
1131         } else {
1132             ds_put_format(&actions, "%s(%s),", key, value);
1133         }
1134
1135         if (error) {
1136             ds_destroy(&actions);
1137             return error;
1138         }
1139     }
1140
1141     if (!actions.length) {
1142         return xstrdup("bucket must specify actions");
1143     }
1144     ds_chomp(&actions, ',');
1145
1146     ofpbuf_init(&ofpacts, 0);
1147     error = ofpacts_parse_actions(ds_cstr(&actions), &ofpacts,
1148                                   usable_protocols);
1149     ds_destroy(&actions);
1150     if (error) {
1151         ofpbuf_uninit(&ofpacts);
1152         return error;
1153     }
1154     bucket->ofpacts = ofpacts.data;
1155     bucket->ofpacts_len = ofpacts.size;
1156
1157     return NULL;
1158 }
1159
1160 static char * OVS_WARN_UNUSED_RESULT
1161 parse_select_group_field(char *s, struct field_array *fa,
1162                          enum ofputil_protocol *usable_protocols)
1163 {
1164     char *save_ptr = NULL;
1165     char *name;
1166
1167     for (name = strtok_r(s, "=, \t\r\n", &save_ptr); name;
1168          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1169         const struct mf_field *mf = mf_from_name(name);
1170
1171         if (mf) {
1172             char *error;
1173             const char *value_str;
1174             union mf_value value;
1175
1176             if (bitmap_is_set(fa->used.bm, mf->id)) {
1177                 return xasprintf("%s: duplicate field", name);
1178             }
1179
1180             value_str = strtok_r(NULL, ", \t\r\n", &save_ptr);
1181             if (value_str) {
1182                 error = mf_parse_value(mf, value_str, &value);
1183                 if (error) {
1184                     return error;
1185                 }
1186
1187                 /* The mask cannot be all-zeros */
1188                 if (is_all_zeros(&value, mf->n_bytes)) {
1189                     return xasprintf("%s: values are wildcards here "
1190                                      "and must not be all-zeros", s);
1191                 }
1192
1193                 /* The values parsed are masks for fields used
1194                  * by the selection method */
1195                 if (!mf_is_mask_valid(mf, &value)) {
1196                     return xasprintf("%s: invalid mask for field %s",
1197                                      value_str, mf->name);
1198                 }
1199             } else {
1200                 memset(&value, 0xff, mf->n_bytes);
1201             }
1202
1203             field_array_set(mf->id, &value, fa);
1204
1205             if (is_all_ones(&value, mf->n_bytes)) {
1206                 *usable_protocols &= mf->usable_protocols_exact;
1207             } else if (mf->usable_protocols_bitwise == mf->usable_protocols_cidr
1208                        || ip_is_cidr(value.be32)) {
1209                 *usable_protocols &= mf->usable_protocols_cidr;
1210             } else {
1211                 *usable_protocols &= mf->usable_protocols_bitwise;
1212             }
1213         } else {
1214             return xasprintf("%s: unknown field %s", s, name);
1215         }
1216     }
1217
1218     return NULL;
1219 }
1220
1221 static char * OVS_WARN_UNUSED_RESULT
1222 parse_ofp_group_mod_str__(struct ofputil_group_mod *gm, uint16_t command,
1223                           char *string,
1224                           enum ofputil_protocol *usable_protocols)
1225 {
1226     enum {
1227         F_GROUP_TYPE            = 1 << 0,
1228         F_BUCKETS               = 1 << 1,
1229         F_COMMAND_BUCKET_ID     = 1 << 2,
1230         F_COMMAND_BUCKET_ID_ALL = 1 << 3,
1231     } fields;
1232     char *save_ptr = NULL;
1233     bool had_type = false;
1234     bool had_command_bucket_id = false;
1235     char *name;
1236     struct ofputil_bucket *bucket;
1237     char *error = NULL;
1238
1239     *usable_protocols = OFPUTIL_P_OF11_UP;
1240
1241     switch (command) {
1242     case OFPGC11_ADD:
1243         fields = F_GROUP_TYPE | F_BUCKETS;
1244         break;
1245
1246     case OFPGC11_DELETE:
1247         fields = 0;
1248         break;
1249
1250     case OFPGC11_MODIFY:
1251         fields = F_GROUP_TYPE | F_BUCKETS;
1252         break;
1253
1254     case OFPGC15_INSERT_BUCKET:
1255         fields = F_BUCKETS | F_COMMAND_BUCKET_ID;
1256         *usable_protocols &= OFPUTIL_P_OF15_UP;
1257         break;
1258
1259     case OFPGC15_REMOVE_BUCKET:
1260         fields = F_COMMAND_BUCKET_ID | F_COMMAND_BUCKET_ID_ALL;
1261         *usable_protocols &= OFPUTIL_P_OF15_UP;
1262         break;
1263
1264     default:
1265         OVS_NOT_REACHED();
1266     }
1267
1268     memset(gm, 0, sizeof *gm);
1269     gm->command = command;
1270     gm->group_id = OFPG_ANY;
1271     gm->command_bucket_id = OFPG15_BUCKET_ALL;
1272     list_init(&gm->buckets);
1273     if (command == OFPGC11_DELETE && string[0] == '\0') {
1274         gm->group_id = OFPG_ALL;
1275         return NULL;
1276     }
1277
1278     *usable_protocols = OFPUTIL_P_OF11_UP;
1279
1280     if (fields & F_BUCKETS) {
1281         char *bkt_str = strstr(string, "bucket=");
1282
1283         if (bkt_str) {
1284             *bkt_str = '\0';
1285         }
1286
1287         while (bkt_str) {
1288             char *next_bkt_str;
1289
1290             bkt_str = strchr(bkt_str + 1, '=');
1291             if (!bkt_str) {
1292                 error = xstrdup("must specify bucket content");
1293                 goto out;
1294             }
1295             bkt_str++;
1296
1297             next_bkt_str = strstr(bkt_str, "bucket=");
1298             if (next_bkt_str) {
1299                 *next_bkt_str = '\0';
1300             }
1301
1302             bucket = xzalloc(sizeof(struct ofputil_bucket));
1303             error = parse_bucket_str(bucket, bkt_str, usable_protocols);
1304             if (error) {
1305                 free(bucket);
1306                 goto out;
1307             }
1308             list_push_back(&gm->buckets, &bucket->list_node);
1309
1310             bkt_str = next_bkt_str;
1311         }
1312     }
1313
1314     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1315          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1316         char *value;
1317
1318         value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1319         if (!value) {
1320             error = xasprintf("field %s missing value", name);
1321             goto out;
1322         }
1323
1324         if (!strcmp(name, "command_bucket_id")) {
1325             if (!(fields & F_COMMAND_BUCKET_ID)) {
1326                 error = xstrdup("command bucket id is not needed");
1327                 goto out;
1328             }
1329             if (!strcmp(value, "all")) {
1330                 gm->command_bucket_id = OFPG15_BUCKET_ALL;
1331             } else if (!strcmp(value, "first")) {
1332                 gm->command_bucket_id = OFPG15_BUCKET_FIRST;
1333             } else if (!strcmp(value, "last")) {
1334                 gm->command_bucket_id = OFPG15_BUCKET_LAST;
1335             } else {
1336                 error = str_to_u32(value, &gm->command_bucket_id);
1337                 if (error) {
1338                     goto out;
1339                 }
1340                 if (gm->command_bucket_id > OFPG15_BUCKET_MAX
1341                     && (gm->command_bucket_id != OFPG15_BUCKET_FIRST
1342                         && gm->command_bucket_id != OFPG15_BUCKET_LAST
1343                         && gm->command_bucket_id != OFPG15_BUCKET_ALL)) {
1344                     error = xasprintf("invalid command bucket id %"PRIu32,
1345                                       gm->command_bucket_id);
1346                     goto out;
1347                 }
1348             }
1349             if (gm->command_bucket_id == OFPG15_BUCKET_ALL
1350                 && !(fields & F_COMMAND_BUCKET_ID_ALL)) {
1351                 error = xstrdup("command_bucket_id=all is not permitted");
1352                 goto out;
1353             }
1354             had_command_bucket_id = true;
1355         } else if (!strcmp(name, "group_id")) {
1356             if(!strcmp(value, "all")) {
1357                 gm->group_id = OFPG_ALL;
1358             } else {
1359                 char *error = str_to_u32(value, &gm->group_id);
1360                 if (error) {
1361                     goto out;
1362                 }
1363                 if (gm->group_id != OFPG_ALL && gm->group_id > OFPG_MAX) {
1364                     error = xasprintf("invalid group id %"PRIu32,
1365                                       gm->group_id);
1366                     goto out;
1367                 }
1368             }
1369         } else if (!strcmp(name, "type")){
1370             if (!(fields & F_GROUP_TYPE)) {
1371                 error = xstrdup("type is not needed");
1372                 goto out;
1373             }
1374             if (!strcmp(value, "all")) {
1375                 gm->type = OFPGT11_ALL;
1376             } else if (!strcmp(value, "select")) {
1377                 gm->type = OFPGT11_SELECT;
1378             } else if (!strcmp(value, "indirect")) {
1379                 gm->type = OFPGT11_INDIRECT;
1380             } else if (!strcmp(value, "ff") ||
1381                        !strcmp(value, "fast_failover")) {
1382                 gm->type = OFPGT11_FF;
1383             } else {
1384                 error = xasprintf("invalid group type %s", value);
1385                 goto out;
1386             }
1387             had_type = true;
1388         } else if (!strcmp(name, "bucket")) {
1389             error = xstrdup("bucket is not needed");
1390             goto out;
1391         } else if (!strcmp(name, "selection_method")) {
1392             if (!(fields & F_GROUP_TYPE)) {
1393                 error = xstrdup("selection method is not needed");
1394                 goto out;
1395             }
1396             if (strlen(value) >= NTR_MAX_SELECTION_METHOD_LEN) {
1397                 error = xasprintf("selection method is longer than %u"
1398                                   " bytes long",
1399                                   NTR_MAX_SELECTION_METHOD_LEN - 1);
1400                 goto out;
1401             }
1402             memset(gm->props.selection_method, '\0',
1403                    NTR_MAX_SELECTION_METHOD_LEN);
1404             strcpy(gm->props.selection_method, value);
1405             *usable_protocols &= OFPUTIL_P_OF15_UP;
1406         } else if (!strcmp(name, "selection_method_param")) {
1407             if (!(fields & F_GROUP_TYPE)) {
1408                 error = xstrdup("selection method param is not needed");
1409                 goto out;
1410             }
1411             error = str_to_u64(value, &gm->props.selection_method_param);
1412             *usable_protocols &= OFPUTIL_P_OF15_UP;
1413         } else if (!strcmp(name, "fields")) {
1414             if (!(fields & F_GROUP_TYPE)) {
1415                 error = xstrdup("fields are not needed");
1416                 goto out;
1417             }
1418             error = parse_select_group_field(value, &gm->props.fields,
1419                                              usable_protocols);
1420             if (error) {
1421                 goto out;
1422             }
1423             *usable_protocols &= OFPUTIL_P_OF15_UP;
1424         } else {
1425             error = xasprintf("unknown keyword %s", name);
1426             goto out;
1427         }
1428     }
1429     if (gm->group_id == OFPG_ANY) {
1430         error = xstrdup("must specify a group_id");
1431         goto out;
1432     }
1433     if (fields & F_GROUP_TYPE && !had_type) {
1434         error = xstrdup("must specify a type");
1435         goto out;
1436     }
1437
1438     if (fields & F_COMMAND_BUCKET_ID) {
1439         if (!(fields & F_COMMAND_BUCKET_ID_ALL || had_command_bucket_id)) {
1440             error = xstrdup("must specify a command bucket id");
1441             goto out;
1442         }
1443     } else if (had_command_bucket_id) {
1444         error = xstrdup("command bucket id is not needed");
1445         goto out;
1446     }
1447
1448     /* Validate buckets. */
1449     LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
1450         if (bucket->weight != 1 && gm->type != OFPGT11_SELECT) {
1451             error = xstrdup("Only select groups can have bucket weights.");
1452             goto out;
1453         }
1454     }
1455     if (gm->type == OFPGT11_INDIRECT && !list_is_short(&gm->buckets)) {
1456         error = xstrdup("Indirect groups can have at most one bucket.");
1457         goto out;
1458     }
1459
1460     return NULL;
1461  out:
1462     ofputil_bucket_list_destroy(&gm->buckets);
1463     return error;
1464 }
1465
1466 char * OVS_WARN_UNUSED_RESULT
1467 parse_ofp_group_mod_str(struct ofputil_group_mod *gm, uint16_t command,
1468                         const char *str_,
1469                         enum ofputil_protocol *usable_protocols)
1470 {
1471     char *string = xstrdup(str_);
1472     char *error = parse_ofp_group_mod_str__(gm, command, string,
1473                                             usable_protocols);
1474     free(string);
1475
1476     if (error) {
1477         ofputil_bucket_list_destroy(&gm->buckets);
1478     }
1479     return error;
1480 }
1481
1482 char * OVS_WARN_UNUSED_RESULT
1483 parse_ofp_group_mod_file(const char *file_name, uint16_t command,
1484                          struct ofputil_group_mod **gms, size_t *n_gms,
1485                          enum ofputil_protocol *usable_protocols)
1486 {
1487     size_t allocated_gms;
1488     int line_number;
1489     FILE *stream;
1490     struct ds s;
1491
1492     *gms = NULL;
1493     *n_gms = 0;
1494
1495     stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
1496     if (stream == NULL) {
1497         return xasprintf("%s: open failed (%s)",
1498                          file_name, ovs_strerror(errno));
1499     }
1500
1501     allocated_gms = *n_gms;
1502     ds_init(&s);
1503     line_number = 0;
1504     *usable_protocols = OFPUTIL_P_OF11_UP;
1505     while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
1506         enum ofputil_protocol usable;
1507         char *error;
1508
1509         if (*n_gms >= allocated_gms) {
1510             struct ofputil_group_mod *new_gms;
1511             size_t i;
1512
1513             new_gms = x2nrealloc(*gms, &allocated_gms, sizeof **gms);
1514             for (i = 0; i < *n_gms; i++) {
1515                 list_moved(&new_gms[i].buckets, &(*gms)[i].buckets);
1516             }
1517             *gms = new_gms;
1518         }
1519         error = parse_ofp_group_mod_str(&(*gms)[*n_gms], command, ds_cstr(&s),
1520                                         &usable);
1521         if (error) {
1522             size_t i;
1523
1524             for (i = 0; i < *n_gms; i++) {
1525                 ofputil_bucket_list_destroy(&(*gms)[i].buckets);
1526             }
1527             free(*gms);
1528             *gms = NULL;
1529             *n_gms = 0;
1530
1531             ds_destroy(&s);
1532             if (stream != stdin) {
1533                 fclose(stream);
1534             }
1535
1536             return xasprintf("%s:%d: %s", file_name, line_number, error);
1537         }
1538         *usable_protocols &= usable;
1539         *n_gms += 1;
1540     }
1541
1542     ds_destroy(&s);
1543     if (stream != stdin) {
1544         fclose(stream);
1545     }
1546     return NULL;
1547 }