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 or tun_id. */
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)
272 struct ofp11_match_header *omh;
273 struct ofp11_match *om;
275 if (buf->size < sizeof(struct ofp11_match_header)) {
276 return OFPERR_OFPBMC_BAD_LEN;
280 switch (ntohs(omh->type)) {
282 if (omh->length != htons(sizeof *om) || buf->size < sizeof *om) {
283 return OFPERR_OFPBMC_BAD_LEN;
285 om = ofpbuf_pull(buf, sizeof *om);
286 return ofputil_cls_rule_from_ofp11_match(om, priority, rule);
289 return OFPERR_OFPBMC_BAD_TYPE;
293 /* Converts the ofp11_match in 'match' into a cls_rule in 'rule', with the
294 * given 'priority'. Returns 0 if successful, otherwise an OFPERR_* value. */
296 ofputil_cls_rule_from_ofp11_match(const struct ofp11_match *match,
297 unsigned int priority,
298 struct cls_rule *rule)
300 uint16_t wc = ntohl(match->wildcards);
301 uint8_t dl_src_mask[ETH_ADDR_LEN];
302 uint8_t dl_dst_mask[ETH_ADDR_LEN];
306 cls_rule_init_catchall(rule, priority);
308 if (!(wc & OFPFW11_IN_PORT)) {
312 error = ofputil_port_from_ofp11(match->in_port, &ofp_port);
314 return OFPERR_OFPBMC_BAD_VALUE;
316 cls_rule_set_in_port(rule, ofp_port);
319 for (i = 0; i < ETH_ADDR_LEN; i++) {
320 dl_src_mask[i] = ~match->dl_src_mask[i];
322 cls_rule_set_dl_src_masked(rule, match->dl_src, dl_src_mask);
324 for (i = 0; i < ETH_ADDR_LEN; i++) {
325 dl_dst_mask[i] = ~match->dl_dst_mask[i];
327 cls_rule_set_dl_dst_masked(rule, match->dl_dst, dl_dst_mask);
329 if (!(wc & OFPFW11_DL_VLAN)) {
330 if (match->dl_vlan == htons(OFPVID11_NONE)) {
331 /* Match only packets without a VLAN tag. */
332 rule->flow.vlan_tci = htons(0);
333 rule->wc.vlan_tci_mask = htons(UINT16_MAX);
335 if (match->dl_vlan == htons(OFPVID11_ANY)) {
336 /* Match any packet with a VLAN tag regardless of VID. */
337 rule->flow.vlan_tci = htons(VLAN_CFI);
338 rule->wc.vlan_tci_mask = htons(VLAN_CFI);
339 } else if (ntohs(match->dl_vlan) < 4096) {
340 /* Match only packets with the specified VLAN VID. */
341 rule->flow.vlan_tci = htons(VLAN_CFI) | match->dl_vlan;
342 rule->wc.vlan_tci_mask = htons(VLAN_CFI | VLAN_VID_MASK);
345 return OFPERR_OFPBMC_BAD_VALUE;
348 if (!(wc & OFPFW11_DL_VLAN_PCP)) {
349 if (match->dl_vlan_pcp <= 7) {
350 rule->flow.vlan_tci |= htons(match->dl_vlan_pcp
352 rule->wc.vlan_tci_mask |= htons(VLAN_PCP_MASK);
355 return OFPERR_OFPBMC_BAD_VALUE;
361 if (!(wc & OFPFW11_DL_TYPE)) {
362 cls_rule_set_dl_type(rule,
363 ofputil_dl_type_from_openflow(match->dl_type));
366 ipv4 = rule->flow.dl_type == htons(ETH_TYPE_IP);
367 arp = rule->flow.dl_type == htons(ETH_TYPE_ARP);
369 if (ipv4 && !(wc & OFPFW11_NW_TOS)) {
370 if (match->nw_tos & ~IP_DSCP_MASK) {
372 return OFPERR_OFPBMC_BAD_VALUE;
375 cls_rule_set_nw_dscp(rule, match->nw_tos);
379 if (!(wc & OFPFW11_NW_PROTO)) {
380 cls_rule_set_nw_proto(rule, match->nw_proto);
382 cls_rule_set_nw_src_masked(rule, match->nw_src, ~match->nw_src_mask);
383 cls_rule_set_nw_dst_masked(rule, match->nw_dst, ~match->nw_dst_mask);
386 #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
387 if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) {
388 switch (rule->flow.nw_proto) {
390 /* "A.2.3 Flow Match Structures" in OF1.1 says:
392 * The tp_src and tp_dst fields will be ignored unless the
393 * network protocol specified is as TCP, UDP or SCTP.
395 * but I'm pretty sure we should support ICMP too, otherwise
396 * that's a regression from OF1.0. */
397 if (!(wc & OFPFW11_TP_SRC)) {
398 uint16_t icmp_type = ntohs(match->tp_src);
399 if (icmp_type < 0x100) {
400 cls_rule_set_icmp_type(rule, icmp_type);
402 return OFPERR_OFPBMC_BAD_FIELD;
405 if (!(wc & OFPFW11_TP_DST)) {
406 uint16_t icmp_code = ntohs(match->tp_dst);
407 if (icmp_code < 0x100) {
408 cls_rule_set_icmp_code(rule, icmp_code);
410 return OFPERR_OFPBMC_BAD_FIELD;
417 if (!(wc & (OFPFW11_TP_SRC))) {
418 cls_rule_set_tp_src(rule, match->tp_src);
420 if (!(wc & (OFPFW11_TP_DST))) {
421 cls_rule_set_tp_dst(rule, match->tp_dst);
426 /* We don't support SCTP and it seems that we should tell the
427 * controller, since OF1.1 implementations are supposed to. */
428 return OFPERR_OFPBMC_BAD_FIELD;
431 /* OF1.1 says explicitly to ignore this. */
436 if (rule->flow.dl_type == htons(ETH_TYPE_MPLS) ||
437 rule->flow.dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
438 enum { OFPFW11_MPLS_ALL = OFPFW11_MPLS_LABEL | OFPFW11_MPLS_TC };
440 if ((wc & OFPFW11_MPLS_ALL) != OFPFW11_MPLS_ALL) {
441 /* MPLS not supported. */
442 return OFPERR_OFPBMC_BAD_TAG;
446 if (match->metadata_mask != htonll(UINT64_MAX)) {
447 cls_rule_set_metadata_masked(rule, match->metadata,
448 ~match->metadata_mask);
454 /* Convert 'rule' into the OpenFlow 1.1 match structure 'match'. */
456 ofputil_cls_rule_to_ofp11_match(const struct cls_rule *rule,
457 struct ofp11_match *match)
462 memset(match, 0, sizeof *match);
463 match->omh.type = htons(OFPMT_STANDARD);
464 match->omh.length = htons(OFPMT11_STANDARD_LENGTH);
466 if (rule->wc.wildcards & FWW_IN_PORT) {
467 wc |= OFPFW11_IN_PORT;
469 match->in_port = ofputil_port_to_ofp11(rule->flow.in_port);
473 memcpy(match->dl_src, rule->flow.dl_src, ETH_ADDR_LEN);
474 for (i = 0; i < ETH_ADDR_LEN; i++) {
475 match->dl_src_mask[i] = ~rule->wc.dl_src_mask[i];
478 memcpy(match->dl_dst, rule->flow.dl_dst, ETH_ADDR_LEN);
479 for (i = 0; i < ETH_ADDR_LEN; i++) {
480 match->dl_dst_mask[i] = ~rule->wc.dl_dst_mask[i];
483 if (rule->wc.vlan_tci_mask == htons(0)) {
484 wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP;
485 } else if (rule->wc.vlan_tci_mask & htons(VLAN_CFI)
486 && !(rule->flow.vlan_tci & htons(VLAN_CFI))) {
487 match->dl_vlan = htons(OFPVID11_NONE);
488 wc |= OFPFW11_DL_VLAN_PCP;
490 if (!(rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK))) {
491 match->dl_vlan = htons(OFPVID11_ANY);
493 match->dl_vlan = htons(vlan_tci_to_vid(rule->flow.vlan_tci));
496 if (!(rule->wc.vlan_tci_mask & htons(VLAN_PCP_MASK))) {
497 wc |= OFPFW11_DL_VLAN_PCP;
499 match->dl_vlan_pcp = vlan_tci_to_pcp(rule->flow.vlan_tci);
503 if (rule->wc.wildcards & FWW_DL_TYPE) {
504 wc |= OFPFW11_DL_TYPE;
506 match->dl_type = ofputil_dl_type_to_openflow(rule->flow.dl_type);
509 if (rule->wc.wildcards & FWW_NW_DSCP) {
510 wc |= OFPFW11_NW_TOS;
512 match->nw_tos = rule->flow.nw_tos & IP_DSCP_MASK;
515 if (rule->wc.wildcards & FWW_NW_PROTO) {
516 wc |= OFPFW11_NW_PROTO;
518 match->nw_proto = rule->flow.nw_proto;
521 match->nw_src = rule->flow.nw_src;
522 match->nw_src_mask = ~rule->wc.nw_src_mask;
523 match->nw_dst = rule->flow.nw_dst;
524 match->nw_dst_mask = ~rule->wc.nw_dst_mask;
526 if (!rule->wc.tp_src_mask) {
527 wc |= OFPFW11_TP_SRC;
529 match->tp_src = rule->flow.tp_src;
532 if (!rule->wc.tp_dst_mask) {
533 wc |= OFPFW11_TP_DST;
535 match->tp_dst = rule->flow.tp_dst;
538 /* MPLS not supported. */
539 wc |= OFPFW11_MPLS_LABEL;
540 wc |= OFPFW11_MPLS_TC;
542 match->metadata = rule->flow.metadata;
543 match->metadata_mask = ~rule->wc.metadata_mask;
545 match->wildcards = htonl(wc);
548 /* Given a 'dl_type' value in the format used in struct flow, returns the
549 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
552 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
554 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
555 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
559 /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
560 * structure, returns the corresponding 'dl_type' value for use in struct
563 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
565 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
566 ? htons(FLOW_DL_TYPE_NONE)
572 struct proto_abbrev {
573 enum ofputil_protocol protocol;
577 /* Most users really don't care about some of the differences between
578 * protocols. These abbreviations help with that. */
579 static const struct proto_abbrev proto_abbrevs[] = {
580 { OFPUTIL_P_ANY, "any" },
581 { OFPUTIL_P_OF10_ANY, "OpenFlow10" },
582 { OFPUTIL_P_NXM_ANY, "NXM" },
584 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
586 enum ofputil_protocol ofputil_flow_dump_protocols[] = {
590 size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
592 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
593 * connection that has negotiated the given 'version'. 'version' should
594 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
595 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
596 * outside the valid range. */
597 enum ofputil_protocol
598 ofputil_protocol_from_ofp_version(enum ofp_version version)
602 return OFPUTIL_P_OF10;
604 return OFPUTIL_P_OF12;
611 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
612 * OFP11_VERSION or OFP12_VERSION) that corresponds to 'protocol'. */
614 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
618 case OFPUTIL_P_OF10_TID:
620 case OFPUTIL_P_NXM_TID:
621 return OFP10_VERSION;
623 return OFP12_VERSION;
629 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
632 ofputil_protocol_is_valid(enum ofputil_protocol protocol)
634 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
637 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
638 * extension turned on or off if 'enable' is true or false, respectively.
640 * This extension is only useful for protocols whose "standard" version does
641 * not allow specific tables to be modified. In particular, this is true of
642 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
643 * specifies a table ID and so there is no need for such an extension. When
644 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
645 * extension, this function just returns its 'protocol' argument unchanged
646 * regardless of the value of 'enable'. */
647 enum ofputil_protocol
648 ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
652 case OFPUTIL_P_OF10_TID:
653 return enable ? OFPUTIL_P_OF10_TID : OFPUTIL_P_OF10;
656 case OFPUTIL_P_NXM_TID:
657 return enable ? OFPUTIL_P_NXM_TID : OFPUTIL_P_NXM;
660 return OFPUTIL_P_OF12;
667 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
668 * some extension to a standard protocol version, the return value is the
669 * standard version of that protocol without any extension. If 'protocol' is a
670 * standard protocol version, returns 'protocol' unchanged. */
671 enum ofputil_protocol
672 ofputil_protocol_to_base(enum ofputil_protocol protocol)
674 return ofputil_protocol_set_tid(protocol, false);
677 /* Returns 'new_base' with any extensions taken from 'cur'. */
678 enum ofputil_protocol
679 ofputil_protocol_set_base(enum ofputil_protocol cur,
680 enum ofputil_protocol new_base)
682 bool tid = (cur & OFPUTIL_P_TID) != 0;
686 case OFPUTIL_P_OF10_TID:
687 return ofputil_protocol_set_tid(OFPUTIL_P_OF10, tid);
690 case OFPUTIL_P_NXM_TID:
691 return ofputil_protocol_set_tid(OFPUTIL_P_NXM, tid);
694 return ofputil_protocol_set_tid(OFPUTIL_P_OF12, tid);
701 /* Returns a string form of 'protocol', if a simple form exists (that is, if
702 * 'protocol' is either a single protocol or it is a combination of protocols
703 * that have a single abbreviation). Otherwise, returns NULL. */
705 ofputil_protocol_to_string(enum ofputil_protocol protocol)
707 const struct proto_abbrev *p;
709 /* Use a "switch" statement for single-bit names so that we get a compiler
710 * warning if we forget any. */
713 return "NXM-table_id";
715 case OFPUTIL_P_NXM_TID:
716 return "NXM+table_id";
719 return "OpenFlow10-table_id";
721 case OFPUTIL_P_OF10_TID:
722 return "OpenFlow10+table_id";
728 /* Check abbreviations. */
729 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
730 if (protocol == p->protocol) {
738 /* Returns a string that represents 'protocols'. The return value might be a
739 * comma-separated list if 'protocols' doesn't have a simple name. The return
740 * value is "none" if 'protocols' is 0.
742 * The caller must free the returned string (with free()). */
744 ofputil_protocols_to_string(enum ofputil_protocol protocols)
748 assert(!(protocols & ~OFPUTIL_P_ANY));
749 if (protocols == 0) {
750 return xstrdup("none");
755 const struct proto_abbrev *p;
759 ds_put_char(&s, ',');
762 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
763 if ((protocols & p->protocol) == p->protocol) {
764 ds_put_cstr(&s, p->name);
765 protocols &= ~p->protocol;
770 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
771 enum ofputil_protocol bit = 1u << i;
773 if (protocols & bit) {
774 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
783 return ds_steal_cstr(&s);
786 static enum ofputil_protocol
787 ofputil_protocol_from_string__(const char *s, size_t n)
789 const struct proto_abbrev *p;
792 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
793 enum ofputil_protocol bit = 1u << i;
794 const char *name = ofputil_protocol_to_string(bit);
796 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
801 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
802 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
810 /* Returns the nonempty set of protocols represented by 's', which can be a
811 * single protocol name or abbreviation or a comma-separated list of them.
813 * Aborts the program with an error message if 's' is invalid. */
814 enum ofputil_protocol
815 ofputil_protocols_from_string(const char *s)
817 const char *orig_s = s;
818 enum ofputil_protocol protocols;
822 enum ofputil_protocol p;
831 p = ofputil_protocol_from_string__(s, n);
833 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
841 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
847 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
849 switch (packet_in_format) {
850 case NXPIF_OPENFLOW10:
859 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
861 switch (packet_in_format) {
862 case NXPIF_OPENFLOW10:
872 ofputil_packet_in_format_from_string(const char *s)
874 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
875 : !strcmp(s, "nxm") ? NXPIF_NXM
880 regs_fully_wildcarded(const struct flow_wildcards *wc)
884 for (i = 0; i < FLOW_N_REGS; i++) {
885 if (wc->reg_masks[i] != 0) {
892 /* Returns a bit-mask of ofputil_protocols that can be used for sending 'rule'
893 * to a switch (e.g. to add or remove a flow). Only NXM can handle tunnel IDs,
894 * registers, or fixing the Ethernet multicast bit. Otherwise, it's better to
895 * use OpenFlow 1.0 protocol for backward compatibility. */
896 enum ofputil_protocol
897 ofputil_usable_protocols(const struct cls_rule *rule)
899 const struct flow_wildcards *wc = &rule->wc;
901 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 14);
903 /* NXM and OF1.1+ supports bitwise matching on ethernet addresses. */
904 if (!eth_mask_is_exact(wc->dl_src_mask)
905 && !eth_addr_is_zero(wc->dl_src_mask)) {
906 return OFPUTIL_P_NXM_ANY;
908 if (!eth_mask_is_exact(wc->dl_dst_mask)
909 && !eth_addr_is_zero(wc->dl_dst_mask)) {
910 return OFPUTIL_P_NXM_ANY;
913 /* NXM and OF1.1+ support matching metadata. */
914 if (wc->metadata_mask != htonll(0)) {
915 return OFPUTIL_P_NXM_ANY;
918 /* Only NXM supports matching ARP hardware addresses. */
919 if (!eth_addr_is_zero(wc->arp_sha_mask) ||
920 !eth_addr_is_zero(wc->arp_tha_mask)) {
921 return OFPUTIL_P_NXM_ANY;
924 /* Only NXM supports matching IPv6 traffic. */
925 if (!(wc->wildcards & FWW_DL_TYPE)
926 && (rule->flow.dl_type == htons(ETH_TYPE_IPV6))) {
927 return OFPUTIL_P_NXM_ANY;
930 /* Only NXM supports matching registers. */
931 if (!regs_fully_wildcarded(wc)) {
932 return OFPUTIL_P_NXM_ANY;
935 /* Only NXM supports matching tun_id. */
936 if (wc->tun_id_mask != htonll(0)) {
937 return OFPUTIL_P_NXM_ANY;
940 /* Only NXM supports matching fragments. */
941 if (wc->nw_frag_mask) {
942 return OFPUTIL_P_NXM_ANY;
945 /* Only NXM supports matching IPv6 flow label. */
946 if (wc->ipv6_label_mask) {
947 return OFPUTIL_P_NXM_ANY;
950 /* Only NXM supports matching IP ECN bits. */
951 if (!(wc->wildcards & FWW_NW_ECN)) {
952 return OFPUTIL_P_NXM_ANY;
955 /* Only NXM supports matching IP TTL/hop limit. */
956 if (!(wc->wildcards & FWW_NW_TTL)) {
957 return OFPUTIL_P_NXM_ANY;
960 /* Only NXM supports non-CIDR IPv4 address masks. */
961 if (!ip_is_cidr(wc->nw_src_mask) || !ip_is_cidr(wc->nw_dst_mask)) {
962 return OFPUTIL_P_NXM_ANY;
965 /* Only NXM supports bitwise matching on transport port. */
966 if ((wc->tp_src_mask && wc->tp_src_mask != htons(UINT16_MAX)) ||
967 (wc->tp_dst_mask && wc->tp_dst_mask != htons(UINT16_MAX))) {
968 return OFPUTIL_P_NXM_ANY;
971 /* Other formats can express this rule. */
972 return OFPUTIL_P_ANY;
975 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
976 * protocol is 'current', at least partly transitions the protocol to 'want'.
977 * Stores in '*next' the protocol that will be in effect on the OpenFlow
978 * connection if the switch processes the returned message correctly. (If
979 * '*next != want' then the caller will have to iterate.)
981 * If 'current == want', returns NULL and stores 'current' in '*next'. */
983 ofputil_encode_set_protocol(enum ofputil_protocol current,
984 enum ofputil_protocol want,
985 enum ofputil_protocol *next)
987 enum ofputil_protocol cur_base, want_base;
988 bool cur_tid, want_tid;
990 cur_base = ofputil_protocol_to_base(current);
991 want_base = ofputil_protocol_to_base(want);
992 if (cur_base != want_base) {
993 *next = ofputil_protocol_set_base(current, want_base);
997 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1000 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1002 case OFPUTIL_P_OF12:
1003 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW12);
1005 case OFPUTIL_P_OF10_TID:
1006 case OFPUTIL_P_NXM_TID:
1011 cur_tid = (current & OFPUTIL_P_TID) != 0;
1012 want_tid = (want & OFPUTIL_P_TID) != 0;
1013 if (cur_tid != want_tid) {
1014 *next = ofputil_protocol_set_tid(current, want_tid);
1015 return ofputil_make_flow_mod_table_id(want_tid);
1018 assert(current == want);
1024 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1025 * format to 'nxff'. */
1027 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1029 struct nx_set_flow_format *sff;
1032 assert(ofputil_nx_flow_format_is_valid(nxff));
1034 msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1035 sff = ofpbuf_put_zeros(msg, sizeof *sff);
1036 sff->format = htonl(nxff);
1041 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1043 enum ofputil_protocol
1044 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1046 switch (flow_format) {
1047 case NXFF_OPENFLOW10:
1048 return OFPUTIL_P_OF10;
1051 return OFPUTIL_P_NXM;
1053 case NXFF_OPENFLOW12:
1054 return OFPUTIL_P_OF12;
1061 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1063 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1065 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1068 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1071 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1073 switch (flow_format) {
1074 case NXFF_OPENFLOW10:
1075 return "openflow10";
1078 case NXFF_OPENFLOW12:
1079 return "openflow12";
1086 ofputil_make_set_packet_in_format(enum nx_packet_in_format packet_in_format)
1088 struct nx_set_packet_in_format *spif;
1091 msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, OFP10_VERSION, 0);
1092 spif = ofpbuf_put_zeros(msg, sizeof *spif);
1093 spif->format = htonl(packet_in_format);
1098 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1099 * extension on or off (according to 'flow_mod_table_id'). */
1101 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1103 struct nx_flow_mod_table_id *nfmti;
1106 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1107 nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
1108 nfmti->set = flow_mod_table_id;
1112 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1113 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1116 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1117 * The caller must initialize 'ofpacts' and retains ownership of it.
1118 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1120 * Does not validate the flow_mod actions. The caller should do that, with
1121 * ofpacts_check(). */
1123 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1124 const struct ofp_header *oh,
1125 enum ofputil_protocol protocol,
1126 struct ofpbuf *ofpacts)
1132 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1133 raw = ofpraw_pull_assert(&b);
1134 if (raw == OFPRAW_OFPT11_FLOW_MOD) {
1135 /* Standard OpenFlow 1.1 flow_mod. */
1136 const struct ofp11_flow_mod *ofm;
1139 ofm = ofpbuf_pull(&b, sizeof *ofm);
1141 error = ofputil_pull_ofp11_match(&b, ntohs(ofm->priority), &fm->cr);
1146 error = ofpacts_pull_openflow11_instructions(&b, b.size, ofpacts);
1151 /* Translate the message. */
1152 if (ofm->command == OFPFC_ADD) {
1153 fm->cookie = htonll(0);
1154 fm->cookie_mask = htonll(0);
1155 fm->new_cookie = ofm->cookie;
1158 fm->cookie = ofm->cookie;
1159 fm->cookie_mask = ofm->cookie_mask;
1160 fm->new_cookie = htonll(UINT64_MAX);
1162 fm->command = ofm->command;
1163 fm->table_id = ofm->table_id;
1164 fm->idle_timeout = ntohs(ofm->idle_timeout);
1165 fm->hard_timeout = ntohs(ofm->hard_timeout);
1166 fm->buffer_id = ntohl(ofm->buffer_id);
1167 error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
1171 if (ofm->out_group != htonl(OFPG_ANY)) {
1172 return OFPERR_NXFMFC_GROUPS_NOT_SUPPORTED;
1174 fm->flags = ntohs(ofm->flags);
1176 if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1177 /* Standard OpenFlow 1.0 flow_mod. */
1178 const struct ofp10_flow_mod *ofm;
1182 /* Get the ofp10_flow_mod. */
1183 ofm = ofpbuf_pull(&b, sizeof *ofm);
1185 /* Set priority based on original wildcards. Normally we'd allow
1186 * ofputil_cls_rule_from_match() to do this for us, but
1187 * ofputil_normalize_rule() can put wildcards where the original
1188 * flow didn't have them. */
1189 priority = ntohs(ofm->priority);
1190 if (!(ofm->match.wildcards & htonl(OFPFW10_ALL))) {
1191 priority = UINT16_MAX;
1194 /* Translate the rule. */
1195 ofputil_cls_rule_from_ofp10_match(&ofm->match, priority, &fm->cr);
1196 ofputil_normalize_rule(&fm->cr);
1198 /* Now get the actions. */
1199 error = ofpacts_pull_openflow10(&b, b.size, ofpacts);
1204 /* Translate the message. */
1205 command = ntohs(ofm->command);
1206 fm->cookie = htonll(0);
1207 fm->cookie_mask = htonll(0);
1208 fm->new_cookie = ofm->cookie;
1209 fm->idle_timeout = ntohs(ofm->idle_timeout);
1210 fm->hard_timeout = ntohs(ofm->hard_timeout);
1211 fm->buffer_id = ntohl(ofm->buffer_id);
1212 fm->out_port = ntohs(ofm->out_port);
1213 fm->flags = ntohs(ofm->flags);
1214 } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1215 /* Nicira extended flow_mod. */
1216 const struct nx_flow_mod *nfm;
1219 /* Dissect the message. */
1220 nfm = ofpbuf_pull(&b, sizeof *nfm);
1221 error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
1222 &fm->cr, &fm->cookie, &fm->cookie_mask);
1226 error = ofpacts_pull_openflow10(&b, b.size, ofpacts);
1231 /* Translate the message. */
1232 command = ntohs(nfm->command);
1233 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1234 /* Flow additions may only set a new cookie, not match an
1235 * existing cookie. */
1236 return OFPERR_NXBRC_NXM_INVALID;
1238 fm->new_cookie = nfm->cookie;
1239 fm->idle_timeout = ntohs(nfm->idle_timeout);
1240 fm->hard_timeout = ntohs(nfm->hard_timeout);
1241 fm->buffer_id = ntohl(nfm->buffer_id);
1242 fm->out_port = ntohs(nfm->out_port);
1243 fm->flags = ntohs(nfm->flags);
1248 if (protocol & OFPUTIL_P_TID) {
1249 fm->command = command & 0xff;
1250 fm->table_id = command >> 8;
1252 fm->command = command;
1253 fm->table_id = 0xff;
1257 fm->ofpacts = ofpacts->data;
1258 fm->ofpacts_len = ofpacts->size;
1263 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
1264 * 'protocol' and returns the message. */
1266 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
1267 enum ofputil_protocol protocol)
1272 command = (protocol & OFPUTIL_P_TID
1273 ? (fm->command & 0xff) | (fm->table_id << 8)
1277 case OFPUTIL_P_OF10:
1278 case OFPUTIL_P_OF10_TID: {
1279 struct ofp10_flow_mod *ofm;
1281 msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
1283 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
1284 ofputil_cls_rule_to_ofp10_match(&fm->cr, &ofm->match);
1285 ofm->cookie = fm->new_cookie;
1286 ofm->command = htons(command);
1287 ofm->idle_timeout = htons(fm->idle_timeout);
1288 ofm->hard_timeout = htons(fm->hard_timeout);
1289 ofm->priority = htons(fm->cr.priority);
1290 ofm->buffer_id = htonl(fm->buffer_id);
1291 ofm->out_port = htons(fm->out_port);
1292 ofm->flags = htons(fm->flags);
1297 case OFPUTIL_P_NXM_TID: {
1298 struct nx_flow_mod *nfm;
1301 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
1302 NXM_TYPICAL_LEN + fm->ofpacts_len);
1303 nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
1304 nfm->command = htons(command);
1305 nfm->cookie = fm->new_cookie;
1306 match_len = nx_put_match(msg, false, &fm->cr,
1307 fm->cookie, fm->cookie_mask);
1309 nfm->idle_timeout = htons(fm->idle_timeout);
1310 nfm->hard_timeout = htons(fm->hard_timeout);
1311 nfm->priority = htons(fm->cr.priority);
1312 nfm->buffer_id = htonl(fm->buffer_id);
1313 nfm->out_port = htons(fm->out_port);
1314 nfm->flags = htons(fm->flags);
1315 nfm->match_len = htons(match_len);
1319 case OFPUTIL_P_OF12:
1325 ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
1327 ofpmsg_update_length(msg);
1331 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1332 * send all of the 'n_fm's flow table modification requests in 'fms', and a
1333 * 0-bit for each protocol that is inadequate.
1335 * (The return value will have at least one 1-bit.) */
1336 enum ofputil_protocol
1337 ofputil_flow_mod_usable_protocols(const struct ofputil_flow_mod *fms,
1340 enum ofputil_protocol usable_protocols;
1343 usable_protocols = OFPUTIL_P_ANY;
1344 for (i = 0; i < n_fms; i++) {
1345 const struct ofputil_flow_mod *fm = &fms[i];
1347 usable_protocols &= ofputil_usable_protocols(&fm->cr);
1348 if (fm->table_id != 0xff) {
1349 usable_protocols &= OFPUTIL_P_TID;
1352 /* Matching of the cookie is only supported through NXM. */
1353 if (fm->cookie_mask != htonll(0)) {
1354 usable_protocols &= OFPUTIL_P_NXM_ANY;
1357 assert(usable_protocols);
1359 return usable_protocols;
1363 ofputil_decode_ofpst_flow_request(struct ofputil_flow_stats_request *fsr,
1364 const struct ofp10_flow_stats_request *ofsr,
1367 fsr->aggregate = aggregate;
1368 ofputil_cls_rule_from_ofp10_match(&ofsr->match, 0, &fsr->match);
1369 fsr->out_port = ntohs(ofsr->out_port);
1370 fsr->table_id = ofsr->table_id;
1371 fsr->cookie = fsr->cookie_mask = htonll(0);
1377 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
1378 struct ofpbuf *b, bool aggregate)
1380 const struct nx_flow_stats_request *nfsr;
1383 nfsr = ofpbuf_pull(b, sizeof *nfsr);
1384 error = nx_pull_match(b, ntohs(nfsr->match_len), 0, &fsr->match,
1385 &fsr->cookie, &fsr->cookie_mask);
1390 return OFPERR_OFPBRC_BAD_LEN;
1393 fsr->aggregate = aggregate;
1394 fsr->out_port = ntohs(nfsr->out_port);
1395 fsr->table_id = nfsr->table_id;
1400 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1401 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1402 * successful, otherwise an OpenFlow error code. */
1404 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
1405 const struct ofp_header *oh)
1410 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1411 raw = ofpraw_pull_assert(&b);
1412 switch ((int) raw) {
1413 case OFPRAW_OFPST_FLOW_REQUEST:
1414 return ofputil_decode_ofpst_flow_request(fsr, b.data, false);
1416 case OFPRAW_OFPST_AGGREGATE_REQUEST:
1417 return ofputil_decode_ofpst_flow_request(fsr, b.data, true);
1419 case OFPRAW_NXST_FLOW_REQUEST:
1420 return ofputil_decode_nxst_flow_request(fsr, &b, false);
1422 case OFPRAW_NXST_AGGREGATE_REQUEST:
1423 return ofputil_decode_nxst_flow_request(fsr, &b, true);
1426 /* Hey, the caller lied. */
1431 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1432 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1433 * 'protocol', and returns the message. */
1435 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
1436 enum ofputil_protocol protocol)
1442 case OFPUTIL_P_OF10:
1443 case OFPUTIL_P_OF10_TID: {
1444 struct ofp10_flow_stats_request *ofsr;
1446 raw = (fsr->aggregate
1447 ? OFPRAW_OFPST_AGGREGATE_REQUEST
1448 : OFPRAW_OFPST_FLOW_REQUEST);
1449 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
1450 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
1451 ofputil_cls_rule_to_ofp10_match(&fsr->match, &ofsr->match);
1452 ofsr->table_id = fsr->table_id;
1453 ofsr->out_port = htons(fsr->out_port);
1458 case OFPUTIL_P_NXM_TID: {
1459 struct nx_flow_stats_request *nfsr;
1462 raw = (fsr->aggregate
1463 ? OFPRAW_NXST_AGGREGATE_REQUEST
1464 : OFPRAW_NXST_FLOW_REQUEST);
1465 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
1466 ofpbuf_put_zeros(msg, sizeof *nfsr);
1467 match_len = nx_put_match(msg, false, &fsr->match,
1468 fsr->cookie, fsr->cookie_mask);
1471 nfsr->out_port = htons(fsr->out_port);
1472 nfsr->match_len = htons(match_len);
1473 nfsr->table_id = fsr->table_id;
1477 case OFPUTIL_P_OF12:
1485 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1486 * accurately encode 'fsr', and a 0-bit for each protocol that is inadequate.
1488 * (The return value will have at least one 1-bit.) */
1489 enum ofputil_protocol
1490 ofputil_flow_stats_request_usable_protocols(
1491 const struct ofputil_flow_stats_request *fsr)
1493 enum ofputil_protocol usable_protocols;
1495 usable_protocols = ofputil_usable_protocols(&fsr->match);
1496 if (fsr->cookie_mask != htonll(0)) {
1497 usable_protocols &= OFPUTIL_P_NXM_ANY;
1499 return usable_protocols;
1502 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1503 * ofputil_flow_stats in 'fs'.
1505 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1506 * OpenFlow message. Calling this function multiple times for a single 'msg'
1507 * iterates through the replies. The caller must initially leave 'msg''s layer
1508 * pointers null and not modify them between calls.
1510 * Most switches don't send the values needed to populate fs->idle_age and
1511 * fs->hard_age, so those members will usually be set to 0. If the switch from
1512 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
1513 * 'flow_age_extension' as true so that the contents of 'msg' determine the
1514 * 'idle_age' and 'hard_age' members in 'fs'.
1516 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
1517 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
1518 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
1520 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1521 * otherwise a positive errno value. */
1523 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1525 bool flow_age_extension,
1526 struct ofpbuf *ofpacts)
1532 ? ofpraw_decode(&raw, msg->l2)
1533 : ofpraw_pull(&raw, msg));
1540 } else if (raw == OFPRAW_OFPST_FLOW_REPLY) {
1541 const struct ofp10_flow_stats *ofs;
1544 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1546 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1547 "bytes at end", msg->size);
1551 length = ntohs(ofs->length);
1552 if (length < sizeof *ofs) {
1553 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1554 "length %zu", length);
1558 if (ofpacts_pull_openflow10(msg, length - sizeof *ofs, ofpacts)) {
1562 fs->cookie = get_32aligned_be64(&ofs->cookie);
1563 ofputil_cls_rule_from_ofp10_match(&ofs->match, ntohs(ofs->priority),
1565 fs->table_id = ofs->table_id;
1566 fs->duration_sec = ntohl(ofs->duration_sec);
1567 fs->duration_nsec = ntohl(ofs->duration_nsec);
1568 fs->idle_timeout = ntohs(ofs->idle_timeout);
1569 fs->hard_timeout = ntohs(ofs->hard_timeout);
1572 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1573 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
1574 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
1575 const struct nx_flow_stats *nfs;
1576 size_t match_len, actions_len, length;
1578 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1580 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1581 "bytes at end", msg->size);
1585 length = ntohs(nfs->length);
1586 match_len = ntohs(nfs->match_len);
1587 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1588 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1589 "claims invalid length %zu", match_len, length);
1592 if (nx_pull_match(msg, match_len, ntohs(nfs->priority), &fs->rule,
1597 actions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
1598 if (ofpacts_pull_openflow10(msg, actions_len, ofpacts)) {
1602 fs->cookie = nfs->cookie;
1603 fs->table_id = nfs->table_id;
1604 fs->duration_sec = ntohl(nfs->duration_sec);
1605 fs->duration_nsec = ntohl(nfs->duration_nsec);
1606 fs->idle_timeout = ntohs(nfs->idle_timeout);
1607 fs->hard_timeout = ntohs(nfs->hard_timeout);
1610 if (flow_age_extension) {
1611 if (nfs->idle_age) {
1612 fs->idle_age = ntohs(nfs->idle_age) - 1;
1614 if (nfs->hard_age) {
1615 fs->hard_age = ntohs(nfs->hard_age) - 1;
1618 fs->packet_count = ntohll(nfs->packet_count);
1619 fs->byte_count = ntohll(nfs->byte_count);
1624 fs->ofpacts = ofpacts->data;
1625 fs->ofpacts_len = ofpacts->size;
1630 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
1632 * We use this in situations where OVS internally uses UINT64_MAX to mean
1633 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
1635 unknown_to_zero(uint64_t count)
1637 return count != UINT64_MAX ? count : 0;
1640 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
1641 * those already present in the list of ofpbufs in 'replies'. 'replies' should
1642 * have been initialized with ofputil_start_stats_reply(). */
1644 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
1645 struct list *replies)
1647 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
1648 size_t start_ofs = reply->size;
1651 ofpraw_decode_partial(&raw, reply->data, reply->size);
1652 if (raw == OFPRAW_OFPST_FLOW_REPLY) {
1653 struct ofp10_flow_stats *ofs;
1655 ofpbuf_put_uninit(reply, sizeof *ofs);
1656 ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply);
1658 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
1659 ofs->length = htons(reply->size - start_ofs);
1660 ofs->table_id = fs->table_id;
1662 ofputil_cls_rule_to_ofp10_match(&fs->rule, &ofs->match);
1663 ofs->duration_sec = htonl(fs->duration_sec);
1664 ofs->duration_nsec = htonl(fs->duration_nsec);
1665 ofs->priority = htons(fs->rule.priority);
1666 ofs->idle_timeout = htons(fs->idle_timeout);
1667 ofs->hard_timeout = htons(fs->hard_timeout);
1668 memset(ofs->pad2, 0, sizeof ofs->pad2);
1669 put_32aligned_be64(&ofs->cookie, fs->cookie);
1670 put_32aligned_be64(&ofs->packet_count,
1671 htonll(unknown_to_zero(fs->packet_count)));
1672 put_32aligned_be64(&ofs->byte_count,
1673 htonll(unknown_to_zero(fs->byte_count)));
1674 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
1675 struct nx_flow_stats *nfs;
1678 ofpbuf_put_uninit(reply, sizeof *nfs);
1679 match_len = nx_put_match(reply, false, &fs->rule, 0, 0);
1680 ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply);
1682 nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
1683 nfs->length = htons(reply->size - start_ofs);
1684 nfs->table_id = fs->table_id;
1686 nfs->duration_sec = htonl(fs->duration_sec);
1687 nfs->duration_nsec = htonl(fs->duration_nsec);
1688 nfs->priority = htons(fs->rule.priority);
1689 nfs->idle_timeout = htons(fs->idle_timeout);
1690 nfs->hard_timeout = htons(fs->hard_timeout);
1691 nfs->idle_age = htons(fs->idle_age < 0 ? 0
1692 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
1694 nfs->hard_age = htons(fs->hard_age < 0 ? 0
1695 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
1697 nfs->match_len = htons(match_len);
1698 nfs->cookie = fs->cookie;
1699 nfs->packet_count = htonll(fs->packet_count);
1700 nfs->byte_count = htonll(fs->byte_count);
1705 ofpmp_postappend(replies, start_ofs);
1708 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
1709 * NXST_AGGREGATE reply matching 'request', and returns the message. */
1711 ofputil_encode_aggregate_stats_reply(
1712 const struct ofputil_aggregate_stats *stats,
1713 const struct ofp_header *request)
1715 struct ofp_aggregate_stats_reply *asr;
1716 uint64_t packet_count;
1717 uint64_t byte_count;
1721 ofpraw_decode(&raw, request);
1722 if (raw == OFPRAW_OFPST_AGGREGATE_REQUEST) {
1723 packet_count = unknown_to_zero(stats->packet_count);
1724 byte_count = unknown_to_zero(stats->byte_count);
1726 packet_count = stats->packet_count;
1727 byte_count = stats->byte_count;
1730 msg = ofpraw_alloc_stats_reply(request, 0);
1731 asr = ofpbuf_put_zeros(msg, sizeof *asr);
1732 put_32aligned_be64(&asr->packet_count, htonll(packet_count));
1733 put_32aligned_be64(&asr->byte_count, htonll(byte_count));
1734 asr->flow_count = htonl(stats->flow_count);
1740 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
1741 const struct ofp_header *reply)
1743 struct ofp_aggregate_stats_reply *asr;
1746 ofpbuf_use_const(&msg, reply, ntohs(reply->length));
1747 ofpraw_pull_assert(&msg);
1750 stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
1751 stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
1752 stats->flow_count = ntohl(asr->flow_count);
1757 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
1758 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
1759 * an OpenFlow error code. */
1761 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
1762 const struct ofp_header *oh)
1767 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1768 raw = ofpraw_pull_assert(&b);
1769 if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
1770 const struct ofp_flow_removed *ofr;
1772 ofr = ofpbuf_pull(&b, sizeof *ofr);
1774 ofputil_cls_rule_from_ofp10_match(&ofr->match, ntohs(ofr->priority),
1776 fr->cookie = ofr->cookie;
1777 fr->reason = ofr->reason;
1778 fr->duration_sec = ntohl(ofr->duration_sec);
1779 fr->duration_nsec = ntohl(ofr->duration_nsec);
1780 fr->idle_timeout = ntohs(ofr->idle_timeout);
1781 fr->packet_count = ntohll(ofr->packet_count);
1782 fr->byte_count = ntohll(ofr->byte_count);
1783 } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
1784 struct nx_flow_removed *nfr;
1787 nfr = ofpbuf_pull(&b, sizeof *nfr);
1788 error = nx_pull_match(&b, ntohs(nfr->match_len), ntohs(nfr->priority),
1789 &fr->rule, NULL, NULL);
1794 return OFPERR_OFPBRC_BAD_LEN;
1797 fr->cookie = nfr->cookie;
1798 fr->reason = nfr->reason;
1799 fr->duration_sec = ntohl(nfr->duration_sec);
1800 fr->duration_nsec = ntohl(nfr->duration_nsec);
1801 fr->idle_timeout = ntohs(nfr->idle_timeout);
1802 fr->packet_count = ntohll(nfr->packet_count);
1803 fr->byte_count = ntohll(nfr->byte_count);
1811 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
1812 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
1815 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
1816 enum ofputil_protocol protocol)
1821 case OFPUTIL_P_OF10:
1822 case OFPUTIL_P_OF10_TID: {
1823 struct ofp_flow_removed *ofr;
1825 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
1827 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
1828 ofputil_cls_rule_to_ofp10_match(&fr->rule, &ofr->match);
1829 ofr->cookie = fr->cookie;
1830 ofr->priority = htons(fr->rule.priority);
1831 ofr->reason = fr->reason;
1832 ofr->duration_sec = htonl(fr->duration_sec);
1833 ofr->duration_nsec = htonl(fr->duration_nsec);
1834 ofr->idle_timeout = htons(fr->idle_timeout);
1835 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
1836 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
1841 case OFPUTIL_P_NXM_TID: {
1842 struct nx_flow_removed *nfr;
1845 msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
1846 htonl(0), NXM_TYPICAL_LEN);
1847 nfr = ofpbuf_put_zeros(msg, sizeof *nfr);
1848 match_len = nx_put_match(msg, false, &fr->rule, 0, 0);
1851 nfr->cookie = fr->cookie;
1852 nfr->priority = htons(fr->rule.priority);
1853 nfr->reason = fr->reason;
1854 nfr->duration_sec = htonl(fr->duration_sec);
1855 nfr->duration_nsec = htonl(fr->duration_nsec);
1856 nfr->idle_timeout = htons(fr->idle_timeout);
1857 nfr->match_len = htons(match_len);
1858 nfr->packet_count = htonll(fr->packet_count);
1859 nfr->byte_count = htonll(fr->byte_count);
1863 case OFPUTIL_P_OF12:
1872 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
1873 const struct ofp_header *oh)
1878 memset(pin, 0, sizeof *pin);
1880 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1881 raw = ofpraw_pull_assert(&b);
1882 if (raw == OFPRAW_OFPT10_PACKET_IN) {
1883 const struct ofp_packet_in *opi;
1885 opi = ofpbuf_pull(&b, offsetof(struct ofp_packet_in, data));
1887 pin->packet = opi->data;
1888 pin->packet_len = b.size;
1890 pin->fmd.in_port = ntohs(opi->in_port);
1891 pin->reason = opi->reason;
1892 pin->buffer_id = ntohl(opi->buffer_id);
1893 pin->total_len = ntohs(opi->total_len);
1894 } else if (raw == OFPRAW_NXT_PACKET_IN) {
1895 const struct nx_packet_in *npi;
1896 struct cls_rule rule;
1899 npi = ofpbuf_pull(&b, sizeof *npi);
1900 error = nx_pull_match_loose(&b, ntohs(npi->match_len), 0, &rule, NULL,
1906 if (!ofpbuf_try_pull(&b, 2)) {
1907 return OFPERR_OFPBRC_BAD_LEN;
1910 pin->packet = b.data;
1911 pin->packet_len = b.size;
1912 pin->reason = npi->reason;
1913 pin->table_id = npi->table_id;
1914 pin->cookie = npi->cookie;
1916 pin->fmd.in_port = rule.flow.in_port;
1918 pin->fmd.tun_id = rule.flow.tun_id;
1919 pin->fmd.tun_id_mask = rule.wc.tun_id_mask;
1921 pin->fmd.metadata = rule.flow.metadata;
1922 pin->fmd.metadata_mask = rule.wc.metadata_mask;
1924 memcpy(pin->fmd.regs, rule.flow.regs, sizeof pin->fmd.regs);
1925 memcpy(pin->fmd.reg_masks, rule.wc.reg_masks,
1926 sizeof pin->fmd.reg_masks);
1928 pin->buffer_id = ntohl(npi->buffer_id);
1929 pin->total_len = ntohs(npi->total_len);
1937 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
1938 * in the format specified by 'packet_in_format'. */
1940 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
1941 enum nx_packet_in_format packet_in_format)
1943 size_t send_len = MIN(pin->send_len, pin->packet_len);
1944 struct ofpbuf *packet;
1946 /* Add OFPT_PACKET_IN. */
1947 if (packet_in_format == NXPIF_OPENFLOW10) {
1948 struct ofp_packet_in *opi;
1950 packet = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
1951 htonl(0), send_len);
1952 opi = ofpbuf_put_zeros(packet, offsetof(struct ofp_packet_in, data));
1953 opi->total_len = htons(pin->total_len);
1954 opi->in_port = htons(pin->fmd.in_port);
1955 opi->reason = pin->reason;
1956 opi->buffer_id = htonl(pin->buffer_id);
1958 ofpbuf_put(packet, pin->packet, send_len);
1959 } else if (packet_in_format == NXPIF_NXM) {
1960 struct nx_packet_in *npi;
1961 struct cls_rule rule;
1965 cls_rule_init_catchall(&rule, 0);
1966 cls_rule_set_tun_id_masked(&rule, pin->fmd.tun_id,
1967 pin->fmd.tun_id_mask);
1968 cls_rule_set_metadata_masked(&rule, pin->fmd.metadata,
1969 pin->fmd.metadata_mask);
1972 for (i = 0; i < FLOW_N_REGS; i++) {
1973 cls_rule_set_reg_masked(&rule, i, pin->fmd.regs[i],
1974 pin->fmd.reg_masks[i]);
1977 cls_rule_set_in_port(&rule, pin->fmd.in_port);
1979 /* The final argument is just an estimate of the space required. */
1980 packet = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, OFP10_VERSION,
1981 htonl(0), (sizeof(struct flow_metadata) * 2
1983 ofpbuf_put_zeros(packet, sizeof *npi);
1984 match_len = nx_put_match(packet, false, &rule, 0, 0);
1985 ofpbuf_put_zeros(packet, 2);
1986 ofpbuf_put(packet, pin->packet, send_len);
1989 npi->buffer_id = htonl(pin->buffer_id);
1990 npi->total_len = htons(pin->total_len);
1991 npi->reason = pin->reason;
1992 npi->table_id = pin->table_id;
1993 npi->cookie = pin->cookie;
1994 npi->match_len = htons(match_len);
1998 ofpmsg_update_length(packet);
2004 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason)
2006 static char s[INT_STRLEN(int) + 1];
2013 case OFPR_INVALID_TTL:
2014 return "invalid_ttl";
2016 case OFPR_N_REASONS:
2018 sprintf(s, "%d", (int) reason);
2024 ofputil_packet_in_reason_from_string(const char *s,
2025 enum ofp_packet_in_reason *reason)
2029 for (i = 0; i < OFPR_N_REASONS; i++) {
2030 if (!strcasecmp(s, ofputil_packet_in_reason_to_string(i))) {
2038 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
2041 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
2042 * message's actions. The caller must initialize 'ofpacts' and retains
2043 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
2045 * Returns 0 if successful, otherwise an OFPERR_* value. */
2047 ofputil_decode_packet_out(struct ofputil_packet_out *po,
2048 const struct ofp_header *oh,
2049 struct ofpbuf *ofpacts)
2051 const struct ofp_packet_out *opo;
2056 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2057 raw = ofpraw_pull_assert(&b);
2058 assert(raw == OFPRAW_OFPT10_PACKET_OUT);
2060 opo = ofpbuf_pull(&b, sizeof *opo);
2061 po->buffer_id = ntohl(opo->buffer_id);
2062 po->in_port = ntohs(opo->in_port);
2063 if (po->in_port >= OFPP_MAX && po->in_port != OFPP_LOCAL
2064 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
2065 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
2067 return OFPERR_NXBRC_BAD_IN_PORT;
2070 error = ofpacts_pull_openflow10(&b, ntohs(opo->actions_len), ofpacts);
2074 po->ofpacts = ofpacts->data;
2075 po->ofpacts_len = ofpacts->size;
2077 if (po->buffer_id == UINT32_MAX) {
2078 po->packet = b.data;
2079 po->packet_len = b.size;
2088 /* ofputil_phy_port */
2090 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
2091 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2092 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2093 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2094 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2095 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2096 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2097 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2099 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
2100 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
2101 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
2102 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
2103 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
2104 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
2106 static enum netdev_features
2107 netdev_port_features_from_ofp10(ovs_be32 ofp10_)
2109 uint32_t ofp10 = ntohl(ofp10_);
2110 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
2114 netdev_port_features_to_ofp10(enum netdev_features features)
2116 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
2119 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2120 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2121 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2122 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2123 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2124 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2125 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2126 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
2127 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
2128 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
2129 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
2130 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
2131 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
2132 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
2133 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
2134 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
2136 static enum netdev_features
2137 netdev_port_features_from_ofp11(ovs_be32 ofp11)
2139 return ntohl(ofp11) & 0xffff;
2143 netdev_port_features_to_ofp11(enum netdev_features features)
2145 return htonl(features & 0xffff);
2149 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
2150 const struct ofp10_phy_port *opp)
2152 memset(pp, 0, sizeof *pp);
2154 pp->port_no = ntohs(opp->port_no);
2155 memcpy(pp->hw_addr, opp->hw_addr, OFP_ETH_ALEN);
2156 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
2158 pp->config = ntohl(opp->config) & OFPPC10_ALL;
2159 pp->state = ntohl(opp->state) & OFPPS10_ALL;
2161 pp->curr = netdev_port_features_from_ofp10(opp->curr);
2162 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
2163 pp->supported = netdev_port_features_from_ofp10(opp->supported);
2164 pp->peer = netdev_port_features_from_ofp10(opp->peer);
2166 pp->curr_speed = netdev_features_to_bps(pp->curr) / 1000;
2167 pp->max_speed = netdev_features_to_bps(pp->supported) / 1000;
2173 ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
2174 const struct ofp11_port *op)
2178 memset(pp, 0, sizeof *pp);
2180 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
2184 memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN);
2185 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
2187 pp->config = ntohl(op->config) & OFPPC11_ALL;
2188 pp->state = ntohl(op->state) & OFPPC11_ALL;
2190 pp->curr = netdev_port_features_from_ofp11(op->curr);
2191 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
2192 pp->supported = netdev_port_features_from_ofp11(op->supported);
2193 pp->peer = netdev_port_features_from_ofp11(op->peer);
2195 pp->curr_speed = ntohl(op->curr_speed);
2196 pp->max_speed = ntohl(op->max_speed);
2202 ofputil_get_phy_port_size(enum ofp_version ofp_version)
2204 switch (ofp_version) {
2206 return sizeof(struct ofp10_phy_port);
2209 return sizeof(struct ofp11_port);
2216 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
2217 struct ofp10_phy_port *opp)
2219 memset(opp, 0, sizeof *opp);
2221 opp->port_no = htons(pp->port_no);
2222 memcpy(opp->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2223 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2225 opp->config = htonl(pp->config & OFPPC10_ALL);
2226 opp->state = htonl(pp->state & OFPPS10_ALL);
2228 opp->curr = netdev_port_features_to_ofp10(pp->curr);
2229 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
2230 opp->supported = netdev_port_features_to_ofp10(pp->supported);
2231 opp->peer = netdev_port_features_to_ofp10(pp->peer);
2235 ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
2236 struct ofp11_port *op)
2238 memset(op, 0, sizeof *op);
2240 op->port_no = ofputil_port_to_ofp11(pp->port_no);
2241 memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2242 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2244 op->config = htonl(pp->config & OFPPC11_ALL);
2245 op->state = htonl(pp->state & OFPPS11_ALL);
2247 op->curr = netdev_port_features_to_ofp11(pp->curr);
2248 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
2249 op->supported = netdev_port_features_to_ofp11(pp->supported);
2250 op->peer = netdev_port_features_to_ofp11(pp->peer);
2252 op->curr_speed = htonl(pp->curr_speed);
2253 op->max_speed = htonl(pp->max_speed);
2257 ofputil_put_phy_port(enum ofp_version ofp_version,
2258 const struct ofputil_phy_port *pp, struct ofpbuf *b)
2260 switch (ofp_version) {
2261 case OFP10_VERSION: {
2262 struct ofp10_phy_port *opp;
2263 if (b->size + sizeof *opp <= UINT16_MAX) {
2264 opp = ofpbuf_put_uninit(b, sizeof *opp);
2265 ofputil_encode_ofp10_phy_port(pp, opp);
2271 case OFP12_VERSION: {
2272 struct ofp11_port *op;
2273 if (b->size + sizeof *op <= UINT16_MAX) {
2274 op = ofpbuf_put_uninit(b, sizeof *op);
2275 ofputil_encode_ofp11_port(pp, op);
2286 ofputil_append_port_desc_stats_reply(enum ofp_version ofp_version,
2287 const struct ofputil_phy_port *pp,
2288 struct list *replies)
2290 switch (ofp_version) {
2291 case OFP10_VERSION: {
2292 struct ofp10_phy_port *opp;
2294 opp = ofpmp_append(replies, sizeof *opp);
2295 ofputil_encode_ofp10_phy_port(pp, opp);
2300 case OFP12_VERSION: {
2301 struct ofp11_port *op;
2303 op = ofpmp_append(replies, sizeof *op);
2304 ofputil_encode_ofp11_port(pp, op);
2313 /* ofputil_switch_features */
2315 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
2316 OFPC_IP_REASM | OFPC_QUEUE_STATS)
2317 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
2318 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
2319 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
2320 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
2321 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
2322 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
2324 struct ofputil_action_bit_translation {
2325 enum ofputil_action_bitmap ofputil_bit;
2329 static const struct ofputil_action_bit_translation of10_action_bits[] = {
2330 { OFPUTIL_A_OUTPUT, OFPAT10_OUTPUT },
2331 { OFPUTIL_A_SET_VLAN_VID, OFPAT10_SET_VLAN_VID },
2332 { OFPUTIL_A_SET_VLAN_PCP, OFPAT10_SET_VLAN_PCP },
2333 { OFPUTIL_A_STRIP_VLAN, OFPAT10_STRIP_VLAN },
2334 { OFPUTIL_A_SET_DL_SRC, OFPAT10_SET_DL_SRC },
2335 { OFPUTIL_A_SET_DL_DST, OFPAT10_SET_DL_DST },
2336 { OFPUTIL_A_SET_NW_SRC, OFPAT10_SET_NW_SRC },
2337 { OFPUTIL_A_SET_NW_DST, OFPAT10_SET_NW_DST },
2338 { OFPUTIL_A_SET_NW_TOS, OFPAT10_SET_NW_TOS },
2339 { OFPUTIL_A_SET_TP_SRC, OFPAT10_SET_TP_SRC },
2340 { OFPUTIL_A_SET_TP_DST, OFPAT10_SET_TP_DST },
2341 { OFPUTIL_A_ENQUEUE, OFPAT10_ENQUEUE },
2345 static const struct ofputil_action_bit_translation of11_action_bits[] = {
2346 { OFPUTIL_A_OUTPUT, OFPAT11_OUTPUT },
2347 { OFPUTIL_A_SET_VLAN_VID, OFPAT11_SET_VLAN_VID },
2348 { OFPUTIL_A_SET_VLAN_PCP, OFPAT11_SET_VLAN_PCP },
2349 { OFPUTIL_A_SET_DL_SRC, OFPAT11_SET_DL_SRC },
2350 { OFPUTIL_A_SET_DL_DST, OFPAT11_SET_DL_DST },
2351 { OFPUTIL_A_SET_NW_SRC, OFPAT11_SET_NW_SRC },
2352 { OFPUTIL_A_SET_NW_DST, OFPAT11_SET_NW_DST },
2353 { OFPUTIL_A_SET_NW_TOS, OFPAT11_SET_NW_TOS },
2354 { OFPUTIL_A_SET_NW_ECN, OFPAT11_SET_NW_ECN },
2355 { OFPUTIL_A_SET_TP_SRC, OFPAT11_SET_TP_SRC },
2356 { OFPUTIL_A_SET_TP_DST, OFPAT11_SET_TP_DST },
2357 { OFPUTIL_A_COPY_TTL_OUT, OFPAT11_COPY_TTL_OUT },
2358 { OFPUTIL_A_COPY_TTL_IN, OFPAT11_COPY_TTL_IN },
2359 { OFPUTIL_A_SET_MPLS_LABEL, OFPAT11_SET_MPLS_LABEL },
2360 { OFPUTIL_A_SET_MPLS_TC, OFPAT11_SET_MPLS_TC },
2361 { OFPUTIL_A_SET_MPLS_TTL, OFPAT11_SET_MPLS_TTL },
2362 { OFPUTIL_A_DEC_MPLS_TTL, OFPAT11_DEC_MPLS_TTL },
2363 { OFPUTIL_A_PUSH_VLAN, OFPAT11_PUSH_VLAN },
2364 { OFPUTIL_A_POP_VLAN, OFPAT11_POP_VLAN },
2365 { OFPUTIL_A_PUSH_MPLS, OFPAT11_PUSH_MPLS },
2366 { OFPUTIL_A_POP_MPLS, OFPAT11_POP_MPLS },
2367 { OFPUTIL_A_SET_QUEUE, OFPAT11_SET_QUEUE },
2368 { OFPUTIL_A_GROUP, OFPAT11_GROUP },
2369 { OFPUTIL_A_SET_NW_TTL, OFPAT11_SET_NW_TTL },
2370 { OFPUTIL_A_DEC_NW_TTL, OFPAT11_DEC_NW_TTL },
2374 static const struct ofputil_action_bit_translation of12_action_bits[] = {
2375 { OFPUTIL_A_OUTPUT, OFPAT12_OUTPUT },
2376 { OFPUTIL_A_COPY_TTL_OUT, OFPAT12_COPY_TTL_OUT },
2377 { OFPUTIL_A_COPY_TTL_IN, OFPAT12_COPY_TTL_IN },
2378 { OFPUTIL_A_SET_MPLS_TTL, OFPAT12_SET_MPLS_TTL },
2379 { OFPUTIL_A_DEC_MPLS_TTL, OFPAT12_DEC_MPLS_TTL },
2380 { OFPUTIL_A_PUSH_VLAN, OFPAT12_PUSH_VLAN },
2381 { OFPUTIL_A_POP_VLAN, OFPAT12_POP_VLAN },
2382 { OFPUTIL_A_PUSH_MPLS, OFPAT12_PUSH_MPLS },
2383 { OFPUTIL_A_POP_MPLS, OFPAT12_POP_MPLS },
2384 { OFPUTIL_A_SET_QUEUE, OFPAT12_SET_QUEUE },
2385 { OFPUTIL_A_GROUP, OFPAT12_GROUP },
2386 { OFPUTIL_A_SET_NW_TTL, OFPAT12_SET_NW_TTL },
2387 { OFPUTIL_A_DEC_NW_TTL, OFPAT12_DEC_NW_TTL },
2391 static enum ofputil_action_bitmap
2392 decode_action_bits(ovs_be32 of_actions,
2393 const struct ofputil_action_bit_translation *x)
2395 enum ofputil_action_bitmap ofputil_actions;
2397 ofputil_actions = 0;
2398 for (; x->ofputil_bit; x++) {
2399 if (of_actions & htonl(1u << x->of_bit)) {
2400 ofputil_actions |= x->ofputil_bit;
2403 return ofputil_actions;
2407 ofputil_capabilities_mask(enum ofp_version ofp_version)
2409 /* Handle capabilities whose bit is unique for all Open Flow versions */
2410 switch (ofp_version) {
2413 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
2415 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
2417 /* Caller needs to check osf->header.version itself */
2422 /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an
2423 * abstract representation in '*features'. Initializes '*b' to iterate over
2424 * the OpenFlow port structures following 'osf' with later calls to
2425 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an
2426 * OFPERR_* value. */
2428 ofputil_decode_switch_features(const struct ofp_header *oh,
2429 struct ofputil_switch_features *features,
2432 const struct ofp_switch_features *osf;
2435 ofpbuf_use_const(b, oh, ntohs(oh->length));
2436 raw = ofpraw_pull_assert(b);
2438 osf = ofpbuf_pull(b, sizeof *osf);
2439 features->datapath_id = ntohll(osf->datapath_id);
2440 features->n_buffers = ntohl(osf->n_buffers);
2441 features->n_tables = osf->n_tables;
2443 features->capabilities = ntohl(osf->capabilities) &
2444 ofputil_capabilities_mask(oh->version);
2446 if (b->size % ofputil_get_phy_port_size(oh->version)) {
2447 return OFPERR_OFPBRC_BAD_LEN;
2450 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
2451 if (osf->capabilities & htonl(OFPC10_STP)) {
2452 features->capabilities |= OFPUTIL_C_STP;
2454 features->actions = decode_action_bits(osf->actions, of10_action_bits);
2455 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY) {
2456 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
2457 features->capabilities |= OFPUTIL_C_GROUP_STATS;
2459 switch ((enum ofp_version)oh->version) {
2461 features->actions = decode_action_bits(htonl(UINT32_MAX),
2465 features->actions = decode_action_bits(htonl(UINT32_MAX),
2473 return OFPERR_OFPBRC_BAD_VERSION;
2479 /* Returns true if the maximum number of ports are in 'oh'. */
2481 max_ports_in_features(const struct ofp_header *oh)
2483 size_t pp_size = ofputil_get_phy_port_size(oh->version);
2484 return ntohs(oh->length) + pp_size > UINT16_MAX;
2487 /* Given a buffer 'b' that contains a Features Reply message, checks if
2488 * it contains the maximum number of ports that will fit. If so, it
2489 * returns true and removes the ports from the message. The caller
2490 * should then send an OFPST_PORT_DESC stats request to get the ports,
2491 * since the switch may have more ports than could be represented in the
2492 * Features Reply. Otherwise, returns false.
2495 ofputil_switch_features_ports_trunc(struct ofpbuf *b)
2497 struct ofp_header *oh = b->data;
2499 if (max_ports_in_features(oh)) {
2500 /* Remove all the ports. */
2501 b->size = (sizeof(struct ofp_header)
2502 + sizeof(struct ofp_switch_features));
2503 ofpmsg_update_length(b);
2512 encode_action_bits(enum ofputil_action_bitmap ofputil_actions,
2513 const struct ofputil_action_bit_translation *x)
2515 uint32_t of_actions;
2518 for (; x->ofputil_bit; x++) {
2519 if (ofputil_actions & x->ofputil_bit) {
2520 of_actions |= 1 << x->of_bit;
2523 return htonl(of_actions);
2526 /* Returns a buffer owned by the caller that encodes 'features' in the format
2527 * required by 'protocol' with the given 'xid'. The caller should append port
2528 * information to the buffer with subsequent calls to
2529 * ofputil_put_switch_features_port(). */
2531 ofputil_encode_switch_features(const struct ofputil_switch_features *features,
2532 enum ofputil_protocol protocol, ovs_be32 xid)
2534 struct ofp_switch_features *osf;
2536 enum ofp_version version;
2539 version = ofputil_protocol_to_ofp_version(protocol);
2542 raw = OFPRAW_OFPT10_FEATURES_REPLY;
2546 raw = OFPRAW_OFPT11_FEATURES_REPLY;
2551 b = ofpraw_alloc_xid(raw, version, xid, 0);
2552 osf = ofpbuf_put_zeros(b, sizeof *osf);
2553 osf->datapath_id = htonll(features->datapath_id);
2554 osf->n_buffers = htonl(features->n_buffers);
2555 osf->n_tables = features->n_tables;
2557 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
2558 osf->capabilities = htonl(features->capabilities &
2559 ofputil_capabilities_mask(version));
2562 if (features->capabilities & OFPUTIL_C_STP) {
2563 osf->capabilities |= htonl(OFPC10_STP);
2565 osf->actions = encode_action_bits(features->actions, of10_action_bits);
2569 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
2570 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
2580 /* Encodes 'pp' into the format required by the switch_features message already
2581 * in 'b', which should have been returned by ofputil_encode_switch_features(),
2582 * and appends the encoded version to 'b'. */
2584 ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
2587 const struct ofp_header *oh = b->data;
2589 ofputil_put_phy_port(oh->version, pp, b);
2592 /* ofputil_port_status */
2594 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
2595 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
2597 ofputil_decode_port_status(const struct ofp_header *oh,
2598 struct ofputil_port_status *ps)
2600 const struct ofp_port_status *ops;
2604 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2605 ofpraw_pull_assert(&b);
2606 ops = ofpbuf_pull(&b, sizeof *ops);
2608 if (ops->reason != OFPPR_ADD &&
2609 ops->reason != OFPPR_DELETE &&
2610 ops->reason != OFPPR_MODIFY) {
2611 return OFPERR_NXBRC_BAD_REASON;
2613 ps->reason = ops->reason;
2615 retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
2616 assert(retval != EOF);
2620 /* Converts the abstract form of a "port status" message in '*ps' into an
2621 * OpenFlow message suitable for 'protocol', and returns that encoded form in
2622 * a buffer owned by the caller. */
2624 ofputil_encode_port_status(const struct ofputil_port_status *ps,
2625 enum ofputil_protocol protocol)
2627 struct ofp_port_status *ops;
2629 enum ofp_version version;
2632 version = ofputil_protocol_to_ofp_version(protocol);
2635 raw = OFPRAW_OFPT10_PORT_STATUS;
2640 raw = OFPRAW_OFPT11_PORT_STATUS;
2647 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
2648 ops = ofpbuf_put_zeros(b, sizeof *ops);
2649 ops->reason = ps->reason;
2650 ofputil_put_phy_port(version, &ps->desc, b);
2651 ofpmsg_update_length(b);
2655 /* ofputil_port_mod */
2657 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
2658 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
2660 ofputil_decode_port_mod(const struct ofp_header *oh,
2661 struct ofputil_port_mod *pm)
2666 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2667 raw = ofpraw_pull_assert(&b);
2669 if (raw == OFPRAW_OFPT10_PORT_MOD) {
2670 const struct ofp10_port_mod *opm = b.data;
2672 pm->port_no = ntohs(opm->port_no);
2673 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
2674 pm->config = ntohl(opm->config) & OFPPC10_ALL;
2675 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
2676 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
2677 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
2678 const struct ofp11_port_mod *opm = b.data;
2681 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
2686 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
2687 pm->config = ntohl(opm->config) & OFPPC11_ALL;
2688 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
2689 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
2691 return OFPERR_OFPBRC_BAD_TYPE;
2694 pm->config &= pm->mask;
2698 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
2699 * message suitable for 'protocol', and returns that encoded form in a buffer
2700 * owned by the caller. */
2702 ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
2703 enum ofputil_protocol protocol)
2705 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
2708 switch (ofp_version) {
2709 case OFP10_VERSION: {
2710 struct ofp10_port_mod *opm;
2712 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
2713 opm = ofpbuf_put_zeros(b, sizeof *opm);
2714 opm->port_no = htons(pm->port_no);
2715 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
2716 opm->config = htonl(pm->config & OFPPC10_ALL);
2717 opm->mask = htonl(pm->mask & OFPPC10_ALL);
2718 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
2722 case OFP11_VERSION: {
2723 struct ofp11_port_mod *opm;
2725 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
2726 opm = ofpbuf_put_zeros(b, sizeof *opm);
2727 opm->port_no = htonl(pm->port_no);
2728 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
2729 opm->config = htonl(pm->config & OFPPC11_ALL);
2730 opm->mask = htonl(pm->mask & OFPPC11_ALL);
2731 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
2743 /* ofputil_flow_monitor_request */
2745 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
2746 * ofputil_flow_monitor_request in 'rq'.
2748 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
2749 * message. Calling this function multiple times for a single 'msg' iterates
2750 * through the requests. The caller must initially leave 'msg''s layer
2751 * pointers null and not modify them between calls.
2753 * Returns 0 if successful, EOF if no requests were left in this 'msg',
2754 * otherwise an OFPERR_* value. */
2756 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
2759 struct nx_flow_monitor_request *nfmr;
2763 msg->l2 = msg->data;
2764 ofpraw_pull_assert(msg);
2771 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
2773 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %zu "
2774 "leftover bytes at end", msg->size);
2775 return OFPERR_OFPBRC_BAD_LEN;
2778 flags = ntohs(nfmr->flags);
2779 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
2780 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
2781 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
2782 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
2784 return OFPERR_NXBRC_FM_BAD_FLAGS;
2787 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
2788 return OFPERR_NXBRC_MUST_BE_ZERO;
2791 rq->id = ntohl(nfmr->id);
2793 rq->out_port = ntohs(nfmr->out_port);
2794 rq->table_id = nfmr->table_id;
2796 return nx_pull_match(msg, ntohs(nfmr->match_len), OFP_DEFAULT_PRIORITY,
2797 &rq->match, NULL, NULL);
2801 ofputil_append_flow_monitor_request(
2802 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
2804 struct nx_flow_monitor_request *nfmr;
2809 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
2812 start_ofs = msg->size;
2813 ofpbuf_put_zeros(msg, sizeof *nfmr);
2814 match_len = nx_put_match(msg, false, &rq->match, htonll(0), htonll(0));
2816 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
2817 nfmr->id = htonl(rq->id);
2818 nfmr->flags = htons(rq->flags);
2819 nfmr->out_port = htons(rq->out_port);
2820 nfmr->match_len = htons(match_len);
2821 nfmr->table_id = rq->table_id;
2824 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
2825 * into an abstract ofputil_flow_update in 'update'. The caller must have
2826 * initialized update->match to point to space allocated for a cls_rule.
2828 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
2829 * actions (except for NXFME_ABBREV, which never includes actions). The caller
2830 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
2831 * will point into the 'ofpacts' buffer.
2833 * Multiple flow updates can be packed into a single OpenFlow message. Calling
2834 * this function multiple times for a single 'msg' iterates through the
2835 * updates. The caller must initially leave 'msg''s layer pointers null and
2836 * not modify them between calls.
2838 * Returns 0 if successful, EOF if no updates were left in this 'msg',
2839 * otherwise an OFPERR_* value. */
2841 ofputil_decode_flow_update(struct ofputil_flow_update *update,
2842 struct ofpbuf *msg, struct ofpbuf *ofpacts)
2844 struct nx_flow_update_header *nfuh;
2845 unsigned int length;
2848 msg->l2 = msg->data;
2849 ofpraw_pull_assert(msg);
2856 if (msg->size < sizeof(struct nx_flow_update_header)) {
2861 update->event = ntohs(nfuh->event);
2862 length = ntohs(nfuh->length);
2863 if (length > msg->size || length % 8) {
2867 if (update->event == NXFME_ABBREV) {
2868 struct nx_flow_update_abbrev *nfua;
2870 if (length != sizeof *nfua) {
2874 nfua = ofpbuf_pull(msg, sizeof *nfua);
2875 update->xid = nfua->xid;
2877 } else if (update->event == NXFME_ADDED
2878 || update->event == NXFME_DELETED
2879 || update->event == NXFME_MODIFIED) {
2880 struct nx_flow_update_full *nfuf;
2881 unsigned int actions_len;
2882 unsigned int match_len;
2885 if (length < sizeof *nfuf) {
2889 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
2890 match_len = ntohs(nfuf->match_len);
2891 if (sizeof *nfuf + match_len > length) {
2895 update->reason = ntohs(nfuf->reason);
2896 update->idle_timeout = ntohs(nfuf->idle_timeout);
2897 update->hard_timeout = ntohs(nfuf->hard_timeout);
2898 update->table_id = nfuf->table_id;
2899 update->cookie = nfuf->cookie;
2901 error = nx_pull_match(msg, match_len, ntohs(nfuf->priority),
2902 update->match, NULL, NULL);
2907 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
2908 error = ofpacts_pull_openflow10(msg, actions_len, ofpacts);
2913 update->ofpacts = ofpacts->data;
2914 update->ofpacts_len = ofpacts->size;
2917 VLOG_WARN_RL(&bad_ofmsg_rl,
2918 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
2919 ntohs(nfuh->event));
2920 return OFPERR_OFPET_BAD_REQUEST;
2924 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %zu "
2925 "leftover bytes at end", msg->size);
2926 return OFPERR_OFPBRC_BAD_LEN;
2930 ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
2932 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
2934 return ntohl(cancel->id);
2938 ofputil_encode_flow_monitor_cancel(uint32_t id)
2940 struct nx_flow_monitor_cancel *nfmc;
2943 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
2944 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
2945 nfmc->id = htonl(id);
2950 ofputil_start_flow_update(struct list *replies)
2954 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
2958 list_push_back(replies, &msg->list_node);
2962 ofputil_append_flow_update(const struct ofputil_flow_update *update,
2963 struct list *replies)
2965 struct nx_flow_update_header *nfuh;
2969 msg = ofpbuf_from_list(list_back(replies));
2970 start_ofs = msg->size;
2972 if (update->event == NXFME_ABBREV) {
2973 struct nx_flow_update_abbrev *nfua;
2975 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
2976 nfua->xid = update->xid;
2978 struct nx_flow_update_full *nfuf;
2981 ofpbuf_put_zeros(msg, sizeof *nfuf);
2982 match_len = nx_put_match(msg, false, update->match,
2983 htonll(0), htonll(0));
2984 ofpacts_put_openflow10(update->ofpacts, update->ofpacts_len, msg);
2986 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
2987 nfuf->reason = htons(update->reason);
2988 nfuf->priority = htons(update->match->priority);
2989 nfuf->idle_timeout = htons(update->idle_timeout);
2990 nfuf->hard_timeout = htons(update->hard_timeout);
2991 nfuf->match_len = htons(match_len);
2992 nfuf->table_id = update->table_id;
2993 nfuf->cookie = update->cookie;
2996 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
2997 nfuh->length = htons(msg->size - start_ofs);
2998 nfuh->event = htons(update->event);
3000 ofpmp_postappend(replies, start_ofs);
3004 ofputil_encode_packet_out(const struct ofputil_packet_out *po)
3006 struct ofp_packet_out *opo;
3011 size = po->ofpacts_len;
3012 if (po->buffer_id == UINT32_MAX) {
3013 size += po->packet_len;
3016 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
3017 ofpbuf_put_zeros(msg, sizeof *opo);
3018 actions_ofs = msg->size;
3019 ofpacts_put_openflow10(po->ofpacts, po->ofpacts_len, msg);
3022 opo->buffer_id = htonl(po->buffer_id);
3023 opo->in_port = htons(po->in_port);
3024 opo->actions_len = htons(msg->size - actions_ofs);
3026 if (po->buffer_id == UINT32_MAX) {
3027 ofpbuf_put(msg, po->packet, po->packet_len);
3030 ofpmsg_update_length(msg);
3035 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
3037 make_echo_request(void)
3039 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, OFP10_VERSION,
3043 /* Creates and returns an OFPT_ECHO_REPLY message matching the
3044 * OFPT_ECHO_REQUEST message in 'rq'. */
3046 make_echo_reply(const struct ofp_header *rq)
3048 struct ofpbuf rq_buf;
3049 struct ofpbuf *reply;
3051 ofpbuf_use_const(&rq_buf, rq, ntohs(rq->length));
3052 ofpraw_pull_assert(&rq_buf);
3054 reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY, rq, rq_buf.size);
3055 ofpbuf_put(reply, rq_buf.data, rq_buf.size);
3060 ofputil_encode_barrier_request(void)
3062 return ofpraw_alloc(OFPRAW_OFPT10_BARRIER_REQUEST, OFP10_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"
3336 /* "Normalizes" the wildcards in 'rule'. That means:
3338 * 1. If the type of level N is known, then only the valid fields for that
3339 * level may be specified. For example, ARP does not have a TOS field,
3340 * so nw_tos must be wildcarded if 'rule' specifies an ARP flow.
3341 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
3342 * ipv6_dst (and other fields) must be wildcarded if 'rule' specifies an
3345 * 2. If the type of level N is not known (or not understood by Open
3346 * vSwitch), then no fields at all for that level may be specified. For
3347 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
3348 * L4 fields tp_src and tp_dst must be wildcarded if 'rule' specifies an
3352 ofputil_normalize_rule(struct cls_rule *rule)
3355 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
3356 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
3357 MAY_NW_PROTO = 1 << 2, /* nw_proto */
3358 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
3359 MAY_ARP_SHA = 1 << 4, /* arp_sha */
3360 MAY_ARP_THA = 1 << 5, /* arp_tha */
3361 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
3362 MAY_ND_TARGET = 1 << 7 /* nd_target */
3365 struct flow_wildcards wc;
3367 /* Figure out what fields may be matched. */
3368 if (rule->flow.dl_type == htons(ETH_TYPE_IP)) {
3369 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
3370 if (rule->flow.nw_proto == IPPROTO_TCP ||
3371 rule->flow.nw_proto == IPPROTO_UDP ||
3372 rule->flow.nw_proto == IPPROTO_ICMP) {
3373 may_match |= MAY_TP_ADDR;
3375 } else if (rule->flow.dl_type == htons(ETH_TYPE_IPV6)) {
3376 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
3377 if (rule->flow.nw_proto == IPPROTO_TCP ||
3378 rule->flow.nw_proto == IPPROTO_UDP) {
3379 may_match |= MAY_TP_ADDR;
3380 } else if (rule->flow.nw_proto == IPPROTO_ICMPV6) {
3381 may_match |= MAY_TP_ADDR;
3382 if (rule->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
3383 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
3384 } else if (rule->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
3385 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
3388 } else if (rule->flow.dl_type == htons(ETH_TYPE_ARP)) {
3389 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
3394 /* Clear the fields that may not be matched. */
3396 if (!(may_match & MAY_NW_ADDR)) {
3397 wc.nw_src_mask = wc.nw_dst_mask = htonl(0);
3399 if (!(may_match & MAY_TP_ADDR)) {
3400 wc.tp_src_mask = wc.tp_dst_mask = htons(0);
3402 if (!(may_match & MAY_NW_PROTO)) {
3403 wc.wildcards |= FWW_NW_PROTO;
3405 if (!(may_match & MAY_IPVx)) {
3406 wc.wildcards |= FWW_NW_DSCP;
3407 wc.wildcards |= FWW_NW_ECN;
3408 wc.wildcards |= FWW_NW_TTL;
3410 if (!(may_match & MAY_ARP_SHA)) {
3411 memset(wc.arp_sha_mask, 0, ETH_ADDR_LEN);
3413 if (!(may_match & MAY_ARP_THA)) {
3414 memset(wc.arp_tha_mask, 0, ETH_ADDR_LEN);
3416 if (!(may_match & MAY_IPV6)) {
3417 wc.ipv6_src_mask = wc.ipv6_dst_mask = in6addr_any;
3418 wc.ipv6_label_mask = htonl(0);
3420 if (!(may_match & MAY_ND_TARGET)) {
3421 wc.nd_target_mask = in6addr_any;
3424 /* Log any changes. */
3425 if (!flow_wildcards_equal(&wc, &rule->wc)) {
3426 bool log = !VLOG_DROP_INFO(&bad_ofmsg_rl);
3427 char *pre = log ? cls_rule_to_string(rule) : NULL;
3430 cls_rule_zero_wildcarded_fields(rule);
3433 char *post = cls_rule_to_string(rule);
3434 VLOG_INFO("normalization changed ofp_match, details:");
3435 VLOG_INFO(" pre: %s", pre);
3436 VLOG_INFO("post: %s", post);
3443 /* Parses a key or a key-value pair from '*stringp'.
3445 * On success: Stores the key into '*keyp'. Stores the value, if present, into
3446 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
3447 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
3448 * are substrings of '*stringp' created by replacing some of its bytes by null
3449 * terminators. Returns true.
3451 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
3452 * NULL and returns false. */
3454 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
3456 char *pos, *key, *value;
3460 pos += strspn(pos, ", \t\r\n");
3462 *keyp = *valuep = NULL;
3467 key_len = strcspn(pos, ":=(, \t\r\n");
3468 if (key[key_len] == ':' || key[key_len] == '=') {
3469 /* The value can be separated by a colon. */
3472 value = key + key_len + 1;
3473 value_len = strcspn(value, ", \t\r\n");
3474 pos = value + value_len + (value[value_len] != '\0');
3475 value[value_len] = '\0';
3476 } else if (key[key_len] == '(') {
3477 /* The value can be surrounded by balanced parentheses. The outermost
3478 * set of parentheses is removed. */
3482 value = key + key_len + 1;
3483 for (value_len = 0; level > 0; value_len++) {
3484 switch (value[value_len]) {
3498 value[value_len - 1] = '\0';
3499 pos = value + value_len;
3501 /* There might be no value at all. */
3502 value = key + key_len; /* Will become the empty string below. */
3503 pos = key + key_len + (key[key_len] != '\0');
3505 key[key_len] = '\0';