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"
39 #include "socket-util.h"
43 VLOG_DEFINE_THIS_MODULE(ofp_parse
);
45 /* Parses 'str' as an 8-bit unsigned integer into '*valuep'.
47 * 'name' describes the value parsed in an error message, if any.
49 * Returns NULL if successful, otherwise a malloc()'d string describing the
50 * error. The caller is responsible for freeing the returned string. */
51 static char * WARN_UNUSED_RESULT
52 str_to_u8(const char *str
, const char *name
, uint8_t *valuep
)
56 if (!str_to_int(str
, 0, &value
) || value
< 0 || value
> 255) {
57 return xasprintf("invalid %s \"%s\"", name
, str
);
63 /* Parses 'str' as a 16-bit unsigned integer into '*valuep'.
65 * 'name' describes the value parsed in an error message, if any.
67 * Returns NULL if successful, otherwise a malloc()'d string describing the
68 * error. The caller is responsible for freeing the returned string. */
69 static char * WARN_UNUSED_RESULT
70 str_to_u16(const char *str
, const char *name
, uint16_t *valuep
)
74 if (!str_to_int(str
, 0, &value
) || value
< 0 || value
> 65535) {
75 return xasprintf("invalid %s \"%s\"", name
, str
);
81 /* Parses 'str' as a 32-bit unsigned integer into '*valuep'.
83 * Returns NULL if successful, otherwise a malloc()'d string describing the
84 * error. The caller is responsible for freeing the returned string. */
85 static char * WARN_UNUSED_RESULT
86 str_to_u32(const char *str
, uint32_t *valuep
)
92 return xstrdup("missing required numeric argument");
96 value
= strtoul(str
, &tail
, 0);
97 if (errno
== EINVAL
|| errno
== ERANGE
|| *tail
) {
98 return xasprintf("invalid numeric format %s", str
);
104 /* Parses 'str' as an 64-bit unsigned integer into '*valuep'.
106 * Returns NULL if successful, otherwise a malloc()'d string describing the
107 * error. The caller is responsible for freeing the returned string. */
108 static char * WARN_UNUSED_RESULT
109 str_to_u64(const char *str
, uint64_t *valuep
)
115 return xstrdup("missing required numeric argument");
119 value
= strtoull(str
, &tail
, 0);
120 if (errno
== EINVAL
|| errno
== ERANGE
|| *tail
) {
121 return xasprintf("invalid numeric format %s", str
);
127 /* Parses 'str' as an 64-bit unsigned integer in network byte order into
130 * Returns NULL if successful, otherwise a malloc()'d string describing the
131 * error. The caller is responsible for freeing the returned string. */
132 static char * WARN_UNUSED_RESULT
133 str_to_be64(const char *str
, ovs_be64
*valuep
)
138 error
= str_to_u64(str
, &value
);
140 *valuep
= htonll(value
);
145 /* Parses 'str' as an Ethernet address into 'mac'.
147 * Returns NULL if successful, otherwise a malloc()'d string describing the
148 * error. The caller is responsible for freeing the returned string. */
149 static char * WARN_UNUSED_RESULT
150 str_to_mac(const char *str
, uint8_t mac
[6])
152 if (sscanf(str
, ETH_ADDR_SCAN_FMT
, ETH_ADDR_SCAN_ARGS(mac
))
153 != ETH_ADDR_SCAN_COUNT
) {
154 return xasprintf("invalid mac address %s", str
);
159 /* Parses 'str' as an IP address into '*ip'.
161 * Returns NULL if successful, otherwise a malloc()'d string describing the
162 * error. The caller is responsible for freeing the returned string. */
163 static char * WARN_UNUSED_RESULT
164 str_to_ip(const char *str
, ovs_be32
*ip
)
166 struct in_addr in_addr
;
168 if (lookup_ip(str
, &in_addr
)) {
169 return xasprintf("%s: could not convert to IP address", str
);
171 *ip
= in_addr
.s_addr
;
175 /* Parses 'arg' as the argument to an "enqueue" action, and appends such an
176 * action to 'ofpacts'.
178 * Returns NULL if successful, otherwise a malloc()'d string describing the
179 * error. The caller is responsible for freeing the returned string. */
180 static char * WARN_UNUSED_RESULT
181 parse_enqueue(char *arg
, struct ofpbuf
*ofpacts
)
184 char *port
= strtok_r(arg
, ":q", &sp
);
185 char *queue
= strtok_r(NULL
, "", &sp
);
186 struct ofpact_enqueue
*enqueue
;
188 if (port
== NULL
|| queue
== NULL
) {
189 return xstrdup("\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
192 enqueue
= ofpact_put_ENQUEUE(ofpacts
);
193 if (!ofputil_port_from_string(port
, &enqueue
->port
)) {
194 return xasprintf("%s: enqueue to unknown port", port
);
196 return str_to_u32(queue
, &enqueue
->queue
);
199 /* Parses 'arg' as the argument to an "output" action, and appends such an
200 * action to 'ofpacts'.
202 * Returns NULL if successful, otherwise a malloc()'d string describing the
203 * error. The caller is responsible for freeing the returned string. */
204 static char * WARN_UNUSED_RESULT
205 parse_output(const char *arg
, struct ofpbuf
*ofpacts
)
207 if (strchr(arg
, '[')) {
208 struct ofpact_output_reg
*output_reg
;
210 output_reg
= ofpact_put_OUTPUT_REG(ofpacts
);
211 output_reg
->max_len
= UINT16_MAX
;
212 return mf_parse_subfield(&output_reg
->src
, arg
);
214 struct ofpact_output
*output
;
216 output
= ofpact_put_OUTPUT(ofpacts
);
217 output
->max_len
= output
->port
== OFPP_CONTROLLER
? UINT16_MAX
: 0;
218 if (!ofputil_port_from_string(arg
, &output
->port
)) {
219 return xasprintf("%s: output to unknown port", arg
);
225 /* Parses 'arg' as the argument to an "resubmit" action, and appends such an
226 * action to 'ofpacts'.
228 * Returns NULL if successful, otherwise a malloc()'d string describing the
229 * error. The caller is responsible for freeing the returned string. */
230 static char * WARN_UNUSED_RESULT
231 parse_resubmit(char *arg
, struct ofpbuf
*ofpacts
)
233 struct ofpact_resubmit
*resubmit
;
234 char *in_port_s
, *table_s
;
236 resubmit
= ofpact_put_RESUBMIT(ofpacts
);
238 in_port_s
= strsep(&arg
, ",");
239 if (in_port_s
&& in_port_s
[0]) {
240 if (!ofputil_port_from_string(in_port_s
, &resubmit
->in_port
)) {
241 return xasprintf("%s: resubmit to unknown port", in_port_s
);
244 resubmit
->in_port
= OFPP_IN_PORT
;
247 table_s
= strsep(&arg
, ",");
248 if (table_s
&& table_s
[0]) {
249 uint32_t table_id
= 0;
252 error
= str_to_u32(table_s
, &table_id
);
256 resubmit
->table_id
= table_id
;
258 resubmit
->table_id
= 255;
261 if (resubmit
->in_port
== OFPP_IN_PORT
&& resubmit
->table_id
== 255) {
262 return xstrdup("at least one \"in_port\" or \"table\" must be "
263 "specified on resubmit");
268 /* Parses 'arg' as the argument to a "note" action, and appends such an action
271 * Returns NULL if successful, otherwise a malloc()'d string describing the
272 * error. The caller is responsible for freeing the returned string. */
273 static char * WARN_UNUSED_RESULT
274 parse_note(const char *arg
, struct ofpbuf
*ofpacts
)
276 struct ofpact_note
*note
;
278 note
= ofpact_put_NOTE(ofpacts
);
279 while (*arg
!= '\0') {
290 byte
= hexits_value(arg
, 2, &ok
);
292 return xstrdup("bad hex digit in `note' argument");
294 ofpbuf_put(ofpacts
, &byte
, 1);
301 ofpact_update_len(ofpacts
, ¬e
->ofpact
);
305 /* Parses 'arg' as the argument to a "fin_timeout" action, and appends such an
306 * action to 'ofpacts'.
308 * Returns NULL if successful, otherwise a malloc()'d string describing the
309 * error. The caller is responsible for freeing the returned string. */
310 static char * WARN_UNUSED_RESULT
311 parse_fin_timeout(struct ofpbuf
*b
, char *arg
)
313 struct ofpact_fin_timeout
*oft
= ofpact_put_FIN_TIMEOUT(b
);
316 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
319 if (!strcmp(key
, "idle_timeout")) {
320 error
= str_to_u16(value
, key
, &oft
->fin_idle_timeout
);
321 } else if (!strcmp(key
, "hard_timeout")) {
322 error
= str_to_u16(value
, key
, &oft
->fin_hard_timeout
);
324 error
= xasprintf("invalid key '%s' in 'fin_timeout' argument",
335 /* Parses 'arg' as the argument to a "controller" action, and appends such an
336 * action to 'ofpacts'.
338 * Returns NULL if successful, otherwise a malloc()'d string describing the
339 * error. The caller is responsible for freeing the returned string. */
340 static char * WARN_UNUSED_RESULT
341 parse_controller(struct ofpbuf
*b
, char *arg
)
343 enum ofp_packet_in_reason reason
= OFPR_ACTION
;
344 uint16_t controller_id
= 0;
345 uint16_t max_len
= UINT16_MAX
;
349 } else if (strspn(arg
, "0123456789") == strlen(arg
)) {
350 char *error
= str_to_u16(arg
, "max_len", &max_len
);
357 while (ofputil_parse_key_value(&arg
, &name
, &value
)) {
358 if (!strcmp(name
, "reason")) {
359 if (!ofputil_packet_in_reason_from_string(value
, &reason
)) {
360 return xasprintf("unknown reason \"%s\"", value
);
362 } else if (!strcmp(name
, "max_len")) {
363 char *error
= str_to_u16(value
, "max_len", &max_len
);
367 } else if (!strcmp(name
, "id")) {
368 char *error
= str_to_u16(value
, "id", &controller_id
);
373 return xasprintf("unknown key \"%s\" parsing controller "
379 if (reason
== OFPR_ACTION
&& controller_id
== 0) {
380 struct ofpact_output
*output
;
382 output
= ofpact_put_OUTPUT(b
);
383 output
->port
= OFPP_CONTROLLER
;
384 output
->max_len
= max_len
;
386 struct ofpact_controller
*controller
;
388 controller
= ofpact_put_CONTROLLER(b
);
389 controller
->max_len
= max_len
;
390 controller
->reason
= reason
;
391 controller
->controller_id
= controller_id
;
398 parse_noargs_dec_ttl(struct ofpbuf
*b
)
400 struct ofpact_cnt_ids
*ids
;
403 ids
= ofpact_put_DEC_TTL(b
);
404 ofpbuf_put(b
, &id
, sizeof id
);
406 ids
->n_controllers
++;
407 ofpact_update_len(b
, &ids
->ofpact
);
410 /* Parses 'arg' as the argument to a "dec_ttl" action, and appends such an
411 * action to 'ofpacts'.
413 * Returns NULL if successful, otherwise a malloc()'d string describing the
414 * error. The caller is responsible for freeing the returned string. */
415 static char * WARN_UNUSED_RESULT
416 parse_dec_ttl(struct ofpbuf
*b
, char *arg
)
419 parse_noargs_dec_ttl(b
);
421 struct ofpact_cnt_ids
*ids
;
424 ids
= ofpact_put_DEC_TTL(b
);
425 ids
->ofpact
.compat
= OFPUTIL_NXAST_DEC_TTL_CNT_IDS
;
426 for (cntr
= strtok_r(arg
, ", ", &arg
); cntr
!= NULL
;
427 cntr
= strtok_r(NULL
, ", ", &arg
)) {
428 uint16_t id
= atoi(cntr
);
430 ofpbuf_put(b
, &id
, sizeof id
);
432 ids
->n_controllers
++;
434 if (!ids
->n_controllers
) {
435 return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
438 ofpact_update_len(b
, &ids
->ofpact
);
443 /* Parses 'arg' as the argument to a "set_mpls_ttl" action, and appends such an
444 * action to 'ofpacts'.
446 * Returns NULL if successful, otherwise a malloc()'d string describing the
447 * error. The caller is responsible for freeing the returned string. */
448 static char * WARN_UNUSED_RESULT
449 parse_set_mpls_ttl(struct ofpbuf
*b
, const char *arg
)
451 struct ofpact_mpls_ttl
*mpls_ttl
= ofpact_put_SET_MPLS_TTL(b
);
454 return xstrdup("parse_set_mpls_ttl: expected ttl.");
457 mpls_ttl
->ttl
= atoi(arg
);
461 /* Parses a "set_field" action with argument 'arg', appending the parsed
462 * action to 'ofpacts'.
464 * Returns NULL if successful, otherwise a malloc()'d string describing the
465 * error. The caller is responsible for freeing the returned string. */
466 static char * WARN_UNUSED_RESULT
467 set_field_parse__(char *arg
, struct ofpbuf
*ofpacts
,
468 enum ofputil_protocol
*usable_protocols
)
470 struct ofpact_reg_load
*load
= ofpact_put_REG_LOAD(ofpacts
);
474 const struct mf_field
*mf
;
476 union mf_value mf_value
;
479 delim
= strstr(arg
, "->");
481 return xasprintf("%s: missing `->'", arg
);
483 if (strlen(delim
) <= strlen("->")) {
484 return xasprintf("%s: missing field name following `->'", arg
);
487 key
= delim
+ strlen("->");
488 mf
= mf_from_name(key
);
490 return xasprintf("%s is not a valid OXM field name", key
);
493 return xasprintf("%s is read-only", key
);
497 error
= mf_parse_value(mf
, value
, &mf_value
);
501 if (!mf_is_value_valid(mf
, &mf_value
)) {
502 return xasprintf("%s is not a valid value for field %s", value
, key
);
504 ofpact_set_field_init(load
, mf
, &mf_value
);
506 *usable_protocols
&= mf
->usable_protocols
;
510 /* Parses 'arg' as the argument to a "set_field" action, and appends such an
511 * action to 'ofpacts'.
513 * Returns NULL if successful, otherwise a malloc()'d string describing the
514 * error. The caller is responsible for freeing the returned string. */
515 static char * WARN_UNUSED_RESULT
516 set_field_parse(const char *arg
, struct ofpbuf
*ofpacts
,
517 enum ofputil_protocol
*usable_protocols
)
519 char *copy
= xstrdup(arg
);
520 char *error
= set_field_parse__(copy
, ofpacts
, usable_protocols
);
525 /* Parses 'arg' as the argument to a "write_metadata" instruction, and appends
526 * such an action to 'ofpacts'.
528 * Returns NULL if successful, otherwise a malloc()'d string describing the
529 * error. The caller is responsible for freeing the returned string. */
530 static char * WARN_UNUSED_RESULT
531 parse_metadata(struct ofpbuf
*b
, char *arg
)
533 struct ofpact_metadata
*om
;
534 char *mask
= strchr(arg
, '/');
536 om
= ofpact_put_WRITE_METADATA(b
);
542 error
= str_to_be64(mask
+ 1, &om
->mask
);
547 om
->mask
= htonll(UINT64_MAX
);
550 return str_to_be64(arg
, &om
->metadata
);
553 /* Parses 'arg' as the argument to a "sample" action, and appends such an
554 * action to 'ofpacts'.
556 * Returns NULL if successful, otherwise a malloc()'d string describing the
557 * error. The caller is responsible for freeing the returned string. */
558 static char * WARN_UNUSED_RESULT
559 parse_sample(struct ofpbuf
*b
, char *arg
)
561 struct ofpact_sample
*os
= ofpact_put_SAMPLE(b
);
564 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
567 if (!strcmp(key
, "probability")) {
568 error
= str_to_u16(value
, "probability", &os
->probability
);
569 if (!error
&& os
->probability
== 0) {
570 error
= xasprintf("invalid probability value \"%s\"", value
);
572 } else if (!strcmp(key
, "collector_set_id")) {
573 error
= str_to_u32(value
, &os
->collector_set_id
);
574 } else if (!strcmp(key
, "obs_domain_id")) {
575 error
= str_to_u32(value
, &os
->obs_domain_id
);
576 } else if (!strcmp(key
, "obs_point_id")) {
577 error
= str_to_u32(value
, &os
->obs_point_id
);
579 error
= xasprintf("invalid key \"%s\" in \"sample\" argument",
586 if (os
->probability
== 0) {
587 return xstrdup("non-zero \"probability\" must be specified on sample");
592 /* Parses 'arg' as the argument to action 'code', and appends such an action to
595 * Returns NULL if successful, otherwise a malloc()'d string describing the
596 * error. The caller is responsible for freeing the returned string. */
597 static char * WARN_UNUSED_RESULT
598 parse_named_action(enum ofputil_action_code code
,
599 char *arg
, struct ofpbuf
*ofpacts
,
600 enum ofputil_protocol
*usable_protocols
)
602 size_t orig_size
= ofpacts
->size
;
603 struct ofpact_tunnel
*tunnel
;
605 uint16_t ethertype
= 0;
611 case OFPUTIL_ACTION_INVALID
:
614 case OFPUTIL_OFPAT10_OUTPUT
:
615 case OFPUTIL_OFPAT11_OUTPUT
:
616 error
= parse_output(arg
, ofpacts
);
619 case OFPUTIL_OFPAT10_SET_VLAN_VID
:
620 case OFPUTIL_OFPAT11_SET_VLAN_VID
:
621 error
= str_to_u16(arg
, "VLAN VID", &vid
);
626 if (vid
& ~VLAN_VID_MASK
) {
627 return xasprintf("%s: not a valid VLAN VID", arg
);
629 ofpact_put_SET_VLAN_VID(ofpacts
)->vlan_vid
= vid
;
632 case OFPUTIL_OFPAT10_SET_VLAN_PCP
:
633 case OFPUTIL_OFPAT11_SET_VLAN_PCP
:
634 error
= str_to_u8(arg
, "VLAN PCP", &pcp
);
640 return xasprintf("%s: not a valid VLAN PCP", arg
);
642 ofpact_put_SET_VLAN_PCP(ofpacts
)->vlan_pcp
= pcp
;
645 case OFPUTIL_OFPAT12_SET_FIELD
:
646 return set_field_parse(arg
, ofpacts
, usable_protocols
);
648 case OFPUTIL_OFPAT10_STRIP_VLAN
:
649 case OFPUTIL_OFPAT11_POP_VLAN
:
650 ofpact_put_STRIP_VLAN(ofpacts
);
653 case OFPUTIL_OFPAT11_PUSH_VLAN
:
654 *usable_protocols
&= OFPUTIL_P_OF11_UP
;
655 error
= str_to_u16(arg
, "ethertype", ðertype
);
660 if (ethertype
!= ETH_TYPE_VLAN_8021Q
) {
661 /* XXX ETH_TYPE_VLAN_8021AD case isn't supported */
662 return xasprintf("%s: not a valid VLAN ethertype", arg
);
665 ofpact_put_PUSH_VLAN(ofpacts
);
668 case OFPUTIL_OFPAT11_SET_QUEUE
:
669 error
= str_to_u32(arg
, &ofpact_put_SET_QUEUE(ofpacts
)->queue_id
);
672 case OFPUTIL_OFPAT10_SET_DL_SRC
:
673 case OFPUTIL_OFPAT11_SET_DL_SRC
:
674 error
= str_to_mac(arg
, ofpact_put_SET_ETH_SRC(ofpacts
)->mac
);
677 case OFPUTIL_OFPAT10_SET_DL_DST
:
678 case OFPUTIL_OFPAT11_SET_DL_DST
:
679 error
= str_to_mac(arg
, ofpact_put_SET_ETH_DST(ofpacts
)->mac
);
682 case OFPUTIL_OFPAT10_SET_NW_SRC
:
683 case OFPUTIL_OFPAT11_SET_NW_SRC
:
684 error
= str_to_ip(arg
, &ofpact_put_SET_IPV4_SRC(ofpacts
)->ipv4
);
687 case OFPUTIL_OFPAT10_SET_NW_DST
:
688 case OFPUTIL_OFPAT11_SET_NW_DST
:
689 error
= str_to_ip(arg
, &ofpact_put_SET_IPV4_DST(ofpacts
)->ipv4
);
692 case OFPUTIL_OFPAT10_SET_NW_TOS
:
693 case OFPUTIL_OFPAT11_SET_NW_TOS
:
694 error
= str_to_u8(arg
, "TOS", &tos
);
699 if (tos
& ~IP_DSCP_MASK
) {
700 return xasprintf("%s: not a valid TOS", arg
);
702 ofpact_put_SET_IPV4_DSCP(ofpacts
)->dscp
= tos
;
705 case OFPUTIL_OFPAT11_DEC_NW_TTL
:
708 case OFPUTIL_OFPAT10_SET_TP_SRC
:
709 case OFPUTIL_OFPAT11_SET_TP_SRC
:
710 error
= str_to_u16(arg
, "source port",
711 &ofpact_put_SET_L4_SRC_PORT(ofpacts
)->port
);
714 case OFPUTIL_OFPAT10_SET_TP_DST
:
715 case OFPUTIL_OFPAT11_SET_TP_DST
:
716 error
= str_to_u16(arg
, "destination port",
717 &ofpact_put_SET_L4_DST_PORT(ofpacts
)->port
);
720 case OFPUTIL_OFPAT10_ENQUEUE
:
721 error
= parse_enqueue(arg
, ofpacts
);
724 case OFPUTIL_NXAST_RESUBMIT
:
725 error
= parse_resubmit(arg
, ofpacts
);
728 case OFPUTIL_NXAST_SET_TUNNEL
:
729 case OFPUTIL_NXAST_SET_TUNNEL64
:
730 tunnel
= ofpact_put_SET_TUNNEL(ofpacts
);
731 tunnel
->ofpact
.compat
= code
;
732 error
= str_to_u64(arg
, &tunnel
->tun_id
);
735 case OFPUTIL_NXAST_WRITE_METADATA
:
736 error
= parse_metadata(ofpacts
, arg
);
739 case OFPUTIL_NXAST_SET_QUEUE
:
740 error
= str_to_u32(arg
, &ofpact_put_SET_QUEUE(ofpacts
)->queue_id
);
743 case OFPUTIL_NXAST_POP_QUEUE
:
744 ofpact_put_POP_QUEUE(ofpacts
);
747 case OFPUTIL_NXAST_REG_MOVE
:
748 error
= nxm_parse_reg_move(ofpact_put_REG_MOVE(ofpacts
), arg
);
751 case OFPUTIL_NXAST_REG_LOAD
:
752 error
= nxm_parse_reg_load(ofpact_put_REG_LOAD(ofpacts
), arg
);
755 case OFPUTIL_NXAST_NOTE
:
756 error
= parse_note(arg
, ofpacts
);
759 case OFPUTIL_NXAST_MULTIPATH
:
760 error
= multipath_parse(ofpact_put_MULTIPATH(ofpacts
), arg
);
763 case OFPUTIL_NXAST_BUNDLE
:
764 error
= bundle_parse(arg
, ofpacts
);
767 case OFPUTIL_NXAST_BUNDLE_LOAD
:
768 error
= bundle_parse_load(arg
, ofpacts
);
771 case OFPUTIL_NXAST_RESUBMIT_TABLE
:
772 case OFPUTIL_NXAST_OUTPUT_REG
:
773 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS
:
776 case OFPUTIL_NXAST_LEARN
:
777 error
= learn_parse(arg
, ofpacts
);
780 case OFPUTIL_NXAST_EXIT
:
781 ofpact_put_EXIT(ofpacts
);
784 case OFPUTIL_NXAST_DEC_TTL
:
785 error
= parse_dec_ttl(ofpacts
, arg
);
788 case OFPUTIL_NXAST_SET_MPLS_TTL
:
789 case OFPUTIL_OFPAT11_SET_MPLS_TTL
:
790 error
= parse_set_mpls_ttl(ofpacts
, arg
);
793 case OFPUTIL_OFPAT11_DEC_MPLS_TTL
:
794 case OFPUTIL_NXAST_DEC_MPLS_TTL
:
795 ofpact_put_DEC_MPLS_TTL(ofpacts
);
798 case OFPUTIL_NXAST_FIN_TIMEOUT
:
799 error
= parse_fin_timeout(ofpacts
, arg
);
802 case OFPUTIL_NXAST_CONTROLLER
:
803 error
= parse_controller(ofpacts
, arg
);
806 case OFPUTIL_OFPAT11_PUSH_MPLS
:
807 case OFPUTIL_NXAST_PUSH_MPLS
:
808 error
= str_to_u16(arg
, "push_mpls", ðertype
);
810 ofpact_put_PUSH_MPLS(ofpacts
)->ethertype
= htons(ethertype
);
814 case OFPUTIL_OFPAT11_POP_MPLS
:
815 case OFPUTIL_NXAST_POP_MPLS
:
816 error
= str_to_u16(arg
, "pop_mpls", ðertype
);
818 ofpact_put_POP_MPLS(ofpacts
)->ethertype
= htons(ethertype
);
822 case OFPUTIL_NXAST_STACK_PUSH
:
823 error
= nxm_parse_stack_action(ofpact_put_STACK_PUSH(ofpacts
), arg
);
825 case OFPUTIL_NXAST_STACK_POP
:
826 error
= nxm_parse_stack_action(ofpact_put_STACK_POP(ofpacts
), arg
);
829 case OFPUTIL_NXAST_SAMPLE
:
830 error
= parse_sample(ofpacts
, arg
);
835 ofpacts
->size
= orig_size
;
840 /* Parses action 'act', with argument 'arg', and appends a parsed version to
843 * 'n_actions' specifies the number of actions already parsed (for proper
844 * handling of "drop" actions).
846 * Returns NULL if successful, otherwise a malloc()'d string describing the
847 * error. The caller is responsible for freeing the returned string. */
848 static char * WARN_UNUSED_RESULT
849 str_to_ofpact__(char *pos
, char *act
, char *arg
,
850 struct ofpbuf
*ofpacts
, int n_actions
,
851 enum ofputil_protocol
*usable_protocols
)
853 int code
= ofputil_action_code_from_name(act
);
855 return parse_named_action(code
, arg
, ofpacts
, usable_protocols
);
856 } else if (!strcasecmp(act
, "drop")) {
858 return xstrdup("Drop actions must not be preceded by other "
860 } else if (ofputil_parse_key_value(&pos
, &act
, &arg
)) {
861 return xstrdup("Drop actions must not be followed by other "
866 if (ofputil_port_from_string(act
, &port
)) {
867 ofpact_put_OUTPUT(ofpacts
)->port
= port
;
869 return xasprintf("Unknown action: %s", act
);
876 /* Parses 'str' as a series of actions, and appends them to 'ofpacts'.
878 * Returns NULL if successful, otherwise a malloc()'d string describing the
879 * error. The caller is responsible for freeing the returned string. */
880 static char * WARN_UNUSED_RESULT
881 str_to_ofpacts(char *str
, struct ofpbuf
*ofpacts
,
882 enum ofputil_protocol
*usable_protocols
)
884 size_t orig_size
= ofpacts
->size
;
885 char *pos
, *act
, *arg
;
891 while (ofputil_parse_key_value(&pos
, &act
, &arg
)) {
892 char *error
= str_to_ofpact__(pos
, act
, arg
, ofpacts
, n_actions
,
895 ofpacts
->size
= orig_size
;
901 error
= ofpacts_verify(ofpacts
->data
, ofpacts
->size
);
903 ofpacts
->size
= orig_size
;
904 return xstrdup("Incorrect action ordering");
911 /* Parses 'arg' as the argument to instruction 'type', and appends such an
912 * instruction to 'ofpacts'.
914 * Returns NULL if successful, otherwise a malloc()'d string describing the
915 * error. The caller is responsible for freeing the returned string. */
916 static char * WARN_UNUSED_RESULT
917 parse_named_instruction(enum ovs_instruction_type type
,
918 char *arg
, struct ofpbuf
*ofpacts
,
919 enum ofputil_protocol
*usable_protocols
)
921 char *error_s
= NULL
;
924 *usable_protocols
&= OFPUTIL_P_OF11_UP
;
927 case OVSINST_OFPIT11_APPLY_ACTIONS
:
928 NOT_REACHED(); /* This case is handled by str_to_inst_ofpacts() */
931 case OVSINST_OFPIT11_WRITE_ACTIONS
:
933 error_s
= xstrdup("instruction write-actions is not supported yet");
936 case OVSINST_OFPIT11_CLEAR_ACTIONS
:
937 ofpact_put_CLEAR_ACTIONS(ofpacts
);
940 case OVSINST_OFPIT13_METER
:
941 *usable_protocols
&= OFPUTIL_P_OF13_UP
;
942 error_s
= str_to_u32(arg
, &ofpact_put_METER(ofpacts
)->meter_id
);
945 case OVSINST_OFPIT11_WRITE_METADATA
:
946 *usable_protocols
&= OFPUTIL_P_NXM_OF11_UP
;
947 error_s
= parse_metadata(ofpacts
, arg
);
950 case OVSINST_OFPIT11_GOTO_TABLE
: {
951 struct ofpact_goto_table
*ogt
= ofpact_put_GOTO_TABLE(ofpacts
);
952 char *table_s
= strsep(&arg
, ",");
953 if (!table_s
|| !table_s
[0]) {
954 return xstrdup("instruction goto-table needs table id");
956 error_s
= str_to_u8(table_s
, "table", &ogt
->table_id
);
965 /* If write_metadata is specified as an action AND an instruction, ofpacts
967 error
= ofpacts_verify(ofpacts
->data
, ofpacts
->size
);
969 return xstrdup("Incorrect instruction ordering");
974 /* Parses 'str' as a series of instructions, and appends them to 'ofpacts'.
976 * Returns NULL if successful, otherwise a malloc()'d string describing the
977 * error. The caller is responsible for freeing the returned string. */
978 static char * WARN_UNUSED_RESULT
979 str_to_inst_ofpacts(char *str
, struct ofpbuf
*ofpacts
,
980 enum ofputil_protocol
*usable_protocols
)
982 size_t orig_size
= ofpacts
->size
;
983 char *pos
, *inst
, *arg
;
985 const char *prev_inst
= NULL
;
990 while (ofputil_parse_key_value(&pos
, &inst
, &arg
)) {
991 type
= ovs_instruction_type_from_name(inst
);
993 char *error
= str_to_ofpact__(pos
, inst
, arg
, ofpacts
, n_actions
,
996 ofpacts
->size
= orig_size
;
1000 type
= OVSINST_OFPIT11_APPLY_ACTIONS
;
1001 if (prev_type
== type
) {
1005 } else if (type
== OVSINST_OFPIT11_APPLY_ACTIONS
) {
1006 ofpacts
->size
= orig_size
;
1007 return xasprintf("%s isn't supported. Just write actions then "
1008 "it is interpreted as apply_actions", inst
);
1010 char *error
= parse_named_instruction(type
, arg
, ofpacts
,
1013 ofpacts
->size
= orig_size
;
1018 if (type
<= prev_type
) {
1019 ofpacts
->size
= orig_size
;
1020 if (type
== prev_type
) {
1021 return xasprintf("instruction %s may be specified only once",
1024 return xasprintf("instruction %s must be specified before %s",
1033 ofpact_pad(ofpacts
);
1045 parse_protocol(const char *name
, const struct protocol
**p_out
)
1047 static const struct protocol protocols
[] = {
1048 { "ip", ETH_TYPE_IP
, 0 },
1049 { "arp", ETH_TYPE_ARP
, 0 },
1050 { "icmp", ETH_TYPE_IP
, IPPROTO_ICMP
},
1051 { "tcp", ETH_TYPE_IP
, IPPROTO_TCP
},
1052 { "udp", ETH_TYPE_IP
, IPPROTO_UDP
},
1053 { "sctp", ETH_TYPE_IP
, IPPROTO_SCTP
},
1054 { "ipv6", ETH_TYPE_IPV6
, 0 },
1055 { "ip6", ETH_TYPE_IPV6
, 0 },
1056 { "icmp6", ETH_TYPE_IPV6
, IPPROTO_ICMPV6
},
1057 { "tcp6", ETH_TYPE_IPV6
, IPPROTO_TCP
},
1058 { "udp6", ETH_TYPE_IPV6
, IPPROTO_UDP
},
1059 { "sctp6", ETH_TYPE_IPV6
, IPPROTO_SCTP
},
1060 { "rarp", ETH_TYPE_RARP
, 0},
1061 { "mpls", ETH_TYPE_MPLS
, 0 },
1062 { "mplsm", ETH_TYPE_MPLS_MCAST
, 0 },
1064 const struct protocol
*p
;
1066 for (p
= protocols
; p
< &protocols
[ARRAY_SIZE(protocols
)]; p
++) {
1067 if (!strcmp(p
->name
, name
)) {
1076 /* Parses 's' as the (possibly masked) value of field 'mf', and updates
1077 * 'match' appropriately. Restricts the set of usable protocols to ones
1078 * supporting the parsed field.
1080 * Returns NULL if successful, otherwise a malloc()'d string describing the
1081 * error. The caller is responsible for freeing the returned string. */
1082 static char * WARN_UNUSED_RESULT
1083 parse_field(const struct mf_field
*mf
, const char *s
, struct match
*match
,
1084 enum ofputil_protocol
*usable_protocols
)
1086 union mf_value value
, mask
;
1089 error
= mf_parse(mf
, s
, &value
, &mask
);
1091 *usable_protocols
&= mf_set(mf
, &value
, &mask
, match
);
1096 static char * WARN_UNUSED_RESULT
1097 parse_ofp_str__(struct ofputil_flow_mod
*fm
, int command
, char *string
,
1098 enum ofputil_protocol
*usable_protocols
)
1101 F_OUT_PORT
= 1 << 0,
1104 F_PRIORITY
= 1 << 4,
1107 char *save_ptr
= NULL
;
1108 char *act_str
= NULL
;
1111 *usable_protocols
= OFPUTIL_P_ANY
;
1115 fields
= F_OUT_PORT
;
1119 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
;
1123 fields
= F_OUT_PORT
;
1126 case OFPFC_DELETE_STRICT
:
1127 fields
= F_OUT_PORT
| F_PRIORITY
;
1131 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
;
1134 case OFPFC_MODIFY_STRICT
:
1135 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
;
1142 match_init_catchall(&fm
->match
);
1143 fm
->priority
= OFP_DEFAULT_PRIORITY
;
1144 fm
->cookie
= htonll(0);
1145 fm
->cookie_mask
= htonll(0);
1146 if (command
== OFPFC_MODIFY
|| command
== OFPFC_MODIFY_STRICT
) {
1147 /* For modify, by default, don't update the cookie. */
1148 fm
->new_cookie
= htonll(UINT64_MAX
);
1150 fm
->new_cookie
= htonll(0);
1152 fm
->modify_cookie
= false;
1153 fm
->table_id
= 0xff;
1154 fm
->command
= command
;
1155 fm
->idle_timeout
= OFP_FLOW_PERMANENT
;
1156 fm
->hard_timeout
= OFP_FLOW_PERMANENT
;
1157 fm
->buffer_id
= UINT32_MAX
;
1158 fm
->out_port
= OFPP_ANY
;
1160 if (fields
& F_ACTIONS
) {
1161 act_str
= strstr(string
, "action");
1163 return xstrdup("must specify an action");
1167 act_str
= strchr(act_str
+ 1, '=');
1169 return xstrdup("must specify an action");
1174 for (name
= strtok_r(string
, "=, \t\r\n", &save_ptr
); name
;
1175 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
1176 const struct protocol
*p
;
1179 if (parse_protocol(name
, &p
)) {
1180 match_set_dl_type(&fm
->match
, htons(p
->dl_type
));
1182 match_set_nw_proto(&fm
->match
, p
->nw_proto
);
1184 } else if (fields
& F_FLAGS
&& !strcmp(name
, "send_flow_rem")) {
1185 fm
->flags
|= OFPUTIL_FF_SEND_FLOW_REM
;
1186 } else if (fields
& F_FLAGS
&& !strcmp(name
, "check_overlap")) {
1187 fm
->flags
|= OFPUTIL_FF_CHECK_OVERLAP
;
1188 } else if (fields
& F_FLAGS
&& !strcmp(name
, "reset_counts")) {
1189 fm
->flags
|= OFPUTIL_FF_RESET_COUNTS
;
1190 *usable_protocols
&= OFPUTIL_P_OF12_UP
;
1191 } else if (fields
& F_FLAGS
&& !strcmp(name
, "no_packet_counts")) {
1192 fm
->flags
|= OFPUTIL_FF_NO_PKT_COUNTS
;
1193 *usable_protocols
&= OFPUTIL_P_OF13_UP
;
1194 } else if (fields
& F_FLAGS
&& !strcmp(name
, "no_byte_counts")) {
1195 fm
->flags
|= OFPUTIL_FF_NO_BYT_COUNTS
;
1196 *usable_protocols
&= OFPUTIL_P_OF13_UP
;
1200 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
1202 return xasprintf("field %s missing value", name
);
1205 if (!strcmp(name
, "table")) {
1206 error
= str_to_u8(value
, "table", &fm
->table_id
);
1207 if (fm
->table_id
!= 0xff) {
1208 *usable_protocols
&= OFPUTIL_P_TID
;
1210 } else if (!strcmp(name
, "out_port")) {
1211 if (!ofputil_port_from_string(value
, &fm
->out_port
)) {
1212 error
= xasprintf("%s is not a valid OpenFlow port",
1215 } else if (fields
& F_PRIORITY
&& !strcmp(name
, "priority")) {
1216 uint16_t priority
= 0;
1218 error
= str_to_u16(value
, name
, &priority
);
1219 fm
->priority
= priority
;
1220 } else if (fields
& F_TIMEOUT
&& !strcmp(name
, "idle_timeout")) {
1221 error
= str_to_u16(value
, name
, &fm
->idle_timeout
);
1222 } else if (fields
& F_TIMEOUT
&& !strcmp(name
, "hard_timeout")) {
1223 error
= str_to_u16(value
, name
, &fm
->hard_timeout
);
1224 } else if (!strcmp(name
, "cookie")) {
1225 char *mask
= strchr(value
, '/');
1228 /* A mask means we're searching for a cookie. */
1229 if (command
== OFPFC_ADD
) {
1230 return xstrdup("flow additions cannot use "
1234 error
= str_to_be64(value
, &fm
->cookie
);
1238 error
= str_to_be64(mask
+ 1, &fm
->cookie_mask
);
1240 /* Matching of the cookie is only supported through NXM or
1242 if (fm
->cookie_mask
!= htonll(0)) {
1243 *usable_protocols
&= OFPUTIL_P_NXM_OF11_UP
;
1246 /* No mask means that the cookie is being set. */
1247 if (command
!= OFPFC_ADD
&& command
!= OFPFC_MODIFY
1248 && command
!= OFPFC_MODIFY_STRICT
) {
1249 return xstrdup("cannot set cookie");
1251 error
= str_to_be64(value
, &fm
->new_cookie
);
1252 fm
->modify_cookie
= true;
1254 } else if (mf_from_name(name
)) {
1255 error
= parse_field(mf_from_name(name
), value
, &fm
->match
,
1257 } else if (!strcmp(name
, "duration")
1258 || !strcmp(name
, "n_packets")
1259 || !strcmp(name
, "n_bytes")
1260 || !strcmp(name
, "idle_age")
1261 || !strcmp(name
, "hard_age")) {
1262 /* Ignore these, so that users can feed the output of
1263 * "ovs-ofctl dump-flows" back into commands that parse
1266 error
= xasprintf("unknown keyword %s", name
);
1274 /* Check for usable protocol interdependencies between match fields. */
1275 if (fm
->match
.flow
.dl_type
== htons(ETH_TYPE_IPV6
)) {
1276 const struct flow_wildcards
*wc
= &fm
->match
.wc
;
1277 /* Only NXM and OXM support matching L3 and L4 fields within IPv6.
1279 * (IPv6 specific fields as well as arp_sha, arp_tha, nw_frag, and
1280 * nw_ttl are covered elsewhere so they don't need to be included in
1283 if (wc
->masks
.nw_proto
|| wc
->masks
.nw_tos
1284 || wc
->masks
.tp_src
|| wc
->masks
.tp_dst
) {
1285 *usable_protocols
&= OFPUTIL_P_NXM_OXM_ANY
;
1288 if (!fm
->cookie_mask
&& fm
->new_cookie
== htonll(UINT64_MAX
)
1289 && (command
== OFPFC_MODIFY
|| command
== OFPFC_MODIFY_STRICT
)) {
1290 /* On modifies without a mask, we are supposed to add a flow if
1291 * one does not exist. If a cookie wasn't been specified, use a
1292 * default of zero. */
1293 fm
->new_cookie
= htonll(0);
1295 if (fields
& F_ACTIONS
) {
1296 struct ofpbuf ofpacts
;
1299 ofpbuf_init(&ofpacts
, 32);
1300 error
= str_to_inst_ofpacts(act_str
, &ofpacts
, usable_protocols
);
1304 err
= ofpacts_check(ofpacts
.data
, ofpacts
.size
, &fm
->match
.flow
,
1307 error
= xasprintf("actions are invalid with specified match "
1308 "(%s)", ofperr_to_string(err
));
1312 ofpbuf_uninit(&ofpacts
);
1316 fm
->ofpacts_len
= ofpacts
.size
;
1317 fm
->ofpacts
= ofpbuf_steal_data(&ofpacts
);
1319 fm
->ofpacts_len
= 0;
1326 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
1327 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
1328 * Returns the set of usable protocols in '*usable_protocols'.
1330 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
1331 * constant for 'command'. To parse syntax for an OFPST_FLOW or
1332 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'.
1334 * Returns NULL if successful, otherwise a malloc()'d string describing the
1335 * error. The caller is responsible for freeing the returned string. */
1336 char * WARN_UNUSED_RESULT
1337 parse_ofp_str(struct ofputil_flow_mod
*fm
, int command
, const char *str_
,
1338 enum ofputil_protocol
*usable_protocols
)
1340 char *string
= xstrdup(str_
);
1343 error
= parse_ofp_str__(fm
, command
, string
, usable_protocols
);
1346 fm
->ofpacts_len
= 0;
1353 static char * WARN_UNUSED_RESULT
1354 parse_ofp_meter_mod_str__(struct ofputil_meter_mod
*mm
, char *string
,
1355 struct ofpbuf
*bands
, int command
,
1356 enum ofputil_protocol
*usable_protocols
)
1363 char *save_ptr
= NULL
;
1364 char *band_str
= NULL
;
1367 /* Meters require at least OF 1.3. */
1368 *usable_protocols
&= OFPUTIL_P_OF13_UP
;
1376 fields
= F_METER
| F_FLAGS
| F_BANDS
;
1379 case OFPMC13_DELETE
:
1383 case OFPMC13_MODIFY
:
1384 fields
= F_METER
| F_FLAGS
| F_BANDS
;
1391 mm
->command
= command
;
1392 mm
->meter
.meter_id
= 0;
1393 mm
->meter
.flags
= 0;
1394 if (fields
& F_BANDS
) {
1395 band_str
= strstr(string
, "band");
1397 return xstrdup("must specify bands");
1401 band_str
= strchr(band_str
+ 1, '=');
1403 return xstrdup("must specify bands");
1408 for (name
= strtok_r(string
, "=, \t\r\n", &save_ptr
); name
;
1409 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
1411 if (fields
& F_FLAGS
&& !strcmp(name
, "kbps")) {
1412 mm
->meter
.flags
|= OFPMF13_KBPS
;
1413 } else if (fields
& F_FLAGS
&& !strcmp(name
, "pktps")) {
1414 mm
->meter
.flags
|= OFPMF13_PKTPS
;
1415 } else if (fields
& F_FLAGS
&& !strcmp(name
, "burst")) {
1416 mm
->meter
.flags
|= OFPMF13_BURST
;
1417 } else if (fields
& F_FLAGS
&& !strcmp(name
, "stats")) {
1418 mm
->meter
.flags
|= OFPMF13_STATS
;
1422 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
1424 return xasprintf("field %s missing value", name
);
1427 if (!strcmp(name
, "meter")) {
1428 if (!strcmp(value
, "all")) {
1429 mm
->meter
.meter_id
= OFPM13_ALL
;
1430 } else if (!strcmp(value
, "controller")) {
1431 mm
->meter
.meter_id
= OFPM13_CONTROLLER
;
1432 } else if (!strcmp(value
, "slowpath")) {
1433 mm
->meter
.meter_id
= OFPM13_SLOWPATH
;
1435 char *error
= str_to_u32(value
, &mm
->meter
.meter_id
);
1439 if (mm
->meter
.meter_id
> OFPM13_MAX
) {
1440 return xasprintf("invalid value for %s", name
);
1444 return xasprintf("unknown keyword %s", name
);
1448 if (fields
& F_METER
&& !mm
->meter
.meter_id
) {
1449 return xstrdup("must specify 'meter'");
1451 if (fields
& F_FLAGS
&& !mm
->meter
.flags
) {
1452 return xstrdup("meter must specify either 'kbps' or 'pktps'");
1455 if (fields
& F_BANDS
) {
1456 uint16_t n_bands
= 0;
1457 struct ofputil_meter_band
*band
= NULL
;
1460 for (name
= strtok_r(band_str
, "=, \t\r\n", &save_ptr
); name
;
1461 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
1465 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
1467 return xasprintf("field %s missing value", name
);
1470 if (!strcmp(name
, "type")) {
1471 /* Start a new band */
1472 band
= ofpbuf_put_zeros(bands
, sizeof *band
);
1475 if (!strcmp(value
, "drop")) {
1476 band
->type
= OFPMBT13_DROP
;
1477 } else if (!strcmp(value
, "dscp_remark")) {
1478 band
->type
= OFPMBT13_DSCP_REMARK
;
1480 return xasprintf("field %s unknown value %s", name
, value
);
1482 } else if (!band
|| !band
->type
) {
1483 return xstrdup("band must start with the 'type' keyword");
1484 } else if (!strcmp(name
, "rate")) {
1485 char *error
= str_to_u32(value
, &band
->rate
);
1489 } else if (!strcmp(name
, "burst_size")) {
1490 char *error
= str_to_u32(value
, &band
->burst_size
);
1494 } else if (!strcmp(name
, "prec_level")) {
1495 char *error
= str_to_u8(value
, name
, &band
->prec_level
);
1500 return xasprintf("unknown keyword %s", name
);
1503 /* validate bands */
1505 return xstrdup("meter must have bands");
1508 mm
->meter
.n_bands
= n_bands
;
1509 mm
->meter
.bands
= ofpbuf_steal_data(bands
);
1511 for (i
= 0; i
< n_bands
; ++i
) {
1512 band
= &mm
->meter
.bands
[i
];
1515 return xstrdup("band must have 'type'");
1517 if (band
->type
== OFPMBT13_DSCP_REMARK
) {
1518 if (!band
->prec_level
) {
1519 return xstrdup("'dscp_remark' band must have"
1523 if (band
->prec_level
) {
1524 return xstrdup("Only 'dscp_remark' band may have"
1529 return xstrdup("band must have 'rate'");
1531 if (mm
->meter
.flags
& OFPMF13_BURST
) {
1532 if (!band
->burst_size
) {
1533 return xstrdup("band must have 'burst_size' "
1534 "when 'burst' flag is set");
1537 if (band
->burst_size
) {
1538 return xstrdup("band may have 'burst_size' only "
1539 "when 'burst' flag is set");
1544 mm
->meter
.n_bands
= 0;
1545 mm
->meter
.bands
= NULL
;
1551 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
1552 * page) into 'mm' for sending the specified meter_mod 'command' to a switch.
1554 * Returns NULL if successful, otherwise a malloc()'d string describing the
1555 * error. The caller is responsible for freeing the returned string. */
1556 char * WARN_UNUSED_RESULT
1557 parse_ofp_meter_mod_str(struct ofputil_meter_mod
*mm
, const char *str_
,
1558 int command
, enum ofputil_protocol
*usable_protocols
)
1560 struct ofpbuf bands
;
1564 ofpbuf_init(&bands
, 64);
1565 string
= xstrdup(str_
);
1567 error
= parse_ofp_meter_mod_str__(mm
, string
, &bands
, command
,
1571 ofpbuf_uninit(&bands
);
1576 static char * WARN_UNUSED_RESULT
1577 parse_flow_monitor_request__(struct ofputil_flow_monitor_request
*fmr
,
1578 const char *str_
, char *string
,
1579 enum ofputil_protocol
*usable_protocols
)
1581 static atomic_uint32_t id
= ATOMIC_VAR_INIT(0);
1582 char *save_ptr
= NULL
;
1585 atomic_add(&id
, 1, &fmr
->id
);
1587 fmr
->flags
= (NXFMF_INITIAL
| NXFMF_ADD
| NXFMF_DELETE
| NXFMF_MODIFY
1588 | NXFMF_OWN
| NXFMF_ACTIONS
);
1589 fmr
->out_port
= OFPP_NONE
;
1590 fmr
->table_id
= 0xff;
1591 match_init_catchall(&fmr
->match
);
1593 for (name
= strtok_r(string
, "=, \t\r\n", &save_ptr
); name
;
1594 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
1595 const struct protocol
*p
;
1597 if (!strcmp(name
, "!initial")) {
1598 fmr
->flags
&= ~NXFMF_INITIAL
;
1599 } else if (!strcmp(name
, "!add")) {
1600 fmr
->flags
&= ~NXFMF_ADD
;
1601 } else if (!strcmp(name
, "!delete")) {
1602 fmr
->flags
&= ~NXFMF_DELETE
;
1603 } else if (!strcmp(name
, "!modify")) {
1604 fmr
->flags
&= ~NXFMF_MODIFY
;
1605 } else if (!strcmp(name
, "!actions")) {
1606 fmr
->flags
&= ~NXFMF_ACTIONS
;
1607 } else if (!strcmp(name
, "!own")) {
1608 fmr
->flags
&= ~NXFMF_OWN
;
1609 } else if (parse_protocol(name
, &p
)) {
1610 match_set_dl_type(&fmr
->match
, htons(p
->dl_type
));
1612 match_set_nw_proto(&fmr
->match
, p
->nw_proto
);
1617 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
1619 return xasprintf("%s: field %s missing value", str_
, name
);
1622 if (!strcmp(name
, "table")) {
1623 char *error
= str_to_u8(value
, "table", &fmr
->table_id
);
1627 } else if (!strcmp(name
, "out_port")) {
1628 fmr
->out_port
= u16_to_ofp(atoi(value
));
1629 } else if (mf_from_name(name
)) {
1632 error
= parse_field(mf_from_name(name
), value
, &fmr
->match
,
1638 return xasprintf("%s: unknown keyword %s", str_
, name
);
1645 /* Convert 'str_' (as described in the documentation for the "monitor" command
1646 * in the ovs-ofctl man page) into 'fmr'.
1648 * Returns NULL if successful, otherwise a malloc()'d string describing the
1649 * error. The caller is responsible for freeing the returned string. */
1650 char * WARN_UNUSED_RESULT
1651 parse_flow_monitor_request(struct ofputil_flow_monitor_request
*fmr
,
1653 enum ofputil_protocol
*usable_protocols
)
1655 char *string
= xstrdup(str_
);
1656 char *error
= parse_flow_monitor_request__(fmr
, str_
, string
,
1662 /* Parses 's' as a set of OpenFlow actions and appends the actions to
1665 * Returns NULL if successful, otherwise a malloc()'d string describing the
1666 * error. The caller is responsible for freeing the returned string. */
1667 char * WARN_UNUSED_RESULT
1668 parse_ofpacts(const char *s_
, struct ofpbuf
*ofpacts
,
1669 enum ofputil_protocol
*usable_protocols
)
1671 char *s
= xstrdup(s_
);
1674 *usable_protocols
= OFPUTIL_P_ANY
;
1676 error
= str_to_ofpacts(s
, ofpacts
, usable_protocols
);
1682 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
1683 * (one of OFPFC_*) into 'fm'.
1685 * Returns NULL if successful, otherwise a malloc()'d string describing the
1686 * error. The caller is responsible for freeing the returned string. */
1687 char * WARN_UNUSED_RESULT
1688 parse_ofp_flow_mod_str(struct ofputil_flow_mod
*fm
, const char *string
,
1690 enum ofputil_protocol
*usable_protocols
)
1692 char *error
= parse_ofp_str(fm
, command
, string
, usable_protocols
);
1694 /* Normalize a copy of the match. This ensures that non-normalized
1695 * flows get logged but doesn't affect what gets sent to the switch, so
1696 * that the switch can do whatever it likes with the flow. */
1697 struct match match_copy
= fm
->match
;
1698 ofputil_normalize_match(&match_copy
);
1704 /* Opens file 'file_name' and reads each line as a flow_mod of the specified
1705 * type (one of OFPFC_*). Stores each flow_mod in '*fm', an array allocated
1706 * on the caller's behalf, and the number of flow_mods in '*n_fms'.
1708 * Returns NULL if successful, otherwise a malloc()'d string describing the
1709 * error. The caller is responsible for freeing the returned string. */
1710 char * WARN_UNUSED_RESULT
1711 parse_ofp_flow_mod_file(const char *file_name
, uint16_t command
,
1712 struct ofputil_flow_mod
**fms
, size_t *n_fms
,
1713 enum ofputil_protocol
*usable_protocols
)
1715 size_t allocated_fms
;
1720 *usable_protocols
= OFPUTIL_P_ANY
;
1725 stream
= !strcmp(file_name
, "-") ? stdin
: fopen(file_name
, "r");
1726 if (stream
== NULL
) {
1727 return xasprintf("%s: open failed (%s)",
1728 file_name
, ovs_strerror(errno
));
1731 allocated_fms
= *n_fms
;
1734 while (!ds_get_preprocessed_line(&s
, stream
, &line_number
)) {
1736 enum ofputil_protocol usable
;
1738 if (*n_fms
>= allocated_fms
) {
1739 *fms
= x2nrealloc(*fms
, &allocated_fms
, sizeof **fms
);
1741 error
= parse_ofp_flow_mod_str(&(*fms
)[*n_fms
], ds_cstr(&s
), command
,
1746 for (i
= 0; i
< *n_fms
; i
++) {
1747 free((*fms
)[i
].ofpacts
);
1754 if (stream
!= stdin
) {
1758 return xasprintf("%s:%d: %s", file_name
, line_number
, error
);
1760 *usable_protocols
&= usable
; /* Each line can narrow the set. */
1765 if (stream
!= stdin
) {
1771 char * WARN_UNUSED_RESULT
1772 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request
*fsr
,
1773 bool aggregate
, const char *string
,
1774 enum ofputil_protocol
*usable_protocols
)
1776 struct ofputil_flow_mod fm
;
1779 error
= parse_ofp_str(&fm
, -1, string
, usable_protocols
);
1784 /* Special table ID support not required for stats requests. */
1785 if (*usable_protocols
& OFPUTIL_P_OF10_STD_TID
) {
1786 *usable_protocols
|= OFPUTIL_P_OF10_STD
;
1788 if (*usable_protocols
& OFPUTIL_P_OF10_NXM_TID
) {
1789 *usable_protocols
|= OFPUTIL_P_OF10_NXM
;
1792 fsr
->aggregate
= aggregate
;
1793 fsr
->cookie
= fm
.cookie
;
1794 fsr
->cookie_mask
= fm
.cookie_mask
;
1795 fsr
->match
= fm
.match
;
1796 fsr
->out_port
= fm
.out_port
;
1797 fsr
->table_id
= fm
.table_id
;
1801 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
1802 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
1803 * mf_field. Fields must be specified in a natural order for satisfying
1806 * Returns NULL on success, otherwise a malloc()'d string that explains the
1809 parse_ofp_exact_flow(struct flow
*flow
, const char *s
)
1811 char *pos
, *key
, *value_s
;
1815 memset(flow
, 0, sizeof *flow
);
1817 pos
= copy
= xstrdup(s
);
1818 while (ofputil_parse_key_value(&pos
, &key
, &value_s
)) {
1819 const struct protocol
*p
;
1820 if (parse_protocol(key
, &p
)) {
1821 if (flow
->dl_type
) {
1822 error
= xasprintf("%s: Ethernet type set multiple times", s
);
1825 flow
->dl_type
= htons(p
->dl_type
);
1828 if (flow
->nw_proto
) {
1829 error
= xasprintf("%s: network protocol set "
1830 "multiple times", s
);
1833 flow
->nw_proto
= p
->nw_proto
;
1836 const struct mf_field
*mf
;
1837 union mf_value value
;
1840 mf
= mf_from_name(key
);
1842 error
= xasprintf("%s: unknown field %s", s
, key
);
1846 if (!mf_are_prereqs_ok(mf
, flow
)) {
1847 error
= xasprintf("%s: prerequisites not met for setting %s",
1852 if (!mf_is_zero(mf
, flow
)) {
1853 error
= xasprintf("%s: field %s set multiple times", s
, key
);
1857 field_error
= mf_parse_value(mf
, value_s
, &value
);
1859 error
= xasprintf("%s: bad value for %s (%s)",
1860 s
, key
, field_error
);
1865 mf_set_flow_value(mf
, &value
, flow
);
1869 if (!flow
->in_port
.ofp_port
) {
1870 flow
->in_port
.ofp_port
= OFPP_NONE
;
1877 memset(flow
, 0, sizeof *flow
);