2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofp-print.h"
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
27 #include "byte-order.h"
28 #include "classifier.h"
29 #include "dynamic-string.h"
31 #include "meta-flow.h"
32 #include "multipath.h"
36 #include "ofp-actions.h"
37 #include "ofp-errors.h"
42 #include "openflow/netronome-ext.h"
45 #include "tun-metadata.h"
46 #include "unaligned.h"
47 #include "type-props.h"
48 #include "openvswitch/vlog.h"
51 VLOG_DEFINE_THIS_MODULE(ofp_util);
53 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
54 * in the peer and so there's not much point in showing a lot of them. */
55 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
57 static enum ofputil_table_vacancy ofputil_decode_table_vacancy(
58 ovs_be32 config, enum ofp_version);
59 static enum ofputil_table_eviction ofputil_decode_table_eviction(
60 ovs_be32 config, enum ofp_version);
61 static ovs_be32 ofputil_encode_table_config(enum ofputil_table_miss,
62 enum ofputil_table_eviction,
63 enum ofputil_table_vacancy,
67 ofputil_check_mask(uint16_t type, uint32_t mask)
70 case OFPACPT_PACKET_IN_SLAVE:
71 case OFPACPT_PACKET_IN_MASTER:
72 if (mask > MAXIMUM_MASK_PACKET_IN) {
73 return OFPERR_OFPACFC_INVALID;
77 case OFPACPT_FLOW_REMOVED_SLAVE:
78 case OFPACPT_FLOW_REMOVED_MASTER:
79 if (mask > MAXIMUM_MASK_FLOW_REMOVED) {
80 return OFPERR_OFPACFC_INVALID;
84 case OFPACPT_PORT_STATUS_SLAVE:
85 case OFPACPT_PORT_STATUS_MASTER:
86 if (mask > MAXIMUM_MASK_PORT_STATUS) {
87 return OFPERR_OFPACFC_INVALID;
91 case OFPACPT_ROLE_STATUS_SLAVE:
92 case OFPACPT_ROLE_STATUS_MASTER:
93 if (mask > MAXIMUM_MASK_ROLE_STATUS) {
94 return OFPERR_OFPACFC_INVALID;
98 case OFPACPT_TABLE_STATUS_SLAVE:
99 case OFPACPT_TABLE_STATUS_MASTER:
100 if ((mask < MINIMUM_MASK_TABLE_STATUS && mask != 0) |
101 (mask > MAXIMUM_MASK_TABLE_STATUS)) {
102 return OFPERR_OFPACFC_INVALID;
106 case OFPACPT_REQUESTFORWARD_SLAVE:
107 case OFPACPT_REQUESTFORWARD_MASTER:
108 if (mask > MAXIMUM_MASK_REQUESTFORWARD) {
109 return OFPERR_OFPACFC_INVALID;
116 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
117 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
120 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
121 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
122 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
123 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
126 ofputil_wcbits_to_netmask(int wcbits)
129 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
132 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
133 * that it wildcards, that is, the number of 0-bits in 'netmask', a number
134 * between 0 and 32 inclusive.
136 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
137 * still be in the valid range but isn't otherwise meaningful. */
139 ofputil_netmask_to_wcbits(ovs_be32 netmask)
141 return 32 - ip_count_cidr_bits(netmask);
144 /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
145 * flow_wildcards in 'wc' for use in struct match. It is the caller's
146 * responsibility to handle the special case where the flow match's dl_vlan is
147 * set to OFP_VLAN_NONE. */
149 ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc)
151 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 35);
153 /* Initialize most of wc. */
154 flow_wildcards_init_catchall(wc);
156 if (!(ofpfw & OFPFW10_IN_PORT)) {
157 wc->masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
160 if (!(ofpfw & OFPFW10_NW_TOS)) {
161 wc->masks.nw_tos |= IP_DSCP_MASK;
164 if (!(ofpfw & OFPFW10_NW_PROTO)) {
165 wc->masks.nw_proto = UINT8_MAX;
167 wc->masks.nw_src = ofputil_wcbits_to_netmask(ofpfw
168 >> OFPFW10_NW_SRC_SHIFT);
169 wc->masks.nw_dst = ofputil_wcbits_to_netmask(ofpfw
170 >> OFPFW10_NW_DST_SHIFT);
172 if (!(ofpfw & OFPFW10_TP_SRC)) {
173 wc->masks.tp_src = OVS_BE16_MAX;
175 if (!(ofpfw & OFPFW10_TP_DST)) {
176 wc->masks.tp_dst = OVS_BE16_MAX;
179 if (!(ofpfw & OFPFW10_DL_SRC)) {
180 WC_MASK_FIELD(wc, dl_src);
182 if (!(ofpfw & OFPFW10_DL_DST)) {
183 WC_MASK_FIELD(wc, dl_dst);
185 if (!(ofpfw & OFPFW10_DL_TYPE)) {
186 wc->masks.dl_type = OVS_BE16_MAX;
190 if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) {
191 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
193 if (!(ofpfw & OFPFW10_DL_VLAN)) {
194 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
198 /* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */
200 ofputil_match_from_ofp10_match(const struct ofp10_match *ofmatch,
203 uint32_t ofpfw = ntohl(ofmatch->wildcards) & OFPFW10_ALL;
205 /* Initialize match->wc. */
206 memset(&match->flow, 0, sizeof match->flow);
207 ofputil_wildcard_from_ofpfw10(ofpfw, &match->wc);
209 /* Initialize most of match->flow. */
210 match->flow.nw_src = ofmatch->nw_src;
211 match->flow.nw_dst = ofmatch->nw_dst;
212 match->flow.in_port.ofp_port = u16_to_ofp(ntohs(ofmatch->in_port));
213 match->flow.dl_type = ofputil_dl_type_from_openflow(ofmatch->dl_type);
214 match->flow.tp_src = ofmatch->tp_src;
215 match->flow.tp_dst = ofmatch->tp_dst;
216 match->flow.dl_src = ofmatch->dl_src;
217 match->flow.dl_dst = ofmatch->dl_dst;
218 match->flow.nw_tos = ofmatch->nw_tos & IP_DSCP_MASK;
219 match->flow.nw_proto = ofmatch->nw_proto;
221 /* Translate VLANs. */
222 if (!(ofpfw & OFPFW10_DL_VLAN) &&
223 ofmatch->dl_vlan == htons(OFP10_VLAN_NONE)) {
224 /* Match only packets without 802.1Q header.
226 * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
228 * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
229 * because we can't have a specific PCP without an 802.1Q header.
230 * However, older versions of OVS treated this as matching packets
231 * withut an 802.1Q header, so we do here too. */
232 match->flow.vlan_tci = htons(0);
233 match->wc.masks.vlan_tci = htons(0xffff);
235 ovs_be16 vid, pcp, tci;
238 vid = ofmatch->dl_vlan & htons(VLAN_VID_MASK);
239 hpcp = (ofmatch->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK;
241 tci = vid | pcp | htons(VLAN_CFI);
242 match->flow.vlan_tci = tci & match->wc.masks.vlan_tci;
246 match_zero_wildcarded_fields(match);
249 /* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */
251 ofputil_match_to_ofp10_match(const struct match *match,
252 struct ofp10_match *ofmatch)
254 const struct flow_wildcards *wc = &match->wc;
257 /* Figure out most OpenFlow wildcards. */
259 if (!wc->masks.in_port.ofp_port) {
260 ofpfw |= OFPFW10_IN_PORT;
262 if (!wc->masks.dl_type) {
263 ofpfw |= OFPFW10_DL_TYPE;
265 if (!wc->masks.nw_proto) {
266 ofpfw |= OFPFW10_NW_PROTO;
268 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_src)
269 << OFPFW10_NW_SRC_SHIFT);
270 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_dst)
271 << OFPFW10_NW_DST_SHIFT);
272 if (!(wc->masks.nw_tos & IP_DSCP_MASK)) {
273 ofpfw |= OFPFW10_NW_TOS;
275 if (!wc->masks.tp_src) {
276 ofpfw |= OFPFW10_TP_SRC;
278 if (!wc->masks.tp_dst) {
279 ofpfw |= OFPFW10_TP_DST;
281 if (eth_addr_is_zero(wc->masks.dl_src)) {
282 ofpfw |= OFPFW10_DL_SRC;
284 if (eth_addr_is_zero(wc->masks.dl_dst)) {
285 ofpfw |= OFPFW10_DL_DST;
288 /* Translate VLANs. */
289 ofmatch->dl_vlan = htons(0);
290 ofmatch->dl_vlan_pcp = 0;
291 if (match->wc.masks.vlan_tci == htons(0)) {
292 ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP;
293 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
294 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
295 ofmatch->dl_vlan = htons(OFP10_VLAN_NONE);
297 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
298 ofpfw |= OFPFW10_DL_VLAN;
300 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
303 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
304 ofpfw |= OFPFW10_DL_VLAN_PCP;
306 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
310 /* Compose most of the match structure. */
311 ofmatch->wildcards = htonl(ofpfw);
312 ofmatch->in_port = htons(ofp_to_u16(match->flow.in_port.ofp_port));
313 ofmatch->dl_src = match->flow.dl_src;
314 ofmatch->dl_dst = match->flow.dl_dst;
315 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
316 ofmatch->nw_src = match->flow.nw_src;
317 ofmatch->nw_dst = match->flow.nw_dst;
318 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
319 ofmatch->nw_proto = match->flow.nw_proto;
320 ofmatch->tp_src = match->flow.tp_src;
321 ofmatch->tp_dst = match->flow.tp_dst;
322 memset(ofmatch->pad1, '\0', sizeof ofmatch->pad1);
323 memset(ofmatch->pad2, '\0', sizeof ofmatch->pad2);
327 ofputil_pull_ofp11_match(struct ofpbuf *buf, struct match *match,
328 uint16_t *padded_match_len)
330 struct ofp11_match_header *omh = buf->data;
333 if (buf->size < sizeof *omh) {
334 return OFPERR_OFPBMC_BAD_LEN;
337 match_len = ntohs(omh->length);
339 switch (ntohs(omh->type)) {
340 case OFPMT_STANDARD: {
341 struct ofp11_match *om;
343 if (match_len != sizeof *om || buf->size < sizeof *om) {
344 return OFPERR_OFPBMC_BAD_LEN;
346 om = ofpbuf_pull(buf, sizeof *om);
347 if (padded_match_len) {
348 *padded_match_len = match_len;
350 return ofputil_match_from_ofp11_match(om, match);
354 if (padded_match_len) {
355 *padded_match_len = ROUND_UP(match_len, 8);
357 return oxm_pull_match(buf, match);
360 return OFPERR_OFPBMC_BAD_TYPE;
364 /* Converts the ofp11_match in 'ofmatch' into a struct match in 'match'.
365 * Returns 0 if successful, otherwise an OFPERR_* value. */
367 ofputil_match_from_ofp11_match(const struct ofp11_match *ofmatch,
370 uint16_t wc = ntohl(ofmatch->wildcards);
371 bool ipv4, arp, rarp;
373 match_init_catchall(match);
375 if (!(wc & OFPFW11_IN_PORT)) {
379 error = ofputil_port_from_ofp11(ofmatch->in_port, &ofp_port);
381 return OFPERR_OFPBMC_BAD_VALUE;
383 match_set_in_port(match, ofp_port);
386 match_set_dl_src_masked(match, ofmatch->dl_src,
387 eth_addr_invert(ofmatch->dl_src_mask));
388 match_set_dl_dst_masked(match, ofmatch->dl_dst,
389 eth_addr_invert(ofmatch->dl_dst_mask));
391 if (!(wc & OFPFW11_DL_VLAN)) {
392 if (ofmatch->dl_vlan == htons(OFPVID11_NONE)) {
393 /* Match only packets without a VLAN tag. */
394 match->flow.vlan_tci = htons(0);
395 match->wc.masks.vlan_tci = OVS_BE16_MAX;
397 if (ofmatch->dl_vlan == htons(OFPVID11_ANY)) {
398 /* Match any packet with a VLAN tag regardless of VID. */
399 match->flow.vlan_tci = htons(VLAN_CFI);
400 match->wc.masks.vlan_tci = htons(VLAN_CFI);
401 } else if (ntohs(ofmatch->dl_vlan) < 4096) {
402 /* Match only packets with the specified VLAN VID. */
403 match->flow.vlan_tci = htons(VLAN_CFI) | ofmatch->dl_vlan;
404 match->wc.masks.vlan_tci = htons(VLAN_CFI | VLAN_VID_MASK);
407 return OFPERR_OFPBMC_BAD_VALUE;
410 if (!(wc & OFPFW11_DL_VLAN_PCP)) {
411 if (ofmatch->dl_vlan_pcp <= 7) {
412 match->flow.vlan_tci |= htons(ofmatch->dl_vlan_pcp
414 match->wc.masks.vlan_tci |= htons(VLAN_PCP_MASK);
417 return OFPERR_OFPBMC_BAD_VALUE;
423 if (!(wc & OFPFW11_DL_TYPE)) {
424 match_set_dl_type(match,
425 ofputil_dl_type_from_openflow(ofmatch->dl_type));
428 ipv4 = match->flow.dl_type == htons(ETH_TYPE_IP);
429 arp = match->flow.dl_type == htons(ETH_TYPE_ARP);
430 rarp = match->flow.dl_type == htons(ETH_TYPE_RARP);
432 if (ipv4 && !(wc & OFPFW11_NW_TOS)) {
433 if (ofmatch->nw_tos & ~IP_DSCP_MASK) {
435 return OFPERR_OFPBMC_BAD_VALUE;
438 match_set_nw_dscp(match, ofmatch->nw_tos);
441 if (ipv4 || arp || rarp) {
442 if (!(wc & OFPFW11_NW_PROTO)) {
443 match_set_nw_proto(match, ofmatch->nw_proto);
445 match_set_nw_src_masked(match, ofmatch->nw_src, ~ofmatch->nw_src_mask);
446 match_set_nw_dst_masked(match, ofmatch->nw_dst, ~ofmatch->nw_dst_mask);
449 #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
450 if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) {
451 switch (match->flow.nw_proto) {
453 /* "A.2.3 Flow Match Structures" in OF1.1 says:
455 * The tp_src and tp_dst fields will be ignored unless the
456 * network protocol specified is as TCP, UDP or SCTP.
458 * but I'm pretty sure we should support ICMP too, otherwise
459 * that's a regression from OF1.0. */
460 if (!(wc & OFPFW11_TP_SRC)) {
461 uint16_t icmp_type = ntohs(ofmatch->tp_src);
462 if (icmp_type < 0x100) {
463 match_set_icmp_type(match, icmp_type);
465 return OFPERR_OFPBMC_BAD_FIELD;
468 if (!(wc & OFPFW11_TP_DST)) {
469 uint16_t icmp_code = ntohs(ofmatch->tp_dst);
470 if (icmp_code < 0x100) {
471 match_set_icmp_code(match, icmp_code);
473 return OFPERR_OFPBMC_BAD_FIELD;
481 if (!(wc & (OFPFW11_TP_SRC))) {
482 match_set_tp_src(match, ofmatch->tp_src);
484 if (!(wc & (OFPFW11_TP_DST))) {
485 match_set_tp_dst(match, ofmatch->tp_dst);
490 /* OF1.1 says explicitly to ignore this. */
495 if (eth_type_mpls(match->flow.dl_type)) {
496 if (!(wc & OFPFW11_MPLS_LABEL)) {
497 match_set_mpls_label(match, 0, ofmatch->mpls_label);
499 if (!(wc & OFPFW11_MPLS_TC)) {
500 match_set_mpls_tc(match, 0, ofmatch->mpls_tc);
504 match_set_metadata_masked(match, ofmatch->metadata,
505 ~ofmatch->metadata_mask);
510 /* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */
512 ofputil_match_to_ofp11_match(const struct match *match,
513 struct ofp11_match *ofmatch)
517 memset(ofmatch, 0, sizeof *ofmatch);
518 ofmatch->omh.type = htons(OFPMT_STANDARD);
519 ofmatch->omh.length = htons(OFPMT11_STANDARD_LENGTH);
521 if (!match->wc.masks.in_port.ofp_port) {
522 wc |= OFPFW11_IN_PORT;
524 ofmatch->in_port = ofputil_port_to_ofp11(match->flow.in_port.ofp_port);
527 ofmatch->dl_src = match->flow.dl_src;
528 ofmatch->dl_src_mask = eth_addr_invert(match->wc.masks.dl_src);
529 ofmatch->dl_dst = match->flow.dl_dst;
530 ofmatch->dl_dst_mask = eth_addr_invert(match->wc.masks.dl_dst);
532 if (match->wc.masks.vlan_tci == htons(0)) {
533 wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP;
534 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
535 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
536 ofmatch->dl_vlan = htons(OFPVID11_NONE);
537 wc |= OFPFW11_DL_VLAN_PCP;
539 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
540 ofmatch->dl_vlan = htons(OFPVID11_ANY);
542 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
545 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
546 wc |= OFPFW11_DL_VLAN_PCP;
548 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
552 if (!match->wc.masks.dl_type) {
553 wc |= OFPFW11_DL_TYPE;
555 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
558 if (!(match->wc.masks.nw_tos & IP_DSCP_MASK)) {
559 wc |= OFPFW11_NW_TOS;
561 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
564 if (!match->wc.masks.nw_proto) {
565 wc |= OFPFW11_NW_PROTO;
567 ofmatch->nw_proto = match->flow.nw_proto;
570 ofmatch->nw_src = match->flow.nw_src;
571 ofmatch->nw_src_mask = ~match->wc.masks.nw_src;
572 ofmatch->nw_dst = match->flow.nw_dst;
573 ofmatch->nw_dst_mask = ~match->wc.masks.nw_dst;
575 if (!match->wc.masks.tp_src) {
576 wc |= OFPFW11_TP_SRC;
578 ofmatch->tp_src = match->flow.tp_src;
581 if (!match->wc.masks.tp_dst) {
582 wc |= OFPFW11_TP_DST;
584 ofmatch->tp_dst = match->flow.tp_dst;
587 if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK))) {
588 wc |= OFPFW11_MPLS_LABEL;
590 ofmatch->mpls_label = htonl(mpls_lse_to_label(
591 match->flow.mpls_lse[0]));
594 if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK))) {
595 wc |= OFPFW11_MPLS_TC;
597 ofmatch->mpls_tc = mpls_lse_to_tc(match->flow.mpls_lse[0]);
600 ofmatch->metadata = match->flow.metadata;
601 ofmatch->metadata_mask = ~match->wc.masks.metadata;
603 ofmatch->wildcards = htonl(wc);
606 /* Returns the "typical" length of a match for 'protocol', for use in
607 * estimating space to preallocate. */
609 ofputil_match_typical_len(enum ofputil_protocol protocol)
612 case OFPUTIL_P_OF10_STD:
613 case OFPUTIL_P_OF10_STD_TID:
614 return sizeof(struct ofp10_match);
616 case OFPUTIL_P_OF10_NXM:
617 case OFPUTIL_P_OF10_NXM_TID:
618 return NXM_TYPICAL_LEN;
620 case OFPUTIL_P_OF11_STD:
621 return sizeof(struct ofp11_match);
623 case OFPUTIL_P_OF12_OXM:
624 case OFPUTIL_P_OF13_OXM:
625 case OFPUTIL_P_OF14_OXM:
626 case OFPUTIL_P_OF15_OXM:
627 return NXM_TYPICAL_LEN;
634 /* Appends to 'b' an struct ofp11_match_header followed by a match that
635 * expresses 'match' properly for 'protocol', plus enough zero bytes to pad the
636 * data appended out to a multiple of 8. 'protocol' must be one that is usable
637 * in OpenFlow 1.1 or later.
639 * This function can cause 'b''s data to be reallocated.
641 * Returns the number of bytes appended to 'b', excluding the padding. Never
644 ofputil_put_ofp11_match(struct ofpbuf *b, const struct match *match,
645 enum ofputil_protocol protocol)
648 case OFPUTIL_P_OF10_STD:
649 case OFPUTIL_P_OF10_STD_TID:
650 case OFPUTIL_P_OF10_NXM:
651 case OFPUTIL_P_OF10_NXM_TID:
654 case OFPUTIL_P_OF11_STD: {
655 struct ofp11_match *om;
657 /* Make sure that no padding is needed. */
658 BUILD_ASSERT_DECL(sizeof *om % 8 == 0);
660 om = ofpbuf_put_uninit(b, sizeof *om);
661 ofputil_match_to_ofp11_match(match, om);
665 case OFPUTIL_P_OF12_OXM:
666 case OFPUTIL_P_OF13_OXM:
667 case OFPUTIL_P_OF14_OXM:
668 case OFPUTIL_P_OF15_OXM:
669 return oxm_put_match(b, match,
670 ofputil_protocol_to_ofp_version(protocol));
676 /* Given a 'dl_type' value in the format used in struct flow, returns the
677 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
680 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
682 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
683 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
687 /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
688 * structure, returns the corresponding 'dl_type' value for use in struct
691 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
693 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
694 ? htons(FLOW_DL_TYPE_NONE)
700 struct proto_abbrev {
701 enum ofputil_protocol protocol;
705 /* Most users really don't care about some of the differences between
706 * protocols. These abbreviations help with that. */
707 static const struct proto_abbrev proto_abbrevs[] = {
708 { OFPUTIL_P_ANY, "any" },
709 { OFPUTIL_P_OF10_STD_ANY, "OpenFlow10" },
710 { OFPUTIL_P_OF10_NXM_ANY, "NXM" },
711 { OFPUTIL_P_ANY_OXM, "OXM" },
713 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
715 enum ofputil_protocol ofputil_flow_dump_protocols[] = {
724 size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
726 /* Returns the set of ofputil_protocols that are supported with the given
727 * OpenFlow 'version'. 'version' should normally be an 8-bit OpenFlow version
728 * identifier (e.g. 0x01 for OpenFlow 1.0, 0x02 for OpenFlow 1.1). Returns 0
729 * if 'version' is not supported or outside the valid range. */
730 enum ofputil_protocol
731 ofputil_protocols_from_ofp_version(enum ofp_version version)
735 return OFPUTIL_P_OF10_STD_ANY | OFPUTIL_P_OF10_NXM_ANY;
737 return OFPUTIL_P_OF11_STD;
739 return OFPUTIL_P_OF12_OXM;
741 return OFPUTIL_P_OF13_OXM;
743 return OFPUTIL_P_OF14_OXM;
745 return OFPUTIL_P_OF15_OXM;
751 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
752 * connection that has negotiated the given 'version'. 'version' should
753 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
754 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
755 * outside the valid range. */
756 enum ofputil_protocol
757 ofputil_protocol_from_ofp_version(enum ofp_version version)
759 return rightmost_1bit(ofputil_protocols_from_ofp_version(version));
762 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
763 * etc.) that corresponds to 'protocol'. */
765 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
768 case OFPUTIL_P_OF10_STD:
769 case OFPUTIL_P_OF10_STD_TID:
770 case OFPUTIL_P_OF10_NXM:
771 case OFPUTIL_P_OF10_NXM_TID:
772 return OFP10_VERSION;
773 case OFPUTIL_P_OF11_STD:
774 return OFP11_VERSION;
775 case OFPUTIL_P_OF12_OXM:
776 return OFP12_VERSION;
777 case OFPUTIL_P_OF13_OXM:
778 return OFP13_VERSION;
779 case OFPUTIL_P_OF14_OXM:
780 return OFP14_VERSION;
781 case OFPUTIL_P_OF15_OXM:
782 return OFP15_VERSION;
788 /* Returns a bitmap of OpenFlow versions that are supported by at
789 * least one of the 'protocols'. */
791 ofputil_protocols_to_version_bitmap(enum ofputil_protocol protocols)
795 for (; protocols; protocols = zero_rightmost_1bit(protocols)) {
796 enum ofputil_protocol protocol = rightmost_1bit(protocols);
798 bitmap |= 1u << ofputil_protocol_to_ofp_version(protocol);
804 /* Returns the set of protocols that are supported on top of the
805 * OpenFlow versions included in 'bitmap'. */
806 enum ofputil_protocol
807 ofputil_protocols_from_version_bitmap(uint32_t bitmap)
809 enum ofputil_protocol protocols = 0;
811 for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
812 enum ofp_version version = rightmost_1bit_idx(bitmap);
814 protocols |= ofputil_protocols_from_ofp_version(version);
820 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
823 ofputil_protocol_is_valid(enum ofputil_protocol protocol)
825 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
828 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
829 * extension turned on or off if 'enable' is true or false, respectively.
831 * This extension is only useful for protocols whose "standard" version does
832 * not allow specific tables to be modified. In particular, this is true of
833 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
834 * specifies a table ID and so there is no need for such an extension. When
835 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
836 * extension, this function just returns its 'protocol' argument unchanged
837 * regardless of the value of 'enable'. */
838 enum ofputil_protocol
839 ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
842 case OFPUTIL_P_OF10_STD:
843 case OFPUTIL_P_OF10_STD_TID:
844 return enable ? OFPUTIL_P_OF10_STD_TID : OFPUTIL_P_OF10_STD;
846 case OFPUTIL_P_OF10_NXM:
847 case OFPUTIL_P_OF10_NXM_TID:
848 return enable ? OFPUTIL_P_OF10_NXM_TID : OFPUTIL_P_OF10_NXM;
850 case OFPUTIL_P_OF11_STD:
851 return OFPUTIL_P_OF11_STD;
853 case OFPUTIL_P_OF12_OXM:
854 return OFPUTIL_P_OF12_OXM;
856 case OFPUTIL_P_OF13_OXM:
857 return OFPUTIL_P_OF13_OXM;
859 case OFPUTIL_P_OF14_OXM:
860 return OFPUTIL_P_OF14_OXM;
862 case OFPUTIL_P_OF15_OXM:
863 return OFPUTIL_P_OF15_OXM;
870 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
871 * some extension to a standard protocol version, the return value is the
872 * standard version of that protocol without any extension. If 'protocol' is a
873 * standard protocol version, returns 'protocol' unchanged. */
874 enum ofputil_protocol
875 ofputil_protocol_to_base(enum ofputil_protocol protocol)
877 return ofputil_protocol_set_tid(protocol, false);
880 /* Returns 'new_base' with any extensions taken from 'cur'. */
881 enum ofputil_protocol
882 ofputil_protocol_set_base(enum ofputil_protocol cur,
883 enum ofputil_protocol new_base)
885 bool tid = (cur & OFPUTIL_P_TID) != 0;
888 case OFPUTIL_P_OF10_STD:
889 case OFPUTIL_P_OF10_STD_TID:
890 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD, tid);
892 case OFPUTIL_P_OF10_NXM:
893 case OFPUTIL_P_OF10_NXM_TID:
894 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM, tid);
896 case OFPUTIL_P_OF11_STD:
897 return ofputil_protocol_set_tid(OFPUTIL_P_OF11_STD, tid);
899 case OFPUTIL_P_OF12_OXM:
900 return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM, tid);
902 case OFPUTIL_P_OF13_OXM:
903 return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM, tid);
905 case OFPUTIL_P_OF14_OXM:
906 return ofputil_protocol_set_tid(OFPUTIL_P_OF14_OXM, tid);
908 case OFPUTIL_P_OF15_OXM:
909 return ofputil_protocol_set_tid(OFPUTIL_P_OF15_OXM, tid);
916 /* Returns a string form of 'protocol', if a simple form exists (that is, if
917 * 'protocol' is either a single protocol or it is a combination of protocols
918 * that have a single abbreviation). Otherwise, returns NULL. */
920 ofputil_protocol_to_string(enum ofputil_protocol protocol)
922 const struct proto_abbrev *p;
924 /* Use a "switch" statement for single-bit names so that we get a compiler
925 * warning if we forget any. */
927 case OFPUTIL_P_OF10_NXM:
928 return "NXM-table_id";
930 case OFPUTIL_P_OF10_NXM_TID:
931 return "NXM+table_id";
933 case OFPUTIL_P_OF10_STD:
934 return "OpenFlow10-table_id";
936 case OFPUTIL_P_OF10_STD_TID:
937 return "OpenFlow10+table_id";
939 case OFPUTIL_P_OF11_STD:
942 case OFPUTIL_P_OF12_OXM:
943 return "OXM-OpenFlow12";
945 case OFPUTIL_P_OF13_OXM:
946 return "OXM-OpenFlow13";
948 case OFPUTIL_P_OF14_OXM:
949 return "OXM-OpenFlow14";
951 case OFPUTIL_P_OF15_OXM:
952 return "OXM-OpenFlow15";
955 /* Check abbreviations. */
956 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
957 if (protocol == p->protocol) {
965 /* Returns a string that represents 'protocols'. The return value might be a
966 * comma-separated list if 'protocols' doesn't have a simple name. The return
967 * value is "none" if 'protocols' is 0.
969 * The caller must free the returned string (with free()). */
971 ofputil_protocols_to_string(enum ofputil_protocol protocols)
975 ovs_assert(!(protocols & ~OFPUTIL_P_ANY));
976 if (protocols == 0) {
977 return xstrdup("none");
982 const struct proto_abbrev *p;
986 ds_put_char(&s, ',');
989 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
990 if ((protocols & p->protocol) == p->protocol) {
991 ds_put_cstr(&s, p->name);
992 protocols &= ~p->protocol;
997 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
998 enum ofputil_protocol bit = 1u << i;
1000 if (protocols & bit) {
1001 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
1010 return ds_steal_cstr(&s);
1013 static enum ofputil_protocol
1014 ofputil_protocol_from_string__(const char *s, size_t n)
1016 const struct proto_abbrev *p;
1019 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
1020 enum ofputil_protocol bit = 1u << i;
1021 const char *name = ofputil_protocol_to_string(bit);
1023 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
1028 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1029 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
1037 /* Returns the nonempty set of protocols represented by 's', which can be a
1038 * single protocol name or abbreviation or a comma-separated list of them.
1040 * Aborts the program with an error message if 's' is invalid. */
1041 enum ofputil_protocol
1042 ofputil_protocols_from_string(const char *s)
1044 const char *orig_s = s;
1045 enum ofputil_protocol protocols;
1049 enum ofputil_protocol p;
1052 n = strcspn(s, ",");
1058 p = ofputil_protocol_from_string__(s, n);
1060 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
1068 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
1074 ofputil_version_from_string(const char *s)
1076 if (!strcasecmp(s, "OpenFlow10")) {
1077 return OFP10_VERSION;
1079 if (!strcasecmp(s, "OpenFlow11")) {
1080 return OFP11_VERSION;
1082 if (!strcasecmp(s, "OpenFlow12")) {
1083 return OFP12_VERSION;
1085 if (!strcasecmp(s, "OpenFlow13")) {
1086 return OFP13_VERSION;
1088 if (!strcasecmp(s, "OpenFlow14")) {
1089 return OFP14_VERSION;
1091 if (!strcasecmp(s, "OpenFlow15")) {
1092 return OFP15_VERSION;
1098 is_delimiter(unsigned char c)
1100 return isspace(c) || c == ',';
1104 ofputil_versions_from_string(const char *s)
1107 uint32_t bitmap = 0;
1114 if (is_delimiter(s[i])) {
1119 while (s[i + j] && !is_delimiter(s[i + j])) {
1122 key = xmemdup0(s + i, j);
1123 version = ofputil_version_from_string(key);
1125 VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key);
1128 bitmap |= 1u << version;
1136 ofputil_versions_from_strings(char ** const s, size_t count)
1138 uint32_t bitmap = 0;
1141 int version = ofputil_version_from_string(s[count]);
1143 VLOG_WARN("Unknown OpenFlow version: \"%s\"", s[count]);
1145 bitmap |= 1u << version;
1153 ofputil_version_to_string(enum ofp_version ofp_version)
1155 switch (ofp_version) {
1157 return "OpenFlow10";
1159 return "OpenFlow11";
1161 return "OpenFlow12";
1163 return "OpenFlow13";
1165 return "OpenFlow14";
1167 return "OpenFlow15";
1174 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
1176 switch (packet_in_format) {
1177 case NXPIF_OPENFLOW10:
1186 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
1188 switch (packet_in_format) {
1189 case NXPIF_OPENFLOW10:
1190 return "openflow10";
1199 ofputil_packet_in_format_from_string(const char *s)
1201 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
1202 : !strcmp(s, "nxm") ? NXPIF_NXM
1207 ofputil_format_version(struct ds *msg, enum ofp_version version)
1209 ds_put_format(msg, "0x%02x", version);
1213 ofputil_format_version_name(struct ds *msg, enum ofp_version version)
1215 ds_put_cstr(msg, ofputil_version_to_string(version));
1219 ofputil_format_version_bitmap__(struct ds *msg, uint32_t bitmap,
1220 void (*format_version)(struct ds *msg,
1224 format_version(msg, raw_ctz(bitmap));
1225 bitmap = zero_rightmost_1bit(bitmap);
1227 ds_put_cstr(msg, ", ");
1233 ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap)
1235 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version);
1239 ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap)
1241 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version_name);
1245 ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header *oheh,
1246 uint32_t *allowed_versionsp)
1248 uint16_t bitmap_len = ntohs(oheh->length) - sizeof *oheh;
1249 const ovs_be32 *bitmap = ALIGNED_CAST(const ovs_be32 *, oheh + 1);
1250 uint32_t allowed_versions;
1252 if (!bitmap_len || bitmap_len % sizeof *bitmap) {
1256 /* Only use the first 32-bit element of the bitmap as that is all the
1257 * current implementation supports. Subsequent elements are ignored which
1258 * should have no effect on session negotiation until Open vSwitch supports
1259 * wire-protocol versions greater than 31.
1261 allowed_versions = ntohl(bitmap[0]);
1263 if (allowed_versions & 1) {
1264 /* There's no OpenFlow version 0. */
1265 VLOG_WARN_RL(&bad_ofmsg_rl, "peer claims to support invalid OpenFlow "
1267 allowed_versions &= ~1u;
1270 if (!allowed_versions) {
1271 VLOG_WARN_RL(&bad_ofmsg_rl, "peer does not support any OpenFlow "
1272 "version (between 0x01 and 0x1f)");
1276 *allowed_versionsp = allowed_versions;
1281 version_bitmap_from_version(uint8_t ofp_version)
1283 return ((ofp_version < 32 ? 1u << ofp_version : 0) - 1) << 1;
1286 /* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions'
1287 * the set of OpenFlow versions for which 'oh' announces support.
1289 * Because of how OpenFlow defines OFPT_HELLO messages, this function is always
1290 * successful, and thus '*allowed_versions' is always initialized. However, it
1291 * returns false if 'oh' contains some data that could not be fully understood,
1292 * true if 'oh' was completely parsed. */
1294 ofputil_decode_hello(const struct ofp_header *oh, uint32_t *allowed_versions)
1299 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
1300 ofpbuf_pull(&msg, sizeof *oh);
1302 *allowed_versions = version_bitmap_from_version(oh->version);
1304 const struct ofp_hello_elem_header *oheh;
1307 if (msg.size < sizeof *oheh) {
1312 len = ntohs(oheh->length);
1313 if (len < sizeof *oheh || !ofpbuf_try_pull(&msg, ROUND_UP(len, 8))) {
1317 if (oheh->type != htons(OFPHET_VERSIONBITMAP)
1318 || !ofputil_decode_hello_bitmap(oheh, allowed_versions)) {
1326 /* Returns true if 'allowed_versions' needs to be accompanied by a version
1327 * bitmap to be correctly expressed in an OFPT_HELLO message. */
1329 should_send_version_bitmap(uint32_t allowed_versions)
1331 return !is_pow2((allowed_versions >> 1) + 1);
1334 /* Create an OFPT_HELLO message that expresses support for the OpenFlow
1335 * versions in the 'allowed_versions' bitmaps and returns the message. */
1337 ofputil_encode_hello(uint32_t allowed_versions)
1339 enum ofp_version ofp_version;
1342 ofp_version = leftmost_1bit_idx(allowed_versions);
1343 msg = ofpraw_alloc(OFPRAW_OFPT_HELLO, ofp_version, 0);
1345 if (should_send_version_bitmap(allowed_versions)) {
1346 struct ofp_hello_elem_header *oheh;
1349 map_len = sizeof allowed_versions;
1350 oheh = ofpbuf_put_zeros(msg, ROUND_UP(map_len + sizeof *oheh, 8));
1351 oheh->type = htons(OFPHET_VERSIONBITMAP);
1352 oheh->length = htons(map_len + sizeof *oheh);
1353 *ALIGNED_CAST(ovs_be32 *, oheh + 1) = htonl(allowed_versions);
1355 ofpmsg_update_length(msg);
1361 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1362 * protocol is 'current', at least partly transitions the protocol to 'want'.
1363 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1364 * connection if the switch processes the returned message correctly. (If
1365 * '*next != want' then the caller will have to iterate.)
1367 * If 'current == want', or if it is not possible to transition from 'current'
1368 * to 'want' (because, for example, 'current' and 'want' use different OpenFlow
1369 * protocol versions), returns NULL and stores 'current' in '*next'. */
1371 ofputil_encode_set_protocol(enum ofputil_protocol current,
1372 enum ofputil_protocol want,
1373 enum ofputil_protocol *next)
1375 enum ofp_version cur_version, want_version;
1376 enum ofputil_protocol cur_base, want_base;
1377 bool cur_tid, want_tid;
1379 cur_version = ofputil_protocol_to_ofp_version(current);
1380 want_version = ofputil_protocol_to_ofp_version(want);
1381 if (cur_version != want_version) {
1386 cur_base = ofputil_protocol_to_base(current);
1387 want_base = ofputil_protocol_to_base(want);
1388 if (cur_base != want_base) {
1389 *next = ofputil_protocol_set_base(current, want_base);
1391 switch (want_base) {
1392 case OFPUTIL_P_OF10_NXM:
1393 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1395 case OFPUTIL_P_OF10_STD:
1396 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1398 case OFPUTIL_P_OF11_STD:
1399 case OFPUTIL_P_OF12_OXM:
1400 case OFPUTIL_P_OF13_OXM:
1401 case OFPUTIL_P_OF14_OXM:
1402 case OFPUTIL_P_OF15_OXM:
1403 /* There is only one variant of each OpenFlow 1.1+ protocol, and we
1404 * verified above that we're not trying to change versions. */
1407 case OFPUTIL_P_OF10_STD_TID:
1408 case OFPUTIL_P_OF10_NXM_TID:
1413 cur_tid = (current & OFPUTIL_P_TID) != 0;
1414 want_tid = (want & OFPUTIL_P_TID) != 0;
1415 if (cur_tid != want_tid) {
1416 *next = ofputil_protocol_set_tid(current, want_tid);
1417 return ofputil_make_flow_mod_table_id(want_tid);
1420 ovs_assert(current == want);
1426 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1427 * format to 'nxff'. */
1429 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1431 struct nx_set_flow_format *sff;
1434 ovs_assert(ofputil_nx_flow_format_is_valid(nxff));
1436 msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1437 sff = ofpbuf_put_zeros(msg, sizeof *sff);
1438 sff->format = htonl(nxff);
1443 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1445 enum ofputil_protocol
1446 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1448 switch (flow_format) {
1449 case NXFF_OPENFLOW10:
1450 return OFPUTIL_P_OF10_STD;
1453 return OFPUTIL_P_OF10_NXM;
1460 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1462 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1464 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1467 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1470 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1472 switch (flow_format) {
1473 case NXFF_OPENFLOW10:
1474 return "openflow10";
1483 ofputil_make_set_packet_in_format(enum ofp_version ofp_version,
1484 enum nx_packet_in_format packet_in_format)
1486 struct nx_set_packet_in_format *spif;
1489 msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, ofp_version, 0);
1490 spif = ofpbuf_put_zeros(msg, sizeof *spif);
1491 spif->format = htonl(packet_in_format);
1496 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1497 * extension on or off (according to 'flow_mod_table_id'). */
1499 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1501 struct nx_flow_mod_table_id *nfmti;
1504 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1505 nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
1506 nfmti->set = flow_mod_table_id;
1510 struct ofputil_flow_mod_flag {
1512 enum ofp_version min_version, max_version;
1513 enum ofputil_flow_mod_flags flag;
1516 static const struct ofputil_flow_mod_flag ofputil_flow_mod_flags[] = {
1517 { OFPFF_SEND_FLOW_REM, OFP10_VERSION, 0, OFPUTIL_FF_SEND_FLOW_REM },
1518 { OFPFF_CHECK_OVERLAP, OFP10_VERSION, 0, OFPUTIL_FF_CHECK_OVERLAP },
1519 { OFPFF10_EMERG, OFP10_VERSION, OFP10_VERSION,
1521 { OFPFF12_RESET_COUNTS, OFP12_VERSION, 0, OFPUTIL_FF_RESET_COUNTS },
1522 { OFPFF13_NO_PKT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_PKT_COUNTS },
1523 { OFPFF13_NO_BYT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_BYT_COUNTS },
1528 ofputil_decode_flow_mod_flags(ovs_be16 raw_flags_,
1529 enum ofp_flow_mod_command command,
1530 enum ofp_version version,
1531 enum ofputil_flow_mod_flags *flagsp)
1533 uint16_t raw_flags = ntohs(raw_flags_);
1534 const struct ofputil_flow_mod_flag *f;
1537 for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1538 if (raw_flags & f->raw_flag
1539 && version >= f->min_version
1540 && (!f->max_version || version <= f->max_version)) {
1541 raw_flags &= ~f->raw_flag;
1546 /* In OF1.0 and OF1.1, "add" always resets counters, and other commands
1549 * In OF1.2 and later, OFPFF12_RESET_COUNTS controls whether each command
1550 * resets counters. */
1551 if ((version == OFP10_VERSION || version == OFP11_VERSION)
1552 && command == OFPFC_ADD) {
1553 *flagsp |= OFPUTIL_FF_RESET_COUNTS;
1556 return raw_flags ? OFPERR_OFPFMFC_BAD_FLAGS : 0;
1560 ofputil_encode_flow_mod_flags(enum ofputil_flow_mod_flags flags,
1561 enum ofp_version version)
1563 const struct ofputil_flow_mod_flag *f;
1567 for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1569 && version >= f->min_version
1570 && (!f->max_version || version <= f->max_version)) {
1571 raw_flags |= f->raw_flag;
1575 return htons(raw_flags);
1578 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1579 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1582 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1583 * The caller must initialize 'ofpacts' and retains ownership of it.
1584 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1586 * Does not validate the flow_mod actions. The caller should do that, with
1587 * ofpacts_check(). */
1589 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1590 const struct ofp_header *oh,
1591 enum ofputil_protocol protocol,
1592 struct ofpbuf *ofpacts,
1593 ofp_port_t max_port, uint8_t max_table)
1600 /* Ignored for non-delete actions */
1601 fm->delete_reason = OFPRR_DELETE;
1603 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1604 raw = ofpraw_pull_assert(&b);
1605 if (raw == OFPRAW_OFPT11_FLOW_MOD) {
1606 /* Standard OpenFlow 1.1+ flow_mod. */
1607 const struct ofp11_flow_mod *ofm;
1609 ofm = ofpbuf_pull(&b, sizeof *ofm);
1611 error = ofputil_pull_ofp11_match(&b, &fm->match, NULL);
1616 /* Translate the message. */
1617 fm->priority = ntohs(ofm->priority);
1618 if (ofm->command == OFPFC_ADD
1619 || (oh->version == OFP11_VERSION
1620 && (ofm->command == OFPFC_MODIFY ||
1621 ofm->command == OFPFC_MODIFY_STRICT)
1622 && ofm->cookie_mask == htonll(0))) {
1623 /* In OpenFlow 1.1 only, a "modify" or "modify-strict" that does
1624 * not match on the cookie is treated as an "add" if there is no
1626 fm->cookie = htonll(0);
1627 fm->cookie_mask = htonll(0);
1628 fm->new_cookie = ofm->cookie;
1630 fm->cookie = ofm->cookie;
1631 fm->cookie_mask = ofm->cookie_mask;
1632 fm->new_cookie = OVS_BE64_MAX;
1634 fm->modify_cookie = false;
1635 fm->command = ofm->command;
1639 * OF1.1 entirely forbids table_id == OFPTT_ALL.
1640 * OF1.2+ allows table_id == OFPTT_ALL only for deletes. */
1641 fm->table_id = ofm->table_id;
1642 if (fm->table_id == OFPTT_ALL
1643 && (oh->version == OFP11_VERSION
1644 || (ofm->command != OFPFC_DELETE &&
1645 ofm->command != OFPFC_DELETE_STRICT))) {
1646 return OFPERR_OFPFMFC_BAD_TABLE_ID;
1649 fm->idle_timeout = ntohs(ofm->idle_timeout);
1650 fm->hard_timeout = ntohs(ofm->hard_timeout);
1651 if (oh->version >= OFP14_VERSION && ofm->command == OFPFC_ADD) {
1652 fm->importance = ntohs(ofm->importance);
1656 fm->buffer_id = ntohl(ofm->buffer_id);
1657 error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
1662 fm->out_group = (ofm->command == OFPFC_DELETE ||
1663 ofm->command == OFPFC_DELETE_STRICT
1664 ? ntohl(ofm->out_group)
1666 raw_flags = ofm->flags;
1670 if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1671 /* Standard OpenFlow 1.0 flow_mod. */
1672 const struct ofp10_flow_mod *ofm;
1674 /* Get the ofp10_flow_mod. */
1675 ofm = ofpbuf_pull(&b, sizeof *ofm);
1677 /* Translate the rule. */
1678 ofputil_match_from_ofp10_match(&ofm->match, &fm->match);
1679 ofputil_normalize_match(&fm->match);
1681 /* OpenFlow 1.0 says that exact-match rules have to have the
1682 * highest possible priority. */
1683 fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL)
1684 ? ntohs(ofm->priority)
1687 /* Translate the message. */
1688 command = ntohs(ofm->command);
1689 fm->cookie = htonll(0);
1690 fm->cookie_mask = htonll(0);
1691 fm->new_cookie = ofm->cookie;
1692 fm->idle_timeout = ntohs(ofm->idle_timeout);
1693 fm->hard_timeout = ntohs(ofm->hard_timeout);
1695 fm->buffer_id = ntohl(ofm->buffer_id);
1696 fm->out_port = u16_to_ofp(ntohs(ofm->out_port));
1697 fm->out_group = OFPG_ANY;
1698 raw_flags = ofm->flags;
1699 } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1700 /* Nicira extended flow_mod. */
1701 const struct nx_flow_mod *nfm;
1703 /* Dissect the message. */
1704 nfm = ofpbuf_pull(&b, sizeof *nfm);
1705 error = nx_pull_match(&b, ntohs(nfm->match_len),
1706 &fm->match, &fm->cookie, &fm->cookie_mask);
1711 /* Translate the message. */
1712 command = ntohs(nfm->command);
1713 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1714 /* Flow additions may only set a new cookie, not match an
1715 * existing cookie. */
1716 return OFPERR_NXBRC_NXM_INVALID;
1718 fm->priority = ntohs(nfm->priority);
1719 fm->new_cookie = nfm->cookie;
1720 fm->idle_timeout = ntohs(nfm->idle_timeout);
1721 fm->hard_timeout = ntohs(nfm->hard_timeout);
1723 fm->buffer_id = ntohl(nfm->buffer_id);
1724 fm->out_port = u16_to_ofp(ntohs(nfm->out_port));
1725 fm->out_group = OFPG_ANY;
1726 raw_flags = nfm->flags;
1731 fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX;
1732 if (protocol & OFPUTIL_P_TID) {
1733 fm->command = command & 0xff;
1734 fm->table_id = command >> 8;
1736 if (command > 0xff) {
1737 VLOG_WARN_RL(&bad_ofmsg_rl, "flow_mod has explicit table_id "
1738 "but flow_mod_table_id extension is not enabled");
1740 fm->command = command;
1741 fm->table_id = 0xff;
1745 if (fm->command > OFPFC_DELETE_STRICT) {
1746 return OFPERR_OFPFMFC_BAD_COMMAND;
1749 error = ofpacts_pull_openflow_instructions(&b, b.size,
1750 oh->version, ofpacts);
1754 fm->ofpacts = ofpacts->data;
1755 fm->ofpacts_len = ofpacts->size;
1757 error = ofputil_decode_flow_mod_flags(raw_flags, fm->command,
1758 oh->version, &fm->flags);
1763 if (fm->flags & OFPUTIL_FF_EMERG) {
1764 /* We do not support the OpenFlow 1.0 emergency flow cache, which
1765 * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
1767 * OpenFlow 1.0 specifies the error code to use when idle_timeout
1768 * or hard_timeout is nonzero. Otherwise, there is no good error
1769 * code, so just state that the flow table is full. */
1770 return (fm->hard_timeout || fm->idle_timeout
1771 ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT
1772 : OFPERR_OFPFMFC_TABLE_FULL);
1775 return ofpacts_check_consistency(fm->ofpacts, fm->ofpacts_len,
1776 &fm->match.flow, max_port,
1777 fm->table_id, max_table, protocol);
1781 ofputil_pull_bands(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
1782 struct ofpbuf *bands)
1784 const struct ofp13_meter_band_header *ombh;
1785 struct ofputil_meter_band *mb;
1788 ombh = ofpbuf_try_pull(msg, len);
1790 return OFPERR_OFPBRC_BAD_LEN;
1793 while (len >= sizeof (struct ofp13_meter_band_drop)) {
1794 size_t ombh_len = ntohs(ombh->len);
1795 /* All supported band types have the same length. */
1796 if (ombh_len != sizeof (struct ofp13_meter_band_drop)) {
1797 return OFPERR_OFPBRC_BAD_LEN;
1799 mb = ofpbuf_put_uninit(bands, sizeof *mb);
1800 mb->type = ntohs(ombh->type);
1801 if (mb->type != OFPMBT13_DROP && mb->type != OFPMBT13_DSCP_REMARK) {
1802 return OFPERR_OFPMMFC_BAD_BAND;
1804 mb->rate = ntohl(ombh->rate);
1805 mb->burst_size = ntohl(ombh->burst_size);
1806 mb->prec_level = (mb->type == OFPMBT13_DSCP_REMARK) ?
1807 ((struct ofp13_meter_band_dscp_remark *)ombh)->prec_level : 0;
1810 ombh = ALIGNED_CAST(struct ofp13_meter_band_header *,
1811 (char *) ombh + ombh_len);
1814 return OFPERR_OFPBRC_BAD_LEN;
1821 ofputil_decode_meter_mod(const struct ofp_header *oh,
1822 struct ofputil_meter_mod *mm,
1823 struct ofpbuf *bands)
1825 const struct ofp13_meter_mod *omm;
1828 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1829 ofpraw_pull_assert(&b);
1830 omm = ofpbuf_pull(&b, sizeof *omm);
1832 /* Translate the message. */
1833 mm->command = ntohs(omm->command);
1834 if (mm->command != OFPMC13_ADD &&
1835 mm->command != OFPMC13_MODIFY &&
1836 mm->command != OFPMC13_DELETE) {
1837 return OFPERR_OFPMMFC_BAD_COMMAND;
1839 mm->meter.meter_id = ntohl(omm->meter_id);
1841 if (mm->command == OFPMC13_DELETE) {
1842 mm->meter.flags = 0;
1843 mm->meter.n_bands = 0;
1844 mm->meter.bands = NULL;
1848 mm->meter.flags = ntohs(omm->flags);
1849 if (mm->meter.flags & OFPMF13_KBPS &&
1850 mm->meter.flags & OFPMF13_PKTPS) {
1851 return OFPERR_OFPMMFC_BAD_FLAGS;
1853 mm->meter.bands = bands->data;
1855 error = ofputil_pull_bands(&b, b.size, &mm->meter.n_bands, bands);
1864 ofputil_decode_meter_request(const struct ofp_header *oh, uint32_t *meter_id)
1866 const struct ofp13_meter_multipart_request *omr = ofpmsg_body(oh);
1867 *meter_id = ntohl(omr->meter_id);
1871 ofputil_encode_meter_request(enum ofp_version ofp_version,
1872 enum ofputil_meter_request_type type,
1880 case OFPUTIL_METER_CONFIG:
1881 raw = OFPRAW_OFPST13_METER_CONFIG_REQUEST;
1883 case OFPUTIL_METER_STATS:
1884 raw = OFPRAW_OFPST13_METER_REQUEST;
1887 case OFPUTIL_METER_FEATURES:
1888 raw = OFPRAW_OFPST13_METER_FEATURES_REQUEST;
1892 msg = ofpraw_alloc(raw, ofp_version, 0);
1894 if (type != OFPUTIL_METER_FEATURES) {
1895 struct ofp13_meter_multipart_request *omr;
1896 omr = ofpbuf_put_zeros(msg, sizeof *omr);
1897 omr->meter_id = htonl(meter_id);
1903 ofputil_put_bands(uint16_t n_bands, const struct ofputil_meter_band *mb,
1908 for (n = 0; n < n_bands; ++n) {
1909 /* Currently all band types have same size. */
1910 struct ofp13_meter_band_dscp_remark *ombh;
1911 size_t ombh_len = sizeof *ombh;
1913 ombh = ofpbuf_put_zeros(msg, ombh_len);
1915 ombh->type = htons(mb->type);
1916 ombh->len = htons(ombh_len);
1917 ombh->rate = htonl(mb->rate);
1918 ombh->burst_size = htonl(mb->burst_size);
1919 ombh->prec_level = mb->prec_level;
1925 /* Encode a meter stat for 'mc' and append it to 'replies'. */
1927 ofputil_append_meter_config(struct ovs_list *replies,
1928 const struct ofputil_meter_config *mc)
1930 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
1931 size_t start_ofs = msg->size;
1932 struct ofp13_meter_config *reply;
1934 ofpbuf_put_uninit(msg, sizeof *reply);
1935 ofputil_put_bands(mc->n_bands, mc->bands, msg);
1937 reply = ofpbuf_at_assert(msg, start_ofs, sizeof *reply);
1938 reply->flags = htons(mc->flags);
1939 reply->meter_id = htonl(mc->meter_id);
1940 reply->length = htons(msg->size - start_ofs);
1942 ofpmp_postappend(replies, start_ofs);
1945 /* Encode a meter stat for 'ms' and append it to 'replies'. */
1947 ofputil_append_meter_stats(struct ovs_list *replies,
1948 const struct ofputil_meter_stats *ms)
1950 struct ofp13_meter_stats *reply;
1954 len = sizeof *reply + ms->n_bands * sizeof(struct ofp13_meter_band_stats);
1955 reply = ofpmp_append(replies, len);
1957 reply->meter_id = htonl(ms->meter_id);
1958 reply->len = htons(len);
1959 memset(reply->pad, 0, sizeof reply->pad);
1960 reply->flow_count = htonl(ms->flow_count);
1961 reply->packet_in_count = htonll(ms->packet_in_count);
1962 reply->byte_in_count = htonll(ms->byte_in_count);
1963 reply->duration_sec = htonl(ms->duration_sec);
1964 reply->duration_nsec = htonl(ms->duration_nsec);
1966 for (n = 0; n < ms->n_bands; ++n) {
1967 const struct ofputil_meter_band_stats *src = &ms->bands[n];
1968 struct ofp13_meter_band_stats *dst = &reply->band_stats[n];
1970 dst->packet_band_count = htonll(src->packet_count);
1971 dst->byte_band_count = htonll(src->byte_count);
1975 /* Converts an OFPMP_METER_CONFIG reply in 'msg' into an abstract
1976 * ofputil_meter_config in 'mc', with mc->bands pointing to bands decoded into
1977 * 'bands'. The caller must have initialized 'bands' and retains ownership of
1978 * it across the call.
1980 * Multiple OFPST13_METER_CONFIG replies can be packed into a single OpenFlow
1981 * message. Calling this function multiple times for a single 'msg' iterates
1982 * through the replies. 'bands' is cleared for each reply.
1984 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1985 * otherwise a positive errno value. */
1987 ofputil_decode_meter_config(struct ofpbuf *msg,
1988 struct ofputil_meter_config *mc,
1989 struct ofpbuf *bands)
1991 const struct ofp13_meter_config *omc;
1994 /* Pull OpenFlow headers for the first call. */
1996 ofpraw_pull_assert(msg);
2003 omc = ofpbuf_try_pull(msg, sizeof *omc);
2005 VLOG_WARN_RL(&bad_ofmsg_rl,
2006 "OFPMP_METER_CONFIG reply has %"PRIu32" leftover bytes at end",
2008 return OFPERR_OFPBRC_BAD_LEN;
2011 ofpbuf_clear(bands);
2012 err = ofputil_pull_bands(msg, ntohs(omc->length) - sizeof *omc,
2013 &mc->n_bands, bands);
2017 mc->meter_id = ntohl(omc->meter_id);
2018 mc->flags = ntohs(omc->flags);
2019 mc->bands = bands->data;
2025 ofputil_pull_band_stats(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
2026 struct ofpbuf *bands)
2028 const struct ofp13_meter_band_stats *ombs;
2029 struct ofputil_meter_band_stats *mbs;
2032 ombs = ofpbuf_try_pull(msg, len);
2034 return OFPERR_OFPBRC_BAD_LEN;
2037 n = len / sizeof *ombs;
2038 if (len != n * sizeof *ombs) {
2039 return OFPERR_OFPBRC_BAD_LEN;
2042 mbs = ofpbuf_put_uninit(bands, len);
2044 for (i = 0; i < n; ++i) {
2045 mbs[i].packet_count = ntohll(ombs[i].packet_band_count);
2046 mbs[i].byte_count = ntohll(ombs[i].byte_band_count);
2052 /* Converts an OFPMP_METER reply in 'msg' into an abstract
2053 * ofputil_meter_stats in 'ms', with ms->bands pointing to band stats
2054 * decoded into 'bands'.
2056 * Multiple OFPMP_METER replies can be packed into a single OpenFlow
2057 * message. Calling this function multiple times for a single 'msg' iterates
2058 * through the replies. 'bands' is cleared for each reply.
2060 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2061 * otherwise a positive errno value. */
2063 ofputil_decode_meter_stats(struct ofpbuf *msg,
2064 struct ofputil_meter_stats *ms,
2065 struct ofpbuf *bands)
2067 const struct ofp13_meter_stats *oms;
2070 /* Pull OpenFlow headers for the first call. */
2072 ofpraw_pull_assert(msg);
2079 oms = ofpbuf_try_pull(msg, sizeof *oms);
2081 VLOG_WARN_RL(&bad_ofmsg_rl,
2082 "OFPMP_METER reply has %"PRIu32" leftover bytes at end",
2084 return OFPERR_OFPBRC_BAD_LEN;
2087 ofpbuf_clear(bands);
2088 err = ofputil_pull_band_stats(msg, ntohs(oms->len) - sizeof *oms,
2089 &ms->n_bands, bands);
2093 ms->meter_id = ntohl(oms->meter_id);
2094 ms->flow_count = ntohl(oms->flow_count);
2095 ms->packet_in_count = ntohll(oms->packet_in_count);
2096 ms->byte_in_count = ntohll(oms->byte_in_count);
2097 ms->duration_sec = ntohl(oms->duration_sec);
2098 ms->duration_nsec = ntohl(oms->duration_nsec);
2099 ms->bands = bands->data;
2105 ofputil_decode_meter_features(const struct ofp_header *oh,
2106 struct ofputil_meter_features *mf)
2108 const struct ofp13_meter_features *omf = ofpmsg_body(oh);
2110 mf->max_meters = ntohl(omf->max_meter);
2111 mf->band_types = ntohl(omf->band_types);
2112 mf->capabilities = ntohl(omf->capabilities);
2113 mf->max_bands = omf->max_bands;
2114 mf->max_color = omf->max_color;
2118 ofputil_encode_meter_features_reply(const struct ofputil_meter_features *mf,
2119 const struct ofp_header *request)
2121 struct ofpbuf *reply;
2122 struct ofp13_meter_features *omf;
2124 reply = ofpraw_alloc_stats_reply(request, 0);
2125 omf = ofpbuf_put_zeros(reply, sizeof *omf);
2127 omf->max_meter = htonl(mf->max_meters);
2128 omf->band_types = htonl(mf->band_types);
2129 omf->capabilities = htonl(mf->capabilities);
2130 omf->max_bands = mf->max_bands;
2131 omf->max_color = mf->max_color;
2137 ofputil_encode_meter_mod(enum ofp_version ofp_version,
2138 const struct ofputil_meter_mod *mm)
2142 struct ofp13_meter_mod *omm;
2144 msg = ofpraw_alloc(OFPRAW_OFPT13_METER_MOD, ofp_version,
2145 NXM_TYPICAL_LEN + mm->meter.n_bands * 16);
2146 omm = ofpbuf_put_zeros(msg, sizeof *omm);
2147 omm->command = htons(mm->command);
2148 if (mm->command != OFPMC13_DELETE) {
2149 omm->flags = htons(mm->meter.flags);
2151 omm->meter_id = htonl(mm->meter.meter_id);
2153 ofputil_put_bands(mm->meter.n_bands, mm->meter.bands, msg);
2155 ofpmsg_update_length(msg);
2160 ofputil_tid_command(const struct ofputil_flow_mod *fm,
2161 enum ofputil_protocol protocol)
2163 return htons(protocol & OFPUTIL_P_TID
2164 ? (fm->command & 0xff) | (fm->table_id << 8)
2168 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
2169 * 'protocol' and returns the message. */
2171 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
2172 enum ofputil_protocol protocol)
2174 enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
2175 ovs_be16 raw_flags = ofputil_encode_flow_mod_flags(fm->flags, version);
2179 case OFPUTIL_P_OF11_STD:
2180 case OFPUTIL_P_OF12_OXM:
2181 case OFPUTIL_P_OF13_OXM:
2182 case OFPUTIL_P_OF14_OXM:
2183 case OFPUTIL_P_OF15_OXM: {
2184 struct ofp11_flow_mod *ofm;
2187 tailroom = ofputil_match_typical_len(protocol) + fm->ofpacts_len;
2188 msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, version, tailroom);
2189 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
2190 if ((protocol == OFPUTIL_P_OF11_STD
2191 && (fm->command == OFPFC_MODIFY ||
2192 fm->command == OFPFC_MODIFY_STRICT)
2193 && fm->cookie_mask == htonll(0))
2194 || fm->command == OFPFC_ADD) {
2195 ofm->cookie = fm->new_cookie;
2197 ofm->cookie = fm->cookie & fm->cookie_mask;
2199 ofm->cookie_mask = fm->cookie_mask;
2200 if (fm->table_id != OFPTT_ALL
2201 || (protocol != OFPUTIL_P_OF11_STD
2202 && (fm->command == OFPFC_DELETE ||
2203 fm->command == OFPFC_DELETE_STRICT))) {
2204 ofm->table_id = fm->table_id;
2208 ofm->command = fm->command;
2209 ofm->idle_timeout = htons(fm->idle_timeout);
2210 ofm->hard_timeout = htons(fm->hard_timeout);
2211 ofm->priority = htons(fm->priority);
2212 ofm->buffer_id = htonl(fm->buffer_id);
2213 ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
2214 ofm->out_group = htonl(fm->out_group);
2215 ofm->flags = raw_flags;
2216 if (version >= OFP14_VERSION && fm->command == OFPFC_ADD) {
2217 ofm->importance = htons(fm->importance);
2219 ofm->importance = 0;
2221 ofputil_put_ofp11_match(msg, &fm->match, protocol);
2222 ofpacts_put_openflow_instructions(fm->ofpacts, fm->ofpacts_len, msg,
2227 case OFPUTIL_P_OF10_STD:
2228 case OFPUTIL_P_OF10_STD_TID: {
2229 struct ofp10_flow_mod *ofm;
2231 msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
2233 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
2234 ofputil_match_to_ofp10_match(&fm->match, &ofm->match);
2235 ofm->cookie = fm->new_cookie;
2236 ofm->command = ofputil_tid_command(fm, protocol);
2237 ofm->idle_timeout = htons(fm->idle_timeout);
2238 ofm->hard_timeout = htons(fm->hard_timeout);
2239 ofm->priority = htons(fm->priority);
2240 ofm->buffer_id = htonl(fm->buffer_id);
2241 ofm->out_port = htons(ofp_to_u16(fm->out_port));
2242 ofm->flags = raw_flags;
2243 ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2248 case OFPUTIL_P_OF10_NXM:
2249 case OFPUTIL_P_OF10_NXM_TID: {
2250 struct nx_flow_mod *nfm;
2253 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
2254 NXM_TYPICAL_LEN + fm->ofpacts_len);
2255 nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
2256 nfm->command = ofputil_tid_command(fm, protocol);
2257 nfm->cookie = fm->new_cookie;
2258 match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask);
2260 nfm->idle_timeout = htons(fm->idle_timeout);
2261 nfm->hard_timeout = htons(fm->hard_timeout);
2262 nfm->priority = htons(fm->priority);
2263 nfm->buffer_id = htonl(fm->buffer_id);
2264 nfm->out_port = htons(ofp_to_u16(fm->out_port));
2265 nfm->flags = raw_flags;
2266 nfm->match_len = htons(match_len);
2267 ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2276 ofpmsg_update_length(msg);
2281 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr,
2282 const struct ofp10_flow_stats_request *ofsr,
2285 fsr->aggregate = aggregate;
2286 ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match);
2287 fsr->out_port = u16_to_ofp(ntohs(ofsr->out_port));
2288 fsr->out_group = OFPG_ANY;
2289 fsr->table_id = ofsr->table_id;
2290 fsr->cookie = fsr->cookie_mask = htonll(0);
2296 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr,
2297 struct ofpbuf *b, bool aggregate)
2299 const struct ofp11_flow_stats_request *ofsr;
2302 ofsr = ofpbuf_pull(b, sizeof *ofsr);
2303 fsr->aggregate = aggregate;
2304 fsr->table_id = ofsr->table_id;
2305 error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port);
2309 fsr->out_group = ntohl(ofsr->out_group);
2310 fsr->cookie = ofsr->cookie;
2311 fsr->cookie_mask = ofsr->cookie_mask;
2312 error = ofputil_pull_ofp11_match(b, &fsr->match, NULL);
2321 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
2322 struct ofpbuf *b, bool aggregate)
2324 const struct nx_flow_stats_request *nfsr;
2327 nfsr = ofpbuf_pull(b, sizeof *nfsr);
2328 error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match,
2329 &fsr->cookie, &fsr->cookie_mask);
2334 return OFPERR_OFPBRC_BAD_LEN;
2337 fsr->aggregate = aggregate;
2338 fsr->out_port = u16_to_ofp(ntohs(nfsr->out_port));
2339 fsr->out_group = OFPG_ANY;
2340 fsr->table_id = nfsr->table_id;
2345 /* Constructs and returns an OFPT_QUEUE_GET_CONFIG request for the specified
2346 * 'port', suitable for OpenFlow version 'version'. */
2348 ofputil_encode_queue_get_config_request(enum ofp_version version,
2351 struct ofpbuf *request;
2353 if (version == OFP10_VERSION) {
2354 struct ofp10_queue_get_config_request *qgcr10;
2356 request = ofpraw_alloc(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST,
2358 qgcr10 = ofpbuf_put_zeros(request, sizeof *qgcr10);
2359 qgcr10->port = htons(ofp_to_u16(port));
2361 struct ofp11_queue_get_config_request *qgcr11;
2363 request = ofpraw_alloc(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST,
2365 qgcr11 = ofpbuf_put_zeros(request, sizeof *qgcr11);
2366 qgcr11->port = ofputil_port_to_ofp11(port);
2372 /* Parses OFPT_QUEUE_GET_CONFIG request 'oh', storing the port specified by the
2373 * request into '*port'. Returns 0 if successful, otherwise an OpenFlow error
2376 ofputil_decode_queue_get_config_request(const struct ofp_header *oh,
2379 const struct ofp10_queue_get_config_request *qgcr10;
2380 const struct ofp11_queue_get_config_request *qgcr11;
2384 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2385 raw = ofpraw_pull_assert(&b);
2387 switch ((int) raw) {
2388 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2390 *port = u16_to_ofp(ntohs(qgcr10->port));
2393 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2395 enum ofperr error = ofputil_port_from_ofp11(qgcr11->port, port);
2396 if (error || *port == OFPP_ANY) {
2405 return (ofp_to_u16(*port) < ofp_to_u16(OFPP_MAX)
2407 : OFPERR_OFPQOFC_BAD_PORT);
2410 /* Constructs and returns the beginning of a reply to
2411 * OFPT_QUEUE_GET_CONFIG_REQUEST 'oh'. The caller may append information about
2412 * individual queues with ofputil_append_queue_get_config_reply(). */
2414 ofputil_encode_queue_get_config_reply(const struct ofp_header *oh)
2416 struct ofp10_queue_get_config_reply *qgcr10;
2417 struct ofp11_queue_get_config_reply *qgcr11;
2418 struct ofpbuf *reply;
2424 error = ofputil_decode_queue_get_config_request(oh, &port);
2427 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2428 raw = ofpraw_pull_assert(&b);
2430 switch ((int) raw) {
2431 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2432 reply = ofpraw_alloc_reply(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY,
2434 qgcr10 = ofpbuf_put_zeros(reply, sizeof *qgcr10);
2435 qgcr10->port = htons(ofp_to_u16(port));
2438 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2439 reply = ofpraw_alloc_reply(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY,
2441 qgcr11 = ofpbuf_put_zeros(reply, sizeof *qgcr11);
2442 qgcr11->port = ofputil_port_to_ofp11(port);
2453 put_ofp10_queue_rate(struct ofpbuf *reply,
2454 enum ofp10_queue_properties property, uint16_t rate)
2456 if (rate != UINT16_MAX) {
2457 struct ofp10_queue_prop_rate *oqpr;
2459 oqpr = ofpbuf_put_zeros(reply, sizeof *oqpr);
2460 oqpr->prop_header.property = htons(property);
2461 oqpr->prop_header.len = htons(sizeof *oqpr);
2462 oqpr->rate = htons(rate);
2466 /* Appends a queue description for 'queue_id' to the
2467 * OFPT_QUEUE_GET_CONFIG_REPLY already in 'oh'. */
2469 ofputil_append_queue_get_config_reply(struct ofpbuf *reply,
2470 const struct ofputil_queue_config *oqc)
2472 const struct ofp_header *oh = reply->data;
2473 size_t start_ofs, len_ofs;
2476 start_ofs = reply->size;
2477 if (oh->version < OFP12_VERSION) {
2478 struct ofp10_packet_queue *opq10;
2480 opq10 = ofpbuf_put_zeros(reply, sizeof *opq10);
2481 opq10->queue_id = htonl(oqc->queue_id);
2482 len_ofs = (char *) &opq10->len - (char *) reply->data;
2484 struct ofp12_packet_queue *opq12;
2486 opq12 = ofpbuf_put_zeros(reply, sizeof *opq12);
2487 opq12->port = ofputil_port_to_ofp11(oqc->port);
2488 opq12->queue_id = htonl(oqc->queue_id);
2489 len_ofs = (char *) &opq12->len - (char *) reply->data;
2492 put_ofp10_queue_rate(reply, OFPQT10_MIN_RATE, oqc->min_rate);
2493 put_ofp10_queue_rate(reply, OFPQT11_MAX_RATE, oqc->max_rate);
2495 len = ofpbuf_at(reply, len_ofs, sizeof *len);
2496 *len = htons(reply->size - start_ofs);
2499 /* Decodes the initial part of an OFPT_QUEUE_GET_CONFIG_REPLY from 'reply' and
2500 * stores in '*port' the port that the reply is about. The caller may call
2501 * ofputil_pull_queue_get_config_reply() to obtain information about individual
2502 * queues included in the reply. Returns 0 if successful, otherwise an
2505 ofputil_decode_queue_get_config_reply(struct ofpbuf *reply, ofp_port_t *port)
2507 const struct ofp10_queue_get_config_reply *qgcr10;
2508 const struct ofp11_queue_get_config_reply *qgcr11;
2511 raw = ofpraw_pull_assert(reply);
2512 switch ((int) raw) {
2513 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY:
2514 qgcr10 = ofpbuf_pull(reply, sizeof *qgcr10);
2515 *port = u16_to_ofp(ntohs(qgcr10->port));
2518 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY:
2519 qgcr11 = ofpbuf_pull(reply, sizeof *qgcr11);
2520 return ofputil_port_from_ofp11(qgcr11->port, port);
2527 parse_ofp10_queue_rate(const struct ofp10_queue_prop_header *hdr,
2530 const struct ofp10_queue_prop_rate *oqpr;
2532 if (hdr->len == htons(sizeof *oqpr)) {
2533 oqpr = (const struct ofp10_queue_prop_rate *) hdr;
2534 *rate = ntohs(oqpr->rate);
2537 return OFPERR_OFPBRC_BAD_LEN;
2541 /* Decodes information about a queue from the OFPT_QUEUE_GET_CONFIG_REPLY in
2542 * 'reply' and stores it in '*queue'. ofputil_decode_queue_get_config_reply()
2543 * must already have pulled off the main header.
2545 * This function returns EOF if the last queue has already been decoded, 0 if a
2546 * queue was successfully decoded into '*queue', or an ofperr if there was a
2547 * problem decoding 'reply'. */
2549 ofputil_pull_queue_get_config_reply(struct ofpbuf *reply,
2550 struct ofputil_queue_config *queue)
2552 const struct ofp_header *oh;
2553 unsigned int opq_len;
2560 queue->min_rate = UINT16_MAX;
2561 queue->max_rate = UINT16_MAX;
2564 if (oh->version < OFP12_VERSION) {
2565 const struct ofp10_packet_queue *opq10;
2567 opq10 = ofpbuf_try_pull(reply, sizeof *opq10);
2569 return OFPERR_OFPBRC_BAD_LEN;
2571 queue->queue_id = ntohl(opq10->queue_id);
2572 len = ntohs(opq10->len);
2573 opq_len = sizeof *opq10;
2575 const struct ofp12_packet_queue *opq12;
2577 opq12 = ofpbuf_try_pull(reply, sizeof *opq12);
2579 return OFPERR_OFPBRC_BAD_LEN;
2581 queue->queue_id = ntohl(opq12->queue_id);
2582 len = ntohs(opq12->len);
2583 opq_len = sizeof *opq12;
2586 if (len < opq_len || len > reply->size + opq_len || len % 8) {
2587 return OFPERR_OFPBRC_BAD_LEN;
2592 const struct ofp10_queue_prop_header *hdr;
2593 unsigned int property;
2594 unsigned int prop_len;
2595 enum ofperr error = 0;
2597 hdr = ofpbuf_at_assert(reply, 0, sizeof *hdr);
2598 prop_len = ntohs(hdr->len);
2599 if (prop_len < sizeof *hdr || prop_len > reply->size || prop_len % 8) {
2600 return OFPERR_OFPBRC_BAD_LEN;
2603 property = ntohs(hdr->property);
2605 case OFPQT10_MIN_RATE:
2606 error = parse_ofp10_queue_rate(hdr, &queue->min_rate);
2609 case OFPQT11_MAX_RATE:
2610 error = parse_ofp10_queue_rate(hdr, &queue->max_rate);
2614 VLOG_INFO_RL(&bad_ofmsg_rl, "unknown queue property %u", property);
2621 ofpbuf_pull(reply, prop_len);
2627 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
2628 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
2629 * successful, otherwise an OpenFlow error code. */
2631 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
2632 const struct ofp_header *oh)
2637 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2638 raw = ofpraw_pull_assert(&b);
2639 switch ((int) raw) {
2640 case OFPRAW_OFPST10_FLOW_REQUEST:
2641 return ofputil_decode_ofpst10_flow_request(fsr, b.data, false);
2643 case OFPRAW_OFPST10_AGGREGATE_REQUEST:
2644 return ofputil_decode_ofpst10_flow_request(fsr, b.data, true);
2646 case OFPRAW_OFPST11_FLOW_REQUEST:
2647 return ofputil_decode_ofpst11_flow_request(fsr, &b, false);
2649 case OFPRAW_OFPST11_AGGREGATE_REQUEST:
2650 return ofputil_decode_ofpst11_flow_request(fsr, &b, true);
2652 case OFPRAW_NXST_FLOW_REQUEST:
2653 return ofputil_decode_nxst_flow_request(fsr, &b, false);
2655 case OFPRAW_NXST_AGGREGATE_REQUEST:
2656 return ofputil_decode_nxst_flow_request(fsr, &b, true);
2659 /* Hey, the caller lied. */
2664 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
2665 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
2666 * 'protocol', and returns the message. */
2668 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
2669 enum ofputil_protocol protocol)
2675 case OFPUTIL_P_OF11_STD:
2676 case OFPUTIL_P_OF12_OXM:
2677 case OFPUTIL_P_OF13_OXM:
2678 case OFPUTIL_P_OF14_OXM:
2679 case OFPUTIL_P_OF15_OXM: {
2680 struct ofp11_flow_stats_request *ofsr;
2682 raw = (fsr->aggregate
2683 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
2684 : OFPRAW_OFPST11_FLOW_REQUEST);
2685 msg = ofpraw_alloc(raw, ofputil_protocol_to_ofp_version(protocol),
2686 ofputil_match_typical_len(protocol));
2687 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2688 ofsr->table_id = fsr->table_id;
2689 ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port);
2690 ofsr->out_group = htonl(fsr->out_group);
2691 ofsr->cookie = fsr->cookie;
2692 ofsr->cookie_mask = fsr->cookie_mask;
2693 ofputil_put_ofp11_match(msg, &fsr->match, protocol);
2697 case OFPUTIL_P_OF10_STD:
2698 case OFPUTIL_P_OF10_STD_TID: {
2699 struct ofp10_flow_stats_request *ofsr;
2701 raw = (fsr->aggregate
2702 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
2703 : OFPRAW_OFPST10_FLOW_REQUEST);
2704 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
2705 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2706 ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match);
2707 ofsr->table_id = fsr->table_id;
2708 ofsr->out_port = htons(ofp_to_u16(fsr->out_port));
2712 case OFPUTIL_P_OF10_NXM:
2713 case OFPUTIL_P_OF10_NXM_TID: {
2714 struct nx_flow_stats_request *nfsr;
2717 raw = (fsr->aggregate
2718 ? OFPRAW_NXST_AGGREGATE_REQUEST
2719 : OFPRAW_NXST_FLOW_REQUEST);
2720 msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN);
2721 ofpbuf_put_zeros(msg, sizeof *nfsr);
2722 match_len = nx_put_match(msg, &fsr->match,
2723 fsr->cookie, fsr->cookie_mask);
2726 nfsr->out_port = htons(ofp_to_u16(fsr->out_port));
2727 nfsr->match_len = htons(match_len);
2728 nfsr->table_id = fsr->table_id;
2739 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
2740 * ofputil_flow_stats in 'fs'.
2742 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
2743 * OpenFlow message. Calling this function multiple times for a single 'msg'
2744 * iterates through the replies. The caller must initially leave 'msg''s layer
2745 * pointers null and not modify them between calls.
2747 * Most switches don't send the values needed to populate fs->idle_age and
2748 * fs->hard_age, so those members will usually be set to 0. If the switch from
2749 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
2750 * 'flow_age_extension' as true so that the contents of 'msg' determine the
2751 * 'idle_age' and 'hard_age' members in 'fs'.
2753 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
2754 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
2755 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
2757 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2758 * otherwise a positive errno value. */
2760 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
2762 bool flow_age_extension,
2763 struct ofpbuf *ofpacts)
2765 const struct ofp_header *oh;
2766 size_t instructions_len;
2770 error = (msg->header ? ofpraw_decode(&raw, msg->header)
2771 : ofpraw_pull(&raw, msg));
2779 } else if (raw == OFPRAW_OFPST11_FLOW_REPLY
2780 || raw == OFPRAW_OFPST13_FLOW_REPLY) {
2781 const struct ofp11_flow_stats *ofs;
2783 uint16_t padded_match_len;
2785 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2787 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
2788 "bytes at end", msg->size);
2792 length = ntohs(ofs->length);
2793 if (length < sizeof *ofs) {
2794 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2795 "length %"PRIuSIZE, length);
2799 if (ofputil_pull_ofp11_match(msg, &fs->match, &padded_match_len)) {
2800 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match");
2803 instructions_len = length - sizeof *ofs - padded_match_len;
2805 fs->priority = ntohs(ofs->priority);
2806 fs->table_id = ofs->table_id;
2807 fs->duration_sec = ntohl(ofs->duration_sec);
2808 fs->duration_nsec = ntohl(ofs->duration_nsec);
2809 fs->idle_timeout = ntohs(ofs->idle_timeout);
2810 fs->hard_timeout = ntohs(ofs->hard_timeout);
2811 if (oh->version >= OFP14_VERSION) {
2812 fs->importance = ntohs(ofs->importance);
2816 if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
2817 error = ofputil_decode_flow_mod_flags(ofs->flags, -1, oh->version,
2827 fs->cookie = ofs->cookie;
2828 fs->packet_count = ntohll(ofs->packet_count);
2829 fs->byte_count = ntohll(ofs->byte_count);
2830 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
2831 const struct ofp10_flow_stats *ofs;
2834 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2836 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
2837 "bytes at end", msg->size);
2841 length = ntohs(ofs->length);
2842 if (length < sizeof *ofs) {
2843 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2844 "length %"PRIuSIZE, length);
2847 instructions_len = length - sizeof *ofs;
2849 fs->cookie = get_32aligned_be64(&ofs->cookie);
2850 ofputil_match_from_ofp10_match(&ofs->match, &fs->match);
2851 fs->priority = ntohs(ofs->priority);
2852 fs->table_id = ofs->table_id;
2853 fs->duration_sec = ntohl(ofs->duration_sec);
2854 fs->duration_nsec = ntohl(ofs->duration_nsec);
2855 fs->idle_timeout = ntohs(ofs->idle_timeout);
2856 fs->hard_timeout = ntohs(ofs->hard_timeout);
2860 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
2861 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
2863 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
2864 const struct nx_flow_stats *nfs;
2865 size_t match_len, length;
2867 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
2869 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %"PRIu32" leftover "
2870 "bytes at end", msg->size);
2874 length = ntohs(nfs->length);
2875 match_len = ntohs(nfs->match_len);
2876 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
2877 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%"PRIuSIZE" "
2878 "claims invalid length %"PRIuSIZE, match_len, length);
2881 if (nx_pull_match(msg, match_len, &fs->match, NULL, NULL)) {
2884 instructions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
2886 fs->cookie = nfs->cookie;
2887 fs->table_id = nfs->table_id;
2888 fs->duration_sec = ntohl(nfs->duration_sec);
2889 fs->duration_nsec = ntohl(nfs->duration_nsec);
2890 fs->priority = ntohs(nfs->priority);
2891 fs->idle_timeout = ntohs(nfs->idle_timeout);
2892 fs->hard_timeout = ntohs(nfs->hard_timeout);
2896 if (flow_age_extension) {
2897 if (nfs->idle_age) {
2898 fs->idle_age = ntohs(nfs->idle_age) - 1;
2900 if (nfs->hard_age) {
2901 fs->hard_age = ntohs(nfs->hard_age) - 1;
2904 fs->packet_count = ntohll(nfs->packet_count);
2905 fs->byte_count = ntohll(nfs->byte_count);
2911 if (ofpacts_pull_openflow_instructions(msg, instructions_len, oh->version,
2913 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions");
2916 fs->ofpacts = ofpacts->data;
2917 fs->ofpacts_len = ofpacts->size;
2922 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
2924 * We use this in situations where OVS internally uses UINT64_MAX to mean
2925 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
2927 unknown_to_zero(uint64_t count)
2929 return count != UINT64_MAX ? count : 0;
2932 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
2933 * those already present in the list of ofpbufs in 'replies'. 'replies' should
2934 * have been initialized with ofpmp_init(). */
2936 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
2937 struct ovs_list *replies)
2939 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
2940 size_t start_ofs = reply->size;
2941 enum ofp_version version = ofpmp_version(replies);
2942 enum ofpraw raw = ofpmp_decode_raw(replies);
2944 if (raw == OFPRAW_OFPST11_FLOW_REPLY || raw == OFPRAW_OFPST13_FLOW_REPLY) {
2945 struct ofp11_flow_stats *ofs;
2947 ofpbuf_put_uninit(reply, sizeof *ofs);
2948 oxm_put_match(reply, &fs->match, version);
2949 ofpacts_put_openflow_instructions(fs->ofpacts, fs->ofpacts_len, reply,
2952 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
2953 ofs->length = htons(reply->size - start_ofs);
2954 ofs->table_id = fs->table_id;
2956 ofs->duration_sec = htonl(fs->duration_sec);
2957 ofs->duration_nsec = htonl(fs->duration_nsec);
2958 ofs->priority = htons(fs->priority);
2959 ofs->idle_timeout = htons(fs->idle_timeout);
2960 ofs->hard_timeout = htons(fs->hard_timeout);
2961 if (version >= OFP14_VERSION) {
2962 ofs->importance = htons(fs->importance);
2964 ofs->importance = 0;
2966 if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
2967 ofs->flags = ofputil_encode_flow_mod_flags(fs->flags, version);
2971 memset(ofs->pad2, 0, sizeof ofs->pad2);
2972 ofs->cookie = fs->cookie;
2973 ofs->packet_count = htonll(unknown_to_zero(fs->packet_count));
2974 ofs->byte_count = htonll(unknown_to_zero(fs->byte_count));
2975 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
2976 struct ofp10_flow_stats *ofs;
2978 ofpbuf_put_uninit(reply, sizeof *ofs);
2979 ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
2981 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
2982 ofs->length = htons(reply->size - start_ofs);
2983 ofs->table_id = fs->table_id;
2985 ofputil_match_to_ofp10_match(&fs->match, &ofs->match);
2986 ofs->duration_sec = htonl(fs->duration_sec);
2987 ofs->duration_nsec = htonl(fs->duration_nsec);
2988 ofs->priority = htons(fs->priority);
2989 ofs->idle_timeout = htons(fs->idle_timeout);
2990 ofs->hard_timeout = htons(fs->hard_timeout);
2991 memset(ofs->pad2, 0, sizeof ofs->pad2);
2992 put_32aligned_be64(&ofs->cookie, fs->cookie);
2993 put_32aligned_be64(&ofs->packet_count,
2994 htonll(unknown_to_zero(fs->packet_count)));
2995 put_32aligned_be64(&ofs->byte_count,
2996 htonll(unknown_to_zero(fs->byte_count)));
2997 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
2998 struct nx_flow_stats *nfs;
3001 ofpbuf_put_uninit(reply, sizeof *nfs);
3002 match_len = nx_put_match(reply, &fs->match, 0, 0);
3003 ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
3005 nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
3006 nfs->length = htons(reply->size - start_ofs);
3007 nfs->table_id = fs->table_id;
3009 nfs->duration_sec = htonl(fs->duration_sec);
3010 nfs->duration_nsec = htonl(fs->duration_nsec);
3011 nfs->priority = htons(fs->priority);
3012 nfs->idle_timeout = htons(fs->idle_timeout);
3013 nfs->hard_timeout = htons(fs->hard_timeout);
3014 nfs->idle_age = htons(fs->idle_age < 0 ? 0
3015 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
3017 nfs->hard_age = htons(fs->hard_age < 0 ? 0
3018 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
3020 nfs->match_len = htons(match_len);
3021 nfs->cookie = fs->cookie;
3022 nfs->packet_count = htonll(fs->packet_count);
3023 nfs->byte_count = htonll(fs->byte_count);
3028 ofpmp_postappend(replies, start_ofs);
3031 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
3032 * NXST_AGGREGATE reply matching 'request', and returns the message. */
3034 ofputil_encode_aggregate_stats_reply(
3035 const struct ofputil_aggregate_stats *stats,
3036 const struct ofp_header *request)
3038 struct ofp_aggregate_stats_reply *asr;
3039 uint64_t packet_count;
3040 uint64_t byte_count;
3044 ofpraw_decode(&raw, request);
3045 if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) {
3046 packet_count = unknown_to_zero(stats->packet_count);
3047 byte_count = unknown_to_zero(stats->byte_count);
3049 packet_count = stats->packet_count;
3050 byte_count = stats->byte_count;
3053 msg = ofpraw_alloc_stats_reply(request, 0);
3054 asr = ofpbuf_put_zeros(msg, sizeof *asr);
3055 put_32aligned_be64(&asr->packet_count, htonll(packet_count));
3056 put_32aligned_be64(&asr->byte_count, htonll(byte_count));
3057 asr->flow_count = htonl(stats->flow_count);
3063 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
3064 const struct ofp_header *reply)
3066 struct ofp_aggregate_stats_reply *asr;
3069 ofpbuf_use_const(&msg, reply, ntohs(reply->length));
3070 ofpraw_pull_assert(&msg);
3073 stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
3074 stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
3075 stats->flow_count = ntohl(asr->flow_count);
3080 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
3081 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
3082 * an OpenFlow error code. */
3084 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
3085 const struct ofp_header *oh)
3090 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3091 raw = ofpraw_pull_assert(&b);
3092 if (raw == OFPRAW_OFPT11_FLOW_REMOVED) {
3093 const struct ofp12_flow_removed *ofr;
3096 ofr = ofpbuf_pull(&b, sizeof *ofr);
3098 error = ofputil_pull_ofp11_match(&b, &fr->match, NULL);
3103 fr->priority = ntohs(ofr->priority);
3104 fr->cookie = ofr->cookie;
3105 fr->reason = ofr->reason;
3106 fr->table_id = ofr->table_id;
3107 fr->duration_sec = ntohl(ofr->duration_sec);
3108 fr->duration_nsec = ntohl(ofr->duration_nsec);
3109 fr->idle_timeout = ntohs(ofr->idle_timeout);
3110 fr->hard_timeout = ntohs(ofr->hard_timeout);
3111 fr->packet_count = ntohll(ofr->packet_count);
3112 fr->byte_count = ntohll(ofr->byte_count);
3113 } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
3114 const struct ofp10_flow_removed *ofr;
3116 ofr = ofpbuf_pull(&b, sizeof *ofr);
3118 ofputil_match_from_ofp10_match(&ofr->match, &fr->match);
3119 fr->priority = ntohs(ofr->priority);
3120 fr->cookie = ofr->cookie;
3121 fr->reason = ofr->reason;
3123 fr->duration_sec = ntohl(ofr->duration_sec);
3124 fr->duration_nsec = ntohl(ofr->duration_nsec);
3125 fr->idle_timeout = ntohs(ofr->idle_timeout);
3126 fr->hard_timeout = 0;
3127 fr->packet_count = ntohll(ofr->packet_count);
3128 fr->byte_count = ntohll(ofr->byte_count);
3129 } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
3130 struct nx_flow_removed *nfr;
3133 nfr = ofpbuf_pull(&b, sizeof *nfr);
3134 error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match,
3140 return OFPERR_OFPBRC_BAD_LEN;
3143 fr->priority = ntohs(nfr->priority);
3144 fr->cookie = nfr->cookie;
3145 fr->reason = nfr->reason;
3146 fr->table_id = nfr->table_id ? nfr->table_id - 1 : 255;
3147 fr->duration_sec = ntohl(nfr->duration_sec);
3148 fr->duration_nsec = ntohl(nfr->duration_nsec);
3149 fr->idle_timeout = ntohs(nfr->idle_timeout);
3150 fr->hard_timeout = 0;
3151 fr->packet_count = ntohll(nfr->packet_count);
3152 fr->byte_count = ntohll(nfr->byte_count);
3160 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
3161 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
3164 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
3165 enum ofputil_protocol protocol)
3168 enum ofp_flow_removed_reason reason = fr->reason;
3170 if (reason == OFPRR_METER_DELETE && !(protocol & OFPUTIL_P_OF14_UP)) {
3171 reason = OFPRR_DELETE;
3175 case OFPUTIL_P_OF11_STD:
3176 case OFPUTIL_P_OF12_OXM:
3177 case OFPUTIL_P_OF13_OXM:
3178 case OFPUTIL_P_OF14_OXM:
3179 case OFPUTIL_P_OF15_OXM: {
3180 struct ofp12_flow_removed *ofr;
3182 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED,
3183 ofputil_protocol_to_ofp_version(protocol),
3185 ofputil_match_typical_len(protocol));
3186 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3187 ofr->cookie = fr->cookie;
3188 ofr->priority = htons(fr->priority);
3189 ofr->reason = reason;
3190 ofr->table_id = fr->table_id;
3191 ofr->duration_sec = htonl(fr->duration_sec);
3192 ofr->duration_nsec = htonl(fr->duration_nsec);
3193 ofr->idle_timeout = htons(fr->idle_timeout);
3194 ofr->hard_timeout = htons(fr->hard_timeout);
3195 ofr->packet_count = htonll(fr->packet_count);
3196 ofr->byte_count = htonll(fr->byte_count);
3197 ofputil_put_ofp11_match(msg, &fr->match, protocol);
3201 case OFPUTIL_P_OF10_STD:
3202 case OFPUTIL_P_OF10_STD_TID: {
3203 struct ofp10_flow_removed *ofr;
3205 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
3207 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3208 ofputil_match_to_ofp10_match(&fr->match, &ofr->match);
3209 ofr->cookie = fr->cookie;
3210 ofr->priority = htons(fr->priority);
3211 ofr->reason = reason;
3212 ofr->duration_sec = htonl(fr->duration_sec);
3213 ofr->duration_nsec = htonl(fr->duration_nsec);
3214 ofr->idle_timeout = htons(fr->idle_timeout);
3215 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
3216 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
3220 case OFPUTIL_P_OF10_NXM:
3221 case OFPUTIL_P_OF10_NXM_TID: {
3222 struct nx_flow_removed *nfr;
3225 msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
3226 htonl(0), NXM_TYPICAL_LEN);
3227 ofpbuf_put_zeros(msg, sizeof *nfr);
3228 match_len = nx_put_match(msg, &fr->match, 0, 0);
3231 nfr->cookie = fr->cookie;
3232 nfr->priority = htons(fr->priority);
3233 nfr->reason = reason;
3234 nfr->table_id = fr->table_id + 1;
3235 nfr->duration_sec = htonl(fr->duration_sec);
3236 nfr->duration_nsec = htonl(fr->duration_nsec);
3237 nfr->idle_timeout = htons(fr->idle_timeout);
3238 nfr->match_len = htons(match_len);
3239 nfr->packet_count = htonll(fr->packet_count);
3240 nfr->byte_count = htonll(fr->byte_count);
3252 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
3253 const struct ofp_header *oh)
3258 memset(pin, 0, sizeof *pin);
3259 pin->cookie = OVS_BE64_MAX;
3261 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3262 raw = ofpraw_pull_assert(&b);
3263 if (raw == OFPRAW_OFPT13_PACKET_IN || raw == OFPRAW_OFPT12_PACKET_IN) {
3264 const struct ofp13_packet_in *opi;
3266 size_t packet_in_size;
3268 if (raw == OFPRAW_OFPT12_PACKET_IN) {
3269 packet_in_size = sizeof (struct ofp12_packet_in);
3271 packet_in_size = sizeof (struct ofp13_packet_in);
3274 opi = ofpbuf_pull(&b, packet_in_size);
3275 error = oxm_pull_match_loose(&b, &pin->flow_metadata);
3280 if (!ofpbuf_try_pull(&b, 2)) {
3281 return OFPERR_OFPBRC_BAD_LEN;
3284 pin->reason = opi->pi.reason;
3285 pin->table_id = opi->pi.table_id;
3286 pin->buffer_id = ntohl(opi->pi.buffer_id);
3287 pin->total_len = ntohs(opi->pi.total_len);
3289 if (raw == OFPRAW_OFPT13_PACKET_IN) {
3290 pin->cookie = opi->cookie;
3293 pin->packet = b.data;
3294 pin->packet_len = b.size;
3295 } else if (raw == OFPRAW_OFPT10_PACKET_IN) {
3296 const struct ofp10_packet_in *opi;
3298 opi = ofpbuf_pull(&b, offsetof(struct ofp10_packet_in, data));
3300 pin->packet = opi->data;
3301 pin->packet_len = b.size;
3303 match_init_catchall(&pin->flow_metadata);
3304 match_set_in_port(&pin->flow_metadata, u16_to_ofp(ntohs(opi->in_port)));
3305 pin->reason = opi->reason;
3306 pin->buffer_id = ntohl(opi->buffer_id);
3307 pin->total_len = ntohs(opi->total_len);
3308 } else if (raw == OFPRAW_OFPT11_PACKET_IN) {
3309 const struct ofp11_packet_in *opi;
3313 opi = ofpbuf_pull(&b, sizeof *opi);
3315 pin->packet = b.data;
3316 pin->packet_len = b.size;
3318 pin->buffer_id = ntohl(opi->buffer_id);
3319 error = ofputil_port_from_ofp11(opi->in_port, &in_port);
3323 match_init_catchall(&pin->flow_metadata);
3324 match_set_in_port(&pin->flow_metadata, in_port);
3325 pin->total_len = ntohs(opi->total_len);
3326 pin->reason = opi->reason;
3327 pin->table_id = opi->table_id;
3328 } else if (raw == OFPRAW_NXT_PACKET_IN) {
3329 const struct nx_packet_in *npi;
3332 npi = ofpbuf_pull(&b, sizeof *npi);
3333 error = nx_pull_match_loose(&b, ntohs(npi->match_len),
3334 &pin->flow_metadata, NULL, NULL);
3339 if (!ofpbuf_try_pull(&b, 2)) {
3340 return OFPERR_OFPBRC_BAD_LEN;
3343 pin->reason = npi->reason;
3344 pin->table_id = npi->table_id;
3345 pin->cookie = npi->cookie;
3347 pin->buffer_id = ntohl(npi->buffer_id);
3348 pin->total_len = ntohs(npi->total_len);
3350 pin->packet = b.data;
3351 pin->packet_len = b.size;
3359 static struct ofpbuf *
3360 ofputil_encode_ofp10_packet_in(const struct ofputil_packet_in *pin)
3362 struct ofp10_packet_in *opi;
3363 struct ofpbuf *packet;
3365 packet = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
3366 htonl(0), pin->packet_len);
3367 opi = ofpbuf_put_zeros(packet, offsetof(struct ofp10_packet_in, data));
3368 opi->total_len = htons(pin->total_len);
3369 opi->in_port = htons(ofp_to_u16(pin->flow_metadata.flow.in_port.ofp_port));
3370 opi->reason = pin->reason;
3371 opi->buffer_id = htonl(pin->buffer_id);
3373 ofpbuf_put(packet, pin->packet, pin->packet_len);
3378 static struct ofpbuf *
3379 ofputil_encode_nx_packet_in(const struct ofputil_packet_in *pin)
3381 struct nx_packet_in *npi;
3382 struct ofpbuf *packet;
3385 /* The final argument is just an estimate of the space required. */
3386 packet = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, OFP10_VERSION,
3387 htonl(0), NXM_TYPICAL_LEN + 2 + pin->packet_len);
3388 ofpbuf_put_zeros(packet, sizeof *npi);
3389 match_len = nx_put_match(packet, &pin->flow_metadata, 0, 0);
3390 ofpbuf_put_zeros(packet, 2);
3391 ofpbuf_put(packet, pin->packet, pin->packet_len);
3394 npi->buffer_id = htonl(pin->buffer_id);
3395 npi->total_len = htons(pin->total_len);
3396 npi->reason = pin->reason;
3397 npi->table_id = pin->table_id;
3398 npi->cookie = pin->cookie;
3399 npi->match_len = htons(match_len);
3404 static struct ofpbuf *
3405 ofputil_encode_ofp11_packet_in(const struct ofputil_packet_in *pin)
3407 struct ofp11_packet_in *opi;
3408 struct ofpbuf *packet;
3410 packet = ofpraw_alloc_xid(OFPRAW_OFPT11_PACKET_IN, OFP11_VERSION,
3411 htonl(0), pin->packet_len);
3412 opi = ofpbuf_put_zeros(packet, sizeof *opi);
3413 opi->buffer_id = htonl(pin->buffer_id);
3414 opi->in_port = ofputil_port_to_ofp11(pin->flow_metadata.flow.in_port.ofp_port);
3415 opi->in_phy_port = opi->in_port;
3416 opi->total_len = htons(pin->total_len);
3417 opi->reason = pin->reason;
3418 opi->table_id = pin->table_id;
3420 ofpbuf_put(packet, pin->packet, pin->packet_len);
3425 static struct ofpbuf *
3426 ofputil_encode_ofp12_packet_in(const struct ofputil_packet_in *pin,
3427 enum ofputil_protocol protocol)
3429 struct ofp13_packet_in *opi;
3430 enum ofpraw packet_in_raw;
3431 enum ofp_version packet_in_version;
3432 size_t packet_in_size;
3433 struct ofpbuf *packet;
3435 if (protocol == OFPUTIL_P_OF12_OXM) {
3436 packet_in_raw = OFPRAW_OFPT12_PACKET_IN;
3437 packet_in_version = OFP12_VERSION;
3438 packet_in_size = sizeof (struct ofp12_packet_in);
3440 packet_in_raw = OFPRAW_OFPT13_PACKET_IN;
3441 packet_in_version = ofputil_protocol_to_ofp_version(protocol);
3442 packet_in_size = sizeof (struct ofp13_packet_in);
3445 /* The final argument is just an estimate of the space required. */
3446 packet = ofpraw_alloc_xid(packet_in_raw, packet_in_version,
3447 htonl(0), NXM_TYPICAL_LEN + 2 + pin->packet_len);
3448 ofpbuf_put_zeros(packet, packet_in_size);
3449 oxm_put_match(packet, &pin->flow_metadata,
3450 ofputil_protocol_to_ofp_version(protocol));
3451 ofpbuf_put_zeros(packet, 2);
3452 ofpbuf_put(packet, pin->packet, pin->packet_len);
3455 opi->pi.buffer_id = htonl(pin->buffer_id);
3456 opi->pi.total_len = htons(pin->total_len);
3457 opi->pi.reason = pin->reason;
3458 opi->pi.table_id = pin->table_id;
3459 if (protocol != OFPUTIL_P_OF12_OXM) {
3460 opi->cookie = pin->cookie;
3466 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
3467 * in the format specified by 'packet_in_format'. */
3469 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
3470 enum ofputil_protocol protocol,
3471 enum nx_packet_in_format packet_in_format)
3473 struct ofpbuf *packet;
3476 case OFPUTIL_P_OF10_STD:
3477 case OFPUTIL_P_OF10_STD_TID:
3478 case OFPUTIL_P_OF10_NXM:
3479 case OFPUTIL_P_OF10_NXM_TID:
3480 packet = (packet_in_format == NXPIF_NXM
3481 ? ofputil_encode_nx_packet_in(pin)
3482 : ofputil_encode_ofp10_packet_in(pin));
3485 case OFPUTIL_P_OF11_STD:
3486 packet = ofputil_encode_ofp11_packet_in(pin);
3489 case OFPUTIL_P_OF12_OXM:
3490 case OFPUTIL_P_OF13_OXM:
3491 case OFPUTIL_P_OF14_OXM:
3492 case OFPUTIL_P_OF15_OXM:
3493 packet = ofputil_encode_ofp12_packet_in(pin, protocol);
3500 ofpmsg_update_length(packet);
3504 /* Returns a string form of 'reason'. The return value is either a statically
3505 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
3506 * 'bufsize' should be at least OFPUTIL_PACKET_IN_REASON_BUFSIZE. */
3508 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason,
3509 char *reasonbuf, size_t bufsize)
3516 case OFPR_INVALID_TTL:
3517 return "invalid_ttl";
3518 case OFPR_ACTION_SET:
3519 return "action_set";
3522 case OFPR_PACKET_OUT:
3523 return "packet_out";
3525 case OFPR_N_REASONS:
3527 snprintf(reasonbuf, bufsize, "%d", (int) reason);
3533 ofputil_packet_in_reason_from_string(const char *s,
3534 enum ofp_packet_in_reason *reason)
3538 for (i = 0; i < OFPR_N_REASONS; i++) {
3539 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
3540 const char *reason_s;
3542 reason_s = ofputil_packet_in_reason_to_string(i, reasonbuf,
3544 if (!strcasecmp(s, reason_s)) {
3552 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
3555 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
3556 * message's actions. The caller must initialize 'ofpacts' and retains
3557 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
3559 * Returns 0 if successful, otherwise an OFPERR_* value. */
3561 ofputil_decode_packet_out(struct ofputil_packet_out *po,
3562 const struct ofp_header *oh,
3563 struct ofpbuf *ofpacts)
3568 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3569 raw = ofpraw_pull_assert(&b);
3571 ofpbuf_clear(ofpacts);
3572 if (raw == OFPRAW_OFPT11_PACKET_OUT) {
3574 const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
3576 po->buffer_id = ntohl(opo->buffer_id);
3577 error = ofputil_port_from_ofp11(opo->in_port, &po->in_port);
3582 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
3583 oh->version, ofpacts);
3587 } else if (raw == OFPRAW_OFPT10_PACKET_OUT) {
3589 const struct ofp10_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
3591 po->buffer_id = ntohl(opo->buffer_id);
3592 po->in_port = u16_to_ofp(ntohs(opo->in_port));
3594 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
3595 oh->version, ofpacts);
3603 if (ofp_to_u16(po->in_port) >= ofp_to_u16(OFPP_MAX)
3604 && po->in_port != OFPP_LOCAL
3605 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
3606 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
3608 return OFPERR_OFPBRC_BAD_PORT;
3611 po->ofpacts = ofpacts->data;
3612 po->ofpacts_len = ofpacts->size;
3614 if (po->buffer_id == UINT32_MAX) {
3615 po->packet = b.data;
3616 po->packet_len = b.size;
3625 /* ofputil_phy_port */
3627 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
3628 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
3629 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
3630 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
3631 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
3632 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
3633 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
3634 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
3636 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
3637 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
3638 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
3639 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
3640 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
3641 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
3643 static enum netdev_features
3644 netdev_port_features_from_ofp10(ovs_be32 ofp10_)
3646 uint32_t ofp10 = ntohl(ofp10_);
3647 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
3651 netdev_port_features_to_ofp10(enum netdev_features features)
3653 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
3656 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
3657 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
3658 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
3659 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
3660 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
3661 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
3662 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
3663 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
3664 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
3665 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
3666 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
3667 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
3668 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
3669 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
3670 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
3671 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
3673 static enum netdev_features
3674 netdev_port_features_from_ofp11(ovs_be32 ofp11)
3676 return ntohl(ofp11) & 0xffff;
3680 netdev_port_features_to_ofp11(enum netdev_features features)
3682 return htonl(features & 0xffff);
3686 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
3687 const struct ofp10_phy_port *opp)
3689 pp->port_no = u16_to_ofp(ntohs(opp->port_no));
3690 pp->hw_addr = opp->hw_addr;
3691 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
3693 pp->config = ntohl(opp->config) & OFPPC10_ALL;
3694 pp->state = ntohl(opp->state) & OFPPS10_ALL;
3696 pp->curr = netdev_port_features_from_ofp10(opp->curr);
3697 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
3698 pp->supported = netdev_port_features_from_ofp10(opp->supported);
3699 pp->peer = netdev_port_features_from_ofp10(opp->peer);
3701 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
3702 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
3708 ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
3709 const struct ofp11_port *op)
3713 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
3717 pp->hw_addr = op->hw_addr;
3718 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
3720 pp->config = ntohl(op->config) & OFPPC11_ALL;
3721 pp->state = ntohl(op->state) & OFPPS11_ALL;
3723 pp->curr = netdev_port_features_from_ofp11(op->curr);
3724 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
3725 pp->supported = netdev_port_features_from_ofp11(op->supported);
3726 pp->peer = netdev_port_features_from_ofp11(op->peer);
3728 pp->curr_speed = ntohl(op->curr_speed);
3729 pp->max_speed = ntohl(op->max_speed);
3735 parse_ofp14_port_ethernet_property(const struct ofpbuf *payload,
3736 struct ofputil_phy_port *pp)
3738 struct ofp14_port_desc_prop_ethernet *eth = payload->data;
3740 if (payload->size != sizeof *eth) {
3741 return OFPERR_OFPBPC_BAD_LEN;
3744 pp->curr = netdev_port_features_from_ofp11(eth->curr);
3745 pp->advertised = netdev_port_features_from_ofp11(eth->advertised);
3746 pp->supported = netdev_port_features_from_ofp11(eth->supported);
3747 pp->peer = netdev_port_features_from_ofp11(eth->peer);
3749 pp->curr_speed = ntohl(eth->curr_speed);
3750 pp->max_speed = ntohl(eth->max_speed);
3756 ofputil_pull_ofp14_port(struct ofputil_phy_port *pp, struct ofpbuf *msg)
3758 struct ofpbuf properties;
3759 struct ofp14_port *op;
3763 op = ofpbuf_try_pull(msg, sizeof *op);
3765 return OFPERR_OFPBRC_BAD_LEN;
3768 len = ntohs(op->length);
3769 if (len < sizeof *op || len - sizeof *op > msg->size) {
3770 return OFPERR_OFPBRC_BAD_LEN;
3773 ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len);
3775 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
3779 pp->hw_addr = op->hw_addr;
3780 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
3782 pp->config = ntohl(op->config) & OFPPC11_ALL;
3783 pp->state = ntohl(op->state) & OFPPS11_ALL;
3785 while (properties.size > 0) {
3786 struct ofpbuf payload;
3790 error = ofpprop_pull(&properties, &payload, &type);
3796 case OFPPDPT14_ETHERNET:
3797 error = parse_ofp14_port_ethernet_property(&payload, pp);
3801 OFPPROP_LOG(&bad_ofmsg_rl, true,
3802 "unknown port property %"PRIu16, type);
3816 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
3817 struct ofp10_phy_port *opp)
3819 memset(opp, 0, sizeof *opp);
3821 opp->port_no = htons(ofp_to_u16(pp->port_no));
3822 opp->hw_addr = pp->hw_addr;
3823 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
3825 opp->config = htonl(pp->config & OFPPC10_ALL);
3826 opp->state = htonl(pp->state & OFPPS10_ALL);
3828 opp->curr = netdev_port_features_to_ofp10(pp->curr);
3829 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
3830 opp->supported = netdev_port_features_to_ofp10(pp->supported);
3831 opp->peer = netdev_port_features_to_ofp10(pp->peer);
3835 ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
3836 struct ofp11_port *op)
3838 memset(op, 0, sizeof *op);
3840 op->port_no = ofputil_port_to_ofp11(pp->port_no);
3841 op->hw_addr = pp->hw_addr;
3842 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
3844 op->config = htonl(pp->config & OFPPC11_ALL);
3845 op->state = htonl(pp->state & OFPPS11_ALL);
3847 op->curr = netdev_port_features_to_ofp11(pp->curr);
3848 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
3849 op->supported = netdev_port_features_to_ofp11(pp->supported);
3850 op->peer = netdev_port_features_to_ofp11(pp->peer);
3852 op->curr_speed = htonl(pp->curr_speed);
3853 op->max_speed = htonl(pp->max_speed);
3857 ofputil_put_ofp14_port(const struct ofputil_phy_port *pp,
3860 struct ofp14_port *op;
3861 struct ofp14_port_desc_prop_ethernet *eth;
3863 ofpbuf_prealloc_tailroom(b, sizeof *op + sizeof *eth);
3865 op = ofpbuf_put_zeros(b, sizeof *op);
3866 op->port_no = ofputil_port_to_ofp11(pp->port_no);
3867 op->length = htons(sizeof *op + sizeof *eth);
3868 op->hw_addr = pp->hw_addr;
3869 ovs_strlcpy(op->name, pp->name, sizeof op->name);
3870 op->config = htonl(pp->config & OFPPC11_ALL);
3871 op->state = htonl(pp->state & OFPPS11_ALL);
3873 eth = ofpbuf_put_zeros(b, sizeof *eth);
3874 eth->type = htons(OFPPDPT14_ETHERNET);
3875 eth->length = htons(sizeof *eth);
3876 eth->curr = netdev_port_features_to_ofp11(pp->curr);
3877 eth->advertised = netdev_port_features_to_ofp11(pp->advertised);
3878 eth->supported = netdev_port_features_to_ofp11(pp->supported);
3879 eth->peer = netdev_port_features_to_ofp11(pp->peer);
3880 eth->curr_speed = htonl(pp->curr_speed);
3881 eth->max_speed = htonl(pp->max_speed);
3885 ofputil_put_phy_port(enum ofp_version ofp_version,
3886 const struct ofputil_phy_port *pp, struct ofpbuf *b)
3888 switch (ofp_version) {
3889 case OFP10_VERSION: {
3890 struct ofp10_phy_port *opp = ofpbuf_put_uninit(b, sizeof *opp);
3891 ofputil_encode_ofp10_phy_port(pp, opp);
3897 case OFP13_VERSION: {
3898 struct ofp11_port *op = ofpbuf_put_uninit(b, sizeof *op);
3899 ofputil_encode_ofp11_port(pp, op);
3905 ofputil_put_ofp14_port(pp, b);
3914 ofputil_decode_port_desc_stats_request(const struct ofp_header *request,
3920 ofpbuf_use_const(&b, request, ntohs(request->length));
3921 raw = ofpraw_pull_assert(&b);
3922 if (raw == OFPRAW_OFPST10_PORT_DESC_REQUEST) {
3925 } else if (raw == OFPRAW_OFPST15_PORT_DESC_REQUEST) {
3926 ovs_be32 *ofp11_port;
3928 ofp11_port = ofpbuf_pull(&b, sizeof *ofp11_port);
3929 return ofputil_port_from_ofp11(*ofp11_port, port);
3936 ofputil_encode_port_desc_stats_request(enum ofp_version ofp_version,
3939 struct ofpbuf *request;
3941 switch (ofp_version) {
3947 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_DESC_REQUEST,
3950 case OFP15_VERSION:{
3951 struct ofp15_port_desc_request *req;
3952 request = ofpraw_alloc(OFPRAW_OFPST15_PORT_DESC_REQUEST,
3954 req = ofpbuf_put_zeros(request, sizeof *req);
3955 req->port_no = ofputil_port_to_ofp11(port);
3966 ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port *pp,
3967 struct ovs_list *replies)
3969 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
3970 size_t start_ofs = reply->size;
3972 ofputil_put_phy_port(ofpmp_version(replies), pp, reply);
3973 ofpmp_postappend(replies, start_ofs);
3976 /* ofputil_switch_config */
3978 /* Decodes 'oh', which must be an OFPT_GET_CONFIG_REPLY or OFPT_SET_CONFIG
3979 * message, into 'config'. Returns false if 'oh' contained any flags that
3980 * aren't specified in its version of OpenFlow, true otherwise. */
3982 ofputil_decode_switch_config(const struct ofp_header *oh,
3983 struct ofputil_switch_config *config)
3985 const struct ofp_switch_config *osc;
3988 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3989 ofpraw_pull_assert(&b);
3990 osc = ofpbuf_pull(&b, sizeof *osc);
3992 config->frag = ntohs(osc->flags) & OFPC_FRAG_MASK;
3993 config->miss_send_len = ntohs(osc->miss_send_len);
3995 ovs_be16 valid_mask = htons(OFPC_FRAG_MASK);
3996 if (oh->version < OFP13_VERSION) {
3997 const ovs_be16 ttl_bit = htons(OFPC_INVALID_TTL_TO_CONTROLLER);
3998 valid_mask |= ttl_bit;
3999 config->invalid_ttl_to_controller = (osc->flags & ttl_bit) != 0;
4001 config->invalid_ttl_to_controller = -1;
4004 return !(osc->flags & ~valid_mask);
4008 ofputil_decode_get_config_reply(const struct ofp_header *oh,
4009 struct ofputil_switch_config *config)
4011 ofputil_decode_switch_config(oh, config);
4015 ofputil_decode_set_config(const struct ofp_header *oh,
4016 struct ofputil_switch_config *config)
4018 return (ofputil_decode_switch_config(oh, config)
4020 : OFPERR_OFPSCFC_BAD_FLAGS);
4023 static struct ofpbuf *
4024 ofputil_put_switch_config(const struct ofputil_switch_config *config,
4027 const struct ofp_header *oh = b->data;
4028 struct ofp_switch_config *osc = ofpbuf_put_zeros(b, sizeof *osc);
4029 osc->flags = htons(config->frag);
4030 if (config->invalid_ttl_to_controller > 0 && oh->version < OFP13_VERSION) {
4031 osc->flags |= htons(OFPC_INVALID_TTL_TO_CONTROLLER);
4033 osc->miss_send_len = htons(config->miss_send_len);
4038 ofputil_encode_get_config_reply(const struct ofp_header *request,
4039 const struct ofputil_switch_config *config)
4041 struct ofpbuf *b = ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY,
4043 return ofputil_put_switch_config(config, b);
4047 ofputil_encode_set_config(const struct ofputil_switch_config *config,
4048 enum ofp_version version)
4050 struct ofpbuf *b = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, version, 0);
4051 return ofputil_put_switch_config(config, b);
4054 /* ofputil_switch_features */
4056 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
4057 OFPC_IP_REASM | OFPC_QUEUE_STATS)
4058 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
4059 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
4060 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
4061 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
4062 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
4063 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
4066 ofputil_capabilities_mask(enum ofp_version ofp_version)
4068 /* Handle capabilities whose bit is unique for all OpenFlow versions */
4069 switch (ofp_version) {
4072 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
4077 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
4079 /* Caller needs to check osf->header.version itself */
4084 /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an
4085 * abstract representation in '*features'. Initializes '*b' to iterate over
4086 * the OpenFlow port structures following 'osf' with later calls to
4087 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an
4088 * OFPERR_* value. */
4090 ofputil_decode_switch_features(const struct ofp_header *oh,
4091 struct ofputil_switch_features *features,
4094 const struct ofp_switch_features *osf;
4097 ofpbuf_use_const(b, oh, ntohs(oh->length));
4098 raw = ofpraw_pull_assert(b);
4100 osf = ofpbuf_pull(b, sizeof *osf);
4101 features->datapath_id = ntohll(osf->datapath_id);
4102 features->n_buffers = ntohl(osf->n_buffers);
4103 features->n_tables = osf->n_tables;
4104 features->auxiliary_id = 0;
4106 features->capabilities = ntohl(osf->capabilities) &
4107 ofputil_capabilities_mask(oh->version);
4109 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
4110 if (osf->capabilities & htonl(OFPC10_STP)) {
4111 features->capabilities |= OFPUTIL_C_STP;
4113 features->ofpacts = ofpact_bitmap_from_openflow(osf->actions,
4115 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY
4116 || raw == OFPRAW_OFPT13_FEATURES_REPLY) {
4117 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
4118 features->capabilities |= OFPUTIL_C_GROUP_STATS;
4120 features->ofpacts = 0;
4121 if (raw == OFPRAW_OFPT13_FEATURES_REPLY) {
4122 features->auxiliary_id = osf->auxiliary_id;
4125 return OFPERR_OFPBRC_BAD_VERSION;
4131 /* In OpenFlow 1.0, 1.1, and 1.2, an OFPT_FEATURES_REPLY message lists all the
4132 * switch's ports, unless there are too many to fit. In OpenFlow 1.3 and
4133 * later, an OFPT_FEATURES_REPLY does not list ports at all.
4135 * Given a buffer 'b' that contains a Features Reply message, this message
4136 * checks if it contains a complete list of the switch's ports. Returns true,
4137 * if so. Returns false if the list is missing (OF1.3+) or incomplete
4138 * (OF1.0/1.1/1.2), and in the latter case removes all of the ports from the
4141 * When this function returns false, the caller should send an OFPST_PORT_DESC
4142 * stats request to get the ports. */
4144 ofputil_switch_features_has_ports(struct ofpbuf *b)
4146 struct ofp_header *oh = b->data;
4147 size_t phy_port_size;
4149 if (oh->version >= OFP13_VERSION) {
4150 /* OpenFlow 1.3+ never has ports in the feature reply. */
4154 phy_port_size = (oh->version == OFP10_VERSION
4155 ? sizeof(struct ofp10_phy_port)
4156 : sizeof(struct ofp11_port));
4157 if (ntohs(oh->length) + phy_port_size <= UINT16_MAX) {
4158 /* There's room for additional ports in the feature reply.
4159 * Assume that the list is complete. */
4163 /* The feature reply has no room for more ports. Probably the list is
4164 * truncated. Drop the ports and tell the caller to retrieve them with
4165 * OFPST_PORT_DESC. */
4166 b->size = sizeof *oh + sizeof(struct ofp_switch_features);
4167 ofpmsg_update_length(b);
4171 /* Returns a buffer owned by the caller that encodes 'features' in the format
4172 * required by 'protocol' with the given 'xid'. The caller should append port
4173 * information to the buffer with subsequent calls to
4174 * ofputil_put_switch_features_port(). */
4176 ofputil_encode_switch_features(const struct ofputil_switch_features *features,
4177 enum ofputil_protocol protocol, ovs_be32 xid)
4179 struct ofp_switch_features *osf;
4181 enum ofp_version version;
4184 version = ofputil_protocol_to_ofp_version(protocol);
4187 raw = OFPRAW_OFPT10_FEATURES_REPLY;
4191 raw = OFPRAW_OFPT11_FEATURES_REPLY;
4196 raw = OFPRAW_OFPT13_FEATURES_REPLY;
4201 b = ofpraw_alloc_xid(raw, version, xid, 0);
4202 osf = ofpbuf_put_zeros(b, sizeof *osf);
4203 osf->datapath_id = htonll(features->datapath_id);
4204 osf->n_buffers = htonl(features->n_buffers);
4205 osf->n_tables = features->n_tables;
4207 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
4208 osf->capabilities = htonl(features->capabilities &
4209 ofputil_capabilities_mask(version));
4212 if (features->capabilities & OFPUTIL_C_STP) {
4213 osf->capabilities |= htonl(OFPC10_STP);
4215 osf->actions = ofpact_bitmap_to_openflow(features->ofpacts,
4221 osf->auxiliary_id = features->auxiliary_id;
4225 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
4226 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
4236 /* Encodes 'pp' into the format required by the switch_features message already
4237 * in 'b', which should have been returned by ofputil_encode_switch_features(),
4238 * and appends the encoded version to 'b'. */
4240 ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
4243 const struct ofp_header *oh = b->data;
4245 if (oh->version < OFP13_VERSION) {
4246 /* Try adding a port description to the message, but drop it again if
4247 * the buffer overflows. (This possibility for overflow is why
4248 * OpenFlow 1.3+ moved port descriptions into a multipart message.) */
4249 size_t start_ofs = b->size;
4250 ofputil_put_phy_port(oh->version, pp, b);
4251 if (b->size > UINT16_MAX) {
4252 b->size = start_ofs;
4257 /* ofputil_port_status */
4259 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
4260 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
4262 ofputil_decode_port_status(const struct ofp_header *oh,
4263 struct ofputil_port_status *ps)
4265 const struct ofp_port_status *ops;
4269 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4270 ofpraw_pull_assert(&b);
4271 ops = ofpbuf_pull(&b, sizeof *ops);
4273 if (ops->reason != OFPPR_ADD &&
4274 ops->reason != OFPPR_DELETE &&
4275 ops->reason != OFPPR_MODIFY) {
4276 return OFPERR_NXBRC_BAD_REASON;
4278 ps->reason = ops->reason;
4280 retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
4281 ovs_assert(retval != EOF);
4285 /* Converts the abstract form of a "port status" message in '*ps' into an
4286 * OpenFlow message suitable for 'protocol', and returns that encoded form in
4287 * a buffer owned by the caller. */
4289 ofputil_encode_port_status(const struct ofputil_port_status *ps,
4290 enum ofputil_protocol protocol)
4292 struct ofp_port_status *ops;
4294 enum ofp_version version;
4297 version = ofputil_protocol_to_ofp_version(protocol);
4300 raw = OFPRAW_OFPT10_PORT_STATUS;
4306 raw = OFPRAW_OFPT11_PORT_STATUS;
4311 raw = OFPRAW_OFPT14_PORT_STATUS;
4318 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
4319 ops = ofpbuf_put_zeros(b, sizeof *ops);
4320 ops->reason = ps->reason;
4321 ofputil_put_phy_port(version, &ps->desc, b);
4322 ofpmsg_update_length(b);
4326 /* ofputil_port_mod */
4329 parse_port_mod_ethernet_property(struct ofpbuf *property,
4330 struct ofputil_port_mod *pm)
4332 struct ofp14_port_mod_prop_ethernet *eth = property->data;
4334 if (property->size != sizeof *eth) {
4335 return OFPERR_OFPBRC_BAD_LEN;
4338 pm->advertise = netdev_port_features_from_ofp11(eth->advertise);
4342 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
4343 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
4345 ofputil_decode_port_mod(const struct ofp_header *oh,
4346 struct ofputil_port_mod *pm, bool loose)
4351 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4352 raw = ofpraw_pull_assert(&b);
4354 if (raw == OFPRAW_OFPT10_PORT_MOD) {
4355 const struct ofp10_port_mod *opm = b.data;
4357 pm->port_no = u16_to_ofp(ntohs(opm->port_no));
4358 pm->hw_addr = opm->hw_addr;
4359 pm->config = ntohl(opm->config) & OFPPC10_ALL;
4360 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
4361 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
4362 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
4363 const struct ofp11_port_mod *opm = b.data;
4366 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
4371 pm->hw_addr = opm->hw_addr;
4372 pm->config = ntohl(opm->config) & OFPPC11_ALL;
4373 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
4374 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
4375 } else if (raw == OFPRAW_OFPT14_PORT_MOD) {
4376 const struct ofp14_port_mod *opm = ofpbuf_pull(&b, sizeof *opm);
4379 memset(pm, 0, sizeof *pm);
4381 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
4386 pm->hw_addr = opm->hw_addr;
4387 pm->config = ntohl(opm->config) & OFPPC11_ALL;
4388 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
4390 while (b.size > 0) {
4391 struct ofpbuf property;
4395 error = ofpprop_pull(&b, &property, &type);
4401 case OFPPMPT14_ETHERNET:
4402 error = parse_port_mod_ethernet_property(&property, pm);
4406 OFPPROP_LOG(&bad_ofmsg_rl, loose,
4407 "unknown port_mod property %"PRIu16, type);
4410 } else if (type == OFPPMPT14_EXPERIMENTER) {
4411 error = OFPERR_OFPBPC_BAD_EXPERIMENTER;
4413 error = OFPERR_OFPBRC_BAD_TYPE;
4423 return OFPERR_OFPBRC_BAD_TYPE;
4426 pm->config &= pm->mask;
4430 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
4431 * message suitable for 'protocol', and returns that encoded form in a buffer
4432 * owned by the caller. */
4434 ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
4435 enum ofputil_protocol protocol)
4437 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
4440 switch (ofp_version) {
4441 case OFP10_VERSION: {
4442 struct ofp10_port_mod *opm;
4444 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
4445 opm = ofpbuf_put_zeros(b, sizeof *opm);
4446 opm->port_no = htons(ofp_to_u16(pm->port_no));
4447 opm->hw_addr = pm->hw_addr;
4448 opm->config = htonl(pm->config & OFPPC10_ALL);
4449 opm->mask = htonl(pm->mask & OFPPC10_ALL);
4450 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
4456 case OFP13_VERSION: {
4457 struct ofp11_port_mod *opm;
4459 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
4460 opm = ofpbuf_put_zeros(b, sizeof *opm);
4461 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
4462 opm->hw_addr = pm->hw_addr;
4463 opm->config = htonl(pm->config & OFPPC11_ALL);
4464 opm->mask = htonl(pm->mask & OFPPC11_ALL);
4465 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
4469 case OFP15_VERSION: {
4470 struct ofp14_port_mod_prop_ethernet *eth;
4471 struct ofp14_port_mod *opm;
4473 b = ofpraw_alloc(OFPRAW_OFPT14_PORT_MOD, ofp_version, sizeof *eth);
4474 opm = ofpbuf_put_zeros(b, sizeof *opm);
4475 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
4476 opm->hw_addr = pm->hw_addr;
4477 opm->config = htonl(pm->config & OFPPC11_ALL);
4478 opm->mask = htonl(pm->mask & OFPPC11_ALL);
4480 if (pm->advertise) {
4481 eth = ofpbuf_put_zeros(b, sizeof *eth);
4482 eth->type = htons(OFPPMPT14_ETHERNET);
4483 eth->length = htons(sizeof *eth);
4484 eth->advertise = netdev_port_features_to_ofp11(pm->advertise);
4495 /* Table features. */
4498 pull_table_feature_property(struct ofpbuf *msg, struct ofpbuf *payload,
4503 error = ofpprop_pull(msg, payload, typep);
4504 if (payload && !error) {
4505 ofpbuf_pull(payload, sizeof(struct ofp_prop_header));
4511 parse_action_bitmap(struct ofpbuf *payload, enum ofp_version ofp_version,
4516 while (payload->size > 0) {
4520 error = ofpprop_pull__(payload, NULL, 1, &type);
4524 if (type < CHAR_BIT * sizeof types) {
4525 types |= 1u << type;
4529 *ofpacts = ofpact_bitmap_from_openflow(htonl(types), ofp_version);
4534 parse_instruction_ids(struct ofpbuf *payload, bool loose, uint32_t *insts)
4537 while (payload->size > 0) {
4538 enum ovs_instruction_type inst;
4542 /* OF1.3 and OF1.4 aren't clear about padding in the instruction IDs.
4543 * It seems clear that they aren't padded to 8 bytes, though, because
4544 * both standards say that "non-experimenter instructions are 4 bytes"
4545 * and do not mention any padding before the first instruction ID.
4546 * (There wouldn't be any point in padding to 8 bytes if the IDs were
4547 * aligned on an odd 4-byte boundary.)
4549 * Anyway, we just assume they're all glommed together on byte
4551 error = ofpprop_pull__(payload, NULL, 1, &ofpit);
4556 error = ovs_instruction_type_from_inst_type(&inst, ofpit);
4558 *insts |= 1u << inst;
4559 } else if (!loose) {
4567 parse_table_features_next_table(struct ofpbuf *payload,
4568 unsigned long int *next_tables)
4572 memset(next_tables, 0, bitmap_n_bytes(255));
4573 for (i = 0; i < payload->size; i++) {
4574 uint8_t id = ((const uint8_t *) payload->data)[i];
4576 return OFPERR_OFPBPC_BAD_VALUE;
4578 bitmap_set1(next_tables, id);
4584 parse_oxms(struct ofpbuf *payload, bool loose,
4585 struct mf_bitmap *exactp, struct mf_bitmap *maskedp)
4587 struct mf_bitmap exact = MF_BITMAP_INITIALIZER;
4588 struct mf_bitmap masked = MF_BITMAP_INITIALIZER;
4590 while (payload->size > 0) {
4591 const struct mf_field *field;
4595 error = nx_pull_header(payload, &field, &hasmask);
4597 bitmap_set1(hasmask ? masked.bm : exact.bm, field->id);
4598 } else if (error != OFPERR_OFPBMC_BAD_FIELD || !loose) {
4604 } else if (!bitmap_is_all_zeros(exact.bm, MFF_N_IDS)) {
4605 return OFPERR_OFPBMC_BAD_MASK;
4609 } else if (!bitmap_is_all_zeros(masked.bm, MFF_N_IDS)) {
4610 return OFPERR_OFPBMC_BAD_MASK;
4615 /* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract
4616 * ofputil_table_features in 'tf'.
4618 * If 'loose' is true, this function ignores properties and values that it does
4619 * not understand, as a controller would want to do when interpreting
4620 * capabilities provided by a switch. If 'loose' is false, this function
4621 * treats unknown properties and values as an error, as a switch would want to
4622 * do when interpreting a configuration request made by a controller.
4624 * A single OpenFlow message can specify features for multiple tables. Calling
4625 * this function multiple times for a single 'msg' iterates through the tables
4626 * in the message. The caller must initially leave 'msg''s layer pointers null
4627 * and not modify them between calls.
4629 * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise
4630 * a positive "enum ofperr" value. */
4632 ofputil_decode_table_features(struct ofpbuf *msg,
4633 struct ofputil_table_features *tf, bool loose)
4635 const struct ofp_header *oh;
4636 struct ofp13_table_features *otf;
4637 struct ofpbuf properties;
4640 memset(tf, 0, sizeof *tf);
4643 ofpraw_pull_assert(msg);
4651 if (msg->size < sizeof *otf) {
4652 return OFPERR_OFPBPC_BAD_LEN;
4656 len = ntohs(otf->length);
4657 if (len < sizeof *otf || len % 8 || len > msg->size) {
4658 return OFPERR_OFPBPC_BAD_LEN;
4660 ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len);
4661 ofpbuf_pull(&properties, sizeof *otf);
4663 tf->table_id = otf->table_id;
4664 if (tf->table_id == OFPTT_ALL) {
4665 return OFPERR_OFPTFFC_BAD_TABLE;
4668 ovs_strlcpy(tf->name, otf->name, OFP_MAX_TABLE_NAME_LEN);
4669 tf->metadata_match = otf->metadata_match;
4670 tf->metadata_write = otf->metadata_write;
4671 tf->miss_config = OFPUTIL_TABLE_MISS_DEFAULT;
4672 if (oh->version >= OFP14_VERSION) {
4673 uint32_t caps = ntohl(otf->capabilities);
4674 tf->supports_eviction = (caps & OFPTC14_EVICTION) != 0;
4675 tf->supports_vacancy_events = (caps & OFPTC14_VACANCY_EVENTS) != 0;
4677 tf->supports_eviction = -1;
4678 tf->supports_vacancy_events = -1;
4680 tf->max_entries = ntohl(otf->max_entries);
4682 while (properties.size > 0) {
4683 struct ofpbuf payload;
4687 error = pull_table_feature_property(&properties, &payload, &type);
4692 switch ((enum ofp13_table_feature_prop_type) type) {
4693 case OFPTFPT13_INSTRUCTIONS:
4694 error = parse_instruction_ids(&payload, loose,
4695 &tf->nonmiss.instructions);
4697 case OFPTFPT13_INSTRUCTIONS_MISS:
4698 error = parse_instruction_ids(&payload, loose,
4699 &tf->miss.instructions);
4702 case OFPTFPT13_NEXT_TABLES:
4703 error = parse_table_features_next_table(&payload,
4706 case OFPTFPT13_NEXT_TABLES_MISS:
4707 error = parse_table_features_next_table(&payload, tf->miss.next);
4710 case OFPTFPT13_WRITE_ACTIONS:
4711 error = parse_action_bitmap(&payload, oh->version,
4712 &tf->nonmiss.write.ofpacts);
4714 case OFPTFPT13_WRITE_ACTIONS_MISS:
4715 error = parse_action_bitmap(&payload, oh->version,
4716 &tf->miss.write.ofpacts);
4719 case OFPTFPT13_APPLY_ACTIONS:
4720 error = parse_action_bitmap(&payload, oh->version,
4721 &tf->nonmiss.apply.ofpacts);
4723 case OFPTFPT13_APPLY_ACTIONS_MISS:
4724 error = parse_action_bitmap(&payload, oh->version,
4725 &tf->miss.apply.ofpacts);
4728 case OFPTFPT13_MATCH:
4729 error = parse_oxms(&payload, loose, &tf->match, &tf->mask);
4731 case OFPTFPT13_WILDCARDS:
4732 error = parse_oxms(&payload, loose, &tf->wildcard, NULL);
4735 case OFPTFPT13_WRITE_SETFIELD:
4736 error = parse_oxms(&payload, loose,
4737 &tf->nonmiss.write.set_fields, NULL);
4739 case OFPTFPT13_WRITE_SETFIELD_MISS:
4740 error = parse_oxms(&payload, loose,
4741 &tf->miss.write.set_fields, NULL);
4743 case OFPTFPT13_APPLY_SETFIELD:
4744 error = parse_oxms(&payload, loose,
4745 &tf->nonmiss.apply.set_fields, NULL);
4747 case OFPTFPT13_APPLY_SETFIELD_MISS:
4748 error = parse_oxms(&payload, loose,
4749 &tf->miss.apply.set_fields, NULL);
4752 case OFPTFPT13_EXPERIMENTER:
4753 case OFPTFPT13_EXPERIMENTER_MISS:
4755 OFPPROP_LOG(&bad_ofmsg_rl, loose,
4756 "unknown table features property %"PRIu16, type);
4757 error = loose ? 0 : OFPERR_OFPBPC_BAD_TYPE;
4765 /* Fix inconsistencies:
4767 * - Turn on 'match' bits that are set in 'mask', because maskable
4768 * fields are matchable.
4770 * - Turn on 'wildcard' bits that are set in 'mask', because a field
4771 * that is arbitrarily maskable can be wildcarded entirely.
4773 * - Turn off 'wildcard' bits that are not in 'match', because a field
4774 * must be matchable for it to be meaningfully wildcarded. */
4775 bitmap_or(tf->match.bm, tf->mask.bm, MFF_N_IDS);
4776 bitmap_or(tf->wildcard.bm, tf->mask.bm, MFF_N_IDS);
4777 bitmap_and(tf->wildcard.bm, tf->match.bm, MFF_N_IDS);
4782 /* Encodes and returns a request to obtain the table features of a switch.
4783 * The message is encoded for OpenFlow version 'ofp_version'. */
4785 ofputil_encode_table_features_request(enum ofp_version ofp_version)
4787 struct ofpbuf *request = NULL;
4789 switch (ofp_version) {
4793 ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later "
4794 "(\'-O OpenFlow13\')");
4798 request = ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST,
4809 put_fields_property(struct ofpbuf *reply,
4810 const struct mf_bitmap *fields,
4811 const struct mf_bitmap *masks,
4812 enum ofp13_table_feature_prop_type property,
4813 enum ofp_version version)
4818 start_ofs = ofpprop_start(reply, property);
4819 BITMAP_FOR_EACH_1 (field, MFF_N_IDS, fields->bm) {
4820 nx_put_header(reply, field, version,
4821 masks && bitmap_is_set(masks->bm, field));
4823 ofpprop_end(reply, start_ofs);
4827 put_table_action_features(struct ofpbuf *reply,
4828 const struct ofputil_table_action_features *taf,
4829 enum ofp13_table_feature_prop_type actions_type,
4830 enum ofp13_table_feature_prop_type set_fields_type,
4831 int miss_offset, enum ofp_version version)
4833 ofpprop_put_bitmap(reply, actions_type + miss_offset,
4834 ntohl(ofpact_bitmap_to_openflow(taf->ofpacts,
4836 put_fields_property(reply, &taf->set_fields, NULL,
4837 set_fields_type + miss_offset, version);
4841 put_table_instruction_features(
4842 struct ofpbuf *reply, const struct ofputil_table_instruction_features *tif,
4843 int miss_offset, enum ofp_version version)
4848 ofpprop_put_bitmap(reply, OFPTFPT13_INSTRUCTIONS + miss_offset,
4849 ntohl(ovsinst_bitmap_to_openflow(tif->instructions,
4852 start_ofs = ofpprop_start(reply, OFPTFPT13_NEXT_TABLES + miss_offset);
4853 BITMAP_FOR_EACH_1 (table_id, 255, tif->next) {
4854 ofpbuf_put(reply, &table_id, 1);
4856 ofpprop_end(reply, start_ofs);
4858 put_table_action_features(reply, &tif->write,
4859 OFPTFPT13_WRITE_ACTIONS,
4860 OFPTFPT13_WRITE_SETFIELD, miss_offset, version);
4861 put_table_action_features(reply, &tif->apply,
4862 OFPTFPT13_APPLY_ACTIONS,
4863 OFPTFPT13_APPLY_SETFIELD, miss_offset, version);
4867 ofputil_append_table_features_reply(const struct ofputil_table_features *tf,
4868 struct ovs_list *replies)
4870 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
4871 enum ofp_version version = ofpmp_version(replies);
4872 size_t start_ofs = reply->size;
4873 struct ofp13_table_features *otf;
4875 otf = ofpbuf_put_zeros(reply, sizeof *otf);
4876 otf->table_id = tf->table_id;
4877 ovs_strlcpy(otf->name, tf->name, sizeof otf->name);
4878 otf->metadata_match = tf->metadata_match;
4879 otf->metadata_write = tf->metadata_write;
4880 if (version >= OFP14_VERSION) {
4881 if (tf->supports_eviction) {
4882 otf->capabilities |= htonl(OFPTC14_EVICTION);
4884 if (tf->supports_vacancy_events) {
4885 otf->capabilities |= htonl(OFPTC14_VACANCY_EVENTS);
4888 otf->max_entries = htonl(tf->max_entries);
4890 put_table_instruction_features(reply, &tf->nonmiss, 0, version);
4891 put_table_instruction_features(reply, &tf->miss, 1, version);
4893 put_fields_property(reply, &tf->match, &tf->mask,
4894 OFPTFPT13_MATCH, version);
4895 put_fields_property(reply, &tf->wildcard, NULL,
4896 OFPTFPT13_WILDCARDS, version);
4898 otf = ofpbuf_at_assert(reply, start_ofs, sizeof *otf);
4899 otf->length = htons(reply->size - start_ofs);
4900 ofpmp_postappend(replies, start_ofs);
4904 parse_table_desc_eviction_property(struct ofpbuf *property,
4905 struct ofputil_table_desc *td)
4907 struct ofp14_table_mod_prop_eviction *ote = property->data;
4909 if (property->size != sizeof *ote) {
4910 return OFPERR_OFPBPC_BAD_LEN;
4913 td->eviction_flags = ntohl(ote->flags);
4918 parse_table_desc_vacancy_property(struct ofpbuf *property,
4919 struct ofputil_table_desc *td)
4921 struct ofp14_table_mod_prop_vacancy *otv = property->data;
4923 if (property->size != sizeof *otv) {
4924 return OFPERR_OFPBPC_BAD_LEN;
4927 td->table_vacancy.vacancy_down = otv->vacancy_down;
4928 td->table_vacancy.vacancy_up = otv->vacancy_up;
4929 td->table_vacancy.vacancy = otv->vacancy;
4933 /* Decodes the next OpenFlow "table desc" message (of possibly several) from
4934 * 'msg' into an abstract form in '*td'. Returns 0 if successful, EOF if the
4935 * last "table desc" in 'msg' was already decoded, otherwise an OFPERR_*
4938 ofputil_decode_table_desc(struct ofpbuf *msg,
4939 struct ofputil_table_desc *td,
4940 enum ofp_version version)
4942 struct ofp14_table_desc *otd;
4943 struct ofpbuf properties;
4946 memset(td, 0, sizeof *td);
4949 ofpraw_pull_assert(msg);
4956 otd = ofpbuf_try_pull(msg, sizeof *otd);
4958 VLOG_WARN_RL(&bad_ofmsg_rl, "OFP14_TABLE_DESC reply has %"PRIu32" "
4959 "leftover bytes at end", msg->size);
4960 return OFPERR_OFPBRC_BAD_LEN;
4963 td->table_id = otd->table_id;
4964 length = ntohs(otd->length);
4965 if (length < sizeof *otd || length - sizeof *otd > msg->size) {
4966 VLOG_WARN_RL(&bad_ofmsg_rl, "OFP14_TABLE_DESC reply claims invalid "
4967 "length %"PRIuSIZE, length);
4968 return OFPERR_OFPBRC_BAD_LEN;
4970 length -= sizeof *otd;
4971 ofpbuf_use_const(&properties, ofpbuf_pull(msg, length), length);
4973 td->eviction = ofputil_decode_table_eviction(otd->config, version);
4974 td->vacancy = ofputil_decode_table_vacancy(otd->config, version);
4975 td->eviction_flags = UINT32_MAX;
4977 while (properties.size > 0) {
4978 struct ofpbuf payload;
4982 error = ofpprop_pull(&properties, &payload, &type);
4988 case OFPTMPT14_EVICTION:
4989 error = parse_table_desc_eviction_property(&payload, td);
4992 case OFPTMPT14_VACANCY:
4993 error = parse_table_desc_vacancy_property(&payload, td);
4997 OFPPROP_LOG(&bad_ofmsg_rl, true,
4998 "unknown table_desc property %"PRIu16, type);
5011 /* Encodes and returns a request to obtain description of tables of a switch.
5012 * The message is encoded for OpenFlow version 'ofp_version'. */
5014 ofputil_encode_table_desc_request(enum ofp_version ofp_version)
5016 struct ofpbuf *request = NULL;
5018 if (ofp_version >= OFP14_VERSION) {
5019 request = ofpraw_alloc(OFPRAW_OFPST14_TABLE_DESC_REQUEST,
5022 ovs_fatal(0, "dump-table-desc needs OpenFlow 1.4 or later "
5023 "(\'-O OpenFlow14\')");
5029 /* Function to append Table desc information in a reply list. */
5031 ofputil_append_table_desc_reply(const struct ofputil_table_desc *td,
5032 struct ovs_list *replies,
5033 enum ofp_version version)
5035 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
5037 struct ofp14_table_desc *otd;
5039 start_otd = reply->size;
5040 ofpbuf_put_zeros(reply, sizeof *otd);
5041 if (td->eviction_flags != UINT32_MAX) {
5042 struct ofp14_table_mod_prop_eviction *ote;
5044 ote = ofpbuf_put_zeros(reply, sizeof *ote);
5045 ote->type = htons(OFPTMPT14_EVICTION);
5046 ote->length = htons(sizeof *ote);
5047 ote->flags = htonl(td->eviction_flags);
5049 if (td->vacancy == OFPUTIL_TABLE_VACANCY_ON) {
5050 struct ofp14_table_mod_prop_vacancy *otv;
5052 otv = ofpbuf_put_zeros(reply, sizeof *otv);
5053 otv->type = htons(OFPTMPT14_VACANCY);
5054 otv->length = htons(sizeof *otv);
5055 otv->vacancy_down = td->table_vacancy.vacancy_down;
5056 otv->vacancy_up = td->table_vacancy.vacancy_up;
5057 otv->vacancy = td->table_vacancy.vacancy;
5060 otd = ofpbuf_at_assert(reply, start_otd, sizeof *otd);
5061 otd->length = htons(reply->size - start_otd);
5062 otd->table_id = td->table_id;
5063 otd->config = ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT,
5064 td->eviction, td->vacancy,
5066 ofpmp_postappend(replies, start_otd);
5069 /* This function parses Vacancy property, and decodes the
5070 * ofp14_table_mod_prop_vacancy in ofputil_table_mod.
5071 * Returns OFPERR_OFPBPC_BAD_VALUE error code when vacancy_down is
5072 * greater than vacancy_up and also when current vacancy has non-zero
5073 * value. Returns 0 on success. */
5075 parse_table_mod_vacancy_property(struct ofpbuf *property,
5076 struct ofputil_table_mod *tm)
5078 struct ofp14_table_mod_prop_vacancy *otv = property->data;
5080 if (property->size != sizeof *otv) {
5081 return OFPERR_OFPBPC_BAD_LEN;
5083 tm->table_vacancy.vacancy_down = otv->vacancy_down;
5084 tm->table_vacancy.vacancy_up = otv->vacancy_up;
5085 if (tm->table_vacancy.vacancy_down > tm->table_vacancy.vacancy_up) {
5086 OFPPROP_LOG(&bad_ofmsg_rl, false,
5087 "Value of vacancy_down is greater than vacancy_up");
5088 return OFPERR_OFPBPC_BAD_VALUE;
5090 if (tm->table_vacancy.vacancy_down > 100 ||
5091 tm->table_vacancy.vacancy_up > 100) {
5092 OFPPROP_LOG(&bad_ofmsg_rl, false, "Vacancy threshold percentage "
5093 "should not be greater than 100");
5094 return OFPERR_OFPBPC_BAD_VALUE;
5096 tm->table_vacancy.vacancy = otv->vacancy;
5097 if (tm->table_vacancy.vacancy) {
5098 OFPPROP_LOG(&bad_ofmsg_rl, false,
5099 "Vacancy value should be zero for table-mod messages");
5100 return OFPERR_OFPBPC_BAD_VALUE;
5105 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5106 * table configuration (a table mod, table stats, or table features message),
5107 * returns the table vacancy configuration that it specifies.
5109 * Only OpenFlow 1.4 and later specify table vacancy configuration this way,
5110 * so for other 'version' this function always returns
5111 * OFPUTIL_TABLE_VACANCY_DEFAULT. */
5112 static enum ofputil_table_vacancy
5113 ofputil_decode_table_vacancy(ovs_be32 config, enum ofp_version version)
5115 return (version < OFP14_VERSION ? OFPUTIL_TABLE_VACANCY_DEFAULT
5116 : config & htonl(OFPTC14_VACANCY_EVENTS) ? OFPUTIL_TABLE_VACANCY_ON
5117 : OFPUTIL_TABLE_VACANCY_OFF);
5121 parse_table_mod_eviction_property(struct ofpbuf *property,
5122 struct ofputil_table_mod *tm)
5124 struct ofp14_table_mod_prop_eviction *ote = property->data;
5126 if (property->size != sizeof *ote) {
5127 return OFPERR_OFPBPC_BAD_LEN;
5130 tm->eviction_flags = ntohl(ote->flags);
5134 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5135 * table configuration (a table mod, table stats, or table features message),
5136 * returns the table eviction configuration that it specifies.
5138 * Only OpenFlow 1.4 and later specify table eviction configuration this way,
5139 * so for other 'version' values this function always returns
5140 * OFPUTIL_TABLE_EVICTION_DEFAULT. */
5141 static enum ofputil_table_eviction
5142 ofputil_decode_table_eviction(ovs_be32 config, enum ofp_version version)
5144 return (version < OFP14_VERSION ? OFPUTIL_TABLE_EVICTION_DEFAULT
5145 : config & htonl(OFPTC14_EVICTION) ? OFPUTIL_TABLE_EVICTION_ON
5146 : OFPUTIL_TABLE_EVICTION_OFF);
5149 /* Returns a bitmap of OFPTC* values suitable for 'config' fields in various
5150 * OpenFlow messages of the given 'version', based on the provided 'miss' and
5151 * 'eviction' values. */
5153 ofputil_encode_table_config(enum ofputil_table_miss miss,
5154 enum ofputil_table_eviction eviction,
5155 enum ofputil_table_vacancy vacancy,
5156 enum ofp_version version)
5158 uint32_t config = 0;
5159 /* See the section "OFPTC_* Table Configuration" in DESIGN.md for more
5160 * information on the crazy evolution of this field. */
5163 /* OpenFlow 1.0 didn't have such a field, any value ought to do. */
5168 /* OpenFlow 1.1 and 1.2 define only OFPTC11_TABLE_MISS_*. */
5170 case OFPUTIL_TABLE_MISS_DEFAULT:
5171 /* Really this shouldn't be used for encoding (the caller should
5172 * provide a specific value) but I can't imagine that defaulting to
5173 * the fall-through case here will hurt. */
5174 case OFPUTIL_TABLE_MISS_CONTROLLER:
5176 return htonl(OFPTC11_TABLE_MISS_CONTROLLER);
5177 case OFPUTIL_TABLE_MISS_CONTINUE:
5178 return htonl(OFPTC11_TABLE_MISS_CONTINUE);
5179 case OFPUTIL_TABLE_MISS_DROP:
5180 return htonl(OFPTC11_TABLE_MISS_DROP);
5185 /* OpenFlow 1.3 removed OFPTC11_TABLE_MISS_* and didn't define any new
5186 * flags, so this is correct. */
5191 /* OpenFlow 1.4 introduced OFPTC14_EVICTION and
5192 * OFPTC14_VACANCY_EVENTS. */
5193 if (eviction == OFPUTIL_TABLE_EVICTION_ON) {
5194 config |= OFPTC14_EVICTION;
5196 if (vacancy == OFPUTIL_TABLE_VACANCY_ON) {
5197 config |= OFPTC14_VACANCY_EVENTS;
5199 return htonl(config);
5205 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5206 * table configuration (a table mod, table stats, or table features message),
5207 * returns the table miss configuration that it specifies.
5209 * Only OpenFlow 1.1 and 1.2 specify table miss configurations this way, so for
5210 * other 'version' values this function always returns
5211 * OFPUTIL_TABLE_MISS_DEFAULT. */
5212 static enum ofputil_table_miss
5213 ofputil_decode_table_miss(ovs_be32 config_, enum ofp_version version)
5215 uint32_t config = ntohl(config_);
5217 if (version == OFP11_VERSION || version == OFP12_VERSION) {
5218 switch (config & OFPTC11_TABLE_MISS_MASK) {
5219 case OFPTC11_TABLE_MISS_CONTROLLER:
5220 return OFPUTIL_TABLE_MISS_CONTROLLER;
5222 case OFPTC11_TABLE_MISS_CONTINUE:
5223 return OFPUTIL_TABLE_MISS_CONTINUE;
5225 case OFPTC11_TABLE_MISS_DROP:
5226 return OFPUTIL_TABLE_MISS_DROP;
5229 VLOG_WARN_RL(&bad_ofmsg_rl, "bad table miss config %d", config);
5230 return OFPUTIL_TABLE_MISS_CONTROLLER;
5233 return OFPUTIL_TABLE_MISS_DEFAULT;
5237 /* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in
5238 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
5240 ofputil_decode_table_mod(const struct ofp_header *oh,
5241 struct ofputil_table_mod *pm)
5246 memset(pm, 0, sizeof *pm);
5247 pm->miss = OFPUTIL_TABLE_MISS_DEFAULT;
5248 pm->eviction = OFPUTIL_TABLE_EVICTION_DEFAULT;
5249 pm->eviction_flags = UINT32_MAX;
5250 pm->vacancy = OFPUTIL_TABLE_VACANCY_DEFAULT;
5251 ofpbuf_use_const(&b, oh, ntohs(oh->length));
5252 raw = ofpraw_pull_assert(&b);
5254 if (raw == OFPRAW_OFPT11_TABLE_MOD) {
5255 const struct ofp11_table_mod *otm = b.data;
5257 pm->table_id = otm->table_id;
5258 pm->miss = ofputil_decode_table_miss(otm->config, oh->version);
5259 } else if (raw == OFPRAW_OFPT14_TABLE_MOD) {
5260 const struct ofp14_table_mod *otm = ofpbuf_pull(&b, sizeof *otm);
5262 pm->table_id = otm->table_id;
5263 pm->miss = ofputil_decode_table_miss(otm->config, oh->version);
5264 pm->eviction = ofputil_decode_table_eviction(otm->config, oh->version);
5265 pm->vacancy = ofputil_decode_table_vacancy(otm->config, oh->version);
5266 while (b.size > 0) {
5267 struct ofpbuf property;
5271 error = ofpprop_pull(&b, &property, &type);
5277 case OFPTMPT14_EVICTION:
5278 error = parse_table_mod_eviction_property(&property, pm);
5281 case OFPTMPT14_VACANCY:
5282 error = parse_table_mod_vacancy_property(&property, pm);
5286 error = OFPERR_OFPBRC_BAD_TYPE;
5295 return OFPERR_OFPBRC_BAD_TYPE;
5301 /* Converts the abstract form of a "table mod" message in '*tm' into an
5302 * OpenFlow message suitable for 'protocol', and returns that encoded form in a
5303 * buffer owned by the caller. */
5305 ofputil_encode_table_mod(const struct ofputil_table_mod *tm,
5306 enum ofputil_protocol protocol)
5308 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
5311 switch (ofp_version) {
5312 case OFP10_VERSION: {
5313 ovs_fatal(0, "table mod needs OpenFlow 1.1 or later "
5314 "(\'-O OpenFlow11\')");
5319 case OFP13_VERSION: {
5320 struct ofp11_table_mod *otm;
5322 b = ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD, ofp_version, 0);
5323 otm = ofpbuf_put_zeros(b, sizeof *otm);
5324 otm->table_id = tm->table_id;
5325 otm->config = ofputil_encode_table_config(tm->miss, tm->eviction,
5326 tm->vacancy, ofp_version);
5330 case OFP15_VERSION: {
5331 struct ofp14_table_mod *otm;
5332 struct ofp14_table_mod_prop_eviction *ote;
5333 struct ofp14_table_mod_prop_vacancy *otv;
5335 b = ofpraw_alloc(OFPRAW_OFPT14_TABLE_MOD, ofp_version, 0);
5336 otm = ofpbuf_put_zeros(b, sizeof *otm);
5337 otm->table_id = tm->table_id;
5338 otm->config = ofputil_encode_table_config(tm->miss, tm->eviction,
5339 tm->vacancy, ofp_version);
5341 if (tm->eviction_flags != UINT32_MAX) {
5342 ote = ofpbuf_put_zeros(b, sizeof *ote);
5343 ote->type = htons(OFPTMPT14_EVICTION);
5344 ote->length = htons(sizeof *ote);
5345 ote->flags = htonl(tm->eviction_flags);
5347 if (tm->vacancy == OFPUTIL_TABLE_VACANCY_ON) {
5348 otv = ofpbuf_put_zeros(b, sizeof *otv);
5349 otv->type = htons(OFPTMPT14_VACANCY);
5350 otv->length = htons(sizeof *otv);
5351 otv->vacancy_down = tm->table_vacancy.vacancy_down;
5352 otv->vacancy_up = tm->table_vacancy.vacancy_up;
5363 /* ofputil_role_request */
5365 /* Decodes the OpenFlow "role request" or "role reply" message in '*oh' into
5366 * an abstract form in '*rr'. Returns 0 if successful, otherwise an
5367 * OFPERR_* value. */
5369 ofputil_decode_role_message(const struct ofp_header *oh,
5370 struct ofputil_role_request *rr)
5375 ofpbuf_use_const(&b, oh, ntohs(oh->length));
5376 raw = ofpraw_pull_assert(&b);
5378 if (raw == OFPRAW_OFPT12_ROLE_REQUEST ||
5379 raw == OFPRAW_OFPT12_ROLE_REPLY) {
5380 const struct ofp12_role_request *orr = b.msg;
5382 if (orr->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
5383 orr->role != htonl(OFPCR12_ROLE_EQUAL) &&
5384 orr->role != htonl(OFPCR12_ROLE_MASTER) &&
5385 orr->role != htonl(OFPCR12_ROLE_SLAVE)) {
5386 return OFPERR_OFPRRFC_BAD_ROLE;
5389 rr->role = ntohl(orr->role);
5390 if (raw == OFPRAW_OFPT12_ROLE_REQUEST
5391 ? orr->role == htonl(OFPCR12_ROLE_NOCHANGE)
5392 : orr->generation_id == OVS_BE64_MAX) {
5393 rr->have_generation_id = false;
5394 rr->generation_id = 0;
5396 rr->have_generation_id = true;
5397 rr->generation_id = ntohll(orr->generation_id);
5399 } else if (raw == OFPRAW_NXT_ROLE_REQUEST ||
5400 raw == OFPRAW_NXT_ROLE_REPLY) {
5401 const struct nx_role_request *nrr = b.msg;
5403 BUILD_ASSERT(NX_ROLE_OTHER + 1 == OFPCR12_ROLE_EQUAL);
5404 BUILD_ASSERT(NX_ROLE_MASTER + 1 == OFPCR12_ROLE_MASTER);
5405 BUILD_ASSERT(NX_ROLE_SLAVE + 1 == OFPCR12_ROLE_SLAVE);
5407 if (nrr->role != htonl(NX_ROLE_OTHER) &&
5408 nrr->role != htonl(NX_ROLE_MASTER) &&
5409 nrr->role != htonl(NX_ROLE_SLAVE)) {
5410 return OFPERR_OFPRRFC_BAD_ROLE;
5413 rr->role = ntohl(nrr->role) + 1;
5414 rr->have_generation_id = false;
5415 rr->generation_id = 0;
5423 /* Returns an encoded form of a role reply suitable for the "request" in a
5424 * buffer owned by the caller. */
5426 ofputil_encode_role_reply(const struct ofp_header *request,
5427 const struct ofputil_role_request *rr)
5432 raw = ofpraw_decode_assert(request);
5433 if (raw == OFPRAW_OFPT12_ROLE_REQUEST) {
5434 struct ofp12_role_request *orr;
5436 buf = ofpraw_alloc_reply(OFPRAW_OFPT12_ROLE_REPLY, request, 0);
5437 orr = ofpbuf_put_zeros(buf, sizeof *orr);
5439 orr->role = htonl(rr->role);
5440 orr->generation_id = htonll(rr->have_generation_id
5443 } else if (raw == OFPRAW_NXT_ROLE_REQUEST) {
5444 struct nx_role_request *nrr;
5446 BUILD_ASSERT(NX_ROLE_OTHER == OFPCR12_ROLE_EQUAL - 1);
5447 BUILD_ASSERT(NX_ROLE_MASTER == OFPCR12_ROLE_MASTER - 1);
5448 BUILD_ASSERT(NX_ROLE_SLAVE == OFPCR12_ROLE_SLAVE - 1);
5450 buf = ofpraw_alloc_reply(OFPRAW_NXT_ROLE_REPLY, request, 0);
5451 nrr = ofpbuf_put_zeros(buf, sizeof *nrr);
5452 nrr->role = htonl(rr->role - 1);
5460 /* Encodes "role status" message 'status' for sending in the given
5461 * 'protocol'. Returns the role status message, if 'protocol' supports them,
5462 * otherwise a null pointer. */
5464 ofputil_encode_role_status(const struct ofputil_role_status *status,
5465 enum ofputil_protocol protocol)
5467 enum ofp_version version;
5469 version = ofputil_protocol_to_ofp_version(protocol);
5470 if (version >= OFP14_VERSION) {
5471 struct ofp14_role_status *rstatus;
5474 buf = ofpraw_alloc_xid(OFPRAW_OFPT14_ROLE_STATUS, version, htonl(0),
5476 rstatus = ofpbuf_put_zeros(buf, sizeof *rstatus);
5477 rstatus->role = htonl(status->role);
5478 rstatus->reason = status->reason;
5479 rstatus->generation_id = htonll(status->generation_id);
5488 ofputil_decode_role_status(const struct ofp_header *oh,
5489 struct ofputil_role_status *rs)
5493 const struct ofp14_role_status *r;
5495 ofpbuf_use_const(&b, oh, ntohs(oh->length));
5496 raw = ofpraw_pull_assert(&b);
5497 ovs_assert(raw == OFPRAW_OFPT14_ROLE_STATUS);
5500 if (r->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
5501 r->role != htonl(OFPCR12_ROLE_EQUAL) &&
5502 r->role != htonl(OFPCR12_ROLE_MASTER) &&
5503 r->role != htonl(OFPCR12_ROLE_SLAVE)) {
5504 return OFPERR_OFPRRFC_BAD_ROLE;
5507 rs->role = ntohl(r->role);
5508 rs->generation_id = ntohll(r->generation_id);
5509 rs->reason = r->reason;
5514 /* Encodes 'rf' according to 'protocol', and returns the encoded message.
5515 * 'protocol' must be for OpenFlow 1.4 or later. */
5517 ofputil_encode_requestforward(const struct ofputil_requestforward *rf,
5518 enum ofputil_protocol protocol)
5520 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
5521 struct ofpbuf *inner;
5523 switch (rf->reason) {
5524 case OFPRFR_GROUP_MOD:
5525 inner = ofputil_encode_group_mod(ofp_version, rf->group_mod);
5528 case OFPRFR_METER_MOD:
5529 inner = ofputil_encode_meter_mod(ofp_version, rf->meter_mod);
5532 case OFPRFR_N_REASONS:
5537 struct ofp_header *inner_oh = inner->data;
5538 inner_oh->xid = rf->xid;
5539 inner_oh->length = htons(inner->size);
5541 struct ofpbuf *outer = ofpraw_alloc_xid(OFPRAW_OFPT14_REQUESTFORWARD,
5542 ofp_version, htonl(0),
5544 ofpbuf_put(outer, inner->data, inner->size);
5545 ofpbuf_delete(inner);
5550 /* Decodes OFPT_REQUESTFORWARD message 'outer'. On success, puts the decoded
5551 * form into '*rf' and returns 0, and the caller is later responsible for
5552 * freeing the content of 'rf', with ofputil_destroy_requestforward(rf). On
5553 * failure, returns an ofperr and '*rf' is indeterminate. */
5555 ofputil_decode_requestforward(const struct ofp_header *outer,
5556 struct ofputil_requestforward *rf)
5561 ofpbuf_use_const(&b, outer, ntohs(outer->length));
5563 /* Skip past outer message. */
5564 enum ofpraw outer_raw = ofpraw_pull_assert(&b);
5565 ovs_assert(outer_raw == OFPRAW_OFPT14_REQUESTFORWARD);
5567 /* Validate inner message. */
5568 if (b.size < sizeof(struct ofp_header)) {
5569 return OFPERR_OFPBFC_MSG_BAD_LEN;
5571 const struct ofp_header *inner = b.data;
5572 unsigned int inner_len = ntohs(inner->length);
5573 if (inner_len < sizeof(struct ofp_header) || inner_len > b.size) {
5574 return OFPERR_OFPBFC_MSG_BAD_LEN;
5576 if (inner->version != outer->version) {
5577 return OFPERR_OFPBRC_BAD_VERSION;
5580 /* Parse inner message. */
5582 error = ofptype_decode(&type, inner);
5587 rf->xid = inner->xid;
5588 if (type == OFPTYPE_GROUP_MOD) {
5589 rf->reason = OFPRFR_GROUP_MOD;
5590 rf->group_mod = xmalloc(sizeof *rf->group_mod);
5591 error = ofputil_decode_group_mod(inner, rf->group_mod);
5593 free(rf->group_mod);
5596 } else if (type == OFPTYPE_METER_MOD) {
5597 rf->reason = OFPRFR_METER_MOD;
5598 rf->meter_mod = xmalloc(sizeof *rf->meter_mod);
5599 ofpbuf_init(&rf->bands, 64);
5600 error = ofputil_decode_meter_mod(inner, rf->meter_mod, &rf->bands);
5602 free(rf->meter_mod);
5603 ofpbuf_uninit(&rf->bands);
5607 return OFPERR_OFPBFC_MSG_UNSUP;
5613 /* Frees the content of 'rf', which should have been initialized through a
5614 * successful call to ofputil_decode_requestforward(). */
5616 ofputil_destroy_requestforward(struct ofputil_requestforward *rf)
5622 switch (rf->reason) {
5623 case OFPRFR_GROUP_MOD:
5624 ofputil_uninit_group_mod(rf->group_mod);
5625 free(rf->group_mod);
5628 case OFPRFR_METER_MOD:
5629 ofpbuf_uninit(&rf->bands);
5630 free(rf->meter_mod);
5633 case OFPRFR_N_REASONS:
5640 /* OpenFlow 1.0 and 1.1 don't distinguish between a field that cannot be
5641 * matched and a field that must be wildcarded. This function returns a bitmap
5642 * that contains both kinds of fields. */
5643 static struct mf_bitmap
5644 wild_or_nonmatchable_fields(const struct ofputil_table_features *features)
5646 struct mf_bitmap wc = features->match;
5647 bitmap_not(wc.bm, MFF_N_IDS);
5648 bitmap_or(wc.bm, features->wildcard.bm, MFF_N_IDS);
5652 struct ofp10_wc_map {
5653 enum ofp10_flow_wildcards wc10;
5654 enum mf_field_id mf;
5657 static const struct ofp10_wc_map ofp10_wc_map[] = {
5658 { OFPFW10_IN_PORT, MFF_IN_PORT },
5659 { OFPFW10_DL_VLAN, MFF_VLAN_VID },
5660 { OFPFW10_DL_SRC, MFF_ETH_SRC },
5661 { OFPFW10_DL_DST, MFF_ETH_DST},
5662 { OFPFW10_DL_TYPE, MFF_ETH_TYPE },
5663 { OFPFW10_NW_PROTO, MFF_IP_PROTO },
5664 { OFPFW10_TP_SRC, MFF_TCP_SRC },
5665 { OFPFW10_TP_DST, MFF_TCP_DST },
5666 { OFPFW10_NW_SRC_MASK, MFF_IPV4_SRC },
5667 { OFPFW10_NW_DST_MASK, MFF_IPV4_DST },
5668 { OFPFW10_DL_VLAN_PCP, MFF_VLAN_PCP },
5669 { OFPFW10_NW_TOS, MFF_IP_DSCP },
5673 mf_bitmap_to_of10(const struct mf_bitmap *fields)
5675 const struct ofp10_wc_map *p;
5678 for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
5679 if (bitmap_is_set(fields->bm, p->mf)) {
5686 static struct mf_bitmap
5687 mf_bitmap_from_of10(ovs_be32 wc10_)
5689 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
5690 const struct ofp10_wc_map *p;
5691 uint32_t wc10 = ntohl(wc10_);
5693 for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
5694 if (wc10 & p->wc10) {
5695 bitmap_set1(fields.bm, p->mf);
5702 ofputil_put_ofp10_table_stats(const struct ofputil_table_stats *stats,
5703 const struct ofputil_table_features *features,
5706 struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
5707 struct ofp10_table_stats *out;
5709 out = ofpbuf_put_zeros(buf, sizeof *out);
5710 out->table_id = features->table_id;
5711 ovs_strlcpy(out->name, features->name, sizeof out->name);
5712 out->wildcards = mf_bitmap_to_of10(&wc);
5713 out->max_entries = htonl(features->max_entries);
5714 out->active_count = htonl(stats->active_count);
5715 put_32aligned_be64(&out->lookup_count, htonll(stats->lookup_count));
5716 put_32aligned_be64(&out->matched_count, htonll(stats->matched_count));
5719 struct ofp11_wc_map {
5720 enum ofp11_flow_match_fields wc11;
5721 enum mf_field_id mf;
5724 static const struct ofp11_wc_map ofp11_wc_map[] = {
5725 { OFPFMF11_IN_PORT, MFF_IN_PORT },
5726 { OFPFMF11_DL_VLAN, MFF_VLAN_VID },
5727 { OFPFMF11_DL_VLAN_PCP, MFF_VLAN_PCP },
5728 { OFPFMF11_DL_TYPE, MFF_ETH_TYPE },
5729 { OFPFMF11_NW_TOS, MFF_IP_DSCP },
5730 { OFPFMF11_NW_PROTO, MFF_IP_PROTO },
5731 { OFPFMF11_TP_SRC, MFF_TCP_SRC },
5732 { OFPFMF11_TP_DST, MFF_TCP_DST },
5733 { OFPFMF11_MPLS_LABEL, MFF_MPLS_LABEL },
5734 { OFPFMF11_MPLS_TC, MFF_MPLS_TC },
5735 /* I don't know what OFPFMF11_TYPE means. */
5736 { OFPFMF11_DL_SRC, MFF_ETH_SRC },
5737 { OFPFMF11_DL_DST, MFF_ETH_DST },
5738 { OFPFMF11_NW_SRC, MFF_IPV4_SRC },
5739 { OFPFMF11_NW_DST, MFF_IPV4_DST },
5740 { OFPFMF11_METADATA, MFF_METADATA },
5744 mf_bitmap_to_of11(const struct mf_bitmap *fields)
5746 const struct ofp11_wc_map *p;
5749 for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
5750 if (bitmap_is_set(fields->bm, p->mf)) {
5757 static struct mf_bitmap
5758 mf_bitmap_from_of11(ovs_be32 wc11_)
5760 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
5761 const struct ofp11_wc_map *p;
5762 uint32_t wc11 = ntohl(wc11_);
5764 for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
5765 if (wc11 & p->wc11) {
5766 bitmap_set1(fields.bm, p->mf);
5773 ofputil_put_ofp11_table_stats(const struct ofputil_table_stats *stats,
5774 const struct ofputil_table_features *features,
5777 struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
5778 struct ofp11_table_stats *out;
5780 out = ofpbuf_put_zeros(buf, sizeof *out);
5781 out->table_id = features->table_id;
5782 ovs_strlcpy(out->name, features->name, sizeof out->name);
5783 out->wildcards = mf_bitmap_to_of11(&wc);
5784 out->match = mf_bitmap_to_of11(&features->match);
5785 out->instructions = ovsinst_bitmap_to_openflow(
5786 features->nonmiss.instructions, OFP11_VERSION);
5787 out->write_actions = ofpact_bitmap_to_openflow(
5788 features->nonmiss.write.ofpacts, OFP11_VERSION);
5789 out->apply_actions = ofpact_bitmap_to_openflow(
5790 features->nonmiss.apply.ofpacts, OFP11_VERSION);
5791 out->config = htonl(features->miss_config);
5792 out->max_entries = htonl(features->max_entries);
5793 out->active_count = htonl(stats->active_count);
5794 out->lookup_count = htonll(stats->lookup_count);
5795 out->matched_count = htonll(stats->matched_count);
5799 ofputil_put_ofp12_table_stats(const struct ofputil_table_stats *stats,
5800 const struct ofputil_table_features *features,
5803 struct ofp12_table_stats *out;
5805 out = ofpbuf_put_zeros(buf, sizeof *out);
5806 out->table_id = features->table_id;
5807 ovs_strlcpy(out->name, features->name, sizeof out->name);
5808 out->match = oxm_bitmap_from_mf_bitmap(&features->match, OFP12_VERSION);
5809 out->wildcards = oxm_bitmap_from_mf_bitmap(&features->wildcard,
5811 out->write_actions = ofpact_bitmap_to_openflow(
5812 features->nonmiss.write.ofpacts, OFP12_VERSION);
5813 out->apply_actions = ofpact_bitmap_to_openflow(
5814 features->nonmiss.apply.ofpacts, OFP12_VERSION);
5815 out->write_setfields = oxm_bitmap_from_mf_bitmap(
5816 &features->nonmiss.write.set_fields, OFP12_VERSION);
5817 out->apply_setfields = oxm_bitmap_from_mf_bitmap(
5818 &features->nonmiss.apply.set_fields, OFP12_VERSION);
5819 out->metadata_match = features->metadata_match;
5820 out->metadata_write = features->metadata_write;
5821 out->instructions = ovsinst_bitmap_to_openflow(
5822 features->nonmiss.instructions, OFP12_VERSION);
5823 out->config = ofputil_encode_table_config(features->miss_config,
5824 OFPUTIL_TABLE_EVICTION_DEFAULT,
5825 OFPUTIL_TABLE_VACANCY_DEFAULT,
5827 out->max_entries = htonl(features->max_entries);
5828 out->active_count = htonl(stats->active_count);
5829 out->lookup_count = htonll(stats->lookup_count);
5830 out->matched_count = htonll(stats->matched_count);
5834 ofputil_put_ofp13_table_stats(const struct ofputil_table_stats *stats,
5837 struct ofp13_table_stats *out;
5839 out = ofpbuf_put_zeros(buf, sizeof *out);
5840 out->table_id = stats->table_id;
5841 out->active_count = htonl(stats->active_count);
5842 out->lookup_count = htonll(stats->lookup_count);
5843 out->matched_count = htonll(stats->matched_count);
5847 ofputil_encode_table_stats_reply(const struct ofp_header *request)
5849 return ofpraw_alloc_stats_reply(request, 0);
5853 ofputil_append_table_stats_reply(struct ofpbuf *reply,
5854 const struct ofputil_table_stats *stats,
5855 const struct ofputil_table_features *features)
5857 struct ofp_header *oh = reply->header;
5859 ovs_assert(stats->table_id == features->table_id);
5861 switch ((enum ofp_version) oh->version) {
5863 ofputil_put_ofp10_table_stats(stats, features, reply);
5867 ofputil_put_ofp11_table_stats(stats, features, reply);
5871 ofputil_put_ofp12_table_stats(stats, features, reply);
5877 ofputil_put_ofp13_table_stats(stats, reply);
5886 ofputil_decode_ofp10_table_stats(struct ofpbuf *msg,
5887 struct ofputil_table_stats *stats,
5888 struct ofputil_table_features *features)
5890 struct ofp10_table_stats *ots;
5892 ots = ofpbuf_try_pull(msg, sizeof *ots);
5894 return OFPERR_OFPBRC_BAD_LEN;
5897 features->table_id = ots->table_id;
5898 ovs_strlcpy(features->name, ots->name, sizeof features->name);
5899 features->max_entries = ntohl(ots->max_entries);
5900 features->match = features->wildcard = mf_bitmap_from_of10(ots->wildcards);
5902 stats->table_id = ots->table_id;
5903 stats->active_count = ntohl(ots->active_count);
5904 stats->lookup_count = ntohll(get_32aligned_be64(&ots->lookup_count));
5905 stats->matched_count = ntohll(get_32aligned_be64(&ots->matched_count));
5911 ofputil_decode_ofp11_table_stats(struct ofpbuf *msg,
5912 struct ofputil_table_stats *stats,
5913 struct ofputil_table_features *features)
5915 struct ofp11_table_stats *ots;
5917 ots = ofpbuf_try_pull(msg, sizeof *ots);
5919 return OFPERR_OFPBRC_BAD_LEN;
5922 features->table_id = ots->table_id;
5923 ovs_strlcpy(features->name, ots->name, sizeof features->name);
5924 features->max_entries = ntohl(ots->max_entries);
5925 features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
5926 ots->instructions, OFP11_VERSION);
5927 features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
5928 ots->write_actions, OFP11_VERSION);
5929 features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
5930 ots->write_actions, OFP11_VERSION);
5931 features->miss = features->nonmiss;
5932 features->miss_config = ofputil_decode_table_miss(ots->config,
5934 features->match = mf_bitmap_from_of11(ots->match);
5935 features->wildcard = mf_bitmap_from_of11(ots->wildcards);
5936 bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
5938 stats->table_id = ots->table_id;
5939 stats->active_count = ntohl(ots->active_count);
5940 stats->lookup_count = ntohll(ots->lookup_count);
5941 stats->matched_count = ntohll(ots->matched_count);
5947 ofputil_decode_ofp12_table_stats(struct ofpbuf *msg,
5948 struct ofputil_table_stats *stats,
5949 struct ofputil_table_features *features)
5951 struct ofp12_table_stats *ots;
5953 ots = ofpbuf_try_pull(msg, sizeof *ots);
5955 return OFPERR_OFPBRC_BAD_LEN;
5958 features->table_id = ots->table_id;
5959 ovs_strlcpy(features->name, ots->name, sizeof features->name);
5960 features->metadata_match = ots->metadata_match;
5961 features->metadata_write = ots->metadata_write;
5962 features->miss_config = ofputil_decode_table_miss(ots->config,
5964 features->max_entries = ntohl(ots->max_entries);
5966 features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
5967 ots->instructions, OFP12_VERSION);
5968 features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
5969 ots->write_actions, OFP12_VERSION);
5970 features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
5971 ots->apply_actions, OFP12_VERSION);
5972 features->nonmiss.write.set_fields = oxm_bitmap_to_mf_bitmap(
5973 ots->write_setfields, OFP12_VERSION);
5974 features->nonmiss.apply.set_fields = oxm_bitmap_to_mf_bitmap(
5975 ots->apply_setfields, OFP12_VERSION);
5976 features->miss = features->nonmiss;
5978 features->match = oxm_bitmap_to_mf_bitmap(ots->match, OFP12_VERSION);
5979 features->wildcard = oxm_bitmap_to_mf_bitmap(ots->wildcards,
5981 bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
5983 stats->table_id = ots->table_id;
5984 stats->active_count = ntohl(ots->active_count);
5985 stats->lookup_count = ntohll(ots->lookup_count);
5986 stats->matched_count = ntohll(ots->matched_count);
5992 ofputil_decode_ofp13_table_stats(struct ofpbuf *msg,
5993 struct ofputil_table_stats *stats,
5994 struct ofputil_table_features *features)
5996 struct ofp13_table_stats *ots;
5998 ots = ofpbuf_try_pull(msg, sizeof *ots);
6000 return OFPERR_OFPBRC_BAD_LEN;
6003 features->table_id = ots->table_id;
6005 stats->table_id = ots->table_id;
6006 stats->active_count = ntohl(ots->active_count);
6007 stats->lookup_count = ntohll(ots->lookup_count);
6008 stats->matched_count = ntohll(ots->matched_count);
6014 ofputil_decode_table_stats_reply(struct ofpbuf *msg,
6015 struct ofputil_table_stats *stats,
6016 struct ofputil_table_features *features)
6018 const struct ofp_header *oh;
6021 ofpraw_pull_assert(msg);
6029 memset(stats, 0, sizeof *stats);
6030 memset(features, 0, sizeof *features);
6031 features->supports_eviction = -1;
6032 features->supports_vacancy_events = -1;
6034 switch ((enum ofp_version) oh->version) {
6036 return ofputil_decode_ofp10_table_stats(msg, stats, features);
6039 return ofputil_decode_ofp11_table_stats(msg, stats, features);
6042 return ofputil_decode_ofp12_table_stats(msg, stats, features);
6047 return ofputil_decode_ofp13_table_stats(msg, stats, features);
6054 /* ofputil_flow_monitor_request */
6056 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
6057 * ofputil_flow_monitor_request in 'rq'.
6059 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
6060 * message. Calling this function multiple times for a single 'msg' iterates
6061 * through the requests. The caller must initially leave 'msg''s layer
6062 * pointers null and not modify them between calls.
6064 * Returns 0 if successful, EOF if no requests were left in this 'msg',
6065 * otherwise an OFPERR_* value. */
6067 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
6070 struct nx_flow_monitor_request *nfmr;
6074 ofpraw_pull_assert(msg);
6081 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
6083 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %"PRIu32" "
6084 "leftover bytes at end", msg->size);
6085 return OFPERR_OFPBRC_BAD_LEN;
6088 flags = ntohs(nfmr->flags);
6089 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
6090 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
6091 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
6092 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
6094 return OFPERR_OFPMOFC_BAD_FLAGS;
6097 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
6098 return OFPERR_NXBRC_MUST_BE_ZERO;
6101 rq->id = ntohl(nfmr->id);
6103 rq->out_port = u16_to_ofp(ntohs(nfmr->out_port));
6104 rq->table_id = nfmr->table_id;
6106 return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL, NULL);
6110 ofputil_append_flow_monitor_request(
6111 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
6113 struct nx_flow_monitor_request *nfmr;
6118 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
6121 start_ofs = msg->size;
6122 ofpbuf_put_zeros(msg, sizeof *nfmr);
6123 match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
6125 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
6126 nfmr->id = htonl(rq->id);
6127 nfmr->flags = htons(rq->flags);
6128 nfmr->out_port = htons(ofp_to_u16(rq->out_port));
6129 nfmr->match_len = htons(match_len);
6130 nfmr->table_id = rq->table_id;
6133 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
6134 * into an abstract ofputil_flow_update in 'update'. The caller must have
6135 * initialized update->match to point to space allocated for a match.
6137 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
6138 * actions (except for NXFME_ABBREV, which never includes actions). The caller
6139 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
6140 * will point into the 'ofpacts' buffer.
6142 * Multiple flow updates can be packed into a single OpenFlow message. Calling
6143 * this function multiple times for a single 'msg' iterates through the
6144 * updates. The caller must initially leave 'msg''s layer pointers null and
6145 * not modify them between calls.
6147 * Returns 0 if successful, EOF if no updates were left in this 'msg',
6148 * otherwise an OFPERR_* value. */
6150 ofputil_decode_flow_update(struct ofputil_flow_update *update,
6151 struct ofpbuf *msg, struct ofpbuf *ofpacts)
6153 struct nx_flow_update_header *nfuh;
6154 unsigned int length;
6155 struct ofp_header *oh;
6158 ofpraw_pull_assert(msg);
6161 ofpbuf_clear(ofpacts);
6166 if (msg->size < sizeof(struct nx_flow_update_header)) {
6173 update->event = ntohs(nfuh->event);
6174 length = ntohs(nfuh->length);
6175 if (length > msg->size || length % 8) {
6179 if (update->event == NXFME_ABBREV) {
6180 struct nx_flow_update_abbrev *nfua;
6182 if (length != sizeof *nfua) {
6186 nfua = ofpbuf_pull(msg, sizeof *nfua);
6187 update->xid = nfua->xid;
6189 } else if (update->event == NXFME_ADDED
6190 || update->event == NXFME_DELETED
6191 || update->event == NXFME_MODIFIED) {
6192 struct nx_flow_update_full *nfuf;
6193 unsigned int actions_len;
6194 unsigned int match_len;
6197 if (length < sizeof *nfuf) {
6201 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
6202 match_len = ntohs(nfuf->match_len);
6203 if (sizeof *nfuf + match_len > length) {
6207 update->reason = ntohs(nfuf->reason);
6208 update->idle_timeout = ntohs(nfuf->idle_timeout);
6209 update->hard_timeout = ntohs(nfuf->hard_timeout);
6210 update->table_id = nfuf->table_id;
6211 update->cookie = nfuf->cookie;
6212 update->priority = ntohs(nfuf->priority);
6214 error = nx_pull_match(msg, match_len, update->match, NULL, NULL);
6219 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
6220 error = ofpacts_pull_openflow_actions(msg, actions_len, oh->version,
6226 update->ofpacts = ofpacts->data;
6227 update->ofpacts_len = ofpacts->size;
6230 VLOG_WARN_RL(&bad_ofmsg_rl,
6231 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
6232 ntohs(nfuh->event));
6233 return OFPERR_NXBRC_FM_BAD_EVENT;
6237 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %"PRIu32" "
6238 "leftover bytes at end", msg->size);
6239 return OFPERR_OFPBRC_BAD_LEN;
6243 ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
6245 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
6247 return ntohl(cancel->id);
6251 ofputil_encode_flow_monitor_cancel(uint32_t id)
6253 struct nx_flow_monitor_cancel *nfmc;
6256 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
6257 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
6258 nfmc->id = htonl(id);
6263 ofputil_start_flow_update(struct ovs_list *replies)
6267 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
6271 list_push_back(replies, &msg->list_node);
6275 ofputil_append_flow_update(const struct ofputil_flow_update *update,
6276 struct ovs_list *replies)
6278 enum ofp_version version = ofpmp_version(replies);
6279 struct nx_flow_update_header *nfuh;
6283 msg = ofpbuf_from_list(list_back(replies));
6284 start_ofs = msg->size;
6286 if (update->event == NXFME_ABBREV) {
6287 struct nx_flow_update_abbrev *nfua;
6289 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
6290 nfua->xid = update->xid;
6292 struct nx_flow_update_full *nfuf;
6295 ofpbuf_put_zeros(msg, sizeof *nfuf);
6296 match_len = nx_put_match(msg, update->match, htonll(0), htonll(0));
6297 ofpacts_put_openflow_actions(update->ofpacts, update->ofpacts_len, msg,
6299 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
6300 nfuf->reason = htons(update->reason);
6301 nfuf->priority = htons(update->priority);
6302 nfuf->idle_timeout = htons(update->idle_timeout);
6303 nfuf->hard_timeout = htons(update->hard_timeout);
6304 nfuf->match_len = htons(match_len);
6305 nfuf->table_id = update->table_id;
6306 nfuf->cookie = update->cookie;
6309 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
6310 nfuh->length = htons(msg->size - start_ofs);
6311 nfuh->event = htons(update->event);
6313 ofpmp_postappend(replies, start_ofs);
6317 ofputil_encode_packet_out(const struct ofputil_packet_out *po,
6318 enum ofputil_protocol protocol)
6320 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
6324 size = po->ofpacts_len;
6325 if (po->buffer_id == UINT32_MAX) {
6326 size += po->packet_len;
6329 switch (ofp_version) {
6330 case OFP10_VERSION: {
6331 struct ofp10_packet_out *opo;
6334 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
6335 ofpbuf_put_zeros(msg, sizeof *opo);
6336 actions_ofs = msg->size;
6337 ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
6341 opo->buffer_id = htonl(po->buffer_id);
6342 opo->in_port = htons(ofp_to_u16(po->in_port));
6343 opo->actions_len = htons(msg->size - actions_ofs);
6351 case OFP15_VERSION: {
6352 struct ofp11_packet_out *opo;
6355 msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size);
6356 ofpbuf_put_zeros(msg, sizeof *opo);
6357 len = ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
6360 opo->buffer_id = htonl(po->buffer_id);
6361 opo->in_port = ofputil_port_to_ofp11(po->in_port);
6362 opo->actions_len = htons(len);
6370 if (po->buffer_id == UINT32_MAX) {
6371 ofpbuf_put(msg, po->packet, po->packet_len);
6374 ofpmsg_update_length(msg);
6379 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
6381 make_echo_request(enum ofp_version ofp_version)
6383 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
6387 /* Creates and returns an OFPT_ECHO_REPLY message matching the
6388 * OFPT_ECHO_REQUEST message in 'rq'. */
6390 make_echo_reply(const struct ofp_header *rq)
6392 struct ofpbuf rq_buf;
6393 struct ofpbuf *reply;
6395 ofpbuf_use_const(&rq_buf, rq, ntohs(rq->length));
6396 ofpraw_pull_assert(&rq_buf);
6398 reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY, rq, rq_buf.size);
6399 ofpbuf_put(reply, rq_buf.data, rq_buf.size);
6404 ofputil_encode_barrier_request(enum ofp_version ofp_version)
6408 switch (ofp_version) {
6414 type = OFPRAW_OFPT11_BARRIER_REQUEST;
6418 type = OFPRAW_OFPT10_BARRIER_REQUEST;
6425 return ofpraw_alloc(type, ofp_version, 0);
6429 ofputil_frag_handling_to_string(enum ofputil_frag_handling frag)
6432 case OFPUTIL_FRAG_NORMAL: return "normal";
6433 case OFPUTIL_FRAG_DROP: return "drop";
6434 case OFPUTIL_FRAG_REASM: return "reassemble";
6435 case OFPUTIL_FRAG_NX_MATCH: return "nx-match";
6442 ofputil_frag_handling_from_string(const char *s,
6443 enum ofputil_frag_handling *frag)
6445 if (!strcasecmp(s, "normal")) {
6446 *frag = OFPUTIL_FRAG_NORMAL;
6447 } else if (!strcasecmp(s, "drop")) {
6448 *frag = OFPUTIL_FRAG_DROP;
6449 } else if (!strcasecmp(s, "reassemble")) {
6450 *frag = OFPUTIL_FRAG_REASM;
6451 } else if (!strcasecmp(s, "nx-match")) {
6452 *frag = OFPUTIL_FRAG_NX_MATCH;
6459 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
6460 * port number and stores the latter in '*ofp10_port', for the purpose of
6461 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
6462 * otherwise an OFPERR_* number. On error, stores OFPP_NONE in '*ofp10_port'.
6464 * See the definition of OFP11_MAX for an explanation of the mapping. */
6466 ofputil_port_from_ofp11(ovs_be32 ofp11_port, ofp_port_t *ofp10_port)
6468 uint32_t ofp11_port_h = ntohl(ofp11_port);
6470 if (ofp11_port_h < ofp_to_u16(OFPP_MAX)) {
6471 *ofp10_port = u16_to_ofp(ofp11_port_h);
6473 } else if (ofp11_port_h >= ofp11_to_u32(OFPP11_MAX)) {
6474 *ofp10_port = u16_to_ofp(ofp11_port_h - OFPP11_OFFSET);
6477 *ofp10_port = OFPP_NONE;
6478 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
6479 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
6480 ofp11_port_h, ofp_to_u16(OFPP_MAX) - 1,
6481 ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
6482 return OFPERR_OFPBAC_BAD_OUT_PORT;
6486 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
6487 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
6489 * See the definition of OFP11_MAX for an explanation of the mapping. */
6491 ofputil_port_to_ofp11(ofp_port_t ofp10_port)
6493 return htonl(ofp_to_u16(ofp10_port) < ofp_to_u16(OFPP_MAX)
6494 ? ofp_to_u16(ofp10_port)
6495 : ofp_to_u16(ofp10_port) + OFPP11_OFFSET);
6498 #define OFPUTIL_NAMED_PORTS \
6499 OFPUTIL_NAMED_PORT(IN_PORT) \
6500 OFPUTIL_NAMED_PORT(TABLE) \
6501 OFPUTIL_NAMED_PORT(NORMAL) \
6502 OFPUTIL_NAMED_PORT(FLOOD) \
6503 OFPUTIL_NAMED_PORT(ALL) \
6504 OFPUTIL_NAMED_PORT(CONTROLLER) \
6505 OFPUTIL_NAMED_PORT(LOCAL) \
6506 OFPUTIL_NAMED_PORT(ANY) \
6507 OFPUTIL_NAMED_PORT(UNSET)
6509 /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */
6510 #define OFPUTIL_NAMED_PORTS_WITH_NONE \
6511 OFPUTIL_NAMED_PORTS \
6512 OFPUTIL_NAMED_PORT(NONE)
6514 /* Stores the port number represented by 's' into '*portp'. 's' may be an
6515 * integer or, for reserved ports, the standard OpenFlow name for the port
6518 * Returns true if successful, false if 's' is not a valid OpenFlow port number
6519 * or name. The caller should issue an error message in this case, because
6520 * this function usually does not. (This gives the caller an opportunity to
6521 * look up the port name another way, e.g. by contacting the switch and listing
6522 * the names of all its ports).
6524 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
6525 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
6526 * range as described in include/openflow/openflow-1.1.h. */
6528 ofputil_port_from_string(const char *s, ofp_port_t *portp)
6530 unsigned int port32; /* int is at least 32 bits wide. */
6533 VLOG_WARN("Negative value %s is not a valid port number.", s);
6537 if (str_to_uint(s, 10, &port32)) {
6538 if (port32 < ofp_to_u16(OFPP_MAX)) {
6540 } else if (port32 < ofp_to_u16(OFPP_FIRST_RESV)) {
6541 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
6542 "be translated to %u when talking to an OF1.1 or "
6543 "later controller", port32, port32 + OFPP11_OFFSET);
6544 } else if (port32 <= ofp_to_u16(OFPP_LAST_RESV)) {
6545 char name[OFP_MAX_PORT_NAME_LEN];
6547 ofputil_port_to_string(u16_to_ofp(port32), name, sizeof name);
6548 VLOG_WARN_ONCE("referring to port %s as %"PRIu32" is deprecated "
6549 "for compatibility with OpenFlow 1.1 and later",
6551 } else if (port32 < ofp11_to_u32(OFPP11_MAX)) {
6552 VLOG_WARN("port %u is outside the supported range 0 through "
6553 "%"PRIx16" or 0x%x through 0x%"PRIx32, port32,
6554 UINT16_MAX, ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
6557 port32 -= OFPP11_OFFSET;
6560 *portp = u16_to_ofp(port32);
6567 static const struct pair pairs[] = {
6568 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
6569 OFPUTIL_NAMED_PORTS_WITH_NONE
6570 #undef OFPUTIL_NAMED_PORT
6572 const struct pair *p;
6574 for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) {
6575 if (!strcasecmp(s, p->name)) {
6584 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
6585 * Most ports' string representation is just the port number, but for special
6586 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
6588 ofputil_format_port(ofp_port_t port, struct ds *s)
6590 char name[OFP_MAX_PORT_NAME_LEN];
6592 ofputil_port_to_string(port, name, sizeof name);
6593 ds_put_cstr(s, name);
6596 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
6597 * representation of OpenFlow port number 'port'. Most ports are represented
6598 * as just the port number, but special ports, e.g. OFPP_LOCAL, are represented
6599 * by name, e.g. "LOCAL". */
6601 ofputil_port_to_string(ofp_port_t port,
6602 char namebuf[OFP_MAX_PORT_NAME_LEN], size_t bufsize)
6605 #define OFPUTIL_NAMED_PORT(NAME) \
6607 ovs_strlcpy(namebuf, #NAME, bufsize); \
6610 #undef OFPUTIL_NAMED_PORT
6613 snprintf(namebuf, bufsize, "%"PRIu16, port);
6618 /* Stores the group id represented by 's' into '*group_idp'. 's' may be an
6619 * integer or, for reserved group IDs, the standard OpenFlow name for the group
6620 * (either "ANY" or "ALL").
6622 * Returns true if successful, false if 's' is not a valid OpenFlow group ID or
6625 ofputil_group_from_string(const char *s, uint32_t *group_idp)
6627 if (!strcasecmp(s, "any")) {
6628 *group_idp = OFPG_ANY;
6629 } else if (!strcasecmp(s, "all")) {
6630 *group_idp = OFPG_ALL;
6631 } else if (!str_to_uint(s, 10, group_idp)) {
6632 VLOG_WARN("%s is not a valid group ID. (Valid group IDs are "
6633 "32-bit nonnegative integers or the keywords ANY or "
6641 /* Appends to 's' a string representation of the OpenFlow group ID 'group_id'.
6642 * Most groups' string representation is just the number, but for special
6643 * groups, e.g. OFPG_ALL, it is the name, e.g. "ALL". */
6645 ofputil_format_group(uint32_t group_id, struct ds *s)
6647 char name[MAX_GROUP_NAME_LEN];
6649 ofputil_group_to_string(group_id, name, sizeof name);
6650 ds_put_cstr(s, name);
6654 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
6655 * representation of OpenFlow group ID 'group_id'. Most group are represented
6656 * as just their number, but special groups, e.g. OFPG_ALL, are represented
6657 * by name, e.g. "ALL". */
6659 ofputil_group_to_string(uint32_t group_id,
6660 char namebuf[MAX_GROUP_NAME_LEN + 1], size_t bufsize)
6664 ovs_strlcpy(namebuf, "ALL", bufsize);
6668 ovs_strlcpy(namebuf, "ANY", bufsize);
6672 snprintf(namebuf, bufsize, "%"PRIu32, group_id);
6677 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
6678 * 'ofp_version', tries to pull the first element from the array. If
6679 * successful, initializes '*pp' with an abstract representation of the
6680 * port and returns 0. If no ports remain to be decoded, returns EOF.
6681 * On an error, returns a positive OFPERR_* value. */
6683 ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
6684 struct ofputil_phy_port *pp)
6686 memset(pp, 0, sizeof *pp);
6688 switch (ofp_version) {
6689 case OFP10_VERSION: {
6690 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
6691 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
6695 case OFP13_VERSION: {
6696 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
6697 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
6701 return b->size ? ofputil_pull_ofp14_port(pp, b) : EOF;
6708 ofputil_normalize_match__(struct match *match, bool may_log)
6711 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
6712 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
6713 MAY_NW_PROTO = 1 << 2, /* nw_proto */
6714 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
6715 MAY_ARP_SHA = 1 << 4, /* arp_sha */
6716 MAY_ARP_THA = 1 << 5, /* arp_tha */
6717 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
6718 MAY_ND_TARGET = 1 << 7, /* nd_target */
6719 MAY_MPLS = 1 << 8, /* mpls label and tc */
6722 struct flow_wildcards wc;
6724 /* Figure out what fields may be matched. */
6725 if (match->flow.dl_type == htons(ETH_TYPE_IP)) {
6726 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
6727 if (match->flow.nw_proto == IPPROTO_TCP ||
6728 match->flow.nw_proto == IPPROTO_UDP ||
6729 match->flow.nw_proto == IPPROTO_SCTP ||
6730 match->flow.nw_proto == IPPROTO_ICMP) {
6731 may_match |= MAY_TP_ADDR;
6733 } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
6734 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
6735 if (match->flow.nw_proto == IPPROTO_TCP ||
6736 match->flow.nw_proto == IPPROTO_UDP ||
6737 match->flow.nw_proto == IPPROTO_SCTP) {
6738 may_match |= MAY_TP_ADDR;
6739 } else if (match->flow.nw_proto == IPPROTO_ICMPV6) {
6740 may_match |= MAY_TP_ADDR;
6741 if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
6742 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
6743 } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
6744 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
6747 } else if (match->flow.dl_type == htons(ETH_TYPE_ARP) ||
6748 match->flow.dl_type == htons(ETH_TYPE_RARP)) {
6749 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
6750 } else if (eth_type_mpls(match->flow.dl_type)) {
6751 may_match = MAY_MPLS;
6756 /* Clear the fields that may not be matched. */
6758 if (!(may_match & MAY_NW_ADDR)) {
6759 wc.masks.nw_src = wc.masks.nw_dst = htonl(0);
6761 if (!(may_match & MAY_TP_ADDR)) {
6762 wc.masks.tp_src = wc.masks.tp_dst = htons(0);
6764 if (!(may_match & MAY_NW_PROTO)) {
6765 wc.masks.nw_proto = 0;
6767 if (!(may_match & MAY_IPVx)) {
6768 wc.masks.nw_tos = 0;
6769 wc.masks.nw_ttl = 0;
6771 if (!(may_match & MAY_ARP_SHA)) {
6772 WC_UNMASK_FIELD(&wc, arp_sha);
6774 if (!(may_match & MAY_ARP_THA)) {
6775 WC_UNMASK_FIELD(&wc, arp_tha);
6777 if (!(may_match & MAY_IPV6)) {
6778 wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any;
6779 wc.masks.ipv6_label = htonl(0);
6781 if (!(may_match & MAY_ND_TARGET)) {
6782 wc.masks.nd_target = in6addr_any;
6784 if (!(may_match & MAY_MPLS)) {
6785 memset(wc.masks.mpls_lse, 0, sizeof wc.masks.mpls_lse);
6788 /* Log any changes. */
6789 if (!flow_wildcards_equal(&wc, &match->wc)) {
6790 bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
6791 char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL;
6794 match_zero_wildcarded_fields(match);
6797 char *post = match_to_string(match, OFP_DEFAULT_PRIORITY);
6798 VLOG_INFO("normalization changed ofp_match, details:");
6799 VLOG_INFO(" pre: %s", pre);
6800 VLOG_INFO("post: %s", post);
6807 /* "Normalizes" the wildcards in 'match'. That means:
6809 * 1. If the type of level N is known, then only the valid fields for that
6810 * level may be specified. For example, ARP does not have a TOS field,
6811 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
6812 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
6813 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
6816 * 2. If the type of level N is not known (or not understood by Open
6817 * vSwitch), then no fields at all for that level may be specified. For
6818 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
6819 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
6822 * If this function changes 'match', it logs a rate-limited informational
6825 ofputil_normalize_match(struct match *match)
6827 ofputil_normalize_match__(match, true);
6830 /* Same as ofputil_normalize_match() without the logging. Thus, this function
6831 * is suitable for a program's internal use, whereas ofputil_normalize_match()
6832 * sense for use on flows received from elsewhere (so that a bug in the program
6833 * that sent them can be reported and corrected). */
6835 ofputil_normalize_match_quiet(struct match *match)
6837 ofputil_normalize_match__(match, false);
6840 /* Parses a key or a key-value pair from '*stringp'.
6842 * On success: Stores the key into '*keyp'. Stores the value, if present, into
6843 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
6844 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
6845 * are substrings of '*stringp' created by replacing some of its bytes by null
6846 * terminators. Returns true.
6848 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
6849 * NULL and returns false. */
6851 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
6853 char *pos, *key, *value;
6857 pos += strspn(pos, ", \t\r\n");
6859 *keyp = *valuep = NULL;
6864 key_len = strcspn(pos, ":=(, \t\r\n");
6865 if (key[key_len] == ':' || key[key_len] == '=') {
6866 /* The value can be separated by a colon. */
6869 value = key + key_len + 1;
6870 value_len = strcspn(value, ", \t\r\n");
6871 pos = value + value_len + (value[value_len] != '\0');
6872 value[value_len] = '\0';
6873 } else if (key[key_len] == '(') {
6874 /* The value can be surrounded by balanced parentheses. The outermost
6875 * set of parentheses is removed. */
6879 value = key + key_len + 1;
6880 for (value_len = 0; level > 0; value_len++) {
6881 switch (value[value_len]) {
6895 value[value_len - 1] = '\0';
6896 pos = value + value_len;
6898 /* There might be no value at all. */
6899 value = key + key_len; /* Will become the empty string below. */
6900 pos = key + key_len + (key[key_len] != '\0');
6902 key[key_len] = '\0';
6910 /* Encode a dump ports request for 'port', the encoded message
6911 * will be for OpenFlow version 'ofp_version'. Returns message
6912 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
6914 ofputil_encode_dump_ports_request(enum ofp_version ofp_version, ofp_port_t port)
6916 struct ofpbuf *request;
6918 switch (ofp_version) {
6919 case OFP10_VERSION: {
6920 struct ofp10_port_stats_request *req;
6921 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0);
6922 req = ofpbuf_put_zeros(request, sizeof *req);
6923 req->port_no = htons(ofp_to_u16(port));
6930 case OFP15_VERSION: {
6931 struct ofp11_port_stats_request *req;
6932 request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0);
6933 req = ofpbuf_put_zeros(request, sizeof *req);
6934 req->port_no = ofputil_port_to_ofp11(port);
6945 ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops,
6946 struct ofp10_port_stats *ps10)
6948 ps10->port_no = htons(ofp_to_u16(ops->port_no));
6949 memset(ps10->pad, 0, sizeof ps10->pad);
6950 put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets));
6951 put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets));
6952 put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes));
6953 put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes));
6954 put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped));
6955 put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped));
6956 put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors));
6957 put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors));
6958 put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors));
6959 put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors));
6960 put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors));
6961 put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions));
6965 ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops,
6966 struct ofp11_port_stats *ps11)
6968 ps11->port_no = ofputil_port_to_ofp11(ops->port_no);
6969 memset(ps11->pad, 0, sizeof ps11->pad);
6970 ps11->rx_packets = htonll(ops->stats.rx_packets);
6971 ps11->tx_packets = htonll(ops->stats.tx_packets);
6972 ps11->rx_bytes = htonll(ops->stats.rx_bytes);
6973 ps11->tx_bytes = htonll(ops->stats.tx_bytes);
6974 ps11->rx_dropped = htonll(ops->stats.rx_dropped);
6975 ps11->tx_dropped = htonll(ops->stats.tx_dropped);
6976 ps11->rx_errors = htonll(ops->stats.rx_errors);
6977 ps11->tx_errors = htonll(ops->stats.tx_errors);
6978 ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors);
6979 ps11->rx_over_err = htonll(ops->stats.rx_over_errors);
6980 ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors);
6981 ps11->collisions = htonll(ops->stats.collisions);
6985 ofputil_port_stats_to_ofp13(const struct ofputil_port_stats *ops,
6986 struct ofp13_port_stats *ps13)
6988 ofputil_port_stats_to_ofp11(ops, &ps13->ps);
6989 ps13->duration_sec = htonl(ops->duration_sec);
6990 ps13->duration_nsec = htonl(ops->duration_nsec);
6994 ofputil_append_ofp14_port_stats(const struct ofputil_port_stats *ops,
6995 struct ovs_list *replies)
6997 struct ofp14_port_stats_prop_ethernet *eth;
6998 struct ofp14_port_stats *ps14;
6999 struct ofpbuf *reply;
7001 reply = ofpmp_reserve(replies, sizeof *ps14 + sizeof *eth);
7003 ps14 = ofpbuf_put_uninit(reply, sizeof *ps14);
7004 ps14->length = htons(sizeof *ps14 + sizeof *eth);
7005 memset(ps14->pad, 0, sizeof ps14->pad);
7006 ps14->port_no = ofputil_port_to_ofp11(ops->port_no);
7007 ps14->duration_sec = htonl(ops->duration_sec);
7008 ps14->duration_nsec = htonl(ops->duration_nsec);
7009 ps14->rx_packets = htonll(ops->stats.rx_packets);
7010 ps14->tx_packets = htonll(ops->stats.tx_packets);
7011 ps14->rx_bytes = htonll(ops->stats.rx_bytes);
7012 ps14->tx_bytes = htonll(ops->stats.tx_bytes);
7013 ps14->rx_dropped = htonll(ops->stats.rx_dropped);
7014 ps14->tx_dropped = htonll(ops->stats.tx_dropped);
7015 ps14->rx_errors = htonll(ops->stats.rx_errors);
7016 ps14->tx_errors = htonll(ops->stats.tx_errors);
7018 eth = ofpbuf_put_uninit(reply, sizeof *eth);
7019 eth->type = htons(OFPPSPT14_ETHERNET);
7020 eth->length = htons(sizeof *eth);
7021 memset(eth->pad, 0, sizeof eth->pad);
7022 eth->rx_frame_err = htonll(ops->stats.rx_frame_errors);
7023 eth->rx_over_err = htonll(ops->stats.rx_over_errors);
7024 eth->rx_crc_err = htonll(ops->stats.rx_crc_errors);
7025 eth->collisions = htonll(ops->stats.collisions);
7028 /* Encode a ports stat for 'ops' and append it to 'replies'. */
7030 ofputil_append_port_stat(struct ovs_list *replies,
7031 const struct ofputil_port_stats *ops)
7033 switch (ofpmp_version(replies)) {
7034 case OFP13_VERSION: {
7035 struct ofp13_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7036 ofputil_port_stats_to_ofp13(ops, reply);
7040 case OFP11_VERSION: {
7041 struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7042 ofputil_port_stats_to_ofp11(ops, reply);
7046 case OFP10_VERSION: {
7047 struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7048 ofputil_port_stats_to_ofp10(ops, reply);
7054 ofputil_append_ofp14_port_stats(ops, replies);
7063 ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops,
7064 const struct ofp10_port_stats *ps10)
7066 memset(ops, 0, sizeof *ops);
7068 ops->port_no = u16_to_ofp(ntohs(ps10->port_no));
7069 ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets));
7070 ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets));
7071 ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes));
7072 ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes));
7073 ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped));
7074 ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped));
7075 ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors));
7076 ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors));
7077 ops->stats.rx_frame_errors =
7078 ntohll(get_32aligned_be64(&ps10->rx_frame_err));
7079 ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err));
7080 ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err));
7081 ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions));
7082 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
7088 ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops,
7089 const struct ofp11_port_stats *ps11)
7093 memset(ops, 0, sizeof *ops);
7094 error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no);
7099 ops->stats.rx_packets = ntohll(ps11->rx_packets);
7100 ops->stats.tx_packets = ntohll(ps11->tx_packets);
7101 ops->stats.rx_bytes = ntohll(ps11->rx_bytes);
7102 ops->stats.tx_bytes = ntohll(ps11->tx_bytes);
7103 ops->stats.rx_dropped = ntohll(ps11->rx_dropped);
7104 ops->stats.tx_dropped = ntohll(ps11->tx_dropped);
7105 ops->stats.rx_errors = ntohll(ps11->rx_errors);
7106 ops->stats.tx_errors = ntohll(ps11->tx_errors);
7107 ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err);
7108 ops->stats.rx_over_errors = ntohll(ps11->rx_over_err);
7109 ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err);
7110 ops->stats.collisions = ntohll(ps11->collisions);
7111 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
7117 ofputil_port_stats_from_ofp13(struct ofputil_port_stats *ops,
7118 const struct ofp13_port_stats *ps13)
7120 enum ofperr error = ofputil_port_stats_from_ofp11(ops, &ps13->ps);
7122 ops->duration_sec = ntohl(ps13->duration_sec);
7123 ops->duration_nsec = ntohl(ps13->duration_nsec);
7129 parse_ofp14_port_stats_ethernet_property(const struct ofpbuf *payload,
7130 struct ofputil_port_stats *ops)
7132 const struct ofp14_port_stats_prop_ethernet *eth = payload->data;
7134 if (payload->size != sizeof *eth) {
7135 return OFPERR_OFPBPC_BAD_LEN;
7138 ops->stats.rx_frame_errors = ntohll(eth->rx_frame_err);
7139 ops->stats.rx_over_errors = ntohll(eth->rx_over_err);
7140 ops->stats.rx_crc_errors = ntohll(eth->rx_crc_err);
7141 ops->stats.collisions = ntohll(eth->collisions);
7147 ofputil_pull_ofp14_port_stats(struct ofputil_port_stats *ops,
7150 const struct ofp14_port_stats *ps14;
7151 struct ofpbuf properties;
7155 ps14 = ofpbuf_try_pull(msg, sizeof *ps14);
7157 return OFPERR_OFPBRC_BAD_LEN;
7160 len = ntohs(ps14->length);
7161 if (len < sizeof *ps14 || len - sizeof *ps14 > msg->size) {
7162 return OFPERR_OFPBRC_BAD_LEN;
7164 len -= sizeof *ps14;
7165 ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len);
7167 error = ofputil_port_from_ofp11(ps14->port_no, &ops->port_no);
7172 ops->duration_sec = ntohl(ps14->duration_sec);
7173 ops->duration_nsec = ntohl(ps14->duration_nsec);
7174 ops->stats.rx_packets = ntohll(ps14->rx_packets);
7175 ops->stats.tx_packets = ntohll(ps14->tx_packets);
7176 ops->stats.rx_bytes = ntohll(ps14->rx_bytes);
7177 ops->stats.tx_bytes = ntohll(ps14->tx_bytes);
7178 ops->stats.rx_dropped = ntohll(ps14->rx_dropped);
7179 ops->stats.tx_dropped = ntohll(ps14->tx_dropped);
7180 ops->stats.rx_errors = ntohll(ps14->rx_errors);
7181 ops->stats.tx_errors = ntohll(ps14->tx_errors);
7182 ops->stats.rx_frame_errors = UINT64_MAX;
7183 ops->stats.rx_over_errors = UINT64_MAX;
7184 ops->stats.rx_crc_errors = UINT64_MAX;
7185 ops->stats.collisions = UINT64_MAX;
7187 while (properties.size > 0) {
7188 struct ofpbuf payload;
7192 error = ofpprop_pull(&properties, &payload, &type);
7198 case OFPPSPT14_ETHERNET:
7199 error = parse_ofp14_port_stats_ethernet_property(&payload, ops);
7203 OFPPROP_LOG(&bad_ofmsg_rl, true,
7204 "unknown port stats property %"PRIu16, type);
7217 /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
7220 ofputil_count_port_stats(const struct ofp_header *oh)
7222 struct ofputil_port_stats ps;
7226 ofpbuf_use_const(&b, oh, ntohs(oh->length));
7227 ofpraw_pull_assert(&b);
7228 while (!ofputil_decode_port_stats(&ps, &b)) {
7234 /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
7235 * ofputil_port_stats in 'ps'.
7237 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
7238 * message. Calling this function multiple times for a single 'msg' iterates
7239 * through the replies. The caller must initially leave 'msg''s layer pointers
7240 * null and not modify them between calls.
7242 * Returns 0 if successful, EOF if no replies were left in this 'msg',
7243 * otherwise a positive errno value. */
7245 ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg)
7250 error = (msg->header ? ofpraw_decode(&raw, msg->header)
7251 : ofpraw_pull(&raw, msg));
7258 } else if (raw == OFPRAW_OFPST14_PORT_REPLY) {
7259 return ofputil_pull_ofp14_port_stats(ps, msg);
7260 } else if (raw == OFPRAW_OFPST13_PORT_REPLY) {
7261 const struct ofp13_port_stats *ps13;
7263 ps13 = ofpbuf_try_pull(msg, sizeof *ps13);
7267 return ofputil_port_stats_from_ofp13(ps, ps13);
7268 } else if (raw == OFPRAW_OFPST11_PORT_REPLY) {
7269 const struct ofp11_port_stats *ps11;
7271 ps11 = ofpbuf_try_pull(msg, sizeof *ps11);
7275 return ofputil_port_stats_from_ofp11(ps, ps11);
7276 } else if (raw == OFPRAW_OFPST10_PORT_REPLY) {
7277 const struct ofp10_port_stats *ps10;
7279 ps10 = ofpbuf_try_pull(msg, sizeof *ps10);
7283 return ofputil_port_stats_from_ofp10(ps, ps10);
7289 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %"PRIu32" leftover "
7290 "bytes at end", msg->size);
7291 return OFPERR_OFPBRC_BAD_LEN;
7294 /* Parse a port status request message into a 16 bit OpenFlow 1.0
7295 * port number and stores the latter in '*ofp10_port'.
7296 * Returns 0 if successful, otherwise an OFPERR_* number. */
7298 ofputil_decode_port_stats_request(const struct ofp_header *request,
7299 ofp_port_t *ofp10_port)
7301 switch ((enum ofp_version)request->version) {
7306 case OFP11_VERSION: {
7307 const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request);
7308 return ofputil_port_from_ofp11(psr11->port_no, ofp10_port);
7311 case OFP10_VERSION: {
7312 const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request);
7313 *ofp10_port = u16_to_ofp(ntohs(psr10->port_no));
7322 /* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */
7324 ofputil_bucket_list_destroy(struct ovs_list *buckets)
7326 struct ofputil_bucket *bucket;
7328 LIST_FOR_EACH_POP (bucket, list_node, buckets) {
7329 free(bucket->ofpacts);
7334 /* Clones 'bucket' and its ofpacts data */
7335 static struct ofputil_bucket *
7336 ofputil_bucket_clone_data(const struct ofputil_bucket *bucket)
7338 struct ofputil_bucket *new;
7340 new = xmemdup(bucket, sizeof *bucket);
7341 new->ofpacts = xmemdup(bucket->ofpacts, bucket->ofpacts_len);
7346 /* Clones each of the buckets in the list 'src' appending them
7347 * in turn to 'dest' which should be an initialised list.
7348 * An exception is that if the pointer value of a bucket in 'src'
7349 * matches 'skip' then it is not cloned or appended to 'dest'.
7350 * This allows all of 'src' or 'all of 'src' except 'skip' to
7351 * be cloned and appended to 'dest'. */
7353 ofputil_bucket_clone_list(struct ovs_list *dest, const struct ovs_list *src,
7354 const struct ofputil_bucket *skip)
7356 struct ofputil_bucket *bucket;
7358 LIST_FOR_EACH (bucket, list_node, src) {
7359 struct ofputil_bucket *new_bucket;
7361 if (bucket == skip) {
7365 new_bucket = ofputil_bucket_clone_data(bucket);
7366 list_push_back(dest, &new_bucket->list_node);
7370 /* Find a bucket in the list 'buckets' whose bucket id is 'bucket_id'
7371 * Returns the first bucket found or NULL if no buckets are found. */
7372 struct ofputil_bucket *
7373 ofputil_bucket_find(const struct ovs_list *buckets, uint32_t bucket_id)
7375 struct ofputil_bucket *bucket;
7377 if (bucket_id > OFPG15_BUCKET_MAX) {
7381 LIST_FOR_EACH (bucket, list_node, buckets) {
7382 if (bucket->bucket_id == bucket_id) {
7390 /* Returns true if more than one bucket in the list 'buckets'
7391 * have the same bucket id. Returns false otherwise. */
7393 ofputil_bucket_check_duplicate_id(const struct ovs_list *buckets)
7395 struct ofputil_bucket *i, *j;
7397 LIST_FOR_EACH (i, list_node, buckets) {
7398 LIST_FOR_EACH_REVERSE (j, list_node, buckets) {
7402 if (i->bucket_id == j->bucket_id) {
7411 /* Returns the bucket at the front of the list 'buckets'.
7412 * Undefined if 'buckets is empty. */
7413 struct ofputil_bucket *
7414 ofputil_bucket_list_front(const struct ovs_list *buckets)
7416 static struct ofputil_bucket *bucket;
7418 ASSIGN_CONTAINER(bucket, list_front(buckets), list_node);
7423 /* Returns the bucket at the back of the list 'buckets'.
7424 * Undefined if 'buckets is empty. */
7425 struct ofputil_bucket *
7426 ofputil_bucket_list_back(const struct ovs_list *buckets)
7428 static struct ofputil_bucket *bucket;
7430 ASSIGN_CONTAINER(bucket, list_back(buckets), list_node);
7435 /* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version',
7436 * that requests stats for group 'group_id'. (Use OFPG_ALL to request stats
7439 * Group statistics include packet and byte counts for each group. */
7441 ofputil_encode_group_stats_request(enum ofp_version ofp_version,
7444 struct ofpbuf *request;
7446 switch (ofp_version) {
7448 ovs_fatal(0, "dump-group-stats needs OpenFlow 1.1 or later "
7449 "(\'-O OpenFlow11\')");
7454 case OFP15_VERSION: {
7455 struct ofp11_group_stats_request *req;
7456 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_REQUEST, ofp_version, 0);
7457 req = ofpbuf_put_zeros(request, sizeof *req);
7458 req->group_id = htonl(group_id);
7469 ofputil_uninit_group_desc(struct ofputil_group_desc *gd)
7471 ofputil_bucket_list_destroy(&gd->buckets);
7472 free(&gd->props.fields);
7475 /* Decodes the OpenFlow group description request in 'oh', returning the group
7476 * whose description is requested, or OFPG_ALL if stats for all groups was
7479 ofputil_decode_group_desc_request(const struct ofp_header *oh)
7481 struct ofpbuf request;
7484 ofpbuf_use_const(&request, oh, ntohs(oh->length));
7485 raw = ofpraw_pull_assert(&request);
7486 if (raw == OFPRAW_OFPST11_GROUP_DESC_REQUEST) {
7488 } else if (raw == OFPRAW_OFPST15_GROUP_DESC_REQUEST) {
7489 ovs_be32 *group_id = ofpbuf_pull(&request, sizeof *group_id);
7490 return ntohl(*group_id);
7496 /* Returns an OpenFlow group description request for OpenFlow version
7497 * 'ofp_version', that requests stats for group 'group_id'. Use OFPG_ALL to
7498 * request stats for all groups (OpenFlow 1.4 and earlier always request all
7501 * Group descriptions include the bucket and action configuration for each
7504 ofputil_encode_group_desc_request(enum ofp_version ofp_version,
7507 struct ofpbuf *request;
7509 switch (ofp_version) {
7511 ovs_fatal(0, "dump-groups needs OpenFlow 1.1 or later "
7512 "(\'-O OpenFlow11\')");
7517 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_DESC_REQUEST,
7520 case OFP15_VERSION:{
7521 struct ofp15_group_desc_request *req;
7522 request = ofpraw_alloc(OFPRAW_OFPST15_GROUP_DESC_REQUEST,
7524 req = ofpbuf_put_zeros(request, sizeof *req);
7525 req->group_id = htonl(group_id);
7536 ofputil_group_bucket_counters_to_ofp11(const struct ofputil_group_stats *gs,
7537 struct ofp11_bucket_counter bucket_cnts[])
7541 for (i = 0; i < gs->n_buckets; i++) {
7542 bucket_cnts[i].packet_count = htonll(gs->bucket_stats[i].packet_count);
7543 bucket_cnts[i].byte_count = htonll(gs->bucket_stats[i].byte_count);
7548 ofputil_group_stats_to_ofp11(const struct ofputil_group_stats *gs,
7549 struct ofp11_group_stats *gs11, size_t length,
7550 struct ofp11_bucket_counter bucket_cnts[])
7552 memset(gs11, 0, sizeof *gs11);
7553 gs11->length = htons(length);
7554 gs11->group_id = htonl(gs->group_id);
7555 gs11->ref_count = htonl(gs->ref_count);
7556 gs11->packet_count = htonll(gs->packet_count);
7557 gs11->byte_count = htonll(gs->byte_count);
7558 ofputil_group_bucket_counters_to_ofp11(gs, bucket_cnts);
7562 ofputil_group_stats_to_ofp13(const struct ofputil_group_stats *gs,
7563 struct ofp13_group_stats *gs13, size_t length,
7564 struct ofp11_bucket_counter bucket_cnts[])
7566 ofputil_group_stats_to_ofp11(gs, &gs13->gs, length, bucket_cnts);
7567 gs13->duration_sec = htonl(gs->duration_sec);
7568 gs13->duration_nsec = htonl(gs->duration_nsec);
7572 /* Encodes 'gs' properly for the format of the list of group statistics
7573 * replies already begun in 'replies' and appends it to the list. 'replies'
7574 * must have originally been initialized with ofpmp_init(). */
7576 ofputil_append_group_stats(struct ovs_list *replies,
7577 const struct ofputil_group_stats *gs)
7579 size_t bucket_counter_size;
7580 struct ofp11_bucket_counter *bucket_counters;
7583 bucket_counter_size = gs->n_buckets * sizeof(struct ofp11_bucket_counter);
7585 switch (ofpmp_version(replies)) {
7587 case OFP12_VERSION:{
7588 struct ofp11_group_stats *gs11;
7590 length = sizeof *gs11 + bucket_counter_size;
7591 gs11 = ofpmp_append(replies, length);
7592 bucket_counters = (struct ofp11_bucket_counter *)(gs11 + 1);
7593 ofputil_group_stats_to_ofp11(gs, gs11, length, bucket_counters);
7599 case OFP15_VERSION: {
7600 struct ofp13_group_stats *gs13;
7602 length = sizeof *gs13 + bucket_counter_size;
7603 gs13 = ofpmp_append(replies, length);
7604 bucket_counters = (struct ofp11_bucket_counter *)(gs13 + 1);
7605 ofputil_group_stats_to_ofp13(gs, gs13, length, bucket_counters);
7614 /* Returns an OpenFlow group features request for OpenFlow version
7617 ofputil_encode_group_features_request(enum ofp_version ofp_version)
7619 struct ofpbuf *request = NULL;
7621 switch (ofp_version) {
7624 ovs_fatal(0, "dump-group-features needs OpenFlow 1.2 or later "
7625 "(\'-O OpenFlow12\')");
7630 request = ofpraw_alloc(OFPRAW_OFPST12_GROUP_FEATURES_REQUEST,
7640 /* Returns a OpenFlow message that encodes 'features' properly as a reply to
7641 * group features request 'request'. */
7643 ofputil_encode_group_features_reply(
7644 const struct ofputil_group_features *features,
7645 const struct ofp_header *request)
7647 struct ofp12_group_features_stats *ogf;
7648 struct ofpbuf *reply;
7651 reply = ofpraw_alloc_xid(OFPRAW_OFPST12_GROUP_FEATURES_REPLY,
7652 request->version, request->xid, 0);
7653 ogf = ofpbuf_put_zeros(reply, sizeof *ogf);
7654 ogf->types = htonl(features->types);
7655 ogf->capabilities = htonl(features->capabilities);
7656 for (i = 0; i < OFPGT12_N_TYPES; i++) {
7657 ogf->max_groups[i] = htonl(features->max_groups[i]);
7658 ogf->actions[i] = ofpact_bitmap_to_openflow(features->ofpacts[i],
7665 /* Decodes group features reply 'oh' into 'features'. */
7667 ofputil_decode_group_features_reply(const struct ofp_header *oh,
7668 struct ofputil_group_features *features)
7670 const struct ofp12_group_features_stats *ogf = ofpmsg_body(oh);
7673 features->types = ntohl(ogf->types);
7674 features->capabilities = ntohl(ogf->capabilities);
7675 for (i = 0; i < OFPGT12_N_TYPES; i++) {
7676 features->max_groups[i] = ntohl(ogf->max_groups[i]);
7677 features->ofpacts[i] = ofpact_bitmap_from_openflow(
7678 ogf->actions[i], oh->version);
7682 /* Parse a group status request message into a 32 bit OpenFlow 1.1
7683 * group ID and stores the latter in '*group_id'.
7684 * Returns 0 if successful, otherwise an OFPERR_* number. */
7686 ofputil_decode_group_stats_request(const struct ofp_header *request,
7689 const struct ofp11_group_stats_request *gsr11 = ofpmsg_body(request);
7690 *group_id = ntohl(gsr11->group_id);
7694 /* Converts a group stats reply in 'msg' into an abstract ofputil_group_stats
7695 * in 'gs'. Assigns freshly allocated memory to gs->bucket_stats for the
7696 * caller to eventually free.
7698 * Multiple group stats replies can be packed into a single OpenFlow message.
7699 * Calling this function multiple times for a single 'msg' iterates through the
7700 * replies. The caller must initially leave 'msg''s layer pointers null and
7701 * not modify them between calls.
7703 * Returns 0 if successful, EOF if no replies were left in this 'msg',
7704 * otherwise a positive errno value. */
7706 ofputil_decode_group_stats_reply(struct ofpbuf *msg,
7707 struct ofputil_group_stats *gs)
7709 struct ofp11_bucket_counter *obc;
7710 struct ofp11_group_stats *ogs11;
7717 gs->bucket_stats = NULL;
7718 error = (msg->header ? ofpraw_decode(&raw, msg->header)
7719 : ofpraw_pull(&raw, msg));
7728 if (raw == OFPRAW_OFPST11_GROUP_REPLY) {
7729 base_len = sizeof *ogs11;
7730 ogs11 = ofpbuf_try_pull(msg, sizeof *ogs11);
7731 gs->duration_sec = gs->duration_nsec = UINT32_MAX;
7732 } else if (raw == OFPRAW_OFPST13_GROUP_REPLY) {
7733 struct ofp13_group_stats *ogs13;
7735 base_len = sizeof *ogs13;
7736 ogs13 = ofpbuf_try_pull(msg, sizeof *ogs13);
7739 gs->duration_sec = ntohl(ogs13->duration_sec);
7740 gs->duration_nsec = ntohl(ogs13->duration_nsec);
7749 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
7750 ofpraw_get_name(raw), msg->size);
7751 return OFPERR_OFPBRC_BAD_LEN;
7753 length = ntohs(ogs11->length);
7754 if (length < sizeof base_len) {
7755 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply claims invalid length %"PRIuSIZE,
7756 ofpraw_get_name(raw), length);
7757 return OFPERR_OFPBRC_BAD_LEN;
7760 gs->group_id = ntohl(ogs11->group_id);
7761 gs->ref_count = ntohl(ogs11->ref_count);
7762 gs->packet_count = ntohll(ogs11->packet_count);
7763 gs->byte_count = ntohll(ogs11->byte_count);
7765 gs->n_buckets = (length - base_len) / sizeof *obc;
7766 obc = ofpbuf_try_pull(msg, gs->n_buckets * sizeof *obc);
7768 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
7769 ofpraw_get_name(raw), msg->size);
7770 return OFPERR_OFPBRC_BAD_LEN;
7773 gs->bucket_stats = xmalloc(gs->n_buckets * sizeof *gs->bucket_stats);
7774 for (i = 0; i < gs->n_buckets; i++) {
7775 gs->bucket_stats[i].packet_count = ntohll(obc[i].packet_count);
7776 gs->bucket_stats[i].byte_count = ntohll(obc[i].byte_count);
7783 ofputil_put_ofp11_bucket(const struct ofputil_bucket *bucket,
7784 struct ofpbuf *openflow, enum ofp_version ofp_version)
7786 struct ofp11_bucket *ob;
7789 start = openflow->size;
7790 ofpbuf_put_zeros(openflow, sizeof *ob);
7791 ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
7792 openflow, ofp_version);
7793 ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
7794 ob->len = htons(openflow->size - start);
7795 ob->weight = htons(bucket->weight);
7796 ob->watch_port = ofputil_port_to_ofp11(bucket->watch_port);
7797 ob->watch_group = htonl(bucket->watch_group);
7801 ofputil_put_ofp15_group_bucket_prop_weight(ovs_be16 weight,
7802 struct ofpbuf *openflow)
7805 struct ofp15_group_bucket_prop_weight *prop;
7807 start_ofs = ofpprop_start(openflow, OFPGBPT15_WEIGHT);
7808 ofpbuf_put_zeros(openflow, sizeof *prop - sizeof(struct ofp_prop_header));
7809 prop = ofpbuf_at_assert(openflow, start_ofs, sizeof *prop);
7810 prop->weight = weight;
7811 ofpprop_end(openflow, start_ofs);
7815 ofputil_put_ofp15_group_bucket_prop_watch(ovs_be32 watch, uint16_t type,
7816 struct ofpbuf *openflow)
7819 struct ofp15_group_bucket_prop_watch *prop;
7821 start_ofs = ofpprop_start(openflow, type);
7822 ofpbuf_put_zeros(openflow, sizeof *prop - sizeof(struct ofp_prop_header));
7823 prop = ofpbuf_at_assert(openflow, start_ofs, sizeof *prop);
7824 prop->watch = watch;
7825 ofpprop_end(openflow, start_ofs);
7829 ofputil_put_ofp15_bucket(const struct ofputil_bucket *bucket,
7830 uint32_t bucket_id, enum ofp11_group_type group_type,
7831 struct ofpbuf *openflow, enum ofp_version ofp_version)
7833 struct ofp15_bucket *ob;
7834 size_t start, actions_start, actions_len;
7836 start = openflow->size;
7837 ofpbuf_put_zeros(openflow, sizeof *ob);
7839 actions_start = openflow->size;
7840 ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
7841 openflow, ofp_version);
7842 actions_len = openflow->size - actions_start;
7844 if (group_type == OFPGT11_SELECT) {
7845 ofputil_put_ofp15_group_bucket_prop_weight(htons(bucket->weight),
7848 if (bucket->watch_port != OFPP_ANY) {
7849 ovs_be32 port = ofputil_port_to_ofp11(bucket->watch_port);
7850 ofputil_put_ofp15_group_bucket_prop_watch(port,
7851 OFPGBPT15_WATCH_PORT,
7854 if (bucket->watch_group != OFPG_ANY) {
7855 ovs_be32 group = htonl(bucket->watch_group);
7856 ofputil_put_ofp15_group_bucket_prop_watch(group,
7857 OFPGBPT15_WATCH_GROUP,
7861 ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
7862 ob->len = htons(openflow->size - start);
7863 ob->action_array_len = htons(actions_len);
7864 ob->bucket_id = htonl(bucket_id);
7868 ofputil_put_group_prop_ntr_selection_method(enum ofp_version ofp_version,
7869 const struct ofputil_group_props *gp,
7870 struct ofpbuf *openflow)
7872 struct ntr_group_prop_selection_method *prop;
7875 start = openflow->size;
7876 ofpbuf_put_zeros(openflow, sizeof *prop);
7877 oxm_put_field_array(openflow, &gp->fields, ofp_version);
7878 prop = ofpbuf_at_assert(openflow, start, sizeof *prop);
7879 prop->type = htons(OFPGPT15_EXPERIMENTER);
7880 prop->experimenter = htonl(NTR_VENDOR_ID);
7881 prop->exp_type = htonl(NTRT_SELECTION_METHOD);
7882 strcpy(prop->selection_method, gp->selection_method);
7883 prop->selection_method_param = htonll(gp->selection_method_param);
7884 ofpprop_end(openflow, start);
7888 ofputil_append_ofp11_group_desc_reply(const struct ofputil_group_desc *gds,
7889 const struct ovs_list *buckets,
7890 struct ovs_list *replies,
7891 enum ofp_version version)
7893 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
7894 struct ofp11_group_desc_stats *ogds;
7895 struct ofputil_bucket *bucket;
7898 start_ogds = reply->size;
7899 ofpbuf_put_zeros(reply, sizeof *ogds);
7900 LIST_FOR_EACH (bucket, list_node, buckets) {
7901 ofputil_put_ofp11_bucket(bucket, reply, version);
7903 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
7904 ogds->length = htons(reply->size - start_ogds);
7905 ogds->type = gds->type;
7906 ogds->group_id = htonl(gds->group_id);
7908 ofpmp_postappend(replies, start_ogds);
7912 ofputil_append_ofp15_group_desc_reply(const struct ofputil_group_desc *gds,
7913 const struct ovs_list *buckets,
7914 struct ovs_list *replies,
7915 enum ofp_version version)
7917 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
7918 struct ofp15_group_desc_stats *ogds;
7919 struct ofputil_bucket *bucket;
7920 size_t start_ogds, start_buckets;
7922 start_ogds = reply->size;
7923 ofpbuf_put_zeros(reply, sizeof *ogds);
7924 start_buckets = reply->size;
7925 LIST_FOR_EACH (bucket, list_node, buckets) {
7926 ofputil_put_ofp15_bucket(bucket, bucket->bucket_id,
7927 gds->type, reply, version);
7929 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
7930 ogds->type = gds->type;
7931 ogds->group_id = htonl(gds->group_id);
7932 ogds->bucket_list_len = htons(reply->size - start_buckets);
7934 /* Add group properties */
7935 if (gds->props.selection_method[0]) {
7936 ofputil_put_group_prop_ntr_selection_method(version, &gds->props,
7939 ogds->length = htons(reply->size - start_ogds);
7941 ofpmp_postappend(replies, start_ogds);
7944 /* Appends a group stats reply that contains the data in 'gds' to those already
7945 * present in the list of ofpbufs in 'replies'. 'replies' should have been
7946 * initialized with ofpmp_init(). */
7948 ofputil_append_group_desc_reply(const struct ofputil_group_desc *gds,
7949 const struct ovs_list *buckets,
7950 struct ovs_list *replies)
7952 enum ofp_version version = ofpmp_version(replies);
7960 ofputil_append_ofp11_group_desc_reply(gds, buckets, replies, version);
7964 ofputil_append_ofp15_group_desc_reply(gds, buckets, replies, version);
7974 ofputil_pull_ofp11_buckets(struct ofpbuf *msg, size_t buckets_length,
7975 enum ofp_version version, struct ovs_list *buckets)
7977 struct ofp11_bucket *ob;
7978 uint32_t bucket_id = 0;
7981 while (buckets_length > 0) {
7982 struct ofputil_bucket *bucket;
7983 struct ofpbuf ofpacts;
7987 ob = (buckets_length >= sizeof *ob
7988 ? ofpbuf_try_pull(msg, sizeof *ob)
7991 VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE" leftover bytes",
7993 return OFPERR_OFPGMFC_BAD_BUCKET;
7996 ob_len = ntohs(ob->len);
7997 if (ob_len < sizeof *ob) {
7998 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
7999 "%"PRIuSIZE" is not valid", ob_len);
8000 return OFPERR_OFPGMFC_BAD_BUCKET;
8001 } else if (ob_len > buckets_length) {
8002 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8003 "%"PRIuSIZE" exceeds remaining buckets data size %"PRIuSIZE,
8004 ob_len, buckets_length);
8005 return OFPERR_OFPGMFC_BAD_BUCKET;
8007 buckets_length -= ob_len;
8009 ofpbuf_init(&ofpacts, 0);
8010 error = ofpacts_pull_openflow_actions(msg, ob_len - sizeof *ob,
8013 ofpbuf_uninit(&ofpacts);
8014 ofputil_bucket_list_destroy(buckets);
8018 bucket = xzalloc(sizeof *bucket);
8019 bucket->weight = ntohs(ob->weight);
8020 error = ofputil_port_from_ofp11(ob->watch_port, &bucket->watch_port);
8022 ofpbuf_uninit(&ofpacts);
8023 ofputil_bucket_list_destroy(buckets);
8024 return OFPERR_OFPGMFC_BAD_WATCH;
8026 bucket->watch_group = ntohl(ob->watch_group);
8027 bucket->bucket_id = bucket_id++;
8029 bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
8030 bucket->ofpacts_len = ofpacts.size;
8031 list_push_back(buckets, &bucket->list_node);
8038 parse_ofp15_group_bucket_prop_weight(const struct ofpbuf *payload,
8041 struct ofp15_group_bucket_prop_weight *prop = payload->data;
8043 if (payload->size != sizeof *prop) {
8044 OFPPROP_LOG(&bad_ofmsg_rl, false, "OpenFlow bucket weight property "
8045 "length %u is not valid", payload->size);
8046 return OFPERR_OFPBPC_BAD_LEN;
8049 *weight = prop->weight;
8055 parse_ofp15_group_bucket_prop_watch(const struct ofpbuf *payload,
8058 struct ofp15_group_bucket_prop_watch *prop = payload->data;
8060 if (payload->size != sizeof *prop) {
8061 OFPPROP_LOG(&bad_ofmsg_rl, false, "OpenFlow bucket watch port or "
8062 "group property length %u is not valid", payload->size);
8063 return OFPERR_OFPBPC_BAD_LEN;
8066 *watch = prop->watch;
8072 ofputil_pull_ofp15_buckets(struct ofpbuf *msg, size_t buckets_length,
8073 enum ofp_version version, uint8_t group_type,
8074 struct ovs_list *buckets)
8076 struct ofp15_bucket *ob;
8079 while (buckets_length > 0) {
8080 struct ofputil_bucket *bucket = NULL;
8081 struct ofpbuf ofpacts;
8082 enum ofperr err = OFPERR_OFPGMFC_BAD_BUCKET;
8083 struct ofpbuf properties;
8084 size_t ob_len, actions_len, properties_len;
8085 ovs_be32 watch_port = ofputil_port_to_ofp11(OFPP_ANY);
8086 ovs_be32 watch_group = htonl(OFPG_ANY);
8087 ovs_be16 weight = htons(group_type == OFPGT11_SELECT ? 1 : 0);
8089 ofpbuf_init(&ofpacts, 0);
8091 ob = ofpbuf_try_pull(msg, sizeof *ob);
8093 VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE
8094 " leftover bytes", buckets_length);
8098 ob_len = ntohs(ob->len);
8099 actions_len = ntohs(ob->action_array_len);
8101 if (ob_len < sizeof *ob) {
8102 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8103 "%"PRIuSIZE" is not valid", ob_len);
8105 } else if (ob_len > buckets_length) {
8106 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8107 "%"PRIuSIZE" exceeds remaining buckets data size %"
8108 PRIuSIZE, ob_len, buckets_length);
8110 } else if (actions_len > ob_len - sizeof *ob) {
8111 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket actions "
8112 "length %"PRIuSIZE" exceeds remaining bucket "
8113 "data size %"PRIuSIZE, actions_len,
8114 ob_len - sizeof *ob);
8117 buckets_length -= ob_len;
8119 err = ofpacts_pull_openflow_actions(msg, actions_len, version,
8125 properties_len = ob_len - sizeof *ob - actions_len;
8126 ofpbuf_use_const(&properties, ofpbuf_pull(msg, properties_len),
8129 while (properties.size > 0) {
8130 struct ofpbuf payload;
8133 err = ofpprop_pull(&properties, &payload, &type);
8139 case OFPGBPT15_WEIGHT:
8140 err = parse_ofp15_group_bucket_prop_weight(&payload, &weight);
8143 case OFPGBPT15_WATCH_PORT:
8144 err = parse_ofp15_group_bucket_prop_watch(&payload,
8148 case OFPGBPT15_WATCH_GROUP:
8149 err = parse_ofp15_group_bucket_prop_watch(&payload,
8154 OFPPROP_LOG(&bad_ofmsg_rl, false,
8155 "unknown group bucket property %"PRIu16, type);
8156 err = OFPERR_OFPBPC_BAD_TYPE;
8165 bucket = xzalloc(sizeof *bucket);
8167 bucket->weight = ntohs(weight);
8168 err = ofputil_port_from_ofp11(watch_port, &bucket->watch_port);
8170 err = OFPERR_OFPGMFC_BAD_WATCH;
8173 bucket->watch_group = ntohl(watch_group);
8174 bucket->bucket_id = ntohl(ob->bucket_id);
8175 if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
8176 VLOG_WARN_RL(&bad_ofmsg_rl, "bucket id (%u) is out of range",
8178 err = OFPERR_OFPGMFC_BAD_BUCKET;
8182 bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
8183 bucket->ofpacts_len = ofpacts.size;
8184 list_push_back(buckets, &bucket->list_node);
8190 ofpbuf_uninit(&ofpacts);
8191 ofputil_bucket_list_destroy(buckets);
8195 if (ofputil_bucket_check_duplicate_id(buckets)) {
8196 VLOG_WARN_RL(&bad_ofmsg_rl, "Duplicate bucket id");
8197 ofputil_bucket_list_destroy(buckets);
8198 return OFPERR_OFPGMFC_BAD_BUCKET;
8205 ofputil_init_group_properties(struct ofputil_group_props *gp)
8207 memset(gp, 0, sizeof *gp);
8211 parse_group_prop_ntr_selection_method(struct ofpbuf *payload,
8212 enum ofp11_group_type group_type,
8213 enum ofp15_group_mod_command group_cmd,
8214 struct ofputil_group_props *gp)
8216 struct ntr_group_prop_selection_method *prop = payload->data;
8217 size_t fields_len, method_len;
8220 switch (group_type) {
8221 case OFPGT11_SELECT:
8224 case OFPGT11_INDIRECT:
8226 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property is "
8227 "only allowed for select groups");
8228 return OFPERR_OFPBPC_BAD_VALUE;
8233 switch (group_cmd) {
8235 case OFPGC15_MODIFY:
8237 case OFPGC15_DELETE:
8238 case OFPGC15_INSERT_BUCKET:
8239 case OFPGC15_REMOVE_BUCKET:
8240 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property is "
8241 "only allowed for add and delete group modifications");
8242 return OFPERR_OFPBPC_BAD_VALUE;
8247 if (payload->size < sizeof *prop) {
8248 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property "
8249 "length %u is not valid", payload->size);
8250 return OFPERR_OFPBPC_BAD_LEN;
8253 method_len = strnlen(prop->selection_method, NTR_MAX_SELECTION_METHOD_LEN);
8255 if (method_len == NTR_MAX_SELECTION_METHOD_LEN) {
8256 OFPPROP_LOG(&bad_ofmsg_rl, false,
8257 "ntr selection method is not null terminated");
8258 return OFPERR_OFPBPC_BAD_VALUE;
8261 if (strcmp("hash", prop->selection_method)) {
8262 OFPPROP_LOG(&bad_ofmsg_rl, false,
8263 "ntr selection method '%s' is not supported",
8264 prop->selection_method);
8265 return OFPERR_OFPBPC_BAD_VALUE;
8268 strcpy(gp->selection_method, prop->selection_method);
8269 gp->selection_method_param = ntohll(prop->selection_method_param);
8271 if (!method_len && gp->selection_method_param) {
8272 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method parameter is "
8273 "non-zero but selection method is empty");
8274 return OFPERR_OFPBPC_BAD_VALUE;
8277 ofpbuf_pull(payload, sizeof *prop);
8279 fields_len = ntohs(prop->length) - sizeof *prop;
8280 if (!method_len && fields_len) {
8281 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method parameter is "
8282 "zero but fields are provided");
8283 return OFPERR_OFPBPC_BAD_VALUE;
8286 error = oxm_pull_field_array(payload->data, fields_len,
8289 OFPPROP_LOG(&bad_ofmsg_rl, false,
8290 "ntr selection method fields are invalid");
8298 parse_group_prop_ntr(struct ofpbuf *payload, uint32_t exp_type,
8299 enum ofp11_group_type group_type,
8300 enum ofp15_group_mod_command group_cmd,
8301 struct ofputil_group_props *gp)
8306 case NTRT_SELECTION_METHOD:
8307 error = parse_group_prop_ntr_selection_method(payload, group_type,
8312 OFPPROP_LOG(&bad_ofmsg_rl, false,
8313 "unknown group property ntr experimenter type %"PRIu32,
8315 error = OFPERR_OFPBPC_BAD_TYPE;
8323 parse_ofp15_group_prop_exp(struct ofpbuf *payload,
8324 enum ofp11_group_type group_type,
8325 enum ofp15_group_mod_command group_cmd,
8326 struct ofputil_group_props *gp)
8328 struct ofp_prop_experimenter *prop = payload->data;
8329 uint16_t experimenter;
8333 if (payload->size < sizeof *prop) {
8334 return OFPERR_OFPBPC_BAD_LEN;
8337 experimenter = ntohl(prop->experimenter);
8338 exp_type = ntohl(prop->exp_type);
8340 switch (experimenter) {
8342 case NTR_COMPAT_VENDOR_ID:
8343 error = parse_group_prop_ntr(payload, exp_type, group_type,
8348 OFPPROP_LOG(&bad_ofmsg_rl, false,
8349 "unknown group property experimenter %"PRIu16,
8351 error = OFPERR_OFPBPC_BAD_EXPERIMENTER;
8359 parse_ofp15_group_properties(struct ofpbuf *msg,
8360 enum ofp11_group_type group_type,
8361 enum ofp15_group_mod_command group_cmd,
8362 struct ofputil_group_props *gp,
8363 size_t properties_len)
8365 struct ofpbuf properties;
8367 ofpbuf_use_const(&properties, ofpbuf_pull(msg, properties_len),
8370 while (properties.size > 0) {
8371 struct ofpbuf payload;
8375 error = ofpprop_pull(&properties, &payload, &type);
8381 case OFPGPT15_EXPERIMENTER:
8382 error = parse_ofp15_group_prop_exp(&payload, group_type,
8387 OFPPROP_LOG(&bad_ofmsg_rl, false,
8388 "unknown group property %"PRIu16, type);
8389 error = OFPERR_OFPBPC_BAD_TYPE;
8402 ofputil_decode_ofp11_group_desc_reply(struct ofputil_group_desc *gd,
8404 enum ofp_version version)
8406 struct ofp11_group_desc_stats *ogds;
8410 ofpraw_pull_assert(msg);
8417 ogds = ofpbuf_try_pull(msg, sizeof *ogds);
8419 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
8420 "leftover bytes at end", msg->size);
8421 return OFPERR_OFPBRC_BAD_LEN;
8423 gd->type = ogds->type;
8424 gd->group_id = ntohl(ogds->group_id);
8426 length = ntohs(ogds->length);
8427 if (length < sizeof *ogds || length - sizeof *ogds > msg->size) {
8428 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
8429 "length %"PRIuSIZE, length);
8430 return OFPERR_OFPBRC_BAD_LEN;
8433 return ofputil_pull_ofp11_buckets(msg, length - sizeof *ogds, version,
8438 ofputil_decode_ofp15_group_desc_reply(struct ofputil_group_desc *gd,
8440 enum ofp_version version)
8442 struct ofp15_group_desc_stats *ogds;
8443 uint16_t length, bucket_list_len;
8447 ofpraw_pull_assert(msg);
8454 ogds = ofpbuf_try_pull(msg, sizeof *ogds);
8456 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
8457 "leftover bytes at end", msg->size);
8458 return OFPERR_OFPBRC_BAD_LEN;
8460 gd->type = ogds->type;
8461 gd->group_id = ntohl(ogds->group_id);
8463 length = ntohs(ogds->length);
8464 if (length < sizeof *ogds || length - sizeof *ogds > msg->size) {
8465 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
8466 "length %u", length);
8467 return OFPERR_OFPBRC_BAD_LEN;
8470 bucket_list_len = ntohs(ogds->bucket_list_len);
8471 if (length < bucket_list_len + sizeof *ogds) {
8472 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
8473 "bucket list length %u", bucket_list_len);
8474 return OFPERR_OFPBRC_BAD_LEN;
8476 error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, version, gd->type,
8482 /* By definition group desc messages don't have a group mod command.
8483 * However, parse_group_prop_ntr_selection_method() checks to make sure
8484 * that the command is OFPGC15_ADD or OFPGC15_DELETE to guard
8485 * against group mod messages with other commands supplying
8486 * a NTR selection method group experimenter property.
8487 * Such properties are valid for group desc replies so
8488 * claim that the group mod command is OFPGC15_ADD to
8489 * satisfy the check in parse_group_prop_ntr_selection_method() */
8490 return parse_ofp15_group_properties(msg, gd->type, OFPGC15_ADD, &gd->props,
8491 length - sizeof *ogds - bucket_list_len);
8494 /* Converts a group description reply in 'msg' into an abstract
8495 * ofputil_group_desc in 'gd'.
8497 * Multiple group description replies can be packed into a single OpenFlow
8498 * message. Calling this function multiple times for a single 'msg' iterates
8499 * through the replies. The caller must initially leave 'msg''s layer pointers
8500 * null and not modify them between calls.
8502 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8503 * otherwise a positive errno value. */
8505 ofputil_decode_group_desc_reply(struct ofputil_group_desc *gd,
8506 struct ofpbuf *msg, enum ofp_version version)
8508 ofputil_init_group_properties(&gd->props);
8516 return ofputil_decode_ofp11_group_desc_reply(gd, msg, version);
8519 return ofputil_decode_ofp15_group_desc_reply(gd, msg, version);
8528 ofputil_uninit_group_mod(struct ofputil_group_mod *gm)
8530 ofputil_bucket_list_destroy(&gm->buckets);
8533 static struct ofpbuf *
8534 ofputil_encode_ofp11_group_mod(enum ofp_version ofp_version,
8535 const struct ofputil_group_mod *gm)
8538 struct ofp11_group_mod *ogm;
8540 struct ofputil_bucket *bucket;
8542 b = ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD, ofp_version, 0);
8543 start_ogm = b->size;
8544 ofpbuf_put_zeros(b, sizeof *ogm);
8546 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
8547 ofputil_put_ofp11_bucket(bucket, b, ofp_version);
8549 ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
8550 ogm->command = htons(gm->command);
8551 ogm->type = gm->type;
8552 ogm->group_id = htonl(gm->group_id);
8557 static struct ofpbuf *
8558 ofputil_encode_ofp15_group_mod(enum ofp_version ofp_version,
8559 const struct ofputil_group_mod *gm)
8562 struct ofp15_group_mod *ogm;
8564 struct ofputil_bucket *bucket;
8565 struct id_pool *bucket_ids = NULL;
8567 b = ofpraw_alloc(OFPRAW_OFPT15_GROUP_MOD, ofp_version, 0);
8568 start_ogm = b->size;
8569 ofpbuf_put_zeros(b, sizeof *ogm);
8571 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
8574 /* Generate a bucket id if none was supplied */
8575 if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
8577 const struct ofputil_bucket *bkt;
8579 bucket_ids = id_pool_create(0, OFPG15_BUCKET_MAX + 1);
8581 /* Mark all bucket_ids that are present in gm
8582 * as used in the pool. */
8583 LIST_FOR_EACH_REVERSE (bkt, list_node, &gm->buckets) {
8584 if (bkt == bucket) {
8587 if (bkt->bucket_id <= OFPG15_BUCKET_MAX) {
8588 id_pool_add(bucket_ids, bkt->bucket_id);
8593 if (!id_pool_alloc_id(bucket_ids, &bucket_id)) {
8597 bucket_id = bucket->bucket_id;
8600 ofputil_put_ofp15_bucket(bucket, bucket_id, gm->type, b, ofp_version);
8602 ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
8603 ogm->command = htons(gm->command);
8604 ogm->type = gm->type;
8605 ogm->group_id = htonl(gm->group_id);
8606 ogm->command_bucket_id = htonl(gm->command_bucket_id);
8607 ogm->bucket_array_len = htons(b->size - start_ogm - sizeof *ogm);
8609 /* Add group properties */
8610 if (gm->props.selection_method[0]) {
8611 ofputil_put_group_prop_ntr_selection_method(ofp_version, &gm->props, b);
8614 id_pool_destroy(bucket_ids);
8619 bad_group_cmd(enum ofp15_group_mod_command cmd)
8621 const char *opt_version;
8622 const char *version;
8623 const char *cmd_str;
8627 case OFPGC15_MODIFY:
8628 case OFPGC15_DELETE:
8633 case OFPGC15_INSERT_BUCKET:
8634 case OFPGC15_REMOVE_BUCKET:
8645 cmd_str = "add-group";
8648 case OFPGC15_MODIFY:
8649 cmd_str = "mod-group";
8652 case OFPGC15_DELETE:
8653 cmd_str = "del-group";
8656 case OFPGC15_INSERT_BUCKET:
8657 cmd_str = "insert-bucket";
8660 case OFPGC15_REMOVE_BUCKET:
8661 cmd_str = "remove-bucket";
8668 ovs_fatal(0, "%s needs OpenFlow %s or later (\'-O OpenFlow%s\')",
8669 cmd_str, version, opt_version);
8673 /* Converts abstract group mod 'gm' into a message for OpenFlow version
8674 * 'ofp_version' and returns the message. */
8676 ofputil_encode_group_mod(enum ofp_version ofp_version,
8677 const struct ofputil_group_mod *gm)
8680 switch (ofp_version) {
8682 bad_group_cmd(gm->command);
8688 if (gm->command > OFPGC11_DELETE) {
8689 bad_group_cmd(gm->command);
8691 return ofputil_encode_ofp11_group_mod(ofp_version, gm);
8694 return ofputil_encode_ofp15_group_mod(ofp_version, gm);
8702 ofputil_pull_ofp11_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
8703 struct ofputil_group_mod *gm)
8705 const struct ofp11_group_mod *ogm;
8708 ogm = ofpbuf_pull(msg, sizeof *ogm);
8709 gm->command = ntohs(ogm->command);
8710 gm->type = ogm->type;
8711 gm->group_id = ntohl(ogm->group_id);
8712 gm->command_bucket_id = OFPG15_BUCKET_ALL;
8714 error = ofputil_pull_ofp11_buckets(msg, msg->size, ofp_version,
8717 /* OF1.3.5+ prescribes an error when an OFPGC_DELETE includes buckets. */
8719 && ofp_version >= OFP13_VERSION
8720 && gm->command == OFPGC11_DELETE
8721 && !list_is_empty(&gm->buckets)) {
8722 error = OFPERR_OFPGMFC_INVALID_GROUP;
8729 ofputil_pull_ofp15_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
8730 struct ofputil_group_mod *gm)
8732 const struct ofp15_group_mod *ogm;
8733 uint16_t bucket_list_len;
8734 enum ofperr error = OFPERR_OFPGMFC_BAD_BUCKET;
8736 ogm = ofpbuf_pull(msg, sizeof *ogm);
8737 gm->command = ntohs(ogm->command);
8738 gm->type = ogm->type;
8739 gm->group_id = ntohl(ogm->group_id);
8741 gm->command_bucket_id = ntohl(ogm->command_bucket_id);
8742 switch (gm->command) {
8743 case OFPGC15_REMOVE_BUCKET:
8744 if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
8748 case OFPGC15_INSERT_BUCKET:
8749 if (gm->command_bucket_id <= OFPG15_BUCKET_MAX ||
8750 gm->command_bucket_id == OFPG15_BUCKET_FIRST
8751 || gm->command_bucket_id == OFPG15_BUCKET_LAST) {
8757 case OFPGC11_MODIFY:
8758 case OFPGC11_DELETE:
8760 if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
8766 VLOG_WARN_RL(&bad_ofmsg_rl,
8767 "group command bucket id (%u) is out of range",
8768 gm->command_bucket_id);
8769 return OFPERR_OFPGMFC_BAD_BUCKET;
8772 bucket_list_len = ntohs(ogm->bucket_array_len);
8773 error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, ofp_version,
8774 gm->type, &gm->buckets);
8779 return parse_ofp15_group_properties(msg, gm->type, gm->command, &gm->props,
8783 /* Converts OpenFlow group mod message 'oh' into an abstract group mod in
8784 * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */
8786 ofputil_decode_group_mod(const struct ofp_header *oh,
8787 struct ofputil_group_mod *gm)
8789 enum ofp_version ofp_version = oh->version;
8791 struct ofputil_bucket *bucket;
8794 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
8795 ofpraw_pull_assert(&msg);
8797 ofputil_init_group_properties(&gm->props);
8799 switch (ofp_version)
8805 err = ofputil_pull_ofp11_group_mod(&msg, ofp_version, gm);
8809 err = ofputil_pull_ofp15_group_mod(&msg, ofp_version, gm);
8822 case OFPGT11_INDIRECT:
8823 if (!list_is_singleton(&gm->buckets)) {
8824 return OFPERR_OFPGMFC_INVALID_GROUP;
8828 case OFPGT11_SELECT:
8832 return OFPERR_OFPGMFC_BAD_TYPE;
8835 switch (gm->command) {
8837 case OFPGC11_MODIFY:
8838 case OFPGC11_DELETE:
8839 case OFPGC15_INSERT_BUCKET:
8841 case OFPGC15_REMOVE_BUCKET:
8842 if (!list_is_empty(&gm->buckets)) {
8843 return OFPERR_OFPGMFC_BAD_BUCKET;
8847 return OFPERR_OFPGMFC_BAD_COMMAND;
8850 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
8851 if (bucket->weight && gm->type != OFPGT11_SELECT) {
8852 return OFPERR_OFPGMFC_INVALID_GROUP;
8857 case OFPGT11_INDIRECT:
8858 if (ofputil_bucket_has_liveness(bucket)) {
8859 return OFPERR_OFPGMFC_WATCH_UNSUPPORTED;
8862 case OFPGT11_SELECT:
8865 if (!ofputil_bucket_has_liveness(bucket)) {
8866 return OFPERR_OFPGMFC_INVALID_GROUP;
8870 /* Returning BAD TYPE to be consistent
8871 * though gm->type has been checked already. */
8872 return OFPERR_OFPGMFC_BAD_TYPE;
8879 /* Parse a queue status request message into 'oqsr'.
8880 * Returns 0 if successful, otherwise an OFPERR_* number. */
8882 ofputil_decode_queue_stats_request(const struct ofp_header *request,
8883 struct ofputil_queue_stats_request *oqsr)
8885 switch ((enum ofp_version)request->version) {
8890 case OFP11_VERSION: {
8891 const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request);
8892 oqsr->queue_id = ntohl(qsr11->queue_id);
8893 return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no);
8896 case OFP10_VERSION: {
8897 const struct ofp10_queue_stats_request *qsr10 = ofpmsg_body(request);
8898 oqsr->queue_id = ntohl(qsr10->queue_id);
8899 oqsr->port_no = u16_to_ofp(ntohs(qsr10->port_no));
8900 /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
8901 if (oqsr->port_no == OFPP_ALL) {
8902 oqsr->port_no = OFPP_ANY;
8912 /* Encode a queue stats request for 'oqsr', the encoded message
8913 * will be for OpenFlow version 'ofp_version'. Returns message
8914 * as a struct ofpbuf. Returns encoded message on success, NULL on error. */
8916 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
8917 const struct ofputil_queue_stats_request *oqsr)
8919 struct ofpbuf *request;
8921 switch (ofp_version) {
8926 case OFP15_VERSION: {
8927 struct ofp11_queue_stats_request *req;
8928 request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0);
8929 req = ofpbuf_put_zeros(request, sizeof *req);
8930 req->port_no = ofputil_port_to_ofp11(oqsr->port_no);
8931 req->queue_id = htonl(oqsr->queue_id);
8934 case OFP10_VERSION: {
8935 struct ofp10_queue_stats_request *req;
8936 request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0);
8937 req = ofpbuf_put_zeros(request, sizeof *req);
8938 /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
8939 req->port_no = htons(ofp_to_u16(oqsr->port_no == OFPP_ANY
8940 ? OFPP_ALL : oqsr->port_no));
8941 req->queue_id = htonl(oqsr->queue_id);
8951 /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
8954 ofputil_count_queue_stats(const struct ofp_header *oh)
8956 struct ofputil_queue_stats qs;
8960 ofpbuf_use_const(&b, oh, ntohs(oh->length));
8961 ofpraw_pull_assert(&b);
8962 while (!ofputil_decode_queue_stats(&qs, &b)) {
8969 ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs,
8970 const struct ofp10_queue_stats *qs10)
8972 oqs->port_no = u16_to_ofp(ntohs(qs10->port_no));
8973 oqs->queue_id = ntohl(qs10->queue_id);
8974 oqs->tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes));
8975 oqs->tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets));
8976 oqs->tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors));
8977 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
8983 ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs,
8984 const struct ofp11_queue_stats *qs11)
8988 error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no);
8993 oqs->queue_id = ntohl(qs11->queue_id);
8994 oqs->tx_bytes = ntohll(qs11->tx_bytes);
8995 oqs->tx_packets = ntohll(qs11->tx_packets);
8996 oqs->tx_errors = ntohll(qs11->tx_errors);
8997 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
9003 ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats *oqs,
9004 const struct ofp13_queue_stats *qs13)
9006 enum ofperr error = ofputil_queue_stats_from_ofp11(oqs, &qs13->qs);
9008 oqs->duration_sec = ntohl(qs13->duration_sec);
9009 oqs->duration_nsec = ntohl(qs13->duration_nsec);
9016 ofputil_pull_ofp14_queue_stats(struct ofputil_queue_stats *oqs,
9019 const struct ofp14_queue_stats *qs14;
9022 qs14 = ofpbuf_try_pull(msg, sizeof *qs14);
9024 return OFPERR_OFPBRC_BAD_LEN;
9027 len = ntohs(qs14->length);
9028 if (len < sizeof *qs14 || len - sizeof *qs14 > msg->size) {
9029 return OFPERR_OFPBRC_BAD_LEN;
9031 ofpbuf_pull(msg, len - sizeof *qs14);
9033 /* No properties yet defined, so ignore them for now. */
9035 return ofputil_queue_stats_from_ofp13(oqs, &qs14->qs);
9038 /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
9039 * ofputil_queue_stats in 'qs'.
9041 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
9042 * message. Calling this function multiple times for a single 'msg' iterates
9043 * through the replies. The caller must initially leave 'msg''s layer pointers
9044 * null and not modify them between calls.
9046 * Returns 0 if successful, EOF if no replies were left in this 'msg',
9047 * otherwise a positive errno value. */
9049 ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg)
9054 error = (msg->header ? ofpraw_decode(&raw, msg->header)
9055 : ofpraw_pull(&raw, msg));
9062 } else if (raw == OFPRAW_OFPST14_QUEUE_REPLY) {
9063 return ofputil_pull_ofp14_queue_stats(qs, msg);
9064 } else if (raw == OFPRAW_OFPST13_QUEUE_REPLY) {
9065 const struct ofp13_queue_stats *qs13;
9067 qs13 = ofpbuf_try_pull(msg, sizeof *qs13);
9071 return ofputil_queue_stats_from_ofp13(qs, qs13);
9072 } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) {
9073 const struct ofp11_queue_stats *qs11;
9075 qs11 = ofpbuf_try_pull(msg, sizeof *qs11);
9079 return ofputil_queue_stats_from_ofp11(qs, qs11);
9080 } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) {
9081 const struct ofp10_queue_stats *qs10;
9083 qs10 = ofpbuf_try_pull(msg, sizeof *qs10);
9087 return ofputil_queue_stats_from_ofp10(qs, qs10);
9093 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %"PRIu32" leftover "
9094 "bytes at end", msg->size);
9095 return OFPERR_OFPBRC_BAD_LEN;
9099 ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs,
9100 struct ofp10_queue_stats *qs10)
9102 qs10->port_no = htons(ofp_to_u16(oqs->port_no));
9103 memset(qs10->pad, 0, sizeof qs10->pad);
9104 qs10->queue_id = htonl(oqs->queue_id);
9105 put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->tx_bytes));
9106 put_32aligned_be64(&qs10->tx_packets, htonll(oqs->tx_packets));
9107 put_32aligned_be64(&qs10->tx_errors, htonll(oqs->tx_errors));
9111 ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs,
9112 struct ofp11_queue_stats *qs11)
9114 qs11->port_no = ofputil_port_to_ofp11(oqs->port_no);
9115 qs11->queue_id = htonl(oqs->queue_id);
9116 qs11->tx_bytes = htonll(oqs->tx_bytes);
9117 qs11->tx_packets = htonll(oqs->tx_packets);
9118 qs11->tx_errors = htonll(oqs->tx_errors);
9122 ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats *oqs,
9123 struct ofp13_queue_stats *qs13)
9125 ofputil_queue_stats_to_ofp11(oqs, &qs13->qs);
9126 if (oqs->duration_sec != UINT32_MAX) {
9127 qs13->duration_sec = htonl(oqs->duration_sec);
9128 qs13->duration_nsec = htonl(oqs->duration_nsec);
9130 qs13->duration_sec = OVS_BE32_MAX;
9131 qs13->duration_nsec = OVS_BE32_MAX;
9136 ofputil_queue_stats_to_ofp14(const struct ofputil_queue_stats *oqs,
9137 struct ofp14_queue_stats *qs14)
9139 qs14->length = htons(sizeof *qs14);
9140 memset(qs14->pad, 0, sizeof qs14->pad);
9141 ofputil_queue_stats_to_ofp13(oqs, &qs14->qs);
9145 /* Encode a queue stat for 'oqs' and append it to 'replies'. */
9147 ofputil_append_queue_stat(struct ovs_list *replies,
9148 const struct ofputil_queue_stats *oqs)
9150 switch (ofpmp_version(replies)) {
9151 case OFP13_VERSION: {
9152 struct ofp13_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
9153 ofputil_queue_stats_to_ofp13(oqs, reply);
9158 case OFP11_VERSION: {
9159 struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
9160 ofputil_queue_stats_to_ofp11(oqs, reply);
9164 case OFP10_VERSION: {
9165 struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
9166 ofputil_queue_stats_to_ofp10(oqs, reply);
9171 case OFP15_VERSION: {
9172 struct ofp14_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
9173 ofputil_queue_stats_to_ofp14(oqs, reply);
9183 ofputil_decode_bundle_ctrl(const struct ofp_header *oh,
9184 struct ofputil_bundle_ctrl_msg *msg)
9188 const struct ofp14_bundle_ctrl_msg *m;
9190 ofpbuf_use_const(&b, oh, ntohs(oh->length));
9191 raw = ofpraw_pull_assert(&b);
9192 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_CONTROL);
9195 msg->bundle_id = ntohl(m->bundle_id);
9196 msg->type = ntohs(m->type);
9197 msg->flags = ntohs(m->flags);
9203 ofputil_encode_bundle_ctrl_request(enum ofp_version ofp_version,
9204 struct ofputil_bundle_ctrl_msg *bc)
9206 struct ofpbuf *request;
9207 struct ofp14_bundle_ctrl_msg *m;
9209 switch (ofp_version) {
9214 ovs_fatal(0, "bundles need OpenFlow 1.4 or later "
9215 "(\'-O OpenFlow14\')");
9218 request = ofpraw_alloc(OFPRAW_OFPT14_BUNDLE_CONTROL, ofp_version, 0);
9219 m = ofpbuf_put_zeros(request, sizeof *m);
9221 m->bundle_id = htonl(bc->bundle_id);
9222 m->type = htons(bc->type);
9223 m->flags = htons(bc->flags);
9233 ofputil_encode_bundle_ctrl_reply(const struct ofp_header *oh,
9234 struct ofputil_bundle_ctrl_msg *msg)
9237 struct ofp14_bundle_ctrl_msg *m;
9239 buf = ofpraw_alloc_reply(OFPRAW_OFPT14_BUNDLE_CONTROL, oh, 0);
9240 m = ofpbuf_put_zeros(buf, sizeof *m);
9242 m->bundle_id = htonl(msg->bundle_id);
9243 m->type = htons(msg->type);
9244 m->flags = htons(msg->flags);
9249 /* Return true for bundlable state change requests, false for other messages.
9252 ofputil_is_bundlable(enum ofptype type)
9255 /* Minimum required by OpenFlow 1.4. */
9256 case OFPTYPE_PORT_MOD:
9257 case OFPTYPE_FLOW_MOD:
9260 /* Nice to have later. */
9261 case OFPTYPE_FLOW_MOD_TABLE_ID:
9262 case OFPTYPE_GROUP_MOD:
9263 case OFPTYPE_TABLE_MOD:
9264 case OFPTYPE_METER_MOD:
9265 case OFPTYPE_PACKET_OUT:
9266 case OFPTYPE_NXT_TLV_TABLE_MOD:
9268 /* Not to be bundlable. */
9269 case OFPTYPE_ECHO_REQUEST:
9270 case OFPTYPE_FEATURES_REQUEST:
9271 case OFPTYPE_GET_CONFIG_REQUEST:
9272 case OFPTYPE_SET_CONFIG:
9273 case OFPTYPE_BARRIER_REQUEST:
9274 case OFPTYPE_ROLE_REQUEST:
9275 case OFPTYPE_ECHO_REPLY:
9276 case OFPTYPE_SET_FLOW_FORMAT:
9277 case OFPTYPE_SET_PACKET_IN_FORMAT:
9278 case OFPTYPE_SET_CONTROLLER_ID:
9279 case OFPTYPE_FLOW_AGE:
9280 case OFPTYPE_FLOW_MONITOR_CANCEL:
9281 case OFPTYPE_SET_ASYNC_CONFIG:
9282 case OFPTYPE_GET_ASYNC_REQUEST:
9283 case OFPTYPE_DESC_STATS_REQUEST:
9284 case OFPTYPE_FLOW_STATS_REQUEST:
9285 case OFPTYPE_AGGREGATE_STATS_REQUEST:
9286 case OFPTYPE_TABLE_STATS_REQUEST:
9287 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
9288 case OFPTYPE_TABLE_DESC_REQUEST:
9289 case OFPTYPE_PORT_STATS_REQUEST:
9290 case OFPTYPE_QUEUE_STATS_REQUEST:
9291 case OFPTYPE_PORT_DESC_STATS_REQUEST:
9292 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
9293 case OFPTYPE_METER_STATS_REQUEST:
9294 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
9295 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
9296 case OFPTYPE_GROUP_STATS_REQUEST:
9297 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
9298 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
9299 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
9300 case OFPTYPE_BUNDLE_CONTROL:
9301 case OFPTYPE_BUNDLE_ADD_MESSAGE:
9304 case OFPTYPE_FEATURES_REPLY:
9305 case OFPTYPE_GET_CONFIG_REPLY:
9306 case OFPTYPE_PACKET_IN:
9307 case OFPTYPE_FLOW_REMOVED:
9308 case OFPTYPE_PORT_STATUS:
9309 case OFPTYPE_BARRIER_REPLY:
9310 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
9311 case OFPTYPE_DESC_STATS_REPLY:
9312 case OFPTYPE_FLOW_STATS_REPLY:
9313 case OFPTYPE_QUEUE_STATS_REPLY:
9314 case OFPTYPE_PORT_STATS_REPLY:
9315 case OFPTYPE_TABLE_STATS_REPLY:
9316 case OFPTYPE_AGGREGATE_STATS_REPLY:
9317 case OFPTYPE_PORT_DESC_STATS_REPLY:
9318 case OFPTYPE_ROLE_REPLY:
9319 case OFPTYPE_FLOW_MONITOR_PAUSED:
9320 case OFPTYPE_FLOW_MONITOR_RESUMED:
9321 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
9322 case OFPTYPE_GET_ASYNC_REPLY:
9323 case OFPTYPE_GROUP_STATS_REPLY:
9324 case OFPTYPE_GROUP_DESC_STATS_REPLY:
9325 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
9326 case OFPTYPE_METER_STATS_REPLY:
9327 case OFPTYPE_METER_CONFIG_STATS_REPLY:
9328 case OFPTYPE_METER_FEATURES_STATS_REPLY:
9329 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
9330 case OFPTYPE_TABLE_DESC_REPLY:
9331 case OFPTYPE_ROLE_STATUS:
9332 case OFPTYPE_REQUESTFORWARD:
9333 case OFPTYPE_NXT_TLV_TABLE_REQUEST:
9334 case OFPTYPE_NXT_TLV_TABLE_REPLY:
9342 ofputil_decode_bundle_add(const struct ofp_header *oh,
9343 struct ofputil_bundle_add_msg *msg,
9344 enum ofptype *type_ptr)
9346 const struct ofp14_bundle_ctrl_msg *m;
9353 ofpbuf_use_const(&b, oh, ntohs(oh->length));
9354 raw = ofpraw_pull_assert(&b);
9355 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE);
9357 m = ofpbuf_pull(&b, sizeof *m);
9358 msg->bundle_id = ntohl(m->bundle_id);
9359 msg->flags = ntohs(m->flags);
9362 if (msg->msg->version != oh->version) {
9363 return OFPERR_NXBFC_BAD_VERSION;
9365 inner_len = ntohs(msg->msg->length);
9366 if (inner_len < sizeof(struct ofp_header) || inner_len > b.size) {
9367 return OFPERR_OFPBFC_MSG_BAD_LEN;
9369 if (msg->msg->xid != oh->xid) {
9370 return OFPERR_OFPBFC_MSG_BAD_XID;
9373 /* Reject unbundlable messages. */
9377 error = ofptype_decode(type_ptr, msg->msg);
9379 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPT14_BUNDLE_ADD_MESSAGE contained "
9380 "message is unparsable (%s)", ofperr_get_name(error));
9381 return OFPERR_OFPBFC_MSG_UNSUP; /* 'error' would be confusing. */
9384 if (!ofputil_is_bundlable(*type_ptr)) {
9385 VLOG_WARN_RL(&bad_ofmsg_rl, "%s message not allowed inside "
9386 "OFPT14_BUNDLE_ADD_MESSAGE", ofptype_get_name(*type_ptr));
9387 return OFPERR_OFPBFC_MSG_UNSUP;
9394 ofputil_encode_bundle_add(enum ofp_version ofp_version,
9395 struct ofputil_bundle_add_msg *msg)
9397 struct ofpbuf *request;
9398 struct ofp14_bundle_ctrl_msg *m;
9400 /* Must use the same xid as the embedded message. */
9401 request = ofpraw_alloc_xid(OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE, ofp_version,
9403 m = ofpbuf_put_zeros(request, sizeof *m);
9405 m->bundle_id = htonl(msg->bundle_id);
9406 m->flags = htons(msg->flags);
9407 ofpbuf_put(request, msg->msg, ntohs(msg->msg->length));
9413 encode_tlv_table_mappings(struct ofpbuf *b, struct ovs_list *mappings)
9415 struct ofputil_tlv_map *map;
9417 LIST_FOR_EACH (map, list_node, mappings) {
9418 struct nx_tlv_map *nx_map;
9420 nx_map = ofpbuf_put_zeros(b, sizeof *nx_map);
9421 nx_map->option_class = htons(map->option_class);
9422 nx_map->option_type = map->option_type;
9423 nx_map->option_len = map->option_len;
9424 nx_map->index = htons(map->index);
9429 ofputil_encode_tlv_table_mod(enum ofp_version ofp_version,
9430 struct ofputil_tlv_table_mod *ttm)
9433 struct nx_tlv_table_mod *nx_ttm;
9435 b = ofpraw_alloc(OFPRAW_NXT_TLV_TABLE_MOD, ofp_version, 0);
9436 nx_ttm = ofpbuf_put_zeros(b, sizeof *nx_ttm);
9437 nx_ttm->command = htons(ttm->command);
9438 encode_tlv_table_mappings(b, &ttm->mappings);
9444 decode_tlv_table_mappings(struct ofpbuf *msg, unsigned int max_fields,
9445 struct ovs_list *mappings)
9447 list_init(mappings);
9450 struct nx_tlv_map *nx_map;
9451 struct ofputil_tlv_map *map;
9453 nx_map = ofpbuf_pull(msg, sizeof *nx_map);
9454 map = xmalloc(sizeof *map);
9455 list_push_back(mappings, &map->list_node);
9457 map->option_class = ntohs(nx_map->option_class);
9458 map->option_type = nx_map->option_type;
9460 map->option_len = nx_map->option_len;
9461 if (map->option_len % 4 || map->option_len > TLV_MAX_OPT_SIZE) {
9462 VLOG_WARN_RL(&bad_ofmsg_rl,
9463 "tlv table option length (%u) is not a valid option size",
9465 ofputil_uninit_tlv_table(mappings);
9466 return OFPERR_NXTTMFC_BAD_OPT_LEN;
9469 map->index = ntohs(nx_map->index);
9470 if (map->index >= max_fields) {
9471 VLOG_WARN_RL(&bad_ofmsg_rl,
9472 "tlv table field index (%u) is too large (max %u)",
9473 map->index, max_fields - 1);
9474 ofputil_uninit_tlv_table(mappings);
9475 return OFPERR_NXTTMFC_BAD_FIELD_IDX;
9483 ofputil_decode_tlv_table_mod(const struct ofp_header *oh,
9484 struct ofputil_tlv_table_mod *ttm)
9487 struct nx_tlv_table_mod *nx_ttm;
9489 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
9490 ofpraw_pull_assert(&msg);
9492 nx_ttm = ofpbuf_pull(&msg, sizeof *nx_ttm);
9493 ttm->command = ntohs(nx_ttm->command);
9494 if (ttm->command > NXTTMC_CLEAR) {
9495 VLOG_WARN_RL(&bad_ofmsg_rl,
9496 "tlv table mod command (%u) is out of range",
9498 return OFPERR_NXTTMFC_BAD_COMMAND;
9501 return decode_tlv_table_mappings(&msg, TUN_METADATA_NUM_OPTS,
9506 ofputil_encode_tlv_table_reply(const struct ofp_header *oh,
9507 struct ofputil_tlv_table_reply *ttr)
9510 struct nx_tlv_table_reply *nx_ttr;
9512 b = ofpraw_alloc_reply(OFPRAW_NXT_TLV_TABLE_REPLY, oh, 0);
9513 nx_ttr = ofpbuf_put_zeros(b, sizeof *nx_ttr);
9514 nx_ttr->max_option_space = htonl(ttr->max_option_space);
9515 nx_ttr->max_fields = htons(ttr->max_fields);
9517 encode_tlv_table_mappings(b, &ttr->mappings);
9522 /* Decodes the NXT_TLV_TABLE_REPLY message in 'oh' into '*ttr'. Returns 0
9523 * if successful, otherwise an ofperr.
9525 * The decoder verifies that the indexes in 'ttr->mappings' are less than
9526 * 'ttr->max_fields', but the caller must ensure, if necessary, that they are
9527 * less than TUN_METADATA_NUM_OPTS. */
9529 ofputil_decode_tlv_table_reply(const struct ofp_header *oh,
9530 struct ofputil_tlv_table_reply *ttr)
9533 struct nx_tlv_table_reply *nx_ttr;
9535 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
9536 ofpraw_pull_assert(&msg);
9538 nx_ttr = ofpbuf_pull(&msg, sizeof *nx_ttr);
9539 ttr->max_option_space = ntohl(nx_ttr->max_option_space);
9540 ttr->max_fields = ntohs(nx_ttr->max_fields);
9542 return decode_tlv_table_mappings(&msg, ttr->max_fields, &ttr->mappings);
9546 ofputil_uninit_tlv_table(struct ovs_list *mappings)
9548 struct ofputil_tlv_map *map;
9550 LIST_FOR_EACH_POP (map, list_node, mappings) {
9555 /* Decodes the OpenFlow "set async config" request and "get async config
9556 * reply" message in '*oh' into an abstract form in 'master' and 'slave'.
9558 * If 'loose' is true, this function ignores properties and values that it does
9559 * not understand, as a controller would want to do when interpreting
9560 * capabilities provided by a switch. If 'loose' is false, this function
9561 * treats unknown properties and values as an error, as a switch would want to
9562 * do when interpreting a configuration request made by a controller.
9564 * Returns 0 if successful, otherwise an OFPERR_* value.
9566 * Returns error code OFPERR_OFPACFC_INVALID if the value of mask is not in
9567 * the valid range of mask.
9569 * Returns error code OFPERR_OFPACFC_UNSUPPORTED if the configuration is not
9572 ofputil_decode_set_async_config(const struct ofp_header *oh,
9573 uint32_t master[OAM_N_TYPES],
9574 uint32_t slave[OAM_N_TYPES],
9580 ofpbuf_use_const(&b, oh, ntohs(oh->length));
9581 raw = ofpraw_pull_assert(&b);
9583 if (raw == OFPRAW_OFPT13_SET_ASYNC ||
9584 raw == OFPRAW_NXT_SET_ASYNC_CONFIG ||
9585 raw == OFPRAW_OFPT13_GET_ASYNC_REPLY) {
9586 const struct nx_async_config *msg = ofpmsg_body(oh);
9588 master[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[0]);
9589 master[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[0]);
9590 master[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[0]);
9592 slave[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[1]);
9593 slave[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[1]);
9594 slave[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[1]);
9596 } else if (raw == OFPRAW_OFPT14_SET_ASYNC ||
9597 raw == OFPRAW_OFPT14_GET_ASYNC_REPLY) {
9599 while (b.size > 0) {
9600 struct ofp14_async_config_prop_reasons *msg;
9601 struct ofpbuf property;
9605 error = ofpprop_pull(&b, &property, &type);
9610 msg = property.data;
9612 if (property.size != sizeof *msg) {
9613 return OFPERR_OFPBRC_BAD_LEN;
9617 error = ofputil_check_mask(type, ntohl(msg->mask));
9624 case OFPACPT_PACKET_IN_SLAVE:
9625 slave[OAM_PACKET_IN] = ntohl(msg->mask);
9628 case OFPACPT_PACKET_IN_MASTER:
9629 master[OAM_PACKET_IN] = ntohl(msg->mask);
9632 case OFPACPT_PORT_STATUS_SLAVE:
9633 slave[OAM_PORT_STATUS] = ntohl(msg->mask);
9636 case OFPACPT_PORT_STATUS_MASTER:
9637 master[OAM_PORT_STATUS] = ntohl(msg->mask);
9640 case OFPACPT_FLOW_REMOVED_SLAVE:
9641 slave[OAM_FLOW_REMOVED] = ntohl(msg->mask);
9644 case OFPACPT_FLOW_REMOVED_MASTER:
9645 master[OAM_FLOW_REMOVED] = ntohl(msg->mask);
9648 case OFPACPT_ROLE_STATUS_SLAVE:
9649 slave[OAM_ROLE_STATUS] = ntohl(msg->mask);
9652 case OFPACPT_ROLE_STATUS_MASTER:
9653 master[OAM_ROLE_STATUS] = ntohl(msg->mask);
9656 case OFPACPT_TABLE_STATUS_SLAVE:
9657 slave[OAM_TABLE_STATUS] = ntohl(msg->mask);
9660 case OFPACPT_TABLE_STATUS_MASTER:
9661 master[OAM_TABLE_STATUS] = ntohl(msg->mask);
9664 case OFPACPT_REQUESTFORWARD_SLAVE:
9665 slave[OAM_REQUESTFORWARD] = ntohl(msg->mask);
9668 case OFPACPT_REQUESTFORWARD_MASTER:
9669 master[OAM_REQUESTFORWARD] = ntohl(msg->mask);
9673 error = loose ? 0 : OFPERR_OFPACFC_UNSUPPORTED;
9678 return OFPERR_OFPBRC_BAD_VERSION;
9683 /* Append all asynchronous configuration properties in GET_ASYNC_REPLY
9684 * message, describing if various set of asynchronous messages are enabled
9687 ofputil_get_async_reply(struct ofpbuf *buf, const uint32_t master_mask,
9688 const uint32_t slave_mask, const uint32_t type)
9692 for (role = 0; role < 2; role++) {
9693 struct ofp14_async_config_prop_reasons *msg;
9695 msg = ofpbuf_put_zeros(buf, sizeof *msg);
9699 msg->type = (role ? htons(OFPACPT_PACKET_IN_SLAVE)
9700 : htons(OFPACPT_PACKET_IN_MASTER));
9703 case OAM_PORT_STATUS:
9704 msg->type = (role ? htons(OFPACPT_PORT_STATUS_SLAVE)
9705 : htons(OFPACPT_PORT_STATUS_MASTER));
9708 case OAM_FLOW_REMOVED:
9709 msg->type = (role ? htons(OFPACPT_FLOW_REMOVED_SLAVE)
9710 : htons(OFPACPT_FLOW_REMOVED_MASTER));
9713 case OAM_ROLE_STATUS:
9714 msg->type = (role ? htons(OFPACPT_ROLE_STATUS_SLAVE)
9715 : htons(OFPACPT_ROLE_STATUS_MASTER));
9718 case OAM_TABLE_STATUS:
9719 msg->type = (role ? htons(OFPACPT_TABLE_STATUS_SLAVE)
9720 : htons(OFPACPT_TABLE_STATUS_MASTER));
9723 case OAM_REQUESTFORWARD:
9724 msg->type = (role ? htons(OFPACPT_REQUESTFORWARD_SLAVE)
9725 : htons(OFPACPT_REQUESTFORWARD_MASTER));
9729 return OFPERR_OFPBRC_BAD_TYPE;
9731 msg->length = htons(sizeof *msg);
9732 msg->mask = (role ? htonl(slave_mask) : htonl(master_mask));
9738 /* Returns a OpenFlow message that encodes 'asynchronous configuration' properly
9739 * as a reply to get async config request. */
9741 ofputil_encode_get_async_config(const struct ofp_header *oh,
9742 uint32_t master[OAM_N_TYPES],
9743 uint32_t slave[OAM_N_TYPES])
9748 buf = ofpraw_alloc_reply((oh->version < OFP14_VERSION
9749 ? OFPRAW_OFPT13_GET_ASYNC_REPLY
9750 : OFPRAW_OFPT14_GET_ASYNC_REPLY), oh, 0);
9752 if (oh->version < OFP14_VERSION) {
9753 struct nx_async_config *msg;
9754 msg = ofpbuf_put_zeros(buf, sizeof *msg);
9756 msg->packet_in_mask[0] = htonl(master[OAM_PACKET_IN]);
9757 msg->port_status_mask[0] = htonl(master[OAM_PORT_STATUS]);
9758 msg->flow_removed_mask[0] = htonl(master[OAM_FLOW_REMOVED]);
9760 msg->packet_in_mask[1] = htonl(slave[OAM_PACKET_IN]);
9761 msg->port_status_mask[1] = htonl(slave[OAM_PORT_STATUS]);
9762 msg->flow_removed_mask[1] = htonl(slave[OAM_FLOW_REMOVED]);
9763 } else if (oh->version == OFP14_VERSION) {
9764 for (type = 0; type < OAM_N_TYPES; type++) {
9765 ofputil_get_async_reply(buf, master[type], slave[type], type);