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_set_in_port(&po
->flow_metadata
, OFPP_CONTROLLER
);
627 act_str
= extract_actions(string
);
629 while (ofputil_parse_key_value(&string
, &name
, &value
)) {
631 error
= xasprintf("field %s missing value", name
);
635 if (!strcmp(name
, "in_port")) {
637 if (!ofputil_port_from_string(value
, &in_port
)) {
638 error
= xasprintf("%s is not a valid OpenFlow port", value
);
641 if (ofp_to_u16(in_port
) > ofp_to_u16(OFPP_MAX
)
642 && in_port
!= OFPP_LOCAL
643 && in_port
!= OFPP_NONE
644 && in_port
!= OFPP_CONTROLLER
) {
646 "%s is not a valid OpenFlow port for PACKET_OUT",
650 match_set_in_port(&po
->flow_metadata
, in_port
);
651 } else if (!strcmp(name
, "packet")) {
652 const char *error_msg
= eth_from_hex(value
, &packet
);
654 error
= xasprintf("%s: %s", name
, error_msg
);
658 error
= xasprintf("unknown keyword %s", name
);
663 if (!packet
|| !dp_packet_size(packet
)) {
664 error
= xstrdup("must specify packet");
669 error
= ofpacts_parse_actions(act_str
, &ofpacts
,
670 &action_usable_protocols
);
671 *usable_protocols
&= action_usable_protocols
;
676 po
->ofpacts_len
= ofpacts
.size
;
677 po
->ofpacts
= ofpbuf_steal_data(&ofpacts
);
679 po
->packet_len
= dp_packet_size(packet
);
680 po
->packet
= dp_packet_steal_data(packet
);
682 ofpbuf_uninit(&ofpacts
);
683 dp_packet_delete(packet
);
687 /* Convert 'str_' (as described in the Packet-Out Syntax section of the
688 * ovs-ofctl man page) into 'po' for sending a OFPT_PACKET_OUT message to a
689 * switch. Returns the set of usable protocols in '*usable_protocols'.
691 * Returns NULL if successful, otherwise a malloc()'d string describing the
692 * error. The caller is responsible for freeing the returned string. */
693 char * OVS_WARN_UNUSED_RESULT
694 parse_ofp_packet_out_str(struct ofputil_packet_out
*po
, const char *str_
,
695 enum ofputil_protocol
*usable_protocols
)
697 char *string
= xstrdup(str_
);
700 error
= parse_ofp_packet_out_str__(po
, string
, usable_protocols
);
710 static char * OVS_WARN_UNUSED_RESULT
711 parse_ofp_meter_mod_str__(struct ofputil_meter_mod
*mm
, char *string
,
712 struct ofpbuf
*bands
, int command
,
713 enum ofputil_protocol
*usable_protocols
)
720 char *save_ptr
= NULL
;
721 char *band_str
= NULL
;
724 /* Meters require at least OF 1.3. */
725 *usable_protocols
= OFPUTIL_P_OF13_UP
;
733 fields
= F_METER
| F_FLAGS
| F_BANDS
;
741 fields
= F_METER
| F_FLAGS
| F_BANDS
;
748 mm
->command
= command
;
749 mm
->meter
.meter_id
= 0;
751 if (fields
& F_BANDS
) {
752 band_str
= strstr(string
, "band");
754 return xstrdup("must specify bands");
758 band_str
= strchr(band_str
+ 1, '=');
760 return xstrdup("must specify bands");
765 for (name
= strtok_r(string
, "=, \t\r\n", &save_ptr
); name
;
766 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
768 if (fields
& F_FLAGS
&& !strcmp(name
, "kbps")) {
769 mm
->meter
.flags
|= OFPMF13_KBPS
;
770 } else if (fields
& F_FLAGS
&& !strcmp(name
, "pktps")) {
771 mm
->meter
.flags
|= OFPMF13_PKTPS
;
772 } else if (fields
& F_FLAGS
&& !strcmp(name
, "burst")) {
773 mm
->meter
.flags
|= OFPMF13_BURST
;
774 } else if (fields
& F_FLAGS
&& !strcmp(name
, "stats")) {
775 mm
->meter
.flags
|= OFPMF13_STATS
;
779 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
781 return xasprintf("field %s missing value", name
);
784 if (!strcmp(name
, "meter")) {
785 if (!strcmp(value
, "all")) {
786 mm
->meter
.meter_id
= OFPM13_ALL
;
787 } else if (!strcmp(value
, "controller")) {
788 mm
->meter
.meter_id
= OFPM13_CONTROLLER
;
789 } else if (!strcmp(value
, "slowpath")) {
790 mm
->meter
.meter_id
= OFPM13_SLOWPATH
;
792 char *error
= str_to_u32(value
, &mm
->meter
.meter_id
);
796 if (mm
->meter
.meter_id
> OFPM13_MAX
797 || !mm
->meter
.meter_id
) {
798 return xasprintf("invalid value for %s", name
);
802 return xasprintf("unknown keyword %s", name
);
806 if (fields
& F_METER
&& !mm
->meter
.meter_id
) {
807 return xstrdup("must specify 'meter'");
809 if (fields
& F_FLAGS
&& !mm
->meter
.flags
) {
810 return xstrdup("meter must specify either 'kbps' or 'pktps'");
813 if (fields
& F_BANDS
) {
814 uint16_t n_bands
= 0;
815 struct ofputil_meter_band
*band
= NULL
;
818 for (name
= strtok_r(band_str
, "=, \t\r\n", &save_ptr
); name
;
819 name
= strtok_r(NULL
, "=, \t\r\n", &save_ptr
)) {
823 value
= strtok_r(NULL
, ", \t\r\n", &save_ptr
);
825 return xasprintf("field %s missing value", name
);
828 if (!strcmp(name
, "type")) {
829 /* Start a new band */
830 band
= ofpbuf_put_zeros(bands
, sizeof *band
);
833 if (!strcmp(value
, "drop")) {
834 band
->type
= OFPMBT13_DROP
;
835 } else if (!strcmp(value
, "dscp_remark")) {
836 band
->type
= OFPMBT13_DSCP_REMARK
;
838 return xasprintf("field %s unknown value %s", name
, value
);
840 } else if (!band
|| !band
->type
) {
841 return xstrdup("band must start with the 'type' keyword");
842 } else if (!strcmp(name
, "rate")) {
843 char *error
= str_to_u32(value
, &band
->rate
);
847 } else if (!strcmp(name
, "burst_size")) {
848 char *error
= str_to_u32(value
, &band
->burst_size
);
852 } else if (!strcmp(name
, "prec_level")) {
853 char *error
= str_to_u8(value
, name
, &band
->prec_level
);
858 return xasprintf("unknown keyword %s", name
);
863 return xstrdup("meter must have bands");
866 mm
->meter
.n_bands
= n_bands
;
867 mm
->meter
.bands
= ofpbuf_steal_data(bands
);
869 for (i
= 0; i
< n_bands
; ++i
) {
870 band
= &mm
->meter
.bands
[i
];
873 return xstrdup("band must have 'type'");
875 if (band
->type
== OFPMBT13_DSCP_REMARK
) {
876 if (!band
->prec_level
) {
877 return xstrdup("'dscp_remark' band must have"
881 if (band
->prec_level
) {
882 return xstrdup("Only 'dscp_remark' band may have"
887 return xstrdup("band must have 'rate'");
889 if (mm
->meter
.flags
& OFPMF13_BURST
) {
890 if (!band
->burst_size
) {
891 return xstrdup("band must have 'burst_size' "
892 "when 'burst' flag is set");
895 if (band
->burst_size
) {
896 return xstrdup("band may have 'burst_size' only "
897 "when 'burst' flag is set");
902 mm
->meter
.n_bands
= 0;
903 mm
->meter
.bands
= NULL
;
909 /* Convert 'str_' (as described in the Flow Syntax section of the ovs-ofctl man
910 * page) into 'mm' for sending the specified meter_mod 'command' to a switch.
912 * Returns NULL if successful, otherwise a malloc()'d string describing the
913 * error. The caller is responsible for freeing the returned string. */
914 char * OVS_WARN_UNUSED_RESULT
915 parse_ofp_meter_mod_str(struct ofputil_meter_mod
*mm
, const char *str_
,
916 int command
, enum ofputil_protocol
*usable_protocols
)
922 ofpbuf_init(&bands
, 64);
923 string
= xstrdup(str_
);
925 error
= parse_ofp_meter_mod_str__(mm
, string
, &bands
, command
,
929 ofpbuf_uninit(&bands
);
934 static char * OVS_WARN_UNUSED_RESULT
935 parse_flow_monitor_request__(struct ofputil_flow_monitor_request
*fmr
,
936 const char *str_
, char *string
,
937 enum ofputil_protocol
*usable_protocols
)
939 static atomic_count id
= ATOMIC_COUNT_INIT(0);
942 fmr
->id
= atomic_count_inc(&id
);
944 fmr
->flags
= (NXFMF_INITIAL
| NXFMF_ADD
| NXFMF_DELETE
| NXFMF_MODIFY
945 | NXFMF_OWN
| NXFMF_ACTIONS
);
946 fmr
->out_port
= OFPP_NONE
;
947 fmr
->table_id
= 0xff;
948 match_init_catchall(&fmr
->match
);
950 while (ofputil_parse_key_value(&string
, &name
, &value
)) {
951 const struct protocol
*p
;
954 if (!strcmp(name
, "!initial")) {
955 fmr
->flags
&= ~NXFMF_INITIAL
;
956 } else if (!strcmp(name
, "!add")) {
957 fmr
->flags
&= ~NXFMF_ADD
;
958 } else if (!strcmp(name
, "!delete")) {
959 fmr
->flags
&= ~NXFMF_DELETE
;
960 } else if (!strcmp(name
, "!modify")) {
961 fmr
->flags
&= ~NXFMF_MODIFY
;
962 } else if (!strcmp(name
, "!actions")) {
963 fmr
->flags
&= ~NXFMF_ACTIONS
;
964 } else if (!strcmp(name
, "!own")) {
965 fmr
->flags
&= ~NXFMF_OWN
;
966 } else if (parse_protocol(name
, &p
)) {
967 match_set_dl_type(&fmr
->match
, htons(p
->dl_type
));
969 match_set_nw_proto(&fmr
->match
, p
->nw_proto
);
971 } else if (mf_from_name(name
)) {
972 error
= parse_field(mf_from_name(name
), value
, &fmr
->match
,
976 return xasprintf("%s: field %s missing value", str_
, name
);
979 if (!strcmp(name
, "table")) {
980 error
= str_to_u8(value
, "table", &fmr
->table_id
);
981 } else if (!strcmp(name
, "out_port")) {
982 fmr
->out_port
= u16_to_ofp(atoi(value
));
984 return xasprintf("%s: unknown keyword %s", str_
, name
);
995 /* Convert 'str_' (as described in the documentation for the "monitor" command
996 * in the ovs-ofctl man page) into 'fmr'.
998 * Returns NULL if successful, otherwise a malloc()'d string describing the
999 * error. The caller is responsible for freeing the returned string. */
1000 char * OVS_WARN_UNUSED_RESULT
1001 parse_flow_monitor_request(struct ofputil_flow_monitor_request
*fmr
,
1003 enum ofputil_protocol
*usable_protocols
)
1005 char *string
= xstrdup(str_
);
1006 char *error
= parse_flow_monitor_request__(fmr
, str_
, string
,
1012 /* Parses 'string' as an OFPT_FLOW_MOD or NXT_FLOW_MOD with command 'command'
1013 * (one of OFPFC_*) into 'fm'.
1015 * If 'command' is given as -2, 'string' may begin with a command name ("add",
1016 * "modify", "delete", "modify_strict", or "delete_strict"). A missing command
1017 * name is treated as "add".
1019 * Returns NULL if successful, otherwise a malloc()'d string describing the
1020 * error. The caller is responsible for freeing the returned string. */
1021 char * OVS_WARN_UNUSED_RESULT
1022 parse_ofp_flow_mod_str(struct ofputil_flow_mod
*fm
, const char *string
,
1024 enum ofputil_protocol
*usable_protocols
)
1026 char *error
= parse_ofp_str(fm
, command
, string
, usable_protocols
);
1029 /* Normalize a copy of the match. This ensures that non-normalized
1030 * flows get logged but doesn't affect what gets sent to the switch, so
1031 * that the switch can do whatever it likes with the flow. */
1032 struct match match_copy
= fm
->match
;
1033 ofputil_normalize_match(&match_copy
);
1039 /* Convert 'setting' (as described for the "mod-table" command
1040 * in ovs-ofctl man page) into 'tm->table_vacancy->vacancy_up' and
1041 * 'tm->table_vacancy->vacancy_down' threshold values.
1042 * For the two threshold values, value of vacancy_up is always greater
1043 * than value of vacancy_down.
1045 * Returns NULL if successful, otherwise a malloc()'d string describing the
1046 * error. The caller is responsible for freeing the returned string. */
1047 char * OVS_WARN_UNUSED_RESULT
1048 parse_ofp_table_vacancy(struct ofputil_table_mod
*tm
, const char *setting
)
1050 char *save_ptr
= NULL
;
1051 char *vac_up
, *vac_down
;
1052 char *value
= xstrdup(setting
);
1054 int vacancy_up
, vacancy_down
;
1056 strtok_r(value
, ":", &save_ptr
);
1057 vac_down
= strtok_r(NULL
, ",", &save_ptr
);
1059 ret_msg
= xasprintf("Vacancy down value missing");
1062 if (!str_to_int(vac_down
, 0, &vacancy_down
) ||
1063 vacancy_down
< 0 || vacancy_down
> 100) {
1064 ret_msg
= xasprintf("Invalid vacancy down value \"%s\"", vac_down
);
1067 vac_up
= strtok_r(NULL
, ",", &save_ptr
);
1069 ret_msg
= xasprintf("Vacancy up value missing");
1072 if (!str_to_int(vac_up
, 0, &vacancy_up
) ||
1073 vacancy_up
< 0 || vacancy_up
> 100) {
1074 ret_msg
= xasprintf("Invalid vacancy up value \"%s\"", vac_up
);
1077 if (vacancy_down
> vacancy_up
) {
1078 ret_msg
= xasprintf("Invalid vacancy range, vacancy up should be "
1079 "greater than vacancy down (%s)",
1080 ofperr_to_string(OFPERR_OFPBPC_BAD_VALUE
));
1085 tm
->table_vacancy
.vacancy_down
= vacancy_down
;
1086 tm
->table_vacancy
.vacancy_up
= vacancy_up
;
1094 /* Convert 'table_id' and 'setting' (as described for the "mod-table" command
1095 * in the ovs-ofctl man page) into 'tm' for sending a table_mod command to a
1098 * Stores a bitmap of the OpenFlow versions that are usable for 'tm' into
1099 * '*usable_versions'.
1101 * Returns NULL if successful, otherwise a malloc()'d string describing the
1102 * error. The caller is responsible for freeing the returned string. */
1103 char * OVS_WARN_UNUSED_RESULT
1104 parse_ofp_table_mod(struct ofputil_table_mod
*tm
, const char *table_id
,
1105 const char *setting
, uint32_t *usable_versions
)
1107 *usable_versions
= 0;
1108 if (!strcasecmp(table_id
, "all")) {
1109 tm
->table_id
= OFPTT_ALL
;
1111 char *error
= str_to_u8(table_id
, "table_id", &tm
->table_id
);
1117 tm
->miss
= OFPUTIL_TABLE_MISS_DEFAULT
;
1118 tm
->eviction
= OFPUTIL_TABLE_EVICTION_DEFAULT
;
1119 tm
->eviction_flags
= UINT32_MAX
;
1120 tm
->vacancy
= OFPUTIL_TABLE_VACANCY_DEFAULT
;
1121 tm
->table_vacancy
.vacancy_down
= 0;
1122 tm
->table_vacancy
.vacancy_up
= 0;
1123 tm
->table_vacancy
.vacancy
= 0;
1124 /* Only OpenFlow 1.1 and 1.2 can configure table-miss via table_mod.
1125 * Only OpenFlow 1.4+ can configure eviction and vacancy events
1128 if (!strcmp(setting
, "controller")) {
1129 tm
->miss
= OFPUTIL_TABLE_MISS_CONTROLLER
;
1130 *usable_versions
= (1u << OFP11_VERSION
) | (1u << OFP12_VERSION
);
1131 } else if (!strcmp(setting
, "continue")) {
1132 tm
->miss
= OFPUTIL_TABLE_MISS_CONTINUE
;
1133 *usable_versions
= (1u << OFP11_VERSION
) | (1u << OFP12_VERSION
);
1134 } else if (!strcmp(setting
, "drop")) {
1135 tm
->miss
= OFPUTIL_TABLE_MISS_DROP
;
1136 *usable_versions
= (1u << OFP11_VERSION
) | (1u << OFP12_VERSION
);
1137 } else if (!strcmp(setting
, "evict")) {
1138 tm
->eviction
= OFPUTIL_TABLE_EVICTION_ON
;
1139 *usable_versions
= (1 << OFP14_VERSION
) | (1u << OFP15_VERSION
);
1140 } else if (!strcmp(setting
, "noevict")) {
1141 tm
->eviction
= OFPUTIL_TABLE_EVICTION_OFF
;
1142 *usable_versions
= (1 << OFP14_VERSION
) | (1u << OFP15_VERSION
);
1143 } else if (!strncmp(setting
, "vacancy", strcspn(setting
, ":"))) {
1144 tm
->vacancy
= OFPUTIL_TABLE_VACANCY_ON
;
1145 *usable_versions
= (1 << OFP14_VERSION
) | (1u << OFP15_VERSION
);
1146 char *error
= parse_ofp_table_vacancy(tm
, setting
);
1150 } else if (!strcmp(setting
, "novacancy")) {
1151 tm
->vacancy
= OFPUTIL_TABLE_VACANCY_OFF
;
1152 *usable_versions
= (1 << OFP14_VERSION
) | (1u << OFP15_VERSION
);
1154 return xasprintf("invalid table_mod setting %s", setting
);
1157 if (tm
->table_id
== 0xfe
1158 && tm
->miss
== OFPUTIL_TABLE_MISS_CONTINUE
) {
1159 return xstrdup("last table's flow miss handling can not be continue");
1166 /* Opens file 'file_name' and reads each line as a flow_mod of the specified
1167 * type (one of OFPFC_*). Stores each flow_mod in '*fm', an array allocated
1168 * on the caller's behalf, and the number of flow_mods in '*n_fms'.
1170 * If 'command' is given as -2, each line may start with a command name
1171 * ("add", "modify", "delete", "modify_strict", or "delete_strict"). A missing
1172 * command name is treated as "add".
1174 * Returns NULL if successful, otherwise a malloc()'d string describing the
1175 * error. The caller is responsible for freeing the returned string. */
1176 char * OVS_WARN_UNUSED_RESULT
1177 parse_ofp_flow_mod_file(const char *file_name
, int command
,
1178 struct ofputil_flow_mod
**fms
, size_t *n_fms
,
1179 enum ofputil_protocol
*usable_protocols
)
1181 size_t allocated_fms
;
1186 *usable_protocols
= OFPUTIL_P_ANY
;
1191 stream
= !strcmp(file_name
, "-") ? stdin
: fopen(file_name
, "r");
1192 if (stream
== NULL
) {
1193 return xasprintf("%s: open failed (%s)",
1194 file_name
, ovs_strerror(errno
));
1197 allocated_fms
= *n_fms
;
1200 while (!ds_get_preprocessed_line(&s
, stream
, &line_number
)) {
1202 enum ofputil_protocol usable
;
1204 if (*n_fms
>= allocated_fms
) {
1205 *fms
= x2nrealloc(*fms
, &allocated_fms
, sizeof **fms
);
1207 error
= parse_ofp_flow_mod_str(&(*fms
)[*n_fms
], ds_cstr(&s
), command
,
1213 for (i
= 0; i
< *n_fms
; i
++) {
1214 free(CONST_CAST(struct ofpact
*, (*fms
)[i
].ofpacts
));
1221 if (stream
!= stdin
) {
1225 err_msg
= xasprintf("%s:%d: %s", file_name
, line_number
, error
);
1229 *usable_protocols
&= usable
; /* Each line can narrow the set. */
1234 if (stream
!= stdin
) {
1240 char * OVS_WARN_UNUSED_RESULT
1241 parse_ofp_flow_stats_request_str(struct ofputil_flow_stats_request
*fsr
,
1242 bool aggregate
, const char *string
,
1243 enum ofputil_protocol
*usable_protocols
)
1245 struct ofputil_flow_mod fm
;
1248 error
= parse_ofp_str(&fm
, -1, string
, usable_protocols
);
1253 /* Special table ID support not required for stats requests. */
1254 if (*usable_protocols
& OFPUTIL_P_OF10_STD_TID
) {
1255 *usable_protocols
|= OFPUTIL_P_OF10_STD
;
1257 if (*usable_protocols
& OFPUTIL_P_OF10_NXM_TID
) {
1258 *usable_protocols
|= OFPUTIL_P_OF10_NXM
;
1261 fsr
->aggregate
= aggregate
;
1262 fsr
->cookie
= fm
.cookie
;
1263 fsr
->cookie_mask
= fm
.cookie_mask
;
1264 fsr
->match
= fm
.match
;
1265 fsr
->out_port
= fm
.out_port
;
1266 fsr
->out_group
= fm
.out_group
;
1267 fsr
->table_id
= fm
.table_id
;
1271 /* Parses a specification of a flow from 's' into 'flow'. 's' must take the
1272 * form FIELD=VALUE[,FIELD=VALUE]... where each FIELD is the name of a
1273 * mf_field. Fields must be specified in a natural order for satisfying
1274 * prerequisites. If 'wc' is specified, masks the field in 'wc' for each of the
1275 * field specified in flow. If the map, 'names_portno' is specfied, converts
1276 * the in_port name into port no while setting the 'flow'.
1278 * Returns NULL on success, otherwise a malloc()'d string that explains the
1281 parse_ofp_exact_flow(struct flow
*flow
, struct flow_wildcards
*wc
,
1282 const struct tun_table
*tun_table
, const char *s
,
1283 const struct simap
*portno_names
)
1285 char *pos
, *key
, *value_s
;
1289 memset(flow
, 0, sizeof *flow
);
1291 memset(wc
, 0, sizeof *wc
);
1293 flow
->tunnel
.metadata
.tab
= tun_table
;
1295 pos
= copy
= xstrdup(s
);
1296 while (ofputil_parse_key_value(&pos
, &key
, &value_s
)) {
1297 const struct protocol
*p
;
1298 if (parse_protocol(key
, &p
)) {
1299 if (flow
->dl_type
) {
1300 error
= xasprintf("%s: Ethernet type set multiple times", s
);
1303 flow
->dl_type
= htons(p
->dl_type
);
1305 wc
->masks
.dl_type
= OVS_BE16_MAX
;
1309 if (flow
->nw_proto
) {
1310 error
= xasprintf("%s: network protocol set "
1311 "multiple times", s
);
1314 flow
->nw_proto
= p
->nw_proto
;
1316 wc
->masks
.nw_proto
= UINT8_MAX
;
1320 const struct mf_field
*mf
;
1321 union mf_value value
;
1324 mf
= mf_from_name(key
);
1326 error
= xasprintf("%s: unknown field %s", s
, key
);
1330 if (!mf_are_prereqs_ok(mf
, flow
, NULL
)) {
1331 error
= xasprintf("%s: prerequisites not met for setting %s",
1336 if (mf_is_set(mf
, flow
)) {
1337 error
= xasprintf("%s: field %s set multiple times", s
, key
);
1341 if (!strcmp(key
, "in_port")
1343 && simap_contains(portno_names
, value_s
)) {
1344 flow
->in_port
.ofp_port
= u16_to_ofp(
1345 simap_get(portno_names
, value_s
));
1347 wc
->masks
.in_port
.ofp_port
1348 = u16_to_ofp(ntohs(OVS_BE16_MAX
));
1351 field_error
= mf_parse_value(mf
, value_s
, &value
);
1353 error
= xasprintf("%s: bad value for %s (%s)",
1354 s
, key
, field_error
);
1359 mf_set_flow_value(mf
, &value
, flow
);
1361 mf_mask_field(mf
, wc
);
1367 if (!flow
->in_port
.ofp_port
) {
1368 flow
->in_port
.ofp_port
= OFPP_NONE
;
1375 memset(flow
, 0, sizeof *flow
);
1377 memset(wc
, 0, sizeof *wc
);
1383 static char * OVS_WARN_UNUSED_RESULT
1384 parse_bucket_str(struct ofputil_bucket
*bucket
, char *str_
, uint8_t group_type
,
1385 enum ofputil_protocol
*usable_protocols
)
1387 char *pos
, *key
, *value
;
1388 struct ofpbuf ofpacts
;
1392 bucket
->weight
= group_type
== OFPGT11_SELECT
? 1 : 0;
1393 bucket
->bucket_id
= OFPG15_BUCKET_ALL
;
1394 bucket
->watch_port
= OFPP_ANY
;
1395 bucket
->watch_group
= OFPG_ANY
;
1401 while (ofputil_parse_key_value(&pos
, &key
, &value
)) {
1402 if (!strcasecmp(key
, "weight")) {
1403 error
= str_to_u16(value
, "weight", &bucket
->weight
);
1404 } else if (!strcasecmp(key
, "watch_port")) {
1405 if (!ofputil_port_from_string(value
, &bucket
->watch_port
)
1406 || (ofp_to_u16(bucket
->watch_port
) >= ofp_to_u16(OFPP_MAX
)
1407 && bucket
->watch_port
!= OFPP_ANY
)) {
1408 error
= xasprintf("%s: invalid watch_port", value
);
1410 } else if (!strcasecmp(key
, "watch_group")) {
1411 error
= str_to_u32(value
, &bucket
->watch_group
);
1412 if (!error
&& bucket
->watch_group
> OFPG_MAX
) {
1413 error
= xasprintf("invalid watch_group id %"PRIu32
,
1414 bucket
->watch_group
);
1416 } else if (!strcasecmp(key
, "bucket_id")) {
1417 error
= str_to_u32(value
, &bucket
->bucket_id
);
1418 if (!error
&& bucket
->bucket_id
> OFPG15_BUCKET_MAX
) {
1419 error
= xasprintf("invalid bucket_id id %"PRIu32
,
1422 *usable_protocols
&= OFPUTIL_P_OF15_UP
;
1423 } else if (!strcasecmp(key
, "action") || !strcasecmp(key
, "actions")) {
1424 ds_put_format(&actions
, "%s,", value
);
1426 ds_put_format(&actions
, "%s(%s),", key
, value
);
1430 ds_destroy(&actions
);
1435 if (!actions
.length
) {
1436 return xstrdup("bucket must specify actions");
1438 ds_chomp(&actions
, ',');
1440 ofpbuf_init(&ofpacts
, 0);
1441 error
= ofpacts_parse_actions(ds_cstr(&actions
), &ofpacts
,
1443 ds_destroy(&actions
);
1445 ofpbuf_uninit(&ofpacts
);
1448 bucket
->ofpacts
= ofpacts
.data
;
1449 bucket
->ofpacts_len
= ofpacts
.size
;
1454 static char * OVS_WARN_UNUSED_RESULT
1455 parse_select_group_field(char *s
, struct field_array
*fa
,
1456 enum ofputil_protocol
*usable_protocols
)
1458 char *name
, *value_str
;
1460 while (ofputil_parse_key_value(&s
, &name
, &value_str
)) {
1461 const struct mf_field
*mf
= mf_from_name(name
);
1465 union mf_value value
;
1467 if (bitmap_is_set(fa
->used
.bm
, mf
->id
)) {
1468 return xasprintf("%s: duplicate field", name
);
1472 error
= mf_parse_value(mf
, value_str
, &value
);
1477 /* The mask cannot be all-zeros */
1478 if (!mf_is_tun_metadata(mf
) &&
1479 is_all_zeros(&value
, mf
->n_bytes
)) {
1480 return xasprintf("%s: values are wildcards here "
1481 "and must not be all-zeros", s
);
1484 /* The values parsed are masks for fields used
1485 * by the selection method */
1486 if (!mf_is_mask_valid(mf
, &value
)) {
1487 return xasprintf("%s: invalid mask for field %s",
1488 value_str
, mf
->name
);
1491 memset(&value
, 0xff, mf
->n_bytes
);
1494 field_array_set(mf
->id
, &value
, fa
);
1496 if (is_all_ones(&value
, mf
->n_bytes
)) {
1497 *usable_protocols
&= mf
->usable_protocols_exact
;
1498 } else if (mf
->usable_protocols_bitwise
== mf
->usable_protocols_cidr
1499 || ip_is_cidr(value
.be32
)) {
1500 *usable_protocols
&= mf
->usable_protocols_cidr
;
1502 *usable_protocols
&= mf
->usable_protocols_bitwise
;
1505 return xasprintf("%s: unknown field %s", s
, name
);
1512 static char * OVS_WARN_UNUSED_RESULT
1513 parse_ofp_group_mod_str__(struct ofputil_group_mod
*gm
, int command
,
1515 enum ofputil_protocol
*usable_protocols
)
1518 F_GROUP_TYPE
= 1 << 0,
1520 F_COMMAND_BUCKET_ID
= 1 << 2,
1521 F_COMMAND_BUCKET_ID_ALL
= 1 << 3,
1523 bool had_type
= false;
1524 bool had_command_bucket_id
= false;
1525 struct ofputil_bucket
*bucket
;
1528 *usable_protocols
= OFPUTIL_P_OF11_UP
;
1530 if (command
== -2) {
1533 string
+= strspn(string
, " \t\r\n"); /* Skip white space. */
1534 len
= strcspn(string
, ", \t\r\n"); /* Get length of the first token. */
1536 if (!strncmp(string
, "add", len
)) {
1537 command
= OFPGC11_ADD
;
1538 } else if (!strncmp(string
, "delete", len
)) {
1539 command
= OFPGC11_DELETE
;
1540 } else if (!strncmp(string
, "modify", len
)) {
1541 command
= OFPGC11_MODIFY
;
1542 } else if (!strncmp(string
, "add_or_mod", len
)) {
1543 command
= OFPGC11_ADD_OR_MOD
;
1544 } else if (!strncmp(string
, "insert_bucket", len
)) {
1545 command
= OFPGC15_INSERT_BUCKET
;
1546 } else if (!strncmp(string
, "remove_bucket", len
)) {
1547 command
= OFPGC15_REMOVE_BUCKET
;
1550 command
= OFPGC11_ADD
;
1557 fields
= F_GROUP_TYPE
| F_BUCKETS
;
1560 case OFPGC11_DELETE
:
1564 case OFPGC11_MODIFY
:
1565 fields
= F_GROUP_TYPE
| F_BUCKETS
;
1568 case OFPGC11_ADD_OR_MOD
:
1569 fields
= F_GROUP_TYPE
| F_BUCKETS
;
1572 case OFPGC15_INSERT_BUCKET
:
1573 fields
= F_BUCKETS
| F_COMMAND_BUCKET_ID
;
1574 *usable_protocols
&= OFPUTIL_P_OF15_UP
;
1577 case OFPGC15_REMOVE_BUCKET
:
1578 fields
= F_COMMAND_BUCKET_ID
| F_COMMAND_BUCKET_ID_ALL
;
1579 *usable_protocols
&= OFPUTIL_P_OF15_UP
;
1586 memset(gm
, 0, sizeof *gm
);
1587 gm
->command
= command
;
1588 gm
->group_id
= OFPG_ANY
;
1589 gm
->command_bucket_id
= OFPG15_BUCKET_ALL
;
1590 ovs_list_init(&gm
->buckets
);
1591 if (command
== OFPGC11_DELETE
&& string
[0] == '\0') {
1592 gm
->group_id
= OFPG_ALL
;
1596 *usable_protocols
= OFPUTIL_P_OF11_UP
;
1598 /* Strip the buckets off the end of 'string', if there are any, saving a
1599 * pointer for later. We want to parse the buckets last because the bucket
1600 * type influences bucket defaults. */
1601 char *bkt_str
= strstr(string
, "bucket=");
1603 if (!(fields
& F_BUCKETS
)) {
1604 error
= xstrdup("bucket is not needed");
1610 /* Parse everything before the buckets. */
1613 while (ofputil_parse_key_value(&pos
, &name
, &value
)) {
1614 if (!strcmp(name
, "command_bucket_id")) {
1615 if (!(fields
& F_COMMAND_BUCKET_ID
)) {
1616 error
= xstrdup("command bucket id is not needed");
1619 if (!strcmp(value
, "all")) {
1620 gm
->command_bucket_id
= OFPG15_BUCKET_ALL
;
1621 } else if (!strcmp(value
, "first")) {
1622 gm
->command_bucket_id
= OFPG15_BUCKET_FIRST
;
1623 } else if (!strcmp(value
, "last")) {
1624 gm
->command_bucket_id
= OFPG15_BUCKET_LAST
;
1626 error
= str_to_u32(value
, &gm
->command_bucket_id
);
1630 if (gm
->command_bucket_id
> OFPG15_BUCKET_MAX
1631 && (gm
->command_bucket_id
!= OFPG15_BUCKET_FIRST
1632 && gm
->command_bucket_id
!= OFPG15_BUCKET_LAST
1633 && gm
->command_bucket_id
!= OFPG15_BUCKET_ALL
)) {
1634 error
= xasprintf("invalid command bucket id %"PRIu32
,
1635 gm
->command_bucket_id
);
1639 if (gm
->command_bucket_id
== OFPG15_BUCKET_ALL
1640 && !(fields
& F_COMMAND_BUCKET_ID_ALL
)) {
1641 error
= xstrdup("command_bucket_id=all is not permitted");
1644 had_command_bucket_id
= true;
1645 } else if (!strcmp(name
, "group_id")) {
1646 if(!strcmp(value
, "all")) {
1647 gm
->group_id
= OFPG_ALL
;
1649 error
= str_to_u32(value
, &gm
->group_id
);
1653 if (gm
->group_id
!= OFPG_ALL
&& gm
->group_id
> OFPG_MAX
) {
1654 error
= xasprintf("invalid group id %"PRIu32
,
1659 } else if (!strcmp(name
, "type")){
1660 if (!(fields
& F_GROUP_TYPE
)) {
1661 error
= xstrdup("type is not needed");
1664 if (!strcmp(value
, "all")) {
1665 gm
->type
= OFPGT11_ALL
;
1666 } else if (!strcmp(value
, "select")) {
1667 gm
->type
= OFPGT11_SELECT
;
1668 } else if (!strcmp(value
, "indirect")) {
1669 gm
->type
= OFPGT11_INDIRECT
;
1670 } else if (!strcmp(value
, "ff") ||
1671 !strcmp(value
, "fast_failover")) {
1672 gm
->type
= OFPGT11_FF
;
1674 error
= xasprintf("invalid group type %s", value
);
1678 } else if (!strcmp(name
, "selection_method")) {
1679 if (!(fields
& F_GROUP_TYPE
)) {
1680 error
= xstrdup("selection method is not needed");
1683 if (strlen(value
) >= NTR_MAX_SELECTION_METHOD_LEN
) {
1684 error
= xasprintf("selection method is longer than %u"
1686 NTR_MAX_SELECTION_METHOD_LEN
- 1);
1689 memset(gm
->props
.selection_method
, '\0',
1690 NTR_MAX_SELECTION_METHOD_LEN
);
1691 strcpy(gm
->props
.selection_method
, value
);
1692 *usable_protocols
&= OFPUTIL_P_OF15_UP
;
1693 } else if (!strcmp(name
, "selection_method_param")) {
1694 if (!(fields
& F_GROUP_TYPE
)) {
1695 error
= xstrdup("selection method param is not needed");
1698 error
= str_to_u64(value
, &gm
->props
.selection_method_param
);
1702 *usable_protocols
&= OFPUTIL_P_OF15_UP
;
1703 } else if (!strcmp(name
, "fields")) {
1704 if (!(fields
& F_GROUP_TYPE
)) {
1705 error
= xstrdup("fields are not needed");
1708 error
= parse_select_group_field(value
, &gm
->props
.fields
,
1713 *usable_protocols
&= OFPUTIL_P_OF15_UP
;
1715 error
= xasprintf("unknown keyword %s", name
);
1719 if (gm
->group_id
== OFPG_ANY
) {
1720 error
= xstrdup("must specify a group_id");
1723 if (fields
& F_GROUP_TYPE
&& !had_type
) {
1724 error
= xstrdup("must specify a type");
1728 /* Exclude fields for non "hash" selection method. */
1729 if (strcmp(gm
->props
.selection_method
, "hash") &&
1730 gm
->props
.fields
.values_size
) {
1731 error
= xstrdup("fields may only be specified with \"selection_method=hash\"");
1734 /* Exclude selection_method_param if no selection_method is given. */
1735 if (gm
->props
.selection_method
[0] == 0
1736 && gm
->props
.selection_method_param
!= 0) {
1737 error
= xstrdup("selection_method_param is only allowed with \"selection_method\"");
1740 if (fields
& F_COMMAND_BUCKET_ID
) {
1741 if (!(fields
& F_COMMAND_BUCKET_ID_ALL
|| had_command_bucket_id
)) {
1742 error
= xstrdup("must specify a command bucket id");
1745 } else if (had_command_bucket_id
) {
1746 error
= xstrdup("command bucket id is not needed");
1750 /* Now parse the buckets, if any. */
1754 bkt_str
= strchr(bkt_str
+ 1, '=');
1756 error
= xstrdup("must specify bucket content");
1761 next_bkt_str
= strstr(bkt_str
, "bucket=");
1763 *next_bkt_str
= '\0';
1766 bucket
= xzalloc(sizeof(struct ofputil_bucket
));
1767 error
= parse_bucket_str(bucket
, bkt_str
, gm
->type
, usable_protocols
);
1772 ovs_list_push_back(&gm
->buckets
, &bucket
->list_node
);
1774 if (gm
->type
!= OFPGT11_SELECT
&& bucket
->weight
) {
1775 error
= xstrdup("Only select groups can have bucket weights.");
1779 bkt_str
= next_bkt_str
;
1781 if (gm
->type
== OFPGT11_INDIRECT
&& !ovs_list_is_short(&gm
->buckets
)) {
1782 error
= xstrdup("Indirect groups can have at most one bucket.");
1788 ofputil_uninit_group_mod(gm
);
1792 /* If 'command' is given as -2, each line may start with a command name ("add",
1793 * "modify", "add_or_mod", "delete", "insert_bucket", or "remove_bucket"). A
1794 * missing command name is treated as "add".
1796 char * OVS_WARN_UNUSED_RESULT
1797 parse_ofp_group_mod_str(struct ofputil_group_mod
*gm
, int command
,
1799 enum ofputil_protocol
*usable_protocols
)
1801 char *string
= xstrdup(str_
);
1802 char *error
= parse_ofp_group_mod_str__(gm
, command
, string
,
1808 /* If 'command' is given as -2, each line may start with a command name ("add",
1809 * "modify", "add_or_mod", "delete", "insert_bucket", or "remove_bucket"). A
1810 * missing command name is treated as "add".
1812 char * OVS_WARN_UNUSED_RESULT
1813 parse_ofp_group_mod_file(const char *file_name
, int command
,
1814 struct ofputil_group_mod
**gms
, size_t *n_gms
,
1815 enum ofputil_protocol
*usable_protocols
)
1817 size_t allocated_gms
;
1825 stream
= !strcmp(file_name
, "-") ? stdin
: fopen(file_name
, "r");
1826 if (stream
== NULL
) {
1827 return xasprintf("%s: open failed (%s)",
1828 file_name
, ovs_strerror(errno
));
1831 allocated_gms
= *n_gms
;
1834 *usable_protocols
= OFPUTIL_P_OF11_UP
;
1835 while (!ds_get_preprocessed_line(&s
, stream
, &line_number
)) {
1836 enum ofputil_protocol usable
;
1839 if (*n_gms
>= allocated_gms
) {
1840 struct ofputil_group_mod
*new_gms
;
1843 new_gms
= x2nrealloc(*gms
, &allocated_gms
, sizeof **gms
);
1844 for (i
= 0; i
< *n_gms
; i
++) {
1845 ovs_list_moved(&new_gms
[i
].buckets
, &(*gms
)[i
].buckets
);
1849 error
= parse_ofp_group_mod_str(&(*gms
)[*n_gms
], command
, ds_cstr(&s
),
1854 for (i
= 0; i
< *n_gms
; i
++) {
1855 ofputil_uninit_group_mod(&(*gms
)[i
]);
1862 if (stream
!= stdin
) {
1866 return xasprintf("%s:%d: %s", file_name
, line_number
, error
);
1868 *usable_protocols
&= usable
;
1873 if (stream
!= stdin
) {
1879 /* Opens file 'file_name' and reads each line as a flow_mod or a group_mod,
1880 * depending on the first keyword on each line. Stores each flow and group
1881 * mods in '*bms', an array allocated on the caller's behalf, and the number of
1882 * messages in '*n_bms'.
1884 * Returns NULL if successful, otherwise a malloc()'d string describing the
1885 * error. The caller is responsible for freeing the returned string. */
1886 char * OVS_WARN_UNUSED_RESULT
1887 parse_ofp_bundle_file(const char *file_name
,
1888 struct ofputil_bundle_msg
**bms
, size_t *n_bms
,
1889 enum ofputil_protocol
*usable_protocols
)
1891 size_t allocated_bms
;
1897 *usable_protocols
= OFPUTIL_P_ANY
;
1902 stream
= !strcmp(file_name
, "-") ? stdin
: fopen(file_name
, "r");
1903 if (stream
== NULL
) {
1904 return xasprintf("%s: open failed (%s)",
1905 file_name
, ovs_strerror(errno
));
1908 allocated_bms
= *n_bms
;
1911 while (!ds_get_preprocessed_line(&ds
, stream
, &line_number
)) {
1912 enum ofputil_protocol usable
;
1913 char *s
= ds_cstr(&ds
);
1916 if (*n_bms
>= allocated_bms
) {
1917 struct ofputil_bundle_msg
*new_bms
;
1919 new_bms
= x2nrealloc(*bms
, &allocated_bms
, sizeof **bms
);
1920 for (size_t i
= 0; i
< *n_bms
; i
++) {
1921 if (new_bms
[i
].type
== OFPTYPE_GROUP_MOD
) {
1922 ovs_list_moved(&new_bms
[i
].gm
.buckets
,
1923 &(*bms
)[i
].gm
.buckets
);
1929 s
+= strspn(s
, " \t\r\n"); /* Skip white space. */
1930 len
= strcspn(s
, ", \t\r\n"); /* Get length of the first token. */
1932 if (!strncmp(s
, "flow", len
)) {
1934 error
= parse_ofp_flow_mod_str(&(*bms
)[*n_bms
].fm
, s
, -2, &usable
);
1938 (*bms
)[*n_bms
].type
= OFPTYPE_FLOW_MOD
;
1939 } else if (!strncmp(s
, "group", len
)) {
1941 error
= parse_ofp_group_mod_str(&(*bms
)[*n_bms
].gm
, -2, s
,
1946 (*bms
)[*n_bms
].type
= OFPTYPE_GROUP_MOD
;
1947 } else if (!strncmp(s
, "packet-out", len
)) {
1949 error
= parse_ofp_packet_out_str(&(*bms
)[*n_bms
].po
, s
, &usable
);
1953 (*bms
)[*n_bms
].type
= OFPTYPE_PACKET_OUT
;
1955 error
= xasprintf("Unsupported bundle message type: %.*s",
1960 *usable_protocols
&= usable
; /* Each line can narrow the set. */
1965 if (stream
!= stdin
) {
1970 char *err_msg
= xasprintf("%s:%d: %s", file_name
, line_number
, error
);
1973 ofputil_free_bundle_msgs(*bms
, *n_bms
);
1981 char * OVS_WARN_UNUSED_RESULT
1982 parse_ofp_tlv_table_mod_str(struct ofputil_tlv_table_mod
*ttm
,
1983 uint16_t command
, const char *s
,
1984 enum ofputil_protocol
*usable_protocols
)
1986 *usable_protocols
= OFPUTIL_P_NXM_OXM_ANY
;
1988 ttm
->command
= command
;
1989 ovs_list_init(&ttm
->mappings
);
1992 struct ofputil_tlv_map
*map
= xmalloc(sizeof *map
);
1999 ovs_list_push_back(&ttm
->mappings
, &map
->list_node
);
2001 if (!ovs_scan(s
, "{class=%"SCNi16
",type=%"SCNi8
",len=%"SCNi8
"}->tun_metadata%"SCNi16
"%n",
2002 &map
->option_class
, &map
->option_type
, &map
->option_len
,
2004 ofputil_uninit_tlv_table(&ttm
->mappings
);
2005 return xstrdup("invalid tlv mapping");