nicira-ext: Remove the autopath action.
[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 "bundle.h"
26 #include "byte-order.h"
27 #include "dynamic-string.h"
28 #include "learn.h"
29 #include "meta-flow.h"
30 #include "multipath.h"
31 #include "netdev.h"
32 #include "nx-match.h"
33 #include "ofp-actions.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 void
126 parse_enqueue(char *arg, struct ofpbuf *ofpacts)
127 {
128     char *sp = NULL;
129     char *port = strtok_r(arg, ":q", &sp);
130     char *queue = strtok_r(NULL, "", &sp);
131     struct ofpact_enqueue *enqueue;
132
133     if (port == NULL || queue == NULL) {
134         ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
135     }
136
137     enqueue = ofpact_put_ENQUEUE(ofpacts);
138     enqueue->port = str_to_u32(port);
139     enqueue->queue = str_to_u32(queue);
140 }
141
142 static void
143 parse_output(char *arg, struct ofpbuf *ofpacts)
144 {
145     if (strchr(arg, '[')) {
146         struct ofpact_output_reg *output_reg;
147
148         output_reg = ofpact_put_OUTPUT_REG(ofpacts);
149         mf_parse_subfield(&output_reg->src, arg);
150         output_reg->max_len = UINT16_MAX;
151     } else {
152         struct ofpact_output *output;
153
154         output = ofpact_put_OUTPUT(ofpacts);
155         output->port = str_to_u32(arg);
156         output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
157     }
158 }
159
160 static void
161 parse_resubmit(char *arg, struct ofpbuf *ofpacts)
162 {
163     struct ofpact_resubmit *resubmit;
164     char *in_port_s, *table_s;
165
166     resubmit = ofpact_put_RESUBMIT(ofpacts);
167
168     in_port_s = strsep(&arg, ",");
169     if (in_port_s && in_port_s[0]) {
170         if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
171             ovs_fatal(0, "%s: resubmit to unknown port", in_port_s);
172         }
173     } else {
174         resubmit->in_port = OFPP_IN_PORT;
175     }
176
177     table_s = strsep(&arg, ",");
178     resubmit->table_id = table_s && table_s[0] ? str_to_u32(table_s) : 255;
179
180     if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
181         ovs_fatal(0, "at least one \"in_port\" or \"table\" must be specified "
182                   " on resubmit");
183     }
184 }
185
186 static void
187 parse_note(const char *arg, struct ofpbuf *ofpacts)
188 {
189     struct ofpact_note *note;
190
191     note = ofpact_put_NOTE(ofpacts);
192     while (*arg != '\0') {
193         uint8_t byte;
194         bool ok;
195
196         if (*arg == '.') {
197             arg++;
198         }
199         if (*arg == '\0') {
200             break;
201         }
202
203         byte = hexits_value(arg, 2, &ok);
204         if (!ok) {
205             ovs_fatal(0, "bad hex digit in `note' argument");
206         }
207         ofpbuf_put(ofpacts, &byte, 1);
208
209         note = ofpacts->l2;
210         note->length++;
211
212         arg += 2;
213     }
214     ofpact_update_len(ofpacts, &note->ofpact);
215 }
216
217 static void
218 parse_fin_timeout(struct ofpbuf *b, char *arg)
219 {
220     struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(b);
221     char *key, *value;
222
223     while (ofputil_parse_key_value(&arg, &key, &value)) {
224         if (!strcmp(key, "idle_timeout")) {
225             oft->fin_idle_timeout = str_to_u16(value, key);
226         } else if (!strcmp(key, "hard_timeout")) {
227             oft->fin_hard_timeout = str_to_u16(value, key);
228         } else {
229             ovs_fatal(0, "invalid key '%s' in 'fin_timeout' argument", key);
230         }
231     }
232 }
233
234 static void
235 parse_controller(struct ofpbuf *b, char *arg)
236 {
237     enum ofp_packet_in_reason reason = OFPR_ACTION;
238     uint16_t controller_id = 0;
239     uint16_t max_len = UINT16_MAX;
240
241     if (!arg[0]) {
242         /* Use defaults. */
243     } else if (strspn(arg, "0123456789") == strlen(arg)) {
244         max_len = str_to_u16(arg, "max_len");
245     } else {
246         char *name, *value;
247
248         while (ofputil_parse_key_value(&arg, &name, &value)) {
249             if (!strcmp(name, "reason")) {
250                 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
251                     ovs_fatal(0, "unknown reason \"%s\"", value);
252                 }
253             } else if (!strcmp(name, "max_len")) {
254                 max_len = str_to_u16(value, "max_len");
255             } else if (!strcmp(name, "id")) {
256                 controller_id = str_to_u16(value, "id");
257             } else {
258                 ovs_fatal(0, "unknown key \"%s\" parsing controller action",
259                           name);
260             }
261         }
262     }
263
264     if (reason == OFPR_ACTION && controller_id == 0) {
265         struct ofpact_output *output;
266
267         output = ofpact_put_OUTPUT(b);
268         output->port = OFPP_CONTROLLER;
269         output->max_len = max_len;
270     } else {
271         struct ofpact_controller *controller;
272
273         controller = ofpact_put_CONTROLLER(b);
274         controller->max_len = max_len;
275         controller->reason = reason;
276         controller->controller_id = controller_id;
277     }
278 }
279
280 static void
281 parse_noargs_dec_ttl(struct ofpbuf *b)
282 {
283     struct ofpact_cnt_ids *ids;
284     uint16_t id = 0;
285
286     ids = ofpact_put_DEC_TTL(b);
287     ofpbuf_put(b, &id, sizeof id);
288     ids = b->l2;
289     ids->n_controllers++;
290     ofpact_update_len(b, &ids->ofpact);
291 }
292
293 static void
294 parse_dec_ttl(struct ofpbuf *b, char *arg)
295 {
296     if (*arg == '\0') {
297         parse_noargs_dec_ttl(b);
298     } else {
299         struct ofpact_cnt_ids *ids;
300         char *cntr;
301
302         ids = ofpact_put_DEC_TTL(b);
303         ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
304         for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
305              cntr = strtok_r(NULL, ", ", &arg)) {
306             uint16_t id = atoi(cntr);
307
308             ofpbuf_put(b, &id, sizeof id);
309             ids = b->l2;
310             ids->n_controllers++;
311         }
312         if (!ids->n_controllers) {
313             ovs_fatal(0, "dec_ttl_cnt_ids: expected at least one controller "
314                       "id.");
315         }
316         ofpact_update_len(b, &ids->ofpact);
317     }
318 }
319
320 static void
321 set_field_parse(const char *arg, struct ofpbuf *ofpacts)
322 {
323     char *orig = xstrdup(arg);
324     struct ofpact_reg_load *load = ofpact_put_REG_LOAD(ofpacts);
325     char *value;
326     char *delim;
327     char *key;
328     const struct mf_field *mf;
329     const char *error;
330     union mf_value mf_value;
331
332     value = orig;
333     delim = strstr(orig, "->");
334     if (!delim) {
335         ovs_fatal(0, "%s: missing `->'", orig);
336     }
337     if (strlen(delim) <= strlen("->")) {
338         ovs_fatal(0, "%s: missing field name following `->'", orig);
339     }
340
341     key = delim + strlen("->");
342     mf = mf_from_name(key);
343     if (!mf) {
344         ovs_fatal(0, "%s is not valid oxm field name", key);
345     }
346     if (!mf->writable) {
347         ovs_fatal(0, "%s is not allowed to set", key);
348     }
349
350     delim[0] = '\0';
351     error = mf_parse_value(mf, value, &mf_value);
352     if (error) {
353         ovs_fatal(0, "%s", error);
354     }
355     if (!mf_is_value_valid(mf, &mf_value)) {
356         ovs_fatal(0, "%s is not valid valid for field %s", value, key);
357     }
358     ofpact_set_field_init(load, mf, &mf_value);
359     free(orig);
360 }
361
362 static void
363 parse_metadata(struct ofpbuf *b, char *arg)
364 {
365     struct ofpact_metadata *om;
366     char *mask = strchr(arg, '/');
367
368     om = ofpact_put_WRITE_METADATA(b);
369
370     if (mask) {
371         *mask = '\0';
372         om->mask = htonll(str_to_u64(mask + 1));
373     } else {
374         om->mask = htonll(UINT64_MAX);
375     }
376
377     om->metadata = htonll(str_to_u64(arg));
378 }
379
380 static void
381 parse_named_action(enum ofputil_action_code code, const struct flow *flow,
382                    char *arg, struct ofpbuf *ofpacts)
383 {
384     struct ofpact_tunnel *tunnel;
385     uint16_t vid;
386     uint16_t ethertype;
387     ovs_be32 ip;
388     uint8_t pcp;
389     uint8_t tos;
390
391     switch (code) {
392     case OFPUTIL_ACTION_INVALID:
393         NOT_REACHED();
394
395     case OFPUTIL_OFPAT10_OUTPUT:
396     case OFPUTIL_OFPAT11_OUTPUT:
397         parse_output(arg, ofpacts);
398         break;
399
400     case OFPUTIL_OFPAT10_SET_VLAN_VID:
401     case OFPUTIL_OFPAT11_SET_VLAN_VID:
402         vid = str_to_u32(arg);
403         if (vid & ~VLAN_VID_MASK) {
404             ovs_fatal(0, "%s: not a valid VLAN VID", arg);
405         }
406         ofpact_put_SET_VLAN_VID(ofpacts)->vlan_vid = vid;
407         break;
408
409     case OFPUTIL_OFPAT10_SET_VLAN_PCP:
410     case OFPUTIL_OFPAT11_SET_VLAN_PCP:
411         pcp = str_to_u32(arg);
412         if (pcp & ~7) {
413             ovs_fatal(0, "%s: not a valid VLAN PCP", arg);
414         }
415         ofpact_put_SET_VLAN_PCP(ofpacts)->vlan_pcp = pcp;
416         break;
417
418     case OFPUTIL_OFPAT12_SET_FIELD:
419         set_field_parse(arg, ofpacts);
420         break;
421
422     case OFPUTIL_OFPAT10_STRIP_VLAN:
423     case OFPUTIL_OFPAT11_POP_VLAN:
424         ofpact_put_STRIP_VLAN(ofpacts);
425         break;
426
427     case OFPUTIL_OFPAT11_PUSH_VLAN:
428         ethertype = str_to_u16(arg, "ethertype");
429         if (ethertype != ETH_TYPE_VLAN_8021Q) {
430             /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
431             ovs_fatal(0, "%s: not a valid VLAN ethertype", arg);
432         }
433         ofpact_put_PUSH_VLAN(ofpacts);
434         break;
435
436     case OFPUTIL_OFPAT11_SET_QUEUE:
437         ofpact_put_SET_QUEUE(ofpacts)->queue_id = str_to_u32(arg);
438         break;
439
440
441     case OFPUTIL_OFPAT10_SET_DL_SRC:
442     case OFPUTIL_OFPAT11_SET_DL_SRC:
443         str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac);
444         break;
445
446     case OFPUTIL_OFPAT10_SET_DL_DST:
447     case OFPUTIL_OFPAT11_SET_DL_DST:
448         str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac);
449         break;
450
451     case OFPUTIL_OFPAT10_SET_NW_SRC:
452     case OFPUTIL_OFPAT11_SET_NW_SRC:
453         str_to_ip(arg, &ip);
454         ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4 = ip;
455         break;
456
457     case OFPUTIL_OFPAT10_SET_NW_DST:
458     case OFPUTIL_OFPAT11_SET_NW_DST:
459         str_to_ip(arg, &ip);
460         ofpact_put_SET_IPV4_DST(ofpacts)->ipv4 = ip;
461         break;
462
463     case OFPUTIL_OFPAT10_SET_NW_TOS:
464     case OFPUTIL_OFPAT11_SET_NW_TOS:
465         tos = str_to_u32(arg);
466         if (tos & ~IP_DSCP_MASK) {
467             ovs_fatal(0, "%s: not a valid TOS", arg);
468         }
469         ofpact_put_SET_IPV4_DSCP(ofpacts)->dscp = tos;
470         break;
471
472     case OFPUTIL_OFPAT11_DEC_NW_TTL:
473         NOT_REACHED();
474
475     case OFPUTIL_OFPAT10_SET_TP_SRC:
476     case OFPUTIL_OFPAT11_SET_TP_SRC:
477         ofpact_put_SET_L4_SRC_PORT(ofpacts)->port = str_to_u32(arg);
478         break;
479
480     case OFPUTIL_OFPAT10_SET_TP_DST:
481     case OFPUTIL_OFPAT11_SET_TP_DST:
482         ofpact_put_SET_L4_DST_PORT(ofpacts)->port = str_to_u32(arg);
483         break;
484
485     case OFPUTIL_OFPAT10_ENQUEUE:
486         parse_enqueue(arg, ofpacts);
487         break;
488
489     case OFPUTIL_NXAST_RESUBMIT:
490         parse_resubmit(arg, ofpacts);
491         break;
492
493     case OFPUTIL_NXAST_SET_TUNNEL:
494     case OFPUTIL_NXAST_SET_TUNNEL64:
495         tunnel = ofpact_put_SET_TUNNEL(ofpacts);
496         tunnel->ofpact.compat = code;
497         tunnel->tun_id = str_to_u64(arg);
498         break;
499
500     case OFPUTIL_NXAST_WRITE_METADATA:
501         parse_metadata(ofpacts, arg);
502         break;
503
504     case OFPUTIL_NXAST_SET_QUEUE:
505         ofpact_put_SET_QUEUE(ofpacts)->queue_id = str_to_u32(arg);
506         break;
507
508     case OFPUTIL_NXAST_POP_QUEUE:
509         ofpact_put_POP_QUEUE(ofpacts);
510         break;
511
512     case OFPUTIL_NXAST_REG_MOVE:
513         nxm_parse_reg_move(ofpact_put_REG_MOVE(ofpacts), arg);
514         break;
515
516     case OFPUTIL_NXAST_REG_LOAD:
517         nxm_parse_reg_load(ofpact_put_REG_LOAD(ofpacts), arg);
518         break;
519
520     case OFPUTIL_NXAST_NOTE:
521         parse_note(arg, ofpacts);
522         break;
523
524     case OFPUTIL_NXAST_MULTIPATH:
525         multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
526         break;
527
528     case OFPUTIL_NXAST_BUNDLE:
529         bundle_parse(arg, ofpacts);
530         break;
531
532     case OFPUTIL_NXAST_BUNDLE_LOAD:
533         bundle_parse_load(arg, ofpacts);
534         break;
535
536     case OFPUTIL_NXAST_RESUBMIT_TABLE:
537     case OFPUTIL_NXAST_OUTPUT_REG:
538     case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
539         NOT_REACHED();
540
541     case OFPUTIL_NXAST_LEARN:
542         learn_parse(arg, flow, ofpacts);
543         break;
544
545     case OFPUTIL_NXAST_EXIT:
546         ofpact_put_EXIT(ofpacts);
547         break;
548
549     case OFPUTIL_NXAST_DEC_TTL:
550         parse_dec_ttl(ofpacts, arg);
551         break;
552
553     case OFPUTIL_NXAST_FIN_TIMEOUT:
554         parse_fin_timeout(ofpacts, arg);
555         break;
556
557     case OFPUTIL_NXAST_CONTROLLER:
558         parse_controller(ofpacts, arg);
559         break;
560
561     case OFPUTIL_OFPAT11_PUSH_MPLS:
562     case OFPUTIL_NXAST_PUSH_MPLS:
563         ofpact_put_PUSH_MPLS(ofpacts)->ethertype =
564             htons(str_to_u16(arg, "push_mpls"));
565         break;
566
567     case OFPUTIL_OFPAT11_POP_MPLS:
568     case OFPUTIL_NXAST_POP_MPLS:
569         ofpact_put_POP_MPLS(ofpacts)->ethertype =
570             htons(str_to_u16(arg, "pop_mpls"));
571         break;
572     }
573 }
574
575 static bool
576 str_to_ofpact__(const struct flow *flow, char *pos, char *act, char *arg,
577                 struct ofpbuf *ofpacts, int n_actions)
578 {
579     int code = ofputil_action_code_from_name(act);
580     if (code >= 0) {
581         parse_named_action(code, flow, arg, ofpacts);
582     } else if (!strcasecmp(act, "drop")) {
583         if (n_actions) {
584             ovs_fatal(0, "Drop actions must not be preceded by other "
585                       "actions");
586         } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
587             ovs_fatal(0, "Drop actions must not be followed by other "
588                       "actions");
589         }
590         return false;
591     } else {
592         uint16_t port;
593         if (ofputil_port_from_string(act, &port)) {
594             ofpact_put_OUTPUT(ofpacts)->port = port;
595         } else {
596             ovs_fatal(0, "Unknown action: %s", act);
597         }
598     }
599
600     return true;
601 }
602
603 static void
604 str_to_ofpacts(const struct flow *flow, char *str, struct ofpbuf *ofpacts)
605 {
606     char *pos, *act, *arg;
607     enum ofperr error;
608     int n_actions;
609
610     pos = str;
611     n_actions = 0;
612     while (ofputil_parse_key_value(&pos, &act, &arg)) {
613         if (!str_to_ofpact__(flow, pos, act, arg, ofpacts, n_actions)) {
614             break;
615         }
616         n_actions++;
617     }
618
619     error = ofpacts_verify(ofpacts->data, ofpacts->size);
620     if (error) {
621         ovs_fatal(0, "Incorrect action ordering");
622     }
623
624     ofpact_pad(ofpacts);
625 }
626
627 static void
628 parse_named_instruction(enum ovs_instruction_type type,
629                         char *arg, struct ofpbuf *ofpacts)
630 {
631     enum ofperr error;
632
633     switch (type) {
634     case OVSINST_OFPIT11_APPLY_ACTIONS:
635         NOT_REACHED();  /* This case is handled by str_to_inst_ofpacts() */
636         break;
637
638     case OVSINST_OFPIT11_WRITE_ACTIONS:
639         /* XXX */
640         ovs_fatal(0, "instruction write-actions is not supported yet");
641         break;
642
643     case OVSINST_OFPIT11_CLEAR_ACTIONS:
644         ofpact_put_CLEAR_ACTIONS(ofpacts);
645         break;
646
647     case OVSINST_OFPIT11_WRITE_METADATA:
648         parse_metadata(ofpacts, arg);
649         break;
650
651     case OVSINST_OFPIT11_GOTO_TABLE: {
652         struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
653         char *table_s = strsep(&arg, ",");
654         if (!table_s || !table_s[0]) {
655             ovs_fatal(0, "instruction goto-table needs table id");
656         }
657         ogt->table_id = str_to_table_id(table_s);
658         break;
659     }
660     }
661
662     /* If write_metadata is specified as an action AND an instruction, ofpacts
663        could be invalid. */
664     error = ofpacts_verify(ofpacts->data, ofpacts->size);
665     if (error) {
666         ovs_fatal(0, "Incorrect instruction ordering");
667     }
668 }
669
670 static void
671 str_to_inst_ofpacts(const struct flow *flow, char *str, struct ofpbuf *ofpacts)
672 {
673     char *pos, *inst, *arg;
674     int type;
675     const char *prev_inst = NULL;
676     int prev_type = -1;
677     int n_actions = 0;
678
679     pos = str;
680     while (ofputil_parse_key_value(&pos, &inst, &arg)) {
681         type = ofpact_instruction_type_from_name(inst);
682         if (type < 0) {
683             if (!str_to_ofpact__(flow, pos, inst, arg, ofpacts, n_actions)) {
684                 break;
685             }
686
687             type = OVSINST_OFPIT11_APPLY_ACTIONS;
688             if (prev_type == type) {
689                 n_actions++;
690                 continue;
691             }
692         } else if (type == OVSINST_OFPIT11_APPLY_ACTIONS) {
693             ovs_fatal(0, "%s isn't supported. Just write actions then "
694                       "it is interpreted as apply_actions", inst);
695         } else {
696             parse_named_instruction(type, arg, ofpacts);
697         }
698
699         if (type == prev_type) {
700             ovs_fatal(0, "instruction can be specified at most once: %s",
701                       inst);
702         }
703         if (type <= prev_type) {
704             ovs_fatal(0, "Instruction %s must be specified before %s",
705                       inst, prev_inst);
706         }
707
708         prev_inst = inst;
709         prev_type = type;
710         n_actions++;
711     }
712     ofpact_pad(ofpacts);
713 }
714
715 struct protocol {
716     const char *name;
717     uint16_t dl_type;
718     uint8_t nw_proto;
719 };
720
721 static bool
722 parse_protocol(const char *name, const struct protocol **p_out)
723 {
724     static const struct protocol protocols[] = {
725         { "ip", ETH_TYPE_IP, 0 },
726         { "arp", ETH_TYPE_ARP, 0 },
727         { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
728         { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
729         { "udp", ETH_TYPE_IP, IPPROTO_UDP },
730         { "ipv6", ETH_TYPE_IPV6, 0 },
731         { "ip6", ETH_TYPE_IPV6, 0 },
732         { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
733         { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
734         { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
735         { "rarp", ETH_TYPE_RARP, 0},
736         { "mpls", ETH_TYPE_MPLS, 0 },
737         { "mplsm", ETH_TYPE_MPLS_MCAST, 0 },
738     };
739     const struct protocol *p;
740
741     for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
742         if (!strcmp(p->name, name)) {
743             *p_out = p;
744             return true;
745         }
746     }
747     *p_out = NULL;
748     return false;
749 }
750
751 static void
752 ofp_fatal(const char *flow, bool verbose, const char *format, ...)
753 {
754     va_list args;
755
756     if (verbose) {
757         fprintf(stderr, "%s:\n", flow);
758     }
759
760     va_start(args, format);
761     ovs_fatal_valist(0, format, args);
762 }
763
764 static void
765 parse_field(const struct mf_field *mf, const char *s, struct match *match)
766 {
767     union mf_value value, mask;
768     char *error;
769
770     error = mf_parse(mf, s, &value, &mask);
771     if (error) {
772         ovs_fatal(0, "%s", error);
773     }
774
775     mf_set(mf, &value, &mask, match);
776 }
777
778 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
779  * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
780  * If 'actions' is specified, an action must be in 'string' and may be expanded
781  * or reallocated.
782  *
783  * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
784  * constant for 'command'.  To parse syntax for an OFPST_FLOW or
785  * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. */
786 void
787 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
788               bool verbose)
789 {
790     enum {
791         F_OUT_PORT = 1 << 0,
792         F_ACTIONS = 1 << 1,
793         F_TIMEOUT = 1 << 3,
794         F_PRIORITY = 1 << 4,
795         F_FLAGS = 1 << 5,
796     } fields;
797     char *string = xstrdup(str_);
798     char *save_ptr = NULL;
799     char *act_str = NULL;
800     char *name;
801
802     switch (command) {
803     case -1:
804         fields = F_OUT_PORT;
805         break;
806
807     case OFPFC_ADD:
808         fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
809         break;
810
811     case OFPFC_DELETE:
812         fields = F_OUT_PORT;
813         break;
814
815     case OFPFC_DELETE_STRICT:
816         fields = F_OUT_PORT | F_PRIORITY;
817         break;
818
819     case OFPFC_MODIFY:
820         fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
821         break;
822
823     case OFPFC_MODIFY_STRICT:
824         fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
825         break;
826
827     default:
828         NOT_REACHED();
829     }
830
831     match_init_catchall(&fm->match);
832     fm->priority = OFP_DEFAULT_PRIORITY;
833     fm->cookie = htonll(0);
834     fm->cookie_mask = htonll(0);
835     if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
836         /* For modify, by default, don't update the cookie. */
837         fm->new_cookie = htonll(UINT64_MAX);
838     } else{
839         fm->new_cookie = htonll(0);
840     }
841     fm->table_id = 0xff;
842     fm->command = command;
843     fm->idle_timeout = OFP_FLOW_PERMANENT;
844     fm->hard_timeout = OFP_FLOW_PERMANENT;
845     fm->buffer_id = UINT32_MAX;
846     fm->out_port = OFPP_ANY;
847     fm->flags = 0;
848     if (fields & F_ACTIONS) {
849         act_str = strstr(string, "action");
850         if (!act_str) {
851             ofp_fatal(str_, verbose, "must specify an action");
852         }
853         *act_str = '\0';
854
855         act_str = strchr(act_str + 1, '=');
856         if (!act_str) {
857             ofp_fatal(str_, verbose, "must specify an action");
858         }
859
860         act_str++;
861     }
862     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
863          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
864         const struct protocol *p;
865
866         if (parse_protocol(name, &p)) {
867             match_set_dl_type(&fm->match, htons(p->dl_type));
868             if (p->nw_proto) {
869                 match_set_nw_proto(&fm->match, p->nw_proto);
870             }
871         } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
872             fm->flags |= OFPFF_SEND_FLOW_REM;
873         } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
874             fm->flags |= OFPFF_CHECK_OVERLAP;
875         } else if (fields & F_FLAGS && !strcmp(name, "reset_counts")) {
876             fm->flags |= OFPFF12_RESET_COUNTS;
877         } else if (fields & F_FLAGS && !strcmp(name, "no_packet_counts")) {
878             fm->flags |= OFPFF13_NO_PKT_COUNTS;
879         } else if (fields & F_FLAGS && !strcmp(name, "no_byte_counts")) {
880             fm->flags |= OFPFF13_NO_BYT_COUNTS;
881         } else {
882             char *value;
883
884             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
885             if (!value) {
886                 ofp_fatal(str_, verbose, "field %s missing value", name);
887             }
888
889             if (!strcmp(name, "table")) {
890                 fm->table_id = str_to_table_id(value);
891             } else if (!strcmp(name, "out_port")) {
892                 if (!ofputil_port_from_string(name, &fm->out_port)) {
893                     ofp_fatal(str_, verbose, "%s is not a valid OpenFlow port",
894                               name);
895                 }
896             } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
897                 fm->priority = str_to_u16(value, name);
898             } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
899                 fm->idle_timeout = str_to_u16(value, name);
900             } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
901                 fm->hard_timeout = str_to_u16(value, name);
902             } else if (!strcmp(name, "cookie")) {
903                 char *mask = strchr(value, '/');
904
905                 if (mask) {
906                     /* A mask means we're searching for a cookie. */
907                     if (command == OFPFC_ADD) {
908                         ofp_fatal(str_, verbose, "flow additions cannot use "
909                                   "a cookie mask");
910                     }
911                     *mask = '\0';
912                     fm->cookie = htonll(str_to_u64(value));
913                     fm->cookie_mask = htonll(str_to_u64(mask+1));
914                 } else {
915                     /* No mask means that the cookie is being set. */
916                     if (command != OFPFC_ADD && command != OFPFC_MODIFY
917                             && command != OFPFC_MODIFY_STRICT) {
918                         ofp_fatal(str_, verbose, "cannot set cookie");
919                     }
920                     fm->new_cookie = htonll(str_to_u64(value));
921                 }
922             } else if (mf_from_name(name)) {
923                 parse_field(mf_from_name(name), value, &fm->match);
924             } else if (!strcmp(name, "duration")
925                        || !strcmp(name, "n_packets")
926                        || !strcmp(name, "n_bytes")
927                        || !strcmp(name, "idle_age")
928                        || !strcmp(name, "hard_age")) {
929                 /* Ignore these, so that users can feed the output of
930                  * "ovs-ofctl dump-flows" back into commands that parse
931                  * flows. */
932             } else {
933                 ofp_fatal(str_, verbose, "unknown keyword %s", name);
934             }
935         }
936     }
937     if (!fm->cookie_mask && fm->new_cookie == htonll(UINT64_MAX)
938             && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
939         /* On modifies without a mask, we are supposed to add a flow if
940          * one does not exist.  If a cookie wasn't been specified, use a
941          * default of zero. */
942         fm->new_cookie = htonll(0);
943     }
944     if (fields & F_ACTIONS) {
945         struct ofpbuf ofpacts;
946
947         ofpbuf_init(&ofpacts, 32);
948         str_to_inst_ofpacts(&fm->match.flow, act_str, &ofpacts);
949         fm->ofpacts_len = ofpacts.size;
950         fm->ofpacts = ofpbuf_steal_data(&ofpacts);
951     } else {
952         fm->ofpacts_len = 0;
953         fm->ofpacts = NULL;
954     }
955
956     free(string);
957 }
958
959 /* Convert 'str_' (as described in the documentation for the "monitor" command
960  * in the ovs-ofctl man page) into 'fmr'. */
961 void
962 parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
963                            const char *str_)
964 {
965     static uint32_t id;
966
967     char *string = xstrdup(str_);
968     char *save_ptr = NULL;
969     char *name;
970
971     fmr->id = id++;
972     fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY
973                   | NXFMF_OWN | NXFMF_ACTIONS);
974     fmr->out_port = OFPP_NONE;
975     fmr->table_id = 0xff;
976     match_init_catchall(&fmr->match);
977
978     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
979          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
980         const struct protocol *p;
981
982         if (!strcmp(name, "!initial")) {
983             fmr->flags &= ~NXFMF_INITIAL;
984         } else if (!strcmp(name, "!add")) {
985             fmr->flags &= ~NXFMF_ADD;
986         } else if (!strcmp(name, "!delete")) {
987             fmr->flags &= ~NXFMF_DELETE;
988         } else if (!strcmp(name, "!modify")) {
989             fmr->flags &= ~NXFMF_MODIFY;
990         } else if (!strcmp(name, "!actions")) {
991             fmr->flags &= ~NXFMF_ACTIONS;
992         } else if (!strcmp(name, "!own")) {
993             fmr->flags &= ~NXFMF_OWN;
994         } else if (parse_protocol(name, &p)) {
995             match_set_dl_type(&fmr->match, htons(p->dl_type));
996             if (p->nw_proto) {
997                 match_set_nw_proto(&fmr->match, p->nw_proto);
998             }
999         } else {
1000             char *value;
1001
1002             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1003             if (!value) {
1004                 ovs_fatal(0, "%s: field %s missing value", str_, name);
1005             }
1006
1007             if (!strcmp(name, "table")) {
1008                 fmr->table_id = str_to_table_id(value);
1009             } else if (!strcmp(name, "out_port")) {
1010                 fmr->out_port = atoi(value);
1011             } else if (mf_from_name(name)) {
1012                 parse_field(mf_from_name(name), value, &fmr->match);
1013             } else {
1014                 ovs_fatal(0, "%s: unknown keyword %s", str_, name);
1015             }
1016         }
1017     }
1018     free(string);
1019 }
1020
1021 /* Parses 's' as a set of OpenFlow actions and appends the actions to
1022  * 'actions'.
1023  *
1024  * Prints an error on stderr and aborts the program if 's' syntax is
1025  * invalid. */
1026 void
1027 parse_ofpacts(const char *s_, struct ofpbuf *ofpacts)
1028 {
1029     char *s = xstrdup(s_);
1030     str_to_ofpacts(NULL, s, ofpacts);
1031     free(s);
1032 }
1033
1034 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
1035  * (one of OFPFC_*) into 'fm'. */
1036 void
1037 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
1038                        uint16_t command, bool verbose)
1039 {
1040     struct match match_copy;
1041
1042     parse_ofp_str(fm, command, string, verbose);
1043
1044     /* Normalize a copy of the match.  This ensures that non-normalized flows
1045      * get logged but doesn't affect what gets sent to the switch, so that the
1046      * switch can do whatever it likes with the flow. */
1047     match_copy = fm->match;
1048     ofputil_normalize_match(&match_copy);
1049 }
1050
1051 void
1052 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
1053                         struct ofputil_flow_mod **fms, size_t *n_fms)
1054 {
1055     size_t allocated_fms;
1056     FILE *stream;
1057     struct ds s;
1058
1059     stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
1060     if (stream == NULL) {
1061         ovs_fatal(errno, "%s: open", file_name);
1062     }
1063
1064     allocated_fms = *n_fms;
1065     ds_init(&s);
1066     while (!ds_get_preprocessed_line(&s, stream)) {
1067         if (*n_fms >= allocated_fms) {
1068             *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
1069         }
1070         parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command, false);
1071         *n_fms += 1;
1072     }
1073     ds_destroy(&s);
1074
1075     if (stream != stdin) {
1076         fclose(stream);
1077     }
1078 }
1079
1080 void
1081 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
1082                                  bool aggregate, const char *string)
1083 {
1084     struct ofputil_flow_mod fm;
1085
1086     parse_ofp_str(&fm, -1, string, false);
1087     fsr->aggregate = aggregate;
1088     fsr->cookie = fm.cookie;
1089     fsr->cookie_mask = fm.cookie_mask;
1090     fsr->match = fm.match;
1091     fsr->out_port = fm.out_port;
1092     fsr->table_id = fm.table_id;
1093 }
1094
1095 /* Parses a specification of a flow from 's' into 'flow'.  's' must take the
1096  * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
1097  * mf_field.  Fields must be specified in a natural order for satisfying
1098  * prerequisites.
1099  *
1100  * Returns NULL on success, otherwise a malloc()'d string that explains the
1101  * problem. */
1102 char *
1103 parse_ofp_exact_flow(struct flow *flow, const char *s)
1104 {
1105     char *pos, *key, *value_s;
1106     char *error = NULL;
1107     char *copy;
1108
1109     memset(flow, 0, sizeof *flow);
1110
1111     pos = copy = xstrdup(s);
1112     while (ofputil_parse_key_value(&pos, &key, &value_s)) {
1113         const struct protocol *p;
1114         if (parse_protocol(key, &p)) {
1115             if (flow->dl_type) {
1116                 error = xasprintf("%s: Ethernet type set multiple times", s);
1117                 goto exit;
1118             }
1119             flow->dl_type = htons(p->dl_type);
1120
1121             if (p->nw_proto) {
1122                 if (flow->nw_proto) {
1123                     error = xasprintf("%s: network protocol set "
1124                                       "multiple times", s);
1125                     goto exit;
1126                 }
1127                 flow->nw_proto = p->nw_proto;
1128             }
1129         } else {
1130             const struct mf_field *mf;
1131             union mf_value value;
1132             char *field_error;
1133
1134             mf = mf_from_name(key);
1135             if (!mf) {
1136                 error = xasprintf("%s: unknown field %s", s, key);
1137                 goto exit;
1138             }
1139
1140             if (!mf_are_prereqs_ok(mf, flow)) {
1141                 error = xasprintf("%s: prerequisites not met for setting %s",
1142                                   s, key);
1143                 goto exit;
1144             }
1145
1146             if (!mf_is_zero(mf, flow)) {
1147                 error = xasprintf("%s: field %s set multiple times", s, key);
1148                 goto exit;
1149             }
1150
1151             field_error = mf_parse_value(mf, value_s, &value);
1152             if (field_error) {
1153                 error = xasprintf("%s: bad value for %s (%s)",
1154                                   s, key, field_error);
1155                 free(field_error);
1156                 goto exit;
1157             }
1158
1159             mf_set_flow_value(mf, &value, flow);
1160         }
1161     }
1162
1163     if (!flow->in_port) {
1164         flow->in_port = OFPP_NONE;
1165     }
1166
1167 exit:
1168     free(copy);
1169
1170     if (error) {
1171         memset(flow, 0, sizeof *flow);
1172     }
1173     return error;
1174 }