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