2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofp-print.h"
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
27 #include "byte-order.h"
28 #include "classifier.h"
29 #include "dynamic-string.h"
31 #include "meta-flow.h"
32 #include "multipath.h"
36 #include "ofp-actions.h"
37 #include "ofp-errors.h"
42 #include "openflow/netronome-ext.h"
46 #include "tun-metadata.h"
47 #include "unaligned.h"
48 #include "type-props.h"
49 #include "openvswitch/vlog.h"
52 VLOG_DEFINE_THIS_MODULE(ofp_util);
54 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
55 * in the peer and so there's not much point in showing a lot of them. */
56 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
58 static enum ofputil_table_vacancy ofputil_decode_table_vacancy(
59 ovs_be32 config, enum ofp_version);
60 static enum ofputil_table_eviction ofputil_decode_table_eviction(
61 ovs_be32 config, enum ofp_version);
62 static ovs_be32 ofputil_encode_table_config(enum ofputil_table_miss,
63 enum ofputil_table_eviction,
64 enum ofputil_table_vacancy,
67 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
68 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
71 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
72 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
73 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
74 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
77 ofputil_wcbits_to_netmask(int wcbits)
80 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
83 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
84 * that it wildcards, that is, the number of 0-bits in 'netmask', a number
85 * between 0 and 32 inclusive.
87 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
88 * still be in the valid range but isn't otherwise meaningful. */
90 ofputil_netmask_to_wcbits(ovs_be32 netmask)
92 return 32 - ip_count_cidr_bits(netmask);
95 /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
96 * flow_wildcards in 'wc' for use in struct match. It is the caller's
97 * responsibility to handle the special case where the flow match's dl_vlan is
98 * set to OFP_VLAN_NONE. */
100 ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc)
102 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 35);
104 /* Initialize most of wc. */
105 flow_wildcards_init_catchall(wc);
107 if (!(ofpfw & OFPFW10_IN_PORT)) {
108 wc->masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
111 if (!(ofpfw & OFPFW10_NW_TOS)) {
112 wc->masks.nw_tos |= IP_DSCP_MASK;
115 if (!(ofpfw & OFPFW10_NW_PROTO)) {
116 wc->masks.nw_proto = UINT8_MAX;
118 wc->masks.nw_src = ofputil_wcbits_to_netmask(ofpfw
119 >> OFPFW10_NW_SRC_SHIFT);
120 wc->masks.nw_dst = ofputil_wcbits_to_netmask(ofpfw
121 >> OFPFW10_NW_DST_SHIFT);
123 if (!(ofpfw & OFPFW10_TP_SRC)) {
124 wc->masks.tp_src = OVS_BE16_MAX;
126 if (!(ofpfw & OFPFW10_TP_DST)) {
127 wc->masks.tp_dst = OVS_BE16_MAX;
130 if (!(ofpfw & OFPFW10_DL_SRC)) {
131 WC_MASK_FIELD(wc, dl_src);
133 if (!(ofpfw & OFPFW10_DL_DST)) {
134 WC_MASK_FIELD(wc, dl_dst);
136 if (!(ofpfw & OFPFW10_DL_TYPE)) {
137 wc->masks.dl_type = OVS_BE16_MAX;
141 if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) {
142 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
144 if (!(ofpfw & OFPFW10_DL_VLAN)) {
145 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
149 /* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */
151 ofputil_match_from_ofp10_match(const struct ofp10_match *ofmatch,
154 uint32_t ofpfw = ntohl(ofmatch->wildcards) & OFPFW10_ALL;
156 /* Initialize match->wc. */
157 memset(&match->flow, 0, sizeof match->flow);
158 ofputil_wildcard_from_ofpfw10(ofpfw, &match->wc);
160 /* Initialize most of match->flow. */
161 match->flow.nw_src = ofmatch->nw_src;
162 match->flow.nw_dst = ofmatch->nw_dst;
163 match->flow.in_port.ofp_port = u16_to_ofp(ntohs(ofmatch->in_port));
164 match->flow.dl_type = ofputil_dl_type_from_openflow(ofmatch->dl_type);
165 match->flow.tp_src = ofmatch->tp_src;
166 match->flow.tp_dst = ofmatch->tp_dst;
167 match->flow.dl_src = ofmatch->dl_src;
168 match->flow.dl_dst = ofmatch->dl_dst;
169 match->flow.nw_tos = ofmatch->nw_tos & IP_DSCP_MASK;
170 match->flow.nw_proto = ofmatch->nw_proto;
172 /* Translate VLANs. */
173 if (!(ofpfw & OFPFW10_DL_VLAN) &&
174 ofmatch->dl_vlan == htons(OFP10_VLAN_NONE)) {
175 /* Match only packets without 802.1Q header.
177 * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
179 * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
180 * because we can't have a specific PCP without an 802.1Q header.
181 * However, older versions of OVS treated this as matching packets
182 * withut an 802.1Q header, so we do here too. */
183 match->flow.vlan_tci = htons(0);
184 match->wc.masks.vlan_tci = htons(0xffff);
186 ovs_be16 vid, pcp, tci;
189 vid = ofmatch->dl_vlan & htons(VLAN_VID_MASK);
190 hpcp = (ofmatch->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK;
192 tci = vid | pcp | htons(VLAN_CFI);
193 match->flow.vlan_tci = tci & match->wc.masks.vlan_tci;
197 match_zero_wildcarded_fields(match);
200 /* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */
202 ofputil_match_to_ofp10_match(const struct match *match,
203 struct ofp10_match *ofmatch)
205 const struct flow_wildcards *wc = &match->wc;
208 /* Figure out most OpenFlow wildcards. */
210 if (!wc->masks.in_port.ofp_port) {
211 ofpfw |= OFPFW10_IN_PORT;
213 if (!wc->masks.dl_type) {
214 ofpfw |= OFPFW10_DL_TYPE;
216 if (!wc->masks.nw_proto) {
217 ofpfw |= OFPFW10_NW_PROTO;
219 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_src)
220 << OFPFW10_NW_SRC_SHIFT);
221 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_dst)
222 << OFPFW10_NW_DST_SHIFT);
223 if (!(wc->masks.nw_tos & IP_DSCP_MASK)) {
224 ofpfw |= OFPFW10_NW_TOS;
226 if (!wc->masks.tp_src) {
227 ofpfw |= OFPFW10_TP_SRC;
229 if (!wc->masks.tp_dst) {
230 ofpfw |= OFPFW10_TP_DST;
232 if (eth_addr_is_zero(wc->masks.dl_src)) {
233 ofpfw |= OFPFW10_DL_SRC;
235 if (eth_addr_is_zero(wc->masks.dl_dst)) {
236 ofpfw |= OFPFW10_DL_DST;
239 /* Translate VLANs. */
240 ofmatch->dl_vlan = htons(0);
241 ofmatch->dl_vlan_pcp = 0;
242 if (match->wc.masks.vlan_tci == htons(0)) {
243 ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP;
244 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
245 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
246 ofmatch->dl_vlan = htons(OFP10_VLAN_NONE);
248 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
249 ofpfw |= OFPFW10_DL_VLAN;
251 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
254 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
255 ofpfw |= OFPFW10_DL_VLAN_PCP;
257 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
261 /* Compose most of the match structure. */
262 ofmatch->wildcards = htonl(ofpfw);
263 ofmatch->in_port = htons(ofp_to_u16(match->flow.in_port.ofp_port));
264 ofmatch->dl_src = match->flow.dl_src;
265 ofmatch->dl_dst = match->flow.dl_dst;
266 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
267 ofmatch->nw_src = match->flow.nw_src;
268 ofmatch->nw_dst = match->flow.nw_dst;
269 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
270 ofmatch->nw_proto = match->flow.nw_proto;
271 ofmatch->tp_src = match->flow.tp_src;
272 ofmatch->tp_dst = match->flow.tp_dst;
273 memset(ofmatch->pad1, '\0', sizeof ofmatch->pad1);
274 memset(ofmatch->pad2, '\0', sizeof ofmatch->pad2);
278 ofputil_pull_ofp11_match(struct ofpbuf *buf, struct match *match,
279 uint16_t *padded_match_len)
281 struct ofp11_match_header *omh = buf->data;
284 if (buf->size < sizeof *omh) {
285 return OFPERR_OFPBMC_BAD_LEN;
288 match_len = ntohs(omh->length);
290 switch (ntohs(omh->type)) {
291 case OFPMT_STANDARD: {
292 struct ofp11_match *om;
294 if (match_len != sizeof *om || buf->size < sizeof *om) {
295 return OFPERR_OFPBMC_BAD_LEN;
297 om = ofpbuf_pull(buf, sizeof *om);
298 if (padded_match_len) {
299 *padded_match_len = match_len;
301 return ofputil_match_from_ofp11_match(om, match);
305 if (padded_match_len) {
306 *padded_match_len = ROUND_UP(match_len, 8);
308 return oxm_pull_match(buf, match);
311 return OFPERR_OFPBMC_BAD_TYPE;
315 /* Converts the ofp11_match in 'ofmatch' into a struct match in 'match'.
316 * Returns 0 if successful, otherwise an OFPERR_* value. */
318 ofputil_match_from_ofp11_match(const struct ofp11_match *ofmatch,
321 uint16_t wc = ntohl(ofmatch->wildcards);
322 bool ipv4, arp, rarp;
324 match_init_catchall(match);
326 if (!(wc & OFPFW11_IN_PORT)) {
330 error = ofputil_port_from_ofp11(ofmatch->in_port, &ofp_port);
332 return OFPERR_OFPBMC_BAD_VALUE;
334 match_set_in_port(match, ofp_port);
337 match_set_dl_src_masked(match, ofmatch->dl_src,
338 eth_addr_invert(ofmatch->dl_src_mask));
339 match_set_dl_dst_masked(match, ofmatch->dl_dst,
340 eth_addr_invert(ofmatch->dl_dst_mask));
342 if (!(wc & OFPFW11_DL_VLAN)) {
343 if (ofmatch->dl_vlan == htons(OFPVID11_NONE)) {
344 /* Match only packets without a VLAN tag. */
345 match->flow.vlan_tci = htons(0);
346 match->wc.masks.vlan_tci = OVS_BE16_MAX;
348 if (ofmatch->dl_vlan == htons(OFPVID11_ANY)) {
349 /* Match any packet with a VLAN tag regardless of VID. */
350 match->flow.vlan_tci = htons(VLAN_CFI);
351 match->wc.masks.vlan_tci = htons(VLAN_CFI);
352 } else if (ntohs(ofmatch->dl_vlan) < 4096) {
353 /* Match only packets with the specified VLAN VID. */
354 match->flow.vlan_tci = htons(VLAN_CFI) | ofmatch->dl_vlan;
355 match->wc.masks.vlan_tci = htons(VLAN_CFI | VLAN_VID_MASK);
358 return OFPERR_OFPBMC_BAD_VALUE;
361 if (!(wc & OFPFW11_DL_VLAN_PCP)) {
362 if (ofmatch->dl_vlan_pcp <= 7) {
363 match->flow.vlan_tci |= htons(ofmatch->dl_vlan_pcp
365 match->wc.masks.vlan_tci |= htons(VLAN_PCP_MASK);
368 return OFPERR_OFPBMC_BAD_VALUE;
374 if (!(wc & OFPFW11_DL_TYPE)) {
375 match_set_dl_type(match,
376 ofputil_dl_type_from_openflow(ofmatch->dl_type));
379 ipv4 = match->flow.dl_type == htons(ETH_TYPE_IP);
380 arp = match->flow.dl_type == htons(ETH_TYPE_ARP);
381 rarp = match->flow.dl_type == htons(ETH_TYPE_RARP);
383 if (ipv4 && !(wc & OFPFW11_NW_TOS)) {
384 if (ofmatch->nw_tos & ~IP_DSCP_MASK) {
386 return OFPERR_OFPBMC_BAD_VALUE;
389 match_set_nw_dscp(match, ofmatch->nw_tos);
392 if (ipv4 || arp || rarp) {
393 if (!(wc & OFPFW11_NW_PROTO)) {
394 match_set_nw_proto(match, ofmatch->nw_proto);
396 match_set_nw_src_masked(match, ofmatch->nw_src, ~ofmatch->nw_src_mask);
397 match_set_nw_dst_masked(match, ofmatch->nw_dst, ~ofmatch->nw_dst_mask);
400 #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
401 if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) {
402 switch (match->flow.nw_proto) {
404 /* "A.2.3 Flow Match Structures" in OF1.1 says:
406 * The tp_src and tp_dst fields will be ignored unless the
407 * network protocol specified is as TCP, UDP or SCTP.
409 * but I'm pretty sure we should support ICMP too, otherwise
410 * that's a regression from OF1.0. */
411 if (!(wc & OFPFW11_TP_SRC)) {
412 uint16_t icmp_type = ntohs(ofmatch->tp_src);
413 if (icmp_type < 0x100) {
414 match_set_icmp_type(match, icmp_type);
416 return OFPERR_OFPBMC_BAD_FIELD;
419 if (!(wc & OFPFW11_TP_DST)) {
420 uint16_t icmp_code = ntohs(ofmatch->tp_dst);
421 if (icmp_code < 0x100) {
422 match_set_icmp_code(match, icmp_code);
424 return OFPERR_OFPBMC_BAD_FIELD;
432 if (!(wc & (OFPFW11_TP_SRC))) {
433 match_set_tp_src(match, ofmatch->tp_src);
435 if (!(wc & (OFPFW11_TP_DST))) {
436 match_set_tp_dst(match, ofmatch->tp_dst);
441 /* OF1.1 says explicitly to ignore this. */
446 if (eth_type_mpls(match->flow.dl_type)) {
447 if (!(wc & OFPFW11_MPLS_LABEL)) {
448 match_set_mpls_label(match, 0, ofmatch->mpls_label);
450 if (!(wc & OFPFW11_MPLS_TC)) {
451 match_set_mpls_tc(match, 0, ofmatch->mpls_tc);
455 match_set_metadata_masked(match, ofmatch->metadata,
456 ~ofmatch->metadata_mask);
461 /* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */
463 ofputil_match_to_ofp11_match(const struct match *match,
464 struct ofp11_match *ofmatch)
468 memset(ofmatch, 0, sizeof *ofmatch);
469 ofmatch->omh.type = htons(OFPMT_STANDARD);
470 ofmatch->omh.length = htons(OFPMT11_STANDARD_LENGTH);
472 if (!match->wc.masks.in_port.ofp_port) {
473 wc |= OFPFW11_IN_PORT;
475 ofmatch->in_port = ofputil_port_to_ofp11(match->flow.in_port.ofp_port);
478 ofmatch->dl_src = match->flow.dl_src;
479 ofmatch->dl_src_mask = eth_addr_invert(match->wc.masks.dl_src);
480 ofmatch->dl_dst = match->flow.dl_dst;
481 ofmatch->dl_dst_mask = eth_addr_invert(match->wc.masks.dl_dst);
483 if (match->wc.masks.vlan_tci == htons(0)) {
484 wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP;
485 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
486 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
487 ofmatch->dl_vlan = htons(OFPVID11_NONE);
488 wc |= OFPFW11_DL_VLAN_PCP;
490 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
491 ofmatch->dl_vlan = htons(OFPVID11_ANY);
493 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
496 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
497 wc |= OFPFW11_DL_VLAN_PCP;
499 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
503 if (!match->wc.masks.dl_type) {
504 wc |= OFPFW11_DL_TYPE;
506 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
509 if (!(match->wc.masks.nw_tos & IP_DSCP_MASK)) {
510 wc |= OFPFW11_NW_TOS;
512 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
515 if (!match->wc.masks.nw_proto) {
516 wc |= OFPFW11_NW_PROTO;
518 ofmatch->nw_proto = match->flow.nw_proto;
521 ofmatch->nw_src = match->flow.nw_src;
522 ofmatch->nw_src_mask = ~match->wc.masks.nw_src;
523 ofmatch->nw_dst = match->flow.nw_dst;
524 ofmatch->nw_dst_mask = ~match->wc.masks.nw_dst;
526 if (!match->wc.masks.tp_src) {
527 wc |= OFPFW11_TP_SRC;
529 ofmatch->tp_src = match->flow.tp_src;
532 if (!match->wc.masks.tp_dst) {
533 wc |= OFPFW11_TP_DST;
535 ofmatch->tp_dst = match->flow.tp_dst;
538 if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK))) {
539 wc |= OFPFW11_MPLS_LABEL;
541 ofmatch->mpls_label = htonl(mpls_lse_to_label(
542 match->flow.mpls_lse[0]));
545 if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK))) {
546 wc |= OFPFW11_MPLS_TC;
548 ofmatch->mpls_tc = mpls_lse_to_tc(match->flow.mpls_lse[0]);
551 ofmatch->metadata = match->flow.metadata;
552 ofmatch->metadata_mask = ~match->wc.masks.metadata;
554 ofmatch->wildcards = htonl(wc);
557 /* Returns the "typical" length of a match for 'protocol', for use in
558 * estimating space to preallocate. */
560 ofputil_match_typical_len(enum ofputil_protocol protocol)
563 case OFPUTIL_P_OF10_STD:
564 case OFPUTIL_P_OF10_STD_TID:
565 return sizeof(struct ofp10_match);
567 case OFPUTIL_P_OF10_NXM:
568 case OFPUTIL_P_OF10_NXM_TID:
569 return NXM_TYPICAL_LEN;
571 case OFPUTIL_P_OF11_STD:
572 return sizeof(struct ofp11_match);
574 case OFPUTIL_P_OF12_OXM:
575 case OFPUTIL_P_OF13_OXM:
576 case OFPUTIL_P_OF14_OXM:
577 case OFPUTIL_P_OF15_OXM:
578 return NXM_TYPICAL_LEN;
585 /* Appends to 'b' an struct ofp11_match_header followed by a match that
586 * expresses 'match' properly for 'protocol', plus enough zero bytes to pad the
587 * data appended out to a multiple of 8. 'protocol' must be one that is usable
588 * in OpenFlow 1.1 or later.
590 * This function can cause 'b''s data to be reallocated.
592 * Returns the number of bytes appended to 'b', excluding the padding. Never
595 ofputil_put_ofp11_match(struct ofpbuf *b, const struct match *match,
596 enum ofputil_protocol protocol)
599 case OFPUTIL_P_OF10_STD:
600 case OFPUTIL_P_OF10_STD_TID:
601 case OFPUTIL_P_OF10_NXM:
602 case OFPUTIL_P_OF10_NXM_TID:
605 case OFPUTIL_P_OF11_STD: {
606 struct ofp11_match *om;
608 /* Make sure that no padding is needed. */
609 BUILD_ASSERT_DECL(sizeof *om % 8 == 0);
611 om = ofpbuf_put_uninit(b, sizeof *om);
612 ofputil_match_to_ofp11_match(match, om);
616 case OFPUTIL_P_OF12_OXM:
617 case OFPUTIL_P_OF13_OXM:
618 case OFPUTIL_P_OF14_OXM:
619 case OFPUTIL_P_OF15_OXM:
620 return oxm_put_match(b, match,
621 ofputil_protocol_to_ofp_version(protocol));
627 /* Given a 'dl_type' value in the format used in struct flow, returns the
628 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
631 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
633 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
634 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
638 /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
639 * structure, returns the corresponding 'dl_type' value for use in struct
642 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
644 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
645 ? htons(FLOW_DL_TYPE_NONE)
651 struct proto_abbrev {
652 enum ofputil_protocol protocol;
656 /* Most users really don't care about some of the differences between
657 * protocols. These abbreviations help with that. */
658 static const struct proto_abbrev proto_abbrevs[] = {
659 { OFPUTIL_P_ANY, "any" },
660 { OFPUTIL_P_OF10_STD_ANY, "OpenFlow10" },
661 { OFPUTIL_P_OF10_NXM_ANY, "NXM" },
662 { OFPUTIL_P_ANY_OXM, "OXM" },
664 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
666 enum ofputil_protocol ofputil_flow_dump_protocols[] = {
675 size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
677 /* Returns the set of ofputil_protocols that are supported with the given
678 * OpenFlow 'version'. 'version' should normally be an 8-bit OpenFlow version
679 * identifier (e.g. 0x01 for OpenFlow 1.0, 0x02 for OpenFlow 1.1). Returns 0
680 * if 'version' is not supported or outside the valid range. */
681 enum ofputil_protocol
682 ofputil_protocols_from_ofp_version(enum ofp_version version)
686 return OFPUTIL_P_OF10_STD_ANY | OFPUTIL_P_OF10_NXM_ANY;
688 return OFPUTIL_P_OF11_STD;
690 return OFPUTIL_P_OF12_OXM;
692 return OFPUTIL_P_OF13_OXM;
694 return OFPUTIL_P_OF14_OXM;
696 return OFPUTIL_P_OF15_OXM;
702 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
703 * connection that has negotiated the given 'version'. 'version' should
704 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
705 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
706 * outside the valid range. */
707 enum ofputil_protocol
708 ofputil_protocol_from_ofp_version(enum ofp_version version)
710 return rightmost_1bit(ofputil_protocols_from_ofp_version(version));
713 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
714 * etc.) that corresponds to 'protocol'. */
716 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
719 case OFPUTIL_P_OF10_STD:
720 case OFPUTIL_P_OF10_STD_TID:
721 case OFPUTIL_P_OF10_NXM:
722 case OFPUTIL_P_OF10_NXM_TID:
723 return OFP10_VERSION;
724 case OFPUTIL_P_OF11_STD:
725 return OFP11_VERSION;
726 case OFPUTIL_P_OF12_OXM:
727 return OFP12_VERSION;
728 case OFPUTIL_P_OF13_OXM:
729 return OFP13_VERSION;
730 case OFPUTIL_P_OF14_OXM:
731 return OFP14_VERSION;
732 case OFPUTIL_P_OF15_OXM:
733 return OFP15_VERSION;
739 /* Returns a bitmap of OpenFlow versions that are supported by at
740 * least one of the 'protocols'. */
742 ofputil_protocols_to_version_bitmap(enum ofputil_protocol protocols)
746 for (; protocols; protocols = zero_rightmost_1bit(protocols)) {
747 enum ofputil_protocol protocol = rightmost_1bit(protocols);
749 bitmap |= 1u << ofputil_protocol_to_ofp_version(protocol);
755 /* Returns the set of protocols that are supported on top of the
756 * OpenFlow versions included in 'bitmap'. */
757 enum ofputil_protocol
758 ofputil_protocols_from_version_bitmap(uint32_t bitmap)
760 enum ofputil_protocol protocols = 0;
762 for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
763 enum ofp_version version = rightmost_1bit_idx(bitmap);
765 protocols |= ofputil_protocols_from_ofp_version(version);
771 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
774 ofputil_protocol_is_valid(enum ofputil_protocol protocol)
776 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
779 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
780 * extension turned on or off if 'enable' is true or false, respectively.
782 * This extension is only useful for protocols whose "standard" version does
783 * not allow specific tables to be modified. In particular, this is true of
784 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
785 * specifies a table ID and so there is no need for such an extension. When
786 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
787 * extension, this function just returns its 'protocol' argument unchanged
788 * regardless of the value of 'enable'. */
789 enum ofputil_protocol
790 ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
793 case OFPUTIL_P_OF10_STD:
794 case OFPUTIL_P_OF10_STD_TID:
795 return enable ? OFPUTIL_P_OF10_STD_TID : OFPUTIL_P_OF10_STD;
797 case OFPUTIL_P_OF10_NXM:
798 case OFPUTIL_P_OF10_NXM_TID:
799 return enable ? OFPUTIL_P_OF10_NXM_TID : OFPUTIL_P_OF10_NXM;
801 case OFPUTIL_P_OF11_STD:
802 return OFPUTIL_P_OF11_STD;
804 case OFPUTIL_P_OF12_OXM:
805 return OFPUTIL_P_OF12_OXM;
807 case OFPUTIL_P_OF13_OXM:
808 return OFPUTIL_P_OF13_OXM;
810 case OFPUTIL_P_OF14_OXM:
811 return OFPUTIL_P_OF14_OXM;
813 case OFPUTIL_P_OF15_OXM:
814 return OFPUTIL_P_OF15_OXM;
821 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
822 * some extension to a standard protocol version, the return value is the
823 * standard version of that protocol without any extension. If 'protocol' is a
824 * standard protocol version, returns 'protocol' unchanged. */
825 enum ofputil_protocol
826 ofputil_protocol_to_base(enum ofputil_protocol protocol)
828 return ofputil_protocol_set_tid(protocol, false);
831 /* Returns 'new_base' with any extensions taken from 'cur'. */
832 enum ofputil_protocol
833 ofputil_protocol_set_base(enum ofputil_protocol cur,
834 enum ofputil_protocol new_base)
836 bool tid = (cur & OFPUTIL_P_TID) != 0;
839 case OFPUTIL_P_OF10_STD:
840 case OFPUTIL_P_OF10_STD_TID:
841 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD, tid);
843 case OFPUTIL_P_OF10_NXM:
844 case OFPUTIL_P_OF10_NXM_TID:
845 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM, tid);
847 case OFPUTIL_P_OF11_STD:
848 return ofputil_protocol_set_tid(OFPUTIL_P_OF11_STD, tid);
850 case OFPUTIL_P_OF12_OXM:
851 return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM, tid);
853 case OFPUTIL_P_OF13_OXM:
854 return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM, tid);
856 case OFPUTIL_P_OF14_OXM:
857 return ofputil_protocol_set_tid(OFPUTIL_P_OF14_OXM, tid);
859 case OFPUTIL_P_OF15_OXM:
860 return ofputil_protocol_set_tid(OFPUTIL_P_OF15_OXM, tid);
867 /* Returns a string form of 'protocol', if a simple form exists (that is, if
868 * 'protocol' is either a single protocol or it is a combination of protocols
869 * that have a single abbreviation). Otherwise, returns NULL. */
871 ofputil_protocol_to_string(enum ofputil_protocol protocol)
873 const struct proto_abbrev *p;
875 /* Use a "switch" statement for single-bit names so that we get a compiler
876 * warning if we forget any. */
878 case OFPUTIL_P_OF10_NXM:
879 return "NXM-table_id";
881 case OFPUTIL_P_OF10_NXM_TID:
882 return "NXM+table_id";
884 case OFPUTIL_P_OF10_STD:
885 return "OpenFlow10-table_id";
887 case OFPUTIL_P_OF10_STD_TID:
888 return "OpenFlow10+table_id";
890 case OFPUTIL_P_OF11_STD:
893 case OFPUTIL_P_OF12_OXM:
894 return "OXM-OpenFlow12";
896 case OFPUTIL_P_OF13_OXM:
897 return "OXM-OpenFlow13";
899 case OFPUTIL_P_OF14_OXM:
900 return "OXM-OpenFlow14";
902 case OFPUTIL_P_OF15_OXM:
903 return "OXM-OpenFlow15";
906 /* Check abbreviations. */
907 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
908 if (protocol == p->protocol) {
916 /* Returns a string that represents 'protocols'. The return value might be a
917 * comma-separated list if 'protocols' doesn't have a simple name. The return
918 * value is "none" if 'protocols' is 0.
920 * The caller must free the returned string (with free()). */
922 ofputil_protocols_to_string(enum ofputil_protocol protocols)
926 ovs_assert(!(protocols & ~OFPUTIL_P_ANY));
927 if (protocols == 0) {
928 return xstrdup("none");
933 const struct proto_abbrev *p;
937 ds_put_char(&s, ',');
940 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
941 if ((protocols & p->protocol) == p->protocol) {
942 ds_put_cstr(&s, p->name);
943 protocols &= ~p->protocol;
948 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
949 enum ofputil_protocol bit = 1u << i;
951 if (protocols & bit) {
952 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
961 return ds_steal_cstr(&s);
964 static enum ofputil_protocol
965 ofputil_protocol_from_string__(const char *s, size_t n)
967 const struct proto_abbrev *p;
970 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
971 enum ofputil_protocol bit = 1u << i;
972 const char *name = ofputil_protocol_to_string(bit);
974 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
979 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
980 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
988 /* Returns the nonempty set of protocols represented by 's', which can be a
989 * single protocol name or abbreviation or a comma-separated list of them.
991 * Aborts the program with an error message if 's' is invalid. */
992 enum ofputil_protocol
993 ofputil_protocols_from_string(const char *s)
995 const char *orig_s = s;
996 enum ofputil_protocol protocols;
1000 enum ofputil_protocol p;
1003 n = strcspn(s, ",");
1009 p = ofputil_protocol_from_string__(s, n);
1011 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
1019 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
1025 ofputil_version_from_string(const char *s)
1027 if (!strcasecmp(s, "OpenFlow10")) {
1028 return OFP10_VERSION;
1030 if (!strcasecmp(s, "OpenFlow11")) {
1031 return OFP11_VERSION;
1033 if (!strcasecmp(s, "OpenFlow12")) {
1034 return OFP12_VERSION;
1036 if (!strcasecmp(s, "OpenFlow13")) {
1037 return OFP13_VERSION;
1039 if (!strcasecmp(s, "OpenFlow14")) {
1040 return OFP14_VERSION;
1042 if (!strcasecmp(s, "OpenFlow15")) {
1043 return OFP15_VERSION;
1049 is_delimiter(unsigned char c)
1051 return isspace(c) || c == ',';
1055 ofputil_versions_from_string(const char *s)
1058 uint32_t bitmap = 0;
1065 if (is_delimiter(s[i])) {
1070 while (s[i + j] && !is_delimiter(s[i + j])) {
1073 key = xmemdup0(s + i, j);
1074 version = ofputil_version_from_string(key);
1076 VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key);
1079 bitmap |= 1u << version;
1087 ofputil_versions_from_strings(char ** const s, size_t count)
1089 uint32_t bitmap = 0;
1092 int version = ofputil_version_from_string(s[count]);
1094 VLOG_WARN("Unknown OpenFlow version: \"%s\"", s[count]);
1096 bitmap |= 1u << version;
1104 ofputil_version_to_string(enum ofp_version ofp_version)
1106 switch (ofp_version) {
1108 return "OpenFlow10";
1110 return "OpenFlow11";
1112 return "OpenFlow12";
1114 return "OpenFlow13";
1116 return "OpenFlow14";
1118 return "OpenFlow15";
1125 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
1127 switch (packet_in_format) {
1128 case NXPIF_STANDARD:
1129 case NXPIF_NXT_PACKET_IN:
1130 case NXPIF_NXT_PACKET_IN2:
1138 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
1140 switch (packet_in_format) {
1141 case NXPIF_STANDARD:
1143 case NXPIF_NXT_PACKET_IN:
1144 return "nxt_packet_in";
1145 case NXPIF_NXT_PACKET_IN2:
1146 return "nxt_packet_in2";
1153 ofputil_packet_in_format_from_string(const char *s)
1155 return (!strcmp(s, "standard") || !strcmp(s, "openflow10")
1157 : !strcmp(s, "nxt_packet_in") || !strcmp(s, "nxm")
1158 ? NXPIF_NXT_PACKET_IN
1159 : !strcmp(s, "nxt_packet_in2")
1160 ? NXPIF_NXT_PACKET_IN2
1165 ofputil_format_version(struct ds *msg, enum ofp_version version)
1167 ds_put_format(msg, "0x%02x", version);
1171 ofputil_format_version_name(struct ds *msg, enum ofp_version version)
1173 ds_put_cstr(msg, ofputil_version_to_string(version));
1177 ofputil_format_version_bitmap__(struct ds *msg, uint32_t bitmap,
1178 void (*format_version)(struct ds *msg,
1182 format_version(msg, raw_ctz(bitmap));
1183 bitmap = zero_rightmost_1bit(bitmap);
1185 ds_put_cstr(msg, ", ");
1191 ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap)
1193 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version);
1197 ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap)
1199 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version_name);
1203 ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header *oheh,
1204 uint32_t *allowed_versionsp)
1206 uint16_t bitmap_len = ntohs(oheh->length) - sizeof *oheh;
1207 const ovs_be32 *bitmap = ALIGNED_CAST(const ovs_be32 *, oheh + 1);
1208 uint32_t allowed_versions;
1210 if (!bitmap_len || bitmap_len % sizeof *bitmap) {
1214 /* Only use the first 32-bit element of the bitmap as that is all the
1215 * current implementation supports. Subsequent elements are ignored which
1216 * should have no effect on session negotiation until Open vSwitch supports
1217 * wire-protocol versions greater than 31.
1219 allowed_versions = ntohl(bitmap[0]);
1221 if (allowed_versions & 1) {
1222 /* There's no OpenFlow version 0. */
1223 VLOG_WARN_RL(&bad_ofmsg_rl, "peer claims to support invalid OpenFlow "
1225 allowed_versions &= ~1u;
1228 if (!allowed_versions) {
1229 VLOG_WARN_RL(&bad_ofmsg_rl, "peer does not support any OpenFlow "
1230 "version (between 0x01 and 0x1f)");
1234 *allowed_versionsp = allowed_versions;
1239 version_bitmap_from_version(uint8_t ofp_version)
1241 return ((ofp_version < 32 ? 1u << ofp_version : 0) - 1) << 1;
1244 /* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions'
1245 * the set of OpenFlow versions for which 'oh' announces support.
1247 * Because of how OpenFlow defines OFPT_HELLO messages, this function is always
1248 * successful, and thus '*allowed_versions' is always initialized. However, it
1249 * returns false if 'oh' contains some data that could not be fully understood,
1250 * true if 'oh' was completely parsed. */
1252 ofputil_decode_hello(const struct ofp_header *oh, uint32_t *allowed_versions)
1254 struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
1255 ofpbuf_pull(&msg, sizeof *oh);
1257 *allowed_versions = version_bitmap_from_version(oh->version);
1261 const struct ofp_hello_elem_header *oheh;
1264 if (msg.size < sizeof *oheh) {
1269 len = ntohs(oheh->length);
1270 if (len < sizeof *oheh || !ofpbuf_try_pull(&msg, ROUND_UP(len, 8))) {
1274 if (oheh->type != htons(OFPHET_VERSIONBITMAP)
1275 || !ofputil_decode_hello_bitmap(oheh, allowed_versions)) {
1283 /* Returns true if 'allowed_versions' needs to be accompanied by a version
1284 * bitmap to be correctly expressed in an OFPT_HELLO message. */
1286 should_send_version_bitmap(uint32_t allowed_versions)
1288 return !is_pow2((allowed_versions >> 1) + 1);
1291 /* Create an OFPT_HELLO message that expresses support for the OpenFlow
1292 * versions in the 'allowed_versions' bitmaps and returns the message. */
1294 ofputil_encode_hello(uint32_t allowed_versions)
1296 enum ofp_version ofp_version;
1299 ofp_version = leftmost_1bit_idx(allowed_versions);
1300 msg = ofpraw_alloc(OFPRAW_OFPT_HELLO, ofp_version, 0);
1302 if (should_send_version_bitmap(allowed_versions)) {
1303 struct ofp_hello_elem_header *oheh;
1306 map_len = sizeof allowed_versions;
1307 oheh = ofpbuf_put_zeros(msg, ROUND_UP(map_len + sizeof *oheh, 8));
1308 oheh->type = htons(OFPHET_VERSIONBITMAP);
1309 oheh->length = htons(map_len + sizeof *oheh);
1310 *ALIGNED_CAST(ovs_be32 *, oheh + 1) = htonl(allowed_versions);
1312 ofpmsg_update_length(msg);
1318 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1319 * protocol is 'current', at least partly transitions the protocol to 'want'.
1320 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1321 * connection if the switch processes the returned message correctly. (If
1322 * '*next != want' then the caller will have to iterate.)
1324 * If 'current == want', or if it is not possible to transition from 'current'
1325 * to 'want' (because, for example, 'current' and 'want' use different OpenFlow
1326 * protocol versions), returns NULL and stores 'current' in '*next'. */
1328 ofputil_encode_set_protocol(enum ofputil_protocol current,
1329 enum ofputil_protocol want,
1330 enum ofputil_protocol *next)
1332 enum ofp_version cur_version, want_version;
1333 enum ofputil_protocol cur_base, want_base;
1334 bool cur_tid, want_tid;
1336 cur_version = ofputil_protocol_to_ofp_version(current);
1337 want_version = ofputil_protocol_to_ofp_version(want);
1338 if (cur_version != want_version) {
1343 cur_base = ofputil_protocol_to_base(current);
1344 want_base = ofputil_protocol_to_base(want);
1345 if (cur_base != want_base) {
1346 *next = ofputil_protocol_set_base(current, want_base);
1348 switch (want_base) {
1349 case OFPUTIL_P_OF10_NXM:
1350 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1352 case OFPUTIL_P_OF10_STD:
1353 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1355 case OFPUTIL_P_OF11_STD:
1356 case OFPUTIL_P_OF12_OXM:
1357 case OFPUTIL_P_OF13_OXM:
1358 case OFPUTIL_P_OF14_OXM:
1359 case OFPUTIL_P_OF15_OXM:
1360 /* There is only one variant of each OpenFlow 1.1+ protocol, and we
1361 * verified above that we're not trying to change versions. */
1364 case OFPUTIL_P_OF10_STD_TID:
1365 case OFPUTIL_P_OF10_NXM_TID:
1370 cur_tid = (current & OFPUTIL_P_TID) != 0;
1371 want_tid = (want & OFPUTIL_P_TID) != 0;
1372 if (cur_tid != want_tid) {
1373 *next = ofputil_protocol_set_tid(current, want_tid);
1374 return ofputil_make_flow_mod_table_id(want_tid);
1377 ovs_assert(current == want);
1383 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1384 * format to 'nxff'. */
1386 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1388 struct nx_set_flow_format *sff;
1391 ovs_assert(ofputil_nx_flow_format_is_valid(nxff));
1393 msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1394 sff = ofpbuf_put_zeros(msg, sizeof *sff);
1395 sff->format = htonl(nxff);
1400 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1402 enum ofputil_protocol
1403 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1405 switch (flow_format) {
1406 case NXFF_OPENFLOW10:
1407 return OFPUTIL_P_OF10_STD;
1410 return OFPUTIL_P_OF10_NXM;
1417 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1419 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1421 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1424 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1427 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1429 switch (flow_format) {
1430 case NXFF_OPENFLOW10:
1431 return "openflow10";
1440 ofputil_make_set_packet_in_format(enum ofp_version ofp_version,
1441 enum nx_packet_in_format packet_in_format)
1443 struct nx_set_packet_in_format *spif;
1446 msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, ofp_version, 0);
1447 spif = ofpbuf_put_zeros(msg, sizeof *spif);
1448 spif->format = htonl(packet_in_format);
1453 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1454 * extension on or off (according to 'flow_mod_table_id'). */
1456 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1458 struct nx_flow_mod_table_id *nfmti;
1461 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1462 nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
1463 nfmti->set = flow_mod_table_id;
1467 struct ofputil_flow_mod_flag {
1469 enum ofp_version min_version, max_version;
1470 enum ofputil_flow_mod_flags flag;
1473 static const struct ofputil_flow_mod_flag ofputil_flow_mod_flags[] = {
1474 { OFPFF_SEND_FLOW_REM, OFP10_VERSION, 0, OFPUTIL_FF_SEND_FLOW_REM },
1475 { OFPFF_CHECK_OVERLAP, OFP10_VERSION, 0, OFPUTIL_FF_CHECK_OVERLAP },
1476 { OFPFF10_EMERG, OFP10_VERSION, OFP10_VERSION,
1478 { OFPFF12_RESET_COUNTS, OFP12_VERSION, 0, OFPUTIL_FF_RESET_COUNTS },
1479 { OFPFF13_NO_PKT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_PKT_COUNTS },
1480 { OFPFF13_NO_BYT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_BYT_COUNTS },
1485 ofputil_decode_flow_mod_flags(ovs_be16 raw_flags_,
1486 enum ofp_flow_mod_command command,
1487 enum ofp_version version,
1488 enum ofputil_flow_mod_flags *flagsp)
1490 uint16_t raw_flags = ntohs(raw_flags_);
1491 const struct ofputil_flow_mod_flag *f;
1494 for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1495 if (raw_flags & f->raw_flag
1496 && version >= f->min_version
1497 && (!f->max_version || version <= f->max_version)) {
1498 raw_flags &= ~f->raw_flag;
1503 /* In OF1.0 and OF1.1, "add" always resets counters, and other commands
1506 * In OF1.2 and later, OFPFF12_RESET_COUNTS controls whether each command
1507 * resets counters. */
1508 if ((version == OFP10_VERSION || version == OFP11_VERSION)
1509 && command == OFPFC_ADD) {
1510 *flagsp |= OFPUTIL_FF_RESET_COUNTS;
1513 return raw_flags ? OFPERR_OFPFMFC_BAD_FLAGS : 0;
1517 ofputil_encode_flow_mod_flags(enum ofputil_flow_mod_flags flags,
1518 enum ofp_version version)
1520 const struct ofputil_flow_mod_flag *f;
1524 for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1526 && version >= f->min_version
1527 && (!f->max_version || version <= f->max_version)) {
1528 raw_flags |= f->raw_flag;
1532 return htons(raw_flags);
1535 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1536 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1539 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1540 * The caller must initialize 'ofpacts' and retains ownership of it.
1541 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1543 * Does not validate the flow_mod actions. The caller should do that, with
1544 * ofpacts_check(). */
1546 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1547 const struct ofp_header *oh,
1548 enum ofputil_protocol protocol,
1549 struct ofpbuf *ofpacts,
1550 ofp_port_t max_port, uint8_t max_table)
1555 /* Ignored for non-delete actions */
1556 fm->delete_reason = OFPRR_DELETE;
1558 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
1559 enum ofpraw raw = ofpraw_pull_assert(&b);
1560 if (raw == OFPRAW_OFPT11_FLOW_MOD) {
1561 /* Standard OpenFlow 1.1+ flow_mod. */
1562 const struct ofp11_flow_mod *ofm;
1564 ofm = ofpbuf_pull(&b, sizeof *ofm);
1566 error = ofputil_pull_ofp11_match(&b, &fm->match, NULL);
1571 /* Translate the message. */
1572 fm->priority = ntohs(ofm->priority);
1573 if (ofm->command == OFPFC_ADD
1574 || (oh->version == OFP11_VERSION
1575 && (ofm->command == OFPFC_MODIFY ||
1576 ofm->command == OFPFC_MODIFY_STRICT)
1577 && ofm->cookie_mask == htonll(0))) {
1578 /* In OpenFlow 1.1 only, a "modify" or "modify-strict" that does
1579 * not match on the cookie is treated as an "add" if there is no
1581 fm->cookie = htonll(0);
1582 fm->cookie_mask = htonll(0);
1583 fm->new_cookie = ofm->cookie;
1585 fm->cookie = ofm->cookie;
1586 fm->cookie_mask = ofm->cookie_mask;
1587 fm->new_cookie = OVS_BE64_MAX;
1589 fm->modify_cookie = false;
1590 fm->command = ofm->command;
1594 * OF1.1 entirely forbids table_id == OFPTT_ALL.
1595 * OF1.2+ allows table_id == OFPTT_ALL only for deletes. */
1596 fm->table_id = ofm->table_id;
1597 if (fm->table_id == OFPTT_ALL
1598 && (oh->version == OFP11_VERSION
1599 || (ofm->command != OFPFC_DELETE &&
1600 ofm->command != OFPFC_DELETE_STRICT))) {
1601 return OFPERR_OFPFMFC_BAD_TABLE_ID;
1604 fm->idle_timeout = ntohs(ofm->idle_timeout);
1605 fm->hard_timeout = ntohs(ofm->hard_timeout);
1606 if (oh->version >= OFP14_VERSION && ofm->command == OFPFC_ADD) {
1607 fm->importance = ntohs(ofm->importance);
1611 fm->buffer_id = ntohl(ofm->buffer_id);
1612 error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
1617 fm->out_group = (ofm->command == OFPFC_DELETE ||
1618 ofm->command == OFPFC_DELETE_STRICT
1619 ? ntohl(ofm->out_group)
1621 raw_flags = ofm->flags;
1625 if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1626 /* Standard OpenFlow 1.0 flow_mod. */
1627 const struct ofp10_flow_mod *ofm;
1629 /* Get the ofp10_flow_mod. */
1630 ofm = ofpbuf_pull(&b, sizeof *ofm);
1632 /* Translate the rule. */
1633 ofputil_match_from_ofp10_match(&ofm->match, &fm->match);
1634 ofputil_normalize_match(&fm->match);
1636 /* OpenFlow 1.0 says that exact-match rules have to have the
1637 * highest possible priority. */
1638 fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL)
1639 ? ntohs(ofm->priority)
1642 /* Translate the message. */
1643 command = ntohs(ofm->command);
1644 fm->cookie = htonll(0);
1645 fm->cookie_mask = htonll(0);
1646 fm->new_cookie = ofm->cookie;
1647 fm->idle_timeout = ntohs(ofm->idle_timeout);
1648 fm->hard_timeout = ntohs(ofm->hard_timeout);
1650 fm->buffer_id = ntohl(ofm->buffer_id);
1651 fm->out_port = u16_to_ofp(ntohs(ofm->out_port));
1652 fm->out_group = OFPG_ANY;
1653 raw_flags = ofm->flags;
1654 } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1655 /* Nicira extended flow_mod. */
1656 const struct nx_flow_mod *nfm;
1658 /* Dissect the message. */
1659 nfm = ofpbuf_pull(&b, sizeof *nfm);
1660 error = nx_pull_match(&b, ntohs(nfm->match_len),
1661 &fm->match, &fm->cookie, &fm->cookie_mask);
1666 /* Translate the message. */
1667 command = ntohs(nfm->command);
1668 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1669 /* Flow additions may only set a new cookie, not match an
1670 * existing cookie. */
1671 return OFPERR_NXBRC_NXM_INVALID;
1673 fm->priority = ntohs(nfm->priority);
1674 fm->new_cookie = nfm->cookie;
1675 fm->idle_timeout = ntohs(nfm->idle_timeout);
1676 fm->hard_timeout = ntohs(nfm->hard_timeout);
1678 fm->buffer_id = ntohl(nfm->buffer_id);
1679 fm->out_port = u16_to_ofp(ntohs(nfm->out_port));
1680 fm->out_group = OFPG_ANY;
1681 raw_flags = nfm->flags;
1686 fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX;
1687 if (protocol & OFPUTIL_P_TID) {
1688 fm->command = command & 0xff;
1689 fm->table_id = command >> 8;
1691 if (command > 0xff) {
1692 VLOG_WARN_RL(&bad_ofmsg_rl, "flow_mod has explicit table_id "
1693 "but flow_mod_table_id extension is not enabled");
1695 fm->command = command;
1696 fm->table_id = 0xff;
1700 if (fm->command > OFPFC_DELETE_STRICT) {
1701 return OFPERR_OFPFMFC_BAD_COMMAND;
1704 error = ofpacts_pull_openflow_instructions(&b, b.size,
1705 oh->version, ofpacts);
1709 fm->ofpacts = ofpacts->data;
1710 fm->ofpacts_len = ofpacts->size;
1712 error = ofputil_decode_flow_mod_flags(raw_flags, fm->command,
1713 oh->version, &fm->flags);
1718 if (fm->flags & OFPUTIL_FF_EMERG) {
1719 /* We do not support the OpenFlow 1.0 emergency flow cache, which
1720 * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
1722 * OpenFlow 1.0 specifies the error code to use when idle_timeout
1723 * or hard_timeout is nonzero. Otherwise, there is no good error
1724 * code, so just state that the flow table is full. */
1725 return (fm->hard_timeout || fm->idle_timeout
1726 ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT
1727 : OFPERR_OFPFMFC_TABLE_FULL);
1730 return ofpacts_check_consistency(fm->ofpacts, fm->ofpacts_len,
1731 &fm->match.flow, max_port,
1732 fm->table_id, max_table, protocol);
1736 ofputil_pull_bands(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
1737 struct ofpbuf *bands)
1739 const struct ofp13_meter_band_header *ombh;
1740 struct ofputil_meter_band *mb;
1743 ombh = ofpbuf_try_pull(msg, len);
1745 return OFPERR_OFPBRC_BAD_LEN;
1748 while (len >= sizeof (struct ofp13_meter_band_drop)) {
1749 size_t ombh_len = ntohs(ombh->len);
1750 /* All supported band types have the same length. */
1751 if (ombh_len != sizeof (struct ofp13_meter_band_drop)) {
1752 return OFPERR_OFPBRC_BAD_LEN;
1754 mb = ofpbuf_put_uninit(bands, sizeof *mb);
1755 mb->type = ntohs(ombh->type);
1756 if (mb->type != OFPMBT13_DROP && mb->type != OFPMBT13_DSCP_REMARK) {
1757 return OFPERR_OFPMMFC_BAD_BAND;
1759 mb->rate = ntohl(ombh->rate);
1760 mb->burst_size = ntohl(ombh->burst_size);
1761 mb->prec_level = (mb->type == OFPMBT13_DSCP_REMARK) ?
1762 ((struct ofp13_meter_band_dscp_remark *)ombh)->prec_level : 0;
1765 ombh = ALIGNED_CAST(struct ofp13_meter_band_header *,
1766 (char *) ombh + ombh_len);
1769 return OFPERR_OFPBRC_BAD_LEN;
1776 ofputil_decode_meter_mod(const struct ofp_header *oh,
1777 struct ofputil_meter_mod *mm,
1778 struct ofpbuf *bands)
1780 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
1781 ofpraw_pull_assert(&b);
1782 const struct ofp13_meter_mod *omm = ofpbuf_pull(&b, sizeof *omm);
1784 /* Translate the message. */
1785 mm->command = ntohs(omm->command);
1786 if (mm->command != OFPMC13_ADD &&
1787 mm->command != OFPMC13_MODIFY &&
1788 mm->command != OFPMC13_DELETE) {
1789 return OFPERR_OFPMMFC_BAD_COMMAND;
1791 mm->meter.meter_id = ntohl(omm->meter_id);
1793 if (mm->command == OFPMC13_DELETE) {
1794 mm->meter.flags = 0;
1795 mm->meter.n_bands = 0;
1796 mm->meter.bands = NULL;
1800 mm->meter.flags = ntohs(omm->flags);
1801 if (mm->meter.flags & OFPMF13_KBPS &&
1802 mm->meter.flags & OFPMF13_PKTPS) {
1803 return OFPERR_OFPMMFC_BAD_FLAGS;
1805 mm->meter.bands = bands->data;
1807 error = ofputil_pull_bands(&b, b.size, &mm->meter.n_bands, bands);
1816 ofputil_decode_meter_request(const struct ofp_header *oh, uint32_t *meter_id)
1818 const struct ofp13_meter_multipart_request *omr = ofpmsg_body(oh);
1819 *meter_id = ntohl(omr->meter_id);
1823 ofputil_encode_meter_request(enum ofp_version ofp_version,
1824 enum ofputil_meter_request_type type,
1832 case OFPUTIL_METER_CONFIG:
1833 raw = OFPRAW_OFPST13_METER_CONFIG_REQUEST;
1835 case OFPUTIL_METER_STATS:
1836 raw = OFPRAW_OFPST13_METER_REQUEST;
1839 case OFPUTIL_METER_FEATURES:
1840 raw = OFPRAW_OFPST13_METER_FEATURES_REQUEST;
1844 msg = ofpraw_alloc(raw, ofp_version, 0);
1846 if (type != OFPUTIL_METER_FEATURES) {
1847 struct ofp13_meter_multipart_request *omr;
1848 omr = ofpbuf_put_zeros(msg, sizeof *omr);
1849 omr->meter_id = htonl(meter_id);
1855 ofputil_put_bands(uint16_t n_bands, const struct ofputil_meter_band *mb,
1860 for (n = 0; n < n_bands; ++n) {
1861 /* Currently all band types have same size. */
1862 struct ofp13_meter_band_dscp_remark *ombh;
1863 size_t ombh_len = sizeof *ombh;
1865 ombh = ofpbuf_put_zeros(msg, ombh_len);
1867 ombh->type = htons(mb->type);
1868 ombh->len = htons(ombh_len);
1869 ombh->rate = htonl(mb->rate);
1870 ombh->burst_size = htonl(mb->burst_size);
1871 ombh->prec_level = mb->prec_level;
1877 /* Encode a meter stat for 'mc' and append it to 'replies'. */
1879 ofputil_append_meter_config(struct ovs_list *replies,
1880 const struct ofputil_meter_config *mc)
1882 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
1883 size_t start_ofs = msg->size;
1884 struct ofp13_meter_config *reply;
1886 ofpbuf_put_uninit(msg, sizeof *reply);
1887 ofputil_put_bands(mc->n_bands, mc->bands, msg);
1889 reply = ofpbuf_at_assert(msg, start_ofs, sizeof *reply);
1890 reply->flags = htons(mc->flags);
1891 reply->meter_id = htonl(mc->meter_id);
1892 reply->length = htons(msg->size - start_ofs);
1894 ofpmp_postappend(replies, start_ofs);
1897 /* Encode a meter stat for 'ms' and append it to 'replies'. */
1899 ofputil_append_meter_stats(struct ovs_list *replies,
1900 const struct ofputil_meter_stats *ms)
1902 struct ofp13_meter_stats *reply;
1906 len = sizeof *reply + ms->n_bands * sizeof(struct ofp13_meter_band_stats);
1907 reply = ofpmp_append(replies, len);
1909 reply->meter_id = htonl(ms->meter_id);
1910 reply->len = htons(len);
1911 memset(reply->pad, 0, sizeof reply->pad);
1912 reply->flow_count = htonl(ms->flow_count);
1913 reply->packet_in_count = htonll(ms->packet_in_count);
1914 reply->byte_in_count = htonll(ms->byte_in_count);
1915 reply->duration_sec = htonl(ms->duration_sec);
1916 reply->duration_nsec = htonl(ms->duration_nsec);
1918 for (n = 0; n < ms->n_bands; ++n) {
1919 const struct ofputil_meter_band_stats *src = &ms->bands[n];
1920 struct ofp13_meter_band_stats *dst = &reply->band_stats[n];
1922 dst->packet_band_count = htonll(src->packet_count);
1923 dst->byte_band_count = htonll(src->byte_count);
1927 /* Converts an OFPMP_METER_CONFIG reply in 'msg' into an abstract
1928 * ofputil_meter_config in 'mc', with mc->bands pointing to bands decoded into
1929 * 'bands'. The caller must have initialized 'bands' and retains ownership of
1930 * it across the call.
1932 * Multiple OFPST13_METER_CONFIG replies can be packed into a single OpenFlow
1933 * message. Calling this function multiple times for a single 'msg' iterates
1934 * through the replies. 'bands' is cleared for each reply.
1936 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1937 * otherwise a positive errno value. */
1939 ofputil_decode_meter_config(struct ofpbuf *msg,
1940 struct ofputil_meter_config *mc,
1941 struct ofpbuf *bands)
1943 const struct ofp13_meter_config *omc;
1946 /* Pull OpenFlow headers for the first call. */
1948 ofpraw_pull_assert(msg);
1955 omc = ofpbuf_try_pull(msg, sizeof *omc);
1957 VLOG_WARN_RL(&bad_ofmsg_rl,
1958 "OFPMP_METER_CONFIG reply has %"PRIu32" leftover bytes at end",
1960 return OFPERR_OFPBRC_BAD_LEN;
1963 ofpbuf_clear(bands);
1964 err = ofputil_pull_bands(msg, ntohs(omc->length) - sizeof *omc,
1965 &mc->n_bands, bands);
1969 mc->meter_id = ntohl(omc->meter_id);
1970 mc->flags = ntohs(omc->flags);
1971 mc->bands = bands->data;
1977 ofputil_pull_band_stats(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
1978 struct ofpbuf *bands)
1980 const struct ofp13_meter_band_stats *ombs;
1981 struct ofputil_meter_band_stats *mbs;
1984 ombs = ofpbuf_try_pull(msg, len);
1986 return OFPERR_OFPBRC_BAD_LEN;
1989 n = len / sizeof *ombs;
1990 if (len != n * sizeof *ombs) {
1991 return OFPERR_OFPBRC_BAD_LEN;
1994 mbs = ofpbuf_put_uninit(bands, len);
1996 for (i = 0; i < n; ++i) {
1997 mbs[i].packet_count = ntohll(ombs[i].packet_band_count);
1998 mbs[i].byte_count = ntohll(ombs[i].byte_band_count);
2004 /* Converts an OFPMP_METER reply in 'msg' into an abstract
2005 * ofputil_meter_stats in 'ms', with ms->bands pointing to band stats
2006 * decoded into 'bands'.
2008 * Multiple OFPMP_METER replies can be packed into a single OpenFlow
2009 * message. Calling this function multiple times for a single 'msg' iterates
2010 * through the replies. 'bands' is cleared for each reply.
2012 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2013 * otherwise a positive errno value. */
2015 ofputil_decode_meter_stats(struct ofpbuf *msg,
2016 struct ofputil_meter_stats *ms,
2017 struct ofpbuf *bands)
2019 const struct ofp13_meter_stats *oms;
2022 /* Pull OpenFlow headers for the first call. */
2024 ofpraw_pull_assert(msg);
2031 oms = ofpbuf_try_pull(msg, sizeof *oms);
2033 VLOG_WARN_RL(&bad_ofmsg_rl,
2034 "OFPMP_METER reply has %"PRIu32" leftover bytes at end",
2036 return OFPERR_OFPBRC_BAD_LEN;
2039 ofpbuf_clear(bands);
2040 err = ofputil_pull_band_stats(msg, ntohs(oms->len) - sizeof *oms,
2041 &ms->n_bands, bands);
2045 ms->meter_id = ntohl(oms->meter_id);
2046 ms->flow_count = ntohl(oms->flow_count);
2047 ms->packet_in_count = ntohll(oms->packet_in_count);
2048 ms->byte_in_count = ntohll(oms->byte_in_count);
2049 ms->duration_sec = ntohl(oms->duration_sec);
2050 ms->duration_nsec = ntohl(oms->duration_nsec);
2051 ms->bands = bands->data;
2057 ofputil_decode_meter_features(const struct ofp_header *oh,
2058 struct ofputil_meter_features *mf)
2060 const struct ofp13_meter_features *omf = ofpmsg_body(oh);
2062 mf->max_meters = ntohl(omf->max_meter);
2063 mf->band_types = ntohl(omf->band_types);
2064 mf->capabilities = ntohl(omf->capabilities);
2065 mf->max_bands = omf->max_bands;
2066 mf->max_color = omf->max_color;
2070 ofputil_encode_meter_features_reply(const struct ofputil_meter_features *mf,
2071 const struct ofp_header *request)
2073 struct ofpbuf *reply;
2074 struct ofp13_meter_features *omf;
2076 reply = ofpraw_alloc_stats_reply(request, 0);
2077 omf = ofpbuf_put_zeros(reply, sizeof *omf);
2079 omf->max_meter = htonl(mf->max_meters);
2080 omf->band_types = htonl(mf->band_types);
2081 omf->capabilities = htonl(mf->capabilities);
2082 omf->max_bands = mf->max_bands;
2083 omf->max_color = mf->max_color;
2089 ofputil_encode_meter_mod(enum ofp_version ofp_version,
2090 const struct ofputil_meter_mod *mm)
2094 struct ofp13_meter_mod *omm;
2096 msg = ofpraw_alloc(OFPRAW_OFPT13_METER_MOD, ofp_version,
2097 NXM_TYPICAL_LEN + mm->meter.n_bands * 16);
2098 omm = ofpbuf_put_zeros(msg, sizeof *omm);
2099 omm->command = htons(mm->command);
2100 if (mm->command != OFPMC13_DELETE) {
2101 omm->flags = htons(mm->meter.flags);
2103 omm->meter_id = htonl(mm->meter.meter_id);
2105 ofputil_put_bands(mm->meter.n_bands, mm->meter.bands, msg);
2107 ofpmsg_update_length(msg);
2112 ofputil_tid_command(const struct ofputil_flow_mod *fm,
2113 enum ofputil_protocol protocol)
2115 return htons(protocol & OFPUTIL_P_TID
2116 ? (fm->command & 0xff) | (fm->table_id << 8)
2120 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
2121 * 'protocol' and returns the message. */
2123 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
2124 enum ofputil_protocol protocol)
2126 enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
2127 ovs_be16 raw_flags = ofputil_encode_flow_mod_flags(fm->flags, version);
2131 case OFPUTIL_P_OF11_STD:
2132 case OFPUTIL_P_OF12_OXM:
2133 case OFPUTIL_P_OF13_OXM:
2134 case OFPUTIL_P_OF14_OXM:
2135 case OFPUTIL_P_OF15_OXM: {
2136 struct ofp11_flow_mod *ofm;
2139 tailroom = ofputil_match_typical_len(protocol) + fm->ofpacts_len;
2140 msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, version, tailroom);
2141 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
2142 if ((protocol == OFPUTIL_P_OF11_STD
2143 && (fm->command == OFPFC_MODIFY ||
2144 fm->command == OFPFC_MODIFY_STRICT)
2145 && fm->cookie_mask == htonll(0))
2146 || fm->command == OFPFC_ADD) {
2147 ofm->cookie = fm->new_cookie;
2149 ofm->cookie = fm->cookie & fm->cookie_mask;
2151 ofm->cookie_mask = fm->cookie_mask;
2152 if (fm->table_id != OFPTT_ALL
2153 || (protocol != OFPUTIL_P_OF11_STD
2154 && (fm->command == OFPFC_DELETE ||
2155 fm->command == OFPFC_DELETE_STRICT))) {
2156 ofm->table_id = fm->table_id;
2160 ofm->command = fm->command;
2161 ofm->idle_timeout = htons(fm->idle_timeout);
2162 ofm->hard_timeout = htons(fm->hard_timeout);
2163 ofm->priority = htons(fm->priority);
2164 ofm->buffer_id = htonl(fm->buffer_id);
2165 ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
2166 ofm->out_group = htonl(fm->out_group);
2167 ofm->flags = raw_flags;
2168 if (version >= OFP14_VERSION && fm->command == OFPFC_ADD) {
2169 ofm->importance = htons(fm->importance);
2171 ofm->importance = 0;
2173 ofputil_put_ofp11_match(msg, &fm->match, protocol);
2174 ofpacts_put_openflow_instructions(fm->ofpacts, fm->ofpacts_len, msg,
2179 case OFPUTIL_P_OF10_STD:
2180 case OFPUTIL_P_OF10_STD_TID: {
2181 struct ofp10_flow_mod *ofm;
2183 msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
2185 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
2186 ofputil_match_to_ofp10_match(&fm->match, &ofm->match);
2187 ofm->cookie = fm->new_cookie;
2188 ofm->command = ofputil_tid_command(fm, protocol);
2189 ofm->idle_timeout = htons(fm->idle_timeout);
2190 ofm->hard_timeout = htons(fm->hard_timeout);
2191 ofm->priority = htons(fm->priority);
2192 ofm->buffer_id = htonl(fm->buffer_id);
2193 ofm->out_port = htons(ofp_to_u16(fm->out_port));
2194 ofm->flags = raw_flags;
2195 ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2200 case OFPUTIL_P_OF10_NXM:
2201 case OFPUTIL_P_OF10_NXM_TID: {
2202 struct nx_flow_mod *nfm;
2205 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
2206 NXM_TYPICAL_LEN + fm->ofpacts_len);
2207 nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
2208 nfm->command = ofputil_tid_command(fm, protocol);
2209 nfm->cookie = fm->new_cookie;
2210 match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask);
2212 nfm->idle_timeout = htons(fm->idle_timeout);
2213 nfm->hard_timeout = htons(fm->hard_timeout);
2214 nfm->priority = htons(fm->priority);
2215 nfm->buffer_id = htonl(fm->buffer_id);
2216 nfm->out_port = htons(ofp_to_u16(fm->out_port));
2217 nfm->flags = raw_flags;
2218 nfm->match_len = htons(match_len);
2219 ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2228 ofpmsg_update_length(msg);
2233 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr,
2234 const struct ofp10_flow_stats_request *ofsr,
2237 fsr->aggregate = aggregate;
2238 ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match);
2239 fsr->out_port = u16_to_ofp(ntohs(ofsr->out_port));
2240 fsr->out_group = OFPG_ANY;
2241 fsr->table_id = ofsr->table_id;
2242 fsr->cookie = fsr->cookie_mask = htonll(0);
2248 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr,
2249 struct ofpbuf *b, bool aggregate)
2251 const struct ofp11_flow_stats_request *ofsr;
2254 ofsr = ofpbuf_pull(b, sizeof *ofsr);
2255 fsr->aggregate = aggregate;
2256 fsr->table_id = ofsr->table_id;
2257 error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port);
2261 fsr->out_group = ntohl(ofsr->out_group);
2262 fsr->cookie = ofsr->cookie;
2263 fsr->cookie_mask = ofsr->cookie_mask;
2264 error = ofputil_pull_ofp11_match(b, &fsr->match, NULL);
2273 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
2274 struct ofpbuf *b, bool aggregate)
2276 const struct nx_flow_stats_request *nfsr;
2279 nfsr = ofpbuf_pull(b, sizeof *nfsr);
2280 error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match,
2281 &fsr->cookie, &fsr->cookie_mask);
2286 return OFPERR_OFPBRC_BAD_LEN;
2289 fsr->aggregate = aggregate;
2290 fsr->out_port = u16_to_ofp(ntohs(nfsr->out_port));
2291 fsr->out_group = OFPG_ANY;
2292 fsr->table_id = nfsr->table_id;
2297 /* Constructs and returns an OFPT_QUEUE_GET_CONFIG request for the specified
2298 * 'port' and 'queue', suitable for OpenFlow version 'version'.
2300 * 'queue' is honored only for OpenFlow 1.4 and later; older versions always
2301 * request all queues. */
2303 ofputil_encode_queue_get_config_request(enum ofp_version version,
2307 struct ofpbuf *request;
2309 if (version == OFP10_VERSION) {
2310 struct ofp10_queue_get_config_request *qgcr10;
2312 request = ofpraw_alloc(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST,
2314 qgcr10 = ofpbuf_put_zeros(request, sizeof *qgcr10);
2315 qgcr10->port = htons(ofp_to_u16(port));
2316 } else if (version < OFP14_VERSION) {
2317 struct ofp11_queue_get_config_request *qgcr11;
2319 request = ofpraw_alloc(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST,
2321 qgcr11 = ofpbuf_put_zeros(request, sizeof *qgcr11);
2322 qgcr11->port = ofputil_port_to_ofp11(port);
2324 struct ofp14_queue_desc_request *qdr14;
2326 request = ofpraw_alloc(OFPRAW_OFPST14_QUEUE_DESC_REQUEST,
2328 qdr14 = ofpbuf_put_zeros(request, sizeof *qdr14);
2329 qdr14->port = ofputil_port_to_ofp11(port);
2330 qdr14->queue = htonl(queue);
2336 /* Parses OFPT_QUEUE_GET_CONFIG request 'oh', storing the port specified by the
2337 * request into '*port'. Returns 0 if successful, otherwise an OpenFlow error
2340 ofputil_decode_queue_get_config_request(const struct ofp_header *oh,
2341 ofp_port_t *port, uint32_t *queue)
2343 const struct ofp10_queue_get_config_request *qgcr10;
2344 const struct ofp11_queue_get_config_request *qgcr11;
2345 const struct ofp14_queue_desc_request *qdr14;
2346 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
2347 enum ofpraw raw = ofpraw_pull_assert(&b);
2349 switch ((int) raw) {
2350 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2352 *port = u16_to_ofp(ntohs(qgcr10->port));
2356 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2359 enum ofperr error = ofputil_port_from_ofp11(qgcr11->port, port);
2360 if (error || *port == OFPP_ANY) {
2365 case OFPRAW_OFPST14_QUEUE_DESC_REQUEST:
2367 *queue = ntohl(qdr14->queue);
2368 return ofputil_port_from_ofp11(qdr14->port, port);
2374 return (ofp_to_u16(*port) < ofp_to_u16(OFPP_MAX)
2376 : OFPERR_OFPQOFC_BAD_PORT);
2379 /* Constructs and returns the beginning of a reply to
2380 * OFPT_QUEUE_GET_CONFIG_REQUEST or OFPMP_QUEUE_DESC request 'oh'. The caller
2381 * may append information about individual queues with
2382 * ofputil_append_queue_get_config_reply(). */
2384 ofputil_start_queue_get_config_reply(const struct ofp_header *request,
2385 struct ovs_list *replies)
2387 struct ofpbuf *reply;
2392 error = ofputil_decode_queue_get_config_request(request, &port, &queue);
2395 enum ofpraw raw = ofpraw_decode_assert(request);
2396 switch ((int) raw) {
2397 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2398 reply = ofpraw_alloc_reply(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY,
2400 struct ofp10_queue_get_config_reply *qgcr10
2401 = ofpbuf_put_zeros(reply, sizeof *qgcr10);
2402 qgcr10->port = htons(ofp_to_u16(port));
2405 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2406 reply = ofpraw_alloc_reply(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY,
2408 struct ofp11_queue_get_config_reply *qgcr11
2409 = ofpbuf_put_zeros(reply, sizeof *qgcr11);
2410 qgcr11->port = ofputil_port_to_ofp11(port);
2413 case OFPRAW_OFPST14_QUEUE_DESC_REQUEST:
2414 reply = ofpraw_alloc_stats_reply(request, 0);
2422 list_push_back(replies, &reply->list_node);
2426 put_ofp10_queue_rate(struct ofpbuf *reply,
2427 enum ofp10_queue_properties property, uint16_t rate)
2429 if (rate != UINT16_MAX) {
2430 struct ofp10_queue_prop_rate *oqpr;
2432 oqpr = ofpbuf_put_zeros(reply, sizeof *oqpr);
2433 oqpr->prop_header.property = htons(property);
2434 oqpr->prop_header.len = htons(sizeof *oqpr);
2435 oqpr->rate = htons(rate);
2440 put_ofp14_queue_rate(struct ofpbuf *reply,
2441 enum ofp14_queue_desc_prop_type type, uint16_t rate)
2443 if (rate != UINT16_MAX) {
2444 ofpprop_put_u16(reply, type, rate);
2449 ofputil_append_queue_get_config_reply(const struct ofputil_queue_config *qc,
2450 struct ovs_list *replies)
2452 enum ofp_version ofp_version = ofpmp_version(replies);
2453 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
2454 size_t start_ofs = reply->size;
2458 if (ofp_version < OFP14_VERSION) {
2459 if (ofp_version < OFP12_VERSION) {
2460 struct ofp10_packet_queue *opq10;
2462 opq10 = ofpbuf_put_zeros(reply, sizeof *opq10);
2463 opq10->queue_id = htonl(qc->queue);
2464 len_ofs = (char *) &opq10->len - (char *) reply->data;
2466 struct ofp12_packet_queue *opq12;
2468 opq12 = ofpbuf_put_zeros(reply, sizeof *opq12);
2469 opq12->port = ofputil_port_to_ofp11(qc->port);
2470 opq12->queue_id = htonl(qc->queue);
2471 len_ofs = (char *) &opq12->len - (char *) reply->data;
2474 put_ofp10_queue_rate(reply, OFPQT10_MIN_RATE, qc->min_rate);
2475 put_ofp10_queue_rate(reply, OFPQT11_MAX_RATE, qc->max_rate);
2477 struct ofp14_queue_desc *oqd = ofpbuf_put_zeros(reply, sizeof *oqd);
2478 oqd->port_no = ofputil_port_to_ofp11(qc->port);
2479 oqd->queue_id = htonl(qc->queue);
2480 len_ofs = (char *) &oqd->len - (char *) reply->data;
2481 put_ofp14_queue_rate(reply, OFPQDPT14_MIN_RATE, qc->min_rate);
2482 put_ofp14_queue_rate(reply, OFPQDPT14_MAX_RATE, qc->max_rate);
2485 len = ofpbuf_at(reply, len_ofs, sizeof *len);
2486 *len = htons(reply->size - start_ofs);
2488 if (ofp_version >= OFP14_VERSION) {
2489 ofpmp_postappend(replies, start_ofs);
2494 parse_ofp10_queue_rate(const struct ofp10_queue_prop_header *hdr,
2497 const struct ofp10_queue_prop_rate *oqpr;
2499 if (hdr->len == htons(sizeof *oqpr)) {
2500 oqpr = (const struct ofp10_queue_prop_rate *) hdr;
2501 *rate = ntohs(oqpr->rate);
2504 return OFPERR_OFPBRC_BAD_LEN;
2509 ofputil_pull_queue_get_config_reply10(struct ofpbuf *msg,
2510 struct ofputil_queue_config *queue)
2512 const struct ofp_header *oh = msg->header;
2513 unsigned int opq_len; /* Length of protocol-specific queue header. */
2514 unsigned int len; /* Total length of queue + properties. */
2516 /* Obtain the port number from the message header. */
2517 if (oh->version == OFP10_VERSION) {
2518 const struct ofp10_queue_get_config_reply *oqgcr10 = msg->msg;
2519 queue->port = u16_to_ofp(ntohs(oqgcr10->port));
2521 const struct ofp11_queue_get_config_reply *oqgcr11 = msg->msg;
2522 enum ofperr error = ofputil_port_from_ofp11(oqgcr11->port,
2529 /* Pull off the queue header and get the queue number and length. */
2530 if (oh->version < OFP12_VERSION) {
2531 const struct ofp10_packet_queue *opq10;
2532 opq10 = ofpbuf_try_pull(msg, sizeof *opq10);
2534 return OFPERR_OFPBRC_BAD_LEN;
2536 queue->queue = ntohl(opq10->queue_id);
2537 len = ntohs(opq10->len);
2538 opq_len = sizeof *opq10;
2540 const struct ofp12_packet_queue *opq12;
2541 opq12 = ofpbuf_try_pull(msg, sizeof *opq12);
2543 return OFPERR_OFPBRC_BAD_LEN;
2545 queue->queue = ntohl(opq12->queue_id);
2546 len = ntohs(opq12->len);
2547 opq_len = sizeof *opq12;
2551 if (len < opq_len || len > msg->size + opq_len || len % 8) {
2552 return OFPERR_OFPBRC_BAD_LEN;
2556 /* Pull properties. The format of these properties differs from used in
2557 * OF1.4+ so we can't use the common property functions. */
2559 const struct ofp10_queue_prop_header *hdr;
2560 unsigned int property;
2561 unsigned int prop_len;
2562 enum ofperr error = 0;
2564 hdr = ofpbuf_at_assert(msg, 0, sizeof *hdr);
2565 prop_len = ntohs(hdr->len);
2566 if (prop_len < sizeof *hdr || prop_len > msg->size || prop_len % 8) {
2567 return OFPERR_OFPBRC_BAD_LEN;
2570 property = ntohs(hdr->property);
2572 case OFPQT10_MIN_RATE:
2573 error = parse_ofp10_queue_rate(hdr, &queue->min_rate);
2576 case OFPQT11_MAX_RATE:
2577 error = parse_ofp10_queue_rate(hdr, &queue->max_rate);
2581 VLOG_INFO_RL(&bad_ofmsg_rl, "unknown queue property %u", property);
2588 ofpbuf_pull(msg, prop_len);
2595 ofputil_pull_queue_get_config_reply14(struct ofpbuf *msg,
2596 struct ofputil_queue_config *queue)
2598 struct ofp14_queue_desc *oqd14 = ofpbuf_try_pull(msg, sizeof *oqd14);
2600 return OFPERR_OFPBRC_BAD_LEN;
2602 enum ofperr error = ofputil_port_from_ofp11(oqd14->port_no, &queue->port);
2606 queue->queue = ntohl(oqd14->queue_id);
2609 unsigned int len = ntohs(oqd14->len);
2610 if (len < sizeof *oqd14 || len > msg->size + sizeof *oqd14 || len % 8) {
2611 return OFPERR_OFPBRC_BAD_LEN;
2613 len -= sizeof *oqd14;
2615 struct ofpbuf properties = ofpbuf_const_initializer(ofpbuf_pull(msg, len),
2617 while (properties.size > 0) {
2618 struct ofpbuf payload;
2621 error = ofpprop_pull(&properties, &payload, &type);
2627 case OFPQDPT14_MIN_RATE:
2628 error = ofpprop_parse_u16(&payload, &queue->min_rate);
2631 case OFPQDPT14_MAX_RATE:
2632 error = ofpprop_parse_u16(&payload, &queue->max_rate);
2636 error = OFPPROP_UNKNOWN(true, "queue desc", type);
2648 /* Decodes information about a queue from the OFPT_QUEUE_GET_CONFIG_REPLY in
2649 * 'reply' and stores it in '*queue'. ofputil_decode_queue_get_config_reply()
2650 * must already have pulled off the main header.
2652 * This function returns EOF if the last queue has already been decoded, 0 if a
2653 * queue was successfully decoded into '*queue', or an ofperr if there was a
2654 * problem decoding 'reply'. */
2656 ofputil_pull_queue_get_config_reply(struct ofpbuf *msg,
2657 struct ofputil_queue_config *queue)
2661 /* Pull OpenFlow header. */
2662 raw = ofpraw_pull_assert(msg);
2664 /* Pull protocol-specific ofp_queue_get_config_reply header (OF1.4
2665 * doesn't have one at all). */
2666 if (raw == OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY) {
2667 ofpbuf_pull(msg, sizeof(struct ofp10_queue_get_config_reply));
2668 } else if (raw == OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY) {
2669 ofpbuf_pull(msg, sizeof(struct ofp11_queue_get_config_reply));
2671 ovs_assert(raw == OFPRAW_OFPST14_QUEUE_DESC_REPLY);
2674 raw = ofpraw_decode_assert(msg->header);
2677 queue->min_rate = UINT16_MAX;
2678 queue->max_rate = UINT16_MAX;
2682 } else if (raw == OFPRAW_OFPST14_QUEUE_DESC_REPLY) {
2683 return ofputil_pull_queue_get_config_reply14(msg, queue);
2685 return ofputil_pull_queue_get_config_reply10(msg, queue);
2689 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
2690 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
2691 * successful, otherwise an OpenFlow error code. */
2693 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
2694 const struct ofp_header *oh)
2696 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
2697 enum ofpraw raw = ofpraw_pull_assert(&b);
2698 switch ((int) raw) {
2699 case OFPRAW_OFPST10_FLOW_REQUEST:
2700 return ofputil_decode_ofpst10_flow_request(fsr, b.data, false);
2702 case OFPRAW_OFPST10_AGGREGATE_REQUEST:
2703 return ofputil_decode_ofpst10_flow_request(fsr, b.data, true);
2705 case OFPRAW_OFPST11_FLOW_REQUEST:
2706 return ofputil_decode_ofpst11_flow_request(fsr, &b, false);
2708 case OFPRAW_OFPST11_AGGREGATE_REQUEST:
2709 return ofputil_decode_ofpst11_flow_request(fsr, &b, true);
2711 case OFPRAW_NXST_FLOW_REQUEST:
2712 return ofputil_decode_nxst_flow_request(fsr, &b, false);
2714 case OFPRAW_NXST_AGGREGATE_REQUEST:
2715 return ofputil_decode_nxst_flow_request(fsr, &b, true);
2718 /* Hey, the caller lied. */
2723 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
2724 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
2725 * 'protocol', and returns the message. */
2727 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
2728 enum ofputil_protocol protocol)
2734 case OFPUTIL_P_OF11_STD:
2735 case OFPUTIL_P_OF12_OXM:
2736 case OFPUTIL_P_OF13_OXM:
2737 case OFPUTIL_P_OF14_OXM:
2738 case OFPUTIL_P_OF15_OXM: {
2739 struct ofp11_flow_stats_request *ofsr;
2741 raw = (fsr->aggregate
2742 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
2743 : OFPRAW_OFPST11_FLOW_REQUEST);
2744 msg = ofpraw_alloc(raw, ofputil_protocol_to_ofp_version(protocol),
2745 ofputil_match_typical_len(protocol));
2746 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2747 ofsr->table_id = fsr->table_id;
2748 ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port);
2749 ofsr->out_group = htonl(fsr->out_group);
2750 ofsr->cookie = fsr->cookie;
2751 ofsr->cookie_mask = fsr->cookie_mask;
2752 ofputil_put_ofp11_match(msg, &fsr->match, protocol);
2756 case OFPUTIL_P_OF10_STD:
2757 case OFPUTIL_P_OF10_STD_TID: {
2758 struct ofp10_flow_stats_request *ofsr;
2760 raw = (fsr->aggregate
2761 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
2762 : OFPRAW_OFPST10_FLOW_REQUEST);
2763 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
2764 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2765 ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match);
2766 ofsr->table_id = fsr->table_id;
2767 ofsr->out_port = htons(ofp_to_u16(fsr->out_port));
2771 case OFPUTIL_P_OF10_NXM:
2772 case OFPUTIL_P_OF10_NXM_TID: {
2773 struct nx_flow_stats_request *nfsr;
2776 raw = (fsr->aggregate
2777 ? OFPRAW_NXST_AGGREGATE_REQUEST
2778 : OFPRAW_NXST_FLOW_REQUEST);
2779 msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN);
2780 ofpbuf_put_zeros(msg, sizeof *nfsr);
2781 match_len = nx_put_match(msg, &fsr->match,
2782 fsr->cookie, fsr->cookie_mask);
2785 nfsr->out_port = htons(ofp_to_u16(fsr->out_port));
2786 nfsr->match_len = htons(match_len);
2787 nfsr->table_id = fsr->table_id;
2798 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
2799 * ofputil_flow_stats in 'fs'.
2801 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
2802 * OpenFlow message. Calling this function multiple times for a single 'msg'
2803 * iterates through the replies. The caller must initially leave 'msg''s layer
2804 * pointers null and not modify them between calls.
2806 * Most switches don't send the values needed to populate fs->idle_age and
2807 * fs->hard_age, so those members will usually be set to 0. If the switch from
2808 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
2809 * 'flow_age_extension' as true so that the contents of 'msg' determine the
2810 * 'idle_age' and 'hard_age' members in 'fs'.
2812 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
2813 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
2814 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
2816 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2817 * otherwise a positive errno value. */
2819 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
2821 bool flow_age_extension,
2822 struct ofpbuf *ofpacts)
2824 const struct ofp_header *oh;
2825 size_t instructions_len;
2829 error = (msg->header ? ofpraw_decode(&raw, msg->header)
2830 : ofpraw_pull(&raw, msg));
2838 } else if (raw == OFPRAW_OFPST11_FLOW_REPLY
2839 || raw == OFPRAW_OFPST13_FLOW_REPLY) {
2840 const struct ofp11_flow_stats *ofs;
2842 uint16_t padded_match_len;
2844 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2846 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
2847 "bytes at end", msg->size);
2851 length = ntohs(ofs->length);
2852 if (length < sizeof *ofs) {
2853 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2854 "length %"PRIuSIZE, length);
2858 if (ofputil_pull_ofp11_match(msg, &fs->match, &padded_match_len)) {
2859 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match");
2862 instructions_len = length - sizeof *ofs - padded_match_len;
2864 fs->priority = ntohs(ofs->priority);
2865 fs->table_id = ofs->table_id;
2866 fs->duration_sec = ntohl(ofs->duration_sec);
2867 fs->duration_nsec = ntohl(ofs->duration_nsec);
2868 fs->idle_timeout = ntohs(ofs->idle_timeout);
2869 fs->hard_timeout = ntohs(ofs->hard_timeout);
2870 if (oh->version >= OFP14_VERSION) {
2871 fs->importance = ntohs(ofs->importance);
2875 if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
2876 error = ofputil_decode_flow_mod_flags(ofs->flags, -1, oh->version,
2886 fs->cookie = ofs->cookie;
2887 fs->packet_count = ntohll(ofs->packet_count);
2888 fs->byte_count = ntohll(ofs->byte_count);
2889 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
2890 const struct ofp10_flow_stats *ofs;
2893 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2895 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
2896 "bytes at end", msg->size);
2900 length = ntohs(ofs->length);
2901 if (length < sizeof *ofs) {
2902 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2903 "length %"PRIuSIZE, length);
2906 instructions_len = length - sizeof *ofs;
2908 fs->cookie = get_32aligned_be64(&ofs->cookie);
2909 ofputil_match_from_ofp10_match(&ofs->match, &fs->match);
2910 fs->priority = ntohs(ofs->priority);
2911 fs->table_id = ofs->table_id;
2912 fs->duration_sec = ntohl(ofs->duration_sec);
2913 fs->duration_nsec = ntohl(ofs->duration_nsec);
2914 fs->idle_timeout = ntohs(ofs->idle_timeout);
2915 fs->hard_timeout = ntohs(ofs->hard_timeout);
2919 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
2920 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
2922 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
2923 const struct nx_flow_stats *nfs;
2924 size_t match_len, length;
2926 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
2928 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %"PRIu32" leftover "
2929 "bytes at end", msg->size);
2933 length = ntohs(nfs->length);
2934 match_len = ntohs(nfs->match_len);
2935 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
2936 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%"PRIuSIZE" "
2937 "claims invalid length %"PRIuSIZE, match_len, length);
2940 if (nx_pull_match(msg, match_len, &fs->match, NULL, NULL)) {
2943 instructions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
2945 fs->cookie = nfs->cookie;
2946 fs->table_id = nfs->table_id;
2947 fs->duration_sec = ntohl(nfs->duration_sec);
2948 fs->duration_nsec = ntohl(nfs->duration_nsec);
2949 fs->priority = ntohs(nfs->priority);
2950 fs->idle_timeout = ntohs(nfs->idle_timeout);
2951 fs->hard_timeout = ntohs(nfs->hard_timeout);
2955 if (flow_age_extension) {
2956 if (nfs->idle_age) {
2957 fs->idle_age = ntohs(nfs->idle_age) - 1;
2959 if (nfs->hard_age) {
2960 fs->hard_age = ntohs(nfs->hard_age) - 1;
2963 fs->packet_count = ntohll(nfs->packet_count);
2964 fs->byte_count = ntohll(nfs->byte_count);
2970 if (ofpacts_pull_openflow_instructions(msg, instructions_len, oh->version,
2972 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions");
2975 fs->ofpacts = ofpacts->data;
2976 fs->ofpacts_len = ofpacts->size;
2981 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
2983 * We use this in situations where OVS internally uses UINT64_MAX to mean
2984 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
2986 unknown_to_zero(uint64_t count)
2988 return count != UINT64_MAX ? count : 0;
2991 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
2992 * those already present in the list of ofpbufs in 'replies'. 'replies' should
2993 * have been initialized with ofpmp_init(). */
2995 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
2996 struct ovs_list *replies)
2998 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
2999 size_t start_ofs = reply->size;
3000 enum ofp_version version = ofpmp_version(replies);
3001 enum ofpraw raw = ofpmp_decode_raw(replies);
3003 if (raw == OFPRAW_OFPST11_FLOW_REPLY || raw == OFPRAW_OFPST13_FLOW_REPLY) {
3004 struct ofp11_flow_stats *ofs;
3006 ofpbuf_put_uninit(reply, sizeof *ofs);
3007 oxm_put_match(reply, &fs->match, version);
3008 ofpacts_put_openflow_instructions(fs->ofpacts, fs->ofpacts_len, reply,
3011 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
3012 ofs->length = htons(reply->size - start_ofs);
3013 ofs->table_id = fs->table_id;
3015 ofs->duration_sec = htonl(fs->duration_sec);
3016 ofs->duration_nsec = htonl(fs->duration_nsec);
3017 ofs->priority = htons(fs->priority);
3018 ofs->idle_timeout = htons(fs->idle_timeout);
3019 ofs->hard_timeout = htons(fs->hard_timeout);
3020 if (version >= OFP14_VERSION) {
3021 ofs->importance = htons(fs->importance);
3023 ofs->importance = 0;
3025 if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
3026 ofs->flags = ofputil_encode_flow_mod_flags(fs->flags, version);
3030 memset(ofs->pad2, 0, sizeof ofs->pad2);
3031 ofs->cookie = fs->cookie;
3032 ofs->packet_count = htonll(unknown_to_zero(fs->packet_count));
3033 ofs->byte_count = htonll(unknown_to_zero(fs->byte_count));
3034 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
3035 struct ofp10_flow_stats *ofs;
3037 ofpbuf_put_uninit(reply, sizeof *ofs);
3038 ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
3040 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
3041 ofs->length = htons(reply->size - start_ofs);
3042 ofs->table_id = fs->table_id;
3044 ofputil_match_to_ofp10_match(&fs->match, &ofs->match);
3045 ofs->duration_sec = htonl(fs->duration_sec);
3046 ofs->duration_nsec = htonl(fs->duration_nsec);
3047 ofs->priority = htons(fs->priority);
3048 ofs->idle_timeout = htons(fs->idle_timeout);
3049 ofs->hard_timeout = htons(fs->hard_timeout);
3050 memset(ofs->pad2, 0, sizeof ofs->pad2);
3051 put_32aligned_be64(&ofs->cookie, fs->cookie);
3052 put_32aligned_be64(&ofs->packet_count,
3053 htonll(unknown_to_zero(fs->packet_count)));
3054 put_32aligned_be64(&ofs->byte_count,
3055 htonll(unknown_to_zero(fs->byte_count)));
3056 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
3057 struct nx_flow_stats *nfs;
3060 ofpbuf_put_uninit(reply, sizeof *nfs);
3061 match_len = nx_put_match(reply, &fs->match, 0, 0);
3062 ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
3064 nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
3065 nfs->length = htons(reply->size - start_ofs);
3066 nfs->table_id = fs->table_id;
3068 nfs->duration_sec = htonl(fs->duration_sec);
3069 nfs->duration_nsec = htonl(fs->duration_nsec);
3070 nfs->priority = htons(fs->priority);
3071 nfs->idle_timeout = htons(fs->idle_timeout);
3072 nfs->hard_timeout = htons(fs->hard_timeout);
3073 nfs->idle_age = htons(fs->idle_age < 0 ? 0
3074 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
3076 nfs->hard_age = htons(fs->hard_age < 0 ? 0
3077 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
3079 nfs->match_len = htons(match_len);
3080 nfs->cookie = fs->cookie;
3081 nfs->packet_count = htonll(fs->packet_count);
3082 nfs->byte_count = htonll(fs->byte_count);
3087 ofpmp_postappend(replies, start_ofs);
3090 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
3091 * NXST_AGGREGATE reply matching 'request', and returns the message. */
3093 ofputil_encode_aggregate_stats_reply(
3094 const struct ofputil_aggregate_stats *stats,
3095 const struct ofp_header *request)
3097 struct ofp_aggregate_stats_reply *asr;
3098 uint64_t packet_count;
3099 uint64_t byte_count;
3103 ofpraw_decode(&raw, request);
3104 if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) {
3105 packet_count = unknown_to_zero(stats->packet_count);
3106 byte_count = unknown_to_zero(stats->byte_count);
3108 packet_count = stats->packet_count;
3109 byte_count = stats->byte_count;
3112 msg = ofpraw_alloc_stats_reply(request, 0);
3113 asr = ofpbuf_put_zeros(msg, sizeof *asr);
3114 put_32aligned_be64(&asr->packet_count, htonll(packet_count));
3115 put_32aligned_be64(&asr->byte_count, htonll(byte_count));
3116 asr->flow_count = htonl(stats->flow_count);
3122 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
3123 const struct ofp_header *reply)
3125 struct ofpbuf msg = ofpbuf_const_initializer(reply, ntohs(reply->length));
3126 ofpraw_pull_assert(&msg);
3128 struct ofp_aggregate_stats_reply *asr = msg.msg;
3129 stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
3130 stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
3131 stats->flow_count = ntohl(asr->flow_count);
3136 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
3137 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
3138 * an OpenFlow error code. */
3140 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
3141 const struct ofp_header *oh)
3143 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
3144 enum ofpraw raw = ofpraw_pull_assert(&b);
3145 if (raw == OFPRAW_OFPT11_FLOW_REMOVED) {
3146 const struct ofp12_flow_removed *ofr;
3149 ofr = ofpbuf_pull(&b, sizeof *ofr);
3151 error = ofputil_pull_ofp11_match(&b, &fr->match, NULL);
3156 fr->priority = ntohs(ofr->priority);
3157 fr->cookie = ofr->cookie;
3158 fr->reason = ofr->reason;
3159 fr->table_id = ofr->table_id;
3160 fr->duration_sec = ntohl(ofr->duration_sec);
3161 fr->duration_nsec = ntohl(ofr->duration_nsec);
3162 fr->idle_timeout = ntohs(ofr->idle_timeout);
3163 fr->hard_timeout = ntohs(ofr->hard_timeout);
3164 fr->packet_count = ntohll(ofr->packet_count);
3165 fr->byte_count = ntohll(ofr->byte_count);
3166 } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
3167 const struct ofp10_flow_removed *ofr;
3169 ofr = ofpbuf_pull(&b, sizeof *ofr);
3171 ofputil_match_from_ofp10_match(&ofr->match, &fr->match);
3172 fr->priority = ntohs(ofr->priority);
3173 fr->cookie = ofr->cookie;
3174 fr->reason = ofr->reason;
3176 fr->duration_sec = ntohl(ofr->duration_sec);
3177 fr->duration_nsec = ntohl(ofr->duration_nsec);
3178 fr->idle_timeout = ntohs(ofr->idle_timeout);
3179 fr->hard_timeout = 0;
3180 fr->packet_count = ntohll(ofr->packet_count);
3181 fr->byte_count = ntohll(ofr->byte_count);
3182 } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
3183 struct nx_flow_removed *nfr;
3186 nfr = ofpbuf_pull(&b, sizeof *nfr);
3187 error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match,
3193 return OFPERR_OFPBRC_BAD_LEN;
3196 fr->priority = ntohs(nfr->priority);
3197 fr->cookie = nfr->cookie;
3198 fr->reason = nfr->reason;
3199 fr->table_id = nfr->table_id ? nfr->table_id - 1 : 255;
3200 fr->duration_sec = ntohl(nfr->duration_sec);
3201 fr->duration_nsec = ntohl(nfr->duration_nsec);
3202 fr->idle_timeout = ntohs(nfr->idle_timeout);
3203 fr->hard_timeout = 0;
3204 fr->packet_count = ntohll(nfr->packet_count);
3205 fr->byte_count = ntohll(nfr->byte_count);
3213 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
3214 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
3217 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
3218 enum ofputil_protocol protocol)
3221 enum ofp_flow_removed_reason reason = fr->reason;
3223 if (reason == OFPRR_METER_DELETE && !(protocol & OFPUTIL_P_OF14_UP)) {
3224 reason = OFPRR_DELETE;
3228 case OFPUTIL_P_OF11_STD:
3229 case OFPUTIL_P_OF12_OXM:
3230 case OFPUTIL_P_OF13_OXM:
3231 case OFPUTIL_P_OF14_OXM:
3232 case OFPUTIL_P_OF15_OXM: {
3233 struct ofp12_flow_removed *ofr;
3235 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED,
3236 ofputil_protocol_to_ofp_version(protocol),
3238 ofputil_match_typical_len(protocol));
3239 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3240 ofr->cookie = fr->cookie;
3241 ofr->priority = htons(fr->priority);
3242 ofr->reason = reason;
3243 ofr->table_id = fr->table_id;
3244 ofr->duration_sec = htonl(fr->duration_sec);
3245 ofr->duration_nsec = htonl(fr->duration_nsec);
3246 ofr->idle_timeout = htons(fr->idle_timeout);
3247 ofr->hard_timeout = htons(fr->hard_timeout);
3248 ofr->packet_count = htonll(fr->packet_count);
3249 ofr->byte_count = htonll(fr->byte_count);
3250 ofputil_put_ofp11_match(msg, &fr->match, protocol);
3254 case OFPUTIL_P_OF10_STD:
3255 case OFPUTIL_P_OF10_STD_TID: {
3256 struct ofp10_flow_removed *ofr;
3258 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
3260 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3261 ofputil_match_to_ofp10_match(&fr->match, &ofr->match);
3262 ofr->cookie = fr->cookie;
3263 ofr->priority = htons(fr->priority);
3264 ofr->reason = reason;
3265 ofr->duration_sec = htonl(fr->duration_sec);
3266 ofr->duration_nsec = htonl(fr->duration_nsec);
3267 ofr->idle_timeout = htons(fr->idle_timeout);
3268 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
3269 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
3273 case OFPUTIL_P_OF10_NXM:
3274 case OFPUTIL_P_OF10_NXM_TID: {
3275 struct nx_flow_removed *nfr;
3278 msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
3279 htonl(0), NXM_TYPICAL_LEN);
3280 ofpbuf_put_zeros(msg, sizeof *nfr);
3281 match_len = nx_put_match(msg, &fr->match, 0, 0);
3284 nfr->cookie = fr->cookie;
3285 nfr->priority = htons(fr->priority);
3286 nfr->reason = reason;
3287 nfr->table_id = fr->table_id + 1;
3288 nfr->duration_sec = htonl(fr->duration_sec);
3289 nfr->duration_nsec = htonl(fr->duration_nsec);
3290 nfr->idle_timeout = htons(fr->idle_timeout);
3291 nfr->match_len = htons(match_len);
3292 nfr->packet_count = htonll(fr->packet_count);
3293 nfr->byte_count = htonll(fr->byte_count);
3304 /* The caller has done basic initialization of '*pin'; the other output
3305 * arguments needs to be initialized. */
3307 decode_nx_packet_in2(const struct ofp_header *oh,
3308 struct ofputil_packet_in *pin,
3309 size_t *total_len, uint32_t *buffer_id)
3312 *buffer_id = UINT32_MAX;
3314 struct ofpbuf properties;
3315 ofpbuf_use_const(&properties, oh, ntohs(oh->length));
3316 ofpraw_pull_assert(&properties);
3318 while (properties.size > 0) {
3319 struct ofpbuf payload;
3322 enum ofperr error = ofpprop_pull(&properties, &payload, &type);
3329 pin->packet = payload.msg;
3330 pin->packet_len = ofpbuf_msgsize(&payload);
3333 case NXPINT_FULL_LEN: {
3335 error = ofpprop_parse_u32(&payload, &u32);
3340 case NXPINT_BUFFER_ID:
3341 error = ofpprop_parse_u32(&payload, buffer_id);
3344 case NXPINT_TABLE_ID:
3345 error = ofpprop_parse_u8(&payload, &pin->table_id);
3349 error = ofpprop_parse_be64(&payload, &pin->cookie);
3352 case NXPINT_REASON: {
3354 error = ofpprop_parse_u8(&payload, &reason);
3355 pin->reason = reason;
3359 case NXPINT_METADATA:
3360 error = oxm_decode_match(payload.msg, ofpbuf_msgsize(&payload),
3361 &pin->flow_metadata);
3365 error = OFPPROP_UNKNOWN(true, "NX_PACKET_IN2", type);
3373 if (!pin->packet_len) {
3374 VLOG_WARN_RL(&bad_ofmsg_rl, "NXT_PACKET_IN2 lacks packet");
3375 return OFPERR_OFPBRC_BAD_LEN;
3376 } else if (!*total_len) {
3377 *total_len = pin->packet_len;
3378 } else if (*total_len < pin->packet_len) {
3379 VLOG_WARN_RL(&bad_ofmsg_rl, "NXT_PACKET_IN2 claimed full_len < len");
3380 return OFPERR_OFPBRC_BAD_LEN;
3386 /* Decodes the packet-in message starting at 'oh' into '*pin'. Populates
3387 * 'pin->packet' and 'pin->len' with the part of the packet actually included
3388 * in the message, and '*total_len' with the original length of the packet
3389 * (which is larger than 'packet->len' if only part of the packet was
3390 * included). Stores the packet's buffer ID in '*buffer_id' (UINT32_MAX if it
3391 * was not buffered).
3393 * Returns 0 if successful, otherwise an OpenFlow error code. */
3395 ofputil_decode_packet_in(const struct ofp_header *oh,
3396 struct ofputil_packet_in *pin,
3397 size_t *total_len, uint32_t *buffer_id)
3399 memset(pin, 0, sizeof *pin);
3400 pin->cookie = OVS_BE64_MAX;
3402 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
3403 enum ofpraw raw = ofpraw_pull_assert(&b);
3404 if (raw == OFPRAW_OFPT13_PACKET_IN || raw == OFPRAW_OFPT12_PACKET_IN) {
3405 const struct ofp12_packet_in *opi = ofpbuf_pull(&b, sizeof *opi);
3406 const ovs_be64 *cookie = (raw == OFPRAW_OFPT13_PACKET_IN
3407 ? ofpbuf_pull(&b, sizeof *cookie)
3409 enum ofperr error = oxm_pull_match_loose(&b, &pin->flow_metadata);
3414 if (!ofpbuf_try_pull(&b, 2)) {
3415 return OFPERR_OFPBRC_BAD_LEN;
3418 pin->reason = opi->reason;
3419 pin->table_id = opi->table_id;
3420 *buffer_id = ntohl(opi->buffer_id);
3421 *total_len = ntohs(opi->total_len);
3423 pin->cookie = *cookie;
3426 pin->packet = b.data;
3427 pin->packet_len = b.size;
3428 } else if (raw == OFPRAW_OFPT10_PACKET_IN) {
3429 const struct ofp10_packet_in *opi;
3431 opi = ofpbuf_pull(&b, offsetof(struct ofp10_packet_in, data));
3433 pin->packet = CONST_CAST(uint8_t *, opi->data);
3434 pin->packet_len = b.size;
3436 match_init_catchall(&pin->flow_metadata);
3437 match_set_in_port(&pin->flow_metadata,
3438 u16_to_ofp(ntohs(opi->in_port)));
3439 pin->reason = opi->reason;
3440 *buffer_id = ntohl(opi->buffer_id);
3441 *total_len = ntohs(opi->total_len);
3442 } else if (raw == OFPRAW_OFPT11_PACKET_IN) {
3443 const struct ofp11_packet_in *opi;
3447 opi = ofpbuf_pull(&b, sizeof *opi);
3449 pin->packet = b.data;
3450 pin->packet_len = b.size;
3452 *buffer_id = ntohl(opi->buffer_id);
3453 error = ofputil_port_from_ofp11(opi->in_port, &in_port);
3457 match_init_catchall(&pin->flow_metadata);
3458 match_set_in_port(&pin->flow_metadata, in_port);
3459 *total_len = ntohs(opi->total_len);
3460 pin->reason = opi->reason;
3461 pin->table_id = opi->table_id;
3462 } else if (raw == OFPRAW_NXT_PACKET_IN) {
3463 const struct nx_packet_in *npi;
3466 npi = ofpbuf_pull(&b, sizeof *npi);
3467 error = nx_pull_match_loose(&b, ntohs(npi->match_len),
3468 &pin->flow_metadata, NULL, NULL);
3473 if (!ofpbuf_try_pull(&b, 2)) {
3474 return OFPERR_OFPBRC_BAD_LEN;
3477 pin->reason = npi->reason;
3478 pin->table_id = npi->table_id;
3479 pin->cookie = npi->cookie;
3481 *buffer_id = ntohl(npi->buffer_id);
3482 *total_len = ntohs(npi->total_len);
3484 pin->packet = b.data;
3485 pin->packet_len = b.size;
3486 } else if (raw == OFPRAW_NXT_PACKET_IN2) {
3487 return decode_nx_packet_in2(oh, pin, total_len, buffer_id);
3496 encode_packet_in_reason(enum ofp_packet_in_reason reason,
3497 enum ofp_version version)
3502 case OFPR_INVALID_TTL:
3505 case OFPR_ACTION_SET:
3507 case OFPR_PACKET_OUT:
3508 return version < OFP14_VERSION ? OFPR_ACTION : reason;
3510 case OFPR_EXPLICIT_MISS:
3511 return version < OFP13_VERSION ? OFPR_ACTION : OFPR_NO_MATCH;
3513 case OFPR_IMPLICIT_MISS:
3514 return OFPR_NO_MATCH;
3516 case OFPR_N_REASONS:
3522 static struct ofpbuf *
3523 ofputil_encode_ofp10_packet_in(const struct ofputil_packet_in *pin,
3526 struct ofp10_packet_in *opi;
3529 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
3530 htonl(0), pin->packet_len);
3531 opi = ofpbuf_put_zeros(msg, offsetof(struct ofp10_packet_in, data));
3532 opi->total_len = htons(pin->packet_len);
3533 opi->in_port = htons(ofp_to_u16(pin->flow_metadata.flow.in_port.ofp_port));
3534 opi->reason = encode_packet_in_reason(pin->reason, OFP10_VERSION);
3535 opi->buffer_id = htonl(buffer_id);
3540 static struct ofpbuf *
3541 ofputil_encode_nx_packet_in(const struct ofputil_packet_in *pin,
3542 enum ofp_version version, uint32_t buffer_id)
3544 struct nx_packet_in *npi;
3548 /* The final argument is just an estimate of the space required. */
3549 msg = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, version,
3550 htonl(0), NXM_TYPICAL_LEN + 2 + pin->packet_len);
3551 ofpbuf_put_zeros(msg, sizeof *npi);
3552 match_len = nx_put_match(msg, &pin->flow_metadata, 0, 0);
3553 ofpbuf_put_zeros(msg, 2);
3556 npi->buffer_id = htonl(buffer_id);
3557 npi->total_len = htons(pin->packet_len);
3558 npi->reason = encode_packet_in_reason(pin->reason, version);
3559 npi->table_id = pin->table_id;
3560 npi->cookie = pin->cookie;
3561 npi->match_len = htons(match_len);
3566 static struct ofpbuf *
3567 ofputil_encode_nx_packet_in2(const struct ofputil_packet_in *pin,
3568 enum ofp_version version, uint32_t buffer_id,
3569 size_t include_bytes)
3571 /* 'extra' is just an estimate of the space required. */
3572 size_t extra = include_bytes + NXM_TYPICAL_LEN + 256;
3573 struct ofpbuf *msg = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN2, version,
3576 /* Add packet properties. */
3577 ofpprop_put(msg, NXPINT_PACKET, pin->packet, include_bytes);
3578 if (include_bytes != pin->packet_len) {
3579 ofpprop_put_u32(msg, NXPINT_FULL_LEN, pin->packet_len);
3581 if (buffer_id != UINT32_MAX) {
3582 ofpprop_put_u32(msg, NXPINT_BUFFER_ID, buffer_id);
3585 /* Add flow properties. */
3586 ofpprop_put_u8(msg, NXPINT_TABLE_ID, pin->table_id);
3587 if (pin->cookie != OVS_BE64_MAX) {
3588 ofpprop_put_be64(msg, NXPINT_COOKIE, pin->cookie);
3591 /* Add other properties. */
3592 ofpprop_put_u8(msg, NXPINT_REASON,
3593 encode_packet_in_reason(pin->reason, version));
3595 size_t start = ofpprop_start(msg, NXPINT_METADATA);
3596 oxm_put_raw(msg, &pin->flow_metadata, version);
3597 ofpprop_end(msg, start);
3599 ofpmsg_update_length(msg);
3603 static struct ofpbuf *
3604 ofputil_encode_ofp11_packet_in(const struct ofputil_packet_in *pin,
3607 struct ofp11_packet_in *opi;
3610 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_PACKET_IN, OFP11_VERSION,
3611 htonl(0), pin->packet_len);
3612 opi = ofpbuf_put_zeros(msg, sizeof *opi);
3613 opi->buffer_id = htonl(buffer_id);
3614 opi->in_port = ofputil_port_to_ofp11(
3615 pin->flow_metadata.flow.in_port.ofp_port);
3616 opi->in_phy_port = opi->in_port;
3617 opi->total_len = htons(pin->packet_len);
3618 opi->reason = encode_packet_in_reason(pin->reason, OFP11_VERSION);
3619 opi->table_id = pin->table_id;
3624 static struct ofpbuf *
3625 ofputil_encode_ofp12_packet_in(const struct ofputil_packet_in *pin,
3626 enum ofp_version version,
3629 enum ofpraw raw = (version >= OFP13_VERSION
3630 ? OFPRAW_OFPT13_PACKET_IN
3631 : OFPRAW_OFPT12_PACKET_IN);
3634 /* The final argument is just an estimate of the space required. */
3635 msg = ofpraw_alloc_xid(raw, version,
3636 htonl(0), NXM_TYPICAL_LEN + 2 + pin->packet_len);
3638 struct ofp12_packet_in *opi = ofpbuf_put_zeros(msg, sizeof *opi);
3639 opi->buffer_id = htonl(buffer_id);
3640 opi->total_len = htons(pin->packet_len);
3641 opi->reason = encode_packet_in_reason(pin->reason, version);
3642 opi->table_id = pin->table_id;
3644 if (version >= OFP13_VERSION) {
3645 ovs_be64 cookie = pin->cookie;
3646 ofpbuf_put(msg, &cookie, sizeof cookie);
3649 oxm_put_match(msg, &pin->flow_metadata, version);
3650 ofpbuf_put_zeros(msg, 2);
3655 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message for
3656 * 'protocol', using the packet-in format specified by 'packet_in_format'.
3658 * If 'pkt_buf' is nonnull and 'max_len' allows the packet to be buffered, this
3659 * function will attempt to obtain a buffer ID from 'pktbuf' and truncate the
3660 * packet to 'max_len' bytes. Otherwise, or if 'pktbuf' doesn't have a free
3661 * buffer, it will send the whole packet without buffering. */
3663 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
3664 enum ofputil_protocol protocol,
3665 enum nx_packet_in_format packet_in_format,
3666 uint16_t max_len, struct pktbuf *pktbuf)
3668 enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
3670 /* Get buffer ID. */
3671 ofp_port_t in_port = pin->flow_metadata.flow.in_port.ofp_port;
3672 uint32_t buffer_id = (max_len != OFPCML12_NO_BUFFER && pktbuf
3673 ? pktbuf_save(pktbuf, pin->packet,
3674 pin->packet_len, in_port)
3677 /* Calculate the number of bytes of the packet to include in the
3680 * - If not buffered, the whole thing.
3682 * - Otherwise, no more than 'max_len' bytes. */
3683 size_t include_bytes = (buffer_id == UINT32_MAX
3685 : MIN(max_len, pin->packet_len));
3688 switch (packet_in_format) {
3689 case NXPIF_STANDARD:
3691 case OFPUTIL_P_OF10_STD:
3692 case OFPUTIL_P_OF10_STD_TID:
3693 case OFPUTIL_P_OF10_NXM:
3694 case OFPUTIL_P_OF10_NXM_TID:
3695 msg = ofputil_encode_ofp10_packet_in(pin, buffer_id);
3698 case OFPUTIL_P_OF11_STD:
3699 msg = ofputil_encode_ofp11_packet_in(pin, buffer_id);
3702 case OFPUTIL_P_OF12_OXM:
3703 case OFPUTIL_P_OF13_OXM:
3704 case OFPUTIL_P_OF14_OXM:
3705 case OFPUTIL_P_OF15_OXM:
3706 msg = ofputil_encode_ofp12_packet_in(pin, version, buffer_id);
3714 case NXPIF_NXT_PACKET_IN:
3715 msg = ofputil_encode_nx_packet_in(pin, version, buffer_id);
3718 case NXPIF_NXT_PACKET_IN2:
3719 return ofputil_encode_nx_packet_in2(pin, version, buffer_id,
3726 ofpbuf_put(msg, pin->packet, include_bytes);
3727 ofpmsg_update_length(msg);
3731 /* Returns a string form of 'reason'. The return value is either a statically
3732 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
3733 * 'bufsize' should be at least OFPUTIL_PACKET_IN_REASON_BUFSIZE. */
3735 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason,
3736 char *reasonbuf, size_t bufsize)
3743 case OFPR_INVALID_TTL:
3744 return "invalid_ttl";
3745 case OFPR_ACTION_SET:
3746 return "action_set";
3749 case OFPR_PACKET_OUT:
3750 return "packet_out";
3751 case OFPR_EXPLICIT_MISS:
3752 case OFPR_IMPLICIT_MISS:
3755 case OFPR_N_REASONS:
3757 snprintf(reasonbuf, bufsize, "%d", (int) reason);
3763 ofputil_packet_in_reason_from_string(const char *s,
3764 enum ofp_packet_in_reason *reason)
3768 for (i = 0; i < OFPR_N_REASONS; i++) {
3769 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
3770 const char *reason_s;
3772 reason_s = ofputil_packet_in_reason_to_string(i, reasonbuf,
3774 if (!strcasecmp(s, reason_s)) {
3782 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
3785 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
3786 * message's actions. The caller must initialize 'ofpacts' and retains
3787 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
3789 * Returns 0 if successful, otherwise an OFPERR_* value. */
3791 ofputil_decode_packet_out(struct ofputil_packet_out *po,
3792 const struct ofp_header *oh,
3793 struct ofpbuf *ofpacts)
3795 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
3796 enum ofpraw raw = ofpraw_pull_assert(&b);
3798 ofpbuf_clear(ofpacts);
3799 if (raw == OFPRAW_OFPT11_PACKET_OUT) {
3801 const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
3803 po->buffer_id = ntohl(opo->buffer_id);
3804 error = ofputil_port_from_ofp11(opo->in_port, &po->in_port);
3809 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
3810 oh->version, ofpacts);
3814 } else if (raw == OFPRAW_OFPT10_PACKET_OUT) {
3816 const struct ofp10_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
3818 po->buffer_id = ntohl(opo->buffer_id);
3819 po->in_port = u16_to_ofp(ntohs(opo->in_port));
3821 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
3822 oh->version, ofpacts);
3830 if (ofp_to_u16(po->in_port) >= ofp_to_u16(OFPP_MAX)
3831 && po->in_port != OFPP_LOCAL
3832 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
3833 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
3835 return OFPERR_OFPBRC_BAD_PORT;
3838 po->ofpacts = ofpacts->data;
3839 po->ofpacts_len = ofpacts->size;
3841 if (po->buffer_id == UINT32_MAX) {
3842 po->packet = b.data;
3843 po->packet_len = b.size;
3852 /* ofputil_phy_port */
3854 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
3855 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
3856 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
3857 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
3858 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
3859 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
3860 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
3861 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
3863 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
3864 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
3865 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
3866 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
3867 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
3868 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
3870 static enum netdev_features
3871 netdev_port_features_from_ofp10(ovs_be32 ofp10_)
3873 uint32_t ofp10 = ntohl(ofp10_);
3874 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
3878 netdev_port_features_to_ofp10(enum netdev_features features)
3880 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
3883 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
3884 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
3885 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
3886 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
3887 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
3888 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
3889 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
3890 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
3891 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
3892 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
3893 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
3894 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
3895 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
3896 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
3897 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
3898 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
3900 static enum netdev_features
3901 netdev_port_features_from_ofp11(ovs_be32 ofp11)
3903 return ntohl(ofp11) & 0xffff;
3907 netdev_port_features_to_ofp11(enum netdev_features features)
3909 return htonl(features & 0xffff);
3913 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
3914 const struct ofp10_phy_port *opp)
3916 pp->port_no = u16_to_ofp(ntohs(opp->port_no));
3917 pp->hw_addr = opp->hw_addr;
3918 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
3920 pp->config = ntohl(opp->config) & OFPPC10_ALL;
3921 pp->state = ntohl(opp->state) & OFPPS10_ALL;
3923 pp->curr = netdev_port_features_from_ofp10(opp->curr);
3924 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
3925 pp->supported = netdev_port_features_from_ofp10(opp->supported);
3926 pp->peer = netdev_port_features_from_ofp10(opp->peer);
3928 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
3929 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
3935 ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
3936 const struct ofp11_port *op)
3940 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
3944 pp->hw_addr = op->hw_addr;
3945 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
3947 pp->config = ntohl(op->config) & OFPPC11_ALL;
3948 pp->state = ntohl(op->state) & OFPPS11_ALL;
3950 pp->curr = netdev_port_features_from_ofp11(op->curr);
3951 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
3952 pp->supported = netdev_port_features_from_ofp11(op->supported);
3953 pp->peer = netdev_port_features_from_ofp11(op->peer);
3955 pp->curr_speed = ntohl(op->curr_speed);
3956 pp->max_speed = ntohl(op->max_speed);
3962 parse_ofp14_port_ethernet_property(const struct ofpbuf *payload,
3963 struct ofputil_phy_port *pp)
3965 struct ofp14_port_desc_prop_ethernet *eth = payload->data;
3967 if (payload->size != sizeof *eth) {
3968 return OFPERR_OFPBPC_BAD_LEN;
3971 pp->curr = netdev_port_features_from_ofp11(eth->curr);
3972 pp->advertised = netdev_port_features_from_ofp11(eth->advertised);
3973 pp->supported = netdev_port_features_from_ofp11(eth->supported);
3974 pp->peer = netdev_port_features_from_ofp11(eth->peer);
3976 pp->curr_speed = ntohl(eth->curr_speed);
3977 pp->max_speed = ntohl(eth->max_speed);
3983 ofputil_pull_ofp14_port(struct ofputil_phy_port *pp, struct ofpbuf *msg)
3985 struct ofp14_port *op = ofpbuf_try_pull(msg, sizeof *op);
3987 return OFPERR_OFPBRC_BAD_LEN;
3990 size_t len = ntohs(op->length);
3991 if (len < sizeof *op || len - sizeof *op > msg->size) {
3992 return OFPERR_OFPBRC_BAD_LEN;
3996 enum ofperr error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
4000 pp->hw_addr = op->hw_addr;
4001 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
4003 pp->config = ntohl(op->config) & OFPPC11_ALL;
4004 pp->state = ntohl(op->state) & OFPPS11_ALL;
4006 struct ofpbuf properties = ofpbuf_const_initializer(ofpbuf_pull(msg, len),
4008 while (properties.size > 0) {
4009 struct ofpbuf payload;
4013 error = ofpprop_pull(&properties, &payload, &type);
4019 case OFPPDPT14_ETHERNET:
4020 error = parse_ofp14_port_ethernet_property(&payload, pp);
4024 error = OFPPROP_UNKNOWN(true, "port", type);
4037 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
4038 struct ofp10_phy_port *opp)
4040 memset(opp, 0, sizeof *opp);
4042 opp->port_no = htons(ofp_to_u16(pp->port_no));
4043 opp->hw_addr = pp->hw_addr;
4044 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
4046 opp->config = htonl(pp->config & OFPPC10_ALL);
4047 opp->state = htonl(pp->state & OFPPS10_ALL);
4049 opp->curr = netdev_port_features_to_ofp10(pp->curr);
4050 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
4051 opp->supported = netdev_port_features_to_ofp10(pp->supported);
4052 opp->peer = netdev_port_features_to_ofp10(pp->peer);
4056 ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
4057 struct ofp11_port *op)
4059 memset(op, 0, sizeof *op);
4061 op->port_no = ofputil_port_to_ofp11(pp->port_no);
4062 op->hw_addr = pp->hw_addr;
4063 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
4065 op->config = htonl(pp->config & OFPPC11_ALL);
4066 op->state = htonl(pp->state & OFPPS11_ALL);
4068 op->curr = netdev_port_features_to_ofp11(pp->curr);
4069 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
4070 op->supported = netdev_port_features_to_ofp11(pp->supported);
4071 op->peer = netdev_port_features_to_ofp11(pp->peer);
4073 op->curr_speed = htonl(pp->curr_speed);
4074 op->max_speed = htonl(pp->max_speed);
4078 ofputil_put_ofp14_port(const struct ofputil_phy_port *pp,
4081 struct ofp14_port *op;
4082 struct ofp14_port_desc_prop_ethernet *eth;
4084 ofpbuf_prealloc_tailroom(b, sizeof *op + sizeof *eth);
4086 op = ofpbuf_put_zeros(b, sizeof *op);
4087 op->port_no = ofputil_port_to_ofp11(pp->port_no);
4088 op->length = htons(sizeof *op + sizeof *eth);
4089 op->hw_addr = pp->hw_addr;
4090 ovs_strlcpy(op->name, pp->name, sizeof op->name);
4091 op->config = htonl(pp->config & OFPPC11_ALL);
4092 op->state = htonl(pp->state & OFPPS11_ALL);
4094 eth = ofpprop_put_zeros(b, OFPPDPT14_ETHERNET, sizeof *eth);
4095 eth->curr = netdev_port_features_to_ofp11(pp->curr);
4096 eth->advertised = netdev_port_features_to_ofp11(pp->advertised);
4097 eth->supported = netdev_port_features_to_ofp11(pp->supported);
4098 eth->peer = netdev_port_features_to_ofp11(pp->peer);
4099 eth->curr_speed = htonl(pp->curr_speed);
4100 eth->max_speed = htonl(pp->max_speed);
4104 ofputil_put_phy_port(enum ofp_version ofp_version,
4105 const struct ofputil_phy_port *pp, struct ofpbuf *b)
4107 switch (ofp_version) {
4108 case OFP10_VERSION: {
4109 struct ofp10_phy_port *opp = ofpbuf_put_uninit(b, sizeof *opp);
4110 ofputil_encode_ofp10_phy_port(pp, opp);
4116 case OFP13_VERSION: {
4117 struct ofp11_port *op = ofpbuf_put_uninit(b, sizeof *op);
4118 ofputil_encode_ofp11_port(pp, op);
4124 ofputil_put_ofp14_port(pp, b);
4133 ofputil_decode_port_desc_stats_request(const struct ofp_header *request,
4136 struct ofpbuf b = ofpbuf_const_initializer(request,
4137 ntohs(request->length));
4138 enum ofpraw raw = ofpraw_pull_assert(&b);
4139 if (raw == OFPRAW_OFPST10_PORT_DESC_REQUEST) {
4142 } else if (raw == OFPRAW_OFPST15_PORT_DESC_REQUEST) {
4143 ovs_be32 *ofp11_port;
4145 ofp11_port = ofpbuf_pull(&b, sizeof *ofp11_port);
4146 return ofputil_port_from_ofp11(*ofp11_port, port);
4153 ofputil_encode_port_desc_stats_request(enum ofp_version ofp_version,
4156 struct ofpbuf *request;
4158 switch (ofp_version) {
4164 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_DESC_REQUEST,
4167 case OFP15_VERSION:{
4168 struct ofp15_port_desc_request *req;
4169 request = ofpraw_alloc(OFPRAW_OFPST15_PORT_DESC_REQUEST,
4171 req = ofpbuf_put_zeros(request, sizeof *req);
4172 req->port_no = ofputil_port_to_ofp11(port);
4183 ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port *pp,
4184 struct ovs_list *replies)
4186 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
4187 size_t start_ofs = reply->size;
4189 ofputil_put_phy_port(ofpmp_version(replies), pp, reply);
4190 ofpmp_postappend(replies, start_ofs);
4193 /* ofputil_switch_config */
4195 /* Decodes 'oh', which must be an OFPT_GET_CONFIG_REPLY or OFPT_SET_CONFIG
4196 * message, into 'config'. Returns false if 'oh' contained any flags that
4197 * aren't specified in its version of OpenFlow, true otherwise. */
4199 ofputil_decode_switch_config(const struct ofp_header *oh,
4200 struct ofputil_switch_config *config)
4202 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
4203 ofpraw_pull_assert(&b);
4205 const struct ofp_switch_config *osc = ofpbuf_pull(&b, sizeof *osc);
4206 config->frag = ntohs(osc->flags) & OFPC_FRAG_MASK;
4207 config->miss_send_len = ntohs(osc->miss_send_len);
4209 ovs_be16 valid_mask = htons(OFPC_FRAG_MASK);
4210 if (oh->version < OFP13_VERSION) {
4211 const ovs_be16 ttl_bit = htons(OFPC_INVALID_TTL_TO_CONTROLLER);
4212 valid_mask |= ttl_bit;
4213 config->invalid_ttl_to_controller = (osc->flags & ttl_bit) != 0;
4215 config->invalid_ttl_to_controller = -1;
4218 return !(osc->flags & ~valid_mask);
4222 ofputil_decode_get_config_reply(const struct ofp_header *oh,
4223 struct ofputil_switch_config *config)
4225 ofputil_decode_switch_config(oh, config);
4229 ofputil_decode_set_config(const struct ofp_header *oh,
4230 struct ofputil_switch_config *config)
4232 return (ofputil_decode_switch_config(oh, config)
4234 : OFPERR_OFPSCFC_BAD_FLAGS);
4237 static struct ofpbuf *
4238 ofputil_put_switch_config(const struct ofputil_switch_config *config,
4241 const struct ofp_header *oh = b->data;
4242 struct ofp_switch_config *osc = ofpbuf_put_zeros(b, sizeof *osc);
4243 osc->flags = htons(config->frag);
4244 if (config->invalid_ttl_to_controller > 0 && oh->version < OFP13_VERSION) {
4245 osc->flags |= htons(OFPC_INVALID_TTL_TO_CONTROLLER);
4247 osc->miss_send_len = htons(config->miss_send_len);
4252 ofputil_encode_get_config_reply(const struct ofp_header *request,
4253 const struct ofputil_switch_config *config)
4255 struct ofpbuf *b = ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY,
4257 return ofputil_put_switch_config(config, b);
4261 ofputil_encode_set_config(const struct ofputil_switch_config *config,
4262 enum ofp_version version)
4264 struct ofpbuf *b = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, version, 0);
4265 return ofputil_put_switch_config(config, b);
4268 /* ofputil_switch_features */
4270 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
4271 OFPC_IP_REASM | OFPC_QUEUE_STATS)
4272 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
4273 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
4274 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
4275 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
4276 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
4277 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
4280 ofputil_capabilities_mask(enum ofp_version ofp_version)
4282 /* Handle capabilities whose bit is unique for all OpenFlow versions */
4283 switch (ofp_version) {
4286 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
4291 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
4293 /* Caller needs to check osf->header.version itself */
4298 /* Pulls an OpenFlow "switch_features" structure from 'b' and decodes it into
4299 * an abstract representation in '*features', readying 'b' to iterate over the
4300 * OpenFlow port structures following 'osf' with later calls to
4301 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an OFPERR_*
4304 ofputil_pull_switch_features(struct ofpbuf *b,
4305 struct ofputil_switch_features *features)
4307 const struct ofp_header *oh = b->data;
4308 enum ofpraw raw = ofpraw_pull_assert(b);
4309 const struct ofp_switch_features *osf = ofpbuf_pull(b, sizeof *osf);
4310 features->datapath_id = ntohll(osf->datapath_id);
4311 features->n_buffers = ntohl(osf->n_buffers);
4312 features->n_tables = osf->n_tables;
4313 features->auxiliary_id = 0;
4315 features->capabilities = ntohl(osf->capabilities) &
4316 ofputil_capabilities_mask(oh->version);
4318 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
4319 if (osf->capabilities & htonl(OFPC10_STP)) {
4320 features->capabilities |= OFPUTIL_C_STP;
4322 features->ofpacts = ofpact_bitmap_from_openflow(osf->actions,
4324 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY
4325 || raw == OFPRAW_OFPT13_FEATURES_REPLY) {
4326 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
4327 features->capabilities |= OFPUTIL_C_GROUP_STATS;
4329 features->ofpacts = 0;
4330 if (raw == OFPRAW_OFPT13_FEATURES_REPLY) {
4331 features->auxiliary_id = osf->auxiliary_id;
4334 return OFPERR_OFPBRC_BAD_VERSION;
4340 /* In OpenFlow 1.0, 1.1, and 1.2, an OFPT_FEATURES_REPLY message lists all the
4341 * switch's ports, unless there are too many to fit. In OpenFlow 1.3 and
4342 * later, an OFPT_FEATURES_REPLY does not list ports at all.
4344 * Given a buffer 'b' that contains a Features Reply message, this message
4345 * checks if it contains a complete list of the switch's ports. Returns true,
4346 * if so. Returns false if the list is missing (OF1.3+) or incomplete
4347 * (OF1.0/1.1/1.2), and in the latter case removes all of the ports from the
4350 * When this function returns false, the caller should send an OFPST_PORT_DESC
4351 * stats request to get the ports. */
4353 ofputil_switch_features_has_ports(struct ofpbuf *b)
4355 struct ofp_header *oh = b->data;
4356 size_t phy_port_size;
4358 if (oh->version >= OFP13_VERSION) {
4359 /* OpenFlow 1.3+ never has ports in the feature reply. */
4363 phy_port_size = (oh->version == OFP10_VERSION
4364 ? sizeof(struct ofp10_phy_port)
4365 : sizeof(struct ofp11_port));
4366 if (ntohs(oh->length) + phy_port_size <= UINT16_MAX) {
4367 /* There's room for additional ports in the feature reply.
4368 * Assume that the list is complete. */
4372 /* The feature reply has no room for more ports. Probably the list is
4373 * truncated. Drop the ports and tell the caller to retrieve them with
4374 * OFPST_PORT_DESC. */
4375 b->size = sizeof *oh + sizeof(struct ofp_switch_features);
4376 ofpmsg_update_length(b);
4380 /* Returns a buffer owned by the caller that encodes 'features' in the format
4381 * required by 'protocol' with the given 'xid'. The caller should append port
4382 * information to the buffer with subsequent calls to
4383 * ofputil_put_switch_features_port(). */
4385 ofputil_encode_switch_features(const struct ofputil_switch_features *features,
4386 enum ofputil_protocol protocol, ovs_be32 xid)
4388 struct ofp_switch_features *osf;
4390 enum ofp_version version;
4393 version = ofputil_protocol_to_ofp_version(protocol);
4396 raw = OFPRAW_OFPT10_FEATURES_REPLY;
4400 raw = OFPRAW_OFPT11_FEATURES_REPLY;
4405 raw = OFPRAW_OFPT13_FEATURES_REPLY;
4410 b = ofpraw_alloc_xid(raw, version, xid, 0);
4411 osf = ofpbuf_put_zeros(b, sizeof *osf);
4412 osf->datapath_id = htonll(features->datapath_id);
4413 osf->n_buffers = htonl(features->n_buffers);
4414 osf->n_tables = features->n_tables;
4416 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
4417 osf->capabilities = htonl(features->capabilities &
4418 ofputil_capabilities_mask(version));
4421 if (features->capabilities & OFPUTIL_C_STP) {
4422 osf->capabilities |= htonl(OFPC10_STP);
4424 osf->actions = ofpact_bitmap_to_openflow(features->ofpacts,
4430 osf->auxiliary_id = features->auxiliary_id;
4434 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
4435 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
4445 /* Encodes 'pp' into the format required by the switch_features message already
4446 * in 'b', which should have been returned by ofputil_encode_switch_features(),
4447 * and appends the encoded version to 'b'. */
4449 ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
4452 const struct ofp_header *oh = b->data;
4454 if (oh->version < OFP13_VERSION) {
4455 /* Try adding a port description to the message, but drop it again if
4456 * the buffer overflows. (This possibility for overflow is why
4457 * OpenFlow 1.3+ moved port descriptions into a multipart message.) */
4458 size_t start_ofs = b->size;
4459 ofputil_put_phy_port(oh->version, pp, b);
4460 if (b->size > UINT16_MAX) {
4461 b->size = start_ofs;
4466 /* ofputil_port_status */
4468 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
4469 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
4471 ofputil_decode_port_status(const struct ofp_header *oh,
4472 struct ofputil_port_status *ps)
4474 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
4475 ofpraw_pull_assert(&b);
4477 const struct ofp_port_status *ops = ofpbuf_pull(&b, sizeof *ops);
4478 if (ops->reason != OFPPR_ADD &&
4479 ops->reason != OFPPR_DELETE &&
4480 ops->reason != OFPPR_MODIFY) {
4481 return OFPERR_NXBRC_BAD_REASON;
4483 ps->reason = ops->reason;
4485 int retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
4486 ovs_assert(retval != EOF);
4490 /* Converts the abstract form of a "port status" message in '*ps' into an
4491 * OpenFlow message suitable for 'protocol', and returns that encoded form in
4492 * a buffer owned by the caller. */
4494 ofputil_encode_port_status(const struct ofputil_port_status *ps,
4495 enum ofputil_protocol protocol)
4497 struct ofp_port_status *ops;
4499 enum ofp_version version;
4502 version = ofputil_protocol_to_ofp_version(protocol);
4505 raw = OFPRAW_OFPT10_PORT_STATUS;
4511 raw = OFPRAW_OFPT11_PORT_STATUS;
4516 raw = OFPRAW_OFPT14_PORT_STATUS;
4523 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
4524 ops = ofpbuf_put_zeros(b, sizeof *ops);
4525 ops->reason = ps->reason;
4526 ofputil_put_phy_port(version, &ps->desc, b);
4527 ofpmsg_update_length(b);
4531 /* ofputil_port_mod */
4534 parse_port_mod_ethernet_property(struct ofpbuf *property,
4535 struct ofputil_port_mod *pm)
4540 error = ofpprop_parse_be32(property, &advertise);
4542 pm->advertise = netdev_port_features_from_ofp11(advertise);
4547 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
4548 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
4550 ofputil_decode_port_mod(const struct ofp_header *oh,
4551 struct ofputil_port_mod *pm, bool loose)
4553 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
4554 enum ofpraw raw = ofpraw_pull_assert(&b);
4555 if (raw == OFPRAW_OFPT10_PORT_MOD) {
4556 const struct ofp10_port_mod *opm = b.data;
4558 pm->port_no = u16_to_ofp(ntohs(opm->port_no));
4559 pm->hw_addr = opm->hw_addr;
4560 pm->config = ntohl(opm->config) & OFPPC10_ALL;
4561 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
4562 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
4563 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
4564 const struct ofp11_port_mod *opm = b.data;
4567 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
4572 pm->hw_addr = opm->hw_addr;
4573 pm->config = ntohl(opm->config) & OFPPC11_ALL;
4574 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
4575 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
4576 } else if (raw == OFPRAW_OFPT14_PORT_MOD) {
4577 const struct ofp14_port_mod *opm = ofpbuf_pull(&b, sizeof *opm);
4580 memset(pm, 0, sizeof *pm);
4582 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
4587 pm->hw_addr = opm->hw_addr;
4588 pm->config = ntohl(opm->config) & OFPPC11_ALL;
4589 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
4591 while (b.size > 0) {
4592 struct ofpbuf property;
4596 error = ofpprop_pull(&b, &property, &type);
4602 case OFPPMPT14_ETHERNET:
4603 error = parse_port_mod_ethernet_property(&property, pm);
4607 error = OFPPROP_UNKNOWN(loose, "port_mod", type);
4616 return OFPERR_OFPBRC_BAD_TYPE;
4619 pm->config &= pm->mask;
4623 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
4624 * message suitable for 'protocol', and returns that encoded form in a buffer
4625 * owned by the caller. */
4627 ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
4628 enum ofputil_protocol protocol)
4630 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
4633 switch (ofp_version) {
4634 case OFP10_VERSION: {
4635 struct ofp10_port_mod *opm;
4637 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
4638 opm = ofpbuf_put_zeros(b, sizeof *opm);
4639 opm->port_no = htons(ofp_to_u16(pm->port_no));
4640 opm->hw_addr = pm->hw_addr;
4641 opm->config = htonl(pm->config & OFPPC10_ALL);
4642 opm->mask = htonl(pm->mask & OFPPC10_ALL);
4643 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
4649 case OFP13_VERSION: {
4650 struct ofp11_port_mod *opm;
4652 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
4653 opm = ofpbuf_put_zeros(b, sizeof *opm);
4654 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
4655 opm->hw_addr = pm->hw_addr;
4656 opm->config = htonl(pm->config & OFPPC11_ALL);
4657 opm->mask = htonl(pm->mask & OFPPC11_ALL);
4658 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
4662 case OFP15_VERSION: {
4663 struct ofp14_port_mod *opm;
4665 b = ofpraw_alloc(OFPRAW_OFPT14_PORT_MOD, ofp_version, 0);
4666 opm = ofpbuf_put_zeros(b, sizeof *opm);
4667 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
4668 opm->hw_addr = pm->hw_addr;
4669 opm->config = htonl(pm->config & OFPPC11_ALL);
4670 opm->mask = htonl(pm->mask & OFPPC11_ALL);
4672 if (pm->advertise) {
4673 ofpprop_put_be32(b, OFPPMPT14_ETHERNET,
4674 netdev_port_features_to_ofp11(pm->advertise));
4685 /* Table features. */
4688 pull_table_feature_property(struct ofpbuf *msg, struct ofpbuf *payload,
4693 error = ofpprop_pull(msg, payload, typep);
4694 if (payload && !error) {
4695 ofpbuf_pull(payload, (char *)payload->msg - (char *)payload->header);
4701 parse_action_bitmap(struct ofpbuf *payload, enum ofp_version ofp_version,
4706 while (payload->size > 0) {
4710 error = ofpprop_pull__(payload, NULL, 1, 0x10000, &type);
4714 if (type < CHAR_BIT * sizeof types) {
4715 types |= 1u << type;
4719 *ofpacts = ofpact_bitmap_from_openflow(htonl(types), ofp_version);
4724 parse_instruction_ids(struct ofpbuf *payload, bool loose, uint32_t *insts)
4727 while (payload->size > 0) {
4728 enum ovs_instruction_type inst;
4732 /* OF1.3 and OF1.4 aren't clear about padding in the instruction IDs.
4733 * It seems clear that they aren't padded to 8 bytes, though, because
4734 * both standards say that "non-experimenter instructions are 4 bytes"
4735 * and do not mention any padding before the first instruction ID.
4736 * (There wouldn't be any point in padding to 8 bytes if the IDs were
4737 * aligned on an odd 4-byte boundary.)
4739 * Anyway, we just assume they're all glommed together on byte
4741 error = ofpprop_pull__(payload, NULL, 1, 0x10000, &ofpit);
4746 error = ovs_instruction_type_from_inst_type(&inst, ofpit);
4748 *insts |= 1u << inst;
4749 } else if (!loose) {
4757 parse_table_features_next_table(struct ofpbuf *payload,
4758 unsigned long int *next_tables)
4762 memset(next_tables, 0, bitmap_n_bytes(255));
4763 for (i = 0; i < payload->size; i++) {
4764 uint8_t id = ((const uint8_t *) payload->data)[i];
4766 return OFPERR_OFPBPC_BAD_VALUE;
4768 bitmap_set1(next_tables, id);
4774 parse_oxms(struct ofpbuf *payload, bool loose,
4775 struct mf_bitmap *exactp, struct mf_bitmap *maskedp)
4777 struct mf_bitmap exact = MF_BITMAP_INITIALIZER;
4778 struct mf_bitmap masked = MF_BITMAP_INITIALIZER;
4780 while (payload->size > 0) {
4781 const struct mf_field *field;
4785 error = nx_pull_header(payload, &field, &hasmask);
4787 bitmap_set1(hasmask ? masked.bm : exact.bm, field->id);
4788 } else if (error != OFPERR_OFPBMC_BAD_FIELD || !loose) {
4794 } else if (!bitmap_is_all_zeros(exact.bm, MFF_N_IDS)) {
4795 return OFPERR_OFPBMC_BAD_MASK;
4799 } else if (!bitmap_is_all_zeros(masked.bm, MFF_N_IDS)) {
4800 return OFPERR_OFPBMC_BAD_MASK;
4805 /* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract
4806 * ofputil_table_features in 'tf'.
4808 * If 'loose' is true, this function ignores properties and values that it does
4809 * not understand, as a controller would want to do when interpreting
4810 * capabilities provided by a switch. If 'loose' is false, this function
4811 * treats unknown properties and values as an error, as a switch would want to
4812 * do when interpreting a configuration request made by a controller.
4814 * A single OpenFlow message can specify features for multiple tables. Calling
4815 * this function multiple times for a single 'msg' iterates through the tables
4816 * in the message. The caller must initially leave 'msg''s layer pointers null
4817 * and not modify them between calls.
4819 * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise
4820 * a positive "enum ofperr" value. */
4822 ofputil_decode_table_features(struct ofpbuf *msg,
4823 struct ofputil_table_features *tf, bool loose)
4825 memset(tf, 0, sizeof *tf);
4828 ofpraw_pull_assert(msg);
4835 const struct ofp_header *oh = msg->header;
4836 struct ofp13_table_features *otf = msg->data;
4837 if (msg->size < sizeof *otf) {
4838 return OFPERR_OFPBPC_BAD_LEN;
4841 unsigned int len = ntohs(otf->length);
4842 if (len < sizeof *otf || len % 8 || len > msg->size) {
4843 return OFPERR_OFPBPC_BAD_LEN;
4846 tf->table_id = otf->table_id;
4847 if (tf->table_id == OFPTT_ALL) {
4848 return OFPERR_OFPTFFC_BAD_TABLE;
4851 ovs_strlcpy(tf->name, otf->name, OFP_MAX_TABLE_NAME_LEN);
4852 tf->metadata_match = otf->metadata_match;
4853 tf->metadata_write = otf->metadata_write;
4854 tf->miss_config = OFPUTIL_TABLE_MISS_DEFAULT;
4855 if (oh->version >= OFP14_VERSION) {
4856 uint32_t caps = ntohl(otf->capabilities);
4857 tf->supports_eviction = (caps & OFPTC14_EVICTION) != 0;
4858 tf->supports_vacancy_events = (caps & OFPTC14_VACANCY_EVENTS) != 0;
4860 tf->supports_eviction = -1;
4861 tf->supports_vacancy_events = -1;
4863 tf->max_entries = ntohl(otf->max_entries);
4865 struct ofpbuf properties = ofpbuf_const_initializer(ofpbuf_pull(msg, len),
4867 ofpbuf_pull(&properties, sizeof *otf);
4868 while (properties.size > 0) {
4869 struct ofpbuf payload;
4873 error = pull_table_feature_property(&properties, &payload, &type);
4878 switch ((enum ofp13_table_feature_prop_type) type) {
4879 case OFPTFPT13_INSTRUCTIONS:
4880 error = parse_instruction_ids(&payload, loose,
4881 &tf->nonmiss.instructions);
4883 case OFPTFPT13_INSTRUCTIONS_MISS:
4884 error = parse_instruction_ids(&payload, loose,
4885 &tf->miss.instructions);
4888 case OFPTFPT13_NEXT_TABLES:
4889 error = parse_table_features_next_table(&payload,
4892 case OFPTFPT13_NEXT_TABLES_MISS:
4893 error = parse_table_features_next_table(&payload, tf->miss.next);
4896 case OFPTFPT13_WRITE_ACTIONS:
4897 error = parse_action_bitmap(&payload, oh->version,
4898 &tf->nonmiss.write.ofpacts);
4900 case OFPTFPT13_WRITE_ACTIONS_MISS:
4901 error = parse_action_bitmap(&payload, oh->version,
4902 &tf->miss.write.ofpacts);
4905 case OFPTFPT13_APPLY_ACTIONS:
4906 error = parse_action_bitmap(&payload, oh->version,
4907 &tf->nonmiss.apply.ofpacts);
4909 case OFPTFPT13_APPLY_ACTIONS_MISS:
4910 error = parse_action_bitmap(&payload, oh->version,
4911 &tf->miss.apply.ofpacts);
4914 case OFPTFPT13_MATCH:
4915 error = parse_oxms(&payload, loose, &tf->match, &tf->mask);
4917 case OFPTFPT13_WILDCARDS:
4918 error = parse_oxms(&payload, loose, &tf->wildcard, NULL);
4921 case OFPTFPT13_WRITE_SETFIELD:
4922 error = parse_oxms(&payload, loose,
4923 &tf->nonmiss.write.set_fields, NULL);
4925 case OFPTFPT13_WRITE_SETFIELD_MISS:
4926 error = parse_oxms(&payload, loose,
4927 &tf->miss.write.set_fields, NULL);
4929 case OFPTFPT13_APPLY_SETFIELD:
4930 error = parse_oxms(&payload, loose,
4931 &tf->nonmiss.apply.set_fields, NULL);
4933 case OFPTFPT13_APPLY_SETFIELD_MISS:
4934 error = parse_oxms(&payload, loose,
4935 &tf->miss.apply.set_fields, NULL);
4938 case OFPTFPT13_EXPERIMENTER:
4939 case OFPTFPT13_EXPERIMENTER_MISS:
4941 error = OFPPROP_UNKNOWN(loose, "table features", type);
4949 /* Fix inconsistencies:
4951 * - Turn on 'match' bits that are set in 'mask', because maskable
4952 * fields are matchable.
4954 * - Turn on 'wildcard' bits that are set in 'mask', because a field
4955 * that is arbitrarily maskable can be wildcarded entirely.
4957 * - Turn off 'wildcard' bits that are not in 'match', because a field
4958 * must be matchable for it to be meaningfully wildcarded. */
4959 bitmap_or(tf->match.bm, tf->mask.bm, MFF_N_IDS);
4960 bitmap_or(tf->wildcard.bm, tf->mask.bm, MFF_N_IDS);
4961 bitmap_and(tf->wildcard.bm, tf->match.bm, MFF_N_IDS);
4966 /* Encodes and returns a request to obtain the table features of a switch.
4967 * The message is encoded for OpenFlow version 'ofp_version'. */
4969 ofputil_encode_table_features_request(enum ofp_version ofp_version)
4971 struct ofpbuf *request = NULL;
4973 switch (ofp_version) {
4977 ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later "
4978 "(\'-O OpenFlow13\')");
4982 request = ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST,
4993 put_fields_property(struct ofpbuf *reply,
4994 const struct mf_bitmap *fields,
4995 const struct mf_bitmap *masks,
4996 enum ofp13_table_feature_prop_type property,
4997 enum ofp_version version)
5002 start_ofs = ofpprop_start(reply, property);
5003 BITMAP_FOR_EACH_1 (field, MFF_N_IDS, fields->bm) {
5004 nx_put_header(reply, field, version,
5005 masks && bitmap_is_set(masks->bm, field));
5007 ofpprop_end(reply, start_ofs);
5011 put_table_action_features(struct ofpbuf *reply,
5012 const struct ofputil_table_action_features *taf,
5013 enum ofp13_table_feature_prop_type actions_type,
5014 enum ofp13_table_feature_prop_type set_fields_type,
5015 int miss_offset, enum ofp_version version)
5017 ofpprop_put_bitmap(reply, actions_type + miss_offset,
5018 ntohl(ofpact_bitmap_to_openflow(taf->ofpacts,
5020 put_fields_property(reply, &taf->set_fields, NULL,
5021 set_fields_type + miss_offset, version);
5025 put_table_instruction_features(
5026 struct ofpbuf *reply, const struct ofputil_table_instruction_features *tif,
5027 int miss_offset, enum ofp_version version)
5032 ofpprop_put_bitmap(reply, OFPTFPT13_INSTRUCTIONS + miss_offset,
5033 ntohl(ovsinst_bitmap_to_openflow(tif->instructions,
5036 start_ofs = ofpprop_start(reply, OFPTFPT13_NEXT_TABLES + miss_offset);
5037 BITMAP_FOR_EACH_1 (table_id, 255, tif->next) {
5038 ofpbuf_put(reply, &table_id, 1);
5040 ofpprop_end(reply, start_ofs);
5042 put_table_action_features(reply, &tif->write,
5043 OFPTFPT13_WRITE_ACTIONS,
5044 OFPTFPT13_WRITE_SETFIELD, miss_offset, version);
5045 put_table_action_features(reply, &tif->apply,
5046 OFPTFPT13_APPLY_ACTIONS,
5047 OFPTFPT13_APPLY_SETFIELD, miss_offset, version);
5051 ofputil_append_table_features_reply(const struct ofputil_table_features *tf,
5052 struct ovs_list *replies)
5054 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
5055 enum ofp_version version = ofpmp_version(replies);
5056 size_t start_ofs = reply->size;
5057 struct ofp13_table_features *otf;
5059 otf = ofpbuf_put_zeros(reply, sizeof *otf);
5060 otf->table_id = tf->table_id;
5061 ovs_strlcpy(otf->name, tf->name, sizeof otf->name);
5062 otf->metadata_match = tf->metadata_match;
5063 otf->metadata_write = tf->metadata_write;
5064 if (version >= OFP14_VERSION) {
5065 if (tf->supports_eviction) {
5066 otf->capabilities |= htonl(OFPTC14_EVICTION);
5068 if (tf->supports_vacancy_events) {
5069 otf->capabilities |= htonl(OFPTC14_VACANCY_EVENTS);
5072 otf->max_entries = htonl(tf->max_entries);
5074 put_table_instruction_features(reply, &tf->nonmiss, 0, version);
5075 put_table_instruction_features(reply, &tf->miss, 1, version);
5077 put_fields_property(reply, &tf->match, &tf->mask,
5078 OFPTFPT13_MATCH, version);
5079 put_fields_property(reply, &tf->wildcard, NULL,
5080 OFPTFPT13_WILDCARDS, version);
5082 otf = ofpbuf_at_assert(reply, start_ofs, sizeof *otf);
5083 otf->length = htons(reply->size - start_ofs);
5084 ofpmp_postappend(replies, start_ofs);
5088 parse_table_desc_vacancy_property(struct ofpbuf *property,
5089 struct ofputil_table_desc *td)
5091 struct ofp14_table_mod_prop_vacancy *otv = property->data;
5093 if (property->size != sizeof *otv) {
5094 return OFPERR_OFPBPC_BAD_LEN;
5097 td->table_vacancy.vacancy_down = otv->vacancy_down;
5098 td->table_vacancy.vacancy_up = otv->vacancy_up;
5099 td->table_vacancy.vacancy = otv->vacancy;
5103 /* Decodes the next OpenFlow "table desc" message (of possibly several) from
5104 * 'msg' into an abstract form in '*td'. Returns 0 if successful, EOF if the
5105 * last "table desc" in 'msg' was already decoded, otherwise an OFPERR_*
5108 ofputil_decode_table_desc(struct ofpbuf *msg,
5109 struct ofputil_table_desc *td,
5110 enum ofp_version version)
5112 memset(td, 0, sizeof *td);
5115 ofpraw_pull_assert(msg);
5122 struct ofp14_table_desc *otd = ofpbuf_try_pull(msg, sizeof *otd);
5124 VLOG_WARN_RL(&bad_ofmsg_rl, "OFP14_TABLE_DESC reply has %"PRIu32" "
5125 "leftover bytes at end", msg->size);
5126 return OFPERR_OFPBRC_BAD_LEN;
5129 td->table_id = otd->table_id;
5130 size_t length = ntohs(otd->length);
5131 if (length < sizeof *otd || length - sizeof *otd > msg->size) {
5132 VLOG_WARN_RL(&bad_ofmsg_rl, "OFP14_TABLE_DESC reply claims invalid "
5133 "length %"PRIuSIZE, length);
5134 return OFPERR_OFPBRC_BAD_LEN;
5136 length -= sizeof *otd;
5138 td->eviction = ofputil_decode_table_eviction(otd->config, version);
5139 td->vacancy = ofputil_decode_table_vacancy(otd->config, version);
5140 td->eviction_flags = UINT32_MAX;
5142 struct ofpbuf properties = ofpbuf_const_initializer(
5143 ofpbuf_pull(msg, length), length);
5144 while (properties.size > 0) {
5145 struct ofpbuf payload;
5149 error = ofpprop_pull(&properties, &payload, &type);
5155 case OFPTMPT14_EVICTION:
5156 error = ofpprop_parse_u32(&payload, &td->eviction_flags);
5159 case OFPTMPT14_VACANCY:
5160 error = parse_table_desc_vacancy_property(&payload, td);
5164 error = OFPPROP_UNKNOWN(true, "table_desc", type);
5176 /* Encodes and returns a request to obtain description of tables of a switch.
5177 * The message is encoded for OpenFlow version 'ofp_version'. */
5179 ofputil_encode_table_desc_request(enum ofp_version ofp_version)
5181 struct ofpbuf *request = NULL;
5183 if (ofp_version >= OFP14_VERSION) {
5184 request = ofpraw_alloc(OFPRAW_OFPST14_TABLE_DESC_REQUEST,
5187 ovs_fatal(0, "dump-table-desc needs OpenFlow 1.4 or later "
5188 "(\'-O OpenFlow14\')");
5194 /* Function to append Table desc information in a reply list. */
5196 ofputil_append_table_desc_reply(const struct ofputil_table_desc *td,
5197 struct ovs_list *replies,
5198 enum ofp_version version)
5200 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
5202 struct ofp14_table_desc *otd;
5204 start_otd = reply->size;
5205 ofpbuf_put_zeros(reply, sizeof *otd);
5206 if (td->eviction_flags != UINT32_MAX) {
5207 ofpprop_put_u32(reply, OFPTMPT14_EVICTION, td->eviction_flags);
5209 if (td->vacancy == OFPUTIL_TABLE_VACANCY_ON) {
5210 struct ofp14_table_mod_prop_vacancy *otv;
5212 otv = ofpprop_put_zeros(reply, OFPTMPT14_VACANCY, sizeof *otv);
5213 otv->vacancy_down = td->table_vacancy.vacancy_down;
5214 otv->vacancy_up = td->table_vacancy.vacancy_up;
5215 otv->vacancy = td->table_vacancy.vacancy;
5218 otd = ofpbuf_at_assert(reply, start_otd, sizeof *otd);
5219 otd->length = htons(reply->size - start_otd);
5220 otd->table_id = td->table_id;
5221 otd->config = ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT,
5222 td->eviction, td->vacancy,
5224 ofpmp_postappend(replies, start_otd);
5227 /* This function parses Vacancy property, and decodes the
5228 * ofp14_table_mod_prop_vacancy in ofputil_table_mod.
5229 * Returns OFPERR_OFPBPC_BAD_VALUE error code when vacancy_down is
5230 * greater than vacancy_up and also when current vacancy has non-zero
5231 * value. Returns 0 on success. */
5233 parse_table_mod_vacancy_property(struct ofpbuf *property,
5234 struct ofputil_table_mod *tm)
5236 struct ofp14_table_mod_prop_vacancy *otv = property->data;
5238 if (property->size != sizeof *otv) {
5239 return OFPERR_OFPBPC_BAD_LEN;
5241 tm->table_vacancy.vacancy_down = otv->vacancy_down;
5242 tm->table_vacancy.vacancy_up = otv->vacancy_up;
5243 if (tm->table_vacancy.vacancy_down > tm->table_vacancy.vacancy_up) {
5244 OFPPROP_LOG(&bad_ofmsg_rl, false,
5245 "Value of vacancy_down is greater than vacancy_up");
5246 return OFPERR_OFPBPC_BAD_VALUE;
5248 if (tm->table_vacancy.vacancy_down > 100 ||
5249 tm->table_vacancy.vacancy_up > 100) {
5250 OFPPROP_LOG(&bad_ofmsg_rl, false, "Vacancy threshold percentage "
5251 "should not be greater than 100");
5252 return OFPERR_OFPBPC_BAD_VALUE;
5254 tm->table_vacancy.vacancy = otv->vacancy;
5255 if (tm->table_vacancy.vacancy) {
5256 OFPPROP_LOG(&bad_ofmsg_rl, false,
5257 "Vacancy value should be zero for table-mod messages");
5258 return OFPERR_OFPBPC_BAD_VALUE;
5263 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5264 * table configuration (a table mod, table stats, or table features message),
5265 * returns the table vacancy configuration that it specifies.
5267 * Only OpenFlow 1.4 and later specify table vacancy configuration this way,
5268 * so for other 'version' this function always returns
5269 * OFPUTIL_TABLE_VACANCY_DEFAULT. */
5270 static enum ofputil_table_vacancy
5271 ofputil_decode_table_vacancy(ovs_be32 config, enum ofp_version version)
5273 return (version < OFP14_VERSION ? OFPUTIL_TABLE_VACANCY_DEFAULT
5274 : config & htonl(OFPTC14_VACANCY_EVENTS) ? OFPUTIL_TABLE_VACANCY_ON
5275 : OFPUTIL_TABLE_VACANCY_OFF);
5278 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5279 * table configuration (a table mod, table stats, or table features message),
5280 * returns the table eviction configuration that it specifies.
5282 * Only OpenFlow 1.4 and later specify table eviction configuration this way,
5283 * so for other 'version' values this function always returns
5284 * OFPUTIL_TABLE_EVICTION_DEFAULT. */
5285 static enum ofputil_table_eviction
5286 ofputil_decode_table_eviction(ovs_be32 config, enum ofp_version version)
5288 return (version < OFP14_VERSION ? OFPUTIL_TABLE_EVICTION_DEFAULT
5289 : config & htonl(OFPTC14_EVICTION) ? OFPUTIL_TABLE_EVICTION_ON
5290 : OFPUTIL_TABLE_EVICTION_OFF);
5293 /* Returns a bitmap of OFPTC* values suitable for 'config' fields in various
5294 * OpenFlow messages of the given 'version', based on the provided 'miss' and
5295 * 'eviction' values. */
5297 ofputil_encode_table_config(enum ofputil_table_miss miss,
5298 enum ofputil_table_eviction eviction,
5299 enum ofputil_table_vacancy vacancy,
5300 enum ofp_version version)
5302 uint32_t config = 0;
5303 /* See the section "OFPTC_* Table Configuration" in DESIGN.md for more
5304 * information on the crazy evolution of this field. */
5307 /* OpenFlow 1.0 didn't have such a field, any value ought to do. */
5312 /* OpenFlow 1.1 and 1.2 define only OFPTC11_TABLE_MISS_*. */
5314 case OFPUTIL_TABLE_MISS_DEFAULT:
5315 /* Really this shouldn't be used for encoding (the caller should
5316 * provide a specific value) but I can't imagine that defaulting to
5317 * the fall-through case here will hurt. */
5318 case OFPUTIL_TABLE_MISS_CONTROLLER:
5320 return htonl(OFPTC11_TABLE_MISS_CONTROLLER);
5321 case OFPUTIL_TABLE_MISS_CONTINUE:
5322 return htonl(OFPTC11_TABLE_MISS_CONTINUE);
5323 case OFPUTIL_TABLE_MISS_DROP:
5324 return htonl(OFPTC11_TABLE_MISS_DROP);
5329 /* OpenFlow 1.3 removed OFPTC11_TABLE_MISS_* and didn't define any new
5330 * flags, so this is correct. */
5335 /* OpenFlow 1.4 introduced OFPTC14_EVICTION and
5336 * OFPTC14_VACANCY_EVENTS. */
5337 if (eviction == OFPUTIL_TABLE_EVICTION_ON) {
5338 config |= OFPTC14_EVICTION;
5340 if (vacancy == OFPUTIL_TABLE_VACANCY_ON) {
5341 config |= OFPTC14_VACANCY_EVENTS;
5343 return htonl(config);
5349 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5350 * table configuration (a table mod, table stats, or table features message),
5351 * returns the table miss configuration that it specifies.
5353 * Only OpenFlow 1.1 and 1.2 specify table miss configurations this way, so for
5354 * other 'version' values this function always returns
5355 * OFPUTIL_TABLE_MISS_DEFAULT. */
5356 static enum ofputil_table_miss
5357 ofputil_decode_table_miss(ovs_be32 config_, enum ofp_version version)
5359 uint32_t config = ntohl(config_);
5361 if (version == OFP11_VERSION || version == OFP12_VERSION) {
5362 switch (config & OFPTC11_TABLE_MISS_MASK) {
5363 case OFPTC11_TABLE_MISS_CONTROLLER:
5364 return OFPUTIL_TABLE_MISS_CONTROLLER;
5366 case OFPTC11_TABLE_MISS_CONTINUE:
5367 return OFPUTIL_TABLE_MISS_CONTINUE;
5369 case OFPTC11_TABLE_MISS_DROP:
5370 return OFPUTIL_TABLE_MISS_DROP;
5373 VLOG_WARN_RL(&bad_ofmsg_rl, "bad table miss config %d", config);
5374 return OFPUTIL_TABLE_MISS_CONTROLLER;
5377 return OFPUTIL_TABLE_MISS_DEFAULT;
5381 /* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in
5382 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
5384 ofputil_decode_table_mod(const struct ofp_header *oh,
5385 struct ofputil_table_mod *pm)
5387 memset(pm, 0, sizeof *pm);
5388 pm->miss = OFPUTIL_TABLE_MISS_DEFAULT;
5389 pm->eviction = OFPUTIL_TABLE_EVICTION_DEFAULT;
5390 pm->eviction_flags = UINT32_MAX;
5391 pm->vacancy = OFPUTIL_TABLE_VACANCY_DEFAULT;
5393 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
5394 enum ofpraw raw = ofpraw_pull_assert(&b);
5395 if (raw == OFPRAW_OFPT11_TABLE_MOD) {
5396 const struct ofp11_table_mod *otm = b.data;
5398 pm->table_id = otm->table_id;
5399 pm->miss = ofputil_decode_table_miss(otm->config, oh->version);
5400 } else if (raw == OFPRAW_OFPT14_TABLE_MOD) {
5401 const struct ofp14_table_mod *otm = ofpbuf_pull(&b, sizeof *otm);
5403 pm->table_id = otm->table_id;
5404 pm->miss = ofputil_decode_table_miss(otm->config, oh->version);
5405 pm->eviction = ofputil_decode_table_eviction(otm->config, oh->version);
5406 pm->vacancy = ofputil_decode_table_vacancy(otm->config, oh->version);
5407 while (b.size > 0) {
5408 struct ofpbuf property;
5412 error = ofpprop_pull(&b, &property, &type);
5418 case OFPTMPT14_EVICTION:
5419 error = ofpprop_parse_u32(&property, &pm->eviction);
5422 case OFPTMPT14_VACANCY:
5423 error = parse_table_mod_vacancy_property(&property, pm);
5427 error = OFPERR_OFPBRC_BAD_TYPE;
5436 return OFPERR_OFPBRC_BAD_TYPE;
5442 /* Converts the abstract form of a "table mod" message in '*tm' into an
5443 * OpenFlow message suitable for 'protocol', and returns that encoded form in a
5444 * buffer owned by the caller. */
5446 ofputil_encode_table_mod(const struct ofputil_table_mod *tm,
5447 enum ofputil_protocol protocol)
5449 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
5452 switch (ofp_version) {
5453 case OFP10_VERSION: {
5454 ovs_fatal(0, "table mod needs OpenFlow 1.1 or later "
5455 "(\'-O OpenFlow11\')");
5460 case OFP13_VERSION: {
5461 struct ofp11_table_mod *otm;
5463 b = ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD, ofp_version, 0);
5464 otm = ofpbuf_put_zeros(b, sizeof *otm);
5465 otm->table_id = tm->table_id;
5466 otm->config = ofputil_encode_table_config(tm->miss, tm->eviction,
5467 tm->vacancy, ofp_version);
5471 case OFP15_VERSION: {
5472 struct ofp14_table_mod *otm;
5474 b = ofpraw_alloc(OFPRAW_OFPT14_TABLE_MOD, ofp_version, 0);
5475 otm = ofpbuf_put_zeros(b, sizeof *otm);
5476 otm->table_id = tm->table_id;
5477 otm->config = ofputil_encode_table_config(tm->miss, tm->eviction,
5478 tm->vacancy, ofp_version);
5480 if (tm->eviction_flags != UINT32_MAX) {
5481 ofpprop_put_u32(b, OFPTMPT14_EVICTION, tm->eviction_flags);
5483 if (tm->vacancy == OFPUTIL_TABLE_VACANCY_ON) {
5484 struct ofp14_table_mod_prop_vacancy *otv;
5486 otv = ofpprop_put_zeros(b, OFPTMPT14_VACANCY, sizeof *otv);
5487 otv->vacancy_down = tm->table_vacancy.vacancy_down;
5488 otv->vacancy_up = tm->table_vacancy.vacancy_up;
5499 /* ofputil_role_request */
5501 /* Decodes the OpenFlow "role request" or "role reply" message in '*oh' into
5502 * an abstract form in '*rr'. Returns 0 if successful, otherwise an
5503 * OFPERR_* value. */
5505 ofputil_decode_role_message(const struct ofp_header *oh,
5506 struct ofputil_role_request *rr)
5508 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
5509 enum ofpraw raw = ofpraw_pull_assert(&b);
5510 if (raw == OFPRAW_OFPT12_ROLE_REQUEST ||
5511 raw == OFPRAW_OFPT12_ROLE_REPLY) {
5512 const struct ofp12_role_request *orr = b.msg;
5514 if (orr->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
5515 orr->role != htonl(OFPCR12_ROLE_EQUAL) &&
5516 orr->role != htonl(OFPCR12_ROLE_MASTER) &&
5517 orr->role != htonl(OFPCR12_ROLE_SLAVE)) {
5518 return OFPERR_OFPRRFC_BAD_ROLE;
5521 rr->role = ntohl(orr->role);
5522 if (raw == OFPRAW_OFPT12_ROLE_REQUEST
5523 ? orr->role == htonl(OFPCR12_ROLE_NOCHANGE)
5524 : orr->generation_id == OVS_BE64_MAX) {
5525 rr->have_generation_id = false;
5526 rr->generation_id = 0;
5528 rr->have_generation_id = true;
5529 rr->generation_id = ntohll(orr->generation_id);
5531 } else if (raw == OFPRAW_NXT_ROLE_REQUEST ||
5532 raw == OFPRAW_NXT_ROLE_REPLY) {
5533 const struct nx_role_request *nrr = b.msg;
5535 BUILD_ASSERT(NX_ROLE_OTHER + 1 == OFPCR12_ROLE_EQUAL);
5536 BUILD_ASSERT(NX_ROLE_MASTER + 1 == OFPCR12_ROLE_MASTER);
5537 BUILD_ASSERT(NX_ROLE_SLAVE + 1 == OFPCR12_ROLE_SLAVE);
5539 if (nrr->role != htonl(NX_ROLE_OTHER) &&
5540 nrr->role != htonl(NX_ROLE_MASTER) &&
5541 nrr->role != htonl(NX_ROLE_SLAVE)) {
5542 return OFPERR_OFPRRFC_BAD_ROLE;
5545 rr->role = ntohl(nrr->role) + 1;
5546 rr->have_generation_id = false;
5547 rr->generation_id = 0;
5555 /* Returns an encoded form of a role reply suitable for the "request" in a
5556 * buffer owned by the caller. */
5558 ofputil_encode_role_reply(const struct ofp_header *request,
5559 const struct ofputil_role_request *rr)
5564 raw = ofpraw_decode_assert(request);
5565 if (raw == OFPRAW_OFPT12_ROLE_REQUEST) {
5566 struct ofp12_role_request *orr;
5568 buf = ofpraw_alloc_reply(OFPRAW_OFPT12_ROLE_REPLY, request, 0);
5569 orr = ofpbuf_put_zeros(buf, sizeof *orr);
5571 orr->role = htonl(rr->role);
5572 orr->generation_id = htonll(rr->have_generation_id
5575 } else if (raw == OFPRAW_NXT_ROLE_REQUEST) {
5576 struct nx_role_request *nrr;
5578 BUILD_ASSERT(NX_ROLE_OTHER == OFPCR12_ROLE_EQUAL - 1);
5579 BUILD_ASSERT(NX_ROLE_MASTER == OFPCR12_ROLE_MASTER - 1);
5580 BUILD_ASSERT(NX_ROLE_SLAVE == OFPCR12_ROLE_SLAVE - 1);
5582 buf = ofpraw_alloc_reply(OFPRAW_NXT_ROLE_REPLY, request, 0);
5583 nrr = ofpbuf_put_zeros(buf, sizeof *nrr);
5584 nrr->role = htonl(rr->role - 1);
5592 /* Encodes "role status" message 'status' for sending in the given
5593 * 'protocol'. Returns the role status message, if 'protocol' supports them,
5594 * otherwise a null pointer. */
5596 ofputil_encode_role_status(const struct ofputil_role_status *status,
5597 enum ofputil_protocol protocol)
5599 enum ofp_version version;
5601 version = ofputil_protocol_to_ofp_version(protocol);
5602 if (version >= OFP14_VERSION) {
5603 struct ofp14_role_status *rstatus;
5606 buf = ofpraw_alloc_xid(OFPRAW_OFPT14_ROLE_STATUS, version, htonl(0),
5608 rstatus = ofpbuf_put_zeros(buf, sizeof *rstatus);
5609 rstatus->role = htonl(status->role);
5610 rstatus->reason = status->reason;
5611 rstatus->generation_id = htonll(status->generation_id);
5620 ofputil_decode_role_status(const struct ofp_header *oh,
5621 struct ofputil_role_status *rs)
5623 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
5624 enum ofpraw raw = ofpraw_pull_assert(&b);
5625 ovs_assert(raw == OFPRAW_OFPT14_ROLE_STATUS);
5627 const struct ofp14_role_status *r = b.msg;
5628 if (r->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
5629 r->role != htonl(OFPCR12_ROLE_EQUAL) &&
5630 r->role != htonl(OFPCR12_ROLE_MASTER) &&
5631 r->role != htonl(OFPCR12_ROLE_SLAVE)) {
5632 return OFPERR_OFPRRFC_BAD_ROLE;
5635 rs->role = ntohl(r->role);
5636 rs->generation_id = ntohll(r->generation_id);
5637 rs->reason = r->reason;
5642 /* Encodes 'rf' according to 'protocol', and returns the encoded message.
5643 * 'protocol' must be for OpenFlow 1.4 or later. */
5645 ofputil_encode_requestforward(const struct ofputil_requestforward *rf,
5646 enum ofputil_protocol protocol)
5648 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
5649 struct ofpbuf *inner;
5651 switch (rf->reason) {
5652 case OFPRFR_GROUP_MOD:
5653 inner = ofputil_encode_group_mod(ofp_version, rf->group_mod);
5656 case OFPRFR_METER_MOD:
5657 inner = ofputil_encode_meter_mod(ofp_version, rf->meter_mod);
5660 case OFPRFR_N_REASONS:
5665 struct ofp_header *inner_oh = inner->data;
5666 inner_oh->xid = rf->xid;
5667 inner_oh->length = htons(inner->size);
5669 struct ofpbuf *outer = ofpraw_alloc_xid(OFPRAW_OFPT14_REQUESTFORWARD,
5670 ofp_version, htonl(0),
5672 ofpbuf_put(outer, inner->data, inner->size);
5673 ofpbuf_delete(inner);
5678 /* Decodes OFPT_REQUESTFORWARD message 'outer'. On success, puts the decoded
5679 * form into '*rf' and returns 0, and the caller is later responsible for
5680 * freeing the content of 'rf', with ofputil_destroy_requestforward(rf). On
5681 * failure, returns an ofperr and '*rf' is indeterminate. */
5683 ofputil_decode_requestforward(const struct ofp_header *outer,
5684 struct ofputil_requestforward *rf)
5686 struct ofpbuf b = ofpbuf_const_initializer(outer, ntohs(outer->length));
5688 /* Skip past outer message. */
5689 enum ofpraw outer_raw = ofpraw_pull_assert(&b);
5690 ovs_assert(outer_raw == OFPRAW_OFPT14_REQUESTFORWARD);
5692 /* Validate inner message. */
5693 if (b.size < sizeof(struct ofp_header)) {
5694 return OFPERR_OFPBFC_MSG_BAD_LEN;
5696 const struct ofp_header *inner = b.data;
5697 unsigned int inner_len = ntohs(inner->length);
5698 if (inner_len < sizeof(struct ofp_header) || inner_len > b.size) {
5699 return OFPERR_OFPBFC_MSG_BAD_LEN;
5701 if (inner->version != outer->version) {
5702 return OFPERR_OFPBRC_BAD_VERSION;
5705 /* Parse inner message. */
5707 enum ofperr error = ofptype_decode(&type, inner);
5712 rf->xid = inner->xid;
5713 if (type == OFPTYPE_GROUP_MOD) {
5714 rf->reason = OFPRFR_GROUP_MOD;
5715 rf->group_mod = xmalloc(sizeof *rf->group_mod);
5716 error = ofputil_decode_group_mod(inner, rf->group_mod);
5718 free(rf->group_mod);
5721 } else if (type == OFPTYPE_METER_MOD) {
5722 rf->reason = OFPRFR_METER_MOD;
5723 rf->meter_mod = xmalloc(sizeof *rf->meter_mod);
5724 ofpbuf_init(&rf->bands, 64);
5725 error = ofputil_decode_meter_mod(inner, rf->meter_mod, &rf->bands);
5727 free(rf->meter_mod);
5728 ofpbuf_uninit(&rf->bands);
5732 return OFPERR_OFPBFC_MSG_UNSUP;
5738 /* Frees the content of 'rf', which should have been initialized through a
5739 * successful call to ofputil_decode_requestforward(). */
5741 ofputil_destroy_requestforward(struct ofputil_requestforward *rf)
5747 switch (rf->reason) {
5748 case OFPRFR_GROUP_MOD:
5749 ofputil_uninit_group_mod(rf->group_mod);
5750 free(rf->group_mod);
5753 case OFPRFR_METER_MOD:
5754 ofpbuf_uninit(&rf->bands);
5755 free(rf->meter_mod);
5758 case OFPRFR_N_REASONS:
5765 /* OpenFlow 1.0 and 1.1 don't distinguish between a field that cannot be
5766 * matched and a field that must be wildcarded. This function returns a bitmap
5767 * that contains both kinds of fields. */
5768 static struct mf_bitmap
5769 wild_or_nonmatchable_fields(const struct ofputil_table_features *features)
5771 struct mf_bitmap wc = features->match;
5772 bitmap_not(wc.bm, MFF_N_IDS);
5773 bitmap_or(wc.bm, features->wildcard.bm, MFF_N_IDS);
5777 struct ofp10_wc_map {
5778 enum ofp10_flow_wildcards wc10;
5779 enum mf_field_id mf;
5782 static const struct ofp10_wc_map ofp10_wc_map[] = {
5783 { OFPFW10_IN_PORT, MFF_IN_PORT },
5784 { OFPFW10_DL_VLAN, MFF_VLAN_VID },
5785 { OFPFW10_DL_SRC, MFF_ETH_SRC },
5786 { OFPFW10_DL_DST, MFF_ETH_DST},
5787 { OFPFW10_DL_TYPE, MFF_ETH_TYPE },
5788 { OFPFW10_NW_PROTO, MFF_IP_PROTO },
5789 { OFPFW10_TP_SRC, MFF_TCP_SRC },
5790 { OFPFW10_TP_DST, MFF_TCP_DST },
5791 { OFPFW10_NW_SRC_MASK, MFF_IPV4_SRC },
5792 { OFPFW10_NW_DST_MASK, MFF_IPV4_DST },
5793 { OFPFW10_DL_VLAN_PCP, MFF_VLAN_PCP },
5794 { OFPFW10_NW_TOS, MFF_IP_DSCP },
5798 mf_bitmap_to_of10(const struct mf_bitmap *fields)
5800 const struct ofp10_wc_map *p;
5803 for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
5804 if (bitmap_is_set(fields->bm, p->mf)) {
5811 static struct mf_bitmap
5812 mf_bitmap_from_of10(ovs_be32 wc10_)
5814 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
5815 const struct ofp10_wc_map *p;
5816 uint32_t wc10 = ntohl(wc10_);
5818 for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
5819 if (wc10 & p->wc10) {
5820 bitmap_set1(fields.bm, p->mf);
5827 ofputil_put_ofp10_table_stats(const struct ofputil_table_stats *stats,
5828 const struct ofputil_table_features *features,
5831 struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
5832 struct ofp10_table_stats *out;
5834 out = ofpbuf_put_zeros(buf, sizeof *out);
5835 out->table_id = features->table_id;
5836 ovs_strlcpy(out->name, features->name, sizeof out->name);
5837 out->wildcards = mf_bitmap_to_of10(&wc);
5838 out->max_entries = htonl(features->max_entries);
5839 out->active_count = htonl(stats->active_count);
5840 put_32aligned_be64(&out->lookup_count, htonll(stats->lookup_count));
5841 put_32aligned_be64(&out->matched_count, htonll(stats->matched_count));
5844 struct ofp11_wc_map {
5845 enum ofp11_flow_match_fields wc11;
5846 enum mf_field_id mf;
5849 static const struct ofp11_wc_map ofp11_wc_map[] = {
5850 { OFPFMF11_IN_PORT, MFF_IN_PORT },
5851 { OFPFMF11_DL_VLAN, MFF_VLAN_VID },
5852 { OFPFMF11_DL_VLAN_PCP, MFF_VLAN_PCP },
5853 { OFPFMF11_DL_TYPE, MFF_ETH_TYPE },
5854 { OFPFMF11_NW_TOS, MFF_IP_DSCP },
5855 { OFPFMF11_NW_PROTO, MFF_IP_PROTO },
5856 { OFPFMF11_TP_SRC, MFF_TCP_SRC },
5857 { OFPFMF11_TP_DST, MFF_TCP_DST },
5858 { OFPFMF11_MPLS_LABEL, MFF_MPLS_LABEL },
5859 { OFPFMF11_MPLS_TC, MFF_MPLS_TC },
5860 /* I don't know what OFPFMF11_TYPE means. */
5861 { OFPFMF11_DL_SRC, MFF_ETH_SRC },
5862 { OFPFMF11_DL_DST, MFF_ETH_DST },
5863 { OFPFMF11_NW_SRC, MFF_IPV4_SRC },
5864 { OFPFMF11_NW_DST, MFF_IPV4_DST },
5865 { OFPFMF11_METADATA, MFF_METADATA },
5869 mf_bitmap_to_of11(const struct mf_bitmap *fields)
5871 const struct ofp11_wc_map *p;
5874 for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
5875 if (bitmap_is_set(fields->bm, p->mf)) {
5882 static struct mf_bitmap
5883 mf_bitmap_from_of11(ovs_be32 wc11_)
5885 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
5886 const struct ofp11_wc_map *p;
5887 uint32_t wc11 = ntohl(wc11_);
5889 for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
5890 if (wc11 & p->wc11) {
5891 bitmap_set1(fields.bm, p->mf);
5898 ofputil_put_ofp11_table_stats(const struct ofputil_table_stats *stats,
5899 const struct ofputil_table_features *features,
5902 struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
5903 struct ofp11_table_stats *out;
5905 out = ofpbuf_put_zeros(buf, sizeof *out);
5906 out->table_id = features->table_id;
5907 ovs_strlcpy(out->name, features->name, sizeof out->name);
5908 out->wildcards = mf_bitmap_to_of11(&wc);
5909 out->match = mf_bitmap_to_of11(&features->match);
5910 out->instructions = ovsinst_bitmap_to_openflow(
5911 features->nonmiss.instructions, OFP11_VERSION);
5912 out->write_actions = ofpact_bitmap_to_openflow(
5913 features->nonmiss.write.ofpacts, OFP11_VERSION);
5914 out->apply_actions = ofpact_bitmap_to_openflow(
5915 features->nonmiss.apply.ofpacts, OFP11_VERSION);
5916 out->config = htonl(features->miss_config);
5917 out->max_entries = htonl(features->max_entries);
5918 out->active_count = htonl(stats->active_count);
5919 out->lookup_count = htonll(stats->lookup_count);
5920 out->matched_count = htonll(stats->matched_count);
5924 ofputil_put_ofp12_table_stats(const struct ofputil_table_stats *stats,
5925 const struct ofputil_table_features *features,
5928 struct ofp12_table_stats *out;
5930 out = ofpbuf_put_zeros(buf, sizeof *out);
5931 out->table_id = features->table_id;
5932 ovs_strlcpy(out->name, features->name, sizeof out->name);
5933 out->match = oxm_bitmap_from_mf_bitmap(&features->match, OFP12_VERSION);
5934 out->wildcards = oxm_bitmap_from_mf_bitmap(&features->wildcard,
5936 out->write_actions = ofpact_bitmap_to_openflow(
5937 features->nonmiss.write.ofpacts, OFP12_VERSION);
5938 out->apply_actions = ofpact_bitmap_to_openflow(
5939 features->nonmiss.apply.ofpacts, OFP12_VERSION);
5940 out->write_setfields = oxm_bitmap_from_mf_bitmap(
5941 &features->nonmiss.write.set_fields, OFP12_VERSION);
5942 out->apply_setfields = oxm_bitmap_from_mf_bitmap(
5943 &features->nonmiss.apply.set_fields, OFP12_VERSION);
5944 out->metadata_match = features->metadata_match;
5945 out->metadata_write = features->metadata_write;
5946 out->instructions = ovsinst_bitmap_to_openflow(
5947 features->nonmiss.instructions, OFP12_VERSION);
5948 out->config = ofputil_encode_table_config(features->miss_config,
5949 OFPUTIL_TABLE_EVICTION_DEFAULT,
5950 OFPUTIL_TABLE_VACANCY_DEFAULT,
5952 out->max_entries = htonl(features->max_entries);
5953 out->active_count = htonl(stats->active_count);
5954 out->lookup_count = htonll(stats->lookup_count);
5955 out->matched_count = htonll(stats->matched_count);
5959 ofputil_put_ofp13_table_stats(const struct ofputil_table_stats *stats,
5962 struct ofp13_table_stats *out;
5964 out = ofpbuf_put_zeros(buf, sizeof *out);
5965 out->table_id = stats->table_id;
5966 out->active_count = htonl(stats->active_count);
5967 out->lookup_count = htonll(stats->lookup_count);
5968 out->matched_count = htonll(stats->matched_count);
5972 ofputil_encode_table_stats_reply(const struct ofp_header *request)
5974 return ofpraw_alloc_stats_reply(request, 0);
5978 ofputil_append_table_stats_reply(struct ofpbuf *reply,
5979 const struct ofputil_table_stats *stats,
5980 const struct ofputil_table_features *features)
5982 struct ofp_header *oh = reply->header;
5984 ovs_assert(stats->table_id == features->table_id);
5986 switch ((enum ofp_version) oh->version) {
5988 ofputil_put_ofp10_table_stats(stats, features, reply);
5992 ofputil_put_ofp11_table_stats(stats, features, reply);
5996 ofputil_put_ofp12_table_stats(stats, features, reply);
6002 ofputil_put_ofp13_table_stats(stats, reply);
6011 ofputil_decode_ofp10_table_stats(struct ofpbuf *msg,
6012 struct ofputil_table_stats *stats,
6013 struct ofputil_table_features *features)
6015 struct ofp10_table_stats *ots;
6017 ots = ofpbuf_try_pull(msg, sizeof *ots);
6019 return OFPERR_OFPBRC_BAD_LEN;
6022 features->table_id = ots->table_id;
6023 ovs_strlcpy(features->name, ots->name, sizeof features->name);
6024 features->max_entries = ntohl(ots->max_entries);
6025 features->match = features->wildcard = mf_bitmap_from_of10(ots->wildcards);
6027 stats->table_id = ots->table_id;
6028 stats->active_count = ntohl(ots->active_count);
6029 stats->lookup_count = ntohll(get_32aligned_be64(&ots->lookup_count));
6030 stats->matched_count = ntohll(get_32aligned_be64(&ots->matched_count));
6036 ofputil_decode_ofp11_table_stats(struct ofpbuf *msg,
6037 struct ofputil_table_stats *stats,
6038 struct ofputil_table_features *features)
6040 struct ofp11_table_stats *ots;
6042 ots = ofpbuf_try_pull(msg, sizeof *ots);
6044 return OFPERR_OFPBRC_BAD_LEN;
6047 features->table_id = ots->table_id;
6048 ovs_strlcpy(features->name, ots->name, sizeof features->name);
6049 features->max_entries = ntohl(ots->max_entries);
6050 features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
6051 ots->instructions, OFP11_VERSION);
6052 features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
6053 ots->write_actions, OFP11_VERSION);
6054 features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
6055 ots->write_actions, OFP11_VERSION);
6056 features->miss = features->nonmiss;
6057 features->miss_config = ofputil_decode_table_miss(ots->config,
6059 features->match = mf_bitmap_from_of11(ots->match);
6060 features->wildcard = mf_bitmap_from_of11(ots->wildcards);
6061 bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
6063 stats->table_id = ots->table_id;
6064 stats->active_count = ntohl(ots->active_count);
6065 stats->lookup_count = ntohll(ots->lookup_count);
6066 stats->matched_count = ntohll(ots->matched_count);
6072 ofputil_decode_ofp12_table_stats(struct ofpbuf *msg,
6073 struct ofputil_table_stats *stats,
6074 struct ofputil_table_features *features)
6076 struct ofp12_table_stats *ots;
6078 ots = ofpbuf_try_pull(msg, sizeof *ots);
6080 return OFPERR_OFPBRC_BAD_LEN;
6083 features->table_id = ots->table_id;
6084 ovs_strlcpy(features->name, ots->name, sizeof features->name);
6085 features->metadata_match = ots->metadata_match;
6086 features->metadata_write = ots->metadata_write;
6087 features->miss_config = ofputil_decode_table_miss(ots->config,
6089 features->max_entries = ntohl(ots->max_entries);
6091 features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
6092 ots->instructions, OFP12_VERSION);
6093 features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
6094 ots->write_actions, OFP12_VERSION);
6095 features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
6096 ots->apply_actions, OFP12_VERSION);
6097 features->nonmiss.write.set_fields = oxm_bitmap_to_mf_bitmap(
6098 ots->write_setfields, OFP12_VERSION);
6099 features->nonmiss.apply.set_fields = oxm_bitmap_to_mf_bitmap(
6100 ots->apply_setfields, OFP12_VERSION);
6101 features->miss = features->nonmiss;
6103 features->match = oxm_bitmap_to_mf_bitmap(ots->match, OFP12_VERSION);
6104 features->wildcard = oxm_bitmap_to_mf_bitmap(ots->wildcards,
6106 bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
6108 stats->table_id = ots->table_id;
6109 stats->active_count = ntohl(ots->active_count);
6110 stats->lookup_count = ntohll(ots->lookup_count);
6111 stats->matched_count = ntohll(ots->matched_count);
6117 ofputil_decode_ofp13_table_stats(struct ofpbuf *msg,
6118 struct ofputil_table_stats *stats,
6119 struct ofputil_table_features *features)
6121 struct ofp13_table_stats *ots;
6123 ots = ofpbuf_try_pull(msg, sizeof *ots);
6125 return OFPERR_OFPBRC_BAD_LEN;
6128 features->table_id = ots->table_id;
6130 stats->table_id = ots->table_id;
6131 stats->active_count = ntohl(ots->active_count);
6132 stats->lookup_count = ntohll(ots->lookup_count);
6133 stats->matched_count = ntohll(ots->matched_count);
6139 ofputil_decode_table_stats_reply(struct ofpbuf *msg,
6140 struct ofputil_table_stats *stats,
6141 struct ofputil_table_features *features)
6143 const struct ofp_header *oh;
6146 ofpraw_pull_assert(msg);
6154 memset(stats, 0, sizeof *stats);
6155 memset(features, 0, sizeof *features);
6156 features->supports_eviction = -1;
6157 features->supports_vacancy_events = -1;
6159 switch ((enum ofp_version) oh->version) {
6161 return ofputil_decode_ofp10_table_stats(msg, stats, features);
6164 return ofputil_decode_ofp11_table_stats(msg, stats, features);
6167 return ofputil_decode_ofp12_table_stats(msg, stats, features);
6172 return ofputil_decode_ofp13_table_stats(msg, stats, features);
6179 /* ofputil_flow_monitor_request */
6181 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
6182 * ofputil_flow_monitor_request in 'rq'.
6184 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
6185 * message. Calling this function multiple times for a single 'msg' iterates
6186 * through the requests. The caller must initially leave 'msg''s layer
6187 * pointers null and not modify them between calls.
6189 * Returns 0 if successful, EOF if no requests were left in this 'msg',
6190 * otherwise an OFPERR_* value. */
6192 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
6195 struct nx_flow_monitor_request *nfmr;
6199 ofpraw_pull_assert(msg);
6206 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
6208 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %"PRIu32" "
6209 "leftover bytes at end", msg->size);
6210 return OFPERR_OFPBRC_BAD_LEN;
6213 flags = ntohs(nfmr->flags);
6214 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
6215 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
6216 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
6217 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
6219 return OFPERR_OFPMOFC_BAD_FLAGS;
6222 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
6223 return OFPERR_NXBRC_MUST_BE_ZERO;
6226 rq->id = ntohl(nfmr->id);
6228 rq->out_port = u16_to_ofp(ntohs(nfmr->out_port));
6229 rq->table_id = nfmr->table_id;
6231 return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL, NULL);
6235 ofputil_append_flow_monitor_request(
6236 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
6238 struct nx_flow_monitor_request *nfmr;
6243 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
6246 start_ofs = msg->size;
6247 ofpbuf_put_zeros(msg, sizeof *nfmr);
6248 match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
6250 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
6251 nfmr->id = htonl(rq->id);
6252 nfmr->flags = htons(rq->flags);
6253 nfmr->out_port = htons(ofp_to_u16(rq->out_port));
6254 nfmr->match_len = htons(match_len);
6255 nfmr->table_id = rq->table_id;
6258 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
6259 * into an abstract ofputil_flow_update in 'update'. The caller must have
6260 * initialized update->match to point to space allocated for a match.
6262 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
6263 * actions (except for NXFME_ABBREV, which never includes actions). The caller
6264 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
6265 * will point into the 'ofpacts' buffer.
6267 * Multiple flow updates can be packed into a single OpenFlow message. Calling
6268 * this function multiple times for a single 'msg' iterates through the
6269 * updates. The caller must initially leave 'msg''s layer pointers null and
6270 * not modify them between calls.
6272 * Returns 0 if successful, EOF if no updates were left in this 'msg',
6273 * otherwise an OFPERR_* value. */
6275 ofputil_decode_flow_update(struct ofputil_flow_update *update,
6276 struct ofpbuf *msg, struct ofpbuf *ofpacts)
6278 struct nx_flow_update_header *nfuh;
6279 unsigned int length;
6280 struct ofp_header *oh;
6283 ofpraw_pull_assert(msg);
6286 ofpbuf_clear(ofpacts);
6291 if (msg->size < sizeof(struct nx_flow_update_header)) {
6298 update->event = ntohs(nfuh->event);
6299 length = ntohs(nfuh->length);
6300 if (length > msg->size || length % 8) {
6304 if (update->event == NXFME_ABBREV) {
6305 struct nx_flow_update_abbrev *nfua;
6307 if (length != sizeof *nfua) {
6311 nfua = ofpbuf_pull(msg, sizeof *nfua);
6312 update->xid = nfua->xid;
6314 } else if (update->event == NXFME_ADDED
6315 || update->event == NXFME_DELETED
6316 || update->event == NXFME_MODIFIED) {
6317 struct nx_flow_update_full *nfuf;
6318 unsigned int actions_len;
6319 unsigned int match_len;
6322 if (length < sizeof *nfuf) {
6326 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
6327 match_len = ntohs(nfuf->match_len);
6328 if (sizeof *nfuf + match_len > length) {
6332 update->reason = ntohs(nfuf->reason);
6333 update->idle_timeout = ntohs(nfuf->idle_timeout);
6334 update->hard_timeout = ntohs(nfuf->hard_timeout);
6335 update->table_id = nfuf->table_id;
6336 update->cookie = nfuf->cookie;
6337 update->priority = ntohs(nfuf->priority);
6339 error = nx_pull_match(msg, match_len, update->match, NULL, NULL);
6344 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
6345 error = ofpacts_pull_openflow_actions(msg, actions_len, oh->version,
6351 update->ofpacts = ofpacts->data;
6352 update->ofpacts_len = ofpacts->size;
6355 VLOG_WARN_RL(&bad_ofmsg_rl,
6356 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
6357 ntohs(nfuh->event));
6358 return OFPERR_NXBRC_FM_BAD_EVENT;
6362 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %"PRIu32" "
6363 "leftover bytes at end", msg->size);
6364 return OFPERR_OFPBRC_BAD_LEN;
6368 ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
6370 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
6372 return ntohl(cancel->id);
6376 ofputil_encode_flow_monitor_cancel(uint32_t id)
6378 struct nx_flow_monitor_cancel *nfmc;
6381 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
6382 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
6383 nfmc->id = htonl(id);
6388 ofputil_start_flow_update(struct ovs_list *replies)
6392 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
6396 list_push_back(replies, &msg->list_node);
6400 ofputil_append_flow_update(const struct ofputil_flow_update *update,
6401 struct ovs_list *replies)
6403 enum ofp_version version = ofpmp_version(replies);
6404 struct nx_flow_update_header *nfuh;
6408 msg = ofpbuf_from_list(list_back(replies));
6409 start_ofs = msg->size;
6411 if (update->event == NXFME_ABBREV) {
6412 struct nx_flow_update_abbrev *nfua;
6414 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
6415 nfua->xid = update->xid;
6417 struct nx_flow_update_full *nfuf;
6420 ofpbuf_put_zeros(msg, sizeof *nfuf);
6421 match_len = nx_put_match(msg, update->match, htonll(0), htonll(0));
6422 ofpacts_put_openflow_actions(update->ofpacts, update->ofpacts_len, msg,
6424 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
6425 nfuf->reason = htons(update->reason);
6426 nfuf->priority = htons(update->priority);
6427 nfuf->idle_timeout = htons(update->idle_timeout);
6428 nfuf->hard_timeout = htons(update->hard_timeout);
6429 nfuf->match_len = htons(match_len);
6430 nfuf->table_id = update->table_id;
6431 nfuf->cookie = update->cookie;
6434 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
6435 nfuh->length = htons(msg->size - start_ofs);
6436 nfuh->event = htons(update->event);
6438 ofpmp_postappend(replies, start_ofs);
6442 ofputil_encode_packet_out(const struct ofputil_packet_out *po,
6443 enum ofputil_protocol protocol)
6445 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
6449 size = po->ofpacts_len;
6450 if (po->buffer_id == UINT32_MAX) {
6451 size += po->packet_len;
6454 switch (ofp_version) {
6455 case OFP10_VERSION: {
6456 struct ofp10_packet_out *opo;
6459 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
6460 ofpbuf_put_zeros(msg, sizeof *opo);
6461 actions_ofs = msg->size;
6462 ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
6466 opo->buffer_id = htonl(po->buffer_id);
6467 opo->in_port = htons(ofp_to_u16(po->in_port));
6468 opo->actions_len = htons(msg->size - actions_ofs);
6476 case OFP15_VERSION: {
6477 struct ofp11_packet_out *opo;
6480 msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size);
6481 ofpbuf_put_zeros(msg, sizeof *opo);
6482 len = ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
6485 opo->buffer_id = htonl(po->buffer_id);
6486 opo->in_port = ofputil_port_to_ofp11(po->in_port);
6487 opo->actions_len = htons(len);
6495 if (po->buffer_id == UINT32_MAX) {
6496 ofpbuf_put(msg, po->packet, po->packet_len);
6499 ofpmsg_update_length(msg);
6504 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
6506 make_echo_request(enum ofp_version ofp_version)
6508 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
6512 /* Creates and returns an OFPT_ECHO_REPLY message matching the
6513 * OFPT_ECHO_REQUEST message in 'rq'. */
6515 make_echo_reply(const struct ofp_header *rq)
6517 struct ofpbuf rq_buf = ofpbuf_const_initializer(rq, ntohs(rq->length));
6518 ofpraw_pull_assert(&rq_buf);
6520 struct ofpbuf *reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY,
6522 ofpbuf_put(reply, rq_buf.data, rq_buf.size);
6527 ofputil_encode_barrier_request(enum ofp_version ofp_version)
6531 switch (ofp_version) {
6537 type = OFPRAW_OFPT11_BARRIER_REQUEST;
6541 type = OFPRAW_OFPT10_BARRIER_REQUEST;
6548 return ofpraw_alloc(type, ofp_version, 0);
6552 ofputil_frag_handling_to_string(enum ofputil_frag_handling frag)
6555 case OFPUTIL_FRAG_NORMAL: return "normal";
6556 case OFPUTIL_FRAG_DROP: return "drop";
6557 case OFPUTIL_FRAG_REASM: return "reassemble";
6558 case OFPUTIL_FRAG_NX_MATCH: return "nx-match";
6565 ofputil_frag_handling_from_string(const char *s,
6566 enum ofputil_frag_handling *frag)
6568 if (!strcasecmp(s, "normal")) {
6569 *frag = OFPUTIL_FRAG_NORMAL;
6570 } else if (!strcasecmp(s, "drop")) {
6571 *frag = OFPUTIL_FRAG_DROP;
6572 } else if (!strcasecmp(s, "reassemble")) {
6573 *frag = OFPUTIL_FRAG_REASM;
6574 } else if (!strcasecmp(s, "nx-match")) {
6575 *frag = OFPUTIL_FRAG_NX_MATCH;
6582 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
6583 * port number and stores the latter in '*ofp10_port', for the purpose of
6584 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
6585 * otherwise an OFPERR_* number. On error, stores OFPP_NONE in '*ofp10_port'.
6587 * See the definition of OFP11_MAX for an explanation of the mapping. */
6589 ofputil_port_from_ofp11(ovs_be32 ofp11_port, ofp_port_t *ofp10_port)
6591 uint32_t ofp11_port_h = ntohl(ofp11_port);
6593 if (ofp11_port_h < ofp_to_u16(OFPP_MAX)) {
6594 *ofp10_port = u16_to_ofp(ofp11_port_h);
6596 } else if (ofp11_port_h >= ofp11_to_u32(OFPP11_MAX)) {
6597 *ofp10_port = u16_to_ofp(ofp11_port_h - OFPP11_OFFSET);
6600 *ofp10_port = OFPP_NONE;
6601 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
6602 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
6603 ofp11_port_h, ofp_to_u16(OFPP_MAX) - 1,
6604 ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
6605 return OFPERR_OFPBAC_BAD_OUT_PORT;
6609 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
6610 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
6612 * See the definition of OFP11_MAX for an explanation of the mapping. */
6614 ofputil_port_to_ofp11(ofp_port_t ofp10_port)
6616 return htonl(ofp_to_u16(ofp10_port) < ofp_to_u16(OFPP_MAX)
6617 ? ofp_to_u16(ofp10_port)
6618 : ofp_to_u16(ofp10_port) + OFPP11_OFFSET);
6621 #define OFPUTIL_NAMED_PORTS \
6622 OFPUTIL_NAMED_PORT(IN_PORT) \
6623 OFPUTIL_NAMED_PORT(TABLE) \
6624 OFPUTIL_NAMED_PORT(NORMAL) \
6625 OFPUTIL_NAMED_PORT(FLOOD) \
6626 OFPUTIL_NAMED_PORT(ALL) \
6627 OFPUTIL_NAMED_PORT(CONTROLLER) \
6628 OFPUTIL_NAMED_PORT(LOCAL) \
6629 OFPUTIL_NAMED_PORT(ANY) \
6630 OFPUTIL_NAMED_PORT(UNSET)
6632 /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */
6633 #define OFPUTIL_NAMED_PORTS_WITH_NONE \
6634 OFPUTIL_NAMED_PORTS \
6635 OFPUTIL_NAMED_PORT(NONE)
6637 /* Stores the port number represented by 's' into '*portp'. 's' may be an
6638 * integer or, for reserved ports, the standard OpenFlow name for the port
6641 * Returns true if successful, false if 's' is not a valid OpenFlow port number
6642 * or name. The caller should issue an error message in this case, because
6643 * this function usually does not. (This gives the caller an opportunity to
6644 * look up the port name another way, e.g. by contacting the switch and listing
6645 * the names of all its ports).
6647 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
6648 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
6649 * range as described in include/openflow/openflow-1.1.h. */
6651 ofputil_port_from_string(const char *s, ofp_port_t *portp)
6653 unsigned int port32; /* int is at least 32 bits wide. */
6656 VLOG_WARN("Negative value %s is not a valid port number.", s);
6660 if (str_to_uint(s, 10, &port32)) {
6661 if (port32 < ofp_to_u16(OFPP_MAX)) {
6663 } else if (port32 < ofp_to_u16(OFPP_FIRST_RESV)) {
6664 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
6665 "be translated to %u when talking to an OF1.1 or "
6666 "later controller", port32, port32 + OFPP11_OFFSET);
6667 } else if (port32 <= ofp_to_u16(OFPP_LAST_RESV)) {
6668 char name[OFP_MAX_PORT_NAME_LEN];
6670 ofputil_port_to_string(u16_to_ofp(port32), name, sizeof name);
6671 VLOG_WARN_ONCE("referring to port %s as %"PRIu32" is deprecated "
6672 "for compatibility with OpenFlow 1.1 and later",
6674 } else if (port32 < ofp11_to_u32(OFPP11_MAX)) {
6675 VLOG_WARN("port %u is outside the supported range 0 through "
6676 "%"PRIx16" or 0x%x through 0x%"PRIx32, port32,
6677 UINT16_MAX, ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
6680 port32 -= OFPP11_OFFSET;
6683 *portp = u16_to_ofp(port32);
6690 static const struct pair pairs[] = {
6691 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
6692 OFPUTIL_NAMED_PORTS_WITH_NONE
6693 #undef OFPUTIL_NAMED_PORT
6695 const struct pair *p;
6697 for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) {
6698 if (!strcasecmp(s, p->name)) {
6707 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
6708 * Most ports' string representation is just the port number, but for special
6709 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
6711 ofputil_format_port(ofp_port_t port, struct ds *s)
6713 char name[OFP_MAX_PORT_NAME_LEN];
6715 ofputil_port_to_string(port, name, sizeof name);
6716 ds_put_cstr(s, name);
6719 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
6720 * representation of OpenFlow port number 'port'. Most ports are represented
6721 * as just the port number, but special ports, e.g. OFPP_LOCAL, are represented
6722 * by name, e.g. "LOCAL". */
6724 ofputil_port_to_string(ofp_port_t port,
6725 char namebuf[OFP_MAX_PORT_NAME_LEN], size_t bufsize)
6728 #define OFPUTIL_NAMED_PORT(NAME) \
6730 ovs_strlcpy(namebuf, #NAME, bufsize); \
6733 #undef OFPUTIL_NAMED_PORT
6736 snprintf(namebuf, bufsize, "%"PRIu16, port);
6741 /* Stores the group id represented by 's' into '*group_idp'. 's' may be an
6742 * integer or, for reserved group IDs, the standard OpenFlow name for the group
6743 * (either "ANY" or "ALL").
6745 * Returns true if successful, false if 's' is not a valid OpenFlow group ID or
6748 ofputil_group_from_string(const char *s, uint32_t *group_idp)
6750 if (!strcasecmp(s, "any")) {
6751 *group_idp = OFPG_ANY;
6752 } else if (!strcasecmp(s, "all")) {
6753 *group_idp = OFPG_ALL;
6754 } else if (!str_to_uint(s, 10, group_idp)) {
6755 VLOG_WARN("%s is not a valid group ID. (Valid group IDs are "
6756 "32-bit nonnegative integers or the keywords ANY or "
6764 /* Appends to 's' a string representation of the OpenFlow group ID 'group_id'.
6765 * Most groups' string representation is just the number, but for special
6766 * groups, e.g. OFPG_ALL, it is the name, e.g. "ALL". */
6768 ofputil_format_group(uint32_t group_id, struct ds *s)
6770 char name[MAX_GROUP_NAME_LEN];
6772 ofputil_group_to_string(group_id, name, sizeof name);
6773 ds_put_cstr(s, name);
6777 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
6778 * representation of OpenFlow group ID 'group_id'. Most group are represented
6779 * as just their number, but special groups, e.g. OFPG_ALL, are represented
6780 * by name, e.g. "ALL". */
6782 ofputil_group_to_string(uint32_t group_id,
6783 char namebuf[MAX_GROUP_NAME_LEN + 1], size_t bufsize)
6787 ovs_strlcpy(namebuf, "ALL", bufsize);
6791 ovs_strlcpy(namebuf, "ANY", bufsize);
6795 snprintf(namebuf, bufsize, "%"PRIu32, group_id);
6800 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
6801 * 'ofp_version', tries to pull the first element from the array. If
6802 * successful, initializes '*pp' with an abstract representation of the
6803 * port and returns 0. If no ports remain to be decoded, returns EOF.
6804 * On an error, returns a positive OFPERR_* value. */
6806 ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
6807 struct ofputil_phy_port *pp)
6809 memset(pp, 0, sizeof *pp);
6811 switch (ofp_version) {
6812 case OFP10_VERSION: {
6813 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
6814 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
6818 case OFP13_VERSION: {
6819 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
6820 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
6824 return b->size ? ofputil_pull_ofp14_port(pp, b) : EOF;
6831 ofputil_normalize_match__(struct match *match, bool may_log)
6834 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
6835 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
6836 MAY_NW_PROTO = 1 << 2, /* nw_proto */
6837 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
6838 MAY_ARP_SHA = 1 << 4, /* arp_sha */
6839 MAY_ARP_THA = 1 << 5, /* arp_tha */
6840 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
6841 MAY_ND_TARGET = 1 << 7, /* nd_target */
6842 MAY_MPLS = 1 << 8, /* mpls label and tc */
6845 struct flow_wildcards wc;
6847 /* Figure out what fields may be matched. */
6848 if (match->flow.dl_type == htons(ETH_TYPE_IP)) {
6849 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
6850 if (match->flow.nw_proto == IPPROTO_TCP ||
6851 match->flow.nw_proto == IPPROTO_UDP ||
6852 match->flow.nw_proto == IPPROTO_SCTP ||
6853 match->flow.nw_proto == IPPROTO_ICMP) {
6854 may_match |= MAY_TP_ADDR;
6856 } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
6857 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
6858 if (match->flow.nw_proto == IPPROTO_TCP ||
6859 match->flow.nw_proto == IPPROTO_UDP ||
6860 match->flow.nw_proto == IPPROTO_SCTP) {
6861 may_match |= MAY_TP_ADDR;
6862 } else if (match->flow.nw_proto == IPPROTO_ICMPV6) {
6863 may_match |= MAY_TP_ADDR;
6864 if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
6865 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
6866 } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
6867 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
6870 } else if (match->flow.dl_type == htons(ETH_TYPE_ARP) ||
6871 match->flow.dl_type == htons(ETH_TYPE_RARP)) {
6872 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
6873 } else if (eth_type_mpls(match->flow.dl_type)) {
6874 may_match = MAY_MPLS;
6879 /* Clear the fields that may not be matched. */
6881 if (!(may_match & MAY_NW_ADDR)) {
6882 wc.masks.nw_src = wc.masks.nw_dst = htonl(0);
6884 if (!(may_match & MAY_TP_ADDR)) {
6885 wc.masks.tp_src = wc.masks.tp_dst = htons(0);
6887 if (!(may_match & MAY_NW_PROTO)) {
6888 wc.masks.nw_proto = 0;
6890 if (!(may_match & MAY_IPVx)) {
6891 wc.masks.nw_tos = 0;
6892 wc.masks.nw_ttl = 0;
6894 if (!(may_match & MAY_ARP_SHA)) {
6895 WC_UNMASK_FIELD(&wc, arp_sha);
6897 if (!(may_match & MAY_ARP_THA)) {
6898 WC_UNMASK_FIELD(&wc, arp_tha);
6900 if (!(may_match & MAY_IPV6)) {
6901 wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any;
6902 wc.masks.ipv6_label = htonl(0);
6904 if (!(may_match & MAY_ND_TARGET)) {
6905 wc.masks.nd_target = in6addr_any;
6907 if (!(may_match & MAY_MPLS)) {
6908 memset(wc.masks.mpls_lse, 0, sizeof wc.masks.mpls_lse);
6911 /* Log any changes. */
6912 if (!flow_wildcards_equal(&wc, &match->wc)) {
6913 bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
6914 char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL;
6917 match_zero_wildcarded_fields(match);
6920 char *post = match_to_string(match, OFP_DEFAULT_PRIORITY);
6921 VLOG_INFO("normalization changed ofp_match, details:");
6922 VLOG_INFO(" pre: %s", pre);
6923 VLOG_INFO("post: %s", post);
6930 /* "Normalizes" the wildcards in 'match'. That means:
6932 * 1. If the type of level N is known, then only the valid fields for that
6933 * level may be specified. For example, ARP does not have a TOS field,
6934 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
6935 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
6936 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
6939 * 2. If the type of level N is not known (or not understood by Open
6940 * vSwitch), then no fields at all for that level may be specified. For
6941 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
6942 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
6945 * If this function changes 'match', it logs a rate-limited informational
6948 ofputil_normalize_match(struct match *match)
6950 ofputil_normalize_match__(match, true);
6953 /* Same as ofputil_normalize_match() without the logging. Thus, this function
6954 * is suitable for a program's internal use, whereas ofputil_normalize_match()
6955 * sense for use on flows received from elsewhere (so that a bug in the program
6956 * that sent them can be reported and corrected). */
6958 ofputil_normalize_match_quiet(struct match *match)
6960 ofputil_normalize_match__(match, false);
6963 /* Parses a key or a key-value pair from '*stringp'.
6965 * On success: Stores the key into '*keyp'. Stores the value, if present, into
6966 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
6967 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
6968 * are substrings of '*stringp' created by replacing some of its bytes by null
6969 * terminators. Returns true.
6971 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
6972 * NULL and returns false. */
6974 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
6976 char *pos, *key, *value;
6980 pos += strspn(pos, ", \t\r\n");
6982 *keyp = *valuep = NULL;
6987 key_len = strcspn(pos, ":=(, \t\r\n");
6988 if (key[key_len] == ':' || key[key_len] == '=') {
6989 /* The value can be separated by a colon. */
6992 value = key + key_len + 1;
6993 value_len = strcspn(value, ", \t\r\n");
6994 pos = value + value_len + (value[value_len] != '\0');
6995 value[value_len] = '\0';
6996 } else if (key[key_len] == '(') {
6997 /* The value can be surrounded by balanced parentheses. The outermost
6998 * set of parentheses is removed. */
7002 value = key + key_len + 1;
7003 for (value_len = 0; level > 0; value_len++) {
7004 switch (value[value_len]) {
7018 value[value_len - 1] = '\0';
7019 pos = value + value_len;
7021 /* There might be no value at all. */
7022 value = key + key_len; /* Will become the empty string below. */
7023 pos = key + key_len + (key[key_len] != '\0');
7025 key[key_len] = '\0';
7033 /* Encode a dump ports request for 'port', the encoded message
7034 * will be for OpenFlow version 'ofp_version'. Returns message
7035 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
7037 ofputil_encode_dump_ports_request(enum ofp_version ofp_version, ofp_port_t port)
7039 struct ofpbuf *request;
7041 switch (ofp_version) {
7042 case OFP10_VERSION: {
7043 struct ofp10_port_stats_request *req;
7044 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0);
7045 req = ofpbuf_put_zeros(request, sizeof *req);
7046 req->port_no = htons(ofp_to_u16(port));
7053 case OFP15_VERSION: {
7054 struct ofp11_port_stats_request *req;
7055 request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0);
7056 req = ofpbuf_put_zeros(request, sizeof *req);
7057 req->port_no = ofputil_port_to_ofp11(port);
7068 ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops,
7069 struct ofp10_port_stats *ps10)
7071 ps10->port_no = htons(ofp_to_u16(ops->port_no));
7072 memset(ps10->pad, 0, sizeof ps10->pad);
7073 put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets));
7074 put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets));
7075 put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes));
7076 put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes));
7077 put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped));
7078 put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped));
7079 put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors));
7080 put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors));
7081 put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors));
7082 put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors));
7083 put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors));
7084 put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions));
7088 ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops,
7089 struct ofp11_port_stats *ps11)
7091 ps11->port_no = ofputil_port_to_ofp11(ops->port_no);
7092 memset(ps11->pad, 0, sizeof ps11->pad);
7093 ps11->rx_packets = htonll(ops->stats.rx_packets);
7094 ps11->tx_packets = htonll(ops->stats.tx_packets);
7095 ps11->rx_bytes = htonll(ops->stats.rx_bytes);
7096 ps11->tx_bytes = htonll(ops->stats.tx_bytes);
7097 ps11->rx_dropped = htonll(ops->stats.rx_dropped);
7098 ps11->tx_dropped = htonll(ops->stats.tx_dropped);
7099 ps11->rx_errors = htonll(ops->stats.rx_errors);
7100 ps11->tx_errors = htonll(ops->stats.tx_errors);
7101 ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors);
7102 ps11->rx_over_err = htonll(ops->stats.rx_over_errors);
7103 ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors);
7104 ps11->collisions = htonll(ops->stats.collisions);
7108 ofputil_port_stats_to_ofp13(const struct ofputil_port_stats *ops,
7109 struct ofp13_port_stats *ps13)
7111 ofputil_port_stats_to_ofp11(ops, &ps13->ps);
7112 ps13->duration_sec = htonl(ops->duration_sec);
7113 ps13->duration_nsec = htonl(ops->duration_nsec);
7117 ofputil_append_ofp14_port_stats(const struct ofputil_port_stats *ops,
7118 struct ovs_list *replies)
7120 struct ofp14_port_stats_prop_ethernet *eth;
7121 struct ofp14_port_stats *ps14;
7122 struct ofpbuf *reply;
7124 reply = ofpmp_reserve(replies, sizeof *ps14 + sizeof *eth);
7126 ps14 = ofpbuf_put_uninit(reply, sizeof *ps14);
7127 ps14->length = htons(sizeof *ps14 + sizeof *eth);
7128 memset(ps14->pad, 0, sizeof ps14->pad);
7129 ps14->port_no = ofputil_port_to_ofp11(ops->port_no);
7130 ps14->duration_sec = htonl(ops->duration_sec);
7131 ps14->duration_nsec = htonl(ops->duration_nsec);
7132 ps14->rx_packets = htonll(ops->stats.rx_packets);
7133 ps14->tx_packets = htonll(ops->stats.tx_packets);
7134 ps14->rx_bytes = htonll(ops->stats.rx_bytes);
7135 ps14->tx_bytes = htonll(ops->stats.tx_bytes);
7136 ps14->rx_dropped = htonll(ops->stats.rx_dropped);
7137 ps14->tx_dropped = htonll(ops->stats.tx_dropped);
7138 ps14->rx_errors = htonll(ops->stats.rx_errors);
7139 ps14->tx_errors = htonll(ops->stats.tx_errors);
7141 eth = ofpprop_put_zeros(reply, OFPPSPT14_ETHERNET, sizeof *eth);
7142 eth->rx_frame_err = htonll(ops->stats.rx_frame_errors);
7143 eth->rx_over_err = htonll(ops->stats.rx_over_errors);
7144 eth->rx_crc_err = htonll(ops->stats.rx_crc_errors);
7145 eth->collisions = htonll(ops->stats.collisions);
7148 /* Encode a ports stat for 'ops' and append it to 'replies'. */
7150 ofputil_append_port_stat(struct ovs_list *replies,
7151 const struct ofputil_port_stats *ops)
7153 switch (ofpmp_version(replies)) {
7154 case OFP13_VERSION: {
7155 struct ofp13_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7156 ofputil_port_stats_to_ofp13(ops, reply);
7160 case OFP11_VERSION: {
7161 struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7162 ofputil_port_stats_to_ofp11(ops, reply);
7166 case OFP10_VERSION: {
7167 struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply);
7168 ofputil_port_stats_to_ofp10(ops, reply);
7174 ofputil_append_ofp14_port_stats(ops, replies);
7183 ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops,
7184 const struct ofp10_port_stats *ps10)
7186 memset(ops, 0, sizeof *ops);
7188 ops->port_no = u16_to_ofp(ntohs(ps10->port_no));
7189 ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets));
7190 ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets));
7191 ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes));
7192 ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes));
7193 ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped));
7194 ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped));
7195 ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors));
7196 ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors));
7197 ops->stats.rx_frame_errors =
7198 ntohll(get_32aligned_be64(&ps10->rx_frame_err));
7199 ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err));
7200 ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err));
7201 ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions));
7202 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
7208 ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops,
7209 const struct ofp11_port_stats *ps11)
7213 memset(ops, 0, sizeof *ops);
7214 error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no);
7219 ops->stats.rx_packets = ntohll(ps11->rx_packets);
7220 ops->stats.tx_packets = ntohll(ps11->tx_packets);
7221 ops->stats.rx_bytes = ntohll(ps11->rx_bytes);
7222 ops->stats.tx_bytes = ntohll(ps11->tx_bytes);
7223 ops->stats.rx_dropped = ntohll(ps11->rx_dropped);
7224 ops->stats.tx_dropped = ntohll(ps11->tx_dropped);
7225 ops->stats.rx_errors = ntohll(ps11->rx_errors);
7226 ops->stats.tx_errors = ntohll(ps11->tx_errors);
7227 ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err);
7228 ops->stats.rx_over_errors = ntohll(ps11->rx_over_err);
7229 ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err);
7230 ops->stats.collisions = ntohll(ps11->collisions);
7231 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
7237 ofputil_port_stats_from_ofp13(struct ofputil_port_stats *ops,
7238 const struct ofp13_port_stats *ps13)
7240 enum ofperr error = ofputil_port_stats_from_ofp11(ops, &ps13->ps);
7242 ops->duration_sec = ntohl(ps13->duration_sec);
7243 ops->duration_nsec = ntohl(ps13->duration_nsec);
7249 parse_ofp14_port_stats_ethernet_property(const struct ofpbuf *payload,
7250 struct ofputil_port_stats *ops)
7252 const struct ofp14_port_stats_prop_ethernet *eth = payload->data;
7254 if (payload->size != sizeof *eth) {
7255 return OFPERR_OFPBPC_BAD_LEN;
7258 ops->stats.rx_frame_errors = ntohll(eth->rx_frame_err);
7259 ops->stats.rx_over_errors = ntohll(eth->rx_over_err);
7260 ops->stats.rx_crc_errors = ntohll(eth->rx_crc_err);
7261 ops->stats.collisions = ntohll(eth->collisions);
7267 ofputil_pull_ofp14_port_stats(struct ofputil_port_stats *ops,
7270 const struct ofp14_port_stats *ps14 = ofpbuf_try_pull(msg, sizeof *ps14);
7272 return OFPERR_OFPBRC_BAD_LEN;
7275 size_t len = ntohs(ps14->length);
7276 if (len < sizeof *ps14 || len - sizeof *ps14 > msg->size) {
7277 return OFPERR_OFPBRC_BAD_LEN;
7279 len -= sizeof *ps14;
7281 enum ofperr error = ofputil_port_from_ofp11(ps14->port_no, &ops->port_no);
7286 ops->duration_sec = ntohl(ps14->duration_sec);
7287 ops->duration_nsec = ntohl(ps14->duration_nsec);
7288 ops->stats.rx_packets = ntohll(ps14->rx_packets);
7289 ops->stats.tx_packets = ntohll(ps14->tx_packets);
7290 ops->stats.rx_bytes = ntohll(ps14->rx_bytes);
7291 ops->stats.tx_bytes = ntohll(ps14->tx_bytes);
7292 ops->stats.rx_dropped = ntohll(ps14->rx_dropped);
7293 ops->stats.tx_dropped = ntohll(ps14->tx_dropped);
7294 ops->stats.rx_errors = ntohll(ps14->rx_errors);
7295 ops->stats.tx_errors = ntohll(ps14->tx_errors);
7296 ops->stats.rx_frame_errors = UINT64_MAX;
7297 ops->stats.rx_over_errors = UINT64_MAX;
7298 ops->stats.rx_crc_errors = UINT64_MAX;
7299 ops->stats.collisions = UINT64_MAX;
7301 struct ofpbuf properties = ofpbuf_const_initializer(ofpbuf_pull(msg, len),
7303 while (properties.size > 0) {
7304 struct ofpbuf payload;
7308 error = ofpprop_pull(&properties, &payload, &type);
7314 case OFPPSPT14_ETHERNET:
7315 error = parse_ofp14_port_stats_ethernet_property(&payload, ops);
7319 error = OFPPROP_UNKNOWN(true, "port stats", type);
7331 /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
7334 ofputil_count_port_stats(const struct ofp_header *oh)
7336 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
7337 ofpraw_pull_assert(&b);
7339 for (size_t n = 0; ; n++) {
7340 struct ofputil_port_stats ps;
7341 if (ofputil_decode_port_stats(&ps, &b)) {
7347 /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
7348 * ofputil_port_stats in 'ps'.
7350 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
7351 * message. Calling this function multiple times for a single 'msg' iterates
7352 * through the replies. The caller must initially leave 'msg''s layer pointers
7353 * null and not modify them between calls.
7355 * Returns 0 if successful, EOF if no replies were left in this 'msg',
7356 * otherwise a positive errno value. */
7358 ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg)
7363 error = (msg->header ? ofpraw_decode(&raw, msg->header)
7364 : ofpraw_pull(&raw, msg));
7371 } else if (raw == OFPRAW_OFPST14_PORT_REPLY) {
7372 return ofputil_pull_ofp14_port_stats(ps, msg);
7373 } else if (raw == OFPRAW_OFPST13_PORT_REPLY) {
7374 const struct ofp13_port_stats *ps13;
7376 ps13 = ofpbuf_try_pull(msg, sizeof *ps13);
7380 return ofputil_port_stats_from_ofp13(ps, ps13);
7381 } else if (raw == OFPRAW_OFPST11_PORT_REPLY) {
7382 const struct ofp11_port_stats *ps11;
7384 ps11 = ofpbuf_try_pull(msg, sizeof *ps11);
7388 return ofputil_port_stats_from_ofp11(ps, ps11);
7389 } else if (raw == OFPRAW_OFPST10_PORT_REPLY) {
7390 const struct ofp10_port_stats *ps10;
7392 ps10 = ofpbuf_try_pull(msg, sizeof *ps10);
7396 return ofputil_port_stats_from_ofp10(ps, ps10);
7402 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %"PRIu32" leftover "
7403 "bytes at end", msg->size);
7404 return OFPERR_OFPBRC_BAD_LEN;
7407 /* Parse a port status request message into a 16 bit OpenFlow 1.0
7408 * port number and stores the latter in '*ofp10_port'.
7409 * Returns 0 if successful, otherwise an OFPERR_* number. */
7411 ofputil_decode_port_stats_request(const struct ofp_header *request,
7412 ofp_port_t *ofp10_port)
7414 switch ((enum ofp_version)request->version) {
7419 case OFP11_VERSION: {
7420 const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request);
7421 return ofputil_port_from_ofp11(psr11->port_no, ofp10_port);
7424 case OFP10_VERSION: {
7425 const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request);
7426 *ofp10_port = u16_to_ofp(ntohs(psr10->port_no));
7435 /* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */
7437 ofputil_bucket_list_destroy(struct ovs_list *buckets)
7439 struct ofputil_bucket *bucket;
7441 LIST_FOR_EACH_POP (bucket, list_node, buckets) {
7442 free(bucket->ofpacts);
7447 /* Clones 'bucket' and its ofpacts data */
7448 static struct ofputil_bucket *
7449 ofputil_bucket_clone_data(const struct ofputil_bucket *bucket)
7451 struct ofputil_bucket *new;
7453 new = xmemdup(bucket, sizeof *bucket);
7454 new->ofpacts = xmemdup(bucket->ofpacts, bucket->ofpacts_len);
7459 /* Clones each of the buckets in the list 'src' appending them
7460 * in turn to 'dest' which should be an initialised list.
7461 * An exception is that if the pointer value of a bucket in 'src'
7462 * matches 'skip' then it is not cloned or appended to 'dest'.
7463 * This allows all of 'src' or 'all of 'src' except 'skip' to
7464 * be cloned and appended to 'dest'. */
7466 ofputil_bucket_clone_list(struct ovs_list *dest, const struct ovs_list *src,
7467 const struct ofputil_bucket *skip)
7469 struct ofputil_bucket *bucket;
7471 LIST_FOR_EACH (bucket, list_node, src) {
7472 struct ofputil_bucket *new_bucket;
7474 if (bucket == skip) {
7478 new_bucket = ofputil_bucket_clone_data(bucket);
7479 list_push_back(dest, &new_bucket->list_node);
7483 /* Find a bucket in the list 'buckets' whose bucket id is 'bucket_id'
7484 * Returns the first bucket found or NULL if no buckets are found. */
7485 struct ofputil_bucket *
7486 ofputil_bucket_find(const struct ovs_list *buckets, uint32_t bucket_id)
7488 struct ofputil_bucket *bucket;
7490 if (bucket_id > OFPG15_BUCKET_MAX) {
7494 LIST_FOR_EACH (bucket, list_node, buckets) {
7495 if (bucket->bucket_id == bucket_id) {
7503 /* Returns true if more than one bucket in the list 'buckets'
7504 * have the same bucket id. Returns false otherwise. */
7506 ofputil_bucket_check_duplicate_id(const struct ovs_list *buckets)
7508 struct ofputil_bucket *i, *j;
7510 LIST_FOR_EACH (i, list_node, buckets) {
7511 LIST_FOR_EACH_REVERSE (j, list_node, buckets) {
7515 if (i->bucket_id == j->bucket_id) {
7524 /* Returns the bucket at the front of the list 'buckets'.
7525 * Undefined if 'buckets is empty. */
7526 struct ofputil_bucket *
7527 ofputil_bucket_list_front(const struct ovs_list *buckets)
7529 static struct ofputil_bucket *bucket;
7531 ASSIGN_CONTAINER(bucket, list_front(buckets), list_node);
7536 /* Returns the bucket at the back of the list 'buckets'.
7537 * Undefined if 'buckets is empty. */
7538 struct ofputil_bucket *
7539 ofputil_bucket_list_back(const struct ovs_list *buckets)
7541 static struct ofputil_bucket *bucket;
7543 ASSIGN_CONTAINER(bucket, list_back(buckets), list_node);
7548 /* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version',
7549 * that requests stats for group 'group_id'. (Use OFPG_ALL to request stats
7552 * Group statistics include packet and byte counts for each group. */
7554 ofputil_encode_group_stats_request(enum ofp_version ofp_version,
7557 struct ofpbuf *request;
7559 switch (ofp_version) {
7561 ovs_fatal(0, "dump-group-stats needs OpenFlow 1.1 or later "
7562 "(\'-O OpenFlow11\')");
7567 case OFP15_VERSION: {
7568 struct ofp11_group_stats_request *req;
7569 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_REQUEST, ofp_version, 0);
7570 req = ofpbuf_put_zeros(request, sizeof *req);
7571 req->group_id = htonl(group_id);
7582 ofputil_uninit_group_desc(struct ofputil_group_desc *gd)
7584 ofputil_bucket_list_destroy(&gd->buckets);
7585 free(&gd->props.fields);
7588 /* Decodes the OpenFlow group description request in 'oh', returning the group
7589 * whose description is requested, or OFPG_ALL if stats for all groups was
7592 ofputil_decode_group_desc_request(const struct ofp_header *oh)
7594 struct ofpbuf request = ofpbuf_const_initializer(oh, ntohs(oh->length));
7595 enum ofpraw raw = ofpraw_pull_assert(&request);
7596 if (raw == OFPRAW_OFPST11_GROUP_DESC_REQUEST) {
7598 } else if (raw == OFPRAW_OFPST15_GROUP_DESC_REQUEST) {
7599 ovs_be32 *group_id = ofpbuf_pull(&request, sizeof *group_id);
7600 return ntohl(*group_id);
7606 /* Returns an OpenFlow group description request for OpenFlow version
7607 * 'ofp_version', that requests stats for group 'group_id'. Use OFPG_ALL to
7608 * request stats for all groups (OpenFlow 1.4 and earlier always request all
7611 * Group descriptions include the bucket and action configuration for each
7614 ofputil_encode_group_desc_request(enum ofp_version ofp_version,
7617 struct ofpbuf *request;
7619 switch (ofp_version) {
7621 ovs_fatal(0, "dump-groups needs OpenFlow 1.1 or later "
7622 "(\'-O OpenFlow11\')");
7627 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_DESC_REQUEST,
7630 case OFP15_VERSION:{
7631 struct ofp15_group_desc_request *req;
7632 request = ofpraw_alloc(OFPRAW_OFPST15_GROUP_DESC_REQUEST,
7634 req = ofpbuf_put_zeros(request, sizeof *req);
7635 req->group_id = htonl(group_id);
7646 ofputil_group_bucket_counters_to_ofp11(const struct ofputil_group_stats *gs,
7647 struct ofp11_bucket_counter bucket_cnts[])
7651 for (i = 0; i < gs->n_buckets; i++) {
7652 bucket_cnts[i].packet_count = htonll(gs->bucket_stats[i].packet_count);
7653 bucket_cnts[i].byte_count = htonll(gs->bucket_stats[i].byte_count);
7658 ofputil_group_stats_to_ofp11(const struct ofputil_group_stats *gs,
7659 struct ofp11_group_stats *gs11, size_t length,
7660 struct ofp11_bucket_counter bucket_cnts[])
7662 memset(gs11, 0, sizeof *gs11);
7663 gs11->length = htons(length);
7664 gs11->group_id = htonl(gs->group_id);
7665 gs11->ref_count = htonl(gs->ref_count);
7666 gs11->packet_count = htonll(gs->packet_count);
7667 gs11->byte_count = htonll(gs->byte_count);
7668 ofputil_group_bucket_counters_to_ofp11(gs, bucket_cnts);
7672 ofputil_group_stats_to_ofp13(const struct ofputil_group_stats *gs,
7673 struct ofp13_group_stats *gs13, size_t length,
7674 struct ofp11_bucket_counter bucket_cnts[])
7676 ofputil_group_stats_to_ofp11(gs, &gs13->gs, length, bucket_cnts);
7677 gs13->duration_sec = htonl(gs->duration_sec);
7678 gs13->duration_nsec = htonl(gs->duration_nsec);
7682 /* Encodes 'gs' properly for the format of the list of group statistics
7683 * replies already begun in 'replies' and appends it to the list. 'replies'
7684 * must have originally been initialized with ofpmp_init(). */
7686 ofputil_append_group_stats(struct ovs_list *replies,
7687 const struct ofputil_group_stats *gs)
7689 size_t bucket_counter_size;
7690 struct ofp11_bucket_counter *bucket_counters;
7693 bucket_counter_size = gs->n_buckets * sizeof(struct ofp11_bucket_counter);
7695 switch (ofpmp_version(replies)) {
7697 case OFP12_VERSION:{
7698 struct ofp11_group_stats *gs11;
7700 length = sizeof *gs11 + bucket_counter_size;
7701 gs11 = ofpmp_append(replies, length);
7702 bucket_counters = (struct ofp11_bucket_counter *)(gs11 + 1);
7703 ofputil_group_stats_to_ofp11(gs, gs11, length, bucket_counters);
7709 case OFP15_VERSION: {
7710 struct ofp13_group_stats *gs13;
7712 length = sizeof *gs13 + bucket_counter_size;
7713 gs13 = ofpmp_append(replies, length);
7714 bucket_counters = (struct ofp11_bucket_counter *)(gs13 + 1);
7715 ofputil_group_stats_to_ofp13(gs, gs13, length, bucket_counters);
7724 /* Returns an OpenFlow group features request for OpenFlow version
7727 ofputil_encode_group_features_request(enum ofp_version ofp_version)
7729 struct ofpbuf *request = NULL;
7731 switch (ofp_version) {
7734 ovs_fatal(0, "dump-group-features needs OpenFlow 1.2 or later "
7735 "(\'-O OpenFlow12\')");
7740 request = ofpraw_alloc(OFPRAW_OFPST12_GROUP_FEATURES_REQUEST,
7750 /* Returns a OpenFlow message that encodes 'features' properly as a reply to
7751 * group features request 'request'. */
7753 ofputil_encode_group_features_reply(
7754 const struct ofputil_group_features *features,
7755 const struct ofp_header *request)
7757 struct ofp12_group_features_stats *ogf;
7758 struct ofpbuf *reply;
7761 reply = ofpraw_alloc_xid(OFPRAW_OFPST12_GROUP_FEATURES_REPLY,
7762 request->version, request->xid, 0);
7763 ogf = ofpbuf_put_zeros(reply, sizeof *ogf);
7764 ogf->types = htonl(features->types);
7765 ogf->capabilities = htonl(features->capabilities);
7766 for (i = 0; i < OFPGT12_N_TYPES; i++) {
7767 ogf->max_groups[i] = htonl(features->max_groups[i]);
7768 ogf->actions[i] = ofpact_bitmap_to_openflow(features->ofpacts[i],
7775 /* Decodes group features reply 'oh' into 'features'. */
7777 ofputil_decode_group_features_reply(const struct ofp_header *oh,
7778 struct ofputil_group_features *features)
7780 const struct ofp12_group_features_stats *ogf = ofpmsg_body(oh);
7783 features->types = ntohl(ogf->types);
7784 features->capabilities = ntohl(ogf->capabilities);
7785 for (i = 0; i < OFPGT12_N_TYPES; i++) {
7786 features->max_groups[i] = ntohl(ogf->max_groups[i]);
7787 features->ofpacts[i] = ofpact_bitmap_from_openflow(
7788 ogf->actions[i], oh->version);
7792 /* Parse a group status request message into a 32 bit OpenFlow 1.1
7793 * group ID and stores the latter in '*group_id'.
7794 * Returns 0 if successful, otherwise an OFPERR_* number. */
7796 ofputil_decode_group_stats_request(const struct ofp_header *request,
7799 const struct ofp11_group_stats_request *gsr11 = ofpmsg_body(request);
7800 *group_id = ntohl(gsr11->group_id);
7804 /* Converts a group stats reply in 'msg' into an abstract ofputil_group_stats
7805 * in 'gs'. Assigns freshly allocated memory to gs->bucket_stats for the
7806 * caller to eventually free.
7808 * Multiple group stats replies can be packed into a single OpenFlow message.
7809 * Calling this function multiple times for a single 'msg' iterates through the
7810 * replies. The caller must initially leave 'msg''s layer pointers null and
7811 * not modify them between calls.
7813 * Returns 0 if successful, EOF if no replies were left in this 'msg',
7814 * otherwise a positive errno value. */
7816 ofputil_decode_group_stats_reply(struct ofpbuf *msg,
7817 struct ofputil_group_stats *gs)
7819 struct ofp11_bucket_counter *obc;
7820 struct ofp11_group_stats *ogs11;
7827 gs->bucket_stats = NULL;
7828 error = (msg->header ? ofpraw_decode(&raw, msg->header)
7829 : ofpraw_pull(&raw, msg));
7838 if (raw == OFPRAW_OFPST11_GROUP_REPLY) {
7839 base_len = sizeof *ogs11;
7840 ogs11 = ofpbuf_try_pull(msg, sizeof *ogs11);
7841 gs->duration_sec = gs->duration_nsec = UINT32_MAX;
7842 } else if (raw == OFPRAW_OFPST13_GROUP_REPLY) {
7843 struct ofp13_group_stats *ogs13;
7845 base_len = sizeof *ogs13;
7846 ogs13 = ofpbuf_try_pull(msg, sizeof *ogs13);
7849 gs->duration_sec = ntohl(ogs13->duration_sec);
7850 gs->duration_nsec = ntohl(ogs13->duration_nsec);
7859 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
7860 ofpraw_get_name(raw), msg->size);
7861 return OFPERR_OFPBRC_BAD_LEN;
7863 length = ntohs(ogs11->length);
7864 if (length < sizeof base_len) {
7865 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply claims invalid length %"PRIuSIZE,
7866 ofpraw_get_name(raw), length);
7867 return OFPERR_OFPBRC_BAD_LEN;
7870 gs->group_id = ntohl(ogs11->group_id);
7871 gs->ref_count = ntohl(ogs11->ref_count);
7872 gs->packet_count = ntohll(ogs11->packet_count);
7873 gs->byte_count = ntohll(ogs11->byte_count);
7875 gs->n_buckets = (length - base_len) / sizeof *obc;
7876 obc = ofpbuf_try_pull(msg, gs->n_buckets * sizeof *obc);
7878 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
7879 ofpraw_get_name(raw), msg->size);
7880 return OFPERR_OFPBRC_BAD_LEN;
7883 gs->bucket_stats = xmalloc(gs->n_buckets * sizeof *gs->bucket_stats);
7884 for (i = 0; i < gs->n_buckets; i++) {
7885 gs->bucket_stats[i].packet_count = ntohll(obc[i].packet_count);
7886 gs->bucket_stats[i].byte_count = ntohll(obc[i].byte_count);
7893 ofputil_put_ofp11_bucket(const struct ofputil_bucket *bucket,
7894 struct ofpbuf *openflow, enum ofp_version ofp_version)
7896 struct ofp11_bucket *ob;
7899 start = openflow->size;
7900 ofpbuf_put_zeros(openflow, sizeof *ob);
7901 ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
7902 openflow, ofp_version);
7903 ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
7904 ob->len = htons(openflow->size - start);
7905 ob->weight = htons(bucket->weight);
7906 ob->watch_port = ofputil_port_to_ofp11(bucket->watch_port);
7907 ob->watch_group = htonl(bucket->watch_group);
7911 ofputil_put_ofp15_bucket(const struct ofputil_bucket *bucket,
7912 uint32_t bucket_id, enum ofp11_group_type group_type,
7913 struct ofpbuf *openflow, enum ofp_version ofp_version)
7915 struct ofp15_bucket *ob;
7916 size_t start, actions_start, actions_len;
7918 start = openflow->size;
7919 ofpbuf_put_zeros(openflow, sizeof *ob);
7921 actions_start = openflow->size;
7922 ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
7923 openflow, ofp_version);
7924 actions_len = openflow->size - actions_start;
7926 if (group_type == OFPGT11_SELECT) {
7927 ofpprop_put_u16(openflow, OFPGBPT15_WEIGHT, bucket->weight);
7929 if (bucket->watch_port != OFPP_ANY) {
7930 ofpprop_put_be32(openflow, OFPGBPT15_WATCH_PORT,
7931 ofputil_port_to_ofp11(bucket->watch_port));
7933 if (bucket->watch_group != OFPG_ANY) {
7934 ofpprop_put_u32(openflow, OFPGBPT15_WATCH_GROUP, bucket->watch_group);
7937 ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
7938 ob->len = htons(openflow->size - start);
7939 ob->action_array_len = htons(actions_len);
7940 ob->bucket_id = htonl(bucket_id);
7944 ofputil_put_group_prop_ntr_selection_method(enum ofp_version ofp_version,
7945 const struct ofputil_group_props *gp,
7946 struct ofpbuf *openflow)
7948 struct ntr_group_prop_selection_method *prop;
7951 start = openflow->size;
7952 ofpbuf_put_zeros(openflow, sizeof *prop);
7953 oxm_put_field_array(openflow, &gp->fields, ofp_version);
7954 prop = ofpbuf_at_assert(openflow, start, sizeof *prop);
7955 prop->type = htons(OFPGPT15_EXPERIMENTER);
7956 prop->experimenter = htonl(NTR_VENDOR_ID);
7957 prop->exp_type = htonl(NTRT_SELECTION_METHOD);
7958 strcpy(prop->selection_method, gp->selection_method);
7959 prop->selection_method_param = htonll(gp->selection_method_param);
7960 ofpprop_end(openflow, start);
7964 ofputil_append_ofp11_group_desc_reply(const struct ofputil_group_desc *gds,
7965 const struct ovs_list *buckets,
7966 struct ovs_list *replies,
7967 enum ofp_version version)
7969 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
7970 struct ofp11_group_desc_stats *ogds;
7971 struct ofputil_bucket *bucket;
7974 start_ogds = reply->size;
7975 ofpbuf_put_zeros(reply, sizeof *ogds);
7976 LIST_FOR_EACH (bucket, list_node, buckets) {
7977 ofputil_put_ofp11_bucket(bucket, reply, version);
7979 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
7980 ogds->length = htons(reply->size - start_ogds);
7981 ogds->type = gds->type;
7982 ogds->group_id = htonl(gds->group_id);
7984 ofpmp_postappend(replies, start_ogds);
7988 ofputil_append_ofp15_group_desc_reply(const struct ofputil_group_desc *gds,
7989 const struct ovs_list *buckets,
7990 struct ovs_list *replies,
7991 enum ofp_version version)
7993 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
7994 struct ofp15_group_desc_stats *ogds;
7995 struct ofputil_bucket *bucket;
7996 size_t start_ogds, start_buckets;
7998 start_ogds = reply->size;
7999 ofpbuf_put_zeros(reply, sizeof *ogds);
8000 start_buckets = reply->size;
8001 LIST_FOR_EACH (bucket, list_node, buckets) {
8002 ofputil_put_ofp15_bucket(bucket, bucket->bucket_id,
8003 gds->type, reply, version);
8005 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
8006 ogds->type = gds->type;
8007 ogds->group_id = htonl(gds->group_id);
8008 ogds->bucket_list_len = htons(reply->size - start_buckets);
8010 /* Add group properties */
8011 if (gds->props.selection_method[0]) {
8012 ofputil_put_group_prop_ntr_selection_method(version, &gds->props,
8015 ogds->length = htons(reply->size - start_ogds);
8017 ofpmp_postappend(replies, start_ogds);
8020 /* Appends a group stats reply that contains the data in 'gds' to those already
8021 * present in the list of ofpbufs in 'replies'. 'replies' should have been
8022 * initialized with ofpmp_init(). */
8024 ofputil_append_group_desc_reply(const struct ofputil_group_desc *gds,
8025 const struct ovs_list *buckets,
8026 struct ovs_list *replies)
8028 enum ofp_version version = ofpmp_version(replies);
8036 ofputil_append_ofp11_group_desc_reply(gds, buckets, replies, version);
8040 ofputil_append_ofp15_group_desc_reply(gds, buckets, replies, version);
8050 ofputil_pull_ofp11_buckets(struct ofpbuf *msg, size_t buckets_length,
8051 enum ofp_version version, struct ovs_list *buckets)
8053 struct ofp11_bucket *ob;
8054 uint32_t bucket_id = 0;
8057 while (buckets_length > 0) {
8058 struct ofputil_bucket *bucket;
8059 struct ofpbuf ofpacts;
8063 ob = (buckets_length >= sizeof *ob
8064 ? ofpbuf_try_pull(msg, sizeof *ob)
8067 VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE" leftover bytes",
8069 return OFPERR_OFPGMFC_BAD_BUCKET;
8072 ob_len = ntohs(ob->len);
8073 if (ob_len < sizeof *ob) {
8074 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8075 "%"PRIuSIZE" is not valid", ob_len);
8076 return OFPERR_OFPGMFC_BAD_BUCKET;
8077 } else if (ob_len > buckets_length) {
8078 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8079 "%"PRIuSIZE" exceeds remaining buckets data size %"PRIuSIZE,
8080 ob_len, buckets_length);
8081 return OFPERR_OFPGMFC_BAD_BUCKET;
8083 buckets_length -= ob_len;
8085 ofpbuf_init(&ofpacts, 0);
8086 error = ofpacts_pull_openflow_actions(msg, ob_len - sizeof *ob,
8089 ofpbuf_uninit(&ofpacts);
8090 ofputil_bucket_list_destroy(buckets);
8094 bucket = xzalloc(sizeof *bucket);
8095 bucket->weight = ntohs(ob->weight);
8096 error = ofputil_port_from_ofp11(ob->watch_port, &bucket->watch_port);
8098 ofpbuf_uninit(&ofpacts);
8099 ofputil_bucket_list_destroy(buckets);
8100 return OFPERR_OFPGMFC_BAD_WATCH;
8102 bucket->watch_group = ntohl(ob->watch_group);
8103 bucket->bucket_id = bucket_id++;
8105 bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
8106 bucket->ofpacts_len = ofpacts.size;
8107 list_push_back(buckets, &bucket->list_node);
8114 ofputil_pull_ofp15_buckets(struct ofpbuf *msg, size_t buckets_length,
8115 enum ofp_version version, uint8_t group_type,
8116 struct ovs_list *buckets)
8118 struct ofp15_bucket *ob;
8121 while (buckets_length > 0) {
8122 struct ofputil_bucket *bucket = NULL;
8123 struct ofpbuf ofpacts;
8124 enum ofperr err = OFPERR_OFPGMFC_BAD_BUCKET;
8125 size_t ob_len, actions_len, properties_len;
8126 ovs_be32 watch_port = ofputil_port_to_ofp11(OFPP_ANY);
8127 ovs_be32 watch_group = htonl(OFPG_ANY);
8128 ovs_be16 weight = htons(group_type == OFPGT11_SELECT ? 1 : 0);
8130 ofpbuf_init(&ofpacts, 0);
8132 ob = ofpbuf_try_pull(msg, sizeof *ob);
8134 VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE
8135 " leftover bytes", buckets_length);
8139 ob_len = ntohs(ob->len);
8140 actions_len = ntohs(ob->action_array_len);
8142 if (ob_len < sizeof *ob) {
8143 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8144 "%"PRIuSIZE" is not valid", ob_len);
8146 } else if (ob_len > buckets_length) {
8147 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
8148 "%"PRIuSIZE" exceeds remaining buckets data size %"
8149 PRIuSIZE, ob_len, buckets_length);
8151 } else if (actions_len > ob_len - sizeof *ob) {
8152 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket actions "
8153 "length %"PRIuSIZE" exceeds remaining bucket "
8154 "data size %"PRIuSIZE, actions_len,
8155 ob_len - sizeof *ob);
8158 buckets_length -= ob_len;
8160 err = ofpacts_pull_openflow_actions(msg, actions_len, version,
8166 properties_len = ob_len - sizeof *ob - actions_len;
8167 struct ofpbuf properties = ofpbuf_const_initializer(
8168 ofpbuf_pull(msg, properties_len), properties_len);
8169 while (properties.size > 0) {
8170 struct ofpbuf payload;
8173 err = ofpprop_pull(&properties, &payload, &type);
8179 case OFPGBPT15_WEIGHT:
8180 err = ofpprop_parse_be16(&payload, &weight);
8183 case OFPGBPT15_WATCH_PORT:
8184 err = ofpprop_parse_be32(&payload, &watch_port);
8187 case OFPGBPT15_WATCH_GROUP:
8188 err = ofpprop_parse_be32(&payload, &watch_group);
8192 err = OFPPROP_UNKNOWN(false, "group bucket", type);
8201 bucket = xzalloc(sizeof *bucket);
8203 bucket->weight = ntohs(weight);
8204 err = ofputil_port_from_ofp11(watch_port, &bucket->watch_port);
8206 err = OFPERR_OFPGMFC_BAD_WATCH;
8209 bucket->watch_group = ntohl(watch_group);
8210 bucket->bucket_id = ntohl(ob->bucket_id);
8211 if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
8212 VLOG_WARN_RL(&bad_ofmsg_rl, "bucket id (%u) is out of range",
8214 err = OFPERR_OFPGMFC_BAD_BUCKET;
8218 bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
8219 bucket->ofpacts_len = ofpacts.size;
8220 list_push_back(buckets, &bucket->list_node);
8226 ofpbuf_uninit(&ofpacts);
8227 ofputil_bucket_list_destroy(buckets);
8231 if (ofputil_bucket_check_duplicate_id(buckets)) {
8232 VLOG_WARN_RL(&bad_ofmsg_rl, "Duplicate bucket id");
8233 ofputil_bucket_list_destroy(buckets);
8234 return OFPERR_OFPGMFC_BAD_BUCKET;
8241 ofputil_init_group_properties(struct ofputil_group_props *gp)
8243 memset(gp, 0, sizeof *gp);
8247 parse_group_prop_ntr_selection_method(struct ofpbuf *payload,
8248 enum ofp11_group_type group_type,
8249 enum ofp15_group_mod_command group_cmd,
8250 struct ofputil_group_props *gp)
8252 struct ntr_group_prop_selection_method *prop = payload->data;
8253 size_t fields_len, method_len;
8256 switch (group_type) {
8257 case OFPGT11_SELECT:
8260 case OFPGT11_INDIRECT:
8262 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property is "
8263 "only allowed for select groups");
8264 return OFPERR_OFPBPC_BAD_VALUE;
8269 switch (group_cmd) {
8271 case OFPGC15_MODIFY:
8273 case OFPGC15_DELETE:
8274 case OFPGC15_INSERT_BUCKET:
8275 case OFPGC15_REMOVE_BUCKET:
8276 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property is "
8277 "only allowed for add and delete group modifications");
8278 return OFPERR_OFPBPC_BAD_VALUE;
8283 if (payload->size < sizeof *prop) {
8284 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method property "
8285 "length %u is not valid", payload->size);
8286 return OFPERR_OFPBPC_BAD_LEN;
8289 method_len = strnlen(prop->selection_method, NTR_MAX_SELECTION_METHOD_LEN);
8291 if (method_len == NTR_MAX_SELECTION_METHOD_LEN) {
8292 OFPPROP_LOG(&bad_ofmsg_rl, false,
8293 "ntr selection method is not null terminated");
8294 return OFPERR_OFPBPC_BAD_VALUE;
8297 if (strcmp("hash", prop->selection_method)) {
8298 OFPPROP_LOG(&bad_ofmsg_rl, false,
8299 "ntr selection method '%s' is not supported",
8300 prop->selection_method);
8301 return OFPERR_OFPBPC_BAD_VALUE;
8304 strcpy(gp->selection_method, prop->selection_method);
8305 gp->selection_method_param = ntohll(prop->selection_method_param);
8307 if (!method_len && gp->selection_method_param) {
8308 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method parameter is "
8309 "non-zero but selection method is empty");
8310 return OFPERR_OFPBPC_BAD_VALUE;
8313 ofpbuf_pull(payload, sizeof *prop);
8315 fields_len = ntohs(prop->length) - sizeof *prop;
8316 if (!method_len && fields_len) {
8317 OFPPROP_LOG(&bad_ofmsg_rl, false, "ntr selection method parameter is "
8318 "zero but fields are provided");
8319 return OFPERR_OFPBPC_BAD_VALUE;
8322 error = oxm_pull_field_array(payload->data, fields_len,
8325 OFPPROP_LOG(&bad_ofmsg_rl, false,
8326 "ntr selection method fields are invalid");
8334 parse_ofp15_group_properties(struct ofpbuf *msg,
8335 enum ofp11_group_type group_type,
8336 enum ofp15_group_mod_command group_cmd,
8337 struct ofputil_group_props *gp,
8338 size_t properties_len)
8340 struct ofpbuf properties = ofpbuf_const_initializer(
8341 ofpbuf_pull(msg, properties_len), properties_len);
8342 while (properties.size > 0) {
8343 struct ofpbuf payload;
8347 error = ofpprop_pull(&properties, &payload, &type);
8353 case OFPPROP_EXP(NTR_VENDOR_ID, NTRT_SELECTION_METHOD):
8354 case OFPPROP_EXP(NTR_COMPAT_VENDOR_ID, NTRT_SELECTION_METHOD):
8355 error = parse_group_prop_ntr_selection_method(&payload, group_type,
8360 error = OFPPROP_UNKNOWN(false, "group", type);
8373 ofputil_decode_ofp11_group_desc_reply(struct ofputil_group_desc *gd,
8375 enum ofp_version version)
8377 struct ofp11_group_desc_stats *ogds;
8381 ofpraw_pull_assert(msg);
8388 ogds = ofpbuf_try_pull(msg, sizeof *ogds);
8390 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
8391 "leftover bytes at end", msg->size);
8392 return OFPERR_OFPBRC_BAD_LEN;
8394 gd->type = ogds->type;
8395 gd->group_id = ntohl(ogds->group_id);
8397 length = ntohs(ogds->length);
8398 if (length < sizeof *ogds || length - sizeof *ogds > msg->size) {
8399 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
8400 "length %"PRIuSIZE, length);
8401 return OFPERR_OFPBRC_BAD_LEN;
8404 return ofputil_pull_ofp11_buckets(msg, length - sizeof *ogds, version,
8409 ofputil_decode_ofp15_group_desc_reply(struct ofputil_group_desc *gd,
8411 enum ofp_version version)
8413 struct ofp15_group_desc_stats *ogds;
8414 uint16_t length, bucket_list_len;
8418 ofpraw_pull_assert(msg);
8425 ogds = ofpbuf_try_pull(msg, sizeof *ogds);
8427 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
8428 "leftover bytes at end", msg->size);
8429 return OFPERR_OFPBRC_BAD_LEN;
8431 gd->type = ogds->type;
8432 gd->group_id = ntohl(ogds->group_id);
8434 length = ntohs(ogds->length);
8435 if (length < sizeof *ogds || length - sizeof *ogds > msg->size) {
8436 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
8437 "length %u", length);
8438 return OFPERR_OFPBRC_BAD_LEN;
8441 bucket_list_len = ntohs(ogds->bucket_list_len);
8442 if (length < bucket_list_len + sizeof *ogds) {
8443 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
8444 "bucket list length %u", bucket_list_len);
8445 return OFPERR_OFPBRC_BAD_LEN;
8447 error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, version, gd->type,
8453 /* By definition group desc messages don't have a group mod command.
8454 * However, parse_group_prop_ntr_selection_method() checks to make sure
8455 * that the command is OFPGC15_ADD or OFPGC15_DELETE to guard
8456 * against group mod messages with other commands supplying
8457 * a NTR selection method group experimenter property.
8458 * Such properties are valid for group desc replies so
8459 * claim that the group mod command is OFPGC15_ADD to
8460 * satisfy the check in parse_group_prop_ntr_selection_method() */
8461 return parse_ofp15_group_properties(msg, gd->type, OFPGC15_ADD, &gd->props,
8462 length - sizeof *ogds - bucket_list_len);
8465 /* Converts a group description reply in 'msg' into an abstract
8466 * ofputil_group_desc in 'gd'.
8468 * Multiple group description replies can be packed into a single OpenFlow
8469 * message. Calling this function multiple times for a single 'msg' iterates
8470 * through the replies. The caller must initially leave 'msg''s layer pointers
8471 * null and not modify them between calls.
8473 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8474 * otherwise a positive errno value. */
8476 ofputil_decode_group_desc_reply(struct ofputil_group_desc *gd,
8477 struct ofpbuf *msg, enum ofp_version version)
8479 ofputil_init_group_properties(&gd->props);
8487 return ofputil_decode_ofp11_group_desc_reply(gd, msg, version);
8490 return ofputil_decode_ofp15_group_desc_reply(gd, msg, version);
8499 ofputil_uninit_group_mod(struct ofputil_group_mod *gm)
8501 ofputil_bucket_list_destroy(&gm->buckets);
8504 static struct ofpbuf *
8505 ofputil_encode_ofp11_group_mod(enum ofp_version ofp_version,
8506 const struct ofputil_group_mod *gm)
8509 struct ofp11_group_mod *ogm;
8511 struct ofputil_bucket *bucket;
8513 b = ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD, ofp_version, 0);
8514 start_ogm = b->size;
8515 ofpbuf_put_zeros(b, sizeof *ogm);
8517 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
8518 ofputil_put_ofp11_bucket(bucket, b, ofp_version);
8520 ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
8521 ogm->command = htons(gm->command);
8522 ogm->type = gm->type;
8523 ogm->group_id = htonl(gm->group_id);
8528 static struct ofpbuf *
8529 ofputil_encode_ofp15_group_mod(enum ofp_version ofp_version,
8530 const struct ofputil_group_mod *gm)
8533 struct ofp15_group_mod *ogm;
8535 struct ofputil_bucket *bucket;
8536 struct id_pool *bucket_ids = NULL;
8538 b = ofpraw_alloc(OFPRAW_OFPT15_GROUP_MOD, ofp_version, 0);
8539 start_ogm = b->size;
8540 ofpbuf_put_zeros(b, sizeof *ogm);
8542 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
8545 /* Generate a bucket id if none was supplied */
8546 if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
8548 const struct ofputil_bucket *bkt;
8550 bucket_ids = id_pool_create(0, OFPG15_BUCKET_MAX + 1);
8552 /* Mark all bucket_ids that are present in gm
8553 * as used in the pool. */
8554 LIST_FOR_EACH_REVERSE (bkt, list_node, &gm->buckets) {
8555 if (bkt == bucket) {
8558 if (bkt->bucket_id <= OFPG15_BUCKET_MAX) {
8559 id_pool_add(bucket_ids, bkt->bucket_id);
8564 if (!id_pool_alloc_id(bucket_ids, &bucket_id)) {
8568 bucket_id = bucket->bucket_id;
8571 ofputil_put_ofp15_bucket(bucket, bucket_id, gm->type, b, ofp_version);
8573 ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
8574 ogm->command = htons(gm->command);
8575 ogm->type = gm->type;
8576 ogm->group_id = htonl(gm->group_id);
8577 ogm->command_bucket_id = htonl(gm->command_bucket_id);
8578 ogm->bucket_array_len = htons(b->size - start_ogm - sizeof *ogm);
8580 /* Add group properties */
8581 if (gm->props.selection_method[0]) {
8582 ofputil_put_group_prop_ntr_selection_method(ofp_version, &gm->props, b);
8585 id_pool_destroy(bucket_ids);
8590 bad_group_cmd(enum ofp15_group_mod_command cmd)
8592 const char *opt_version;
8593 const char *version;
8594 const char *cmd_str;
8598 case OFPGC15_MODIFY:
8599 case OFPGC15_DELETE:
8604 case OFPGC15_INSERT_BUCKET:
8605 case OFPGC15_REMOVE_BUCKET:
8616 cmd_str = "add-group";
8619 case OFPGC15_MODIFY:
8620 cmd_str = "mod-group";
8623 case OFPGC15_DELETE:
8624 cmd_str = "del-group";
8627 case OFPGC15_INSERT_BUCKET:
8628 cmd_str = "insert-bucket";
8631 case OFPGC15_REMOVE_BUCKET:
8632 cmd_str = "remove-bucket";
8639 ovs_fatal(0, "%s needs OpenFlow %s or later (\'-O OpenFlow%s\')",
8640 cmd_str, version, opt_version);
8644 /* Converts abstract group mod 'gm' into a message for OpenFlow version
8645 * 'ofp_version' and returns the message. */
8647 ofputil_encode_group_mod(enum ofp_version ofp_version,
8648 const struct ofputil_group_mod *gm)
8651 switch (ofp_version) {
8653 bad_group_cmd(gm->command);
8659 if (gm->command > OFPGC11_DELETE) {
8660 bad_group_cmd(gm->command);
8662 return ofputil_encode_ofp11_group_mod(ofp_version, gm);
8665 return ofputil_encode_ofp15_group_mod(ofp_version, gm);
8673 ofputil_pull_ofp11_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
8674 struct ofputil_group_mod *gm)
8676 const struct ofp11_group_mod *ogm;
8679 ogm = ofpbuf_pull(msg, sizeof *ogm);
8680 gm->command = ntohs(ogm->command);
8681 gm->type = ogm->type;
8682 gm->group_id = ntohl(ogm->group_id);
8683 gm->command_bucket_id = OFPG15_BUCKET_ALL;
8685 error = ofputil_pull_ofp11_buckets(msg, msg->size, ofp_version,
8688 /* OF1.3.5+ prescribes an error when an OFPGC_DELETE includes buckets. */
8690 && ofp_version >= OFP13_VERSION
8691 && gm->command == OFPGC11_DELETE
8692 && !list_is_empty(&gm->buckets)) {
8693 error = OFPERR_OFPGMFC_INVALID_GROUP;
8700 ofputil_pull_ofp15_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
8701 struct ofputil_group_mod *gm)
8703 const struct ofp15_group_mod *ogm;
8704 uint16_t bucket_list_len;
8705 enum ofperr error = OFPERR_OFPGMFC_BAD_BUCKET;
8707 ogm = ofpbuf_pull(msg, sizeof *ogm);
8708 gm->command = ntohs(ogm->command);
8709 gm->type = ogm->type;
8710 gm->group_id = ntohl(ogm->group_id);
8712 gm->command_bucket_id = ntohl(ogm->command_bucket_id);
8713 switch (gm->command) {
8714 case OFPGC15_REMOVE_BUCKET:
8715 if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
8719 case OFPGC15_INSERT_BUCKET:
8720 if (gm->command_bucket_id <= OFPG15_BUCKET_MAX ||
8721 gm->command_bucket_id == OFPG15_BUCKET_FIRST
8722 || gm->command_bucket_id == OFPG15_BUCKET_LAST) {
8728 case OFPGC11_MODIFY:
8729 case OFPGC11_DELETE:
8731 if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
8737 VLOG_WARN_RL(&bad_ofmsg_rl,
8738 "group command bucket id (%u) is out of range",
8739 gm->command_bucket_id);
8740 return OFPERR_OFPGMFC_BAD_BUCKET;
8743 bucket_list_len = ntohs(ogm->bucket_array_len);
8744 error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, ofp_version,
8745 gm->type, &gm->buckets);
8750 return parse_ofp15_group_properties(msg, gm->type, gm->command, &gm->props,
8754 /* Converts OpenFlow group mod message 'oh' into an abstract group mod in
8755 * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */
8757 ofputil_decode_group_mod(const struct ofp_header *oh,
8758 struct ofputil_group_mod *gm)
8760 ofputil_init_group_properties(&gm->props);
8762 enum ofp_version ofp_version = oh->version;
8763 struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
8764 ofpraw_pull_assert(&msg);
8767 switch (ofp_version)
8773 err = ofputil_pull_ofp11_group_mod(&msg, ofp_version, gm);
8777 err = ofputil_pull_ofp15_group_mod(&msg, ofp_version, gm);
8789 case OFPGT11_INDIRECT:
8790 if (!list_is_singleton(&gm->buckets)) {
8791 return OFPERR_OFPGMFC_INVALID_GROUP;
8795 case OFPGT11_SELECT:
8799 return OFPERR_OFPGMFC_BAD_TYPE;
8802 switch (gm->command) {
8804 case OFPGC11_MODIFY:
8805 case OFPGC11_DELETE:
8806 case OFPGC15_INSERT_BUCKET:
8808 case OFPGC15_REMOVE_BUCKET:
8809 if (!list_is_empty(&gm->buckets)) {
8810 return OFPERR_OFPGMFC_BAD_BUCKET;
8814 return OFPERR_OFPGMFC_BAD_COMMAND;
8817 struct ofputil_bucket *bucket;
8818 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
8819 if (bucket->weight && gm->type != OFPGT11_SELECT) {
8820 return OFPERR_OFPGMFC_INVALID_GROUP;
8825 case OFPGT11_INDIRECT:
8826 if (ofputil_bucket_has_liveness(bucket)) {
8827 return OFPERR_OFPGMFC_WATCH_UNSUPPORTED;
8830 case OFPGT11_SELECT:
8833 if (!ofputil_bucket_has_liveness(bucket)) {
8834 return OFPERR_OFPGMFC_INVALID_GROUP;
8838 /* Returning BAD TYPE to be consistent
8839 * though gm->type has been checked already. */
8840 return OFPERR_OFPGMFC_BAD_TYPE;
8847 /* Parse a queue status request message into 'oqsr'.
8848 * Returns 0 if successful, otherwise an OFPERR_* number. */
8850 ofputil_decode_queue_stats_request(const struct ofp_header *request,
8851 struct ofputil_queue_stats_request *oqsr)
8853 switch ((enum ofp_version)request->version) {
8858 case OFP11_VERSION: {
8859 const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request);
8860 oqsr->queue_id = ntohl(qsr11->queue_id);
8861 return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no);
8864 case OFP10_VERSION: {
8865 const struct ofp10_queue_stats_request *qsr10 = ofpmsg_body(request);
8866 oqsr->queue_id = ntohl(qsr10->queue_id);
8867 oqsr->port_no = u16_to_ofp(ntohs(qsr10->port_no));
8868 /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
8869 if (oqsr->port_no == OFPP_ALL) {
8870 oqsr->port_no = OFPP_ANY;
8880 /* Encode a queue stats request for 'oqsr', the encoded message
8881 * will be for OpenFlow version 'ofp_version'. Returns message
8882 * as a struct ofpbuf. Returns encoded message on success, NULL on error. */
8884 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
8885 const struct ofputil_queue_stats_request *oqsr)
8887 struct ofpbuf *request;
8889 switch (ofp_version) {
8894 case OFP15_VERSION: {
8895 struct ofp11_queue_stats_request *req;
8896 request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0);
8897 req = ofpbuf_put_zeros(request, sizeof *req);
8898 req->port_no = ofputil_port_to_ofp11(oqsr->port_no);
8899 req->queue_id = htonl(oqsr->queue_id);
8902 case OFP10_VERSION: {
8903 struct ofp10_queue_stats_request *req;
8904 request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0);
8905 req = ofpbuf_put_zeros(request, sizeof *req);
8906 /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
8907 req->port_no = htons(ofp_to_u16(oqsr->port_no == OFPP_ANY
8908 ? OFPP_ALL : oqsr->port_no));
8909 req->queue_id = htonl(oqsr->queue_id);
8919 /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
8922 ofputil_count_queue_stats(const struct ofp_header *oh)
8924 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
8925 ofpraw_pull_assert(&b);
8927 for (size_t n = 0; ; n++) {
8928 struct ofputil_queue_stats qs;
8929 if (ofputil_decode_queue_stats(&qs, &b)) {
8936 ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs,
8937 const struct ofp10_queue_stats *qs10)
8939 oqs->port_no = u16_to_ofp(ntohs(qs10->port_no));
8940 oqs->queue_id = ntohl(qs10->queue_id);
8941 oqs->tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes));
8942 oqs->tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets));
8943 oqs->tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors));
8944 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
8950 ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs,
8951 const struct ofp11_queue_stats *qs11)
8955 error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no);
8960 oqs->queue_id = ntohl(qs11->queue_id);
8961 oqs->tx_bytes = ntohll(qs11->tx_bytes);
8962 oqs->tx_packets = ntohll(qs11->tx_packets);
8963 oqs->tx_errors = ntohll(qs11->tx_errors);
8964 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
8970 ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats *oqs,
8971 const struct ofp13_queue_stats *qs13)
8973 enum ofperr error = ofputil_queue_stats_from_ofp11(oqs, &qs13->qs);
8975 oqs->duration_sec = ntohl(qs13->duration_sec);
8976 oqs->duration_nsec = ntohl(qs13->duration_nsec);
8983 ofputil_pull_ofp14_queue_stats(struct ofputil_queue_stats *oqs,
8986 const struct ofp14_queue_stats *qs14;
8989 qs14 = ofpbuf_try_pull(msg, sizeof *qs14);
8991 return OFPERR_OFPBRC_BAD_LEN;
8994 len = ntohs(qs14->length);
8995 if (len < sizeof *qs14 || len - sizeof *qs14 > msg->size) {
8996 return OFPERR_OFPBRC_BAD_LEN;
8998 ofpbuf_pull(msg, len - sizeof *qs14);
9000 /* No properties yet defined, so ignore them for now. */
9002 return ofputil_queue_stats_from_ofp13(oqs, &qs14->qs);
9005 /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
9006 * ofputil_queue_stats in 'qs'.
9008 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
9009 * message. Calling this function multiple times for a single 'msg' iterates
9010 * through the replies. The caller must initially leave 'msg''s layer pointers
9011 * null and not modify them between calls.
9013 * Returns 0 if successful, EOF if no replies were left in this 'msg',
9014 * otherwise a positive errno value. */
9016 ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg)
9021 error = (msg->header ? ofpraw_decode(&raw, msg->header)
9022 : ofpraw_pull(&raw, msg));
9029 } else if (raw == OFPRAW_OFPST14_QUEUE_REPLY) {
9030 return ofputil_pull_ofp14_queue_stats(qs, msg);
9031 } else if (raw == OFPRAW_OFPST13_QUEUE_REPLY) {
9032 const struct ofp13_queue_stats *qs13;
9034 qs13 = ofpbuf_try_pull(msg, sizeof *qs13);
9038 return ofputil_queue_stats_from_ofp13(qs, qs13);
9039 } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) {
9040 const struct ofp11_queue_stats *qs11;
9042 qs11 = ofpbuf_try_pull(msg, sizeof *qs11);
9046 return ofputil_queue_stats_from_ofp11(qs, qs11);
9047 } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) {
9048 const struct ofp10_queue_stats *qs10;
9050 qs10 = ofpbuf_try_pull(msg, sizeof *qs10);
9054 return ofputil_queue_stats_from_ofp10(qs, qs10);
9060 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %"PRIu32" leftover "
9061 "bytes at end", msg->size);
9062 return OFPERR_OFPBRC_BAD_LEN;
9066 ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs,
9067 struct ofp10_queue_stats *qs10)
9069 qs10->port_no = htons(ofp_to_u16(oqs->port_no));
9070 memset(qs10->pad, 0, sizeof qs10->pad);
9071 qs10->queue_id = htonl(oqs->queue_id);
9072 put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->tx_bytes));
9073 put_32aligned_be64(&qs10->tx_packets, htonll(oqs->tx_packets));
9074 put_32aligned_be64(&qs10->tx_errors, htonll(oqs->tx_errors));
9078 ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs,
9079 struct ofp11_queue_stats *qs11)
9081 qs11->port_no = ofputil_port_to_ofp11(oqs->port_no);
9082 qs11->queue_id = htonl(oqs->queue_id);
9083 qs11->tx_bytes = htonll(oqs->tx_bytes);
9084 qs11->tx_packets = htonll(oqs->tx_packets);
9085 qs11->tx_errors = htonll(oqs->tx_errors);
9089 ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats *oqs,
9090 struct ofp13_queue_stats *qs13)
9092 ofputil_queue_stats_to_ofp11(oqs, &qs13->qs);
9093 if (oqs->duration_sec != UINT32_MAX) {
9094 qs13->duration_sec = htonl(oqs->duration_sec);
9095 qs13->duration_nsec = htonl(oqs->duration_nsec);
9097 qs13->duration_sec = OVS_BE32_MAX;
9098 qs13->duration_nsec = OVS_BE32_MAX;
9103 ofputil_queue_stats_to_ofp14(const struct ofputil_queue_stats *oqs,
9104 struct ofp14_queue_stats *qs14)
9106 qs14->length = htons(sizeof *qs14);
9107 memset(qs14->pad, 0, sizeof qs14->pad);
9108 ofputil_queue_stats_to_ofp13(oqs, &qs14->qs);
9112 /* Encode a queue stat for 'oqs' and append it to 'replies'. */
9114 ofputil_append_queue_stat(struct ovs_list *replies,
9115 const struct ofputil_queue_stats *oqs)
9117 switch (ofpmp_version(replies)) {
9118 case OFP13_VERSION: {
9119 struct ofp13_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
9120 ofputil_queue_stats_to_ofp13(oqs, reply);
9125 case OFP11_VERSION: {
9126 struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
9127 ofputil_queue_stats_to_ofp11(oqs, reply);
9131 case OFP10_VERSION: {
9132 struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
9133 ofputil_queue_stats_to_ofp10(oqs, reply);
9138 case OFP15_VERSION: {
9139 struct ofp14_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
9140 ofputil_queue_stats_to_ofp14(oqs, reply);
9150 ofputil_decode_bundle_ctrl(const struct ofp_header *oh,
9151 struct ofputil_bundle_ctrl_msg *msg)
9153 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
9154 enum ofpraw raw = ofpraw_pull_assert(&b);
9155 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_CONTROL);
9157 const struct ofp14_bundle_ctrl_msg *m = b.msg;
9158 msg->bundle_id = ntohl(m->bundle_id);
9159 msg->type = ntohs(m->type);
9160 msg->flags = ntohs(m->flags);
9166 ofputil_encode_bundle_ctrl_request(enum ofp_version ofp_version,
9167 struct ofputil_bundle_ctrl_msg *bc)
9169 struct ofpbuf *request;
9170 struct ofp14_bundle_ctrl_msg *m;
9172 switch (ofp_version) {
9177 ovs_fatal(0, "bundles need OpenFlow 1.4 or later "
9178 "(\'-O OpenFlow14\')");
9181 request = ofpraw_alloc(OFPRAW_OFPT14_BUNDLE_CONTROL, ofp_version, 0);
9182 m = ofpbuf_put_zeros(request, sizeof *m);
9184 m->bundle_id = htonl(bc->bundle_id);
9185 m->type = htons(bc->type);
9186 m->flags = htons(bc->flags);
9196 ofputil_encode_bundle_ctrl_reply(const struct ofp_header *oh,
9197 struct ofputil_bundle_ctrl_msg *msg)
9200 struct ofp14_bundle_ctrl_msg *m;
9202 buf = ofpraw_alloc_reply(OFPRAW_OFPT14_BUNDLE_CONTROL, oh, 0);
9203 m = ofpbuf_put_zeros(buf, sizeof *m);
9205 m->bundle_id = htonl(msg->bundle_id);
9206 m->type = htons(msg->type);
9207 m->flags = htons(msg->flags);
9212 /* Return true for bundlable state change requests, false for other messages.
9215 ofputil_is_bundlable(enum ofptype type)
9218 /* Minimum required by OpenFlow 1.4. */
9219 case OFPTYPE_PORT_MOD:
9220 case OFPTYPE_FLOW_MOD:
9223 /* Nice to have later. */
9224 case OFPTYPE_FLOW_MOD_TABLE_ID:
9225 case OFPTYPE_GROUP_MOD:
9226 case OFPTYPE_TABLE_MOD:
9227 case OFPTYPE_METER_MOD:
9228 case OFPTYPE_PACKET_OUT:
9229 case OFPTYPE_NXT_TLV_TABLE_MOD:
9231 /* Not to be bundlable. */
9232 case OFPTYPE_ECHO_REQUEST:
9233 case OFPTYPE_FEATURES_REQUEST:
9234 case OFPTYPE_GET_CONFIG_REQUEST:
9235 case OFPTYPE_SET_CONFIG:
9236 case OFPTYPE_BARRIER_REQUEST:
9237 case OFPTYPE_ROLE_REQUEST:
9238 case OFPTYPE_ECHO_REPLY:
9239 case OFPTYPE_SET_FLOW_FORMAT:
9240 case OFPTYPE_SET_PACKET_IN_FORMAT:
9241 case OFPTYPE_SET_CONTROLLER_ID:
9242 case OFPTYPE_FLOW_AGE:
9243 case OFPTYPE_FLOW_MONITOR_CANCEL:
9244 case OFPTYPE_SET_ASYNC_CONFIG:
9245 case OFPTYPE_GET_ASYNC_REQUEST:
9246 case OFPTYPE_DESC_STATS_REQUEST:
9247 case OFPTYPE_FLOW_STATS_REQUEST:
9248 case OFPTYPE_AGGREGATE_STATS_REQUEST:
9249 case OFPTYPE_TABLE_STATS_REQUEST:
9250 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
9251 case OFPTYPE_TABLE_DESC_REQUEST:
9252 case OFPTYPE_PORT_STATS_REQUEST:
9253 case OFPTYPE_QUEUE_STATS_REQUEST:
9254 case OFPTYPE_PORT_DESC_STATS_REQUEST:
9255 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
9256 case OFPTYPE_METER_STATS_REQUEST:
9257 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
9258 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
9259 case OFPTYPE_GROUP_STATS_REQUEST:
9260 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
9261 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
9262 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
9263 case OFPTYPE_BUNDLE_CONTROL:
9264 case OFPTYPE_BUNDLE_ADD_MESSAGE:
9267 case OFPTYPE_FEATURES_REPLY:
9268 case OFPTYPE_GET_CONFIG_REPLY:
9269 case OFPTYPE_PACKET_IN:
9270 case OFPTYPE_FLOW_REMOVED:
9271 case OFPTYPE_PORT_STATUS:
9272 case OFPTYPE_BARRIER_REPLY:
9273 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
9274 case OFPTYPE_DESC_STATS_REPLY:
9275 case OFPTYPE_FLOW_STATS_REPLY:
9276 case OFPTYPE_QUEUE_STATS_REPLY:
9277 case OFPTYPE_PORT_STATS_REPLY:
9278 case OFPTYPE_TABLE_STATS_REPLY:
9279 case OFPTYPE_AGGREGATE_STATS_REPLY:
9280 case OFPTYPE_PORT_DESC_STATS_REPLY:
9281 case OFPTYPE_ROLE_REPLY:
9282 case OFPTYPE_FLOW_MONITOR_PAUSED:
9283 case OFPTYPE_FLOW_MONITOR_RESUMED:
9284 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
9285 case OFPTYPE_GET_ASYNC_REPLY:
9286 case OFPTYPE_GROUP_STATS_REPLY:
9287 case OFPTYPE_GROUP_DESC_STATS_REPLY:
9288 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
9289 case OFPTYPE_METER_STATS_REPLY:
9290 case OFPTYPE_METER_CONFIG_STATS_REPLY:
9291 case OFPTYPE_METER_FEATURES_STATS_REPLY:
9292 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
9293 case OFPTYPE_TABLE_DESC_REPLY:
9294 case OFPTYPE_ROLE_STATUS:
9295 case OFPTYPE_REQUESTFORWARD:
9296 case OFPTYPE_NXT_TLV_TABLE_REQUEST:
9297 case OFPTYPE_NXT_TLV_TABLE_REPLY:
9305 ofputil_decode_bundle_add(const struct ofp_header *oh,
9306 struct ofputil_bundle_add_msg *msg,
9307 enum ofptype *typep)
9309 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
9310 enum ofpraw raw = ofpraw_pull_assert(&b);
9311 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE);
9313 const struct ofp14_bundle_ctrl_msg *m = ofpbuf_pull(&b, sizeof *m);
9314 msg->bundle_id = ntohl(m->bundle_id);
9315 msg->flags = ntohs(m->flags);
9318 if (msg->msg->version != oh->version) {
9319 return OFPERR_NXBFC_BAD_VERSION;
9321 size_t inner_len = ntohs(msg->msg->length);
9322 if (inner_len < sizeof(struct ofp_header) || inner_len > b.size) {
9323 return OFPERR_OFPBFC_MSG_BAD_LEN;
9325 if (msg->msg->xid != oh->xid) {
9326 return OFPERR_OFPBFC_MSG_BAD_XID;
9329 /* Reject unbundlable messages. */
9331 enum ofperr error = ofptype_decode(&type, msg->msg);
9333 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPT14_BUNDLE_ADD_MESSAGE contained "
9334 "message is unparsable (%s)", ofperr_get_name(error));
9335 return OFPERR_OFPBFC_MSG_UNSUP; /* 'error' would be confusing. */
9338 if (!ofputil_is_bundlable(type)) {
9339 VLOG_WARN_RL(&bad_ofmsg_rl, "%s message not allowed inside "
9340 "OFPT14_BUNDLE_ADD_MESSAGE", ofptype_get_name(type));
9341 return OFPERR_OFPBFC_MSG_UNSUP;
9351 ofputil_encode_bundle_add(enum ofp_version ofp_version,
9352 struct ofputil_bundle_add_msg *msg)
9354 struct ofpbuf *request;
9355 struct ofp14_bundle_ctrl_msg *m;
9357 /* Must use the same xid as the embedded message. */
9358 request = ofpraw_alloc_xid(OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE, ofp_version,
9360 m = ofpbuf_put_zeros(request, sizeof *m);
9362 m->bundle_id = htonl(msg->bundle_id);
9363 m->flags = htons(msg->flags);
9364 ofpbuf_put(request, msg->msg, ntohs(msg->msg->length));
9370 encode_tlv_table_mappings(struct ofpbuf *b, struct ovs_list *mappings)
9372 struct ofputil_tlv_map *map;
9374 LIST_FOR_EACH (map, list_node, mappings) {
9375 struct nx_tlv_map *nx_map;
9377 nx_map = ofpbuf_put_zeros(b, sizeof *nx_map);
9378 nx_map->option_class = htons(map->option_class);
9379 nx_map->option_type = map->option_type;
9380 nx_map->option_len = map->option_len;
9381 nx_map->index = htons(map->index);
9386 ofputil_encode_tlv_table_mod(enum ofp_version ofp_version,
9387 struct ofputil_tlv_table_mod *ttm)
9390 struct nx_tlv_table_mod *nx_ttm;
9392 b = ofpraw_alloc(OFPRAW_NXT_TLV_TABLE_MOD, ofp_version, 0);
9393 nx_ttm = ofpbuf_put_zeros(b, sizeof *nx_ttm);
9394 nx_ttm->command = htons(ttm->command);
9395 encode_tlv_table_mappings(b, &ttm->mappings);
9401 decode_tlv_table_mappings(struct ofpbuf *msg, unsigned int max_fields,
9402 struct ovs_list *mappings)
9404 list_init(mappings);
9407 struct nx_tlv_map *nx_map;
9408 struct ofputil_tlv_map *map;
9410 nx_map = ofpbuf_pull(msg, sizeof *nx_map);
9411 map = xmalloc(sizeof *map);
9412 list_push_back(mappings, &map->list_node);
9414 map->option_class = ntohs(nx_map->option_class);
9415 map->option_type = nx_map->option_type;
9417 map->option_len = nx_map->option_len;
9418 if (map->option_len % 4 || map->option_len > TLV_MAX_OPT_SIZE) {
9419 VLOG_WARN_RL(&bad_ofmsg_rl,
9420 "tlv table option length (%u) is not a valid option size",
9422 ofputil_uninit_tlv_table(mappings);
9423 return OFPERR_NXTTMFC_BAD_OPT_LEN;
9426 map->index = ntohs(nx_map->index);
9427 if (map->index >= max_fields) {
9428 VLOG_WARN_RL(&bad_ofmsg_rl,
9429 "tlv table field index (%u) is too large (max %u)",
9430 map->index, max_fields - 1);
9431 ofputil_uninit_tlv_table(mappings);
9432 return OFPERR_NXTTMFC_BAD_FIELD_IDX;
9440 ofputil_decode_tlv_table_mod(const struct ofp_header *oh,
9441 struct ofputil_tlv_table_mod *ttm)
9443 struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
9444 ofpraw_pull_assert(&msg);
9446 struct nx_tlv_table_mod *nx_ttm = ofpbuf_pull(&msg, sizeof *nx_ttm);
9447 ttm->command = ntohs(nx_ttm->command);
9448 if (ttm->command > NXTTMC_CLEAR) {
9449 VLOG_WARN_RL(&bad_ofmsg_rl,
9450 "tlv table mod command (%u) is out of range",
9452 return OFPERR_NXTTMFC_BAD_COMMAND;
9455 return decode_tlv_table_mappings(&msg, TUN_METADATA_NUM_OPTS,
9460 ofputil_encode_tlv_table_reply(const struct ofp_header *oh,
9461 struct ofputil_tlv_table_reply *ttr)
9464 struct nx_tlv_table_reply *nx_ttr;
9466 b = ofpraw_alloc_reply(OFPRAW_NXT_TLV_TABLE_REPLY, oh, 0);
9467 nx_ttr = ofpbuf_put_zeros(b, sizeof *nx_ttr);
9468 nx_ttr->max_option_space = htonl(ttr->max_option_space);
9469 nx_ttr->max_fields = htons(ttr->max_fields);
9471 encode_tlv_table_mappings(b, &ttr->mappings);
9476 /* Decodes the NXT_TLV_TABLE_REPLY message in 'oh' into '*ttr'. Returns 0
9477 * if successful, otherwise an ofperr.
9479 * The decoder verifies that the indexes in 'ttr->mappings' are less than
9480 * 'ttr->max_fields', but the caller must ensure, if necessary, that they are
9481 * less than TUN_METADATA_NUM_OPTS. */
9483 ofputil_decode_tlv_table_reply(const struct ofp_header *oh,
9484 struct ofputil_tlv_table_reply *ttr)
9486 struct ofpbuf msg = ofpbuf_const_initializer(oh, ntohs(oh->length));
9487 ofpraw_pull_assert(&msg);
9489 struct nx_tlv_table_reply *nx_ttr = ofpbuf_pull(&msg, sizeof *nx_ttr);
9490 ttr->max_option_space = ntohl(nx_ttr->max_option_space);
9491 ttr->max_fields = ntohs(nx_ttr->max_fields);
9493 return decode_tlv_table_mappings(&msg, ttr->max_fields, &ttr->mappings);
9497 ofputil_uninit_tlv_table(struct ovs_list *mappings)
9499 struct ofputil_tlv_map *map;
9501 LIST_FOR_EACH_POP (map, list_node, mappings) {
9507 ofputil_async_msg_type_to_string(enum ofputil_async_msg_type type)
9510 case OAM_PACKET_IN: return "PACKET_IN";
9511 case OAM_PORT_STATUS: return "PORT_STATUS";
9512 case OAM_FLOW_REMOVED: return "FLOW_REMOVED";
9513 case OAM_ROLE_STATUS: return "ROLE_STATUS";
9514 case OAM_TABLE_STATUS: return "TABLE_STATUS";
9515 case OAM_REQUESTFORWARD: return "REQUESTFORWARD";
9523 struct ofp14_async_prop {
9525 enum ofputil_async_msg_type oam;
9527 uint32_t allowed10, allowed14;
9530 #define AP_PAIR(SLAVE_PROP_TYPE, OAM, A10, A14) \
9531 { SLAVE_PROP_TYPE, OAM, false, A10, (A14) ? (A14) : (A10) }, \
9532 { (SLAVE_PROP_TYPE + 1), OAM, true, A10, (A14) ? (A14) : (A10) }
9534 static const struct ofp14_async_prop async_props[] = {
9535 AP_PAIR( 0, OAM_PACKET_IN, OFPR10_BITS, OFPR14_BITS),
9536 AP_PAIR( 2, OAM_PORT_STATUS, (1 << OFPPR_N_REASONS) - 1, 0),
9537 AP_PAIR( 4, OAM_FLOW_REMOVED, (1 << OVS_OFPRR_NONE) - 1, 0),
9538 AP_PAIR( 6, OAM_ROLE_STATUS, (1 << OFPCRR_N_REASONS) - 1, 0),
9539 AP_PAIR( 8, OAM_TABLE_STATUS, OFPTR_BITS, 0),
9540 AP_PAIR(10, OAM_REQUESTFORWARD, (1 << OFPRFR_N_REASONS) - 1, 0),
9543 #define FOR_EACH_ASYNC_PROP(VAR) \
9544 for (const struct ofp14_async_prop *VAR = async_props; \
9545 VAR < &async_props[ARRAY_SIZE(async_props)]; VAR++)
9547 static const struct ofp14_async_prop *
9548 get_ofp14_async_config_prop_by_prop_type(uint64_t prop_type)
9550 FOR_EACH_ASYNC_PROP (ap) {
9551 if (prop_type == ap->prop_type) {
9558 static const struct ofp14_async_prop *
9559 get_ofp14_async_config_prop_by_oam(enum ofputil_async_msg_type oam,
9562 FOR_EACH_ASYNC_PROP (ap) {
9563 if (ap->oam == oam && ap->master == master) {
9571 ofp14_async_prop_allowed(const struct ofp14_async_prop *prop,
9572 enum ofp_version version)
9574 return version >= OFP14_VERSION ? prop->allowed14 : prop->allowed10;
9578 encode_async_mask(const struct ofputil_async_cfg *src,
9579 const struct ofp14_async_prop *ap,
9580 enum ofp_version version)
9582 uint32_t mask = ap->master ? src->master[ap->oam] : src->slave[ap->oam];
9583 return htonl(mask & ofp14_async_prop_allowed(ap, version));
9587 decode_async_mask(ovs_be32 src,
9588 const struct ofp14_async_prop *ap, enum ofp_version version,
9589 bool loose, struct ofputil_async_cfg *dst)
9591 uint32_t mask = ntohl(src);
9592 uint32_t allowed = ofp14_async_prop_allowed(ap, version);
9593 if (mask & ~allowed) {
9594 OFPPROP_LOG(&bad_ofmsg_rl, loose,
9595 "bad value %#x for %s (allowed mask %#x)",
9596 mask, ofputil_async_msg_type_to_string(ap->oam),
9600 return OFPERR_OFPACFC_INVALID;
9604 if (ap->oam == OAM_PACKET_IN) {
9605 if (mask & (1u << OFPR_NO_MATCH)) {
9606 mask |= 1u << OFPR_EXPLICIT_MISS;
9607 if (version < OFP13_VERSION) {
9608 mask |= 1u << OFPR_IMPLICIT_MISS;
9613 uint32_t *array = ap->master ? dst->master : dst->slave;
9614 array[ap->oam] = mask;
9619 parse_async_tlv(const struct ofpbuf *property,
9620 const struct ofp14_async_prop *ap,
9621 struct ofputil_async_cfg *ac,
9622 enum ofp_version version, bool loose)
9627 error = ofpprop_parse_be32(property, &mask);
9632 if (ofpprop_is_experimenter(ap->prop_type)) {
9633 /* For experimenter properties, whether a property is for the master or
9634 * slave role is indicated by both 'type' and 'exp_type' in struct
9635 * ofp_prop_experimenter. Check that these are consistent. */
9636 const struct ofp_prop_experimenter *ope = property->data;
9637 bool should_be_master = ope->type == htons(0xffff);
9638 if (should_be_master != ap->master) {
9639 VLOG_WARN_RL(&bad_ofmsg_rl, "async property type %#"PRIx16" "
9640 "indicates %s role but exp_type %"PRIu32" indicates "
9643 should_be_master ? "master" : "slave",
9644 ntohl(ope->exp_type),
9645 ap->master ? "master" : "slave");
9646 return OFPERR_OFPBPC_BAD_EXP_TYPE;
9650 return decode_async_mask(mask, ap, version, loose, ac);
9654 decode_legacy_async_masks(const ovs_be32 masks[2],
9655 enum ofputil_async_msg_type oam,
9656 enum ofp_version version,
9657 struct ofputil_async_cfg *dst)
9659 for (int i = 0; i < 2; i++) {
9660 bool master = i == 0;
9661 const struct ofp14_async_prop *ap
9662 = get_ofp14_async_config_prop_by_oam(oam, master);
9663 decode_async_mask(masks[i], ap, version, true, dst);
9667 /* Decodes the OpenFlow "set async config" request and "get async config
9668 * reply" message in '*oh' into an abstract form in 'ac'.
9670 * Some versions of the "set async config" request change only some of the
9671 * settings and leave the others alone. This function uses 'basis' as the
9672 * initial state for decoding these. Other versions of the request change all
9673 * the settings; this function ignores 'basis' when decoding these.
9675 * If 'loose' is true, this function ignores properties and values that it does
9676 * not understand, as a controller would want to do when interpreting
9677 * capabilities provided by a switch. If 'loose' is false, this function
9678 * treats unknown properties and values as an error, as a switch would want to
9679 * do when interpreting a configuration request made by a controller.
9681 * Returns 0 if successful, otherwise an OFPERR_* value.
9683 * Returns error code OFPERR_OFPACFC_INVALID if the value of mask is not in
9684 * the valid range of mask.
9686 * Returns error code OFPERR_OFPACFC_UNSUPPORTED if the configuration is not
9689 ofputil_decode_set_async_config(const struct ofp_header *oh, bool loose,
9690 const struct ofputil_async_cfg *basis,
9691 struct ofputil_async_cfg *ac)
9693 struct ofpbuf b = ofpbuf_const_initializer(oh, ntohs(oh->length));
9694 enum ofpraw raw = ofpraw_pull_assert(&b);
9696 if (raw == OFPRAW_OFPT13_SET_ASYNC ||
9697 raw == OFPRAW_NXT_SET_ASYNC_CONFIG ||
9698 raw == OFPRAW_OFPT13_GET_ASYNC_REPLY) {
9699 const struct nx_async_config *msg = ofpmsg_body(oh);
9701 *ac = OFPUTIL_ASYNC_CFG_INIT;
9702 decode_legacy_async_masks(msg->packet_in_mask, OAM_PACKET_IN,
9704 decode_legacy_async_masks(msg->port_status_mask, OAM_PORT_STATUS,
9706 decode_legacy_async_masks(msg->flow_removed_mask, OAM_FLOW_REMOVED,
9708 } else if (raw == OFPRAW_OFPT14_SET_ASYNC ||
9709 raw == OFPRAW_OFPT14_GET_ASYNC_REPLY ||
9710 raw == OFPRAW_NXT_SET_ASYNC_CONFIG2) {
9712 while (b.size > 0) {
9713 struct ofpbuf property;
9717 error = ofpprop_pull__(&b, &property, 8, 0xfffe, &type);
9722 const struct ofp14_async_prop *ap
9723 = get_ofp14_async_config_prop_by_prop_type(type);
9725 ? parse_async_tlv(&property, ap, ac, oh->version, loose)
9726 : OFPPROP_UNKNOWN(loose, "async config", type));
9728 /* Most messages use OFPBPC_BAD_TYPE but async has its own (who
9729 * knows why, it's OpenFlow. */
9730 if (error == OFPERR_OFPBPC_BAD_TYPE) {
9731 error = OFPERR_OFPACFC_UNSUPPORTED;
9737 return OFPERR_OFPBRC_BAD_VERSION;
9743 encode_legacy_async_masks(const struct ofputil_async_cfg *ac,
9744 enum ofputil_async_msg_type oam,
9745 enum ofp_version version,
9748 for (int i = 0; i < 2; i++) {
9749 bool master = i == 0;
9750 const struct ofp14_async_prop *ap
9751 = get_ofp14_async_config_prop_by_oam(oam, master);
9752 masks[i] = encode_async_mask(ac, ap, version);
9757 ofputil_put_async_config__(const struct ofputil_async_cfg *ac,
9758 struct ofpbuf *buf, bool tlv,
9759 enum ofp_version version, uint32_t oams)
9762 struct nx_async_config *msg = ofpbuf_put_zeros(buf, sizeof *msg);
9763 encode_legacy_async_masks(ac, OAM_PACKET_IN, version,
9764 msg->packet_in_mask);
9765 encode_legacy_async_masks(ac, OAM_PORT_STATUS, version,
9766 msg->port_status_mask);
9767 encode_legacy_async_masks(ac, OAM_FLOW_REMOVED, version,
9768 msg->flow_removed_mask);
9770 FOR_EACH_ASYNC_PROP (ap) {
9771 if (oams & (1u << ap->oam)) {
9772 size_t ofs = buf->size;
9773 ofpprop_put_be32(buf, ap->prop_type,
9774 encode_async_mask(ac, ap, version));
9776 /* For experimenter properties, we need to use type 0xfffe for
9777 * master and 0xffff for slaves. */
9778 if (ofpprop_is_experimenter(ap->prop_type)) {
9779 struct ofp_prop_experimenter *ope
9780 = ofpbuf_at_assert(buf, ofs, sizeof *ope);
9781 ope->type = ap->master ? htons(0xffff) : htons(0xfffe);
9788 /* Encodes and returns a reply to the OFPT_GET_ASYNC_REQUEST in 'oh' that
9789 * states that the asynchronous message configuration is 'ac'. */
9791 ofputil_encode_get_async_reply(const struct ofp_header *oh,
9792 const struct ofputil_async_cfg *ac)
9796 enum ofpraw raw = (oh->version < OFP14_VERSION
9797 ? OFPRAW_OFPT13_GET_ASYNC_REPLY
9798 : OFPRAW_OFPT14_GET_ASYNC_REPLY);
9799 struct ofpbuf *reply = ofpraw_alloc_reply(raw, oh, 0);
9800 ofputil_put_async_config__(ac, reply,
9801 raw == OFPRAW_OFPT14_GET_ASYNC_REPLY,
9802 oh->version, UINT32_MAX);
9808 /* Encodes and returns a message, in a format appropriate for OpenFlow version
9809 * 'ofp_version', that sets the asynchronous message configuration to 'ac'.
9811 * Specify 'oams' as a bitmap of OAM_* that indicate the asynchronous messages
9812 * to configure. OF1.0 through OF1.3 can't natively configure a subset of
9813 * messages, so more messages than requested may be configured. OF1.0 through
9814 * OF1.3 also can't configure OVS extension OAM_* values, so if 'oam' includes
9815 * any extensions then this function encodes an Open vSwitch extension message
9816 * that does support configuring OVS extension OAM_*. */
9818 ofputil_encode_set_async_config(const struct ofputil_async_cfg *ac,
9819 uint32_t oams, enum ofp_version ofp_version)
9821 enum ofpraw raw = (ofp_version >= OFP14_VERSION ? OFPRAW_OFPT14_SET_ASYNC
9822 : oams & OAM_EXTENSIONS ? OFPRAW_NXT_SET_ASYNC_CONFIG2
9823 : ofp_version >= OFP13_VERSION ? OFPRAW_OFPT13_SET_ASYNC
9824 : OFPRAW_NXT_SET_ASYNC_CONFIG);
9825 struct ofpbuf *request = ofpraw_alloc(raw, ofp_version, 0);
9826 ofputil_put_async_config__(ac, request,
9827 (raw == OFPRAW_OFPT14_SET_ASYNC ||
9828 raw == OFPRAW_NXT_SET_ASYNC_CONFIG2),
9833 struct ofputil_async_cfg
9834 ofputil_async_cfg_default(enum ofp_version version)
9836 /* We enable all of the OF1.4 reasons regardless of 'version' because the
9837 * reasons added in OF1.4 just are just refinements of the OFPR_ACTION
9838 * introduced in OF1.0, breaking it into more specific categories. When we
9839 * encode these for earlier OpenFlow versions, we translate them into
9841 uint32_t pin = OFPR14_BITS & ~(1u << OFPR_INVALID_TTL);
9842 pin |= 1u << OFPR_EXPLICIT_MISS;
9843 if (version <= OFP12_VERSION) {
9844 pin |= 1u << OFPR_IMPLICIT_MISS;
9847 return (struct ofputil_async_cfg) {
9848 .master[OAM_PACKET_IN] = pin,
9850 .master[OAM_FLOW_REMOVED]
9851 = (version >= OFP14_VERSION ? OFPRR14_BITS : OFPRR10_BITS),
9853 .master[OAM_PORT_STATUS] = OFPPR_BITS,
9854 .slave[OAM_PORT_STATUS] = OFPPR_BITS,