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"
41 #include "openflow/netronome-ext.h"
44 #include "tun-metadata.h"
45 #include "unaligned.h"
46 #include "type-props.h"
47 #include "openvswitch/vlog.h"
50 VLOG_DEFINE_THIS_MODULE(ofp_util);
52 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
53 * in the peer and so there's not much point in showing a lot of them. */
54 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
56 static enum ofputil_table_vacancy ofputil_decode_table_vacancy(
57 ovs_be32 config, enum ofp_version);
58 static enum ofputil_table_eviction ofputil_decode_table_eviction(
59 ovs_be32 config, enum ofp_version);
60 static ovs_be32 ofputil_encode_table_config(enum ofputil_table_miss,
61 enum ofputil_table_eviction,
62 enum ofputil_table_vacancy,
65 struct ofp_prop_header {
70 struct ofp_prop_experimenter {
71 ovs_be16 type; /* OFP*_EXPERIMENTER. */
72 ovs_be16 length; /* Length in bytes of this property. */
73 ovs_be32 experimenter; /* Experimenter ID which takes the same form as
74 * in struct ofp_experimenter_header. */
75 ovs_be32 exp_type; /* Experimenter defined. */
78 /* Pulls a property, beginning with struct ofp_prop_header, from the beginning
79 * of 'msg'. Stores the type of the property in '*typep' and, if 'property' is
80 * nonnull, the entire property, including the header, in '*property'. Returns
81 * 0 if successful, otherwise an error code.
83 * This function pulls the property's stated size padded out to a multiple of
84 * 'alignment' bytes. The common case in OpenFlow is an 'alignment' of 8, so
85 * you can use ofputil_pull_property() for that case. */
87 ofputil_pull_property__(struct ofpbuf *msg, struct ofpbuf *property,
88 unsigned int alignment, uint16_t *typep)
90 struct ofp_prop_header *oph;
91 unsigned int padded_len;
94 if (msg->size < sizeof *oph) {
95 return OFPERR_OFPBPC_BAD_LEN;
99 len = ntohs(oph->len);
100 padded_len = ROUND_UP(len, alignment);
101 if (len < sizeof *oph || padded_len > msg->size) {
102 return OFPERR_OFPBPC_BAD_LEN;
105 *typep = ntohs(oph->type);
107 ofpbuf_use_const(property, msg->data, len);
109 ofpbuf_pull(msg, padded_len);
113 /* Pulls a property, beginning with struct ofp_prop_header, from the beginning
114 * of 'msg'. Stores the type of the property in '*typep' and, if 'property' is
115 * nonnull, the entire property, including the header, in '*property'. Returns
116 * 0 if successful, otherwise an error code.
118 * This function pulls the property's stated size padded out to a multiple of
119 * 8 bytes, which is the common case for OpenFlow properties. */
121 ofputil_pull_property(struct ofpbuf *msg, struct ofpbuf *property,
124 return ofputil_pull_property__(msg, property, 8, typep);
127 static void OVS_PRINTF_FORMAT(2, 3)
128 log_property(bool loose, const char *message, ...)
130 enum vlog_level level = loose ? VLL_DBG : VLL_WARN;
131 if (!vlog_should_drop(THIS_MODULE, level, &bad_ofmsg_rl)) {
134 va_start(args, message);
135 vlog_valist(THIS_MODULE, level, message, args);
141 ofputil_check_mask(uint16_t type, uint32_t mask)
144 case OFPACPT_PACKET_IN_SLAVE:
145 case OFPACPT_PACKET_IN_MASTER:
146 if (mask > MAXIMUM_MASK_PACKET_IN) {
147 return OFPERR_OFPACFC_INVALID;
151 case OFPACPT_FLOW_REMOVED_SLAVE:
152 case OFPACPT_FLOW_REMOVED_MASTER:
153 if (mask > MAXIMUM_MASK_FLOW_REMOVED) {
154 return OFPERR_OFPACFC_INVALID;
158 case OFPACPT_PORT_STATUS_SLAVE:
159 case OFPACPT_PORT_STATUS_MASTER:
160 if (mask > MAXIMUM_MASK_PORT_STATUS) {
161 return OFPERR_OFPACFC_INVALID;
165 case OFPACPT_ROLE_STATUS_SLAVE:
166 case OFPACPT_ROLE_STATUS_MASTER:
167 if (mask > MAXIMUM_MASK_ROLE_STATUS) {
168 return OFPERR_OFPACFC_INVALID;
172 case OFPACPT_TABLE_STATUS_SLAVE:
173 case OFPACPT_TABLE_STATUS_MASTER:
174 if ((mask < MINIMUM_MASK_TABLE_STATUS && mask != 0) |
175 (mask > MAXIMUM_MASK_TABLE_STATUS)) {
176 return OFPERR_OFPACFC_INVALID;
180 case OFPACPT_REQUESTFORWARD_SLAVE:
181 case OFPACPT_REQUESTFORWARD_MASTER:
182 if (mask > MAXIMUM_MASK_REQUESTFORWARD) {
183 return OFPERR_OFPACFC_INVALID;
191 start_property(struct ofpbuf *msg, uint16_t type)
193 size_t start_ofs = msg->size;
194 struct ofp_prop_header *oph;
196 oph = ofpbuf_put_uninit(msg, sizeof *oph);
197 oph->type = htons(type);
198 oph->len = htons(4); /* May be updated later by end_property(). */
203 end_property(struct ofpbuf *msg, size_t start_ofs)
205 struct ofp_prop_header *oph;
207 oph = ofpbuf_at_assert(msg, start_ofs, sizeof *oph);
208 oph->len = htons(msg->size - start_ofs);
209 ofpbuf_padto(msg, ROUND_UP(msg->size, 8));
213 put_bitmap_properties(struct ofpbuf *msg, uint64_t bitmap)
215 for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
216 start_property(msg, rightmost_1bit_idx(bitmap));
220 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
221 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
224 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
225 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
226 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
227 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
230 ofputil_wcbits_to_netmask(int wcbits)
233 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
236 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
237 * that it wildcards, that is, the number of 0-bits in 'netmask', a number
238 * between 0 and 32 inclusive.
240 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
241 * still be in the valid range but isn't otherwise meaningful. */
243 ofputil_netmask_to_wcbits(ovs_be32 netmask)
245 return 32 - ip_count_cidr_bits(netmask);
248 /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
249 * flow_wildcards in 'wc' for use in struct match. It is the caller's
250 * responsibility to handle the special case where the flow match's dl_vlan is
251 * set to OFP_VLAN_NONE. */
253 ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc)
255 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 35);
257 /* Initialize most of wc. */
258 flow_wildcards_init_catchall(wc);
260 if (!(ofpfw & OFPFW10_IN_PORT)) {
261 wc->masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
264 if (!(ofpfw & OFPFW10_NW_TOS)) {
265 wc->masks.nw_tos |= IP_DSCP_MASK;
268 if (!(ofpfw & OFPFW10_NW_PROTO)) {
269 wc->masks.nw_proto = UINT8_MAX;
271 wc->masks.nw_src = ofputil_wcbits_to_netmask(ofpfw
272 >> OFPFW10_NW_SRC_SHIFT);
273 wc->masks.nw_dst = ofputil_wcbits_to_netmask(ofpfw
274 >> OFPFW10_NW_DST_SHIFT);
276 if (!(ofpfw & OFPFW10_TP_SRC)) {
277 wc->masks.tp_src = OVS_BE16_MAX;
279 if (!(ofpfw & OFPFW10_TP_DST)) {
280 wc->masks.tp_dst = OVS_BE16_MAX;
283 if (!(ofpfw & OFPFW10_DL_SRC)) {
284 WC_MASK_FIELD(wc, dl_src);
286 if (!(ofpfw & OFPFW10_DL_DST)) {
287 WC_MASK_FIELD(wc, dl_dst);
289 if (!(ofpfw & OFPFW10_DL_TYPE)) {
290 wc->masks.dl_type = OVS_BE16_MAX;
294 if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) {
295 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
297 if (!(ofpfw & OFPFW10_DL_VLAN)) {
298 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
302 /* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */
304 ofputil_match_from_ofp10_match(const struct ofp10_match *ofmatch,
307 uint32_t ofpfw = ntohl(ofmatch->wildcards) & OFPFW10_ALL;
309 /* Initialize match->wc. */
310 memset(&match->flow, 0, sizeof match->flow);
311 ofputil_wildcard_from_ofpfw10(ofpfw, &match->wc);
313 /* Initialize most of match->flow. */
314 match->flow.nw_src = ofmatch->nw_src;
315 match->flow.nw_dst = ofmatch->nw_dst;
316 match->flow.in_port.ofp_port = u16_to_ofp(ntohs(ofmatch->in_port));
317 match->flow.dl_type = ofputil_dl_type_from_openflow(ofmatch->dl_type);
318 match->flow.tp_src = ofmatch->tp_src;
319 match->flow.tp_dst = ofmatch->tp_dst;
320 match->flow.dl_src = ofmatch->dl_src;
321 match->flow.dl_dst = ofmatch->dl_dst;
322 match->flow.nw_tos = ofmatch->nw_tos & IP_DSCP_MASK;
323 match->flow.nw_proto = ofmatch->nw_proto;
325 /* Translate VLANs. */
326 if (!(ofpfw & OFPFW10_DL_VLAN) &&
327 ofmatch->dl_vlan == htons(OFP10_VLAN_NONE)) {
328 /* Match only packets without 802.1Q header.
330 * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
332 * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
333 * because we can't have a specific PCP without an 802.1Q header.
334 * However, older versions of OVS treated this as matching packets
335 * withut an 802.1Q header, so we do here too. */
336 match->flow.vlan_tci = htons(0);
337 match->wc.masks.vlan_tci = htons(0xffff);
339 ovs_be16 vid, pcp, tci;
342 vid = ofmatch->dl_vlan & htons(VLAN_VID_MASK);
343 hpcp = (ofmatch->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK;
345 tci = vid | pcp | htons(VLAN_CFI);
346 match->flow.vlan_tci = tci & match->wc.masks.vlan_tci;
350 match_zero_wildcarded_fields(match);
353 /* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */
355 ofputil_match_to_ofp10_match(const struct match *match,
356 struct ofp10_match *ofmatch)
358 const struct flow_wildcards *wc = &match->wc;
361 /* Figure out most OpenFlow wildcards. */
363 if (!wc->masks.in_port.ofp_port) {
364 ofpfw |= OFPFW10_IN_PORT;
366 if (!wc->masks.dl_type) {
367 ofpfw |= OFPFW10_DL_TYPE;
369 if (!wc->masks.nw_proto) {
370 ofpfw |= OFPFW10_NW_PROTO;
372 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_src)
373 << OFPFW10_NW_SRC_SHIFT);
374 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_dst)
375 << OFPFW10_NW_DST_SHIFT);
376 if (!(wc->masks.nw_tos & IP_DSCP_MASK)) {
377 ofpfw |= OFPFW10_NW_TOS;
379 if (!wc->masks.tp_src) {
380 ofpfw |= OFPFW10_TP_SRC;
382 if (!wc->masks.tp_dst) {
383 ofpfw |= OFPFW10_TP_DST;
385 if (eth_addr_is_zero(wc->masks.dl_src)) {
386 ofpfw |= OFPFW10_DL_SRC;
388 if (eth_addr_is_zero(wc->masks.dl_dst)) {
389 ofpfw |= OFPFW10_DL_DST;
392 /* Translate VLANs. */
393 ofmatch->dl_vlan = htons(0);
394 ofmatch->dl_vlan_pcp = 0;
395 if (match->wc.masks.vlan_tci == htons(0)) {
396 ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP;
397 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
398 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
399 ofmatch->dl_vlan = htons(OFP10_VLAN_NONE);
401 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
402 ofpfw |= OFPFW10_DL_VLAN;
404 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
407 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
408 ofpfw |= OFPFW10_DL_VLAN_PCP;
410 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
414 /* Compose most of the match structure. */
415 ofmatch->wildcards = htonl(ofpfw);
416 ofmatch->in_port = htons(ofp_to_u16(match->flow.in_port.ofp_port));
417 ofmatch->dl_src = match->flow.dl_src;
418 ofmatch->dl_dst = match->flow.dl_dst;
419 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
420 ofmatch->nw_src = match->flow.nw_src;
421 ofmatch->nw_dst = match->flow.nw_dst;
422 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
423 ofmatch->nw_proto = match->flow.nw_proto;
424 ofmatch->tp_src = match->flow.tp_src;
425 ofmatch->tp_dst = match->flow.tp_dst;
426 memset(ofmatch->pad1, '\0', sizeof ofmatch->pad1);
427 memset(ofmatch->pad2, '\0', sizeof ofmatch->pad2);
431 ofputil_pull_ofp11_match(struct ofpbuf *buf, struct match *match,
432 uint16_t *padded_match_len)
434 struct ofp11_match_header *omh = buf->data;
437 if (buf->size < sizeof *omh) {
438 return OFPERR_OFPBMC_BAD_LEN;
441 match_len = ntohs(omh->length);
443 switch (ntohs(omh->type)) {
444 case OFPMT_STANDARD: {
445 struct ofp11_match *om;
447 if (match_len != sizeof *om || buf->size < sizeof *om) {
448 return OFPERR_OFPBMC_BAD_LEN;
450 om = ofpbuf_pull(buf, sizeof *om);
451 if (padded_match_len) {
452 *padded_match_len = match_len;
454 return ofputil_match_from_ofp11_match(om, match);
458 if (padded_match_len) {
459 *padded_match_len = ROUND_UP(match_len, 8);
461 return oxm_pull_match(buf, match);
464 return OFPERR_OFPBMC_BAD_TYPE;
468 /* Converts the ofp11_match in 'ofmatch' into a struct match in 'match'.
469 * Returns 0 if successful, otherwise an OFPERR_* value. */
471 ofputil_match_from_ofp11_match(const struct ofp11_match *ofmatch,
474 uint16_t wc = ntohl(ofmatch->wildcards);
475 bool ipv4, arp, rarp;
477 match_init_catchall(match);
479 if (!(wc & OFPFW11_IN_PORT)) {
483 error = ofputil_port_from_ofp11(ofmatch->in_port, &ofp_port);
485 return OFPERR_OFPBMC_BAD_VALUE;
487 match_set_in_port(match, ofp_port);
490 match_set_dl_src_masked(match, ofmatch->dl_src,
491 eth_addr_invert(ofmatch->dl_src_mask));
492 match_set_dl_dst_masked(match, ofmatch->dl_dst,
493 eth_addr_invert(ofmatch->dl_dst_mask));
495 if (!(wc & OFPFW11_DL_VLAN)) {
496 if (ofmatch->dl_vlan == htons(OFPVID11_NONE)) {
497 /* Match only packets without a VLAN tag. */
498 match->flow.vlan_tci = htons(0);
499 match->wc.masks.vlan_tci = OVS_BE16_MAX;
501 if (ofmatch->dl_vlan == htons(OFPVID11_ANY)) {
502 /* Match any packet with a VLAN tag regardless of VID. */
503 match->flow.vlan_tci = htons(VLAN_CFI);
504 match->wc.masks.vlan_tci = htons(VLAN_CFI);
505 } else if (ntohs(ofmatch->dl_vlan) < 4096) {
506 /* Match only packets with the specified VLAN VID. */
507 match->flow.vlan_tci = htons(VLAN_CFI) | ofmatch->dl_vlan;
508 match->wc.masks.vlan_tci = htons(VLAN_CFI | VLAN_VID_MASK);
511 return OFPERR_OFPBMC_BAD_VALUE;
514 if (!(wc & OFPFW11_DL_VLAN_PCP)) {
515 if (ofmatch->dl_vlan_pcp <= 7) {
516 match->flow.vlan_tci |= htons(ofmatch->dl_vlan_pcp
518 match->wc.masks.vlan_tci |= htons(VLAN_PCP_MASK);
521 return OFPERR_OFPBMC_BAD_VALUE;
527 if (!(wc & OFPFW11_DL_TYPE)) {
528 match_set_dl_type(match,
529 ofputil_dl_type_from_openflow(ofmatch->dl_type));
532 ipv4 = match->flow.dl_type == htons(ETH_TYPE_IP);
533 arp = match->flow.dl_type == htons(ETH_TYPE_ARP);
534 rarp = match->flow.dl_type == htons(ETH_TYPE_RARP);
536 if (ipv4 && !(wc & OFPFW11_NW_TOS)) {
537 if (ofmatch->nw_tos & ~IP_DSCP_MASK) {
539 return OFPERR_OFPBMC_BAD_VALUE;
542 match_set_nw_dscp(match, ofmatch->nw_tos);
545 if (ipv4 || arp || rarp) {
546 if (!(wc & OFPFW11_NW_PROTO)) {
547 match_set_nw_proto(match, ofmatch->nw_proto);
549 match_set_nw_src_masked(match, ofmatch->nw_src, ~ofmatch->nw_src_mask);
550 match_set_nw_dst_masked(match, ofmatch->nw_dst, ~ofmatch->nw_dst_mask);
553 #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
554 if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) {
555 switch (match->flow.nw_proto) {
557 /* "A.2.3 Flow Match Structures" in OF1.1 says:
559 * The tp_src and tp_dst fields will be ignored unless the
560 * network protocol specified is as TCP, UDP or SCTP.
562 * but I'm pretty sure we should support ICMP too, otherwise
563 * that's a regression from OF1.0. */
564 if (!(wc & OFPFW11_TP_SRC)) {
565 uint16_t icmp_type = ntohs(ofmatch->tp_src);
566 if (icmp_type < 0x100) {
567 match_set_icmp_type(match, icmp_type);
569 return OFPERR_OFPBMC_BAD_FIELD;
572 if (!(wc & OFPFW11_TP_DST)) {
573 uint16_t icmp_code = ntohs(ofmatch->tp_dst);
574 if (icmp_code < 0x100) {
575 match_set_icmp_code(match, icmp_code);
577 return OFPERR_OFPBMC_BAD_FIELD;
585 if (!(wc & (OFPFW11_TP_SRC))) {
586 match_set_tp_src(match, ofmatch->tp_src);
588 if (!(wc & (OFPFW11_TP_DST))) {
589 match_set_tp_dst(match, ofmatch->tp_dst);
594 /* OF1.1 says explicitly to ignore this. */
599 if (eth_type_mpls(match->flow.dl_type)) {
600 if (!(wc & OFPFW11_MPLS_LABEL)) {
601 match_set_mpls_label(match, 0, ofmatch->mpls_label);
603 if (!(wc & OFPFW11_MPLS_TC)) {
604 match_set_mpls_tc(match, 0, ofmatch->mpls_tc);
608 match_set_metadata_masked(match, ofmatch->metadata,
609 ~ofmatch->metadata_mask);
614 /* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */
616 ofputil_match_to_ofp11_match(const struct match *match,
617 struct ofp11_match *ofmatch)
621 memset(ofmatch, 0, sizeof *ofmatch);
622 ofmatch->omh.type = htons(OFPMT_STANDARD);
623 ofmatch->omh.length = htons(OFPMT11_STANDARD_LENGTH);
625 if (!match->wc.masks.in_port.ofp_port) {
626 wc |= OFPFW11_IN_PORT;
628 ofmatch->in_port = ofputil_port_to_ofp11(match->flow.in_port.ofp_port);
631 ofmatch->dl_src = match->flow.dl_src;
632 ofmatch->dl_src_mask = eth_addr_invert(match->wc.masks.dl_src);
633 ofmatch->dl_dst = match->flow.dl_dst;
634 ofmatch->dl_dst_mask = eth_addr_invert(match->wc.masks.dl_dst);
636 if (match->wc.masks.vlan_tci == htons(0)) {
637 wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP;
638 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
639 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
640 ofmatch->dl_vlan = htons(OFPVID11_NONE);
641 wc |= OFPFW11_DL_VLAN_PCP;
643 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
644 ofmatch->dl_vlan = htons(OFPVID11_ANY);
646 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
649 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
650 wc |= OFPFW11_DL_VLAN_PCP;
652 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
656 if (!match->wc.masks.dl_type) {
657 wc |= OFPFW11_DL_TYPE;
659 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
662 if (!(match->wc.masks.nw_tos & IP_DSCP_MASK)) {
663 wc |= OFPFW11_NW_TOS;
665 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
668 if (!match->wc.masks.nw_proto) {
669 wc |= OFPFW11_NW_PROTO;
671 ofmatch->nw_proto = match->flow.nw_proto;
674 ofmatch->nw_src = match->flow.nw_src;
675 ofmatch->nw_src_mask = ~match->wc.masks.nw_src;
676 ofmatch->nw_dst = match->flow.nw_dst;
677 ofmatch->nw_dst_mask = ~match->wc.masks.nw_dst;
679 if (!match->wc.masks.tp_src) {
680 wc |= OFPFW11_TP_SRC;
682 ofmatch->tp_src = match->flow.tp_src;
685 if (!match->wc.masks.tp_dst) {
686 wc |= OFPFW11_TP_DST;
688 ofmatch->tp_dst = match->flow.tp_dst;
691 if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK))) {
692 wc |= OFPFW11_MPLS_LABEL;
694 ofmatch->mpls_label = htonl(mpls_lse_to_label(
695 match->flow.mpls_lse[0]));
698 if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK))) {
699 wc |= OFPFW11_MPLS_TC;
701 ofmatch->mpls_tc = mpls_lse_to_tc(match->flow.mpls_lse[0]);
704 ofmatch->metadata = match->flow.metadata;
705 ofmatch->metadata_mask = ~match->wc.masks.metadata;
707 ofmatch->wildcards = htonl(wc);
710 /* Returns the "typical" length of a match for 'protocol', for use in
711 * estimating space to preallocate. */
713 ofputil_match_typical_len(enum ofputil_protocol protocol)
716 case OFPUTIL_P_OF10_STD:
717 case OFPUTIL_P_OF10_STD_TID:
718 return sizeof(struct ofp10_match);
720 case OFPUTIL_P_OF10_NXM:
721 case OFPUTIL_P_OF10_NXM_TID:
722 return NXM_TYPICAL_LEN;
724 case OFPUTIL_P_OF11_STD:
725 return sizeof(struct ofp11_match);
727 case OFPUTIL_P_OF12_OXM:
728 case OFPUTIL_P_OF13_OXM:
729 case OFPUTIL_P_OF14_OXM:
730 case OFPUTIL_P_OF15_OXM:
731 return NXM_TYPICAL_LEN;
738 /* Appends to 'b' an struct ofp11_match_header followed by a match that
739 * expresses 'match' properly for 'protocol', plus enough zero bytes to pad the
740 * data appended out to a multiple of 8. 'protocol' must be one that is usable
741 * in OpenFlow 1.1 or later.
743 * This function can cause 'b''s data to be reallocated.
745 * Returns the number of bytes appended to 'b', excluding the padding. Never
748 ofputil_put_ofp11_match(struct ofpbuf *b, const struct match *match,
749 enum ofputil_protocol protocol)
752 case OFPUTIL_P_OF10_STD:
753 case OFPUTIL_P_OF10_STD_TID:
754 case OFPUTIL_P_OF10_NXM:
755 case OFPUTIL_P_OF10_NXM_TID:
758 case OFPUTIL_P_OF11_STD: {
759 struct ofp11_match *om;
761 /* Make sure that no padding is needed. */
762 BUILD_ASSERT_DECL(sizeof *om % 8 == 0);
764 om = ofpbuf_put_uninit(b, sizeof *om);
765 ofputil_match_to_ofp11_match(match, om);
769 case OFPUTIL_P_OF12_OXM:
770 case OFPUTIL_P_OF13_OXM:
771 case OFPUTIL_P_OF14_OXM:
772 case OFPUTIL_P_OF15_OXM:
773 return oxm_put_match(b, match,
774 ofputil_protocol_to_ofp_version(protocol));
780 /* Given a 'dl_type' value in the format used in struct flow, returns the
781 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
784 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
786 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
787 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
791 /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
792 * structure, returns the corresponding 'dl_type' value for use in struct
795 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
797 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
798 ? htons(FLOW_DL_TYPE_NONE)
804 struct proto_abbrev {
805 enum ofputil_protocol protocol;
809 /* Most users really don't care about some of the differences between
810 * protocols. These abbreviations help with that. */
811 static const struct proto_abbrev proto_abbrevs[] = {
812 { OFPUTIL_P_ANY, "any" },
813 { OFPUTIL_P_OF10_STD_ANY, "OpenFlow10" },
814 { OFPUTIL_P_OF10_NXM_ANY, "NXM" },
815 { OFPUTIL_P_ANY_OXM, "OXM" },
817 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
819 enum ofputil_protocol ofputil_flow_dump_protocols[] = {
828 size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
830 /* Returns the set of ofputil_protocols that are supported with the given
831 * OpenFlow 'version'. 'version' should normally be an 8-bit OpenFlow version
832 * identifier (e.g. 0x01 for OpenFlow 1.0, 0x02 for OpenFlow 1.1). Returns 0
833 * if 'version' is not supported or outside the valid range. */
834 enum ofputil_protocol
835 ofputil_protocols_from_ofp_version(enum ofp_version version)
839 return OFPUTIL_P_OF10_STD_ANY | OFPUTIL_P_OF10_NXM_ANY;
841 return OFPUTIL_P_OF11_STD;
843 return OFPUTIL_P_OF12_OXM;
845 return OFPUTIL_P_OF13_OXM;
847 return OFPUTIL_P_OF14_OXM;
849 return OFPUTIL_P_OF15_OXM;
855 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
856 * connection that has negotiated the given 'version'. 'version' should
857 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
858 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
859 * outside the valid range. */
860 enum ofputil_protocol
861 ofputil_protocol_from_ofp_version(enum ofp_version version)
863 return rightmost_1bit(ofputil_protocols_from_ofp_version(version));
866 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
867 * etc.) that corresponds to 'protocol'. */
869 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
872 case OFPUTIL_P_OF10_STD:
873 case OFPUTIL_P_OF10_STD_TID:
874 case OFPUTIL_P_OF10_NXM:
875 case OFPUTIL_P_OF10_NXM_TID:
876 return OFP10_VERSION;
877 case OFPUTIL_P_OF11_STD:
878 return OFP11_VERSION;
879 case OFPUTIL_P_OF12_OXM:
880 return OFP12_VERSION;
881 case OFPUTIL_P_OF13_OXM:
882 return OFP13_VERSION;
883 case OFPUTIL_P_OF14_OXM:
884 return OFP14_VERSION;
885 case OFPUTIL_P_OF15_OXM:
886 return OFP15_VERSION;
892 /* Returns a bitmap of OpenFlow versions that are supported by at
893 * least one of the 'protocols'. */
895 ofputil_protocols_to_version_bitmap(enum ofputil_protocol protocols)
899 for (; protocols; protocols = zero_rightmost_1bit(protocols)) {
900 enum ofputil_protocol protocol = rightmost_1bit(protocols);
902 bitmap |= 1u << ofputil_protocol_to_ofp_version(protocol);
908 /* Returns the set of protocols that are supported on top of the
909 * OpenFlow versions included in 'bitmap'. */
910 enum ofputil_protocol
911 ofputil_protocols_from_version_bitmap(uint32_t bitmap)
913 enum ofputil_protocol protocols = 0;
915 for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
916 enum ofp_version version = rightmost_1bit_idx(bitmap);
918 protocols |= ofputil_protocols_from_ofp_version(version);
924 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
927 ofputil_protocol_is_valid(enum ofputil_protocol protocol)
929 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
932 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
933 * extension turned on or off if 'enable' is true or false, respectively.
935 * This extension is only useful for protocols whose "standard" version does
936 * not allow specific tables to be modified. In particular, this is true of
937 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
938 * specifies a table ID and so there is no need for such an extension. When
939 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
940 * extension, this function just returns its 'protocol' argument unchanged
941 * regardless of the value of 'enable'. */
942 enum ofputil_protocol
943 ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
946 case OFPUTIL_P_OF10_STD:
947 case OFPUTIL_P_OF10_STD_TID:
948 return enable ? OFPUTIL_P_OF10_STD_TID : OFPUTIL_P_OF10_STD;
950 case OFPUTIL_P_OF10_NXM:
951 case OFPUTIL_P_OF10_NXM_TID:
952 return enable ? OFPUTIL_P_OF10_NXM_TID : OFPUTIL_P_OF10_NXM;
954 case OFPUTIL_P_OF11_STD:
955 return OFPUTIL_P_OF11_STD;
957 case OFPUTIL_P_OF12_OXM:
958 return OFPUTIL_P_OF12_OXM;
960 case OFPUTIL_P_OF13_OXM:
961 return OFPUTIL_P_OF13_OXM;
963 case OFPUTIL_P_OF14_OXM:
964 return OFPUTIL_P_OF14_OXM;
966 case OFPUTIL_P_OF15_OXM:
967 return OFPUTIL_P_OF15_OXM;
974 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
975 * some extension to a standard protocol version, the return value is the
976 * standard version of that protocol without any extension. If 'protocol' is a
977 * standard protocol version, returns 'protocol' unchanged. */
978 enum ofputil_protocol
979 ofputil_protocol_to_base(enum ofputil_protocol protocol)
981 return ofputil_protocol_set_tid(protocol, false);
984 /* Returns 'new_base' with any extensions taken from 'cur'. */
985 enum ofputil_protocol
986 ofputil_protocol_set_base(enum ofputil_protocol cur,
987 enum ofputil_protocol new_base)
989 bool tid = (cur & OFPUTIL_P_TID) != 0;
992 case OFPUTIL_P_OF10_STD:
993 case OFPUTIL_P_OF10_STD_TID:
994 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD, tid);
996 case OFPUTIL_P_OF10_NXM:
997 case OFPUTIL_P_OF10_NXM_TID:
998 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM, tid);
1000 case OFPUTIL_P_OF11_STD:
1001 return ofputil_protocol_set_tid(OFPUTIL_P_OF11_STD, tid);
1003 case OFPUTIL_P_OF12_OXM:
1004 return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM, tid);
1006 case OFPUTIL_P_OF13_OXM:
1007 return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM, tid);
1009 case OFPUTIL_P_OF14_OXM:
1010 return ofputil_protocol_set_tid(OFPUTIL_P_OF14_OXM, tid);
1012 case OFPUTIL_P_OF15_OXM:
1013 return ofputil_protocol_set_tid(OFPUTIL_P_OF15_OXM, tid);
1020 /* Returns a string form of 'protocol', if a simple form exists (that is, if
1021 * 'protocol' is either a single protocol or it is a combination of protocols
1022 * that have a single abbreviation). Otherwise, returns NULL. */
1024 ofputil_protocol_to_string(enum ofputil_protocol protocol)
1026 const struct proto_abbrev *p;
1028 /* Use a "switch" statement for single-bit names so that we get a compiler
1029 * warning if we forget any. */
1031 case OFPUTIL_P_OF10_NXM:
1032 return "NXM-table_id";
1034 case OFPUTIL_P_OF10_NXM_TID:
1035 return "NXM+table_id";
1037 case OFPUTIL_P_OF10_STD:
1038 return "OpenFlow10-table_id";
1040 case OFPUTIL_P_OF10_STD_TID:
1041 return "OpenFlow10+table_id";
1043 case OFPUTIL_P_OF11_STD:
1044 return "OpenFlow11";
1046 case OFPUTIL_P_OF12_OXM:
1047 return "OXM-OpenFlow12";
1049 case OFPUTIL_P_OF13_OXM:
1050 return "OXM-OpenFlow13";
1052 case OFPUTIL_P_OF14_OXM:
1053 return "OXM-OpenFlow14";
1055 case OFPUTIL_P_OF15_OXM:
1056 return "OXM-OpenFlow15";
1059 /* Check abbreviations. */
1060 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1061 if (protocol == p->protocol) {
1069 /* Returns a string that represents 'protocols'. The return value might be a
1070 * comma-separated list if 'protocols' doesn't have a simple name. The return
1071 * value is "none" if 'protocols' is 0.
1073 * The caller must free the returned string (with free()). */
1075 ofputil_protocols_to_string(enum ofputil_protocol protocols)
1079 ovs_assert(!(protocols & ~OFPUTIL_P_ANY));
1080 if (protocols == 0) {
1081 return xstrdup("none");
1086 const struct proto_abbrev *p;
1090 ds_put_char(&s, ',');
1093 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1094 if ((protocols & p->protocol) == p->protocol) {
1095 ds_put_cstr(&s, p->name);
1096 protocols &= ~p->protocol;
1101 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
1102 enum ofputil_protocol bit = 1u << i;
1104 if (protocols & bit) {
1105 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
1114 return ds_steal_cstr(&s);
1117 static enum ofputil_protocol
1118 ofputil_protocol_from_string__(const char *s, size_t n)
1120 const struct proto_abbrev *p;
1123 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
1124 enum ofputil_protocol bit = 1u << i;
1125 const char *name = ofputil_protocol_to_string(bit);
1127 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
1132 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1133 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
1141 /* Returns the nonempty set of protocols represented by 's', which can be a
1142 * single protocol name or abbreviation or a comma-separated list of them.
1144 * Aborts the program with an error message if 's' is invalid. */
1145 enum ofputil_protocol
1146 ofputil_protocols_from_string(const char *s)
1148 const char *orig_s = s;
1149 enum ofputil_protocol protocols;
1153 enum ofputil_protocol p;
1156 n = strcspn(s, ",");
1162 p = ofputil_protocol_from_string__(s, n);
1164 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
1172 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
1178 ofputil_version_from_string(const char *s)
1180 if (!strcasecmp(s, "OpenFlow10")) {
1181 return OFP10_VERSION;
1183 if (!strcasecmp(s, "OpenFlow11")) {
1184 return OFP11_VERSION;
1186 if (!strcasecmp(s, "OpenFlow12")) {
1187 return OFP12_VERSION;
1189 if (!strcasecmp(s, "OpenFlow13")) {
1190 return OFP13_VERSION;
1192 if (!strcasecmp(s, "OpenFlow14")) {
1193 return OFP14_VERSION;
1195 if (!strcasecmp(s, "OpenFlow15")) {
1196 return OFP15_VERSION;
1202 is_delimiter(unsigned char c)
1204 return isspace(c) || c == ',';
1208 ofputil_versions_from_string(const char *s)
1211 uint32_t bitmap = 0;
1218 if (is_delimiter(s[i])) {
1223 while (s[i + j] && !is_delimiter(s[i + j])) {
1226 key = xmemdup0(s + i, j);
1227 version = ofputil_version_from_string(key);
1229 VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key);
1232 bitmap |= 1u << version;
1240 ofputil_versions_from_strings(char ** const s, size_t count)
1242 uint32_t bitmap = 0;
1245 int version = ofputil_version_from_string(s[count]);
1247 VLOG_WARN("Unknown OpenFlow version: \"%s\"", s[count]);
1249 bitmap |= 1u << version;
1257 ofputil_version_to_string(enum ofp_version ofp_version)
1259 switch (ofp_version) {
1261 return "OpenFlow10";
1263 return "OpenFlow11";
1265 return "OpenFlow12";
1267 return "OpenFlow13";
1269 return "OpenFlow14";
1271 return "OpenFlow15";
1278 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
1280 switch (packet_in_format) {
1281 case NXPIF_OPENFLOW10:
1290 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
1292 switch (packet_in_format) {
1293 case NXPIF_OPENFLOW10:
1294 return "openflow10";
1303 ofputil_packet_in_format_from_string(const char *s)
1305 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
1306 : !strcmp(s, "nxm") ? NXPIF_NXM
1311 ofputil_format_version(struct ds *msg, enum ofp_version version)
1313 ds_put_format(msg, "0x%02x", version);
1317 ofputil_format_version_name(struct ds *msg, enum ofp_version version)
1319 ds_put_cstr(msg, ofputil_version_to_string(version));
1323 ofputil_format_version_bitmap__(struct ds *msg, uint32_t bitmap,
1324 void (*format_version)(struct ds *msg,
1328 format_version(msg, raw_ctz(bitmap));
1329 bitmap = zero_rightmost_1bit(bitmap);
1331 ds_put_cstr(msg, ", ");
1337 ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap)
1339 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version);
1343 ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap)
1345 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version_name);
1349 ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header *oheh,
1350 uint32_t *allowed_versionsp)
1352 uint16_t bitmap_len = ntohs(oheh->length) - sizeof *oheh;
1353 const ovs_be32 *bitmap = ALIGNED_CAST(const ovs_be32 *, oheh + 1);
1354 uint32_t allowed_versions;
1356 if (!bitmap_len || bitmap_len % sizeof *bitmap) {
1360 /* Only use the first 32-bit element of the bitmap as that is all the
1361 * current implementation supports. Subsequent elements are ignored which
1362 * should have no effect on session negotiation until Open vSwitch supports
1363 * wire-protocol versions greater than 31.
1365 allowed_versions = ntohl(bitmap[0]);
1367 if (allowed_versions & 1) {
1368 /* There's no OpenFlow version 0. */
1369 VLOG_WARN_RL(&bad_ofmsg_rl, "peer claims to support invalid OpenFlow "
1371 allowed_versions &= ~1u;
1374 if (!allowed_versions) {
1375 VLOG_WARN_RL(&bad_ofmsg_rl, "peer does not support any OpenFlow "
1376 "version (between 0x01 and 0x1f)");
1380 *allowed_versionsp = allowed_versions;
1385 version_bitmap_from_version(uint8_t ofp_version)
1387 return ((ofp_version < 32 ? 1u << ofp_version : 0) - 1) << 1;
1390 /* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions'
1391 * the set of OpenFlow versions for which 'oh' announces support.
1393 * Because of how OpenFlow defines OFPT_HELLO messages, this function is always
1394 * successful, and thus '*allowed_versions' is always initialized. However, it
1395 * returns false if 'oh' contains some data that could not be fully understood,
1396 * true if 'oh' was completely parsed. */
1398 ofputil_decode_hello(const struct ofp_header *oh, uint32_t *allowed_versions)
1403 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
1404 ofpbuf_pull(&msg, sizeof *oh);
1406 *allowed_versions = version_bitmap_from_version(oh->version);
1408 const struct ofp_hello_elem_header *oheh;
1411 if (msg.size < sizeof *oheh) {
1416 len = ntohs(oheh->length);
1417 if (len < sizeof *oheh || !ofpbuf_try_pull(&msg, ROUND_UP(len, 8))) {
1421 if (oheh->type != htons(OFPHET_VERSIONBITMAP)
1422 || !ofputil_decode_hello_bitmap(oheh, allowed_versions)) {
1430 /* Returns true if 'allowed_versions' needs to be accompanied by a version
1431 * bitmap to be correctly expressed in an OFPT_HELLO message. */
1433 should_send_version_bitmap(uint32_t allowed_versions)
1435 return !is_pow2((allowed_versions >> 1) + 1);
1438 /* Create an OFPT_HELLO message that expresses support for the OpenFlow
1439 * versions in the 'allowed_versions' bitmaps and returns the message. */
1441 ofputil_encode_hello(uint32_t allowed_versions)
1443 enum ofp_version ofp_version;
1446 ofp_version = leftmost_1bit_idx(allowed_versions);
1447 msg = ofpraw_alloc(OFPRAW_OFPT_HELLO, ofp_version, 0);
1449 if (should_send_version_bitmap(allowed_versions)) {
1450 struct ofp_hello_elem_header *oheh;
1453 map_len = sizeof allowed_versions;
1454 oheh = ofpbuf_put_zeros(msg, ROUND_UP(map_len + sizeof *oheh, 8));
1455 oheh->type = htons(OFPHET_VERSIONBITMAP);
1456 oheh->length = htons(map_len + sizeof *oheh);
1457 *ALIGNED_CAST(ovs_be32 *, oheh + 1) = htonl(allowed_versions);
1459 ofpmsg_update_length(msg);
1465 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1466 * protocol is 'current', at least partly transitions the protocol to 'want'.
1467 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1468 * connection if the switch processes the returned message correctly. (If
1469 * '*next != want' then the caller will have to iterate.)
1471 * If 'current == want', or if it is not possible to transition from 'current'
1472 * to 'want' (because, for example, 'current' and 'want' use different OpenFlow
1473 * protocol versions), returns NULL and stores 'current' in '*next'. */
1475 ofputil_encode_set_protocol(enum ofputil_protocol current,
1476 enum ofputil_protocol want,
1477 enum ofputil_protocol *next)
1479 enum ofp_version cur_version, want_version;
1480 enum ofputil_protocol cur_base, want_base;
1481 bool cur_tid, want_tid;
1483 cur_version = ofputil_protocol_to_ofp_version(current);
1484 want_version = ofputil_protocol_to_ofp_version(want);
1485 if (cur_version != want_version) {
1490 cur_base = ofputil_protocol_to_base(current);
1491 want_base = ofputil_protocol_to_base(want);
1492 if (cur_base != want_base) {
1493 *next = ofputil_protocol_set_base(current, want_base);
1495 switch (want_base) {
1496 case OFPUTIL_P_OF10_NXM:
1497 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1499 case OFPUTIL_P_OF10_STD:
1500 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1502 case OFPUTIL_P_OF11_STD:
1503 case OFPUTIL_P_OF12_OXM:
1504 case OFPUTIL_P_OF13_OXM:
1505 case OFPUTIL_P_OF14_OXM:
1506 case OFPUTIL_P_OF15_OXM:
1507 /* There is only one variant of each OpenFlow 1.1+ protocol, and we
1508 * verified above that we're not trying to change versions. */
1511 case OFPUTIL_P_OF10_STD_TID:
1512 case OFPUTIL_P_OF10_NXM_TID:
1517 cur_tid = (current & OFPUTIL_P_TID) != 0;
1518 want_tid = (want & OFPUTIL_P_TID) != 0;
1519 if (cur_tid != want_tid) {
1520 *next = ofputil_protocol_set_tid(current, want_tid);
1521 return ofputil_make_flow_mod_table_id(want_tid);
1524 ovs_assert(current == want);
1530 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1531 * format to 'nxff'. */
1533 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1535 struct nx_set_flow_format *sff;
1538 ovs_assert(ofputil_nx_flow_format_is_valid(nxff));
1540 msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1541 sff = ofpbuf_put_zeros(msg, sizeof *sff);
1542 sff->format = htonl(nxff);
1547 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1549 enum ofputil_protocol
1550 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1552 switch (flow_format) {
1553 case NXFF_OPENFLOW10:
1554 return OFPUTIL_P_OF10_STD;
1557 return OFPUTIL_P_OF10_NXM;
1564 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1566 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1568 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1571 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1574 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1576 switch (flow_format) {
1577 case NXFF_OPENFLOW10:
1578 return "openflow10";
1587 ofputil_make_set_packet_in_format(enum ofp_version ofp_version,
1588 enum nx_packet_in_format packet_in_format)
1590 struct nx_set_packet_in_format *spif;
1593 msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, ofp_version, 0);
1594 spif = ofpbuf_put_zeros(msg, sizeof *spif);
1595 spif->format = htonl(packet_in_format);
1600 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1601 * extension on or off (according to 'flow_mod_table_id'). */
1603 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1605 struct nx_flow_mod_table_id *nfmti;
1608 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1609 nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
1610 nfmti->set = flow_mod_table_id;
1614 struct ofputil_flow_mod_flag {
1616 enum ofp_version min_version, max_version;
1617 enum ofputil_flow_mod_flags flag;
1620 static const struct ofputil_flow_mod_flag ofputil_flow_mod_flags[] = {
1621 { OFPFF_SEND_FLOW_REM, OFP10_VERSION, 0, OFPUTIL_FF_SEND_FLOW_REM },
1622 { OFPFF_CHECK_OVERLAP, OFP10_VERSION, 0, OFPUTIL_FF_CHECK_OVERLAP },
1623 { OFPFF10_EMERG, OFP10_VERSION, OFP10_VERSION,
1625 { OFPFF12_RESET_COUNTS, OFP12_VERSION, 0, OFPUTIL_FF_RESET_COUNTS },
1626 { OFPFF13_NO_PKT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_PKT_COUNTS },
1627 { OFPFF13_NO_BYT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_BYT_COUNTS },
1632 ofputil_decode_flow_mod_flags(ovs_be16 raw_flags_,
1633 enum ofp_flow_mod_command command,
1634 enum ofp_version version,
1635 enum ofputil_flow_mod_flags *flagsp)
1637 uint16_t raw_flags = ntohs(raw_flags_);
1638 const struct ofputil_flow_mod_flag *f;
1641 for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1642 if (raw_flags & f->raw_flag
1643 && version >= f->min_version
1644 && (!f->max_version || version <= f->max_version)) {
1645 raw_flags &= ~f->raw_flag;
1650 /* In OF1.0 and OF1.1, "add" always resets counters, and other commands
1653 * In OF1.2 and later, OFPFF12_RESET_COUNTS controls whether each command
1654 * resets counters. */
1655 if ((version == OFP10_VERSION || version == OFP11_VERSION)
1656 && command == OFPFC_ADD) {
1657 *flagsp |= OFPUTIL_FF_RESET_COUNTS;
1660 return raw_flags ? OFPERR_OFPFMFC_BAD_FLAGS : 0;
1664 ofputil_encode_flow_mod_flags(enum ofputil_flow_mod_flags flags,
1665 enum ofp_version version)
1667 const struct ofputil_flow_mod_flag *f;
1671 for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1673 && version >= f->min_version
1674 && (!f->max_version || version <= f->max_version)) {
1675 raw_flags |= f->raw_flag;
1679 return htons(raw_flags);
1682 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1683 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1686 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1687 * The caller must initialize 'ofpacts' and retains ownership of it.
1688 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1690 * Does not validate the flow_mod actions. The caller should do that, with
1691 * ofpacts_check(). */
1693 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1694 const struct ofp_header *oh,
1695 enum ofputil_protocol protocol,
1696 struct ofpbuf *ofpacts,
1697 ofp_port_t max_port, uint8_t max_table)
1704 /* Ignored for non-delete actions */
1705 fm->delete_reason = OFPRR_DELETE;
1707 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1708 raw = ofpraw_pull_assert(&b);
1709 if (raw == OFPRAW_OFPT11_FLOW_MOD) {
1710 /* Standard OpenFlow 1.1+ flow_mod. */
1711 const struct ofp11_flow_mod *ofm;
1713 ofm = ofpbuf_pull(&b, sizeof *ofm);
1715 error = ofputil_pull_ofp11_match(&b, &fm->match, NULL);
1720 /* Translate the message. */
1721 fm->priority = ntohs(ofm->priority);
1722 if (ofm->command == OFPFC_ADD
1723 || (oh->version == OFP11_VERSION
1724 && (ofm->command == OFPFC_MODIFY ||
1725 ofm->command == OFPFC_MODIFY_STRICT)
1726 && ofm->cookie_mask == htonll(0))) {
1727 /* In OpenFlow 1.1 only, a "modify" or "modify-strict" that does
1728 * not match on the cookie is treated as an "add" if there is no
1730 fm->cookie = htonll(0);
1731 fm->cookie_mask = htonll(0);
1732 fm->new_cookie = ofm->cookie;
1734 fm->cookie = ofm->cookie;
1735 fm->cookie_mask = ofm->cookie_mask;
1736 fm->new_cookie = OVS_BE64_MAX;
1738 fm->modify_cookie = false;
1739 fm->command = ofm->command;
1743 * OF1.1 entirely forbids table_id == OFPTT_ALL.
1744 * OF1.2+ allows table_id == OFPTT_ALL only for deletes. */
1745 fm->table_id = ofm->table_id;
1746 if (fm->table_id == OFPTT_ALL
1747 && (oh->version == OFP11_VERSION
1748 || (ofm->command != OFPFC_DELETE &&
1749 ofm->command != OFPFC_DELETE_STRICT))) {
1750 return OFPERR_OFPFMFC_BAD_TABLE_ID;
1753 fm->idle_timeout = ntohs(ofm->idle_timeout);
1754 fm->hard_timeout = ntohs(ofm->hard_timeout);
1755 if (oh->version >= OFP14_VERSION && ofm->command == OFPFC_ADD) {
1756 fm->importance = ntohs(ofm->importance);
1760 fm->buffer_id = ntohl(ofm->buffer_id);
1761 error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
1766 fm->out_group = (ofm->command == OFPFC_DELETE ||
1767 ofm->command == OFPFC_DELETE_STRICT
1768 ? ntohl(ofm->out_group)
1770 raw_flags = ofm->flags;
1774 if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1775 /* Standard OpenFlow 1.0 flow_mod. */
1776 const struct ofp10_flow_mod *ofm;
1778 /* Get the ofp10_flow_mod. */
1779 ofm = ofpbuf_pull(&b, sizeof *ofm);
1781 /* Translate the rule. */
1782 ofputil_match_from_ofp10_match(&ofm->match, &fm->match);
1783 ofputil_normalize_match(&fm->match);
1785 /* OpenFlow 1.0 says that exact-match rules have to have the
1786 * highest possible priority. */
1787 fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL)
1788 ? ntohs(ofm->priority)
1791 /* Translate the message. */
1792 command = ntohs(ofm->command);
1793 fm->cookie = htonll(0);
1794 fm->cookie_mask = htonll(0);
1795 fm->new_cookie = ofm->cookie;
1796 fm->idle_timeout = ntohs(ofm->idle_timeout);
1797 fm->hard_timeout = ntohs(ofm->hard_timeout);
1799 fm->buffer_id = ntohl(ofm->buffer_id);
1800 fm->out_port = u16_to_ofp(ntohs(ofm->out_port));
1801 fm->out_group = OFPG_ANY;
1802 raw_flags = ofm->flags;
1803 } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1804 /* Nicira extended flow_mod. */
1805 const struct nx_flow_mod *nfm;
1807 /* Dissect the message. */
1808 nfm = ofpbuf_pull(&b, sizeof *nfm);
1809 error = nx_pull_match(&b, ntohs(nfm->match_len),
1810 &fm->match, &fm->cookie, &fm->cookie_mask);
1815 /* Translate the message. */
1816 command = ntohs(nfm->command);
1817 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1818 /* Flow additions may only set a new cookie, not match an
1819 * existing cookie. */
1820 return OFPERR_NXBRC_NXM_INVALID;
1822 fm->priority = ntohs(nfm->priority);
1823 fm->new_cookie = nfm->cookie;
1824 fm->idle_timeout = ntohs(nfm->idle_timeout);
1825 fm->hard_timeout = ntohs(nfm->hard_timeout);
1827 fm->buffer_id = ntohl(nfm->buffer_id);
1828 fm->out_port = u16_to_ofp(ntohs(nfm->out_port));
1829 fm->out_group = OFPG_ANY;
1830 raw_flags = nfm->flags;
1835 fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX;
1836 if (protocol & OFPUTIL_P_TID) {
1837 fm->command = command & 0xff;
1838 fm->table_id = command >> 8;
1840 if (command > 0xff) {
1841 VLOG_WARN_RL(&bad_ofmsg_rl, "flow_mod has explicit table_id "
1842 "but flow_mod_table_id extension is not enabled");
1844 fm->command = command;
1845 fm->table_id = 0xff;
1849 if (fm->command > OFPFC_DELETE_STRICT) {
1850 return OFPERR_OFPFMFC_BAD_COMMAND;
1853 error = ofpacts_pull_openflow_instructions(&b, b.size,
1854 oh->version, ofpacts);
1858 fm->ofpacts = ofpacts->data;
1859 fm->ofpacts_len = ofpacts->size;
1861 error = ofputil_decode_flow_mod_flags(raw_flags, fm->command,
1862 oh->version, &fm->flags);
1867 if (fm->flags & OFPUTIL_FF_EMERG) {
1868 /* We do not support the OpenFlow 1.0 emergency flow cache, which
1869 * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
1871 * OpenFlow 1.0 specifies the error code to use when idle_timeout
1872 * or hard_timeout is nonzero. Otherwise, there is no good error
1873 * code, so just state that the flow table is full. */
1874 return (fm->hard_timeout || fm->idle_timeout
1875 ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT
1876 : OFPERR_OFPFMFC_TABLE_FULL);
1879 return ofpacts_check_consistency(fm->ofpacts, fm->ofpacts_len,
1880 &fm->match.flow, max_port,
1881 fm->table_id, max_table, protocol);
1885 ofputil_pull_bands(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
1886 struct ofpbuf *bands)
1888 const struct ofp13_meter_band_header *ombh;
1889 struct ofputil_meter_band *mb;
1892 ombh = ofpbuf_try_pull(msg, len);
1894 return OFPERR_OFPBRC_BAD_LEN;
1897 while (len >= sizeof (struct ofp13_meter_band_drop)) {
1898 size_t ombh_len = ntohs(ombh->len);
1899 /* All supported band types have the same length. */
1900 if (ombh_len != sizeof (struct ofp13_meter_band_drop)) {
1901 return OFPERR_OFPBRC_BAD_LEN;
1903 mb = ofpbuf_put_uninit(bands, sizeof *mb);
1904 mb->type = ntohs(ombh->type);
1905 if (mb->type != OFPMBT13_DROP && mb->type != OFPMBT13_DSCP_REMARK) {
1906 return OFPERR_OFPMMFC_BAD_BAND;
1908 mb->rate = ntohl(ombh->rate);
1909 mb->burst_size = ntohl(ombh->burst_size);
1910 mb->prec_level = (mb->type == OFPMBT13_DSCP_REMARK) ?
1911 ((struct ofp13_meter_band_dscp_remark *)ombh)->prec_level : 0;
1914 ombh = ALIGNED_CAST(struct ofp13_meter_band_header *,
1915 (char *) ombh + ombh_len);
1918 return OFPERR_OFPBRC_BAD_LEN;
1925 ofputil_decode_meter_mod(const struct ofp_header *oh,
1926 struct ofputil_meter_mod *mm,
1927 struct ofpbuf *bands)
1929 const struct ofp13_meter_mod *omm;
1932 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1933 ofpraw_pull_assert(&b);
1934 omm = ofpbuf_pull(&b, sizeof *omm);
1936 /* Translate the message. */
1937 mm->command = ntohs(omm->command);
1938 if (mm->command != OFPMC13_ADD &&
1939 mm->command != OFPMC13_MODIFY &&
1940 mm->command != OFPMC13_DELETE) {
1941 return OFPERR_OFPMMFC_BAD_COMMAND;
1943 mm->meter.meter_id = ntohl(omm->meter_id);
1945 if (mm->command == OFPMC13_DELETE) {
1946 mm->meter.flags = 0;
1947 mm->meter.n_bands = 0;
1948 mm->meter.bands = NULL;
1952 mm->meter.flags = ntohs(omm->flags);
1953 if (mm->meter.flags & OFPMF13_KBPS &&
1954 mm->meter.flags & OFPMF13_PKTPS) {
1955 return OFPERR_OFPMMFC_BAD_FLAGS;
1957 mm->meter.bands = bands->data;
1959 error = ofputil_pull_bands(&b, b.size, &mm->meter.n_bands, bands);
1968 ofputil_decode_meter_request(const struct ofp_header *oh, uint32_t *meter_id)
1970 const struct ofp13_meter_multipart_request *omr = ofpmsg_body(oh);
1971 *meter_id = ntohl(omr->meter_id);
1975 ofputil_encode_meter_request(enum ofp_version ofp_version,
1976 enum ofputil_meter_request_type type,
1984 case OFPUTIL_METER_CONFIG:
1985 raw = OFPRAW_OFPST13_METER_CONFIG_REQUEST;
1987 case OFPUTIL_METER_STATS:
1988 raw = OFPRAW_OFPST13_METER_REQUEST;
1991 case OFPUTIL_METER_FEATURES:
1992 raw = OFPRAW_OFPST13_METER_FEATURES_REQUEST;
1996 msg = ofpraw_alloc(raw, ofp_version, 0);
1998 if (type != OFPUTIL_METER_FEATURES) {
1999 struct ofp13_meter_multipart_request *omr;
2000 omr = ofpbuf_put_zeros(msg, sizeof *omr);
2001 omr->meter_id = htonl(meter_id);
2007 ofputil_put_bands(uint16_t n_bands, const struct ofputil_meter_band *mb,
2012 for (n = 0; n < n_bands; ++n) {
2013 /* Currently all band types have same size. */
2014 struct ofp13_meter_band_dscp_remark *ombh;
2015 size_t ombh_len = sizeof *ombh;
2017 ombh = ofpbuf_put_zeros(msg, ombh_len);
2019 ombh->type = htons(mb->type);
2020 ombh->len = htons(ombh_len);
2021 ombh->rate = htonl(mb->rate);
2022 ombh->burst_size = htonl(mb->burst_size);
2023 ombh->prec_level = mb->prec_level;
2029 /* Encode a meter stat for 'mc' and append it to 'replies'. */
2031 ofputil_append_meter_config(struct ovs_list *replies,
2032 const struct ofputil_meter_config *mc)
2034 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
2035 size_t start_ofs = msg->size;
2036 struct ofp13_meter_config *reply;
2038 ofpbuf_put_uninit(msg, sizeof *reply);
2039 ofputil_put_bands(mc->n_bands, mc->bands, msg);
2041 reply = ofpbuf_at_assert(msg, start_ofs, sizeof *reply);
2042 reply->flags = htons(mc->flags);
2043 reply->meter_id = htonl(mc->meter_id);
2044 reply->length = htons(msg->size - start_ofs);
2046 ofpmp_postappend(replies, start_ofs);
2049 /* Encode a meter stat for 'ms' and append it to 'replies'. */
2051 ofputil_append_meter_stats(struct ovs_list *replies,
2052 const struct ofputil_meter_stats *ms)
2054 struct ofp13_meter_stats *reply;
2058 len = sizeof *reply + ms->n_bands * sizeof(struct ofp13_meter_band_stats);
2059 reply = ofpmp_append(replies, len);
2061 reply->meter_id = htonl(ms->meter_id);
2062 reply->len = htons(len);
2063 memset(reply->pad, 0, sizeof reply->pad);
2064 reply->flow_count = htonl(ms->flow_count);
2065 reply->packet_in_count = htonll(ms->packet_in_count);
2066 reply->byte_in_count = htonll(ms->byte_in_count);
2067 reply->duration_sec = htonl(ms->duration_sec);
2068 reply->duration_nsec = htonl(ms->duration_nsec);
2070 for (n = 0; n < ms->n_bands; ++n) {
2071 const struct ofputil_meter_band_stats *src = &ms->bands[n];
2072 struct ofp13_meter_band_stats *dst = &reply->band_stats[n];
2074 dst->packet_band_count = htonll(src->packet_count);
2075 dst->byte_band_count = htonll(src->byte_count);
2079 /* Converts an OFPMP_METER_CONFIG reply in 'msg' into an abstract
2080 * ofputil_meter_config in 'mc', with mc->bands pointing to bands decoded into
2081 * 'bands'. The caller must have initialized 'bands' and retains ownership of
2082 * it across the call.
2084 * Multiple OFPST13_METER_CONFIG replies can be packed into a single OpenFlow
2085 * message. Calling this function multiple times for a single 'msg' iterates
2086 * through the replies. 'bands' is cleared for each reply.
2088 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2089 * otherwise a positive errno value. */
2091 ofputil_decode_meter_config(struct ofpbuf *msg,
2092 struct ofputil_meter_config *mc,
2093 struct ofpbuf *bands)
2095 const struct ofp13_meter_config *omc;
2098 /* Pull OpenFlow headers for the first call. */
2100 ofpraw_pull_assert(msg);
2107 omc = ofpbuf_try_pull(msg, sizeof *omc);
2109 VLOG_WARN_RL(&bad_ofmsg_rl,
2110 "OFPMP_METER_CONFIG reply has %"PRIu32" leftover bytes at end",
2112 return OFPERR_OFPBRC_BAD_LEN;
2115 ofpbuf_clear(bands);
2116 err = ofputil_pull_bands(msg, ntohs(omc->length) - sizeof *omc,
2117 &mc->n_bands, bands);
2121 mc->meter_id = ntohl(omc->meter_id);
2122 mc->flags = ntohs(omc->flags);
2123 mc->bands = bands->data;
2129 ofputil_pull_band_stats(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
2130 struct ofpbuf *bands)
2132 const struct ofp13_meter_band_stats *ombs;
2133 struct ofputil_meter_band_stats *mbs;
2136 ombs = ofpbuf_try_pull(msg, len);
2138 return OFPERR_OFPBRC_BAD_LEN;
2141 n = len / sizeof *ombs;
2142 if (len != n * sizeof *ombs) {
2143 return OFPERR_OFPBRC_BAD_LEN;
2146 mbs = ofpbuf_put_uninit(bands, len);
2148 for (i = 0; i < n; ++i) {
2149 mbs[i].packet_count = ntohll(ombs[i].packet_band_count);
2150 mbs[i].byte_count = ntohll(ombs[i].byte_band_count);
2156 /* Converts an OFPMP_METER reply in 'msg' into an abstract
2157 * ofputil_meter_stats in 'ms', with ms->bands pointing to band stats
2158 * decoded into 'bands'.
2160 * Multiple OFPMP_METER replies can be packed into a single OpenFlow
2161 * message. Calling this function multiple times for a single 'msg' iterates
2162 * through the replies. 'bands' is cleared for each reply.
2164 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2165 * otherwise a positive errno value. */
2167 ofputil_decode_meter_stats(struct ofpbuf *msg,
2168 struct ofputil_meter_stats *ms,
2169 struct ofpbuf *bands)
2171 const struct ofp13_meter_stats *oms;
2174 /* Pull OpenFlow headers for the first call. */
2176 ofpraw_pull_assert(msg);
2183 oms = ofpbuf_try_pull(msg, sizeof *oms);
2185 VLOG_WARN_RL(&bad_ofmsg_rl,
2186 "OFPMP_METER reply has %"PRIu32" leftover bytes at end",
2188 return OFPERR_OFPBRC_BAD_LEN;
2191 ofpbuf_clear(bands);
2192 err = ofputil_pull_band_stats(msg, ntohs(oms->len) - sizeof *oms,
2193 &ms->n_bands, bands);
2197 ms->meter_id = ntohl(oms->meter_id);
2198 ms->flow_count = ntohl(oms->flow_count);
2199 ms->packet_in_count = ntohll(oms->packet_in_count);
2200 ms->byte_in_count = ntohll(oms->byte_in_count);
2201 ms->duration_sec = ntohl(oms->duration_sec);
2202 ms->duration_nsec = ntohl(oms->duration_nsec);
2203 ms->bands = bands->data;
2209 ofputil_decode_meter_features(const struct ofp_header *oh,
2210 struct ofputil_meter_features *mf)
2212 const struct ofp13_meter_features *omf = ofpmsg_body(oh);
2214 mf->max_meters = ntohl(omf->max_meter);
2215 mf->band_types = ntohl(omf->band_types);
2216 mf->capabilities = ntohl(omf->capabilities);
2217 mf->max_bands = omf->max_bands;
2218 mf->max_color = omf->max_color;
2222 ofputil_encode_meter_features_reply(const struct ofputil_meter_features *mf,
2223 const struct ofp_header *request)
2225 struct ofpbuf *reply;
2226 struct ofp13_meter_features *omf;
2228 reply = ofpraw_alloc_stats_reply(request, 0);
2229 omf = ofpbuf_put_zeros(reply, sizeof *omf);
2231 omf->max_meter = htonl(mf->max_meters);
2232 omf->band_types = htonl(mf->band_types);
2233 omf->capabilities = htonl(mf->capabilities);
2234 omf->max_bands = mf->max_bands;
2235 omf->max_color = mf->max_color;
2241 ofputil_encode_meter_mod(enum ofp_version ofp_version,
2242 const struct ofputil_meter_mod *mm)
2246 struct ofp13_meter_mod *omm;
2248 msg = ofpraw_alloc(OFPRAW_OFPT13_METER_MOD, ofp_version,
2249 NXM_TYPICAL_LEN + mm->meter.n_bands * 16);
2250 omm = ofpbuf_put_zeros(msg, sizeof *omm);
2251 omm->command = htons(mm->command);
2252 if (mm->command != OFPMC13_DELETE) {
2253 omm->flags = htons(mm->meter.flags);
2255 omm->meter_id = htonl(mm->meter.meter_id);
2257 ofputil_put_bands(mm->meter.n_bands, mm->meter.bands, msg);
2259 ofpmsg_update_length(msg);
2264 ofputil_tid_command(const struct ofputil_flow_mod *fm,
2265 enum ofputil_protocol protocol)
2267 return htons(protocol & OFPUTIL_P_TID
2268 ? (fm->command & 0xff) | (fm->table_id << 8)
2272 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
2273 * 'protocol' and returns the message. */
2275 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
2276 enum ofputil_protocol protocol)
2278 enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
2279 ovs_be16 raw_flags = ofputil_encode_flow_mod_flags(fm->flags, version);
2283 case OFPUTIL_P_OF11_STD:
2284 case OFPUTIL_P_OF12_OXM:
2285 case OFPUTIL_P_OF13_OXM:
2286 case OFPUTIL_P_OF14_OXM:
2287 case OFPUTIL_P_OF15_OXM: {
2288 struct ofp11_flow_mod *ofm;
2291 tailroom = ofputil_match_typical_len(protocol) + fm->ofpacts_len;
2292 msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, version, tailroom);
2293 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
2294 if ((protocol == OFPUTIL_P_OF11_STD
2295 && (fm->command == OFPFC_MODIFY ||
2296 fm->command == OFPFC_MODIFY_STRICT)
2297 && fm->cookie_mask == htonll(0))
2298 || fm->command == OFPFC_ADD) {
2299 ofm->cookie = fm->new_cookie;
2301 ofm->cookie = fm->cookie & fm->cookie_mask;
2303 ofm->cookie_mask = fm->cookie_mask;
2304 if (fm->table_id != OFPTT_ALL
2305 || (protocol != OFPUTIL_P_OF11_STD
2306 && (fm->command == OFPFC_DELETE ||
2307 fm->command == OFPFC_DELETE_STRICT))) {
2308 ofm->table_id = fm->table_id;
2312 ofm->command = fm->command;
2313 ofm->idle_timeout = htons(fm->idle_timeout);
2314 ofm->hard_timeout = htons(fm->hard_timeout);
2315 ofm->priority = htons(fm->priority);
2316 ofm->buffer_id = htonl(fm->buffer_id);
2317 ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
2318 ofm->out_group = htonl(fm->out_group);
2319 ofm->flags = raw_flags;
2320 if (version >= OFP14_VERSION && fm->command == OFPFC_ADD) {
2321 ofm->importance = htons(fm->importance);
2323 ofm->importance = 0;
2325 ofputil_put_ofp11_match(msg, &fm->match, protocol);
2326 ofpacts_put_openflow_instructions(fm->ofpacts, fm->ofpacts_len, msg,
2331 case OFPUTIL_P_OF10_STD:
2332 case OFPUTIL_P_OF10_STD_TID: {
2333 struct ofp10_flow_mod *ofm;
2335 msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
2337 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
2338 ofputil_match_to_ofp10_match(&fm->match, &ofm->match);
2339 ofm->cookie = fm->new_cookie;
2340 ofm->command = ofputil_tid_command(fm, protocol);
2341 ofm->idle_timeout = htons(fm->idle_timeout);
2342 ofm->hard_timeout = htons(fm->hard_timeout);
2343 ofm->priority = htons(fm->priority);
2344 ofm->buffer_id = htonl(fm->buffer_id);
2345 ofm->out_port = htons(ofp_to_u16(fm->out_port));
2346 ofm->flags = raw_flags;
2347 ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2352 case OFPUTIL_P_OF10_NXM:
2353 case OFPUTIL_P_OF10_NXM_TID: {
2354 struct nx_flow_mod *nfm;
2357 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
2358 NXM_TYPICAL_LEN + fm->ofpacts_len);
2359 nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
2360 nfm->command = ofputil_tid_command(fm, protocol);
2361 nfm->cookie = fm->new_cookie;
2362 match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask);
2364 nfm->idle_timeout = htons(fm->idle_timeout);
2365 nfm->hard_timeout = htons(fm->hard_timeout);
2366 nfm->priority = htons(fm->priority);
2367 nfm->buffer_id = htonl(fm->buffer_id);
2368 nfm->out_port = htons(ofp_to_u16(fm->out_port));
2369 nfm->flags = raw_flags;
2370 nfm->match_len = htons(match_len);
2371 ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2380 ofpmsg_update_length(msg);
2385 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr,
2386 const struct ofp10_flow_stats_request *ofsr,
2389 fsr->aggregate = aggregate;
2390 ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match);
2391 fsr->out_port = u16_to_ofp(ntohs(ofsr->out_port));
2392 fsr->out_group = OFPG_ANY;
2393 fsr->table_id = ofsr->table_id;
2394 fsr->cookie = fsr->cookie_mask = htonll(0);
2400 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr,
2401 struct ofpbuf *b, bool aggregate)
2403 const struct ofp11_flow_stats_request *ofsr;
2406 ofsr = ofpbuf_pull(b, sizeof *ofsr);
2407 fsr->aggregate = aggregate;
2408 fsr->table_id = ofsr->table_id;
2409 error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port);
2413 fsr->out_group = ntohl(ofsr->out_group);
2414 fsr->cookie = ofsr->cookie;
2415 fsr->cookie_mask = ofsr->cookie_mask;
2416 error = ofputil_pull_ofp11_match(b, &fsr->match, NULL);
2425 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
2426 struct ofpbuf *b, bool aggregate)
2428 const struct nx_flow_stats_request *nfsr;
2431 nfsr = ofpbuf_pull(b, sizeof *nfsr);
2432 error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match,
2433 &fsr->cookie, &fsr->cookie_mask);
2438 return OFPERR_OFPBRC_BAD_LEN;
2441 fsr->aggregate = aggregate;
2442 fsr->out_port = u16_to_ofp(ntohs(nfsr->out_port));
2443 fsr->out_group = OFPG_ANY;
2444 fsr->table_id = nfsr->table_id;
2449 /* Constructs and returns an OFPT_QUEUE_GET_CONFIG request for the specified
2450 * 'port', suitable for OpenFlow version 'version'. */
2452 ofputil_encode_queue_get_config_request(enum ofp_version version,
2455 struct ofpbuf *request;
2457 if (version == OFP10_VERSION) {
2458 struct ofp10_queue_get_config_request *qgcr10;
2460 request = ofpraw_alloc(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST,
2462 qgcr10 = ofpbuf_put_zeros(request, sizeof *qgcr10);
2463 qgcr10->port = htons(ofp_to_u16(port));
2465 struct ofp11_queue_get_config_request *qgcr11;
2467 request = ofpraw_alloc(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST,
2469 qgcr11 = ofpbuf_put_zeros(request, sizeof *qgcr11);
2470 qgcr11->port = ofputil_port_to_ofp11(port);
2476 /* Parses OFPT_QUEUE_GET_CONFIG request 'oh', storing the port specified by the
2477 * request into '*port'. Returns 0 if successful, otherwise an OpenFlow error
2480 ofputil_decode_queue_get_config_request(const struct ofp_header *oh,
2483 const struct ofp10_queue_get_config_request *qgcr10;
2484 const struct ofp11_queue_get_config_request *qgcr11;
2488 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2489 raw = ofpraw_pull_assert(&b);
2491 switch ((int) raw) {
2492 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2494 *port = u16_to_ofp(ntohs(qgcr10->port));
2497 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2499 enum ofperr error = ofputil_port_from_ofp11(qgcr11->port, port);
2500 if (error || *port == OFPP_ANY) {
2509 return (ofp_to_u16(*port) < ofp_to_u16(OFPP_MAX)
2511 : OFPERR_OFPQOFC_BAD_PORT);
2514 /* Constructs and returns the beginning of a reply to
2515 * OFPT_QUEUE_GET_CONFIG_REQUEST 'oh'. The caller may append information about
2516 * individual queues with ofputil_append_queue_get_config_reply(). */
2518 ofputil_encode_queue_get_config_reply(const struct ofp_header *oh)
2520 struct ofp10_queue_get_config_reply *qgcr10;
2521 struct ofp11_queue_get_config_reply *qgcr11;
2522 struct ofpbuf *reply;
2528 error = ofputil_decode_queue_get_config_request(oh, &port);
2531 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2532 raw = ofpraw_pull_assert(&b);
2534 switch ((int) raw) {
2535 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2536 reply = ofpraw_alloc_reply(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY,
2538 qgcr10 = ofpbuf_put_zeros(reply, sizeof *qgcr10);
2539 qgcr10->port = htons(ofp_to_u16(port));
2542 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2543 reply = ofpraw_alloc_reply(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY,
2545 qgcr11 = ofpbuf_put_zeros(reply, sizeof *qgcr11);
2546 qgcr11->port = ofputil_port_to_ofp11(port);
2557 put_ofp10_queue_rate(struct ofpbuf *reply,
2558 enum ofp10_queue_properties property, uint16_t rate)
2560 if (rate != UINT16_MAX) {
2561 struct ofp10_queue_prop_rate *oqpr;
2563 oqpr = ofpbuf_put_zeros(reply, sizeof *oqpr);
2564 oqpr->prop_header.property = htons(property);
2565 oqpr->prop_header.len = htons(sizeof *oqpr);
2566 oqpr->rate = htons(rate);
2570 /* Appends a queue description for 'queue_id' to the
2571 * OFPT_QUEUE_GET_CONFIG_REPLY already in 'oh'. */
2573 ofputil_append_queue_get_config_reply(struct ofpbuf *reply,
2574 const struct ofputil_queue_config *oqc)
2576 const struct ofp_header *oh = reply->data;
2577 size_t start_ofs, len_ofs;
2580 start_ofs = reply->size;
2581 if (oh->version < OFP12_VERSION) {
2582 struct ofp10_packet_queue *opq10;
2584 opq10 = ofpbuf_put_zeros(reply, sizeof *opq10);
2585 opq10->queue_id = htonl(oqc->queue_id);
2586 len_ofs = (char *) &opq10->len - (char *) reply->data;
2588 struct ofp12_packet_queue *opq12;
2590 opq12 = ofpbuf_put_zeros(reply, sizeof *opq12);
2591 opq12->port = ofputil_port_to_ofp11(oqc->port);
2592 opq12->queue_id = htonl(oqc->queue_id);
2593 len_ofs = (char *) &opq12->len - (char *) reply->data;
2596 put_ofp10_queue_rate(reply, OFPQT10_MIN_RATE, oqc->min_rate);
2597 put_ofp10_queue_rate(reply, OFPQT11_MAX_RATE, oqc->max_rate);
2599 len = ofpbuf_at(reply, len_ofs, sizeof *len);
2600 *len = htons(reply->size - start_ofs);
2603 /* Decodes the initial part of an OFPT_QUEUE_GET_CONFIG_REPLY from 'reply' and
2604 * stores in '*port' the port that the reply is about. The caller may call
2605 * ofputil_pull_queue_get_config_reply() to obtain information about individual
2606 * queues included in the reply. Returns 0 if successful, otherwise an
2609 ofputil_decode_queue_get_config_reply(struct ofpbuf *reply, ofp_port_t *port)
2611 const struct ofp10_queue_get_config_reply *qgcr10;
2612 const struct ofp11_queue_get_config_reply *qgcr11;
2615 raw = ofpraw_pull_assert(reply);
2616 switch ((int) raw) {
2617 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY:
2618 qgcr10 = ofpbuf_pull(reply, sizeof *qgcr10);
2619 *port = u16_to_ofp(ntohs(qgcr10->port));
2622 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY:
2623 qgcr11 = ofpbuf_pull(reply, sizeof *qgcr11);
2624 return ofputil_port_from_ofp11(qgcr11->port, port);
2631 parse_ofp10_queue_rate(const struct ofp10_queue_prop_header *hdr,
2634 const struct ofp10_queue_prop_rate *oqpr;
2636 if (hdr->len == htons(sizeof *oqpr)) {
2637 oqpr = (const struct ofp10_queue_prop_rate *) hdr;
2638 *rate = ntohs(oqpr->rate);
2641 return OFPERR_OFPBRC_BAD_LEN;
2645 /* Decodes information about a queue from the OFPT_QUEUE_GET_CONFIG_REPLY in
2646 * 'reply' and stores it in '*queue'. ofputil_decode_queue_get_config_reply()
2647 * must already have pulled off the main header.
2649 * This function returns EOF if the last queue has already been decoded, 0 if a
2650 * queue was successfully decoded into '*queue', or an ofperr if there was a
2651 * problem decoding 'reply'. */
2653 ofputil_pull_queue_get_config_reply(struct ofpbuf *reply,
2654 struct ofputil_queue_config *queue)
2656 const struct ofp_header *oh;
2657 unsigned int opq_len;
2664 queue->min_rate = UINT16_MAX;
2665 queue->max_rate = UINT16_MAX;
2668 if (oh->version < OFP12_VERSION) {
2669 const struct ofp10_packet_queue *opq10;
2671 opq10 = ofpbuf_try_pull(reply, sizeof *opq10);
2673 return OFPERR_OFPBRC_BAD_LEN;
2675 queue->queue_id = ntohl(opq10->queue_id);
2676 len = ntohs(opq10->len);
2677 opq_len = sizeof *opq10;
2679 const struct ofp12_packet_queue *opq12;
2681 opq12 = ofpbuf_try_pull(reply, sizeof *opq12);
2683 return OFPERR_OFPBRC_BAD_LEN;
2685 queue->queue_id = ntohl(opq12->queue_id);
2686 len = ntohs(opq12->len);
2687 opq_len = sizeof *opq12;
2690 if (len < opq_len || len > reply->size + opq_len || len % 8) {
2691 return OFPERR_OFPBRC_BAD_LEN;
2696 const struct ofp10_queue_prop_header *hdr;
2697 unsigned int property;
2698 unsigned int prop_len;
2699 enum ofperr error = 0;
2701 hdr = ofpbuf_at_assert(reply, 0, sizeof *hdr);
2702 prop_len = ntohs(hdr->len);
2703 if (prop_len < sizeof *hdr || prop_len > reply->size || prop_len % 8) {
2704 return OFPERR_OFPBRC_BAD_LEN;
2707 property = ntohs(hdr->property);
2709 case OFPQT10_MIN_RATE:
2710 error = parse_ofp10_queue_rate(hdr, &queue->min_rate);
2713 case OFPQT11_MAX_RATE:
2714 error = parse_ofp10_queue_rate(hdr, &queue->max_rate);
2718 VLOG_INFO_RL(&bad_ofmsg_rl, "unknown queue property %u", property);
2725 ofpbuf_pull(reply, prop_len);
2731 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
2732 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
2733 * successful, otherwise an OpenFlow error code. */
2735 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
2736 const struct ofp_header *oh)
2741 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2742 raw = ofpraw_pull_assert(&b);
2743 switch ((int) raw) {
2744 case OFPRAW_OFPST10_FLOW_REQUEST:
2745 return ofputil_decode_ofpst10_flow_request(fsr, b.data, false);
2747 case OFPRAW_OFPST10_AGGREGATE_REQUEST:
2748 return ofputil_decode_ofpst10_flow_request(fsr, b.data, true);
2750 case OFPRAW_OFPST11_FLOW_REQUEST:
2751 return ofputil_decode_ofpst11_flow_request(fsr, &b, false);
2753 case OFPRAW_OFPST11_AGGREGATE_REQUEST:
2754 return ofputil_decode_ofpst11_flow_request(fsr, &b, true);
2756 case OFPRAW_NXST_FLOW_REQUEST:
2757 return ofputil_decode_nxst_flow_request(fsr, &b, false);
2759 case OFPRAW_NXST_AGGREGATE_REQUEST:
2760 return ofputil_decode_nxst_flow_request(fsr, &b, true);
2763 /* Hey, the caller lied. */
2768 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
2769 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
2770 * 'protocol', and returns the message. */
2772 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
2773 enum ofputil_protocol protocol)
2779 case OFPUTIL_P_OF11_STD:
2780 case OFPUTIL_P_OF12_OXM:
2781 case OFPUTIL_P_OF13_OXM:
2782 case OFPUTIL_P_OF14_OXM:
2783 case OFPUTIL_P_OF15_OXM: {
2784 struct ofp11_flow_stats_request *ofsr;
2786 raw = (fsr->aggregate
2787 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
2788 : OFPRAW_OFPST11_FLOW_REQUEST);
2789 msg = ofpraw_alloc(raw, ofputil_protocol_to_ofp_version(protocol),
2790 ofputil_match_typical_len(protocol));
2791 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2792 ofsr->table_id = fsr->table_id;
2793 ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port);
2794 ofsr->out_group = htonl(fsr->out_group);
2795 ofsr->cookie = fsr->cookie;
2796 ofsr->cookie_mask = fsr->cookie_mask;
2797 ofputil_put_ofp11_match(msg, &fsr->match, protocol);
2801 case OFPUTIL_P_OF10_STD:
2802 case OFPUTIL_P_OF10_STD_TID: {
2803 struct ofp10_flow_stats_request *ofsr;
2805 raw = (fsr->aggregate
2806 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
2807 : OFPRAW_OFPST10_FLOW_REQUEST);
2808 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
2809 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2810 ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match);
2811 ofsr->table_id = fsr->table_id;
2812 ofsr->out_port = htons(ofp_to_u16(fsr->out_port));
2816 case OFPUTIL_P_OF10_NXM:
2817 case OFPUTIL_P_OF10_NXM_TID: {
2818 struct nx_flow_stats_request *nfsr;
2821 raw = (fsr->aggregate
2822 ? OFPRAW_NXST_AGGREGATE_REQUEST
2823 : OFPRAW_NXST_FLOW_REQUEST);
2824 msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN);
2825 ofpbuf_put_zeros(msg, sizeof *nfsr);
2826 match_len = nx_put_match(msg, &fsr->match,
2827 fsr->cookie, fsr->cookie_mask);
2830 nfsr->out_port = htons(ofp_to_u16(fsr->out_port));
2831 nfsr->match_len = htons(match_len);
2832 nfsr->table_id = fsr->table_id;
2843 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
2844 * ofputil_flow_stats in 'fs'.
2846 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
2847 * OpenFlow message. Calling this function multiple times for a single 'msg'
2848 * iterates through the replies. The caller must initially leave 'msg''s layer
2849 * pointers null and not modify them between calls.
2851 * Most switches don't send the values needed to populate fs->idle_age and
2852 * fs->hard_age, so those members will usually be set to 0. If the switch from
2853 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
2854 * 'flow_age_extension' as true so that the contents of 'msg' determine the
2855 * 'idle_age' and 'hard_age' members in 'fs'.
2857 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
2858 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
2859 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
2861 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2862 * otherwise a positive errno value. */
2864 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
2866 bool flow_age_extension,
2867 struct ofpbuf *ofpacts)
2869 const struct ofp_header *oh;
2870 size_t instructions_len;
2874 error = (msg->header ? ofpraw_decode(&raw, msg->header)
2875 : ofpraw_pull(&raw, msg));
2883 } else if (raw == OFPRAW_OFPST11_FLOW_REPLY
2884 || raw == OFPRAW_OFPST13_FLOW_REPLY) {
2885 const struct ofp11_flow_stats *ofs;
2887 uint16_t padded_match_len;
2889 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2891 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
2892 "bytes at end", msg->size);
2896 length = ntohs(ofs->length);
2897 if (length < sizeof *ofs) {
2898 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2899 "length %"PRIuSIZE, length);
2903 if (ofputil_pull_ofp11_match(msg, &fs->match, &padded_match_len)) {
2904 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match");
2907 instructions_len = length - sizeof *ofs - padded_match_len;
2909 fs->priority = ntohs(ofs->priority);
2910 fs->table_id = ofs->table_id;
2911 fs->duration_sec = ntohl(ofs->duration_sec);
2912 fs->duration_nsec = ntohl(ofs->duration_nsec);
2913 fs->idle_timeout = ntohs(ofs->idle_timeout);
2914 fs->hard_timeout = ntohs(ofs->hard_timeout);
2915 if (oh->version >= OFP14_VERSION) {
2916 fs->importance = ntohs(ofs->importance);
2920 if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
2921 error = ofputil_decode_flow_mod_flags(ofs->flags, -1, oh->version,
2931 fs->cookie = ofs->cookie;
2932 fs->packet_count = ntohll(ofs->packet_count);
2933 fs->byte_count = ntohll(ofs->byte_count);
2934 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
2935 const struct ofp10_flow_stats *ofs;
2938 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2940 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
2941 "bytes at end", msg->size);
2945 length = ntohs(ofs->length);
2946 if (length < sizeof *ofs) {
2947 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2948 "length %"PRIuSIZE, length);
2951 instructions_len = length - sizeof *ofs;
2953 fs->cookie = get_32aligned_be64(&ofs->cookie);
2954 ofputil_match_from_ofp10_match(&ofs->match, &fs->match);
2955 fs->priority = ntohs(ofs->priority);
2956 fs->table_id = ofs->table_id;
2957 fs->duration_sec = ntohl(ofs->duration_sec);
2958 fs->duration_nsec = ntohl(ofs->duration_nsec);
2959 fs->idle_timeout = ntohs(ofs->idle_timeout);
2960 fs->hard_timeout = ntohs(ofs->hard_timeout);
2964 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
2965 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
2967 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
2968 const struct nx_flow_stats *nfs;
2969 size_t match_len, length;
2971 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
2973 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %"PRIu32" leftover "
2974 "bytes at end", msg->size);
2978 length = ntohs(nfs->length);
2979 match_len = ntohs(nfs->match_len);
2980 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
2981 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%"PRIuSIZE" "
2982 "claims invalid length %"PRIuSIZE, match_len, length);
2985 if (nx_pull_match(msg, match_len, &fs->match, NULL, NULL)) {
2988 instructions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
2990 fs->cookie = nfs->cookie;
2991 fs->table_id = nfs->table_id;
2992 fs->duration_sec = ntohl(nfs->duration_sec);
2993 fs->duration_nsec = ntohl(nfs->duration_nsec);
2994 fs->priority = ntohs(nfs->priority);
2995 fs->idle_timeout = ntohs(nfs->idle_timeout);
2996 fs->hard_timeout = ntohs(nfs->hard_timeout);
3000 if (flow_age_extension) {
3001 if (nfs->idle_age) {
3002 fs->idle_age = ntohs(nfs->idle_age) - 1;
3004 if (nfs->hard_age) {
3005 fs->hard_age = ntohs(nfs->hard_age) - 1;
3008 fs->packet_count = ntohll(nfs->packet_count);
3009 fs->byte_count = ntohll(nfs->byte_count);
3015 if (ofpacts_pull_openflow_instructions(msg, instructions_len, oh->version,
3017 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions");
3020 fs->ofpacts = ofpacts->data;
3021 fs->ofpacts_len = ofpacts->size;
3026 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
3028 * We use this in situations where OVS internally uses UINT64_MAX to mean
3029 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
3031 unknown_to_zero(uint64_t count)
3033 return count != UINT64_MAX ? count : 0;
3036 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
3037 * those already present in the list of ofpbufs in 'replies'. 'replies' should
3038 * have been initialized with ofpmp_init(). */
3040 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
3041 struct ovs_list *replies)
3043 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
3044 size_t start_ofs = reply->size;
3045 enum ofp_version version = ofpmp_version(replies);
3046 enum ofpraw raw = ofpmp_decode_raw(replies);
3048 if (raw == OFPRAW_OFPST11_FLOW_REPLY || raw == OFPRAW_OFPST13_FLOW_REPLY) {
3049 struct ofp11_flow_stats *ofs;
3051 ofpbuf_put_uninit(reply, sizeof *ofs);
3052 oxm_put_match(reply, &fs->match, version);
3053 ofpacts_put_openflow_instructions(fs->ofpacts, fs->ofpacts_len, reply,
3056 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
3057 ofs->length = htons(reply->size - start_ofs);
3058 ofs->table_id = fs->table_id;
3060 ofs->duration_sec = htonl(fs->duration_sec);
3061 ofs->duration_nsec = htonl(fs->duration_nsec);
3062 ofs->priority = htons(fs->priority);
3063 ofs->idle_timeout = htons(fs->idle_timeout);
3064 ofs->hard_timeout = htons(fs->hard_timeout);
3065 if (version >= OFP14_VERSION) {
3066 ofs->importance = htons(fs->importance);
3068 ofs->importance = 0;
3070 if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
3071 ofs->flags = ofputil_encode_flow_mod_flags(fs->flags, version);
3075 memset(ofs->pad2, 0, sizeof ofs->pad2);
3076 ofs->cookie = fs->cookie;
3077 ofs->packet_count = htonll(unknown_to_zero(fs->packet_count));
3078 ofs->byte_count = htonll(unknown_to_zero(fs->byte_count));
3079 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
3080 struct ofp10_flow_stats *ofs;
3082 ofpbuf_put_uninit(reply, sizeof *ofs);
3083 ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
3085 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
3086 ofs->length = htons(reply->size - start_ofs);
3087 ofs->table_id = fs->table_id;
3089 ofputil_match_to_ofp10_match(&fs->match, &ofs->match);
3090 ofs->duration_sec = htonl(fs->duration_sec);
3091 ofs->duration_nsec = htonl(fs->duration_nsec);
3092 ofs->priority = htons(fs->priority);
3093 ofs->idle_timeout = htons(fs->idle_timeout);
3094 ofs->hard_timeout = htons(fs->hard_timeout);
3095 memset(ofs->pad2, 0, sizeof ofs->pad2);
3096 put_32aligned_be64(&ofs->cookie, fs->cookie);
3097 put_32aligned_be64(&ofs->packet_count,
3098 htonll(unknown_to_zero(fs->packet_count)));
3099 put_32aligned_be64(&ofs->byte_count,
3100 htonll(unknown_to_zero(fs->byte_count)));
3101 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
3102 struct nx_flow_stats *nfs;
3105 ofpbuf_put_uninit(reply, sizeof *nfs);
3106 match_len = nx_put_match(reply, &fs->match, 0, 0);
3107 ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
3109 nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
3110 nfs->length = htons(reply->size - start_ofs);
3111 nfs->table_id = fs->table_id;
3113 nfs->duration_sec = htonl(fs->duration_sec);
3114 nfs->duration_nsec = htonl(fs->duration_nsec);
3115 nfs->priority = htons(fs->priority);
3116 nfs->idle_timeout = htons(fs->idle_timeout);
3117 nfs->hard_timeout = htons(fs->hard_timeout);
3118 nfs->idle_age = htons(fs->idle_age < 0 ? 0
3119 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
3121 nfs->hard_age = htons(fs->hard_age < 0 ? 0
3122 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
3124 nfs->match_len = htons(match_len);
3125 nfs->cookie = fs->cookie;
3126 nfs->packet_count = htonll(fs->packet_count);
3127 nfs->byte_count = htonll(fs->byte_count);
3132 ofpmp_postappend(replies, start_ofs);
3135 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
3136 * NXST_AGGREGATE reply matching 'request', and returns the message. */
3138 ofputil_encode_aggregate_stats_reply(
3139 const struct ofputil_aggregate_stats *stats,
3140 const struct ofp_header *request)
3142 struct ofp_aggregate_stats_reply *asr;
3143 uint64_t packet_count;
3144 uint64_t byte_count;
3148 ofpraw_decode(&raw, request);
3149 if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) {
3150 packet_count = unknown_to_zero(stats->packet_count);
3151 byte_count = unknown_to_zero(stats->byte_count);
3153 packet_count = stats->packet_count;
3154 byte_count = stats->byte_count;
3157 msg = ofpraw_alloc_stats_reply(request, 0);
3158 asr = ofpbuf_put_zeros(msg, sizeof *asr);
3159 put_32aligned_be64(&asr->packet_count, htonll(packet_count));
3160 put_32aligned_be64(&asr->byte_count, htonll(byte_count));
3161 asr->flow_count = htonl(stats->flow_count);
3167 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
3168 const struct ofp_header *reply)
3170 struct ofp_aggregate_stats_reply *asr;
3173 ofpbuf_use_const(&msg, reply, ntohs(reply->length));
3174 ofpraw_pull_assert(&msg);
3177 stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
3178 stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
3179 stats->flow_count = ntohl(asr->flow_count);
3184 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
3185 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
3186 * an OpenFlow error code. */
3188 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
3189 const struct ofp_header *oh)
3194 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3195 raw = ofpraw_pull_assert(&b);
3196 if (raw == OFPRAW_OFPT11_FLOW_REMOVED) {
3197 const struct ofp12_flow_removed *ofr;
3200 ofr = ofpbuf_pull(&b, sizeof *ofr);
3202 error = ofputil_pull_ofp11_match(&b, &fr->match, NULL);
3207 fr->priority = ntohs(ofr->priority);
3208 fr->cookie = ofr->cookie;
3209 fr->reason = ofr->reason;
3210 fr->table_id = ofr->table_id;
3211 fr->duration_sec = ntohl(ofr->duration_sec);
3212 fr->duration_nsec = ntohl(ofr->duration_nsec);
3213 fr->idle_timeout = ntohs(ofr->idle_timeout);
3214 fr->hard_timeout = ntohs(ofr->hard_timeout);
3215 fr->packet_count = ntohll(ofr->packet_count);
3216 fr->byte_count = ntohll(ofr->byte_count);
3217 } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
3218 const struct ofp10_flow_removed *ofr;
3220 ofr = ofpbuf_pull(&b, sizeof *ofr);
3222 ofputil_match_from_ofp10_match(&ofr->match, &fr->match);
3223 fr->priority = ntohs(ofr->priority);
3224 fr->cookie = ofr->cookie;
3225 fr->reason = ofr->reason;
3227 fr->duration_sec = ntohl(ofr->duration_sec);
3228 fr->duration_nsec = ntohl(ofr->duration_nsec);
3229 fr->idle_timeout = ntohs(ofr->idle_timeout);
3230 fr->hard_timeout = 0;
3231 fr->packet_count = ntohll(ofr->packet_count);
3232 fr->byte_count = ntohll(ofr->byte_count);
3233 } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
3234 struct nx_flow_removed *nfr;
3237 nfr = ofpbuf_pull(&b, sizeof *nfr);
3238 error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match,
3244 return OFPERR_OFPBRC_BAD_LEN;
3247 fr->priority = ntohs(nfr->priority);
3248 fr->cookie = nfr->cookie;
3249 fr->reason = nfr->reason;
3250 fr->table_id = nfr->table_id ? nfr->table_id - 1 : 255;
3251 fr->duration_sec = ntohl(nfr->duration_sec);
3252 fr->duration_nsec = ntohl(nfr->duration_nsec);
3253 fr->idle_timeout = ntohs(nfr->idle_timeout);
3254 fr->hard_timeout = 0;
3255 fr->packet_count = ntohll(nfr->packet_count);
3256 fr->byte_count = ntohll(nfr->byte_count);
3264 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
3265 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
3268 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
3269 enum ofputil_protocol protocol)
3272 enum ofp_flow_removed_reason reason = fr->reason;
3274 if (reason == OFPRR_METER_DELETE && !(protocol & OFPUTIL_P_OF14_UP)) {
3275 reason = OFPRR_DELETE;
3279 case OFPUTIL_P_OF11_STD:
3280 case OFPUTIL_P_OF12_OXM:
3281 case OFPUTIL_P_OF13_OXM:
3282 case OFPUTIL_P_OF14_OXM:
3283 case OFPUTIL_P_OF15_OXM: {
3284 struct ofp12_flow_removed *ofr;
3286 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED,
3287 ofputil_protocol_to_ofp_version(protocol),
3289 ofputil_match_typical_len(protocol));
3290 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3291 ofr->cookie = fr->cookie;
3292 ofr->priority = htons(fr->priority);
3293 ofr->reason = reason;
3294 ofr->table_id = fr->table_id;
3295 ofr->duration_sec = htonl(fr->duration_sec);
3296 ofr->duration_nsec = htonl(fr->duration_nsec);
3297 ofr->idle_timeout = htons(fr->idle_timeout);
3298 ofr->hard_timeout = htons(fr->hard_timeout);
3299 ofr->packet_count = htonll(fr->packet_count);
3300 ofr->byte_count = htonll(fr->byte_count);
3301 ofputil_put_ofp11_match(msg, &fr->match, protocol);
3305 case OFPUTIL_P_OF10_STD:
3306 case OFPUTIL_P_OF10_STD_TID: {
3307 struct ofp10_flow_removed *ofr;
3309 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
3311 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3312 ofputil_match_to_ofp10_match(&fr->match, &ofr->match);
3313 ofr->cookie = fr->cookie;
3314 ofr->priority = htons(fr->priority);
3315 ofr->reason = reason;
3316 ofr->duration_sec = htonl(fr->duration_sec);
3317 ofr->duration_nsec = htonl(fr->duration_nsec);
3318 ofr->idle_timeout = htons(fr->idle_timeout);
3319 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
3320 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
3324 case OFPUTIL_P_OF10_NXM:
3325 case OFPUTIL_P_OF10_NXM_TID: {
3326 struct nx_flow_removed *nfr;
3329 msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
3330 htonl(0), NXM_TYPICAL_LEN);
3331 ofpbuf_put_zeros(msg, sizeof *nfr);
3332 match_len = nx_put_match(msg, &fr->match, 0, 0);
3335 nfr->cookie = fr->cookie;
3336 nfr->priority = htons(fr->priority);
3337 nfr->reason = reason;
3338 nfr->table_id = fr->table_id + 1;
3339 nfr->duration_sec = htonl(fr->duration_sec);
3340 nfr->duration_nsec = htonl(fr->duration_nsec);
3341 nfr->idle_timeout = htons(fr->idle_timeout);
3342 nfr->match_len = htons(match_len);
3343 nfr->packet_count = htonll(fr->packet_count);
3344 nfr->byte_count = htonll(fr->byte_count);
3356 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
3357 const struct ofp_header *oh)
3362 memset(pin, 0, sizeof *pin);
3363 pin->cookie = OVS_BE64_MAX;
3365 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3366 raw = ofpraw_pull_assert(&b);
3367 if (raw == OFPRAW_OFPT13_PACKET_IN || raw == OFPRAW_OFPT12_PACKET_IN) {
3368 const struct ofp13_packet_in *opi;
3370 size_t packet_in_size;
3372 if (raw == OFPRAW_OFPT12_PACKET_IN) {
3373 packet_in_size = sizeof (struct ofp12_packet_in);
3375 packet_in_size = sizeof (struct ofp13_packet_in);
3378 opi = ofpbuf_pull(&b, packet_in_size);
3379 error = oxm_pull_match_loose(&b, &pin->flow_metadata);
3384 if (!ofpbuf_try_pull(&b, 2)) {
3385 return OFPERR_OFPBRC_BAD_LEN;
3388 pin->reason = opi->pi.reason;
3389 pin->table_id = opi->pi.table_id;
3390 pin->buffer_id = ntohl(opi->pi.buffer_id);
3391 pin->total_len = ntohs(opi->pi.total_len);
3393 if (raw == OFPRAW_OFPT13_PACKET_IN) {
3394 pin->cookie = opi->cookie;
3397 pin->packet = b.data;
3398 pin->packet_len = b.size;
3399 } else if (raw == OFPRAW_OFPT10_PACKET_IN) {
3400 const struct ofp10_packet_in *opi;
3402 opi = ofpbuf_pull(&b, offsetof(struct ofp10_packet_in, data));
3404 pin->packet = opi->data;
3405 pin->packet_len = b.size;
3407 match_init_catchall(&pin->flow_metadata);
3408 match_set_in_port(&pin->flow_metadata, u16_to_ofp(ntohs(opi->in_port)));
3409 pin->reason = opi->reason;
3410 pin->buffer_id = ntohl(opi->buffer_id);
3411 pin->total_len = ntohs(opi->total_len);
3412 } else if (raw == OFPRAW_OFPT11_PACKET_IN) {
3413 const struct ofp11_packet_in *opi;
3417 opi = ofpbuf_pull(&b, sizeof *opi);
3419 pin->packet = b.data;
3420 pin->packet_len = b.size;
3422 pin->buffer_id = ntohl(opi->buffer_id);
3423 error = ofputil_port_from_ofp11(opi->in_port, &in_port);
3427 match_init_catchall(&pin->flow_metadata);
3428 match_set_in_port(&pin->flow_metadata, in_port);
3429 pin->total_len = ntohs(opi->total_len);
3430 pin->reason = opi->reason;
3431 pin->table_id = opi->table_id;
3432 } else if (raw == OFPRAW_NXT_PACKET_IN) {
3433 const struct nx_packet_in *npi;
3436 npi = ofpbuf_pull(&b, sizeof *npi);
3437 error = nx_pull_match_loose(&b, ntohs(npi->match_len),
3438 &pin->flow_metadata, NULL, NULL);
3443 if (!ofpbuf_try_pull(&b, 2)) {
3444 return OFPERR_OFPBRC_BAD_LEN;
3447 pin->reason = npi->reason;
3448 pin->table_id = npi->table_id;
3449 pin->cookie = npi->cookie;
3451 pin->buffer_id = ntohl(npi->buffer_id);
3452 pin->total_len = ntohs(npi->total_len);
3454 pin->packet = b.data;
3455 pin->packet_len = b.size;
3463 static struct ofpbuf *
3464 ofputil_encode_ofp10_packet_in(const struct ofputil_packet_in *pin)
3466 struct ofp10_packet_in *opi;
3467 struct ofpbuf *packet;
3469 packet = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
3470 htonl(0), pin->packet_len);
3471 opi = ofpbuf_put_zeros(packet, offsetof(struct ofp10_packet_in, data));
3472 opi->total_len = htons(pin->total_len);
3473 opi->in_port = htons(ofp_to_u16(pin->flow_metadata.flow.in_port.ofp_port));
3474 opi->reason = pin->reason;
3475 opi->buffer_id = htonl(pin->buffer_id);
3477 ofpbuf_put(packet, pin->packet, pin->packet_len);
3482 static struct ofpbuf *
3483 ofputil_encode_nx_packet_in(const struct ofputil_packet_in *pin)
3485 struct nx_packet_in *npi;
3486 struct ofpbuf *packet;
3489 /* The final argument is just an estimate of the space required. */
3490 packet = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, OFP10_VERSION,
3491 htonl(0), NXM_TYPICAL_LEN + 2 + pin->packet_len);
3492 ofpbuf_put_zeros(packet, sizeof *npi);
3493 match_len = nx_put_match(packet, &pin->flow_metadata, 0, 0);
3494 ofpbuf_put_zeros(packet, 2);
3495 ofpbuf_put(packet, pin->packet, pin->packet_len);
3498 npi->buffer_id = htonl(pin->buffer_id);
3499 npi->total_len = htons(pin->total_len);
3500 npi->reason = pin->reason;
3501 npi->table_id = pin->table_id;
3502 npi->cookie = pin->cookie;
3503 npi->match_len = htons(match_len);
3508 static struct ofpbuf *
3509 ofputil_encode_ofp11_packet_in(const struct ofputil_packet_in *pin)
3511 struct ofp11_packet_in *opi;
3512 struct ofpbuf *packet;
3514 packet = ofpraw_alloc_xid(OFPRAW_OFPT11_PACKET_IN, OFP11_VERSION,
3515 htonl(0), pin->packet_len);
3516 opi = ofpbuf_put_zeros(packet, sizeof *opi);
3517 opi->buffer_id = htonl(pin->buffer_id);
3518 opi->in_port = ofputil_port_to_ofp11(pin->flow_metadata.flow.in_port.ofp_port);
3519 opi->in_phy_port = opi->in_port;
3520 opi->total_len = htons(pin->total_len);
3521 opi->reason = pin->reason;
3522 opi->table_id = pin->table_id;
3524 ofpbuf_put(packet, pin->packet, pin->packet_len);
3529 static struct ofpbuf *
3530 ofputil_encode_ofp12_packet_in(const struct ofputil_packet_in *pin,
3531 enum ofputil_protocol protocol)
3533 struct ofp13_packet_in *opi;
3534 enum ofpraw packet_in_raw;
3535 enum ofp_version packet_in_version;
3536 size_t packet_in_size;
3537 struct ofpbuf *packet;
3539 if (protocol == OFPUTIL_P_OF12_OXM) {
3540 packet_in_raw = OFPRAW_OFPT12_PACKET_IN;
3541 packet_in_version = OFP12_VERSION;
3542 packet_in_size = sizeof (struct ofp12_packet_in);
3544 packet_in_raw = OFPRAW_OFPT13_PACKET_IN;
3545 packet_in_version = ofputil_protocol_to_ofp_version(protocol);
3546 packet_in_size = sizeof (struct ofp13_packet_in);
3549 /* The final argument is just an estimate of the space required. */
3550 packet = ofpraw_alloc_xid(packet_in_raw, packet_in_version,
3551 htonl(0), NXM_TYPICAL_LEN + 2 + pin->packet_len);
3552 ofpbuf_put_zeros(packet, packet_in_size);
3553 oxm_put_match(packet, &pin->flow_metadata,
3554 ofputil_protocol_to_ofp_version(protocol));
3555 ofpbuf_put_zeros(packet, 2);
3556 ofpbuf_put(packet, pin->packet, pin->packet_len);
3559 opi->pi.buffer_id = htonl(pin->buffer_id);
3560 opi->pi.total_len = htons(pin->total_len);
3561 opi->pi.reason = pin->reason;
3562 opi->pi.table_id = pin->table_id;
3563 if (protocol != OFPUTIL_P_OF12_OXM) {
3564 opi->cookie = pin->cookie;
3570 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
3571 * in the format specified by 'packet_in_format'. */
3573 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
3574 enum ofputil_protocol protocol,
3575 enum nx_packet_in_format packet_in_format)
3577 struct ofpbuf *packet;
3580 case OFPUTIL_P_OF10_STD:
3581 case OFPUTIL_P_OF10_STD_TID:
3582 case OFPUTIL_P_OF10_NXM:
3583 case OFPUTIL_P_OF10_NXM_TID:
3584 packet = (packet_in_format == NXPIF_NXM
3585 ? ofputil_encode_nx_packet_in(pin)
3586 : ofputil_encode_ofp10_packet_in(pin));
3589 case OFPUTIL_P_OF11_STD:
3590 packet = ofputil_encode_ofp11_packet_in(pin);
3593 case OFPUTIL_P_OF12_OXM:
3594 case OFPUTIL_P_OF13_OXM:
3595 case OFPUTIL_P_OF14_OXM:
3596 case OFPUTIL_P_OF15_OXM:
3597 packet = ofputil_encode_ofp12_packet_in(pin, protocol);
3604 ofpmsg_update_length(packet);
3608 /* Returns a string form of 'reason'. The return value is either a statically
3609 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
3610 * 'bufsize' should be at least OFPUTIL_PACKET_IN_REASON_BUFSIZE. */
3612 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason,
3613 char *reasonbuf, size_t bufsize)
3620 case OFPR_INVALID_TTL:
3621 return "invalid_ttl";
3622 case OFPR_ACTION_SET:
3623 return "action_set";
3626 case OFPR_PACKET_OUT:
3627 return "packet_out";
3629 case OFPR_N_REASONS:
3631 snprintf(reasonbuf, bufsize, "%d", (int) reason);
3637 ofputil_packet_in_reason_from_string(const char *s,
3638 enum ofp_packet_in_reason *reason)
3642 for (i = 0; i < OFPR_N_REASONS; i++) {
3643 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
3644 const char *reason_s;
3646 reason_s = ofputil_packet_in_reason_to_string(i, reasonbuf,
3648 if (!strcasecmp(s, reason_s)) {
3656 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
3659 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
3660 * message's actions. The caller must initialize 'ofpacts' and retains
3661 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
3663 * Returns 0 if successful, otherwise an OFPERR_* value. */
3665 ofputil_decode_packet_out(struct ofputil_packet_out *po,
3666 const struct ofp_header *oh,
3667 struct ofpbuf *ofpacts)
3672 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3673 raw = ofpraw_pull_assert(&b);
3675 ofpbuf_clear(ofpacts);
3676 if (raw == OFPRAW_OFPT11_PACKET_OUT) {
3678 const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
3680 po->buffer_id = ntohl(opo->buffer_id);
3681 error = ofputil_port_from_ofp11(opo->in_port, &po->in_port);
3686 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
3687 oh->version, ofpacts);
3691 } else if (raw == OFPRAW_OFPT10_PACKET_OUT) {
3693 const struct ofp10_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
3695 po->buffer_id = ntohl(opo->buffer_id);
3696 po->in_port = u16_to_ofp(ntohs(opo->in_port));
3698 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
3699 oh->version, ofpacts);
3707 if (ofp_to_u16(po->in_port) >= ofp_to_u16(OFPP_MAX)
3708 && po->in_port != OFPP_LOCAL
3709 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
3710 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
3712 return OFPERR_OFPBRC_BAD_PORT;
3715 po->ofpacts = ofpacts->data;
3716 po->ofpacts_len = ofpacts->size;
3718 if (po->buffer_id == UINT32_MAX) {
3719 po->packet = b.data;
3720 po->packet_len = b.size;
3729 /* ofputil_phy_port */
3731 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
3732 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
3733 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
3734 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
3735 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
3736 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
3737 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
3738 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
3740 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
3741 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
3742 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
3743 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
3744 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
3745 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
3747 static enum netdev_features
3748 netdev_port_features_from_ofp10(ovs_be32 ofp10_)
3750 uint32_t ofp10 = ntohl(ofp10_);
3751 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
3755 netdev_port_features_to_ofp10(enum netdev_features features)
3757 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
3760 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
3761 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
3762 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
3763 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
3764 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
3765 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
3766 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
3767 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
3768 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
3769 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
3770 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
3771 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
3772 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
3773 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
3774 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
3775 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
3777 static enum netdev_features
3778 netdev_port_features_from_ofp11(ovs_be32 ofp11)
3780 return ntohl(ofp11) & 0xffff;
3784 netdev_port_features_to_ofp11(enum netdev_features features)
3786 return htonl(features & 0xffff);
3790 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
3791 const struct ofp10_phy_port *opp)
3793 pp->port_no = u16_to_ofp(ntohs(opp->port_no));
3794 pp->hw_addr = opp->hw_addr;
3795 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
3797 pp->config = ntohl(opp->config) & OFPPC10_ALL;
3798 pp->state = ntohl(opp->state) & OFPPS10_ALL;
3800 pp->curr = netdev_port_features_from_ofp10(opp->curr);
3801 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
3802 pp->supported = netdev_port_features_from_ofp10(opp->supported);
3803 pp->peer = netdev_port_features_from_ofp10(opp->peer);
3805 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
3806 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
3812 ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
3813 const struct ofp11_port *op)
3817 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
3821 pp->hw_addr = op->hw_addr;
3822 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
3824 pp->config = ntohl(op->config) & OFPPC11_ALL;
3825 pp->state = ntohl(op->state) & OFPPS11_ALL;
3827 pp->curr = netdev_port_features_from_ofp11(op->curr);
3828 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
3829 pp->supported = netdev_port_features_from_ofp11(op->supported);
3830 pp->peer = netdev_port_features_from_ofp11(op->peer);
3832 pp->curr_speed = ntohl(op->curr_speed);
3833 pp->max_speed = ntohl(op->max_speed);
3839 parse_ofp14_port_ethernet_property(const struct ofpbuf *payload,
3840 struct ofputil_phy_port *pp)
3842 struct ofp14_port_desc_prop_ethernet *eth = payload->data;
3844 if (payload->size != sizeof *eth) {
3845 return OFPERR_OFPBPC_BAD_LEN;
3848 pp->curr = netdev_port_features_from_ofp11(eth->curr);
3849 pp->advertised = netdev_port_features_from_ofp11(eth->advertised);
3850 pp->supported = netdev_port_features_from_ofp11(eth->supported);
3851 pp->peer = netdev_port_features_from_ofp11(eth->peer);
3853 pp->curr_speed = ntohl(eth->curr_speed);
3854 pp->max_speed = ntohl(eth->max_speed);
3860 ofputil_pull_ofp14_port(struct ofputil_phy_port *pp, struct ofpbuf *msg)
3862 struct ofpbuf properties;
3863 struct ofp14_port *op;
3867 op = ofpbuf_try_pull(msg, sizeof *op);
3869 return OFPERR_OFPBRC_BAD_LEN;
3872 len = ntohs(op->length);
3873 if (len < sizeof *op || len - sizeof *op > msg->size) {
3874 return OFPERR_OFPBRC_BAD_LEN;
3877 ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len);
3879 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
3883 pp->hw_addr = op->hw_addr;
3884 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
3886 pp->config = ntohl(op->config) & OFPPC11_ALL;
3887 pp->state = ntohl(op->state) & OFPPS11_ALL;
3889 while (properties.size > 0) {
3890 struct ofpbuf payload;
3894 error = ofputil_pull_property(&properties, &payload, &type);
3900 case OFPPDPT14_ETHERNET:
3901 error = parse_ofp14_port_ethernet_property(&payload, pp);
3905 log_property(true, "unknown port property %"PRIu16, type);
3919 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
3920 struct ofp10_phy_port *opp)
3922 memset(opp, 0, sizeof *opp);
3924 opp->port_no = htons(ofp_to_u16(pp->port_no));
3925 opp->hw_addr = pp->hw_addr;
3926 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
3928 opp->config = htonl(pp->config & OFPPC10_ALL);
3929 opp->state = htonl(pp->state & OFPPS10_ALL);
3931 opp->curr = netdev_port_features_to_ofp10(pp->curr);
3932 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
3933 opp->supported = netdev_port_features_to_ofp10(pp->supported);
3934 opp->peer = netdev_port_features_to_ofp10(pp->peer);
3938 ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
3939 struct ofp11_port *op)
3941 memset(op, 0, sizeof *op);
3943 op->port_no = ofputil_port_to_ofp11(pp->port_no);
3944 op->hw_addr = pp->hw_addr;
3945 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
3947 op->config = htonl(pp->config & OFPPC11_ALL);
3948 op->state = htonl(pp->state & OFPPS11_ALL);
3950 op->curr = netdev_port_features_to_ofp11(pp->curr);
3951 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
3952 op->supported = netdev_port_features_to_ofp11(pp->supported);
3953 op->peer = netdev_port_features_to_ofp11(pp->peer);
3955 op->curr_speed = htonl(pp->curr_speed);
3956 op->max_speed = htonl(pp->max_speed);
3960 ofputil_put_ofp14_port(const struct ofputil_phy_port *pp,
3963 struct ofp14_port *op;
3964 struct ofp14_port_desc_prop_ethernet *eth;
3966 ofpbuf_prealloc_tailroom(b, sizeof *op + sizeof *eth);
3968 op = ofpbuf_put_zeros(b, sizeof *op);
3969 op->port_no = ofputil_port_to_ofp11(pp->port_no);
3970 op->length = htons(sizeof *op + sizeof *eth);
3971 op->hw_addr = pp->hw_addr;
3972 ovs_strlcpy(op->name, pp->name, sizeof op->name);
3973 op->config = htonl(pp->config & OFPPC11_ALL);
3974 op->state = htonl(pp->state & OFPPS11_ALL);
3976 eth = ofpbuf_put_zeros(b, sizeof *eth);
3977 eth->type = htons(OFPPDPT14_ETHERNET);
3978 eth->length = htons(sizeof *eth);
3979 eth->curr = netdev_port_features_to_ofp11(pp->curr);
3980 eth->advertised = netdev_port_features_to_ofp11(pp->advertised);
3981 eth->supported = netdev_port_features_to_ofp11(pp->supported);
3982 eth->peer = netdev_port_features_to_ofp11(pp->peer);
3983 eth->curr_speed = htonl(pp->curr_speed);
3984 eth->max_speed = htonl(pp->max_speed);
3988 ofputil_put_phy_port(enum ofp_version ofp_version,
3989 const struct ofputil_phy_port *pp, struct ofpbuf *b)
3991 switch (ofp_version) {
3992 case OFP10_VERSION: {
3993 struct ofp10_phy_port *opp = ofpbuf_put_uninit(b, sizeof *opp);
3994 ofputil_encode_ofp10_phy_port(pp, opp);
4000 case OFP13_VERSION: {
4001 struct ofp11_port *op = ofpbuf_put_uninit(b, sizeof *op);
4002 ofputil_encode_ofp11_port(pp, op);
4008 ofputil_put_ofp14_port(pp, b);
4017 ofputil_decode_port_desc_stats_request(const struct ofp_header *request,
4023 ofpbuf_use_const(&b, request, ntohs(request->length));
4024 raw = ofpraw_pull_assert(&b);
4025 if (raw == OFPRAW_OFPST10_PORT_DESC_REQUEST) {
4028 } else if (raw == OFPRAW_OFPST15_PORT_DESC_REQUEST) {
4029 ovs_be32 *ofp11_port;
4031 ofp11_port = ofpbuf_pull(&b, sizeof *ofp11_port);
4032 return ofputil_port_from_ofp11(*ofp11_port, port);
4039 ofputil_encode_port_desc_stats_request(enum ofp_version ofp_version,
4042 struct ofpbuf *request;
4044 switch (ofp_version) {
4050 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_DESC_REQUEST,
4053 case OFP15_VERSION:{
4054 struct ofp15_port_desc_request *req;
4055 request = ofpraw_alloc(OFPRAW_OFPST15_PORT_DESC_REQUEST,
4057 req = ofpbuf_put_zeros(request, sizeof *req);
4058 req->port_no = ofputil_port_to_ofp11(port);
4069 ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port *pp,
4070 struct ovs_list *replies)
4072 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
4073 size_t start_ofs = reply->size;
4075 ofputil_put_phy_port(ofpmp_version(replies), pp, reply);
4076 ofpmp_postappend(replies, start_ofs);
4079 /* ofputil_switch_config */
4081 /* Decodes 'oh', which must be an OFPT_GET_CONFIG_REPLY or OFPT_SET_CONFIG
4082 * message, into 'config'. Returns false if 'oh' contained any flags that
4083 * aren't specified in its version of OpenFlow, true otherwise. */
4085 ofputil_decode_switch_config(const struct ofp_header *oh,
4086 struct ofputil_switch_config *config)
4088 const struct ofp_switch_config *osc;
4091 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4092 ofpraw_pull_assert(&b);
4093 osc = ofpbuf_pull(&b, sizeof *osc);
4095 config->frag = ntohs(osc->flags) & OFPC_FRAG_MASK;
4096 config->miss_send_len = ntohs(osc->miss_send_len);
4098 ovs_be16 valid_mask = htons(OFPC_FRAG_MASK);
4099 if (oh->version < OFP13_VERSION) {
4100 const ovs_be16 ttl_bit = htons(OFPC_INVALID_TTL_TO_CONTROLLER);
4101 valid_mask |= ttl_bit;
4102 config->invalid_ttl_to_controller = (osc->flags & ttl_bit) != 0;
4104 config->invalid_ttl_to_controller = -1;
4107 return !(osc->flags & ~valid_mask);
4111 ofputil_decode_get_config_reply(const struct ofp_header *oh,
4112 struct ofputil_switch_config *config)
4114 ofputil_decode_switch_config(oh, config);
4118 ofputil_decode_set_config(const struct ofp_header *oh,
4119 struct ofputil_switch_config *config)
4121 return (ofputil_decode_switch_config(oh, config)
4123 : OFPERR_OFPSCFC_BAD_FLAGS);
4126 static struct ofpbuf *
4127 ofputil_put_switch_config(const struct ofputil_switch_config *config,
4130 const struct ofp_header *oh = b->data;
4131 struct ofp_switch_config *osc = ofpbuf_put_zeros(b, sizeof *osc);
4132 osc->flags = htons(config->frag);
4133 if (config->invalid_ttl_to_controller > 0 && oh->version < OFP13_VERSION) {
4134 osc->flags |= htons(OFPC_INVALID_TTL_TO_CONTROLLER);
4136 osc->miss_send_len = htons(config->miss_send_len);
4141 ofputil_encode_get_config_reply(const struct ofp_header *request,
4142 const struct ofputil_switch_config *config)
4144 struct ofpbuf *b = ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY,
4146 return ofputil_put_switch_config(config, b);
4150 ofputil_encode_set_config(const struct ofputil_switch_config *config,
4151 enum ofp_version version)
4153 struct ofpbuf *b = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, version, 0);
4154 return ofputil_put_switch_config(config, b);
4157 /* ofputil_switch_features */
4159 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
4160 OFPC_IP_REASM | OFPC_QUEUE_STATS)
4161 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
4162 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
4163 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
4164 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
4165 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
4166 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
4169 ofputil_capabilities_mask(enum ofp_version ofp_version)
4171 /* Handle capabilities whose bit is unique for all OpenFlow versions */
4172 switch (ofp_version) {
4175 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
4180 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
4182 /* Caller needs to check osf->header.version itself */
4187 /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an
4188 * abstract representation in '*features'. Initializes '*b' to iterate over
4189 * the OpenFlow port structures following 'osf' with later calls to
4190 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an
4191 * OFPERR_* value. */
4193 ofputil_decode_switch_features(const struct ofp_header *oh,
4194 struct ofputil_switch_features *features,
4197 const struct ofp_switch_features *osf;
4200 ofpbuf_use_const(b, oh, ntohs(oh->length));
4201 raw = ofpraw_pull_assert(b);
4203 osf = ofpbuf_pull(b, sizeof *osf);
4204 features->datapath_id = ntohll(osf->datapath_id);
4205 features->n_buffers = ntohl(osf->n_buffers);
4206 features->n_tables = osf->n_tables;
4207 features->auxiliary_id = 0;
4209 features->capabilities = ntohl(osf->capabilities) &
4210 ofputil_capabilities_mask(oh->version);
4212 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
4213 if (osf->capabilities & htonl(OFPC10_STP)) {
4214 features->capabilities |= OFPUTIL_C_STP;
4216 features->ofpacts = ofpact_bitmap_from_openflow(osf->actions,
4218 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY
4219 || raw == OFPRAW_OFPT13_FEATURES_REPLY) {
4220 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
4221 features->capabilities |= OFPUTIL_C_GROUP_STATS;
4223 features->ofpacts = 0;
4224 if (raw == OFPRAW_OFPT13_FEATURES_REPLY) {
4225 features->auxiliary_id = osf->auxiliary_id;
4228 return OFPERR_OFPBRC_BAD_VERSION;
4234 /* In OpenFlow 1.0, 1.1, and 1.2, an OFPT_FEATURES_REPLY message lists all the
4235 * switch's ports, unless there are too many to fit. In OpenFlow 1.3 and
4236 * later, an OFPT_FEATURES_REPLY does not list ports at all.
4238 * Given a buffer 'b' that contains a Features Reply message, this message
4239 * checks if it contains a complete list of the switch's ports. Returns true,
4240 * if so. Returns false if the list is missing (OF1.3+) or incomplete
4241 * (OF1.0/1.1/1.2), and in the latter case removes all of the ports from the
4244 * When this function returns false, the caller should send an OFPST_PORT_DESC
4245 * stats request to get the ports. */
4247 ofputil_switch_features_has_ports(struct ofpbuf *b)
4249 struct ofp_header *oh = b->data;
4250 size_t phy_port_size;
4252 if (oh->version >= OFP13_VERSION) {
4253 /* OpenFlow 1.3+ never has ports in the feature reply. */
4257 phy_port_size = (oh->version == OFP10_VERSION
4258 ? sizeof(struct ofp10_phy_port)
4259 : sizeof(struct ofp11_port));
4260 if (ntohs(oh->length) + phy_port_size <= UINT16_MAX) {
4261 /* There's room for additional ports in the feature reply.
4262 * Assume that the list is complete. */
4266 /* The feature reply has no room for more ports. Probably the list is
4267 * truncated. Drop the ports and tell the caller to retrieve them with
4268 * OFPST_PORT_DESC. */
4269 b->size = sizeof *oh + sizeof(struct ofp_switch_features);
4270 ofpmsg_update_length(b);
4274 /* Returns a buffer owned by the caller that encodes 'features' in the format
4275 * required by 'protocol' with the given 'xid'. The caller should append port
4276 * information to the buffer with subsequent calls to
4277 * ofputil_put_switch_features_port(). */
4279 ofputil_encode_switch_features(const struct ofputil_switch_features *features,
4280 enum ofputil_protocol protocol, ovs_be32 xid)
4282 struct ofp_switch_features *osf;
4284 enum ofp_version version;
4287 version = ofputil_protocol_to_ofp_version(protocol);
4290 raw = OFPRAW_OFPT10_FEATURES_REPLY;
4294 raw = OFPRAW_OFPT11_FEATURES_REPLY;
4299 raw = OFPRAW_OFPT13_FEATURES_REPLY;
4304 b = ofpraw_alloc_xid(raw, version, xid, 0);
4305 osf = ofpbuf_put_zeros(b, sizeof *osf);
4306 osf->datapath_id = htonll(features->datapath_id);
4307 osf->n_buffers = htonl(features->n_buffers);
4308 osf->n_tables = features->n_tables;
4310 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
4311 osf->capabilities = htonl(features->capabilities &
4312 ofputil_capabilities_mask(version));
4315 if (features->capabilities & OFPUTIL_C_STP) {
4316 osf->capabilities |= htonl(OFPC10_STP);
4318 osf->actions = ofpact_bitmap_to_openflow(features->ofpacts,
4324 osf->auxiliary_id = features->auxiliary_id;
4328 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
4329 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
4339 /* Encodes 'pp' into the format required by the switch_features message already
4340 * in 'b', which should have been returned by ofputil_encode_switch_features(),
4341 * and appends the encoded version to 'b'. */
4343 ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
4346 const struct ofp_header *oh = b->data;
4348 if (oh->version < OFP13_VERSION) {
4349 /* Try adding a port description to the message, but drop it again if
4350 * the buffer overflows. (This possibility for overflow is why
4351 * OpenFlow 1.3+ moved port descriptions into a multipart message.) */
4352 size_t start_ofs = b->size;
4353 ofputil_put_phy_port(oh->version, pp, b);
4354 if (b->size > UINT16_MAX) {
4355 b->size = start_ofs;
4360 /* ofputil_port_status */
4362 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
4363 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
4365 ofputil_decode_port_status(const struct ofp_header *oh,
4366 struct ofputil_port_status *ps)
4368 const struct ofp_port_status *ops;
4372 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4373 ofpraw_pull_assert(&b);
4374 ops = ofpbuf_pull(&b, sizeof *ops);
4376 if (ops->reason != OFPPR_ADD &&
4377 ops->reason != OFPPR_DELETE &&
4378 ops->reason != OFPPR_MODIFY) {
4379 return OFPERR_NXBRC_BAD_REASON;
4381 ps->reason = ops->reason;
4383 retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
4384 ovs_assert(retval != EOF);
4388 /* Converts the abstract form of a "port status" message in '*ps' into an
4389 * OpenFlow message suitable for 'protocol', and returns that encoded form in
4390 * a buffer owned by the caller. */
4392 ofputil_encode_port_status(const struct ofputil_port_status *ps,
4393 enum ofputil_protocol protocol)
4395 struct ofp_port_status *ops;
4397 enum ofp_version version;
4400 version = ofputil_protocol_to_ofp_version(protocol);
4403 raw = OFPRAW_OFPT10_PORT_STATUS;
4409 raw = OFPRAW_OFPT11_PORT_STATUS;
4414 raw = OFPRAW_OFPT14_PORT_STATUS;
4421 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
4422 ops = ofpbuf_put_zeros(b, sizeof *ops);
4423 ops->reason = ps->reason;
4424 ofputil_put_phy_port(version, &ps->desc, b);
4425 ofpmsg_update_length(b);
4429 /* ofputil_port_mod */
4432 parse_port_mod_ethernet_property(struct ofpbuf *property,
4433 struct ofputil_port_mod *pm)
4435 struct ofp14_port_mod_prop_ethernet *eth = property->data;
4437 if (property->size != sizeof *eth) {
4438 return OFPERR_OFPBRC_BAD_LEN;
4441 pm->advertise = netdev_port_features_from_ofp11(eth->advertise);
4445 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
4446 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
4448 ofputil_decode_port_mod(const struct ofp_header *oh,
4449 struct ofputil_port_mod *pm, bool loose)
4454 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4455 raw = ofpraw_pull_assert(&b);
4457 if (raw == OFPRAW_OFPT10_PORT_MOD) {
4458 const struct ofp10_port_mod *opm = b.data;
4460 pm->port_no = u16_to_ofp(ntohs(opm->port_no));
4461 pm->hw_addr = opm->hw_addr;
4462 pm->config = ntohl(opm->config) & OFPPC10_ALL;
4463 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
4464 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
4465 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
4466 const struct ofp11_port_mod *opm = b.data;
4469 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
4474 pm->hw_addr = opm->hw_addr;
4475 pm->config = ntohl(opm->config) & OFPPC11_ALL;
4476 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
4477 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
4478 } else if (raw == OFPRAW_OFPT14_PORT_MOD) {
4479 const struct ofp14_port_mod *opm = ofpbuf_pull(&b, sizeof *opm);
4482 memset(pm, 0, sizeof *pm);
4484 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
4489 pm->hw_addr = opm->hw_addr;
4490 pm->config = ntohl(opm->config) & OFPPC11_ALL;
4491 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
4493 while (b.size > 0) {
4494 struct ofpbuf property;
4498 error = ofputil_pull_property(&b, &property, &type);
4504 case OFPPMPT14_ETHERNET:
4505 error = parse_port_mod_ethernet_property(&property, pm);
4509 log_property(loose, "unknown port_mod property %"PRIu16, type);
4512 } else if (type == OFPPMPT14_EXPERIMENTER) {
4513 error = OFPERR_OFPBPC_BAD_EXPERIMENTER;
4515 error = OFPERR_OFPBRC_BAD_TYPE;
4525 return OFPERR_OFPBRC_BAD_TYPE;
4528 pm->config &= pm->mask;
4532 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
4533 * message suitable for 'protocol', and returns that encoded form in a buffer
4534 * owned by the caller. */
4536 ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
4537 enum ofputil_protocol protocol)
4539 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
4542 switch (ofp_version) {
4543 case OFP10_VERSION: {
4544 struct ofp10_port_mod *opm;
4546 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
4547 opm = ofpbuf_put_zeros(b, sizeof *opm);
4548 opm->port_no = htons(ofp_to_u16(pm->port_no));
4549 opm->hw_addr = pm->hw_addr;
4550 opm->config = htonl(pm->config & OFPPC10_ALL);
4551 opm->mask = htonl(pm->mask & OFPPC10_ALL);
4552 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
4558 case OFP13_VERSION: {
4559 struct ofp11_port_mod *opm;
4561 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
4562 opm = ofpbuf_put_zeros(b, sizeof *opm);
4563 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
4564 opm->hw_addr = pm->hw_addr;
4565 opm->config = htonl(pm->config & OFPPC11_ALL);
4566 opm->mask = htonl(pm->mask & OFPPC11_ALL);
4567 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
4571 case OFP15_VERSION: {
4572 struct ofp14_port_mod_prop_ethernet *eth;
4573 struct ofp14_port_mod *opm;
4575 b = ofpraw_alloc(OFPRAW_OFPT14_PORT_MOD, ofp_version, sizeof *eth);
4576 opm = ofpbuf_put_zeros(b, sizeof *opm);
4577 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
4578 opm->hw_addr = pm->hw_addr;
4579 opm->config = htonl(pm->config & OFPPC11_ALL);
4580 opm->mask = htonl(pm->mask & OFPPC11_ALL);
4582 if (pm->advertise) {
4583 eth = ofpbuf_put_zeros(b, sizeof *eth);
4584 eth->type = htons(OFPPMPT14_ETHERNET);
4585 eth->length = htons(sizeof *eth);
4586 eth->advertise = netdev_port_features_to_ofp11(pm->advertise);
4597 /* Table features. */
4600 pull_table_feature_property(struct ofpbuf *msg, struct ofpbuf *payload,
4605 error = ofputil_pull_property(msg, payload, typep);
4606 if (payload && !error) {
4607 ofpbuf_pull(payload, sizeof(struct ofp_prop_header));
4613 parse_action_bitmap(struct ofpbuf *payload, enum ofp_version ofp_version,
4618 while (payload->size > 0) {
4622 error = ofputil_pull_property__(payload, NULL, 1, &type);
4626 if (type < CHAR_BIT * sizeof types) {
4627 types |= 1u << type;
4631 *ofpacts = ofpact_bitmap_from_openflow(htonl(types), ofp_version);
4636 parse_instruction_ids(struct ofpbuf *payload, bool loose, uint32_t *insts)
4639 while (payload->size > 0) {
4640 enum ovs_instruction_type inst;
4644 /* OF1.3 and OF1.4 aren't clear about padding in the instruction IDs.
4645 * It seems clear that they aren't padded to 8 bytes, though, because
4646 * both standards say that "non-experimenter instructions are 4 bytes"
4647 * and do not mention any padding before the first instruction ID.
4648 * (There wouldn't be any point in padding to 8 bytes if the IDs were
4649 * aligned on an odd 4-byte boundary.)
4651 * Anyway, we just assume they're all glommed together on byte
4653 error = ofputil_pull_property__(payload, NULL, 1, &ofpit);
4658 error = ovs_instruction_type_from_inst_type(&inst, ofpit);
4660 *insts |= 1u << inst;
4661 } else if (!loose) {
4669 parse_table_features_next_table(struct ofpbuf *payload,
4670 unsigned long int *next_tables)
4674 memset(next_tables, 0, bitmap_n_bytes(255));
4675 for (i = 0; i < payload->size; i++) {
4676 uint8_t id = ((const uint8_t *) payload->data)[i];
4678 return OFPERR_OFPBPC_BAD_VALUE;
4680 bitmap_set1(next_tables, id);
4686 parse_oxms(struct ofpbuf *payload, bool loose,
4687 struct mf_bitmap *exactp, struct mf_bitmap *maskedp)
4689 struct mf_bitmap exact = MF_BITMAP_INITIALIZER;
4690 struct mf_bitmap masked = MF_BITMAP_INITIALIZER;
4692 while (payload->size > 0) {
4693 const struct mf_field *field;
4697 error = nx_pull_header(payload, &field, &hasmask);
4699 bitmap_set1(hasmask ? masked.bm : exact.bm, field->id);
4700 } else if (error != OFPERR_OFPBMC_BAD_FIELD || !loose) {
4706 } else if (!bitmap_is_all_zeros(exact.bm, MFF_N_IDS)) {
4707 return OFPERR_OFPBMC_BAD_MASK;
4711 } else if (!bitmap_is_all_zeros(masked.bm, MFF_N_IDS)) {
4712 return OFPERR_OFPBMC_BAD_MASK;
4717 /* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract
4718 * ofputil_table_features in 'tf'.
4720 * If 'loose' is true, this function ignores properties and values that it does
4721 * not understand, as a controller would want to do when interpreting
4722 * capabilities provided by a switch. If 'loose' is false, this function
4723 * treats unknown properties and values as an error, as a switch would want to
4724 * do when interpreting a configuration request made by a controller.
4726 * A single OpenFlow message can specify features for multiple tables. Calling
4727 * this function multiple times for a single 'msg' iterates through the tables
4728 * in the message. The caller must initially leave 'msg''s layer pointers null
4729 * and not modify them between calls.
4731 * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise
4732 * a positive "enum ofperr" value. */
4734 ofputil_decode_table_features(struct ofpbuf *msg,
4735 struct ofputil_table_features *tf, bool loose)
4737 const struct ofp_header *oh;
4738 struct ofp13_table_features *otf;
4739 struct ofpbuf properties;
4742 memset(tf, 0, sizeof *tf);
4745 ofpraw_pull_assert(msg);
4753 if (msg->size < sizeof *otf) {
4754 return OFPERR_OFPBPC_BAD_LEN;
4758 len = ntohs(otf->length);
4759 if (len < sizeof *otf || len % 8 || len > msg->size) {
4760 return OFPERR_OFPBPC_BAD_LEN;
4762 ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len);
4763 ofpbuf_pull(&properties, sizeof *otf);
4765 tf->table_id = otf->table_id;
4766 if (tf->table_id == OFPTT_ALL) {
4767 return OFPERR_OFPTFFC_BAD_TABLE;
4770 ovs_strlcpy(tf->name, otf->name, OFP_MAX_TABLE_NAME_LEN);
4771 tf->metadata_match = otf->metadata_match;
4772 tf->metadata_write = otf->metadata_write;
4773 tf->miss_config = OFPUTIL_TABLE_MISS_DEFAULT;
4774 if (oh->version >= OFP14_VERSION) {
4775 uint32_t caps = ntohl(otf->capabilities);
4776 tf->supports_eviction = (caps & OFPTC14_EVICTION) != 0;
4777 tf->supports_vacancy_events = (caps & OFPTC14_VACANCY_EVENTS) != 0;
4779 tf->supports_eviction = -1;
4780 tf->supports_vacancy_events = -1;
4782 tf->max_entries = ntohl(otf->max_entries);
4784 while (properties.size > 0) {
4785 struct ofpbuf payload;
4789 error = pull_table_feature_property(&properties, &payload, &type);
4794 switch ((enum ofp13_table_feature_prop_type) type) {
4795 case OFPTFPT13_INSTRUCTIONS:
4796 error = parse_instruction_ids(&payload, loose,
4797 &tf->nonmiss.instructions);
4799 case OFPTFPT13_INSTRUCTIONS_MISS:
4800 error = parse_instruction_ids(&payload, loose,
4801 &tf->miss.instructions);
4804 case OFPTFPT13_NEXT_TABLES:
4805 error = parse_table_features_next_table(&payload,
4808 case OFPTFPT13_NEXT_TABLES_MISS:
4809 error = parse_table_features_next_table(&payload, tf->miss.next);
4812 case OFPTFPT13_WRITE_ACTIONS:
4813 error = parse_action_bitmap(&payload, oh->version,
4814 &tf->nonmiss.write.ofpacts);
4816 case OFPTFPT13_WRITE_ACTIONS_MISS:
4817 error = parse_action_bitmap(&payload, oh->version,
4818 &tf->miss.write.ofpacts);
4821 case OFPTFPT13_APPLY_ACTIONS:
4822 error = parse_action_bitmap(&payload, oh->version,
4823 &tf->nonmiss.apply.ofpacts);
4825 case OFPTFPT13_APPLY_ACTIONS_MISS:
4826 error = parse_action_bitmap(&payload, oh->version,
4827 &tf->miss.apply.ofpacts);
4830 case OFPTFPT13_MATCH:
4831 error = parse_oxms(&payload, loose, &tf->match, &tf->mask);
4833 case OFPTFPT13_WILDCARDS:
4834 error = parse_oxms(&payload, loose, &tf->wildcard, NULL);
4837 case OFPTFPT13_WRITE_SETFIELD:
4838 error = parse_oxms(&payload, loose,
4839 &tf->nonmiss.write.set_fields, NULL);
4841 case OFPTFPT13_WRITE_SETFIELD_MISS:
4842 error = parse_oxms(&payload, loose,
4843 &tf->miss.write.set_fields, NULL);
4845 case OFPTFPT13_APPLY_SETFIELD:
4846 error = parse_oxms(&payload, loose,
4847 &tf->nonmiss.apply.set_fields, NULL);
4849 case OFPTFPT13_APPLY_SETFIELD_MISS:
4850 error = parse_oxms(&payload, loose,
4851 &tf->miss.apply.set_fields, NULL);
4854 case OFPTFPT13_EXPERIMENTER:
4855 case OFPTFPT13_EXPERIMENTER_MISS:
4857 log_property(loose, "unknown table features property %"PRIu16,
4859 error = loose ? 0 : OFPERR_OFPBPC_BAD_TYPE;
4867 /* Fix inconsistencies:
4869 * - Turn on 'match' bits that are set in 'mask', because maskable
4870 * fields are matchable.
4872 * - Turn on 'wildcard' bits that are set in 'mask', because a field
4873 * that is arbitrarily maskable can be wildcarded entirely.
4875 * - Turn off 'wildcard' bits that are not in 'match', because a field
4876 * must be matchable for it to be meaningfully wildcarded. */
4877 bitmap_or(tf->match.bm, tf->mask.bm, MFF_N_IDS);
4878 bitmap_or(tf->wildcard.bm, tf->mask.bm, MFF_N_IDS);
4879 bitmap_and(tf->wildcard.bm, tf->match.bm, MFF_N_IDS);
4884 /* Encodes and returns a request to obtain the table features of a switch.
4885 * The message is encoded for OpenFlow version 'ofp_version'. */
4887 ofputil_encode_table_features_request(enum ofp_version ofp_version)
4889 struct ofpbuf *request = NULL;
4891 switch (ofp_version) {
4895 ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later "
4896 "(\'-O OpenFlow13\')");
4900 request = ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST,
4911 put_fields_property(struct ofpbuf *reply,
4912 const struct mf_bitmap *fields,
4913 const struct mf_bitmap *masks,
4914 enum ofp13_table_feature_prop_type property,
4915 enum ofp_version version)
4920 start_ofs = start_property(reply, property);
4921 BITMAP_FOR_EACH_1 (field, MFF_N_IDS, fields->bm) {
4922 nx_put_header(reply, field, version,
4923 masks && bitmap_is_set(masks->bm, field));
4925 end_property(reply, start_ofs);
4929 put_table_action_features(struct ofpbuf *reply,
4930 const struct ofputil_table_action_features *taf,
4931 enum ofp13_table_feature_prop_type actions_type,
4932 enum ofp13_table_feature_prop_type set_fields_type,
4933 int miss_offset, enum ofp_version version)
4937 start_ofs = start_property(reply, actions_type + miss_offset);
4938 put_bitmap_properties(reply,
4939 ntohl(ofpact_bitmap_to_openflow(taf->ofpacts,
4941 end_property(reply, start_ofs);
4943 put_fields_property(reply, &taf->set_fields, NULL,
4944 set_fields_type + miss_offset, version);
4948 put_table_instruction_features(
4949 struct ofpbuf *reply, const struct ofputil_table_instruction_features *tif,
4950 int miss_offset, enum ofp_version version)
4955 start_ofs = start_property(reply, OFPTFPT13_INSTRUCTIONS + miss_offset);
4956 put_bitmap_properties(reply,
4957 ntohl(ovsinst_bitmap_to_openflow(tif->instructions,
4959 end_property(reply, start_ofs);
4961 start_ofs = start_property(reply, OFPTFPT13_NEXT_TABLES + miss_offset);
4962 BITMAP_FOR_EACH_1 (table_id, 255, tif->next) {
4963 ofpbuf_put(reply, &table_id, 1);
4965 end_property(reply, start_ofs);
4967 put_table_action_features(reply, &tif->write,
4968 OFPTFPT13_WRITE_ACTIONS,
4969 OFPTFPT13_WRITE_SETFIELD, miss_offset, version);
4970 put_table_action_features(reply, &tif->apply,
4971 OFPTFPT13_APPLY_ACTIONS,
4972 OFPTFPT13_APPLY_SETFIELD, miss_offset, version);
4976 ofputil_append_table_features_reply(const struct ofputil_table_features *tf,
4977 struct ovs_list *replies)
4979 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
4980 enum ofp_version version = ofpmp_version(replies);
4981 size_t start_ofs = reply->size;
4982 struct ofp13_table_features *otf;
4984 otf = ofpbuf_put_zeros(reply, sizeof *otf);
4985 otf->table_id = tf->table_id;
4986 ovs_strlcpy(otf->name, tf->name, sizeof otf->name);
4987 otf->metadata_match = tf->metadata_match;
4988 otf->metadata_write = tf->metadata_write;
4989 if (version >= OFP14_VERSION) {
4990 if (tf->supports_eviction) {
4991 otf->capabilities |= htonl(OFPTC14_EVICTION);
4993 if (tf->supports_vacancy_events) {
4994 otf->capabilities |= htonl(OFPTC14_VACANCY_EVENTS);
4997 otf->max_entries = htonl(tf->max_entries);
4999 put_table_instruction_features(reply, &tf->nonmiss, 0, version);
5000 put_table_instruction_features(reply, &tf->miss, 1, version);
5002 put_fields_property(reply, &tf->match, &tf->mask,
5003 OFPTFPT13_MATCH, version);
5004 put_fields_property(reply, &tf->wildcard, NULL,
5005 OFPTFPT13_WILDCARDS, version);
5007 otf = ofpbuf_at_assert(reply, start_ofs, sizeof *otf);
5008 otf->length = htons(reply->size - start_ofs);
5009 ofpmp_postappend(replies, start_ofs);
5013 parse_table_desc_eviction_property(struct ofpbuf *property,
5014 struct ofputil_table_desc *td)
5016 struct ofp14_table_mod_prop_eviction *ote = property->data;
5018 if (property->size != sizeof *ote) {
5019 return OFPERR_OFPBPC_BAD_LEN;
5022 td->eviction_flags = ntohl(ote->flags);
5027 parse_table_desc_vacancy_property(struct ofpbuf *property,
5028 struct ofputil_table_desc *td)
5030 struct ofp14_table_mod_prop_vacancy *otv = property->data;
5032 if (property->size != sizeof *otv) {
5033 return OFPERR_OFPBPC_BAD_LEN;
5036 td->table_vacancy.vacancy_down = otv->vacancy_down;
5037 td->table_vacancy.vacancy_up = otv->vacancy_up;
5038 td->table_vacancy.vacancy = otv->vacancy;
5042 /* Decodes the next OpenFlow "table desc" message (of possibly several) from
5043 * 'msg' into an abstract form in '*td'. Returns 0 if successful, EOF if the
5044 * last "table desc" in 'msg' was already decoded, otherwise an OFPERR_*
5047 ofputil_decode_table_desc(struct ofpbuf *msg,
5048 struct ofputil_table_desc *td,
5049 enum ofp_version version)
5051 struct ofp14_table_desc *otd;
5052 struct ofpbuf properties;
5055 memset(td, 0, sizeof *td);
5058 ofpraw_pull_assert(msg);
5065 otd = ofpbuf_try_pull(msg, sizeof *otd);
5067 VLOG_WARN_RL(&bad_ofmsg_rl, "OFP14_TABLE_DESC reply has %"PRIu32" "
5068 "leftover bytes at end", msg->size);
5069 return OFPERR_OFPBRC_BAD_LEN;
5072 td->table_id = otd->table_id;
5073 length = ntohs(otd->length);
5074 if (length < sizeof *otd || length - sizeof *otd > msg->size) {
5075 VLOG_WARN_RL(&bad_ofmsg_rl, "OFP14_TABLE_DESC reply claims invalid "
5076 "length %"PRIuSIZE, length);
5077 return OFPERR_OFPBRC_BAD_LEN;
5079 length -= sizeof *otd;
5080 ofpbuf_use_const(&properties, ofpbuf_pull(msg, length), length);
5082 td->eviction = ofputil_decode_table_eviction(otd->config, version);
5083 td->vacancy = ofputil_decode_table_vacancy(otd->config, version);
5084 td->eviction_flags = UINT32_MAX;
5086 while (properties.size > 0) {
5087 struct ofpbuf payload;
5091 error = ofputil_pull_property(&properties, &payload, &type);
5097 case OFPTMPT14_EVICTION:
5098 error = parse_table_desc_eviction_property(&payload, td);
5101 case OFPTMPT14_VACANCY:
5102 error = parse_table_desc_vacancy_property(&payload, td);
5106 log_property(true, "unknown table_desc property %"PRIu16, type);
5119 /* Encodes and returns a request to obtain description of tables of a switch.
5120 * The message is encoded for OpenFlow version 'ofp_version'. */
5122 ofputil_encode_table_desc_request(enum ofp_version ofp_version)
5124 struct ofpbuf *request = NULL;
5126 if (ofp_version >= OFP14_VERSION) {
5127 request = ofpraw_alloc(OFPRAW_OFPST14_TABLE_DESC_REQUEST,
5130 ovs_fatal(0, "dump-table-desc needs OpenFlow 1.4 or later "
5131 "(\'-O OpenFlow14\')");
5137 /* Function to append Table desc information in a reply list. */
5139 ofputil_append_table_desc_reply(const struct ofputil_table_desc *td,
5140 struct ovs_list *replies,
5141 enum ofp_version version)
5143 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
5145 struct ofp14_table_desc *otd;
5147 start_otd = reply->size;
5148 ofpbuf_put_zeros(reply, sizeof *otd);
5149 if (td->eviction_flags != UINT32_MAX) {
5150 struct ofp14_table_mod_prop_eviction *ote;
5152 ote = ofpbuf_put_zeros(reply, sizeof *ote);
5153 ote->type = htons(OFPTMPT14_EVICTION);
5154 ote->length = htons(sizeof *ote);
5155 ote->flags = htonl(td->eviction_flags);
5157 if (td->vacancy == OFPUTIL_TABLE_VACANCY_ON) {
5158 struct ofp14_table_mod_prop_vacancy *otv;
5160 otv = ofpbuf_put_zeros(reply, sizeof *otv);
5161 otv->type = htons(OFPTMPT14_VACANCY);
5162 otv->length = htons(sizeof *otv);
5163 otv->vacancy_down = td->table_vacancy.vacancy_down;
5164 otv->vacancy_up = td->table_vacancy.vacancy_up;
5165 otv->vacancy = td->table_vacancy.vacancy;
5168 otd = ofpbuf_at_assert(reply, start_otd, sizeof *otd);
5169 otd->length = htons(reply->size - start_otd);
5170 otd->table_id = td->table_id;
5171 otd->config = ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT,
5172 td->eviction, td->vacancy,
5174 ofpmp_postappend(replies, start_otd);
5177 /* This function parses Vacancy property, and decodes the
5178 * ofp14_table_mod_prop_vacancy in ofputil_table_mod.
5179 * Returns OFPERR_OFPBPC_BAD_VALUE error code when vacancy_down is
5180 * greater than vacancy_up and also when current vacancy has non-zero
5181 * value. Returns 0 on success. */
5183 parse_table_mod_vacancy_property(struct ofpbuf *property,
5184 struct ofputil_table_mod *tm)
5186 struct ofp14_table_mod_prop_vacancy *otv = property->data;
5188 if (property->size != sizeof *otv) {
5189 return OFPERR_OFPBPC_BAD_LEN;
5191 tm->table_vacancy.vacancy_down = otv->vacancy_down;
5192 tm->table_vacancy.vacancy_up = otv->vacancy_up;
5193 if (tm->table_vacancy.vacancy_down > tm->table_vacancy.vacancy_up) {
5194 log_property(false, "Value of vacancy_down is greater than "
5196 return OFPERR_OFPBPC_BAD_VALUE;
5198 if (tm->table_vacancy.vacancy_down > 100 ||
5199 tm->table_vacancy.vacancy_up > 100) {
5200 log_property(false, "Vacancy threshold percentage should not be"
5201 "greater than 100");
5202 return OFPERR_OFPBPC_BAD_VALUE;
5204 tm->table_vacancy.vacancy = otv->vacancy;
5205 if (tm->table_vacancy.vacancy) {
5206 log_property(false, "Vacancy value should be zero for table-mod "
5208 return OFPERR_OFPBPC_BAD_VALUE;
5213 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5214 * table configuration (a table mod, table stats, or table features message),
5215 * returns the table vacancy configuration that it specifies.
5217 * Only OpenFlow 1.4 and later specify table vacancy configuration this way,
5218 * so for other 'version' this function always returns
5219 * OFPUTIL_TABLE_VACANCY_DEFAULT. */
5220 static enum ofputil_table_vacancy
5221 ofputil_decode_table_vacancy(ovs_be32 config, enum ofp_version version)
5223 return (version < OFP14_VERSION ? OFPUTIL_TABLE_VACANCY_DEFAULT
5224 : config & htonl(OFPTC14_VACANCY_EVENTS) ? OFPUTIL_TABLE_VACANCY_ON
5225 : OFPUTIL_TABLE_VACANCY_OFF);
5229 parse_table_mod_eviction_property(struct ofpbuf *property,
5230 struct ofputil_table_mod *tm)
5232 struct ofp14_table_mod_prop_eviction *ote = property->data;
5234 if (property->size != sizeof *ote) {
5235 return OFPERR_OFPBPC_BAD_LEN;
5238 tm->eviction_flags = ntohl(ote->flags);
5242 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5243 * table configuration (a table mod, table stats, or table features message),
5244 * returns the table eviction configuration that it specifies.
5246 * Only OpenFlow 1.4 and later specify table eviction configuration this way,
5247 * so for other 'version' values this function always returns
5248 * OFPUTIL_TABLE_EVICTION_DEFAULT. */
5249 static enum ofputil_table_eviction
5250 ofputil_decode_table_eviction(ovs_be32 config, enum ofp_version version)
5252 return (version < OFP14_VERSION ? OFPUTIL_TABLE_EVICTION_DEFAULT
5253 : config & htonl(OFPTC14_EVICTION) ? OFPUTIL_TABLE_EVICTION_ON
5254 : OFPUTIL_TABLE_EVICTION_OFF);
5257 /* Returns a bitmap of OFPTC* values suitable for 'config' fields in various
5258 * OpenFlow messages of the given 'version', based on the provided 'miss' and
5259 * 'eviction' values. */
5261 ofputil_encode_table_config(enum ofputil_table_miss miss,
5262 enum ofputil_table_eviction eviction,
5263 enum ofputil_table_vacancy vacancy,
5264 enum ofp_version version)
5266 uint32_t config = 0;
5267 /* See the section "OFPTC_* Table Configuration" in DESIGN.md for more
5268 * information on the crazy evolution of this field. */
5271 /* OpenFlow 1.0 didn't have such a field, any value ought to do. */
5276 /* OpenFlow 1.1 and 1.2 define only OFPTC11_TABLE_MISS_*. */
5278 case OFPUTIL_TABLE_MISS_DEFAULT:
5279 /* Really this shouldn't be used for encoding (the caller should
5280 * provide a specific value) but I can't imagine that defaulting to
5281 * the fall-through case here will hurt. */
5282 case OFPUTIL_TABLE_MISS_CONTROLLER:
5284 return htonl(OFPTC11_TABLE_MISS_CONTROLLER);
5285 case OFPUTIL_TABLE_MISS_CONTINUE:
5286 return htonl(OFPTC11_TABLE_MISS_CONTINUE);
5287 case OFPUTIL_TABLE_MISS_DROP:
5288 return htonl(OFPTC11_TABLE_MISS_DROP);
5293 /* OpenFlow 1.3 removed OFPTC11_TABLE_MISS_* and didn't define any new
5294 * flags, so this is correct. */
5299 /* OpenFlow 1.4 introduced OFPTC14_EVICTION and
5300 * OFPTC14_VACANCY_EVENTS. */
5301 if (eviction == OFPUTIL_TABLE_EVICTION_ON) {
5302 config |= OFPTC14_EVICTION;
5304 if (vacancy == OFPUTIL_TABLE_VACANCY_ON) {
5305 config |= OFPTC14_VACANCY_EVENTS;
5307 return htonl(config);
5313 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5314 * table configuration (a table mod, table stats, or table features message),
5315 * returns the table miss configuration that it specifies.
5317 * Only OpenFlow 1.1 and 1.2 specify table miss configurations this way, so for
5318 * other 'version' values this function always returns
5319 * OFPUTIL_TABLE_MISS_DEFAULT. */
5320 static enum ofputil_table_miss
5321 ofputil_decode_table_miss(ovs_be32 config_, enum ofp_version version)
5323 uint32_t config = ntohl(config_);
5325 if (version == OFP11_VERSION || version == OFP12_VERSION) {
5326 switch (config & OFPTC11_TABLE_MISS_MASK) {
5327 case OFPTC11_TABLE_MISS_CONTROLLER:
5328 return OFPUTIL_TABLE_MISS_CONTROLLER;
5330 case OFPTC11_TABLE_MISS_CONTINUE:
5331 return OFPUTIL_TABLE_MISS_CONTINUE;
5333 case OFPTC11_TABLE_MISS_DROP:
5334 return OFPUTIL_TABLE_MISS_DROP;
5337 VLOG_WARN_RL(&bad_ofmsg_rl, "bad table miss config %d", config);
5338 return OFPUTIL_TABLE_MISS_CONTROLLER;
5341 return OFPUTIL_TABLE_MISS_DEFAULT;
5345 /* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in
5346 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
5348 ofputil_decode_table_mod(const struct ofp_header *oh,
5349 struct ofputil_table_mod *pm)
5354 memset(pm, 0, sizeof *pm);
5355 pm->miss = OFPUTIL_TABLE_MISS_DEFAULT;
5356 pm->eviction = OFPUTIL_TABLE_EVICTION_DEFAULT;
5357 pm->eviction_flags = UINT32_MAX;
5358 pm->vacancy = OFPUTIL_TABLE_VACANCY_DEFAULT;
5359 ofpbuf_use_const(&b, oh, ntohs(oh->length));
5360 raw = ofpraw_pull_assert(&b);
5362 if (raw == OFPRAW_OFPT11_TABLE_MOD) {
5363 const struct ofp11_table_mod *otm = b.data;
5365 pm->table_id = otm->table_id;
5366 pm->miss = ofputil_decode_table_miss(otm->config, oh->version);
5367 } else if (raw == OFPRAW_OFPT14_TABLE_MOD) {
5368 const struct ofp14_table_mod *otm = ofpbuf_pull(&b, sizeof *otm);
5370 pm->table_id = otm->table_id;
5371 pm->miss = ofputil_decode_table_miss(otm->config, oh->version);
5372 pm->eviction = ofputil_decode_table_eviction(otm->config, oh->version);
5373 pm->vacancy = ofputil_decode_table_vacancy(otm->config, oh->version);
5374 while (b.size > 0) {
5375 struct ofpbuf property;
5379 error = ofputil_pull_property(&b, &property, &type);
5385 case OFPTMPT14_EVICTION:
5386 error = parse_table_mod_eviction_property(&property, pm);
5389 case OFPTMPT14_VACANCY:
5390 error = parse_table_mod_vacancy_property(&property, pm);
5394 error = OFPERR_OFPBRC_BAD_TYPE;
5403 return OFPERR_OFPBRC_BAD_TYPE;
5409 /* Converts the abstract form of a "table mod" message in '*tm' into an
5410 * OpenFlow message suitable for 'protocol', and returns that encoded form in a
5411 * buffer owned by the caller. */
5413 ofputil_encode_table_mod(const struct ofputil_table_mod *tm,
5414 enum ofputil_protocol protocol)
5416 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
5419 switch (ofp_version) {
5420 case OFP10_VERSION: {
5421 ovs_fatal(0, "table mod needs OpenFlow 1.1 or later "
5422 "(\'-O OpenFlow11\')");
5427 case OFP13_VERSION: {
5428 struct ofp11_table_mod *otm;
5430 b = ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD, ofp_version, 0);
5431 otm = ofpbuf_put_zeros(b, sizeof *otm);
5432 otm->table_id = tm->table_id;
5433 otm->config = ofputil_encode_table_config(tm->miss, tm->eviction,
5434 tm->vacancy, ofp_version);
5438 case OFP15_VERSION: {
5439 struct ofp14_table_mod *otm;
5440 struct ofp14_table_mod_prop_eviction *ote;
5441 struct ofp14_table_mod_prop_vacancy *otv;
5443 b = ofpraw_alloc(OFPRAW_OFPT14_TABLE_MOD, ofp_version, 0);
5444 otm = ofpbuf_put_zeros(b, sizeof *otm);
5445 otm->table_id = tm->table_id;
5446 otm->config = ofputil_encode_table_config(tm->miss, tm->eviction,
5447 tm->vacancy, ofp_version);
5449 if (tm->eviction_flags != UINT32_MAX) {
5450 ote = ofpbuf_put_zeros(b, sizeof *ote);
5451 ote->type = htons(OFPTMPT14_EVICTION);
5452 ote->length = htons(sizeof *ote);
5453 ote->flags = htonl(tm->eviction_flags);
5455 if (tm->vacancy == OFPUTIL_TABLE_VACANCY_ON) {
5456 otv = ofpbuf_put_zeros(b, sizeof *otv);
5457 otv->type = htons(OFPTMPT14_VACANCY);
5458 otv->length = htons(sizeof *otv);
5459 otv->vacancy_down = tm->table_vacancy.vacancy_down;
5460 otv->vacancy_up = tm->table_vacancy.vacancy_up;
5471 /* ofputil_role_request */
5473 /* Decodes the OpenFlow "role request" or "role reply" message in '*oh' into
5474 * an abstract form in '*rr'. Returns 0 if successful, otherwise an
5475 * OFPERR_* value. */
5477 ofputil_decode_role_message(const struct ofp_header *oh,
5478 struct ofputil_role_request *rr)
5483 ofpbuf_use_const(&b, oh, ntohs(oh->length));
5484 raw = ofpraw_pull_assert(&b);
5486 if (raw == OFPRAW_OFPT12_ROLE_REQUEST ||
5487 raw == OFPRAW_OFPT12_ROLE_REPLY) {
5488 const struct ofp12_role_request *orr = b.msg;
5490 if (orr->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
5491 orr->role != htonl(OFPCR12_ROLE_EQUAL) &&
5492 orr->role != htonl(OFPCR12_ROLE_MASTER) &&
5493 orr->role != htonl(OFPCR12_ROLE_SLAVE)) {
5494 return OFPERR_OFPRRFC_BAD_ROLE;
5497 rr->role = ntohl(orr->role);
5498 if (raw == OFPRAW_OFPT12_ROLE_REQUEST
5499 ? orr->role == htonl(OFPCR12_ROLE_NOCHANGE)
5500 : orr->generation_id == OVS_BE64_MAX) {
5501 rr->have_generation_id = false;
5502 rr->generation_id = 0;
5504 rr->have_generation_id = true;
5505 rr->generation_id = ntohll(orr->generation_id);
5507 } else if (raw == OFPRAW_NXT_ROLE_REQUEST ||
5508 raw == OFPRAW_NXT_ROLE_REPLY) {
5509 const struct nx_role_request *nrr = b.msg;
5511 BUILD_ASSERT(NX_ROLE_OTHER + 1 == OFPCR12_ROLE_EQUAL);
5512 BUILD_ASSERT(NX_ROLE_MASTER + 1 == OFPCR12_ROLE_MASTER);
5513 BUILD_ASSERT(NX_ROLE_SLAVE + 1 == OFPCR12_ROLE_SLAVE);
5515 if (nrr->role != htonl(NX_ROLE_OTHER) &&
5516 nrr->role != htonl(NX_ROLE_MASTER) &&
5517 nrr->role != htonl(NX_ROLE_SLAVE)) {
5518 return OFPERR_OFPRRFC_BAD_ROLE;
5521 rr->role = ntohl(nrr->role) + 1;
5522 rr->have_generation_id = false;
5523 rr->generation_id = 0;
5531 /* Returns an encoded form of a role reply suitable for the "request" in a
5532 * buffer owned by the caller. */
5534 ofputil_encode_role_reply(const struct ofp_header *request,
5535 const struct ofputil_role_request *rr)
5540 raw = ofpraw_decode_assert(request);
5541 if (raw == OFPRAW_OFPT12_ROLE_REQUEST) {
5542 struct ofp12_role_request *orr;
5544 buf = ofpraw_alloc_reply(OFPRAW_OFPT12_ROLE_REPLY, request, 0);
5545 orr = ofpbuf_put_zeros(buf, sizeof *orr);
5547 orr->role = htonl(rr->role);
5548 orr->generation_id = htonll(rr->have_generation_id
5551 } else if (raw == OFPRAW_NXT_ROLE_REQUEST) {
5552 struct nx_role_request *nrr;
5554 BUILD_ASSERT(NX_ROLE_OTHER == OFPCR12_ROLE_EQUAL - 1);
5555 BUILD_ASSERT(NX_ROLE_MASTER == OFPCR12_ROLE_MASTER - 1);
5556 BUILD_ASSERT(NX_ROLE_SLAVE == OFPCR12_ROLE_SLAVE - 1);
5558 buf = ofpraw_alloc_reply(OFPRAW_NXT_ROLE_REPLY, request, 0);
5559 nrr = ofpbuf_put_zeros(buf, sizeof *nrr);
5560 nrr->role = htonl(rr->role - 1);
5568 /* Encodes "role status" message 'status' for sending in the given
5569 * 'protocol'. Returns the role status message, if 'protocol' supports them,
5570 * otherwise a null pointer. */
5572 ofputil_encode_role_status(const struct ofputil_role_status *status,
5573 enum ofputil_protocol protocol)
5575 enum ofp_version version;
5577 version = ofputil_protocol_to_ofp_version(protocol);
5578 if (version >= OFP14_VERSION) {
5579 struct ofp14_role_status *rstatus;
5582 buf = ofpraw_alloc_xid(OFPRAW_OFPT14_ROLE_STATUS, version, htonl(0),
5584 rstatus = ofpbuf_put_zeros(buf, sizeof *rstatus);
5585 rstatus->role = htonl(status->role);
5586 rstatus->reason = status->reason;
5587 rstatus->generation_id = htonll(status->generation_id);
5596 ofputil_decode_role_status(const struct ofp_header *oh,
5597 struct ofputil_role_status *rs)
5601 const struct ofp14_role_status *r;
5603 ofpbuf_use_const(&b, oh, ntohs(oh->length));
5604 raw = ofpraw_pull_assert(&b);
5605 ovs_assert(raw == OFPRAW_OFPT14_ROLE_STATUS);
5608 if (r->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
5609 r->role != htonl(OFPCR12_ROLE_EQUAL) &&
5610 r->role != htonl(OFPCR12_ROLE_MASTER) &&
5611 r->role != htonl(OFPCR12_ROLE_SLAVE)) {
5612 return OFPERR_OFPRRFC_BAD_ROLE;
5615 rs->role = ntohl(r->role);
5616 rs->generation_id = ntohll(r->generation_id);
5617 rs->reason = r->reason;
5622 /* Encodes 'rf' according to 'protocol', and returns the encoded message.
5623 * 'protocol' must be for OpenFlow 1.4 or later. */
5625 ofputil_encode_requestforward(const struct ofputil_requestforward *rf,
5626 enum ofputil_protocol protocol)
5628 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
5629 struct ofpbuf *inner;
5631 switch (rf->reason) {
5632 case OFPRFR_GROUP_MOD:
5633 inner = ofputil_encode_group_mod(ofp_version, rf->group_mod);
5636 case OFPRFR_METER_MOD:
5637 inner = ofputil_encode_meter_mod(ofp_version, rf->meter_mod);
5640 case OFPRFR_N_REASONS:
5645 struct ofp_header *inner_oh = inner->data;
5646 inner_oh->xid = rf->xid;
5647 inner_oh->length = htons(inner->size);
5649 struct ofpbuf *outer = ofpraw_alloc_xid(OFPRAW_OFPT14_REQUESTFORWARD,
5650 ofp_version, htonl(0),
5652 ofpbuf_put(outer, inner->data, inner->size);
5653 ofpbuf_delete(inner);
5658 /* Decodes OFPT_REQUESTFORWARD message 'outer'. On success, puts the decoded
5659 * form into '*rf' and returns 0, and the caller is later responsible for
5660 * freeing the content of 'rf', with ofputil_destroy_requestforward(rf). On
5661 * failure, returns an ofperr and '*rf' is indeterminate. */
5663 ofputil_decode_requestforward(const struct ofp_header *outer,
5664 struct ofputil_requestforward *rf)
5669 ofpbuf_use_const(&b, outer, ntohs(outer->length));
5671 /* Skip past outer message. */
5672 enum ofpraw outer_raw = ofpraw_pull_assert(&b);
5673 ovs_assert(outer_raw == OFPRAW_OFPT14_REQUESTFORWARD);
5675 /* Validate inner message. */
5676 if (b.size < sizeof(struct ofp_header)) {
5677 return OFPERR_OFPBFC_MSG_BAD_LEN;
5679 const struct ofp_header *inner = b.data;
5680 unsigned int inner_len = ntohs(inner->length);
5681 if (inner_len < sizeof(struct ofp_header) || inner_len > b.size) {
5682 return OFPERR_OFPBFC_MSG_BAD_LEN;
5684 if (inner->version != outer->version) {
5685 return OFPERR_OFPBRC_BAD_VERSION;
5688 /* Parse inner message. */
5690 error = ofptype_decode(&type, inner);
5695 rf->xid = inner->xid;
5696 if (type == OFPTYPE_GROUP_MOD) {
5697 rf->reason = OFPRFR_GROUP_MOD;
5698 rf->group_mod = xmalloc(sizeof *rf->group_mod);
5699 error = ofputil_decode_group_mod(inner, rf->group_mod);
5701 free(rf->group_mod);
5704 } else if (type == OFPTYPE_METER_MOD) {
5705 rf->reason = OFPRFR_METER_MOD;
5706 rf->meter_mod = xmalloc(sizeof *rf->meter_mod);
5707 ofpbuf_init(&rf->bands, 64);
5708 error = ofputil_decode_meter_mod(inner, rf->meter_mod, &rf->bands);
5710 free(rf->meter_mod);
5711 ofpbuf_uninit(&rf->bands);
5715 return OFPERR_OFPBFC_MSG_UNSUP;
5721 /* Frees the content of 'rf', which should have been initialized through a
5722 * successful call to ofputil_decode_requestforward(). */
5724 ofputil_destroy_requestforward(struct ofputil_requestforward *rf)
5730 switch (rf->reason) {
5731 case OFPRFR_GROUP_MOD:
5732 ofputil_uninit_group_mod(rf->group_mod);
5733 free(rf->group_mod);
5736 case OFPRFR_METER_MOD:
5737 ofpbuf_uninit(&rf->bands);
5738 free(rf->meter_mod);
5741 case OFPRFR_N_REASONS:
5748 /* OpenFlow 1.0 and 1.1 don't distinguish between a field that cannot be
5749 * matched and a field that must be wildcarded. This function returns a bitmap
5750 * that contains both kinds of fields. */
5751 static struct mf_bitmap
5752 wild_or_nonmatchable_fields(const struct ofputil_table_features *features)
5754 struct mf_bitmap wc = features->match;
5755 bitmap_not(wc.bm, MFF_N_IDS);
5756 bitmap_or(wc.bm, features->wildcard.bm, MFF_N_IDS);
5760 struct ofp10_wc_map {
5761 enum ofp10_flow_wildcards wc10;
5762 enum mf_field_id mf;
5765 static const struct ofp10_wc_map ofp10_wc_map[] = {
5766 { OFPFW10_IN_PORT, MFF_IN_PORT },
5767 { OFPFW10_DL_VLAN, MFF_VLAN_VID },
5768 { OFPFW10_DL_SRC, MFF_ETH_SRC },
5769 { OFPFW10_DL_DST, MFF_ETH_DST},
5770 { OFPFW10_DL_TYPE, MFF_ETH_TYPE },
5771 { OFPFW10_NW_PROTO, MFF_IP_PROTO },
5772 { OFPFW10_TP_SRC, MFF_TCP_SRC },
5773 { OFPFW10_TP_DST, MFF_TCP_DST },
5774 { OFPFW10_NW_SRC_MASK, MFF_IPV4_SRC },
5775 { OFPFW10_NW_DST_MASK, MFF_IPV4_DST },
5776 { OFPFW10_DL_VLAN_PCP, MFF_VLAN_PCP },
5777 { OFPFW10_NW_TOS, MFF_IP_DSCP },
5781 mf_bitmap_to_of10(const struct mf_bitmap *fields)
5783 const struct ofp10_wc_map *p;
5786 for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
5787 if (bitmap_is_set(fields->bm, p->mf)) {
5794 static struct mf_bitmap
5795 mf_bitmap_from_of10(ovs_be32 wc10_)
5797 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
5798 const struct ofp10_wc_map *p;
5799 uint32_t wc10 = ntohl(wc10_);
5801 for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
5802 if (wc10 & p->wc10) {
5803 bitmap_set1(fields.bm, p->mf);
5810 ofputil_put_ofp10_table_stats(const struct ofputil_table_stats *stats,
5811 const struct ofputil_table_features *features,
5814 struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
5815 struct ofp10_table_stats *out;
5817 out = ofpbuf_put_zeros(buf, sizeof *out);
5818 out->table_id = features->table_id;
5819 ovs_strlcpy(out->name, features->name, sizeof out->name);
5820 out->wildcards = mf_bitmap_to_of10(&wc);
5821 out->max_entries = htonl(features->max_entries);
5822 out->active_count = htonl(stats->active_count);
5823 put_32aligned_be64(&out->lookup_count, htonll(stats->lookup_count));
5824 put_32aligned_be64(&out->matched_count, htonll(stats->matched_count));
5827 struct ofp11_wc_map {
5828 enum ofp11_flow_match_fields wc11;
5829 enum mf_field_id mf;
5832 static const struct ofp11_wc_map ofp11_wc_map[] = {
5833 { OFPFMF11_IN_PORT, MFF_IN_PORT },
5834 { OFPFMF11_DL_VLAN, MFF_VLAN_VID },
5835 { OFPFMF11_DL_VLAN_PCP, MFF_VLAN_PCP },
5836 { OFPFMF11_DL_TYPE, MFF_ETH_TYPE },
5837 { OFPFMF11_NW_TOS, MFF_IP_DSCP },
5838 { OFPFMF11_NW_PROTO, MFF_IP_PROTO },
5839 { OFPFMF11_TP_SRC, MFF_TCP_SRC },
5840 { OFPFMF11_TP_DST, MFF_TCP_DST },
5841 { OFPFMF11_MPLS_LABEL, MFF_MPLS_LABEL },
5842 { OFPFMF11_MPLS_TC, MFF_MPLS_TC },
5843 /* I don't know what OFPFMF11_TYPE means. */
5844 { OFPFMF11_DL_SRC, MFF_ETH_SRC },
5845 { OFPFMF11_DL_DST, MFF_ETH_DST },
5846 { OFPFMF11_NW_SRC, MFF_IPV4_SRC },
5847 { OFPFMF11_NW_DST, MFF_IPV4_DST },
5848 { OFPFMF11_METADATA, MFF_METADATA },
5852 mf_bitmap_to_of11(const struct mf_bitmap *fields)
5854 const struct ofp11_wc_map *p;
5857 for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
5858 if (bitmap_is_set(fields->bm, p->mf)) {
5865 static struct mf_bitmap
5866 mf_bitmap_from_of11(ovs_be32 wc11_)
5868 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
5869 const struct ofp11_wc_map *p;
5870 uint32_t wc11 = ntohl(wc11_);
5872 for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
5873 if (wc11 & p->wc11) {
5874 bitmap_set1(fields.bm, p->mf);
5881 ofputil_put_ofp11_table_stats(const struct ofputil_table_stats *stats,
5882 const struct ofputil_table_features *features,
5885 struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
5886 struct ofp11_table_stats *out;
5888 out = ofpbuf_put_zeros(buf, sizeof *out);
5889 out->table_id = features->table_id;
5890 ovs_strlcpy(out->name, features->name, sizeof out->name);
5891 out->wildcards = mf_bitmap_to_of11(&wc);
5892 out->match = mf_bitmap_to_of11(&features->match);
5893 out->instructions = ovsinst_bitmap_to_openflow(
5894 features->nonmiss.instructions, OFP11_VERSION);
5895 out->write_actions = ofpact_bitmap_to_openflow(
5896 features->nonmiss.write.ofpacts, OFP11_VERSION);
5897 out->apply_actions = ofpact_bitmap_to_openflow(
5898 features->nonmiss.apply.ofpacts, OFP11_VERSION);
5899 out->config = htonl(features->miss_config);
5900 out->max_entries = htonl(features->max_entries);
5901 out->active_count = htonl(stats->active_count);
5902 out->lookup_count = htonll(stats->lookup_count);
5903 out->matched_count = htonll(stats->matched_count);
5907 ofputil_put_ofp12_table_stats(const struct ofputil_table_stats *stats,
5908 const struct ofputil_table_features *features,
5911 struct ofp12_table_stats *out;
5913 out = ofpbuf_put_zeros(buf, sizeof *out);
5914 out->table_id = features->table_id;
5915 ovs_strlcpy(out->name, features->name, sizeof out->name);
5916 out->match = oxm_bitmap_from_mf_bitmap(&features->match, OFP12_VERSION);
5917 out->wildcards = oxm_bitmap_from_mf_bitmap(&features->wildcard,
5919 out->write_actions = ofpact_bitmap_to_openflow(
5920 features->nonmiss.write.ofpacts, OFP12_VERSION);
5921 out->apply_actions = ofpact_bitmap_to_openflow(
5922 features->nonmiss.apply.ofpacts, OFP12_VERSION);
5923 out->write_setfields = oxm_bitmap_from_mf_bitmap(
5924 &features->nonmiss.write.set_fields, OFP12_VERSION);
5925 out->apply_setfields = oxm_bitmap_from_mf_bitmap(
5926 &features->nonmiss.apply.set_fields, OFP12_VERSION);
5927 out->metadata_match = features->metadata_match;
5928 out->metadata_write = features->metadata_write;
5929 out->instructions = ovsinst_bitmap_to_openflow(
5930 features->nonmiss.instructions, OFP12_VERSION);
5931 out->config = ofputil_encode_table_config(features->miss_config,
5932 OFPUTIL_TABLE_EVICTION_DEFAULT,
5933 OFPUTIL_TABLE_VACANCY_DEFAULT,
5935 out->max_entries = htonl(features->max_entries);
5936 out->active_count = htonl(stats->active_count);
5937 out->lookup_count = htonll(stats->lookup_count);
5938 out->matched_count = htonll(stats->matched_count);
5942 ofputil_put_ofp13_table_stats(const struct ofputil_table_stats *stats,
5945 struct ofp13_table_stats *out;
5947 out = ofpbuf_put_zeros(buf, sizeof *out);
5948 out->table_id = stats->table_id;
5949 out->active_count = htonl(stats->active_count);
5950 out->lookup_count = htonll(stats->lookup_count);
5951 out->matched_count = htonll(stats->matched_count);
5955 ofputil_encode_table_stats_reply(const struct ofp_header *request)
5957 return ofpraw_alloc_stats_reply(request, 0);
5961 ofputil_append_table_stats_reply(struct ofpbuf *reply,
5962 const struct ofputil_table_stats *stats,
5963 const struct ofputil_table_features *features)
5965 struct ofp_header *oh = reply->header;
5967 ovs_assert(stats->table_id == features->table_id);
5969 switch ((enum ofp_version) oh->version) {
5971 ofputil_put_ofp10_table_stats(stats, features, reply);
5975 ofputil_put_ofp11_table_stats(stats, features, reply);
5979 ofputil_put_ofp12_table_stats(stats, features, reply);
5985 ofputil_put_ofp13_table_stats(stats, reply);
5994 ofputil_decode_ofp10_table_stats(struct ofpbuf *msg,
5995 struct ofputil_table_stats *stats,
5996 struct ofputil_table_features *features)
5998 struct ofp10_table_stats *ots;
6000 ots = ofpbuf_try_pull(msg, sizeof *ots);
6002 return OFPERR_OFPBRC_BAD_LEN;
6005 features->table_id = ots->table_id;
6006 ovs_strlcpy(features->name, ots->name, sizeof features->name);
6007 features->max_entries = ntohl(ots->max_entries);
6008 features->match = features->wildcard = mf_bitmap_from_of10(ots->wildcards);
6010 stats->table_id = ots->table_id;
6011 stats->active_count = ntohl(ots->active_count);
6012 stats->lookup_count = ntohll(get_32aligned_be64(&ots->lookup_count));
6013 stats->matched_count = ntohll(get_32aligned_be64(&ots->matched_count));
6019 ofputil_decode_ofp11_table_stats(struct ofpbuf *msg,
6020 struct ofputil_table_stats *stats,
6021 struct ofputil_table_features *features)
6023 struct ofp11_table_stats *ots;
6025 ots = ofpbuf_try_pull(msg, sizeof *ots);
6027 return OFPERR_OFPBRC_BAD_LEN;
6030 features->table_id = ots->table_id;
6031 ovs_strlcpy(features->name, ots->name, sizeof features->name);
6032 features->max_entries = ntohl(ots->max_entries);
6033 features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
6034 ots->instructions, OFP11_VERSION);
6035 features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
6036 ots->write_actions, OFP11_VERSION);
6037 features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
6038 ots->write_actions, OFP11_VERSION);
6039 features->miss = features->nonmiss;
6040 features->miss_config = ofputil_decode_table_miss(ots->config,
6042 features->match = mf_bitmap_from_of11(ots->match);
6043 features->wildcard = mf_bitmap_from_of11(ots->wildcards);
6044 bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
6046 stats->table_id = ots->table_id;
6047 stats->active_count = ntohl(ots->active_count);
6048 stats->lookup_count = ntohll(ots->lookup_count);
6049 stats->matched_count = ntohll(ots->matched_count);
6055 ofputil_decode_ofp12_table_stats(struct ofpbuf *msg,
6056 struct ofputil_table_stats *stats,
6057 struct ofputil_table_features *features)
6059 struct ofp12_table_stats *ots;
6061 ots = ofpbuf_try_pull(msg, sizeof *ots);
6063 return OFPERR_OFPBRC_BAD_LEN;
6066 features->table_id = ots->table_id;
6067 ovs_strlcpy(features->name, ots->name, sizeof features->name);
6068 features->metadata_match = ots->metadata_match;
6069 features->metadata_write = ots->metadata_write;
6070 features->miss_config = ofputil_decode_table_miss(ots->config,
6072 features->max_entries = ntohl(ots->max_entries);
6074 features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
6075 ots->instructions, OFP12_VERSION);
6076 features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
6077 ots->write_actions, OFP12_VERSION);
6078 features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
6079 ots->apply_actions, OFP12_VERSION);
6080 features->nonmiss.write.set_fields = oxm_bitmap_to_mf_bitmap(
6081 ots->write_setfields, OFP12_VERSION);
6082 features->nonmiss.apply.set_fields = oxm_bitmap_to_mf_bitmap(
6083 ots->apply_setfields, OFP12_VERSION);
6084 features->miss = features->nonmiss;
6086 features->match = oxm_bitmap_to_mf_bitmap(ots->match, OFP12_VERSION);
6087 features->wildcard = oxm_bitmap_to_mf_bitmap(ots->wildcards,
6089 bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
6091 stats->table_id = ots->table_id;
6092 stats->active_count = ntohl(ots->active_count);
6093 stats->lookup_count = ntohll(ots->lookup_count);
6094 stats->matched_count = ntohll(ots->matched_count);
6100 ofputil_decode_ofp13_table_stats(struct ofpbuf *msg,
6101 struct ofputil_table_stats *stats,
6102 struct ofputil_table_features *features)
6104 struct ofp13_table_stats *ots;
6106 ots = ofpbuf_try_pull(msg, sizeof *ots);
6108 return OFPERR_OFPBRC_BAD_LEN;
6111 features->table_id = ots->table_id;
6113 stats->table_id = ots->table_id;
6114 stats->active_count = ntohl(ots->active_count);
6115 stats->lookup_count = ntohll(ots->lookup_count);
6116 stats->matched_count = ntohll(ots->matched_count);
6122 ofputil_decode_table_stats_reply(struct ofpbuf *msg,
6123 struct ofputil_table_stats *stats,
6124 struct ofputil_table_features *features)
6126 const struct ofp_header *oh;
6129 ofpraw_pull_assert(msg);
6137 memset(stats, 0, sizeof *stats);
6138 memset(features, 0, sizeof *features);
6139 features->supports_eviction = -1;
6140 features->supports_vacancy_events = -1;
6142 switch ((enum ofp_version) oh->version) {
6144 return ofputil_decode_ofp10_table_stats(msg, stats, features);
6147 return ofputil_decode_ofp11_table_stats(msg, stats, features);
6150 return ofputil_decode_ofp12_table_stats(msg, stats, features);
6155 return ofputil_decode_ofp13_table_stats(msg, stats, features);
6162 /* ofputil_flow_monitor_request */
6164 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
6165 * ofputil_flow_monitor_request in 'rq'.
6167 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
6168 * message. Calling this function multiple times for a single 'msg' iterates
6169 * through the requests. The caller must initially leave 'msg''s layer
6170 * pointers null and not modify them between calls.
6172 * Returns 0 if successful, EOF if no requests were left in this 'msg',
6173 * otherwise an OFPERR_* value. */
6175 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
6178 struct nx_flow_monitor_request *nfmr;
6182 ofpraw_pull_assert(msg);
6189 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
6191 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %"PRIu32" "
6192 "leftover bytes at end", msg->size);
6193 return OFPERR_OFPBRC_BAD_LEN;
6196 flags = ntohs(nfmr->flags);
6197 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
6198 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
6199 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
6200 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
6202 return OFPERR_OFPMOFC_BAD_FLAGS;
6205 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
6206 return OFPERR_NXBRC_MUST_BE_ZERO;
6209 rq->id = ntohl(nfmr->id);
6211 rq->out_port = u16_to_ofp(ntohs(nfmr->out_port));
6212 rq->table_id = nfmr->table_id;
6214 return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL, NULL);
6218 ofputil_append_flow_monitor_request(
6219 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
6221 struct nx_flow_monitor_request *nfmr;
6226 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
6229 start_ofs = msg->size;
6230 ofpbuf_put_zeros(msg, sizeof *nfmr);
6231 match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
6233 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
6234 nfmr->id = htonl(rq->id);
6235 nfmr->flags = htons(rq->flags);
6236 nfmr->out_port = htons(ofp_to_u16(rq->out_port));
6237 nfmr->match_len = htons(match_len);
6238 nfmr->table_id = rq->table_id;
6241 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
6242 * into an abstract ofputil_flow_update in 'update'. The caller must have
6243 * initialized update->match to point to space allocated for a match.
6245 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
6246 * actions (except for NXFME_ABBREV, which never includes actions). The caller
6247 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
6248 * will point into the 'ofpacts' buffer.
6250 * Multiple flow updates can be packed into a single OpenFlow message. Calling
6251 * this function multiple times for a single 'msg' iterates through the
6252 * updates. The caller must initially leave 'msg''s layer pointers null and
6253 * not modify them between calls.
6255 * Returns 0 if successful, EOF if no updates were left in this 'msg',
6256 * otherwise an OFPERR_* value. */
6258 ofputil_decode_flow_update(struct ofputil_flow_update *update,
6259 struct ofpbuf *msg, struct ofpbuf *ofpacts)
6261 struct nx_flow_update_header *nfuh;
6262 unsigned int length;
6263 struct ofp_header *oh;
6266 ofpraw_pull_assert(msg);
6269 ofpbuf_clear(ofpacts);
6274 if (msg->size < sizeof(struct nx_flow_update_header)) {
6281 update->event = ntohs(nfuh->event);
6282 length = ntohs(nfuh->length);
6283 if (length > msg->size || length % 8) {
6287 if (update->event == NXFME_ABBREV) {
6288 struct nx_flow_update_abbrev *nfua;
6290 if (length != sizeof *nfua) {
6294 nfua = ofpbuf_pull(msg, sizeof *nfua);
6295 update->xid = nfua->xid;
6297 } else if (update->event == NXFME_ADDED
6298 || update->event == NXFME_DELETED
6299 || update->event == NXFME_MODIFIED) {
6300 struct nx_flow_update_full *nfuf;
6301 unsigned int actions_len;
6302 unsigned int match_len;
6305 if (length < sizeof *nfuf) {
6309 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
6310 match_len = ntohs(nfuf->match_len);
6311 if (sizeof *nfuf + match_len > length) {
6315 update->reason = ntohs(nfuf->reason);
6316 update->idle_timeout = ntohs(nfuf->idle_timeout);
6317 update->hard_timeout = ntohs(nfuf->hard_timeout);
6318 update->table_id = nfuf->table_id;
6319 update->cookie = nfuf->cookie;
6320 update->priority = ntohs(nfuf->priority);
6322 error = nx_pull_match(msg, match_len, update->match, NULL, NULL);
6327 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
6328 error = ofpacts_pull_openflow_actions(msg, actions_len, oh->version,
6334 update->ofpacts = ofpacts->data;
6335 update->ofpacts_len = ofpacts->size;
6338 VLOG_WARN_RL(&bad_ofmsg_rl,
6339 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
6340 ntohs(nfuh->event));
6341 return OFPERR_NXBRC_FM_BAD_EVENT;
6345 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %"PRIu32" "
6346 "leftover bytes at end", msg->size);
6347 return OFPERR_OFPBRC_BAD_LEN;
6351 ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
6353 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
6355 return ntohl(cancel->id);
6359 ofputil_encode_flow_monitor_cancel(uint32_t id)
6361 struct nx_flow_monitor_cancel *nfmc;
6364 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
6365 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
6366 nfmc->id = htonl(id);
6371 ofputil_start_flow_update(struct ovs_list *replies)
6375 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
6379 list_push_back(replies, &msg->list_node);
6383 ofputil_append_flow_update(const struct ofputil_flow_update *update,
6384 struct ovs_list *replies)
6386 enum ofp_version version = ofpmp_version(replies);
6387 struct nx_flow_update_header *nfuh;
6391 msg = ofpbuf_from_list(list_back(replies));
6392 start_ofs = msg->size;
6394 if (update->event == NXFME_ABBREV) {
6395 struct nx_flow_update_abbrev *nfua;
6397 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
6398 nfua->xid = update->xid;
6400 struct nx_flow_update_full *nfuf;
6403 ofpbuf_put_zeros(msg, sizeof *nfuf);
6404 match_len = nx_put_match(msg, update->match, htonll(0), htonll(0));
6405 ofpacts_put_openflow_actions(update->ofpacts, update->ofpacts_len, msg,
6407 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
6408 nfuf->reason = htons(update->reason);
6409 nfuf->priority = htons(update->priority);
6410 nfuf->idle_timeout = htons(update->idle_timeout);
6411 nfuf->hard_timeout = htons(update->hard_timeout);
6412 nfuf->match_len = htons(match_len);
6413 nfuf->table_id = update->table_id;
6414 nfuf->cookie = update->cookie;
6417 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
6418 nfuh->length = htons(msg->size - start_ofs);
6419 nfuh->event = htons(update->event);
6421 ofpmp_postappend(replies, start_ofs);
6425 ofputil_encode_packet_out(const struct ofputil_packet_out *po,
6426 enum ofputil_protocol protocol)
6428 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
6432 size = po->ofpacts_len;
6433 if (po->buffer_id == UINT32_MAX) {
6434 size += po->packet_len;
6437 switch (ofp_version) {
6438 case OFP10_VERSION: {
6439 struct ofp10_packet_out *opo;
6442 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
6443 ofpbuf_put_zeros(msg, sizeof *opo);
6444 actions_ofs = msg->size;
6445 ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
6449 opo->buffer_id = htonl(po->buffer_id);
6450 opo->in_port = htons(ofp_to_u16(po->in_port));
6451 opo->actions_len = htons(msg->size - actions_ofs);
6459 case OFP15_VERSION: {
6460 struct ofp11_packet_out *opo;
6463 msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size);
6464 ofpbuf_put_zeros(msg, sizeof *opo);
6465 len = ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
6468 opo->buffer_id = htonl(po->buffer_id);
6469 opo->in_port = ofputil_port_to_ofp11(po->in_port);
6470 opo->actions_len = htons(len);
6478 if (po->buffer_id == UINT32_MAX) {
6479 ofpbuf_put(msg, po->packet, po->packet_len);
6482 ofpmsg_update_length(msg);
6487 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
6489 make_echo_request(enum ofp_version ofp_version)
6491 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
6495 /* Creates and returns an OFPT_ECHO_REPLY message matching the
6496 * OFPT_ECHO_REQUEST message in 'rq'. */
6498 make_echo_reply(const struct ofp_header *rq)
6500 struct ofpbuf rq_buf;
6501 struct ofpbuf *reply;
6503 ofpbuf_use_const(&rq_buf, rq, ntohs(rq->length));
6504 ofpraw_pull_assert(&rq_buf);
6506 reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY, rq, rq_buf.size);
6507 ofpbuf_put(reply, rq_buf.data, rq_buf.size);
6512 ofputil_encode_barrier_request(enum ofp_version ofp_version)
6516 switch (ofp_version) {
6522 type = OFPRAW_OFPT11_BARRIER_REQUEST;
6526 type = OFPRAW_OFPT10_BARRIER_REQUEST;
6533 return ofpraw_alloc(type, ofp_version, 0);
6537 ofputil_frag_handling_to_string(enum ofputil_frag_handling frag)
6540 case OFPUTIL_FRAG_NORMAL: return "normal";
6541 case OFPUTIL_FRAG_DROP: return "drop";
6542 case OFPUTIL_FRAG_REASM: return "reassemble";
6543 case OFPUTIL_FRAG_NX_MATCH: return "nx-match";
6550 ofputil_frag_handling_from_string(const char *s,
6551 enum ofputil_frag_handling *frag)
6553 if (!strcasecmp(s, "normal")) {
6554 *frag = OFPUTIL_FRAG_NORMAL;
6555 } else if (!strcasecmp(s, "drop")) {
6556 *frag = OFPUTIL_FRAG_DROP;
6557 } else if (!strcasecmp(s, "reassemble")) {
6558 *frag = OFPUTIL_FRAG_REASM;
6559 } else if (!strcasecmp(s, "nx-match")) {
6560 *frag = OFPUTIL_FRAG_NX_MATCH;
6567 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
6568 * port number and stores the latter in '*ofp10_port', for the purpose of
6569 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
6570 * otherwise an OFPERR_* number. On error, stores OFPP_NONE in '*ofp10_port'.
6572 * See the definition of OFP11_MAX for an explanation of the mapping. */
6574 ofputil_port_from_ofp11(ovs_be32 ofp11_port, ofp_port_t *ofp10_port)
6576 uint32_t ofp11_port_h = ntohl(ofp11_port);
6578 if (ofp11_port_h < ofp_to_u16(OFPP_MAX)) {
6579 *ofp10_port = u16_to_ofp(ofp11_port_h);
6581 } else if (ofp11_port_h >= ofp11_to_u32(OFPP11_MAX)) {
6582 *ofp10_port = u16_to_ofp(ofp11_port_h - OFPP11_OFFSET);
6585 *ofp10_port = OFPP_NONE;
6586 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
6587 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
6588 ofp11_port_h, ofp_to_u16(OFPP_MAX) - 1,
6589 ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
6590 return OFPERR_OFPBAC_BAD_OUT_PORT;
6594 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
6595 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
6597 * See the definition of OFP11_MAX for an explanation of the mapping. */
6599 ofputil_port_to_ofp11(ofp_port_t ofp10_port)
6601 return htonl(ofp_to_u16(ofp10_port) < ofp_to_u16(OFPP_MAX)
6602 ? ofp_to_u16(ofp10_port)
6603 : ofp_to_u16(ofp10_port) + OFPP11_OFFSET);
6606 #define OFPUTIL_NAMED_PORTS \
6607 OFPUTIL_NAMED_PORT(IN_PORT) \
6608 OFPUTIL_NAMED_PORT(TABLE) \
6609 OFPUTIL_NAMED_PORT(NORMAL) \
6610 OFPUTIL_NAMED_PORT(FLOOD) \
6611 OFPUTIL_NAMED_PORT(ALL) \
6612 OFPUTIL_NAMED_PORT(CONTROLLER) \
6613 OFPUTIL_NAMED_PORT(LOCAL) \
6614 OFPUTIL_NAMED_PORT(ANY) \
6615 OFPUTIL_NAMED_PORT(UNSET)
6617 /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */
6618 #define OFPUTIL_NAMED_PORTS_WITH_NONE \
6619 OFPUTIL_NAMED_PORTS \
6620 OFPUTIL_NAMED_PORT(NONE)
6622 /* Stores the port number represented by 's' into '*portp'. 's' may be an
6623 * integer or, for reserved ports, the standard OpenFlow name for the port
6626 * Returns true if successful, false if 's' is not a valid OpenFlow port number
6627 * or name. The caller should issue an error message in this case, because
6628 * this function usually does not. (This gives the caller an opportunity to
6629 * look up the port name another way, e.g. by contacting the switch and listing
6630 * the names of all its ports).
6632 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
6633 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
6634 * range as described in include/openflow/openflow-1.1.h. */
6636 ofputil_port_from_string(const char *s, ofp_port_t *portp)
6638 unsigned int port32; /* int is at least 32 bits wide. */
6641 VLOG_WARN("Negative value %s is not a valid port number.", s);
6645 if (str_to_uint(s, 10, &port32)) {
6646 if (port32 < ofp_to_u16(OFPP_MAX)) {
6648 } else if (port32 < ofp_to_u16(OFPP_FIRST_RESV)) {
6649 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
6650 "be translated to %u when talking to an OF1.1 or "
6651 "later controller", port32, port32 + OFPP11_OFFSET);
6652 } else if (port32 <= ofp_to_u16(OFPP_LAST_RESV)) {
6653 char name[OFP_MAX_PORT_NAME_LEN];
6655 ofputil_port_to_string(u16_to_ofp(port32), name, sizeof name);
6656 VLOG_WARN_ONCE("referring to port %s as %"PRIu32" is deprecated "
6657 "for compatibility with OpenFlow 1.1 and later",
6659 } else if (port32 < ofp11_to_u32(OFPP11_MAX)) {
6660 VLOG_WARN("port %u is outside the supported range 0 through "
6661 "%"PRIx16" or 0x%x through 0x%"PRIx32, port32,
6662 UINT16_MAX, ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
6665 port32 -= OFPP11_OFFSET;
6668 *portp = u16_to_ofp(port32);
6675 static const struct pair pairs[] = {
6676 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
6677 OFPUTIL_NAMED_PORTS_WITH_NONE
6678 #undef OFPUTIL_NAMED_PORT
6680 const struct pair *p;
6682 for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) {
6683 if (!strcasecmp(s, p->name)) {
6692 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
6693 * Most ports' string representation is just the port number, but for special
6694 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
6696 ofputil_format_port(ofp_port_t port, struct ds *s)
6698 char name[OFP_MAX_PORT_NAME_LEN];
6700 ofputil_port_to_string(port, name, sizeof name);
6701 ds_put_cstr(s, name);
6704 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
6705 * representation of OpenFlow port number 'port'. Most ports are represented
6706 * as just the port number, but special ports, e.g. OFPP_LOCAL, are represented
6707 * by name, e.g. "LOCAL". */
6709 ofputil_port_to_string(ofp_port_t port,
6710 char namebuf[OFP_MAX_PORT_NAME_LEN], size_t bufsize)
6713 #define OFPUTIL_NAMED_PORT(NAME) \
6715 ovs_strlcpy(namebuf, #NAME, bufsize); \
6718 #undef OFPUTIL_NAMED_PORT
6721 snprintf(namebuf, bufsize, "%"PRIu16, port);
6726 /* Stores the group id represented by 's' into '*group_idp'. 's' may be an
6727 * integer or, for reserved group IDs, the standard OpenFlow name for the group
6728 * (either "ANY" or "ALL").
6730 * Returns true if successful, false if 's' is not a valid OpenFlow group ID or
6733 ofputil_group_from_string(const char *s, uint32_t *group_idp)
6735 if (!strcasecmp(s, "any")) {
6736 *group_idp = OFPG_ANY;
6737 } else if (!strcasecmp(s, "all")) {
6738 *group_idp = OFPG_ALL;
6739 } else if (!str_to_uint(s, 10, group_idp)) {
6740 VLOG_WARN("%s is not a valid group ID. (Valid group IDs are "
6741 "32-bit nonnegative integers or the keywords ANY or "
6749 /* Appends to 's' a string representation of the OpenFlow group ID 'group_id'.
6750 * Most groups' string representation is just the number, but for special
6751 * groups, e.g. OFPG_ALL, it is the name, e.g. "ALL". */
6753 ofputil_format_group(uint32_t group_id, struct ds *s)
6755 char name[MAX_GROUP_NAME_LEN];
6757 ofputil_group_to_string(group_id, name, sizeof name);
6758 ds_put_cstr(s, name);
6762 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
6763 * representation of OpenFlow group ID 'group_id'. Most group are represented
6764 * as just their number, but special groups, e.g. OFPG_ALL, are represented
6765 * by name, e.g. "ALL". */
6767 ofputil_group_to_string(uint32_t group_id,
6768 char namebuf[MAX_GROUP_NAME_LEN + 1], size_t bufsize)
6772 ovs_strlcpy(namebuf, "ALL", bufsize);
6776 ovs_strlcpy(namebuf, "ANY", bufsize);
6780 snprintf(namebuf, bufsize, "%"PRIu32, group_id);
6785 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
6786 * 'ofp_version', tries to pull the first element from the array. If
6787 * successful, initializes '*pp' with an abstract representation of the
6788 * port and returns 0. If no ports remain to be decoded, returns EOF.
6789 * On an error, returns a positive OFPERR_* value. */
6791 ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
6792 struct ofputil_phy_port *pp)
6794 memset(pp, 0, sizeof *pp);
6796 switch (ofp_version) {
6797 case OFP10_VERSION: {
6798 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
6799 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
6803 case OFP13_VERSION: {
6804 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
6805 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
6809 return b->size ? ofputil_pull_ofp14_port(pp, b) : EOF;
6816 ofputil_normalize_match__(struct match *match, bool may_log)
6819 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
6820 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
6821 MAY_NW_PROTO = 1 << 2, /* nw_proto */
6822 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
6823 MAY_ARP_SHA = 1 << 4, /* arp_sha */
6824 MAY_ARP_THA = 1 << 5, /* arp_tha */
6825 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
6826 MAY_ND_TARGET = 1 << 7, /* nd_target */
6827 MAY_MPLS = 1 << 8, /* mpls label and tc */
6830 struct flow_wildcards wc;
6832 /* Figure out what fields may be matched. */
6833 if (match->flow.dl_type == htons(ETH_TYPE_IP)) {
6834 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
6835 if (match->flow.nw_proto == IPPROTO_TCP ||
6836 match->flow.nw_proto == IPPROTO_UDP ||
6837 match->flow.nw_proto == IPPROTO_SCTP ||
6838 match->flow.nw_proto == IPPROTO_ICMP) {
6839 may_match |= MAY_TP_ADDR;
6841 } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
6842 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
6843 if (match->flow.nw_proto == IPPROTO_TCP ||
6844 match->flow.nw_proto == IPPROTO_UDP ||
6845 match->flow.nw_proto == IPPROTO_SCTP) {
6846 may_match |= MAY_TP_ADDR;
6847 } else if (match->flow.nw_proto == IPPROTO_ICMPV6) {
6848 may_match |= MAY_TP_ADDR;
6849 if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
6850 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
6851 } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
6852 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
6855 } else if (match->flow.dl_type == htons(ETH_TYPE_ARP) ||
6856 match->flow.dl_type == htons(ETH_TYPE_RARP)) {
6857 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
6858 } else if (eth_type_mpls(match->flow.dl_type)) {
6859 may_match = MAY_MPLS;
6864 /* Clear the fields that may not be matched. */
6866 if (!(may_match & MAY_NW_ADDR)) {
6867 wc.masks.nw_src = wc.masks.nw_dst = htonl(0);
6869 if (!(may_match & MAY_TP_ADDR)) {
6870 wc.masks.tp_src = wc.masks.tp_dst = htons(0);
6872 if (!(may_match & MAY_NW_PROTO)) {
6873 wc.masks.nw_proto = 0;
6875 if (!(may_match & MAY_IPVx)) {
6876 wc.masks.nw_tos = 0;
6877 wc.masks.nw_ttl = 0;
6879 if (!(may_match & MAY_ARP_SHA)) {
6880 WC_UNMASK_FIELD(&wc, arp_sha);
6882 if (!(may_match & MAY_ARP_THA)) {
6883 WC_UNMASK_FIELD(&wc, arp_tha);
6885 if (!(may_match & MAY_IPV6)) {
6886 wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any;
6887 wc.masks.ipv6_label = htonl(0);
6889 if (!(may_match & MAY_ND_TARGET)) {
6890 wc.masks.nd_target = in6addr_any;
6892 if (!(may_match & MAY_MPLS)) {
6893 memset(wc.masks.mpls_lse, 0, sizeof wc.masks.mpls_lse);
6896 /* Log any changes. */
6897 if (!flow_wildcards_equal(&wc, &match->wc)) {
6898 bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
6899 char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL;
6902 match_zero_wildcarded_fields(match);
6905 char *post = match_to_string(match, OFP_DEFAULT_PRIORITY);
6906 VLOG_INFO("normalization changed ofp_match, details:");
6907 VLOG_INFO(" pre: %s", pre);
6908 VLOG_INFO("post: %s", post);
6915 /* "Normalizes" the wildcards in 'match'. That means:
6917 * 1. If the type of level N is known, then only the valid fields for that
6918 * level may be specified. For example, ARP does not have a TOS field,
6919 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
6920 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
6921 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
6924 * 2. If the type of level N is not known (or not understood by Open
6925 * vSwitch), then no fields at all for that level may be specified. For
6926 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
6927 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
6930 * If this function changes 'match', it logs a rate-limited informational
6933 ofputil_normalize_match(struct match *match)
6935 ofputil_normalize_match__(match, true);
6938 /* Same as ofputil_normalize_match() without the logging. Thus, this function
6939 * is suitable for a program's internal use, whereas ofputil_normalize_match()
6940 * sense for use on flows received from elsewhere (so that a bug in the program
6941 * that sent them can be reported and corrected). */
6943 ofputil_normalize_match_quiet(struct match *match)
6945 ofputil_normalize_match__(match, false);
6948 /* Parses a key or a key-value pair from '*stringp'.
6950 * On success: Stores the key into '*keyp'. Stores the value, if present, into
6951 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
6952 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
6953 * are substrings of '*stringp' created by replacing some of its bytes by null
6954 * terminators. Returns true.
6956 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
6957 * NULL and returns false. */
6959 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
6961 char *pos, *key, *value;
6965 pos += strspn(pos, ", \t\r\n");
6967 *keyp = *valuep = NULL;
6972 key_len = strcspn(pos, ":=(, \t\r\n");
6973 if (key[key_len] == ':' || key[key_len] == '=') {
6974 /* The value can be separated by a colon. */
6977 value = key + key_len + 1;
6978 value_len = strcspn(value, ", \t\r\n");
6979 pos = value + value_len + (value[value_len] != '\0');
6980 value[value_len] = '\0';
6981 } else if (key[key_len] == '(') {
6982 /* The value can be surrounded by balanced parentheses. The outermost
6983 * set of parentheses is removed. */
6987 value = key + key_len + 1;
6988 for (value_len = 0; level > 0; value_len++) {
6989 switch (value[value_len]) {
7003 value[value_len - 1] = '\0';
7004 pos = value + value_len;
7006 /* There might be no value at all. */
7007 value = key + key_len; /* Will become the empty string below. */
7008 pos = key + key_len + (key[key_len] != '\0');
7010 key[key_len] = '\0';
7018 /* Encode a dump ports request for 'port', the encoded message
7019 * will be for OpenFlow version 'ofp_version'. Returns message
7020 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
7022 ofputil_encode_dump_ports_request(enum ofp_version ofp_version, ofp_port_t port)
7024 struct ofpbuf *request;
7026 switch (ofp_version) {
7027 case OFP10_VERSION: {
7028 struct ofp10_port_stats_request *req;
7029 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0);
7030 req = ofpbuf_put_zeros(request, sizeof *req);
7031 req->port_no = htons(ofp_to_u16(port));
7038 case OFP15_VERSION: {
7039 struct ofp11_port_stats_request *req;
7040 request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0);
7041 req = ofpbuf_put_zeros(request, sizeof *req);
7042 req->port_no = ofputil_port_to_ofp11(port);
7053 ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops,
7054 struct ofp10_port_stats *ps10)
7056 ps10->port_no = htons(ofp_to_u16(ops->port_no));
7057 memset(ps10->pad, 0, sizeof ps10->pad);
7058 put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets));
7059 put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets));
7060 put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes));
7061 put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes));
7062 put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped));
7063 put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped));
7064 put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors));
7065 put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors));
7066 put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors));
7067 put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors));
7068 put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors));
7069 put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions));
7073 ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops,
7074 struct ofp11_port_stats *ps11)
7076 ps11->port_no = ofputil_port_to_ofp11(ops->port_no);
7077 memset(ps11->pad, 0, sizeof ps11->pad);
7078 ps11->rx_packets = htonll(ops->stats.rx_packets);
7079 ps11->tx_packets = htonll(ops->stats.tx_packets);
7080 ps11->rx_bytes = htonll(ops->stats.rx_bytes);
7081 ps11->tx_bytes = htonll(ops->stats.tx_bytes);
7082 ps11->rx_dropped = htonll(ops->stats.rx_dropped);
7083 ps11->tx_dropped = htonll(ops->stats.tx_dropped);
7084 ps11->rx_errors = htonll(ops->stats.rx_errors);
7085 ps11->tx_errors = htonll(ops->stats.tx_errors);
7086 ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors);
7087 ps11->rx_over_err = htonll(ops->stats.rx_over_errors);
7088 ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors);
7089 ps11->collisions = htonll(ops->stats.collisions);
7093 ofputil_port_stats_to_ofp13(const struct ofputil_port_stats *ops,
7094 struct ofp13_port_stats *ps13)
7096 ofputil_port_stats_to_ofp11(ops, &ps13->ps);
7097 ps13->duration_sec = htonl(ops->duration_sec);
7098 ps13->duration_nsec = htonl(ops->duration_nsec);
7102 ofputil_append_ofp14_port_stats(const struct ofputil_port_stats *ops,
7103 struct ovs_list *replies)
7105 struct ofp14_port_stats_prop_ethernet *eth;
7106 struct ofp14_port_stats *ps14;
7107 struct ofpbuf *reply;
7109 reply = ofpmp_reserve(replies, sizeof *ps14 + sizeof *eth);
7111 ps14 = ofpbuf_put_uninit(reply, sizeof *ps14);
7112 ps14->length = htons(sizeof *ps14 + sizeof *eth);
7113 memset(ps14->pad, 0, sizeof ps14->pad);
7114 ps14->port_no = ofputil_port_to_ofp11(ops->port_no);
7115 ps14->duration_sec = htonl(ops->duration_sec);
7116 ps14->duration_nsec = htonl(ops->duration_nsec);
7117 ps14->rx_packets = htonll(ops->stats.rx_packets);
7118 ps14->tx_packets = htonll(ops->stats.tx_packets);
7119 ps14->rx_bytes = htonll(ops->stats.rx_bytes);
7120 ps14->tx_bytes = htonll(ops->stats.tx_bytes);
7121 ps14->rx_dropped = htonll(ops->stats.rx_dropped);
7122 ps14->tx_dropped = htonll(ops->stats.tx_dropped);
7123 ps14->rx_errors = htonll(ops->stats.rx_errors);
7124 ps14->tx_errors = htonll(ops->stats.tx_errors);
7126 eth = ofpbuf_put_uninit(reply, sizeof *eth);
7127 eth->type = htons(OFPPSPT14_ETHERNET);
7128 eth->length = htons(sizeof *eth);
7129 memset(eth->pad, 0, sizeof eth->pad);
7130 eth->rx_frame_err = htonll(ops->stats.rx_frame_errors);
7131 eth->rx_over_err = htonll(ops->stats.rx_over_errors);
7132 eth->rx_crc_err = htonll(ops->stats.rx_crc_errors);
7133 eth->collisions = htonll(ops->stats.collisions);
7136 /* Encode a ports stat for 'ops' and append it to 'replies'. */
7138 ofputil_append_port_stat(struct ovs_list *replies,
7139 const struct ofputil_port_stats *ops)
7141 switch (ofpmp_version(replies)) {
7142 case OFP13_VERSION: {
7143 struct ofp13_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7144 ofputil_port_stats_to_ofp13(ops, reply);
7148 case OFP11_VERSION: {
7149 struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7150 ofputil_port_stats_to_ofp11(ops, reply);
7154 case OFP10_VERSION: {
7155 struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7156 ofputil_port_stats_to_ofp10(ops, reply);
7162 ofputil_append_ofp14_port_stats(ops, replies);
7171 ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops,
7172 const struct ofp10_port_stats *ps10)
7174 memset(ops, 0, sizeof *ops);
7176 ops->port_no = u16_to_ofp(ntohs(ps10->port_no));
7177 ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets));
7178 ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets));
7179 ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes));
7180 ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes));
7181 ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped));
7182 ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped));
7183 ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors));
7184 ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors));
7185 ops->stats.rx_frame_errors =
7186 ntohll(get_32aligned_be64(&ps10->rx_frame_err));
7187 ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err));
7188 ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err));
7189 ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions));
7190 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
7196 ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops,
7197 const struct ofp11_port_stats *ps11)
7201 memset(ops, 0, sizeof *ops);
7202 error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no);
7207 ops->stats.rx_packets = ntohll(ps11->rx_packets);
7208 ops->stats.tx_packets = ntohll(ps11->tx_packets);
7209 ops->stats.rx_bytes = ntohll(ps11->rx_bytes);
7210 ops->stats.tx_bytes = ntohll(ps11->tx_bytes);
7211 ops->stats.rx_dropped = ntohll(ps11->rx_dropped);
7212 ops->stats.tx_dropped = ntohll(ps11->tx_dropped);
7213 ops->stats.rx_errors = ntohll(ps11->rx_errors);
7214 ops->stats.tx_errors = ntohll(ps11->tx_errors);
7215 ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err);
7216 ops->stats.rx_over_errors = ntohll(ps11->rx_over_err);
7217 ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err);
7218 ops->stats.collisions = ntohll(ps11->collisions);
7219 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
7225 ofputil_port_stats_from_ofp13(struct ofputil_port_stats *ops,
7226 const struct ofp13_port_stats *ps13)
7228 enum ofperr error = ofputil_port_stats_from_ofp11(ops, &ps13->ps);
7230 ops->duration_sec = ntohl(ps13->duration_sec);
7231 ops->duration_nsec = ntohl(ps13->duration_nsec);
7237 parse_ofp14_port_stats_ethernet_property(const struct ofpbuf *payload,
7238 struct ofputil_port_stats *ops)
7240 const struct ofp14_port_stats_prop_ethernet *eth = payload->data;
7242 if (payload->size != sizeof *eth) {
7243 return OFPERR_OFPBPC_BAD_LEN;
7246 ops->stats.rx_frame_errors = ntohll(eth->rx_frame_err);
7247 ops->stats.rx_over_errors = ntohll(eth->rx_over_err);
7248 ops->stats.rx_crc_errors = ntohll(eth->rx_crc_err);
7249 ops->stats.collisions = ntohll(eth->collisions);
7255 ofputil_pull_ofp14_port_stats(struct ofputil_port_stats *ops,
7258 const struct ofp14_port_stats *ps14;
7259 struct ofpbuf properties;
7263 ps14 = ofpbuf_try_pull(msg, sizeof *ps14);
7265 return OFPERR_OFPBRC_BAD_LEN;
7268 len = ntohs(ps14->length);
7269 if (len < sizeof *ps14 || len - sizeof *ps14 > msg->size) {
7270 return OFPERR_OFPBRC_BAD_LEN;
7272 len -= sizeof *ps14;
7273 ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len);
7275 error = ofputil_port_from_ofp11(ps14->port_no, &ops->port_no);
7280 ops->duration_sec = ntohl(ps14->duration_sec);
7281 ops->duration_nsec = ntohl(ps14->duration_nsec);
7282 ops->stats.rx_packets = ntohll(ps14->rx_packets);
7283 ops->stats.tx_packets = ntohll(ps14->tx_packets);
7284 ops->stats.rx_bytes = ntohll(ps14->rx_bytes);
7285 ops->stats.tx_bytes = ntohll(ps14->tx_bytes);
7286 ops->stats.rx_dropped = ntohll(ps14->rx_dropped);
7287 ops->stats.tx_dropped = ntohll(ps14->tx_dropped);
7288 ops->stats.rx_errors = ntohll(ps14->rx_errors);
7289 ops->stats.tx_errors = ntohll(ps14->tx_errors);
7290 ops->stats.rx_frame_errors = UINT64_MAX;
7291 ops->stats.rx_over_errors = UINT64_MAX;
7292 ops->stats.rx_crc_errors = UINT64_MAX;
7293 ops->stats.collisions = UINT64_MAX;
7295 while (properties.size > 0) {
7296 struct ofpbuf payload;
7300 error = ofputil_pull_property(&properties, &payload, &type);
7306 case OFPPSPT14_ETHERNET:
7307 error = parse_ofp14_port_stats_ethernet_property(&payload, ops);
7311 log_property(true, "unknown port stats property %"PRIu16, type);
7324 /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
7327 ofputil_count_port_stats(const struct ofp_header *oh)
7329 struct ofputil_port_stats ps;
7333 ofpbuf_use_const(&b, oh, ntohs(oh->length));
7334 ofpraw_pull_assert(&b);
7335 while (!ofputil_decode_port_stats(&ps, &b)) {
7341 /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
7342 * ofputil_port_stats in 'ps'.
7344 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
7345 * message. Calling this function multiple times for a single 'msg' iterates
7346 * through the replies. The caller must initially leave 'msg''s layer pointers
7347 * null and not modify them between calls.
7349 * Returns 0 if successful, EOF if no replies were left in this 'msg',
7350 * otherwise a positive errno value. */
7352 ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg)
7357 error = (msg->header ? ofpraw_decode(&raw, msg->header)
7358 : ofpraw_pull(&raw, msg));
7365 } else if (raw == OFPRAW_OFPST14_PORT_REPLY) {
7366 return ofputil_pull_ofp14_port_stats(ps, msg);
7367 } else if (raw == OFPRAW_OFPST13_PORT_REPLY) {
7368 const struct ofp13_port_stats *ps13;
7370 ps13 = ofpbuf_try_pull(msg, sizeof *ps13);
7374 return ofputil_port_stats_from_ofp13(ps, ps13);
7375 } else if (raw == OFPRAW_OFPST11_PORT_REPLY) {
7376 const struct ofp11_port_stats *ps11;
7378 ps11 = ofpbuf_try_pull(msg, sizeof *ps11);
7382 return ofputil_port_stats_from_ofp11(ps, ps11);
7383 } else if (raw == OFPRAW_OFPST10_PORT_REPLY) {
7384 const struct ofp10_port_stats *ps10;
7386 ps10 = ofpbuf_try_pull(msg, sizeof *ps10);
7390 return ofputil_port_stats_from_ofp10(ps, ps10);
7396 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %"PRIu32" leftover "
7397 "bytes at end", msg->size);
7398 return OFPERR_OFPBRC_BAD_LEN;
7401 /* Parse a port status request message into a 16 bit OpenFlow 1.0
7402 * port number and stores the latter in '*ofp10_port'.
7403 * Returns 0 if successful, otherwise an OFPERR_* number. */
7405 ofputil_decode_port_stats_request(const struct ofp_header *request,
7406 ofp_port_t *ofp10_port)
7408 switch ((enum ofp_version)request->version) {
7413 case OFP11_VERSION: {
7414 const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request);
7415 return ofputil_port_from_ofp11(psr11->port_no, ofp10_port);
7418 case OFP10_VERSION: {
7419 const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request);
7420 *ofp10_port = u16_to_ofp(ntohs(psr10->port_no));
7429 /* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */
7431 ofputil_bucket_list_destroy(struct ovs_list *buckets)
7433 struct ofputil_bucket *bucket;
7435 LIST_FOR_EACH_POP (bucket, list_node, buckets) {
7436 free(bucket->ofpacts);
7441 /* Clones 'bucket' and its ofpacts data */
7442 static struct ofputil_bucket *
7443 ofputil_bucket_clone_data(const struct ofputil_bucket *bucket)
7445 struct ofputil_bucket *new;
7447 new = xmemdup(bucket, sizeof *bucket);
7448 new->ofpacts = xmemdup(bucket->ofpacts, bucket->ofpacts_len);
7453 /* Clones each of the buckets in the list 'src' appending them
7454 * in turn to 'dest' which should be an initialised list.
7455 * An exception is that if the pointer value of a bucket in 'src'
7456 * matches 'skip' then it is not cloned or appended to 'dest'.
7457 * This allows all of 'src' or 'all of 'src' except 'skip' to
7458 * be cloned and appended to 'dest'. */
7460 ofputil_bucket_clone_list(struct ovs_list *dest, const struct ovs_list *src,
7461 const struct ofputil_bucket *skip)
7463 struct ofputil_bucket *bucket;
7465 LIST_FOR_EACH (bucket, list_node, src) {
7466 struct ofputil_bucket *new_bucket;
7468 if (bucket == skip) {
7472 new_bucket = ofputil_bucket_clone_data(bucket);
7473 list_push_back(dest, &new_bucket->list_node);
7477 /* Find a bucket in the list 'buckets' whose bucket id is 'bucket_id'
7478 * Returns the first bucket found or NULL if no buckets are found. */
7479 struct ofputil_bucket *
7480 ofputil_bucket_find(const struct ovs_list *buckets, uint32_t bucket_id)
7482 struct ofputil_bucket *bucket;
7484 if (bucket_id > OFPG15_BUCKET_MAX) {
7488 LIST_FOR_EACH (bucket, list_node, buckets) {
7489 if (bucket->bucket_id == bucket_id) {
7497 /* Returns true if more than one bucket in the list 'buckets'
7498 * have the same bucket id. Returns false otherwise. */
7500 ofputil_bucket_check_duplicate_id(const struct ovs_list *buckets)
7502 struct ofputil_bucket *i, *j;
7504 LIST_FOR_EACH (i, list_node, buckets) {
7505 LIST_FOR_EACH_REVERSE (j, list_node, buckets) {
7509 if (i->bucket_id == j->bucket_id) {
7518 /* Returns the bucket at the front of the list 'buckets'.
7519 * Undefined if 'buckets is empty. */
7520 struct ofputil_bucket *
7521 ofputil_bucket_list_front(const struct ovs_list *buckets)
7523 static struct ofputil_bucket *bucket;
7525 ASSIGN_CONTAINER(bucket, list_front(buckets), list_node);
7530 /* Returns the bucket at the back of the list 'buckets'.
7531 * Undefined if 'buckets is empty. */
7532 struct ofputil_bucket *
7533 ofputil_bucket_list_back(const struct ovs_list *buckets)
7535 static struct ofputil_bucket *bucket;
7537 ASSIGN_CONTAINER(bucket, list_back(buckets), list_node);
7542 /* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version',
7543 * that requests stats for group 'group_id'. (Use OFPG_ALL to request stats
7546 * Group statistics include packet and byte counts for each group. */
7548 ofputil_encode_group_stats_request(enum ofp_version ofp_version,
7551 struct ofpbuf *request;
7553 switch (ofp_version) {
7555 ovs_fatal(0, "dump-group-stats needs OpenFlow 1.1 or later "
7556 "(\'-O OpenFlow11\')");
7561 case OFP15_VERSION: {
7562 struct ofp11_group_stats_request *req;
7563 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_REQUEST, ofp_version, 0);
7564 req = ofpbuf_put_zeros(request, sizeof *req);
7565 req->group_id = htonl(group_id);
7576 ofputil_uninit_group_desc(struct ofputil_group_desc *gd)
7578 ofputil_bucket_list_destroy(&gd->buckets);
7579 free(&gd->props.fields);
7582 /* Decodes the OpenFlow group description request in 'oh', returning the group
7583 * whose description is requested, or OFPG_ALL if stats for all groups was
7586 ofputil_decode_group_desc_request(const struct ofp_header *oh)
7588 struct ofpbuf request;
7591 ofpbuf_use_const(&request, oh, ntohs(oh->length));
7592 raw = ofpraw_pull_assert(&request);
7593 if (raw == OFPRAW_OFPST11_GROUP_DESC_REQUEST) {
7595 } else if (raw == OFPRAW_OFPST15_GROUP_DESC_REQUEST) {
7596 ovs_be32 *group_id = ofpbuf_pull(&request, sizeof *group_id);
7597 return ntohl(*group_id);
7603 /* Returns an OpenFlow group description request for OpenFlow version
7604 * 'ofp_version', that requests stats for group 'group_id'. Use OFPG_ALL to
7605 * request stats for all groups (OpenFlow 1.4 and earlier always request all
7608 * Group descriptions include the bucket and action configuration for each
7611 ofputil_encode_group_desc_request(enum ofp_version ofp_version,
7614 struct ofpbuf *request;
7616 switch (ofp_version) {
7618 ovs_fatal(0, "dump-groups needs OpenFlow 1.1 or later "
7619 "(\'-O OpenFlow11\')");
7624 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_DESC_REQUEST,
7627 case OFP15_VERSION:{
7628 struct ofp15_group_desc_request *req;
7629 request = ofpraw_alloc(OFPRAW_OFPST15_GROUP_DESC_REQUEST,
7631 req = ofpbuf_put_zeros(request, sizeof *req);
7632 req->group_id = htonl(group_id);
7643 ofputil_group_bucket_counters_to_ofp11(const struct ofputil_group_stats *gs,
7644 struct ofp11_bucket_counter bucket_cnts[])
7648 for (i = 0; i < gs->n_buckets; i++) {
7649 bucket_cnts[i].packet_count = htonll(gs->bucket_stats[i].packet_count);
7650 bucket_cnts[i].byte_count = htonll(gs->bucket_stats[i].byte_count);
7655 ofputil_group_stats_to_ofp11(const struct ofputil_group_stats *gs,
7656 struct ofp11_group_stats *gs11, size_t length,
7657 struct ofp11_bucket_counter bucket_cnts[])
7659 memset(gs11, 0, sizeof *gs11);
7660 gs11->length = htons(length);
7661 gs11->group_id = htonl(gs->group_id);
7662 gs11->ref_count = htonl(gs->ref_count);
7663 gs11->packet_count = htonll(gs->packet_count);
7664 gs11->byte_count = htonll(gs->byte_count);
7665 ofputil_group_bucket_counters_to_ofp11(gs, bucket_cnts);
7669 ofputil_group_stats_to_ofp13(const struct ofputil_group_stats *gs,
7670 struct ofp13_group_stats *gs13, size_t length,
7671 struct ofp11_bucket_counter bucket_cnts[])
7673 ofputil_group_stats_to_ofp11(gs, &gs13->gs, length, bucket_cnts);
7674 gs13->duration_sec = htonl(gs->duration_sec);
7675 gs13->duration_nsec = htonl(gs->duration_nsec);
7679 /* Encodes 'gs' properly for the format of the list of group statistics
7680 * replies already begun in 'replies' and appends it to the list. 'replies'
7681 * must have originally been initialized with ofpmp_init(). */
7683 ofputil_append_group_stats(struct ovs_list *replies,
7684 const struct ofputil_group_stats *gs)
7686 size_t bucket_counter_size;
7687 struct ofp11_bucket_counter *bucket_counters;
7690 bucket_counter_size = gs->n_buckets * sizeof(struct ofp11_bucket_counter);
7692 switch (ofpmp_version(replies)) {
7694 case OFP12_VERSION:{
7695 struct ofp11_group_stats *gs11;
7697 length = sizeof *gs11 + bucket_counter_size;
7698 gs11 = ofpmp_append(replies, length);
7699 bucket_counters = (struct ofp11_bucket_counter *)(gs11 + 1);
7700 ofputil_group_stats_to_ofp11(gs, gs11, length, bucket_counters);
7706 case OFP15_VERSION: {
7707 struct ofp13_group_stats *gs13;
7709 length = sizeof *gs13 + bucket_counter_size;
7710 gs13 = ofpmp_append(replies, length);
7711 bucket_counters = (struct ofp11_bucket_counter *)(gs13 + 1);
7712 ofputil_group_stats_to_ofp13(gs, gs13, length, bucket_counters);
7721 /* Returns an OpenFlow group features request for OpenFlow version
7724 ofputil_encode_group_features_request(enum ofp_version ofp_version)
7726 struct ofpbuf *request = NULL;
7728 switch (ofp_version) {
7731 ovs_fatal(0, "dump-group-features needs OpenFlow 1.2 or later "
7732 "(\'-O OpenFlow12\')");
7737 request = ofpraw_alloc(OFPRAW_OFPST12_GROUP_FEATURES_REQUEST,
7747 /* Returns a OpenFlow message that encodes 'features' properly as a reply to
7748 * group features request 'request'. */
7750 ofputil_encode_group_features_reply(
7751 const struct ofputil_group_features *features,
7752 const struct ofp_header *request)
7754 struct ofp12_group_features_stats *ogf;
7755 struct ofpbuf *reply;
7758 reply = ofpraw_alloc_xid(OFPRAW_OFPST12_GROUP_FEATURES_REPLY,
7759 request->version, request->xid, 0);
7760 ogf = ofpbuf_put_zeros(reply, sizeof *ogf);
7761 ogf->types = htonl(features->types);
7762 ogf->capabilities = htonl(features->capabilities);
7763 for (i = 0; i < OFPGT12_N_TYPES; i++) {
7764 ogf->max_groups[i] = htonl(features->max_groups[i]);
7765 ogf->actions[i] = ofpact_bitmap_to_openflow(features->ofpacts[i],
7772 /* Decodes group features reply 'oh' into 'features'. */
7774 ofputil_decode_group_features_reply(const struct ofp_header *oh,
7775 struct ofputil_group_features *features)
7777 const struct ofp12_group_features_stats *ogf = ofpmsg_body(oh);
7780 features->types = ntohl(ogf->types);
7781 features->capabilities = ntohl(ogf->capabilities);
7782 for (i = 0; i < OFPGT12_N_TYPES; i++) {
7783 features->max_groups[i] = ntohl(ogf->max_groups[i]);
7784 features->ofpacts[i] = ofpact_bitmap_from_openflow(
7785 ogf->actions[i], oh->version);
7789 /* Parse a group status request message into a 32 bit OpenFlow 1.1
7790 * group ID and stores the latter in '*group_id'.
7791 * Returns 0 if successful, otherwise an OFPERR_* number. */
7793 ofputil_decode_group_stats_request(const struct ofp_header *request,
7796 const struct ofp11_group_stats_request *gsr11 = ofpmsg_body(request);
7797 *group_id = ntohl(gsr11->group_id);
7801 /* Converts a group stats reply in 'msg' into an abstract ofputil_group_stats
7802 * in 'gs'. Assigns freshly allocated memory to gs->bucket_stats for the
7803 * caller to eventually free.
7805 * Multiple group stats replies can be packed into a single OpenFlow message.
7806 * Calling this function multiple times for a single 'msg' iterates through the
7807 * replies. The caller must initially leave 'msg''s layer pointers null and
7808 * not modify them between calls.
7810 * Returns 0 if successful, EOF if no replies were left in this 'msg',
7811 * otherwise a positive errno value. */
7813 ofputil_decode_group_stats_reply(struct ofpbuf *msg,
7814 struct ofputil_group_stats *gs)
7816 struct ofp11_bucket_counter *obc;
7817 struct ofp11_group_stats *ogs11;
7824 gs->bucket_stats = NULL;
7825 error = (msg->header ? ofpraw_decode(&raw, msg->header)
7826 : ofpraw_pull(&raw, msg));
7835 if (raw == OFPRAW_OFPST11_GROUP_REPLY) {
7836 base_len = sizeof *ogs11;
7837 ogs11 = ofpbuf_try_pull(msg, sizeof *ogs11);
7838 gs->duration_sec = gs->duration_nsec = UINT32_MAX;
7839 } else if (raw == OFPRAW_OFPST13_GROUP_REPLY) {
7840 struct ofp13_group_stats *ogs13;
7842 base_len = sizeof *ogs13;
7843 ogs13 = ofpbuf_try_pull(msg, sizeof *ogs13);
7846 gs->duration_sec = ntohl(ogs13->duration_sec);
7847 gs->duration_nsec = ntohl(ogs13->duration_nsec);
7856 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
7857 ofpraw_get_name(raw), msg->size);
7858 return OFPERR_OFPBRC_BAD_LEN;
7860 length = ntohs(ogs11->length);
7861 if (length < sizeof base_len) {
7862 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply claims invalid length %"PRIuSIZE,
7863 ofpraw_get_name(raw), length);
7864 return OFPERR_OFPBRC_BAD_LEN;
7867 gs->group_id = ntohl(ogs11->group_id);
7868 gs->ref_count = ntohl(ogs11->ref_count);
7869 gs->packet_count = ntohll(ogs11->packet_count);
7870 gs->byte_count = ntohll(ogs11->byte_count);
7872 gs->n_buckets = (length - base_len) / sizeof *obc;
7873 obc = ofpbuf_try_pull(msg, gs->n_buckets * sizeof *obc);
7875 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
7876 ofpraw_get_name(raw), msg->size);
7877 return OFPERR_OFPBRC_BAD_LEN;
7880 gs->bucket_stats = xmalloc(gs->n_buckets * sizeof *gs->bucket_stats);
7881 for (i = 0; i < gs->n_buckets; i++) {
7882 gs->bucket_stats[i].packet_count = ntohll(obc[i].packet_count);
7883 gs->bucket_stats[i].byte_count = ntohll(obc[i].byte_count);
7890 ofputil_put_ofp11_bucket(const struct ofputil_bucket *bucket,
7891 struct ofpbuf *openflow, enum ofp_version ofp_version)
7893 struct ofp11_bucket *ob;
7896 start = openflow->size;
7897 ofpbuf_put_zeros(openflow, sizeof *ob);
7898 ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
7899 openflow, ofp_version);
7900 ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
7901 ob->len = htons(openflow->size - start);
7902 ob->weight = htons(bucket->weight);
7903 ob->watch_port = ofputil_port_to_ofp11(bucket->watch_port);
7904 ob->watch_group = htonl(bucket->watch_group);
7908 ofputil_put_ofp15_group_bucket_prop_weight(ovs_be16 weight,
7909 struct ofpbuf *openflow)
7912 struct ofp15_group_bucket_prop_weight *prop;
7914 start_ofs = start_property(openflow, OFPGBPT15_WEIGHT);
7915 ofpbuf_put_zeros(openflow, sizeof *prop - sizeof(struct ofp_prop_header));
7916 prop = ofpbuf_at_assert(openflow, start_ofs, sizeof *prop);
7917 prop->weight = weight;
7918 end_property(openflow, start_ofs);
7922 ofputil_put_ofp15_group_bucket_prop_watch(ovs_be32 watch, uint16_t type,
7923 struct ofpbuf *openflow)
7926 struct ofp15_group_bucket_prop_watch *prop;
7928 start_ofs = start_property(openflow, type);
7929 ofpbuf_put_zeros(openflow, sizeof *prop - sizeof(struct ofp_prop_header));
7930 prop = ofpbuf_at_assert(openflow, start_ofs, sizeof *prop);
7931 prop->watch = watch;
7932 end_property(openflow, start_ofs);
7936 ofputil_put_ofp15_bucket(const struct ofputil_bucket *bucket,
7937 uint32_t bucket_id, enum ofp11_group_type group_type,
7938 struct ofpbuf *openflow, enum ofp_version ofp_version)
7940 struct ofp15_bucket *ob;
7941 size_t start, actions_start, actions_len;
7943 start = openflow->size;
7944 ofpbuf_put_zeros(openflow, sizeof *ob);
7946 actions_start = openflow->size;
7947 ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
7948 openflow, ofp_version);
7949 actions_len = openflow->size - actions_start;
7951 if (group_type == OFPGT11_SELECT) {
7952 ofputil_put_ofp15_group_bucket_prop_weight(htons(bucket->weight),
7955 if (bucket->watch_port != OFPP_ANY) {
7956 ovs_be32 port = ofputil_port_to_ofp11(bucket->watch_port);
7957 ofputil_put_ofp15_group_bucket_prop_watch(port,
7958 OFPGBPT15_WATCH_PORT,
7961 if (bucket->watch_group != OFPG_ANY) {
7962 ovs_be32 group = htonl(bucket->watch_group);
7963 ofputil_put_ofp15_group_bucket_prop_watch(group,
7964 OFPGBPT15_WATCH_GROUP,
7968 ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
7969 ob->len = htons(openflow->size - start);
7970 ob->action_array_len = htons(actions_len);
7971 ob->bucket_id = htonl(bucket_id);
7975 ofputil_put_group_prop_ntr_selection_method(enum ofp_version ofp_version,
7976 const struct ofputil_group_props *gp,
7977 struct ofpbuf *openflow)
7979 struct ntr_group_prop_selection_method *prop;
7982 start = openflow->size;
7983 ofpbuf_put_zeros(openflow, sizeof *prop);
7984 oxm_put_field_array(openflow, &gp->fields, ofp_version);
7985 prop = ofpbuf_at_assert(openflow, start, sizeof *prop);
7986 prop->type = htons(OFPGPT15_EXPERIMENTER);
7987 prop->experimenter = htonl(NTR_VENDOR_ID);
7988 prop->exp_type = htonl(NTRT_SELECTION_METHOD);
7989 strcpy(prop->selection_method, gp->selection_method);
7990 prop->selection_method_param = htonll(gp->selection_method_param);
7991 end_property(openflow, start);
7995 ofputil_append_ofp11_group_desc_reply(const struct ofputil_group_desc *gds,
7996 const struct ovs_list *buckets,
7997 struct ovs_list *replies,
7998 enum ofp_version version)
8000 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
8001 struct ofp11_group_desc_stats *ogds;
8002 struct ofputil_bucket *bucket;
8005 start_ogds = reply->size;
8006 ofpbuf_put_zeros(reply, sizeof *ogds);
8007 LIST_FOR_EACH (bucket, list_node, buckets) {
8008 ofputil_put_ofp11_bucket(bucket, reply, version);
8010 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
8011 ogds->length = htons(reply->size - start_ogds);
8012 ogds->type = gds->type;
8013 ogds->group_id = htonl(gds->group_id);
8015 ofpmp_postappend(replies, start_ogds);
8019 ofputil_append_ofp15_group_desc_reply(const struct ofputil_group_desc *gds,
8020 const struct ovs_list *buckets,
8021 struct ovs_list *replies,
8022 enum ofp_version version)
8024 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
8025 struct ofp15_group_desc_stats *ogds;
8026 struct ofputil_bucket *bucket;
8027 size_t start_ogds, start_buckets;
8029 start_ogds = reply->size;
8030 ofpbuf_put_zeros(reply, sizeof *ogds);
8031 start_buckets = reply->size;
8032 LIST_FOR_EACH (bucket, list_node, buckets) {
8033 ofputil_put_ofp15_bucket(bucket, bucket->bucket_id,
8034 gds->type, reply, version);
8036 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
8037 ogds->type = gds->type;
8038 ogds->group_id = htonl(gds->group_id);
8039 ogds->bucket_list_len = htons(reply->size - start_buckets);
8041 /* Add group properties */
8042 if (gds->props.selection_method[0]) {
8043 ofputil_put_group_prop_ntr_selection_method(version, &gds->props,
8046 ogds->length = htons(reply->size - start_ogds);
8048 ofpmp_postappend(replies, start_ogds);
8051 /* Appends a group stats reply that contains the data in 'gds' to those already
8052 * present in the list of ofpbufs in 'replies'. 'replies' should have been
8053 * initialized with ofpmp_init(). */
8055 ofputil_append_group_desc_reply(const struct ofputil_group_desc *gds,
8056 const struct ovs_list *buckets,
8057 struct ovs_list *replies)
8059 enum ofp_version version = ofpmp_version(replies);
8067 ofputil_append_ofp11_group_desc_reply(gds, buckets, replies, version);
8071 ofputil_append_ofp15_group_desc_reply(gds, buckets, replies, version);
8081 ofputil_pull_ofp11_buckets(struct ofpbuf *msg, size_t buckets_length,
8082 enum ofp_version version, struct ovs_list *buckets)
8084 struct ofp11_bucket *ob;
8085 uint32_t bucket_id = 0;
8088 while (buckets_length > 0) {
8089 struct ofputil_bucket *bucket;
8090 struct ofpbuf ofpacts;
8094 ob = (buckets_length >= sizeof *ob
8095 ? ofpbuf_try_pull(msg, sizeof *ob)
8098 VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE" leftover bytes",
8100 return OFPERR_OFPGMFC_BAD_BUCKET;
8103 ob_len = ntohs(ob->len);
8104 if (ob_len < sizeof *ob) {
8105 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8106 "%"PRIuSIZE" is not valid", ob_len);
8107 return OFPERR_OFPGMFC_BAD_BUCKET;
8108 } else if (ob_len > buckets_length) {
8109 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8110 "%"PRIuSIZE" exceeds remaining buckets data size %"PRIuSIZE,
8111 ob_len, buckets_length);
8112 return OFPERR_OFPGMFC_BAD_BUCKET;
8114 buckets_length -= ob_len;
8116 ofpbuf_init(&ofpacts, 0);
8117 error = ofpacts_pull_openflow_actions(msg, ob_len - sizeof *ob,
8120 ofpbuf_uninit(&ofpacts);
8121 ofputil_bucket_list_destroy(buckets);
8125 bucket = xzalloc(sizeof *bucket);
8126 bucket->weight = ntohs(ob->weight);
8127 error = ofputil_port_from_ofp11(ob->watch_port, &bucket->watch_port);
8129 ofpbuf_uninit(&ofpacts);
8130 ofputil_bucket_list_destroy(buckets);
8131 return OFPERR_OFPGMFC_BAD_WATCH;
8133 bucket->watch_group = ntohl(ob->watch_group);
8134 bucket->bucket_id = bucket_id++;
8136 bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
8137 bucket->ofpacts_len = ofpacts.size;
8138 list_push_back(buckets, &bucket->list_node);
8145 parse_ofp15_group_bucket_prop_weight(const struct ofpbuf *payload,
8148 struct ofp15_group_bucket_prop_weight *prop = payload->data;
8150 if (payload->size != sizeof *prop) {
8151 log_property(false, "OpenFlow bucket weight property length "
8152 "%u is not valid", payload->size);
8153 return OFPERR_OFPBPC_BAD_LEN;
8156 *weight = prop->weight;
8162 parse_ofp15_group_bucket_prop_watch(const struct ofpbuf *payload,
8165 struct ofp15_group_bucket_prop_watch *prop = payload->data;
8167 if (payload->size != sizeof *prop) {
8168 log_property(false, "OpenFlow bucket watch port or group "
8169 "property length %u is not valid", payload->size);
8170 return OFPERR_OFPBPC_BAD_LEN;
8173 *watch = prop->watch;
8179 ofputil_pull_ofp15_buckets(struct ofpbuf *msg, size_t buckets_length,
8180 enum ofp_version version, uint8_t group_type,
8181 struct ovs_list *buckets)
8183 struct ofp15_bucket *ob;
8186 while (buckets_length > 0) {
8187 struct ofputil_bucket *bucket = NULL;
8188 struct ofpbuf ofpacts;
8189 enum ofperr err = OFPERR_OFPGMFC_BAD_BUCKET;
8190 struct ofpbuf properties;
8191 size_t ob_len, actions_len, properties_len;
8192 ovs_be32 watch_port = ofputil_port_to_ofp11(OFPP_ANY);
8193 ovs_be32 watch_group = htonl(OFPG_ANY);
8194 ovs_be16 weight = htons(group_type == OFPGT11_SELECT ? 1 : 0);
8196 ofpbuf_init(&ofpacts, 0);
8198 ob = ofpbuf_try_pull(msg, sizeof *ob);
8200 VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE
8201 " leftover bytes", buckets_length);
8205 ob_len = ntohs(ob->len);
8206 actions_len = ntohs(ob->action_array_len);
8208 if (ob_len < sizeof *ob) {
8209 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8210 "%"PRIuSIZE" is not valid", ob_len);
8212 } else if (ob_len > buckets_length) {
8213 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8214 "%"PRIuSIZE" exceeds remaining buckets data size %"
8215 PRIuSIZE, ob_len, buckets_length);
8217 } else if (actions_len > ob_len - sizeof *ob) {
8218 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket actions "
8219 "length %"PRIuSIZE" exceeds remaining bucket "
8220 "data size %"PRIuSIZE, actions_len,
8221 ob_len - sizeof *ob);
8224 buckets_length -= ob_len;
8226 err = ofpacts_pull_openflow_actions(msg, actions_len, version,
8232 properties_len = ob_len - sizeof *ob - actions_len;
8233 ofpbuf_use_const(&properties, ofpbuf_pull(msg, properties_len),
8236 while (properties.size > 0) {
8237 struct ofpbuf payload;
8240 err = ofputil_pull_property(&properties, &payload, &type);
8246 case OFPGBPT15_WEIGHT:
8247 err = parse_ofp15_group_bucket_prop_weight(&payload, &weight);
8250 case OFPGBPT15_WATCH_PORT:
8251 err = parse_ofp15_group_bucket_prop_watch(&payload,
8255 case OFPGBPT15_WATCH_GROUP:
8256 err = parse_ofp15_group_bucket_prop_watch(&payload,
8261 log_property(false, "unknown group bucket property %"PRIu16,
8263 err = OFPERR_OFPBPC_BAD_TYPE;
8272 bucket = xzalloc(sizeof *bucket);
8274 bucket->weight = ntohs(weight);
8275 err = ofputil_port_from_ofp11(watch_port, &bucket->watch_port);
8277 err = OFPERR_OFPGMFC_BAD_WATCH;
8280 bucket->watch_group = ntohl(watch_group);
8281 bucket->bucket_id = ntohl(ob->bucket_id);
8282 if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
8283 VLOG_WARN_RL(&bad_ofmsg_rl, "bucket id (%u) is out of range",
8285 err = OFPERR_OFPGMFC_BAD_BUCKET;
8289 bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
8290 bucket->ofpacts_len = ofpacts.size;
8291 list_push_back(buckets, &bucket->list_node);
8297 ofpbuf_uninit(&ofpacts);
8298 ofputil_bucket_list_destroy(buckets);
8302 if (ofputil_bucket_check_duplicate_id(buckets)) {
8303 VLOG_WARN_RL(&bad_ofmsg_rl, "Duplicate bucket id");
8304 ofputil_bucket_list_destroy(buckets);
8305 return OFPERR_OFPGMFC_BAD_BUCKET;
8312 ofputil_init_group_properties(struct ofputil_group_props *gp)
8314 memset(gp, 0, sizeof *gp);
8318 parse_group_prop_ntr_selection_method(struct ofpbuf *payload,
8319 enum ofp11_group_type group_type,
8320 enum ofp15_group_mod_command group_cmd,
8321 struct ofputil_group_props *gp)
8323 struct ntr_group_prop_selection_method *prop = payload->data;
8324 size_t fields_len, method_len;
8327 switch (group_type) {
8328 case OFPGT11_SELECT:
8331 case OFPGT11_INDIRECT:
8333 log_property(false, "ntr selection method property is only allowed "
8334 "for select groups");
8335 return OFPERR_OFPBPC_BAD_VALUE;
8340 switch (group_cmd) {
8342 case OFPGC15_MODIFY:
8344 case OFPGC15_DELETE:
8345 case OFPGC15_INSERT_BUCKET:
8346 case OFPGC15_REMOVE_BUCKET:
8347 log_property(false, "ntr selection method property is only allowed "
8348 "for add and delete group modifications");
8349 return OFPERR_OFPBPC_BAD_VALUE;
8354 if (payload->size < sizeof *prop) {
8355 log_property(false, "ntr selection method property length "
8356 "%u is not valid", payload->size);
8357 return OFPERR_OFPBPC_BAD_LEN;
8360 method_len = strnlen(prop->selection_method, NTR_MAX_SELECTION_METHOD_LEN);
8362 if (method_len == NTR_MAX_SELECTION_METHOD_LEN) {
8363 log_property(false, "ntr selection method is not null terminated");
8364 return OFPERR_OFPBPC_BAD_VALUE;
8367 if (strcmp("hash", prop->selection_method)) {
8368 log_property(false, "ntr selection method '%s' is not supported",
8369 prop->selection_method);
8370 return OFPERR_OFPBPC_BAD_VALUE;
8373 strcpy(gp->selection_method, prop->selection_method);
8374 gp->selection_method_param = ntohll(prop->selection_method_param);
8376 if (!method_len && gp->selection_method_param) {
8377 log_property(false, "ntr selection method parameter is non-zero but "
8378 "selection method is empty");
8379 return OFPERR_OFPBPC_BAD_VALUE;
8382 ofpbuf_pull(payload, sizeof *prop);
8384 fields_len = ntohs(prop->length) - sizeof *prop;
8385 if (!method_len && fields_len) {
8386 log_property(false, "ntr selection method parameter is zero "
8387 "but fields are provided");
8388 return OFPERR_OFPBPC_BAD_VALUE;
8391 error = oxm_pull_field_array(payload->data, fields_len,
8394 log_property(false, "ntr selection method fields are invalid");
8402 parse_group_prop_ntr(struct ofpbuf *payload, uint32_t exp_type,
8403 enum ofp11_group_type group_type,
8404 enum ofp15_group_mod_command group_cmd,
8405 struct ofputil_group_props *gp)
8410 case NTRT_SELECTION_METHOD:
8411 error = parse_group_prop_ntr_selection_method(payload, group_type,
8416 log_property(false, "unknown group property ntr experimenter type "
8417 "%"PRIu32, exp_type);
8418 error = OFPERR_OFPBPC_BAD_TYPE;
8426 parse_ofp15_group_prop_exp(struct ofpbuf *payload,
8427 enum ofp11_group_type group_type,
8428 enum ofp15_group_mod_command group_cmd,
8429 struct ofputil_group_props *gp)
8431 struct ofp_prop_experimenter *prop = payload->data;
8432 uint16_t experimenter;
8436 if (payload->size < sizeof *prop) {
8437 return OFPERR_OFPBPC_BAD_LEN;
8440 experimenter = ntohl(prop->experimenter);
8441 exp_type = ntohl(prop->exp_type);
8443 switch (experimenter) {
8445 case NTR_COMPAT_VENDOR_ID:
8446 error = parse_group_prop_ntr(payload, exp_type, group_type,
8451 log_property(false, "unknown group property experimenter %"PRIu16,
8453 error = OFPERR_OFPBPC_BAD_EXPERIMENTER;
8461 parse_ofp15_group_properties(struct ofpbuf *msg,
8462 enum ofp11_group_type group_type,
8463 enum ofp15_group_mod_command group_cmd,
8464 struct ofputil_group_props *gp,
8465 size_t properties_len)
8467 struct ofpbuf properties;
8469 ofpbuf_use_const(&properties, ofpbuf_pull(msg, properties_len),
8472 while (properties.size > 0) {
8473 struct ofpbuf payload;
8477 error = ofputil_pull_property(&properties, &payload, &type);
8483 case OFPGPT15_EXPERIMENTER:
8484 error = parse_ofp15_group_prop_exp(&payload, group_type,
8489 log_property(false, "unknown group property %"PRIu16, type);
8490 error = OFPERR_OFPBPC_BAD_TYPE;
8503 ofputil_decode_ofp11_group_desc_reply(struct ofputil_group_desc *gd,
8505 enum ofp_version version)
8507 struct ofp11_group_desc_stats *ogds;
8511 ofpraw_pull_assert(msg);
8518 ogds = ofpbuf_try_pull(msg, sizeof *ogds);
8520 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
8521 "leftover bytes at end", msg->size);
8522 return OFPERR_OFPBRC_BAD_LEN;
8524 gd->type = ogds->type;
8525 gd->group_id = ntohl(ogds->group_id);
8527 length = ntohs(ogds->length);
8528 if (length < sizeof *ogds || length - sizeof *ogds > msg->size) {
8529 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
8530 "length %"PRIuSIZE, length);
8531 return OFPERR_OFPBRC_BAD_LEN;
8534 return ofputil_pull_ofp11_buckets(msg, length - sizeof *ogds, version,
8539 ofputil_decode_ofp15_group_desc_reply(struct ofputil_group_desc *gd,
8541 enum ofp_version version)
8543 struct ofp15_group_desc_stats *ogds;
8544 uint16_t length, bucket_list_len;
8548 ofpraw_pull_assert(msg);
8555 ogds = ofpbuf_try_pull(msg, sizeof *ogds);
8557 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
8558 "leftover bytes at end", msg->size);
8559 return OFPERR_OFPBRC_BAD_LEN;
8561 gd->type = ogds->type;
8562 gd->group_id = ntohl(ogds->group_id);
8564 length = ntohs(ogds->length);
8565 if (length < sizeof *ogds || length - sizeof *ogds > msg->size) {
8566 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
8567 "length %u", length);
8568 return OFPERR_OFPBRC_BAD_LEN;
8571 bucket_list_len = ntohs(ogds->bucket_list_len);
8572 if (length < bucket_list_len + sizeof *ogds) {
8573 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
8574 "bucket list length %u", bucket_list_len);
8575 return OFPERR_OFPBRC_BAD_LEN;
8577 error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, version, gd->type,
8583 /* By definition group desc messages don't have a group mod command.
8584 * However, parse_group_prop_ntr_selection_method() checks to make sure
8585 * that the command is OFPGC15_ADD or OFPGC15_DELETE to guard
8586 * against group mod messages with other commands supplying
8587 * a NTR selection method group experimenter property.
8588 * Such properties are valid for group desc replies so
8589 * claim that the group mod command is OFPGC15_ADD to
8590 * satisfy the check in parse_group_prop_ntr_selection_method() */
8591 return parse_ofp15_group_properties(msg, gd->type, OFPGC15_ADD, &gd->props,
8592 length - sizeof *ogds - bucket_list_len);
8595 /* Converts a group description reply in 'msg' into an abstract
8596 * ofputil_group_desc in 'gd'.
8598 * Multiple group description replies can be packed into a single OpenFlow
8599 * message. Calling this function multiple times for a single 'msg' iterates
8600 * through the replies. The caller must initially leave 'msg''s layer pointers
8601 * null and not modify them between calls.
8603 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8604 * otherwise a positive errno value. */
8606 ofputil_decode_group_desc_reply(struct ofputil_group_desc *gd,
8607 struct ofpbuf *msg, enum ofp_version version)
8609 ofputil_init_group_properties(&gd->props);
8617 return ofputil_decode_ofp11_group_desc_reply(gd, msg, version);
8620 return ofputil_decode_ofp15_group_desc_reply(gd, msg, version);
8629 ofputil_uninit_group_mod(struct ofputil_group_mod *gm)
8631 ofputil_bucket_list_destroy(&gm->buckets);
8634 static struct ofpbuf *
8635 ofputil_encode_ofp11_group_mod(enum ofp_version ofp_version,
8636 const struct ofputil_group_mod *gm)
8639 struct ofp11_group_mod *ogm;
8641 struct ofputil_bucket *bucket;
8643 b = ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD, ofp_version, 0);
8644 start_ogm = b->size;
8645 ofpbuf_put_zeros(b, sizeof *ogm);
8647 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
8648 ofputil_put_ofp11_bucket(bucket, b, ofp_version);
8650 ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
8651 ogm->command = htons(gm->command);
8652 ogm->type = gm->type;
8653 ogm->group_id = htonl(gm->group_id);
8658 static struct ofpbuf *
8659 ofputil_encode_ofp15_group_mod(enum ofp_version ofp_version,
8660 const struct ofputil_group_mod *gm)
8663 struct ofp15_group_mod *ogm;
8665 struct ofputil_bucket *bucket;
8666 struct id_pool *bucket_ids = NULL;
8668 b = ofpraw_alloc(OFPRAW_OFPT15_GROUP_MOD, ofp_version, 0);
8669 start_ogm = b->size;
8670 ofpbuf_put_zeros(b, sizeof *ogm);
8672 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
8675 /* Generate a bucket id if none was supplied */
8676 if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
8678 const struct ofputil_bucket *bkt;
8680 bucket_ids = id_pool_create(0, OFPG15_BUCKET_MAX + 1);
8682 /* Mark all bucket_ids that are present in gm
8683 * as used in the pool. */
8684 LIST_FOR_EACH_REVERSE (bkt, list_node, &gm->buckets) {
8685 if (bkt == bucket) {
8688 if (bkt->bucket_id <= OFPG15_BUCKET_MAX) {
8689 id_pool_add(bucket_ids, bkt->bucket_id);
8694 if (!id_pool_alloc_id(bucket_ids, &bucket_id)) {
8698 bucket_id = bucket->bucket_id;
8701 ofputil_put_ofp15_bucket(bucket, bucket_id, gm->type, b, ofp_version);
8703 ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
8704 ogm->command = htons(gm->command);
8705 ogm->type = gm->type;
8706 ogm->group_id = htonl(gm->group_id);
8707 ogm->command_bucket_id = htonl(gm->command_bucket_id);
8708 ogm->bucket_array_len = htons(b->size - start_ogm - sizeof *ogm);
8710 /* Add group properties */
8711 if (gm->props.selection_method[0]) {
8712 ofputil_put_group_prop_ntr_selection_method(ofp_version, &gm->props, b);
8715 id_pool_destroy(bucket_ids);
8720 bad_group_cmd(enum ofp15_group_mod_command cmd)
8722 const char *opt_version;
8723 const char *version;
8724 const char *cmd_str;
8728 case OFPGC15_MODIFY:
8729 case OFPGC15_DELETE:
8734 case OFPGC15_INSERT_BUCKET:
8735 case OFPGC15_REMOVE_BUCKET:
8746 cmd_str = "add-group";
8749 case OFPGC15_MODIFY:
8750 cmd_str = "mod-group";
8753 case OFPGC15_DELETE:
8754 cmd_str = "del-group";
8757 case OFPGC15_INSERT_BUCKET:
8758 cmd_str = "insert-bucket";
8761 case OFPGC15_REMOVE_BUCKET:
8762 cmd_str = "remove-bucket";
8769 ovs_fatal(0, "%s needs OpenFlow %s or later (\'-O OpenFlow%s\')",
8770 cmd_str, version, opt_version);
8774 /* Converts abstract group mod 'gm' into a message for OpenFlow version
8775 * 'ofp_version' and returns the message. */
8777 ofputil_encode_group_mod(enum ofp_version ofp_version,
8778 const struct ofputil_group_mod *gm)
8781 switch (ofp_version) {
8783 bad_group_cmd(gm->command);
8789 if (gm->command > OFPGC11_DELETE) {
8790 bad_group_cmd(gm->command);
8792 return ofputil_encode_ofp11_group_mod(ofp_version, gm);
8795 return ofputil_encode_ofp15_group_mod(ofp_version, gm);
8803 ofputil_pull_ofp11_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
8804 struct ofputil_group_mod *gm)
8806 const struct ofp11_group_mod *ogm;
8809 ogm = ofpbuf_pull(msg, sizeof *ogm);
8810 gm->command = ntohs(ogm->command);
8811 gm->type = ogm->type;
8812 gm->group_id = ntohl(ogm->group_id);
8813 gm->command_bucket_id = OFPG15_BUCKET_ALL;
8815 error = ofputil_pull_ofp11_buckets(msg, msg->size, ofp_version,
8818 /* OF1.3.5+ prescribes an error when an OFPGC_DELETE includes buckets. */
8820 && ofp_version >= OFP13_VERSION
8821 && gm->command == OFPGC11_DELETE
8822 && !list_is_empty(&gm->buckets)) {
8823 error = OFPERR_OFPGMFC_INVALID_GROUP;
8830 ofputil_pull_ofp15_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
8831 struct ofputil_group_mod *gm)
8833 const struct ofp15_group_mod *ogm;
8834 uint16_t bucket_list_len;
8835 enum ofperr error = OFPERR_OFPGMFC_BAD_BUCKET;
8837 ogm = ofpbuf_pull(msg, sizeof *ogm);
8838 gm->command = ntohs(ogm->command);
8839 gm->type = ogm->type;
8840 gm->group_id = ntohl(ogm->group_id);
8842 gm->command_bucket_id = ntohl(ogm->command_bucket_id);
8843 switch (gm->command) {
8844 case OFPGC15_REMOVE_BUCKET:
8845 if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
8849 case OFPGC15_INSERT_BUCKET:
8850 if (gm->command_bucket_id <= OFPG15_BUCKET_MAX ||
8851 gm->command_bucket_id == OFPG15_BUCKET_FIRST
8852 || gm->command_bucket_id == OFPG15_BUCKET_LAST) {
8858 case OFPGC11_MODIFY:
8859 case OFPGC11_DELETE:
8861 if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
8867 VLOG_WARN_RL(&bad_ofmsg_rl,
8868 "group command bucket id (%u) is out of range",
8869 gm->command_bucket_id);
8870 return OFPERR_OFPGMFC_BAD_BUCKET;
8873 bucket_list_len = ntohs(ogm->bucket_array_len);
8874 error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, ofp_version,
8875 gm->type, &gm->buckets);
8880 return parse_ofp15_group_properties(msg, gm->type, gm->command, &gm->props,
8884 /* Converts OpenFlow group mod message 'oh' into an abstract group mod in
8885 * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */
8887 ofputil_decode_group_mod(const struct ofp_header *oh,
8888 struct ofputil_group_mod *gm)
8890 enum ofp_version ofp_version = oh->version;
8892 struct ofputil_bucket *bucket;
8895 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
8896 ofpraw_pull_assert(&msg);
8898 ofputil_init_group_properties(&gm->props);
8900 switch (ofp_version)
8906 err = ofputil_pull_ofp11_group_mod(&msg, ofp_version, gm);
8910 err = ofputil_pull_ofp15_group_mod(&msg, ofp_version, gm);
8923 case OFPGT11_INDIRECT:
8924 if (!list_is_singleton(&gm->buckets)) {
8925 return OFPERR_OFPGMFC_INVALID_GROUP;
8929 case OFPGT11_SELECT:
8933 return OFPERR_OFPGMFC_BAD_TYPE;
8936 switch (gm->command) {
8938 case OFPGC11_MODIFY:
8939 case OFPGC11_DELETE:
8940 case OFPGC15_INSERT_BUCKET:
8942 case OFPGC15_REMOVE_BUCKET:
8943 if (!list_is_empty(&gm->buckets)) {
8944 return OFPERR_OFPGMFC_BAD_BUCKET;
8948 return OFPERR_OFPGMFC_BAD_COMMAND;
8951 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
8952 if (bucket->weight && gm->type != OFPGT11_SELECT) {
8953 return OFPERR_OFPGMFC_INVALID_GROUP;
8958 case OFPGT11_INDIRECT:
8959 if (ofputil_bucket_has_liveness(bucket)) {
8960 return OFPERR_OFPGMFC_WATCH_UNSUPPORTED;
8963 case OFPGT11_SELECT:
8966 if (!ofputil_bucket_has_liveness(bucket)) {
8967 return OFPERR_OFPGMFC_INVALID_GROUP;
8971 /* Returning BAD TYPE to be consistent
8972 * though gm->type has been checked already. */
8973 return OFPERR_OFPGMFC_BAD_TYPE;
8980 /* Parse a queue status request message into 'oqsr'.
8981 * Returns 0 if successful, otherwise an OFPERR_* number. */
8983 ofputil_decode_queue_stats_request(const struct ofp_header *request,
8984 struct ofputil_queue_stats_request *oqsr)
8986 switch ((enum ofp_version)request->version) {
8991 case OFP11_VERSION: {
8992 const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request);
8993 oqsr->queue_id = ntohl(qsr11->queue_id);
8994 return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no);
8997 case OFP10_VERSION: {
8998 const struct ofp10_queue_stats_request *qsr10 = ofpmsg_body(request);
8999 oqsr->queue_id = ntohl(qsr10->queue_id);
9000 oqsr->port_no = u16_to_ofp(ntohs(qsr10->port_no));
9001 /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
9002 if (oqsr->port_no == OFPP_ALL) {
9003 oqsr->port_no = OFPP_ANY;
9013 /* Encode a queue stats request for 'oqsr', the encoded message
9014 * will be for OpenFlow version 'ofp_version'. Returns message
9015 * as a struct ofpbuf. Returns encoded message on success, NULL on error. */
9017 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
9018 const struct ofputil_queue_stats_request *oqsr)
9020 struct ofpbuf *request;
9022 switch (ofp_version) {
9027 case OFP15_VERSION: {
9028 struct ofp11_queue_stats_request *req;
9029 request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0);
9030 req = ofpbuf_put_zeros(request, sizeof *req);
9031 req->port_no = ofputil_port_to_ofp11(oqsr->port_no);
9032 req->queue_id = htonl(oqsr->queue_id);
9035 case OFP10_VERSION: {
9036 struct ofp10_queue_stats_request *req;
9037 request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0);
9038 req = ofpbuf_put_zeros(request, sizeof *req);
9039 /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
9040 req->port_no = htons(ofp_to_u16(oqsr->port_no == OFPP_ANY
9041 ? OFPP_ALL : oqsr->port_no));
9042 req->queue_id = htonl(oqsr->queue_id);
9052 /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
9055 ofputil_count_queue_stats(const struct ofp_header *oh)
9057 struct ofputil_queue_stats qs;
9061 ofpbuf_use_const(&b, oh, ntohs(oh->length));
9062 ofpraw_pull_assert(&b);
9063 while (!ofputil_decode_queue_stats(&qs, &b)) {
9070 ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs,
9071 const struct ofp10_queue_stats *qs10)
9073 oqs->port_no = u16_to_ofp(ntohs(qs10->port_no));
9074 oqs->queue_id = ntohl(qs10->queue_id);
9075 oqs->tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes));
9076 oqs->tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets));
9077 oqs->tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors));
9078 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
9084 ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs,
9085 const struct ofp11_queue_stats *qs11)
9089 error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no);
9094 oqs->queue_id = ntohl(qs11->queue_id);
9095 oqs->tx_bytes = ntohll(qs11->tx_bytes);
9096 oqs->tx_packets = ntohll(qs11->tx_packets);
9097 oqs->tx_errors = ntohll(qs11->tx_errors);
9098 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
9104 ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats *oqs,
9105 const struct ofp13_queue_stats *qs13)
9107 enum ofperr error = ofputil_queue_stats_from_ofp11(oqs, &qs13->qs);
9109 oqs->duration_sec = ntohl(qs13->duration_sec);
9110 oqs->duration_nsec = ntohl(qs13->duration_nsec);
9117 ofputil_pull_ofp14_queue_stats(struct ofputil_queue_stats *oqs,
9120 const struct ofp14_queue_stats *qs14;
9123 qs14 = ofpbuf_try_pull(msg, sizeof *qs14);
9125 return OFPERR_OFPBRC_BAD_LEN;
9128 len = ntohs(qs14->length);
9129 if (len < sizeof *qs14 || len - sizeof *qs14 > msg->size) {
9130 return OFPERR_OFPBRC_BAD_LEN;
9132 ofpbuf_pull(msg, len - sizeof *qs14);
9134 /* No properties yet defined, so ignore them for now. */
9136 return ofputil_queue_stats_from_ofp13(oqs, &qs14->qs);
9139 /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
9140 * ofputil_queue_stats in 'qs'.
9142 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
9143 * message. Calling this function multiple times for a single 'msg' iterates
9144 * through the replies. The caller must initially leave 'msg''s layer pointers
9145 * null and not modify them between calls.
9147 * Returns 0 if successful, EOF if no replies were left in this 'msg',
9148 * otherwise a positive errno value. */
9150 ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg)
9155 error = (msg->header ? ofpraw_decode(&raw, msg->header)
9156 : ofpraw_pull(&raw, msg));
9163 } else if (raw == OFPRAW_OFPST14_QUEUE_REPLY) {
9164 return ofputil_pull_ofp14_queue_stats(qs, msg);
9165 } else if (raw == OFPRAW_OFPST13_QUEUE_REPLY) {
9166 const struct ofp13_queue_stats *qs13;
9168 qs13 = ofpbuf_try_pull(msg, sizeof *qs13);
9172 return ofputil_queue_stats_from_ofp13(qs, qs13);
9173 } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) {
9174 const struct ofp11_queue_stats *qs11;
9176 qs11 = ofpbuf_try_pull(msg, sizeof *qs11);
9180 return ofputil_queue_stats_from_ofp11(qs, qs11);
9181 } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) {
9182 const struct ofp10_queue_stats *qs10;
9184 qs10 = ofpbuf_try_pull(msg, sizeof *qs10);
9188 return ofputil_queue_stats_from_ofp10(qs, qs10);
9194 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %"PRIu32" leftover "
9195 "bytes at end", msg->size);
9196 return OFPERR_OFPBRC_BAD_LEN;
9200 ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs,
9201 struct ofp10_queue_stats *qs10)
9203 qs10->port_no = htons(ofp_to_u16(oqs->port_no));
9204 memset(qs10->pad, 0, sizeof qs10->pad);
9205 qs10->queue_id = htonl(oqs->queue_id);
9206 put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->tx_bytes));
9207 put_32aligned_be64(&qs10->tx_packets, htonll(oqs->tx_packets));
9208 put_32aligned_be64(&qs10->tx_errors, htonll(oqs->tx_errors));
9212 ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs,
9213 struct ofp11_queue_stats *qs11)
9215 qs11->port_no = ofputil_port_to_ofp11(oqs->port_no);
9216 qs11->queue_id = htonl(oqs->queue_id);
9217 qs11->tx_bytes = htonll(oqs->tx_bytes);
9218 qs11->tx_packets = htonll(oqs->tx_packets);
9219 qs11->tx_errors = htonll(oqs->tx_errors);
9223 ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats *oqs,
9224 struct ofp13_queue_stats *qs13)
9226 ofputil_queue_stats_to_ofp11(oqs, &qs13->qs);
9227 if (oqs->duration_sec != UINT32_MAX) {
9228 qs13->duration_sec = htonl(oqs->duration_sec);
9229 qs13->duration_nsec = htonl(oqs->duration_nsec);
9231 qs13->duration_sec = OVS_BE32_MAX;
9232 qs13->duration_nsec = OVS_BE32_MAX;
9237 ofputil_queue_stats_to_ofp14(const struct ofputil_queue_stats *oqs,
9238 struct ofp14_queue_stats *qs14)
9240 qs14->length = htons(sizeof *qs14);
9241 memset(qs14->pad, 0, sizeof qs14->pad);
9242 ofputil_queue_stats_to_ofp13(oqs, &qs14->qs);
9246 /* Encode a queue stat for 'oqs' and append it to 'replies'. */
9248 ofputil_append_queue_stat(struct ovs_list *replies,
9249 const struct ofputil_queue_stats *oqs)
9251 switch (ofpmp_version(replies)) {
9252 case OFP13_VERSION: {
9253 struct ofp13_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
9254 ofputil_queue_stats_to_ofp13(oqs, reply);
9259 case OFP11_VERSION: {
9260 struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
9261 ofputil_queue_stats_to_ofp11(oqs, reply);
9265 case OFP10_VERSION: {
9266 struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
9267 ofputil_queue_stats_to_ofp10(oqs, reply);
9272 case OFP15_VERSION: {
9273 struct ofp14_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
9274 ofputil_queue_stats_to_ofp14(oqs, reply);
9284 ofputil_decode_bundle_ctrl(const struct ofp_header *oh,
9285 struct ofputil_bundle_ctrl_msg *msg)
9289 const struct ofp14_bundle_ctrl_msg *m;
9291 ofpbuf_use_const(&b, oh, ntohs(oh->length));
9292 raw = ofpraw_pull_assert(&b);
9293 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_CONTROL);
9296 msg->bundle_id = ntohl(m->bundle_id);
9297 msg->type = ntohs(m->type);
9298 msg->flags = ntohs(m->flags);
9304 ofputil_encode_bundle_ctrl_request(enum ofp_version ofp_version,
9305 struct ofputil_bundle_ctrl_msg *bc)
9307 struct ofpbuf *request;
9308 struct ofp14_bundle_ctrl_msg *m;
9310 switch (ofp_version) {
9315 ovs_fatal(0, "bundles need OpenFlow 1.4 or later "
9316 "(\'-O OpenFlow14\')");
9319 request = ofpraw_alloc(OFPRAW_OFPT14_BUNDLE_CONTROL, ofp_version, 0);
9320 m = ofpbuf_put_zeros(request, sizeof *m);
9322 m->bundle_id = htonl(bc->bundle_id);
9323 m->type = htons(bc->type);
9324 m->flags = htons(bc->flags);
9334 ofputil_encode_bundle_ctrl_reply(const struct ofp_header *oh,
9335 struct ofputil_bundle_ctrl_msg *msg)
9338 struct ofp14_bundle_ctrl_msg *m;
9340 buf = ofpraw_alloc_reply(OFPRAW_OFPT14_BUNDLE_CONTROL, oh, 0);
9341 m = ofpbuf_put_zeros(buf, sizeof *m);
9343 m->bundle_id = htonl(msg->bundle_id);
9344 m->type = htons(msg->type);
9345 m->flags = htons(msg->flags);
9350 /* Return true for bundlable state change requests, false for other messages.
9353 ofputil_is_bundlable(enum ofptype type)
9356 /* Minimum required by OpenFlow 1.4. */
9357 case OFPTYPE_PORT_MOD:
9358 case OFPTYPE_FLOW_MOD:
9361 /* Nice to have later. */
9362 case OFPTYPE_FLOW_MOD_TABLE_ID:
9363 case OFPTYPE_GROUP_MOD:
9364 case OFPTYPE_TABLE_MOD:
9365 case OFPTYPE_METER_MOD:
9366 case OFPTYPE_PACKET_OUT:
9367 case OFPTYPE_NXT_TLV_TABLE_MOD:
9369 /* Not to be bundlable. */
9370 case OFPTYPE_ECHO_REQUEST:
9371 case OFPTYPE_FEATURES_REQUEST:
9372 case OFPTYPE_GET_CONFIG_REQUEST:
9373 case OFPTYPE_SET_CONFIG:
9374 case OFPTYPE_BARRIER_REQUEST:
9375 case OFPTYPE_ROLE_REQUEST:
9376 case OFPTYPE_ECHO_REPLY:
9377 case OFPTYPE_SET_FLOW_FORMAT:
9378 case OFPTYPE_SET_PACKET_IN_FORMAT:
9379 case OFPTYPE_SET_CONTROLLER_ID:
9380 case OFPTYPE_FLOW_AGE:
9381 case OFPTYPE_FLOW_MONITOR_CANCEL:
9382 case OFPTYPE_SET_ASYNC_CONFIG:
9383 case OFPTYPE_GET_ASYNC_REQUEST:
9384 case OFPTYPE_DESC_STATS_REQUEST:
9385 case OFPTYPE_FLOW_STATS_REQUEST:
9386 case OFPTYPE_AGGREGATE_STATS_REQUEST:
9387 case OFPTYPE_TABLE_STATS_REQUEST:
9388 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
9389 case OFPTYPE_TABLE_DESC_REQUEST:
9390 case OFPTYPE_PORT_STATS_REQUEST:
9391 case OFPTYPE_QUEUE_STATS_REQUEST:
9392 case OFPTYPE_PORT_DESC_STATS_REQUEST:
9393 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
9394 case OFPTYPE_METER_STATS_REQUEST:
9395 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
9396 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
9397 case OFPTYPE_GROUP_STATS_REQUEST:
9398 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
9399 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
9400 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
9401 case OFPTYPE_BUNDLE_CONTROL:
9402 case OFPTYPE_BUNDLE_ADD_MESSAGE:
9405 case OFPTYPE_FEATURES_REPLY:
9406 case OFPTYPE_GET_CONFIG_REPLY:
9407 case OFPTYPE_PACKET_IN:
9408 case OFPTYPE_FLOW_REMOVED:
9409 case OFPTYPE_PORT_STATUS:
9410 case OFPTYPE_BARRIER_REPLY:
9411 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
9412 case OFPTYPE_DESC_STATS_REPLY:
9413 case OFPTYPE_FLOW_STATS_REPLY:
9414 case OFPTYPE_QUEUE_STATS_REPLY:
9415 case OFPTYPE_PORT_STATS_REPLY:
9416 case OFPTYPE_TABLE_STATS_REPLY:
9417 case OFPTYPE_AGGREGATE_STATS_REPLY:
9418 case OFPTYPE_PORT_DESC_STATS_REPLY:
9419 case OFPTYPE_ROLE_REPLY:
9420 case OFPTYPE_FLOW_MONITOR_PAUSED:
9421 case OFPTYPE_FLOW_MONITOR_RESUMED:
9422 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
9423 case OFPTYPE_GET_ASYNC_REPLY:
9424 case OFPTYPE_GROUP_STATS_REPLY:
9425 case OFPTYPE_GROUP_DESC_STATS_REPLY:
9426 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
9427 case OFPTYPE_METER_STATS_REPLY:
9428 case OFPTYPE_METER_CONFIG_STATS_REPLY:
9429 case OFPTYPE_METER_FEATURES_STATS_REPLY:
9430 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
9431 case OFPTYPE_TABLE_DESC_REPLY:
9432 case OFPTYPE_ROLE_STATUS:
9433 case OFPTYPE_REQUESTFORWARD:
9434 case OFPTYPE_NXT_TLV_TABLE_REQUEST:
9435 case OFPTYPE_NXT_TLV_TABLE_REPLY:
9443 ofputil_decode_bundle_add(const struct ofp_header *oh,
9444 struct ofputil_bundle_add_msg *msg,
9445 enum ofptype *type_ptr)
9447 const struct ofp14_bundle_ctrl_msg *m;
9454 ofpbuf_use_const(&b, oh, ntohs(oh->length));
9455 raw = ofpraw_pull_assert(&b);
9456 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE);
9458 m = ofpbuf_pull(&b, sizeof *m);
9459 msg->bundle_id = ntohl(m->bundle_id);
9460 msg->flags = ntohs(m->flags);
9463 if (msg->msg->version != oh->version) {
9464 return OFPERR_NXBFC_BAD_VERSION;
9466 inner_len = ntohs(msg->msg->length);
9467 if (inner_len < sizeof(struct ofp_header) || inner_len > b.size) {
9468 return OFPERR_OFPBFC_MSG_BAD_LEN;
9470 if (msg->msg->xid != oh->xid) {
9471 return OFPERR_OFPBFC_MSG_BAD_XID;
9474 /* Reject unbundlable messages. */
9478 error = ofptype_decode(type_ptr, msg->msg);
9480 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPT14_BUNDLE_ADD_MESSAGE contained "
9481 "message is unparsable (%s)", ofperr_get_name(error));
9482 return OFPERR_OFPBFC_MSG_UNSUP; /* 'error' would be confusing. */
9485 if (!ofputil_is_bundlable(*type_ptr)) {
9486 VLOG_WARN_RL(&bad_ofmsg_rl, "%s message not allowed inside "
9487 "OFPT14_BUNDLE_ADD_MESSAGE", ofptype_get_name(*type_ptr));
9488 return OFPERR_OFPBFC_MSG_UNSUP;
9495 ofputil_encode_bundle_add(enum ofp_version ofp_version,
9496 struct ofputil_bundle_add_msg *msg)
9498 struct ofpbuf *request;
9499 struct ofp14_bundle_ctrl_msg *m;
9501 /* Must use the same xid as the embedded message. */
9502 request = ofpraw_alloc_xid(OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE, ofp_version,
9504 m = ofpbuf_put_zeros(request, sizeof *m);
9506 m->bundle_id = htonl(msg->bundle_id);
9507 m->flags = htons(msg->flags);
9508 ofpbuf_put(request, msg->msg, ntohs(msg->msg->length));
9514 encode_tlv_table_mappings(struct ofpbuf *b, struct ovs_list *mappings)
9516 struct ofputil_tlv_map *map;
9518 LIST_FOR_EACH (map, list_node, mappings) {
9519 struct nx_tlv_map *nx_map;
9521 nx_map = ofpbuf_put_zeros(b, sizeof *nx_map);
9522 nx_map->option_class = htons(map->option_class);
9523 nx_map->option_type = map->option_type;
9524 nx_map->option_len = map->option_len;
9525 nx_map->index = htons(map->index);
9530 ofputil_encode_tlv_table_mod(enum ofp_version ofp_version,
9531 struct ofputil_tlv_table_mod *ttm)
9534 struct nx_tlv_table_mod *nx_ttm;
9536 b = ofpraw_alloc(OFPRAW_NXT_TLV_TABLE_MOD, ofp_version, 0);
9537 nx_ttm = ofpbuf_put_zeros(b, sizeof *nx_ttm);
9538 nx_ttm->command = htons(ttm->command);
9539 encode_tlv_table_mappings(b, &ttm->mappings);
9545 decode_tlv_table_mappings(struct ofpbuf *msg, unsigned int max_fields,
9546 struct ovs_list *mappings)
9548 list_init(mappings);
9551 struct nx_tlv_map *nx_map;
9552 struct ofputil_tlv_map *map;
9554 nx_map = ofpbuf_pull(msg, sizeof *nx_map);
9555 map = xmalloc(sizeof *map);
9556 list_push_back(mappings, &map->list_node);
9558 map->option_class = ntohs(nx_map->option_class);
9559 map->option_type = nx_map->option_type;
9561 map->option_len = nx_map->option_len;
9562 if (map->option_len % 4 || map->option_len > TLV_MAX_OPT_SIZE) {
9563 VLOG_WARN_RL(&bad_ofmsg_rl,
9564 "tlv table option length (%u) is not a valid option size",
9566 ofputil_uninit_tlv_table(mappings);
9567 return OFPERR_NXTTMFC_BAD_OPT_LEN;
9570 map->index = ntohs(nx_map->index);
9571 if (map->index >= max_fields) {
9572 VLOG_WARN_RL(&bad_ofmsg_rl,
9573 "tlv table field index (%u) is too large (max %u)",
9574 map->index, max_fields - 1);
9575 ofputil_uninit_tlv_table(mappings);
9576 return OFPERR_NXTTMFC_BAD_FIELD_IDX;
9584 ofputil_decode_tlv_table_mod(const struct ofp_header *oh,
9585 struct ofputil_tlv_table_mod *ttm)
9588 struct nx_tlv_table_mod *nx_ttm;
9590 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
9591 ofpraw_pull_assert(&msg);
9593 nx_ttm = ofpbuf_pull(&msg, sizeof *nx_ttm);
9594 ttm->command = ntohs(nx_ttm->command);
9595 if (ttm->command > NXTTMC_CLEAR) {
9596 VLOG_WARN_RL(&bad_ofmsg_rl,
9597 "tlv table mod command (%u) is out of range",
9599 return OFPERR_NXTTMFC_BAD_COMMAND;
9602 return decode_tlv_table_mappings(&msg, TUN_METADATA_NUM_OPTS,
9607 ofputil_encode_tlv_table_reply(const struct ofp_header *oh,
9608 struct ofputil_tlv_table_reply *ttr)
9611 struct nx_tlv_table_reply *nx_ttr;
9613 b = ofpraw_alloc_reply(OFPRAW_NXT_TLV_TABLE_REPLY, oh, 0);
9614 nx_ttr = ofpbuf_put_zeros(b, sizeof *nx_ttr);
9615 nx_ttr->max_option_space = htonl(ttr->max_option_space);
9616 nx_ttr->max_fields = htons(ttr->max_fields);
9618 encode_tlv_table_mappings(b, &ttr->mappings);
9623 /* Decodes the NXT_TLV_TABLE_REPLY message in 'oh' into '*ttr'. Returns 0
9624 * if successful, otherwise an ofperr.
9626 * The decoder verifies that the indexes in 'ttr->mappings' are less than
9627 * 'ttr->max_fields', but the caller must ensure, if necessary, that they are
9628 * less than TUN_METADATA_NUM_OPTS. */
9630 ofputil_decode_tlv_table_reply(const struct ofp_header *oh,
9631 struct ofputil_tlv_table_reply *ttr)
9634 struct nx_tlv_table_reply *nx_ttr;
9636 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
9637 ofpraw_pull_assert(&msg);
9639 nx_ttr = ofpbuf_pull(&msg, sizeof *nx_ttr);
9640 ttr->max_option_space = ntohl(nx_ttr->max_option_space);
9641 ttr->max_fields = ntohs(nx_ttr->max_fields);
9643 return decode_tlv_table_mappings(&msg, ttr->max_fields, &ttr->mappings);
9647 ofputil_uninit_tlv_table(struct ovs_list *mappings)
9649 struct ofputil_tlv_map *map;
9651 LIST_FOR_EACH_POP (map, list_node, mappings) {
9656 /* Decodes the OpenFlow "set async config" request and "get async config
9657 * reply" message in '*oh' into an abstract form in 'master' and 'slave'.
9659 * If 'loose' is true, this function ignores properties and values that it does
9660 * not understand, as a controller would want to do when interpreting
9661 * capabilities provided by a switch. If 'loose' is false, this function
9662 * treats unknown properties and values as an error, as a switch would want to
9663 * do when interpreting a configuration request made by a controller.
9665 * Returns 0 if successful, otherwise an OFPERR_* value.
9667 * Returns error code OFPERR_OFPACFC_INVALID if the value of mask is not in
9668 * the valid range of mask.
9670 * Returns error code OFPERR_OFPACFC_UNSUPPORTED if the configuration is not
9673 ofputil_decode_set_async_config(const struct ofp_header *oh,
9674 uint32_t master[OAM_N_TYPES],
9675 uint32_t slave[OAM_N_TYPES],
9681 ofpbuf_use_const(&b, oh, ntohs(oh->length));
9682 raw = ofpraw_pull_assert(&b);
9684 if (raw == OFPRAW_OFPT13_SET_ASYNC ||
9685 raw == OFPRAW_NXT_SET_ASYNC_CONFIG ||
9686 raw == OFPRAW_OFPT13_GET_ASYNC_REPLY) {
9687 const struct nx_async_config *msg = ofpmsg_body(oh);
9689 master[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[0]);
9690 master[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[0]);
9691 master[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[0]);
9693 slave[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[1]);
9694 slave[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[1]);
9695 slave[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[1]);
9697 } else if (raw == OFPRAW_OFPT14_SET_ASYNC ||
9698 raw == OFPRAW_OFPT14_GET_ASYNC_REPLY) {
9700 while (b.size > 0) {
9701 struct ofp14_async_config_prop_reasons *msg;
9702 struct ofpbuf property;
9706 error = ofputil_pull_property(&b, &property, &type);
9711 msg = property.data;
9713 if (property.size != sizeof *msg) {
9714 return OFPERR_OFPBRC_BAD_LEN;
9718 error = ofputil_check_mask(type, ntohl(msg->mask));
9725 case OFPACPT_PACKET_IN_SLAVE:
9726 slave[OAM_PACKET_IN] = ntohl(msg->mask);
9729 case OFPACPT_PACKET_IN_MASTER:
9730 master[OAM_PACKET_IN] = ntohl(msg->mask);
9733 case OFPACPT_PORT_STATUS_SLAVE:
9734 slave[OAM_PORT_STATUS] = ntohl(msg->mask);
9737 case OFPACPT_PORT_STATUS_MASTER:
9738 master[OAM_PORT_STATUS] = ntohl(msg->mask);
9741 case OFPACPT_FLOW_REMOVED_SLAVE:
9742 slave[OAM_FLOW_REMOVED] = ntohl(msg->mask);
9745 case OFPACPT_FLOW_REMOVED_MASTER:
9746 master[OAM_FLOW_REMOVED] = ntohl(msg->mask);
9749 case OFPACPT_ROLE_STATUS_SLAVE:
9750 slave[OAM_ROLE_STATUS] = ntohl(msg->mask);
9753 case OFPACPT_ROLE_STATUS_MASTER:
9754 master[OAM_ROLE_STATUS] = ntohl(msg->mask);
9757 case OFPACPT_TABLE_STATUS_SLAVE:
9758 slave[OAM_TABLE_STATUS] = ntohl(msg->mask);
9761 case OFPACPT_TABLE_STATUS_MASTER:
9762 master[OAM_TABLE_STATUS] = ntohl(msg->mask);
9765 case OFPACPT_REQUESTFORWARD_SLAVE:
9766 slave[OAM_REQUESTFORWARD] = ntohl(msg->mask);
9769 case OFPACPT_REQUESTFORWARD_MASTER:
9770 master[OAM_REQUESTFORWARD] = ntohl(msg->mask);
9774 error = loose ? 0 : OFPERR_OFPACFC_UNSUPPORTED;
9779 return OFPERR_OFPBRC_BAD_VERSION;
9784 /* Append all asynchronous configuration properties in GET_ASYNC_REPLY
9785 * message, describing if various set of asynchronous messages are enabled
9788 ofputil_get_async_reply(struct ofpbuf *buf, const uint32_t master_mask,
9789 const uint32_t slave_mask, const uint32_t type)
9793 for (role = 0; role < 2; role++) {
9794 struct ofp14_async_config_prop_reasons *msg;
9796 msg = ofpbuf_put_zeros(buf, sizeof *msg);
9800 msg->type = (role ? htons(OFPACPT_PACKET_IN_SLAVE)
9801 : htons(OFPACPT_PACKET_IN_MASTER));
9804 case OAM_PORT_STATUS:
9805 msg->type = (role ? htons(OFPACPT_PORT_STATUS_SLAVE)
9806 : htons(OFPACPT_PORT_STATUS_MASTER));
9809 case OAM_FLOW_REMOVED:
9810 msg->type = (role ? htons(OFPACPT_FLOW_REMOVED_SLAVE)
9811 : htons(OFPACPT_FLOW_REMOVED_MASTER));
9814 case OAM_ROLE_STATUS:
9815 msg->type = (role ? htons(OFPACPT_ROLE_STATUS_SLAVE)
9816 : htons(OFPACPT_ROLE_STATUS_MASTER));
9819 case OAM_TABLE_STATUS:
9820 msg->type = (role ? htons(OFPACPT_TABLE_STATUS_SLAVE)
9821 : htons(OFPACPT_TABLE_STATUS_MASTER));
9824 case OAM_REQUESTFORWARD:
9825 msg->type = (role ? htons(OFPACPT_REQUESTFORWARD_SLAVE)
9826 : htons(OFPACPT_REQUESTFORWARD_MASTER));
9830 return OFPERR_OFPBRC_BAD_TYPE;
9832 msg->length = htons(sizeof *msg);
9833 msg->mask = (role ? htonl(slave_mask) : htonl(master_mask));
9839 /* Returns a OpenFlow message that encodes 'asynchronous configuration' properly
9840 * as a reply to get async config request. */
9842 ofputil_encode_get_async_config(const struct ofp_header *oh,
9843 uint32_t master[OAM_N_TYPES],
9844 uint32_t slave[OAM_N_TYPES])
9849 buf = ofpraw_alloc_reply((oh->version < OFP14_VERSION
9850 ? OFPRAW_OFPT13_GET_ASYNC_REPLY
9851 : OFPRAW_OFPT14_GET_ASYNC_REPLY), oh, 0);
9853 if (oh->version < OFP14_VERSION) {
9854 struct nx_async_config *msg;
9855 msg = ofpbuf_put_zeros(buf, sizeof *msg);
9857 msg->packet_in_mask[0] = htonl(master[OAM_PACKET_IN]);
9858 msg->port_status_mask[0] = htonl(master[OAM_PORT_STATUS]);
9859 msg->flow_removed_mask[0] = htonl(master[OAM_FLOW_REMOVED]);
9861 msg->packet_in_mask[1] = htonl(slave[OAM_PACKET_IN]);
9862 msg->port_status_mask[1] = htonl(slave[OAM_PORT_STATUS]);
9863 msg->flow_removed_mask[1] = htonl(slave[OAM_FLOW_REMOVED]);
9864 } else if (oh->version == OFP14_VERSION) {
9865 for (type = 0; type < OAM_N_TYPES; type++) {
9866 ofputil_get_async_reply(buf, master[type], slave[type], type);