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