2 * Copyright (c) 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "ofp-parse.h"
26 #include "byte-order.h"
27 #include "dynamic-string.h"
29 #include "meta-flow.h"
30 #include "multipath.h"
33 #include "ofp-actions.h"
36 #include "openflow/openflow.h"
37 #include "ovs-thread.h"
40 #include "socket-util.h"
43 /* Parses 'str' as an 8-bit unsigned integer into '*valuep'.
45 * 'name' describes the value parsed in an error message, if any.
47 * Returns NULL if successful, otherwise a malloc()'d string describing the
48 * error. The caller is responsible for freeing the returned string. */
49 char * WARN_UNUSED_RESULT
50 str_to_u8(const char *str
, const char *name
, uint8_t *valuep
)
54 if (!str_to_int(str
, 0, &value
) || value
< 0 || value
> 255) {
55 return xasprintf("invalid %s \"%s\"", name
, str
);
61 /* Parses 'str' as a 16-bit unsigned integer into '*valuep'.
63 * 'name' describes the value parsed in an error message, if any.
65 * Returns NULL if successful, otherwise a malloc()'d string describing the
66 * error. The caller is responsible for freeing the returned string. */
67 char * WARN_UNUSED_RESULT
68 str_to_u16(const char *str
, const char *name
, uint16_t *valuep
)
72 if (!str_to_int(str
, 0, &value
) || value
< 0 || value
> 65535) {
73 return xasprintf("invalid %s \"%s\"", name
, str
);
79 /* Parses 'str' as a 32-bit unsigned integer into '*valuep'.
81 * Returns NULL if successful, otherwise a malloc()'d string describing the
82 * error. The caller is responsible for freeing the returned string. */
83 char * WARN_UNUSED_RESULT
84 str_to_u32(const char *str
, uint32_t *valuep
)
90 return xstrdup("missing required numeric argument");
94 value
= strtoul(str
, &tail
, 0);
95 if (errno
== EINVAL
|| errno
== ERANGE
|| *tail
) {
96 return xasprintf("invalid numeric format %s", str
);
102 /* Parses 'str' as an 64-bit unsigned integer into '*valuep'.
104 * Returns NULL if successful, otherwise a malloc()'d string describing the
105 * error. The caller is responsible for freeing the returned string. */
106 char * WARN_UNUSED_RESULT
107 str_to_u64(const char *str
, uint64_t *valuep
)
113 return xstrdup("missing required numeric argument");
117 value
= strtoull(str
, &tail
, 0);
118 if (errno
== EINVAL
|| errno
== ERANGE
|| *tail
) {
119 return xasprintf("invalid numeric format %s", str
);
125 /* Parses 'str' as an 64-bit unsigned integer in network byte order into
128 * Returns NULL if successful, otherwise a malloc()'d string describing the
129 * error. The caller is responsible for freeing the returned string. */
130 char * WARN_UNUSED_RESULT
131 str_to_be64(const char *str
, ovs_be64
*valuep
)
136 error
= str_to_u64(str
, &value
);
138 *valuep
= htonll(value
);
143 /* Parses 'str' as an Ethernet address into 'mac'.
145 * Returns NULL if successful, otherwise a malloc()'d string describing the
146 * error. The caller is responsible for freeing the returned string. */
147 char * WARN_UNUSED_RESULT
148 str_to_mac(const char *str
, uint8_t mac
[ETH_ADDR_LEN
])
150 if (!ovs_scan(str
, ETH_ADDR_SCAN_FMT
, ETH_ADDR_SCAN_ARGS(mac
))) {
151 return xasprintf("invalid mac address %s", str
);
156 /* Parses 'str' as an IP address into '*ip'.
158 * Returns NULL if successful, otherwise a malloc()'d string describing the
159 * error. The caller is responsible for freeing the returned string. */
160 char * WARN_UNUSED_RESULT
161 str_to_ip(const char *str
, ovs_be32
*ip
)
163 struct in_addr in_addr
;
165 if (lookup_ip(str
, &in_addr
)) {
166 return xasprintf("%s: could not convert to IP address", str
);
168 *ip
= in_addr
.s_addr
;
179 parse_protocol(const char *name
, const struct protocol
**p_out
)
181 static const struct protocol protocols
[] = {
182 { "ip", ETH_TYPE_IP
, 0 },
183 { "arp", ETH_TYPE_ARP
, 0 },
184 { "icmp", ETH_TYPE_IP
, IPPROTO_ICMP
},
185 { "tcp", ETH_TYPE_IP
, IPPROTO_TCP
},
186 { "udp", ETH_TYPE_IP
, IPPROTO_UDP
},
187 { "sctp", ETH_TYPE_IP
, IPPROTO_SCTP
},
188 { "ipv6", ETH_TYPE_IPV6
, 0 },
189 { "ip6", ETH_TYPE_IPV6
, 0 },
190 { "icmp6", ETH_TYPE_IPV6
, IPPROTO_ICMPV6
},
191 { "tcp6", ETH_TYPE_IPV6
, IPPROTO_TCP
},
192 { "udp6", ETH_TYPE_IPV6
, IPPROTO_UDP
},
193 { "sctp6", ETH_TYPE_IPV6
, IPPROTO_SCTP
},
194 { "rarp", ETH_TYPE_RARP
, 0},
195 { "mpls", ETH_TYPE_MPLS
, 0 },
196 { "mplsm", ETH_TYPE_MPLS_MCAST
, 0 },
198 const struct protocol
*p
;
200 for (p
= protocols
; p
< &protocols
[ARRAY_SIZE(protocols
)]; p
++) {
201 if (!strcmp(p
->name
, name
)) {
210 /* Parses 's' as the (possibly masked) value of field 'mf', and updates
211 * 'match' appropriately. Restricts the set of usable protocols to ones
212 * supporting the parsed field.
214 * Returns NULL if successful, otherwise a malloc()'d string describing the
215 * error. The caller is responsible for freeing the returned string. */
216 static char * WARN_UNUSED_RESULT
217 parse_field(const struct mf_field
*mf
, const char *s
, struct match
*match
,
218 enum ofputil_protocol
*usable_protocols
)
220 union mf_value value
, mask
;
223 error
= mf_parse(mf
, s
, &value
, &mask
);
225 *usable_protocols
&= mf_set(mf
, &value
, &mask
, match
);
231 extract_actions(char *s
)
233 s
= strstr(s
, "action");
236 s
= strchr(s
+ 1, '=');
237 return s
? s
+ 1 : NULL
;
244 static char * WARN_UNUSED_RESULT
245 parse_ofp_str__(struct ofputil_flow_mod
*fm
, int command
, char *string
,
246 enum ofputil_protocol
*usable_protocols
)
255 char *save_ptr
= NULL
;
256 char *act_str
= NULL
;
259 *usable_protocols
= OFPUTIL_P_ANY
;
267 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
;
274 case OFPFC_DELETE_STRICT
:
275 fields
= F_OUT_PORT
| F_PRIORITY
;
279 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
;
282 case OFPFC_MODIFY_STRICT
:
283 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
;
290 match_init_catchall(&fm
->match
);
291 fm
->priority
= OFP_DEFAULT_PRIORITY
;
292 fm
->cookie
= htonll(0);
293 fm
->cookie_mask
= htonll(0);
294 if (command
== OFPFC_MODIFY
|| command
== OFPFC_MODIFY_STRICT
) {
295 /* For modify, by default, don't update the cookie. */
296 fm
->new_cookie
= OVS_BE64_MAX
;
298 fm
->new_cookie
= htonll(0);
300 fm
->modify_cookie
= false;
302 fm
->command
= command
;
303 fm
->idle_timeout
= OFP_FLOW_PERMANENT
;
304 fm
->hard_timeout
= OFP_FLOW_PERMANENT
;
305 fm
->buffer_id
= UINT32_MAX
;
306 fm
->out_port
= OFPP_ANY
;
308 fm
->out_group
= OFPG11_ANY
;
309 fm
->delete_reason
= OFPRR_DELETE
;
310 if (fields
& F_ACTIONS
) {
311 act_str
= extract_actions(string
);
313 return xstrdup("must specify an action");
316 for (name
= strtok_r(string
, "=, \t\r\n", &save_ptr
); name
;
317 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
318 const struct protocol
*p
;
321 if (parse_protocol(name
, &p
)) {
322 match_set_dl_type(&fm
->match
, htons(p
->dl_type
));
324 match_set_nw_proto(&fm
->match
, p
->nw_proto
);
326 } else if (fields
& F_FLAGS
&& !strcmp(name
, "send_flow_rem")) {
327 fm
->flags
|= OFPUTIL_FF_SEND_FLOW_REM
;
328 } else if (fields
& F_FLAGS
&& !strcmp(name
, "check_overlap")) {
329 fm
->flags
|= OFPUTIL_FF_CHECK_OVERLAP
;
330 } else if (fields
& F_FLAGS
&& !strcmp(name
, "reset_counts")) {
331 fm
->flags
|= OFPUTIL_FF_RESET_COUNTS
;
332 *usable_protocols
&= OFPUTIL_P_OF12_UP
;
333 } else if (fields
& F_FLAGS
&& !strcmp(name
, "no_packet_counts")) {
334 fm
->flags
|= OFPUTIL_FF_NO_PKT_COUNTS
;
335 *usable_protocols
&= OFPUTIL_P_OF13_UP
;
336 } else if (fields
& F_FLAGS
&& !strcmp(name
, "no_byte_counts")) {
337 fm
->flags
|= OFPUTIL_FF_NO_BYT_COUNTS
;
338 *usable_protocols
&= OFPUTIL_P_OF13_UP
;
339 } else if (!strcmp(name
, "no_readonly_table")
340 || !strcmp(name
, "allow_hidden_fields")) {
341 /* ignore these fields. */
345 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
347 return xasprintf("field %s missing value", name
);
350 if (!strcmp(name
, "table")) {
351 error
= str_to_u8(value
, "table", &fm
->table_id
);
352 if (fm
->table_id
!= 0xff) {
353 *usable_protocols
&= OFPUTIL_P_TID
;
355 } else if (!strcmp(name
, "out_port")) {
356 if (!ofputil_port_from_string(value
, &fm
->out_port
)) {
357 error
= xasprintf("%s is not a valid OpenFlow port",
360 } else if (fields
& F_PRIORITY
&& !strcmp(name
, "priority")) {
361 uint16_t priority
= 0;
363 error
= str_to_u16(value
, name
, &priority
);
364 fm
->priority
= priority
;
365 } else if (fields
& F_TIMEOUT
&& !strcmp(name
, "idle_timeout")) {
366 error
= str_to_u16(value
, name
, &fm
->idle_timeout
);
367 } else if (fields
& F_TIMEOUT
&& !strcmp(name
, "hard_timeout")) {
368 error
= str_to_u16(value
, name
, &fm
->hard_timeout
);
369 } else if (!strcmp(name
, "cookie")) {
370 char *mask
= strchr(value
, '/');
373 /* A mask means we're searching for a cookie. */
374 if (command
== OFPFC_ADD
) {
375 return xstrdup("flow additions cannot use "
379 error
= str_to_be64(value
, &fm
->cookie
);
383 error
= str_to_be64(mask
+ 1, &fm
->cookie_mask
);
385 /* Matching of the cookie is only supported through NXM or
387 if (fm
->cookie_mask
!= htonll(0)) {
388 *usable_protocols
&= OFPUTIL_P_NXM_OF11_UP
;
391 /* No mask means that the cookie is being set. */
392 if (command
!= OFPFC_ADD
&& command
!= OFPFC_MODIFY
393 && command
!= OFPFC_MODIFY_STRICT
) {
394 return xstrdup("cannot set cookie");
396 error
= str_to_be64(value
, &fm
->new_cookie
);
397 fm
->modify_cookie
= true;
399 } else if (mf_from_name(name
)) {
400 error
= parse_field(mf_from_name(name
), value
, &fm
->match
,
402 } else if (!strcmp(name
, "duration")
403 || !strcmp(name
, "n_packets")
404 || !strcmp(name
, "n_bytes")
405 || !strcmp(name
, "idle_age")
406 || !strcmp(name
, "hard_age")) {
407 /* Ignore these, so that users can feed the output of
408 * "ovs-ofctl dump-flows" back into commands that parse
411 error
= xasprintf("unknown keyword %s", name
);
419 /* Check for usable protocol interdependencies between match fields. */
420 if (fm
->match
.flow
.dl_type
== htons(ETH_TYPE_IPV6
)) {
421 const struct flow_wildcards
*wc
= &fm
->match
.wc
;
422 /* Only NXM and OXM support matching L3 and L4 fields within IPv6.
424 * (IPv6 specific fields as well as arp_sha, arp_tha, nw_frag, and
425 * nw_ttl are covered elsewhere so they don't need to be included in
428 if (wc
->masks
.nw_proto
|| wc
->masks
.nw_tos
429 || wc
->masks
.tp_src
|| wc
->masks
.tp_dst
) {
430 *usable_protocols
&= OFPUTIL_P_NXM_OXM_ANY
;
433 if (!fm
->cookie_mask
&& fm
->new_cookie
== OVS_BE64_MAX
434 && (command
== OFPFC_MODIFY
|| command
== OFPFC_MODIFY_STRICT
)) {
435 /* On modifies without a mask, we are supposed to add a flow if
436 * one does not exist. If a cookie wasn't been specified, use a
437 * default of zero. */
438 fm
->new_cookie
= htonll(0);
440 if (fields
& F_ACTIONS
) {
441 enum ofputil_protocol action_usable_protocols
;
442 struct ofpbuf ofpacts
;
445 ofpbuf_init(&ofpacts
, 32);
446 error
= ofpacts_parse_instructions(act_str
, &ofpacts
,
447 &action_usable_protocols
);
448 *usable_protocols
&= action_usable_protocols
;
452 err
= ofpacts_check(ofpbuf_data(&ofpacts
), ofpbuf_size(&ofpacts
), &fm
->match
.flow
,
453 OFPP_MAX
, fm
->table_id
, 255, usable_protocols
);
454 if (!err
&& !usable_protocols
) {
455 err
= OFPERR_OFPBAC_MATCH_INCONSISTENT
;
458 error
= xasprintf("actions are invalid with specified match "
459 "(%s)", ofperr_to_string(err
));
464 ofpbuf_uninit(&ofpacts
);
468 fm
->ofpacts_len
= ofpbuf_size(&ofpacts
);
469 fm
->ofpacts
= ofpbuf_steal_data(&ofpacts
);
478 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
479 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
480 * Returns the set of usable protocols in '*usable_protocols'.
482 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
483 * constant for 'command'. To parse syntax for an OFPST_FLOW or
484 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'.
486 * Returns NULL if successful, otherwise a malloc()'d string describing the
487 * error. The caller is responsible for freeing the returned string. */
488 char * WARN_UNUSED_RESULT
489 parse_ofp_str(struct ofputil_flow_mod
*fm
, int command
, const char *str_
,
490 enum ofputil_protocol
*usable_protocols
)
492 char *string
= xstrdup(str_
);
495 error
= parse_ofp_str__(fm
, command
, string
, usable_protocols
);
505 static char * WARN_UNUSED_RESULT
506 parse_ofp_meter_mod_str__(struct ofputil_meter_mod
*mm
, char *string
,
507 struct ofpbuf
*bands
, int command
,
508 enum ofputil_protocol
*usable_protocols
)
515 char *save_ptr
= NULL
;
516 char *band_str
= NULL
;
519 /* Meters require at least OF 1.3. */
520 *usable_protocols
= OFPUTIL_P_OF13_UP
;
528 fields
= F_METER
| F_FLAGS
| F_BANDS
;
536 fields
= F_METER
| F_FLAGS
| F_BANDS
;
543 mm
->command
= command
;
544 mm
->meter
.meter_id
= 0;
546 if (fields
& F_BANDS
) {
547 band_str
= strstr(string
, "band");
549 return xstrdup("must specify bands");
553 band_str
= strchr(band_str
+ 1, '=');
555 return xstrdup("must specify bands");
560 for (name
= strtok_r(string
, "=, \t\r\n", &save_ptr
); name
;
561 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
563 if (fields
& F_FLAGS
&& !strcmp(name
, "kbps")) {
564 mm
->meter
.flags
|= OFPMF13_KBPS
;
565 } else if (fields
& F_FLAGS
&& !strcmp(name
, "pktps")) {
566 mm
->meter
.flags
|= OFPMF13_PKTPS
;
567 } else if (fields
& F_FLAGS
&& !strcmp(name
, "burst")) {
568 mm
->meter
.flags
|= OFPMF13_BURST
;
569 } else if (fields
& F_FLAGS
&& !strcmp(name
, "stats")) {
570 mm
->meter
.flags
|= OFPMF13_STATS
;
574 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
576 return xasprintf("field %s missing value", name
);
579 if (!strcmp(name
, "meter")) {
580 if (!strcmp(value
, "all")) {
581 mm
->meter
.meter_id
= OFPM13_ALL
;
582 } else if (!strcmp(value
, "controller")) {
583 mm
->meter
.meter_id
= OFPM13_CONTROLLER
;
584 } else if (!strcmp(value
, "slowpath")) {
585 mm
->meter
.meter_id
= OFPM13_SLOWPATH
;
587 char *error
= str_to_u32(value
, &mm
->meter
.meter_id
);
591 if (mm
->meter
.meter_id
> OFPM13_MAX
592 || !mm
->meter
.meter_id
) {
593 return xasprintf("invalid value for %s", name
);
597 return xasprintf("unknown keyword %s", name
);
601 if (fields
& F_METER
&& !mm
->meter
.meter_id
) {
602 return xstrdup("must specify 'meter'");
604 if (fields
& F_FLAGS
&& !mm
->meter
.flags
) {
605 return xstrdup("meter must specify either 'kbps' or 'pktps'");
608 if (fields
& F_BANDS
) {
609 uint16_t n_bands
= 0;
610 struct ofputil_meter_band
*band
= NULL
;
613 for (name
= strtok_r(band_str
, "=, \t\r\n", &save_ptr
); name
;
614 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
618 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
620 return xasprintf("field %s missing value", name
);
623 if (!strcmp(name
, "type")) {
624 /* Start a new band */
625 band
= ofpbuf_put_zeros(bands
, sizeof *band
);
628 if (!strcmp(value
, "drop")) {
629 band
->type
= OFPMBT13_DROP
;
630 } else if (!strcmp(value
, "dscp_remark")) {
631 band
->type
= OFPMBT13_DSCP_REMARK
;
633 return xasprintf("field %s unknown value %s", name
, value
);
635 } else if (!band
|| !band
->type
) {
636 return xstrdup("band must start with the 'type' keyword");
637 } else if (!strcmp(name
, "rate")) {
638 char *error
= str_to_u32(value
, &band
->rate
);
642 } else if (!strcmp(name
, "burst_size")) {
643 char *error
= str_to_u32(value
, &band
->burst_size
);
647 } else if (!strcmp(name
, "prec_level")) {
648 char *error
= str_to_u8(value
, name
, &band
->prec_level
);
653 return xasprintf("unknown keyword %s", name
);
658 return xstrdup("meter must have bands");
661 mm
->meter
.n_bands
= n_bands
;
662 mm
->meter
.bands
= ofpbuf_steal_data(bands
);
664 for (i
= 0; i
< n_bands
; ++i
) {
665 band
= &mm
->meter
.bands
[i
];
668 return xstrdup("band must have 'type'");
670 if (band
->type
== OFPMBT13_DSCP_REMARK
) {
671 if (!band
->prec_level
) {
672 return xstrdup("'dscp_remark' band must have"
676 if (band
->prec_level
) {
677 return xstrdup("Only 'dscp_remark' band may have"
682 return xstrdup("band must have 'rate'");
684 if (mm
->meter
.flags
& OFPMF13_BURST
) {
685 if (!band
->burst_size
) {
686 return xstrdup("band must have 'burst_size' "
687 "when 'burst' flag is set");
690 if (band
->burst_size
) {
691 return xstrdup("band may have 'burst_size' only "
692 "when 'burst' flag is set");
697 mm
->meter
.n_bands
= 0;
698 mm
->meter
.bands
= NULL
;
704 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
705 * page) into 'mm' for sending the specified meter_mod 'command' to a switch.
707 * Returns NULL if successful, otherwise a malloc()'d string describing the
708 * error. The caller is responsible for freeing the returned string. */
709 char * WARN_UNUSED_RESULT
710 parse_ofp_meter_mod_str(struct ofputil_meter_mod
*mm
, const char *str_
,
711 int command
, enum ofputil_protocol
*usable_protocols
)
717 ofpbuf_init(&bands
, 64);
718 string
= xstrdup(str_
);
720 error
= parse_ofp_meter_mod_str__(mm
, string
, &bands
, command
,
724 ofpbuf_uninit(&bands
);
729 static char * WARN_UNUSED_RESULT
730 parse_flow_monitor_request__(struct ofputil_flow_monitor_request
*fmr
,
731 const char *str_
, char *string
,
732 enum ofputil_protocol
*usable_protocols
)
734 static atomic_count id
= ATOMIC_COUNT_INIT(0);
735 char *save_ptr
= NULL
;
738 fmr
->id
= atomic_count_inc(&id
);
740 fmr
->flags
= (NXFMF_INITIAL
| NXFMF_ADD
| NXFMF_DELETE
| NXFMF_MODIFY
741 | NXFMF_OWN
| NXFMF_ACTIONS
);
742 fmr
->out_port
= OFPP_NONE
;
743 fmr
->table_id
= 0xff;
744 match_init_catchall(&fmr
->match
);
746 for (name
= strtok_r(string
, "=, \t\r\n", &save_ptr
); name
;
747 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
748 const struct protocol
*p
;
750 if (!strcmp(name
, "!initial")) {
751 fmr
->flags
&= ~NXFMF_INITIAL
;
752 } else if (!strcmp(name
, "!add")) {
753 fmr
->flags
&= ~NXFMF_ADD
;
754 } else if (!strcmp(name
, "!delete")) {
755 fmr
->flags
&= ~NXFMF_DELETE
;
756 } else if (!strcmp(name
, "!modify")) {
757 fmr
->flags
&= ~NXFMF_MODIFY
;
758 } else if (!strcmp(name
, "!actions")) {
759 fmr
->flags
&= ~NXFMF_ACTIONS
;
760 } else if (!strcmp(name
, "!own")) {
761 fmr
->flags
&= ~NXFMF_OWN
;
762 } else if (parse_protocol(name
, &p
)) {
763 match_set_dl_type(&fmr
->match
, htons(p
->dl_type
));
765 match_set_nw_proto(&fmr
->match
, p
->nw_proto
);
770 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
772 return xasprintf("%s: field %s missing value", str_
, name
);
775 if (!strcmp(name
, "table")) {
776 char *error
= str_to_u8(value
, "table", &fmr
->table_id
);
780 } else if (!strcmp(name
, "out_port")) {
781 fmr
->out_port
= u16_to_ofp(atoi(value
));
782 } else if (mf_from_name(name
)) {
785 error
= parse_field(mf_from_name(name
), value
, &fmr
->match
,
791 return xasprintf("%s: unknown keyword %s", str_
, name
);
798 /* Convert 'str_' (as described in the documentation for the "monitor" command
799 * in the ovs-ofctl man page) into 'fmr'.
801 * Returns NULL if successful, otherwise a malloc()'d string describing the
802 * error. The caller is responsible for freeing the returned string. */
803 char * WARN_UNUSED_RESULT
804 parse_flow_monitor_request(struct ofputil_flow_monitor_request
*fmr
,
806 enum ofputil_protocol
*usable_protocols
)
808 char *string
= xstrdup(str_
);
809 char *error
= parse_flow_monitor_request__(fmr
, str_
, string
,
815 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
816 * (one of OFPFC_*) into 'fm'.
818 * Returns NULL if successful, otherwise a malloc()'d string describing the
819 * error. The caller is responsible for freeing the returned string. */
820 char * WARN_UNUSED_RESULT
821 parse_ofp_flow_mod_str(struct ofputil_flow_mod
*fm
, const char *string
,
823 enum ofputil_protocol
*usable_protocols
)
825 char *error
= parse_ofp_str(fm
, command
, string
, usable_protocols
);
827 /* Normalize a copy of the match. This ensures that non-normalized
828 * flows get logged but doesn't affect what gets sent to the switch, so
829 * that the switch can do whatever it likes with the flow. */
830 struct match match_copy
= fm
->match
;
831 ofputil_normalize_match(&match_copy
);
837 /* Convert 'table_id' and 'flow_miss_handling' (as described for the
838 * "mod-table" command in the ovs-ofctl man page) into 'tm' for sending the
839 * specified table_mod 'command' to a switch.
841 * Returns NULL if successful, otherwise a malloc()'d string describing the
842 * error. The caller is responsible for freeing the returned string. */
843 char * WARN_UNUSED_RESULT
844 parse_ofp_table_mod(struct ofputil_table_mod
*tm
, const char *table_id
,
845 const char *flow_miss_handling
,
846 enum ofputil_protocol
*usable_protocols
)
848 /* Table mod requires at least OF 1.1. */
849 *usable_protocols
= OFPUTIL_P_OF11_UP
;
851 if (!strcasecmp(table_id
, "all")) {
852 tm
->table_id
= OFPTT_ALL
;
854 char *error
= str_to_u8(table_id
, "table_id", &tm
->table_id
);
860 if (strcmp(flow_miss_handling
, "controller") == 0) {
861 tm
->miss_config
= OFPUTIL_TABLE_MISS_CONTROLLER
;
862 } else if (strcmp(flow_miss_handling
, "continue") == 0) {
863 tm
->miss_config
= OFPUTIL_TABLE_MISS_CONTINUE
;
864 } else if (strcmp(flow_miss_handling
, "drop") == 0) {
865 tm
->miss_config
= OFPUTIL_TABLE_MISS_DROP
;
867 return xasprintf("invalid flow_miss_handling %s", flow_miss_handling
);
870 if (tm
->table_id
== 0xfe
871 && tm
->miss_config
== OFPUTIL_TABLE_MISS_CONTINUE
) {
872 return xstrdup("last table's flow miss handling can not be continue");
879 /* Opens file 'file_name' and reads each line as a flow_mod of the specified
880 * type (one of OFPFC_*). Stores each flow_mod in '*fm', an array allocated
881 * on the caller's behalf, and the number of flow_mods in '*n_fms'.
883 * Returns NULL if successful, otherwise a malloc()'d string describing the
884 * error. The caller is responsible for freeing the returned string. */
885 char * WARN_UNUSED_RESULT
886 parse_ofp_flow_mod_file(const char *file_name
, uint16_t command
,
887 struct ofputil_flow_mod
**fms
, size_t *n_fms
,
888 enum ofputil_protocol
*usable_protocols
)
890 size_t allocated_fms
;
895 *usable_protocols
= OFPUTIL_P_ANY
;
900 stream
= !strcmp(file_name
, "-") ? stdin
: fopen(file_name
, "r");
901 if (stream
== NULL
) {
902 return xasprintf("%s: open failed (%s)",
903 file_name
, ovs_strerror(errno
));
906 allocated_fms
= *n_fms
;
909 while (!ds_get_preprocessed_line(&s
, stream
, &line_number
)) {
911 enum ofputil_protocol usable
;
913 if (*n_fms
>= allocated_fms
) {
914 *fms
= x2nrealloc(*fms
, &allocated_fms
, sizeof **fms
);
916 error
= parse_ofp_flow_mod_str(&(*fms
)[*n_fms
], ds_cstr(&s
), command
,
921 for (i
= 0; i
< *n_fms
; i
++) {
922 free(CONST_CAST(struct ofpact
*, (*fms
)[i
].ofpacts
));
929 if (stream
!= stdin
) {
933 return xasprintf("%s:%d: %s", file_name
, line_number
, error
);
935 *usable_protocols
&= usable
; /* Each line can narrow the set. */
940 if (stream
!= stdin
) {
946 char * WARN_UNUSED_RESULT
947 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request
*fsr
,
948 bool aggregate
, const char *string
,
949 enum ofputil_protocol
*usable_protocols
)
951 struct ofputil_flow_mod fm
;
954 error
= parse_ofp_str(&fm
, -1, string
, usable_protocols
);
959 /* Special table ID support not required for stats requests. */
960 if (*usable_protocols
& OFPUTIL_P_OF10_STD_TID
) {
961 *usable_protocols
|= OFPUTIL_P_OF10_STD
;
963 if (*usable_protocols
& OFPUTIL_P_OF10_NXM_TID
) {
964 *usable_protocols
|= OFPUTIL_P_OF10_NXM
;
967 fsr
->aggregate
= aggregate
;
968 fsr
->cookie
= fm
.cookie
;
969 fsr
->cookie_mask
= fm
.cookie_mask
;
970 fsr
->match
= fm
.match
;
971 fsr
->out_port
= fm
.out_port
;
972 fsr
->out_group
= fm
.out_group
;
973 fsr
->table_id
= fm
.table_id
;
977 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
978 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
979 * mf_field. Fields must be specified in a natural order for satisfying
980 * prerequisites. If 'mask' is specified, fills the mask field for each of the
981 * field specified in flow. If the map, 'names_portno' is specfied, converts
982 * the in_port name into port no while setting the 'flow'.
984 * Returns NULL on success, otherwise a malloc()'d string that explains the
987 parse_ofp_exact_flow(struct flow
*flow
, struct flow
*mask
, const char *s
,
988 const struct simap
*portno_names
)
990 char *pos
, *key
, *value_s
;
994 memset(flow
, 0, sizeof *flow
);
996 memset(mask
, 0, sizeof *mask
);
999 pos
= copy
= xstrdup(s
);
1000 while (ofputil_parse_key_value(&pos
, &key
, &value_s
)) {
1001 const struct protocol
*p
;
1002 if (parse_protocol(key
, &p
)) {
1003 if (flow
->dl_type
) {
1004 error
= xasprintf("%s: Ethernet type set multiple times", s
);
1007 flow
->dl_type
= htons(p
->dl_type
);
1009 mask
->dl_type
= OVS_BE16_MAX
;
1013 if (flow
->nw_proto
) {
1014 error
= xasprintf("%s: network protocol set "
1015 "multiple times", s
);
1018 flow
->nw_proto
= p
->nw_proto
;
1020 mask
->nw_proto
= UINT8_MAX
;
1024 const struct mf_field
*mf
;
1025 union mf_value value
;
1028 mf
= mf_from_name(key
);
1030 error
= xasprintf("%s: unknown field %s", s
, key
);
1034 if (!mf_are_prereqs_ok(mf
, flow
)) {
1035 error
= xasprintf("%s: prerequisites not met for setting %s",
1040 if (!mf_is_zero(mf
, flow
)) {
1041 error
= xasprintf("%s: field %s set multiple times", s
, key
);
1045 if (!strcmp(key
, "in_port")
1047 && simap_contains(portno_names
, value_s
)) {
1048 flow
->in_port
.ofp_port
= u16_to_ofp(
1049 simap_get(portno_names
, value_s
));
1051 mask
->in_port
.ofp_port
= u16_to_ofp(ntohs(OVS_BE16_MAX
));
1054 field_error
= mf_parse_value(mf
, value_s
, &value
);
1056 error
= xasprintf("%s: bad value for %s (%s)",
1057 s
, key
, field_error
);
1062 mf_set_flow_value(mf
, &value
, flow
);
1064 mf_mask_field(mf
, mask
);
1070 if (!flow
->in_port
.ofp_port
) {
1071 flow
->in_port
.ofp_port
= OFPP_NONE
;
1078 memset(flow
, 0, sizeof *flow
);
1080 memset(mask
, 0, sizeof *mask
);
1086 static char * WARN_UNUSED_RESULT
1087 parse_bucket_str(struct ofputil_bucket
*bucket
, char *str_
,
1088 enum ofputil_protocol
*usable_protocols
)
1090 char *pos
, *key
, *value
;
1091 struct ofpbuf ofpacts
;
1096 bucket
->watch_port
= OFPP_ANY
;
1097 bucket
->watch_group
= OFPG11_ANY
;
1103 while (ofputil_parse_key_value(&pos
, &key
, &value
)) {
1104 if (!strcasecmp(key
, "weight")) {
1105 error
= str_to_u16(value
, "weight", &bucket
->weight
);
1106 } else if (!strcasecmp(key
, "watch_port")) {
1107 if (!ofputil_port_from_string(value
, &bucket
->watch_port
)
1108 || (ofp_to_u16(bucket
->watch_port
) >= ofp_to_u16(OFPP_MAX
)
1109 && bucket
->watch_port
!= OFPP_ANY
)) {
1110 error
= xasprintf("%s: invalid watch_port", value
);
1112 } else if (!strcasecmp(key
, "watch_group")) {
1113 error
= str_to_u32(value
, &bucket
->watch_group
);
1114 if (!error
&& bucket
->watch_group
> OFPG_MAX
) {
1115 error
= xasprintf("invalid watch_group id %"PRIu32
,
1116 bucket
->watch_group
);
1118 } else if (!strcasecmp(key
, "action") || !strcasecmp(key
, "actions")) {
1119 ds_put_format(&actions
, "%s,", value
);
1121 ds_put_format(&actions
, "%s(%s),", key
, value
);
1125 ds_destroy(&actions
);
1130 if (!actions
.length
) {
1131 return xstrdup("bucket must specify actions");
1133 ds_chomp(&actions
, ',');
1135 ofpbuf_init(&ofpacts
, 0);
1136 error
= ofpacts_parse_actions(ds_cstr(&actions
), &ofpacts
,
1138 ds_destroy(&actions
);
1140 ofpbuf_uninit(&ofpacts
);
1143 bucket
->ofpacts
= ofpbuf_data(&ofpacts
);
1144 bucket
->ofpacts_len
= ofpbuf_size(&ofpacts
);
1149 static char * WARN_UNUSED_RESULT
1150 parse_ofp_group_mod_str__(struct ofputil_group_mod
*gm
, uint16_t command
,
1152 enum ofputil_protocol
*usable_protocols
)
1155 F_GROUP_TYPE
= 1 << 0,
1158 char *save_ptr
= NULL
;
1159 bool had_type
= false;
1161 struct ofputil_bucket
*bucket
;
1164 *usable_protocols
= OFPUTIL_P_OF11_UP
;
1168 fields
= F_GROUP_TYPE
| F_BUCKETS
;
1171 case OFPGC11_DELETE
:
1175 case OFPGC11_MODIFY
:
1176 fields
= F_GROUP_TYPE
| F_BUCKETS
;
1183 memset(gm
, 0, sizeof *gm
);
1184 gm
->command
= command
;
1185 gm
->group_id
= OFPG_ANY
;
1186 list_init(&gm
->buckets
);
1187 if (command
== OFPGC11_DELETE
&& string
[0] == '\0') {
1188 gm
->group_id
= OFPG_ALL
;
1192 *usable_protocols
= OFPUTIL_P_OF11_UP
;
1194 if (fields
& F_BUCKETS
) {
1195 char *bkt_str
= strstr(string
, "bucket");
1204 bkt_str
= strchr(bkt_str
+ 1, '=');
1206 error
= xstrdup("must specify bucket content");
1211 next_bkt_str
= strstr(bkt_str
, "bucket");
1213 *next_bkt_str
= '\0';
1216 bucket
= xzalloc(sizeof(struct ofputil_bucket
));
1217 error
= parse_bucket_str(bucket
, bkt_str
, usable_protocols
);
1222 list_push_back(&gm
->buckets
, &bucket
->list_node
);
1224 bkt_str
= next_bkt_str
;
1228 for (name
= strtok_r(string
, "=, \t\r\n", &save_ptr
); name
;
1229 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
1232 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
1234 error
= xasprintf("field %s missing value", name
);
1238 if (!strcmp(name
, "group_id")) {
1239 if(!strcmp(value
, "all")) {
1240 gm
->group_id
= OFPG_ALL
;
1242 char *error
= str_to_u32(value
, &gm
->group_id
);
1246 if (gm
->group_id
!= OFPG_ALL
&& gm
->group_id
> OFPG_MAX
) {
1247 error
= xasprintf("invalid group id %"PRIu32
,
1252 } else if (!strcmp(name
, "type")){
1253 if (!(fields
& F_GROUP_TYPE
)) {
1254 error
= xstrdup("type is not needed");
1257 if (!strcmp(value
, "all")) {
1258 gm
->type
= OFPGT11_ALL
;
1259 } else if (!strcmp(value
, "select")) {
1260 gm
->type
= OFPGT11_SELECT
;
1261 } else if (!strcmp(value
, "indirect")) {
1262 gm
->type
= OFPGT11_INDIRECT
;
1263 } else if (!strcmp(value
, "ff") ||
1264 !strcmp(value
, "fast_failover")) {
1265 gm
->type
= OFPGT11_FF
;
1267 error
= xasprintf("invalid group type %s", value
);
1271 } else if (!strcmp(name
, "bucket")) {
1272 error
= xstrdup("bucket is not needed");
1275 error
= xasprintf("unknown keyword %s", name
);
1279 if (gm
->group_id
== OFPG_ANY
) {
1280 error
= xstrdup("must specify a group_id");
1283 if (fields
& F_GROUP_TYPE
&& !had_type
) {
1284 error
= xstrdup("must specify a type");
1288 /* Validate buckets. */
1289 LIST_FOR_EACH (bucket
, list_node
, &gm
->buckets
) {
1290 if (bucket
->weight
!= 1 && gm
->type
!= OFPGT11_SELECT
) {
1291 error
= xstrdup("Only select groups can have bucket weights.");
1295 if (gm
->type
== OFPGT11_INDIRECT
&& !list_is_short(&gm
->buckets
)) {
1296 error
= xstrdup("Indirect groups can have at most one bucket.");
1302 ofputil_bucket_list_destroy(&gm
->buckets
);
1306 char * WARN_UNUSED_RESULT
1307 parse_ofp_group_mod_str(struct ofputil_group_mod
*gm
, uint16_t command
,
1309 enum ofputil_protocol
*usable_protocols
)
1311 char *string
= xstrdup(str_
);
1312 char *error
= parse_ofp_group_mod_str__(gm
, command
, string
,
1317 ofputil_bucket_list_destroy(&gm
->buckets
);
1322 char * WARN_UNUSED_RESULT
1323 parse_ofp_group_mod_file(const char *file_name
, uint16_t command
,
1324 struct ofputil_group_mod
**gms
, size_t *n_gms
,
1325 enum ofputil_protocol
*usable_protocols
)
1327 size_t allocated_gms
;
1335 stream
= !strcmp(file_name
, "-") ? stdin
: fopen(file_name
, "r");
1336 if (stream
== NULL
) {
1337 return xasprintf("%s: open failed (%s)",
1338 file_name
, ovs_strerror(errno
));
1341 allocated_gms
= *n_gms
;
1344 *usable_protocols
= OFPUTIL_P_OF11_UP
;
1345 while (!ds_get_preprocessed_line(&s
, stream
, &line_number
)) {
1346 enum ofputil_protocol usable
;
1349 if (*n_gms
>= allocated_gms
) {
1352 *gms
= x2nrealloc(*gms
, &allocated_gms
, sizeof **gms
);
1353 for (i
= 0; i
< *n_gms
; i
++) {
1354 list_moved(&(*gms
)[i
].buckets
);
1357 error
= parse_ofp_group_mod_str(&(*gms
)[*n_gms
], command
, ds_cstr(&s
),
1362 for (i
= 0; i
< *n_gms
; i
++) {
1363 ofputil_bucket_list_destroy(&(*gms
)[i
].buckets
);
1370 if (stream
!= stdin
) {
1374 return xasprintf("%s:%d: %s", file_name
, line_number
, error
);
1376 *usable_protocols
&= usable
;
1381 if (stream
!= stdin
) {