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 ofputil_protocol that is initially in effect on an OpenFlow
600 * connection that has negotiated the given 'version'. 'version' should
601 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
602 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
603 * outside the valid range. */
604 enum ofputil_protocol
605 ofputil_protocol_from_ofp_version(enum ofp_version version)
609 return OFPUTIL_P_OF10_STD;
611 return OFPUTIL_P_OF12_OXM;
613 return OFPUTIL_P_OF13_OXM;
620 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
621 * etc.) that corresponds to 'protocol'. */
623 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
626 case OFPUTIL_P_OF10_STD:
627 case OFPUTIL_P_OF10_STD_TID:
628 case OFPUTIL_P_OF10_NXM:
629 case OFPUTIL_P_OF10_NXM_TID:
630 return OFP10_VERSION;
631 case OFPUTIL_P_OF12_OXM:
632 return OFP12_VERSION;
633 case OFPUTIL_P_OF13_OXM:
634 return OFP13_VERSION;
640 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
643 ofputil_protocol_is_valid(enum ofputil_protocol protocol)
645 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
648 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
649 * extension turned on or off if 'enable' is true or false, respectively.
651 * This extension is only useful for protocols whose "standard" version does
652 * not allow specific tables to be modified. In particular, this is true of
653 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
654 * specifies a table ID and so there is no need for such an extension. When
655 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
656 * extension, this function just returns its 'protocol' argument unchanged
657 * regardless of the value of 'enable'. */
658 enum ofputil_protocol
659 ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
662 case OFPUTIL_P_OF10_STD:
663 case OFPUTIL_P_OF10_STD_TID:
664 return enable ? OFPUTIL_P_OF10_STD_TID : OFPUTIL_P_OF10_STD;
666 case OFPUTIL_P_OF10_NXM:
667 case OFPUTIL_P_OF10_NXM_TID:
668 return enable ? OFPUTIL_P_OF10_NXM_TID : OFPUTIL_P_OF10_NXM;
670 case OFPUTIL_P_OF12_OXM:
671 return OFPUTIL_P_OF12_OXM;
673 case OFPUTIL_P_OF13_OXM:
674 return OFPUTIL_P_OF13_OXM;
681 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
682 * some extension to a standard protocol version, the return value is the
683 * standard version of that protocol without any extension. If 'protocol' is a
684 * standard protocol version, returns 'protocol' unchanged. */
685 enum ofputil_protocol
686 ofputil_protocol_to_base(enum ofputil_protocol protocol)
688 return ofputil_protocol_set_tid(protocol, false);
691 /* Returns 'new_base' with any extensions taken from 'cur'. */
692 enum ofputil_protocol
693 ofputil_protocol_set_base(enum ofputil_protocol cur,
694 enum ofputil_protocol new_base)
696 bool tid = (cur & OFPUTIL_P_TID) != 0;
699 case OFPUTIL_P_OF10_STD:
700 case OFPUTIL_P_OF10_STD_TID:
701 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD, tid);
703 case OFPUTIL_P_OF10_NXM:
704 case OFPUTIL_P_OF10_NXM_TID:
705 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM, tid);
707 case OFPUTIL_P_OF12_OXM:
708 return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM, tid);
710 case OFPUTIL_P_OF13_OXM:
711 return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM, tid);
718 /* Returns a string form of 'protocol', if a simple form exists (that is, if
719 * 'protocol' is either a single protocol or it is a combination of protocols
720 * that have a single abbreviation). Otherwise, returns NULL. */
722 ofputil_protocol_to_string(enum ofputil_protocol protocol)
724 const struct proto_abbrev *p;
726 /* Use a "switch" statement for single-bit names so that we get a compiler
727 * warning if we forget any. */
729 case OFPUTIL_P_OF10_NXM:
730 return "NXM-table_id";
732 case OFPUTIL_P_OF10_NXM_TID:
733 return "NXM+table_id";
735 case OFPUTIL_P_OF10_STD:
736 return "OpenFlow10-table_id";
738 case OFPUTIL_P_OF10_STD_TID:
739 return "OpenFlow10+table_id";
741 case OFPUTIL_P_OF12_OXM:
742 return "OXM-OpenFlow12";
744 case OFPUTIL_P_OF13_OXM:
745 return "OXM-OpenFlow13";
748 /* Check abbreviations. */
749 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
750 if (protocol == p->protocol) {
758 /* Returns a string that represents 'protocols'. The return value might be a
759 * comma-separated list if 'protocols' doesn't have a simple name. The return
760 * value is "none" if 'protocols' is 0.
762 * The caller must free the returned string (with free()). */
764 ofputil_protocols_to_string(enum ofputil_protocol protocols)
768 assert(!(protocols & ~OFPUTIL_P_ANY));
769 if (protocols == 0) {
770 return xstrdup("none");
775 const struct proto_abbrev *p;
779 ds_put_char(&s, ',');
782 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
783 if ((protocols & p->protocol) == p->protocol) {
784 ds_put_cstr(&s, p->name);
785 protocols &= ~p->protocol;
790 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
791 enum ofputil_protocol bit = 1u << i;
793 if (protocols & bit) {
794 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
803 return ds_steal_cstr(&s);
806 static enum ofputil_protocol
807 ofputil_protocol_from_string__(const char *s, size_t n)
809 const struct proto_abbrev *p;
812 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
813 enum ofputil_protocol bit = 1u << i;
814 const char *name = ofputil_protocol_to_string(bit);
816 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
821 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
822 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
830 /* Returns the nonempty set of protocols represented by 's', which can be a
831 * single protocol name or abbreviation or a comma-separated list of them.
833 * Aborts the program with an error message if 's' is invalid. */
834 enum ofputil_protocol
835 ofputil_protocols_from_string(const char *s)
837 const char *orig_s = s;
838 enum ofputil_protocol protocols;
842 enum ofputil_protocol p;
851 p = ofputil_protocol_from_string__(s, n);
853 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
861 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
867 ofputil_version_from_string(const char *s)
869 if (!strcasecmp(s, "OpenFlow10")) {
870 return OFP10_VERSION;
872 if (!strcasecmp(s, "OpenFlow11")) {
873 return OFP11_VERSION;
875 if (!strcasecmp(s, "OpenFlow12")) {
876 return OFP12_VERSION;
878 if (!strcasecmp(s, "OpenFlow13")) {
879 return OFP13_VERSION;
887 return isspace(c) || c == ',';
891 ofputil_versions_from_string(const char *s)
901 if (is_delimiter(s[i])) {
906 while (s[i + j] && !is_delimiter(s[i + j])) {
909 key = xmemdup0(s + i, j);
910 version = ofputil_version_from_string(key);
912 VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key);
915 bitmap |= 1u << version;
923 ofputil_versions_from_strings(char ** const s, size_t count)
928 int version = ofputil_version_from_string(s[count]);
930 VLOG_WARN("Unknown OpenFlow version: \"%s\"", s[count]);
932 bitmap |= 1u << version;
940 ofputil_version_to_string(enum ofp_version ofp_version)
942 switch (ofp_version) {
957 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
959 switch (packet_in_format) {
960 case NXPIF_OPENFLOW10:
969 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
971 switch (packet_in_format) {
972 case NXPIF_OPENFLOW10:
982 ofputil_packet_in_format_from_string(const char *s)
984 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
985 : !strcmp(s, "nxm") ? NXPIF_NXM
990 regs_fully_wildcarded(const struct flow_wildcards *wc)
994 for (i = 0; i < FLOW_N_REGS; i++) {
995 if (wc->masks.regs[i] != 0) {
1003 tun_parms_fully_wildcarded(const struct flow_wildcards *wc)
1005 return (!wc->masks.tunnel.ip_src &&
1006 !wc->masks.tunnel.ip_dst &&
1007 !wc->masks.tunnel.ip_ttl &&
1008 !wc->masks.tunnel.ip_tos &&
1009 !wc->masks.tunnel.flags);
1012 /* Returns a bit-mask of ofputil_protocols that can be used for sending 'match'
1013 * to a switch (e.g. to add or remove a flow). Only NXM can handle tunnel IDs,
1014 * registers, or fixing the Ethernet multicast bit. Otherwise, it's better to
1015 * use OpenFlow 1.0 protocol for backward compatibility. */
1016 enum ofputil_protocol
1017 ofputil_usable_protocols(const struct match *match)
1019 const struct flow_wildcards *wc = &match->wc;
1021 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 18);
1023 /* tunnel params other than tun_id can't be sent in a flow_mod */
1024 if (!tun_parms_fully_wildcarded(wc)) {
1025 return OFPUTIL_P_NONE;
1028 /* NXM, OXM, and OF1.1 support bitwise matching on ethernet addresses. */
1029 if (!eth_mask_is_exact(wc->masks.dl_src)
1030 && !eth_addr_is_zero(wc->masks.dl_src)) {
1031 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1032 | OFPUTIL_P_OF13_OXM;
1034 if (!eth_mask_is_exact(wc->masks.dl_dst)
1035 && !eth_addr_is_zero(wc->masks.dl_dst)) {
1036 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1037 | OFPUTIL_P_OF13_OXM;
1040 /* NXM, OXM, and OF1.1+ support matching metadata. */
1041 if (wc->masks.metadata != htonll(0)) {
1042 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1043 | OFPUTIL_P_OF13_OXM;
1046 /* NXM and OXM support matching ARP hardware addresses. */
1047 if (!eth_addr_is_zero(wc->masks.arp_sha) ||
1048 !eth_addr_is_zero(wc->masks.arp_tha)) {
1049 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1050 | OFPUTIL_P_OF13_OXM;
1053 /* NXM and OXM support matching IPv6 traffic. */
1054 if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
1055 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1056 | OFPUTIL_P_OF13_OXM;
1059 /* NXM and OXM support matching registers. */
1060 if (!regs_fully_wildcarded(wc)) {
1061 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1062 | OFPUTIL_P_OF13_OXM;
1065 /* NXM and OXM support matching tun_id. */
1066 if (wc->masks.tunnel.tun_id != htonll(0)) {
1067 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1068 | OFPUTIL_P_OF13_OXM;
1071 /* NXM and OXM support matching fragments. */
1072 if (wc->masks.nw_frag) {
1073 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1074 | OFPUTIL_P_OF13_OXM;
1077 /* NXM and OXM support matching IPv6 flow label. */
1078 if (wc->masks.ipv6_label) {
1079 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1080 | OFPUTIL_P_OF13_OXM;
1083 /* NXM and OXM support matching IP ECN bits. */
1084 if (wc->masks.nw_tos & IP_ECN_MASK) {
1085 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1086 | OFPUTIL_P_OF13_OXM;
1089 /* NXM and OXM support matching IP TTL/hop limit. */
1090 if (wc->masks.nw_ttl) {
1091 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1092 | OFPUTIL_P_OF13_OXM;
1095 /* NXM and OXM support non-CIDR IPv4 address masks. */
1096 if (!ip_is_cidr(wc->masks.nw_src) || !ip_is_cidr(wc->masks.nw_dst)) {
1097 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1098 | OFPUTIL_P_OF13_OXM;
1101 /* NXM and OXM support bitwise matching on transport port. */
1102 if ((wc->masks.tp_src && wc->masks.tp_src != htons(UINT16_MAX)) ||
1103 (wc->masks.tp_dst && wc->masks.tp_dst != htons(UINT16_MAX))) {
1104 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1105 | OFPUTIL_P_OF13_OXM;
1108 /* Other formats can express this rule. */
1109 return OFPUTIL_P_ANY;
1113 ofputil_format_version(struct ds *msg, enum ofp_version version)
1115 ds_put_format(msg, "0x%02x", version);
1119 ofputil_format_version_name(struct ds *msg, enum ofp_version version)
1121 ds_put_cstr(msg, ofputil_version_to_string(version));
1125 ofputil_format_version_bitmap__(struct ds *msg, uint32_t bitmap,
1126 void (*format_version)(struct ds *msg,
1130 format_version(msg, raw_ctz(bitmap));
1131 bitmap = zero_rightmost_1bit(bitmap);
1133 ds_put_cstr(msg, ", ");
1139 ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap)
1141 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version);
1145 ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap)
1147 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version_name);
1151 ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header *oheh,
1152 uint32_t *allowed_versionsp)
1154 uint16_t bitmap_len = ntohs(oheh->length) - sizeof *oheh;
1155 const ovs_be32 *bitmap = (const ovs_be32 *) (oheh + 1);
1156 uint32_t allowed_versions;
1158 if (!bitmap_len || bitmap_len % sizeof *bitmap) {
1162 /* Only use the first 32-bit element of the bitmap as that is all the
1163 * current implementation supports. Subsequent elements are ignored which
1164 * should have no effect on session negotiation until Open vSwtich supports
1165 * wire-protocol versions greater than 31.
1167 allowed_versions = ntohl(bitmap[0]);
1169 if (allowed_versions & 1) {
1170 /* There's no OpenFlow version 0. */
1171 VLOG_WARN_RL(&bad_ofmsg_rl, "peer claims to support invalid OpenFlow "
1173 allowed_versions &= ~1u;
1176 if (!allowed_versions) {
1177 VLOG_WARN_RL(&bad_ofmsg_rl, "peer does not support any OpenFlow "
1178 "version (between 0x01 and 0x1f)");
1182 *allowed_versionsp = allowed_versions;
1187 version_bitmap_from_version(uint8_t ofp_version)
1189 return ((ofp_version < 32 ? 1u << ofp_version : 0) - 1) << 1;
1192 /* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions'
1193 * the set of OpenFlow versions for which 'oh' announces support.
1195 * Because of how OpenFlow defines OFPT_HELLO messages, this function is always
1196 * successful, and thus '*allowed_versions' is always initialized. However, it
1197 * returns false if 'oh' contains some data that could not be fully understood,
1198 * true if 'oh' was completely parsed. */
1200 ofputil_decode_hello(const struct ofp_header *oh, uint32_t *allowed_versions)
1205 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
1206 ofpbuf_pull(&msg, sizeof *oh);
1208 *allowed_versions = version_bitmap_from_version(oh->version);
1210 const struct ofp_hello_elem_header *oheh;
1213 if (msg.size < sizeof *oheh) {
1218 len = ntohs(oheh->length);
1219 if (len < sizeof *oheh || !ofpbuf_try_pull(&msg, ROUND_UP(len, 8))) {
1223 if (oheh->type != htons(OFPHET_VERSIONBITMAP)
1224 || !ofputil_decode_hello_bitmap(oheh, allowed_versions)) {
1232 /* Returns true if 'allowed_versions' needs to be accompanied by a version
1233 * bitmap to be correctly expressed in an OFPT_HELLO message. */
1235 should_send_version_bitmap(uint32_t allowed_versions)
1237 return !is_pow2((allowed_versions >> 1) + 1);
1240 /* Create an OFPT_HELLO message that expresses support for the OpenFlow
1241 * versions in the 'allowed_versions' bitmaps and returns the message. */
1243 ofputil_encode_hello(uint32_t allowed_versions)
1245 enum ofp_version ofp_version;
1248 ofp_version = leftmost_1bit_idx(allowed_versions);
1249 msg = ofpraw_alloc(OFPRAW_OFPT_HELLO, ofp_version, 0);
1251 if (should_send_version_bitmap(allowed_versions)) {
1252 struct ofp_hello_elem_header *oheh;
1255 map_len = sizeof allowed_versions;
1256 oheh = ofpbuf_put_zeros(msg, ROUND_UP(map_len + sizeof *oheh, 8));
1257 oheh->type = htons(OFPHET_VERSIONBITMAP);
1258 oheh->length = htons(map_len + sizeof *oheh);
1259 *(ovs_be32 *)(oheh + 1) = htonl(allowed_versions);
1261 ofpmsg_update_length(msg);
1267 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1268 * protocol is 'current', at least partly transitions the protocol to 'want'.
1269 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1270 * connection if the switch processes the returned message correctly. (If
1271 * '*next != want' then the caller will have to iterate.)
1273 * If 'current == want', or if it is not possible to transition from 'current'
1274 * to 'want' (because, for example, 'current' and 'want' use different OpenFlow
1275 * protocol versions), returns NULL and stores 'current' in '*next'. */
1277 ofputil_encode_set_protocol(enum ofputil_protocol current,
1278 enum ofputil_protocol want,
1279 enum ofputil_protocol *next)
1281 enum ofp_version cur_version, want_version;
1282 enum ofputil_protocol cur_base, want_base;
1283 bool cur_tid, want_tid;
1285 cur_version = ofputil_protocol_to_ofp_version(current);
1286 want_version = ofputil_protocol_to_ofp_version(want);
1287 if (cur_version != want_version) {
1292 cur_base = ofputil_protocol_to_base(current);
1293 want_base = ofputil_protocol_to_base(want);
1294 if (cur_base != want_base) {
1295 *next = ofputil_protocol_set_base(current, want_base);
1297 switch (want_base) {
1298 case OFPUTIL_P_OF10_NXM:
1299 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1301 case OFPUTIL_P_OF10_STD:
1302 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1304 case OFPUTIL_P_OF12_OXM:
1305 case OFPUTIL_P_OF13_OXM:
1306 /* There are only one of each OpenFlow 1.2+ protocols and we already
1307 * verified above that we're not trying to change versions. */
1310 case OFPUTIL_P_OF10_STD_TID:
1311 case OFPUTIL_P_OF10_NXM_TID:
1316 cur_tid = (current & OFPUTIL_P_TID) != 0;
1317 want_tid = (want & OFPUTIL_P_TID) != 0;
1318 if (cur_tid != want_tid) {
1319 *next = ofputil_protocol_set_tid(current, want_tid);
1320 return ofputil_make_flow_mod_table_id(want_tid);
1323 assert(current == want);
1329 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1330 * format to 'nxff'. */
1332 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1334 struct nx_set_flow_format *sff;
1337 assert(ofputil_nx_flow_format_is_valid(nxff));
1339 msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1340 sff = ofpbuf_put_zeros(msg, sizeof *sff);
1341 sff->format = htonl(nxff);
1346 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1348 enum ofputil_protocol
1349 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1351 switch (flow_format) {
1352 case NXFF_OPENFLOW10:
1353 return OFPUTIL_P_OF10_STD;
1356 return OFPUTIL_P_OF10_NXM;
1363 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1365 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1367 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1370 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1373 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1375 switch (flow_format) {
1376 case NXFF_OPENFLOW10:
1377 return "openflow10";
1386 ofputil_make_set_packet_in_format(enum ofp_version ofp_version,
1387 enum nx_packet_in_format packet_in_format)
1389 struct nx_set_packet_in_format *spif;
1392 msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, ofp_version, 0);
1393 spif = ofpbuf_put_zeros(msg, sizeof *spif);
1394 spif->format = htonl(packet_in_format);
1399 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1400 * extension on or off (according to 'flow_mod_table_id'). */
1402 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1404 struct nx_flow_mod_table_id *nfmti;
1407 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1408 nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
1409 nfmti->set = flow_mod_table_id;
1413 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1414 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1417 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1418 * The caller must initialize 'ofpacts' and retains ownership of it.
1419 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1421 * Does not validate the flow_mod actions. The caller should do that, with
1422 * ofpacts_check(). */
1424 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1425 const struct ofp_header *oh,
1426 enum ofputil_protocol protocol,
1427 struct ofpbuf *ofpacts)
1433 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1434 raw = ofpraw_pull_assert(&b);
1435 if (raw == OFPRAW_OFPT11_FLOW_MOD) {
1436 /* Standard OpenFlow 1.1 flow_mod. */
1437 const struct ofp11_flow_mod *ofm;
1440 ofm = ofpbuf_pull(&b, sizeof *ofm);
1442 error = ofputil_pull_ofp11_match(&b, &fm->match, NULL);
1447 error = ofpacts_pull_openflow11_instructions(&b, b.size, ofpacts);
1452 /* Translate the message. */
1453 fm->priority = ntohs(ofm->priority);
1454 if (ofm->command == OFPFC_ADD) {
1455 fm->cookie = htonll(0);
1456 fm->cookie_mask = htonll(0);
1457 fm->new_cookie = ofm->cookie;
1459 fm->cookie = ofm->cookie;
1460 fm->cookie_mask = ofm->cookie_mask;
1461 fm->new_cookie = htonll(UINT64_MAX);
1463 fm->command = ofm->command;
1464 fm->table_id = ofm->table_id;
1465 fm->idle_timeout = ntohs(ofm->idle_timeout);
1466 fm->hard_timeout = ntohs(ofm->hard_timeout);
1467 fm->buffer_id = ntohl(ofm->buffer_id);
1468 error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
1472 if ((ofm->command == OFPFC_DELETE
1473 || ofm->command == OFPFC_DELETE_STRICT)
1474 && ofm->out_group != htonl(OFPG_ANY)) {
1475 return OFPERR_OFPFMFC_UNKNOWN;
1477 fm->flags = ntohs(ofm->flags);
1479 if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1480 /* Standard OpenFlow 1.0 flow_mod. */
1481 const struct ofp10_flow_mod *ofm;
1484 /* Get the ofp10_flow_mod. */
1485 ofm = ofpbuf_pull(&b, sizeof *ofm);
1487 /* Translate the rule. */
1488 ofputil_match_from_ofp10_match(&ofm->match, &fm->match);
1489 ofputil_normalize_match(&fm->match);
1491 /* Now get the actions. */
1492 error = ofpacts_pull_openflow10(&b, b.size, ofpacts);
1497 /* OpenFlow 1.0 says that exact-match rules have to have the
1498 * highest possible priority. */
1499 fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL)
1500 ? ntohs(ofm->priority)
1503 /* Translate the message. */
1504 command = ntohs(ofm->command);
1505 fm->cookie = htonll(0);
1506 fm->cookie_mask = htonll(0);
1507 fm->new_cookie = ofm->cookie;
1508 fm->idle_timeout = ntohs(ofm->idle_timeout);
1509 fm->hard_timeout = ntohs(ofm->hard_timeout);
1510 fm->buffer_id = ntohl(ofm->buffer_id);
1511 fm->out_port = ntohs(ofm->out_port);
1512 fm->flags = ntohs(ofm->flags);
1513 } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1514 /* Nicira extended flow_mod. */
1515 const struct nx_flow_mod *nfm;
1518 /* Dissect the message. */
1519 nfm = ofpbuf_pull(&b, sizeof *nfm);
1520 error = nx_pull_match(&b, ntohs(nfm->match_len),
1521 &fm->match, &fm->cookie, &fm->cookie_mask);
1525 error = ofpacts_pull_openflow10(&b, b.size, ofpacts);
1530 /* Translate the message. */
1531 command = ntohs(nfm->command);
1532 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1533 /* Flow additions may only set a new cookie, not match an
1534 * existing cookie. */
1535 return OFPERR_NXBRC_NXM_INVALID;
1537 fm->priority = ntohs(nfm->priority);
1538 fm->new_cookie = nfm->cookie;
1539 fm->idle_timeout = ntohs(nfm->idle_timeout);
1540 fm->hard_timeout = ntohs(nfm->hard_timeout);
1541 fm->buffer_id = ntohl(nfm->buffer_id);
1542 fm->out_port = ntohs(nfm->out_port);
1543 fm->flags = ntohs(nfm->flags);
1548 if (fm->flags & OFPFF10_EMERG) {
1549 /* We do not support the OpenFlow 1.0 emergency flow cache, which
1550 * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
1551 * There is no good error code, so just state that the flow table
1553 * Moreover, OFPFF10_EMERG overlaps with OFPFF12_RESET_COUNTS,
1554 * so this check must be here */
1555 return OFPERR_OFPFMFC_TABLE_FULL;
1558 if (protocol & OFPUTIL_P_TID) {
1559 fm->command = command & 0xff;
1560 fm->table_id = command >> 8;
1562 fm->command = command;
1563 fm->table_id = 0xff;
1567 fm->ofpacts = ofpacts->data;
1568 fm->ofpacts_len = ofpacts->size;
1574 ofputil_tid_command(const struct ofputil_flow_mod *fm,
1575 enum ofputil_protocol protocol)
1577 return htons(protocol & OFPUTIL_P_TID
1578 ? (fm->command & 0xff) | (fm->table_id << 8)
1582 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
1583 * 'protocol' and returns the message. */
1585 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
1586 enum ofputil_protocol protocol)
1591 case OFPUTIL_P_OF12_OXM:
1592 case OFPUTIL_P_OF13_OXM: {
1593 struct ofp11_flow_mod *ofm;
1595 msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD,
1596 ofputil_protocol_to_ofp_version(protocol),
1597 NXM_TYPICAL_LEN + fm->ofpacts_len);
1598 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
1599 if (fm->command == OFPFC_ADD) {
1600 ofm->cookie = fm->new_cookie;
1602 ofm->cookie = fm->cookie;
1604 ofm->cookie_mask = fm->cookie_mask;
1605 ofm->table_id = fm->table_id;
1606 ofm->command = fm->command;
1607 ofm->idle_timeout = htons(fm->idle_timeout);
1608 ofm->hard_timeout = htons(fm->hard_timeout);
1609 ofm->priority = htons(fm->priority);
1610 ofm->buffer_id = htonl(fm->buffer_id);
1611 ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
1612 ofm->out_group = htonl(OFPG11_ANY);
1613 ofm->flags = htons(fm->flags);
1614 oxm_put_match(msg, &fm->match);
1615 ofpacts_put_openflow11_instructions(fm->ofpacts, fm->ofpacts_len, msg);
1619 case OFPUTIL_P_OF10_STD:
1620 case OFPUTIL_P_OF10_STD_TID: {
1621 struct ofp10_flow_mod *ofm;
1623 msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
1625 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
1626 ofputil_match_to_ofp10_match(&fm->match, &ofm->match);
1627 ofm->cookie = fm->new_cookie;
1628 ofm->command = ofputil_tid_command(fm, protocol);
1629 ofm->idle_timeout = htons(fm->idle_timeout);
1630 ofm->hard_timeout = htons(fm->hard_timeout);
1631 ofm->priority = htons(fm->priority);
1632 ofm->buffer_id = htonl(fm->buffer_id);
1633 ofm->out_port = htons(fm->out_port);
1634 ofm->flags = htons(fm->flags);
1635 ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
1639 case OFPUTIL_P_OF10_NXM:
1640 case OFPUTIL_P_OF10_NXM_TID: {
1641 struct nx_flow_mod *nfm;
1644 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
1645 NXM_TYPICAL_LEN + fm->ofpacts_len);
1646 nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
1647 nfm->command = ofputil_tid_command(fm, protocol);
1648 nfm->cookie = fm->new_cookie;
1649 match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask);
1651 nfm->idle_timeout = htons(fm->idle_timeout);
1652 nfm->hard_timeout = htons(fm->hard_timeout);
1653 nfm->priority = htons(fm->priority);
1654 nfm->buffer_id = htonl(fm->buffer_id);
1655 nfm->out_port = htons(fm->out_port);
1656 nfm->flags = htons(fm->flags);
1657 nfm->match_len = htons(match_len);
1658 ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
1666 ofpmsg_update_length(msg);
1670 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1671 * send all of the 'n_fm's flow table modification requests in 'fms', and a
1672 * 0-bit for each protocol that is inadequate.
1674 * (The return value will have at least one 1-bit.) */
1675 enum ofputil_protocol
1676 ofputil_flow_mod_usable_protocols(const struct ofputil_flow_mod *fms,
1679 enum ofputil_protocol usable_protocols;
1682 usable_protocols = OFPUTIL_P_ANY;
1683 for (i = 0; i < n_fms; i++) {
1684 const struct ofputil_flow_mod *fm = &fms[i];
1686 usable_protocols &= ofputil_usable_protocols(&fm->match);
1687 if (fm->table_id != 0xff) {
1688 usable_protocols &= OFPUTIL_P_TID;
1691 /* Matching of the cookie is only supported through NXM or OF1.1+. */
1692 if (fm->cookie_mask != htonll(0)) {
1693 usable_protocols &= OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1694 | OFPUTIL_P_OF13_OXM;
1698 return usable_protocols;
1702 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr,
1703 const struct ofp10_flow_stats_request *ofsr,
1706 fsr->aggregate = aggregate;
1707 ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match);
1708 fsr->out_port = ntohs(ofsr->out_port);
1709 fsr->table_id = ofsr->table_id;
1710 fsr->cookie = fsr->cookie_mask = htonll(0);
1716 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr,
1717 struct ofpbuf *b, bool aggregate)
1719 const struct ofp11_flow_stats_request *ofsr;
1722 ofsr = ofpbuf_pull(b, sizeof *ofsr);
1723 fsr->aggregate = aggregate;
1724 fsr->table_id = ofsr->table_id;
1725 error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port);
1729 if (ofsr->out_group != htonl(OFPG11_ANY)) {
1730 return OFPERR_OFPFMFC_UNKNOWN;
1732 fsr->cookie = ofsr->cookie;
1733 fsr->cookie_mask = ofsr->cookie_mask;
1734 error = ofputil_pull_ofp11_match(b, &fsr->match, NULL);
1743 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
1744 struct ofpbuf *b, bool aggregate)
1746 const struct nx_flow_stats_request *nfsr;
1749 nfsr = ofpbuf_pull(b, sizeof *nfsr);
1750 error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match,
1751 &fsr->cookie, &fsr->cookie_mask);
1756 return OFPERR_OFPBRC_BAD_LEN;
1759 fsr->aggregate = aggregate;
1760 fsr->out_port = ntohs(nfsr->out_port);
1761 fsr->table_id = nfsr->table_id;
1766 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1767 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1768 * successful, otherwise an OpenFlow error code. */
1770 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
1771 const struct ofp_header *oh)
1776 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1777 raw = ofpraw_pull_assert(&b);
1778 switch ((int) raw) {
1779 case OFPRAW_OFPST10_FLOW_REQUEST:
1780 return ofputil_decode_ofpst10_flow_request(fsr, b.data, false);
1782 case OFPRAW_OFPST10_AGGREGATE_REQUEST:
1783 return ofputil_decode_ofpst10_flow_request(fsr, b.data, true);
1785 case OFPRAW_OFPST11_FLOW_REQUEST:
1786 return ofputil_decode_ofpst11_flow_request(fsr, &b, false);
1788 case OFPRAW_OFPST11_AGGREGATE_REQUEST:
1789 return ofputil_decode_ofpst11_flow_request(fsr, &b, true);
1791 case OFPRAW_NXST_FLOW_REQUEST:
1792 return ofputil_decode_nxst_flow_request(fsr, &b, false);
1794 case OFPRAW_NXST_AGGREGATE_REQUEST:
1795 return ofputil_decode_nxst_flow_request(fsr, &b, true);
1798 /* Hey, the caller lied. */
1803 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1804 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1805 * 'protocol', and returns the message. */
1807 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
1808 enum ofputil_protocol protocol)
1814 case OFPUTIL_P_OF12_OXM:
1815 case OFPUTIL_P_OF13_OXM: {
1816 struct ofp11_flow_stats_request *ofsr;
1818 raw = (fsr->aggregate
1819 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
1820 : OFPRAW_OFPST11_FLOW_REQUEST);
1821 msg = ofpraw_alloc(raw, ofputil_protocol_to_ofp_version(protocol),
1823 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
1824 ofsr->table_id = fsr->table_id;
1825 ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port);
1826 ofsr->out_group = htonl(OFPG11_ANY);
1827 ofsr->cookie = fsr->cookie;
1828 ofsr->cookie_mask = fsr->cookie_mask;
1829 oxm_put_match(msg, &fsr->match);
1833 case OFPUTIL_P_OF10_STD:
1834 case OFPUTIL_P_OF10_STD_TID: {
1835 struct ofp10_flow_stats_request *ofsr;
1837 raw = (fsr->aggregate
1838 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
1839 : OFPRAW_OFPST10_FLOW_REQUEST);
1840 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
1841 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
1842 ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match);
1843 ofsr->table_id = fsr->table_id;
1844 ofsr->out_port = htons(fsr->out_port);
1848 case OFPUTIL_P_OF10_NXM:
1849 case OFPUTIL_P_OF10_NXM_TID: {
1850 struct nx_flow_stats_request *nfsr;
1853 raw = (fsr->aggregate
1854 ? OFPRAW_NXST_AGGREGATE_REQUEST
1855 : OFPRAW_NXST_FLOW_REQUEST);
1856 msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN);
1857 ofpbuf_put_zeros(msg, sizeof *nfsr);
1858 match_len = nx_put_match(msg, &fsr->match,
1859 fsr->cookie, fsr->cookie_mask);
1862 nfsr->out_port = htons(fsr->out_port);
1863 nfsr->match_len = htons(match_len);
1864 nfsr->table_id = fsr->table_id;
1875 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1876 * accurately encode 'fsr', and a 0-bit for each protocol that is inadequate.
1878 * (The return value will have at least one 1-bit.) */
1879 enum ofputil_protocol
1880 ofputil_flow_stats_request_usable_protocols(
1881 const struct ofputil_flow_stats_request *fsr)
1883 enum ofputil_protocol usable_protocols;
1885 usable_protocols = ofputil_usable_protocols(&fsr->match);
1886 if (fsr->cookie_mask != htonll(0)) {
1887 usable_protocols &= OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1888 | OFPUTIL_P_OF13_OXM;
1890 return usable_protocols;
1893 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1894 * ofputil_flow_stats in 'fs'.
1896 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1897 * OpenFlow message. Calling this function multiple times for a single 'msg'
1898 * iterates through the replies. The caller must initially leave 'msg''s layer
1899 * pointers null and not modify them between calls.
1901 * Most switches don't send the values needed to populate fs->idle_age and
1902 * fs->hard_age, so those members will usually be set to 0. If the switch from
1903 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
1904 * 'flow_age_extension' as true so that the contents of 'msg' determine the
1905 * 'idle_age' and 'hard_age' members in 'fs'.
1907 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
1908 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
1909 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
1911 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1912 * otherwise a positive errno value. */
1914 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1916 bool flow_age_extension,
1917 struct ofpbuf *ofpacts)
1923 ? ofpraw_decode(&raw, msg->l2)
1924 : ofpraw_pull(&raw, msg));
1931 } else if (raw == OFPRAW_OFPST11_FLOW_REPLY
1932 || raw == OFPRAW_OFPST13_FLOW_REPLY) {
1933 const struct ofp11_flow_stats *ofs;
1935 uint16_t padded_match_len;
1937 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1939 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1940 "bytes at end", msg->size);
1944 length = ntohs(ofs->length);
1945 if (length < sizeof *ofs) {
1946 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1947 "length %zu", length);
1951 if (ofputil_pull_ofp11_match(msg, &fs->match, &padded_match_len)) {
1952 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match");
1956 if (ofpacts_pull_openflow11_instructions(msg, length - sizeof *ofs -
1957 padded_match_len, ofpacts)) {
1958 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions");
1962 fs->priority = ntohs(ofs->priority);
1963 fs->table_id = ofs->table_id;
1964 fs->duration_sec = ntohl(ofs->duration_sec);
1965 fs->duration_nsec = ntohl(ofs->duration_nsec);
1966 fs->idle_timeout = ntohs(ofs->idle_timeout);
1967 fs->hard_timeout = ntohs(ofs->hard_timeout);
1968 fs->flags = (raw == OFPRAW_OFPST13_FLOW_REPLY) ? ntohs(ofs->flags) : 0;
1971 fs->cookie = ofs->cookie;
1972 fs->packet_count = ntohll(ofs->packet_count);
1973 fs->byte_count = ntohll(ofs->byte_count);
1974 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
1975 const struct ofp10_flow_stats *ofs;
1978 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1980 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1981 "bytes at end", msg->size);
1985 length = ntohs(ofs->length);
1986 if (length < sizeof *ofs) {
1987 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1988 "length %zu", length);
1992 if (ofpacts_pull_openflow10(msg, length - sizeof *ofs, ofpacts)) {
1996 fs->cookie = get_32aligned_be64(&ofs->cookie);
1997 ofputil_match_from_ofp10_match(&ofs->match, &fs->match);
1998 fs->priority = ntohs(ofs->priority);
1999 fs->table_id = ofs->table_id;
2000 fs->duration_sec = ntohl(ofs->duration_sec);
2001 fs->duration_nsec = ntohl(ofs->duration_nsec);
2002 fs->idle_timeout = ntohs(ofs->idle_timeout);
2003 fs->hard_timeout = ntohs(ofs->hard_timeout);
2006 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
2007 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
2009 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
2010 const struct nx_flow_stats *nfs;
2011 size_t match_len, actions_len, length;
2013 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
2015 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
2016 "bytes at end", msg->size);
2020 length = ntohs(nfs->length);
2021 match_len = ntohs(nfs->match_len);
2022 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
2023 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
2024 "claims invalid length %zu", match_len, length);
2027 if (nx_pull_match(msg, match_len, &fs->match, NULL, NULL)) {
2031 actions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
2032 if (ofpacts_pull_openflow10(msg, actions_len, ofpacts)) {
2036 fs->cookie = nfs->cookie;
2037 fs->table_id = nfs->table_id;
2038 fs->duration_sec = ntohl(nfs->duration_sec);
2039 fs->duration_nsec = ntohl(nfs->duration_nsec);
2040 fs->priority = ntohs(nfs->priority);
2041 fs->idle_timeout = ntohs(nfs->idle_timeout);
2042 fs->hard_timeout = ntohs(nfs->hard_timeout);
2045 if (flow_age_extension) {
2046 if (nfs->idle_age) {
2047 fs->idle_age = ntohs(nfs->idle_age) - 1;
2049 if (nfs->hard_age) {
2050 fs->hard_age = ntohs(nfs->hard_age) - 1;
2053 fs->packet_count = ntohll(nfs->packet_count);
2054 fs->byte_count = ntohll(nfs->byte_count);
2060 fs->ofpacts = ofpacts->data;
2061 fs->ofpacts_len = ofpacts->size;
2066 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
2068 * We use this in situations where OVS internally uses UINT64_MAX to mean
2069 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
2071 unknown_to_zero(uint64_t count)
2073 return count != UINT64_MAX ? count : 0;
2076 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
2077 * those already present in the list of ofpbufs in 'replies'. 'replies' should
2078 * have been initialized with ofputil_start_stats_reply(). */
2080 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
2081 struct list *replies)
2083 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
2084 size_t start_ofs = reply->size;
2087 ofpraw_decode_partial(&raw, reply->data, reply->size);
2088 if (raw == OFPRAW_OFPST11_FLOW_REPLY || raw == OFPRAW_OFPST13_FLOW_REPLY) {
2089 struct ofp11_flow_stats *ofs;
2091 ofpbuf_put_uninit(reply, sizeof *ofs);
2092 oxm_put_match(reply, &fs->match);
2093 ofpacts_put_openflow11_instructions(fs->ofpacts, fs->ofpacts_len,
2096 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
2097 ofs->length = htons(reply->size - start_ofs);
2098 ofs->table_id = fs->table_id;
2100 ofs->duration_sec = htonl(fs->duration_sec);
2101 ofs->duration_nsec = htonl(fs->duration_nsec);
2102 ofs->priority = htons(fs->priority);
2103 ofs->idle_timeout = htons(fs->idle_timeout);
2104 ofs->hard_timeout = htons(fs->hard_timeout);
2105 ofs->flags = (raw == OFPRAW_OFPST13_FLOW_REPLY) ? htons(fs->flags) : 0;
2106 memset(ofs->pad2, 0, sizeof ofs->pad2);
2107 ofs->cookie = fs->cookie;
2108 ofs->packet_count = htonll(unknown_to_zero(fs->packet_count));
2109 ofs->byte_count = htonll(unknown_to_zero(fs->byte_count));
2110 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
2111 struct ofp10_flow_stats *ofs;
2113 ofpbuf_put_uninit(reply, sizeof *ofs);
2114 ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply);
2116 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
2117 ofs->length = htons(reply->size - start_ofs);
2118 ofs->table_id = fs->table_id;
2120 ofputil_match_to_ofp10_match(&fs->match, &ofs->match);
2121 ofs->duration_sec = htonl(fs->duration_sec);
2122 ofs->duration_nsec = htonl(fs->duration_nsec);
2123 ofs->priority = htons(fs->priority);
2124 ofs->idle_timeout = htons(fs->idle_timeout);
2125 ofs->hard_timeout = htons(fs->hard_timeout);
2126 memset(ofs->pad2, 0, sizeof ofs->pad2);
2127 put_32aligned_be64(&ofs->cookie, fs->cookie);
2128 put_32aligned_be64(&ofs->packet_count,
2129 htonll(unknown_to_zero(fs->packet_count)));
2130 put_32aligned_be64(&ofs->byte_count,
2131 htonll(unknown_to_zero(fs->byte_count)));
2132 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
2133 struct nx_flow_stats *nfs;
2136 ofpbuf_put_uninit(reply, sizeof *nfs);
2137 match_len = nx_put_match(reply, &fs->match, 0, 0);
2138 ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply);
2140 nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
2141 nfs->length = htons(reply->size - start_ofs);
2142 nfs->table_id = fs->table_id;
2144 nfs->duration_sec = htonl(fs->duration_sec);
2145 nfs->duration_nsec = htonl(fs->duration_nsec);
2146 nfs->priority = htons(fs->priority);
2147 nfs->idle_timeout = htons(fs->idle_timeout);
2148 nfs->hard_timeout = htons(fs->hard_timeout);
2149 nfs->idle_age = htons(fs->idle_age < 0 ? 0
2150 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
2152 nfs->hard_age = htons(fs->hard_age < 0 ? 0
2153 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
2155 nfs->match_len = htons(match_len);
2156 nfs->cookie = fs->cookie;
2157 nfs->packet_count = htonll(fs->packet_count);
2158 nfs->byte_count = htonll(fs->byte_count);
2163 ofpmp_postappend(replies, start_ofs);
2166 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
2167 * NXST_AGGREGATE reply matching 'request', and returns the message. */
2169 ofputil_encode_aggregate_stats_reply(
2170 const struct ofputil_aggregate_stats *stats,
2171 const struct ofp_header *request)
2173 struct ofp_aggregate_stats_reply *asr;
2174 uint64_t packet_count;
2175 uint64_t byte_count;
2179 ofpraw_decode(&raw, request);
2180 if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) {
2181 packet_count = unknown_to_zero(stats->packet_count);
2182 byte_count = unknown_to_zero(stats->byte_count);
2184 packet_count = stats->packet_count;
2185 byte_count = stats->byte_count;
2188 msg = ofpraw_alloc_stats_reply(request, 0);
2189 asr = ofpbuf_put_zeros(msg, sizeof *asr);
2190 put_32aligned_be64(&asr->packet_count, htonll(packet_count));
2191 put_32aligned_be64(&asr->byte_count, htonll(byte_count));
2192 asr->flow_count = htonl(stats->flow_count);
2198 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
2199 const struct ofp_header *reply)
2201 struct ofp_aggregate_stats_reply *asr;
2204 ofpbuf_use_const(&msg, reply, ntohs(reply->length));
2205 ofpraw_pull_assert(&msg);
2208 stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
2209 stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
2210 stats->flow_count = ntohl(asr->flow_count);
2215 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
2216 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
2217 * an OpenFlow error code. */
2219 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
2220 const struct ofp_header *oh)
2225 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2226 raw = ofpraw_pull_assert(&b);
2227 if (raw == OFPRAW_OFPT11_FLOW_REMOVED) {
2228 const struct ofp12_flow_removed *ofr;
2231 ofr = ofpbuf_pull(&b, sizeof *ofr);
2233 error = ofputil_pull_ofp11_match(&b, &fr->match, NULL);
2238 fr->priority = ntohs(ofr->priority);
2239 fr->cookie = ofr->cookie;
2240 fr->reason = ofr->reason;
2241 fr->table_id = ofr->table_id;
2242 fr->duration_sec = ntohl(ofr->duration_sec);
2243 fr->duration_nsec = ntohl(ofr->duration_nsec);
2244 fr->idle_timeout = ntohs(ofr->idle_timeout);
2245 fr->hard_timeout = ntohs(ofr->hard_timeout);
2246 fr->packet_count = ntohll(ofr->packet_count);
2247 fr->byte_count = ntohll(ofr->byte_count);
2248 } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
2249 const struct ofp_flow_removed *ofr;
2251 ofr = ofpbuf_pull(&b, sizeof *ofr);
2253 ofputil_match_from_ofp10_match(&ofr->match, &fr->match);
2254 fr->priority = ntohs(ofr->priority);
2255 fr->cookie = ofr->cookie;
2256 fr->reason = ofr->reason;
2258 fr->duration_sec = ntohl(ofr->duration_sec);
2259 fr->duration_nsec = ntohl(ofr->duration_nsec);
2260 fr->idle_timeout = ntohs(ofr->idle_timeout);
2261 fr->hard_timeout = 0;
2262 fr->packet_count = ntohll(ofr->packet_count);
2263 fr->byte_count = ntohll(ofr->byte_count);
2264 } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
2265 struct nx_flow_removed *nfr;
2268 nfr = ofpbuf_pull(&b, sizeof *nfr);
2269 error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match,
2275 return OFPERR_OFPBRC_BAD_LEN;
2278 fr->priority = ntohs(nfr->priority);
2279 fr->cookie = nfr->cookie;
2280 fr->reason = nfr->reason;
2282 fr->duration_sec = ntohl(nfr->duration_sec);
2283 fr->duration_nsec = ntohl(nfr->duration_nsec);
2284 fr->idle_timeout = ntohs(nfr->idle_timeout);
2285 fr->hard_timeout = 0;
2286 fr->packet_count = ntohll(nfr->packet_count);
2287 fr->byte_count = ntohll(nfr->byte_count);
2295 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
2296 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
2299 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
2300 enum ofputil_protocol protocol)
2305 case OFPUTIL_P_OF12_OXM:
2306 case OFPUTIL_P_OF13_OXM: {
2307 struct ofp12_flow_removed *ofr;
2309 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED,
2310 ofputil_protocol_to_ofp_version(protocol),
2311 htonl(0), NXM_TYPICAL_LEN);
2312 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
2313 ofr->cookie = fr->cookie;
2314 ofr->priority = htons(fr->priority);
2315 ofr->reason = fr->reason;
2316 ofr->table_id = fr->table_id;
2317 ofr->duration_sec = htonl(fr->duration_sec);
2318 ofr->duration_nsec = htonl(fr->duration_nsec);
2319 ofr->idle_timeout = htons(fr->idle_timeout);
2320 ofr->hard_timeout = htons(fr->hard_timeout);
2321 ofr->packet_count = htonll(fr->packet_count);
2322 ofr->byte_count = htonll(fr->byte_count);
2323 oxm_put_match(msg, &fr->match);
2327 case OFPUTIL_P_OF10_STD:
2328 case OFPUTIL_P_OF10_STD_TID: {
2329 struct ofp_flow_removed *ofr;
2331 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
2333 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
2334 ofputil_match_to_ofp10_match(&fr->match, &ofr->match);
2335 ofr->cookie = fr->cookie;
2336 ofr->priority = htons(fr->priority);
2337 ofr->reason = fr->reason;
2338 ofr->duration_sec = htonl(fr->duration_sec);
2339 ofr->duration_nsec = htonl(fr->duration_nsec);
2340 ofr->idle_timeout = htons(fr->idle_timeout);
2341 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
2342 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
2346 case OFPUTIL_P_OF10_NXM:
2347 case OFPUTIL_P_OF10_NXM_TID: {
2348 struct nx_flow_removed *nfr;
2351 msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
2352 htonl(0), NXM_TYPICAL_LEN);
2353 nfr = ofpbuf_put_zeros(msg, sizeof *nfr);
2354 match_len = nx_put_match(msg, &fr->match, 0, 0);
2357 nfr->cookie = fr->cookie;
2358 nfr->priority = htons(fr->priority);
2359 nfr->reason = fr->reason;
2360 nfr->duration_sec = htonl(fr->duration_sec);
2361 nfr->duration_nsec = htonl(fr->duration_nsec);
2362 nfr->idle_timeout = htons(fr->idle_timeout);
2363 nfr->match_len = htons(match_len);
2364 nfr->packet_count = htonll(fr->packet_count);
2365 nfr->byte_count = htonll(fr->byte_count);
2377 ofputil_decode_packet_in_finish(struct ofputil_packet_in *pin,
2378 struct match *match, struct ofpbuf *b)
2380 pin->packet = b->data;
2381 pin->packet_len = b->size;
2383 pin->fmd.in_port = match->flow.in_port;
2384 pin->fmd.tun_id = match->flow.tunnel.tun_id;
2385 pin->fmd.metadata = match->flow.metadata;
2386 memcpy(pin->fmd.regs, match->flow.regs, sizeof pin->fmd.regs);
2390 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
2391 const struct ofp_header *oh)
2396 memset(pin, 0, sizeof *pin);
2398 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2399 raw = ofpraw_pull_assert(&b);
2400 if (raw == OFPRAW_OFPT13_PACKET_IN || raw == OFPRAW_OFPT12_PACKET_IN) {
2401 const struct ofp13_packet_in *opi;
2404 size_t packet_in_size;
2406 if (raw == OFPRAW_OFPT12_PACKET_IN) {
2407 packet_in_size = sizeof (struct ofp12_packet_in);
2409 packet_in_size = sizeof (struct ofp13_packet_in);
2412 opi = ofpbuf_pull(&b, packet_in_size);
2413 error = oxm_pull_match_loose(&b, &match);
2418 if (!ofpbuf_try_pull(&b, 2)) {
2419 return OFPERR_OFPBRC_BAD_LEN;
2422 pin->reason = opi->pi.reason;
2423 pin->table_id = opi->pi.table_id;
2424 pin->buffer_id = ntohl(opi->pi.buffer_id);
2425 pin->total_len = ntohs(opi->pi.total_len);
2427 if (raw == OFPRAW_OFPT13_PACKET_IN) {
2428 pin->cookie = opi->cookie;
2431 ofputil_decode_packet_in_finish(pin, &match, &b);
2432 } else if (raw == OFPRAW_OFPT10_PACKET_IN) {
2433 const struct ofp_packet_in *opi;
2435 opi = ofpbuf_pull(&b, offsetof(struct ofp_packet_in, data));
2437 pin->packet = opi->data;
2438 pin->packet_len = b.size;
2440 pin->fmd.in_port = ntohs(opi->in_port);
2441 pin->reason = opi->reason;
2442 pin->buffer_id = ntohl(opi->buffer_id);
2443 pin->total_len = ntohs(opi->total_len);
2444 } else if (raw == OFPRAW_NXT_PACKET_IN) {
2445 const struct nx_packet_in *npi;
2449 npi = ofpbuf_pull(&b, sizeof *npi);
2450 error = nx_pull_match_loose(&b, ntohs(npi->match_len), &match, NULL,
2456 if (!ofpbuf_try_pull(&b, 2)) {
2457 return OFPERR_OFPBRC_BAD_LEN;
2460 pin->reason = npi->reason;
2461 pin->table_id = npi->table_id;
2462 pin->cookie = npi->cookie;
2464 pin->buffer_id = ntohl(npi->buffer_id);
2465 pin->total_len = ntohs(npi->total_len);
2467 ofputil_decode_packet_in_finish(pin, &match, &b);
2476 ofputil_packet_in_to_match(const struct ofputil_packet_in *pin,
2477 struct match *match)
2481 match_init_catchall(match);
2482 if (pin->fmd.tun_id != htonll(0)) {
2483 match_set_tun_id(match, pin->fmd.tun_id);
2485 if (pin->fmd.metadata != htonll(0)) {
2486 match_set_metadata(match, pin->fmd.metadata);
2489 for (i = 0; i < FLOW_N_REGS; i++) {
2490 if (pin->fmd.regs[i]) {
2491 match_set_reg(match, i, pin->fmd.regs[i]);
2495 match_set_in_port(match, pin->fmd.in_port);
2498 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
2499 * in the format specified by 'packet_in_format'. */
2501 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
2502 enum ofputil_protocol protocol,
2503 enum nx_packet_in_format packet_in_format)
2505 size_t send_len = MIN(pin->send_len, pin->packet_len);
2506 struct ofpbuf *packet;
2508 /* Add OFPT_PACKET_IN. */
2509 if (protocol == OFPUTIL_P_OF13_OXM || protocol == OFPUTIL_P_OF12_OXM) {
2510 struct ofp13_packet_in *opi;
2512 enum ofpraw packet_in_raw;
2513 enum ofp_version packet_in_version;
2514 size_t packet_in_size;
2516 if (protocol == OFPUTIL_P_OF12_OXM) {
2517 packet_in_raw = OFPRAW_OFPT12_PACKET_IN;
2518 packet_in_version = OFP12_VERSION;
2519 packet_in_size = sizeof (struct ofp12_packet_in);
2521 packet_in_raw = OFPRAW_OFPT13_PACKET_IN;
2522 packet_in_version = OFP13_VERSION;
2523 packet_in_size = sizeof (struct ofp13_packet_in);
2526 ofputil_packet_in_to_match(pin, &match);
2528 /* The final argument is just an estimate of the space required. */
2529 packet = ofpraw_alloc_xid(packet_in_raw, packet_in_version,
2530 htonl(0), (sizeof(struct flow_metadata) * 2
2532 ofpbuf_put_zeros(packet, packet_in_size);
2533 oxm_put_match(packet, &match);
2534 ofpbuf_put_zeros(packet, 2);
2535 ofpbuf_put(packet, pin->packet, send_len);
2538 opi->pi.buffer_id = htonl(pin->buffer_id);
2539 opi->pi.total_len = htons(pin->total_len);
2540 opi->pi.reason = pin->reason;
2541 opi->pi.table_id = pin->table_id;
2542 if (protocol == OFPUTIL_P_OF13_OXM) {
2543 opi->cookie = pin->cookie;
2545 } else if (packet_in_format == NXPIF_OPENFLOW10) {
2546 struct ofp_packet_in *opi;
2548 packet = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
2549 htonl(0), send_len);
2550 opi = ofpbuf_put_zeros(packet, offsetof(struct ofp_packet_in, data));
2551 opi->total_len = htons(pin->total_len);
2552 opi->in_port = htons(pin->fmd.in_port);
2553 opi->reason = pin->reason;
2554 opi->buffer_id = htonl(pin->buffer_id);
2556 ofpbuf_put(packet, pin->packet, send_len);
2557 } else if (packet_in_format == NXPIF_NXM) {
2558 struct nx_packet_in *npi;
2562 ofputil_packet_in_to_match(pin, &match);
2564 /* The final argument is just an estimate of the space required. */
2565 packet = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, OFP10_VERSION,
2566 htonl(0), (sizeof(struct flow_metadata) * 2
2568 ofpbuf_put_zeros(packet, sizeof *npi);
2569 match_len = nx_put_match(packet, &match, 0, 0);
2570 ofpbuf_put_zeros(packet, 2);
2571 ofpbuf_put(packet, pin->packet, send_len);
2574 npi->buffer_id = htonl(pin->buffer_id);
2575 npi->total_len = htons(pin->total_len);
2576 npi->reason = pin->reason;
2577 npi->table_id = pin->table_id;
2578 npi->cookie = pin->cookie;
2579 npi->match_len = htons(match_len);
2583 ofpmsg_update_length(packet);
2589 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason)
2591 static char s[INT_STRLEN(int) + 1];
2598 case OFPR_INVALID_TTL:
2599 return "invalid_ttl";
2601 case OFPR_N_REASONS:
2603 sprintf(s, "%d", (int) reason);
2609 ofputil_packet_in_reason_from_string(const char *s,
2610 enum ofp_packet_in_reason *reason)
2614 for (i = 0; i < OFPR_N_REASONS; i++) {
2615 if (!strcasecmp(s, ofputil_packet_in_reason_to_string(i))) {
2623 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
2626 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
2627 * message's actions. The caller must initialize 'ofpacts' and retains
2628 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
2630 * Returns 0 if successful, otherwise an OFPERR_* value. */
2632 ofputil_decode_packet_out(struct ofputil_packet_out *po,
2633 const struct ofp_header *oh,
2634 struct ofpbuf *ofpacts)
2639 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2640 raw = ofpraw_pull_assert(&b);
2642 if (raw == OFPRAW_OFPT11_PACKET_OUT) {
2644 const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
2646 po->buffer_id = ntohl(opo->buffer_id);
2647 error = ofputil_port_from_ofp11(opo->in_port, &po->in_port);
2652 error = ofpacts_pull_openflow11_actions(&b, ntohs(opo->actions_len),
2657 } else if (raw == OFPRAW_OFPT10_PACKET_OUT) {
2659 const struct ofp_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
2661 po->buffer_id = ntohl(opo->buffer_id);
2662 po->in_port = ntohs(opo->in_port);
2664 error = ofpacts_pull_openflow10(&b, ntohs(opo->actions_len), ofpacts);
2672 if (po->in_port >= OFPP_MAX && po->in_port != OFPP_LOCAL
2673 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
2674 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
2676 return OFPERR_OFPBRC_BAD_PORT;
2679 po->ofpacts = ofpacts->data;
2680 po->ofpacts_len = ofpacts->size;
2682 if (po->buffer_id == UINT32_MAX) {
2683 po->packet = b.data;
2684 po->packet_len = b.size;
2693 /* ofputil_phy_port */
2695 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
2696 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2697 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2698 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2699 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2700 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2701 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2702 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2704 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
2705 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
2706 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
2707 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
2708 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
2709 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
2711 static enum netdev_features
2712 netdev_port_features_from_ofp10(ovs_be32 ofp10_)
2714 uint32_t ofp10 = ntohl(ofp10_);
2715 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
2719 netdev_port_features_to_ofp10(enum netdev_features features)
2721 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
2724 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2725 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2726 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2727 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2728 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2729 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2730 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2731 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
2732 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
2733 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
2734 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
2735 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
2736 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
2737 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
2738 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
2739 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
2741 static enum netdev_features
2742 netdev_port_features_from_ofp11(ovs_be32 ofp11)
2744 return ntohl(ofp11) & 0xffff;
2748 netdev_port_features_to_ofp11(enum netdev_features features)
2750 return htonl(features & 0xffff);
2754 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
2755 const struct ofp10_phy_port *opp)
2757 memset(pp, 0, sizeof *pp);
2759 pp->port_no = ntohs(opp->port_no);
2760 memcpy(pp->hw_addr, opp->hw_addr, OFP_ETH_ALEN);
2761 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
2763 pp->config = ntohl(opp->config) & OFPPC10_ALL;
2764 pp->state = ntohl(opp->state) & OFPPS10_ALL;
2766 pp->curr = netdev_port_features_from_ofp10(opp->curr);
2767 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
2768 pp->supported = netdev_port_features_from_ofp10(opp->supported);
2769 pp->peer = netdev_port_features_from_ofp10(opp->peer);
2771 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
2772 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
2778 ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
2779 const struct ofp11_port *op)
2783 memset(pp, 0, sizeof *pp);
2785 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
2789 memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN);
2790 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
2792 pp->config = ntohl(op->config) & OFPPC11_ALL;
2793 pp->state = ntohl(op->state) & OFPPC11_ALL;
2795 pp->curr = netdev_port_features_from_ofp11(op->curr);
2796 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
2797 pp->supported = netdev_port_features_from_ofp11(op->supported);
2798 pp->peer = netdev_port_features_from_ofp11(op->peer);
2800 pp->curr_speed = ntohl(op->curr_speed);
2801 pp->max_speed = ntohl(op->max_speed);
2807 ofputil_get_phy_port_size(enum ofp_version ofp_version)
2809 switch (ofp_version) {
2811 return sizeof(struct ofp10_phy_port);
2815 return sizeof(struct ofp11_port);
2822 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
2823 struct ofp10_phy_port *opp)
2825 memset(opp, 0, sizeof *opp);
2827 opp->port_no = htons(pp->port_no);
2828 memcpy(opp->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2829 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2831 opp->config = htonl(pp->config & OFPPC10_ALL);
2832 opp->state = htonl(pp->state & OFPPS10_ALL);
2834 opp->curr = netdev_port_features_to_ofp10(pp->curr);
2835 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
2836 opp->supported = netdev_port_features_to_ofp10(pp->supported);
2837 opp->peer = netdev_port_features_to_ofp10(pp->peer);
2841 ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
2842 struct ofp11_port *op)
2844 memset(op, 0, sizeof *op);
2846 op->port_no = ofputil_port_to_ofp11(pp->port_no);
2847 memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2848 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2850 op->config = htonl(pp->config & OFPPC11_ALL);
2851 op->state = htonl(pp->state & OFPPS11_ALL);
2853 op->curr = netdev_port_features_to_ofp11(pp->curr);
2854 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
2855 op->supported = netdev_port_features_to_ofp11(pp->supported);
2856 op->peer = netdev_port_features_to_ofp11(pp->peer);
2858 op->curr_speed = htonl(pp->curr_speed);
2859 op->max_speed = htonl(pp->max_speed);
2863 ofputil_put_phy_port(enum ofp_version ofp_version,
2864 const struct ofputil_phy_port *pp, struct ofpbuf *b)
2866 switch (ofp_version) {
2867 case OFP10_VERSION: {
2868 struct ofp10_phy_port *opp;
2869 if (b->size + sizeof *opp <= UINT16_MAX) {
2870 opp = ofpbuf_put_uninit(b, sizeof *opp);
2871 ofputil_encode_ofp10_phy_port(pp, opp);
2878 case OFP13_VERSION: {
2879 struct ofp11_port *op;
2880 if (b->size + sizeof *op <= UINT16_MAX) {
2881 op = ofpbuf_put_uninit(b, sizeof *op);
2882 ofputil_encode_ofp11_port(pp, op);
2893 ofputil_append_port_desc_stats_reply(enum ofp_version ofp_version,
2894 const struct ofputil_phy_port *pp,
2895 struct list *replies)
2897 switch (ofp_version) {
2898 case OFP10_VERSION: {
2899 struct ofp10_phy_port *opp;
2901 opp = ofpmp_append(replies, sizeof *opp);
2902 ofputil_encode_ofp10_phy_port(pp, opp);
2908 case OFP13_VERSION: {
2909 struct ofp11_port *op;
2911 op = ofpmp_append(replies, sizeof *op);
2912 ofputil_encode_ofp11_port(pp, op);
2921 /* ofputil_switch_features */
2923 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
2924 OFPC_IP_REASM | OFPC_QUEUE_STATS)
2925 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
2926 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
2927 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
2928 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
2929 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
2930 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
2932 struct ofputil_action_bit_translation {
2933 enum ofputil_action_bitmap ofputil_bit;
2937 static const struct ofputil_action_bit_translation of10_action_bits[] = {
2938 { OFPUTIL_A_OUTPUT, OFPAT10_OUTPUT },
2939 { OFPUTIL_A_SET_VLAN_VID, OFPAT10_SET_VLAN_VID },
2940 { OFPUTIL_A_SET_VLAN_PCP, OFPAT10_SET_VLAN_PCP },
2941 { OFPUTIL_A_STRIP_VLAN, OFPAT10_STRIP_VLAN },
2942 { OFPUTIL_A_SET_DL_SRC, OFPAT10_SET_DL_SRC },
2943 { OFPUTIL_A_SET_DL_DST, OFPAT10_SET_DL_DST },
2944 { OFPUTIL_A_SET_NW_SRC, OFPAT10_SET_NW_SRC },
2945 { OFPUTIL_A_SET_NW_DST, OFPAT10_SET_NW_DST },
2946 { OFPUTIL_A_SET_NW_TOS, OFPAT10_SET_NW_TOS },
2947 { OFPUTIL_A_SET_TP_SRC, OFPAT10_SET_TP_SRC },
2948 { OFPUTIL_A_SET_TP_DST, OFPAT10_SET_TP_DST },
2949 { OFPUTIL_A_ENQUEUE, OFPAT10_ENQUEUE },
2953 static enum ofputil_action_bitmap
2954 decode_action_bits(ovs_be32 of_actions,
2955 const struct ofputil_action_bit_translation *x)
2957 enum ofputil_action_bitmap ofputil_actions;
2959 ofputil_actions = 0;
2960 for (; x->ofputil_bit; x++) {
2961 if (of_actions & htonl(1u << x->of_bit)) {
2962 ofputil_actions |= x->ofputil_bit;
2965 return ofputil_actions;
2969 ofputil_capabilities_mask(enum ofp_version ofp_version)
2971 /* Handle capabilities whose bit is unique for all Open Flow versions */
2972 switch (ofp_version) {
2975 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
2978 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
2980 /* Caller needs to check osf->header.version itself */
2985 /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an
2986 * abstract representation in '*features'. Initializes '*b' to iterate over
2987 * the OpenFlow port structures following 'osf' with later calls to
2988 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an
2989 * OFPERR_* value. */
2991 ofputil_decode_switch_features(const struct ofp_header *oh,
2992 struct ofputil_switch_features *features,
2995 const struct ofp_switch_features *osf;
2998 ofpbuf_use_const(b, oh, ntohs(oh->length));
2999 raw = ofpraw_pull_assert(b);
3001 osf = ofpbuf_pull(b, sizeof *osf);
3002 features->datapath_id = ntohll(osf->datapath_id);
3003 features->n_buffers = ntohl(osf->n_buffers);
3004 features->n_tables = osf->n_tables;
3005 features->auxiliary_id = 0;
3007 features->capabilities = ntohl(osf->capabilities) &
3008 ofputil_capabilities_mask(oh->version);
3010 if (b->size % ofputil_get_phy_port_size(oh->version)) {
3011 return OFPERR_OFPBRC_BAD_LEN;
3014 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
3015 if (osf->capabilities & htonl(OFPC10_STP)) {
3016 features->capabilities |= OFPUTIL_C_STP;
3018 features->actions = decode_action_bits(osf->actions, of10_action_bits);
3019 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY
3020 || raw == OFPRAW_OFPT13_FEATURES_REPLY) {
3021 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
3022 features->capabilities |= OFPUTIL_C_GROUP_STATS;
3024 features->actions = 0;
3025 if (raw == OFPRAW_OFPT13_FEATURES_REPLY) {
3026 features->auxiliary_id = osf->auxiliary_id;
3029 return OFPERR_OFPBRC_BAD_VERSION;
3035 /* Returns true if the maximum number of ports are in 'oh'. */
3037 max_ports_in_features(const struct ofp_header *oh)
3039 size_t pp_size = ofputil_get_phy_port_size(oh->version);
3040 return ntohs(oh->length) + pp_size > UINT16_MAX;
3043 /* Given a buffer 'b' that contains a Features Reply message, checks if
3044 * it contains the maximum number of ports that will fit. If so, it
3045 * returns true and removes the ports from the message. The caller
3046 * should then send an OFPST_PORT_DESC stats request to get the ports,
3047 * since the switch may have more ports than could be represented in the
3048 * Features Reply. Otherwise, returns false.
3051 ofputil_switch_features_ports_trunc(struct ofpbuf *b)
3053 struct ofp_header *oh = b->data;
3055 if (max_ports_in_features(oh)) {
3056 /* Remove all the ports. */
3057 b->size = (sizeof(struct ofp_header)
3058 + sizeof(struct ofp_switch_features));
3059 ofpmsg_update_length(b);
3068 encode_action_bits(enum ofputil_action_bitmap ofputil_actions,
3069 const struct ofputil_action_bit_translation *x)
3071 uint32_t of_actions;
3074 for (; x->ofputil_bit; x++) {
3075 if (ofputil_actions & x->ofputil_bit) {
3076 of_actions |= 1 << x->of_bit;
3079 return htonl(of_actions);
3082 /* Returns a buffer owned by the caller that encodes 'features' in the format
3083 * required by 'protocol' with the given 'xid'. The caller should append port
3084 * information to the buffer with subsequent calls to
3085 * ofputil_put_switch_features_port(). */
3087 ofputil_encode_switch_features(const struct ofputil_switch_features *features,
3088 enum ofputil_protocol protocol, ovs_be32 xid)
3090 struct ofp_switch_features *osf;
3092 enum ofp_version version;
3095 version = ofputil_protocol_to_ofp_version(protocol);
3098 raw = OFPRAW_OFPT10_FEATURES_REPLY;
3102 raw = OFPRAW_OFPT11_FEATURES_REPLY;
3105 raw = OFPRAW_OFPT13_FEATURES_REPLY;
3110 b = ofpraw_alloc_xid(raw, version, xid, 0);
3111 osf = ofpbuf_put_zeros(b, sizeof *osf);
3112 osf->datapath_id = htonll(features->datapath_id);
3113 osf->n_buffers = htonl(features->n_buffers);
3114 osf->n_tables = features->n_tables;
3116 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
3117 osf->capabilities = htonl(features->capabilities &
3118 ofputil_capabilities_mask(version));
3121 if (features->capabilities & OFPUTIL_C_STP) {
3122 osf->capabilities |= htonl(OFPC10_STP);
3124 osf->actions = encode_action_bits(features->actions, of10_action_bits);
3127 osf->auxiliary_id = features->auxiliary_id;
3131 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
3132 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
3142 /* Encodes 'pp' into the format required by the switch_features message already
3143 * in 'b', which should have been returned by ofputil_encode_switch_features(),
3144 * and appends the encoded version to 'b'. */
3146 ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
3149 const struct ofp_header *oh = b->data;
3151 ofputil_put_phy_port(oh->version, pp, b);
3154 /* ofputil_port_status */
3156 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
3157 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
3159 ofputil_decode_port_status(const struct ofp_header *oh,
3160 struct ofputil_port_status *ps)
3162 const struct ofp_port_status *ops;
3166 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3167 ofpraw_pull_assert(&b);
3168 ops = ofpbuf_pull(&b, sizeof *ops);
3170 if (ops->reason != OFPPR_ADD &&
3171 ops->reason != OFPPR_DELETE &&
3172 ops->reason != OFPPR_MODIFY) {
3173 return OFPERR_NXBRC_BAD_REASON;
3175 ps->reason = ops->reason;
3177 retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
3178 assert(retval != EOF);
3182 /* Converts the abstract form of a "port status" message in '*ps' into an
3183 * OpenFlow message suitable for 'protocol', and returns that encoded form in
3184 * a buffer owned by the caller. */
3186 ofputil_encode_port_status(const struct ofputil_port_status *ps,
3187 enum ofputil_protocol protocol)
3189 struct ofp_port_status *ops;
3191 enum ofp_version version;
3194 version = ofputil_protocol_to_ofp_version(protocol);
3197 raw = OFPRAW_OFPT10_PORT_STATUS;
3203 raw = OFPRAW_OFPT11_PORT_STATUS;
3210 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
3211 ops = ofpbuf_put_zeros(b, sizeof *ops);
3212 ops->reason = ps->reason;
3213 ofputil_put_phy_port(version, &ps->desc, b);
3214 ofpmsg_update_length(b);
3218 /* ofputil_port_mod */
3220 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
3221 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
3223 ofputil_decode_port_mod(const struct ofp_header *oh,
3224 struct ofputil_port_mod *pm)
3229 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3230 raw = ofpraw_pull_assert(&b);
3232 if (raw == OFPRAW_OFPT10_PORT_MOD) {
3233 const struct ofp10_port_mod *opm = b.data;
3235 pm->port_no = ntohs(opm->port_no);
3236 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
3237 pm->config = ntohl(opm->config) & OFPPC10_ALL;
3238 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
3239 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
3240 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
3241 const struct ofp11_port_mod *opm = b.data;
3244 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
3249 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
3250 pm->config = ntohl(opm->config) & OFPPC11_ALL;
3251 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
3252 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
3254 return OFPERR_OFPBRC_BAD_TYPE;
3257 pm->config &= pm->mask;
3261 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
3262 * message suitable for 'protocol', and returns that encoded form in a buffer
3263 * owned by the caller. */
3265 ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
3266 enum ofputil_protocol protocol)
3268 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
3271 switch (ofp_version) {
3272 case OFP10_VERSION: {
3273 struct ofp10_port_mod *opm;
3275 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
3276 opm = ofpbuf_put_zeros(b, sizeof *opm);
3277 opm->port_no = htons(pm->port_no);
3278 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
3279 opm->config = htonl(pm->config & OFPPC10_ALL);
3280 opm->mask = htonl(pm->mask & OFPPC10_ALL);
3281 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
3287 case OFP13_VERSION: {
3288 struct ofp11_port_mod *opm;
3290 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
3291 opm = ofpbuf_put_zeros(b, sizeof *opm);
3292 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
3293 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
3294 opm->config = htonl(pm->config & OFPPC11_ALL);
3295 opm->mask = htonl(pm->mask & OFPPC11_ALL);
3296 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
3310 ofputil_put_ofp10_table_stats(const struct ofp12_table_stats *in,
3314 enum ofp_flow_wildcards wc10;
3315 enum oxm12_ofb_match_fields mf12;
3318 static const struct wc_map wc_map[] = {
3319 { OFPFW10_IN_PORT, OFPXMT12_OFB_IN_PORT },
3320 { OFPFW10_DL_VLAN, OFPXMT12_OFB_VLAN_VID },
3321 { OFPFW10_DL_SRC, OFPXMT12_OFB_ETH_SRC },
3322 { OFPFW10_DL_DST, OFPXMT12_OFB_ETH_DST},
3323 { OFPFW10_DL_TYPE, OFPXMT12_OFB_ETH_TYPE },
3324 { OFPFW10_NW_PROTO, OFPXMT12_OFB_IP_PROTO },
3325 { OFPFW10_TP_SRC, OFPXMT12_OFB_TCP_SRC },
3326 { OFPFW10_TP_DST, OFPXMT12_OFB_TCP_DST },
3327 { OFPFW10_NW_SRC_MASK, OFPXMT12_OFB_IPV4_SRC },
3328 { OFPFW10_NW_DST_MASK, OFPXMT12_OFB_IPV4_DST },
3329 { OFPFW10_DL_VLAN_PCP, OFPXMT12_OFB_VLAN_PCP },
3330 { OFPFW10_NW_TOS, OFPXMT12_OFB_IP_DSCP },
3333 struct ofp10_table_stats *out;
3334 const struct wc_map *p;
3336 out = ofpbuf_put_uninit(buf, sizeof *out);
3337 out->table_id = in->table_id;
3338 strcpy(out->name, in->name);
3340 for (p = wc_map; p < &wc_map[ARRAY_SIZE(wc_map)]; p++) {
3341 if (in->wildcards & htonll(1ULL << p->mf12)) {
3342 out->wildcards |= htonl(p->wc10);
3345 out->max_entries = in->max_entries;
3346 out->active_count = in->active_count;
3347 put_32aligned_be64(&out->lookup_count, in->lookup_count);
3348 put_32aligned_be64(&out->matched_count, in->matched_count);
3352 oxm12_to_ofp11_flow_match_fields(ovs_be64 oxm12)
3355 enum ofp11_flow_match_fields fmf11;
3356 enum oxm12_ofb_match_fields mf12;
3359 static const struct map map[] = {
3360 { OFPFMF11_IN_PORT, OFPXMT12_OFB_IN_PORT },
3361 { OFPFMF11_DL_VLAN, OFPXMT12_OFB_VLAN_VID },
3362 { OFPFMF11_DL_VLAN_PCP, OFPXMT12_OFB_VLAN_PCP },
3363 { OFPFMF11_DL_TYPE, OFPXMT12_OFB_ETH_TYPE },
3364 { OFPFMF11_NW_TOS, OFPXMT12_OFB_IP_DSCP },
3365 { OFPFMF11_NW_PROTO, OFPXMT12_OFB_IP_PROTO },
3366 { OFPFMF11_TP_SRC, OFPXMT12_OFB_TCP_SRC },
3367 { OFPFMF11_TP_DST, OFPXMT12_OFB_TCP_DST },
3368 { OFPFMF11_MPLS_LABEL, OFPXMT12_OFB_MPLS_LABEL },
3369 { OFPFMF11_MPLS_TC, OFPXMT12_OFB_MPLS_TC },
3370 /* I don't know what OFPFMF11_TYPE means. */
3371 { OFPFMF11_DL_SRC, OFPXMT12_OFB_ETH_SRC },
3372 { OFPFMF11_DL_DST, OFPXMT12_OFB_ETH_DST },
3373 { OFPFMF11_NW_SRC, OFPXMT12_OFB_IPV4_SRC },
3374 { OFPFMF11_NW_DST, OFPXMT12_OFB_IPV4_DST },
3375 { OFPFMF11_METADATA, OFPXMT12_OFB_METADATA },
3378 const struct map *p;
3382 for (p = map; p < &map[ARRAY_SIZE(map)]; p++) {
3383 if (oxm12 & htonll(1ULL << p->mf12)) {
3387 return htonl(fmf11);
3391 ofputil_put_ofp11_table_stats(const struct ofp12_table_stats *in,
3394 struct ofp11_table_stats *out;
3396 out = ofpbuf_put_uninit(buf, sizeof *out);
3397 out->table_id = in->table_id;
3398 strcpy(out->name, in->name);
3399 out->wildcards = oxm12_to_ofp11_flow_match_fields(in->wildcards);
3400 out->match = oxm12_to_ofp11_flow_match_fields(in->match);
3401 out->instructions = in->instructions;
3402 out->write_actions = in->write_actions;
3403 out->apply_actions = in->apply_actions;
3404 out->config = in->config;
3405 out->max_entries = in->max_entries;
3406 out->active_count = in->active_count;
3407 out->lookup_count = in->lookup_count;
3408 out->matched_count = in->matched_count;
3412 ofputil_put_ofp13_table_stats(const struct ofp12_table_stats *in,
3415 struct ofp13_table_stats *out;
3417 /* OF 1.3 splits table features off the ofp_table_stats,
3418 * so there is not much here. */
3420 out = ofpbuf_put_uninit(buf, sizeof *out);
3421 out->table_id = in->table_id;
3422 out->active_count = in->active_count;
3423 out->lookup_count = in->lookup_count;
3424 out->matched_count = in->matched_count;
3428 ofputil_encode_table_stats_reply(const struct ofp12_table_stats stats[], int n,
3429 const struct ofp_header *request)
3431 struct ofpbuf *reply;
3434 reply = ofpraw_alloc_stats_reply(request, n * sizeof *stats);
3436 switch ((enum ofp_version) request->version) {
3438 for (i = 0; i < n; i++) {
3439 ofputil_put_ofp10_table_stats(&stats[i], reply);
3444 for (i = 0; i < n; i++) {
3445 ofputil_put_ofp11_table_stats(&stats[i], reply);
3450 ofpbuf_put(reply, stats, n * sizeof *stats);
3454 for (i = 0; i < n; i++) {
3455 ofputil_put_ofp13_table_stats(&stats[i], reply);
3466 /* ofputil_flow_monitor_request */
3468 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
3469 * ofputil_flow_monitor_request in 'rq'.
3471 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
3472 * message. Calling this function multiple times for a single 'msg' iterates
3473 * through the requests. The caller must initially leave 'msg''s layer
3474 * pointers null and not modify them between calls.
3476 * Returns 0 if successful, EOF if no requests were left in this 'msg',
3477 * otherwise an OFPERR_* value. */
3479 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
3482 struct nx_flow_monitor_request *nfmr;
3486 msg->l2 = msg->data;
3487 ofpraw_pull_assert(msg);
3494 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
3496 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %zu "
3497 "leftover bytes at end", msg->size);
3498 return OFPERR_OFPBRC_BAD_LEN;
3501 flags = ntohs(nfmr->flags);
3502 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
3503 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
3504 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
3505 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
3507 return OFPERR_NXBRC_FM_BAD_FLAGS;
3510 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
3511 return OFPERR_NXBRC_MUST_BE_ZERO;
3514 rq->id = ntohl(nfmr->id);
3516 rq->out_port = ntohs(nfmr->out_port);
3517 rq->table_id = nfmr->table_id;
3519 return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL, NULL);
3523 ofputil_append_flow_monitor_request(
3524 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
3526 struct nx_flow_monitor_request *nfmr;
3531 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
3534 start_ofs = msg->size;
3535 ofpbuf_put_zeros(msg, sizeof *nfmr);
3536 match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
3538 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
3539 nfmr->id = htonl(rq->id);
3540 nfmr->flags = htons(rq->flags);
3541 nfmr->out_port = htons(rq->out_port);
3542 nfmr->match_len = htons(match_len);
3543 nfmr->table_id = rq->table_id;
3546 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
3547 * into an abstract ofputil_flow_update in 'update'. The caller must have
3548 * initialized update->match to point to space allocated for a match.
3550 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
3551 * actions (except for NXFME_ABBREV, which never includes actions). The caller
3552 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
3553 * will point into the 'ofpacts' buffer.
3555 * Multiple flow updates can be packed into a single OpenFlow message. Calling
3556 * this function multiple times for a single 'msg' iterates through the
3557 * updates. The caller must initially leave 'msg''s layer pointers null and
3558 * not modify them between calls.
3560 * Returns 0 if successful, EOF if no updates were left in this 'msg',
3561 * otherwise an OFPERR_* value. */
3563 ofputil_decode_flow_update(struct ofputil_flow_update *update,
3564 struct ofpbuf *msg, struct ofpbuf *ofpacts)
3566 struct nx_flow_update_header *nfuh;
3567 unsigned int length;
3570 msg->l2 = msg->data;
3571 ofpraw_pull_assert(msg);
3578 if (msg->size < sizeof(struct nx_flow_update_header)) {
3583 update->event = ntohs(nfuh->event);
3584 length = ntohs(nfuh->length);
3585 if (length > msg->size || length % 8) {
3589 if (update->event == NXFME_ABBREV) {
3590 struct nx_flow_update_abbrev *nfua;
3592 if (length != sizeof *nfua) {
3596 nfua = ofpbuf_pull(msg, sizeof *nfua);
3597 update->xid = nfua->xid;
3599 } else if (update->event == NXFME_ADDED
3600 || update->event == NXFME_DELETED
3601 || update->event == NXFME_MODIFIED) {
3602 struct nx_flow_update_full *nfuf;
3603 unsigned int actions_len;
3604 unsigned int match_len;
3607 if (length < sizeof *nfuf) {
3611 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
3612 match_len = ntohs(nfuf->match_len);
3613 if (sizeof *nfuf + match_len > length) {
3617 update->reason = ntohs(nfuf->reason);
3618 update->idle_timeout = ntohs(nfuf->idle_timeout);
3619 update->hard_timeout = ntohs(nfuf->hard_timeout);
3620 update->table_id = nfuf->table_id;
3621 update->cookie = nfuf->cookie;
3622 update->priority = ntohs(nfuf->priority);
3624 error = nx_pull_match(msg, match_len, update->match, NULL, NULL);
3629 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
3630 error = ofpacts_pull_openflow10(msg, actions_len, ofpacts);
3635 update->ofpacts = ofpacts->data;
3636 update->ofpacts_len = ofpacts->size;
3639 VLOG_WARN_RL(&bad_ofmsg_rl,
3640 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
3641 ntohs(nfuh->event));
3642 return OFPERR_OFPET_BAD_REQUEST;
3646 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %zu "
3647 "leftover bytes at end", msg->size);
3648 return OFPERR_OFPBRC_BAD_LEN;
3652 ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
3654 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
3656 return ntohl(cancel->id);
3660 ofputil_encode_flow_monitor_cancel(uint32_t id)
3662 struct nx_flow_monitor_cancel *nfmc;
3665 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
3666 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
3667 nfmc->id = htonl(id);
3672 ofputil_start_flow_update(struct list *replies)
3676 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
3680 list_push_back(replies, &msg->list_node);
3684 ofputil_append_flow_update(const struct ofputil_flow_update *update,
3685 struct list *replies)
3687 struct nx_flow_update_header *nfuh;
3691 msg = ofpbuf_from_list(list_back(replies));
3692 start_ofs = msg->size;
3694 if (update->event == NXFME_ABBREV) {
3695 struct nx_flow_update_abbrev *nfua;
3697 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
3698 nfua->xid = update->xid;
3700 struct nx_flow_update_full *nfuf;
3703 ofpbuf_put_zeros(msg, sizeof *nfuf);
3704 match_len = nx_put_match(msg, update->match, htonll(0), htonll(0));
3705 ofpacts_put_openflow10(update->ofpacts, update->ofpacts_len, msg);
3707 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
3708 nfuf->reason = htons(update->reason);
3709 nfuf->priority = htons(update->priority);
3710 nfuf->idle_timeout = htons(update->idle_timeout);
3711 nfuf->hard_timeout = htons(update->hard_timeout);
3712 nfuf->match_len = htons(match_len);
3713 nfuf->table_id = update->table_id;
3714 nfuf->cookie = update->cookie;
3717 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
3718 nfuh->length = htons(msg->size - start_ofs);
3719 nfuh->event = htons(update->event);
3721 ofpmp_postappend(replies, start_ofs);
3725 ofputil_encode_packet_out(const struct ofputil_packet_out *po,
3726 enum ofputil_protocol protocol)
3728 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
3732 size = po->ofpacts_len;
3733 if (po->buffer_id == UINT32_MAX) {
3734 size += po->packet_len;
3737 switch (ofp_version) {
3738 case OFP10_VERSION: {
3739 struct ofp_packet_out *opo;
3742 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
3743 ofpbuf_put_zeros(msg, sizeof *opo);
3744 actions_ofs = msg->size;
3745 ofpacts_put_openflow10(po->ofpacts, po->ofpacts_len, msg);
3748 opo->buffer_id = htonl(po->buffer_id);
3749 opo->in_port = htons(po->in_port);
3750 opo->actions_len = htons(msg->size - actions_ofs);
3756 case OFP13_VERSION: {
3757 struct ofp11_packet_out *opo;
3760 msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size);
3761 ofpbuf_put_zeros(msg, sizeof *opo);
3762 len = ofpacts_put_openflow11_actions(po->ofpacts, po->ofpacts_len, msg);
3765 opo->buffer_id = htonl(po->buffer_id);
3766 opo->in_port = ofputil_port_to_ofp11(po->in_port);
3767 opo->actions_len = htons(len);
3775 if (po->buffer_id == UINT32_MAX) {
3776 ofpbuf_put(msg, po->packet, po->packet_len);
3779 ofpmsg_update_length(msg);
3784 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
3786 make_echo_request(enum ofp_version ofp_version)
3788 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
3792 /* Creates and returns an OFPT_ECHO_REPLY message matching the
3793 * OFPT_ECHO_REQUEST message in 'rq'. */
3795 make_echo_reply(const struct ofp_header *rq)
3797 struct ofpbuf rq_buf;
3798 struct ofpbuf *reply;
3800 ofpbuf_use_const(&rq_buf, rq, ntohs(rq->length));
3801 ofpraw_pull_assert(&rq_buf);
3803 reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY, rq, rq_buf.size);
3804 ofpbuf_put(reply, rq_buf.data, rq_buf.size);
3809 ofputil_encode_barrier_request(enum ofp_version ofp_version)
3813 switch (ofp_version) {
3817 type = OFPRAW_OFPT11_BARRIER_REQUEST;
3821 type = OFPRAW_OFPT10_BARRIER_REQUEST;
3828 return ofpraw_alloc(type, ofp_version, 0);
3832 ofputil_frag_handling_to_string(enum ofp_config_flags flags)
3834 switch (flags & OFPC_FRAG_MASK) {
3835 case OFPC_FRAG_NORMAL: return "normal";
3836 case OFPC_FRAG_DROP: return "drop";
3837 case OFPC_FRAG_REASM: return "reassemble";
3838 case OFPC_FRAG_NX_MATCH: return "nx-match";
3845 ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
3847 if (!strcasecmp(s, "normal")) {
3848 *flags = OFPC_FRAG_NORMAL;
3849 } else if (!strcasecmp(s, "drop")) {
3850 *flags = OFPC_FRAG_DROP;
3851 } else if (!strcasecmp(s, "reassemble")) {
3852 *flags = OFPC_FRAG_REASM;
3853 } else if (!strcasecmp(s, "nx-match")) {
3854 *flags = OFPC_FRAG_NX_MATCH;
3861 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
3862 * port number and stores the latter in '*ofp10_port', for the purpose of
3863 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
3864 * otherwise an OFPERR_* number.
3866 * See the definition of OFP11_MAX for an explanation of the mapping. */
3868 ofputil_port_from_ofp11(ovs_be32 ofp11_port, uint16_t *ofp10_port)
3870 uint32_t ofp11_port_h = ntohl(ofp11_port);
3872 if (ofp11_port_h < OFPP_MAX) {
3873 *ofp10_port = ofp11_port_h;
3875 } else if (ofp11_port_h >= OFPP11_MAX) {
3876 *ofp10_port = ofp11_port_h - OFPP11_OFFSET;
3879 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
3880 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
3881 ofp11_port_h, OFPP_MAX - 1,
3882 (uint32_t) OFPP11_MAX, UINT32_MAX);
3883 return OFPERR_OFPBAC_BAD_OUT_PORT;
3887 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
3888 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
3890 * See the definition of OFP11_MAX for an explanation of the mapping. */
3892 ofputil_port_to_ofp11(uint16_t ofp10_port)
3894 return htonl(ofp10_port < OFPP_MAX
3896 : ofp10_port + OFPP11_OFFSET);
3899 /* Checks that 'port' is a valid output port for the OFPAT10_OUTPUT action, given
3900 * that the switch will never have more than 'max_ports' ports. Returns 0 if
3901 * 'port' is valid, otherwise an OpenFlow return code. */
3903 ofputil_check_output_port(uint16_t port, int max_ports)
3911 case OFPP_CONTROLLER:
3917 if (port < max_ports) {
3920 return OFPERR_OFPBAC_BAD_OUT_PORT;
3924 #define OFPUTIL_NAMED_PORTS \
3925 OFPUTIL_NAMED_PORT(IN_PORT) \
3926 OFPUTIL_NAMED_PORT(TABLE) \
3927 OFPUTIL_NAMED_PORT(NORMAL) \
3928 OFPUTIL_NAMED_PORT(FLOOD) \
3929 OFPUTIL_NAMED_PORT(ALL) \
3930 OFPUTIL_NAMED_PORT(CONTROLLER) \
3931 OFPUTIL_NAMED_PORT(LOCAL) \
3932 OFPUTIL_NAMED_PORT(ANY)
3934 /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */
3935 #define OFPUTIL_NAMED_PORTS_WITH_NONE \
3936 OFPUTIL_NAMED_PORTS \
3937 OFPUTIL_NAMED_PORT(NONE)
3939 /* Stores the port number represented by 's' into '*portp'. 's' may be an
3940 * integer or, for reserved ports, the standard OpenFlow name for the port
3943 * Returns true if successful, false if 's' is not a valid OpenFlow port number
3944 * or name. The caller should issue an error message in this case, because
3945 * this function usually does not. (This gives the caller an opportunity to
3946 * look up the port name another way, e.g. by contacting the switch and listing
3947 * the names of all its ports).
3949 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
3950 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
3951 * range as described in include/openflow/openflow-1.1.h. */
3953 ofputil_port_from_string(const char *s, uint16_t *portp)
3955 unsigned int port32;
3958 if (str_to_uint(s, 10, &port32)) {
3959 if (port32 < OFPP_MAX) {
3962 } else if (port32 < OFPP_FIRST_RESV) {
3963 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
3964 "be translated to %u when talking to an OF1.1 or "
3965 "later controller", port32, port32 + OFPP11_OFFSET);
3968 } else if (port32 <= OFPP_LAST_RESV) {
3972 ofputil_format_port(port32, &s);
3973 VLOG_WARN_ONCE("referring to port %s as %u is deprecated for "
3974 "compatibility with future versions of OpenFlow",
3975 ds_cstr(&s), port32);
3980 } else if (port32 < OFPP11_MAX) {
3981 VLOG_WARN("port %u is outside the supported range 0 through "
3982 "%"PRIx16"or 0x%x through 0x%"PRIx32, port32,
3983 UINT16_MAX, (unsigned int) OFPP11_MAX, UINT32_MAX);
3986 *portp = port32 - OFPP11_OFFSET;
3994 static const struct pair pairs[] = {
3995 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
3996 OFPUTIL_NAMED_PORTS_WITH_NONE
3997 #undef OFPUTIL_NAMED_PORT
3999 const struct pair *p;
4001 for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) {
4002 if (!strcasecmp(s, p->name)) {
4011 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
4012 * Most ports' string representation is just the port number, but for special
4013 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
4015 ofputil_format_port(uint16_t port, struct ds *s)
4020 #define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break;
4022 #undef OFPUTIL_NAMED_PORT
4025 ds_put_format(s, "%"PRIu16, port);
4028 ds_put_cstr(s, name);
4031 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
4032 * 'ofp_version', tries to pull the first element from the array. If
4033 * successful, initializes '*pp' with an abstract representation of the
4034 * port and returns 0. If no ports remain to be decoded, returns EOF.
4035 * On an error, returns a positive OFPERR_* value. */
4037 ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
4038 struct ofputil_phy_port *pp)
4040 switch (ofp_version) {
4041 case OFP10_VERSION: {
4042 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
4043 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
4047 case OFP13_VERSION: {
4048 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
4049 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
4056 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
4057 * 'ofp_version', returns the number of elements. */
4058 size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *b)
4060 return b->size / ofputil_get_phy_port_size(ofp_version);
4063 /* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if
4064 * 'name' is "output" then the return value is OFPUTIL_OFPAT10_OUTPUT), or -1 if
4065 * 'name' is not the name of any action.
4067 * ofp-util.def lists the mapping from names to action. */
4069 ofputil_action_code_from_name(const char *name)
4071 static const char *names[OFPUTIL_N_ACTIONS] = {
4073 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) NAME,
4074 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
4075 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
4076 #include "ofp-util.def"
4081 for (p = names; p < &names[ARRAY_SIZE(names)]; p++) {
4082 if (*p && !strcasecmp(name, *p)) {
4089 /* Appends an action of the type specified by 'code' to 'buf' and returns the
4090 * action. Initializes the parts of 'action' that identify it as having type
4091 * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that
4092 * have variable length, the length used and cleared is that of struct
4095 ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
4098 case OFPUTIL_ACTION_INVALID:
4101 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
4102 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
4103 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
4104 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
4105 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
4106 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
4107 #include "ofp-util.def"
4112 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
4114 ofputil_init_##ENUM(struct STRUCT *s) \
4116 memset(s, 0, sizeof *s); \
4117 s->type = htons(ENUM); \
4118 s->len = htons(sizeof *s); \
4122 ofputil_put_##ENUM(struct ofpbuf *buf) \
4124 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
4125 ofputil_init_##ENUM(s); \
4128 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
4129 OFPAT10_ACTION(ENUM, STRUCT, NAME)
4130 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
4132 ofputil_init_##ENUM(struct STRUCT *s) \
4134 memset(s, 0, sizeof *s); \
4135 s->type = htons(OFPAT10_VENDOR); \
4136 s->len = htons(sizeof *s); \
4137 s->vendor = htonl(NX_VENDOR_ID); \
4138 s->subtype = htons(ENUM); \
4142 ofputil_put_##ENUM(struct ofpbuf *buf) \
4144 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
4145 ofputil_init_##ENUM(s); \
4148 #include "ofp-util.def"
4151 ofputil_normalize_match__(struct match *match, bool may_log)
4154 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
4155 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
4156 MAY_NW_PROTO = 1 << 2, /* nw_proto */
4157 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
4158 MAY_ARP_SHA = 1 << 4, /* arp_sha */
4159 MAY_ARP_THA = 1 << 5, /* arp_tha */
4160 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
4161 MAY_ND_TARGET = 1 << 7 /* nd_target */
4164 struct flow_wildcards wc;
4166 /* Figure out what fields may be matched. */
4167 if (match->flow.dl_type == htons(ETH_TYPE_IP)) {
4168 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
4169 if (match->flow.nw_proto == IPPROTO_TCP ||
4170 match->flow.nw_proto == IPPROTO_UDP ||
4171 match->flow.nw_proto == IPPROTO_ICMP) {
4172 may_match |= MAY_TP_ADDR;
4174 } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
4175 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
4176 if (match->flow.nw_proto == IPPROTO_TCP ||
4177 match->flow.nw_proto == IPPROTO_UDP) {
4178 may_match |= MAY_TP_ADDR;
4179 } else if (match->flow.nw_proto == IPPROTO_ICMPV6) {
4180 may_match |= MAY_TP_ADDR;
4181 if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
4182 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
4183 } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
4184 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
4187 } else if (match->flow.dl_type == htons(ETH_TYPE_ARP) ||
4188 match->flow.dl_type == htons(ETH_TYPE_RARP)) {
4189 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
4194 /* Clear the fields that may not be matched. */
4196 if (!(may_match & MAY_NW_ADDR)) {
4197 wc.masks.nw_src = wc.masks.nw_dst = htonl(0);
4199 if (!(may_match & MAY_TP_ADDR)) {
4200 wc.masks.tp_src = wc.masks.tp_dst = htons(0);
4202 if (!(may_match & MAY_NW_PROTO)) {
4203 wc.masks.nw_proto = 0;
4205 if (!(may_match & MAY_IPVx)) {
4206 wc.masks.nw_tos = 0;
4207 wc.masks.nw_ttl = 0;
4209 if (!(may_match & MAY_ARP_SHA)) {
4210 memset(wc.masks.arp_sha, 0, ETH_ADDR_LEN);
4212 if (!(may_match & MAY_ARP_THA)) {
4213 memset(wc.masks.arp_tha, 0, ETH_ADDR_LEN);
4215 if (!(may_match & MAY_IPV6)) {
4216 wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any;
4217 wc.masks.ipv6_label = htonl(0);
4219 if (!(may_match & MAY_ND_TARGET)) {
4220 wc.masks.nd_target = in6addr_any;
4223 /* Log any changes. */
4224 if (!flow_wildcards_equal(&wc, &match->wc)) {
4225 bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
4226 char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL;
4229 match_zero_wildcarded_fields(match);
4232 char *post = match_to_string(match, OFP_DEFAULT_PRIORITY);
4233 VLOG_INFO("normalization changed ofp_match, details:");
4234 VLOG_INFO(" pre: %s", pre);
4235 VLOG_INFO("post: %s", post);
4242 /* "Normalizes" the wildcards in 'match'. That means:
4244 * 1. If the type of level N is known, then only the valid fields for that
4245 * level may be specified. For example, ARP does not have a TOS field,
4246 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
4247 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
4248 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
4251 * 2. If the type of level N is not known (or not understood by Open
4252 * vSwitch), then no fields at all for that level may be specified. For
4253 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
4254 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
4257 * If this function changes 'match', it logs a rate-limited informational
4260 ofputil_normalize_match(struct match *match)
4262 ofputil_normalize_match__(match, true);
4265 /* Same as ofputil_normalize_match() without the logging. Thus, this function
4266 * is suitable for a program's internal use, whereas ofputil_normalize_match()
4267 * sense for use on flows received from elsewhere (so that a bug in the program
4268 * that sent them can be reported and corrected). */
4270 ofputil_normalize_match_quiet(struct match *match)
4272 ofputil_normalize_match__(match, false);
4275 /* Parses a key or a key-value pair from '*stringp'.
4277 * On success: Stores the key into '*keyp'. Stores the value, if present, into
4278 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
4279 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
4280 * are substrings of '*stringp' created by replacing some of its bytes by null
4281 * terminators. Returns true.
4283 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
4284 * NULL and returns false. */
4286 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
4288 char *pos, *key, *value;
4292 pos += strspn(pos, ", \t\r\n");
4294 *keyp = *valuep = NULL;
4299 key_len = strcspn(pos, ":=(, \t\r\n");
4300 if (key[key_len] == ':' || key[key_len] == '=') {
4301 /* The value can be separated by a colon. */
4304 value = key + key_len + 1;
4305 value_len = strcspn(value, ", \t\r\n");
4306 pos = value + value_len + (value[value_len] != '\0');
4307 value[value_len] = '\0';
4308 } else if (key[key_len] == '(') {
4309 /* The value can be surrounded by balanced parentheses. The outermost
4310 * set of parentheses is removed. */
4314 value = key + key_len + 1;
4315 for (value_len = 0; level > 0; value_len++) {
4316 switch (value[value_len]) {
4330 value[value_len - 1] = '\0';
4331 pos = value + value_len;
4333 /* There might be no value at all. */
4334 value = key + key_len; /* Will become the empty string below. */
4335 pos = key + key_len + (key[key_len] != '\0');
4337 key[key_len] = '\0';
4345 /* Encode a dump ports request for 'port', the encoded message
4346 * will be for Open Flow version 'ofp_version'. Returns message
4347 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
4349 ofputil_encode_dump_ports_request(enum ofp_version ofp_version, int16_t port)
4351 struct ofpbuf *request;
4353 switch (ofp_version) {
4354 case OFP10_VERSION: {
4355 struct ofp10_port_stats_request *req;
4356 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0);
4357 req = ofpbuf_put_zeros(request, sizeof *req);
4358 req->port_no = htons(port);
4363 case OFP13_VERSION: {
4364 struct ofp11_port_stats_request *req;
4365 request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0);
4366 req = ofpbuf_put_zeros(request, sizeof *req);
4367 req->port_no = ofputil_port_to_ofp11(port);
4378 ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops,
4379 struct ofp10_port_stats *ps10)
4381 ps10->port_no = htons(ops->port_no);
4382 memset(ps10->pad, 0, sizeof ps10->pad);
4383 put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets));
4384 put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets));
4385 put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes));
4386 put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes));
4387 put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped));
4388 put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped));
4389 put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors));
4390 put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors));
4391 put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors));
4392 put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors));
4393 put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors));
4394 put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions));
4398 ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops,
4399 struct ofp11_port_stats *ps11)
4401 ps11->port_no = ofputil_port_to_ofp11(ops->port_no);
4402 memset(ps11->pad, 0, sizeof ps11->pad);
4403 ps11->rx_packets = htonll(ops->stats.rx_packets);
4404 ps11->tx_packets = htonll(ops->stats.tx_packets);
4405 ps11->rx_bytes = htonll(ops->stats.rx_bytes);
4406 ps11->tx_bytes = htonll(ops->stats.tx_bytes);
4407 ps11->rx_dropped = htonll(ops->stats.rx_dropped);
4408 ps11->tx_dropped = htonll(ops->stats.tx_dropped);
4409 ps11->rx_errors = htonll(ops->stats.rx_errors);
4410 ps11->tx_errors = htonll(ops->stats.tx_errors);
4411 ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors);
4412 ps11->rx_over_err = htonll(ops->stats.rx_over_errors);
4413 ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors);
4414 ps11->collisions = htonll(ops->stats.collisions);
4418 ofputil_port_stats_to_ofp13(const struct ofputil_port_stats *ops,
4419 struct ofp13_port_stats *ps13)
4421 ofputil_port_stats_to_ofp11(ops, &ps13->ps);
4423 /* OF 1.3 adds duration fields */
4424 /* FIXME: Need to implement port alive duration (sec + nsec) */
4425 ps13->duration_sec = htonl(~0);
4426 ps13->duration_nsec = htonl(~0);
4430 /* Encode a ports stat for 'ops' and append it to 'replies'. */
4432 ofputil_append_port_stat(struct list *replies,
4433 const struct ofputil_port_stats *ops)
4435 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
4436 struct ofp_header *oh = msg->data;
4438 switch ((enum ofp_version)oh->version) {
4439 case OFP13_VERSION: {
4440 struct ofp13_port_stats *reply = ofpmp_append(replies, sizeof *reply);
4441 ofputil_port_stats_to_ofp13(ops, reply);
4445 case OFP11_VERSION: {
4446 struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply);
4447 ofputil_port_stats_to_ofp11(ops, reply);
4451 case OFP10_VERSION: {
4452 struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply);
4453 ofputil_port_stats_to_ofp10(ops, reply);
4463 ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops,
4464 const struct ofp10_port_stats *ps10)
4466 memset(ops, 0, sizeof *ops);
4468 ops->port_no = ntohs(ps10->port_no);
4469 ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets));
4470 ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets));
4471 ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes));
4472 ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes));
4473 ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped));
4474 ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped));
4475 ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors));
4476 ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors));
4477 ops->stats.rx_frame_errors =
4478 ntohll(get_32aligned_be64(&ps10->rx_frame_err));
4479 ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err));
4480 ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err));
4481 ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions));
4487 ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops,
4488 const struct ofp11_port_stats *ps11)
4492 memset(ops, 0, sizeof *ops);
4493 error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no);
4498 ops->stats.rx_packets = ntohll(ps11->rx_packets);
4499 ops->stats.tx_packets = ntohll(ps11->tx_packets);
4500 ops->stats.rx_bytes = ntohll(ps11->rx_bytes);
4501 ops->stats.tx_bytes = ntohll(ps11->tx_bytes);
4502 ops->stats.rx_dropped = ntohll(ps11->rx_dropped);
4503 ops->stats.tx_dropped = ntohll(ps11->tx_dropped);
4504 ops->stats.rx_errors = ntohll(ps11->rx_errors);
4505 ops->stats.tx_errors = ntohll(ps11->tx_errors);
4506 ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err);
4507 ops->stats.rx_over_errors = ntohll(ps11->rx_over_err);
4508 ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err);
4509 ops->stats.collisions = ntohll(ps11->collisions);
4515 ofputil_port_stats_from_ofp13(struct ofputil_port_stats *ops,
4516 const struct ofp13_port_stats *ps13)
4519 ofputil_port_stats_from_ofp11(ops, &ps13->ps);
4521 /* FIXME: Get ps13->duration_sec and ps13->duration_nsec,
4522 * Add to netdev_stats? */
4529 /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
4532 ofputil_count_port_stats(const struct ofp_header *oh)
4536 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4537 ofpraw_pull_assert(&b);
4539 BUILD_ASSERT(sizeof(struct ofp10_port_stats) ==
4540 sizeof(struct ofp11_port_stats));
4541 return b.size / sizeof(struct ofp10_port_stats);
4544 /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
4545 * ofputil_port_stats in 'ps'.
4547 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
4548 * message. Calling this function multiple times for a single 'msg' iterates
4549 * through the replies. The caller must initially leave 'msg''s layer pointers
4550 * null and not modify them between calls.
4552 * Returns 0 if successful, EOF if no replies were left in this 'msg',
4553 * otherwise a positive errno value. */
4555 ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg)
4561 ? ofpraw_decode(&raw, msg->l2)
4562 : ofpraw_pull(&raw, msg));
4569 } else if (raw == OFPRAW_OFPST13_PORT_REPLY) {
4570 const struct ofp13_port_stats *ps13;
4572 ps13 = ofpbuf_try_pull(msg, sizeof *ps13);
4576 return ofputil_port_stats_from_ofp13(ps, ps13);
4577 } else if (raw == OFPRAW_OFPST11_PORT_REPLY) {
4578 const struct ofp11_port_stats *ps11;
4580 ps11 = ofpbuf_try_pull(msg, sizeof *ps11);
4584 return ofputil_port_stats_from_ofp11(ps, ps11);
4585 } else if (raw == OFPRAW_OFPST10_PORT_REPLY) {
4586 const struct ofp10_port_stats *ps10;
4588 ps10 = ofpbuf_try_pull(msg, sizeof *ps10);
4592 return ofputil_port_stats_from_ofp10(ps, ps10);
4598 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %zu leftover "
4599 "bytes at end", msg->size);
4600 return OFPERR_OFPBRC_BAD_LEN;
4603 /* Parse a port status request message into a 16 bit OpenFlow 1.0
4604 * port number and stores the latter in '*ofp10_port'.
4605 * Returns 0 if successful, otherwise an OFPERR_* number. */
4607 ofputil_decode_port_stats_request(const struct ofp_header *request,
4608 uint16_t *ofp10_port)
4610 switch ((enum ofp_version)request->version) {
4613 case OFP11_VERSION: {
4614 const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request);
4615 return ofputil_port_from_ofp11(psr11->port_no, ofp10_port);
4618 case OFP10_VERSION: {
4619 const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request);
4620 *ofp10_port = ntohs(psr10->port_no);
4629 /* Parse a queue status request message into 'oqsr'.
4630 * Returns 0 if successful, otherwise an OFPERR_* number. */
4632 ofputil_decode_queue_stats_request(const struct ofp_header *request,
4633 struct ofputil_queue_stats_request *oqsr)
4635 switch ((enum ofp_version)request->version) {
4638 case OFP11_VERSION: {
4639 const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request);
4640 oqsr->queue_id = ntohl(qsr11->queue_id);
4641 return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no);
4644 case OFP10_VERSION: {
4645 const struct ofp10_queue_stats_request *qsr10 = ofpmsg_body(request);
4646 oqsr->queue_id = ntohl(qsr10->queue_id);
4647 oqsr->port_no = ntohs(qsr10->port_no);
4648 /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
4649 if (oqsr->port_no == OFPP_ALL) {
4650 oqsr->port_no = OFPP_ANY;
4660 /* Encode a queue statsrequest for 'oqsr', the encoded message
4661 * will be fore Open Flow version 'ofp_version'. Returns message
4662 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
4664 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
4665 const struct ofputil_queue_stats_request *oqsr)
4667 struct ofpbuf *request;
4669 switch (ofp_version) {
4672 case OFP13_VERSION: {
4673 struct ofp11_queue_stats_request *req;
4674 request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0);
4675 req = ofpbuf_put_zeros(request, sizeof *req);
4676 req->port_no = ofputil_port_to_ofp11(oqsr->port_no);
4677 req->queue_id = htonl(oqsr->queue_id);
4680 case OFP10_VERSION: {
4681 struct ofp10_queue_stats_request *req;
4682 request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0);
4683 req = ofpbuf_put_zeros(request, sizeof *req);
4684 /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
4685 req->port_no = htons(oqsr->port_no == OFPP_ANY
4686 ? OFPP_ALL : oqsr->port_no);
4687 req->queue_id = htonl(oqsr->queue_id);
4697 /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
4700 ofputil_count_queue_stats(const struct ofp_header *oh)
4704 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4705 ofpraw_pull_assert(&b);
4707 BUILD_ASSERT(sizeof(struct ofp10_queue_stats) ==
4708 sizeof(struct ofp11_queue_stats));
4709 return b.size / sizeof(struct ofp10_queue_stats);
4713 ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs,
4714 const struct ofp10_queue_stats *qs10)
4716 oqs->port_no = ntohs(qs10->port_no);
4717 oqs->queue_id = ntohl(qs10->queue_id);
4718 oqs->stats.tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes));
4719 oqs->stats.tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets));
4720 oqs->stats.tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors));
4726 ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs,
4727 const struct ofp11_queue_stats *qs11)
4731 error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no);
4736 oqs->queue_id = ntohl(qs11->queue_id);
4737 oqs->stats.tx_bytes = ntohll(qs11->tx_bytes);
4738 oqs->stats.tx_packets = ntohll(qs11->tx_packets);
4739 oqs->stats.tx_errors = ntohll(qs11->tx_errors);
4745 ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats *oqs,
4746 const struct ofp13_queue_stats *qs13)
4749 = ofputil_queue_stats_from_ofp11(oqs, &qs13->qs);
4751 /* FIXME: Get qs13->duration_sec and qs13->duration_nsec,
4752 * Add to netdev_queue_stats? */
4758 /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
4759 * ofputil_queue_stats in 'qs'.
4761 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
4762 * message. Calling this function multiple times for a single 'msg' iterates
4763 * through the replies. The caller must initially leave 'msg''s layer pointers
4764 * null and not modify them between calls.
4766 * Returns 0 if successful, EOF if no replies were left in this 'msg',
4767 * otherwise a positive errno value. */
4769 ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg)
4775 ? ofpraw_decode(&raw, msg->l2)
4776 : ofpraw_pull(&raw, msg));
4783 } else if (raw == OFPRAW_OFPST13_QUEUE_REPLY) {
4784 const struct ofp13_queue_stats *qs13;
4786 qs13 = ofpbuf_try_pull(msg, sizeof *qs13);
4790 return ofputil_queue_stats_from_ofp13(qs, qs13);
4791 } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) {
4792 const struct ofp11_queue_stats *qs11;
4794 qs11 = ofpbuf_try_pull(msg, sizeof *qs11);
4798 return ofputil_queue_stats_from_ofp11(qs, qs11);
4799 } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) {
4800 const struct ofp10_queue_stats *qs10;
4802 qs10 = ofpbuf_try_pull(msg, sizeof *qs10);
4806 return ofputil_queue_stats_from_ofp10(qs, qs10);
4812 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %zu leftover "
4813 "bytes at end", msg->size);
4814 return OFPERR_OFPBRC_BAD_LEN;
4818 ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs,
4819 struct ofp10_queue_stats *qs10)
4821 qs10->port_no = htons(oqs->port_no);
4822 memset(qs10->pad, 0, sizeof qs10->pad);
4823 qs10->queue_id = htonl(oqs->queue_id);
4824 put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->stats.tx_bytes));
4825 put_32aligned_be64(&qs10->tx_packets, htonll(oqs->stats.tx_packets));
4826 put_32aligned_be64(&qs10->tx_errors, htonll(oqs->stats.tx_errors));
4830 ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs,
4831 struct ofp11_queue_stats *qs11)
4833 qs11->port_no = ofputil_port_to_ofp11(oqs->port_no);
4834 qs11->queue_id = htonl(oqs->queue_id);
4835 qs11->tx_bytes = htonll(oqs->stats.tx_bytes);
4836 qs11->tx_packets = htonll(oqs->stats.tx_packets);
4837 qs11->tx_errors = htonll(oqs->stats.tx_errors);
4841 ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats *oqs,
4842 struct ofp13_queue_stats *qs13)
4844 ofputil_queue_stats_to_ofp11(oqs, &qs13->qs);
4845 /* OF 1.3 adds duration fields */
4846 /* FIXME: Need to implement queue alive duration (sec + nsec) */
4847 qs13->duration_sec = htonl(~0);
4848 qs13->duration_nsec = htonl(~0);
4851 /* Encode a queue stat for 'oqs' and append it to 'replies'. */
4853 ofputil_append_queue_stat(struct list *replies,
4854 const struct ofputil_queue_stats *oqs)
4856 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
4857 struct ofp_header *oh = msg->data;
4859 switch ((enum ofp_version)oh->version) {
4860 case OFP13_VERSION: {
4861 struct ofp13_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
4862 ofputil_queue_stats_to_ofp13(oqs, reply);
4867 case OFP11_VERSION: {
4868 struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
4869 ofputil_queue_stats_to_ofp11(oqs, reply);
4873 case OFP10_VERSION: {
4874 struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
4875 ofputil_queue_stats_to_ofp10(oqs, reply);