2 * Copyright (c) 2010, 2011, 2012, 2013, 2014, 2015, 2016 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.
22 #include <netinet/in.h>
24 #include "byte-order.h"
25 #include "dp-packet.h"
27 #include "multipath.h"
30 #include "openflow/openflow.h"
31 #include "openvswitch/dynamic-string.h"
32 #include "openvswitch/meta-flow.h"
33 #include "openvswitch/ofp-actions.h"
34 #include "openvswitch/ofp-parse.h"
35 #include "openvswitch/ofp-util.h"
36 #include "openvswitch/ofpbuf.h"
37 #include "openvswitch/vconn.h"
38 #include "ovs-thread.h"
41 #include "socket-util.h"
44 /* Parses 'str' as an 8-bit unsigned integer into '*valuep'.
46 * 'name' describes the value parsed in an error message, if any.
48 * Returns NULL if successful, otherwise a malloc()'d string describing the
49 * error. The caller is responsible for freeing the returned string. */
50 char * OVS_WARN_UNUSED_RESULT
51 str_to_u8(const char *str
, const char *name
, uint8_t *valuep
)
55 if (!str_to_int(str
, 0, &value
) || value
< 0 || value
> 255) {
56 return xasprintf("invalid %s \"%s\"", name
, str
);
62 /* Parses 'str' as a 16-bit unsigned integer into '*valuep'.
64 * 'name' describes the value parsed in an error message, if any.
66 * Returns NULL if successful, otherwise a malloc()'d string describing the
67 * error. The caller is responsible for freeing the returned string. */
68 char * OVS_WARN_UNUSED_RESULT
69 str_to_u16(const char *str
, const char *name
, uint16_t *valuep
)
73 if (!str_to_int(str
, 0, &value
) || value
< 0 || value
> 65535) {
74 return xasprintf("invalid %s \"%s\"", name
, str
);
80 /* Parses 'str' as a 32-bit unsigned integer into '*valuep'.
82 * Returns NULL if successful, otherwise a malloc()'d string describing the
83 * error. The caller is responsible for freeing the returned string. */
84 char * OVS_WARN_UNUSED_RESULT
85 str_to_u32(const char *str
, uint32_t *valuep
)
91 return xstrdup("missing required numeric argument");
95 value
= strtoul(str
, &tail
, 0);
96 if (errno
== EINVAL
|| errno
== ERANGE
|| *tail
) {
97 return xasprintf("invalid numeric format %s", str
);
103 /* Parses 'str' as an 64-bit unsigned integer into '*valuep'.
105 * Returns NULL if successful, otherwise a malloc()'d string describing the
106 * error. The caller is responsible for freeing the returned string. */
107 char * OVS_WARN_UNUSED_RESULT
108 str_to_u64(const char *str
, uint64_t *valuep
)
114 return xstrdup("missing required numeric argument");
118 value
= strtoull(str
, &tail
, 0);
119 if (errno
== EINVAL
|| errno
== ERANGE
|| *tail
) {
120 return xasprintf("invalid numeric format %s", str
);
126 /* Parses 'str' as an 64-bit unsigned integer in network byte order into
129 * Returns NULL if successful, otherwise a malloc()'d string describing the
130 * error. The caller is responsible for freeing the returned string. */
131 char * OVS_WARN_UNUSED_RESULT
132 str_to_be64(const char *str
, ovs_be64
*valuep
)
137 error
= str_to_u64(str
, &value
);
139 *valuep
= htonll(value
);
144 /* Parses 'str' as an Ethernet address into 'mac'.
146 * Returns NULL if successful, otherwise a malloc()'d string describing the
147 * error. The caller is responsible for freeing the returned string. */
148 char * OVS_WARN_UNUSED_RESULT
149 str_to_mac(const char *str
, struct eth_addr
*mac
)
151 if (!ovs_scan(str
, ETH_ADDR_SCAN_FMT
, ETH_ADDR_SCAN_ARGS(*mac
))) {
152 return xasprintf("invalid mac address %s", str
);
157 /* Parses 'str' as an IP address into '*ip'.
159 * Returns NULL if successful, otherwise a malloc()'d string describing the
160 * error. The caller is responsible for freeing the returned string. */
161 char * OVS_WARN_UNUSED_RESULT
162 str_to_ip(const char *str
, ovs_be32
*ip
)
164 struct in_addr in_addr
;
166 if (lookup_ip(str
, &in_addr
)) {
167 return xasprintf("%s: could not convert to IP address", str
);
169 *ip
= in_addr
.s_addr
;
173 /* Parses 'str' as a conntrack helper into 'alg'.
175 * Returns NULL if successful, otherwise a malloc()'d string describing the
176 * error. The caller is responsible for freeing the returned string. */
177 char * OVS_WARN_UNUSED_RESULT
178 str_to_connhelper(const char *str
, uint16_t *alg
)
180 if (!strcmp(str
, "ftp")) {
184 if (!strcmp(str
, "tftp")) {
188 return xasprintf("invalid conntrack helper \"%s\"", str
);
198 parse_protocol(const char *name
, const struct protocol
**p_out
)
200 static const struct protocol protocols
[] = {
201 { "ip", ETH_TYPE_IP
, 0 },
202 { "ipv4", ETH_TYPE_IP
, 0 },
203 { "ip4", ETH_TYPE_IP
, 0 },
204 { "arp", ETH_TYPE_ARP
, 0 },
205 { "icmp", ETH_TYPE_IP
, IPPROTO_ICMP
},
206 { "tcp", ETH_TYPE_IP
, IPPROTO_TCP
},
207 { "udp", ETH_TYPE_IP
, IPPROTO_UDP
},
208 { "sctp", ETH_TYPE_IP
, IPPROTO_SCTP
},
209 { "ipv6", ETH_TYPE_IPV6
, 0 },
210 { "ip6", ETH_TYPE_IPV6
, 0 },
211 { "icmp6", ETH_TYPE_IPV6
, IPPROTO_ICMPV6
},
212 { "tcp6", ETH_TYPE_IPV6
, IPPROTO_TCP
},
213 { "udp6", ETH_TYPE_IPV6
, IPPROTO_UDP
},
214 { "sctp6", ETH_TYPE_IPV6
, IPPROTO_SCTP
},
215 { "rarp", ETH_TYPE_RARP
, 0},
216 { "mpls", ETH_TYPE_MPLS
, 0 },
217 { "mplsm", ETH_TYPE_MPLS_MCAST
, 0 },
219 const struct protocol
*p
;
221 for (p
= protocols
; p
< &protocols
[ARRAY_SIZE(protocols
)]; p
++) {
222 if (!strcmp(p
->name
, name
)) {
231 /* Parses 's' as the (possibly masked) value of field 'mf', and updates
232 * 'match' appropriately. Restricts the set of usable protocols to ones
233 * supporting the parsed field.
235 * Returns NULL if successful, otherwise a malloc()'d string describing the
236 * error. The caller is responsible for freeing the returned string. */
237 static char * OVS_WARN_UNUSED_RESULT
238 parse_field(const struct mf_field
*mf
, const char *s
, struct match
*match
,
239 enum ofputil_protocol
*usable_protocols
)
241 union mf_value value
, mask
;
245 /* If there's no string, we're just trying to match on the
246 * existence of the field, so use a no-op value. */
250 error
= mf_parse(mf
, s
, &value
, &mask
);
252 *usable_protocols
&= mf_set(mf
, &value
, &mask
, match
, &error
);
257 /* Parses 'str_value' as the value of subfield 'name', and updates
258 * 'match' appropriately. Restricts the set of usable protocols to ones
259 * supporting the parsed field.
261 * Returns NULL if successful, otherwise a malloc()'d string describing the
262 * error. The caller is responsible for freeing the returned string. */
263 static char * OVS_WARN_UNUSED_RESULT
264 parse_subfield(const char *name
, const char *str_value
, struct match
*match
,
265 enum ofputil_protocol
*usable_protocols
)
267 struct mf_subfield sf
;
270 error
= mf_parse_subfield(&sf
, name
);
274 if (parse_int_string(str_value
, (uint8_t *)&val
, sf
.field
->n_bytes
,
275 &tail
) || *tail
!= 0) {
276 return xasprintf("%s: cannot parse integer value: %s", name
,
279 if (!bitwise_is_all_zeros(&val
, sf
.field
->n_bytes
, sf
.n_bits
,
280 sf
.field
->n_bytes
* 8 - sf
.n_bits
)) {
284 mf_format(sf
.field
, &val
, NULL
, &ds
);
285 error
= xasprintf("%s: value %s does not fit into %d bits",
286 name
, ds_cstr(&ds
), sf
.n_bits
);
291 const struct mf_field
*field
= sf
.field
;
292 union mf_value value
, mask
;
293 unsigned int size
= field
->n_bytes
;
295 mf_get(field
, match
, &value
, &mask
);
296 bitwise_copy(&val
, size
, 0, &value
, size
, sf
.ofs
, sf
.n_bits
);
297 bitwise_one ( &mask
, size
, sf
.ofs
, sf
.n_bits
);
298 *usable_protocols
&= mf_set(field
, &value
, &mask
, match
, &error
);
304 extract_actions(char *s
)
306 s
= strstr(s
, "action");
309 s
= strchr(s
+ 1, '=');
310 return s
? s
+ 1 : NULL
;
317 static char * OVS_WARN_UNUSED_RESULT
318 parse_ofp_str__(struct ofputil_flow_mod
*fm
, int command
, char *string
,
319 enum ofputil_protocol
*usable_protocols
)
324 F_IMPORTANCE
= 1 << 2,
329 char *act_str
= NULL
;
332 *usable_protocols
= OFPUTIL_P_ANY
;
337 string
+= strspn(string
, " \t\r\n"); /* Skip white space. */
338 len
= strcspn(string
, ", \t\r\n"); /* Get length of the first token. */
340 if (!strncmp(string
, "add", len
)) {
342 } else if (!strncmp(string
, "delete", len
)) {
343 command
= OFPFC_DELETE
;
344 } else if (!strncmp(string
, "delete_strict", len
)) {
345 command
= OFPFC_DELETE_STRICT
;
346 } else if (!strncmp(string
, "modify", len
)) {
347 command
= OFPFC_MODIFY
;
348 } else if (!strncmp(string
, "modify_strict", len
)) {
349 command
= OFPFC_MODIFY_STRICT
;
363 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
| F_IMPORTANCE
;
370 case OFPFC_DELETE_STRICT
:
371 fields
= F_OUT_PORT
| F_PRIORITY
;
375 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
;
378 case OFPFC_MODIFY_STRICT
:
379 fields
= F_ACTIONS
| F_TIMEOUT
| F_PRIORITY
| F_FLAGS
;
386 *fm
= (struct ofputil_flow_mod
) {
387 .match
= MATCH_CATCHALL_INITIALIZER
,
388 .priority
= OFP_DEFAULT_PRIORITY
,
391 .buffer_id
= UINT32_MAX
,
392 .out_port
= OFPP_ANY
,
393 .out_group
= OFPG_ANY
,
395 /* For modify, by default, don't update the cookie. */
396 if (command
== OFPFC_MODIFY
|| command
== OFPFC_MODIFY_STRICT
) {
397 fm
->new_cookie
= OVS_BE64_MAX
;
400 if (fields
& F_ACTIONS
) {
401 act_str
= extract_actions(string
);
403 return xstrdup("must specify an action");
407 while (ofputil_parse_key_value(&string
, &name
, &value
)) {
408 const struct protocol
*p
;
409 const struct mf_field
*mf
;
412 if (parse_protocol(name
, &p
)) {
413 match_set_dl_type(&fm
->match
, htons(p
->dl_type
));
415 match_set_nw_proto(&fm
->match
, p
->nw_proto
);
417 } else if (fields
& F_FLAGS
&& !strcmp(name
, "send_flow_rem")) {
418 fm
->flags
|= OFPUTIL_FF_SEND_FLOW_REM
;
419 } else if (fields
& F_FLAGS
&& !strcmp(name
, "check_overlap")) {
420 fm
->flags
|= OFPUTIL_FF_CHECK_OVERLAP
;
421 } else if (fields
& F_FLAGS
&& !strcmp(name
, "reset_counts")) {
422 fm
->flags
|= OFPUTIL_FF_RESET_COUNTS
;
423 *usable_protocols
&= OFPUTIL_P_OF12_UP
;
424 } else if (fields
& F_FLAGS
&& !strcmp(name
, "no_packet_counts")) {
425 fm
->flags
|= OFPUTIL_FF_NO_PKT_COUNTS
;
426 *usable_protocols
&= OFPUTIL_P_OF13_UP
;
427 } else if (fields
& F_FLAGS
&& !strcmp(name
, "no_byte_counts")) {
428 fm
->flags
|= OFPUTIL_FF_NO_BYT_COUNTS
;
429 *usable_protocols
&= OFPUTIL_P_OF13_UP
;
430 } else if (!strcmp(name
, "no_readonly_table")
431 || !strcmp(name
, "allow_hidden_fields")) {
432 /* ignore these fields. */
433 } else if ((mf
= mf_from_name(name
)) != NULL
) {
434 error
= parse_field(mf
, value
, &fm
->match
, usable_protocols
);
435 } else if (strchr(name
, '[')) {
436 error
= parse_subfield(name
, value
, &fm
->match
, usable_protocols
);
439 return xasprintf("field %s missing value", name
);
442 if (!strcmp(name
, "table")) {
443 error
= str_to_u8(value
, "table", &fm
->table_id
);
444 if (fm
->table_id
!= 0xff) {
445 *usable_protocols
&= OFPUTIL_P_TID
;
447 } else if (fields
& F_OUT_PORT
&& !strcmp(name
, "out_port")) {
448 if (!ofputil_port_from_string(value
, &fm
->out_port
)) {
449 error
= xasprintf("%s is not a valid OpenFlow port",
452 } else if (fields
& F_OUT_PORT
&& !strcmp(name
, "out_group")) {
453 *usable_protocols
&= OFPUTIL_P_OF11_UP
;
454 if (!ofputil_group_from_string(value
, &fm
->out_group
)) {
455 error
= xasprintf("%s is not a valid OpenFlow group",
458 } else if (fields
& F_PRIORITY
&& !strcmp(name
, "priority")) {
459 uint16_t priority
= 0;
461 error
= str_to_u16(value
, name
, &priority
);
462 fm
->priority
= priority
;
463 } else if (fields
& F_TIMEOUT
&& !strcmp(name
, "idle_timeout")) {
464 error
= str_to_u16(value
, name
, &fm
->idle_timeout
);
465 } else if (fields
& F_TIMEOUT
&& !strcmp(name
, "hard_timeout")) {
466 error
= str_to_u16(value
, name
, &fm
->hard_timeout
);
467 } else if (fields
& F_IMPORTANCE
&& !strcmp(name
, "importance")) {
468 error
= str_to_u16(value
, name
, &fm
->importance
);
469 } else if (!strcmp(name
, "cookie")) {
470 char *mask
= strchr(value
, '/');
473 /* A mask means we're searching for a cookie. */
474 if (command
== OFPFC_ADD
) {
475 return xstrdup("flow additions cannot use "
479 error
= str_to_be64(value
, &fm
->cookie
);
483 error
= str_to_be64(mask
+ 1, &fm
->cookie_mask
);
485 /* Matching of the cookie is only supported through NXM or
487 if (fm
->cookie_mask
!= htonll(0)) {
488 *usable_protocols
&= OFPUTIL_P_NXM_OF11_UP
;
491 /* No mask means that the cookie is being set. */
492 if (command
!= OFPFC_ADD
&& command
!= OFPFC_MODIFY
493 && command
!= OFPFC_MODIFY_STRICT
) {
494 return xstrdup("cannot set cookie");
496 error
= str_to_be64(value
, &fm
->new_cookie
);
497 fm
->modify_cookie
= true;
499 } else if (!strcmp(name
, "duration")
500 || !strcmp(name
, "n_packets")
501 || !strcmp(name
, "n_bytes")
502 || !strcmp(name
, "idle_age")
503 || !strcmp(name
, "hard_age")) {
504 /* Ignore these, so that users can feed the output of
505 * "ovs-ofctl dump-flows" back into commands that parse
508 error
= xasprintf("unknown keyword %s", name
);
516 /* Check for usable protocol interdependencies between match fields. */
517 if (fm
->match
.flow
.dl_type
== htons(ETH_TYPE_IPV6
)) {
518 const struct flow_wildcards
*wc
= &fm
->match
.wc
;
519 /* Only NXM and OXM support matching L3 and L4 fields within IPv6.
521 * (IPv6 specific fields as well as arp_sha, arp_tha, nw_frag, and
522 * nw_ttl are covered elsewhere so they don't need to be included in
525 if (wc
->masks
.nw_proto
|| wc
->masks
.nw_tos
526 || wc
->masks
.tp_src
|| wc
->masks
.tp_dst
) {
527 *usable_protocols
&= OFPUTIL_P_NXM_OXM_ANY
;
530 if (!fm
->cookie_mask
&& fm
->new_cookie
== OVS_BE64_MAX
531 && (command
== OFPFC_MODIFY
|| command
== OFPFC_MODIFY_STRICT
)) {
532 /* On modifies without a mask, we are supposed to add a flow if
533 * one does not exist. If a cookie wasn't been specified, use a
534 * default of zero. */
535 fm
->new_cookie
= htonll(0);
537 if (fields
& F_ACTIONS
) {
538 enum ofputil_protocol action_usable_protocols
;
539 struct ofpbuf ofpacts
;
542 ofpbuf_init(&ofpacts
, 32);
543 error
= ofpacts_parse_instructions(act_str
, &ofpacts
,
544 &action_usable_protocols
);
545 *usable_protocols
&= action_usable_protocols
;
549 err
= ofpacts_check(ofpacts
.data
, ofpacts
.size
, &fm
->match
,
550 OFPP_MAX
, fm
->table_id
, 255, usable_protocols
);
551 if (!err
&& !*usable_protocols
) {
552 err
= OFPERR_OFPBAC_MATCH_INCONSISTENT
;
555 error
= xasprintf("actions are invalid with specified match "
556 "(%s)", ofperr_to_string(err
));
561 ofpbuf_uninit(&ofpacts
);
565 fm
->ofpacts_len
= ofpacts
.size
;
566 fm
->ofpacts
= ofpbuf_steal_data(&ofpacts
);
575 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
576 * page) into 'fm' for sending the specified flow_mod 'command' to a switch.
577 * Returns the set of usable protocols in '*usable_protocols'.
579 * To parse syntax for an OFPT_FLOW_MOD (or NXT_FLOW_MOD), use an OFPFC_*
580 * constant for 'command'. To parse syntax for an OFPST_FLOW or
581 * OFPST_AGGREGATE (or NXST_FLOW or NXST_AGGREGATE), use -1 for 'command'.
583 * If 'command' is given as -2, 'str_' may begin with a command name ("add",
584 * "modify", "delete", "modify_strict", or "delete_strict"). A missing command
585 * name is treated as "add".
587 * Returns NULL if successful, otherwise a malloc()'d string describing the
588 * error. The caller is responsible for freeing the returned string. */
589 char * OVS_WARN_UNUSED_RESULT
590 parse_ofp_str(struct ofputil_flow_mod
*fm
, int command
, const char *str_
,
591 enum ofputil_protocol
*usable_protocols
)
593 char *string
= xstrdup(str_
);
596 error
= parse_ofp_str__(fm
, command
, string
, usable_protocols
);
606 /* Parse a string representation of a OFPT_PACKET_OUT to '*po'. If successful,
607 * both 'po->ofpacts' and 'po->packet' must be free()d by the caller. */
608 static char * OVS_WARN_UNUSED_RESULT
609 parse_ofp_packet_out_str__(struct ofputil_packet_out
*po
, char *string
,
610 enum ofputil_protocol
*usable_protocols
)
612 enum ofputil_protocol action_usable_protocols
;
613 uint64_t stub
[256 / 8];
614 struct ofpbuf ofpacts
= OFPBUF_STUB_INITIALIZER(stub
);
615 struct dp_packet
*packet
= NULL
;
616 char *act_str
= NULL
;
620 *usable_protocols
= OFPUTIL_P_ANY
;
622 *po
= (struct ofputil_packet_out
) {
623 .buffer_id
= UINT32_MAX
,
625 match_init_catchall(&po
->flow_metadata
);
626 match_set_in_port(&po
->flow_metadata
, OFPP_CONTROLLER
);
628 act_str
= extract_actions(string
);
630 while (ofputil_parse_key_value(&string
, &name
, &value
)) {
632 error
= xasprintf("field %s missing value", name
);
636 if (!strcmp(name
, "in_port")) {
638 if (!ofputil_port_from_string(value
, &in_port
)) {
639 error
= xasprintf("%s is not a valid OpenFlow port", value
);
642 if (ofp_to_u16(in_port
) > ofp_to_u16(OFPP_MAX
)
643 && in_port
!= OFPP_LOCAL
644 && in_port
!= OFPP_NONE
645 && in_port
!= OFPP_CONTROLLER
) {
647 "%s is not a valid OpenFlow port for PACKET_OUT",
651 match_set_in_port(&po
->flow_metadata
, in_port
);
652 } else if (!strcmp(name
, "packet")) {
653 const char *error_msg
= eth_from_hex(value
, &packet
);
655 error
= xasprintf("%s: %s", name
, error_msg
);
659 const struct mf_field
*mf
= mf_from_name(name
);
661 error
= xasprintf("unknown keyword %s", name
);
665 error
= parse_field(mf
, value
, &po
->flow_metadata
,
670 if (!mf_is_pipeline_field(mf
)) {
671 error
= xasprintf("%s is not a valid pipeline field "
672 "for PACKET_OUT", name
);
678 if (!packet
|| !dp_packet_size(packet
)) {
679 error
= xstrdup("must specify packet");
684 error
= ofpacts_parse_actions(act_str
, &ofpacts
,
685 &action_usable_protocols
);
686 *usable_protocols
&= action_usable_protocols
;
691 po
->ofpacts_len
= ofpacts
.size
;
692 po
->ofpacts
= ofpbuf_steal_data(&ofpacts
);
694 po
->packet_len
= dp_packet_size(packet
);
695 po
->packet
= dp_packet_steal_data(packet
);
697 ofpbuf_uninit(&ofpacts
);
698 dp_packet_delete(packet
);
702 /* Convert 'str_' (as described in the Packet-Out Syntax section of the
703 * ovs-ofctl man page) into 'po' for sending a OFPT_PACKET_OUT message to a
704 * switch. Returns the set of usable protocols in '*usable_protocols'.
706 * Returns NULL if successful, otherwise a malloc()'d string describing the
707 * error. The caller is responsible for freeing the returned string. */
708 char * OVS_WARN_UNUSED_RESULT
709 parse_ofp_packet_out_str(struct ofputil_packet_out
*po
, const char *str_
,
710 enum ofputil_protocol
*usable_protocols
)
712 char *string
= xstrdup(str_
);
715 error
= parse_ofp_packet_out_str__(po
, string
, usable_protocols
);
725 static char * OVS_WARN_UNUSED_RESULT
726 parse_ofp_meter_mod_str__(struct ofputil_meter_mod
*mm
, char *string
,
727 struct ofpbuf
*bands
, int command
,
728 enum ofputil_protocol
*usable_protocols
)
735 char *save_ptr
= NULL
;
736 char *band_str
= NULL
;
739 /* Meters require at least OF 1.3. */
740 *usable_protocols
= OFPUTIL_P_OF13_UP
;
748 fields
= F_METER
| F_FLAGS
| F_BANDS
;
756 fields
= F_METER
| F_FLAGS
| F_BANDS
;
763 mm
->command
= command
;
764 mm
->meter
.meter_id
= 0;
766 if (fields
& F_BANDS
) {
767 band_str
= strstr(string
, "band");
769 return xstrdup("must specify bands");
773 band_str
= strchr(band_str
+ 1, '=');
775 return xstrdup("must specify bands");
780 for (name
= strtok_r(string
, "=, \t\r\n", &save_ptr
); name
;
781 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
783 if (fields
& F_FLAGS
&& !strcmp(name
, "kbps")) {
784 mm
->meter
.flags
|= OFPMF13_KBPS
;
785 } else if (fields
& F_FLAGS
&& !strcmp(name
, "pktps")) {
786 mm
->meter
.flags
|= OFPMF13_PKTPS
;
787 } else if (fields
& F_FLAGS
&& !strcmp(name
, "burst")) {
788 mm
->meter
.flags
|= OFPMF13_BURST
;
789 } else if (fields
& F_FLAGS
&& !strcmp(name
, "stats")) {
790 mm
->meter
.flags
|= OFPMF13_STATS
;
794 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
796 return xasprintf("field %s missing value", name
);
799 if (!strcmp(name
, "meter")) {
800 if (!strcmp(value
, "all")) {
801 mm
->meter
.meter_id
= OFPM13_ALL
;
802 } else if (!strcmp(value
, "controller")) {
803 mm
->meter
.meter_id
= OFPM13_CONTROLLER
;
804 } else if (!strcmp(value
, "slowpath")) {
805 mm
->meter
.meter_id
= OFPM13_SLOWPATH
;
807 char *error
= str_to_u32(value
, &mm
->meter
.meter_id
);
811 if (mm
->meter
.meter_id
> OFPM13_MAX
812 || !mm
->meter
.meter_id
) {
813 return xasprintf("invalid value for %s", name
);
817 return xasprintf("unknown keyword %s", name
);
821 if (fields
& F_METER
&& !mm
->meter
.meter_id
) {
822 return xstrdup("must specify 'meter'");
824 if (fields
& F_FLAGS
&& !mm
->meter
.flags
) {
825 return xstrdup("meter must specify either 'kbps' or 'pktps'");
828 if (fields
& F_BANDS
) {
829 uint16_t n_bands
= 0;
830 struct ofputil_meter_band
*band
= NULL
;
833 for (name
= strtok_r(band_str
, "=, \t\r\n", &save_ptr
); name
;
834 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
838 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
840 return xasprintf("field %s missing value", name
);
843 if (!strcmp(name
, "type")) {
844 /* Start a new band */
845 band
= ofpbuf_put_zeros(bands
, sizeof *band
);
848 if (!strcmp(value
, "drop")) {
849 band
->type
= OFPMBT13_DROP
;
850 } else if (!strcmp(value
, "dscp_remark")) {
851 band
->type
= OFPMBT13_DSCP_REMARK
;
853 return xasprintf("field %s unknown value %s", name
, value
);
855 } else if (!band
|| !band
->type
) {
856 return xstrdup("band must start with the 'type' keyword");
857 } else if (!strcmp(name
, "rate")) {
858 char *error
= str_to_u32(value
, &band
->rate
);
862 } else if (!strcmp(name
, "burst_size")) {
863 char *error
= str_to_u32(value
, &band
->burst_size
);
867 } else if (!strcmp(name
, "prec_level")) {
868 char *error
= str_to_u8(value
, name
, &band
->prec_level
);
873 return xasprintf("unknown keyword %s", name
);
878 return xstrdup("meter must have bands");
881 mm
->meter
.n_bands
= n_bands
;
882 mm
->meter
.bands
= ofpbuf_steal_data(bands
);
884 for (i
= 0; i
< n_bands
; ++i
) {
885 band
= &mm
->meter
.bands
[i
];
888 return xstrdup("band must have 'type'");
890 if (band
->type
== OFPMBT13_DSCP_REMARK
) {
891 if (!band
->prec_level
) {
892 return xstrdup("'dscp_remark' band must have"
896 if (band
->prec_level
) {
897 return xstrdup("Only 'dscp_remark' band may have"
902 return xstrdup("band must have 'rate'");
904 if (mm
->meter
.flags
& OFPMF13_BURST
) {
905 if (!band
->burst_size
) {
906 return xstrdup("band must have 'burst_size' "
907 "when 'burst' flag is set");
910 if (band
->burst_size
) {
911 return xstrdup("band may have 'burst_size' only "
912 "when 'burst' flag is set");
917 mm
->meter
.n_bands
= 0;
918 mm
->meter
.bands
= NULL
;
924 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
925 * page) into 'mm' for sending the specified meter_mod 'command' to a switch.
927 * Returns NULL if successful, otherwise a malloc()'d string describing the
928 * error. The caller is responsible for freeing the returned string. */
929 char * OVS_WARN_UNUSED_RESULT
930 parse_ofp_meter_mod_str(struct ofputil_meter_mod
*mm
, const char *str_
,
931 int command
, enum ofputil_protocol
*usable_protocols
)
937 ofpbuf_init(&bands
, 64);
938 string
= xstrdup(str_
);
940 error
= parse_ofp_meter_mod_str__(mm
, string
, &bands
, command
,
944 ofpbuf_uninit(&bands
);
949 static char * OVS_WARN_UNUSED_RESULT
950 parse_flow_monitor_request__(struct ofputil_flow_monitor_request
*fmr
,
951 const char *str_
, char *string
,
952 enum ofputil_protocol
*usable_protocols
)
954 static atomic_count id
= ATOMIC_COUNT_INIT(0);
957 fmr
->id
= atomic_count_inc(&id
);
959 fmr
->flags
= (NXFMF_INITIAL
| NXFMF_ADD
| NXFMF_DELETE
| NXFMF_MODIFY
960 | NXFMF_OWN
| NXFMF_ACTIONS
);
961 fmr
->out_port
= OFPP_NONE
;
962 fmr
->table_id
= 0xff;
963 match_init_catchall(&fmr
->match
);
965 while (ofputil_parse_key_value(&string
, &name
, &value
)) {
966 const struct protocol
*p
;
969 if (!strcmp(name
, "!initial")) {
970 fmr
->flags
&= ~NXFMF_INITIAL
;
971 } else if (!strcmp(name
, "!add")) {
972 fmr
->flags
&= ~NXFMF_ADD
;
973 } else if (!strcmp(name
, "!delete")) {
974 fmr
->flags
&= ~NXFMF_DELETE
;
975 } else if (!strcmp(name
, "!modify")) {
976 fmr
->flags
&= ~NXFMF_MODIFY
;
977 } else if (!strcmp(name
, "!actions")) {
978 fmr
->flags
&= ~NXFMF_ACTIONS
;
979 } else if (!strcmp(name
, "!own")) {
980 fmr
->flags
&= ~NXFMF_OWN
;
981 } else if (parse_protocol(name
, &p
)) {
982 match_set_dl_type(&fmr
->match
, htons(p
->dl_type
));
984 match_set_nw_proto(&fmr
->match
, p
->nw_proto
);
986 } else if (mf_from_name(name
)) {
987 error
= parse_field(mf_from_name(name
), value
, &fmr
->match
,
991 return xasprintf("%s: field %s missing value", str_
, name
);
994 if (!strcmp(name
, "table")) {
995 error
= str_to_u8(value
, "table", &fmr
->table_id
);
996 } else if (!strcmp(name
, "out_port")) {
997 fmr
->out_port
= u16_to_ofp(atoi(value
));
999 return xasprintf("%s: unknown keyword %s", str_
, name
);
1010 /* Convert 'str_' (as described in the documentation for the "monitor" command
1011 * in the ovs-ofctl man page) into 'fmr'.
1013 * Returns NULL if successful, otherwise a malloc()'d string describing the
1014 * error. The caller is responsible for freeing the returned string. */
1015 char * OVS_WARN_UNUSED_RESULT
1016 parse_flow_monitor_request(struct ofputil_flow_monitor_request
*fmr
,
1018 enum ofputil_protocol
*usable_protocols
)
1020 char *string
= xstrdup(str_
);
1021 char *error
= parse_flow_monitor_request__(fmr
, str_
, string
,
1027 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
1028 * (one of OFPFC_*) into 'fm'.
1030 * If 'command' is given as -2, 'string' may begin with a command name ("add",
1031 * "modify", "delete", "modify_strict", or "delete_strict"). A missing command
1032 * name is treated as "add".
1034 * Returns NULL if successful, otherwise a malloc()'d string describing the
1035 * error. The caller is responsible for freeing the returned string. */
1036 char * OVS_WARN_UNUSED_RESULT
1037 parse_ofp_flow_mod_str(struct ofputil_flow_mod
*fm
, const char *string
,
1039 enum ofputil_protocol
*usable_protocols
)
1041 char *error
= parse_ofp_str(fm
, command
, string
, usable_protocols
);
1044 /* Normalize a copy of the match. This ensures that non-normalized
1045 * flows get logged but doesn't affect what gets sent to the switch, so
1046 * that the switch can do whatever it likes with the flow. */
1047 struct match match_copy
= fm
->match
;
1048 ofputil_normalize_match(&match_copy
);
1054 /* Convert 'setting' (as described for the "mod-table" command
1055 * in ovs-ofctl man page) into 'tm->table_vacancy->vacancy_up' and
1056 * 'tm->table_vacancy->vacancy_down' threshold values.
1057 * For the two threshold values, value of vacancy_up is always greater
1058 * than value of vacancy_down.
1060 * Returns NULL if successful, otherwise a malloc()'d string describing the
1061 * error. The caller is responsible for freeing the returned string. */
1062 char * OVS_WARN_UNUSED_RESULT
1063 parse_ofp_table_vacancy(struct ofputil_table_mod
*tm
, const char *setting
)
1065 char *save_ptr
= NULL
;
1066 char *vac_up
, *vac_down
;
1067 char *value
= xstrdup(setting
);
1069 int vacancy_up
, vacancy_down
;
1071 strtok_r(value
, ":", &save_ptr
);
1072 vac_down
= strtok_r(NULL
, ",", &save_ptr
);
1074 ret_msg
= xasprintf("Vacancy down value missing");
1077 if (!str_to_int(vac_down
, 0, &vacancy_down
) ||
1078 vacancy_down
< 0 || vacancy_down
> 100) {
1079 ret_msg
= xasprintf("Invalid vacancy down value \"%s\"", vac_down
);
1082 vac_up
= strtok_r(NULL
, ",", &save_ptr
);
1084 ret_msg
= xasprintf("Vacancy up value missing");
1087 if (!str_to_int(vac_up
, 0, &vacancy_up
) ||
1088 vacancy_up
< 0 || vacancy_up
> 100) {
1089 ret_msg
= xasprintf("Invalid vacancy up value \"%s\"", vac_up
);
1092 if (vacancy_down
> vacancy_up
) {
1093 ret_msg
= xasprintf("Invalid vacancy range, vacancy up should be "
1094 "greater than vacancy down (%s)",
1095 ofperr_to_string(OFPERR_OFPBPC_BAD_VALUE
));
1100 tm
->table_vacancy
.vacancy_down
= vacancy_down
;
1101 tm
->table_vacancy
.vacancy_up
= vacancy_up
;
1109 /* Convert 'table_id' and 'setting' (as described for the "mod-table" command
1110 * in the ovs-ofctl man page) into 'tm' for sending a table_mod command to a
1113 * Stores a bitmap of the OpenFlow versions that are usable for 'tm' into
1114 * '*usable_versions'.
1116 * Returns NULL if successful, otherwise a malloc()'d string describing the
1117 * error. The caller is responsible for freeing the returned string. */
1118 char * OVS_WARN_UNUSED_RESULT
1119 parse_ofp_table_mod(struct ofputil_table_mod
*tm
, const char *table_id
,
1120 const char *setting
, uint32_t *usable_versions
)
1122 *usable_versions
= 0;
1123 if (!strcasecmp(table_id
, "all")) {
1124 tm
->table_id
= OFPTT_ALL
;
1126 char *error
= str_to_u8(table_id
, "table_id", &tm
->table_id
);
1132 tm
->miss
= OFPUTIL_TABLE_MISS_DEFAULT
;
1133 tm
->eviction
= OFPUTIL_TABLE_EVICTION_DEFAULT
;
1134 tm
->eviction_flags
= UINT32_MAX
;
1135 tm
->vacancy
= OFPUTIL_TABLE_VACANCY_DEFAULT
;
1136 tm
->table_vacancy
.vacancy_down
= 0;
1137 tm
->table_vacancy
.vacancy_up
= 0;
1138 tm
->table_vacancy
.vacancy
= 0;
1139 /* Only OpenFlow 1.1 and 1.2 can configure table-miss via table_mod.
1140 * Only OpenFlow 1.4+ can configure eviction and vacancy events
1143 if (!strcmp(setting
, "controller")) {
1144 tm
->miss
= OFPUTIL_TABLE_MISS_CONTROLLER
;
1145 *usable_versions
= (1u << OFP11_VERSION
) | (1u << OFP12_VERSION
);
1146 } else if (!strcmp(setting
, "continue")) {
1147 tm
->miss
= OFPUTIL_TABLE_MISS_CONTINUE
;
1148 *usable_versions
= (1u << OFP11_VERSION
) | (1u << OFP12_VERSION
);
1149 } else if (!strcmp(setting
, "drop")) {
1150 tm
->miss
= OFPUTIL_TABLE_MISS_DROP
;
1151 *usable_versions
= (1u << OFP11_VERSION
) | (1u << OFP12_VERSION
);
1152 } else if (!strcmp(setting
, "evict")) {
1153 tm
->eviction
= OFPUTIL_TABLE_EVICTION_ON
;
1154 *usable_versions
= (1 << OFP14_VERSION
) | (1u << OFP15_VERSION
);
1155 } else if (!strcmp(setting
, "noevict")) {
1156 tm
->eviction
= OFPUTIL_TABLE_EVICTION_OFF
;
1157 *usable_versions
= (1 << OFP14_VERSION
) | (1u << OFP15_VERSION
);
1158 } else if (!strncmp(setting
, "vacancy", strcspn(setting
, ":"))) {
1159 tm
->vacancy
= OFPUTIL_TABLE_VACANCY_ON
;
1160 *usable_versions
= (1 << OFP14_VERSION
) | (1u << OFP15_VERSION
);
1161 char *error
= parse_ofp_table_vacancy(tm
, setting
);
1165 } else if (!strcmp(setting
, "novacancy")) {
1166 tm
->vacancy
= OFPUTIL_TABLE_VACANCY_OFF
;
1167 *usable_versions
= (1 << OFP14_VERSION
) | (1u << OFP15_VERSION
);
1169 return xasprintf("invalid table_mod setting %s", setting
);
1172 if (tm
->table_id
== 0xfe
1173 && tm
->miss
== OFPUTIL_TABLE_MISS_CONTINUE
) {
1174 return xstrdup("last table's flow miss handling can not be continue");
1181 /* Opens file 'file_name' and reads each line as a flow_mod of the specified
1182 * type (one of OFPFC_*). Stores each flow_mod in '*fm', an array allocated
1183 * on the caller's behalf, and the number of flow_mods in '*n_fms'.
1185 * If 'command' is given as -2, each line may start with a command name
1186 * ("add", "modify", "delete", "modify_strict", or "delete_strict"). A missing
1187 * command name is treated as "add".
1189 * Returns NULL if successful, otherwise a malloc()'d string describing the
1190 * error. The caller is responsible for freeing the returned string. */
1191 char * OVS_WARN_UNUSED_RESULT
1192 parse_ofp_flow_mod_file(const char *file_name
, int command
,
1193 struct ofputil_flow_mod
**fms
, size_t *n_fms
,
1194 enum ofputil_protocol
*usable_protocols
)
1196 size_t allocated_fms
;
1201 *usable_protocols
= OFPUTIL_P_ANY
;
1206 stream
= !strcmp(file_name
, "-") ? stdin
: fopen(file_name
, "r");
1207 if (stream
== NULL
) {
1208 return xasprintf("%s: open failed (%s)",
1209 file_name
, ovs_strerror(errno
));
1212 allocated_fms
= *n_fms
;
1215 while (!ds_get_preprocessed_line(&s
, stream
, &line_number
)) {
1217 enum ofputil_protocol usable
;
1219 if (*n_fms
>= allocated_fms
) {
1220 *fms
= x2nrealloc(*fms
, &allocated_fms
, sizeof **fms
);
1222 error
= parse_ofp_flow_mod_str(&(*fms
)[*n_fms
], ds_cstr(&s
), command
,
1228 for (i
= 0; i
< *n_fms
; i
++) {
1229 free(CONST_CAST(struct ofpact
*, (*fms
)[i
].ofpacts
));
1236 if (stream
!= stdin
) {
1240 err_msg
= xasprintf("%s:%d: %s", file_name
, line_number
, error
);
1244 *usable_protocols
&= usable
; /* Each line can narrow the set. */
1249 if (stream
!= stdin
) {
1255 char * OVS_WARN_UNUSED_RESULT
1256 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request
*fsr
,
1257 bool aggregate
, const char *string
,
1258 enum ofputil_protocol
*usable_protocols
)
1260 struct ofputil_flow_mod fm
;
1263 error
= parse_ofp_str(&fm
, -1, string
, usable_protocols
);
1268 /* Special table ID support not required for stats requests. */
1269 if (*usable_protocols
& OFPUTIL_P_OF10_STD_TID
) {
1270 *usable_protocols
|= OFPUTIL_P_OF10_STD
;
1272 if (*usable_protocols
& OFPUTIL_P_OF10_NXM_TID
) {
1273 *usable_protocols
|= OFPUTIL_P_OF10_NXM
;
1276 fsr
->aggregate
= aggregate
;
1277 fsr
->cookie
= fm
.cookie
;
1278 fsr
->cookie_mask
= fm
.cookie_mask
;
1279 fsr
->match
= fm
.match
;
1280 fsr
->out_port
= fm
.out_port
;
1281 fsr
->out_group
= fm
.out_group
;
1282 fsr
->table_id
= fm
.table_id
;
1286 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
1287 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
1288 * mf_field. Fields must be specified in a natural order for satisfying
1289 * prerequisites. If 'wc' is specified, masks the field in 'wc' for each of the
1290 * field specified in flow. If the map, 'names_portno' is specfied, converts
1291 * the in_port name into port no while setting the 'flow'.
1293 * Returns NULL on success, otherwise a malloc()'d string that explains the
1296 parse_ofp_exact_flow(struct flow
*flow
, struct flow_wildcards
*wc
,
1297 const struct tun_table
*tun_table
, const char *s
,
1298 const struct simap
*portno_names
)
1300 char *pos
, *key
, *value_s
;
1304 memset(flow
, 0, sizeof *flow
);
1306 memset(wc
, 0, sizeof *wc
);
1308 flow
->tunnel
.metadata
.tab
= tun_table
;
1310 pos
= copy
= xstrdup(s
);
1311 while (ofputil_parse_key_value(&pos
, &key
, &value_s
)) {
1312 const struct protocol
*p
;
1313 if (parse_protocol(key
, &p
)) {
1314 if (flow
->dl_type
) {
1315 error
= xasprintf("%s: Ethernet type set multiple times", s
);
1318 flow
->dl_type
= htons(p
->dl_type
);
1320 wc
->masks
.dl_type
= OVS_BE16_MAX
;
1324 if (flow
->nw_proto
) {
1325 error
= xasprintf("%s: network protocol set "
1326 "multiple times", s
);
1329 flow
->nw_proto
= p
->nw_proto
;
1331 wc
->masks
.nw_proto
= UINT8_MAX
;
1335 const struct mf_field
*mf
;
1336 union mf_value value
;
1339 mf
= mf_from_name(key
);
1341 error
= xasprintf("%s: unknown field %s", s
, key
);
1345 if (!mf_are_prereqs_ok(mf
, flow
, NULL
)) {
1346 error
= xasprintf("%s: prerequisites not met for setting %s",
1351 if (mf_is_set(mf
, flow
)) {
1352 error
= xasprintf("%s: field %s set multiple times", s
, key
);
1356 if (!strcmp(key
, "in_port")
1358 && simap_contains(portno_names
, value_s
)) {
1359 flow
->in_port
.ofp_port
= u16_to_ofp(
1360 simap_get(portno_names
, value_s
));
1362 wc
->masks
.in_port
.ofp_port
1363 = u16_to_ofp(ntohs(OVS_BE16_MAX
));
1366 field_error
= mf_parse_value(mf
, value_s
, &value
);
1368 error
= xasprintf("%s: bad value for %s (%s)",
1369 s
, key
, field_error
);
1374 mf_set_flow_value(mf
, &value
, flow
);
1376 mf_mask_field(mf
, wc
);
1382 if (!flow
->in_port
.ofp_port
) {
1383 flow
->in_port
.ofp_port
= OFPP_NONE
;
1390 memset(flow
, 0, sizeof *flow
);
1392 memset(wc
, 0, sizeof *wc
);
1398 static char * OVS_WARN_UNUSED_RESULT
1399 parse_bucket_str(struct ofputil_bucket
*bucket
, char *str_
, uint8_t group_type
,
1400 enum ofputil_protocol
*usable_protocols
)
1402 char *pos
, *key
, *value
;
1403 struct ofpbuf ofpacts
;
1407 bucket
->weight
= group_type
== OFPGT11_SELECT
? 1 : 0;
1408 bucket
->bucket_id
= OFPG15_BUCKET_ALL
;
1409 bucket
->watch_port
= OFPP_ANY
;
1410 bucket
->watch_group
= OFPG_ANY
;
1416 while (ofputil_parse_key_value(&pos
, &key
, &value
)) {
1417 if (!strcasecmp(key
, "weight")) {
1418 error
= str_to_u16(value
, "weight", &bucket
->weight
);
1419 } else if (!strcasecmp(key
, "watch_port")) {
1420 if (!ofputil_port_from_string(value
, &bucket
->watch_port
)
1421 || (ofp_to_u16(bucket
->watch_port
) >= ofp_to_u16(OFPP_MAX
)
1422 && bucket
->watch_port
!= OFPP_ANY
)) {
1423 error
= xasprintf("%s: invalid watch_port", value
);
1425 } else if (!strcasecmp(key
, "watch_group")) {
1426 error
= str_to_u32(value
, &bucket
->watch_group
);
1427 if (!error
&& bucket
->watch_group
> OFPG_MAX
) {
1428 error
= xasprintf("invalid watch_group id %"PRIu32
,
1429 bucket
->watch_group
);
1431 } else if (!strcasecmp(key
, "bucket_id")) {
1432 error
= str_to_u32(value
, &bucket
->bucket_id
);
1433 if (!error
&& bucket
->bucket_id
> OFPG15_BUCKET_MAX
) {
1434 error
= xasprintf("invalid bucket_id id %"PRIu32
,
1437 *usable_protocols
&= OFPUTIL_P_OF15_UP
;
1438 } else if (!strcasecmp(key
, "action") || !strcasecmp(key
, "actions")) {
1439 ds_put_format(&actions
, "%s,", value
);
1441 ds_put_format(&actions
, "%s(%s),", key
, value
);
1445 ds_destroy(&actions
);
1450 if (!actions
.length
) {
1451 return xstrdup("bucket must specify actions");
1453 ds_chomp(&actions
, ',');
1455 ofpbuf_init(&ofpacts
, 0);
1456 error
= ofpacts_parse_actions(ds_cstr(&actions
), &ofpacts
,
1458 ds_destroy(&actions
);
1460 ofpbuf_uninit(&ofpacts
);
1463 bucket
->ofpacts
= ofpacts
.data
;
1464 bucket
->ofpacts_len
= ofpacts
.size
;
1469 static char * OVS_WARN_UNUSED_RESULT
1470 parse_select_group_field(char *s
, struct field_array
*fa
,
1471 enum ofputil_protocol
*usable_protocols
)
1473 char *name
, *value_str
;
1475 while (ofputil_parse_key_value(&s
, &name
, &value_str
)) {
1476 const struct mf_field
*mf
= mf_from_name(name
);
1480 union mf_value value
;
1482 if (bitmap_is_set(fa
->used
.bm
, mf
->id
)) {
1483 return xasprintf("%s: duplicate field", name
);
1487 error
= mf_parse_value(mf
, value_str
, &value
);
1492 /* The mask cannot be all-zeros */
1493 if (!mf_is_tun_metadata(mf
) &&
1494 is_all_zeros(&value
, mf
->n_bytes
)) {
1495 return xasprintf("%s: values are wildcards here "
1496 "and must not be all-zeros", s
);
1499 /* The values parsed are masks for fields used
1500 * by the selection method */
1501 if (!mf_is_mask_valid(mf
, &value
)) {
1502 return xasprintf("%s: invalid mask for field %s",
1503 value_str
, mf
->name
);
1506 memset(&value
, 0xff, mf
->n_bytes
);
1509 field_array_set(mf
->id
, &value
, fa
);
1511 if (is_all_ones(&value
, mf
->n_bytes
)) {
1512 *usable_protocols
&= mf
->usable_protocols_exact
;
1513 } else if (mf
->usable_protocols_bitwise
== mf
->usable_protocols_cidr
1514 || ip_is_cidr(value
.be32
)) {
1515 *usable_protocols
&= mf
->usable_protocols_cidr
;
1517 *usable_protocols
&= mf
->usable_protocols_bitwise
;
1520 return xasprintf("%s: unknown field %s", s
, name
);
1527 static char * OVS_WARN_UNUSED_RESULT
1528 parse_ofp_group_mod_str__(struct ofputil_group_mod
*gm
, int command
,
1530 enum ofputil_protocol
*usable_protocols
)
1533 F_GROUP_TYPE
= 1 << 0,
1535 F_COMMAND_BUCKET_ID
= 1 << 2,
1536 F_COMMAND_BUCKET_ID_ALL
= 1 << 3,
1538 bool had_type
= false;
1539 bool had_command_bucket_id
= false;
1540 struct ofputil_bucket
*bucket
;
1543 *usable_protocols
= OFPUTIL_P_OF11_UP
;
1545 if (command
== -2) {
1548 string
+= strspn(string
, " \t\r\n"); /* Skip white space. */
1549 len
= strcspn(string
, ", \t\r\n"); /* Get length of the first token. */
1551 if (!strncmp(string
, "add", len
)) {
1552 command
= OFPGC11_ADD
;
1553 } else if (!strncmp(string
, "delete", len
)) {
1554 command
= OFPGC11_DELETE
;
1555 } else if (!strncmp(string
, "modify", len
)) {
1556 command
= OFPGC11_MODIFY
;
1557 } else if (!strncmp(string
, "add_or_mod", len
)) {
1558 command
= OFPGC11_ADD_OR_MOD
;
1559 } else if (!strncmp(string
, "insert_bucket", len
)) {
1560 command
= OFPGC15_INSERT_BUCKET
;
1561 } else if (!strncmp(string
, "remove_bucket", len
)) {
1562 command
= OFPGC15_REMOVE_BUCKET
;
1565 command
= OFPGC11_ADD
;
1572 fields
= F_GROUP_TYPE
| F_BUCKETS
;
1575 case OFPGC11_DELETE
:
1579 case OFPGC11_MODIFY
:
1580 fields
= F_GROUP_TYPE
| F_BUCKETS
;
1583 case OFPGC11_ADD_OR_MOD
:
1584 fields
= F_GROUP_TYPE
| F_BUCKETS
;
1587 case OFPGC15_INSERT_BUCKET
:
1588 fields
= F_BUCKETS
| F_COMMAND_BUCKET_ID
;
1589 *usable_protocols
&= OFPUTIL_P_OF15_UP
;
1592 case OFPGC15_REMOVE_BUCKET
:
1593 fields
= F_COMMAND_BUCKET_ID
| F_COMMAND_BUCKET_ID_ALL
;
1594 *usable_protocols
&= OFPUTIL_P_OF15_UP
;
1601 memset(gm
, 0, sizeof *gm
);
1602 gm
->command
= command
;
1603 gm
->group_id
= OFPG_ANY
;
1604 gm
->command_bucket_id
= OFPG15_BUCKET_ALL
;
1605 ovs_list_init(&gm
->buckets
);
1606 if (command
== OFPGC11_DELETE
&& string
[0] == '\0') {
1607 gm
->group_id
= OFPG_ALL
;
1611 *usable_protocols
= OFPUTIL_P_OF11_UP
;
1613 /* Strip the buckets off the end of 'string', if there are any, saving a
1614 * pointer for later. We want to parse the buckets last because the bucket
1615 * type influences bucket defaults. */
1616 char *bkt_str
= strstr(string
, "bucket=");
1618 if (!(fields
& F_BUCKETS
)) {
1619 error
= xstrdup("bucket is not needed");
1625 /* Parse everything before the buckets. */
1628 while (ofputil_parse_key_value(&pos
, &name
, &value
)) {
1629 if (!strcmp(name
, "command_bucket_id")) {
1630 if (!(fields
& F_COMMAND_BUCKET_ID
)) {
1631 error
= xstrdup("command bucket id is not needed");
1634 if (!strcmp(value
, "all")) {
1635 gm
->command_bucket_id
= OFPG15_BUCKET_ALL
;
1636 } else if (!strcmp(value
, "first")) {
1637 gm
->command_bucket_id
= OFPG15_BUCKET_FIRST
;
1638 } else if (!strcmp(value
, "last")) {
1639 gm
->command_bucket_id
= OFPG15_BUCKET_LAST
;
1641 error
= str_to_u32(value
, &gm
->command_bucket_id
);
1645 if (gm
->command_bucket_id
> OFPG15_BUCKET_MAX
1646 && (gm
->command_bucket_id
!= OFPG15_BUCKET_FIRST
1647 && gm
->command_bucket_id
!= OFPG15_BUCKET_LAST
1648 && gm
->command_bucket_id
!= OFPG15_BUCKET_ALL
)) {
1649 error
= xasprintf("invalid command bucket id %"PRIu32
,
1650 gm
->command_bucket_id
);
1654 if (gm
->command_bucket_id
== OFPG15_BUCKET_ALL
1655 && !(fields
& F_COMMAND_BUCKET_ID_ALL
)) {
1656 error
= xstrdup("command_bucket_id=all is not permitted");
1659 had_command_bucket_id
= true;
1660 } else if (!strcmp(name
, "group_id")) {
1661 if(!strcmp(value
, "all")) {
1662 gm
->group_id
= OFPG_ALL
;
1664 error
= str_to_u32(value
, &gm
->group_id
);
1668 if (gm
->group_id
!= OFPG_ALL
&& gm
->group_id
> OFPG_MAX
) {
1669 error
= xasprintf("invalid group id %"PRIu32
,
1674 } else if (!strcmp(name
, "type")){
1675 if (!(fields
& F_GROUP_TYPE
)) {
1676 error
= xstrdup("type is not needed");
1679 if (!strcmp(value
, "all")) {
1680 gm
->type
= OFPGT11_ALL
;
1681 } else if (!strcmp(value
, "select")) {
1682 gm
->type
= OFPGT11_SELECT
;
1683 } else if (!strcmp(value
, "indirect")) {
1684 gm
->type
= OFPGT11_INDIRECT
;
1685 } else if (!strcmp(value
, "ff") ||
1686 !strcmp(value
, "fast_failover")) {
1687 gm
->type
= OFPGT11_FF
;
1689 error
= xasprintf("invalid group type %s", value
);
1693 } else if (!strcmp(name
, "selection_method")) {
1694 if (!(fields
& F_GROUP_TYPE
)) {
1695 error
= xstrdup("selection method is not needed");
1698 if (strlen(value
) >= NTR_MAX_SELECTION_METHOD_LEN
) {
1699 error
= xasprintf("selection method is longer than %u"
1701 NTR_MAX_SELECTION_METHOD_LEN
- 1);
1704 memset(gm
->props
.selection_method
, '\0',
1705 NTR_MAX_SELECTION_METHOD_LEN
);
1706 strcpy(gm
->props
.selection_method
, value
);
1707 *usable_protocols
&= OFPUTIL_P_OF15_UP
;
1708 } else if (!strcmp(name
, "selection_method_param")) {
1709 if (!(fields
& F_GROUP_TYPE
)) {
1710 error
= xstrdup("selection method param is not needed");
1713 error
= str_to_u64(value
, &gm
->props
.selection_method_param
);
1717 *usable_protocols
&= OFPUTIL_P_OF15_UP
;
1718 } else if (!strcmp(name
, "fields")) {
1719 if (!(fields
& F_GROUP_TYPE
)) {
1720 error
= xstrdup("fields are not needed");
1723 error
= parse_select_group_field(value
, &gm
->props
.fields
,
1728 *usable_protocols
&= OFPUTIL_P_OF15_UP
;
1730 error
= xasprintf("unknown keyword %s", name
);
1734 if (gm
->group_id
== OFPG_ANY
) {
1735 error
= xstrdup("must specify a group_id");
1738 if (fields
& F_GROUP_TYPE
&& !had_type
) {
1739 error
= xstrdup("must specify a type");
1743 /* Exclude fields for non "hash" selection method. */
1744 if (strcmp(gm
->props
.selection_method
, "hash") &&
1745 gm
->props
.fields
.values_size
) {
1746 error
= xstrdup("fields may only be specified with \"selection_method=hash\"");
1749 /* Exclude selection_method_param if no selection_method is given. */
1750 if (gm
->props
.selection_method
[0] == 0
1751 && gm
->props
.selection_method_param
!= 0) {
1752 error
= xstrdup("selection_method_param is only allowed with \"selection_method\"");
1755 if (fields
& F_COMMAND_BUCKET_ID
) {
1756 if (!(fields
& F_COMMAND_BUCKET_ID_ALL
|| had_command_bucket_id
)) {
1757 error
= xstrdup("must specify a command bucket id");
1760 } else if (had_command_bucket_id
) {
1761 error
= xstrdup("command bucket id is not needed");
1765 /* Now parse the buckets, if any. */
1769 bkt_str
= strchr(bkt_str
+ 1, '=');
1771 error
= xstrdup("must specify bucket content");
1776 next_bkt_str
= strstr(bkt_str
, "bucket=");
1778 *next_bkt_str
= '\0';
1781 bucket
= xzalloc(sizeof(struct ofputil_bucket
));
1782 error
= parse_bucket_str(bucket
, bkt_str
, gm
->type
, usable_protocols
);
1787 ovs_list_push_back(&gm
->buckets
, &bucket
->list_node
);
1789 if (gm
->type
!= OFPGT11_SELECT
&& bucket
->weight
) {
1790 error
= xstrdup("Only select groups can have bucket weights.");
1794 bkt_str
= next_bkt_str
;
1796 if (gm
->type
== OFPGT11_INDIRECT
&& !ovs_list_is_short(&gm
->buckets
)) {
1797 error
= xstrdup("Indirect groups can have at most one bucket.");
1803 ofputil_uninit_group_mod(gm
);
1807 /* If 'command' is given as -2, each line may start with a command name ("add",
1808 * "modify", "add_or_mod", "delete", "insert_bucket", or "remove_bucket"). A
1809 * missing command name is treated as "add".
1811 char * OVS_WARN_UNUSED_RESULT
1812 parse_ofp_group_mod_str(struct ofputil_group_mod
*gm
, int command
,
1814 enum ofputil_protocol
*usable_protocols
)
1816 char *string
= xstrdup(str_
);
1817 char *error
= parse_ofp_group_mod_str__(gm
, command
, string
,
1823 /* If 'command' is given as -2, each line may start with a command name ("add",
1824 * "modify", "add_or_mod", "delete", "insert_bucket", or "remove_bucket"). A
1825 * missing command name is treated as "add".
1827 char * OVS_WARN_UNUSED_RESULT
1828 parse_ofp_group_mod_file(const char *file_name
, int command
,
1829 struct ofputil_group_mod
**gms
, size_t *n_gms
,
1830 enum ofputil_protocol
*usable_protocols
)
1832 size_t allocated_gms
;
1840 stream
= !strcmp(file_name
, "-") ? stdin
: fopen(file_name
, "r");
1841 if (stream
== NULL
) {
1842 return xasprintf("%s: open failed (%s)",
1843 file_name
, ovs_strerror(errno
));
1846 allocated_gms
= *n_gms
;
1849 *usable_protocols
= OFPUTIL_P_OF11_UP
;
1850 while (!ds_get_preprocessed_line(&s
, stream
, &line_number
)) {
1851 enum ofputil_protocol usable
;
1854 if (*n_gms
>= allocated_gms
) {
1855 struct ofputil_group_mod
*new_gms
;
1858 new_gms
= x2nrealloc(*gms
, &allocated_gms
, sizeof **gms
);
1859 for (i
= 0; i
< *n_gms
; i
++) {
1860 ovs_list_moved(&new_gms
[i
].buckets
, &(*gms
)[i
].buckets
);
1864 error
= parse_ofp_group_mod_str(&(*gms
)[*n_gms
], command
, ds_cstr(&s
),
1869 for (i
= 0; i
< *n_gms
; i
++) {
1870 ofputil_uninit_group_mod(&(*gms
)[i
]);
1877 if (stream
!= stdin
) {
1881 return xasprintf("%s:%d: %s", file_name
, line_number
, error
);
1883 *usable_protocols
&= usable
;
1888 if (stream
!= stdin
) {
1894 /* Opens file 'file_name' and reads each line as a flow_mod or a group_mod,
1895 * depending on the first keyword on each line. Stores each flow and group
1896 * mods in '*bms', an array allocated on the caller's behalf, and the number of
1897 * messages in '*n_bms'.
1899 * Returns NULL if successful, otherwise a malloc()'d string describing the
1900 * error. The caller is responsible for freeing the returned string. */
1901 char * OVS_WARN_UNUSED_RESULT
1902 parse_ofp_bundle_file(const char *file_name
,
1903 struct ofputil_bundle_msg
**bms
, size_t *n_bms
,
1904 enum ofputil_protocol
*usable_protocols
)
1906 size_t allocated_bms
;
1912 *usable_protocols
= OFPUTIL_P_ANY
;
1917 stream
= !strcmp(file_name
, "-") ? stdin
: fopen(file_name
, "r");
1918 if (stream
== NULL
) {
1919 return xasprintf("%s: open failed (%s)",
1920 file_name
, ovs_strerror(errno
));
1923 allocated_bms
= *n_bms
;
1926 while (!ds_get_preprocessed_line(&ds
, stream
, &line_number
)) {
1927 enum ofputil_protocol usable
;
1928 char *s
= ds_cstr(&ds
);
1931 if (*n_bms
>= allocated_bms
) {
1932 struct ofputil_bundle_msg
*new_bms
;
1934 new_bms
= x2nrealloc(*bms
, &allocated_bms
, sizeof **bms
);
1935 for (size_t i
= 0; i
< *n_bms
; i
++) {
1936 if (new_bms
[i
].type
== OFPTYPE_GROUP_MOD
) {
1937 ovs_list_moved(&new_bms
[i
].gm
.buckets
,
1938 &(*bms
)[i
].gm
.buckets
);
1944 s
+= strspn(s
, " \t\r\n"); /* Skip white space. */
1945 len
= strcspn(s
, ", \t\r\n"); /* Get length of the first token. */
1947 if (!strncmp(s
, "flow", len
)) {
1949 error
= parse_ofp_flow_mod_str(&(*bms
)[*n_bms
].fm
, s
, -2, &usable
);
1953 (*bms
)[*n_bms
].type
= OFPTYPE_FLOW_MOD
;
1954 } else if (!strncmp(s
, "group", len
)) {
1956 error
= parse_ofp_group_mod_str(&(*bms
)[*n_bms
].gm
, -2, s
,
1961 (*bms
)[*n_bms
].type
= OFPTYPE_GROUP_MOD
;
1962 } else if (!strncmp(s
, "packet-out", len
)) {
1964 error
= parse_ofp_packet_out_str(&(*bms
)[*n_bms
].po
, s
, &usable
);
1968 (*bms
)[*n_bms
].type
= OFPTYPE_PACKET_OUT
;
1970 error
= xasprintf("Unsupported bundle message type: %.*s",
1975 *usable_protocols
&= usable
; /* Each line can narrow the set. */
1980 if (stream
!= stdin
) {
1985 char *err_msg
= xasprintf("%s:%d: %s", file_name
, line_number
, error
);
1988 ofputil_free_bundle_msgs(*bms
, *n_bms
);
1996 char * OVS_WARN_UNUSED_RESULT
1997 parse_ofp_tlv_table_mod_str(struct ofputil_tlv_table_mod
*ttm
,
1998 uint16_t command
, const char *s
,
1999 enum ofputil_protocol
*usable_protocols
)
2001 *usable_protocols
= OFPUTIL_P_NXM_OXM_ANY
;
2003 ttm
->command
= command
;
2004 ovs_list_init(&ttm
->mappings
);
2007 struct ofputil_tlv_map
*map
= xmalloc(sizeof *map
);
2014 ovs_list_push_back(&ttm
->mappings
, &map
->list_node
);
2016 if (!ovs_scan(s
, "{class=%"SCNi16
",type=%"SCNi8
",len=%"SCNi8
"}->tun_metadata%"SCNi16
"%n",
2017 &map
->option_class
, &map
->option_type
, &map
->option_len
,
2019 ofputil_uninit_tlv_table(&ttm
->mappings
);
2020 return xstrdup("invalid tlv mapping");