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" },
588 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
590 enum ofputil_protocol ofputil_flow_dump_protocols[] = {
596 size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
598 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
599 * connection that has negotiated the given 'version'. 'version' should
600 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
601 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
602 * outside the valid range. */
603 enum ofputil_protocol
604 ofputil_protocol_from_ofp_version(enum ofp_version version)
608 return OFPUTIL_P_OF10_STD;
610 return OFPUTIL_P_OF12_OXM;
612 return OFPUTIL_P_OF13_OXM;
619 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
620 * etc.) that corresponds to 'protocol'. */
622 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
625 case OFPUTIL_P_OF10_STD:
626 case OFPUTIL_P_OF10_STD_TID:
627 case OFPUTIL_P_OF10_NXM:
628 case OFPUTIL_P_OF10_NXM_TID:
629 return OFP10_VERSION;
630 case OFPUTIL_P_OF12_OXM:
631 return OFP12_VERSION;
632 case OFPUTIL_P_OF13_OXM:
633 return OFP13_VERSION;
639 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
642 ofputil_protocol_is_valid(enum ofputil_protocol protocol)
644 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
647 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
648 * extension turned on or off if 'enable' is true or false, respectively.
650 * This extension is only useful for protocols whose "standard" version does
651 * not allow specific tables to be modified. In particular, this is true of
652 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
653 * specifies a table ID and so there is no need for such an extension. When
654 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
655 * extension, this function just returns its 'protocol' argument unchanged
656 * regardless of the value of 'enable'. */
657 enum ofputil_protocol
658 ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
661 case OFPUTIL_P_OF10_STD:
662 case OFPUTIL_P_OF10_STD_TID:
663 return enable ? OFPUTIL_P_OF10_STD_TID : OFPUTIL_P_OF10_STD;
665 case OFPUTIL_P_OF10_NXM:
666 case OFPUTIL_P_OF10_NXM_TID:
667 return enable ? OFPUTIL_P_OF10_NXM_TID : OFPUTIL_P_OF10_NXM;
669 case OFPUTIL_P_OF12_OXM:
670 return OFPUTIL_P_OF12_OXM;
672 case OFPUTIL_P_OF13_OXM:
673 return OFPUTIL_P_OF13_OXM;
680 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
681 * some extension to a standard protocol version, the return value is the
682 * standard version of that protocol without any extension. If 'protocol' is a
683 * standard protocol version, returns 'protocol' unchanged. */
684 enum ofputil_protocol
685 ofputil_protocol_to_base(enum ofputil_protocol protocol)
687 return ofputil_protocol_set_tid(protocol, false);
690 /* Returns 'new_base' with any extensions taken from 'cur'. */
691 enum ofputil_protocol
692 ofputil_protocol_set_base(enum ofputil_protocol cur,
693 enum ofputil_protocol new_base)
695 bool tid = (cur & OFPUTIL_P_TID) != 0;
698 case OFPUTIL_P_OF10_STD:
699 case OFPUTIL_P_OF10_STD_TID:
700 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD, tid);
702 case OFPUTIL_P_OF10_NXM:
703 case OFPUTIL_P_OF10_NXM_TID:
704 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM, tid);
706 case OFPUTIL_P_OF12_OXM:
707 return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM, tid);
709 case OFPUTIL_P_OF13_OXM:
710 return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM, tid);
717 /* Returns a string form of 'protocol', if a simple form exists (that is, if
718 * 'protocol' is either a single protocol or it is a combination of protocols
719 * that have a single abbreviation). Otherwise, returns NULL. */
721 ofputil_protocol_to_string(enum ofputil_protocol protocol)
723 const struct proto_abbrev *p;
725 /* Use a "switch" statement for single-bit names so that we get a compiler
726 * warning if we forget any. */
728 case OFPUTIL_P_OF10_NXM:
729 return "NXM-table_id";
731 case OFPUTIL_P_OF10_NXM_TID:
732 return "NXM+table_id";
734 case OFPUTIL_P_OF10_STD:
735 return "OpenFlow10-table_id";
737 case OFPUTIL_P_OF10_STD_TID:
738 return "OpenFlow10+table_id";
740 case OFPUTIL_P_OF12_OXM:
743 case OFPUTIL_P_OF13_OXM:
747 /* Check abbreviations. */
748 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
749 if (protocol == p->protocol) {
757 /* Returns a string that represents 'protocols'. The return value might be a
758 * comma-separated list if 'protocols' doesn't have a simple name. The return
759 * value is "none" if 'protocols' is 0.
761 * The caller must free the returned string (with free()). */
763 ofputil_protocols_to_string(enum ofputil_protocol protocols)
767 assert(!(protocols & ~OFPUTIL_P_ANY));
768 if (protocols == 0) {
769 return xstrdup("none");
774 const struct proto_abbrev *p;
778 ds_put_char(&s, ',');
781 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
782 if ((protocols & p->protocol) == p->protocol) {
783 ds_put_cstr(&s, p->name);
784 protocols &= ~p->protocol;
789 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
790 enum ofputil_protocol bit = 1u << i;
792 if (protocols & bit) {
793 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
802 return ds_steal_cstr(&s);
805 static enum ofputil_protocol
806 ofputil_protocol_from_string__(const char *s, size_t n)
808 const struct proto_abbrev *p;
811 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
812 enum ofputil_protocol bit = 1u << i;
813 const char *name = ofputil_protocol_to_string(bit);
815 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
820 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
821 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
829 /* Returns the nonempty set of protocols represented by 's', which can be a
830 * single protocol name or abbreviation or a comma-separated list of them.
832 * Aborts the program with an error message if 's' is invalid. */
833 enum ofputil_protocol
834 ofputil_protocols_from_string(const char *s)
836 const char *orig_s = s;
837 enum ofputil_protocol protocols;
841 enum ofputil_protocol p;
850 p = ofputil_protocol_from_string__(s, n);
852 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
860 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
866 ofputil_version_from_string(const char *s)
868 if (!strcasecmp(s, "OpenFlow10")) {
869 return OFP10_VERSION;
871 if (!strcasecmp(s, "OpenFlow11")) {
872 return OFP11_VERSION;
874 if (!strcasecmp(s, "OpenFlow12")) {
875 return OFP12_VERSION;
877 if (!strcasecmp(s, "OpenFlow13")) {
878 return OFP13_VERSION;
886 return isspace(c) || c == ',';
890 ofputil_versions_from_string(const char *s)
900 if (is_delimiter(s[i])) {
905 while (s[i + j] && !is_delimiter(s[i + j])) {
908 key = xmemdup0(s + i, j);
909 version = ofputil_version_from_string(key);
911 VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key);
914 bitmap |= 1u << version;
922 ofputil_versions_from_strings(char ** const s, size_t count)
927 int version = ofputil_version_from_string(s[count]);
929 VLOG_WARN("Unknown OpenFlow version: \"%s\"", s[count]);
931 bitmap |= 1u << version;
939 ofputil_version_to_string(enum ofp_version ofp_version)
941 switch (ofp_version) {
956 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
958 switch (packet_in_format) {
959 case NXPIF_OPENFLOW10:
968 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
970 switch (packet_in_format) {
971 case NXPIF_OPENFLOW10:
981 ofputil_packet_in_format_from_string(const char *s)
983 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
984 : !strcmp(s, "nxm") ? NXPIF_NXM
989 regs_fully_wildcarded(const struct flow_wildcards *wc)
993 for (i = 0; i < FLOW_N_REGS; i++) {
994 if (wc->masks.regs[i] != 0) {
1002 tun_parms_fully_wildcarded(const struct flow_wildcards *wc)
1004 return (!wc->masks.tunnel.ip_src &&
1005 !wc->masks.tunnel.ip_dst &&
1006 !wc->masks.tunnel.ip_ttl &&
1007 !wc->masks.tunnel.ip_tos &&
1008 !wc->masks.tunnel.flags);
1011 /* Returns a bit-mask of ofputil_protocols that can be used for sending 'match'
1012 * to a switch (e.g. to add or remove a flow). Only NXM can handle tunnel IDs,
1013 * registers, or fixing the Ethernet multicast bit. Otherwise, it's better to
1014 * use OpenFlow 1.0 protocol for backward compatibility. */
1015 enum ofputil_protocol
1016 ofputil_usable_protocols(const struct match *match)
1018 const struct flow_wildcards *wc = &match->wc;
1020 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 18);
1022 /* tunnel params other than tun_id can't be sent in a flow_mod */
1023 if (!tun_parms_fully_wildcarded(wc)) {
1024 return OFPUTIL_P_NONE;
1027 /* NXM, OXM, and OF1.1 support bitwise matching on ethernet addresses. */
1028 if (!eth_mask_is_exact(wc->masks.dl_src)
1029 && !eth_addr_is_zero(wc->masks.dl_src)) {
1030 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1031 | OFPUTIL_P_OF13_OXM;
1033 if (!eth_mask_is_exact(wc->masks.dl_dst)
1034 && !eth_addr_is_zero(wc->masks.dl_dst)) {
1035 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1036 | OFPUTIL_P_OF13_OXM;
1039 /* NXM, OXM, and OF1.1+ support matching metadata. */
1040 if (wc->masks.metadata != htonll(0)) {
1041 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1042 | OFPUTIL_P_OF13_OXM;
1045 /* NXM and OXM support matching ARP hardware addresses. */
1046 if (!eth_addr_is_zero(wc->masks.arp_sha) ||
1047 !eth_addr_is_zero(wc->masks.arp_tha)) {
1048 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1049 | OFPUTIL_P_OF13_OXM;
1052 /* NXM and OXM support matching IPv6 traffic. */
1053 if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
1054 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1055 | OFPUTIL_P_OF13_OXM;
1058 /* NXM and OXM support matching registers. */
1059 if (!regs_fully_wildcarded(wc)) {
1060 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1061 | OFPUTIL_P_OF13_OXM;
1064 /* NXM and OXM support matching tun_id. */
1065 if (wc->masks.tunnel.tun_id != htonll(0)) {
1066 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1067 | OFPUTIL_P_OF13_OXM;
1070 /* NXM and OXM support matching fragments. */
1071 if (wc->masks.nw_frag) {
1072 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1073 | OFPUTIL_P_OF13_OXM;
1076 /* NXM and OXM support matching IPv6 flow label. */
1077 if (wc->masks.ipv6_label) {
1078 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1079 | OFPUTIL_P_OF13_OXM;
1082 /* NXM and OXM support matching IP ECN bits. */
1083 if (wc->masks.nw_tos & IP_ECN_MASK) {
1084 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1085 | OFPUTIL_P_OF13_OXM;
1088 /* NXM and OXM support matching IP TTL/hop limit. */
1089 if (wc->masks.nw_ttl) {
1090 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1091 | OFPUTIL_P_OF13_OXM;
1094 /* NXM and OXM support non-CIDR IPv4 address masks. */
1095 if (!ip_is_cidr(wc->masks.nw_src) || !ip_is_cidr(wc->masks.nw_dst)) {
1096 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1097 | OFPUTIL_P_OF13_OXM;
1100 /* NXM and OXM support bitwise matching on transport port. */
1101 if ((wc->masks.tp_src && wc->masks.tp_src != htons(UINT16_MAX)) ||
1102 (wc->masks.tp_dst && wc->masks.tp_dst != htons(UINT16_MAX))) {
1103 return OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1104 | OFPUTIL_P_OF13_OXM;
1107 /* Other formats can express this rule. */
1108 return OFPUTIL_P_ANY;
1112 ofputil_format_version(struct ds *msg, enum ofp_version version)
1114 ds_put_format(msg, "0x%02x", version);
1118 ofputil_format_version_name(struct ds *msg, enum ofp_version version)
1120 ds_put_cstr(msg, ofputil_version_to_string(version));
1124 ofputil_format_version_bitmap__(struct ds *msg, uint32_t bitmap,
1125 void (*format_version)(struct ds *msg,
1129 format_version(msg, raw_ctz(bitmap));
1130 bitmap = zero_rightmost_1bit(bitmap);
1132 ds_put_cstr(msg, ", ");
1138 ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap)
1140 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version);
1144 ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap)
1146 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version_name);
1150 ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header *oheh,
1151 uint32_t *allowed_versionsp)
1153 uint16_t bitmap_len = ntohs(oheh->length) - sizeof *oheh;
1154 const ovs_be32 *bitmap = (const ovs_be32 *) (oheh + 1);
1155 uint32_t allowed_versions;
1157 if (!bitmap_len || bitmap_len % sizeof *bitmap) {
1161 /* Only use the first 32-bit element of the bitmap as that is all the
1162 * current implementation supports. Subsequent elements are ignored which
1163 * should have no effect on session negotiation until Open vSwtich supports
1164 * wire-protocol versions greater than 31.
1166 allowed_versions = ntohl(bitmap[0]);
1168 if (allowed_versions & 1) {
1169 /* There's no OpenFlow version 0. */
1170 VLOG_WARN_RL(&bad_ofmsg_rl, "peer claims to support invalid OpenFlow "
1172 allowed_versions &= ~1u;
1175 if (!allowed_versions) {
1176 VLOG_WARN_RL(&bad_ofmsg_rl, "peer does not support any OpenFlow "
1177 "version (between 0x01 and 0x1f)");
1181 *allowed_versionsp = allowed_versions;
1186 version_bitmap_from_version(uint8_t ofp_version)
1188 return ((ofp_version < 32 ? 1u << ofp_version : 0) - 1) << 1;
1191 /* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions'
1192 * the set of OpenFlow versions for which 'oh' announces support.
1194 * Because of how OpenFlow defines OFPT_HELLO messages, this function is always
1195 * successful, and thus '*allowed_versions' is always initialized. However, it
1196 * returns false if 'oh' contains some data that could not be fully understood,
1197 * true if 'oh' was completely parsed. */
1199 ofputil_decode_hello(const struct ofp_header *oh, uint32_t *allowed_versions)
1204 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
1205 ofpbuf_pull(&msg, sizeof *oh);
1207 *allowed_versions = version_bitmap_from_version(oh->version);
1209 const struct ofp_hello_elem_header *oheh;
1212 if (msg.size < sizeof *oheh) {
1217 len = ntohs(oheh->length);
1218 if (len < sizeof *oheh || !ofpbuf_try_pull(&msg, ROUND_UP(len, 8))) {
1222 if (oheh->type != htons(OFPHET_VERSIONBITMAP)
1223 || !ofputil_decode_hello_bitmap(oheh, allowed_versions)) {
1231 /* Returns true if 'allowed_versions' needs to be accompanied by a version
1232 * bitmap to be correctly expressed in an OFPT_HELLO message. */
1234 should_send_version_bitmap(uint32_t allowed_versions)
1236 return !is_pow2((allowed_versions >> 1) + 1);
1239 /* Create an OFPT_HELLO message that expresses support for the OpenFlow
1240 * versions in the 'allowed_versions' bitmaps and returns the message. */
1242 ofputil_encode_hello(uint32_t allowed_versions)
1244 enum ofp_version ofp_version;
1247 ofp_version = leftmost_1bit_idx(allowed_versions);
1248 msg = ofpraw_alloc(OFPRAW_OFPT_HELLO, ofp_version, 0);
1250 if (should_send_version_bitmap(allowed_versions)) {
1251 struct ofp_hello_elem_header *oheh;
1254 map_len = sizeof allowed_versions;
1255 oheh = ofpbuf_put_zeros(msg, ROUND_UP(map_len + sizeof *oheh, 8));
1256 oheh->type = htons(OFPHET_VERSIONBITMAP);
1257 oheh->length = htons(map_len + sizeof *oheh);
1258 *(ovs_be32 *)(oheh + 1) = htonl(allowed_versions);
1260 ofpmsg_update_length(msg);
1266 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1267 * protocol is 'current', at least partly transitions the protocol to 'want'.
1268 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1269 * connection if the switch processes the returned message correctly. (If
1270 * '*next != want' then the caller will have to iterate.)
1272 * If 'current == want', or if it is not possible to transition from 'current'
1273 * to 'want' (because, for example, 'current' and 'want' use different OpenFlow
1274 * protocol versions), returns NULL and stores 'current' in '*next'. */
1276 ofputil_encode_set_protocol(enum ofputil_protocol current,
1277 enum ofputil_protocol want,
1278 enum ofputil_protocol *next)
1280 enum ofp_version cur_version, want_version;
1281 enum ofputil_protocol cur_base, want_base;
1282 bool cur_tid, want_tid;
1284 cur_version = ofputil_protocol_to_ofp_version(current);
1285 want_version = ofputil_protocol_to_ofp_version(want);
1286 if (cur_version != want_version) {
1291 cur_base = ofputil_protocol_to_base(current);
1292 want_base = ofputil_protocol_to_base(want);
1293 if (cur_base != want_base) {
1294 *next = ofputil_protocol_set_base(current, want_base);
1296 switch (want_base) {
1297 case OFPUTIL_P_OF10_NXM:
1298 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1300 case OFPUTIL_P_OF10_STD:
1301 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1303 case OFPUTIL_P_OF12_OXM:
1304 case OFPUTIL_P_OF13_OXM:
1305 /* There are only one of each OpenFlow 1.2+ protocols and we already
1306 * verified above that we're not trying to change versions. */
1309 case OFPUTIL_P_OF10_STD_TID:
1310 case OFPUTIL_P_OF10_NXM_TID:
1315 cur_tid = (current & OFPUTIL_P_TID) != 0;
1316 want_tid = (want & OFPUTIL_P_TID) != 0;
1317 if (cur_tid != want_tid) {
1318 *next = ofputil_protocol_set_tid(current, want_tid);
1319 return ofputil_make_flow_mod_table_id(want_tid);
1322 assert(current == want);
1328 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1329 * format to 'nxff'. */
1331 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1333 struct nx_set_flow_format *sff;
1336 assert(ofputil_nx_flow_format_is_valid(nxff));
1338 msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1339 sff = ofpbuf_put_zeros(msg, sizeof *sff);
1340 sff->format = htonl(nxff);
1345 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1347 enum ofputil_protocol
1348 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1350 switch (flow_format) {
1351 case NXFF_OPENFLOW10:
1352 return OFPUTIL_P_OF10_STD;
1355 return OFPUTIL_P_OF10_NXM;
1362 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1364 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1366 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1369 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1372 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1374 switch (flow_format) {
1375 case NXFF_OPENFLOW10:
1376 return "openflow10";
1385 ofputil_make_set_packet_in_format(enum ofp_version ofp_version,
1386 enum nx_packet_in_format packet_in_format)
1388 struct nx_set_packet_in_format *spif;
1391 msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, ofp_version, 0);
1392 spif = ofpbuf_put_zeros(msg, sizeof *spif);
1393 spif->format = htonl(packet_in_format);
1398 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1399 * extension on or off (according to 'flow_mod_table_id'). */
1401 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1403 struct nx_flow_mod_table_id *nfmti;
1406 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1407 nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
1408 nfmti->set = flow_mod_table_id;
1412 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1413 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1416 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1417 * The caller must initialize 'ofpacts' and retains ownership of it.
1418 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1420 * Does not validate the flow_mod actions. The caller should do that, with
1421 * ofpacts_check(). */
1423 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1424 const struct ofp_header *oh,
1425 enum ofputil_protocol protocol,
1426 struct ofpbuf *ofpacts)
1432 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1433 raw = ofpraw_pull_assert(&b);
1434 if (raw == OFPRAW_OFPT11_FLOW_MOD) {
1435 /* Standard OpenFlow 1.1 flow_mod. */
1436 const struct ofp11_flow_mod *ofm;
1439 ofm = ofpbuf_pull(&b, sizeof *ofm);
1441 error = ofputil_pull_ofp11_match(&b, &fm->match, NULL);
1446 error = ofpacts_pull_openflow11_instructions(&b, b.size, ofpacts);
1451 /* Translate the message. */
1452 fm->priority = ntohs(ofm->priority);
1453 if (ofm->command == OFPFC_ADD) {
1454 fm->cookie = htonll(0);
1455 fm->cookie_mask = htonll(0);
1456 fm->new_cookie = ofm->cookie;
1458 fm->cookie = ofm->cookie;
1459 fm->cookie_mask = ofm->cookie_mask;
1460 fm->new_cookie = htonll(UINT64_MAX);
1462 fm->command = ofm->command;
1463 fm->table_id = ofm->table_id;
1464 fm->idle_timeout = ntohs(ofm->idle_timeout);
1465 fm->hard_timeout = ntohs(ofm->hard_timeout);
1466 fm->buffer_id = ntohl(ofm->buffer_id);
1467 error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
1471 if ((ofm->command == OFPFC_DELETE
1472 || ofm->command == OFPFC_DELETE_STRICT)
1473 && ofm->out_group != htonl(OFPG_ANY)) {
1474 return OFPERR_OFPFMFC_UNKNOWN;
1476 fm->flags = ntohs(ofm->flags);
1478 if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1479 /* Standard OpenFlow 1.0 flow_mod. */
1480 const struct ofp10_flow_mod *ofm;
1483 /* Get the ofp10_flow_mod. */
1484 ofm = ofpbuf_pull(&b, sizeof *ofm);
1486 /* Translate the rule. */
1487 ofputil_match_from_ofp10_match(&ofm->match, &fm->match);
1488 ofputil_normalize_match(&fm->match);
1490 /* Now get the actions. */
1491 error = ofpacts_pull_openflow10(&b, b.size, ofpacts);
1496 /* OpenFlow 1.0 says that exact-match rules have to have the
1497 * highest possible priority. */
1498 fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL)
1499 ? ntohs(ofm->priority)
1502 /* Translate the message. */
1503 command = ntohs(ofm->command);
1504 fm->cookie = htonll(0);
1505 fm->cookie_mask = htonll(0);
1506 fm->new_cookie = ofm->cookie;
1507 fm->idle_timeout = ntohs(ofm->idle_timeout);
1508 fm->hard_timeout = ntohs(ofm->hard_timeout);
1509 fm->buffer_id = ntohl(ofm->buffer_id);
1510 fm->out_port = ntohs(ofm->out_port);
1511 fm->flags = ntohs(ofm->flags);
1512 } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1513 /* Nicira extended flow_mod. */
1514 const struct nx_flow_mod *nfm;
1517 /* Dissect the message. */
1518 nfm = ofpbuf_pull(&b, sizeof *nfm);
1519 error = nx_pull_match(&b, ntohs(nfm->match_len),
1520 &fm->match, &fm->cookie, &fm->cookie_mask);
1524 error = ofpacts_pull_openflow10(&b, b.size, ofpacts);
1529 /* Translate the message. */
1530 command = ntohs(nfm->command);
1531 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1532 /* Flow additions may only set a new cookie, not match an
1533 * existing cookie. */
1534 return OFPERR_NXBRC_NXM_INVALID;
1536 fm->priority = ntohs(nfm->priority);
1537 fm->new_cookie = nfm->cookie;
1538 fm->idle_timeout = ntohs(nfm->idle_timeout);
1539 fm->hard_timeout = ntohs(nfm->hard_timeout);
1540 fm->buffer_id = ntohl(nfm->buffer_id);
1541 fm->out_port = ntohs(nfm->out_port);
1542 fm->flags = ntohs(nfm->flags);
1547 if (fm->flags & OFPFF10_EMERG) {
1548 /* We do not support the OpenFlow 1.0 emergency flow cache, which
1549 * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
1550 * There is no good error code, so just state that the flow table
1552 * Moreover, OFPFF10_EMERG overlaps with OFPFF12_RESET_COUNTS,
1553 * so this check must be here */
1554 return OFPERR_OFPFMFC_TABLE_FULL;
1557 if (protocol & OFPUTIL_P_TID) {
1558 fm->command = command & 0xff;
1559 fm->table_id = command >> 8;
1561 fm->command = command;
1562 fm->table_id = 0xff;
1566 fm->ofpacts = ofpacts->data;
1567 fm->ofpacts_len = ofpacts->size;
1573 ofputil_tid_command(const struct ofputil_flow_mod *fm,
1574 enum ofputil_protocol protocol)
1576 return htons(protocol & OFPUTIL_P_TID
1577 ? (fm->command & 0xff) | (fm->table_id << 8)
1581 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
1582 * 'protocol' and returns the message. */
1584 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
1585 enum ofputil_protocol protocol)
1590 case OFPUTIL_P_OF12_OXM:
1591 case OFPUTIL_P_OF13_OXM: {
1592 struct ofp11_flow_mod *ofm;
1594 msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD,
1595 ofputil_protocol_to_ofp_version(protocol),
1596 NXM_TYPICAL_LEN + fm->ofpacts_len);
1597 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
1598 if (fm->command == OFPFC_ADD) {
1599 ofm->cookie = fm->new_cookie;
1601 ofm->cookie = fm->cookie;
1603 ofm->cookie_mask = fm->cookie_mask;
1604 ofm->table_id = fm->table_id;
1605 ofm->command = fm->command;
1606 ofm->idle_timeout = htons(fm->idle_timeout);
1607 ofm->hard_timeout = htons(fm->hard_timeout);
1608 ofm->priority = htons(fm->priority);
1609 ofm->buffer_id = htonl(fm->buffer_id);
1610 ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
1611 ofm->out_group = htonl(OFPG11_ANY);
1612 ofm->flags = htons(fm->flags);
1613 oxm_put_match(msg, &fm->match);
1614 ofpacts_put_openflow11_instructions(fm->ofpacts, fm->ofpacts_len, msg);
1618 case OFPUTIL_P_OF10_STD:
1619 case OFPUTIL_P_OF10_STD_TID: {
1620 struct ofp10_flow_mod *ofm;
1622 msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
1624 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
1625 ofputil_match_to_ofp10_match(&fm->match, &ofm->match);
1626 ofm->cookie = fm->new_cookie;
1627 ofm->command = ofputil_tid_command(fm, protocol);
1628 ofm->idle_timeout = htons(fm->idle_timeout);
1629 ofm->hard_timeout = htons(fm->hard_timeout);
1630 ofm->priority = htons(fm->priority);
1631 ofm->buffer_id = htonl(fm->buffer_id);
1632 ofm->out_port = htons(fm->out_port);
1633 ofm->flags = htons(fm->flags);
1634 ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
1638 case OFPUTIL_P_OF10_NXM:
1639 case OFPUTIL_P_OF10_NXM_TID: {
1640 struct nx_flow_mod *nfm;
1643 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
1644 NXM_TYPICAL_LEN + fm->ofpacts_len);
1645 nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
1646 nfm->command = ofputil_tid_command(fm, protocol);
1647 nfm->cookie = fm->new_cookie;
1648 match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask);
1650 nfm->idle_timeout = htons(fm->idle_timeout);
1651 nfm->hard_timeout = htons(fm->hard_timeout);
1652 nfm->priority = htons(fm->priority);
1653 nfm->buffer_id = htonl(fm->buffer_id);
1654 nfm->out_port = htons(fm->out_port);
1655 nfm->flags = htons(fm->flags);
1656 nfm->match_len = htons(match_len);
1657 ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
1665 ofpmsg_update_length(msg);
1669 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1670 * send all of the 'n_fm's flow table modification requests in 'fms', and a
1671 * 0-bit for each protocol that is inadequate.
1673 * (The return value will have at least one 1-bit.) */
1674 enum ofputil_protocol
1675 ofputil_flow_mod_usable_protocols(const struct ofputil_flow_mod *fms,
1678 enum ofputil_protocol usable_protocols;
1681 usable_protocols = OFPUTIL_P_ANY;
1682 for (i = 0; i < n_fms; i++) {
1683 const struct ofputil_flow_mod *fm = &fms[i];
1685 usable_protocols &= ofputil_usable_protocols(&fm->match);
1686 if (fm->table_id != 0xff) {
1687 usable_protocols &= OFPUTIL_P_TID;
1690 /* Matching of the cookie is only supported through NXM or OF1.1+. */
1691 if (fm->cookie_mask != htonll(0)) {
1692 usable_protocols &= OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1693 | OFPUTIL_P_OF13_OXM;
1697 return usable_protocols;
1701 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr,
1702 const struct ofp10_flow_stats_request *ofsr,
1705 fsr->aggregate = aggregate;
1706 ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match);
1707 fsr->out_port = ntohs(ofsr->out_port);
1708 fsr->table_id = ofsr->table_id;
1709 fsr->cookie = fsr->cookie_mask = htonll(0);
1715 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr,
1716 struct ofpbuf *b, bool aggregate)
1718 const struct ofp11_flow_stats_request *ofsr;
1721 ofsr = ofpbuf_pull(b, sizeof *ofsr);
1722 fsr->aggregate = aggregate;
1723 fsr->table_id = ofsr->table_id;
1724 error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port);
1728 if (ofsr->out_group != htonl(OFPG11_ANY)) {
1729 return OFPERR_OFPFMFC_UNKNOWN;
1731 fsr->cookie = ofsr->cookie;
1732 fsr->cookie_mask = ofsr->cookie_mask;
1733 error = ofputil_pull_ofp11_match(b, &fsr->match, NULL);
1742 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
1743 struct ofpbuf *b, bool aggregate)
1745 const struct nx_flow_stats_request *nfsr;
1748 nfsr = ofpbuf_pull(b, sizeof *nfsr);
1749 error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match,
1750 &fsr->cookie, &fsr->cookie_mask);
1755 return OFPERR_OFPBRC_BAD_LEN;
1758 fsr->aggregate = aggregate;
1759 fsr->out_port = ntohs(nfsr->out_port);
1760 fsr->table_id = nfsr->table_id;
1765 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1766 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1767 * successful, otherwise an OpenFlow error code. */
1769 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
1770 const struct ofp_header *oh)
1775 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1776 raw = ofpraw_pull_assert(&b);
1777 switch ((int) raw) {
1778 case OFPRAW_OFPST10_FLOW_REQUEST:
1779 return ofputil_decode_ofpst10_flow_request(fsr, b.data, false);
1781 case OFPRAW_OFPST10_AGGREGATE_REQUEST:
1782 return ofputil_decode_ofpst10_flow_request(fsr, b.data, true);
1784 case OFPRAW_OFPST11_FLOW_REQUEST:
1785 return ofputil_decode_ofpst11_flow_request(fsr, &b, false);
1787 case OFPRAW_OFPST11_AGGREGATE_REQUEST:
1788 return ofputil_decode_ofpst11_flow_request(fsr, &b, true);
1790 case OFPRAW_NXST_FLOW_REQUEST:
1791 return ofputil_decode_nxst_flow_request(fsr, &b, false);
1793 case OFPRAW_NXST_AGGREGATE_REQUEST:
1794 return ofputil_decode_nxst_flow_request(fsr, &b, true);
1797 /* Hey, the caller lied. */
1802 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1803 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1804 * 'protocol', and returns the message. */
1806 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
1807 enum ofputil_protocol protocol)
1813 case OFPUTIL_P_OF12_OXM:
1814 case OFPUTIL_P_OF13_OXM: {
1815 struct ofp11_flow_stats_request *ofsr;
1817 raw = (fsr->aggregate
1818 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
1819 : OFPRAW_OFPST11_FLOW_REQUEST);
1820 msg = ofpraw_alloc(raw, ofputil_protocol_to_ofp_version(protocol),
1822 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
1823 ofsr->table_id = fsr->table_id;
1824 ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port);
1825 ofsr->out_group = htonl(OFPG11_ANY);
1826 ofsr->cookie = fsr->cookie;
1827 ofsr->cookie_mask = fsr->cookie_mask;
1828 oxm_put_match(msg, &fsr->match);
1832 case OFPUTIL_P_OF10_STD:
1833 case OFPUTIL_P_OF10_STD_TID: {
1834 struct ofp10_flow_stats_request *ofsr;
1836 raw = (fsr->aggregate
1837 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
1838 : OFPRAW_OFPST10_FLOW_REQUEST);
1839 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
1840 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
1841 ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match);
1842 ofsr->table_id = fsr->table_id;
1843 ofsr->out_port = htons(fsr->out_port);
1847 case OFPUTIL_P_OF10_NXM:
1848 case OFPUTIL_P_OF10_NXM_TID: {
1849 struct nx_flow_stats_request *nfsr;
1852 raw = (fsr->aggregate
1853 ? OFPRAW_NXST_AGGREGATE_REQUEST
1854 : OFPRAW_NXST_FLOW_REQUEST);
1855 msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN);
1856 ofpbuf_put_zeros(msg, sizeof *nfsr);
1857 match_len = nx_put_match(msg, &fsr->match,
1858 fsr->cookie, fsr->cookie_mask);
1861 nfsr->out_port = htons(fsr->out_port);
1862 nfsr->match_len = htons(match_len);
1863 nfsr->table_id = fsr->table_id;
1874 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1875 * accurately encode 'fsr', and a 0-bit for each protocol that is inadequate.
1877 * (The return value will have at least one 1-bit.) */
1878 enum ofputil_protocol
1879 ofputil_flow_stats_request_usable_protocols(
1880 const struct ofputil_flow_stats_request *fsr)
1882 enum ofputil_protocol usable_protocols;
1884 usable_protocols = ofputil_usable_protocols(&fsr->match);
1885 if (fsr->cookie_mask != htonll(0)) {
1886 usable_protocols &= OFPUTIL_P_OF10_NXM_ANY | OFPUTIL_P_OF12_OXM
1887 | OFPUTIL_P_OF13_OXM;
1889 return usable_protocols;
1892 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1893 * ofputil_flow_stats in 'fs'.
1895 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1896 * OpenFlow message. Calling this function multiple times for a single 'msg'
1897 * iterates through the replies. The caller must initially leave 'msg''s layer
1898 * pointers null and not modify them between calls.
1900 * Most switches don't send the values needed to populate fs->idle_age and
1901 * fs->hard_age, so those members will usually be set to 0. If the switch from
1902 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
1903 * 'flow_age_extension' as true so that the contents of 'msg' determine the
1904 * 'idle_age' and 'hard_age' members in 'fs'.
1906 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
1907 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
1908 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
1910 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1911 * otherwise a positive errno value. */
1913 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1915 bool flow_age_extension,
1916 struct ofpbuf *ofpacts)
1922 ? ofpraw_decode(&raw, msg->l2)
1923 : ofpraw_pull(&raw, msg));
1930 } else if (raw == OFPRAW_OFPST11_FLOW_REPLY
1931 || raw == OFPRAW_OFPST13_FLOW_REPLY) {
1932 const struct ofp11_flow_stats *ofs;
1934 uint16_t padded_match_len;
1936 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1938 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1939 "bytes at end", msg->size);
1943 length = ntohs(ofs->length);
1944 if (length < sizeof *ofs) {
1945 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1946 "length %zu", length);
1950 if (ofputil_pull_ofp11_match(msg, &fs->match, &padded_match_len)) {
1951 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match");
1955 if (ofpacts_pull_openflow11_instructions(msg, length - sizeof *ofs -
1956 padded_match_len, ofpacts)) {
1957 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions");
1961 fs->priority = ntohs(ofs->priority);
1962 fs->table_id = ofs->table_id;
1963 fs->duration_sec = ntohl(ofs->duration_sec);
1964 fs->duration_nsec = ntohl(ofs->duration_nsec);
1965 fs->idle_timeout = ntohs(ofs->idle_timeout);
1966 fs->hard_timeout = ntohs(ofs->hard_timeout);
1967 fs->flags = (raw == OFPRAW_OFPST13_FLOW_REPLY) ? ntohs(ofs->flags) : 0;
1970 fs->cookie = ofs->cookie;
1971 fs->packet_count = ntohll(ofs->packet_count);
1972 fs->byte_count = ntohll(ofs->byte_count);
1973 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
1974 const struct ofp10_flow_stats *ofs;
1977 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1979 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1980 "bytes at end", msg->size);
1984 length = ntohs(ofs->length);
1985 if (length < sizeof *ofs) {
1986 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1987 "length %zu", length);
1991 if (ofpacts_pull_openflow10(msg, length - sizeof *ofs, ofpacts)) {
1995 fs->cookie = get_32aligned_be64(&ofs->cookie);
1996 ofputil_match_from_ofp10_match(&ofs->match, &fs->match);
1997 fs->priority = ntohs(ofs->priority);
1998 fs->table_id = ofs->table_id;
1999 fs->duration_sec = ntohl(ofs->duration_sec);
2000 fs->duration_nsec = ntohl(ofs->duration_nsec);
2001 fs->idle_timeout = ntohs(ofs->idle_timeout);
2002 fs->hard_timeout = ntohs(ofs->hard_timeout);
2005 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
2006 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
2008 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
2009 const struct nx_flow_stats *nfs;
2010 size_t match_len, actions_len, length;
2012 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
2014 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
2015 "bytes at end", msg->size);
2019 length = ntohs(nfs->length);
2020 match_len = ntohs(nfs->match_len);
2021 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
2022 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
2023 "claims invalid length %zu", match_len, length);
2026 if (nx_pull_match(msg, match_len, &fs->match, NULL, NULL)) {
2030 actions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
2031 if (ofpacts_pull_openflow10(msg, actions_len, ofpacts)) {
2035 fs->cookie = nfs->cookie;
2036 fs->table_id = nfs->table_id;
2037 fs->duration_sec = ntohl(nfs->duration_sec);
2038 fs->duration_nsec = ntohl(nfs->duration_nsec);
2039 fs->priority = ntohs(nfs->priority);
2040 fs->idle_timeout = ntohs(nfs->idle_timeout);
2041 fs->hard_timeout = ntohs(nfs->hard_timeout);
2044 if (flow_age_extension) {
2045 if (nfs->idle_age) {
2046 fs->idle_age = ntohs(nfs->idle_age) - 1;
2048 if (nfs->hard_age) {
2049 fs->hard_age = ntohs(nfs->hard_age) - 1;
2052 fs->packet_count = ntohll(nfs->packet_count);
2053 fs->byte_count = ntohll(nfs->byte_count);
2059 fs->ofpacts = ofpacts->data;
2060 fs->ofpacts_len = ofpacts->size;
2065 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
2067 * We use this in situations where OVS internally uses UINT64_MAX to mean
2068 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
2070 unknown_to_zero(uint64_t count)
2072 return count != UINT64_MAX ? count : 0;
2075 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
2076 * those already present in the list of ofpbufs in 'replies'. 'replies' should
2077 * have been initialized with ofputil_start_stats_reply(). */
2079 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
2080 struct list *replies)
2082 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
2083 size_t start_ofs = reply->size;
2086 ofpraw_decode_partial(&raw, reply->data, reply->size);
2087 if (raw == OFPRAW_OFPST11_FLOW_REPLY || raw == OFPRAW_OFPST13_FLOW_REPLY) {
2088 struct ofp11_flow_stats *ofs;
2090 ofpbuf_put_uninit(reply, sizeof *ofs);
2091 oxm_put_match(reply, &fs->match);
2092 ofpacts_put_openflow11_instructions(fs->ofpacts, fs->ofpacts_len,
2095 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
2096 ofs->length = htons(reply->size - start_ofs);
2097 ofs->table_id = fs->table_id;
2099 ofs->duration_sec = htonl(fs->duration_sec);
2100 ofs->duration_nsec = htonl(fs->duration_nsec);
2101 ofs->priority = htons(fs->priority);
2102 ofs->idle_timeout = htons(fs->idle_timeout);
2103 ofs->hard_timeout = htons(fs->hard_timeout);
2104 ofs->flags = (raw == OFPRAW_OFPST13_FLOW_REPLY) ? htons(fs->flags) : 0;
2105 memset(ofs->pad2, 0, sizeof ofs->pad2);
2106 ofs->cookie = fs->cookie;
2107 ofs->packet_count = htonll(unknown_to_zero(fs->packet_count));
2108 ofs->byte_count = htonll(unknown_to_zero(fs->byte_count));
2109 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
2110 struct ofp10_flow_stats *ofs;
2112 ofpbuf_put_uninit(reply, sizeof *ofs);
2113 ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply);
2115 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
2116 ofs->length = htons(reply->size - start_ofs);
2117 ofs->table_id = fs->table_id;
2119 ofputil_match_to_ofp10_match(&fs->match, &ofs->match);
2120 ofs->duration_sec = htonl(fs->duration_sec);
2121 ofs->duration_nsec = htonl(fs->duration_nsec);
2122 ofs->priority = htons(fs->priority);
2123 ofs->idle_timeout = htons(fs->idle_timeout);
2124 ofs->hard_timeout = htons(fs->hard_timeout);
2125 memset(ofs->pad2, 0, sizeof ofs->pad2);
2126 put_32aligned_be64(&ofs->cookie, fs->cookie);
2127 put_32aligned_be64(&ofs->packet_count,
2128 htonll(unknown_to_zero(fs->packet_count)));
2129 put_32aligned_be64(&ofs->byte_count,
2130 htonll(unknown_to_zero(fs->byte_count)));
2131 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
2132 struct nx_flow_stats *nfs;
2135 ofpbuf_put_uninit(reply, sizeof *nfs);
2136 match_len = nx_put_match(reply, &fs->match, 0, 0);
2137 ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply);
2139 nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
2140 nfs->length = htons(reply->size - start_ofs);
2141 nfs->table_id = fs->table_id;
2143 nfs->duration_sec = htonl(fs->duration_sec);
2144 nfs->duration_nsec = htonl(fs->duration_nsec);
2145 nfs->priority = htons(fs->priority);
2146 nfs->idle_timeout = htons(fs->idle_timeout);
2147 nfs->hard_timeout = htons(fs->hard_timeout);
2148 nfs->idle_age = htons(fs->idle_age < 0 ? 0
2149 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
2151 nfs->hard_age = htons(fs->hard_age < 0 ? 0
2152 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
2154 nfs->match_len = htons(match_len);
2155 nfs->cookie = fs->cookie;
2156 nfs->packet_count = htonll(fs->packet_count);
2157 nfs->byte_count = htonll(fs->byte_count);
2162 ofpmp_postappend(replies, start_ofs);
2165 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
2166 * NXST_AGGREGATE reply matching 'request', and returns the message. */
2168 ofputil_encode_aggregate_stats_reply(
2169 const struct ofputil_aggregate_stats *stats,
2170 const struct ofp_header *request)
2172 struct ofp_aggregate_stats_reply *asr;
2173 uint64_t packet_count;
2174 uint64_t byte_count;
2178 ofpraw_decode(&raw, request);
2179 if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) {
2180 packet_count = unknown_to_zero(stats->packet_count);
2181 byte_count = unknown_to_zero(stats->byte_count);
2183 packet_count = stats->packet_count;
2184 byte_count = stats->byte_count;
2187 msg = ofpraw_alloc_stats_reply(request, 0);
2188 asr = ofpbuf_put_zeros(msg, sizeof *asr);
2189 put_32aligned_be64(&asr->packet_count, htonll(packet_count));
2190 put_32aligned_be64(&asr->byte_count, htonll(byte_count));
2191 asr->flow_count = htonl(stats->flow_count);
2197 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
2198 const struct ofp_header *reply)
2200 struct ofp_aggregate_stats_reply *asr;
2203 ofpbuf_use_const(&msg, reply, ntohs(reply->length));
2204 ofpraw_pull_assert(&msg);
2207 stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
2208 stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
2209 stats->flow_count = ntohl(asr->flow_count);
2214 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
2215 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
2216 * an OpenFlow error code. */
2218 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
2219 const struct ofp_header *oh)
2224 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2225 raw = ofpraw_pull_assert(&b);
2226 if (raw == OFPRAW_OFPT11_FLOW_REMOVED) {
2227 const struct ofp12_flow_removed *ofr;
2230 ofr = ofpbuf_pull(&b, sizeof *ofr);
2232 error = ofputil_pull_ofp11_match(&b, &fr->match, NULL);
2237 fr->priority = ntohs(ofr->priority);
2238 fr->cookie = ofr->cookie;
2239 fr->reason = ofr->reason;
2240 fr->table_id = ofr->table_id;
2241 fr->duration_sec = ntohl(ofr->duration_sec);
2242 fr->duration_nsec = ntohl(ofr->duration_nsec);
2243 fr->idle_timeout = ntohs(ofr->idle_timeout);
2244 fr->hard_timeout = ntohs(ofr->hard_timeout);
2245 fr->packet_count = ntohll(ofr->packet_count);
2246 fr->byte_count = ntohll(ofr->byte_count);
2247 } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
2248 const struct ofp_flow_removed *ofr;
2250 ofr = ofpbuf_pull(&b, sizeof *ofr);
2252 ofputil_match_from_ofp10_match(&ofr->match, &fr->match);
2253 fr->priority = ntohs(ofr->priority);
2254 fr->cookie = ofr->cookie;
2255 fr->reason = ofr->reason;
2257 fr->duration_sec = ntohl(ofr->duration_sec);
2258 fr->duration_nsec = ntohl(ofr->duration_nsec);
2259 fr->idle_timeout = ntohs(ofr->idle_timeout);
2260 fr->hard_timeout = 0;
2261 fr->packet_count = ntohll(ofr->packet_count);
2262 fr->byte_count = ntohll(ofr->byte_count);
2263 } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
2264 struct nx_flow_removed *nfr;
2267 nfr = ofpbuf_pull(&b, sizeof *nfr);
2268 error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match,
2274 return OFPERR_OFPBRC_BAD_LEN;
2277 fr->priority = ntohs(nfr->priority);
2278 fr->cookie = nfr->cookie;
2279 fr->reason = nfr->reason;
2281 fr->duration_sec = ntohl(nfr->duration_sec);
2282 fr->duration_nsec = ntohl(nfr->duration_nsec);
2283 fr->idle_timeout = ntohs(nfr->idle_timeout);
2284 fr->hard_timeout = 0;
2285 fr->packet_count = ntohll(nfr->packet_count);
2286 fr->byte_count = ntohll(nfr->byte_count);
2294 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
2295 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
2298 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
2299 enum ofputil_protocol protocol)
2304 case OFPUTIL_P_OF12_OXM:
2305 case OFPUTIL_P_OF13_OXM: {
2306 struct ofp12_flow_removed *ofr;
2308 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED,
2309 ofputil_protocol_to_ofp_version(protocol),
2310 htonl(0), NXM_TYPICAL_LEN);
2311 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
2312 ofr->cookie = fr->cookie;
2313 ofr->priority = htons(fr->priority);
2314 ofr->reason = fr->reason;
2315 ofr->table_id = fr->table_id;
2316 ofr->duration_sec = htonl(fr->duration_sec);
2317 ofr->duration_nsec = htonl(fr->duration_nsec);
2318 ofr->idle_timeout = htons(fr->idle_timeout);
2319 ofr->hard_timeout = htons(fr->hard_timeout);
2320 ofr->packet_count = htonll(fr->packet_count);
2321 ofr->byte_count = htonll(fr->byte_count);
2322 oxm_put_match(msg, &fr->match);
2326 case OFPUTIL_P_OF10_STD:
2327 case OFPUTIL_P_OF10_STD_TID: {
2328 struct ofp_flow_removed *ofr;
2330 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
2332 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
2333 ofputil_match_to_ofp10_match(&fr->match, &ofr->match);
2334 ofr->cookie = fr->cookie;
2335 ofr->priority = htons(fr->priority);
2336 ofr->reason = fr->reason;
2337 ofr->duration_sec = htonl(fr->duration_sec);
2338 ofr->duration_nsec = htonl(fr->duration_nsec);
2339 ofr->idle_timeout = htons(fr->idle_timeout);
2340 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
2341 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
2345 case OFPUTIL_P_OF10_NXM:
2346 case OFPUTIL_P_OF10_NXM_TID: {
2347 struct nx_flow_removed *nfr;
2350 msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
2351 htonl(0), NXM_TYPICAL_LEN);
2352 nfr = ofpbuf_put_zeros(msg, sizeof *nfr);
2353 match_len = nx_put_match(msg, &fr->match, 0, 0);
2356 nfr->cookie = fr->cookie;
2357 nfr->priority = htons(fr->priority);
2358 nfr->reason = fr->reason;
2359 nfr->duration_sec = htonl(fr->duration_sec);
2360 nfr->duration_nsec = htonl(fr->duration_nsec);
2361 nfr->idle_timeout = htons(fr->idle_timeout);
2362 nfr->match_len = htons(match_len);
2363 nfr->packet_count = htonll(fr->packet_count);
2364 nfr->byte_count = htonll(fr->byte_count);
2376 ofputil_decode_packet_in_finish(struct ofputil_packet_in *pin,
2377 struct match *match, struct ofpbuf *b)
2379 pin->packet = b->data;
2380 pin->packet_len = b->size;
2382 pin->fmd.in_port = match->flow.in_port;
2383 pin->fmd.tun_id = match->flow.tunnel.tun_id;
2384 pin->fmd.metadata = match->flow.metadata;
2385 memcpy(pin->fmd.regs, match->flow.regs, sizeof pin->fmd.regs);
2389 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
2390 const struct ofp_header *oh)
2395 memset(pin, 0, sizeof *pin);
2397 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2398 raw = ofpraw_pull_assert(&b);
2399 if (raw == OFPRAW_OFPT13_PACKET_IN || raw == OFPRAW_OFPT12_PACKET_IN) {
2400 const struct ofp13_packet_in *opi;
2403 size_t packet_in_size;
2405 if (raw == OFPRAW_OFPT12_PACKET_IN) {
2406 packet_in_size = sizeof (struct ofp12_packet_in);
2408 packet_in_size = sizeof (struct ofp13_packet_in);
2411 opi = ofpbuf_pull(&b, packet_in_size);
2412 error = oxm_pull_match_loose(&b, &match);
2417 if (!ofpbuf_try_pull(&b, 2)) {
2418 return OFPERR_OFPBRC_BAD_LEN;
2421 pin->reason = opi->pi.reason;
2422 pin->table_id = opi->pi.table_id;
2423 pin->buffer_id = ntohl(opi->pi.buffer_id);
2424 pin->total_len = ntohs(opi->pi.total_len);
2426 if (raw == OFPRAW_OFPT13_PACKET_IN) {
2427 pin->cookie = opi->cookie;
2430 ofputil_decode_packet_in_finish(pin, &match, &b);
2431 } else if (raw == OFPRAW_OFPT10_PACKET_IN) {
2432 const struct ofp_packet_in *opi;
2434 opi = ofpbuf_pull(&b, offsetof(struct ofp_packet_in, data));
2436 pin->packet = opi->data;
2437 pin->packet_len = b.size;
2439 pin->fmd.in_port = ntohs(opi->in_port);
2440 pin->reason = opi->reason;
2441 pin->buffer_id = ntohl(opi->buffer_id);
2442 pin->total_len = ntohs(opi->total_len);
2443 } else if (raw == OFPRAW_NXT_PACKET_IN) {
2444 const struct nx_packet_in *npi;
2448 npi = ofpbuf_pull(&b, sizeof *npi);
2449 error = nx_pull_match_loose(&b, ntohs(npi->match_len), &match, NULL,
2455 if (!ofpbuf_try_pull(&b, 2)) {
2456 return OFPERR_OFPBRC_BAD_LEN;
2459 pin->reason = npi->reason;
2460 pin->table_id = npi->table_id;
2461 pin->cookie = npi->cookie;
2463 pin->buffer_id = ntohl(npi->buffer_id);
2464 pin->total_len = ntohs(npi->total_len);
2466 ofputil_decode_packet_in_finish(pin, &match, &b);
2475 ofputil_packet_in_to_match(const struct ofputil_packet_in *pin,
2476 struct match *match)
2480 match_init_catchall(match);
2481 if (pin->fmd.tun_id != htonll(0)) {
2482 match_set_tun_id(match, pin->fmd.tun_id);
2484 if (pin->fmd.metadata != htonll(0)) {
2485 match_set_metadata(match, pin->fmd.metadata);
2488 for (i = 0; i < FLOW_N_REGS; i++) {
2489 if (pin->fmd.regs[i]) {
2490 match_set_reg(match, i, pin->fmd.regs[i]);
2494 match_set_in_port(match, pin->fmd.in_port);
2497 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
2498 * in the format specified by 'packet_in_format'. */
2500 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
2501 enum ofputil_protocol protocol,
2502 enum nx_packet_in_format packet_in_format)
2504 size_t send_len = MIN(pin->send_len, pin->packet_len);
2505 struct ofpbuf *packet;
2507 /* Add OFPT_PACKET_IN. */
2508 if (protocol == OFPUTIL_P_OF13_OXM || protocol == OFPUTIL_P_OF12_OXM) {
2509 struct ofp13_packet_in *opi;
2511 enum ofpraw packet_in_raw;
2512 enum ofp_version packet_in_version;
2513 size_t packet_in_size;
2515 if (protocol == OFPUTIL_P_OF12_OXM) {
2516 packet_in_raw = OFPRAW_OFPT12_PACKET_IN;
2517 packet_in_version = OFP12_VERSION;
2518 packet_in_size = sizeof (struct ofp12_packet_in);
2520 packet_in_raw = OFPRAW_OFPT13_PACKET_IN;
2521 packet_in_version = OFP13_VERSION;
2522 packet_in_size = sizeof (struct ofp13_packet_in);
2525 ofputil_packet_in_to_match(pin, &match);
2527 /* The final argument is just an estimate of the space required. */
2528 packet = ofpraw_alloc_xid(packet_in_raw, packet_in_version,
2529 htonl(0), (sizeof(struct flow_metadata) * 2
2531 ofpbuf_put_zeros(packet, packet_in_size);
2532 oxm_put_match(packet, &match);
2533 ofpbuf_put_zeros(packet, 2);
2534 ofpbuf_put(packet, pin->packet, send_len);
2537 opi->pi.buffer_id = htonl(pin->buffer_id);
2538 opi->pi.total_len = htons(pin->total_len);
2539 opi->pi.reason = pin->reason;
2540 opi->pi.table_id = pin->table_id;
2541 if (protocol == OFPUTIL_P_OF13_OXM) {
2542 opi->cookie = pin->cookie;
2544 } else if (packet_in_format == NXPIF_OPENFLOW10) {
2545 struct ofp_packet_in *opi;
2547 packet = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
2548 htonl(0), send_len);
2549 opi = ofpbuf_put_zeros(packet, offsetof(struct ofp_packet_in, data));
2550 opi->total_len = htons(pin->total_len);
2551 opi->in_port = htons(pin->fmd.in_port);
2552 opi->reason = pin->reason;
2553 opi->buffer_id = htonl(pin->buffer_id);
2555 ofpbuf_put(packet, pin->packet, send_len);
2556 } else if (packet_in_format == NXPIF_NXM) {
2557 struct nx_packet_in *npi;
2561 ofputil_packet_in_to_match(pin, &match);
2563 /* The final argument is just an estimate of the space required. */
2564 packet = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, OFP10_VERSION,
2565 htonl(0), (sizeof(struct flow_metadata) * 2
2567 ofpbuf_put_zeros(packet, sizeof *npi);
2568 match_len = nx_put_match(packet, &match, 0, 0);
2569 ofpbuf_put_zeros(packet, 2);
2570 ofpbuf_put(packet, pin->packet, send_len);
2573 npi->buffer_id = htonl(pin->buffer_id);
2574 npi->total_len = htons(pin->total_len);
2575 npi->reason = pin->reason;
2576 npi->table_id = pin->table_id;
2577 npi->cookie = pin->cookie;
2578 npi->match_len = htons(match_len);
2582 ofpmsg_update_length(packet);
2588 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason)
2590 static char s[INT_STRLEN(int) + 1];
2597 case OFPR_INVALID_TTL:
2598 return "invalid_ttl";
2600 case OFPR_N_REASONS:
2602 sprintf(s, "%d", (int) reason);
2608 ofputil_packet_in_reason_from_string(const char *s,
2609 enum ofp_packet_in_reason *reason)
2613 for (i = 0; i < OFPR_N_REASONS; i++) {
2614 if (!strcasecmp(s, ofputil_packet_in_reason_to_string(i))) {
2622 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
2625 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
2626 * message's actions. The caller must initialize 'ofpacts' and retains
2627 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
2629 * Returns 0 if successful, otherwise an OFPERR_* value. */
2631 ofputil_decode_packet_out(struct ofputil_packet_out *po,
2632 const struct ofp_header *oh,
2633 struct ofpbuf *ofpacts)
2638 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2639 raw = ofpraw_pull_assert(&b);
2641 if (raw == OFPRAW_OFPT11_PACKET_OUT) {
2643 const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
2645 po->buffer_id = ntohl(opo->buffer_id);
2646 error = ofputil_port_from_ofp11(opo->in_port, &po->in_port);
2651 error = ofpacts_pull_openflow11_actions(&b, ntohs(opo->actions_len),
2656 } else if (raw == OFPRAW_OFPT10_PACKET_OUT) {
2658 const struct ofp_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
2660 po->buffer_id = ntohl(opo->buffer_id);
2661 po->in_port = ntohs(opo->in_port);
2663 error = ofpacts_pull_openflow10(&b, ntohs(opo->actions_len), ofpacts);
2671 if (po->in_port >= OFPP_MAX && po->in_port != OFPP_LOCAL
2672 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
2673 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
2675 return OFPERR_OFPBRC_BAD_PORT;
2678 po->ofpacts = ofpacts->data;
2679 po->ofpacts_len = ofpacts->size;
2681 if (po->buffer_id == UINT32_MAX) {
2682 po->packet = b.data;
2683 po->packet_len = b.size;
2692 /* ofputil_phy_port */
2694 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
2695 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2696 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2697 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2698 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2699 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2700 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2701 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2703 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
2704 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
2705 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
2706 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
2707 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
2708 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
2710 static enum netdev_features
2711 netdev_port_features_from_ofp10(ovs_be32 ofp10_)
2713 uint32_t ofp10 = ntohl(ofp10_);
2714 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
2718 netdev_port_features_to_ofp10(enum netdev_features features)
2720 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
2723 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2724 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2725 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2726 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2727 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2728 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2729 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2730 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
2731 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
2732 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
2733 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
2734 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
2735 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
2736 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
2737 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
2738 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
2740 static enum netdev_features
2741 netdev_port_features_from_ofp11(ovs_be32 ofp11)
2743 return ntohl(ofp11) & 0xffff;
2747 netdev_port_features_to_ofp11(enum netdev_features features)
2749 return htonl(features & 0xffff);
2753 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
2754 const struct ofp10_phy_port *opp)
2756 memset(pp, 0, sizeof *pp);
2758 pp->port_no = ntohs(opp->port_no);
2759 memcpy(pp->hw_addr, opp->hw_addr, OFP_ETH_ALEN);
2760 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
2762 pp->config = ntohl(opp->config) & OFPPC10_ALL;
2763 pp->state = ntohl(opp->state) & OFPPS10_ALL;
2765 pp->curr = netdev_port_features_from_ofp10(opp->curr);
2766 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
2767 pp->supported = netdev_port_features_from_ofp10(opp->supported);
2768 pp->peer = netdev_port_features_from_ofp10(opp->peer);
2770 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
2771 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
2777 ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
2778 const struct ofp11_port *op)
2782 memset(pp, 0, sizeof *pp);
2784 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
2788 memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN);
2789 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
2791 pp->config = ntohl(op->config) & OFPPC11_ALL;
2792 pp->state = ntohl(op->state) & OFPPC11_ALL;
2794 pp->curr = netdev_port_features_from_ofp11(op->curr);
2795 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
2796 pp->supported = netdev_port_features_from_ofp11(op->supported);
2797 pp->peer = netdev_port_features_from_ofp11(op->peer);
2799 pp->curr_speed = ntohl(op->curr_speed);
2800 pp->max_speed = ntohl(op->max_speed);
2806 ofputil_get_phy_port_size(enum ofp_version ofp_version)
2808 switch (ofp_version) {
2810 return sizeof(struct ofp10_phy_port);
2814 return sizeof(struct ofp11_port);
2821 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
2822 struct ofp10_phy_port *opp)
2824 memset(opp, 0, sizeof *opp);
2826 opp->port_no = htons(pp->port_no);
2827 memcpy(opp->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2828 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2830 opp->config = htonl(pp->config & OFPPC10_ALL);
2831 opp->state = htonl(pp->state & OFPPS10_ALL);
2833 opp->curr = netdev_port_features_to_ofp10(pp->curr);
2834 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
2835 opp->supported = netdev_port_features_to_ofp10(pp->supported);
2836 opp->peer = netdev_port_features_to_ofp10(pp->peer);
2840 ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
2841 struct ofp11_port *op)
2843 memset(op, 0, sizeof *op);
2845 op->port_no = ofputil_port_to_ofp11(pp->port_no);
2846 memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2847 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2849 op->config = htonl(pp->config & OFPPC11_ALL);
2850 op->state = htonl(pp->state & OFPPS11_ALL);
2852 op->curr = netdev_port_features_to_ofp11(pp->curr);
2853 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
2854 op->supported = netdev_port_features_to_ofp11(pp->supported);
2855 op->peer = netdev_port_features_to_ofp11(pp->peer);
2857 op->curr_speed = htonl(pp->curr_speed);
2858 op->max_speed = htonl(pp->max_speed);
2862 ofputil_put_phy_port(enum ofp_version ofp_version,
2863 const struct ofputil_phy_port *pp, struct ofpbuf *b)
2865 switch (ofp_version) {
2866 case OFP10_VERSION: {
2867 struct ofp10_phy_port *opp;
2868 if (b->size + sizeof *opp <= UINT16_MAX) {
2869 opp = ofpbuf_put_uninit(b, sizeof *opp);
2870 ofputil_encode_ofp10_phy_port(pp, opp);
2877 case OFP13_VERSION: {
2878 struct ofp11_port *op;
2879 if (b->size + sizeof *op <= UINT16_MAX) {
2880 op = ofpbuf_put_uninit(b, sizeof *op);
2881 ofputil_encode_ofp11_port(pp, op);
2892 ofputil_append_port_desc_stats_reply(enum ofp_version ofp_version,
2893 const struct ofputil_phy_port *pp,
2894 struct list *replies)
2896 switch (ofp_version) {
2897 case OFP10_VERSION: {
2898 struct ofp10_phy_port *opp;
2900 opp = ofpmp_append(replies, sizeof *opp);
2901 ofputil_encode_ofp10_phy_port(pp, opp);
2907 case OFP13_VERSION: {
2908 struct ofp11_port *op;
2910 op = ofpmp_append(replies, sizeof *op);
2911 ofputil_encode_ofp11_port(pp, op);
2920 /* ofputil_switch_features */
2922 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
2923 OFPC_IP_REASM | OFPC_QUEUE_STATS)
2924 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
2925 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
2926 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
2927 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
2928 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
2929 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
2931 struct ofputil_action_bit_translation {
2932 enum ofputil_action_bitmap ofputil_bit;
2936 static const struct ofputil_action_bit_translation of10_action_bits[] = {
2937 { OFPUTIL_A_OUTPUT, OFPAT10_OUTPUT },
2938 { OFPUTIL_A_SET_VLAN_VID, OFPAT10_SET_VLAN_VID },
2939 { OFPUTIL_A_SET_VLAN_PCP, OFPAT10_SET_VLAN_PCP },
2940 { OFPUTIL_A_STRIP_VLAN, OFPAT10_STRIP_VLAN },
2941 { OFPUTIL_A_SET_DL_SRC, OFPAT10_SET_DL_SRC },
2942 { OFPUTIL_A_SET_DL_DST, OFPAT10_SET_DL_DST },
2943 { OFPUTIL_A_SET_NW_SRC, OFPAT10_SET_NW_SRC },
2944 { OFPUTIL_A_SET_NW_DST, OFPAT10_SET_NW_DST },
2945 { OFPUTIL_A_SET_NW_TOS, OFPAT10_SET_NW_TOS },
2946 { OFPUTIL_A_SET_TP_SRC, OFPAT10_SET_TP_SRC },
2947 { OFPUTIL_A_SET_TP_DST, OFPAT10_SET_TP_DST },
2948 { OFPUTIL_A_ENQUEUE, OFPAT10_ENQUEUE },
2952 static enum ofputil_action_bitmap
2953 decode_action_bits(ovs_be32 of_actions,
2954 const struct ofputil_action_bit_translation *x)
2956 enum ofputil_action_bitmap ofputil_actions;
2958 ofputil_actions = 0;
2959 for (; x->ofputil_bit; x++) {
2960 if (of_actions & htonl(1u << x->of_bit)) {
2961 ofputil_actions |= x->ofputil_bit;
2964 return ofputil_actions;
2968 ofputil_capabilities_mask(enum ofp_version ofp_version)
2970 /* Handle capabilities whose bit is unique for all Open Flow versions */
2971 switch (ofp_version) {
2974 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
2977 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
2979 /* Caller needs to check osf->header.version itself */
2984 /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an
2985 * abstract representation in '*features'. Initializes '*b' to iterate over
2986 * the OpenFlow port structures following 'osf' with later calls to
2987 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an
2988 * OFPERR_* value. */
2990 ofputil_decode_switch_features(const struct ofp_header *oh,
2991 struct ofputil_switch_features *features,
2994 const struct ofp_switch_features *osf;
2997 ofpbuf_use_const(b, oh, ntohs(oh->length));
2998 raw = ofpraw_pull_assert(b);
3000 osf = ofpbuf_pull(b, sizeof *osf);
3001 features->datapath_id = ntohll(osf->datapath_id);
3002 features->n_buffers = ntohl(osf->n_buffers);
3003 features->n_tables = osf->n_tables;
3004 features->auxiliary_id = 0;
3006 features->capabilities = ntohl(osf->capabilities) &
3007 ofputil_capabilities_mask(oh->version);
3009 if (b->size % ofputil_get_phy_port_size(oh->version)) {
3010 return OFPERR_OFPBRC_BAD_LEN;
3013 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
3014 if (osf->capabilities & htonl(OFPC10_STP)) {
3015 features->capabilities |= OFPUTIL_C_STP;
3017 features->actions = decode_action_bits(osf->actions, of10_action_bits);
3018 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY
3019 || raw == OFPRAW_OFPT13_FEATURES_REPLY) {
3020 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
3021 features->capabilities |= OFPUTIL_C_GROUP_STATS;
3023 features->actions = 0;
3024 if (raw == OFPRAW_OFPT13_FEATURES_REPLY) {
3025 features->auxiliary_id = osf->auxiliary_id;
3028 return OFPERR_OFPBRC_BAD_VERSION;
3034 /* Returns true if the maximum number of ports are in 'oh'. */
3036 max_ports_in_features(const struct ofp_header *oh)
3038 size_t pp_size = ofputil_get_phy_port_size(oh->version);
3039 return ntohs(oh->length) + pp_size > UINT16_MAX;
3042 /* Given a buffer 'b' that contains a Features Reply message, checks if
3043 * it contains the maximum number of ports that will fit. If so, it
3044 * returns true and removes the ports from the message. The caller
3045 * should then send an OFPST_PORT_DESC stats request to get the ports,
3046 * since the switch may have more ports than could be represented in the
3047 * Features Reply. Otherwise, returns false.
3050 ofputil_switch_features_ports_trunc(struct ofpbuf *b)
3052 struct ofp_header *oh = b->data;
3054 if (max_ports_in_features(oh)) {
3055 /* Remove all the ports. */
3056 b->size = (sizeof(struct ofp_header)
3057 + sizeof(struct ofp_switch_features));
3058 ofpmsg_update_length(b);
3067 encode_action_bits(enum ofputil_action_bitmap ofputil_actions,
3068 const struct ofputil_action_bit_translation *x)
3070 uint32_t of_actions;
3073 for (; x->ofputil_bit; x++) {
3074 if (ofputil_actions & x->ofputil_bit) {
3075 of_actions |= 1 << x->of_bit;
3078 return htonl(of_actions);
3081 /* Returns a buffer owned by the caller that encodes 'features' in the format
3082 * required by 'protocol' with the given 'xid'. The caller should append port
3083 * information to the buffer with subsequent calls to
3084 * ofputil_put_switch_features_port(). */
3086 ofputil_encode_switch_features(const struct ofputil_switch_features *features,
3087 enum ofputil_protocol protocol, ovs_be32 xid)
3089 struct ofp_switch_features *osf;
3091 enum ofp_version version;
3094 version = ofputil_protocol_to_ofp_version(protocol);
3097 raw = OFPRAW_OFPT10_FEATURES_REPLY;
3101 raw = OFPRAW_OFPT11_FEATURES_REPLY;
3104 raw = OFPRAW_OFPT13_FEATURES_REPLY;
3109 b = ofpraw_alloc_xid(raw, version, xid, 0);
3110 osf = ofpbuf_put_zeros(b, sizeof *osf);
3111 osf->datapath_id = htonll(features->datapath_id);
3112 osf->n_buffers = htonl(features->n_buffers);
3113 osf->n_tables = features->n_tables;
3115 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
3116 osf->capabilities = htonl(features->capabilities &
3117 ofputil_capabilities_mask(version));
3120 if (features->capabilities & OFPUTIL_C_STP) {
3121 osf->capabilities |= htonl(OFPC10_STP);
3123 osf->actions = encode_action_bits(features->actions, of10_action_bits);
3126 osf->auxiliary_id = features->auxiliary_id;
3130 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
3131 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
3141 /* Encodes 'pp' into the format required by the switch_features message already
3142 * in 'b', which should have been returned by ofputil_encode_switch_features(),
3143 * and appends the encoded version to 'b'. */
3145 ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
3148 const struct ofp_header *oh = b->data;
3150 ofputil_put_phy_port(oh->version, pp, b);
3153 /* ofputil_port_status */
3155 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
3156 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
3158 ofputil_decode_port_status(const struct ofp_header *oh,
3159 struct ofputil_port_status *ps)
3161 const struct ofp_port_status *ops;
3165 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3166 ofpraw_pull_assert(&b);
3167 ops = ofpbuf_pull(&b, sizeof *ops);
3169 if (ops->reason != OFPPR_ADD &&
3170 ops->reason != OFPPR_DELETE &&
3171 ops->reason != OFPPR_MODIFY) {
3172 return OFPERR_NXBRC_BAD_REASON;
3174 ps->reason = ops->reason;
3176 retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
3177 assert(retval != EOF);
3181 /* Converts the abstract form of a "port status" message in '*ps' into an
3182 * OpenFlow message suitable for 'protocol', and returns that encoded form in
3183 * a buffer owned by the caller. */
3185 ofputil_encode_port_status(const struct ofputil_port_status *ps,
3186 enum ofputil_protocol protocol)
3188 struct ofp_port_status *ops;
3190 enum ofp_version version;
3193 version = ofputil_protocol_to_ofp_version(protocol);
3196 raw = OFPRAW_OFPT10_PORT_STATUS;
3202 raw = OFPRAW_OFPT11_PORT_STATUS;
3209 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
3210 ops = ofpbuf_put_zeros(b, sizeof *ops);
3211 ops->reason = ps->reason;
3212 ofputil_put_phy_port(version, &ps->desc, b);
3213 ofpmsg_update_length(b);
3217 /* ofputil_port_mod */
3219 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
3220 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
3222 ofputil_decode_port_mod(const struct ofp_header *oh,
3223 struct ofputil_port_mod *pm)
3228 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3229 raw = ofpraw_pull_assert(&b);
3231 if (raw == OFPRAW_OFPT10_PORT_MOD) {
3232 const struct ofp10_port_mod *opm = b.data;
3234 pm->port_no = ntohs(opm->port_no);
3235 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
3236 pm->config = ntohl(opm->config) & OFPPC10_ALL;
3237 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
3238 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
3239 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
3240 const struct ofp11_port_mod *opm = b.data;
3243 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
3248 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
3249 pm->config = ntohl(opm->config) & OFPPC11_ALL;
3250 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
3251 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
3253 return OFPERR_OFPBRC_BAD_TYPE;
3256 pm->config &= pm->mask;
3260 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
3261 * message suitable for 'protocol', and returns that encoded form in a buffer
3262 * owned by the caller. */
3264 ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
3265 enum ofputil_protocol protocol)
3267 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
3270 switch (ofp_version) {
3271 case OFP10_VERSION: {
3272 struct ofp10_port_mod *opm;
3274 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
3275 opm = ofpbuf_put_zeros(b, sizeof *opm);
3276 opm->port_no = htons(pm->port_no);
3277 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
3278 opm->config = htonl(pm->config & OFPPC10_ALL);
3279 opm->mask = htonl(pm->mask & OFPPC10_ALL);
3280 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
3286 case OFP13_VERSION: {
3287 struct ofp11_port_mod *opm;
3289 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
3290 opm = ofpbuf_put_zeros(b, sizeof *opm);
3291 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
3292 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
3293 opm->config = htonl(pm->config & OFPPC11_ALL);
3294 opm->mask = htonl(pm->mask & OFPPC11_ALL);
3295 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
3309 ofputil_put_ofp10_table_stats(const struct ofp12_table_stats *in,
3313 enum ofp_flow_wildcards wc10;
3314 enum oxm12_ofb_match_fields mf12;
3317 static const struct wc_map wc_map[] = {
3318 { OFPFW10_IN_PORT, OFPXMT12_OFB_IN_PORT },
3319 { OFPFW10_DL_VLAN, OFPXMT12_OFB_VLAN_VID },
3320 { OFPFW10_DL_SRC, OFPXMT12_OFB_ETH_SRC },
3321 { OFPFW10_DL_DST, OFPXMT12_OFB_ETH_DST},
3322 { OFPFW10_DL_TYPE, OFPXMT12_OFB_ETH_TYPE },
3323 { OFPFW10_NW_PROTO, OFPXMT12_OFB_IP_PROTO },
3324 { OFPFW10_TP_SRC, OFPXMT12_OFB_TCP_SRC },
3325 { OFPFW10_TP_DST, OFPXMT12_OFB_TCP_DST },
3326 { OFPFW10_NW_SRC_MASK, OFPXMT12_OFB_IPV4_SRC },
3327 { OFPFW10_NW_DST_MASK, OFPXMT12_OFB_IPV4_DST },
3328 { OFPFW10_DL_VLAN_PCP, OFPXMT12_OFB_VLAN_PCP },
3329 { OFPFW10_NW_TOS, OFPXMT12_OFB_IP_DSCP },
3332 struct ofp10_table_stats *out;
3333 const struct wc_map *p;
3335 out = ofpbuf_put_uninit(buf, sizeof *out);
3336 out->table_id = in->table_id;
3337 strcpy(out->name, in->name);
3339 for (p = wc_map; p < &wc_map[ARRAY_SIZE(wc_map)]; p++) {
3340 if (in->wildcards & htonll(1ULL << p->mf12)) {
3341 out->wildcards |= htonl(p->wc10);
3344 out->max_entries = in->max_entries;
3345 out->active_count = in->active_count;
3346 put_32aligned_be64(&out->lookup_count, in->lookup_count);
3347 put_32aligned_be64(&out->matched_count, in->matched_count);
3351 oxm12_to_ofp11_flow_match_fields(ovs_be64 oxm12)
3354 enum ofp11_flow_match_fields fmf11;
3355 enum oxm12_ofb_match_fields mf12;
3358 static const struct map map[] = {
3359 { OFPFMF11_IN_PORT, OFPXMT12_OFB_IN_PORT },
3360 { OFPFMF11_DL_VLAN, OFPXMT12_OFB_VLAN_VID },
3361 { OFPFMF11_DL_VLAN_PCP, OFPXMT12_OFB_VLAN_PCP },
3362 { OFPFMF11_DL_TYPE, OFPXMT12_OFB_ETH_TYPE },
3363 { OFPFMF11_NW_TOS, OFPXMT12_OFB_IP_DSCP },
3364 { OFPFMF11_NW_PROTO, OFPXMT12_OFB_IP_PROTO },
3365 { OFPFMF11_TP_SRC, OFPXMT12_OFB_TCP_SRC },
3366 { OFPFMF11_TP_DST, OFPXMT12_OFB_TCP_DST },
3367 { OFPFMF11_MPLS_LABEL, OFPXMT12_OFB_MPLS_LABEL },
3368 { OFPFMF11_MPLS_TC, OFPXMT12_OFB_MPLS_TC },
3369 /* I don't know what OFPFMF11_TYPE means. */
3370 { OFPFMF11_DL_SRC, OFPXMT12_OFB_ETH_SRC },
3371 { OFPFMF11_DL_DST, OFPXMT12_OFB_ETH_DST },
3372 { OFPFMF11_NW_SRC, OFPXMT12_OFB_IPV4_SRC },
3373 { OFPFMF11_NW_DST, OFPXMT12_OFB_IPV4_DST },
3374 { OFPFMF11_METADATA, OFPXMT12_OFB_METADATA },
3377 const struct map *p;
3381 for (p = map; p < &map[ARRAY_SIZE(map)]; p++) {
3382 if (oxm12 & htonll(1ULL << p->mf12)) {
3386 return htonl(fmf11);
3390 ofputil_put_ofp11_table_stats(const struct ofp12_table_stats *in,
3393 struct ofp11_table_stats *out;
3395 out = ofpbuf_put_uninit(buf, sizeof *out);
3396 out->table_id = in->table_id;
3397 strcpy(out->name, in->name);
3398 out->wildcards = oxm12_to_ofp11_flow_match_fields(in->wildcards);
3399 out->match = oxm12_to_ofp11_flow_match_fields(in->match);
3400 out->instructions = in->instructions;
3401 out->write_actions = in->write_actions;
3402 out->apply_actions = in->apply_actions;
3403 out->config = in->config;
3404 out->max_entries = in->max_entries;
3405 out->active_count = in->active_count;
3406 out->lookup_count = in->lookup_count;
3407 out->matched_count = in->matched_count;
3411 ofputil_put_ofp13_table_stats(const struct ofp12_table_stats *in,
3414 struct ofp13_table_stats *out;
3416 /* OF 1.3 splits table features off the ofp_table_stats,
3417 * so there is not much here. */
3419 out = ofpbuf_put_uninit(buf, sizeof *out);
3420 out->table_id = in->table_id;
3421 out->active_count = in->active_count;
3422 out->lookup_count = in->lookup_count;
3423 out->matched_count = in->matched_count;
3427 ofputil_encode_table_stats_reply(const struct ofp12_table_stats stats[], int n,
3428 const struct ofp_header *request)
3430 struct ofpbuf *reply;
3433 reply = ofpraw_alloc_stats_reply(request, n * sizeof *stats);
3435 switch ((enum ofp_version) request->version) {
3437 for (i = 0; i < n; i++) {
3438 ofputil_put_ofp10_table_stats(&stats[i], reply);
3443 for (i = 0; i < n; i++) {
3444 ofputil_put_ofp11_table_stats(&stats[i], reply);
3449 ofpbuf_put(reply, stats, n * sizeof *stats);
3453 for (i = 0; i < n; i++) {
3454 ofputil_put_ofp13_table_stats(&stats[i], reply);
3465 /* ofputil_flow_monitor_request */
3467 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
3468 * ofputil_flow_monitor_request in 'rq'.
3470 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
3471 * message. Calling this function multiple times for a single 'msg' iterates
3472 * through the requests. The caller must initially leave 'msg''s layer
3473 * pointers null and not modify them between calls.
3475 * Returns 0 if successful, EOF if no requests were left in this 'msg',
3476 * otherwise an OFPERR_* value. */
3478 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
3481 struct nx_flow_monitor_request *nfmr;
3485 msg->l2 = msg->data;
3486 ofpraw_pull_assert(msg);
3493 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
3495 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %zu "
3496 "leftover bytes at end", msg->size);
3497 return OFPERR_OFPBRC_BAD_LEN;
3500 flags = ntohs(nfmr->flags);
3501 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
3502 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
3503 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
3504 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
3506 return OFPERR_NXBRC_FM_BAD_FLAGS;
3509 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
3510 return OFPERR_NXBRC_MUST_BE_ZERO;
3513 rq->id = ntohl(nfmr->id);
3515 rq->out_port = ntohs(nfmr->out_port);
3516 rq->table_id = nfmr->table_id;
3518 return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL, NULL);
3522 ofputil_append_flow_monitor_request(
3523 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
3525 struct nx_flow_monitor_request *nfmr;
3530 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
3533 start_ofs = msg->size;
3534 ofpbuf_put_zeros(msg, sizeof *nfmr);
3535 match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
3537 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
3538 nfmr->id = htonl(rq->id);
3539 nfmr->flags = htons(rq->flags);
3540 nfmr->out_port = htons(rq->out_port);
3541 nfmr->match_len = htons(match_len);
3542 nfmr->table_id = rq->table_id;
3545 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
3546 * into an abstract ofputil_flow_update in 'update'. The caller must have
3547 * initialized update->match to point to space allocated for a match.
3549 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
3550 * actions (except for NXFME_ABBREV, which never includes actions). The caller
3551 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
3552 * will point into the 'ofpacts' buffer.
3554 * Multiple flow updates can be packed into a single OpenFlow message. Calling
3555 * this function multiple times for a single 'msg' iterates through the
3556 * updates. The caller must initially leave 'msg''s layer pointers null and
3557 * not modify them between calls.
3559 * Returns 0 if successful, EOF if no updates were left in this 'msg',
3560 * otherwise an OFPERR_* value. */
3562 ofputil_decode_flow_update(struct ofputil_flow_update *update,
3563 struct ofpbuf *msg, struct ofpbuf *ofpacts)
3565 struct nx_flow_update_header *nfuh;
3566 unsigned int length;
3569 msg->l2 = msg->data;
3570 ofpraw_pull_assert(msg);
3577 if (msg->size < sizeof(struct nx_flow_update_header)) {
3582 update->event = ntohs(nfuh->event);
3583 length = ntohs(nfuh->length);
3584 if (length > msg->size || length % 8) {
3588 if (update->event == NXFME_ABBREV) {
3589 struct nx_flow_update_abbrev *nfua;
3591 if (length != sizeof *nfua) {
3595 nfua = ofpbuf_pull(msg, sizeof *nfua);
3596 update->xid = nfua->xid;
3598 } else if (update->event == NXFME_ADDED
3599 || update->event == NXFME_DELETED
3600 || update->event == NXFME_MODIFIED) {
3601 struct nx_flow_update_full *nfuf;
3602 unsigned int actions_len;
3603 unsigned int match_len;
3606 if (length < sizeof *nfuf) {
3610 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
3611 match_len = ntohs(nfuf->match_len);
3612 if (sizeof *nfuf + match_len > length) {
3616 update->reason = ntohs(nfuf->reason);
3617 update->idle_timeout = ntohs(nfuf->idle_timeout);
3618 update->hard_timeout = ntohs(nfuf->hard_timeout);
3619 update->table_id = nfuf->table_id;
3620 update->cookie = nfuf->cookie;
3621 update->priority = ntohs(nfuf->priority);
3623 error = nx_pull_match(msg, match_len, update->match, NULL, NULL);
3628 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
3629 error = ofpacts_pull_openflow10(msg, actions_len, ofpacts);
3634 update->ofpacts = ofpacts->data;
3635 update->ofpacts_len = ofpacts->size;
3638 VLOG_WARN_RL(&bad_ofmsg_rl,
3639 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
3640 ntohs(nfuh->event));
3641 return OFPERR_OFPET_BAD_REQUEST;
3645 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %zu "
3646 "leftover bytes at end", msg->size);
3647 return OFPERR_OFPBRC_BAD_LEN;
3651 ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
3653 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
3655 return ntohl(cancel->id);
3659 ofputil_encode_flow_monitor_cancel(uint32_t id)
3661 struct nx_flow_monitor_cancel *nfmc;
3664 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
3665 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
3666 nfmc->id = htonl(id);
3671 ofputil_start_flow_update(struct list *replies)
3675 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
3679 list_push_back(replies, &msg->list_node);
3683 ofputil_append_flow_update(const struct ofputil_flow_update *update,
3684 struct list *replies)
3686 struct nx_flow_update_header *nfuh;
3690 msg = ofpbuf_from_list(list_back(replies));
3691 start_ofs = msg->size;
3693 if (update->event == NXFME_ABBREV) {
3694 struct nx_flow_update_abbrev *nfua;
3696 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
3697 nfua->xid = update->xid;
3699 struct nx_flow_update_full *nfuf;
3702 ofpbuf_put_zeros(msg, sizeof *nfuf);
3703 match_len = nx_put_match(msg, update->match, htonll(0), htonll(0));
3704 ofpacts_put_openflow10(update->ofpacts, update->ofpacts_len, msg);
3706 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
3707 nfuf->reason = htons(update->reason);
3708 nfuf->priority = htons(update->priority);
3709 nfuf->idle_timeout = htons(update->idle_timeout);
3710 nfuf->hard_timeout = htons(update->hard_timeout);
3711 nfuf->match_len = htons(match_len);
3712 nfuf->table_id = update->table_id;
3713 nfuf->cookie = update->cookie;
3716 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
3717 nfuh->length = htons(msg->size - start_ofs);
3718 nfuh->event = htons(update->event);
3720 ofpmp_postappend(replies, start_ofs);
3724 ofputil_encode_packet_out(const struct ofputil_packet_out *po,
3725 enum ofputil_protocol protocol)
3727 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
3731 size = po->ofpacts_len;
3732 if (po->buffer_id == UINT32_MAX) {
3733 size += po->packet_len;
3736 switch (ofp_version) {
3737 case OFP10_VERSION: {
3738 struct ofp_packet_out *opo;
3741 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
3742 ofpbuf_put_zeros(msg, sizeof *opo);
3743 actions_ofs = msg->size;
3744 ofpacts_put_openflow10(po->ofpacts, po->ofpacts_len, msg);
3747 opo->buffer_id = htonl(po->buffer_id);
3748 opo->in_port = htons(po->in_port);
3749 opo->actions_len = htons(msg->size - actions_ofs);
3755 case OFP13_VERSION: {
3756 struct ofp11_packet_out *opo;
3759 msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size);
3760 ofpbuf_put_zeros(msg, sizeof *opo);
3761 len = ofpacts_put_openflow11_actions(po->ofpacts, po->ofpacts_len, msg);
3764 opo->buffer_id = htonl(po->buffer_id);
3765 opo->in_port = ofputil_port_to_ofp11(po->in_port);
3766 opo->actions_len = htons(len);
3774 if (po->buffer_id == UINT32_MAX) {
3775 ofpbuf_put(msg, po->packet, po->packet_len);
3778 ofpmsg_update_length(msg);
3783 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
3785 make_echo_request(enum ofp_version ofp_version)
3787 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
3791 /* Creates and returns an OFPT_ECHO_REPLY message matching the
3792 * OFPT_ECHO_REQUEST message in 'rq'. */
3794 make_echo_reply(const struct ofp_header *rq)
3796 struct ofpbuf rq_buf;
3797 struct ofpbuf *reply;
3799 ofpbuf_use_const(&rq_buf, rq, ntohs(rq->length));
3800 ofpraw_pull_assert(&rq_buf);
3802 reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY, rq, rq_buf.size);
3803 ofpbuf_put(reply, rq_buf.data, rq_buf.size);
3808 ofputil_encode_barrier_request(enum ofp_version ofp_version)
3812 switch (ofp_version) {
3816 type = OFPRAW_OFPT11_BARRIER_REQUEST;
3820 type = OFPRAW_OFPT10_BARRIER_REQUEST;
3827 return ofpraw_alloc(type, ofp_version, 0);
3831 ofputil_frag_handling_to_string(enum ofp_config_flags flags)
3833 switch (flags & OFPC_FRAG_MASK) {
3834 case OFPC_FRAG_NORMAL: return "normal";
3835 case OFPC_FRAG_DROP: return "drop";
3836 case OFPC_FRAG_REASM: return "reassemble";
3837 case OFPC_FRAG_NX_MATCH: return "nx-match";
3844 ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
3846 if (!strcasecmp(s, "normal")) {
3847 *flags = OFPC_FRAG_NORMAL;
3848 } else if (!strcasecmp(s, "drop")) {
3849 *flags = OFPC_FRAG_DROP;
3850 } else if (!strcasecmp(s, "reassemble")) {
3851 *flags = OFPC_FRAG_REASM;
3852 } else if (!strcasecmp(s, "nx-match")) {
3853 *flags = OFPC_FRAG_NX_MATCH;
3860 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
3861 * port number and stores the latter in '*ofp10_port', for the purpose of
3862 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
3863 * otherwise an OFPERR_* number.
3865 * See the definition of OFP11_MAX for an explanation of the mapping. */
3867 ofputil_port_from_ofp11(ovs_be32 ofp11_port, uint16_t *ofp10_port)
3869 uint32_t ofp11_port_h = ntohl(ofp11_port);
3871 if (ofp11_port_h < OFPP_MAX) {
3872 *ofp10_port = ofp11_port_h;
3874 } else if (ofp11_port_h >= OFPP11_MAX) {
3875 *ofp10_port = ofp11_port_h - OFPP11_OFFSET;
3878 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
3879 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
3880 ofp11_port_h, OFPP_MAX - 1,
3881 (uint32_t) OFPP11_MAX, UINT32_MAX);
3882 return OFPERR_OFPBAC_BAD_OUT_PORT;
3886 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
3887 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
3889 * See the definition of OFP11_MAX for an explanation of the mapping. */
3891 ofputil_port_to_ofp11(uint16_t ofp10_port)
3893 return htonl(ofp10_port < OFPP_MAX
3895 : ofp10_port + OFPP11_OFFSET);
3898 /* Checks that 'port' is a valid output port for the OFPAT10_OUTPUT action, given
3899 * that the switch will never have more than 'max_ports' ports. Returns 0 if
3900 * 'port' is valid, otherwise an OpenFlow return code. */
3902 ofputil_check_output_port(uint16_t port, int max_ports)
3910 case OFPP_CONTROLLER:
3916 if (port < max_ports) {
3919 return OFPERR_OFPBAC_BAD_OUT_PORT;
3923 #define OFPUTIL_NAMED_PORTS \
3924 OFPUTIL_NAMED_PORT(IN_PORT) \
3925 OFPUTIL_NAMED_PORT(TABLE) \
3926 OFPUTIL_NAMED_PORT(NORMAL) \
3927 OFPUTIL_NAMED_PORT(FLOOD) \
3928 OFPUTIL_NAMED_PORT(ALL) \
3929 OFPUTIL_NAMED_PORT(CONTROLLER) \
3930 OFPUTIL_NAMED_PORT(LOCAL) \
3931 OFPUTIL_NAMED_PORT(ANY)
3933 /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */
3934 #define OFPUTIL_NAMED_PORTS_WITH_NONE \
3935 OFPUTIL_NAMED_PORTS \
3936 OFPUTIL_NAMED_PORT(NONE)
3938 /* Stores the port number represented by 's' into '*portp'. 's' may be an
3939 * integer or, for reserved ports, the standard OpenFlow name for the port
3942 * Returns true if successful, false if 's' is not a valid OpenFlow port number
3943 * or name. The caller should issue an error message in this case, because
3944 * this function usually does not. (This gives the caller an opportunity to
3945 * look up the port name another way, e.g. by contacting the switch and listing
3946 * the names of all its ports).
3948 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
3949 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
3950 * range as described in include/openflow/openflow-1.1.h. */
3952 ofputil_port_from_string(const char *s, uint16_t *portp)
3954 unsigned int port32;
3957 if (str_to_uint(s, 10, &port32)) {
3958 if (port32 < OFPP_MAX) {
3961 } else if (port32 < OFPP_FIRST_RESV) {
3962 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
3963 "be translated to %u when talking to an OF1.1 or "
3964 "later controller", port32, port32 + OFPP11_OFFSET);
3967 } else if (port32 <= OFPP_LAST_RESV) {
3971 ofputil_format_port(port32, &s);
3972 VLOG_WARN_ONCE("referring to port %s as %u is deprecated for "
3973 "compatibility with future versions of OpenFlow",
3974 ds_cstr(&s), port32);
3979 } else if (port32 < OFPP11_MAX) {
3980 VLOG_WARN("port %u is outside the supported range 0 through "
3981 "%"PRIx16"or 0x%x through 0x%"PRIx32, port32,
3982 UINT16_MAX, (unsigned int) OFPP11_MAX, UINT32_MAX);
3985 *portp = port32 - OFPP11_OFFSET;
3993 static const struct pair pairs[] = {
3994 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
3995 OFPUTIL_NAMED_PORTS_WITH_NONE
3996 #undef OFPUTIL_NAMED_PORT
3998 const struct pair *p;
4000 for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) {
4001 if (!strcasecmp(s, p->name)) {
4010 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
4011 * Most ports' string representation is just the port number, but for special
4012 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
4014 ofputil_format_port(uint16_t port, struct ds *s)
4019 #define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break;
4021 #undef OFPUTIL_NAMED_PORT
4024 ds_put_format(s, "%"PRIu16, port);
4027 ds_put_cstr(s, name);
4030 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
4031 * 'ofp_version', tries to pull the first element from the array. If
4032 * successful, initializes '*pp' with an abstract representation of the
4033 * port and returns 0. If no ports remain to be decoded, returns EOF.
4034 * On an error, returns a positive OFPERR_* value. */
4036 ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
4037 struct ofputil_phy_port *pp)
4039 switch (ofp_version) {
4040 case OFP10_VERSION: {
4041 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
4042 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
4046 case OFP13_VERSION: {
4047 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
4048 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
4055 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
4056 * 'ofp_version', returns the number of elements. */
4057 size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *b)
4059 return b->size / ofputil_get_phy_port_size(ofp_version);
4062 /* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if
4063 * 'name' is "output" then the return value is OFPUTIL_OFPAT10_OUTPUT), or -1 if
4064 * 'name' is not the name of any action.
4066 * ofp-util.def lists the mapping from names to action. */
4068 ofputil_action_code_from_name(const char *name)
4070 static const char *names[OFPUTIL_N_ACTIONS] = {
4072 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) NAME,
4073 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
4074 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
4075 #include "ofp-util.def"
4080 for (p = names; p < &names[ARRAY_SIZE(names)]; p++) {
4081 if (*p && !strcasecmp(name, *p)) {
4088 /* Appends an action of the type specified by 'code' to 'buf' and returns the
4089 * action. Initializes the parts of 'action' that identify it as having type
4090 * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that
4091 * have variable length, the length used and cleared is that of struct
4094 ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
4097 case OFPUTIL_ACTION_INVALID:
4100 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
4101 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
4102 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
4103 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
4104 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
4105 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
4106 #include "ofp-util.def"
4111 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
4113 ofputil_init_##ENUM(struct STRUCT *s) \
4115 memset(s, 0, sizeof *s); \
4116 s->type = htons(ENUM); \
4117 s->len = htons(sizeof *s); \
4121 ofputil_put_##ENUM(struct ofpbuf *buf) \
4123 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
4124 ofputil_init_##ENUM(s); \
4127 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
4128 OFPAT10_ACTION(ENUM, STRUCT, NAME)
4129 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
4131 ofputil_init_##ENUM(struct STRUCT *s) \
4133 memset(s, 0, sizeof *s); \
4134 s->type = htons(OFPAT10_VENDOR); \
4135 s->len = htons(sizeof *s); \
4136 s->vendor = htonl(NX_VENDOR_ID); \
4137 s->subtype = htons(ENUM); \
4141 ofputil_put_##ENUM(struct ofpbuf *buf) \
4143 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
4144 ofputil_init_##ENUM(s); \
4147 #include "ofp-util.def"
4150 ofputil_normalize_match__(struct match *match, bool may_log)
4153 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
4154 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
4155 MAY_NW_PROTO = 1 << 2, /* nw_proto */
4156 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
4157 MAY_ARP_SHA = 1 << 4, /* arp_sha */
4158 MAY_ARP_THA = 1 << 5, /* arp_tha */
4159 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
4160 MAY_ND_TARGET = 1 << 7 /* nd_target */
4163 struct flow_wildcards wc;
4165 /* Figure out what fields may be matched. */
4166 if (match->flow.dl_type == htons(ETH_TYPE_IP)) {
4167 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
4168 if (match->flow.nw_proto == IPPROTO_TCP ||
4169 match->flow.nw_proto == IPPROTO_UDP ||
4170 match->flow.nw_proto == IPPROTO_ICMP) {
4171 may_match |= MAY_TP_ADDR;
4173 } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
4174 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
4175 if (match->flow.nw_proto == IPPROTO_TCP ||
4176 match->flow.nw_proto == IPPROTO_UDP) {
4177 may_match |= MAY_TP_ADDR;
4178 } else if (match->flow.nw_proto == IPPROTO_ICMPV6) {
4179 may_match |= MAY_TP_ADDR;
4180 if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
4181 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
4182 } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
4183 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
4186 } else if (match->flow.dl_type == htons(ETH_TYPE_ARP) ||
4187 match->flow.dl_type == htons(ETH_TYPE_RARP)) {
4188 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
4193 /* Clear the fields that may not be matched. */
4195 if (!(may_match & MAY_NW_ADDR)) {
4196 wc.masks.nw_src = wc.masks.nw_dst = htonl(0);
4198 if (!(may_match & MAY_TP_ADDR)) {
4199 wc.masks.tp_src = wc.masks.tp_dst = htons(0);
4201 if (!(may_match & MAY_NW_PROTO)) {
4202 wc.masks.nw_proto = 0;
4204 if (!(may_match & MAY_IPVx)) {
4205 wc.masks.nw_tos = 0;
4206 wc.masks.nw_ttl = 0;
4208 if (!(may_match & MAY_ARP_SHA)) {
4209 memset(wc.masks.arp_sha, 0, ETH_ADDR_LEN);
4211 if (!(may_match & MAY_ARP_THA)) {
4212 memset(wc.masks.arp_tha, 0, ETH_ADDR_LEN);
4214 if (!(may_match & MAY_IPV6)) {
4215 wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any;
4216 wc.masks.ipv6_label = htonl(0);
4218 if (!(may_match & MAY_ND_TARGET)) {
4219 wc.masks.nd_target = in6addr_any;
4222 /* Log any changes. */
4223 if (!flow_wildcards_equal(&wc, &match->wc)) {
4224 bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
4225 char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL;
4228 match_zero_wildcarded_fields(match);
4231 char *post = match_to_string(match, OFP_DEFAULT_PRIORITY);
4232 VLOG_INFO("normalization changed ofp_match, details:");
4233 VLOG_INFO(" pre: %s", pre);
4234 VLOG_INFO("post: %s", post);
4241 /* "Normalizes" the wildcards in 'match'. That means:
4243 * 1. If the type of level N is known, then only the valid fields for that
4244 * level may be specified. For example, ARP does not have a TOS field,
4245 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
4246 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
4247 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
4250 * 2. If the type of level N is not known (or not understood by Open
4251 * vSwitch), then no fields at all for that level may be specified. For
4252 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
4253 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
4256 * If this function changes 'match', it logs a rate-limited informational
4259 ofputil_normalize_match(struct match *match)
4261 ofputil_normalize_match__(match, true);
4264 /* Same as ofputil_normalize_match() without the logging. Thus, this function
4265 * is suitable for a program's internal use, whereas ofputil_normalize_match()
4266 * sense for use on flows received from elsewhere (so that a bug in the program
4267 * that sent them can be reported and corrected). */
4269 ofputil_normalize_match_quiet(struct match *match)
4271 ofputil_normalize_match__(match, false);
4274 /* Parses a key or a key-value pair from '*stringp'.
4276 * On success: Stores the key into '*keyp'. Stores the value, if present, into
4277 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
4278 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
4279 * are substrings of '*stringp' created by replacing some of its bytes by null
4280 * terminators. Returns true.
4282 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
4283 * NULL and returns false. */
4285 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
4287 char *pos, *key, *value;
4291 pos += strspn(pos, ", \t\r\n");
4293 *keyp = *valuep = NULL;
4298 key_len = strcspn(pos, ":=(, \t\r\n");
4299 if (key[key_len] == ':' || key[key_len] == '=') {
4300 /* The value can be separated by a colon. */
4303 value = key + key_len + 1;
4304 value_len = strcspn(value, ", \t\r\n");
4305 pos = value + value_len + (value[value_len] != '\0');
4306 value[value_len] = '\0';
4307 } else if (key[key_len] == '(') {
4308 /* The value can be surrounded by balanced parentheses. The outermost
4309 * set of parentheses is removed. */
4313 value = key + key_len + 1;
4314 for (value_len = 0; level > 0; value_len++) {
4315 switch (value[value_len]) {
4329 value[value_len - 1] = '\0';
4330 pos = value + value_len;
4332 /* There might be no value at all. */
4333 value = key + key_len; /* Will become the empty string below. */
4334 pos = key + key_len + (key[key_len] != '\0');
4336 key[key_len] = '\0';
4344 /* Encode a dump ports request for 'port', the encoded message
4345 * will be for Open Flow version 'ofp_version'. Returns message
4346 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
4348 ofputil_encode_dump_ports_request(enum ofp_version ofp_version, int16_t port)
4350 struct ofpbuf *request;
4352 switch (ofp_version) {
4353 case OFP10_VERSION: {
4354 struct ofp10_port_stats_request *req;
4355 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0);
4356 req = ofpbuf_put_zeros(request, sizeof *req);
4357 req->port_no = htons(port);
4362 case OFP13_VERSION: {
4363 struct ofp11_port_stats_request *req;
4364 request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0);
4365 req = ofpbuf_put_zeros(request, sizeof *req);
4366 req->port_no = ofputil_port_to_ofp11(port);
4377 ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops,
4378 struct ofp10_port_stats *ps10)
4380 ps10->port_no = htons(ops->port_no);
4381 memset(ps10->pad, 0, sizeof ps10->pad);
4382 put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets));
4383 put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets));
4384 put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes));
4385 put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes));
4386 put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped));
4387 put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped));
4388 put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors));
4389 put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors));
4390 put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors));
4391 put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors));
4392 put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors));
4393 put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions));
4397 ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops,
4398 struct ofp11_port_stats *ps11)
4400 ps11->port_no = ofputil_port_to_ofp11(ops->port_no);
4401 memset(ps11->pad, 0, sizeof ps11->pad);
4402 ps11->rx_packets = htonll(ops->stats.rx_packets);
4403 ps11->tx_packets = htonll(ops->stats.tx_packets);
4404 ps11->rx_bytes = htonll(ops->stats.rx_bytes);
4405 ps11->tx_bytes = htonll(ops->stats.tx_bytes);
4406 ps11->rx_dropped = htonll(ops->stats.rx_dropped);
4407 ps11->tx_dropped = htonll(ops->stats.tx_dropped);
4408 ps11->rx_errors = htonll(ops->stats.rx_errors);
4409 ps11->tx_errors = htonll(ops->stats.tx_errors);
4410 ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors);
4411 ps11->rx_over_err = htonll(ops->stats.rx_over_errors);
4412 ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors);
4413 ps11->collisions = htonll(ops->stats.collisions);
4417 ofputil_port_stats_to_ofp13(const struct ofputil_port_stats *ops,
4418 struct ofp13_port_stats *ps13)
4420 ofputil_port_stats_to_ofp11(ops, &ps13->ps);
4422 /* OF 1.3 adds duration fields */
4423 /* FIXME: Need to implement port alive duration (sec + nsec) */
4424 ps13->duration_sec = htonl(~0);
4425 ps13->duration_nsec = htonl(~0);
4429 /* Encode a ports stat for 'ops' and append it to 'replies'. */
4431 ofputil_append_port_stat(struct list *replies,
4432 const struct ofputil_port_stats *ops)
4434 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
4435 struct ofp_header *oh = msg->data;
4437 switch ((enum ofp_version)oh->version) {
4438 case OFP13_VERSION: {
4439 struct ofp13_port_stats *reply = ofpmp_append(replies, sizeof *reply);
4440 ofputil_port_stats_to_ofp13(ops, reply);
4444 case OFP11_VERSION: {
4445 struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply);
4446 ofputil_port_stats_to_ofp11(ops, reply);
4450 case OFP10_VERSION: {
4451 struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply);
4452 ofputil_port_stats_to_ofp10(ops, reply);
4462 ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops,
4463 const struct ofp10_port_stats *ps10)
4465 memset(ops, 0, sizeof *ops);
4467 ops->port_no = ntohs(ps10->port_no);
4468 ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets));
4469 ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets));
4470 ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes));
4471 ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes));
4472 ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped));
4473 ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped));
4474 ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors));
4475 ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors));
4476 ops->stats.rx_frame_errors =
4477 ntohll(get_32aligned_be64(&ps10->rx_frame_err));
4478 ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err));
4479 ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err));
4480 ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions));
4486 ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops,
4487 const struct ofp11_port_stats *ps11)
4491 memset(ops, 0, sizeof *ops);
4492 error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no);
4497 ops->stats.rx_packets = ntohll(ps11->rx_packets);
4498 ops->stats.tx_packets = ntohll(ps11->tx_packets);
4499 ops->stats.rx_bytes = ntohll(ps11->rx_bytes);
4500 ops->stats.tx_bytes = ntohll(ps11->tx_bytes);
4501 ops->stats.rx_dropped = ntohll(ps11->rx_dropped);
4502 ops->stats.tx_dropped = ntohll(ps11->tx_dropped);
4503 ops->stats.rx_errors = ntohll(ps11->rx_errors);
4504 ops->stats.tx_errors = ntohll(ps11->tx_errors);
4505 ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err);
4506 ops->stats.rx_over_errors = ntohll(ps11->rx_over_err);
4507 ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err);
4508 ops->stats.collisions = ntohll(ps11->collisions);
4514 ofputil_port_stats_from_ofp13(struct ofputil_port_stats *ops,
4515 const struct ofp13_port_stats *ps13)
4518 ofputil_port_stats_from_ofp11(ops, &ps13->ps);
4520 /* FIXME: Get ps13->duration_sec and ps13->duration_nsec,
4521 * Add to netdev_stats? */
4528 /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
4531 ofputil_count_port_stats(const struct ofp_header *oh)
4535 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4536 ofpraw_pull_assert(&b);
4538 BUILD_ASSERT(sizeof(struct ofp10_port_stats) ==
4539 sizeof(struct ofp11_port_stats));
4540 return b.size / sizeof(struct ofp10_port_stats);
4543 /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
4544 * ofputil_port_stats in 'ps'.
4546 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
4547 * message. Calling this function multiple times for a single 'msg' iterates
4548 * through the replies. The caller must initially leave 'msg''s layer pointers
4549 * null and not modify them between calls.
4551 * Returns 0 if successful, EOF if no replies were left in this 'msg',
4552 * otherwise a positive errno value. */
4554 ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg)
4560 ? ofpraw_decode(&raw, msg->l2)
4561 : ofpraw_pull(&raw, msg));
4568 } else if (raw == OFPRAW_OFPST13_PORT_REPLY) {
4569 const struct ofp13_port_stats *ps13;
4571 ps13 = ofpbuf_try_pull(msg, sizeof *ps13);
4575 return ofputil_port_stats_from_ofp13(ps, ps13);
4576 } else if (raw == OFPRAW_OFPST11_PORT_REPLY) {
4577 const struct ofp11_port_stats *ps11;
4579 ps11 = ofpbuf_try_pull(msg, sizeof *ps11);
4583 return ofputil_port_stats_from_ofp11(ps, ps11);
4584 } else if (raw == OFPRAW_OFPST10_PORT_REPLY) {
4585 const struct ofp10_port_stats *ps10;
4587 ps10 = ofpbuf_try_pull(msg, sizeof *ps10);
4591 return ofputil_port_stats_from_ofp10(ps, ps10);
4597 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %zu leftover "
4598 "bytes at end", msg->size);
4599 return OFPERR_OFPBRC_BAD_LEN;
4602 /* Parse a port status request message into a 16 bit OpenFlow 1.0
4603 * port number and stores the latter in '*ofp10_port'.
4604 * Returns 0 if successful, otherwise an OFPERR_* number. */
4606 ofputil_decode_port_stats_request(const struct ofp_header *request,
4607 uint16_t *ofp10_port)
4609 switch ((enum ofp_version)request->version) {
4612 case OFP11_VERSION: {
4613 const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request);
4614 return ofputil_port_from_ofp11(psr11->port_no, ofp10_port);
4617 case OFP10_VERSION: {
4618 const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request);
4619 *ofp10_port = ntohs(psr10->port_no);
4628 /* Parse a queue status request message into 'oqsr'.
4629 * Returns 0 if successful, otherwise an OFPERR_* number. */
4631 ofputil_decode_queue_stats_request(const struct ofp_header *request,
4632 struct ofputil_queue_stats_request *oqsr)
4634 switch ((enum ofp_version)request->version) {
4637 case OFP11_VERSION: {
4638 const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request);
4639 oqsr->queue_id = ntohl(qsr11->queue_id);
4640 return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no);
4643 case OFP10_VERSION: {
4644 const struct ofp10_queue_stats_request *qsr10 = ofpmsg_body(request);
4645 oqsr->queue_id = ntohl(qsr10->queue_id);
4646 oqsr->port_no = ntohs(qsr10->port_no);
4647 /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
4648 if (oqsr->port_no == OFPP_ALL) {
4649 oqsr->port_no = OFPP_ANY;
4659 /* Encode a queue statsrequest for 'oqsr', the encoded message
4660 * will be fore Open Flow version 'ofp_version'. Returns message
4661 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
4663 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
4664 const struct ofputil_queue_stats_request *oqsr)
4666 struct ofpbuf *request;
4668 switch (ofp_version) {
4671 case OFP13_VERSION: {
4672 struct ofp11_queue_stats_request *req;
4673 request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0);
4674 req = ofpbuf_put_zeros(request, sizeof *req);
4675 req->port_no = ofputil_port_to_ofp11(oqsr->port_no);
4676 req->queue_id = htonl(oqsr->queue_id);
4679 case OFP10_VERSION: {
4680 struct ofp10_queue_stats_request *req;
4681 request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0);
4682 req = ofpbuf_put_zeros(request, sizeof *req);
4683 /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
4684 req->port_no = htons(oqsr->port_no == OFPP_ANY
4685 ? OFPP_ALL : oqsr->port_no);
4686 req->queue_id = htonl(oqsr->queue_id);
4696 /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
4699 ofputil_count_queue_stats(const struct ofp_header *oh)
4703 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4704 ofpraw_pull_assert(&b);
4706 BUILD_ASSERT(sizeof(struct ofp10_queue_stats) ==
4707 sizeof(struct ofp11_queue_stats));
4708 return b.size / sizeof(struct ofp10_queue_stats);
4712 ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs,
4713 const struct ofp10_queue_stats *qs10)
4715 oqs->port_no = ntohs(qs10->port_no);
4716 oqs->queue_id = ntohl(qs10->queue_id);
4717 oqs->stats.tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes));
4718 oqs->stats.tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets));
4719 oqs->stats.tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors));
4725 ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs,
4726 const struct ofp11_queue_stats *qs11)
4730 error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no);
4735 oqs->queue_id = ntohl(qs11->queue_id);
4736 oqs->stats.tx_bytes = ntohll(qs11->tx_bytes);
4737 oqs->stats.tx_packets = ntohll(qs11->tx_packets);
4738 oqs->stats.tx_errors = ntohll(qs11->tx_errors);
4744 ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats *oqs,
4745 const struct ofp13_queue_stats *qs13)
4748 = ofputil_queue_stats_from_ofp11(oqs, &qs13->qs);
4750 /* FIXME: Get qs13->duration_sec and qs13->duration_nsec,
4751 * Add to netdev_queue_stats? */
4757 /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
4758 * ofputil_queue_stats in 'qs'.
4760 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
4761 * message. Calling this function multiple times for a single 'msg' iterates
4762 * through the replies. The caller must initially leave 'msg''s layer pointers
4763 * null and not modify them between calls.
4765 * Returns 0 if successful, EOF if no replies were left in this 'msg',
4766 * otherwise a positive errno value. */
4768 ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg)
4774 ? ofpraw_decode(&raw, msg->l2)
4775 : ofpraw_pull(&raw, msg));
4782 } else if (raw == OFPRAW_OFPST13_QUEUE_REPLY) {
4783 const struct ofp13_queue_stats *qs13;
4785 qs13 = ofpbuf_try_pull(msg, sizeof *qs13);
4789 return ofputil_queue_stats_from_ofp13(qs, qs13);
4790 } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) {
4791 const struct ofp11_queue_stats *qs11;
4793 qs11 = ofpbuf_try_pull(msg, sizeof *qs11);
4797 return ofputil_queue_stats_from_ofp11(qs, qs11);
4798 } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) {
4799 const struct ofp10_queue_stats *qs10;
4801 qs10 = ofpbuf_try_pull(msg, sizeof *qs10);
4805 return ofputil_queue_stats_from_ofp10(qs, qs10);
4811 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %zu leftover "
4812 "bytes at end", msg->size);
4813 return OFPERR_OFPBRC_BAD_LEN;
4817 ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs,
4818 struct ofp10_queue_stats *qs10)
4820 qs10->port_no = htons(oqs->port_no);
4821 memset(qs10->pad, 0, sizeof qs10->pad);
4822 qs10->queue_id = htonl(oqs->queue_id);
4823 put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->stats.tx_bytes));
4824 put_32aligned_be64(&qs10->tx_packets, htonll(oqs->stats.tx_packets));
4825 put_32aligned_be64(&qs10->tx_errors, htonll(oqs->stats.tx_errors));
4829 ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs,
4830 struct ofp11_queue_stats *qs11)
4832 qs11->port_no = ofputil_port_to_ofp11(oqs->port_no);
4833 qs11->queue_id = htonl(oqs->queue_id);
4834 qs11->tx_bytes = htonll(oqs->stats.tx_bytes);
4835 qs11->tx_packets = htonll(oqs->stats.tx_packets);
4836 qs11->tx_errors = htonll(oqs->stats.tx_errors);
4840 ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats *oqs,
4841 struct ofp13_queue_stats *qs13)
4843 ofputil_queue_stats_to_ofp11(oqs, &qs13->qs);
4844 /* OF 1.3 adds duration fields */
4845 /* FIXME: Need to implement queue alive duration (sec + nsec) */
4846 qs13->duration_sec = htonl(~0);
4847 qs13->duration_nsec = htonl(~0);
4850 /* Encode a queue stat for 'oqs' and append it to 'replies'. */
4852 ofputil_append_queue_stat(struct list *replies,
4853 const struct ofputil_queue_stats *oqs)
4855 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
4856 struct ofp_header *oh = msg->data;
4858 switch ((enum ofp_version)oh->version) {
4859 case OFP13_VERSION: {
4860 struct ofp13_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
4861 ofputil_queue_stats_to_ofp13(oqs, reply);
4866 case OFP11_VERSION: {
4867 struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
4868 ofputil_queue_stats_to_ofp11(oqs, reply);
4872 case OFP10_VERSION: {
4873 struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
4874 ofputil_queue_stats_to_ofp10(oqs, reply);