2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofp-print.h"
21 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include <netinet/icmp6.h>
27 #include "byte-order.h"
28 #include "classifier.h"
29 #include "dynamic-string.h"
31 #include "meta-flow.h"
32 #include "multipath.h"
35 #include "ofp-actions.h"
36 #include "ofp-errors.h"
42 #include "unaligned.h"
43 #include "type-props.h"
46 VLOG_DEFINE_THIS_MODULE(ofp_util);
48 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
49 * in the peer and so there's not much point in showing a lot of them. */
50 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
52 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
53 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
56 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
57 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
58 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
59 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
62 ofputil_wcbits_to_netmask(int wcbits)
65 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
68 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
69 * that it wildcards, that is, the number of 0-bits in 'netmask', a number
70 * between 0 and 32 inclusive.
72 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
73 * still be in the valid range but isn't otherwise meaningful. */
75 ofputil_netmask_to_wcbits(ovs_be32 netmask)
77 return 32 - ip_count_cidr_bits(netmask);
80 /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
81 * flow_wildcards in 'wc' for use in struct match. It is the caller's
82 * responsibility to handle the special case where the flow match's dl_vlan is
83 * set to OFP_VLAN_NONE. */
85 ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc)
87 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 18);
89 /* Initialize most of wc. */
90 flow_wildcards_init_catchall(wc);
92 if (!(ofpfw & OFPFW10_IN_PORT)) {
93 wc->masks.in_port = UINT16_MAX;
96 if (!(ofpfw & OFPFW10_NW_TOS)) {
97 wc->masks.nw_tos |= IP_DSCP_MASK;
100 if (!(ofpfw & OFPFW10_NW_PROTO)) {
101 wc->masks.nw_proto = UINT8_MAX;
103 wc->masks.nw_src = ofputil_wcbits_to_netmask(ofpfw
104 >> OFPFW10_NW_SRC_SHIFT);
105 wc->masks.nw_dst = ofputil_wcbits_to_netmask(ofpfw
106 >> OFPFW10_NW_DST_SHIFT);
108 if (!(ofpfw & OFPFW10_TP_SRC)) {
109 wc->masks.tp_src = htons(UINT16_MAX);
111 if (!(ofpfw & OFPFW10_TP_DST)) {
112 wc->masks.tp_dst = htons(UINT16_MAX);
115 if (!(ofpfw & OFPFW10_DL_SRC)) {
116 memset(wc->masks.dl_src, 0xff, ETH_ADDR_LEN);
118 if (!(ofpfw & OFPFW10_DL_DST)) {
119 memset(wc->masks.dl_dst, 0xff, ETH_ADDR_LEN);
121 if (!(ofpfw & OFPFW10_DL_TYPE)) {
122 wc->masks.dl_type = htons(UINT16_MAX);
126 if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) {
127 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
129 if (!(ofpfw & OFPFW10_DL_VLAN)) {
130 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
134 /* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */
136 ofputil_match_from_ofp10_match(const struct ofp10_match *ofmatch,
139 uint32_t ofpfw = ntohl(ofmatch->wildcards) & OFPFW10_ALL;
141 /* Initialize match->wc. */
142 memset(&match->flow, 0, sizeof match->flow);
143 ofputil_wildcard_from_ofpfw10(ofpfw, &match->wc);
145 /* Initialize most of match->flow. */
146 match->flow.nw_src = ofmatch->nw_src;
147 match->flow.nw_dst = ofmatch->nw_dst;
148 match->flow.in_port = ntohs(ofmatch->in_port);
149 match->flow.dl_type = ofputil_dl_type_from_openflow(ofmatch->dl_type);
150 match->flow.tp_src = ofmatch->tp_src;
151 match->flow.tp_dst = ofmatch->tp_dst;
152 memcpy(match->flow.dl_src, ofmatch->dl_src, ETH_ADDR_LEN);
153 memcpy(match->flow.dl_dst, ofmatch->dl_dst, ETH_ADDR_LEN);
154 match->flow.nw_tos = ofmatch->nw_tos & IP_DSCP_MASK;
155 match->flow.nw_proto = ofmatch->nw_proto;
157 /* Translate VLANs. */
158 if (!(ofpfw & OFPFW10_DL_VLAN) &&
159 ofmatch->dl_vlan == htons(OFP10_VLAN_NONE)) {
160 /* Match only packets without 802.1Q header.
162 * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
164 * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
165 * because we can't have a specific PCP without an 802.1Q header.
166 * However, older versions of OVS treated this as matching packets
167 * withut an 802.1Q header, so we do here too. */
168 match->flow.vlan_tci = htons(0);
169 match->wc.masks.vlan_tci = htons(0xffff);
171 ovs_be16 vid, pcp, tci;
173 vid = ofmatch->dl_vlan & htons(VLAN_VID_MASK);
174 pcp = htons((ofmatch->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK);
175 tci = vid | pcp | htons(VLAN_CFI);
176 match->flow.vlan_tci = tci & match->wc.masks.vlan_tci;
180 match_zero_wildcarded_fields(match);
183 /* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */
185 ofputil_match_to_ofp10_match(const struct match *match,
186 struct ofp10_match *ofmatch)
188 const struct flow_wildcards *wc = &match->wc;
191 /* Figure out most OpenFlow wildcards. */
193 if (!wc->masks.in_port) {
194 ofpfw |= OFPFW10_IN_PORT;
196 if (!wc->masks.dl_type) {
197 ofpfw |= OFPFW10_DL_TYPE;
199 if (!wc->masks.nw_proto) {
200 ofpfw |= OFPFW10_NW_PROTO;
202 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_src)
203 << OFPFW10_NW_SRC_SHIFT);
204 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_dst)
205 << OFPFW10_NW_DST_SHIFT);
206 if (!(wc->masks.nw_tos & IP_DSCP_MASK)) {
207 ofpfw |= OFPFW10_NW_TOS;
209 if (!wc->masks.tp_src) {
210 ofpfw |= OFPFW10_TP_SRC;
212 if (!wc->masks.tp_dst) {
213 ofpfw |= OFPFW10_TP_DST;
215 if (eth_addr_is_zero(wc->masks.dl_src)) {
216 ofpfw |= OFPFW10_DL_SRC;
218 if (eth_addr_is_zero(wc->masks.dl_dst)) {
219 ofpfw |= OFPFW10_DL_DST;
222 /* Translate VLANs. */
223 ofmatch->dl_vlan = htons(0);
224 ofmatch->dl_vlan_pcp = 0;
225 if (match->wc.masks.vlan_tci == htons(0)) {
226 ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP;
227 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
228 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
229 ofmatch->dl_vlan = htons(OFP10_VLAN_NONE);
230 ofpfw |= OFPFW10_DL_VLAN_PCP;
232 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
233 ofpfw |= OFPFW10_DL_VLAN;
235 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
238 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
239 ofpfw |= OFPFW10_DL_VLAN_PCP;
241 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
245 /* Compose most of the match structure. */
246 ofmatch->wildcards = htonl(ofpfw);
247 ofmatch->in_port = htons(match->flow.in_port);
248 memcpy(ofmatch->dl_src, match->flow.dl_src, ETH_ADDR_LEN);
249 memcpy(ofmatch->dl_dst, match->flow.dl_dst, ETH_ADDR_LEN);
250 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
251 ofmatch->nw_src = match->flow.nw_src;
252 ofmatch->nw_dst = match->flow.nw_dst;
253 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
254 ofmatch->nw_proto = match->flow.nw_proto;
255 ofmatch->tp_src = match->flow.tp_src;
256 ofmatch->tp_dst = match->flow.tp_dst;
257 memset(ofmatch->pad1, '\0', sizeof ofmatch->pad1);
258 memset(ofmatch->pad2, '\0', sizeof ofmatch->pad2);
262 ofputil_pull_ofp11_match(struct ofpbuf *buf, struct match *match,
263 uint16_t *padded_match_len)
265 struct ofp11_match_header *omh = buf->data;
268 if (buf->size < sizeof *omh) {
269 return OFPERR_OFPBMC_BAD_LEN;
272 match_len = ntohs(omh->length);
274 switch (ntohs(omh->type)) {
275 case OFPMT_STANDARD: {
276 struct ofp11_match *om;
278 if (match_len != sizeof *om || buf->size < sizeof *om) {
279 return OFPERR_OFPBMC_BAD_LEN;
281 om = ofpbuf_pull(buf, sizeof *om);
282 if (padded_match_len) {
283 *padded_match_len = match_len;
285 return ofputil_match_from_ofp11_match(om, match);
289 if (padded_match_len) {
290 *padded_match_len = ROUND_UP(match_len, 8);
292 return oxm_pull_match(buf, match);
295 return OFPERR_OFPBMC_BAD_TYPE;
299 /* Converts the ofp11_match in 'match' into a struct match in 'match. Returns
300 * 0 if successful, otherwise an OFPERR_* value. */
302 ofputil_match_from_ofp11_match(const struct ofp11_match *ofmatch,
305 uint16_t wc = ntohl(ofmatch->wildcards);
306 uint8_t dl_src_mask[ETH_ADDR_LEN];
307 uint8_t dl_dst_mask[ETH_ADDR_LEN];
308 bool ipv4, arp, rarp;
311 match_init_catchall(match);
313 if (!(wc & OFPFW11_IN_PORT)) {
317 error = ofputil_port_from_ofp11(ofmatch->in_port, &ofp_port);
319 return OFPERR_OFPBMC_BAD_VALUE;
321 match_set_in_port(match, ofp_port);
324 for (i = 0; i < ETH_ADDR_LEN; i++) {
325 dl_src_mask[i] = ~ofmatch->dl_src_mask[i];
327 match_set_dl_src_masked(match, ofmatch->dl_src, dl_src_mask);
329 for (i = 0; i < ETH_ADDR_LEN; i++) {
330 dl_dst_mask[i] = ~ofmatch->dl_dst_mask[i];
332 match_set_dl_dst_masked(match, ofmatch->dl_dst, dl_dst_mask);
334 if (!(wc & OFPFW11_DL_VLAN)) {
335 if (ofmatch->dl_vlan == htons(OFPVID11_NONE)) {
336 /* Match only packets without a VLAN tag. */
337 match->flow.vlan_tci = htons(0);
338 match->wc.masks.vlan_tci = htons(UINT16_MAX);
340 if (ofmatch->dl_vlan == htons(OFPVID11_ANY)) {
341 /* Match any packet with a VLAN tag regardless of VID. */
342 match->flow.vlan_tci = htons(VLAN_CFI);
343 match->wc.masks.vlan_tci = htons(VLAN_CFI);
344 } else if (ntohs(ofmatch->dl_vlan) < 4096) {
345 /* Match only packets with the specified VLAN VID. */
346 match->flow.vlan_tci = htons(VLAN_CFI) | ofmatch->dl_vlan;
347 match->wc.masks.vlan_tci = htons(VLAN_CFI | VLAN_VID_MASK);
350 return OFPERR_OFPBMC_BAD_VALUE;
353 if (!(wc & OFPFW11_DL_VLAN_PCP)) {
354 if (ofmatch->dl_vlan_pcp <= 7) {
355 match->flow.vlan_tci |= htons(ofmatch->dl_vlan_pcp
357 match->wc.masks.vlan_tci |= htons(VLAN_PCP_MASK);
360 return OFPERR_OFPBMC_BAD_VALUE;
366 if (!(wc & OFPFW11_DL_TYPE)) {
367 match_set_dl_type(match,
368 ofputil_dl_type_from_openflow(ofmatch->dl_type));
371 ipv4 = match->flow.dl_type == htons(ETH_TYPE_IP);
372 arp = match->flow.dl_type == htons(ETH_TYPE_ARP);
373 rarp = match->flow.dl_type == htons(ETH_TYPE_RARP);
375 if (ipv4 && !(wc & OFPFW11_NW_TOS)) {
376 if (ofmatch->nw_tos & ~IP_DSCP_MASK) {
378 return OFPERR_OFPBMC_BAD_VALUE;
381 match_set_nw_dscp(match, ofmatch->nw_tos);
384 if (ipv4 || arp || rarp) {
385 if (!(wc & OFPFW11_NW_PROTO)) {
386 match_set_nw_proto(match, ofmatch->nw_proto);
388 match_set_nw_src_masked(match, ofmatch->nw_src, ~ofmatch->nw_src_mask);
389 match_set_nw_dst_masked(match, ofmatch->nw_dst, ~ofmatch->nw_dst_mask);
392 #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
393 if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) {
394 switch (match->flow.nw_proto) {
396 /* "A.2.3 Flow Match Structures" in OF1.1 says:
398 * The tp_src and tp_dst fields will be ignored unless the
399 * network protocol specified is as TCP, UDP or SCTP.
401 * but I'm pretty sure we should support ICMP too, otherwise
402 * that's a regression from OF1.0. */
403 if (!(wc & OFPFW11_TP_SRC)) {
404 uint16_t icmp_type = ntohs(ofmatch->tp_src);
405 if (icmp_type < 0x100) {
406 match_set_icmp_type(match, icmp_type);
408 return OFPERR_OFPBMC_BAD_FIELD;
411 if (!(wc & OFPFW11_TP_DST)) {
412 uint16_t icmp_code = ntohs(ofmatch->tp_dst);
413 if (icmp_code < 0x100) {
414 match_set_icmp_code(match, icmp_code);
416 return OFPERR_OFPBMC_BAD_FIELD;
423 if (!(wc & (OFPFW11_TP_SRC))) {
424 match_set_tp_src(match, ofmatch->tp_src);
426 if (!(wc & (OFPFW11_TP_DST))) {
427 match_set_tp_dst(match, ofmatch->tp_dst);
432 /* We don't support SCTP and it seems that we should tell the
433 * controller, since OF1.1 implementations are supposed to. */
434 return OFPERR_OFPBMC_BAD_FIELD;
437 /* OF1.1 says explicitly to ignore this. */
442 if (match->flow.dl_type == htons(ETH_TYPE_MPLS) ||
443 match->flow.dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
444 enum { OFPFW11_MPLS_ALL = OFPFW11_MPLS_LABEL | OFPFW11_MPLS_TC };
446 if ((wc & OFPFW11_MPLS_ALL) != OFPFW11_MPLS_ALL) {
447 /* MPLS not supported. */
448 return OFPERR_OFPBMC_BAD_TAG;
452 match_set_metadata_masked(match, ofmatch->metadata,
453 ~ofmatch->metadata_mask);
458 /* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */
460 ofputil_match_to_ofp11_match(const struct match *match,
461 struct ofp11_match *ofmatch)
466 memset(ofmatch, 0, sizeof *ofmatch);
467 ofmatch->omh.type = htons(OFPMT_STANDARD);
468 ofmatch->omh.length = htons(OFPMT11_STANDARD_LENGTH);
470 if (!match->wc.masks.in_port) {
471 wc |= OFPFW11_IN_PORT;
473 ofmatch->in_port = ofputil_port_to_ofp11(match->flow.in_port);
476 memcpy(ofmatch->dl_src, match->flow.dl_src, ETH_ADDR_LEN);
477 for (i = 0; i < ETH_ADDR_LEN; i++) {
478 ofmatch->dl_src_mask[i] = ~match->wc.masks.dl_src[i];
481 memcpy(ofmatch->dl_dst, match->flow.dl_dst, ETH_ADDR_LEN);
482 for (i = 0; i < ETH_ADDR_LEN; i++) {
483 ofmatch->dl_dst_mask[i] = ~match->wc.masks.dl_dst[i];
486 if (match->wc.masks.vlan_tci == htons(0)) {
487 wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP;
488 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
489 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
490 ofmatch->dl_vlan = htons(OFPVID11_NONE);
491 wc |= OFPFW11_DL_VLAN_PCP;
493 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
494 ofmatch->dl_vlan = htons(OFPVID11_ANY);
496 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
499 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
500 wc |= OFPFW11_DL_VLAN_PCP;
502 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
506 if (!match->wc.masks.dl_type) {
507 wc |= OFPFW11_DL_TYPE;
509 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
512 if (!(match->wc.masks.nw_tos & IP_DSCP_MASK)) {
513 wc |= OFPFW11_NW_TOS;
515 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
518 if (!match->wc.masks.nw_proto) {
519 wc |= OFPFW11_NW_PROTO;
521 ofmatch->nw_proto = match->flow.nw_proto;
524 ofmatch->nw_src = match->flow.nw_src;
525 ofmatch->nw_src_mask = ~match->wc.masks.nw_src;
526 ofmatch->nw_dst = match->flow.nw_dst;
527 ofmatch->nw_dst_mask = ~match->wc.masks.nw_dst;
529 if (!match->wc.masks.tp_src) {
530 wc |= OFPFW11_TP_SRC;
532 ofmatch->tp_src = match->flow.tp_src;
535 if (!match->wc.masks.tp_dst) {
536 wc |= OFPFW11_TP_DST;
538 ofmatch->tp_dst = match->flow.tp_dst;
541 /* MPLS not supported. */
542 wc |= OFPFW11_MPLS_LABEL;
543 wc |= OFPFW11_MPLS_TC;
545 ofmatch->metadata = match->flow.metadata;
546 ofmatch->metadata_mask = ~match->wc.masks.metadata;
548 ofmatch->wildcards = htonl(wc);
551 /* Given a 'dl_type' value in the format used in struct flow, returns the
552 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
555 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
557 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
558 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
562 /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
563 * structure, returns the corresponding 'dl_type' value for use in struct
566 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
568 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
569 ? htons(FLOW_DL_TYPE_NONE)
575 struct proto_abbrev {
576 enum ofputil_protocol protocol;
580 /* Most users really don't care about some of the differences between
581 * protocols. These abbreviations help with that. */
582 static const struct proto_abbrev proto_abbrevs[] = {
583 { OFPUTIL_P_ANY, "any" },
584 { OFPUTIL_P_OF10_ANY, "OpenFlow10" },
585 { OFPUTIL_P_NXM_ANY, "NXM" },
587 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
589 enum ofputil_protocol ofputil_flow_dump_protocols[] = {
593 size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
595 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
596 * connection that has negotiated the given 'version'. 'version' should
597 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
598 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
599 * outside the valid range. */
600 enum ofputil_protocol
601 ofputil_protocol_from_ofp_version(enum ofp_version version)
605 return OFPUTIL_P_OF10;
607 return OFPUTIL_P_OF12;
614 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
615 * OFP11_VERSION or OFP12_VERSION) that corresponds to 'protocol'. */
617 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
621 case OFPUTIL_P_OF10_TID:
623 case OFPUTIL_P_NXM_TID:
624 return OFP10_VERSION;
626 return OFP12_VERSION;
632 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
635 ofputil_protocol_is_valid(enum ofputil_protocol protocol)
637 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
640 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
641 * extension turned on or off if 'enable' is true or false, respectively.
643 * This extension is only useful for protocols whose "standard" version does
644 * not allow specific tables to be modified. In particular, this is true of
645 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
646 * specifies a table ID and so there is no need for such an extension. When
647 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
648 * extension, this function just returns its 'protocol' argument unchanged
649 * regardless of the value of 'enable'. */
650 enum ofputil_protocol
651 ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
655 case OFPUTIL_P_OF10_TID:
656 return enable ? OFPUTIL_P_OF10_TID : OFPUTIL_P_OF10;
659 case OFPUTIL_P_NXM_TID:
660 return enable ? OFPUTIL_P_NXM_TID : OFPUTIL_P_NXM;
663 return OFPUTIL_P_OF12;
670 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
671 * some extension to a standard protocol version, the return value is the
672 * standard version of that protocol without any extension. If 'protocol' is a
673 * standard protocol version, returns 'protocol' unchanged. */
674 enum ofputil_protocol
675 ofputil_protocol_to_base(enum ofputil_protocol protocol)
677 return ofputil_protocol_set_tid(protocol, false);
680 /* Returns 'new_base' with any extensions taken from 'cur'. */
681 enum ofputil_protocol
682 ofputil_protocol_set_base(enum ofputil_protocol cur,
683 enum ofputil_protocol new_base)
685 bool tid = (cur & OFPUTIL_P_TID) != 0;
689 case OFPUTIL_P_OF10_TID:
690 return ofputil_protocol_set_tid(OFPUTIL_P_OF10, tid);
693 case OFPUTIL_P_NXM_TID:
694 return ofputil_protocol_set_tid(OFPUTIL_P_NXM, tid);
697 return ofputil_protocol_set_tid(OFPUTIL_P_OF12, tid);
704 /* Returns a string form of 'protocol', if a simple form exists (that is, if
705 * 'protocol' is either a single protocol or it is a combination of protocols
706 * that have a single abbreviation). Otherwise, returns NULL. */
708 ofputil_protocol_to_string(enum ofputil_protocol protocol)
710 const struct proto_abbrev *p;
712 /* Use a "switch" statement for single-bit names so that we get a compiler
713 * warning if we forget any. */
716 return "NXM-table_id";
718 case OFPUTIL_P_NXM_TID:
719 return "NXM+table_id";
722 return "OpenFlow10-table_id";
724 case OFPUTIL_P_OF10_TID:
725 return "OpenFlow10+table_id";
731 /* Check abbreviations. */
732 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
733 if (protocol == p->protocol) {
741 /* Returns a string that represents 'protocols'. The return value might be a
742 * comma-separated list if 'protocols' doesn't have a simple name. The return
743 * value is "none" if 'protocols' is 0.
745 * The caller must free the returned string (with free()). */
747 ofputil_protocols_to_string(enum ofputil_protocol protocols)
751 assert(!(protocols & ~OFPUTIL_P_ANY));
752 if (protocols == 0) {
753 return xstrdup("none");
758 const struct proto_abbrev *p;
762 ds_put_char(&s, ',');
765 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
766 if ((protocols & p->protocol) == p->protocol) {
767 ds_put_cstr(&s, p->name);
768 protocols &= ~p->protocol;
773 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
774 enum ofputil_protocol bit = 1u << i;
776 if (protocols & bit) {
777 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
786 return ds_steal_cstr(&s);
789 static enum ofputil_protocol
790 ofputil_protocol_from_string__(const char *s, size_t n)
792 const struct proto_abbrev *p;
795 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
796 enum ofputil_protocol bit = 1u << i;
797 const char *name = ofputil_protocol_to_string(bit);
799 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
804 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
805 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
813 /* Returns the nonempty set of protocols represented by 's', which can be a
814 * single protocol name or abbreviation or a comma-separated list of them.
816 * Aborts the program with an error message if 's' is invalid. */
817 enum ofputil_protocol
818 ofputil_protocols_from_string(const char *s)
820 const char *orig_s = s;
821 enum ofputil_protocol protocols;
825 enum ofputil_protocol p;
834 p = ofputil_protocol_from_string__(s, n);
836 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
844 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
850 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
852 switch (packet_in_format) {
853 case NXPIF_OPENFLOW10:
862 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
864 switch (packet_in_format) {
865 case NXPIF_OPENFLOW10:
875 ofputil_packet_in_format_from_string(const char *s)
877 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
878 : !strcmp(s, "nxm") ? NXPIF_NXM
883 regs_fully_wildcarded(const struct flow_wildcards *wc)
887 for (i = 0; i < FLOW_N_REGS; i++) {
888 if (wc->masks.regs[i] != 0) {
896 tun_parms_fully_wildcarded(const struct flow_wildcards *wc)
898 return (!wc->masks.tunnel.ip_src &&
899 !wc->masks.tunnel.ip_dst &&
900 !wc->masks.tunnel.ip_ttl &&
901 !wc->masks.tunnel.ip_tos &&
902 !wc->masks.tunnel.flags);
905 /* Returns a bit-mask of ofputil_protocols that can be used for sending 'match'
906 * to a switch (e.g. to add or remove a flow). Only NXM can handle tunnel IDs,
907 * registers, or fixing the Ethernet multicast bit. Otherwise, it's better to
908 * use OpenFlow 1.0 protocol for backward compatibility. */
909 enum ofputil_protocol
910 ofputil_usable_protocols(const struct match *match)
912 const struct flow_wildcards *wc = &match->wc;
914 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 18);
916 /* tunnel params other than tun_id can't be sent in a flow_mod */
917 if (!tun_parms_fully_wildcarded(wc)) {
921 /* NXM and OF1.1+ supports bitwise matching on ethernet addresses. */
922 if (!eth_mask_is_exact(wc->masks.dl_src)
923 && !eth_addr_is_zero(wc->masks.dl_src)) {
924 return OFPUTIL_P_NXM_ANY;
926 if (!eth_mask_is_exact(wc->masks.dl_dst)
927 && !eth_addr_is_zero(wc->masks.dl_dst)) {
928 return OFPUTIL_P_NXM_ANY;
931 /* NXM and OF1.1+ support matching metadata. */
932 if (wc->masks.metadata != htonll(0)) {
933 return OFPUTIL_P_NXM_ANY;
936 /* Only NXM supports matching ARP hardware addresses. */
937 if (!eth_addr_is_zero(wc->masks.arp_sha) ||
938 !eth_addr_is_zero(wc->masks.arp_tha)) {
939 return OFPUTIL_P_NXM_ANY;
942 /* Only NXM supports matching IPv6 traffic. */
943 if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
944 return OFPUTIL_P_NXM_ANY;
947 /* Only NXM supports matching registers. */
948 if (!regs_fully_wildcarded(wc)) {
949 return OFPUTIL_P_NXM_ANY;
952 /* Only NXM supports matching tun_id. */
953 if (wc->masks.tunnel.tun_id != htonll(0)) {
954 return OFPUTIL_P_NXM_ANY;
957 /* Only NXM supports matching fragments. */
958 if (wc->masks.nw_frag) {
959 return OFPUTIL_P_NXM_ANY;
962 /* Only NXM supports matching IPv6 flow label. */
963 if (wc->masks.ipv6_label) {
964 return OFPUTIL_P_NXM_ANY;
967 /* Only NXM supports matching IP ECN bits. */
968 if (wc->masks.nw_tos & IP_ECN_MASK) {
969 return OFPUTIL_P_NXM_ANY;
972 /* Only NXM supports matching IP TTL/hop limit. */
973 if (wc->masks.nw_ttl) {
974 return OFPUTIL_P_NXM_ANY;
977 /* Only NXM supports non-CIDR IPv4 address masks. */
978 if (!ip_is_cidr(wc->masks.nw_src) || !ip_is_cidr(wc->masks.nw_dst)) {
979 return OFPUTIL_P_NXM_ANY;
982 /* Only NXM supports bitwise matching on transport port. */
983 if ((wc->masks.tp_src && wc->masks.tp_src != htons(UINT16_MAX)) ||
984 (wc->masks.tp_dst && wc->masks.tp_dst != htons(UINT16_MAX))) {
985 return OFPUTIL_P_NXM_ANY;
988 /* Other formats can express this rule. */
989 return OFPUTIL_P_ANY;
992 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
993 * protocol is 'current', at least partly transitions the protocol to 'want'.
994 * Stores in '*next' the protocol that will be in effect on the OpenFlow
995 * connection if the switch processes the returned message correctly. (If
996 * '*next != want' then the caller will have to iterate.)
998 * If 'current == want', returns NULL and stores 'current' in '*next'. */
1000 ofputil_encode_set_protocol(enum ofputil_protocol current,
1001 enum ofputil_protocol want,
1002 enum ofputil_protocol *next)
1004 enum ofputil_protocol cur_base, want_base;
1005 bool cur_tid, want_tid;
1007 cur_base = ofputil_protocol_to_base(current);
1008 want_base = ofputil_protocol_to_base(want);
1009 if (cur_base != want_base) {
1010 *next = ofputil_protocol_set_base(current, want_base);
1012 switch (want_base) {
1014 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1016 case OFPUTIL_P_OF10:
1017 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1019 case OFPUTIL_P_OF12:
1020 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW12);
1022 case OFPUTIL_P_OF10_TID:
1023 case OFPUTIL_P_NXM_TID:
1028 cur_tid = (current & OFPUTIL_P_TID) != 0;
1029 want_tid = (want & OFPUTIL_P_TID) != 0;
1030 if (cur_tid != want_tid) {
1031 *next = ofputil_protocol_set_tid(current, want_tid);
1032 return ofputil_make_flow_mod_table_id(want_tid);
1035 assert(current == want);
1041 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1042 * format to 'nxff'. */
1044 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1046 struct nx_set_flow_format *sff;
1049 assert(ofputil_nx_flow_format_is_valid(nxff));
1051 msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1052 sff = ofpbuf_put_zeros(msg, sizeof *sff);
1053 sff->format = htonl(nxff);
1058 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1060 enum ofputil_protocol
1061 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1063 switch (flow_format) {
1064 case NXFF_OPENFLOW10:
1065 return OFPUTIL_P_OF10;
1068 return OFPUTIL_P_NXM;
1070 case NXFF_OPENFLOW12:
1071 return OFPUTIL_P_OF12;
1078 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1080 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1082 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1085 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1088 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1090 switch (flow_format) {
1091 case NXFF_OPENFLOW10:
1092 return "openflow10";
1095 case NXFF_OPENFLOW12:
1096 return "openflow12";
1103 ofputil_make_set_packet_in_format(enum ofp_version ofp_version,
1104 enum nx_packet_in_format packet_in_format)
1106 struct nx_set_packet_in_format *spif;
1109 msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, ofp_version, 0);
1110 spif = ofpbuf_put_zeros(msg, sizeof *spif);
1111 spif->format = htonl(packet_in_format);
1116 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1117 * extension on or off (according to 'flow_mod_table_id'). */
1119 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1121 struct nx_flow_mod_table_id *nfmti;
1124 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1125 nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
1126 nfmti->set = flow_mod_table_id;
1130 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1131 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1134 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1135 * The caller must initialize 'ofpacts' and retains ownership of it.
1136 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1138 * Does not validate the flow_mod actions. The caller should do that, with
1139 * ofpacts_check(). */
1141 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1142 const struct ofp_header *oh,
1143 enum ofputil_protocol protocol,
1144 struct ofpbuf *ofpacts)
1150 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1151 raw = ofpraw_pull_assert(&b);
1152 if (raw == OFPRAW_OFPT11_FLOW_MOD) {
1153 /* Standard OpenFlow 1.1 flow_mod. */
1154 const struct ofp11_flow_mod *ofm;
1157 ofm = ofpbuf_pull(&b, sizeof *ofm);
1159 error = ofputil_pull_ofp11_match(&b, &fm->match, NULL);
1164 error = ofpacts_pull_openflow11_instructions(&b, b.size, ofpacts);
1169 /* Translate the message. */
1170 fm->priority = ntohs(ofm->priority);
1171 if (ofm->command == OFPFC_ADD) {
1172 fm->cookie = htonll(0);
1173 fm->cookie_mask = htonll(0);
1174 fm->new_cookie = ofm->cookie;
1176 fm->cookie = ofm->cookie;
1177 fm->cookie_mask = ofm->cookie_mask;
1178 fm->new_cookie = htonll(UINT64_MAX);
1180 fm->command = ofm->command;
1181 fm->table_id = ofm->table_id;
1182 fm->idle_timeout = ntohs(ofm->idle_timeout);
1183 fm->hard_timeout = ntohs(ofm->hard_timeout);
1184 fm->buffer_id = ntohl(ofm->buffer_id);
1185 error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
1189 if (ofm->out_group != htonl(OFPG_ANY)) {
1190 return OFPERR_OFPFMFC_UNKNOWN;
1192 fm->flags = ntohs(ofm->flags);
1194 if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1195 /* Standard OpenFlow 1.0 flow_mod. */
1196 const struct ofp10_flow_mod *ofm;
1199 /* Get the ofp10_flow_mod. */
1200 ofm = ofpbuf_pull(&b, sizeof *ofm);
1202 /* Translate the rule. */
1203 ofputil_match_from_ofp10_match(&ofm->match, &fm->match);
1204 ofputil_normalize_match(&fm->match);
1206 /* Now get the actions. */
1207 error = ofpacts_pull_openflow10(&b, b.size, ofpacts);
1212 /* OpenFlow 1.0 says that exact-match rules have to have the
1213 * highest possible priority. */
1214 fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL)
1215 ? ntohs(ofm->priority)
1218 /* Translate the message. */
1219 command = ntohs(ofm->command);
1220 fm->cookie = htonll(0);
1221 fm->cookie_mask = htonll(0);
1222 fm->new_cookie = ofm->cookie;
1223 fm->idle_timeout = ntohs(ofm->idle_timeout);
1224 fm->hard_timeout = ntohs(ofm->hard_timeout);
1225 fm->buffer_id = ntohl(ofm->buffer_id);
1226 fm->out_port = ntohs(ofm->out_port);
1227 fm->flags = ntohs(ofm->flags);
1228 } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1229 /* Nicira extended flow_mod. */
1230 const struct nx_flow_mod *nfm;
1233 /* Dissect the message. */
1234 nfm = ofpbuf_pull(&b, sizeof *nfm);
1235 error = nx_pull_match(&b, ntohs(nfm->match_len),
1236 &fm->match, &fm->cookie, &fm->cookie_mask);
1240 error = ofpacts_pull_openflow10(&b, b.size, ofpacts);
1245 /* Translate the message. */
1246 command = ntohs(nfm->command);
1247 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1248 /* Flow additions may only set a new cookie, not match an
1249 * existing cookie. */
1250 return OFPERR_NXBRC_NXM_INVALID;
1252 fm->priority = ntohs(nfm->priority);
1253 fm->new_cookie = nfm->cookie;
1254 fm->idle_timeout = ntohs(nfm->idle_timeout);
1255 fm->hard_timeout = ntohs(nfm->hard_timeout);
1256 fm->buffer_id = ntohl(nfm->buffer_id);
1257 fm->out_port = ntohs(nfm->out_port);
1258 fm->flags = ntohs(nfm->flags);
1263 if (protocol & OFPUTIL_P_TID) {
1264 fm->command = command & 0xff;
1265 fm->table_id = command >> 8;
1267 fm->command = command;
1268 fm->table_id = 0xff;
1272 fm->ofpacts = ofpacts->data;
1273 fm->ofpacts_len = ofpacts->size;
1279 ofputil_tid_command(const struct ofputil_flow_mod *fm,
1280 enum ofputil_protocol protocol)
1282 return htons(protocol & OFPUTIL_P_TID
1283 ? (fm->command & 0xff) | (fm->table_id << 8)
1287 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
1288 * 'protocol' and returns the message. */
1290 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
1291 enum ofputil_protocol protocol)
1296 case OFPUTIL_P_OF12: {
1297 struct ofp11_flow_mod *ofm;
1299 msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, OFP12_VERSION,
1300 NXM_TYPICAL_LEN + fm->ofpacts_len);
1301 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
1302 if (fm->command == OFPFC_ADD) {
1303 ofm->cookie = fm->new_cookie;
1305 ofm->cookie = fm->cookie;
1307 ofm->cookie_mask = fm->cookie_mask;
1308 ofm->table_id = fm->table_id;
1309 ofm->command = fm->command;
1310 ofm->idle_timeout = htons(fm->idle_timeout);
1311 ofm->hard_timeout = htons(fm->hard_timeout);
1312 ofm->priority = htons(fm->priority);
1313 ofm->buffer_id = htonl(fm->buffer_id);
1314 ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
1315 ofm->out_group = htonl(OFPG11_ANY);
1316 ofm->flags = htons(fm->flags);
1317 oxm_put_match(msg, &fm->match);
1318 ofpacts_put_openflow11_instructions(fm->ofpacts, fm->ofpacts_len, msg);
1322 case OFPUTIL_P_OF10:
1323 case OFPUTIL_P_OF10_TID: {
1324 struct ofp10_flow_mod *ofm;
1326 msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
1328 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
1329 ofputil_match_to_ofp10_match(&fm->match, &ofm->match);
1330 ofm->cookie = fm->new_cookie;
1331 ofm->command = ofputil_tid_command(fm, protocol);
1332 ofm->idle_timeout = htons(fm->idle_timeout);
1333 ofm->hard_timeout = htons(fm->hard_timeout);
1334 ofm->priority = htons(fm->priority);
1335 ofm->buffer_id = htonl(fm->buffer_id);
1336 ofm->out_port = htons(fm->out_port);
1337 ofm->flags = htons(fm->flags);
1338 ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
1343 case OFPUTIL_P_NXM_TID: {
1344 struct nx_flow_mod *nfm;
1347 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
1348 NXM_TYPICAL_LEN + fm->ofpacts_len);
1349 nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
1350 nfm->command = ofputil_tid_command(fm, protocol);
1351 nfm->cookie = fm->new_cookie;
1352 match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask);
1354 nfm->idle_timeout = htons(fm->idle_timeout);
1355 nfm->hard_timeout = htons(fm->hard_timeout);
1356 nfm->priority = htons(fm->priority);
1357 nfm->buffer_id = htonl(fm->buffer_id);
1358 nfm->out_port = htons(fm->out_port);
1359 nfm->flags = htons(fm->flags);
1360 nfm->match_len = htons(match_len);
1361 ofpacts_put_openflow10(fm->ofpacts, fm->ofpacts_len, msg);
1369 ofpmsg_update_length(msg);
1373 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1374 * send all of the 'n_fm's flow table modification requests in 'fms', and a
1375 * 0-bit for each protocol that is inadequate.
1377 * (The return value will have at least one 1-bit.) */
1378 enum ofputil_protocol
1379 ofputil_flow_mod_usable_protocols(const struct ofputil_flow_mod *fms,
1382 enum ofputil_protocol usable_protocols;
1385 usable_protocols = OFPUTIL_P_ANY;
1386 for (i = 0; i < n_fms; i++) {
1387 const struct ofputil_flow_mod *fm = &fms[i];
1389 usable_protocols &= ofputil_usable_protocols(&fm->match);
1390 if (fm->table_id != 0xff) {
1391 usable_protocols &= OFPUTIL_P_TID;
1394 /* Matching of the cookie is only supported through NXM. */
1395 if (fm->cookie_mask != htonll(0)) {
1396 usable_protocols &= OFPUTIL_P_NXM_ANY;
1400 return usable_protocols;
1404 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr,
1405 const struct ofp10_flow_stats_request *ofsr,
1408 fsr->aggregate = aggregate;
1409 ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match);
1410 fsr->out_port = ntohs(ofsr->out_port);
1411 fsr->table_id = ofsr->table_id;
1412 fsr->cookie = fsr->cookie_mask = htonll(0);
1418 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr,
1419 struct ofpbuf *b, bool aggregate)
1421 const struct ofp11_flow_stats_request *ofsr;
1424 ofsr = ofpbuf_pull(b, sizeof *ofsr);
1425 fsr->aggregate = aggregate;
1426 fsr->table_id = ofsr->table_id;
1427 error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port);
1431 if (ofsr->out_group != htonl(OFPG11_ANY)) {
1432 return OFPERR_OFPFMFC_UNKNOWN;
1434 fsr->cookie = ofsr->cookie;
1435 fsr->cookie_mask = ofsr->cookie_mask;
1436 error = ofputil_pull_ofp11_match(b, &fsr->match, NULL);
1445 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
1446 struct ofpbuf *b, bool aggregate)
1448 const struct nx_flow_stats_request *nfsr;
1451 nfsr = ofpbuf_pull(b, sizeof *nfsr);
1452 error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match,
1453 &fsr->cookie, &fsr->cookie_mask);
1458 return OFPERR_OFPBRC_BAD_LEN;
1461 fsr->aggregate = aggregate;
1462 fsr->out_port = ntohs(nfsr->out_port);
1463 fsr->table_id = nfsr->table_id;
1468 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
1469 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
1470 * successful, otherwise an OpenFlow error code. */
1472 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
1473 const struct ofp_header *oh)
1478 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1479 raw = ofpraw_pull_assert(&b);
1480 switch ((int) raw) {
1481 case OFPRAW_OFPST10_FLOW_REQUEST:
1482 return ofputil_decode_ofpst10_flow_request(fsr, b.data, false);
1484 case OFPRAW_OFPST10_AGGREGATE_REQUEST:
1485 return ofputil_decode_ofpst10_flow_request(fsr, b.data, true);
1487 case OFPRAW_OFPST11_FLOW_REQUEST:
1488 return ofputil_decode_ofpst11_flow_request(fsr, &b, false);
1490 case OFPRAW_OFPST11_AGGREGATE_REQUEST:
1491 return ofputil_decode_ofpst11_flow_request(fsr, &b, true);
1493 case OFPRAW_NXST_FLOW_REQUEST:
1494 return ofputil_decode_nxst_flow_request(fsr, &b, false);
1496 case OFPRAW_NXST_AGGREGATE_REQUEST:
1497 return ofputil_decode_nxst_flow_request(fsr, &b, true);
1500 /* Hey, the caller lied. */
1505 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
1506 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
1507 * 'protocol', and returns the message. */
1509 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
1510 enum ofputil_protocol protocol)
1516 case OFPUTIL_P_OF12: {
1517 struct ofp11_flow_stats_request *ofsr;
1519 raw = (fsr->aggregate
1520 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
1521 : OFPRAW_OFPST11_FLOW_REQUEST);
1522 msg = ofpraw_alloc(raw, OFP12_VERSION, NXM_TYPICAL_LEN);
1523 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
1524 ofsr->table_id = fsr->table_id;
1525 ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port);
1526 ofsr->out_group = htonl(OFPG11_ANY);
1527 ofsr->cookie = fsr->cookie;
1528 ofsr->cookie_mask = fsr->cookie_mask;
1529 oxm_put_match(msg, &fsr->match);
1533 case OFPUTIL_P_OF10:
1534 case OFPUTIL_P_OF10_TID: {
1535 struct ofp10_flow_stats_request *ofsr;
1537 raw = (fsr->aggregate
1538 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
1539 : OFPRAW_OFPST10_FLOW_REQUEST);
1540 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
1541 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
1542 ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match);
1543 ofsr->table_id = fsr->table_id;
1544 ofsr->out_port = htons(fsr->out_port);
1549 case OFPUTIL_P_NXM_TID: {
1550 struct nx_flow_stats_request *nfsr;
1553 raw = (fsr->aggregate
1554 ? OFPRAW_NXST_AGGREGATE_REQUEST
1555 : OFPRAW_NXST_FLOW_REQUEST);
1556 msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN);
1557 ofpbuf_put_zeros(msg, sizeof *nfsr);
1558 match_len = nx_put_match(msg, &fsr->match,
1559 fsr->cookie, fsr->cookie_mask);
1562 nfsr->out_port = htons(fsr->out_port);
1563 nfsr->match_len = htons(match_len);
1564 nfsr->table_id = fsr->table_id;
1575 /* Returns a bitmask with a 1-bit for each protocol that could be used to
1576 * accurately encode 'fsr', and a 0-bit for each protocol that is inadequate.
1578 * (The return value will have at least one 1-bit.) */
1579 enum ofputil_protocol
1580 ofputil_flow_stats_request_usable_protocols(
1581 const struct ofputil_flow_stats_request *fsr)
1583 enum ofputil_protocol usable_protocols;
1585 usable_protocols = ofputil_usable_protocols(&fsr->match);
1586 if (fsr->cookie_mask != htonll(0)) {
1587 usable_protocols &= OFPUTIL_P_NXM_ANY;
1589 return usable_protocols;
1592 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
1593 * ofputil_flow_stats in 'fs'.
1595 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
1596 * OpenFlow message. Calling this function multiple times for a single 'msg'
1597 * iterates through the replies. The caller must initially leave 'msg''s layer
1598 * pointers null and not modify them between calls.
1600 * Most switches don't send the values needed to populate fs->idle_age and
1601 * fs->hard_age, so those members will usually be set to 0. If the switch from
1602 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
1603 * 'flow_age_extension' as true so that the contents of 'msg' determine the
1604 * 'idle_age' and 'hard_age' members in 'fs'.
1606 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
1607 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
1608 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
1610 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1611 * otherwise a positive errno value. */
1613 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
1615 bool flow_age_extension,
1616 struct ofpbuf *ofpacts)
1622 ? ofpraw_decode(&raw, msg->l2)
1623 : ofpraw_pull(&raw, msg));
1630 } else if (raw == OFPRAW_OFPST11_FLOW_REPLY) {
1631 const struct ofp11_flow_stats *ofs;
1633 uint16_t padded_match_len;
1635 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1637 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1638 "bytes at end", msg->size);
1642 length = ntohs(ofs->length);
1643 if (length < sizeof *ofs) {
1644 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1645 "length %zu", length);
1649 if (ofputil_pull_ofp11_match(msg, &fs->match, &padded_match_len)) {
1650 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match");
1654 if (ofpacts_pull_openflow11_instructions(msg, length - sizeof *ofs -
1655 padded_match_len, ofpacts)) {
1656 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions");
1660 fs->priority = ntohs(ofs->priority);
1661 fs->table_id = ofs->table_id;
1662 fs->duration_sec = ntohl(ofs->duration_sec);
1663 fs->duration_nsec = ntohl(ofs->duration_nsec);
1664 fs->idle_timeout = ntohs(ofs->idle_timeout);
1665 fs->hard_timeout = ntohs(ofs->hard_timeout);
1668 fs->cookie = ofs->cookie;
1669 fs->packet_count = ntohll(ofs->packet_count);
1670 fs->byte_count = ntohll(ofs->byte_count);
1671 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
1672 const struct ofp10_flow_stats *ofs;
1675 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
1677 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %zu leftover "
1678 "bytes at end", msg->size);
1682 length = ntohs(ofs->length);
1683 if (length < sizeof *ofs) {
1684 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
1685 "length %zu", length);
1689 if (ofpacts_pull_openflow10(msg, length - sizeof *ofs, ofpacts)) {
1693 fs->cookie = get_32aligned_be64(&ofs->cookie);
1694 ofputil_match_from_ofp10_match(&ofs->match, &fs->match);
1695 fs->priority = ntohs(ofs->priority);
1696 fs->table_id = ofs->table_id;
1697 fs->duration_sec = ntohl(ofs->duration_sec);
1698 fs->duration_nsec = ntohl(ofs->duration_nsec);
1699 fs->idle_timeout = ntohs(ofs->idle_timeout);
1700 fs->hard_timeout = ntohs(ofs->hard_timeout);
1703 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
1704 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
1705 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
1706 const struct nx_flow_stats *nfs;
1707 size_t match_len, actions_len, length;
1709 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
1711 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %zu leftover "
1712 "bytes at end", msg->size);
1716 length = ntohs(nfs->length);
1717 match_len = ntohs(nfs->match_len);
1718 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
1719 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%zu "
1720 "claims invalid length %zu", match_len, length);
1723 if (nx_pull_match(msg, match_len, &fs->match, NULL, NULL)) {
1727 actions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
1728 if (ofpacts_pull_openflow10(msg, actions_len, ofpacts)) {
1732 fs->cookie = nfs->cookie;
1733 fs->table_id = nfs->table_id;
1734 fs->duration_sec = ntohl(nfs->duration_sec);
1735 fs->duration_nsec = ntohl(nfs->duration_nsec);
1736 fs->priority = ntohs(nfs->priority);
1737 fs->idle_timeout = ntohs(nfs->idle_timeout);
1738 fs->hard_timeout = ntohs(nfs->hard_timeout);
1741 if (flow_age_extension) {
1742 if (nfs->idle_age) {
1743 fs->idle_age = ntohs(nfs->idle_age) - 1;
1745 if (nfs->hard_age) {
1746 fs->hard_age = ntohs(nfs->hard_age) - 1;
1749 fs->packet_count = ntohll(nfs->packet_count);
1750 fs->byte_count = ntohll(nfs->byte_count);
1755 fs->ofpacts = ofpacts->data;
1756 fs->ofpacts_len = ofpacts->size;
1761 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
1763 * We use this in situations where OVS internally uses UINT64_MAX to mean
1764 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
1766 unknown_to_zero(uint64_t count)
1768 return count != UINT64_MAX ? count : 0;
1771 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
1772 * those already present in the list of ofpbufs in 'replies'. 'replies' should
1773 * have been initialized with ofputil_start_stats_reply(). */
1775 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
1776 struct list *replies)
1778 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
1779 size_t start_ofs = reply->size;
1782 ofpraw_decode_partial(&raw, reply->data, reply->size);
1783 if (raw == OFPRAW_OFPST11_FLOW_REPLY) {
1784 struct ofp11_flow_stats *ofs;
1786 ofpbuf_put_uninit(reply, sizeof *ofs);
1787 oxm_put_match(reply, &fs->match);
1788 ofpacts_put_openflow11_instructions(fs->ofpacts, fs->ofpacts_len,
1791 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
1792 ofs->length = htons(reply->size - start_ofs);
1793 ofs->table_id = fs->table_id;
1795 ofs->duration_sec = htonl(fs->duration_sec);
1796 ofs->duration_nsec = htonl(fs->duration_nsec);
1797 ofs->priority = htons(fs->priority);
1798 ofs->idle_timeout = htons(fs->idle_timeout);
1799 ofs->hard_timeout = htons(fs->hard_timeout);
1800 memset(ofs->pad2, 0, sizeof ofs->pad2);
1801 ofs->cookie = fs->cookie;
1802 ofs->packet_count = htonll(unknown_to_zero(fs->packet_count));
1803 ofs->byte_count = htonll(unknown_to_zero(fs->byte_count));
1804 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
1805 struct ofp10_flow_stats *ofs;
1807 ofpbuf_put_uninit(reply, sizeof *ofs);
1808 ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply);
1810 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
1811 ofs->length = htons(reply->size - start_ofs);
1812 ofs->table_id = fs->table_id;
1814 ofputil_match_to_ofp10_match(&fs->match, &ofs->match);
1815 ofs->duration_sec = htonl(fs->duration_sec);
1816 ofs->duration_nsec = htonl(fs->duration_nsec);
1817 ofs->priority = htons(fs->priority);
1818 ofs->idle_timeout = htons(fs->idle_timeout);
1819 ofs->hard_timeout = htons(fs->hard_timeout);
1820 memset(ofs->pad2, 0, sizeof ofs->pad2);
1821 put_32aligned_be64(&ofs->cookie, fs->cookie);
1822 put_32aligned_be64(&ofs->packet_count,
1823 htonll(unknown_to_zero(fs->packet_count)));
1824 put_32aligned_be64(&ofs->byte_count,
1825 htonll(unknown_to_zero(fs->byte_count)));
1826 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
1827 struct nx_flow_stats *nfs;
1830 ofpbuf_put_uninit(reply, sizeof *nfs);
1831 match_len = nx_put_match(reply, &fs->match, 0, 0);
1832 ofpacts_put_openflow10(fs->ofpacts, fs->ofpacts_len, reply);
1834 nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
1835 nfs->length = htons(reply->size - start_ofs);
1836 nfs->table_id = fs->table_id;
1838 nfs->duration_sec = htonl(fs->duration_sec);
1839 nfs->duration_nsec = htonl(fs->duration_nsec);
1840 nfs->priority = htons(fs->priority);
1841 nfs->idle_timeout = htons(fs->idle_timeout);
1842 nfs->hard_timeout = htons(fs->hard_timeout);
1843 nfs->idle_age = htons(fs->idle_age < 0 ? 0
1844 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
1846 nfs->hard_age = htons(fs->hard_age < 0 ? 0
1847 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
1849 nfs->match_len = htons(match_len);
1850 nfs->cookie = fs->cookie;
1851 nfs->packet_count = htonll(fs->packet_count);
1852 nfs->byte_count = htonll(fs->byte_count);
1857 ofpmp_postappend(replies, start_ofs);
1860 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
1861 * NXST_AGGREGATE reply matching 'request', and returns the message. */
1863 ofputil_encode_aggregate_stats_reply(
1864 const struct ofputil_aggregate_stats *stats,
1865 const struct ofp_header *request)
1867 struct ofp_aggregate_stats_reply *asr;
1868 uint64_t packet_count;
1869 uint64_t byte_count;
1873 ofpraw_decode(&raw, request);
1874 if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) {
1875 packet_count = unknown_to_zero(stats->packet_count);
1876 byte_count = unknown_to_zero(stats->byte_count);
1878 packet_count = stats->packet_count;
1879 byte_count = stats->byte_count;
1882 msg = ofpraw_alloc_stats_reply(request, 0);
1883 asr = ofpbuf_put_zeros(msg, sizeof *asr);
1884 put_32aligned_be64(&asr->packet_count, htonll(packet_count));
1885 put_32aligned_be64(&asr->byte_count, htonll(byte_count));
1886 asr->flow_count = htonl(stats->flow_count);
1892 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
1893 const struct ofp_header *reply)
1895 struct ofp_aggregate_stats_reply *asr;
1898 ofpbuf_use_const(&msg, reply, ntohs(reply->length));
1899 ofpraw_pull_assert(&msg);
1902 stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
1903 stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
1904 stats->flow_count = ntohl(asr->flow_count);
1909 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
1910 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
1911 * an OpenFlow error code. */
1913 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
1914 const struct ofp_header *oh)
1919 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1920 raw = ofpraw_pull_assert(&b);
1921 if (raw == OFPRAW_OFPT11_FLOW_REMOVED) {
1922 const struct ofp12_flow_removed *ofr;
1925 ofr = ofpbuf_pull(&b, sizeof *ofr);
1927 error = ofputil_pull_ofp11_match(&b, &fr->match, NULL);
1932 fr->priority = ntohs(ofr->priority);
1933 fr->cookie = ofr->cookie;
1934 fr->reason = ofr->reason;
1935 fr->table_id = ofr->table_id;
1936 fr->duration_sec = ntohl(ofr->duration_sec);
1937 fr->duration_nsec = ntohl(ofr->duration_nsec);
1938 fr->idle_timeout = ntohs(ofr->idle_timeout);
1939 fr->hard_timeout = ntohs(ofr->hard_timeout);
1940 fr->packet_count = ntohll(ofr->packet_count);
1941 fr->byte_count = ntohll(ofr->byte_count);
1942 } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
1943 const struct ofp_flow_removed *ofr;
1945 ofr = ofpbuf_pull(&b, sizeof *ofr);
1947 ofputil_match_from_ofp10_match(&ofr->match, &fr->match);
1948 fr->priority = ntohs(ofr->priority);
1949 fr->cookie = ofr->cookie;
1950 fr->reason = ofr->reason;
1952 fr->duration_sec = ntohl(ofr->duration_sec);
1953 fr->duration_nsec = ntohl(ofr->duration_nsec);
1954 fr->idle_timeout = ntohs(ofr->idle_timeout);
1955 fr->hard_timeout = 0;
1956 fr->packet_count = ntohll(ofr->packet_count);
1957 fr->byte_count = ntohll(ofr->byte_count);
1958 } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
1959 struct nx_flow_removed *nfr;
1962 nfr = ofpbuf_pull(&b, sizeof *nfr);
1963 error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match,
1969 return OFPERR_OFPBRC_BAD_LEN;
1972 fr->priority = ntohs(nfr->priority);
1973 fr->cookie = nfr->cookie;
1974 fr->reason = nfr->reason;
1976 fr->duration_sec = ntohl(nfr->duration_sec);
1977 fr->duration_nsec = ntohl(nfr->duration_nsec);
1978 fr->idle_timeout = ntohs(nfr->idle_timeout);
1979 fr->hard_timeout = 0;
1980 fr->packet_count = ntohll(nfr->packet_count);
1981 fr->byte_count = ntohll(nfr->byte_count);
1989 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
1990 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
1993 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
1994 enum ofputil_protocol protocol)
1999 case OFPUTIL_P_OF12: {
2000 struct ofp12_flow_removed *ofr;
2002 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED,
2003 ofputil_protocol_to_ofp_version(protocol),
2004 htonl(0), NXM_TYPICAL_LEN);
2005 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
2006 ofr->cookie = fr->cookie;
2007 ofr->priority = htons(fr->priority);
2008 ofr->reason = fr->reason;
2009 ofr->table_id = fr->table_id;
2010 ofr->duration_sec = htonl(fr->duration_sec);
2011 ofr->duration_nsec = htonl(fr->duration_nsec);
2012 ofr->idle_timeout = htons(fr->idle_timeout);
2013 ofr->hard_timeout = htons(fr->hard_timeout);
2014 ofr->packet_count = htonll(fr->packet_count);
2015 ofr->byte_count = htonll(fr->byte_count);
2016 oxm_put_match(msg, &fr->match);
2020 case OFPUTIL_P_OF10:
2021 case OFPUTIL_P_OF10_TID: {
2022 struct ofp_flow_removed *ofr;
2024 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
2026 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
2027 ofputil_match_to_ofp10_match(&fr->match, &ofr->match);
2028 ofr->cookie = fr->cookie;
2029 ofr->priority = htons(fr->priority);
2030 ofr->reason = fr->reason;
2031 ofr->duration_sec = htonl(fr->duration_sec);
2032 ofr->duration_nsec = htonl(fr->duration_nsec);
2033 ofr->idle_timeout = htons(fr->idle_timeout);
2034 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
2035 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
2040 case OFPUTIL_P_NXM_TID: {
2041 struct nx_flow_removed *nfr;
2044 msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
2045 htonl(0), NXM_TYPICAL_LEN);
2046 nfr = ofpbuf_put_zeros(msg, sizeof *nfr);
2047 match_len = nx_put_match(msg, &fr->match, 0, 0);
2050 nfr->cookie = fr->cookie;
2051 nfr->priority = htons(fr->priority);
2052 nfr->reason = fr->reason;
2053 nfr->duration_sec = htonl(fr->duration_sec);
2054 nfr->duration_nsec = htonl(fr->duration_nsec);
2055 nfr->idle_timeout = htons(fr->idle_timeout);
2056 nfr->match_len = htons(match_len);
2057 nfr->packet_count = htonll(fr->packet_count);
2058 nfr->byte_count = htonll(fr->byte_count);
2070 ofputil_decode_packet_in_finish(struct ofputil_packet_in *pin,
2071 struct match *match, struct ofpbuf *b)
2073 pin->packet = b->data;
2074 pin->packet_len = b->size;
2076 pin->fmd.in_port = match->flow.in_port;
2077 pin->fmd.tun_id = match->flow.tunnel.tun_id;
2078 pin->fmd.metadata = match->flow.metadata;
2079 memcpy(pin->fmd.regs, match->flow.regs, sizeof pin->fmd.regs);
2083 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
2084 const struct ofp_header *oh)
2089 memset(pin, 0, sizeof *pin);
2091 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2092 raw = ofpraw_pull_assert(&b);
2093 if (raw == OFPRAW_OFPT12_PACKET_IN) {
2094 const struct ofp12_packet_in *opi;
2098 opi = ofpbuf_pull(&b, sizeof *opi);
2099 error = oxm_pull_match_loose(&b, &match);
2104 if (!ofpbuf_try_pull(&b, 2)) {
2105 return OFPERR_OFPBRC_BAD_LEN;
2108 pin->reason = opi->reason;
2109 pin->table_id = opi->table_id;
2111 pin->buffer_id = ntohl(opi->buffer_id);
2112 pin->total_len = ntohs(opi->total_len);
2114 ofputil_decode_packet_in_finish(pin, &match, &b);
2115 } else if (raw == OFPRAW_OFPT10_PACKET_IN) {
2116 const struct ofp_packet_in *opi;
2118 opi = ofpbuf_pull(&b, offsetof(struct ofp_packet_in, data));
2120 pin->packet = opi->data;
2121 pin->packet_len = b.size;
2123 pin->fmd.in_port = ntohs(opi->in_port);
2124 pin->reason = opi->reason;
2125 pin->buffer_id = ntohl(opi->buffer_id);
2126 pin->total_len = ntohs(opi->total_len);
2127 } else if (raw == OFPRAW_NXT_PACKET_IN) {
2128 const struct nx_packet_in *npi;
2132 npi = ofpbuf_pull(&b, sizeof *npi);
2133 error = nx_pull_match_loose(&b, ntohs(npi->match_len), &match, NULL,
2139 if (!ofpbuf_try_pull(&b, 2)) {
2140 return OFPERR_OFPBRC_BAD_LEN;
2143 pin->reason = npi->reason;
2144 pin->table_id = npi->table_id;
2145 pin->cookie = npi->cookie;
2147 pin->buffer_id = ntohl(npi->buffer_id);
2148 pin->total_len = ntohs(npi->total_len);
2150 ofputil_decode_packet_in_finish(pin, &match, &b);
2159 ofputil_packet_in_to_match(const struct ofputil_packet_in *pin,
2160 struct match *match)
2164 match_init_catchall(match);
2165 if (pin->fmd.tun_id != htonll(0)) {
2166 match_set_tun_id(match, pin->fmd.tun_id);
2168 if (pin->fmd.metadata != htonll(0)) {
2169 match_set_metadata(match, pin->fmd.metadata);
2172 for (i = 0; i < FLOW_N_REGS; i++) {
2173 if (pin->fmd.regs[i]) {
2174 match_set_reg(match, i, pin->fmd.regs[i]);
2178 match_set_in_port(match, pin->fmd.in_port);
2181 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
2182 * in the format specified by 'packet_in_format'. */
2184 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
2185 enum ofputil_protocol protocol,
2186 enum nx_packet_in_format packet_in_format)
2188 size_t send_len = MIN(pin->send_len, pin->packet_len);
2189 struct ofpbuf *packet;
2191 /* Add OFPT_PACKET_IN. */
2192 if (protocol == OFPUTIL_P_OF12) {
2193 struct ofp12_packet_in *opi;
2196 ofputil_packet_in_to_match(pin, &match);
2198 /* The final argument is just an estimate of the space required. */
2199 packet = ofpraw_alloc_xid(OFPRAW_OFPT12_PACKET_IN, OFP12_VERSION,
2200 htonl(0), (sizeof(struct flow_metadata) * 2
2202 ofpbuf_put_zeros(packet, sizeof *opi);
2203 oxm_put_match(packet, &match);
2204 ofpbuf_put_zeros(packet, 2);
2205 ofpbuf_put(packet, pin->packet, send_len);
2208 opi->buffer_id = htonl(pin->buffer_id);
2209 opi->total_len = htons(pin->total_len);
2210 opi->reason = pin->reason;
2211 opi->table_id = pin->table_id;
2212 } else if (packet_in_format == NXPIF_OPENFLOW10) {
2213 struct ofp_packet_in *opi;
2215 packet = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
2216 htonl(0), send_len);
2217 opi = ofpbuf_put_zeros(packet, offsetof(struct ofp_packet_in, data));
2218 opi->total_len = htons(pin->total_len);
2219 opi->in_port = htons(pin->fmd.in_port);
2220 opi->reason = pin->reason;
2221 opi->buffer_id = htonl(pin->buffer_id);
2223 ofpbuf_put(packet, pin->packet, send_len);
2224 } else if (packet_in_format == NXPIF_NXM) {
2225 struct nx_packet_in *npi;
2229 ofputil_packet_in_to_match(pin, &match);
2231 /* The final argument is just an estimate of the space required. */
2232 packet = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, OFP10_VERSION,
2233 htonl(0), (sizeof(struct flow_metadata) * 2
2235 ofpbuf_put_zeros(packet, sizeof *npi);
2236 match_len = nx_put_match(packet, &match, 0, 0);
2237 ofpbuf_put_zeros(packet, 2);
2238 ofpbuf_put(packet, pin->packet, send_len);
2241 npi->buffer_id = htonl(pin->buffer_id);
2242 npi->total_len = htons(pin->total_len);
2243 npi->reason = pin->reason;
2244 npi->table_id = pin->table_id;
2245 npi->cookie = pin->cookie;
2246 npi->match_len = htons(match_len);
2250 ofpmsg_update_length(packet);
2256 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason)
2258 static char s[INT_STRLEN(int) + 1];
2265 case OFPR_INVALID_TTL:
2266 return "invalid_ttl";
2268 case OFPR_N_REASONS:
2270 sprintf(s, "%d", (int) reason);
2276 ofputil_packet_in_reason_from_string(const char *s,
2277 enum ofp_packet_in_reason *reason)
2281 for (i = 0; i < OFPR_N_REASONS; i++) {
2282 if (!strcasecmp(s, ofputil_packet_in_reason_to_string(i))) {
2290 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
2293 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
2294 * message's actions. The caller must initialize 'ofpacts' and retains
2295 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
2297 * Returns 0 if successful, otherwise an OFPERR_* value. */
2299 ofputil_decode_packet_out(struct ofputil_packet_out *po,
2300 const struct ofp_header *oh,
2301 struct ofpbuf *ofpacts)
2306 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2307 raw = ofpraw_pull_assert(&b);
2309 if (raw == OFPRAW_OFPT11_PACKET_OUT) {
2311 const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
2313 po->buffer_id = ntohl(opo->buffer_id);
2314 error = ofputil_port_from_ofp11(opo->in_port, &po->in_port);
2319 error = ofpacts_pull_openflow11_actions(&b, ntohs(opo->actions_len),
2324 } else if (raw == OFPRAW_OFPT10_PACKET_OUT) {
2326 const struct ofp_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
2328 po->buffer_id = ntohl(opo->buffer_id);
2329 po->in_port = ntohs(opo->in_port);
2331 error = ofpacts_pull_openflow10(&b, ntohs(opo->actions_len), ofpacts);
2339 if (po->in_port >= OFPP_MAX && po->in_port != OFPP_LOCAL
2340 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
2341 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
2343 return OFPERR_OFPBRC_BAD_PORT;
2346 po->ofpacts = ofpacts->data;
2347 po->ofpacts_len = ofpacts->size;
2349 if (po->buffer_id == UINT32_MAX) {
2350 po->packet = b.data;
2351 po->packet_len = b.size;
2360 /* ofputil_phy_port */
2362 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
2363 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2364 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2365 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2366 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2367 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2368 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2369 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2371 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
2372 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
2373 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
2374 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
2375 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
2376 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
2378 static enum netdev_features
2379 netdev_port_features_from_ofp10(ovs_be32 ofp10_)
2381 uint32_t ofp10 = ntohl(ofp10_);
2382 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
2386 netdev_port_features_to_ofp10(enum netdev_features features)
2388 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
2391 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
2392 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
2393 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
2394 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
2395 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
2396 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
2397 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
2398 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
2399 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
2400 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
2401 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
2402 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
2403 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
2404 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
2405 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
2406 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
2408 static enum netdev_features
2409 netdev_port_features_from_ofp11(ovs_be32 ofp11)
2411 return ntohl(ofp11) & 0xffff;
2415 netdev_port_features_to_ofp11(enum netdev_features features)
2417 return htonl(features & 0xffff);
2421 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
2422 const struct ofp10_phy_port *opp)
2424 memset(pp, 0, sizeof *pp);
2426 pp->port_no = ntohs(opp->port_no);
2427 memcpy(pp->hw_addr, opp->hw_addr, OFP_ETH_ALEN);
2428 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
2430 pp->config = ntohl(opp->config) & OFPPC10_ALL;
2431 pp->state = ntohl(opp->state) & OFPPS10_ALL;
2433 pp->curr = netdev_port_features_from_ofp10(opp->curr);
2434 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
2435 pp->supported = netdev_port_features_from_ofp10(opp->supported);
2436 pp->peer = netdev_port_features_from_ofp10(opp->peer);
2438 pp->curr_speed = netdev_features_to_bps(pp->curr) / 1000;
2439 pp->max_speed = netdev_features_to_bps(pp->supported) / 1000;
2445 ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
2446 const struct ofp11_port *op)
2450 memset(pp, 0, sizeof *pp);
2452 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
2456 memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN);
2457 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
2459 pp->config = ntohl(op->config) & OFPPC11_ALL;
2460 pp->state = ntohl(op->state) & OFPPC11_ALL;
2462 pp->curr = netdev_port_features_from_ofp11(op->curr);
2463 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
2464 pp->supported = netdev_port_features_from_ofp11(op->supported);
2465 pp->peer = netdev_port_features_from_ofp11(op->peer);
2467 pp->curr_speed = ntohl(op->curr_speed);
2468 pp->max_speed = ntohl(op->max_speed);
2474 ofputil_get_phy_port_size(enum ofp_version ofp_version)
2476 switch (ofp_version) {
2478 return sizeof(struct ofp10_phy_port);
2481 return sizeof(struct ofp11_port);
2488 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
2489 struct ofp10_phy_port *opp)
2491 memset(opp, 0, sizeof *opp);
2493 opp->port_no = htons(pp->port_no);
2494 memcpy(opp->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2495 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2497 opp->config = htonl(pp->config & OFPPC10_ALL);
2498 opp->state = htonl(pp->state & OFPPS10_ALL);
2500 opp->curr = netdev_port_features_to_ofp10(pp->curr);
2501 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
2502 opp->supported = netdev_port_features_to_ofp10(pp->supported);
2503 opp->peer = netdev_port_features_to_ofp10(pp->peer);
2507 ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
2508 struct ofp11_port *op)
2510 memset(op, 0, sizeof *op);
2512 op->port_no = ofputil_port_to_ofp11(pp->port_no);
2513 memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
2514 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
2516 op->config = htonl(pp->config & OFPPC11_ALL);
2517 op->state = htonl(pp->state & OFPPS11_ALL);
2519 op->curr = netdev_port_features_to_ofp11(pp->curr);
2520 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
2521 op->supported = netdev_port_features_to_ofp11(pp->supported);
2522 op->peer = netdev_port_features_to_ofp11(pp->peer);
2524 op->curr_speed = htonl(pp->curr_speed);
2525 op->max_speed = htonl(pp->max_speed);
2529 ofputil_put_phy_port(enum ofp_version ofp_version,
2530 const struct ofputil_phy_port *pp, struct ofpbuf *b)
2532 switch (ofp_version) {
2533 case OFP10_VERSION: {
2534 struct ofp10_phy_port *opp;
2535 if (b->size + sizeof *opp <= UINT16_MAX) {
2536 opp = ofpbuf_put_uninit(b, sizeof *opp);
2537 ofputil_encode_ofp10_phy_port(pp, opp);
2543 case OFP12_VERSION: {
2544 struct ofp11_port *op;
2545 if (b->size + sizeof *op <= UINT16_MAX) {
2546 op = ofpbuf_put_uninit(b, sizeof *op);
2547 ofputil_encode_ofp11_port(pp, op);
2558 ofputil_append_port_desc_stats_reply(enum ofp_version ofp_version,
2559 const struct ofputil_phy_port *pp,
2560 struct list *replies)
2562 switch (ofp_version) {
2563 case OFP10_VERSION: {
2564 struct ofp10_phy_port *opp;
2566 opp = ofpmp_append(replies, sizeof *opp);
2567 ofputil_encode_ofp10_phy_port(pp, opp);
2572 case OFP12_VERSION: {
2573 struct ofp11_port *op;
2575 op = ofpmp_append(replies, sizeof *op);
2576 ofputil_encode_ofp11_port(pp, op);
2585 /* ofputil_switch_features */
2587 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
2588 OFPC_IP_REASM | OFPC_QUEUE_STATS)
2589 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
2590 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
2591 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
2592 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
2593 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
2594 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
2596 struct ofputil_action_bit_translation {
2597 enum ofputil_action_bitmap ofputil_bit;
2601 static const struct ofputil_action_bit_translation of10_action_bits[] = {
2602 { OFPUTIL_A_OUTPUT, OFPAT10_OUTPUT },
2603 { OFPUTIL_A_SET_VLAN_VID, OFPAT10_SET_VLAN_VID },
2604 { OFPUTIL_A_SET_VLAN_PCP, OFPAT10_SET_VLAN_PCP },
2605 { OFPUTIL_A_STRIP_VLAN, OFPAT10_STRIP_VLAN },
2606 { OFPUTIL_A_SET_DL_SRC, OFPAT10_SET_DL_SRC },
2607 { OFPUTIL_A_SET_DL_DST, OFPAT10_SET_DL_DST },
2608 { OFPUTIL_A_SET_NW_SRC, OFPAT10_SET_NW_SRC },
2609 { OFPUTIL_A_SET_NW_DST, OFPAT10_SET_NW_DST },
2610 { OFPUTIL_A_SET_NW_TOS, OFPAT10_SET_NW_TOS },
2611 { OFPUTIL_A_SET_TP_SRC, OFPAT10_SET_TP_SRC },
2612 { OFPUTIL_A_SET_TP_DST, OFPAT10_SET_TP_DST },
2613 { OFPUTIL_A_ENQUEUE, OFPAT10_ENQUEUE },
2617 static enum ofputil_action_bitmap
2618 decode_action_bits(ovs_be32 of_actions,
2619 const struct ofputil_action_bit_translation *x)
2621 enum ofputil_action_bitmap ofputil_actions;
2623 ofputil_actions = 0;
2624 for (; x->ofputil_bit; x++) {
2625 if (of_actions & htonl(1u << x->of_bit)) {
2626 ofputil_actions |= x->ofputil_bit;
2629 return ofputil_actions;
2633 ofputil_capabilities_mask(enum ofp_version ofp_version)
2635 /* Handle capabilities whose bit is unique for all Open Flow versions */
2636 switch (ofp_version) {
2639 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
2641 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
2643 /* Caller needs to check osf->header.version itself */
2648 /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an
2649 * abstract representation in '*features'. Initializes '*b' to iterate over
2650 * the OpenFlow port structures following 'osf' with later calls to
2651 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an
2652 * OFPERR_* value. */
2654 ofputil_decode_switch_features(const struct ofp_header *oh,
2655 struct ofputil_switch_features *features,
2658 const struct ofp_switch_features *osf;
2661 ofpbuf_use_const(b, oh, ntohs(oh->length));
2662 raw = ofpraw_pull_assert(b);
2664 osf = ofpbuf_pull(b, sizeof *osf);
2665 features->datapath_id = ntohll(osf->datapath_id);
2666 features->n_buffers = ntohl(osf->n_buffers);
2667 features->n_tables = osf->n_tables;
2669 features->capabilities = ntohl(osf->capabilities) &
2670 ofputil_capabilities_mask(oh->version);
2672 if (b->size % ofputil_get_phy_port_size(oh->version)) {
2673 return OFPERR_OFPBRC_BAD_LEN;
2676 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
2677 if (osf->capabilities & htonl(OFPC10_STP)) {
2678 features->capabilities |= OFPUTIL_C_STP;
2680 features->actions = decode_action_bits(osf->actions, of10_action_bits);
2681 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY) {
2682 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
2683 features->capabilities |= OFPUTIL_C_GROUP_STATS;
2685 features->actions = 0;
2687 return OFPERR_OFPBRC_BAD_VERSION;
2693 /* Returns true if the maximum number of ports are in 'oh'. */
2695 max_ports_in_features(const struct ofp_header *oh)
2697 size_t pp_size = ofputil_get_phy_port_size(oh->version);
2698 return ntohs(oh->length) + pp_size > UINT16_MAX;
2701 /* Given a buffer 'b' that contains a Features Reply message, checks if
2702 * it contains the maximum number of ports that will fit. If so, it
2703 * returns true and removes the ports from the message. The caller
2704 * should then send an OFPST_PORT_DESC stats request to get the ports,
2705 * since the switch may have more ports than could be represented in the
2706 * Features Reply. Otherwise, returns false.
2709 ofputil_switch_features_ports_trunc(struct ofpbuf *b)
2711 struct ofp_header *oh = b->data;
2713 if (max_ports_in_features(oh)) {
2714 /* Remove all the ports. */
2715 b->size = (sizeof(struct ofp_header)
2716 + sizeof(struct ofp_switch_features));
2717 ofpmsg_update_length(b);
2726 encode_action_bits(enum ofputil_action_bitmap ofputil_actions,
2727 const struct ofputil_action_bit_translation *x)
2729 uint32_t of_actions;
2732 for (; x->ofputil_bit; x++) {
2733 if (ofputil_actions & x->ofputil_bit) {
2734 of_actions |= 1 << x->of_bit;
2737 return htonl(of_actions);
2740 /* Returns a buffer owned by the caller that encodes 'features' in the format
2741 * required by 'protocol' with the given 'xid'. The caller should append port
2742 * information to the buffer with subsequent calls to
2743 * ofputil_put_switch_features_port(). */
2745 ofputil_encode_switch_features(const struct ofputil_switch_features *features,
2746 enum ofputil_protocol protocol, ovs_be32 xid)
2748 struct ofp_switch_features *osf;
2750 enum ofp_version version;
2753 version = ofputil_protocol_to_ofp_version(protocol);
2756 raw = OFPRAW_OFPT10_FEATURES_REPLY;
2760 raw = OFPRAW_OFPT11_FEATURES_REPLY;
2765 b = ofpraw_alloc_xid(raw, version, xid, 0);
2766 osf = ofpbuf_put_zeros(b, sizeof *osf);
2767 osf->datapath_id = htonll(features->datapath_id);
2768 osf->n_buffers = htonl(features->n_buffers);
2769 osf->n_tables = features->n_tables;
2771 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
2772 osf->capabilities = htonl(features->capabilities &
2773 ofputil_capabilities_mask(version));
2776 if (features->capabilities & OFPUTIL_C_STP) {
2777 osf->capabilities |= htonl(OFPC10_STP);
2779 osf->actions = encode_action_bits(features->actions, of10_action_bits);
2783 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
2784 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
2794 /* Encodes 'pp' into the format required by the switch_features message already
2795 * in 'b', which should have been returned by ofputil_encode_switch_features(),
2796 * and appends the encoded version to 'b'. */
2798 ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
2801 const struct ofp_header *oh = b->data;
2803 ofputil_put_phy_port(oh->version, pp, b);
2806 /* ofputil_port_status */
2808 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
2809 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
2811 ofputil_decode_port_status(const struct ofp_header *oh,
2812 struct ofputil_port_status *ps)
2814 const struct ofp_port_status *ops;
2818 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2819 ofpraw_pull_assert(&b);
2820 ops = ofpbuf_pull(&b, sizeof *ops);
2822 if (ops->reason != OFPPR_ADD &&
2823 ops->reason != OFPPR_DELETE &&
2824 ops->reason != OFPPR_MODIFY) {
2825 return OFPERR_NXBRC_BAD_REASON;
2827 ps->reason = ops->reason;
2829 retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
2830 assert(retval != EOF);
2834 /* Converts the abstract form of a "port status" message in '*ps' into an
2835 * OpenFlow message suitable for 'protocol', and returns that encoded form in
2836 * a buffer owned by the caller. */
2838 ofputil_encode_port_status(const struct ofputil_port_status *ps,
2839 enum ofputil_protocol protocol)
2841 struct ofp_port_status *ops;
2843 enum ofp_version version;
2846 version = ofputil_protocol_to_ofp_version(protocol);
2849 raw = OFPRAW_OFPT10_PORT_STATUS;
2854 raw = OFPRAW_OFPT11_PORT_STATUS;
2861 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
2862 ops = ofpbuf_put_zeros(b, sizeof *ops);
2863 ops->reason = ps->reason;
2864 ofputil_put_phy_port(version, &ps->desc, b);
2865 ofpmsg_update_length(b);
2869 /* ofputil_port_mod */
2871 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
2872 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
2874 ofputil_decode_port_mod(const struct ofp_header *oh,
2875 struct ofputil_port_mod *pm)
2880 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2881 raw = ofpraw_pull_assert(&b);
2883 if (raw == OFPRAW_OFPT10_PORT_MOD) {
2884 const struct ofp10_port_mod *opm = b.data;
2886 pm->port_no = ntohs(opm->port_no);
2887 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
2888 pm->config = ntohl(opm->config) & OFPPC10_ALL;
2889 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
2890 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
2891 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
2892 const struct ofp11_port_mod *opm = b.data;
2895 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
2900 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
2901 pm->config = ntohl(opm->config) & OFPPC11_ALL;
2902 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
2903 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
2905 return OFPERR_OFPBRC_BAD_TYPE;
2908 pm->config &= pm->mask;
2912 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
2913 * message suitable for 'protocol', and returns that encoded form in a buffer
2914 * owned by the caller. */
2916 ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
2917 enum ofputil_protocol protocol)
2919 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
2922 switch (ofp_version) {
2923 case OFP10_VERSION: {
2924 struct ofp10_port_mod *opm;
2926 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
2927 opm = ofpbuf_put_zeros(b, sizeof *opm);
2928 opm->port_no = htons(pm->port_no);
2929 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
2930 opm->config = htonl(pm->config & OFPPC10_ALL);
2931 opm->mask = htonl(pm->mask & OFPPC10_ALL);
2932 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
2937 case OFP12_VERSION: {
2938 struct ofp11_port_mod *opm;
2940 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
2941 opm = ofpbuf_put_zeros(b, sizeof *opm);
2942 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
2943 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
2944 opm->config = htonl(pm->config & OFPPC11_ALL);
2945 opm->mask = htonl(pm->mask & OFPPC11_ALL);
2946 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
2960 ofputil_put_ofp10_table_stats(const struct ofp12_table_stats *in,
2964 enum ofp_flow_wildcards wc10;
2965 enum oxm12_ofb_match_fields mf12;
2968 static const struct wc_map wc_map[] = {
2969 { OFPFW10_IN_PORT, OFPXMT12_OFB_IN_PORT },
2970 { OFPFW10_DL_VLAN, OFPXMT12_OFB_VLAN_VID },
2971 { OFPFW10_DL_SRC, OFPXMT12_OFB_ETH_SRC },
2972 { OFPFW10_DL_DST, OFPXMT12_OFB_ETH_DST},
2973 { OFPFW10_DL_TYPE, OFPXMT12_OFB_ETH_TYPE },
2974 { OFPFW10_NW_PROTO, OFPXMT12_OFB_IP_PROTO },
2975 { OFPFW10_TP_SRC, OFPXMT12_OFB_TCP_SRC },
2976 { OFPFW10_TP_DST, OFPXMT12_OFB_TCP_DST },
2977 { OFPFW10_NW_SRC_MASK, OFPXMT12_OFB_IPV4_SRC },
2978 { OFPFW10_NW_DST_MASK, OFPXMT12_OFB_IPV4_DST },
2979 { OFPFW10_DL_VLAN_PCP, OFPXMT12_OFB_VLAN_PCP },
2980 { OFPFW10_NW_TOS, OFPXMT12_OFB_IP_DSCP },
2983 struct ofp10_table_stats *out;
2984 const struct wc_map *p;
2986 out = ofpbuf_put_uninit(buf, sizeof *out);
2987 out->table_id = in->table_id;
2988 strcpy(out->name, in->name);
2990 for (p = wc_map; p < &wc_map[ARRAY_SIZE(wc_map)]; p++) {
2991 if (in->wildcards & htonll(1ULL << p->mf12)) {
2992 out->wildcards |= htonl(p->wc10);
2995 out->max_entries = in->max_entries;
2996 out->active_count = in->active_count;
2997 put_32aligned_be64(&out->lookup_count, in->lookup_count);
2998 put_32aligned_be64(&out->matched_count, in->matched_count);
3002 oxm12_to_ofp11_flow_match_fields(ovs_be64 oxm12)
3005 enum ofp11_flow_match_fields fmf11;
3006 enum oxm12_ofb_match_fields mf12;
3009 static const struct map map[] = {
3010 { OFPFMF11_IN_PORT, OFPXMT12_OFB_IN_PORT },
3011 { OFPFMF11_DL_VLAN, OFPXMT12_OFB_VLAN_VID },
3012 { OFPFMF11_DL_VLAN_PCP, OFPXMT12_OFB_VLAN_PCP },
3013 { OFPFMF11_DL_TYPE, OFPXMT12_OFB_ETH_TYPE },
3014 { OFPFMF11_NW_TOS, OFPXMT12_OFB_IP_DSCP },
3015 { OFPFMF11_NW_PROTO, OFPXMT12_OFB_IP_PROTO },
3016 { OFPFMF11_TP_SRC, OFPXMT12_OFB_TCP_SRC },
3017 { OFPFMF11_TP_DST, OFPXMT12_OFB_TCP_DST },
3018 { OFPFMF11_MPLS_LABEL, OFPXMT12_OFB_MPLS_LABEL },
3019 { OFPFMF11_MPLS_TC, OFPXMT12_OFB_MPLS_TC },
3020 /* I don't know what OFPFMF11_TYPE means. */
3021 { OFPFMF11_DL_SRC, OFPXMT12_OFB_ETH_SRC },
3022 { OFPFMF11_DL_DST, OFPXMT12_OFB_ETH_DST },
3023 { OFPFMF11_NW_SRC, OFPXMT12_OFB_IPV4_SRC },
3024 { OFPFMF11_NW_DST, OFPXMT12_OFB_IPV4_DST },
3025 { OFPFMF11_METADATA, OFPXMT12_OFB_METADATA },
3028 const struct map *p;
3032 for (p = map; p < &map[ARRAY_SIZE(map)]; p++) {
3033 if (oxm12 & htonll(1ULL << p->mf12)) {
3037 return htonl(fmf11);
3041 ofputil_put_ofp11_table_stats(const struct ofp12_table_stats *in,
3044 struct ofp11_table_stats *out;
3046 out = ofpbuf_put_uninit(buf, sizeof *out);
3047 out->table_id = in->table_id;
3048 strcpy(out->name, in->name);
3049 out->wildcards = oxm12_to_ofp11_flow_match_fields(in->wildcards);
3050 out->match = oxm12_to_ofp11_flow_match_fields(in->match);
3051 out->instructions = in->instructions;
3052 out->write_actions = in->write_actions;
3053 out->apply_actions = in->apply_actions;
3054 out->config = in->config;
3055 out->max_entries = in->max_entries;
3056 out->active_count = in->active_count;
3057 out->lookup_count = in->lookup_count;
3058 out->matched_count = in->matched_count;
3062 ofputil_encode_table_stats_reply(const struct ofp12_table_stats stats[], int n,
3063 const struct ofp_header *request)
3065 struct ofpbuf *reply;
3068 reply = ofpraw_alloc_stats_reply(request, n * sizeof *stats);
3070 switch ((enum ofp_version) request->version) {
3072 for (i = 0; i < n; i++) {
3073 ofputil_put_ofp10_table_stats(&stats[i], reply);
3078 for (i = 0; i < n; i++) {
3079 ofputil_put_ofp11_table_stats(&stats[i], reply);
3084 ofpbuf_put(reply, stats, n * sizeof *stats);
3094 /* ofputil_flow_monitor_request */
3096 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
3097 * ofputil_flow_monitor_request in 'rq'.
3099 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
3100 * message. Calling this function multiple times for a single 'msg' iterates
3101 * through the requests. The caller must initially leave 'msg''s layer
3102 * pointers null and not modify them between calls.
3104 * Returns 0 if successful, EOF if no requests were left in this 'msg',
3105 * otherwise an OFPERR_* value. */
3107 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
3110 struct nx_flow_monitor_request *nfmr;
3114 msg->l2 = msg->data;
3115 ofpraw_pull_assert(msg);
3122 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
3124 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %zu "
3125 "leftover bytes at end", msg->size);
3126 return OFPERR_OFPBRC_BAD_LEN;
3129 flags = ntohs(nfmr->flags);
3130 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
3131 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
3132 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
3133 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
3135 return OFPERR_NXBRC_FM_BAD_FLAGS;
3138 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
3139 return OFPERR_NXBRC_MUST_BE_ZERO;
3142 rq->id = ntohl(nfmr->id);
3144 rq->out_port = ntohs(nfmr->out_port);
3145 rq->table_id = nfmr->table_id;
3147 return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL, NULL);
3151 ofputil_append_flow_monitor_request(
3152 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
3154 struct nx_flow_monitor_request *nfmr;
3159 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
3162 start_ofs = msg->size;
3163 ofpbuf_put_zeros(msg, sizeof *nfmr);
3164 match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
3166 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
3167 nfmr->id = htonl(rq->id);
3168 nfmr->flags = htons(rq->flags);
3169 nfmr->out_port = htons(rq->out_port);
3170 nfmr->match_len = htons(match_len);
3171 nfmr->table_id = rq->table_id;
3174 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
3175 * into an abstract ofputil_flow_update in 'update'. The caller must have
3176 * initialized update->match to point to space allocated for a match.
3178 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
3179 * actions (except for NXFME_ABBREV, which never includes actions). The caller
3180 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
3181 * will point into the 'ofpacts' buffer.
3183 * Multiple flow updates can be packed into a single OpenFlow message. Calling
3184 * this function multiple times for a single 'msg' iterates through the
3185 * updates. The caller must initially leave 'msg''s layer pointers null and
3186 * not modify them between calls.
3188 * Returns 0 if successful, EOF if no updates were left in this 'msg',
3189 * otherwise an OFPERR_* value. */
3191 ofputil_decode_flow_update(struct ofputil_flow_update *update,
3192 struct ofpbuf *msg, struct ofpbuf *ofpacts)
3194 struct nx_flow_update_header *nfuh;
3195 unsigned int length;
3198 msg->l2 = msg->data;
3199 ofpraw_pull_assert(msg);
3206 if (msg->size < sizeof(struct nx_flow_update_header)) {
3211 update->event = ntohs(nfuh->event);
3212 length = ntohs(nfuh->length);
3213 if (length > msg->size || length % 8) {
3217 if (update->event == NXFME_ABBREV) {
3218 struct nx_flow_update_abbrev *nfua;
3220 if (length != sizeof *nfua) {
3224 nfua = ofpbuf_pull(msg, sizeof *nfua);
3225 update->xid = nfua->xid;
3227 } else if (update->event == NXFME_ADDED
3228 || update->event == NXFME_DELETED
3229 || update->event == NXFME_MODIFIED) {
3230 struct nx_flow_update_full *nfuf;
3231 unsigned int actions_len;
3232 unsigned int match_len;
3235 if (length < sizeof *nfuf) {
3239 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
3240 match_len = ntohs(nfuf->match_len);
3241 if (sizeof *nfuf + match_len > length) {
3245 update->reason = ntohs(nfuf->reason);
3246 update->idle_timeout = ntohs(nfuf->idle_timeout);
3247 update->hard_timeout = ntohs(nfuf->hard_timeout);
3248 update->table_id = nfuf->table_id;
3249 update->cookie = nfuf->cookie;
3250 update->priority = ntohs(nfuf->priority);
3252 error = nx_pull_match(msg, match_len, update->match, NULL, NULL);
3257 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
3258 error = ofpacts_pull_openflow10(msg, actions_len, ofpacts);
3263 update->ofpacts = ofpacts->data;
3264 update->ofpacts_len = ofpacts->size;
3267 VLOG_WARN_RL(&bad_ofmsg_rl,
3268 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
3269 ntohs(nfuh->event));
3270 return OFPERR_OFPET_BAD_REQUEST;
3274 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %zu "
3275 "leftover bytes at end", msg->size);
3276 return OFPERR_OFPBRC_BAD_LEN;
3280 ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
3282 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
3284 return ntohl(cancel->id);
3288 ofputil_encode_flow_monitor_cancel(uint32_t id)
3290 struct nx_flow_monitor_cancel *nfmc;
3293 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
3294 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
3295 nfmc->id = htonl(id);
3300 ofputil_start_flow_update(struct list *replies)
3304 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
3308 list_push_back(replies, &msg->list_node);
3312 ofputil_append_flow_update(const struct ofputil_flow_update *update,
3313 struct list *replies)
3315 struct nx_flow_update_header *nfuh;
3319 msg = ofpbuf_from_list(list_back(replies));
3320 start_ofs = msg->size;
3322 if (update->event == NXFME_ABBREV) {
3323 struct nx_flow_update_abbrev *nfua;
3325 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
3326 nfua->xid = update->xid;
3328 struct nx_flow_update_full *nfuf;
3331 ofpbuf_put_zeros(msg, sizeof *nfuf);
3332 match_len = nx_put_match(msg, update->match, htonll(0), htonll(0));
3333 ofpacts_put_openflow10(update->ofpacts, update->ofpacts_len, msg);
3335 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
3336 nfuf->reason = htons(update->reason);
3337 nfuf->priority = htons(update->priority);
3338 nfuf->idle_timeout = htons(update->idle_timeout);
3339 nfuf->hard_timeout = htons(update->hard_timeout);
3340 nfuf->match_len = htons(match_len);
3341 nfuf->table_id = update->table_id;
3342 nfuf->cookie = update->cookie;
3345 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
3346 nfuh->length = htons(msg->size - start_ofs);
3347 nfuh->event = htons(update->event);
3349 ofpmp_postappend(replies, start_ofs);
3353 ofputil_encode_packet_out(const struct ofputil_packet_out *po,
3354 enum ofputil_protocol protocol)
3356 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
3360 size = po->ofpacts_len;
3361 if (po->buffer_id == UINT32_MAX) {
3362 size += po->packet_len;
3365 switch (ofp_version) {
3366 case OFP10_VERSION: {
3367 struct ofp_packet_out *opo;
3370 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
3371 ofpbuf_put_zeros(msg, sizeof *opo);
3372 actions_ofs = msg->size;
3373 ofpacts_put_openflow10(po->ofpacts, po->ofpacts_len, msg);
3376 opo->buffer_id = htonl(po->buffer_id);
3377 opo->in_port = htons(po->in_port);
3378 opo->actions_len = htons(msg->size - actions_ofs);
3383 case OFP12_VERSION: {
3384 struct ofp11_packet_out *opo;
3387 msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size);
3388 ofpbuf_put_zeros(msg, sizeof *opo);
3389 len = ofpacts_put_openflow11_actions(po->ofpacts, po->ofpacts_len, msg);
3392 opo->buffer_id = htonl(po->buffer_id);
3393 opo->in_port = ofputil_port_to_ofp11(po->in_port);
3394 opo->actions_len = htons(len);
3402 if (po->buffer_id == UINT32_MAX) {
3403 ofpbuf_put(msg, po->packet, po->packet_len);
3406 ofpmsg_update_length(msg);
3411 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
3413 make_echo_request(enum ofp_version ofp_version)
3415 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
3419 /* Creates and returns an OFPT_ECHO_REPLY message matching the
3420 * OFPT_ECHO_REQUEST message in 'rq'. */
3422 make_echo_reply(const struct ofp_header *rq)
3424 struct ofpbuf rq_buf;
3425 struct ofpbuf *reply;
3427 ofpbuf_use_const(&rq_buf, rq, ntohs(rq->length));
3428 ofpraw_pull_assert(&rq_buf);
3430 reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY, rq, rq_buf.size);
3431 ofpbuf_put(reply, rq_buf.data, rq_buf.size);
3436 ofputil_encode_barrier_request(enum ofp_version ofp_version)
3440 switch (ofp_version) {
3443 type = OFPRAW_OFPT11_BARRIER_REQUEST;
3447 type = OFPRAW_OFPT10_BARRIER_REQUEST;
3454 return ofpraw_alloc(type, ofp_version, 0);
3458 ofputil_frag_handling_to_string(enum ofp_config_flags flags)
3460 switch (flags & OFPC_FRAG_MASK) {
3461 case OFPC_FRAG_NORMAL: return "normal";
3462 case OFPC_FRAG_DROP: return "drop";
3463 case OFPC_FRAG_REASM: return "reassemble";
3464 case OFPC_FRAG_NX_MATCH: return "nx-match";
3471 ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
3473 if (!strcasecmp(s, "normal")) {
3474 *flags = OFPC_FRAG_NORMAL;
3475 } else if (!strcasecmp(s, "drop")) {
3476 *flags = OFPC_FRAG_DROP;
3477 } else if (!strcasecmp(s, "reassemble")) {
3478 *flags = OFPC_FRAG_REASM;
3479 } else if (!strcasecmp(s, "nx-match")) {
3480 *flags = OFPC_FRAG_NX_MATCH;
3487 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
3488 * port number and stores the latter in '*ofp10_port', for the purpose of
3489 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
3490 * otherwise an OFPERR_* number.
3492 * See the definition of OFP11_MAX for an explanation of the mapping. */
3494 ofputil_port_from_ofp11(ovs_be32 ofp11_port, uint16_t *ofp10_port)
3496 uint32_t ofp11_port_h = ntohl(ofp11_port);
3498 if (ofp11_port_h < OFPP_MAX) {
3499 *ofp10_port = ofp11_port_h;
3501 } else if (ofp11_port_h >= OFPP11_MAX) {
3502 *ofp10_port = ofp11_port_h - OFPP11_OFFSET;
3505 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
3506 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
3507 ofp11_port_h, OFPP_MAX - 1,
3508 (uint32_t) OFPP11_MAX, UINT32_MAX);
3509 return OFPERR_OFPBAC_BAD_OUT_PORT;
3513 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
3514 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
3516 * See the definition of OFP11_MAX for an explanation of the mapping. */
3518 ofputil_port_to_ofp11(uint16_t ofp10_port)
3520 return htonl(ofp10_port < OFPP_MAX
3522 : ofp10_port + OFPP11_OFFSET);
3525 /* Checks that 'port' is a valid output port for the OFPAT10_OUTPUT action, given
3526 * that the switch will never have more than 'max_ports' ports. Returns 0 if
3527 * 'port' is valid, otherwise an OpenFlow return code. */
3529 ofputil_check_output_port(uint16_t port, int max_ports)
3537 case OFPP_CONTROLLER:
3543 if (port < max_ports) {
3546 return OFPERR_OFPBAC_BAD_OUT_PORT;
3550 #define OFPUTIL_NAMED_PORTS \
3551 OFPUTIL_NAMED_PORT(IN_PORT) \
3552 OFPUTIL_NAMED_PORT(TABLE) \
3553 OFPUTIL_NAMED_PORT(NORMAL) \
3554 OFPUTIL_NAMED_PORT(FLOOD) \
3555 OFPUTIL_NAMED_PORT(ALL) \
3556 OFPUTIL_NAMED_PORT(CONTROLLER) \
3557 OFPUTIL_NAMED_PORT(LOCAL) \
3558 OFPUTIL_NAMED_PORT(NONE)
3560 /* Stores the port number represented by 's' into '*portp'. 's' may be an
3561 * integer or, for reserved ports, the standard OpenFlow name for the port
3564 * Returns true if successful, false if 's' is not a valid OpenFlow port number
3565 * or name. The caller should issue an error message in this case, because
3566 * this function usually does not. (This gives the caller an opportunity to
3567 * look up the port name another way, e.g. by contacting the switch and listing
3568 * the names of all its ports).
3570 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
3571 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
3572 * range as described in include/openflow/openflow-1.1.h. */
3574 ofputil_port_from_string(const char *s, uint16_t *portp)
3576 unsigned int port32;
3579 if (str_to_uint(s, 10, &port32)) {
3580 if (port32 < OFPP_MAX) {
3583 } else if (port32 < OFPP_FIRST_RESV) {
3584 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
3585 "be translated to %u when talking to an OF1.1 or "
3586 "later controller", port32, port32 + OFPP11_OFFSET);
3589 } else if (port32 <= OFPP_LAST_RESV) {
3593 ofputil_format_port(port32, &s);
3594 VLOG_WARN_ONCE("referring to port %s as %u is deprecated for "
3595 "compatibility with future versions of OpenFlow",
3596 ds_cstr(&s), port32);
3601 } else if (port32 < OFPP11_MAX) {
3602 VLOG_WARN("port %u is outside the supported range 0 through "
3603 "%"PRIx16"or 0x%x through 0x%"PRIx32, port32,
3604 UINT16_MAX, (unsigned int) OFPP11_MAX, UINT32_MAX);
3607 *portp = port32 - OFPP11_OFFSET;
3615 static const struct pair pairs[] = {
3616 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
3618 #undef OFPUTIL_NAMED_PORT
3620 const struct pair *p;
3622 for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) {
3623 if (!strcasecmp(s, p->name)) {
3632 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
3633 * Most ports' string representation is just the port number, but for special
3634 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
3636 ofputil_format_port(uint16_t port, struct ds *s)
3641 #define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: name = #NAME; break;
3643 #undef OFPUTIL_NAMED_PORT
3646 ds_put_format(s, "%"PRIu16, port);
3649 ds_put_cstr(s, name);
3652 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
3653 * 'ofp_version', tries to pull the first element from the array. If
3654 * successful, initializes '*pp' with an abstract representation of the
3655 * port and returns 0. If no ports remain to be decoded, returns EOF.
3656 * On an error, returns a positive OFPERR_* value. */
3658 ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
3659 struct ofputil_phy_port *pp)
3661 switch (ofp_version) {
3662 case OFP10_VERSION: {
3663 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
3664 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
3667 case OFP12_VERSION: {
3668 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
3669 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
3676 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
3677 * 'ofp_version', returns the number of elements. */
3678 size_t ofputil_count_phy_ports(uint8_t ofp_version, struct ofpbuf *b)
3680 return b->size / ofputil_get_phy_port_size(ofp_version);
3683 /* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if
3684 * 'name' is "output" then the return value is OFPUTIL_OFPAT10_OUTPUT), or -1 if
3685 * 'name' is not the name of any action.
3687 * ofp-util.def lists the mapping from names to action. */
3689 ofputil_action_code_from_name(const char *name)
3691 static const char *names[OFPUTIL_N_ACTIONS] = {
3693 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) NAME,
3694 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
3695 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
3696 #include "ofp-util.def"
3701 for (p = names; p < &names[ARRAY_SIZE(names)]; p++) {
3702 if (*p && !strcasecmp(name, *p)) {
3709 /* Appends an action of the type specified by 'code' to 'buf' and returns the
3710 * action. Initializes the parts of 'action' that identify it as having type
3711 * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that
3712 * have variable length, the length used and cleared is that of struct
3715 ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
3718 case OFPUTIL_ACTION_INVALID:
3721 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
3722 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
3723 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3724 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
3725 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3726 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
3727 #include "ofp-util.def"
3732 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
3734 ofputil_init_##ENUM(struct STRUCT *s) \
3736 memset(s, 0, sizeof *s); \
3737 s->type = htons(ENUM); \
3738 s->len = htons(sizeof *s); \
3742 ofputil_put_##ENUM(struct ofpbuf *buf) \
3744 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
3745 ofputil_init_##ENUM(s); \
3748 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3749 OFPAT10_ACTION(ENUM, STRUCT, NAME)
3750 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
3752 ofputil_init_##ENUM(struct STRUCT *s) \
3754 memset(s, 0, sizeof *s); \
3755 s->type = htons(OFPAT10_VENDOR); \
3756 s->len = htons(sizeof *s); \
3757 s->vendor = htonl(NX_VENDOR_ID); \
3758 s->subtype = htons(ENUM); \
3762 ofputil_put_##ENUM(struct ofpbuf *buf) \
3764 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
3765 ofputil_init_##ENUM(s); \
3768 #include "ofp-util.def"
3771 ofputil_normalize_match__(struct match *match, bool may_log)
3774 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
3775 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
3776 MAY_NW_PROTO = 1 << 2, /* nw_proto */
3777 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
3778 MAY_ARP_SHA = 1 << 4, /* arp_sha */
3779 MAY_ARP_THA = 1 << 5, /* arp_tha */
3780 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
3781 MAY_ND_TARGET = 1 << 7 /* nd_target */
3784 struct flow_wildcards wc;
3786 /* Figure out what fields may be matched. */
3787 if (match->flow.dl_type == htons(ETH_TYPE_IP)) {
3788 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
3789 if (match->flow.nw_proto == IPPROTO_TCP ||
3790 match->flow.nw_proto == IPPROTO_UDP ||
3791 match->flow.nw_proto == IPPROTO_ICMP) {
3792 may_match |= MAY_TP_ADDR;
3794 } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
3795 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
3796 if (match->flow.nw_proto == IPPROTO_TCP ||
3797 match->flow.nw_proto == IPPROTO_UDP) {
3798 may_match |= MAY_TP_ADDR;
3799 } else if (match->flow.nw_proto == IPPROTO_ICMPV6) {
3800 may_match |= MAY_TP_ADDR;
3801 if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
3802 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
3803 } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
3804 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
3807 } else if (match->flow.dl_type == htons(ETH_TYPE_ARP) ||
3808 match->flow.dl_type == htons(ETH_TYPE_RARP)) {
3809 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
3814 /* Clear the fields that may not be matched. */
3816 if (!(may_match & MAY_NW_ADDR)) {
3817 wc.masks.nw_src = wc.masks.nw_dst = htonl(0);
3819 if (!(may_match & MAY_TP_ADDR)) {
3820 wc.masks.tp_src = wc.masks.tp_dst = htons(0);
3822 if (!(may_match & MAY_NW_PROTO)) {
3823 wc.masks.nw_proto = 0;
3825 if (!(may_match & MAY_IPVx)) {
3826 wc.masks.nw_tos = 0;
3827 wc.masks.nw_ttl = 0;
3829 if (!(may_match & MAY_ARP_SHA)) {
3830 memset(wc.masks.arp_sha, 0, ETH_ADDR_LEN);
3832 if (!(may_match & MAY_ARP_THA)) {
3833 memset(wc.masks.arp_tha, 0, ETH_ADDR_LEN);
3835 if (!(may_match & MAY_IPV6)) {
3836 wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any;
3837 wc.masks.ipv6_label = htonl(0);
3839 if (!(may_match & MAY_ND_TARGET)) {
3840 wc.masks.nd_target = in6addr_any;
3843 /* Log any changes. */
3844 if (!flow_wildcards_equal(&wc, &match->wc)) {
3845 bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
3846 char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL;
3849 match_zero_wildcarded_fields(match);
3852 char *post = match_to_string(match, OFP_DEFAULT_PRIORITY);
3853 VLOG_INFO("normalization changed ofp_match, details:");
3854 VLOG_INFO(" pre: %s", pre);
3855 VLOG_INFO("post: %s", post);
3862 /* "Normalizes" the wildcards in 'match'. That means:
3864 * 1. If the type of level N is known, then only the valid fields for that
3865 * level may be specified. For example, ARP does not have a TOS field,
3866 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
3867 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
3868 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
3871 * 2. If the type of level N is not known (or not understood by Open
3872 * vSwitch), then no fields at all for that level may be specified. For
3873 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
3874 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
3877 * If this function changes 'match', it logs a rate-limited informational
3880 ofputil_normalize_match(struct match *match)
3882 ofputil_normalize_match__(match, true);
3885 /* Same as ofputil_normalize_match() without the logging. Thus, this function
3886 * is suitable for a program's internal use, whereas ofputil_normalize_match()
3887 * sense for use on flows received from elsewhere (so that a bug in the program
3888 * that sent them can be reported and corrected). */
3890 ofputil_normalize_match_quiet(struct match *match)
3892 ofputil_normalize_match__(match, false);
3895 /* Parses a key or a key-value pair from '*stringp'.
3897 * On success: Stores the key into '*keyp'. Stores the value, if present, into
3898 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
3899 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
3900 * are substrings of '*stringp' created by replacing some of its bytes by null
3901 * terminators. Returns true.
3903 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
3904 * NULL and returns false. */
3906 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
3908 char *pos, *key, *value;
3912 pos += strspn(pos, ", \t\r\n");
3914 *keyp = *valuep = NULL;
3919 key_len = strcspn(pos, ":=(, \t\r\n");
3920 if (key[key_len] == ':' || key[key_len] == '=') {
3921 /* The value can be separated by a colon. */
3924 value = key + key_len + 1;
3925 value_len = strcspn(value, ", \t\r\n");
3926 pos = value + value_len + (value[value_len] != '\0');
3927 value[value_len] = '\0';
3928 } else if (key[key_len] == '(') {
3929 /* The value can be surrounded by balanced parentheses. The outermost
3930 * set of parentheses is removed. */
3934 value = key + key_len + 1;
3935 for (value_len = 0; level > 0; value_len++) {
3936 switch (value[value_len]) {
3950 value[value_len - 1] = '\0';
3951 pos = value + value_len;
3953 /* There might be no value at all. */
3954 value = key + key_len; /* Will become the empty string below. */
3955 pos = key + key_len + (key[key_len] != '\0');
3957 key[key_len] = '\0';
3965 /* Encode a dump ports request for 'port', the encoded message
3966 * will be fore Open Flow version 'ofp_version'. Returns message
3967 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
3969 ofputil_encode_dump_ports_request(enum ofp_version ofp_version, int16_t port)
3971 struct ofpbuf *request;
3973 switch (ofp_version) {
3974 case OFP10_VERSION: {
3975 struct ofp10_port_stats_request *req;
3976 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0);
3977 req = ofpbuf_put_zeros(request, sizeof *req);
3978 req->port_no = htons(port);
3982 case OFP12_VERSION: {
3983 struct ofp11_port_stats_request *req;
3984 request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0);
3985 req = ofpbuf_put_zeros(request, sizeof *req);
3986 req->port_no = ofputil_port_to_ofp11(port);
3997 ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops,
3998 struct ofp10_port_stats *ps10)
4000 ps10->port_no = htons(ops->port_no);
4001 memset(ps10->pad, 0, sizeof ps10->pad);
4002 put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets));
4003 put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets));
4004 put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes));
4005 put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes));
4006 put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped));
4007 put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped));
4008 put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors));
4009 put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors));
4010 put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors));
4011 put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors));
4012 put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors));
4013 put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions));
4017 ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops,
4018 struct ofp11_port_stats *ps11)
4020 ps11->port_no = ofputil_port_to_ofp11(ops->port_no);
4021 memset(ps11->pad, 0, sizeof ps11->pad);
4022 ps11->rx_packets = htonll(ops->stats.rx_packets);
4023 ps11->tx_packets = htonll(ops->stats.tx_packets);
4024 ps11->rx_bytes = htonll(ops->stats.rx_bytes);
4025 ps11->tx_bytes = htonll(ops->stats.tx_bytes);
4026 ps11->rx_dropped = htonll(ops->stats.rx_dropped);
4027 ps11->tx_dropped = htonll(ops->stats.tx_dropped);
4028 ps11->rx_errors = htonll(ops->stats.rx_errors);
4029 ps11->tx_errors = htonll(ops->stats.tx_errors);
4030 ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors);
4031 ps11->rx_over_err = htonll(ops->stats.rx_over_errors);
4032 ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors);
4033 ps11->collisions = htonll(ops->stats.collisions);
4036 /* Encode a ports stat for 'ops' and append it to 'replies'. */
4038 ofputil_append_port_stat(struct list *replies,
4039 const struct ofputil_port_stats *ops)
4041 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
4042 struct ofp_header *oh = msg->data;
4044 switch ((enum ofp_version)oh->version) {
4046 case OFP11_VERSION: {
4047 struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply);
4048 ofputil_port_stats_to_ofp11(ops, reply);
4052 case OFP10_VERSION: {
4053 struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply);
4054 ofputil_port_stats_to_ofp10(ops, reply);
4064 ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops,
4065 const struct ofp10_port_stats *ps10)
4067 memset(ops, 0, sizeof *ops);
4069 ops->port_no = ntohs(ps10->port_no);
4070 ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets));
4071 ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets));
4072 ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes));
4073 ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes));
4074 ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped));
4075 ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped));
4076 ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors));
4077 ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors));
4078 ops->stats.rx_frame_errors =
4079 ntohll(get_32aligned_be64(&ps10->rx_frame_err));
4080 ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err));
4081 ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err));
4082 ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions));
4088 ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops,
4089 const struct ofp11_port_stats *ps11)
4093 memset(ops, 0, sizeof *ops);
4094 error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no);
4099 ops->stats.rx_packets = ntohll(ps11->rx_packets);
4100 ops->stats.tx_packets = ntohll(ps11->tx_packets);
4101 ops->stats.rx_bytes = ntohll(ps11->rx_bytes);
4102 ops->stats.tx_bytes = ntohll(ps11->tx_bytes);
4103 ops->stats.rx_dropped = ntohll(ps11->rx_dropped);
4104 ops->stats.tx_dropped = ntohll(ps11->tx_dropped);
4105 ops->stats.rx_errors = ntohll(ps11->rx_errors);
4106 ops->stats.tx_errors = ntohll(ps11->tx_errors);
4107 ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err);
4108 ops->stats.rx_over_errors = ntohll(ps11->rx_over_err);
4109 ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err);
4110 ops->stats.collisions = ntohll(ps11->collisions);
4115 /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
4118 ofputil_count_port_stats(const struct ofp_header *oh)
4122 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4123 ofpraw_pull_assert(&b);
4125 BUILD_ASSERT(sizeof(struct ofp10_port_stats) ==
4126 sizeof(struct ofp11_port_stats));
4127 return b.size / sizeof(struct ofp10_port_stats);
4130 /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
4131 * ofputil_port_stats in 'ps'.
4133 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
4134 * message. Calling this function multiple times for a single 'msg' iterates
4135 * through the replies. The caller must initially leave 'msg''s layer pointers
4136 * null and not modify them between calls.
4138 * Returns 0 if successful, EOF if no replies were left in this 'msg',
4139 * otherwise a positive errno value. */
4141 ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg)
4147 ? ofpraw_decode(&raw, msg->l2)
4148 : ofpraw_pull(&raw, msg));
4155 } else if (raw == OFPRAW_OFPST11_PORT_REPLY) {
4156 const struct ofp11_port_stats *ps11;
4158 ps11 = ofpbuf_try_pull(msg, sizeof *ps11);
4160 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %zu leftover "
4161 "bytes at end", msg->size);
4162 return OFPERR_OFPBRC_BAD_LEN;
4164 return ofputil_port_stats_from_ofp11(ps, ps11);
4165 } else if (raw == OFPRAW_OFPST10_PORT_REPLY) {
4166 const struct ofp10_port_stats *ps10;
4168 ps10 = ofpbuf_try_pull(msg, sizeof *ps10);
4170 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %zu leftover "
4171 "bytes at end", msg->size);
4172 return OFPERR_OFPBRC_BAD_LEN;
4174 return ofputil_port_stats_from_ofp10(ps, ps10);
4181 /* Parse a port status request message into a 16 bit OpenFlow 1.0
4182 * port number and stores the latter in '*ofp10_port'.
4183 * Returns 0 if successful, otherwise an OFPERR_* number. */
4185 ofputil_decode_port_stats_request(const struct ofp_header *request,
4186 uint16_t *ofp10_port)
4188 switch ((enum ofp_version)request->version) {
4190 case OFP11_VERSION: {
4191 const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request);
4192 return ofputil_port_from_ofp11(psr11->port_no, ofp10_port);
4195 case OFP10_VERSION: {
4196 const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request);
4197 *ofp10_port = ntohs(psr10->port_no);
4206 /* Parse a queue status request message into 'oqsr'.
4207 * Returns 0 if successful, otherwise an OFPERR_* number. */
4209 ofputil_decode_queue_stats_request(const struct ofp_header *request,
4210 struct ofputil_queue_stats_request *oqsr)
4212 switch ((enum ofp_version)request->version) {
4214 case OFP11_VERSION: {
4215 const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request);
4216 oqsr->queue_id = ntohl(qsr11->queue_id);
4217 return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no);
4220 case OFP10_VERSION: {
4221 const struct ofp10_queue_stats_request *qsr11 = ofpmsg_body(request);
4222 oqsr->queue_id = ntohl(qsr11->queue_id);
4223 oqsr->port_no = ntohs(qsr11->port_no);
4232 /* Encode a queue statsrequest for 'oqsr', the encoded message
4233 * will be fore Open Flow version 'ofp_version'. Returns message
4234 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
4236 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
4237 const struct ofputil_queue_stats_request *oqsr)
4239 struct ofpbuf *request;
4241 switch (ofp_version) {
4243 case OFP12_VERSION: {
4244 struct ofp11_queue_stats_request *req;
4245 request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0);
4246 req = ofpbuf_put_zeros(request, sizeof *req);
4247 req->port_no = ofputil_port_to_ofp11(oqsr->port_no);
4248 req->queue_id = htonl(oqsr->queue_id);
4251 case OFP10_VERSION: {
4252 struct ofp10_queue_stats_request *req;
4253 request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0);
4254 req = ofpbuf_put_zeros(request, sizeof *req);
4255 req->port_no = htons(oqsr->port_no);
4256 req->queue_id = htonl(oqsr->queue_id);
4266 /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
4269 ofputil_count_queue_stats(const struct ofp_header *oh)
4273 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4274 ofpraw_pull_assert(&b);
4276 BUILD_ASSERT(sizeof(struct ofp10_queue_stats) ==
4277 sizeof(struct ofp11_queue_stats));
4278 return b.size / sizeof(struct ofp10_queue_stats);
4282 ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs,
4283 const struct ofp10_queue_stats *qs10)
4285 oqs->port_no = ntohs(qs10->port_no);
4286 oqs->queue_id = ntohl(qs10->queue_id);
4287 oqs->stats.tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes));
4288 oqs->stats.tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets));
4289 oqs->stats.tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors));
4295 ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs,
4296 const struct ofp11_queue_stats *qs11)
4300 error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no);
4305 oqs->queue_id = ntohl(qs11->queue_id);
4306 oqs->stats.tx_bytes = ntohll(qs11->tx_bytes);
4307 oqs->stats.tx_packets = ntohll(qs11->tx_packets);
4308 oqs->stats.tx_errors = ntohll(qs11->tx_errors);
4313 /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
4314 * ofputil_queue_stats in 'qs'.
4316 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
4317 * message. Calling this function multiple times for a single 'msg' iterates
4318 * through the replies. The caller must initially leave 'msg''s layer pointers
4319 * null and not modify them between calls.
4321 * Returns 0 if successful, EOF if no replies were left in this 'msg',
4322 * otherwise a positive errno value. */
4324 ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg)
4330 ? ofpraw_decode(&raw, msg->l2)
4331 : ofpraw_pull(&raw, msg));
4338 } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) {
4339 const struct ofp11_queue_stats *qs11;
4341 qs11 = ofpbuf_try_pull(msg, sizeof *qs11);
4343 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %zu leftover "
4344 "bytes at end", msg->size);
4345 return OFPERR_OFPBRC_BAD_LEN;
4347 return ofputil_queue_stats_from_ofp11(qs, qs11);
4348 } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) {
4349 const struct ofp10_queue_stats *qs10;
4351 qs10 = ofpbuf_try_pull(msg, sizeof *qs10);
4353 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %zu leftover "
4354 "bytes at end", msg->size);
4355 return OFPERR_OFPBRC_BAD_LEN;
4357 return ofputil_queue_stats_from_ofp10(qs, qs10);
4364 ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs,
4365 struct ofp10_queue_stats *qs10)
4367 qs10->port_no = htons(oqs->port_no);
4368 memset(qs10->pad, 0, sizeof qs10->pad);
4369 qs10->queue_id = htonl(oqs->queue_id);
4370 put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->stats.tx_bytes));
4371 put_32aligned_be64(&qs10->tx_packets, htonll(oqs->stats.tx_packets));
4372 put_32aligned_be64(&qs10->tx_errors, htonll(oqs->stats.tx_errors));
4376 ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs,
4377 struct ofp11_queue_stats *qs11)
4379 qs11->port_no = ofputil_port_to_ofp11(oqs->port_no);
4380 qs11->queue_id = htonl(oqs->queue_id);
4381 qs11->tx_bytes = htonll(oqs->stats.tx_bytes);
4382 qs11->tx_packets = htonll(oqs->stats.tx_packets);
4383 qs11->tx_errors = htonll(oqs->stats.tx_errors);
4386 /* Encode a queue stat for 'oqs' and append it to 'replies'. */
4388 ofputil_append_queue_stat(struct list *replies,
4389 const struct ofputil_queue_stats *oqs)
4391 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
4392 struct ofp_header *oh = msg->data;
4394 switch ((enum ofp_version)oh->version) {
4396 case OFP11_VERSION: {
4397 struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply);;
4398 ofputil_queue_stats_to_ofp11(oqs, reply);
4402 case OFP10_VERSION: {
4403 struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply);;
4404 ofputil_queue_stats_to_ofp10(oqs, reply);