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"
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
28 #include "byte-order.h"
29 #include "classifier.h"
30 #include "dynamic-string.h"
32 #include "meta-flow.h"
33 #include "multipath.h"
36 #include "ofp-actions.h"
37 #include "ofp-errors.h"
43 #include "unaligned.h"
44 #include "type-props.h"
47 VLOG_DEFINE_THIS_MODULE(ofp_util);
49 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
50 * in the peer and so there's not much point in showing a lot of them. */
51 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
53 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
54 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
57 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
58 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
59 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
60 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
63 ofputil_wcbits_to_netmask(int wcbits)
66 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
69 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
70 * that it wildcards, that is, the number of 0-bits in 'netmask', a number
71 * between 0 and 32 inclusive.
73 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
74 * still be in the valid range but isn't otherwise meaningful. */
76 ofputil_netmask_to_wcbits(ovs_be32 netmask)
78 return 32 - ip_count_cidr_bits(netmask);
81 /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
82 * flow_wildcards in 'wc' for use in struct match. It is the caller's
83 * responsibility to handle the special case where the flow match's dl_vlan is
84 * set to OFP_VLAN_NONE. */
86 ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc)
88 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 18);
90 /* Initialize most of wc. */
91 flow_wildcards_init_catchall(wc);
93 if (!(ofpfw & OFPFW10_IN_PORT)) {
94 wc->masks.in_port = UINT16_MAX;
97 if (!(ofpfw & OFPFW10_NW_TOS)) {
98 wc->masks.nw_tos |= IP_DSCP_MASK;
101 if (!(ofpfw & OFPFW10_NW_PROTO)) {
102 wc->masks.nw_proto = UINT8_MAX;
104 wc->masks.nw_src = ofputil_wcbits_to_netmask(ofpfw
105 >> OFPFW10_NW_SRC_SHIFT);
106 wc->masks.nw_dst = ofputil_wcbits_to_netmask(ofpfw
107 >> OFPFW10_NW_DST_SHIFT);
109 if (!(ofpfw & OFPFW10_TP_SRC)) {
110 wc->masks.tp_src = htons(UINT16_MAX);
112 if (!(ofpfw & OFPFW10_TP_DST)) {
113 wc->masks.tp_dst = htons(UINT16_MAX);
116 if (!(ofpfw & OFPFW10_DL_SRC)) {
117 memset(wc->masks.dl_src, 0xff, ETH_ADDR_LEN);
119 if (!(ofpfw & OFPFW10_DL_DST)) {
120 memset(wc->masks.dl_dst, 0xff, ETH_ADDR_LEN);
122 if (!(ofpfw & OFPFW10_DL_TYPE)) {
123 wc->masks.dl_type = htons(UINT16_MAX);
127 if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) {
128 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
130 if (!(ofpfw & OFPFW10_DL_VLAN)) {
131 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
135 /* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */
137 ofputil_match_from_ofp10_match(const struct ofp10_match *ofmatch,
140 uint32_t ofpfw = ntohl(ofmatch->wildcards) & OFPFW10_ALL;
142 /* Initialize match->wc. */
143 memset(&match->flow, 0, sizeof match->flow);
144 ofputil_wildcard_from_ofpfw10(ofpfw, &match->wc);
146 /* Initialize most of match->flow. */
147 match->flow.nw_src = ofmatch->nw_src;
148 match->flow.nw_dst = ofmatch->nw_dst;
149 match->flow.in_port = ntohs(ofmatch->in_port);
150 match->flow.dl_type = ofputil_dl_type_from_openflow(ofmatch->dl_type);
151 match->flow.tp_src = ofmatch->tp_src;
152 match->flow.tp_dst = ofmatch->tp_dst;
153 memcpy(match->flow.dl_src, ofmatch->dl_src, ETH_ADDR_LEN);
154 memcpy(match->flow.dl_dst, ofmatch->dl_dst, ETH_ADDR_LEN);
155 match->flow.nw_tos = ofmatch->nw_tos & IP_DSCP_MASK;
156 match->flow.nw_proto = ofmatch->nw_proto;
158 /* Translate VLANs. */
159 if (!(ofpfw & OFPFW10_DL_VLAN) &&
160 ofmatch->dl_vlan == htons(OFP10_VLAN_NONE)) {
161 /* Match only packets without 802.1Q header.
163 * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
165 * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
166 * because we can't have a specific PCP without an 802.1Q header.
167 * However, older versions of OVS treated this as matching packets
168 * withut an 802.1Q header, so we do here too. */
169 match->flow.vlan_tci = htons(0);
170 match->wc.masks.vlan_tci = htons(0xffff);
172 ovs_be16 vid, pcp, tci;
174 vid = ofmatch->dl_vlan & htons(VLAN_VID_MASK);
175 pcp = htons((ofmatch->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
176 tci = vid | pcp | htons(VLAN_CFI);
177 match->flow.vlan_tci = tci & match->wc.masks.vlan_tci;
181 match_zero_wildcarded_fields(match);
184 /* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */
186 ofputil_match_to_ofp10_match(const struct match *match,
187 struct ofp10_match *ofmatch)
189 const struct flow_wildcards *wc = &match->wc;
192 /* Figure out most OpenFlow wildcards. */
194 if (!wc->masks.in_port) {
195 ofpfw |= OFPFW10_IN_PORT;
197 if (!wc->masks.dl_type) {
198 ofpfw |= OFPFW10_DL_TYPE;
200 if (!wc->masks.nw_proto) {
201 ofpfw |= OFPFW10_NW_PROTO;
203 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_src)
204 << OFPFW10_NW_SRC_SHIFT);
205 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_dst)
206 << OFPFW10_NW_DST_SHIFT);
207 if (!(wc->masks.nw_tos & IP_DSCP_MASK)) {
208 ofpfw |= OFPFW10_NW_TOS;
210 if (!wc->masks.tp_src) {
211 ofpfw |= OFPFW10_TP_SRC;
213 if (!wc->masks.tp_dst) {
214 ofpfw |= OFPFW10_TP_DST;
216 if (eth_addr_is_zero(wc->masks.dl_src)) {
217 ofpfw |= OFPFW10_DL_SRC;
219 if (eth_addr_is_zero(wc->masks.dl_dst)) {
220 ofpfw |= OFPFW10_DL_DST;
223 /* Translate VLANs. */
224 ofmatch->dl_vlan = htons(0);
225 ofmatch->dl_vlan_pcp = 0;
226 if (match->wc.masks.vlan_tci == htons(0)) {
227 ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP;
228 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
229 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
230 ofmatch->dl_vlan = htons(OFP10_VLAN_NONE);
231 ofpfw |= OFPFW10_DL_VLAN_PCP;
233 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
234 ofpfw |= OFPFW10_DL_VLAN;
236 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
239 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
240 ofpfw |= OFPFW10_DL_VLAN_PCP;
242 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
246 /* Compose most of the match structure. */
247 ofmatch->wildcards = htonl(ofpfw);
248 ofmatch->in_port = htons(match->flow.in_port);
249 memcpy(ofmatch->dl_src, match->flow.dl_src, ETH_ADDR_LEN);
250 memcpy(ofmatch->dl_dst, match->flow.dl_dst, ETH_ADDR_LEN);
251 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
252 ofmatch->nw_src = match->flow.nw_src;
253 ofmatch->nw_dst = match->flow.nw_dst;
254 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
255 ofmatch->nw_proto = match->flow.nw_proto;
256 ofmatch->tp_src = match->flow.tp_src;
257 ofmatch->tp_dst = match->flow.tp_dst;
258 memset(ofmatch->pad1, '\0', sizeof ofmatch->pad1);
259 memset(ofmatch->pad2, '\0', sizeof ofmatch->pad2);
263 ofputil_pull_ofp11_match(struct ofpbuf *buf, struct match *match,
264 uint16_t *padded_match_len)
266 struct ofp11_match_header *omh = buf->data;
269 if (buf->size < sizeof *omh) {
270 return OFPERR_OFPBMC_BAD_LEN;
273 match_len = ntohs(omh->length);
275 switch (ntohs(omh->type)) {
276 case OFPMT_STANDARD: {
277 struct ofp11_match *om;
279 if (match_len != sizeof *om || buf->size < sizeof *om) {
280 return OFPERR_OFPBMC_BAD_LEN;
282 om = ofpbuf_pull(buf, sizeof *om);
283 if (padded_match_len) {
284 *padded_match_len = match_len;
286 return ofputil_match_from_ofp11_match(om, match);
290 if (padded_match_len) {
291 *padded_match_len = ROUND_UP(match_len, 8);
293 return oxm_pull_match(buf, match);
296 return OFPERR_OFPBMC_BAD_TYPE;
300 /* Converts the ofp11_match in 'match' into a struct match in 'match. Returns
301 * 0 if successful, otherwise an OFPERR_* value. */
303 ofputil_match_from_ofp11_match(const struct ofp11_match *ofmatch,
306 uint16_t wc = ntohl(ofmatch->wildcards);
307 uint8_t dl_src_mask[ETH_ADDR_LEN];
308 uint8_t dl_dst_mask[ETH_ADDR_LEN];
309 bool ipv4, arp, rarp;
312 match_init_catchall(match);
314 if (!(wc & OFPFW11_IN_PORT)) {
318 error = ofputil_port_from_ofp11(ofmatch->in_port, &ofp_port);
320 return OFPERR_OFPBMC_BAD_VALUE;
322 match_set_in_port(match, ofp_port);
325 for (i = 0; i < ETH_ADDR_LEN; i++) {
326 dl_src_mask[i] = ~ofmatch->dl_src_mask[i];
328 match_set_dl_src_masked(match, ofmatch->dl_src, dl_src_mask);
330 for (i = 0; i < ETH_ADDR_LEN; i++) {
331 dl_dst_mask[i] = ~ofmatch->dl_dst_mask[i];
333 match_set_dl_dst_masked(match, ofmatch->dl_dst, dl_dst_mask);
335 if (!(wc & OFPFW11_DL_VLAN)) {
336 if (ofmatch->dl_vlan == htons(OFPVID11_NONE)) {
337 /* Match only packets without a VLAN tag. */
338 match->flow.vlan_tci = htons(0);
339 match->wc.masks.vlan_tci = htons(UINT16_MAX);
341 if (ofmatch->dl_vlan == htons(OFPVID11_ANY)) {
342 /* Match any packet with a VLAN tag regardless of VID. */
343 match->flow.vlan_tci = htons(VLAN_CFI);
344 match->wc.masks.vlan_tci = htons(VLAN_CFI);
345 } else if (ntohs(ofmatch->dl_vlan) < 4096) {
346 /* Match only packets with the specified VLAN VID. */
347 match->flow.vlan_tci = htons(VLAN_CFI) | ofmatch->dl_vlan;
348 match->wc.masks.vlan_tci = htons(VLAN_CFI | VLAN_VID_MASK);
351 return OFPERR_OFPBMC_BAD_VALUE;
354 if (!(wc & OFPFW11_DL_VLAN_PCP)) {
355 if (ofmatch->dl_vlan_pcp <= 7) {
356 match->flow.vlan_tci |= htons(ofmatch->dl_vlan_pcp
358 match->wc.masks.vlan_tci |= htons(VLAN_PCP_MASK);
361 return OFPERR_OFPBMC_BAD_VALUE;
367 if (!(wc & OFPFW11_DL_TYPE)) {
368 match_set_dl_type(match,
369 ofputil_dl_type_from_openflow(ofmatch->dl_type));
372 ipv4 = match->flow.dl_type == htons(ETH_TYPE_IP);
373 arp = match->flow.dl_type == htons(ETH_TYPE_ARP);
374 rarp = match->flow.dl_type == htons(ETH_TYPE_RARP);
376 if (ipv4 && !(wc & OFPFW11_NW_TOS)) {
377 if (ofmatch->nw_tos & ~IP_DSCP_MASK) {
379 return OFPERR_OFPBMC_BAD_VALUE;
382 match_set_nw_dscp(match, ofmatch->nw_tos);
385 if (ipv4 || arp || rarp) {
386 if (!(wc & OFPFW11_NW_PROTO)) {
387 match_set_nw_proto(match, ofmatch->nw_proto);
389 match_set_nw_src_masked(match, ofmatch->nw_src, ~ofmatch->nw_src_mask);
390 match_set_nw_dst_masked(match, ofmatch->nw_dst, ~ofmatch->nw_dst_mask);
393 #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
394 if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) {
395 switch (match->flow.nw_proto) {
397 /* "A.2.3 Flow Match Structures" in OF1.1 says:
399 * The tp_src and tp_dst fields will be ignored unless the
400 * network protocol specified is as TCP, UDP or SCTP.
402 * but I'm pretty sure we should support ICMP too, otherwise
403 * that's a regression from OF1.0. */
404 if (!(wc & OFPFW11_TP_SRC)) {
405 uint16_t icmp_type = ntohs(ofmatch->tp_src);
406 if (icmp_type < 0x100) {
407 match_set_icmp_type(match, icmp_type);
409 return OFPERR_OFPBMC_BAD_FIELD;
412 if (!(wc & OFPFW11_TP_DST)) {
413 uint16_t icmp_code = ntohs(ofmatch->tp_dst);
414 if (icmp_code < 0x100) {
415 match_set_icmp_code(match, icmp_code);
417 return OFPERR_OFPBMC_BAD_FIELD;
424 if (!(wc & (OFPFW11_TP_SRC))) {
425 match_set_tp_src(match, ofmatch->tp_src);
427 if (!(wc & (OFPFW11_TP_DST))) {
428 match_set_tp_dst(match, ofmatch->tp_dst);
433 /* We don't support SCTP and it seems that we should tell the
434 * controller, since OF1.1 implementations are supposed to. */
435 return OFPERR_OFPBMC_BAD_FIELD;
438 /* OF1.1 says explicitly to ignore this. */
443 if (match->flow.dl_type == htons(ETH_TYPE_MPLS) ||
444 match->flow.dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
445 enum { OFPFW11_MPLS_ALL = OFPFW11_MPLS_LABEL | OFPFW11_MPLS_TC };
447 if ((wc & OFPFW11_MPLS_ALL) != OFPFW11_MPLS_ALL) {
448 /* MPLS not supported. */
449 return OFPERR_OFPBMC_BAD_TAG;
453 match_set_metadata_masked(match, ofmatch->metadata,
454 ~ofmatch->metadata_mask);
459 /* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */
461 ofputil_match_to_ofp11_match(const struct match *match,
462 struct ofp11_match *ofmatch)
467 memset(ofmatch, 0, sizeof *ofmatch);
468 ofmatch->omh.type = htons(OFPMT_STANDARD);
469 ofmatch->omh.length = htons(OFPMT11_STANDARD_LENGTH);
471 if (!match->wc.masks.in_port) {
472 wc |= OFPFW11_IN_PORT;
474 ofmatch->in_port = ofputil_port_to_ofp11(match->flow.in_port);
477 memcpy(ofmatch->dl_src, match->flow.dl_src, ETH_ADDR_LEN);
478 for (i = 0; i < ETH_ADDR_LEN; i++) {
479 ofmatch->dl_src_mask[i] = ~match->wc.masks.dl_src[i];
482 memcpy(ofmatch->dl_dst, match->flow.dl_dst, ETH_ADDR_LEN);
483 for (i = 0; i < ETH_ADDR_LEN; i++) {
484 ofmatch->dl_dst_mask[i] = ~match->wc.masks.dl_dst[i];
487 if (match->wc.masks.vlan_tci == htons(0)) {
488 wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP;
489 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
490 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
491 ofmatch->dl_vlan = htons(OFPVID11_NONE);
492 wc |= OFPFW11_DL_VLAN_PCP;
494 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
495 ofmatch->dl_vlan = htons(OFPVID11_ANY);
497 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
500 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
501 wc |= OFPFW11_DL_VLAN_PCP;
503 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
507 if (!match->wc.masks.dl_type) {
508 wc |= OFPFW11_DL_TYPE;
510 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
513 if (!(match->wc.masks.nw_tos & IP_DSCP_MASK)) {
514 wc |= OFPFW11_NW_TOS;
516 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
519 if (!match->wc.masks.nw_proto) {
520 wc |= OFPFW11_NW_PROTO;
522 ofmatch->nw_proto = match->flow.nw_proto;
525 ofmatch->nw_src = match->flow.nw_src;
526 ofmatch->nw_src_mask = ~match->wc.masks.nw_src;
527 ofmatch->nw_dst = match->flow.nw_dst;
528 ofmatch->nw_dst_mask = ~match->wc.masks.nw_dst;
530 if (!match->wc.masks.tp_src) {
531 wc |= OFPFW11_TP_SRC;
533 ofmatch->tp_src = match->flow.tp_src;
536 if (!match->wc.masks.tp_dst) {
537 wc |= OFPFW11_TP_DST;
539 ofmatch->tp_dst = match->flow.tp_dst;
542 /* MPLS not supported. */
543 wc |= OFPFW11_MPLS_LABEL;
544 wc |= OFPFW11_MPLS_TC;
546 ofmatch->metadata = match->flow.metadata;
547 ofmatch->metadata_mask = ~match->wc.masks.metadata;
549 ofmatch->wildcards = htonl(wc);
552 /* Given a 'dl_type' value in the format used in struct flow, returns the
553 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
556 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
558 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
559 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
563 /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
564 * structure, returns the corresponding 'dl_type' value for use in struct
567 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
569 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
570 ? htons(FLOW_DL_TYPE_NONE)
576 struct proto_abbrev {
577 enum ofputil_protocol protocol;
581 /* Most users really don't care about some of the differences between
582 * protocols. These abbreviations help with that. */
583 static const struct proto_abbrev proto_abbrevs[] = {
584 { OFPUTIL_P_ANY, "any" },
585 { OFPUTIL_P_OF10_STD_ANY, "OpenFlow10" },
586 { OFPUTIL_P_OF10_NXM_ANY, "NXM" },
587 { OFPUTIL_P_ANY_OXM, "OXM" },
589 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
591 enum ofputil_protocol ofputil_flow_dump_protocols[] = {
597 size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
599 /* Returns the set of ofputil_protocols that are supported with the given
600 * OpenFlow 'version'. 'version' should normally be an 8-bit OpenFlow version
601 * identifier (e.g. 0x01 for OpenFlow 1.0, 0x02 for OpenFlow 1.1). Returns 0
602 * if 'version' is not supported or outside the valid range. */
603 enum ofputil_protocol
604 ofputil_protocols_from_ofp_version(enum ofp_version version)
608 return OFPUTIL_P_OF10_STD_ANY | OFPUTIL_P_OF10_NXM_ANY;
610 return OFPUTIL_P_OF12_OXM;
612 return OFPUTIL_P_OF13_OXM;
619 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
620 * connection that has negotiated the given 'version'. 'version' should
621 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
622 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
623 * outside the valid range. */
624 enum ofputil_protocol
625 ofputil_protocol_from_ofp_version(enum ofp_version version)
627 return rightmost_1bit(ofputil_protocols_from_ofp_version(version));
630 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
631 * etc.) that corresponds to 'protocol'. */
633 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
636 case OFPUTIL_P_OF10_STD:
637 case OFPUTIL_P_OF10_STD_TID:
638 case OFPUTIL_P_OF10_NXM:
639 case OFPUTIL_P_OF10_NXM_TID:
640 return OFP10_VERSION;
641 case OFPUTIL_P_OF12_OXM:
642 return OFP12_VERSION;
643 case OFPUTIL_P_OF13_OXM:
644 return OFP13_VERSION;
650 /* Returns a bitmap of OpenFlow versions that are supported by at
651 * least one of the 'protocols'. */
653 ofputil_protocols_to_version_bitmap(enum ofputil_protocol protocols)
657 for (; protocols; protocols = zero_rightmost_1bit(protocols)) {
658 enum ofputil_protocol protocol = rightmost_1bit(protocols);
660 bitmap |= 1u << ofputil_protocol_to_ofp_version(protocol);
666 /* Returns the set of protocols that are supported on top of the
667 * OpenFlow versions included in 'bitmap'. */
668 enum ofputil_protocol
669 ofputil_protocols_from_version_bitmap(uint32_t bitmap)
671 enum ofputil_protocol protocols = 0;
673 for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
674 enum ofp_version version = rightmost_1bit_idx(bitmap);
676 protocols |= ofputil_protocols_from_ofp_version(version);
682 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
685 ofputil_protocol_is_valid(enum ofputil_protocol protocol)
687 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
690 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
691 * extension turned on or off if 'enable' is true or false, respectively.
693 * This extension is only useful for protocols whose "standard" version does
694 * not allow specific tables to be modified. In particular, this is true of
695 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
696 * specifies a table ID and so there is no need for such an extension. When
697 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
698 * extension, this function just returns its 'protocol' argument unchanged
699 * regardless of the value of 'enable'. */
700 enum ofputil_protocol
701 ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
704 case OFPUTIL_P_OF10_STD:
705 case OFPUTIL_P_OF10_STD_TID:
706 return enable ? OFPUTIL_P_OF10_STD_TID : OFPUTIL_P_OF10_STD;
708 case OFPUTIL_P_OF10_NXM:
709 case OFPUTIL_P_OF10_NXM_TID:
710 return enable ? OFPUTIL_P_OF10_NXM_TID : OFPUTIL_P_OF10_NXM;
712 case OFPUTIL_P_OF12_OXM:
713 return OFPUTIL_P_OF12_OXM;
715 case OFPUTIL_P_OF13_OXM:
716 return OFPUTIL_P_OF13_OXM;
723 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
724 * some extension to a standard protocol version, the return value is the
725 * standard version of that protocol without any extension. If 'protocol' is a
726 * standard protocol version, returns 'protocol' unchanged. */
727 enum ofputil_protocol
728 ofputil_protocol_to_base(enum ofputil_protocol protocol)
730 return ofputil_protocol_set_tid(protocol, false);
733 /* Returns 'new_base' with any extensions taken from 'cur'. */
734 enum ofputil_protocol
735 ofputil_protocol_set_base(enum ofputil_protocol cur,
736 enum ofputil_protocol new_base)
738 bool tid = (cur & OFPUTIL_P_TID) != 0;
741 case OFPUTIL_P_OF10_STD:
742 case OFPUTIL_P_OF10_STD_TID:
743 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD, tid);
745 case OFPUTIL_P_OF10_NXM:
746 case OFPUTIL_P_OF10_NXM_TID:
747 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM, tid);
749 case OFPUTIL_P_OF12_OXM:
750 return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM, tid);
752 case OFPUTIL_P_OF13_OXM:
753 return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM, tid);
760 /* Returns a string form of 'protocol', if a simple form exists (that is, if
761 * 'protocol' is either a single protocol or it is a combination of protocols
762 * that have a single abbreviation). Otherwise, returns NULL. */
764 ofputil_protocol_to_string(enum ofputil_protocol protocol)
766 const struct proto_abbrev *p;
768 /* Use a "switch" statement for single-bit names so that we get a compiler
769 * warning if we forget any. */
771 case OFPUTIL_P_OF10_NXM:
772 return "NXM-table_id";
774 case OFPUTIL_P_OF10_NXM_TID:
775 return "NXM+table_id";
777 case OFPUTIL_P_OF10_STD:
778 return "OpenFlow10-table_id";
780 case OFPUTIL_P_OF10_STD_TID:
781 return "OpenFlow10+table_id";
783 case OFPUTIL_P_OF12_OXM:
784 return "OXM-OpenFlow12";
786 case OFPUTIL_P_OF13_OXM:
787 return "OXM-OpenFlow13";
790 /* Check abbreviations. */
791 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
792 if (protocol == p->protocol) {
800 /* Returns a string that represents 'protocols'. The return value might be a
801 * comma-separated list if 'protocols' doesn't have a simple name. The return
802 * value is "none" if 'protocols' is 0.
804 * The caller must free the returned string (with free()). */
806 ofputil_protocols_to_string(enum ofputil_protocol protocols)
810 assert(!(protocols & ~OFPUTIL_P_ANY));
811 if (protocols == 0) {
812 return xstrdup("none");
817 const struct proto_abbrev *p;
821 ds_put_char(&s, ',');
824 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
825 if ((protocols & p->protocol) == p->protocol) {
826 ds_put_cstr(&s, p->name);
827 protocols &= ~p->protocol;
832 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
833 enum ofputil_protocol bit = 1u << i;
835 if (protocols & bit) {
836 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
845 return ds_steal_cstr(&s);
848 static enum ofputil_protocol
849 ofputil_protocol_from_string__(const char *s, size_t n)
851 const struct proto_abbrev *p;
854 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
855 enum ofputil_protocol bit = 1u << i;
856 const char *name = ofputil_protocol_to_string(bit);
858 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
863 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
864 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
872 /* Returns the nonempty set of protocols represented by 's', which can be a
873 * single protocol name or abbreviation or a comma-separated list of them.
875 * Aborts the program with an error message if 's' is invalid. */
876 enum ofputil_protocol
877 ofputil_protocols_from_string(const char *s)
879 const char *orig_s = s;
880 enum ofputil_protocol protocols;
884 enum ofputil_protocol p;
893 p = ofputil_protocol_from_string__(s, n);
895 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
903 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
909 ofputil_version_from_string(const char *s)
911 if (!strcasecmp(s, "OpenFlow10")) {
912 return OFP10_VERSION;
914 if (!strcasecmp(s, "OpenFlow11")) {
915 return OFP11_VERSION;
917 if (!strcasecmp(s, "OpenFlow12")) {
918 return OFP12_VERSION;
920 if (!strcasecmp(s, "OpenFlow13")) {
921 return OFP13_VERSION;
929 return isspace(c) || c == ',';
933 ofputil_versions_from_string(const char *s)
943 if (is_delimiter(s[i])) {
948 while (s[i + j] && !is_delimiter(s[i + j])) {
951 key = xmemdup0(s + i, j);
952 version = ofputil_version_from_string(key);
954 VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key);
957 bitmap |= 1u << version;
965 ofputil_versions_from_strings(char ** const s, size_t count)
970 int version = ofputil_version_from_string(s[count]);
972 VLOG_WARN("Unknown OpenFlow version: \"%s\"", s[count]);
974 bitmap |= 1u << version;
982 ofputil_version_to_string(enum ofp_version ofp_version)
984 switch (ofp_version) {
999 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
1001 switch (packet_in_format) {
1002 case NXPIF_OPENFLOW10:
1011 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
1013 switch (packet_in_format) {
1014 case NXPIF_OPENFLOW10:
1015 return "openflow10";
1024 ofputil_packet_in_format_from_string(const char *s)
1026 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
1027 : !strcmp(s, "nxm") ? NXPIF_NXM
1032 regs_fully_wildcarded(const struct flow_wildcards *wc)
1036 for (i = 0; i < FLOW_N_REGS; i++) {
1037 if (wc->masks.regs[i] != 0) {
1045 tun_parms_fully_wildcarded(const struct flow_wildcards *wc)
1047 return (!wc->masks.tunnel.ip_src &&
1048 !wc->masks.tunnel.ip_dst &&
1049 !wc->masks.tunnel.ip_ttl &&
1050 !wc->masks.tunnel.ip_tos &&
1051 !wc->masks.tunnel.flags);
1054 /* Returns a bit-mask of ofputil_protocols that can be used for sending 'match'
1055 * to a switch (e.g. to add or remove a flow). Only NXM can handle tunnel IDs,
1056 * registers, or fixing the Ethernet multicast bit. Otherwise, it's better to
1057 * use OpenFlow 1.0 protocol for backward compatibility. */
1058 enum ofputil_protocol
1059 ofputil_usable_protocols(const struct match *match)
1061 const struct flow_wildcards *wc = &match->wc;
1063 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 18);
1065 /* tunnel params other than tun_id can't be sent in a flow_mod */
1066 if (!tun_parms_fully_wildcarded(wc)) {
1067 return OFPUTIL_P_NONE;
1070 /* skb_mark and skb_priority can't be sent in a flow_mod */
1071 if (wc->masks.skb_mark || wc->masks.skb_priority) {
1072 return OFPUTIL_P_NONE;
1075 /* NXM, OXM, and OF1.1 support bitwise matching on ethernet addresses. */
1076 if (!eth_mask_is_exact(wc->masks.dl_src)
1077 && !eth_addr_is_zero(wc->masks.dl_src)) {
1078 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1079 | OFPUTIL_P_OF13_OXM;
1081 if (!eth_mask_is_exact(wc->masks.dl_dst)
1082 && !eth_addr_is_zero(wc->masks.dl_dst)) {
1083 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1084 | OFPUTIL_P_OF13_OXM;
1087 /* NXM, OXM, and OF1.1+ support matching metadata. */
1088 if (wc->masks.metadata != htonll(0)) {
1089 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1090 | OFPUTIL_P_OF13_OXM;
1093 /* NXM and OXM support matching ARP hardware addresses. */
1094 if (!eth_addr_is_zero(wc->masks.arp_sha) ||
1095 !eth_addr_is_zero(wc->masks.arp_tha)) {
1096 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1097 | OFPUTIL_P_OF13_OXM;
1100 /* NXM and OXM support matching IPv6 traffic. */
1101 if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
1102 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1103 | OFPUTIL_P_OF13_OXM;
1106 /* NXM and OXM support matching registers. */
1107 if (!regs_fully_wildcarded(wc)) {
1108 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1109 | OFPUTIL_P_OF13_OXM;
1112 /* NXM and OXM support matching tun_id. */
1113 if (wc->masks.tunnel.tun_id != htonll(0)) {
1114 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1115 | OFPUTIL_P_OF13_OXM;
1118 /* NXM and OXM support matching fragments. */
1119 if (wc->masks.nw_frag) {
1120 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1121 | OFPUTIL_P_OF13_OXM;
1124 /* NXM and OXM support matching IPv6 flow label. */
1125 if (wc->masks.ipv6_label) {
1126 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1127 | OFPUTIL_P_OF13_OXM;
1130 /* NXM and OXM support matching IP ECN bits. */
1131 if (wc->masks.nw_tos & IP_ECN_MASK) {
1132 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1133 | OFPUTIL_P_OF13_OXM;
1136 /* NXM and OXM support matching IP TTL/hop limit. */
1137 if (wc->masks.nw_ttl) {
1138 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1139 | OFPUTIL_P_OF13_OXM;
1142 /* NXM and OXM support non-CIDR IPv4 address masks. */
1143 if (!ip_is_cidr(wc->masks.nw_src) || !ip_is_cidr(wc->masks.nw_dst)) {
1144 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1145 | OFPUTIL_P_OF13_OXM;
1148 /* NXM and OXM support bitwise matching on transport port. */
1149 if ((wc->masks.tp_src && wc->masks.tp_src != htons(UINT16_MAX)) ||
1150 (wc->masks.tp_dst && wc->masks.tp_dst != htons(UINT16_MAX))) {
1151 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1152 | OFPUTIL_P_OF13_OXM;
1155 /* Other formats can express this rule. */
1156 return OFPUTIL_P_ANY;
1160 ofputil_format_version(struct ds *msg, enum ofp_version version)
1162 ds_put_format(msg, "0x%02x", version);
1166 ofputil_format_version_name(struct ds *msg, enum ofp_version version)
1168 ds_put_cstr(msg, ofputil_version_to_string(version));
1172 ofputil_format_version_bitmap__(struct ds *msg, uint32_t bitmap,
1173 void (*format_version)(struct ds *msg,
1177 format_version(msg, raw_ctz(bitmap));
1178 bitmap = zero_rightmost_1bit(bitmap);
1180 ds_put_cstr(msg, ", ");
1186 ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap)
1188 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version);
1192 ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap)
1194 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version_name);
1198 ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header *oheh,
1199 uint32_t *allowed_versionsp)
1201 uint16_t bitmap_len = ntohs(oheh->length) - sizeof *oheh;
1202 const ovs_be32 *bitmap = (const ovs_be32 *) (oheh + 1);
1203 uint32_t allowed_versions;
1205 if (!bitmap_len || bitmap_len % sizeof *bitmap) {
1209 /* Only use the first 32-bit element of the bitmap as that is all the
1210 * current implementation supports. Subsequent elements are ignored which
1211 * should have no effect on session negotiation until Open vSwtich supports
1212 * wire-protocol versions greater than 31.
1214 allowed_versions = ntohl(bitmap[0]);
1216 if (allowed_versions & 1) {
1217 /* There's no OpenFlow version 0. */
1218 VLOG_WARN_RL(&bad_ofmsg_rl, "peer claims to support invalid OpenFlow "
1220 allowed_versions &= ~1u;
1223 if (!allowed_versions) {
1224 VLOG_WARN_RL(&bad_ofmsg_rl, "peer does not support any OpenFlow "
1225 "version (between 0x01 and 0x1f)");
1229 *allowed_versionsp = allowed_versions;
1234 version_bitmap_from_version(uint8_t ofp_version)
1236 return ((ofp_version < 32 ? 1u << ofp_version : 0) - 1) << 1;
1239 /* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions'
1240 * the set of OpenFlow versions for which 'oh' announces support.
1242 * Because of how OpenFlow defines OFPT_HELLO messages, this function is always
1243 * successful, and thus '*allowed_versions' is always initialized. However, it
1244 * returns false if 'oh' contains some data that could not be fully understood,
1245 * true if 'oh' was completely parsed. */
1247 ofputil_decode_hello(const struct ofp_header *oh, uint32_t *allowed_versions)
1252 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
1253 ofpbuf_pull(&msg, sizeof *oh);
1255 *allowed_versions = version_bitmap_from_version(oh->version);
1257 const struct ofp_hello_elem_header *oheh;
1260 if (msg.size < sizeof *oheh) {
1265 len = ntohs(oheh->length);
1266 if (len < sizeof *oheh || !ofpbuf_try_pull(&msg, ROUND_UP(len, 8))) {
1270 if (oheh->type != htons(OFPHET_VERSIONBITMAP)
1271 || !ofputil_decode_hello_bitmap(oheh, allowed_versions)) {
1279 /* Returns true if 'allowed_versions' needs to be accompanied by a version
1280 * bitmap to be correctly expressed in an OFPT_HELLO message. */
1282 should_send_version_bitmap(uint32_t allowed_versions)
1284 return !is_pow2((allowed_versions >> 1) + 1);
1287 /* Create an OFPT_HELLO message that expresses support for the OpenFlow
1288 * versions in the 'allowed_versions' bitmaps and returns the message. */
1290 ofputil_encode_hello(uint32_t allowed_versions)
1292 enum ofp_version ofp_version;
1295 ofp_version = leftmost_1bit_idx(allowed_versions);
1296 msg = ofpraw_alloc(OFPRAW_OFPT_HELLO, ofp_version, 0);
1298 if (should_send_version_bitmap(allowed_versions)) {
1299 struct ofp_hello_elem_header *oheh;
1302 map_len = sizeof allowed_versions;
1303 oheh = ofpbuf_put_zeros(msg, ROUND_UP(map_len + sizeof *oheh, 8));
1304 oheh->type = htons(OFPHET_VERSIONBITMAP);
1305 oheh->length = htons(map_len + sizeof *oheh);
1306 *(ovs_be32 *)(oheh + 1) = htonl(allowed_versions);
1308 ofpmsg_update_length(msg);
1314 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1315 * protocol is 'current', at least partly transitions the protocol to 'want'.
1316 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1317 * connection if the switch processes the returned message correctly. (If
1318 * '*next != want' then the caller will have to iterate.)
1320 * If 'current == want', or if it is not possible to transition from 'current'
1321 * to 'want' (because, for example, 'current' and 'want' use different OpenFlow
1322 * protocol versions), returns NULL and stores 'current' in '*next'. */
1324 ofputil_encode_set_protocol(enum ofputil_protocol current,
1325 enum ofputil_protocol want,
1326 enum ofputil_protocol *next)
1328 enum ofp_version cur_version, want_version;
1329 enum ofputil_protocol cur_base, want_base;
1330 bool cur_tid, want_tid;
1332 cur_version = ofputil_protocol_to_ofp_version(current);
1333 want_version = ofputil_protocol_to_ofp_version(want);
1334 if (cur_version != want_version) {
1339 cur_base = ofputil_protocol_to_base(current);
1340 want_base = ofputil_protocol_to_base(want);
1341 if (cur_base != want_base) {
1342 *next = ofputil_protocol_set_base(current, want_base);
1344 switch (want_base) {
1345 case OFPUTIL_P_OF10_NXM:
1346 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1348 case OFPUTIL_P_OF10_STD:
1349 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1351 case OFPUTIL_P_OF12_OXM:
1352 case OFPUTIL_P_OF13_OXM:
1353 /* There are only one of each OpenFlow 1.2+ protocols and we already
1354 * verified above that we're not trying to change versions. */
1357 case OFPUTIL_P_OF10_STD_TID:
1358 case OFPUTIL_P_OF10_NXM_TID:
1363 cur_tid = (current & OFPUTIL_P_TID) != 0;
1364 want_tid = (want & OFPUTIL_P_TID) != 0;
1365 if (cur_tid != want_tid) {
1366 *next = ofputil_protocol_set_tid(current, want_tid);
1367 return ofputil_make_flow_mod_table_id(want_tid);
1370 assert(current == want);
1376 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1377 * format to 'nxff'. */
1379 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1381 struct nx_set_flow_format *sff;
1384 assert(ofputil_nx_flow_format_is_valid(nxff));
1386 msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1387 sff = ofpbuf_put_zeros(msg, sizeof *sff);
1388 sff->format = htonl(nxff);
1393 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1395 enum ofputil_protocol
1396 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1398 switch (flow_format) {
1399 case NXFF_OPENFLOW10:
1400 return OFPUTIL_P_OF10_STD;
1403 return OFPUTIL_P_OF10_NXM;
1410 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1412 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1414 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1417 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1420 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1422 switch (flow_format) {
1423 case NXFF_OPENFLOW10:
1424 return "openflow10";
1433 ofputil_make_set_packet_in_format(enum ofp_version ofp_version,
1434 enum nx_packet_in_format packet_in_format)
1436 struct nx_set_packet_in_format *spif;
1439 msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, ofp_version, 0);
1440 spif = ofpbuf_put_zeros(msg, sizeof *spif);
1441 spif->format = htonl(packet_in_format);
1446 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1447 * extension on or off (according to 'flow_mod_table_id'). */
1449 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1451 struct nx_flow_mod_table_id *nfmti;
1454 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1455 nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
1456 nfmti->set = flow_mod_table_id;
1460 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1461 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1464 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1465 * The caller must initialize 'ofpacts' and retains ownership of it.
1466 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1468 * Does not validate the flow_mod actions. The caller should do that, with
1469 * ofpacts_check(). */
1471 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1472 const struct ofp_header *oh,
1473 enum ofputil_protocol protocol,
1474 struct ofpbuf *ofpacts)
1480 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1481 raw = ofpraw_pull_assert(&b);
1482 if (raw == OFPRAW_OFPT11_FLOW_MOD) {
1483 /* Standard OpenFlow 1.1 flow_mod. */
1484 const struct ofp11_flow_mod *ofm;
1487 ofm = ofpbuf_pull(&b, sizeof *ofm);
1489 error = ofputil_pull_ofp11_match(&b, &fm->match, NULL);
1494 error = ofpacts_pull_openflow11_instructions(&b, b.size, ofpacts);
1499 /* Translate the message. */
1500 fm->priority = ntohs(ofm->priority);
1501 if (ofm->command == OFPFC_ADD) {
1502 fm->cookie = htonll(0);
1503 fm->cookie_mask = htonll(0);
1504 fm->new_cookie = ofm->cookie;
1506 fm->cookie = ofm->cookie;
1507 fm->cookie_mask = ofm->cookie_mask;
1508 fm->new_cookie = htonll(UINT64_MAX);
1510 fm->command = ofm->command;
1511 fm->table_id = ofm->table_id;
1512 fm->idle_timeout = ntohs(ofm->idle_timeout);
1513 fm->hard_timeout = ntohs(ofm->hard_timeout);
1514 fm->buffer_id = ntohl(ofm->buffer_id);
1515 error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
1519 if ((ofm->command == OFPFC_DELETE
1520 || ofm->command == OFPFC_DELETE_STRICT)
1521 && ofm->out_group != htonl(OFPG_ANY)) {
1522 return OFPERR_OFPFMFC_UNKNOWN;
1524 fm->flags = ntohs(ofm->flags);
1526 if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1527 /* Standard OpenFlow 1.0 flow_mod. */
1528 const struct ofp10_flow_mod *ofm;
1531 /* Get the ofp10_flow_mod. */
1532 ofm = ofpbuf_pull(&b, sizeof *ofm);
1534 /* Translate the rule. */
1535 ofputil_match_from_ofp10_match(&ofm->match, &fm->match);
1536 ofputil_normalize_match(&fm->match);
1538 /* Now get the actions. */
1539 error = ofpacts_pull_openflow10(&b, b.size, ofpacts);
1544 /* OpenFlow 1.0 says that exact-match rules have to have the
1545 * highest possible priority. */
1546 fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL)
1547 ? ntohs(ofm->priority)
1550 /* Translate the message. */
1551 command = ntohs(ofm->command);
1552 fm->cookie = htonll(0);
1553 fm->cookie_mask = htonll(0);
1554 fm->new_cookie = ofm->cookie;
1555 fm->idle_timeout = ntohs(ofm->idle_timeout);
1556 fm->hard_timeout = ntohs(ofm->hard_timeout);
1557 fm->buffer_id = ntohl(ofm->buffer_id);
1558 fm->out_port = ntohs(ofm->out_port);
1559 fm->flags = ntohs(ofm->flags);
1560 } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1561 /* Nicira extended flow_mod. */
1562 const struct nx_flow_mod *nfm;
1565 /* Dissect the message. */
1566 nfm = ofpbuf_pull(&b, sizeof *nfm);
1567 error = nx_pull_match(&b, ntohs(nfm->match_len),
1568 &fm->match, &fm->cookie, &fm->cookie_mask);
1572 error = ofpacts_pull_openflow10(&b, b.size, ofpacts);
1577 /* Translate the message. */
1578 command = ntohs(nfm->command);
1579 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1580 /* Flow additions may only set a new cookie, not match an
1581 * existing cookie. */
1582 return OFPERR_NXBRC_NXM_INVALID;
1584 fm->priority = ntohs(nfm->priority);
1585 fm->new_cookie = nfm->cookie;
1586 fm->idle_timeout = ntohs(nfm->idle_timeout);
1587 fm->hard_timeout = ntohs(nfm->hard_timeout);
1588 fm->buffer_id = ntohl(nfm->buffer_id);
1589 fm->out_port = ntohs(nfm->out_port);
1590 fm->flags = ntohs(nfm->flags);
1595 if (fm->flags & OFPFF10_EMERG) {
1596 /* We do not support the OpenFlow 1.0 emergency flow cache, which
1597 * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
1599 * OpenFlow 1.0 specifies the error code to use when idle_timeout
1600 * or hard_timeout is nonzero. Otherwise, there is no good error
1601 * code, so just state that the flow table is full. */
1602 return (fm->hard_timeout || fm->idle_timeout
1603 ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT
1604 : OFPERR_OFPFMFC_TABLE_FULL);
1607 if (protocol & OFPUTIL_P_TID) {
1608 fm->command = command & 0xff;
1609 fm->table_id = command >> 8;
1611 fm->command = command;
1612 fm->table_id = 0xff;
1616 fm->ofpacts = ofpacts->data;
1617 fm->ofpacts_len = ofpacts->size;
1623 ofputil_tid_command(const struct ofputil_flow_mod *fm,
1624 enum ofputil_protocol protocol)
1626 return htons(protocol & OFPUTIL_P_TID
1627 ? (fm->command & 0xff) | (fm->table_id << 8)
1631 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
1632 * 'protocol' and returns the message. */
1634 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
1635 enum ofputil_protocol protocol)
1640 case OFPUTIL_P_OF12_OXM:
1641 case OFPUTIL_P_OF13_OXM: {
1642 struct ofp11_flow_mod *ofm;
1644 msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD,
1645 ofputil_protocol_to_ofp_version(protocol),
1646 NXM_TYPICAL_LEN + fm->ofpacts_len);
1647 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
1648 if (fm->command == OFPFC_ADD) {
1649 ofm->cookie = fm->new_cookie;
1651 ofm->cookie = fm->cookie;
1653 ofm->cookie_mask = fm->cookie_mask;
1654 ofm->table_id = fm->table_id;
1655 ofm->command = fm->command;
1656 ofm->idle_timeout = htons(fm->idle_timeout);
1657 ofm->hard_timeout = htons(fm->hard_timeout);
1658 ofm->priority = htons(fm->priority);
1659 ofm->buffer_id = htonl(fm->buffer_id);
1660 ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
1661 ofm->out_group = htonl(OFPG11_ANY);
1662 ofm->flags = htons(fm->flags);
1663 oxm_put_match(msg, &fm->match);
1664 ofpacts_put_openflow11_instructions(fm->ofpacts, fm->ofpacts_len, msg);
1668 case OFPUTIL_P_OF10_STD:
1669 case OFPUTIL_P_OF10_STD_TID: {
1670 struct ofp10_flow_mod *ofm;
1672 msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
1674 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
1675 ofputil_match_to_ofp10_match(&fm->match, &ofm->match);
1676 ofm->cookie = fm->new_cookie;
1677 ofm->command = ofputil_tid_command(fm, protocol);
1678 ofm->idle_timeout = htons(fm->idle_timeout);
1679 ofm->hard_timeout = htons(fm->hard_timeout);
1680 ofm->priority = htons(fm->priority);
1681 ofm->buffer_id = htonl(fm->buffer_id);
1682 ofm->out_port = htons(fm->out_port);
1683 ofm->flags = htons(fm->flags);
1684 ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
1688 case OFPUTIL_P_OF10_NXM:
1689 case OFPUTIL_P_OF10_NXM_TID: {
1690 struct nx_flow_mod *nfm;
1693 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
1694 NXM_TYPICAL_LEN + fm->ofpacts_len);
1695 nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
1696 nfm->command = ofputil_tid_command(fm, protocol);
1697 nfm->cookie = fm->new_cookie;
1698 match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask);
1700 nfm->idle_timeout = htons(fm->idle_timeout);
1701 nfm->hard_timeout = htons(fm->hard_timeout);
1702 nfm->priority = htons(fm->priority);
1703 nfm->buffer_id = htonl(fm->buffer_id);
1704 nfm->out_port = htons(fm->out_port);
1705 nfm->flags = htons(fm->flags);
1706 nfm->match_len = htons(match_len);
1707 ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
1715 ofpmsg_update_length(msg);
1719 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1720 * send all of the 'n_fm's flow table modification requests in 'fms', and a
1721 * 0-bit for each protocol that is inadequate.
1723 * (The return value will have at least one 1-bit.) */
1724 enum ofputil_protocol
1725 ofputil_flow_mod_usable_protocols(const struct ofputil_flow_mod *fms,
1728 enum ofputil_protocol usable_protocols;
1731 usable_protocols = OFPUTIL_P_ANY;
1732 for (i = 0; i < n_fms; i++) {
1733 const struct ofputil_flow_mod *fm = &fms[i];
1735 usable_protocols &= ofputil_usable_protocols(&fm->match);
1736 if (fm->table_id != 0xff) {
1737 usable_protocols &= OFPUTIL_P_TID;
1740 /* Matching of the cookie is only supported through NXM or OF1.1+. */
1741 if (fm->cookie_mask != htonll(0)) {
1742 usable_protocols &= OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1743 | OFPUTIL_P_OF13_OXM;
1747 return usable_protocols;
1751 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr,
1752 const struct ofp10_flow_stats_request *ofsr,
1755 fsr->aggregate = aggregate;
1756 ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match);
1757 fsr->out_port = ntohs(ofsr->out_port);
1758 fsr->table_id = ofsr->table_id;
1759 fsr->cookie = fsr->cookie_mask = htonll(0);
1765 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr,
1766 struct ofpbuf *b, bool aggregate)
1768 const struct ofp11_flow_stats_request *ofsr;
1771 ofsr = ofpbuf_pull(b, sizeof *ofsr);
1772 fsr->aggregate = aggregate;
1773 fsr->table_id = ofsr->table_id;
1774 error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port);
1778 if (ofsr->out_group != htonl(OFPG11_ANY)) {
1779 return OFPERR_OFPFMFC_UNKNOWN;
1781 fsr->cookie = ofsr->cookie;
1782 fsr->cookie_mask = ofsr->cookie_mask;
1783 error = ofputil_pull_ofp11_match(b, &fsr->match, NULL);
1792 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
1793 struct ofpbuf *b, bool aggregate)
1795 const struct nx_flow_stats_request *nfsr;
1798 nfsr = ofpbuf_pull(b, sizeof *nfsr);
1799 error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match,
1800 &fsr->cookie, &fsr->cookie_mask);
1805 return OFPERR_OFPBRC_BAD_LEN;
1808 fsr->aggregate = aggregate;
1809 fsr->out_port = ntohs(nfsr->out_port);
1810 fsr->table_id = nfsr->table_id;
1815 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1816 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1817 * successful, otherwise an OpenFlow error code. */
1819 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
1820 const struct ofp_header *oh)
1825 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1826 raw = ofpraw_pull_assert(&b);
1827 switch ((int) raw) {
1828 case OFPRAW_OFPST10_FLOW_REQUEST:
1829 return ofputil_decode_ofpst10_flow_request(fsr, b.data, false);
1831 case OFPRAW_OFPST10_AGGREGATE_REQUEST:
1832 return ofputil_decode_ofpst10_flow_request(fsr, b.data, true);
1834 case OFPRAW_OFPST11_FLOW_REQUEST:
1835 return ofputil_decode_ofpst11_flow_request(fsr, &b, false);
1837 case OFPRAW_OFPST11_AGGREGATE_REQUEST:
1838 return ofputil_decode_ofpst11_flow_request(fsr, &b, true);
1840 case OFPRAW_NXST_FLOW_REQUEST:
1841 return ofputil_decode_nxst_flow_request(fsr, &b, false);
1843 case OFPRAW_NXST_AGGREGATE_REQUEST:
1844 return ofputil_decode_nxst_flow_request(fsr, &b, true);
1847 /* Hey, the caller lied. */
1852 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1853 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1854 * 'protocol', and returns the message. */
1856 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
1857 enum ofputil_protocol protocol)
1863 case OFPUTIL_P_OF12_OXM:
1864 case OFPUTIL_P_OF13_OXM: {
1865 struct ofp11_flow_stats_request *ofsr;
1867 raw = (fsr->aggregate
1868 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
1869 : OFPRAW_OFPST11_FLOW_REQUEST);
1870 msg = ofpraw_alloc(raw, ofputil_protocol_to_ofp_version(protocol),
1872 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
1873 ofsr->table_id = fsr->table_id;
1874 ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port);
1875 ofsr->out_group = htonl(OFPG11_ANY);
1876 ofsr->cookie = fsr->cookie;
1877 ofsr->cookie_mask = fsr->cookie_mask;
1878 oxm_put_match(msg, &fsr->match);
1882 case OFPUTIL_P_OF10_STD:
1883 case OFPUTIL_P_OF10_STD_TID: {
1884 struct ofp10_flow_stats_request *ofsr;
1886 raw = (fsr->aggregate
1887 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
1888 : OFPRAW_OFPST10_FLOW_REQUEST);
1889 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
1890 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
1891 ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match);
1892 ofsr->table_id = fsr->table_id;
1893 ofsr->out_port = htons(fsr->out_port);
1897 case OFPUTIL_P_OF10_NXM:
1898 case OFPUTIL_P_OF10_NXM_TID: {
1899 struct nx_flow_stats_request *nfsr;
1902 raw = (fsr->aggregate
1903 ? OFPRAW_NXST_AGGREGATE_REQUEST
1904 : OFPRAW_NXST_FLOW_REQUEST);
1905 msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN);
1906 ofpbuf_put_zeros(msg, sizeof *nfsr);
1907 match_len = nx_put_match(msg, &fsr->match,
1908 fsr->cookie, fsr->cookie_mask);
1911 nfsr->out_port = htons(fsr->out_port);
1912 nfsr->match_len = htons(match_len);
1913 nfsr->table_id = fsr->table_id;
1924 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1925 * accurately encode 'fsr', and a 0-bit for each protocol that is inadequate.
1927 * (The return value will have at least one 1-bit.) */
1928 enum ofputil_protocol
1929 ofputil_flow_stats_request_usable_protocols(
1930 const struct ofputil_flow_stats_request *fsr)
1932 enum ofputil_protocol usable_protocols;
1934 usable_protocols = ofputil_usable_protocols(&fsr->match);
1935 if (fsr->cookie_mask != htonll(0)) {
1936 usable_protocols &= OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1937 | OFPUTIL_P_OF13_OXM;
1939 return usable_protocols;
1942 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1943 * ofputil_flow_stats in 'fs'.
1945 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1946 * OpenFlow message. Calling this function multiple times for a single 'msg'
1947 * iterates through the replies. The caller must initially leave 'msg''s layer
1948 * pointers null and not modify them between calls.
1950 * Most switches don't send the values needed to populate fs->idle_age and
1951 * fs->hard_age, so those members will usually be set to 0. If the switch from
1952 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
1953 * 'flow_age_extension' as true so that the contents of 'msg' determine the
1954 * 'idle_age' and 'hard_age' members in 'fs'.
1956 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
1957 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
1958 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
1960 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1961 * otherwise a positive errno value. */
1963 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1965 bool flow_age_extension,
1966 struct ofpbuf *ofpacts)
1972 ? ofpraw_decode(&raw, msg->l2)
1973 : ofpraw_pull(&raw, msg));
1980 } else if (raw == OFPRAW_OFPST11_FLOW_REPLY
1981 || raw == OFPRAW_OFPST13_FLOW_REPLY) {
1982 const struct ofp11_flow_stats *ofs;
1984 uint16_t padded_match_len;
1986 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1988 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1989 "bytes at end", msg->size);
1993 length = ntohs(ofs->length);
1994 if (length < sizeof *ofs) {
1995 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1996 "length %zu", length);
2000 if (ofputil_pull_ofp11_match(msg, &fs->match, &padded_match_len)) {
2001 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match");
2005 if (ofpacts_pull_openflow11_instructions(msg, length - sizeof *ofs -
2006 padded_match_len, ofpacts)) {
2007 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions");
2011 fs->priority = ntohs(ofs->priority);
2012 fs->table_id = ofs->table_id;
2013 fs->duration_sec = ntohl(ofs->duration_sec);
2014 fs->duration_nsec = ntohl(ofs->duration_nsec);
2015 fs->idle_timeout = ntohs(ofs->idle_timeout);
2016 fs->hard_timeout = ntohs(ofs->hard_timeout);
2017 fs->flags = (raw == OFPRAW_OFPST13_FLOW_REPLY) ? ntohs(ofs->flags) : 0;
2020 fs->cookie = ofs->cookie;
2021 fs->packet_count = ntohll(ofs->packet_count);
2022 fs->byte_count = ntohll(ofs->byte_count);
2023 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
2024 const struct ofp10_flow_stats *ofs;
2027 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2029 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
2030 "bytes at end", msg->size);
2034 length = ntohs(ofs->length);
2035 if (length < sizeof *ofs) {
2036 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2037 "length %zu", length);
2041 if (ofpacts_pull_openflow10(msg, length - sizeof *ofs, ofpacts)) {
2045 fs->cookie = get_32aligned_be64(&ofs->cookie);
2046 ofputil_match_from_ofp10_match(&ofs->match, &fs->match);
2047 fs->priority = ntohs(ofs->priority);
2048 fs->table_id = ofs->table_id;
2049 fs->duration_sec = ntohl(ofs->duration_sec);
2050 fs->duration_nsec = ntohl(ofs->duration_nsec);
2051 fs->idle_timeout = ntohs(ofs->idle_timeout);
2052 fs->hard_timeout = ntohs(ofs->hard_timeout);
2055 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
2056 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
2058 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
2059 const struct nx_flow_stats *nfs;
2060 size_t match_len, actions_len, length;
2062 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
2064 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
2065 "bytes at end", msg->size);
2069 length = ntohs(nfs->length);
2070 match_len = ntohs(nfs->match_len);
2071 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
2072 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
2073 "claims invalid length %zu", match_len, length);
2076 if (nx_pull_match(msg, match_len, &fs->match, NULL, NULL)) {
2080 actions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
2081 if (ofpacts_pull_openflow10(msg, actions_len, ofpacts)) {
2085 fs->cookie = nfs->cookie;
2086 fs->table_id = nfs->table_id;
2087 fs->duration_sec = ntohl(nfs->duration_sec);
2088 fs->duration_nsec = ntohl(nfs->duration_nsec);
2089 fs->priority = ntohs(nfs->priority);
2090 fs->idle_timeout = ntohs(nfs->idle_timeout);
2091 fs->hard_timeout = ntohs(nfs->hard_timeout);
2094 if (flow_age_extension) {
2095 if (nfs->idle_age) {
2096 fs->idle_age = ntohs(nfs->idle_age) - 1;
2098 if (nfs->hard_age) {
2099 fs->hard_age = ntohs(nfs->hard_age) - 1;
2102 fs->packet_count = ntohll(nfs->packet_count);
2103 fs->byte_count = ntohll(nfs->byte_count);
2109 fs->ofpacts = ofpacts->data;
2110 fs->ofpacts_len = ofpacts->size;
2115 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
2117 * We use this in situations where OVS internally uses UINT64_MAX to mean
2118 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
2120 unknown_to_zero(uint64_t count)
2122 return count != UINT64_MAX ? count : 0;
2125 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
2126 * those already present in the list of ofpbufs in 'replies'. 'replies' should
2127 * have been initialized with ofputil_start_stats_reply(). */
2129 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
2130 struct list *replies)
2132 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
2133 size_t start_ofs = reply->size;
2136 ofpraw_decode_partial(&raw, reply->data, reply->size);
2137 if (raw == OFPRAW_OFPST11_FLOW_REPLY || raw == OFPRAW_OFPST13_FLOW_REPLY) {
2138 struct ofp11_flow_stats *ofs;
2140 ofpbuf_put_uninit(reply, sizeof *ofs);
2141 oxm_put_match(reply, &fs->match);
2142 ofpacts_put_openflow11_instructions(fs->ofpacts, fs->ofpacts_len,
2145 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
2146 ofs->length = htons(reply->size - start_ofs);
2147 ofs->table_id = fs->table_id;
2149 ofs->duration_sec = htonl(fs->duration_sec);
2150 ofs->duration_nsec = htonl(fs->duration_nsec);
2151 ofs->priority = htons(fs->priority);
2152 ofs->idle_timeout = htons(fs->idle_timeout);
2153 ofs->hard_timeout = htons(fs->hard_timeout);
2154 ofs->flags = (raw == OFPRAW_OFPST13_FLOW_REPLY) ? htons(fs->flags) : 0;
2155 memset(ofs->pad2, 0, sizeof ofs->pad2);
2156 ofs->cookie = fs->cookie;
2157 ofs->packet_count = htonll(unknown_to_zero(fs->packet_count));
2158 ofs->byte_count = htonll(unknown_to_zero(fs->byte_count));
2159 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
2160 struct ofp10_flow_stats *ofs;
2162 ofpbuf_put_uninit(reply, sizeof *ofs);
2163 ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply);
2165 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
2166 ofs->length = htons(reply->size - start_ofs);
2167 ofs->table_id = fs->table_id;
2169 ofputil_match_to_ofp10_match(&fs->match, &ofs->match);
2170 ofs->duration_sec = htonl(fs->duration_sec);
2171 ofs->duration_nsec = htonl(fs->duration_nsec);
2172 ofs->priority = htons(fs->priority);
2173 ofs->idle_timeout = htons(fs->idle_timeout);
2174 ofs->hard_timeout = htons(fs->hard_timeout);
2175 memset(ofs->pad2, 0, sizeof ofs->pad2);
2176 put_32aligned_be64(&ofs->cookie, fs->cookie);
2177 put_32aligned_be64(&ofs->packet_count,
2178 htonll(unknown_to_zero(fs->packet_count)));
2179 put_32aligned_be64(&ofs->byte_count,
2180 htonll(unknown_to_zero(fs->byte_count)));
2181 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
2182 struct nx_flow_stats *nfs;
2185 ofpbuf_put_uninit(reply, sizeof *nfs);
2186 match_len = nx_put_match(reply, &fs->match, 0, 0);
2187 ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply);
2189 nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
2190 nfs->length = htons(reply->size - start_ofs);
2191 nfs->table_id = fs->table_id;
2193 nfs->duration_sec = htonl(fs->duration_sec);
2194 nfs->duration_nsec = htonl(fs->duration_nsec);
2195 nfs->priority = htons(fs->priority);
2196 nfs->idle_timeout = htons(fs->idle_timeout);
2197 nfs->hard_timeout = htons(fs->hard_timeout);
2198 nfs->idle_age = htons(fs->idle_age < 0 ? 0
2199 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
2201 nfs->hard_age = htons(fs->hard_age < 0 ? 0
2202 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
2204 nfs->match_len = htons(match_len);
2205 nfs->cookie = fs->cookie;
2206 nfs->packet_count = htonll(fs->packet_count);
2207 nfs->byte_count = htonll(fs->byte_count);
2212 ofpmp_postappend(replies, start_ofs);
2215 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
2216 * NXST_AGGREGATE reply matching 'request', and returns the message. */
2218 ofputil_encode_aggregate_stats_reply(
2219 const struct ofputil_aggregate_stats *stats,
2220 const struct ofp_header *request)
2222 struct ofp_aggregate_stats_reply *asr;
2223 uint64_t packet_count;
2224 uint64_t byte_count;
2228 ofpraw_decode(&raw, request);
2229 if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) {
2230 packet_count = unknown_to_zero(stats->packet_count);
2231 byte_count = unknown_to_zero(stats->byte_count);
2233 packet_count = stats->packet_count;
2234 byte_count = stats->byte_count;
2237 msg = ofpraw_alloc_stats_reply(request, 0);
2238 asr = ofpbuf_put_zeros(msg, sizeof *asr);
2239 put_32aligned_be64(&asr->packet_count, htonll(packet_count));
2240 put_32aligned_be64(&asr->byte_count, htonll(byte_count));
2241 asr->flow_count = htonl(stats->flow_count);
2247 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
2248 const struct ofp_header *reply)
2250 struct ofp_aggregate_stats_reply *asr;
2253 ofpbuf_use_const(&msg, reply, ntohs(reply->length));
2254 ofpraw_pull_assert(&msg);
2257 stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
2258 stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
2259 stats->flow_count = ntohl(asr->flow_count);
2264 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
2265 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
2266 * an OpenFlow error code. */
2268 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
2269 const struct ofp_header *oh)
2274 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2275 raw = ofpraw_pull_assert(&b);
2276 if (raw == OFPRAW_OFPT11_FLOW_REMOVED) {
2277 const struct ofp12_flow_removed *ofr;
2280 ofr = ofpbuf_pull(&b, sizeof *ofr);
2282 error = ofputil_pull_ofp11_match(&b, &fr->match, NULL);
2287 fr->priority = ntohs(ofr->priority);
2288 fr->cookie = ofr->cookie;
2289 fr->reason = ofr->reason;
2290 fr->table_id = ofr->table_id;
2291 fr->duration_sec = ntohl(ofr->duration_sec);
2292 fr->duration_nsec = ntohl(ofr->duration_nsec);
2293 fr->idle_timeout = ntohs(ofr->idle_timeout);
2294 fr->hard_timeout = ntohs(ofr->hard_timeout);
2295 fr->packet_count = ntohll(ofr->packet_count);
2296 fr->byte_count = ntohll(ofr->byte_count);
2297 } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
2298 const struct ofp10_flow_removed *ofr;
2300 ofr = ofpbuf_pull(&b, sizeof *ofr);
2302 ofputil_match_from_ofp10_match(&ofr->match, &fr->match);
2303 fr->priority = ntohs(ofr->priority);
2304 fr->cookie = ofr->cookie;
2305 fr->reason = ofr->reason;
2307 fr->duration_sec = ntohl(ofr->duration_sec);
2308 fr->duration_nsec = ntohl(ofr->duration_nsec);
2309 fr->idle_timeout = ntohs(ofr->idle_timeout);
2310 fr->hard_timeout = 0;
2311 fr->packet_count = ntohll(ofr->packet_count);
2312 fr->byte_count = ntohll(ofr->byte_count);
2313 } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
2314 struct nx_flow_removed *nfr;
2317 nfr = ofpbuf_pull(&b, sizeof *nfr);
2318 error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match,
2324 return OFPERR_OFPBRC_BAD_LEN;
2327 fr->priority = ntohs(nfr->priority);
2328 fr->cookie = nfr->cookie;
2329 fr->reason = nfr->reason;
2331 fr->duration_sec = ntohl(nfr->duration_sec);
2332 fr->duration_nsec = ntohl(nfr->duration_nsec);
2333 fr->idle_timeout = ntohs(nfr->idle_timeout);
2334 fr->hard_timeout = 0;
2335 fr->packet_count = ntohll(nfr->packet_count);
2336 fr->byte_count = ntohll(nfr->byte_count);
2344 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
2345 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
2348 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
2349 enum ofputil_protocol protocol)
2354 case OFPUTIL_P_OF12_OXM:
2355 case OFPUTIL_P_OF13_OXM: {
2356 struct ofp12_flow_removed *ofr;
2358 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED,
2359 ofputil_protocol_to_ofp_version(protocol),
2360 htonl(0), NXM_TYPICAL_LEN);
2361 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
2362 ofr->cookie = fr->cookie;
2363 ofr->priority = htons(fr->priority);
2364 ofr->reason = fr->reason;
2365 ofr->table_id = fr->table_id;
2366 ofr->duration_sec = htonl(fr->duration_sec);
2367 ofr->duration_nsec = htonl(fr->duration_nsec);
2368 ofr->idle_timeout = htons(fr->idle_timeout);
2369 ofr->hard_timeout = htons(fr->hard_timeout);
2370 ofr->packet_count = htonll(fr->packet_count);
2371 ofr->byte_count = htonll(fr->byte_count);
2372 oxm_put_match(msg, &fr->match);
2376 case OFPUTIL_P_OF10_STD:
2377 case OFPUTIL_P_OF10_STD_TID: {
2378 struct ofp10_flow_removed *ofr;
2380 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
2382 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
2383 ofputil_match_to_ofp10_match(&fr->match, &ofr->match);
2384 ofr->cookie = fr->cookie;
2385 ofr->priority = htons(fr->priority);
2386 ofr->reason = fr->reason;
2387 ofr->duration_sec = htonl(fr->duration_sec);
2388 ofr->duration_nsec = htonl(fr->duration_nsec);
2389 ofr->idle_timeout = htons(fr->idle_timeout);
2390 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
2391 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
2395 case OFPUTIL_P_OF10_NXM:
2396 case OFPUTIL_P_OF10_NXM_TID: {
2397 struct nx_flow_removed *nfr;
2400 msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
2401 htonl(0), NXM_TYPICAL_LEN);
2402 nfr = ofpbuf_put_zeros(msg, sizeof *nfr);
2403 match_len = nx_put_match(msg, &fr->match, 0, 0);
2406 nfr->cookie = fr->cookie;
2407 nfr->priority = htons(fr->priority);
2408 nfr->reason = fr->reason;
2409 nfr->duration_sec = htonl(fr->duration_sec);
2410 nfr->duration_nsec = htonl(fr->duration_nsec);
2411 nfr->idle_timeout = htons(fr->idle_timeout);
2412 nfr->match_len = htons(match_len);
2413 nfr->packet_count = htonll(fr->packet_count);
2414 nfr->byte_count = htonll(fr->byte_count);
2426 ofputil_decode_packet_in_finish(struct ofputil_packet_in *pin,
2427 struct match *match, struct ofpbuf *b)
2429 pin->packet = b->data;
2430 pin->packet_len = b->size;
2432 pin->fmd.in_port = match->flow.in_port;
2433 pin->fmd.tun_id = match->flow.tunnel.tun_id;
2434 pin->fmd.metadata = match->flow.metadata;
2435 memcpy(pin->fmd.regs, match->flow.regs, sizeof pin->fmd.regs);
2439 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
2440 const struct ofp_header *oh)
2445 memset(pin, 0, sizeof *pin);
2447 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2448 raw = ofpraw_pull_assert(&b);
2449 if (raw == OFPRAW_OFPT13_PACKET_IN || raw == OFPRAW_OFPT12_PACKET_IN) {
2450 const struct ofp13_packet_in *opi;
2453 size_t packet_in_size;
2455 if (raw == OFPRAW_OFPT12_PACKET_IN) {
2456 packet_in_size = sizeof (struct ofp12_packet_in);
2458 packet_in_size = sizeof (struct ofp13_packet_in);
2461 opi = ofpbuf_pull(&b, packet_in_size);
2462 error = oxm_pull_match_loose(&b, &match);
2467 if (!ofpbuf_try_pull(&b, 2)) {
2468 return OFPERR_OFPBRC_BAD_LEN;
2471 pin->reason = opi->pi.reason;
2472 pin->table_id = opi->pi.table_id;
2473 pin->buffer_id = ntohl(opi->pi.buffer_id);
2474 pin->total_len = ntohs(opi->pi.total_len);
2476 if (raw == OFPRAW_OFPT13_PACKET_IN) {
2477 pin->cookie = opi->cookie;
2480 ofputil_decode_packet_in_finish(pin, &match, &b);
2481 } else if (raw == OFPRAW_OFPT10_PACKET_IN) {
2482 const struct ofp10_packet_in *opi;
2484 opi = ofpbuf_pull(&b, offsetof(struct ofp10_packet_in, data));
2486 pin->packet = opi->data;
2487 pin->packet_len = b.size;
2489 pin->fmd.in_port = ntohs(opi->in_port);
2490 pin->reason = opi->reason;
2491 pin->buffer_id = ntohl(opi->buffer_id);
2492 pin->total_len = ntohs(opi->total_len);
2493 } else if (raw == OFPRAW_NXT_PACKET_IN) {
2494 const struct nx_packet_in *npi;
2498 npi = ofpbuf_pull(&b, sizeof *npi);
2499 error = nx_pull_match_loose(&b, ntohs(npi->match_len), &match, NULL,
2505 if (!ofpbuf_try_pull(&b, 2)) {
2506 return OFPERR_OFPBRC_BAD_LEN;
2509 pin->reason = npi->reason;
2510 pin->table_id = npi->table_id;
2511 pin->cookie = npi->cookie;
2513 pin->buffer_id = ntohl(npi->buffer_id);
2514 pin->total_len = ntohs(npi->total_len);
2516 ofputil_decode_packet_in_finish(pin, &match, &b);
2525 ofputil_packet_in_to_match(const struct ofputil_packet_in *pin,
2526 struct match *match)
2530 match_init_catchall(match);
2531 if (pin->fmd.tun_id != htonll(0)) {
2532 match_set_tun_id(match, pin->fmd.tun_id);
2534 if (pin->fmd.metadata != htonll(0)) {
2535 match_set_metadata(match, pin->fmd.metadata);
2538 for (i = 0; i < FLOW_N_REGS; i++) {
2539 if (pin->fmd.regs[i]) {
2540 match_set_reg(match, i, pin->fmd.regs[i]);
2544 match_set_in_port(match, pin->fmd.in_port);
2547 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
2548 * in the format specified by 'packet_in_format'. */
2550 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
2551 enum ofputil_protocol protocol,
2552 enum nx_packet_in_format packet_in_format)
2554 size_t send_len = MIN(pin->send_len, pin->packet_len);
2555 struct ofpbuf *packet;
2557 /* Add OFPT_PACKET_IN. */
2558 if (protocol == OFPUTIL_P_OF13_OXM || protocol == OFPUTIL_P_OF12_OXM) {
2559 struct ofp13_packet_in *opi;
2561 enum ofpraw packet_in_raw;
2562 enum ofp_version packet_in_version;
2563 size_t packet_in_size;
2565 if (protocol == OFPUTIL_P_OF12_OXM) {
2566 packet_in_raw = OFPRAW_OFPT12_PACKET_IN;
2567 packet_in_version = OFP12_VERSION;
2568 packet_in_size = sizeof (struct ofp12_packet_in);
2570 packet_in_raw = OFPRAW_OFPT13_PACKET_IN;
2571 packet_in_version = OFP13_VERSION;
2572 packet_in_size = sizeof (struct ofp13_packet_in);
2575 ofputil_packet_in_to_match(pin, &match);
2577 /* The final argument is just an estimate of the space required. */
2578 packet = ofpraw_alloc_xid(packet_in_raw, packet_in_version,
2579 htonl(0), (sizeof(struct flow_metadata) * 2
2581 ofpbuf_put_zeros(packet, packet_in_size);
2582 oxm_put_match(packet, &match);
2583 ofpbuf_put_zeros(packet, 2);
2584 ofpbuf_put(packet, pin->packet, send_len);
2587 opi->pi.buffer_id = htonl(pin->buffer_id);
2588 opi->pi.total_len = htons(pin->total_len);
2589 opi->pi.reason = pin->reason;
2590 opi->pi.table_id = pin->table_id;
2591 if (protocol == OFPUTIL_P_OF13_OXM) {
2592 opi->cookie = pin->cookie;
2594 } else if (packet_in_format == NXPIF_OPENFLOW10) {
2595 struct ofp10_packet_in *opi;
2597 packet = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
2598 htonl(0), send_len);
2599 opi = ofpbuf_put_zeros(packet, offsetof(struct ofp10_packet_in, data));
2600 opi->total_len = htons(pin->total_len);
2601 opi->in_port = htons(pin->fmd.in_port);
2602 opi->reason = pin->reason;
2603 opi->buffer_id = htonl(pin->buffer_id);
2605 ofpbuf_put(packet, pin->packet, send_len);
2606 } else if (packet_in_format == NXPIF_NXM) {
2607 struct nx_packet_in *npi;
2611 ofputil_packet_in_to_match(pin, &match);
2613 /* The final argument is just an estimate of the space required. */
2614 packet = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, OFP10_VERSION,
2615 htonl(0), (sizeof(struct flow_metadata) * 2
2617 ofpbuf_put_zeros(packet, sizeof *npi);
2618 match_len = nx_put_match(packet, &match, 0, 0);
2619 ofpbuf_put_zeros(packet, 2);
2620 ofpbuf_put(packet, pin->packet, send_len);
2623 npi->buffer_id = htonl(pin->buffer_id);
2624 npi->total_len = htons(pin->total_len);
2625 npi->reason = pin->reason;
2626 npi->table_id = pin->table_id;
2627 npi->cookie = pin->cookie;
2628 npi->match_len = htons(match_len);
2632 ofpmsg_update_length(packet);
2638 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason)
2640 static char s[INT_STRLEN(int) + 1];
2647 case OFPR_INVALID_TTL:
2648 return "invalid_ttl";
2650 case OFPR_N_REASONS:
2652 sprintf(s, "%d", (int) reason);
2658 ofputil_packet_in_reason_from_string(const char *s,
2659 enum ofp_packet_in_reason *reason)
2663 for (i = 0; i < OFPR_N_REASONS; i++) {
2664 if (!strcasecmp(s, ofputil_packet_in_reason_to_string(i))) {
2672 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
2675 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
2676 * message's actions. The caller must initialize 'ofpacts' and retains
2677 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
2679 * Returns 0 if successful, otherwise an OFPERR_* value. */
2681 ofputil_decode_packet_out(struct ofputil_packet_out *po,
2682 const struct ofp_header *oh,
2683 struct ofpbuf *ofpacts)
2688 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2689 raw = ofpraw_pull_assert(&b);
2691 if (raw == OFPRAW_OFPT11_PACKET_OUT) {
2693 const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
2695 po->buffer_id = ntohl(opo->buffer_id);
2696 error = ofputil_port_from_ofp11(opo->in_port, &po->in_port);
2701 error = ofpacts_pull_openflow11_actions(&b, ntohs(opo->actions_len),
2706 } else if (raw == OFPRAW_OFPT10_PACKET_OUT) {
2708 const struct ofp10_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
2710 po->buffer_id = ntohl(opo->buffer_id);
2711 po->in_port = ntohs(opo->in_port);
2713 error = ofpacts_pull_openflow10(&b, ntohs(opo->actions_len), ofpacts);
2721 if (po->in_port >= OFPP_MAX && po->in_port != OFPP_LOCAL
2722 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
2723 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
2725 return OFPERR_OFPBRC_BAD_PORT;
2728 po->ofpacts = ofpacts->data;
2729 po->ofpacts_len = ofpacts->size;
2731 if (po->buffer_id == UINT32_MAX) {
2732 po->packet = b.data;
2733 po->packet_len = b.size;
2742 /* ofputil_phy_port */
2744 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
2745 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2746 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2747 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2748 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2749 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2750 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2751 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2753 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
2754 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
2755 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
2756 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
2757 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
2758 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
2760 static enum netdev_features
2761 netdev_port_features_from_ofp10(ovs_be32 ofp10_)
2763 uint32_t ofp10 = ntohl(ofp10_);
2764 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
2768 netdev_port_features_to_ofp10(enum netdev_features features)
2770 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
2773 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2774 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2775 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2776 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2777 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2778 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2779 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2780 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
2781 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
2782 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
2783 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
2784 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
2785 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
2786 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
2787 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
2788 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
2790 static enum netdev_features
2791 netdev_port_features_from_ofp11(ovs_be32 ofp11)
2793 return ntohl(ofp11) & 0xffff;
2797 netdev_port_features_to_ofp11(enum netdev_features features)
2799 return htonl(features & 0xffff);
2803 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
2804 const struct ofp10_phy_port *opp)
2806 memset(pp, 0, sizeof *pp);
2808 pp->port_no = ntohs(opp->port_no);
2809 memcpy(pp->hw_addr, opp->hw_addr, OFP_ETH_ALEN);
2810 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
2812 pp->config = ntohl(opp->config) & OFPPC10_ALL;
2813 pp->state = ntohl(opp->state) & OFPPS10_ALL;
2815 pp->curr = netdev_port_features_from_ofp10(opp->curr);
2816 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
2817 pp->supported = netdev_port_features_from_ofp10(opp->supported);
2818 pp->peer = netdev_port_features_from_ofp10(opp->peer);
2820 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
2821 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
2827 ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
2828 const struct ofp11_port *op)
2832 memset(pp, 0, sizeof *pp);
2834 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
2838 memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN);
2839 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
2841 pp->config = ntohl(op->config) & OFPPC11_ALL;
2842 pp->state = ntohl(op->state) & OFPPC11_ALL;
2844 pp->curr = netdev_port_features_from_ofp11(op->curr);
2845 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
2846 pp->supported = netdev_port_features_from_ofp11(op->supported);
2847 pp->peer = netdev_port_features_from_ofp11(op->peer);
2849 pp->curr_speed = ntohl(op->curr_speed);
2850 pp->max_speed = ntohl(op->max_speed);
2856 ofputil_get_phy_port_size(enum ofp_version ofp_version)
2858 switch (ofp_version) {
2860 return sizeof(struct ofp10_phy_port);
2864 return sizeof(struct ofp11_port);
2871 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
2872 struct ofp10_phy_port *opp)
2874 memset(opp, 0, sizeof *opp);
2876 opp->port_no = htons(pp->port_no);
2877 memcpy(opp->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2878 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2880 opp->config = htonl(pp->config & OFPPC10_ALL);
2881 opp->state = htonl(pp->state & OFPPS10_ALL);
2883 opp->curr = netdev_port_features_to_ofp10(pp->curr);
2884 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
2885 opp->supported = netdev_port_features_to_ofp10(pp->supported);
2886 opp->peer = netdev_port_features_to_ofp10(pp->peer);
2890 ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
2891 struct ofp11_port *op)
2893 memset(op, 0, sizeof *op);
2895 op->port_no = ofputil_port_to_ofp11(pp->port_no);
2896 memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2897 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2899 op->config = htonl(pp->config & OFPPC11_ALL);
2900 op->state = htonl(pp->state & OFPPS11_ALL);
2902 op->curr = netdev_port_features_to_ofp11(pp->curr);
2903 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
2904 op->supported = netdev_port_features_to_ofp11(pp->supported);
2905 op->peer = netdev_port_features_to_ofp11(pp->peer);
2907 op->curr_speed = htonl(pp->curr_speed);
2908 op->max_speed = htonl(pp->max_speed);
2912 ofputil_put_phy_port(enum ofp_version ofp_version,
2913 const struct ofputil_phy_port *pp, struct ofpbuf *b)
2915 switch (ofp_version) {
2916 case OFP10_VERSION: {
2917 struct ofp10_phy_port *opp;
2918 if (b->size + sizeof *opp <= UINT16_MAX) {
2919 opp = ofpbuf_put_uninit(b, sizeof *opp);
2920 ofputil_encode_ofp10_phy_port(pp, opp);
2927 case OFP13_VERSION: {
2928 struct ofp11_port *op;
2929 if (b->size + sizeof *op <= UINT16_MAX) {
2930 op = ofpbuf_put_uninit(b, sizeof *op);
2931 ofputil_encode_ofp11_port(pp, op);
2942 ofputil_append_port_desc_stats_reply(enum ofp_version ofp_version,
2943 const struct ofputil_phy_port *pp,
2944 struct list *replies)
2946 switch (ofp_version) {
2947 case OFP10_VERSION: {
2948 struct ofp10_phy_port *opp;
2950 opp = ofpmp_append(replies, sizeof *opp);
2951 ofputil_encode_ofp10_phy_port(pp, opp);
2957 case OFP13_VERSION: {
2958 struct ofp11_port *op;
2960 op = ofpmp_append(replies, sizeof *op);
2961 ofputil_encode_ofp11_port(pp, op);
2970 /* ofputil_switch_features */
2972 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
2973 OFPC_IP_REASM | OFPC_QUEUE_STATS)
2974 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
2975 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
2976 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
2977 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
2978 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
2979 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
2981 struct ofputil_action_bit_translation {
2982 enum ofputil_action_bitmap ofputil_bit;
2986 static const struct ofputil_action_bit_translation of10_action_bits[] = {
2987 { OFPUTIL_A_OUTPUT, OFPAT10_OUTPUT },
2988 { OFPUTIL_A_SET_VLAN_VID, OFPAT10_SET_VLAN_VID },
2989 { OFPUTIL_A_SET_VLAN_PCP, OFPAT10_SET_VLAN_PCP },
2990 { OFPUTIL_A_STRIP_VLAN, OFPAT10_STRIP_VLAN },
2991 { OFPUTIL_A_SET_DL_SRC, OFPAT10_SET_DL_SRC },
2992 { OFPUTIL_A_SET_DL_DST, OFPAT10_SET_DL_DST },
2993 { OFPUTIL_A_SET_NW_SRC, OFPAT10_SET_NW_SRC },
2994 { OFPUTIL_A_SET_NW_DST, OFPAT10_SET_NW_DST },
2995 { OFPUTIL_A_SET_NW_TOS, OFPAT10_SET_NW_TOS },
2996 { OFPUTIL_A_SET_TP_SRC, OFPAT10_SET_TP_SRC },
2997 { OFPUTIL_A_SET_TP_DST, OFPAT10_SET_TP_DST },
2998 { OFPUTIL_A_ENQUEUE, OFPAT10_ENQUEUE },
3002 static enum ofputil_action_bitmap
3003 decode_action_bits(ovs_be32 of_actions,
3004 const struct ofputil_action_bit_translation *x)
3006 enum ofputil_action_bitmap ofputil_actions;
3008 ofputil_actions = 0;
3009 for (; x->ofputil_bit; x++) {
3010 if (of_actions & htonl(1u << x->of_bit)) {
3011 ofputil_actions |= x->ofputil_bit;
3014 return ofputil_actions;
3018 ofputil_capabilities_mask(enum ofp_version ofp_version)
3020 /* Handle capabilities whose bit is unique for all Open Flow versions */
3021 switch (ofp_version) {
3024 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
3027 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
3029 /* Caller needs to check osf->header.version itself */
3034 /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an
3035 * abstract representation in '*features'. Initializes '*b' to iterate over
3036 * the OpenFlow port structures following 'osf' with later calls to
3037 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an
3038 * OFPERR_* value. */
3040 ofputil_decode_switch_features(const struct ofp_header *oh,
3041 struct ofputil_switch_features *features,
3044 const struct ofp_switch_features *osf;
3047 ofpbuf_use_const(b, oh, ntohs(oh->length));
3048 raw = ofpraw_pull_assert(b);
3050 osf = ofpbuf_pull(b, sizeof *osf);
3051 features->datapath_id = ntohll(osf->datapath_id);
3052 features->n_buffers = ntohl(osf->n_buffers);
3053 features->n_tables = osf->n_tables;
3054 features->auxiliary_id = 0;
3056 features->capabilities = ntohl(osf->capabilities) &
3057 ofputil_capabilities_mask(oh->version);
3059 if (b->size % ofputil_get_phy_port_size(oh->version)) {
3060 return OFPERR_OFPBRC_BAD_LEN;
3063 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
3064 if (osf->capabilities & htonl(OFPC10_STP)) {
3065 features->capabilities |= OFPUTIL_C_STP;
3067 features->actions = decode_action_bits(osf->actions, of10_action_bits);
3068 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY
3069 || raw == OFPRAW_OFPT13_FEATURES_REPLY) {
3070 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
3071 features->capabilities |= OFPUTIL_C_GROUP_STATS;
3073 features->actions = 0;
3074 if (raw == OFPRAW_OFPT13_FEATURES_REPLY) {
3075 features->auxiliary_id = osf->auxiliary_id;
3078 return OFPERR_OFPBRC_BAD_VERSION;
3084 /* Returns true if the maximum number of ports are in 'oh'. */
3086 max_ports_in_features(const struct ofp_header *oh)
3088 size_t pp_size = ofputil_get_phy_port_size(oh->version);
3089 return ntohs(oh->length) + pp_size > UINT16_MAX;
3092 /* Given a buffer 'b' that contains a Features Reply message, checks if
3093 * it contains the maximum number of ports that will fit. If so, it
3094 * returns true and removes the ports from the message. The caller
3095 * should then send an OFPST_PORT_DESC stats request to get the ports,
3096 * since the switch may have more ports than could be represented in the
3097 * Features Reply. Otherwise, returns false.
3100 ofputil_switch_features_ports_trunc(struct ofpbuf *b)
3102 struct ofp_header *oh = b->data;
3104 if (max_ports_in_features(oh)) {
3105 /* Remove all the ports. */
3106 b->size = (sizeof(struct ofp_header)
3107 + sizeof(struct ofp_switch_features));
3108 ofpmsg_update_length(b);
3117 encode_action_bits(enum ofputil_action_bitmap ofputil_actions,
3118 const struct ofputil_action_bit_translation *x)
3120 uint32_t of_actions;
3123 for (; x->ofputil_bit; x++) {
3124 if (ofputil_actions & x->ofputil_bit) {
3125 of_actions |= 1 << x->of_bit;
3128 return htonl(of_actions);
3131 /* Returns a buffer owned by the caller that encodes 'features' in the format
3132 * required by 'protocol' with the given 'xid'. The caller should append port
3133 * information to the buffer with subsequent calls to
3134 * ofputil_put_switch_features_port(). */
3136 ofputil_encode_switch_features(const struct ofputil_switch_features *features,
3137 enum ofputil_protocol protocol, ovs_be32 xid)
3139 struct ofp_switch_features *osf;
3141 enum ofp_version version;
3144 version = ofputil_protocol_to_ofp_version(protocol);
3147 raw = OFPRAW_OFPT10_FEATURES_REPLY;
3151 raw = OFPRAW_OFPT11_FEATURES_REPLY;
3154 raw = OFPRAW_OFPT13_FEATURES_REPLY;
3159 b = ofpraw_alloc_xid(raw, version, xid, 0);
3160 osf = ofpbuf_put_zeros(b, sizeof *osf);
3161 osf->datapath_id = htonll(features->datapath_id);
3162 osf->n_buffers = htonl(features->n_buffers);
3163 osf->n_tables = features->n_tables;
3165 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
3166 osf->capabilities = htonl(features->capabilities &
3167 ofputil_capabilities_mask(version));
3170 if (features->capabilities & OFPUTIL_C_STP) {
3171 osf->capabilities |= htonl(OFPC10_STP);
3173 osf->actions = encode_action_bits(features->actions, of10_action_bits);
3176 osf->auxiliary_id = features->auxiliary_id;
3180 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
3181 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
3191 /* Encodes 'pp' into the format required by the switch_features message already
3192 * in 'b', which should have been returned by ofputil_encode_switch_features(),
3193 * and appends the encoded version to 'b'. */
3195 ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
3198 const struct ofp_header *oh = b->data;
3200 if (oh->version < OFP13_VERSION) {
3201 ofputil_put_phy_port(oh->version, pp, b);
3205 /* ofputil_port_status */
3207 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
3208 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
3210 ofputil_decode_port_status(const struct ofp_header *oh,
3211 struct ofputil_port_status *ps)
3213 const struct ofp_port_status *ops;
3217 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3218 ofpraw_pull_assert(&b);
3219 ops = ofpbuf_pull(&b, sizeof *ops);
3221 if (ops->reason != OFPPR_ADD &&
3222 ops->reason != OFPPR_DELETE &&
3223 ops->reason != OFPPR_MODIFY) {
3224 return OFPERR_NXBRC_BAD_REASON;
3226 ps->reason = ops->reason;
3228 retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
3229 assert(retval != EOF);
3233 /* Converts the abstract form of a "port status" message in '*ps' into an
3234 * OpenFlow message suitable for 'protocol', and returns that encoded form in
3235 * a buffer owned by the caller. */
3237 ofputil_encode_port_status(const struct ofputil_port_status *ps,
3238 enum ofputil_protocol protocol)
3240 struct ofp_port_status *ops;
3242 enum ofp_version version;
3245 version = ofputil_protocol_to_ofp_version(protocol);
3248 raw = OFPRAW_OFPT10_PORT_STATUS;
3254 raw = OFPRAW_OFPT11_PORT_STATUS;
3261 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
3262 ops = ofpbuf_put_zeros(b, sizeof *ops);
3263 ops->reason = ps->reason;
3264 ofputil_put_phy_port(version, &ps->desc, b);
3265 ofpmsg_update_length(b);
3269 /* ofputil_port_mod */
3271 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
3272 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
3274 ofputil_decode_port_mod(const struct ofp_header *oh,
3275 struct ofputil_port_mod *pm)
3280 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3281 raw = ofpraw_pull_assert(&b);
3283 if (raw == OFPRAW_OFPT10_PORT_MOD) {
3284 const struct ofp10_port_mod *opm = b.data;
3286 pm->port_no = ntohs(opm->port_no);
3287 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
3288 pm->config = ntohl(opm->config) & OFPPC10_ALL;
3289 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
3290 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
3291 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
3292 const struct ofp11_port_mod *opm = b.data;
3295 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
3300 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
3301 pm->config = ntohl(opm->config) & OFPPC11_ALL;
3302 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
3303 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
3305 return OFPERR_OFPBRC_BAD_TYPE;
3308 pm->config &= pm->mask;
3312 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
3313 * message suitable for 'protocol', and returns that encoded form in a buffer
3314 * owned by the caller. */
3316 ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
3317 enum ofputil_protocol protocol)
3319 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
3322 switch (ofp_version) {
3323 case OFP10_VERSION: {
3324 struct ofp10_port_mod *opm;
3326 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
3327 opm = ofpbuf_put_zeros(b, sizeof *opm);
3328 opm->port_no = htons(pm->port_no);
3329 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
3330 opm->config = htonl(pm->config & OFPPC10_ALL);
3331 opm->mask = htonl(pm->mask & OFPPC10_ALL);
3332 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
3338 case OFP13_VERSION: {
3339 struct ofp11_port_mod *opm;
3341 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
3342 opm = ofpbuf_put_zeros(b, sizeof *opm);
3343 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
3344 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
3345 opm->config = htonl(pm->config & OFPPC11_ALL);
3346 opm->mask = htonl(pm->mask & OFPPC11_ALL);
3347 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
3361 ofputil_put_ofp10_table_stats(const struct ofp12_table_stats *in,
3365 enum ofp10_flow_wildcards wc10;
3366 enum oxm12_ofb_match_fields mf12;
3369 static const struct wc_map wc_map[] = {
3370 { OFPFW10_IN_PORT, OFPXMT12_OFB_IN_PORT },
3371 { OFPFW10_DL_VLAN, OFPXMT12_OFB_VLAN_VID },
3372 { OFPFW10_DL_SRC, OFPXMT12_OFB_ETH_SRC },
3373 { OFPFW10_DL_DST, OFPXMT12_OFB_ETH_DST},
3374 { OFPFW10_DL_TYPE, OFPXMT12_OFB_ETH_TYPE },
3375 { OFPFW10_NW_PROTO, OFPXMT12_OFB_IP_PROTO },
3376 { OFPFW10_TP_SRC, OFPXMT12_OFB_TCP_SRC },
3377 { OFPFW10_TP_DST, OFPXMT12_OFB_TCP_DST },
3378 { OFPFW10_NW_SRC_MASK, OFPXMT12_OFB_IPV4_SRC },
3379 { OFPFW10_NW_DST_MASK, OFPXMT12_OFB_IPV4_DST },
3380 { OFPFW10_DL_VLAN_PCP, OFPXMT12_OFB_VLAN_PCP },
3381 { OFPFW10_NW_TOS, OFPXMT12_OFB_IP_DSCP },
3384 struct ofp10_table_stats *out;
3385 const struct wc_map *p;
3387 out = ofpbuf_put_uninit(buf, sizeof *out);
3388 out->table_id = in->table_id;
3389 strcpy(out->name, in->name);
3391 for (p = wc_map; p < &wc_map[ARRAY_SIZE(wc_map)]; p++) {
3392 if (in->wildcards & htonll(1ULL << p->mf12)) {
3393 out->wildcards |= htonl(p->wc10);
3396 out->max_entries = in->max_entries;
3397 out->active_count = in->active_count;
3398 put_32aligned_be64(&out->lookup_count, in->lookup_count);
3399 put_32aligned_be64(&out->matched_count, in->matched_count);
3403 oxm12_to_ofp11_flow_match_fields(ovs_be64 oxm12)
3406 enum ofp11_flow_match_fields fmf11;
3407 enum oxm12_ofb_match_fields mf12;
3410 static const struct map map[] = {
3411 { OFPFMF11_IN_PORT, OFPXMT12_OFB_IN_PORT },
3412 { OFPFMF11_DL_VLAN, OFPXMT12_OFB_VLAN_VID },
3413 { OFPFMF11_DL_VLAN_PCP, OFPXMT12_OFB_VLAN_PCP },
3414 { OFPFMF11_DL_TYPE, OFPXMT12_OFB_ETH_TYPE },
3415 { OFPFMF11_NW_TOS, OFPXMT12_OFB_IP_DSCP },
3416 { OFPFMF11_NW_PROTO, OFPXMT12_OFB_IP_PROTO },
3417 { OFPFMF11_TP_SRC, OFPXMT12_OFB_TCP_SRC },
3418 { OFPFMF11_TP_DST, OFPXMT12_OFB_TCP_DST },
3419 { OFPFMF11_MPLS_LABEL, OFPXMT12_OFB_MPLS_LABEL },
3420 { OFPFMF11_MPLS_TC, OFPXMT12_OFB_MPLS_TC },
3421 /* I don't know what OFPFMF11_TYPE means. */
3422 { OFPFMF11_DL_SRC, OFPXMT12_OFB_ETH_SRC },
3423 { OFPFMF11_DL_DST, OFPXMT12_OFB_ETH_DST },
3424 { OFPFMF11_NW_SRC, OFPXMT12_OFB_IPV4_SRC },
3425 { OFPFMF11_NW_DST, OFPXMT12_OFB_IPV4_DST },
3426 { OFPFMF11_METADATA, OFPXMT12_OFB_METADATA },
3429 const struct map *p;
3433 for (p = map; p < &map[ARRAY_SIZE(map)]; p++) {
3434 if (oxm12 & htonll(1ULL << p->mf12)) {
3438 return htonl(fmf11);
3442 ofputil_put_ofp11_table_stats(const struct ofp12_table_stats *in,
3445 struct ofp11_table_stats *out;
3447 out = ofpbuf_put_uninit(buf, sizeof *out);
3448 out->table_id = in->table_id;
3449 strcpy(out->name, in->name);
3450 out->wildcards = oxm12_to_ofp11_flow_match_fields(in->wildcards);
3451 out->match = oxm12_to_ofp11_flow_match_fields(in->match);
3452 out->instructions = in->instructions;
3453 out->write_actions = in->write_actions;
3454 out->apply_actions = in->apply_actions;
3455 out->config = in->config;
3456 out->max_entries = in->max_entries;
3457 out->active_count = in->active_count;
3458 out->lookup_count = in->lookup_count;
3459 out->matched_count = in->matched_count;
3463 ofputil_put_ofp13_table_stats(const struct ofp12_table_stats *in,
3466 struct ofp13_table_stats *out;
3468 /* OF 1.3 splits table features off the ofp_table_stats,
3469 * so there is not much here. */
3471 out = ofpbuf_put_uninit(buf, sizeof *out);
3472 out->table_id = in->table_id;
3473 out->active_count = in->active_count;
3474 out->lookup_count = in->lookup_count;
3475 out->matched_count = in->matched_count;
3479 ofputil_encode_table_stats_reply(const struct ofp12_table_stats stats[], int n,
3480 const struct ofp_header *request)
3482 struct ofpbuf *reply;
3485 reply = ofpraw_alloc_stats_reply(request, n * sizeof *stats);
3487 switch ((enum ofp_version) request->version) {
3489 for (i = 0; i < n; i++) {
3490 ofputil_put_ofp10_table_stats(&stats[i], reply);
3495 for (i = 0; i < n; i++) {
3496 ofputil_put_ofp11_table_stats(&stats[i], reply);
3501 ofpbuf_put(reply, stats, n * sizeof *stats);
3505 for (i = 0; i < n; i++) {
3506 ofputil_put_ofp13_table_stats(&stats[i], reply);
3517 /* ofputil_flow_monitor_request */
3519 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
3520 * ofputil_flow_monitor_request in 'rq'.
3522 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
3523 * message. Calling this function multiple times for a single 'msg' iterates
3524 * through the requests. The caller must initially leave 'msg''s layer
3525 * pointers null and not modify them between calls.
3527 * Returns 0 if successful, EOF if no requests were left in this 'msg',
3528 * otherwise an OFPERR_* value. */
3530 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
3533 struct nx_flow_monitor_request *nfmr;
3537 msg->l2 = msg->data;
3538 ofpraw_pull_assert(msg);
3545 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
3547 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %zu "
3548 "leftover bytes at end", msg->size);
3549 return OFPERR_OFPBRC_BAD_LEN;
3552 flags = ntohs(nfmr->flags);
3553 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
3554 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
3555 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
3556 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
3558 return OFPERR_NXBRC_FM_BAD_FLAGS;
3561 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
3562 return OFPERR_NXBRC_MUST_BE_ZERO;
3565 rq->id = ntohl(nfmr->id);
3567 rq->out_port = ntohs(nfmr->out_port);
3568 rq->table_id = nfmr->table_id;
3570 return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL, NULL);
3574 ofputil_append_flow_monitor_request(
3575 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
3577 struct nx_flow_monitor_request *nfmr;
3582 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
3585 start_ofs = msg->size;
3586 ofpbuf_put_zeros(msg, sizeof *nfmr);
3587 match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
3589 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
3590 nfmr->id = htonl(rq->id);
3591 nfmr->flags = htons(rq->flags);
3592 nfmr->out_port = htons(rq->out_port);
3593 nfmr->match_len = htons(match_len);
3594 nfmr->table_id = rq->table_id;
3597 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
3598 * into an abstract ofputil_flow_update in 'update'. The caller must have
3599 * initialized update->match to point to space allocated for a match.
3601 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
3602 * actions (except for NXFME_ABBREV, which never includes actions). The caller
3603 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
3604 * will point into the 'ofpacts' buffer.
3606 * Multiple flow updates can be packed into a single OpenFlow message. Calling
3607 * this function multiple times for a single 'msg' iterates through the
3608 * updates. The caller must initially leave 'msg''s layer pointers null and
3609 * not modify them between calls.
3611 * Returns 0 if successful, EOF if no updates were left in this 'msg',
3612 * otherwise an OFPERR_* value. */
3614 ofputil_decode_flow_update(struct ofputil_flow_update *update,
3615 struct ofpbuf *msg, struct ofpbuf *ofpacts)
3617 struct nx_flow_update_header *nfuh;
3618 unsigned int length;
3621 msg->l2 = msg->data;
3622 ofpraw_pull_assert(msg);
3629 if (msg->size < sizeof(struct nx_flow_update_header)) {
3634 update->event = ntohs(nfuh->event);
3635 length = ntohs(nfuh->length);
3636 if (length > msg->size || length % 8) {
3640 if (update->event == NXFME_ABBREV) {
3641 struct nx_flow_update_abbrev *nfua;
3643 if (length != sizeof *nfua) {
3647 nfua = ofpbuf_pull(msg, sizeof *nfua);
3648 update->xid = nfua->xid;
3650 } else if (update->event == NXFME_ADDED
3651 || update->event == NXFME_DELETED
3652 || update->event == NXFME_MODIFIED) {
3653 struct nx_flow_update_full *nfuf;
3654 unsigned int actions_len;
3655 unsigned int match_len;
3658 if (length < sizeof *nfuf) {
3662 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
3663 match_len = ntohs(nfuf->match_len);
3664 if (sizeof *nfuf + match_len > length) {
3668 update->reason = ntohs(nfuf->reason);
3669 update->idle_timeout = ntohs(nfuf->idle_timeout);
3670 update->hard_timeout = ntohs(nfuf->hard_timeout);
3671 update->table_id = nfuf->table_id;
3672 update->cookie = nfuf->cookie;
3673 update->priority = ntohs(nfuf->priority);
3675 error = nx_pull_match(msg, match_len, update->match, NULL, NULL);
3680 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
3681 error = ofpacts_pull_openflow10(msg, actions_len, ofpacts);
3686 update->ofpacts = ofpacts->data;
3687 update->ofpacts_len = ofpacts->size;
3690 VLOG_WARN_RL(&bad_ofmsg_rl,
3691 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
3692 ntohs(nfuh->event));
3693 return OFPERR_NXBRC_FM_BAD_EVENT;
3697 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %zu "
3698 "leftover bytes at end", msg->size);
3699 return OFPERR_OFPBRC_BAD_LEN;
3703 ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
3705 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
3707 return ntohl(cancel->id);
3711 ofputil_encode_flow_monitor_cancel(uint32_t id)
3713 struct nx_flow_monitor_cancel *nfmc;
3716 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
3717 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
3718 nfmc->id = htonl(id);
3723 ofputil_start_flow_update(struct list *replies)
3727 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
3731 list_push_back(replies, &msg->list_node);
3735 ofputil_append_flow_update(const struct ofputil_flow_update *update,
3736 struct list *replies)
3738 struct nx_flow_update_header *nfuh;
3742 msg = ofpbuf_from_list(list_back(replies));
3743 start_ofs = msg->size;
3745 if (update->event == NXFME_ABBREV) {
3746 struct nx_flow_update_abbrev *nfua;
3748 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
3749 nfua->xid = update->xid;
3751 struct nx_flow_update_full *nfuf;
3754 ofpbuf_put_zeros(msg, sizeof *nfuf);
3755 match_len = nx_put_match(msg, update->match, htonll(0), htonll(0));
3756 ofpacts_put_openflow10(update->ofpacts, update->ofpacts_len, msg);
3758 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
3759 nfuf->reason = htons(update->reason);
3760 nfuf->priority = htons(update->priority);
3761 nfuf->idle_timeout = htons(update->idle_timeout);
3762 nfuf->hard_timeout = htons(update->hard_timeout);
3763 nfuf->match_len = htons(match_len);
3764 nfuf->table_id = update->table_id;
3765 nfuf->cookie = update->cookie;
3768 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
3769 nfuh->length = htons(msg->size - start_ofs);
3770 nfuh->event = htons(update->event);
3772 ofpmp_postappend(replies, start_ofs);
3776 ofputil_encode_packet_out(const struct ofputil_packet_out *po,
3777 enum ofputil_protocol protocol)
3779 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
3783 size = po->ofpacts_len;
3784 if (po->buffer_id == UINT32_MAX) {
3785 size += po->packet_len;
3788 switch (ofp_version) {
3789 case OFP10_VERSION: {
3790 struct ofp10_packet_out *opo;
3793 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
3794 ofpbuf_put_zeros(msg, sizeof *opo);
3795 actions_ofs = msg->size;
3796 ofpacts_put_openflow10(po->ofpacts, po->ofpacts_len, msg);
3799 opo->buffer_id = htonl(po->buffer_id);
3800 opo->in_port = htons(po->in_port);
3801 opo->actions_len = htons(msg->size - actions_ofs);
3807 case OFP13_VERSION: {
3808 struct ofp11_packet_out *opo;
3811 msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size);
3812 ofpbuf_put_zeros(msg, sizeof *opo);
3813 len = ofpacts_put_openflow11_actions(po->ofpacts, po->ofpacts_len, msg);
3816 opo->buffer_id = htonl(po->buffer_id);
3817 opo->in_port = ofputil_port_to_ofp11(po->in_port);
3818 opo->actions_len = htons(len);
3826 if (po->buffer_id == UINT32_MAX) {
3827 ofpbuf_put(msg, po->packet, po->packet_len);
3830 ofpmsg_update_length(msg);
3835 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
3837 make_echo_request(enum ofp_version ofp_version)
3839 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
3843 /* Creates and returns an OFPT_ECHO_REPLY message matching the
3844 * OFPT_ECHO_REQUEST message in 'rq'. */
3846 make_echo_reply(const struct ofp_header *rq)
3848 struct ofpbuf rq_buf;
3849 struct ofpbuf *reply;
3851 ofpbuf_use_const(&rq_buf, rq, ntohs(rq->length));
3852 ofpraw_pull_assert(&rq_buf);
3854 reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY, rq, rq_buf.size);
3855 ofpbuf_put(reply, rq_buf.data, rq_buf.size);
3860 ofputil_encode_barrier_request(enum ofp_version ofp_version)
3864 switch (ofp_version) {
3868 type = OFPRAW_OFPT11_BARRIER_REQUEST;
3872 type = OFPRAW_OFPT10_BARRIER_REQUEST;
3879 return ofpraw_alloc(type, ofp_version, 0);
3883 ofputil_frag_handling_to_string(enum ofp_config_flags flags)
3885 switch (flags & OFPC_FRAG_MASK) {
3886 case OFPC_FRAG_NORMAL: return "normal";
3887 case OFPC_FRAG_DROP: return "drop";
3888 case OFPC_FRAG_REASM: return "reassemble";
3889 case OFPC_FRAG_NX_MATCH: return "nx-match";
3896 ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
3898 if (!strcasecmp(s, "normal")) {
3899 *flags = OFPC_FRAG_NORMAL;
3900 } else if (!strcasecmp(s, "drop")) {
3901 *flags = OFPC_FRAG_DROP;
3902 } else if (!strcasecmp(s, "reassemble")) {
3903 *flags = OFPC_FRAG_REASM;
3904 } else if (!strcasecmp(s, "nx-match")) {
3905 *flags = OFPC_FRAG_NX_MATCH;
3912 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
3913 * port number and stores the latter in '*ofp10_port', for the purpose of
3914 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
3915 * otherwise an OFPERR_* number.
3917 * See the definition of OFP11_MAX for an explanation of the mapping. */
3919 ofputil_port_from_ofp11(ovs_be32 ofp11_port, uint16_t *ofp10_port)
3921 uint32_t ofp11_port_h = ntohl(ofp11_port);
3923 if (ofp11_port_h < OFPP_MAX) {
3924 *ofp10_port = ofp11_port_h;
3926 } else if (ofp11_port_h >= OFPP11_MAX) {
3927 *ofp10_port = ofp11_port_h - OFPP11_OFFSET;
3930 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
3931 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
3932 ofp11_port_h, OFPP_MAX - 1,
3933 (uint32_t) OFPP11_MAX, UINT32_MAX);
3934 return OFPERR_OFPBAC_BAD_OUT_PORT;
3938 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
3939 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
3941 * See the definition of OFP11_MAX for an explanation of the mapping. */
3943 ofputil_port_to_ofp11(uint16_t ofp10_port)
3945 return htonl(ofp10_port < OFPP_MAX
3947 : ofp10_port + OFPP11_OFFSET);
3950 /* Checks that 'port' is a valid output port for the OFPAT10_OUTPUT action, given
3951 * that the switch will never have more than 'max_ports' ports. Returns 0 if
3952 * 'port' is valid, otherwise an OpenFlow return code. */
3954 ofputil_check_output_port(uint16_t port, int max_ports)
3962 case OFPP_CONTROLLER:
3968 if (port < max_ports) {
3971 return OFPERR_OFPBAC_BAD_OUT_PORT;
3975 #define OFPUTIL_NAMED_PORTS \
3976 OFPUTIL_NAMED_PORT(IN_PORT) \
3977 OFPUTIL_NAMED_PORT(TABLE) \
3978 OFPUTIL_NAMED_PORT(NORMAL) \
3979 OFPUTIL_NAMED_PORT(FLOOD) \
3980 OFPUTIL_NAMED_PORT(ALL) \
3981 OFPUTIL_NAMED_PORT(CONTROLLER) \
3982 OFPUTIL_NAMED_PORT(LOCAL) \
3983 OFPUTIL_NAMED_PORT(ANY)
3985 /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */
3986 #define OFPUTIL_NAMED_PORTS_WITH_NONE \
3987 OFPUTIL_NAMED_PORTS \
3988 OFPUTIL_NAMED_PORT(NONE)
3990 /* Stores the port number represented by 's' into '*portp'. 's' may be an
3991 * integer or, for reserved ports, the standard OpenFlow name for the port
3994 * Returns true if successful, false if 's' is not a valid OpenFlow port number
3995 * or name. The caller should issue an error message in this case, because
3996 * this function usually does not. (This gives the caller an opportunity to
3997 * look up the port name another way, e.g. by contacting the switch and listing
3998 * the names of all its ports).
4000 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
4001 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
4002 * range as described in include/openflow/openflow-1.1.h. */
4004 ofputil_port_from_string(const char *s, uint16_t *portp)
4006 unsigned int port32;
4009 if (str_to_uint(s, 10, &port32)) {
4010 if (port32 < OFPP_MAX) {
4013 } else if (port32 < OFPP_FIRST_RESV) {
4014 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
4015 "be translated to %u when talking to an OF1.1 or "
4016 "later controller", port32, port32 + OFPP11_OFFSET);
4019 } else if (port32 <= OFPP_LAST_RESV) {
4023 ofputil_format_port(port32, &s);
4024 VLOG_WARN_ONCE("referring to port %s as %u is deprecated for "
4025 "compatibility with future versions of OpenFlow",
4026 ds_cstr(&s), port32);
4031 } else if (port32 < OFPP11_MAX) {
4032 VLOG_WARN("port %u is outside the supported range 0 through "
4033 "%"PRIx16" or 0x%x through 0x%"PRIx32, port32,
4034 UINT16_MAX, (unsigned int) OFPP11_MAX, UINT32_MAX);
4037 *portp = port32 - OFPP11_OFFSET;
4045 static const struct pair pairs[] = {
4046 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
4047 OFPUTIL_NAMED_PORTS_WITH_NONE
4048 #undef OFPUTIL_NAMED_PORT
4050 const struct pair *p;
4052 for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) {
4053 if (!strcasecmp(s, p->name)) {
4062 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
4063 * Most ports' string representation is just the port number, but for special
4064 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
4066 ofputil_format_port(uint16_t port, struct ds *s)
4071 #define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break;
4073 #undef OFPUTIL_NAMED_PORT
4076 ds_put_format(s, "%"PRIu16, port);
4079 ds_put_cstr(s, name);
4082 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
4083 * 'ofp_version', tries to pull the first element from the array. If
4084 * successful, initializes '*pp' with an abstract representation of the
4085 * port and returns 0. If no ports remain to be decoded, returns EOF.
4086 * On an error, returns a positive OFPERR_* value. */
4088 ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
4089 struct ofputil_phy_port *pp)
4091 switch (ofp_version) {
4092 case OFP10_VERSION: {
4093 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
4094 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
4098 case OFP13_VERSION: {
4099 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
4100 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
4107 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
4108 * 'ofp_version', returns the number of elements. */
4109 size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *b)
4111 return b->size / ofputil_get_phy_port_size(ofp_version);
4114 /* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if
4115 * 'name' is "output" then the return value is OFPUTIL_OFPAT10_OUTPUT), or -1 if
4116 * 'name' is not the name of any action.
4118 * ofp-util.def lists the mapping from names to action. */
4120 ofputil_action_code_from_name(const char *name)
4122 static const char *names[OFPUTIL_N_ACTIONS] = {
4124 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) NAME,
4125 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
4126 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
4127 #include "ofp-util.def"
4132 for (p = names; p < &names[ARRAY_SIZE(names)]; p++) {
4133 if (*p && !strcasecmp(name, *p)) {
4140 /* Appends an action of the type specified by 'code' to 'buf' and returns the
4141 * action. Initializes the parts of 'action' that identify it as having type
4142 * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that
4143 * have variable length, the length used and cleared is that of struct
4146 ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
4149 case OFPUTIL_ACTION_INVALID:
4152 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
4153 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
4154 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
4155 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
4156 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
4157 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
4158 #include "ofp-util.def"
4163 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
4165 ofputil_init_##ENUM(struct STRUCT *s) \
4167 memset(s, 0, sizeof *s); \
4168 s->type = htons(ENUM); \
4169 s->len = htons(sizeof *s); \
4173 ofputil_put_##ENUM(struct ofpbuf *buf) \
4175 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
4176 ofputil_init_##ENUM(s); \
4179 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
4180 OFPAT10_ACTION(ENUM, STRUCT, NAME)
4181 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
4183 ofputil_init_##ENUM(struct STRUCT *s) \
4185 memset(s, 0, sizeof *s); \
4186 s->type = htons(OFPAT10_VENDOR); \
4187 s->len = htons(sizeof *s); \
4188 s->vendor = htonl(NX_VENDOR_ID); \
4189 s->subtype = htons(ENUM); \
4193 ofputil_put_##ENUM(struct ofpbuf *buf) \
4195 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
4196 ofputil_init_##ENUM(s); \
4199 #include "ofp-util.def"
4202 ofputil_normalize_match__(struct match *match, bool may_log)
4205 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
4206 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
4207 MAY_NW_PROTO = 1 << 2, /* nw_proto */
4208 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
4209 MAY_ARP_SHA = 1 << 4, /* arp_sha */
4210 MAY_ARP_THA = 1 << 5, /* arp_tha */
4211 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
4212 MAY_ND_TARGET = 1 << 7 /* nd_target */
4215 struct flow_wildcards wc;
4217 /* Figure out what fields may be matched. */
4218 if (match->flow.dl_type == htons(ETH_TYPE_IP)) {
4219 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
4220 if (match->flow.nw_proto == IPPROTO_TCP ||
4221 match->flow.nw_proto == IPPROTO_UDP ||
4222 match->flow.nw_proto == IPPROTO_ICMP) {
4223 may_match |= MAY_TP_ADDR;
4225 } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
4226 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
4227 if (match->flow.nw_proto == IPPROTO_TCP ||
4228 match->flow.nw_proto == IPPROTO_UDP) {
4229 may_match |= MAY_TP_ADDR;
4230 } else if (match->flow.nw_proto == IPPROTO_ICMPV6) {
4231 may_match |= MAY_TP_ADDR;
4232 if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
4233 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
4234 } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
4235 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
4238 } else if (match->flow.dl_type == htons(ETH_TYPE_ARP) ||
4239 match->flow.dl_type == htons(ETH_TYPE_RARP)) {
4240 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
4245 /* Clear the fields that may not be matched. */
4247 if (!(may_match & MAY_NW_ADDR)) {
4248 wc.masks.nw_src = wc.masks.nw_dst = htonl(0);
4250 if (!(may_match & MAY_TP_ADDR)) {
4251 wc.masks.tp_src = wc.masks.tp_dst = htons(0);
4253 if (!(may_match & MAY_NW_PROTO)) {
4254 wc.masks.nw_proto = 0;
4256 if (!(may_match & MAY_IPVx)) {
4257 wc.masks.nw_tos = 0;
4258 wc.masks.nw_ttl = 0;
4260 if (!(may_match & MAY_ARP_SHA)) {
4261 memset(wc.masks.arp_sha, 0, ETH_ADDR_LEN);
4263 if (!(may_match & MAY_ARP_THA)) {
4264 memset(wc.masks.arp_tha, 0, ETH_ADDR_LEN);
4266 if (!(may_match & MAY_IPV6)) {
4267 wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any;
4268 wc.masks.ipv6_label = htonl(0);
4270 if (!(may_match & MAY_ND_TARGET)) {
4271 wc.masks.nd_target = in6addr_any;
4274 /* Log any changes. */
4275 if (!flow_wildcards_equal(&wc, &match->wc)) {
4276 bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
4277 char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL;
4280 match_zero_wildcarded_fields(match);
4283 char *post = match_to_string(match, OFP_DEFAULT_PRIORITY);
4284 VLOG_INFO("normalization changed ofp_match, details:");
4285 VLOG_INFO(" pre: %s", pre);
4286 VLOG_INFO("post: %s", post);
4293 /* "Normalizes" the wildcards in 'match'. That means:
4295 * 1. If the type of level N is known, then only the valid fields for that
4296 * level may be specified. For example, ARP does not have a TOS field,
4297 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
4298 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
4299 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
4302 * 2. If the type of level N is not known (or not understood by Open
4303 * vSwitch), then no fields at all for that level may be specified. For
4304 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
4305 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
4308 * If this function changes 'match', it logs a rate-limited informational
4311 ofputil_normalize_match(struct match *match)
4313 ofputil_normalize_match__(match, true);
4316 /* Same as ofputil_normalize_match() without the logging. Thus, this function
4317 * is suitable for a program's internal use, whereas ofputil_normalize_match()
4318 * sense for use on flows received from elsewhere (so that a bug in the program
4319 * that sent them can be reported and corrected). */
4321 ofputil_normalize_match_quiet(struct match *match)
4323 ofputil_normalize_match__(match, false);
4326 /* Parses a key or a key-value pair from '*stringp'.
4328 * On success: Stores the key into '*keyp'. Stores the value, if present, into
4329 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
4330 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
4331 * are substrings of '*stringp' created by replacing some of its bytes by null
4332 * terminators. Returns true.
4334 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
4335 * NULL and returns false. */
4337 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
4339 char *pos, *key, *value;
4343 pos += strspn(pos, ", \t\r\n");
4345 *keyp = *valuep = NULL;
4350 key_len = strcspn(pos, ":=(, \t\r\n");
4351 if (key[key_len] == ':' || key[key_len] == '=') {
4352 /* The value can be separated by a colon. */
4355 value = key + key_len + 1;
4356 value_len = strcspn(value, ", \t\r\n");
4357 pos = value + value_len + (value[value_len] != '\0');
4358 value[value_len] = '\0';
4359 } else if (key[key_len] == '(') {
4360 /* The value can be surrounded by balanced parentheses. The outermost
4361 * set of parentheses is removed. */
4365 value = key + key_len + 1;
4366 for (value_len = 0; level > 0; value_len++) {
4367 switch (value[value_len]) {
4381 value[value_len - 1] = '\0';
4382 pos = value + value_len;
4384 /* There might be no value at all. */
4385 value = key + key_len; /* Will become the empty string below. */
4386 pos = key + key_len + (key[key_len] != '\0');
4388 key[key_len] = '\0';
4396 /* Encode a dump ports request for 'port', the encoded message
4397 * will be for Open Flow version 'ofp_version'. Returns message
4398 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
4400 ofputil_encode_dump_ports_request(enum ofp_version ofp_version, int16_t port)
4402 struct ofpbuf *request;
4404 switch (ofp_version) {
4405 case OFP10_VERSION: {
4406 struct ofp10_port_stats_request *req;
4407 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0);
4408 req = ofpbuf_put_zeros(request, sizeof *req);
4409 req->port_no = htons(port);
4414 case OFP13_VERSION: {
4415 struct ofp11_port_stats_request *req;
4416 request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0);
4417 req = ofpbuf_put_zeros(request, sizeof *req);
4418 req->port_no = ofputil_port_to_ofp11(port);
4429 ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops,
4430 struct ofp10_port_stats *ps10)
4432 ps10->port_no = htons(ops->port_no);
4433 memset(ps10->pad, 0, sizeof ps10->pad);
4434 put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets));
4435 put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets));
4436 put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes));
4437 put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes));
4438 put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped));
4439 put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped));
4440 put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors));
4441 put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors));
4442 put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors));
4443 put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors));
4444 put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors));
4445 put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions));
4449 ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops,
4450 struct ofp11_port_stats *ps11)
4452 ps11->port_no = ofputil_port_to_ofp11(ops->port_no);
4453 memset(ps11->pad, 0, sizeof ps11->pad);
4454 ps11->rx_packets = htonll(ops->stats.rx_packets);
4455 ps11->tx_packets = htonll(ops->stats.tx_packets);
4456 ps11->rx_bytes = htonll(ops->stats.rx_bytes);
4457 ps11->tx_bytes = htonll(ops->stats.tx_bytes);
4458 ps11->rx_dropped = htonll(ops->stats.rx_dropped);
4459 ps11->tx_dropped = htonll(ops->stats.tx_dropped);
4460 ps11->rx_errors = htonll(ops->stats.rx_errors);
4461 ps11->tx_errors = htonll(ops->stats.tx_errors);
4462 ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors);
4463 ps11->rx_over_err = htonll(ops->stats.rx_over_errors);
4464 ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors);
4465 ps11->collisions = htonll(ops->stats.collisions);
4469 ofputil_port_stats_to_ofp13(const struct ofputil_port_stats *ops,
4470 struct ofp13_port_stats *ps13)
4472 ofputil_port_stats_to_ofp11(ops, &ps13->ps);
4474 /* OF 1.3 adds duration fields */
4475 /* FIXME: Need to implement port alive duration (sec + nsec) */
4476 ps13->duration_sec = htonl(~0);
4477 ps13->duration_nsec = htonl(~0);
4481 /* Encode a ports stat for 'ops' and append it to 'replies'. */
4483 ofputil_append_port_stat(struct list *replies,
4484 const struct ofputil_port_stats *ops)
4486 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
4487 struct ofp_header *oh = msg->data;
4489 switch ((enum ofp_version)oh->version) {
4490 case OFP13_VERSION: {
4491 struct ofp13_port_stats *reply = ofpmp_append(replies, sizeof *reply);
4492 ofputil_port_stats_to_ofp13(ops, reply);
4496 case OFP11_VERSION: {
4497 struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply);
4498 ofputil_port_stats_to_ofp11(ops, reply);
4502 case OFP10_VERSION: {
4503 struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply);
4504 ofputil_port_stats_to_ofp10(ops, reply);
4514 ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops,
4515 const struct ofp10_port_stats *ps10)
4517 memset(ops, 0, sizeof *ops);
4519 ops->port_no = ntohs(ps10->port_no);
4520 ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets));
4521 ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets));
4522 ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes));
4523 ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes));
4524 ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped));
4525 ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped));
4526 ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors));
4527 ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors));
4528 ops->stats.rx_frame_errors =
4529 ntohll(get_32aligned_be64(&ps10->rx_frame_err));
4530 ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err));
4531 ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err));
4532 ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions));
4538 ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops,
4539 const struct ofp11_port_stats *ps11)
4543 memset(ops, 0, sizeof *ops);
4544 error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no);
4549 ops->stats.rx_packets = ntohll(ps11->rx_packets);
4550 ops->stats.tx_packets = ntohll(ps11->tx_packets);
4551 ops->stats.rx_bytes = ntohll(ps11->rx_bytes);
4552 ops->stats.tx_bytes = ntohll(ps11->tx_bytes);
4553 ops->stats.rx_dropped = ntohll(ps11->rx_dropped);
4554 ops->stats.tx_dropped = ntohll(ps11->tx_dropped);
4555 ops->stats.rx_errors = ntohll(ps11->rx_errors);
4556 ops->stats.tx_errors = ntohll(ps11->tx_errors);
4557 ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err);
4558 ops->stats.rx_over_errors = ntohll(ps11->rx_over_err);
4559 ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err);
4560 ops->stats.collisions = ntohll(ps11->collisions);
4566 ofputil_port_stats_from_ofp13(struct ofputil_port_stats *ops,
4567 const struct ofp13_port_stats *ps13)
4570 ofputil_port_stats_from_ofp11(ops, &ps13->ps);
4572 /* FIXME: Get ps13->duration_sec and ps13->duration_nsec,
4573 * Add to netdev_stats? */
4580 /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
4583 ofputil_count_port_stats(const struct ofp_header *oh)
4587 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4588 ofpraw_pull_assert(&b);
4590 BUILD_ASSERT(sizeof(struct ofp10_port_stats) ==
4591 sizeof(struct ofp11_port_stats));
4592 return b.size / sizeof(struct ofp10_port_stats);
4595 /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
4596 * ofputil_port_stats in 'ps'.
4598 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
4599 * message. Calling this function multiple times for a single 'msg' iterates
4600 * through the replies. The caller must initially leave 'msg''s layer pointers
4601 * null and not modify them between calls.
4603 * Returns 0 if successful, EOF if no replies were left in this 'msg',
4604 * otherwise a positive errno value. */
4606 ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg)
4612 ? ofpraw_decode(&raw, msg->l2)
4613 : ofpraw_pull(&raw, msg));
4620 } else if (raw == OFPRAW_OFPST13_PORT_REPLY) {
4621 const struct ofp13_port_stats *ps13;
4623 ps13 = ofpbuf_try_pull(msg, sizeof *ps13);
4627 return ofputil_port_stats_from_ofp13(ps, ps13);
4628 } else if (raw == OFPRAW_OFPST11_PORT_REPLY) {
4629 const struct ofp11_port_stats *ps11;
4631 ps11 = ofpbuf_try_pull(msg, sizeof *ps11);
4635 return ofputil_port_stats_from_ofp11(ps, ps11);
4636 } else if (raw == OFPRAW_OFPST10_PORT_REPLY) {
4637 const struct ofp10_port_stats *ps10;
4639 ps10 = ofpbuf_try_pull(msg, sizeof *ps10);
4643 return ofputil_port_stats_from_ofp10(ps, ps10);
4649 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %zu leftover "
4650 "bytes at end", msg->size);
4651 return OFPERR_OFPBRC_BAD_LEN;
4654 /* Parse a port status request message into a 16 bit OpenFlow 1.0
4655 * port number and stores the latter in '*ofp10_port'.
4656 * Returns 0 if successful, otherwise an OFPERR_* number. */
4658 ofputil_decode_port_stats_request(const struct ofp_header *request,
4659 uint16_t *ofp10_port)
4661 switch ((enum ofp_version)request->version) {
4664 case OFP11_VERSION: {
4665 const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request);
4666 return ofputil_port_from_ofp11(psr11->port_no, ofp10_port);
4669 case OFP10_VERSION: {
4670 const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request);
4671 *ofp10_port = ntohs(psr10->port_no);
4680 /* Parse a queue status request message into 'oqsr'.
4681 * Returns 0 if successful, otherwise an OFPERR_* number. */
4683 ofputil_decode_queue_stats_request(const struct ofp_header *request,
4684 struct ofputil_queue_stats_request *oqsr)
4686 switch ((enum ofp_version)request->version) {
4689 case OFP11_VERSION: {
4690 const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request);
4691 oqsr->queue_id = ntohl(qsr11->queue_id);
4692 return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no);
4695 case OFP10_VERSION: {
4696 const struct ofp10_queue_stats_request *qsr10 = ofpmsg_body(request);
4697 oqsr->queue_id = ntohl(qsr10->queue_id);
4698 oqsr->port_no = ntohs(qsr10->port_no);
4699 /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
4700 if (oqsr->port_no == OFPP_ALL) {
4701 oqsr->port_no = OFPP_ANY;
4711 /* Encode a queue statsrequest for 'oqsr', the encoded message
4712 * will be fore Open Flow version 'ofp_version'. Returns message
4713 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
4715 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
4716 const struct ofputil_queue_stats_request *oqsr)
4718 struct ofpbuf *request;
4720 switch (ofp_version) {
4723 case OFP13_VERSION: {
4724 struct ofp11_queue_stats_request *req;
4725 request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0);
4726 req = ofpbuf_put_zeros(request, sizeof *req);
4727 req->port_no = ofputil_port_to_ofp11(oqsr->port_no);
4728 req->queue_id = htonl(oqsr->queue_id);
4731 case OFP10_VERSION: {
4732 struct ofp10_queue_stats_request *req;
4733 request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0);
4734 req = ofpbuf_put_zeros(request, sizeof *req);
4735 /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
4736 req->port_no = htons(oqsr->port_no == OFPP_ANY
4737 ? OFPP_ALL : oqsr->port_no);
4738 req->queue_id = htonl(oqsr->queue_id);
4748 /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
4751 ofputil_count_queue_stats(const struct ofp_header *oh)
4755 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4756 ofpraw_pull_assert(&b);
4758 BUILD_ASSERT(sizeof(struct ofp10_queue_stats) ==
4759 sizeof(struct ofp11_queue_stats));
4760 return b.size / sizeof(struct ofp10_queue_stats);
4764 ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs,
4765 const struct ofp10_queue_stats *qs10)
4767 oqs->port_no = ntohs(qs10->port_no);
4768 oqs->queue_id = ntohl(qs10->queue_id);
4769 oqs->stats.tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes));
4770 oqs->stats.tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets));
4771 oqs->stats.tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors));
4777 ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs,
4778 const struct ofp11_queue_stats *qs11)
4782 error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no);
4787 oqs->queue_id = ntohl(qs11->queue_id);
4788 oqs->stats.tx_bytes = ntohll(qs11->tx_bytes);
4789 oqs->stats.tx_packets = ntohll(qs11->tx_packets);
4790 oqs->stats.tx_errors = ntohll(qs11->tx_errors);
4796 ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats *oqs,
4797 const struct ofp13_queue_stats *qs13)
4800 = ofputil_queue_stats_from_ofp11(oqs, &qs13->qs);
4802 /* FIXME: Get qs13->duration_sec and qs13->duration_nsec,
4803 * Add to netdev_queue_stats? */
4809 /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
4810 * ofputil_queue_stats in 'qs'.
4812 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
4813 * message. Calling this function multiple times for a single 'msg' iterates
4814 * through the replies. The caller must initially leave 'msg''s layer pointers
4815 * null and not modify them between calls.
4817 * Returns 0 if successful, EOF if no replies were left in this 'msg',
4818 * otherwise a positive errno value. */
4820 ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg)
4826 ? ofpraw_decode(&raw, msg->l2)
4827 : ofpraw_pull(&raw, msg));
4834 } else if (raw == OFPRAW_OFPST13_QUEUE_REPLY) {
4835 const struct ofp13_queue_stats *qs13;
4837 qs13 = ofpbuf_try_pull(msg, sizeof *qs13);
4841 return ofputil_queue_stats_from_ofp13(qs, qs13);
4842 } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) {
4843 const struct ofp11_queue_stats *qs11;
4845 qs11 = ofpbuf_try_pull(msg, sizeof *qs11);
4849 return ofputil_queue_stats_from_ofp11(qs, qs11);
4850 } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) {
4851 const struct ofp10_queue_stats *qs10;
4853 qs10 = ofpbuf_try_pull(msg, sizeof *qs10);
4857 return ofputil_queue_stats_from_ofp10(qs, qs10);
4863 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %zu leftover "
4864 "bytes at end", msg->size);
4865 return OFPERR_OFPBRC_BAD_LEN;
4869 ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs,
4870 struct ofp10_queue_stats *qs10)
4872 qs10->port_no = htons(oqs->port_no);
4873 memset(qs10->pad, 0, sizeof qs10->pad);
4874 qs10->queue_id = htonl(oqs->queue_id);
4875 put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->stats.tx_bytes));
4876 put_32aligned_be64(&qs10->tx_packets, htonll(oqs->stats.tx_packets));
4877 put_32aligned_be64(&qs10->tx_errors, htonll(oqs->stats.tx_errors));
4881 ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs,
4882 struct ofp11_queue_stats *qs11)
4884 qs11->port_no = ofputil_port_to_ofp11(oqs->port_no);
4885 qs11->queue_id = htonl(oqs->queue_id);
4886 qs11->tx_bytes = htonll(oqs->stats.tx_bytes);
4887 qs11->tx_packets = htonll(oqs->stats.tx_packets);
4888 qs11->tx_errors = htonll(oqs->stats.tx_errors);
4892 ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats *oqs,
4893 struct ofp13_queue_stats *qs13)
4895 ofputil_queue_stats_to_ofp11(oqs, &qs13->qs);
4896 /* OF 1.3 adds duration fields */
4897 /* FIXME: Need to implement queue alive duration (sec + nsec) */
4898 qs13->duration_sec = htonl(~0);
4899 qs13->duration_nsec = htonl(~0);
4902 /* Encode a queue stat for 'oqs' and append it to 'replies'. */
4904 ofputil_append_queue_stat(struct list *replies,
4905 const struct ofputil_queue_stats *oqs)
4907 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
4908 struct ofp_header *oh = msg->data;
4910 switch ((enum ofp_version)oh->version) {
4911 case OFP13_VERSION: {
4912 struct ofp13_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
4913 ofputil_queue_stats_to_ofp13(oqs, reply);
4918 case OFP11_VERSION: {
4919 struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
4920 ofputil_queue_stats_to_ofp11(oqs, reply);
4924 case OFP10_VERSION: {
4925 struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
4926 ofputil_queue_stats_to_ofp10(oqs, reply);