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