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 == 17);
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_ANY, "OpenFlow10" },
586 { OFPUTIL_P_NXM_ANY, "NXM" },
588 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
590 enum ofputil_protocol ofputil_flow_dump_protocols[] = {
594 size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
596 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
597 * connection that has negotiated the given 'version'. 'version' should
598 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
599 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
600 * outside the valid range. */
601 enum ofputil_protocol
602 ofputil_protocol_from_ofp_version(enum ofp_version version)
606 return OFPUTIL_P_OF10;
608 return OFPUTIL_P_OF12;
615 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
616 * OFP11_VERSION or OFP12_VERSION) that corresponds to 'protocol'. */
618 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
622 case OFPUTIL_P_OF10_TID:
624 case OFPUTIL_P_NXM_TID:
625 return OFP10_VERSION;
627 return OFP12_VERSION;
633 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
636 ofputil_protocol_is_valid(enum ofputil_protocol protocol)
638 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
641 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
642 * extension turned on or off if 'enable' is true or false, respectively.
644 * This extension is only useful for protocols whose "standard" version does
645 * not allow specific tables to be modified. In particular, this is true of
646 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
647 * specifies a table ID and so there is no need for such an extension. When
648 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
649 * extension, this function just returns its 'protocol' argument unchanged
650 * regardless of the value of 'enable'. */
651 enum ofputil_protocol
652 ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
656 case OFPUTIL_P_OF10_TID:
657 return enable ? OFPUTIL_P_OF10_TID : OFPUTIL_P_OF10;
660 case OFPUTIL_P_NXM_TID:
661 return enable ? OFPUTIL_P_NXM_TID : OFPUTIL_P_NXM;
664 return OFPUTIL_P_OF12;
671 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
672 * some extension to a standard protocol version, the return value is the
673 * standard version of that protocol without any extension. If 'protocol' is a
674 * standard protocol version, returns 'protocol' unchanged. */
675 enum ofputil_protocol
676 ofputil_protocol_to_base(enum ofputil_protocol protocol)
678 return ofputil_protocol_set_tid(protocol, false);
681 /* Returns 'new_base' with any extensions taken from 'cur'. */
682 enum ofputil_protocol
683 ofputil_protocol_set_base(enum ofputil_protocol cur,
684 enum ofputil_protocol new_base)
686 bool tid = (cur & OFPUTIL_P_TID) != 0;
690 case OFPUTIL_P_OF10_TID:
691 return ofputil_protocol_set_tid(OFPUTIL_P_OF10, tid);
694 case OFPUTIL_P_NXM_TID:
695 return ofputil_protocol_set_tid(OFPUTIL_P_NXM, tid);
698 return ofputil_protocol_set_tid(OFPUTIL_P_OF12, tid);
705 /* Returns a string form of 'protocol', if a simple form exists (that is, if
706 * 'protocol' is either a single protocol or it is a combination of protocols
707 * that have a single abbreviation). Otherwise, returns NULL. */
709 ofputil_protocol_to_string(enum ofputil_protocol protocol)
711 const struct proto_abbrev *p;
713 /* Use a "switch" statement for single-bit names so that we get a compiler
714 * warning if we forget any. */
717 return "NXM-table_id";
719 case OFPUTIL_P_NXM_TID:
720 return "NXM+table_id";
723 return "OpenFlow10-table_id";
725 case OFPUTIL_P_OF10_TID:
726 return "OpenFlow10+table_id";
732 /* Check abbreviations. */
733 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
734 if (protocol == p->protocol) {
742 /* Returns a string that represents 'protocols'. The return value might be a
743 * comma-separated list if 'protocols' doesn't have a simple name. The return
744 * value is "none" if 'protocols' is 0.
746 * The caller must free the returned string (with free()). */
748 ofputil_protocols_to_string(enum ofputil_protocol protocols)
752 assert(!(protocols & ~OFPUTIL_P_ANY));
753 if (protocols == 0) {
754 return xstrdup("none");
759 const struct proto_abbrev *p;
763 ds_put_char(&s, ',');
766 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
767 if ((protocols & p->protocol) == p->protocol) {
768 ds_put_cstr(&s, p->name);
769 protocols &= ~p->protocol;
774 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
775 enum ofputil_protocol bit = 1u << i;
777 if (protocols & bit) {
778 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
787 return ds_steal_cstr(&s);
790 static enum ofputil_protocol
791 ofputil_protocol_from_string__(const char *s, size_t n)
793 const struct proto_abbrev *p;
796 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
797 enum ofputil_protocol bit = 1u << i;
798 const char *name = ofputil_protocol_to_string(bit);
800 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
805 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
806 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
814 /* Returns the nonempty set of protocols represented by 's', which can be a
815 * single protocol name or abbreviation or a comma-separated list of them.
817 * Aborts the program with an error message if 's' is invalid. */
818 enum ofputil_protocol
819 ofputil_protocols_from_string(const char *s)
821 const char *orig_s = s;
822 enum ofputil_protocol protocols;
826 enum ofputil_protocol p;
835 p = ofputil_protocol_from_string__(s, n);
837 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
845 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
850 static enum ofp_version
851 ofputil_version_from_string(const char *s)
853 if (!strcasecmp(s, "OpenFlow10")) {
854 return OFP10_VERSION;
856 if (!strcasecmp(s, "OpenFlow11")) {
857 return OFP11_VERSION;
859 if (!strcasecmp(s, "OpenFlow12")) {
860 return OFP12_VERSION;
862 VLOG_FATAL("Unknown OpenFlow version: \"%s\"", s);
868 return isspace(c) || c == ',';
872 ofputil_versions_from_string(const char *s)
879 enum ofp_version version;
882 if (is_delimiter(s[i])) {
887 while (s[i + j] && !is_delimiter(s[i + j])) {
890 key = xmemdup0(s + i, j);
891 version = ofputil_version_from_string(key);
893 bitmap |= 1u << version;
901 ofputil_version_to_string(enum ofp_version ofp_version)
903 switch (ofp_version) {
916 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
918 switch (packet_in_format) {
919 case NXPIF_OPENFLOW10:
928 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
930 switch (packet_in_format) {
931 case NXPIF_OPENFLOW10:
941 ofputil_packet_in_format_from_string(const char *s)
943 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
944 : !strcmp(s, "nxm") ? NXPIF_NXM
949 regs_fully_wildcarded(const struct flow_wildcards *wc)
953 for (i = 0; i < FLOW_N_REGS; i++) {
954 if (wc->masks.regs[i] != 0) {
961 /* Returns a bit-mask of ofputil_protocols that can be used for sending 'match'
962 * to a switch (e.g. to add or remove a flow). Only NXM can handle tunnel IDs,
963 * registers, or fixing the Ethernet multicast bit. Otherwise, it's better to
964 * use OpenFlow 1.0 protocol for backward compatibility. */
965 enum ofputil_protocol
966 ofputil_usable_protocols(const struct match *match)
968 const struct flow_wildcards *wc = &match->wc;
970 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 17);
972 /* NXM and OF1.1+ supports bitwise matching on ethernet addresses. */
973 if (!eth_mask_is_exact(wc->masks.dl_src)
974 && !eth_addr_is_zero(wc->masks.dl_src)) {
975 return OFPUTIL_P_NXM_ANY;
977 if (!eth_mask_is_exact(wc->masks.dl_dst)
978 && !eth_addr_is_zero(wc->masks.dl_dst)) {
979 return OFPUTIL_P_NXM_ANY;
982 /* NXM and OF1.1+ support matching metadata. */
983 if (wc->masks.metadata != htonll(0)) {
984 return OFPUTIL_P_NXM_ANY;
987 /* Only NXM supports matching ARP hardware addresses. */
988 if (!eth_addr_is_zero(wc->masks.arp_sha) ||
989 !eth_addr_is_zero(wc->masks.arp_tha)) {
990 return OFPUTIL_P_NXM_ANY;
993 /* Only NXM supports matching IPv6 traffic. */
994 if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
995 return OFPUTIL_P_NXM_ANY;
998 /* Only NXM supports matching registers. */
999 if (!regs_fully_wildcarded(wc)) {
1000 return OFPUTIL_P_NXM_ANY;
1003 /* Only NXM supports matching tun_id. */
1004 if (wc->masks.tunnel.tun_id != htonll(0)) {
1005 return OFPUTIL_P_NXM_ANY;
1008 /* Only NXM supports matching fragments. */
1009 if (wc->masks.nw_frag) {
1010 return OFPUTIL_P_NXM_ANY;
1013 /* Only NXM supports matching IPv6 flow label. */
1014 if (wc->masks.ipv6_label) {
1015 return OFPUTIL_P_NXM_ANY;
1018 /* Only NXM supports matching IP ECN bits. */
1019 if (wc->masks.nw_tos & IP_ECN_MASK) {
1020 return OFPUTIL_P_NXM_ANY;
1023 /* Only NXM supports matching IP TTL/hop limit. */
1024 if (wc->masks.nw_ttl) {
1025 return OFPUTIL_P_NXM_ANY;
1028 /* Only NXM supports non-CIDR IPv4 address masks. */
1029 if (!ip_is_cidr(wc->masks.nw_src) || !ip_is_cidr(wc->masks.nw_dst)) {
1030 return OFPUTIL_P_NXM_ANY;
1033 /* Only NXM supports bitwise matching on transport port. */
1034 if ((wc->masks.tp_src && wc->masks.tp_src != htons(UINT16_MAX)) ||
1035 (wc->masks.tp_dst && wc->masks.tp_dst != htons(UINT16_MAX))) {
1036 return OFPUTIL_P_NXM_ANY;
1039 /* Other formats can express this rule. */
1040 return OFPUTIL_P_ANY;
1044 ofputil_format_version(struct ds *msg, enum ofp_version version)
1046 ds_put_format(msg, "0x%02x", version);
1050 ofputil_format_version_name(struct ds *msg, enum ofp_version version)
1052 ds_put_cstr(msg, ofputil_version_to_string(version));
1056 ofputil_format_version_bitmap__(struct ds *msg, uint32_t bitmap,
1057 void (*format_version)(struct ds *msg,
1061 format_version(msg, raw_ctz(bitmap));
1062 bitmap = zero_rightmost_1bit(bitmap);
1064 ds_put_cstr(msg, ", ");
1070 ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap)
1072 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version);
1076 ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap)
1078 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version_name);
1082 ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header *oheh,
1083 uint32_t *allowed_versionsp)
1085 uint16_t bitmap_len = ntohs(oheh->length) - sizeof *oheh;
1086 const ovs_be32 *bitmap = (const ovs_be32 *) (oheh + 1);
1087 uint32_t allowed_versions;
1089 if (!bitmap_len || bitmap_len % sizeof *bitmap) {
1093 /* Only use the first 32-bit element of the bitmap as that is all the
1094 * current implementation supports. Subsequent elements are ignored which
1095 * should have no effect on session negotiation until Open vSwtich supports
1096 * wire-protocol versions greater than 31.
1098 allowed_versions = ntohl(bitmap[0]);
1100 if (allowed_versions & 1) {
1101 /* There's no OpenFlow version 0. */
1102 VLOG_WARN_RL(&bad_ofmsg_rl, "peer claims to support invalid OpenFlow "
1104 allowed_versions &= ~1u;
1107 if (!allowed_versions) {
1108 VLOG_WARN_RL(&bad_ofmsg_rl, "peer does not support any OpenFlow "
1109 "version (between 0x01 and 0x1f)");
1113 *allowed_versionsp = allowed_versions;
1118 version_bitmap_from_version(uint8_t ofp_version)
1120 return ((ofp_version < 32 ? 1u << ofp_version : 0) - 1) << 1;
1123 /* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions'
1124 * the set of OpenFlow versions for which 'oh' announces support.
1126 * Because of how OpenFlow defines OFPT_HELLO messages, this function is always
1127 * successful, and thus '*allowed_versions' is always initialized. However, it
1128 * returns false if 'oh' contains some data that could not be fully understood,
1129 * true if 'oh' was completely parsed. */
1131 ofputil_decode_hello(const struct ofp_header *oh, uint32_t *allowed_versions)
1136 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
1137 ofpbuf_pull(&msg, sizeof *oh);
1139 *allowed_versions = version_bitmap_from_version(oh->version);
1141 const struct ofp_hello_elem_header *oheh;
1144 if (msg.size < sizeof *oheh) {
1149 len = ntohs(oheh->length);
1150 if (len < sizeof *oheh || !ofpbuf_try_pull(&msg, ROUND_UP(len, 8))) {
1154 if (oheh->type != htons(OFPHET_VERSIONBITMAP)
1155 || !ofputil_decode_hello_bitmap(oheh, allowed_versions)) {
1163 /* Returns true if 'allowed_versions' needs to be accompanied by a version
1164 * bitmap to be correctly expressed in an OFPT_HELLO message. */
1166 should_send_version_bitmap(uint32_t allowed_versions)
1168 return !is_pow2((allowed_versions >> 1) + 1);
1171 /* Create an OFPT_HELLO message that expresses support for the OpenFlow
1172 * versions in the 'allowed_versions' bitmaps and returns the message. */
1174 ofputil_encode_hello(uint32_t allowed_versions)
1176 enum ofp_version ofp_version;
1179 ofp_version = leftmost_1bit_idx(allowed_versions);
1180 msg = ofpraw_alloc(OFPRAW_OFPT_HELLO, ofp_version, 0);
1182 if (should_send_version_bitmap(allowed_versions)) {
1183 struct ofp_hello_elem_header *oheh;
1186 map_len = sizeof allowed_versions;
1187 oheh = ofpbuf_put_zeros(msg, ROUND_UP(map_len + sizeof *oheh, 8));
1188 oheh->type = htons(OFPHET_VERSIONBITMAP);
1189 oheh->length = htons(map_len + sizeof *oheh);
1190 *(ovs_be32 *)(oheh + 1) = htonl(allowed_versions);
1196 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1197 * protocol is 'current', at least partly transitions the protocol to 'want'.
1198 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1199 * connection if the switch processes the returned message correctly. (If
1200 * '*next != want' then the caller will have to iterate.)
1202 * If 'current == want', returns NULL and stores 'current' in '*next'. */
1204 ofputil_encode_set_protocol(enum ofputil_protocol current,
1205 enum ofputil_protocol want,
1206 enum ofputil_protocol *next)
1208 enum ofputil_protocol cur_base, want_base;
1209 bool cur_tid, want_tid;
1211 cur_base = ofputil_protocol_to_base(current);
1212 want_base = ofputil_protocol_to_base(want);
1213 if (cur_base != want_base) {
1214 *next = ofputil_protocol_set_base(current, want_base);
1216 switch (want_base) {
1218 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1220 case OFPUTIL_P_OF10:
1221 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1223 case OFPUTIL_P_OF12:
1224 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW12);
1226 case OFPUTIL_P_OF10_TID:
1227 case OFPUTIL_P_NXM_TID:
1232 cur_tid = (current & OFPUTIL_P_TID) != 0;
1233 want_tid = (want & OFPUTIL_P_TID) != 0;
1234 if (cur_tid != want_tid) {
1235 *next = ofputil_protocol_set_tid(current, want_tid);
1236 return ofputil_make_flow_mod_table_id(want_tid);
1239 assert(current == want);
1245 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1246 * format to 'nxff'. */
1248 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1250 struct nx_set_flow_format *sff;
1253 assert(ofputil_nx_flow_format_is_valid(nxff));
1255 msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1256 sff = ofpbuf_put_zeros(msg, sizeof *sff);
1257 sff->format = htonl(nxff);
1262 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1264 enum ofputil_protocol
1265 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1267 switch (flow_format) {
1268 case NXFF_OPENFLOW10:
1269 return OFPUTIL_P_OF10;
1272 return OFPUTIL_P_NXM;
1274 case NXFF_OPENFLOW12:
1275 return OFPUTIL_P_OF12;
1282 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1284 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1286 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1289 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1292 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1294 switch (flow_format) {
1295 case NXFF_OPENFLOW10:
1296 return "openflow10";
1299 case NXFF_OPENFLOW12:
1300 return "openflow12";
1307 ofputil_make_set_packet_in_format(enum ofp_version ofp_version,
1308 enum nx_packet_in_format packet_in_format)
1310 struct nx_set_packet_in_format *spif;
1313 msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, ofp_version, 0);
1314 spif = ofpbuf_put_zeros(msg, sizeof *spif);
1315 spif->format = htonl(packet_in_format);
1320 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1321 * extension on or off (according to 'flow_mod_table_id'). */
1323 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1325 struct nx_flow_mod_table_id *nfmti;
1328 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1329 nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
1330 nfmti->set = flow_mod_table_id;
1334 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1335 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1338 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1339 * The caller must initialize 'ofpacts' and retains ownership of it.
1340 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1342 * Does not validate the flow_mod actions. The caller should do that, with
1343 * ofpacts_check(). */
1345 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1346 const struct ofp_header *oh,
1347 enum ofputil_protocol protocol,
1348 struct ofpbuf *ofpacts)
1354 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1355 raw = ofpraw_pull_assert(&b);
1356 if (raw == OFPRAW_OFPT11_FLOW_MOD) {
1357 /* Standard OpenFlow 1.1 flow_mod. */
1358 const struct ofp11_flow_mod *ofm;
1361 ofm = ofpbuf_pull(&b, sizeof *ofm);
1363 error = ofputil_pull_ofp11_match(&b, &fm->match, NULL);
1368 error = ofpacts_pull_openflow11_instructions(&b, b.size, ofpacts);
1373 /* Translate the message. */
1374 fm->priority = ntohs(ofm->priority);
1375 if (ofm->command == OFPFC_ADD) {
1376 fm->cookie = htonll(0);
1377 fm->cookie_mask = htonll(0);
1378 fm->new_cookie = ofm->cookie;
1380 fm->cookie = ofm->cookie;
1381 fm->cookie_mask = ofm->cookie_mask;
1382 fm->new_cookie = htonll(UINT64_MAX);
1384 fm->command = ofm->command;
1385 fm->table_id = ofm->table_id;
1386 fm->idle_timeout = ntohs(ofm->idle_timeout);
1387 fm->hard_timeout = ntohs(ofm->hard_timeout);
1388 fm->buffer_id = ntohl(ofm->buffer_id);
1389 error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
1393 if (ofm->out_group != htonl(OFPG_ANY)) {
1394 return OFPERR_OFPFMFC_UNKNOWN;
1396 fm->flags = ntohs(ofm->flags);
1398 if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1399 /* Standard OpenFlow 1.0 flow_mod. */
1400 const struct ofp10_flow_mod *ofm;
1403 /* Get the ofp10_flow_mod. */
1404 ofm = ofpbuf_pull(&b, sizeof *ofm);
1406 /* Translate the rule. */
1407 ofputil_match_from_ofp10_match(&ofm->match, &fm->match);
1408 ofputil_normalize_match(&fm->match);
1410 /* Now get the actions. */
1411 error = ofpacts_pull_openflow10(&b, b.size, ofpacts);
1416 /* OpenFlow 1.0 says that exact-match rules have to have the
1417 * highest possible priority. */
1418 fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL)
1419 ? ntohs(ofm->priority)
1422 /* Translate the message. */
1423 command = ntohs(ofm->command);
1424 fm->cookie = htonll(0);
1425 fm->cookie_mask = htonll(0);
1426 fm->new_cookie = ofm->cookie;
1427 fm->idle_timeout = ntohs(ofm->idle_timeout);
1428 fm->hard_timeout = ntohs(ofm->hard_timeout);
1429 fm->buffer_id = ntohl(ofm->buffer_id);
1430 fm->out_port = ntohs(ofm->out_port);
1431 fm->flags = ntohs(ofm->flags);
1432 } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1433 /* Nicira extended flow_mod. */
1434 const struct nx_flow_mod *nfm;
1437 /* Dissect the message. */
1438 nfm = ofpbuf_pull(&b, sizeof *nfm);
1439 error = nx_pull_match(&b, ntohs(nfm->match_len),
1440 &fm->match, &fm->cookie, &fm->cookie_mask);
1444 error = ofpacts_pull_openflow10(&b, b.size, ofpacts);
1449 /* Translate the message. */
1450 command = ntohs(nfm->command);
1451 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1452 /* Flow additions may only set a new cookie, not match an
1453 * existing cookie. */
1454 return OFPERR_NXBRC_NXM_INVALID;
1456 fm->priority = ntohs(nfm->priority);
1457 fm->new_cookie = nfm->cookie;
1458 fm->idle_timeout = ntohs(nfm->idle_timeout);
1459 fm->hard_timeout = ntohs(nfm->hard_timeout);
1460 fm->buffer_id = ntohl(nfm->buffer_id);
1461 fm->out_port = ntohs(nfm->out_port);
1462 fm->flags = ntohs(nfm->flags);
1467 if (protocol & OFPUTIL_P_TID) {
1468 fm->command = command & 0xff;
1469 fm->table_id = command >> 8;
1471 fm->command = command;
1472 fm->table_id = 0xff;
1476 fm->ofpacts = ofpacts->data;
1477 fm->ofpacts_len = ofpacts->size;
1483 ofputil_tid_command(const struct ofputil_flow_mod *fm,
1484 enum ofputil_protocol protocol)
1486 return htons(protocol & OFPUTIL_P_TID
1487 ? (fm->command & 0xff) | (fm->table_id << 8)
1491 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
1492 * 'protocol' and returns the message. */
1494 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
1495 enum ofputil_protocol protocol)
1500 case OFPUTIL_P_OF12: {
1501 struct ofp11_flow_mod *ofm;
1503 msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, OFP12_VERSION,
1504 NXM_TYPICAL_LEN + fm->ofpacts_len);
1505 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
1506 if (fm->command == OFPFC_ADD) {
1507 ofm->cookie = fm->new_cookie;
1509 ofm->cookie = fm->cookie;
1511 ofm->cookie_mask = fm->cookie_mask;
1512 ofm->table_id = fm->table_id;
1513 ofm->command = fm->command;
1514 ofm->idle_timeout = htons(fm->idle_timeout);
1515 ofm->hard_timeout = htons(fm->hard_timeout);
1516 ofm->priority = htons(fm->priority);
1517 ofm->buffer_id = htonl(fm->buffer_id);
1518 ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
1519 ofm->out_group = htonl(OFPG11_ANY);
1520 ofm->flags = htons(fm->flags);
1521 oxm_put_match(msg, &fm->match);
1522 ofpacts_put_openflow11_instructions(fm->ofpacts, fm->ofpacts_len, msg);
1526 case OFPUTIL_P_OF10:
1527 case OFPUTIL_P_OF10_TID: {
1528 struct ofp10_flow_mod *ofm;
1530 msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
1532 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
1533 ofputil_match_to_ofp10_match(&fm->match, &ofm->match);
1534 ofm->cookie = fm->new_cookie;
1535 ofm->command = ofputil_tid_command(fm, protocol);
1536 ofm->idle_timeout = htons(fm->idle_timeout);
1537 ofm->hard_timeout = htons(fm->hard_timeout);
1538 ofm->priority = htons(fm->priority);
1539 ofm->buffer_id = htonl(fm->buffer_id);
1540 ofm->out_port = htons(fm->out_port);
1541 ofm->flags = htons(fm->flags);
1542 ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
1547 case OFPUTIL_P_NXM_TID: {
1548 struct nx_flow_mod *nfm;
1551 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
1552 NXM_TYPICAL_LEN + fm->ofpacts_len);
1553 nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
1554 nfm->command = ofputil_tid_command(fm, protocol);
1555 nfm->cookie = fm->new_cookie;
1556 match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask);
1558 nfm->idle_timeout = htons(fm->idle_timeout);
1559 nfm->hard_timeout = htons(fm->hard_timeout);
1560 nfm->priority = htons(fm->priority);
1561 nfm->buffer_id = htonl(fm->buffer_id);
1562 nfm->out_port = htons(fm->out_port);
1563 nfm->flags = htons(fm->flags);
1564 nfm->match_len = htons(match_len);
1565 ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
1573 ofpmsg_update_length(msg);
1577 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1578 * send all of the 'n_fm's flow table modification requests in 'fms', and a
1579 * 0-bit for each protocol that is inadequate.
1581 * (The return value will have at least one 1-bit.) */
1582 enum ofputil_protocol
1583 ofputil_flow_mod_usable_protocols(const struct ofputil_flow_mod *fms,
1586 enum ofputil_protocol usable_protocols;
1589 usable_protocols = OFPUTIL_P_ANY;
1590 for (i = 0; i < n_fms; i++) {
1591 const struct ofputil_flow_mod *fm = &fms[i];
1593 usable_protocols &= ofputil_usable_protocols(&fm->match);
1594 if (fm->table_id != 0xff) {
1595 usable_protocols &= OFPUTIL_P_TID;
1598 /* Matching of the cookie is only supported through NXM. */
1599 if (fm->cookie_mask != htonll(0)) {
1600 usable_protocols &= OFPUTIL_P_NXM_ANY;
1603 assert(usable_protocols);
1605 return usable_protocols;
1609 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr,
1610 const struct ofp10_flow_stats_request *ofsr,
1613 fsr->aggregate = aggregate;
1614 ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match);
1615 fsr->out_port = ntohs(ofsr->out_port);
1616 fsr->table_id = ofsr->table_id;
1617 fsr->cookie = fsr->cookie_mask = htonll(0);
1623 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr,
1624 struct ofpbuf *b, bool aggregate)
1626 const struct ofp11_flow_stats_request *ofsr;
1629 ofsr = ofpbuf_pull(b, sizeof *ofsr);
1630 fsr->aggregate = aggregate;
1631 fsr->table_id = ofsr->table_id;
1632 error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port);
1636 if (ofsr->out_group != htonl(OFPG11_ANY)) {
1637 return OFPERR_OFPFMFC_UNKNOWN;
1639 fsr->cookie = ofsr->cookie;
1640 fsr->cookie_mask = ofsr->cookie_mask;
1641 error = ofputil_pull_ofp11_match(b, &fsr->match, NULL);
1650 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
1651 struct ofpbuf *b, bool aggregate)
1653 const struct nx_flow_stats_request *nfsr;
1656 nfsr = ofpbuf_pull(b, sizeof *nfsr);
1657 error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match,
1658 &fsr->cookie, &fsr->cookie_mask);
1663 return OFPERR_OFPBRC_BAD_LEN;
1666 fsr->aggregate = aggregate;
1667 fsr->out_port = ntohs(nfsr->out_port);
1668 fsr->table_id = nfsr->table_id;
1673 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1674 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1675 * successful, otherwise an OpenFlow error code. */
1677 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
1678 const struct ofp_header *oh)
1683 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1684 raw = ofpraw_pull_assert(&b);
1685 switch ((int) raw) {
1686 case OFPRAW_OFPST10_FLOW_REQUEST:
1687 return ofputil_decode_ofpst10_flow_request(fsr, b.data, false);
1689 case OFPRAW_OFPST10_AGGREGATE_REQUEST:
1690 return ofputil_decode_ofpst10_flow_request(fsr, b.data, true);
1692 case OFPRAW_OFPST11_FLOW_REQUEST:
1693 return ofputil_decode_ofpst11_flow_request(fsr, &b, false);
1695 case OFPRAW_OFPST11_AGGREGATE_REQUEST:
1696 return ofputil_decode_ofpst11_flow_request(fsr, &b, true);
1698 case OFPRAW_NXST_FLOW_REQUEST:
1699 return ofputil_decode_nxst_flow_request(fsr, &b, false);
1701 case OFPRAW_NXST_AGGREGATE_REQUEST:
1702 return ofputil_decode_nxst_flow_request(fsr, &b, true);
1705 /* Hey, the caller lied. */
1710 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1711 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1712 * 'protocol', and returns the message. */
1714 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
1715 enum ofputil_protocol protocol)
1721 case OFPUTIL_P_OF12: {
1722 struct ofp11_flow_stats_request *ofsr;
1724 raw = (fsr->aggregate
1725 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
1726 : OFPRAW_OFPST11_FLOW_REQUEST);
1727 msg = ofpraw_alloc(raw, OFP12_VERSION, NXM_TYPICAL_LEN);
1728 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
1729 ofsr->table_id = fsr->table_id;
1730 ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port);
1731 ofsr->out_group = htonl(OFPG11_ANY);
1732 ofsr->cookie = fsr->cookie;
1733 ofsr->cookie_mask = fsr->cookie_mask;
1734 oxm_put_match(msg, &fsr->match);
1738 case OFPUTIL_P_OF10:
1739 case OFPUTIL_P_OF10_TID: {
1740 struct ofp10_flow_stats_request *ofsr;
1742 raw = (fsr->aggregate
1743 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
1744 : OFPRAW_OFPST10_FLOW_REQUEST);
1745 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
1746 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
1747 ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match);
1748 ofsr->table_id = fsr->table_id;
1749 ofsr->out_port = htons(fsr->out_port);
1754 case OFPUTIL_P_NXM_TID: {
1755 struct nx_flow_stats_request *nfsr;
1758 raw = (fsr->aggregate
1759 ? OFPRAW_NXST_AGGREGATE_REQUEST
1760 : OFPRAW_NXST_FLOW_REQUEST);
1761 msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN);
1762 ofpbuf_put_zeros(msg, sizeof *nfsr);
1763 match_len = nx_put_match(msg, &fsr->match,
1764 fsr->cookie, fsr->cookie_mask);
1767 nfsr->out_port = htons(fsr->out_port);
1768 nfsr->match_len = htons(match_len);
1769 nfsr->table_id = fsr->table_id;
1780 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1781 * accurately encode 'fsr', and a 0-bit for each protocol that is inadequate.
1783 * (The return value will have at least one 1-bit.) */
1784 enum ofputil_protocol
1785 ofputil_flow_stats_request_usable_protocols(
1786 const struct ofputil_flow_stats_request *fsr)
1788 enum ofputil_protocol usable_protocols;
1790 usable_protocols = ofputil_usable_protocols(&fsr->match);
1791 if (fsr->cookie_mask != htonll(0)) {
1792 usable_protocols &= OFPUTIL_P_NXM_ANY;
1794 return usable_protocols;
1797 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1798 * ofputil_flow_stats in 'fs'.
1800 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1801 * OpenFlow message. Calling this function multiple times for a single 'msg'
1802 * iterates through the replies. The caller must initially leave 'msg''s layer
1803 * pointers null and not modify them between calls.
1805 * Most switches don't send the values needed to populate fs->idle_age and
1806 * fs->hard_age, so those members will usually be set to 0. If the switch from
1807 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
1808 * 'flow_age_extension' as true so that the contents of 'msg' determine the
1809 * 'idle_age' and 'hard_age' members in 'fs'.
1811 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
1812 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
1813 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
1815 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1816 * otherwise a positive errno value. */
1818 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1820 bool flow_age_extension,
1821 struct ofpbuf *ofpacts)
1827 ? ofpraw_decode(&raw, msg->l2)
1828 : ofpraw_pull(&raw, msg));
1835 } else if (raw == OFPRAW_OFPST11_FLOW_REPLY) {
1836 const struct ofp11_flow_stats *ofs;
1838 uint16_t padded_match_len;
1840 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1842 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1843 "bytes at end", msg->size);
1847 length = ntohs(ofs->length);
1848 if (length < sizeof *ofs) {
1849 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1850 "length %zu", length);
1854 if (ofputil_pull_ofp11_match(msg, &fs->match, &padded_match_len)) {
1855 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match");
1859 if (ofpacts_pull_openflow11_instructions(msg, length - sizeof *ofs -
1860 padded_match_len, ofpacts)) {
1861 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions");
1865 fs->priority = ntohs(ofs->priority);
1866 fs->table_id = ofs->table_id;
1867 fs->duration_sec = ntohl(ofs->duration_sec);
1868 fs->duration_nsec = ntohl(ofs->duration_nsec);
1869 fs->idle_timeout = ntohs(ofs->idle_timeout);
1870 fs->hard_timeout = ntohs(ofs->hard_timeout);
1873 fs->cookie = ofs->cookie;
1874 fs->packet_count = ntohll(ofs->packet_count);
1875 fs->byte_count = ntohll(ofs->byte_count);
1876 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
1877 const struct ofp10_flow_stats *ofs;
1880 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1882 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1883 "bytes at end", msg->size);
1887 length = ntohs(ofs->length);
1888 if (length < sizeof *ofs) {
1889 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1890 "length %zu", length);
1894 if (ofpacts_pull_openflow10(msg, length - sizeof *ofs, ofpacts)) {
1898 fs->cookie = get_32aligned_be64(&ofs->cookie);
1899 ofputil_match_from_ofp10_match(&ofs->match, &fs->match);
1900 fs->priority = ntohs(ofs->priority);
1901 fs->table_id = ofs->table_id;
1902 fs->duration_sec = ntohl(ofs->duration_sec);
1903 fs->duration_nsec = ntohl(ofs->duration_nsec);
1904 fs->idle_timeout = ntohs(ofs->idle_timeout);
1905 fs->hard_timeout = ntohs(ofs->hard_timeout);
1908 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1909 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
1910 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
1911 const struct nx_flow_stats *nfs;
1912 size_t match_len, actions_len, length;
1914 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1916 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1917 "bytes at end", msg->size);
1921 length = ntohs(nfs->length);
1922 match_len = ntohs(nfs->match_len);
1923 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1924 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1925 "claims invalid length %zu", match_len, length);
1928 if (nx_pull_match(msg, match_len, &fs->match, NULL, NULL)) {
1932 actions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
1933 if (ofpacts_pull_openflow10(msg, actions_len, ofpacts)) {
1937 fs->cookie = nfs->cookie;
1938 fs->table_id = nfs->table_id;
1939 fs->duration_sec = ntohl(nfs->duration_sec);
1940 fs->duration_nsec = ntohl(nfs->duration_nsec);
1941 fs->priority = ntohs(nfs->priority);
1942 fs->idle_timeout = ntohs(nfs->idle_timeout);
1943 fs->hard_timeout = ntohs(nfs->hard_timeout);
1946 if (flow_age_extension) {
1947 if (nfs->idle_age) {
1948 fs->idle_age = ntohs(nfs->idle_age) - 1;
1950 if (nfs->hard_age) {
1951 fs->hard_age = ntohs(nfs->hard_age) - 1;
1954 fs->packet_count = ntohll(nfs->packet_count);
1955 fs->byte_count = ntohll(nfs->byte_count);
1960 fs->ofpacts = ofpacts->data;
1961 fs->ofpacts_len = ofpacts->size;
1966 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
1968 * We use this in situations where OVS internally uses UINT64_MAX to mean
1969 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
1971 unknown_to_zero(uint64_t count)
1973 return count != UINT64_MAX ? count : 0;
1976 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
1977 * those already present in the list of ofpbufs in 'replies'. 'replies' should
1978 * have been initialized with ofputil_start_stats_reply(). */
1980 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
1981 struct list *replies)
1983 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
1984 size_t start_ofs = reply->size;
1987 ofpraw_decode_partial(&raw, reply->data, reply->size);
1988 if (raw == OFPRAW_OFPST11_FLOW_REPLY) {
1989 struct ofp11_flow_stats *ofs;
1991 ofpbuf_put_uninit(reply, sizeof *ofs);
1992 oxm_put_match(reply, &fs->match);
1993 ofpacts_put_openflow11_instructions(fs->ofpacts, fs->ofpacts_len,
1996 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
1997 ofs->length = htons(reply->size - start_ofs);
1998 ofs->table_id = fs->table_id;
2000 ofs->duration_sec = htonl(fs->duration_sec);
2001 ofs->duration_nsec = htonl(fs->duration_nsec);
2002 ofs->priority = htons(fs->priority);
2003 ofs->idle_timeout = htons(fs->idle_timeout);
2004 ofs->hard_timeout = htons(fs->hard_timeout);
2005 memset(ofs->pad2, 0, sizeof ofs->pad2);
2006 ofs->cookie = fs->cookie;
2007 ofs->packet_count = htonll(unknown_to_zero(fs->packet_count));
2008 ofs->byte_count = htonll(unknown_to_zero(fs->byte_count));
2009 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
2010 struct ofp10_flow_stats *ofs;
2012 ofpbuf_put_uninit(reply, sizeof *ofs);
2013 ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply);
2015 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
2016 ofs->length = htons(reply->size - start_ofs);
2017 ofs->table_id = fs->table_id;
2019 ofputil_match_to_ofp10_match(&fs->match, &ofs->match);
2020 ofs->duration_sec = htonl(fs->duration_sec);
2021 ofs->duration_nsec = htonl(fs->duration_nsec);
2022 ofs->priority = htons(fs->priority);
2023 ofs->idle_timeout = htons(fs->idle_timeout);
2024 ofs->hard_timeout = htons(fs->hard_timeout);
2025 memset(ofs->pad2, 0, sizeof ofs->pad2);
2026 put_32aligned_be64(&ofs->cookie, fs->cookie);
2027 put_32aligned_be64(&ofs->packet_count,
2028 htonll(unknown_to_zero(fs->packet_count)));
2029 put_32aligned_be64(&ofs->byte_count,
2030 htonll(unknown_to_zero(fs->byte_count)));
2031 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
2032 struct nx_flow_stats *nfs;
2035 ofpbuf_put_uninit(reply, sizeof *nfs);
2036 match_len = nx_put_match(reply, &fs->match, 0, 0);
2037 ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply);
2039 nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
2040 nfs->length = htons(reply->size - start_ofs);
2041 nfs->table_id = fs->table_id;
2043 nfs->duration_sec = htonl(fs->duration_sec);
2044 nfs->duration_nsec = htonl(fs->duration_nsec);
2045 nfs->priority = htons(fs->priority);
2046 nfs->idle_timeout = htons(fs->idle_timeout);
2047 nfs->hard_timeout = htons(fs->hard_timeout);
2048 nfs->idle_age = htons(fs->idle_age < 0 ? 0
2049 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
2051 nfs->hard_age = htons(fs->hard_age < 0 ? 0
2052 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
2054 nfs->match_len = htons(match_len);
2055 nfs->cookie = fs->cookie;
2056 nfs->packet_count = htonll(fs->packet_count);
2057 nfs->byte_count = htonll(fs->byte_count);
2062 ofpmp_postappend(replies, start_ofs);
2065 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
2066 * NXST_AGGREGATE reply matching 'request', and returns the message. */
2068 ofputil_encode_aggregate_stats_reply(
2069 const struct ofputil_aggregate_stats *stats,
2070 const struct ofp_header *request)
2072 struct ofp_aggregate_stats_reply *asr;
2073 uint64_t packet_count;
2074 uint64_t byte_count;
2078 ofpraw_decode(&raw, request);
2079 if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) {
2080 packet_count = unknown_to_zero(stats->packet_count);
2081 byte_count = unknown_to_zero(stats->byte_count);
2083 packet_count = stats->packet_count;
2084 byte_count = stats->byte_count;
2087 msg = ofpraw_alloc_stats_reply(request, 0);
2088 asr = ofpbuf_put_zeros(msg, sizeof *asr);
2089 put_32aligned_be64(&asr->packet_count, htonll(packet_count));
2090 put_32aligned_be64(&asr->byte_count, htonll(byte_count));
2091 asr->flow_count = htonl(stats->flow_count);
2097 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
2098 const struct ofp_header *reply)
2100 struct ofp_aggregate_stats_reply *asr;
2103 ofpbuf_use_const(&msg, reply, ntohs(reply->length));
2104 ofpraw_pull_assert(&msg);
2107 stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
2108 stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
2109 stats->flow_count = ntohl(asr->flow_count);
2114 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
2115 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
2116 * an OpenFlow error code. */
2118 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
2119 const struct ofp_header *oh)
2124 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2125 raw = ofpraw_pull_assert(&b);
2126 if (raw == OFPRAW_OFPT11_FLOW_REMOVED) {
2127 const struct ofp12_flow_removed *ofr;
2130 ofr = ofpbuf_pull(&b, sizeof *ofr);
2132 error = ofputil_pull_ofp11_match(&b, &fr->match, NULL);
2137 fr->priority = ntohs(ofr->priority);
2138 fr->cookie = ofr->cookie;
2139 fr->reason = ofr->reason;
2140 fr->table_id = ofr->table_id;
2141 fr->duration_sec = ntohl(ofr->duration_sec);
2142 fr->duration_nsec = ntohl(ofr->duration_nsec);
2143 fr->idle_timeout = ntohs(ofr->idle_timeout);
2144 fr->hard_timeout = ntohs(ofr->hard_timeout);
2145 fr->packet_count = ntohll(ofr->packet_count);
2146 fr->byte_count = ntohll(ofr->byte_count);
2147 } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
2148 const struct ofp_flow_removed *ofr;
2150 ofr = ofpbuf_pull(&b, sizeof *ofr);
2152 ofputil_match_from_ofp10_match(&ofr->match, &fr->match);
2153 fr->priority = ntohs(ofr->priority);
2154 fr->cookie = ofr->cookie;
2155 fr->reason = ofr->reason;
2157 fr->duration_sec = ntohl(ofr->duration_sec);
2158 fr->duration_nsec = ntohl(ofr->duration_nsec);
2159 fr->idle_timeout = ntohs(ofr->idle_timeout);
2160 fr->hard_timeout = 0;
2161 fr->packet_count = ntohll(ofr->packet_count);
2162 fr->byte_count = ntohll(ofr->byte_count);
2163 } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
2164 struct nx_flow_removed *nfr;
2167 nfr = ofpbuf_pull(&b, sizeof *nfr);
2168 error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match,
2174 return OFPERR_OFPBRC_BAD_LEN;
2177 fr->priority = ntohs(nfr->priority);
2178 fr->cookie = nfr->cookie;
2179 fr->reason = nfr->reason;
2181 fr->duration_sec = ntohl(nfr->duration_sec);
2182 fr->duration_nsec = ntohl(nfr->duration_nsec);
2183 fr->idle_timeout = ntohs(nfr->idle_timeout);
2184 fr->hard_timeout = 0;
2185 fr->packet_count = ntohll(nfr->packet_count);
2186 fr->byte_count = ntohll(nfr->byte_count);
2194 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
2195 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
2198 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
2199 enum ofputil_protocol protocol)
2204 case OFPUTIL_P_OF12: {
2205 struct ofp12_flow_removed *ofr;
2207 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED,
2208 ofputil_protocol_to_ofp_version(protocol),
2209 htonl(0), NXM_TYPICAL_LEN);
2210 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
2211 ofr->cookie = fr->cookie;
2212 ofr->priority = htons(fr->priority);
2213 ofr->reason = fr->reason;
2214 ofr->table_id = fr->table_id;
2215 ofr->duration_sec = htonl(fr->duration_sec);
2216 ofr->duration_nsec = htonl(fr->duration_nsec);
2217 ofr->idle_timeout = htons(fr->idle_timeout);
2218 ofr->hard_timeout = htons(fr->hard_timeout);
2219 ofr->packet_count = htonll(fr->packet_count);
2220 ofr->byte_count = htonll(fr->byte_count);
2221 oxm_put_match(msg, &fr->match);
2225 case OFPUTIL_P_OF10:
2226 case OFPUTIL_P_OF10_TID: {
2227 struct ofp_flow_removed *ofr;
2229 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
2231 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
2232 ofputil_match_to_ofp10_match(&fr->match, &ofr->match);
2233 ofr->cookie = fr->cookie;
2234 ofr->priority = htons(fr->priority);
2235 ofr->reason = fr->reason;
2236 ofr->duration_sec = htonl(fr->duration_sec);
2237 ofr->duration_nsec = htonl(fr->duration_nsec);
2238 ofr->idle_timeout = htons(fr->idle_timeout);
2239 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
2240 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
2245 case OFPUTIL_P_NXM_TID: {
2246 struct nx_flow_removed *nfr;
2249 msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
2250 htonl(0), NXM_TYPICAL_LEN);
2251 nfr = ofpbuf_put_zeros(msg, sizeof *nfr);
2252 match_len = nx_put_match(msg, &fr->match, 0, 0);
2255 nfr->cookie = fr->cookie;
2256 nfr->priority = htons(fr->priority);
2257 nfr->reason = fr->reason;
2258 nfr->duration_sec = htonl(fr->duration_sec);
2259 nfr->duration_nsec = htonl(fr->duration_nsec);
2260 nfr->idle_timeout = htons(fr->idle_timeout);
2261 nfr->match_len = htons(match_len);
2262 nfr->packet_count = htonll(fr->packet_count);
2263 nfr->byte_count = htonll(fr->byte_count);
2275 ofputil_decode_packet_in_finish(struct ofputil_packet_in *pin,
2276 struct match *match, struct ofpbuf *b)
2278 pin->packet = b->data;
2279 pin->packet_len = b->size;
2281 pin->fmd.in_port = match->flow.in_port;
2282 pin->fmd.tun_id = match->flow.tunnel.tun_id;
2283 pin->fmd.metadata = match->flow.metadata;
2284 memcpy(pin->fmd.regs, match->flow.regs, sizeof pin->fmd.regs);
2288 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
2289 const struct ofp_header *oh)
2294 memset(pin, 0, sizeof *pin);
2296 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2297 raw = ofpraw_pull_assert(&b);
2298 if (raw == OFPRAW_OFPT12_PACKET_IN) {
2299 const struct ofp12_packet_in *opi;
2303 opi = ofpbuf_pull(&b, sizeof *opi);
2304 error = oxm_pull_match_loose(&b, &match);
2309 if (!ofpbuf_try_pull(&b, 2)) {
2310 return OFPERR_OFPBRC_BAD_LEN;
2313 pin->reason = opi->reason;
2314 pin->table_id = opi->table_id;
2316 pin->buffer_id = ntohl(opi->buffer_id);
2317 pin->total_len = ntohs(opi->total_len);
2319 ofputil_decode_packet_in_finish(pin, &match, &b);
2320 } else if (raw == OFPRAW_OFPT10_PACKET_IN) {
2321 const struct ofp_packet_in *opi;
2323 opi = ofpbuf_pull(&b, offsetof(struct ofp_packet_in, data));
2325 pin->packet = opi->data;
2326 pin->packet_len = b.size;
2328 pin->fmd.in_port = ntohs(opi->in_port);
2329 pin->reason = opi->reason;
2330 pin->buffer_id = ntohl(opi->buffer_id);
2331 pin->total_len = ntohs(opi->total_len);
2332 } else if (raw == OFPRAW_NXT_PACKET_IN) {
2333 const struct nx_packet_in *npi;
2337 npi = ofpbuf_pull(&b, sizeof *npi);
2338 error = nx_pull_match_loose(&b, ntohs(npi->match_len), &match, NULL,
2344 if (!ofpbuf_try_pull(&b, 2)) {
2345 return OFPERR_OFPBRC_BAD_LEN;
2348 pin->reason = npi->reason;
2349 pin->table_id = npi->table_id;
2350 pin->cookie = npi->cookie;
2352 pin->buffer_id = ntohl(npi->buffer_id);
2353 pin->total_len = ntohs(npi->total_len);
2355 ofputil_decode_packet_in_finish(pin, &match, &b);
2364 ofputil_packet_in_to_match(const struct ofputil_packet_in *pin,
2365 struct match *match)
2369 match_init_catchall(match);
2370 if (pin->fmd.tun_id != htonll(0)) {
2371 match_set_tun_id(match, pin->fmd.tun_id);
2373 if (pin->fmd.metadata != htonll(0)) {
2374 match_set_metadata(match, pin->fmd.metadata);
2377 for (i = 0; i < FLOW_N_REGS; i++) {
2378 if (pin->fmd.regs[i]) {
2379 match_set_reg(match, i, pin->fmd.regs[i]);
2383 match_set_in_port(match, pin->fmd.in_port);
2386 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
2387 * in the format specified by 'packet_in_format'. */
2389 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
2390 enum ofputil_protocol protocol,
2391 enum nx_packet_in_format packet_in_format)
2393 size_t send_len = MIN(pin->send_len, pin->packet_len);
2394 struct ofpbuf *packet;
2396 /* Add OFPT_PACKET_IN. */
2397 if (protocol == OFPUTIL_P_OF12) {
2398 struct ofp12_packet_in *opi;
2401 ofputil_packet_in_to_match(pin, &match);
2403 /* The final argument is just an estimate of the space required. */
2404 packet = ofpraw_alloc_xid(OFPRAW_OFPT12_PACKET_IN, OFP12_VERSION,
2405 htonl(0), (sizeof(struct flow_metadata) * 2
2407 ofpbuf_put_zeros(packet, sizeof *opi);
2408 oxm_put_match(packet, &match);
2409 ofpbuf_put_zeros(packet, 2);
2410 ofpbuf_put(packet, pin->packet, send_len);
2413 opi->buffer_id = htonl(pin->buffer_id);
2414 opi->total_len = htons(pin->total_len);
2415 opi->reason = pin->reason;
2416 opi->table_id = pin->table_id;
2417 } else if (packet_in_format == NXPIF_OPENFLOW10) {
2418 struct ofp_packet_in *opi;
2420 packet = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
2421 htonl(0), send_len);
2422 opi = ofpbuf_put_zeros(packet, offsetof(struct ofp_packet_in, data));
2423 opi->total_len = htons(pin->total_len);
2424 opi->in_port = htons(pin->fmd.in_port);
2425 opi->reason = pin->reason;
2426 opi->buffer_id = htonl(pin->buffer_id);
2428 ofpbuf_put(packet, pin->packet, send_len);
2429 } else if (packet_in_format == NXPIF_NXM) {
2430 struct nx_packet_in *npi;
2434 ofputil_packet_in_to_match(pin, &match);
2436 /* The final argument is just an estimate of the space required. */
2437 packet = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, OFP10_VERSION,
2438 htonl(0), (sizeof(struct flow_metadata) * 2
2440 ofpbuf_put_zeros(packet, sizeof *npi);
2441 match_len = nx_put_match(packet, &match, 0, 0);
2442 ofpbuf_put_zeros(packet, 2);
2443 ofpbuf_put(packet, pin->packet, send_len);
2446 npi->buffer_id = htonl(pin->buffer_id);
2447 npi->total_len = htons(pin->total_len);
2448 npi->reason = pin->reason;
2449 npi->table_id = pin->table_id;
2450 npi->cookie = pin->cookie;
2451 npi->match_len = htons(match_len);
2455 ofpmsg_update_length(packet);
2461 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason)
2463 static char s[INT_STRLEN(int) + 1];
2470 case OFPR_INVALID_TTL:
2471 return "invalid_ttl";
2473 case OFPR_N_REASONS:
2475 sprintf(s, "%d", (int) reason);
2481 ofputil_packet_in_reason_from_string(const char *s,
2482 enum ofp_packet_in_reason *reason)
2486 for (i = 0; i < OFPR_N_REASONS; i++) {
2487 if (!strcasecmp(s, ofputil_packet_in_reason_to_string(i))) {
2495 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
2498 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
2499 * message's actions. The caller must initialize 'ofpacts' and retains
2500 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
2502 * Returns 0 if successful, otherwise an OFPERR_* value. */
2504 ofputil_decode_packet_out(struct ofputil_packet_out *po,
2505 const struct ofp_header *oh,
2506 struct ofpbuf *ofpacts)
2511 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2512 raw = ofpraw_pull_assert(&b);
2514 if (raw == OFPRAW_OFPT11_PACKET_OUT) {
2516 const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
2518 po->buffer_id = ntohl(opo->buffer_id);
2519 error = ofputil_port_from_ofp11(opo->in_port, &po->in_port);
2524 error = ofpacts_pull_openflow11_actions(&b, ntohs(opo->actions_len),
2529 } else if (raw == OFPRAW_OFPT10_PACKET_OUT) {
2531 const struct ofp_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
2533 po->buffer_id = ntohl(opo->buffer_id);
2534 po->in_port = ntohs(opo->in_port);
2536 error = ofpacts_pull_openflow10(&b, ntohs(opo->actions_len), ofpacts);
2544 if (po->in_port >= OFPP_MAX && po->in_port != OFPP_LOCAL
2545 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
2546 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
2548 return OFPERR_OFPBRC_BAD_PORT;
2551 po->ofpacts = ofpacts->data;
2552 po->ofpacts_len = ofpacts->size;
2554 if (po->buffer_id == UINT32_MAX) {
2555 po->packet = b.data;
2556 po->packet_len = b.size;
2565 /* ofputil_phy_port */
2567 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
2568 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2569 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2570 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2571 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2572 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2573 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2574 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2576 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
2577 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
2578 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
2579 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
2580 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
2581 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
2583 static enum netdev_features
2584 netdev_port_features_from_ofp10(ovs_be32 ofp10_)
2586 uint32_t ofp10 = ntohl(ofp10_);
2587 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
2591 netdev_port_features_to_ofp10(enum netdev_features features)
2593 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
2596 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2597 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2598 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2599 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2600 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2601 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2602 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2603 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
2604 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
2605 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
2606 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
2607 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
2608 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
2609 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
2610 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
2611 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
2613 static enum netdev_features
2614 netdev_port_features_from_ofp11(ovs_be32 ofp11)
2616 return ntohl(ofp11) & 0xffff;
2620 netdev_port_features_to_ofp11(enum netdev_features features)
2622 return htonl(features & 0xffff);
2626 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
2627 const struct ofp10_phy_port *opp)
2629 memset(pp, 0, sizeof *pp);
2631 pp->port_no = ntohs(opp->port_no);
2632 memcpy(pp->hw_addr, opp->hw_addr, OFP_ETH_ALEN);
2633 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
2635 pp->config = ntohl(opp->config) & OFPPC10_ALL;
2636 pp->state = ntohl(opp->state) & OFPPS10_ALL;
2638 pp->curr = netdev_port_features_from_ofp10(opp->curr);
2639 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
2640 pp->supported = netdev_port_features_from_ofp10(opp->supported);
2641 pp->peer = netdev_port_features_from_ofp10(opp->peer);
2643 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
2644 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
2650 ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
2651 const struct ofp11_port *op)
2655 memset(pp, 0, sizeof *pp);
2657 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
2661 memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN);
2662 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
2664 pp->config = ntohl(op->config) & OFPPC11_ALL;
2665 pp->state = ntohl(op->state) & OFPPC11_ALL;
2667 pp->curr = netdev_port_features_from_ofp11(op->curr);
2668 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
2669 pp->supported = netdev_port_features_from_ofp11(op->supported);
2670 pp->peer = netdev_port_features_from_ofp11(op->peer);
2672 pp->curr_speed = ntohl(op->curr_speed);
2673 pp->max_speed = ntohl(op->max_speed);
2679 ofputil_get_phy_port_size(enum ofp_version ofp_version)
2681 switch (ofp_version) {
2683 return sizeof(struct ofp10_phy_port);
2686 return sizeof(struct ofp11_port);
2693 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
2694 struct ofp10_phy_port *opp)
2696 memset(opp, 0, sizeof *opp);
2698 opp->port_no = htons(pp->port_no);
2699 memcpy(opp->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2700 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2702 opp->config = htonl(pp->config & OFPPC10_ALL);
2703 opp->state = htonl(pp->state & OFPPS10_ALL);
2705 opp->curr = netdev_port_features_to_ofp10(pp->curr);
2706 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
2707 opp->supported = netdev_port_features_to_ofp10(pp->supported);
2708 opp->peer = netdev_port_features_to_ofp10(pp->peer);
2712 ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
2713 struct ofp11_port *op)
2715 memset(op, 0, sizeof *op);
2717 op->port_no = ofputil_port_to_ofp11(pp->port_no);
2718 memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2719 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2721 op->config = htonl(pp->config & OFPPC11_ALL);
2722 op->state = htonl(pp->state & OFPPS11_ALL);
2724 op->curr = netdev_port_features_to_ofp11(pp->curr);
2725 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
2726 op->supported = netdev_port_features_to_ofp11(pp->supported);
2727 op->peer = netdev_port_features_to_ofp11(pp->peer);
2729 op->curr_speed = htonl(pp->curr_speed);
2730 op->max_speed = htonl(pp->max_speed);
2734 ofputil_put_phy_port(enum ofp_version ofp_version,
2735 const struct ofputil_phy_port *pp, struct ofpbuf *b)
2737 switch (ofp_version) {
2738 case OFP10_VERSION: {
2739 struct ofp10_phy_port *opp;
2740 if (b->size + sizeof *opp <= UINT16_MAX) {
2741 opp = ofpbuf_put_uninit(b, sizeof *opp);
2742 ofputil_encode_ofp10_phy_port(pp, opp);
2748 case OFP12_VERSION: {
2749 struct ofp11_port *op;
2750 if (b->size + sizeof *op <= UINT16_MAX) {
2751 op = ofpbuf_put_uninit(b, sizeof *op);
2752 ofputil_encode_ofp11_port(pp, op);
2763 ofputil_append_port_desc_stats_reply(enum ofp_version ofp_version,
2764 const struct ofputil_phy_port *pp,
2765 struct list *replies)
2767 switch (ofp_version) {
2768 case OFP10_VERSION: {
2769 struct ofp10_phy_port *opp;
2771 opp = ofpmp_append(replies, sizeof *opp);
2772 ofputil_encode_ofp10_phy_port(pp, opp);
2777 case OFP12_VERSION: {
2778 struct ofp11_port *op;
2780 op = ofpmp_append(replies, sizeof *op);
2781 ofputil_encode_ofp11_port(pp, op);
2790 /* ofputil_switch_features */
2792 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
2793 OFPC_IP_REASM | OFPC_QUEUE_STATS)
2794 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
2795 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
2796 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
2797 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
2798 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
2799 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
2801 struct ofputil_action_bit_translation {
2802 enum ofputil_action_bitmap ofputil_bit;
2806 static const struct ofputil_action_bit_translation of10_action_bits[] = {
2807 { OFPUTIL_A_OUTPUT, OFPAT10_OUTPUT },
2808 { OFPUTIL_A_SET_VLAN_VID, OFPAT10_SET_VLAN_VID },
2809 { OFPUTIL_A_SET_VLAN_PCP, OFPAT10_SET_VLAN_PCP },
2810 { OFPUTIL_A_STRIP_VLAN, OFPAT10_STRIP_VLAN },
2811 { OFPUTIL_A_SET_DL_SRC, OFPAT10_SET_DL_SRC },
2812 { OFPUTIL_A_SET_DL_DST, OFPAT10_SET_DL_DST },
2813 { OFPUTIL_A_SET_NW_SRC, OFPAT10_SET_NW_SRC },
2814 { OFPUTIL_A_SET_NW_DST, OFPAT10_SET_NW_DST },
2815 { OFPUTIL_A_SET_NW_TOS, OFPAT10_SET_NW_TOS },
2816 { OFPUTIL_A_SET_TP_SRC, OFPAT10_SET_TP_SRC },
2817 { OFPUTIL_A_SET_TP_DST, OFPAT10_SET_TP_DST },
2818 { OFPUTIL_A_ENQUEUE, OFPAT10_ENQUEUE },
2822 static enum ofputil_action_bitmap
2823 decode_action_bits(ovs_be32 of_actions,
2824 const struct ofputil_action_bit_translation *x)
2826 enum ofputil_action_bitmap ofputil_actions;
2828 ofputil_actions = 0;
2829 for (; x->ofputil_bit; x++) {
2830 if (of_actions & htonl(1u << x->of_bit)) {
2831 ofputil_actions |= x->ofputil_bit;
2834 return ofputil_actions;
2838 ofputil_capabilities_mask(enum ofp_version ofp_version)
2840 /* Handle capabilities whose bit is unique for all Open Flow versions */
2841 switch (ofp_version) {
2844 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
2846 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
2848 /* Caller needs to check osf->header.version itself */
2853 /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an
2854 * abstract representation in '*features'. Initializes '*b' to iterate over
2855 * the OpenFlow port structures following 'osf' with later calls to
2856 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an
2857 * OFPERR_* value. */
2859 ofputil_decode_switch_features(const struct ofp_header *oh,
2860 struct ofputil_switch_features *features,
2863 const struct ofp_switch_features *osf;
2866 ofpbuf_use_const(b, oh, ntohs(oh->length));
2867 raw = ofpraw_pull_assert(b);
2869 osf = ofpbuf_pull(b, sizeof *osf);
2870 features->datapath_id = ntohll(osf->datapath_id);
2871 features->n_buffers = ntohl(osf->n_buffers);
2872 features->n_tables = osf->n_tables;
2874 features->capabilities = ntohl(osf->capabilities) &
2875 ofputil_capabilities_mask(oh->version);
2877 if (b->size % ofputil_get_phy_port_size(oh->version)) {
2878 return OFPERR_OFPBRC_BAD_LEN;
2881 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
2882 if (osf->capabilities & htonl(OFPC10_STP)) {
2883 features->capabilities |= OFPUTIL_C_STP;
2885 features->actions = decode_action_bits(osf->actions, of10_action_bits);
2886 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY) {
2887 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
2888 features->capabilities |= OFPUTIL_C_GROUP_STATS;
2890 features->actions = 0;
2892 return OFPERR_OFPBRC_BAD_VERSION;
2898 /* Returns true if the maximum number of ports are in 'oh'. */
2900 max_ports_in_features(const struct ofp_header *oh)
2902 size_t pp_size = ofputil_get_phy_port_size(oh->version);
2903 return ntohs(oh->length) + pp_size > UINT16_MAX;
2906 /* Given a buffer 'b' that contains a Features Reply message, checks if
2907 * it contains the maximum number of ports that will fit. If so, it
2908 * returns true and removes the ports from the message. The caller
2909 * should then send an OFPST_PORT_DESC stats request to get the ports,
2910 * since the switch may have more ports than could be represented in the
2911 * Features Reply. Otherwise, returns false.
2914 ofputil_switch_features_ports_trunc(struct ofpbuf *b)
2916 struct ofp_header *oh = b->data;
2918 if (max_ports_in_features(oh)) {
2919 /* Remove all the ports. */
2920 b->size = (sizeof(struct ofp_header)
2921 + sizeof(struct ofp_switch_features));
2922 ofpmsg_update_length(b);
2931 encode_action_bits(enum ofputil_action_bitmap ofputil_actions,
2932 const struct ofputil_action_bit_translation *x)
2934 uint32_t of_actions;
2937 for (; x->ofputil_bit; x++) {
2938 if (ofputil_actions & x->ofputil_bit) {
2939 of_actions |= 1 << x->of_bit;
2942 return htonl(of_actions);
2945 /* Returns a buffer owned by the caller that encodes 'features' in the format
2946 * required by 'protocol' with the given 'xid'. The caller should append port
2947 * information to the buffer with subsequent calls to
2948 * ofputil_put_switch_features_port(). */
2950 ofputil_encode_switch_features(const struct ofputil_switch_features *features,
2951 enum ofputil_protocol protocol, ovs_be32 xid)
2953 struct ofp_switch_features *osf;
2955 enum ofp_version version;
2958 version = ofputil_protocol_to_ofp_version(protocol);
2961 raw = OFPRAW_OFPT10_FEATURES_REPLY;
2965 raw = OFPRAW_OFPT11_FEATURES_REPLY;
2970 b = ofpraw_alloc_xid(raw, version, xid, 0);
2971 osf = ofpbuf_put_zeros(b, sizeof *osf);
2972 osf->datapath_id = htonll(features->datapath_id);
2973 osf->n_buffers = htonl(features->n_buffers);
2974 osf->n_tables = features->n_tables;
2976 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
2977 osf->capabilities = htonl(features->capabilities &
2978 ofputil_capabilities_mask(version));
2981 if (features->capabilities & OFPUTIL_C_STP) {
2982 osf->capabilities |= htonl(OFPC10_STP);
2984 osf->actions = encode_action_bits(features->actions, of10_action_bits);
2988 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
2989 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
2999 /* Encodes 'pp' into the format required by the switch_features message already
3000 * in 'b', which should have been returned by ofputil_encode_switch_features(),
3001 * and appends the encoded version to 'b'. */
3003 ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
3006 const struct ofp_header *oh = b->data;
3008 ofputil_put_phy_port(oh->version, pp, b);
3011 /* ofputil_port_status */
3013 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
3014 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
3016 ofputil_decode_port_status(const struct ofp_header *oh,
3017 struct ofputil_port_status *ps)
3019 const struct ofp_port_status *ops;
3023 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3024 ofpraw_pull_assert(&b);
3025 ops = ofpbuf_pull(&b, sizeof *ops);
3027 if (ops->reason != OFPPR_ADD &&
3028 ops->reason != OFPPR_DELETE &&
3029 ops->reason != OFPPR_MODIFY) {
3030 return OFPERR_NXBRC_BAD_REASON;
3032 ps->reason = ops->reason;
3034 retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
3035 assert(retval != EOF);
3039 /* Converts the abstract form of a "port status" message in '*ps' into an
3040 * OpenFlow message suitable for 'protocol', and returns that encoded form in
3041 * a buffer owned by the caller. */
3043 ofputil_encode_port_status(const struct ofputil_port_status *ps,
3044 enum ofputil_protocol protocol)
3046 struct ofp_port_status *ops;
3048 enum ofp_version version;
3051 version = ofputil_protocol_to_ofp_version(protocol);
3054 raw = OFPRAW_OFPT10_PORT_STATUS;
3059 raw = OFPRAW_OFPT11_PORT_STATUS;
3066 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
3067 ops = ofpbuf_put_zeros(b, sizeof *ops);
3068 ops->reason = ps->reason;
3069 ofputil_put_phy_port(version, &ps->desc, b);
3070 ofpmsg_update_length(b);
3074 /* ofputil_port_mod */
3076 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
3077 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
3079 ofputil_decode_port_mod(const struct ofp_header *oh,
3080 struct ofputil_port_mod *pm)
3085 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3086 raw = ofpraw_pull_assert(&b);
3088 if (raw == OFPRAW_OFPT10_PORT_MOD) {
3089 const struct ofp10_port_mod *opm = b.data;
3091 pm->port_no = ntohs(opm->port_no);
3092 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
3093 pm->config = ntohl(opm->config) & OFPPC10_ALL;
3094 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
3095 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
3096 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
3097 const struct ofp11_port_mod *opm = b.data;
3100 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
3105 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
3106 pm->config = ntohl(opm->config) & OFPPC11_ALL;
3107 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
3108 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
3110 return OFPERR_OFPBRC_BAD_TYPE;
3113 pm->config &= pm->mask;
3117 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
3118 * message suitable for 'protocol', and returns that encoded form in a buffer
3119 * owned by the caller. */
3121 ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
3122 enum ofputil_protocol protocol)
3124 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
3127 switch (ofp_version) {
3128 case OFP10_VERSION: {
3129 struct ofp10_port_mod *opm;
3131 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
3132 opm = ofpbuf_put_zeros(b, sizeof *opm);
3133 opm->port_no = htons(pm->port_no);
3134 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
3135 opm->config = htonl(pm->config & OFPPC10_ALL);
3136 opm->mask = htonl(pm->mask & OFPPC10_ALL);
3137 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
3142 case OFP12_VERSION: {
3143 struct ofp11_port_mod *opm;
3145 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
3146 opm = ofpbuf_put_zeros(b, sizeof *opm);
3147 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
3148 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
3149 opm->config = htonl(pm->config & OFPPC11_ALL);
3150 opm->mask = htonl(pm->mask & OFPPC11_ALL);
3151 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
3165 ofputil_put_ofp10_table_stats(const struct ofp12_table_stats *in,
3169 enum ofp_flow_wildcards wc10;
3170 enum oxm12_ofb_match_fields mf12;
3173 static const struct wc_map wc_map[] = {
3174 { OFPFW10_IN_PORT, OFPXMT12_OFB_IN_PORT },
3175 { OFPFW10_DL_VLAN, OFPXMT12_OFB_VLAN_VID },
3176 { OFPFW10_DL_SRC, OFPXMT12_OFB_ETH_SRC },
3177 { OFPFW10_DL_DST, OFPXMT12_OFB_ETH_DST},
3178 { OFPFW10_DL_TYPE, OFPXMT12_OFB_ETH_TYPE },
3179 { OFPFW10_NW_PROTO, OFPXMT12_OFB_IP_PROTO },
3180 { OFPFW10_TP_SRC, OFPXMT12_OFB_TCP_SRC },
3181 { OFPFW10_TP_DST, OFPXMT12_OFB_TCP_DST },
3182 { OFPFW10_NW_SRC_MASK, OFPXMT12_OFB_IPV4_SRC },
3183 { OFPFW10_NW_DST_MASK, OFPXMT12_OFB_IPV4_DST },
3184 { OFPFW10_DL_VLAN_PCP, OFPXMT12_OFB_VLAN_PCP },
3185 { OFPFW10_NW_TOS, OFPXMT12_OFB_IP_DSCP },
3188 struct ofp10_table_stats *out;
3189 const struct wc_map *p;
3191 out = ofpbuf_put_uninit(buf, sizeof *out);
3192 out->table_id = in->table_id;
3193 strcpy(out->name, in->name);
3195 for (p = wc_map; p < &wc_map[ARRAY_SIZE(wc_map)]; p++) {
3196 if (in->wildcards & htonll(1ULL << p->mf12)) {
3197 out->wildcards |= htonl(p->wc10);
3200 out->max_entries = in->max_entries;
3201 out->active_count = in->active_count;
3202 put_32aligned_be64(&out->lookup_count, in->lookup_count);
3203 put_32aligned_be64(&out->matched_count, in->matched_count);
3207 oxm12_to_ofp11_flow_match_fields(ovs_be64 oxm12)
3210 enum ofp11_flow_match_fields fmf11;
3211 enum oxm12_ofb_match_fields mf12;
3214 static const struct map map[] = {
3215 { OFPFMF11_IN_PORT, OFPXMT12_OFB_IN_PORT },
3216 { OFPFMF11_DL_VLAN, OFPXMT12_OFB_VLAN_VID },
3217 { OFPFMF11_DL_VLAN_PCP, OFPXMT12_OFB_VLAN_PCP },
3218 { OFPFMF11_DL_TYPE, OFPXMT12_OFB_ETH_TYPE },
3219 { OFPFMF11_NW_TOS, OFPXMT12_OFB_IP_DSCP },
3220 { OFPFMF11_NW_PROTO, OFPXMT12_OFB_IP_PROTO },
3221 { OFPFMF11_TP_SRC, OFPXMT12_OFB_TCP_SRC },
3222 { OFPFMF11_TP_DST, OFPXMT12_OFB_TCP_DST },
3223 { OFPFMF11_MPLS_LABEL, OFPXMT12_OFB_MPLS_LABEL },
3224 { OFPFMF11_MPLS_TC, OFPXMT12_OFB_MPLS_TC },
3225 /* I don't know what OFPFMF11_TYPE means. */
3226 { OFPFMF11_DL_SRC, OFPXMT12_OFB_ETH_SRC },
3227 { OFPFMF11_DL_DST, OFPXMT12_OFB_ETH_DST },
3228 { OFPFMF11_NW_SRC, OFPXMT12_OFB_IPV4_SRC },
3229 { OFPFMF11_NW_DST, OFPXMT12_OFB_IPV4_DST },
3230 { OFPFMF11_METADATA, OFPXMT12_OFB_METADATA },
3233 const struct map *p;
3237 for (p = map; p < &map[ARRAY_SIZE(map)]; p++) {
3238 if (oxm12 & htonll(1ULL << p->mf12)) {
3242 return htonl(fmf11);
3246 ofputil_put_ofp11_table_stats(const struct ofp12_table_stats *in,
3249 struct ofp11_table_stats *out;
3251 out = ofpbuf_put_uninit(buf, sizeof *out);
3252 out->table_id = in->table_id;
3253 strcpy(out->name, in->name);
3254 out->wildcards = oxm12_to_ofp11_flow_match_fields(in->wildcards);
3255 out->match = oxm12_to_ofp11_flow_match_fields(in->match);
3256 out->instructions = in->instructions;
3257 out->write_actions = in->write_actions;
3258 out->apply_actions = in->apply_actions;
3259 out->config = in->config;
3260 out->max_entries = in->max_entries;
3261 out->active_count = in->active_count;
3262 out->lookup_count = in->lookup_count;
3263 out->matched_count = in->matched_count;
3267 ofputil_encode_table_stats_reply(const struct ofp12_table_stats stats[], int n,
3268 const struct ofp_header *request)
3270 struct ofpbuf *reply;
3273 reply = ofpraw_alloc_stats_reply(request, n * sizeof *stats);
3275 switch ((enum ofp_version) request->version) {
3277 for (i = 0; i < n; i++) {
3278 ofputil_put_ofp10_table_stats(&stats[i], reply);
3283 for (i = 0; i < n; i++) {
3284 ofputil_put_ofp11_table_stats(&stats[i], reply);
3289 ofpbuf_put(reply, stats, n * sizeof *stats);
3299 /* ofputil_flow_monitor_request */
3301 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
3302 * ofputil_flow_monitor_request in 'rq'.
3304 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
3305 * message. Calling this function multiple times for a single 'msg' iterates
3306 * through the requests. The caller must initially leave 'msg''s layer
3307 * pointers null and not modify them between calls.
3309 * Returns 0 if successful, EOF if no requests were left in this 'msg',
3310 * otherwise an OFPERR_* value. */
3312 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
3315 struct nx_flow_monitor_request *nfmr;
3319 msg->l2 = msg->data;
3320 ofpraw_pull_assert(msg);
3327 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
3329 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %zu "
3330 "leftover bytes at end", msg->size);
3331 return OFPERR_OFPBRC_BAD_LEN;
3334 flags = ntohs(nfmr->flags);
3335 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
3336 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
3337 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
3338 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
3340 return OFPERR_NXBRC_FM_BAD_FLAGS;
3343 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
3344 return OFPERR_NXBRC_MUST_BE_ZERO;
3347 rq->id = ntohl(nfmr->id);
3349 rq->out_port = ntohs(nfmr->out_port);
3350 rq->table_id = nfmr->table_id;
3352 return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL, NULL);
3356 ofputil_append_flow_monitor_request(
3357 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
3359 struct nx_flow_monitor_request *nfmr;
3364 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
3367 start_ofs = msg->size;
3368 ofpbuf_put_zeros(msg, sizeof *nfmr);
3369 match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
3371 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
3372 nfmr->id = htonl(rq->id);
3373 nfmr->flags = htons(rq->flags);
3374 nfmr->out_port = htons(rq->out_port);
3375 nfmr->match_len = htons(match_len);
3376 nfmr->table_id = rq->table_id;
3379 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
3380 * into an abstract ofputil_flow_update in 'update'. The caller must have
3381 * initialized update->match to point to space allocated for a match.
3383 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
3384 * actions (except for NXFME_ABBREV, which never includes actions). The caller
3385 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
3386 * will point into the 'ofpacts' buffer.
3388 * Multiple flow updates can be packed into a single OpenFlow message. Calling
3389 * this function multiple times for a single 'msg' iterates through the
3390 * updates. The caller must initially leave 'msg''s layer pointers null and
3391 * not modify them between calls.
3393 * Returns 0 if successful, EOF if no updates were left in this 'msg',
3394 * otherwise an OFPERR_* value. */
3396 ofputil_decode_flow_update(struct ofputil_flow_update *update,
3397 struct ofpbuf *msg, struct ofpbuf *ofpacts)
3399 struct nx_flow_update_header *nfuh;
3400 unsigned int length;
3403 msg->l2 = msg->data;
3404 ofpraw_pull_assert(msg);
3411 if (msg->size < sizeof(struct nx_flow_update_header)) {
3416 update->event = ntohs(nfuh->event);
3417 length = ntohs(nfuh->length);
3418 if (length > msg->size || length % 8) {
3422 if (update->event == NXFME_ABBREV) {
3423 struct nx_flow_update_abbrev *nfua;
3425 if (length != sizeof *nfua) {
3429 nfua = ofpbuf_pull(msg, sizeof *nfua);
3430 update->xid = nfua->xid;
3432 } else if (update->event == NXFME_ADDED
3433 || update->event == NXFME_DELETED
3434 || update->event == NXFME_MODIFIED) {
3435 struct nx_flow_update_full *nfuf;
3436 unsigned int actions_len;
3437 unsigned int match_len;
3440 if (length < sizeof *nfuf) {
3444 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
3445 match_len = ntohs(nfuf->match_len);
3446 if (sizeof *nfuf + match_len > length) {
3450 update->reason = ntohs(nfuf->reason);
3451 update->idle_timeout = ntohs(nfuf->idle_timeout);
3452 update->hard_timeout = ntohs(nfuf->hard_timeout);
3453 update->table_id = nfuf->table_id;
3454 update->cookie = nfuf->cookie;
3455 update->priority = ntohs(nfuf->priority);
3457 error = nx_pull_match(msg, match_len, update->match, NULL, NULL);
3462 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
3463 error = ofpacts_pull_openflow10(msg, actions_len, ofpacts);
3468 update->ofpacts = ofpacts->data;
3469 update->ofpacts_len = ofpacts->size;
3472 VLOG_WARN_RL(&bad_ofmsg_rl,
3473 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
3474 ntohs(nfuh->event));
3475 return OFPERR_OFPET_BAD_REQUEST;
3479 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %zu "
3480 "leftover bytes at end", msg->size);
3481 return OFPERR_OFPBRC_BAD_LEN;
3485 ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
3487 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
3489 return ntohl(cancel->id);
3493 ofputil_encode_flow_monitor_cancel(uint32_t id)
3495 struct nx_flow_monitor_cancel *nfmc;
3498 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
3499 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
3500 nfmc->id = htonl(id);
3505 ofputil_start_flow_update(struct list *replies)
3509 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
3513 list_push_back(replies, &msg->list_node);
3517 ofputil_append_flow_update(const struct ofputil_flow_update *update,
3518 struct list *replies)
3520 struct nx_flow_update_header *nfuh;
3524 msg = ofpbuf_from_list(list_back(replies));
3525 start_ofs = msg->size;
3527 if (update->event == NXFME_ABBREV) {
3528 struct nx_flow_update_abbrev *nfua;
3530 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
3531 nfua->xid = update->xid;
3533 struct nx_flow_update_full *nfuf;
3536 ofpbuf_put_zeros(msg, sizeof *nfuf);
3537 match_len = nx_put_match(msg, update->match, htonll(0), htonll(0));
3538 ofpacts_put_openflow10(update->ofpacts, update->ofpacts_len, msg);
3540 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
3541 nfuf->reason = htons(update->reason);
3542 nfuf->priority = htons(update->priority);
3543 nfuf->idle_timeout = htons(update->idle_timeout);
3544 nfuf->hard_timeout = htons(update->hard_timeout);
3545 nfuf->match_len = htons(match_len);
3546 nfuf->table_id = update->table_id;
3547 nfuf->cookie = update->cookie;
3550 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
3551 nfuh->length = htons(msg->size - start_ofs);
3552 nfuh->event = htons(update->event);
3554 ofpmp_postappend(replies, start_ofs);
3558 ofputil_encode_packet_out(const struct ofputil_packet_out *po,
3559 enum ofputil_protocol protocol)
3561 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
3565 size = po->ofpacts_len;
3566 if (po->buffer_id == UINT32_MAX) {
3567 size += po->packet_len;
3570 switch (ofp_version) {
3571 case OFP10_VERSION: {
3572 struct ofp_packet_out *opo;
3575 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
3576 ofpbuf_put_zeros(msg, sizeof *opo);
3577 actions_ofs = msg->size;
3578 ofpacts_put_openflow10(po->ofpacts, po->ofpacts_len, msg);
3581 opo->buffer_id = htonl(po->buffer_id);
3582 opo->in_port = htons(po->in_port);
3583 opo->actions_len = htons(msg->size - actions_ofs);
3588 case OFP12_VERSION: {
3589 struct ofp11_packet_out *opo;
3592 msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size);
3593 ofpbuf_put_zeros(msg, sizeof *opo);
3594 len = ofpacts_put_openflow11_actions(po->ofpacts, po->ofpacts_len, msg);
3597 opo->buffer_id = htonl(po->buffer_id);
3598 opo->in_port = ofputil_port_to_ofp11(po->in_port);
3599 opo->actions_len = htons(len);
3607 if (po->buffer_id == UINT32_MAX) {
3608 ofpbuf_put(msg, po->packet, po->packet_len);
3611 ofpmsg_update_length(msg);
3616 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
3618 make_echo_request(enum ofp_version ofp_version)
3620 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
3624 /* Creates and returns an OFPT_ECHO_REPLY message matching the
3625 * OFPT_ECHO_REQUEST message in 'rq'. */
3627 make_echo_reply(const struct ofp_header *rq)
3629 struct ofpbuf rq_buf;
3630 struct ofpbuf *reply;
3632 ofpbuf_use_const(&rq_buf, rq, ntohs(rq->length));
3633 ofpraw_pull_assert(&rq_buf);
3635 reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY, rq, rq_buf.size);
3636 ofpbuf_put(reply, rq_buf.data, rq_buf.size);
3641 ofputil_encode_barrier_request(enum ofp_version ofp_version)
3645 switch (ofp_version) {
3648 type = OFPRAW_OFPT11_BARRIER_REQUEST;
3652 type = OFPRAW_OFPT10_BARRIER_REQUEST;
3659 return ofpraw_alloc(type, ofp_version, 0);
3663 ofputil_frag_handling_to_string(enum ofp_config_flags flags)
3665 switch (flags & OFPC_FRAG_MASK) {
3666 case OFPC_FRAG_NORMAL: return "normal";
3667 case OFPC_FRAG_DROP: return "drop";
3668 case OFPC_FRAG_REASM: return "reassemble";
3669 case OFPC_FRAG_NX_MATCH: return "nx-match";
3676 ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
3678 if (!strcasecmp(s, "normal")) {
3679 *flags = OFPC_FRAG_NORMAL;
3680 } else if (!strcasecmp(s, "drop")) {
3681 *flags = OFPC_FRAG_DROP;
3682 } else if (!strcasecmp(s, "reassemble")) {
3683 *flags = OFPC_FRAG_REASM;
3684 } else if (!strcasecmp(s, "nx-match")) {
3685 *flags = OFPC_FRAG_NX_MATCH;
3692 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
3693 * port number and stores the latter in '*ofp10_port', for the purpose of
3694 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
3695 * otherwise an OFPERR_* number.
3697 * See the definition of OFP11_MAX for an explanation of the mapping. */
3699 ofputil_port_from_ofp11(ovs_be32 ofp11_port, uint16_t *ofp10_port)
3701 uint32_t ofp11_port_h = ntohl(ofp11_port);
3703 if (ofp11_port_h < OFPP_MAX) {
3704 *ofp10_port = ofp11_port_h;
3706 } else if (ofp11_port_h >= OFPP11_MAX) {
3707 *ofp10_port = ofp11_port_h - OFPP11_OFFSET;
3710 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
3711 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
3712 ofp11_port_h, OFPP_MAX - 1,
3713 (uint32_t) OFPP11_MAX, UINT32_MAX);
3714 return OFPERR_OFPBAC_BAD_OUT_PORT;
3718 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
3719 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
3721 * See the definition of OFP11_MAX for an explanation of the mapping. */
3723 ofputil_port_to_ofp11(uint16_t ofp10_port)
3725 return htonl(ofp10_port < OFPP_MAX
3727 : ofp10_port + OFPP11_OFFSET);
3730 /* Checks that 'port' is a valid output port for the OFPAT10_OUTPUT action, given
3731 * that the switch will never have more than 'max_ports' ports. Returns 0 if
3732 * 'port' is valid, otherwise an OpenFlow return code. */
3734 ofputil_check_output_port(uint16_t port, int max_ports)
3742 case OFPP_CONTROLLER:
3748 if (port < max_ports) {
3751 return OFPERR_OFPBAC_BAD_OUT_PORT;
3755 #define OFPUTIL_NAMED_PORTS \
3756 OFPUTIL_NAMED_PORT(IN_PORT) \
3757 OFPUTIL_NAMED_PORT(TABLE) \
3758 OFPUTIL_NAMED_PORT(NORMAL) \
3759 OFPUTIL_NAMED_PORT(FLOOD) \
3760 OFPUTIL_NAMED_PORT(ALL) \
3761 OFPUTIL_NAMED_PORT(CONTROLLER) \
3762 OFPUTIL_NAMED_PORT(LOCAL) \
3763 OFPUTIL_NAMED_PORT(NONE)
3765 /* Stores the port number represented by 's' into '*portp'. 's' may be an
3766 * integer or, for reserved ports, the standard OpenFlow name for the port
3769 * Returns true if successful, false if 's' is not a valid OpenFlow port number
3770 * or name. The caller should issue an error message in this case, because
3771 * this function usually does not. (This gives the caller an opportunity to
3772 * look up the port name another way, e.g. by contacting the switch and listing
3773 * the names of all its ports).
3775 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
3776 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
3777 * range as described in include/openflow/openflow-1.1.h. */
3779 ofputil_port_from_string(const char *s, uint16_t *portp)
3781 unsigned int port32;
3784 if (str_to_uint(s, 10, &port32)) {
3785 if (port32 < OFPP_MAX) {
3788 } else if (port32 < OFPP_FIRST_RESV) {
3789 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
3790 "be translated to %u when talking to an OF1.1 or "
3791 "later controller", port32, port32 + OFPP11_OFFSET);
3794 } else if (port32 <= OFPP_LAST_RESV) {
3798 ofputil_format_port(port32, &s);
3799 VLOG_WARN_ONCE("referring to port %s as %u is deprecated for "
3800 "compatibility with future versions of OpenFlow",
3801 ds_cstr(&s), port32);
3806 } else if (port32 < OFPP11_MAX) {
3807 VLOG_WARN("port %u is outside the supported range 0 through "
3808 "%"PRIx16"or 0x%x through 0x%"PRIx32, port32,
3809 UINT16_MAX, (unsigned int) OFPP11_MAX, UINT32_MAX);
3812 *portp = port32 - OFPP11_OFFSET;
3820 static const struct pair pairs[] = {
3821 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
3823 #undef OFPUTIL_NAMED_PORT
3825 const struct pair *p;
3827 for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) {
3828 if (!strcasecmp(s, p->name)) {
3837 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
3838 * Most ports' string representation is just the port number, but for special
3839 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
3841 ofputil_format_port(uint16_t port, struct ds *s)
3846 #define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break;
3848 #undef OFPUTIL_NAMED_PORT
3851 ds_put_format(s, "%"PRIu16, port);
3854 ds_put_cstr(s, name);
3857 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
3858 * 'ofp_version', tries to pull the first element from the array. If
3859 * successful, initializes '*pp' with an abstract representation of the
3860 * port and returns 0. If no ports remain to be decoded, returns EOF.
3861 * On an error, returns a positive OFPERR_* value. */
3863 ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
3864 struct ofputil_phy_port *pp)
3866 switch (ofp_version) {
3867 case OFP10_VERSION: {
3868 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
3869 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
3872 case OFP12_VERSION: {
3873 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
3874 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
3881 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
3882 * 'ofp_version', returns the number of elements. */
3883 size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *b)
3885 return b->size / ofputil_get_phy_port_size(ofp_version);
3888 /* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if
3889 * 'name' is "output" then the return value is OFPUTIL_OFPAT10_OUTPUT), or -1 if
3890 * 'name' is not the name of any action.
3892 * ofp-util.def lists the mapping from names to action. */
3894 ofputil_action_code_from_name(const char *name)
3896 static const char *names[OFPUTIL_N_ACTIONS] = {
3898 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) NAME,
3899 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
3900 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
3901 #include "ofp-util.def"
3906 for (p = names; p < &names[ARRAY_SIZE(names)]; p++) {
3907 if (*p && !strcasecmp(name, *p)) {
3914 /* Appends an action of the type specified by 'code' to 'buf' and returns the
3915 * action. Initializes the parts of 'action' that identify it as having type
3916 * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that
3917 * have variable length, the length used and cleared is that of struct
3920 ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
3923 case OFPUTIL_ACTION_INVALID:
3926 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
3927 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
3928 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3929 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
3930 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3931 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
3932 #include "ofp-util.def"
3937 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
3939 ofputil_init_##ENUM(struct STRUCT *s) \
3941 memset(s, 0, sizeof *s); \
3942 s->type = htons(ENUM); \
3943 s->len = htons(sizeof *s); \
3947 ofputil_put_##ENUM(struct ofpbuf *buf) \
3949 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
3950 ofputil_init_##ENUM(s); \
3953 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3954 OFPAT10_ACTION(ENUM, STRUCT, NAME)
3955 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3957 ofputil_init_##ENUM(struct STRUCT *s) \
3959 memset(s, 0, sizeof *s); \
3960 s->type = htons(OFPAT10_VENDOR); \
3961 s->len = htons(sizeof *s); \
3962 s->vendor = htonl(NX_VENDOR_ID); \
3963 s->subtype = htons(ENUM); \
3967 ofputil_put_##ENUM(struct ofpbuf *buf) \
3969 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
3970 ofputil_init_##ENUM(s); \
3973 #include "ofp-util.def"
3976 ofputil_normalize_match__(struct match *match, bool may_log)
3979 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
3980 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
3981 MAY_NW_PROTO = 1 << 2, /* nw_proto */
3982 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
3983 MAY_ARP_SHA = 1 << 4, /* arp_sha */
3984 MAY_ARP_THA = 1 << 5, /* arp_tha */
3985 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
3986 MAY_ND_TARGET = 1 << 7 /* nd_target */
3989 struct flow_wildcards wc;
3991 /* Figure out what fields may be matched. */
3992 if (match->flow.dl_type == htons(ETH_TYPE_IP)) {
3993 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
3994 if (match->flow.nw_proto == IPPROTO_TCP ||
3995 match->flow.nw_proto == IPPROTO_UDP ||
3996 match->flow.nw_proto == IPPROTO_ICMP) {
3997 may_match |= MAY_TP_ADDR;
3999 } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
4000 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
4001 if (match->flow.nw_proto == IPPROTO_TCP ||
4002 match->flow.nw_proto == IPPROTO_UDP) {
4003 may_match |= MAY_TP_ADDR;
4004 } else if (match->flow.nw_proto == IPPROTO_ICMPV6) {
4005 may_match |= MAY_TP_ADDR;
4006 if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
4007 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
4008 } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
4009 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
4012 } else if (match->flow.dl_type == htons(ETH_TYPE_ARP) ||
4013 match->flow.dl_type == htons(ETH_TYPE_RARP)) {
4014 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
4019 /* Clear the fields that may not be matched. */
4021 if (!(may_match & MAY_NW_ADDR)) {
4022 wc.masks.nw_src = wc.masks.nw_dst = htonl(0);
4024 if (!(may_match & MAY_TP_ADDR)) {
4025 wc.masks.tp_src = wc.masks.tp_dst = htons(0);
4027 if (!(may_match & MAY_NW_PROTO)) {
4028 wc.masks.nw_proto = 0;
4030 if (!(may_match & MAY_IPVx)) {
4031 wc.masks.nw_tos = 0;
4032 wc.masks.nw_ttl = 0;
4034 if (!(may_match & MAY_ARP_SHA)) {
4035 memset(wc.masks.arp_sha, 0, ETH_ADDR_LEN);
4037 if (!(may_match & MAY_ARP_THA)) {
4038 memset(wc.masks.arp_tha, 0, ETH_ADDR_LEN);
4040 if (!(may_match & MAY_IPV6)) {
4041 wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any;
4042 wc.masks.ipv6_label = htonl(0);
4044 if (!(may_match & MAY_ND_TARGET)) {
4045 wc.masks.nd_target = in6addr_any;
4048 /* Log any changes. */
4049 if (!flow_wildcards_equal(&wc, &match->wc)) {
4050 bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
4051 char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL;
4054 match_zero_wildcarded_fields(match);
4057 char *post = match_to_string(match, OFP_DEFAULT_PRIORITY);
4058 VLOG_INFO("normalization changed ofp_match, details:");
4059 VLOG_INFO(" pre: %s", pre);
4060 VLOG_INFO("post: %s", post);
4067 /* "Normalizes" the wildcards in 'match'. That means:
4069 * 1. If the type of level N is known, then only the valid fields for that
4070 * level may be specified. For example, ARP does not have a TOS field,
4071 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
4072 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
4073 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
4076 * 2. If the type of level N is not known (or not understood by Open
4077 * vSwitch), then no fields at all for that level may be specified. For
4078 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
4079 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
4082 * If this function changes 'match', it logs a rate-limited informational
4085 ofputil_normalize_match(struct match *match)
4087 ofputil_normalize_match__(match, true);
4090 /* Same as ofputil_normalize_match() without the logging. Thus, this function
4091 * is suitable for a program's internal use, whereas ofputil_normalize_match()
4092 * sense for use on flows received from elsewhere (so that a bug in the program
4093 * that sent them can be reported and corrected). */
4095 ofputil_normalize_match_quiet(struct match *match)
4097 ofputil_normalize_match__(match, false);
4100 /* Parses a key or a key-value pair from '*stringp'.
4102 * On success: Stores the key into '*keyp'. Stores the value, if present, into
4103 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
4104 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
4105 * are substrings of '*stringp' created by replacing some of its bytes by null
4106 * terminators. Returns true.
4108 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
4109 * NULL and returns false. */
4111 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
4113 char *pos, *key, *value;
4117 pos += strspn(pos, ", \t\r\n");
4119 *keyp = *valuep = NULL;
4124 key_len = strcspn(pos, ":=(, \t\r\n");
4125 if (key[key_len] == ':' || key[key_len] == '=') {
4126 /* The value can be separated by a colon. */
4129 value = key + key_len + 1;
4130 value_len = strcspn(value, ", \t\r\n");
4131 pos = value + value_len + (value[value_len] != '\0');
4132 value[value_len] = '\0';
4133 } else if (key[key_len] == '(') {
4134 /* The value can be surrounded by balanced parentheses. The outermost
4135 * set of parentheses is removed. */
4139 value = key + key_len + 1;
4140 for (value_len = 0; level > 0; value_len++) {
4141 switch (value[value_len]) {
4155 value[value_len - 1] = '\0';
4156 pos = value + value_len;
4158 /* There might be no value at all. */
4159 value = key + key_len; /* Will become the empty string below. */
4160 pos = key + key_len + (key[key_len] != '\0');
4162 key[key_len] = '\0';
4170 /* Encode a dump ports request for 'port', the encoded message
4171 * will be fore Open Flow version 'ofp_version'. Returns message
4172 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
4174 ofputil_encode_dump_ports_request(enum ofp_version ofp_version, int16_t port)
4176 struct ofpbuf *request;
4178 switch (ofp_version) {
4179 case OFP10_VERSION: {
4180 struct ofp10_port_stats_request *req;
4181 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0);
4182 req = ofpbuf_put_zeros(request, sizeof *req);
4183 req->port_no = htons(port);
4187 case OFP12_VERSION: {
4188 struct ofp11_port_stats_request *req;
4189 request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0);
4190 req = ofpbuf_put_zeros(request, sizeof *req);
4191 req->port_no = ofputil_port_to_ofp11(port);
4202 ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops,
4203 struct ofp10_port_stats *ps10)
4205 ps10->port_no = htons(ops->port_no);
4206 memset(ps10->pad, 0, sizeof ps10->pad);
4207 put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets));
4208 put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets));
4209 put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes));
4210 put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes));
4211 put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped));
4212 put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped));
4213 put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors));
4214 put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors));
4215 put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors));
4216 put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors));
4217 put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors));
4218 put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions));
4222 ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops,
4223 struct ofp11_port_stats *ps11)
4225 ps11->port_no = ofputil_port_to_ofp11(ops->port_no);
4226 memset(ps11->pad, 0, sizeof ps11->pad);
4227 ps11->rx_packets = htonll(ops->stats.rx_packets);
4228 ps11->tx_packets = htonll(ops->stats.tx_packets);
4229 ps11->rx_bytes = htonll(ops->stats.rx_bytes);
4230 ps11->tx_bytes = htonll(ops->stats.tx_bytes);
4231 ps11->rx_dropped = htonll(ops->stats.rx_dropped);
4232 ps11->tx_dropped = htonll(ops->stats.tx_dropped);
4233 ps11->rx_errors = htonll(ops->stats.rx_errors);
4234 ps11->tx_errors = htonll(ops->stats.tx_errors);
4235 ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors);
4236 ps11->rx_over_err = htonll(ops->stats.rx_over_errors);
4237 ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors);
4238 ps11->collisions = htonll(ops->stats.collisions);
4241 /* Encode a ports stat for 'ops' and append it to 'replies'. */
4243 ofputil_append_port_stat(struct list *replies,
4244 const struct ofputil_port_stats *ops)
4246 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
4247 struct ofp_header *oh = msg->data;
4249 switch ((enum ofp_version)oh->version) {
4251 case OFP11_VERSION: {
4252 struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply);
4253 ofputil_port_stats_to_ofp11(ops, reply);
4257 case OFP10_VERSION: {
4258 struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply);
4259 ofputil_port_stats_to_ofp10(ops, reply);
4269 ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops,
4270 const struct ofp10_port_stats *ps10)
4272 memset(ops, 0, sizeof *ops);
4274 ops->port_no = ntohs(ps10->port_no);
4275 ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets));
4276 ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets));
4277 ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes));
4278 ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes));
4279 ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped));
4280 ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped));
4281 ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors));
4282 ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors));
4283 ops->stats.rx_frame_errors =
4284 ntohll(get_32aligned_be64(&ps10->rx_frame_err));
4285 ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err));
4286 ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err));
4287 ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions));
4293 ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops,
4294 const struct ofp11_port_stats *ps11)
4298 memset(ops, 0, sizeof *ops);
4299 error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no);
4304 ops->stats.rx_packets = ntohll(ps11->rx_packets);
4305 ops->stats.tx_packets = ntohll(ps11->tx_packets);
4306 ops->stats.rx_bytes = ntohll(ps11->rx_bytes);
4307 ops->stats.tx_bytes = ntohll(ps11->tx_bytes);
4308 ops->stats.rx_dropped = ntohll(ps11->rx_dropped);
4309 ops->stats.tx_dropped = ntohll(ps11->tx_dropped);
4310 ops->stats.rx_errors = ntohll(ps11->rx_errors);
4311 ops->stats.tx_errors = ntohll(ps11->tx_errors);
4312 ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err);
4313 ops->stats.rx_over_errors = ntohll(ps11->rx_over_err);
4314 ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err);
4315 ops->stats.collisions = ntohll(ps11->collisions);
4320 /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
4323 ofputil_count_port_stats(const struct ofp_header *oh)
4327 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4328 ofpraw_pull_assert(&b);
4330 BUILD_ASSERT(sizeof(struct ofp10_port_stats) ==
4331 sizeof(struct ofp11_port_stats));
4332 return b.size / sizeof(struct ofp10_port_stats);
4335 /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
4336 * ofputil_port_stats in 'ps'.
4338 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
4339 * message. Calling this function multiple times for a single 'msg' iterates
4340 * through the replies. The caller must initially leave 'msg''s layer pointers
4341 * null and not modify them between calls.
4343 * Returns 0 if successful, EOF if no replies were left in this 'msg',
4344 * otherwise a positive errno value. */
4346 ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg)
4352 ? ofpraw_decode(&raw, msg->l2)
4353 : ofpraw_pull(&raw, msg));
4360 } else if (raw == OFPRAW_OFPST11_PORT_REPLY) {
4361 const struct ofp11_port_stats *ps11;
4363 ps11 = ofpbuf_try_pull(msg, sizeof *ps11);
4365 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %zu leftover "
4366 "bytes at end", msg->size);
4367 return OFPERR_OFPBRC_BAD_LEN;
4369 return ofputil_port_stats_from_ofp11(ps, ps11);
4370 } else if (raw == OFPRAW_OFPST10_PORT_REPLY) {
4371 const struct ofp10_port_stats *ps10;
4373 ps10 = ofpbuf_try_pull(msg, sizeof *ps10);
4375 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %zu leftover "
4376 "bytes at end", msg->size);
4377 return OFPERR_OFPBRC_BAD_LEN;
4379 return ofputil_port_stats_from_ofp10(ps, ps10);
4386 /* Parse a port status request message into a 16 bit OpenFlow 1.0
4387 * port number and stores the latter in '*ofp10_port'.
4388 * Returns 0 if successful, otherwise an OFPERR_* number. */
4390 ofputil_decode_port_stats_request(const struct ofp_header *request,
4391 uint16_t *ofp10_port)
4393 switch ((enum ofp_version)request->version) {
4395 case OFP11_VERSION: {
4396 const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request);
4397 return ofputil_port_from_ofp11(psr11->port_no, ofp10_port);
4400 case OFP10_VERSION: {
4401 const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request);
4402 *ofp10_port = ntohs(psr10->port_no);
4411 /* Parse a queue status request message into 'oqsr'.
4412 * Returns 0 if successful, otherwise an OFPERR_* number. */
4414 ofputil_decode_queue_stats_request(const struct ofp_header *request,
4415 struct ofputil_queue_stats_request *oqsr)
4417 switch ((enum ofp_version)request->version) {
4419 case OFP11_VERSION: {
4420 const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request);
4421 oqsr->queue_id = ntohl(qsr11->queue_id);
4422 return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no);
4425 case OFP10_VERSION: {
4426 const struct ofp10_queue_stats_request *qsr11 = ofpmsg_body(request);
4427 oqsr->queue_id = ntohl(qsr11->queue_id);
4428 oqsr->port_no = ntohs(qsr11->port_no);
4437 /* Encode a queue statsrequest for 'oqsr', the encoded message
4438 * will be fore Open Flow version 'ofp_version'. Returns message
4439 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
4441 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
4442 const struct ofputil_queue_stats_request *oqsr)
4444 struct ofpbuf *request;
4446 switch (ofp_version) {
4448 case OFP12_VERSION: {
4449 struct ofp11_queue_stats_request *req;
4450 request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0);
4451 req = ofpbuf_put_zeros(request, sizeof *req);
4452 req->port_no = ofputil_port_to_ofp11(oqsr->port_no);
4453 req->queue_id = htonl(oqsr->queue_id);
4456 case OFP10_VERSION: {
4457 struct ofp10_queue_stats_request *req;
4458 request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0);
4459 req = ofpbuf_put_zeros(request, sizeof *req);
4460 req->port_no = htons(oqsr->port_no);
4461 req->queue_id = htonl(oqsr->queue_id);
4471 /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
4474 ofputil_count_queue_stats(const struct ofp_header *oh)
4478 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4479 ofpraw_pull_assert(&b);
4481 BUILD_ASSERT(sizeof(struct ofp10_queue_stats) ==
4482 sizeof(struct ofp11_queue_stats));
4483 return b.size / sizeof(struct ofp10_queue_stats);
4487 ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs,
4488 const struct ofp10_queue_stats *qs10)
4490 oqs->port_no = ntohs(qs10->port_no);
4491 oqs->queue_id = ntohl(qs10->queue_id);
4492 oqs->stats.tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes));
4493 oqs->stats.tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets));
4494 oqs->stats.tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors));
4500 ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs,
4501 const struct ofp11_queue_stats *qs11)
4505 error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no);
4510 oqs->queue_id = ntohl(qs11->queue_id);
4511 oqs->stats.tx_bytes = ntohll(qs11->tx_bytes);
4512 oqs->stats.tx_packets = ntohll(qs11->tx_packets);
4513 oqs->stats.tx_errors = ntohll(qs11->tx_errors);
4518 /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
4519 * ofputil_queue_stats in 'qs'.
4521 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
4522 * message. Calling this function multiple times for a single 'msg' iterates
4523 * through the replies. The caller must initially leave 'msg''s layer pointers
4524 * null and not modify them between calls.
4526 * Returns 0 if successful, EOF if no replies were left in this 'msg',
4527 * otherwise a positive errno value. */
4529 ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg)
4535 ? ofpraw_decode(&raw, msg->l2)
4536 : ofpraw_pull(&raw, msg));
4543 } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) {
4544 const struct ofp11_queue_stats *qs11;
4546 qs11 = ofpbuf_try_pull(msg, sizeof *qs11);
4548 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %zu leftover "
4549 "bytes at end", msg->size);
4550 return OFPERR_OFPBRC_BAD_LEN;
4552 return ofputil_queue_stats_from_ofp11(qs, qs11);
4553 } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) {
4554 const struct ofp10_queue_stats *qs10;
4556 qs10 = ofpbuf_try_pull(msg, sizeof *qs10);
4558 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %zu leftover "
4559 "bytes at end", msg->size);
4560 return OFPERR_OFPBRC_BAD_LEN;
4562 return ofputil_queue_stats_from_ofp10(qs, qs10);
4569 ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs,
4570 struct ofp10_queue_stats *qs10)
4572 qs10->port_no = htons(oqs->port_no);
4573 memset(qs10->pad, 0, sizeof qs10->pad);
4574 qs10->queue_id = htonl(oqs->queue_id);
4575 put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->stats.tx_bytes));
4576 put_32aligned_be64(&qs10->tx_packets, htonll(oqs->stats.tx_packets));
4577 put_32aligned_be64(&qs10->tx_errors, htonll(oqs->stats.tx_errors));
4581 ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs,
4582 struct ofp11_queue_stats *qs11)
4584 qs11->port_no = ofputil_port_to_ofp11(oqs->port_no);
4585 qs11->queue_id = htonl(oqs->queue_id);
4586 qs11->tx_bytes = htonll(oqs->stats.tx_bytes);
4587 qs11->tx_packets = htonll(oqs->stats.tx_packets);
4588 qs11->tx_errors = htonll(oqs->stats.tx_errors);
4591 /* Encode a queue stat for 'oqs' and append it to 'replies'. */
4593 ofputil_append_queue_stat(struct list *replies,
4594 const struct ofputil_queue_stats *oqs)
4596 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
4597 struct ofp_header *oh = msg->data;
4599 switch ((enum ofp_version)oh->version) {
4601 case OFP11_VERSION: {
4602 struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply);;
4603 ofputil_queue_stats_to_ofp11(oqs, reply);
4607 case OFP10_VERSION: {
4608 struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply);;
4609 ofputil_queue_stats_to_ofp10(oqs, reply);