2 * Copyright (c) 2010, 2011, 2012 Nicira Networks.
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"
32 #include "multipath.h"
36 #include "openflow/openflow.h"
38 #include "socket-util.h"
42 VLOG_DEFINE_THIS_MODULE(ofp_parse
);
45 str_to_table_id(const char *str
)
49 if (!str_to_int(str
, 10, &table_id
) || table_id
< 0 || table_id
> 255) {
50 ovs_fatal(0, "invalid table \"%s\"", str
);
56 str_to_u16(const char *str
, const char *name
)
60 if (!str_to_int(str
, 0, &value
) || value
< 0 || value
> 65535) {
61 ovs_fatal(0, "invalid %s \"%s\"", name
, str
);
67 str_to_u32(const char *str
)
73 ovs_fatal(0, "missing required numeric argument");
77 value
= strtoul(str
, &tail
, 0);
78 if (errno
== EINVAL
|| errno
== ERANGE
|| *tail
) {
79 ovs_fatal(0, "invalid numeric format %s", str
);
85 str_to_u64(const char *str
)
91 ovs_fatal(0, "missing required numeric argument");
95 value
= strtoull(str
, &tail
, 0);
96 if (errno
== EINVAL
|| errno
== ERANGE
|| *tail
) {
97 ovs_fatal(0, "invalid numeric format %s", str
);
103 str_to_mac(const char *str
, uint8_t mac
[6])
105 if (sscanf(str
, ETH_ADDR_SCAN_FMT
, ETH_ADDR_SCAN_ARGS(mac
))
106 != ETH_ADDR_SCAN_COUNT
) {
107 ovs_fatal(0, "invalid mac address %s", str
);
112 str_to_ip(const char *str
, ovs_be32
*ip
)
114 struct in_addr in_addr
;
116 if (lookup_ip(str
, &in_addr
)) {
117 ovs_fatal(0, "%s: could not convert to IP address", str
);
119 *ip
= in_addr
.s_addr
;
122 static struct ofp_action_output
*
123 put_output_action(struct ofpbuf
*b
, uint16_t port
)
125 struct ofp_action_output
*oao
;
127 oao
= ofputil_put_OFPAT_OUTPUT(b
);
128 oao
->port
= htons(port
);
133 parse_enqueue(struct ofpbuf
*b
, char *arg
)
136 char *port
= strtok_r(arg
, ":q", &sp
);
137 char *queue
= strtok_r(NULL
, "", &sp
);
138 struct ofp_action_enqueue
*oae
;
140 if (port
== NULL
|| queue
== NULL
) {
141 ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
144 oae
= ofputil_put_OFPAT_ENQUEUE(b
);
145 oae
->port
= htons(str_to_u32(port
));
146 oae
->queue_id
= htonl(str_to_u32(queue
));
150 parse_output(struct ofpbuf
*b
, char *arg
)
152 if (strchr(arg
, '[')) {
153 struct nx_action_output_reg
*naor
;
154 struct mf_subfield src
;
156 mf_parse_subfield(&src
, arg
);
158 naor
= ofputil_put_NXAST_OUTPUT_REG(b
);
159 naor
->ofs_nbits
= nxm_encode_ofs_nbits(src
.ofs
, src
.n_bits
);
160 naor
->src
= htonl(src
.field
->nxm_header
);
161 naor
->max_len
= htons(UINT16_MAX
);
163 put_output_action(b
, str_to_u32(arg
));
168 parse_resubmit(struct ofpbuf
*b
, char *arg
)
170 struct nx_action_resubmit
*nar
;
171 char *in_port_s
, *table_s
;
175 in_port_s
= strsep(&arg
, ",");
176 if (in_port_s
&& in_port_s
[0]) {
177 if (!ofputil_port_from_string(in_port_s
, &in_port
)) {
178 in_port
= str_to_u32(in_port_s
);
181 in_port
= OFPP_IN_PORT
;
184 table_s
= strsep(&arg
, ",");
185 table
= table_s
&& table_s
[0] ? str_to_u32(table_s
) : 255;
187 if (in_port
== OFPP_IN_PORT
&& table
== 255) {
188 ovs_fatal(0, "at least one \"in_port\" or \"table\" must be specified "
192 if (in_port
!= OFPP_IN_PORT
&& table
== 255) {
193 nar
= ofputil_put_NXAST_RESUBMIT(b
);
195 nar
= ofputil_put_NXAST_RESUBMIT_TABLE(b
);
198 nar
->in_port
= htons(in_port
);
202 parse_set_tunnel(struct ofpbuf
*b
, const char *arg
)
204 uint64_t tun_id
= str_to_u64(arg
);
205 if (tun_id
> UINT32_MAX
) {
206 ofputil_put_NXAST_SET_TUNNEL64(b
)->tun_id
= htonll(tun_id
);
208 ofputil_put_NXAST_SET_TUNNEL(b
)->tun_id
= htonl(tun_id
);
213 parse_note(struct ofpbuf
*b
, const char *arg
)
215 size_t start_ofs
= b
->size
;
216 struct nx_action_note
*nan
;
220 nan
= ofputil_put_NXAST_NOTE(b
);
222 b
->size
-= sizeof nan
->note
;
223 while (*arg
!= '\0') {
234 byte
= hexits_value(arg
, 2, &ok
);
236 ovs_fatal(0, "bad hex digit in `note' argument");
238 ofpbuf_put(b
, &byte
, 1);
243 len
= b
->size
- start_ofs
;
244 remainder
= len
% OFP_ACTION_ALIGN
;
246 ofpbuf_put_zeros(b
, OFP_ACTION_ALIGN
- remainder
);
248 nan
= (struct nx_action_note
*)((char *)b
->data
+ start_ofs
);
249 nan
->len
= htons(b
->size
- start_ofs
);
253 parse_fin_timeout(struct ofpbuf
*b
, char *arg
)
255 struct nx_action_fin_timeout
*naft
;
258 naft
= ofputil_put_NXAST_FIN_TIMEOUT(b
);
259 while (ofputil_parse_key_value(&arg
, &key
, &value
)) {
260 if (!strcmp(key
, "idle_timeout")) {
261 naft
->fin_idle_timeout
= htons(str_to_u16(value
, key
));
262 } else if (!strcmp(key
, "hard_timeout")) {
263 naft
->fin_hard_timeout
= htons(str_to_u16(value
, key
));
265 ovs_fatal(0, "invalid key '%s' in 'fin_timeout' argument", key
);
271 parse_controller(struct ofpbuf
*b
, char *arg
)
273 enum ofp_packet_in_reason reason
= OFPR_ACTION
;
274 uint16_t controller_id
= 0;
275 uint16_t max_len
= UINT16_MAX
;
279 } else if (strspn(arg
, "0123456789") == strlen(arg
)) {
280 max_len
= str_to_u16(arg
, "max_len");
284 while (ofputil_parse_key_value(&arg
, &name
, &value
)) {
285 if (!strcmp(name
, "reason")) {
286 if (!ofputil_packet_in_reason_from_string(value
, &reason
)) {
287 ovs_fatal(0, "unknown reason \"%s\"", value
);
289 } else if (!strcmp(name
, "max_len")) {
290 max_len
= str_to_u16(value
, "max_len");
291 } else if (!strcmp(name
, "id")) {
292 controller_id
= str_to_u16(value
, "id");
294 ovs_fatal(0, "unknown key \"%s\" parsing controller action",
300 if (reason
== OFPR_ACTION
&& controller_id
== 0) {
301 put_output_action(b
, OFPP_CONTROLLER
)->max_len
= htons(max_len
);
303 struct nx_action_controller
*nac
;
305 nac
= ofputil_put_NXAST_CONTROLLER(b
);
306 nac
->max_len
= htons(max_len
);
307 nac
->reason
= reason
;
308 nac
->controller_id
= htons(controller_id
);
313 parse_named_action(enum ofputil_action_code code
, const struct flow
*flow
,
314 struct ofpbuf
*b
, char *arg
)
316 struct ofp_action_dl_addr
*oada
;
317 struct ofp_action_vlan_pcp
*oavp
;
318 struct ofp_action_vlan_vid
*oavv
;
319 struct ofp_action_nw_addr
*oana
;
320 struct ofp_action_tp_port
*oata
;
323 case OFPUTIL_OFPAT_OUTPUT
:
324 parse_output(b
, arg
);
327 case OFPUTIL_OFPAT_SET_VLAN_VID
:
328 oavv
= ofputil_put_OFPAT_SET_VLAN_VID(b
);
329 oavv
->vlan_vid
= htons(str_to_u32(arg
));
332 case OFPUTIL_OFPAT_SET_VLAN_PCP
:
333 oavp
= ofputil_put_OFPAT_SET_VLAN_PCP(b
);
334 oavp
->vlan_pcp
= str_to_u32(arg
);
337 case OFPUTIL_OFPAT_STRIP_VLAN
:
338 ofputil_put_OFPAT_STRIP_VLAN(b
);
341 case OFPUTIL_OFPAT_SET_DL_SRC
:
342 case OFPUTIL_OFPAT_SET_DL_DST
:
343 oada
= ofputil_put_action(code
, b
);
344 str_to_mac(arg
, oada
->dl_addr
);
347 case OFPUTIL_OFPAT_SET_NW_SRC
:
348 case OFPUTIL_OFPAT_SET_NW_DST
:
349 oana
= ofputil_put_action(code
, b
);
350 str_to_ip(arg
, &oana
->nw_addr
);
353 case OFPUTIL_OFPAT_SET_NW_TOS
:
354 ofputil_put_OFPAT_SET_NW_TOS(b
)->nw_tos
= str_to_u32(arg
);
357 case OFPUTIL_OFPAT_SET_TP_SRC
:
358 case OFPUTIL_OFPAT_SET_TP_DST
:
359 oata
= ofputil_put_action(code
, b
);
360 oata
->tp_port
= htons(str_to_u32(arg
));
363 case OFPUTIL_OFPAT_ENQUEUE
:
364 parse_enqueue(b
, arg
);
367 case OFPUTIL_NXAST_RESUBMIT
:
368 parse_resubmit(b
, arg
);
371 case OFPUTIL_NXAST_SET_TUNNEL
:
372 parse_set_tunnel(b
, arg
);
375 case OFPUTIL_NXAST_SET_QUEUE
:
376 ofputil_put_NXAST_SET_QUEUE(b
)->queue_id
= htonl(str_to_u32(arg
));
379 case OFPUTIL_NXAST_POP_QUEUE
:
380 ofputil_put_NXAST_POP_QUEUE(b
);
383 case OFPUTIL_NXAST_REG_MOVE
:
384 nxm_parse_reg_move(ofputil_put_NXAST_REG_MOVE(b
), arg
);
387 case OFPUTIL_NXAST_REG_LOAD
:
388 nxm_parse_reg_load(ofputil_put_NXAST_REG_LOAD(b
), arg
);
391 case OFPUTIL_NXAST_NOTE
:
395 case OFPUTIL_NXAST_SET_TUNNEL64
:
396 ofputil_put_NXAST_SET_TUNNEL64(b
)->tun_id
= htonll(str_to_u64(arg
));
399 case OFPUTIL_NXAST_MULTIPATH
:
400 multipath_parse(ofputil_put_NXAST_MULTIPATH(b
), arg
);
403 case OFPUTIL_NXAST_AUTOPATH
:
404 autopath_parse(ofputil_put_NXAST_AUTOPATH(b
), arg
);
407 case OFPUTIL_NXAST_BUNDLE
:
408 bundle_parse(b
, arg
);
411 case OFPUTIL_NXAST_BUNDLE_LOAD
:
412 bundle_parse_load(b
, arg
);
415 case OFPUTIL_NXAST_RESUBMIT_TABLE
:
416 case OFPUTIL_NXAST_OUTPUT_REG
:
419 case OFPUTIL_NXAST_LEARN
:
420 learn_parse(b
, arg
, flow
);
423 case OFPUTIL_NXAST_EXIT
:
424 ofputil_put_NXAST_EXIT(b
);
427 case OFPUTIL_NXAST_DEC_TTL
:
428 ofputil_put_NXAST_DEC_TTL(b
);
431 case OFPUTIL_NXAST_FIN_TIMEOUT
:
432 parse_fin_timeout(b
, arg
);
435 case OFPUTIL_NXAST_CONTROLLER
:
436 parse_controller(b
, arg
);
442 str_to_action(const struct flow
*flow
, char *str
, struct ofpbuf
*b
)
444 char *pos
, *act
, *arg
;
449 while (ofputil_parse_key_value(&pos
, &act
, &arg
)) {
453 code
= ofputil_action_code_from_name(act
);
455 parse_named_action(code
, flow
, b
, arg
);
456 } else if (!strcasecmp(act
, "drop")) {
457 /* A drop action in OpenFlow occurs by just not setting
460 ovs_fatal(0, "Drop actions must not be preceded by other "
462 } else if (ofputil_parse_key_value(&pos
, &act
, &arg
)) {
463 ovs_fatal(0, "Drop actions must not be followed by other "
467 } else if (ofputil_port_from_string(act
, &port
)) {
468 put_output_action(b
, port
);
470 ovs_fatal(0, "Unknown action: %s", act
);
483 parse_protocol(const char *name
, const struct protocol
**p_out
)
485 static const struct protocol protocols
[] = {
486 { "ip", ETH_TYPE_IP
, 0 },
487 { "arp", ETH_TYPE_ARP
, 0 },
488 { "icmp", ETH_TYPE_IP
, IPPROTO_ICMP
},
489 { "tcp", ETH_TYPE_IP
, IPPROTO_TCP
},
490 { "udp", ETH_TYPE_IP
, IPPROTO_UDP
},
491 { "ipv6", ETH_TYPE_IPV6
, 0 },
492 { "ip6", ETH_TYPE_IPV6
, 0 },
493 { "icmp6", ETH_TYPE_IPV6
, IPPROTO_ICMPV6
},
494 { "tcp6", ETH_TYPE_IPV6
, IPPROTO_TCP
},
495 { "udp6", ETH_TYPE_IPV6
, IPPROTO_UDP
},
497 const struct protocol
*p
;
499 for (p
= protocols
; p
< &protocols
[ARRAY_SIZE(protocols
)]; p
++) {
500 if (!strcmp(p
->name
, name
)) {
510 ofp_fatal(const char *flow
, bool verbose
, const char *format
, ...)
515 fprintf(stderr
, "%s:\n", flow
);
518 va_start(args
, format
);
519 ovs_fatal_valist(0, format
, args
);
523 parse_field(const struct mf_field
*mf
, const char *s
, struct cls_rule
*rule
)
525 union mf_value value
, mask
;
528 error
= mf_parse(mf
, s
, &value
, &mask
);
530 ovs_fatal(0, "%s", error
);
533 mf_set(mf
, &value
, &mask
, rule
);
536 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
537 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
538 * If 'actions' is specified, an action must be in 'string' and may be expanded
541 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
542 * constant for 'command'. To parse syntax for an OFPST_FLOW or
543 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'. */
545 parse_ofp_str(struct ofputil_flow_mod
*fm
, int command
, const char *str_
,
555 char *string
= xstrdup(str_
);
556 char *save_ptr
= NULL
;
557 char *act_str
= NULL
;
566 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
;
573 case OFPFC_DELETE_STRICT
:
574 fields
= F_OUT_PORT
| F_PRIORITY
;
578 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
;
581 case OFPFC_MODIFY_STRICT
:
582 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
;
589 cls_rule_init_catchall(&fm
->cr
, OFP_DEFAULT_PRIORITY
);
590 fm
->cookie
= htonll(0);
591 fm
->cookie_mask
= htonll(0);
593 fm
->command
= command
;
594 fm
->idle_timeout
= OFP_FLOW_PERMANENT
;
595 fm
->hard_timeout
= OFP_FLOW_PERMANENT
;
596 fm
->buffer_id
= UINT32_MAX
;
597 fm
->out_port
= OFPP_NONE
;
599 if (fields
& F_ACTIONS
) {
600 act_str
= strstr(string
, "action");
602 ofp_fatal(str_
, verbose
, "must specify an action");
606 act_str
= strchr(act_str
+ 1, '=');
608 ofp_fatal(str_
, verbose
, "must specify an action");
613 for (name
= strtok_r(string
, "=, \t\r\n", &save_ptr
); name
;
614 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
615 const struct protocol
*p
;
617 if (parse_protocol(name
, &p
)) {
618 cls_rule_set_dl_type(&fm
->cr
, htons(p
->dl_type
));
620 cls_rule_set_nw_proto(&fm
->cr
, p
->nw_proto
);
622 } else if (fields
& F_FLAGS
&& !strcmp(name
, "send_flow_rem")) {
623 fm
->flags
|= OFPFF_SEND_FLOW_REM
;
624 } else if (fields
& F_FLAGS
&& !strcmp(name
, "check_overlap")) {
625 fm
->flags
|= OFPFF_CHECK_OVERLAP
;
629 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
631 ofp_fatal(str_
, verbose
, "field %s missing value", name
);
634 if (!strcmp(name
, "table")) {
635 fm
->table_id
= str_to_table_id(value
);
636 } else if (!strcmp(name
, "out_port")) {
637 fm
->out_port
= atoi(value
);
638 } else if (fields
& F_PRIORITY
&& !strcmp(name
, "priority")) {
639 fm
->cr
.priority
= str_to_u16(value
, name
);
640 } else if (fields
& F_TIMEOUT
&& !strcmp(name
, "idle_timeout")) {
641 fm
->idle_timeout
= str_to_u16(value
, name
);
642 } else if (fields
& F_TIMEOUT
&& !strcmp(name
, "hard_timeout")) {
643 fm
->hard_timeout
= str_to_u16(value
, name
);
644 } else if (!strcmp(name
, "cookie")) {
645 char *mask
= strchr(value
, '/');
647 if (command
== OFPFC_ADD
) {
648 ofp_fatal(str_
, verbose
, "flow additions cannot use "
652 fm
->cookie_mask
= htonll(str_to_u64(mask
+1));
654 fm
->cookie_mask
= htonll(UINT64_MAX
);
656 fm
->cookie
= htonll(str_to_u64(value
));
657 } else if (mf_from_name(name
)) {
658 parse_field(mf_from_name(name
), value
, &fm
->cr
);
659 } else if (!strcmp(name
, "duration")
660 || !strcmp(name
, "n_packets")
661 || !strcmp(name
, "n_bytes")) {
662 /* Ignore these, so that users can feed the output of
663 * "ovs-ofctl dump-flows" back into commands that parse
666 ofp_fatal(str_
, verbose
, "unknown keyword %s", name
);
670 if (fields
& F_ACTIONS
) {
671 struct ofpbuf actions
;
673 ofpbuf_init(&actions
, sizeof(union ofp_action
));
674 str_to_action(&fm
->cr
.flow
, act_str
, &actions
);
675 fm
->actions
= ofpbuf_steal_data(&actions
);
676 fm
->n_actions
= actions
.size
/ sizeof(union ofp_action
);
685 /* Parses 's' as a set of OpenFlow actions and appends the actions to
688 * Prints an error on stderr and aborts the program if 's' syntax is
691 parse_ofp_actions(const char *s_
, struct ofpbuf
*actions
)
693 char *s
= xstrdup(s_
);
694 str_to_action(NULL
, s
, actions
);
698 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
699 * (one of OFPFC_*) and appends the parsed OpenFlow message to 'packets'.
700 * '*cur_format' should initially contain the flow format currently configured
701 * on the connection; this function will add a message to change the flow
702 * format and update '*cur_format', if this is necessary to add the parsed
705 parse_ofp_flow_mod_str(struct list
*packets
, enum nx_flow_format
*cur_format
,
706 bool *flow_mod_table_id
, const char *string
,
707 uint16_t command
, bool verbose
)
709 enum nx_flow_format min_format
, next_format
;
710 struct cls_rule rule_copy
;
712 struct ofputil_flow_mod fm
;
714 parse_ofp_str(&fm
, command
, string
, verbose
);
716 min_format
= ofputil_min_flow_format(&fm
.cr
);
717 if (command
!= OFPFC_ADD
&& fm
.cookie_mask
!= htonll(0)) {
718 min_format
= NXFF_NXM
;
720 next_format
= MAX(*cur_format
, min_format
);
721 if (next_format
!= *cur_format
) {
722 struct ofpbuf
*sff
= ofputil_make_set_flow_format(next_format
);
723 list_push_back(packets
, &sff
->list_node
);
724 *cur_format
= next_format
;
727 /* Normalize a copy of the rule. This ensures that non-normalized flows
728 * get logged but doesn't affect what gets sent to the switch, so that the
729 * switch can do whatever it likes with the flow. */
731 ofputil_normalize_rule(&rule_copy
, next_format
);
733 if (fm
.table_id
!= 0xff && !*flow_mod_table_id
) {
734 struct ofpbuf
*sff
= ofputil_make_flow_mod_table_id(true);
735 list_push_back(packets
, &sff
->list_node
);
736 *flow_mod_table_id
= true;
739 ofm
= ofputil_encode_flow_mod(&fm
, *cur_format
, *flow_mod_table_id
);
740 list_push_back(packets
, &ofm
->list_node
);
743 /* Similar to parse_ofp_flow_mod_str(), except that the string is read from
744 * 'stream' and the command is always OFPFC_ADD. Returns false if end-of-file
745 * is reached before reading a flow, otherwise true. */
747 parse_ofp_flow_mod_file(struct list
*packets
,
748 enum nx_flow_format
*cur
, bool *flow_mod_table_id
,
749 FILE *stream
, uint16_t command
)
755 ok
= ds_get_preprocessed_line(&s
, stream
) == 0;
757 parse_ofp_flow_mod_str(packets
, cur
, flow_mod_table_id
,
758 ds_cstr(&s
), command
, true);
766 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request
*fsr
,
767 bool aggregate
, const char *string
)
769 struct ofputil_flow_mod fm
;
771 parse_ofp_str(&fm
, -1, string
, false);
772 fsr
->aggregate
= aggregate
;
773 fsr
->cookie
= fm
.cookie
;
774 fsr
->cookie_mask
= fm
.cookie_mask
;
776 fsr
->out_port
= fm
.out_port
;
777 fsr
->table_id
= fm
.table_id
;