ofp-util: Add OFPUTIL_ACTION_INVALID to enum ofputil_action_code.
[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_ACTION_INVALID:
327         NOT_REACHED();
328
329     case OFPUTIL_OFPAT10_OUTPUT:
330         parse_output(b, arg);
331         break;
332
333     case OFPUTIL_OFPAT10_SET_VLAN_VID:
334         oavv = ofputil_put_OFPAT10_SET_VLAN_VID(b);
335         oavv->vlan_vid = htons(str_to_u32(arg));
336         break;
337
338     case OFPUTIL_OFPAT10_SET_VLAN_PCP:
339         oavp = ofputil_put_OFPAT10_SET_VLAN_PCP(b);
340         oavp->vlan_pcp = str_to_u32(arg);
341         break;
342
343     case OFPUTIL_OFPAT10_STRIP_VLAN:
344         ofputil_put_OFPAT10_STRIP_VLAN(b);
345         break;
346
347     case OFPUTIL_OFPAT10_SET_DL_SRC:
348     case OFPUTIL_OFPAT10_SET_DL_DST:
349         oada = ofputil_put_action(code, b);
350         str_to_mac(arg, oada->dl_addr);
351         break;
352
353     case OFPUTIL_OFPAT10_SET_NW_SRC:
354     case OFPUTIL_OFPAT10_SET_NW_DST:
355         oana = ofputil_put_action(code, b);
356         str_to_ip(arg, &oana->nw_addr);
357         break;
358
359     case OFPUTIL_OFPAT10_SET_NW_TOS:
360         ofputil_put_OFPAT10_SET_NW_TOS(b)->nw_tos = str_to_u32(arg);
361         break;
362
363     case OFPUTIL_OFPAT10_SET_TP_SRC:
364     case OFPUTIL_OFPAT10_SET_TP_DST:
365         oata = ofputil_put_action(code, b);
366         oata->tp_port = htons(str_to_u32(arg));
367         break;
368
369     case OFPUTIL_OFPAT10_ENQUEUE:
370         parse_enqueue(b, arg);
371         break;
372
373     case OFPUTIL_NXAST_RESUBMIT:
374         parse_resubmit(b, arg);
375         break;
376
377     case OFPUTIL_NXAST_SET_TUNNEL:
378         parse_set_tunnel(b, arg);
379         break;
380
381     case OFPUTIL_NXAST_SET_QUEUE:
382         ofputil_put_NXAST_SET_QUEUE(b)->queue_id = htonl(str_to_u32(arg));
383         break;
384
385     case OFPUTIL_NXAST_POP_QUEUE:
386         ofputil_put_NXAST_POP_QUEUE(b);
387         break;
388
389     case OFPUTIL_NXAST_REG_MOVE:
390         nxm_parse_reg_move(ofputil_put_NXAST_REG_MOVE(b), arg);
391         break;
392
393     case OFPUTIL_NXAST_REG_LOAD:
394         nxm_parse_reg_load(ofputil_put_NXAST_REG_LOAD(b), arg);
395         break;
396
397     case OFPUTIL_NXAST_NOTE:
398         parse_note(b, arg);
399         break;
400
401     case OFPUTIL_NXAST_SET_TUNNEL64:
402         ofputil_put_NXAST_SET_TUNNEL64(b)->tun_id = htonll(str_to_u64(arg));
403         break;
404
405     case OFPUTIL_NXAST_MULTIPATH:
406         multipath_parse(ofputil_put_NXAST_MULTIPATH(b), arg);
407         break;
408
409     case OFPUTIL_NXAST_AUTOPATH:
410         autopath_parse(ofputil_put_NXAST_AUTOPATH(b), arg);
411         break;
412
413     case OFPUTIL_NXAST_BUNDLE:
414         bundle_parse(b, arg);
415         break;
416
417     case OFPUTIL_NXAST_BUNDLE_LOAD:
418         bundle_parse_load(b, arg);
419         break;
420
421     case OFPUTIL_NXAST_RESUBMIT_TABLE:
422     case OFPUTIL_NXAST_OUTPUT_REG:
423         NOT_REACHED();
424
425     case OFPUTIL_NXAST_LEARN:
426         learn_parse(b, arg, flow);
427         break;
428
429     case OFPUTIL_NXAST_EXIT:
430         ofputil_put_NXAST_EXIT(b);
431         break;
432
433     case OFPUTIL_NXAST_DEC_TTL:
434         ofputil_put_NXAST_DEC_TTL(b);
435         break;
436
437     case OFPUTIL_NXAST_FIN_TIMEOUT:
438         parse_fin_timeout(b, arg);
439         break;
440
441     case OFPUTIL_NXAST_CONTROLLER:
442         parse_controller(b, arg);
443         break;
444     }
445 }
446
447 static void
448 str_to_action(const struct flow *flow, char *str, struct ofpbuf *b)
449 {
450     char *pos, *act, *arg;
451     int n_actions;
452
453     pos = str;
454     n_actions = 0;
455     while (ofputil_parse_key_value(&pos, &act, &arg)) {
456         uint16_t port;
457         int code;
458
459         code = ofputil_action_code_from_name(act);
460         if (code >= 0) {
461             parse_named_action(code, flow, b, arg);
462         } else if (!strcasecmp(act, "drop")) {
463             /* A drop action in OpenFlow occurs by just not setting
464              * an action. */
465             if (n_actions) {
466                 ovs_fatal(0, "Drop actions must not be preceded by other "
467                           "actions");
468             } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
469                 ovs_fatal(0, "Drop actions must not be followed by other "
470                           "actions");
471             }
472             break;
473         } else if (ofputil_port_from_string(act, &port)) {
474             put_output_action(b, port);
475         } else {
476             ovs_fatal(0, "Unknown action: %s", act);
477         }
478         n_actions++;
479     }
480 }
481
482 struct protocol {
483     const char *name;
484     uint16_t dl_type;
485     uint8_t nw_proto;
486 };
487
488 static bool
489 parse_protocol(const char *name, const struct protocol **p_out)
490 {
491     static const struct protocol protocols[] = {
492         { "ip", ETH_TYPE_IP, 0 },
493         { "arp", ETH_TYPE_ARP, 0 },
494         { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
495         { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
496         { "udp", ETH_TYPE_IP, IPPROTO_UDP },
497         { "ipv6", ETH_TYPE_IPV6, 0 },
498         { "ip6", ETH_TYPE_IPV6, 0 },
499         { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
500         { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
501         { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
502     };
503     const struct protocol *p;
504
505     for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
506         if (!strcmp(p->name, name)) {
507             *p_out = p;
508             return true;
509         }
510     }
511     *p_out = NULL;
512     return false;
513 }
514
515 static void
516 ofp_fatal(const char *flow, bool verbose, const char *format, ...)
517 {
518     va_list args;
519
520     if (verbose) {
521         fprintf(stderr, "%s:\n", flow);
522     }
523
524     va_start(args, format);
525     ovs_fatal_valist(0, format, args);
526 }
527
528 static void
529 parse_field(const struct mf_field *mf, const char *s, struct cls_rule *rule)
530 {
531     union mf_value value, mask;
532     char *error;
533
534     error = mf_parse(mf, s, &value, &mask);
535     if (error) {
536         ovs_fatal(0, "%s", error);
537     }
538
539     mf_set(mf, &value, &mask, rule);
540 }
541
542 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
543  * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
544  * If 'actions' is specified, an action must be in 'string' and may be expanded
545  * or reallocated.
546  *
547  * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
548  * constant for 'command'.  To parse syntax for an OFPST_FLOW or
549  * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. */
550 void
551 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
552               bool verbose)
553 {
554     enum {
555         F_OUT_PORT = 1 << 0,
556         F_ACTIONS = 1 << 1,
557         F_TIMEOUT = 1 << 3,
558         F_PRIORITY = 1 << 4,
559         F_FLAGS = 1 << 5,
560     } fields;
561     char *string = xstrdup(str_);
562     char *save_ptr = NULL;
563     char *act_str = NULL;
564     char *name;
565
566     switch (command) {
567     case -1:
568         fields = F_OUT_PORT;
569         break;
570
571     case OFPFC_ADD:
572         fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
573         break;
574
575     case OFPFC_DELETE:
576         fields = F_OUT_PORT;
577         break;
578
579     case OFPFC_DELETE_STRICT:
580         fields = F_OUT_PORT | F_PRIORITY;
581         break;
582
583     case OFPFC_MODIFY:
584         fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
585         break;
586
587     case OFPFC_MODIFY_STRICT:
588         fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
589         break;
590
591     default:
592         NOT_REACHED();
593     }
594
595     cls_rule_init_catchall(&fm->cr, OFP_DEFAULT_PRIORITY);
596     fm->cookie = htonll(0);
597     fm->cookie_mask = htonll(0);
598     if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
599         /* For modify, by default, don't update the cookie. */
600         fm->new_cookie = htonll(UINT64_MAX);
601     } else{
602         fm->new_cookie = htonll(0);
603     }
604     fm->table_id = 0xff;
605     fm->command = command;
606     fm->idle_timeout = OFP_FLOW_PERMANENT;
607     fm->hard_timeout = OFP_FLOW_PERMANENT;
608     fm->buffer_id = UINT32_MAX;
609     fm->out_port = OFPP_NONE;
610     fm->flags = 0;
611     if (fields & F_ACTIONS) {
612         act_str = strstr(string, "action");
613         if (!act_str) {
614             ofp_fatal(str_, verbose, "must specify an action");
615         }
616         *act_str = '\0';
617
618         act_str = strchr(act_str + 1, '=');
619         if (!act_str) {
620             ofp_fatal(str_, verbose, "must specify an action");
621         }
622
623         act_str++;
624     }
625     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
626          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
627         const struct protocol *p;
628
629         if (parse_protocol(name, &p)) {
630             cls_rule_set_dl_type(&fm->cr, htons(p->dl_type));
631             if (p->nw_proto) {
632                 cls_rule_set_nw_proto(&fm->cr, p->nw_proto);
633             }
634         } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
635             fm->flags |= OFPFF_SEND_FLOW_REM;
636         } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
637             fm->flags |= OFPFF_CHECK_OVERLAP;
638         } else {
639             char *value;
640
641             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
642             if (!value) {
643                 ofp_fatal(str_, verbose, "field %s missing value", name);
644             }
645
646             if (!strcmp(name, "table")) {
647                 fm->table_id = str_to_table_id(value);
648             } else if (!strcmp(name, "out_port")) {
649                 fm->out_port = atoi(value);
650             } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
651                 fm->cr.priority = str_to_u16(value, name);
652             } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
653                 fm->idle_timeout = str_to_u16(value, name);
654             } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
655                 fm->hard_timeout = str_to_u16(value, name);
656             } else if (!strcmp(name, "cookie")) {
657                 char *mask = strchr(value, '/');
658
659                 if (mask) {
660                     /* A mask means we're searching for a cookie. */
661                     if (command == OFPFC_ADD) {
662                         ofp_fatal(str_, verbose, "flow additions cannot use "
663                                   "a cookie mask");
664                     }
665                     *mask = '\0';
666                     fm->cookie = htonll(str_to_u64(value));
667                     fm->cookie_mask = htonll(str_to_u64(mask+1));
668                 } else {
669                     /* No mask means that the cookie is being set. */
670                     if (command != OFPFC_ADD && command != OFPFC_MODIFY
671                             && command != OFPFC_MODIFY_STRICT) {
672                         ofp_fatal(str_, verbose, "cannot set cookie");
673                     }
674                     fm->new_cookie = htonll(str_to_u64(value));
675                 }
676             } else if (mf_from_name(name)) {
677                 parse_field(mf_from_name(name), value, &fm->cr);
678             } else if (!strcmp(name, "duration")
679                        || !strcmp(name, "n_packets")
680                        || !strcmp(name, "n_bytes")) {
681                 /* Ignore these, so that users can feed the output of
682                  * "ovs-ofctl dump-flows" back into commands that parse
683                  * flows. */
684             } else {
685                 ofp_fatal(str_, verbose, "unknown keyword %s", name);
686             }
687         }
688     }
689     if (!fm->cookie_mask && fm->new_cookie == htonll(UINT64_MAX)
690             && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
691         /* On modifies without a mask, we are supposed to add a flow if
692          * one does not exist.  If a cookie wasn't been specified, use a
693          * default of zero. */
694         fm->new_cookie = htonll(0);
695     }
696     if (fields & F_ACTIONS) {
697         struct ofpbuf actions;
698
699         ofpbuf_init(&actions, sizeof(union ofp_action));
700         str_to_action(&fm->cr.flow, act_str, &actions);
701         fm->actions = ofpbuf_steal_data(&actions);
702         fm->n_actions = actions.size / sizeof(union ofp_action);
703     } else {
704         fm->actions = NULL;
705         fm->n_actions = 0;
706     }
707
708     free(string);
709 }
710
711 /* Parses 's' as a set of OpenFlow actions and appends the actions to
712  * 'actions'.
713  *
714  * Prints an error on stderr and aborts the program if 's' syntax is
715  * invalid. */
716 void
717 parse_ofp_actions(const char *s_, struct ofpbuf *actions)
718 {
719     char *s = xstrdup(s_);
720     str_to_action(NULL, s, actions);
721     free(s);
722 }
723
724 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
725  * (one of OFPFC_*) into 'fm'. */
726 void
727 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
728                        uint16_t command, bool verbose)
729 {
730     struct cls_rule rule_copy;
731
732     parse_ofp_str(fm, command, string, verbose);
733
734     /* Normalize a copy of the rule.  This ensures that non-normalized flows
735      * get logged but doesn't affect what gets sent to the switch, so that the
736      * switch can do whatever it likes with the flow. */
737     rule_copy = fm->cr;
738     ofputil_normalize_rule(&rule_copy);
739 }
740
741 void
742 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
743                         struct ofputil_flow_mod **fms, size_t *n_fms)
744 {
745     size_t allocated_fms;
746     FILE *stream;
747     struct ds s;
748
749     stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
750     if (stream == NULL) {
751         ovs_fatal(errno, "%s: open", file_name);
752     }
753
754     allocated_fms = *n_fms;
755     ds_init(&s);
756     while (!ds_get_preprocessed_line(&s, stream)) {
757         if (*n_fms >= allocated_fms) {
758             *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
759         }
760         parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command, false);
761         *n_fms += 1;
762     }
763     ds_destroy(&s);
764
765     if (stream != stdin) {
766         fclose(stream);
767     }
768 }
769
770 void
771 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
772                                  bool aggregate, const char *string)
773 {
774     struct ofputil_flow_mod fm;
775
776     parse_ofp_str(&fm, -1, string, false);
777     fsr->aggregate = aggregate;
778     fsr->cookie = fm.cookie;
779     fsr->cookie_mask = fm.cookie_mask;
780     fsr->match = fm.cr;
781     fsr->out_port = fm.out_port;
782     fsr->table_id = fm.table_id;
783 }
784
785 /* Parses a specification of a flow from 's' into 'flow'.  's' must take the
786  * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
787  * mf_field.  Fields must be specified in a natural order for satisfying
788  * prerequisites.
789  *
790  * Returns NULL on success, otherwise a malloc()'d string that explains the
791  * problem. */
792 char *
793 parse_ofp_exact_flow(struct flow *flow, const char *s)
794 {
795     char *pos, *key, *value_s;
796     char *error = NULL;
797     char *copy;
798
799     memset(flow, 0, sizeof *flow);
800
801     pos = copy = xstrdup(s);
802     while (ofputil_parse_key_value(&pos, &key, &value_s)) {
803         const struct protocol *p;
804         if (parse_protocol(key, &p)) {
805             if (flow->dl_type) {
806                 error = xasprintf("%s: Ethernet type set multiple times", s);
807                 goto exit;
808             }
809             flow->dl_type = htons(p->dl_type);
810
811             if (p->nw_proto) {
812                 if (flow->nw_proto) {
813                     error = xasprintf("%s: network protocol set "
814                                       "multiple times", s);
815                     goto exit;
816                 }
817                 flow->nw_proto = p->nw_proto;
818             }
819         } else {
820             const struct mf_field *mf;
821             union mf_value value;
822             char *field_error;
823
824             mf = mf_from_name(key);
825             if (!mf) {
826                 error = xasprintf("%s: unknown field %s", s, key);
827                 goto exit;
828             }
829
830             if (!mf_are_prereqs_ok(mf, flow)) {
831                 error = xasprintf("%s: prerequisites not met for setting %s",
832                                   s, key);
833                 goto exit;
834             }
835
836             if (!mf_is_zero(mf, flow)) {
837                 error = xasprintf("%s: field %s set multiple times", s, key);
838                 goto exit;
839             }
840
841             field_error = mf_parse_value(mf, value_s, &value);
842             if (field_error) {
843                 error = xasprintf("%s: bad value for %s (%s)",
844                                   s, key, field_error);
845                 free(field_error);
846                 goto exit;
847             }
848
849             mf_set_flow_value(mf, &value, flow);
850         }
851     }
852
853 exit:
854     free(copy);
855
856     if (error) {
857         memset(flow, 0, sizeof *flow);
858     }
859     return error;
860 }