lib/ofp-util: preparation for OF12 of ofp-util
[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 "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_dec_ttl(struct ofpbuf *b, char *arg)
283 {
284     struct ofpact_cnt_ids *ids;
285
286     ids = ofpact_put_DEC_TTL(b);
287
288     if (*arg == '\0') {
289         uint16_t id = 0;
290
291         ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL;
292         ofpbuf_put(b, &id, sizeof id);
293         ids = b->l2;
294         ids->n_controllers++;
295     } else {
296         char *cntr;
297
298         ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
299         for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
300              cntr = strtok_r(NULL, ", ", &arg)) {
301             uint16_t id = atoi(cntr);
302
303             ofpbuf_put(b, &id, sizeof id);
304             ids = b->l2;
305             ids->n_controllers++;
306         }
307         if (!ids->n_controllers) {
308             ovs_fatal(0, "dec_ttl_cnt_ids: expected at least one controller "
309                       "id.");
310         }
311
312     }
313     ofpact_update_len(b, &ids->ofpact);
314 }
315
316 static void
317 parse_named_action(enum ofputil_action_code code, const struct flow *flow,
318                    char *arg, struct ofpbuf *ofpacts)
319 {
320     struct ofpact_tunnel *tunnel;
321     uint16_t vid;
322     ovs_be32 ip;
323     uint8_t pcp;
324     uint8_t tos;
325
326     switch (code) {
327     case OFPUTIL_ACTION_INVALID:
328         NOT_REACHED();
329
330     case OFPUTIL_OFPAT10_OUTPUT:
331     case OFPUTIL_OFPAT11_OUTPUT:
332         parse_output(arg, ofpacts);
333         break;
334
335     case OFPUTIL_OFPAT10_SET_VLAN_VID:
336     case OFPUTIL_OFPAT11_SET_VLAN_VID:
337         vid = str_to_u32(arg);
338         if (vid & ~VLAN_VID_MASK) {
339             ovs_fatal(0, "%s: not a valid VLAN VID", arg);
340         }
341         ofpact_put_SET_VLAN_VID(ofpacts)->vlan_vid = vid;
342         break;
343
344     case OFPUTIL_OFPAT10_SET_VLAN_PCP:
345     case OFPUTIL_OFPAT11_SET_VLAN_PCP:
346         pcp = str_to_u32(arg);
347         if (pcp & ~7) {
348             ovs_fatal(0, "%s: not a valid VLAN PCP", arg);
349         }
350         ofpact_put_SET_VLAN_PCP(ofpacts)->vlan_pcp = pcp;
351         break;
352
353     case OFPUTIL_OFPAT12_SET_FIELD:
354         NOT_REACHED();  /* This will be implemented by later patch and
355                          * enabled using a non-NULL name in
356                          * OFPAT12_ACTION(OFPAT12_SET_FIELD, ...) */
357         break;
358
359     case OFPUTIL_OFPAT10_STRIP_VLAN:
360         ofpact_put_STRIP_VLAN(ofpacts);
361         break;
362
363     case OFPUTIL_OFPAT10_SET_DL_SRC:
364     case OFPUTIL_OFPAT11_SET_DL_SRC:
365         str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac);
366         break;
367
368     case OFPUTIL_OFPAT10_SET_DL_DST:
369     case OFPUTIL_OFPAT11_SET_DL_DST:
370         str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac);
371         break;
372
373     case OFPUTIL_OFPAT10_SET_NW_SRC:
374     case OFPUTIL_OFPAT11_SET_NW_SRC:
375         str_to_ip(arg, &ip);
376         ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4 = ip;
377         break;
378
379     case OFPUTIL_OFPAT10_SET_NW_DST:
380     case OFPUTIL_OFPAT11_SET_NW_DST:
381         str_to_ip(arg, &ip);
382         ofpact_put_SET_IPV4_DST(ofpacts)->ipv4 = ip;
383         break;
384
385     case OFPUTIL_OFPAT10_SET_NW_TOS:
386     case OFPUTIL_OFPAT11_SET_NW_TOS:
387         tos = str_to_u32(arg);
388         if (tos & ~IP_DSCP_MASK) {
389             ovs_fatal(0, "%s: not a valid TOS", arg);
390         }
391         ofpact_put_SET_IPV4_DSCP(ofpacts)->dscp = tos;
392         break;
393
394     case OFPUTIL_OFPAT10_SET_TP_SRC:
395     case OFPUTIL_OFPAT11_SET_TP_SRC:
396         ofpact_put_SET_L4_SRC_PORT(ofpacts)->port = str_to_u32(arg);
397         break;
398
399     case OFPUTIL_OFPAT10_SET_TP_DST:
400     case OFPUTIL_OFPAT11_SET_TP_DST:
401         ofpact_put_SET_L4_DST_PORT(ofpacts)->port = str_to_u32(arg);
402         break;
403
404     case OFPUTIL_OFPAT10_ENQUEUE:
405         parse_enqueue(arg, ofpacts);
406         break;
407
408     case OFPUTIL_NXAST_RESUBMIT:
409         parse_resubmit(arg, ofpacts);
410         break;
411
412     case OFPUTIL_NXAST_SET_TUNNEL:
413     case OFPUTIL_NXAST_SET_TUNNEL64:
414         tunnel = ofpact_put_SET_TUNNEL(ofpacts);
415         tunnel->ofpact.compat = code;
416         tunnel->tun_id = str_to_u64(arg);
417         break;
418
419     case OFPUTIL_NXAST_SET_QUEUE:
420         ofpact_put_SET_QUEUE(ofpacts)->queue_id = str_to_u32(arg);
421         break;
422
423     case OFPUTIL_NXAST_POP_QUEUE:
424         ofpact_put_POP_QUEUE(ofpacts);
425         break;
426
427     case OFPUTIL_NXAST_REG_MOVE:
428         nxm_parse_reg_move(ofpact_put_REG_MOVE(ofpacts), arg);
429         break;
430
431     case OFPUTIL_NXAST_REG_LOAD:
432         nxm_parse_reg_load(ofpact_put_REG_LOAD(ofpacts), arg);
433         break;
434
435     case OFPUTIL_NXAST_NOTE:
436         parse_note(arg, ofpacts);
437         break;
438
439     case OFPUTIL_NXAST_MULTIPATH:
440         multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
441         break;
442
443     case OFPUTIL_NXAST_AUTOPATH__DEPRECATED:
444         autopath_parse(ofpact_put_AUTOPATH(ofpacts), arg);
445         break;
446
447     case OFPUTIL_NXAST_BUNDLE:
448         bundle_parse(arg, ofpacts);
449         break;
450
451     case OFPUTIL_NXAST_BUNDLE_LOAD:
452         bundle_parse_load(arg, ofpacts);
453         break;
454
455     case OFPUTIL_NXAST_RESUBMIT_TABLE:
456     case OFPUTIL_NXAST_OUTPUT_REG:
457     case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
458         NOT_REACHED();
459
460     case OFPUTIL_NXAST_LEARN:
461         learn_parse(arg, flow, ofpacts);
462         break;
463
464     case OFPUTIL_NXAST_EXIT:
465         ofpact_put_EXIT(ofpacts);
466         break;
467
468     case OFPUTIL_NXAST_DEC_TTL:
469         parse_dec_ttl(ofpacts, arg);
470         break;
471
472     case OFPUTIL_NXAST_FIN_TIMEOUT:
473         parse_fin_timeout(ofpacts, arg);
474         break;
475
476     case OFPUTIL_NXAST_CONTROLLER:
477         parse_controller(ofpacts, arg);
478         break;
479     }
480 }
481
482 static void
483 str_to_ofpacts(const struct flow *flow, char *str, struct ofpbuf *ofpacts)
484 {
485     char *pos, *act, *arg;
486     int n_actions;
487
488     pos = str;
489     n_actions = 0;
490     while (ofputil_parse_key_value(&pos, &act, &arg)) {
491         uint16_t port;
492         int code;
493
494         code = ofputil_action_code_from_name(act);
495         if (code >= 0) {
496             parse_named_action(code, flow, arg, ofpacts);
497         } else if (!strcasecmp(act, "drop")) {
498             if (n_actions) {
499                 ovs_fatal(0, "Drop actions must not be preceded by other "
500                           "actions");
501             } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
502                 ovs_fatal(0, "Drop actions must not be followed by other "
503                           "actions");
504             }
505             break;
506         } else if (ofputil_port_from_string(act, &port)) {
507             ofpact_put_OUTPUT(ofpacts)->port = port;
508         } else {
509             ovs_fatal(0, "Unknown action: %s", act);
510         }
511         n_actions++;
512     }
513     ofpact_pad(ofpacts);
514 }
515
516 struct protocol {
517     const char *name;
518     uint16_t dl_type;
519     uint8_t nw_proto;
520 };
521
522 static bool
523 parse_protocol(const char *name, const struct protocol **p_out)
524 {
525     static const struct protocol protocols[] = {
526         { "ip", ETH_TYPE_IP, 0 },
527         { "arp", ETH_TYPE_ARP, 0 },
528         { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
529         { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
530         { "udp", ETH_TYPE_IP, IPPROTO_UDP },
531         { "ipv6", ETH_TYPE_IPV6, 0 },
532         { "ip6", ETH_TYPE_IPV6, 0 },
533         { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
534         { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
535         { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
536     };
537     const struct protocol *p;
538
539     for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
540         if (!strcmp(p->name, name)) {
541             *p_out = p;
542             return true;
543         }
544     }
545     *p_out = NULL;
546     return false;
547 }
548
549 static void
550 ofp_fatal(const char *flow, bool verbose, const char *format, ...)
551 {
552     va_list args;
553
554     if (verbose) {
555         fprintf(stderr, "%s:\n", flow);
556     }
557
558     va_start(args, format);
559     ovs_fatal_valist(0, format, args);
560 }
561
562 static void
563 parse_field(const struct mf_field *mf, const char *s, struct match *match)
564 {
565     union mf_value value, mask;
566     char *error;
567
568     error = mf_parse(mf, s, &value, &mask);
569     if (error) {
570         ovs_fatal(0, "%s", error);
571     }
572
573     mf_set(mf, &value, &mask, match);
574 }
575
576 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
577  * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
578  * If 'actions' is specified, an action must be in 'string' and may be expanded
579  * or reallocated.
580  *
581  * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
582  * constant for 'command'.  To parse syntax for an OFPST_FLOW or
583  * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. */
584 void
585 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
586               bool verbose)
587 {
588     enum {
589         F_OUT_PORT = 1 << 0,
590         F_ACTIONS = 1 << 1,
591         F_TIMEOUT = 1 << 3,
592         F_PRIORITY = 1 << 4,
593         F_FLAGS = 1 << 5,
594     } fields;
595     char *string = xstrdup(str_);
596     char *save_ptr = NULL;
597     char *act_str = NULL;
598     char *name;
599
600     switch (command) {
601     case -1:
602         fields = F_OUT_PORT;
603         break;
604
605     case OFPFC_ADD:
606         fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
607         break;
608
609     case OFPFC_DELETE:
610         fields = F_OUT_PORT;
611         break;
612
613     case OFPFC_DELETE_STRICT:
614         fields = F_OUT_PORT | F_PRIORITY;
615         break;
616
617     case OFPFC_MODIFY:
618         fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
619         break;
620
621     case OFPFC_MODIFY_STRICT:
622         fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
623         break;
624
625     default:
626         NOT_REACHED();
627     }
628
629     match_init_catchall(&fm->match);
630     fm->priority = OFP_DEFAULT_PRIORITY;
631     fm->cookie = htonll(0);
632     fm->cookie_mask = htonll(0);
633     if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
634         /* For modify, by default, don't update the cookie. */
635         fm->new_cookie = htonll(UINT64_MAX);
636     } else{
637         fm->new_cookie = htonll(0);
638     }
639     fm->table_id = 0xff;
640     fm->command = command;
641     fm->idle_timeout = OFP_FLOW_PERMANENT;
642     fm->hard_timeout = OFP_FLOW_PERMANENT;
643     fm->buffer_id = UINT32_MAX;
644     fm->out_port = OFPP_NONE;
645     fm->flags = 0;
646     if (fields & F_ACTIONS) {
647         act_str = strstr(string, "action");
648         if (!act_str) {
649             ofp_fatal(str_, verbose, "must specify an action");
650         }
651         *act_str = '\0';
652
653         act_str = strchr(act_str + 1, '=');
654         if (!act_str) {
655             ofp_fatal(str_, verbose, "must specify an action");
656         }
657
658         act_str++;
659     }
660     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
661          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
662         const struct protocol *p;
663
664         if (parse_protocol(name, &p)) {
665             match_set_dl_type(&fm->match, htons(p->dl_type));
666             if (p->nw_proto) {
667                 match_set_nw_proto(&fm->match, p->nw_proto);
668             }
669         } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
670             fm->flags |= OFPFF_SEND_FLOW_REM;
671         } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
672             fm->flags |= OFPFF_CHECK_OVERLAP;
673         } else {
674             char *value;
675
676             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
677             if (!value) {
678                 ofp_fatal(str_, verbose, "field %s missing value", name);
679             }
680
681             if (!strcmp(name, "table")) {
682                 fm->table_id = str_to_table_id(value);
683             } else if (!strcmp(name, "out_port")) {
684                 fm->out_port = atoi(value);
685             } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
686                 fm->priority = str_to_u16(value, name);
687             } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
688                 fm->idle_timeout = str_to_u16(value, name);
689             } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
690                 fm->hard_timeout = str_to_u16(value, name);
691             } else if (!strcmp(name, "cookie")) {
692                 char *mask = strchr(value, '/');
693
694                 if (mask) {
695                     /* A mask means we're searching for a cookie. */
696                     if (command == OFPFC_ADD) {
697                         ofp_fatal(str_, verbose, "flow additions cannot use "
698                                   "a cookie mask");
699                     }
700                     *mask = '\0';
701                     fm->cookie = htonll(str_to_u64(value));
702                     fm->cookie_mask = htonll(str_to_u64(mask+1));
703                 } else {
704                     /* No mask means that the cookie is being set. */
705                     if (command != OFPFC_ADD && command != OFPFC_MODIFY
706                             && command != OFPFC_MODIFY_STRICT) {
707                         ofp_fatal(str_, verbose, "cannot set cookie");
708                     }
709                     fm->new_cookie = htonll(str_to_u64(value));
710                 }
711             } else if (mf_from_name(name)) {
712                 parse_field(mf_from_name(name), value, &fm->match);
713             } else if (!strcmp(name, "duration")
714                        || !strcmp(name, "n_packets")
715                        || !strcmp(name, "n_bytes")) {
716                 /* Ignore these, so that users can feed the output of
717                  * "ovs-ofctl dump-flows" back into commands that parse
718                  * flows. */
719             } else {
720                 ofp_fatal(str_, verbose, "unknown keyword %s", name);
721             }
722         }
723     }
724     if (!fm->cookie_mask && fm->new_cookie == htonll(UINT64_MAX)
725             && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
726         /* On modifies without a mask, we are supposed to add a flow if
727          * one does not exist.  If a cookie wasn't been specified, use a
728          * default of zero. */
729         fm->new_cookie = htonll(0);
730     }
731     if (fields & F_ACTIONS) {
732         struct ofpbuf ofpacts;
733
734         ofpbuf_init(&ofpacts, 32);
735         str_to_ofpacts(&fm->match.flow, act_str, &ofpacts);
736         fm->ofpacts_len = ofpacts.size;
737         fm->ofpacts = ofpbuf_steal_data(&ofpacts);
738     } else {
739         fm->ofpacts_len = 0;
740         fm->ofpacts = NULL;
741     }
742
743     free(string);
744 }
745
746 /* Convert 'str_' (as described in the documentation for the "monitor" command
747  * in the ovs-ofctl man page) into 'fmr'. */
748 void
749 parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
750                            const char *str_)
751 {
752     static uint32_t id;
753
754     char *string = xstrdup(str_);
755     char *save_ptr = NULL;
756     char *name;
757
758     fmr->id = id++;
759     fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY
760                   | NXFMF_OWN | NXFMF_ACTIONS);
761     fmr->out_port = OFPP_NONE;
762     fmr->table_id = 0xff;
763     match_init_catchall(&fmr->match);
764
765     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
766          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
767         const struct protocol *p;
768
769         if (!strcmp(name, "!initial")) {
770             fmr->flags &= ~NXFMF_INITIAL;
771         } else if (!strcmp(name, "!add")) {
772             fmr->flags &= ~NXFMF_ADD;
773         } else if (!strcmp(name, "!delete")) {
774             fmr->flags &= ~NXFMF_DELETE;
775         } else if (!strcmp(name, "!modify")) {
776             fmr->flags &= ~NXFMF_MODIFY;
777         } else if (!strcmp(name, "!actions")) {
778             fmr->flags &= ~NXFMF_ACTIONS;
779         } else if (!strcmp(name, "!own")) {
780             fmr->flags &= ~NXFMF_OWN;
781         } else if (parse_protocol(name, &p)) {
782             match_set_dl_type(&fmr->match, htons(p->dl_type));
783             if (p->nw_proto) {
784                 match_set_nw_proto(&fmr->match, p->nw_proto);
785             }
786         } else {
787             char *value;
788
789             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
790             if (!value) {
791                 ovs_fatal(0, "%s: field %s missing value", str_, name);
792             }
793
794             if (!strcmp(name, "table")) {
795                 fmr->table_id = str_to_table_id(value);
796             } else if (!strcmp(name, "out_port")) {
797                 fmr->out_port = atoi(value);
798             } else if (mf_from_name(name)) {
799                 parse_field(mf_from_name(name), value, &fmr->match);
800             } else {
801                 ovs_fatal(0, "%s: unknown keyword %s", str_, name);
802             }
803         }
804     }
805     free(string);
806 }
807
808 /* Parses 's' as a set of OpenFlow actions and appends the actions to
809  * 'actions'.
810  *
811  * Prints an error on stderr and aborts the program if 's' syntax is
812  * invalid. */
813 void
814 parse_ofpacts(const char *s_, struct ofpbuf *ofpacts)
815 {
816     char *s = xstrdup(s_);
817     str_to_ofpacts(NULL, s, ofpacts);
818     free(s);
819 }
820
821 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
822  * (one of OFPFC_*) into 'fm'. */
823 void
824 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
825                        uint16_t command, bool verbose)
826 {
827     struct match match_copy;
828
829     parse_ofp_str(fm, command, string, verbose);
830
831     /* Normalize a copy of the match.  This ensures that non-normalized flows
832      * get logged but doesn't affect what gets sent to the switch, so that the
833      * switch can do whatever it likes with the flow. */
834     match_copy = fm->match;
835     ofputil_normalize_match(&match_copy);
836 }
837
838 void
839 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
840                         struct ofputil_flow_mod **fms, size_t *n_fms)
841 {
842     size_t allocated_fms;
843     FILE *stream;
844     struct ds s;
845
846     stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
847     if (stream == NULL) {
848         ovs_fatal(errno, "%s: open", file_name);
849     }
850
851     allocated_fms = *n_fms;
852     ds_init(&s);
853     while (!ds_get_preprocessed_line(&s, stream)) {
854         if (*n_fms >= allocated_fms) {
855             *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
856         }
857         parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command, false);
858         *n_fms += 1;
859     }
860     ds_destroy(&s);
861
862     if (stream != stdin) {
863         fclose(stream);
864     }
865 }
866
867 void
868 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
869                                  bool aggregate, const char *string)
870 {
871     struct ofputil_flow_mod fm;
872
873     parse_ofp_str(&fm, -1, string, false);
874     fsr->aggregate = aggregate;
875     fsr->cookie = fm.cookie;
876     fsr->cookie_mask = fm.cookie_mask;
877     fsr->match = fm.match;
878     fsr->out_port = fm.out_port;
879     fsr->table_id = fm.table_id;
880 }
881
882 /* Parses a specification of a flow from 's' into 'flow'.  's' must take the
883  * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
884  * mf_field.  Fields must be specified in a natural order for satisfying
885  * prerequisites.
886  *
887  * Returns NULL on success, otherwise a malloc()'d string that explains the
888  * problem. */
889 char *
890 parse_ofp_exact_flow(struct flow *flow, const char *s)
891 {
892     char *pos, *key, *value_s;
893     char *error = NULL;
894     char *copy;
895
896     memset(flow, 0, sizeof *flow);
897
898     pos = copy = xstrdup(s);
899     while (ofputil_parse_key_value(&pos, &key, &value_s)) {
900         const struct protocol *p;
901         if (parse_protocol(key, &p)) {
902             if (flow->dl_type) {
903                 error = xasprintf("%s: Ethernet type set multiple times", s);
904                 goto exit;
905             }
906             flow->dl_type = htons(p->dl_type);
907
908             if (p->nw_proto) {
909                 if (flow->nw_proto) {
910                     error = xasprintf("%s: network protocol set "
911                                       "multiple times", s);
912                     goto exit;
913                 }
914                 flow->nw_proto = p->nw_proto;
915             }
916         } else {
917             const struct mf_field *mf;
918             union mf_value value;
919             char *field_error;
920
921             mf = mf_from_name(key);
922             if (!mf) {
923                 error = xasprintf("%s: unknown field %s", s, key);
924                 goto exit;
925             }
926
927             if (!mf_are_prereqs_ok(mf, flow)) {
928                 error = xasprintf("%s: prerequisites not met for setting %s",
929                                   s, key);
930                 goto exit;
931             }
932
933             if (!mf_is_zero(mf, flow)) {
934                 error = xasprintf("%s: field %s set multiple times", s, key);
935                 goto exit;
936             }
937
938             field_error = mf_parse_value(mf, value_s, &value);
939             if (field_error) {
940                 error = xasprintf("%s: bad value for %s (%s)",
941                                   s, key, field_error);
942                 free(field_error);
943                 goto exit;
944             }
945
946             mf_set_flow_value(mf, &value, flow);
947         }
948     }
949
950 exit:
951     free(copy);
952
953     if (error) {
954         memset(flow, 0, sizeof *flow);
955     }
956     return error;
957 }