2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofp-print.h"
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
27 #include "byte-order.h"
28 #include "classifier.h"
29 #include "dynamic-string.h"
31 #include "meta-flow.h"
32 #include "multipath.h"
36 #include "ofp-actions.h"
37 #include "ofp-errors.h"
43 #include "unaligned.h"
44 #include "type-props.h"
45 #include "openvswitch/vlog.h"
48 VLOG_DEFINE_THIS_MODULE(ofp_util);
50 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
51 * in the peer and so there's not much point in showing a lot of them. */
52 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
54 static enum ofputil_table_miss ofputil_table_miss_from_config(
55 ovs_be32 config_, enum ofp_version);
57 struct ofp_prop_header {
62 /* Pulls a property, beginning with struct ofp_prop_header, from the beginning
63 * of 'msg'. Stores the type of the property in '*typep' and, if 'property' is
64 * nonnull, the entire property, including the header, in '*property'. Returns
65 * 0 if successful, otherwise an error code.
67 * This function pulls the property's stated size padded out to a multiple of
68 * 'alignment' bytes. The common case in OpenFlow is an 'alignment' of 8, so
69 * you can use ofputil_pull_property() for that case. */
71 ofputil_pull_property__(struct ofpbuf *msg, struct ofpbuf *property,
72 unsigned int alignment, uint16_t *typep)
74 struct ofp_prop_header *oph;
75 unsigned int padded_len;
78 if (ofpbuf_size(msg) < sizeof *oph) {
79 return OFPERR_OFPBPC_BAD_LEN;
82 oph = ofpbuf_data(msg);
83 len = ntohs(oph->len);
84 padded_len = ROUND_UP(len, alignment);
85 if (len < sizeof *oph || padded_len > ofpbuf_size(msg)) {
86 return OFPERR_OFPBPC_BAD_LEN;
89 *typep = ntohs(oph->type);
91 ofpbuf_use_const(property, ofpbuf_data(msg), len);
93 ofpbuf_pull(msg, padded_len);
97 /* Pulls a property, beginning with struct ofp_prop_header, from the beginning
98 * of 'msg'. Stores the type of the property in '*typep' and, if 'property' is
99 * nonnull, the entire property, including the header, in '*property'. Returns
100 * 0 if successful, otherwise an error code.
102 * This function pulls the property's stated size padded out to a multiple of
103 * 8 bytes, which is the common case for OpenFlow properties. */
105 ofputil_pull_property(struct ofpbuf *msg, struct ofpbuf *property,
108 return ofputil_pull_property__(msg, property, 8, typep);
111 static void OVS_PRINTF_FORMAT(2, 3)
112 log_property(bool loose, const char *message, ...)
114 enum vlog_level level = loose ? VLL_DBG : VLL_WARN;
115 if (!vlog_should_drop(THIS_MODULE, level, &bad_ofmsg_rl)) {
118 va_start(args, message);
119 vlog_valist(THIS_MODULE, level, message, args);
125 start_property(struct ofpbuf *msg, uint16_t type)
127 size_t start_ofs = ofpbuf_size(msg);
128 struct ofp_prop_header *oph;
130 oph = ofpbuf_put_uninit(msg, sizeof *oph);
131 oph->type = htons(type);
132 oph->len = htons(4); /* May be updated later by end_property(). */
137 end_property(struct ofpbuf *msg, size_t start_ofs)
139 struct ofp_prop_header *oph;
141 oph = ofpbuf_at_assert(msg, start_ofs, sizeof *oph);
142 oph->len = htons(ofpbuf_size(msg) - start_ofs);
143 ofpbuf_padto(msg, ROUND_UP(ofpbuf_size(msg), 8));
147 put_bitmap_properties(struct ofpbuf *msg, uint64_t bitmap)
149 for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
150 start_property(msg, rightmost_1bit_idx(bitmap));
154 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
155 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
158 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
159 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
160 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
161 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
164 ofputil_wcbits_to_netmask(int wcbits)
167 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
170 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
171 * that it wildcards, that is, the number of 0-bits in 'netmask', a number
172 * between 0 and 32 inclusive.
174 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
175 * still be in the valid range but isn't otherwise meaningful. */
177 ofputil_netmask_to_wcbits(ovs_be32 netmask)
179 return 32 - ip_count_cidr_bits(netmask);
182 /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
183 * flow_wildcards in 'wc' for use in struct match. It is the caller's
184 * responsibility to handle the special case where the flow match's dl_vlan is
185 * set to OFP_VLAN_NONE. */
187 ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc)
189 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 31);
191 /* Initialize most of wc. */
192 flow_wildcards_init_catchall(wc);
194 if (!(ofpfw & OFPFW10_IN_PORT)) {
195 wc->masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
198 if (!(ofpfw & OFPFW10_NW_TOS)) {
199 wc->masks.nw_tos |= IP_DSCP_MASK;
202 if (!(ofpfw & OFPFW10_NW_PROTO)) {
203 wc->masks.nw_proto = UINT8_MAX;
205 wc->masks.nw_src = ofputil_wcbits_to_netmask(ofpfw
206 >> OFPFW10_NW_SRC_SHIFT);
207 wc->masks.nw_dst = ofputil_wcbits_to_netmask(ofpfw
208 >> OFPFW10_NW_DST_SHIFT);
210 if (!(ofpfw & OFPFW10_TP_SRC)) {
211 wc->masks.tp_src = OVS_BE16_MAX;
213 if (!(ofpfw & OFPFW10_TP_DST)) {
214 wc->masks.tp_dst = OVS_BE16_MAX;
217 if (!(ofpfw & OFPFW10_DL_SRC)) {
218 memset(wc->masks.dl_src, 0xff, ETH_ADDR_LEN);
220 if (!(ofpfw & OFPFW10_DL_DST)) {
221 memset(wc->masks.dl_dst, 0xff, ETH_ADDR_LEN);
223 if (!(ofpfw & OFPFW10_DL_TYPE)) {
224 wc->masks.dl_type = OVS_BE16_MAX;
228 if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) {
229 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
231 if (!(ofpfw & OFPFW10_DL_VLAN)) {
232 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
236 /* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */
238 ofputil_match_from_ofp10_match(const struct ofp10_match *ofmatch,
241 uint32_t ofpfw = ntohl(ofmatch->wildcards) & OFPFW10_ALL;
243 /* Initialize match->wc. */
244 memset(&match->flow, 0, sizeof match->flow);
245 ofputil_wildcard_from_ofpfw10(ofpfw, &match->wc);
247 /* Initialize most of match->flow. */
248 match->flow.nw_src = ofmatch->nw_src;
249 match->flow.nw_dst = ofmatch->nw_dst;
250 match->flow.in_port.ofp_port = u16_to_ofp(ntohs(ofmatch->in_port));
251 match->flow.dl_type = ofputil_dl_type_from_openflow(ofmatch->dl_type);
252 match->flow.tp_src = ofmatch->tp_src;
253 match->flow.tp_dst = ofmatch->tp_dst;
254 memcpy(match->flow.dl_src, ofmatch->dl_src, ETH_ADDR_LEN);
255 memcpy(match->flow.dl_dst, ofmatch->dl_dst, ETH_ADDR_LEN);
256 match->flow.nw_tos = ofmatch->nw_tos & IP_DSCP_MASK;
257 match->flow.nw_proto = ofmatch->nw_proto;
259 /* Translate VLANs. */
260 if (!(ofpfw & OFPFW10_DL_VLAN) &&
261 ofmatch->dl_vlan == htons(OFP10_VLAN_NONE)) {
262 /* Match only packets without 802.1Q header.
264 * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
266 * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
267 * because we can't have a specific PCP without an 802.1Q header.
268 * However, older versions of OVS treated this as matching packets
269 * withut an 802.1Q header, so we do here too. */
270 match->flow.vlan_tci = htons(0);
271 match->wc.masks.vlan_tci = htons(0xffff);
273 ovs_be16 vid, pcp, tci;
276 vid = ofmatch->dl_vlan & htons(VLAN_VID_MASK);
277 hpcp = (ofmatch->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK;
279 tci = vid | pcp | htons(VLAN_CFI);
280 match->flow.vlan_tci = tci & match->wc.masks.vlan_tci;
284 match_zero_wildcarded_fields(match);
287 /* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */
289 ofputil_match_to_ofp10_match(const struct match *match,
290 struct ofp10_match *ofmatch)
292 const struct flow_wildcards *wc = &match->wc;
295 /* Figure out most OpenFlow wildcards. */
297 if (!wc->masks.in_port.ofp_port) {
298 ofpfw |= OFPFW10_IN_PORT;
300 if (!wc->masks.dl_type) {
301 ofpfw |= OFPFW10_DL_TYPE;
303 if (!wc->masks.nw_proto) {
304 ofpfw |= OFPFW10_NW_PROTO;
306 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_src)
307 << OFPFW10_NW_SRC_SHIFT);
308 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_dst)
309 << OFPFW10_NW_DST_SHIFT);
310 if (!(wc->masks.nw_tos & IP_DSCP_MASK)) {
311 ofpfw |= OFPFW10_NW_TOS;
313 if (!wc->masks.tp_src) {
314 ofpfw |= OFPFW10_TP_SRC;
316 if (!wc->masks.tp_dst) {
317 ofpfw |= OFPFW10_TP_DST;
319 if (eth_addr_is_zero(wc->masks.dl_src)) {
320 ofpfw |= OFPFW10_DL_SRC;
322 if (eth_addr_is_zero(wc->masks.dl_dst)) {
323 ofpfw |= OFPFW10_DL_DST;
326 /* Translate VLANs. */
327 ofmatch->dl_vlan = htons(0);
328 ofmatch->dl_vlan_pcp = 0;
329 if (match->wc.masks.vlan_tci == htons(0)) {
330 ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP;
331 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
332 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
333 ofmatch->dl_vlan = htons(OFP10_VLAN_NONE);
334 ofpfw |= OFPFW10_DL_VLAN_PCP;
336 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
337 ofpfw |= OFPFW10_DL_VLAN;
339 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
342 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
343 ofpfw |= OFPFW10_DL_VLAN_PCP;
345 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
349 /* Compose most of the match structure. */
350 ofmatch->wildcards = htonl(ofpfw);
351 ofmatch->in_port = htons(ofp_to_u16(match->flow.in_port.ofp_port));
352 memcpy(ofmatch->dl_src, match->flow.dl_src, ETH_ADDR_LEN);
353 memcpy(ofmatch->dl_dst, match->flow.dl_dst, ETH_ADDR_LEN);
354 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
355 ofmatch->nw_src = match->flow.nw_src;
356 ofmatch->nw_dst = match->flow.nw_dst;
357 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
358 ofmatch->nw_proto = match->flow.nw_proto;
359 ofmatch->tp_src = match->flow.tp_src;
360 ofmatch->tp_dst = match->flow.tp_dst;
361 memset(ofmatch->pad1, '\0', sizeof ofmatch->pad1);
362 memset(ofmatch->pad2, '\0', sizeof ofmatch->pad2);
366 ofputil_pull_ofp11_match(struct ofpbuf *buf, struct match *match,
367 uint16_t *padded_match_len)
369 struct ofp11_match_header *omh = ofpbuf_data(buf);
372 if (ofpbuf_size(buf) < sizeof *omh) {
373 return OFPERR_OFPBMC_BAD_LEN;
376 match_len = ntohs(omh->length);
378 switch (ntohs(omh->type)) {
379 case OFPMT_STANDARD: {
380 struct ofp11_match *om;
382 if (match_len != sizeof *om || ofpbuf_size(buf) < sizeof *om) {
383 return OFPERR_OFPBMC_BAD_LEN;
385 om = ofpbuf_pull(buf, sizeof *om);
386 if (padded_match_len) {
387 *padded_match_len = match_len;
389 return ofputil_match_from_ofp11_match(om, match);
393 if (padded_match_len) {
394 *padded_match_len = ROUND_UP(match_len, 8);
396 return oxm_pull_match(buf, match);
399 return OFPERR_OFPBMC_BAD_TYPE;
403 /* Converts the ofp11_match in 'ofmatch' into a struct match in 'match'.
404 * Returns 0 if successful, otherwise an OFPERR_* value. */
406 ofputil_match_from_ofp11_match(const struct ofp11_match *ofmatch,
409 uint16_t wc = ntohl(ofmatch->wildcards);
410 uint8_t dl_src_mask[ETH_ADDR_LEN];
411 uint8_t dl_dst_mask[ETH_ADDR_LEN];
412 bool ipv4, arp, rarp;
415 match_init_catchall(match);
417 if (!(wc & OFPFW11_IN_PORT)) {
421 error = ofputil_port_from_ofp11(ofmatch->in_port, &ofp_port);
423 return OFPERR_OFPBMC_BAD_VALUE;
425 match_set_in_port(match, ofp_port);
428 for (i = 0; i < ETH_ADDR_LEN; i++) {
429 dl_src_mask[i] = ~ofmatch->dl_src_mask[i];
431 match_set_dl_src_masked(match, ofmatch->dl_src, dl_src_mask);
433 for (i = 0; i < ETH_ADDR_LEN; i++) {
434 dl_dst_mask[i] = ~ofmatch->dl_dst_mask[i];
436 match_set_dl_dst_masked(match, ofmatch->dl_dst, dl_dst_mask);
438 if (!(wc & OFPFW11_DL_VLAN)) {
439 if (ofmatch->dl_vlan == htons(OFPVID11_NONE)) {
440 /* Match only packets without a VLAN tag. */
441 match->flow.vlan_tci = htons(0);
442 match->wc.masks.vlan_tci = OVS_BE16_MAX;
444 if (ofmatch->dl_vlan == htons(OFPVID11_ANY)) {
445 /* Match any packet with a VLAN tag regardless of VID. */
446 match->flow.vlan_tci = htons(VLAN_CFI);
447 match->wc.masks.vlan_tci = htons(VLAN_CFI);
448 } else if (ntohs(ofmatch->dl_vlan) < 4096) {
449 /* Match only packets with the specified VLAN VID. */
450 match->flow.vlan_tci = htons(VLAN_CFI) | ofmatch->dl_vlan;
451 match->wc.masks.vlan_tci = htons(VLAN_CFI | VLAN_VID_MASK);
454 return OFPERR_OFPBMC_BAD_VALUE;
457 if (!(wc & OFPFW11_DL_VLAN_PCP)) {
458 if (ofmatch->dl_vlan_pcp <= 7) {
459 match->flow.vlan_tci |= htons(ofmatch->dl_vlan_pcp
461 match->wc.masks.vlan_tci |= htons(VLAN_PCP_MASK);
464 return OFPERR_OFPBMC_BAD_VALUE;
470 if (!(wc & OFPFW11_DL_TYPE)) {
471 match_set_dl_type(match,
472 ofputil_dl_type_from_openflow(ofmatch->dl_type));
475 ipv4 = match->flow.dl_type == htons(ETH_TYPE_IP);
476 arp = match->flow.dl_type == htons(ETH_TYPE_ARP);
477 rarp = match->flow.dl_type == htons(ETH_TYPE_RARP);
479 if (ipv4 && !(wc & OFPFW11_NW_TOS)) {
480 if (ofmatch->nw_tos & ~IP_DSCP_MASK) {
482 return OFPERR_OFPBMC_BAD_VALUE;
485 match_set_nw_dscp(match, ofmatch->nw_tos);
488 if (ipv4 || arp || rarp) {
489 if (!(wc & OFPFW11_NW_PROTO)) {
490 match_set_nw_proto(match, ofmatch->nw_proto);
492 match_set_nw_src_masked(match, ofmatch->nw_src, ~ofmatch->nw_src_mask);
493 match_set_nw_dst_masked(match, ofmatch->nw_dst, ~ofmatch->nw_dst_mask);
496 #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
497 if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) {
498 switch (match->flow.nw_proto) {
500 /* "A.2.3 Flow Match Structures" in OF1.1 says:
502 * The tp_src and tp_dst fields will be ignored unless the
503 * network protocol specified is as TCP, UDP or SCTP.
505 * but I'm pretty sure we should support ICMP too, otherwise
506 * that's a regression from OF1.0. */
507 if (!(wc & OFPFW11_TP_SRC)) {
508 uint16_t icmp_type = ntohs(ofmatch->tp_src);
509 if (icmp_type < 0x100) {
510 match_set_icmp_type(match, icmp_type);
512 return OFPERR_OFPBMC_BAD_FIELD;
515 if (!(wc & OFPFW11_TP_DST)) {
516 uint16_t icmp_code = ntohs(ofmatch->tp_dst);
517 if (icmp_code < 0x100) {
518 match_set_icmp_code(match, icmp_code);
520 return OFPERR_OFPBMC_BAD_FIELD;
528 if (!(wc & (OFPFW11_TP_SRC))) {
529 match_set_tp_src(match, ofmatch->tp_src);
531 if (!(wc & (OFPFW11_TP_DST))) {
532 match_set_tp_dst(match, ofmatch->tp_dst);
537 /* OF1.1 says explicitly to ignore this. */
542 if (eth_type_mpls(match->flow.dl_type)) {
543 if (!(wc & OFPFW11_MPLS_LABEL)) {
544 match_set_mpls_label(match, 0, ofmatch->mpls_label);
546 if (!(wc & OFPFW11_MPLS_TC)) {
547 match_set_mpls_tc(match, 0, ofmatch->mpls_tc);
551 match_set_metadata_masked(match, ofmatch->metadata,
552 ~ofmatch->metadata_mask);
557 /* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */
559 ofputil_match_to_ofp11_match(const struct match *match,
560 struct ofp11_match *ofmatch)
565 memset(ofmatch, 0, sizeof *ofmatch);
566 ofmatch->omh.type = htons(OFPMT_STANDARD);
567 ofmatch->omh.length = htons(OFPMT11_STANDARD_LENGTH);
569 if (!match->wc.masks.in_port.ofp_port) {
570 wc |= OFPFW11_IN_PORT;
572 ofmatch->in_port = ofputil_port_to_ofp11(match->flow.in_port.ofp_port);
575 memcpy(ofmatch->dl_src, match->flow.dl_src, ETH_ADDR_LEN);
576 for (i = 0; i < ETH_ADDR_LEN; i++) {
577 ofmatch->dl_src_mask[i] = ~match->wc.masks.dl_src[i];
580 memcpy(ofmatch->dl_dst, match->flow.dl_dst, ETH_ADDR_LEN);
581 for (i = 0; i < ETH_ADDR_LEN; i++) {
582 ofmatch->dl_dst_mask[i] = ~match->wc.masks.dl_dst[i];
585 if (match->wc.masks.vlan_tci == htons(0)) {
586 wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP;
587 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
588 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
589 ofmatch->dl_vlan = htons(OFPVID11_NONE);
590 wc |= OFPFW11_DL_VLAN_PCP;
592 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
593 ofmatch->dl_vlan = htons(OFPVID11_ANY);
595 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
598 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
599 wc |= OFPFW11_DL_VLAN_PCP;
601 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
605 if (!match->wc.masks.dl_type) {
606 wc |= OFPFW11_DL_TYPE;
608 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
611 if (!(match->wc.masks.nw_tos & IP_DSCP_MASK)) {
612 wc |= OFPFW11_NW_TOS;
614 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
617 if (!match->wc.masks.nw_proto) {
618 wc |= OFPFW11_NW_PROTO;
620 ofmatch->nw_proto = match->flow.nw_proto;
623 ofmatch->nw_src = match->flow.nw_src;
624 ofmatch->nw_src_mask = ~match->wc.masks.nw_src;
625 ofmatch->nw_dst = match->flow.nw_dst;
626 ofmatch->nw_dst_mask = ~match->wc.masks.nw_dst;
628 if (!match->wc.masks.tp_src) {
629 wc |= OFPFW11_TP_SRC;
631 ofmatch->tp_src = match->flow.tp_src;
634 if (!match->wc.masks.tp_dst) {
635 wc |= OFPFW11_TP_DST;
637 ofmatch->tp_dst = match->flow.tp_dst;
640 if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK))) {
641 wc |= OFPFW11_MPLS_LABEL;
643 ofmatch->mpls_label = htonl(mpls_lse_to_label(
644 match->flow.mpls_lse[0]));
647 if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK))) {
648 wc |= OFPFW11_MPLS_TC;
650 ofmatch->mpls_tc = mpls_lse_to_tc(match->flow.mpls_lse[0]);
653 ofmatch->metadata = match->flow.metadata;
654 ofmatch->metadata_mask = ~match->wc.masks.metadata;
656 ofmatch->wildcards = htonl(wc);
659 /* Returns the "typical" length of a match for 'protocol', for use in
660 * estimating space to preallocate. */
662 ofputil_match_typical_len(enum ofputil_protocol protocol)
665 case OFPUTIL_P_OF10_STD:
666 case OFPUTIL_P_OF10_STD_TID:
667 return sizeof(struct ofp10_match);
669 case OFPUTIL_P_OF10_NXM:
670 case OFPUTIL_P_OF10_NXM_TID:
671 return NXM_TYPICAL_LEN;
673 case OFPUTIL_P_OF11_STD:
674 return sizeof(struct ofp11_match);
676 case OFPUTIL_P_OF12_OXM:
677 case OFPUTIL_P_OF13_OXM:
678 case OFPUTIL_P_OF14_OXM:
679 case OFPUTIL_P_OF15_OXM:
680 return NXM_TYPICAL_LEN;
687 /* Appends to 'b' an struct ofp11_match_header followed by a match that
688 * expresses 'match' properly for 'protocol', plus enough zero bytes to pad the
689 * data appended out to a multiple of 8. 'protocol' must be one that is usable
690 * in OpenFlow 1.1 or later.
692 * This function can cause 'b''s data to be reallocated.
694 * Returns the number of bytes appended to 'b', excluding the padding. Never
697 ofputil_put_ofp11_match(struct ofpbuf *b, const struct match *match,
698 enum ofputil_protocol protocol)
701 case OFPUTIL_P_OF10_STD:
702 case OFPUTIL_P_OF10_STD_TID:
703 case OFPUTIL_P_OF10_NXM:
704 case OFPUTIL_P_OF10_NXM_TID:
707 case OFPUTIL_P_OF11_STD: {
708 struct ofp11_match *om;
710 /* Make sure that no padding is needed. */
711 BUILD_ASSERT_DECL(sizeof *om % 8 == 0);
713 om = ofpbuf_put_uninit(b, sizeof *om);
714 ofputil_match_to_ofp11_match(match, om);
718 case OFPUTIL_P_OF12_OXM:
719 case OFPUTIL_P_OF13_OXM:
720 case OFPUTIL_P_OF14_OXM:
721 case OFPUTIL_P_OF15_OXM:
722 return oxm_put_match(b, match,
723 ofputil_protocol_to_ofp_version(protocol));
729 /* Given a 'dl_type' value in the format used in struct flow, returns the
730 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
733 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
735 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
736 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
740 /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
741 * structure, returns the corresponding 'dl_type' value for use in struct
744 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
746 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
747 ? htons(FLOW_DL_TYPE_NONE)
753 struct proto_abbrev {
754 enum ofputil_protocol protocol;
758 /* Most users really don't care about some of the differences between
759 * protocols. These abbreviations help with that. */
760 static const struct proto_abbrev proto_abbrevs[] = {
761 { OFPUTIL_P_ANY, "any" },
762 { OFPUTIL_P_OF10_STD_ANY, "OpenFlow10" },
763 { OFPUTIL_P_OF10_NXM_ANY, "NXM" },
764 { OFPUTIL_P_ANY_OXM, "OXM" },
766 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
768 enum ofputil_protocol ofputil_flow_dump_protocols[] = {
777 size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
779 /* Returns the set of ofputil_protocols that are supported with the given
780 * OpenFlow 'version'. 'version' should normally be an 8-bit OpenFlow version
781 * identifier (e.g. 0x01 for OpenFlow 1.0, 0x02 for OpenFlow 1.1). Returns 0
782 * if 'version' is not supported or outside the valid range. */
783 enum ofputil_protocol
784 ofputil_protocols_from_ofp_version(enum ofp_version version)
788 return OFPUTIL_P_OF10_STD_ANY | OFPUTIL_P_OF10_NXM_ANY;
790 return OFPUTIL_P_OF11_STD;
792 return OFPUTIL_P_OF12_OXM;
794 return OFPUTIL_P_OF13_OXM;
796 return OFPUTIL_P_OF14_OXM;
798 return OFPUTIL_P_OF15_OXM;
804 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
805 * connection that has negotiated the given 'version'. 'version' should
806 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
807 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
808 * outside the valid range. */
809 enum ofputil_protocol
810 ofputil_protocol_from_ofp_version(enum ofp_version version)
812 return rightmost_1bit(ofputil_protocols_from_ofp_version(version));
815 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
816 * etc.) that corresponds to 'protocol'. */
818 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
821 case OFPUTIL_P_OF10_STD:
822 case OFPUTIL_P_OF10_STD_TID:
823 case OFPUTIL_P_OF10_NXM:
824 case OFPUTIL_P_OF10_NXM_TID:
825 return OFP10_VERSION;
826 case OFPUTIL_P_OF11_STD:
827 return OFP11_VERSION;
828 case OFPUTIL_P_OF12_OXM:
829 return OFP12_VERSION;
830 case OFPUTIL_P_OF13_OXM:
831 return OFP13_VERSION;
832 case OFPUTIL_P_OF14_OXM:
833 return OFP14_VERSION;
834 case OFPUTIL_P_OF15_OXM:
835 return OFP15_VERSION;
841 /* Returns a bitmap of OpenFlow versions that are supported by at
842 * least one of the 'protocols'. */
844 ofputil_protocols_to_version_bitmap(enum ofputil_protocol protocols)
848 for (; protocols; protocols = zero_rightmost_1bit(protocols)) {
849 enum ofputil_protocol protocol = rightmost_1bit(protocols);
851 bitmap |= 1u << ofputil_protocol_to_ofp_version(protocol);
857 /* Returns the set of protocols that are supported on top of the
858 * OpenFlow versions included in 'bitmap'. */
859 enum ofputil_protocol
860 ofputil_protocols_from_version_bitmap(uint32_t bitmap)
862 enum ofputil_protocol protocols = 0;
864 for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
865 enum ofp_version version = rightmost_1bit_idx(bitmap);
867 protocols |= ofputil_protocols_from_ofp_version(version);
873 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
876 ofputil_protocol_is_valid(enum ofputil_protocol protocol)
878 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
881 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
882 * extension turned on or off if 'enable' is true or false, respectively.
884 * This extension is only useful for protocols whose "standard" version does
885 * not allow specific tables to be modified. In particular, this is true of
886 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
887 * specifies a table ID and so there is no need for such an extension. When
888 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
889 * extension, this function just returns its 'protocol' argument unchanged
890 * regardless of the value of 'enable'. */
891 enum ofputil_protocol
892 ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
895 case OFPUTIL_P_OF10_STD:
896 case OFPUTIL_P_OF10_STD_TID:
897 return enable ? OFPUTIL_P_OF10_STD_TID : OFPUTIL_P_OF10_STD;
899 case OFPUTIL_P_OF10_NXM:
900 case OFPUTIL_P_OF10_NXM_TID:
901 return enable ? OFPUTIL_P_OF10_NXM_TID : OFPUTIL_P_OF10_NXM;
903 case OFPUTIL_P_OF11_STD:
904 return OFPUTIL_P_OF11_STD;
906 case OFPUTIL_P_OF12_OXM:
907 return OFPUTIL_P_OF12_OXM;
909 case OFPUTIL_P_OF13_OXM:
910 return OFPUTIL_P_OF13_OXM;
912 case OFPUTIL_P_OF14_OXM:
913 return OFPUTIL_P_OF14_OXM;
915 case OFPUTIL_P_OF15_OXM:
916 return OFPUTIL_P_OF15_OXM;
923 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
924 * some extension to a standard protocol version, the return value is the
925 * standard version of that protocol without any extension. If 'protocol' is a
926 * standard protocol version, returns 'protocol' unchanged. */
927 enum ofputil_protocol
928 ofputil_protocol_to_base(enum ofputil_protocol protocol)
930 return ofputil_protocol_set_tid(protocol, false);
933 /* Returns 'new_base' with any extensions taken from 'cur'. */
934 enum ofputil_protocol
935 ofputil_protocol_set_base(enum ofputil_protocol cur,
936 enum ofputil_protocol new_base)
938 bool tid = (cur & OFPUTIL_P_TID) != 0;
941 case OFPUTIL_P_OF10_STD:
942 case OFPUTIL_P_OF10_STD_TID:
943 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD, tid);
945 case OFPUTIL_P_OF10_NXM:
946 case OFPUTIL_P_OF10_NXM_TID:
947 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM, tid);
949 case OFPUTIL_P_OF11_STD:
950 return ofputil_protocol_set_tid(OFPUTIL_P_OF11_STD, tid);
952 case OFPUTIL_P_OF12_OXM:
953 return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM, tid);
955 case OFPUTIL_P_OF13_OXM:
956 return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM, tid);
958 case OFPUTIL_P_OF14_OXM:
959 return ofputil_protocol_set_tid(OFPUTIL_P_OF14_OXM, tid);
961 case OFPUTIL_P_OF15_OXM:
962 return ofputil_protocol_set_tid(OFPUTIL_P_OF15_OXM, tid);
969 /* Returns a string form of 'protocol', if a simple form exists (that is, if
970 * 'protocol' is either a single protocol or it is a combination of protocols
971 * that have a single abbreviation). Otherwise, returns NULL. */
973 ofputil_protocol_to_string(enum ofputil_protocol protocol)
975 const struct proto_abbrev *p;
977 /* Use a "switch" statement for single-bit names so that we get a compiler
978 * warning if we forget any. */
980 case OFPUTIL_P_OF10_NXM:
981 return "NXM-table_id";
983 case OFPUTIL_P_OF10_NXM_TID:
984 return "NXM+table_id";
986 case OFPUTIL_P_OF10_STD:
987 return "OpenFlow10-table_id";
989 case OFPUTIL_P_OF10_STD_TID:
990 return "OpenFlow10+table_id";
992 case OFPUTIL_P_OF11_STD:
995 case OFPUTIL_P_OF12_OXM:
996 return "OXM-OpenFlow12";
998 case OFPUTIL_P_OF13_OXM:
999 return "OXM-OpenFlow13";
1001 case OFPUTIL_P_OF14_OXM:
1002 return "OXM-OpenFlow14";
1004 case OFPUTIL_P_OF15_OXM:
1005 return "OXM-OpenFlow15";
1008 /* Check abbreviations. */
1009 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1010 if (protocol == p->protocol) {
1018 /* Returns a string that represents 'protocols'. The return value might be a
1019 * comma-separated list if 'protocols' doesn't have a simple name. The return
1020 * value is "none" if 'protocols' is 0.
1022 * The caller must free the returned string (with free()). */
1024 ofputil_protocols_to_string(enum ofputil_protocol protocols)
1028 ovs_assert(!(protocols & ~OFPUTIL_P_ANY));
1029 if (protocols == 0) {
1030 return xstrdup("none");
1035 const struct proto_abbrev *p;
1039 ds_put_char(&s, ',');
1042 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1043 if ((protocols & p->protocol) == p->protocol) {
1044 ds_put_cstr(&s, p->name);
1045 protocols &= ~p->protocol;
1050 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
1051 enum ofputil_protocol bit = 1u << i;
1053 if (protocols & bit) {
1054 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
1063 return ds_steal_cstr(&s);
1066 static enum ofputil_protocol
1067 ofputil_protocol_from_string__(const char *s, size_t n)
1069 const struct proto_abbrev *p;
1072 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
1073 enum ofputil_protocol bit = 1u << i;
1074 const char *name = ofputil_protocol_to_string(bit);
1076 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
1081 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1082 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
1090 /* Returns the nonempty set of protocols represented by 's', which can be a
1091 * single protocol name or abbreviation or a comma-separated list of them.
1093 * Aborts the program with an error message if 's' is invalid. */
1094 enum ofputil_protocol
1095 ofputil_protocols_from_string(const char *s)
1097 const char *orig_s = s;
1098 enum ofputil_protocol protocols;
1102 enum ofputil_protocol p;
1105 n = strcspn(s, ",");
1111 p = ofputil_protocol_from_string__(s, n);
1113 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
1121 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
1127 ofputil_version_from_string(const char *s)
1129 if (!strcasecmp(s, "OpenFlow10")) {
1130 return OFP10_VERSION;
1132 if (!strcasecmp(s, "OpenFlow11")) {
1133 return OFP11_VERSION;
1135 if (!strcasecmp(s, "OpenFlow12")) {
1136 return OFP12_VERSION;
1138 if (!strcasecmp(s, "OpenFlow13")) {
1139 return OFP13_VERSION;
1141 if (!strcasecmp(s, "OpenFlow14")) {
1142 return OFP14_VERSION;
1144 if (!strcasecmp(s, "OpenFlow15")) {
1145 return OFP15_VERSION;
1151 is_delimiter(unsigned char c)
1153 return isspace(c) || c == ',';
1157 ofputil_versions_from_string(const char *s)
1160 uint32_t bitmap = 0;
1167 if (is_delimiter(s[i])) {
1172 while (s[i + j] && !is_delimiter(s[i + j])) {
1175 key = xmemdup0(s + i, j);
1176 version = ofputil_version_from_string(key);
1178 VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key);
1181 bitmap |= 1u << version;
1189 ofputil_versions_from_strings(char ** const s, size_t count)
1191 uint32_t bitmap = 0;
1194 int version = ofputil_version_from_string(s[count]);
1196 VLOG_WARN("Unknown OpenFlow version: \"%s\"", s[count]);
1198 bitmap |= 1u << version;
1206 ofputil_version_to_string(enum ofp_version ofp_version)
1208 switch (ofp_version) {
1210 return "OpenFlow10";
1212 return "OpenFlow11";
1214 return "OpenFlow12";
1216 return "OpenFlow13";
1218 return "OpenFlow14";
1220 return "OpenFlow15";
1227 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
1229 switch (packet_in_format) {
1230 case NXPIF_OPENFLOW10:
1239 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
1241 switch (packet_in_format) {
1242 case NXPIF_OPENFLOW10:
1243 return "openflow10";
1252 ofputil_packet_in_format_from_string(const char *s)
1254 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
1255 : !strcmp(s, "nxm") ? NXPIF_NXM
1260 ofputil_format_version(struct ds *msg, enum ofp_version version)
1262 ds_put_format(msg, "0x%02x", version);
1266 ofputil_format_version_name(struct ds *msg, enum ofp_version version)
1268 ds_put_cstr(msg, ofputil_version_to_string(version));
1272 ofputil_format_version_bitmap__(struct ds *msg, uint32_t bitmap,
1273 void (*format_version)(struct ds *msg,
1277 format_version(msg, raw_ctz(bitmap));
1278 bitmap = zero_rightmost_1bit(bitmap);
1280 ds_put_cstr(msg, ", ");
1286 ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap)
1288 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version);
1292 ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap)
1294 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version_name);
1298 ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header *oheh,
1299 uint32_t *allowed_versionsp)
1301 uint16_t bitmap_len = ntohs(oheh->length) - sizeof *oheh;
1302 const ovs_be32 *bitmap = ALIGNED_CAST(const ovs_be32 *, oheh + 1);
1303 uint32_t allowed_versions;
1305 if (!bitmap_len || bitmap_len % sizeof *bitmap) {
1309 /* Only use the first 32-bit element of the bitmap as that is all the
1310 * current implementation supports. Subsequent elements are ignored which
1311 * should have no effect on session negotiation until Open vSwtich supports
1312 * wire-protocol versions greater than 31.
1314 allowed_versions = ntohl(bitmap[0]);
1316 if (allowed_versions & 1) {
1317 /* There's no OpenFlow version 0. */
1318 VLOG_WARN_RL(&bad_ofmsg_rl, "peer claims to support invalid OpenFlow "
1320 allowed_versions &= ~1u;
1323 if (!allowed_versions) {
1324 VLOG_WARN_RL(&bad_ofmsg_rl, "peer does not support any OpenFlow "
1325 "version (between 0x01 and 0x1f)");
1329 *allowed_versionsp = allowed_versions;
1334 version_bitmap_from_version(uint8_t ofp_version)
1336 return ((ofp_version < 32 ? 1u << ofp_version : 0) - 1) << 1;
1339 /* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions'
1340 * the set of OpenFlow versions for which 'oh' announces support.
1342 * Because of how OpenFlow defines OFPT_HELLO messages, this function is always
1343 * successful, and thus '*allowed_versions' is always initialized. However, it
1344 * returns false if 'oh' contains some data that could not be fully understood,
1345 * true if 'oh' was completely parsed. */
1347 ofputil_decode_hello(const struct ofp_header *oh, uint32_t *allowed_versions)
1352 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
1353 ofpbuf_pull(&msg, sizeof *oh);
1355 *allowed_versions = version_bitmap_from_version(oh->version);
1356 while (ofpbuf_size(&msg)) {
1357 const struct ofp_hello_elem_header *oheh;
1360 if (ofpbuf_size(&msg) < sizeof *oheh) {
1364 oheh = ofpbuf_data(&msg);
1365 len = ntohs(oheh->length);
1366 if (len < sizeof *oheh || !ofpbuf_try_pull(&msg, ROUND_UP(len, 8))) {
1370 if (oheh->type != htons(OFPHET_VERSIONBITMAP)
1371 || !ofputil_decode_hello_bitmap(oheh, allowed_versions)) {
1379 /* Returns true if 'allowed_versions' needs to be accompanied by a version
1380 * bitmap to be correctly expressed in an OFPT_HELLO message. */
1382 should_send_version_bitmap(uint32_t allowed_versions)
1384 return !is_pow2((allowed_versions >> 1) + 1);
1387 /* Create an OFPT_HELLO message that expresses support for the OpenFlow
1388 * versions in the 'allowed_versions' bitmaps and returns the message. */
1390 ofputil_encode_hello(uint32_t allowed_versions)
1392 enum ofp_version ofp_version;
1395 ofp_version = leftmost_1bit_idx(allowed_versions);
1396 msg = ofpraw_alloc(OFPRAW_OFPT_HELLO, ofp_version, 0);
1398 if (should_send_version_bitmap(allowed_versions)) {
1399 struct ofp_hello_elem_header *oheh;
1402 map_len = sizeof allowed_versions;
1403 oheh = ofpbuf_put_zeros(msg, ROUND_UP(map_len + sizeof *oheh, 8));
1404 oheh->type = htons(OFPHET_VERSIONBITMAP);
1405 oheh->length = htons(map_len + sizeof *oheh);
1406 *ALIGNED_CAST(ovs_be32 *, oheh + 1) = htonl(allowed_versions);
1408 ofpmsg_update_length(msg);
1414 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1415 * protocol is 'current', at least partly transitions the protocol to 'want'.
1416 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1417 * connection if the switch processes the returned message correctly. (If
1418 * '*next != want' then the caller will have to iterate.)
1420 * If 'current == want', or if it is not possible to transition from 'current'
1421 * to 'want' (because, for example, 'current' and 'want' use different OpenFlow
1422 * protocol versions), returns NULL and stores 'current' in '*next'. */
1424 ofputil_encode_set_protocol(enum ofputil_protocol current,
1425 enum ofputil_protocol want,
1426 enum ofputil_protocol *next)
1428 enum ofp_version cur_version, want_version;
1429 enum ofputil_protocol cur_base, want_base;
1430 bool cur_tid, want_tid;
1432 cur_version = ofputil_protocol_to_ofp_version(current);
1433 want_version = ofputil_protocol_to_ofp_version(want);
1434 if (cur_version != want_version) {
1439 cur_base = ofputil_protocol_to_base(current);
1440 want_base = ofputil_protocol_to_base(want);
1441 if (cur_base != want_base) {
1442 *next = ofputil_protocol_set_base(current, want_base);
1444 switch (want_base) {
1445 case OFPUTIL_P_OF10_NXM:
1446 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1448 case OFPUTIL_P_OF10_STD:
1449 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1451 case OFPUTIL_P_OF11_STD:
1452 case OFPUTIL_P_OF12_OXM:
1453 case OFPUTIL_P_OF13_OXM:
1454 case OFPUTIL_P_OF14_OXM:
1455 case OFPUTIL_P_OF15_OXM:
1456 /* There is only one variant of each OpenFlow 1.1+ protocol, and we
1457 * verified above that we're not trying to change versions. */
1460 case OFPUTIL_P_OF10_STD_TID:
1461 case OFPUTIL_P_OF10_NXM_TID:
1466 cur_tid = (current & OFPUTIL_P_TID) != 0;
1467 want_tid = (want & OFPUTIL_P_TID) != 0;
1468 if (cur_tid != want_tid) {
1469 *next = ofputil_protocol_set_tid(current, want_tid);
1470 return ofputil_make_flow_mod_table_id(want_tid);
1473 ovs_assert(current == want);
1479 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1480 * format to 'nxff'. */
1482 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1484 struct nx_set_flow_format *sff;
1487 ovs_assert(ofputil_nx_flow_format_is_valid(nxff));
1489 msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1490 sff = ofpbuf_put_zeros(msg, sizeof *sff);
1491 sff->format = htonl(nxff);
1496 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1498 enum ofputil_protocol
1499 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1501 switch (flow_format) {
1502 case NXFF_OPENFLOW10:
1503 return OFPUTIL_P_OF10_STD;
1506 return OFPUTIL_P_OF10_NXM;
1513 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1515 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1517 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1520 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1523 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1525 switch (flow_format) {
1526 case NXFF_OPENFLOW10:
1527 return "openflow10";
1536 ofputil_make_set_packet_in_format(enum ofp_version ofp_version,
1537 enum nx_packet_in_format packet_in_format)
1539 struct nx_set_packet_in_format *spif;
1542 msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, ofp_version, 0);
1543 spif = ofpbuf_put_zeros(msg, sizeof *spif);
1544 spif->format = htonl(packet_in_format);
1549 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1550 * extension on or off (according to 'flow_mod_table_id'). */
1552 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1554 struct nx_flow_mod_table_id *nfmti;
1557 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1558 nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
1559 nfmti->set = flow_mod_table_id;
1563 struct ofputil_flow_mod_flag {
1565 enum ofp_version min_version, max_version;
1566 enum ofputil_flow_mod_flags flag;
1569 static const struct ofputil_flow_mod_flag ofputil_flow_mod_flags[] = {
1570 { OFPFF_SEND_FLOW_REM, OFP10_VERSION, 0, OFPUTIL_FF_SEND_FLOW_REM },
1571 { OFPFF_CHECK_OVERLAP, OFP10_VERSION, 0, OFPUTIL_FF_CHECK_OVERLAP },
1572 { OFPFF10_EMERG, OFP10_VERSION, OFP10_VERSION,
1574 { OFPFF12_RESET_COUNTS, OFP12_VERSION, 0, OFPUTIL_FF_RESET_COUNTS },
1575 { OFPFF13_NO_PKT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_PKT_COUNTS },
1576 { OFPFF13_NO_BYT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_BYT_COUNTS },
1581 ofputil_decode_flow_mod_flags(ovs_be16 raw_flags_,
1582 enum ofp_flow_mod_command command,
1583 enum ofp_version version,
1584 enum ofputil_flow_mod_flags *flagsp)
1586 uint16_t raw_flags = ntohs(raw_flags_);
1587 const struct ofputil_flow_mod_flag *f;
1590 for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1591 if (raw_flags & f->raw_flag
1592 && version >= f->min_version
1593 && (!f->max_version || version <= f->max_version)) {
1594 raw_flags &= ~f->raw_flag;
1599 /* In OF1.0 and OF1.1, "add" always resets counters, and other commands
1602 * In OF1.2 and later, OFPFF12_RESET_COUNTS controls whether each command
1603 * resets counters. */
1604 if ((version == OFP10_VERSION || version == OFP11_VERSION)
1605 && command == OFPFC_ADD) {
1606 *flagsp |= OFPUTIL_FF_RESET_COUNTS;
1609 return raw_flags ? OFPERR_OFPFMFC_BAD_FLAGS : 0;
1613 ofputil_encode_flow_mod_flags(enum ofputil_flow_mod_flags flags,
1614 enum ofp_version version)
1616 const struct ofputil_flow_mod_flag *f;
1620 for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1622 && version >= f->min_version
1623 && (!f->max_version || version <= f->max_version)) {
1624 raw_flags |= f->raw_flag;
1628 return htons(raw_flags);
1631 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1632 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1635 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1636 * The caller must initialize 'ofpacts' and retains ownership of it.
1637 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1639 * Does not validate the flow_mod actions. The caller should do that, with
1640 * ofpacts_check(). */
1642 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1643 const struct ofp_header *oh,
1644 enum ofputil_protocol protocol,
1645 struct ofpbuf *ofpacts,
1646 ofp_port_t max_port, uint8_t max_table)
1653 /* Ignored for non-delete actions */
1654 fm->delete_reason = OFPRR_DELETE;
1656 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1657 raw = ofpraw_pull_assert(&b);
1658 if (raw == OFPRAW_OFPT11_FLOW_MOD) {
1659 /* Standard OpenFlow 1.1+ flow_mod. */
1660 const struct ofp11_flow_mod *ofm;
1662 ofm = ofpbuf_pull(&b, sizeof *ofm);
1664 error = ofputil_pull_ofp11_match(&b, &fm->match, NULL);
1669 /* Translate the message. */
1670 fm->priority = ntohs(ofm->priority);
1671 if (ofm->command == OFPFC_ADD
1672 || (oh->version == OFP11_VERSION
1673 && (ofm->command == OFPFC_MODIFY ||
1674 ofm->command == OFPFC_MODIFY_STRICT)
1675 && ofm->cookie_mask == htonll(0))) {
1676 /* In OpenFlow 1.1 only, a "modify" or "modify-strict" that does
1677 * not match on the cookie is treated as an "add" if there is no
1679 fm->cookie = htonll(0);
1680 fm->cookie_mask = htonll(0);
1681 fm->new_cookie = ofm->cookie;
1683 fm->cookie = ofm->cookie;
1684 fm->cookie_mask = ofm->cookie_mask;
1685 fm->new_cookie = OVS_BE64_MAX;
1687 fm->modify_cookie = false;
1688 fm->command = ofm->command;
1692 * OF1.1 entirely forbids table_id == OFPTT_ALL.
1693 * OF1.2+ allows table_id == OFPTT_ALL only for deletes. */
1694 fm->table_id = ofm->table_id;
1695 if (fm->table_id == OFPTT_ALL
1696 && (oh->version == OFP11_VERSION
1697 || (ofm->command != OFPFC_DELETE &&
1698 ofm->command != OFPFC_DELETE_STRICT))) {
1699 return OFPERR_OFPFMFC_BAD_TABLE_ID;
1702 fm->idle_timeout = ntohs(ofm->idle_timeout);
1703 fm->hard_timeout = ntohs(ofm->hard_timeout);
1704 if (oh->version >= OFP14_VERSION && ofm->command == OFPFC_ADD) {
1705 fm->importance = ntohs(ofm->importance);
1709 fm->buffer_id = ntohl(ofm->buffer_id);
1710 error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
1715 fm->out_group = (ofm->command == OFPFC_DELETE ||
1716 ofm->command == OFPFC_DELETE_STRICT
1717 ? ntohl(ofm->out_group)
1719 raw_flags = ofm->flags;
1723 if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1724 /* Standard OpenFlow 1.0 flow_mod. */
1725 const struct ofp10_flow_mod *ofm;
1727 /* Get the ofp10_flow_mod. */
1728 ofm = ofpbuf_pull(&b, sizeof *ofm);
1730 /* Translate the rule. */
1731 ofputil_match_from_ofp10_match(&ofm->match, &fm->match);
1732 ofputil_normalize_match(&fm->match);
1734 /* OpenFlow 1.0 says that exact-match rules have to have the
1735 * highest possible priority. */
1736 fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL)
1737 ? ntohs(ofm->priority)
1740 /* Translate the message. */
1741 command = ntohs(ofm->command);
1742 fm->cookie = htonll(0);
1743 fm->cookie_mask = htonll(0);
1744 fm->new_cookie = ofm->cookie;
1745 fm->idle_timeout = ntohs(ofm->idle_timeout);
1746 fm->hard_timeout = ntohs(ofm->hard_timeout);
1748 fm->buffer_id = ntohl(ofm->buffer_id);
1749 fm->out_port = u16_to_ofp(ntohs(ofm->out_port));
1750 fm->out_group = OFPG11_ANY;
1751 raw_flags = ofm->flags;
1752 } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1753 /* Nicira extended flow_mod. */
1754 const struct nx_flow_mod *nfm;
1756 /* Dissect the message. */
1757 nfm = ofpbuf_pull(&b, sizeof *nfm);
1758 error = nx_pull_match(&b, ntohs(nfm->match_len),
1759 &fm->match, &fm->cookie, &fm->cookie_mask);
1764 /* Translate the message. */
1765 command = ntohs(nfm->command);
1766 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1767 /* Flow additions may only set a new cookie, not match an
1768 * existing cookie. */
1769 return OFPERR_NXBRC_NXM_INVALID;
1771 fm->priority = ntohs(nfm->priority);
1772 fm->new_cookie = nfm->cookie;
1773 fm->idle_timeout = ntohs(nfm->idle_timeout);
1774 fm->hard_timeout = ntohs(nfm->hard_timeout);
1776 fm->buffer_id = ntohl(nfm->buffer_id);
1777 fm->out_port = u16_to_ofp(ntohs(nfm->out_port));
1778 fm->out_group = OFPG11_ANY;
1779 raw_flags = nfm->flags;
1784 fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX;
1785 if (protocol & OFPUTIL_P_TID) {
1786 fm->command = command & 0xff;
1787 fm->table_id = command >> 8;
1789 fm->command = command;
1790 fm->table_id = 0xff;
1794 error = ofpacts_pull_openflow_instructions(&b, ofpbuf_size(&b),
1795 oh->version, ofpacts);
1799 fm->ofpacts = ofpbuf_data(ofpacts);
1800 fm->ofpacts_len = ofpbuf_size(ofpacts);
1802 error = ofputil_decode_flow_mod_flags(raw_flags, fm->command,
1803 oh->version, &fm->flags);
1808 if (fm->flags & OFPUTIL_FF_EMERG) {
1809 /* We do not support the OpenFlow 1.0 emergency flow cache, which
1810 * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
1812 * OpenFlow 1.0 specifies the error code to use when idle_timeout
1813 * or hard_timeout is nonzero. Otherwise, there is no good error
1814 * code, so just state that the flow table is full. */
1815 return (fm->hard_timeout || fm->idle_timeout
1816 ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT
1817 : OFPERR_OFPFMFC_TABLE_FULL);
1820 return ofpacts_check_consistency(fm->ofpacts, fm->ofpacts_len,
1821 &fm->match.flow, max_port,
1822 fm->table_id, max_table, protocol);
1826 ofputil_pull_bands(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
1827 struct ofpbuf *bands)
1829 const struct ofp13_meter_band_header *ombh;
1830 struct ofputil_meter_band *mb;
1833 ombh = ofpbuf_try_pull(msg, len);
1835 return OFPERR_OFPBRC_BAD_LEN;
1838 while (len >= sizeof (struct ofp13_meter_band_drop)) {
1839 size_t ombh_len = ntohs(ombh->len);
1840 /* All supported band types have the same length. */
1841 if (ombh_len != sizeof (struct ofp13_meter_band_drop)) {
1842 return OFPERR_OFPBRC_BAD_LEN;
1844 mb = ofpbuf_put_uninit(bands, sizeof *mb);
1845 mb->type = ntohs(ombh->type);
1846 if (mb->type != OFPMBT13_DROP && mb->type != OFPMBT13_DSCP_REMARK) {
1847 return OFPERR_OFPMMFC_BAD_BAND;
1849 mb->rate = ntohl(ombh->rate);
1850 mb->burst_size = ntohl(ombh->burst_size);
1851 mb->prec_level = (mb->type == OFPMBT13_DSCP_REMARK) ?
1852 ((struct ofp13_meter_band_dscp_remark *)ombh)->prec_level : 0;
1855 ombh = ALIGNED_CAST(struct ofp13_meter_band_header *,
1856 (char *) ombh + ombh_len);
1859 return OFPERR_OFPBRC_BAD_LEN;
1866 ofputil_decode_meter_mod(const struct ofp_header *oh,
1867 struct ofputil_meter_mod *mm,
1868 struct ofpbuf *bands)
1870 const struct ofp13_meter_mod *omm;
1873 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1874 ofpraw_pull_assert(&b);
1875 omm = ofpbuf_pull(&b, sizeof *omm);
1877 /* Translate the message. */
1878 mm->command = ntohs(omm->command);
1879 if (mm->command != OFPMC13_ADD &&
1880 mm->command != OFPMC13_MODIFY &&
1881 mm->command != OFPMC13_DELETE) {
1882 return OFPERR_OFPMMFC_BAD_COMMAND;
1884 mm->meter.meter_id = ntohl(omm->meter_id);
1886 if (mm->command == OFPMC13_DELETE) {
1887 mm->meter.flags = 0;
1888 mm->meter.n_bands = 0;
1889 mm->meter.bands = NULL;
1893 mm->meter.flags = ntohs(omm->flags);
1894 if (mm->meter.flags & OFPMF13_KBPS &&
1895 mm->meter.flags & OFPMF13_PKTPS) {
1896 return OFPERR_OFPMMFC_BAD_FLAGS;
1898 mm->meter.bands = ofpbuf_data(bands);
1900 error = ofputil_pull_bands(&b, ofpbuf_size(&b), &mm->meter.n_bands, bands);
1909 ofputil_decode_meter_request(const struct ofp_header *oh, uint32_t *meter_id)
1911 const struct ofp13_meter_multipart_request *omr = ofpmsg_body(oh);
1912 *meter_id = ntohl(omr->meter_id);
1916 ofputil_encode_meter_request(enum ofp_version ofp_version,
1917 enum ofputil_meter_request_type type,
1925 case OFPUTIL_METER_CONFIG:
1926 raw = OFPRAW_OFPST13_METER_CONFIG_REQUEST;
1928 case OFPUTIL_METER_STATS:
1929 raw = OFPRAW_OFPST13_METER_REQUEST;
1932 case OFPUTIL_METER_FEATURES:
1933 raw = OFPRAW_OFPST13_METER_FEATURES_REQUEST;
1937 msg = ofpraw_alloc(raw, ofp_version, 0);
1939 if (type != OFPUTIL_METER_FEATURES) {
1940 struct ofp13_meter_multipart_request *omr;
1941 omr = ofpbuf_put_zeros(msg, sizeof *omr);
1942 omr->meter_id = htonl(meter_id);
1948 ofputil_put_bands(uint16_t n_bands, const struct ofputil_meter_band *mb,
1953 for (n = 0; n < n_bands; ++n) {
1954 /* Currently all band types have same size. */
1955 struct ofp13_meter_band_dscp_remark *ombh;
1956 size_t ombh_len = sizeof *ombh;
1958 ombh = ofpbuf_put_zeros(msg, ombh_len);
1960 ombh->type = htons(mb->type);
1961 ombh->len = htons(ombh_len);
1962 ombh->rate = htonl(mb->rate);
1963 ombh->burst_size = htonl(mb->burst_size);
1964 ombh->prec_level = mb->prec_level;
1970 /* Encode a meter stat for 'mc' and append it to 'replies'. */
1972 ofputil_append_meter_config(struct ovs_list *replies,
1973 const struct ofputil_meter_config *mc)
1975 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
1976 size_t start_ofs = ofpbuf_size(msg);
1977 struct ofp13_meter_config *reply = ofpbuf_put_uninit(msg, sizeof *reply);
1978 reply->flags = htons(mc->flags);
1979 reply->meter_id = htonl(mc->meter_id);
1981 ofputil_put_bands(mc->n_bands, mc->bands, msg);
1983 reply->length = htons(ofpbuf_size(msg) - start_ofs);
1985 ofpmp_postappend(replies, start_ofs);
1988 /* Encode a meter stat for 'ms' and append it to 'replies'. */
1990 ofputil_append_meter_stats(struct ovs_list *replies,
1991 const struct ofputil_meter_stats *ms)
1993 struct ofp13_meter_stats *reply;
1997 len = sizeof *reply + ms->n_bands * sizeof(struct ofp13_meter_band_stats);
1998 reply = ofpmp_append(replies, len);
2000 reply->meter_id = htonl(ms->meter_id);
2001 reply->len = htons(len);
2002 memset(reply->pad, 0, sizeof reply->pad);
2003 reply->flow_count = htonl(ms->flow_count);
2004 reply->packet_in_count = htonll(ms->packet_in_count);
2005 reply->byte_in_count = htonll(ms->byte_in_count);
2006 reply->duration_sec = htonl(ms->duration_sec);
2007 reply->duration_nsec = htonl(ms->duration_nsec);
2009 for (n = 0; n < ms->n_bands; ++n) {
2010 const struct ofputil_meter_band_stats *src = &ms->bands[n];
2011 struct ofp13_meter_band_stats *dst = &reply->band_stats[n];
2013 dst->packet_band_count = htonll(src->packet_count);
2014 dst->byte_band_count = htonll(src->byte_count);
2018 /* Converts an OFPMP_METER_CONFIG reply in 'msg' into an abstract
2019 * ofputil_meter_config in 'mc', with mc->bands pointing to bands decoded into
2020 * 'bands'. The caller must have initialized 'bands' and retains ownership of
2021 * it across the call.
2023 * Multiple OFPST13_METER_CONFIG replies can be packed into a single OpenFlow
2024 * message. Calling this function multiple times for a single 'msg' iterates
2025 * through the replies. 'bands' is cleared for each reply.
2027 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2028 * otherwise a positive errno value. */
2030 ofputil_decode_meter_config(struct ofpbuf *msg,
2031 struct ofputil_meter_config *mc,
2032 struct ofpbuf *bands)
2034 const struct ofp13_meter_config *omc;
2037 /* Pull OpenFlow headers for the first call. */
2039 ofpraw_pull_assert(msg);
2042 if (!ofpbuf_size(msg)) {
2046 omc = ofpbuf_try_pull(msg, sizeof *omc);
2048 VLOG_WARN_RL(&bad_ofmsg_rl,
2049 "OFPMP_METER_CONFIG reply has %"PRIu32" leftover bytes at end",
2051 return OFPERR_OFPBRC_BAD_LEN;
2054 ofpbuf_clear(bands);
2055 err = ofputil_pull_bands(msg, ntohs(omc->length) - sizeof *omc,
2056 &mc->n_bands, bands);
2060 mc->meter_id = ntohl(omc->meter_id);
2061 mc->flags = ntohs(omc->flags);
2062 mc->bands = ofpbuf_data(bands);
2068 ofputil_pull_band_stats(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
2069 struct ofpbuf *bands)
2071 const struct ofp13_meter_band_stats *ombs;
2072 struct ofputil_meter_band_stats *mbs;
2075 ombs = ofpbuf_try_pull(msg, len);
2077 return OFPERR_OFPBRC_BAD_LEN;
2080 n = len / sizeof *ombs;
2081 if (len != n * sizeof *ombs) {
2082 return OFPERR_OFPBRC_BAD_LEN;
2085 mbs = ofpbuf_put_uninit(bands, len);
2087 for (i = 0; i < n; ++i) {
2088 mbs[i].packet_count = ntohll(ombs[i].packet_band_count);
2089 mbs[i].byte_count = ntohll(ombs[i].byte_band_count);
2095 /* Converts an OFPMP_METER reply in 'msg' into an abstract
2096 * ofputil_meter_stats in 'ms', with ms->bands pointing to band stats
2097 * decoded into 'bands'.
2099 * Multiple OFPMP_METER replies can be packed into a single OpenFlow
2100 * message. Calling this function multiple times for a single 'msg' iterates
2101 * through the replies. 'bands' is cleared for each reply.
2103 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2104 * otherwise a positive errno value. */
2106 ofputil_decode_meter_stats(struct ofpbuf *msg,
2107 struct ofputil_meter_stats *ms,
2108 struct ofpbuf *bands)
2110 const struct ofp13_meter_stats *oms;
2113 /* Pull OpenFlow headers for the first call. */
2115 ofpraw_pull_assert(msg);
2118 if (!ofpbuf_size(msg)) {
2122 oms = ofpbuf_try_pull(msg, sizeof *oms);
2124 VLOG_WARN_RL(&bad_ofmsg_rl,
2125 "OFPMP_METER reply has %"PRIu32" leftover bytes at end",
2127 return OFPERR_OFPBRC_BAD_LEN;
2130 ofpbuf_clear(bands);
2131 err = ofputil_pull_band_stats(msg, ntohs(oms->len) - sizeof *oms,
2132 &ms->n_bands, bands);
2136 ms->meter_id = ntohl(oms->meter_id);
2137 ms->flow_count = ntohl(oms->flow_count);
2138 ms->packet_in_count = ntohll(oms->packet_in_count);
2139 ms->byte_in_count = ntohll(oms->byte_in_count);
2140 ms->duration_sec = ntohl(oms->duration_sec);
2141 ms->duration_nsec = ntohl(oms->duration_nsec);
2142 ms->bands = ofpbuf_data(bands);
2148 ofputil_decode_meter_features(const struct ofp_header *oh,
2149 struct ofputil_meter_features *mf)
2151 const struct ofp13_meter_features *omf = ofpmsg_body(oh);
2153 mf->max_meters = ntohl(omf->max_meter);
2154 mf->band_types = ntohl(omf->band_types);
2155 mf->capabilities = ntohl(omf->capabilities);
2156 mf->max_bands = omf->max_bands;
2157 mf->max_color = omf->max_color;
2161 ofputil_encode_meter_features_reply(const struct ofputil_meter_features *mf,
2162 const struct ofp_header *request)
2164 struct ofpbuf *reply;
2165 struct ofp13_meter_features *omf;
2167 reply = ofpraw_alloc_stats_reply(request, 0);
2168 omf = ofpbuf_put_zeros(reply, sizeof *omf);
2170 omf->max_meter = htonl(mf->max_meters);
2171 omf->band_types = htonl(mf->band_types);
2172 omf->capabilities = htonl(mf->capabilities);
2173 omf->max_bands = mf->max_bands;
2174 omf->max_color = mf->max_color;
2180 ofputil_encode_meter_mod(enum ofp_version ofp_version,
2181 const struct ofputil_meter_mod *mm)
2185 struct ofp13_meter_mod *omm;
2187 msg = ofpraw_alloc(OFPRAW_OFPT13_METER_MOD, ofp_version,
2188 NXM_TYPICAL_LEN + mm->meter.n_bands * 16);
2189 omm = ofpbuf_put_zeros(msg, sizeof *omm);
2190 omm->command = htons(mm->command);
2191 if (mm->command != OFPMC13_DELETE) {
2192 omm->flags = htons(mm->meter.flags);
2194 omm->meter_id = htonl(mm->meter.meter_id);
2196 ofputil_put_bands(mm->meter.n_bands, mm->meter.bands, msg);
2198 ofpmsg_update_length(msg);
2203 ofputil_tid_command(const struct ofputil_flow_mod *fm,
2204 enum ofputil_protocol protocol)
2206 return htons(protocol & OFPUTIL_P_TID
2207 ? (fm->command & 0xff) | (fm->table_id << 8)
2211 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
2212 * 'protocol' and returns the message. */
2214 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
2215 enum ofputil_protocol protocol)
2217 enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
2218 ovs_be16 raw_flags = ofputil_encode_flow_mod_flags(fm->flags, version);
2222 case OFPUTIL_P_OF11_STD:
2223 case OFPUTIL_P_OF12_OXM:
2224 case OFPUTIL_P_OF13_OXM:
2225 case OFPUTIL_P_OF14_OXM:
2226 case OFPUTIL_P_OF15_OXM: {
2227 struct ofp11_flow_mod *ofm;
2230 tailroom = ofputil_match_typical_len(protocol) + fm->ofpacts_len;
2231 msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, version, tailroom);
2232 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
2233 if ((protocol == OFPUTIL_P_OF11_STD
2234 && (fm->command == OFPFC_MODIFY ||
2235 fm->command == OFPFC_MODIFY_STRICT)
2236 && fm->cookie_mask == htonll(0))
2237 || fm->command == OFPFC_ADD) {
2238 ofm->cookie = fm->new_cookie;
2240 ofm->cookie = fm->cookie & fm->cookie_mask;
2242 ofm->cookie_mask = fm->cookie_mask;
2243 if (fm->table_id != OFPTT_ALL
2244 || (protocol != OFPUTIL_P_OF11_STD
2245 && (fm->command == OFPFC_DELETE ||
2246 fm->command == OFPFC_DELETE_STRICT))) {
2247 ofm->table_id = fm->table_id;
2251 ofm->command = fm->command;
2252 ofm->idle_timeout = htons(fm->idle_timeout);
2253 ofm->hard_timeout = htons(fm->hard_timeout);
2254 ofm->priority = htons(fm->priority);
2255 ofm->buffer_id = htonl(fm->buffer_id);
2256 ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
2257 ofm->out_group = htonl(fm->out_group);
2258 ofm->flags = raw_flags;
2259 if (version >= OFP14_VERSION && fm->command == OFPFC_ADD) {
2260 ofm->importance = htons(fm->importance);
2262 ofm->importance = 0;
2264 ofputil_put_ofp11_match(msg, &fm->match, protocol);
2265 ofpacts_put_openflow_instructions(fm->ofpacts, fm->ofpacts_len, msg,
2270 case OFPUTIL_P_OF10_STD:
2271 case OFPUTIL_P_OF10_STD_TID: {
2272 struct ofp10_flow_mod *ofm;
2274 msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
2276 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
2277 ofputil_match_to_ofp10_match(&fm->match, &ofm->match);
2278 ofm->cookie = fm->new_cookie;
2279 ofm->command = ofputil_tid_command(fm, protocol);
2280 ofm->idle_timeout = htons(fm->idle_timeout);
2281 ofm->hard_timeout = htons(fm->hard_timeout);
2282 ofm->priority = htons(fm->priority);
2283 ofm->buffer_id = htonl(fm->buffer_id);
2284 ofm->out_port = htons(ofp_to_u16(fm->out_port));
2285 ofm->flags = raw_flags;
2286 ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2291 case OFPUTIL_P_OF10_NXM:
2292 case OFPUTIL_P_OF10_NXM_TID: {
2293 struct nx_flow_mod *nfm;
2296 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
2297 NXM_TYPICAL_LEN + fm->ofpacts_len);
2298 nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
2299 nfm->command = ofputil_tid_command(fm, protocol);
2300 nfm->cookie = fm->new_cookie;
2301 match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask);
2302 nfm = ofpbuf_l3(msg);
2303 nfm->idle_timeout = htons(fm->idle_timeout);
2304 nfm->hard_timeout = htons(fm->hard_timeout);
2305 nfm->priority = htons(fm->priority);
2306 nfm->buffer_id = htonl(fm->buffer_id);
2307 nfm->out_port = htons(ofp_to_u16(fm->out_port));
2308 nfm->flags = raw_flags;
2309 nfm->match_len = htons(match_len);
2310 ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2319 ofpmsg_update_length(msg);
2324 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr,
2325 const struct ofp10_flow_stats_request *ofsr,
2328 fsr->aggregate = aggregate;
2329 ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match);
2330 fsr->out_port = u16_to_ofp(ntohs(ofsr->out_port));
2331 fsr->out_group = OFPG11_ANY;
2332 fsr->table_id = ofsr->table_id;
2333 fsr->cookie = fsr->cookie_mask = htonll(0);
2339 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr,
2340 struct ofpbuf *b, bool aggregate)
2342 const struct ofp11_flow_stats_request *ofsr;
2345 ofsr = ofpbuf_pull(b, sizeof *ofsr);
2346 fsr->aggregate = aggregate;
2347 fsr->table_id = ofsr->table_id;
2348 error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port);
2352 fsr->out_group = ntohl(ofsr->out_group);
2353 fsr->cookie = ofsr->cookie;
2354 fsr->cookie_mask = ofsr->cookie_mask;
2355 error = ofputil_pull_ofp11_match(b, &fsr->match, NULL);
2364 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
2365 struct ofpbuf *b, bool aggregate)
2367 const struct nx_flow_stats_request *nfsr;
2370 nfsr = ofpbuf_pull(b, sizeof *nfsr);
2371 error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match,
2372 &fsr->cookie, &fsr->cookie_mask);
2376 if (ofpbuf_size(b)) {
2377 return OFPERR_OFPBRC_BAD_LEN;
2380 fsr->aggregate = aggregate;
2381 fsr->out_port = u16_to_ofp(ntohs(nfsr->out_port));
2382 fsr->out_group = OFPG11_ANY;
2383 fsr->table_id = nfsr->table_id;
2388 /* Constructs and returns an OFPT_QUEUE_GET_CONFIG request for the specified
2389 * 'port', suitable for OpenFlow version 'version'. */
2391 ofputil_encode_queue_get_config_request(enum ofp_version version,
2394 struct ofpbuf *request;
2396 if (version == OFP10_VERSION) {
2397 struct ofp10_queue_get_config_request *qgcr10;
2399 request = ofpraw_alloc(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST,
2401 qgcr10 = ofpbuf_put_zeros(request, sizeof *qgcr10);
2402 qgcr10->port = htons(ofp_to_u16(port));
2404 struct ofp11_queue_get_config_request *qgcr11;
2406 request = ofpraw_alloc(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST,
2408 qgcr11 = ofpbuf_put_zeros(request, sizeof *qgcr11);
2409 qgcr11->port = ofputil_port_to_ofp11(port);
2415 /* Parses OFPT_QUEUE_GET_CONFIG request 'oh', storing the port specified by the
2416 * request into '*port'. Returns 0 if successful, otherwise an OpenFlow error
2419 ofputil_decode_queue_get_config_request(const struct ofp_header *oh,
2422 const struct ofp10_queue_get_config_request *qgcr10;
2423 const struct ofp11_queue_get_config_request *qgcr11;
2427 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2428 raw = ofpraw_pull_assert(&b);
2430 switch ((int) raw) {
2431 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2432 qgcr10 = ofpbuf_data(&b);
2433 *port = u16_to_ofp(ntohs(qgcr10->port));
2436 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2437 qgcr11 = ofpbuf_data(&b);
2438 return ofputil_port_from_ofp11(qgcr11->port, port);
2444 /* Constructs and returns the beginning of a reply to
2445 * OFPT_QUEUE_GET_CONFIG_REQUEST 'oh'. The caller may append information about
2446 * individual queues with ofputil_append_queue_get_config_reply(). */
2448 ofputil_encode_queue_get_config_reply(const struct ofp_header *oh)
2450 struct ofp10_queue_get_config_reply *qgcr10;
2451 struct ofp11_queue_get_config_reply *qgcr11;
2452 struct ofpbuf *reply;
2458 error = ofputil_decode_queue_get_config_request(oh, &port);
2461 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2462 raw = ofpraw_pull_assert(&b);
2464 switch ((int) raw) {
2465 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2466 reply = ofpraw_alloc_reply(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY,
2468 qgcr10 = ofpbuf_put_zeros(reply, sizeof *qgcr10);
2469 qgcr10->port = htons(ofp_to_u16(port));
2472 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2473 reply = ofpraw_alloc_reply(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY,
2475 qgcr11 = ofpbuf_put_zeros(reply, sizeof *qgcr11);
2476 qgcr11->port = ofputil_port_to_ofp11(port);
2487 put_queue_rate(struct ofpbuf *reply, enum ofp_queue_properties property,
2490 if (rate != UINT16_MAX) {
2491 struct ofp_queue_prop_rate *oqpr;
2493 oqpr = ofpbuf_put_zeros(reply, sizeof *oqpr);
2494 oqpr->prop_header.property = htons(property);
2495 oqpr->prop_header.len = htons(sizeof *oqpr);
2496 oqpr->rate = htons(rate);
2500 /* Appends a queue description for 'queue_id' to the
2501 * OFPT_QUEUE_GET_CONFIG_REPLY already in 'oh'. */
2503 ofputil_append_queue_get_config_reply(struct ofpbuf *reply,
2504 const struct ofputil_queue_config *oqc)
2506 const struct ofp_header *oh = ofpbuf_data(reply);
2507 size_t start_ofs, len_ofs;
2510 start_ofs = ofpbuf_size(reply);
2511 if (oh->version < OFP12_VERSION) {
2512 struct ofp10_packet_queue *opq10;
2514 opq10 = ofpbuf_put_zeros(reply, sizeof *opq10);
2515 opq10->queue_id = htonl(oqc->queue_id);
2516 len_ofs = (char *) &opq10->len - (char *) ofpbuf_data(reply);
2518 struct ofp11_queue_get_config_reply *qgcr11;
2519 struct ofp12_packet_queue *opq12;
2522 qgcr11 = ofpbuf_l3(reply);
2523 port = qgcr11->port;
2525 opq12 = ofpbuf_put_zeros(reply, sizeof *opq12);
2527 opq12->queue_id = htonl(oqc->queue_id);
2528 len_ofs = (char *) &opq12->len - (char *) ofpbuf_data(reply);
2531 put_queue_rate(reply, OFPQT_MIN_RATE, oqc->min_rate);
2532 put_queue_rate(reply, OFPQT_MAX_RATE, oqc->max_rate);
2534 len = ofpbuf_at(reply, len_ofs, sizeof *len);
2535 *len = htons(ofpbuf_size(reply) - start_ofs);
2538 /* Decodes the initial part of an OFPT_QUEUE_GET_CONFIG_REPLY from 'reply' and
2539 * stores in '*port' the port that the reply is about. The caller may call
2540 * ofputil_pull_queue_get_config_reply() to obtain information about individual
2541 * queues included in the reply. Returns 0 if successful, otherwise an
2544 ofputil_decode_queue_get_config_reply(struct ofpbuf *reply, ofp_port_t *port)
2546 const struct ofp10_queue_get_config_reply *qgcr10;
2547 const struct ofp11_queue_get_config_reply *qgcr11;
2550 raw = ofpraw_pull_assert(reply);
2551 switch ((int) raw) {
2552 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY:
2553 qgcr10 = ofpbuf_pull(reply, sizeof *qgcr10);
2554 *port = u16_to_ofp(ntohs(qgcr10->port));
2557 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY:
2558 qgcr11 = ofpbuf_pull(reply, sizeof *qgcr11);
2559 return ofputil_port_from_ofp11(qgcr11->port, port);
2566 parse_queue_rate(const struct ofp_queue_prop_header *hdr, uint16_t *rate)
2568 const struct ofp_queue_prop_rate *oqpr;
2570 if (hdr->len == htons(sizeof *oqpr)) {
2571 oqpr = (const struct ofp_queue_prop_rate *) hdr;
2572 *rate = ntohs(oqpr->rate);
2575 return OFPERR_OFPBRC_BAD_LEN;
2579 /* Decodes information about a queue from the OFPT_QUEUE_GET_CONFIG_REPLY in
2580 * 'reply' and stores it in '*queue'. ofputil_decode_queue_get_config_reply()
2581 * must already have pulled off the main header.
2583 * This function returns EOF if the last queue has already been decoded, 0 if a
2584 * queue was successfully decoded into '*queue', or an ofperr if there was a
2585 * problem decoding 'reply'. */
2587 ofputil_pull_queue_get_config_reply(struct ofpbuf *reply,
2588 struct ofputil_queue_config *queue)
2590 const struct ofp_header *oh;
2591 unsigned int opq_len;
2594 if (!ofpbuf_size(reply)) {
2598 queue->min_rate = UINT16_MAX;
2599 queue->max_rate = UINT16_MAX;
2602 if (oh->version < OFP12_VERSION) {
2603 const struct ofp10_packet_queue *opq10;
2605 opq10 = ofpbuf_try_pull(reply, sizeof *opq10);
2607 return OFPERR_OFPBRC_BAD_LEN;
2609 queue->queue_id = ntohl(opq10->queue_id);
2610 len = ntohs(opq10->len);
2611 opq_len = sizeof *opq10;
2613 const struct ofp12_packet_queue *opq12;
2615 opq12 = ofpbuf_try_pull(reply, sizeof *opq12);
2617 return OFPERR_OFPBRC_BAD_LEN;
2619 queue->queue_id = ntohl(opq12->queue_id);
2620 len = ntohs(opq12->len);
2621 opq_len = sizeof *opq12;
2624 if (len < opq_len || len > ofpbuf_size(reply) + opq_len || len % 8) {
2625 return OFPERR_OFPBRC_BAD_LEN;
2630 const struct ofp_queue_prop_header *hdr;
2631 unsigned int property;
2632 unsigned int prop_len;
2633 enum ofperr error = 0;
2635 hdr = ofpbuf_at_assert(reply, 0, sizeof *hdr);
2636 prop_len = ntohs(hdr->len);
2637 if (prop_len < sizeof *hdr || prop_len > ofpbuf_size(reply) || prop_len % 8) {
2638 return OFPERR_OFPBRC_BAD_LEN;
2641 property = ntohs(hdr->property);
2643 case OFPQT_MIN_RATE:
2644 error = parse_queue_rate(hdr, &queue->min_rate);
2647 case OFPQT_MAX_RATE:
2648 error = parse_queue_rate(hdr, &queue->max_rate);
2652 VLOG_INFO_RL(&bad_ofmsg_rl, "unknown queue property %u", property);
2659 ofpbuf_pull(reply, prop_len);
2665 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
2666 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
2667 * successful, otherwise an OpenFlow error code. */
2669 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
2670 const struct ofp_header *oh)
2675 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2676 raw = ofpraw_pull_assert(&b);
2677 switch ((int) raw) {
2678 case OFPRAW_OFPST10_FLOW_REQUEST:
2679 return ofputil_decode_ofpst10_flow_request(fsr, ofpbuf_data(&b), false);
2681 case OFPRAW_OFPST10_AGGREGATE_REQUEST:
2682 return ofputil_decode_ofpst10_flow_request(fsr, ofpbuf_data(&b), true);
2684 case OFPRAW_OFPST11_FLOW_REQUEST:
2685 return ofputil_decode_ofpst11_flow_request(fsr, &b, false);
2687 case OFPRAW_OFPST11_AGGREGATE_REQUEST:
2688 return ofputil_decode_ofpst11_flow_request(fsr, &b, true);
2690 case OFPRAW_NXST_FLOW_REQUEST:
2691 return ofputil_decode_nxst_flow_request(fsr, &b, false);
2693 case OFPRAW_NXST_AGGREGATE_REQUEST:
2694 return ofputil_decode_nxst_flow_request(fsr, &b, true);
2697 /* Hey, the caller lied. */
2702 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
2703 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
2704 * 'protocol', and returns the message. */
2706 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
2707 enum ofputil_protocol protocol)
2713 case OFPUTIL_P_OF11_STD:
2714 case OFPUTIL_P_OF12_OXM:
2715 case OFPUTIL_P_OF13_OXM:
2716 case OFPUTIL_P_OF14_OXM:
2717 case OFPUTIL_P_OF15_OXM: {
2718 struct ofp11_flow_stats_request *ofsr;
2720 raw = (fsr->aggregate
2721 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
2722 : OFPRAW_OFPST11_FLOW_REQUEST);
2723 msg = ofpraw_alloc(raw, ofputil_protocol_to_ofp_version(protocol),
2724 ofputil_match_typical_len(protocol));
2725 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2726 ofsr->table_id = fsr->table_id;
2727 ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port);
2728 ofsr->out_group = htonl(fsr->out_group);
2729 ofsr->cookie = fsr->cookie;
2730 ofsr->cookie_mask = fsr->cookie_mask;
2731 ofputil_put_ofp11_match(msg, &fsr->match, protocol);
2735 case OFPUTIL_P_OF10_STD:
2736 case OFPUTIL_P_OF10_STD_TID: {
2737 struct ofp10_flow_stats_request *ofsr;
2739 raw = (fsr->aggregate
2740 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
2741 : OFPRAW_OFPST10_FLOW_REQUEST);
2742 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
2743 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2744 ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match);
2745 ofsr->table_id = fsr->table_id;
2746 ofsr->out_port = htons(ofp_to_u16(fsr->out_port));
2750 case OFPUTIL_P_OF10_NXM:
2751 case OFPUTIL_P_OF10_NXM_TID: {
2752 struct nx_flow_stats_request *nfsr;
2755 raw = (fsr->aggregate
2756 ? OFPRAW_NXST_AGGREGATE_REQUEST
2757 : OFPRAW_NXST_FLOW_REQUEST);
2758 msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN);
2759 ofpbuf_put_zeros(msg, sizeof *nfsr);
2760 match_len = nx_put_match(msg, &fsr->match,
2761 fsr->cookie, fsr->cookie_mask);
2763 nfsr = ofpbuf_l3(msg);
2764 nfsr->out_port = htons(ofp_to_u16(fsr->out_port));
2765 nfsr->match_len = htons(match_len);
2766 nfsr->table_id = fsr->table_id;
2777 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
2778 * ofputil_flow_stats in 'fs'.
2780 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
2781 * OpenFlow message. Calling this function multiple times for a single 'msg'
2782 * iterates through the replies. The caller must initially leave 'msg''s layer
2783 * pointers null and not modify them between calls.
2785 * Most switches don't send the values needed to populate fs->idle_age and
2786 * fs->hard_age, so those members will usually be set to 0. If the switch from
2787 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
2788 * 'flow_age_extension' as true so that the contents of 'msg' determine the
2789 * 'idle_age' and 'hard_age' members in 'fs'.
2791 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
2792 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
2793 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
2795 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2796 * otherwise a positive errno value. */
2798 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
2800 bool flow_age_extension,
2801 struct ofpbuf *ofpacts)
2803 const struct ofp_header *oh;
2804 size_t instructions_len;
2809 ? ofpraw_decode(&raw, msg->frame)
2810 : ofpraw_pull(&raw, msg));
2816 if (!ofpbuf_size(msg)) {
2818 } else if (raw == OFPRAW_OFPST11_FLOW_REPLY
2819 || raw == OFPRAW_OFPST13_FLOW_REPLY) {
2820 const struct ofp11_flow_stats *ofs;
2822 uint16_t padded_match_len;
2824 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2826 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
2827 "bytes at end", ofpbuf_size(msg));
2831 length = ntohs(ofs->length);
2832 if (length < sizeof *ofs) {
2833 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2834 "length %"PRIuSIZE, length);
2838 if (ofputil_pull_ofp11_match(msg, &fs->match, &padded_match_len)) {
2839 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match");
2842 instructions_len = length - sizeof *ofs - padded_match_len;
2844 fs->priority = ntohs(ofs->priority);
2845 fs->table_id = ofs->table_id;
2846 fs->duration_sec = ntohl(ofs->duration_sec);
2847 fs->duration_nsec = ntohl(ofs->duration_nsec);
2848 fs->idle_timeout = ntohs(ofs->idle_timeout);
2849 fs->hard_timeout = ntohs(ofs->hard_timeout);
2850 if (oh->version >= OFP14_VERSION) {
2851 fs->importance = ntohs(ofs->importance);
2855 if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
2856 error = ofputil_decode_flow_mod_flags(ofs->flags, -1, oh->version,
2866 fs->cookie = ofs->cookie;
2867 fs->packet_count = ntohll(ofs->packet_count);
2868 fs->byte_count = ntohll(ofs->byte_count);
2869 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
2870 const struct ofp10_flow_stats *ofs;
2873 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2875 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
2876 "bytes at end", ofpbuf_size(msg));
2880 length = ntohs(ofs->length);
2881 if (length < sizeof *ofs) {
2882 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2883 "length %"PRIuSIZE, length);
2886 instructions_len = length - sizeof *ofs;
2888 fs->cookie = get_32aligned_be64(&ofs->cookie);
2889 ofputil_match_from_ofp10_match(&ofs->match, &fs->match);
2890 fs->priority = ntohs(ofs->priority);
2891 fs->table_id = ofs->table_id;
2892 fs->duration_sec = ntohl(ofs->duration_sec);
2893 fs->duration_nsec = ntohl(ofs->duration_nsec);
2894 fs->idle_timeout = ntohs(ofs->idle_timeout);
2895 fs->hard_timeout = ntohs(ofs->hard_timeout);
2899 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
2900 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
2902 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
2903 const struct nx_flow_stats *nfs;
2904 size_t match_len, length;
2906 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
2908 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %"PRIu32" leftover "
2909 "bytes at end", ofpbuf_size(msg));
2913 length = ntohs(nfs->length);
2914 match_len = ntohs(nfs->match_len);
2915 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
2916 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%"PRIuSIZE" "
2917 "claims invalid length %"PRIuSIZE, match_len, length);
2920 if (nx_pull_match(msg, match_len, &fs->match, NULL, NULL)) {
2923 instructions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
2925 fs->cookie = nfs->cookie;
2926 fs->table_id = nfs->table_id;
2927 fs->duration_sec = ntohl(nfs->duration_sec);
2928 fs->duration_nsec = ntohl(nfs->duration_nsec);
2929 fs->priority = ntohs(nfs->priority);
2930 fs->idle_timeout = ntohs(nfs->idle_timeout);
2931 fs->hard_timeout = ntohs(nfs->hard_timeout);
2935 if (flow_age_extension) {
2936 if (nfs->idle_age) {
2937 fs->idle_age = ntohs(nfs->idle_age) - 1;
2939 if (nfs->hard_age) {
2940 fs->hard_age = ntohs(nfs->hard_age) - 1;
2943 fs->packet_count = ntohll(nfs->packet_count);
2944 fs->byte_count = ntohll(nfs->byte_count);
2950 if (ofpacts_pull_openflow_instructions(msg, instructions_len, oh->version,
2952 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions");
2955 fs->ofpacts = ofpbuf_data(ofpacts);
2956 fs->ofpacts_len = ofpbuf_size(ofpacts);
2961 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
2963 * We use this in situations where OVS internally uses UINT64_MAX to mean
2964 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
2966 unknown_to_zero(uint64_t count)
2968 return count != UINT64_MAX ? count : 0;
2971 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
2972 * those already present in the list of ofpbufs in 'replies'. 'replies' should
2973 * have been initialized with ofpmp_init(). */
2975 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
2976 struct ovs_list *replies)
2978 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
2979 size_t start_ofs = ofpbuf_size(reply);
2980 enum ofp_version version = ofpmp_version(replies);
2981 enum ofpraw raw = ofpmp_decode_raw(replies);
2983 if (raw == OFPRAW_OFPST11_FLOW_REPLY || raw == OFPRAW_OFPST13_FLOW_REPLY) {
2984 struct ofp11_flow_stats *ofs;
2986 ofpbuf_put_uninit(reply, sizeof *ofs);
2987 oxm_put_match(reply, &fs->match, version);
2988 ofpacts_put_openflow_instructions(fs->ofpacts, fs->ofpacts_len, reply,
2991 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
2992 ofs->length = htons(ofpbuf_size(reply) - start_ofs);
2993 ofs->table_id = fs->table_id;
2995 ofs->duration_sec = htonl(fs->duration_sec);
2996 ofs->duration_nsec = htonl(fs->duration_nsec);
2997 ofs->priority = htons(fs->priority);
2998 ofs->idle_timeout = htons(fs->idle_timeout);
2999 ofs->hard_timeout = htons(fs->hard_timeout);
3000 if (version >= OFP14_VERSION) {
3001 ofs->importance = htons(fs->importance);
3003 ofs->importance = 0;
3005 if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
3006 ofs->flags = ofputil_encode_flow_mod_flags(fs->flags, version);
3010 memset(ofs->pad2, 0, sizeof ofs->pad2);
3011 ofs->cookie = fs->cookie;
3012 ofs->packet_count = htonll(unknown_to_zero(fs->packet_count));
3013 ofs->byte_count = htonll(unknown_to_zero(fs->byte_count));
3014 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
3015 struct ofp10_flow_stats *ofs;
3017 ofpbuf_put_uninit(reply, sizeof *ofs);
3018 ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
3020 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
3021 ofs->length = htons(ofpbuf_size(reply) - start_ofs);
3022 ofs->table_id = fs->table_id;
3024 ofputil_match_to_ofp10_match(&fs->match, &ofs->match);
3025 ofs->duration_sec = htonl(fs->duration_sec);
3026 ofs->duration_nsec = htonl(fs->duration_nsec);
3027 ofs->priority = htons(fs->priority);
3028 ofs->idle_timeout = htons(fs->idle_timeout);
3029 ofs->hard_timeout = htons(fs->hard_timeout);
3030 memset(ofs->pad2, 0, sizeof ofs->pad2);
3031 put_32aligned_be64(&ofs->cookie, fs->cookie);
3032 put_32aligned_be64(&ofs->packet_count,
3033 htonll(unknown_to_zero(fs->packet_count)));
3034 put_32aligned_be64(&ofs->byte_count,
3035 htonll(unknown_to_zero(fs->byte_count)));
3036 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
3037 struct nx_flow_stats *nfs;
3040 ofpbuf_put_uninit(reply, sizeof *nfs);
3041 match_len = nx_put_match(reply, &fs->match, 0, 0);
3042 ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
3044 nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
3045 nfs->length = htons(ofpbuf_size(reply) - start_ofs);
3046 nfs->table_id = fs->table_id;
3048 nfs->duration_sec = htonl(fs->duration_sec);
3049 nfs->duration_nsec = htonl(fs->duration_nsec);
3050 nfs->priority = htons(fs->priority);
3051 nfs->idle_timeout = htons(fs->idle_timeout);
3052 nfs->hard_timeout = htons(fs->hard_timeout);
3053 nfs->idle_age = htons(fs->idle_age < 0 ? 0
3054 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
3056 nfs->hard_age = htons(fs->hard_age < 0 ? 0
3057 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
3059 nfs->match_len = htons(match_len);
3060 nfs->cookie = fs->cookie;
3061 nfs->packet_count = htonll(fs->packet_count);
3062 nfs->byte_count = htonll(fs->byte_count);
3067 ofpmp_postappend(replies, start_ofs);
3070 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
3071 * NXST_AGGREGATE reply matching 'request', and returns the message. */
3073 ofputil_encode_aggregate_stats_reply(
3074 const struct ofputil_aggregate_stats *stats,
3075 const struct ofp_header *request)
3077 struct ofp_aggregate_stats_reply *asr;
3078 uint64_t packet_count;
3079 uint64_t byte_count;
3083 ofpraw_decode(&raw, request);
3084 if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) {
3085 packet_count = unknown_to_zero(stats->packet_count);
3086 byte_count = unknown_to_zero(stats->byte_count);
3088 packet_count = stats->packet_count;
3089 byte_count = stats->byte_count;
3092 msg = ofpraw_alloc_stats_reply(request, 0);
3093 asr = ofpbuf_put_zeros(msg, sizeof *asr);
3094 put_32aligned_be64(&asr->packet_count, htonll(packet_count));
3095 put_32aligned_be64(&asr->byte_count, htonll(byte_count));
3096 asr->flow_count = htonl(stats->flow_count);
3102 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
3103 const struct ofp_header *reply)
3105 struct ofp_aggregate_stats_reply *asr;
3108 ofpbuf_use_const(&msg, reply, ntohs(reply->length));
3109 ofpraw_pull_assert(&msg);
3111 asr = ofpbuf_l3(&msg);
3112 stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
3113 stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
3114 stats->flow_count = ntohl(asr->flow_count);
3119 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
3120 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
3121 * an OpenFlow error code. */
3123 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
3124 const struct ofp_header *oh)
3129 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3130 raw = ofpraw_pull_assert(&b);
3131 if (raw == OFPRAW_OFPT11_FLOW_REMOVED) {
3132 const struct ofp12_flow_removed *ofr;
3135 ofr = ofpbuf_pull(&b, sizeof *ofr);
3137 error = ofputil_pull_ofp11_match(&b, &fr->match, NULL);
3142 fr->priority = ntohs(ofr->priority);
3143 fr->cookie = ofr->cookie;
3144 fr->reason = ofr->reason;
3145 fr->table_id = ofr->table_id;
3146 fr->duration_sec = ntohl(ofr->duration_sec);
3147 fr->duration_nsec = ntohl(ofr->duration_nsec);
3148 fr->idle_timeout = ntohs(ofr->idle_timeout);
3149 fr->hard_timeout = ntohs(ofr->hard_timeout);
3150 fr->packet_count = ntohll(ofr->packet_count);
3151 fr->byte_count = ntohll(ofr->byte_count);
3152 } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
3153 const struct ofp10_flow_removed *ofr;
3155 ofr = ofpbuf_pull(&b, sizeof *ofr);
3157 ofputil_match_from_ofp10_match(&ofr->match, &fr->match);
3158 fr->priority = ntohs(ofr->priority);
3159 fr->cookie = ofr->cookie;
3160 fr->reason = ofr->reason;
3162 fr->duration_sec = ntohl(ofr->duration_sec);
3163 fr->duration_nsec = ntohl(ofr->duration_nsec);
3164 fr->idle_timeout = ntohs(ofr->idle_timeout);
3165 fr->hard_timeout = 0;
3166 fr->packet_count = ntohll(ofr->packet_count);
3167 fr->byte_count = ntohll(ofr->byte_count);
3168 } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
3169 struct nx_flow_removed *nfr;
3172 nfr = ofpbuf_pull(&b, sizeof *nfr);
3173 error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match,
3178 if (ofpbuf_size(&b)) {
3179 return OFPERR_OFPBRC_BAD_LEN;
3182 fr->priority = ntohs(nfr->priority);
3183 fr->cookie = nfr->cookie;
3184 fr->reason = nfr->reason;
3185 fr->table_id = nfr->table_id ? nfr->table_id - 1 : 255;
3186 fr->duration_sec = ntohl(nfr->duration_sec);
3187 fr->duration_nsec = ntohl(nfr->duration_nsec);
3188 fr->idle_timeout = ntohs(nfr->idle_timeout);
3189 fr->hard_timeout = 0;
3190 fr->packet_count = ntohll(nfr->packet_count);
3191 fr->byte_count = ntohll(nfr->byte_count);
3199 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
3200 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
3203 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
3204 enum ofputil_protocol protocol)
3207 enum ofp_flow_removed_reason reason = fr->reason;
3209 if (reason == OFPRR_METER_DELETE && !(protocol & OFPUTIL_P_OF14_UP)) {
3210 reason = OFPRR_DELETE;
3214 case OFPUTIL_P_OF11_STD:
3215 case OFPUTIL_P_OF12_OXM:
3216 case OFPUTIL_P_OF13_OXM:
3217 case OFPUTIL_P_OF14_OXM:
3218 case OFPUTIL_P_OF15_OXM: {
3219 struct ofp12_flow_removed *ofr;
3221 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED,
3222 ofputil_protocol_to_ofp_version(protocol),
3224 ofputil_match_typical_len(protocol));
3225 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3226 ofr->cookie = fr->cookie;
3227 ofr->priority = htons(fr->priority);
3228 ofr->reason = reason;
3229 ofr->table_id = fr->table_id;
3230 ofr->duration_sec = htonl(fr->duration_sec);
3231 ofr->duration_nsec = htonl(fr->duration_nsec);
3232 ofr->idle_timeout = htons(fr->idle_timeout);
3233 ofr->hard_timeout = htons(fr->hard_timeout);
3234 ofr->packet_count = htonll(fr->packet_count);
3235 ofr->byte_count = htonll(fr->byte_count);
3236 ofputil_put_ofp11_match(msg, &fr->match, protocol);
3240 case OFPUTIL_P_OF10_STD:
3241 case OFPUTIL_P_OF10_STD_TID: {
3242 struct ofp10_flow_removed *ofr;
3244 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
3246 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3247 ofputil_match_to_ofp10_match(&fr->match, &ofr->match);
3248 ofr->cookie = fr->cookie;
3249 ofr->priority = htons(fr->priority);
3250 ofr->reason = reason;
3251 ofr->duration_sec = htonl(fr->duration_sec);
3252 ofr->duration_nsec = htonl(fr->duration_nsec);
3253 ofr->idle_timeout = htons(fr->idle_timeout);
3254 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
3255 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
3259 case OFPUTIL_P_OF10_NXM:
3260 case OFPUTIL_P_OF10_NXM_TID: {
3261 struct nx_flow_removed *nfr;
3264 msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
3265 htonl(0), NXM_TYPICAL_LEN);
3266 ofpbuf_put_zeros(msg, sizeof *nfr);
3267 match_len = nx_put_match(msg, &fr->match, 0, 0);
3269 nfr = ofpbuf_l3(msg);
3270 nfr->cookie = fr->cookie;
3271 nfr->priority = htons(fr->priority);
3272 nfr->reason = reason;
3273 nfr->table_id = fr->table_id + 1;
3274 nfr->duration_sec = htonl(fr->duration_sec);
3275 nfr->duration_nsec = htonl(fr->duration_nsec);
3276 nfr->idle_timeout = htons(fr->idle_timeout);
3277 nfr->match_len = htons(match_len);
3278 nfr->packet_count = htonll(fr->packet_count);
3279 nfr->byte_count = htonll(fr->byte_count);
3291 ofputil_decode_packet_in_finish(struct ofputil_packet_in *pin,
3292 struct match *match, struct ofpbuf *b)
3294 pin->packet = ofpbuf_data(b);
3295 pin->packet_len = ofpbuf_size(b);
3297 pin->fmd.in_port = match->flow.in_port.ofp_port;
3298 pin->fmd.tun_id = match->flow.tunnel.tun_id;
3299 pin->fmd.tun_src = match->flow.tunnel.ip_src;
3300 pin->fmd.tun_dst = match->flow.tunnel.ip_dst;
3301 pin->fmd.gbp_id = match->flow.tunnel.gbp_id;
3302 pin->fmd.gbp_flags = match->flow.tunnel.gbp_flags;
3303 pin->fmd.metadata = match->flow.metadata;
3304 memcpy(pin->fmd.regs, match->flow.regs, sizeof pin->fmd.regs);
3305 pin->fmd.pkt_mark = match->flow.pkt_mark;
3309 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
3310 const struct ofp_header *oh)
3315 memset(pin, 0, sizeof *pin);
3316 pin->cookie = OVS_BE64_MAX;
3318 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3319 raw = ofpraw_pull_assert(&b);
3320 if (raw == OFPRAW_OFPT13_PACKET_IN || raw == OFPRAW_OFPT12_PACKET_IN) {
3321 const struct ofp13_packet_in *opi;
3324 size_t packet_in_size;
3326 if (raw == OFPRAW_OFPT12_PACKET_IN) {
3327 packet_in_size = sizeof (struct ofp12_packet_in);
3329 packet_in_size = sizeof (struct ofp13_packet_in);
3332 opi = ofpbuf_pull(&b, packet_in_size);
3333 error = oxm_pull_match_loose(&b, &match);
3338 if (!ofpbuf_try_pull(&b, 2)) {
3339 return OFPERR_OFPBRC_BAD_LEN;
3342 pin->reason = opi->pi.reason;
3343 pin->table_id = opi->pi.table_id;
3344 pin->buffer_id = ntohl(opi->pi.buffer_id);
3345 pin->total_len = ntohs(opi->pi.total_len);
3347 if (raw == OFPRAW_OFPT13_PACKET_IN) {
3348 pin->cookie = opi->cookie;
3351 ofputil_decode_packet_in_finish(pin, &match, &b);
3352 } else if (raw == OFPRAW_OFPT10_PACKET_IN) {
3353 const struct ofp10_packet_in *opi;
3355 opi = ofpbuf_pull(&b, offsetof(struct ofp10_packet_in, data));
3357 pin->packet = opi->data;
3358 pin->packet_len = ofpbuf_size(&b);
3360 pin->fmd.in_port = u16_to_ofp(ntohs(opi->in_port));
3361 pin->reason = opi->reason;
3362 pin->buffer_id = ntohl(opi->buffer_id);
3363 pin->total_len = ntohs(opi->total_len);
3364 } else if (raw == OFPRAW_OFPT11_PACKET_IN) {
3365 const struct ofp11_packet_in *opi;
3368 opi = ofpbuf_pull(&b, sizeof *opi);
3370 pin->packet = ofpbuf_data(&b);
3371 pin->packet_len = ofpbuf_size(&b);
3373 pin->buffer_id = ntohl(opi->buffer_id);
3374 error = ofputil_port_from_ofp11(opi->in_port, &pin->fmd.in_port);
3378 pin->total_len = ntohs(opi->total_len);
3379 pin->reason = opi->reason;
3380 pin->table_id = opi->table_id;
3381 } else if (raw == OFPRAW_NXT_PACKET_IN) {
3382 const struct nx_packet_in *npi;
3386 npi = ofpbuf_pull(&b, sizeof *npi);
3387 error = nx_pull_match_loose(&b, ntohs(npi->match_len), &match, NULL,
3393 if (!ofpbuf_try_pull(&b, 2)) {
3394 return OFPERR_OFPBRC_BAD_LEN;
3397 pin->reason = npi->reason;
3398 pin->table_id = npi->table_id;
3399 pin->cookie = npi->cookie;
3401 pin->buffer_id = ntohl(npi->buffer_id);
3402 pin->total_len = ntohs(npi->total_len);
3404 ofputil_decode_packet_in_finish(pin, &match, &b);
3413 ofputil_packet_in_to_match(const struct ofputil_packet_in *pin,
3414 struct match *match)
3418 match_init_catchall(match);
3419 if (pin->fmd.tun_id != htonll(0)) {
3420 match_set_tun_id(match, pin->fmd.tun_id);
3422 if (pin->fmd.tun_src != htonl(0)) {
3423 match_set_tun_src(match, pin->fmd.tun_src);
3425 if (pin->fmd.tun_dst != htonl(0)) {
3426 match_set_tun_dst(match, pin->fmd.tun_dst);
3428 if (pin->fmd.gbp_id != htons(0)) {
3429 match_set_tun_gbp_id(match, pin->fmd.gbp_id);
3431 if (pin->fmd.gbp_flags) {
3432 match_set_tun_gbp_flags(match, pin->fmd.gbp_flags);
3434 if (pin->fmd.metadata != htonll(0)) {
3435 match_set_metadata(match, pin->fmd.metadata);
3438 for (i = 0; i < FLOW_N_REGS; i++) {
3439 if (pin->fmd.regs[i]) {
3440 match_set_reg(match, i, pin->fmd.regs[i]);
3444 if (pin->fmd.pkt_mark != 0) {
3445 match_set_pkt_mark(match, pin->fmd.pkt_mark);
3448 match_set_in_port(match, pin->fmd.in_port);
3451 static struct ofpbuf *
3452 ofputil_encode_ofp10_packet_in(const struct ofputil_packet_in *pin)
3454 struct ofp10_packet_in *opi;
3455 struct ofpbuf *packet;
3457 packet = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
3458 htonl(0), pin->packet_len);
3459 opi = ofpbuf_put_zeros(packet, offsetof(struct ofp10_packet_in, data));
3460 opi->total_len = htons(pin->total_len);
3461 opi->in_port = htons(ofp_to_u16(pin->fmd.in_port));
3462 opi->reason = pin->reason;
3463 opi->buffer_id = htonl(pin->buffer_id);
3465 ofpbuf_put(packet, pin->packet, pin->packet_len);
3470 static struct ofpbuf *
3471 ofputil_encode_nx_packet_in(const struct ofputil_packet_in *pin)
3473 struct nx_packet_in *npi;
3474 struct ofpbuf *packet;
3478 ofputil_packet_in_to_match(pin, &match);
3480 /* The final argument is just an estimate of the space required. */
3481 packet = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, OFP10_VERSION,
3482 htonl(0), (sizeof(struct flow_metadata) * 2
3483 + 2 + pin->packet_len));
3484 ofpbuf_put_zeros(packet, sizeof *npi);
3485 match_len = nx_put_match(packet, &match, 0, 0);
3486 ofpbuf_put_zeros(packet, 2);
3487 ofpbuf_put(packet, pin->packet, pin->packet_len);
3489 npi = ofpbuf_l3(packet);
3490 npi->buffer_id = htonl(pin->buffer_id);
3491 npi->total_len = htons(pin->total_len);
3492 npi->reason = pin->reason;
3493 npi->table_id = pin->table_id;
3494 npi->cookie = pin->cookie;
3495 npi->match_len = htons(match_len);
3500 static struct ofpbuf *
3501 ofputil_encode_ofp11_packet_in(const struct ofputil_packet_in *pin)
3503 struct ofp11_packet_in *opi;
3504 struct ofpbuf *packet;
3506 packet = ofpraw_alloc_xid(OFPRAW_OFPT11_PACKET_IN, OFP11_VERSION,
3507 htonl(0), pin->packet_len);
3508 opi = ofpbuf_put_zeros(packet, sizeof *opi);
3509 opi->buffer_id = htonl(pin->buffer_id);
3510 opi->in_port = ofputil_port_to_ofp11(pin->fmd.in_port);
3511 opi->in_phy_port = opi->in_port;
3512 opi->total_len = htons(pin->total_len);
3513 opi->reason = pin->reason;
3514 opi->table_id = pin->table_id;
3516 ofpbuf_put(packet, pin->packet, pin->packet_len);
3521 static struct ofpbuf *
3522 ofputil_encode_ofp12_packet_in(const struct ofputil_packet_in *pin,
3523 enum ofputil_protocol protocol)
3525 struct ofp13_packet_in *opi;
3527 enum ofpraw packet_in_raw;
3528 enum ofp_version packet_in_version;
3529 size_t packet_in_size;
3530 struct ofpbuf *packet;
3532 if (protocol == OFPUTIL_P_OF12_OXM) {
3533 packet_in_raw = OFPRAW_OFPT12_PACKET_IN;
3534 packet_in_version = OFP12_VERSION;
3535 packet_in_size = sizeof (struct ofp12_packet_in);
3537 packet_in_raw = OFPRAW_OFPT13_PACKET_IN;
3538 packet_in_version = ofputil_protocol_to_ofp_version(protocol);
3539 packet_in_size = sizeof (struct ofp13_packet_in);
3542 ofputil_packet_in_to_match(pin, &match);
3544 /* The final argument is just an estimate of the space required. */
3545 packet = ofpraw_alloc_xid(packet_in_raw, packet_in_version,
3546 htonl(0), (sizeof(struct flow_metadata) * 2
3547 + 2 + pin->packet_len));
3548 ofpbuf_put_zeros(packet, packet_in_size);
3549 oxm_put_match(packet, &match, ofputil_protocol_to_ofp_version(protocol));
3550 ofpbuf_put_zeros(packet, 2);
3551 ofpbuf_put(packet, pin->packet, pin->packet_len);
3553 opi = ofpbuf_l3(packet);
3554 opi->pi.buffer_id = htonl(pin->buffer_id);
3555 opi->pi.total_len = htons(pin->total_len);
3556 opi->pi.reason = pin->reason;
3557 opi->pi.table_id = pin->table_id;
3558 if (protocol != OFPUTIL_P_OF12_OXM) {
3559 opi->cookie = pin->cookie;
3565 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
3566 * in the format specified by 'packet_in_format'. */
3568 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
3569 enum ofputil_protocol protocol,
3570 enum nx_packet_in_format packet_in_format)
3572 struct ofpbuf *packet;
3575 case OFPUTIL_P_OF10_STD:
3576 case OFPUTIL_P_OF10_STD_TID:
3577 case OFPUTIL_P_OF10_NXM:
3578 case OFPUTIL_P_OF10_NXM_TID:
3579 packet = (packet_in_format == NXPIF_NXM
3580 ? ofputil_encode_nx_packet_in(pin)
3581 : ofputil_encode_ofp10_packet_in(pin));
3584 case OFPUTIL_P_OF11_STD:
3585 packet = ofputil_encode_ofp11_packet_in(pin);
3588 case OFPUTIL_P_OF12_OXM:
3589 case OFPUTIL_P_OF13_OXM:
3590 case OFPUTIL_P_OF14_OXM:
3591 case OFPUTIL_P_OF15_OXM:
3592 packet = ofputil_encode_ofp12_packet_in(pin, protocol);
3599 ofpmsg_update_length(packet);
3603 /* Returns a string form of 'reason'. The return value is either a statically
3604 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
3605 * 'bufsize' should be at least OFPUTIL_PACKET_IN_REASON_BUFSIZE. */
3607 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason,
3608 char *reasonbuf, size_t bufsize)
3615 case OFPR_INVALID_TTL:
3616 return "invalid_ttl";
3617 case OFPR_ACTION_SET:
3618 return "action_set";
3621 case OFPR_PACKET_OUT:
3622 return "packet_out";
3624 case OFPR_N_REASONS:
3626 snprintf(reasonbuf, bufsize, "%d", (int) reason);
3632 ofputil_packet_in_reason_from_string(const char *s,
3633 enum ofp_packet_in_reason *reason)
3637 for (i = 0; i < OFPR_N_REASONS; i++) {
3638 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
3639 const char *reason_s;
3641 reason_s = ofputil_packet_in_reason_to_string(i, reasonbuf,
3643 if (!strcasecmp(s, reason_s)) {
3651 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
3654 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
3655 * message's actions. The caller must initialize 'ofpacts' and retains
3656 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
3658 * Returns 0 if successful, otherwise an OFPERR_* value. */
3660 ofputil_decode_packet_out(struct ofputil_packet_out *po,
3661 const struct ofp_header *oh,
3662 struct ofpbuf *ofpacts)
3667 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3668 raw = ofpraw_pull_assert(&b);
3670 if (raw == OFPRAW_OFPT11_PACKET_OUT) {
3672 const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
3674 po->buffer_id = ntohl(opo->buffer_id);
3675 error = ofputil_port_from_ofp11(opo->in_port, &po->in_port);
3680 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
3681 oh->version, ofpacts);
3685 } else if (raw == OFPRAW_OFPT10_PACKET_OUT) {
3687 const struct ofp10_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
3689 po->buffer_id = ntohl(opo->buffer_id);
3690 po->in_port = u16_to_ofp(ntohs(opo->in_port));
3692 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
3693 oh->version, ofpacts);
3701 if (ofp_to_u16(po->in_port) >= ofp_to_u16(OFPP_MAX)
3702 && po->in_port != OFPP_LOCAL
3703 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
3704 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
3706 return OFPERR_OFPBRC_BAD_PORT;
3709 po->ofpacts = ofpbuf_data(ofpacts);
3710 po->ofpacts_len = ofpbuf_size(ofpacts);
3712 if (po->buffer_id == UINT32_MAX) {
3713 po->packet = ofpbuf_data(&b);
3714 po->packet_len = ofpbuf_size(&b);
3723 /* ofputil_phy_port */
3725 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
3726 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
3727 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
3728 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
3729 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
3730 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
3731 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
3732 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
3734 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
3735 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
3736 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
3737 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
3738 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
3739 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
3741 static enum netdev_features
3742 netdev_port_features_from_ofp10(ovs_be32 ofp10_)
3744 uint32_t ofp10 = ntohl(ofp10_);
3745 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
3749 netdev_port_features_to_ofp10(enum netdev_features features)
3751 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
3754 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
3755 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
3756 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
3757 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
3758 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
3759 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
3760 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
3761 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
3762 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
3763 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
3764 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
3765 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
3766 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
3767 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
3768 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
3769 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
3771 static enum netdev_features
3772 netdev_port_features_from_ofp11(ovs_be32 ofp11)
3774 return ntohl(ofp11) & 0xffff;
3778 netdev_port_features_to_ofp11(enum netdev_features features)
3780 return htonl(features & 0xffff);
3784 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
3785 const struct ofp10_phy_port *opp)
3787 pp->port_no = u16_to_ofp(ntohs(opp->port_no));
3788 memcpy(pp->hw_addr, opp->hw_addr, OFP_ETH_ALEN);
3789 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
3791 pp->config = ntohl(opp->config) & OFPPC10_ALL;
3792 pp->state = ntohl(opp->state) & OFPPS10_ALL;
3794 pp->curr = netdev_port_features_from_ofp10(opp->curr);
3795 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
3796 pp->supported = netdev_port_features_from_ofp10(opp->supported);
3797 pp->peer = netdev_port_features_from_ofp10(opp->peer);
3799 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
3800 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
3806 ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
3807 const struct ofp11_port *op)
3811 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
3815 memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN);
3816 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
3818 pp->config = ntohl(op->config) & OFPPC11_ALL;
3819 pp->state = ntohl(op->state) & OFPPS11_ALL;
3821 pp->curr = netdev_port_features_from_ofp11(op->curr);
3822 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
3823 pp->supported = netdev_port_features_from_ofp11(op->supported);
3824 pp->peer = netdev_port_features_from_ofp11(op->peer);
3826 pp->curr_speed = ntohl(op->curr_speed);
3827 pp->max_speed = ntohl(op->max_speed);
3833 parse_ofp14_port_ethernet_property(const struct ofpbuf *payload,
3834 struct ofputil_phy_port *pp)
3836 struct ofp14_port_desc_prop_ethernet *eth = ofpbuf_data(payload);
3838 if (ofpbuf_size(payload) != sizeof *eth) {
3839 return OFPERR_OFPBPC_BAD_LEN;
3842 pp->curr = netdev_port_features_from_ofp11(eth->curr);
3843 pp->advertised = netdev_port_features_from_ofp11(eth->advertised);
3844 pp->supported = netdev_port_features_from_ofp11(eth->supported);
3845 pp->peer = netdev_port_features_from_ofp11(eth->peer);
3847 pp->curr_speed = ntohl(eth->curr_speed);
3848 pp->max_speed = ntohl(eth->max_speed);
3854 ofputil_pull_ofp14_port(struct ofputil_phy_port *pp, struct ofpbuf *msg)
3856 struct ofpbuf properties;
3857 struct ofp14_port *op;
3861 op = ofpbuf_try_pull(msg, sizeof *op);
3863 return OFPERR_OFPBRC_BAD_LEN;
3866 len = ntohs(op->length);
3867 if (len < sizeof *op || len - sizeof *op > ofpbuf_size(msg)) {
3868 return OFPERR_OFPBRC_BAD_LEN;
3871 ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len);
3873 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
3877 memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN);
3878 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
3880 pp->config = ntohl(op->config) & OFPPC11_ALL;
3881 pp->state = ntohl(op->state) & OFPPS11_ALL;
3883 while (ofpbuf_size(&properties) > 0) {
3884 struct ofpbuf payload;
3888 error = ofputil_pull_property(&properties, &payload, &type);
3894 case OFPPDPT14_ETHERNET:
3895 error = parse_ofp14_port_ethernet_property(&payload, pp);
3899 log_property(true, "unknown port property %"PRIu16, type);
3913 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
3914 struct ofp10_phy_port *opp)
3916 memset(opp, 0, sizeof *opp);
3918 opp->port_no = htons(ofp_to_u16(pp->port_no));
3919 memcpy(opp->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
3920 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
3922 opp->config = htonl(pp->config & OFPPC10_ALL);
3923 opp->state = htonl(pp->state & OFPPS10_ALL);
3925 opp->curr = netdev_port_features_to_ofp10(pp->curr);
3926 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
3927 opp->supported = netdev_port_features_to_ofp10(pp->supported);
3928 opp->peer = netdev_port_features_to_ofp10(pp->peer);
3932 ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
3933 struct ofp11_port *op)
3935 memset(op, 0, sizeof *op);
3937 op->port_no = ofputil_port_to_ofp11(pp->port_no);
3938 memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
3939 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
3941 op->config = htonl(pp->config & OFPPC11_ALL);
3942 op->state = htonl(pp->state & OFPPS11_ALL);
3944 op->curr = netdev_port_features_to_ofp11(pp->curr);
3945 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
3946 op->supported = netdev_port_features_to_ofp11(pp->supported);
3947 op->peer = netdev_port_features_to_ofp11(pp->peer);
3949 op->curr_speed = htonl(pp->curr_speed);
3950 op->max_speed = htonl(pp->max_speed);
3954 ofputil_put_ofp14_port(const struct ofputil_phy_port *pp,
3957 struct ofp14_port *op;
3958 struct ofp14_port_desc_prop_ethernet *eth;
3960 ofpbuf_prealloc_tailroom(b, sizeof *op + sizeof *eth);
3962 op = ofpbuf_put_zeros(b, sizeof *op);
3963 op->port_no = ofputil_port_to_ofp11(pp->port_no);
3964 op->length = htons(sizeof *op + sizeof *eth);
3965 memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
3966 ovs_strlcpy(op->name, pp->name, sizeof op->name);
3967 op->config = htonl(pp->config & OFPPC11_ALL);
3968 op->state = htonl(pp->state & OFPPS11_ALL);
3970 eth = ofpbuf_put_zeros(b, sizeof *eth);
3971 eth->type = htons(OFPPDPT14_ETHERNET);
3972 eth->length = htons(sizeof *eth);
3973 eth->curr = netdev_port_features_to_ofp11(pp->curr);
3974 eth->advertised = netdev_port_features_to_ofp11(pp->advertised);
3975 eth->supported = netdev_port_features_to_ofp11(pp->supported);
3976 eth->peer = netdev_port_features_to_ofp11(pp->peer);
3977 eth->curr_speed = htonl(pp->curr_speed);
3978 eth->max_speed = htonl(pp->max_speed);
3982 ofputil_put_phy_port(enum ofp_version ofp_version,
3983 const struct ofputil_phy_port *pp, struct ofpbuf *b)
3985 switch (ofp_version) {
3986 case OFP10_VERSION: {
3987 struct ofp10_phy_port *opp = ofpbuf_put_uninit(b, sizeof *opp);
3988 ofputil_encode_ofp10_phy_port(pp, opp);
3994 case OFP13_VERSION: {
3995 struct ofp11_port *op = ofpbuf_put_uninit(b, sizeof *op);
3996 ofputil_encode_ofp11_port(pp, op);
4002 ofputil_put_ofp14_port(pp, b);
4011 ofputil_decode_port_desc_stats_request(const struct ofp_header *request,
4017 ofpbuf_use_const(&b, request, ntohs(request->length));
4018 raw = ofpraw_pull_assert(&b);
4019 if (raw == OFPRAW_OFPST10_PORT_DESC_REQUEST) {
4022 } else if (raw == OFPRAW_OFPST15_PORT_DESC_REQUEST) {
4023 ovs_be32 *ofp11_port;
4025 ofp11_port = ofpbuf_pull(&b, sizeof *ofp11_port);
4026 return ofputil_port_from_ofp11(*ofp11_port, port);
4033 ofputil_encode_port_desc_stats_request(enum ofp_version ofp_version,
4036 struct ofpbuf *request;
4037 ovs_be32 ofp11_port;
4039 switch (ofp_version) {
4045 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_DESC_REQUEST,
4050 request = ofpraw_alloc(OFPRAW_OFPST15_PORT_DESC_REQUEST,
4052 ofp11_port = ofputil_port_to_ofp11(port);
4053 ofpbuf_put(request, &ofp11_port, sizeof ofp11_port);
4064 ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port *pp,
4065 struct ovs_list *replies)
4067 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
4068 size_t start_ofs = ofpbuf_size(reply);
4070 ofputil_put_phy_port(ofpmp_version(replies), pp, reply);
4071 ofpmp_postappend(replies, start_ofs);
4074 /* ofputil_switch_features */
4076 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
4077 OFPC_IP_REASM | OFPC_QUEUE_STATS)
4078 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
4079 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
4080 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
4081 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
4082 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
4083 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
4086 ofputil_capabilities_mask(enum ofp_version ofp_version)
4088 /* Handle capabilities whose bit is unique for all OpenFlow versions */
4089 switch (ofp_version) {
4092 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
4097 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
4099 /* Caller needs to check osf->header.version itself */
4104 /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an
4105 * abstract representation in '*features'. Initializes '*b' to iterate over
4106 * the OpenFlow port structures following 'osf' with later calls to
4107 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an
4108 * OFPERR_* value. */
4110 ofputil_decode_switch_features(const struct ofp_header *oh,
4111 struct ofputil_switch_features *features,
4114 const struct ofp_switch_features *osf;
4117 ofpbuf_use_const(b, oh, ntohs(oh->length));
4118 raw = ofpraw_pull_assert(b);
4120 osf = ofpbuf_pull(b, sizeof *osf);
4121 features->datapath_id = ntohll(osf->datapath_id);
4122 features->n_buffers = ntohl(osf->n_buffers);
4123 features->n_tables = osf->n_tables;
4124 features->auxiliary_id = 0;
4126 features->capabilities = ntohl(osf->capabilities) &
4127 ofputil_capabilities_mask(oh->version);
4129 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
4130 if (osf->capabilities & htonl(OFPC10_STP)) {
4131 features->capabilities |= OFPUTIL_C_STP;
4133 features->ofpacts = ofpact_bitmap_from_openflow(osf->actions,
4135 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY
4136 || raw == OFPRAW_OFPT13_FEATURES_REPLY) {
4137 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
4138 features->capabilities |= OFPUTIL_C_GROUP_STATS;
4140 features->ofpacts = 0;
4141 if (raw == OFPRAW_OFPT13_FEATURES_REPLY) {
4142 features->auxiliary_id = osf->auxiliary_id;
4145 return OFPERR_OFPBRC_BAD_VERSION;
4151 /* In OpenFlow 1.0, 1.1, and 1.2, an OFPT_FEATURES_REPLY message lists all the
4152 * switch's ports, unless there are too many to fit. In OpenFlow 1.3 and
4153 * later, an OFPT_FEATURES_REPLY does not list ports at all.
4155 * Given a buffer 'b' that contains a Features Reply message, this message
4156 * checks if it contains a complete list of the switch's ports. Returns true,
4157 * if so. Returns false if the list is missing (OF1.3+) or incomplete
4158 * (OF1.0/1.1/1.2), and in the latter case removes all of the ports from the
4161 * When this function returns false, the caller should send an OFPST_PORT_DESC
4162 * stats request to get the ports. */
4164 ofputil_switch_features_has_ports(struct ofpbuf *b)
4166 struct ofp_header *oh = ofpbuf_data(b);
4167 size_t phy_port_size;
4169 if (oh->version >= OFP13_VERSION) {
4170 /* OpenFlow 1.3+ never has ports in the feature reply. */
4174 phy_port_size = (oh->version == OFP10_VERSION
4175 ? sizeof(struct ofp10_phy_port)
4176 : sizeof(struct ofp11_port));
4177 if (ntohs(oh->length) + phy_port_size <= UINT16_MAX) {
4178 /* There's room for additional ports in the feature reply.
4179 * Assume that the list is complete. */
4183 /* The feature reply has no room for more ports. Probably the list is
4184 * truncated. Drop the ports and tell the caller to retrieve them with
4185 * OFPST_PORT_DESC. */
4186 ofpbuf_set_size(b, sizeof *oh + sizeof(struct ofp_switch_features));
4187 ofpmsg_update_length(b);
4191 /* Returns a buffer owned by the caller that encodes 'features' in the format
4192 * required by 'protocol' with the given 'xid'. The caller should append port
4193 * information to the buffer with subsequent calls to
4194 * ofputil_put_switch_features_port(). */
4196 ofputil_encode_switch_features(const struct ofputil_switch_features *features,
4197 enum ofputil_protocol protocol, ovs_be32 xid)
4199 struct ofp_switch_features *osf;
4201 enum ofp_version version;
4204 version = ofputil_protocol_to_ofp_version(protocol);
4207 raw = OFPRAW_OFPT10_FEATURES_REPLY;
4211 raw = OFPRAW_OFPT11_FEATURES_REPLY;
4216 raw = OFPRAW_OFPT13_FEATURES_REPLY;
4221 b = ofpraw_alloc_xid(raw, version, xid, 0);
4222 osf = ofpbuf_put_zeros(b, sizeof *osf);
4223 osf->datapath_id = htonll(features->datapath_id);
4224 osf->n_buffers = htonl(features->n_buffers);
4225 osf->n_tables = features->n_tables;
4227 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
4228 osf->capabilities = htonl(features->capabilities &
4229 ofputil_capabilities_mask(version));
4232 if (features->capabilities & OFPUTIL_C_STP) {
4233 osf->capabilities |= htonl(OFPC10_STP);
4235 osf->actions = ofpact_bitmap_to_openflow(features->ofpacts,
4241 osf->auxiliary_id = features->auxiliary_id;
4245 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
4246 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
4256 /* Encodes 'pp' into the format required by the switch_features message already
4257 * in 'b', which should have been returned by ofputil_encode_switch_features(),
4258 * and appends the encoded version to 'b'. */
4260 ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
4263 const struct ofp_header *oh = ofpbuf_data(b);
4265 if (oh->version < OFP13_VERSION) {
4266 /* Try adding a port description to the message, but drop it again if
4267 * the buffer overflows. (This possibility for overflow is why
4268 * OpenFlow 1.3+ moved port descriptions into a multipart message.) */
4269 size_t start_ofs = ofpbuf_size(b);
4270 ofputil_put_phy_port(oh->version, pp, b);
4271 if (ofpbuf_size(b) > UINT16_MAX) {
4272 ofpbuf_set_size(b, start_ofs);
4277 /* ofputil_port_status */
4279 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
4280 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
4282 ofputil_decode_port_status(const struct ofp_header *oh,
4283 struct ofputil_port_status *ps)
4285 const struct ofp_port_status *ops;
4289 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4290 ofpraw_pull_assert(&b);
4291 ops = ofpbuf_pull(&b, sizeof *ops);
4293 if (ops->reason != OFPPR_ADD &&
4294 ops->reason != OFPPR_DELETE &&
4295 ops->reason != OFPPR_MODIFY) {
4296 return OFPERR_NXBRC_BAD_REASON;
4298 ps->reason = ops->reason;
4300 retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
4301 ovs_assert(retval != EOF);
4305 /* Converts the abstract form of a "port status" message in '*ps' into an
4306 * OpenFlow message suitable for 'protocol', and returns that encoded form in
4307 * a buffer owned by the caller. */
4309 ofputil_encode_port_status(const struct ofputil_port_status *ps,
4310 enum ofputil_protocol protocol)
4312 struct ofp_port_status *ops;
4314 enum ofp_version version;
4317 version = ofputil_protocol_to_ofp_version(protocol);
4320 raw = OFPRAW_OFPT10_PORT_STATUS;
4326 raw = OFPRAW_OFPT11_PORT_STATUS;
4331 raw = OFPRAW_OFPT14_PORT_STATUS;
4338 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
4339 ops = ofpbuf_put_zeros(b, sizeof *ops);
4340 ops->reason = ps->reason;
4341 ofputil_put_phy_port(version, &ps->desc, b);
4342 ofpmsg_update_length(b);
4346 /* ofputil_port_mod */
4349 parse_port_mod_ethernet_property(struct ofpbuf *property,
4350 struct ofputil_port_mod *pm)
4352 struct ofp14_port_mod_prop_ethernet *eth = ofpbuf_data(property);
4354 if (ofpbuf_size(property) != sizeof *eth) {
4355 return OFPERR_OFPBRC_BAD_LEN;
4358 pm->advertise = netdev_port_features_from_ofp11(eth->advertise);
4362 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
4363 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
4365 ofputil_decode_port_mod(const struct ofp_header *oh,
4366 struct ofputil_port_mod *pm, bool loose)
4371 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4372 raw = ofpraw_pull_assert(&b);
4374 if (raw == OFPRAW_OFPT10_PORT_MOD) {
4375 const struct ofp10_port_mod *opm = ofpbuf_data(&b);
4377 pm->port_no = u16_to_ofp(ntohs(opm->port_no));
4378 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
4379 pm->config = ntohl(opm->config) & OFPPC10_ALL;
4380 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
4381 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
4382 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
4383 const struct ofp11_port_mod *opm = ofpbuf_data(&b);
4386 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
4391 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
4392 pm->config = ntohl(opm->config) & OFPPC11_ALL;
4393 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
4394 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
4395 } else if (raw == OFPRAW_OFPT14_PORT_MOD) {
4396 const struct ofp14_port_mod *opm = ofpbuf_pull(&b, sizeof *opm);
4399 memset(pm, 0, sizeof *pm);
4401 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
4406 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
4407 pm->config = ntohl(opm->config) & OFPPC11_ALL;
4408 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
4410 while (ofpbuf_size(&b) > 0) {
4411 struct ofpbuf property;
4415 error = ofputil_pull_property(&b, &property, &type);
4421 case OFPPMPT14_ETHERNET:
4422 error = parse_port_mod_ethernet_property(&property, pm);
4426 log_property(loose, "unknown port_mod property %"PRIu16, type);
4429 } else if (type == OFPPMPT14_EXPERIMENTER) {
4430 error = OFPERR_OFPBPC_BAD_EXPERIMENTER;
4432 error = OFPERR_OFPBRC_BAD_TYPE;
4442 return OFPERR_OFPBRC_BAD_TYPE;
4445 pm->config &= pm->mask;
4449 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
4450 * message suitable for 'protocol', and returns that encoded form in a buffer
4451 * owned by the caller. */
4453 ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
4454 enum ofputil_protocol protocol)
4456 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
4459 switch (ofp_version) {
4460 case OFP10_VERSION: {
4461 struct ofp10_port_mod *opm;
4463 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
4464 opm = ofpbuf_put_zeros(b, sizeof *opm);
4465 opm->port_no = htons(ofp_to_u16(pm->port_no));
4466 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
4467 opm->config = htonl(pm->config & OFPPC10_ALL);
4468 opm->mask = htonl(pm->mask & OFPPC10_ALL);
4469 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
4475 case OFP13_VERSION: {
4476 struct ofp11_port_mod *opm;
4478 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
4479 opm = ofpbuf_put_zeros(b, sizeof *opm);
4480 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
4481 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
4482 opm->config = htonl(pm->config & OFPPC11_ALL);
4483 opm->mask = htonl(pm->mask & OFPPC11_ALL);
4484 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
4488 case OFP15_VERSION: {
4489 struct ofp14_port_mod_prop_ethernet *eth;
4490 struct ofp14_port_mod *opm;
4492 b = ofpraw_alloc(OFPRAW_OFPT14_PORT_MOD, ofp_version, sizeof *eth);
4493 opm = ofpbuf_put_zeros(b, sizeof *opm);
4494 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
4495 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
4496 opm->config = htonl(pm->config & OFPPC11_ALL);
4497 opm->mask = htonl(pm->mask & OFPPC11_ALL);
4499 if (pm->advertise) {
4500 eth = ofpbuf_put_zeros(b, sizeof *eth);
4501 eth->type = htons(OFPPMPT14_ETHERNET);
4502 eth->length = htons(sizeof *eth);
4503 eth->advertise = netdev_port_features_to_ofp11(pm->advertise);
4514 /* Table features. */
4517 pull_table_feature_property(struct ofpbuf *msg, struct ofpbuf *payload,
4522 error = ofputil_pull_property(msg, payload, typep);
4523 if (payload && !error) {
4524 ofpbuf_pull(payload, sizeof(struct ofp_prop_header));
4530 parse_action_bitmap(struct ofpbuf *payload, enum ofp_version ofp_version,
4535 while (ofpbuf_size(payload) > 0) {
4539 error = ofputil_pull_property__(payload, NULL, 1, &type);
4543 if (type < CHAR_BIT * sizeof types) {
4544 types |= 1u << type;
4548 *ofpacts = ofpact_bitmap_from_openflow(htonl(types), ofp_version);
4553 parse_instruction_ids(struct ofpbuf *payload, bool loose, uint32_t *insts)
4556 while (ofpbuf_size(payload) > 0) {
4557 enum ovs_instruction_type inst;
4561 /* OF1.3 and OF1.4 aren't clear about padding in the instruction IDs.
4562 * It seems clear that they aren't padded to 8 bytes, though, because
4563 * both standards say that "non-experimenter instructions are 4 bytes"
4564 * and do not mention any padding before the first instruction ID.
4565 * (There wouldn't be any point in padding to 8 bytes if the IDs were
4566 * aligned on an odd 4-byte boundary.)
4568 * Anyway, we just assume they're all glommed together on byte
4570 error = ofputil_pull_property__(payload, NULL, 1, &ofpit);
4575 error = ovs_instruction_type_from_inst_type(&inst, ofpit);
4577 *insts |= 1u << inst;
4578 } else if (!loose) {
4586 parse_table_features_next_table(struct ofpbuf *payload,
4587 unsigned long int *next_tables)
4591 memset(next_tables, 0, bitmap_n_bytes(255));
4592 for (i = 0; i < ofpbuf_size(payload); i++) {
4593 uint8_t id = ((const uint8_t *) ofpbuf_data(payload))[i];
4595 return OFPERR_OFPBPC_BAD_VALUE;
4597 bitmap_set1(next_tables, id);
4603 parse_oxms(struct ofpbuf *payload, bool loose,
4604 struct mf_bitmap *exactp, struct mf_bitmap *maskedp)
4606 struct mf_bitmap exact = MF_BITMAP_INITIALIZER;
4607 struct mf_bitmap masked = MF_BITMAP_INITIALIZER;
4609 while (ofpbuf_size(payload) > 0) {
4610 const struct mf_field *field;
4614 error = nx_pull_header(payload, &field, &hasmask);
4616 bitmap_set1(hasmask ? masked.bm : exact.bm, field->id);
4617 } else if (error != OFPERR_OFPBMC_BAD_FIELD || !loose) {
4623 } else if (!bitmap_is_all_zeros(exact.bm, MFF_N_IDS)) {
4624 return OFPERR_OFPBMC_BAD_MASK;
4628 } else if (!bitmap_is_all_zeros(masked.bm, MFF_N_IDS)) {
4629 return OFPERR_OFPBMC_BAD_MASK;
4634 /* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract
4635 * ofputil_table_features in 'tf'.
4637 * If 'loose' is true, this function ignores properties and values that it does
4638 * not understand, as a controller would want to do when interpreting
4639 * capabilities provided by a switch. If 'loose' is false, this function
4640 * treats unknown properties and values as an error, as a switch would want to
4641 * do when interpreting a configuration request made by a controller.
4643 * A single OpenFlow message can specify features for multiple tables. Calling
4644 * this function multiple times for a single 'msg' iterates through the tables
4645 * in the message. The caller must initially leave 'msg''s layer pointers null
4646 * and not modify them between calls.
4648 * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise
4649 * a positive "enum ofperr" value. */
4651 ofputil_decode_table_features(struct ofpbuf *msg,
4652 struct ofputil_table_features *tf, bool loose)
4654 const struct ofp_header *oh;
4655 struct ofp13_table_features *otf;
4656 struct ofpbuf properties;
4659 memset(tf, 0, sizeof *tf);
4662 ofpraw_pull_assert(msg);
4664 oh = ofpbuf_l2(msg);
4666 if (!ofpbuf_size(msg)) {
4670 if (ofpbuf_size(msg) < sizeof *otf) {
4671 return OFPERR_OFPBPC_BAD_LEN;
4674 otf = ofpbuf_data(msg);
4675 len = ntohs(otf->length);
4676 if (len < sizeof *otf || len % 8 || len > ofpbuf_size(msg)) {
4677 return OFPERR_OFPBPC_BAD_LEN;
4679 ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len);
4680 ofpbuf_pull(&properties, sizeof *otf);
4682 tf->table_id = otf->table_id;
4683 if (tf->table_id == OFPTT_ALL) {
4684 return OFPERR_OFPTFFC_BAD_TABLE;
4687 ovs_strlcpy(tf->name, otf->name, OFP_MAX_TABLE_NAME_LEN);
4688 tf->metadata_match = otf->metadata_match;
4689 tf->metadata_write = otf->metadata_write;
4690 tf->miss_config = ofputil_table_miss_from_config(otf->config, oh->version);
4691 tf->max_entries = ntohl(otf->max_entries);
4693 while (ofpbuf_size(&properties) > 0) {
4694 struct ofpbuf payload;
4698 error = pull_table_feature_property(&properties, &payload, &type);
4703 switch ((enum ofp13_table_feature_prop_type) type) {
4704 case OFPTFPT13_INSTRUCTIONS:
4705 error = parse_instruction_ids(&payload, loose,
4706 &tf->nonmiss.instructions);
4708 case OFPTFPT13_INSTRUCTIONS_MISS:
4709 error = parse_instruction_ids(&payload, loose,
4710 &tf->miss.instructions);
4713 case OFPTFPT13_NEXT_TABLES:
4714 error = parse_table_features_next_table(&payload,
4717 case OFPTFPT13_NEXT_TABLES_MISS:
4718 error = parse_table_features_next_table(&payload, tf->miss.next);
4721 case OFPTFPT13_WRITE_ACTIONS:
4722 error = parse_action_bitmap(&payload, oh->version,
4723 &tf->nonmiss.write.ofpacts);
4725 case OFPTFPT13_WRITE_ACTIONS_MISS:
4726 error = parse_action_bitmap(&payload, oh->version,
4727 &tf->miss.write.ofpacts);
4730 case OFPTFPT13_APPLY_ACTIONS:
4731 error = parse_action_bitmap(&payload, oh->version,
4732 &tf->nonmiss.apply.ofpacts);
4734 case OFPTFPT13_APPLY_ACTIONS_MISS:
4735 error = parse_action_bitmap(&payload, oh->version,
4736 &tf->miss.apply.ofpacts);
4739 case OFPTFPT13_MATCH:
4740 error = parse_oxms(&payload, loose, &tf->match, &tf->mask);
4742 case OFPTFPT13_WILDCARDS:
4743 error = parse_oxms(&payload, loose, &tf->wildcard, NULL);
4746 case OFPTFPT13_WRITE_SETFIELD:
4747 error = parse_oxms(&payload, loose,
4748 &tf->nonmiss.write.set_fields, NULL);
4750 case OFPTFPT13_WRITE_SETFIELD_MISS:
4751 error = parse_oxms(&payload, loose,
4752 &tf->miss.write.set_fields, NULL);
4754 case OFPTFPT13_APPLY_SETFIELD:
4755 error = parse_oxms(&payload, loose,
4756 &tf->nonmiss.apply.set_fields, NULL);
4758 case OFPTFPT13_APPLY_SETFIELD_MISS:
4759 error = parse_oxms(&payload, loose,
4760 &tf->miss.apply.set_fields, NULL);
4763 case OFPTFPT13_EXPERIMENTER:
4764 case OFPTFPT13_EXPERIMENTER_MISS:
4766 log_property(loose, "unknown table features property %"PRIu16,
4768 error = loose ? 0 : OFPERR_OFPBPC_BAD_TYPE;
4776 /* Fix inconsistencies:
4778 * - Turn on 'match' bits that are set in 'mask', because maskable
4779 * fields are matchable.
4781 * - Turn on 'wildcard' bits that are set in 'mask', because a field
4782 * that is arbitrarily maskable can be wildcarded entirely.
4784 * - Turn off 'wildcard' bits that are not in 'match', because a field
4785 * must be matchable for it to be meaningfully wildcarded. */
4786 bitmap_or(tf->match.bm, tf->mask.bm, MFF_N_IDS);
4787 bitmap_or(tf->wildcard.bm, tf->mask.bm, MFF_N_IDS);
4788 bitmap_and(tf->wildcard.bm, tf->match.bm, MFF_N_IDS);
4793 /* Encodes and returns a request to obtain the table features of a switch.
4794 * The message is encoded for OpenFlow version 'ofp_version'. */
4796 ofputil_encode_table_features_request(enum ofp_version ofp_version)
4798 struct ofpbuf *request = NULL;
4800 switch (ofp_version) {
4804 ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later "
4805 "(\'-O OpenFlow13\')");
4809 request = ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST,
4820 put_fields_property(struct ofpbuf *reply,
4821 const struct mf_bitmap *fields,
4822 const struct mf_bitmap *masks,
4823 enum ofp13_table_feature_prop_type property,
4824 enum ofp_version version)
4829 start_ofs = start_property(reply, property);
4830 BITMAP_FOR_EACH_1 (field, MFF_N_IDS, fields->bm) {
4831 nx_put_header(reply, field, version,
4832 masks && bitmap_is_set(masks->bm, field));
4834 end_property(reply, start_ofs);
4838 put_table_action_features(struct ofpbuf *reply,
4839 const struct ofputil_table_action_features *taf,
4840 enum ofp13_table_feature_prop_type actions_type,
4841 enum ofp13_table_feature_prop_type set_fields_type,
4842 int miss_offset, enum ofp_version version)
4846 start_ofs = start_property(reply, actions_type + miss_offset);
4847 put_bitmap_properties(reply,
4848 ntohl(ofpact_bitmap_to_openflow(taf->ofpacts,
4850 end_property(reply, start_ofs);
4852 put_fields_property(reply, &taf->set_fields, NULL,
4853 set_fields_type + miss_offset, version);
4857 put_table_instruction_features(
4858 struct ofpbuf *reply, const struct ofputil_table_instruction_features *tif,
4859 int miss_offset, enum ofp_version version)
4864 start_ofs = start_property(reply, OFPTFPT13_INSTRUCTIONS + miss_offset);
4865 put_bitmap_properties(reply,
4866 ntohl(ovsinst_bitmap_to_openflow(tif->instructions,
4868 end_property(reply, start_ofs);
4870 start_ofs = start_property(reply, OFPTFPT13_NEXT_TABLES + miss_offset);
4871 BITMAP_FOR_EACH_1 (table_id, 255, tif->next) {
4872 ofpbuf_put(reply, &table_id, 1);
4874 end_property(reply, start_ofs);
4876 put_table_action_features(reply, &tif->write,
4877 OFPTFPT13_WRITE_ACTIONS,
4878 OFPTFPT13_WRITE_SETFIELD, miss_offset, version);
4879 put_table_action_features(reply, &tif->apply,
4880 OFPTFPT13_APPLY_ACTIONS,
4881 OFPTFPT13_APPLY_SETFIELD, miss_offset, version);
4885 ofputil_append_table_features_reply(const struct ofputil_table_features *tf,
4886 struct ovs_list *replies)
4888 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
4889 enum ofp_version version = ofpmp_version(replies);
4890 size_t start_ofs = ofpbuf_size(reply);
4891 struct ofp13_table_features *otf;
4893 otf = ofpbuf_put_zeros(reply, sizeof *otf);
4894 otf->table_id = tf->table_id;
4895 ovs_strlcpy(otf->name, tf->name, sizeof otf->name);
4896 otf->metadata_match = tf->metadata_match;
4897 otf->metadata_write = tf->metadata_write;
4898 otf->config = ofputil_table_miss_to_config(tf->miss_config, version);
4899 otf->max_entries = htonl(tf->max_entries);
4901 put_table_instruction_features(reply, &tf->nonmiss, 0, version);
4902 put_table_instruction_features(reply, &tf->miss, 1, version);
4904 put_fields_property(reply, &tf->match, &tf->mask,
4905 OFPTFPT13_MATCH, version);
4906 put_fields_property(reply, &tf->wildcard, NULL,
4907 OFPTFPT13_WILDCARDS, version);
4909 otf = ofpbuf_at_assert(reply, start_ofs, sizeof *otf);
4910 otf->length = htons(ofpbuf_size(reply) - start_ofs);
4911 ofpmp_postappend(replies, start_ofs);
4914 /* ofputil_table_mod */
4916 /* Given 'config', taken from an OpenFlow 'version' message that specifies
4917 * table configuration (a table mod, table stats, or table features message),
4918 * returns the table miss configuration that it specifies. */
4919 static enum ofputil_table_miss
4920 ofputil_table_miss_from_config(ovs_be32 config_, enum ofp_version version)
4922 uint32_t config = ntohl(config_);
4924 if (version < OFP13_VERSION) {
4925 switch (config & OFPTC11_TABLE_MISS_MASK) {
4926 case OFPTC11_TABLE_MISS_CONTROLLER:
4927 return OFPUTIL_TABLE_MISS_CONTROLLER;
4929 case OFPTC11_TABLE_MISS_CONTINUE:
4930 return OFPUTIL_TABLE_MISS_CONTINUE;
4932 case OFPTC11_TABLE_MISS_DROP:
4933 return OFPUTIL_TABLE_MISS_DROP;
4936 VLOG_WARN_RL(&bad_ofmsg_rl, "bad table miss config %d", config);
4937 return OFPUTIL_TABLE_MISS_CONTROLLER;
4940 return OFPUTIL_TABLE_MISS_DEFAULT;
4944 /* Given a table miss configuration, returns the corresponding OpenFlow table
4945 * configuration for use in an OpenFlow message of the given 'version'. */
4947 ofputil_table_miss_to_config(enum ofputil_table_miss miss,
4948 enum ofp_version version)
4950 if (version < OFP13_VERSION) {
4952 case OFPUTIL_TABLE_MISS_CONTROLLER:
4953 case OFPUTIL_TABLE_MISS_DEFAULT:
4954 return htonl(OFPTC11_TABLE_MISS_CONTROLLER);
4956 case OFPUTIL_TABLE_MISS_CONTINUE:
4957 return htonl(OFPTC11_TABLE_MISS_CONTINUE);
4959 case OFPUTIL_TABLE_MISS_DROP:
4960 return htonl(OFPTC11_TABLE_MISS_DROP);
4970 /* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in
4971 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
4973 ofputil_decode_table_mod(const struct ofp_header *oh,
4974 struct ofputil_table_mod *pm)
4979 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4980 raw = ofpraw_pull_assert(&b);
4982 if (raw == OFPRAW_OFPT11_TABLE_MOD) {
4983 const struct ofp11_table_mod *otm = ofpbuf_data(&b);
4985 pm->table_id = otm->table_id;
4986 pm->miss_config = ofputil_table_miss_from_config(otm->config,
4988 } else if (raw == OFPRAW_OFPT14_TABLE_MOD) {
4989 const struct ofp14_table_mod *otm = ofpbuf_pull(&b, sizeof *otm);
4991 pm->table_id = otm->table_id;
4992 pm->miss_config = ofputil_table_miss_from_config(otm->config,
4994 /* We do not understand any properties yet, so we do not bother
4997 return OFPERR_OFPBRC_BAD_TYPE;
5003 /* Converts the abstract form of a "table mod" message in '*pm' into an OpenFlow
5004 * message suitable for 'protocol', and returns that encoded form in a buffer
5005 * owned by the caller. */
5007 ofputil_encode_table_mod(const struct ofputil_table_mod *pm,
5008 enum ofputil_protocol protocol)
5010 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
5013 switch (ofp_version) {
5014 case OFP10_VERSION: {
5015 ovs_fatal(0, "table mod needs OpenFlow 1.1 or later "
5016 "(\'-O OpenFlow11\')");
5021 case OFP13_VERSION: {
5022 struct ofp11_table_mod *otm;
5024 b = ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD, ofp_version, 0);
5025 otm = ofpbuf_put_zeros(b, sizeof *otm);
5026 otm->table_id = pm->table_id;
5027 otm->config = ofputil_table_miss_to_config(pm->miss_config,
5032 case OFP15_VERSION: {
5033 struct ofp14_table_mod *otm;
5035 b = ofpraw_alloc(OFPRAW_OFPT14_TABLE_MOD, ofp_version, 0);
5036 otm = ofpbuf_put_zeros(b, sizeof *otm);
5037 otm->table_id = pm->table_id;
5038 otm->config = ofputil_table_miss_to_config(pm->miss_config,
5049 /* ofputil_role_request */
5051 /* Decodes the OpenFlow "role request" or "role reply" message in '*oh' into
5052 * an abstract form in '*rr'. Returns 0 if successful, otherwise an
5053 * OFPERR_* value. */
5055 ofputil_decode_role_message(const struct ofp_header *oh,
5056 struct ofputil_role_request *rr)
5061 ofpbuf_use_const(&b, oh, ntohs(oh->length));
5062 raw = ofpraw_pull_assert(&b);
5064 if (raw == OFPRAW_OFPT12_ROLE_REQUEST ||
5065 raw == OFPRAW_OFPT12_ROLE_REPLY) {
5066 const struct ofp12_role_request *orr = ofpbuf_l3(&b);
5068 if (orr->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
5069 orr->role != htonl(OFPCR12_ROLE_EQUAL) &&
5070 orr->role != htonl(OFPCR12_ROLE_MASTER) &&
5071 orr->role != htonl(OFPCR12_ROLE_SLAVE)) {
5072 return OFPERR_OFPRRFC_BAD_ROLE;
5075 rr->role = ntohl(orr->role);
5076 if (raw == OFPRAW_OFPT12_ROLE_REQUEST
5077 ? orr->role == htonl(OFPCR12_ROLE_NOCHANGE)
5078 : orr->generation_id == OVS_BE64_MAX) {
5079 rr->have_generation_id = false;
5080 rr->generation_id = 0;
5082 rr->have_generation_id = true;
5083 rr->generation_id = ntohll(orr->generation_id);
5085 } else if (raw == OFPRAW_NXT_ROLE_REQUEST ||
5086 raw == OFPRAW_NXT_ROLE_REPLY) {
5087 const struct nx_role_request *nrr = ofpbuf_l3(&b);
5089 BUILD_ASSERT(NX_ROLE_OTHER + 1 == OFPCR12_ROLE_EQUAL);
5090 BUILD_ASSERT(NX_ROLE_MASTER + 1 == OFPCR12_ROLE_MASTER);
5091 BUILD_ASSERT(NX_ROLE_SLAVE + 1 == OFPCR12_ROLE_SLAVE);
5093 if (nrr->role != htonl(NX_ROLE_OTHER) &&
5094 nrr->role != htonl(NX_ROLE_MASTER) &&
5095 nrr->role != htonl(NX_ROLE_SLAVE)) {
5096 return OFPERR_OFPRRFC_BAD_ROLE;
5099 rr->role = ntohl(nrr->role) + 1;
5100 rr->have_generation_id = false;
5101 rr->generation_id = 0;
5109 /* Returns an encoded form of a role reply suitable for the "request" in a
5110 * buffer owned by the caller. */
5112 ofputil_encode_role_reply(const struct ofp_header *request,
5113 const struct ofputil_role_request *rr)
5118 raw = ofpraw_decode_assert(request);
5119 if (raw == OFPRAW_OFPT12_ROLE_REQUEST) {
5120 struct ofp12_role_request *orr;
5122 buf = ofpraw_alloc_reply(OFPRAW_OFPT12_ROLE_REPLY, request, 0);
5123 orr = ofpbuf_put_zeros(buf, sizeof *orr);
5125 orr->role = htonl(rr->role);
5126 orr->generation_id = htonll(rr->have_generation_id
5129 } else if (raw == OFPRAW_NXT_ROLE_REQUEST) {
5130 struct nx_role_request *nrr;
5132 BUILD_ASSERT(NX_ROLE_OTHER == OFPCR12_ROLE_EQUAL - 1);
5133 BUILD_ASSERT(NX_ROLE_MASTER == OFPCR12_ROLE_MASTER - 1);
5134 BUILD_ASSERT(NX_ROLE_SLAVE == OFPCR12_ROLE_SLAVE - 1);
5136 buf = ofpraw_alloc_reply(OFPRAW_NXT_ROLE_REPLY, request, 0);
5137 nrr = ofpbuf_put_zeros(buf, sizeof *nrr);
5138 nrr->role = htonl(rr->role - 1);
5146 /* Encodes "role status" message 'status' for sending in the given
5147 * 'protocol'. Returns the role status message, if 'protocol' supports them,
5148 * otherwise a null pointer. */
5150 ofputil_encode_role_status(const struct ofputil_role_status *status,
5151 enum ofputil_protocol protocol)
5153 enum ofp_version version;
5155 version = ofputil_protocol_to_ofp_version(protocol);
5156 if (version >= OFP14_VERSION) {
5157 struct ofp14_role_status *rstatus;
5160 buf = ofpraw_alloc_xid(OFPRAW_OFPT14_ROLE_STATUS, version, htonl(0),
5162 rstatus = ofpbuf_put_zeros(buf, sizeof *rstatus);
5163 rstatus->role = htonl(status->role);
5164 rstatus->reason = status->reason;
5165 rstatus->generation_id = htonll(status->generation_id);
5174 ofputil_decode_role_status(const struct ofp_header *oh,
5175 struct ofputil_role_status *rs)
5179 const struct ofp14_role_status *r;
5181 ofpbuf_use_const(&b, oh, ntohs(oh->length));
5182 raw = ofpraw_pull_assert(&b);
5183 ovs_assert(raw == OFPRAW_OFPT14_ROLE_STATUS);
5186 if (r->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
5187 r->role != htonl(OFPCR12_ROLE_EQUAL) &&
5188 r->role != htonl(OFPCR12_ROLE_MASTER) &&
5189 r->role != htonl(OFPCR12_ROLE_SLAVE)) {
5190 return OFPERR_OFPRRFC_BAD_ROLE;
5193 rs->role = ntohl(r->role);
5194 rs->generation_id = ntohll(r->generation_id);
5195 rs->reason = r->reason;
5202 /* OpenFlow 1.0 and 1.1 don't distinguish between a field that cannot be
5203 * matched and a field that must be wildcarded. This function returns a bitmap
5204 * that contains both kinds of fields. */
5205 static struct mf_bitmap
5206 wild_or_nonmatchable_fields(const struct ofputil_table_features *features)
5208 struct mf_bitmap wc = features->match;
5209 bitmap_not(wc.bm, MFF_N_IDS);
5210 bitmap_or(wc.bm, features->wildcard.bm, MFF_N_IDS);
5214 struct ofp10_wc_map {
5215 enum ofp10_flow_wildcards wc10;
5216 enum mf_field_id mf;
5219 static const struct ofp10_wc_map ofp10_wc_map[] = {
5220 { OFPFW10_IN_PORT, MFF_IN_PORT },
5221 { OFPFW10_DL_VLAN, MFF_VLAN_VID },
5222 { OFPFW10_DL_SRC, MFF_ETH_SRC },
5223 { OFPFW10_DL_DST, MFF_ETH_DST},
5224 { OFPFW10_DL_TYPE, MFF_ETH_TYPE },
5225 { OFPFW10_NW_PROTO, MFF_IP_PROTO },
5226 { OFPFW10_TP_SRC, MFF_TCP_SRC },
5227 { OFPFW10_TP_DST, MFF_TCP_DST },
5228 { OFPFW10_NW_SRC_MASK, MFF_IPV4_SRC },
5229 { OFPFW10_NW_DST_MASK, MFF_IPV4_DST },
5230 { OFPFW10_DL_VLAN_PCP, MFF_VLAN_PCP },
5231 { OFPFW10_NW_TOS, MFF_IP_DSCP },
5235 mf_bitmap_to_of10(const struct mf_bitmap *fields)
5237 const struct ofp10_wc_map *p;
5240 for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
5241 if (bitmap_is_set(fields->bm, p->mf)) {
5248 static struct mf_bitmap
5249 mf_bitmap_from_of10(ovs_be32 wc10_)
5251 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
5252 const struct ofp10_wc_map *p;
5253 uint32_t wc10 = ntohl(wc10_);
5255 for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
5256 if (wc10 & p->wc10) {
5257 bitmap_set1(fields.bm, p->mf);
5264 ofputil_put_ofp10_table_stats(const struct ofputil_table_stats *stats,
5265 const struct ofputil_table_features *features,
5268 struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
5269 struct ofp10_table_stats *out;
5271 out = ofpbuf_put_zeros(buf, sizeof *out);
5272 out->table_id = features->table_id;
5273 ovs_strlcpy(out->name, features->name, sizeof out->name);
5274 out->wildcards = mf_bitmap_to_of10(&wc);
5275 out->max_entries = htonl(features->max_entries);
5276 out->active_count = htonl(stats->active_count);
5277 put_32aligned_be64(&out->lookup_count, htonll(stats->lookup_count));
5278 put_32aligned_be64(&out->matched_count, htonll(stats->matched_count));
5281 struct ofp11_wc_map {
5282 enum ofp11_flow_match_fields wc11;
5283 enum mf_field_id mf;
5286 static const struct ofp11_wc_map ofp11_wc_map[] = {
5287 { OFPFMF11_IN_PORT, MFF_IN_PORT },
5288 { OFPFMF11_DL_VLAN, MFF_VLAN_VID },
5289 { OFPFMF11_DL_VLAN_PCP, MFF_VLAN_PCP },
5290 { OFPFMF11_DL_TYPE, MFF_ETH_TYPE },
5291 { OFPFMF11_NW_TOS, MFF_IP_DSCP },
5292 { OFPFMF11_NW_PROTO, MFF_IP_PROTO },
5293 { OFPFMF11_TP_SRC, MFF_TCP_SRC },
5294 { OFPFMF11_TP_DST, MFF_TCP_DST },
5295 { OFPFMF11_MPLS_LABEL, MFF_MPLS_LABEL },
5296 { OFPFMF11_MPLS_TC, MFF_MPLS_TC },
5297 /* I don't know what OFPFMF11_TYPE means. */
5298 { OFPFMF11_DL_SRC, MFF_ETH_SRC },
5299 { OFPFMF11_DL_DST, MFF_ETH_DST },
5300 { OFPFMF11_NW_SRC, MFF_IPV4_SRC },
5301 { OFPFMF11_NW_DST, MFF_IPV4_DST },
5302 { OFPFMF11_METADATA, MFF_METADATA },
5306 mf_bitmap_to_of11(const struct mf_bitmap *fields)
5308 const struct ofp11_wc_map *p;
5311 for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
5312 if (bitmap_is_set(fields->bm, p->mf)) {
5319 static struct mf_bitmap
5320 mf_bitmap_from_of11(ovs_be32 wc11_)
5322 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
5323 const struct ofp11_wc_map *p;
5324 uint32_t wc11 = ntohl(wc11_);
5326 for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
5327 if (wc11 & p->wc11) {
5328 bitmap_set1(fields.bm, p->mf);
5335 ofputil_put_ofp11_table_stats(const struct ofputil_table_stats *stats,
5336 const struct ofputil_table_features *features,
5339 struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
5340 struct ofp11_table_stats *out;
5342 out = ofpbuf_put_zeros(buf, sizeof *out);
5343 out->table_id = features->table_id;
5344 ovs_strlcpy(out->name, features->name, sizeof out->name);
5345 out->wildcards = mf_bitmap_to_of11(&wc);
5346 out->match = mf_bitmap_to_of11(&features->match);
5347 out->instructions = ovsinst_bitmap_to_openflow(
5348 features->nonmiss.instructions, OFP11_VERSION);
5349 out->write_actions = ofpact_bitmap_to_openflow(
5350 features->nonmiss.write.ofpacts, OFP11_VERSION);
5351 out->apply_actions = ofpact_bitmap_to_openflow(
5352 features->nonmiss.apply.ofpacts, OFP11_VERSION);
5353 out->config = htonl(features->miss_config);
5354 out->max_entries = htonl(features->max_entries);
5355 out->active_count = htonl(stats->active_count);
5356 out->lookup_count = htonll(stats->lookup_count);
5357 out->matched_count = htonll(stats->matched_count);
5361 ofputil_put_ofp12_table_stats(const struct ofputil_table_stats *stats,
5362 const struct ofputil_table_features *features,
5365 struct ofp12_table_stats *out;
5367 out = ofpbuf_put_zeros(buf, sizeof *out);
5368 out->table_id = features->table_id;
5369 ovs_strlcpy(out->name, features->name, sizeof out->name);
5370 out->match = oxm_bitmap_from_mf_bitmap(&features->match, OFP12_VERSION);
5371 out->wildcards = oxm_bitmap_from_mf_bitmap(&features->wildcard,
5373 out->write_actions = ofpact_bitmap_to_openflow(
5374 features->nonmiss.write.ofpacts, OFP12_VERSION);
5375 out->apply_actions = ofpact_bitmap_to_openflow(
5376 features->nonmiss.apply.ofpacts, OFP12_VERSION);
5377 out->write_setfields = oxm_bitmap_from_mf_bitmap(
5378 &features->nonmiss.write.set_fields, OFP12_VERSION);
5379 out->apply_setfields = oxm_bitmap_from_mf_bitmap(
5380 &features->nonmiss.apply.set_fields, OFP12_VERSION);
5381 out->metadata_match = features->metadata_match;
5382 out->metadata_write = features->metadata_write;
5383 out->instructions = ovsinst_bitmap_to_openflow(
5384 features->nonmiss.instructions, OFP12_VERSION);
5385 out->config = ofputil_table_miss_to_config(features->miss_config,
5387 out->max_entries = htonl(features->max_entries);
5388 out->active_count = htonl(stats->active_count);
5389 out->lookup_count = htonll(stats->lookup_count);
5390 out->matched_count = htonll(stats->matched_count);
5394 ofputil_put_ofp13_table_stats(const struct ofputil_table_stats *stats,
5397 struct ofp13_table_stats *out;
5399 out = ofpbuf_put_zeros(buf, sizeof *out);
5400 out->table_id = stats->table_id;
5401 out->active_count = htonl(stats->active_count);
5402 out->lookup_count = htonll(stats->lookup_count);
5403 out->matched_count = htonll(stats->matched_count);
5407 ofputil_encode_table_stats_reply(const struct ofp_header *request)
5409 return ofpraw_alloc_stats_reply(request, 0);
5413 ofputil_append_table_stats_reply(struct ofpbuf *reply,
5414 const struct ofputil_table_stats *stats,
5415 const struct ofputil_table_features *features)
5417 struct ofp_header *oh = ofpbuf_l2(reply);
5419 ovs_assert(stats->table_id == features->table_id);
5421 switch ((enum ofp_version) oh->version) {
5423 ofputil_put_ofp10_table_stats(stats, features, reply);
5427 ofputil_put_ofp11_table_stats(stats, features, reply);
5431 ofputil_put_ofp12_table_stats(stats, features, reply);
5437 ofputil_put_ofp13_table_stats(stats, reply);
5446 ofputil_decode_ofp10_table_stats(struct ofpbuf *msg,
5447 struct ofputil_table_stats *stats,
5448 struct ofputil_table_features *features)
5450 struct ofp10_table_stats *ots;
5452 ots = ofpbuf_try_pull(msg, sizeof *ots);
5454 return OFPERR_OFPBRC_BAD_LEN;
5457 features->table_id = ots->table_id;
5458 ovs_strlcpy(features->name, ots->name, sizeof features->name);
5459 features->max_entries = ntohl(ots->max_entries);
5460 features->match = features->wildcard = mf_bitmap_from_of10(ots->wildcards);
5462 stats->table_id = ots->table_id;
5463 stats->active_count = ntohl(ots->active_count);
5464 stats->lookup_count = ntohll(get_32aligned_be64(&ots->lookup_count));
5465 stats->matched_count = ntohll(get_32aligned_be64(&ots->matched_count));
5471 ofputil_decode_ofp11_table_stats(struct ofpbuf *msg,
5472 struct ofputil_table_stats *stats,
5473 struct ofputil_table_features *features)
5475 struct ofp11_table_stats *ots;
5477 ots = ofpbuf_try_pull(msg, sizeof *ots);
5479 return OFPERR_OFPBRC_BAD_LEN;
5482 features->table_id = ots->table_id;
5483 ovs_strlcpy(features->name, ots->name, sizeof features->name);
5484 features->max_entries = ntohl(ots->max_entries);
5485 features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
5486 ots->instructions, OFP11_VERSION);
5487 features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
5488 ots->write_actions, OFP11_VERSION);
5489 features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
5490 ots->write_actions, OFP11_VERSION);
5491 features->miss = features->nonmiss;
5492 features->miss_config = ofputil_table_miss_from_config(ots->config,
5494 features->match = mf_bitmap_from_of11(ots->match);
5495 features->wildcard = mf_bitmap_from_of11(ots->wildcards);
5496 bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
5498 stats->table_id = ots->table_id;
5499 stats->active_count = ntohl(ots->active_count);
5500 stats->lookup_count = ntohll(ots->lookup_count);
5501 stats->matched_count = ntohll(ots->matched_count);
5507 ofputil_decode_ofp12_table_stats(struct ofpbuf *msg,
5508 struct ofputil_table_stats *stats,
5509 struct ofputil_table_features *features)
5511 struct ofp12_table_stats *ots;
5513 ots = ofpbuf_try_pull(msg, sizeof *ots);
5515 return OFPERR_OFPBRC_BAD_LEN;
5518 features->table_id = ots->table_id;
5519 ovs_strlcpy(features->name, ots->name, sizeof features->name);
5520 features->metadata_match = ots->metadata_match;
5521 features->metadata_write = ots->metadata_write;
5522 features->miss_config = ofputil_table_miss_from_config(ots->config,
5524 features->max_entries = ntohl(ots->max_entries);
5526 features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
5527 ots->instructions, OFP12_VERSION);
5528 features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
5529 ots->write_actions, OFP12_VERSION);
5530 features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
5531 ots->apply_actions, OFP12_VERSION);
5532 features->nonmiss.write.set_fields = oxm_bitmap_to_mf_bitmap(
5533 ots->write_setfields, OFP12_VERSION);
5534 features->nonmiss.apply.set_fields = oxm_bitmap_to_mf_bitmap(
5535 ots->apply_setfields, OFP12_VERSION);
5536 features->miss = features->nonmiss;
5538 features->match = oxm_bitmap_to_mf_bitmap(ots->match, OFP12_VERSION);
5539 features->wildcard = oxm_bitmap_to_mf_bitmap(ots->wildcards,
5541 bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
5543 stats->table_id = ots->table_id;
5544 stats->active_count = ntohl(ots->active_count);
5545 stats->lookup_count = ntohll(ots->lookup_count);
5546 stats->matched_count = ntohll(ots->matched_count);
5552 ofputil_decode_ofp13_table_stats(struct ofpbuf *msg,
5553 struct ofputil_table_stats *stats,
5554 struct ofputil_table_features *features)
5556 struct ofp13_table_stats *ots;
5558 ots = ofpbuf_try_pull(msg, sizeof *ots);
5560 return OFPERR_OFPBRC_BAD_LEN;
5563 features->table_id = ots->table_id;
5565 stats->table_id = ots->table_id;
5566 stats->active_count = ntohl(ots->active_count);
5567 stats->lookup_count = ntohll(ots->lookup_count);
5568 stats->matched_count = ntohll(ots->matched_count);
5574 ofputil_decode_table_stats_reply(struct ofpbuf *msg,
5575 struct ofputil_table_stats *stats,
5576 struct ofputil_table_features *features)
5578 const struct ofp_header *oh;
5581 ofpraw_pull_assert(msg);
5585 if (!ofpbuf_size(msg)) {
5589 memset(stats, 0, sizeof *stats);
5590 memset(features, 0, sizeof *features);
5592 switch ((enum ofp_version) oh->version) {
5594 return ofputil_decode_ofp10_table_stats(msg, stats, features);
5597 return ofputil_decode_ofp11_table_stats(msg, stats, features);
5600 return ofputil_decode_ofp12_table_stats(msg, stats, features);
5605 return ofputil_decode_ofp13_table_stats(msg, stats, features);
5612 /* ofputil_flow_monitor_request */
5614 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
5615 * ofputil_flow_monitor_request in 'rq'.
5617 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
5618 * message. Calling this function multiple times for a single 'msg' iterates
5619 * through the requests. The caller must initially leave 'msg''s layer
5620 * pointers null and not modify them between calls.
5622 * Returns 0 if successful, EOF if no requests were left in this 'msg',
5623 * otherwise an OFPERR_* value. */
5625 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
5628 struct nx_flow_monitor_request *nfmr;
5632 ofpraw_pull_assert(msg);
5635 if (!ofpbuf_size(msg)) {
5639 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
5641 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %"PRIu32" "
5642 "leftover bytes at end", ofpbuf_size(msg));
5643 return OFPERR_OFPBRC_BAD_LEN;
5646 flags = ntohs(nfmr->flags);
5647 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
5648 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
5649 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
5650 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
5652 return OFPERR_OFPMOFC_BAD_FLAGS;
5655 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
5656 return OFPERR_NXBRC_MUST_BE_ZERO;
5659 rq->id = ntohl(nfmr->id);
5661 rq->out_port = u16_to_ofp(ntohs(nfmr->out_port));
5662 rq->table_id = nfmr->table_id;
5664 return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL, NULL);
5668 ofputil_append_flow_monitor_request(
5669 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
5671 struct nx_flow_monitor_request *nfmr;
5675 if (!ofpbuf_size(msg)) {
5676 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
5679 start_ofs = ofpbuf_size(msg);
5680 ofpbuf_put_zeros(msg, sizeof *nfmr);
5681 match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
5683 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
5684 nfmr->id = htonl(rq->id);
5685 nfmr->flags = htons(rq->flags);
5686 nfmr->out_port = htons(ofp_to_u16(rq->out_port));
5687 nfmr->match_len = htons(match_len);
5688 nfmr->table_id = rq->table_id;
5691 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
5692 * into an abstract ofputil_flow_update in 'update'. The caller must have
5693 * initialized update->match to point to space allocated for a match.
5695 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
5696 * actions (except for NXFME_ABBREV, which never includes actions). The caller
5697 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
5698 * will point into the 'ofpacts' buffer.
5700 * Multiple flow updates can be packed into a single OpenFlow message. Calling
5701 * this function multiple times for a single 'msg' iterates through the
5702 * updates. The caller must initially leave 'msg''s layer pointers null and
5703 * not modify them between calls.
5705 * Returns 0 if successful, EOF if no updates were left in this 'msg',
5706 * otherwise an OFPERR_* value. */
5708 ofputil_decode_flow_update(struct ofputil_flow_update *update,
5709 struct ofpbuf *msg, struct ofpbuf *ofpacts)
5711 struct nx_flow_update_header *nfuh;
5712 unsigned int length;
5713 struct ofp_header *oh;
5716 ofpraw_pull_assert(msg);
5719 if (!ofpbuf_size(msg)) {
5723 if (ofpbuf_size(msg) < sizeof(struct nx_flow_update_header)) {
5729 nfuh = ofpbuf_data(msg);
5730 update->event = ntohs(nfuh->event);
5731 length = ntohs(nfuh->length);
5732 if (length > ofpbuf_size(msg) || length % 8) {
5736 if (update->event == NXFME_ABBREV) {
5737 struct nx_flow_update_abbrev *nfua;
5739 if (length != sizeof *nfua) {
5743 nfua = ofpbuf_pull(msg, sizeof *nfua);
5744 update->xid = nfua->xid;
5746 } else if (update->event == NXFME_ADDED
5747 || update->event == NXFME_DELETED
5748 || update->event == NXFME_MODIFIED) {
5749 struct nx_flow_update_full *nfuf;
5750 unsigned int actions_len;
5751 unsigned int match_len;
5754 if (length < sizeof *nfuf) {
5758 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
5759 match_len = ntohs(nfuf->match_len);
5760 if (sizeof *nfuf + match_len > length) {
5764 update->reason = ntohs(nfuf->reason);
5765 update->idle_timeout = ntohs(nfuf->idle_timeout);
5766 update->hard_timeout = ntohs(nfuf->hard_timeout);
5767 update->table_id = nfuf->table_id;
5768 update->cookie = nfuf->cookie;
5769 update->priority = ntohs(nfuf->priority);
5771 error = nx_pull_match(msg, match_len, update->match, NULL, NULL);
5776 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
5777 error = ofpacts_pull_openflow_actions(msg, actions_len, oh->version,
5783 update->ofpacts = ofpbuf_data(ofpacts);
5784 update->ofpacts_len = ofpbuf_size(ofpacts);
5787 VLOG_WARN_RL(&bad_ofmsg_rl,
5788 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
5789 ntohs(nfuh->event));
5790 return OFPERR_NXBRC_FM_BAD_EVENT;
5794 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %"PRIu32" "
5795 "leftover bytes at end", ofpbuf_size(msg));
5796 return OFPERR_OFPBRC_BAD_LEN;
5800 ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
5802 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
5804 return ntohl(cancel->id);
5808 ofputil_encode_flow_monitor_cancel(uint32_t id)
5810 struct nx_flow_monitor_cancel *nfmc;
5813 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
5814 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
5815 nfmc->id = htonl(id);
5820 ofputil_start_flow_update(struct ovs_list *replies)
5824 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
5828 list_push_back(replies, &msg->list_node);
5832 ofputil_append_flow_update(const struct ofputil_flow_update *update,
5833 struct ovs_list *replies)
5835 enum ofp_version version = ofpmp_version(replies);
5836 struct nx_flow_update_header *nfuh;
5840 msg = ofpbuf_from_list(list_back(replies));
5841 start_ofs = ofpbuf_size(msg);
5843 if (update->event == NXFME_ABBREV) {
5844 struct nx_flow_update_abbrev *nfua;
5846 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
5847 nfua->xid = update->xid;
5849 struct nx_flow_update_full *nfuf;
5852 ofpbuf_put_zeros(msg, sizeof *nfuf);
5853 match_len = nx_put_match(msg, update->match, htonll(0), htonll(0));
5854 ofpacts_put_openflow_actions(update->ofpacts, update->ofpacts_len, msg,
5856 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
5857 nfuf->reason = htons(update->reason);
5858 nfuf->priority = htons(update->priority);
5859 nfuf->idle_timeout = htons(update->idle_timeout);
5860 nfuf->hard_timeout = htons(update->hard_timeout);
5861 nfuf->match_len = htons(match_len);
5862 nfuf->table_id = update->table_id;
5863 nfuf->cookie = update->cookie;
5866 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
5867 nfuh->length = htons(ofpbuf_size(msg) - start_ofs);
5868 nfuh->event = htons(update->event);
5870 ofpmp_postappend(replies, start_ofs);
5874 ofputil_encode_packet_out(const struct ofputil_packet_out *po,
5875 enum ofputil_protocol protocol)
5877 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
5881 size = po->ofpacts_len;
5882 if (po->buffer_id == UINT32_MAX) {
5883 size += po->packet_len;
5886 switch (ofp_version) {
5887 case OFP10_VERSION: {
5888 struct ofp10_packet_out *opo;
5891 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
5892 ofpbuf_put_zeros(msg, sizeof *opo);
5893 actions_ofs = ofpbuf_size(msg);
5894 ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
5897 opo = ofpbuf_l3(msg);
5898 opo->buffer_id = htonl(po->buffer_id);
5899 opo->in_port = htons(ofp_to_u16(po->in_port));
5900 opo->actions_len = htons(ofpbuf_size(msg) - actions_ofs);
5908 case OFP15_VERSION: {
5909 struct ofp11_packet_out *opo;
5912 msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size);
5913 ofpbuf_put_zeros(msg, sizeof *opo);
5914 len = ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
5916 opo = ofpbuf_l3(msg);
5917 opo->buffer_id = htonl(po->buffer_id);
5918 opo->in_port = ofputil_port_to_ofp11(po->in_port);
5919 opo->actions_len = htons(len);
5927 if (po->buffer_id == UINT32_MAX) {
5928 ofpbuf_put(msg, po->packet, po->packet_len);
5931 ofpmsg_update_length(msg);
5936 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
5938 make_echo_request(enum ofp_version ofp_version)
5940 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
5944 /* Creates and returns an OFPT_ECHO_REPLY message matching the
5945 * OFPT_ECHO_REQUEST message in 'rq'. */
5947 make_echo_reply(const struct ofp_header *rq)
5949 struct ofpbuf rq_buf;
5950 struct ofpbuf *reply;
5952 ofpbuf_use_const(&rq_buf, rq, ntohs(rq->length));
5953 ofpraw_pull_assert(&rq_buf);
5955 reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY, rq, ofpbuf_size(&rq_buf));
5956 ofpbuf_put(reply, ofpbuf_data(&rq_buf), ofpbuf_size(&rq_buf));
5961 ofputil_encode_barrier_request(enum ofp_version ofp_version)
5965 switch (ofp_version) {
5971 type = OFPRAW_OFPT11_BARRIER_REQUEST;
5975 type = OFPRAW_OFPT10_BARRIER_REQUEST;
5982 return ofpraw_alloc(type, ofp_version, 0);
5986 ofputil_frag_handling_to_string(enum ofp_config_flags flags)
5988 switch (flags & OFPC_FRAG_MASK) {
5989 case OFPC_FRAG_NORMAL: return "normal";
5990 case OFPC_FRAG_DROP: return "drop";
5991 case OFPC_FRAG_REASM: return "reassemble";
5992 case OFPC_FRAG_NX_MATCH: return "nx-match";
5999 ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
6001 if (!strcasecmp(s, "normal")) {
6002 *flags = OFPC_FRAG_NORMAL;
6003 } else if (!strcasecmp(s, "drop")) {
6004 *flags = OFPC_FRAG_DROP;
6005 } else if (!strcasecmp(s, "reassemble")) {
6006 *flags = OFPC_FRAG_REASM;
6007 } else if (!strcasecmp(s, "nx-match")) {
6008 *flags = OFPC_FRAG_NX_MATCH;
6015 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
6016 * port number and stores the latter in '*ofp10_port', for the purpose of
6017 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
6018 * otherwise an OFPERR_* number. On error, stores OFPP_NONE in '*ofp10_port'.
6020 * See the definition of OFP11_MAX for an explanation of the mapping. */
6022 ofputil_port_from_ofp11(ovs_be32 ofp11_port, ofp_port_t *ofp10_port)
6024 uint32_t ofp11_port_h = ntohl(ofp11_port);
6026 if (ofp11_port_h < ofp_to_u16(OFPP_MAX)) {
6027 *ofp10_port = u16_to_ofp(ofp11_port_h);
6029 } else if (ofp11_port_h >= ofp11_to_u32(OFPP11_MAX)) {
6030 *ofp10_port = u16_to_ofp(ofp11_port_h - OFPP11_OFFSET);
6033 *ofp10_port = OFPP_NONE;
6034 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
6035 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
6036 ofp11_port_h, ofp_to_u16(OFPP_MAX) - 1,
6037 ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
6038 return OFPERR_OFPBAC_BAD_OUT_PORT;
6042 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
6043 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
6045 * See the definition of OFP11_MAX for an explanation of the mapping. */
6047 ofputil_port_to_ofp11(ofp_port_t ofp10_port)
6049 return htonl(ofp_to_u16(ofp10_port) < ofp_to_u16(OFPP_MAX)
6050 ? ofp_to_u16(ofp10_port)
6051 : ofp_to_u16(ofp10_port) + OFPP11_OFFSET);
6054 #define OFPUTIL_NAMED_PORTS \
6055 OFPUTIL_NAMED_PORT(IN_PORT) \
6056 OFPUTIL_NAMED_PORT(TABLE) \
6057 OFPUTIL_NAMED_PORT(NORMAL) \
6058 OFPUTIL_NAMED_PORT(FLOOD) \
6059 OFPUTIL_NAMED_PORT(ALL) \
6060 OFPUTIL_NAMED_PORT(CONTROLLER) \
6061 OFPUTIL_NAMED_PORT(LOCAL) \
6062 OFPUTIL_NAMED_PORT(ANY) \
6063 OFPUTIL_NAMED_PORT(UNSET)
6065 /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */
6066 #define OFPUTIL_NAMED_PORTS_WITH_NONE \
6067 OFPUTIL_NAMED_PORTS \
6068 OFPUTIL_NAMED_PORT(NONE)
6070 /* Stores the port number represented by 's' into '*portp'. 's' may be an
6071 * integer or, for reserved ports, the standard OpenFlow name for the port
6074 * Returns true if successful, false if 's' is not a valid OpenFlow port number
6075 * or name. The caller should issue an error message in this case, because
6076 * this function usually does not. (This gives the caller an opportunity to
6077 * look up the port name another way, e.g. by contacting the switch and listing
6078 * the names of all its ports).
6080 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
6081 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
6082 * range as described in include/openflow/openflow-1.1.h. */
6084 ofputil_port_from_string(const char *s, ofp_port_t *portp)
6086 unsigned int port32; /* int is at least 32 bits wide. */
6089 VLOG_WARN("Negative value %s is not a valid port number.", s);
6093 if (str_to_uint(s, 10, &port32)) {
6094 if (port32 < ofp_to_u16(OFPP_MAX)) {
6096 } else if (port32 < ofp_to_u16(OFPP_FIRST_RESV)) {
6097 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
6098 "be translated to %u when talking to an OF1.1 or "
6099 "later controller", port32, port32 + OFPP11_OFFSET);
6100 } else if (port32 <= ofp_to_u16(OFPP_LAST_RESV)) {
6101 char name[OFP_MAX_PORT_NAME_LEN];
6103 ofputil_port_to_string(u16_to_ofp(port32), name, sizeof name);
6104 VLOG_WARN_ONCE("referring to port %s as %"PRIu32" is deprecated "
6105 "for compatibility with OpenFlow 1.1 and later",
6107 } else if (port32 < ofp11_to_u32(OFPP11_MAX)) {
6108 VLOG_WARN("port %u is outside the supported range 0 through "
6109 "%"PRIx16" or 0x%x through 0x%"PRIx32, port32,
6110 UINT16_MAX, ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
6113 port32 -= OFPP11_OFFSET;
6116 *portp = u16_to_ofp(port32);
6123 static const struct pair pairs[] = {
6124 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
6125 OFPUTIL_NAMED_PORTS_WITH_NONE
6126 #undef OFPUTIL_NAMED_PORT
6128 const struct pair *p;
6130 for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) {
6131 if (!strcasecmp(s, p->name)) {
6140 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
6141 * Most ports' string representation is just the port number, but for special
6142 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
6144 ofputil_format_port(ofp_port_t port, struct ds *s)
6146 char name[OFP_MAX_PORT_NAME_LEN];
6148 ofputil_port_to_string(port, name, sizeof name);
6149 ds_put_cstr(s, name);
6152 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
6153 * representation of OpenFlow port number 'port'. Most ports are represented
6154 * as just the port number, but special ports, e.g. OFPP_LOCAL, are represented
6155 * by name, e.g. "LOCAL". */
6157 ofputil_port_to_string(ofp_port_t port,
6158 char namebuf[OFP_MAX_PORT_NAME_LEN], size_t bufsize)
6161 #define OFPUTIL_NAMED_PORT(NAME) \
6163 ovs_strlcpy(namebuf, #NAME, bufsize); \
6166 #undef OFPUTIL_NAMED_PORT
6169 snprintf(namebuf, bufsize, "%"PRIu16, port);
6174 /* Stores the group id represented by 's' into '*group_idp'. 's' may be an
6175 * integer or, for reserved group IDs, the standard OpenFlow name for the group
6176 * (either "ANY" or "ALL").
6178 * Returns true if successful, false if 's' is not a valid OpenFlow group ID or
6181 ofputil_group_from_string(const char *s, uint32_t *group_idp)
6183 if (!strcasecmp(s, "any")) {
6184 *group_idp = OFPG11_ANY;
6185 } else if (!strcasecmp(s, "all")) {
6186 *group_idp = OFPG11_ALL;
6187 } else if (!str_to_uint(s, 10, group_idp)) {
6188 VLOG_WARN("%s is not a valid group ID. (Valid group IDs are "
6189 "32-bit nonnegative integers or the keywords ANY or "
6197 /* Appends to 's' a string representation of the OpenFlow group ID 'group_id'.
6198 * Most groups' string representation is just the number, but for special
6199 * groups, e.g. OFPG11_ALL, it is the name, e.g. "ALL". */
6201 ofputil_format_group(uint32_t group_id, struct ds *s)
6203 char name[MAX_GROUP_NAME_LEN];
6205 ofputil_group_to_string(group_id, name, sizeof name);
6206 ds_put_cstr(s, name);
6210 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
6211 * representation of OpenFlow group ID 'group_id'. Most group are represented
6212 * as just their number, but special groups, e.g. OFPG11_ALL, are represented
6213 * by name, e.g. "ALL". */
6215 ofputil_group_to_string(uint32_t group_id,
6216 char namebuf[MAX_GROUP_NAME_LEN + 1], size_t bufsize)
6220 ovs_strlcpy(namebuf, "ALL", bufsize);
6224 ovs_strlcpy(namebuf, "ANY", bufsize);
6228 snprintf(namebuf, bufsize, "%"PRIu32, group_id);
6233 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
6234 * 'ofp_version', tries to pull the first element from the array. If
6235 * successful, initializes '*pp' with an abstract representation of the
6236 * port and returns 0. If no ports remain to be decoded, returns EOF.
6237 * On an error, returns a positive OFPERR_* value. */
6239 ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
6240 struct ofputil_phy_port *pp)
6242 memset(pp, 0, sizeof *pp);
6244 switch (ofp_version) {
6245 case OFP10_VERSION: {
6246 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
6247 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
6251 case OFP13_VERSION: {
6252 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
6253 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
6257 return ofpbuf_size(b) ? ofputil_pull_ofp14_port(pp, b) : EOF;
6264 ofputil_normalize_match__(struct match *match, bool may_log)
6267 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
6268 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
6269 MAY_NW_PROTO = 1 << 2, /* nw_proto */
6270 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
6271 MAY_ARP_SHA = 1 << 4, /* arp_sha */
6272 MAY_ARP_THA = 1 << 5, /* arp_tha */
6273 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
6274 MAY_ND_TARGET = 1 << 7, /* nd_target */
6275 MAY_MPLS = 1 << 8, /* mpls label and tc */
6278 struct flow_wildcards wc;
6280 /* Figure out what fields may be matched. */
6281 if (match->flow.dl_type == htons(ETH_TYPE_IP)) {
6282 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
6283 if (match->flow.nw_proto == IPPROTO_TCP ||
6284 match->flow.nw_proto == IPPROTO_UDP ||
6285 match->flow.nw_proto == IPPROTO_SCTP ||
6286 match->flow.nw_proto == IPPROTO_ICMP) {
6287 may_match |= MAY_TP_ADDR;
6289 } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
6290 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
6291 if (match->flow.nw_proto == IPPROTO_TCP ||
6292 match->flow.nw_proto == IPPROTO_UDP ||
6293 match->flow.nw_proto == IPPROTO_SCTP) {
6294 may_match |= MAY_TP_ADDR;
6295 } else if (match->flow.nw_proto == IPPROTO_ICMPV6) {
6296 may_match |= MAY_TP_ADDR;
6297 if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
6298 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
6299 } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
6300 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
6303 } else if (match->flow.dl_type == htons(ETH_TYPE_ARP) ||
6304 match->flow.dl_type == htons(ETH_TYPE_RARP)) {
6305 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
6306 } else if (eth_type_mpls(match->flow.dl_type)) {
6307 may_match = MAY_MPLS;
6312 /* Clear the fields that may not be matched. */
6314 if (!(may_match & MAY_NW_ADDR)) {
6315 wc.masks.nw_src = wc.masks.nw_dst = htonl(0);
6317 if (!(may_match & MAY_TP_ADDR)) {
6318 wc.masks.tp_src = wc.masks.tp_dst = htons(0);
6320 if (!(may_match & MAY_NW_PROTO)) {
6321 wc.masks.nw_proto = 0;
6323 if (!(may_match & MAY_IPVx)) {
6324 wc.masks.nw_tos = 0;
6325 wc.masks.nw_ttl = 0;
6327 if (!(may_match & MAY_ARP_SHA)) {
6328 memset(wc.masks.arp_sha, 0, ETH_ADDR_LEN);
6330 if (!(may_match & MAY_ARP_THA)) {
6331 memset(wc.masks.arp_tha, 0, ETH_ADDR_LEN);
6333 if (!(may_match & MAY_IPV6)) {
6334 wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any;
6335 wc.masks.ipv6_label = htonl(0);
6337 if (!(may_match & MAY_ND_TARGET)) {
6338 wc.masks.nd_target = in6addr_any;
6340 if (!(may_match & MAY_MPLS)) {
6341 memset(wc.masks.mpls_lse, 0, sizeof wc.masks.mpls_lse);
6344 /* Log any changes. */
6345 if (!flow_wildcards_equal(&wc, &match->wc)) {
6346 bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
6347 char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL;
6350 match_zero_wildcarded_fields(match);
6353 char *post = match_to_string(match, OFP_DEFAULT_PRIORITY);
6354 VLOG_INFO("normalization changed ofp_match, details:");
6355 VLOG_INFO(" pre: %s", pre);
6356 VLOG_INFO("post: %s", post);
6363 /* "Normalizes" the wildcards in 'match'. That means:
6365 * 1. If the type of level N is known, then only the valid fields for that
6366 * level may be specified. For example, ARP does not have a TOS field,
6367 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
6368 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
6369 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
6372 * 2. If the type of level N is not known (or not understood by Open
6373 * vSwitch), then no fields at all for that level may be specified. For
6374 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
6375 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
6378 * If this function changes 'match', it logs a rate-limited informational
6381 ofputil_normalize_match(struct match *match)
6383 ofputil_normalize_match__(match, true);
6386 /* Same as ofputil_normalize_match() without the logging. Thus, this function
6387 * is suitable for a program's internal use, whereas ofputil_normalize_match()
6388 * sense for use on flows received from elsewhere (so that a bug in the program
6389 * that sent them can be reported and corrected). */
6391 ofputil_normalize_match_quiet(struct match *match)
6393 ofputil_normalize_match__(match, false);
6396 /* Parses a key or a key-value pair from '*stringp'.
6398 * On success: Stores the key into '*keyp'. Stores the value, if present, into
6399 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
6400 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
6401 * are substrings of '*stringp' created by replacing some of its bytes by null
6402 * terminators. Returns true.
6404 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
6405 * NULL and returns false. */
6407 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
6409 char *pos, *key, *value;
6413 pos += strspn(pos, ", \t\r\n");
6415 *keyp = *valuep = NULL;
6420 key_len = strcspn(pos, ":=(, \t\r\n");
6421 if (key[key_len] == ':' || key[key_len] == '=') {
6422 /* The value can be separated by a colon. */
6425 value = key + key_len + 1;
6426 value_len = strcspn(value, ", \t\r\n");
6427 pos = value + value_len + (value[value_len] != '\0');
6428 value[value_len] = '\0';
6429 } else if (key[key_len] == '(') {
6430 /* The value can be surrounded by balanced parentheses. The outermost
6431 * set of parentheses is removed. */
6435 value = key + key_len + 1;
6436 for (value_len = 0; level > 0; value_len++) {
6437 switch (value[value_len]) {
6451 value[value_len - 1] = '\0';
6452 pos = value + value_len;
6454 /* There might be no value at all. */
6455 value = key + key_len; /* Will become the empty string below. */
6456 pos = key + key_len + (key[key_len] != '\0');
6458 key[key_len] = '\0';
6466 /* Encode a dump ports request for 'port', the encoded message
6467 * will be for OpenFlow version 'ofp_version'. Returns message
6468 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
6470 ofputil_encode_dump_ports_request(enum ofp_version ofp_version, ofp_port_t port)
6472 struct ofpbuf *request;
6474 switch (ofp_version) {
6475 case OFP10_VERSION: {
6476 struct ofp10_port_stats_request *req;
6477 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0);
6478 req = ofpbuf_put_zeros(request, sizeof *req);
6479 req->port_no = htons(ofp_to_u16(port));
6486 case OFP15_VERSION: {
6487 struct ofp11_port_stats_request *req;
6488 request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0);
6489 req = ofpbuf_put_zeros(request, sizeof *req);
6490 req->port_no = ofputil_port_to_ofp11(port);
6501 ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops,
6502 struct ofp10_port_stats *ps10)
6504 ps10->port_no = htons(ofp_to_u16(ops->port_no));
6505 memset(ps10->pad, 0, sizeof ps10->pad);
6506 put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets));
6507 put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets));
6508 put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes));
6509 put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes));
6510 put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped));
6511 put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped));
6512 put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors));
6513 put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors));
6514 put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors));
6515 put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors));
6516 put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors));
6517 put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions));
6521 ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops,
6522 struct ofp11_port_stats *ps11)
6524 ps11->port_no = ofputil_port_to_ofp11(ops->port_no);
6525 memset(ps11->pad, 0, sizeof ps11->pad);
6526 ps11->rx_packets = htonll(ops->stats.rx_packets);
6527 ps11->tx_packets = htonll(ops->stats.tx_packets);
6528 ps11->rx_bytes = htonll(ops->stats.rx_bytes);
6529 ps11->tx_bytes = htonll(ops->stats.tx_bytes);
6530 ps11->rx_dropped = htonll(ops->stats.rx_dropped);
6531 ps11->tx_dropped = htonll(ops->stats.tx_dropped);
6532 ps11->rx_errors = htonll(ops->stats.rx_errors);
6533 ps11->tx_errors = htonll(ops->stats.tx_errors);
6534 ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors);
6535 ps11->rx_over_err = htonll(ops->stats.rx_over_errors);
6536 ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors);
6537 ps11->collisions = htonll(ops->stats.collisions);
6541 ofputil_port_stats_to_ofp13(const struct ofputil_port_stats *ops,
6542 struct ofp13_port_stats *ps13)
6544 ofputil_port_stats_to_ofp11(ops, &ps13->ps);
6545 ps13->duration_sec = htonl(ops->duration_sec);
6546 ps13->duration_nsec = htonl(ops->duration_nsec);
6550 ofputil_append_ofp14_port_stats(const struct ofputil_port_stats *ops,
6551 struct ovs_list *replies)
6553 struct ofp14_port_stats_prop_ethernet *eth;
6554 struct ofp14_port_stats *ps14;
6555 struct ofpbuf *reply;
6557 reply = ofpmp_reserve(replies, sizeof *ps14 + sizeof *eth);
6559 ps14 = ofpbuf_put_uninit(reply, sizeof *ps14);
6560 ps14->length = htons(sizeof *ps14 + sizeof *eth);
6561 memset(ps14->pad, 0, sizeof ps14->pad);
6562 ps14->port_no = ofputil_port_to_ofp11(ops->port_no);
6563 ps14->duration_sec = htonl(ops->duration_sec);
6564 ps14->duration_nsec = htonl(ops->duration_nsec);
6565 ps14->rx_packets = htonll(ops->stats.rx_packets);
6566 ps14->tx_packets = htonll(ops->stats.tx_packets);
6567 ps14->rx_bytes = htonll(ops->stats.rx_bytes);
6568 ps14->tx_bytes = htonll(ops->stats.tx_bytes);
6569 ps14->rx_dropped = htonll(ops->stats.rx_dropped);
6570 ps14->tx_dropped = htonll(ops->stats.tx_dropped);
6571 ps14->rx_errors = htonll(ops->stats.rx_errors);
6572 ps14->tx_errors = htonll(ops->stats.tx_errors);
6574 eth = ofpbuf_put_uninit(reply, sizeof *eth);
6575 eth->type = htons(OFPPSPT14_ETHERNET);
6576 eth->length = htons(sizeof *eth);
6577 memset(eth->pad, 0, sizeof eth->pad);
6578 eth->rx_frame_err = htonll(ops->stats.rx_frame_errors);
6579 eth->rx_over_err = htonll(ops->stats.rx_over_errors);
6580 eth->rx_crc_err = htonll(ops->stats.rx_crc_errors);
6581 eth->collisions = htonll(ops->stats.collisions);
6584 /* Encode a ports stat for 'ops' and append it to 'replies'. */
6586 ofputil_append_port_stat(struct ovs_list *replies,
6587 const struct ofputil_port_stats *ops)
6589 switch (ofpmp_version(replies)) {
6590 case OFP13_VERSION: {
6591 struct ofp13_port_stats *reply = ofpmp_append(replies, sizeof *reply);
6592 ofputil_port_stats_to_ofp13(ops, reply);
6596 case OFP11_VERSION: {
6597 struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply);
6598 ofputil_port_stats_to_ofp11(ops, reply);
6602 case OFP10_VERSION: {
6603 struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply);
6604 ofputil_port_stats_to_ofp10(ops, reply);
6610 ofputil_append_ofp14_port_stats(ops, replies);
6619 ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops,
6620 const struct ofp10_port_stats *ps10)
6622 memset(ops, 0, sizeof *ops);
6624 ops->port_no = u16_to_ofp(ntohs(ps10->port_no));
6625 ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets));
6626 ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets));
6627 ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes));
6628 ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes));
6629 ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped));
6630 ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped));
6631 ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors));
6632 ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors));
6633 ops->stats.rx_frame_errors =
6634 ntohll(get_32aligned_be64(&ps10->rx_frame_err));
6635 ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err));
6636 ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err));
6637 ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions));
6638 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
6644 ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops,
6645 const struct ofp11_port_stats *ps11)
6649 memset(ops, 0, sizeof *ops);
6650 error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no);
6655 ops->stats.rx_packets = ntohll(ps11->rx_packets);
6656 ops->stats.tx_packets = ntohll(ps11->tx_packets);
6657 ops->stats.rx_bytes = ntohll(ps11->rx_bytes);
6658 ops->stats.tx_bytes = ntohll(ps11->tx_bytes);
6659 ops->stats.rx_dropped = ntohll(ps11->rx_dropped);
6660 ops->stats.tx_dropped = ntohll(ps11->tx_dropped);
6661 ops->stats.rx_errors = ntohll(ps11->rx_errors);
6662 ops->stats.tx_errors = ntohll(ps11->tx_errors);
6663 ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err);
6664 ops->stats.rx_over_errors = ntohll(ps11->rx_over_err);
6665 ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err);
6666 ops->stats.collisions = ntohll(ps11->collisions);
6667 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
6673 ofputil_port_stats_from_ofp13(struct ofputil_port_stats *ops,
6674 const struct ofp13_port_stats *ps13)
6676 enum ofperr error = ofputil_port_stats_from_ofp11(ops, &ps13->ps);
6678 ops->duration_sec = ntohl(ps13->duration_sec);
6679 ops->duration_nsec = ntohl(ps13->duration_nsec);
6685 parse_ofp14_port_stats_ethernet_property(const struct ofpbuf *payload,
6686 struct ofputil_port_stats *ops)
6688 const struct ofp14_port_stats_prop_ethernet *eth = ofpbuf_data(payload);
6690 if (ofpbuf_size(payload) != sizeof *eth) {
6691 return OFPERR_OFPBPC_BAD_LEN;
6694 ops->stats.rx_frame_errors = ntohll(eth->rx_frame_err);
6695 ops->stats.rx_over_errors = ntohll(eth->rx_over_err);
6696 ops->stats.rx_crc_errors = ntohll(eth->rx_crc_err);
6697 ops->stats.collisions = ntohll(eth->collisions);
6703 ofputil_pull_ofp14_port_stats(struct ofputil_port_stats *ops,
6706 const struct ofp14_port_stats *ps14;
6707 struct ofpbuf properties;
6711 ps14 = ofpbuf_try_pull(msg, sizeof *ps14);
6713 return OFPERR_OFPBRC_BAD_LEN;
6716 len = ntohs(ps14->length);
6717 if (len < sizeof *ps14 || len - sizeof *ps14 > ofpbuf_size(msg)) {
6718 return OFPERR_OFPBRC_BAD_LEN;
6720 len -= sizeof *ps14;
6721 ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len);
6723 error = ofputil_port_from_ofp11(ps14->port_no, &ops->port_no);
6728 ops->duration_sec = ntohl(ps14->duration_sec);
6729 ops->duration_nsec = ntohl(ps14->duration_nsec);
6730 ops->stats.rx_packets = ntohll(ps14->rx_packets);
6731 ops->stats.tx_packets = ntohll(ps14->tx_packets);
6732 ops->stats.rx_bytes = ntohll(ps14->rx_bytes);
6733 ops->stats.tx_bytes = ntohll(ps14->tx_bytes);
6734 ops->stats.rx_dropped = ntohll(ps14->rx_dropped);
6735 ops->stats.tx_dropped = ntohll(ps14->tx_dropped);
6736 ops->stats.rx_errors = ntohll(ps14->rx_errors);
6737 ops->stats.tx_errors = ntohll(ps14->tx_errors);
6738 ops->stats.rx_frame_errors = UINT64_MAX;
6739 ops->stats.rx_over_errors = UINT64_MAX;
6740 ops->stats.rx_crc_errors = UINT64_MAX;
6741 ops->stats.collisions = UINT64_MAX;
6743 while (ofpbuf_size(&properties) > 0) {
6744 struct ofpbuf payload;
6748 error = ofputil_pull_property(&properties, &payload, &type);
6754 case OFPPSPT14_ETHERNET:
6755 error = parse_ofp14_port_stats_ethernet_property(&payload, ops);
6759 log_property(true, "unknown port stats property %"PRIu16, type);
6772 /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
6775 ofputil_count_port_stats(const struct ofp_header *oh)
6777 struct ofputil_port_stats ps;
6781 ofpbuf_use_const(&b, oh, ntohs(oh->length));
6782 ofpraw_pull_assert(&b);
6783 while (!ofputil_decode_port_stats(&ps, &b)) {
6789 /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
6790 * ofputil_port_stats in 'ps'.
6792 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
6793 * message. Calling this function multiple times for a single 'msg' iterates
6794 * through the replies. The caller must initially leave 'msg''s layer pointers
6795 * null and not modify them between calls.
6797 * Returns 0 if successful, EOF if no replies were left in this 'msg',
6798 * otherwise a positive errno value. */
6800 ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg)
6806 ? ofpraw_decode(&raw, msg->frame)
6807 : ofpraw_pull(&raw, msg));
6812 if (!ofpbuf_size(msg)) {
6814 } else if (raw == OFPRAW_OFPST14_PORT_REPLY) {
6815 return ofputil_pull_ofp14_port_stats(ps, msg);
6816 } else if (raw == OFPRAW_OFPST13_PORT_REPLY) {
6817 const struct ofp13_port_stats *ps13;
6819 ps13 = ofpbuf_try_pull(msg, sizeof *ps13);
6823 return ofputil_port_stats_from_ofp13(ps, ps13);
6824 } else if (raw == OFPRAW_OFPST11_PORT_REPLY) {
6825 const struct ofp11_port_stats *ps11;
6827 ps11 = ofpbuf_try_pull(msg, sizeof *ps11);
6831 return ofputil_port_stats_from_ofp11(ps, ps11);
6832 } else if (raw == OFPRAW_OFPST10_PORT_REPLY) {
6833 const struct ofp10_port_stats *ps10;
6835 ps10 = ofpbuf_try_pull(msg, sizeof *ps10);
6839 return ofputil_port_stats_from_ofp10(ps, ps10);
6845 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %"PRIu32" leftover "
6846 "bytes at end", ofpbuf_size(msg));
6847 return OFPERR_OFPBRC_BAD_LEN;
6850 /* Parse a port status request message into a 16 bit OpenFlow 1.0
6851 * port number and stores the latter in '*ofp10_port'.
6852 * Returns 0 if successful, otherwise an OFPERR_* number. */
6854 ofputil_decode_port_stats_request(const struct ofp_header *request,
6855 ofp_port_t *ofp10_port)
6857 switch ((enum ofp_version)request->version) {
6862 case OFP11_VERSION: {
6863 const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request);
6864 return ofputil_port_from_ofp11(psr11->port_no, ofp10_port);
6867 case OFP10_VERSION: {
6868 const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request);
6869 *ofp10_port = u16_to_ofp(ntohs(psr10->port_no));
6878 /* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */
6880 ofputil_bucket_list_destroy(struct ovs_list *buckets)
6882 struct ofputil_bucket *bucket, *next_bucket;
6884 LIST_FOR_EACH_SAFE (bucket, next_bucket, list_node, buckets) {
6885 list_remove(&bucket->list_node);
6886 free(bucket->ofpacts);
6891 /* Clones 'bucket' and its ofpacts data */
6892 static struct ofputil_bucket *
6893 ofputil_bucket_clone_data(const struct ofputil_bucket *bucket)
6895 struct ofputil_bucket *new;
6897 new = xmemdup(bucket, sizeof *bucket);
6898 new->ofpacts = xmemdup(bucket->ofpacts, bucket->ofpacts_len);
6903 /* Clones each of the buckets in the list 'src' appending them
6904 * in turn to 'dest' which should be an initialised list.
6905 * An exception is that if the pointer value of a bucket in 'src'
6906 * matches 'skip' then it is not cloned or appended to 'dest'.
6907 * This allows all of 'src' or 'all of 'src' except 'skip' to
6908 * be cloned and appended to 'dest'. */
6910 ofputil_bucket_clone_list(struct ovs_list *dest, const struct ovs_list *src,
6911 const struct ofputil_bucket *skip)
6913 struct ofputil_bucket *bucket;
6915 LIST_FOR_EACH (bucket, list_node, src) {
6916 struct ofputil_bucket *new_bucket;
6918 if (bucket == skip) {
6922 new_bucket = ofputil_bucket_clone_data(bucket);
6923 list_push_back(dest, &new_bucket->list_node);
6927 /* Find a bucket in the list 'buckets' whose bucket id is 'bucket_id'
6928 * Returns the first bucket found or NULL if no buckets are found. */
6929 struct ofputil_bucket *
6930 ofputil_bucket_find(const struct ovs_list *buckets, uint32_t bucket_id)
6932 struct ofputil_bucket *bucket;
6934 if (bucket_id > OFPG15_BUCKET_MAX) {
6938 LIST_FOR_EACH (bucket, list_node, buckets) {
6939 if (bucket->bucket_id == bucket_id) {
6947 /* Returns true if more than one bucket in the list 'buckets'
6948 * have the same bucket id. Returns false otherwise. */
6950 ofputil_bucket_check_duplicate_id(const struct ovs_list *buckets)
6952 struct ofputil_bucket *i, *j;
6954 LIST_FOR_EACH (i, list_node, buckets) {
6955 LIST_FOR_EACH_REVERSE (j, list_node, buckets) {
6959 if (i->bucket_id == j->bucket_id) {
6968 /* Returns the bucket at the front of the list 'buckets'.
6969 * Undefined if 'buckets is empty. */
6970 struct ofputil_bucket *
6971 ofputil_bucket_list_front(const struct ovs_list *buckets)
6973 static struct ofputil_bucket *bucket;
6975 ASSIGN_CONTAINER(bucket, list_front(buckets), list_node);
6980 /* Returns the bucket at the back of the list 'buckets'.
6981 * Undefined if 'buckets is empty. */
6982 struct ofputil_bucket *
6983 ofputil_bucket_list_back(const struct ovs_list *buckets)
6985 static struct ofputil_bucket *bucket;
6987 ASSIGN_CONTAINER(bucket, list_back(buckets), list_node);
6992 /* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version',
6993 * that requests stats for group 'group_id'. (Use OFPG_ALL to request stats
6996 * Group statistics include packet and byte counts for each group. */
6998 ofputil_encode_group_stats_request(enum ofp_version ofp_version,
7001 struct ofpbuf *request;
7003 switch (ofp_version) {
7005 ovs_fatal(0, "dump-group-stats needs OpenFlow 1.1 or later "
7006 "(\'-O OpenFlow11\')");
7011 case OFP15_VERSION: {
7012 struct ofp11_group_stats_request *req;
7013 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_REQUEST, ofp_version, 0);
7014 req = ofpbuf_put_zeros(request, sizeof *req);
7015 req->group_id = htonl(group_id);
7025 /* Decodes the OpenFlow group description request in 'oh', returning the group
7026 * whose description is requested, or OFPG_ALL if stats for all groups was
7029 ofputil_decode_group_desc_request(const struct ofp_header *oh)
7031 struct ofpbuf request;
7034 ofpbuf_use_const(&request, oh, ntohs(oh->length));
7035 raw = ofpraw_pull_assert(&request);
7036 if (raw == OFPRAW_OFPST11_GROUP_DESC_REQUEST) {
7038 } else if (raw == OFPRAW_OFPST15_GROUP_DESC_REQUEST) {
7039 ovs_be32 *group_id = ofpbuf_pull(&request, sizeof *group_id);
7040 return ntohl(*group_id);
7046 /* Returns an OpenFlow group description request for OpenFlow version
7047 * 'ofp_version', that requests stats for group 'group_id'. Use OFPG_ALL to
7048 * request stats for all groups (OpenFlow 1.4 and earlier always request all
7051 * Group descriptions include the bucket and action configuration for each
7054 ofputil_encode_group_desc_request(enum ofp_version ofp_version,
7057 struct ofpbuf *request;
7060 switch (ofp_version) {
7062 ovs_fatal(0, "dump-groups needs OpenFlow 1.1 or later "
7063 "(\'-O OpenFlow11\')");
7068 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_DESC_REQUEST,
7072 request = ofpraw_alloc(OFPRAW_OFPST15_GROUP_DESC_REQUEST,
7074 gid = htonl(group_id);
7075 ofpbuf_put(request, &gid, sizeof gid);
7085 ofputil_group_bucket_counters_to_ofp11(const struct ofputil_group_stats *gs,
7086 struct ofp11_bucket_counter bucket_cnts[])
7090 for (i = 0; i < gs->n_buckets; i++) {
7091 bucket_cnts[i].packet_count = htonll(gs->bucket_stats[i].packet_count);
7092 bucket_cnts[i].byte_count = htonll(gs->bucket_stats[i].byte_count);
7097 ofputil_group_stats_to_ofp11(const struct ofputil_group_stats *gs,
7098 struct ofp11_group_stats *gs11, size_t length,
7099 struct ofp11_bucket_counter bucket_cnts[])
7101 memset(gs11, 0, sizeof *gs11);
7102 gs11->length = htons(length);
7103 gs11->group_id = htonl(gs->group_id);
7104 gs11->ref_count = htonl(gs->ref_count);
7105 gs11->packet_count = htonll(gs->packet_count);
7106 gs11->byte_count = htonll(gs->byte_count);
7107 ofputil_group_bucket_counters_to_ofp11(gs, bucket_cnts);
7111 ofputil_group_stats_to_ofp13(const struct ofputil_group_stats *gs,
7112 struct ofp13_group_stats *gs13, size_t length,
7113 struct ofp11_bucket_counter bucket_cnts[])
7115 ofputil_group_stats_to_ofp11(gs, &gs13->gs, length, bucket_cnts);
7116 gs13->duration_sec = htonl(gs->duration_sec);
7117 gs13->duration_nsec = htonl(gs->duration_nsec);
7121 /* Encodes 'gs' properly for the format of the list of group statistics
7122 * replies already begun in 'replies' and appends it to the list. 'replies'
7123 * must have originally been initialized with ofpmp_init(). */
7125 ofputil_append_group_stats(struct ovs_list *replies,
7126 const struct ofputil_group_stats *gs)
7128 size_t bucket_counter_size;
7129 struct ofp11_bucket_counter *bucket_counters;
7132 bucket_counter_size = gs->n_buckets * sizeof(struct ofp11_bucket_counter);
7134 switch (ofpmp_version(replies)) {
7136 case OFP12_VERSION:{
7137 struct ofp11_group_stats *gs11;
7139 length = sizeof *gs11 + bucket_counter_size;
7140 gs11 = ofpmp_append(replies, length);
7141 bucket_counters = (struct ofp11_bucket_counter *)(gs11 + 1);
7142 ofputil_group_stats_to_ofp11(gs, gs11, length, bucket_counters);
7148 case OFP15_VERSION: {
7149 struct ofp13_group_stats *gs13;
7151 length = sizeof *gs13 + bucket_counter_size;
7152 gs13 = ofpmp_append(replies, length);
7153 bucket_counters = (struct ofp11_bucket_counter *)(gs13 + 1);
7154 ofputil_group_stats_to_ofp13(gs, gs13, length, bucket_counters);
7163 /* Returns an OpenFlow group features request for OpenFlow version
7166 ofputil_encode_group_features_request(enum ofp_version ofp_version)
7168 struct ofpbuf *request = NULL;
7170 switch (ofp_version) {
7173 ovs_fatal(0, "dump-group-features needs OpenFlow 1.2 or later "
7174 "(\'-O OpenFlow12\')");
7179 request = ofpraw_alloc(OFPRAW_OFPST12_GROUP_FEATURES_REQUEST,
7189 /* Returns a OpenFlow message that encodes 'features' properly as a reply to
7190 * group features request 'request'. */
7192 ofputil_encode_group_features_reply(
7193 const struct ofputil_group_features *features,
7194 const struct ofp_header *request)
7196 struct ofp12_group_features_stats *ogf;
7197 struct ofpbuf *reply;
7200 reply = ofpraw_alloc_xid(OFPRAW_OFPST12_GROUP_FEATURES_REPLY,
7201 request->version, request->xid, 0);
7202 ogf = ofpbuf_put_zeros(reply, sizeof *ogf);
7203 ogf->types = htonl(features->types);
7204 ogf->capabilities = htonl(features->capabilities);
7205 for (i = 0; i < OFPGT12_N_TYPES; i++) {
7206 ogf->max_groups[i] = htonl(features->max_groups[i]);
7207 ogf->actions[i] = ofpact_bitmap_to_openflow(features->ofpacts[i],
7214 /* Decodes group features reply 'oh' into 'features'. */
7216 ofputil_decode_group_features_reply(const struct ofp_header *oh,
7217 struct ofputil_group_features *features)
7219 const struct ofp12_group_features_stats *ogf = ofpmsg_body(oh);
7222 features->types = ntohl(ogf->types);
7223 features->capabilities = ntohl(ogf->capabilities);
7224 for (i = 0; i < OFPGT12_N_TYPES; i++) {
7225 features->max_groups[i] = ntohl(ogf->max_groups[i]);
7226 features->ofpacts[i] = ofpact_bitmap_from_openflow(
7227 ogf->actions[i], oh->version);
7231 /* Parse a group status request message into a 32 bit OpenFlow 1.1
7232 * group ID and stores the latter in '*group_id'.
7233 * Returns 0 if successful, otherwise an OFPERR_* number. */
7235 ofputil_decode_group_stats_request(const struct ofp_header *request,
7238 const struct ofp11_group_stats_request *gsr11 = ofpmsg_body(request);
7239 *group_id = ntohl(gsr11->group_id);
7243 /* Converts a group stats reply in 'msg' into an abstract ofputil_group_stats
7244 * in 'gs'. Assigns freshly allocated memory to gs->bucket_stats for the
7245 * caller to eventually free.
7247 * Multiple group stats replies can be packed into a single OpenFlow message.
7248 * Calling this function multiple times for a single 'msg' iterates through the
7249 * replies. The caller must initially leave 'msg''s layer pointers null and
7250 * not modify them between calls.
7252 * Returns 0 if successful, EOF if no replies were left in this 'msg',
7253 * otherwise a positive errno value. */
7255 ofputil_decode_group_stats_reply(struct ofpbuf *msg,
7256 struct ofputil_group_stats *gs)
7258 struct ofp11_bucket_counter *obc;
7259 struct ofp11_group_stats *ogs11;
7266 gs->bucket_stats = NULL;
7268 ? ofpraw_decode(&raw, msg->frame)
7269 : ofpraw_pull(&raw, msg));
7274 if (!ofpbuf_size(msg)) {
7278 if (raw == OFPRAW_OFPST11_GROUP_REPLY) {
7279 base_len = sizeof *ogs11;
7280 ogs11 = ofpbuf_try_pull(msg, sizeof *ogs11);
7281 gs->duration_sec = gs->duration_nsec = UINT32_MAX;
7282 } else if (raw == OFPRAW_OFPST13_GROUP_REPLY) {
7283 struct ofp13_group_stats *ogs13;
7285 base_len = sizeof *ogs13;
7286 ogs13 = ofpbuf_try_pull(msg, sizeof *ogs13);
7289 gs->duration_sec = ntohl(ogs13->duration_sec);
7290 gs->duration_nsec = ntohl(ogs13->duration_nsec);
7299 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
7300 ofpraw_get_name(raw), ofpbuf_size(msg));
7301 return OFPERR_OFPBRC_BAD_LEN;
7303 length = ntohs(ogs11->length);
7304 if (length < sizeof base_len) {
7305 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply claims invalid length %"PRIuSIZE,
7306 ofpraw_get_name(raw), length);
7307 return OFPERR_OFPBRC_BAD_LEN;
7310 gs->group_id = ntohl(ogs11->group_id);
7311 gs->ref_count = ntohl(ogs11->ref_count);
7312 gs->packet_count = ntohll(ogs11->packet_count);
7313 gs->byte_count = ntohll(ogs11->byte_count);
7315 gs->n_buckets = (length - base_len) / sizeof *obc;
7316 obc = ofpbuf_try_pull(msg, gs->n_buckets * sizeof *obc);
7318 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
7319 ofpraw_get_name(raw), ofpbuf_size(msg));
7320 return OFPERR_OFPBRC_BAD_LEN;
7323 gs->bucket_stats = xmalloc(gs->n_buckets * sizeof *gs->bucket_stats);
7324 for (i = 0; i < gs->n_buckets; i++) {
7325 gs->bucket_stats[i].packet_count = ntohll(obc[i].packet_count);
7326 gs->bucket_stats[i].byte_count = ntohll(obc[i].byte_count);
7333 ofputil_put_ofp11_bucket(const struct ofputil_bucket *bucket,
7334 struct ofpbuf *openflow, enum ofp_version ofp_version)
7336 struct ofp11_bucket *ob;
7339 start = ofpbuf_size(openflow);
7340 ofpbuf_put_zeros(openflow, sizeof *ob);
7341 ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
7342 openflow, ofp_version);
7343 ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
7344 ob->len = htons(ofpbuf_size(openflow) - start);
7345 ob->weight = htons(bucket->weight);
7346 ob->watch_port = ofputil_port_to_ofp11(bucket->watch_port);
7347 ob->watch_group = htonl(bucket->watch_group);
7351 ofputil_put_ofp15_group_bucket_prop_weight(ovs_be16 weight,
7352 struct ofpbuf *openflow)
7355 struct ofp15_group_bucket_prop_weight *prop;
7357 start_ofs = start_property(openflow, OFPGBPT15_WEIGHT);
7358 ofpbuf_put_zeros(openflow, sizeof *prop - sizeof(struct ofp_prop_header));
7359 prop = ofpbuf_at_assert(openflow, start_ofs, sizeof *prop);
7360 prop->weight = weight;
7361 end_property(openflow, start_ofs);
7365 ofputil_put_ofp15_group_bucket_prop_watch(ovs_be32 watch, uint16_t type,
7366 struct ofpbuf *openflow)
7369 struct ofp15_group_bucket_prop_watch *prop;
7371 start_ofs = start_property(openflow, type);
7372 ofpbuf_put_zeros(openflow, sizeof *prop - sizeof(struct ofp_prop_header));
7373 prop = ofpbuf_at_assert(openflow, start_ofs, sizeof *prop);
7374 prop->watch = watch;
7375 end_property(openflow, start_ofs);
7379 ofputil_put_ofp15_bucket(const struct ofputil_bucket *bucket,
7380 uint32_t bucket_id, enum ofp11_group_type group_type,
7381 struct ofpbuf *openflow, enum ofp_version ofp_version)
7383 struct ofp15_bucket *ob;
7384 size_t start, actions_start, actions_len;
7386 start = ofpbuf_size(openflow);
7387 ofpbuf_put_zeros(openflow, sizeof *ob);
7389 actions_start = ofpbuf_size(openflow);
7390 ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
7391 openflow, ofp_version);
7392 actions_len = ofpbuf_size(openflow) - actions_start;
7394 if (group_type == OFPGT11_SELECT) {
7395 ofputil_put_ofp15_group_bucket_prop_weight(htons(bucket->weight),
7398 if (bucket->watch_port != OFPP_ANY) {
7399 ovs_be32 port = ofputil_port_to_ofp11(bucket->watch_port);
7400 ofputil_put_ofp15_group_bucket_prop_watch(port,
7401 OFPGBPT15_WATCH_PORT,
7404 if (bucket->watch_group != OFPG_ANY) {
7405 ovs_be32 group = htonl(bucket->watch_group);
7406 ofputil_put_ofp15_group_bucket_prop_watch(group,
7407 OFPGBPT15_WATCH_GROUP,
7411 ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
7412 ob->len = htons(ofpbuf_size(openflow) - start);
7413 ob->action_array_len = htons(actions_len);
7414 ob->bucket_id = htonl(bucket_id);
7418 ofputil_append_ofp11_group_desc_reply(const struct ofputil_group_desc *gds,
7419 const struct ovs_list *buckets,
7420 struct ovs_list *replies,
7421 enum ofp_version version)
7423 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
7424 struct ofp11_group_desc_stats *ogds;
7425 struct ofputil_bucket *bucket;
7428 start_ogds = ofpbuf_size(reply);
7429 ofpbuf_put_zeros(reply, sizeof *ogds);
7430 LIST_FOR_EACH (bucket, list_node, buckets) {
7431 ofputil_put_ofp11_bucket(bucket, reply, version);
7433 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
7434 ogds->length = htons(ofpbuf_size(reply) - start_ogds);
7435 ogds->type = gds->type;
7436 ogds->group_id = htonl(gds->group_id);
7438 ofpmp_postappend(replies, start_ogds);
7442 ofputil_append_ofp15_group_desc_reply(const struct ofputil_group_desc *gds,
7443 const struct ovs_list *buckets,
7444 struct ovs_list *replies,
7445 enum ofp_version version)
7447 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
7448 struct ofp15_group_desc_stats *ogds;
7449 struct ofputil_bucket *bucket;
7450 size_t start_ogds, start_buckets;
7452 start_ogds = ofpbuf_size(reply);
7453 ofpbuf_put_zeros(reply, sizeof *ogds);
7454 start_buckets = ofpbuf_size(reply);
7455 LIST_FOR_EACH (bucket, list_node, buckets) {
7456 ofputil_put_ofp15_bucket(bucket, bucket->bucket_id,
7457 gds->type, reply, version);
7459 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
7460 ogds->length = htons(ofpbuf_size(reply) - start_ogds);
7461 ogds->type = gds->type;
7462 ogds->group_id = htonl(gds->group_id);
7463 ogds->bucket_list_len = htons(ofpbuf_size(reply) - start_buckets);
7465 ofpmp_postappend(replies, start_ogds);
7468 /* Appends a group stats reply that contains the data in 'gds' to those already
7469 * present in the list of ofpbufs in 'replies'. 'replies' should have been
7470 * initialized with ofpmp_init(). */
7472 ofputil_append_group_desc_reply(const struct ofputil_group_desc *gds,
7473 const struct ovs_list *buckets,
7474 struct ovs_list *replies)
7476 enum ofp_version version = ofpmp_version(replies);
7484 ofputil_append_ofp11_group_desc_reply(gds, buckets, replies, version);
7488 ofputil_append_ofp15_group_desc_reply(gds, buckets, replies, version);
7498 ofputil_pull_ofp11_buckets(struct ofpbuf *msg, size_t buckets_length,
7499 enum ofp_version version, struct ovs_list *buckets)
7501 struct ofp11_bucket *ob;
7502 uint32_t bucket_id = 0;
7505 while (buckets_length > 0) {
7506 struct ofputil_bucket *bucket;
7507 struct ofpbuf ofpacts;
7511 ob = (buckets_length >= sizeof *ob
7512 ? ofpbuf_try_pull(msg, sizeof *ob)
7515 VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE" leftover bytes",
7517 return OFPERR_OFPGMFC_BAD_BUCKET;
7520 ob_len = ntohs(ob->len);
7521 if (ob_len < sizeof *ob) {
7522 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
7523 "%"PRIuSIZE" is not valid", ob_len);
7524 return OFPERR_OFPGMFC_BAD_BUCKET;
7525 } else if (ob_len > buckets_length) {
7526 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
7527 "%"PRIuSIZE" exceeds remaining buckets data size %"PRIuSIZE,
7528 ob_len, buckets_length);
7529 return OFPERR_OFPGMFC_BAD_BUCKET;
7531 buckets_length -= ob_len;
7533 ofpbuf_init(&ofpacts, 0);
7534 error = ofpacts_pull_openflow_actions(msg, ob_len - sizeof *ob,
7537 ofpbuf_uninit(&ofpacts);
7538 ofputil_bucket_list_destroy(buckets);
7542 bucket = xzalloc(sizeof *bucket);
7543 bucket->weight = ntohs(ob->weight);
7544 error = ofputil_port_from_ofp11(ob->watch_port, &bucket->watch_port);
7546 ofpbuf_uninit(&ofpacts);
7547 ofputil_bucket_list_destroy(buckets);
7548 return OFPERR_OFPGMFC_BAD_WATCH;
7550 bucket->watch_group = ntohl(ob->watch_group);
7551 bucket->bucket_id = bucket_id++;
7553 bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
7554 bucket->ofpacts_len = ofpbuf_size(&ofpacts);
7555 list_push_back(buckets, &bucket->list_node);
7562 parse_ofp15_group_bucket_prop_weight(const struct ofpbuf *payload,
7565 struct ofp15_group_bucket_prop_weight *prop = ofpbuf_data(payload);
7567 if (ofpbuf_size(payload) != sizeof *prop) {
7568 log_property(false, "OpenFlow bucket weight property length "
7569 "%u is not valid", ofpbuf_size(payload));
7570 return OFPERR_OFPBPC_BAD_LEN;
7573 *weight = prop->weight;
7579 parse_ofp15_group_bucket_prop_watch(const struct ofpbuf *payload,
7582 struct ofp15_group_bucket_prop_watch *prop = ofpbuf_data(payload);
7584 if (ofpbuf_size(payload) != sizeof *prop) {
7585 log_property(false, "OpenFlow bucket watch port or group "
7586 "property length %u is not valid", ofpbuf_size(payload));
7587 return OFPERR_OFPBPC_BAD_LEN;
7590 *watch = prop->watch;
7596 ofputil_pull_ofp15_buckets(struct ofpbuf *msg, size_t buckets_length,
7597 enum ofp_version version, struct ovs_list *buckets)
7599 struct ofp15_bucket *ob;
7602 while (buckets_length > 0) {
7603 struct ofputil_bucket *bucket = NULL;
7604 struct ofpbuf ofpacts;
7605 enum ofperr err = OFPERR_OFPGMFC_BAD_BUCKET;
7606 struct ofpbuf properties;
7607 size_t ob_len, actions_len, properties_len;
7608 ovs_be32 watch_port = ofputil_port_to_ofp11(OFPP_ANY);
7609 ovs_be32 watch_group = htonl(OFPG_ANY);
7610 ovs_be16 weight = htons(1);
7612 ofpbuf_init(&ofpacts, 0);
7614 ob = ofpbuf_try_pull(msg, sizeof *ob);
7616 VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE
7617 " leftover bytes", buckets_length);
7621 ob_len = ntohs(ob->len);
7622 actions_len = ntohs(ob->action_array_len);
7624 if (ob_len < sizeof *ob) {
7625 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
7626 "%"PRIuSIZE" is not valid", ob_len);
7628 } else if (ob_len > buckets_length) {
7629 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
7630 "%"PRIuSIZE" exceeds remaining buckets data size %"
7631 PRIuSIZE, ob_len, buckets_length);
7633 } else if (actions_len > ob_len - sizeof *ob) {
7634 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket actions "
7635 "length %"PRIuSIZE" exceeds remaining bucket "
7636 "data size %"PRIuSIZE, actions_len,
7637 ob_len - sizeof *ob);
7640 buckets_length -= ob_len;
7642 err = ofpacts_pull_openflow_actions(msg, actions_len, version,
7648 properties_len = ob_len - sizeof *ob - actions_len;
7649 ofpbuf_use_const(&properties, ofpbuf_pull(msg, properties_len),
7652 while (ofpbuf_size(&properties) > 0) {
7653 struct ofpbuf payload;
7656 err = ofputil_pull_property(&properties, &payload, &type);
7662 case OFPGBPT15_WEIGHT:
7663 err = parse_ofp15_group_bucket_prop_weight(&payload, &weight);
7666 case OFPGBPT15_WATCH_PORT:
7667 err = parse_ofp15_group_bucket_prop_watch(&payload,
7671 case OFPGBPT15_WATCH_GROUP:
7672 err = parse_ofp15_group_bucket_prop_watch(&payload,
7677 log_property(false, "unknown group bucket property %"PRIu16,
7679 err = OFPERR_OFPBPC_BAD_TYPE;
7688 bucket = xzalloc(sizeof *bucket);
7690 bucket->weight = ntohs(weight);
7691 err = ofputil_port_from_ofp11(watch_port, &bucket->watch_port);
7693 err = OFPERR_OFPGMFC_BAD_WATCH;
7696 bucket->watch_group = ntohl(watch_group);
7697 bucket->bucket_id = ntohl(ob->bucket_id);
7698 if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
7699 VLOG_WARN_RL(&bad_ofmsg_rl, "bucket id (%u) is out of range",
7701 err = OFPERR_OFPGMFC_BAD_BUCKET;
7705 bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
7706 bucket->ofpacts_len = ofpbuf_size(&ofpacts);
7707 list_push_back(buckets, &bucket->list_node);
7713 ofpbuf_uninit(&ofpacts);
7714 ofputil_bucket_list_destroy(buckets);
7718 if (ofputil_bucket_check_duplicate_id(buckets)) {
7719 VLOG_WARN_RL(&bad_ofmsg_rl, "Duplicate bucket id");
7720 ofputil_bucket_list_destroy(buckets);
7721 return OFPERR_OFPGMFC_BAD_BUCKET;
7728 ofputil_decode_ofp11_group_desc_reply(struct ofputil_group_desc *gd,
7730 enum ofp_version version)
7732 struct ofp11_group_desc_stats *ogds;
7736 ofpraw_pull_assert(msg);
7739 if (!ofpbuf_size(msg)) {
7743 ogds = ofpbuf_try_pull(msg, sizeof *ogds);
7745 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
7746 "leftover bytes at end", ofpbuf_size(msg));
7747 return OFPERR_OFPBRC_BAD_LEN;
7749 gd->type = ogds->type;
7750 gd->group_id = ntohl(ogds->group_id);
7752 length = ntohs(ogds->length);
7753 if (length < sizeof *ogds || length - sizeof *ogds > ofpbuf_size(msg)) {
7754 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
7755 "length %"PRIuSIZE, length);
7756 return OFPERR_OFPBRC_BAD_LEN;
7759 return ofputil_pull_ofp11_buckets(msg, length - sizeof *ogds, version,
7764 ofputil_decode_ofp15_group_desc_reply(struct ofputil_group_desc *gd,
7766 enum ofp_version version)
7768 struct ofp15_group_desc_stats *ogds;
7769 uint16_t length, bucket_list_len;
7772 ofpraw_pull_assert(msg);
7775 if (!ofpbuf_size(msg)) {
7779 ogds = ofpbuf_try_pull(msg, sizeof *ogds);
7781 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
7782 "leftover bytes at end", ofpbuf_size(msg));
7783 return OFPERR_OFPBRC_BAD_LEN;
7785 gd->type = ogds->type;
7786 gd->group_id = ntohl(ogds->group_id);
7788 length = ntohs(ogds->length);
7789 if (length < sizeof *ogds || length - sizeof *ogds > ofpbuf_size(msg)) {
7790 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
7791 "length %u", length);
7792 return OFPERR_OFPBRC_BAD_LEN;
7795 bucket_list_len = ntohs(ogds->bucket_list_len);
7796 if (length < bucket_list_len + sizeof *ogds) {
7797 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
7798 "bucket list length %u", bucket_list_len);
7799 return OFPERR_OFPBRC_BAD_LEN;
7802 return ofputil_pull_ofp15_buckets(msg, bucket_list_len, version,
7806 /* Converts a group description reply in 'msg' into an abstract
7807 * ofputil_group_desc in 'gd'.
7809 * Multiple group description replies can be packed into a single OpenFlow
7810 * message. Calling this function multiple times for a single 'msg' iterates
7811 * through the replies. The caller must initially leave 'msg''s layer pointers
7812 * null and not modify them between calls.
7814 * Returns 0 if successful, EOF if no replies were left in this 'msg',
7815 * otherwise a positive errno value. */
7817 ofputil_decode_group_desc_reply(struct ofputil_group_desc *gd,
7818 struct ofpbuf *msg, enum ofp_version version)
7826 return ofputil_decode_ofp11_group_desc_reply(gd, msg, version);
7829 return ofputil_decode_ofp15_group_desc_reply(gd, msg, version);
7837 static struct ofpbuf *
7838 ofputil_encode_ofp11_group_mod(enum ofp_version ofp_version,
7839 const struct ofputil_group_mod *gm)
7842 struct ofp11_group_mod *ogm;
7844 struct ofputil_bucket *bucket;
7846 b = ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD, ofp_version, 0);
7847 start_ogm = ofpbuf_size(b);
7848 ofpbuf_put_zeros(b, sizeof *ogm);
7850 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
7851 ofputil_put_ofp11_bucket(bucket, b, ofp_version);
7853 ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
7854 ogm->command = htons(gm->command);
7855 ogm->type = gm->type;
7856 ogm->group_id = htonl(gm->group_id);
7861 static struct ofpbuf *
7862 ofputil_encode_ofp15_group_mod(enum ofp_version ofp_version,
7863 const struct ofputil_group_mod *gm)
7866 struct ofp15_group_mod *ogm;
7868 struct ofputil_bucket *bucket;
7869 struct id_pool *bucket_ids = NULL;
7871 b = ofpraw_alloc(OFPRAW_OFPT15_GROUP_MOD, ofp_version, 0);
7872 start_ogm = ofpbuf_size(b);
7873 ofpbuf_put_zeros(b, sizeof *ogm);
7875 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
7878 /* Generate a bucket id if none was supplied */
7879 if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
7881 const struct ofputil_bucket *bkt;
7883 bucket_ids = id_pool_create(0, OFPG15_BUCKET_MAX + 1);
7885 /* Mark all bucket_ids that are present in gm
7886 * as used in the pool. */
7887 LIST_FOR_EACH_REVERSE (bkt, list_node, &gm->buckets) {
7888 if (bkt == bucket) {
7891 if (bkt->bucket_id <= OFPG15_BUCKET_MAX) {
7892 id_pool_add(bucket_ids, bkt->bucket_id);
7897 if (!id_pool_alloc_id(bucket_ids, &bucket_id)) {
7901 bucket_id = bucket->bucket_id;
7904 ofputil_put_ofp15_bucket(bucket, bucket_id, gm->type, b, ofp_version);
7906 ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
7907 ogm->command = htons(gm->command);
7908 ogm->type = gm->type;
7909 ogm->group_id = htonl(gm->group_id);
7910 ogm->command_bucket_id = htonl(gm->command_bucket_id);
7911 ogm->bucket_array_len = htons(ofpbuf_size(b) - start_ogm - sizeof *ogm);
7913 id_pool_destroy(bucket_ids);
7918 bad_group_cmd(enum ofp15_group_mod_command cmd) {
7919 const char *opt_version;
7920 const char *version;
7921 const char *cmd_str;
7925 case OFPGC15_MODIFY:
7926 case OFPGC15_DELETE:
7931 case OFPGC15_INSERT_BUCKET:
7932 case OFPGC15_REMOVE_BUCKET:
7942 cmd_str = "add-group";
7945 case OFPGC15_MODIFY:
7946 cmd_str = "mod-group";
7949 case OFPGC15_DELETE:
7950 cmd_str = "del-group";
7953 case OFPGC15_INSERT_BUCKET:
7954 cmd_str = "insert-bucket";
7957 case OFPGC15_REMOVE_BUCKET:
7958 cmd_str = "insert-bucket";
7965 ovs_fatal(0, "%s needs OpenFlow %s or later (\'-O OpenFlow%s\')",
7966 cmd_str, version, opt_version);
7970 /* Converts abstract group mod 'gm' into a message for OpenFlow version
7971 * 'ofp_version' and returns the message. */
7973 ofputil_encode_group_mod(enum ofp_version ofp_version,
7974 const struct ofputil_group_mod *gm)
7977 switch (ofp_version) {
7979 bad_group_cmd(gm->command);
7985 if (gm->command > OFPGC11_DELETE) {
7986 bad_group_cmd(gm->command);
7988 return ofputil_encode_ofp11_group_mod(ofp_version, gm);
7991 return ofputil_encode_ofp15_group_mod(ofp_version, gm);
7999 ofputil_pull_ofp11_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
8000 struct ofputil_group_mod *gm)
8002 const struct ofp11_group_mod *ogm;
8005 ogm = ofpbuf_pull(msg, sizeof *ogm);
8006 gm->command = ntohs(ogm->command);
8007 gm->type = ogm->type;
8008 gm->group_id = ntohl(ogm->group_id);
8009 gm->command_bucket_id = OFPG15_BUCKET_ALL;
8011 error = ofputil_pull_ofp11_buckets(msg, ofpbuf_size(msg), ofp_version,
8014 /* OF1.3.5+ prescribes an error when an OFPGC_DELETE includes buckets. */
8016 && ofp_version >= OFP13_VERSION
8017 && gm->command == OFPGC11_DELETE
8018 && !list_is_empty(&gm->buckets)) {
8019 error = OFPERR_OFPGMFC_INVALID_GROUP;
8026 ofputil_pull_ofp15_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
8027 struct ofputil_group_mod *gm)
8029 const struct ofp15_group_mod *ogm;
8030 uint16_t bucket_list_len;
8031 enum ofperr error = OFPERR_OFPGMFC_BAD_BUCKET;
8033 ogm = ofpbuf_pull(msg, sizeof *ogm);
8034 gm->command = ntohs(ogm->command);
8035 gm->type = ogm->type;
8036 gm->group_id = ntohl(ogm->group_id);
8038 gm->command_bucket_id = ntohl(ogm->command_bucket_id);
8039 switch (gm->command) {
8040 case OFPGC15_REMOVE_BUCKET:
8041 if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
8045 case OFPGC15_INSERT_BUCKET:
8046 if (gm->command_bucket_id <= OFPG15_BUCKET_MAX ||
8047 gm->command_bucket_id == OFPG15_BUCKET_FIRST
8048 || gm->command_bucket_id == OFPG15_BUCKET_LAST) {
8054 case OFPGC11_MODIFY:
8055 case OFPGC11_DELETE:
8057 if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
8063 VLOG_WARN_RL(&bad_ofmsg_rl,
8064 "group command bucket id (%u) is out of range",
8065 gm->command_bucket_id);
8066 return OFPERR_OFPGMFC_BAD_BUCKET;
8069 bucket_list_len = ntohs(ogm->bucket_array_len);
8070 if (bucket_list_len < ofpbuf_size(msg)) {
8071 VLOG_WARN_RL(&bad_ofmsg_rl, "group has %u trailing bytes",
8072 ofpbuf_size(msg) - bucket_list_len);
8073 return OFPERR_OFPGMFC_BAD_BUCKET;
8076 return ofputil_pull_ofp15_buckets(msg, bucket_list_len, ofp_version,
8080 /* Converts OpenFlow group mod message 'oh' into an abstract group mod in
8081 * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */
8083 ofputil_decode_group_mod(const struct ofp_header *oh,
8084 struct ofputil_group_mod *gm)
8086 enum ofp_version ofp_version = oh->version;
8088 struct ofputil_bucket *bucket;
8091 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
8092 ofpraw_pull_assert(&msg);
8094 switch (ofp_version)
8100 err = ofputil_pull_ofp11_group_mod(&msg, ofp_version, gm);
8104 err = ofputil_pull_ofp15_group_mod(&msg, ofp_version, gm);
8117 case OFPGT11_INDIRECT:
8118 if (!list_is_singleton(&gm->buckets)) {
8119 return OFPERR_OFPGMFC_INVALID_GROUP;
8123 case OFPGT11_SELECT:
8130 switch (gm->command) {
8132 case OFPGC11_MODIFY:
8133 case OFPGC11_DELETE:
8134 case OFPGC15_INSERT_BUCKET:
8136 case OFPGC15_REMOVE_BUCKET:
8137 if (!list_is_empty(&gm->buckets)) {
8138 return OFPERR_OFPGMFC_BAD_BUCKET;
8145 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
8148 case OFPGT11_INDIRECT:
8149 if (ofputil_bucket_has_liveness(bucket)) {
8150 return OFPERR_OFPGMFC_WATCH_UNSUPPORTED;
8153 case OFPGT11_SELECT:
8156 if (!ofputil_bucket_has_liveness(bucket)) {
8157 return OFPERR_OFPGMFC_INVALID_GROUP;
8168 /* Parse a queue status request message into 'oqsr'.
8169 * Returns 0 if successful, otherwise an OFPERR_* number. */
8171 ofputil_decode_queue_stats_request(const struct ofp_header *request,
8172 struct ofputil_queue_stats_request *oqsr)
8174 switch ((enum ofp_version)request->version) {
8179 case OFP11_VERSION: {
8180 const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request);
8181 oqsr->queue_id = ntohl(qsr11->queue_id);
8182 return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no);
8185 case OFP10_VERSION: {
8186 const struct ofp10_queue_stats_request *qsr10 = ofpmsg_body(request);
8187 oqsr->queue_id = ntohl(qsr10->queue_id);
8188 oqsr->port_no = u16_to_ofp(ntohs(qsr10->port_no));
8189 /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
8190 if (oqsr->port_no == OFPP_ALL) {
8191 oqsr->port_no = OFPP_ANY;
8201 /* Encode a queue stats request for 'oqsr', the encoded message
8202 * will be for OpenFlow version 'ofp_version'. Returns message
8203 * as a struct ofpbuf. Returns encoded message on success, NULL on error. */
8205 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
8206 const struct ofputil_queue_stats_request *oqsr)
8208 struct ofpbuf *request;
8210 switch (ofp_version) {
8215 case OFP15_VERSION: {
8216 struct ofp11_queue_stats_request *req;
8217 request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0);
8218 req = ofpbuf_put_zeros(request, sizeof *req);
8219 req->port_no = ofputil_port_to_ofp11(oqsr->port_no);
8220 req->queue_id = htonl(oqsr->queue_id);
8223 case OFP10_VERSION: {
8224 struct ofp10_queue_stats_request *req;
8225 request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0);
8226 req = ofpbuf_put_zeros(request, sizeof *req);
8227 /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
8228 req->port_no = htons(ofp_to_u16(oqsr->port_no == OFPP_ANY
8229 ? OFPP_ALL : oqsr->port_no));
8230 req->queue_id = htonl(oqsr->queue_id);
8240 /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
8243 ofputil_count_queue_stats(const struct ofp_header *oh)
8245 struct ofputil_queue_stats qs;
8249 ofpbuf_use_const(&b, oh, ntohs(oh->length));
8250 ofpraw_pull_assert(&b);
8251 while (!ofputil_decode_queue_stats(&qs, &b)) {
8258 ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs,
8259 const struct ofp10_queue_stats *qs10)
8261 oqs->port_no = u16_to_ofp(ntohs(qs10->port_no));
8262 oqs->queue_id = ntohl(qs10->queue_id);
8263 oqs->tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes));
8264 oqs->tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets));
8265 oqs->tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors));
8266 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
8272 ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs,
8273 const struct ofp11_queue_stats *qs11)
8277 error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no);
8282 oqs->queue_id = ntohl(qs11->queue_id);
8283 oqs->tx_bytes = ntohll(qs11->tx_bytes);
8284 oqs->tx_packets = ntohll(qs11->tx_packets);
8285 oqs->tx_errors = ntohll(qs11->tx_errors);
8286 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
8292 ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats *oqs,
8293 const struct ofp13_queue_stats *qs13)
8295 enum ofperr error = ofputil_queue_stats_from_ofp11(oqs, &qs13->qs);
8297 oqs->duration_sec = ntohl(qs13->duration_sec);
8298 oqs->duration_nsec = ntohl(qs13->duration_nsec);
8305 ofputil_pull_ofp14_queue_stats(struct ofputil_queue_stats *oqs,
8308 const struct ofp14_queue_stats *qs14;
8311 qs14 = ofpbuf_try_pull(msg, sizeof *qs14);
8313 return OFPERR_OFPBRC_BAD_LEN;
8316 len = ntohs(qs14->length);
8317 if (len < sizeof *qs14 || len - sizeof *qs14 > ofpbuf_size(msg)) {
8318 return OFPERR_OFPBRC_BAD_LEN;
8320 ofpbuf_pull(msg, len - sizeof *qs14);
8322 /* No properties yet defined, so ignore them for now. */
8324 return ofputil_queue_stats_from_ofp13(oqs, &qs14->qs);
8327 /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
8328 * ofputil_queue_stats in 'qs'.
8330 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
8331 * message. Calling this function multiple times for a single 'msg' iterates
8332 * through the replies. The caller must initially leave 'msg''s layer pointers
8333 * null and not modify them between calls.
8335 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8336 * otherwise a positive errno value. */
8338 ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg)
8344 ? ofpraw_decode(&raw, msg->frame)
8345 : ofpraw_pull(&raw, msg));
8350 if (!ofpbuf_size(msg)) {
8352 } else if (raw == OFPRAW_OFPST14_QUEUE_REPLY) {
8353 return ofputil_pull_ofp14_queue_stats(qs, msg);
8354 } else if (raw == OFPRAW_OFPST13_QUEUE_REPLY) {
8355 const struct ofp13_queue_stats *qs13;
8357 qs13 = ofpbuf_try_pull(msg, sizeof *qs13);
8361 return ofputil_queue_stats_from_ofp13(qs, qs13);
8362 } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) {
8363 const struct ofp11_queue_stats *qs11;
8365 qs11 = ofpbuf_try_pull(msg, sizeof *qs11);
8369 return ofputil_queue_stats_from_ofp11(qs, qs11);
8370 } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) {
8371 const struct ofp10_queue_stats *qs10;
8373 qs10 = ofpbuf_try_pull(msg, sizeof *qs10);
8377 return ofputil_queue_stats_from_ofp10(qs, qs10);
8383 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %"PRIu32" leftover "
8384 "bytes at end", ofpbuf_size(msg));
8385 return OFPERR_OFPBRC_BAD_LEN;
8389 ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs,
8390 struct ofp10_queue_stats *qs10)
8392 qs10->port_no = htons(ofp_to_u16(oqs->port_no));
8393 memset(qs10->pad, 0, sizeof qs10->pad);
8394 qs10->queue_id = htonl(oqs->queue_id);
8395 put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->tx_bytes));
8396 put_32aligned_be64(&qs10->tx_packets, htonll(oqs->tx_packets));
8397 put_32aligned_be64(&qs10->tx_errors, htonll(oqs->tx_errors));
8401 ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs,
8402 struct ofp11_queue_stats *qs11)
8404 qs11->port_no = ofputil_port_to_ofp11(oqs->port_no);
8405 qs11->queue_id = htonl(oqs->queue_id);
8406 qs11->tx_bytes = htonll(oqs->tx_bytes);
8407 qs11->tx_packets = htonll(oqs->tx_packets);
8408 qs11->tx_errors = htonll(oqs->tx_errors);
8412 ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats *oqs,
8413 struct ofp13_queue_stats *qs13)
8415 ofputil_queue_stats_to_ofp11(oqs, &qs13->qs);
8416 if (oqs->duration_sec != UINT32_MAX) {
8417 qs13->duration_sec = htonl(oqs->duration_sec);
8418 qs13->duration_nsec = htonl(oqs->duration_nsec);
8420 qs13->duration_sec = OVS_BE32_MAX;
8421 qs13->duration_nsec = OVS_BE32_MAX;
8426 ofputil_queue_stats_to_ofp14(const struct ofputil_queue_stats *oqs,
8427 struct ofp14_queue_stats *qs14)
8429 qs14->length = htons(sizeof *qs14);
8430 memset(qs14->pad, 0, sizeof qs14->pad);
8431 ofputil_queue_stats_to_ofp13(oqs, &qs14->qs);
8435 /* Encode a queue stat for 'oqs' and append it to 'replies'. */
8437 ofputil_append_queue_stat(struct ovs_list *replies,
8438 const struct ofputil_queue_stats *oqs)
8440 switch (ofpmp_version(replies)) {
8441 case OFP13_VERSION: {
8442 struct ofp13_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
8443 ofputil_queue_stats_to_ofp13(oqs, reply);
8448 case OFP11_VERSION: {
8449 struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
8450 ofputil_queue_stats_to_ofp11(oqs, reply);
8454 case OFP10_VERSION: {
8455 struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
8456 ofputil_queue_stats_to_ofp10(oqs, reply);
8461 case OFP15_VERSION: {
8462 struct ofp14_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
8463 ofputil_queue_stats_to_ofp14(oqs, reply);
8473 ofputil_decode_bundle_ctrl(const struct ofp_header *oh,
8474 struct ofputil_bundle_ctrl_msg *msg)
8478 const struct ofp14_bundle_ctrl_msg *m;
8480 ofpbuf_use_const(&b, oh, ntohs(oh->length));
8481 raw = ofpraw_pull_assert(&b);
8482 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_CONTROL);
8485 msg->bundle_id = ntohl(m->bundle_id);
8486 msg->type = ntohs(m->type);
8487 msg->flags = ntohs(m->flags);
8493 ofputil_encode_bundle_ctrl_reply(const struct ofp_header *oh,
8494 struct ofputil_bundle_ctrl_msg *msg)
8497 struct ofp14_bundle_ctrl_msg *m;
8499 buf = ofpraw_alloc_reply(OFPRAW_OFPT14_BUNDLE_CONTROL, oh, 0);
8500 m = ofpbuf_put_zeros(buf, sizeof *m);
8502 m->bundle_id = htonl(msg->bundle_id);
8503 m->type = htons(msg->type);
8504 m->flags = htons(msg->flags);
8509 /* Return true for bundlable state change requests, false for other messages.
8512 ofputil_is_bundlable(enum ofptype type)
8515 /* Minimum required by OpenFlow 1.4. */
8516 case OFPTYPE_PORT_MOD:
8517 case OFPTYPE_FLOW_MOD:
8520 /* Nice to have later. */
8521 case OFPTYPE_FLOW_MOD_TABLE_ID:
8522 case OFPTYPE_GROUP_MOD:
8523 case OFPTYPE_TABLE_MOD:
8524 case OFPTYPE_METER_MOD:
8525 case OFPTYPE_PACKET_OUT:
8527 /* Not to be bundlable. */
8528 case OFPTYPE_ECHO_REQUEST:
8529 case OFPTYPE_FEATURES_REQUEST:
8530 case OFPTYPE_GET_CONFIG_REQUEST:
8531 case OFPTYPE_SET_CONFIG:
8532 case OFPTYPE_BARRIER_REQUEST:
8533 case OFPTYPE_ROLE_REQUEST:
8534 case OFPTYPE_ECHO_REPLY:
8535 case OFPTYPE_SET_FLOW_FORMAT:
8536 case OFPTYPE_SET_PACKET_IN_FORMAT:
8537 case OFPTYPE_SET_CONTROLLER_ID:
8538 case OFPTYPE_FLOW_AGE:
8539 case OFPTYPE_FLOW_MONITOR_CANCEL:
8540 case OFPTYPE_SET_ASYNC_CONFIG:
8541 case OFPTYPE_GET_ASYNC_REQUEST:
8542 case OFPTYPE_DESC_STATS_REQUEST:
8543 case OFPTYPE_FLOW_STATS_REQUEST:
8544 case OFPTYPE_AGGREGATE_STATS_REQUEST:
8545 case OFPTYPE_TABLE_STATS_REQUEST:
8546 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
8547 case OFPTYPE_PORT_STATS_REQUEST:
8548 case OFPTYPE_QUEUE_STATS_REQUEST:
8549 case OFPTYPE_PORT_DESC_STATS_REQUEST:
8550 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
8551 case OFPTYPE_METER_STATS_REQUEST:
8552 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
8553 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
8554 case OFPTYPE_GROUP_STATS_REQUEST:
8555 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
8556 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
8557 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
8558 case OFPTYPE_BUNDLE_CONTROL:
8559 case OFPTYPE_BUNDLE_ADD_MESSAGE:
8562 case OFPTYPE_FEATURES_REPLY:
8563 case OFPTYPE_GET_CONFIG_REPLY:
8564 case OFPTYPE_PACKET_IN:
8565 case OFPTYPE_FLOW_REMOVED:
8566 case OFPTYPE_PORT_STATUS:
8567 case OFPTYPE_BARRIER_REPLY:
8568 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
8569 case OFPTYPE_DESC_STATS_REPLY:
8570 case OFPTYPE_FLOW_STATS_REPLY:
8571 case OFPTYPE_QUEUE_STATS_REPLY:
8572 case OFPTYPE_PORT_STATS_REPLY:
8573 case OFPTYPE_TABLE_STATS_REPLY:
8574 case OFPTYPE_AGGREGATE_STATS_REPLY:
8575 case OFPTYPE_PORT_DESC_STATS_REPLY:
8576 case OFPTYPE_ROLE_REPLY:
8577 case OFPTYPE_FLOW_MONITOR_PAUSED:
8578 case OFPTYPE_FLOW_MONITOR_RESUMED:
8579 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
8580 case OFPTYPE_GET_ASYNC_REPLY:
8581 case OFPTYPE_GROUP_STATS_REPLY:
8582 case OFPTYPE_GROUP_DESC_STATS_REPLY:
8583 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
8584 case OFPTYPE_METER_STATS_REPLY:
8585 case OFPTYPE_METER_CONFIG_STATS_REPLY:
8586 case OFPTYPE_METER_FEATURES_STATS_REPLY:
8587 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
8588 case OFPTYPE_ROLE_STATUS:
8596 ofputil_decode_bundle_add(const struct ofp_header *oh,
8597 struct ofputil_bundle_add_msg *msg)
8599 const struct ofp14_bundle_ctrl_msg *m;
8606 ofpbuf_use_const(&b, oh, ntohs(oh->length));
8607 raw = ofpraw_pull_assert(&b);
8608 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE);
8610 m = ofpbuf_pull(&b, sizeof *m);
8611 msg->bundle_id = ntohl(m->bundle_id);
8612 msg->flags = ntohs(m->flags);
8614 msg->msg = ofpbuf_data(&b);
8615 inner_len = ntohs(msg->msg->length);
8616 if (inner_len < sizeof(struct ofp_header) || inner_len > ofpbuf_size(&b)) {
8617 return OFPERR_OFPBFC_MSG_BAD_LEN;
8619 if (msg->msg->xid != oh->xid) {
8620 return OFPERR_OFPBFC_MSG_BAD_XID;
8623 /* Reject unbundlable messages. */
8624 error = ofptype_decode(&type, msg->msg);
8626 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPT14_BUNDLE_ADD_MESSAGE contained "
8627 "message is unparsable (%s)", ofperr_get_name(error));
8628 return OFPERR_OFPBFC_MSG_UNSUP; /* 'error' would be confusing. */
8631 if (!ofputil_is_bundlable(type)) {
8632 return OFPERR_OFPBFC_MSG_UNSUP;
8639 ofputil_encode_bundle_add(enum ofp_version ofp_version,
8640 struct ofputil_bundle_add_msg *msg)
8642 struct ofpbuf *request;
8643 struct ofp14_bundle_ctrl_msg *m;
8645 request = ofpraw_alloc(OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE, ofp_version, 0);
8646 m = ofpbuf_put_zeros(request, sizeof *m);
8648 m->bundle_id = htonl(msg->bundle_id);
8649 m->flags = htons(msg->flags);
8650 ofpbuf_put(request, msg->msg, ntohs(msg->msg->length));