2 * Copyright (c) 2010, 2011, 2012, 2013 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 static 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 static 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 static 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 static 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 static 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 static char * WARN_UNUSED_RESULT
148 str_to_mac(const char *str
, uint8_t mac
[6])
150 if (sscanf(str
, ETH_ADDR_SCAN_FMT
, ETH_ADDR_SCAN_ARGS(mac
))
151 != ETH_ADDR_SCAN_COUNT
) {
152 return xasprintf("invalid mac address %s", str
);
157 /* Parses 'str' as an IP address into '*ip'.
159 * Returns NULL if successful, otherwise a malloc()'d string describing the
160 * error. The caller is responsible for freeing the returned string. */
161 static char * WARN_UNUSED_RESULT
162 str_to_ip(const char *str
, ovs_be32
*ip
)
164 struct in_addr in_addr
;
166 if (lookup_ip(str
, &in_addr
)) {
167 return xasprintf("%s: could not convert to IP address", str
);
169 *ip
= in_addr
.s_addr
;
173 /* Parses 'arg' as the argument to an "enqueue" action, and appends such an
174 * action to 'ofpacts'.
176 * Returns NULL if successful, otherwise a malloc()'d string describing the
177 * error. The caller is responsible for freeing the returned string. */
178 static char * WARN_UNUSED_RESULT
179 parse_enqueue(char *arg
, struct ofpbuf
*ofpacts
)
182 char *port
= strtok_r(arg
, ":q", &sp
);
183 char *queue
= strtok_r(NULL
, "", &sp
);
184 struct ofpact_enqueue
*enqueue
;
186 if (port
== NULL
|| queue
== NULL
) {
187 return xstrdup("\"enqueue\" syntax is \"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 output
->max_len
= output
->port
== OFPP_CONTROLLER
? UINT16_MAX
: 0;
216 if (!ofputil_port_from_string(arg
, &output
->port
)) {
217 return xasprintf("%s: output to unknown port", arg
);
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);
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 ids
= 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_ttl" action, and appends such an
442 * action to 'ofpacts'.
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_ttl(struct ofpbuf
*b
, const char *arg
)
449 struct ofpact_mpls_ttl
*mpls_ttl
= ofpact_put_SET_MPLS_TTL(b
);
452 return xstrdup("parse_set_mpls_ttl: expected ttl.");
455 mpls_ttl
->ttl
= atoi(arg
);
459 /* Parses a "set_field" action with argument 'arg', appending the parsed
460 * action to 'ofpacts'.
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 set_field_parse__(char *arg
, struct ofpbuf
*ofpacts
,
466 enum ofputil_protocol
*usable_protocols
)
468 struct ofpact_reg_load
*load
= ofpact_put_REG_LOAD(ofpacts
);
472 const struct mf_field
*mf
;
474 union mf_value mf_value
;
477 delim
= strstr(arg
, "->");
479 return xasprintf("%s: missing `->'", arg
);
481 if (strlen(delim
) <= strlen("->")) {
482 return xasprintf("%s: missing field name following `->'", arg
);
485 key
= delim
+ strlen("->");
486 mf
= mf_from_name(key
);
488 return xasprintf("%s is not a valid OXM field name", key
);
491 return xasprintf("%s is read-only", key
);
495 error
= mf_parse_value(mf
, value
, &mf_value
);
499 if (!mf_is_value_valid(mf
, &mf_value
)) {
500 return xasprintf("%s is not a valid value for field %s", value
, key
);
502 ofpact_set_field_init(load
, mf
, &mf_value
);
504 *usable_protocols
&= mf
->usable_protocols
;
508 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
509 * action to 'ofpacts'.
511 * Returns NULL if successful, otherwise a malloc()'d string describing the
512 * error. The caller is responsible for freeing the returned string. */
513 static char * WARN_UNUSED_RESULT
514 set_field_parse(const char *arg
, struct ofpbuf
*ofpacts
,
515 enum ofputil_protocol
*usable_protocols
)
517 char *copy
= xstrdup(arg
);
518 char *error
= set_field_parse__(copy
, ofpacts
, usable_protocols
);
523 /* Parses 'arg' as the argument to a "write_metadata" instruction, and appends
524 * such an action to 'ofpacts'.
526 * Returns NULL if successful, otherwise a malloc()'d string describing the
527 * error. The caller is responsible for freeing the returned string. */
528 static char * WARN_UNUSED_RESULT
529 parse_metadata(struct ofpbuf
*b
, char *arg
)
531 struct ofpact_metadata
*om
;
532 char *mask
= strchr(arg
, '/');
534 om
= ofpact_put_WRITE_METADATA(b
);
540 error
= str_to_be64(mask
+ 1, &om
->mask
);
545 om
->mask
= OVS_BE64_MAX
;
548 return str_to_be64(arg
, &om
->metadata
);
551 /* Parses 'arg' as the argument to a "sample" action, and appends such an
552 * action to 'ofpacts'.
554 * Returns NULL if successful, otherwise a malloc()'d string describing the
555 * error. The caller is responsible for freeing the returned string. */
556 static char * WARN_UNUSED_RESULT
557 parse_sample(struct ofpbuf
*b
, char *arg
)
559 struct ofpact_sample
*os
= ofpact_put_SAMPLE(b
);
562 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
565 if (!strcmp(key
, "probability")) {
566 error
= str_to_u16(value
, "probability", &os
->probability
);
567 if (!error
&& os
->probability
== 0) {
568 error
= xasprintf("invalid probability value \"%s\"", value
);
570 } else if (!strcmp(key
, "collector_set_id")) {
571 error
= str_to_u32(value
, &os
->collector_set_id
);
572 } else if (!strcmp(key
, "obs_domain_id")) {
573 error
= str_to_u32(value
, &os
->obs_domain_id
);
574 } else if (!strcmp(key
, "obs_point_id")) {
575 error
= str_to_u32(value
, &os
->obs_point_id
);
577 error
= xasprintf("invalid key \"%s\" in \"sample\" argument",
584 if (os
->probability
== 0) {
585 return xstrdup("non-zero \"probability\" must be specified on sample");
590 /* Parses 'arg' as the argument to action 'code', and appends such an action to
593 * Returns NULL if successful, otherwise a malloc()'d string describing the
594 * error. The caller is responsible for freeing the returned string. */
595 static char * WARN_UNUSED_RESULT
596 parse_named_action(enum ofputil_action_code code
,
597 char *arg
, struct ofpbuf
*ofpacts
,
598 enum ofputil_protocol
*usable_protocols
)
600 size_t orig_size
= ofpacts
->size
;
601 struct ofpact_tunnel
*tunnel
;
603 uint16_t ethertype
= 0;
605 uint8_t tos
= 0, ecn
, ttl
;
609 case OFPUTIL_ACTION_INVALID
:
612 case OFPUTIL_OFPAT10_OUTPUT
:
613 case OFPUTIL_OFPAT11_OUTPUT
:
614 error
= parse_output(arg
, ofpacts
);
617 case OFPUTIL_OFPAT10_SET_VLAN_VID
:
618 case OFPUTIL_OFPAT11_SET_VLAN_VID
:
619 error
= str_to_u16(arg
, "VLAN VID", &vid
);
624 if (vid
& ~VLAN_VID_MASK
) {
625 return xasprintf("%s: not a valid VLAN VID", arg
);
627 ofpact_put_SET_VLAN_VID(ofpacts
)->vlan_vid
= vid
;
630 case OFPUTIL_OFPAT10_SET_VLAN_PCP
:
631 case OFPUTIL_OFPAT11_SET_VLAN_PCP
:
632 error
= str_to_u8(arg
, "VLAN PCP", &pcp
);
638 return xasprintf("%s: not a valid VLAN PCP", arg
);
640 ofpact_put_SET_VLAN_PCP(ofpacts
)->vlan_pcp
= pcp
;
643 case OFPUTIL_OFPAT12_SET_FIELD
:
644 return set_field_parse(arg
, ofpacts
, usable_protocols
);
646 case OFPUTIL_OFPAT10_STRIP_VLAN
:
647 case OFPUTIL_OFPAT11_POP_VLAN
:
648 ofpact_put_STRIP_VLAN(ofpacts
);
651 case OFPUTIL_OFPAT11_PUSH_VLAN
:
652 *usable_protocols
&= OFPUTIL_P_OF11_UP
;
653 error
= str_to_u16(arg
, "ethertype", ðertype
);
658 if (ethertype
!= ETH_TYPE_VLAN_8021Q
) {
659 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
660 return xasprintf("%s: not a valid VLAN ethertype", arg
);
663 ofpact_put_PUSH_VLAN(ofpacts
);
666 case OFPUTIL_OFPAT11_SET_QUEUE
:
667 error
= str_to_u32(arg
, &ofpact_put_SET_QUEUE(ofpacts
)->queue_id
);
670 case OFPUTIL_OFPAT10_SET_DL_SRC
:
671 case OFPUTIL_OFPAT11_SET_DL_SRC
:
672 error
= str_to_mac(arg
, ofpact_put_SET_ETH_SRC(ofpacts
)->mac
);
675 case OFPUTIL_OFPAT10_SET_DL_DST
:
676 case OFPUTIL_OFPAT11_SET_DL_DST
:
677 error
= str_to_mac(arg
, ofpact_put_SET_ETH_DST(ofpacts
)->mac
);
680 case OFPUTIL_OFPAT10_SET_NW_SRC
:
681 case OFPUTIL_OFPAT11_SET_NW_SRC
:
682 error
= str_to_ip(arg
, &ofpact_put_SET_IPV4_SRC(ofpacts
)->ipv4
);
685 case OFPUTIL_OFPAT10_SET_NW_DST
:
686 case OFPUTIL_OFPAT11_SET_NW_DST
:
687 error
= str_to_ip(arg
, &ofpact_put_SET_IPV4_DST(ofpacts
)->ipv4
);
690 case OFPUTIL_OFPAT10_SET_NW_TOS
:
691 case OFPUTIL_OFPAT11_SET_NW_TOS
:
692 error
= str_to_u8(arg
, "TOS", &tos
);
697 if (tos
& ~IP_DSCP_MASK
) {
698 return xasprintf("%s: not a valid TOS", arg
);
700 ofpact_put_SET_IP_DSCP(ofpacts
)->dscp
= tos
;
703 case OFPUTIL_OFPAT11_SET_NW_ECN
:
704 error
= str_to_u8(arg
, "ECN", &ecn
);
709 if (ecn
& ~IP_ECN_MASK
) {
710 return xasprintf("%s: not a valid ECN", arg
);
712 ofpact_put_SET_IP_ECN(ofpacts
)->ecn
= ecn
;
715 case OFPUTIL_OFPAT11_SET_NW_TTL
:
716 error
= str_to_u8(arg
, "TTL", &ttl
);
721 ofpact_put_SET_IP_TTL(ofpacts
)->ttl
= ttl
;
724 case OFPUTIL_OFPAT11_DEC_NW_TTL
:
727 case OFPUTIL_OFPAT10_SET_TP_SRC
:
728 case OFPUTIL_OFPAT11_SET_TP_SRC
:
729 error
= str_to_u16(arg
, "source port",
730 &ofpact_put_SET_L4_SRC_PORT(ofpacts
)->port
);
733 case OFPUTIL_OFPAT10_SET_TP_DST
:
734 case OFPUTIL_OFPAT11_SET_TP_DST
:
735 error
= str_to_u16(arg
, "destination port",
736 &ofpact_put_SET_L4_DST_PORT(ofpacts
)->port
);
739 case OFPUTIL_OFPAT10_ENQUEUE
:
740 error
= parse_enqueue(arg
, ofpacts
);
743 case OFPUTIL_NXAST_RESUBMIT
:
744 error
= parse_resubmit(arg
, ofpacts
);
747 case OFPUTIL_NXAST_SET_TUNNEL
:
748 case OFPUTIL_NXAST_SET_TUNNEL64
:
749 tunnel
= ofpact_put_SET_TUNNEL(ofpacts
);
750 tunnel
->ofpact
.compat
= code
;
751 error
= str_to_u64(arg
, &tunnel
->tun_id
);
754 case OFPUTIL_NXAST_WRITE_METADATA
:
755 error
= parse_metadata(ofpacts
, arg
);
758 case OFPUTIL_NXAST_SET_QUEUE
:
759 error
= str_to_u32(arg
, &ofpact_put_SET_QUEUE(ofpacts
)->queue_id
);
762 case OFPUTIL_NXAST_POP_QUEUE
:
763 ofpact_put_POP_QUEUE(ofpacts
);
766 case OFPUTIL_NXAST_REG_MOVE
:
767 error
= nxm_parse_reg_move(ofpact_put_REG_MOVE(ofpacts
), arg
);
770 case OFPUTIL_NXAST_REG_LOAD
:
771 error
= nxm_parse_reg_load(ofpact_put_REG_LOAD(ofpacts
), arg
);
774 case OFPUTIL_NXAST_NOTE
:
775 error
= parse_note(arg
, ofpacts
);
778 case OFPUTIL_NXAST_MULTIPATH
:
779 error
= multipath_parse(ofpact_put_MULTIPATH(ofpacts
), arg
);
782 case OFPUTIL_NXAST_BUNDLE
:
783 error
= bundle_parse(arg
, ofpacts
);
786 case OFPUTIL_NXAST_BUNDLE_LOAD
:
787 error
= bundle_parse_load(arg
, ofpacts
);
790 case OFPUTIL_NXAST_RESUBMIT_TABLE
:
791 case OFPUTIL_NXAST_OUTPUT_REG
:
792 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS
:
795 case OFPUTIL_NXAST_LEARN
:
796 error
= learn_parse(arg
, ofpacts
);
799 case OFPUTIL_NXAST_EXIT
:
800 ofpact_put_EXIT(ofpacts
);
803 case OFPUTIL_NXAST_DEC_TTL
:
804 error
= parse_dec_ttl(ofpacts
, arg
);
807 case OFPUTIL_NXAST_SET_MPLS_TTL
:
808 case OFPUTIL_OFPAT11_SET_MPLS_TTL
:
809 error
= parse_set_mpls_ttl(ofpacts
, arg
);
812 case OFPUTIL_OFPAT11_DEC_MPLS_TTL
:
813 case OFPUTIL_NXAST_DEC_MPLS_TTL
:
814 ofpact_put_DEC_MPLS_TTL(ofpacts
);
817 case OFPUTIL_NXAST_FIN_TIMEOUT
:
818 error
= parse_fin_timeout(ofpacts
, arg
);
821 case OFPUTIL_NXAST_CONTROLLER
:
822 error
= parse_controller(ofpacts
, arg
);
825 case OFPUTIL_OFPAT11_PUSH_MPLS
:
826 case OFPUTIL_NXAST_PUSH_MPLS
:
827 error
= str_to_u16(arg
, "push_mpls", ðertype
);
829 ofpact_put_PUSH_MPLS(ofpacts
)->ethertype
= htons(ethertype
);
833 case OFPUTIL_OFPAT11_POP_MPLS
:
834 case OFPUTIL_NXAST_POP_MPLS
:
835 error
= str_to_u16(arg
, "pop_mpls", ðertype
);
837 ofpact_put_POP_MPLS(ofpacts
)->ethertype
= htons(ethertype
);
841 case OFPUTIL_OFPAT11_GROUP
:
842 error
= str_to_u32(arg
, &ofpact_put_GROUP(ofpacts
)->group_id
);
845 case OFPUTIL_NXAST_STACK_PUSH
:
846 error
= nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts
), arg
);
848 case OFPUTIL_NXAST_STACK_POP
:
849 error
= nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts
), arg
);
852 case OFPUTIL_NXAST_SAMPLE
:
853 error
= parse_sample(ofpacts
, arg
);
858 ofpacts
->size
= orig_size
;
863 /* Parses action 'act', with argument 'arg', and appends a parsed version to
866 * 'n_actions' specifies the number of actions already parsed (for proper
867 * handling of "drop" actions).
869 * Returns NULL if successful, otherwise a malloc()'d string describing the
870 * error. The caller is responsible for freeing the returned string. */
871 static char * WARN_UNUSED_RESULT
872 str_to_ofpact__(char *pos
, char *act
, char *arg
,
873 struct ofpbuf
*ofpacts
, int n_actions
,
874 enum ofputil_protocol
*usable_protocols
)
876 int code
= ofputil_action_code_from_name(act
);
878 return parse_named_action(code
, arg
, ofpacts
, usable_protocols
);
879 } else if (!strcasecmp(act
, "drop")) {
881 return xstrdup("Drop actions must not be preceded by other "
883 } else if (ofputil_parse_key_value(&pos
, &act
, &arg
)) {
884 return xstrdup("Drop actions must not be followed by other "
889 if (ofputil_port_from_string(act
, &port
)) {
890 ofpact_put_OUTPUT(ofpacts
)->port
= port
;
892 return xasprintf("Unknown action: %s", act
);
899 /* Parses 'str' as a series of actions, and appends them to 'ofpacts'.
901 * Returns NULL if successful, otherwise a malloc()'d string describing the
902 * error. The caller is responsible for freeing the returned string. */
903 static char * WARN_UNUSED_RESULT
904 str_to_ofpacts__(char *str
, struct ofpbuf
*ofpacts
,
905 enum ofputil_protocol
*usable_protocols
)
907 size_t orig_size
= ofpacts
->size
;
908 char *pos
, *act
, *arg
;
913 while (ofputil_parse_key_value(&pos
, &act
, &arg
)) {
914 char *error
= str_to_ofpact__(pos
, act
, arg
, ofpacts
, n_actions
,
917 ofpacts
->size
= orig_size
;
928 /* Parses 'str' as a series of actions, and appends them to 'ofpacts'.
930 * Returns NULL if successful, otherwise a malloc()'d string describing the
931 * error. The caller is responsible for freeing the returned string. */
932 static char * WARN_UNUSED_RESULT
933 str_to_ofpacts(char *str
, struct ofpbuf
*ofpacts
,
934 enum ofputil_protocol
*usable_protocols
)
936 size_t orig_size
= ofpacts
->size
;
940 error_s
= str_to_ofpacts__(str
, ofpacts
, usable_protocols
);
945 error
= ofpacts_verify(ofpacts
->data
, ofpacts
->size
);
947 ofpacts
->size
= orig_size
;
948 return xstrdup("Incorrect action ordering");
954 /* Parses 'arg' as the argument to instruction 'type', and appends such an
955 * instruction to 'ofpacts'.
957 * Returns NULL if successful, otherwise a malloc()'d string describing the
958 * error. The caller is responsible for freeing the returned string. */
959 static char * WARN_UNUSED_RESULT
960 parse_named_instruction(enum ovs_instruction_type type
,
961 char *arg
, struct ofpbuf
*ofpacts
,
962 enum ofputil_protocol
*usable_protocols
)
964 char *error_s
= NULL
;
967 *usable_protocols
&= OFPUTIL_P_OF11_UP
;
970 case OVSINST_OFPIT11_APPLY_ACTIONS
:
971 NOT_REACHED(); /* This case is handled by str_to_inst_ofpacts() */
974 case OVSINST_OFPIT11_WRITE_ACTIONS
: {
975 struct ofpact_nest
*on
;
980 on
= ofpact_put(ofpacts
, OFPACT_WRITE_ACTIONS
,
981 offsetof(struct ofpact_nest
, actions
));
982 error_s
= str_to_ofpacts__(arg
, ofpacts
, usable_protocols
);
984 on
= ofpbuf_at_assert(ofpacts
, ofs
, sizeof *on
);
985 on
->ofpact
.len
= ofpacts
->size
- ofs
;
993 case OVSINST_OFPIT11_CLEAR_ACTIONS
:
994 ofpact_put_CLEAR_ACTIONS(ofpacts
);
997 case OVSINST_OFPIT13_METER
:
998 *usable_protocols
&= OFPUTIL_P_OF13_UP
;
999 error_s
= str_to_u32(arg
, &ofpact_put_METER(ofpacts
)->meter_id
);
1002 case OVSINST_OFPIT11_WRITE_METADATA
:
1003 *usable_protocols
&= OFPUTIL_P_NXM_OF11_UP
;
1004 error_s
= parse_metadata(ofpacts
, arg
);
1007 case OVSINST_OFPIT11_GOTO_TABLE
: {
1008 struct ofpact_goto_table
*ogt
= ofpact_put_GOTO_TABLE(ofpacts
);
1009 char *table_s
= strsep(&arg
, ",");
1010 if (!table_s
|| !table_s
[0]) {
1011 return xstrdup("instruction goto-table needs table id");
1013 error_s
= str_to_u8(table_s
, "table", &ogt
->table_id
);
1022 /* If write_metadata is specified as an action AND an instruction, ofpacts
1023 could be invalid. */
1024 error
= ofpacts_verify(ofpacts
->data
, ofpacts
->size
);
1026 return xstrdup("Incorrect instruction ordering");
1031 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
1033 * Returns NULL if successful, otherwise a malloc()'d string describing the
1034 * error. The caller is responsible for freeing the returned string. */
1035 static char * WARN_UNUSED_RESULT
1036 str_to_inst_ofpacts(char *str
, struct ofpbuf
*ofpacts
,
1037 enum ofputil_protocol
*usable_protocols
)
1039 size_t orig_size
= ofpacts
->size
;
1040 char *pos
, *inst
, *arg
;
1042 const char *prev_inst
= NULL
;
1047 while (ofputil_parse_key_value(&pos
, &inst
, &arg
)) {
1048 type
= ovs_instruction_type_from_name(inst
);
1050 char *error
= str_to_ofpact__(pos
, inst
, arg
, ofpacts
, n_actions
,
1053 ofpacts
->size
= orig_size
;
1057 type
= OVSINST_OFPIT11_APPLY_ACTIONS
;
1058 if (prev_type
== type
) {
1062 } else if (type
== OVSINST_OFPIT11_APPLY_ACTIONS
) {
1063 ofpacts
->size
= orig_size
;
1064 return xasprintf("%s isn't supported. Just write actions then "
1065 "it is interpreted as apply_actions", inst
);
1067 char *error
= parse_named_instruction(type
, arg
, ofpacts
,
1070 ofpacts
->size
= orig_size
;
1075 if (type
<= prev_type
) {
1076 ofpacts
->size
= orig_size
;
1077 if (type
== prev_type
) {
1078 return xasprintf("instruction %s may be specified only once",
1081 return xasprintf("instruction %s must be specified before %s",
1090 ofpact_pad(ofpacts
);
1102 parse_protocol(const char *name
, const struct protocol
**p_out
)
1104 static const struct protocol protocols
[] = {
1105 { "ip", ETH_TYPE_IP
, 0 },
1106 { "arp", ETH_TYPE_ARP
, 0 },
1107 { "icmp", ETH_TYPE_IP
, IPPROTO_ICMP
},
1108 { "tcp", ETH_TYPE_IP
, IPPROTO_TCP
},
1109 { "udp", ETH_TYPE_IP
, IPPROTO_UDP
},
1110 { "sctp", ETH_TYPE_IP
, IPPROTO_SCTP
},
1111 { "ipv6", ETH_TYPE_IPV6
, 0 },
1112 { "ip6", ETH_TYPE_IPV6
, 0 },
1113 { "icmp6", ETH_TYPE_IPV6
, IPPROTO_ICMPV6
},
1114 { "tcp6", ETH_TYPE_IPV6
, IPPROTO_TCP
},
1115 { "udp6", ETH_TYPE_IPV6
, IPPROTO_UDP
},
1116 { "sctp6", ETH_TYPE_IPV6
, IPPROTO_SCTP
},
1117 { "rarp", ETH_TYPE_RARP
, 0},
1118 { "mpls", ETH_TYPE_MPLS
, 0 },
1119 { "mplsm", ETH_TYPE_MPLS_MCAST
, 0 },
1121 const struct protocol
*p
;
1123 for (p
= protocols
; p
< &protocols
[ARRAY_SIZE(protocols
)]; p
++) {
1124 if (!strcmp(p
->name
, name
)) {
1133 /* Parses 's' as the (possibly masked) value of field 'mf', and updates
1134 * 'match' appropriately. Restricts the set of usable protocols to ones
1135 * supporting the parsed field.
1137 * Returns NULL if successful, otherwise a malloc()'d string describing the
1138 * error. The caller is responsible for freeing the returned string. */
1139 static char * WARN_UNUSED_RESULT
1140 parse_field(const struct mf_field
*mf
, const char *s
, struct match
*match
,
1141 enum ofputil_protocol
*usable_protocols
)
1143 union mf_value value
, mask
;
1146 error
= mf_parse(mf
, s
, &value
, &mask
);
1148 *usable_protocols
&= mf_set(mf
, &value
, &mask
, match
);
1153 static char * WARN_UNUSED_RESULT
1154 parse_ofp_str__(struct ofputil_flow_mod
*fm
, int command
, char *string
,
1155 enum ofputil_protocol
*usable_protocols
,
1156 bool enforce_consistency
)
1159 F_OUT_PORT
= 1 << 0,
1162 F_PRIORITY
= 1 << 4,
1165 char *save_ptr
= NULL
;
1166 char *act_str
= NULL
;
1169 *usable_protocols
= OFPUTIL_P_ANY
;
1173 fields
= F_OUT_PORT
;
1177 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
;
1181 fields
= F_OUT_PORT
;
1184 case OFPFC_DELETE_STRICT
:
1185 fields
= F_OUT_PORT
| F_PRIORITY
;
1189 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
;
1192 case OFPFC_MODIFY_STRICT
:
1193 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
;
1200 match_init_catchall(&fm
->match
);
1201 fm
->priority
= OFP_DEFAULT_PRIORITY
;
1202 fm
->cookie
= htonll(0);
1203 fm
->cookie_mask
= htonll(0);
1204 if (command
== OFPFC_MODIFY
|| command
== OFPFC_MODIFY_STRICT
) {
1205 /* For modify, by default, don't update the cookie. */
1206 fm
->new_cookie
= OVS_BE64_MAX
;
1208 fm
->new_cookie
= htonll(0);
1210 fm
->modify_cookie
= false;
1211 fm
->table_id
= 0xff;
1212 fm
->command
= command
;
1213 fm
->idle_timeout
= OFP_FLOW_PERMANENT
;
1214 fm
->hard_timeout
= OFP_FLOW_PERMANENT
;
1215 fm
->buffer_id
= UINT32_MAX
;
1216 fm
->out_port
= OFPP_ANY
;
1218 fm
->out_group
= OFPG11_ANY
;
1219 if (fields
& F_ACTIONS
) {
1220 act_str
= strstr(string
, "action");
1222 return xstrdup("must specify an action");
1226 act_str
= strchr(act_str
+ 1, '=');
1228 return xstrdup("must specify an action");
1233 for (name
= strtok_r(string
, "=, \t\r\n", &save_ptr
); name
;
1234 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
1235 const struct protocol
*p
;
1238 if (parse_protocol(name
, &p
)) {
1239 match_set_dl_type(&fm
->match
, htons(p
->dl_type
));
1241 match_set_nw_proto(&fm
->match
, p
->nw_proto
);
1243 } else if (fields
& F_FLAGS
&& !strcmp(name
, "send_flow_rem")) {
1244 fm
->flags
|= OFPUTIL_FF_SEND_FLOW_REM
;
1245 } else if (fields
& F_FLAGS
&& !strcmp(name
, "check_overlap")) {
1246 fm
->flags
|= OFPUTIL_FF_CHECK_OVERLAP
;
1247 } else if (fields
& F_FLAGS
&& !strcmp(name
, "reset_counts")) {
1248 fm
->flags
|= OFPUTIL_FF_RESET_COUNTS
;
1249 *usable_protocols
&= OFPUTIL_P_OF12_UP
;
1250 } else if (fields
& F_FLAGS
&& !strcmp(name
, "no_packet_counts")) {
1251 fm
->flags
|= OFPUTIL_FF_NO_PKT_COUNTS
;
1252 *usable_protocols
&= OFPUTIL_P_OF13_UP
;
1253 } else if (fields
& F_FLAGS
&& !strcmp(name
, "no_byte_counts")) {
1254 fm
->flags
|= OFPUTIL_FF_NO_BYT_COUNTS
;
1255 *usable_protocols
&= OFPUTIL_P_OF13_UP
;
1259 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
1261 return xasprintf("field %s missing value", name
);
1264 if (!strcmp(name
, "table")) {
1265 error
= str_to_u8(value
, "table", &fm
->table_id
);
1266 if (fm
->table_id
!= 0xff) {
1267 *usable_protocols
&= OFPUTIL_P_TID
;
1269 } else if (!strcmp(name
, "out_port")) {
1270 if (!ofputil_port_from_string(value
, &fm
->out_port
)) {
1271 error
= xasprintf("%s is not a valid OpenFlow port",
1274 } else if (fields
& F_PRIORITY
&& !strcmp(name
, "priority")) {
1275 uint16_t priority
= 0;
1277 error
= str_to_u16(value
, name
, &priority
);
1278 fm
->priority
= priority
;
1279 } else if (fields
& F_TIMEOUT
&& !strcmp(name
, "idle_timeout")) {
1280 error
= str_to_u16(value
, name
, &fm
->idle_timeout
);
1281 } else if (fields
& F_TIMEOUT
&& !strcmp(name
, "hard_timeout")) {
1282 error
= str_to_u16(value
, name
, &fm
->hard_timeout
);
1283 } else if (!strcmp(name
, "cookie")) {
1284 char *mask
= strchr(value
, '/');
1287 /* A mask means we're searching for a cookie. */
1288 if (command
== OFPFC_ADD
) {
1289 return xstrdup("flow additions cannot use "
1293 error
= str_to_be64(value
, &fm
->cookie
);
1297 error
= str_to_be64(mask
+ 1, &fm
->cookie_mask
);
1299 /* Matching of the cookie is only supported through NXM or
1301 if (fm
->cookie_mask
!= htonll(0)) {
1302 *usable_protocols
&= OFPUTIL_P_NXM_OF11_UP
;
1305 /* No mask means that the cookie is being set. */
1306 if (command
!= OFPFC_ADD
&& command
!= OFPFC_MODIFY
1307 && command
!= OFPFC_MODIFY_STRICT
) {
1308 return xstrdup("cannot set cookie");
1310 error
= str_to_be64(value
, &fm
->new_cookie
);
1311 fm
->modify_cookie
= true;
1313 } else if (mf_from_name(name
)) {
1314 error
= parse_field(mf_from_name(name
), value
, &fm
->match
,
1316 } else if (!strcmp(name
, "duration")
1317 || !strcmp(name
, "n_packets")
1318 || !strcmp(name
, "n_bytes")
1319 || !strcmp(name
, "idle_age")
1320 || !strcmp(name
, "hard_age")) {
1321 /* Ignore these, so that users can feed the output of
1322 * "ovs-ofctl dump-flows" back into commands that parse
1325 error
= xasprintf("unknown keyword %s", name
);
1333 /* Check for usable protocol interdependencies between match fields. */
1334 if (fm
->match
.flow
.dl_type
== htons(ETH_TYPE_IPV6
)) {
1335 const struct flow_wildcards
*wc
= &fm
->match
.wc
;
1336 /* Only NXM and OXM support matching L3 and L4 fields within IPv6.
1338 * (IPv6 specific fields as well as arp_sha, arp_tha, nw_frag, and
1339 * nw_ttl are covered elsewhere so they don't need to be included in
1342 if (wc
->masks
.nw_proto
|| wc
->masks
.nw_tos
1343 || wc
->masks
.tp_src
|| wc
->masks
.tp_dst
) {
1344 *usable_protocols
&= OFPUTIL_P_NXM_OXM_ANY
;
1347 if (!fm
->cookie_mask
&& fm
->new_cookie
== OVS_BE64_MAX
1348 && (command
== OFPFC_MODIFY
|| command
== OFPFC_MODIFY_STRICT
)) {
1349 /* On modifies without a mask, we are supposed to add a flow if
1350 * one does not exist. If a cookie wasn't been specified, use a
1351 * default of zero. */
1352 fm
->new_cookie
= htonll(0);
1354 if (fields
& F_ACTIONS
) {
1355 struct ofpbuf ofpacts
;
1358 ofpbuf_init(&ofpacts
, 32);
1359 error
= str_to_inst_ofpacts(act_str
, &ofpacts
, usable_protocols
);
1363 err
= ofpacts_check(ofpacts
.data
, ofpacts
.size
, &fm
->match
.flow
,
1366 if (!enforce_consistency
&&
1367 err
== OFPERR_OFPBAC_MATCH_INCONSISTENT
) {
1368 /* Allow inconsistent actions to be used with OF 1.0. */
1369 *usable_protocols
&= OFPUTIL_P_OF10_ANY
;
1370 /* Try again, allowing for inconsistency.
1371 * XXX: As a side effect, logging may be duplicated. */
1372 err
= ofpacts_check(ofpacts
.data
, ofpacts
.size
,
1373 &fm
->match
.flow
, OFPP_MAX
, 0, false);
1376 error
= xasprintf("actions are invalid with specified match "
1377 "(%s)", ofperr_to_string(err
));
1382 ofpbuf_uninit(&ofpacts
);
1386 fm
->ofpacts_len
= ofpacts
.size
;
1387 fm
->ofpacts
= ofpbuf_steal_data(&ofpacts
);
1389 fm
->ofpacts_len
= 0;
1396 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
1397 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
1398 * Returns the set of usable protocols in '*usable_protocols'.
1400 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
1401 * constant for 'command'. To parse syntax for an OFPST_FLOW or
1402 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'.
1404 * Returns NULL if successful, otherwise a malloc()'d string describing the
1405 * error. The caller is responsible for freeing the returned string. */
1406 char * WARN_UNUSED_RESULT
1407 parse_ofp_str(struct ofputil_flow_mod
*fm
, int command
, const char *str_
,
1408 enum ofputil_protocol
*usable_protocols
,
1409 bool enforce_consistency
)
1411 char *string
= xstrdup(str_
);
1414 error
= parse_ofp_str__(fm
, command
, string
, usable_protocols
,
1415 enforce_consistency
);
1418 fm
->ofpacts_len
= 0;
1425 static char * WARN_UNUSED_RESULT
1426 parse_ofp_meter_mod_str__(struct ofputil_meter_mod
*mm
, char *string
,
1427 struct ofpbuf
*bands
, int command
,
1428 enum ofputil_protocol
*usable_protocols
)
1435 char *save_ptr
= NULL
;
1436 char *band_str
= NULL
;
1439 /* Meters require at least OF 1.3. */
1440 *usable_protocols
= OFPUTIL_P_OF13_UP
;
1448 fields
= F_METER
| F_FLAGS
| F_BANDS
;
1451 case OFPMC13_DELETE
:
1455 case OFPMC13_MODIFY
:
1456 fields
= F_METER
| F_FLAGS
| F_BANDS
;
1463 mm
->command
= command
;
1464 mm
->meter
.meter_id
= 0;
1465 mm
->meter
.flags
= 0;
1466 if (fields
& F_BANDS
) {
1467 band_str
= strstr(string
, "band");
1469 return xstrdup("must specify bands");
1473 band_str
= strchr(band_str
+ 1, '=');
1475 return xstrdup("must specify bands");
1480 for (name
= strtok_r(string
, "=, \t\r\n", &save_ptr
); name
;
1481 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
1483 if (fields
& F_FLAGS
&& !strcmp(name
, "kbps")) {
1484 mm
->meter
.flags
|= OFPMF13_KBPS
;
1485 } else if (fields
& F_FLAGS
&& !strcmp(name
, "pktps")) {
1486 mm
->meter
.flags
|= OFPMF13_PKTPS
;
1487 } else if (fields
& F_FLAGS
&& !strcmp(name
, "burst")) {
1488 mm
->meter
.flags
|= OFPMF13_BURST
;
1489 } else if (fields
& F_FLAGS
&& !strcmp(name
, "stats")) {
1490 mm
->meter
.flags
|= OFPMF13_STATS
;
1494 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
1496 return xasprintf("field %s missing value", name
);
1499 if (!strcmp(name
, "meter")) {
1500 if (!strcmp(value
, "all")) {
1501 mm
->meter
.meter_id
= OFPM13_ALL
;
1502 } else if (!strcmp(value
, "controller")) {
1503 mm
->meter
.meter_id
= OFPM13_CONTROLLER
;
1504 } else if (!strcmp(value
, "slowpath")) {
1505 mm
->meter
.meter_id
= OFPM13_SLOWPATH
;
1507 char *error
= str_to_u32(value
, &mm
->meter
.meter_id
);
1511 if (mm
->meter
.meter_id
> OFPM13_MAX
) {
1512 return xasprintf("invalid value for %s", name
);
1516 return xasprintf("unknown keyword %s", name
);
1520 if (fields
& F_METER
&& !mm
->meter
.meter_id
) {
1521 return xstrdup("must specify 'meter'");
1523 if (fields
& F_FLAGS
&& !mm
->meter
.flags
) {
1524 return xstrdup("meter must specify either 'kbps' or 'pktps'");
1527 if (fields
& F_BANDS
) {
1528 uint16_t n_bands
= 0;
1529 struct ofputil_meter_band
*band
= NULL
;
1532 for (name
= strtok_r(band_str
, "=, \t\r\n", &save_ptr
); name
;
1533 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
1537 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
1539 return xasprintf("field %s missing value", name
);
1542 if (!strcmp(name
, "type")) {
1543 /* Start a new band */
1544 band
= ofpbuf_put_zeros(bands
, sizeof *band
);
1547 if (!strcmp(value
, "drop")) {
1548 band
->type
= OFPMBT13_DROP
;
1549 } else if (!strcmp(value
, "dscp_remark")) {
1550 band
->type
= OFPMBT13_DSCP_REMARK
;
1552 return xasprintf("field %s unknown value %s", name
, value
);
1554 } else if (!band
|| !band
->type
) {
1555 return xstrdup("band must start with the 'type' keyword");
1556 } else if (!strcmp(name
, "rate")) {
1557 char *error
= str_to_u32(value
, &band
->rate
);
1561 } else if (!strcmp(name
, "burst_size")) {
1562 char *error
= str_to_u32(value
, &band
->burst_size
);
1566 } else if (!strcmp(name
, "prec_level")) {
1567 char *error
= str_to_u8(value
, name
, &band
->prec_level
);
1572 return xasprintf("unknown keyword %s", name
);
1575 /* validate bands */
1577 return xstrdup("meter must have bands");
1580 mm
->meter
.n_bands
= n_bands
;
1581 mm
->meter
.bands
= ofpbuf_steal_data(bands
);
1583 for (i
= 0; i
< n_bands
; ++i
) {
1584 band
= &mm
->meter
.bands
[i
];
1587 return xstrdup("band must have 'type'");
1589 if (band
->type
== OFPMBT13_DSCP_REMARK
) {
1590 if (!band
->prec_level
) {
1591 return xstrdup("'dscp_remark' band must have"
1595 if (band
->prec_level
) {
1596 return xstrdup("Only 'dscp_remark' band may have"
1601 return xstrdup("band must have 'rate'");
1603 if (mm
->meter
.flags
& OFPMF13_BURST
) {
1604 if (!band
->burst_size
) {
1605 return xstrdup("band must have 'burst_size' "
1606 "when 'burst' flag is set");
1609 if (band
->burst_size
) {
1610 return xstrdup("band may have 'burst_size' only "
1611 "when 'burst' flag is set");
1616 mm
->meter
.n_bands
= 0;
1617 mm
->meter
.bands
= NULL
;
1623 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
1624 * page) into 'mm' for sending the specified meter_mod 'command' to a switch.
1626 * Returns NULL if successful, otherwise a malloc()'d string describing the
1627 * error. The caller is responsible for freeing the returned string. */
1628 char * WARN_UNUSED_RESULT
1629 parse_ofp_meter_mod_str(struct ofputil_meter_mod
*mm
, const char *str_
,
1630 int command
, enum ofputil_protocol
*usable_protocols
)
1632 struct ofpbuf bands
;
1636 ofpbuf_init(&bands
, 64);
1637 string
= xstrdup(str_
);
1639 error
= parse_ofp_meter_mod_str__(mm
, string
, &bands
, command
,
1643 ofpbuf_uninit(&bands
);
1648 static char * WARN_UNUSED_RESULT
1649 parse_flow_monitor_request__(struct ofputil_flow_monitor_request
*fmr
,
1650 const char *str_
, char *string
,
1651 enum ofputil_protocol
*usable_protocols
)
1653 static atomic_uint32_t id
= ATOMIC_VAR_INIT(0);
1654 char *save_ptr
= NULL
;
1657 atomic_add(&id
, 1, &fmr
->id
);
1659 fmr
->flags
= (NXFMF_INITIAL
| NXFMF_ADD
| NXFMF_DELETE
| NXFMF_MODIFY
1660 | NXFMF_OWN
| NXFMF_ACTIONS
);
1661 fmr
->out_port
= OFPP_NONE
;
1662 fmr
->table_id
= 0xff;
1663 match_init_catchall(&fmr
->match
);
1665 for (name
= strtok_r(string
, "=, \t\r\n", &save_ptr
); name
;
1666 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
1667 const struct protocol
*p
;
1669 if (!strcmp(name
, "!initial")) {
1670 fmr
->flags
&= ~NXFMF_INITIAL
;
1671 } else if (!strcmp(name
, "!add")) {
1672 fmr
->flags
&= ~NXFMF_ADD
;
1673 } else if (!strcmp(name
, "!delete")) {
1674 fmr
->flags
&= ~NXFMF_DELETE
;
1675 } else if (!strcmp(name
, "!modify")) {
1676 fmr
->flags
&= ~NXFMF_MODIFY
;
1677 } else if (!strcmp(name
, "!actions")) {
1678 fmr
->flags
&= ~NXFMF_ACTIONS
;
1679 } else if (!strcmp(name
, "!own")) {
1680 fmr
->flags
&= ~NXFMF_OWN
;
1681 } else if (parse_protocol(name
, &p
)) {
1682 match_set_dl_type(&fmr
->match
, htons(p
->dl_type
));
1684 match_set_nw_proto(&fmr
->match
, p
->nw_proto
);
1689 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
1691 return xasprintf("%s: field %s missing value", str_
, name
);
1694 if (!strcmp(name
, "table")) {
1695 char *error
= str_to_u8(value
, "table", &fmr
->table_id
);
1699 } else if (!strcmp(name
, "out_port")) {
1700 fmr
->out_port
= u16_to_ofp(atoi(value
));
1701 } else if (mf_from_name(name
)) {
1704 error
= parse_field(mf_from_name(name
), value
, &fmr
->match
,
1710 return xasprintf("%s: unknown keyword %s", str_
, name
);
1717 /* Convert 'str_' (as described in the documentation for the "monitor" command
1718 * in the ovs-ofctl man page) into 'fmr'.
1720 * Returns NULL if successful, otherwise a malloc()'d string describing the
1721 * error. The caller is responsible for freeing the returned string. */
1722 char * WARN_UNUSED_RESULT
1723 parse_flow_monitor_request(struct ofputil_flow_monitor_request
*fmr
,
1725 enum ofputil_protocol
*usable_protocols
)
1727 char *string
= xstrdup(str_
);
1728 char *error
= parse_flow_monitor_request__(fmr
, str_
, string
,
1734 /* Parses 's' as a set of OpenFlow actions and appends the actions to
1737 * Returns NULL if successful, otherwise a malloc()'d string describing the
1738 * error. The caller is responsible for freeing the returned string. */
1739 char * WARN_UNUSED_RESULT
1740 parse_ofpacts(const char *s_
, struct ofpbuf
*ofpacts
,
1741 enum ofputil_protocol
*usable_protocols
)
1743 char *s
= xstrdup(s_
);
1746 *usable_protocols
= OFPUTIL_P_ANY
;
1748 error
= str_to_ofpacts(s
, ofpacts
, usable_protocols
);
1754 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
1755 * (one of OFPFC_*) into 'fm'.
1757 * Returns NULL if successful, otherwise a malloc()'d string describing the
1758 * error. The caller is responsible for freeing the returned string. */
1759 char * WARN_UNUSED_RESULT
1760 parse_ofp_flow_mod_str(struct ofputil_flow_mod
*fm
, const char *string
,
1762 enum ofputil_protocol
*usable_protocols
,
1763 bool enforce_consistency
)
1765 char *error
= parse_ofp_str(fm
, command
, string
, usable_protocols
,
1766 enforce_consistency
);
1768 /* Normalize a copy of the match. This ensures that non-normalized
1769 * flows get logged but doesn't affect what gets sent to the switch, so
1770 * that the switch can do whatever it likes with the flow. */
1771 struct match match_copy
= fm
->match
;
1772 ofputil_normalize_match(&match_copy
);
1778 /* Convert 'table_id' and 'flow_miss_handling' (as described for the
1779 * "mod-table" command in the ovs-ofctl man page) into 'tm' for sending the
1780 * specified table_mod 'command' to a switch.
1782 * Returns NULL if successful, otherwise a malloc()'d string describing the
1783 * error. The caller is responsible for freeing the returned string. */
1784 char * WARN_UNUSED_RESULT
1785 parse_ofp_table_mod(struct ofputil_table_mod
*tm
, const char *table_id
,
1786 const char *flow_miss_handling
,
1787 enum ofputil_protocol
*usable_protocols
)
1789 /* Table mod requires at least OF 1.1. */
1790 *usable_protocols
= OFPUTIL_P_OF11_UP
;
1792 if (!strcasecmp(table_id
, "all")) {
1795 char *error
= str_to_u8(table_id
, "table_id", &tm
->table_id
);
1801 if (strcmp(flow_miss_handling
, "controller") == 0) {
1802 tm
->config
= OFPTC11_TABLE_MISS_CONTROLLER
;
1803 } else if (strcmp(flow_miss_handling
, "continue") == 0) {
1804 tm
->config
= OFPTC11_TABLE_MISS_CONTINUE
;
1805 } else if (strcmp(flow_miss_handling
, "drop") == 0) {
1806 tm
->config
= OFPTC11_TABLE_MISS_DROP
;
1808 return xasprintf("invalid flow_miss_handling %s", flow_miss_handling
);
1811 if (tm
->table_id
== 0xfe && tm
->config
== OFPTC11_TABLE_MISS_CONTINUE
) {
1812 return xstrdup("last table's flow miss handling can not be continue");
1819 /* Opens file 'file_name' and reads each line as a flow_mod of the specified
1820 * type (one of OFPFC_*). Stores each flow_mod in '*fm', an array allocated
1821 * on the caller's behalf, and the number of flow_mods in '*n_fms'.
1823 * Returns NULL if successful, otherwise a malloc()'d string describing the
1824 * error. The caller is responsible for freeing the returned string. */
1825 char * WARN_UNUSED_RESULT
1826 parse_ofp_flow_mod_file(const char *file_name
, uint16_t command
,
1827 struct ofputil_flow_mod
**fms
, size_t *n_fms
,
1828 enum ofputil_protocol
*usable_protocols
,
1829 bool enforce_consistency
)
1831 size_t allocated_fms
;
1836 *usable_protocols
= OFPUTIL_P_ANY
;
1841 stream
= !strcmp(file_name
, "-") ? stdin
: fopen(file_name
, "r");
1842 if (stream
== NULL
) {
1843 return xasprintf("%s: open failed (%s)",
1844 file_name
, ovs_strerror(errno
));
1847 allocated_fms
= *n_fms
;
1850 while (!ds_get_preprocessed_line(&s
, stream
, &line_number
)) {
1852 enum ofputil_protocol usable
;
1854 if (*n_fms
>= allocated_fms
) {
1855 *fms
= x2nrealloc(*fms
, &allocated_fms
, sizeof **fms
);
1857 error
= parse_ofp_flow_mod_str(&(*fms
)[*n_fms
], ds_cstr(&s
), command
,
1858 &usable
, enforce_consistency
);
1862 for (i
= 0; i
< *n_fms
; i
++) {
1863 free((*fms
)[i
].ofpacts
);
1870 if (stream
!= stdin
) {
1874 return xasprintf("%s:%d: %s", file_name
, line_number
, error
);
1876 *usable_protocols
&= usable
; /* Each line can narrow the set. */
1881 if (stream
!= stdin
) {
1887 char * WARN_UNUSED_RESULT
1888 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request
*fsr
,
1889 bool aggregate
, const char *string
,
1890 enum ofputil_protocol
*usable_protocols
,
1891 bool enforce_consistency
)
1893 struct ofputil_flow_mod fm
;
1896 error
= parse_ofp_str(&fm
, -1, string
, usable_protocols
,
1897 enforce_consistency
);
1902 /* Special table ID support not required for stats requests. */
1903 if (*usable_protocols
& OFPUTIL_P_OF10_STD_TID
) {
1904 *usable_protocols
|= OFPUTIL_P_OF10_STD
;
1906 if (*usable_protocols
& OFPUTIL_P_OF10_NXM_TID
) {
1907 *usable_protocols
|= OFPUTIL_P_OF10_NXM
;
1910 fsr
->aggregate
= aggregate
;
1911 fsr
->cookie
= fm
.cookie
;
1912 fsr
->cookie_mask
= fm
.cookie_mask
;
1913 fsr
->match
= fm
.match
;
1914 fsr
->out_port
= fm
.out_port
;
1915 fsr
->out_group
= fm
.out_group
;
1916 fsr
->table_id
= fm
.table_id
;
1920 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
1921 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
1922 * mf_field. Fields must be specified in a natural order for satisfying
1923 * prerequisites. If 'mask' is specified, fills the mask field for each of the
1924 * field specified in flow. If the map, 'names_portno' is specfied, converts
1925 * the in_port name into port no while setting the 'flow'.
1927 * Returns NULL on success, otherwise a malloc()'d string that explains the
1930 parse_ofp_exact_flow(struct flow
*flow
, struct flow
*mask
, const char *s
,
1931 const struct simap
*portno_names
)
1933 char *pos
, *key
, *value_s
;
1937 memset(flow
, 0, sizeof *flow
);
1939 memset(mask
, 0, sizeof *mask
);
1942 pos
= copy
= xstrdup(s
);
1943 while (ofputil_parse_key_value(&pos
, &key
, &value_s
)) {
1944 const struct protocol
*p
;
1945 if (parse_protocol(key
, &p
)) {
1946 if (flow
->dl_type
) {
1947 error
= xasprintf("%s: Ethernet type set multiple times", s
);
1950 flow
->dl_type
= htons(p
->dl_type
);
1952 mask
->dl_type
= OVS_BE16_MAX
;
1956 if (flow
->nw_proto
) {
1957 error
= xasprintf("%s: network protocol set "
1958 "multiple times", s
);
1961 flow
->nw_proto
= p
->nw_proto
;
1963 mask
->nw_proto
= UINT8_MAX
;
1967 const struct mf_field
*mf
;
1968 union mf_value value
;
1971 mf
= mf_from_name(key
);
1973 error
= xasprintf("%s: unknown field %s", s
, key
);
1977 if (!mf_are_prereqs_ok(mf
, flow
)) {
1978 error
= xasprintf("%s: prerequisites not met for setting %s",
1983 if (!mf_is_zero(mf
, flow
)) {
1984 error
= xasprintf("%s: field %s set multiple times", s
, key
);
1988 if (!strcmp(key
, "in_port")
1990 && simap_contains(portno_names
, value_s
)) {
1991 flow
->in_port
.ofp_port
= u16_to_ofp(
1992 simap_get(portno_names
, value_s
));
1994 mask
->in_port
.ofp_port
= u16_to_ofp(ntohs(OVS_BE16_MAX
));
1997 field_error
= mf_parse_value(mf
, value_s
, &value
);
1999 error
= xasprintf("%s: bad value for %s (%s)",
2000 s
, key
, field_error
);
2005 mf_set_flow_value(mf
, &value
, flow
);
2007 mf_mask_field(mf
, mask
);
2013 if (!flow
->in_port
.ofp_port
) {
2014 flow
->in_port
.ofp_port
= OFPP_NONE
;
2021 memset(flow
, 0, sizeof *flow
);
2023 memset(mask
, 0, sizeof *mask
);
2029 static char * WARN_UNUSED_RESULT
2030 parse_bucket_str(struct ofputil_bucket
*bucket
, char *str_
,
2031 enum ofputil_protocol
*usable_protocols
)
2033 struct ofpbuf ofpacts
;
2034 char *pos
, *act
, *arg
;
2038 bucket
->watch_port
= OFPP_ANY
;
2039 bucket
->watch_group
= OFPG11_ANY
;
2043 ofpbuf_init(&ofpacts
, 64);
2044 while (ofputil_parse_key_value(&pos
, &act
, &arg
)) {
2047 if (!strcasecmp(act
, "weight")) {
2048 error
= str_to_u16(arg
, "weight", &bucket
->weight
);
2049 } else if (!strcasecmp(act
, "watch_port")) {
2050 if (!ofputil_port_from_string(arg
, &bucket
->watch_port
)
2051 || (ofp_to_u16(bucket
->watch_port
) >= ofp_to_u16(OFPP_MAX
)
2052 && bucket
->watch_port
!= OFPP_ANY
)) {
2053 error
= xasprintf("%s: invalid watch_port", arg
);
2055 } else if (!strcasecmp(act
, "watch_group")) {
2056 error
= str_to_u32(arg
, &bucket
->watch_group
);
2057 if (!error
&& bucket
->watch_group
> OFPG_MAX
) {
2058 error
= xasprintf("invalid watch_group id %"PRIu32
,
2059 bucket
->watch_group
);
2062 error
= str_to_ofpact__(pos
, act
, arg
, &ofpacts
, n_actions
,
2068 ofpbuf_uninit(&ofpacts
);
2073 ofpact_pad(&ofpacts
);
2074 bucket
->ofpacts
= ofpacts
.data
;
2075 bucket
->ofpacts_len
= ofpacts
.size
;
2080 static char * WARN_UNUSED_RESULT
2081 parse_ofp_group_mod_str__(struct ofputil_group_mod
*gm
, uint16_t command
,
2083 enum ofputil_protocol
*usable_protocols
)
2086 F_GROUP_TYPE
= 1 << 0,
2089 char *save_ptr
= NULL
;
2090 bool had_type
= false;
2092 struct ofputil_bucket
*bucket
;
2095 *usable_protocols
= OFPUTIL_P_OF11_UP
;
2099 fields
= F_GROUP_TYPE
| F_BUCKETS
;
2102 case OFPGC11_DELETE
:
2106 case OFPGC11_MODIFY
:
2107 fields
= F_GROUP_TYPE
| F_BUCKETS
;
2114 memset(gm
, 0, sizeof *gm
);
2115 gm
->command
= command
;
2116 gm
->group_id
= OFPG_ANY
;
2117 list_init(&gm
->buckets
);
2118 if (command
== OFPGC11_DELETE
&& string
[0] == '\0') {
2119 gm
->group_id
= OFPG_ALL
;
2123 *usable_protocols
= OFPUTIL_P_OF11_UP
;
2125 if (fields
& F_BUCKETS
) {
2126 char *bkt_str
= strstr(string
, "bucket");
2135 bkt_str
= strchr(bkt_str
+ 1, '=');
2137 error
= xstrdup("must specify bucket content");
2142 next_bkt_str
= strstr(bkt_str
, "bucket");
2144 *next_bkt_str
= '\0';
2147 bucket
= xzalloc(sizeof(struct ofputil_bucket
));
2148 error
= parse_bucket_str(bucket
, bkt_str
, usable_protocols
);
2153 list_push_back(&gm
->buckets
, &bucket
->list_node
);
2155 bkt_str
= next_bkt_str
;
2159 for (name
= strtok_r(string
, "=, \t\r\n", &save_ptr
); name
;
2160 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
2163 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
2165 error
= xasprintf("field %s missing value", name
);
2169 if (!strcmp(name
, "group_id")) {
2170 if(!strcmp(value
, "all")) {
2171 gm
->group_id
= OFPG_ALL
;
2173 char *error
= str_to_u32(value
, &gm
->group_id
);
2177 if (gm
->group_id
!= OFPG_ALL
&& gm
->group_id
> OFPG_MAX
) {
2178 error
= xasprintf("invalid group id %"PRIu32
,
2183 } else if (!strcmp(name
, "type")){
2184 if (!(fields
& F_GROUP_TYPE
)) {
2185 error
= xstrdup("type is not needed");
2188 if (!strcmp(value
, "all")) {
2189 gm
->type
= OFPGT11_ALL
;
2190 } else if (!strcmp(value
, "select")) {
2191 gm
->type
= OFPGT11_SELECT
;
2192 } else if (!strcmp(value
, "indirect")) {
2193 gm
->type
= OFPGT11_INDIRECT
;
2194 } else if (!strcmp(value
, "ff") ||
2195 !strcmp(value
, "fast_failover")) {
2196 gm
->type
= OFPGT11_FF
;
2198 error
= xasprintf("invalid group type %s", value
);
2202 } else if (!strcmp(name
, "bucket")) {
2203 error
= xstrdup("bucket is not needed");
2206 error
= xasprintf("unknown keyword %s", name
);
2210 if (gm
->group_id
== OFPG_ANY
) {
2211 error
= xstrdup("must specify a group_id");
2214 if (fields
& F_GROUP_TYPE
&& !had_type
) {
2215 error
= xstrdup("must specify a type");
2219 /* Validate buckets. */
2220 LIST_FOR_EACH (bucket
, list_node
, &gm
->buckets
) {
2221 if (bucket
->weight
!= 1 && gm
->type
!= OFPGT11_SELECT
) {
2222 error
= xstrdup("Only select groups can have bucket weights.");
2226 if (gm
->type
== OFPGT11_INDIRECT
&& !list_is_short(&gm
->buckets
)) {
2227 error
= xstrdup("Indirect groups can have at most one bucket.");
2233 ofputil_bucket_list_destroy(&gm
->buckets
);
2237 char * WARN_UNUSED_RESULT
2238 parse_ofp_group_mod_str(struct ofputil_group_mod
*gm
, uint16_t command
,
2240 enum ofputil_protocol
*usable_protocols
)
2242 char *string
= xstrdup(str_
);
2243 char *error
= parse_ofp_group_mod_str__(gm
, command
, string
,
2248 ofputil_bucket_list_destroy(&gm
->buckets
);
2253 char * WARN_UNUSED_RESULT
2254 parse_ofp_group_mod_file(const char *file_name
, uint16_t command
,
2255 struct ofputil_group_mod
**gms
, size_t *n_gms
,
2256 enum ofputil_protocol
*usable_protocols
)
2258 size_t allocated_gms
;
2266 stream
= !strcmp(file_name
, "-") ? stdin
: fopen(file_name
, "r");
2267 if (stream
== NULL
) {
2268 return xasprintf("%s: open failed (%s)",
2269 file_name
, ovs_strerror(errno
));
2272 allocated_gms
= *n_gms
;
2275 *usable_protocols
= OFPUTIL_P_OF11_UP
;
2276 while (!ds_get_preprocessed_line(&s
, stream
, &line_number
)) {
2277 enum ofputil_protocol usable
;
2280 if (*n_gms
>= allocated_gms
) {
2281 *gms
= x2nrealloc(*gms
, &allocated_gms
, sizeof **gms
);
2283 error
= parse_ofp_group_mod_str(&(*gms
)[*n_gms
], command
, ds_cstr(&s
),
2288 for (i
= 0; i
< *n_gms
; i
++) {
2289 ofputil_bucket_list_destroy(&(*gms
)[i
].buckets
);
2296 if (stream
!= stdin
) {
2300 return xasprintf("%s:%d: %s", file_name
, line_number
, error
);
2302 *usable_protocols
&= usable
;
2307 if (stream
!= stdin
) {