ovs-ofctl: Improve error message for meter=0.
[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                         || !mm->meter.meter_id) {
593                         return xasprintf("invalid value for %s", name);
594                     }
595                 }
596             } else {
597                 return xasprintf("unknown keyword %s", name);
598             }
599         }
600     }
601     if (fields & F_METER && !mm->meter.meter_id) {
602         return xstrdup("must specify 'meter'");
603     }
604     if (fields & F_FLAGS && !mm->meter.flags) {
605         return xstrdup("meter must specify either 'kbps' or 'pktps'");
606     }
607
608     if (fields & F_BANDS) {
609         uint16_t n_bands = 0;
610         struct ofputil_meter_band *band = NULL;
611         int i;
612
613         for (name = strtok_r(band_str, "=, \t\r\n", &save_ptr); name;
614              name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
615
616             char *value;
617
618             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
619             if (!value) {
620                 return xasprintf("field %s missing value", name);
621             }
622
623             if (!strcmp(name, "type")) {
624                 /* Start a new band */
625                 band = ofpbuf_put_zeros(bands, sizeof *band);
626                 n_bands++;
627
628                 if (!strcmp(value, "drop")) {
629                     band->type = OFPMBT13_DROP;
630                 } else if (!strcmp(value, "dscp_remark")) {
631                     band->type = OFPMBT13_DSCP_REMARK;
632                 } else {
633                     return xasprintf("field %s unknown value %s", name, value);
634                 }
635             } else if (!band || !band->type) {
636                 return xstrdup("band must start with the 'type' keyword");
637             } else if (!strcmp(name, "rate")) {
638                 char *error = str_to_u32(value, &band->rate);
639                 if (error) {
640                     return error;
641                 }
642             } else if (!strcmp(name, "burst_size")) {
643                 char *error = str_to_u32(value, &band->burst_size);
644                 if (error) {
645                     return error;
646                 }
647             } else if (!strcmp(name, "prec_level")) {
648                 char *error = str_to_u8(value, name, &band->prec_level);
649                 if (error) {
650                     return error;
651                 }
652             } else {
653                 return xasprintf("unknown keyword %s", name);
654             }
655         }
656         /* validate bands */
657         if (!n_bands) {
658             return xstrdup("meter must have bands");
659         }
660
661         mm->meter.n_bands = n_bands;
662         mm->meter.bands = ofpbuf_steal_data(bands);
663
664         for (i = 0; i < n_bands; ++i) {
665             band = &mm->meter.bands[i];
666
667             if (!band->type) {
668                 return xstrdup("band must have 'type'");
669             }
670             if (band->type == OFPMBT13_DSCP_REMARK) {
671                 if (!band->prec_level) {
672                     return xstrdup("'dscp_remark' band must have"
673                                    " 'prec_level'");
674                 }
675             } else {
676                 if (band->prec_level) {
677                     return xstrdup("Only 'dscp_remark' band may have"
678                                    " 'prec_level'");
679                 }
680             }
681             if (!band->rate) {
682                 return xstrdup("band must have 'rate'");
683             }
684             if (mm->meter.flags & OFPMF13_BURST) {
685                 if (!band->burst_size) {
686                     return xstrdup("band must have 'burst_size' "
687                                    "when 'burst' flag is set");
688                 }
689             } else {
690                 if (band->burst_size) {
691                     return xstrdup("band may have 'burst_size' only "
692                                    "when 'burst' flag is set");
693                 }
694             }
695         }
696     } else {
697         mm->meter.n_bands = 0;
698         mm->meter.bands = NULL;
699     }
700
701     return NULL;
702 }
703
704 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
705  * page) into 'mm' for sending the specified meter_mod 'command' to a switch.
706  *
707  * Returns NULL if successful, otherwise a malloc()'d string describing the
708  * error.  The caller is responsible for freeing the returned string. */
709 char * WARN_UNUSED_RESULT
710 parse_ofp_meter_mod_str(struct ofputil_meter_mod *mm, const char *str_,
711                         int command, enum ofputil_protocol *usable_protocols)
712 {
713     struct ofpbuf bands;
714     char *string;
715     char *error;
716
717     ofpbuf_init(&bands, 64);
718     string = xstrdup(str_);
719
720     error = parse_ofp_meter_mod_str__(mm, string, &bands, command,
721                                       usable_protocols);
722
723     free(string);
724     ofpbuf_uninit(&bands);
725
726     return error;
727 }
728
729 static char * WARN_UNUSED_RESULT
730 parse_flow_monitor_request__(struct ofputil_flow_monitor_request *fmr,
731                              const char *str_, char *string,
732                              enum ofputil_protocol *usable_protocols)
733 {
734     static atomic_count id = ATOMIC_COUNT_INIT(0);
735     char *save_ptr = NULL;
736     char *name;
737
738     fmr->id = atomic_count_inc(&id);
739
740     fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY
741                   | NXFMF_OWN | NXFMF_ACTIONS);
742     fmr->out_port = OFPP_NONE;
743     fmr->table_id = 0xff;
744     match_init_catchall(&fmr->match);
745
746     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
747          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
748         const struct protocol *p;
749
750         if (!strcmp(name, "!initial")) {
751             fmr->flags &= ~NXFMF_INITIAL;
752         } else if (!strcmp(name, "!add")) {
753             fmr->flags &= ~NXFMF_ADD;
754         } else if (!strcmp(name, "!delete")) {
755             fmr->flags &= ~NXFMF_DELETE;
756         } else if (!strcmp(name, "!modify")) {
757             fmr->flags &= ~NXFMF_MODIFY;
758         } else if (!strcmp(name, "!actions")) {
759             fmr->flags &= ~NXFMF_ACTIONS;
760         } else if (!strcmp(name, "!own")) {
761             fmr->flags &= ~NXFMF_OWN;
762         } else if (parse_protocol(name, &p)) {
763             match_set_dl_type(&fmr->match, htons(p->dl_type));
764             if (p->nw_proto) {
765                 match_set_nw_proto(&fmr->match, p->nw_proto);
766             }
767         } else {
768             char *value;
769
770             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
771             if (!value) {
772                 return xasprintf("%s: field %s missing value", str_, name);
773             }
774
775             if (!strcmp(name, "table")) {
776                 char *error = str_to_u8(value, "table", &fmr->table_id);
777                 if (error) {
778                     return error;
779                 }
780             } else if (!strcmp(name, "out_port")) {
781                 fmr->out_port = u16_to_ofp(atoi(value));
782             } else if (mf_from_name(name)) {
783                 char *error;
784
785                 error = parse_field(mf_from_name(name), value, &fmr->match,
786                                     usable_protocols);
787                 if (error) {
788                     return error;
789                 }
790             } else {
791                 return xasprintf("%s: unknown keyword %s", str_, name);
792             }
793         }
794     }
795     return NULL;
796 }
797
798 /* Convert 'str_' (as described in the documentation for the "monitor" command
799  * in the ovs-ofctl man page) into 'fmr'.
800  *
801  * Returns NULL if successful, otherwise a malloc()'d string describing the
802  * error.  The caller is responsible for freeing the returned string. */
803 char * WARN_UNUSED_RESULT
804 parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
805                            const char *str_,
806                            enum ofputil_protocol *usable_protocols)
807 {
808     char *string = xstrdup(str_);
809     char *error = parse_flow_monitor_request__(fmr, str_, string,
810                                                usable_protocols);
811     free(string);
812     return error;
813 }
814
815 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
816  * (one of OFPFC_*) into 'fm'.
817  *
818  * Returns NULL if successful, otherwise a malloc()'d string describing the
819  * error.  The caller is responsible for freeing the returned string. */
820 char * WARN_UNUSED_RESULT
821 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
822                        uint16_t command,
823                        enum ofputil_protocol *usable_protocols)
824 {
825     char *error = parse_ofp_str(fm, command, string, usable_protocols);
826     if (!error) {
827         /* Normalize a copy of the match.  This ensures that non-normalized
828          * flows get logged but doesn't affect what gets sent to the switch, so
829          * that the switch can do whatever it likes with the flow. */
830         struct match match_copy = fm->match;
831         ofputil_normalize_match(&match_copy);
832     }
833
834     return error;
835 }
836
837 /* Convert 'table_id' and 'flow_miss_handling' (as described for the
838  * "mod-table" command in the ovs-ofctl man page) into 'tm' for sending the
839  * specified table_mod 'command' to a switch.
840  *
841  * Returns NULL if successful, otherwise a malloc()'d string describing the
842  * error.  The caller is responsible for freeing the returned string. */
843 char * WARN_UNUSED_RESULT
844 parse_ofp_table_mod(struct ofputil_table_mod *tm, const char *table_id,
845                     const char *flow_miss_handling,
846                     enum ofputil_protocol *usable_protocols)
847 {
848     /* Table mod requires at least OF 1.1. */
849     *usable_protocols = OFPUTIL_P_OF11_UP;
850
851     if (!strcasecmp(table_id, "all")) {
852         tm->table_id = OFPTT_ALL;
853     } else {
854         char *error = str_to_u8(table_id, "table_id", &tm->table_id);
855         if (error) {
856             return error;
857         }
858     }
859
860     if (strcmp(flow_miss_handling, "controller") == 0) {
861         tm->miss_config = OFPUTIL_TABLE_MISS_CONTROLLER;
862     } else if (strcmp(flow_miss_handling, "continue") == 0) {
863         tm->miss_config = OFPUTIL_TABLE_MISS_CONTINUE;
864     } else if (strcmp(flow_miss_handling, "drop") == 0) {
865         tm->miss_config = OFPUTIL_TABLE_MISS_DROP;
866     } else {
867         return xasprintf("invalid flow_miss_handling %s", flow_miss_handling);
868     }
869
870     if (tm->table_id == 0xfe
871         && tm->miss_config == OFPUTIL_TABLE_MISS_CONTINUE) {
872         return xstrdup("last table's flow miss handling can not be continue");
873     }
874
875     return NULL;
876 }
877
878
879 /* Opens file 'file_name' and reads each line as a flow_mod of the specified
880  * type (one of OFPFC_*).  Stores each flow_mod in '*fm', an array allocated
881  * on the caller's behalf, and the number of flow_mods in '*n_fms'.
882  *
883  * Returns NULL if successful, otherwise a malloc()'d string describing the
884  * error.  The caller is responsible for freeing the returned string. */
885 char * WARN_UNUSED_RESULT
886 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
887                         struct ofputil_flow_mod **fms, size_t *n_fms,
888                         enum ofputil_protocol *usable_protocols)
889 {
890     size_t allocated_fms;
891     int line_number;
892     FILE *stream;
893     struct ds s;
894
895     *usable_protocols = OFPUTIL_P_ANY;
896
897     *fms = NULL;
898     *n_fms = 0;
899
900     stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
901     if (stream == NULL) {
902         return xasprintf("%s: open failed (%s)",
903                          file_name, ovs_strerror(errno));
904     }
905
906     allocated_fms = *n_fms;
907     ds_init(&s);
908     line_number = 0;
909     while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
910         char *error;
911         enum ofputil_protocol usable;
912
913         if (*n_fms >= allocated_fms) {
914             *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
915         }
916         error = parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command,
917                                        &usable);
918         if (error) {
919             size_t i;
920
921             for (i = 0; i < *n_fms; i++) {
922                 free(CONST_CAST(struct ofpact *, (*fms)[i].ofpacts));
923             }
924             free(*fms);
925             *fms = NULL;
926             *n_fms = 0;
927
928             ds_destroy(&s);
929             if (stream != stdin) {
930                 fclose(stream);
931             }
932
933             return xasprintf("%s:%d: %s", file_name, line_number, error);
934         }
935         *usable_protocols &= usable; /* Each line can narrow the set. */
936         *n_fms += 1;
937     }
938
939     ds_destroy(&s);
940     if (stream != stdin) {
941         fclose(stream);
942     }
943     return NULL;
944 }
945
946 char * WARN_UNUSED_RESULT
947 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
948                                  bool aggregate, const char *string,
949                                  enum ofputil_protocol *usable_protocols)
950 {
951     struct ofputil_flow_mod fm;
952     char *error;
953
954     error = parse_ofp_str(&fm, -1, string, usable_protocols);
955     if (error) {
956         return error;
957     }
958
959     /* Special table ID support not required for stats requests. */
960     if (*usable_protocols & OFPUTIL_P_OF10_STD_TID) {
961         *usable_protocols |= OFPUTIL_P_OF10_STD;
962     }
963     if (*usable_protocols & OFPUTIL_P_OF10_NXM_TID) {
964         *usable_protocols |= OFPUTIL_P_OF10_NXM;
965     }
966
967     fsr->aggregate = aggregate;
968     fsr->cookie = fm.cookie;
969     fsr->cookie_mask = fm.cookie_mask;
970     fsr->match = fm.match;
971     fsr->out_port = fm.out_port;
972     fsr->out_group = fm.out_group;
973     fsr->table_id = fm.table_id;
974     return NULL;
975 }
976
977 /* Parses a specification of a flow from 's' into 'flow'.  's' must take the
978  * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
979  * mf_field.  Fields must be specified in a natural order for satisfying
980  * prerequisites. If 'mask' is specified, fills the mask field for each of the
981  * field specified in flow. If the map, 'names_portno' is specfied, converts
982  * the in_port name into port no while setting the 'flow'.
983  *
984  * Returns NULL on success, otherwise a malloc()'d string that explains the
985  * problem. */
986 char *
987 parse_ofp_exact_flow(struct flow *flow, struct flow *mask, const char *s,
988                      const struct simap *portno_names)
989 {
990     char *pos, *key, *value_s;
991     char *error = NULL;
992     char *copy;
993
994     memset(flow, 0, sizeof *flow);
995     if (mask) {
996         memset(mask, 0, sizeof *mask);
997     }
998
999     pos = copy = xstrdup(s);
1000     while (ofputil_parse_key_value(&pos, &key, &value_s)) {
1001         const struct protocol *p;
1002         if (parse_protocol(key, &p)) {
1003             if (flow->dl_type) {
1004                 error = xasprintf("%s: Ethernet type set multiple times", s);
1005                 goto exit;
1006             }
1007             flow->dl_type = htons(p->dl_type);
1008             if (mask) {
1009                 mask->dl_type = OVS_BE16_MAX;
1010             }
1011
1012             if (p->nw_proto) {
1013                 if (flow->nw_proto) {
1014                     error = xasprintf("%s: network protocol set "
1015                                       "multiple times", s);
1016                     goto exit;
1017                 }
1018                 flow->nw_proto = p->nw_proto;
1019                 if (mask) {
1020                     mask->nw_proto = UINT8_MAX;
1021                 }
1022             }
1023         } else {
1024             const struct mf_field *mf;
1025             union mf_value value;
1026             char *field_error;
1027
1028             mf = mf_from_name(key);
1029             if (!mf) {
1030                 error = xasprintf("%s: unknown field %s", s, key);
1031                 goto exit;
1032             }
1033
1034             if (!mf_are_prereqs_ok(mf, flow)) {
1035                 error = xasprintf("%s: prerequisites not met for setting %s",
1036                                   s, key);
1037                 goto exit;
1038             }
1039
1040             if (!mf_is_zero(mf, flow)) {
1041                 error = xasprintf("%s: field %s set multiple times", s, key);
1042                 goto exit;
1043             }
1044
1045             if (!strcmp(key, "in_port")
1046                 && portno_names
1047                 && simap_contains(portno_names, value_s)) {
1048                 flow->in_port.ofp_port = u16_to_ofp(
1049                     simap_get(portno_names, value_s));
1050                 if (mask) {
1051                     mask->in_port.ofp_port = u16_to_ofp(ntohs(OVS_BE16_MAX));
1052                 }
1053             } else {
1054                 field_error = mf_parse_value(mf, value_s, &value);
1055                 if (field_error) {
1056                     error = xasprintf("%s: bad value for %s (%s)",
1057                                       s, key, field_error);
1058                     free(field_error);
1059                     goto exit;
1060                 }
1061
1062                 mf_set_flow_value(mf, &value, flow);
1063                 if (mask) {
1064                     mf_mask_field(mf, mask);
1065                 }
1066             }
1067         }
1068     }
1069
1070     if (!flow->in_port.ofp_port) {
1071         flow->in_port.ofp_port = OFPP_NONE;
1072     }
1073
1074 exit:
1075     free(copy);
1076
1077     if (error) {
1078         memset(flow, 0, sizeof *flow);
1079         if (mask) {
1080             memset(mask, 0, sizeof *mask);
1081         }
1082     }
1083     return error;
1084 }
1085
1086 static char * WARN_UNUSED_RESULT
1087 parse_bucket_str(struct ofputil_bucket *bucket, char *str_,
1088                   enum ofputil_protocol *usable_protocols)
1089 {
1090     char *pos, *key, *value;
1091     struct ofpbuf ofpacts;
1092     struct ds actions;
1093     char *error;
1094
1095     bucket->weight = 1;
1096     bucket->watch_port = OFPP_ANY;
1097     bucket->watch_group = OFPG11_ANY;
1098
1099     ds_init(&actions);
1100
1101     pos = str_;
1102     error = NULL;
1103     while (ofputil_parse_key_value(&pos, &key, &value)) {
1104         if (!strcasecmp(key, "weight")) {
1105             error = str_to_u16(value, "weight", &bucket->weight);
1106         } else if (!strcasecmp(key, "watch_port")) {
1107             if (!ofputil_port_from_string(value, &bucket->watch_port)
1108                 || (ofp_to_u16(bucket->watch_port) >= ofp_to_u16(OFPP_MAX)
1109                     && bucket->watch_port != OFPP_ANY)) {
1110                 error = xasprintf("%s: invalid watch_port", value);
1111             }
1112         } else if (!strcasecmp(key, "watch_group")) {
1113             error = str_to_u32(value, &bucket->watch_group);
1114             if (!error && bucket->watch_group > OFPG_MAX) {
1115                 error = xasprintf("invalid watch_group id %"PRIu32,
1116                                   bucket->watch_group);
1117             }
1118         } else if (!strcasecmp(key, "action") || !strcasecmp(key, "actions")) {
1119             ds_put_format(&actions, "%s,", value);
1120         } else {
1121             ds_put_format(&actions, "%s(%s),", key, value);
1122         }
1123
1124         if (error) {
1125             ds_destroy(&actions);
1126             return error;
1127         }
1128     }
1129
1130     if (!actions.length) {
1131         return xstrdup("bucket must specify actions");
1132     }
1133     ds_chomp(&actions, ',');
1134
1135     ofpbuf_init(&ofpacts, 0);
1136     error = ofpacts_parse_actions(ds_cstr(&actions), &ofpacts,
1137                                   usable_protocols);
1138     ds_destroy(&actions);
1139     if (error) {
1140         ofpbuf_uninit(&ofpacts);
1141         return error;
1142     }
1143     bucket->ofpacts = ofpbuf_data(&ofpacts);
1144     bucket->ofpacts_len = ofpbuf_size(&ofpacts);
1145
1146     return NULL;
1147 }
1148
1149 static char * WARN_UNUSED_RESULT
1150 parse_ofp_group_mod_str__(struct ofputil_group_mod *gm, uint16_t command,
1151                           char *string,
1152                           enum ofputil_protocol *usable_protocols)
1153 {
1154     enum {
1155         F_GROUP_TYPE  = 1 << 0,
1156         F_BUCKETS     = 1 << 1,
1157     } fields;
1158     char *save_ptr = NULL;
1159     bool had_type = false;
1160     char *name;
1161     struct ofputil_bucket *bucket;
1162     char *error = NULL;
1163
1164     *usable_protocols = OFPUTIL_P_OF11_UP;
1165
1166     switch (command) {
1167     case OFPGC11_ADD:
1168         fields = F_GROUP_TYPE | F_BUCKETS;
1169         break;
1170
1171     case OFPGC11_DELETE:
1172         fields = 0;
1173         break;
1174
1175     case OFPGC11_MODIFY:
1176         fields = F_GROUP_TYPE | F_BUCKETS;
1177         break;
1178
1179     default:
1180         OVS_NOT_REACHED();
1181     }
1182
1183     memset(gm, 0, sizeof *gm);
1184     gm->command = command;
1185     gm->group_id = OFPG_ANY;
1186     list_init(&gm->buckets);
1187     if (command == OFPGC11_DELETE && string[0] == '\0') {
1188         gm->group_id = OFPG_ALL;
1189         return NULL;
1190     }
1191
1192     *usable_protocols = OFPUTIL_P_OF11_UP;
1193
1194     if (fields & F_BUCKETS) {
1195         char *bkt_str = strstr(string, "bucket");
1196
1197         if (bkt_str) {
1198             *bkt_str = '\0';
1199         }
1200
1201         while (bkt_str) {
1202             char *next_bkt_str;
1203
1204             bkt_str = strchr(bkt_str + 1, '=');
1205             if (!bkt_str) {
1206                 error = xstrdup("must specify bucket content");
1207                 goto out;
1208             }
1209             bkt_str++;
1210
1211             next_bkt_str = strstr(bkt_str, "bucket");
1212             if (next_bkt_str) {
1213                 *next_bkt_str = '\0';
1214             }
1215
1216             bucket = xzalloc(sizeof(struct ofputil_bucket));
1217             error = parse_bucket_str(bucket, bkt_str, usable_protocols);
1218             if (error) {
1219                 free(bucket);
1220                 goto out;
1221             }
1222             list_push_back(&gm->buckets, &bucket->list_node);
1223
1224             bkt_str = next_bkt_str;
1225         }
1226     }
1227
1228     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1229          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1230         char *value;
1231
1232         value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1233         if (!value) {
1234             error = xasprintf("field %s missing value", name);
1235             goto out;
1236         }
1237
1238         if (!strcmp(name, "group_id")) {
1239             if(!strcmp(value, "all")) {
1240                 gm->group_id = OFPG_ALL;
1241             } else {
1242                 char *error = str_to_u32(value, &gm->group_id);
1243                 if (error) {
1244                     goto out;
1245                 }
1246                 if (gm->group_id != OFPG_ALL && gm->group_id > OFPG_MAX) {
1247                     error = xasprintf("invalid group id %"PRIu32,
1248                                       gm->group_id);
1249                     goto out;
1250                 }
1251             }
1252         } else if (!strcmp(name, "type")){
1253             if (!(fields & F_GROUP_TYPE)) {
1254                 error = xstrdup("type is not needed");
1255                 goto out;
1256             }
1257             if (!strcmp(value, "all")) {
1258                 gm->type = OFPGT11_ALL;
1259             } else if (!strcmp(value, "select")) {
1260                 gm->type = OFPGT11_SELECT;
1261             } else if (!strcmp(value, "indirect")) {
1262                 gm->type = OFPGT11_INDIRECT;
1263             } else if (!strcmp(value, "ff") ||
1264                        !strcmp(value, "fast_failover")) {
1265                 gm->type = OFPGT11_FF;
1266             } else {
1267                 error = xasprintf("invalid group type %s", value);
1268                 goto out;
1269             }
1270             had_type = true;
1271         } else if (!strcmp(name, "bucket")) {
1272             error = xstrdup("bucket is not needed");
1273             goto out;
1274         } else {
1275             error = xasprintf("unknown keyword %s", name);
1276             goto out;
1277         }
1278     }
1279     if (gm->group_id == OFPG_ANY) {
1280         error = xstrdup("must specify a group_id");
1281         goto out;
1282     }
1283     if (fields & F_GROUP_TYPE && !had_type) {
1284         error = xstrdup("must specify a type");
1285         goto out;
1286     }
1287
1288     /* Validate buckets. */
1289     LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
1290         if (bucket->weight != 1 && gm->type != OFPGT11_SELECT) {
1291             error = xstrdup("Only select groups can have bucket weights.");
1292             goto out;
1293         }
1294     }
1295     if (gm->type == OFPGT11_INDIRECT && !list_is_short(&gm->buckets)) {
1296         error = xstrdup("Indirect groups can have at most one bucket.");
1297         goto out;
1298     }
1299
1300     return NULL;
1301  out:
1302     ofputil_bucket_list_destroy(&gm->buckets);
1303     return error;
1304 }
1305
1306 char * WARN_UNUSED_RESULT
1307 parse_ofp_group_mod_str(struct ofputil_group_mod *gm, uint16_t command,
1308                         const char *str_,
1309                         enum ofputil_protocol *usable_protocols)
1310 {
1311     char *string = xstrdup(str_);
1312     char *error = parse_ofp_group_mod_str__(gm, command, string,
1313                                             usable_protocols);
1314     free(string);
1315
1316     if (error) {
1317         ofputil_bucket_list_destroy(&gm->buckets);
1318     }
1319     return error;
1320 }
1321
1322 char * WARN_UNUSED_RESULT
1323 parse_ofp_group_mod_file(const char *file_name, uint16_t command,
1324                          struct ofputil_group_mod **gms, size_t *n_gms,
1325                          enum ofputil_protocol *usable_protocols)
1326 {
1327     size_t allocated_gms;
1328     int line_number;
1329     FILE *stream;
1330     struct ds s;
1331
1332     *gms = NULL;
1333     *n_gms = 0;
1334
1335     stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
1336     if (stream == NULL) {
1337         return xasprintf("%s: open failed (%s)",
1338                          file_name, ovs_strerror(errno));
1339     }
1340
1341     allocated_gms = *n_gms;
1342     ds_init(&s);
1343     line_number = 0;
1344     *usable_protocols = OFPUTIL_P_OF11_UP;
1345     while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
1346         enum ofputil_protocol usable;
1347         char *error;
1348
1349         if (*n_gms >= allocated_gms) {
1350             size_t i;
1351
1352             *gms = x2nrealloc(*gms, &allocated_gms, sizeof **gms);
1353             for (i = 0; i < *n_gms; i++) {
1354                 list_moved(&(*gms)[i].buckets);
1355             }
1356         }
1357         error = parse_ofp_group_mod_str(&(*gms)[*n_gms], command, ds_cstr(&s),
1358                                         &usable);
1359         if (error) {
1360             size_t i;
1361
1362             for (i = 0; i < *n_gms; i++) {
1363                 ofputil_bucket_list_destroy(&(*gms)[i].buckets);
1364             }
1365             free(*gms);
1366             *gms = NULL;
1367             *n_gms = 0;
1368
1369             ds_destroy(&s);
1370             if (stream != stdin) {
1371                 fclose(stream);
1372             }
1373
1374             return xasprintf("%s:%d: %s", file_name, line_number, error);
1375         }
1376         *usable_protocols &= usable;
1377         *n_gms += 1;
1378     }
1379
1380     ds_destroy(&s);
1381     if (stream != stdin) {
1382         fclose(stream);
1383     }
1384     return NULL;
1385 }