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 /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
81 * flow_wildcards in 'wc' for use in struct match. It is the caller's
82 * responsibility to handle the special case where the flow match's dl_vlan is
83 * set to OFP_VLAN_NONE. */
85 ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc)
87 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 17);
89 /* Initialize most of wc. */
90 flow_wildcards_init_catchall(wc);
92 if (!(ofpfw & OFPFW10_IN_PORT)) {
93 wc->masks.in_port = UINT16_MAX;
96 if (!(ofpfw & OFPFW10_NW_TOS)) {
97 wc->masks.nw_tos |= IP_DSCP_MASK;
100 if (!(ofpfw & OFPFW10_NW_PROTO)) {
101 wc->masks.nw_proto = UINT8_MAX;
103 wc->masks.nw_src = ofputil_wcbits_to_netmask(ofpfw
104 >> OFPFW10_NW_SRC_SHIFT);
105 wc->masks.nw_dst = ofputil_wcbits_to_netmask(ofpfw
106 >> OFPFW10_NW_DST_SHIFT);
108 if (!(ofpfw & OFPFW10_TP_SRC)) {
109 wc->masks.tp_src = htons(UINT16_MAX);
111 if (!(ofpfw & OFPFW10_TP_DST)) {
112 wc->masks.tp_dst = htons(UINT16_MAX);
115 if (!(ofpfw & OFPFW10_DL_SRC)) {
116 memset(wc->masks.dl_src, 0xff, ETH_ADDR_LEN);
118 if (!(ofpfw & OFPFW10_DL_DST)) {
119 memset(wc->masks.dl_dst, 0xff, ETH_ADDR_LEN);
121 if (!(ofpfw & OFPFW10_DL_TYPE)) {
122 wc->masks.dl_type = htons(UINT16_MAX);
126 if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) {
127 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
129 if (!(ofpfw & OFPFW10_DL_VLAN)) {
130 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
134 /* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */
136 ofputil_match_from_ofp10_match(const struct ofp10_match *ofmatch,
139 uint32_t ofpfw = ntohl(ofmatch->wildcards) & OFPFW10_ALL;
141 /* Initialize match->wc. */
142 memset(&match->flow, 0, sizeof match->flow);
143 ofputil_wildcard_from_ofpfw10(ofpfw, &match->wc);
145 /* Initialize most of match->flow. */
146 match->flow.nw_src = ofmatch->nw_src;
147 match->flow.nw_dst = ofmatch->nw_dst;
148 match->flow.in_port = ntohs(ofmatch->in_port);
149 match->flow.dl_type = ofputil_dl_type_from_openflow(ofmatch->dl_type);
150 match->flow.tp_src = ofmatch->tp_src;
151 match->flow.tp_dst = ofmatch->tp_dst;
152 memcpy(match->flow.dl_src, ofmatch->dl_src, ETH_ADDR_LEN);
153 memcpy(match->flow.dl_dst, ofmatch->dl_dst, ETH_ADDR_LEN);
154 match->flow.nw_tos = ofmatch->nw_tos & IP_DSCP_MASK;
155 match->flow.nw_proto = ofmatch->nw_proto;
157 /* Translate VLANs. */
158 if (!(ofpfw & OFPFW10_DL_VLAN) &&
159 ofmatch->dl_vlan == htons(OFP10_VLAN_NONE)) {
160 /* Match only packets without 802.1Q header.
162 * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
164 * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
165 * because we can't have a specific PCP without an 802.1Q header.
166 * However, older versions of OVS treated this as matching packets
167 * withut an 802.1Q header, so we do here too. */
168 match->flow.vlan_tci = htons(0);
169 match->wc.masks.vlan_tci = htons(0xffff);
171 ovs_be16 vid, pcp, tci;
173 vid = ofmatch->dl_vlan & htons(VLAN_VID_MASK);
174 pcp = htons((ofmatch->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
175 tci = vid | pcp | htons(VLAN_CFI);
176 match->flow.vlan_tci = tci & match->wc.masks.vlan_tci;
180 match_zero_wildcarded_fields(match);
183 /* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */
185 ofputil_match_to_ofp10_match(const struct match *match,
186 struct ofp10_match *ofmatch)
188 const struct flow_wildcards *wc = &match->wc;
191 /* Figure out most OpenFlow wildcards. */
193 if (!wc->masks.in_port) {
194 ofpfw |= OFPFW10_IN_PORT;
196 if (!wc->masks.dl_type) {
197 ofpfw |= OFPFW10_DL_TYPE;
199 if (!wc->masks.nw_proto) {
200 ofpfw |= OFPFW10_NW_PROTO;
202 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_src)
203 << OFPFW10_NW_SRC_SHIFT);
204 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_dst)
205 << OFPFW10_NW_DST_SHIFT);
206 if (!(wc->masks.nw_tos & IP_DSCP_MASK)) {
207 ofpfw |= OFPFW10_NW_TOS;
209 if (!wc->masks.tp_src) {
210 ofpfw |= OFPFW10_TP_SRC;
212 if (!wc->masks.tp_dst) {
213 ofpfw |= OFPFW10_TP_DST;
215 if (eth_addr_is_zero(wc->masks.dl_src)) {
216 ofpfw |= OFPFW10_DL_SRC;
218 if (eth_addr_is_zero(wc->masks.dl_dst)) {
219 ofpfw |= OFPFW10_DL_DST;
222 /* Translate VLANs. */
223 ofmatch->dl_vlan = htons(0);
224 ofmatch->dl_vlan_pcp = 0;
225 if (match->wc.masks.vlan_tci == htons(0)) {
226 ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP;
227 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
228 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
229 ofmatch->dl_vlan = htons(OFP10_VLAN_NONE);
230 ofpfw |= OFPFW10_DL_VLAN_PCP;
232 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
233 ofpfw |= OFPFW10_DL_VLAN;
235 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
238 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
239 ofpfw |= OFPFW10_DL_VLAN_PCP;
241 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
245 /* Compose most of the match structure. */
246 ofmatch->wildcards = htonl(ofpfw);
247 ofmatch->in_port = htons(match->flow.in_port);
248 memcpy(ofmatch->dl_src, match->flow.dl_src, ETH_ADDR_LEN);
249 memcpy(ofmatch->dl_dst, match->flow.dl_dst, ETH_ADDR_LEN);
250 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
251 ofmatch->nw_src = match->flow.nw_src;
252 ofmatch->nw_dst = match->flow.nw_dst;
253 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
254 ofmatch->nw_proto = match->flow.nw_proto;
255 ofmatch->tp_src = match->flow.tp_src;
256 ofmatch->tp_dst = match->flow.tp_dst;
257 memset(ofmatch->pad1, '\0', sizeof ofmatch->pad1);
258 memset(ofmatch->pad2, '\0', sizeof ofmatch->pad2);
262 ofputil_pull_ofp11_match(struct ofpbuf *buf, struct match *match,
263 uint16_t *padded_match_len)
265 struct ofp11_match_header *omh = buf->data;
268 if (buf->size < sizeof *omh) {
269 return OFPERR_OFPBMC_BAD_LEN;
272 match_len = ntohs(omh->length);
274 switch (ntohs(omh->type)) {
275 case OFPMT_STANDARD: {
276 struct ofp11_match *om;
278 if (match_len != sizeof *om || buf->size < sizeof *om) {
279 return OFPERR_OFPBMC_BAD_LEN;
281 om = ofpbuf_pull(buf, sizeof *om);
282 if (padded_match_len) {
283 *padded_match_len = match_len;
285 return ofputil_match_from_ofp11_match(om, match);
289 if (padded_match_len) {
290 *padded_match_len = ROUND_UP(match_len, 8);
292 return oxm_pull_match(buf, match);
295 return OFPERR_OFPBMC_BAD_TYPE;
299 /* Converts the ofp11_match in 'match' into a struct match in 'match. Returns
300 * 0 if successful, otherwise an OFPERR_* value. */
302 ofputil_match_from_ofp11_match(const struct ofp11_match *ofmatch,
305 uint16_t wc = ntohl(ofmatch->wildcards);
306 uint8_t dl_src_mask[ETH_ADDR_LEN];
307 uint8_t dl_dst_mask[ETH_ADDR_LEN];
311 match_init_catchall(match);
313 if (!(wc & OFPFW11_IN_PORT)) {
317 error = ofputil_port_from_ofp11(ofmatch->in_port, &ofp_port);
319 return OFPERR_OFPBMC_BAD_VALUE;
321 match_set_in_port(match, ofp_port);
324 for (i = 0; i < ETH_ADDR_LEN; i++) {
325 dl_src_mask[i] = ~ofmatch->dl_src_mask[i];
327 match_set_dl_src_masked(match, ofmatch->dl_src, dl_src_mask);
329 for (i = 0; i < ETH_ADDR_LEN; i++) {
330 dl_dst_mask[i] = ~ofmatch->dl_dst_mask[i];
332 match_set_dl_dst_masked(match, ofmatch->dl_dst, dl_dst_mask);
334 if (!(wc & OFPFW11_DL_VLAN)) {
335 if (ofmatch->dl_vlan == htons(OFPVID11_NONE)) {
336 /* Match only packets without a VLAN tag. */
337 match->flow.vlan_tci = htons(0);
338 match->wc.masks.vlan_tci = htons(UINT16_MAX);
340 if (ofmatch->dl_vlan == htons(OFPVID11_ANY)) {
341 /* Match any packet with a VLAN tag regardless of VID. */
342 match->flow.vlan_tci = htons(VLAN_CFI);
343 match->wc.masks.vlan_tci = htons(VLAN_CFI);
344 } else if (ntohs(ofmatch->dl_vlan) < 4096) {
345 /* Match only packets with the specified VLAN VID. */
346 match->flow.vlan_tci = htons(VLAN_CFI) | ofmatch->dl_vlan;
347 match->wc.masks.vlan_tci = htons(VLAN_CFI | VLAN_VID_MASK);
350 return OFPERR_OFPBMC_BAD_VALUE;
353 if (!(wc & OFPFW11_DL_VLAN_PCP)) {
354 if (ofmatch->dl_vlan_pcp <= 7) {
355 match->flow.vlan_tci |= htons(ofmatch->dl_vlan_pcp
357 match->wc.masks.vlan_tci |= htons(VLAN_PCP_MASK);
360 return OFPERR_OFPBMC_BAD_VALUE;
366 if (!(wc & OFPFW11_DL_TYPE)) {
367 match_set_dl_type(match,
368 ofputil_dl_type_from_openflow(ofmatch->dl_type));
371 ipv4 = match->flow.dl_type == htons(ETH_TYPE_IP);
372 arp = match->flow.dl_type == htons(ETH_TYPE_ARP);
374 if (ipv4 && !(wc & OFPFW11_NW_TOS)) {
375 if (ofmatch->nw_tos & ~IP_DSCP_MASK) {
377 return OFPERR_OFPBMC_BAD_VALUE;
380 match_set_nw_dscp(match, ofmatch->nw_tos);
384 if (!(wc & OFPFW11_NW_PROTO)) {
385 match_set_nw_proto(match, ofmatch->nw_proto);
387 match_set_nw_src_masked(match, ofmatch->nw_src, ~ofmatch->nw_src_mask);
388 match_set_nw_dst_masked(match, ofmatch->nw_dst, ~ofmatch->nw_dst_mask);
391 #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
392 if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) {
393 switch (match->flow.nw_proto) {
395 /* "A.2.3 Flow Match Structures" in OF1.1 says:
397 * The tp_src and tp_dst fields will be ignored unless the
398 * network protocol specified is as TCP, UDP or SCTP.
400 * but I'm pretty sure we should support ICMP too, otherwise
401 * that's a regression from OF1.0. */
402 if (!(wc & OFPFW11_TP_SRC)) {
403 uint16_t icmp_type = ntohs(ofmatch->tp_src);
404 if (icmp_type < 0x100) {
405 match_set_icmp_type(match, icmp_type);
407 return OFPERR_OFPBMC_BAD_FIELD;
410 if (!(wc & OFPFW11_TP_DST)) {
411 uint16_t icmp_code = ntohs(ofmatch->tp_dst);
412 if (icmp_code < 0x100) {
413 match_set_icmp_code(match, icmp_code);
415 return OFPERR_OFPBMC_BAD_FIELD;
422 if (!(wc & (OFPFW11_TP_SRC))) {
423 match_set_tp_src(match, ofmatch->tp_src);
425 if (!(wc & (OFPFW11_TP_DST))) {
426 match_set_tp_dst(match, ofmatch->tp_dst);
431 /* We don't support SCTP and it seems that we should tell the
432 * controller, since OF1.1 implementations are supposed to. */
433 return OFPERR_OFPBMC_BAD_FIELD;
436 /* OF1.1 says explicitly to ignore this. */
441 if (match->flow.dl_type == htons(ETH_TYPE_MPLS) ||
442 match->flow.dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
443 enum { OFPFW11_MPLS_ALL = OFPFW11_MPLS_LABEL | OFPFW11_MPLS_TC };
445 if ((wc & OFPFW11_MPLS_ALL) != OFPFW11_MPLS_ALL) {
446 /* MPLS not supported. */
447 return OFPERR_OFPBMC_BAD_TAG;
451 match_set_metadata_masked(match, ofmatch->metadata,
452 ~ofmatch->metadata_mask);
457 /* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */
459 ofputil_match_to_ofp11_match(const struct match *match,
460 struct ofp11_match *ofmatch)
465 memset(ofmatch, 0, sizeof *ofmatch);
466 ofmatch->omh.type = htons(OFPMT_STANDARD);
467 ofmatch->omh.length = htons(OFPMT11_STANDARD_LENGTH);
469 if (!match->wc.masks.in_port) {
470 wc |= OFPFW11_IN_PORT;
472 ofmatch->in_port = ofputil_port_to_ofp11(match->flow.in_port);
475 memcpy(ofmatch->dl_src, match->flow.dl_src, ETH_ADDR_LEN);
476 for (i = 0; i < ETH_ADDR_LEN; i++) {
477 ofmatch->dl_src_mask[i] = ~match->wc.masks.dl_src[i];
480 memcpy(ofmatch->dl_dst, match->flow.dl_dst, ETH_ADDR_LEN);
481 for (i = 0; i < ETH_ADDR_LEN; i++) {
482 ofmatch->dl_dst_mask[i] = ~match->wc.masks.dl_dst[i];
485 if (match->wc.masks.vlan_tci == htons(0)) {
486 wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP;
487 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
488 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
489 ofmatch->dl_vlan = htons(OFPVID11_NONE);
490 wc |= OFPFW11_DL_VLAN_PCP;
492 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
493 ofmatch->dl_vlan = htons(OFPVID11_ANY);
495 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
498 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
499 wc |= OFPFW11_DL_VLAN_PCP;
501 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
505 if (!match->wc.masks.dl_type) {
506 wc |= OFPFW11_DL_TYPE;
508 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
511 if (!(match->wc.masks.nw_tos & IP_DSCP_MASK)) {
512 wc |= OFPFW11_NW_TOS;
514 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
517 if (!match->wc.masks.nw_proto) {
518 wc |= OFPFW11_NW_PROTO;
520 ofmatch->nw_proto = match->flow.nw_proto;
523 ofmatch->nw_src = match->flow.nw_src;
524 ofmatch->nw_src_mask = ~match->wc.masks.nw_src;
525 ofmatch->nw_dst = match->flow.nw_dst;
526 ofmatch->nw_dst_mask = ~match->wc.masks.nw_dst;
528 if (!match->wc.masks.tp_src) {
529 wc |= OFPFW11_TP_SRC;
531 ofmatch->tp_src = match->flow.tp_src;
534 if (!match->wc.masks.tp_dst) {
535 wc |= OFPFW11_TP_DST;
537 ofmatch->tp_dst = match->flow.tp_dst;
540 /* MPLS not supported. */
541 wc |= OFPFW11_MPLS_LABEL;
542 wc |= OFPFW11_MPLS_TC;
544 ofmatch->metadata = match->flow.metadata;
545 ofmatch->metadata_mask = ~match->wc.masks.metadata;
547 ofmatch->wildcards = htonl(wc);
550 /* Given a 'dl_type' value in the format used in struct flow, returns the
551 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
554 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
556 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
557 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
561 /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
562 * structure, returns the corresponding 'dl_type' value for use in struct
565 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
567 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
568 ? htons(FLOW_DL_TYPE_NONE)
574 struct proto_abbrev {
575 enum ofputil_protocol protocol;
579 /* Most users really don't care about some of the differences between
580 * protocols. These abbreviations help with that. */
581 static const struct proto_abbrev proto_abbrevs[] = {
582 { OFPUTIL_P_ANY, "any" },
583 { OFPUTIL_P_OF10_ANY, "OpenFlow10" },
584 { OFPUTIL_P_NXM_ANY, "NXM" },
586 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
588 enum ofputil_protocol ofputil_flow_dump_protocols[] = {
592 size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
594 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
595 * connection that has negotiated the given 'version'. 'version' should
596 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
597 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
598 * outside the valid range. */
599 enum ofputil_protocol
600 ofputil_protocol_from_ofp_version(enum ofp_version version)
604 return OFPUTIL_P_OF10;
606 return OFPUTIL_P_OF12;
613 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
614 * OFP11_VERSION or OFP12_VERSION) that corresponds to 'protocol'. */
616 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
620 case OFPUTIL_P_OF10_TID:
622 case OFPUTIL_P_NXM_TID:
623 return OFP10_VERSION;
625 return OFP12_VERSION;
631 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
634 ofputil_protocol_is_valid(enum ofputil_protocol protocol)
636 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
639 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
640 * extension turned on or off if 'enable' is true or false, respectively.
642 * This extension is only useful for protocols whose "standard" version does
643 * not allow specific tables to be modified. In particular, this is true of
644 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
645 * specifies a table ID and so there is no need for such an extension. When
646 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
647 * extension, this function just returns its 'protocol' argument unchanged
648 * regardless of the value of 'enable'. */
649 enum ofputil_protocol
650 ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
654 case OFPUTIL_P_OF10_TID:
655 return enable ? OFPUTIL_P_OF10_TID : OFPUTIL_P_OF10;
658 case OFPUTIL_P_NXM_TID:
659 return enable ? OFPUTIL_P_NXM_TID : OFPUTIL_P_NXM;
662 return OFPUTIL_P_OF12;
669 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
670 * some extension to a standard protocol version, the return value is the
671 * standard version of that protocol without any extension. If 'protocol' is a
672 * standard protocol version, returns 'protocol' unchanged. */
673 enum ofputil_protocol
674 ofputil_protocol_to_base(enum ofputil_protocol protocol)
676 return ofputil_protocol_set_tid(protocol, false);
679 /* Returns 'new_base' with any extensions taken from 'cur'. */
680 enum ofputil_protocol
681 ofputil_protocol_set_base(enum ofputil_protocol cur,
682 enum ofputil_protocol new_base)
684 bool tid = (cur & OFPUTIL_P_TID) != 0;
688 case OFPUTIL_P_OF10_TID:
689 return ofputil_protocol_set_tid(OFPUTIL_P_OF10, tid);
692 case OFPUTIL_P_NXM_TID:
693 return ofputil_protocol_set_tid(OFPUTIL_P_NXM, tid);
696 return ofputil_protocol_set_tid(OFPUTIL_P_OF12, tid);
703 /* Returns a string form of 'protocol', if a simple form exists (that is, if
704 * 'protocol' is either a single protocol or it is a combination of protocols
705 * that have a single abbreviation). Otherwise, returns NULL. */
707 ofputil_protocol_to_string(enum ofputil_protocol protocol)
709 const struct proto_abbrev *p;
711 /* Use a "switch" statement for single-bit names so that we get a compiler
712 * warning if we forget any. */
715 return "NXM-table_id";
717 case OFPUTIL_P_NXM_TID:
718 return "NXM+table_id";
721 return "OpenFlow10-table_id";
723 case OFPUTIL_P_OF10_TID:
724 return "OpenFlow10+table_id";
730 /* Check abbreviations. */
731 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
732 if (protocol == p->protocol) {
740 /* Returns a string that represents 'protocols'. The return value might be a
741 * comma-separated list if 'protocols' doesn't have a simple name. The return
742 * value is "none" if 'protocols' is 0.
744 * The caller must free the returned string (with free()). */
746 ofputil_protocols_to_string(enum ofputil_protocol protocols)
750 assert(!(protocols & ~OFPUTIL_P_ANY));
751 if (protocols == 0) {
752 return xstrdup("none");
757 const struct proto_abbrev *p;
761 ds_put_char(&s, ',');
764 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
765 if ((protocols & p->protocol) == p->protocol) {
766 ds_put_cstr(&s, p->name);
767 protocols &= ~p->protocol;
772 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
773 enum ofputil_protocol bit = 1u << i;
775 if (protocols & bit) {
776 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
785 return ds_steal_cstr(&s);
788 static enum ofputil_protocol
789 ofputil_protocol_from_string__(const char *s, size_t n)
791 const struct proto_abbrev *p;
794 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
795 enum ofputil_protocol bit = 1u << i;
796 const char *name = ofputil_protocol_to_string(bit);
798 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
803 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
804 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
812 /* Returns the nonempty set of protocols represented by 's', which can be a
813 * single protocol name or abbreviation or a comma-separated list of them.
815 * Aborts the program with an error message if 's' is invalid. */
816 enum ofputil_protocol
817 ofputil_protocols_from_string(const char *s)
819 const char *orig_s = s;
820 enum ofputil_protocol protocols;
824 enum ofputil_protocol p;
833 p = ofputil_protocol_from_string__(s, n);
835 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
843 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
849 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
851 switch (packet_in_format) {
852 case NXPIF_OPENFLOW10:
861 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
863 switch (packet_in_format) {
864 case NXPIF_OPENFLOW10:
874 ofputil_packet_in_format_from_string(const char *s)
876 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
877 : !strcmp(s, "nxm") ? NXPIF_NXM
882 regs_fully_wildcarded(const struct flow_wildcards *wc)
886 for (i = 0; i < FLOW_N_REGS; i++) {
887 if (wc->masks.regs[i] != 0) {
894 /* Returns a bit-mask of ofputil_protocols that can be used for sending 'match'
895 * to a switch (e.g. to add or remove a flow). Only NXM can handle tunnel IDs,
896 * registers, or fixing the Ethernet multicast bit. Otherwise, it's better to
897 * use OpenFlow 1.0 protocol for backward compatibility. */
898 enum ofputil_protocol
899 ofputil_usable_protocols(const struct match *match)
901 const struct flow_wildcards *wc = &match->wc;
903 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 17);
905 /* NXM and OF1.1+ supports bitwise matching on ethernet addresses. */
906 if (!eth_mask_is_exact(wc->masks.dl_src)
907 && !eth_addr_is_zero(wc->masks.dl_src)) {
908 return OFPUTIL_P_NXM_ANY;
910 if (!eth_mask_is_exact(wc->masks.dl_dst)
911 && !eth_addr_is_zero(wc->masks.dl_dst)) {
912 return OFPUTIL_P_NXM_ANY;
915 /* NXM and OF1.1+ support matching metadata. */
916 if (wc->masks.metadata != htonll(0)) {
917 return OFPUTIL_P_NXM_ANY;
920 /* Only NXM supports matching ARP hardware addresses. */
921 if (!eth_addr_is_zero(wc->masks.arp_sha) ||
922 !eth_addr_is_zero(wc->masks.arp_tha)) {
923 return OFPUTIL_P_NXM_ANY;
926 /* Only NXM supports matching IPv6 traffic. */
927 if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
928 return OFPUTIL_P_NXM_ANY;
931 /* Only NXM supports matching registers. */
932 if (!regs_fully_wildcarded(wc)) {
933 return OFPUTIL_P_NXM_ANY;
936 /* Only NXM supports matching tun_id. */
937 if (wc->masks.tunnel.tun_id != htonll(0)) {
938 return OFPUTIL_P_NXM_ANY;
941 /* Only NXM supports matching fragments. */
942 if (wc->masks.nw_frag) {
943 return OFPUTIL_P_NXM_ANY;
946 /* Only NXM supports matching IPv6 flow label. */
947 if (wc->masks.ipv6_label) {
948 return OFPUTIL_P_NXM_ANY;
951 /* Only NXM supports matching IP ECN bits. */
952 if (wc->masks.nw_tos & IP_ECN_MASK) {
953 return OFPUTIL_P_NXM_ANY;
956 /* Only NXM supports matching IP TTL/hop limit. */
957 if (wc->masks.nw_ttl) {
958 return OFPUTIL_P_NXM_ANY;
961 /* Only NXM supports non-CIDR IPv4 address masks. */
962 if (!ip_is_cidr(wc->masks.nw_src) || !ip_is_cidr(wc->masks.nw_dst)) {
963 return OFPUTIL_P_NXM_ANY;
966 /* Only NXM supports bitwise matching on transport port. */
967 if ((wc->masks.tp_src && wc->masks.tp_src != htons(UINT16_MAX)) ||
968 (wc->masks.tp_dst && wc->masks.tp_dst != htons(UINT16_MAX))) {
969 return OFPUTIL_P_NXM_ANY;
972 /* Other formats can express this rule. */
973 return OFPUTIL_P_ANY;
976 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
977 * protocol is 'current', at least partly transitions the protocol to 'want'.
978 * Stores in '*next' the protocol that will be in effect on the OpenFlow
979 * connection if the switch processes the returned message correctly. (If
980 * '*next != want' then the caller will have to iterate.)
982 * If 'current == want', returns NULL and stores 'current' in '*next'. */
984 ofputil_encode_set_protocol(enum ofputil_protocol current,
985 enum ofputil_protocol want,
986 enum ofputil_protocol *next)
988 enum ofputil_protocol cur_base, want_base;
989 bool cur_tid, want_tid;
991 cur_base = ofputil_protocol_to_base(current);
992 want_base = ofputil_protocol_to_base(want);
993 if (cur_base != want_base) {
994 *next = ofputil_protocol_set_base(current, want_base);
998 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1000 case OFPUTIL_P_OF10:
1001 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1003 case OFPUTIL_P_OF12:
1004 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW12);
1006 case OFPUTIL_P_OF10_TID:
1007 case OFPUTIL_P_NXM_TID:
1012 cur_tid = (current & OFPUTIL_P_TID) != 0;
1013 want_tid = (want & OFPUTIL_P_TID) != 0;
1014 if (cur_tid != want_tid) {
1015 *next = ofputil_protocol_set_tid(current, want_tid);
1016 return ofputil_make_flow_mod_table_id(want_tid);
1019 assert(current == want);
1025 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1026 * format to 'nxff'. */
1028 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1030 struct nx_set_flow_format *sff;
1033 assert(ofputil_nx_flow_format_is_valid(nxff));
1035 msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1036 sff = ofpbuf_put_zeros(msg, sizeof *sff);
1037 sff->format = htonl(nxff);
1042 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1044 enum ofputil_protocol
1045 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1047 switch (flow_format) {
1048 case NXFF_OPENFLOW10:
1049 return OFPUTIL_P_OF10;
1052 return OFPUTIL_P_NXM;
1054 case NXFF_OPENFLOW12:
1055 return OFPUTIL_P_OF12;
1062 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1064 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1066 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1069 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1072 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1074 switch (flow_format) {
1075 case NXFF_OPENFLOW10:
1076 return "openflow10";
1079 case NXFF_OPENFLOW12:
1080 return "openflow12";
1087 ofputil_make_set_packet_in_format(enum ofp_version ofp_version,
1088 enum nx_packet_in_format packet_in_format)
1090 struct nx_set_packet_in_format *spif;
1093 msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, ofp_version, 0);
1094 spif = ofpbuf_put_zeros(msg, sizeof *spif);
1095 spif->format = htonl(packet_in_format);
1100 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1101 * extension on or off (according to 'flow_mod_table_id'). */
1103 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1105 struct nx_flow_mod_table_id *nfmti;
1108 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1109 nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
1110 nfmti->set = flow_mod_table_id;
1114 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1115 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1118 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1119 * The caller must initialize 'ofpacts' and retains ownership of it.
1120 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1122 * Does not validate the flow_mod actions. The caller should do that, with
1123 * ofpacts_check(). */
1125 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1126 const struct ofp_header *oh,
1127 enum ofputil_protocol protocol,
1128 struct ofpbuf *ofpacts)
1134 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1135 raw = ofpraw_pull_assert(&b);
1136 if (raw == OFPRAW_OFPT11_FLOW_MOD) {
1137 /* Standard OpenFlow 1.1 flow_mod. */
1138 const struct ofp11_flow_mod *ofm;
1141 ofm = ofpbuf_pull(&b, sizeof *ofm);
1143 error = ofputil_pull_ofp11_match(&b, &fm->match, NULL);
1148 error = ofpacts_pull_openflow11_instructions(&b, b.size, ofpacts);
1153 /* Translate the message. */
1154 fm->priority = ntohs(ofm->priority);
1155 if (ofm->command == OFPFC_ADD) {
1156 fm->cookie = htonll(0);
1157 fm->cookie_mask = htonll(0);
1158 fm->new_cookie = ofm->cookie;
1161 fm->cookie = ofm->cookie;
1162 fm->cookie_mask = ofm->cookie_mask;
1163 fm->new_cookie = htonll(UINT64_MAX);
1165 fm->command = ofm->command;
1166 fm->table_id = ofm->table_id;
1167 fm->idle_timeout = ntohs(ofm->idle_timeout);
1168 fm->hard_timeout = ntohs(ofm->hard_timeout);
1169 fm->buffer_id = ntohl(ofm->buffer_id);
1170 error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
1174 if (ofm->out_group != htonl(OFPG_ANY)) {
1175 return OFPERR_OFPFMFC_UNKNOWN;
1177 fm->flags = ntohs(ofm->flags);
1179 if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1180 /* Standard OpenFlow 1.0 flow_mod. */
1181 const struct ofp10_flow_mod *ofm;
1184 /* Get the ofp10_flow_mod. */
1185 ofm = ofpbuf_pull(&b, sizeof *ofm);
1187 /* Translate the rule. */
1188 ofputil_match_from_ofp10_match(&ofm->match, &fm->match);
1189 ofputil_normalize_match(&fm->match);
1191 /* Now get the actions. */
1192 error = ofpacts_pull_openflow10(&b, b.size, ofpacts);
1197 /* OpenFlow 1.0 says that exact-match rules have to have the
1198 * highest possible priority. */
1199 fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL)
1200 ? ntohs(ofm->priority)
1203 /* Translate the message. */
1204 command = ntohs(ofm->command);
1205 fm->cookie = htonll(0);
1206 fm->cookie_mask = htonll(0);
1207 fm->new_cookie = ofm->cookie;
1208 fm->idle_timeout = ntohs(ofm->idle_timeout);
1209 fm->hard_timeout = ntohs(ofm->hard_timeout);
1210 fm->buffer_id = ntohl(ofm->buffer_id);
1211 fm->out_port = ntohs(ofm->out_port);
1212 fm->flags = ntohs(ofm->flags);
1213 } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1214 /* Nicira extended flow_mod. */
1215 const struct nx_flow_mod *nfm;
1218 /* Dissect the message. */
1219 nfm = ofpbuf_pull(&b, sizeof *nfm);
1220 error = nx_pull_match(&b, ntohs(nfm->match_len),
1221 &fm->match, &fm->cookie, &fm->cookie_mask);
1225 error = ofpacts_pull_openflow10(&b, b.size, ofpacts);
1230 /* Translate the message. */
1231 command = ntohs(nfm->command);
1232 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1233 /* Flow additions may only set a new cookie, not match an
1234 * existing cookie. */
1235 return OFPERR_NXBRC_NXM_INVALID;
1237 fm->priority = ntohs(nfm->priority);
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;
1264 ofputil_tid_command(const struct ofputil_flow_mod *fm,
1265 enum ofputil_protocol protocol)
1267 return htons(protocol & OFPUTIL_P_TID
1268 ? (fm->command & 0xff) | (fm->table_id << 8)
1272 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
1273 * 'protocol' and returns the message. */
1275 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
1276 enum ofputil_protocol protocol)
1281 case OFPUTIL_P_OF12: {
1282 struct ofp11_flow_mod *ofm;
1284 msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, OFP12_VERSION,
1285 NXM_TYPICAL_LEN + fm->ofpacts_len);
1286 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
1287 ofm->cookie = fm->new_cookie;
1288 ofm->cookie_mask = fm->cookie_mask;
1289 ofm->table_id = fm->table_id;
1290 ofm->command = fm->command;
1291 ofm->idle_timeout = htons(fm->idle_timeout);
1292 ofm->hard_timeout = htons(fm->hard_timeout);
1293 ofm->priority = htons(fm->priority);
1294 ofm->buffer_id = htonl(fm->buffer_id);
1295 ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
1296 ofm->out_group = htonl(OFPG11_ANY);
1297 ofm->flags = htons(fm->flags);
1298 oxm_put_match(msg, &fm->match);
1299 ofpacts_put_openflow11_instructions(fm->ofpacts, fm->ofpacts_len, msg);
1303 case OFPUTIL_P_OF10:
1304 case OFPUTIL_P_OF10_TID: {
1305 struct ofp10_flow_mod *ofm;
1307 msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
1309 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
1310 ofputil_match_to_ofp10_match(&fm->match, &ofm->match);
1311 ofm->cookie = fm->new_cookie;
1312 ofm->command = ofputil_tid_command(fm, protocol);
1313 ofm->idle_timeout = htons(fm->idle_timeout);
1314 ofm->hard_timeout = htons(fm->hard_timeout);
1315 ofm->priority = htons(fm->priority);
1316 ofm->buffer_id = htonl(fm->buffer_id);
1317 ofm->out_port = htons(fm->out_port);
1318 ofm->flags = htons(fm->flags);
1319 ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
1324 case OFPUTIL_P_NXM_TID: {
1325 struct nx_flow_mod *nfm;
1328 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
1329 NXM_TYPICAL_LEN + fm->ofpacts_len);
1330 nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
1331 nfm->command = ofputil_tid_command(fm, protocol);
1332 nfm->cookie = fm->new_cookie;
1333 match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask);
1335 nfm->idle_timeout = htons(fm->idle_timeout);
1336 nfm->hard_timeout = htons(fm->hard_timeout);
1337 nfm->priority = htons(fm->priority);
1338 nfm->buffer_id = htonl(fm->buffer_id);
1339 nfm->out_port = htons(fm->out_port);
1340 nfm->flags = htons(fm->flags);
1341 nfm->match_len = htons(match_len);
1342 ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
1350 ofpmsg_update_length(msg);
1354 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1355 * send all of the 'n_fm's flow table modification requests in 'fms', and a
1356 * 0-bit for each protocol that is inadequate.
1358 * (The return value will have at least one 1-bit.) */
1359 enum ofputil_protocol
1360 ofputil_flow_mod_usable_protocols(const struct ofputil_flow_mod *fms,
1363 enum ofputil_protocol usable_protocols;
1366 usable_protocols = OFPUTIL_P_ANY;
1367 for (i = 0; i < n_fms; i++) {
1368 const struct ofputil_flow_mod *fm = &fms[i];
1370 usable_protocols &= ofputil_usable_protocols(&fm->match);
1371 if (fm->table_id != 0xff) {
1372 usable_protocols &= OFPUTIL_P_TID;
1375 /* Matching of the cookie is only supported through NXM. */
1376 if (fm->cookie_mask != htonll(0)) {
1377 usable_protocols &= OFPUTIL_P_NXM_ANY;
1380 assert(usable_protocols);
1382 return usable_protocols;
1386 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr,
1387 const struct ofp10_flow_stats_request *ofsr,
1390 fsr->aggregate = aggregate;
1391 ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match);
1392 fsr->out_port = ntohs(ofsr->out_port);
1393 fsr->table_id = ofsr->table_id;
1394 fsr->cookie = fsr->cookie_mask = htonll(0);
1400 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr,
1401 struct ofpbuf *b, bool aggregate)
1403 const struct ofp11_flow_stats_request *ofsr;
1406 ofsr = ofpbuf_pull(b, sizeof *ofsr);
1407 fsr->aggregate = aggregate;
1408 fsr->table_id = ofsr->table_id;
1409 error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port);
1413 if (ofsr->out_group != htonl(OFPG11_ANY)) {
1414 return OFPERR_OFPFMFC_UNKNOWN;
1416 fsr->cookie = ofsr->cookie;
1417 fsr->cookie_mask = ofsr->cookie_mask;
1418 error = ofputil_pull_ofp11_match(b, &fsr->match, NULL);
1427 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
1428 struct ofpbuf *b, bool aggregate)
1430 const struct nx_flow_stats_request *nfsr;
1433 nfsr = ofpbuf_pull(b, sizeof *nfsr);
1434 error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match,
1435 &fsr->cookie, &fsr->cookie_mask);
1440 return OFPERR_OFPBRC_BAD_LEN;
1443 fsr->aggregate = aggregate;
1444 fsr->out_port = ntohs(nfsr->out_port);
1445 fsr->table_id = nfsr->table_id;
1450 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1451 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1452 * successful, otherwise an OpenFlow error code. */
1454 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
1455 const struct ofp_header *oh)
1460 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1461 raw = ofpraw_pull_assert(&b);
1462 switch ((int) raw) {
1463 case OFPRAW_OFPST10_FLOW_REQUEST:
1464 return ofputil_decode_ofpst10_flow_request(fsr, b.data, false);
1466 case OFPRAW_OFPST10_AGGREGATE_REQUEST:
1467 return ofputil_decode_ofpst10_flow_request(fsr, b.data, true);
1469 case OFPRAW_OFPST11_FLOW_REQUEST:
1470 return ofputil_decode_ofpst11_flow_request(fsr, &b, false);
1472 case OFPRAW_OFPST11_AGGREGATE_REQUEST:
1473 return ofputil_decode_ofpst11_flow_request(fsr, &b, true);
1475 case OFPRAW_NXST_FLOW_REQUEST:
1476 return ofputil_decode_nxst_flow_request(fsr, &b, false);
1478 case OFPRAW_NXST_AGGREGATE_REQUEST:
1479 return ofputil_decode_nxst_flow_request(fsr, &b, true);
1482 /* Hey, the caller lied. */
1487 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1488 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1489 * 'protocol', and returns the message. */
1491 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
1492 enum ofputil_protocol protocol)
1498 case OFPUTIL_P_OF12: {
1499 struct ofp11_flow_stats_request *ofsr;
1501 raw = (fsr->aggregate
1502 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
1503 : OFPRAW_OFPST11_FLOW_REQUEST);
1504 msg = ofpraw_alloc(raw, OFP12_VERSION, NXM_TYPICAL_LEN);
1505 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
1506 ofsr->table_id = fsr->table_id;
1507 ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port);
1508 ofsr->out_group = htonl(OFPG11_ANY);
1509 ofsr->cookie = fsr->cookie;
1510 ofsr->cookie_mask = fsr->cookie_mask;
1511 oxm_put_match(msg, &fsr->match);
1515 case OFPUTIL_P_OF10:
1516 case OFPUTIL_P_OF10_TID: {
1517 struct ofp10_flow_stats_request *ofsr;
1519 raw = (fsr->aggregate
1520 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
1521 : OFPRAW_OFPST10_FLOW_REQUEST);
1522 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
1523 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
1524 ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match);
1525 ofsr->table_id = fsr->table_id;
1526 ofsr->out_port = htons(fsr->out_port);
1531 case OFPUTIL_P_NXM_TID: {
1532 struct nx_flow_stats_request *nfsr;
1535 raw = (fsr->aggregate
1536 ? OFPRAW_NXST_AGGREGATE_REQUEST
1537 : OFPRAW_NXST_FLOW_REQUEST);
1538 msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN);
1539 ofpbuf_put_zeros(msg, sizeof *nfsr);
1540 match_len = nx_put_match(msg, &fsr->match,
1541 fsr->cookie, fsr->cookie_mask);
1544 nfsr->out_port = htons(fsr->out_port);
1545 nfsr->match_len = htons(match_len);
1546 nfsr->table_id = fsr->table_id;
1557 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1558 * accurately encode 'fsr', and a 0-bit for each protocol that is inadequate.
1560 * (The return value will have at least one 1-bit.) */
1561 enum ofputil_protocol
1562 ofputil_flow_stats_request_usable_protocols(
1563 const struct ofputil_flow_stats_request *fsr)
1565 enum ofputil_protocol usable_protocols;
1567 usable_protocols = ofputil_usable_protocols(&fsr->match);
1568 if (fsr->cookie_mask != htonll(0)) {
1569 usable_protocols &= OFPUTIL_P_NXM_ANY;
1571 return usable_protocols;
1574 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1575 * ofputil_flow_stats in 'fs'.
1577 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1578 * OpenFlow message. Calling this function multiple times for a single 'msg'
1579 * iterates through the replies. The caller must initially leave 'msg''s layer
1580 * pointers null and not modify them between calls.
1582 * Most switches don't send the values needed to populate fs->idle_age and
1583 * fs->hard_age, so those members will usually be set to 0. If the switch from
1584 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
1585 * 'flow_age_extension' as true so that the contents of 'msg' determine the
1586 * 'idle_age' and 'hard_age' members in 'fs'.
1588 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
1589 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
1590 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
1592 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1593 * otherwise a positive errno value. */
1595 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1597 bool flow_age_extension,
1598 struct ofpbuf *ofpacts)
1604 ? ofpraw_decode(&raw, msg->l2)
1605 : ofpraw_pull(&raw, msg));
1612 } else if (raw == OFPRAW_OFPST11_FLOW_REPLY) {
1613 const struct ofp11_flow_stats *ofs;
1615 uint16_t padded_match_len;
1617 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1619 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1620 "bytes at end", msg->size);
1624 length = ntohs(ofs->length);
1625 if (length < sizeof *ofs) {
1626 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1627 "length %zu", length);
1631 if (ofputil_pull_ofp11_match(msg, &fs->match, &padded_match_len)) {
1632 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match");
1636 if (ofpacts_pull_openflow11_instructions(msg, length - sizeof *ofs -
1637 padded_match_len, ofpacts)) {
1638 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions");
1642 fs->priority = ntohs(ofs->priority);
1643 fs->table_id = ofs->table_id;
1644 fs->duration_sec = ntohl(ofs->duration_sec);
1645 fs->duration_nsec = ntohl(ofs->duration_nsec);
1646 fs->idle_timeout = ntohs(ofs->idle_timeout);
1647 fs->hard_timeout = ntohs(ofs->hard_timeout);
1650 fs->cookie = ofs->cookie;
1651 fs->packet_count = ntohll(ofs->packet_count);
1652 fs->byte_count = ntohll(ofs->byte_count);
1653 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
1654 const struct ofp10_flow_stats *ofs;
1657 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1659 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1660 "bytes at end", msg->size);
1664 length = ntohs(ofs->length);
1665 if (length < sizeof *ofs) {
1666 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1667 "length %zu", length);
1671 if (ofpacts_pull_openflow10(msg, length - sizeof *ofs, ofpacts)) {
1675 fs->cookie = get_32aligned_be64(&ofs->cookie);
1676 ofputil_match_from_ofp10_match(&ofs->match, &fs->match);
1677 fs->priority = ntohs(ofs->priority);
1678 fs->table_id = ofs->table_id;
1679 fs->duration_sec = ntohl(ofs->duration_sec);
1680 fs->duration_nsec = ntohl(ofs->duration_nsec);
1681 fs->idle_timeout = ntohs(ofs->idle_timeout);
1682 fs->hard_timeout = ntohs(ofs->hard_timeout);
1685 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1686 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
1687 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
1688 const struct nx_flow_stats *nfs;
1689 size_t match_len, actions_len, length;
1691 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1693 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1694 "bytes at end", msg->size);
1698 length = ntohs(nfs->length);
1699 match_len = ntohs(nfs->match_len);
1700 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1701 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1702 "claims invalid length %zu", match_len, length);
1705 if (nx_pull_match(msg, match_len, &fs->match, NULL, NULL)) {
1709 actions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
1710 if (ofpacts_pull_openflow10(msg, actions_len, ofpacts)) {
1714 fs->cookie = nfs->cookie;
1715 fs->table_id = nfs->table_id;
1716 fs->duration_sec = ntohl(nfs->duration_sec);
1717 fs->duration_nsec = ntohl(nfs->duration_nsec);
1718 fs->priority = ntohs(nfs->priority);
1719 fs->idle_timeout = ntohs(nfs->idle_timeout);
1720 fs->hard_timeout = ntohs(nfs->hard_timeout);
1723 if (flow_age_extension) {
1724 if (nfs->idle_age) {
1725 fs->idle_age = ntohs(nfs->idle_age) - 1;
1727 if (nfs->hard_age) {
1728 fs->hard_age = ntohs(nfs->hard_age) - 1;
1731 fs->packet_count = ntohll(nfs->packet_count);
1732 fs->byte_count = ntohll(nfs->byte_count);
1737 fs->ofpacts = ofpacts->data;
1738 fs->ofpacts_len = ofpacts->size;
1743 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
1745 * We use this in situations where OVS internally uses UINT64_MAX to mean
1746 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
1748 unknown_to_zero(uint64_t count)
1750 return count != UINT64_MAX ? count : 0;
1753 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
1754 * those already present in the list of ofpbufs in 'replies'. 'replies' should
1755 * have been initialized with ofputil_start_stats_reply(). */
1757 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
1758 struct list *replies)
1760 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
1761 size_t start_ofs = reply->size;
1764 ofpraw_decode_partial(&raw, reply->data, reply->size);
1765 if (raw == OFPRAW_OFPST11_FLOW_REPLY) {
1766 struct ofp11_flow_stats *ofs;
1768 ofpbuf_put_uninit(reply, sizeof *ofs);
1769 oxm_put_match(reply, &fs->match);
1770 ofpacts_put_openflow11_instructions(fs->ofpacts, fs->ofpacts_len,
1773 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
1774 ofs->length = htons(reply->size - start_ofs);
1775 ofs->table_id = fs->table_id;
1777 ofs->duration_sec = htonl(fs->duration_sec);
1778 ofs->duration_nsec = htonl(fs->duration_nsec);
1779 ofs->priority = htons(fs->priority);
1780 ofs->idle_timeout = htons(fs->idle_timeout);
1781 ofs->hard_timeout = htons(fs->hard_timeout);
1782 memset(ofs->pad2, 0, sizeof ofs->pad2);
1783 ofs->cookie = fs->cookie;
1784 ofs->packet_count = htonll(unknown_to_zero(fs->packet_count));
1785 ofs->byte_count = htonll(unknown_to_zero(fs->byte_count));
1786 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
1787 struct ofp10_flow_stats *ofs;
1789 ofpbuf_put_uninit(reply, sizeof *ofs);
1790 ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply);
1792 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
1793 ofs->length = htons(reply->size - start_ofs);
1794 ofs->table_id = fs->table_id;
1796 ofputil_match_to_ofp10_match(&fs->match, &ofs->match);
1797 ofs->duration_sec = htonl(fs->duration_sec);
1798 ofs->duration_nsec = htonl(fs->duration_nsec);
1799 ofs->priority = htons(fs->priority);
1800 ofs->idle_timeout = htons(fs->idle_timeout);
1801 ofs->hard_timeout = htons(fs->hard_timeout);
1802 memset(ofs->pad2, 0, sizeof ofs->pad2);
1803 put_32aligned_be64(&ofs->cookie, fs->cookie);
1804 put_32aligned_be64(&ofs->packet_count,
1805 htonll(unknown_to_zero(fs->packet_count)));
1806 put_32aligned_be64(&ofs->byte_count,
1807 htonll(unknown_to_zero(fs->byte_count)));
1808 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
1809 struct nx_flow_stats *nfs;
1812 ofpbuf_put_uninit(reply, sizeof *nfs);
1813 match_len = nx_put_match(reply, &fs->match, 0, 0);
1814 ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply);
1816 nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
1817 nfs->length = htons(reply->size - start_ofs);
1818 nfs->table_id = fs->table_id;
1820 nfs->duration_sec = htonl(fs->duration_sec);
1821 nfs->duration_nsec = htonl(fs->duration_nsec);
1822 nfs->priority = htons(fs->priority);
1823 nfs->idle_timeout = htons(fs->idle_timeout);
1824 nfs->hard_timeout = htons(fs->hard_timeout);
1825 nfs->idle_age = htons(fs->idle_age < 0 ? 0
1826 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
1828 nfs->hard_age = htons(fs->hard_age < 0 ? 0
1829 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
1831 nfs->match_len = htons(match_len);
1832 nfs->cookie = fs->cookie;
1833 nfs->packet_count = htonll(fs->packet_count);
1834 nfs->byte_count = htonll(fs->byte_count);
1839 ofpmp_postappend(replies, start_ofs);
1842 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
1843 * NXST_AGGREGATE reply matching 'request', and returns the message. */
1845 ofputil_encode_aggregate_stats_reply(
1846 const struct ofputil_aggregate_stats *stats,
1847 const struct ofp_header *request)
1849 struct ofp_aggregate_stats_reply *asr;
1850 uint64_t packet_count;
1851 uint64_t byte_count;
1855 ofpraw_decode(&raw, request);
1856 if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) {
1857 packet_count = unknown_to_zero(stats->packet_count);
1858 byte_count = unknown_to_zero(stats->byte_count);
1860 packet_count = stats->packet_count;
1861 byte_count = stats->byte_count;
1864 msg = ofpraw_alloc_stats_reply(request, 0);
1865 asr = ofpbuf_put_zeros(msg, sizeof *asr);
1866 put_32aligned_be64(&asr->packet_count, htonll(packet_count));
1867 put_32aligned_be64(&asr->byte_count, htonll(byte_count));
1868 asr->flow_count = htonl(stats->flow_count);
1874 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
1875 const struct ofp_header *reply)
1877 struct ofp_aggregate_stats_reply *asr;
1880 ofpbuf_use_const(&msg, reply, ntohs(reply->length));
1881 ofpraw_pull_assert(&msg);
1884 stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
1885 stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
1886 stats->flow_count = ntohl(asr->flow_count);
1891 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
1892 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
1893 * an OpenFlow error code. */
1895 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
1896 const struct ofp_header *oh)
1901 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1902 raw = ofpraw_pull_assert(&b);
1903 if (raw == OFPRAW_OFPT11_FLOW_REMOVED) {
1904 const struct ofp12_flow_removed *ofr;
1907 ofr = ofpbuf_pull(&b, sizeof *ofr);
1909 error = ofputil_pull_ofp11_match(&b, &fr->match, NULL);
1914 fr->priority = ntohs(ofr->priority);
1915 fr->cookie = ofr->cookie;
1916 fr->reason = ofr->reason;
1917 fr->table_id = ofr->table_id;
1918 fr->duration_sec = ntohl(ofr->duration_sec);
1919 fr->duration_nsec = ntohl(ofr->duration_nsec);
1920 fr->idle_timeout = ntohs(ofr->idle_timeout);
1921 fr->hard_timeout = ntohs(ofr->hard_timeout);
1922 fr->packet_count = ntohll(ofr->packet_count);
1923 fr->byte_count = ntohll(ofr->byte_count);
1924 } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
1925 const struct ofp_flow_removed *ofr;
1927 ofr = ofpbuf_pull(&b, sizeof *ofr);
1929 ofputil_match_from_ofp10_match(&ofr->match, &fr->match);
1930 fr->priority = ntohs(ofr->priority);
1931 fr->cookie = ofr->cookie;
1932 fr->reason = ofr->reason;
1934 fr->duration_sec = ntohl(ofr->duration_sec);
1935 fr->duration_nsec = ntohl(ofr->duration_nsec);
1936 fr->idle_timeout = ntohs(ofr->idle_timeout);
1937 fr->hard_timeout = 0;
1938 fr->packet_count = ntohll(ofr->packet_count);
1939 fr->byte_count = ntohll(ofr->byte_count);
1940 } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
1941 struct nx_flow_removed *nfr;
1944 nfr = ofpbuf_pull(&b, sizeof *nfr);
1945 error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match,
1951 return OFPERR_OFPBRC_BAD_LEN;
1954 fr->priority = ntohs(nfr->priority);
1955 fr->cookie = nfr->cookie;
1956 fr->reason = nfr->reason;
1958 fr->duration_sec = ntohl(nfr->duration_sec);
1959 fr->duration_nsec = ntohl(nfr->duration_nsec);
1960 fr->idle_timeout = ntohs(nfr->idle_timeout);
1961 fr->hard_timeout = 0;
1962 fr->packet_count = ntohll(nfr->packet_count);
1963 fr->byte_count = ntohll(nfr->byte_count);
1971 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
1972 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
1975 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
1976 enum ofputil_protocol protocol)
1981 case OFPUTIL_P_OF12: {
1982 struct ofp12_flow_removed *ofr;
1984 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED,
1985 ofputil_protocol_to_ofp_version(protocol),
1986 htonl(0), NXM_TYPICAL_LEN);
1987 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
1988 ofr->cookie = fr->cookie;
1989 ofr->priority = htons(fr->priority);
1990 ofr->reason = fr->reason;
1991 ofr->table_id = fr->table_id;
1992 ofr->duration_sec = htonl(fr->duration_sec);
1993 ofr->duration_nsec = htonl(fr->duration_nsec);
1994 ofr->idle_timeout = htons(fr->idle_timeout);
1995 ofr->hard_timeout = htons(fr->hard_timeout);
1996 ofr->packet_count = htonll(fr->packet_count);
1997 ofr->byte_count = htonll(fr->byte_count);
1998 oxm_put_match(msg, &fr->match);
2002 case OFPUTIL_P_OF10:
2003 case OFPUTIL_P_OF10_TID: {
2004 struct ofp_flow_removed *ofr;
2006 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
2008 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
2009 ofputil_match_to_ofp10_match(&fr->match, &ofr->match);
2010 ofr->cookie = fr->cookie;
2011 ofr->priority = htons(fr->priority);
2012 ofr->reason = fr->reason;
2013 ofr->duration_sec = htonl(fr->duration_sec);
2014 ofr->duration_nsec = htonl(fr->duration_nsec);
2015 ofr->idle_timeout = htons(fr->idle_timeout);
2016 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
2017 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
2022 case OFPUTIL_P_NXM_TID: {
2023 struct nx_flow_removed *nfr;
2026 msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
2027 htonl(0), NXM_TYPICAL_LEN);
2028 nfr = ofpbuf_put_zeros(msg, sizeof *nfr);
2029 match_len = nx_put_match(msg, &fr->match, 0, 0);
2032 nfr->cookie = fr->cookie;
2033 nfr->priority = htons(fr->priority);
2034 nfr->reason = fr->reason;
2035 nfr->duration_sec = htonl(fr->duration_sec);
2036 nfr->duration_nsec = htonl(fr->duration_nsec);
2037 nfr->idle_timeout = htons(fr->idle_timeout);
2038 nfr->match_len = htons(match_len);
2039 nfr->packet_count = htonll(fr->packet_count);
2040 nfr->byte_count = htonll(fr->byte_count);
2052 ofputil_decode_packet_in_finish(struct ofputil_packet_in *pin,
2053 struct match *match, struct ofpbuf *b)
2055 pin->packet = b->data;
2056 pin->packet_len = b->size;
2058 pin->fmd.in_port = match->flow.in_port;
2059 pin->fmd.tun_id = match->flow.tunnel.tun_id;
2060 pin->fmd.metadata = match->flow.metadata;
2061 memcpy(pin->fmd.regs, match->flow.regs, sizeof pin->fmd.regs);
2065 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
2066 const struct ofp_header *oh)
2071 memset(pin, 0, sizeof *pin);
2073 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2074 raw = ofpraw_pull_assert(&b);
2075 if (raw == OFPRAW_OFPT12_PACKET_IN) {
2076 const struct ofp12_packet_in *opi;
2080 opi = ofpbuf_pull(&b, sizeof *opi);
2081 error = oxm_pull_match_loose(&b, &match);
2086 if (!ofpbuf_try_pull(&b, 2)) {
2087 return OFPERR_OFPBRC_BAD_LEN;
2090 pin->reason = opi->reason;
2091 pin->table_id = opi->table_id;
2093 pin->buffer_id = ntohl(opi->buffer_id);
2094 pin->total_len = ntohs(opi->total_len);
2096 ofputil_decode_packet_in_finish(pin, &match, &b);
2097 } else if (raw == OFPRAW_OFPT10_PACKET_IN) {
2098 const struct ofp_packet_in *opi;
2100 opi = ofpbuf_pull(&b, offsetof(struct ofp_packet_in, data));
2102 pin->packet = opi->data;
2103 pin->packet_len = b.size;
2105 pin->fmd.in_port = ntohs(opi->in_port);
2106 pin->reason = opi->reason;
2107 pin->buffer_id = ntohl(opi->buffer_id);
2108 pin->total_len = ntohs(opi->total_len);
2109 } else if (raw == OFPRAW_NXT_PACKET_IN) {
2110 const struct nx_packet_in *npi;
2114 npi = ofpbuf_pull(&b, sizeof *npi);
2115 error = nx_pull_match_loose(&b, ntohs(npi->match_len), &match, NULL,
2121 if (!ofpbuf_try_pull(&b, 2)) {
2122 return OFPERR_OFPBRC_BAD_LEN;
2125 pin->reason = npi->reason;
2126 pin->table_id = npi->table_id;
2127 pin->cookie = npi->cookie;
2129 pin->buffer_id = ntohl(npi->buffer_id);
2130 pin->total_len = ntohs(npi->total_len);
2132 ofputil_decode_packet_in_finish(pin, &match, &b);
2141 ofputil_packet_in_to_match(const struct ofputil_packet_in *pin,
2142 struct match *match)
2146 match_init_catchall(match);
2147 if (pin->fmd.tun_id != htonll(0)) {
2148 match_set_tun_id(match, pin->fmd.tun_id);
2150 if (pin->fmd.metadata != htonll(0)) {
2151 match_set_metadata(match, pin->fmd.metadata);
2154 for (i = 0; i < FLOW_N_REGS; i++) {
2155 if (pin->fmd.regs[i]) {
2156 match_set_reg(match, i, pin->fmd.regs[i]);
2160 match_set_in_port(match, pin->fmd.in_port);
2163 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
2164 * in the format specified by 'packet_in_format'. */
2166 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
2167 enum ofputil_protocol protocol,
2168 enum nx_packet_in_format packet_in_format)
2170 size_t send_len = MIN(pin->send_len, pin->packet_len);
2171 struct ofpbuf *packet;
2173 /* Add OFPT_PACKET_IN. */
2174 if (protocol == OFPUTIL_P_OF12) {
2175 struct ofp12_packet_in *opi;
2178 ofputil_packet_in_to_match(pin, &match);
2180 /* The final argument is just an estimate of the space required. */
2181 packet = ofpraw_alloc_xid(OFPRAW_OFPT12_PACKET_IN, OFP12_VERSION,
2182 htonl(0), (sizeof(struct flow_metadata) * 2
2184 ofpbuf_put_zeros(packet, sizeof *opi);
2185 oxm_put_match(packet, &match);
2186 ofpbuf_put_zeros(packet, 2);
2187 ofpbuf_put(packet, pin->packet, send_len);
2190 opi->buffer_id = htonl(pin->buffer_id);
2191 opi->total_len = htons(pin->total_len);
2192 opi->reason = pin->reason;
2193 opi->table_id = pin->table_id;
2194 } else if (packet_in_format == NXPIF_OPENFLOW10) {
2195 struct ofp_packet_in *opi;
2197 packet = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
2198 htonl(0), send_len);
2199 opi = ofpbuf_put_zeros(packet, offsetof(struct ofp_packet_in, data));
2200 opi->total_len = htons(pin->total_len);
2201 opi->in_port = htons(pin->fmd.in_port);
2202 opi->reason = pin->reason;
2203 opi->buffer_id = htonl(pin->buffer_id);
2205 ofpbuf_put(packet, pin->packet, send_len);
2206 } else if (packet_in_format == NXPIF_NXM) {
2207 struct nx_packet_in *npi;
2211 ofputil_packet_in_to_match(pin, &match);
2213 /* The final argument is just an estimate of the space required. */
2214 packet = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, OFP10_VERSION,
2215 htonl(0), (sizeof(struct flow_metadata) * 2
2217 ofpbuf_put_zeros(packet, sizeof *npi);
2218 match_len = nx_put_match(packet, &match, 0, 0);
2219 ofpbuf_put_zeros(packet, 2);
2220 ofpbuf_put(packet, pin->packet, send_len);
2223 npi->buffer_id = htonl(pin->buffer_id);
2224 npi->total_len = htons(pin->total_len);
2225 npi->reason = pin->reason;
2226 npi->table_id = pin->table_id;
2227 npi->cookie = pin->cookie;
2228 npi->match_len = htons(match_len);
2232 ofpmsg_update_length(packet);
2238 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason)
2240 static char s[INT_STRLEN(int) + 1];
2247 case OFPR_INVALID_TTL:
2248 return "invalid_ttl";
2250 case OFPR_N_REASONS:
2252 sprintf(s, "%d", (int) reason);
2258 ofputil_packet_in_reason_from_string(const char *s,
2259 enum ofp_packet_in_reason *reason)
2263 for (i = 0; i < OFPR_N_REASONS; i++) {
2264 if (!strcasecmp(s, ofputil_packet_in_reason_to_string(i))) {
2272 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
2275 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
2276 * message's actions. The caller must initialize 'ofpacts' and retains
2277 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
2279 * Returns 0 if successful, otherwise an OFPERR_* value. */
2281 ofputil_decode_packet_out(struct ofputil_packet_out *po,
2282 const struct ofp_header *oh,
2283 struct ofpbuf *ofpacts)
2288 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2289 raw = ofpraw_pull_assert(&b);
2291 if (raw == OFPRAW_OFPT11_PACKET_OUT) {
2293 const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
2295 po->buffer_id = ntohl(opo->buffer_id);
2296 error = ofputil_port_from_ofp11(opo->in_port, &po->in_port);
2301 error = ofpacts_pull_openflow11_actions(&b, ntohs(opo->actions_len),
2306 } else if (raw == OFPRAW_OFPT10_PACKET_OUT) {
2308 const struct ofp_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
2310 po->buffer_id = ntohl(opo->buffer_id);
2311 po->in_port = ntohs(opo->in_port);
2313 error = ofpacts_pull_openflow10(&b, ntohs(opo->actions_len), ofpacts);
2321 if (po->in_port >= OFPP_MAX && po->in_port != OFPP_LOCAL
2322 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
2323 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
2325 return OFPERR_OFPBRC_BAD_PORT;
2328 po->ofpacts = ofpacts->data;
2329 po->ofpacts_len = ofpacts->size;
2331 if (po->buffer_id == UINT32_MAX) {
2332 po->packet = b.data;
2333 po->packet_len = b.size;
2342 /* ofputil_phy_port */
2344 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
2345 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2346 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2347 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2348 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2349 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2350 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2351 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2353 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
2354 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
2355 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
2356 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
2357 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
2358 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
2360 static enum netdev_features
2361 netdev_port_features_from_ofp10(ovs_be32 ofp10_)
2363 uint32_t ofp10 = ntohl(ofp10_);
2364 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
2368 netdev_port_features_to_ofp10(enum netdev_features features)
2370 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
2373 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2374 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2375 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2376 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2377 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2378 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2379 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2380 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
2381 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
2382 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
2383 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
2384 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
2385 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
2386 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
2387 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
2388 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
2390 static enum netdev_features
2391 netdev_port_features_from_ofp11(ovs_be32 ofp11)
2393 return ntohl(ofp11) & 0xffff;
2397 netdev_port_features_to_ofp11(enum netdev_features features)
2399 return htonl(features & 0xffff);
2403 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
2404 const struct ofp10_phy_port *opp)
2406 memset(pp, 0, sizeof *pp);
2408 pp->port_no = ntohs(opp->port_no);
2409 memcpy(pp->hw_addr, opp->hw_addr, OFP_ETH_ALEN);
2410 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
2412 pp->config = ntohl(opp->config) & OFPPC10_ALL;
2413 pp->state = ntohl(opp->state) & OFPPS10_ALL;
2415 pp->curr = netdev_port_features_from_ofp10(opp->curr);
2416 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
2417 pp->supported = netdev_port_features_from_ofp10(opp->supported);
2418 pp->peer = netdev_port_features_from_ofp10(opp->peer);
2420 pp->curr_speed = netdev_features_to_bps(pp->curr) / 1000;
2421 pp->max_speed = netdev_features_to_bps(pp->supported) / 1000;
2427 ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
2428 const struct ofp11_port *op)
2432 memset(pp, 0, sizeof *pp);
2434 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
2438 memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN);
2439 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
2441 pp->config = ntohl(op->config) & OFPPC11_ALL;
2442 pp->state = ntohl(op->state) & OFPPC11_ALL;
2444 pp->curr = netdev_port_features_from_ofp11(op->curr);
2445 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
2446 pp->supported = netdev_port_features_from_ofp11(op->supported);
2447 pp->peer = netdev_port_features_from_ofp11(op->peer);
2449 pp->curr_speed = ntohl(op->curr_speed);
2450 pp->max_speed = ntohl(op->max_speed);
2456 ofputil_get_phy_port_size(enum ofp_version ofp_version)
2458 switch (ofp_version) {
2460 return sizeof(struct ofp10_phy_port);
2463 return sizeof(struct ofp11_port);
2470 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
2471 struct ofp10_phy_port *opp)
2473 memset(opp, 0, sizeof *opp);
2475 opp->port_no = htons(pp->port_no);
2476 memcpy(opp->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2477 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2479 opp->config = htonl(pp->config & OFPPC10_ALL);
2480 opp->state = htonl(pp->state & OFPPS10_ALL);
2482 opp->curr = netdev_port_features_to_ofp10(pp->curr);
2483 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
2484 opp->supported = netdev_port_features_to_ofp10(pp->supported);
2485 opp->peer = netdev_port_features_to_ofp10(pp->peer);
2489 ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
2490 struct ofp11_port *op)
2492 memset(op, 0, sizeof *op);
2494 op->port_no = ofputil_port_to_ofp11(pp->port_no);
2495 memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2496 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2498 op->config = htonl(pp->config & OFPPC11_ALL);
2499 op->state = htonl(pp->state & OFPPS11_ALL);
2501 op->curr = netdev_port_features_to_ofp11(pp->curr);
2502 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
2503 op->supported = netdev_port_features_to_ofp11(pp->supported);
2504 op->peer = netdev_port_features_to_ofp11(pp->peer);
2506 op->curr_speed = htonl(pp->curr_speed);
2507 op->max_speed = htonl(pp->max_speed);
2511 ofputil_put_phy_port(enum ofp_version ofp_version,
2512 const struct ofputil_phy_port *pp, struct ofpbuf *b)
2514 switch (ofp_version) {
2515 case OFP10_VERSION: {
2516 struct ofp10_phy_port *opp;
2517 if (b->size + sizeof *opp <= UINT16_MAX) {
2518 opp = ofpbuf_put_uninit(b, sizeof *opp);
2519 ofputil_encode_ofp10_phy_port(pp, opp);
2525 case OFP12_VERSION: {
2526 struct ofp11_port *op;
2527 if (b->size + sizeof *op <= UINT16_MAX) {
2528 op = ofpbuf_put_uninit(b, sizeof *op);
2529 ofputil_encode_ofp11_port(pp, op);
2540 ofputil_append_port_desc_stats_reply(enum ofp_version ofp_version,
2541 const struct ofputil_phy_port *pp,
2542 struct list *replies)
2544 switch (ofp_version) {
2545 case OFP10_VERSION: {
2546 struct ofp10_phy_port *opp;
2548 opp = ofpmp_append(replies, sizeof *opp);
2549 ofputil_encode_ofp10_phy_port(pp, opp);
2554 case OFP12_VERSION: {
2555 struct ofp11_port *op;
2557 op = ofpmp_append(replies, sizeof *op);
2558 ofputil_encode_ofp11_port(pp, op);
2567 /* ofputil_switch_features */
2569 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
2570 OFPC_IP_REASM | OFPC_QUEUE_STATS)
2571 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
2572 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
2573 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
2574 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
2575 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
2576 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
2578 struct ofputil_action_bit_translation {
2579 enum ofputil_action_bitmap ofputil_bit;
2583 static const struct ofputil_action_bit_translation of10_action_bits[] = {
2584 { OFPUTIL_A_OUTPUT, OFPAT10_OUTPUT },
2585 { OFPUTIL_A_SET_VLAN_VID, OFPAT10_SET_VLAN_VID },
2586 { OFPUTIL_A_SET_VLAN_PCP, OFPAT10_SET_VLAN_PCP },
2587 { OFPUTIL_A_STRIP_VLAN, OFPAT10_STRIP_VLAN },
2588 { OFPUTIL_A_SET_DL_SRC, OFPAT10_SET_DL_SRC },
2589 { OFPUTIL_A_SET_DL_DST, OFPAT10_SET_DL_DST },
2590 { OFPUTIL_A_SET_NW_SRC, OFPAT10_SET_NW_SRC },
2591 { OFPUTIL_A_SET_NW_DST, OFPAT10_SET_NW_DST },
2592 { OFPUTIL_A_SET_NW_TOS, OFPAT10_SET_NW_TOS },
2593 { OFPUTIL_A_SET_TP_SRC, OFPAT10_SET_TP_SRC },
2594 { OFPUTIL_A_SET_TP_DST, OFPAT10_SET_TP_DST },
2595 { OFPUTIL_A_ENQUEUE, OFPAT10_ENQUEUE },
2599 static enum ofputil_action_bitmap
2600 decode_action_bits(ovs_be32 of_actions,
2601 const struct ofputil_action_bit_translation *x)
2603 enum ofputil_action_bitmap ofputil_actions;
2605 ofputil_actions = 0;
2606 for (; x->ofputil_bit; x++) {
2607 if (of_actions & htonl(1u << x->of_bit)) {
2608 ofputil_actions |= x->ofputil_bit;
2611 return ofputil_actions;
2615 ofputil_capabilities_mask(enum ofp_version ofp_version)
2617 /* Handle capabilities whose bit is unique for all Open Flow versions */
2618 switch (ofp_version) {
2621 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
2623 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
2625 /* Caller needs to check osf->header.version itself */
2630 /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an
2631 * abstract representation in '*features'. Initializes '*b' to iterate over
2632 * the OpenFlow port structures following 'osf' with later calls to
2633 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an
2634 * OFPERR_* value. */
2636 ofputil_decode_switch_features(const struct ofp_header *oh,
2637 struct ofputil_switch_features *features,
2640 const struct ofp_switch_features *osf;
2643 ofpbuf_use_const(b, oh, ntohs(oh->length));
2644 raw = ofpraw_pull_assert(b);
2646 osf = ofpbuf_pull(b, sizeof *osf);
2647 features->datapath_id = ntohll(osf->datapath_id);
2648 features->n_buffers = ntohl(osf->n_buffers);
2649 features->n_tables = osf->n_tables;
2651 features->capabilities = ntohl(osf->capabilities) &
2652 ofputil_capabilities_mask(oh->version);
2654 if (b->size % ofputil_get_phy_port_size(oh->version)) {
2655 return OFPERR_OFPBRC_BAD_LEN;
2658 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
2659 if (osf->capabilities & htonl(OFPC10_STP)) {
2660 features->capabilities |= OFPUTIL_C_STP;
2662 features->actions = decode_action_bits(osf->actions, of10_action_bits);
2663 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY) {
2664 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
2665 features->capabilities |= OFPUTIL_C_GROUP_STATS;
2667 features->actions = 0;
2669 return OFPERR_OFPBRC_BAD_VERSION;
2675 /* Returns true if the maximum number of ports are in 'oh'. */
2677 max_ports_in_features(const struct ofp_header *oh)
2679 size_t pp_size = ofputil_get_phy_port_size(oh->version);
2680 return ntohs(oh->length) + pp_size > UINT16_MAX;
2683 /* Given a buffer 'b' that contains a Features Reply message, checks if
2684 * it contains the maximum number of ports that will fit. If so, it
2685 * returns true and removes the ports from the message. The caller
2686 * should then send an OFPST_PORT_DESC stats request to get the ports,
2687 * since the switch may have more ports than could be represented in the
2688 * Features Reply. Otherwise, returns false.
2691 ofputil_switch_features_ports_trunc(struct ofpbuf *b)
2693 struct ofp_header *oh = b->data;
2695 if (max_ports_in_features(oh)) {
2696 /* Remove all the ports. */
2697 b->size = (sizeof(struct ofp_header)
2698 + sizeof(struct ofp_switch_features));
2699 ofpmsg_update_length(b);
2708 encode_action_bits(enum ofputil_action_bitmap ofputil_actions,
2709 const struct ofputil_action_bit_translation *x)
2711 uint32_t of_actions;
2714 for (; x->ofputil_bit; x++) {
2715 if (ofputil_actions & x->ofputil_bit) {
2716 of_actions |= 1 << x->of_bit;
2719 return htonl(of_actions);
2722 /* Returns a buffer owned by the caller that encodes 'features' in the format
2723 * required by 'protocol' with the given 'xid'. The caller should append port
2724 * information to the buffer with subsequent calls to
2725 * ofputil_put_switch_features_port(). */
2727 ofputil_encode_switch_features(const struct ofputil_switch_features *features,
2728 enum ofputil_protocol protocol, ovs_be32 xid)
2730 struct ofp_switch_features *osf;
2732 enum ofp_version version;
2735 version = ofputil_protocol_to_ofp_version(protocol);
2738 raw = OFPRAW_OFPT10_FEATURES_REPLY;
2742 raw = OFPRAW_OFPT11_FEATURES_REPLY;
2747 b = ofpraw_alloc_xid(raw, version, xid, 0);
2748 osf = ofpbuf_put_zeros(b, sizeof *osf);
2749 osf->datapath_id = htonll(features->datapath_id);
2750 osf->n_buffers = htonl(features->n_buffers);
2751 osf->n_tables = features->n_tables;
2753 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
2754 osf->capabilities = htonl(features->capabilities &
2755 ofputil_capabilities_mask(version));
2758 if (features->capabilities & OFPUTIL_C_STP) {
2759 osf->capabilities |= htonl(OFPC10_STP);
2761 osf->actions = encode_action_bits(features->actions, of10_action_bits);
2765 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
2766 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
2776 /* Encodes 'pp' into the format required by the switch_features message already
2777 * in 'b', which should have been returned by ofputil_encode_switch_features(),
2778 * and appends the encoded version to 'b'. */
2780 ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
2783 const struct ofp_header *oh = b->data;
2785 ofputil_put_phy_port(oh->version, pp, b);
2788 /* ofputil_port_status */
2790 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
2791 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
2793 ofputil_decode_port_status(const struct ofp_header *oh,
2794 struct ofputil_port_status *ps)
2796 const struct ofp_port_status *ops;
2800 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2801 ofpraw_pull_assert(&b);
2802 ops = ofpbuf_pull(&b, sizeof *ops);
2804 if (ops->reason != OFPPR_ADD &&
2805 ops->reason != OFPPR_DELETE &&
2806 ops->reason != OFPPR_MODIFY) {
2807 return OFPERR_NXBRC_BAD_REASON;
2809 ps->reason = ops->reason;
2811 retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
2812 assert(retval != EOF);
2816 /* Converts the abstract form of a "port status" message in '*ps' into an
2817 * OpenFlow message suitable for 'protocol', and returns that encoded form in
2818 * a buffer owned by the caller. */
2820 ofputil_encode_port_status(const struct ofputil_port_status *ps,
2821 enum ofputil_protocol protocol)
2823 struct ofp_port_status *ops;
2825 enum ofp_version version;
2828 version = ofputil_protocol_to_ofp_version(protocol);
2831 raw = OFPRAW_OFPT10_PORT_STATUS;
2836 raw = OFPRAW_OFPT11_PORT_STATUS;
2843 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
2844 ops = ofpbuf_put_zeros(b, sizeof *ops);
2845 ops->reason = ps->reason;
2846 ofputil_put_phy_port(version, &ps->desc, b);
2847 ofpmsg_update_length(b);
2851 /* ofputil_port_mod */
2853 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
2854 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
2856 ofputil_decode_port_mod(const struct ofp_header *oh,
2857 struct ofputil_port_mod *pm)
2862 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2863 raw = ofpraw_pull_assert(&b);
2865 if (raw == OFPRAW_OFPT10_PORT_MOD) {
2866 const struct ofp10_port_mod *opm = b.data;
2868 pm->port_no = ntohs(opm->port_no);
2869 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
2870 pm->config = ntohl(opm->config) & OFPPC10_ALL;
2871 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
2872 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
2873 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
2874 const struct ofp11_port_mod *opm = b.data;
2877 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
2882 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
2883 pm->config = ntohl(opm->config) & OFPPC11_ALL;
2884 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
2885 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
2887 return OFPERR_OFPBRC_BAD_TYPE;
2890 pm->config &= pm->mask;
2894 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
2895 * message suitable for 'protocol', and returns that encoded form in a buffer
2896 * owned by the caller. */
2898 ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
2899 enum ofputil_protocol protocol)
2901 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
2904 switch (ofp_version) {
2905 case OFP10_VERSION: {
2906 struct ofp10_port_mod *opm;
2908 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
2909 opm = ofpbuf_put_zeros(b, sizeof *opm);
2910 opm->port_no = htons(pm->port_no);
2911 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
2912 opm->config = htonl(pm->config & OFPPC10_ALL);
2913 opm->mask = htonl(pm->mask & OFPPC10_ALL);
2914 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
2919 case OFP12_VERSION: {
2920 struct ofp11_port_mod *opm;
2922 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
2923 opm = ofpbuf_put_zeros(b, sizeof *opm);
2924 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
2925 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
2926 opm->config = htonl(pm->config & OFPPC11_ALL);
2927 opm->mask = htonl(pm->mask & OFPPC11_ALL);
2928 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
2942 ofputil_put_ofp10_table_stats(const struct ofp12_table_stats *in,
2946 enum ofp_flow_wildcards wc10;
2947 enum oxm12_ofb_match_fields mf12;
2950 static const struct wc_map wc_map[] = {
2951 { OFPFW10_IN_PORT, OFPXMT12_OFB_IN_PORT },
2952 { OFPFW10_DL_VLAN, OFPXMT12_OFB_VLAN_VID },
2953 { OFPFW10_DL_SRC, OFPXMT12_OFB_ETH_SRC },
2954 { OFPFW10_DL_DST, OFPXMT12_OFB_ETH_DST},
2955 { OFPFW10_DL_TYPE, OFPXMT12_OFB_ETH_TYPE },
2956 { OFPFW10_NW_PROTO, OFPXMT12_OFB_IP_PROTO },
2957 { OFPFW10_TP_SRC, OFPXMT12_OFB_TCP_SRC },
2958 { OFPFW10_TP_DST, OFPXMT12_OFB_TCP_DST },
2959 { OFPFW10_NW_SRC_MASK, OFPXMT12_OFB_IPV4_SRC },
2960 { OFPFW10_NW_DST_MASK, OFPXMT12_OFB_IPV4_DST },
2961 { OFPFW10_DL_VLAN_PCP, OFPXMT12_OFB_VLAN_PCP },
2962 { OFPFW10_NW_TOS, OFPXMT12_OFB_IP_DSCP },
2965 struct ofp10_table_stats *out;
2966 const struct wc_map *p;
2968 out = ofpbuf_put_uninit(buf, sizeof *out);
2969 out->table_id = in->table_id;
2970 strcpy(out->name, in->name);
2972 for (p = wc_map; p < &wc_map[ARRAY_SIZE(wc_map)]; p++) {
2973 if (in->wildcards & htonll(1ULL << p->mf12)) {
2974 out->wildcards |= htonl(p->wc10);
2977 out->max_entries = in->max_entries;
2978 out->active_count = in->active_count;
2979 put_32aligned_be64(&out->lookup_count, in->lookup_count);
2980 put_32aligned_be64(&out->matched_count, in->matched_count);
2984 oxm12_to_ofp11_flow_match_fields(ovs_be64 oxm12)
2987 enum ofp11_flow_match_fields fmf11;
2988 enum oxm12_ofb_match_fields mf12;
2991 static const struct map map[] = {
2992 { OFPFMF11_IN_PORT, OFPXMT12_OFB_IN_PORT },
2993 { OFPFMF11_DL_VLAN, OFPXMT12_OFB_VLAN_VID },
2994 { OFPFMF11_DL_VLAN_PCP, OFPXMT12_OFB_VLAN_PCP },
2995 { OFPFMF11_DL_TYPE, OFPXMT12_OFB_ETH_TYPE },
2996 { OFPFMF11_NW_TOS, OFPXMT12_OFB_IP_DSCP },
2997 { OFPFMF11_NW_PROTO, OFPXMT12_OFB_IP_PROTO },
2998 { OFPFMF11_TP_SRC, OFPXMT12_OFB_TCP_SRC },
2999 { OFPFMF11_TP_DST, OFPXMT12_OFB_TCP_DST },
3000 { OFPFMF11_MPLS_LABEL, OFPXMT12_OFB_MPLS_LABEL },
3001 { OFPFMF11_MPLS_TC, OFPXMT12_OFB_MPLS_TC },
3002 /* I don't know what OFPFMF11_TYPE means. */
3003 { OFPFMF11_DL_SRC, OFPXMT12_OFB_ETH_SRC },
3004 { OFPFMF11_DL_DST, OFPXMT12_OFB_ETH_DST },
3005 { OFPFMF11_NW_SRC, OFPXMT12_OFB_IPV4_SRC },
3006 { OFPFMF11_NW_DST, OFPXMT12_OFB_IPV4_DST },
3007 { OFPFMF11_METADATA, OFPXMT12_OFB_METADATA },
3010 const struct map *p;
3014 for (p = map; p < &map[ARRAY_SIZE(map)]; p++) {
3015 if (oxm12 & htonll(1ULL << p->mf12)) {
3019 return htonl(fmf11);
3023 ofputil_put_ofp11_table_stats(const struct ofp12_table_stats *in,
3026 struct ofp11_table_stats *out;
3028 out = ofpbuf_put_uninit(buf, sizeof *out);
3029 out->table_id = in->table_id;
3030 strcpy(out->name, in->name);
3031 out->wildcards = oxm12_to_ofp11_flow_match_fields(in->wildcards);
3032 out->match = oxm12_to_ofp11_flow_match_fields(in->match);
3033 out->instructions = in->instructions;
3034 out->write_actions = in->write_actions;
3035 out->apply_actions = in->apply_actions;
3036 out->config = in->config;
3037 out->max_entries = in->max_entries;
3038 out->active_count = in->active_count;
3039 out->lookup_count = in->lookup_count;
3040 out->matched_count = in->matched_count;
3044 ofputil_encode_table_stats_reply(const struct ofp12_table_stats stats[], int n,
3045 const struct ofp_header *request)
3047 struct ofpbuf *reply;
3050 reply = ofpraw_alloc_stats_reply(request, n * sizeof *stats);
3052 switch ((enum ofp_version) request->version) {
3054 for (i = 0; i < n; i++) {
3055 ofputil_put_ofp10_table_stats(&stats[i], reply);
3060 for (i = 0; i < n; i++) {
3061 ofputil_put_ofp11_table_stats(&stats[i], reply);
3066 ofpbuf_put(reply, stats, n * sizeof *stats);
3076 /* ofputil_flow_monitor_request */
3078 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
3079 * ofputil_flow_monitor_request in 'rq'.
3081 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
3082 * message. Calling this function multiple times for a single 'msg' iterates
3083 * through the requests. The caller must initially leave 'msg''s layer
3084 * pointers null and not modify them between calls.
3086 * Returns 0 if successful, EOF if no requests were left in this 'msg',
3087 * otherwise an OFPERR_* value. */
3089 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
3092 struct nx_flow_monitor_request *nfmr;
3096 msg->l2 = msg->data;
3097 ofpraw_pull_assert(msg);
3104 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
3106 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %zu "
3107 "leftover bytes at end", msg->size);
3108 return OFPERR_OFPBRC_BAD_LEN;
3111 flags = ntohs(nfmr->flags);
3112 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
3113 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
3114 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
3115 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
3117 return OFPERR_NXBRC_FM_BAD_FLAGS;
3120 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
3121 return OFPERR_NXBRC_MUST_BE_ZERO;
3124 rq->id = ntohl(nfmr->id);
3126 rq->out_port = ntohs(nfmr->out_port);
3127 rq->table_id = nfmr->table_id;
3129 return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL, NULL);
3133 ofputil_append_flow_monitor_request(
3134 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
3136 struct nx_flow_monitor_request *nfmr;
3141 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
3144 start_ofs = msg->size;
3145 ofpbuf_put_zeros(msg, sizeof *nfmr);
3146 match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
3148 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
3149 nfmr->id = htonl(rq->id);
3150 nfmr->flags = htons(rq->flags);
3151 nfmr->out_port = htons(rq->out_port);
3152 nfmr->match_len = htons(match_len);
3153 nfmr->table_id = rq->table_id;
3156 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
3157 * into an abstract ofputil_flow_update in 'update'. The caller must have
3158 * initialized update->match to point to space allocated for a match.
3160 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
3161 * actions (except for NXFME_ABBREV, which never includes actions). The caller
3162 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
3163 * will point into the 'ofpacts' buffer.
3165 * Multiple flow updates can be packed into a single OpenFlow message. Calling
3166 * this function multiple times for a single 'msg' iterates through the
3167 * updates. The caller must initially leave 'msg''s layer pointers null and
3168 * not modify them between calls.
3170 * Returns 0 if successful, EOF if no updates were left in this 'msg',
3171 * otherwise an OFPERR_* value. */
3173 ofputil_decode_flow_update(struct ofputil_flow_update *update,
3174 struct ofpbuf *msg, struct ofpbuf *ofpacts)
3176 struct nx_flow_update_header *nfuh;
3177 unsigned int length;
3180 msg->l2 = msg->data;
3181 ofpraw_pull_assert(msg);
3188 if (msg->size < sizeof(struct nx_flow_update_header)) {
3193 update->event = ntohs(nfuh->event);
3194 length = ntohs(nfuh->length);
3195 if (length > msg->size || length % 8) {
3199 if (update->event == NXFME_ABBREV) {
3200 struct nx_flow_update_abbrev *nfua;
3202 if (length != sizeof *nfua) {
3206 nfua = ofpbuf_pull(msg, sizeof *nfua);
3207 update->xid = nfua->xid;
3209 } else if (update->event == NXFME_ADDED
3210 || update->event == NXFME_DELETED
3211 || update->event == NXFME_MODIFIED) {
3212 struct nx_flow_update_full *nfuf;
3213 unsigned int actions_len;
3214 unsigned int match_len;
3217 if (length < sizeof *nfuf) {
3221 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
3222 match_len = ntohs(nfuf->match_len);
3223 if (sizeof *nfuf + match_len > length) {
3227 update->reason = ntohs(nfuf->reason);
3228 update->idle_timeout = ntohs(nfuf->idle_timeout);
3229 update->hard_timeout = ntohs(nfuf->hard_timeout);
3230 update->table_id = nfuf->table_id;
3231 update->cookie = nfuf->cookie;
3232 update->priority = ntohs(nfuf->priority);
3234 error = nx_pull_match(msg, match_len, update->match, NULL, NULL);
3239 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
3240 error = ofpacts_pull_openflow10(msg, actions_len, ofpacts);
3245 update->ofpacts = ofpacts->data;
3246 update->ofpacts_len = ofpacts->size;
3249 VLOG_WARN_RL(&bad_ofmsg_rl,
3250 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
3251 ntohs(nfuh->event));
3252 return OFPERR_OFPET_BAD_REQUEST;
3256 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %zu "
3257 "leftover bytes at end", msg->size);
3258 return OFPERR_OFPBRC_BAD_LEN;
3262 ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
3264 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
3266 return ntohl(cancel->id);
3270 ofputil_encode_flow_monitor_cancel(uint32_t id)
3272 struct nx_flow_monitor_cancel *nfmc;
3275 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
3276 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
3277 nfmc->id = htonl(id);
3282 ofputil_start_flow_update(struct list *replies)
3286 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
3290 list_push_back(replies, &msg->list_node);
3294 ofputil_append_flow_update(const struct ofputil_flow_update *update,
3295 struct list *replies)
3297 struct nx_flow_update_header *nfuh;
3301 msg = ofpbuf_from_list(list_back(replies));
3302 start_ofs = msg->size;
3304 if (update->event == NXFME_ABBREV) {
3305 struct nx_flow_update_abbrev *nfua;
3307 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
3308 nfua->xid = update->xid;
3310 struct nx_flow_update_full *nfuf;
3313 ofpbuf_put_zeros(msg, sizeof *nfuf);
3314 match_len = nx_put_match(msg, update->match, htonll(0), htonll(0));
3315 ofpacts_put_openflow10(update->ofpacts, update->ofpacts_len, msg);
3317 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
3318 nfuf->reason = htons(update->reason);
3319 nfuf->priority = htons(update->priority);
3320 nfuf->idle_timeout = htons(update->idle_timeout);
3321 nfuf->hard_timeout = htons(update->hard_timeout);
3322 nfuf->match_len = htons(match_len);
3323 nfuf->table_id = update->table_id;
3324 nfuf->cookie = update->cookie;
3327 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
3328 nfuh->length = htons(msg->size - start_ofs);
3329 nfuh->event = htons(update->event);
3331 ofpmp_postappend(replies, start_ofs);
3335 ofputil_encode_packet_out(const struct ofputil_packet_out *po,
3336 enum ofputil_protocol protocol)
3338 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
3342 size = po->ofpacts_len;
3343 if (po->buffer_id == UINT32_MAX) {
3344 size += po->packet_len;
3347 switch (ofp_version) {
3348 case OFP10_VERSION: {
3349 struct ofp_packet_out *opo;
3352 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
3353 ofpbuf_put_zeros(msg, sizeof *opo);
3354 actions_ofs = msg->size;
3355 ofpacts_put_openflow10(po->ofpacts, po->ofpacts_len, msg);
3358 opo->buffer_id = htonl(po->buffer_id);
3359 opo->in_port = htons(po->in_port);
3360 opo->actions_len = htons(msg->size - actions_ofs);
3365 case OFP12_VERSION: {
3366 struct ofp11_packet_out *opo;
3369 msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size);
3370 ofpbuf_put_zeros(msg, sizeof *opo);
3371 len = ofpacts_put_openflow11_actions(po->ofpacts, po->ofpacts_len, msg);
3374 opo->buffer_id = htonl(po->buffer_id);
3375 opo->in_port = ofputil_port_to_ofp11(po->in_port);
3376 opo->actions_len = htons(len);
3384 if (po->buffer_id == UINT32_MAX) {
3385 ofpbuf_put(msg, po->packet, po->packet_len);
3388 ofpmsg_update_length(msg);
3393 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
3395 make_echo_request(enum ofp_version ofp_version)
3397 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
3401 /* Creates and returns an OFPT_ECHO_REPLY message matching the
3402 * OFPT_ECHO_REQUEST message in 'rq'. */
3404 make_echo_reply(const struct ofp_header *rq)
3406 struct ofpbuf rq_buf;
3407 struct ofpbuf *reply;
3409 ofpbuf_use_const(&rq_buf, rq, ntohs(rq->length));
3410 ofpraw_pull_assert(&rq_buf);
3412 reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY, rq, rq_buf.size);
3413 ofpbuf_put(reply, rq_buf.data, rq_buf.size);
3418 ofputil_encode_barrier_request(enum ofp_version ofp_version)
3422 switch (ofp_version) {
3425 type = OFPRAW_OFPT11_BARRIER_REQUEST;
3429 type = OFPRAW_OFPT10_BARRIER_REQUEST;
3436 return ofpraw_alloc(type, ofp_version, 0);
3440 ofputil_frag_handling_to_string(enum ofp_config_flags flags)
3442 switch (flags & OFPC_FRAG_MASK) {
3443 case OFPC_FRAG_NORMAL: return "normal";
3444 case OFPC_FRAG_DROP: return "drop";
3445 case OFPC_FRAG_REASM: return "reassemble";
3446 case OFPC_FRAG_NX_MATCH: return "nx-match";
3453 ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
3455 if (!strcasecmp(s, "normal")) {
3456 *flags = OFPC_FRAG_NORMAL;
3457 } else if (!strcasecmp(s, "drop")) {
3458 *flags = OFPC_FRAG_DROP;
3459 } else if (!strcasecmp(s, "reassemble")) {
3460 *flags = OFPC_FRAG_REASM;
3461 } else if (!strcasecmp(s, "nx-match")) {
3462 *flags = OFPC_FRAG_NX_MATCH;
3469 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
3470 * port number and stores the latter in '*ofp10_port', for the purpose of
3471 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
3472 * otherwise an OFPERR_* number.
3474 * See the definition of OFP11_MAX for an explanation of the mapping. */
3476 ofputil_port_from_ofp11(ovs_be32 ofp11_port, uint16_t *ofp10_port)
3478 uint32_t ofp11_port_h = ntohl(ofp11_port);
3480 if (ofp11_port_h < OFPP_MAX) {
3481 *ofp10_port = ofp11_port_h;
3483 } else if (ofp11_port_h >= OFPP11_MAX) {
3484 *ofp10_port = ofp11_port_h - OFPP11_OFFSET;
3487 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
3488 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
3489 ofp11_port_h, OFPP_MAX - 1,
3490 (uint32_t) OFPP11_MAX, UINT32_MAX);
3491 return OFPERR_OFPBAC_BAD_OUT_PORT;
3495 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
3496 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
3498 * See the definition of OFP11_MAX for an explanation of the mapping. */
3500 ofputil_port_to_ofp11(uint16_t ofp10_port)
3502 return htonl(ofp10_port < OFPP_MAX
3504 : ofp10_port + OFPP11_OFFSET);
3507 /* Checks that 'port' is a valid output port for the OFPAT10_OUTPUT action, given
3508 * that the switch will never have more than 'max_ports' ports. Returns 0 if
3509 * 'port' is valid, otherwise an OpenFlow return code. */
3511 ofputil_check_output_port(uint16_t port, int max_ports)
3519 case OFPP_CONTROLLER:
3525 if (port < max_ports) {
3528 return OFPERR_OFPBAC_BAD_OUT_PORT;
3532 #define OFPUTIL_NAMED_PORTS \
3533 OFPUTIL_NAMED_PORT(IN_PORT) \
3534 OFPUTIL_NAMED_PORT(TABLE) \
3535 OFPUTIL_NAMED_PORT(NORMAL) \
3536 OFPUTIL_NAMED_PORT(FLOOD) \
3537 OFPUTIL_NAMED_PORT(ALL) \
3538 OFPUTIL_NAMED_PORT(CONTROLLER) \
3539 OFPUTIL_NAMED_PORT(LOCAL) \
3540 OFPUTIL_NAMED_PORT(NONE)
3542 /* Returns the port number represented by 's', which may be an integer or, for
3543 * reserved ports, the standard OpenFlow name for the port (e.g. "LOCAL").
3545 * Returns 0 if 's' is not a valid OpenFlow port number or name. The caller
3546 * should issue an error message in this case, because this function usually
3547 * does not. (This gives the caller an opportunity to look up the port name
3548 * another way, e.g. by contacting the switch and listing the names of all its
3551 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
3552 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
3553 * range as described in include/openflow/openflow-1.1.h. */
3555 ofputil_port_from_string(const char *s)
3557 unsigned int port32;
3559 if (str_to_uint(s, 10, &port32)) {
3561 VLOG_WARN("port 0 is not a valid OpenFlow port number");
3563 } else if (port32 < OFPP_MAX) {
3565 } else if (port32 < OFPP_FIRST_RESV) {
3566 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
3567 "be translated to %u when talking to an OF1.1 or "
3568 "later controller", port32, port32 + OFPP11_OFFSET);
3570 } else if (port32 <= OFPP_LAST_RESV) {
3574 ofputil_format_port(port32, &s);
3575 VLOG_WARN_ONCE("referring to port %s as %u is deprecated for "
3576 "compatibility with future versions of OpenFlow",
3577 ds_cstr(&s), port32);
3581 } else if (port32 < OFPP11_MAX) {
3582 VLOG_WARN("port %u is outside the supported range 0 through "
3583 "%"PRIx16"or 0x%x through 0x%"PRIx32, port32,
3584 UINT16_MAX, (unsigned int) OFPP11_MAX, UINT32_MAX);
3587 return port32 - OFPP11_OFFSET;
3594 static const struct pair pairs[] = {
3595 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
3597 #undef OFPUTIL_NAMED_PORT
3599 const struct pair *p;
3601 for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) {
3602 if (!strcasecmp(s, p->name)) {
3610 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
3611 * Most ports' string representation is just the port number, but for special
3612 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
3614 ofputil_format_port(uint16_t port, struct ds *s)
3619 #define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break;
3621 #undef OFPUTIL_NAMED_PORT
3624 ds_put_format(s, "%"PRIu16, port);
3627 ds_put_cstr(s, name);
3630 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
3631 * 'ofp_version', tries to pull the first element from the array. If
3632 * successful, initializes '*pp' with an abstract representation of the
3633 * port and returns 0. If no ports remain to be decoded, returns EOF.
3634 * On an error, returns a positive OFPERR_* value. */
3636 ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
3637 struct ofputil_phy_port *pp)
3639 switch (ofp_version) {
3640 case OFP10_VERSION: {
3641 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
3642 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
3645 case OFP12_VERSION: {
3646 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
3647 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
3654 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
3655 * 'ofp_version', returns the number of elements. */
3656 size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *b)
3658 return b->size / ofputil_get_phy_port_size(ofp_version);
3661 /* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if
3662 * 'name' is "output" then the return value is OFPUTIL_OFPAT10_OUTPUT), or -1 if
3663 * 'name' is not the name of any action.
3665 * ofp-util.def lists the mapping from names to action. */
3667 ofputil_action_code_from_name(const char *name)
3669 static const char *names[OFPUTIL_N_ACTIONS] = {
3671 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) NAME,
3672 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
3673 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
3674 #include "ofp-util.def"
3679 for (p = names; p < &names[ARRAY_SIZE(names)]; p++) {
3680 if (*p && !strcasecmp(name, *p)) {
3687 /* Appends an action of the type specified by 'code' to 'buf' and returns the
3688 * action. Initializes the parts of 'action' that identify it as having type
3689 * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that
3690 * have variable length, the length used and cleared is that of struct
3693 ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
3696 case OFPUTIL_ACTION_INVALID:
3699 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
3700 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
3701 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3702 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
3703 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3704 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
3705 #include "ofp-util.def"
3710 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
3712 ofputil_init_##ENUM(struct STRUCT *s) \
3714 memset(s, 0, sizeof *s); \
3715 s->type = htons(ENUM); \
3716 s->len = htons(sizeof *s); \
3720 ofputil_put_##ENUM(struct ofpbuf *buf) \
3722 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
3723 ofputil_init_##ENUM(s); \
3726 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3727 OFPAT10_ACTION(ENUM, STRUCT, NAME)
3728 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3730 ofputil_init_##ENUM(struct STRUCT *s) \
3732 memset(s, 0, sizeof *s); \
3733 s->type = htons(OFPAT10_VENDOR); \
3734 s->len = htons(sizeof *s); \
3735 s->vendor = htonl(NX_VENDOR_ID); \
3736 s->subtype = htons(ENUM); \
3740 ofputil_put_##ENUM(struct ofpbuf *buf) \
3742 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
3743 ofputil_init_##ENUM(s); \
3746 #include "ofp-util.def"
3749 ofputil_normalize_match__(struct match *match, bool may_log)
3752 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
3753 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
3754 MAY_NW_PROTO = 1 << 2, /* nw_proto */
3755 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
3756 MAY_ARP_SHA = 1 << 4, /* arp_sha */
3757 MAY_ARP_THA = 1 << 5, /* arp_tha */
3758 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
3759 MAY_ND_TARGET = 1 << 7 /* nd_target */
3762 struct flow_wildcards wc;
3764 /* Figure out what fields may be matched. */
3765 if (match->flow.dl_type == htons(ETH_TYPE_IP)) {
3766 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
3767 if (match->flow.nw_proto == IPPROTO_TCP ||
3768 match->flow.nw_proto == IPPROTO_UDP ||
3769 match->flow.nw_proto == IPPROTO_ICMP) {
3770 may_match |= MAY_TP_ADDR;
3772 } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
3773 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
3774 if (match->flow.nw_proto == IPPROTO_TCP ||
3775 match->flow.nw_proto == IPPROTO_UDP) {
3776 may_match |= MAY_TP_ADDR;
3777 } else if (match->flow.nw_proto == IPPROTO_ICMPV6) {
3778 may_match |= MAY_TP_ADDR;
3779 if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
3780 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
3781 } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
3782 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
3785 } else if (match->flow.dl_type == htons(ETH_TYPE_ARP)) {
3786 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
3791 /* Clear the fields that may not be matched. */
3793 if (!(may_match & MAY_NW_ADDR)) {
3794 wc.masks.nw_src = wc.masks.nw_dst = htonl(0);
3796 if (!(may_match & MAY_TP_ADDR)) {
3797 wc.masks.tp_src = wc.masks.tp_dst = htons(0);
3799 if (!(may_match & MAY_NW_PROTO)) {
3800 wc.masks.nw_proto = 0;
3802 if (!(may_match & MAY_IPVx)) {
3803 wc.masks.nw_tos = 0;
3804 wc.masks.nw_ttl = 0;
3806 if (!(may_match & MAY_ARP_SHA)) {
3807 memset(wc.masks.arp_sha, 0, ETH_ADDR_LEN);
3809 if (!(may_match & MAY_ARP_THA)) {
3810 memset(wc.masks.arp_tha, 0, ETH_ADDR_LEN);
3812 if (!(may_match & MAY_IPV6)) {
3813 wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any;
3814 wc.masks.ipv6_label = htonl(0);
3816 if (!(may_match & MAY_ND_TARGET)) {
3817 wc.masks.nd_target = in6addr_any;
3820 /* Log any changes. */
3821 if (!flow_wildcards_equal(&wc, &match->wc)) {
3822 bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
3823 char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL;
3826 match_zero_wildcarded_fields(match);
3829 char *post = match_to_string(match, OFP_DEFAULT_PRIORITY);
3830 VLOG_INFO("normalization changed ofp_match, details:");
3831 VLOG_INFO(" pre: %s", pre);
3832 VLOG_INFO("post: %s", post);
3839 /* "Normalizes" the wildcards in 'match'. That means:
3841 * 1. If the type of level N is known, then only the valid fields for that
3842 * level may be specified. For example, ARP does not have a TOS field,
3843 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
3844 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
3845 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
3848 * 2. If the type of level N is not known (or not understood by Open
3849 * vSwitch), then no fields at all for that level may be specified. For
3850 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
3851 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
3854 * If this function changes 'match', it logs a rate-limited informational
3857 ofputil_normalize_match(struct match *match)
3859 ofputil_normalize_match__(match, true);
3862 /* Same as ofputil_normalize_match() without the logging. Thus, this function
3863 * is suitable for a program's internal use, whereas ofputil_normalize_match()
3864 * sense for use on flows received from elsewhere (so that a bug in the program
3865 * that sent them can be reported and corrected). */
3867 ofputil_normalize_match_quiet(struct match *match)
3869 ofputil_normalize_match__(match, false);
3872 /* Parses a key or a key-value pair from '*stringp'.
3874 * On success: Stores the key into '*keyp'. Stores the value, if present, into
3875 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
3876 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
3877 * are substrings of '*stringp' created by replacing some of its bytes by null
3878 * terminators. Returns true.
3880 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
3881 * NULL and returns false. */
3883 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
3885 char *pos, *key, *value;
3889 pos += strspn(pos, ", \t\r\n");
3891 *keyp = *valuep = NULL;
3896 key_len = strcspn(pos, ":=(, \t\r\n");
3897 if (key[key_len] == ':' || key[key_len] == '=') {
3898 /* The value can be separated by a colon. */
3901 value = key + key_len + 1;
3902 value_len = strcspn(value, ", \t\r\n");
3903 pos = value + value_len + (value[value_len] != '\0');
3904 value[value_len] = '\0';
3905 } else if (key[key_len] == '(') {
3906 /* The value can be surrounded by balanced parentheses. The outermost
3907 * set of parentheses is removed. */
3911 value = key + key_len + 1;
3912 for (value_len = 0; level > 0; value_len++) {
3913 switch (value[value_len]) {
3927 value[value_len - 1] = '\0';
3928 pos = value + value_len;
3930 /* There might be no value at all. */
3931 value = key + key_len; /* Will become the empty string below. */
3932 pos = key + key_len + (key[key_len] != '\0');
3934 key[key_len] = '\0';
3942 /* Encode a dump ports request for 'port', the encoded message
3943 * will be fore Open Flow version 'ofp_version'. Returns message
3944 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
3946 ofputil_encode_dump_ports_request(enum ofp_version ofp_version, int16_t port)
3948 struct ofpbuf *request;
3950 switch (ofp_version) {
3951 case OFP10_VERSION: {
3952 struct ofp10_port_stats_request *req;
3953 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0);
3954 req = ofpbuf_put_zeros(request, sizeof *req);
3955 req->port_no = htons(port);
3959 case OFP12_VERSION: {
3960 struct ofp11_port_stats_request *req;
3961 request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0);
3962 req = ofpbuf_put_zeros(request, sizeof *req);
3963 req->port_no = ofputil_port_to_ofp11(port);
3974 ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops,
3975 struct ofp10_port_stats *ps10)
3977 ps10->port_no = htons(ops->port_no);
3978 memset(ps10->pad, 0, sizeof ps10->pad);
3979 put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets));
3980 put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets));
3981 put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes));
3982 put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes));
3983 put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped));
3984 put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped));
3985 put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors));
3986 put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors));
3987 put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors));
3988 put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors));
3989 put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors));
3990 put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions));
3994 ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops,
3995 struct ofp11_port_stats *ps11)
3997 ps11->port_no = ofputil_port_to_ofp11(ops->port_no);
3998 memset(ps11->pad, 0, sizeof ps11->pad);
3999 ps11->rx_packets = htonll(ops->stats.rx_packets);
4000 ps11->tx_packets = htonll(ops->stats.tx_packets);
4001 ps11->rx_bytes = htonll(ops->stats.rx_bytes);
4002 ps11->tx_bytes = htonll(ops->stats.tx_bytes);
4003 ps11->rx_dropped = htonll(ops->stats.rx_dropped);
4004 ps11->tx_dropped = htonll(ops->stats.tx_dropped);
4005 ps11->rx_errors = htonll(ops->stats.rx_errors);
4006 ps11->tx_errors = htonll(ops->stats.tx_errors);
4007 ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors);
4008 ps11->rx_over_err = htonll(ops->stats.rx_over_errors);
4009 ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors);
4010 ps11->collisions = htonll(ops->stats.collisions);
4013 /* Encode a ports stat for 'opes' and append it to 'replies'.
4014 * The encoded message will be for Open Flow version 'ofp_version'. */
4016 ofputil_append_port_stat(struct list *replies,
4017 const struct ofputil_port_stats *ops)
4019 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
4020 struct ofp_header *oh = msg->data;
4022 switch ((enum ofp_version)oh->version) {
4024 case OFP11_VERSION: {
4025 struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply);
4026 ofputil_port_stats_to_ofp11(ops, reply);
4030 case OFP10_VERSION: {
4031 struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply);
4032 ofputil_port_stats_to_ofp10(ops, reply);
4042 ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops,
4043 const struct ofp10_port_stats *ps10)
4045 memset(ops, 0, sizeof *ops);
4047 ops->port_no = ntohs(ps10->port_no);
4048 ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets));
4049 ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets));
4050 ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes));
4051 ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes));
4052 ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped));
4053 ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped));
4054 ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors));
4055 ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors));
4056 ops->stats.rx_frame_errors =
4057 ntohll(get_32aligned_be64(&ps10->rx_frame_err));
4058 ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err));
4059 ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err));
4060 ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions));
4066 ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops,
4067 const struct ofp11_port_stats *ps11)
4071 memset(ops, 0, sizeof *ops);
4072 error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no);
4077 ops->stats.rx_packets = ntohll(ps11->rx_packets);
4078 ops->stats.tx_packets = ntohll(ps11->tx_packets);
4079 ops->stats.rx_bytes = ntohll(ps11->rx_bytes);
4080 ops->stats.tx_bytes = ntohll(ps11->tx_bytes);
4081 ops->stats.rx_dropped = ntohll(ps11->rx_dropped);
4082 ops->stats.tx_dropped = ntohll(ps11->tx_dropped);
4083 ops->stats.rx_errors = ntohll(ps11->rx_errors);
4084 ops->stats.tx_errors = ntohll(ps11->tx_errors);
4085 ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err);
4086 ops->stats.rx_over_errors = ntohll(ps11->rx_over_err);
4087 ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err);
4088 ops->stats.collisions = ntohll(ps11->collisions);
4093 /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
4096 ofputil_count_port_stats(const struct ofp_header *oh)
4100 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4101 ofpraw_pull_assert(&b);
4103 BUILD_ASSERT(sizeof(struct ofp10_port_stats) ==
4104 sizeof(struct ofp11_port_stats));
4105 return b.size / sizeof(struct ofp10_port_stats);
4108 /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
4109 * ofputil_port_stats in 'ps'.
4111 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
4112 * message. Calling this function multiple times for a single 'msg' iterates
4113 * through the replies. The caller must initially leave 'msg''s layer pointers
4114 * null and not modify them between calls.
4116 * Returns 0 if successful, EOF if no replies were left in this 'msg',
4117 * otherwise a positive errno value. */
4119 ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg)
4125 ? ofpraw_decode(&raw, msg->l2)
4126 : ofpraw_pull(&raw, msg));
4133 } else if (raw == OFPRAW_OFPST11_PORT_REPLY) {
4134 const struct ofp11_port_stats *ps11;
4136 ps11 = ofpbuf_try_pull(msg, sizeof *ps11);
4138 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %zu leftover "
4139 "bytes at end", msg->size);
4140 return OFPERR_OFPBRC_BAD_LEN;
4142 return ofputil_port_stats_from_ofp11(ps, ps11);
4143 } else if (raw == OFPRAW_OFPST10_PORT_REPLY) {
4144 const struct ofp10_port_stats *ps10;
4146 ps10 = ofpbuf_try_pull(msg, sizeof *ps10);
4148 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %zu leftover "
4149 "bytes at end", msg->size);
4150 return OFPERR_OFPBRC_BAD_LEN;
4152 return ofputil_port_stats_from_ofp10(ps, ps10);
4159 /* Parse a port status request message into a 16 bit OpenFlow 1.0
4160 * port number and stores the latter in '*ofp10_port'.
4161 * Returns 0 if successful, otherwise an OFPERR_* number. */
4163 ofputil_decode_port_stats_request(const struct ofp_header *request,
4164 uint16_t *ofp10_port)
4166 switch ((enum ofp_version)request->version) {
4168 case OFP11_VERSION: {
4169 const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request);
4170 return ofputil_port_from_ofp11(psr11->port_no, ofp10_port);
4173 case OFP10_VERSION: {
4174 const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request);
4175 *ofp10_port = ntohs(psr10->port_no);