2 * Copyright (c) 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "ofp-parse.h"
26 #include "byte-order.h"
27 #include "dynamic-string.h"
29 #include "meta-flow.h"
30 #include "multipath.h"
33 #include "ofp-actions.h"
36 #include "openflow/openflow.h"
37 #include "ovs-thread.h"
40 #include "socket-util.h"
43 /* Parses 'str' as an 8-bit unsigned integer into '*valuep'.
45 * 'name' describes the value parsed in an error message, if any.
47 * Returns NULL if successful, otherwise a malloc()'d string describing the
48 * error. The caller is responsible for freeing the returned string. */
49 char * WARN_UNUSED_RESULT
50 str_to_u8(const char *str, const char *name, uint8_t *valuep)
54 if (!str_to_int(str, 0, &value) || value < 0 || value > 255) {
55 return xasprintf("invalid %s \"%s\"", name, str);
61 /* Parses 'str' as a 16-bit unsigned integer into '*valuep'.
63 * 'name' describes the value parsed in an error message, if any.
65 * Returns NULL if successful, otherwise a malloc()'d string describing the
66 * error. The caller is responsible for freeing the returned string. */
67 char * WARN_UNUSED_RESULT
68 str_to_u16(const char *str, const char *name, uint16_t *valuep)
72 if (!str_to_int(str, 0, &value) || value < 0 || value > 65535) {
73 return xasprintf("invalid %s \"%s\"", name, str);
79 /* Parses 'str' as a 32-bit unsigned integer into '*valuep'.
81 * Returns NULL if successful, otherwise a malloc()'d string describing the
82 * error. The caller is responsible for freeing the returned string. */
83 char * WARN_UNUSED_RESULT
84 str_to_u32(const char *str, uint32_t *valuep)
90 return xstrdup("missing required numeric argument");
94 value = strtoul(str, &tail, 0);
95 if (errno == EINVAL || errno == ERANGE || *tail) {
96 return xasprintf("invalid numeric format %s", str);
102 /* Parses 'str' as an 64-bit unsigned integer into '*valuep'.
104 * Returns NULL if successful, otherwise a malloc()'d string describing the
105 * error. The caller is responsible for freeing the returned string. */
106 char * WARN_UNUSED_RESULT
107 str_to_u64(const char *str, uint64_t *valuep)
113 return xstrdup("missing required numeric argument");
117 value = strtoull(str, &tail, 0);
118 if (errno == EINVAL || errno == ERANGE || *tail) {
119 return xasprintf("invalid numeric format %s", str);
125 /* Parses 'str' as an 64-bit unsigned integer in network byte order into
128 * Returns NULL if successful, otherwise a malloc()'d string describing the
129 * error. The caller is responsible for freeing the returned string. */
130 char * WARN_UNUSED_RESULT
131 str_to_be64(const char *str, ovs_be64 *valuep)
136 error = str_to_u64(str, &value);
138 *valuep = htonll(value);
143 /* Parses 'str' as an Ethernet address into 'mac'.
145 * Returns NULL if successful, otherwise a malloc()'d string describing the
146 * error. The caller is responsible for freeing the returned string. */
147 char * WARN_UNUSED_RESULT
148 str_to_mac(const char *str, uint8_t mac[6])
150 if (!ovs_scan(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))) {
151 return xasprintf("invalid mac address %s", str);
156 /* Parses 'str' as an IP address into '*ip'.
158 * Returns NULL if successful, otherwise a malloc()'d string describing the
159 * error. The caller is responsible for freeing the returned string. */
160 char * WARN_UNUSED_RESULT
161 str_to_ip(const char *str, ovs_be32 *ip)
163 struct in_addr in_addr;
165 if (lookup_ip(str, &in_addr)) {
166 return xasprintf("%s: could not convert to IP address", str);
168 *ip = in_addr.s_addr;
172 /* Parses 'arg' as the argument to an "enqueue" action, and appends such an
173 * action to 'ofpacts'.
175 * Returns NULL if successful, otherwise a malloc()'d string describing the
176 * error. The caller is responsible for freeing the returned string. */
177 static char * WARN_UNUSED_RESULT
178 parse_enqueue(char *arg, struct ofpbuf *ofpacts)
181 char *port = strtok_r(arg, ":q,", &sp);
182 char *queue = strtok_r(NULL, "", &sp);
183 struct ofpact_enqueue *enqueue;
185 if (port == NULL || queue == NULL) {
186 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\" or "
187 "\"enqueue(PORT,QUEUE)\"");
190 enqueue = ofpact_put_ENQUEUE(ofpacts);
191 if (!ofputil_port_from_string(port, &enqueue->port)) {
192 return xasprintf("%s: enqueue to unknown port", port);
194 return str_to_u32(queue, &enqueue->queue);
197 /* Parses 'arg' as the argument to an "output" action, and appends such an
198 * action to 'ofpacts'.
200 * Returns NULL if successful, otherwise a malloc()'d string describing the
201 * error. The caller is responsible for freeing the returned string. */
202 static char * WARN_UNUSED_RESULT
203 parse_output(const char *arg, struct ofpbuf *ofpacts)
205 if (strchr(arg, '[')) {
206 struct ofpact_output_reg *output_reg;
208 output_reg = ofpact_put_OUTPUT_REG(ofpacts);
209 output_reg->max_len = UINT16_MAX;
210 return mf_parse_subfield(&output_reg->src, arg);
212 struct ofpact_output *output;
214 output = ofpact_put_OUTPUT(ofpacts);
215 if (!ofputil_port_from_string(arg, &output->port)) {
216 return xasprintf("%s: output to unknown port", arg);
218 output->max_len = output->port == OFPP_CONTROLLER ? UINT16_MAX : 0;
223 /* Parses 'arg' as the argument to an "resubmit" action, and appends such an
224 * action to 'ofpacts'.
226 * Returns NULL if successful, otherwise a malloc()'d string describing the
227 * error. The caller is responsible for freeing the returned string. */
228 static char * WARN_UNUSED_RESULT
229 parse_resubmit(char *arg, struct ofpbuf *ofpacts)
231 struct ofpact_resubmit *resubmit;
232 char *in_port_s, *table_s;
234 resubmit = ofpact_put_RESUBMIT(ofpacts);
236 in_port_s = strsep(&arg, ",");
237 if (in_port_s && in_port_s[0]) {
238 if (!ofputil_port_from_string(in_port_s, &resubmit->in_port)) {
239 return xasprintf("%s: resubmit to unknown port", in_port_s);
242 resubmit->in_port = OFPP_IN_PORT;
245 table_s = strsep(&arg, ",");
246 if (table_s && table_s[0]) {
247 uint32_t table_id = 0;
250 error = str_to_u32(table_s, &table_id);
254 resubmit->table_id = table_id;
256 resubmit->table_id = 255;
259 if (resubmit->in_port == OFPP_IN_PORT && resubmit->table_id == 255) {
260 return xstrdup("at least one \"in_port\" or \"table\" must be "
261 "specified on resubmit");
266 /* Parses 'arg' as the argument to a "note" action, and appends such an action
269 * Returns NULL if successful, otherwise a malloc()'d string describing the
270 * error. The caller is responsible for freeing the returned string. */
271 static char * WARN_UNUSED_RESULT
272 parse_note(const char *arg, struct ofpbuf *ofpacts)
274 struct ofpact_note *note;
276 note = ofpact_put_NOTE(ofpacts);
277 while (*arg != '\0') {
288 byte = hexits_value(arg, 2, &ok);
290 return xstrdup("bad hex digit in `note' argument");
292 ofpbuf_put(ofpacts, &byte, 1);
294 note = ofpacts->frame;
299 ofpact_update_len(ofpacts, ¬e->ofpact);
303 /* Parses 'arg' as the argument to a "fin_timeout" action, and appends such an
304 * action to 'ofpacts'.
306 * Returns NULL if successful, otherwise a malloc()'d string describing the
307 * error. The caller is responsible for freeing the returned string. */
308 static char * WARN_UNUSED_RESULT
309 parse_fin_timeout(struct ofpbuf *b, char *arg)
311 struct ofpact_fin_timeout *oft = ofpact_put_FIN_TIMEOUT(b);
314 while (ofputil_parse_key_value(&arg, &key, &value)) {
317 if (!strcmp(key, "idle_timeout")) {
318 error = str_to_u16(value, key, &oft->fin_idle_timeout);
319 } else if (!strcmp(key, "hard_timeout")) {
320 error = str_to_u16(value, key, &oft->fin_hard_timeout);
322 error = xasprintf("invalid key '%s' in 'fin_timeout' argument",
333 /* Parses 'arg' as the argument to a "controller" action, and appends such an
334 * action to 'ofpacts'.
336 * Returns NULL if successful, otherwise a malloc()'d string describing the
337 * error. The caller is responsible for freeing the returned string. */
338 static char * WARN_UNUSED_RESULT
339 parse_controller(struct ofpbuf *b, char *arg)
341 enum ofp_packet_in_reason reason = OFPR_ACTION;
342 uint16_t controller_id = 0;
343 uint16_t max_len = UINT16_MAX;
347 } else if (strspn(arg, "0123456789") == strlen(arg)) {
348 char *error = str_to_u16(arg, "max_len", &max_len);
355 while (ofputil_parse_key_value(&arg, &name, &value)) {
356 if (!strcmp(name, "reason")) {
357 if (!ofputil_packet_in_reason_from_string(value, &reason)) {
358 return xasprintf("unknown reason \"%s\"", value);
360 } else if (!strcmp(name, "max_len")) {
361 char *error = str_to_u16(value, "max_len", &max_len);
365 } else if (!strcmp(name, "id")) {
366 char *error = str_to_u16(value, "id", &controller_id);
371 return xasprintf("unknown key \"%s\" parsing controller "
377 if (reason == OFPR_ACTION && controller_id == 0) {
378 struct ofpact_output *output;
380 output = ofpact_put_OUTPUT(b);
381 output->port = OFPP_CONTROLLER;
382 output->max_len = max_len;
384 struct ofpact_controller *controller;
386 controller = ofpact_put_CONTROLLER(b);
387 controller->max_len = max_len;
388 controller->reason = reason;
389 controller->controller_id = controller_id;
396 parse_noargs_dec_ttl(struct ofpbuf *b)
398 struct ofpact_cnt_ids *ids;
401 ofpact_put_DEC_TTL(b);
402 ofpbuf_put(b, &id, sizeof id);
404 ids->n_controllers++;
405 ofpact_update_len(b, &ids->ofpact);
408 /* Parses 'arg' as the argument to a "dec_ttl" action, and appends such an
409 * action to 'ofpacts'.
411 * Returns NULL if successful, otherwise a malloc()'d string describing the
412 * error. The caller is responsible for freeing the returned string. */
413 static char * WARN_UNUSED_RESULT
414 parse_dec_ttl(struct ofpbuf *b, char *arg)
417 parse_noargs_dec_ttl(b);
419 struct ofpact_cnt_ids *ids;
422 ids = ofpact_put_DEC_TTL(b);
423 ids->ofpact.compat = OFPUTIL_NXAST_DEC_TTL_CNT_IDS;
424 for (cntr = strtok_r(arg, ", ", &arg); cntr != NULL;
425 cntr = strtok_r(NULL, ", ", &arg)) {
426 uint16_t id = atoi(cntr);
428 ofpbuf_put(b, &id, sizeof id);
430 ids->n_controllers++;
432 if (!ids->n_controllers) {
433 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
436 ofpact_update_len(b, &ids->ofpact);
441 /* Parses 'arg' as the argument to a "set_mpls_label" action, and appends such
444 * Returns NULL if successful, otherwise a malloc()'d string describing the
445 * error. The caller is responsible for freeing the returned string. */
446 static char * WARN_UNUSED_RESULT
447 parse_set_mpls_label(struct ofpbuf *b, const char *arg)
449 struct ofpact_mpls_label *mpls_label = ofpact_put_SET_MPLS_LABEL(b);
452 return xstrdup("parse_set_mpls_label: expected label.");
455 mpls_label->label = htonl(atoi(arg));
459 /* Parses 'arg' as the argument to a "set_mpls_tc" action, and appends such an
462 * Returns NULL if successful, otherwise a malloc()'d string describing the
463 * error. The caller is responsible for freeing the returned string. */
464 static char * WARN_UNUSED_RESULT
465 parse_set_mpls_tc(struct ofpbuf *b, const char *arg)
467 struct ofpact_mpls_tc *mpls_tc = ofpact_put_SET_MPLS_TC(b);
470 return xstrdup("parse_set_mpls_tc: expected tc.");
473 mpls_tc->tc = atoi(arg);
477 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
478 * action to 'ofpacts'.
480 * Returns NULL if successful, otherwise a malloc()'d string describing the
481 * error. The caller is responsible for freeing the returned string. */
482 static char * WARN_UNUSED_RESULT
483 parse_set_mpls_ttl(struct ofpbuf *b, const char *arg)
485 struct ofpact_mpls_ttl *mpls_ttl = ofpact_put_SET_MPLS_TTL(b);
488 return xstrdup("parse_set_mpls_ttl: expected ttl.");
491 mpls_ttl->ttl = atoi(arg);
495 /* Parses a "set_field" action with argument 'arg', appending the parsed
496 * action to 'ofpacts'.
498 * Returns NULL if successful, otherwise a malloc()'d string describing the
499 * error. The caller is responsible for freeing the returned string. */
500 static char * WARN_UNUSED_RESULT
501 set_field_parse__(char *arg, struct ofpbuf *ofpacts,
502 enum ofputil_protocol *usable_protocols)
504 struct ofpact_set_field *sf = ofpact_put_SET_FIELD(ofpacts);
508 const struct mf_field *mf;
512 delim = strstr(arg, "->");
514 return xasprintf("%s: missing `->'", arg);
516 if (strlen(delim) <= strlen("->")) {
517 return xasprintf("%s: missing field name following `->'", arg);
520 key = delim + strlen("->");
521 mf = mf_from_name(key);
523 return xasprintf("%s is not a valid OXM field name", key);
526 return xasprintf("%s is read-only", key);
530 error = mf_parse_value(mf, value, &sf->value);
535 if (!mf_is_value_valid(mf, &sf->value)) {
536 return xasprintf("%s is not a valid value for field %s", value, key);
539 *usable_protocols &= mf->usable_protocols;
543 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
544 * action to 'ofpacts'.
546 * Returns NULL if successful, otherwise a malloc()'d string describing the
547 * error. The caller is responsible for freeing the returned string. */
548 static char * WARN_UNUSED_RESULT
549 set_field_parse(const char *arg, struct ofpbuf *ofpacts,
550 enum ofputil_protocol *usable_protocols)
552 char *copy = xstrdup(arg);
553 char *error = set_field_parse__(copy, ofpacts, usable_protocols);
558 /* Parses 'arg' as the argument to a "write_metadata" instruction, and appends
559 * such an action to 'ofpacts'.
561 * Returns NULL if successful, otherwise a malloc()'d string describing the
562 * error. The caller is responsible for freeing the returned string. */
563 static char * WARN_UNUSED_RESULT
564 parse_metadata(struct ofpbuf *b, char *arg)
566 struct ofpact_metadata *om;
567 char *mask = strchr(arg, '/');
569 om = ofpact_put_WRITE_METADATA(b);
575 error = str_to_be64(mask + 1, &om->mask);
580 om->mask = OVS_BE64_MAX;
583 return str_to_be64(arg, &om->metadata);
586 /* Parses 'arg' as the argument to a "sample" action, and appends such an
587 * action to 'ofpacts'.
589 * Returns NULL if successful, otherwise a malloc()'d string describing the
590 * error. The caller is responsible for freeing the returned string. */
591 static char * WARN_UNUSED_RESULT
592 parse_sample(struct ofpbuf *b, char *arg)
594 struct ofpact_sample *os = ofpact_put_SAMPLE(b);
597 while (ofputil_parse_key_value(&arg, &key, &value)) {
600 if (!strcmp(key, "probability")) {
601 error = str_to_u16(value, "probability", &os->probability);
602 if (!error && os->probability == 0) {
603 error = xasprintf("invalid probability value \"%s\"", value);
605 } else if (!strcmp(key, "collector_set_id")) {
606 error = str_to_u32(value, &os->collector_set_id);
607 } else if (!strcmp(key, "obs_domain_id")) {
608 error = str_to_u32(value, &os->obs_domain_id);
609 } else if (!strcmp(key, "obs_point_id")) {
610 error = str_to_u32(value, &os->obs_point_id);
612 error = xasprintf("invalid key \"%s\" in \"sample\" argument",
619 if (os->probability == 0) {
620 return xstrdup("non-zero \"probability\" must be specified on sample");
625 /* Parses 'arg' as the argument to action 'code', and appends such an action to
628 * Returns NULL if successful, otherwise a malloc()'d string describing the
629 * error. The caller is responsible for freeing the returned string. */
630 static char * WARN_UNUSED_RESULT
631 parse_named_action(enum ofputil_action_code code,
632 char *arg, struct ofpbuf *ofpacts,
633 enum ofputil_protocol *usable_protocols)
635 size_t orig_size = ofpbuf_size(ofpacts);
636 struct ofpact_tunnel *tunnel;
637 struct ofpact_vlan_vid *vlan_vid;
638 struct ofpact_vlan_pcp *vlan_pcp;
640 uint16_t ethertype = 0;
648 case OFPUTIL_ACTION_INVALID:
651 case OFPUTIL_OFPAT10_OUTPUT:
652 case OFPUTIL_OFPAT11_OUTPUT:
653 case OFPUTIL_OFPAT13_OUTPUT:
654 error = parse_output(arg, ofpacts);
657 case OFPUTIL_OFPAT10_SET_VLAN_VID:
658 case OFPUTIL_OFPAT11_SET_VLAN_VID:
659 error = str_to_u16(arg, "VLAN VID", &vid);
664 if (vid & ~VLAN_VID_MASK) {
665 return xasprintf("%s: not a valid VLAN VID", arg);
667 vlan_vid = ofpact_put_SET_VLAN_VID(ofpacts);
668 vlan_vid->vlan_vid = vid;
669 vlan_vid->ofpact.compat = code;
670 vlan_vid->push_vlan_if_needed = code == OFPUTIL_OFPAT10_SET_VLAN_VID;
673 case OFPUTIL_OFPAT10_SET_VLAN_PCP:
674 case OFPUTIL_OFPAT11_SET_VLAN_PCP:
675 error = str_to_u8(arg, "VLAN PCP", &pcp);
681 return xasprintf("%s: not a valid VLAN PCP", arg);
683 vlan_pcp = ofpact_put_SET_VLAN_PCP(ofpacts);
684 vlan_pcp->vlan_pcp = pcp;
685 vlan_pcp->ofpact.compat = code;
686 vlan_pcp->push_vlan_if_needed = code == OFPUTIL_OFPAT10_SET_VLAN_PCP;
689 case OFPUTIL_OFPAT12_SET_FIELD:
690 case OFPUTIL_OFPAT13_SET_FIELD:
691 return set_field_parse(arg, ofpacts, usable_protocols);
693 case OFPUTIL_OFPAT10_STRIP_VLAN:
694 case OFPUTIL_OFPAT11_POP_VLAN:
695 case OFPUTIL_OFPAT13_POP_VLAN:
696 ofpact_put_STRIP_VLAN(ofpacts)->ofpact.compat = code;
699 case OFPUTIL_OFPAT11_PUSH_VLAN:
700 case OFPUTIL_OFPAT13_PUSH_VLAN:
701 *usable_protocols &= OFPUTIL_P_OF11_UP;
702 error = str_to_u16(arg, "ethertype", ðertype);
707 if (ethertype != ETH_TYPE_VLAN_8021Q) {
708 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
709 return xasprintf("%s: not a valid VLAN ethertype", arg);
712 ofpact_put_PUSH_VLAN(ofpacts);
715 case OFPUTIL_OFPAT11_SET_QUEUE:
716 case OFPUTIL_OFPAT13_SET_QUEUE:
717 error = str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
720 case OFPUTIL_OFPAT10_SET_DL_SRC:
721 case OFPUTIL_OFPAT11_SET_DL_SRC:
722 error = str_to_mac(arg, ofpact_put_SET_ETH_SRC(ofpacts)->mac);
725 case OFPUTIL_OFPAT10_SET_DL_DST:
726 case OFPUTIL_OFPAT11_SET_DL_DST:
727 error = str_to_mac(arg, ofpact_put_SET_ETH_DST(ofpacts)->mac);
730 case OFPUTIL_OFPAT10_SET_NW_SRC:
731 case OFPUTIL_OFPAT11_SET_NW_SRC:
732 error = str_to_ip(arg, &ofpact_put_SET_IPV4_SRC(ofpacts)->ipv4);
735 case OFPUTIL_OFPAT10_SET_NW_DST:
736 case OFPUTIL_OFPAT11_SET_NW_DST:
737 error = str_to_ip(arg, &ofpact_put_SET_IPV4_DST(ofpacts)->ipv4);
740 case OFPUTIL_OFPAT10_SET_NW_TOS:
741 case OFPUTIL_OFPAT11_SET_NW_TOS:
742 error = str_to_u8(arg, "TOS", &tos);
747 if (tos & ~IP_DSCP_MASK) {
748 return xasprintf("%s: not a valid TOS", arg);
750 ofpact_put_SET_IP_DSCP(ofpacts)->dscp = tos;
753 case OFPUTIL_OFPAT11_SET_NW_ECN:
754 error = str_to_u8(arg, "ECN", &ecn);
759 if (ecn & ~IP_ECN_MASK) {
760 return xasprintf("%s: not a valid ECN", arg);
762 ofpact_put_SET_IP_ECN(ofpacts)->ecn = ecn;
765 case OFPUTIL_OFPAT11_SET_NW_TTL:
766 case OFPUTIL_OFPAT13_SET_NW_TTL:
767 error = str_to_u8(arg, "TTL", &ttl);
772 ofpact_put_SET_IP_TTL(ofpacts)->ttl = ttl;
775 case OFPUTIL_OFPAT11_DEC_NW_TTL:
776 case OFPUTIL_OFPAT13_DEC_NW_TTL:
779 case OFPUTIL_OFPAT10_SET_TP_SRC:
780 case OFPUTIL_OFPAT11_SET_TP_SRC:
781 error = str_to_u16(arg, "source port",
782 &ofpact_put_SET_L4_SRC_PORT(ofpacts)->port);
785 case OFPUTIL_OFPAT10_SET_TP_DST:
786 case OFPUTIL_OFPAT11_SET_TP_DST:
787 error = str_to_u16(arg, "destination port",
788 &ofpact_put_SET_L4_DST_PORT(ofpacts)->port);
791 case OFPUTIL_OFPAT10_ENQUEUE:
792 error = parse_enqueue(arg, ofpacts);
795 case OFPUTIL_NXAST_RESUBMIT:
796 error = parse_resubmit(arg, ofpacts);
799 case OFPUTIL_NXAST_SET_TUNNEL:
800 case OFPUTIL_NXAST_SET_TUNNEL64:
801 tunnel = ofpact_put_SET_TUNNEL(ofpacts);
802 tunnel->ofpact.compat = code;
803 error = str_to_u64(arg, &tunnel->tun_id);
806 case OFPUTIL_NXAST_WRITE_METADATA:
807 error = parse_metadata(ofpacts, arg);
810 case OFPUTIL_NXAST_SET_QUEUE:
811 error = str_to_u32(arg, &ofpact_put_SET_QUEUE(ofpacts)->queue_id);
814 case OFPUTIL_NXAST_POP_QUEUE:
815 ofpact_put_POP_QUEUE(ofpacts);
818 case OFPUTIL_NXAST_REG_MOVE:
819 error = nxm_parse_reg_move(ofpact_put_REG_MOVE(ofpacts), arg);
822 case OFPUTIL_NXAST_REG_LOAD:
823 error = nxm_parse_reg_load(ofpact_put_REG_LOAD(ofpacts), arg);
826 case OFPUTIL_NXAST_NOTE:
827 error = parse_note(arg, ofpacts);
830 case OFPUTIL_NXAST_MULTIPATH:
831 error = multipath_parse(ofpact_put_MULTIPATH(ofpacts), arg);
834 case OFPUTIL_NXAST_BUNDLE:
835 error = bundle_parse(arg, ofpacts);
838 case OFPUTIL_NXAST_BUNDLE_LOAD:
839 error = bundle_parse_load(arg, ofpacts);
842 case OFPUTIL_NXAST_RESUBMIT_TABLE:
843 case OFPUTIL_NXAST_OUTPUT_REG:
844 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS:
847 case OFPUTIL_NXAST_LEARN:
848 error = learn_parse(arg, ofpacts);
851 case OFPUTIL_NXAST_EXIT:
852 ofpact_put_EXIT(ofpacts);
855 case OFPUTIL_NXAST_DEC_TTL:
856 error = parse_dec_ttl(ofpacts, arg);
859 case OFPUTIL_NXAST_SET_MPLS_LABEL:
860 case OFPUTIL_OFPAT11_SET_MPLS_LABEL:
861 error = parse_set_mpls_label(ofpacts, arg);
864 case OFPUTIL_NXAST_SET_MPLS_TC:
865 case OFPUTIL_OFPAT11_SET_MPLS_TC:
866 error = parse_set_mpls_tc(ofpacts, arg);
869 case OFPUTIL_NXAST_SET_MPLS_TTL:
870 case OFPUTIL_OFPAT11_SET_MPLS_TTL:
871 case OFPUTIL_OFPAT13_SET_MPLS_TTL:
872 error = parse_set_mpls_ttl(ofpacts, arg);
875 case OFPUTIL_OFPAT11_DEC_MPLS_TTL:
876 case OFPUTIL_OFPAT13_DEC_MPLS_TTL:
877 case OFPUTIL_NXAST_DEC_MPLS_TTL:
878 ofpact_put_DEC_MPLS_TTL(ofpacts);
881 case OFPUTIL_NXAST_FIN_TIMEOUT:
882 error = parse_fin_timeout(ofpacts, arg);
885 case OFPUTIL_NXAST_CONTROLLER:
886 error = parse_controller(ofpacts, arg);
889 case OFPUTIL_OFPAT11_PUSH_MPLS:
890 case OFPUTIL_OFPAT13_PUSH_MPLS:
891 case OFPUTIL_NXAST_PUSH_MPLS:
892 error = str_to_u16(arg, "push_mpls", ðertype);
894 ofpact_put_PUSH_MPLS(ofpacts)->ethertype = htons(ethertype);
898 case OFPUTIL_OFPAT11_POP_MPLS:
899 case OFPUTIL_OFPAT13_POP_MPLS:
900 case OFPUTIL_NXAST_POP_MPLS:
901 error = str_to_u16(arg, "pop_mpls", ðertype);
903 ofpact_put_POP_MPLS(ofpacts)->ethertype = htons(ethertype);
907 case OFPUTIL_OFPAT11_GROUP:
908 case OFPUTIL_OFPAT13_GROUP:
909 error = str_to_u32(arg, &ofpact_put_GROUP(ofpacts)->group_id);
912 /* FIXME when implement OFPAT13_* */
913 case OFPUTIL_OFPAT13_COPY_TTL_OUT:
914 case OFPUTIL_OFPAT13_COPY_TTL_IN:
915 case OFPUTIL_OFPAT13_PUSH_PBB:
916 case OFPUTIL_OFPAT13_POP_PBB:
919 case OFPUTIL_NXAST_STACK_PUSH:
920 error = nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts), arg);
922 case OFPUTIL_NXAST_STACK_POP:
923 error = nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts), arg);
926 case OFPUTIL_NXAST_SAMPLE:
927 error = parse_sample(ofpacts, arg);
932 ofpbuf_set_size(ofpacts, orig_size);
937 /* Parses action 'act', with argument 'arg', and appends a parsed version to
940 * 'n_actions' specifies the number of actions already parsed (for proper
941 * handling of "drop" actions).
943 * Returns NULL if successful, otherwise a malloc()'d string describing the
944 * error. The caller is responsible for freeing the returned string. */
945 static char * WARN_UNUSED_RESULT
946 str_to_ofpact__(char *pos, char *act, char *arg,
947 struct ofpbuf *ofpacts, int n_actions,
948 enum ofputil_protocol *usable_protocols)
950 int code = ofputil_action_code_from_name(act);
952 return parse_named_action(code, arg, ofpacts, usable_protocols);
953 } else if (!strcasecmp(act, "drop")) {
955 return xstrdup("Drop actions must not be preceded by other "
957 } else if (ofputil_parse_key_value(&pos, &act, &arg)) {
958 return xstrdup("Drop actions must not be followed by other "
963 if (ofputil_port_from_string(act, &port)) {
964 ofpact_put_OUTPUT(ofpacts)->port = port;
966 return xasprintf("Unknown action: %s", act);
973 /* Parses 'str' as a series of actions, and appends them to 'ofpacts'.
975 * Returns NULL if successful, otherwise a malloc()'d string describing the
976 * error. The caller is responsible for freeing the returned string. */
977 static char * WARN_UNUSED_RESULT
978 str_to_ofpacts__(char *str, struct ofpbuf *ofpacts,
979 enum ofputil_protocol *usable_protocols)
981 size_t orig_size = ofpbuf_size(ofpacts);
982 char *pos, *act, *arg;
987 while (ofputil_parse_key_value(&pos, &act, &arg)) {
988 char *error = str_to_ofpact__(pos, act, arg, ofpacts, n_actions,
991 ofpbuf_set_size(ofpacts, orig_size);
1002 /* Parses 'str' as a series of actions, and appends them to 'ofpacts'.
1004 * Returns NULL if successful, otherwise a malloc()'d string describing the
1005 * error. The caller is responsible for freeing the returned string. */
1006 static char * WARN_UNUSED_RESULT
1007 str_to_ofpacts(char *str, struct ofpbuf *ofpacts,
1008 enum ofputil_protocol *usable_protocols)
1010 size_t orig_size = ofpbuf_size(ofpacts);
1014 error_s = str_to_ofpacts__(str, ofpacts, usable_protocols);
1019 error = ofpacts_verify(ofpbuf_data(ofpacts), ofpbuf_size(ofpacts),
1020 (1u << OVSINST_OFPIT11_APPLY_ACTIONS));
1022 ofpbuf_set_size(ofpacts, orig_size);
1023 return xstrdup("Incorrect action ordering");
1029 /* Parses 'arg' as the argument to instruction 'type', and appends such an
1030 * instruction to 'ofpacts'.
1032 * Returns NULL if successful, otherwise a malloc()'d string describing the
1033 * error. The caller is responsible for freeing the returned string. */
1034 static char * WARN_UNUSED_RESULT
1035 parse_named_instruction(enum ovs_instruction_type type,
1036 char *arg, struct ofpbuf *ofpacts,
1037 enum ofputil_protocol *usable_protocols)
1039 char *error_s = NULL;
1042 *usable_protocols &= OFPUTIL_P_OF11_UP;
1045 case OVSINST_OFPIT11_APPLY_ACTIONS:
1046 OVS_NOT_REACHED(); /* This case is handled by str_to_inst_ofpacts() */
1049 case OVSINST_OFPIT11_WRITE_ACTIONS: {
1050 struct ofpact_nest *on;
1053 ofpact_pad(ofpacts);
1054 ofs = ofpbuf_size(ofpacts);
1055 ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
1056 offsetof(struct ofpact_nest, actions));
1057 error_s = str_to_ofpacts__(arg, ofpacts, usable_protocols);
1059 on = ofpbuf_at_assert(ofpacts, ofs, sizeof *on);
1060 on->ofpact.len = ofpbuf_size(ofpacts) - ofs;
1063 ofpbuf_set_size(ofpacts, ofs);
1068 case OVSINST_OFPIT11_CLEAR_ACTIONS:
1069 ofpact_put_CLEAR_ACTIONS(ofpacts);
1072 case OVSINST_OFPIT13_METER:
1073 *usable_protocols &= OFPUTIL_P_OF13_UP;
1074 error_s = str_to_u32(arg, &ofpact_put_METER(ofpacts)->meter_id);
1077 case OVSINST_OFPIT11_WRITE_METADATA:
1078 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
1079 error_s = parse_metadata(ofpacts, arg);
1082 case OVSINST_OFPIT11_GOTO_TABLE: {
1083 struct ofpact_goto_table *ogt = ofpact_put_GOTO_TABLE(ofpacts);
1084 char *table_s = strsep(&arg, ",");
1085 if (!table_s || !table_s[0]) {
1086 return xstrdup("instruction goto-table needs table id");
1088 error_s = str_to_u8(table_s, "table", &ogt->table_id);
1097 /* If write_metadata is specified as an action AND an instruction, ofpacts
1098 could be invalid. */
1099 error = ofpacts_verify(ofpbuf_data(ofpacts), ofpbuf_size(ofpacts),
1100 (1u << N_OVS_INSTRUCTIONS) - 1);
1102 return xstrdup("Incorrect instruction ordering");
1107 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
1109 * Returns NULL if successful, otherwise a malloc()'d string describing the
1110 * error. The caller is responsible for freeing the returned string. */
1111 static char * WARN_UNUSED_RESULT
1112 str_to_inst_ofpacts(char *str, struct ofpbuf *ofpacts,
1113 enum ofputil_protocol *usable_protocols)
1115 size_t orig_size = ofpbuf_size(ofpacts);
1116 char *pos, *inst, *arg;
1118 const char *prev_inst = NULL;
1123 while (ofputil_parse_key_value(&pos, &inst, &arg)) {
1124 type = ovs_instruction_type_from_name(inst);
1126 char *error = str_to_ofpact__(pos, inst, arg, ofpacts, n_actions,
1129 ofpbuf_set_size(ofpacts, orig_size);
1133 type = OVSINST_OFPIT11_APPLY_ACTIONS;
1134 if (prev_type == type) {
1138 } else if (type == OVSINST_OFPIT11_APPLY_ACTIONS) {
1139 ofpbuf_set_size(ofpacts, orig_size);
1140 return xasprintf("%s isn't supported. Just write actions then "
1141 "it is interpreted as apply_actions", inst);
1143 char *error = parse_named_instruction(type, arg, ofpacts,
1146 ofpbuf_set_size(ofpacts, orig_size);
1151 if (type <= prev_type) {
1152 ofpbuf_set_size(ofpacts, orig_size);
1153 if (type == prev_type) {
1154 return xasprintf("instruction %s may be specified only once",
1157 return xasprintf("instruction %s must be specified before %s",
1166 ofpact_pad(ofpacts);
1178 parse_protocol(const char *name, const struct protocol **p_out)
1180 static const struct protocol protocols[] = {
1181 { "ip", ETH_TYPE_IP, 0 },
1182 { "arp", ETH_TYPE_ARP, 0 },
1183 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
1184 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
1185 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
1186 { "sctp", ETH_TYPE_IP, IPPROTO_SCTP },
1187 { "ipv6", ETH_TYPE_IPV6, 0 },
1188 { "ip6", ETH_TYPE_IPV6, 0 },
1189 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
1190 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
1191 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
1192 { "sctp6", ETH_TYPE_IPV6, IPPROTO_SCTP },
1193 { "rarp", ETH_TYPE_RARP, 0},
1194 { "mpls", ETH_TYPE_MPLS, 0 },
1195 { "mplsm", ETH_TYPE_MPLS_MCAST, 0 },
1197 const struct protocol *p;
1199 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
1200 if (!strcmp(p->name, name)) {
1209 /* Parses 's' as the (possibly masked) value of field 'mf', and updates
1210 * 'match' appropriately. Restricts the set of usable protocols to ones
1211 * supporting the parsed field.
1213 * Returns NULL if successful, otherwise a malloc()'d string describing the
1214 * error. The caller is responsible for freeing the returned string. */
1215 static char * WARN_UNUSED_RESULT
1216 parse_field(const struct mf_field *mf, const char *s, struct match *match,
1217 enum ofputil_protocol *usable_protocols)
1219 union mf_value value, mask;
1222 error = mf_parse(mf, s, &value, &mask);
1224 *usable_protocols &= mf_set(mf, &value, &mask, match);
1229 static char * WARN_UNUSED_RESULT
1230 parse_ofp_str__(struct ofputil_flow_mod *fm, int command, char *string,
1231 enum ofputil_protocol *usable_protocols)
1234 F_OUT_PORT = 1 << 0,
1237 F_PRIORITY = 1 << 4,
1240 char *save_ptr = NULL;
1241 char *act_str = NULL;
1244 *usable_protocols = OFPUTIL_P_ANY;
1248 fields = F_OUT_PORT;
1252 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
1256 fields = F_OUT_PORT;
1259 case OFPFC_DELETE_STRICT:
1260 fields = F_OUT_PORT | F_PRIORITY;
1264 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
1267 case OFPFC_MODIFY_STRICT:
1268 fields = F_ACTIONS | F_TIMEOUT | F_PRIORITY | F_FLAGS;
1275 match_init_catchall(&fm->match);
1276 fm->priority = OFP_DEFAULT_PRIORITY;
1277 fm->cookie = htonll(0);
1278 fm->cookie_mask = htonll(0);
1279 if (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT) {
1280 /* For modify, by default, don't update the cookie. */
1281 fm->new_cookie = OVS_BE64_MAX;
1283 fm->new_cookie = htonll(0);
1285 fm->modify_cookie = false;
1286 fm->table_id = 0xff;
1287 fm->command = command;
1288 fm->idle_timeout = OFP_FLOW_PERMANENT;
1289 fm->hard_timeout = OFP_FLOW_PERMANENT;
1290 fm->buffer_id = UINT32_MAX;
1291 fm->out_port = OFPP_ANY;
1293 fm->out_group = OFPG11_ANY;
1294 fm->delete_reason = OFPRR_DELETE;
1295 if (fields & F_ACTIONS) {
1296 act_str = strstr(string, "action");
1298 return xstrdup("must specify an action");
1302 act_str = strchr(act_str + 1, '=');
1304 return xstrdup("must specify an action");
1309 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1310 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1311 const struct protocol *p;
1314 if (parse_protocol(name, &p)) {
1315 match_set_dl_type(&fm->match, htons(p->dl_type));
1317 match_set_nw_proto(&fm->match, p->nw_proto);
1319 } else if (fields & F_FLAGS && !strcmp(name, "send_flow_rem")) {
1320 fm->flags |= OFPUTIL_FF_SEND_FLOW_REM;
1321 } else if (fields & F_FLAGS && !strcmp(name, "check_overlap")) {
1322 fm->flags |= OFPUTIL_FF_CHECK_OVERLAP;
1323 } else if (fields & F_FLAGS && !strcmp(name, "reset_counts")) {
1324 fm->flags |= OFPUTIL_FF_RESET_COUNTS;
1325 *usable_protocols &= OFPUTIL_P_OF12_UP;
1326 } else if (fields & F_FLAGS && !strcmp(name, "no_packet_counts")) {
1327 fm->flags |= OFPUTIL_FF_NO_PKT_COUNTS;
1328 *usable_protocols &= OFPUTIL_P_OF13_UP;
1329 } else if (fields & F_FLAGS && !strcmp(name, "no_byte_counts")) {
1330 fm->flags |= OFPUTIL_FF_NO_BYT_COUNTS;
1331 *usable_protocols &= OFPUTIL_P_OF13_UP;
1332 } else if (!strcmp(name, "no_readonly_table")
1333 || !strcmp(name, "allow_hidden_fields")) {
1334 /* ignore these fields. */
1338 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1340 return xasprintf("field %s missing value", name);
1343 if (!strcmp(name, "table")) {
1344 error = str_to_u8(value, "table", &fm->table_id);
1345 if (fm->table_id != 0xff) {
1346 *usable_protocols &= OFPUTIL_P_TID;
1348 } else if (!strcmp(name, "out_port")) {
1349 if (!ofputil_port_from_string(value, &fm->out_port)) {
1350 error = xasprintf("%s is not a valid OpenFlow port",
1353 } else if (fields & F_PRIORITY && !strcmp(name, "priority")) {
1354 uint16_t priority = 0;
1356 error = str_to_u16(value, name, &priority);
1357 fm->priority = priority;
1358 } else if (fields & F_TIMEOUT && !strcmp(name, "idle_timeout")) {
1359 error = str_to_u16(value, name, &fm->idle_timeout);
1360 } else if (fields & F_TIMEOUT && !strcmp(name, "hard_timeout")) {
1361 error = str_to_u16(value, name, &fm->hard_timeout);
1362 } else if (!strcmp(name, "cookie")) {
1363 char *mask = strchr(value, '/');
1366 /* A mask means we're searching for a cookie. */
1367 if (command == OFPFC_ADD) {
1368 return xstrdup("flow additions cannot use "
1372 error = str_to_be64(value, &fm->cookie);
1376 error = str_to_be64(mask + 1, &fm->cookie_mask);
1378 /* Matching of the cookie is only supported through NXM or
1380 if (fm->cookie_mask != htonll(0)) {
1381 *usable_protocols &= OFPUTIL_P_NXM_OF11_UP;
1384 /* No mask means that the cookie is being set. */
1385 if (command != OFPFC_ADD && command != OFPFC_MODIFY
1386 && command != OFPFC_MODIFY_STRICT) {
1387 return xstrdup("cannot set cookie");
1389 error = str_to_be64(value, &fm->new_cookie);
1390 fm->modify_cookie = true;
1392 } else if (mf_from_name(name)) {
1393 error = parse_field(mf_from_name(name), value, &fm->match,
1395 } else if (!strcmp(name, "duration")
1396 || !strcmp(name, "n_packets")
1397 || !strcmp(name, "n_bytes")
1398 || !strcmp(name, "idle_age")
1399 || !strcmp(name, "hard_age")) {
1400 /* Ignore these, so that users can feed the output of
1401 * "ovs-ofctl dump-flows" back into commands that parse
1404 error = xasprintf("unknown keyword %s", name);
1412 /* Check for usable protocol interdependencies between match fields. */
1413 if (fm->match.flow.dl_type == htons(ETH_TYPE_IPV6)) {
1414 const struct flow_wildcards *wc = &fm->match.wc;
1415 /* Only NXM and OXM support matching L3 and L4 fields within IPv6.
1417 * (IPv6 specific fields as well as arp_sha, arp_tha, nw_frag, and
1418 * nw_ttl are covered elsewhere so they don't need to be included in
1421 if (wc->masks.nw_proto || wc->masks.nw_tos
1422 || wc->masks.tp_src || wc->masks.tp_dst) {
1423 *usable_protocols &= OFPUTIL_P_NXM_OXM_ANY;
1426 if (!fm->cookie_mask && fm->new_cookie == OVS_BE64_MAX
1427 && (command == OFPFC_MODIFY || command == OFPFC_MODIFY_STRICT)) {
1428 /* On modifies without a mask, we are supposed to add a flow if
1429 * one does not exist. If a cookie wasn't been specified, use a
1430 * default of zero. */
1431 fm->new_cookie = htonll(0);
1433 if (fields & F_ACTIONS) {
1434 struct ofpbuf ofpacts;
1437 ofpbuf_init(&ofpacts, 32);
1438 error = str_to_inst_ofpacts(act_str, &ofpacts, usable_protocols);
1442 err = ofpacts_check(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &fm->match.flow,
1443 OFPP_MAX, fm->table_id, 255, usable_protocols);
1444 if (!err && !usable_protocols) {
1445 err = OFPERR_OFPBAC_MATCH_INCONSISTENT;
1448 error = xasprintf("actions are invalid with specified match "
1449 "(%s)", ofperr_to_string(err));
1454 ofpbuf_uninit(&ofpacts);
1458 fm->ofpacts_len = ofpbuf_size(&ofpacts);
1459 fm->ofpacts = ofpbuf_steal_data(&ofpacts);
1461 fm->ofpacts_len = 0;
1468 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
1469 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
1470 * Returns the set of usable protocols in '*usable_protocols'.
1472 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
1473 * constant for 'command'. To parse syntax for an OFPST_FLOW or
1474 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'.
1476 * Returns NULL if successful, otherwise a malloc()'d string describing the
1477 * error. The caller is responsible for freeing the returned string. */
1478 char * WARN_UNUSED_RESULT
1479 parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_,
1480 enum ofputil_protocol *usable_protocols)
1482 char *string = xstrdup(str_);
1485 error = parse_ofp_str__(fm, command, string, usable_protocols);
1488 fm->ofpacts_len = 0;
1495 static char * WARN_UNUSED_RESULT
1496 parse_ofp_meter_mod_str__(struct ofputil_meter_mod *mm, char *string,
1497 struct ofpbuf *bands, int command,
1498 enum ofputil_protocol *usable_protocols)
1505 char *save_ptr = NULL;
1506 char *band_str = NULL;
1509 /* Meters require at least OF 1.3. */
1510 *usable_protocols = OFPUTIL_P_OF13_UP;
1518 fields = F_METER | F_FLAGS | F_BANDS;
1521 case OFPMC13_DELETE:
1525 case OFPMC13_MODIFY:
1526 fields = F_METER | F_FLAGS | F_BANDS;
1533 mm->command = command;
1534 mm->meter.meter_id = 0;
1535 mm->meter.flags = 0;
1536 if (fields & F_BANDS) {
1537 band_str = strstr(string, "band");
1539 return xstrdup("must specify bands");
1543 band_str = strchr(band_str + 1, '=');
1545 return xstrdup("must specify bands");
1550 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1551 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1553 if (fields & F_FLAGS && !strcmp(name, "kbps")) {
1554 mm->meter.flags |= OFPMF13_KBPS;
1555 } else if (fields & F_FLAGS && !strcmp(name, "pktps")) {
1556 mm->meter.flags |= OFPMF13_PKTPS;
1557 } else if (fields & F_FLAGS && !strcmp(name, "burst")) {
1558 mm->meter.flags |= OFPMF13_BURST;
1559 } else if (fields & F_FLAGS && !strcmp(name, "stats")) {
1560 mm->meter.flags |= OFPMF13_STATS;
1564 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1566 return xasprintf("field %s missing value", name);
1569 if (!strcmp(name, "meter")) {
1570 if (!strcmp(value, "all")) {
1571 mm->meter.meter_id = OFPM13_ALL;
1572 } else if (!strcmp(value, "controller")) {
1573 mm->meter.meter_id = OFPM13_CONTROLLER;
1574 } else if (!strcmp(value, "slowpath")) {
1575 mm->meter.meter_id = OFPM13_SLOWPATH;
1577 char *error = str_to_u32(value, &mm->meter.meter_id);
1581 if (mm->meter.meter_id > OFPM13_MAX) {
1582 return xasprintf("invalid value for %s", name);
1586 return xasprintf("unknown keyword %s", name);
1590 if (fields & F_METER && !mm->meter.meter_id) {
1591 return xstrdup("must specify 'meter'");
1593 if (fields & F_FLAGS && !mm->meter.flags) {
1594 return xstrdup("meter must specify either 'kbps' or 'pktps'");
1597 if (fields & F_BANDS) {
1598 uint16_t n_bands = 0;
1599 struct ofputil_meter_band *band = NULL;
1602 for (name = strtok_r(band_str, "=, \t\r\n", &save_ptr); name;
1603 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1607 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1609 return xasprintf("field %s missing value", name);
1612 if (!strcmp(name, "type")) {
1613 /* Start a new band */
1614 band = ofpbuf_put_zeros(bands, sizeof *band);
1617 if (!strcmp(value, "drop")) {
1618 band->type = OFPMBT13_DROP;
1619 } else if (!strcmp(value, "dscp_remark")) {
1620 band->type = OFPMBT13_DSCP_REMARK;
1622 return xasprintf("field %s unknown value %s", name, value);
1624 } else if (!band || !band->type) {
1625 return xstrdup("band must start with the 'type' keyword");
1626 } else if (!strcmp(name, "rate")) {
1627 char *error = str_to_u32(value, &band->rate);
1631 } else if (!strcmp(name, "burst_size")) {
1632 char *error = str_to_u32(value, &band->burst_size);
1636 } else if (!strcmp(name, "prec_level")) {
1637 char *error = str_to_u8(value, name, &band->prec_level);
1642 return xasprintf("unknown keyword %s", name);
1645 /* validate bands */
1647 return xstrdup("meter must have bands");
1650 mm->meter.n_bands = n_bands;
1651 mm->meter.bands = ofpbuf_steal_data(bands);
1653 for (i = 0; i < n_bands; ++i) {
1654 band = &mm->meter.bands[i];
1657 return xstrdup("band must have 'type'");
1659 if (band->type == OFPMBT13_DSCP_REMARK) {
1660 if (!band->prec_level) {
1661 return xstrdup("'dscp_remark' band must have"
1665 if (band->prec_level) {
1666 return xstrdup("Only 'dscp_remark' band may have"
1671 return xstrdup("band must have 'rate'");
1673 if (mm->meter.flags & OFPMF13_BURST) {
1674 if (!band->burst_size) {
1675 return xstrdup("band must have 'burst_size' "
1676 "when 'burst' flag is set");
1679 if (band->burst_size) {
1680 return xstrdup("band may have 'burst_size' only "
1681 "when 'burst' flag is set");
1686 mm->meter.n_bands = 0;
1687 mm->meter.bands = NULL;
1693 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
1694 * page) into 'mm' for sending the specified meter_mod 'command' to a switch.
1696 * Returns NULL if successful, otherwise a malloc()'d string describing the
1697 * error. The caller is responsible for freeing the returned string. */
1698 char * WARN_UNUSED_RESULT
1699 parse_ofp_meter_mod_str(struct ofputil_meter_mod *mm, const char *str_,
1700 int command, enum ofputil_protocol *usable_protocols)
1702 struct ofpbuf bands;
1706 ofpbuf_init(&bands, 64);
1707 string = xstrdup(str_);
1709 error = parse_ofp_meter_mod_str__(mm, string, &bands, command,
1713 ofpbuf_uninit(&bands);
1718 static char * WARN_UNUSED_RESULT
1719 parse_flow_monitor_request__(struct ofputil_flow_monitor_request *fmr,
1720 const char *str_, char *string,
1721 enum ofputil_protocol *usable_protocols)
1723 static atomic_uint32_t id = ATOMIC_VAR_INIT(0);
1724 char *save_ptr = NULL;
1727 atomic_add(&id, 1, &fmr->id);
1729 fmr->flags = (NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY
1730 | NXFMF_OWN | NXFMF_ACTIONS);
1731 fmr->out_port = OFPP_NONE;
1732 fmr->table_id = 0xff;
1733 match_init_catchall(&fmr->match);
1735 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
1736 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
1737 const struct protocol *p;
1739 if (!strcmp(name, "!initial")) {
1740 fmr->flags &= ~NXFMF_INITIAL;
1741 } else if (!strcmp(name, "!add")) {
1742 fmr->flags &= ~NXFMF_ADD;
1743 } else if (!strcmp(name, "!delete")) {
1744 fmr->flags &= ~NXFMF_DELETE;
1745 } else if (!strcmp(name, "!modify")) {
1746 fmr->flags &= ~NXFMF_MODIFY;
1747 } else if (!strcmp(name, "!actions")) {
1748 fmr->flags &= ~NXFMF_ACTIONS;
1749 } else if (!strcmp(name, "!own")) {
1750 fmr->flags &= ~NXFMF_OWN;
1751 } else if (parse_protocol(name, &p)) {
1752 match_set_dl_type(&fmr->match, htons(p->dl_type));
1754 match_set_nw_proto(&fmr->match, p->nw_proto);
1759 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
1761 return xasprintf("%s: field %s missing value", str_, name);
1764 if (!strcmp(name, "table")) {
1765 char *error = str_to_u8(value, "table", &fmr->table_id);
1769 } else if (!strcmp(name, "out_port")) {
1770 fmr->out_port = u16_to_ofp(atoi(value));
1771 } else if (mf_from_name(name)) {
1774 error = parse_field(mf_from_name(name), value, &fmr->match,
1780 return xasprintf("%s: unknown keyword %s", str_, name);
1787 /* Convert 'str_' (as described in the documentation for the "monitor" command
1788 * in the ovs-ofctl man page) into 'fmr'.
1790 * Returns NULL if successful, otherwise a malloc()'d string describing the
1791 * error. The caller is responsible for freeing the returned string. */
1792 char * WARN_UNUSED_RESULT
1793 parse_flow_monitor_request(struct ofputil_flow_monitor_request *fmr,
1795 enum ofputil_protocol *usable_protocols)
1797 char *string = xstrdup(str_);
1798 char *error = parse_flow_monitor_request__(fmr, str_, string,
1804 /* Parses 's' as a set of OpenFlow actions and appends the actions to
1807 * Returns NULL if successful, otherwise a malloc()'d string describing the
1808 * error. The caller is responsible for freeing the returned string. */
1809 char * WARN_UNUSED_RESULT
1810 parse_ofpacts(const char *s_, struct ofpbuf *ofpacts,
1811 enum ofputil_protocol *usable_protocols)
1813 char *s = xstrdup(s_);
1816 *usable_protocols = OFPUTIL_P_ANY;
1818 error = str_to_ofpacts(s, ofpacts, usable_protocols);
1824 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
1825 * (one of OFPFC_*) into 'fm'.
1827 * Returns NULL if successful, otherwise a malloc()'d string describing the
1828 * error. The caller is responsible for freeing the returned string. */
1829 char * WARN_UNUSED_RESULT
1830 parse_ofp_flow_mod_str(struct ofputil_flow_mod *fm, const char *string,
1832 enum ofputil_protocol *usable_protocols)
1834 char *error = parse_ofp_str(fm, command, string, usable_protocols);
1836 /* Normalize a copy of the match. This ensures that non-normalized
1837 * flows get logged but doesn't affect what gets sent to the switch, so
1838 * that the switch can do whatever it likes with the flow. */
1839 struct match match_copy = fm->match;
1840 ofputil_normalize_match(&match_copy);
1846 /* Convert 'table_id' and 'flow_miss_handling' (as described for the
1847 * "mod-table" command in the ovs-ofctl man page) into 'tm' for sending the
1848 * specified table_mod 'command' to a switch.
1850 * Returns NULL if successful, otherwise a malloc()'d string describing the
1851 * error. The caller is responsible for freeing the returned string. */
1852 char * WARN_UNUSED_RESULT
1853 parse_ofp_table_mod(struct ofputil_table_mod *tm, const char *table_id,
1854 const char *flow_miss_handling,
1855 enum ofputil_protocol *usable_protocols)
1857 /* Table mod requires at least OF 1.1. */
1858 *usable_protocols = OFPUTIL_P_OF11_UP;
1860 if (!strcasecmp(table_id, "all")) {
1861 tm->table_id = OFPTT_ALL;
1863 char *error = str_to_u8(table_id, "table_id", &tm->table_id);
1869 if (strcmp(flow_miss_handling, "controller") == 0) {
1870 tm->miss_config = OFPUTIL_TABLE_MISS_CONTROLLER;
1871 } else if (strcmp(flow_miss_handling, "continue") == 0) {
1872 tm->miss_config = OFPUTIL_TABLE_MISS_CONTINUE;
1873 } else if (strcmp(flow_miss_handling, "drop") == 0) {
1874 tm->miss_config = OFPUTIL_TABLE_MISS_DROP;
1876 return xasprintf("invalid flow_miss_handling %s", flow_miss_handling);
1879 if (tm->table_id == 0xfe
1880 && tm->miss_config == OFPUTIL_TABLE_MISS_CONTINUE) {
1881 return xstrdup("last table's flow miss handling can not be continue");
1888 /* Opens file 'file_name' and reads each line as a flow_mod of the specified
1889 * type (one of OFPFC_*). Stores each flow_mod in '*fm', an array allocated
1890 * on the caller's behalf, and the number of flow_mods in '*n_fms'.
1892 * Returns NULL if successful, otherwise a malloc()'d string describing the
1893 * error. The caller is responsible for freeing the returned string. */
1894 char * WARN_UNUSED_RESULT
1895 parse_ofp_flow_mod_file(const char *file_name, uint16_t command,
1896 struct ofputil_flow_mod **fms, size_t *n_fms,
1897 enum ofputil_protocol *usable_protocols)
1899 size_t allocated_fms;
1904 *usable_protocols = OFPUTIL_P_ANY;
1909 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
1910 if (stream == NULL) {
1911 return xasprintf("%s: open failed (%s)",
1912 file_name, ovs_strerror(errno));
1915 allocated_fms = *n_fms;
1918 while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
1920 enum ofputil_protocol usable;
1922 if (*n_fms >= allocated_fms) {
1923 *fms = x2nrealloc(*fms, &allocated_fms, sizeof **fms);
1925 error = parse_ofp_flow_mod_str(&(*fms)[*n_fms], ds_cstr(&s), command,
1930 for (i = 0; i < *n_fms; i++) {
1931 free(CONST_CAST(struct ofpact *, (*fms)[i].ofpacts));
1938 if (stream != stdin) {
1942 return xasprintf("%s:%d: %s", file_name, line_number, error);
1944 *usable_protocols &= usable; /* Each line can narrow the set. */
1949 if (stream != stdin) {
1955 char * WARN_UNUSED_RESULT
1956 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request *fsr,
1957 bool aggregate, const char *string,
1958 enum ofputil_protocol *usable_protocols)
1960 struct ofputil_flow_mod fm;
1963 error = parse_ofp_str(&fm, -1, string, usable_protocols);
1968 /* Special table ID support not required for stats requests. */
1969 if (*usable_protocols & OFPUTIL_P_OF10_STD_TID) {
1970 *usable_protocols |= OFPUTIL_P_OF10_STD;
1972 if (*usable_protocols & OFPUTIL_P_OF10_NXM_TID) {
1973 *usable_protocols |= OFPUTIL_P_OF10_NXM;
1976 fsr->aggregate = aggregate;
1977 fsr->cookie = fm.cookie;
1978 fsr->cookie_mask = fm.cookie_mask;
1979 fsr->match = fm.match;
1980 fsr->out_port = fm.out_port;
1981 fsr->out_group = fm.out_group;
1982 fsr->table_id = fm.table_id;
1986 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
1987 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
1988 * mf_field. Fields must be specified in a natural order for satisfying
1989 * prerequisites. If 'mask' is specified, fills the mask field for each of the
1990 * field specified in flow. If the map, 'names_portno' is specfied, converts
1991 * the in_port name into port no while setting the 'flow'.
1993 * Returns NULL on success, otherwise a malloc()'d string that explains the
1996 parse_ofp_exact_flow(struct flow *flow, struct flow *mask, const char *s,
1997 const struct simap *portno_names)
1999 char *pos, *key, *value_s;
2003 memset(flow, 0, sizeof *flow);
2005 memset(mask, 0, sizeof *mask);
2008 pos = copy = xstrdup(s);
2009 while (ofputil_parse_key_value(&pos, &key, &value_s)) {
2010 const struct protocol *p;
2011 if (parse_protocol(key, &p)) {
2012 if (flow->dl_type) {
2013 error = xasprintf("%s: Ethernet type set multiple times", s);
2016 flow->dl_type = htons(p->dl_type);
2018 mask->dl_type = OVS_BE16_MAX;
2022 if (flow->nw_proto) {
2023 error = xasprintf("%s: network protocol set "
2024 "multiple times", s);
2027 flow->nw_proto = p->nw_proto;
2029 mask->nw_proto = UINT8_MAX;
2033 const struct mf_field *mf;
2034 union mf_value value;
2037 mf = mf_from_name(key);
2039 error = xasprintf("%s: unknown field %s", s, key);
2043 if (!mf_are_prereqs_ok(mf, flow)) {
2044 error = xasprintf("%s: prerequisites not met for setting %s",
2049 if (!mf_is_zero(mf, flow)) {
2050 error = xasprintf("%s: field %s set multiple times", s, key);
2054 if (!strcmp(key, "in_port")
2056 && simap_contains(portno_names, value_s)) {
2057 flow->in_port.ofp_port = u16_to_ofp(
2058 simap_get(portno_names, value_s));
2060 mask->in_port.ofp_port = u16_to_ofp(ntohs(OVS_BE16_MAX));
2063 field_error = mf_parse_value(mf, value_s, &value);
2065 error = xasprintf("%s: bad value for %s (%s)",
2066 s, key, field_error);
2071 mf_set_flow_value(mf, &value, flow);
2073 mf_mask_field(mf, mask);
2079 if (!flow->in_port.ofp_port) {
2080 flow->in_port.ofp_port = OFPP_NONE;
2087 memset(flow, 0, sizeof *flow);
2089 memset(mask, 0, sizeof *mask);
2095 static char * WARN_UNUSED_RESULT
2096 parse_bucket_str(struct ofputil_bucket *bucket, char *str_,
2097 enum ofputil_protocol *usable_protocols)
2099 struct ofpbuf ofpacts;
2100 char *pos, *act, *arg;
2104 bucket->watch_port = OFPP_ANY;
2105 bucket->watch_group = OFPG11_ANY;
2109 ofpbuf_init(&ofpacts, 64);
2110 while (ofputil_parse_key_value(&pos, &act, &arg)) {
2113 if (!strcasecmp(act, "weight")) {
2114 error = str_to_u16(arg, "weight", &bucket->weight);
2115 } else if (!strcasecmp(act, "watch_port")) {
2116 if (!ofputil_port_from_string(arg, &bucket->watch_port)
2117 || (ofp_to_u16(bucket->watch_port) >= ofp_to_u16(OFPP_MAX)
2118 && bucket->watch_port != OFPP_ANY)) {
2119 error = xasprintf("%s: invalid watch_port", arg);
2121 } else if (!strcasecmp(act, "watch_group")) {
2122 error = str_to_u32(arg, &bucket->watch_group);
2123 if (!error && bucket->watch_group > OFPG_MAX) {
2124 error = xasprintf("invalid watch_group id %"PRIu32,
2125 bucket->watch_group);
2127 } else if (!strcasecmp(act, "actions")) {
2128 if (ofputil_parse_key_value(&arg, &act, &arg)) {
2129 error = str_to_ofpact__(pos, act, arg, &ofpacts, n_actions,
2134 error = str_to_ofpact__(pos, act, arg, &ofpacts, n_actions,
2140 ofpbuf_uninit(&ofpacts);
2145 ofpact_pad(&ofpacts);
2146 bucket->ofpacts = ofpbuf_data(&ofpacts);
2147 bucket->ofpacts_len = ofpbuf_size(&ofpacts);
2152 static char * WARN_UNUSED_RESULT
2153 parse_ofp_group_mod_str__(struct ofputil_group_mod *gm, uint16_t command,
2155 enum ofputil_protocol *usable_protocols)
2158 F_GROUP_TYPE = 1 << 0,
2161 char *save_ptr = NULL;
2162 bool had_type = false;
2164 struct ofputil_bucket *bucket;
2167 *usable_protocols = OFPUTIL_P_OF11_UP;
2171 fields = F_GROUP_TYPE | F_BUCKETS;
2174 case OFPGC11_DELETE:
2178 case OFPGC11_MODIFY:
2179 fields = F_GROUP_TYPE | F_BUCKETS;
2186 memset(gm, 0, sizeof *gm);
2187 gm->command = command;
2188 gm->group_id = OFPG_ANY;
2189 list_init(&gm->buckets);
2190 if (command == OFPGC11_DELETE && string[0] == '\0') {
2191 gm->group_id = OFPG_ALL;
2195 *usable_protocols = OFPUTIL_P_OF11_UP;
2197 if (fields & F_BUCKETS) {
2198 char *bkt_str = strstr(string, "bucket");
2207 bkt_str = strchr(bkt_str + 1, '=');
2209 error = xstrdup("must specify bucket content");
2214 next_bkt_str = strstr(bkt_str, "bucket");
2216 *next_bkt_str = '\0';
2219 bucket = xzalloc(sizeof(struct ofputil_bucket));
2220 error = parse_bucket_str(bucket, bkt_str, usable_protocols);
2225 list_push_back(&gm->buckets, &bucket->list_node);
2227 bkt_str = next_bkt_str;
2231 for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
2232 name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
2235 value = strtok_r(NULL, ", \t\r\n", &save_ptr);
2237 error = xasprintf("field %s missing value", name);
2241 if (!strcmp(name, "group_id")) {
2242 if(!strcmp(value, "all")) {
2243 gm->group_id = OFPG_ALL;
2245 char *error = str_to_u32(value, &gm->group_id);
2249 if (gm->group_id != OFPG_ALL && gm->group_id > OFPG_MAX) {
2250 error = xasprintf("invalid group id %"PRIu32,
2255 } else if (!strcmp(name, "type")){
2256 if (!(fields & F_GROUP_TYPE)) {
2257 error = xstrdup("type is not needed");
2260 if (!strcmp(value, "all")) {
2261 gm->type = OFPGT11_ALL;
2262 } else if (!strcmp(value, "select")) {
2263 gm->type = OFPGT11_SELECT;
2264 } else if (!strcmp(value, "indirect")) {
2265 gm->type = OFPGT11_INDIRECT;
2266 } else if (!strcmp(value, "ff") ||
2267 !strcmp(value, "fast_failover")) {
2268 gm->type = OFPGT11_FF;
2270 error = xasprintf("invalid group type %s", value);
2274 } else if (!strcmp(name, "bucket")) {
2275 error = xstrdup("bucket is not needed");
2278 error = xasprintf("unknown keyword %s", name);
2282 if (gm->group_id == OFPG_ANY) {
2283 error = xstrdup("must specify a group_id");
2286 if (fields & F_GROUP_TYPE && !had_type) {
2287 error = xstrdup("must specify a type");
2291 /* Validate buckets. */
2292 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
2293 if (bucket->weight != 1 && gm->type != OFPGT11_SELECT) {
2294 error = xstrdup("Only select groups can have bucket weights.");
2298 if (gm->type == OFPGT11_INDIRECT && !list_is_short(&gm->buckets)) {
2299 error = xstrdup("Indirect groups can have at most one bucket.");
2305 ofputil_bucket_list_destroy(&gm->buckets);
2309 char * WARN_UNUSED_RESULT
2310 parse_ofp_group_mod_str(struct ofputil_group_mod *gm, uint16_t command,
2312 enum ofputil_protocol *usable_protocols)
2314 char *string = xstrdup(str_);
2315 char *error = parse_ofp_group_mod_str__(gm, command, string,
2320 ofputil_bucket_list_destroy(&gm->buckets);
2325 char * WARN_UNUSED_RESULT
2326 parse_ofp_group_mod_file(const char *file_name, uint16_t command,
2327 struct ofputil_group_mod **gms, size_t *n_gms,
2328 enum ofputil_protocol *usable_protocols)
2330 size_t allocated_gms;
2338 stream = !strcmp(file_name, "-") ? stdin : fopen(file_name, "r");
2339 if (stream == NULL) {
2340 return xasprintf("%s: open failed (%s)",
2341 file_name, ovs_strerror(errno));
2344 allocated_gms = *n_gms;
2347 *usable_protocols = OFPUTIL_P_OF11_UP;
2348 while (!ds_get_preprocessed_line(&s, stream, &line_number)) {
2349 enum ofputil_protocol usable;
2352 if (*n_gms >= allocated_gms) {
2355 *gms = x2nrealloc(*gms, &allocated_gms, sizeof **gms);
2356 for (i = 0; i < *n_gms; i++) {
2357 list_moved(&(*gms)[i].buckets);
2360 error = parse_ofp_group_mod_str(&(*gms)[*n_gms], command, ds_cstr(&s),
2365 for (i = 0; i < *n_gms; i++) {
2366 ofputil_bucket_list_destroy(&(*gms)[i].buckets);
2373 if (stream != stdin) {
2377 return xasprintf("%s:%d: %s", file_name, line_number, error);
2379 *usable_protocols &= usable;
2384 if (stream != stdin) {