2 * Copyright (c) 2010, 2011, 2012 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"
27 #include "byte-order.h"
28 #include "dynamic-string.h"
30 #include "meta-flow.h"
31 #include "multipath.h"
34 #include "ofp-actions.h"
37 #include "openflow/openflow.h"
39 #include "socket-util.h"
43 VLOG_DEFINE_THIS_MODULE(ofp_parse
);
45 static void ofp_fatal(const char *flow
, bool verbose
, const char *format
, ...)
49 str_to_table_id(const char *str
)
53 if (!str_to_int(str
, 10, &table_id
) || table_id
< 0 || table_id
> 255) {
54 ovs_fatal(0, "invalid table \"%s\"", str
);
60 str_to_u16(const char *str
, const char *name
)
64 if (!str_to_int(str
, 0, &value
) || value
< 0 || value
> 65535) {
65 ovs_fatal(0, "invalid %s \"%s\"", name
, str
);
71 str_to_u32(const char *str
)
77 ovs_fatal(0, "missing required numeric argument");
81 value
= strtoul(str
, &tail
, 0);
82 if (errno
== EINVAL
|| errno
== ERANGE
|| *tail
) {
83 ovs_fatal(0, "invalid numeric format %s", str
);
89 str_to_u64(const char *str
)
95 ovs_fatal(0, "missing required numeric argument");
99 value
= strtoull(str
, &tail
, 0);
100 if (errno
== EINVAL
|| errno
== ERANGE
|| *tail
) {
101 ovs_fatal(0, "invalid numeric format %s", str
);
107 str_to_mac(const char *str
, uint8_t mac
[6])
109 if (sscanf(str
, ETH_ADDR_SCAN_FMT
, ETH_ADDR_SCAN_ARGS(mac
))
110 != ETH_ADDR_SCAN_COUNT
) {
111 ovs_fatal(0, "invalid mac address %s", str
);
116 str_to_ip(const char *str
, ovs_be32
*ip
)
118 struct in_addr in_addr
;
120 if (lookup_ip(str
, &in_addr
)) {
121 ovs_fatal(0, "%s: could not convert to IP address", str
);
123 *ip
= in_addr
.s_addr
;
127 parse_enqueue(char *arg
, struct ofpbuf
*ofpacts
)
130 char *port
= strtok_r(arg
, ":q", &sp
);
131 char *queue
= strtok_r(NULL
, "", &sp
);
132 struct ofpact_enqueue
*enqueue
;
134 if (port
== NULL
|| queue
== NULL
) {
135 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
138 enqueue
= ofpact_put_ENQUEUE(ofpacts
);
139 enqueue
->port
= str_to_u32(port
);
140 enqueue
->queue
= str_to_u32(queue
);
144 parse_output(char *arg
, struct ofpbuf
*ofpacts
)
146 if (strchr(arg
, '[')) {
147 struct ofpact_output_reg
*output_reg
;
149 output_reg
= ofpact_put_OUTPUT_REG(ofpacts
);
150 mf_parse_subfield(&output_reg
->src
, arg
);
151 output_reg
->max_len
= UINT16_MAX
;
153 struct ofpact_output
*output
;
155 output
= ofpact_put_OUTPUT(ofpacts
);
156 output
->port
= str_to_u32(arg
);
157 output
->max_len
= output
->port
== OFPP_CONTROLLER
? UINT16_MAX
: 0;
162 parse_resubmit(char *arg
, struct ofpbuf
*ofpacts
)
164 struct ofpact_resubmit
*resubmit
;
165 char *in_port_s
, *table_s
;
167 resubmit
= ofpact_put_RESUBMIT(ofpacts
);
169 in_port_s
= strsep(&arg
, ",");
170 if (in_port_s
&& in_port_s
[0]) {
171 if (!ofputil_port_from_string(in_port_s
, &resubmit
->in_port
)) {
172 resubmit
->in_port
= str_to_u32(in_port_s
);
175 resubmit
->in_port
= OFPP_IN_PORT
;
178 table_s
= strsep(&arg
, ",");
179 resubmit
->table_id
= table_s
&& table_s
[0] ? str_to_u32(table_s
) : 255;
181 if (resubmit
->in_port
== OFPP_IN_PORT
&& resubmit
->table_id
== 255) {
182 ovs_fatal(0, "at least one \"in_port\" or \"table\" must be specified "
188 parse_note(const char *arg
, struct ofpbuf
*ofpacts
)
190 struct ofpact_note
*note
;
192 note
= ofpact_put_NOTE(ofpacts
);
193 while (*arg
!= '\0') {
204 byte
= hexits_value(arg
, 2, &ok
);
206 ovs_fatal(0, "bad hex digit in `note' argument");
208 ofpbuf_put(ofpacts
, &byte
, 1);
215 ofpact_update_len(ofpacts
, ¬e
->ofpact
);
219 parse_fin_timeout(struct ofpbuf
*b
, char *arg
)
221 struct ofpact_fin_timeout
*oft
= ofpact_put_FIN_TIMEOUT(b
);
224 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
225 if (!strcmp(key
, "idle_timeout")) {
226 oft
->fin_idle_timeout
= str_to_u16(value
, key
);
227 } else if (!strcmp(key
, "hard_timeout")) {
228 oft
->fin_hard_timeout
= str_to_u16(value
, key
);
230 ovs_fatal(0, "invalid key '%s' in 'fin_timeout' argument", key
);
236 parse_controller(struct ofpbuf
*b
, char *arg
)
238 enum ofp_packet_in_reason reason
= OFPR_ACTION
;
239 uint16_t controller_id
= 0;
240 uint16_t max_len
= UINT16_MAX
;
244 } else if (strspn(arg
, "0123456789") == strlen(arg
)) {
245 max_len
= str_to_u16(arg
, "max_len");
249 while (ofputil_parse_key_value(&arg
, &name
, &value
)) {
250 if (!strcmp(name
, "reason")) {
251 if (!ofputil_packet_in_reason_from_string(value
, &reason
)) {
252 ovs_fatal(0, "unknown reason \"%s\"", value
);
254 } else if (!strcmp(name
, "max_len")) {
255 max_len
= str_to_u16(value
, "max_len");
256 } else if (!strcmp(name
, "id")) {
257 controller_id
= str_to_u16(value
, "id");
259 ovs_fatal(0, "unknown key \"%s\" parsing controller action",
265 if (reason
== OFPR_ACTION
&& controller_id
== 0) {
266 struct ofpact_output
*output
;
268 output
= ofpact_put_OUTPUT(b
);
269 output
->port
= OFPP_CONTROLLER
;
270 output
->max_len
= max_len
;
272 struct ofpact_controller
*controller
;
274 controller
= ofpact_put_CONTROLLER(b
);
275 controller
->max_len
= max_len
;
276 controller
->reason
= reason
;
277 controller
->controller_id
= controller_id
;
282 parse_dec_ttl(struct ofpbuf
*b
, char *arg
)
284 struct ofpact_cnt_ids
*ids
;
286 ids
= ofpact_put_DEC_TTL(b
);
291 ids
->ofpact
.compat
= OFPUTIL_NXAST_DEC_TTL
;
292 ofpbuf_put(b
, &id
, sizeof id
);
294 ids
->n_controllers
++;
298 ids
->ofpact
.compat
= OFPUTIL_NXAST_DEC_TTL_CNT_IDS
;
299 for (cntr
= strtok_r(arg
, ", ", &arg
); cntr
!= NULL
;
300 cntr
= strtok_r(NULL
, ", ", &arg
)) {
301 uint16_t id
= atoi(cntr
);
303 ofpbuf_put(b
, &id
, sizeof id
);
305 ids
->n_controllers
++;
307 if (!ids
->n_controllers
) {
308 ovs_fatal(0, "dec_ttl_cnt_ids: expected at least one controller "
313 ofpact_update_len(b
, &ids
->ofpact
);
317 parse_named_action(enum ofputil_action_code code
, const struct flow
*flow
,
318 char *arg
, struct ofpbuf
*ofpacts
)
320 struct ofpact_tunnel
*tunnel
;
327 case OFPUTIL_ACTION_INVALID
:
330 case OFPUTIL_OFPAT10_OUTPUT
:
331 case OFPUTIL_OFPAT11_OUTPUT
:
332 parse_output(arg
, ofpacts
);
335 case OFPUTIL_OFPAT10_SET_VLAN_VID
:
336 case OFPUTIL_OFPAT11_SET_VLAN_VID
:
337 vid
= str_to_u32(arg
);
338 if (vid
& ~VLAN_VID_MASK
) {
339 ovs_fatal(0, "%s: not a valid VLAN VID", arg
);
341 ofpact_put_SET_VLAN_VID(ofpacts
)->vlan_vid
= vid
;
344 case OFPUTIL_OFPAT10_SET_VLAN_PCP
:
345 case OFPUTIL_OFPAT11_SET_VLAN_PCP
:
346 pcp
= str_to_u32(arg
);
348 ovs_fatal(0, "%s: not a valid VLAN PCP", arg
);
350 ofpact_put_SET_VLAN_PCP(ofpacts
)->vlan_pcp
= pcp
;
353 case OFPUTIL_OFPAT10_STRIP_VLAN
:
354 ofpact_put_STRIP_VLAN(ofpacts
);
357 case OFPUTIL_OFPAT10_SET_DL_SRC
:
358 case OFPUTIL_OFPAT11_SET_DL_SRC
:
359 str_to_mac(arg
, ofpact_put_SET_ETH_SRC(ofpacts
)->mac
);
362 case OFPUTIL_OFPAT10_SET_DL_DST
:
363 case OFPUTIL_OFPAT11_SET_DL_DST
:
364 str_to_mac(arg
, ofpact_put_SET_ETH_DST(ofpacts
)->mac
);
367 case OFPUTIL_OFPAT10_SET_NW_SRC
:
368 case OFPUTIL_OFPAT11_SET_NW_SRC
:
370 ofpact_put_SET_IPV4_SRC(ofpacts
)->ipv4
= ip
;
373 case OFPUTIL_OFPAT10_SET_NW_DST
:
374 case OFPUTIL_OFPAT11_SET_NW_DST
:
376 ofpact_put_SET_IPV4_DST(ofpacts
)->ipv4
= ip
;
379 case OFPUTIL_OFPAT10_SET_NW_TOS
:
380 case OFPUTIL_OFPAT11_SET_NW_TOS
:
381 tos
= str_to_u32(arg
);
382 if (tos
& ~IP_DSCP_MASK
) {
383 ovs_fatal(0, "%s: not a valid TOS", arg
);
385 ofpact_put_SET_IPV4_DSCP(ofpacts
)->dscp
= tos
;
388 case OFPUTIL_OFPAT10_SET_TP_SRC
:
389 case OFPUTIL_OFPAT11_SET_TP_SRC
:
390 ofpact_put_SET_L4_SRC_PORT(ofpacts
)->port
= str_to_u32(arg
);
393 case OFPUTIL_OFPAT10_SET_TP_DST
:
394 case OFPUTIL_OFPAT11_SET_TP_DST
:
395 ofpact_put_SET_L4_DST_PORT(ofpacts
)->port
= str_to_u32(arg
);
398 case OFPUTIL_OFPAT10_ENQUEUE
:
399 parse_enqueue(arg
, ofpacts
);
402 case OFPUTIL_NXAST_RESUBMIT
:
403 parse_resubmit(arg
, ofpacts
);
406 case OFPUTIL_NXAST_SET_TUNNEL
:
407 case OFPUTIL_NXAST_SET_TUNNEL64
:
408 tunnel
= ofpact_put_SET_TUNNEL(ofpacts
);
409 tunnel
->ofpact
.compat
= code
;
410 tunnel
->tun_id
= str_to_u64(arg
);
413 case OFPUTIL_NXAST_SET_QUEUE
:
414 ofpact_put_SET_QUEUE(ofpacts
)->queue_id
= str_to_u32(arg
);
417 case OFPUTIL_NXAST_POP_QUEUE
:
418 ofpact_put_POP_QUEUE(ofpacts
);
421 case OFPUTIL_NXAST_REG_MOVE
:
422 nxm_parse_reg_move(ofpact_put_REG_MOVE(ofpacts
), arg
);
425 case OFPUTIL_NXAST_REG_LOAD
:
426 nxm_parse_reg_load(ofpact_put_REG_LOAD(ofpacts
), arg
);
429 case OFPUTIL_NXAST_NOTE
:
430 parse_note(arg
, ofpacts
);
433 case OFPUTIL_NXAST_MULTIPATH
:
434 multipath_parse(ofpact_put_MULTIPATH(ofpacts
), arg
);
437 case OFPUTIL_NXAST_AUTOPATH
:
438 autopath_parse(ofpact_put_AUTOPATH(ofpacts
), arg
);
441 case OFPUTIL_NXAST_BUNDLE
:
442 bundle_parse(arg
, ofpacts
);
445 case OFPUTIL_NXAST_BUNDLE_LOAD
:
446 bundle_parse_load(arg
, ofpacts
);
449 case OFPUTIL_NXAST_RESUBMIT_TABLE
:
450 case OFPUTIL_NXAST_OUTPUT_REG
:
451 case OFPUTIL_NXAST_DEC_TTL_CNT_IDS
:
454 case OFPUTIL_NXAST_LEARN
:
455 learn_parse(arg
, flow
, ofpacts
);
458 case OFPUTIL_NXAST_EXIT
:
459 ofpact_put_EXIT(ofpacts
);
462 case OFPUTIL_NXAST_DEC_TTL
:
463 parse_dec_ttl(ofpacts
, arg
);
466 case OFPUTIL_NXAST_FIN_TIMEOUT
:
467 parse_fin_timeout(ofpacts
, arg
);
470 case OFPUTIL_NXAST_CONTROLLER
:
471 parse_controller(ofpacts
, arg
);
477 str_to_ofpacts(const struct flow
*flow
, char *str
, struct ofpbuf
*ofpacts
)
479 char *pos
, *act
, *arg
;
484 while (ofputil_parse_key_value(&pos
, &act
, &arg
)) {
488 code
= ofputil_action_code_from_name(act
);
490 parse_named_action(code
, flow
, arg
, ofpacts
);
491 } else if (!strcasecmp(act
, "drop")) {
493 ovs_fatal(0, "Drop actions must not be preceded by other "
495 } else if (ofputil_parse_key_value(&pos
, &act
, &arg
)) {
496 ovs_fatal(0, "Drop actions must not be followed by other "
500 } else if (ofputil_port_from_string(act
, &port
)) {
501 ofpact_put_OUTPUT(ofpacts
)->port
= port
;
503 ovs_fatal(0, "Unknown action: %s", act
);
517 parse_protocol(const char *name
, const struct protocol
**p_out
)
519 static const struct protocol protocols
[] = {
520 { "ip", ETH_TYPE_IP
, 0 },
521 { "arp", ETH_TYPE_ARP
, 0 },
522 { "icmp", ETH_TYPE_IP
, IPPROTO_ICMP
},
523 { "tcp", ETH_TYPE_IP
, IPPROTO_TCP
},
524 { "udp", ETH_TYPE_IP
, IPPROTO_UDP
},
525 { "ipv6", ETH_TYPE_IPV6
, 0 },
526 { "ip6", ETH_TYPE_IPV6
, 0 },
527 { "icmp6", ETH_TYPE_IPV6
, IPPROTO_ICMPV6
},
528 { "tcp6", ETH_TYPE_IPV6
, IPPROTO_TCP
},
529 { "udp6", ETH_TYPE_IPV6
, IPPROTO_UDP
},
531 const struct protocol
*p
;
533 for (p
= protocols
; p
< &protocols
[ARRAY_SIZE(protocols
)]; p
++) {
534 if (!strcmp(p
->name
, name
)) {
544 ofp_fatal(const char *flow
, bool verbose
, const char *format
, ...)
549 fprintf(stderr
, "%s:\n", flow
);
552 va_start(args
, format
);
553 ovs_fatal_valist(0, format
, args
);
557 parse_field(const struct mf_field
*mf
, const char *s
, struct cls_rule
*rule
)
559 union mf_value value
, mask
;
562 error
= mf_parse(mf
, s
, &value
, &mask
);
564 ovs_fatal(0, "%s", error
);
567 mf_set(mf
, &value
, &mask
, rule
);
570 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
571 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
572 * If 'actions' is specified, an action must be in 'string' and may be expanded
575 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
576 * constant for 'command'. To parse syntax for an OFPST_FLOW or
577 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. */
579 parse_ofp_str(struct ofputil_flow_mod
*fm
, int command
, const char *str_
,
589 char *string
= xstrdup(str_
);
590 char *save_ptr
= NULL
;
591 char *act_str
= NULL
;
600 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
;
607 case OFPFC_DELETE_STRICT
:
608 fields
= F_OUT_PORT
| F_PRIORITY
;
612 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
;
615 case OFPFC_MODIFY_STRICT
:
616 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
;
623 cls_rule_init_catchall(&fm
->cr
, OFP_DEFAULT_PRIORITY
);
624 fm
->cookie
= htonll(0);
625 fm
->cookie_mask
= htonll(0);
626 if (command
== OFPFC_MODIFY
|| command
== OFPFC_MODIFY_STRICT
) {
627 /* For modify, by default, don't update the cookie. */
628 fm
->new_cookie
= htonll(UINT64_MAX
);
630 fm
->new_cookie
= htonll(0);
633 fm
->command
= command
;
634 fm
->idle_timeout
= OFP_FLOW_PERMANENT
;
635 fm
->hard_timeout
= OFP_FLOW_PERMANENT
;
636 fm
->buffer_id
= UINT32_MAX
;
637 fm
->out_port
= OFPP_NONE
;
639 if (fields
& F_ACTIONS
) {
640 act_str
= strstr(string
, "action");
642 ofp_fatal(str_
, verbose
, "must specify an action");
646 act_str
= strchr(act_str
+ 1, '=');
648 ofp_fatal(str_
, verbose
, "must specify an action");
653 for (name
= strtok_r(string
, "=, \t\r\n", &save_ptr
); name
;
654 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
655 const struct protocol
*p
;
657 if (parse_protocol(name
, &p
)) {
658 cls_rule_set_dl_type(&fm
->cr
, htons(p
->dl_type
));
660 cls_rule_set_nw_proto(&fm
->cr
, p
->nw_proto
);
662 } else if (fields
& F_FLAGS
&& !strcmp(name
, "send_flow_rem")) {
663 fm
->flags
|= OFPFF_SEND_FLOW_REM
;
664 } else if (fields
& F_FLAGS
&& !strcmp(name
, "check_overlap")) {
665 fm
->flags
|= OFPFF_CHECK_OVERLAP
;
669 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
671 ofp_fatal(str_
, verbose
, "field %s missing value", name
);
674 if (!strcmp(name
, "table")) {
675 fm
->table_id
= str_to_table_id(value
);
676 } else if (!strcmp(name
, "out_port")) {
677 fm
->out_port
= atoi(value
);
678 } else if (fields
& F_PRIORITY
&& !strcmp(name
, "priority")) {
679 fm
->cr
.priority
= str_to_u16(value
, name
);
680 } else if (fields
& F_TIMEOUT
&& !strcmp(name
, "idle_timeout")) {
681 fm
->idle_timeout
= str_to_u16(value
, name
);
682 } else if (fields
& F_TIMEOUT
&& !strcmp(name
, "hard_timeout")) {
683 fm
->hard_timeout
= str_to_u16(value
, name
);
684 } else if (!strcmp(name
, "cookie")) {
685 char *mask
= strchr(value
, '/');
688 /* A mask means we're searching for a cookie. */
689 if (command
== OFPFC_ADD
) {
690 ofp_fatal(str_
, verbose
, "flow additions cannot use "
694 fm
->cookie
= htonll(str_to_u64(value
));
695 fm
->cookie_mask
= htonll(str_to_u64(mask
+1));
697 /* No mask means that the cookie is being set. */
698 if (command
!= OFPFC_ADD
&& command
!= OFPFC_MODIFY
699 && command
!= OFPFC_MODIFY_STRICT
) {
700 ofp_fatal(str_
, verbose
, "cannot set cookie");
702 fm
->new_cookie
= htonll(str_to_u64(value
));
704 } else if (mf_from_name(name
)) {
705 parse_field(mf_from_name(name
), value
, &fm
->cr
);
706 } else if (!strcmp(name
, "duration")
707 || !strcmp(name
, "n_packets")
708 || !strcmp(name
, "n_bytes")) {
709 /* Ignore these, so that users can feed the output of
710 * "ovs-ofctl dump-flows" back into commands that parse
713 ofp_fatal(str_
, verbose
, "unknown keyword %s", name
);
717 if (!fm
->cookie_mask
&& fm
->new_cookie
== htonll(UINT64_MAX
)
718 && (command
== OFPFC_MODIFY
|| command
== OFPFC_MODIFY_STRICT
)) {
719 /* On modifies without a mask, we are supposed to add a flow if
720 * one does not exist. If a cookie wasn't been specified, use a
721 * default of zero. */
722 fm
->new_cookie
= htonll(0);
724 if (fields
& F_ACTIONS
) {
725 struct ofpbuf ofpacts
;
727 ofpbuf_init(&ofpacts
, 32);
728 str_to_ofpacts(&fm
->cr
.flow
, act_str
, &ofpacts
);
729 fm
->ofpacts_len
= ofpacts
.size
;
730 fm
->ofpacts
= ofpbuf_steal_data(&ofpacts
);
739 /* Convert 'str_' (as described in the documentation for the "monitor" command
740 * in the ovs-ofctl man page) into 'fmr'. */
742 parse_flow_monitor_request(struct ofputil_flow_monitor_request
*fmr
,
747 char *string
= xstrdup(str_
);
748 char *save_ptr
= NULL
;
752 fmr
->flags
= (NXFMF_INITIAL
| NXFMF_ADD
| NXFMF_DELETE
| NXFMF_MODIFY
753 | NXFMF_OWN
| NXFMF_ACTIONS
);
754 fmr
->out_port
= OFPP_NONE
;
755 fmr
->table_id
= 0xff;
756 cls_rule_init_catchall(&fmr
->match
, 0);
758 for (name
= strtok_r(string
, "=, \t\r\n", &save_ptr
); name
;
759 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
760 const struct protocol
*p
;
762 if (!strcmp(name
, "!initial")) {
763 fmr
->flags
&= ~NXFMF_INITIAL
;
764 } else if (!strcmp(name
, "!add")) {
765 fmr
->flags
&= ~NXFMF_ADD
;
766 } else if (!strcmp(name
, "!delete")) {
767 fmr
->flags
&= ~NXFMF_DELETE
;
768 } else if (!strcmp(name
, "!modify")) {
769 fmr
->flags
&= ~NXFMF_MODIFY
;
770 } else if (!strcmp(name
, "!actions")) {
771 fmr
->flags
&= ~NXFMF_ACTIONS
;
772 } else if (!strcmp(name
, "!own")) {
773 fmr
->flags
&= ~NXFMF_OWN
;
774 } else if (parse_protocol(name
, &p
)) {
775 cls_rule_set_dl_type(&fmr
->match
, htons(p
->dl_type
));
777 cls_rule_set_nw_proto(&fmr
->match
, p
->nw_proto
);
782 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
784 ovs_fatal(0, "%s: field %s missing value", str_
, name
);
787 if (!strcmp(name
, "table")) {
788 fmr
->table_id
= str_to_table_id(value
);
789 } else if (!strcmp(name
, "out_port")) {
790 fmr
->out_port
= atoi(value
);
791 } else if (mf_from_name(name
)) {
792 parse_field(mf_from_name(name
), value
, &fmr
->match
);
794 ovs_fatal(0, "%s: unknown keyword %s", str_
, name
);
801 /* Parses 's' as a set of OpenFlow actions and appends the actions to
804 * Prints an error on stderr and aborts the program if 's' syntax is
807 parse_ofpacts(const char *s_
, struct ofpbuf
*ofpacts
)
809 char *s
= xstrdup(s_
);
810 str_to_ofpacts(NULL
, s
, ofpacts
);
814 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
815 * (one of OFPFC_*) into 'fm'. */
817 parse_ofp_flow_mod_str(struct ofputil_flow_mod
*fm
, const char *string
,
818 uint16_t command
, bool verbose
)
820 struct cls_rule rule_copy
;
822 parse_ofp_str(fm
, command
, string
, verbose
);
824 /* Normalize a copy of the rule. This ensures that non-normalized flows
825 * get logged but doesn't affect what gets sent to the switch, so that the
826 * switch can do whatever it likes with the flow. */
828 ofputil_normalize_rule(&rule_copy
);
832 parse_ofp_flow_mod_file(const char *file_name
, uint16_t command
,
833 struct ofputil_flow_mod
**fms
, size_t *n_fms
)
835 size_t allocated_fms
;
839 stream
= !strcmp(file_name
, "-") ? stdin
: fopen(file_name
, "r");
840 if (stream
== NULL
) {
841 ovs_fatal(errno
, "%s: open", file_name
);
844 allocated_fms
= *n_fms
;
846 while (!ds_get_preprocessed_line(&s
, stream
)) {
847 if (*n_fms
>= allocated_fms
) {
848 *fms
= x2nrealloc(*fms
, &allocated_fms
, sizeof **fms
);
850 parse_ofp_flow_mod_str(&(*fms
)[*n_fms
], ds_cstr(&s
), command
, false);
855 if (stream
!= stdin
) {
861 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request
*fsr
,
862 bool aggregate
, const char *string
)
864 struct ofputil_flow_mod fm
;
866 parse_ofp_str(&fm
, -1, string
, false);
867 fsr
->aggregate
= aggregate
;
868 fsr
->cookie
= fm
.cookie
;
869 fsr
->cookie_mask
= fm
.cookie_mask
;
871 fsr
->out_port
= fm
.out_port
;
872 fsr
->table_id
= fm
.table_id
;
875 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
876 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
877 * mf_field. Fields must be specified in a natural order for satisfying
880 * Returns NULL on success, otherwise a malloc()'d string that explains the
883 parse_ofp_exact_flow(struct flow
*flow
, const char *s
)
885 char *pos
, *key
, *value_s
;
889 memset(flow
, 0, sizeof *flow
);
891 pos
= copy
= xstrdup(s
);
892 while (ofputil_parse_key_value(&pos
, &key
, &value_s
)) {
893 const struct protocol
*p
;
894 if (parse_protocol(key
, &p
)) {
896 error
= xasprintf("%s: Ethernet type set multiple times", s
);
899 flow
->dl_type
= htons(p
->dl_type
);
902 if (flow
->nw_proto
) {
903 error
= xasprintf("%s: network protocol set "
904 "multiple times", s
);
907 flow
->nw_proto
= p
->nw_proto
;
910 const struct mf_field
*mf
;
911 union mf_value value
;
914 mf
= mf_from_name(key
);
916 error
= xasprintf("%s: unknown field %s", s
, key
);
920 if (!mf_are_prereqs_ok(mf
, flow
)) {
921 error
= xasprintf("%s: prerequisites not met for setting %s",
926 if (!mf_is_zero(mf
, flow
)) {
927 error
= xasprintf("%s: field %s set multiple times", s
, key
);
931 field_error
= mf_parse_value(mf
, value_s
, &value
);
933 error
= xasprintf("%s: bad value for %s (%s)",
934 s
, key
, field_error
);
939 mf_set_flow_value(mf
, &value
, flow
);
947 memset(flow
, 0, sizeof *flow
);