2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 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_eviction ofputil_decode_table_eviction(
57 ovs_be32 config, enum ofp_version);
58 static ovs_be32 ofputil_encode_table_config(enum ofputil_table_miss,
59 enum ofputil_table_eviction,
62 struct ofp_prop_header {
67 struct ofp_prop_experimenter {
68 ovs_be16 type; /* OFP*_EXPERIMENTER. */
69 ovs_be16 length; /* Length in bytes of this property. */
70 ovs_be32 experimenter; /* Experimenter ID which takes the same form as
71 * in struct ofp_experimenter_header. */
72 ovs_be32 exp_type; /* Experimenter defined. */
75 /* Pulls a property, beginning with struct ofp_prop_header, from the beginning
76 * of 'msg'. Stores the type of the property in '*typep' and, if 'property' is
77 * nonnull, the entire property, including the header, in '*property'. Returns
78 * 0 if successful, otherwise an error code.
80 * This function pulls the property's stated size padded out to a multiple of
81 * 'alignment' bytes. The common case in OpenFlow is an 'alignment' of 8, so
82 * you can use ofputil_pull_property() for that case. */
84 ofputil_pull_property__(struct ofpbuf *msg, struct ofpbuf *property,
85 unsigned int alignment, uint16_t *typep)
87 struct ofp_prop_header *oph;
88 unsigned int padded_len;
91 if (msg->size < sizeof *oph) {
92 return OFPERR_OFPBPC_BAD_LEN;
96 len = ntohs(oph->len);
97 padded_len = ROUND_UP(len, alignment);
98 if (len < sizeof *oph || padded_len > msg->size) {
99 return OFPERR_OFPBPC_BAD_LEN;
102 *typep = ntohs(oph->type);
104 ofpbuf_use_const(property, msg->data, len);
106 ofpbuf_pull(msg, padded_len);
110 /* Pulls a property, beginning with struct ofp_prop_header, from the beginning
111 * of 'msg'. Stores the type of the property in '*typep' and, if 'property' is
112 * nonnull, the entire property, including the header, in '*property'. Returns
113 * 0 if successful, otherwise an error code.
115 * This function pulls the property's stated size padded out to a multiple of
116 * 8 bytes, which is the common case for OpenFlow properties. */
118 ofputil_pull_property(struct ofpbuf *msg, struct ofpbuf *property,
121 return ofputil_pull_property__(msg, property, 8, typep);
124 static void OVS_PRINTF_FORMAT(2, 3)
125 log_property(bool loose, const char *message, ...)
127 enum vlog_level level = loose ? VLL_DBG : VLL_WARN;
128 if (!vlog_should_drop(THIS_MODULE, level, &bad_ofmsg_rl)) {
131 va_start(args, message);
132 vlog_valist(THIS_MODULE, level, message, args);
138 start_property(struct ofpbuf *msg, uint16_t type)
140 size_t start_ofs = msg->size;
141 struct ofp_prop_header *oph;
143 oph = ofpbuf_put_uninit(msg, sizeof *oph);
144 oph->type = htons(type);
145 oph->len = htons(4); /* May be updated later by end_property(). */
150 end_property(struct ofpbuf *msg, size_t start_ofs)
152 struct ofp_prop_header *oph;
154 oph = ofpbuf_at_assert(msg, start_ofs, sizeof *oph);
155 oph->len = htons(msg->size - start_ofs);
156 ofpbuf_padto(msg, ROUND_UP(msg->size, 8));
160 put_bitmap_properties(struct ofpbuf *msg, uint64_t bitmap)
162 for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
163 start_property(msg, rightmost_1bit_idx(bitmap));
167 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
168 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
171 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
172 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
173 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
174 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
177 ofputil_wcbits_to_netmask(int wcbits)
180 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
183 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
184 * that it wildcards, that is, the number of 0-bits in 'netmask', a number
185 * between 0 and 32 inclusive.
187 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
188 * still be in the valid range but isn't otherwise meaningful. */
190 ofputil_netmask_to_wcbits(ovs_be32 netmask)
192 return 32 - ip_count_cidr_bits(netmask);
195 /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
196 * flow_wildcards in 'wc' for use in struct match. It is the caller's
197 * responsibility to handle the special case where the flow match's dl_vlan is
198 * set to OFP_VLAN_NONE. */
200 ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc)
202 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 33);
204 /* Initialize most of wc. */
205 flow_wildcards_init_catchall(wc);
207 if (!(ofpfw & OFPFW10_IN_PORT)) {
208 wc->masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
211 if (!(ofpfw & OFPFW10_NW_TOS)) {
212 wc->masks.nw_tos |= IP_DSCP_MASK;
215 if (!(ofpfw & OFPFW10_NW_PROTO)) {
216 wc->masks.nw_proto = UINT8_MAX;
218 wc->masks.nw_src = ofputil_wcbits_to_netmask(ofpfw
219 >> OFPFW10_NW_SRC_SHIFT);
220 wc->masks.nw_dst = ofputil_wcbits_to_netmask(ofpfw
221 >> OFPFW10_NW_DST_SHIFT);
223 if (!(ofpfw & OFPFW10_TP_SRC)) {
224 wc->masks.tp_src = OVS_BE16_MAX;
226 if (!(ofpfw & OFPFW10_TP_DST)) {
227 wc->masks.tp_dst = OVS_BE16_MAX;
230 if (!(ofpfw & OFPFW10_DL_SRC)) {
231 WC_MASK_FIELD(wc, dl_src);
233 if (!(ofpfw & OFPFW10_DL_DST)) {
234 WC_MASK_FIELD(wc, dl_dst);
236 if (!(ofpfw & OFPFW10_DL_TYPE)) {
237 wc->masks.dl_type = OVS_BE16_MAX;
241 if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) {
242 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
244 if (!(ofpfw & OFPFW10_DL_VLAN)) {
245 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
249 /* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */
251 ofputil_match_from_ofp10_match(const struct ofp10_match *ofmatch,
254 uint32_t ofpfw = ntohl(ofmatch->wildcards) & OFPFW10_ALL;
256 /* Initialize match->wc. */
257 memset(&match->flow, 0, sizeof match->flow);
258 ofputil_wildcard_from_ofpfw10(ofpfw, &match->wc);
260 /* Initialize most of match->flow. */
261 match->flow.nw_src = ofmatch->nw_src;
262 match->flow.nw_dst = ofmatch->nw_dst;
263 match->flow.in_port.ofp_port = u16_to_ofp(ntohs(ofmatch->in_port));
264 match->flow.dl_type = ofputil_dl_type_from_openflow(ofmatch->dl_type);
265 match->flow.tp_src = ofmatch->tp_src;
266 match->flow.tp_dst = ofmatch->tp_dst;
267 match->flow.dl_src = ofmatch->dl_src;
268 match->flow.dl_dst = ofmatch->dl_dst;
269 match->flow.nw_tos = ofmatch->nw_tos & IP_DSCP_MASK;
270 match->flow.nw_proto = ofmatch->nw_proto;
272 /* Translate VLANs. */
273 if (!(ofpfw & OFPFW10_DL_VLAN) &&
274 ofmatch->dl_vlan == htons(OFP10_VLAN_NONE)) {
275 /* Match only packets without 802.1Q header.
277 * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
279 * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
280 * because we can't have a specific PCP without an 802.1Q header.
281 * However, older versions of OVS treated this as matching packets
282 * withut an 802.1Q header, so we do here too. */
283 match->flow.vlan_tci = htons(0);
284 match->wc.masks.vlan_tci = htons(0xffff);
286 ovs_be16 vid, pcp, tci;
289 vid = ofmatch->dl_vlan & htons(VLAN_VID_MASK);
290 hpcp = (ofmatch->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK;
292 tci = vid | pcp | htons(VLAN_CFI);
293 match->flow.vlan_tci = tci & match->wc.masks.vlan_tci;
297 match_zero_wildcarded_fields(match);
300 /* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */
302 ofputil_match_to_ofp10_match(const struct match *match,
303 struct ofp10_match *ofmatch)
305 const struct flow_wildcards *wc = &match->wc;
308 /* Figure out most OpenFlow wildcards. */
310 if (!wc->masks.in_port.ofp_port) {
311 ofpfw |= OFPFW10_IN_PORT;
313 if (!wc->masks.dl_type) {
314 ofpfw |= OFPFW10_DL_TYPE;
316 if (!wc->masks.nw_proto) {
317 ofpfw |= OFPFW10_NW_PROTO;
319 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_src)
320 << OFPFW10_NW_SRC_SHIFT);
321 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_dst)
322 << OFPFW10_NW_DST_SHIFT);
323 if (!(wc->masks.nw_tos & IP_DSCP_MASK)) {
324 ofpfw |= OFPFW10_NW_TOS;
326 if (!wc->masks.tp_src) {
327 ofpfw |= OFPFW10_TP_SRC;
329 if (!wc->masks.tp_dst) {
330 ofpfw |= OFPFW10_TP_DST;
332 if (eth_addr_is_zero(wc->masks.dl_src)) {
333 ofpfw |= OFPFW10_DL_SRC;
335 if (eth_addr_is_zero(wc->masks.dl_dst)) {
336 ofpfw |= OFPFW10_DL_DST;
339 /* Translate VLANs. */
340 ofmatch->dl_vlan = htons(0);
341 ofmatch->dl_vlan_pcp = 0;
342 if (match->wc.masks.vlan_tci == htons(0)) {
343 ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP;
344 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
345 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
346 ofmatch->dl_vlan = htons(OFP10_VLAN_NONE);
348 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
349 ofpfw |= OFPFW10_DL_VLAN;
351 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
354 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
355 ofpfw |= OFPFW10_DL_VLAN_PCP;
357 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
361 /* Compose most of the match structure. */
362 ofmatch->wildcards = htonl(ofpfw);
363 ofmatch->in_port = htons(ofp_to_u16(match->flow.in_port.ofp_port));
364 ofmatch->dl_src = match->flow.dl_src;
365 ofmatch->dl_dst = match->flow.dl_dst;
366 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
367 ofmatch->nw_src = match->flow.nw_src;
368 ofmatch->nw_dst = match->flow.nw_dst;
369 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
370 ofmatch->nw_proto = match->flow.nw_proto;
371 ofmatch->tp_src = match->flow.tp_src;
372 ofmatch->tp_dst = match->flow.tp_dst;
373 memset(ofmatch->pad1, '\0', sizeof ofmatch->pad1);
374 memset(ofmatch->pad2, '\0', sizeof ofmatch->pad2);
378 ofputil_pull_ofp11_match(struct ofpbuf *buf, struct match *match,
379 uint16_t *padded_match_len)
381 struct ofp11_match_header *omh = buf->data;
384 if (buf->size < sizeof *omh) {
385 return OFPERR_OFPBMC_BAD_LEN;
388 match_len = ntohs(omh->length);
390 switch (ntohs(omh->type)) {
391 case OFPMT_STANDARD: {
392 struct ofp11_match *om;
394 if (match_len != sizeof *om || buf->size < sizeof *om) {
395 return OFPERR_OFPBMC_BAD_LEN;
397 om = ofpbuf_pull(buf, sizeof *om);
398 if (padded_match_len) {
399 *padded_match_len = match_len;
401 return ofputil_match_from_ofp11_match(om, match);
405 if (padded_match_len) {
406 *padded_match_len = ROUND_UP(match_len, 8);
408 return oxm_pull_match(buf, match);
411 return OFPERR_OFPBMC_BAD_TYPE;
415 /* Converts the ofp11_match in 'ofmatch' into a struct match in 'match'.
416 * Returns 0 if successful, otherwise an OFPERR_* value. */
418 ofputil_match_from_ofp11_match(const struct ofp11_match *ofmatch,
421 uint16_t wc = ntohl(ofmatch->wildcards);
422 bool ipv4, arp, rarp;
424 match_init_catchall(match);
426 if (!(wc & OFPFW11_IN_PORT)) {
430 error = ofputil_port_from_ofp11(ofmatch->in_port, &ofp_port);
432 return OFPERR_OFPBMC_BAD_VALUE;
434 match_set_in_port(match, ofp_port);
437 match_set_dl_src_masked(match, ofmatch->dl_src,
438 eth_addr_invert(ofmatch->dl_src_mask));
439 match_set_dl_dst_masked(match, ofmatch->dl_dst,
440 eth_addr_invert(ofmatch->dl_dst_mask));
442 if (!(wc & OFPFW11_DL_VLAN)) {
443 if (ofmatch->dl_vlan == htons(OFPVID11_NONE)) {
444 /* Match only packets without a VLAN tag. */
445 match->flow.vlan_tci = htons(0);
446 match->wc.masks.vlan_tci = OVS_BE16_MAX;
448 if (ofmatch->dl_vlan == htons(OFPVID11_ANY)) {
449 /* Match any packet with a VLAN tag regardless of VID. */
450 match->flow.vlan_tci = htons(VLAN_CFI);
451 match->wc.masks.vlan_tci = htons(VLAN_CFI);
452 } else if (ntohs(ofmatch->dl_vlan) < 4096) {
453 /* Match only packets with the specified VLAN VID. */
454 match->flow.vlan_tci = htons(VLAN_CFI) | ofmatch->dl_vlan;
455 match->wc.masks.vlan_tci = htons(VLAN_CFI | VLAN_VID_MASK);
458 return OFPERR_OFPBMC_BAD_VALUE;
461 if (!(wc & OFPFW11_DL_VLAN_PCP)) {
462 if (ofmatch->dl_vlan_pcp <= 7) {
463 match->flow.vlan_tci |= htons(ofmatch->dl_vlan_pcp
465 match->wc.masks.vlan_tci |= htons(VLAN_PCP_MASK);
468 return OFPERR_OFPBMC_BAD_VALUE;
474 if (!(wc & OFPFW11_DL_TYPE)) {
475 match_set_dl_type(match,
476 ofputil_dl_type_from_openflow(ofmatch->dl_type));
479 ipv4 = match->flow.dl_type == htons(ETH_TYPE_IP);
480 arp = match->flow.dl_type == htons(ETH_TYPE_ARP);
481 rarp = match->flow.dl_type == htons(ETH_TYPE_RARP);
483 if (ipv4 && !(wc & OFPFW11_NW_TOS)) {
484 if (ofmatch->nw_tos & ~IP_DSCP_MASK) {
486 return OFPERR_OFPBMC_BAD_VALUE;
489 match_set_nw_dscp(match, ofmatch->nw_tos);
492 if (ipv4 || arp || rarp) {
493 if (!(wc & OFPFW11_NW_PROTO)) {
494 match_set_nw_proto(match, ofmatch->nw_proto);
496 match_set_nw_src_masked(match, ofmatch->nw_src, ~ofmatch->nw_src_mask);
497 match_set_nw_dst_masked(match, ofmatch->nw_dst, ~ofmatch->nw_dst_mask);
500 #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
501 if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) {
502 switch (match->flow.nw_proto) {
504 /* "A.2.3 Flow Match Structures" in OF1.1 says:
506 * The tp_src and tp_dst fields will be ignored unless the
507 * network protocol specified is as TCP, UDP or SCTP.
509 * but I'm pretty sure we should support ICMP too, otherwise
510 * that's a regression from OF1.0. */
511 if (!(wc & OFPFW11_TP_SRC)) {
512 uint16_t icmp_type = ntohs(ofmatch->tp_src);
513 if (icmp_type < 0x100) {
514 match_set_icmp_type(match, icmp_type);
516 return OFPERR_OFPBMC_BAD_FIELD;
519 if (!(wc & OFPFW11_TP_DST)) {
520 uint16_t icmp_code = ntohs(ofmatch->tp_dst);
521 if (icmp_code < 0x100) {
522 match_set_icmp_code(match, icmp_code);
524 return OFPERR_OFPBMC_BAD_FIELD;
532 if (!(wc & (OFPFW11_TP_SRC))) {
533 match_set_tp_src(match, ofmatch->tp_src);
535 if (!(wc & (OFPFW11_TP_DST))) {
536 match_set_tp_dst(match, ofmatch->tp_dst);
541 /* OF1.1 says explicitly to ignore this. */
546 if (eth_type_mpls(match->flow.dl_type)) {
547 if (!(wc & OFPFW11_MPLS_LABEL)) {
548 match_set_mpls_label(match, 0, ofmatch->mpls_label);
550 if (!(wc & OFPFW11_MPLS_TC)) {
551 match_set_mpls_tc(match, 0, ofmatch->mpls_tc);
555 match_set_metadata_masked(match, ofmatch->metadata,
556 ~ofmatch->metadata_mask);
561 /* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */
563 ofputil_match_to_ofp11_match(const struct match *match,
564 struct ofp11_match *ofmatch)
568 memset(ofmatch, 0, sizeof *ofmatch);
569 ofmatch->omh.type = htons(OFPMT_STANDARD);
570 ofmatch->omh.length = htons(OFPMT11_STANDARD_LENGTH);
572 if (!match->wc.masks.in_port.ofp_port) {
573 wc |= OFPFW11_IN_PORT;
575 ofmatch->in_port = ofputil_port_to_ofp11(match->flow.in_port.ofp_port);
578 ofmatch->dl_src = match->flow.dl_src;
579 ofmatch->dl_src_mask = eth_addr_invert(match->wc.masks.dl_src);
580 ofmatch->dl_dst = match->flow.dl_dst;
581 ofmatch->dl_dst_mask = eth_addr_invert(match->wc.masks.dl_dst);
583 if (match->wc.masks.vlan_tci == htons(0)) {
584 wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP;
585 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
586 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
587 ofmatch->dl_vlan = htons(OFPVID11_NONE);
588 wc |= OFPFW11_DL_VLAN_PCP;
590 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
591 ofmatch->dl_vlan = htons(OFPVID11_ANY);
593 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
596 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
597 wc |= OFPFW11_DL_VLAN_PCP;
599 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
603 if (!match->wc.masks.dl_type) {
604 wc |= OFPFW11_DL_TYPE;
606 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
609 if (!(match->wc.masks.nw_tos & IP_DSCP_MASK)) {
610 wc |= OFPFW11_NW_TOS;
612 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
615 if (!match->wc.masks.nw_proto) {
616 wc |= OFPFW11_NW_PROTO;
618 ofmatch->nw_proto = match->flow.nw_proto;
621 ofmatch->nw_src = match->flow.nw_src;
622 ofmatch->nw_src_mask = ~match->wc.masks.nw_src;
623 ofmatch->nw_dst = match->flow.nw_dst;
624 ofmatch->nw_dst_mask = ~match->wc.masks.nw_dst;
626 if (!match->wc.masks.tp_src) {
627 wc |= OFPFW11_TP_SRC;
629 ofmatch->tp_src = match->flow.tp_src;
632 if (!match->wc.masks.tp_dst) {
633 wc |= OFPFW11_TP_DST;
635 ofmatch->tp_dst = match->flow.tp_dst;
638 if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK))) {
639 wc |= OFPFW11_MPLS_LABEL;
641 ofmatch->mpls_label = htonl(mpls_lse_to_label(
642 match->flow.mpls_lse[0]));
645 if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK))) {
646 wc |= OFPFW11_MPLS_TC;
648 ofmatch->mpls_tc = mpls_lse_to_tc(match->flow.mpls_lse[0]);
651 ofmatch->metadata = match->flow.metadata;
652 ofmatch->metadata_mask = ~match->wc.masks.metadata;
654 ofmatch->wildcards = htonl(wc);
657 /* Returns the "typical" length of a match for 'protocol', for use in
658 * estimating space to preallocate. */
660 ofputil_match_typical_len(enum ofputil_protocol protocol)
663 case OFPUTIL_P_OF10_STD:
664 case OFPUTIL_P_OF10_STD_TID:
665 return sizeof(struct ofp10_match);
667 case OFPUTIL_P_OF10_NXM:
668 case OFPUTIL_P_OF10_NXM_TID:
669 return NXM_TYPICAL_LEN;
671 case OFPUTIL_P_OF11_STD:
672 return sizeof(struct ofp11_match);
674 case OFPUTIL_P_OF12_OXM:
675 case OFPUTIL_P_OF13_OXM:
676 case OFPUTIL_P_OF14_OXM:
677 case OFPUTIL_P_OF15_OXM:
678 return NXM_TYPICAL_LEN;
685 /* Appends to 'b' an struct ofp11_match_header followed by a match that
686 * expresses 'match' properly for 'protocol', plus enough zero bytes to pad the
687 * data appended out to a multiple of 8. 'protocol' must be one that is usable
688 * in OpenFlow 1.1 or later.
690 * This function can cause 'b''s data to be reallocated.
692 * Returns the number of bytes appended to 'b', excluding the padding. Never
695 ofputil_put_ofp11_match(struct ofpbuf *b, const struct match *match,
696 enum ofputil_protocol protocol)
699 case OFPUTIL_P_OF10_STD:
700 case OFPUTIL_P_OF10_STD_TID:
701 case OFPUTIL_P_OF10_NXM:
702 case OFPUTIL_P_OF10_NXM_TID:
705 case OFPUTIL_P_OF11_STD: {
706 struct ofp11_match *om;
708 /* Make sure that no padding is needed. */
709 BUILD_ASSERT_DECL(sizeof *om % 8 == 0);
711 om = ofpbuf_put_uninit(b, sizeof *om);
712 ofputil_match_to_ofp11_match(match, om);
716 case OFPUTIL_P_OF12_OXM:
717 case OFPUTIL_P_OF13_OXM:
718 case OFPUTIL_P_OF14_OXM:
719 case OFPUTIL_P_OF15_OXM:
720 return oxm_put_match(b, match,
721 ofputil_protocol_to_ofp_version(protocol));
727 /* Given a 'dl_type' value in the format used in struct flow, returns the
728 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
731 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
733 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
734 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
738 /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
739 * structure, returns the corresponding 'dl_type' value for use in struct
742 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
744 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
745 ? htons(FLOW_DL_TYPE_NONE)
751 struct proto_abbrev {
752 enum ofputil_protocol protocol;
756 /* Most users really don't care about some of the differences between
757 * protocols. These abbreviations help with that. */
758 static const struct proto_abbrev proto_abbrevs[] = {
759 { OFPUTIL_P_ANY, "any" },
760 { OFPUTIL_P_OF10_STD_ANY, "OpenFlow10" },
761 { OFPUTIL_P_OF10_NXM_ANY, "NXM" },
762 { OFPUTIL_P_ANY_OXM, "OXM" },
764 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
766 enum ofputil_protocol ofputil_flow_dump_protocols[] = {
775 size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
777 /* Returns the set of ofputil_protocols that are supported with the given
778 * OpenFlow 'version'. 'version' should normally be an 8-bit OpenFlow version
779 * identifier (e.g. 0x01 for OpenFlow 1.0, 0x02 for OpenFlow 1.1). Returns 0
780 * if 'version' is not supported or outside the valid range. */
781 enum ofputil_protocol
782 ofputil_protocols_from_ofp_version(enum ofp_version version)
786 return OFPUTIL_P_OF10_STD_ANY | OFPUTIL_P_OF10_NXM_ANY;
788 return OFPUTIL_P_OF11_STD;
790 return OFPUTIL_P_OF12_OXM;
792 return OFPUTIL_P_OF13_OXM;
794 return OFPUTIL_P_OF14_OXM;
796 return OFPUTIL_P_OF15_OXM;
802 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
803 * connection that has negotiated the given 'version'. 'version' should
804 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
805 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
806 * outside the valid range. */
807 enum ofputil_protocol
808 ofputil_protocol_from_ofp_version(enum ofp_version version)
810 return rightmost_1bit(ofputil_protocols_from_ofp_version(version));
813 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
814 * etc.) that corresponds to 'protocol'. */
816 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
819 case OFPUTIL_P_OF10_STD:
820 case OFPUTIL_P_OF10_STD_TID:
821 case OFPUTIL_P_OF10_NXM:
822 case OFPUTIL_P_OF10_NXM_TID:
823 return OFP10_VERSION;
824 case OFPUTIL_P_OF11_STD:
825 return OFP11_VERSION;
826 case OFPUTIL_P_OF12_OXM:
827 return OFP12_VERSION;
828 case OFPUTIL_P_OF13_OXM:
829 return OFP13_VERSION;
830 case OFPUTIL_P_OF14_OXM:
831 return OFP14_VERSION;
832 case OFPUTIL_P_OF15_OXM:
833 return OFP15_VERSION;
839 /* Returns a bitmap of OpenFlow versions that are supported by at
840 * least one of the 'protocols'. */
842 ofputil_protocols_to_version_bitmap(enum ofputil_protocol protocols)
846 for (; protocols; protocols = zero_rightmost_1bit(protocols)) {
847 enum ofputil_protocol protocol = rightmost_1bit(protocols);
849 bitmap |= 1u << ofputil_protocol_to_ofp_version(protocol);
855 /* Returns the set of protocols that are supported on top of the
856 * OpenFlow versions included in 'bitmap'. */
857 enum ofputil_protocol
858 ofputil_protocols_from_version_bitmap(uint32_t bitmap)
860 enum ofputil_protocol protocols = 0;
862 for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
863 enum ofp_version version = rightmost_1bit_idx(bitmap);
865 protocols |= ofputil_protocols_from_ofp_version(version);
871 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
874 ofputil_protocol_is_valid(enum ofputil_protocol protocol)
876 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
879 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
880 * extension turned on or off if 'enable' is true or false, respectively.
882 * This extension is only useful for protocols whose "standard" version does
883 * not allow specific tables to be modified. In particular, this is true of
884 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
885 * specifies a table ID and so there is no need for such an extension. When
886 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
887 * extension, this function just returns its 'protocol' argument unchanged
888 * regardless of the value of 'enable'. */
889 enum ofputil_protocol
890 ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
893 case OFPUTIL_P_OF10_STD:
894 case OFPUTIL_P_OF10_STD_TID:
895 return enable ? OFPUTIL_P_OF10_STD_TID : OFPUTIL_P_OF10_STD;
897 case OFPUTIL_P_OF10_NXM:
898 case OFPUTIL_P_OF10_NXM_TID:
899 return enable ? OFPUTIL_P_OF10_NXM_TID : OFPUTIL_P_OF10_NXM;
901 case OFPUTIL_P_OF11_STD:
902 return OFPUTIL_P_OF11_STD;
904 case OFPUTIL_P_OF12_OXM:
905 return OFPUTIL_P_OF12_OXM;
907 case OFPUTIL_P_OF13_OXM:
908 return OFPUTIL_P_OF13_OXM;
910 case OFPUTIL_P_OF14_OXM:
911 return OFPUTIL_P_OF14_OXM;
913 case OFPUTIL_P_OF15_OXM:
914 return OFPUTIL_P_OF15_OXM;
921 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
922 * some extension to a standard protocol version, the return value is the
923 * standard version of that protocol without any extension. If 'protocol' is a
924 * standard protocol version, returns 'protocol' unchanged. */
925 enum ofputil_protocol
926 ofputil_protocol_to_base(enum ofputil_protocol protocol)
928 return ofputil_protocol_set_tid(protocol, false);
931 /* Returns 'new_base' with any extensions taken from 'cur'. */
932 enum ofputil_protocol
933 ofputil_protocol_set_base(enum ofputil_protocol cur,
934 enum ofputil_protocol new_base)
936 bool tid = (cur & OFPUTIL_P_TID) != 0;
939 case OFPUTIL_P_OF10_STD:
940 case OFPUTIL_P_OF10_STD_TID:
941 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD, tid);
943 case OFPUTIL_P_OF10_NXM:
944 case OFPUTIL_P_OF10_NXM_TID:
945 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM, tid);
947 case OFPUTIL_P_OF11_STD:
948 return ofputil_protocol_set_tid(OFPUTIL_P_OF11_STD, tid);
950 case OFPUTIL_P_OF12_OXM:
951 return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM, tid);
953 case OFPUTIL_P_OF13_OXM:
954 return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM, tid);
956 case OFPUTIL_P_OF14_OXM:
957 return ofputil_protocol_set_tid(OFPUTIL_P_OF14_OXM, tid);
959 case OFPUTIL_P_OF15_OXM:
960 return ofputil_protocol_set_tid(OFPUTIL_P_OF15_OXM, tid);
967 /* Returns a string form of 'protocol', if a simple form exists (that is, if
968 * 'protocol' is either a single protocol or it is a combination of protocols
969 * that have a single abbreviation). Otherwise, returns NULL. */
971 ofputil_protocol_to_string(enum ofputil_protocol protocol)
973 const struct proto_abbrev *p;
975 /* Use a "switch" statement for single-bit names so that we get a compiler
976 * warning if we forget any. */
978 case OFPUTIL_P_OF10_NXM:
979 return "NXM-table_id";
981 case OFPUTIL_P_OF10_NXM_TID:
982 return "NXM+table_id";
984 case OFPUTIL_P_OF10_STD:
985 return "OpenFlow10-table_id";
987 case OFPUTIL_P_OF10_STD_TID:
988 return "OpenFlow10+table_id";
990 case OFPUTIL_P_OF11_STD:
993 case OFPUTIL_P_OF12_OXM:
994 return "OXM-OpenFlow12";
996 case OFPUTIL_P_OF13_OXM:
997 return "OXM-OpenFlow13";
999 case OFPUTIL_P_OF14_OXM:
1000 return "OXM-OpenFlow14";
1002 case OFPUTIL_P_OF15_OXM:
1003 return "OXM-OpenFlow15";
1006 /* Check abbreviations. */
1007 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1008 if (protocol == p->protocol) {
1016 /* Returns a string that represents 'protocols'. The return value might be a
1017 * comma-separated list if 'protocols' doesn't have a simple name. The return
1018 * value is "none" if 'protocols' is 0.
1020 * The caller must free the returned string (with free()). */
1022 ofputil_protocols_to_string(enum ofputil_protocol protocols)
1026 ovs_assert(!(protocols & ~OFPUTIL_P_ANY));
1027 if (protocols == 0) {
1028 return xstrdup("none");
1033 const struct proto_abbrev *p;
1037 ds_put_char(&s, ',');
1040 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1041 if ((protocols & p->protocol) == p->protocol) {
1042 ds_put_cstr(&s, p->name);
1043 protocols &= ~p->protocol;
1048 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
1049 enum ofputil_protocol bit = 1u << i;
1051 if (protocols & bit) {
1052 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
1061 return ds_steal_cstr(&s);
1064 static enum ofputil_protocol
1065 ofputil_protocol_from_string__(const char *s, size_t n)
1067 const struct proto_abbrev *p;
1070 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
1071 enum ofputil_protocol bit = 1u << i;
1072 const char *name = ofputil_protocol_to_string(bit);
1074 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
1079 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1080 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
1088 /* Returns the nonempty set of protocols represented by 's', which can be a
1089 * single protocol name or abbreviation or a comma-separated list of them.
1091 * Aborts the program with an error message if 's' is invalid. */
1092 enum ofputil_protocol
1093 ofputil_protocols_from_string(const char *s)
1095 const char *orig_s = s;
1096 enum ofputil_protocol protocols;
1100 enum ofputil_protocol p;
1103 n = strcspn(s, ",");
1109 p = ofputil_protocol_from_string__(s, n);
1111 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
1119 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
1125 ofputil_version_from_string(const char *s)
1127 if (!strcasecmp(s, "OpenFlow10")) {
1128 return OFP10_VERSION;
1130 if (!strcasecmp(s, "OpenFlow11")) {
1131 return OFP11_VERSION;
1133 if (!strcasecmp(s, "OpenFlow12")) {
1134 return OFP12_VERSION;
1136 if (!strcasecmp(s, "OpenFlow13")) {
1137 return OFP13_VERSION;
1139 if (!strcasecmp(s, "OpenFlow14")) {
1140 return OFP14_VERSION;
1142 if (!strcasecmp(s, "OpenFlow15")) {
1143 return OFP15_VERSION;
1149 is_delimiter(unsigned char c)
1151 return isspace(c) || c == ',';
1155 ofputil_versions_from_string(const char *s)
1158 uint32_t bitmap = 0;
1165 if (is_delimiter(s[i])) {
1170 while (s[i + j] && !is_delimiter(s[i + j])) {
1173 key = xmemdup0(s + i, j);
1174 version = ofputil_version_from_string(key);
1176 VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key);
1179 bitmap |= 1u << version;
1187 ofputil_versions_from_strings(char ** const s, size_t count)
1189 uint32_t bitmap = 0;
1192 int version = ofputil_version_from_string(s[count]);
1194 VLOG_WARN("Unknown OpenFlow version: \"%s\"", s[count]);
1196 bitmap |= 1u << version;
1204 ofputil_version_to_string(enum ofp_version ofp_version)
1206 switch (ofp_version) {
1208 return "OpenFlow10";
1210 return "OpenFlow11";
1212 return "OpenFlow12";
1214 return "OpenFlow13";
1216 return "OpenFlow14";
1218 return "OpenFlow15";
1225 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
1227 switch (packet_in_format) {
1228 case NXPIF_OPENFLOW10:
1237 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
1239 switch (packet_in_format) {
1240 case NXPIF_OPENFLOW10:
1241 return "openflow10";
1250 ofputil_packet_in_format_from_string(const char *s)
1252 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
1253 : !strcmp(s, "nxm") ? NXPIF_NXM
1258 ofputil_format_version(struct ds *msg, enum ofp_version version)
1260 ds_put_format(msg, "0x%02x", version);
1264 ofputil_format_version_name(struct ds *msg, enum ofp_version version)
1266 ds_put_cstr(msg, ofputil_version_to_string(version));
1270 ofputil_format_version_bitmap__(struct ds *msg, uint32_t bitmap,
1271 void (*format_version)(struct ds *msg,
1275 format_version(msg, raw_ctz(bitmap));
1276 bitmap = zero_rightmost_1bit(bitmap);
1278 ds_put_cstr(msg, ", ");
1284 ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap)
1286 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version);
1290 ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap)
1292 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version_name);
1296 ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header *oheh,
1297 uint32_t *allowed_versionsp)
1299 uint16_t bitmap_len = ntohs(oheh->length) - sizeof *oheh;
1300 const ovs_be32 *bitmap = ALIGNED_CAST(const ovs_be32 *, oheh + 1);
1301 uint32_t allowed_versions;
1303 if (!bitmap_len || bitmap_len % sizeof *bitmap) {
1307 /* Only use the first 32-bit element of the bitmap as that is all the
1308 * current implementation supports. Subsequent elements are ignored which
1309 * should have no effect on session negotiation until Open vSwitch supports
1310 * wire-protocol versions greater than 31.
1312 allowed_versions = ntohl(bitmap[0]);
1314 if (allowed_versions & 1) {
1315 /* There's no OpenFlow version 0. */
1316 VLOG_WARN_RL(&bad_ofmsg_rl, "peer claims to support invalid OpenFlow "
1318 allowed_versions &= ~1u;
1321 if (!allowed_versions) {
1322 VLOG_WARN_RL(&bad_ofmsg_rl, "peer does not support any OpenFlow "
1323 "version (between 0x01 and 0x1f)");
1327 *allowed_versionsp = allowed_versions;
1332 version_bitmap_from_version(uint8_t ofp_version)
1334 return ((ofp_version < 32 ? 1u << ofp_version : 0) - 1) << 1;
1337 /* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions'
1338 * the set of OpenFlow versions for which 'oh' announces support.
1340 * Because of how OpenFlow defines OFPT_HELLO messages, this function is always
1341 * successful, and thus '*allowed_versions' is always initialized. However, it
1342 * returns false if 'oh' contains some data that could not be fully understood,
1343 * true if 'oh' was completely parsed. */
1345 ofputil_decode_hello(const struct ofp_header *oh, uint32_t *allowed_versions)
1350 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
1351 ofpbuf_pull(&msg, sizeof *oh);
1353 *allowed_versions = version_bitmap_from_version(oh->version);
1355 const struct ofp_hello_elem_header *oheh;
1358 if (msg.size < sizeof *oheh) {
1363 len = ntohs(oheh->length);
1364 if (len < sizeof *oheh || !ofpbuf_try_pull(&msg, ROUND_UP(len, 8))) {
1368 if (oheh->type != htons(OFPHET_VERSIONBITMAP)
1369 || !ofputil_decode_hello_bitmap(oheh, allowed_versions)) {
1377 /* Returns true if 'allowed_versions' needs to be accompanied by a version
1378 * bitmap to be correctly expressed in an OFPT_HELLO message. */
1380 should_send_version_bitmap(uint32_t allowed_versions)
1382 return !is_pow2((allowed_versions >> 1) + 1);
1385 /* Create an OFPT_HELLO message that expresses support for the OpenFlow
1386 * versions in the 'allowed_versions' bitmaps and returns the message. */
1388 ofputil_encode_hello(uint32_t allowed_versions)
1390 enum ofp_version ofp_version;
1393 ofp_version = leftmost_1bit_idx(allowed_versions);
1394 msg = ofpraw_alloc(OFPRAW_OFPT_HELLO, ofp_version, 0);
1396 if (should_send_version_bitmap(allowed_versions)) {
1397 struct ofp_hello_elem_header *oheh;
1400 map_len = sizeof allowed_versions;
1401 oheh = ofpbuf_put_zeros(msg, ROUND_UP(map_len + sizeof *oheh, 8));
1402 oheh->type = htons(OFPHET_VERSIONBITMAP);
1403 oheh->length = htons(map_len + sizeof *oheh);
1404 *ALIGNED_CAST(ovs_be32 *, oheh + 1) = htonl(allowed_versions);
1406 ofpmsg_update_length(msg);
1412 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1413 * protocol is 'current', at least partly transitions the protocol to 'want'.
1414 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1415 * connection if the switch processes the returned message correctly. (If
1416 * '*next != want' then the caller will have to iterate.)
1418 * If 'current == want', or if it is not possible to transition from 'current'
1419 * to 'want' (because, for example, 'current' and 'want' use different OpenFlow
1420 * protocol versions), returns NULL and stores 'current' in '*next'. */
1422 ofputil_encode_set_protocol(enum ofputil_protocol current,
1423 enum ofputil_protocol want,
1424 enum ofputil_protocol *next)
1426 enum ofp_version cur_version, want_version;
1427 enum ofputil_protocol cur_base, want_base;
1428 bool cur_tid, want_tid;
1430 cur_version = ofputil_protocol_to_ofp_version(current);
1431 want_version = ofputil_protocol_to_ofp_version(want);
1432 if (cur_version != want_version) {
1437 cur_base = ofputil_protocol_to_base(current);
1438 want_base = ofputil_protocol_to_base(want);
1439 if (cur_base != want_base) {
1440 *next = ofputil_protocol_set_base(current, want_base);
1442 switch (want_base) {
1443 case OFPUTIL_P_OF10_NXM:
1444 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1446 case OFPUTIL_P_OF10_STD:
1447 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1449 case OFPUTIL_P_OF11_STD:
1450 case OFPUTIL_P_OF12_OXM:
1451 case OFPUTIL_P_OF13_OXM:
1452 case OFPUTIL_P_OF14_OXM:
1453 case OFPUTIL_P_OF15_OXM:
1454 /* There is only one variant of each OpenFlow 1.1+ protocol, and we
1455 * verified above that we're not trying to change versions. */
1458 case OFPUTIL_P_OF10_STD_TID:
1459 case OFPUTIL_P_OF10_NXM_TID:
1464 cur_tid = (current & OFPUTIL_P_TID) != 0;
1465 want_tid = (want & OFPUTIL_P_TID) != 0;
1466 if (cur_tid != want_tid) {
1467 *next = ofputil_protocol_set_tid(current, want_tid);
1468 return ofputil_make_flow_mod_table_id(want_tid);
1471 ovs_assert(current == want);
1477 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1478 * format to 'nxff'. */
1480 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1482 struct nx_set_flow_format *sff;
1485 ovs_assert(ofputil_nx_flow_format_is_valid(nxff));
1487 msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1488 sff = ofpbuf_put_zeros(msg, sizeof *sff);
1489 sff->format = htonl(nxff);
1494 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1496 enum ofputil_protocol
1497 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1499 switch (flow_format) {
1500 case NXFF_OPENFLOW10:
1501 return OFPUTIL_P_OF10_STD;
1504 return OFPUTIL_P_OF10_NXM;
1511 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1513 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1515 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1518 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1521 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1523 switch (flow_format) {
1524 case NXFF_OPENFLOW10:
1525 return "openflow10";
1534 ofputil_make_set_packet_in_format(enum ofp_version ofp_version,
1535 enum nx_packet_in_format packet_in_format)
1537 struct nx_set_packet_in_format *spif;
1540 msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, ofp_version, 0);
1541 spif = ofpbuf_put_zeros(msg, sizeof *spif);
1542 spif->format = htonl(packet_in_format);
1547 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1548 * extension on or off (according to 'flow_mod_table_id'). */
1550 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1552 struct nx_flow_mod_table_id *nfmti;
1555 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1556 nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
1557 nfmti->set = flow_mod_table_id;
1561 struct ofputil_flow_mod_flag {
1563 enum ofp_version min_version, max_version;
1564 enum ofputil_flow_mod_flags flag;
1567 static const struct ofputil_flow_mod_flag ofputil_flow_mod_flags[] = {
1568 { OFPFF_SEND_FLOW_REM, OFP10_VERSION, 0, OFPUTIL_FF_SEND_FLOW_REM },
1569 { OFPFF_CHECK_OVERLAP, OFP10_VERSION, 0, OFPUTIL_FF_CHECK_OVERLAP },
1570 { OFPFF10_EMERG, OFP10_VERSION, OFP10_VERSION,
1572 { OFPFF12_RESET_COUNTS, OFP12_VERSION, 0, OFPUTIL_FF_RESET_COUNTS },
1573 { OFPFF13_NO_PKT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_PKT_COUNTS },
1574 { OFPFF13_NO_BYT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_BYT_COUNTS },
1579 ofputil_decode_flow_mod_flags(ovs_be16 raw_flags_,
1580 enum ofp_flow_mod_command command,
1581 enum ofp_version version,
1582 enum ofputil_flow_mod_flags *flagsp)
1584 uint16_t raw_flags = ntohs(raw_flags_);
1585 const struct ofputil_flow_mod_flag *f;
1588 for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1589 if (raw_flags & f->raw_flag
1590 && version >= f->min_version
1591 && (!f->max_version || version <= f->max_version)) {
1592 raw_flags &= ~f->raw_flag;
1597 /* In OF1.0 and OF1.1, "add" always resets counters, and other commands
1600 * In OF1.2 and later, OFPFF12_RESET_COUNTS controls whether each command
1601 * resets counters. */
1602 if ((version == OFP10_VERSION || version == OFP11_VERSION)
1603 && command == OFPFC_ADD) {
1604 *flagsp |= OFPUTIL_FF_RESET_COUNTS;
1607 return raw_flags ? OFPERR_OFPFMFC_BAD_FLAGS : 0;
1611 ofputil_encode_flow_mod_flags(enum ofputil_flow_mod_flags flags,
1612 enum ofp_version version)
1614 const struct ofputil_flow_mod_flag *f;
1618 for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1620 && version >= f->min_version
1621 && (!f->max_version || version <= f->max_version)) {
1622 raw_flags |= f->raw_flag;
1626 return htons(raw_flags);
1629 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1630 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1633 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1634 * The caller must initialize 'ofpacts' and retains ownership of it.
1635 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1637 * Does not validate the flow_mod actions. The caller should do that, with
1638 * ofpacts_check(). */
1640 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1641 const struct ofp_header *oh,
1642 enum ofputil_protocol protocol,
1643 struct ofpbuf *ofpacts,
1644 ofp_port_t max_port, uint8_t max_table)
1651 /* Ignored for non-delete actions */
1652 fm->delete_reason = OFPRR_DELETE;
1654 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1655 raw = ofpraw_pull_assert(&b);
1656 if (raw == OFPRAW_OFPT11_FLOW_MOD) {
1657 /* Standard OpenFlow 1.1+ flow_mod. */
1658 const struct ofp11_flow_mod *ofm;
1660 ofm = ofpbuf_pull(&b, sizeof *ofm);
1662 error = ofputil_pull_ofp11_match(&b, &fm->match, NULL);
1667 /* Translate the message. */
1668 fm->priority = ntohs(ofm->priority);
1669 if (ofm->command == OFPFC_ADD
1670 || (oh->version == OFP11_VERSION
1671 && (ofm->command == OFPFC_MODIFY ||
1672 ofm->command == OFPFC_MODIFY_STRICT)
1673 && ofm->cookie_mask == htonll(0))) {
1674 /* In OpenFlow 1.1 only, a "modify" or "modify-strict" that does
1675 * not match on the cookie is treated as an "add" if there is no
1677 fm->cookie = htonll(0);
1678 fm->cookie_mask = htonll(0);
1679 fm->new_cookie = ofm->cookie;
1681 fm->cookie = ofm->cookie;
1682 fm->cookie_mask = ofm->cookie_mask;
1683 fm->new_cookie = OVS_BE64_MAX;
1685 fm->modify_cookie = false;
1686 fm->command = ofm->command;
1690 * OF1.1 entirely forbids table_id == OFPTT_ALL.
1691 * OF1.2+ allows table_id == OFPTT_ALL only for deletes. */
1692 fm->table_id = ofm->table_id;
1693 if (fm->table_id == OFPTT_ALL
1694 && (oh->version == OFP11_VERSION
1695 || (ofm->command != OFPFC_DELETE &&
1696 ofm->command != OFPFC_DELETE_STRICT))) {
1697 return OFPERR_OFPFMFC_BAD_TABLE_ID;
1700 fm->idle_timeout = ntohs(ofm->idle_timeout);
1701 fm->hard_timeout = ntohs(ofm->hard_timeout);
1702 if (oh->version >= OFP14_VERSION && ofm->command == OFPFC_ADD) {
1703 fm->importance = ntohs(ofm->importance);
1707 fm->buffer_id = ntohl(ofm->buffer_id);
1708 error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
1713 fm->out_group = (ofm->command == OFPFC_DELETE ||
1714 ofm->command == OFPFC_DELETE_STRICT
1715 ? ntohl(ofm->out_group)
1717 raw_flags = ofm->flags;
1721 if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1722 /* Standard OpenFlow 1.0 flow_mod. */
1723 const struct ofp10_flow_mod *ofm;
1725 /* Get the ofp10_flow_mod. */
1726 ofm = ofpbuf_pull(&b, sizeof *ofm);
1728 /* Translate the rule. */
1729 ofputil_match_from_ofp10_match(&ofm->match, &fm->match);
1730 ofputil_normalize_match(&fm->match);
1732 /* OpenFlow 1.0 says that exact-match rules have to have the
1733 * highest possible priority. */
1734 fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL)
1735 ? ntohs(ofm->priority)
1738 /* Translate the message. */
1739 command = ntohs(ofm->command);
1740 fm->cookie = htonll(0);
1741 fm->cookie_mask = htonll(0);
1742 fm->new_cookie = ofm->cookie;
1743 fm->idle_timeout = ntohs(ofm->idle_timeout);
1744 fm->hard_timeout = ntohs(ofm->hard_timeout);
1746 fm->buffer_id = ntohl(ofm->buffer_id);
1747 fm->out_port = u16_to_ofp(ntohs(ofm->out_port));
1748 fm->out_group = OFPG11_ANY;
1749 raw_flags = ofm->flags;
1750 } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1751 /* Nicira extended flow_mod. */
1752 const struct nx_flow_mod *nfm;
1754 /* Dissect the message. */
1755 nfm = ofpbuf_pull(&b, sizeof *nfm);
1756 error = nx_pull_match(&b, ntohs(nfm->match_len),
1757 &fm->match, &fm->cookie, &fm->cookie_mask);
1762 /* Translate the message. */
1763 command = ntohs(nfm->command);
1764 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1765 /* Flow additions may only set a new cookie, not match an
1766 * existing cookie. */
1767 return OFPERR_NXBRC_NXM_INVALID;
1769 fm->priority = ntohs(nfm->priority);
1770 fm->new_cookie = nfm->cookie;
1771 fm->idle_timeout = ntohs(nfm->idle_timeout);
1772 fm->hard_timeout = ntohs(nfm->hard_timeout);
1774 fm->buffer_id = ntohl(nfm->buffer_id);
1775 fm->out_port = u16_to_ofp(ntohs(nfm->out_port));
1776 fm->out_group = OFPG11_ANY;
1777 raw_flags = nfm->flags;
1782 fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX;
1783 if (protocol & OFPUTIL_P_TID) {
1784 fm->command = command & 0xff;
1785 fm->table_id = command >> 8;
1787 if (command > 0xff) {
1788 VLOG_WARN_RL(&bad_ofmsg_rl, "flow_mod has explicit table_id "
1789 "but flow_mod_table_id extension is not enabled");
1791 fm->command = command;
1792 fm->table_id = 0xff;
1796 if (fm->command > OFPFC_DELETE_STRICT) {
1797 return OFPERR_OFPFMFC_BAD_COMMAND;
1800 error = ofpacts_pull_openflow_instructions(&b, b.size,
1801 oh->version, ofpacts);
1805 fm->ofpacts = ofpacts->data;
1806 fm->ofpacts_len = ofpacts->size;
1808 error = ofputil_decode_flow_mod_flags(raw_flags, fm->command,
1809 oh->version, &fm->flags);
1814 if (fm->flags & OFPUTIL_FF_EMERG) {
1815 /* We do not support the OpenFlow 1.0 emergency flow cache, which
1816 * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
1818 * OpenFlow 1.0 specifies the error code to use when idle_timeout
1819 * or hard_timeout is nonzero. Otherwise, there is no good error
1820 * code, so just state that the flow table is full. */
1821 return (fm->hard_timeout || fm->idle_timeout
1822 ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT
1823 : OFPERR_OFPFMFC_TABLE_FULL);
1826 return ofpacts_check_consistency(fm->ofpacts, fm->ofpacts_len,
1827 &fm->match.flow, max_port,
1828 fm->table_id, max_table, protocol);
1832 ofputil_pull_bands(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
1833 struct ofpbuf *bands)
1835 const struct ofp13_meter_band_header *ombh;
1836 struct ofputil_meter_band *mb;
1839 ombh = ofpbuf_try_pull(msg, len);
1841 return OFPERR_OFPBRC_BAD_LEN;
1844 while (len >= sizeof (struct ofp13_meter_band_drop)) {
1845 size_t ombh_len = ntohs(ombh->len);
1846 /* All supported band types have the same length. */
1847 if (ombh_len != sizeof (struct ofp13_meter_band_drop)) {
1848 return OFPERR_OFPBRC_BAD_LEN;
1850 mb = ofpbuf_put_uninit(bands, sizeof *mb);
1851 mb->type = ntohs(ombh->type);
1852 if (mb->type != OFPMBT13_DROP && mb->type != OFPMBT13_DSCP_REMARK) {
1853 return OFPERR_OFPMMFC_BAD_BAND;
1855 mb->rate = ntohl(ombh->rate);
1856 mb->burst_size = ntohl(ombh->burst_size);
1857 mb->prec_level = (mb->type == OFPMBT13_DSCP_REMARK) ?
1858 ((struct ofp13_meter_band_dscp_remark *)ombh)->prec_level : 0;
1861 ombh = ALIGNED_CAST(struct ofp13_meter_band_header *,
1862 (char *) ombh + ombh_len);
1865 return OFPERR_OFPBRC_BAD_LEN;
1872 ofputil_decode_meter_mod(const struct ofp_header *oh,
1873 struct ofputil_meter_mod *mm,
1874 struct ofpbuf *bands)
1876 const struct ofp13_meter_mod *omm;
1879 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1880 ofpraw_pull_assert(&b);
1881 omm = ofpbuf_pull(&b, sizeof *omm);
1883 /* Translate the message. */
1884 mm->command = ntohs(omm->command);
1885 if (mm->command != OFPMC13_ADD &&
1886 mm->command != OFPMC13_MODIFY &&
1887 mm->command != OFPMC13_DELETE) {
1888 return OFPERR_OFPMMFC_BAD_COMMAND;
1890 mm->meter.meter_id = ntohl(omm->meter_id);
1892 if (mm->command == OFPMC13_DELETE) {
1893 mm->meter.flags = 0;
1894 mm->meter.n_bands = 0;
1895 mm->meter.bands = NULL;
1899 mm->meter.flags = ntohs(omm->flags);
1900 if (mm->meter.flags & OFPMF13_KBPS &&
1901 mm->meter.flags & OFPMF13_PKTPS) {
1902 return OFPERR_OFPMMFC_BAD_FLAGS;
1904 mm->meter.bands = bands->data;
1906 error = ofputil_pull_bands(&b, b.size, &mm->meter.n_bands, bands);
1915 ofputil_decode_meter_request(const struct ofp_header *oh, uint32_t *meter_id)
1917 const struct ofp13_meter_multipart_request *omr = ofpmsg_body(oh);
1918 *meter_id = ntohl(omr->meter_id);
1922 ofputil_encode_meter_request(enum ofp_version ofp_version,
1923 enum ofputil_meter_request_type type,
1931 case OFPUTIL_METER_CONFIG:
1932 raw = OFPRAW_OFPST13_METER_CONFIG_REQUEST;
1934 case OFPUTIL_METER_STATS:
1935 raw = OFPRAW_OFPST13_METER_REQUEST;
1938 case OFPUTIL_METER_FEATURES:
1939 raw = OFPRAW_OFPST13_METER_FEATURES_REQUEST;
1943 msg = ofpraw_alloc(raw, ofp_version, 0);
1945 if (type != OFPUTIL_METER_FEATURES) {
1946 struct ofp13_meter_multipart_request *omr;
1947 omr = ofpbuf_put_zeros(msg, sizeof *omr);
1948 omr->meter_id = htonl(meter_id);
1954 ofputil_put_bands(uint16_t n_bands, const struct ofputil_meter_band *mb,
1959 for (n = 0; n < n_bands; ++n) {
1960 /* Currently all band types have same size. */
1961 struct ofp13_meter_band_dscp_remark *ombh;
1962 size_t ombh_len = sizeof *ombh;
1964 ombh = ofpbuf_put_zeros(msg, ombh_len);
1966 ombh->type = htons(mb->type);
1967 ombh->len = htons(ombh_len);
1968 ombh->rate = htonl(mb->rate);
1969 ombh->burst_size = htonl(mb->burst_size);
1970 ombh->prec_level = mb->prec_level;
1976 /* Encode a meter stat for 'mc' and append it to 'replies'. */
1978 ofputil_append_meter_config(struct ovs_list *replies,
1979 const struct ofputil_meter_config *mc)
1981 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
1982 size_t start_ofs = msg->size;
1983 struct ofp13_meter_config *reply = ofpbuf_put_uninit(msg, sizeof *reply);
1984 reply->flags = htons(mc->flags);
1985 reply->meter_id = htonl(mc->meter_id);
1987 ofputil_put_bands(mc->n_bands, mc->bands, msg);
1989 reply->length = htons(msg->size - start_ofs);
1991 ofpmp_postappend(replies, start_ofs);
1994 /* Encode a meter stat for 'ms' and append it to 'replies'. */
1996 ofputil_append_meter_stats(struct ovs_list *replies,
1997 const struct ofputil_meter_stats *ms)
1999 struct ofp13_meter_stats *reply;
2003 len = sizeof *reply + ms->n_bands * sizeof(struct ofp13_meter_band_stats);
2004 reply = ofpmp_append(replies, len);
2006 reply->meter_id = htonl(ms->meter_id);
2007 reply->len = htons(len);
2008 memset(reply->pad, 0, sizeof reply->pad);
2009 reply->flow_count = htonl(ms->flow_count);
2010 reply->packet_in_count = htonll(ms->packet_in_count);
2011 reply->byte_in_count = htonll(ms->byte_in_count);
2012 reply->duration_sec = htonl(ms->duration_sec);
2013 reply->duration_nsec = htonl(ms->duration_nsec);
2015 for (n = 0; n < ms->n_bands; ++n) {
2016 const struct ofputil_meter_band_stats *src = &ms->bands[n];
2017 struct ofp13_meter_band_stats *dst = &reply->band_stats[n];
2019 dst->packet_band_count = htonll(src->packet_count);
2020 dst->byte_band_count = htonll(src->byte_count);
2024 /* Converts an OFPMP_METER_CONFIG reply in 'msg' into an abstract
2025 * ofputil_meter_config in 'mc', with mc->bands pointing to bands decoded into
2026 * 'bands'. The caller must have initialized 'bands' and retains ownership of
2027 * it across the call.
2029 * Multiple OFPST13_METER_CONFIG replies can be packed into a single OpenFlow
2030 * message. Calling this function multiple times for a single 'msg' iterates
2031 * through the replies. 'bands' is cleared for each reply.
2033 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2034 * otherwise a positive errno value. */
2036 ofputil_decode_meter_config(struct ofpbuf *msg,
2037 struct ofputil_meter_config *mc,
2038 struct ofpbuf *bands)
2040 const struct ofp13_meter_config *omc;
2043 /* Pull OpenFlow headers for the first call. */
2045 ofpraw_pull_assert(msg);
2052 omc = ofpbuf_try_pull(msg, sizeof *omc);
2054 VLOG_WARN_RL(&bad_ofmsg_rl,
2055 "OFPMP_METER_CONFIG reply has %"PRIu32" leftover bytes at end",
2057 return OFPERR_OFPBRC_BAD_LEN;
2060 ofpbuf_clear(bands);
2061 err = ofputil_pull_bands(msg, ntohs(omc->length) - sizeof *omc,
2062 &mc->n_bands, bands);
2066 mc->meter_id = ntohl(omc->meter_id);
2067 mc->flags = ntohs(omc->flags);
2068 mc->bands = bands->data;
2074 ofputil_pull_band_stats(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
2075 struct ofpbuf *bands)
2077 const struct ofp13_meter_band_stats *ombs;
2078 struct ofputil_meter_band_stats *mbs;
2081 ombs = ofpbuf_try_pull(msg, len);
2083 return OFPERR_OFPBRC_BAD_LEN;
2086 n = len / sizeof *ombs;
2087 if (len != n * sizeof *ombs) {
2088 return OFPERR_OFPBRC_BAD_LEN;
2091 mbs = ofpbuf_put_uninit(bands, len);
2093 for (i = 0; i < n; ++i) {
2094 mbs[i].packet_count = ntohll(ombs[i].packet_band_count);
2095 mbs[i].byte_count = ntohll(ombs[i].byte_band_count);
2101 /* Converts an OFPMP_METER reply in 'msg' into an abstract
2102 * ofputil_meter_stats in 'ms', with ms->bands pointing to band stats
2103 * decoded into 'bands'.
2105 * Multiple OFPMP_METER replies can be packed into a single OpenFlow
2106 * message. Calling this function multiple times for a single 'msg' iterates
2107 * through the replies. 'bands' is cleared for each reply.
2109 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2110 * otherwise a positive errno value. */
2112 ofputil_decode_meter_stats(struct ofpbuf *msg,
2113 struct ofputil_meter_stats *ms,
2114 struct ofpbuf *bands)
2116 const struct ofp13_meter_stats *oms;
2119 /* Pull OpenFlow headers for the first call. */
2121 ofpraw_pull_assert(msg);
2128 oms = ofpbuf_try_pull(msg, sizeof *oms);
2130 VLOG_WARN_RL(&bad_ofmsg_rl,
2131 "OFPMP_METER reply has %"PRIu32" leftover bytes at end",
2133 return OFPERR_OFPBRC_BAD_LEN;
2136 ofpbuf_clear(bands);
2137 err = ofputil_pull_band_stats(msg, ntohs(oms->len) - sizeof *oms,
2138 &ms->n_bands, bands);
2142 ms->meter_id = ntohl(oms->meter_id);
2143 ms->flow_count = ntohl(oms->flow_count);
2144 ms->packet_in_count = ntohll(oms->packet_in_count);
2145 ms->byte_in_count = ntohll(oms->byte_in_count);
2146 ms->duration_sec = ntohl(oms->duration_sec);
2147 ms->duration_nsec = ntohl(oms->duration_nsec);
2148 ms->bands = bands->data;
2154 ofputil_decode_meter_features(const struct ofp_header *oh,
2155 struct ofputil_meter_features *mf)
2157 const struct ofp13_meter_features *omf = ofpmsg_body(oh);
2159 mf->max_meters = ntohl(omf->max_meter);
2160 mf->band_types = ntohl(omf->band_types);
2161 mf->capabilities = ntohl(omf->capabilities);
2162 mf->max_bands = omf->max_bands;
2163 mf->max_color = omf->max_color;
2167 ofputil_encode_meter_features_reply(const struct ofputil_meter_features *mf,
2168 const struct ofp_header *request)
2170 struct ofpbuf *reply;
2171 struct ofp13_meter_features *omf;
2173 reply = ofpraw_alloc_stats_reply(request, 0);
2174 omf = ofpbuf_put_zeros(reply, sizeof *omf);
2176 omf->max_meter = htonl(mf->max_meters);
2177 omf->band_types = htonl(mf->band_types);
2178 omf->capabilities = htonl(mf->capabilities);
2179 omf->max_bands = mf->max_bands;
2180 omf->max_color = mf->max_color;
2186 ofputil_encode_meter_mod(enum ofp_version ofp_version,
2187 const struct ofputil_meter_mod *mm)
2191 struct ofp13_meter_mod *omm;
2193 msg = ofpraw_alloc(OFPRAW_OFPT13_METER_MOD, ofp_version,
2194 NXM_TYPICAL_LEN + mm->meter.n_bands * 16);
2195 omm = ofpbuf_put_zeros(msg, sizeof *omm);
2196 omm->command = htons(mm->command);
2197 if (mm->command != OFPMC13_DELETE) {
2198 omm->flags = htons(mm->meter.flags);
2200 omm->meter_id = htonl(mm->meter.meter_id);
2202 ofputil_put_bands(mm->meter.n_bands, mm->meter.bands, msg);
2204 ofpmsg_update_length(msg);
2209 ofputil_tid_command(const struct ofputil_flow_mod *fm,
2210 enum ofputil_protocol protocol)
2212 return htons(protocol & OFPUTIL_P_TID
2213 ? (fm->command & 0xff) | (fm->table_id << 8)
2217 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
2218 * 'protocol' and returns the message. */
2220 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
2221 enum ofputil_protocol protocol)
2223 enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
2224 ovs_be16 raw_flags = ofputil_encode_flow_mod_flags(fm->flags, version);
2228 case OFPUTIL_P_OF11_STD:
2229 case OFPUTIL_P_OF12_OXM:
2230 case OFPUTIL_P_OF13_OXM:
2231 case OFPUTIL_P_OF14_OXM:
2232 case OFPUTIL_P_OF15_OXM: {
2233 struct ofp11_flow_mod *ofm;
2236 tailroom = ofputil_match_typical_len(protocol) + fm->ofpacts_len;
2237 msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, version, tailroom);
2238 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
2239 if ((protocol == OFPUTIL_P_OF11_STD
2240 && (fm->command == OFPFC_MODIFY ||
2241 fm->command == OFPFC_MODIFY_STRICT)
2242 && fm->cookie_mask == htonll(0))
2243 || fm->command == OFPFC_ADD) {
2244 ofm->cookie = fm->new_cookie;
2246 ofm->cookie = fm->cookie & fm->cookie_mask;
2248 ofm->cookie_mask = fm->cookie_mask;
2249 if (fm->table_id != OFPTT_ALL
2250 || (protocol != OFPUTIL_P_OF11_STD
2251 && (fm->command == OFPFC_DELETE ||
2252 fm->command == OFPFC_DELETE_STRICT))) {
2253 ofm->table_id = fm->table_id;
2257 ofm->command = fm->command;
2258 ofm->idle_timeout = htons(fm->idle_timeout);
2259 ofm->hard_timeout = htons(fm->hard_timeout);
2260 ofm->priority = htons(fm->priority);
2261 ofm->buffer_id = htonl(fm->buffer_id);
2262 ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
2263 ofm->out_group = htonl(fm->out_group);
2264 ofm->flags = raw_flags;
2265 if (version >= OFP14_VERSION && fm->command == OFPFC_ADD) {
2266 ofm->importance = htons(fm->importance);
2268 ofm->importance = 0;
2270 ofputil_put_ofp11_match(msg, &fm->match, protocol);
2271 ofpacts_put_openflow_instructions(fm->ofpacts, fm->ofpacts_len, msg,
2276 case OFPUTIL_P_OF10_STD:
2277 case OFPUTIL_P_OF10_STD_TID: {
2278 struct ofp10_flow_mod *ofm;
2280 msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
2282 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
2283 ofputil_match_to_ofp10_match(&fm->match, &ofm->match);
2284 ofm->cookie = fm->new_cookie;
2285 ofm->command = ofputil_tid_command(fm, protocol);
2286 ofm->idle_timeout = htons(fm->idle_timeout);
2287 ofm->hard_timeout = htons(fm->hard_timeout);
2288 ofm->priority = htons(fm->priority);
2289 ofm->buffer_id = htonl(fm->buffer_id);
2290 ofm->out_port = htons(ofp_to_u16(fm->out_port));
2291 ofm->flags = raw_flags;
2292 ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2297 case OFPUTIL_P_OF10_NXM:
2298 case OFPUTIL_P_OF10_NXM_TID: {
2299 struct nx_flow_mod *nfm;
2302 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
2303 NXM_TYPICAL_LEN + fm->ofpacts_len);
2304 nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
2305 nfm->command = ofputil_tid_command(fm, protocol);
2306 nfm->cookie = fm->new_cookie;
2307 match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask);
2309 nfm->idle_timeout = htons(fm->idle_timeout);
2310 nfm->hard_timeout = htons(fm->hard_timeout);
2311 nfm->priority = htons(fm->priority);
2312 nfm->buffer_id = htonl(fm->buffer_id);
2313 nfm->out_port = htons(ofp_to_u16(fm->out_port));
2314 nfm->flags = raw_flags;
2315 nfm->match_len = htons(match_len);
2316 ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2325 ofpmsg_update_length(msg);
2330 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr,
2331 const struct ofp10_flow_stats_request *ofsr,
2334 fsr->aggregate = aggregate;
2335 ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match);
2336 fsr->out_port = u16_to_ofp(ntohs(ofsr->out_port));
2337 fsr->out_group = OFPG11_ANY;
2338 fsr->table_id = ofsr->table_id;
2339 fsr->cookie = fsr->cookie_mask = htonll(0);
2345 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr,
2346 struct ofpbuf *b, bool aggregate)
2348 const struct ofp11_flow_stats_request *ofsr;
2351 ofsr = ofpbuf_pull(b, sizeof *ofsr);
2352 fsr->aggregate = aggregate;
2353 fsr->table_id = ofsr->table_id;
2354 error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port);
2358 fsr->out_group = ntohl(ofsr->out_group);
2359 fsr->cookie = ofsr->cookie;
2360 fsr->cookie_mask = ofsr->cookie_mask;
2361 error = ofputil_pull_ofp11_match(b, &fsr->match, NULL);
2370 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
2371 struct ofpbuf *b, bool aggregate)
2373 const struct nx_flow_stats_request *nfsr;
2376 nfsr = ofpbuf_pull(b, sizeof *nfsr);
2377 error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match,
2378 &fsr->cookie, &fsr->cookie_mask);
2383 return OFPERR_OFPBRC_BAD_LEN;
2386 fsr->aggregate = aggregate;
2387 fsr->out_port = u16_to_ofp(ntohs(nfsr->out_port));
2388 fsr->out_group = OFPG11_ANY;
2389 fsr->table_id = nfsr->table_id;
2394 /* Constructs and returns an OFPT_QUEUE_GET_CONFIG request for the specified
2395 * 'port', suitable for OpenFlow version 'version'. */
2397 ofputil_encode_queue_get_config_request(enum ofp_version version,
2400 struct ofpbuf *request;
2402 if (version == OFP10_VERSION) {
2403 struct ofp10_queue_get_config_request *qgcr10;
2405 request = ofpraw_alloc(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST,
2407 qgcr10 = ofpbuf_put_zeros(request, sizeof *qgcr10);
2408 qgcr10->port = htons(ofp_to_u16(port));
2410 struct ofp11_queue_get_config_request *qgcr11;
2412 request = ofpraw_alloc(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST,
2414 qgcr11 = ofpbuf_put_zeros(request, sizeof *qgcr11);
2415 qgcr11->port = ofputil_port_to_ofp11(port);
2421 /* Parses OFPT_QUEUE_GET_CONFIG request 'oh', storing the port specified by the
2422 * request into '*port'. Returns 0 if successful, otherwise an OpenFlow error
2425 ofputil_decode_queue_get_config_request(const struct ofp_header *oh,
2428 const struct ofp10_queue_get_config_request *qgcr10;
2429 const struct ofp11_queue_get_config_request *qgcr11;
2433 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2434 raw = ofpraw_pull_assert(&b);
2436 switch ((int) raw) {
2437 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2439 *port = u16_to_ofp(ntohs(qgcr10->port));
2442 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2444 return ofputil_port_from_ofp11(qgcr11->port, port);
2450 /* Constructs and returns the beginning of a reply to
2451 * OFPT_QUEUE_GET_CONFIG_REQUEST 'oh'. The caller may append information about
2452 * individual queues with ofputil_append_queue_get_config_reply(). */
2454 ofputil_encode_queue_get_config_reply(const struct ofp_header *oh)
2456 struct ofp10_queue_get_config_reply *qgcr10;
2457 struct ofp11_queue_get_config_reply *qgcr11;
2458 struct ofpbuf *reply;
2464 error = ofputil_decode_queue_get_config_request(oh, &port);
2467 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2468 raw = ofpraw_pull_assert(&b);
2470 switch ((int) raw) {
2471 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2472 reply = ofpraw_alloc_reply(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY,
2474 qgcr10 = ofpbuf_put_zeros(reply, sizeof *qgcr10);
2475 qgcr10->port = htons(ofp_to_u16(port));
2478 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2479 reply = ofpraw_alloc_reply(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY,
2481 qgcr11 = ofpbuf_put_zeros(reply, sizeof *qgcr11);
2482 qgcr11->port = ofputil_port_to_ofp11(port);
2493 put_queue_rate(struct ofpbuf *reply, enum ofp_queue_properties property,
2496 if (rate != UINT16_MAX) {
2497 struct ofp_queue_prop_rate *oqpr;
2499 oqpr = ofpbuf_put_zeros(reply, sizeof *oqpr);
2500 oqpr->prop_header.property = htons(property);
2501 oqpr->prop_header.len = htons(sizeof *oqpr);
2502 oqpr->rate = htons(rate);
2506 /* Appends a queue description for 'queue_id' to the
2507 * OFPT_QUEUE_GET_CONFIG_REPLY already in 'oh'. */
2509 ofputil_append_queue_get_config_reply(struct ofpbuf *reply,
2510 const struct ofputil_queue_config *oqc)
2512 const struct ofp_header *oh = reply->data;
2513 size_t start_ofs, len_ofs;
2516 start_ofs = reply->size;
2517 if (oh->version < OFP12_VERSION) {
2518 struct ofp10_packet_queue *opq10;
2520 opq10 = ofpbuf_put_zeros(reply, sizeof *opq10);
2521 opq10->queue_id = htonl(oqc->queue_id);
2522 len_ofs = (char *) &opq10->len - (char *) reply->data;
2524 struct ofp11_queue_get_config_reply *qgcr11;
2525 struct ofp12_packet_queue *opq12;
2528 qgcr11 = reply->msg;
2529 port = qgcr11->port;
2531 opq12 = ofpbuf_put_zeros(reply, sizeof *opq12);
2533 opq12->queue_id = htonl(oqc->queue_id);
2534 len_ofs = (char *) &opq12->len - (char *) reply->data;
2537 put_queue_rate(reply, OFPQT_MIN_RATE, oqc->min_rate);
2538 put_queue_rate(reply, OFPQT_MAX_RATE, oqc->max_rate);
2540 len = ofpbuf_at(reply, len_ofs, sizeof *len);
2541 *len = htons(reply->size - start_ofs);
2544 /* Decodes the initial part of an OFPT_QUEUE_GET_CONFIG_REPLY from 'reply' and
2545 * stores in '*port' the port that the reply is about. The caller may call
2546 * ofputil_pull_queue_get_config_reply() to obtain information about individual
2547 * queues included in the reply. Returns 0 if successful, otherwise an
2550 ofputil_decode_queue_get_config_reply(struct ofpbuf *reply, ofp_port_t *port)
2552 const struct ofp10_queue_get_config_reply *qgcr10;
2553 const struct ofp11_queue_get_config_reply *qgcr11;
2556 raw = ofpraw_pull_assert(reply);
2557 switch ((int) raw) {
2558 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY:
2559 qgcr10 = ofpbuf_pull(reply, sizeof *qgcr10);
2560 *port = u16_to_ofp(ntohs(qgcr10->port));
2563 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY:
2564 qgcr11 = ofpbuf_pull(reply, sizeof *qgcr11);
2565 return ofputil_port_from_ofp11(qgcr11->port, port);
2572 parse_queue_rate(const struct ofp_queue_prop_header *hdr, uint16_t *rate)
2574 const struct ofp_queue_prop_rate *oqpr;
2576 if (hdr->len == htons(sizeof *oqpr)) {
2577 oqpr = (const struct ofp_queue_prop_rate *) hdr;
2578 *rate = ntohs(oqpr->rate);
2581 return OFPERR_OFPBRC_BAD_LEN;
2585 /* Decodes information about a queue from the OFPT_QUEUE_GET_CONFIG_REPLY in
2586 * 'reply' and stores it in '*queue'. ofputil_decode_queue_get_config_reply()
2587 * must already have pulled off the main header.
2589 * This function returns EOF if the last queue has already been decoded, 0 if a
2590 * queue was successfully decoded into '*queue', or an ofperr if there was a
2591 * problem decoding 'reply'. */
2593 ofputil_pull_queue_get_config_reply(struct ofpbuf *reply,
2594 struct ofputil_queue_config *queue)
2596 const struct ofp_header *oh;
2597 unsigned int opq_len;
2604 queue->min_rate = UINT16_MAX;
2605 queue->max_rate = UINT16_MAX;
2608 if (oh->version < OFP12_VERSION) {
2609 const struct ofp10_packet_queue *opq10;
2611 opq10 = ofpbuf_try_pull(reply, sizeof *opq10);
2613 return OFPERR_OFPBRC_BAD_LEN;
2615 queue->queue_id = ntohl(opq10->queue_id);
2616 len = ntohs(opq10->len);
2617 opq_len = sizeof *opq10;
2619 const struct ofp12_packet_queue *opq12;
2621 opq12 = ofpbuf_try_pull(reply, sizeof *opq12);
2623 return OFPERR_OFPBRC_BAD_LEN;
2625 queue->queue_id = ntohl(opq12->queue_id);
2626 len = ntohs(opq12->len);
2627 opq_len = sizeof *opq12;
2630 if (len < opq_len || len > reply->size + opq_len || len % 8) {
2631 return OFPERR_OFPBRC_BAD_LEN;
2636 const struct ofp_queue_prop_header *hdr;
2637 unsigned int property;
2638 unsigned int prop_len;
2639 enum ofperr error = 0;
2641 hdr = ofpbuf_at_assert(reply, 0, sizeof *hdr);
2642 prop_len = ntohs(hdr->len);
2643 if (prop_len < sizeof *hdr || prop_len > reply->size || prop_len % 8) {
2644 return OFPERR_OFPBRC_BAD_LEN;
2647 property = ntohs(hdr->property);
2649 case OFPQT_MIN_RATE:
2650 error = parse_queue_rate(hdr, &queue->min_rate);
2653 case OFPQT_MAX_RATE:
2654 error = parse_queue_rate(hdr, &queue->max_rate);
2658 VLOG_INFO_RL(&bad_ofmsg_rl, "unknown queue property %u", property);
2665 ofpbuf_pull(reply, prop_len);
2671 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
2672 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
2673 * successful, otherwise an OpenFlow error code. */
2675 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
2676 const struct ofp_header *oh)
2681 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2682 raw = ofpraw_pull_assert(&b);
2683 switch ((int) raw) {
2684 case OFPRAW_OFPST10_FLOW_REQUEST:
2685 return ofputil_decode_ofpst10_flow_request(fsr, b.data, false);
2687 case OFPRAW_OFPST10_AGGREGATE_REQUEST:
2688 return ofputil_decode_ofpst10_flow_request(fsr, b.data, true);
2690 case OFPRAW_OFPST11_FLOW_REQUEST:
2691 return ofputil_decode_ofpst11_flow_request(fsr, &b, false);
2693 case OFPRAW_OFPST11_AGGREGATE_REQUEST:
2694 return ofputil_decode_ofpst11_flow_request(fsr, &b, true);
2696 case OFPRAW_NXST_FLOW_REQUEST:
2697 return ofputil_decode_nxst_flow_request(fsr, &b, false);
2699 case OFPRAW_NXST_AGGREGATE_REQUEST:
2700 return ofputil_decode_nxst_flow_request(fsr, &b, true);
2703 /* Hey, the caller lied. */
2708 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
2709 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
2710 * 'protocol', and returns the message. */
2712 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
2713 enum ofputil_protocol protocol)
2719 case OFPUTIL_P_OF11_STD:
2720 case OFPUTIL_P_OF12_OXM:
2721 case OFPUTIL_P_OF13_OXM:
2722 case OFPUTIL_P_OF14_OXM:
2723 case OFPUTIL_P_OF15_OXM: {
2724 struct ofp11_flow_stats_request *ofsr;
2726 raw = (fsr->aggregate
2727 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
2728 : OFPRAW_OFPST11_FLOW_REQUEST);
2729 msg = ofpraw_alloc(raw, ofputil_protocol_to_ofp_version(protocol),
2730 ofputil_match_typical_len(protocol));
2731 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2732 ofsr->table_id = fsr->table_id;
2733 ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port);
2734 ofsr->out_group = htonl(fsr->out_group);
2735 ofsr->cookie = fsr->cookie;
2736 ofsr->cookie_mask = fsr->cookie_mask;
2737 ofputil_put_ofp11_match(msg, &fsr->match, protocol);
2741 case OFPUTIL_P_OF10_STD:
2742 case OFPUTIL_P_OF10_STD_TID: {
2743 struct ofp10_flow_stats_request *ofsr;
2745 raw = (fsr->aggregate
2746 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
2747 : OFPRAW_OFPST10_FLOW_REQUEST);
2748 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
2749 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2750 ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match);
2751 ofsr->table_id = fsr->table_id;
2752 ofsr->out_port = htons(ofp_to_u16(fsr->out_port));
2756 case OFPUTIL_P_OF10_NXM:
2757 case OFPUTIL_P_OF10_NXM_TID: {
2758 struct nx_flow_stats_request *nfsr;
2761 raw = (fsr->aggregate
2762 ? OFPRAW_NXST_AGGREGATE_REQUEST
2763 : OFPRAW_NXST_FLOW_REQUEST);
2764 msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN);
2765 ofpbuf_put_zeros(msg, sizeof *nfsr);
2766 match_len = nx_put_match(msg, &fsr->match,
2767 fsr->cookie, fsr->cookie_mask);
2770 nfsr->out_port = htons(ofp_to_u16(fsr->out_port));
2771 nfsr->match_len = htons(match_len);
2772 nfsr->table_id = fsr->table_id;
2783 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
2784 * ofputil_flow_stats in 'fs'.
2786 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
2787 * OpenFlow message. Calling this function multiple times for a single 'msg'
2788 * iterates through the replies. The caller must initially leave 'msg''s layer
2789 * pointers null and not modify them between calls.
2791 * Most switches don't send the values needed to populate fs->idle_age and
2792 * fs->hard_age, so those members will usually be set to 0. If the switch from
2793 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
2794 * 'flow_age_extension' as true so that the contents of 'msg' determine the
2795 * 'idle_age' and 'hard_age' members in 'fs'.
2797 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
2798 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
2799 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
2801 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2802 * otherwise a positive errno value. */
2804 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
2806 bool flow_age_extension,
2807 struct ofpbuf *ofpacts)
2809 const struct ofp_header *oh;
2810 size_t instructions_len;
2814 error = (msg->header ? ofpraw_decode(&raw, msg->header)
2815 : ofpraw_pull(&raw, msg));
2823 } else if (raw == OFPRAW_OFPST11_FLOW_REPLY
2824 || raw == OFPRAW_OFPST13_FLOW_REPLY) {
2825 const struct ofp11_flow_stats *ofs;
2827 uint16_t padded_match_len;
2829 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2831 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
2832 "bytes at end", msg->size);
2836 length = ntohs(ofs->length);
2837 if (length < sizeof *ofs) {
2838 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2839 "length %"PRIuSIZE, length);
2843 if (ofputil_pull_ofp11_match(msg, &fs->match, &padded_match_len)) {
2844 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match");
2847 instructions_len = length - sizeof *ofs - padded_match_len;
2849 fs->priority = ntohs(ofs->priority);
2850 fs->table_id = ofs->table_id;
2851 fs->duration_sec = ntohl(ofs->duration_sec);
2852 fs->duration_nsec = ntohl(ofs->duration_nsec);
2853 fs->idle_timeout = ntohs(ofs->idle_timeout);
2854 fs->hard_timeout = ntohs(ofs->hard_timeout);
2855 if (oh->version >= OFP14_VERSION) {
2856 fs->importance = ntohs(ofs->importance);
2860 if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
2861 error = ofputil_decode_flow_mod_flags(ofs->flags, -1, oh->version,
2871 fs->cookie = ofs->cookie;
2872 fs->packet_count = ntohll(ofs->packet_count);
2873 fs->byte_count = ntohll(ofs->byte_count);
2874 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
2875 const struct ofp10_flow_stats *ofs;
2878 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2880 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
2881 "bytes at end", msg->size);
2885 length = ntohs(ofs->length);
2886 if (length < sizeof *ofs) {
2887 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2888 "length %"PRIuSIZE, length);
2891 instructions_len = length - sizeof *ofs;
2893 fs->cookie = get_32aligned_be64(&ofs->cookie);
2894 ofputil_match_from_ofp10_match(&ofs->match, &fs->match);
2895 fs->priority = ntohs(ofs->priority);
2896 fs->table_id = ofs->table_id;
2897 fs->duration_sec = ntohl(ofs->duration_sec);
2898 fs->duration_nsec = ntohl(ofs->duration_nsec);
2899 fs->idle_timeout = ntohs(ofs->idle_timeout);
2900 fs->hard_timeout = ntohs(ofs->hard_timeout);
2904 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
2905 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
2907 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
2908 const struct nx_flow_stats *nfs;
2909 size_t match_len, length;
2911 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
2913 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %"PRIu32" leftover "
2914 "bytes at end", msg->size);
2918 length = ntohs(nfs->length);
2919 match_len = ntohs(nfs->match_len);
2920 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
2921 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%"PRIuSIZE" "
2922 "claims invalid length %"PRIuSIZE, match_len, length);
2925 if (nx_pull_match(msg, match_len, &fs->match, NULL, NULL)) {
2928 instructions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
2930 fs->cookie = nfs->cookie;
2931 fs->table_id = nfs->table_id;
2932 fs->duration_sec = ntohl(nfs->duration_sec);
2933 fs->duration_nsec = ntohl(nfs->duration_nsec);
2934 fs->priority = ntohs(nfs->priority);
2935 fs->idle_timeout = ntohs(nfs->idle_timeout);
2936 fs->hard_timeout = ntohs(nfs->hard_timeout);
2940 if (flow_age_extension) {
2941 if (nfs->idle_age) {
2942 fs->idle_age = ntohs(nfs->idle_age) - 1;
2944 if (nfs->hard_age) {
2945 fs->hard_age = ntohs(nfs->hard_age) - 1;
2948 fs->packet_count = ntohll(nfs->packet_count);
2949 fs->byte_count = ntohll(nfs->byte_count);
2955 if (ofpacts_pull_openflow_instructions(msg, instructions_len, oh->version,
2957 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions");
2960 fs->ofpacts = ofpacts->data;
2961 fs->ofpacts_len = ofpacts->size;
2966 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
2968 * We use this in situations where OVS internally uses UINT64_MAX to mean
2969 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
2971 unknown_to_zero(uint64_t count)
2973 return count != UINT64_MAX ? count : 0;
2976 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
2977 * those already present in the list of ofpbufs in 'replies'. 'replies' should
2978 * have been initialized with ofpmp_init(). */
2980 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
2981 struct ovs_list *replies)
2983 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
2984 size_t start_ofs = reply->size;
2985 enum ofp_version version = ofpmp_version(replies);
2986 enum ofpraw raw = ofpmp_decode_raw(replies);
2988 if (raw == OFPRAW_OFPST11_FLOW_REPLY || raw == OFPRAW_OFPST13_FLOW_REPLY) {
2989 struct ofp11_flow_stats *ofs;
2991 ofpbuf_put_uninit(reply, sizeof *ofs);
2992 oxm_put_match(reply, &fs->match, version);
2993 ofpacts_put_openflow_instructions(fs->ofpacts, fs->ofpacts_len, reply,
2996 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
2997 ofs->length = htons(reply->size - start_ofs);
2998 ofs->table_id = fs->table_id;
3000 ofs->duration_sec = htonl(fs->duration_sec);
3001 ofs->duration_nsec = htonl(fs->duration_nsec);
3002 ofs->priority = htons(fs->priority);
3003 ofs->idle_timeout = htons(fs->idle_timeout);
3004 ofs->hard_timeout = htons(fs->hard_timeout);
3005 if (version >= OFP14_VERSION) {
3006 ofs->importance = htons(fs->importance);
3008 ofs->importance = 0;
3010 if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
3011 ofs->flags = ofputil_encode_flow_mod_flags(fs->flags, version);
3015 memset(ofs->pad2, 0, sizeof ofs->pad2);
3016 ofs->cookie = fs->cookie;
3017 ofs->packet_count = htonll(unknown_to_zero(fs->packet_count));
3018 ofs->byte_count = htonll(unknown_to_zero(fs->byte_count));
3019 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
3020 struct ofp10_flow_stats *ofs;
3022 ofpbuf_put_uninit(reply, sizeof *ofs);
3023 ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
3025 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
3026 ofs->length = htons(reply->size - start_ofs);
3027 ofs->table_id = fs->table_id;
3029 ofputil_match_to_ofp10_match(&fs->match, &ofs->match);
3030 ofs->duration_sec = htonl(fs->duration_sec);
3031 ofs->duration_nsec = htonl(fs->duration_nsec);
3032 ofs->priority = htons(fs->priority);
3033 ofs->idle_timeout = htons(fs->idle_timeout);
3034 ofs->hard_timeout = htons(fs->hard_timeout);
3035 memset(ofs->pad2, 0, sizeof ofs->pad2);
3036 put_32aligned_be64(&ofs->cookie, fs->cookie);
3037 put_32aligned_be64(&ofs->packet_count,
3038 htonll(unknown_to_zero(fs->packet_count)));
3039 put_32aligned_be64(&ofs->byte_count,
3040 htonll(unknown_to_zero(fs->byte_count)));
3041 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
3042 struct nx_flow_stats *nfs;
3045 ofpbuf_put_uninit(reply, sizeof *nfs);
3046 match_len = nx_put_match(reply, &fs->match, 0, 0);
3047 ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
3049 nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
3050 nfs->length = htons(reply->size - start_ofs);
3051 nfs->table_id = fs->table_id;
3053 nfs->duration_sec = htonl(fs->duration_sec);
3054 nfs->duration_nsec = htonl(fs->duration_nsec);
3055 nfs->priority = htons(fs->priority);
3056 nfs->idle_timeout = htons(fs->idle_timeout);
3057 nfs->hard_timeout = htons(fs->hard_timeout);
3058 nfs->idle_age = htons(fs->idle_age < 0 ? 0
3059 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
3061 nfs->hard_age = htons(fs->hard_age < 0 ? 0
3062 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
3064 nfs->match_len = htons(match_len);
3065 nfs->cookie = fs->cookie;
3066 nfs->packet_count = htonll(fs->packet_count);
3067 nfs->byte_count = htonll(fs->byte_count);
3072 ofpmp_postappend(replies, start_ofs);
3075 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
3076 * NXST_AGGREGATE reply matching 'request', and returns the message. */
3078 ofputil_encode_aggregate_stats_reply(
3079 const struct ofputil_aggregate_stats *stats,
3080 const struct ofp_header *request)
3082 struct ofp_aggregate_stats_reply *asr;
3083 uint64_t packet_count;
3084 uint64_t byte_count;
3088 ofpraw_decode(&raw, request);
3089 if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) {
3090 packet_count = unknown_to_zero(stats->packet_count);
3091 byte_count = unknown_to_zero(stats->byte_count);
3093 packet_count = stats->packet_count;
3094 byte_count = stats->byte_count;
3097 msg = ofpraw_alloc_stats_reply(request, 0);
3098 asr = ofpbuf_put_zeros(msg, sizeof *asr);
3099 put_32aligned_be64(&asr->packet_count, htonll(packet_count));
3100 put_32aligned_be64(&asr->byte_count, htonll(byte_count));
3101 asr->flow_count = htonl(stats->flow_count);
3107 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
3108 const struct ofp_header *reply)
3110 struct ofp_aggregate_stats_reply *asr;
3113 ofpbuf_use_const(&msg, reply, ntohs(reply->length));
3114 ofpraw_pull_assert(&msg);
3117 stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
3118 stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
3119 stats->flow_count = ntohl(asr->flow_count);
3124 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
3125 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
3126 * an OpenFlow error code. */
3128 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
3129 const struct ofp_header *oh)
3134 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3135 raw = ofpraw_pull_assert(&b);
3136 if (raw == OFPRAW_OFPT11_FLOW_REMOVED) {
3137 const struct ofp12_flow_removed *ofr;
3140 ofr = ofpbuf_pull(&b, sizeof *ofr);
3142 error = ofputil_pull_ofp11_match(&b, &fr->match, NULL);
3147 fr->priority = ntohs(ofr->priority);
3148 fr->cookie = ofr->cookie;
3149 fr->reason = ofr->reason;
3150 fr->table_id = ofr->table_id;
3151 fr->duration_sec = ntohl(ofr->duration_sec);
3152 fr->duration_nsec = ntohl(ofr->duration_nsec);
3153 fr->idle_timeout = ntohs(ofr->idle_timeout);
3154 fr->hard_timeout = ntohs(ofr->hard_timeout);
3155 fr->packet_count = ntohll(ofr->packet_count);
3156 fr->byte_count = ntohll(ofr->byte_count);
3157 } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
3158 const struct ofp10_flow_removed *ofr;
3160 ofr = ofpbuf_pull(&b, sizeof *ofr);
3162 ofputil_match_from_ofp10_match(&ofr->match, &fr->match);
3163 fr->priority = ntohs(ofr->priority);
3164 fr->cookie = ofr->cookie;
3165 fr->reason = ofr->reason;
3167 fr->duration_sec = ntohl(ofr->duration_sec);
3168 fr->duration_nsec = ntohl(ofr->duration_nsec);
3169 fr->idle_timeout = ntohs(ofr->idle_timeout);
3170 fr->hard_timeout = 0;
3171 fr->packet_count = ntohll(ofr->packet_count);
3172 fr->byte_count = ntohll(ofr->byte_count);
3173 } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
3174 struct nx_flow_removed *nfr;
3177 nfr = ofpbuf_pull(&b, sizeof *nfr);
3178 error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match,
3184 return OFPERR_OFPBRC_BAD_LEN;
3187 fr->priority = ntohs(nfr->priority);
3188 fr->cookie = nfr->cookie;
3189 fr->reason = nfr->reason;
3190 fr->table_id = nfr->table_id ? nfr->table_id - 1 : 255;
3191 fr->duration_sec = ntohl(nfr->duration_sec);
3192 fr->duration_nsec = ntohl(nfr->duration_nsec);
3193 fr->idle_timeout = ntohs(nfr->idle_timeout);
3194 fr->hard_timeout = 0;
3195 fr->packet_count = ntohll(nfr->packet_count);
3196 fr->byte_count = ntohll(nfr->byte_count);
3204 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
3205 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
3208 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
3209 enum ofputil_protocol protocol)
3212 enum ofp_flow_removed_reason reason = fr->reason;
3214 if (reason == OFPRR_METER_DELETE && !(protocol & OFPUTIL_P_OF14_UP)) {
3215 reason = OFPRR_DELETE;
3219 case OFPUTIL_P_OF11_STD:
3220 case OFPUTIL_P_OF12_OXM:
3221 case OFPUTIL_P_OF13_OXM:
3222 case OFPUTIL_P_OF14_OXM:
3223 case OFPUTIL_P_OF15_OXM: {
3224 struct ofp12_flow_removed *ofr;
3226 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED,
3227 ofputil_protocol_to_ofp_version(protocol),
3229 ofputil_match_typical_len(protocol));
3230 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3231 ofr->cookie = fr->cookie;
3232 ofr->priority = htons(fr->priority);
3233 ofr->reason = reason;
3234 ofr->table_id = fr->table_id;
3235 ofr->duration_sec = htonl(fr->duration_sec);
3236 ofr->duration_nsec = htonl(fr->duration_nsec);
3237 ofr->idle_timeout = htons(fr->idle_timeout);
3238 ofr->hard_timeout = htons(fr->hard_timeout);
3239 ofr->packet_count = htonll(fr->packet_count);
3240 ofr->byte_count = htonll(fr->byte_count);
3241 ofputil_put_ofp11_match(msg, &fr->match, protocol);
3245 case OFPUTIL_P_OF10_STD:
3246 case OFPUTIL_P_OF10_STD_TID: {
3247 struct ofp10_flow_removed *ofr;
3249 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
3251 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3252 ofputil_match_to_ofp10_match(&fr->match, &ofr->match);
3253 ofr->cookie = fr->cookie;
3254 ofr->priority = htons(fr->priority);
3255 ofr->reason = reason;
3256 ofr->duration_sec = htonl(fr->duration_sec);
3257 ofr->duration_nsec = htonl(fr->duration_nsec);
3258 ofr->idle_timeout = htons(fr->idle_timeout);
3259 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
3260 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
3264 case OFPUTIL_P_OF10_NXM:
3265 case OFPUTIL_P_OF10_NXM_TID: {
3266 struct nx_flow_removed *nfr;
3269 msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
3270 htonl(0), NXM_TYPICAL_LEN);
3271 ofpbuf_put_zeros(msg, sizeof *nfr);
3272 match_len = nx_put_match(msg, &fr->match, 0, 0);
3275 nfr->cookie = fr->cookie;
3276 nfr->priority = htons(fr->priority);
3277 nfr->reason = reason;
3278 nfr->table_id = fr->table_id + 1;
3279 nfr->duration_sec = htonl(fr->duration_sec);
3280 nfr->duration_nsec = htonl(fr->duration_nsec);
3281 nfr->idle_timeout = htons(fr->idle_timeout);
3282 nfr->match_len = htons(match_len);
3283 nfr->packet_count = htonll(fr->packet_count);
3284 nfr->byte_count = htonll(fr->byte_count);
3296 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
3297 const struct ofp_header *oh)
3302 memset(pin, 0, sizeof *pin);
3303 pin->cookie = OVS_BE64_MAX;
3305 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3306 raw = ofpraw_pull_assert(&b);
3307 if (raw == OFPRAW_OFPT13_PACKET_IN || raw == OFPRAW_OFPT12_PACKET_IN) {
3308 const struct ofp13_packet_in *opi;
3310 size_t packet_in_size;
3312 if (raw == OFPRAW_OFPT12_PACKET_IN) {
3313 packet_in_size = sizeof (struct ofp12_packet_in);
3315 packet_in_size = sizeof (struct ofp13_packet_in);
3318 opi = ofpbuf_pull(&b, packet_in_size);
3319 error = oxm_pull_match_loose(&b, &pin->flow_metadata);
3324 if (!ofpbuf_try_pull(&b, 2)) {
3325 return OFPERR_OFPBRC_BAD_LEN;
3328 pin->reason = opi->pi.reason;
3329 pin->table_id = opi->pi.table_id;
3330 pin->buffer_id = ntohl(opi->pi.buffer_id);
3331 pin->total_len = ntohs(opi->pi.total_len);
3333 if (raw == OFPRAW_OFPT13_PACKET_IN) {
3334 pin->cookie = opi->cookie;
3337 pin->packet = b.data;
3338 pin->packet_len = b.size;
3339 } else if (raw == OFPRAW_OFPT10_PACKET_IN) {
3340 const struct ofp10_packet_in *opi;
3342 opi = ofpbuf_pull(&b, offsetof(struct ofp10_packet_in, data));
3344 pin->packet = opi->data;
3345 pin->packet_len = b.size;
3347 match_init_catchall(&pin->flow_metadata);
3348 match_set_in_port(&pin->flow_metadata, u16_to_ofp(ntohs(opi->in_port)));
3349 pin->reason = opi->reason;
3350 pin->buffer_id = ntohl(opi->buffer_id);
3351 pin->total_len = ntohs(opi->total_len);
3352 } else if (raw == OFPRAW_OFPT11_PACKET_IN) {
3353 const struct ofp11_packet_in *opi;
3357 opi = ofpbuf_pull(&b, sizeof *opi);
3359 pin->packet = b.data;
3360 pin->packet_len = b.size;
3362 pin->buffer_id = ntohl(opi->buffer_id);
3363 error = ofputil_port_from_ofp11(opi->in_port, &in_port);
3367 match_init_catchall(&pin->flow_metadata);
3368 match_set_in_port(&pin->flow_metadata, in_port);
3369 pin->total_len = ntohs(opi->total_len);
3370 pin->reason = opi->reason;
3371 pin->table_id = opi->table_id;
3372 } else if (raw == OFPRAW_NXT_PACKET_IN) {
3373 const struct nx_packet_in *npi;
3376 npi = ofpbuf_pull(&b, sizeof *npi);
3377 error = nx_pull_match_loose(&b, ntohs(npi->match_len),
3378 &pin->flow_metadata, NULL, NULL);
3383 if (!ofpbuf_try_pull(&b, 2)) {
3384 return OFPERR_OFPBRC_BAD_LEN;
3387 pin->reason = npi->reason;
3388 pin->table_id = npi->table_id;
3389 pin->cookie = npi->cookie;
3391 pin->buffer_id = ntohl(npi->buffer_id);
3392 pin->total_len = ntohs(npi->total_len);
3394 pin->packet = b.data;
3395 pin->packet_len = b.size;
3403 static struct ofpbuf *
3404 ofputil_encode_ofp10_packet_in(const struct ofputil_packet_in *pin)
3406 struct ofp10_packet_in *opi;
3407 struct ofpbuf *packet;
3409 packet = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
3410 htonl(0), pin->packet_len);
3411 opi = ofpbuf_put_zeros(packet, offsetof(struct ofp10_packet_in, data));
3412 opi->total_len = htons(pin->total_len);
3413 opi->in_port = htons(ofp_to_u16(pin->flow_metadata.flow.in_port.ofp_port));
3414 opi->reason = pin->reason;
3415 opi->buffer_id = htonl(pin->buffer_id);
3417 ofpbuf_put(packet, pin->packet, pin->packet_len);
3422 static struct ofpbuf *
3423 ofputil_encode_nx_packet_in(const struct ofputil_packet_in *pin)
3425 struct nx_packet_in *npi;
3426 struct ofpbuf *packet;
3429 /* The final argument is just an estimate of the space required. */
3430 packet = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, OFP10_VERSION,
3431 htonl(0), NXM_TYPICAL_LEN + 2 + pin->packet_len);
3432 ofpbuf_put_zeros(packet, sizeof *npi);
3433 match_len = nx_put_match(packet, &pin->flow_metadata, 0, 0);
3434 ofpbuf_put_zeros(packet, 2);
3435 ofpbuf_put(packet, pin->packet, pin->packet_len);
3438 npi->buffer_id = htonl(pin->buffer_id);
3439 npi->total_len = htons(pin->total_len);
3440 npi->reason = pin->reason;
3441 npi->table_id = pin->table_id;
3442 npi->cookie = pin->cookie;
3443 npi->match_len = htons(match_len);
3448 static struct ofpbuf *
3449 ofputil_encode_ofp11_packet_in(const struct ofputil_packet_in *pin)
3451 struct ofp11_packet_in *opi;
3452 struct ofpbuf *packet;
3454 packet = ofpraw_alloc_xid(OFPRAW_OFPT11_PACKET_IN, OFP11_VERSION,
3455 htonl(0), pin->packet_len);
3456 opi = ofpbuf_put_zeros(packet, sizeof *opi);
3457 opi->buffer_id = htonl(pin->buffer_id);
3458 opi->in_port = ofputil_port_to_ofp11(pin->flow_metadata.flow.in_port.ofp_port);
3459 opi->in_phy_port = opi->in_port;
3460 opi->total_len = htons(pin->total_len);
3461 opi->reason = pin->reason;
3462 opi->table_id = pin->table_id;
3464 ofpbuf_put(packet, pin->packet, pin->packet_len);
3469 static struct ofpbuf *
3470 ofputil_encode_ofp12_packet_in(const struct ofputil_packet_in *pin,
3471 enum ofputil_protocol protocol)
3473 struct ofp13_packet_in *opi;
3474 enum ofpraw packet_in_raw;
3475 enum ofp_version packet_in_version;
3476 size_t packet_in_size;
3477 struct ofpbuf *packet;
3479 if (protocol == OFPUTIL_P_OF12_OXM) {
3480 packet_in_raw = OFPRAW_OFPT12_PACKET_IN;
3481 packet_in_version = OFP12_VERSION;
3482 packet_in_size = sizeof (struct ofp12_packet_in);
3484 packet_in_raw = OFPRAW_OFPT13_PACKET_IN;
3485 packet_in_version = ofputil_protocol_to_ofp_version(protocol);
3486 packet_in_size = sizeof (struct ofp13_packet_in);
3489 /* The final argument is just an estimate of the space required. */
3490 packet = ofpraw_alloc_xid(packet_in_raw, packet_in_version,
3491 htonl(0), NXM_TYPICAL_LEN + 2 + pin->packet_len);
3492 ofpbuf_put_zeros(packet, packet_in_size);
3493 oxm_put_match(packet, &pin->flow_metadata,
3494 ofputil_protocol_to_ofp_version(protocol));
3495 ofpbuf_put_zeros(packet, 2);
3496 ofpbuf_put(packet, pin->packet, pin->packet_len);
3499 opi->pi.buffer_id = htonl(pin->buffer_id);
3500 opi->pi.total_len = htons(pin->total_len);
3501 opi->pi.reason = pin->reason;
3502 opi->pi.table_id = pin->table_id;
3503 if (protocol != OFPUTIL_P_OF12_OXM) {
3504 opi->cookie = pin->cookie;
3510 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
3511 * in the format specified by 'packet_in_format'. */
3513 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
3514 enum ofputil_protocol protocol,
3515 enum nx_packet_in_format packet_in_format)
3517 struct ofpbuf *packet;
3520 case OFPUTIL_P_OF10_STD:
3521 case OFPUTIL_P_OF10_STD_TID:
3522 case OFPUTIL_P_OF10_NXM:
3523 case OFPUTIL_P_OF10_NXM_TID:
3524 packet = (packet_in_format == NXPIF_NXM
3525 ? ofputil_encode_nx_packet_in(pin)
3526 : ofputil_encode_ofp10_packet_in(pin));
3529 case OFPUTIL_P_OF11_STD:
3530 packet = ofputil_encode_ofp11_packet_in(pin);
3533 case OFPUTIL_P_OF12_OXM:
3534 case OFPUTIL_P_OF13_OXM:
3535 case OFPUTIL_P_OF14_OXM:
3536 case OFPUTIL_P_OF15_OXM:
3537 packet = ofputil_encode_ofp12_packet_in(pin, protocol);
3544 ofpmsg_update_length(packet);
3548 /* Returns a string form of 'reason'. The return value is either a statically
3549 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
3550 * 'bufsize' should be at least OFPUTIL_PACKET_IN_REASON_BUFSIZE. */
3552 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason,
3553 char *reasonbuf, size_t bufsize)
3560 case OFPR_INVALID_TTL:
3561 return "invalid_ttl";
3562 case OFPR_ACTION_SET:
3563 return "action_set";
3566 case OFPR_PACKET_OUT:
3567 return "packet_out";
3569 case OFPR_N_REASONS:
3571 snprintf(reasonbuf, bufsize, "%d", (int) reason);
3577 ofputil_packet_in_reason_from_string(const char *s,
3578 enum ofp_packet_in_reason *reason)
3582 for (i = 0; i < OFPR_N_REASONS; i++) {
3583 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
3584 const char *reason_s;
3586 reason_s = ofputil_packet_in_reason_to_string(i, reasonbuf,
3588 if (!strcasecmp(s, reason_s)) {
3596 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
3599 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
3600 * message's actions. The caller must initialize 'ofpacts' and retains
3601 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
3603 * Returns 0 if successful, otherwise an OFPERR_* value. */
3605 ofputil_decode_packet_out(struct ofputil_packet_out *po,
3606 const struct ofp_header *oh,
3607 struct ofpbuf *ofpacts)
3612 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3613 raw = ofpraw_pull_assert(&b);
3615 if (raw == OFPRAW_OFPT11_PACKET_OUT) {
3617 const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
3619 po->buffer_id = ntohl(opo->buffer_id);
3620 error = ofputil_port_from_ofp11(opo->in_port, &po->in_port);
3625 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
3626 oh->version, ofpacts);
3630 } else if (raw == OFPRAW_OFPT10_PACKET_OUT) {
3632 const struct ofp10_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
3634 po->buffer_id = ntohl(opo->buffer_id);
3635 po->in_port = u16_to_ofp(ntohs(opo->in_port));
3637 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
3638 oh->version, ofpacts);
3646 if (ofp_to_u16(po->in_port) >= ofp_to_u16(OFPP_MAX)
3647 && po->in_port != OFPP_LOCAL
3648 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
3649 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
3651 return OFPERR_OFPBRC_BAD_PORT;
3654 po->ofpacts = ofpacts->data;
3655 po->ofpacts_len = ofpacts->size;
3657 if (po->buffer_id == UINT32_MAX) {
3658 po->packet = b.data;
3659 po->packet_len = b.size;
3668 /* ofputil_phy_port */
3670 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
3671 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
3672 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
3673 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
3674 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
3675 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
3676 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
3677 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
3679 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
3680 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
3681 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
3682 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
3683 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
3684 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
3686 static enum netdev_features
3687 netdev_port_features_from_ofp10(ovs_be32 ofp10_)
3689 uint32_t ofp10 = ntohl(ofp10_);
3690 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
3694 netdev_port_features_to_ofp10(enum netdev_features features)
3696 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
3699 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
3700 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
3701 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
3702 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
3703 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
3704 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
3705 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
3706 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
3707 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
3708 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
3709 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
3710 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
3711 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
3712 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
3713 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
3714 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
3716 static enum netdev_features
3717 netdev_port_features_from_ofp11(ovs_be32 ofp11)
3719 return ntohl(ofp11) & 0xffff;
3723 netdev_port_features_to_ofp11(enum netdev_features features)
3725 return htonl(features & 0xffff);
3729 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
3730 const struct ofp10_phy_port *opp)
3732 pp->port_no = u16_to_ofp(ntohs(opp->port_no));
3733 pp->hw_addr = opp->hw_addr;
3734 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
3736 pp->config = ntohl(opp->config) & OFPPC10_ALL;
3737 pp->state = ntohl(opp->state) & OFPPS10_ALL;
3739 pp->curr = netdev_port_features_from_ofp10(opp->curr);
3740 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
3741 pp->supported = netdev_port_features_from_ofp10(opp->supported);
3742 pp->peer = netdev_port_features_from_ofp10(opp->peer);
3744 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
3745 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
3751 ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
3752 const struct ofp11_port *op)
3756 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
3760 pp->hw_addr = op->hw_addr;
3761 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
3763 pp->config = ntohl(op->config) & OFPPC11_ALL;
3764 pp->state = ntohl(op->state) & OFPPS11_ALL;
3766 pp->curr = netdev_port_features_from_ofp11(op->curr);
3767 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
3768 pp->supported = netdev_port_features_from_ofp11(op->supported);
3769 pp->peer = netdev_port_features_from_ofp11(op->peer);
3771 pp->curr_speed = ntohl(op->curr_speed);
3772 pp->max_speed = ntohl(op->max_speed);
3778 parse_ofp14_port_ethernet_property(const struct ofpbuf *payload,
3779 struct ofputil_phy_port *pp)
3781 struct ofp14_port_desc_prop_ethernet *eth = payload->data;
3783 if (payload->size != sizeof *eth) {
3784 return OFPERR_OFPBPC_BAD_LEN;
3787 pp->curr = netdev_port_features_from_ofp11(eth->curr);
3788 pp->advertised = netdev_port_features_from_ofp11(eth->advertised);
3789 pp->supported = netdev_port_features_from_ofp11(eth->supported);
3790 pp->peer = netdev_port_features_from_ofp11(eth->peer);
3792 pp->curr_speed = ntohl(eth->curr_speed);
3793 pp->max_speed = ntohl(eth->max_speed);
3799 ofputil_pull_ofp14_port(struct ofputil_phy_port *pp, struct ofpbuf *msg)
3801 struct ofpbuf properties;
3802 struct ofp14_port *op;
3806 op = ofpbuf_try_pull(msg, sizeof *op);
3808 return OFPERR_OFPBRC_BAD_LEN;
3811 len = ntohs(op->length);
3812 if (len < sizeof *op || len - sizeof *op > msg->size) {
3813 return OFPERR_OFPBRC_BAD_LEN;
3816 ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len);
3818 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
3822 pp->hw_addr = op->hw_addr;
3823 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
3825 pp->config = ntohl(op->config) & OFPPC11_ALL;
3826 pp->state = ntohl(op->state) & OFPPS11_ALL;
3828 while (properties.size > 0) {
3829 struct ofpbuf payload;
3833 error = ofputil_pull_property(&properties, &payload, &type);
3839 case OFPPDPT14_ETHERNET:
3840 error = parse_ofp14_port_ethernet_property(&payload, pp);
3844 log_property(true, "unknown port property %"PRIu16, type);
3858 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
3859 struct ofp10_phy_port *opp)
3861 memset(opp, 0, sizeof *opp);
3863 opp->port_no = htons(ofp_to_u16(pp->port_no));
3864 opp->hw_addr = pp->hw_addr;
3865 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
3867 opp->config = htonl(pp->config & OFPPC10_ALL);
3868 opp->state = htonl(pp->state & OFPPS10_ALL);
3870 opp->curr = netdev_port_features_to_ofp10(pp->curr);
3871 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
3872 opp->supported = netdev_port_features_to_ofp10(pp->supported);
3873 opp->peer = netdev_port_features_to_ofp10(pp->peer);
3877 ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
3878 struct ofp11_port *op)
3880 memset(op, 0, sizeof *op);
3882 op->port_no = ofputil_port_to_ofp11(pp->port_no);
3883 op->hw_addr = pp->hw_addr;
3884 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
3886 op->config = htonl(pp->config & OFPPC11_ALL);
3887 op->state = htonl(pp->state & OFPPS11_ALL);
3889 op->curr = netdev_port_features_to_ofp11(pp->curr);
3890 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
3891 op->supported = netdev_port_features_to_ofp11(pp->supported);
3892 op->peer = netdev_port_features_to_ofp11(pp->peer);
3894 op->curr_speed = htonl(pp->curr_speed);
3895 op->max_speed = htonl(pp->max_speed);
3899 ofputil_put_ofp14_port(const struct ofputil_phy_port *pp,
3902 struct ofp14_port *op;
3903 struct ofp14_port_desc_prop_ethernet *eth;
3905 ofpbuf_prealloc_tailroom(b, sizeof *op + sizeof *eth);
3907 op = ofpbuf_put_zeros(b, sizeof *op);
3908 op->port_no = ofputil_port_to_ofp11(pp->port_no);
3909 op->length = htons(sizeof *op + sizeof *eth);
3910 op->hw_addr = pp->hw_addr;
3911 ovs_strlcpy(op->name, pp->name, sizeof op->name);
3912 op->config = htonl(pp->config & OFPPC11_ALL);
3913 op->state = htonl(pp->state & OFPPS11_ALL);
3915 eth = ofpbuf_put_zeros(b, sizeof *eth);
3916 eth->type = htons(OFPPDPT14_ETHERNET);
3917 eth->length = htons(sizeof *eth);
3918 eth->curr = netdev_port_features_to_ofp11(pp->curr);
3919 eth->advertised = netdev_port_features_to_ofp11(pp->advertised);
3920 eth->supported = netdev_port_features_to_ofp11(pp->supported);
3921 eth->peer = netdev_port_features_to_ofp11(pp->peer);
3922 eth->curr_speed = htonl(pp->curr_speed);
3923 eth->max_speed = htonl(pp->max_speed);
3927 ofputil_put_phy_port(enum ofp_version ofp_version,
3928 const struct ofputil_phy_port *pp, struct ofpbuf *b)
3930 switch (ofp_version) {
3931 case OFP10_VERSION: {
3932 struct ofp10_phy_port *opp = ofpbuf_put_uninit(b, sizeof *opp);
3933 ofputil_encode_ofp10_phy_port(pp, opp);
3939 case OFP13_VERSION: {
3940 struct ofp11_port *op = ofpbuf_put_uninit(b, sizeof *op);
3941 ofputil_encode_ofp11_port(pp, op);
3947 ofputil_put_ofp14_port(pp, b);
3956 ofputil_decode_port_desc_stats_request(const struct ofp_header *request,
3962 ofpbuf_use_const(&b, request, ntohs(request->length));
3963 raw = ofpraw_pull_assert(&b);
3964 if (raw == OFPRAW_OFPST10_PORT_DESC_REQUEST) {
3967 } else if (raw == OFPRAW_OFPST15_PORT_DESC_REQUEST) {
3968 ovs_be32 *ofp11_port;
3970 ofp11_port = ofpbuf_pull(&b, sizeof *ofp11_port);
3971 return ofputil_port_from_ofp11(*ofp11_port, port);
3978 ofputil_encode_port_desc_stats_request(enum ofp_version ofp_version,
3981 struct ofpbuf *request;
3983 switch (ofp_version) {
3989 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_DESC_REQUEST,
3992 case OFP15_VERSION:{
3993 struct ofp15_port_desc_request *req;
3994 request = ofpraw_alloc(OFPRAW_OFPST15_PORT_DESC_REQUEST,
3996 req = ofpbuf_put_zeros(request, sizeof *req);
3997 req->port_no = ofputil_port_to_ofp11(port);
4008 ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port *pp,
4009 struct ovs_list *replies)
4011 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
4012 size_t start_ofs = reply->size;
4014 ofputil_put_phy_port(ofpmp_version(replies), pp, reply);
4015 ofpmp_postappend(replies, start_ofs);
4018 /* ofputil_switch_features */
4020 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
4021 OFPC_IP_REASM | OFPC_QUEUE_STATS)
4022 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
4023 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
4024 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
4025 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
4026 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
4027 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
4030 ofputil_capabilities_mask(enum ofp_version ofp_version)
4032 /* Handle capabilities whose bit is unique for all OpenFlow versions */
4033 switch (ofp_version) {
4036 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
4041 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
4043 /* Caller needs to check osf->header.version itself */
4048 /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an
4049 * abstract representation in '*features'. Initializes '*b' to iterate over
4050 * the OpenFlow port structures following 'osf' with later calls to
4051 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an
4052 * OFPERR_* value. */
4054 ofputil_decode_switch_features(const struct ofp_header *oh,
4055 struct ofputil_switch_features *features,
4058 const struct ofp_switch_features *osf;
4061 ofpbuf_use_const(b, oh, ntohs(oh->length));
4062 raw = ofpraw_pull_assert(b);
4064 osf = ofpbuf_pull(b, sizeof *osf);
4065 features->datapath_id = ntohll(osf->datapath_id);
4066 features->n_buffers = ntohl(osf->n_buffers);
4067 features->n_tables = osf->n_tables;
4068 features->auxiliary_id = 0;
4070 features->capabilities = ntohl(osf->capabilities) &
4071 ofputil_capabilities_mask(oh->version);
4073 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
4074 if (osf->capabilities & htonl(OFPC10_STP)) {
4075 features->capabilities |= OFPUTIL_C_STP;
4077 features->ofpacts = ofpact_bitmap_from_openflow(osf->actions,
4079 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY
4080 || raw == OFPRAW_OFPT13_FEATURES_REPLY) {
4081 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
4082 features->capabilities |= OFPUTIL_C_GROUP_STATS;
4084 features->ofpacts = 0;
4085 if (raw == OFPRAW_OFPT13_FEATURES_REPLY) {
4086 features->auxiliary_id = osf->auxiliary_id;
4089 return OFPERR_OFPBRC_BAD_VERSION;
4095 /* In OpenFlow 1.0, 1.1, and 1.2, an OFPT_FEATURES_REPLY message lists all the
4096 * switch's ports, unless there are too many to fit. In OpenFlow 1.3 and
4097 * later, an OFPT_FEATURES_REPLY does not list ports at all.
4099 * Given a buffer 'b' that contains a Features Reply message, this message
4100 * checks if it contains a complete list of the switch's ports. Returns true,
4101 * if so. Returns false if the list is missing (OF1.3+) or incomplete
4102 * (OF1.0/1.1/1.2), and in the latter case removes all of the ports from the
4105 * When this function returns false, the caller should send an OFPST_PORT_DESC
4106 * stats request to get the ports. */
4108 ofputil_switch_features_has_ports(struct ofpbuf *b)
4110 struct ofp_header *oh = b->data;
4111 size_t phy_port_size;
4113 if (oh->version >= OFP13_VERSION) {
4114 /* OpenFlow 1.3+ never has ports in the feature reply. */
4118 phy_port_size = (oh->version == OFP10_VERSION
4119 ? sizeof(struct ofp10_phy_port)
4120 : sizeof(struct ofp11_port));
4121 if (ntohs(oh->length) + phy_port_size <= UINT16_MAX) {
4122 /* There's room for additional ports in the feature reply.
4123 * Assume that the list is complete. */
4127 /* The feature reply has no room for more ports. Probably the list is
4128 * truncated. Drop the ports and tell the caller to retrieve them with
4129 * OFPST_PORT_DESC. */
4130 b->size = sizeof *oh + sizeof(struct ofp_switch_features);
4131 ofpmsg_update_length(b);
4135 /* Returns a buffer owned by the caller that encodes 'features' in the format
4136 * required by 'protocol' with the given 'xid'. The caller should append port
4137 * information to the buffer with subsequent calls to
4138 * ofputil_put_switch_features_port(). */
4140 ofputil_encode_switch_features(const struct ofputil_switch_features *features,
4141 enum ofputil_protocol protocol, ovs_be32 xid)
4143 struct ofp_switch_features *osf;
4145 enum ofp_version version;
4148 version = ofputil_protocol_to_ofp_version(protocol);
4151 raw = OFPRAW_OFPT10_FEATURES_REPLY;
4155 raw = OFPRAW_OFPT11_FEATURES_REPLY;
4160 raw = OFPRAW_OFPT13_FEATURES_REPLY;
4165 b = ofpraw_alloc_xid(raw, version, xid, 0);
4166 osf = ofpbuf_put_zeros(b, sizeof *osf);
4167 osf->datapath_id = htonll(features->datapath_id);
4168 osf->n_buffers = htonl(features->n_buffers);
4169 osf->n_tables = features->n_tables;
4171 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
4172 osf->capabilities = htonl(features->capabilities &
4173 ofputil_capabilities_mask(version));
4176 if (features->capabilities & OFPUTIL_C_STP) {
4177 osf->capabilities |= htonl(OFPC10_STP);
4179 osf->actions = ofpact_bitmap_to_openflow(features->ofpacts,
4185 osf->auxiliary_id = features->auxiliary_id;
4189 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
4190 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
4200 /* Encodes 'pp' into the format required by the switch_features message already
4201 * in 'b', which should have been returned by ofputil_encode_switch_features(),
4202 * and appends the encoded version to 'b'. */
4204 ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
4207 const struct ofp_header *oh = b->data;
4209 if (oh->version < OFP13_VERSION) {
4210 /* Try adding a port description to the message, but drop it again if
4211 * the buffer overflows. (This possibility for overflow is why
4212 * OpenFlow 1.3+ moved port descriptions into a multipart message.) */
4213 size_t start_ofs = b->size;
4214 ofputil_put_phy_port(oh->version, pp, b);
4215 if (b->size > UINT16_MAX) {
4216 b->size = start_ofs;
4221 /* ofputil_port_status */
4223 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
4224 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
4226 ofputil_decode_port_status(const struct ofp_header *oh,
4227 struct ofputil_port_status *ps)
4229 const struct ofp_port_status *ops;
4233 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4234 ofpraw_pull_assert(&b);
4235 ops = ofpbuf_pull(&b, sizeof *ops);
4237 if (ops->reason != OFPPR_ADD &&
4238 ops->reason != OFPPR_DELETE &&
4239 ops->reason != OFPPR_MODIFY) {
4240 return OFPERR_NXBRC_BAD_REASON;
4242 ps->reason = ops->reason;
4244 retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
4245 ovs_assert(retval != EOF);
4249 /* Converts the abstract form of a "port status" message in '*ps' into an
4250 * OpenFlow message suitable for 'protocol', and returns that encoded form in
4251 * a buffer owned by the caller. */
4253 ofputil_encode_port_status(const struct ofputil_port_status *ps,
4254 enum ofputil_protocol protocol)
4256 struct ofp_port_status *ops;
4258 enum ofp_version version;
4261 version = ofputil_protocol_to_ofp_version(protocol);
4264 raw = OFPRAW_OFPT10_PORT_STATUS;
4270 raw = OFPRAW_OFPT11_PORT_STATUS;
4275 raw = OFPRAW_OFPT14_PORT_STATUS;
4282 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
4283 ops = ofpbuf_put_zeros(b, sizeof *ops);
4284 ops->reason = ps->reason;
4285 ofputil_put_phy_port(version, &ps->desc, b);
4286 ofpmsg_update_length(b);
4290 /* ofputil_port_mod */
4293 parse_port_mod_ethernet_property(struct ofpbuf *property,
4294 struct ofputil_port_mod *pm)
4296 struct ofp14_port_mod_prop_ethernet *eth = property->data;
4298 if (property->size != sizeof *eth) {
4299 return OFPERR_OFPBRC_BAD_LEN;
4302 pm->advertise = netdev_port_features_from_ofp11(eth->advertise);
4306 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
4307 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
4309 ofputil_decode_port_mod(const struct ofp_header *oh,
4310 struct ofputil_port_mod *pm, bool loose)
4315 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4316 raw = ofpraw_pull_assert(&b);
4318 if (raw == OFPRAW_OFPT10_PORT_MOD) {
4319 const struct ofp10_port_mod *opm = b.data;
4321 pm->port_no = u16_to_ofp(ntohs(opm->port_no));
4322 pm->hw_addr = opm->hw_addr;
4323 pm->config = ntohl(opm->config) & OFPPC10_ALL;
4324 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
4325 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
4326 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
4327 const struct ofp11_port_mod *opm = b.data;
4330 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
4335 pm->hw_addr = opm->hw_addr;
4336 pm->config = ntohl(opm->config) & OFPPC11_ALL;
4337 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
4338 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
4339 } else if (raw == OFPRAW_OFPT14_PORT_MOD) {
4340 const struct ofp14_port_mod *opm = ofpbuf_pull(&b, sizeof *opm);
4343 memset(pm, 0, sizeof *pm);
4345 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
4350 pm->hw_addr = opm->hw_addr;
4351 pm->config = ntohl(opm->config) & OFPPC11_ALL;
4352 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
4354 while (b.size > 0) {
4355 struct ofpbuf property;
4359 error = ofputil_pull_property(&b, &property, &type);
4365 case OFPPMPT14_ETHERNET:
4366 error = parse_port_mod_ethernet_property(&property, pm);
4370 log_property(loose, "unknown port_mod property %"PRIu16, type);
4373 } else if (type == OFPPMPT14_EXPERIMENTER) {
4374 error = OFPERR_OFPBPC_BAD_EXPERIMENTER;
4376 error = OFPERR_OFPBRC_BAD_TYPE;
4386 return OFPERR_OFPBRC_BAD_TYPE;
4389 pm->config &= pm->mask;
4393 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
4394 * message suitable for 'protocol', and returns that encoded form in a buffer
4395 * owned by the caller. */
4397 ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
4398 enum ofputil_protocol protocol)
4400 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
4403 switch (ofp_version) {
4404 case OFP10_VERSION: {
4405 struct ofp10_port_mod *opm;
4407 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
4408 opm = ofpbuf_put_zeros(b, sizeof *opm);
4409 opm->port_no = htons(ofp_to_u16(pm->port_no));
4410 opm->hw_addr = pm->hw_addr;
4411 opm->config = htonl(pm->config & OFPPC10_ALL);
4412 opm->mask = htonl(pm->mask & OFPPC10_ALL);
4413 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
4419 case OFP13_VERSION: {
4420 struct ofp11_port_mod *opm;
4422 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
4423 opm = ofpbuf_put_zeros(b, sizeof *opm);
4424 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
4425 opm->hw_addr = pm->hw_addr;
4426 opm->config = htonl(pm->config & OFPPC11_ALL);
4427 opm->mask = htonl(pm->mask & OFPPC11_ALL);
4428 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
4432 case OFP15_VERSION: {
4433 struct ofp14_port_mod_prop_ethernet *eth;
4434 struct ofp14_port_mod *opm;
4436 b = ofpraw_alloc(OFPRAW_OFPT14_PORT_MOD, ofp_version, sizeof *eth);
4437 opm = ofpbuf_put_zeros(b, sizeof *opm);
4438 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
4439 opm->hw_addr = pm->hw_addr;
4440 opm->config = htonl(pm->config & OFPPC11_ALL);
4441 opm->mask = htonl(pm->mask & OFPPC11_ALL);
4443 if (pm->advertise) {
4444 eth = ofpbuf_put_zeros(b, sizeof *eth);
4445 eth->type = htons(OFPPMPT14_ETHERNET);
4446 eth->length = htons(sizeof *eth);
4447 eth->advertise = netdev_port_features_to_ofp11(pm->advertise);
4458 /* Table features. */
4461 pull_table_feature_property(struct ofpbuf *msg, struct ofpbuf *payload,
4466 error = ofputil_pull_property(msg, payload, typep);
4467 if (payload && !error) {
4468 ofpbuf_pull(payload, sizeof(struct ofp_prop_header));
4474 parse_action_bitmap(struct ofpbuf *payload, enum ofp_version ofp_version,
4479 while (payload->size > 0) {
4483 error = ofputil_pull_property__(payload, NULL, 1, &type);
4487 if (type < CHAR_BIT * sizeof types) {
4488 types |= 1u << type;
4492 *ofpacts = ofpact_bitmap_from_openflow(htonl(types), ofp_version);
4497 parse_instruction_ids(struct ofpbuf *payload, bool loose, uint32_t *insts)
4500 while (payload->size > 0) {
4501 enum ovs_instruction_type inst;
4505 /* OF1.3 and OF1.4 aren't clear about padding in the instruction IDs.
4506 * It seems clear that they aren't padded to 8 bytes, though, because
4507 * both standards say that "non-experimenter instructions are 4 bytes"
4508 * and do not mention any padding before the first instruction ID.
4509 * (There wouldn't be any point in padding to 8 bytes if the IDs were
4510 * aligned on an odd 4-byte boundary.)
4512 * Anyway, we just assume they're all glommed together on byte
4514 error = ofputil_pull_property__(payload, NULL, 1, &ofpit);
4519 error = ovs_instruction_type_from_inst_type(&inst, ofpit);
4521 *insts |= 1u << inst;
4522 } else if (!loose) {
4530 parse_table_features_next_table(struct ofpbuf *payload,
4531 unsigned long int *next_tables)
4535 memset(next_tables, 0, bitmap_n_bytes(255));
4536 for (i = 0; i < payload->size; i++) {
4537 uint8_t id = ((const uint8_t *) payload->data)[i];
4539 return OFPERR_OFPBPC_BAD_VALUE;
4541 bitmap_set1(next_tables, id);
4547 parse_oxms(struct ofpbuf *payload, bool loose,
4548 struct mf_bitmap *exactp, struct mf_bitmap *maskedp)
4550 struct mf_bitmap exact = MF_BITMAP_INITIALIZER;
4551 struct mf_bitmap masked = MF_BITMAP_INITIALIZER;
4553 while (payload->size > 0) {
4554 const struct mf_field *field;
4558 error = nx_pull_header(payload, &field, &hasmask);
4560 bitmap_set1(hasmask ? masked.bm : exact.bm, field->id);
4561 } else if (error != OFPERR_OFPBMC_BAD_FIELD || !loose) {
4567 } else if (!bitmap_is_all_zeros(exact.bm, MFF_N_IDS)) {
4568 return OFPERR_OFPBMC_BAD_MASK;
4572 } else if (!bitmap_is_all_zeros(masked.bm, MFF_N_IDS)) {
4573 return OFPERR_OFPBMC_BAD_MASK;
4578 /* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract
4579 * ofputil_table_features in 'tf'.
4581 * If 'loose' is true, this function ignores properties and values that it does
4582 * not understand, as a controller would want to do when interpreting
4583 * capabilities provided by a switch. If 'loose' is false, this function
4584 * treats unknown properties and values as an error, as a switch would want to
4585 * do when interpreting a configuration request made by a controller.
4587 * A single OpenFlow message can specify features for multiple tables. Calling
4588 * this function multiple times for a single 'msg' iterates through the tables
4589 * in the message. The caller must initially leave 'msg''s layer pointers null
4590 * and not modify them between calls.
4592 * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise
4593 * a positive "enum ofperr" value. */
4595 ofputil_decode_table_features(struct ofpbuf *msg,
4596 struct ofputil_table_features *tf, bool loose)
4598 const struct ofp_header *oh;
4599 struct ofp13_table_features *otf;
4600 struct ofpbuf properties;
4603 memset(tf, 0, sizeof *tf);
4606 ofpraw_pull_assert(msg);
4614 if (msg->size < sizeof *otf) {
4615 return OFPERR_OFPBPC_BAD_LEN;
4619 len = ntohs(otf->length);
4620 if (len < sizeof *otf || len % 8 || len > msg->size) {
4621 return OFPERR_OFPBPC_BAD_LEN;
4623 ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len);
4624 ofpbuf_pull(&properties, sizeof *otf);
4626 tf->table_id = otf->table_id;
4627 if (tf->table_id == OFPTT_ALL) {
4628 return OFPERR_OFPTFFC_BAD_TABLE;
4631 ovs_strlcpy(tf->name, otf->name, OFP_MAX_TABLE_NAME_LEN);
4632 tf->metadata_match = otf->metadata_match;
4633 tf->metadata_write = otf->metadata_write;
4634 tf->miss_config = OFPUTIL_TABLE_MISS_DEFAULT;
4635 if (oh->version >= OFP14_VERSION) {
4636 uint32_t caps = ntohl(otf->capabilities);
4637 tf->supports_eviction = (caps & OFPTC14_EVICTION) != 0;
4638 tf->supports_vacancy_events = (caps & OFPTC14_VACANCY_EVENTS) != 0;
4640 tf->supports_eviction = -1;
4641 tf->supports_vacancy_events = -1;
4643 tf->max_entries = ntohl(otf->max_entries);
4645 while (properties.size > 0) {
4646 struct ofpbuf payload;
4650 error = pull_table_feature_property(&properties, &payload, &type);
4655 switch ((enum ofp13_table_feature_prop_type) type) {
4656 case OFPTFPT13_INSTRUCTIONS:
4657 error = parse_instruction_ids(&payload, loose,
4658 &tf->nonmiss.instructions);
4660 case OFPTFPT13_INSTRUCTIONS_MISS:
4661 error = parse_instruction_ids(&payload, loose,
4662 &tf->miss.instructions);
4665 case OFPTFPT13_NEXT_TABLES:
4666 error = parse_table_features_next_table(&payload,
4669 case OFPTFPT13_NEXT_TABLES_MISS:
4670 error = parse_table_features_next_table(&payload, tf->miss.next);
4673 case OFPTFPT13_WRITE_ACTIONS:
4674 error = parse_action_bitmap(&payload, oh->version,
4675 &tf->nonmiss.write.ofpacts);
4677 case OFPTFPT13_WRITE_ACTIONS_MISS:
4678 error = parse_action_bitmap(&payload, oh->version,
4679 &tf->miss.write.ofpacts);
4682 case OFPTFPT13_APPLY_ACTIONS:
4683 error = parse_action_bitmap(&payload, oh->version,
4684 &tf->nonmiss.apply.ofpacts);
4686 case OFPTFPT13_APPLY_ACTIONS_MISS:
4687 error = parse_action_bitmap(&payload, oh->version,
4688 &tf->miss.apply.ofpacts);
4691 case OFPTFPT13_MATCH:
4692 error = parse_oxms(&payload, loose, &tf->match, &tf->mask);
4694 case OFPTFPT13_WILDCARDS:
4695 error = parse_oxms(&payload, loose, &tf->wildcard, NULL);
4698 case OFPTFPT13_WRITE_SETFIELD:
4699 error = parse_oxms(&payload, loose,
4700 &tf->nonmiss.write.set_fields, NULL);
4702 case OFPTFPT13_WRITE_SETFIELD_MISS:
4703 error = parse_oxms(&payload, loose,
4704 &tf->miss.write.set_fields, NULL);
4706 case OFPTFPT13_APPLY_SETFIELD:
4707 error = parse_oxms(&payload, loose,
4708 &tf->nonmiss.apply.set_fields, NULL);
4710 case OFPTFPT13_APPLY_SETFIELD_MISS:
4711 error = parse_oxms(&payload, loose,
4712 &tf->miss.apply.set_fields, NULL);
4715 case OFPTFPT13_EXPERIMENTER:
4716 case OFPTFPT13_EXPERIMENTER_MISS:
4718 log_property(loose, "unknown table features property %"PRIu16,
4720 error = loose ? 0 : OFPERR_OFPBPC_BAD_TYPE;
4728 /* Fix inconsistencies:
4730 * - Turn on 'match' bits that are set in 'mask', because maskable
4731 * fields are matchable.
4733 * - Turn on 'wildcard' bits that are set in 'mask', because a field
4734 * that is arbitrarily maskable can be wildcarded entirely.
4736 * - Turn off 'wildcard' bits that are not in 'match', because a field
4737 * must be matchable for it to be meaningfully wildcarded. */
4738 bitmap_or(tf->match.bm, tf->mask.bm, MFF_N_IDS);
4739 bitmap_or(tf->wildcard.bm, tf->mask.bm, MFF_N_IDS);
4740 bitmap_and(tf->wildcard.bm, tf->match.bm, MFF_N_IDS);
4745 /* Encodes and returns a request to obtain the table features of a switch.
4746 * The message is encoded for OpenFlow version 'ofp_version'. */
4748 ofputil_encode_table_features_request(enum ofp_version ofp_version)
4750 struct ofpbuf *request = NULL;
4752 switch (ofp_version) {
4756 ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later "
4757 "(\'-O OpenFlow13\')");
4761 request = ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST,
4772 put_fields_property(struct ofpbuf *reply,
4773 const struct mf_bitmap *fields,
4774 const struct mf_bitmap *masks,
4775 enum ofp13_table_feature_prop_type property,
4776 enum ofp_version version)
4781 start_ofs = start_property(reply, property);
4782 BITMAP_FOR_EACH_1 (field, MFF_N_IDS, fields->bm) {
4783 nx_put_header(reply, field, version,
4784 masks && bitmap_is_set(masks->bm, field));
4786 end_property(reply, start_ofs);
4790 put_table_action_features(struct ofpbuf *reply,
4791 const struct ofputil_table_action_features *taf,
4792 enum ofp13_table_feature_prop_type actions_type,
4793 enum ofp13_table_feature_prop_type set_fields_type,
4794 int miss_offset, enum ofp_version version)
4798 start_ofs = start_property(reply, actions_type + miss_offset);
4799 put_bitmap_properties(reply,
4800 ntohl(ofpact_bitmap_to_openflow(taf->ofpacts,
4802 end_property(reply, start_ofs);
4804 put_fields_property(reply, &taf->set_fields, NULL,
4805 set_fields_type + miss_offset, version);
4809 put_table_instruction_features(
4810 struct ofpbuf *reply, const struct ofputil_table_instruction_features *tif,
4811 int miss_offset, enum ofp_version version)
4816 start_ofs = start_property(reply, OFPTFPT13_INSTRUCTIONS + miss_offset);
4817 put_bitmap_properties(reply,
4818 ntohl(ovsinst_bitmap_to_openflow(tif->instructions,
4820 end_property(reply, start_ofs);
4822 start_ofs = start_property(reply, OFPTFPT13_NEXT_TABLES + miss_offset);
4823 BITMAP_FOR_EACH_1 (table_id, 255, tif->next) {
4824 ofpbuf_put(reply, &table_id, 1);
4826 end_property(reply, start_ofs);
4828 put_table_action_features(reply, &tif->write,
4829 OFPTFPT13_WRITE_ACTIONS,
4830 OFPTFPT13_WRITE_SETFIELD, miss_offset, version);
4831 put_table_action_features(reply, &tif->apply,
4832 OFPTFPT13_APPLY_ACTIONS,
4833 OFPTFPT13_APPLY_SETFIELD, miss_offset, version);
4837 ofputil_append_table_features_reply(const struct ofputil_table_features *tf,
4838 struct ovs_list *replies)
4840 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
4841 enum ofp_version version = ofpmp_version(replies);
4842 size_t start_ofs = reply->size;
4843 struct ofp13_table_features *otf;
4845 otf = ofpbuf_put_zeros(reply, sizeof *otf);
4846 otf->table_id = tf->table_id;
4847 ovs_strlcpy(otf->name, tf->name, sizeof otf->name);
4848 otf->metadata_match = tf->metadata_match;
4849 otf->metadata_write = tf->metadata_write;
4850 if (version >= OFP14_VERSION) {
4851 if (tf->supports_eviction) {
4852 otf->capabilities |= htonl(OFPTC14_EVICTION);
4854 if (tf->supports_vacancy_events) {
4855 otf->capabilities |= htonl(OFPTC14_VACANCY_EVENTS);
4858 otf->max_entries = htonl(tf->max_entries);
4860 put_table_instruction_features(reply, &tf->nonmiss, 0, version);
4861 put_table_instruction_features(reply, &tf->miss, 1, version);
4863 put_fields_property(reply, &tf->match, &tf->mask,
4864 OFPTFPT13_MATCH, version);
4865 put_fields_property(reply, &tf->wildcard, NULL,
4866 OFPTFPT13_WILDCARDS, version);
4868 otf = ofpbuf_at_assert(reply, start_ofs, sizeof *otf);
4869 otf->length = htons(reply->size - start_ofs);
4870 ofpmp_postappend(replies, start_ofs);
4874 parse_table_desc_eviction_property(struct ofpbuf *property,
4875 struct ofputil_table_desc *td)
4877 struct ofp14_table_mod_prop_eviction *ote = property->data;
4879 if (property->size != sizeof *ote) {
4880 return OFPERR_OFPBPC_BAD_LEN;
4883 td->eviction_flags = ntohl(ote->flags);
4887 /* Decodes the next OpenFlow "table desc" message (of possibly several) from
4888 * 'msg' into an abstract form in '*td'. Returns 0 if successful, EOF if the
4889 * last "table desc" in 'msg' was already decoded, otherwise an OFPERR_*
4892 ofputil_decode_table_desc(struct ofpbuf *msg,
4893 struct ofputil_table_desc *td,
4894 enum ofp_version version)
4896 struct ofp14_table_desc *otd;
4897 struct ofpbuf properties;
4900 memset(td, 0, sizeof *td);
4903 ofpraw_pull_assert(msg);
4910 otd = ofpbuf_try_pull(msg, sizeof *otd);
4912 VLOG_WARN_RL(&bad_ofmsg_rl, "OFP14_TABLE_DESC reply has %"PRIu32" "
4913 "leftover bytes at end", msg->size);
4914 return OFPERR_OFPBRC_BAD_LEN;
4917 td->table_id = otd->table_id;
4918 length = ntohs(otd->length);
4919 if (length < sizeof *otd || length - sizeof *otd > msg->size) {
4920 VLOG_WARN_RL(&bad_ofmsg_rl, "OFP14_TABLE_DESC reply claims invalid "
4921 "length %"PRIuSIZE, length);
4922 return OFPERR_OFPBRC_BAD_LEN;
4924 length -= sizeof *otd;
4925 ofpbuf_use_const(&properties, ofpbuf_pull(msg, length), length);
4927 td->eviction = ofputil_decode_table_eviction(otd->config, version);
4928 td->eviction_flags = UINT32_MAX;
4930 while (properties.size > 0) {
4931 struct ofpbuf payload;
4935 error = ofputil_pull_property(&properties, &payload, &type);
4941 case OFPTMPT14_EVICTION:
4942 error = parse_table_desc_eviction_property(&payload, td);
4946 log_property(true, "unknown table_desc property %"PRIu16, type);
4959 /* Encodes and returns a request to obtain description of tables of a switch.
4960 * The message is encoded for OpenFlow version 'ofp_version'. */
4962 ofputil_encode_table_desc_request(enum ofp_version ofp_version)
4964 struct ofpbuf *request = NULL;
4966 if (ofp_version >= OFP14_VERSION) {
4967 request = ofpraw_alloc(OFPRAW_OFPST14_TABLE_DESC_REQUEST,
4970 ovs_fatal(0, "dump-table-desc needs OpenFlow 1.4 or later "
4971 "(\'-O OpenFlow14\')");
4977 /* Function to append Table desc information in a reply list. */
4979 ofputil_append_table_desc_reply(const struct ofputil_table_desc *td,
4980 struct ovs_list *replies,
4981 enum ofp_version version)
4983 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
4985 struct ofp14_table_desc *otd;
4987 start_otd = reply->size;
4988 ofpbuf_put_zeros(reply, sizeof *otd);
4989 if (td->eviction_flags != UINT32_MAX) {
4990 struct ofp14_table_mod_prop_eviction *ote;
4992 ote = ofpbuf_put_zeros(reply, sizeof *ote);
4993 ote->type = htons(OFPTMPT14_EVICTION);
4994 ote->length = htons(sizeof *ote);
4995 ote->flags = htonl(td->eviction_flags);
4998 otd = ofpbuf_at_assert(reply, start_otd, sizeof *otd);
4999 otd->length = htons(reply->size - start_otd);
5000 otd->table_id = td->table_id;
5001 otd->config = ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT,
5002 td->eviction, version);
5003 ofpmp_postappend(replies, start_otd);
5007 parse_table_mod_eviction_property(struct ofpbuf *property,
5008 struct ofputil_table_mod *tm)
5010 struct ofp14_table_mod_prop_eviction *ote = property->data;
5012 if (property->size != sizeof *ote) {
5013 return OFPERR_OFPBPC_BAD_LEN;
5016 tm->eviction_flags = ntohl(ote->flags);
5020 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5021 * table configuration (a table mod, table stats, or table features message),
5022 * returns the table eviction configuration that it specifies.
5024 * Only OpenFlow 1.4 and later specify table eviction configuration this way,
5025 * so for other 'version' values this function always returns
5026 * OFPUTIL_TABLE_EVICTION_DEFAULT. */
5027 static enum ofputil_table_eviction
5028 ofputil_decode_table_eviction(ovs_be32 config, enum ofp_version version)
5030 return (version < OFP14_VERSION ? OFPUTIL_TABLE_EVICTION_DEFAULT
5031 : config & htonl(OFPTC14_EVICTION) ? OFPUTIL_TABLE_EVICTION_ON
5032 : OFPUTIL_TABLE_EVICTION_OFF);
5035 /* Returns a bitmap of OFPTC* values suitable for 'config' fields in various
5036 * OpenFlow messages of the given 'version', based on the provided 'miss' and
5037 * 'eviction' values. */
5039 ofputil_encode_table_config(enum ofputil_table_miss miss,
5040 enum ofputil_table_eviction eviction,
5041 enum ofp_version version)
5043 /* See the section "OFPTC_* Table Configuration" in DESIGN.md for more
5044 * information on the crazy evolution of this field. */
5047 /* OpenFlow 1.0 didn't have such a field, any value ought to do. */
5052 /* OpenFlow 1.1 and 1.2 define only OFPTC11_TABLE_MISS_*. */
5054 case OFPUTIL_TABLE_MISS_DEFAULT:
5055 /* Really this shouldn't be used for encoding (the caller should
5056 * provide a specific value) but I can't imagine that defaulting to
5057 * the fall-through case here will hurt. */
5058 case OFPUTIL_TABLE_MISS_CONTROLLER:
5060 return htonl(OFPTC11_TABLE_MISS_CONTROLLER);
5061 case OFPUTIL_TABLE_MISS_CONTINUE:
5062 return htonl(OFPTC11_TABLE_MISS_CONTINUE);
5063 case OFPUTIL_TABLE_MISS_DROP:
5064 return htonl(OFPTC11_TABLE_MISS_DROP);
5069 /* OpenFlow 1.3 removed OFPTC11_TABLE_MISS_* and didn't define any new
5070 * flags, so this is correct. */
5075 /* OpenFlow 1.4 introduced OFPTC14_EVICTION and OFPTC14_VACANCY_EVENTS
5076 * and we don't support the latter yet. */
5077 return htonl(eviction == OFPUTIL_TABLE_EVICTION_ON
5078 ? OFPTC14_EVICTION : 0);
5084 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5085 * table configuration (a table mod, table stats, or table features message),
5086 * returns the table miss configuration that it specifies.
5088 * Only OpenFlow 1.1 and 1.2 specify table miss configurations this way, so for
5089 * other 'version' values this function always returns
5090 * OFPUTIL_TABLE_MISS_DEFAULT. */
5091 static enum ofputil_table_miss
5092 ofputil_decode_table_miss(ovs_be32 config_, enum ofp_version version)
5094 uint32_t config = ntohl(config_);
5096 if (version == OFP11_VERSION || version == OFP12_VERSION) {
5097 switch (config & OFPTC11_TABLE_MISS_MASK) {
5098 case OFPTC11_TABLE_MISS_CONTROLLER:
5099 return OFPUTIL_TABLE_MISS_CONTROLLER;
5101 case OFPTC11_TABLE_MISS_CONTINUE:
5102 return OFPUTIL_TABLE_MISS_CONTINUE;
5104 case OFPTC11_TABLE_MISS_DROP:
5105 return OFPUTIL_TABLE_MISS_DROP;
5108 VLOG_WARN_RL(&bad_ofmsg_rl, "bad table miss config %d", config);
5109 return OFPUTIL_TABLE_MISS_CONTROLLER;
5112 return OFPUTIL_TABLE_MISS_DEFAULT;
5116 /* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in
5117 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
5119 ofputil_decode_table_mod(const struct ofp_header *oh,
5120 struct ofputil_table_mod *pm)
5125 memset(pm, 0, sizeof *pm);
5126 pm->miss = OFPUTIL_TABLE_MISS_DEFAULT;
5127 pm->eviction = OFPUTIL_TABLE_EVICTION_DEFAULT;
5128 pm->eviction_flags = UINT32_MAX;
5129 ofpbuf_use_const(&b, oh, ntohs(oh->length));
5130 raw = ofpraw_pull_assert(&b);
5132 if (raw == OFPRAW_OFPT11_TABLE_MOD) {
5133 const struct ofp11_table_mod *otm = b.data;
5135 pm->table_id = otm->table_id;
5136 pm->miss = ofputil_decode_table_miss(otm->config, oh->version);
5137 } else if (raw == OFPRAW_OFPT14_TABLE_MOD) {
5138 const struct ofp14_table_mod *otm = ofpbuf_pull(&b, sizeof *otm);
5140 pm->table_id = otm->table_id;
5141 pm->miss = ofputil_decode_table_miss(otm->config, oh->version);
5142 pm->eviction = ofputil_decode_table_eviction(otm->config, oh->version);
5143 while (b.size > 0) {
5144 struct ofpbuf property;
5148 error = ofputil_pull_property(&b, &property, &type);
5154 case OFPTMPT14_EVICTION:
5155 error = parse_table_mod_eviction_property(&property, pm);
5159 error = OFPERR_OFPBRC_BAD_TYPE;
5168 return OFPERR_OFPBRC_BAD_TYPE;
5174 /* Converts the abstract form of a "table mod" message in '*tm' into an
5175 * OpenFlow message suitable for 'protocol', and returns that encoded form in a
5176 * buffer owned by the caller. */
5178 ofputil_encode_table_mod(const struct ofputil_table_mod *tm,
5179 enum ofputil_protocol protocol)
5181 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
5184 switch (ofp_version) {
5185 case OFP10_VERSION: {
5186 ovs_fatal(0, "table mod needs OpenFlow 1.1 or later "
5187 "(\'-O OpenFlow11\')");
5192 case OFP13_VERSION: {
5193 struct ofp11_table_mod *otm;
5195 b = ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD, ofp_version, 0);
5196 otm = ofpbuf_put_zeros(b, sizeof *otm);
5197 otm->table_id = tm->table_id;
5198 otm->config = ofputil_encode_table_config(tm->miss, tm->eviction,
5203 case OFP15_VERSION: {
5204 struct ofp14_table_mod *otm;
5205 struct ofp14_table_mod_prop_eviction *ote;
5207 b = ofpraw_alloc(OFPRAW_OFPT14_TABLE_MOD, ofp_version, 0);
5208 otm = ofpbuf_put_zeros(b, sizeof *otm);
5209 otm->table_id = tm->table_id;
5210 otm->config = ofputil_encode_table_config(tm->miss, tm->eviction,
5213 if (tm->eviction_flags != UINT32_MAX) {
5214 ote = ofpbuf_put_zeros(b, sizeof *ote);
5215 ote->type = htons(OFPTMPT14_EVICTION);
5216 ote->length = htons(sizeof *ote);
5217 ote->flags = htonl(tm->eviction_flags);
5228 /* ofputil_role_request */
5230 /* Decodes the OpenFlow "role request" or "role reply" message in '*oh' into
5231 * an abstract form in '*rr'. Returns 0 if successful, otherwise an
5232 * OFPERR_* value. */
5234 ofputil_decode_role_message(const struct ofp_header *oh,
5235 struct ofputil_role_request *rr)
5240 ofpbuf_use_const(&b, oh, ntohs(oh->length));
5241 raw = ofpraw_pull_assert(&b);
5243 if (raw == OFPRAW_OFPT12_ROLE_REQUEST ||
5244 raw == OFPRAW_OFPT12_ROLE_REPLY) {
5245 const struct ofp12_role_request *orr = b.msg;
5247 if (orr->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
5248 orr->role != htonl(OFPCR12_ROLE_EQUAL) &&
5249 orr->role != htonl(OFPCR12_ROLE_MASTER) &&
5250 orr->role != htonl(OFPCR12_ROLE_SLAVE)) {
5251 return OFPERR_OFPRRFC_BAD_ROLE;
5254 rr->role = ntohl(orr->role);
5255 if (raw == OFPRAW_OFPT12_ROLE_REQUEST
5256 ? orr->role == htonl(OFPCR12_ROLE_NOCHANGE)
5257 : orr->generation_id == OVS_BE64_MAX) {
5258 rr->have_generation_id = false;
5259 rr->generation_id = 0;
5261 rr->have_generation_id = true;
5262 rr->generation_id = ntohll(orr->generation_id);
5264 } else if (raw == OFPRAW_NXT_ROLE_REQUEST ||
5265 raw == OFPRAW_NXT_ROLE_REPLY) {
5266 const struct nx_role_request *nrr = b.msg;
5268 BUILD_ASSERT(NX_ROLE_OTHER + 1 == OFPCR12_ROLE_EQUAL);
5269 BUILD_ASSERT(NX_ROLE_MASTER + 1 == OFPCR12_ROLE_MASTER);
5270 BUILD_ASSERT(NX_ROLE_SLAVE + 1 == OFPCR12_ROLE_SLAVE);
5272 if (nrr->role != htonl(NX_ROLE_OTHER) &&
5273 nrr->role != htonl(NX_ROLE_MASTER) &&
5274 nrr->role != htonl(NX_ROLE_SLAVE)) {
5275 return OFPERR_OFPRRFC_BAD_ROLE;
5278 rr->role = ntohl(nrr->role) + 1;
5279 rr->have_generation_id = false;
5280 rr->generation_id = 0;
5288 /* Returns an encoded form of a role reply suitable for the "request" in a
5289 * buffer owned by the caller. */
5291 ofputil_encode_role_reply(const struct ofp_header *request,
5292 const struct ofputil_role_request *rr)
5297 raw = ofpraw_decode_assert(request);
5298 if (raw == OFPRAW_OFPT12_ROLE_REQUEST) {
5299 struct ofp12_role_request *orr;
5301 buf = ofpraw_alloc_reply(OFPRAW_OFPT12_ROLE_REPLY, request, 0);
5302 orr = ofpbuf_put_zeros(buf, sizeof *orr);
5304 orr->role = htonl(rr->role);
5305 orr->generation_id = htonll(rr->have_generation_id
5308 } else if (raw == OFPRAW_NXT_ROLE_REQUEST) {
5309 struct nx_role_request *nrr;
5311 BUILD_ASSERT(NX_ROLE_OTHER == OFPCR12_ROLE_EQUAL - 1);
5312 BUILD_ASSERT(NX_ROLE_MASTER == OFPCR12_ROLE_MASTER - 1);
5313 BUILD_ASSERT(NX_ROLE_SLAVE == OFPCR12_ROLE_SLAVE - 1);
5315 buf = ofpraw_alloc_reply(OFPRAW_NXT_ROLE_REPLY, request, 0);
5316 nrr = ofpbuf_put_zeros(buf, sizeof *nrr);
5317 nrr->role = htonl(rr->role - 1);
5325 /* Encodes "role status" message 'status' for sending in the given
5326 * 'protocol'. Returns the role status message, if 'protocol' supports them,
5327 * otherwise a null pointer. */
5329 ofputil_encode_role_status(const struct ofputil_role_status *status,
5330 enum ofputil_protocol protocol)
5332 enum ofp_version version;
5334 version = ofputil_protocol_to_ofp_version(protocol);
5335 if (version >= OFP14_VERSION) {
5336 struct ofp14_role_status *rstatus;
5339 buf = ofpraw_alloc_xid(OFPRAW_OFPT14_ROLE_STATUS, version, htonl(0),
5341 rstatus = ofpbuf_put_zeros(buf, sizeof *rstatus);
5342 rstatus->role = htonl(status->role);
5343 rstatus->reason = status->reason;
5344 rstatus->generation_id = htonll(status->generation_id);
5353 ofputil_decode_role_status(const struct ofp_header *oh,
5354 struct ofputil_role_status *rs)
5358 const struct ofp14_role_status *r;
5360 ofpbuf_use_const(&b, oh, ntohs(oh->length));
5361 raw = ofpraw_pull_assert(&b);
5362 ovs_assert(raw == OFPRAW_OFPT14_ROLE_STATUS);
5365 if (r->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
5366 r->role != htonl(OFPCR12_ROLE_EQUAL) &&
5367 r->role != htonl(OFPCR12_ROLE_MASTER) &&
5368 r->role != htonl(OFPCR12_ROLE_SLAVE)) {
5369 return OFPERR_OFPRRFC_BAD_ROLE;
5372 rs->role = ntohl(r->role);
5373 rs->generation_id = ntohll(r->generation_id);
5374 rs->reason = r->reason;
5381 /* OpenFlow 1.0 and 1.1 don't distinguish between a field that cannot be
5382 * matched and a field that must be wildcarded. This function returns a bitmap
5383 * that contains both kinds of fields. */
5384 static struct mf_bitmap
5385 wild_or_nonmatchable_fields(const struct ofputil_table_features *features)
5387 struct mf_bitmap wc = features->match;
5388 bitmap_not(wc.bm, MFF_N_IDS);
5389 bitmap_or(wc.bm, features->wildcard.bm, MFF_N_IDS);
5393 struct ofp10_wc_map {
5394 enum ofp10_flow_wildcards wc10;
5395 enum mf_field_id mf;
5398 static const struct ofp10_wc_map ofp10_wc_map[] = {
5399 { OFPFW10_IN_PORT, MFF_IN_PORT },
5400 { OFPFW10_DL_VLAN, MFF_VLAN_VID },
5401 { OFPFW10_DL_SRC, MFF_ETH_SRC },
5402 { OFPFW10_DL_DST, MFF_ETH_DST},
5403 { OFPFW10_DL_TYPE, MFF_ETH_TYPE },
5404 { OFPFW10_NW_PROTO, MFF_IP_PROTO },
5405 { OFPFW10_TP_SRC, MFF_TCP_SRC },
5406 { OFPFW10_TP_DST, MFF_TCP_DST },
5407 { OFPFW10_NW_SRC_MASK, MFF_IPV4_SRC },
5408 { OFPFW10_NW_DST_MASK, MFF_IPV4_DST },
5409 { OFPFW10_DL_VLAN_PCP, MFF_VLAN_PCP },
5410 { OFPFW10_NW_TOS, MFF_IP_DSCP },
5414 mf_bitmap_to_of10(const struct mf_bitmap *fields)
5416 const struct ofp10_wc_map *p;
5419 for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
5420 if (bitmap_is_set(fields->bm, p->mf)) {
5427 static struct mf_bitmap
5428 mf_bitmap_from_of10(ovs_be32 wc10_)
5430 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
5431 const struct ofp10_wc_map *p;
5432 uint32_t wc10 = ntohl(wc10_);
5434 for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
5435 if (wc10 & p->wc10) {
5436 bitmap_set1(fields.bm, p->mf);
5443 ofputil_put_ofp10_table_stats(const struct ofputil_table_stats *stats,
5444 const struct ofputil_table_features *features,
5447 struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
5448 struct ofp10_table_stats *out;
5450 out = ofpbuf_put_zeros(buf, sizeof *out);
5451 out->table_id = features->table_id;
5452 ovs_strlcpy(out->name, features->name, sizeof out->name);
5453 out->wildcards = mf_bitmap_to_of10(&wc);
5454 out->max_entries = htonl(features->max_entries);
5455 out->active_count = htonl(stats->active_count);
5456 put_32aligned_be64(&out->lookup_count, htonll(stats->lookup_count));
5457 put_32aligned_be64(&out->matched_count, htonll(stats->matched_count));
5460 struct ofp11_wc_map {
5461 enum ofp11_flow_match_fields wc11;
5462 enum mf_field_id mf;
5465 static const struct ofp11_wc_map ofp11_wc_map[] = {
5466 { OFPFMF11_IN_PORT, MFF_IN_PORT },
5467 { OFPFMF11_DL_VLAN, MFF_VLAN_VID },
5468 { OFPFMF11_DL_VLAN_PCP, MFF_VLAN_PCP },
5469 { OFPFMF11_DL_TYPE, MFF_ETH_TYPE },
5470 { OFPFMF11_NW_TOS, MFF_IP_DSCP },
5471 { OFPFMF11_NW_PROTO, MFF_IP_PROTO },
5472 { OFPFMF11_TP_SRC, MFF_TCP_SRC },
5473 { OFPFMF11_TP_DST, MFF_TCP_DST },
5474 { OFPFMF11_MPLS_LABEL, MFF_MPLS_LABEL },
5475 { OFPFMF11_MPLS_TC, MFF_MPLS_TC },
5476 /* I don't know what OFPFMF11_TYPE means. */
5477 { OFPFMF11_DL_SRC, MFF_ETH_SRC },
5478 { OFPFMF11_DL_DST, MFF_ETH_DST },
5479 { OFPFMF11_NW_SRC, MFF_IPV4_SRC },
5480 { OFPFMF11_NW_DST, MFF_IPV4_DST },
5481 { OFPFMF11_METADATA, MFF_METADATA },
5485 mf_bitmap_to_of11(const struct mf_bitmap *fields)
5487 const struct ofp11_wc_map *p;
5490 for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
5491 if (bitmap_is_set(fields->bm, p->mf)) {
5498 static struct mf_bitmap
5499 mf_bitmap_from_of11(ovs_be32 wc11_)
5501 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
5502 const struct ofp11_wc_map *p;
5503 uint32_t wc11 = ntohl(wc11_);
5505 for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
5506 if (wc11 & p->wc11) {
5507 bitmap_set1(fields.bm, p->mf);
5514 ofputil_put_ofp11_table_stats(const struct ofputil_table_stats *stats,
5515 const struct ofputil_table_features *features,
5518 struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
5519 struct ofp11_table_stats *out;
5521 out = ofpbuf_put_zeros(buf, sizeof *out);
5522 out->table_id = features->table_id;
5523 ovs_strlcpy(out->name, features->name, sizeof out->name);
5524 out->wildcards = mf_bitmap_to_of11(&wc);
5525 out->match = mf_bitmap_to_of11(&features->match);
5526 out->instructions = ovsinst_bitmap_to_openflow(
5527 features->nonmiss.instructions, OFP11_VERSION);
5528 out->write_actions = ofpact_bitmap_to_openflow(
5529 features->nonmiss.write.ofpacts, OFP11_VERSION);
5530 out->apply_actions = ofpact_bitmap_to_openflow(
5531 features->nonmiss.apply.ofpacts, OFP11_VERSION);
5532 out->config = htonl(features->miss_config);
5533 out->max_entries = htonl(features->max_entries);
5534 out->active_count = htonl(stats->active_count);
5535 out->lookup_count = htonll(stats->lookup_count);
5536 out->matched_count = htonll(stats->matched_count);
5540 ofputil_put_ofp12_table_stats(const struct ofputil_table_stats *stats,
5541 const struct ofputil_table_features *features,
5544 struct ofp12_table_stats *out;
5546 out = ofpbuf_put_zeros(buf, sizeof *out);
5547 out->table_id = features->table_id;
5548 ovs_strlcpy(out->name, features->name, sizeof out->name);
5549 out->match = oxm_bitmap_from_mf_bitmap(&features->match, OFP12_VERSION);
5550 out->wildcards = oxm_bitmap_from_mf_bitmap(&features->wildcard,
5552 out->write_actions = ofpact_bitmap_to_openflow(
5553 features->nonmiss.write.ofpacts, OFP12_VERSION);
5554 out->apply_actions = ofpact_bitmap_to_openflow(
5555 features->nonmiss.apply.ofpacts, OFP12_VERSION);
5556 out->write_setfields = oxm_bitmap_from_mf_bitmap(
5557 &features->nonmiss.write.set_fields, OFP12_VERSION);
5558 out->apply_setfields = oxm_bitmap_from_mf_bitmap(
5559 &features->nonmiss.apply.set_fields, OFP12_VERSION);
5560 out->metadata_match = features->metadata_match;
5561 out->metadata_write = features->metadata_write;
5562 out->instructions = ovsinst_bitmap_to_openflow(
5563 features->nonmiss.instructions, OFP12_VERSION);
5564 out->config = ofputil_encode_table_config(features->miss_config,
5565 OFPUTIL_TABLE_EVICTION_DEFAULT,
5567 out->max_entries = htonl(features->max_entries);
5568 out->active_count = htonl(stats->active_count);
5569 out->lookup_count = htonll(stats->lookup_count);
5570 out->matched_count = htonll(stats->matched_count);
5574 ofputil_put_ofp13_table_stats(const struct ofputil_table_stats *stats,
5577 struct ofp13_table_stats *out;
5579 out = ofpbuf_put_zeros(buf, sizeof *out);
5580 out->table_id = stats->table_id;
5581 out->active_count = htonl(stats->active_count);
5582 out->lookup_count = htonll(stats->lookup_count);
5583 out->matched_count = htonll(stats->matched_count);
5587 ofputil_encode_table_stats_reply(const struct ofp_header *request)
5589 return ofpraw_alloc_stats_reply(request, 0);
5593 ofputil_append_table_stats_reply(struct ofpbuf *reply,
5594 const struct ofputil_table_stats *stats,
5595 const struct ofputil_table_features *features)
5597 struct ofp_header *oh = reply->header;
5599 ovs_assert(stats->table_id == features->table_id);
5601 switch ((enum ofp_version) oh->version) {
5603 ofputil_put_ofp10_table_stats(stats, features, reply);
5607 ofputil_put_ofp11_table_stats(stats, features, reply);
5611 ofputil_put_ofp12_table_stats(stats, features, reply);
5617 ofputil_put_ofp13_table_stats(stats, reply);
5626 ofputil_decode_ofp10_table_stats(struct ofpbuf *msg,
5627 struct ofputil_table_stats *stats,
5628 struct ofputil_table_features *features)
5630 struct ofp10_table_stats *ots;
5632 ots = ofpbuf_try_pull(msg, sizeof *ots);
5634 return OFPERR_OFPBRC_BAD_LEN;
5637 features->table_id = ots->table_id;
5638 ovs_strlcpy(features->name, ots->name, sizeof features->name);
5639 features->max_entries = ntohl(ots->max_entries);
5640 features->match = features->wildcard = mf_bitmap_from_of10(ots->wildcards);
5642 stats->table_id = ots->table_id;
5643 stats->active_count = ntohl(ots->active_count);
5644 stats->lookup_count = ntohll(get_32aligned_be64(&ots->lookup_count));
5645 stats->matched_count = ntohll(get_32aligned_be64(&ots->matched_count));
5651 ofputil_decode_ofp11_table_stats(struct ofpbuf *msg,
5652 struct ofputil_table_stats *stats,
5653 struct ofputil_table_features *features)
5655 struct ofp11_table_stats *ots;
5657 ots = ofpbuf_try_pull(msg, sizeof *ots);
5659 return OFPERR_OFPBRC_BAD_LEN;
5662 features->table_id = ots->table_id;
5663 ovs_strlcpy(features->name, ots->name, sizeof features->name);
5664 features->max_entries = ntohl(ots->max_entries);
5665 features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
5666 ots->instructions, OFP11_VERSION);
5667 features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
5668 ots->write_actions, OFP11_VERSION);
5669 features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
5670 ots->write_actions, OFP11_VERSION);
5671 features->miss = features->nonmiss;
5672 features->miss_config = ofputil_decode_table_miss(ots->config,
5674 features->match = mf_bitmap_from_of11(ots->match);
5675 features->wildcard = mf_bitmap_from_of11(ots->wildcards);
5676 bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
5678 stats->table_id = ots->table_id;
5679 stats->active_count = ntohl(ots->active_count);
5680 stats->lookup_count = ntohll(ots->lookup_count);
5681 stats->matched_count = ntohll(ots->matched_count);
5687 ofputil_decode_ofp12_table_stats(struct ofpbuf *msg,
5688 struct ofputil_table_stats *stats,
5689 struct ofputil_table_features *features)
5691 struct ofp12_table_stats *ots;
5693 ots = ofpbuf_try_pull(msg, sizeof *ots);
5695 return OFPERR_OFPBRC_BAD_LEN;
5698 features->table_id = ots->table_id;
5699 ovs_strlcpy(features->name, ots->name, sizeof features->name);
5700 features->metadata_match = ots->metadata_match;
5701 features->metadata_write = ots->metadata_write;
5702 features->miss_config = ofputil_decode_table_miss(ots->config,
5704 features->max_entries = ntohl(ots->max_entries);
5706 features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
5707 ots->instructions, OFP12_VERSION);
5708 features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
5709 ots->write_actions, OFP12_VERSION);
5710 features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
5711 ots->apply_actions, OFP12_VERSION);
5712 features->nonmiss.write.set_fields = oxm_bitmap_to_mf_bitmap(
5713 ots->write_setfields, OFP12_VERSION);
5714 features->nonmiss.apply.set_fields = oxm_bitmap_to_mf_bitmap(
5715 ots->apply_setfields, OFP12_VERSION);
5716 features->miss = features->nonmiss;
5718 features->match = oxm_bitmap_to_mf_bitmap(ots->match, OFP12_VERSION);
5719 features->wildcard = oxm_bitmap_to_mf_bitmap(ots->wildcards,
5721 bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
5723 stats->table_id = ots->table_id;
5724 stats->active_count = ntohl(ots->active_count);
5725 stats->lookup_count = ntohll(ots->lookup_count);
5726 stats->matched_count = ntohll(ots->matched_count);
5732 ofputil_decode_ofp13_table_stats(struct ofpbuf *msg,
5733 struct ofputil_table_stats *stats,
5734 struct ofputil_table_features *features)
5736 struct ofp13_table_stats *ots;
5738 ots = ofpbuf_try_pull(msg, sizeof *ots);
5740 return OFPERR_OFPBRC_BAD_LEN;
5743 features->table_id = ots->table_id;
5745 stats->table_id = ots->table_id;
5746 stats->active_count = ntohl(ots->active_count);
5747 stats->lookup_count = ntohll(ots->lookup_count);
5748 stats->matched_count = ntohll(ots->matched_count);
5754 ofputil_decode_table_stats_reply(struct ofpbuf *msg,
5755 struct ofputil_table_stats *stats,
5756 struct ofputil_table_features *features)
5758 const struct ofp_header *oh;
5761 ofpraw_pull_assert(msg);
5769 memset(stats, 0, sizeof *stats);
5770 memset(features, 0, sizeof *features);
5771 features->supports_eviction = -1;
5772 features->supports_vacancy_events = -1;
5774 switch ((enum ofp_version) oh->version) {
5776 return ofputil_decode_ofp10_table_stats(msg, stats, features);
5779 return ofputil_decode_ofp11_table_stats(msg, stats, features);
5782 return ofputil_decode_ofp12_table_stats(msg, stats, features);
5787 return ofputil_decode_ofp13_table_stats(msg, stats, features);
5794 /* ofputil_flow_monitor_request */
5796 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
5797 * ofputil_flow_monitor_request in 'rq'.
5799 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
5800 * message. Calling this function multiple times for a single 'msg' iterates
5801 * through the requests. The caller must initially leave 'msg''s layer
5802 * pointers null and not modify them between calls.
5804 * Returns 0 if successful, EOF if no requests were left in this 'msg',
5805 * otherwise an OFPERR_* value. */
5807 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
5810 struct nx_flow_monitor_request *nfmr;
5814 ofpraw_pull_assert(msg);
5821 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
5823 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %"PRIu32" "
5824 "leftover bytes at end", msg->size);
5825 return OFPERR_OFPBRC_BAD_LEN;
5828 flags = ntohs(nfmr->flags);
5829 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
5830 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
5831 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
5832 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
5834 return OFPERR_OFPMOFC_BAD_FLAGS;
5837 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
5838 return OFPERR_NXBRC_MUST_BE_ZERO;
5841 rq->id = ntohl(nfmr->id);
5843 rq->out_port = u16_to_ofp(ntohs(nfmr->out_port));
5844 rq->table_id = nfmr->table_id;
5846 return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL, NULL);
5850 ofputil_append_flow_monitor_request(
5851 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
5853 struct nx_flow_monitor_request *nfmr;
5858 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
5861 start_ofs = msg->size;
5862 ofpbuf_put_zeros(msg, sizeof *nfmr);
5863 match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
5865 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
5866 nfmr->id = htonl(rq->id);
5867 nfmr->flags = htons(rq->flags);
5868 nfmr->out_port = htons(ofp_to_u16(rq->out_port));
5869 nfmr->match_len = htons(match_len);
5870 nfmr->table_id = rq->table_id;
5873 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
5874 * into an abstract ofputil_flow_update in 'update'. The caller must have
5875 * initialized update->match to point to space allocated for a match.
5877 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
5878 * actions (except for NXFME_ABBREV, which never includes actions). The caller
5879 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
5880 * will point into the 'ofpacts' buffer.
5882 * Multiple flow updates can be packed into a single OpenFlow message. Calling
5883 * this function multiple times for a single 'msg' iterates through the
5884 * updates. The caller must initially leave 'msg''s layer pointers null and
5885 * not modify them between calls.
5887 * Returns 0 if successful, EOF if no updates were left in this 'msg',
5888 * otherwise an OFPERR_* value. */
5890 ofputil_decode_flow_update(struct ofputil_flow_update *update,
5891 struct ofpbuf *msg, struct ofpbuf *ofpacts)
5893 struct nx_flow_update_header *nfuh;
5894 unsigned int length;
5895 struct ofp_header *oh;
5898 ofpraw_pull_assert(msg);
5905 if (msg->size < sizeof(struct nx_flow_update_header)) {
5912 update->event = ntohs(nfuh->event);
5913 length = ntohs(nfuh->length);
5914 if (length > msg->size || length % 8) {
5918 if (update->event == NXFME_ABBREV) {
5919 struct nx_flow_update_abbrev *nfua;
5921 if (length != sizeof *nfua) {
5925 nfua = ofpbuf_pull(msg, sizeof *nfua);
5926 update->xid = nfua->xid;
5928 } else if (update->event == NXFME_ADDED
5929 || update->event == NXFME_DELETED
5930 || update->event == NXFME_MODIFIED) {
5931 struct nx_flow_update_full *nfuf;
5932 unsigned int actions_len;
5933 unsigned int match_len;
5936 if (length < sizeof *nfuf) {
5940 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
5941 match_len = ntohs(nfuf->match_len);
5942 if (sizeof *nfuf + match_len > length) {
5946 update->reason = ntohs(nfuf->reason);
5947 update->idle_timeout = ntohs(nfuf->idle_timeout);
5948 update->hard_timeout = ntohs(nfuf->hard_timeout);
5949 update->table_id = nfuf->table_id;
5950 update->cookie = nfuf->cookie;
5951 update->priority = ntohs(nfuf->priority);
5953 error = nx_pull_match(msg, match_len, update->match, NULL, NULL);
5958 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
5959 error = ofpacts_pull_openflow_actions(msg, actions_len, oh->version,
5965 update->ofpacts = ofpacts->data;
5966 update->ofpacts_len = ofpacts->size;
5969 VLOG_WARN_RL(&bad_ofmsg_rl,
5970 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
5971 ntohs(nfuh->event));
5972 return OFPERR_NXBRC_FM_BAD_EVENT;
5976 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %"PRIu32" "
5977 "leftover bytes at end", msg->size);
5978 return OFPERR_OFPBRC_BAD_LEN;
5982 ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
5984 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
5986 return ntohl(cancel->id);
5990 ofputil_encode_flow_monitor_cancel(uint32_t id)
5992 struct nx_flow_monitor_cancel *nfmc;
5995 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
5996 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
5997 nfmc->id = htonl(id);
6002 ofputil_start_flow_update(struct ovs_list *replies)
6006 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
6010 list_push_back(replies, &msg->list_node);
6014 ofputil_append_flow_update(const struct ofputil_flow_update *update,
6015 struct ovs_list *replies)
6017 enum ofp_version version = ofpmp_version(replies);
6018 struct nx_flow_update_header *nfuh;
6022 msg = ofpbuf_from_list(list_back(replies));
6023 start_ofs = msg->size;
6025 if (update->event == NXFME_ABBREV) {
6026 struct nx_flow_update_abbrev *nfua;
6028 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
6029 nfua->xid = update->xid;
6031 struct nx_flow_update_full *nfuf;
6034 ofpbuf_put_zeros(msg, sizeof *nfuf);
6035 match_len = nx_put_match(msg, update->match, htonll(0), htonll(0));
6036 ofpacts_put_openflow_actions(update->ofpacts, update->ofpacts_len, msg,
6038 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
6039 nfuf->reason = htons(update->reason);
6040 nfuf->priority = htons(update->priority);
6041 nfuf->idle_timeout = htons(update->idle_timeout);
6042 nfuf->hard_timeout = htons(update->hard_timeout);
6043 nfuf->match_len = htons(match_len);
6044 nfuf->table_id = update->table_id;
6045 nfuf->cookie = update->cookie;
6048 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
6049 nfuh->length = htons(msg->size - start_ofs);
6050 nfuh->event = htons(update->event);
6052 ofpmp_postappend(replies, start_ofs);
6056 ofputil_encode_packet_out(const struct ofputil_packet_out *po,
6057 enum ofputil_protocol protocol)
6059 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
6063 size = po->ofpacts_len;
6064 if (po->buffer_id == UINT32_MAX) {
6065 size += po->packet_len;
6068 switch (ofp_version) {
6069 case OFP10_VERSION: {
6070 struct ofp10_packet_out *opo;
6073 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
6074 ofpbuf_put_zeros(msg, sizeof *opo);
6075 actions_ofs = msg->size;
6076 ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
6080 opo->buffer_id = htonl(po->buffer_id);
6081 opo->in_port = htons(ofp_to_u16(po->in_port));
6082 opo->actions_len = htons(msg->size - actions_ofs);
6090 case OFP15_VERSION: {
6091 struct ofp11_packet_out *opo;
6094 msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size);
6095 ofpbuf_put_zeros(msg, sizeof *opo);
6096 len = ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
6099 opo->buffer_id = htonl(po->buffer_id);
6100 opo->in_port = ofputil_port_to_ofp11(po->in_port);
6101 opo->actions_len = htons(len);
6109 if (po->buffer_id == UINT32_MAX) {
6110 ofpbuf_put(msg, po->packet, po->packet_len);
6113 ofpmsg_update_length(msg);
6118 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
6120 make_echo_request(enum ofp_version ofp_version)
6122 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
6126 /* Creates and returns an OFPT_ECHO_REPLY message matching the
6127 * OFPT_ECHO_REQUEST message in 'rq'. */
6129 make_echo_reply(const struct ofp_header *rq)
6131 struct ofpbuf rq_buf;
6132 struct ofpbuf *reply;
6134 ofpbuf_use_const(&rq_buf, rq, ntohs(rq->length));
6135 ofpraw_pull_assert(&rq_buf);
6137 reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY, rq, rq_buf.size);
6138 ofpbuf_put(reply, rq_buf.data, rq_buf.size);
6143 ofputil_encode_barrier_request(enum ofp_version ofp_version)
6147 switch (ofp_version) {
6153 type = OFPRAW_OFPT11_BARRIER_REQUEST;
6157 type = OFPRAW_OFPT10_BARRIER_REQUEST;
6164 return ofpraw_alloc(type, ofp_version, 0);
6168 ofputil_frag_handling_to_string(enum ofp_config_flags flags)
6170 switch (flags & OFPC_FRAG_MASK) {
6171 case OFPC_FRAG_NORMAL: return "normal";
6172 case OFPC_FRAG_DROP: return "drop";
6173 case OFPC_FRAG_REASM: return "reassemble";
6174 case OFPC_FRAG_NX_MATCH: return "nx-match";
6181 ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
6183 if (!strcasecmp(s, "normal")) {
6184 *flags = OFPC_FRAG_NORMAL;
6185 } else if (!strcasecmp(s, "drop")) {
6186 *flags = OFPC_FRAG_DROP;
6187 } else if (!strcasecmp(s, "reassemble")) {
6188 *flags = OFPC_FRAG_REASM;
6189 } else if (!strcasecmp(s, "nx-match")) {
6190 *flags = OFPC_FRAG_NX_MATCH;
6197 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
6198 * port number and stores the latter in '*ofp10_port', for the purpose of
6199 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
6200 * otherwise an OFPERR_* number. On error, stores OFPP_NONE in '*ofp10_port'.
6202 * See the definition of OFP11_MAX for an explanation of the mapping. */
6204 ofputil_port_from_ofp11(ovs_be32 ofp11_port, ofp_port_t *ofp10_port)
6206 uint32_t ofp11_port_h = ntohl(ofp11_port);
6208 if (ofp11_port_h < ofp_to_u16(OFPP_MAX)) {
6209 *ofp10_port = u16_to_ofp(ofp11_port_h);
6211 } else if (ofp11_port_h >= ofp11_to_u32(OFPP11_MAX)) {
6212 *ofp10_port = u16_to_ofp(ofp11_port_h - OFPP11_OFFSET);
6215 *ofp10_port = OFPP_NONE;
6216 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
6217 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
6218 ofp11_port_h, ofp_to_u16(OFPP_MAX) - 1,
6219 ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
6220 return OFPERR_OFPBAC_BAD_OUT_PORT;
6224 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
6225 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
6227 * See the definition of OFP11_MAX for an explanation of the mapping. */
6229 ofputil_port_to_ofp11(ofp_port_t ofp10_port)
6231 return htonl(ofp_to_u16(ofp10_port) < ofp_to_u16(OFPP_MAX)
6232 ? ofp_to_u16(ofp10_port)
6233 : ofp_to_u16(ofp10_port) + OFPP11_OFFSET);
6236 #define OFPUTIL_NAMED_PORTS \
6237 OFPUTIL_NAMED_PORT(IN_PORT) \
6238 OFPUTIL_NAMED_PORT(TABLE) \
6239 OFPUTIL_NAMED_PORT(NORMAL) \
6240 OFPUTIL_NAMED_PORT(FLOOD) \
6241 OFPUTIL_NAMED_PORT(ALL) \
6242 OFPUTIL_NAMED_PORT(CONTROLLER) \
6243 OFPUTIL_NAMED_PORT(LOCAL) \
6244 OFPUTIL_NAMED_PORT(ANY) \
6245 OFPUTIL_NAMED_PORT(UNSET)
6247 /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */
6248 #define OFPUTIL_NAMED_PORTS_WITH_NONE \
6249 OFPUTIL_NAMED_PORTS \
6250 OFPUTIL_NAMED_PORT(NONE)
6252 /* Stores the port number represented by 's' into '*portp'. 's' may be an
6253 * integer or, for reserved ports, the standard OpenFlow name for the port
6256 * Returns true if successful, false if 's' is not a valid OpenFlow port number
6257 * or name. The caller should issue an error message in this case, because
6258 * this function usually does not. (This gives the caller an opportunity to
6259 * look up the port name another way, e.g. by contacting the switch and listing
6260 * the names of all its ports).
6262 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
6263 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
6264 * range as described in include/openflow/openflow-1.1.h. */
6266 ofputil_port_from_string(const char *s, ofp_port_t *portp)
6268 unsigned int port32; /* int is at least 32 bits wide. */
6271 VLOG_WARN("Negative value %s is not a valid port number.", s);
6275 if (str_to_uint(s, 10, &port32)) {
6276 if (port32 < ofp_to_u16(OFPP_MAX)) {
6278 } else if (port32 < ofp_to_u16(OFPP_FIRST_RESV)) {
6279 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
6280 "be translated to %u when talking to an OF1.1 or "
6281 "later controller", port32, port32 + OFPP11_OFFSET);
6282 } else if (port32 <= ofp_to_u16(OFPP_LAST_RESV)) {
6283 char name[OFP_MAX_PORT_NAME_LEN];
6285 ofputil_port_to_string(u16_to_ofp(port32), name, sizeof name);
6286 VLOG_WARN_ONCE("referring to port %s as %"PRIu32" is deprecated "
6287 "for compatibility with OpenFlow 1.1 and later",
6289 } else if (port32 < ofp11_to_u32(OFPP11_MAX)) {
6290 VLOG_WARN("port %u is outside the supported range 0 through "
6291 "%"PRIx16" or 0x%x through 0x%"PRIx32, port32,
6292 UINT16_MAX, ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
6295 port32 -= OFPP11_OFFSET;
6298 *portp = u16_to_ofp(port32);
6305 static const struct pair pairs[] = {
6306 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
6307 OFPUTIL_NAMED_PORTS_WITH_NONE
6308 #undef OFPUTIL_NAMED_PORT
6310 const struct pair *p;
6312 for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) {
6313 if (!strcasecmp(s, p->name)) {
6322 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
6323 * Most ports' string representation is just the port number, but for special
6324 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
6326 ofputil_format_port(ofp_port_t port, struct ds *s)
6328 char name[OFP_MAX_PORT_NAME_LEN];
6330 ofputil_port_to_string(port, name, sizeof name);
6331 ds_put_cstr(s, name);
6334 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
6335 * representation of OpenFlow port number 'port'. Most ports are represented
6336 * as just the port number, but special ports, e.g. OFPP_LOCAL, are represented
6337 * by name, e.g. "LOCAL". */
6339 ofputil_port_to_string(ofp_port_t port,
6340 char namebuf[OFP_MAX_PORT_NAME_LEN], size_t bufsize)
6343 #define OFPUTIL_NAMED_PORT(NAME) \
6345 ovs_strlcpy(namebuf, #NAME, bufsize); \
6348 #undef OFPUTIL_NAMED_PORT
6351 snprintf(namebuf, bufsize, "%"PRIu16, port);
6356 /* Stores the group id represented by 's' into '*group_idp'. 's' may be an
6357 * integer or, for reserved group IDs, the standard OpenFlow name for the group
6358 * (either "ANY" or "ALL").
6360 * Returns true if successful, false if 's' is not a valid OpenFlow group ID or
6363 ofputil_group_from_string(const char *s, uint32_t *group_idp)
6365 if (!strcasecmp(s, "any")) {
6366 *group_idp = OFPG11_ANY;
6367 } else if (!strcasecmp(s, "all")) {
6368 *group_idp = OFPG11_ALL;
6369 } else if (!str_to_uint(s, 10, group_idp)) {
6370 VLOG_WARN("%s is not a valid group ID. (Valid group IDs are "
6371 "32-bit nonnegative integers or the keywords ANY or "
6379 /* Appends to 's' a string representation of the OpenFlow group ID 'group_id'.
6380 * Most groups' string representation is just the number, but for special
6381 * groups, e.g. OFPG11_ALL, it is the name, e.g. "ALL". */
6383 ofputil_format_group(uint32_t group_id, struct ds *s)
6385 char name[MAX_GROUP_NAME_LEN];
6387 ofputil_group_to_string(group_id, name, sizeof name);
6388 ds_put_cstr(s, name);
6392 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
6393 * representation of OpenFlow group ID 'group_id'. Most group are represented
6394 * as just their number, but special groups, e.g. OFPG11_ALL, are represented
6395 * by name, e.g. "ALL". */
6397 ofputil_group_to_string(uint32_t group_id,
6398 char namebuf[MAX_GROUP_NAME_LEN + 1], size_t bufsize)
6402 ovs_strlcpy(namebuf, "ALL", bufsize);
6406 ovs_strlcpy(namebuf, "ANY", bufsize);
6410 snprintf(namebuf, bufsize, "%"PRIu32, group_id);
6415 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
6416 * 'ofp_version', tries to pull the first element from the array. If
6417 * successful, initializes '*pp' with an abstract representation of the
6418 * port and returns 0. If no ports remain to be decoded, returns EOF.
6419 * On an error, returns a positive OFPERR_* value. */
6421 ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
6422 struct ofputil_phy_port *pp)
6424 memset(pp, 0, sizeof *pp);
6426 switch (ofp_version) {
6427 case OFP10_VERSION: {
6428 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
6429 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
6433 case OFP13_VERSION: {
6434 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
6435 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
6439 return b->size ? ofputil_pull_ofp14_port(pp, b) : EOF;
6446 ofputil_normalize_match__(struct match *match, bool may_log)
6449 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
6450 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
6451 MAY_NW_PROTO = 1 << 2, /* nw_proto */
6452 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
6453 MAY_ARP_SHA = 1 << 4, /* arp_sha */
6454 MAY_ARP_THA = 1 << 5, /* arp_tha */
6455 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
6456 MAY_ND_TARGET = 1 << 7, /* nd_target */
6457 MAY_MPLS = 1 << 8, /* mpls label and tc */
6460 struct flow_wildcards wc;
6462 /* Figure out what fields may be matched. */
6463 if (match->flow.dl_type == htons(ETH_TYPE_IP)) {
6464 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
6465 if (match->flow.nw_proto == IPPROTO_TCP ||
6466 match->flow.nw_proto == IPPROTO_UDP ||
6467 match->flow.nw_proto == IPPROTO_SCTP ||
6468 match->flow.nw_proto == IPPROTO_ICMP) {
6469 may_match |= MAY_TP_ADDR;
6471 } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
6472 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
6473 if (match->flow.nw_proto == IPPROTO_TCP ||
6474 match->flow.nw_proto == IPPROTO_UDP ||
6475 match->flow.nw_proto == IPPROTO_SCTP) {
6476 may_match |= MAY_TP_ADDR;
6477 } else if (match->flow.nw_proto == IPPROTO_ICMPV6) {
6478 may_match |= MAY_TP_ADDR;
6479 if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
6480 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
6481 } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
6482 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
6485 } else if (match->flow.dl_type == htons(ETH_TYPE_ARP) ||
6486 match->flow.dl_type == htons(ETH_TYPE_RARP)) {
6487 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
6488 } else if (eth_type_mpls(match->flow.dl_type)) {
6489 may_match = MAY_MPLS;
6494 /* Clear the fields that may not be matched. */
6496 if (!(may_match & MAY_NW_ADDR)) {
6497 wc.masks.nw_src = wc.masks.nw_dst = htonl(0);
6499 if (!(may_match & MAY_TP_ADDR)) {
6500 wc.masks.tp_src = wc.masks.tp_dst = htons(0);
6502 if (!(may_match & MAY_NW_PROTO)) {
6503 wc.masks.nw_proto = 0;
6505 if (!(may_match & MAY_IPVx)) {
6506 wc.masks.nw_tos = 0;
6507 wc.masks.nw_ttl = 0;
6509 if (!(may_match & MAY_ARP_SHA)) {
6510 WC_UNMASK_FIELD(&wc, arp_sha);
6512 if (!(may_match & MAY_ARP_THA)) {
6513 WC_UNMASK_FIELD(&wc, arp_tha);
6515 if (!(may_match & MAY_IPV6)) {
6516 wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any;
6517 wc.masks.ipv6_label = htonl(0);
6519 if (!(may_match & MAY_ND_TARGET)) {
6520 wc.masks.nd_target = in6addr_any;
6522 if (!(may_match & MAY_MPLS)) {
6523 memset(wc.masks.mpls_lse, 0, sizeof wc.masks.mpls_lse);
6526 /* Log any changes. */
6527 if (!flow_wildcards_equal(&wc, &match->wc)) {
6528 bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
6529 char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL;
6532 match_zero_wildcarded_fields(match);
6535 char *post = match_to_string(match, OFP_DEFAULT_PRIORITY);
6536 VLOG_INFO("normalization changed ofp_match, details:");
6537 VLOG_INFO(" pre: %s", pre);
6538 VLOG_INFO("post: %s", post);
6545 /* "Normalizes" the wildcards in 'match'. That means:
6547 * 1. If the type of level N is known, then only the valid fields for that
6548 * level may be specified. For example, ARP does not have a TOS field,
6549 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
6550 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
6551 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
6554 * 2. If the type of level N is not known (or not understood by Open
6555 * vSwitch), then no fields at all for that level may be specified. For
6556 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
6557 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
6560 * If this function changes 'match', it logs a rate-limited informational
6563 ofputil_normalize_match(struct match *match)
6565 ofputil_normalize_match__(match, true);
6568 /* Same as ofputil_normalize_match() without the logging. Thus, this function
6569 * is suitable for a program's internal use, whereas ofputil_normalize_match()
6570 * sense for use on flows received from elsewhere (so that a bug in the program
6571 * that sent them can be reported and corrected). */
6573 ofputil_normalize_match_quiet(struct match *match)
6575 ofputil_normalize_match__(match, false);
6578 /* Parses a key or a key-value pair from '*stringp'.
6580 * On success: Stores the key into '*keyp'. Stores the value, if present, into
6581 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
6582 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
6583 * are substrings of '*stringp' created by replacing some of its bytes by null
6584 * terminators. Returns true.
6586 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
6587 * NULL and returns false. */
6589 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
6591 char *pos, *key, *value;
6595 pos += strspn(pos, ", \t\r\n");
6597 *keyp = *valuep = NULL;
6602 key_len = strcspn(pos, ":=(, \t\r\n");
6603 if (key[key_len] == ':' || key[key_len] == '=') {
6604 /* The value can be separated by a colon. */
6607 value = key + key_len + 1;
6608 value_len = strcspn(value, ", \t\r\n");
6609 pos = value + value_len + (value[value_len] != '\0');
6610 value[value_len] = '\0';
6611 } else if (key[key_len] == '(') {
6612 /* The value can be surrounded by balanced parentheses. The outermost
6613 * set of parentheses is removed. */
6617 value = key + key_len + 1;
6618 for (value_len = 0; level > 0; value_len++) {
6619 switch (value[value_len]) {
6633 value[value_len - 1] = '\0';
6634 pos = value + value_len;
6636 /* There might be no value at all. */
6637 value = key + key_len; /* Will become the empty string below. */
6638 pos = key + key_len + (key[key_len] != '\0');
6640 key[key_len] = '\0';
6648 /* Encode a dump ports request for 'port', the encoded message
6649 * will be for OpenFlow version 'ofp_version'. Returns message
6650 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
6652 ofputil_encode_dump_ports_request(enum ofp_version ofp_version, ofp_port_t port)
6654 struct ofpbuf *request;
6656 switch (ofp_version) {
6657 case OFP10_VERSION: {
6658 struct ofp10_port_stats_request *req;
6659 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0);
6660 req = ofpbuf_put_zeros(request, sizeof *req);
6661 req->port_no = htons(ofp_to_u16(port));
6668 case OFP15_VERSION: {
6669 struct ofp11_port_stats_request *req;
6670 request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0);
6671 req = ofpbuf_put_zeros(request, sizeof *req);
6672 req->port_no = ofputil_port_to_ofp11(port);
6683 ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops,
6684 struct ofp10_port_stats *ps10)
6686 ps10->port_no = htons(ofp_to_u16(ops->port_no));
6687 memset(ps10->pad, 0, sizeof ps10->pad);
6688 put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets));
6689 put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets));
6690 put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes));
6691 put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes));
6692 put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped));
6693 put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped));
6694 put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors));
6695 put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors));
6696 put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors));
6697 put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors));
6698 put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors));
6699 put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions));
6703 ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops,
6704 struct ofp11_port_stats *ps11)
6706 ps11->port_no = ofputil_port_to_ofp11(ops->port_no);
6707 memset(ps11->pad, 0, sizeof ps11->pad);
6708 ps11->rx_packets = htonll(ops->stats.rx_packets);
6709 ps11->tx_packets = htonll(ops->stats.tx_packets);
6710 ps11->rx_bytes = htonll(ops->stats.rx_bytes);
6711 ps11->tx_bytes = htonll(ops->stats.tx_bytes);
6712 ps11->rx_dropped = htonll(ops->stats.rx_dropped);
6713 ps11->tx_dropped = htonll(ops->stats.tx_dropped);
6714 ps11->rx_errors = htonll(ops->stats.rx_errors);
6715 ps11->tx_errors = htonll(ops->stats.tx_errors);
6716 ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors);
6717 ps11->rx_over_err = htonll(ops->stats.rx_over_errors);
6718 ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors);
6719 ps11->collisions = htonll(ops->stats.collisions);
6723 ofputil_port_stats_to_ofp13(const struct ofputil_port_stats *ops,
6724 struct ofp13_port_stats *ps13)
6726 ofputil_port_stats_to_ofp11(ops, &ps13->ps);
6727 ps13->duration_sec = htonl(ops->duration_sec);
6728 ps13->duration_nsec = htonl(ops->duration_nsec);
6732 ofputil_append_ofp14_port_stats(const struct ofputil_port_stats *ops,
6733 struct ovs_list *replies)
6735 struct ofp14_port_stats_prop_ethernet *eth;
6736 struct ofp14_port_stats *ps14;
6737 struct ofpbuf *reply;
6739 reply = ofpmp_reserve(replies, sizeof *ps14 + sizeof *eth);
6741 ps14 = ofpbuf_put_uninit(reply, sizeof *ps14);
6742 ps14->length = htons(sizeof *ps14 + sizeof *eth);
6743 memset(ps14->pad, 0, sizeof ps14->pad);
6744 ps14->port_no = ofputil_port_to_ofp11(ops->port_no);
6745 ps14->duration_sec = htonl(ops->duration_sec);
6746 ps14->duration_nsec = htonl(ops->duration_nsec);
6747 ps14->rx_packets = htonll(ops->stats.rx_packets);
6748 ps14->tx_packets = htonll(ops->stats.tx_packets);
6749 ps14->rx_bytes = htonll(ops->stats.rx_bytes);
6750 ps14->tx_bytes = htonll(ops->stats.tx_bytes);
6751 ps14->rx_dropped = htonll(ops->stats.rx_dropped);
6752 ps14->tx_dropped = htonll(ops->stats.tx_dropped);
6753 ps14->rx_errors = htonll(ops->stats.rx_errors);
6754 ps14->tx_errors = htonll(ops->stats.tx_errors);
6756 eth = ofpbuf_put_uninit(reply, sizeof *eth);
6757 eth->type = htons(OFPPSPT14_ETHERNET);
6758 eth->length = htons(sizeof *eth);
6759 memset(eth->pad, 0, sizeof eth->pad);
6760 eth->rx_frame_err = htonll(ops->stats.rx_frame_errors);
6761 eth->rx_over_err = htonll(ops->stats.rx_over_errors);
6762 eth->rx_crc_err = htonll(ops->stats.rx_crc_errors);
6763 eth->collisions = htonll(ops->stats.collisions);
6766 /* Encode a ports stat for 'ops' and append it to 'replies'. */
6768 ofputil_append_port_stat(struct ovs_list *replies,
6769 const struct ofputil_port_stats *ops)
6771 switch (ofpmp_version(replies)) {
6772 case OFP13_VERSION: {
6773 struct ofp13_port_stats *reply = ofpmp_append(replies, sizeof *reply);
6774 ofputil_port_stats_to_ofp13(ops, reply);
6778 case OFP11_VERSION: {
6779 struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply);
6780 ofputil_port_stats_to_ofp11(ops, reply);
6784 case OFP10_VERSION: {
6785 struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply);
6786 ofputil_port_stats_to_ofp10(ops, reply);
6792 ofputil_append_ofp14_port_stats(ops, replies);
6801 ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops,
6802 const struct ofp10_port_stats *ps10)
6804 memset(ops, 0, sizeof *ops);
6806 ops->port_no = u16_to_ofp(ntohs(ps10->port_no));
6807 ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets));
6808 ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets));
6809 ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes));
6810 ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes));
6811 ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped));
6812 ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped));
6813 ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors));
6814 ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors));
6815 ops->stats.rx_frame_errors =
6816 ntohll(get_32aligned_be64(&ps10->rx_frame_err));
6817 ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err));
6818 ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err));
6819 ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions));
6820 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
6826 ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops,
6827 const struct ofp11_port_stats *ps11)
6831 memset(ops, 0, sizeof *ops);
6832 error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no);
6837 ops->stats.rx_packets = ntohll(ps11->rx_packets);
6838 ops->stats.tx_packets = ntohll(ps11->tx_packets);
6839 ops->stats.rx_bytes = ntohll(ps11->rx_bytes);
6840 ops->stats.tx_bytes = ntohll(ps11->tx_bytes);
6841 ops->stats.rx_dropped = ntohll(ps11->rx_dropped);
6842 ops->stats.tx_dropped = ntohll(ps11->tx_dropped);
6843 ops->stats.rx_errors = ntohll(ps11->rx_errors);
6844 ops->stats.tx_errors = ntohll(ps11->tx_errors);
6845 ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err);
6846 ops->stats.rx_over_errors = ntohll(ps11->rx_over_err);
6847 ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err);
6848 ops->stats.collisions = ntohll(ps11->collisions);
6849 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
6855 ofputil_port_stats_from_ofp13(struct ofputil_port_stats *ops,
6856 const struct ofp13_port_stats *ps13)
6858 enum ofperr error = ofputil_port_stats_from_ofp11(ops, &ps13->ps);
6860 ops->duration_sec = ntohl(ps13->duration_sec);
6861 ops->duration_nsec = ntohl(ps13->duration_nsec);
6867 parse_ofp14_port_stats_ethernet_property(const struct ofpbuf *payload,
6868 struct ofputil_port_stats *ops)
6870 const struct ofp14_port_stats_prop_ethernet *eth = payload->data;
6872 if (payload->size != sizeof *eth) {
6873 return OFPERR_OFPBPC_BAD_LEN;
6876 ops->stats.rx_frame_errors = ntohll(eth->rx_frame_err);
6877 ops->stats.rx_over_errors = ntohll(eth->rx_over_err);
6878 ops->stats.rx_crc_errors = ntohll(eth->rx_crc_err);
6879 ops->stats.collisions = ntohll(eth->collisions);
6885 ofputil_pull_ofp14_port_stats(struct ofputil_port_stats *ops,
6888 const struct ofp14_port_stats *ps14;
6889 struct ofpbuf properties;
6893 ps14 = ofpbuf_try_pull(msg, sizeof *ps14);
6895 return OFPERR_OFPBRC_BAD_LEN;
6898 len = ntohs(ps14->length);
6899 if (len < sizeof *ps14 || len - sizeof *ps14 > msg->size) {
6900 return OFPERR_OFPBRC_BAD_LEN;
6902 len -= sizeof *ps14;
6903 ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len);
6905 error = ofputil_port_from_ofp11(ps14->port_no, &ops->port_no);
6910 ops->duration_sec = ntohl(ps14->duration_sec);
6911 ops->duration_nsec = ntohl(ps14->duration_nsec);
6912 ops->stats.rx_packets = ntohll(ps14->rx_packets);
6913 ops->stats.tx_packets = ntohll(ps14->tx_packets);
6914 ops->stats.rx_bytes = ntohll(ps14->rx_bytes);
6915 ops->stats.tx_bytes = ntohll(ps14->tx_bytes);
6916 ops->stats.rx_dropped = ntohll(ps14->rx_dropped);
6917 ops->stats.tx_dropped = ntohll(ps14->tx_dropped);
6918 ops->stats.rx_errors = ntohll(ps14->rx_errors);
6919 ops->stats.tx_errors = ntohll(ps14->tx_errors);
6920 ops->stats.rx_frame_errors = UINT64_MAX;
6921 ops->stats.rx_over_errors = UINT64_MAX;
6922 ops->stats.rx_crc_errors = UINT64_MAX;
6923 ops->stats.collisions = UINT64_MAX;
6925 while (properties.size > 0) {
6926 struct ofpbuf payload;
6930 error = ofputil_pull_property(&properties, &payload, &type);
6936 case OFPPSPT14_ETHERNET:
6937 error = parse_ofp14_port_stats_ethernet_property(&payload, ops);
6941 log_property(true, "unknown port stats property %"PRIu16, type);
6954 /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
6957 ofputil_count_port_stats(const struct ofp_header *oh)
6959 struct ofputil_port_stats ps;
6963 ofpbuf_use_const(&b, oh, ntohs(oh->length));
6964 ofpraw_pull_assert(&b);
6965 while (!ofputil_decode_port_stats(&ps, &b)) {
6971 /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
6972 * ofputil_port_stats in 'ps'.
6974 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
6975 * message. Calling this function multiple times for a single 'msg' iterates
6976 * through the replies. The caller must initially leave 'msg''s layer pointers
6977 * null and not modify them between calls.
6979 * Returns 0 if successful, EOF if no replies were left in this 'msg',
6980 * otherwise a positive errno value. */
6982 ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg)
6987 error = (msg->header ? ofpraw_decode(&raw, msg->header)
6988 : ofpraw_pull(&raw, msg));
6995 } else if (raw == OFPRAW_OFPST14_PORT_REPLY) {
6996 return ofputil_pull_ofp14_port_stats(ps, msg);
6997 } else if (raw == OFPRAW_OFPST13_PORT_REPLY) {
6998 const struct ofp13_port_stats *ps13;
7000 ps13 = ofpbuf_try_pull(msg, sizeof *ps13);
7004 return ofputil_port_stats_from_ofp13(ps, ps13);
7005 } else if (raw == OFPRAW_OFPST11_PORT_REPLY) {
7006 const struct ofp11_port_stats *ps11;
7008 ps11 = ofpbuf_try_pull(msg, sizeof *ps11);
7012 return ofputil_port_stats_from_ofp11(ps, ps11);
7013 } else if (raw == OFPRAW_OFPST10_PORT_REPLY) {
7014 const struct ofp10_port_stats *ps10;
7016 ps10 = ofpbuf_try_pull(msg, sizeof *ps10);
7020 return ofputil_port_stats_from_ofp10(ps, ps10);
7026 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %"PRIu32" leftover "
7027 "bytes at end", msg->size);
7028 return OFPERR_OFPBRC_BAD_LEN;
7031 /* Parse a port status request message into a 16 bit OpenFlow 1.0
7032 * port number and stores the latter in '*ofp10_port'.
7033 * Returns 0 if successful, otherwise an OFPERR_* number. */
7035 ofputil_decode_port_stats_request(const struct ofp_header *request,
7036 ofp_port_t *ofp10_port)
7038 switch ((enum ofp_version)request->version) {
7043 case OFP11_VERSION: {
7044 const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request);
7045 return ofputil_port_from_ofp11(psr11->port_no, ofp10_port);
7048 case OFP10_VERSION: {
7049 const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request);
7050 *ofp10_port = u16_to_ofp(ntohs(psr10->port_no));
7059 /* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */
7061 ofputil_bucket_list_destroy(struct ovs_list *buckets)
7063 struct ofputil_bucket *bucket;
7065 LIST_FOR_EACH_POP (bucket, list_node, buckets) {
7066 free(bucket->ofpacts);
7071 /* Clones 'bucket' and its ofpacts data */
7072 static struct ofputil_bucket *
7073 ofputil_bucket_clone_data(const struct ofputil_bucket *bucket)
7075 struct ofputil_bucket *new;
7077 new = xmemdup(bucket, sizeof *bucket);
7078 new->ofpacts = xmemdup(bucket->ofpacts, bucket->ofpacts_len);
7083 /* Clones each of the buckets in the list 'src' appending them
7084 * in turn to 'dest' which should be an initialised list.
7085 * An exception is that if the pointer value of a bucket in 'src'
7086 * matches 'skip' then it is not cloned or appended to 'dest'.
7087 * This allows all of 'src' or 'all of 'src' except 'skip' to
7088 * be cloned and appended to 'dest'. */
7090 ofputil_bucket_clone_list(struct ovs_list *dest, const struct ovs_list *src,
7091 const struct ofputil_bucket *skip)
7093 struct ofputil_bucket *bucket;
7095 LIST_FOR_EACH (bucket, list_node, src) {
7096 struct ofputil_bucket *new_bucket;
7098 if (bucket == skip) {
7102 new_bucket = ofputil_bucket_clone_data(bucket);
7103 list_push_back(dest, &new_bucket->list_node);
7107 /* Find a bucket in the list 'buckets' whose bucket id is 'bucket_id'
7108 * Returns the first bucket found or NULL if no buckets are found. */
7109 struct ofputil_bucket *
7110 ofputil_bucket_find(const struct ovs_list *buckets, uint32_t bucket_id)
7112 struct ofputil_bucket *bucket;
7114 if (bucket_id > OFPG15_BUCKET_MAX) {
7118 LIST_FOR_EACH (bucket, list_node, buckets) {
7119 if (bucket->bucket_id == bucket_id) {
7127 /* Returns true if more than one bucket in the list 'buckets'
7128 * have the same bucket id. Returns false otherwise. */
7130 ofputil_bucket_check_duplicate_id(const struct ovs_list *buckets)
7132 struct ofputil_bucket *i, *j;
7134 LIST_FOR_EACH (i, list_node, buckets) {
7135 LIST_FOR_EACH_REVERSE (j, list_node, buckets) {
7139 if (i->bucket_id == j->bucket_id) {
7148 /* Returns the bucket at the front of the list 'buckets'.
7149 * Undefined if 'buckets is empty. */
7150 struct ofputil_bucket *
7151 ofputil_bucket_list_front(const struct ovs_list *buckets)
7153 static struct ofputil_bucket *bucket;
7155 ASSIGN_CONTAINER(bucket, list_front(buckets), list_node);
7160 /* Returns the bucket at the back of the list 'buckets'.
7161 * Undefined if 'buckets is empty. */
7162 struct ofputil_bucket *
7163 ofputil_bucket_list_back(const struct ovs_list *buckets)
7165 static struct ofputil_bucket *bucket;
7167 ASSIGN_CONTAINER(bucket, list_back(buckets), list_node);
7172 /* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version',
7173 * that requests stats for group 'group_id'. (Use OFPG_ALL to request stats
7176 * Group statistics include packet and byte counts for each group. */
7178 ofputil_encode_group_stats_request(enum ofp_version ofp_version,
7181 struct ofpbuf *request;
7183 switch (ofp_version) {
7185 ovs_fatal(0, "dump-group-stats needs OpenFlow 1.1 or later "
7186 "(\'-O OpenFlow11\')");
7191 case OFP15_VERSION: {
7192 struct ofp11_group_stats_request *req;
7193 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_REQUEST, ofp_version, 0);
7194 req = ofpbuf_put_zeros(request, sizeof *req);
7195 req->group_id = htonl(group_id);
7206 ofputil_uninit_group_desc(struct ofputil_group_desc *gd)
7208 ofputil_bucket_list_destroy(&gd->buckets);
7209 free(&gd->props.fields);
7212 /* Decodes the OpenFlow group description request in 'oh', returning the group
7213 * whose description is requested, or OFPG_ALL if stats for all groups was
7216 ofputil_decode_group_desc_request(const struct ofp_header *oh)
7218 struct ofpbuf request;
7221 ofpbuf_use_const(&request, oh, ntohs(oh->length));
7222 raw = ofpraw_pull_assert(&request);
7223 if (raw == OFPRAW_OFPST11_GROUP_DESC_REQUEST) {
7225 } else if (raw == OFPRAW_OFPST15_GROUP_DESC_REQUEST) {
7226 ovs_be32 *group_id = ofpbuf_pull(&request, sizeof *group_id);
7227 return ntohl(*group_id);
7233 /* Returns an OpenFlow group description request for OpenFlow version
7234 * 'ofp_version', that requests stats for group 'group_id'. Use OFPG_ALL to
7235 * request stats for all groups (OpenFlow 1.4 and earlier always request all
7238 * Group descriptions include the bucket and action configuration for each
7241 ofputil_encode_group_desc_request(enum ofp_version ofp_version,
7244 struct ofpbuf *request;
7246 switch (ofp_version) {
7248 ovs_fatal(0, "dump-groups needs OpenFlow 1.1 or later "
7249 "(\'-O OpenFlow11\')");
7254 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_DESC_REQUEST,
7257 case OFP15_VERSION:{
7258 struct ofp15_group_desc_request *req;
7259 request = ofpraw_alloc(OFPRAW_OFPST15_GROUP_DESC_REQUEST,
7261 req = ofpbuf_put_zeros(request, sizeof *req);
7262 req->group_id = htonl(group_id);
7273 ofputil_group_bucket_counters_to_ofp11(const struct ofputil_group_stats *gs,
7274 struct ofp11_bucket_counter bucket_cnts[])
7278 for (i = 0; i < gs->n_buckets; i++) {
7279 bucket_cnts[i].packet_count = htonll(gs->bucket_stats[i].packet_count);
7280 bucket_cnts[i].byte_count = htonll(gs->bucket_stats[i].byte_count);
7285 ofputil_group_stats_to_ofp11(const struct ofputil_group_stats *gs,
7286 struct ofp11_group_stats *gs11, size_t length,
7287 struct ofp11_bucket_counter bucket_cnts[])
7289 memset(gs11, 0, sizeof *gs11);
7290 gs11->length = htons(length);
7291 gs11->group_id = htonl(gs->group_id);
7292 gs11->ref_count = htonl(gs->ref_count);
7293 gs11->packet_count = htonll(gs->packet_count);
7294 gs11->byte_count = htonll(gs->byte_count);
7295 ofputil_group_bucket_counters_to_ofp11(gs, bucket_cnts);
7299 ofputil_group_stats_to_ofp13(const struct ofputil_group_stats *gs,
7300 struct ofp13_group_stats *gs13, size_t length,
7301 struct ofp11_bucket_counter bucket_cnts[])
7303 ofputil_group_stats_to_ofp11(gs, &gs13->gs, length, bucket_cnts);
7304 gs13->duration_sec = htonl(gs->duration_sec);
7305 gs13->duration_nsec = htonl(gs->duration_nsec);
7309 /* Encodes 'gs' properly for the format of the list of group statistics
7310 * replies already begun in 'replies' and appends it to the list. 'replies'
7311 * must have originally been initialized with ofpmp_init(). */
7313 ofputil_append_group_stats(struct ovs_list *replies,
7314 const struct ofputil_group_stats *gs)
7316 size_t bucket_counter_size;
7317 struct ofp11_bucket_counter *bucket_counters;
7320 bucket_counter_size = gs->n_buckets * sizeof(struct ofp11_bucket_counter);
7322 switch (ofpmp_version(replies)) {
7324 case OFP12_VERSION:{
7325 struct ofp11_group_stats *gs11;
7327 length = sizeof *gs11 + bucket_counter_size;
7328 gs11 = ofpmp_append(replies, length);
7329 bucket_counters = (struct ofp11_bucket_counter *)(gs11 + 1);
7330 ofputil_group_stats_to_ofp11(gs, gs11, length, bucket_counters);
7336 case OFP15_VERSION: {
7337 struct ofp13_group_stats *gs13;
7339 length = sizeof *gs13 + bucket_counter_size;
7340 gs13 = ofpmp_append(replies, length);
7341 bucket_counters = (struct ofp11_bucket_counter *)(gs13 + 1);
7342 ofputil_group_stats_to_ofp13(gs, gs13, length, bucket_counters);
7351 /* Returns an OpenFlow group features request for OpenFlow version
7354 ofputil_encode_group_features_request(enum ofp_version ofp_version)
7356 struct ofpbuf *request = NULL;
7358 switch (ofp_version) {
7361 ovs_fatal(0, "dump-group-features needs OpenFlow 1.2 or later "
7362 "(\'-O OpenFlow12\')");
7367 request = ofpraw_alloc(OFPRAW_OFPST12_GROUP_FEATURES_REQUEST,
7377 /* Returns a OpenFlow message that encodes 'features' properly as a reply to
7378 * group features request 'request'. */
7380 ofputil_encode_group_features_reply(
7381 const struct ofputil_group_features *features,
7382 const struct ofp_header *request)
7384 struct ofp12_group_features_stats *ogf;
7385 struct ofpbuf *reply;
7388 reply = ofpraw_alloc_xid(OFPRAW_OFPST12_GROUP_FEATURES_REPLY,
7389 request->version, request->xid, 0);
7390 ogf = ofpbuf_put_zeros(reply, sizeof *ogf);
7391 ogf->types = htonl(features->types);
7392 ogf->capabilities = htonl(features->capabilities);
7393 for (i = 0; i < OFPGT12_N_TYPES; i++) {
7394 ogf->max_groups[i] = htonl(features->max_groups[i]);
7395 ogf->actions[i] = ofpact_bitmap_to_openflow(features->ofpacts[i],
7402 /* Decodes group features reply 'oh' into 'features'. */
7404 ofputil_decode_group_features_reply(const struct ofp_header *oh,
7405 struct ofputil_group_features *features)
7407 const struct ofp12_group_features_stats *ogf = ofpmsg_body(oh);
7410 features->types = ntohl(ogf->types);
7411 features->capabilities = ntohl(ogf->capabilities);
7412 for (i = 0; i < OFPGT12_N_TYPES; i++) {
7413 features->max_groups[i] = ntohl(ogf->max_groups[i]);
7414 features->ofpacts[i] = ofpact_bitmap_from_openflow(
7415 ogf->actions[i], oh->version);
7419 /* Parse a group status request message into a 32 bit OpenFlow 1.1
7420 * group ID and stores the latter in '*group_id'.
7421 * Returns 0 if successful, otherwise an OFPERR_* number. */
7423 ofputil_decode_group_stats_request(const struct ofp_header *request,
7426 const struct ofp11_group_stats_request *gsr11 = ofpmsg_body(request);
7427 *group_id = ntohl(gsr11->group_id);
7431 /* Converts a group stats reply in 'msg' into an abstract ofputil_group_stats
7432 * in 'gs'. Assigns freshly allocated memory to gs->bucket_stats for the
7433 * caller to eventually free.
7435 * Multiple group stats replies can be packed into a single OpenFlow message.
7436 * Calling this function multiple times for a single 'msg' iterates through the
7437 * replies. The caller must initially leave 'msg''s layer pointers null and
7438 * not modify them between calls.
7440 * Returns 0 if successful, EOF if no replies were left in this 'msg',
7441 * otherwise a positive errno value. */
7443 ofputil_decode_group_stats_reply(struct ofpbuf *msg,
7444 struct ofputil_group_stats *gs)
7446 struct ofp11_bucket_counter *obc;
7447 struct ofp11_group_stats *ogs11;
7454 gs->bucket_stats = NULL;
7455 error = (msg->header ? ofpraw_decode(&raw, msg->header)
7456 : ofpraw_pull(&raw, msg));
7465 if (raw == OFPRAW_OFPST11_GROUP_REPLY) {
7466 base_len = sizeof *ogs11;
7467 ogs11 = ofpbuf_try_pull(msg, sizeof *ogs11);
7468 gs->duration_sec = gs->duration_nsec = UINT32_MAX;
7469 } else if (raw == OFPRAW_OFPST13_GROUP_REPLY) {
7470 struct ofp13_group_stats *ogs13;
7472 base_len = sizeof *ogs13;
7473 ogs13 = ofpbuf_try_pull(msg, sizeof *ogs13);
7476 gs->duration_sec = ntohl(ogs13->duration_sec);
7477 gs->duration_nsec = ntohl(ogs13->duration_nsec);
7486 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
7487 ofpraw_get_name(raw), msg->size);
7488 return OFPERR_OFPBRC_BAD_LEN;
7490 length = ntohs(ogs11->length);
7491 if (length < sizeof base_len) {
7492 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply claims invalid length %"PRIuSIZE,
7493 ofpraw_get_name(raw), length);
7494 return OFPERR_OFPBRC_BAD_LEN;
7497 gs->group_id = ntohl(ogs11->group_id);
7498 gs->ref_count = ntohl(ogs11->ref_count);
7499 gs->packet_count = ntohll(ogs11->packet_count);
7500 gs->byte_count = ntohll(ogs11->byte_count);
7502 gs->n_buckets = (length - base_len) / sizeof *obc;
7503 obc = ofpbuf_try_pull(msg, gs->n_buckets * sizeof *obc);
7505 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
7506 ofpraw_get_name(raw), msg->size);
7507 return OFPERR_OFPBRC_BAD_LEN;
7510 gs->bucket_stats = xmalloc(gs->n_buckets * sizeof *gs->bucket_stats);
7511 for (i = 0; i < gs->n_buckets; i++) {
7512 gs->bucket_stats[i].packet_count = ntohll(obc[i].packet_count);
7513 gs->bucket_stats[i].byte_count = ntohll(obc[i].byte_count);
7520 ofputil_put_ofp11_bucket(const struct ofputil_bucket *bucket,
7521 struct ofpbuf *openflow, enum ofp_version ofp_version)
7523 struct ofp11_bucket *ob;
7526 start = openflow->size;
7527 ofpbuf_put_zeros(openflow, sizeof *ob);
7528 ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
7529 openflow, ofp_version);
7530 ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
7531 ob->len = htons(openflow->size - start);
7532 ob->weight = htons(bucket->weight);
7533 ob->watch_port = ofputil_port_to_ofp11(bucket->watch_port);
7534 ob->watch_group = htonl(bucket->watch_group);
7538 ofputil_put_ofp15_group_bucket_prop_weight(ovs_be16 weight,
7539 struct ofpbuf *openflow)
7542 struct ofp15_group_bucket_prop_weight *prop;
7544 start_ofs = start_property(openflow, OFPGBPT15_WEIGHT);
7545 ofpbuf_put_zeros(openflow, sizeof *prop - sizeof(struct ofp_prop_header));
7546 prop = ofpbuf_at_assert(openflow, start_ofs, sizeof *prop);
7547 prop->weight = weight;
7548 end_property(openflow, start_ofs);
7552 ofputil_put_ofp15_group_bucket_prop_watch(ovs_be32 watch, uint16_t type,
7553 struct ofpbuf *openflow)
7556 struct ofp15_group_bucket_prop_watch *prop;
7558 start_ofs = start_property(openflow, type);
7559 ofpbuf_put_zeros(openflow, sizeof *prop - sizeof(struct ofp_prop_header));
7560 prop = ofpbuf_at_assert(openflow, start_ofs, sizeof *prop);
7561 prop->watch = watch;
7562 end_property(openflow, start_ofs);
7566 ofputil_put_ofp15_bucket(const struct ofputil_bucket *bucket,
7567 uint32_t bucket_id, enum ofp11_group_type group_type,
7568 struct ofpbuf *openflow, enum ofp_version ofp_version)
7570 struct ofp15_bucket *ob;
7571 size_t start, actions_start, actions_len;
7573 start = openflow->size;
7574 ofpbuf_put_zeros(openflow, sizeof *ob);
7576 actions_start = openflow->size;
7577 ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
7578 openflow, ofp_version);
7579 actions_len = openflow->size - actions_start;
7581 if (group_type == OFPGT11_SELECT) {
7582 ofputil_put_ofp15_group_bucket_prop_weight(htons(bucket->weight),
7585 if (bucket->watch_port != OFPP_ANY) {
7586 ovs_be32 port = ofputil_port_to_ofp11(bucket->watch_port);
7587 ofputil_put_ofp15_group_bucket_prop_watch(port,
7588 OFPGBPT15_WATCH_PORT,
7591 if (bucket->watch_group != OFPG_ANY) {
7592 ovs_be32 group = htonl(bucket->watch_group);
7593 ofputil_put_ofp15_group_bucket_prop_watch(group,
7594 OFPGBPT15_WATCH_GROUP,
7598 ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
7599 ob->len = htons(openflow->size - start);
7600 ob->action_array_len = htons(actions_len);
7601 ob->bucket_id = htonl(bucket_id);
7605 ofputil_put_group_prop_ntr_selection_method(enum ofp_version ofp_version,
7606 const struct ofputil_group_props *gp,
7607 struct ofpbuf *openflow)
7609 struct ntr_group_prop_selection_method *prop;
7612 start = openflow->size;
7613 ofpbuf_put_zeros(openflow, sizeof *prop);
7614 oxm_put_field_array(openflow, &gp->fields, ofp_version);
7615 prop = ofpbuf_at_assert(openflow, start, sizeof *prop);
7616 prop->type = htons(OFPGPT15_EXPERIMENTER);
7617 prop->experimenter = htonl(NTR_VENDOR_ID);
7618 prop->exp_type = htonl(NTRT_SELECTION_METHOD);
7619 strcpy(prop->selection_method, gp->selection_method);
7620 prop->selection_method_param = htonll(gp->selection_method_param);
7621 end_property(openflow, start);
7625 ofputil_append_ofp11_group_desc_reply(const struct ofputil_group_desc *gds,
7626 const struct ovs_list *buckets,
7627 struct ovs_list *replies,
7628 enum ofp_version version)
7630 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
7631 struct ofp11_group_desc_stats *ogds;
7632 struct ofputil_bucket *bucket;
7635 start_ogds = reply->size;
7636 ofpbuf_put_zeros(reply, sizeof *ogds);
7637 LIST_FOR_EACH (bucket, list_node, buckets) {
7638 ofputil_put_ofp11_bucket(bucket, reply, version);
7640 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
7641 ogds->length = htons(reply->size - start_ogds);
7642 ogds->type = gds->type;
7643 ogds->group_id = htonl(gds->group_id);
7645 ofpmp_postappend(replies, start_ogds);
7649 ofputil_append_ofp15_group_desc_reply(const struct ofputil_group_desc *gds,
7650 const struct ovs_list *buckets,
7651 struct ovs_list *replies,
7652 enum ofp_version version)
7654 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
7655 struct ofp15_group_desc_stats *ogds;
7656 struct ofputil_bucket *bucket;
7657 size_t start_ogds, start_buckets;
7659 start_ogds = reply->size;
7660 ofpbuf_put_zeros(reply, sizeof *ogds);
7661 start_buckets = reply->size;
7662 LIST_FOR_EACH (bucket, list_node, buckets) {
7663 ofputil_put_ofp15_bucket(bucket, bucket->bucket_id,
7664 gds->type, reply, version);
7666 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
7667 ogds->length = htons(reply->size - start_ogds);
7668 ogds->type = gds->type;
7669 ogds->group_id = htonl(gds->group_id);
7670 ogds->bucket_list_len = htons(reply->size - start_buckets);
7672 /* Add group properties */
7673 if (gds->props.selection_method[0]) {
7674 ofputil_put_group_prop_ntr_selection_method(version, &gds->props,
7678 ofpmp_postappend(replies, start_ogds);
7681 /* Appends a group stats reply that contains the data in 'gds' to those already
7682 * present in the list of ofpbufs in 'replies'. 'replies' should have been
7683 * initialized with ofpmp_init(). */
7685 ofputil_append_group_desc_reply(const struct ofputil_group_desc *gds,
7686 const struct ovs_list *buckets,
7687 struct ovs_list *replies)
7689 enum ofp_version version = ofpmp_version(replies);
7697 ofputil_append_ofp11_group_desc_reply(gds, buckets, replies, version);
7701 ofputil_append_ofp15_group_desc_reply(gds, buckets, replies, version);
7711 ofputil_pull_ofp11_buckets(struct ofpbuf *msg, size_t buckets_length,
7712 enum ofp_version version, struct ovs_list *buckets)
7714 struct ofp11_bucket *ob;
7715 uint32_t bucket_id = 0;
7718 while (buckets_length > 0) {
7719 struct ofputil_bucket *bucket;
7720 struct ofpbuf ofpacts;
7724 ob = (buckets_length >= sizeof *ob
7725 ? ofpbuf_try_pull(msg, sizeof *ob)
7728 VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE" leftover bytes",
7730 return OFPERR_OFPGMFC_BAD_BUCKET;
7733 ob_len = ntohs(ob->len);
7734 if (ob_len < sizeof *ob) {
7735 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
7736 "%"PRIuSIZE" is not valid", ob_len);
7737 return OFPERR_OFPGMFC_BAD_BUCKET;
7738 } else if (ob_len > buckets_length) {
7739 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
7740 "%"PRIuSIZE" exceeds remaining buckets data size %"PRIuSIZE,
7741 ob_len, buckets_length);
7742 return OFPERR_OFPGMFC_BAD_BUCKET;
7744 buckets_length -= ob_len;
7746 ofpbuf_init(&ofpacts, 0);
7747 error = ofpacts_pull_openflow_actions(msg, ob_len - sizeof *ob,
7750 ofpbuf_uninit(&ofpacts);
7751 ofputil_bucket_list_destroy(buckets);
7755 bucket = xzalloc(sizeof *bucket);
7756 bucket->weight = ntohs(ob->weight);
7757 error = ofputil_port_from_ofp11(ob->watch_port, &bucket->watch_port);
7759 ofpbuf_uninit(&ofpacts);
7760 ofputil_bucket_list_destroy(buckets);
7761 return OFPERR_OFPGMFC_BAD_WATCH;
7763 bucket->watch_group = ntohl(ob->watch_group);
7764 bucket->bucket_id = bucket_id++;
7766 bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
7767 bucket->ofpacts_len = ofpacts.size;
7768 list_push_back(buckets, &bucket->list_node);
7775 parse_ofp15_group_bucket_prop_weight(const struct ofpbuf *payload,
7778 struct ofp15_group_bucket_prop_weight *prop = payload->data;
7780 if (payload->size != sizeof *prop) {
7781 log_property(false, "OpenFlow bucket weight property length "
7782 "%u is not valid", payload->size);
7783 return OFPERR_OFPBPC_BAD_LEN;
7786 *weight = prop->weight;
7792 parse_ofp15_group_bucket_prop_watch(const struct ofpbuf *payload,
7795 struct ofp15_group_bucket_prop_watch *prop = payload->data;
7797 if (payload->size != sizeof *prop) {
7798 log_property(false, "OpenFlow bucket watch port or group "
7799 "property length %u is not valid", payload->size);
7800 return OFPERR_OFPBPC_BAD_LEN;
7803 *watch = prop->watch;
7809 ofputil_pull_ofp15_buckets(struct ofpbuf *msg, size_t buckets_length,
7810 enum ofp_version version, uint8_t group_type,
7811 struct ovs_list *buckets)
7813 struct ofp15_bucket *ob;
7816 while (buckets_length > 0) {
7817 struct ofputil_bucket *bucket = NULL;
7818 struct ofpbuf ofpacts;
7819 enum ofperr err = OFPERR_OFPGMFC_BAD_BUCKET;
7820 struct ofpbuf properties;
7821 size_t ob_len, actions_len, properties_len;
7822 ovs_be32 watch_port = ofputil_port_to_ofp11(OFPP_ANY);
7823 ovs_be32 watch_group = htonl(OFPG_ANY);
7824 ovs_be16 weight = htons(group_type == OFPGT11_SELECT ? 1 : 0);
7826 ofpbuf_init(&ofpacts, 0);
7828 ob = ofpbuf_try_pull(msg, sizeof *ob);
7830 VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE
7831 " leftover bytes", buckets_length);
7835 ob_len = ntohs(ob->len);
7836 actions_len = ntohs(ob->action_array_len);
7838 if (ob_len < sizeof *ob) {
7839 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
7840 "%"PRIuSIZE" is not valid", ob_len);
7842 } else if (ob_len > buckets_length) {
7843 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
7844 "%"PRIuSIZE" exceeds remaining buckets data size %"
7845 PRIuSIZE, ob_len, buckets_length);
7847 } else if (actions_len > ob_len - sizeof *ob) {
7848 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket actions "
7849 "length %"PRIuSIZE" exceeds remaining bucket "
7850 "data size %"PRIuSIZE, actions_len,
7851 ob_len - sizeof *ob);
7854 buckets_length -= ob_len;
7856 err = ofpacts_pull_openflow_actions(msg, actions_len, version,
7862 properties_len = ob_len - sizeof *ob - actions_len;
7863 ofpbuf_use_const(&properties, ofpbuf_pull(msg, properties_len),
7866 while (properties.size > 0) {
7867 struct ofpbuf payload;
7870 err = ofputil_pull_property(&properties, &payload, &type);
7876 case OFPGBPT15_WEIGHT:
7877 err = parse_ofp15_group_bucket_prop_weight(&payload, &weight);
7880 case OFPGBPT15_WATCH_PORT:
7881 err = parse_ofp15_group_bucket_prop_watch(&payload,
7885 case OFPGBPT15_WATCH_GROUP:
7886 err = parse_ofp15_group_bucket_prop_watch(&payload,
7891 log_property(false, "unknown group bucket property %"PRIu16,
7893 err = OFPERR_OFPBPC_BAD_TYPE;
7902 bucket = xzalloc(sizeof *bucket);
7904 bucket->weight = ntohs(weight);
7905 err = ofputil_port_from_ofp11(watch_port, &bucket->watch_port);
7907 err = OFPERR_OFPGMFC_BAD_WATCH;
7910 bucket->watch_group = ntohl(watch_group);
7911 bucket->bucket_id = ntohl(ob->bucket_id);
7912 if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
7913 VLOG_WARN_RL(&bad_ofmsg_rl, "bucket id (%u) is out of range",
7915 err = OFPERR_OFPGMFC_BAD_BUCKET;
7919 bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
7920 bucket->ofpacts_len = ofpacts.size;
7921 list_push_back(buckets, &bucket->list_node);
7927 ofpbuf_uninit(&ofpacts);
7928 ofputil_bucket_list_destroy(buckets);
7932 if (ofputil_bucket_check_duplicate_id(buckets)) {
7933 VLOG_WARN_RL(&bad_ofmsg_rl, "Duplicate bucket id");
7934 ofputil_bucket_list_destroy(buckets);
7935 return OFPERR_OFPGMFC_BAD_BUCKET;
7942 ofputil_init_group_properties(struct ofputil_group_props *gp)
7944 memset(gp, 0, sizeof *gp);
7948 parse_group_prop_ntr_selection_method(struct ofpbuf *payload,
7949 enum ofp11_group_type group_type,
7950 enum ofp15_group_mod_command group_cmd,
7951 struct ofputil_group_props *gp)
7953 struct ntr_group_prop_selection_method *prop = payload->data;
7954 size_t fields_len, method_len;
7957 switch (group_type) {
7958 case OFPGT11_SELECT:
7961 case OFPGT11_INDIRECT:
7963 log_property(false, "ntr selection method property is only allowed "
7964 "for select groups");
7965 return OFPERR_OFPBPC_BAD_VALUE;
7970 switch (group_cmd) {
7972 case OFPGC15_MODIFY:
7974 case OFPGC15_DELETE:
7975 case OFPGC15_INSERT_BUCKET:
7976 case OFPGC15_REMOVE_BUCKET:
7977 log_property(false, "ntr selection method property is only allowed "
7978 "for add and delete group modifications");
7979 return OFPERR_OFPBPC_BAD_VALUE;
7984 if (payload->size < sizeof *prop) {
7985 log_property(false, "ntr selection method property length "
7986 "%u is not valid", payload->size);
7987 return OFPERR_OFPBPC_BAD_LEN;
7990 method_len = strnlen(prop->selection_method, NTR_MAX_SELECTION_METHOD_LEN);
7992 if (method_len == NTR_MAX_SELECTION_METHOD_LEN) {
7993 log_property(false, "ntr selection method is not null terminated");
7994 return OFPERR_OFPBPC_BAD_VALUE;
7997 if (strcmp("hash", prop->selection_method)) {
7998 log_property(false, "ntr selection method '%s' is not supported",
7999 prop->selection_method);
8000 return OFPERR_OFPBPC_BAD_VALUE;
8003 strcpy(gp->selection_method, prop->selection_method);
8004 gp->selection_method_param = ntohll(prop->selection_method_param);
8006 if (!method_len && gp->selection_method_param) {
8007 log_property(false, "ntr selection method parameter is non-zero but "
8008 "selection method is empty");
8009 return OFPERR_OFPBPC_BAD_VALUE;
8012 ofpbuf_pull(payload, sizeof *prop);
8014 fields_len = ntohs(prop->length) - sizeof *prop;
8015 if (!method_len && fields_len) {
8016 log_property(false, "ntr selection method parameter is zero "
8017 "but fields are provided");
8018 return OFPERR_OFPBPC_BAD_VALUE;
8021 error = oxm_pull_field_array(payload->data, fields_len,
8024 log_property(false, "ntr selection method fields are invalid");
8032 parse_group_prop_ntr(struct ofpbuf *payload, uint32_t exp_type,
8033 enum ofp11_group_type group_type,
8034 enum ofp15_group_mod_command group_cmd,
8035 struct ofputil_group_props *gp)
8040 case NTRT_SELECTION_METHOD:
8041 error = parse_group_prop_ntr_selection_method(payload, group_type,
8046 log_property(false, "unknown group property ntr experimenter type "
8047 "%"PRIu32, exp_type);
8048 error = OFPERR_OFPBPC_BAD_TYPE;
8056 parse_ofp15_group_prop_exp(struct ofpbuf *payload,
8057 enum ofp11_group_type group_type,
8058 enum ofp15_group_mod_command group_cmd,
8059 struct ofputil_group_props *gp)
8061 struct ofp_prop_experimenter *prop = payload->data;
8062 uint16_t experimenter;
8066 if (payload->size < sizeof *prop) {
8067 return OFPERR_OFPBPC_BAD_LEN;
8070 experimenter = ntohl(prop->experimenter);
8071 exp_type = ntohl(prop->exp_type);
8073 switch (experimenter) {
8075 error = parse_group_prop_ntr(payload, exp_type, group_type,
8080 log_property(false, "unknown group property experimenter %"PRIu16,
8082 error = OFPERR_OFPBPC_BAD_EXPERIMENTER;
8090 parse_ofp15_group_properties(struct ofpbuf *msg,
8091 enum ofp11_group_type group_type,
8092 enum ofp15_group_mod_command group_cmd,
8093 struct ofputil_group_props *gp,
8094 size_t properties_len)
8096 struct ofpbuf properties;
8098 ofpbuf_use_const(&properties, ofpbuf_pull(msg, properties_len),
8101 while (properties.size > 0) {
8102 struct ofpbuf payload;
8106 error = ofputil_pull_property(&properties, &payload, &type);
8112 case OFPGPT15_EXPERIMENTER:
8113 error = parse_ofp15_group_prop_exp(&payload, group_type,
8118 log_property(false, "unknown group property %"PRIu16, type);
8119 error = OFPERR_OFPBPC_BAD_TYPE;
8132 ofputil_decode_ofp11_group_desc_reply(struct ofputil_group_desc *gd,
8134 enum ofp_version version)
8136 struct ofp11_group_desc_stats *ogds;
8140 ofpraw_pull_assert(msg);
8147 ogds = ofpbuf_try_pull(msg, sizeof *ogds);
8149 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
8150 "leftover bytes at end", msg->size);
8151 return OFPERR_OFPBRC_BAD_LEN;
8153 gd->type = ogds->type;
8154 gd->group_id = ntohl(ogds->group_id);
8156 length = ntohs(ogds->length);
8157 if (length < sizeof *ogds || length - sizeof *ogds > msg->size) {
8158 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
8159 "length %"PRIuSIZE, length);
8160 return OFPERR_OFPBRC_BAD_LEN;
8163 return ofputil_pull_ofp11_buckets(msg, length - sizeof *ogds, version,
8168 ofputil_decode_ofp15_group_desc_reply(struct ofputil_group_desc *gd,
8170 enum ofp_version version)
8172 struct ofp15_group_desc_stats *ogds;
8173 uint16_t length, bucket_list_len;
8177 ofpraw_pull_assert(msg);
8184 ogds = ofpbuf_try_pull(msg, sizeof *ogds);
8186 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
8187 "leftover bytes at end", msg->size);
8188 return OFPERR_OFPBRC_BAD_LEN;
8190 gd->type = ogds->type;
8191 gd->group_id = ntohl(ogds->group_id);
8193 length = ntohs(ogds->length);
8194 if (length < sizeof *ogds || length - sizeof *ogds > msg->size) {
8195 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
8196 "length %u", length);
8197 return OFPERR_OFPBRC_BAD_LEN;
8200 bucket_list_len = ntohs(ogds->bucket_list_len);
8201 if (length < bucket_list_len + sizeof *ogds) {
8202 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
8203 "bucket list length %u", bucket_list_len);
8204 return OFPERR_OFPBRC_BAD_LEN;
8206 error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, version, gd->type,
8212 /* By definition group desc messages don't have a group mod command.
8213 * However, parse_group_prop_ntr_selection_method() checks to make sure
8214 * that the command is OFPGC15_ADD or OFPGC15_DELETE to guard
8215 * against group mod messages with other commands supplying
8216 * a NTR selection method group experimenter property.
8217 * Such properties are valid for group desc replies so
8218 * claim that the group mod command is OFPGC15_ADD to
8219 * satisfy the check in parse_group_prop_ntr_selection_method() */
8220 return parse_ofp15_group_properties(msg, gd->type, OFPGC15_ADD, &gd->props,
8224 /* Converts a group description reply in 'msg' into an abstract
8225 * ofputil_group_desc in 'gd'.
8227 * Multiple group description replies can be packed into a single OpenFlow
8228 * message. Calling this function multiple times for a single 'msg' iterates
8229 * through the replies. The caller must initially leave 'msg''s layer pointers
8230 * null and not modify them between calls.
8232 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8233 * otherwise a positive errno value. */
8235 ofputil_decode_group_desc_reply(struct ofputil_group_desc *gd,
8236 struct ofpbuf *msg, enum ofp_version version)
8238 ofputil_init_group_properties(&gd->props);
8246 return ofputil_decode_ofp11_group_desc_reply(gd, msg, version);
8249 return ofputil_decode_ofp15_group_desc_reply(gd, msg, version);
8258 ofputil_uninit_group_mod(struct ofputil_group_mod *gm)
8260 ofputil_bucket_list_destroy(&gm->buckets);
8263 static struct ofpbuf *
8264 ofputil_encode_ofp11_group_mod(enum ofp_version ofp_version,
8265 const struct ofputil_group_mod *gm)
8268 struct ofp11_group_mod *ogm;
8270 struct ofputil_bucket *bucket;
8272 b = ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD, ofp_version, 0);
8273 start_ogm = b->size;
8274 ofpbuf_put_zeros(b, sizeof *ogm);
8276 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
8277 ofputil_put_ofp11_bucket(bucket, b, ofp_version);
8279 ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
8280 ogm->command = htons(gm->command);
8281 ogm->type = gm->type;
8282 ogm->group_id = htonl(gm->group_id);
8287 static struct ofpbuf *
8288 ofputil_encode_ofp15_group_mod(enum ofp_version ofp_version,
8289 const struct ofputil_group_mod *gm)
8292 struct ofp15_group_mod *ogm;
8294 struct ofputil_bucket *bucket;
8295 struct id_pool *bucket_ids = NULL;
8297 b = ofpraw_alloc(OFPRAW_OFPT15_GROUP_MOD, ofp_version, 0);
8298 start_ogm = b->size;
8299 ofpbuf_put_zeros(b, sizeof *ogm);
8301 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
8304 /* Generate a bucket id if none was supplied */
8305 if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
8307 const struct ofputil_bucket *bkt;
8309 bucket_ids = id_pool_create(0, OFPG15_BUCKET_MAX + 1);
8311 /* Mark all bucket_ids that are present in gm
8312 * as used in the pool. */
8313 LIST_FOR_EACH_REVERSE (bkt, list_node, &gm->buckets) {
8314 if (bkt == bucket) {
8317 if (bkt->bucket_id <= OFPG15_BUCKET_MAX) {
8318 id_pool_add(bucket_ids, bkt->bucket_id);
8323 if (!id_pool_alloc_id(bucket_ids, &bucket_id)) {
8327 bucket_id = bucket->bucket_id;
8330 ofputil_put_ofp15_bucket(bucket, bucket_id, gm->type, b, ofp_version);
8332 ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
8333 ogm->command = htons(gm->command);
8334 ogm->type = gm->type;
8335 ogm->group_id = htonl(gm->group_id);
8336 ogm->command_bucket_id = htonl(gm->command_bucket_id);
8337 ogm->bucket_array_len = htons(b->size - start_ogm - sizeof *ogm);
8339 /* Add group properties */
8340 if (gm->props.selection_method[0]) {
8341 ofputil_put_group_prop_ntr_selection_method(ofp_version, &gm->props, b);
8344 id_pool_destroy(bucket_ids);
8349 bad_group_cmd(enum ofp15_group_mod_command cmd)
8351 const char *opt_version;
8352 const char *version;
8353 const char *cmd_str;
8357 case OFPGC15_MODIFY:
8358 case OFPGC15_DELETE:
8363 case OFPGC15_INSERT_BUCKET:
8364 case OFPGC15_REMOVE_BUCKET:
8375 cmd_str = "add-group";
8378 case OFPGC15_MODIFY:
8379 cmd_str = "mod-group";
8382 case OFPGC15_DELETE:
8383 cmd_str = "del-group";
8386 case OFPGC15_INSERT_BUCKET:
8387 cmd_str = "insert-bucket";
8390 case OFPGC15_REMOVE_BUCKET:
8391 cmd_str = "remove-bucket";
8398 ovs_fatal(0, "%s needs OpenFlow %s or later (\'-O OpenFlow%s\')",
8399 cmd_str, version, opt_version);
8403 /* Converts abstract group mod 'gm' into a message for OpenFlow version
8404 * 'ofp_version' and returns the message. */
8406 ofputil_encode_group_mod(enum ofp_version ofp_version,
8407 const struct ofputil_group_mod *gm)
8410 switch (ofp_version) {
8412 bad_group_cmd(gm->command);
8418 if (gm->command > OFPGC11_DELETE) {
8419 bad_group_cmd(gm->command);
8421 return ofputil_encode_ofp11_group_mod(ofp_version, gm);
8424 return ofputil_encode_ofp15_group_mod(ofp_version, gm);
8432 ofputil_pull_ofp11_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
8433 struct ofputil_group_mod *gm)
8435 const struct ofp11_group_mod *ogm;
8438 ogm = ofpbuf_pull(msg, sizeof *ogm);
8439 gm->command = ntohs(ogm->command);
8440 gm->type = ogm->type;
8441 gm->group_id = ntohl(ogm->group_id);
8442 gm->command_bucket_id = OFPG15_BUCKET_ALL;
8444 error = ofputil_pull_ofp11_buckets(msg, msg->size, ofp_version,
8447 /* OF1.3.5+ prescribes an error when an OFPGC_DELETE includes buckets. */
8449 && ofp_version >= OFP13_VERSION
8450 && gm->command == OFPGC11_DELETE
8451 && !list_is_empty(&gm->buckets)) {
8452 error = OFPERR_OFPGMFC_INVALID_GROUP;
8459 ofputil_pull_ofp15_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
8460 struct ofputil_group_mod *gm)
8462 const struct ofp15_group_mod *ogm;
8463 uint16_t bucket_list_len;
8464 enum ofperr error = OFPERR_OFPGMFC_BAD_BUCKET;
8466 ogm = ofpbuf_pull(msg, sizeof *ogm);
8467 gm->command = ntohs(ogm->command);
8468 gm->type = ogm->type;
8469 gm->group_id = ntohl(ogm->group_id);
8471 gm->command_bucket_id = ntohl(ogm->command_bucket_id);
8472 switch (gm->command) {
8473 case OFPGC15_REMOVE_BUCKET:
8474 if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
8478 case OFPGC15_INSERT_BUCKET:
8479 if (gm->command_bucket_id <= OFPG15_BUCKET_MAX ||
8480 gm->command_bucket_id == OFPG15_BUCKET_FIRST
8481 || gm->command_bucket_id == OFPG15_BUCKET_LAST) {
8487 case OFPGC11_MODIFY:
8488 case OFPGC11_DELETE:
8490 if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
8496 VLOG_WARN_RL(&bad_ofmsg_rl,
8497 "group command bucket id (%u) is out of range",
8498 gm->command_bucket_id);
8499 return OFPERR_OFPGMFC_BAD_BUCKET;
8502 bucket_list_len = ntohs(ogm->bucket_array_len);
8503 error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, ofp_version,
8504 gm->type, &gm->buckets);
8509 return parse_ofp15_group_properties(msg, gm->type, gm->command, &gm->props,
8513 /* Converts OpenFlow group mod message 'oh' into an abstract group mod in
8514 * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */
8516 ofputil_decode_group_mod(const struct ofp_header *oh,
8517 struct ofputil_group_mod *gm)
8519 enum ofp_version ofp_version = oh->version;
8521 struct ofputil_bucket *bucket;
8524 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
8525 ofpraw_pull_assert(&msg);
8527 ofputil_init_group_properties(&gm->props);
8529 switch (ofp_version)
8535 err = ofputil_pull_ofp11_group_mod(&msg, ofp_version, gm);
8539 err = ofputil_pull_ofp15_group_mod(&msg, ofp_version, gm);
8552 case OFPGT11_INDIRECT:
8553 if (!list_is_singleton(&gm->buckets)) {
8554 return OFPERR_OFPGMFC_INVALID_GROUP;
8558 case OFPGT11_SELECT:
8565 switch (gm->command) {
8567 case OFPGC11_MODIFY:
8568 case OFPGC11_DELETE:
8569 case OFPGC15_INSERT_BUCKET:
8571 case OFPGC15_REMOVE_BUCKET:
8572 if (!list_is_empty(&gm->buckets)) {
8573 return OFPERR_OFPGMFC_BAD_BUCKET;
8580 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
8581 if (bucket->weight && gm->type != OFPGT11_SELECT) {
8582 return OFPERR_OFPGMFC_INVALID_GROUP;
8587 case OFPGT11_INDIRECT:
8588 if (ofputil_bucket_has_liveness(bucket)) {
8589 return OFPERR_OFPGMFC_WATCH_UNSUPPORTED;
8592 case OFPGT11_SELECT:
8595 if (!ofputil_bucket_has_liveness(bucket)) {
8596 return OFPERR_OFPGMFC_INVALID_GROUP;
8607 /* Parse a queue status request message into 'oqsr'.
8608 * Returns 0 if successful, otherwise an OFPERR_* number. */
8610 ofputil_decode_queue_stats_request(const struct ofp_header *request,
8611 struct ofputil_queue_stats_request *oqsr)
8613 switch ((enum ofp_version)request->version) {
8618 case OFP11_VERSION: {
8619 const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request);
8620 oqsr->queue_id = ntohl(qsr11->queue_id);
8621 return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no);
8624 case OFP10_VERSION: {
8625 const struct ofp10_queue_stats_request *qsr10 = ofpmsg_body(request);
8626 oqsr->queue_id = ntohl(qsr10->queue_id);
8627 oqsr->port_no = u16_to_ofp(ntohs(qsr10->port_no));
8628 /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
8629 if (oqsr->port_no == OFPP_ALL) {
8630 oqsr->port_no = OFPP_ANY;
8640 /* Encode a queue stats request for 'oqsr', the encoded message
8641 * will be for OpenFlow version 'ofp_version'. Returns message
8642 * as a struct ofpbuf. Returns encoded message on success, NULL on error. */
8644 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
8645 const struct ofputil_queue_stats_request *oqsr)
8647 struct ofpbuf *request;
8649 switch (ofp_version) {
8654 case OFP15_VERSION: {
8655 struct ofp11_queue_stats_request *req;
8656 request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0);
8657 req = ofpbuf_put_zeros(request, sizeof *req);
8658 req->port_no = ofputil_port_to_ofp11(oqsr->port_no);
8659 req->queue_id = htonl(oqsr->queue_id);
8662 case OFP10_VERSION: {
8663 struct ofp10_queue_stats_request *req;
8664 request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0);
8665 req = ofpbuf_put_zeros(request, sizeof *req);
8666 /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
8667 req->port_no = htons(ofp_to_u16(oqsr->port_no == OFPP_ANY
8668 ? OFPP_ALL : oqsr->port_no));
8669 req->queue_id = htonl(oqsr->queue_id);
8679 /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
8682 ofputil_count_queue_stats(const struct ofp_header *oh)
8684 struct ofputil_queue_stats qs;
8688 ofpbuf_use_const(&b, oh, ntohs(oh->length));
8689 ofpraw_pull_assert(&b);
8690 while (!ofputil_decode_queue_stats(&qs, &b)) {
8697 ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs,
8698 const struct ofp10_queue_stats *qs10)
8700 oqs->port_no = u16_to_ofp(ntohs(qs10->port_no));
8701 oqs->queue_id = ntohl(qs10->queue_id);
8702 oqs->tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes));
8703 oqs->tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets));
8704 oqs->tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors));
8705 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
8711 ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs,
8712 const struct ofp11_queue_stats *qs11)
8716 error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no);
8721 oqs->queue_id = ntohl(qs11->queue_id);
8722 oqs->tx_bytes = ntohll(qs11->tx_bytes);
8723 oqs->tx_packets = ntohll(qs11->tx_packets);
8724 oqs->tx_errors = ntohll(qs11->tx_errors);
8725 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
8731 ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats *oqs,
8732 const struct ofp13_queue_stats *qs13)
8734 enum ofperr error = ofputil_queue_stats_from_ofp11(oqs, &qs13->qs);
8736 oqs->duration_sec = ntohl(qs13->duration_sec);
8737 oqs->duration_nsec = ntohl(qs13->duration_nsec);
8744 ofputil_pull_ofp14_queue_stats(struct ofputil_queue_stats *oqs,
8747 const struct ofp14_queue_stats *qs14;
8750 qs14 = ofpbuf_try_pull(msg, sizeof *qs14);
8752 return OFPERR_OFPBRC_BAD_LEN;
8755 len = ntohs(qs14->length);
8756 if (len < sizeof *qs14 || len - sizeof *qs14 > msg->size) {
8757 return OFPERR_OFPBRC_BAD_LEN;
8759 ofpbuf_pull(msg, len - sizeof *qs14);
8761 /* No properties yet defined, so ignore them for now. */
8763 return ofputil_queue_stats_from_ofp13(oqs, &qs14->qs);
8766 /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
8767 * ofputil_queue_stats in 'qs'.
8769 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
8770 * message. Calling this function multiple times for a single 'msg' iterates
8771 * through the replies. The caller must initially leave 'msg''s layer pointers
8772 * null and not modify them between calls.
8774 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8775 * otherwise a positive errno value. */
8777 ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg)
8782 error = (msg->header ? ofpraw_decode(&raw, msg->header)
8783 : ofpraw_pull(&raw, msg));
8790 } else if (raw == OFPRAW_OFPST14_QUEUE_REPLY) {
8791 return ofputil_pull_ofp14_queue_stats(qs, msg);
8792 } else if (raw == OFPRAW_OFPST13_QUEUE_REPLY) {
8793 const struct ofp13_queue_stats *qs13;
8795 qs13 = ofpbuf_try_pull(msg, sizeof *qs13);
8799 return ofputil_queue_stats_from_ofp13(qs, qs13);
8800 } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) {
8801 const struct ofp11_queue_stats *qs11;
8803 qs11 = ofpbuf_try_pull(msg, sizeof *qs11);
8807 return ofputil_queue_stats_from_ofp11(qs, qs11);
8808 } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) {
8809 const struct ofp10_queue_stats *qs10;
8811 qs10 = ofpbuf_try_pull(msg, sizeof *qs10);
8815 return ofputil_queue_stats_from_ofp10(qs, qs10);
8821 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %"PRIu32" leftover "
8822 "bytes at end", msg->size);
8823 return OFPERR_OFPBRC_BAD_LEN;
8827 ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs,
8828 struct ofp10_queue_stats *qs10)
8830 qs10->port_no = htons(ofp_to_u16(oqs->port_no));
8831 memset(qs10->pad, 0, sizeof qs10->pad);
8832 qs10->queue_id = htonl(oqs->queue_id);
8833 put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->tx_bytes));
8834 put_32aligned_be64(&qs10->tx_packets, htonll(oqs->tx_packets));
8835 put_32aligned_be64(&qs10->tx_errors, htonll(oqs->tx_errors));
8839 ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs,
8840 struct ofp11_queue_stats *qs11)
8842 qs11->port_no = ofputil_port_to_ofp11(oqs->port_no);
8843 qs11->queue_id = htonl(oqs->queue_id);
8844 qs11->tx_bytes = htonll(oqs->tx_bytes);
8845 qs11->tx_packets = htonll(oqs->tx_packets);
8846 qs11->tx_errors = htonll(oqs->tx_errors);
8850 ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats *oqs,
8851 struct ofp13_queue_stats *qs13)
8853 ofputil_queue_stats_to_ofp11(oqs, &qs13->qs);
8854 if (oqs->duration_sec != UINT32_MAX) {
8855 qs13->duration_sec = htonl(oqs->duration_sec);
8856 qs13->duration_nsec = htonl(oqs->duration_nsec);
8858 qs13->duration_sec = OVS_BE32_MAX;
8859 qs13->duration_nsec = OVS_BE32_MAX;
8864 ofputil_queue_stats_to_ofp14(const struct ofputil_queue_stats *oqs,
8865 struct ofp14_queue_stats *qs14)
8867 qs14->length = htons(sizeof *qs14);
8868 memset(qs14->pad, 0, sizeof qs14->pad);
8869 ofputil_queue_stats_to_ofp13(oqs, &qs14->qs);
8873 /* Encode a queue stat for 'oqs' and append it to 'replies'. */
8875 ofputil_append_queue_stat(struct ovs_list *replies,
8876 const struct ofputil_queue_stats *oqs)
8878 switch (ofpmp_version(replies)) {
8879 case OFP13_VERSION: {
8880 struct ofp13_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
8881 ofputil_queue_stats_to_ofp13(oqs, reply);
8886 case OFP11_VERSION: {
8887 struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
8888 ofputil_queue_stats_to_ofp11(oqs, reply);
8892 case OFP10_VERSION: {
8893 struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
8894 ofputil_queue_stats_to_ofp10(oqs, reply);
8899 case OFP15_VERSION: {
8900 struct ofp14_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
8901 ofputil_queue_stats_to_ofp14(oqs, reply);
8911 ofputil_decode_bundle_ctrl(const struct ofp_header *oh,
8912 struct ofputil_bundle_ctrl_msg *msg)
8916 const struct ofp14_bundle_ctrl_msg *m;
8918 ofpbuf_use_const(&b, oh, ntohs(oh->length));
8919 raw = ofpraw_pull_assert(&b);
8920 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_CONTROL);
8923 msg->bundle_id = ntohl(m->bundle_id);
8924 msg->type = ntohs(m->type);
8925 msg->flags = ntohs(m->flags);
8931 ofputil_encode_bundle_ctrl_request(enum ofp_version ofp_version,
8932 struct ofputil_bundle_ctrl_msg *bc)
8934 struct ofpbuf *request;
8935 struct ofp14_bundle_ctrl_msg *m;
8937 switch (ofp_version) {
8942 ovs_fatal(0, "bundles need OpenFlow 1.4 or later "
8943 "(\'-O OpenFlow14\')");
8946 request = ofpraw_alloc(OFPRAW_OFPT14_BUNDLE_CONTROL, ofp_version, 0);
8947 m = ofpbuf_put_zeros(request, sizeof *m);
8949 m->bundle_id = htonl(bc->bundle_id);
8950 m->type = htons(bc->type);
8951 m->flags = htons(bc->flags);
8961 ofputil_encode_bundle_ctrl_reply(const struct ofp_header *oh,
8962 struct ofputil_bundle_ctrl_msg *msg)
8965 struct ofp14_bundle_ctrl_msg *m;
8967 buf = ofpraw_alloc_reply(OFPRAW_OFPT14_BUNDLE_CONTROL, oh, 0);
8968 m = ofpbuf_put_zeros(buf, sizeof *m);
8970 m->bundle_id = htonl(msg->bundle_id);
8971 m->type = htons(msg->type);
8972 m->flags = htons(msg->flags);
8977 /* Return true for bundlable state change requests, false for other messages.
8980 ofputil_is_bundlable(enum ofptype type)
8983 /* Minimum required by OpenFlow 1.4. */
8984 case OFPTYPE_PORT_MOD:
8985 case OFPTYPE_FLOW_MOD:
8988 /* Nice to have later. */
8989 case OFPTYPE_FLOW_MOD_TABLE_ID:
8990 case OFPTYPE_GROUP_MOD:
8991 case OFPTYPE_TABLE_MOD:
8992 case OFPTYPE_METER_MOD:
8993 case OFPTYPE_PACKET_OUT:
8994 case OFPTYPE_NXT_GENEVE_TABLE_MOD:
8996 /* Not to be bundlable. */
8997 case OFPTYPE_ECHO_REQUEST:
8998 case OFPTYPE_FEATURES_REQUEST:
8999 case OFPTYPE_GET_CONFIG_REQUEST:
9000 case OFPTYPE_SET_CONFIG:
9001 case OFPTYPE_BARRIER_REQUEST:
9002 case OFPTYPE_ROLE_REQUEST:
9003 case OFPTYPE_ECHO_REPLY:
9004 case OFPTYPE_SET_FLOW_FORMAT:
9005 case OFPTYPE_SET_PACKET_IN_FORMAT:
9006 case OFPTYPE_SET_CONTROLLER_ID:
9007 case OFPTYPE_FLOW_AGE:
9008 case OFPTYPE_FLOW_MONITOR_CANCEL:
9009 case OFPTYPE_SET_ASYNC_CONFIG:
9010 case OFPTYPE_GET_ASYNC_REQUEST:
9011 case OFPTYPE_DESC_STATS_REQUEST:
9012 case OFPTYPE_FLOW_STATS_REQUEST:
9013 case OFPTYPE_AGGREGATE_STATS_REQUEST:
9014 case OFPTYPE_TABLE_STATS_REQUEST:
9015 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
9016 case OFPTYPE_TABLE_DESC_REQUEST:
9017 case OFPTYPE_PORT_STATS_REQUEST:
9018 case OFPTYPE_QUEUE_STATS_REQUEST:
9019 case OFPTYPE_PORT_DESC_STATS_REQUEST:
9020 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
9021 case OFPTYPE_METER_STATS_REQUEST:
9022 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
9023 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
9024 case OFPTYPE_GROUP_STATS_REQUEST:
9025 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
9026 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
9027 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
9028 case OFPTYPE_BUNDLE_CONTROL:
9029 case OFPTYPE_BUNDLE_ADD_MESSAGE:
9032 case OFPTYPE_FEATURES_REPLY:
9033 case OFPTYPE_GET_CONFIG_REPLY:
9034 case OFPTYPE_PACKET_IN:
9035 case OFPTYPE_FLOW_REMOVED:
9036 case OFPTYPE_PORT_STATUS:
9037 case OFPTYPE_BARRIER_REPLY:
9038 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
9039 case OFPTYPE_DESC_STATS_REPLY:
9040 case OFPTYPE_FLOW_STATS_REPLY:
9041 case OFPTYPE_QUEUE_STATS_REPLY:
9042 case OFPTYPE_PORT_STATS_REPLY:
9043 case OFPTYPE_TABLE_STATS_REPLY:
9044 case OFPTYPE_AGGREGATE_STATS_REPLY:
9045 case OFPTYPE_PORT_DESC_STATS_REPLY:
9046 case OFPTYPE_ROLE_REPLY:
9047 case OFPTYPE_FLOW_MONITOR_PAUSED:
9048 case OFPTYPE_FLOW_MONITOR_RESUMED:
9049 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
9050 case OFPTYPE_GET_ASYNC_REPLY:
9051 case OFPTYPE_GROUP_STATS_REPLY:
9052 case OFPTYPE_GROUP_DESC_STATS_REPLY:
9053 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
9054 case OFPTYPE_METER_STATS_REPLY:
9055 case OFPTYPE_METER_CONFIG_STATS_REPLY:
9056 case OFPTYPE_METER_FEATURES_STATS_REPLY:
9057 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
9058 case OFPTYPE_TABLE_DESC_REPLY:
9059 case OFPTYPE_ROLE_STATUS:
9060 case OFPTYPE_NXT_GENEVE_TABLE_REQUEST:
9061 case OFPTYPE_NXT_GENEVE_TABLE_REPLY:
9069 ofputil_decode_bundle_add(const struct ofp_header *oh,
9070 struct ofputil_bundle_add_msg *msg,
9071 enum ofptype *type_ptr)
9073 const struct ofp14_bundle_ctrl_msg *m;
9080 ofpbuf_use_const(&b, oh, ntohs(oh->length));
9081 raw = ofpraw_pull_assert(&b);
9082 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE);
9084 m = ofpbuf_pull(&b, sizeof *m);
9085 msg->bundle_id = ntohl(m->bundle_id);
9086 msg->flags = ntohs(m->flags);
9089 if (msg->msg->version != oh->version) {
9090 return OFPERR_NXBFC_BAD_VERSION;
9092 inner_len = ntohs(msg->msg->length);
9093 if (inner_len < sizeof(struct ofp_header) || inner_len > b.size) {
9094 return OFPERR_OFPBFC_MSG_BAD_LEN;
9096 if (msg->msg->xid != oh->xid) {
9097 return OFPERR_OFPBFC_MSG_BAD_XID;
9100 /* Reject unbundlable messages. */
9104 error = ofptype_decode(type_ptr, msg->msg);
9106 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPT14_BUNDLE_ADD_MESSAGE contained "
9107 "message is unparsable (%s)", ofperr_get_name(error));
9108 return OFPERR_OFPBFC_MSG_UNSUP; /* 'error' would be confusing. */
9111 if (!ofputil_is_bundlable(*type_ptr)) {
9112 VLOG_WARN_RL(&bad_ofmsg_rl, "%s message not allowed inside "
9113 "OFPT14_BUNDLE_ADD_MESSAGE", ofptype_get_name(*type_ptr));
9114 return OFPERR_OFPBFC_MSG_UNSUP;
9121 ofputil_encode_bundle_add(enum ofp_version ofp_version,
9122 struct ofputil_bundle_add_msg *msg)
9124 struct ofpbuf *request;
9125 struct ofp14_bundle_ctrl_msg *m;
9127 /* Must use the same xid as the embedded message. */
9128 request = ofpraw_alloc_xid(OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE, ofp_version,
9130 m = ofpbuf_put_zeros(request, sizeof *m);
9132 m->bundle_id = htonl(msg->bundle_id);
9133 m->flags = htons(msg->flags);
9134 ofpbuf_put(request, msg->msg, ntohs(msg->msg->length));
9140 encode_geneve_table_mappings(struct ofpbuf *b, struct ovs_list *mappings)
9142 struct ofputil_geneve_map *map;
9144 LIST_FOR_EACH (map, list_node, mappings) {
9145 struct nx_geneve_map *nx_map;
9147 nx_map = ofpbuf_put_zeros(b, sizeof *nx_map);
9148 nx_map->option_class = htons(map->option_class);
9149 nx_map->option_type = map->option_type;
9150 nx_map->option_len = map->option_len;
9151 nx_map->index = htons(map->index);
9156 ofputil_encode_geneve_table_mod(enum ofp_version ofp_version,
9157 struct ofputil_geneve_table_mod *gtm)
9160 struct nx_geneve_table_mod *nx_gtm;
9162 b = ofpraw_alloc(OFPRAW_NXT_GENEVE_TABLE_MOD, ofp_version, 0);
9163 nx_gtm = ofpbuf_put_zeros(b, sizeof *nx_gtm);
9164 nx_gtm->command = htons(gtm->command);
9165 encode_geneve_table_mappings(b, >m->mappings);
9171 decode_geneve_table_mappings(struct ofpbuf *msg, unsigned int max_fields,
9172 struct ovs_list *mappings)
9174 list_init(mappings);
9177 struct nx_geneve_map *nx_map;
9178 struct ofputil_geneve_map *map;
9180 nx_map = ofpbuf_pull(msg, sizeof *nx_map);
9181 map = xmalloc(sizeof *map);
9182 list_push_back(mappings, &map->list_node);
9184 map->option_class = ntohs(nx_map->option_class);
9185 map->option_type = nx_map->option_type;
9187 map->option_len = nx_map->option_len;
9188 if (map->option_len % 4 || map->option_len > GENEVE_MAX_OPT_SIZE) {
9189 VLOG_WARN_RL(&bad_ofmsg_rl,
9190 "geneve table option length (%u) is not a valid option size",
9192 ofputil_uninit_geneve_table(mappings);
9193 return OFPERR_NXGTMFC_BAD_OPT_LEN;
9196 map->index = ntohs(nx_map->index);
9197 if (map->index >= max_fields) {
9198 VLOG_WARN_RL(&bad_ofmsg_rl,
9199 "geneve table field index (%u) is too large (max %u)",
9200 map->index, max_fields - 1);
9201 ofputil_uninit_geneve_table(mappings);
9202 return OFPERR_NXGTMFC_BAD_FIELD_IDX;
9210 ofputil_decode_geneve_table_mod(const struct ofp_header *oh,
9211 struct ofputil_geneve_table_mod *gtm)
9214 struct nx_geneve_table_mod *nx_gtm;
9216 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
9217 ofpraw_pull_assert(&msg);
9219 nx_gtm = ofpbuf_pull(&msg, sizeof *nx_gtm);
9220 gtm->command = ntohs(nx_gtm->command);
9221 if (gtm->command > NXGTMC_CLEAR) {
9222 VLOG_WARN_RL(&bad_ofmsg_rl,
9223 "geneve table mod command (%u) is out of range",
9225 return OFPERR_NXGTMFC_BAD_COMMAND;
9228 return decode_geneve_table_mappings(&msg, TUN_METADATA_NUM_OPTS,
9233 ofputil_encode_geneve_table_reply(const struct ofp_header *oh,
9234 struct ofputil_geneve_table_reply *gtr)
9237 struct nx_geneve_table_reply *nx_gtr;
9239 b = ofpraw_alloc_reply(OFPRAW_NXT_GENEVE_TABLE_REPLY, oh, 0);
9240 nx_gtr = ofpbuf_put_zeros(b, sizeof *nx_gtr);
9241 nx_gtr->max_option_space = htonl(gtr->max_option_space);
9242 nx_gtr->max_fields = htons(gtr->max_fields);
9244 encode_geneve_table_mappings(b, >r->mappings);
9249 /* Decodes the NXT_GENEVE_TABLE_REPLY message in 'oh' into '*gtr'. Returns 0
9250 * if successful, otherwise an ofperr.
9252 * The decoder verifies that the indexes in 'gtr->mappings' are less than
9253 * 'gtr->max_fields', but the caller must ensure, if necessary, that they are
9254 * less than TUN_METADATA_NUM_OPTS. */
9256 ofputil_decode_geneve_table_reply(const struct ofp_header *oh,
9257 struct ofputil_geneve_table_reply *gtr)
9260 struct nx_geneve_table_reply *nx_gtr;
9262 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
9263 ofpraw_pull_assert(&msg);
9265 nx_gtr = ofpbuf_pull(&msg, sizeof *nx_gtr);
9266 gtr->max_option_space = ntohl(nx_gtr->max_option_space);
9267 gtr->max_fields = ntohs(nx_gtr->max_fields);
9269 return decode_geneve_table_mappings(&msg, gtr->max_fields, >r->mappings);
9273 ofputil_uninit_geneve_table(struct ovs_list *mappings)
9275 struct ofputil_geneve_map *map;
9277 LIST_FOR_EACH_POP (map, list_node, mappings) {
9282 /* Decodes the OpenFlow "set async config" request and "get async config
9283 * reply" message in '*oh' into an abstract form in 'master' and 'slave'.
9285 * If 'loose' is true, this function ignores properties and values that it does
9286 * not understand, as a controller would want to do when interpreting
9287 * capabilities provided by a switch. If 'loose' is false, this function
9288 * treats unknown properties and values as an error, as a switch would want to
9289 * do when interpreting a configuration request made by a controller.
9291 * Returns 0 if successful, otherwise an OFPERR_* value. */
9293 ofputil_decode_set_async_config(const struct ofp_header *oh,
9294 uint32_t master[OAM_N_TYPES],
9295 uint32_t slave[OAM_N_TYPES],
9301 ofpbuf_use_const(&b, oh, ntohs(oh->length));
9302 raw = ofpraw_pull_assert(&b);
9304 if (raw == OFPRAW_OFPT13_SET_ASYNC ||
9305 raw == OFPRAW_NXT_SET_ASYNC_CONFIG ||
9306 raw == OFPRAW_OFPT13_GET_ASYNC_REPLY) {
9307 const struct nx_async_config *msg = ofpmsg_body(oh);
9309 master[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[0]);
9310 master[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[0]);
9311 master[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[0]);
9313 slave[OAM_PACKET_IN] = ntohl(msg->packet_in_mask[1]);
9314 slave[OAM_PORT_STATUS] = ntohl(msg->port_status_mask[1]);
9315 slave[OAM_FLOW_REMOVED] = ntohl(msg->flow_removed_mask[1]);
9317 } else if (raw == OFPRAW_OFPT14_SET_ASYNC ||
9318 raw == OFPRAW_OFPT14_GET_ASYNC_REPLY) {
9320 while (b.size > 0) {
9321 struct ofp14_async_config_prop_reasons *msg;
9322 struct ofpbuf property;
9326 error = ofputil_pull_property(&b, &property, &type);
9331 msg = property.data;
9333 if (property.size != sizeof *msg) {
9334 return OFPERR_OFPBRC_BAD_LEN;
9338 case OFPACPT_PACKET_IN_SLAVE:
9339 slave[OAM_PACKET_IN] = ntohl(msg->mask);
9342 case OFPACPT_PACKET_IN_MASTER:
9343 master[OAM_PACKET_IN] = ntohl(msg->mask);
9346 case OFPACPT_PORT_STATUS_SLAVE:
9347 slave[OAM_PORT_STATUS] = ntohl(msg->mask);
9350 case OFPACPT_PORT_STATUS_MASTER:
9351 master[OAM_PORT_STATUS] = ntohl(msg->mask);
9354 case OFPACPT_FLOW_REMOVED_SLAVE:
9355 slave[OAM_FLOW_REMOVED] = ntohl(msg->mask);
9358 case OFPACPT_FLOW_REMOVED_MASTER:
9359 master[OAM_FLOW_REMOVED] = ntohl(msg->mask);
9362 case OFPACPT_ROLE_STATUS_SLAVE:
9363 slave[OAM_ROLE_STATUS] = ntohl(msg->mask);
9366 case OFPACPT_ROLE_STATUS_MASTER:
9367 master[OAM_ROLE_STATUS] = ntohl(msg->mask);
9370 case OFPACPT_TABLE_STATUS_SLAVE:
9371 slave[OAM_TABLE_STATUS] = ntohl(msg->mask);
9374 case OFPACPT_TABLE_STATUS_MASTER:
9375 master[OAM_TABLE_STATUS] = ntohl(msg->mask);
9378 case OFPACPT_REQUESTFORWARD_SLAVE:
9379 slave[OAM_REQUESTFORWARD] = ntohl(msg->mask);
9382 case OFPACPT_REQUESTFORWARD_MASTER:
9383 master[OAM_REQUESTFORWARD] = ntohl(msg->mask);
9387 error = loose ? 0 : OFPERR_OFPBPC_BAD_TYPE;
9395 return OFPERR_OFPBRC_BAD_VERSION;
9400 /* Append all asynchronous configuration properties in GET_ASYNC_REPLY
9401 * message, describing if various set of asynchronous messages are enabled
9404 ofputil_get_async_reply(struct ofpbuf *buf, const uint32_t master_mask,
9405 const uint32_t slave_mask, const uint32_t type)
9409 for (role = 0; role < 2; role++) {
9410 struct ofp14_async_config_prop_reasons *msg;
9412 msg = ofpbuf_put_zeros(buf, sizeof *msg);
9416 msg->type = (role ? htons(OFPACPT_PACKET_IN_SLAVE)
9417 : htons(OFPACPT_PACKET_IN_MASTER));
9420 case OAM_PORT_STATUS:
9421 msg->type = (role ? htons(OFPACPT_PORT_STATUS_SLAVE)
9422 : htons(OFPACPT_PORT_STATUS_MASTER));
9425 case OAM_FLOW_REMOVED:
9426 msg->type = (role ? htons(OFPACPT_FLOW_REMOVED_SLAVE)
9427 : htons(OFPACPT_FLOW_REMOVED_MASTER));
9430 case OAM_ROLE_STATUS:
9431 msg->type = (role ? htons(OFPACPT_ROLE_STATUS_SLAVE)
9432 : htons(OFPACPT_ROLE_STATUS_MASTER));
9435 case OAM_TABLE_STATUS:
9436 msg->type = (role ? htons(OFPACPT_TABLE_STATUS_SLAVE)
9437 : htons(OFPACPT_TABLE_STATUS_MASTER));
9440 case OAM_REQUESTFORWARD:
9441 msg->type = (role ? htons(OFPACPT_REQUESTFORWARD_SLAVE)
9442 : htons(OFPACPT_REQUESTFORWARD_MASTER));
9446 return OFPERR_OFPBRC_BAD_TYPE;
9448 msg->length = htons(sizeof *msg);
9449 msg->mask = (role ? htonl(slave_mask) : htonl(master_mask));
9455 /* Returns a OpenFlow message that encodes 'asynchronous configuration' properly
9456 * as a reply to get async config request. */
9458 ofputil_encode_get_async_config(const struct ofp_header *oh,
9459 uint32_t master[OAM_N_TYPES],
9460 uint32_t slave[OAM_N_TYPES])
9465 buf = ofpraw_alloc_reply((oh->version < OFP14_VERSION
9466 ? OFPRAW_OFPT13_GET_ASYNC_REPLY
9467 : OFPRAW_OFPT14_GET_ASYNC_REPLY), oh, 0);
9469 if (oh->version < OFP14_VERSION) {
9470 struct nx_async_config *msg;
9471 msg = ofpbuf_put_zeros(buf, sizeof *msg);
9473 msg->packet_in_mask[0] = htonl(master[OAM_PACKET_IN]);
9474 msg->port_status_mask[0] = htonl(master[OAM_PORT_STATUS]);
9475 msg->flow_removed_mask[0] = htonl(master[OAM_FLOW_REMOVED]);
9477 msg->packet_in_mask[1] = htonl(slave[OAM_PACKET_IN]);
9478 msg->port_status_mask[1] = htonl(slave[OAM_PORT_STATUS]);
9479 msg->flow_removed_mask[1] = htonl(slave[OAM_FLOW_REMOVED]);
9480 } else if (oh->version == OFP14_VERSION) {
9481 for (type = 0; type < OAM_N_TYPES; type++) {
9482 ofputil_get_async_reply(buf, master[type], slave[type], type);