2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofp-print.h"
21 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include <netinet/icmp6.h>
27 #include "byte-order.h"
28 #include "classifier.h"
29 #include "dynamic-string.h"
31 #include "meta-flow.h"
32 #include "multipath.h"
35 #include "ofp-actions.h"
36 #include "ofp-errors.h"
42 #include "unaligned.h"
43 #include "type-props.h"
46 VLOG_DEFINE_THIS_MODULE(ofp_util);
48 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
49 * in the peer and so there's not much point in showing a lot of them. */
50 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
52 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
53 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
56 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
57 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
58 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
59 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
62 ofputil_wcbits_to_netmask(int wcbits)
65 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
68 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
69 * that it wildcards, that is, the number of 0-bits in 'netmask', a number
70 * between 0 and 32 inclusive.
72 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
73 * still be in the valid range but isn't otherwise meaningful. */
75 ofputil_netmask_to_wcbits(ovs_be32 netmask)
77 return 32 - ip_count_cidr_bits(netmask);
80 /* A list of the FWW_* and OFPFW10_ bits that have the same value, meaning, and
82 #define WC_INVARIANT_LIST \
83 WC_INVARIANT_BIT(IN_PORT) \
84 WC_INVARIANT_BIT(DL_TYPE) \
85 WC_INVARIANT_BIT(NW_PROTO)
87 /* Verify that all of the invariant bits (as defined on WC_INVARIANT_LIST)
88 * actually have the same names and values. */
89 #define WC_INVARIANT_BIT(NAME) BUILD_ASSERT_DECL(FWW_##NAME == OFPFW10_##NAME);
91 #undef WC_INVARIANT_BIT
93 /* WC_INVARIANTS is the invariant bits (as defined on WC_INVARIANT_LIST) all
95 static const flow_wildcards_t WC_INVARIANTS = 0
96 #define WC_INVARIANT_BIT(NAME) | FWW_##NAME
98 #undef WC_INVARIANT_BIT
101 /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
102 * flow_wildcards in 'wc' for use in struct cls_rule. It is the caller's
103 * responsibility to handle the special case where the flow match's dl_vlan is
104 * set to OFP_VLAN_NONE. */
106 ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc)
108 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 14);
110 /* Initialize most of rule->wc. */
111 flow_wildcards_init_catchall(wc);
112 wc->wildcards = (OVS_FORCE flow_wildcards_t) ofpfw & WC_INVARIANTS;
114 /* Wildcard fields that aren't defined by ofp10_match. */
115 wc->wildcards |= FWW_NW_ECN | FWW_NW_TTL;
117 if (ofpfw & OFPFW10_NW_TOS) {
118 /* OpenFlow 1.0 defines a TOS wildcard, but it's much later in
119 * the enum than we can use. */
120 wc->wildcards |= FWW_NW_DSCP;
123 wc->nw_src_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW10_NW_SRC_SHIFT);
124 wc->nw_dst_mask = ofputil_wcbits_to_netmask(ofpfw >> OFPFW10_NW_DST_SHIFT);
126 if (!(ofpfw & OFPFW10_TP_SRC)) {
127 wc->tp_src_mask = htons(UINT16_MAX);
129 if (!(ofpfw & OFPFW10_TP_DST)) {
130 wc->tp_dst_mask = htons(UINT16_MAX);
133 if (!(ofpfw & OFPFW10_DL_SRC)) {
134 memset(wc->dl_src_mask, 0xff, ETH_ADDR_LEN);
136 if (!(ofpfw & OFPFW10_DL_DST)) {
137 memset(wc->dl_dst_mask, 0xff, ETH_ADDR_LEN);
141 if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) {
142 wc->vlan_tci_mask |= htons(VLAN_PCP_MASK | VLAN_CFI);
144 if (!(ofpfw & OFPFW10_DL_VLAN)) {
145 wc->vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI);
149 /* Converts the ofp10_match in 'match' into a cls_rule in 'rule', with the
150 * given 'priority'. */
152 ofputil_cls_rule_from_ofp10_match(const struct ofp10_match *match,
153 unsigned int priority, struct cls_rule *rule)
155 uint32_t ofpfw = ntohl(match->wildcards) & OFPFW10_ALL;
157 /* Initialize rule->priority, rule->wc. */
158 rule->priority = !ofpfw ? UINT16_MAX : priority;
159 ofputil_wildcard_from_ofpfw10(ofpfw, &rule->wc);
161 /* Initialize most of rule->flow. */
162 rule->flow.nw_src = match->nw_src;
163 rule->flow.nw_dst = match->nw_dst;
164 rule->flow.in_port = ntohs(match->in_port);
165 rule->flow.dl_type = ofputil_dl_type_from_openflow(match->dl_type);
166 rule->flow.tp_src = match->tp_src;
167 rule->flow.tp_dst = match->tp_dst;
168 memcpy(rule->flow.dl_src, match->dl_src, ETH_ADDR_LEN);
169 memcpy(rule->flow.dl_dst, match->dl_dst, ETH_ADDR_LEN);
170 rule->flow.nw_tos = match->nw_tos & IP_DSCP_MASK;
171 rule->flow.nw_proto = match->nw_proto;
173 /* Translate VLANs. */
174 if (!(ofpfw & OFPFW10_DL_VLAN) &&
175 match->dl_vlan == htons(OFP10_VLAN_NONE)) {
176 /* Match only packets without 802.1Q header.
178 * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
180 * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
181 * because we can't have a specific PCP without an 802.1Q header.
182 * However, older versions of OVS treated this as matching packets
183 * withut an 802.1Q header, so we do here too. */
184 rule->flow.vlan_tci = htons(0);
185 rule->wc.vlan_tci_mask = htons(0xffff);
187 ovs_be16 vid, pcp, tci;
189 vid = match->dl_vlan & htons(VLAN_VID_MASK);
190 pcp = htons((match->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
191 tci = vid | pcp | htons(VLAN_CFI);
192 rule->flow.vlan_tci = tci & rule->wc.vlan_tci_mask;
196 cls_rule_zero_wildcarded_fields(rule);
199 /* Convert 'rule' into the OpenFlow 1.0 match structure 'match'. */
201 ofputil_cls_rule_to_ofp10_match(const struct cls_rule *rule,
202 struct ofp10_match *match)
204 const struct flow_wildcards *wc = &rule->wc;
207 /* Figure out most OpenFlow wildcards. */
208 ofpfw = (OVS_FORCE uint32_t) (wc->wildcards & WC_INVARIANTS);
209 ofpfw |= (ofputil_netmask_to_wcbits(wc->nw_src_mask)
210 << OFPFW10_NW_SRC_SHIFT);
211 ofpfw |= (ofputil_netmask_to_wcbits(wc->nw_dst_mask)
212 << OFPFW10_NW_DST_SHIFT);
213 if (wc->wildcards & FWW_NW_DSCP) {
214 ofpfw |= OFPFW10_NW_TOS;
216 if (!wc->tp_src_mask) {
217 ofpfw |= OFPFW10_TP_SRC;
219 if (!wc->tp_dst_mask) {
220 ofpfw |= OFPFW10_TP_DST;
222 if (eth_addr_is_zero(wc->dl_src_mask)) {
223 ofpfw |= OFPFW10_DL_SRC;
225 if (eth_addr_is_zero(wc->dl_dst_mask)) {
226 ofpfw |= OFPFW10_DL_DST;
229 /* Translate VLANs. */
230 match->dl_vlan = htons(0);
231 match->dl_vlan_pcp = 0;
232 if (rule->wc.vlan_tci_mask == htons(0)) {
233 ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP;
234 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
235 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
236 match->dl_vlan = htons(OFP10_VLAN_NONE);
237 ofpfw |= OFPFW10_DL_VLAN_PCP;
239 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
240 ofpfw |= OFPFW10_DL_VLAN;
242 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
245 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
246 ofpfw |= OFPFW10_DL_VLAN_PCP;
248 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
252 /* Compose most of the match structure. */
253 match->wildcards = htonl(ofpfw);
254 match->in_port = htons(rule->flow.in_port);
255 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
256 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
257 match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
258 match->nw_src = rule->flow.nw_src;
259 match->nw_dst = rule->flow.nw_dst;
260 match->nw_tos = rule->flow.nw_tos & IP_DSCP_MASK;
261 match->nw_proto = rule->flow.nw_proto;
262 match->tp_src = rule->flow.tp_src;
263 match->tp_dst = rule->flow.tp_dst;
264 memset(match->pad1, '\0', sizeof match->pad1);
265 memset(match->pad2, '\0', sizeof match->pad2);
269 ofputil_pull_ofp11_match(struct ofpbuf *buf, unsigned int priority,
270 struct cls_rule *rule, uint16_t *padded_match_len)
272 struct ofp11_match_header *omh = buf->data;
275 if (buf->size < sizeof *omh) {
276 return OFPERR_OFPBMC_BAD_LEN;
279 match_len = ntohs(omh->length);
281 switch (ntohs(omh->type)) {
282 case OFPMT_STANDARD: {
283 struct ofp11_match *om;
285 if (match_len != sizeof *om || buf->size < sizeof *om) {
286 return OFPERR_OFPBMC_BAD_LEN;
288 om = ofpbuf_pull(buf, sizeof *om);
289 if (padded_match_len) {
290 *padded_match_len = match_len;
292 return ofputil_cls_rule_from_ofp11_match(om, priority, rule);
296 if (padded_match_len) {
297 *padded_match_len = ROUND_UP(match_len, 8);
299 return oxm_pull_match(buf, priority, rule);
302 return OFPERR_OFPBMC_BAD_TYPE;
306 /* Converts the ofp11_match in 'match' into a cls_rule in 'rule', with the
307 * given 'priority'. Returns 0 if successful, otherwise an OFPERR_* value. */
309 ofputil_cls_rule_from_ofp11_match(const struct ofp11_match *match,
310 unsigned int priority,
311 struct cls_rule *rule)
313 uint16_t wc = ntohl(match->wildcards);
314 uint8_t dl_src_mask[ETH_ADDR_LEN];
315 uint8_t dl_dst_mask[ETH_ADDR_LEN];
319 cls_rule_init_catchall(rule, priority);
321 if (!(wc & OFPFW11_IN_PORT)) {
325 error = ofputil_port_from_ofp11(match->in_port, &ofp_port);
327 return OFPERR_OFPBMC_BAD_VALUE;
329 cls_rule_set_in_port(rule, ofp_port);
332 for (i = 0; i < ETH_ADDR_LEN; i++) {
333 dl_src_mask[i] = ~match->dl_src_mask[i];
335 cls_rule_set_dl_src_masked(rule, match->dl_src, dl_src_mask);
337 for (i = 0; i < ETH_ADDR_LEN; i++) {
338 dl_dst_mask[i] = ~match->dl_dst_mask[i];
340 cls_rule_set_dl_dst_masked(rule, match->dl_dst, dl_dst_mask);
342 if (!(wc & OFPFW11_DL_VLAN)) {
343 if (match->dl_vlan == htons(OFPVID11_NONE)) {
344 /* Match only packets without a VLAN tag. */
345 rule->flow.vlan_tci = htons(0);
346 rule->wc.vlan_tci_mask = htons(UINT16_MAX);
348 if (match->dl_vlan == htons(OFPVID11_ANY)) {
349 /* Match any packet with a VLAN tag regardless of VID. */
350 rule->flow.vlan_tci = htons(VLAN_CFI);
351 rule->wc.vlan_tci_mask = htons(VLAN_CFI);
352 } else if (ntohs(match->dl_vlan) < 4096) {
353 /* Match only packets with the specified VLAN VID. */
354 rule->flow.vlan_tci = htons(VLAN_CFI) | match->dl_vlan;
355 rule->wc.vlan_tci_mask = htons(VLAN_CFI | VLAN_VID_MASK);
358 return OFPERR_OFPBMC_BAD_VALUE;
361 if (!(wc & OFPFW11_DL_VLAN_PCP)) {
362 if (match->dl_vlan_pcp <= 7) {
363 rule->flow.vlan_tci |= htons(match->dl_vlan_pcp
365 rule->wc.vlan_tci_mask |= htons(VLAN_PCP_MASK);
368 return OFPERR_OFPBMC_BAD_VALUE;
374 if (!(wc & OFPFW11_DL_TYPE)) {
375 cls_rule_set_dl_type(rule,
376 ofputil_dl_type_from_openflow(match->dl_type));
379 ipv4 = rule->flow.dl_type == htons(ETH_TYPE_IP);
380 arp = rule->flow.dl_type == htons(ETH_TYPE_ARP);
382 if (ipv4 && !(wc & OFPFW11_NW_TOS)) {
383 if (match->nw_tos & ~IP_DSCP_MASK) {
385 return OFPERR_OFPBMC_BAD_VALUE;
388 cls_rule_set_nw_dscp(rule, match->nw_tos);
392 if (!(wc & OFPFW11_NW_PROTO)) {
393 cls_rule_set_nw_proto(rule, match->nw_proto);
395 cls_rule_set_nw_src_masked(rule, match->nw_src, ~match->nw_src_mask);
396 cls_rule_set_nw_dst_masked(rule, match->nw_dst, ~match->nw_dst_mask);
399 #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
400 if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) {
401 switch (rule->flow.nw_proto) {
403 /* "A.2.3 Flow Match Structures" in OF1.1 says:
405 * The tp_src and tp_dst fields will be ignored unless the
406 * network protocol specified is as TCP, UDP or SCTP.
408 * but I'm pretty sure we should support ICMP too, otherwise
409 * that's a regression from OF1.0. */
410 if (!(wc & OFPFW11_TP_SRC)) {
411 uint16_t icmp_type = ntohs(match->tp_src);
412 if (icmp_type < 0x100) {
413 cls_rule_set_icmp_type(rule, icmp_type);
415 return OFPERR_OFPBMC_BAD_FIELD;
418 if (!(wc & OFPFW11_TP_DST)) {
419 uint16_t icmp_code = ntohs(match->tp_dst);
420 if (icmp_code < 0x100) {
421 cls_rule_set_icmp_code(rule, icmp_code);
423 return OFPERR_OFPBMC_BAD_FIELD;
430 if (!(wc & (OFPFW11_TP_SRC))) {
431 cls_rule_set_tp_src(rule, match->tp_src);
433 if (!(wc & (OFPFW11_TP_DST))) {
434 cls_rule_set_tp_dst(rule, match->tp_dst);
439 /* We don't support SCTP and it seems that we should tell the
440 * controller, since OF1.1 implementations are supposed to. */
441 return OFPERR_OFPBMC_BAD_FIELD;
444 /* OF1.1 says explicitly to ignore this. */
449 if (rule->flow.dl_type == htons(ETH_TYPE_MPLS) ||
450 rule->flow.dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
451 enum { OFPFW11_MPLS_ALL = OFPFW11_MPLS_LABEL | OFPFW11_MPLS_TC };
453 if ((wc & OFPFW11_MPLS_ALL) != OFPFW11_MPLS_ALL) {
454 /* MPLS not supported. */
455 return OFPERR_OFPBMC_BAD_TAG;
459 if (match->metadata_mask != htonll(UINT64_MAX)) {
460 cls_rule_set_metadata_masked(rule, match->metadata,
461 ~match->metadata_mask);
467 /* Convert 'rule' into the OpenFlow 1.1 match structure 'match'. */
469 ofputil_cls_rule_to_ofp11_match(const struct cls_rule *rule,
470 struct ofp11_match *match)
475 memset(match, 0, sizeof *match);
476 match->omh.type = htons(OFPMT_STANDARD);
477 match->omh.length = htons(OFPMT11_STANDARD_LENGTH);
479 if (rule->wc.wildcards & FWW_IN_PORT) {
480 wc |= OFPFW11_IN_PORT;
482 match->in_port = ofputil_port_to_ofp11(rule->flow.in_port);
485 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
486 for (i = 0; i < ETH_ADDR_LEN; i++) {
487 match->dl_src_mask[i] = ~rule->wc.dl_src_mask[i];
490 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
491 for (i = 0; i < ETH_ADDR_LEN; i++) {
492 match->dl_dst_mask[i] = ~rule->wc.dl_dst_mask[i];
495 if (rule->wc.vlan_tci_mask == htons(0)) {
496 wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP;
497 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
498 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
499 match->dl_vlan = htons(OFPVID11_NONE);
500 wc |= OFPFW11_DL_VLAN_PCP;
502 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
503 match->dl_vlan = htons(OFPVID11_ANY);
505 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
508 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
509 wc |= OFPFW11_DL_VLAN_PCP;
511 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
515 if (rule->wc.wildcards & FWW_DL_TYPE) {
516 wc |= OFPFW11_DL_TYPE;
518 match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
521 if (rule->wc.wildcards & FWW_NW_DSCP) {
522 wc |= OFPFW11_NW_TOS;
524 match->nw_tos = rule->flow.nw_tos & IP_DSCP_MASK;
527 if (rule->wc.wildcards & FWW_NW_PROTO) {
528 wc |= OFPFW11_NW_PROTO;
530 match->nw_proto = rule->flow.nw_proto;
533 match->nw_src = rule->flow.nw_src;
534 match->nw_src_mask = ~rule->wc.nw_src_mask;
535 match->nw_dst = rule->flow.nw_dst;
536 match->nw_dst_mask = ~rule->wc.nw_dst_mask;
538 if (!rule->wc.tp_src_mask) {
539 wc |= OFPFW11_TP_SRC;
541 match->tp_src = rule->flow.tp_src;
544 if (!rule->wc.tp_dst_mask) {
545 wc |= OFPFW11_TP_DST;
547 match->tp_dst = rule->flow.tp_dst;
550 /* MPLS not supported. */
551 wc |= OFPFW11_MPLS_LABEL;
552 wc |= OFPFW11_MPLS_TC;
554 match->metadata = rule->flow.metadata;
555 match->metadata_mask = ~rule->wc.metadata_mask;
557 match->wildcards = htonl(wc);
560 /* Given a 'dl_type' value in the format used in struct flow, returns the
561 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
564 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
566 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
567 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
571 /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
572 * structure, returns the corresponding 'dl_type' value for use in struct
575 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
577 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
578 ? htons(FLOW_DL_TYPE_NONE)
584 struct proto_abbrev {
585 enum ofputil_protocol protocol;
589 /* Most users really don't care about some of the differences between
590 * protocols. These abbreviations help with that. */
591 static const struct proto_abbrev proto_abbrevs[] = {
592 { OFPUTIL_P_ANY, "any" },
593 { OFPUTIL_P_OF10_ANY, "OpenFlow10" },
594 { OFPUTIL_P_NXM_ANY, "NXM" },
596 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
598 enum ofputil_protocol ofputil_flow_dump_protocols[] = {
602 size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
604 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
605 * connection that has negotiated the given 'version'. 'version' should
606 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
607 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
608 * outside the valid range. */
609 enum ofputil_protocol
610 ofputil_protocol_from_ofp_version(enum ofp_version version)
614 return OFPUTIL_P_OF10;
616 return OFPUTIL_P_OF12;
623 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
624 * OFP11_VERSION or OFP12_VERSION) that corresponds to 'protocol'. */
626 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
630 case OFPUTIL_P_OF10_TID:
632 case OFPUTIL_P_NXM_TID:
633 return OFP10_VERSION;
635 return OFP12_VERSION;
641 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
644 ofputil_protocol_is_valid(enum ofputil_protocol protocol)
646 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
649 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
650 * extension turned on or off if 'enable' is true or false, respectively.
652 * This extension is only useful for protocols whose "standard" version does
653 * not allow specific tables to be modified. In particular, this is true of
654 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
655 * specifies a table ID and so there is no need for such an extension. When
656 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
657 * extension, this function just returns its 'protocol' argument unchanged
658 * regardless of the value of 'enable'. */
659 enum ofputil_protocol
660 ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
664 case OFPUTIL_P_OF10_TID:
665 return enable ? OFPUTIL_P_OF10_TID : OFPUTIL_P_OF10;
668 case OFPUTIL_P_NXM_TID:
669 return enable ? OFPUTIL_P_NXM_TID : OFPUTIL_P_NXM;
672 return OFPUTIL_P_OF12;
679 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
680 * some extension to a standard protocol version, the return value is the
681 * standard version of that protocol without any extension. If 'protocol' is a
682 * standard protocol version, returns 'protocol' unchanged. */
683 enum ofputil_protocol
684 ofputil_protocol_to_base(enum ofputil_protocol protocol)
686 return ofputil_protocol_set_tid(protocol, false);
689 /* Returns 'new_base' with any extensions taken from 'cur'. */
690 enum ofputil_protocol
691 ofputil_protocol_set_base(enum ofputil_protocol cur,
692 enum ofputil_protocol new_base)
694 bool tid = (cur & OFPUTIL_P_TID) != 0;
698 case OFPUTIL_P_OF10_TID:
699 return ofputil_protocol_set_tid(OFPUTIL_P_OF10, tid);
702 case OFPUTIL_P_NXM_TID:
703 return ofputil_protocol_set_tid(OFPUTIL_P_NXM, tid);
706 return ofputil_protocol_set_tid(OFPUTIL_P_OF12, tid);
713 /* Returns a string form of 'protocol', if a simple form exists (that is, if
714 * 'protocol' is either a single protocol or it is a combination of protocols
715 * that have a single abbreviation). Otherwise, returns NULL. */
717 ofputil_protocol_to_string(enum ofputil_protocol protocol)
719 const struct proto_abbrev *p;
721 /* Use a "switch" statement for single-bit names so that we get a compiler
722 * warning if we forget any. */
725 return "NXM-table_id";
727 case OFPUTIL_P_NXM_TID:
728 return "NXM+table_id";
731 return "OpenFlow10-table_id";
733 case OFPUTIL_P_OF10_TID:
734 return "OpenFlow10+table_id";
740 /* Check abbreviations. */
741 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
742 if (protocol == p->protocol) {
750 /* Returns a string that represents 'protocols'. The return value might be a
751 * comma-separated list if 'protocols' doesn't have a simple name. The return
752 * value is "none" if 'protocols' is 0.
754 * The caller must free the returned string (with free()). */
756 ofputil_protocols_to_string(enum ofputil_protocol protocols)
760 assert(!(protocols & ~OFPUTIL_P_ANY));
761 if (protocols == 0) {
762 return xstrdup("none");
767 const struct proto_abbrev *p;
771 ds_put_char(&s, ',');
774 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
775 if ((protocols & p->protocol) == p->protocol) {
776 ds_put_cstr(&s, p->name);
777 protocols &= ~p->protocol;
782 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
783 enum ofputil_protocol bit = 1u << i;
785 if (protocols & bit) {
786 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
795 return ds_steal_cstr(&s);
798 static enum ofputil_protocol
799 ofputil_protocol_from_string__(const char *s, size_t n)
801 const struct proto_abbrev *p;
804 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
805 enum ofputil_protocol bit = 1u << i;
806 const char *name = ofputil_protocol_to_string(bit);
808 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
813 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
814 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
822 /* Returns the nonempty set of protocols represented by 's', which can be a
823 * single protocol name or abbreviation or a comma-separated list of them.
825 * Aborts the program with an error message if 's' is invalid. */
826 enum ofputil_protocol
827 ofputil_protocols_from_string(const char *s)
829 const char *orig_s = s;
830 enum ofputil_protocol protocols;
834 enum ofputil_protocol p;
843 p = ofputil_protocol_from_string__(s, n);
845 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
853 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
859 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
861 switch (packet_in_format) {
862 case NXPIF_OPENFLOW10:
871 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
873 switch (packet_in_format) {
874 case NXPIF_OPENFLOW10:
884 ofputil_packet_in_format_from_string(const char *s)
886 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
887 : !strcmp(s, "nxm") ? NXPIF_NXM
892 regs_fully_wildcarded(const struct flow_wildcards *wc)
896 for (i = 0; i < FLOW_N_REGS; i++) {
897 if (wc->reg_masks[i] != 0) {
904 /* Returns a bit-mask of ofputil_protocols that can be used for sending 'rule'
905 * to a switch (e.g. to add or remove a flow). Only NXM can handle tunnel IDs,
906 * registers, or fixing the Ethernet multicast bit. Otherwise, it's better to
907 * use OpenFlow 1.0 protocol for backward compatibility. */
908 enum ofputil_protocol
909 ofputil_usable_protocols(const struct cls_rule *rule)
911 const struct flow_wildcards *wc = &rule->wc;
913 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 14);
915 /* NXM and OF1.1+ supports bitwise matching on ethernet addresses. */
916 if (!eth_mask_is_exact(wc->dl_src_mask)
917 && !eth_addr_is_zero(wc->dl_src_mask)) {
918 return OFPUTIL_P_NXM_ANY;
920 if (!eth_mask_is_exact(wc->dl_dst_mask)
921 && !eth_addr_is_zero(wc->dl_dst_mask)) {
922 return OFPUTIL_P_NXM_ANY;
925 /* NXM and OF1.1+ support matching metadata. */
926 if (wc->metadata_mask != htonll(0)) {
927 return OFPUTIL_P_NXM_ANY;
930 /* Only NXM supports matching ARP hardware addresses. */
931 if (!eth_addr_is_zero(wc->arp_sha_mask) ||
932 !eth_addr_is_zero(wc->arp_tha_mask)) {
933 return OFPUTIL_P_NXM_ANY;
936 /* Only NXM supports matching IPv6 traffic. */
937 if (!(wc->wildcards & FWW_DL_TYPE)
938 && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) {
939 return OFPUTIL_P_NXM_ANY;
942 /* Only NXM supports matching registers. */
943 if (!regs_fully_wildcarded(wc)) {
944 return OFPUTIL_P_NXM_ANY;
947 /* Only NXM supports matching tun_id. */
948 if (wc->tun_id_mask != htonll(0)) {
949 return OFPUTIL_P_NXM_ANY;
952 /* Only NXM supports matching fragments. */
953 if (wc->nw_frag_mask) {
954 return OFPUTIL_P_NXM_ANY;
957 /* Only NXM supports matching IPv6 flow label. */
958 if (wc->ipv6_label_mask) {
959 return OFPUTIL_P_NXM_ANY;
962 /* Only NXM supports matching IP ECN bits. */
963 if (!(wc->wildcards & FWW_NW_ECN)) {
964 return OFPUTIL_P_NXM_ANY;
967 /* Only NXM supports matching IP TTL/hop limit. */
968 if (!(wc->wildcards & FWW_NW_TTL)) {
969 return OFPUTIL_P_NXM_ANY;
972 /* Only NXM supports non-CIDR IPv4 address masks. */
973 if (!ip_is_cidr(wc->nw_src_mask) || !ip_is_cidr(wc->nw_dst_mask)) {
974 return OFPUTIL_P_NXM_ANY;
977 /* Only NXM supports bitwise matching on transport port. */
978 if ((wc->tp_src_mask && wc->tp_src_mask != htons(UINT16_MAX)) ||
979 (wc->tp_dst_mask && wc->tp_dst_mask != htons(UINT16_MAX))) {
980 return OFPUTIL_P_NXM_ANY;
983 /* Other formats can express this rule. */
984 return OFPUTIL_P_ANY;
987 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
988 * protocol is 'current', at least partly transitions the protocol to 'want'.
989 * Stores in '*next' the protocol that will be in effect on the OpenFlow
990 * connection if the switch processes the returned message correctly. (If
991 * '*next != want' then the caller will have to iterate.)
993 * If 'current == want', returns NULL and stores 'current' in '*next'. */
995 ofputil_encode_set_protocol(enum ofputil_protocol current,
996 enum ofputil_protocol want,
997 enum ofputil_protocol *next)
999 enum ofputil_protocol cur_base, want_base;
1000 bool cur_tid, want_tid;
1002 cur_base = ofputil_protocol_to_base(current);
1003 want_base = ofputil_protocol_to_base(want);
1004 if (cur_base != want_base) {
1005 *next = ofputil_protocol_set_base(current, want_base);
1007 switch (want_base) {
1009 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1011 case OFPUTIL_P_OF10:
1012 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1014 case OFPUTIL_P_OF12:
1015 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW12);
1017 case OFPUTIL_P_OF10_TID:
1018 case OFPUTIL_P_NXM_TID:
1023 cur_tid = (current & OFPUTIL_P_TID) != 0;
1024 want_tid = (want & OFPUTIL_P_TID) != 0;
1025 if (cur_tid != want_tid) {
1026 *next = ofputil_protocol_set_tid(current, want_tid);
1027 return ofputil_make_flow_mod_table_id(want_tid);
1030 assert(current == want);
1036 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1037 * format to 'nxff'. */
1039 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1041 struct nx_set_flow_format *sff;
1044 assert(ofputil_nx_flow_format_is_valid(nxff));
1046 msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1047 sff = ofpbuf_put_zeros(msg, sizeof *sff);
1048 sff->format = htonl(nxff);
1053 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1055 enum ofputil_protocol
1056 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1058 switch (flow_format) {
1059 case NXFF_OPENFLOW10:
1060 return OFPUTIL_P_OF10;
1063 return OFPUTIL_P_NXM;
1065 case NXFF_OPENFLOW12:
1066 return OFPUTIL_P_OF12;
1073 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1075 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1077 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1080 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1083 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1085 switch (flow_format) {
1086 case NXFF_OPENFLOW10:
1087 return "openflow10";
1090 case NXFF_OPENFLOW12:
1091 return "openflow12";
1098 ofputil_make_set_packet_in_format(enum nx_packet_in_format packet_in_format)
1100 struct nx_set_packet_in_format *spif;
1103 msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, OFP10_VERSION, 0);
1104 spif = ofpbuf_put_zeros(msg, sizeof *spif);
1105 spif->format = htonl(packet_in_format);
1110 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1111 * extension on or off (according to 'flow_mod_table_id'). */
1113 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1115 struct nx_flow_mod_table_id *nfmti;
1118 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1119 nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
1120 nfmti->set = flow_mod_table_id;
1124 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1125 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1128 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1129 * The caller must initialize 'ofpacts' and retains ownership of it.
1130 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1132 * Does not validate the flow_mod actions. The caller should do that, with
1133 * ofpacts_check(). */
1135 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1136 const struct ofp_header *oh,
1137 enum ofputil_protocol protocol,
1138 struct ofpbuf *ofpacts)
1144 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1145 raw = ofpraw_pull_assert(&b);
1146 if (raw == OFPRAW_OFPT11_FLOW_MOD) {
1147 /* Standard OpenFlow 1.1 flow_mod. */
1148 const struct ofp11_flow_mod *ofm;
1151 ofm = ofpbuf_pull(&b, sizeof *ofm);
1153 error = ofputil_pull_ofp11_match(&b, ntohs(ofm->priority), &fm->cr,
1159 error = ofpacts_pull_openflow11_instructions(&b, b.size, ofpacts);
1164 /* Translate the message. */
1165 if (ofm->command == OFPFC_ADD) {
1166 fm->cookie = htonll(0);
1167 fm->cookie_mask = htonll(0);
1168 fm->new_cookie = ofm->cookie;
1171 fm->cookie = ofm->cookie;
1172 fm->cookie_mask = ofm->cookie_mask;
1173 fm->new_cookie = htonll(UINT64_MAX);
1175 fm->command = ofm->command;
1176 fm->table_id = ofm->table_id;
1177 fm->idle_timeout = ntohs(ofm->idle_timeout);
1178 fm->hard_timeout = ntohs(ofm->hard_timeout);
1179 fm->buffer_id = ntohl(ofm->buffer_id);
1180 error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
1184 if (ofm->out_group != htonl(OFPG_ANY)) {
1185 return OFPERR_NXFMFC_GROUPS_NOT_SUPPORTED;
1187 fm->flags = ntohs(ofm->flags);
1189 if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1190 /* Standard OpenFlow 1.0 flow_mod. */
1191 const struct ofp10_flow_mod *ofm;
1195 /* Get the ofp10_flow_mod. */
1196 ofm = ofpbuf_pull(&b, sizeof *ofm);
1198 /* Set priority based on original wildcards. Normally we'd allow
1199 * ofputil_cls_rule_from_match() to do this for us, but
1200 * ofputil_normalize_rule() can put wildcards where the original
1201 * flow didn't have them. */
1202 priority = ntohs(ofm->priority);
1203 if (!(ofm->match.wildcards & htonl(OFPFW10_ALL))) {
1204 priority = UINT16_MAX;
1207 /* Translate the rule. */
1208 ofputil_cls_rule_from_ofp10_match(&ofm->match, priority, &fm->cr);
1209 ofputil_normalize_rule(&fm->cr);
1211 /* Now get the actions. */
1212 error = ofpacts_pull_openflow10(&b, b.size, ofpacts);
1217 /* Translate the message. */
1218 command = ntohs(ofm->command);
1219 fm->cookie = htonll(0);
1220 fm->cookie_mask = htonll(0);
1221 fm->new_cookie = ofm->cookie;
1222 fm->idle_timeout = ntohs(ofm->idle_timeout);
1223 fm->hard_timeout = ntohs(ofm->hard_timeout);
1224 fm->buffer_id = ntohl(ofm->buffer_id);
1225 fm->out_port = ntohs(ofm->out_port);
1226 fm->flags = ntohs(ofm->flags);
1227 } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1228 /* Nicira extended flow_mod. */
1229 const struct nx_flow_mod *nfm;
1232 /* Dissect the message. */
1233 nfm = ofpbuf_pull(&b, sizeof *nfm);
1234 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
1235 &fm->cr, &fm->cookie, &fm->cookie_mask);
1239 error = ofpacts_pull_openflow10(&b, b.size, ofpacts);
1244 /* Translate the message. */
1245 command = ntohs(nfm->command);
1246 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1247 /* Flow additions may only set a new cookie, not match an
1248 * existing cookie. */
1249 return OFPERR_NXBRC_NXM_INVALID;
1251 fm->new_cookie = nfm->cookie;
1252 fm->idle_timeout = ntohs(nfm->idle_timeout);
1253 fm->hard_timeout = ntohs(nfm->hard_timeout);
1254 fm->buffer_id = ntohl(nfm->buffer_id);
1255 fm->out_port = ntohs(nfm->out_port);
1256 fm->flags = ntohs(nfm->flags);
1261 if (protocol & OFPUTIL_P_TID) {
1262 fm->command = command & 0xff;
1263 fm->table_id = command >> 8;
1265 fm->command = command;
1266 fm->table_id = 0xff;
1270 fm->ofpacts = ofpacts->data;
1271 fm->ofpacts_len = ofpacts->size;
1277 ofputil_tid_command(const struct ofputil_flow_mod *fm,
1278 enum ofputil_protocol protocol)
1280 return htons(protocol & OFPUTIL_P_TID
1281 ? (fm->command & 0xff) | (fm->table_id << 8)
1285 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
1286 * 'protocol' and returns the message. */
1288 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
1289 enum ofputil_protocol protocol)
1294 case OFPUTIL_P_OF12: {
1295 struct ofp11_flow_mod *ofm;
1297 msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, OFP12_VERSION,
1298 NXM_TYPICAL_LEN + fm->ofpacts_len);
1299 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
1300 ofm->cookie = fm->new_cookie;
1301 ofm->cookie_mask = fm->cookie_mask;
1302 ofm->table_id = fm->table_id;
1303 ofm->command = fm->command;
1304 ofm->idle_timeout = htons(fm->idle_timeout);
1305 ofm->hard_timeout = htons(fm->hard_timeout);
1306 ofm->priority = htons(fm->cr.priority);
1307 ofm->buffer_id = htonl(fm->buffer_id);
1308 ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
1309 ofm->out_group = htonl(OFPG11_ANY);
1310 ofm->flags = htons(fm->flags);
1311 oxm_put_match(msg, &fm->cr);
1313 ofpacts_put_openflow11_instructions(fm->ofpacts, fm->ofpacts_len,
1319 case OFPUTIL_P_OF10:
1320 case OFPUTIL_P_OF10_TID: {
1321 struct ofp10_flow_mod *ofm;
1323 msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
1325 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
1326 ofputil_cls_rule_to_ofp10_match(&fm->cr, &ofm->match);
1327 ofm->cookie = fm->new_cookie;
1328 ofm->command = ofputil_tid_command(fm, protocol);
1329 ofm->idle_timeout = htons(fm->idle_timeout);
1330 ofm->hard_timeout = htons(fm->hard_timeout);
1331 ofm->priority = htons(fm->cr.priority);
1332 ofm->buffer_id = htonl(fm->buffer_id);
1333 ofm->out_port = htons(fm->out_port);
1334 ofm->flags = htons(fm->flags);
1336 ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
1342 case OFPUTIL_P_NXM_TID: {
1343 struct nx_flow_mod *nfm;
1346 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
1347 NXM_TYPICAL_LEN + fm->ofpacts_len);
1348 nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
1349 nfm->command = ofputil_tid_command(fm, protocol);
1350 nfm->cookie = fm->new_cookie;
1351 match_len = nx_put_match(msg, &fm->cr, fm->cookie, fm->cookie_mask);
1353 nfm->idle_timeout = htons(fm->idle_timeout);
1354 nfm->hard_timeout = htons(fm->hard_timeout);
1355 nfm->priority = htons(fm->cr.priority);
1356 nfm->buffer_id = htonl(fm->buffer_id);
1357 nfm->out_port = htons(fm->out_port);
1358 nfm->flags = htons(fm->flags);
1359 nfm->match_len = htons(match_len);
1361 ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
1370 ofpmsg_update_length(msg);
1374 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1375 * send all of the 'n_fm's flow table modification requests in 'fms', and a
1376 * 0-bit for each protocol that is inadequate.
1378 * (The return value will have at least one 1-bit.) */
1379 enum ofputil_protocol
1380 ofputil_flow_mod_usable_protocols(const struct ofputil_flow_mod *fms,
1383 enum ofputil_protocol usable_protocols;
1386 usable_protocols = OFPUTIL_P_ANY;
1387 for (i = 0; i < n_fms; i++) {
1388 const struct ofputil_flow_mod *fm = &fms[i];
1390 usable_protocols &= ofputil_usable_protocols(&fm->cr);
1391 if (fm->table_id != 0xff) {
1392 usable_protocols &= OFPUTIL_P_TID;
1395 /* Matching of the cookie is only supported through NXM. */
1396 if (fm->cookie_mask != htonll(0)) {
1397 usable_protocols &= OFPUTIL_P_NXM_ANY;
1400 assert(usable_protocols);
1402 return usable_protocols;
1406 ofputil_decode_ofpst_flow_request(struct ofputil_flow_stats_request *fsr,
1407 const struct ofp10_flow_stats_request *ofsr,
1410 fsr->aggregate = aggregate;
1411 ofputil_cls_rule_from_ofp10_match(&ofsr->match, 0, &fsr->match);
1412 fsr->out_port = ntohs(ofsr->out_port);
1413 fsr->table_id = ofsr->table_id;
1414 fsr->cookie = fsr->cookie_mask = htonll(0);
1420 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
1421 struct ofpbuf *b, bool aggregate)
1423 const struct nx_flow_stats_request *nfsr;
1426 nfsr = ofpbuf_pull(b, sizeof *nfsr);
1427 error = nx_pull_match(b, ntohs(nfsr->match_len), 0, &fsr->match,
1428 &fsr->cookie, &fsr->cookie_mask);
1433 return OFPERR_OFPBRC_BAD_LEN;
1436 fsr->aggregate = aggregate;
1437 fsr->out_port = ntohs(nfsr->out_port);
1438 fsr->table_id = nfsr->table_id;
1443 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1444 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1445 * successful, otherwise an OpenFlow error code. */
1447 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
1448 const struct ofp_header *oh)
1453 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1454 raw = ofpraw_pull_assert(&b);
1455 switch ((int) raw) {
1456 case OFPRAW_OFPST_FLOW_REQUEST:
1457 return ofputil_decode_ofpst_flow_request(fsr, b.data, false);
1459 case OFPRAW_OFPST_AGGREGATE_REQUEST:
1460 return ofputil_decode_ofpst_flow_request(fsr, b.data, true);
1462 case OFPRAW_NXST_FLOW_REQUEST:
1463 return ofputil_decode_nxst_flow_request(fsr, &b, false);
1465 case OFPRAW_NXST_AGGREGATE_REQUEST:
1466 return ofputil_decode_nxst_flow_request(fsr, &b, true);
1469 /* Hey, the caller lied. */
1474 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1475 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1476 * 'protocol', and returns the message. */
1478 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
1479 enum ofputil_protocol protocol)
1485 case OFPUTIL_P_OF10:
1486 case OFPUTIL_P_OF10_TID: {
1487 struct ofp10_flow_stats_request *ofsr;
1489 raw = (fsr->aggregate
1490 ? OFPRAW_OFPST_AGGREGATE_REQUEST
1491 : OFPRAW_OFPST_FLOW_REQUEST);
1492 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
1493 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
1494 ofputil_cls_rule_to_ofp10_match(&fsr->match, &ofsr->match);
1495 ofsr->table_id = fsr->table_id;
1496 ofsr->out_port = htons(fsr->out_port);
1501 case OFPUTIL_P_NXM_TID: {
1502 struct nx_flow_stats_request *nfsr;
1505 raw = (fsr->aggregate
1506 ? OFPRAW_NXST_AGGREGATE_REQUEST
1507 : OFPRAW_NXST_FLOW_REQUEST);
1508 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
1509 ofpbuf_put_zeros(msg, sizeof *nfsr);
1510 match_len = nx_put_match(msg, &fsr->match,
1511 fsr->cookie, fsr->cookie_mask);
1514 nfsr->out_port = htons(fsr->out_port);
1515 nfsr->match_len = htons(match_len);
1516 nfsr->table_id = fsr->table_id;
1520 case OFPUTIL_P_OF12:
1528 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1529 * accurately encode 'fsr', and a 0-bit for each protocol that is inadequate.
1531 * (The return value will have at least one 1-bit.) */
1532 enum ofputil_protocol
1533 ofputil_flow_stats_request_usable_protocols(
1534 const struct ofputil_flow_stats_request *fsr)
1536 enum ofputil_protocol usable_protocols;
1538 usable_protocols = ofputil_usable_protocols(&fsr->match);
1539 if (fsr->cookie_mask != htonll(0)) {
1540 usable_protocols &= OFPUTIL_P_NXM_ANY;
1542 return usable_protocols;
1545 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1546 * ofputil_flow_stats in 'fs'.
1548 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1549 * OpenFlow message. Calling this function multiple times for a single 'msg'
1550 * iterates through the replies. The caller must initially leave 'msg''s layer
1551 * pointers null and not modify them between calls.
1553 * Most switches don't send the values needed to populate fs->idle_age and
1554 * fs->hard_age, so those members will usually be set to 0. If the switch from
1555 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
1556 * 'flow_age_extension' as true so that the contents of 'msg' determine the
1557 * 'idle_age' and 'hard_age' members in 'fs'.
1559 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
1560 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
1561 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
1563 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1564 * otherwise a positive errno value. */
1566 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1568 bool flow_age_extension,
1569 struct ofpbuf *ofpacts)
1575 ? ofpraw_decode(&raw, msg->l2)
1576 : ofpraw_pull(&raw, msg));
1583 } else if (raw == OFPRAW_OFPST_FLOW_REPLY) {
1584 const struct ofp10_flow_stats *ofs;
1587 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1589 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1590 "bytes at end", msg->size);
1594 length = ntohs(ofs->length);
1595 if (length < sizeof *ofs) {
1596 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1597 "length %zu", length);
1601 if (ofpacts_pull_openflow10(msg, length - sizeof *ofs, ofpacts)) {
1605 fs->cookie = get_32aligned_be64(&ofs->cookie);
1606 ofputil_cls_rule_from_ofp10_match(&ofs->match, ntohs(ofs->priority),
1608 fs->table_id = ofs->table_id;
1609 fs->duration_sec = ntohl(ofs->duration_sec);
1610 fs->duration_nsec = ntohl(ofs->duration_nsec);
1611 fs->idle_timeout = ntohs(ofs->idle_timeout);
1612 fs->hard_timeout = ntohs(ofs->hard_timeout);
1615 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1616 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
1617 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
1618 const struct nx_flow_stats *nfs;
1619 size_t match_len, actions_len, length;
1621 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1623 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1624 "bytes at end", msg->size);
1628 length = ntohs(nfs->length);
1629 match_len = ntohs(nfs->match_len);
1630 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1631 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1632 "claims invalid length %zu", match_len, length);
1635 if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule,
1640 actions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
1641 if (ofpacts_pull_openflow10(msg, actions_len, ofpacts)) {
1645 fs->cookie = nfs->cookie;
1646 fs->table_id = nfs->table_id;
1647 fs->duration_sec = ntohl(nfs->duration_sec);
1648 fs->duration_nsec = ntohl(nfs->duration_nsec);
1649 fs->idle_timeout = ntohs(nfs->idle_timeout);
1650 fs->hard_timeout = ntohs(nfs->hard_timeout);
1653 if (flow_age_extension) {
1654 if (nfs->idle_age) {
1655 fs->idle_age = ntohs(nfs->idle_age) - 1;
1657 if (nfs->hard_age) {
1658 fs->hard_age = ntohs(nfs->hard_age) - 1;
1661 fs->packet_count = ntohll(nfs->packet_count);
1662 fs->byte_count = ntohll(nfs->byte_count);
1667 fs->ofpacts = ofpacts->data;
1668 fs->ofpacts_len = ofpacts->size;
1673 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
1675 * We use this in situations where OVS internally uses UINT64_MAX to mean
1676 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
1678 unknown_to_zero(uint64_t count)
1680 return count != UINT64_MAX ? count : 0;
1683 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
1684 * those already present in the list of ofpbufs in 'replies'. 'replies' should
1685 * have been initialized with ofputil_start_stats_reply(). */
1687 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
1688 struct list *replies)
1690 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
1691 size_t start_ofs = reply->size;
1694 ofpraw_decode_partial(&raw, reply->data, reply->size);
1695 if (raw == OFPRAW_OFPST_FLOW_REPLY) {
1696 struct ofp10_flow_stats *ofs;
1698 ofpbuf_put_uninit(reply, sizeof *ofs);
1699 ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply);
1701 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
1702 ofs->length = htons(reply->size - start_ofs);
1703 ofs->table_id = fs->table_id;
1705 ofputil_cls_rule_to_ofp10_match(&fs->rule, &ofs->match);
1706 ofs->duration_sec = htonl(fs->duration_sec);
1707 ofs->duration_nsec = htonl(fs->duration_nsec);
1708 ofs->priority = htons(fs->rule.priority);
1709 ofs->idle_timeout = htons(fs->idle_timeout);
1710 ofs->hard_timeout = htons(fs->hard_timeout);
1711 memset(ofs->pad2, 0, sizeof ofs->pad2);
1712 put_32aligned_be64(&ofs->cookie, fs->cookie);
1713 put_32aligned_be64(&ofs->packet_count,
1714 htonll(unknown_to_zero(fs->packet_count)));
1715 put_32aligned_be64(&ofs->byte_count,
1716 htonll(unknown_to_zero(fs->byte_count)));
1717 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
1718 struct nx_flow_stats *nfs;
1721 ofpbuf_put_uninit(reply, sizeof *nfs);
1722 match_len = nx_put_match(reply, &fs->rule, 0, 0);
1723 ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply);
1725 nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
1726 nfs->length = htons(reply->size - start_ofs);
1727 nfs->table_id = fs->table_id;
1729 nfs->duration_sec = htonl(fs->duration_sec);
1730 nfs->duration_nsec = htonl(fs->duration_nsec);
1731 nfs->priority = htons(fs->rule.priority);
1732 nfs->idle_timeout = htons(fs->idle_timeout);
1733 nfs->hard_timeout = htons(fs->hard_timeout);
1734 nfs->idle_age = htons(fs->idle_age < 0 ? 0
1735 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
1737 nfs->hard_age = htons(fs->hard_age < 0 ? 0
1738 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
1740 nfs->match_len = htons(match_len);
1741 nfs->cookie = fs->cookie;
1742 nfs->packet_count = htonll(fs->packet_count);
1743 nfs->byte_count = htonll(fs->byte_count);
1748 ofpmp_postappend(replies, start_ofs);
1751 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
1752 * NXST_AGGREGATE reply matching 'request', and returns the message. */
1754 ofputil_encode_aggregate_stats_reply(
1755 const struct ofputil_aggregate_stats *stats,
1756 const struct ofp_header *request)
1758 struct ofp_aggregate_stats_reply *asr;
1759 uint64_t packet_count;
1760 uint64_t byte_count;
1764 ofpraw_decode(&raw, request);
1765 if (raw == OFPRAW_OFPST_AGGREGATE_REQUEST) {
1766 packet_count = unknown_to_zero(stats->packet_count);
1767 byte_count = unknown_to_zero(stats->byte_count);
1769 packet_count = stats->packet_count;
1770 byte_count = stats->byte_count;
1773 msg = ofpraw_alloc_stats_reply(request, 0);
1774 asr = ofpbuf_put_zeros(msg, sizeof *asr);
1775 put_32aligned_be64(&asr->packet_count, htonll(packet_count));
1776 put_32aligned_be64(&asr->byte_count, htonll(byte_count));
1777 asr->flow_count = htonl(stats->flow_count);
1783 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
1784 const struct ofp_header *reply)
1786 struct ofp_aggregate_stats_reply *asr;
1789 ofpbuf_use_const(&msg, reply, ntohs(reply->length));
1790 ofpraw_pull_assert(&msg);
1793 stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
1794 stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
1795 stats->flow_count = ntohl(asr->flow_count);
1800 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
1801 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
1802 * an OpenFlow error code. */
1804 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
1805 const struct ofp_header *oh)
1810 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1811 raw = ofpraw_pull_assert(&b);
1812 if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
1813 const struct ofp_flow_removed *ofr;
1815 ofr = ofpbuf_pull(&b, sizeof *ofr);
1817 ofputil_cls_rule_from_ofp10_match(&ofr->match, ntohs(ofr->priority),
1819 fr->cookie = ofr->cookie;
1820 fr->reason = ofr->reason;
1821 fr->duration_sec = ntohl(ofr->duration_sec);
1822 fr->duration_nsec = ntohl(ofr->duration_nsec);
1823 fr->idle_timeout = ntohs(ofr->idle_timeout);
1824 fr->packet_count = ntohll(ofr->packet_count);
1825 fr->byte_count = ntohll(ofr->byte_count);
1826 } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
1827 struct nx_flow_removed *nfr;
1830 nfr = ofpbuf_pull(&b, sizeof *nfr);
1831 error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
1832 &fr->rule, NULL, NULL);
1837 return OFPERR_OFPBRC_BAD_LEN;
1840 fr->cookie = nfr->cookie;
1841 fr->reason = nfr->reason;
1842 fr->duration_sec = ntohl(nfr->duration_sec);
1843 fr->duration_nsec = ntohl(nfr->duration_nsec);
1844 fr->idle_timeout = ntohs(nfr->idle_timeout);
1845 fr->packet_count = ntohll(nfr->packet_count);
1846 fr->byte_count = ntohll(nfr->byte_count);
1854 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
1855 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
1858 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
1859 enum ofputil_protocol protocol)
1864 case OFPUTIL_P_OF10:
1865 case OFPUTIL_P_OF10_TID: {
1866 struct ofp_flow_removed *ofr;
1868 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
1870 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
1871 ofputil_cls_rule_to_ofp10_match(&fr->rule, &ofr->match);
1872 ofr->cookie = fr->cookie;
1873 ofr->priority = htons(fr->rule.priority);
1874 ofr->reason = fr->reason;
1875 ofr->duration_sec = htonl(fr->duration_sec);
1876 ofr->duration_nsec = htonl(fr->duration_nsec);
1877 ofr->idle_timeout = htons(fr->idle_timeout);
1878 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
1879 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
1884 case OFPUTIL_P_NXM_TID: {
1885 struct nx_flow_removed *nfr;
1888 msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
1889 htonl(0), NXM_TYPICAL_LEN);
1890 nfr = ofpbuf_put_zeros(msg, sizeof *nfr);
1891 match_len = nx_put_match(msg, &fr->rule, 0, 0);
1894 nfr->cookie = fr->cookie;
1895 nfr->priority = htons(fr->rule.priority);
1896 nfr->reason = fr->reason;
1897 nfr->duration_sec = htonl(fr->duration_sec);
1898 nfr->duration_nsec = htonl(fr->duration_nsec);
1899 nfr->idle_timeout = htons(fr->idle_timeout);
1900 nfr->match_len = htons(match_len);
1901 nfr->packet_count = htonll(fr->packet_count);
1902 nfr->byte_count = htonll(fr->byte_count);
1906 case OFPUTIL_P_OF12:
1915 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
1916 const struct ofp_header *oh)
1921 memset(pin, 0, sizeof *pin);
1923 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1924 raw = ofpraw_pull_assert(&b);
1925 if (raw == OFPRAW_OFPT10_PACKET_IN) {
1926 const struct ofp_packet_in *opi;
1928 opi = ofpbuf_pull(&b, offsetof(struct ofp_packet_in, data));
1930 pin->packet = opi->data;
1931 pin->packet_len = b.size;
1933 pin->fmd.in_port = ntohs(opi->in_port);
1934 pin->reason = opi->reason;
1935 pin->buffer_id = ntohl(opi->buffer_id);
1936 pin->total_len = ntohs(opi->total_len);
1937 } else if (raw == OFPRAW_NXT_PACKET_IN) {
1938 const struct nx_packet_in *npi;
1939 struct cls_rule rule;
1942 npi = ofpbuf_pull(&b, sizeof *npi);
1943 error = nx_pull_match_loose(&b, ntohs(npi->match_len), 0, &rule, NULL,
1949 if (!ofpbuf_try_pull(&b, 2)) {
1950 return OFPERR_OFPBRC_BAD_LEN;
1953 pin->packet = b.data;
1954 pin->packet_len = b.size;
1955 pin->reason = npi->reason;
1956 pin->table_id = npi->table_id;
1957 pin->cookie = npi->cookie;
1959 pin->fmd.in_port = rule.flow.in_port;
1961 pin->fmd.tun_id = rule.flow.tun_id;
1962 pin->fmd.tun_id_mask = rule.wc.tun_id_mask;
1964 pin->fmd.metadata = rule.flow.metadata;
1965 pin->fmd.metadata_mask = rule.wc.metadata_mask;
1967 memcpy(pin->fmd.regs, rule.flow.regs, sizeof pin->fmd.regs);
1968 memcpy(pin->fmd.reg_masks, rule.wc.reg_masks,
1969 sizeof pin->fmd.reg_masks);
1971 pin->buffer_id = ntohl(npi->buffer_id);
1972 pin->total_len = ntohs(npi->total_len);
1980 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
1981 * in the format specified by 'packet_in_format'. */
1983 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
1984 enum nx_packet_in_format packet_in_format)
1986 size_t send_len = MIN(pin->send_len, pin->packet_len);
1987 struct ofpbuf *packet;
1989 /* Add OFPT_PACKET_IN. */
1990 if (packet_in_format == NXPIF_OPENFLOW10) {
1991 struct ofp_packet_in *opi;
1993 packet = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
1994 htonl(0), send_len);
1995 opi = ofpbuf_put_zeros(packet, offsetof(struct ofp_packet_in, data));
1996 opi->total_len = htons(pin->total_len);
1997 opi->in_port = htons(pin->fmd.in_port);
1998 opi->reason = pin->reason;
1999 opi->buffer_id = htonl(pin->buffer_id);
2001 ofpbuf_put(packet, pin->packet, send_len);
2002 } else if (packet_in_format == NXPIF_NXM) {
2003 struct nx_packet_in *npi;
2004 struct cls_rule rule;
2008 cls_rule_init_catchall(&rule, 0);
2009 cls_rule_set_tun_id_masked(&rule, pin->fmd.tun_id,
2010 pin->fmd.tun_id_mask);
2011 cls_rule_set_metadata_masked(&rule, pin->fmd.metadata,
2012 pin->fmd.metadata_mask);
2015 for (i = 0; i < FLOW_N_REGS; i++) {
2016 cls_rule_set_reg_masked(&rule, i, pin->fmd.regs[i],
2017 pin->fmd.reg_masks[i]);
2020 cls_rule_set_in_port(&rule, pin->fmd.in_port);
2022 /* The final argument is just an estimate of the space required. */
2023 packet = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, OFP10_VERSION,
2024 htonl(0), (sizeof(struct flow_metadata) * 2
2026 ofpbuf_put_zeros(packet, sizeof *npi);
2027 match_len = nx_put_match(packet, &rule, 0, 0);
2028 ofpbuf_put_zeros(packet, 2);
2029 ofpbuf_put(packet, pin->packet, send_len);
2032 npi->buffer_id = htonl(pin->buffer_id);
2033 npi->total_len = htons(pin->total_len);
2034 npi->reason = pin->reason;
2035 npi->table_id = pin->table_id;
2036 npi->cookie = pin->cookie;
2037 npi->match_len = htons(match_len);
2041 ofpmsg_update_length(packet);
2047 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason)
2049 static char s[INT_STRLEN(int) + 1];
2056 case OFPR_INVALID_TTL:
2057 return "invalid_ttl";
2059 case OFPR_N_REASONS:
2061 sprintf(s, "%d", (int) reason);
2067 ofputil_packet_in_reason_from_string(const char *s,
2068 enum ofp_packet_in_reason *reason)
2072 for (i = 0; i < OFPR_N_REASONS; i++) {
2073 if (!strcasecmp(s, ofputil_packet_in_reason_to_string(i))) {
2081 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
2084 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
2085 * message's actions. The caller must initialize 'ofpacts' and retains
2086 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
2088 * Returns 0 if successful, otherwise an OFPERR_* value. */
2090 ofputil_decode_packet_out(struct ofputil_packet_out *po,
2091 const struct ofp_header *oh,
2092 struct ofpbuf *ofpacts)
2094 const struct ofp_packet_out *opo;
2099 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2100 raw = ofpraw_pull_assert(&b);
2101 assert(raw == OFPRAW_OFPT10_PACKET_OUT);
2103 opo = ofpbuf_pull(&b, sizeof *opo);
2104 po->buffer_id = ntohl(opo->buffer_id);
2105 po->in_port = ntohs(opo->in_port);
2106 if (po->in_port >= OFPP_MAX && po->in_port != OFPP_LOCAL
2107 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
2108 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
2110 return OFPERR_NXBRC_BAD_IN_PORT;
2113 error = ofpacts_pull_openflow10(&b, ntohs(opo->actions_len), ofpacts);
2117 po->ofpacts = ofpacts->data;
2118 po->ofpacts_len = ofpacts->size;
2120 if (po->buffer_id == UINT32_MAX) {
2121 po->packet = b.data;
2122 po->packet_len = b.size;
2131 /* ofputil_phy_port */
2133 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
2134 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2135 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2136 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2137 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2138 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2139 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2140 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2142 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
2143 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
2144 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
2145 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
2146 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
2147 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
2149 static enum netdev_features
2150 netdev_port_features_from_ofp10(ovs_be32 ofp10_)
2152 uint32_t ofp10 = ntohl(ofp10_);
2153 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
2157 netdev_port_features_to_ofp10(enum netdev_features features)
2159 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
2162 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2163 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2164 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2165 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2166 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2167 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2168 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2169 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
2170 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
2171 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
2172 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
2173 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
2174 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
2175 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
2176 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
2177 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
2179 static enum netdev_features
2180 netdev_port_features_from_ofp11(ovs_be32 ofp11)
2182 return ntohl(ofp11) & 0xffff;
2186 netdev_port_features_to_ofp11(enum netdev_features features)
2188 return htonl(features & 0xffff);
2192 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
2193 const struct ofp10_phy_port *opp)
2195 memset(pp, 0, sizeof *pp);
2197 pp->port_no = ntohs(opp->port_no);
2198 memcpy(pp->hw_addr, opp->hw_addr, OFP_ETH_ALEN);
2199 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
2201 pp->config = ntohl(opp->config) & OFPPC10_ALL;
2202 pp->state = ntohl(opp->state) & OFPPS10_ALL;
2204 pp->curr = netdev_port_features_from_ofp10(opp->curr);
2205 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
2206 pp->supported = netdev_port_features_from_ofp10(opp->supported);
2207 pp->peer = netdev_port_features_from_ofp10(opp->peer);
2209 pp->curr_speed = netdev_features_to_bps(pp->curr) / 1000;
2210 pp->max_speed = netdev_features_to_bps(pp->supported) / 1000;
2216 ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
2217 const struct ofp11_port *op)
2221 memset(pp, 0, sizeof *pp);
2223 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
2227 memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN);
2228 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
2230 pp->config = ntohl(op->config) & OFPPC11_ALL;
2231 pp->state = ntohl(op->state) & OFPPC11_ALL;
2233 pp->curr = netdev_port_features_from_ofp11(op->curr);
2234 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
2235 pp->supported = netdev_port_features_from_ofp11(op->supported);
2236 pp->peer = netdev_port_features_from_ofp11(op->peer);
2238 pp->curr_speed = ntohl(op->curr_speed);
2239 pp->max_speed = ntohl(op->max_speed);
2245 ofputil_get_phy_port_size(enum ofp_version ofp_version)
2247 switch (ofp_version) {
2249 return sizeof(struct ofp10_phy_port);
2252 return sizeof(struct ofp11_port);
2259 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
2260 struct ofp10_phy_port *opp)
2262 memset(opp, 0, sizeof *opp);
2264 opp->port_no = htons(pp->port_no);
2265 memcpy(opp->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2266 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2268 opp->config = htonl(pp->config & OFPPC10_ALL);
2269 opp->state = htonl(pp->state & OFPPS10_ALL);
2271 opp->curr = netdev_port_features_to_ofp10(pp->curr);
2272 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
2273 opp->supported = netdev_port_features_to_ofp10(pp->supported);
2274 opp->peer = netdev_port_features_to_ofp10(pp->peer);
2278 ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
2279 struct ofp11_port *op)
2281 memset(op, 0, sizeof *op);
2283 op->port_no = ofputil_port_to_ofp11(pp->port_no);
2284 memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2285 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2287 op->config = htonl(pp->config & OFPPC11_ALL);
2288 op->state = htonl(pp->state & OFPPS11_ALL);
2290 op->curr = netdev_port_features_to_ofp11(pp->curr);
2291 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
2292 op->supported = netdev_port_features_to_ofp11(pp->supported);
2293 op->peer = netdev_port_features_to_ofp11(pp->peer);
2295 op->curr_speed = htonl(pp->curr_speed);
2296 op->max_speed = htonl(pp->max_speed);
2300 ofputil_put_phy_port(enum ofp_version ofp_version,
2301 const struct ofputil_phy_port *pp, struct ofpbuf *b)
2303 switch (ofp_version) {
2304 case OFP10_VERSION: {
2305 struct ofp10_phy_port *opp;
2306 if (b->size + sizeof *opp <= UINT16_MAX) {
2307 opp = ofpbuf_put_uninit(b, sizeof *opp);
2308 ofputil_encode_ofp10_phy_port(pp, opp);
2314 case OFP12_VERSION: {
2315 struct ofp11_port *op;
2316 if (b->size + sizeof *op <= UINT16_MAX) {
2317 op = ofpbuf_put_uninit(b, sizeof *op);
2318 ofputil_encode_ofp11_port(pp, op);
2329 ofputil_append_port_desc_stats_reply(enum ofp_version ofp_version,
2330 const struct ofputil_phy_port *pp,
2331 struct list *replies)
2333 switch (ofp_version) {
2334 case OFP10_VERSION: {
2335 struct ofp10_phy_port *opp;
2337 opp = ofpmp_append(replies, sizeof *opp);
2338 ofputil_encode_ofp10_phy_port(pp, opp);
2343 case OFP12_VERSION: {
2344 struct ofp11_port *op;
2346 op = ofpmp_append(replies, sizeof *op);
2347 ofputil_encode_ofp11_port(pp, op);
2356 /* ofputil_switch_features */
2358 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
2359 OFPC_IP_REASM | OFPC_QUEUE_STATS)
2360 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
2361 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
2362 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
2363 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
2364 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
2365 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
2367 struct ofputil_action_bit_translation {
2368 enum ofputil_action_bitmap ofputil_bit;
2372 static const struct ofputil_action_bit_translation of10_action_bits[] = {
2373 { OFPUTIL_A_OUTPUT, OFPAT10_OUTPUT },
2374 { OFPUTIL_A_SET_VLAN_VID, OFPAT10_SET_VLAN_VID },
2375 { OFPUTIL_A_SET_VLAN_PCP, OFPAT10_SET_VLAN_PCP },
2376 { OFPUTIL_A_STRIP_VLAN, OFPAT10_STRIP_VLAN },
2377 { OFPUTIL_A_SET_DL_SRC, OFPAT10_SET_DL_SRC },
2378 { OFPUTIL_A_SET_DL_DST, OFPAT10_SET_DL_DST },
2379 { OFPUTIL_A_SET_NW_SRC, OFPAT10_SET_NW_SRC },
2380 { OFPUTIL_A_SET_NW_DST, OFPAT10_SET_NW_DST },
2381 { OFPUTIL_A_SET_NW_TOS, OFPAT10_SET_NW_TOS },
2382 { OFPUTIL_A_SET_TP_SRC, OFPAT10_SET_TP_SRC },
2383 { OFPUTIL_A_SET_TP_DST, OFPAT10_SET_TP_DST },
2384 { OFPUTIL_A_ENQUEUE, OFPAT10_ENQUEUE },
2388 static enum ofputil_action_bitmap
2389 decode_action_bits(ovs_be32 of_actions,
2390 const struct ofputil_action_bit_translation *x)
2392 enum ofputil_action_bitmap ofputil_actions;
2394 ofputil_actions = 0;
2395 for (; x->ofputil_bit; x++) {
2396 if (of_actions & htonl(1u << x->of_bit)) {
2397 ofputil_actions |= x->ofputil_bit;
2400 return ofputil_actions;
2404 ofputil_capabilities_mask(enum ofp_version ofp_version)
2406 /* Handle capabilities whose bit is unique for all Open Flow versions */
2407 switch (ofp_version) {
2410 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
2412 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
2414 /* Caller needs to check osf->header.version itself */
2419 /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an
2420 * abstract representation in '*features'. Initializes '*b' to iterate over
2421 * the OpenFlow port structures following 'osf' with later calls to
2422 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an
2423 * OFPERR_* value. */
2425 ofputil_decode_switch_features(const struct ofp_header *oh,
2426 struct ofputil_switch_features *features,
2429 const struct ofp_switch_features *osf;
2432 ofpbuf_use_const(b, oh, ntohs(oh->length));
2433 raw = ofpraw_pull_assert(b);
2435 osf = ofpbuf_pull(b, sizeof *osf);
2436 features->datapath_id = ntohll(osf->datapath_id);
2437 features->n_buffers = ntohl(osf->n_buffers);
2438 features->n_tables = osf->n_tables;
2440 features->capabilities = ntohl(osf->capabilities) &
2441 ofputil_capabilities_mask(oh->version);
2443 if (b->size % ofputil_get_phy_port_size(oh->version)) {
2444 return OFPERR_OFPBRC_BAD_LEN;
2447 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
2448 if (osf->capabilities & htonl(OFPC10_STP)) {
2449 features->capabilities |= OFPUTIL_C_STP;
2451 features->actions = decode_action_bits(osf->actions, of10_action_bits);
2452 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY) {
2453 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
2454 features->capabilities |= OFPUTIL_C_GROUP_STATS;
2456 features->actions = 0;
2458 return OFPERR_OFPBRC_BAD_VERSION;
2464 /* Returns true if the maximum number of ports are in 'oh'. */
2466 max_ports_in_features(const struct ofp_header *oh)
2468 size_t pp_size = ofputil_get_phy_port_size(oh->version);
2469 return ntohs(oh->length) + pp_size > UINT16_MAX;
2472 /* Given a buffer 'b' that contains a Features Reply message, checks if
2473 * it contains the maximum number of ports that will fit. If so, it
2474 * returns true and removes the ports from the message. The caller
2475 * should then send an OFPST_PORT_DESC stats request to get the ports,
2476 * since the switch may have more ports than could be represented in the
2477 * Features Reply. Otherwise, returns false.
2480 ofputil_switch_features_ports_trunc(struct ofpbuf *b)
2482 struct ofp_header *oh = b->data;
2484 if (max_ports_in_features(oh)) {
2485 /* Remove all the ports. */
2486 b->size = (sizeof(struct ofp_header)
2487 + sizeof(struct ofp_switch_features));
2488 ofpmsg_update_length(b);
2497 encode_action_bits(enum ofputil_action_bitmap ofputil_actions,
2498 const struct ofputil_action_bit_translation *x)
2500 uint32_t of_actions;
2503 for (; x->ofputil_bit; x++) {
2504 if (ofputil_actions & x->ofputil_bit) {
2505 of_actions |= 1 << x->of_bit;
2508 return htonl(of_actions);
2511 /* Returns a buffer owned by the caller that encodes 'features' in the format
2512 * required by 'protocol' with the given 'xid'. The caller should append port
2513 * information to the buffer with subsequent calls to
2514 * ofputil_put_switch_features_port(). */
2516 ofputil_encode_switch_features(const struct ofputil_switch_features *features,
2517 enum ofputil_protocol protocol, ovs_be32 xid)
2519 struct ofp_switch_features *osf;
2521 enum ofp_version version;
2524 version = ofputil_protocol_to_ofp_version(protocol);
2527 raw = OFPRAW_OFPT10_FEATURES_REPLY;
2531 raw = OFPRAW_OFPT11_FEATURES_REPLY;
2536 b = ofpraw_alloc_xid(raw, version, xid, 0);
2537 osf = ofpbuf_put_zeros(b, sizeof *osf);
2538 osf->datapath_id = htonll(features->datapath_id);
2539 osf->n_buffers = htonl(features->n_buffers);
2540 osf->n_tables = features->n_tables;
2542 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
2543 osf->capabilities = htonl(features->capabilities &
2544 ofputil_capabilities_mask(version));
2547 if (features->capabilities & OFPUTIL_C_STP) {
2548 osf->capabilities |= htonl(OFPC10_STP);
2550 osf->actions = encode_action_bits(features->actions, of10_action_bits);
2554 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
2555 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
2565 /* Encodes 'pp' into the format required by the switch_features message already
2566 * in 'b', which should have been returned by ofputil_encode_switch_features(),
2567 * and appends the encoded version to 'b'. */
2569 ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
2572 const struct ofp_header *oh = b->data;
2574 ofputil_put_phy_port(oh->version, pp, b);
2577 /* ofputil_port_status */
2579 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
2580 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
2582 ofputil_decode_port_status(const struct ofp_header *oh,
2583 struct ofputil_port_status *ps)
2585 const struct ofp_port_status *ops;
2589 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2590 ofpraw_pull_assert(&b);
2591 ops = ofpbuf_pull(&b, sizeof *ops);
2593 if (ops->reason != OFPPR_ADD &&
2594 ops->reason != OFPPR_DELETE &&
2595 ops->reason != OFPPR_MODIFY) {
2596 return OFPERR_NXBRC_BAD_REASON;
2598 ps->reason = ops->reason;
2600 retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
2601 assert(retval != EOF);
2605 /* Converts the abstract form of a "port status" message in '*ps' into an
2606 * OpenFlow message suitable for 'protocol', and returns that encoded form in
2607 * a buffer owned by the caller. */
2609 ofputil_encode_port_status(const struct ofputil_port_status *ps,
2610 enum ofputil_protocol protocol)
2612 struct ofp_port_status *ops;
2614 enum ofp_version version;
2617 version = ofputil_protocol_to_ofp_version(protocol);
2620 raw = OFPRAW_OFPT10_PORT_STATUS;
2625 raw = OFPRAW_OFPT11_PORT_STATUS;
2632 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
2633 ops = ofpbuf_put_zeros(b, sizeof *ops);
2634 ops->reason = ps->reason;
2635 ofputil_put_phy_port(version, &ps->desc, b);
2636 ofpmsg_update_length(b);
2640 /* ofputil_port_mod */
2642 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
2643 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
2645 ofputil_decode_port_mod(const struct ofp_header *oh,
2646 struct ofputil_port_mod *pm)
2651 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2652 raw = ofpraw_pull_assert(&b);
2654 if (raw == OFPRAW_OFPT10_PORT_MOD) {
2655 const struct ofp10_port_mod *opm = b.data;
2657 pm->port_no = ntohs(opm->port_no);
2658 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
2659 pm->config = ntohl(opm->config) & OFPPC10_ALL;
2660 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
2661 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
2662 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
2663 const struct ofp11_port_mod *opm = b.data;
2666 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
2671 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
2672 pm->config = ntohl(opm->config) & OFPPC11_ALL;
2673 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
2674 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
2676 return OFPERR_OFPBRC_BAD_TYPE;
2679 pm->config &= pm->mask;
2683 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
2684 * message suitable for 'protocol', and returns that encoded form in a buffer
2685 * owned by the caller. */
2687 ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
2688 enum ofputil_protocol protocol)
2690 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
2693 switch (ofp_version) {
2694 case OFP10_VERSION: {
2695 struct ofp10_port_mod *opm;
2697 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
2698 opm = ofpbuf_put_zeros(b, sizeof *opm);
2699 opm->port_no = htons(pm->port_no);
2700 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
2701 opm->config = htonl(pm->config & OFPPC10_ALL);
2702 opm->mask = htonl(pm->mask & OFPPC10_ALL);
2703 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
2707 case OFP11_VERSION: {
2708 struct ofp11_port_mod *opm;
2710 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
2711 opm = ofpbuf_put_zeros(b, sizeof *opm);
2712 opm->port_no = htonl(pm->port_no);
2713 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
2714 opm->config = htonl(pm->config & OFPPC11_ALL);
2715 opm->mask = htonl(pm->mask & OFPPC11_ALL);
2716 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
2728 /* ofputil_flow_monitor_request */
2730 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
2731 * ofputil_flow_monitor_request in 'rq'.
2733 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
2734 * message. Calling this function multiple times for a single 'msg' iterates
2735 * through the requests. The caller must initially leave 'msg''s layer
2736 * pointers null and not modify them between calls.
2738 * Returns 0 if successful, EOF if no requests were left in this 'msg',
2739 * otherwise an OFPERR_* value. */
2741 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
2744 struct nx_flow_monitor_request *nfmr;
2748 msg->l2 = msg->data;
2749 ofpraw_pull_assert(msg);
2756 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
2758 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %zu "
2759 "leftover bytes at end", msg->size);
2760 return OFPERR_OFPBRC_BAD_LEN;
2763 flags = ntohs(nfmr->flags);
2764 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
2765 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
2766 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
2767 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
2769 return OFPERR_NXBRC_FM_BAD_FLAGS;
2772 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
2773 return OFPERR_NXBRC_MUST_BE_ZERO;
2776 rq->id = ntohl(nfmr->id);
2778 rq->out_port = ntohs(nfmr->out_port);
2779 rq->table_id = nfmr->table_id;
2781 return nx_pull_match(msg, ntohs(nfmr->match_len), OFP_DEFAULT_PRIORITY,
2782 &rq->match, NULL, NULL);
2786 ofputil_append_flow_monitor_request(
2787 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
2789 struct nx_flow_monitor_request *nfmr;
2794 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
2797 start_ofs = msg->size;
2798 ofpbuf_put_zeros(msg, sizeof *nfmr);
2799 match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
2801 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
2802 nfmr->id = htonl(rq->id);
2803 nfmr->flags = htons(rq->flags);
2804 nfmr->out_port = htons(rq->out_port);
2805 nfmr->match_len = htons(match_len);
2806 nfmr->table_id = rq->table_id;
2809 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
2810 * into an abstract ofputil_flow_update in 'update'. The caller must have
2811 * initialized update->match to point to space allocated for a cls_rule.
2813 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
2814 * actions (except for NXFME_ABBREV, which never includes actions). The caller
2815 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
2816 * will point into the 'ofpacts' buffer.
2818 * Multiple flow updates can be packed into a single OpenFlow message. Calling
2819 * this function multiple times for a single 'msg' iterates through the
2820 * updates. The caller must initially leave 'msg''s layer pointers null and
2821 * not modify them between calls.
2823 * Returns 0 if successful, EOF if no updates were left in this 'msg',
2824 * otherwise an OFPERR_* value. */
2826 ofputil_decode_flow_update(struct ofputil_flow_update *update,
2827 struct ofpbuf *msg, struct ofpbuf *ofpacts)
2829 struct nx_flow_update_header *nfuh;
2830 unsigned int length;
2833 msg->l2 = msg->data;
2834 ofpraw_pull_assert(msg);
2841 if (msg->size < sizeof(struct nx_flow_update_header)) {
2846 update->event = ntohs(nfuh->event);
2847 length = ntohs(nfuh->length);
2848 if (length > msg->size || length % 8) {
2852 if (update->event == NXFME_ABBREV) {
2853 struct nx_flow_update_abbrev *nfua;
2855 if (length != sizeof *nfua) {
2859 nfua = ofpbuf_pull(msg, sizeof *nfua);
2860 update->xid = nfua->xid;
2862 } else if (update->event == NXFME_ADDED
2863 || update->event == NXFME_DELETED
2864 || update->event == NXFME_MODIFIED) {
2865 struct nx_flow_update_full *nfuf;
2866 unsigned int actions_len;
2867 unsigned int match_len;
2870 if (length < sizeof *nfuf) {
2874 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
2875 match_len = ntohs(nfuf->match_len);
2876 if (sizeof *nfuf + match_len > length) {
2880 update->reason = ntohs(nfuf->reason);
2881 update->idle_timeout = ntohs(nfuf->idle_timeout);
2882 update->hard_timeout = ntohs(nfuf->hard_timeout);
2883 update->table_id = nfuf->table_id;
2884 update->cookie = nfuf->cookie;
2886 error = nx_pull_match(msg, match_len, ntohs(nfuf->priority),
2887 update->match, NULL, NULL);
2892 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
2893 error = ofpacts_pull_openflow10(msg, actions_len, ofpacts);
2898 update->ofpacts = ofpacts->data;
2899 update->ofpacts_len = ofpacts->size;
2902 VLOG_WARN_RL(&bad_ofmsg_rl,
2903 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
2904 ntohs(nfuh->event));
2905 return OFPERR_OFPET_BAD_REQUEST;
2909 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %zu "
2910 "leftover bytes at end", msg->size);
2911 return OFPERR_OFPBRC_BAD_LEN;
2915 ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
2917 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
2919 return ntohl(cancel->id);
2923 ofputil_encode_flow_monitor_cancel(uint32_t id)
2925 struct nx_flow_monitor_cancel *nfmc;
2928 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
2929 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
2930 nfmc->id = htonl(id);
2935 ofputil_start_flow_update(struct list *replies)
2939 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
2943 list_push_back(replies, &msg->list_node);
2947 ofputil_append_flow_update(const struct ofputil_flow_update *update,
2948 struct list *replies)
2950 struct nx_flow_update_header *nfuh;
2954 msg = ofpbuf_from_list(list_back(replies));
2955 start_ofs = msg->size;
2957 if (update->event == NXFME_ABBREV) {
2958 struct nx_flow_update_abbrev *nfua;
2960 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
2961 nfua->xid = update->xid;
2963 struct nx_flow_update_full *nfuf;
2966 ofpbuf_put_zeros(msg, sizeof *nfuf);
2967 match_len = nx_put_match(msg, update->match, htonll(0), htonll(0));
2968 ofpacts_put_openflow10(update->ofpacts, update->ofpacts_len, msg);
2970 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
2971 nfuf->reason = htons(update->reason);
2972 nfuf->priority = htons(update->match->priority);
2973 nfuf->idle_timeout = htons(update->idle_timeout);
2974 nfuf->hard_timeout = htons(update->hard_timeout);
2975 nfuf->match_len = htons(match_len);
2976 nfuf->table_id = update->table_id;
2977 nfuf->cookie = update->cookie;
2980 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
2981 nfuh->length = htons(msg->size - start_ofs);
2982 nfuh->event = htons(update->event);
2984 ofpmp_postappend(replies, start_ofs);
2988 ofputil_encode_packet_out(const struct ofputil_packet_out *po)
2990 struct ofp_packet_out *opo;
2995 size = po->ofpacts_len;
2996 if (po->buffer_id == UINT32_MAX) {
2997 size += po->packet_len;
3000 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
3001 ofpbuf_put_zeros(msg, sizeof *opo);
3002 actions_ofs = msg->size;
3003 ofpacts_put_openflow10(po->ofpacts, po->ofpacts_len, msg);
3006 opo->buffer_id = htonl(po->buffer_id);
3007 opo->in_port = htons(po->in_port);
3008 opo->actions_len = htons(msg->size - actions_ofs);
3010 if (po->buffer_id == UINT32_MAX) {
3011 ofpbuf_put(msg, po->packet, po->packet_len);
3014 ofpmsg_update_length(msg);
3019 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
3021 make_echo_request(enum ofp_version ofp_version)
3023 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
3027 /* Creates and returns an OFPT_ECHO_REPLY message matching the
3028 * OFPT_ECHO_REQUEST message in 'rq'. */
3030 make_echo_reply(const struct ofp_header *rq)
3032 struct ofpbuf rq_buf;
3033 struct ofpbuf *reply;
3035 ofpbuf_use_const(&rq_buf, rq, ntohs(rq->length));
3036 ofpraw_pull_assert(&rq_buf);
3038 reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY, rq, rq_buf.size);
3039 ofpbuf_put(reply, rq_buf.data, rq_buf.size);
3044 ofputil_encode_barrier_request(enum ofp_version ofp_version)
3048 switch (ofp_version) {
3051 type = OFPRAW_OFPT11_BARRIER_REQUEST;
3055 type = OFPRAW_OFPT10_BARRIER_REQUEST;
3062 return ofpraw_alloc(type, ofp_version, 0);
3066 ofputil_frag_handling_to_string(enum ofp_config_flags flags)
3068 switch (flags & OFPC_FRAG_MASK) {
3069 case OFPC_FRAG_NORMAL: return "normal";
3070 case OFPC_FRAG_DROP: return "drop";
3071 case OFPC_FRAG_REASM: return "reassemble";
3072 case OFPC_FRAG_NX_MATCH: return "nx-match";
3079 ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
3081 if (!strcasecmp(s, "normal")) {
3082 *flags = OFPC_FRAG_NORMAL;
3083 } else if (!strcasecmp(s, "drop")) {
3084 *flags = OFPC_FRAG_DROP;
3085 } else if (!strcasecmp(s, "reassemble")) {
3086 *flags = OFPC_FRAG_REASM;
3087 } else if (!strcasecmp(s, "nx-match")) {
3088 *flags = OFPC_FRAG_NX_MATCH;
3095 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
3096 * port number and stores the latter in '*ofp10_port', for the purpose of
3097 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
3098 * otherwise an OFPERR_* number.
3100 * See the definition of OFP11_MAX for an explanation of the mapping. */
3102 ofputil_port_from_ofp11(ovs_be32 ofp11_port, uint16_t *ofp10_port)
3104 uint32_t ofp11_port_h = ntohl(ofp11_port);
3106 if (ofp11_port_h < OFPP_MAX) {
3107 *ofp10_port = ofp11_port_h;
3109 } else if (ofp11_port_h >= OFPP11_MAX) {
3110 *ofp10_port = ofp11_port_h - OFPP11_OFFSET;
3113 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
3114 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
3115 ofp11_port_h, OFPP_MAX - 1,
3116 (uint32_t) OFPP11_MAX, UINT32_MAX);
3117 return OFPERR_OFPBAC_BAD_OUT_PORT;
3121 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
3122 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
3124 * See the definition of OFP11_MAX for an explanation of the mapping. */
3126 ofputil_port_to_ofp11(uint16_t ofp10_port)
3128 return htonl(ofp10_port < OFPP_MAX
3130 : ofp10_port + OFPP11_OFFSET);
3133 /* Checks that 'port' is a valid output port for the OFPAT10_OUTPUT action, given
3134 * that the switch will never have more than 'max_ports' ports. Returns 0 if
3135 * 'port' is valid, otherwise an OpenFlow return code. */
3137 ofputil_check_output_port(uint16_t port, int max_ports)
3145 case OFPP_CONTROLLER:
3151 if (port < max_ports) {
3154 return OFPERR_OFPBAC_BAD_OUT_PORT;
3158 #define OFPUTIL_NAMED_PORTS \
3159 OFPUTIL_NAMED_PORT(IN_PORT) \
3160 OFPUTIL_NAMED_PORT(TABLE) \
3161 OFPUTIL_NAMED_PORT(NORMAL) \
3162 OFPUTIL_NAMED_PORT(FLOOD) \
3163 OFPUTIL_NAMED_PORT(ALL) \
3164 OFPUTIL_NAMED_PORT(CONTROLLER) \
3165 OFPUTIL_NAMED_PORT(LOCAL) \
3166 OFPUTIL_NAMED_PORT(NONE)
3168 /* Checks whether 's' is the string representation of an OpenFlow port number,
3169 * either as an integer or a string name (e.g. "LOCAL"). If it is, stores the
3170 * number in '*port' and returns true. Otherwise, returns false. */
3172 ofputil_port_from_string(const char *name, uint16_t *port)
3178 static const struct pair pairs[] = {
3179 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
3181 #undef OFPUTIL_NAMED_PORT
3183 static const int n_pairs = ARRAY_SIZE(pairs);
3186 if (str_to_int(name, 0, &i) && i >= 0 && i < UINT16_MAX) {
3191 for (i = 0; i < n_pairs; i++) {
3192 if (!strcasecmp(name, pairs[i].name)) {
3193 *port = pairs[i].value;
3200 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
3201 * Most ports' string representation is just the port number, but for special
3202 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
3204 ofputil_format_port(uint16_t port, struct ds *s)
3209 #define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break;
3211 #undef OFPUTIL_NAMED_PORT
3214 ds_put_format(s, "%"PRIu16, port);
3217 ds_put_cstr(s, name);
3220 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
3221 * 'ofp_version', tries to pull the first element from the array. If
3222 * successful, initializes '*pp' with an abstract representation of the
3223 * port and returns 0. If no ports remain to be decoded, returns EOF.
3224 * On an error, returns a positive OFPERR_* value. */
3226 ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
3227 struct ofputil_phy_port *pp)
3229 switch (ofp_version) {
3230 case OFP10_VERSION: {
3231 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
3232 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
3235 case OFP12_VERSION: {
3236 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
3237 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
3244 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
3245 * 'ofp_version', returns the number of elements. */
3246 size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *b)
3248 return b->size / ofputil_get_phy_port_size(ofp_version);
3251 /* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if
3252 * 'name' is "output" then the return value is OFPUTIL_OFPAT10_OUTPUT), or -1 if
3253 * 'name' is not the name of any action.
3255 * ofp-util.def lists the mapping from names to action. */
3257 ofputil_action_code_from_name(const char *name)
3259 static const char *names[OFPUTIL_N_ACTIONS] = {
3261 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) NAME,
3262 #define OFPAT11_ACTION(ENUM, STRUCT, NAME) NAME,
3263 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
3264 #include "ofp-util.def"
3269 for (p = names; p < &names[ARRAY_SIZE(names)]; p++) {
3270 if (*p && !strcasecmp(name, *p)) {
3277 /* Appends an action of the type specified by 'code' to 'buf' and returns the
3278 * action. Initializes the parts of 'action' that identify it as having type
3279 * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that
3280 * have variable length, the length used and cleared is that of struct
3283 ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
3286 case OFPUTIL_ACTION_INVALID:
3289 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
3290 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
3291 #define OFPAT11_ACTION OFPAT10_ACTION
3292 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3293 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
3294 #include "ofp-util.def"
3299 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
3301 ofputil_init_##ENUM(struct STRUCT *s) \
3303 memset(s, 0, sizeof *s); \
3304 s->type = htons(ENUM); \
3305 s->len = htons(sizeof *s); \
3309 ofputil_put_##ENUM(struct ofpbuf *buf) \
3311 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
3312 ofputil_init_##ENUM(s); \
3315 #define OFPAT11_ACTION OFPAT10_ACTION
3316 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3318 ofputil_init_##ENUM(struct STRUCT *s) \
3320 memset(s, 0, sizeof *s); \
3321 s->type = htons(OFPAT10_VENDOR); \
3322 s->len = htons(sizeof *s); \
3323 s->vendor = htonl(NX_VENDOR_ID); \
3324 s->subtype = htons(ENUM); \
3328 ofputil_put_##ENUM(struct ofpbuf *buf) \
3330 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
3331 ofputil_init_##ENUM(s); \
3334 #include "ofp-util.def"
3337 ofputil_normalize_rule__(struct cls_rule *rule, bool may_log)
3340 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
3341 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
3342 MAY_NW_PROTO = 1 << 2, /* nw_proto */
3343 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
3344 MAY_ARP_SHA = 1 << 4, /* arp_sha */
3345 MAY_ARP_THA = 1 << 5, /* arp_tha */
3346 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
3347 MAY_ND_TARGET = 1 << 7 /* nd_target */
3350 struct flow_wildcards wc;
3352 /* Figure out what fields may be matched. */
3353 if (rule->flow.dl_type == htons(ETH_TYPE_IP)) {
3354 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
3355 if (rule->flow.nw_proto == IPPROTO_TCP ||
3356 rule->flow.nw_proto == IPPROTO_UDP ||
3357 rule->flow.nw_proto == IPPROTO_ICMP) {
3358 may_match |= MAY_TP_ADDR;
3360 } else if (rule->flow.dl_type == htons(ETH_TYPE_IPV6)) {
3361 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
3362 if (rule->flow.nw_proto == IPPROTO_TCP ||
3363 rule->flow.nw_proto == IPPROTO_UDP) {
3364 may_match |= MAY_TP_ADDR;
3365 } else if (rule->flow.nw_proto == IPPROTO_ICMPV6) {
3366 may_match |= MAY_TP_ADDR;
3367 if (rule->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
3368 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
3369 } else if (rule->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
3370 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
3373 } else if (rule->flow.dl_type == htons(ETH_TYPE_ARP)) {
3374 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
3379 /* Clear the fields that may not be matched. */
3381 if (!(may_match & MAY_NW_ADDR)) {
3382 wc.nw_src_mask = wc.nw_dst_mask = htonl(0);
3384 if (!(may_match & MAY_TP_ADDR)) {
3385 wc.tp_src_mask = wc.tp_dst_mask = htons(0);
3387 if (!(may_match & MAY_NW_PROTO)) {
3388 wc.wildcards |= FWW_NW_PROTO;
3390 if (!(may_match & MAY_IPVx)) {
3391 wc.wildcards |= FWW_NW_DSCP;
3392 wc.wildcards |= FWW_NW_ECN;
3393 wc.wildcards |= FWW_NW_TTL;
3395 if (!(may_match & MAY_ARP_SHA)) {
3396 memset(wc.arp_sha_mask, 0, ETH_ADDR_LEN);
3398 if (!(may_match & MAY_ARP_THA)) {
3399 memset(wc.arp_tha_mask, 0, ETH_ADDR_LEN);
3401 if (!(may_match & MAY_IPV6)) {
3402 wc.ipv6_src_mask = wc.ipv6_dst_mask = in6addr_any;
3403 wc.ipv6_label_mask = htonl(0);
3405 if (!(may_match & MAY_ND_TARGET)) {
3406 wc.nd_target_mask = in6addr_any;
3409 /* Log any changes. */
3410 if (!flow_wildcards_equal(&wc, &rule->wc)) {
3411 bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
3412 char *pre = log ? cls_rule_to_string(rule) : NULL;
3415 cls_rule_zero_wildcarded_fields(rule);
3418 char *post = cls_rule_to_string(rule);
3419 VLOG_INFO("normalization changed ofp_match, details:");
3420 VLOG_INFO(" pre: %s", pre);
3421 VLOG_INFO("post: %s", post);
3428 /* "Normalizes" the wildcards in 'rule'. That means:
3430 * 1. If the type of level N is known, then only the valid fields for that
3431 * level may be specified. For example, ARP does not have a TOS field,
3432 * so nw_tos must be wildcarded if 'rule' specifies an ARP flow.
3433 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
3434 * ipv6_dst (and other fields) must be wildcarded if 'rule' specifies an
3437 * 2. If the type of level N is not known (or not understood by Open
3438 * vSwitch), then no fields at all for that level may be specified. For
3439 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
3440 * L4 fields tp_src and tp_dst must be wildcarded if 'rule' specifies an
3443 * If this function changes 'rule', it logs a rate-limited informational
3446 ofputil_normalize_rule(struct cls_rule *rule)
3448 ofputil_normalize_rule__(rule, true);
3451 /* Same as ofputil_normalize_rule() without the logging. Thus, this function
3452 * is suitable for a program's internal use, whereas ofputil_normalize_rule()
3453 * sense for use on flows received from elsewhere (so that a bug in the program
3454 * that sent them can be reported and corrected). */
3456 ofputil_normalize_rule_quiet(struct cls_rule *rule)
3458 ofputil_normalize_rule__(rule, false);
3461 /* Parses a key or a key-value pair from '*stringp'.
3463 * On success: Stores the key into '*keyp'. Stores the value, if present, into
3464 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
3465 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
3466 * are substrings of '*stringp' created by replacing some of its bytes by null
3467 * terminators. Returns true.
3469 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
3470 * NULL and returns false. */
3472 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
3474 char *pos, *key, *value;
3478 pos += strspn(pos, ", \t\r\n");
3480 *keyp = *valuep = NULL;
3485 key_len = strcspn(pos, ":=(, \t\r\n");
3486 if (key[key_len] == ':' || key[key_len] == '=') {
3487 /* The value can be separated by a colon. */
3490 value = key + key_len + 1;
3491 value_len = strcspn(value, ", \t\r\n");
3492 pos = value + value_len + (value[value_len] != '\0');
3493 value[value_len] = '\0';
3494 } else if (key[key_len] == '(') {
3495 /* The value can be surrounded by balanced parentheses. The outermost
3496 * set of parentheses is removed. */
3500 value = key + key_len + 1;
3501 for (value_len = 0; level > 0; value_len++) {
3502 switch (value[value_len]) {
3516 value[value_len - 1] = '\0';
3517 pos = value + value_len;
3519 /* There might be no value at all. */
3520 value = key + key_len; /* Will become the empty string below. */
3521 pos = key + key_len + (key[key_len] != '\0');
3523 key[key_len] = '\0';