Introduce ofpacts, an abstraction of OpenFlow actions.
[cascardo/ovs.git] / lib / ofp-parse.c
1 /*
2  * Copyright (c) 2010, 2011, 2012 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 "autopath.h"
26 #include "bundle.h"
27 #include "byte-order.h"
28 #include "dynamic-string.h"
29 #include "learn.h"
30 #include "meta-flow.h"
31 #include "multipath.h"
32 #include "netdev.h"
33 #include "nx-match.h"
34 #include "ofp-actions.h"
35 #include "ofp-util.h"
36 #include "ofpbuf.h"
37 #include "openflow/openflow.h"
38 #include "packets.h"
39 #include "socket-util.h"
40 #include "vconn.h"
41 #include "vlog.h"
42
43 VLOG_DEFINE_THIS_MODULE(ofp_parse);
44
45 static void ofp_fatal(const char *flow, bool verbose, const char *format, ...)
46     NO_RETURN;
47
48 static uint8_t
49 str_to_table_id(const char *str)
50 {
51     int table_id;
52
53     if (!str_to_int(str, 10, &table_id) || table_id < 0 || table_id > 255) {
54         ovs_fatal(0, "invalid table \"%s\"", str);
55     }
56     return table_id;
57 }
58
59 static uint16_t
60 str_to_u16(const char *str, const char *name)
61 {
62     int value;
63
64     if (!str_to_int(str, 0, &value) || value < 0 || value > 65535) {
65         ovs_fatal(0, "invalid %s \"%s\"", name, str);
66     }
67     return value;
68 }
69
70 static uint32_t
71 str_to_u32(const char *str)
72 {
73     char *tail;
74     uint32_t value;
75
76     if (!str[0]) {
77         ovs_fatal(0, "missing required numeric argument");
78     }
79
80     errno = 0;
81     value = strtoul(str, &tail, 0);
82     if (errno == EINVAL || errno == ERANGE || *tail) {
83         ovs_fatal(0, "invalid numeric format %s", str);
84     }
85     return value;
86 }
87
88 static uint64_t
89 str_to_u64(const char *str)
90 {
91     char *tail;
92     uint64_t value;
93
94     if (!str[0]) {
95         ovs_fatal(0, "missing required numeric argument");
96     }
97
98     errno = 0;
99     value = strtoull(str, &tail, 0);
100     if (errno == EINVAL || errno == ERANGE || *tail) {
101         ovs_fatal(0, "invalid numeric format %s", str);
102     }
103     return value;
104 }
105
106 static void
107 str_to_mac(const char *str, uint8_t mac[6])
108 {
109     if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
110         != ETH_ADDR_SCAN_COUNT) {
111         ovs_fatal(0, "invalid mac address %s", str);
112     }
113 }
114
115 static void
116 str_to_ip(const char *str, ovs_be32 *ip)
117 {
118     struct in_addr in_addr;
119
120     if (lookup_ip(str, &in_addr)) {
121         ovs_fatal(0, "%s: could not convert to IP address", str);
122     }
123     *ip = in_addr.s_addr;
124 }
125
126 static void
127 parse_enqueue(char *arg, struct ofpbuf *ofpacts)
128 {
129     char *sp = NULL;
130     char *port = strtok_r(arg, ":q", &sp);
131     char *queue = strtok_r(NULL, "", &sp);
132     struct ofpact_enqueue *enqueue;
133
134     if (port == NULL || queue == NULL) {
135         ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
136     }
137
138     enqueue = ofpact_put_ENQUEUE(ofpacts);
139     enqueue->port = str_to_u32(port);
140     enqueue->queue = str_to_u32(queue);
141 }
142
143 static void
144 parse_output(char *arg, struct ofpbuf *ofpacts)
145 {
146     if (strchr(arg, '[')) {
147         struct ofpact_output_reg *output_reg;
148
149         output_reg = ofpact_put_OUTPUT_REG(ofpacts);
150         mf_parse_subfield(&output_reg->src, arg);
151         output_reg->max_len = UINT16_MAX;
152     } else {
153         struct ofpact_output *output;
154
155         output = ofpact_put_OUTPUT(ofpacts);
156         output->port = str_to_u32(arg);
157         output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
158     }
159 }
160
161 static void
162 parse_resubmit(char *arg, struct ofpbuf *ofpacts)
163 {
164     struct ofpact_resubmit *resubmit;
165     char *in_port_s, *table_s;
166
167     resubmit = ofpact_put_RESUBMIT(ofpacts);
168
169     in_port_s = strsep(&arg, ",");
170     if (in_port_s && in_port_s[0]) {
171         if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
172             resubmit->in_port = str_to_u32(in_port_s);
173         }
174     } else {
175         resubmit->in_port = OFPP_IN_PORT;
176     }
177
178     table_s = strsep(&arg, ",");
179     resubmit->table_id = table_s && table_s[0] ? str_to_u32(table_s) : 255;
180
181     if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
182         ovs_fatal(0, "at least one \"in_port\" or \"table\" must be specified "
183                   " on resubmit");
184     }
185 }
186
187 static void
188 parse_note(const char *arg, struct ofpbuf *ofpacts)
189 {
190     struct ofpact_note *note;
191
192     note = ofpact_put_NOTE(ofpacts);
193     while (*arg != '\0') {
194         uint8_t byte;
195         bool ok;
196
197         if (*arg == '.') {
198             arg++;
199         }
200         if (*arg == '\0') {
201             break;
202         }
203
204         byte = hexits_value(arg, 2, &ok);
205         if (!ok) {
206             ovs_fatal(0, "bad hex digit in `note' argument");
207         }
208         ofpbuf_put(ofpacts, &byte, 1);
209
210         note = ofpacts->l2;
211         note->length++;
212
213         arg += 2;
214     }
215     ofpact_update_len(ofpacts, &note->ofpact);
216 }
217
218 static void
219 parse_fin_timeout(struct ofpbuf *b, char *arg)
220 {
221     struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(b);
222     char *key, *value;
223
224     while (ofputil_parse_key_value(&arg, &key, &value)) {
225         if (!strcmp(key, "idle_timeout")) {
226             oft->fin_idle_timeout = str_to_u16(value, key);
227         } else if (!strcmp(key, "hard_timeout")) {
228             oft->fin_hard_timeout = str_to_u16(value, key);
229         } else {
230             ovs_fatal(0, "invalid key '%s' in 'fin_timeout' argument", key);
231         }
232     }
233 }
234
235 static void
236 parse_controller(struct ofpbuf *b, char *arg)
237 {
238     enum ofp_packet_in_reason reason = OFPR_ACTION;
239     uint16_t controller_id = 0;
240     uint16_t max_len = UINT16_MAX;
241
242     if (!arg[0]) {
243         /* Use defaults. */
244     } else if (strspn(arg, "0123456789") == strlen(arg)) {
245         max_len = str_to_u16(arg, "max_len");
246     } else {
247         char *name, *value;
248
249         while (ofputil_parse_key_value(&arg, &name, &value)) {
250             if (!strcmp(name, "reason")) {
251                 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
252                     ovs_fatal(0, "unknown reason \"%s\"", value);
253                 }
254             } else if (!strcmp(name, "max_len")) {
255                 max_len = str_to_u16(value, "max_len");
256             } else if (!strcmp(name, "id")) {
257                 controller_id = str_to_u16(value, "id");
258             } else {
259                 ovs_fatal(0, "unknown key \"%s\" parsing controller action",
260                           name);
261             }
262         }
263     }
264
265     if (reason == OFPR_ACTION && controller_id == 0) {
266         struct ofpact_output *output;
267
268         output = ofpact_put_OUTPUT(b);
269         output->port = OFPP_CONTROLLER;
270         output->max_len = max_len;
271     } else {
272         struct ofpact_controller *controller;
273
274         controller = ofpact_put_CONTROLLER(b);
275         controller->max_len = max_len;
276         controller->reason = reason;
277         controller->controller_id = controller_id;
278     }
279 }
280
281 static void
282 parse_named_action(enum ofputil_action_code code, const struct flow *flow,
283                    char *arg, struct ofpbuf *ofpacts)
284 {
285     struct ofpact_tunnel *tunnel;
286     uint16_t vid;
287     ovs_be32 ip;
288     uint8_t pcp;
289     uint8_t tos;
290
291     switch (code) {
292     case OFPUTIL_ACTION_INVALID:
293         NOT_REACHED();
294
295     case OFPUTIL_OFPAT10_OUTPUT:
296         parse_output(arg, ofpacts);
297         break;
298
299     case OFPUTIL_OFPAT10_SET_VLAN_VID:
300         vid = str_to_u32(arg);
301         if (vid & ~VLAN_VID_MASK) {
302             ovs_fatal(0, "%s: not a valid VLAN VID", arg);
303         }
304         ofpact_put_SET_VLAN_VID(ofpacts)->vlan_vid = vid;
305         break;
306
307     case OFPUTIL_OFPAT10_SET_VLAN_PCP:
308         pcp = str_to_u32(arg);
309         if (pcp & ~7) {
310             ovs_fatal(0, "%s: not a valid VLAN PCP", arg);
311         }
312         ofpact_put_SET_VLAN_PCP(ofpacts)->vlan_pcp = pcp;
313         break;
314
315     case OFPUTIL_OFPAT10_STRIP_VLAN:
316         ofpact_put_STRIP_VLAN(ofpacts);
317         break;
318
319     case OFPUTIL_OFPAT10_SET_DL_SRC:
320         str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac);
321         break;
322
323     case OFPUTIL_OFPAT10_SET_DL_DST:
324         str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac);
325         break;
326
327     case OFPUTIL_OFPAT10_SET_NW_SRC:
328         str_to_ip(arg, &ip);
329         ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4 = ip;
330         break;
331
332     case OFPUTIL_OFPAT10_SET_NW_DST:
333         str_to_ip(arg, &ip);
334         ofpact_put_SET_IPV4_DST(ofpacts)->ipv4 = ip;
335         break;
336
337     case OFPUTIL_OFPAT10_SET_NW_TOS:
338         tos = str_to_u32(arg);
339         if (tos & ~IP_DSCP_MASK) {
340             ovs_fatal(0, "%s: not a valid TOS", arg);
341         }
342         ofpact_put_SET_IPV4_DSCP(ofpacts)->dscp = tos;
343         break;
344
345     case OFPUTIL_OFPAT10_SET_TP_SRC:
346         ofpact_put_SET_L4_SRC_PORT(ofpacts)->port = str_to_u32(arg);
347         break;
348
349     case OFPUTIL_OFPAT10_SET_TP_DST:
350         ofpact_put_SET_L4_DST_PORT(ofpacts)->port = str_to_u32(arg);
351         break;
352
353     case OFPUTIL_OFPAT10_ENQUEUE:
354         parse_enqueue(arg, ofpacts);
355         break;
356
357     case OFPUTIL_NXAST_RESUBMIT:
358         parse_resubmit(arg, ofpacts);
359         break;
360
361     case OFPUTIL_NXAST_SET_TUNNEL:
362     case OFPUTIL_NXAST_SET_TUNNEL64:
363         tunnel = ofpact_put_SET_TUNNEL(ofpacts);
364         tunnel->ofpact.compat = code;
365         tunnel->tun_id = str_to_u64(arg);
366         break;
367
368     case OFPUTIL_NXAST_SET_QUEUE:
369         ofpact_put_SET_QUEUE(ofpacts)->queue_id = str_to_u32(arg);
370         break;
371
372     case OFPUTIL_NXAST_POP_QUEUE:
373         ofpact_put_POP_QUEUE(ofpacts);
374         break;
375
376     case OFPUTIL_NXAST_REG_MOVE:
377         nxm_parse_reg_move(ofpact_put_REG_MOVE(ofpacts), arg);
378         break;
379
380     case OFPUTIL_NXAST_REG_LOAD:
381         nxm_parse_reg_load(ofpact_put_REG_LOAD(ofpacts), arg);
382         break;
383
384     case OFPUTIL_NXAST_NOTE:
385         parse_note(arg, ofpacts);
386         break;
387
388     case OFPUTIL_NXAST_MULTIPATH:
389         multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
390         break;
391
392     case OFPUTIL_NXAST_AUTOPATH:
393         autopath_parse(ofpact_put_AUTOPATH(ofpacts), arg);
394         break;
395
396     case OFPUTIL_NXAST_BUNDLE:
397         bundle_parse(arg, ofpacts);
398         break;
399
400     case OFPUTIL_NXAST_BUNDLE_LOAD:
401         bundle_parse_load(arg, ofpacts);
402         break;
403
404     case OFPUTIL_NXAST_RESUBMIT_TABLE:
405     case OFPUTIL_NXAST_OUTPUT_REG:
406         NOT_REACHED();
407
408     case OFPUTIL_NXAST_LEARN:
409         learn_parse(arg, flow, ofpacts);
410         break;
411
412     case OFPUTIL_NXAST_EXIT:
413         ofpact_put_EXIT(ofpacts);
414         break;
415
416     case OFPUTIL_NXAST_DEC_TTL:
417         ofpact_put_DEC_TTL(ofpacts);
418         break;
419
420     case OFPUTIL_NXAST_FIN_TIMEOUT:
421         parse_fin_timeout(ofpacts, arg);
422         break;
423
424     case OFPUTIL_NXAST_CONTROLLER:
425         parse_controller(ofpacts, arg);
426         break;
427     }
428 }
429
430 static void
431 str_to_ofpacts(const struct flow *flow, char *str, struct ofpbuf *ofpacts)
432 {
433     char *pos, *act, *arg;
434     int n_actions;
435
436     pos = str;
437     n_actions = 0;
438     while (ofputil_parse_key_value(&pos, &act, &arg)) {
439         uint16_t port;
440         int code;
441
442         code = ofputil_action_code_from_name(act);
443         if (code >= 0) {
444             parse_named_action(code, flow, arg, ofpacts);
445         } else if (!strcasecmp(act, "drop")) {
446             if (n_actions) {
447                 ovs_fatal(0, "Drop actions must not be preceded by other "
448                           "actions");
449             } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
450                 ovs_fatal(0, "Drop actions must not be followed by other "
451                           "actions");
452             }
453             break;
454         } else if (ofputil_port_from_string(act, &port)) {
455             ofpact_put_OUTPUT(ofpacts)->port = port;
456         } else {
457             ovs_fatal(0, "Unknown action: %s", act);
458         }
459         n_actions++;
460     }
461     ofpact_pad(ofpacts);
462 }
463
464 struct protocol {
465     const char *name;
466     uint16_t dl_type;
467     uint8_t nw_proto;
468 };
469
470 static bool
471 parse_protocol(const char *name, const struct protocol **p_out)
472 {
473     static const struct protocol protocols[] = {
474         { "ip", ETH_TYPE_IP, 0 },
475         { "arp", ETH_TYPE_ARP, 0 },
476         { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
477         { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
478         { "udp", ETH_TYPE_IP, IPPROTO_UDP },
479         { "ipv6", ETH_TYPE_IPV6, 0 },
480         { "ip6", ETH_TYPE_IPV6, 0 },
481         { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
482         { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
483         { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
484     };
485     const struct protocol *p;
486
487     for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
488         if (!strcmp(p->name, name)) {
489             *p_out = p;
490             return true;
491         }
492     }
493     *p_out = NULL;
494     return false;
495 }
496
497 static void
498 ofp_fatal(const char *flow, bool verbose, const char *format, ...)
499 {
500     va_list args;
501
502     if (verbose) {
503         fprintf(stderr, "%s:\n", flow);
504     }
505
506     va_start(args, format);
507     ovs_fatal_valist(0, format, args);
508 }
509
510 static void
511 parse_field(const struct mf_field *mf, const char *s, struct cls_rule *rule)
512 {
513     union mf_value value, mask;
514     char *error;
515
516     error = mf_parse(mf, s, &value, &mask);
517     if (error) {
518         ovs_fatal(0, "%s", error);
519     }
520
521     mf_set(mf, &value, &mask, rule);
522 }
523
524 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
525  * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
526  * If 'actions' is specified, an action must be in 'string' and may be expanded
527  * or reallocated.
528  *
529  * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
530  * constant for 'command'.  To parse syntax for an OFPST_FLOW or
531  * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. */
532 void
533 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
534               bool verbose)
535 {
536     enum {
537         F_OUT_PORT = 1 << 0,
538         F_ACTIONS = 1 << 1,
539         F_TIMEOUT = 1 << 3,
540         F_PRIORITY = 1 << 4,
541         F_FLAGS = 1 << 5,
542     } fields;
543     char *string = xstrdup(str_);
544     char *save_ptr = NULL;
545     char *act_str = NULL;
546     char *name;
547
548     switch (command) {
549     case -1:
550         fields = F_OUT_PORT;
551         break;
552
553     case OFPFC_ADD:
554         fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
555         break;
556
557     case OFPFC_DELETE:
558         fields = F_OUT_PORT;
559         break;
560
561     case OFPFC_DELETE_STRICT:
562         fields = F_OUT_PORT | F_PRIORITY;
563         break;
564
565     case OFPFC_MODIFY:
566         fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
567         break;
568
569     case OFPFC_MODIFY_STRICT:
570         fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
571         break;
572
573     default:
574         NOT_REACHED();
575     }
576
577     cls_rule_init_catchall(&fm->cr, OFP_DEFAULT_PRIORITY);
578     fm->cookie = htonll(0);
579     fm->cookie_mask = htonll(0);
580     if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
581         /* For modify, by default, don't update the cookie. */
582         fm->new_cookie = htonll(UINT64_MAX);
583     } else{
584         fm->new_cookie = htonll(0);
585     }
586     fm->table_id = 0xff;
587     fm->command = command;
588     fm->idle_timeout = OFP_FLOW_PERMANENT;
589     fm->hard_timeout = OFP_FLOW_PERMANENT;
590     fm->buffer_id = UINT32_MAX;
591     fm->out_port = OFPP_NONE;
592     fm->flags = 0;
593     if (fields & F_ACTIONS) {
594         act_str = strstr(string, "action");
595         if (!act_str) {
596             ofp_fatal(str_, verbose, "must specify an action");
597         }
598         *act_str = '\0';
599
600         act_str = strchr(act_str + 1, '=');
601         if (!act_str) {
602             ofp_fatal(str_, verbose, "must specify an action");
603         }
604
605         act_str++;
606     }
607     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
608          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
609         const struct protocol *p;
610
611         if (parse_protocol(name, &p)) {
612             cls_rule_set_dl_type(&fm->cr, htons(p->dl_type));
613             if (p->nw_proto) {
614                 cls_rule_set_nw_proto(&fm->cr, p->nw_proto);
615             }
616         } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
617             fm->flags |= OFPFF_SEND_FLOW_REM;
618         } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
619             fm->flags |= OFPFF_CHECK_OVERLAP;
620         } else {
621             char *value;
622
623             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
624             if (!value) {
625                 ofp_fatal(str_, verbose, "field %s missing value", name);
626             }
627
628             if (!strcmp(name, "table")) {
629                 fm->table_id = str_to_table_id(value);
630             } else if (!strcmp(name, "out_port")) {
631                 fm->out_port = atoi(value);
632             } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
633                 fm->cr.priority = str_to_u16(value, name);
634             } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
635                 fm->idle_timeout = str_to_u16(value, name);
636             } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
637                 fm->hard_timeout = str_to_u16(value, name);
638             } else if (!strcmp(name, "cookie")) {
639                 char *mask = strchr(value, '/');
640
641                 if (mask) {
642                     /* A mask means we're searching for a cookie. */
643                     if (command == OFPFC_ADD) {
644                         ofp_fatal(str_, verbose, "flow additions cannot use "
645                                   "a cookie mask");
646                     }
647                     *mask = '\0';
648                     fm->cookie = htonll(str_to_u64(value));
649                     fm->cookie_mask = htonll(str_to_u64(mask+1));
650                 } else {
651                     /* No mask means that the cookie is being set. */
652                     if (command != OFPFC_ADD && command != OFPFC_MODIFY
653                             && command != OFPFC_MODIFY_STRICT) {
654                         ofp_fatal(str_, verbose, "cannot set cookie");
655                     }
656                     fm->new_cookie = htonll(str_to_u64(value));
657                 }
658             } else if (mf_from_name(name)) {
659                 parse_field(mf_from_name(name), value, &fm->cr);
660             } else if (!strcmp(name, "duration")
661                        || !strcmp(name, "n_packets")
662                        || !strcmp(name, "n_bytes")) {
663                 /* Ignore these, so that users can feed the output of
664                  * "ovs-ofctl dump-flows" back into commands that parse
665                  * flows. */
666             } else {
667                 ofp_fatal(str_, verbose, "unknown keyword %s", name);
668             }
669         }
670     }
671     if (!fm->cookie_mask && fm->new_cookie == htonll(UINT64_MAX)
672             && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
673         /* On modifies without a mask, we are supposed to add a flow if
674          * one does not exist.  If a cookie wasn't been specified, use a
675          * default of zero. */
676         fm->new_cookie = htonll(0);
677     }
678     if (fields & F_ACTIONS) {
679         struct ofpbuf ofpacts;
680
681         ofpbuf_init(&ofpacts, 32);
682         str_to_ofpacts(&fm->cr.flow, act_str, &ofpacts);
683         fm->ofpacts_len = ofpacts.size;
684         fm->ofpacts = ofpbuf_steal_data(&ofpacts);
685     } else {
686         fm->ofpacts_len = 0;
687         fm->ofpacts = NULL;
688     }
689
690     free(string);
691 }
692
693 /* Parses 's' as a set of OpenFlow actions and appends the actions to
694  * 'actions'.
695  *
696  * Prints an error on stderr and aborts the program if 's' syntax is
697  * invalid. */
698 void
699 parse_ofpacts(const char *s_, struct ofpbuf *ofpacts)
700 {
701     char *s = xstrdup(s_);
702     str_to_ofpacts(NULL, s, ofpacts);
703     free(s);
704 }
705
706 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
707  * (one of OFPFC_*) into 'fm'. */
708 void
709 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
710                        uint16_t command, bool verbose)
711 {
712     struct cls_rule rule_copy;
713
714     parse_ofp_str(fm, command, string, verbose);
715
716     /* Normalize a copy of the rule.  This ensures that non-normalized flows
717      * get logged but doesn't affect what gets sent to the switch, so that the
718      * switch can do whatever it likes with the flow. */
719     rule_copy = fm->cr;
720     ofputil_normalize_rule(&rule_copy);
721 }
722
723 void
724 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
725                         struct ofputil_flow_mod **fms, size_t *n_fms)
726 {
727     size_t allocated_fms;
728     FILE *stream;
729     struct ds s;
730
731     stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
732     if (stream == NULL) {
733         ovs_fatal(errno, "%s: open", file_name);
734     }
735
736     allocated_fms = *n_fms;
737     ds_init(&s);
738     while (!ds_get_preprocessed_line(&s, stream)) {
739         if (*n_fms >= allocated_fms) {
740             *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
741         }
742         parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command, false);
743         *n_fms += 1;
744     }
745     ds_destroy(&s);
746
747     if (stream != stdin) {
748         fclose(stream);
749     }
750 }
751
752 void
753 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
754                                  bool aggregate, const char *string)
755 {
756     struct ofputil_flow_mod fm;
757
758     parse_ofp_str(&fm, -1, string, false);
759     fsr->aggregate = aggregate;
760     fsr->cookie = fm.cookie;
761     fsr->cookie_mask = fm.cookie_mask;
762     fsr->match = fm.cr;
763     fsr->out_port = fm.out_port;
764     fsr->table_id = fm.table_id;
765 }
766
767 /* Parses a specification of a flow from 's' into 'flow'.  's' must take the
768  * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
769  * mf_field.  Fields must be specified in a natural order for satisfying
770  * prerequisites.
771  *
772  * Returns NULL on success, otherwise a malloc()'d string that explains the
773  * problem. */
774 char *
775 parse_ofp_exact_flow(struct flow *flow, const char *s)
776 {
777     char *pos, *key, *value_s;
778     char *error = NULL;
779     char *copy;
780
781     memset(flow, 0, sizeof *flow);
782
783     pos = copy = xstrdup(s);
784     while (ofputil_parse_key_value(&pos, &key, &value_s)) {
785         const struct protocol *p;
786         if (parse_protocol(key, &p)) {
787             if (flow->dl_type) {
788                 error = xasprintf("%s: Ethernet type set multiple times", s);
789                 goto exit;
790             }
791             flow->dl_type = htons(p->dl_type);
792
793             if (p->nw_proto) {
794                 if (flow->nw_proto) {
795                     error = xasprintf("%s: network protocol set "
796                                       "multiple times", s);
797                     goto exit;
798                 }
799                 flow->nw_proto = p->nw_proto;
800             }
801         } else {
802             const struct mf_field *mf;
803             union mf_value value;
804             char *field_error;
805
806             mf = mf_from_name(key);
807             if (!mf) {
808                 error = xasprintf("%s: unknown field %s", s, key);
809                 goto exit;
810             }
811
812             if (!mf_are_prereqs_ok(mf, flow)) {
813                 error = xasprintf("%s: prerequisites not met for setting %s",
814                                   s, key);
815                 goto exit;
816             }
817
818             if (!mf_is_zero(mf, flow)) {
819                 error = xasprintf("%s: field %s set multiple times", s, key);
820                 goto exit;
821             }
822
823             field_error = mf_parse_value(mf, value_s, &value);
824             if (field_error) {
825                 error = xasprintf("%s: bad value for %s (%s)",
826                                   s, key, field_error);
827                 free(field_error);
828                 goto exit;
829             }
830
831             mf_set_flow_value(mf, &value, flow);
832         }
833     }
834
835 exit:
836     free(copy);
837
838     if (error) {
839         memset(flow, 0, sizeof *flow);
840     }
841     return error;
842 }