2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofp-print.h"
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
27 #include "byte-order.h"
28 #include "classifier.h"
29 #include "dynamic-string.h"
31 #include "meta-flow.h"
32 #include "multipath.h"
36 #include "ofp-actions.h"
37 #include "ofp-errors.h"
41 #include "openflow/netronome-ext.h"
44 #include "tun-metadata.h"
45 #include "unaligned.h"
46 #include "type-props.h"
47 #include "openvswitch/vlog.h"
50 VLOG_DEFINE_THIS_MODULE(ofp_util);
52 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
53 * in the peer and so there's not much point in showing a lot of them. */
54 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
56 static enum ofputil_table_eviction ofputil_decode_table_eviction(
57 ovs_be32 config, enum ofp_version);
58 static ovs_be32 ofputil_encode_table_config(enum ofputil_table_miss,
59 enum ofputil_table_eviction,
62 struct ofp_prop_header {
67 struct ofp_prop_experimenter {
68 ovs_be16 type; /* OFP*_EXPERIMENTER. */
69 ovs_be16 length; /* Length in bytes of this property. */
70 ovs_be32 experimenter; /* Experimenter ID which takes the same form as
71 * in struct ofp_experimenter_header. */
72 ovs_be32 exp_type; /* Experimenter defined. */
75 /* Pulls a property, beginning with struct ofp_prop_header, from the beginning
76 * of 'msg'. Stores the type of the property in '*typep' and, if 'property' is
77 * nonnull, the entire property, including the header, in '*property'. Returns
78 * 0 if successful, otherwise an error code.
80 * This function pulls the property's stated size padded out to a multiple of
81 * 'alignment' bytes. The common case in OpenFlow is an 'alignment' of 8, so
82 * you can use ofputil_pull_property() for that case. */
84 ofputil_pull_property__(struct ofpbuf *msg, struct ofpbuf *property,
85 unsigned int alignment, uint16_t *typep)
87 struct ofp_prop_header *oph;
88 unsigned int padded_len;
91 if (msg->size < sizeof *oph) {
92 return OFPERR_OFPBPC_BAD_LEN;
96 len = ntohs(oph->len);
97 padded_len = ROUND_UP(len, alignment);
98 if (len < sizeof *oph || padded_len > msg->size) {
99 return OFPERR_OFPBPC_BAD_LEN;
102 *typep = ntohs(oph->type);
104 ofpbuf_use_const(property, msg->data, len);
106 ofpbuf_pull(msg, padded_len);
110 /* Pulls a property, beginning with struct ofp_prop_header, from the beginning
111 * of 'msg'. Stores the type of the property in '*typep' and, if 'property' is
112 * nonnull, the entire property, including the header, in '*property'. Returns
113 * 0 if successful, otherwise an error code.
115 * This function pulls the property's stated size padded out to a multiple of
116 * 8 bytes, which is the common case for OpenFlow properties. */
118 ofputil_pull_property(struct ofpbuf *msg, struct ofpbuf *property,
121 return ofputil_pull_property__(msg, property, 8, typep);
124 static void OVS_PRINTF_FORMAT(2, 3)
125 log_property(bool loose, const char *message, ...)
127 enum vlog_level level = loose ? VLL_DBG : VLL_WARN;
128 if (!vlog_should_drop(THIS_MODULE, level, &bad_ofmsg_rl)) {
131 va_start(args, message);
132 vlog_valist(THIS_MODULE, level, message, args);
138 start_property(struct ofpbuf *msg, uint16_t type)
140 size_t start_ofs = msg->size;
141 struct ofp_prop_header *oph;
143 oph = ofpbuf_put_uninit(msg, sizeof *oph);
144 oph->type = htons(type);
145 oph->len = htons(4); /* May be updated later by end_property(). */
150 end_property(struct ofpbuf *msg, size_t start_ofs)
152 struct ofp_prop_header *oph;
154 oph = ofpbuf_at_assert(msg, start_ofs, sizeof *oph);
155 oph->len = htons(msg->size - start_ofs);
156 ofpbuf_padto(msg, ROUND_UP(msg->size, 8));
160 put_bitmap_properties(struct ofpbuf *msg, uint64_t bitmap)
162 for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
163 start_property(msg, rightmost_1bit_idx(bitmap));
167 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
168 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
171 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
172 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
173 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
174 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
177 ofputil_wcbits_to_netmask(int wcbits)
180 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
183 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
184 * that it wildcards, that is, the number of 0-bits in 'netmask', a number
185 * between 0 and 32 inclusive.
187 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
188 * still be in the valid range but isn't otherwise meaningful. */
190 ofputil_netmask_to_wcbits(ovs_be32 netmask)
192 return 32 - ip_count_cidr_bits(netmask);
195 /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
196 * flow_wildcards in 'wc' for use in struct match. It is the caller's
197 * responsibility to handle the special case where the flow match's dl_vlan is
198 * set to OFP_VLAN_NONE. */
200 ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc)
202 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 32);
204 /* Initialize most of wc. */
205 flow_wildcards_init_catchall(wc);
207 if (!(ofpfw & OFPFW10_IN_PORT)) {
208 wc->masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
211 if (!(ofpfw & OFPFW10_NW_TOS)) {
212 wc->masks.nw_tos |= IP_DSCP_MASK;
215 if (!(ofpfw & OFPFW10_NW_PROTO)) {
216 wc->masks.nw_proto = UINT8_MAX;
218 wc->masks.nw_src = ofputil_wcbits_to_netmask(ofpfw
219 >> OFPFW10_NW_SRC_SHIFT);
220 wc->masks.nw_dst = ofputil_wcbits_to_netmask(ofpfw
221 >> OFPFW10_NW_DST_SHIFT);
223 if (!(ofpfw & OFPFW10_TP_SRC)) {
224 wc->masks.tp_src = OVS_BE16_MAX;
226 if (!(ofpfw & OFPFW10_TP_DST)) {
227 wc->masks.tp_dst = OVS_BE16_MAX;
230 if (!(ofpfw & OFPFW10_DL_SRC)) {
231 memset(wc->masks.dl_src, 0xff, ETH_ADDR_LEN);
233 if (!(ofpfw & OFPFW10_DL_DST)) {
234 memset(wc->masks.dl_dst, 0xff, ETH_ADDR_LEN);
236 if (!(ofpfw & OFPFW10_DL_TYPE)) {
237 wc->masks.dl_type = OVS_BE16_MAX;
241 if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) {
242 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
244 if (!(ofpfw & OFPFW10_DL_VLAN)) {
245 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
249 /* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */
251 ofputil_match_from_ofp10_match(const struct ofp10_match *ofmatch,
254 uint32_t ofpfw = ntohl(ofmatch->wildcards) & OFPFW10_ALL;
256 /* Initialize match->wc. */
257 memset(&match->flow, 0, sizeof match->flow);
258 ofputil_wildcard_from_ofpfw10(ofpfw, &match->wc);
260 /* Initialize most of match->flow. */
261 match->flow.nw_src = ofmatch->nw_src;
262 match->flow.nw_dst = ofmatch->nw_dst;
263 match->flow.in_port.ofp_port = u16_to_ofp(ntohs(ofmatch->in_port));
264 match->flow.dl_type = ofputil_dl_type_from_openflow(ofmatch->dl_type);
265 match->flow.tp_src = ofmatch->tp_src;
266 match->flow.tp_dst = ofmatch->tp_dst;
267 memcpy(match->flow.dl_src, ofmatch->dl_src, ETH_ADDR_LEN);
268 memcpy(match->flow.dl_dst, ofmatch->dl_dst, ETH_ADDR_LEN);
269 match->flow.nw_tos = ofmatch->nw_tos & IP_DSCP_MASK;
270 match->flow.nw_proto = ofmatch->nw_proto;
272 /* Translate VLANs. */
273 if (!(ofpfw & OFPFW10_DL_VLAN) &&
274 ofmatch->dl_vlan == htons(OFP10_VLAN_NONE)) {
275 /* Match only packets without 802.1Q header.
277 * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
279 * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
280 * because we can't have a specific PCP without an 802.1Q header.
281 * However, older versions of OVS treated this as matching packets
282 * withut an 802.1Q header, so we do here too. */
283 match->flow.vlan_tci = htons(0);
284 match->wc.masks.vlan_tci = htons(0xffff);
286 ovs_be16 vid, pcp, tci;
289 vid = ofmatch->dl_vlan & htons(VLAN_VID_MASK);
290 hpcp = (ofmatch->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK;
292 tci = vid | pcp | htons(VLAN_CFI);
293 match->flow.vlan_tci = tci & match->wc.masks.vlan_tci;
297 match_zero_wildcarded_fields(match);
300 /* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */
302 ofputil_match_to_ofp10_match(const struct match *match,
303 struct ofp10_match *ofmatch)
305 const struct flow_wildcards *wc = &match->wc;
308 /* Figure out most OpenFlow wildcards. */
310 if (!wc->masks.in_port.ofp_port) {
311 ofpfw |= OFPFW10_IN_PORT;
313 if (!wc->masks.dl_type) {
314 ofpfw |= OFPFW10_DL_TYPE;
316 if (!wc->masks.nw_proto) {
317 ofpfw |= OFPFW10_NW_PROTO;
319 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_src)
320 << OFPFW10_NW_SRC_SHIFT);
321 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_dst)
322 << OFPFW10_NW_DST_SHIFT);
323 if (!(wc->masks.nw_tos & IP_DSCP_MASK)) {
324 ofpfw |= OFPFW10_NW_TOS;
326 if (!wc->masks.tp_src) {
327 ofpfw |= OFPFW10_TP_SRC;
329 if (!wc->masks.tp_dst) {
330 ofpfw |= OFPFW10_TP_DST;
332 if (eth_addr_is_zero(wc->masks.dl_src)) {
333 ofpfw |= OFPFW10_DL_SRC;
335 if (eth_addr_is_zero(wc->masks.dl_dst)) {
336 ofpfw |= OFPFW10_DL_DST;
339 /* Translate VLANs. */
340 ofmatch->dl_vlan = htons(0);
341 ofmatch->dl_vlan_pcp = 0;
342 if (match->wc.masks.vlan_tci == htons(0)) {
343 ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP;
344 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
345 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
346 ofmatch->dl_vlan = htons(OFP10_VLAN_NONE);
347 ofpfw |= OFPFW10_DL_VLAN_PCP;
349 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
350 ofpfw |= OFPFW10_DL_VLAN;
352 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
355 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
356 ofpfw |= OFPFW10_DL_VLAN_PCP;
358 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
362 /* Compose most of the match structure. */
363 ofmatch->wildcards = htonl(ofpfw);
364 ofmatch->in_port = htons(ofp_to_u16(match->flow.in_port.ofp_port));
365 memcpy(ofmatch->dl_src, match->flow.dl_src, ETH_ADDR_LEN);
366 memcpy(ofmatch->dl_dst, match->flow.dl_dst, ETH_ADDR_LEN);
367 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
368 ofmatch->nw_src = match->flow.nw_src;
369 ofmatch->nw_dst = match->flow.nw_dst;
370 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
371 ofmatch->nw_proto = match->flow.nw_proto;
372 ofmatch->tp_src = match->flow.tp_src;
373 ofmatch->tp_dst = match->flow.tp_dst;
374 memset(ofmatch->pad1, '\0', sizeof ofmatch->pad1);
375 memset(ofmatch->pad2, '\0', sizeof ofmatch->pad2);
379 ofputil_pull_ofp11_match(struct ofpbuf *buf, struct match *match,
380 uint16_t *padded_match_len)
382 struct ofp11_match_header *omh = buf->data;
385 if (buf->size < sizeof *omh) {
386 return OFPERR_OFPBMC_BAD_LEN;
389 match_len = ntohs(omh->length);
391 switch (ntohs(omh->type)) {
392 case OFPMT_STANDARD: {
393 struct ofp11_match *om;
395 if (match_len != sizeof *om || buf->size < sizeof *om) {
396 return OFPERR_OFPBMC_BAD_LEN;
398 om = ofpbuf_pull(buf, sizeof *om);
399 if (padded_match_len) {
400 *padded_match_len = match_len;
402 return ofputil_match_from_ofp11_match(om, match);
406 if (padded_match_len) {
407 *padded_match_len = ROUND_UP(match_len, 8);
409 return oxm_pull_match(buf, match);
412 return OFPERR_OFPBMC_BAD_TYPE;
416 /* Converts the ofp11_match in 'ofmatch' into a struct match in 'match'.
417 * Returns 0 if successful, otherwise an OFPERR_* value. */
419 ofputil_match_from_ofp11_match(const struct ofp11_match *ofmatch,
422 uint16_t wc = ntohl(ofmatch->wildcards);
423 uint8_t dl_src_mask[ETH_ADDR_LEN];
424 uint8_t dl_dst_mask[ETH_ADDR_LEN];
425 bool ipv4, arp, rarp;
428 match_init_catchall(match);
430 if (!(wc & OFPFW11_IN_PORT)) {
434 error = ofputil_port_from_ofp11(ofmatch->in_port, &ofp_port);
436 return OFPERR_OFPBMC_BAD_VALUE;
438 match_set_in_port(match, ofp_port);
441 for (i = 0; i < ETH_ADDR_LEN; i++) {
442 dl_src_mask[i] = ~ofmatch->dl_src_mask[i];
444 match_set_dl_src_masked(match, ofmatch->dl_src, dl_src_mask);
446 for (i = 0; i < ETH_ADDR_LEN; i++) {
447 dl_dst_mask[i] = ~ofmatch->dl_dst_mask[i];
449 match_set_dl_dst_masked(match, ofmatch->dl_dst, dl_dst_mask);
451 if (!(wc & OFPFW11_DL_VLAN)) {
452 if (ofmatch->dl_vlan == htons(OFPVID11_NONE)) {
453 /* Match only packets without a VLAN tag. */
454 match->flow.vlan_tci = htons(0);
455 match->wc.masks.vlan_tci = OVS_BE16_MAX;
457 if (ofmatch->dl_vlan == htons(OFPVID11_ANY)) {
458 /* Match any packet with a VLAN tag regardless of VID. */
459 match->flow.vlan_tci = htons(VLAN_CFI);
460 match->wc.masks.vlan_tci = htons(VLAN_CFI);
461 } else if (ntohs(ofmatch->dl_vlan) < 4096) {
462 /* Match only packets with the specified VLAN VID. */
463 match->flow.vlan_tci = htons(VLAN_CFI) | ofmatch->dl_vlan;
464 match->wc.masks.vlan_tci = htons(VLAN_CFI | VLAN_VID_MASK);
467 return OFPERR_OFPBMC_BAD_VALUE;
470 if (!(wc & OFPFW11_DL_VLAN_PCP)) {
471 if (ofmatch->dl_vlan_pcp <= 7) {
472 match->flow.vlan_tci |= htons(ofmatch->dl_vlan_pcp
474 match->wc.masks.vlan_tci |= htons(VLAN_PCP_MASK);
477 return OFPERR_OFPBMC_BAD_VALUE;
483 if (!(wc & OFPFW11_DL_TYPE)) {
484 match_set_dl_type(match,
485 ofputil_dl_type_from_openflow(ofmatch->dl_type));
488 ipv4 = match->flow.dl_type == htons(ETH_TYPE_IP);
489 arp = match->flow.dl_type == htons(ETH_TYPE_ARP);
490 rarp = match->flow.dl_type == htons(ETH_TYPE_RARP);
492 if (ipv4 && !(wc & OFPFW11_NW_TOS)) {
493 if (ofmatch->nw_tos & ~IP_DSCP_MASK) {
495 return OFPERR_OFPBMC_BAD_VALUE;
498 match_set_nw_dscp(match, ofmatch->nw_tos);
501 if (ipv4 || arp || rarp) {
502 if (!(wc & OFPFW11_NW_PROTO)) {
503 match_set_nw_proto(match, ofmatch->nw_proto);
505 match_set_nw_src_masked(match, ofmatch->nw_src, ~ofmatch->nw_src_mask);
506 match_set_nw_dst_masked(match, ofmatch->nw_dst, ~ofmatch->nw_dst_mask);
509 #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
510 if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) {
511 switch (match->flow.nw_proto) {
513 /* "A.2.3 Flow Match Structures" in OF1.1 says:
515 * The tp_src and tp_dst fields will be ignored unless the
516 * network protocol specified is as TCP, UDP or SCTP.
518 * but I'm pretty sure we should support ICMP too, otherwise
519 * that's a regression from OF1.0. */
520 if (!(wc & OFPFW11_TP_SRC)) {
521 uint16_t icmp_type = ntohs(ofmatch->tp_src);
522 if (icmp_type < 0x100) {
523 match_set_icmp_type(match, icmp_type);
525 return OFPERR_OFPBMC_BAD_FIELD;
528 if (!(wc & OFPFW11_TP_DST)) {
529 uint16_t icmp_code = ntohs(ofmatch->tp_dst);
530 if (icmp_code < 0x100) {
531 match_set_icmp_code(match, icmp_code);
533 return OFPERR_OFPBMC_BAD_FIELD;
541 if (!(wc & (OFPFW11_TP_SRC))) {
542 match_set_tp_src(match, ofmatch->tp_src);
544 if (!(wc & (OFPFW11_TP_DST))) {
545 match_set_tp_dst(match, ofmatch->tp_dst);
550 /* OF1.1 says explicitly to ignore this. */
555 if (eth_type_mpls(match->flow.dl_type)) {
556 if (!(wc & OFPFW11_MPLS_LABEL)) {
557 match_set_mpls_label(match, 0, ofmatch->mpls_label);
559 if (!(wc & OFPFW11_MPLS_TC)) {
560 match_set_mpls_tc(match, 0, ofmatch->mpls_tc);
564 match_set_metadata_masked(match, ofmatch->metadata,
565 ~ofmatch->metadata_mask);
570 /* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */
572 ofputil_match_to_ofp11_match(const struct match *match,
573 struct ofp11_match *ofmatch)
578 memset(ofmatch, 0, sizeof *ofmatch);
579 ofmatch->omh.type = htons(OFPMT_STANDARD);
580 ofmatch->omh.length = htons(OFPMT11_STANDARD_LENGTH);
582 if (!match->wc.masks.in_port.ofp_port) {
583 wc |= OFPFW11_IN_PORT;
585 ofmatch->in_port = ofputil_port_to_ofp11(match->flow.in_port.ofp_port);
588 memcpy(ofmatch->dl_src, match->flow.dl_src, ETH_ADDR_LEN);
589 for (i = 0; i < ETH_ADDR_LEN; i++) {
590 ofmatch->dl_src_mask[i] = ~match->wc.masks.dl_src[i];
593 memcpy(ofmatch->dl_dst, match->flow.dl_dst, ETH_ADDR_LEN);
594 for (i = 0; i < ETH_ADDR_LEN; i++) {
595 ofmatch->dl_dst_mask[i] = ~match->wc.masks.dl_dst[i];
598 if (match->wc.masks.vlan_tci == htons(0)) {
599 wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP;
600 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
601 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
602 ofmatch->dl_vlan = htons(OFPVID11_NONE);
603 wc |= OFPFW11_DL_VLAN_PCP;
605 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
606 ofmatch->dl_vlan = htons(OFPVID11_ANY);
608 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
611 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
612 wc |= OFPFW11_DL_VLAN_PCP;
614 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
618 if (!match->wc.masks.dl_type) {
619 wc |= OFPFW11_DL_TYPE;
621 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
624 if (!(match->wc.masks.nw_tos & IP_DSCP_MASK)) {
625 wc |= OFPFW11_NW_TOS;
627 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
630 if (!match->wc.masks.nw_proto) {
631 wc |= OFPFW11_NW_PROTO;
633 ofmatch->nw_proto = match->flow.nw_proto;
636 ofmatch->nw_src = match->flow.nw_src;
637 ofmatch->nw_src_mask = ~match->wc.masks.nw_src;
638 ofmatch->nw_dst = match->flow.nw_dst;
639 ofmatch->nw_dst_mask = ~match->wc.masks.nw_dst;
641 if (!match->wc.masks.tp_src) {
642 wc |= OFPFW11_TP_SRC;
644 ofmatch->tp_src = match->flow.tp_src;
647 if (!match->wc.masks.tp_dst) {
648 wc |= OFPFW11_TP_DST;
650 ofmatch->tp_dst = match->flow.tp_dst;
653 if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK))) {
654 wc |= OFPFW11_MPLS_LABEL;
656 ofmatch->mpls_label = htonl(mpls_lse_to_label(
657 match->flow.mpls_lse[0]));
660 if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK))) {
661 wc |= OFPFW11_MPLS_TC;
663 ofmatch->mpls_tc = mpls_lse_to_tc(match->flow.mpls_lse[0]);
666 ofmatch->metadata = match->flow.metadata;
667 ofmatch->metadata_mask = ~match->wc.masks.metadata;
669 ofmatch->wildcards = htonl(wc);
672 /* Returns the "typical" length of a match for 'protocol', for use in
673 * estimating space to preallocate. */
675 ofputil_match_typical_len(enum ofputil_protocol protocol)
678 case OFPUTIL_P_OF10_STD:
679 case OFPUTIL_P_OF10_STD_TID:
680 return sizeof(struct ofp10_match);
682 case OFPUTIL_P_OF10_NXM:
683 case OFPUTIL_P_OF10_NXM_TID:
684 return NXM_TYPICAL_LEN;
686 case OFPUTIL_P_OF11_STD:
687 return sizeof(struct ofp11_match);
689 case OFPUTIL_P_OF12_OXM:
690 case OFPUTIL_P_OF13_OXM:
691 case OFPUTIL_P_OF14_OXM:
692 case OFPUTIL_P_OF15_OXM:
693 return NXM_TYPICAL_LEN;
700 /* Appends to 'b' an struct ofp11_match_header followed by a match that
701 * expresses 'match' properly for 'protocol', plus enough zero bytes to pad the
702 * data appended out to a multiple of 8. 'protocol' must be one that is usable
703 * in OpenFlow 1.1 or later.
705 * This function can cause 'b''s data to be reallocated.
707 * Returns the number of bytes appended to 'b', excluding the padding. Never
710 ofputil_put_ofp11_match(struct ofpbuf *b, const struct match *match,
711 enum ofputil_protocol protocol)
714 case OFPUTIL_P_OF10_STD:
715 case OFPUTIL_P_OF10_STD_TID:
716 case OFPUTIL_P_OF10_NXM:
717 case OFPUTIL_P_OF10_NXM_TID:
720 case OFPUTIL_P_OF11_STD: {
721 struct ofp11_match *om;
723 /* Make sure that no padding is needed. */
724 BUILD_ASSERT_DECL(sizeof *om % 8 == 0);
726 om = ofpbuf_put_uninit(b, sizeof *om);
727 ofputil_match_to_ofp11_match(match, om);
731 case OFPUTIL_P_OF12_OXM:
732 case OFPUTIL_P_OF13_OXM:
733 case OFPUTIL_P_OF14_OXM:
734 case OFPUTIL_P_OF15_OXM:
735 return oxm_put_match(b, match,
736 ofputil_protocol_to_ofp_version(protocol));
742 /* Given a 'dl_type' value in the format used in struct flow, returns the
743 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
746 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
748 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
749 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
753 /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
754 * structure, returns the corresponding 'dl_type' value for use in struct
757 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
759 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
760 ? htons(FLOW_DL_TYPE_NONE)
766 struct proto_abbrev {
767 enum ofputil_protocol protocol;
771 /* Most users really don't care about some of the differences between
772 * protocols. These abbreviations help with that. */
773 static const struct proto_abbrev proto_abbrevs[] = {
774 { OFPUTIL_P_ANY, "any" },
775 { OFPUTIL_P_OF10_STD_ANY, "OpenFlow10" },
776 { OFPUTIL_P_OF10_NXM_ANY, "NXM" },
777 { OFPUTIL_P_ANY_OXM, "OXM" },
779 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
781 enum ofputil_protocol ofputil_flow_dump_protocols[] = {
790 size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
792 /* Returns the set of ofputil_protocols that are supported with the given
793 * OpenFlow 'version'. 'version' should normally be an 8-bit OpenFlow version
794 * identifier (e.g. 0x01 for OpenFlow 1.0, 0x02 for OpenFlow 1.1). Returns 0
795 * if 'version' is not supported or outside the valid range. */
796 enum ofputil_protocol
797 ofputil_protocols_from_ofp_version(enum ofp_version version)
801 return OFPUTIL_P_OF10_STD_ANY | OFPUTIL_P_OF10_NXM_ANY;
803 return OFPUTIL_P_OF11_STD;
805 return OFPUTIL_P_OF12_OXM;
807 return OFPUTIL_P_OF13_OXM;
809 return OFPUTIL_P_OF14_OXM;
811 return OFPUTIL_P_OF15_OXM;
817 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
818 * connection that has negotiated the given 'version'. 'version' should
819 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
820 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
821 * outside the valid range. */
822 enum ofputil_protocol
823 ofputil_protocol_from_ofp_version(enum ofp_version version)
825 return rightmost_1bit(ofputil_protocols_from_ofp_version(version));
828 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
829 * etc.) that corresponds to 'protocol'. */
831 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
834 case OFPUTIL_P_OF10_STD:
835 case OFPUTIL_P_OF10_STD_TID:
836 case OFPUTIL_P_OF10_NXM:
837 case OFPUTIL_P_OF10_NXM_TID:
838 return OFP10_VERSION;
839 case OFPUTIL_P_OF11_STD:
840 return OFP11_VERSION;
841 case OFPUTIL_P_OF12_OXM:
842 return OFP12_VERSION;
843 case OFPUTIL_P_OF13_OXM:
844 return OFP13_VERSION;
845 case OFPUTIL_P_OF14_OXM:
846 return OFP14_VERSION;
847 case OFPUTIL_P_OF15_OXM:
848 return OFP15_VERSION;
854 /* Returns a bitmap of OpenFlow versions that are supported by at
855 * least one of the 'protocols'. */
857 ofputil_protocols_to_version_bitmap(enum ofputil_protocol protocols)
861 for (; protocols; protocols = zero_rightmost_1bit(protocols)) {
862 enum ofputil_protocol protocol = rightmost_1bit(protocols);
864 bitmap |= 1u << ofputil_protocol_to_ofp_version(protocol);
870 /* Returns the set of protocols that are supported on top of the
871 * OpenFlow versions included in 'bitmap'. */
872 enum ofputil_protocol
873 ofputil_protocols_from_version_bitmap(uint32_t bitmap)
875 enum ofputil_protocol protocols = 0;
877 for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
878 enum ofp_version version = rightmost_1bit_idx(bitmap);
880 protocols |= ofputil_protocols_from_ofp_version(version);
886 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
889 ofputil_protocol_is_valid(enum ofputil_protocol protocol)
891 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
894 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
895 * extension turned on or off if 'enable' is true or false, respectively.
897 * This extension is only useful for protocols whose "standard" version does
898 * not allow specific tables to be modified. In particular, this is true of
899 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
900 * specifies a table ID and so there is no need for such an extension. When
901 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
902 * extension, this function just returns its 'protocol' argument unchanged
903 * regardless of the value of 'enable'. */
904 enum ofputil_protocol
905 ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
908 case OFPUTIL_P_OF10_STD:
909 case OFPUTIL_P_OF10_STD_TID:
910 return enable ? OFPUTIL_P_OF10_STD_TID : OFPUTIL_P_OF10_STD;
912 case OFPUTIL_P_OF10_NXM:
913 case OFPUTIL_P_OF10_NXM_TID:
914 return enable ? OFPUTIL_P_OF10_NXM_TID : OFPUTIL_P_OF10_NXM;
916 case OFPUTIL_P_OF11_STD:
917 return OFPUTIL_P_OF11_STD;
919 case OFPUTIL_P_OF12_OXM:
920 return OFPUTIL_P_OF12_OXM;
922 case OFPUTIL_P_OF13_OXM:
923 return OFPUTIL_P_OF13_OXM;
925 case OFPUTIL_P_OF14_OXM:
926 return OFPUTIL_P_OF14_OXM;
928 case OFPUTIL_P_OF15_OXM:
929 return OFPUTIL_P_OF15_OXM;
936 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
937 * some extension to a standard protocol version, the return value is the
938 * standard version of that protocol without any extension. If 'protocol' is a
939 * standard protocol version, returns 'protocol' unchanged. */
940 enum ofputil_protocol
941 ofputil_protocol_to_base(enum ofputil_protocol protocol)
943 return ofputil_protocol_set_tid(protocol, false);
946 /* Returns 'new_base' with any extensions taken from 'cur'. */
947 enum ofputil_protocol
948 ofputil_protocol_set_base(enum ofputil_protocol cur,
949 enum ofputil_protocol new_base)
951 bool tid = (cur & OFPUTIL_P_TID) != 0;
954 case OFPUTIL_P_OF10_STD:
955 case OFPUTIL_P_OF10_STD_TID:
956 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD, tid);
958 case OFPUTIL_P_OF10_NXM:
959 case OFPUTIL_P_OF10_NXM_TID:
960 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM, tid);
962 case OFPUTIL_P_OF11_STD:
963 return ofputil_protocol_set_tid(OFPUTIL_P_OF11_STD, tid);
965 case OFPUTIL_P_OF12_OXM:
966 return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM, tid);
968 case OFPUTIL_P_OF13_OXM:
969 return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM, tid);
971 case OFPUTIL_P_OF14_OXM:
972 return ofputil_protocol_set_tid(OFPUTIL_P_OF14_OXM, tid);
974 case OFPUTIL_P_OF15_OXM:
975 return ofputil_protocol_set_tid(OFPUTIL_P_OF15_OXM, tid);
982 /* Returns a string form of 'protocol', if a simple form exists (that is, if
983 * 'protocol' is either a single protocol or it is a combination of protocols
984 * that have a single abbreviation). Otherwise, returns NULL. */
986 ofputil_protocol_to_string(enum ofputil_protocol protocol)
988 const struct proto_abbrev *p;
990 /* Use a "switch" statement for single-bit names so that we get a compiler
991 * warning if we forget any. */
993 case OFPUTIL_P_OF10_NXM:
994 return "NXM-table_id";
996 case OFPUTIL_P_OF10_NXM_TID:
997 return "NXM+table_id";
999 case OFPUTIL_P_OF10_STD:
1000 return "OpenFlow10-table_id";
1002 case OFPUTIL_P_OF10_STD_TID:
1003 return "OpenFlow10+table_id";
1005 case OFPUTIL_P_OF11_STD:
1006 return "OpenFlow11";
1008 case OFPUTIL_P_OF12_OXM:
1009 return "OXM-OpenFlow12";
1011 case OFPUTIL_P_OF13_OXM:
1012 return "OXM-OpenFlow13";
1014 case OFPUTIL_P_OF14_OXM:
1015 return "OXM-OpenFlow14";
1017 case OFPUTIL_P_OF15_OXM:
1018 return "OXM-OpenFlow15";
1021 /* Check abbreviations. */
1022 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1023 if (protocol == p->protocol) {
1031 /* Returns a string that represents 'protocols'. The return value might be a
1032 * comma-separated list if 'protocols' doesn't have a simple name. The return
1033 * value is "none" if 'protocols' is 0.
1035 * The caller must free the returned string (with free()). */
1037 ofputil_protocols_to_string(enum ofputil_protocol protocols)
1041 ovs_assert(!(protocols & ~OFPUTIL_P_ANY));
1042 if (protocols == 0) {
1043 return xstrdup("none");
1048 const struct proto_abbrev *p;
1052 ds_put_char(&s, ',');
1055 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1056 if ((protocols & p->protocol) == p->protocol) {
1057 ds_put_cstr(&s, p->name);
1058 protocols &= ~p->protocol;
1063 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
1064 enum ofputil_protocol bit = 1u << i;
1066 if (protocols & bit) {
1067 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
1076 return ds_steal_cstr(&s);
1079 static enum ofputil_protocol
1080 ofputil_protocol_from_string__(const char *s, size_t n)
1082 const struct proto_abbrev *p;
1085 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
1086 enum ofputil_protocol bit = 1u << i;
1087 const char *name = ofputil_protocol_to_string(bit);
1089 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
1094 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1095 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
1103 /* Returns the nonempty set of protocols represented by 's', which can be a
1104 * single protocol name or abbreviation or a comma-separated list of them.
1106 * Aborts the program with an error message if 's' is invalid. */
1107 enum ofputil_protocol
1108 ofputil_protocols_from_string(const char *s)
1110 const char *orig_s = s;
1111 enum ofputil_protocol protocols;
1115 enum ofputil_protocol p;
1118 n = strcspn(s, ",");
1124 p = ofputil_protocol_from_string__(s, n);
1126 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
1134 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
1140 ofputil_version_from_string(const char *s)
1142 if (!strcasecmp(s, "OpenFlow10")) {
1143 return OFP10_VERSION;
1145 if (!strcasecmp(s, "OpenFlow11")) {
1146 return OFP11_VERSION;
1148 if (!strcasecmp(s, "OpenFlow12")) {
1149 return OFP12_VERSION;
1151 if (!strcasecmp(s, "OpenFlow13")) {
1152 return OFP13_VERSION;
1154 if (!strcasecmp(s, "OpenFlow14")) {
1155 return OFP14_VERSION;
1157 if (!strcasecmp(s, "OpenFlow15")) {
1158 return OFP15_VERSION;
1164 is_delimiter(unsigned char c)
1166 return isspace(c) || c == ',';
1170 ofputil_versions_from_string(const char *s)
1173 uint32_t bitmap = 0;
1180 if (is_delimiter(s[i])) {
1185 while (s[i + j] && !is_delimiter(s[i + j])) {
1188 key = xmemdup0(s + i, j);
1189 version = ofputil_version_from_string(key);
1191 VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key);
1194 bitmap |= 1u << version;
1202 ofputil_versions_from_strings(char ** const s, size_t count)
1204 uint32_t bitmap = 0;
1207 int version = ofputil_version_from_string(s[count]);
1209 VLOG_WARN("Unknown OpenFlow version: \"%s\"", s[count]);
1211 bitmap |= 1u << version;
1219 ofputil_version_to_string(enum ofp_version ofp_version)
1221 switch (ofp_version) {
1223 return "OpenFlow10";
1225 return "OpenFlow11";
1227 return "OpenFlow12";
1229 return "OpenFlow13";
1231 return "OpenFlow14";
1233 return "OpenFlow15";
1240 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
1242 switch (packet_in_format) {
1243 case NXPIF_OPENFLOW10:
1252 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
1254 switch (packet_in_format) {
1255 case NXPIF_OPENFLOW10:
1256 return "openflow10";
1265 ofputil_packet_in_format_from_string(const char *s)
1267 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
1268 : !strcmp(s, "nxm") ? NXPIF_NXM
1273 ofputil_format_version(struct ds *msg, enum ofp_version version)
1275 ds_put_format(msg, "0x%02x", version);
1279 ofputil_format_version_name(struct ds *msg, enum ofp_version version)
1281 ds_put_cstr(msg, ofputil_version_to_string(version));
1285 ofputil_format_version_bitmap__(struct ds *msg, uint32_t bitmap,
1286 void (*format_version)(struct ds *msg,
1290 format_version(msg, raw_ctz(bitmap));
1291 bitmap = zero_rightmost_1bit(bitmap);
1293 ds_put_cstr(msg, ", ");
1299 ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap)
1301 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version);
1305 ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap)
1307 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version_name);
1311 ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header *oheh,
1312 uint32_t *allowed_versionsp)
1314 uint16_t bitmap_len = ntohs(oheh->length) - sizeof *oheh;
1315 const ovs_be32 *bitmap = ALIGNED_CAST(const ovs_be32 *, oheh + 1);
1316 uint32_t allowed_versions;
1318 if (!bitmap_len || bitmap_len % sizeof *bitmap) {
1322 /* Only use the first 32-bit element of the bitmap as that is all the
1323 * current implementation supports. Subsequent elements are ignored which
1324 * should have no effect on session negotiation until Open vSwitch supports
1325 * wire-protocol versions greater than 31.
1327 allowed_versions = ntohl(bitmap[0]);
1329 if (allowed_versions & 1) {
1330 /* There's no OpenFlow version 0. */
1331 VLOG_WARN_RL(&bad_ofmsg_rl, "peer claims to support invalid OpenFlow "
1333 allowed_versions &= ~1u;
1336 if (!allowed_versions) {
1337 VLOG_WARN_RL(&bad_ofmsg_rl, "peer does not support any OpenFlow "
1338 "version (between 0x01 and 0x1f)");
1342 *allowed_versionsp = allowed_versions;
1347 version_bitmap_from_version(uint8_t ofp_version)
1349 return ((ofp_version < 32 ? 1u << ofp_version : 0) - 1) << 1;
1352 /* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions'
1353 * the set of OpenFlow versions for which 'oh' announces support.
1355 * Because of how OpenFlow defines OFPT_HELLO messages, this function is always
1356 * successful, and thus '*allowed_versions' is always initialized. However, it
1357 * returns false if 'oh' contains some data that could not be fully understood,
1358 * true if 'oh' was completely parsed. */
1360 ofputil_decode_hello(const struct ofp_header *oh, uint32_t *allowed_versions)
1365 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
1366 ofpbuf_pull(&msg, sizeof *oh);
1368 *allowed_versions = version_bitmap_from_version(oh->version);
1370 const struct ofp_hello_elem_header *oheh;
1373 if (msg.size < sizeof *oheh) {
1378 len = ntohs(oheh->length);
1379 if (len < sizeof *oheh || !ofpbuf_try_pull(&msg, ROUND_UP(len, 8))) {
1383 if (oheh->type != htons(OFPHET_VERSIONBITMAP)
1384 || !ofputil_decode_hello_bitmap(oheh, allowed_versions)) {
1392 /* Returns true if 'allowed_versions' needs to be accompanied by a version
1393 * bitmap to be correctly expressed in an OFPT_HELLO message. */
1395 should_send_version_bitmap(uint32_t allowed_versions)
1397 return !is_pow2((allowed_versions >> 1) + 1);
1400 /* Create an OFPT_HELLO message that expresses support for the OpenFlow
1401 * versions in the 'allowed_versions' bitmaps and returns the message. */
1403 ofputil_encode_hello(uint32_t allowed_versions)
1405 enum ofp_version ofp_version;
1408 ofp_version = leftmost_1bit_idx(allowed_versions);
1409 msg = ofpraw_alloc(OFPRAW_OFPT_HELLO, ofp_version, 0);
1411 if (should_send_version_bitmap(allowed_versions)) {
1412 struct ofp_hello_elem_header *oheh;
1415 map_len = sizeof allowed_versions;
1416 oheh = ofpbuf_put_zeros(msg, ROUND_UP(map_len + sizeof *oheh, 8));
1417 oheh->type = htons(OFPHET_VERSIONBITMAP);
1418 oheh->length = htons(map_len + sizeof *oheh);
1419 *ALIGNED_CAST(ovs_be32 *, oheh + 1) = htonl(allowed_versions);
1421 ofpmsg_update_length(msg);
1427 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1428 * protocol is 'current', at least partly transitions the protocol to 'want'.
1429 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1430 * connection if the switch processes the returned message correctly. (If
1431 * '*next != want' then the caller will have to iterate.)
1433 * If 'current == want', or if it is not possible to transition from 'current'
1434 * to 'want' (because, for example, 'current' and 'want' use different OpenFlow
1435 * protocol versions), returns NULL and stores 'current' in '*next'. */
1437 ofputil_encode_set_protocol(enum ofputil_protocol current,
1438 enum ofputil_protocol want,
1439 enum ofputil_protocol *next)
1441 enum ofp_version cur_version, want_version;
1442 enum ofputil_protocol cur_base, want_base;
1443 bool cur_tid, want_tid;
1445 cur_version = ofputil_protocol_to_ofp_version(current);
1446 want_version = ofputil_protocol_to_ofp_version(want);
1447 if (cur_version != want_version) {
1452 cur_base = ofputil_protocol_to_base(current);
1453 want_base = ofputil_protocol_to_base(want);
1454 if (cur_base != want_base) {
1455 *next = ofputil_protocol_set_base(current, want_base);
1457 switch (want_base) {
1458 case OFPUTIL_P_OF10_NXM:
1459 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1461 case OFPUTIL_P_OF10_STD:
1462 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1464 case OFPUTIL_P_OF11_STD:
1465 case OFPUTIL_P_OF12_OXM:
1466 case OFPUTIL_P_OF13_OXM:
1467 case OFPUTIL_P_OF14_OXM:
1468 case OFPUTIL_P_OF15_OXM:
1469 /* There is only one variant of each OpenFlow 1.1+ protocol, and we
1470 * verified above that we're not trying to change versions. */
1473 case OFPUTIL_P_OF10_STD_TID:
1474 case OFPUTIL_P_OF10_NXM_TID:
1479 cur_tid = (current & OFPUTIL_P_TID) != 0;
1480 want_tid = (want & OFPUTIL_P_TID) != 0;
1481 if (cur_tid != want_tid) {
1482 *next = ofputil_protocol_set_tid(current, want_tid);
1483 return ofputil_make_flow_mod_table_id(want_tid);
1486 ovs_assert(current == want);
1492 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1493 * format to 'nxff'. */
1495 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1497 struct nx_set_flow_format *sff;
1500 ovs_assert(ofputil_nx_flow_format_is_valid(nxff));
1502 msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1503 sff = ofpbuf_put_zeros(msg, sizeof *sff);
1504 sff->format = htonl(nxff);
1509 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1511 enum ofputil_protocol
1512 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1514 switch (flow_format) {
1515 case NXFF_OPENFLOW10:
1516 return OFPUTIL_P_OF10_STD;
1519 return OFPUTIL_P_OF10_NXM;
1526 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1528 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1530 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1533 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1536 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1538 switch (flow_format) {
1539 case NXFF_OPENFLOW10:
1540 return "openflow10";
1549 ofputil_make_set_packet_in_format(enum ofp_version ofp_version,
1550 enum nx_packet_in_format packet_in_format)
1552 struct nx_set_packet_in_format *spif;
1555 msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, ofp_version, 0);
1556 spif = ofpbuf_put_zeros(msg, sizeof *spif);
1557 spif->format = htonl(packet_in_format);
1562 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1563 * extension on or off (according to 'flow_mod_table_id'). */
1565 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1567 struct nx_flow_mod_table_id *nfmti;
1570 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1571 nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
1572 nfmti->set = flow_mod_table_id;
1576 struct ofputil_flow_mod_flag {
1578 enum ofp_version min_version, max_version;
1579 enum ofputil_flow_mod_flags flag;
1582 static const struct ofputil_flow_mod_flag ofputil_flow_mod_flags[] = {
1583 { OFPFF_SEND_FLOW_REM, OFP10_VERSION, 0, OFPUTIL_FF_SEND_FLOW_REM },
1584 { OFPFF_CHECK_OVERLAP, OFP10_VERSION, 0, OFPUTIL_FF_CHECK_OVERLAP },
1585 { OFPFF10_EMERG, OFP10_VERSION, OFP10_VERSION,
1587 { OFPFF12_RESET_COUNTS, OFP12_VERSION, 0, OFPUTIL_FF_RESET_COUNTS },
1588 { OFPFF13_NO_PKT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_PKT_COUNTS },
1589 { OFPFF13_NO_BYT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_BYT_COUNTS },
1594 ofputil_decode_flow_mod_flags(ovs_be16 raw_flags_,
1595 enum ofp_flow_mod_command command,
1596 enum ofp_version version,
1597 enum ofputil_flow_mod_flags *flagsp)
1599 uint16_t raw_flags = ntohs(raw_flags_);
1600 const struct ofputil_flow_mod_flag *f;
1603 for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1604 if (raw_flags & f->raw_flag
1605 && version >= f->min_version
1606 && (!f->max_version || version <= f->max_version)) {
1607 raw_flags &= ~f->raw_flag;
1612 /* In OF1.0 and OF1.1, "add" always resets counters, and other commands
1615 * In OF1.2 and later, OFPFF12_RESET_COUNTS controls whether each command
1616 * resets counters. */
1617 if ((version == OFP10_VERSION || version == OFP11_VERSION)
1618 && command == OFPFC_ADD) {
1619 *flagsp |= OFPUTIL_FF_RESET_COUNTS;
1622 return raw_flags ? OFPERR_OFPFMFC_BAD_FLAGS : 0;
1626 ofputil_encode_flow_mod_flags(enum ofputil_flow_mod_flags flags,
1627 enum ofp_version version)
1629 const struct ofputil_flow_mod_flag *f;
1633 for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1635 && version >= f->min_version
1636 && (!f->max_version || version <= f->max_version)) {
1637 raw_flags |= f->raw_flag;
1641 return htons(raw_flags);
1644 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1645 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1648 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1649 * The caller must initialize 'ofpacts' and retains ownership of it.
1650 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1652 * Does not validate the flow_mod actions. The caller should do that, with
1653 * ofpacts_check(). */
1655 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1656 const struct ofp_header *oh,
1657 enum ofputil_protocol protocol,
1658 struct ofpbuf *ofpacts,
1659 ofp_port_t max_port, uint8_t max_table)
1666 /* Ignored for non-delete actions */
1667 fm->delete_reason = OFPRR_DELETE;
1669 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1670 raw = ofpraw_pull_assert(&b);
1671 if (raw == OFPRAW_OFPT11_FLOW_MOD) {
1672 /* Standard OpenFlow 1.1+ flow_mod. */
1673 const struct ofp11_flow_mod *ofm;
1675 ofm = ofpbuf_pull(&b, sizeof *ofm);
1677 error = ofputil_pull_ofp11_match(&b, &fm->match, NULL);
1682 /* Translate the message. */
1683 fm->priority = ntohs(ofm->priority);
1684 if (ofm->command == OFPFC_ADD
1685 || (oh->version == OFP11_VERSION
1686 && (ofm->command == OFPFC_MODIFY ||
1687 ofm->command == OFPFC_MODIFY_STRICT)
1688 && ofm->cookie_mask == htonll(0))) {
1689 /* In OpenFlow 1.1 only, a "modify" or "modify-strict" that does
1690 * not match on the cookie is treated as an "add" if there is no
1692 fm->cookie = htonll(0);
1693 fm->cookie_mask = htonll(0);
1694 fm->new_cookie = ofm->cookie;
1696 fm->cookie = ofm->cookie;
1697 fm->cookie_mask = ofm->cookie_mask;
1698 fm->new_cookie = OVS_BE64_MAX;
1700 fm->modify_cookie = false;
1701 fm->command = ofm->command;
1705 * OF1.1 entirely forbids table_id == OFPTT_ALL.
1706 * OF1.2+ allows table_id == OFPTT_ALL only for deletes. */
1707 fm->table_id = ofm->table_id;
1708 if (fm->table_id == OFPTT_ALL
1709 && (oh->version == OFP11_VERSION
1710 || (ofm->command != OFPFC_DELETE &&
1711 ofm->command != OFPFC_DELETE_STRICT))) {
1712 return OFPERR_OFPFMFC_BAD_TABLE_ID;
1715 fm->idle_timeout = ntohs(ofm->idle_timeout);
1716 fm->hard_timeout = ntohs(ofm->hard_timeout);
1717 if (oh->version >= OFP14_VERSION && ofm->command == OFPFC_ADD) {
1718 fm->importance = ntohs(ofm->importance);
1722 fm->buffer_id = ntohl(ofm->buffer_id);
1723 error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
1728 fm->out_group = (ofm->command == OFPFC_DELETE ||
1729 ofm->command == OFPFC_DELETE_STRICT
1730 ? ntohl(ofm->out_group)
1732 raw_flags = ofm->flags;
1736 if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1737 /* Standard OpenFlow 1.0 flow_mod. */
1738 const struct ofp10_flow_mod *ofm;
1740 /* Get the ofp10_flow_mod. */
1741 ofm = ofpbuf_pull(&b, sizeof *ofm);
1743 /* Translate the rule. */
1744 ofputil_match_from_ofp10_match(&ofm->match, &fm->match);
1745 ofputil_normalize_match(&fm->match);
1747 /* OpenFlow 1.0 says that exact-match rules have to have the
1748 * highest possible priority. */
1749 fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL)
1750 ? ntohs(ofm->priority)
1753 /* Translate the message. */
1754 command = ntohs(ofm->command);
1755 fm->cookie = htonll(0);
1756 fm->cookie_mask = htonll(0);
1757 fm->new_cookie = ofm->cookie;
1758 fm->idle_timeout = ntohs(ofm->idle_timeout);
1759 fm->hard_timeout = ntohs(ofm->hard_timeout);
1761 fm->buffer_id = ntohl(ofm->buffer_id);
1762 fm->out_port = u16_to_ofp(ntohs(ofm->out_port));
1763 fm->out_group = OFPG11_ANY;
1764 raw_flags = ofm->flags;
1765 } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1766 /* Nicira extended flow_mod. */
1767 const struct nx_flow_mod *nfm;
1769 /* Dissect the message. */
1770 nfm = ofpbuf_pull(&b, sizeof *nfm);
1771 error = nx_pull_match(&b, ntohs(nfm->match_len),
1772 &fm->match, &fm->cookie, &fm->cookie_mask);
1777 /* Translate the message. */
1778 command = ntohs(nfm->command);
1779 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1780 /* Flow additions may only set a new cookie, not match an
1781 * existing cookie. */
1782 return OFPERR_NXBRC_NXM_INVALID;
1784 fm->priority = ntohs(nfm->priority);
1785 fm->new_cookie = nfm->cookie;
1786 fm->idle_timeout = ntohs(nfm->idle_timeout);
1787 fm->hard_timeout = ntohs(nfm->hard_timeout);
1789 fm->buffer_id = ntohl(nfm->buffer_id);
1790 fm->out_port = u16_to_ofp(ntohs(nfm->out_port));
1791 fm->out_group = OFPG11_ANY;
1792 raw_flags = nfm->flags;
1797 fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX;
1798 if (protocol & OFPUTIL_P_TID) {
1799 fm->command = command & 0xff;
1800 fm->table_id = command >> 8;
1802 if (command > 0xff) {
1803 VLOG_WARN_RL(&bad_ofmsg_rl, "flow_mod has explicit table_id "
1804 "but flow_mod_table_id extension is not enabled");
1806 fm->command = command;
1807 fm->table_id = 0xff;
1811 if (fm->command > OFPFC_DELETE_STRICT) {
1812 return OFPERR_OFPFMFC_BAD_COMMAND;
1815 error = ofpacts_pull_openflow_instructions(&b, b.size,
1816 oh->version, ofpacts);
1820 fm->ofpacts = ofpacts->data;
1821 fm->ofpacts_len = ofpacts->size;
1823 error = ofputil_decode_flow_mod_flags(raw_flags, fm->command,
1824 oh->version, &fm->flags);
1829 if (fm->flags & OFPUTIL_FF_EMERG) {
1830 /* We do not support the OpenFlow 1.0 emergency flow cache, which
1831 * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
1833 * OpenFlow 1.0 specifies the error code to use when idle_timeout
1834 * or hard_timeout is nonzero. Otherwise, there is no good error
1835 * code, so just state that the flow table is full. */
1836 return (fm->hard_timeout || fm->idle_timeout
1837 ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT
1838 : OFPERR_OFPFMFC_TABLE_FULL);
1841 return ofpacts_check_consistency(fm->ofpacts, fm->ofpacts_len,
1842 &fm->match.flow, max_port,
1843 fm->table_id, max_table, protocol);
1847 ofputil_pull_bands(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
1848 struct ofpbuf *bands)
1850 const struct ofp13_meter_band_header *ombh;
1851 struct ofputil_meter_band *mb;
1854 ombh = ofpbuf_try_pull(msg, len);
1856 return OFPERR_OFPBRC_BAD_LEN;
1859 while (len >= sizeof (struct ofp13_meter_band_drop)) {
1860 size_t ombh_len = ntohs(ombh->len);
1861 /* All supported band types have the same length. */
1862 if (ombh_len != sizeof (struct ofp13_meter_band_drop)) {
1863 return OFPERR_OFPBRC_BAD_LEN;
1865 mb = ofpbuf_put_uninit(bands, sizeof *mb);
1866 mb->type = ntohs(ombh->type);
1867 if (mb->type != OFPMBT13_DROP && mb->type != OFPMBT13_DSCP_REMARK) {
1868 return OFPERR_OFPMMFC_BAD_BAND;
1870 mb->rate = ntohl(ombh->rate);
1871 mb->burst_size = ntohl(ombh->burst_size);
1872 mb->prec_level = (mb->type == OFPMBT13_DSCP_REMARK) ?
1873 ((struct ofp13_meter_band_dscp_remark *)ombh)->prec_level : 0;
1876 ombh = ALIGNED_CAST(struct ofp13_meter_band_header *,
1877 (char *) ombh + ombh_len);
1880 return OFPERR_OFPBRC_BAD_LEN;
1887 ofputil_decode_meter_mod(const struct ofp_header *oh,
1888 struct ofputil_meter_mod *mm,
1889 struct ofpbuf *bands)
1891 const struct ofp13_meter_mod *omm;
1894 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1895 ofpraw_pull_assert(&b);
1896 omm = ofpbuf_pull(&b, sizeof *omm);
1898 /* Translate the message. */
1899 mm->command = ntohs(omm->command);
1900 if (mm->command != OFPMC13_ADD &&
1901 mm->command != OFPMC13_MODIFY &&
1902 mm->command != OFPMC13_DELETE) {
1903 return OFPERR_OFPMMFC_BAD_COMMAND;
1905 mm->meter.meter_id = ntohl(omm->meter_id);
1907 if (mm->command == OFPMC13_DELETE) {
1908 mm->meter.flags = 0;
1909 mm->meter.n_bands = 0;
1910 mm->meter.bands = NULL;
1914 mm->meter.flags = ntohs(omm->flags);
1915 if (mm->meter.flags & OFPMF13_KBPS &&
1916 mm->meter.flags & OFPMF13_PKTPS) {
1917 return OFPERR_OFPMMFC_BAD_FLAGS;
1919 mm->meter.bands = bands->data;
1921 error = ofputil_pull_bands(&b, b.size, &mm->meter.n_bands, bands);
1930 ofputil_decode_meter_request(const struct ofp_header *oh, uint32_t *meter_id)
1932 const struct ofp13_meter_multipart_request *omr = ofpmsg_body(oh);
1933 *meter_id = ntohl(omr->meter_id);
1937 ofputil_encode_meter_request(enum ofp_version ofp_version,
1938 enum ofputil_meter_request_type type,
1946 case OFPUTIL_METER_CONFIG:
1947 raw = OFPRAW_OFPST13_METER_CONFIG_REQUEST;
1949 case OFPUTIL_METER_STATS:
1950 raw = OFPRAW_OFPST13_METER_REQUEST;
1953 case OFPUTIL_METER_FEATURES:
1954 raw = OFPRAW_OFPST13_METER_FEATURES_REQUEST;
1958 msg = ofpraw_alloc(raw, ofp_version, 0);
1960 if (type != OFPUTIL_METER_FEATURES) {
1961 struct ofp13_meter_multipart_request *omr;
1962 omr = ofpbuf_put_zeros(msg, sizeof *omr);
1963 omr->meter_id = htonl(meter_id);
1969 ofputil_put_bands(uint16_t n_bands, const struct ofputil_meter_band *mb,
1974 for (n = 0; n < n_bands; ++n) {
1975 /* Currently all band types have same size. */
1976 struct ofp13_meter_band_dscp_remark *ombh;
1977 size_t ombh_len = sizeof *ombh;
1979 ombh = ofpbuf_put_zeros(msg, ombh_len);
1981 ombh->type = htons(mb->type);
1982 ombh->len = htons(ombh_len);
1983 ombh->rate = htonl(mb->rate);
1984 ombh->burst_size = htonl(mb->burst_size);
1985 ombh->prec_level = mb->prec_level;
1991 /* Encode a meter stat for 'mc' and append it to 'replies'. */
1993 ofputil_append_meter_config(struct ovs_list *replies,
1994 const struct ofputil_meter_config *mc)
1996 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
1997 size_t start_ofs = msg->size;
1998 struct ofp13_meter_config *reply = ofpbuf_put_uninit(msg, sizeof *reply);
1999 reply->flags = htons(mc->flags);
2000 reply->meter_id = htonl(mc->meter_id);
2002 ofputil_put_bands(mc->n_bands, mc->bands, msg);
2004 reply->length = htons(msg->size - start_ofs);
2006 ofpmp_postappend(replies, start_ofs);
2009 /* Encode a meter stat for 'ms' and append it to 'replies'. */
2011 ofputil_append_meter_stats(struct ovs_list *replies,
2012 const struct ofputil_meter_stats *ms)
2014 struct ofp13_meter_stats *reply;
2018 len = sizeof *reply + ms->n_bands * sizeof(struct ofp13_meter_band_stats);
2019 reply = ofpmp_append(replies, len);
2021 reply->meter_id = htonl(ms->meter_id);
2022 reply->len = htons(len);
2023 memset(reply->pad, 0, sizeof reply->pad);
2024 reply->flow_count = htonl(ms->flow_count);
2025 reply->packet_in_count = htonll(ms->packet_in_count);
2026 reply->byte_in_count = htonll(ms->byte_in_count);
2027 reply->duration_sec = htonl(ms->duration_sec);
2028 reply->duration_nsec = htonl(ms->duration_nsec);
2030 for (n = 0; n < ms->n_bands; ++n) {
2031 const struct ofputil_meter_band_stats *src = &ms->bands[n];
2032 struct ofp13_meter_band_stats *dst = &reply->band_stats[n];
2034 dst->packet_band_count = htonll(src->packet_count);
2035 dst->byte_band_count = htonll(src->byte_count);
2039 /* Converts an OFPMP_METER_CONFIG reply in 'msg' into an abstract
2040 * ofputil_meter_config in 'mc', with mc->bands pointing to bands decoded into
2041 * 'bands'. The caller must have initialized 'bands' and retains ownership of
2042 * it across the call.
2044 * Multiple OFPST13_METER_CONFIG replies can be packed into a single OpenFlow
2045 * message. Calling this function multiple times for a single 'msg' iterates
2046 * through the replies. 'bands' is cleared for each reply.
2048 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2049 * otherwise a positive errno value. */
2051 ofputil_decode_meter_config(struct ofpbuf *msg,
2052 struct ofputil_meter_config *mc,
2053 struct ofpbuf *bands)
2055 const struct ofp13_meter_config *omc;
2058 /* Pull OpenFlow headers for the first call. */
2060 ofpraw_pull_assert(msg);
2067 omc = ofpbuf_try_pull(msg, sizeof *omc);
2069 VLOG_WARN_RL(&bad_ofmsg_rl,
2070 "OFPMP_METER_CONFIG reply has %"PRIu32" leftover bytes at end",
2072 return OFPERR_OFPBRC_BAD_LEN;
2075 ofpbuf_clear(bands);
2076 err = ofputil_pull_bands(msg, ntohs(omc->length) - sizeof *omc,
2077 &mc->n_bands, bands);
2081 mc->meter_id = ntohl(omc->meter_id);
2082 mc->flags = ntohs(omc->flags);
2083 mc->bands = bands->data;
2089 ofputil_pull_band_stats(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
2090 struct ofpbuf *bands)
2092 const struct ofp13_meter_band_stats *ombs;
2093 struct ofputil_meter_band_stats *mbs;
2096 ombs = ofpbuf_try_pull(msg, len);
2098 return OFPERR_OFPBRC_BAD_LEN;
2101 n = len / sizeof *ombs;
2102 if (len != n * sizeof *ombs) {
2103 return OFPERR_OFPBRC_BAD_LEN;
2106 mbs = ofpbuf_put_uninit(bands, len);
2108 for (i = 0; i < n; ++i) {
2109 mbs[i].packet_count = ntohll(ombs[i].packet_band_count);
2110 mbs[i].byte_count = ntohll(ombs[i].byte_band_count);
2116 /* Converts an OFPMP_METER reply in 'msg' into an abstract
2117 * ofputil_meter_stats in 'ms', with ms->bands pointing to band stats
2118 * decoded into 'bands'.
2120 * Multiple OFPMP_METER replies can be packed into a single OpenFlow
2121 * message. Calling this function multiple times for a single 'msg' iterates
2122 * through the replies. 'bands' is cleared for each reply.
2124 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2125 * otherwise a positive errno value. */
2127 ofputil_decode_meter_stats(struct ofpbuf *msg,
2128 struct ofputil_meter_stats *ms,
2129 struct ofpbuf *bands)
2131 const struct ofp13_meter_stats *oms;
2134 /* Pull OpenFlow headers for the first call. */
2136 ofpraw_pull_assert(msg);
2143 oms = ofpbuf_try_pull(msg, sizeof *oms);
2145 VLOG_WARN_RL(&bad_ofmsg_rl,
2146 "OFPMP_METER reply has %"PRIu32" leftover bytes at end",
2148 return OFPERR_OFPBRC_BAD_LEN;
2151 ofpbuf_clear(bands);
2152 err = ofputil_pull_band_stats(msg, ntohs(oms->len) - sizeof *oms,
2153 &ms->n_bands, bands);
2157 ms->meter_id = ntohl(oms->meter_id);
2158 ms->flow_count = ntohl(oms->flow_count);
2159 ms->packet_in_count = ntohll(oms->packet_in_count);
2160 ms->byte_in_count = ntohll(oms->byte_in_count);
2161 ms->duration_sec = ntohl(oms->duration_sec);
2162 ms->duration_nsec = ntohl(oms->duration_nsec);
2163 ms->bands = bands->data;
2169 ofputil_decode_meter_features(const struct ofp_header *oh,
2170 struct ofputil_meter_features *mf)
2172 const struct ofp13_meter_features *omf = ofpmsg_body(oh);
2174 mf->max_meters = ntohl(omf->max_meter);
2175 mf->band_types = ntohl(omf->band_types);
2176 mf->capabilities = ntohl(omf->capabilities);
2177 mf->max_bands = omf->max_bands;
2178 mf->max_color = omf->max_color;
2182 ofputil_encode_meter_features_reply(const struct ofputil_meter_features *mf,
2183 const struct ofp_header *request)
2185 struct ofpbuf *reply;
2186 struct ofp13_meter_features *omf;
2188 reply = ofpraw_alloc_stats_reply(request, 0);
2189 omf = ofpbuf_put_zeros(reply, sizeof *omf);
2191 omf->max_meter = htonl(mf->max_meters);
2192 omf->band_types = htonl(mf->band_types);
2193 omf->capabilities = htonl(mf->capabilities);
2194 omf->max_bands = mf->max_bands;
2195 omf->max_color = mf->max_color;
2201 ofputil_encode_meter_mod(enum ofp_version ofp_version,
2202 const struct ofputil_meter_mod *mm)
2206 struct ofp13_meter_mod *omm;
2208 msg = ofpraw_alloc(OFPRAW_OFPT13_METER_MOD, ofp_version,
2209 NXM_TYPICAL_LEN + mm->meter.n_bands * 16);
2210 omm = ofpbuf_put_zeros(msg, sizeof *omm);
2211 omm->command = htons(mm->command);
2212 if (mm->command != OFPMC13_DELETE) {
2213 omm->flags = htons(mm->meter.flags);
2215 omm->meter_id = htonl(mm->meter.meter_id);
2217 ofputil_put_bands(mm->meter.n_bands, mm->meter.bands, msg);
2219 ofpmsg_update_length(msg);
2224 ofputil_tid_command(const struct ofputil_flow_mod *fm,
2225 enum ofputil_protocol protocol)
2227 return htons(protocol & OFPUTIL_P_TID
2228 ? (fm->command & 0xff) | (fm->table_id << 8)
2232 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
2233 * 'protocol' and returns the message. */
2235 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
2236 enum ofputil_protocol protocol)
2238 enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
2239 ovs_be16 raw_flags = ofputil_encode_flow_mod_flags(fm->flags, version);
2243 case OFPUTIL_P_OF11_STD:
2244 case OFPUTIL_P_OF12_OXM:
2245 case OFPUTIL_P_OF13_OXM:
2246 case OFPUTIL_P_OF14_OXM:
2247 case OFPUTIL_P_OF15_OXM: {
2248 struct ofp11_flow_mod *ofm;
2251 tailroom = ofputil_match_typical_len(protocol) + fm->ofpacts_len;
2252 msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, version, tailroom);
2253 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
2254 if ((protocol == OFPUTIL_P_OF11_STD
2255 && (fm->command == OFPFC_MODIFY ||
2256 fm->command == OFPFC_MODIFY_STRICT)
2257 && fm->cookie_mask == htonll(0))
2258 || fm->command == OFPFC_ADD) {
2259 ofm->cookie = fm->new_cookie;
2261 ofm->cookie = fm->cookie & fm->cookie_mask;
2263 ofm->cookie_mask = fm->cookie_mask;
2264 if (fm->table_id != OFPTT_ALL
2265 || (protocol != OFPUTIL_P_OF11_STD
2266 && (fm->command == OFPFC_DELETE ||
2267 fm->command == OFPFC_DELETE_STRICT))) {
2268 ofm->table_id = fm->table_id;
2272 ofm->command = fm->command;
2273 ofm->idle_timeout = htons(fm->idle_timeout);
2274 ofm->hard_timeout = htons(fm->hard_timeout);
2275 ofm->priority = htons(fm->priority);
2276 ofm->buffer_id = htonl(fm->buffer_id);
2277 ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
2278 ofm->out_group = htonl(fm->out_group);
2279 ofm->flags = raw_flags;
2280 if (version >= OFP14_VERSION && fm->command == OFPFC_ADD) {
2281 ofm->importance = htons(fm->importance);
2283 ofm->importance = 0;
2285 ofputil_put_ofp11_match(msg, &fm->match, protocol);
2286 ofpacts_put_openflow_instructions(fm->ofpacts, fm->ofpacts_len, msg,
2291 case OFPUTIL_P_OF10_STD:
2292 case OFPUTIL_P_OF10_STD_TID: {
2293 struct ofp10_flow_mod *ofm;
2295 msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
2297 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
2298 ofputil_match_to_ofp10_match(&fm->match, &ofm->match);
2299 ofm->cookie = fm->new_cookie;
2300 ofm->command = ofputil_tid_command(fm, protocol);
2301 ofm->idle_timeout = htons(fm->idle_timeout);
2302 ofm->hard_timeout = htons(fm->hard_timeout);
2303 ofm->priority = htons(fm->priority);
2304 ofm->buffer_id = htonl(fm->buffer_id);
2305 ofm->out_port = htons(ofp_to_u16(fm->out_port));
2306 ofm->flags = raw_flags;
2307 ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2312 case OFPUTIL_P_OF10_NXM:
2313 case OFPUTIL_P_OF10_NXM_TID: {
2314 struct nx_flow_mod *nfm;
2317 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
2318 NXM_TYPICAL_LEN + fm->ofpacts_len);
2319 nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
2320 nfm->command = ofputil_tid_command(fm, protocol);
2321 nfm->cookie = fm->new_cookie;
2322 match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask);
2324 nfm->idle_timeout = htons(fm->idle_timeout);
2325 nfm->hard_timeout = htons(fm->hard_timeout);
2326 nfm->priority = htons(fm->priority);
2327 nfm->buffer_id = htonl(fm->buffer_id);
2328 nfm->out_port = htons(ofp_to_u16(fm->out_port));
2329 nfm->flags = raw_flags;
2330 nfm->match_len = htons(match_len);
2331 ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2340 ofpmsg_update_length(msg);
2345 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr,
2346 const struct ofp10_flow_stats_request *ofsr,
2349 fsr->aggregate = aggregate;
2350 ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match);
2351 fsr->out_port = u16_to_ofp(ntohs(ofsr->out_port));
2352 fsr->out_group = OFPG11_ANY;
2353 fsr->table_id = ofsr->table_id;
2354 fsr->cookie = fsr->cookie_mask = htonll(0);
2360 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr,
2361 struct ofpbuf *b, bool aggregate)
2363 const struct ofp11_flow_stats_request *ofsr;
2366 ofsr = ofpbuf_pull(b, sizeof *ofsr);
2367 fsr->aggregate = aggregate;
2368 fsr->table_id = ofsr->table_id;
2369 error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port);
2373 fsr->out_group = ntohl(ofsr->out_group);
2374 fsr->cookie = ofsr->cookie;
2375 fsr->cookie_mask = ofsr->cookie_mask;
2376 error = ofputil_pull_ofp11_match(b, &fsr->match, NULL);
2385 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
2386 struct ofpbuf *b, bool aggregate)
2388 const struct nx_flow_stats_request *nfsr;
2391 nfsr = ofpbuf_pull(b, sizeof *nfsr);
2392 error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match,
2393 &fsr->cookie, &fsr->cookie_mask);
2398 return OFPERR_OFPBRC_BAD_LEN;
2401 fsr->aggregate = aggregate;
2402 fsr->out_port = u16_to_ofp(ntohs(nfsr->out_port));
2403 fsr->out_group = OFPG11_ANY;
2404 fsr->table_id = nfsr->table_id;
2409 /* Constructs and returns an OFPT_QUEUE_GET_CONFIG request for the specified
2410 * 'port', suitable for OpenFlow version 'version'. */
2412 ofputil_encode_queue_get_config_request(enum ofp_version version,
2415 struct ofpbuf *request;
2417 if (version == OFP10_VERSION) {
2418 struct ofp10_queue_get_config_request *qgcr10;
2420 request = ofpraw_alloc(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST,
2422 qgcr10 = ofpbuf_put_zeros(request, sizeof *qgcr10);
2423 qgcr10->port = htons(ofp_to_u16(port));
2425 struct ofp11_queue_get_config_request *qgcr11;
2427 request = ofpraw_alloc(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST,
2429 qgcr11 = ofpbuf_put_zeros(request, sizeof *qgcr11);
2430 qgcr11->port = ofputil_port_to_ofp11(port);
2436 /* Parses OFPT_QUEUE_GET_CONFIG request 'oh', storing the port specified by the
2437 * request into '*port'. Returns 0 if successful, otherwise an OpenFlow error
2440 ofputil_decode_queue_get_config_request(const struct ofp_header *oh,
2443 const struct ofp10_queue_get_config_request *qgcr10;
2444 const struct ofp11_queue_get_config_request *qgcr11;
2448 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2449 raw = ofpraw_pull_assert(&b);
2451 switch ((int) raw) {
2452 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2454 *port = u16_to_ofp(ntohs(qgcr10->port));
2457 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2459 return ofputil_port_from_ofp11(qgcr11->port, port);
2465 /* Constructs and returns the beginning of a reply to
2466 * OFPT_QUEUE_GET_CONFIG_REQUEST 'oh'. The caller may append information about
2467 * individual queues with ofputil_append_queue_get_config_reply(). */
2469 ofputil_encode_queue_get_config_reply(const struct ofp_header *oh)
2471 struct ofp10_queue_get_config_reply *qgcr10;
2472 struct ofp11_queue_get_config_reply *qgcr11;
2473 struct ofpbuf *reply;
2479 error = ofputil_decode_queue_get_config_request(oh, &port);
2482 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2483 raw = ofpraw_pull_assert(&b);
2485 switch ((int) raw) {
2486 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2487 reply = ofpraw_alloc_reply(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY,
2489 qgcr10 = ofpbuf_put_zeros(reply, sizeof *qgcr10);
2490 qgcr10->port = htons(ofp_to_u16(port));
2493 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2494 reply = ofpraw_alloc_reply(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY,
2496 qgcr11 = ofpbuf_put_zeros(reply, sizeof *qgcr11);
2497 qgcr11->port = ofputil_port_to_ofp11(port);
2508 put_queue_rate(struct ofpbuf *reply, enum ofp_queue_properties property,
2511 if (rate != UINT16_MAX) {
2512 struct ofp_queue_prop_rate *oqpr;
2514 oqpr = ofpbuf_put_zeros(reply, sizeof *oqpr);
2515 oqpr->prop_header.property = htons(property);
2516 oqpr->prop_header.len = htons(sizeof *oqpr);
2517 oqpr->rate = htons(rate);
2521 /* Appends a queue description for 'queue_id' to the
2522 * OFPT_QUEUE_GET_CONFIG_REPLY already in 'oh'. */
2524 ofputil_append_queue_get_config_reply(struct ofpbuf *reply,
2525 const struct ofputil_queue_config *oqc)
2527 const struct ofp_header *oh = reply->data;
2528 size_t start_ofs, len_ofs;
2531 start_ofs = reply->size;
2532 if (oh->version < OFP12_VERSION) {
2533 struct ofp10_packet_queue *opq10;
2535 opq10 = ofpbuf_put_zeros(reply, sizeof *opq10);
2536 opq10->queue_id = htonl(oqc->queue_id);
2537 len_ofs = (char *) &opq10->len - (char *) reply->data;
2539 struct ofp11_queue_get_config_reply *qgcr11;
2540 struct ofp12_packet_queue *opq12;
2543 qgcr11 = reply->msg;
2544 port = qgcr11->port;
2546 opq12 = ofpbuf_put_zeros(reply, sizeof *opq12);
2548 opq12->queue_id = htonl(oqc->queue_id);
2549 len_ofs = (char *) &opq12->len - (char *) reply->data;
2552 put_queue_rate(reply, OFPQT_MIN_RATE, oqc->min_rate);
2553 put_queue_rate(reply, OFPQT_MAX_RATE, oqc->max_rate);
2555 len = ofpbuf_at(reply, len_ofs, sizeof *len);
2556 *len = htons(reply->size - start_ofs);
2559 /* Decodes the initial part of an OFPT_QUEUE_GET_CONFIG_REPLY from 'reply' and
2560 * stores in '*port' the port that the reply is about. The caller may call
2561 * ofputil_pull_queue_get_config_reply() to obtain information about individual
2562 * queues included in the reply. Returns 0 if successful, otherwise an
2565 ofputil_decode_queue_get_config_reply(struct ofpbuf *reply, ofp_port_t *port)
2567 const struct ofp10_queue_get_config_reply *qgcr10;
2568 const struct ofp11_queue_get_config_reply *qgcr11;
2571 raw = ofpraw_pull_assert(reply);
2572 switch ((int) raw) {
2573 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY:
2574 qgcr10 = ofpbuf_pull(reply, sizeof *qgcr10);
2575 *port = u16_to_ofp(ntohs(qgcr10->port));
2578 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY:
2579 qgcr11 = ofpbuf_pull(reply, sizeof *qgcr11);
2580 return ofputil_port_from_ofp11(qgcr11->port, port);
2587 parse_queue_rate(const struct ofp_queue_prop_header *hdr, uint16_t *rate)
2589 const struct ofp_queue_prop_rate *oqpr;
2591 if (hdr->len == htons(sizeof *oqpr)) {
2592 oqpr = (const struct ofp_queue_prop_rate *) hdr;
2593 *rate = ntohs(oqpr->rate);
2596 return OFPERR_OFPBRC_BAD_LEN;
2600 /* Decodes information about a queue from the OFPT_QUEUE_GET_CONFIG_REPLY in
2601 * 'reply' and stores it in '*queue'. ofputil_decode_queue_get_config_reply()
2602 * must already have pulled off the main header.
2604 * This function returns EOF if the last queue has already been decoded, 0 if a
2605 * queue was successfully decoded into '*queue', or an ofperr if there was a
2606 * problem decoding 'reply'. */
2608 ofputil_pull_queue_get_config_reply(struct ofpbuf *reply,
2609 struct ofputil_queue_config *queue)
2611 const struct ofp_header *oh;
2612 unsigned int opq_len;
2619 queue->min_rate = UINT16_MAX;
2620 queue->max_rate = UINT16_MAX;
2623 if (oh->version < OFP12_VERSION) {
2624 const struct ofp10_packet_queue *opq10;
2626 opq10 = ofpbuf_try_pull(reply, sizeof *opq10);
2628 return OFPERR_OFPBRC_BAD_LEN;
2630 queue->queue_id = ntohl(opq10->queue_id);
2631 len = ntohs(opq10->len);
2632 opq_len = sizeof *opq10;
2634 const struct ofp12_packet_queue *opq12;
2636 opq12 = ofpbuf_try_pull(reply, sizeof *opq12);
2638 return OFPERR_OFPBRC_BAD_LEN;
2640 queue->queue_id = ntohl(opq12->queue_id);
2641 len = ntohs(opq12->len);
2642 opq_len = sizeof *opq12;
2645 if (len < opq_len || len > reply->size + opq_len || len % 8) {
2646 return OFPERR_OFPBRC_BAD_LEN;
2651 const struct ofp_queue_prop_header *hdr;
2652 unsigned int property;
2653 unsigned int prop_len;
2654 enum ofperr error = 0;
2656 hdr = ofpbuf_at_assert(reply, 0, sizeof *hdr);
2657 prop_len = ntohs(hdr->len);
2658 if (prop_len < sizeof *hdr || prop_len > reply->size || prop_len % 8) {
2659 return OFPERR_OFPBRC_BAD_LEN;
2662 property = ntohs(hdr->property);
2664 case OFPQT_MIN_RATE:
2665 error = parse_queue_rate(hdr, &queue->min_rate);
2668 case OFPQT_MAX_RATE:
2669 error = parse_queue_rate(hdr, &queue->max_rate);
2673 VLOG_INFO_RL(&bad_ofmsg_rl, "unknown queue property %u", property);
2680 ofpbuf_pull(reply, prop_len);
2686 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
2687 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
2688 * successful, otherwise an OpenFlow error code. */
2690 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
2691 const struct ofp_header *oh)
2696 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2697 raw = ofpraw_pull_assert(&b);
2698 switch ((int) raw) {
2699 case OFPRAW_OFPST10_FLOW_REQUEST:
2700 return ofputil_decode_ofpst10_flow_request(fsr, b.data, false);
2702 case OFPRAW_OFPST10_AGGREGATE_REQUEST:
2703 return ofputil_decode_ofpst10_flow_request(fsr, b.data, true);
2705 case OFPRAW_OFPST11_FLOW_REQUEST:
2706 return ofputil_decode_ofpst11_flow_request(fsr, &b, false);
2708 case OFPRAW_OFPST11_AGGREGATE_REQUEST:
2709 return ofputil_decode_ofpst11_flow_request(fsr, &b, true);
2711 case OFPRAW_NXST_FLOW_REQUEST:
2712 return ofputil_decode_nxst_flow_request(fsr, &b, false);
2714 case OFPRAW_NXST_AGGREGATE_REQUEST:
2715 return ofputil_decode_nxst_flow_request(fsr, &b, true);
2718 /* Hey, the caller lied. */
2723 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
2724 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
2725 * 'protocol', and returns the message. */
2727 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
2728 enum ofputil_protocol protocol)
2734 case OFPUTIL_P_OF11_STD:
2735 case OFPUTIL_P_OF12_OXM:
2736 case OFPUTIL_P_OF13_OXM:
2737 case OFPUTIL_P_OF14_OXM:
2738 case OFPUTIL_P_OF15_OXM: {
2739 struct ofp11_flow_stats_request *ofsr;
2741 raw = (fsr->aggregate
2742 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
2743 : OFPRAW_OFPST11_FLOW_REQUEST);
2744 msg = ofpraw_alloc(raw, ofputil_protocol_to_ofp_version(protocol),
2745 ofputil_match_typical_len(protocol));
2746 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2747 ofsr->table_id = fsr->table_id;
2748 ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port);
2749 ofsr->out_group = htonl(fsr->out_group);
2750 ofsr->cookie = fsr->cookie;
2751 ofsr->cookie_mask = fsr->cookie_mask;
2752 ofputil_put_ofp11_match(msg, &fsr->match, protocol);
2756 case OFPUTIL_P_OF10_STD:
2757 case OFPUTIL_P_OF10_STD_TID: {
2758 struct ofp10_flow_stats_request *ofsr;
2760 raw = (fsr->aggregate
2761 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
2762 : OFPRAW_OFPST10_FLOW_REQUEST);
2763 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
2764 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2765 ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match);
2766 ofsr->table_id = fsr->table_id;
2767 ofsr->out_port = htons(ofp_to_u16(fsr->out_port));
2771 case OFPUTIL_P_OF10_NXM:
2772 case OFPUTIL_P_OF10_NXM_TID: {
2773 struct nx_flow_stats_request *nfsr;
2776 raw = (fsr->aggregate
2777 ? OFPRAW_NXST_AGGREGATE_REQUEST
2778 : OFPRAW_NXST_FLOW_REQUEST);
2779 msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN);
2780 ofpbuf_put_zeros(msg, sizeof *nfsr);
2781 match_len = nx_put_match(msg, &fsr->match,
2782 fsr->cookie, fsr->cookie_mask);
2785 nfsr->out_port = htons(ofp_to_u16(fsr->out_port));
2786 nfsr->match_len = htons(match_len);
2787 nfsr->table_id = fsr->table_id;
2798 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
2799 * ofputil_flow_stats in 'fs'.
2801 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
2802 * OpenFlow message. Calling this function multiple times for a single 'msg'
2803 * iterates through the replies. The caller must initially leave 'msg''s layer
2804 * pointers null and not modify them between calls.
2806 * Most switches don't send the values needed to populate fs->idle_age and
2807 * fs->hard_age, so those members will usually be set to 0. If the switch from
2808 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
2809 * 'flow_age_extension' as true so that the contents of 'msg' determine the
2810 * 'idle_age' and 'hard_age' members in 'fs'.
2812 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
2813 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
2814 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
2816 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2817 * otherwise a positive errno value. */
2819 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
2821 bool flow_age_extension,
2822 struct ofpbuf *ofpacts)
2824 const struct ofp_header *oh;
2825 size_t instructions_len;
2829 error = (msg->header ? ofpraw_decode(&raw, msg->header)
2830 : ofpraw_pull(&raw, msg));
2838 } else if (raw == OFPRAW_OFPST11_FLOW_REPLY
2839 || raw == OFPRAW_OFPST13_FLOW_REPLY) {
2840 const struct ofp11_flow_stats *ofs;
2842 uint16_t padded_match_len;
2844 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2846 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
2847 "bytes at end", msg->size);
2851 length = ntohs(ofs->length);
2852 if (length < sizeof *ofs) {
2853 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2854 "length %"PRIuSIZE, length);
2858 if (ofputil_pull_ofp11_match(msg, &fs->match, &padded_match_len)) {
2859 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match");
2862 instructions_len = length - sizeof *ofs - padded_match_len;
2864 fs->priority = ntohs(ofs->priority);
2865 fs->table_id = ofs->table_id;
2866 fs->duration_sec = ntohl(ofs->duration_sec);
2867 fs->duration_nsec = ntohl(ofs->duration_nsec);
2868 fs->idle_timeout = ntohs(ofs->idle_timeout);
2869 fs->hard_timeout = ntohs(ofs->hard_timeout);
2870 if (oh->version >= OFP14_VERSION) {
2871 fs->importance = ntohs(ofs->importance);
2875 if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
2876 error = ofputil_decode_flow_mod_flags(ofs->flags, -1, oh->version,
2886 fs->cookie = ofs->cookie;
2887 fs->packet_count = ntohll(ofs->packet_count);
2888 fs->byte_count = ntohll(ofs->byte_count);
2889 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
2890 const struct ofp10_flow_stats *ofs;
2893 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2895 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
2896 "bytes at end", msg->size);
2900 length = ntohs(ofs->length);
2901 if (length < sizeof *ofs) {
2902 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2903 "length %"PRIuSIZE, length);
2906 instructions_len = length - sizeof *ofs;
2908 fs->cookie = get_32aligned_be64(&ofs->cookie);
2909 ofputil_match_from_ofp10_match(&ofs->match, &fs->match);
2910 fs->priority = ntohs(ofs->priority);
2911 fs->table_id = ofs->table_id;
2912 fs->duration_sec = ntohl(ofs->duration_sec);
2913 fs->duration_nsec = ntohl(ofs->duration_nsec);
2914 fs->idle_timeout = ntohs(ofs->idle_timeout);
2915 fs->hard_timeout = ntohs(ofs->hard_timeout);
2919 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
2920 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
2922 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
2923 const struct nx_flow_stats *nfs;
2924 size_t match_len, length;
2926 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
2928 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %"PRIu32" leftover "
2929 "bytes at end", msg->size);
2933 length = ntohs(nfs->length);
2934 match_len = ntohs(nfs->match_len);
2935 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
2936 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%"PRIuSIZE" "
2937 "claims invalid length %"PRIuSIZE, match_len, length);
2940 if (nx_pull_match(msg, match_len, &fs->match, NULL, NULL)) {
2943 instructions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
2945 fs->cookie = nfs->cookie;
2946 fs->table_id = nfs->table_id;
2947 fs->duration_sec = ntohl(nfs->duration_sec);
2948 fs->duration_nsec = ntohl(nfs->duration_nsec);
2949 fs->priority = ntohs(nfs->priority);
2950 fs->idle_timeout = ntohs(nfs->idle_timeout);
2951 fs->hard_timeout = ntohs(nfs->hard_timeout);
2955 if (flow_age_extension) {
2956 if (nfs->idle_age) {
2957 fs->idle_age = ntohs(nfs->idle_age) - 1;
2959 if (nfs->hard_age) {
2960 fs->hard_age = ntohs(nfs->hard_age) - 1;
2963 fs->packet_count = ntohll(nfs->packet_count);
2964 fs->byte_count = ntohll(nfs->byte_count);
2970 if (ofpacts_pull_openflow_instructions(msg, instructions_len, oh->version,
2972 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions");
2975 fs->ofpacts = ofpacts->data;
2976 fs->ofpacts_len = ofpacts->size;
2981 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
2983 * We use this in situations where OVS internally uses UINT64_MAX to mean
2984 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
2986 unknown_to_zero(uint64_t count)
2988 return count != UINT64_MAX ? count : 0;
2991 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
2992 * those already present in the list of ofpbufs in 'replies'. 'replies' should
2993 * have been initialized with ofpmp_init(). */
2995 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
2996 struct ovs_list *replies)
2998 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
2999 size_t start_ofs = reply->size;
3000 enum ofp_version version = ofpmp_version(replies);
3001 enum ofpraw raw = ofpmp_decode_raw(replies);
3003 if (raw == OFPRAW_OFPST11_FLOW_REPLY || raw == OFPRAW_OFPST13_FLOW_REPLY) {
3004 struct ofp11_flow_stats *ofs;
3006 ofpbuf_put_uninit(reply, sizeof *ofs);
3007 oxm_put_match(reply, &fs->match, version);
3008 ofpacts_put_openflow_instructions(fs->ofpacts, fs->ofpacts_len, reply,
3011 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
3012 ofs->length = htons(reply->size - start_ofs);
3013 ofs->table_id = fs->table_id;
3015 ofs->duration_sec = htonl(fs->duration_sec);
3016 ofs->duration_nsec = htonl(fs->duration_nsec);
3017 ofs->priority = htons(fs->priority);
3018 ofs->idle_timeout = htons(fs->idle_timeout);
3019 ofs->hard_timeout = htons(fs->hard_timeout);
3020 if (version >= OFP14_VERSION) {
3021 ofs->importance = htons(fs->importance);
3023 ofs->importance = 0;
3025 if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
3026 ofs->flags = ofputil_encode_flow_mod_flags(fs->flags, version);
3030 memset(ofs->pad2, 0, sizeof ofs->pad2);
3031 ofs->cookie = fs->cookie;
3032 ofs->packet_count = htonll(unknown_to_zero(fs->packet_count));
3033 ofs->byte_count = htonll(unknown_to_zero(fs->byte_count));
3034 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
3035 struct ofp10_flow_stats *ofs;
3037 ofpbuf_put_uninit(reply, sizeof *ofs);
3038 ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
3040 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
3041 ofs->length = htons(reply->size - start_ofs);
3042 ofs->table_id = fs->table_id;
3044 ofputil_match_to_ofp10_match(&fs->match, &ofs->match);
3045 ofs->duration_sec = htonl(fs->duration_sec);
3046 ofs->duration_nsec = htonl(fs->duration_nsec);
3047 ofs->priority = htons(fs->priority);
3048 ofs->idle_timeout = htons(fs->idle_timeout);
3049 ofs->hard_timeout = htons(fs->hard_timeout);
3050 memset(ofs->pad2, 0, sizeof ofs->pad2);
3051 put_32aligned_be64(&ofs->cookie, fs->cookie);
3052 put_32aligned_be64(&ofs->packet_count,
3053 htonll(unknown_to_zero(fs->packet_count)));
3054 put_32aligned_be64(&ofs->byte_count,
3055 htonll(unknown_to_zero(fs->byte_count)));
3056 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
3057 struct nx_flow_stats *nfs;
3060 ofpbuf_put_uninit(reply, sizeof *nfs);
3061 match_len = nx_put_match(reply, &fs->match, 0, 0);
3062 ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
3064 nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
3065 nfs->length = htons(reply->size - start_ofs);
3066 nfs->table_id = fs->table_id;
3068 nfs->duration_sec = htonl(fs->duration_sec);
3069 nfs->duration_nsec = htonl(fs->duration_nsec);
3070 nfs->priority = htons(fs->priority);
3071 nfs->idle_timeout = htons(fs->idle_timeout);
3072 nfs->hard_timeout = htons(fs->hard_timeout);
3073 nfs->idle_age = htons(fs->idle_age < 0 ? 0
3074 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
3076 nfs->hard_age = htons(fs->hard_age < 0 ? 0
3077 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
3079 nfs->match_len = htons(match_len);
3080 nfs->cookie = fs->cookie;
3081 nfs->packet_count = htonll(fs->packet_count);
3082 nfs->byte_count = htonll(fs->byte_count);
3087 ofpmp_postappend(replies, start_ofs);
3090 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
3091 * NXST_AGGREGATE reply matching 'request', and returns the message. */
3093 ofputil_encode_aggregate_stats_reply(
3094 const struct ofputil_aggregate_stats *stats,
3095 const struct ofp_header *request)
3097 struct ofp_aggregate_stats_reply *asr;
3098 uint64_t packet_count;
3099 uint64_t byte_count;
3103 ofpraw_decode(&raw, request);
3104 if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) {
3105 packet_count = unknown_to_zero(stats->packet_count);
3106 byte_count = unknown_to_zero(stats->byte_count);
3108 packet_count = stats->packet_count;
3109 byte_count = stats->byte_count;
3112 msg = ofpraw_alloc_stats_reply(request, 0);
3113 asr = ofpbuf_put_zeros(msg, sizeof *asr);
3114 put_32aligned_be64(&asr->packet_count, htonll(packet_count));
3115 put_32aligned_be64(&asr->byte_count, htonll(byte_count));
3116 asr->flow_count = htonl(stats->flow_count);
3122 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
3123 const struct ofp_header *reply)
3125 struct ofp_aggregate_stats_reply *asr;
3128 ofpbuf_use_const(&msg, reply, ntohs(reply->length));
3129 ofpraw_pull_assert(&msg);
3132 stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
3133 stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
3134 stats->flow_count = ntohl(asr->flow_count);
3139 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
3140 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
3141 * an OpenFlow error code. */
3143 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
3144 const struct ofp_header *oh)
3149 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3150 raw = ofpraw_pull_assert(&b);
3151 if (raw == OFPRAW_OFPT11_FLOW_REMOVED) {
3152 const struct ofp12_flow_removed *ofr;
3155 ofr = ofpbuf_pull(&b, sizeof *ofr);
3157 error = ofputil_pull_ofp11_match(&b, &fr->match, NULL);
3162 fr->priority = ntohs(ofr->priority);
3163 fr->cookie = ofr->cookie;
3164 fr->reason = ofr->reason;
3165 fr->table_id = ofr->table_id;
3166 fr->duration_sec = ntohl(ofr->duration_sec);
3167 fr->duration_nsec = ntohl(ofr->duration_nsec);
3168 fr->idle_timeout = ntohs(ofr->idle_timeout);
3169 fr->hard_timeout = ntohs(ofr->hard_timeout);
3170 fr->packet_count = ntohll(ofr->packet_count);
3171 fr->byte_count = ntohll(ofr->byte_count);
3172 } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
3173 const struct ofp10_flow_removed *ofr;
3175 ofr = ofpbuf_pull(&b, sizeof *ofr);
3177 ofputil_match_from_ofp10_match(&ofr->match, &fr->match);
3178 fr->priority = ntohs(ofr->priority);
3179 fr->cookie = ofr->cookie;
3180 fr->reason = ofr->reason;
3182 fr->duration_sec = ntohl(ofr->duration_sec);
3183 fr->duration_nsec = ntohl(ofr->duration_nsec);
3184 fr->idle_timeout = ntohs(ofr->idle_timeout);
3185 fr->hard_timeout = 0;
3186 fr->packet_count = ntohll(ofr->packet_count);
3187 fr->byte_count = ntohll(ofr->byte_count);
3188 } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
3189 struct nx_flow_removed *nfr;
3192 nfr = ofpbuf_pull(&b, sizeof *nfr);
3193 error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match,
3199 return OFPERR_OFPBRC_BAD_LEN;
3202 fr->priority = ntohs(nfr->priority);
3203 fr->cookie = nfr->cookie;
3204 fr->reason = nfr->reason;
3205 fr->table_id = nfr->table_id ? nfr->table_id - 1 : 255;
3206 fr->duration_sec = ntohl(nfr->duration_sec);
3207 fr->duration_nsec = ntohl(nfr->duration_nsec);
3208 fr->idle_timeout = ntohs(nfr->idle_timeout);
3209 fr->hard_timeout = 0;
3210 fr->packet_count = ntohll(nfr->packet_count);
3211 fr->byte_count = ntohll(nfr->byte_count);
3219 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
3220 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
3223 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
3224 enum ofputil_protocol protocol)
3227 enum ofp_flow_removed_reason reason = fr->reason;
3229 if (reason == OFPRR_METER_DELETE && !(protocol & OFPUTIL_P_OF14_UP)) {
3230 reason = OFPRR_DELETE;
3234 case OFPUTIL_P_OF11_STD:
3235 case OFPUTIL_P_OF12_OXM:
3236 case OFPUTIL_P_OF13_OXM:
3237 case OFPUTIL_P_OF14_OXM:
3238 case OFPUTIL_P_OF15_OXM: {
3239 struct ofp12_flow_removed *ofr;
3241 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED,
3242 ofputil_protocol_to_ofp_version(protocol),
3244 ofputil_match_typical_len(protocol));
3245 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3246 ofr->cookie = fr->cookie;
3247 ofr->priority = htons(fr->priority);
3248 ofr->reason = reason;
3249 ofr->table_id = fr->table_id;
3250 ofr->duration_sec = htonl(fr->duration_sec);
3251 ofr->duration_nsec = htonl(fr->duration_nsec);
3252 ofr->idle_timeout = htons(fr->idle_timeout);
3253 ofr->hard_timeout = htons(fr->hard_timeout);
3254 ofr->packet_count = htonll(fr->packet_count);
3255 ofr->byte_count = htonll(fr->byte_count);
3256 ofputil_put_ofp11_match(msg, &fr->match, protocol);
3260 case OFPUTIL_P_OF10_STD:
3261 case OFPUTIL_P_OF10_STD_TID: {
3262 struct ofp10_flow_removed *ofr;
3264 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
3266 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3267 ofputil_match_to_ofp10_match(&fr->match, &ofr->match);
3268 ofr->cookie = fr->cookie;
3269 ofr->priority = htons(fr->priority);
3270 ofr->reason = reason;
3271 ofr->duration_sec = htonl(fr->duration_sec);
3272 ofr->duration_nsec = htonl(fr->duration_nsec);
3273 ofr->idle_timeout = htons(fr->idle_timeout);
3274 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
3275 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
3279 case OFPUTIL_P_OF10_NXM:
3280 case OFPUTIL_P_OF10_NXM_TID: {
3281 struct nx_flow_removed *nfr;
3284 msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
3285 htonl(0), NXM_TYPICAL_LEN);
3286 ofpbuf_put_zeros(msg, sizeof *nfr);
3287 match_len = nx_put_match(msg, &fr->match, 0, 0);
3290 nfr->cookie = fr->cookie;
3291 nfr->priority = htons(fr->priority);
3292 nfr->reason = reason;
3293 nfr->table_id = fr->table_id + 1;
3294 nfr->duration_sec = htonl(fr->duration_sec);
3295 nfr->duration_nsec = htonl(fr->duration_nsec);
3296 nfr->idle_timeout = htons(fr->idle_timeout);
3297 nfr->match_len = htons(match_len);
3298 nfr->packet_count = htonll(fr->packet_count);
3299 nfr->byte_count = htonll(fr->byte_count);
3311 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
3312 const struct ofp_header *oh)
3317 memset(pin, 0, sizeof *pin);
3318 pin->cookie = OVS_BE64_MAX;
3320 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3321 raw = ofpraw_pull_assert(&b);
3322 if (raw == OFPRAW_OFPT13_PACKET_IN || raw == OFPRAW_OFPT12_PACKET_IN) {
3323 const struct ofp13_packet_in *opi;
3325 size_t packet_in_size;
3327 if (raw == OFPRAW_OFPT12_PACKET_IN) {
3328 packet_in_size = sizeof (struct ofp12_packet_in);
3330 packet_in_size = sizeof (struct ofp13_packet_in);
3333 opi = ofpbuf_pull(&b, packet_in_size);
3334 error = oxm_pull_match_loose(&b, &pin->flow_metadata);
3339 if (!ofpbuf_try_pull(&b, 2)) {
3340 return OFPERR_OFPBRC_BAD_LEN;
3343 pin->reason = opi->pi.reason;
3344 pin->table_id = opi->pi.table_id;
3345 pin->buffer_id = ntohl(opi->pi.buffer_id);
3346 pin->total_len = ntohs(opi->pi.total_len);
3348 if (raw == OFPRAW_OFPT13_PACKET_IN) {
3349 pin->cookie = opi->cookie;
3352 pin->packet = b.data;
3353 pin->packet_len = b.size;
3354 } else if (raw == OFPRAW_OFPT10_PACKET_IN) {
3355 const struct ofp10_packet_in *opi;
3357 opi = ofpbuf_pull(&b, offsetof(struct ofp10_packet_in, data));
3359 pin->packet = opi->data;
3360 pin->packet_len = b.size;
3362 match_init_catchall(&pin->flow_metadata);
3363 match_set_in_port(&pin->flow_metadata, u16_to_ofp(ntohs(opi->in_port)));
3364 pin->reason = opi->reason;
3365 pin->buffer_id = ntohl(opi->buffer_id);
3366 pin->total_len = ntohs(opi->total_len);
3367 } else if (raw == OFPRAW_OFPT11_PACKET_IN) {
3368 const struct ofp11_packet_in *opi;
3372 opi = ofpbuf_pull(&b, sizeof *opi);
3374 pin->packet = b.data;
3375 pin->packet_len = b.size;
3377 pin->buffer_id = ntohl(opi->buffer_id);
3378 error = ofputil_port_from_ofp11(opi->in_port, &in_port);
3382 match_init_catchall(&pin->flow_metadata);
3383 match_set_in_port(&pin->flow_metadata, in_port);
3384 pin->total_len = ntohs(opi->total_len);
3385 pin->reason = opi->reason;
3386 pin->table_id = opi->table_id;
3387 } else if (raw == OFPRAW_NXT_PACKET_IN) {
3388 const struct nx_packet_in *npi;
3391 npi = ofpbuf_pull(&b, sizeof *npi);
3392 error = nx_pull_match_loose(&b, ntohs(npi->match_len),
3393 &pin->flow_metadata, NULL, NULL);
3398 if (!ofpbuf_try_pull(&b, 2)) {
3399 return OFPERR_OFPBRC_BAD_LEN;
3402 pin->reason = npi->reason;
3403 pin->table_id = npi->table_id;
3404 pin->cookie = npi->cookie;
3406 pin->buffer_id = ntohl(npi->buffer_id);
3407 pin->total_len = ntohs(npi->total_len);
3409 pin->packet = b.data;
3410 pin->packet_len = b.size;
3418 static struct ofpbuf *
3419 ofputil_encode_ofp10_packet_in(const struct ofputil_packet_in *pin)
3421 struct ofp10_packet_in *opi;
3422 struct ofpbuf *packet;
3424 packet = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
3425 htonl(0), pin->packet_len);
3426 opi = ofpbuf_put_zeros(packet, offsetof(struct ofp10_packet_in, data));
3427 opi->total_len = htons(pin->total_len);
3428 opi->in_port = htons(ofp_to_u16(pin->flow_metadata.flow.in_port.ofp_port));
3429 opi->reason = pin->reason;
3430 opi->buffer_id = htonl(pin->buffer_id);
3432 ofpbuf_put(packet, pin->packet, pin->packet_len);
3437 static struct ofpbuf *
3438 ofputil_encode_nx_packet_in(const struct ofputil_packet_in *pin)
3440 struct nx_packet_in *npi;
3441 struct ofpbuf *packet;
3444 /* The final argument is just an estimate of the space required. */
3445 packet = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, OFP10_VERSION,
3446 htonl(0), NXM_TYPICAL_LEN + 2 + pin->packet_len);
3447 ofpbuf_put_zeros(packet, sizeof *npi);
3448 match_len = nx_put_match(packet, &pin->flow_metadata, 0, 0);
3449 ofpbuf_put_zeros(packet, 2);
3450 ofpbuf_put(packet, pin->packet, pin->packet_len);
3453 npi->buffer_id = htonl(pin->buffer_id);
3454 npi->total_len = htons(pin->total_len);
3455 npi->reason = pin->reason;
3456 npi->table_id = pin->table_id;
3457 npi->cookie = pin->cookie;
3458 npi->match_len = htons(match_len);
3463 static struct ofpbuf *
3464 ofputil_encode_ofp11_packet_in(const struct ofputil_packet_in *pin)
3466 struct ofp11_packet_in *opi;
3467 struct ofpbuf *packet;
3469 packet = ofpraw_alloc_xid(OFPRAW_OFPT11_PACKET_IN, OFP11_VERSION,
3470 htonl(0), pin->packet_len);
3471 opi = ofpbuf_put_zeros(packet, sizeof *opi);
3472 opi->buffer_id = htonl(pin->buffer_id);
3473 opi->in_port = ofputil_port_to_ofp11(pin->flow_metadata.flow.in_port.ofp_port);
3474 opi->in_phy_port = opi->in_port;
3475 opi->total_len = htons(pin->total_len);
3476 opi->reason = pin->reason;
3477 opi->table_id = pin->table_id;
3479 ofpbuf_put(packet, pin->packet, pin->packet_len);
3484 static struct ofpbuf *
3485 ofputil_encode_ofp12_packet_in(const struct ofputil_packet_in *pin,
3486 enum ofputil_protocol protocol)
3488 struct ofp13_packet_in *opi;
3489 enum ofpraw packet_in_raw;
3490 enum ofp_version packet_in_version;
3491 size_t packet_in_size;
3492 struct ofpbuf *packet;
3494 if (protocol == OFPUTIL_P_OF12_OXM) {
3495 packet_in_raw = OFPRAW_OFPT12_PACKET_IN;
3496 packet_in_version = OFP12_VERSION;
3497 packet_in_size = sizeof (struct ofp12_packet_in);
3499 packet_in_raw = OFPRAW_OFPT13_PACKET_IN;
3500 packet_in_version = ofputil_protocol_to_ofp_version(protocol);
3501 packet_in_size = sizeof (struct ofp13_packet_in);
3504 /* The final argument is just an estimate of the space required. */
3505 packet = ofpraw_alloc_xid(packet_in_raw, packet_in_version,
3506 htonl(0), NXM_TYPICAL_LEN + 2 + pin->packet_len);
3507 ofpbuf_put_zeros(packet, packet_in_size);
3508 oxm_put_match(packet, &pin->flow_metadata,
3509 ofputil_protocol_to_ofp_version(protocol));
3510 ofpbuf_put_zeros(packet, 2);
3511 ofpbuf_put(packet, pin->packet, pin->packet_len);
3514 opi->pi.buffer_id = htonl(pin->buffer_id);
3515 opi->pi.total_len = htons(pin->total_len);
3516 opi->pi.reason = pin->reason;
3517 opi->pi.table_id = pin->table_id;
3518 if (protocol != OFPUTIL_P_OF12_OXM) {
3519 opi->cookie = pin->cookie;
3525 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
3526 * in the format specified by 'packet_in_format'. */
3528 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
3529 enum ofputil_protocol protocol,
3530 enum nx_packet_in_format packet_in_format)
3532 struct ofpbuf *packet;
3535 case OFPUTIL_P_OF10_STD:
3536 case OFPUTIL_P_OF10_STD_TID:
3537 case OFPUTIL_P_OF10_NXM:
3538 case OFPUTIL_P_OF10_NXM_TID:
3539 packet = (packet_in_format == NXPIF_NXM
3540 ? ofputil_encode_nx_packet_in(pin)
3541 : ofputil_encode_ofp10_packet_in(pin));
3544 case OFPUTIL_P_OF11_STD:
3545 packet = ofputil_encode_ofp11_packet_in(pin);
3548 case OFPUTIL_P_OF12_OXM:
3549 case OFPUTIL_P_OF13_OXM:
3550 case OFPUTIL_P_OF14_OXM:
3551 case OFPUTIL_P_OF15_OXM:
3552 packet = ofputil_encode_ofp12_packet_in(pin, protocol);
3559 ofpmsg_update_length(packet);
3563 /* Returns a string form of 'reason'. The return value is either a statically
3564 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
3565 * 'bufsize' should be at least OFPUTIL_PACKET_IN_REASON_BUFSIZE. */
3567 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason,
3568 char *reasonbuf, size_t bufsize)
3575 case OFPR_INVALID_TTL:
3576 return "invalid_ttl";
3577 case OFPR_ACTION_SET:
3578 return "action_set";
3581 case OFPR_PACKET_OUT:
3582 return "packet_out";
3584 case OFPR_N_REASONS:
3586 snprintf(reasonbuf, bufsize, "%d", (int) reason);
3592 ofputil_packet_in_reason_from_string(const char *s,
3593 enum ofp_packet_in_reason *reason)
3597 for (i = 0; i < OFPR_N_REASONS; i++) {
3598 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
3599 const char *reason_s;
3601 reason_s = ofputil_packet_in_reason_to_string(i, reasonbuf,
3603 if (!strcasecmp(s, reason_s)) {
3611 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
3614 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
3615 * message's actions. The caller must initialize 'ofpacts' and retains
3616 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
3618 * Returns 0 if successful, otherwise an OFPERR_* value. */
3620 ofputil_decode_packet_out(struct ofputil_packet_out *po,
3621 const struct ofp_header *oh,
3622 struct ofpbuf *ofpacts)
3627 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3628 raw = ofpraw_pull_assert(&b);
3630 if (raw == OFPRAW_OFPT11_PACKET_OUT) {
3632 const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
3634 po->buffer_id = ntohl(opo->buffer_id);
3635 error = ofputil_port_from_ofp11(opo->in_port, &po->in_port);
3640 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
3641 oh->version, ofpacts);
3645 } else if (raw == OFPRAW_OFPT10_PACKET_OUT) {
3647 const struct ofp10_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
3649 po->buffer_id = ntohl(opo->buffer_id);
3650 po->in_port = u16_to_ofp(ntohs(opo->in_port));
3652 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
3653 oh->version, ofpacts);
3661 if (ofp_to_u16(po->in_port) >= ofp_to_u16(OFPP_MAX)
3662 && po->in_port != OFPP_LOCAL
3663 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
3664 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
3666 return OFPERR_OFPBRC_BAD_PORT;
3669 po->ofpacts = ofpacts->data;
3670 po->ofpacts_len = ofpacts->size;
3672 if (po->buffer_id == UINT32_MAX) {
3673 po->packet = b.data;
3674 po->packet_len = b.size;
3683 /* ofputil_phy_port */
3685 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
3686 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
3687 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
3688 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
3689 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
3690 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
3691 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
3692 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
3694 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
3695 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
3696 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
3697 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
3698 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
3699 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
3701 static enum netdev_features
3702 netdev_port_features_from_ofp10(ovs_be32 ofp10_)
3704 uint32_t ofp10 = ntohl(ofp10_);
3705 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
3709 netdev_port_features_to_ofp10(enum netdev_features features)
3711 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
3714 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
3715 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
3716 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
3717 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
3718 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
3719 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
3720 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
3721 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
3722 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
3723 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
3724 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
3725 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
3726 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
3727 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
3728 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
3729 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
3731 static enum netdev_features
3732 netdev_port_features_from_ofp11(ovs_be32 ofp11)
3734 return ntohl(ofp11) & 0xffff;
3738 netdev_port_features_to_ofp11(enum netdev_features features)
3740 return htonl(features & 0xffff);
3744 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
3745 const struct ofp10_phy_port *opp)
3747 pp->port_no = u16_to_ofp(ntohs(opp->port_no));
3748 memcpy(pp->hw_addr, opp->hw_addr, OFP_ETH_ALEN);
3749 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
3751 pp->config = ntohl(opp->config) & OFPPC10_ALL;
3752 pp->state = ntohl(opp->state) & OFPPS10_ALL;
3754 pp->curr = netdev_port_features_from_ofp10(opp->curr);
3755 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
3756 pp->supported = netdev_port_features_from_ofp10(opp->supported);
3757 pp->peer = netdev_port_features_from_ofp10(opp->peer);
3759 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
3760 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
3766 ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
3767 const struct ofp11_port *op)
3771 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
3775 memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN);
3776 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
3778 pp->config = ntohl(op->config) & OFPPC11_ALL;
3779 pp->state = ntohl(op->state) & OFPPS11_ALL;
3781 pp->curr = netdev_port_features_from_ofp11(op->curr);
3782 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
3783 pp->supported = netdev_port_features_from_ofp11(op->supported);
3784 pp->peer = netdev_port_features_from_ofp11(op->peer);
3786 pp->curr_speed = ntohl(op->curr_speed);
3787 pp->max_speed = ntohl(op->max_speed);
3793 parse_ofp14_port_ethernet_property(const struct ofpbuf *payload,
3794 struct ofputil_phy_port *pp)
3796 struct ofp14_port_desc_prop_ethernet *eth = payload->data;
3798 if (payload->size != sizeof *eth) {
3799 return OFPERR_OFPBPC_BAD_LEN;
3802 pp->curr = netdev_port_features_from_ofp11(eth->curr);
3803 pp->advertised = netdev_port_features_from_ofp11(eth->advertised);
3804 pp->supported = netdev_port_features_from_ofp11(eth->supported);
3805 pp->peer = netdev_port_features_from_ofp11(eth->peer);
3807 pp->curr_speed = ntohl(eth->curr_speed);
3808 pp->max_speed = ntohl(eth->max_speed);
3814 ofputil_pull_ofp14_port(struct ofputil_phy_port *pp, struct ofpbuf *msg)
3816 struct ofpbuf properties;
3817 struct ofp14_port *op;
3821 op = ofpbuf_try_pull(msg, sizeof *op);
3823 return OFPERR_OFPBRC_BAD_LEN;
3826 len = ntohs(op->length);
3827 if (len < sizeof *op || len - sizeof *op > msg->size) {
3828 return OFPERR_OFPBRC_BAD_LEN;
3831 ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len);
3833 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
3837 memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN);
3838 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
3840 pp->config = ntohl(op->config) & OFPPC11_ALL;
3841 pp->state = ntohl(op->state) & OFPPS11_ALL;
3843 while (properties.size > 0) {
3844 struct ofpbuf payload;
3848 error = ofputil_pull_property(&properties, &payload, &type);
3854 case OFPPDPT14_ETHERNET:
3855 error = parse_ofp14_port_ethernet_property(&payload, pp);
3859 log_property(true, "unknown port property %"PRIu16, type);
3873 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
3874 struct ofp10_phy_port *opp)
3876 memset(opp, 0, sizeof *opp);
3878 opp->port_no = htons(ofp_to_u16(pp->port_no));
3879 memcpy(opp->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
3880 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
3882 opp->config = htonl(pp->config & OFPPC10_ALL);
3883 opp->state = htonl(pp->state & OFPPS10_ALL);
3885 opp->curr = netdev_port_features_to_ofp10(pp->curr);
3886 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
3887 opp->supported = netdev_port_features_to_ofp10(pp->supported);
3888 opp->peer = netdev_port_features_to_ofp10(pp->peer);
3892 ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
3893 struct ofp11_port *op)
3895 memset(op, 0, sizeof *op);
3897 op->port_no = ofputil_port_to_ofp11(pp->port_no);
3898 memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
3899 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
3901 op->config = htonl(pp->config & OFPPC11_ALL);
3902 op->state = htonl(pp->state & OFPPS11_ALL);
3904 op->curr = netdev_port_features_to_ofp11(pp->curr);
3905 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
3906 op->supported = netdev_port_features_to_ofp11(pp->supported);
3907 op->peer = netdev_port_features_to_ofp11(pp->peer);
3909 op->curr_speed = htonl(pp->curr_speed);
3910 op->max_speed = htonl(pp->max_speed);
3914 ofputil_put_ofp14_port(const struct ofputil_phy_port *pp,
3917 struct ofp14_port *op;
3918 struct ofp14_port_desc_prop_ethernet *eth;
3920 ofpbuf_prealloc_tailroom(b, sizeof *op + sizeof *eth);
3922 op = ofpbuf_put_zeros(b, sizeof *op);
3923 op->port_no = ofputil_port_to_ofp11(pp->port_no);
3924 op->length = htons(sizeof *op + sizeof *eth);
3925 memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
3926 ovs_strlcpy(op->name, pp->name, sizeof op->name);
3927 op->config = htonl(pp->config & OFPPC11_ALL);
3928 op->state = htonl(pp->state & OFPPS11_ALL);
3930 eth = ofpbuf_put_zeros(b, sizeof *eth);
3931 eth->type = htons(OFPPDPT14_ETHERNET);
3932 eth->length = htons(sizeof *eth);
3933 eth->curr = netdev_port_features_to_ofp11(pp->curr);
3934 eth->advertised = netdev_port_features_to_ofp11(pp->advertised);
3935 eth->supported = netdev_port_features_to_ofp11(pp->supported);
3936 eth->peer = netdev_port_features_to_ofp11(pp->peer);
3937 eth->curr_speed = htonl(pp->curr_speed);
3938 eth->max_speed = htonl(pp->max_speed);
3942 ofputil_put_phy_port(enum ofp_version ofp_version,
3943 const struct ofputil_phy_port *pp, struct ofpbuf *b)
3945 switch (ofp_version) {
3946 case OFP10_VERSION: {
3947 struct ofp10_phy_port *opp = ofpbuf_put_uninit(b, sizeof *opp);
3948 ofputil_encode_ofp10_phy_port(pp, opp);
3954 case OFP13_VERSION: {
3955 struct ofp11_port *op = ofpbuf_put_uninit(b, sizeof *op);
3956 ofputil_encode_ofp11_port(pp, op);
3962 ofputil_put_ofp14_port(pp, b);
3971 ofputil_decode_port_desc_stats_request(const struct ofp_header *request,
3977 ofpbuf_use_const(&b, request, ntohs(request->length));
3978 raw = ofpraw_pull_assert(&b);
3979 if (raw == OFPRAW_OFPST10_PORT_DESC_REQUEST) {
3982 } else if (raw == OFPRAW_OFPST15_PORT_DESC_REQUEST) {
3983 ovs_be32 *ofp11_port;
3985 ofp11_port = ofpbuf_pull(&b, sizeof *ofp11_port);
3986 return ofputil_port_from_ofp11(*ofp11_port, port);
3993 ofputil_encode_port_desc_stats_request(enum ofp_version ofp_version,
3996 struct ofpbuf *request;
3997 ovs_be32 ofp11_port;
3999 switch (ofp_version) {
4005 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_DESC_REQUEST,
4010 request = ofpraw_alloc(OFPRAW_OFPST15_PORT_DESC_REQUEST,
4012 ofp11_port = ofputil_port_to_ofp11(port);
4013 ofpbuf_put(request, &ofp11_port, sizeof ofp11_port);
4024 ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port *pp,
4025 struct ovs_list *replies)
4027 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
4028 size_t start_ofs = reply->size;
4030 ofputil_put_phy_port(ofpmp_version(replies), pp, reply);
4031 ofpmp_postappend(replies, start_ofs);
4034 /* ofputil_switch_features */
4036 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
4037 OFPC_IP_REASM | OFPC_QUEUE_STATS)
4038 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
4039 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
4040 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
4041 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
4042 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
4043 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
4046 ofputil_capabilities_mask(enum ofp_version ofp_version)
4048 /* Handle capabilities whose bit is unique for all OpenFlow versions */
4049 switch (ofp_version) {
4052 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
4057 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
4059 /* Caller needs to check osf->header.version itself */
4064 /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an
4065 * abstract representation in '*features'. Initializes '*b' to iterate over
4066 * the OpenFlow port structures following 'osf' with later calls to
4067 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an
4068 * OFPERR_* value. */
4070 ofputil_decode_switch_features(const struct ofp_header *oh,
4071 struct ofputil_switch_features *features,
4074 const struct ofp_switch_features *osf;
4077 ofpbuf_use_const(b, oh, ntohs(oh->length));
4078 raw = ofpraw_pull_assert(b);
4080 osf = ofpbuf_pull(b, sizeof *osf);
4081 features->datapath_id = ntohll(osf->datapath_id);
4082 features->n_buffers = ntohl(osf->n_buffers);
4083 features->n_tables = osf->n_tables;
4084 features->auxiliary_id = 0;
4086 features->capabilities = ntohl(osf->capabilities) &
4087 ofputil_capabilities_mask(oh->version);
4089 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
4090 if (osf->capabilities & htonl(OFPC10_STP)) {
4091 features->capabilities |= OFPUTIL_C_STP;
4093 features->ofpacts = ofpact_bitmap_from_openflow(osf->actions,
4095 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY
4096 || raw == OFPRAW_OFPT13_FEATURES_REPLY) {
4097 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
4098 features->capabilities |= OFPUTIL_C_GROUP_STATS;
4100 features->ofpacts = 0;
4101 if (raw == OFPRAW_OFPT13_FEATURES_REPLY) {
4102 features->auxiliary_id = osf->auxiliary_id;
4105 return OFPERR_OFPBRC_BAD_VERSION;
4111 /* In OpenFlow 1.0, 1.1, and 1.2, an OFPT_FEATURES_REPLY message lists all the
4112 * switch's ports, unless there are too many to fit. In OpenFlow 1.3 and
4113 * later, an OFPT_FEATURES_REPLY does not list ports at all.
4115 * Given a buffer 'b' that contains a Features Reply message, this message
4116 * checks if it contains a complete list of the switch's ports. Returns true,
4117 * if so. Returns false if the list is missing (OF1.3+) or incomplete
4118 * (OF1.0/1.1/1.2), and in the latter case removes all of the ports from the
4121 * When this function returns false, the caller should send an OFPST_PORT_DESC
4122 * stats request to get the ports. */
4124 ofputil_switch_features_has_ports(struct ofpbuf *b)
4126 struct ofp_header *oh = b->data;
4127 size_t phy_port_size;
4129 if (oh->version >= OFP13_VERSION) {
4130 /* OpenFlow 1.3+ never has ports in the feature reply. */
4134 phy_port_size = (oh->version == OFP10_VERSION
4135 ? sizeof(struct ofp10_phy_port)
4136 : sizeof(struct ofp11_port));
4137 if (ntohs(oh->length) + phy_port_size <= UINT16_MAX) {
4138 /* There's room for additional ports in the feature reply.
4139 * Assume that the list is complete. */
4143 /* The feature reply has no room for more ports. Probably the list is
4144 * truncated. Drop the ports and tell the caller to retrieve them with
4145 * OFPST_PORT_DESC. */
4146 b->size = sizeof *oh + sizeof(struct ofp_switch_features);
4147 ofpmsg_update_length(b);
4151 /* Returns a buffer owned by the caller that encodes 'features' in the format
4152 * required by 'protocol' with the given 'xid'. The caller should append port
4153 * information to the buffer with subsequent calls to
4154 * ofputil_put_switch_features_port(). */
4156 ofputil_encode_switch_features(const struct ofputil_switch_features *features,
4157 enum ofputil_protocol protocol, ovs_be32 xid)
4159 struct ofp_switch_features *osf;
4161 enum ofp_version version;
4164 version = ofputil_protocol_to_ofp_version(protocol);
4167 raw = OFPRAW_OFPT10_FEATURES_REPLY;
4171 raw = OFPRAW_OFPT11_FEATURES_REPLY;
4176 raw = OFPRAW_OFPT13_FEATURES_REPLY;
4181 b = ofpraw_alloc_xid(raw, version, xid, 0);
4182 osf = ofpbuf_put_zeros(b, sizeof *osf);
4183 osf->datapath_id = htonll(features->datapath_id);
4184 osf->n_buffers = htonl(features->n_buffers);
4185 osf->n_tables = features->n_tables;
4187 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
4188 osf->capabilities = htonl(features->capabilities &
4189 ofputil_capabilities_mask(version));
4192 if (features->capabilities & OFPUTIL_C_STP) {
4193 osf->capabilities |= htonl(OFPC10_STP);
4195 osf->actions = ofpact_bitmap_to_openflow(features->ofpacts,
4201 osf->auxiliary_id = features->auxiliary_id;
4205 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
4206 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
4216 /* Encodes 'pp' into the format required by the switch_features message already
4217 * in 'b', which should have been returned by ofputil_encode_switch_features(),
4218 * and appends the encoded version to 'b'. */
4220 ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
4223 const struct ofp_header *oh = b->data;
4225 if (oh->version < OFP13_VERSION) {
4226 /* Try adding a port description to the message, but drop it again if
4227 * the buffer overflows. (This possibility for overflow is why
4228 * OpenFlow 1.3+ moved port descriptions into a multipart message.) */
4229 size_t start_ofs = b->size;
4230 ofputil_put_phy_port(oh->version, pp, b);
4231 if (b->size > UINT16_MAX) {
4232 b->size = start_ofs;
4237 /* ofputil_port_status */
4239 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
4240 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
4242 ofputil_decode_port_status(const struct ofp_header *oh,
4243 struct ofputil_port_status *ps)
4245 const struct ofp_port_status *ops;
4249 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4250 ofpraw_pull_assert(&b);
4251 ops = ofpbuf_pull(&b, sizeof *ops);
4253 if (ops->reason != OFPPR_ADD &&
4254 ops->reason != OFPPR_DELETE &&
4255 ops->reason != OFPPR_MODIFY) {
4256 return OFPERR_NXBRC_BAD_REASON;
4258 ps->reason = ops->reason;
4260 retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
4261 ovs_assert(retval != EOF);
4265 /* Converts the abstract form of a "port status" message in '*ps' into an
4266 * OpenFlow message suitable for 'protocol', and returns that encoded form in
4267 * a buffer owned by the caller. */
4269 ofputil_encode_port_status(const struct ofputil_port_status *ps,
4270 enum ofputil_protocol protocol)
4272 struct ofp_port_status *ops;
4274 enum ofp_version version;
4277 version = ofputil_protocol_to_ofp_version(protocol);
4280 raw = OFPRAW_OFPT10_PORT_STATUS;
4286 raw = OFPRAW_OFPT11_PORT_STATUS;
4291 raw = OFPRAW_OFPT14_PORT_STATUS;
4298 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
4299 ops = ofpbuf_put_zeros(b, sizeof *ops);
4300 ops->reason = ps->reason;
4301 ofputil_put_phy_port(version, &ps->desc, b);
4302 ofpmsg_update_length(b);
4306 /* ofputil_port_mod */
4309 parse_port_mod_ethernet_property(struct ofpbuf *property,
4310 struct ofputil_port_mod *pm)
4312 struct ofp14_port_mod_prop_ethernet *eth = property->data;
4314 if (property->size != sizeof *eth) {
4315 return OFPERR_OFPBRC_BAD_LEN;
4318 pm->advertise = netdev_port_features_from_ofp11(eth->advertise);
4322 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
4323 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
4325 ofputil_decode_port_mod(const struct ofp_header *oh,
4326 struct ofputil_port_mod *pm, bool loose)
4331 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4332 raw = ofpraw_pull_assert(&b);
4334 if (raw == OFPRAW_OFPT10_PORT_MOD) {
4335 const struct ofp10_port_mod *opm = b.data;
4337 pm->port_no = u16_to_ofp(ntohs(opm->port_no));
4338 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
4339 pm->config = ntohl(opm->config) & OFPPC10_ALL;
4340 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
4341 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
4342 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
4343 const struct ofp11_port_mod *opm = b.data;
4346 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
4351 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
4352 pm->config = ntohl(opm->config) & OFPPC11_ALL;
4353 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
4354 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
4355 } else if (raw == OFPRAW_OFPT14_PORT_MOD) {
4356 const struct ofp14_port_mod *opm = ofpbuf_pull(&b, sizeof *opm);
4359 memset(pm, 0, sizeof *pm);
4361 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
4366 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
4367 pm->config = ntohl(opm->config) & OFPPC11_ALL;
4368 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
4370 while (b.size > 0) {
4371 struct ofpbuf property;
4375 error = ofputil_pull_property(&b, &property, &type);
4381 case OFPPMPT14_ETHERNET:
4382 error = parse_port_mod_ethernet_property(&property, pm);
4386 log_property(loose, "unknown port_mod property %"PRIu16, type);
4389 } else if (type == OFPPMPT14_EXPERIMENTER) {
4390 error = OFPERR_OFPBPC_BAD_EXPERIMENTER;
4392 error = OFPERR_OFPBRC_BAD_TYPE;
4402 return OFPERR_OFPBRC_BAD_TYPE;
4405 pm->config &= pm->mask;
4409 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
4410 * message suitable for 'protocol', and returns that encoded form in a buffer
4411 * owned by the caller. */
4413 ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
4414 enum ofputil_protocol protocol)
4416 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
4419 switch (ofp_version) {
4420 case OFP10_VERSION: {
4421 struct ofp10_port_mod *opm;
4423 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
4424 opm = ofpbuf_put_zeros(b, sizeof *opm);
4425 opm->port_no = htons(ofp_to_u16(pm->port_no));
4426 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
4427 opm->config = htonl(pm->config & OFPPC10_ALL);
4428 opm->mask = htonl(pm->mask & OFPPC10_ALL);
4429 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
4435 case OFP13_VERSION: {
4436 struct ofp11_port_mod *opm;
4438 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
4439 opm = ofpbuf_put_zeros(b, sizeof *opm);
4440 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
4441 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
4442 opm->config = htonl(pm->config & OFPPC11_ALL);
4443 opm->mask = htonl(pm->mask & OFPPC11_ALL);
4444 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
4448 case OFP15_VERSION: {
4449 struct ofp14_port_mod_prop_ethernet *eth;
4450 struct ofp14_port_mod *opm;
4452 b = ofpraw_alloc(OFPRAW_OFPT14_PORT_MOD, ofp_version, sizeof *eth);
4453 opm = ofpbuf_put_zeros(b, sizeof *opm);
4454 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
4455 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
4456 opm->config = htonl(pm->config & OFPPC11_ALL);
4457 opm->mask = htonl(pm->mask & OFPPC11_ALL);
4459 if (pm->advertise) {
4460 eth = ofpbuf_put_zeros(b, sizeof *eth);
4461 eth->type = htons(OFPPMPT14_ETHERNET);
4462 eth->length = htons(sizeof *eth);
4463 eth->advertise = netdev_port_features_to_ofp11(pm->advertise);
4474 /* Table features. */
4477 pull_table_feature_property(struct ofpbuf *msg, struct ofpbuf *payload,
4482 error = ofputil_pull_property(msg, payload, typep);
4483 if (payload && !error) {
4484 ofpbuf_pull(payload, sizeof(struct ofp_prop_header));
4490 parse_action_bitmap(struct ofpbuf *payload, enum ofp_version ofp_version,
4495 while (payload->size > 0) {
4499 error = ofputil_pull_property__(payload, NULL, 1, &type);
4503 if (type < CHAR_BIT * sizeof types) {
4504 types |= 1u << type;
4508 *ofpacts = ofpact_bitmap_from_openflow(htonl(types), ofp_version);
4513 parse_instruction_ids(struct ofpbuf *payload, bool loose, uint32_t *insts)
4516 while (payload->size > 0) {
4517 enum ovs_instruction_type inst;
4521 /* OF1.3 and OF1.4 aren't clear about padding in the instruction IDs.
4522 * It seems clear that they aren't padded to 8 bytes, though, because
4523 * both standards say that "non-experimenter instructions are 4 bytes"
4524 * and do not mention any padding before the first instruction ID.
4525 * (There wouldn't be any point in padding to 8 bytes if the IDs were
4526 * aligned on an odd 4-byte boundary.)
4528 * Anyway, we just assume they're all glommed together on byte
4530 error = ofputil_pull_property__(payload, NULL, 1, &ofpit);
4535 error = ovs_instruction_type_from_inst_type(&inst, ofpit);
4537 *insts |= 1u << inst;
4538 } else if (!loose) {
4546 parse_table_features_next_table(struct ofpbuf *payload,
4547 unsigned long int *next_tables)
4551 memset(next_tables, 0, bitmap_n_bytes(255));
4552 for (i = 0; i < payload->size; i++) {
4553 uint8_t id = ((const uint8_t *) payload->data)[i];
4555 return OFPERR_OFPBPC_BAD_VALUE;
4557 bitmap_set1(next_tables, id);
4563 parse_oxms(struct ofpbuf *payload, bool loose,
4564 struct mf_bitmap *exactp, struct mf_bitmap *maskedp)
4566 struct mf_bitmap exact = MF_BITMAP_INITIALIZER;
4567 struct mf_bitmap masked = MF_BITMAP_INITIALIZER;
4569 while (payload->size > 0) {
4570 const struct mf_field *field;
4574 error = nx_pull_header(payload, &field, &hasmask);
4576 bitmap_set1(hasmask ? masked.bm : exact.bm, field->id);
4577 } else if (error != OFPERR_OFPBMC_BAD_FIELD || !loose) {
4583 } else if (!bitmap_is_all_zeros(exact.bm, MFF_N_IDS)) {
4584 return OFPERR_OFPBMC_BAD_MASK;
4588 } else if (!bitmap_is_all_zeros(masked.bm, MFF_N_IDS)) {
4589 return OFPERR_OFPBMC_BAD_MASK;
4594 /* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract
4595 * ofputil_table_features in 'tf'.
4597 * If 'loose' is true, this function ignores properties and values that it does
4598 * not understand, as a controller would want to do when interpreting
4599 * capabilities provided by a switch. If 'loose' is false, this function
4600 * treats unknown properties and values as an error, as a switch would want to
4601 * do when interpreting a configuration request made by a controller.
4603 * A single OpenFlow message can specify features for multiple tables. Calling
4604 * this function multiple times for a single 'msg' iterates through the tables
4605 * in the message. The caller must initially leave 'msg''s layer pointers null
4606 * and not modify them between calls.
4608 * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise
4609 * a positive "enum ofperr" value. */
4611 ofputil_decode_table_features(struct ofpbuf *msg,
4612 struct ofputil_table_features *tf, bool loose)
4614 const struct ofp_header *oh;
4615 struct ofp13_table_features *otf;
4616 struct ofpbuf properties;
4619 memset(tf, 0, sizeof *tf);
4622 ofpraw_pull_assert(msg);
4630 if (msg->size < sizeof *otf) {
4631 return OFPERR_OFPBPC_BAD_LEN;
4635 len = ntohs(otf->length);
4636 if (len < sizeof *otf || len % 8 || len > msg->size) {
4637 return OFPERR_OFPBPC_BAD_LEN;
4639 ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len);
4640 ofpbuf_pull(&properties, sizeof *otf);
4642 tf->table_id = otf->table_id;
4643 if (tf->table_id == OFPTT_ALL) {
4644 return OFPERR_OFPTFFC_BAD_TABLE;
4647 ovs_strlcpy(tf->name, otf->name, OFP_MAX_TABLE_NAME_LEN);
4648 tf->metadata_match = otf->metadata_match;
4649 tf->metadata_write = otf->metadata_write;
4650 tf->miss_config = OFPUTIL_TABLE_MISS_DEFAULT;
4651 if (oh->version >= OFP14_VERSION) {
4652 uint32_t caps = ntohl(otf->capabilities);
4653 tf->supports_eviction = (caps & OFPTC14_EVICTION) != 0;
4654 tf->supports_vacancy_events = (caps & OFPTC14_VACANCY_EVENTS) != 0;
4656 tf->supports_eviction = -1;
4657 tf->supports_vacancy_events = -1;
4659 tf->max_entries = ntohl(otf->max_entries);
4661 while (properties.size > 0) {
4662 struct ofpbuf payload;
4666 error = pull_table_feature_property(&properties, &payload, &type);
4671 switch ((enum ofp13_table_feature_prop_type) type) {
4672 case OFPTFPT13_INSTRUCTIONS:
4673 error = parse_instruction_ids(&payload, loose,
4674 &tf->nonmiss.instructions);
4676 case OFPTFPT13_INSTRUCTIONS_MISS:
4677 error = parse_instruction_ids(&payload, loose,
4678 &tf->miss.instructions);
4681 case OFPTFPT13_NEXT_TABLES:
4682 error = parse_table_features_next_table(&payload,
4685 case OFPTFPT13_NEXT_TABLES_MISS:
4686 error = parse_table_features_next_table(&payload, tf->miss.next);
4689 case OFPTFPT13_WRITE_ACTIONS:
4690 error = parse_action_bitmap(&payload, oh->version,
4691 &tf->nonmiss.write.ofpacts);
4693 case OFPTFPT13_WRITE_ACTIONS_MISS:
4694 error = parse_action_bitmap(&payload, oh->version,
4695 &tf->miss.write.ofpacts);
4698 case OFPTFPT13_APPLY_ACTIONS:
4699 error = parse_action_bitmap(&payload, oh->version,
4700 &tf->nonmiss.apply.ofpacts);
4702 case OFPTFPT13_APPLY_ACTIONS_MISS:
4703 error = parse_action_bitmap(&payload, oh->version,
4704 &tf->miss.apply.ofpacts);
4707 case OFPTFPT13_MATCH:
4708 error = parse_oxms(&payload, loose, &tf->match, &tf->mask);
4710 case OFPTFPT13_WILDCARDS:
4711 error = parse_oxms(&payload, loose, &tf->wildcard, NULL);
4714 case OFPTFPT13_WRITE_SETFIELD:
4715 error = parse_oxms(&payload, loose,
4716 &tf->nonmiss.write.set_fields, NULL);
4718 case OFPTFPT13_WRITE_SETFIELD_MISS:
4719 error = parse_oxms(&payload, loose,
4720 &tf->miss.write.set_fields, NULL);
4722 case OFPTFPT13_APPLY_SETFIELD:
4723 error = parse_oxms(&payload, loose,
4724 &tf->nonmiss.apply.set_fields, NULL);
4726 case OFPTFPT13_APPLY_SETFIELD_MISS:
4727 error = parse_oxms(&payload, loose,
4728 &tf->miss.apply.set_fields, NULL);
4731 case OFPTFPT13_EXPERIMENTER:
4732 case OFPTFPT13_EXPERIMENTER_MISS:
4734 log_property(loose, "unknown table features property %"PRIu16,
4736 error = loose ? 0 : OFPERR_OFPBPC_BAD_TYPE;
4744 /* Fix inconsistencies:
4746 * - Turn on 'match' bits that are set in 'mask', because maskable
4747 * fields are matchable.
4749 * - Turn on 'wildcard' bits that are set in 'mask', because a field
4750 * that is arbitrarily maskable can be wildcarded entirely.
4752 * - Turn off 'wildcard' bits that are not in 'match', because a field
4753 * must be matchable for it to be meaningfully wildcarded. */
4754 bitmap_or(tf->match.bm, tf->mask.bm, MFF_N_IDS);
4755 bitmap_or(tf->wildcard.bm, tf->mask.bm, MFF_N_IDS);
4756 bitmap_and(tf->wildcard.bm, tf->match.bm, MFF_N_IDS);
4761 /* Encodes and returns a request to obtain the table features of a switch.
4762 * The message is encoded for OpenFlow version 'ofp_version'. */
4764 ofputil_encode_table_features_request(enum ofp_version ofp_version)
4766 struct ofpbuf *request = NULL;
4768 switch (ofp_version) {
4772 ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later "
4773 "(\'-O OpenFlow13\')");
4777 request = ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST,
4788 put_fields_property(struct ofpbuf *reply,
4789 const struct mf_bitmap *fields,
4790 const struct mf_bitmap *masks,
4791 enum ofp13_table_feature_prop_type property,
4792 enum ofp_version version)
4797 start_ofs = start_property(reply, property);
4798 BITMAP_FOR_EACH_1 (field, MFF_N_IDS, fields->bm) {
4799 nx_put_header(reply, field, version,
4800 masks && bitmap_is_set(masks->bm, field));
4802 end_property(reply, start_ofs);
4806 put_table_action_features(struct ofpbuf *reply,
4807 const struct ofputil_table_action_features *taf,
4808 enum ofp13_table_feature_prop_type actions_type,
4809 enum ofp13_table_feature_prop_type set_fields_type,
4810 int miss_offset, enum ofp_version version)
4814 start_ofs = start_property(reply, actions_type + miss_offset);
4815 put_bitmap_properties(reply,
4816 ntohl(ofpact_bitmap_to_openflow(taf->ofpacts,
4818 end_property(reply, start_ofs);
4820 put_fields_property(reply, &taf->set_fields, NULL,
4821 set_fields_type + miss_offset, version);
4825 put_table_instruction_features(
4826 struct ofpbuf *reply, const struct ofputil_table_instruction_features *tif,
4827 int miss_offset, enum ofp_version version)
4832 start_ofs = start_property(reply, OFPTFPT13_INSTRUCTIONS + miss_offset);
4833 put_bitmap_properties(reply,
4834 ntohl(ovsinst_bitmap_to_openflow(tif->instructions,
4836 end_property(reply, start_ofs);
4838 start_ofs = start_property(reply, OFPTFPT13_NEXT_TABLES + miss_offset);
4839 BITMAP_FOR_EACH_1 (table_id, 255, tif->next) {
4840 ofpbuf_put(reply, &table_id, 1);
4842 end_property(reply, start_ofs);
4844 put_table_action_features(reply, &tif->write,
4845 OFPTFPT13_WRITE_ACTIONS,
4846 OFPTFPT13_WRITE_SETFIELD, miss_offset, version);
4847 put_table_action_features(reply, &tif->apply,
4848 OFPTFPT13_APPLY_ACTIONS,
4849 OFPTFPT13_APPLY_SETFIELD, miss_offset, version);
4853 ofputil_append_table_features_reply(const struct ofputil_table_features *tf,
4854 struct ovs_list *replies)
4856 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
4857 enum ofp_version version = ofpmp_version(replies);
4858 size_t start_ofs = reply->size;
4859 struct ofp13_table_features *otf;
4861 otf = ofpbuf_put_zeros(reply, sizeof *otf);
4862 otf->table_id = tf->table_id;
4863 ovs_strlcpy(otf->name, tf->name, sizeof otf->name);
4864 otf->metadata_match = tf->metadata_match;
4865 otf->metadata_write = tf->metadata_write;
4866 if (version >= OFP14_VERSION) {
4867 if (tf->supports_eviction) {
4868 otf->capabilities |= htonl(OFPTC14_EVICTION);
4870 if (tf->supports_vacancy_events) {
4871 otf->capabilities |= htonl(OFPTC14_VACANCY_EVENTS);
4874 otf->max_entries = htonl(tf->max_entries);
4876 put_table_instruction_features(reply, &tf->nonmiss, 0, version);
4877 put_table_instruction_features(reply, &tf->miss, 1, version);
4879 put_fields_property(reply, &tf->match, &tf->mask,
4880 OFPTFPT13_MATCH, version);
4881 put_fields_property(reply, &tf->wildcard, NULL,
4882 OFPTFPT13_WILDCARDS, version);
4884 otf = ofpbuf_at_assert(reply, start_ofs, sizeof *otf);
4885 otf->length = htons(reply->size - start_ofs);
4886 ofpmp_postappend(replies, start_ofs);
4890 parse_table_desc_eviction_property(struct ofpbuf *property,
4891 struct ofputil_table_desc *td)
4893 struct ofp14_table_mod_prop_eviction *ote = property->data;
4895 if (property->size != sizeof *ote) {
4896 return OFPERR_OFPBPC_BAD_LEN;
4899 td->eviction_flags = ntohl(ote->flags);
4903 /* Decodes the next OpenFlow "table desc" message (of possibly several) from
4904 * 'msg' into an abstract form in '*td'. Returns 0 if successful, EOF if the
4905 * last "table desc" in 'msg' was already decoded, otherwise an OFPERR_*
4908 ofputil_decode_table_desc(struct ofpbuf *msg,
4909 struct ofputil_table_desc *td,
4910 enum ofp_version version)
4912 struct ofp14_table_desc *otd;
4913 struct ofpbuf properties;
4916 memset(td, 0, sizeof *td);
4919 ofpraw_pull_assert(msg);
4926 otd = ofpbuf_try_pull(msg, sizeof *otd);
4928 VLOG_WARN_RL(&bad_ofmsg_rl, "OFP14_TABLE_DESC reply has %"PRIu32" "
4929 "leftover bytes at end", msg->size);
4930 return OFPERR_OFPBRC_BAD_LEN;
4933 td->table_id = otd->table_id;
4934 length = ntohs(otd->length);
4935 if (length < sizeof *otd || length - sizeof *otd > msg->size) {
4936 VLOG_WARN_RL(&bad_ofmsg_rl, "OFP14_TABLE_DESC reply claims invalid "
4937 "length %"PRIuSIZE, length);
4938 return OFPERR_OFPBRC_BAD_LEN;
4940 length -= sizeof *otd;
4941 ofpbuf_use_const(&properties, ofpbuf_pull(msg, length), length);
4943 td->eviction = ofputil_decode_table_eviction(otd->config, version);
4944 td->eviction_flags = UINT32_MAX;
4946 while (properties.size > 0) {
4947 struct ofpbuf payload;
4951 error = ofputil_pull_property(&properties, &payload, &type);
4957 case OFPTMPT14_EVICTION:
4958 error = parse_table_desc_eviction_property(&payload, td);
4962 log_property(true, "unknown table_desc property %"PRIu16, type);
4975 /* Encodes and returns a request to obtain description of tables of a switch.
4976 * The message is encoded for OpenFlow version 'ofp_version'. */
4978 ofputil_encode_table_desc_request(enum ofp_version ofp_version)
4980 struct ofpbuf *request = NULL;
4982 if (ofp_version >= OFP14_VERSION) {
4983 request = ofpraw_alloc(OFPRAW_OFPST14_TABLE_DESC_REQUEST,
4986 ovs_fatal(0, "dump-table-desc needs OpenFlow 1.4 or later "
4987 "(\'-O OpenFlow14\')");
4993 /* Function to append Table desc information in a reply list. */
4995 ofputil_append_table_desc_reply(const struct ofputil_table_desc *td,
4996 struct ovs_list *replies,
4997 enum ofp_version version)
4999 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
5001 struct ofp14_table_desc *otd;
5003 start_otd = reply->size;
5004 ofpbuf_put_zeros(reply, sizeof *otd);
5005 if (td->eviction_flags != UINT32_MAX) {
5006 struct ofp14_table_mod_prop_eviction *ote;
5008 ote = ofpbuf_put_zeros(reply, sizeof *ote);
5009 ote->type = htons(OFPTMPT14_EVICTION);
5010 ote->length = htons(sizeof *ote);
5011 ote->flags = htonl(td->eviction_flags);
5014 otd = ofpbuf_at_assert(reply, start_otd, sizeof *otd);
5015 otd->length = htons(reply->size - start_otd);
5016 otd->table_id = td->table_id;
5017 otd->config = ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT,
5018 td->eviction, version);
5019 ofpmp_postappend(replies, start_otd);
5023 parse_table_mod_eviction_property(struct ofpbuf *property,
5024 struct ofputil_table_mod *tm)
5026 struct ofp14_table_mod_prop_eviction *ote = property->data;
5028 if (property->size != sizeof *ote) {
5029 return OFPERR_OFPBPC_BAD_LEN;
5032 tm->eviction_flags = ntohl(ote->flags);
5036 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5037 * table configuration (a table mod, table stats, or table features message),
5038 * returns the table eviction configuration that it specifies.
5040 * Only OpenFlow 1.4 and later specify table eviction configuration this way,
5041 * so for other 'version' values this function always returns
5042 * OFPUTIL_TABLE_EVICTION_DEFAULT. */
5043 static enum ofputil_table_eviction
5044 ofputil_decode_table_eviction(ovs_be32 config, enum ofp_version version)
5046 return (version < OFP14_VERSION ? OFPUTIL_TABLE_EVICTION_DEFAULT
5047 : config & htonl(OFPTC14_EVICTION) ? OFPUTIL_TABLE_EVICTION_ON
5048 : OFPUTIL_TABLE_EVICTION_OFF);
5051 /* Returns a bitmap of OFPTC* values suitable for 'config' fields in various
5052 * OpenFlow messages of the given 'version', based on the provided 'miss' and
5053 * 'eviction' values. */
5055 ofputil_encode_table_config(enum ofputil_table_miss miss,
5056 enum ofputil_table_eviction eviction,
5057 enum ofp_version version)
5059 /* See the section "OFPTC_* Table Configuration" in DESIGN.md for more
5060 * information on the crazy evolution of this field. */
5063 /* OpenFlow 1.0 didn't have such a field, any value ought to do. */
5068 /* OpenFlow 1.1 and 1.2 define only OFPTC11_TABLE_MISS_*. */
5070 case OFPUTIL_TABLE_MISS_DEFAULT:
5071 /* Really this shouldn't be used for encoding (the caller should
5072 * provide a specific value) but I can't imagine that defaulting to
5073 * the fall-through case here will hurt. */
5074 case OFPUTIL_TABLE_MISS_CONTROLLER:
5076 return htonl(OFPTC11_TABLE_MISS_CONTROLLER);
5077 case OFPUTIL_TABLE_MISS_CONTINUE:
5078 return htonl(OFPTC11_TABLE_MISS_CONTINUE);
5079 case OFPUTIL_TABLE_MISS_DROP:
5080 return htonl(OFPTC11_TABLE_MISS_DROP);
5085 /* OpenFlow 1.3 removed OFPTC11_TABLE_MISS_* and didn't define any new
5086 * flags, so this is correct. */
5091 /* OpenFlow 1.4 introduced OFPTC14_EVICTION and OFPTC14_VACANCY_EVENTS
5092 * and we don't support the latter yet. */
5093 return htonl(eviction == OFPUTIL_TABLE_EVICTION_ON
5094 ? OFPTC14_EVICTION : 0);
5100 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5101 * table configuration (a table mod, table stats, or table features message),
5102 * returns the table miss configuration that it specifies.
5104 * Only OpenFlow 1.1 and 1.2 specify table miss configurations this way, so for
5105 * other 'version' values this function always returns
5106 * OFPUTIL_TABLE_MISS_DEFAULT. */
5107 static enum ofputil_table_miss
5108 ofputil_decode_table_miss(ovs_be32 config_, enum ofp_version version)
5110 uint32_t config = ntohl(config_);
5112 if (version == OFP11_VERSION || version == OFP12_VERSION) {
5113 switch (config & OFPTC11_TABLE_MISS_MASK) {
5114 case OFPTC11_TABLE_MISS_CONTROLLER:
5115 return OFPUTIL_TABLE_MISS_CONTROLLER;
5117 case OFPTC11_TABLE_MISS_CONTINUE:
5118 return OFPUTIL_TABLE_MISS_CONTINUE;
5120 case OFPTC11_TABLE_MISS_DROP:
5121 return OFPUTIL_TABLE_MISS_DROP;
5124 VLOG_WARN_RL(&bad_ofmsg_rl, "bad table miss config %d", config);
5125 return OFPUTIL_TABLE_MISS_CONTROLLER;
5128 return OFPUTIL_TABLE_MISS_DEFAULT;
5132 /* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in
5133 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
5135 ofputil_decode_table_mod(const struct ofp_header *oh,
5136 struct ofputil_table_mod *pm)
5141 memset(pm, 0, sizeof *pm);
5142 pm->miss = OFPUTIL_TABLE_MISS_DEFAULT;
5143 pm->eviction = OFPUTIL_TABLE_EVICTION_DEFAULT;
5144 pm->eviction_flags = UINT32_MAX;
5145 ofpbuf_use_const(&b, oh, ntohs(oh->length));
5146 raw = ofpraw_pull_assert(&b);
5148 if (raw == OFPRAW_OFPT11_TABLE_MOD) {
5149 const struct ofp11_table_mod *otm = b.data;
5151 pm->table_id = otm->table_id;
5152 pm->miss = ofputil_decode_table_miss(otm->config, oh->version);
5153 } else if (raw == OFPRAW_OFPT14_TABLE_MOD) {
5154 const struct ofp14_table_mod *otm = ofpbuf_pull(&b, sizeof *otm);
5156 pm->table_id = otm->table_id;
5157 pm->miss = ofputil_decode_table_miss(otm->config, oh->version);
5158 pm->eviction = ofputil_decode_table_eviction(otm->config, oh->version);
5159 while (b.size > 0) {
5160 struct ofpbuf property;
5164 error = ofputil_pull_property(&b, &property, &type);
5170 case OFPTMPT14_EVICTION:
5171 error = parse_table_mod_eviction_property(&property, pm);
5175 error = OFPERR_OFPBRC_BAD_TYPE;
5184 return OFPERR_OFPBRC_BAD_TYPE;
5190 /* Converts the abstract form of a "table mod" message in '*tm' into an
5191 * OpenFlow message suitable for 'protocol', and returns that encoded form in a
5192 * buffer owned by the caller. */
5194 ofputil_encode_table_mod(const struct ofputil_table_mod *tm,
5195 enum ofputil_protocol protocol)
5197 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
5200 switch (ofp_version) {
5201 case OFP10_VERSION: {
5202 ovs_fatal(0, "table mod needs OpenFlow 1.1 or later "
5203 "(\'-O OpenFlow11\')");
5208 case OFP13_VERSION: {
5209 struct ofp11_table_mod *otm;
5211 b = ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD, ofp_version, 0);
5212 otm = ofpbuf_put_zeros(b, sizeof *otm);
5213 otm->table_id = tm->table_id;
5214 otm->config = ofputil_encode_table_config(tm->miss, tm->eviction,
5219 case OFP15_VERSION: {
5220 struct ofp14_table_mod *otm;
5221 struct ofp14_table_mod_prop_eviction *ote;
5223 b = ofpraw_alloc(OFPRAW_OFPT14_TABLE_MOD, ofp_version, 0);
5224 otm = ofpbuf_put_zeros(b, sizeof *otm);
5225 otm->table_id = tm->table_id;
5226 otm->config = ofputil_encode_table_config(tm->miss, tm->eviction,
5229 if (tm->eviction_flags != UINT32_MAX) {
5230 ote = ofpbuf_put_zeros(b, sizeof *ote);
5231 ote->type = htons(OFPTMPT14_EVICTION);
5232 ote->length = htons(sizeof *ote);
5233 ote->flags = htonl(tm->eviction_flags);
5244 /* ofputil_role_request */
5246 /* Decodes the OpenFlow "role request" or "role reply" message in '*oh' into
5247 * an abstract form in '*rr'. Returns 0 if successful, otherwise an
5248 * OFPERR_* value. */
5250 ofputil_decode_role_message(const struct ofp_header *oh,
5251 struct ofputil_role_request *rr)
5256 ofpbuf_use_const(&b, oh, ntohs(oh->length));
5257 raw = ofpraw_pull_assert(&b);
5259 if (raw == OFPRAW_OFPT12_ROLE_REQUEST ||
5260 raw == OFPRAW_OFPT12_ROLE_REPLY) {
5261 const struct ofp12_role_request *orr = b.msg;
5263 if (orr->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
5264 orr->role != htonl(OFPCR12_ROLE_EQUAL) &&
5265 orr->role != htonl(OFPCR12_ROLE_MASTER) &&
5266 orr->role != htonl(OFPCR12_ROLE_SLAVE)) {
5267 return OFPERR_OFPRRFC_BAD_ROLE;
5270 rr->role = ntohl(orr->role);
5271 if (raw == OFPRAW_OFPT12_ROLE_REQUEST
5272 ? orr->role == htonl(OFPCR12_ROLE_NOCHANGE)
5273 : orr->generation_id == OVS_BE64_MAX) {
5274 rr->have_generation_id = false;
5275 rr->generation_id = 0;
5277 rr->have_generation_id = true;
5278 rr->generation_id = ntohll(orr->generation_id);
5280 } else if (raw == OFPRAW_NXT_ROLE_REQUEST ||
5281 raw == OFPRAW_NXT_ROLE_REPLY) {
5282 const struct nx_role_request *nrr = b.msg;
5284 BUILD_ASSERT(NX_ROLE_OTHER + 1 == OFPCR12_ROLE_EQUAL);
5285 BUILD_ASSERT(NX_ROLE_MASTER + 1 == OFPCR12_ROLE_MASTER);
5286 BUILD_ASSERT(NX_ROLE_SLAVE + 1 == OFPCR12_ROLE_SLAVE);
5288 if (nrr->role != htonl(NX_ROLE_OTHER) &&
5289 nrr->role != htonl(NX_ROLE_MASTER) &&
5290 nrr->role != htonl(NX_ROLE_SLAVE)) {
5291 return OFPERR_OFPRRFC_BAD_ROLE;
5294 rr->role = ntohl(nrr->role) + 1;
5295 rr->have_generation_id = false;
5296 rr->generation_id = 0;
5304 /* Returns an encoded form of a role reply suitable for the "request" in a
5305 * buffer owned by the caller. */
5307 ofputil_encode_role_reply(const struct ofp_header *request,
5308 const struct ofputil_role_request *rr)
5313 raw = ofpraw_decode_assert(request);
5314 if (raw == OFPRAW_OFPT12_ROLE_REQUEST) {
5315 struct ofp12_role_request *orr;
5317 buf = ofpraw_alloc_reply(OFPRAW_OFPT12_ROLE_REPLY, request, 0);
5318 orr = ofpbuf_put_zeros(buf, sizeof *orr);
5320 orr->role = htonl(rr->role);
5321 orr->generation_id = htonll(rr->have_generation_id
5324 } else if (raw == OFPRAW_NXT_ROLE_REQUEST) {
5325 struct nx_role_request *nrr;
5327 BUILD_ASSERT(NX_ROLE_OTHER == OFPCR12_ROLE_EQUAL - 1);
5328 BUILD_ASSERT(NX_ROLE_MASTER == OFPCR12_ROLE_MASTER - 1);
5329 BUILD_ASSERT(NX_ROLE_SLAVE == OFPCR12_ROLE_SLAVE - 1);
5331 buf = ofpraw_alloc_reply(OFPRAW_NXT_ROLE_REPLY, request, 0);
5332 nrr = ofpbuf_put_zeros(buf, sizeof *nrr);
5333 nrr->role = htonl(rr->role - 1);
5341 /* Encodes "role status" message 'status' for sending in the given
5342 * 'protocol'. Returns the role status message, if 'protocol' supports them,
5343 * otherwise a null pointer. */
5345 ofputil_encode_role_status(const struct ofputil_role_status *status,
5346 enum ofputil_protocol protocol)
5348 enum ofp_version version;
5350 version = ofputil_protocol_to_ofp_version(protocol);
5351 if (version >= OFP14_VERSION) {
5352 struct ofp14_role_status *rstatus;
5355 buf = ofpraw_alloc_xid(OFPRAW_OFPT14_ROLE_STATUS, version, htonl(0),
5357 rstatus = ofpbuf_put_zeros(buf, sizeof *rstatus);
5358 rstatus->role = htonl(status->role);
5359 rstatus->reason = status->reason;
5360 rstatus->generation_id = htonll(status->generation_id);
5369 ofputil_decode_role_status(const struct ofp_header *oh,
5370 struct ofputil_role_status *rs)
5374 const struct ofp14_role_status *r;
5376 ofpbuf_use_const(&b, oh, ntohs(oh->length));
5377 raw = ofpraw_pull_assert(&b);
5378 ovs_assert(raw == OFPRAW_OFPT14_ROLE_STATUS);
5381 if (r->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
5382 r->role != htonl(OFPCR12_ROLE_EQUAL) &&
5383 r->role != htonl(OFPCR12_ROLE_MASTER) &&
5384 r->role != htonl(OFPCR12_ROLE_SLAVE)) {
5385 return OFPERR_OFPRRFC_BAD_ROLE;
5388 rs->role = ntohl(r->role);
5389 rs->generation_id = ntohll(r->generation_id);
5390 rs->reason = r->reason;
5397 /* OpenFlow 1.0 and 1.1 don't distinguish between a field that cannot be
5398 * matched and a field that must be wildcarded. This function returns a bitmap
5399 * that contains both kinds of fields. */
5400 static struct mf_bitmap
5401 wild_or_nonmatchable_fields(const struct ofputil_table_features *features)
5403 struct mf_bitmap wc = features->match;
5404 bitmap_not(wc.bm, MFF_N_IDS);
5405 bitmap_or(wc.bm, features->wildcard.bm, MFF_N_IDS);
5409 struct ofp10_wc_map {
5410 enum ofp10_flow_wildcards wc10;
5411 enum mf_field_id mf;
5414 static const struct ofp10_wc_map ofp10_wc_map[] = {
5415 { OFPFW10_IN_PORT, MFF_IN_PORT },
5416 { OFPFW10_DL_VLAN, MFF_VLAN_VID },
5417 { OFPFW10_DL_SRC, MFF_ETH_SRC },
5418 { OFPFW10_DL_DST, MFF_ETH_DST},
5419 { OFPFW10_DL_TYPE, MFF_ETH_TYPE },
5420 { OFPFW10_NW_PROTO, MFF_IP_PROTO },
5421 { OFPFW10_TP_SRC, MFF_TCP_SRC },
5422 { OFPFW10_TP_DST, MFF_TCP_DST },
5423 { OFPFW10_NW_SRC_MASK, MFF_IPV4_SRC },
5424 { OFPFW10_NW_DST_MASK, MFF_IPV4_DST },
5425 { OFPFW10_DL_VLAN_PCP, MFF_VLAN_PCP },
5426 { OFPFW10_NW_TOS, MFF_IP_DSCP },
5430 mf_bitmap_to_of10(const struct mf_bitmap *fields)
5432 const struct ofp10_wc_map *p;
5435 for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
5436 if (bitmap_is_set(fields->bm, p->mf)) {
5443 static struct mf_bitmap
5444 mf_bitmap_from_of10(ovs_be32 wc10_)
5446 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
5447 const struct ofp10_wc_map *p;
5448 uint32_t wc10 = ntohl(wc10_);
5450 for (p = ofp10_wc_map; p < &ofp10_wc_map[ARRAY_SIZE(ofp10_wc_map)]; p++) {
5451 if (wc10 & p->wc10) {
5452 bitmap_set1(fields.bm, p->mf);
5459 ofputil_put_ofp10_table_stats(const struct ofputil_table_stats *stats,
5460 const struct ofputil_table_features *features,
5463 struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
5464 struct ofp10_table_stats *out;
5466 out = ofpbuf_put_zeros(buf, sizeof *out);
5467 out->table_id = features->table_id;
5468 ovs_strlcpy(out->name, features->name, sizeof out->name);
5469 out->wildcards = mf_bitmap_to_of10(&wc);
5470 out->max_entries = htonl(features->max_entries);
5471 out->active_count = htonl(stats->active_count);
5472 put_32aligned_be64(&out->lookup_count, htonll(stats->lookup_count));
5473 put_32aligned_be64(&out->matched_count, htonll(stats->matched_count));
5476 struct ofp11_wc_map {
5477 enum ofp11_flow_match_fields wc11;
5478 enum mf_field_id mf;
5481 static const struct ofp11_wc_map ofp11_wc_map[] = {
5482 { OFPFMF11_IN_PORT, MFF_IN_PORT },
5483 { OFPFMF11_DL_VLAN, MFF_VLAN_VID },
5484 { OFPFMF11_DL_VLAN_PCP, MFF_VLAN_PCP },
5485 { OFPFMF11_DL_TYPE, MFF_ETH_TYPE },
5486 { OFPFMF11_NW_TOS, MFF_IP_DSCP },
5487 { OFPFMF11_NW_PROTO, MFF_IP_PROTO },
5488 { OFPFMF11_TP_SRC, MFF_TCP_SRC },
5489 { OFPFMF11_TP_DST, MFF_TCP_DST },
5490 { OFPFMF11_MPLS_LABEL, MFF_MPLS_LABEL },
5491 { OFPFMF11_MPLS_TC, MFF_MPLS_TC },
5492 /* I don't know what OFPFMF11_TYPE means. */
5493 { OFPFMF11_DL_SRC, MFF_ETH_SRC },
5494 { OFPFMF11_DL_DST, MFF_ETH_DST },
5495 { OFPFMF11_NW_SRC, MFF_IPV4_SRC },
5496 { OFPFMF11_NW_DST, MFF_IPV4_DST },
5497 { OFPFMF11_METADATA, MFF_METADATA },
5501 mf_bitmap_to_of11(const struct mf_bitmap *fields)
5503 const struct ofp11_wc_map *p;
5506 for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
5507 if (bitmap_is_set(fields->bm, p->mf)) {
5514 static struct mf_bitmap
5515 mf_bitmap_from_of11(ovs_be32 wc11_)
5517 struct mf_bitmap fields = MF_BITMAP_INITIALIZER;
5518 const struct ofp11_wc_map *p;
5519 uint32_t wc11 = ntohl(wc11_);
5521 for (p = ofp11_wc_map; p < &ofp11_wc_map[ARRAY_SIZE(ofp11_wc_map)]; p++) {
5522 if (wc11 & p->wc11) {
5523 bitmap_set1(fields.bm, p->mf);
5530 ofputil_put_ofp11_table_stats(const struct ofputil_table_stats *stats,
5531 const struct ofputil_table_features *features,
5534 struct mf_bitmap wc = wild_or_nonmatchable_fields(features);
5535 struct ofp11_table_stats *out;
5537 out = ofpbuf_put_zeros(buf, sizeof *out);
5538 out->table_id = features->table_id;
5539 ovs_strlcpy(out->name, features->name, sizeof out->name);
5540 out->wildcards = mf_bitmap_to_of11(&wc);
5541 out->match = mf_bitmap_to_of11(&features->match);
5542 out->instructions = ovsinst_bitmap_to_openflow(
5543 features->nonmiss.instructions, OFP11_VERSION);
5544 out->write_actions = ofpact_bitmap_to_openflow(
5545 features->nonmiss.write.ofpacts, OFP11_VERSION);
5546 out->apply_actions = ofpact_bitmap_to_openflow(
5547 features->nonmiss.apply.ofpacts, OFP11_VERSION);
5548 out->config = htonl(features->miss_config);
5549 out->max_entries = htonl(features->max_entries);
5550 out->active_count = htonl(stats->active_count);
5551 out->lookup_count = htonll(stats->lookup_count);
5552 out->matched_count = htonll(stats->matched_count);
5556 ofputil_put_ofp12_table_stats(const struct ofputil_table_stats *stats,
5557 const struct ofputil_table_features *features,
5560 struct ofp12_table_stats *out;
5562 out = ofpbuf_put_zeros(buf, sizeof *out);
5563 out->table_id = features->table_id;
5564 ovs_strlcpy(out->name, features->name, sizeof out->name);
5565 out->match = oxm_bitmap_from_mf_bitmap(&features->match, OFP12_VERSION);
5566 out->wildcards = oxm_bitmap_from_mf_bitmap(&features->wildcard,
5568 out->write_actions = ofpact_bitmap_to_openflow(
5569 features->nonmiss.write.ofpacts, OFP12_VERSION);
5570 out->apply_actions = ofpact_bitmap_to_openflow(
5571 features->nonmiss.apply.ofpacts, OFP12_VERSION);
5572 out->write_setfields = oxm_bitmap_from_mf_bitmap(
5573 &features->nonmiss.write.set_fields, OFP12_VERSION);
5574 out->apply_setfields = oxm_bitmap_from_mf_bitmap(
5575 &features->nonmiss.apply.set_fields, OFP12_VERSION);
5576 out->metadata_match = features->metadata_match;
5577 out->metadata_write = features->metadata_write;
5578 out->instructions = ovsinst_bitmap_to_openflow(
5579 features->nonmiss.instructions, OFP12_VERSION);
5580 out->config = ofputil_encode_table_config(features->miss_config,
5581 OFPUTIL_TABLE_EVICTION_DEFAULT,
5583 out->max_entries = htonl(features->max_entries);
5584 out->active_count = htonl(stats->active_count);
5585 out->lookup_count = htonll(stats->lookup_count);
5586 out->matched_count = htonll(stats->matched_count);
5590 ofputil_put_ofp13_table_stats(const struct ofputil_table_stats *stats,
5593 struct ofp13_table_stats *out;
5595 out = ofpbuf_put_zeros(buf, sizeof *out);
5596 out->table_id = stats->table_id;
5597 out->active_count = htonl(stats->active_count);
5598 out->lookup_count = htonll(stats->lookup_count);
5599 out->matched_count = htonll(stats->matched_count);
5603 ofputil_encode_table_stats_reply(const struct ofp_header *request)
5605 return ofpraw_alloc_stats_reply(request, 0);
5609 ofputil_append_table_stats_reply(struct ofpbuf *reply,
5610 const struct ofputil_table_stats *stats,
5611 const struct ofputil_table_features *features)
5613 struct ofp_header *oh = reply->header;
5615 ovs_assert(stats->table_id == features->table_id);
5617 switch ((enum ofp_version) oh->version) {
5619 ofputil_put_ofp10_table_stats(stats, features, reply);
5623 ofputil_put_ofp11_table_stats(stats, features, reply);
5627 ofputil_put_ofp12_table_stats(stats, features, reply);
5633 ofputil_put_ofp13_table_stats(stats, reply);
5642 ofputil_decode_ofp10_table_stats(struct ofpbuf *msg,
5643 struct ofputil_table_stats *stats,
5644 struct ofputil_table_features *features)
5646 struct ofp10_table_stats *ots;
5648 ots = ofpbuf_try_pull(msg, sizeof *ots);
5650 return OFPERR_OFPBRC_BAD_LEN;
5653 features->table_id = ots->table_id;
5654 ovs_strlcpy(features->name, ots->name, sizeof features->name);
5655 features->max_entries = ntohl(ots->max_entries);
5656 features->match = features->wildcard = mf_bitmap_from_of10(ots->wildcards);
5658 stats->table_id = ots->table_id;
5659 stats->active_count = ntohl(ots->active_count);
5660 stats->lookup_count = ntohll(get_32aligned_be64(&ots->lookup_count));
5661 stats->matched_count = ntohll(get_32aligned_be64(&ots->matched_count));
5667 ofputil_decode_ofp11_table_stats(struct ofpbuf *msg,
5668 struct ofputil_table_stats *stats,
5669 struct ofputil_table_features *features)
5671 struct ofp11_table_stats *ots;
5673 ots = ofpbuf_try_pull(msg, sizeof *ots);
5675 return OFPERR_OFPBRC_BAD_LEN;
5678 features->table_id = ots->table_id;
5679 ovs_strlcpy(features->name, ots->name, sizeof features->name);
5680 features->max_entries = ntohl(ots->max_entries);
5681 features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
5682 ots->instructions, OFP11_VERSION);
5683 features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
5684 ots->write_actions, OFP11_VERSION);
5685 features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
5686 ots->write_actions, OFP11_VERSION);
5687 features->miss = features->nonmiss;
5688 features->miss_config = ofputil_decode_table_miss(ots->config,
5690 features->match = mf_bitmap_from_of11(ots->match);
5691 features->wildcard = mf_bitmap_from_of11(ots->wildcards);
5692 bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
5694 stats->table_id = ots->table_id;
5695 stats->active_count = ntohl(ots->active_count);
5696 stats->lookup_count = ntohll(ots->lookup_count);
5697 stats->matched_count = ntohll(ots->matched_count);
5703 ofputil_decode_ofp12_table_stats(struct ofpbuf *msg,
5704 struct ofputil_table_stats *stats,
5705 struct ofputil_table_features *features)
5707 struct ofp12_table_stats *ots;
5709 ots = ofpbuf_try_pull(msg, sizeof *ots);
5711 return OFPERR_OFPBRC_BAD_LEN;
5714 features->table_id = ots->table_id;
5715 ovs_strlcpy(features->name, ots->name, sizeof features->name);
5716 features->metadata_match = ots->metadata_match;
5717 features->metadata_write = ots->metadata_write;
5718 features->miss_config = ofputil_decode_table_miss(ots->config,
5720 features->max_entries = ntohl(ots->max_entries);
5722 features->nonmiss.instructions = ovsinst_bitmap_from_openflow(
5723 ots->instructions, OFP12_VERSION);
5724 features->nonmiss.write.ofpacts = ofpact_bitmap_from_openflow(
5725 ots->write_actions, OFP12_VERSION);
5726 features->nonmiss.apply.ofpacts = ofpact_bitmap_from_openflow(
5727 ots->apply_actions, OFP12_VERSION);
5728 features->nonmiss.write.set_fields = oxm_bitmap_to_mf_bitmap(
5729 ots->write_setfields, OFP12_VERSION);
5730 features->nonmiss.apply.set_fields = oxm_bitmap_to_mf_bitmap(
5731 ots->apply_setfields, OFP12_VERSION);
5732 features->miss = features->nonmiss;
5734 features->match = oxm_bitmap_to_mf_bitmap(ots->match, OFP12_VERSION);
5735 features->wildcard = oxm_bitmap_to_mf_bitmap(ots->wildcards,
5737 bitmap_or(features->match.bm, features->wildcard.bm, MFF_N_IDS);
5739 stats->table_id = ots->table_id;
5740 stats->active_count = ntohl(ots->active_count);
5741 stats->lookup_count = ntohll(ots->lookup_count);
5742 stats->matched_count = ntohll(ots->matched_count);
5748 ofputil_decode_ofp13_table_stats(struct ofpbuf *msg,
5749 struct ofputil_table_stats *stats,
5750 struct ofputil_table_features *features)
5752 struct ofp13_table_stats *ots;
5754 ots = ofpbuf_try_pull(msg, sizeof *ots);
5756 return OFPERR_OFPBRC_BAD_LEN;
5759 features->table_id = ots->table_id;
5761 stats->table_id = ots->table_id;
5762 stats->active_count = ntohl(ots->active_count);
5763 stats->lookup_count = ntohll(ots->lookup_count);
5764 stats->matched_count = ntohll(ots->matched_count);
5770 ofputil_decode_table_stats_reply(struct ofpbuf *msg,
5771 struct ofputil_table_stats *stats,
5772 struct ofputil_table_features *features)
5774 const struct ofp_header *oh;
5777 ofpraw_pull_assert(msg);
5785 memset(stats, 0, sizeof *stats);
5786 memset(features, 0, sizeof *features);
5787 features->supports_eviction = -1;
5788 features->supports_vacancy_events = -1;
5790 switch ((enum ofp_version) oh->version) {
5792 return ofputil_decode_ofp10_table_stats(msg, stats, features);
5795 return ofputil_decode_ofp11_table_stats(msg, stats, features);
5798 return ofputil_decode_ofp12_table_stats(msg, stats, features);
5803 return ofputil_decode_ofp13_table_stats(msg, stats, features);
5810 /* ofputil_flow_monitor_request */
5812 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
5813 * ofputil_flow_monitor_request in 'rq'.
5815 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
5816 * message. Calling this function multiple times for a single 'msg' iterates
5817 * through the requests. The caller must initially leave 'msg''s layer
5818 * pointers null and not modify them between calls.
5820 * Returns 0 if successful, EOF if no requests were left in this 'msg',
5821 * otherwise an OFPERR_* value. */
5823 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
5826 struct nx_flow_monitor_request *nfmr;
5830 ofpraw_pull_assert(msg);
5837 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
5839 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %"PRIu32" "
5840 "leftover bytes at end", msg->size);
5841 return OFPERR_OFPBRC_BAD_LEN;
5844 flags = ntohs(nfmr->flags);
5845 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
5846 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
5847 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
5848 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
5850 return OFPERR_OFPMOFC_BAD_FLAGS;
5853 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
5854 return OFPERR_NXBRC_MUST_BE_ZERO;
5857 rq->id = ntohl(nfmr->id);
5859 rq->out_port = u16_to_ofp(ntohs(nfmr->out_port));
5860 rq->table_id = nfmr->table_id;
5862 return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL, NULL);
5866 ofputil_append_flow_monitor_request(
5867 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
5869 struct nx_flow_monitor_request *nfmr;
5874 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
5877 start_ofs = msg->size;
5878 ofpbuf_put_zeros(msg, sizeof *nfmr);
5879 match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
5881 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
5882 nfmr->id = htonl(rq->id);
5883 nfmr->flags = htons(rq->flags);
5884 nfmr->out_port = htons(ofp_to_u16(rq->out_port));
5885 nfmr->match_len = htons(match_len);
5886 nfmr->table_id = rq->table_id;
5889 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
5890 * into an abstract ofputil_flow_update in 'update'. The caller must have
5891 * initialized update->match to point to space allocated for a match.
5893 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
5894 * actions (except for NXFME_ABBREV, which never includes actions). The caller
5895 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
5896 * will point into the 'ofpacts' buffer.
5898 * Multiple flow updates can be packed into a single OpenFlow message. Calling
5899 * this function multiple times for a single 'msg' iterates through the
5900 * updates. The caller must initially leave 'msg''s layer pointers null and
5901 * not modify them between calls.
5903 * Returns 0 if successful, EOF if no updates were left in this 'msg',
5904 * otherwise an OFPERR_* value. */
5906 ofputil_decode_flow_update(struct ofputil_flow_update *update,
5907 struct ofpbuf *msg, struct ofpbuf *ofpacts)
5909 struct nx_flow_update_header *nfuh;
5910 unsigned int length;
5911 struct ofp_header *oh;
5914 ofpraw_pull_assert(msg);
5921 if (msg->size < sizeof(struct nx_flow_update_header)) {
5928 update->event = ntohs(nfuh->event);
5929 length = ntohs(nfuh->length);
5930 if (length > msg->size || length % 8) {
5934 if (update->event == NXFME_ABBREV) {
5935 struct nx_flow_update_abbrev *nfua;
5937 if (length != sizeof *nfua) {
5941 nfua = ofpbuf_pull(msg, sizeof *nfua);
5942 update->xid = nfua->xid;
5944 } else if (update->event == NXFME_ADDED
5945 || update->event == NXFME_DELETED
5946 || update->event == NXFME_MODIFIED) {
5947 struct nx_flow_update_full *nfuf;
5948 unsigned int actions_len;
5949 unsigned int match_len;
5952 if (length < sizeof *nfuf) {
5956 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
5957 match_len = ntohs(nfuf->match_len);
5958 if (sizeof *nfuf + match_len > length) {
5962 update->reason = ntohs(nfuf->reason);
5963 update->idle_timeout = ntohs(nfuf->idle_timeout);
5964 update->hard_timeout = ntohs(nfuf->hard_timeout);
5965 update->table_id = nfuf->table_id;
5966 update->cookie = nfuf->cookie;
5967 update->priority = ntohs(nfuf->priority);
5969 error = nx_pull_match(msg, match_len, update->match, NULL, NULL);
5974 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
5975 error = ofpacts_pull_openflow_actions(msg, actions_len, oh->version,
5981 update->ofpacts = ofpacts->data;
5982 update->ofpacts_len = ofpacts->size;
5985 VLOG_WARN_RL(&bad_ofmsg_rl,
5986 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
5987 ntohs(nfuh->event));
5988 return OFPERR_NXBRC_FM_BAD_EVENT;
5992 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %"PRIu32" "
5993 "leftover bytes at end", msg->size);
5994 return OFPERR_OFPBRC_BAD_LEN;
5998 ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
6000 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
6002 return ntohl(cancel->id);
6006 ofputil_encode_flow_monitor_cancel(uint32_t id)
6008 struct nx_flow_monitor_cancel *nfmc;
6011 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
6012 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
6013 nfmc->id = htonl(id);
6018 ofputil_start_flow_update(struct ovs_list *replies)
6022 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
6026 list_push_back(replies, &msg->list_node);
6030 ofputil_append_flow_update(const struct ofputil_flow_update *update,
6031 struct ovs_list *replies)
6033 enum ofp_version version = ofpmp_version(replies);
6034 struct nx_flow_update_header *nfuh;
6038 msg = ofpbuf_from_list(list_back(replies));
6039 start_ofs = msg->size;
6041 if (update->event == NXFME_ABBREV) {
6042 struct nx_flow_update_abbrev *nfua;
6044 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
6045 nfua->xid = update->xid;
6047 struct nx_flow_update_full *nfuf;
6050 ofpbuf_put_zeros(msg, sizeof *nfuf);
6051 match_len = nx_put_match(msg, update->match, htonll(0), htonll(0));
6052 ofpacts_put_openflow_actions(update->ofpacts, update->ofpacts_len, msg,
6054 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
6055 nfuf->reason = htons(update->reason);
6056 nfuf->priority = htons(update->priority);
6057 nfuf->idle_timeout = htons(update->idle_timeout);
6058 nfuf->hard_timeout = htons(update->hard_timeout);
6059 nfuf->match_len = htons(match_len);
6060 nfuf->table_id = update->table_id;
6061 nfuf->cookie = update->cookie;
6064 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
6065 nfuh->length = htons(msg->size - start_ofs);
6066 nfuh->event = htons(update->event);
6068 ofpmp_postappend(replies, start_ofs);
6072 ofputil_encode_packet_out(const struct ofputil_packet_out *po,
6073 enum ofputil_protocol protocol)
6075 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
6079 size = po->ofpacts_len;
6080 if (po->buffer_id == UINT32_MAX) {
6081 size += po->packet_len;
6084 switch (ofp_version) {
6085 case OFP10_VERSION: {
6086 struct ofp10_packet_out *opo;
6089 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
6090 ofpbuf_put_zeros(msg, sizeof *opo);
6091 actions_ofs = msg->size;
6092 ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
6096 opo->buffer_id = htonl(po->buffer_id);
6097 opo->in_port = htons(ofp_to_u16(po->in_port));
6098 opo->actions_len = htons(msg->size - actions_ofs);
6106 case OFP15_VERSION: {
6107 struct ofp11_packet_out *opo;
6110 msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size);
6111 ofpbuf_put_zeros(msg, sizeof *opo);
6112 len = ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
6115 opo->buffer_id = htonl(po->buffer_id);
6116 opo->in_port = ofputil_port_to_ofp11(po->in_port);
6117 opo->actions_len = htons(len);
6125 if (po->buffer_id == UINT32_MAX) {
6126 ofpbuf_put(msg, po->packet, po->packet_len);
6129 ofpmsg_update_length(msg);
6134 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
6136 make_echo_request(enum ofp_version ofp_version)
6138 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
6142 /* Creates and returns an OFPT_ECHO_REPLY message matching the
6143 * OFPT_ECHO_REQUEST message in 'rq'. */
6145 make_echo_reply(const struct ofp_header *rq)
6147 struct ofpbuf rq_buf;
6148 struct ofpbuf *reply;
6150 ofpbuf_use_const(&rq_buf, rq, ntohs(rq->length));
6151 ofpraw_pull_assert(&rq_buf);
6153 reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY, rq, rq_buf.size);
6154 ofpbuf_put(reply, rq_buf.data, rq_buf.size);
6159 ofputil_encode_barrier_request(enum ofp_version ofp_version)
6163 switch (ofp_version) {
6169 type = OFPRAW_OFPT11_BARRIER_REQUEST;
6173 type = OFPRAW_OFPT10_BARRIER_REQUEST;
6180 return ofpraw_alloc(type, ofp_version, 0);
6184 ofputil_frag_handling_to_string(enum ofp_config_flags flags)
6186 switch (flags & OFPC_FRAG_MASK) {
6187 case OFPC_FRAG_NORMAL: return "normal";
6188 case OFPC_FRAG_DROP: return "drop";
6189 case OFPC_FRAG_REASM: return "reassemble";
6190 case OFPC_FRAG_NX_MATCH: return "nx-match";
6197 ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
6199 if (!strcasecmp(s, "normal")) {
6200 *flags = OFPC_FRAG_NORMAL;
6201 } else if (!strcasecmp(s, "drop")) {
6202 *flags = OFPC_FRAG_DROP;
6203 } else if (!strcasecmp(s, "reassemble")) {
6204 *flags = OFPC_FRAG_REASM;
6205 } else if (!strcasecmp(s, "nx-match")) {
6206 *flags = OFPC_FRAG_NX_MATCH;
6213 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
6214 * port number and stores the latter in '*ofp10_port', for the purpose of
6215 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
6216 * otherwise an OFPERR_* number. On error, stores OFPP_NONE in '*ofp10_port'.
6218 * See the definition of OFP11_MAX for an explanation of the mapping. */
6220 ofputil_port_from_ofp11(ovs_be32 ofp11_port, ofp_port_t *ofp10_port)
6222 uint32_t ofp11_port_h = ntohl(ofp11_port);
6224 if (ofp11_port_h < ofp_to_u16(OFPP_MAX)) {
6225 *ofp10_port = u16_to_ofp(ofp11_port_h);
6227 } else if (ofp11_port_h >= ofp11_to_u32(OFPP11_MAX)) {
6228 *ofp10_port = u16_to_ofp(ofp11_port_h - OFPP11_OFFSET);
6231 *ofp10_port = OFPP_NONE;
6232 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
6233 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
6234 ofp11_port_h, ofp_to_u16(OFPP_MAX) - 1,
6235 ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
6236 return OFPERR_OFPBAC_BAD_OUT_PORT;
6240 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
6241 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
6243 * See the definition of OFP11_MAX for an explanation of the mapping. */
6245 ofputil_port_to_ofp11(ofp_port_t ofp10_port)
6247 return htonl(ofp_to_u16(ofp10_port) < ofp_to_u16(OFPP_MAX)
6248 ? ofp_to_u16(ofp10_port)
6249 : ofp_to_u16(ofp10_port) + OFPP11_OFFSET);
6252 #define OFPUTIL_NAMED_PORTS \
6253 OFPUTIL_NAMED_PORT(IN_PORT) \
6254 OFPUTIL_NAMED_PORT(TABLE) \
6255 OFPUTIL_NAMED_PORT(NORMAL) \
6256 OFPUTIL_NAMED_PORT(FLOOD) \
6257 OFPUTIL_NAMED_PORT(ALL) \
6258 OFPUTIL_NAMED_PORT(CONTROLLER) \
6259 OFPUTIL_NAMED_PORT(LOCAL) \
6260 OFPUTIL_NAMED_PORT(ANY) \
6261 OFPUTIL_NAMED_PORT(UNSET)
6263 /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */
6264 #define OFPUTIL_NAMED_PORTS_WITH_NONE \
6265 OFPUTIL_NAMED_PORTS \
6266 OFPUTIL_NAMED_PORT(NONE)
6268 /* Stores the port number represented by 's' into '*portp'. 's' may be an
6269 * integer or, for reserved ports, the standard OpenFlow name for the port
6272 * Returns true if successful, false if 's' is not a valid OpenFlow port number
6273 * or name. The caller should issue an error message in this case, because
6274 * this function usually does not. (This gives the caller an opportunity to
6275 * look up the port name another way, e.g. by contacting the switch and listing
6276 * the names of all its ports).
6278 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
6279 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
6280 * range as described in include/openflow/openflow-1.1.h. */
6282 ofputil_port_from_string(const char *s, ofp_port_t *portp)
6284 unsigned int port32; /* int is at least 32 bits wide. */
6287 VLOG_WARN("Negative value %s is not a valid port number.", s);
6291 if (str_to_uint(s, 10, &port32)) {
6292 if (port32 < ofp_to_u16(OFPP_MAX)) {
6294 } else if (port32 < ofp_to_u16(OFPP_FIRST_RESV)) {
6295 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
6296 "be translated to %u when talking to an OF1.1 or "
6297 "later controller", port32, port32 + OFPP11_OFFSET);
6298 } else if (port32 <= ofp_to_u16(OFPP_LAST_RESV)) {
6299 char name[OFP_MAX_PORT_NAME_LEN];
6301 ofputil_port_to_string(u16_to_ofp(port32), name, sizeof name);
6302 VLOG_WARN_ONCE("referring to port %s as %"PRIu32" is deprecated "
6303 "for compatibility with OpenFlow 1.1 and later",
6305 } else if (port32 < ofp11_to_u32(OFPP11_MAX)) {
6306 VLOG_WARN("port %u is outside the supported range 0 through "
6307 "%"PRIx16" or 0x%x through 0x%"PRIx32, port32,
6308 UINT16_MAX, ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
6311 port32 -= OFPP11_OFFSET;
6314 *portp = u16_to_ofp(port32);
6321 static const struct pair pairs[] = {
6322 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
6323 OFPUTIL_NAMED_PORTS_WITH_NONE
6324 #undef OFPUTIL_NAMED_PORT
6326 const struct pair *p;
6328 for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) {
6329 if (!strcasecmp(s, p->name)) {
6338 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
6339 * Most ports' string representation is just the port number, but for special
6340 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
6342 ofputil_format_port(ofp_port_t port, struct ds *s)
6344 char name[OFP_MAX_PORT_NAME_LEN];
6346 ofputil_port_to_string(port, name, sizeof name);
6347 ds_put_cstr(s, name);
6350 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
6351 * representation of OpenFlow port number 'port'. Most ports are represented
6352 * as just the port number, but special ports, e.g. OFPP_LOCAL, are represented
6353 * by name, e.g. "LOCAL". */
6355 ofputil_port_to_string(ofp_port_t port,
6356 char namebuf[OFP_MAX_PORT_NAME_LEN], size_t bufsize)
6359 #define OFPUTIL_NAMED_PORT(NAME) \
6361 ovs_strlcpy(namebuf, #NAME, bufsize); \
6364 #undef OFPUTIL_NAMED_PORT
6367 snprintf(namebuf, bufsize, "%"PRIu16, port);
6372 /* Stores the group id represented by 's' into '*group_idp'. 's' may be an
6373 * integer or, for reserved group IDs, the standard OpenFlow name for the group
6374 * (either "ANY" or "ALL").
6376 * Returns true if successful, false if 's' is not a valid OpenFlow group ID or
6379 ofputil_group_from_string(const char *s, uint32_t *group_idp)
6381 if (!strcasecmp(s, "any")) {
6382 *group_idp = OFPG11_ANY;
6383 } else if (!strcasecmp(s, "all")) {
6384 *group_idp = OFPG11_ALL;
6385 } else if (!str_to_uint(s, 10, group_idp)) {
6386 VLOG_WARN("%s is not a valid group ID. (Valid group IDs are "
6387 "32-bit nonnegative integers or the keywords ANY or "
6395 /* Appends to 's' a string representation of the OpenFlow group ID 'group_id'.
6396 * Most groups' string representation is just the number, but for special
6397 * groups, e.g. OFPG11_ALL, it is the name, e.g. "ALL". */
6399 ofputil_format_group(uint32_t group_id, struct ds *s)
6401 char name[MAX_GROUP_NAME_LEN];
6403 ofputil_group_to_string(group_id, name, sizeof name);
6404 ds_put_cstr(s, name);
6408 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
6409 * representation of OpenFlow group ID 'group_id'. Most group are represented
6410 * as just their number, but special groups, e.g. OFPG11_ALL, are represented
6411 * by name, e.g. "ALL". */
6413 ofputil_group_to_string(uint32_t group_id,
6414 char namebuf[MAX_GROUP_NAME_LEN + 1], size_t bufsize)
6418 ovs_strlcpy(namebuf, "ALL", bufsize);
6422 ovs_strlcpy(namebuf, "ANY", bufsize);
6426 snprintf(namebuf, bufsize, "%"PRIu32, group_id);
6431 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
6432 * 'ofp_version', tries to pull the first element from the array. If
6433 * successful, initializes '*pp' with an abstract representation of the
6434 * port and returns 0. If no ports remain to be decoded, returns EOF.
6435 * On an error, returns a positive OFPERR_* value. */
6437 ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
6438 struct ofputil_phy_port *pp)
6440 memset(pp, 0, sizeof *pp);
6442 switch (ofp_version) {
6443 case OFP10_VERSION: {
6444 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
6445 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
6449 case OFP13_VERSION: {
6450 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
6451 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
6455 return b->size ? ofputil_pull_ofp14_port(pp, b) : EOF;
6462 ofputil_normalize_match__(struct match *match, bool may_log)
6465 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
6466 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
6467 MAY_NW_PROTO = 1 << 2, /* nw_proto */
6468 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
6469 MAY_ARP_SHA = 1 << 4, /* arp_sha */
6470 MAY_ARP_THA = 1 << 5, /* arp_tha */
6471 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
6472 MAY_ND_TARGET = 1 << 7, /* nd_target */
6473 MAY_MPLS = 1 << 8, /* mpls label and tc */
6476 struct flow_wildcards wc;
6478 /* Figure out what fields may be matched. */
6479 if (match->flow.dl_type == htons(ETH_TYPE_IP)) {
6480 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
6481 if (match->flow.nw_proto == IPPROTO_TCP ||
6482 match->flow.nw_proto == IPPROTO_UDP ||
6483 match->flow.nw_proto == IPPROTO_SCTP ||
6484 match->flow.nw_proto == IPPROTO_ICMP) {
6485 may_match |= MAY_TP_ADDR;
6487 } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
6488 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
6489 if (match->flow.nw_proto == IPPROTO_TCP ||
6490 match->flow.nw_proto == IPPROTO_UDP ||
6491 match->flow.nw_proto == IPPROTO_SCTP) {
6492 may_match |= MAY_TP_ADDR;
6493 } else if (match->flow.nw_proto == IPPROTO_ICMPV6) {
6494 may_match |= MAY_TP_ADDR;
6495 if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
6496 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
6497 } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
6498 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
6501 } else if (match->flow.dl_type == htons(ETH_TYPE_ARP) ||
6502 match->flow.dl_type == htons(ETH_TYPE_RARP)) {
6503 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
6504 } else if (eth_type_mpls(match->flow.dl_type)) {
6505 may_match = MAY_MPLS;
6510 /* Clear the fields that may not be matched. */
6512 if (!(may_match & MAY_NW_ADDR)) {
6513 wc.masks.nw_src = wc.masks.nw_dst = htonl(0);
6515 if (!(may_match & MAY_TP_ADDR)) {
6516 wc.masks.tp_src = wc.masks.tp_dst = htons(0);
6518 if (!(may_match & MAY_NW_PROTO)) {
6519 wc.masks.nw_proto = 0;
6521 if (!(may_match & MAY_IPVx)) {
6522 wc.masks.nw_tos = 0;
6523 wc.masks.nw_ttl = 0;
6525 if (!(may_match & MAY_ARP_SHA)) {
6526 memset(wc.masks.arp_sha, 0, ETH_ADDR_LEN);
6528 if (!(may_match & MAY_ARP_THA)) {
6529 memset(wc.masks.arp_tha, 0, ETH_ADDR_LEN);
6531 if (!(may_match & MAY_IPV6)) {
6532 wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any;
6533 wc.masks.ipv6_label = htonl(0);
6535 if (!(may_match & MAY_ND_TARGET)) {
6536 wc.masks.nd_target = in6addr_any;
6538 if (!(may_match & MAY_MPLS)) {
6539 memset(wc.masks.mpls_lse, 0, sizeof wc.masks.mpls_lse);
6542 /* Log any changes. */
6543 if (!flow_wildcards_equal(&wc, &match->wc)) {
6544 bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
6545 char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL;
6548 match_zero_wildcarded_fields(match);
6551 char *post = match_to_string(match, OFP_DEFAULT_PRIORITY);
6552 VLOG_INFO("normalization changed ofp_match, details:");
6553 VLOG_INFO(" pre: %s", pre);
6554 VLOG_INFO("post: %s", post);
6561 /* "Normalizes" the wildcards in 'match'. That means:
6563 * 1. If the type of level N is known, then only the valid fields for that
6564 * level may be specified. For example, ARP does not have a TOS field,
6565 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
6566 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
6567 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
6570 * 2. If the type of level N is not known (or not understood by Open
6571 * vSwitch), then no fields at all for that level may be specified. For
6572 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
6573 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
6576 * If this function changes 'match', it logs a rate-limited informational
6579 ofputil_normalize_match(struct match *match)
6581 ofputil_normalize_match__(match, true);
6584 /* Same as ofputil_normalize_match() without the logging. Thus, this function
6585 * is suitable for a program's internal use, whereas ofputil_normalize_match()
6586 * sense for use on flows received from elsewhere (so that a bug in the program
6587 * that sent them can be reported and corrected). */
6589 ofputil_normalize_match_quiet(struct match *match)
6591 ofputil_normalize_match__(match, false);
6594 /* Parses a key or a key-value pair from '*stringp'.
6596 * On success: Stores the key into '*keyp'. Stores the value, if present, into
6597 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
6598 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
6599 * are substrings of '*stringp' created by replacing some of its bytes by null
6600 * terminators. Returns true.
6602 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
6603 * NULL and returns false. */
6605 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
6607 char *pos, *key, *value;
6611 pos += strspn(pos, ", \t\r\n");
6613 *keyp = *valuep = NULL;
6618 key_len = strcspn(pos, ":=(, \t\r\n");
6619 if (key[key_len] == ':' || key[key_len] == '=') {
6620 /* The value can be separated by a colon. */
6623 value = key + key_len + 1;
6624 value_len = strcspn(value, ", \t\r\n");
6625 pos = value + value_len + (value[value_len] != '\0');
6626 value[value_len] = '\0';
6627 } else if (key[key_len] == '(') {
6628 /* The value can be surrounded by balanced parentheses. The outermost
6629 * set of parentheses is removed. */
6633 value = key + key_len + 1;
6634 for (value_len = 0; level > 0; value_len++) {
6635 switch (value[value_len]) {
6649 value[value_len - 1] = '\0';
6650 pos = value + value_len;
6652 /* There might be no value at all. */
6653 value = key + key_len; /* Will become the empty string below. */
6654 pos = key + key_len + (key[key_len] != '\0');
6656 key[key_len] = '\0';
6664 /* Encode a dump ports request for 'port', the encoded message
6665 * will be for OpenFlow version 'ofp_version'. Returns message
6666 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
6668 ofputil_encode_dump_ports_request(enum ofp_version ofp_version, ofp_port_t port)
6670 struct ofpbuf *request;
6672 switch (ofp_version) {
6673 case OFP10_VERSION: {
6674 struct ofp10_port_stats_request *req;
6675 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0);
6676 req = ofpbuf_put_zeros(request, sizeof *req);
6677 req->port_no = htons(ofp_to_u16(port));
6684 case OFP15_VERSION: {
6685 struct ofp11_port_stats_request *req;
6686 request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0);
6687 req = ofpbuf_put_zeros(request, sizeof *req);
6688 req->port_no = ofputil_port_to_ofp11(port);
6699 ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops,
6700 struct ofp10_port_stats *ps10)
6702 ps10->port_no = htons(ofp_to_u16(ops->port_no));
6703 memset(ps10->pad, 0, sizeof ps10->pad);
6704 put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets));
6705 put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets));
6706 put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes));
6707 put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes));
6708 put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped));
6709 put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped));
6710 put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors));
6711 put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors));
6712 put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors));
6713 put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors));
6714 put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors));
6715 put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions));
6719 ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops,
6720 struct ofp11_port_stats *ps11)
6722 ps11->port_no = ofputil_port_to_ofp11(ops->port_no);
6723 memset(ps11->pad, 0, sizeof ps11->pad);
6724 ps11->rx_packets = htonll(ops->stats.rx_packets);
6725 ps11->tx_packets = htonll(ops->stats.tx_packets);
6726 ps11->rx_bytes = htonll(ops->stats.rx_bytes);
6727 ps11->tx_bytes = htonll(ops->stats.tx_bytes);
6728 ps11->rx_dropped = htonll(ops->stats.rx_dropped);
6729 ps11->tx_dropped = htonll(ops->stats.tx_dropped);
6730 ps11->rx_errors = htonll(ops->stats.rx_errors);
6731 ps11->tx_errors = htonll(ops->stats.tx_errors);
6732 ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors);
6733 ps11->rx_over_err = htonll(ops->stats.rx_over_errors);
6734 ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors);
6735 ps11->collisions = htonll(ops->stats.collisions);
6739 ofputil_port_stats_to_ofp13(const struct ofputil_port_stats *ops,
6740 struct ofp13_port_stats *ps13)
6742 ofputil_port_stats_to_ofp11(ops, &ps13->ps);
6743 ps13->duration_sec = htonl(ops->duration_sec);
6744 ps13->duration_nsec = htonl(ops->duration_nsec);
6748 ofputil_append_ofp14_port_stats(const struct ofputil_port_stats *ops,
6749 struct ovs_list *replies)
6751 struct ofp14_port_stats_prop_ethernet *eth;
6752 struct ofp14_port_stats *ps14;
6753 struct ofpbuf *reply;
6755 reply = ofpmp_reserve(replies, sizeof *ps14 + sizeof *eth);
6757 ps14 = ofpbuf_put_uninit(reply, sizeof *ps14);
6758 ps14->length = htons(sizeof *ps14 + sizeof *eth);
6759 memset(ps14->pad, 0, sizeof ps14->pad);
6760 ps14->port_no = ofputil_port_to_ofp11(ops->port_no);
6761 ps14->duration_sec = htonl(ops->duration_sec);
6762 ps14->duration_nsec = htonl(ops->duration_nsec);
6763 ps14->rx_packets = htonll(ops->stats.rx_packets);
6764 ps14->tx_packets = htonll(ops->stats.tx_packets);
6765 ps14->rx_bytes = htonll(ops->stats.rx_bytes);
6766 ps14->tx_bytes = htonll(ops->stats.tx_bytes);
6767 ps14->rx_dropped = htonll(ops->stats.rx_dropped);
6768 ps14->tx_dropped = htonll(ops->stats.tx_dropped);
6769 ps14->rx_errors = htonll(ops->stats.rx_errors);
6770 ps14->tx_errors = htonll(ops->stats.tx_errors);
6772 eth = ofpbuf_put_uninit(reply, sizeof *eth);
6773 eth->type = htons(OFPPSPT14_ETHERNET);
6774 eth->length = htons(sizeof *eth);
6775 memset(eth->pad, 0, sizeof eth->pad);
6776 eth->rx_frame_err = htonll(ops->stats.rx_frame_errors);
6777 eth->rx_over_err = htonll(ops->stats.rx_over_errors);
6778 eth->rx_crc_err = htonll(ops->stats.rx_crc_errors);
6779 eth->collisions = htonll(ops->stats.collisions);
6782 /* Encode a ports stat for 'ops' and append it to 'replies'. */
6784 ofputil_append_port_stat(struct ovs_list *replies,
6785 const struct ofputil_port_stats *ops)
6787 switch (ofpmp_version(replies)) {
6788 case OFP13_VERSION: {
6789 struct ofp13_port_stats *reply = ofpmp_append(replies, sizeof *reply);
6790 ofputil_port_stats_to_ofp13(ops, reply);
6794 case OFP11_VERSION: {
6795 struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply);
6796 ofputil_port_stats_to_ofp11(ops, reply);
6800 case OFP10_VERSION: {
6801 struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply);
6802 ofputil_port_stats_to_ofp10(ops, reply);
6808 ofputil_append_ofp14_port_stats(ops, replies);
6817 ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops,
6818 const struct ofp10_port_stats *ps10)
6820 memset(ops, 0, sizeof *ops);
6822 ops->port_no = u16_to_ofp(ntohs(ps10->port_no));
6823 ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets));
6824 ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets));
6825 ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes));
6826 ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes));
6827 ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped));
6828 ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped));
6829 ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors));
6830 ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors));
6831 ops->stats.rx_frame_errors =
6832 ntohll(get_32aligned_be64(&ps10->rx_frame_err));
6833 ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err));
6834 ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err));
6835 ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions));
6836 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
6842 ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops,
6843 const struct ofp11_port_stats *ps11)
6847 memset(ops, 0, sizeof *ops);
6848 error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no);
6853 ops->stats.rx_packets = ntohll(ps11->rx_packets);
6854 ops->stats.tx_packets = ntohll(ps11->tx_packets);
6855 ops->stats.rx_bytes = ntohll(ps11->rx_bytes);
6856 ops->stats.tx_bytes = ntohll(ps11->tx_bytes);
6857 ops->stats.rx_dropped = ntohll(ps11->rx_dropped);
6858 ops->stats.tx_dropped = ntohll(ps11->tx_dropped);
6859 ops->stats.rx_errors = ntohll(ps11->rx_errors);
6860 ops->stats.tx_errors = ntohll(ps11->tx_errors);
6861 ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err);
6862 ops->stats.rx_over_errors = ntohll(ps11->rx_over_err);
6863 ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err);
6864 ops->stats.collisions = ntohll(ps11->collisions);
6865 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
6871 ofputil_port_stats_from_ofp13(struct ofputil_port_stats *ops,
6872 const struct ofp13_port_stats *ps13)
6874 enum ofperr error = ofputil_port_stats_from_ofp11(ops, &ps13->ps);
6876 ops->duration_sec = ntohl(ps13->duration_sec);
6877 ops->duration_nsec = ntohl(ps13->duration_nsec);
6883 parse_ofp14_port_stats_ethernet_property(const struct ofpbuf *payload,
6884 struct ofputil_port_stats *ops)
6886 const struct ofp14_port_stats_prop_ethernet *eth = payload->data;
6888 if (payload->size != sizeof *eth) {
6889 return OFPERR_OFPBPC_BAD_LEN;
6892 ops->stats.rx_frame_errors = ntohll(eth->rx_frame_err);
6893 ops->stats.rx_over_errors = ntohll(eth->rx_over_err);
6894 ops->stats.rx_crc_errors = ntohll(eth->rx_crc_err);
6895 ops->stats.collisions = ntohll(eth->collisions);
6901 ofputil_pull_ofp14_port_stats(struct ofputil_port_stats *ops,
6904 const struct ofp14_port_stats *ps14;
6905 struct ofpbuf properties;
6909 ps14 = ofpbuf_try_pull(msg, sizeof *ps14);
6911 return OFPERR_OFPBRC_BAD_LEN;
6914 len = ntohs(ps14->length);
6915 if (len < sizeof *ps14 || len - sizeof *ps14 > msg->size) {
6916 return OFPERR_OFPBRC_BAD_LEN;
6918 len -= sizeof *ps14;
6919 ofpbuf_use_const(&properties, ofpbuf_pull(msg, len), len);
6921 error = ofputil_port_from_ofp11(ps14->port_no, &ops->port_no);
6926 ops->duration_sec = ntohl(ps14->duration_sec);
6927 ops->duration_nsec = ntohl(ps14->duration_nsec);
6928 ops->stats.rx_packets = ntohll(ps14->rx_packets);
6929 ops->stats.tx_packets = ntohll(ps14->tx_packets);
6930 ops->stats.rx_bytes = ntohll(ps14->rx_bytes);
6931 ops->stats.tx_bytes = ntohll(ps14->tx_bytes);
6932 ops->stats.rx_dropped = ntohll(ps14->rx_dropped);
6933 ops->stats.tx_dropped = ntohll(ps14->tx_dropped);
6934 ops->stats.rx_errors = ntohll(ps14->rx_errors);
6935 ops->stats.tx_errors = ntohll(ps14->tx_errors);
6936 ops->stats.rx_frame_errors = UINT64_MAX;
6937 ops->stats.rx_over_errors = UINT64_MAX;
6938 ops->stats.rx_crc_errors = UINT64_MAX;
6939 ops->stats.collisions = UINT64_MAX;
6941 while (properties.size > 0) {
6942 struct ofpbuf payload;
6946 error = ofputil_pull_property(&properties, &payload, &type);
6952 case OFPPSPT14_ETHERNET:
6953 error = parse_ofp14_port_stats_ethernet_property(&payload, ops);
6957 log_property(true, "unknown port stats property %"PRIu16, type);
6970 /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
6973 ofputil_count_port_stats(const struct ofp_header *oh)
6975 struct ofputil_port_stats ps;
6979 ofpbuf_use_const(&b, oh, ntohs(oh->length));
6980 ofpraw_pull_assert(&b);
6981 while (!ofputil_decode_port_stats(&ps, &b)) {
6987 /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
6988 * ofputil_port_stats in 'ps'.
6990 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
6991 * message. Calling this function multiple times for a single 'msg' iterates
6992 * through the replies. The caller must initially leave 'msg''s layer pointers
6993 * null and not modify them between calls.
6995 * Returns 0 if successful, EOF if no replies were left in this 'msg',
6996 * otherwise a positive errno value. */
6998 ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg)
7003 error = (msg->header ? ofpraw_decode(&raw, msg->header)
7004 : ofpraw_pull(&raw, msg));
7011 } else if (raw == OFPRAW_OFPST14_PORT_REPLY) {
7012 return ofputil_pull_ofp14_port_stats(ps, msg);
7013 } else if (raw == OFPRAW_OFPST13_PORT_REPLY) {
7014 const struct ofp13_port_stats *ps13;
7016 ps13 = ofpbuf_try_pull(msg, sizeof *ps13);
7020 return ofputil_port_stats_from_ofp13(ps, ps13);
7021 } else if (raw == OFPRAW_OFPST11_PORT_REPLY) {
7022 const struct ofp11_port_stats *ps11;
7024 ps11 = ofpbuf_try_pull(msg, sizeof *ps11);
7028 return ofputil_port_stats_from_ofp11(ps, ps11);
7029 } else if (raw == OFPRAW_OFPST10_PORT_REPLY) {
7030 const struct ofp10_port_stats *ps10;
7032 ps10 = ofpbuf_try_pull(msg, sizeof *ps10);
7036 return ofputil_port_stats_from_ofp10(ps, ps10);
7042 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %"PRIu32" leftover "
7043 "bytes at end", msg->size);
7044 return OFPERR_OFPBRC_BAD_LEN;
7047 /* Parse a port status request message into a 16 bit OpenFlow 1.0
7048 * port number and stores the latter in '*ofp10_port'.
7049 * Returns 0 if successful, otherwise an OFPERR_* number. */
7051 ofputil_decode_port_stats_request(const struct ofp_header *request,
7052 ofp_port_t *ofp10_port)
7054 switch ((enum ofp_version)request->version) {
7059 case OFP11_VERSION: {
7060 const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request);
7061 return ofputil_port_from_ofp11(psr11->port_no, ofp10_port);
7064 case OFP10_VERSION: {
7065 const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request);
7066 *ofp10_port = u16_to_ofp(ntohs(psr10->port_no));
7075 /* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */
7077 ofputil_bucket_list_destroy(struct ovs_list *buckets)
7079 struct ofputil_bucket *bucket;
7081 LIST_FOR_EACH_POP (bucket, list_node, buckets) {
7082 free(bucket->ofpacts);
7087 /* Clones 'bucket' and its ofpacts data */
7088 static struct ofputil_bucket *
7089 ofputil_bucket_clone_data(const struct ofputil_bucket *bucket)
7091 struct ofputil_bucket *new;
7093 new = xmemdup(bucket, sizeof *bucket);
7094 new->ofpacts = xmemdup(bucket->ofpacts, bucket->ofpacts_len);
7099 /* Clones each of the buckets in the list 'src' appending them
7100 * in turn to 'dest' which should be an initialised list.
7101 * An exception is that if the pointer value of a bucket in 'src'
7102 * matches 'skip' then it is not cloned or appended to 'dest'.
7103 * This allows all of 'src' or 'all of 'src' except 'skip' to
7104 * be cloned and appended to 'dest'. */
7106 ofputil_bucket_clone_list(struct ovs_list *dest, const struct ovs_list *src,
7107 const struct ofputil_bucket *skip)
7109 struct ofputil_bucket *bucket;
7111 LIST_FOR_EACH (bucket, list_node, src) {
7112 struct ofputil_bucket *new_bucket;
7114 if (bucket == skip) {
7118 new_bucket = ofputil_bucket_clone_data(bucket);
7119 list_push_back(dest, &new_bucket->list_node);
7123 /* Find a bucket in the list 'buckets' whose bucket id is 'bucket_id'
7124 * Returns the first bucket found or NULL if no buckets are found. */
7125 struct ofputil_bucket *
7126 ofputil_bucket_find(const struct ovs_list *buckets, uint32_t bucket_id)
7128 struct ofputil_bucket *bucket;
7130 if (bucket_id > OFPG15_BUCKET_MAX) {
7134 LIST_FOR_EACH (bucket, list_node, buckets) {
7135 if (bucket->bucket_id == bucket_id) {
7143 /* Returns true if more than one bucket in the list 'buckets'
7144 * have the same bucket id. Returns false otherwise. */
7146 ofputil_bucket_check_duplicate_id(const struct ovs_list *buckets)
7148 struct ofputil_bucket *i, *j;
7150 LIST_FOR_EACH (i, list_node, buckets) {
7151 LIST_FOR_EACH_REVERSE (j, list_node, buckets) {
7155 if (i->bucket_id == j->bucket_id) {
7164 /* Returns the bucket at the front of the list 'buckets'.
7165 * Undefined if 'buckets is empty. */
7166 struct ofputil_bucket *
7167 ofputil_bucket_list_front(const struct ovs_list *buckets)
7169 static struct ofputil_bucket *bucket;
7171 ASSIGN_CONTAINER(bucket, list_front(buckets), list_node);
7176 /* Returns the bucket at the back of the list 'buckets'.
7177 * Undefined if 'buckets is empty. */
7178 struct ofputil_bucket *
7179 ofputil_bucket_list_back(const struct ovs_list *buckets)
7181 static struct ofputil_bucket *bucket;
7183 ASSIGN_CONTAINER(bucket, list_back(buckets), list_node);
7188 /* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version',
7189 * that requests stats for group 'group_id'. (Use OFPG_ALL to request stats
7192 * Group statistics include packet and byte counts for each group. */
7194 ofputil_encode_group_stats_request(enum ofp_version ofp_version,
7197 struct ofpbuf *request;
7199 switch (ofp_version) {
7201 ovs_fatal(0, "dump-group-stats needs OpenFlow 1.1 or later "
7202 "(\'-O OpenFlow11\')");
7207 case OFP15_VERSION: {
7208 struct ofp11_group_stats_request *req;
7209 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_REQUEST, ofp_version, 0);
7210 req = ofpbuf_put_zeros(request, sizeof *req);
7211 req->group_id = htonl(group_id);
7222 ofputil_uninit_group_desc(struct ofputil_group_desc *gd)
7224 ofputil_bucket_list_destroy(&gd->buckets);
7225 free(&gd->props.fields);
7228 /* Decodes the OpenFlow group description request in 'oh', returning the group
7229 * whose description is requested, or OFPG_ALL if stats for all groups was
7232 ofputil_decode_group_desc_request(const struct ofp_header *oh)
7234 struct ofpbuf request;
7237 ofpbuf_use_const(&request, oh, ntohs(oh->length));
7238 raw = ofpraw_pull_assert(&request);
7239 if (raw == OFPRAW_OFPST11_GROUP_DESC_REQUEST) {
7241 } else if (raw == OFPRAW_OFPST15_GROUP_DESC_REQUEST) {
7242 ovs_be32 *group_id = ofpbuf_pull(&request, sizeof *group_id);
7243 return ntohl(*group_id);
7249 /* Returns an OpenFlow group description request for OpenFlow version
7250 * 'ofp_version', that requests stats for group 'group_id'. Use OFPG_ALL to
7251 * request stats for all groups (OpenFlow 1.4 and earlier always request all
7254 * Group descriptions include the bucket and action configuration for each
7257 ofputil_encode_group_desc_request(enum ofp_version ofp_version,
7260 struct ofpbuf *request;
7263 switch (ofp_version) {
7265 ovs_fatal(0, "dump-groups needs OpenFlow 1.1 or later "
7266 "(\'-O OpenFlow11\')");
7271 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_DESC_REQUEST,
7275 request = ofpraw_alloc(OFPRAW_OFPST15_GROUP_DESC_REQUEST,
7277 gid = htonl(group_id);
7278 ofpbuf_put(request, &gid, sizeof gid);
7288 ofputil_group_bucket_counters_to_ofp11(const struct ofputil_group_stats *gs,
7289 struct ofp11_bucket_counter bucket_cnts[])
7293 for (i = 0; i < gs->n_buckets; i++) {
7294 bucket_cnts[i].packet_count = htonll(gs->bucket_stats[i].packet_count);
7295 bucket_cnts[i].byte_count = htonll(gs->bucket_stats[i].byte_count);
7300 ofputil_group_stats_to_ofp11(const struct ofputil_group_stats *gs,
7301 struct ofp11_group_stats *gs11, size_t length,
7302 struct ofp11_bucket_counter bucket_cnts[])
7304 memset(gs11, 0, sizeof *gs11);
7305 gs11->length = htons(length);
7306 gs11->group_id = htonl(gs->group_id);
7307 gs11->ref_count = htonl(gs->ref_count);
7308 gs11->packet_count = htonll(gs->packet_count);
7309 gs11->byte_count = htonll(gs->byte_count);
7310 ofputil_group_bucket_counters_to_ofp11(gs, bucket_cnts);
7314 ofputil_group_stats_to_ofp13(const struct ofputil_group_stats *gs,
7315 struct ofp13_group_stats *gs13, size_t length,
7316 struct ofp11_bucket_counter bucket_cnts[])
7318 ofputil_group_stats_to_ofp11(gs, &gs13->gs, length, bucket_cnts);
7319 gs13->duration_sec = htonl(gs->duration_sec);
7320 gs13->duration_nsec = htonl(gs->duration_nsec);
7324 /* Encodes 'gs' properly for the format of the list of group statistics
7325 * replies already begun in 'replies' and appends it to the list. 'replies'
7326 * must have originally been initialized with ofpmp_init(). */
7328 ofputil_append_group_stats(struct ovs_list *replies,
7329 const struct ofputil_group_stats *gs)
7331 size_t bucket_counter_size;
7332 struct ofp11_bucket_counter *bucket_counters;
7335 bucket_counter_size = gs->n_buckets * sizeof(struct ofp11_bucket_counter);
7337 switch (ofpmp_version(replies)) {
7339 case OFP12_VERSION:{
7340 struct ofp11_group_stats *gs11;
7342 length = sizeof *gs11 + bucket_counter_size;
7343 gs11 = ofpmp_append(replies, length);
7344 bucket_counters = (struct ofp11_bucket_counter *)(gs11 + 1);
7345 ofputil_group_stats_to_ofp11(gs, gs11, length, bucket_counters);
7351 case OFP15_VERSION: {
7352 struct ofp13_group_stats *gs13;
7354 length = sizeof *gs13 + bucket_counter_size;
7355 gs13 = ofpmp_append(replies, length);
7356 bucket_counters = (struct ofp11_bucket_counter *)(gs13 + 1);
7357 ofputil_group_stats_to_ofp13(gs, gs13, length, bucket_counters);
7366 /* Returns an OpenFlow group features request for OpenFlow version
7369 ofputil_encode_group_features_request(enum ofp_version ofp_version)
7371 struct ofpbuf *request = NULL;
7373 switch (ofp_version) {
7376 ovs_fatal(0, "dump-group-features needs OpenFlow 1.2 or later "
7377 "(\'-O OpenFlow12\')");
7382 request = ofpraw_alloc(OFPRAW_OFPST12_GROUP_FEATURES_REQUEST,
7392 /* Returns a OpenFlow message that encodes 'features' properly as a reply to
7393 * group features request 'request'. */
7395 ofputil_encode_group_features_reply(
7396 const struct ofputil_group_features *features,
7397 const struct ofp_header *request)
7399 struct ofp12_group_features_stats *ogf;
7400 struct ofpbuf *reply;
7403 reply = ofpraw_alloc_xid(OFPRAW_OFPST12_GROUP_FEATURES_REPLY,
7404 request->version, request->xid, 0);
7405 ogf = ofpbuf_put_zeros(reply, sizeof *ogf);
7406 ogf->types = htonl(features->types);
7407 ogf->capabilities = htonl(features->capabilities);
7408 for (i = 0; i < OFPGT12_N_TYPES; i++) {
7409 ogf->max_groups[i] = htonl(features->max_groups[i]);
7410 ogf->actions[i] = ofpact_bitmap_to_openflow(features->ofpacts[i],
7417 /* Decodes group features reply 'oh' into 'features'. */
7419 ofputil_decode_group_features_reply(const struct ofp_header *oh,
7420 struct ofputil_group_features *features)
7422 const struct ofp12_group_features_stats *ogf = ofpmsg_body(oh);
7425 features->types = ntohl(ogf->types);
7426 features->capabilities = ntohl(ogf->capabilities);
7427 for (i = 0; i < OFPGT12_N_TYPES; i++) {
7428 features->max_groups[i] = ntohl(ogf->max_groups[i]);
7429 features->ofpacts[i] = ofpact_bitmap_from_openflow(
7430 ogf->actions[i], oh->version);
7434 /* Parse a group status request message into a 32 bit OpenFlow 1.1
7435 * group ID and stores the latter in '*group_id'.
7436 * Returns 0 if successful, otherwise an OFPERR_* number. */
7438 ofputil_decode_group_stats_request(const struct ofp_header *request,
7441 const struct ofp11_group_stats_request *gsr11 = ofpmsg_body(request);
7442 *group_id = ntohl(gsr11->group_id);
7446 /* Converts a group stats reply in 'msg' into an abstract ofputil_group_stats
7447 * in 'gs'. Assigns freshly allocated memory to gs->bucket_stats for the
7448 * caller to eventually free.
7450 * Multiple group stats replies can be packed into a single OpenFlow message.
7451 * Calling this function multiple times for a single 'msg' iterates through the
7452 * replies. The caller must initially leave 'msg''s layer pointers null and
7453 * not modify them between calls.
7455 * Returns 0 if successful, EOF if no replies were left in this 'msg',
7456 * otherwise a positive errno value. */
7458 ofputil_decode_group_stats_reply(struct ofpbuf *msg,
7459 struct ofputil_group_stats *gs)
7461 struct ofp11_bucket_counter *obc;
7462 struct ofp11_group_stats *ogs11;
7469 gs->bucket_stats = NULL;
7470 error = (msg->header ? ofpraw_decode(&raw, msg->header)
7471 : ofpraw_pull(&raw, msg));
7480 if (raw == OFPRAW_OFPST11_GROUP_REPLY) {
7481 base_len = sizeof *ogs11;
7482 ogs11 = ofpbuf_try_pull(msg, sizeof *ogs11);
7483 gs->duration_sec = gs->duration_nsec = UINT32_MAX;
7484 } else if (raw == OFPRAW_OFPST13_GROUP_REPLY) {
7485 struct ofp13_group_stats *ogs13;
7487 base_len = sizeof *ogs13;
7488 ogs13 = ofpbuf_try_pull(msg, sizeof *ogs13);
7491 gs->duration_sec = ntohl(ogs13->duration_sec);
7492 gs->duration_nsec = ntohl(ogs13->duration_nsec);
7501 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
7502 ofpraw_get_name(raw), msg->size);
7503 return OFPERR_OFPBRC_BAD_LEN;
7505 length = ntohs(ogs11->length);
7506 if (length < sizeof base_len) {
7507 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply claims invalid length %"PRIuSIZE,
7508 ofpraw_get_name(raw), length);
7509 return OFPERR_OFPBRC_BAD_LEN;
7512 gs->group_id = ntohl(ogs11->group_id);
7513 gs->ref_count = ntohl(ogs11->ref_count);
7514 gs->packet_count = ntohll(ogs11->packet_count);
7515 gs->byte_count = ntohll(ogs11->byte_count);
7517 gs->n_buckets = (length - base_len) / sizeof *obc;
7518 obc = ofpbuf_try_pull(msg, gs->n_buckets * sizeof *obc);
7520 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
7521 ofpraw_get_name(raw), msg->size);
7522 return OFPERR_OFPBRC_BAD_LEN;
7525 gs->bucket_stats = xmalloc(gs->n_buckets * sizeof *gs->bucket_stats);
7526 for (i = 0; i < gs->n_buckets; i++) {
7527 gs->bucket_stats[i].packet_count = ntohll(obc[i].packet_count);
7528 gs->bucket_stats[i].byte_count = ntohll(obc[i].byte_count);
7535 ofputil_put_ofp11_bucket(const struct ofputil_bucket *bucket,
7536 struct ofpbuf *openflow, enum ofp_version ofp_version)
7538 struct ofp11_bucket *ob;
7541 start = openflow->size;
7542 ofpbuf_put_zeros(openflow, sizeof *ob);
7543 ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
7544 openflow, ofp_version);
7545 ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
7546 ob->len = htons(openflow->size - start);
7547 ob->weight = htons(bucket->weight);
7548 ob->watch_port = ofputil_port_to_ofp11(bucket->watch_port);
7549 ob->watch_group = htonl(bucket->watch_group);
7553 ofputil_put_ofp15_group_bucket_prop_weight(ovs_be16 weight,
7554 struct ofpbuf *openflow)
7557 struct ofp15_group_bucket_prop_weight *prop;
7559 start_ofs = start_property(openflow, OFPGBPT15_WEIGHT);
7560 ofpbuf_put_zeros(openflow, sizeof *prop - sizeof(struct ofp_prop_header));
7561 prop = ofpbuf_at_assert(openflow, start_ofs, sizeof *prop);
7562 prop->weight = weight;
7563 end_property(openflow, start_ofs);
7567 ofputil_put_ofp15_group_bucket_prop_watch(ovs_be32 watch, uint16_t type,
7568 struct ofpbuf *openflow)
7571 struct ofp15_group_bucket_prop_watch *prop;
7573 start_ofs = start_property(openflow, type);
7574 ofpbuf_put_zeros(openflow, sizeof *prop - sizeof(struct ofp_prop_header));
7575 prop = ofpbuf_at_assert(openflow, start_ofs, sizeof *prop);
7576 prop->watch = watch;
7577 end_property(openflow, start_ofs);
7581 ofputil_put_ofp15_bucket(const struct ofputil_bucket *bucket,
7582 uint32_t bucket_id, enum ofp11_group_type group_type,
7583 struct ofpbuf *openflow, enum ofp_version ofp_version)
7585 struct ofp15_bucket *ob;
7586 size_t start, actions_start, actions_len;
7588 start = openflow->size;
7589 ofpbuf_put_zeros(openflow, sizeof *ob);
7591 actions_start = openflow->size;
7592 ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
7593 openflow, ofp_version);
7594 actions_len = openflow->size - actions_start;
7596 if (group_type == OFPGT11_SELECT) {
7597 ofputil_put_ofp15_group_bucket_prop_weight(htons(bucket->weight),
7600 if (bucket->watch_port != OFPP_ANY) {
7601 ovs_be32 port = ofputil_port_to_ofp11(bucket->watch_port);
7602 ofputil_put_ofp15_group_bucket_prop_watch(port,
7603 OFPGBPT15_WATCH_PORT,
7606 if (bucket->watch_group != OFPG_ANY) {
7607 ovs_be32 group = htonl(bucket->watch_group);
7608 ofputil_put_ofp15_group_bucket_prop_watch(group,
7609 OFPGBPT15_WATCH_GROUP,
7613 ob = ofpbuf_at_assert(openflow, start, sizeof *ob);
7614 ob->len = htons(openflow->size - start);
7615 ob->action_array_len = htons(actions_len);
7616 ob->bucket_id = htonl(bucket_id);
7620 ofputil_put_group_prop_ntr_selection_method(enum ofp_version ofp_version,
7621 const struct ofputil_group_props *gp,
7622 struct ofpbuf *openflow)
7624 struct ntr_group_prop_selection_method *prop;
7627 start = openflow->size;
7628 ofpbuf_put_zeros(openflow, sizeof *prop);
7629 oxm_put_field_array(openflow, &gp->fields, ofp_version);
7630 prop = ofpbuf_at_assert(openflow, start, sizeof *prop);
7631 prop->type = htons(OFPGPT15_EXPERIMENTER);
7632 prop->experimenter = htonl(NTR_VENDOR_ID);
7633 prop->exp_type = htonl(NTRT_SELECTION_METHOD);
7634 strcpy(prop->selection_method, gp->selection_method);
7635 prop->selection_method_param = htonll(gp->selection_method_param);
7636 end_property(openflow, start);
7640 ofputil_append_ofp11_group_desc_reply(const struct ofputil_group_desc *gds,
7641 const struct ovs_list *buckets,
7642 struct ovs_list *replies,
7643 enum ofp_version version)
7645 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
7646 struct ofp11_group_desc_stats *ogds;
7647 struct ofputil_bucket *bucket;
7650 start_ogds = reply->size;
7651 ofpbuf_put_zeros(reply, sizeof *ogds);
7652 LIST_FOR_EACH (bucket, list_node, buckets) {
7653 ofputil_put_ofp11_bucket(bucket, reply, version);
7655 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
7656 ogds->length = htons(reply->size - start_ogds);
7657 ogds->type = gds->type;
7658 ogds->group_id = htonl(gds->group_id);
7660 ofpmp_postappend(replies, start_ogds);
7664 ofputil_append_ofp15_group_desc_reply(const struct ofputil_group_desc *gds,
7665 const struct ovs_list *buckets,
7666 struct ovs_list *replies,
7667 enum ofp_version version)
7669 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
7670 struct ofp15_group_desc_stats *ogds;
7671 struct ofputil_bucket *bucket;
7672 size_t start_ogds, start_buckets;
7674 start_ogds = reply->size;
7675 ofpbuf_put_zeros(reply, sizeof *ogds);
7676 start_buckets = reply->size;
7677 LIST_FOR_EACH (bucket, list_node, buckets) {
7678 ofputil_put_ofp15_bucket(bucket, bucket->bucket_id,
7679 gds->type, reply, version);
7681 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
7682 ogds->length = htons(reply->size - start_ogds);
7683 ogds->type = gds->type;
7684 ogds->group_id = htonl(gds->group_id);
7685 ogds->bucket_list_len = htons(reply->size - start_buckets);
7687 /* Add group properties */
7688 if (gds->props.selection_method[0]) {
7689 ofputil_put_group_prop_ntr_selection_method(version, &gds->props,
7693 ofpmp_postappend(replies, start_ogds);
7696 /* Appends a group stats reply that contains the data in 'gds' to those already
7697 * present in the list of ofpbufs in 'replies'. 'replies' should have been
7698 * initialized with ofpmp_init(). */
7700 ofputil_append_group_desc_reply(const struct ofputil_group_desc *gds,
7701 const struct ovs_list *buckets,
7702 struct ovs_list *replies)
7704 enum ofp_version version = ofpmp_version(replies);
7712 ofputil_append_ofp11_group_desc_reply(gds, buckets, replies, version);
7716 ofputil_append_ofp15_group_desc_reply(gds, buckets, replies, version);
7726 ofputil_pull_ofp11_buckets(struct ofpbuf *msg, size_t buckets_length,
7727 enum ofp_version version, struct ovs_list *buckets)
7729 struct ofp11_bucket *ob;
7730 uint32_t bucket_id = 0;
7733 while (buckets_length > 0) {
7734 struct ofputil_bucket *bucket;
7735 struct ofpbuf ofpacts;
7739 ob = (buckets_length >= sizeof *ob
7740 ? ofpbuf_try_pull(msg, sizeof *ob)
7743 VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE" leftover bytes",
7745 return OFPERR_OFPGMFC_BAD_BUCKET;
7748 ob_len = ntohs(ob->len);
7749 if (ob_len < sizeof *ob) {
7750 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
7751 "%"PRIuSIZE" is not valid", ob_len);
7752 return OFPERR_OFPGMFC_BAD_BUCKET;
7753 } else if (ob_len > buckets_length) {
7754 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
7755 "%"PRIuSIZE" exceeds remaining buckets data size %"PRIuSIZE,
7756 ob_len, buckets_length);
7757 return OFPERR_OFPGMFC_BAD_BUCKET;
7759 buckets_length -= ob_len;
7761 ofpbuf_init(&ofpacts, 0);
7762 error = ofpacts_pull_openflow_actions(msg, ob_len - sizeof *ob,
7765 ofpbuf_uninit(&ofpacts);
7766 ofputil_bucket_list_destroy(buckets);
7770 bucket = xzalloc(sizeof *bucket);
7771 bucket->weight = ntohs(ob->weight);
7772 error = ofputil_port_from_ofp11(ob->watch_port, &bucket->watch_port);
7774 ofpbuf_uninit(&ofpacts);
7775 ofputil_bucket_list_destroy(buckets);
7776 return OFPERR_OFPGMFC_BAD_WATCH;
7778 bucket->watch_group = ntohl(ob->watch_group);
7779 bucket->bucket_id = bucket_id++;
7781 bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
7782 bucket->ofpacts_len = ofpacts.size;
7783 list_push_back(buckets, &bucket->list_node);
7790 parse_ofp15_group_bucket_prop_weight(const struct ofpbuf *payload,
7793 struct ofp15_group_bucket_prop_weight *prop = payload->data;
7795 if (payload->size != sizeof *prop) {
7796 log_property(false, "OpenFlow bucket weight property length "
7797 "%u is not valid", payload->size);
7798 return OFPERR_OFPBPC_BAD_LEN;
7801 *weight = prop->weight;
7807 parse_ofp15_group_bucket_prop_watch(const struct ofpbuf *payload,
7810 struct ofp15_group_bucket_prop_watch *prop = payload->data;
7812 if (payload->size != sizeof *prop) {
7813 log_property(false, "OpenFlow bucket watch port or group "
7814 "property length %u is not valid", payload->size);
7815 return OFPERR_OFPBPC_BAD_LEN;
7818 *watch = prop->watch;
7824 ofputil_pull_ofp15_buckets(struct ofpbuf *msg, size_t buckets_length,
7825 enum ofp_version version, struct ovs_list *buckets)
7827 struct ofp15_bucket *ob;
7830 while (buckets_length > 0) {
7831 struct ofputil_bucket *bucket = NULL;
7832 struct ofpbuf ofpacts;
7833 enum ofperr err = OFPERR_OFPGMFC_BAD_BUCKET;
7834 struct ofpbuf properties;
7835 size_t ob_len, actions_len, properties_len;
7836 ovs_be32 watch_port = ofputil_port_to_ofp11(OFPP_ANY);
7837 ovs_be32 watch_group = htonl(OFPG_ANY);
7838 ovs_be16 weight = htons(1);
7840 ofpbuf_init(&ofpacts, 0);
7842 ob = ofpbuf_try_pull(msg, sizeof *ob);
7844 VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE
7845 " leftover bytes", buckets_length);
7849 ob_len = ntohs(ob->len);
7850 actions_len = ntohs(ob->action_array_len);
7852 if (ob_len < sizeof *ob) {
7853 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
7854 "%"PRIuSIZE" is not valid", ob_len);
7856 } else if (ob_len > buckets_length) {
7857 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
7858 "%"PRIuSIZE" exceeds remaining buckets data size %"
7859 PRIuSIZE, ob_len, buckets_length);
7861 } else if (actions_len > ob_len - sizeof *ob) {
7862 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket actions "
7863 "length %"PRIuSIZE" exceeds remaining bucket "
7864 "data size %"PRIuSIZE, actions_len,
7865 ob_len - sizeof *ob);
7868 buckets_length -= ob_len;
7870 err = ofpacts_pull_openflow_actions(msg, actions_len, version,
7876 properties_len = ob_len - sizeof *ob - actions_len;
7877 ofpbuf_use_const(&properties, ofpbuf_pull(msg, properties_len),
7880 while (properties.size > 0) {
7881 struct ofpbuf payload;
7884 err = ofputil_pull_property(&properties, &payload, &type);
7890 case OFPGBPT15_WEIGHT:
7891 err = parse_ofp15_group_bucket_prop_weight(&payload, &weight);
7894 case OFPGBPT15_WATCH_PORT:
7895 err = parse_ofp15_group_bucket_prop_watch(&payload,
7899 case OFPGBPT15_WATCH_GROUP:
7900 err = parse_ofp15_group_bucket_prop_watch(&payload,
7905 log_property(false, "unknown group bucket property %"PRIu16,
7907 err = OFPERR_OFPBPC_BAD_TYPE;
7916 bucket = xzalloc(sizeof *bucket);
7918 bucket->weight = ntohs(weight);
7919 err = ofputil_port_from_ofp11(watch_port, &bucket->watch_port);
7921 err = OFPERR_OFPGMFC_BAD_WATCH;
7924 bucket->watch_group = ntohl(watch_group);
7925 bucket->bucket_id = ntohl(ob->bucket_id);
7926 if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
7927 VLOG_WARN_RL(&bad_ofmsg_rl, "bucket id (%u) is out of range",
7929 err = OFPERR_OFPGMFC_BAD_BUCKET;
7933 bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
7934 bucket->ofpacts_len = ofpacts.size;
7935 list_push_back(buckets, &bucket->list_node);
7941 ofpbuf_uninit(&ofpacts);
7942 ofputil_bucket_list_destroy(buckets);
7946 if (ofputil_bucket_check_duplicate_id(buckets)) {
7947 VLOG_WARN_RL(&bad_ofmsg_rl, "Duplicate bucket id");
7948 ofputil_bucket_list_destroy(buckets);
7949 return OFPERR_OFPGMFC_BAD_BUCKET;
7956 ofputil_init_group_properties(struct ofputil_group_props *gp)
7958 memset(gp, 0, sizeof *gp);
7962 parse_group_prop_ntr_selection_method(struct ofpbuf *payload,
7963 enum ofp11_group_type group_type,
7964 enum ofp15_group_mod_command group_cmd,
7965 struct ofputil_group_props *gp)
7967 struct ntr_group_prop_selection_method *prop = payload->data;
7968 size_t fields_len, method_len;
7971 switch (group_type) {
7972 case OFPGT11_SELECT:
7975 case OFPGT11_INDIRECT:
7977 log_property(false, "ntr selection method property is only allowed "
7978 "for select groups");
7979 return OFPERR_OFPBPC_BAD_VALUE;
7984 switch (group_cmd) {
7986 case OFPGC15_MODIFY:
7988 case OFPGC15_DELETE:
7989 case OFPGC15_INSERT_BUCKET:
7990 case OFPGC15_REMOVE_BUCKET:
7991 log_property(false, "ntr selection method property is only allowed "
7992 "for add and delete group modifications");
7993 return OFPERR_OFPBPC_BAD_VALUE;
7998 if (payload->size < sizeof *prop) {
7999 log_property(false, "ntr selection method property length "
8000 "%u is not valid", payload->size);
8001 return OFPERR_OFPBPC_BAD_LEN;
8004 method_len = strnlen(prop->selection_method, NTR_MAX_SELECTION_METHOD_LEN);
8006 if (method_len == NTR_MAX_SELECTION_METHOD_LEN) {
8007 log_property(false, "ntr selection method is not null terminated");
8008 return OFPERR_OFPBPC_BAD_VALUE;
8011 if (strcmp("hash", prop->selection_method)) {
8012 log_property(false, "ntr selection method '%s' is not supported",
8013 prop->selection_method);
8014 return OFPERR_OFPBPC_BAD_VALUE;
8017 strcpy(gp->selection_method, prop->selection_method);
8018 gp->selection_method_param = ntohll(prop->selection_method_param);
8020 if (!method_len && gp->selection_method_param) {
8021 log_property(false, "ntr selection method parameter is non-zero but "
8022 "selection method is empty");
8023 return OFPERR_OFPBPC_BAD_VALUE;
8026 ofpbuf_pull(payload, sizeof *prop);
8028 fields_len = ntohs(prop->length) - sizeof *prop;
8029 if (!method_len && fields_len) {
8030 log_property(false, "ntr selection method parameter is zero "
8031 "but fields are provided");
8032 return OFPERR_OFPBPC_BAD_VALUE;
8035 error = oxm_pull_field_array(payload->data, fields_len,
8038 log_property(false, "ntr selection method fields are invalid");
8046 parse_group_prop_ntr(struct ofpbuf *payload, uint32_t exp_type,
8047 enum ofp11_group_type group_type,
8048 enum ofp15_group_mod_command group_cmd,
8049 struct ofputil_group_props *gp)
8054 case NTRT_SELECTION_METHOD:
8055 error = parse_group_prop_ntr_selection_method(payload, group_type,
8060 log_property(false, "unknown group property ntr experimenter type "
8061 "%"PRIu32, exp_type);
8062 error = OFPERR_OFPBPC_BAD_TYPE;
8070 parse_ofp15_group_prop_exp(struct ofpbuf *payload,
8071 enum ofp11_group_type group_type,
8072 enum ofp15_group_mod_command group_cmd,
8073 struct ofputil_group_props *gp)
8075 struct ofp_prop_experimenter *prop = payload->data;
8076 uint16_t experimenter;
8080 if (payload->size < sizeof *prop) {
8081 return OFPERR_OFPBPC_BAD_LEN;
8084 experimenter = ntohl(prop->experimenter);
8085 exp_type = ntohl(prop->exp_type);
8087 switch (experimenter) {
8089 error = parse_group_prop_ntr(payload, exp_type, group_type,
8094 log_property(false, "unknown group property experimenter %"PRIu16,
8096 error = OFPERR_OFPBPC_BAD_EXPERIMENTER;
8104 parse_ofp15_group_properties(struct ofpbuf *msg,
8105 enum ofp11_group_type group_type,
8106 enum ofp15_group_mod_command group_cmd,
8107 struct ofputil_group_props *gp,
8108 size_t properties_len)
8110 struct ofpbuf properties;
8112 ofpbuf_use_const(&properties, ofpbuf_pull(msg, properties_len),
8115 while (properties.size > 0) {
8116 struct ofpbuf payload;
8120 error = ofputil_pull_property(&properties, &payload, &type);
8126 case OFPGPT15_EXPERIMENTER:
8127 error = parse_ofp15_group_prop_exp(&payload, group_type,
8132 log_property(false, "unknown group property %"PRIu16, type);
8133 error = OFPERR_OFPBPC_BAD_TYPE;
8146 ofputil_decode_ofp11_group_desc_reply(struct ofputil_group_desc *gd,
8148 enum ofp_version version)
8150 struct ofp11_group_desc_stats *ogds;
8154 ofpraw_pull_assert(msg);
8161 ogds = ofpbuf_try_pull(msg, sizeof *ogds);
8163 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
8164 "leftover bytes at end", msg->size);
8165 return OFPERR_OFPBRC_BAD_LEN;
8167 gd->type = ogds->type;
8168 gd->group_id = ntohl(ogds->group_id);
8170 length = ntohs(ogds->length);
8171 if (length < sizeof *ogds || length - sizeof *ogds > msg->size) {
8172 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
8173 "length %"PRIuSIZE, length);
8174 return OFPERR_OFPBRC_BAD_LEN;
8177 return ofputil_pull_ofp11_buckets(msg, length - sizeof *ogds, version,
8182 ofputil_decode_ofp15_group_desc_reply(struct ofputil_group_desc *gd,
8184 enum ofp_version version)
8186 struct ofp15_group_desc_stats *ogds;
8187 uint16_t length, bucket_list_len;
8191 ofpraw_pull_assert(msg);
8198 ogds = ofpbuf_try_pull(msg, sizeof *ogds);
8200 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
8201 "leftover bytes at end", msg->size);
8202 return OFPERR_OFPBRC_BAD_LEN;
8204 gd->type = ogds->type;
8205 gd->group_id = ntohl(ogds->group_id);
8207 length = ntohs(ogds->length);
8208 if (length < sizeof *ogds || length - sizeof *ogds > msg->size) {
8209 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
8210 "length %u", length);
8211 return OFPERR_OFPBRC_BAD_LEN;
8214 bucket_list_len = ntohs(ogds->bucket_list_len);
8215 if (length < bucket_list_len + sizeof *ogds) {
8216 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
8217 "bucket list length %u", bucket_list_len);
8218 return OFPERR_OFPBRC_BAD_LEN;
8220 error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, version,
8226 /* By definition group desc messages don't have a group mod command.
8227 * However, parse_group_prop_ntr_selection_method() checks to make sure
8228 * that the command is OFPGC15_ADD or OFPGC15_DELETE to guard
8229 * against group mod messages with other commands supplying
8230 * a NTR selection method group experimenter property.
8231 * Such properties are valid for group desc replies so
8232 * claim that the group mod command is OFPGC15_ADD to
8233 * satisfy the check in parse_group_prop_ntr_selection_method() */
8234 return parse_ofp15_group_properties(msg, gd->type, OFPGC15_ADD, &gd->props,
8238 /* Converts a group description reply in 'msg' into an abstract
8239 * ofputil_group_desc in 'gd'.
8241 * Multiple group description replies can be packed into a single OpenFlow
8242 * message. Calling this function multiple times for a single 'msg' iterates
8243 * through the replies. The caller must initially leave 'msg''s layer pointers
8244 * null and not modify them between calls.
8246 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8247 * otherwise a positive errno value. */
8249 ofputil_decode_group_desc_reply(struct ofputil_group_desc *gd,
8250 struct ofpbuf *msg, enum ofp_version version)
8252 ofputil_init_group_properties(&gd->props);
8260 return ofputil_decode_ofp11_group_desc_reply(gd, msg, version);
8263 return ofputil_decode_ofp15_group_desc_reply(gd, msg, version);
8272 ofputil_uninit_group_mod(struct ofputil_group_mod *gm)
8274 ofputil_bucket_list_destroy(&gm->buckets);
8277 static struct ofpbuf *
8278 ofputil_encode_ofp11_group_mod(enum ofp_version ofp_version,
8279 const struct ofputil_group_mod *gm)
8282 struct ofp11_group_mod *ogm;
8284 struct ofputil_bucket *bucket;
8286 b = ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD, ofp_version, 0);
8287 start_ogm = b->size;
8288 ofpbuf_put_zeros(b, sizeof *ogm);
8290 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
8291 ofputil_put_ofp11_bucket(bucket, b, ofp_version);
8293 ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
8294 ogm->command = htons(gm->command);
8295 ogm->type = gm->type;
8296 ogm->group_id = htonl(gm->group_id);
8301 static struct ofpbuf *
8302 ofputil_encode_ofp15_group_mod(enum ofp_version ofp_version,
8303 const struct ofputil_group_mod *gm)
8306 struct ofp15_group_mod *ogm;
8308 struct ofputil_bucket *bucket;
8309 struct id_pool *bucket_ids = NULL;
8311 b = ofpraw_alloc(OFPRAW_OFPT15_GROUP_MOD, ofp_version, 0);
8312 start_ogm = b->size;
8313 ofpbuf_put_zeros(b, sizeof *ogm);
8315 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
8318 /* Generate a bucket id if none was supplied */
8319 if (bucket->bucket_id > OFPG15_BUCKET_MAX) {
8321 const struct ofputil_bucket *bkt;
8323 bucket_ids = id_pool_create(0, OFPG15_BUCKET_MAX + 1);
8325 /* Mark all bucket_ids that are present in gm
8326 * as used in the pool. */
8327 LIST_FOR_EACH_REVERSE (bkt, list_node, &gm->buckets) {
8328 if (bkt == bucket) {
8331 if (bkt->bucket_id <= OFPG15_BUCKET_MAX) {
8332 id_pool_add(bucket_ids, bkt->bucket_id);
8337 if (!id_pool_alloc_id(bucket_ids, &bucket_id)) {
8341 bucket_id = bucket->bucket_id;
8344 ofputil_put_ofp15_bucket(bucket, bucket_id, gm->type, b, ofp_version);
8346 ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
8347 ogm->command = htons(gm->command);
8348 ogm->type = gm->type;
8349 ogm->group_id = htonl(gm->group_id);
8350 ogm->command_bucket_id = htonl(gm->command_bucket_id);
8351 ogm->bucket_array_len = htons(b->size - start_ogm - sizeof *ogm);
8353 /* Add group properties */
8354 if (gm->props.selection_method[0]) {
8355 ofputil_put_group_prop_ntr_selection_method(ofp_version, &gm->props, b);
8358 id_pool_destroy(bucket_ids);
8363 bad_group_cmd(enum ofp15_group_mod_command cmd)
8365 const char *opt_version;
8366 const char *version;
8367 const char *cmd_str;
8371 case OFPGC15_MODIFY:
8372 case OFPGC15_DELETE:
8377 case OFPGC15_INSERT_BUCKET:
8378 case OFPGC15_REMOVE_BUCKET:
8389 cmd_str = "add-group";
8392 case OFPGC15_MODIFY:
8393 cmd_str = "mod-group";
8396 case OFPGC15_DELETE:
8397 cmd_str = "del-group";
8400 case OFPGC15_INSERT_BUCKET:
8401 cmd_str = "insert-bucket";
8404 case OFPGC15_REMOVE_BUCKET:
8405 cmd_str = "remove-bucket";
8412 ovs_fatal(0, "%s needs OpenFlow %s or later (\'-O OpenFlow%s\')",
8413 cmd_str, version, opt_version);
8417 /* Converts abstract group mod 'gm' into a message for OpenFlow version
8418 * 'ofp_version' and returns the message. */
8420 ofputil_encode_group_mod(enum ofp_version ofp_version,
8421 const struct ofputil_group_mod *gm)
8424 switch (ofp_version) {
8426 bad_group_cmd(gm->command);
8432 if (gm->command > OFPGC11_DELETE) {
8433 bad_group_cmd(gm->command);
8435 return ofputil_encode_ofp11_group_mod(ofp_version, gm);
8438 return ofputil_encode_ofp15_group_mod(ofp_version, gm);
8446 ofputil_pull_ofp11_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
8447 struct ofputil_group_mod *gm)
8449 const struct ofp11_group_mod *ogm;
8452 ogm = ofpbuf_pull(msg, sizeof *ogm);
8453 gm->command = ntohs(ogm->command);
8454 gm->type = ogm->type;
8455 gm->group_id = ntohl(ogm->group_id);
8456 gm->command_bucket_id = OFPG15_BUCKET_ALL;
8458 error = ofputil_pull_ofp11_buckets(msg, msg->size, ofp_version,
8461 /* OF1.3.5+ prescribes an error when an OFPGC_DELETE includes buckets. */
8463 && ofp_version >= OFP13_VERSION
8464 && gm->command == OFPGC11_DELETE
8465 && !list_is_empty(&gm->buckets)) {
8466 error = OFPERR_OFPGMFC_INVALID_GROUP;
8473 ofputil_pull_ofp15_group_mod(struct ofpbuf *msg, enum ofp_version ofp_version,
8474 struct ofputil_group_mod *gm)
8476 const struct ofp15_group_mod *ogm;
8477 uint16_t bucket_list_len;
8478 enum ofperr error = OFPERR_OFPGMFC_BAD_BUCKET;
8480 ogm = ofpbuf_pull(msg, sizeof *ogm);
8481 gm->command = ntohs(ogm->command);
8482 gm->type = ogm->type;
8483 gm->group_id = ntohl(ogm->group_id);
8485 gm->command_bucket_id = ntohl(ogm->command_bucket_id);
8486 switch (gm->command) {
8487 case OFPGC15_REMOVE_BUCKET:
8488 if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
8492 case OFPGC15_INSERT_BUCKET:
8493 if (gm->command_bucket_id <= OFPG15_BUCKET_MAX ||
8494 gm->command_bucket_id == OFPG15_BUCKET_FIRST
8495 || gm->command_bucket_id == OFPG15_BUCKET_LAST) {
8501 case OFPGC11_MODIFY:
8502 case OFPGC11_DELETE:
8504 if (gm->command_bucket_id == OFPG15_BUCKET_ALL) {
8510 VLOG_WARN_RL(&bad_ofmsg_rl,
8511 "group command bucket id (%u) is out of range",
8512 gm->command_bucket_id);
8513 return OFPERR_OFPGMFC_BAD_BUCKET;
8516 bucket_list_len = ntohs(ogm->bucket_array_len);
8517 error = ofputil_pull_ofp15_buckets(msg, bucket_list_len, ofp_version,
8523 return parse_ofp15_group_properties(msg, gm->type, gm->command, &gm->props,
8527 /* Converts OpenFlow group mod message 'oh' into an abstract group mod in
8528 * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */
8530 ofputil_decode_group_mod(const struct ofp_header *oh,
8531 struct ofputil_group_mod *gm)
8533 enum ofp_version ofp_version = oh->version;
8535 struct ofputil_bucket *bucket;
8538 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
8539 ofpraw_pull_assert(&msg);
8541 ofputil_init_group_properties(&gm->props);
8543 switch (ofp_version)
8549 err = ofputil_pull_ofp11_group_mod(&msg, ofp_version, gm);
8553 err = ofputil_pull_ofp15_group_mod(&msg, ofp_version, gm);
8566 case OFPGT11_INDIRECT:
8567 if (!list_is_singleton(&gm->buckets)) {
8568 return OFPERR_OFPGMFC_OUT_OF_BUCKETS;
8572 case OFPGT11_SELECT:
8579 switch (gm->command) {
8581 case OFPGC11_MODIFY:
8582 case OFPGC11_DELETE:
8583 case OFPGC15_INSERT_BUCKET:
8585 case OFPGC15_REMOVE_BUCKET:
8586 if (!list_is_empty(&gm->buckets)) {
8587 return OFPERR_OFPGMFC_BAD_BUCKET;
8594 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
8597 case OFPGT11_INDIRECT:
8598 if (ofputil_bucket_has_liveness(bucket)) {
8599 return OFPERR_OFPGMFC_WATCH_UNSUPPORTED;
8602 case OFPGT11_SELECT:
8605 if (!ofputil_bucket_has_liveness(bucket)) {
8606 return OFPERR_OFPGMFC_INVALID_GROUP;
8617 /* Parse a queue status request message into 'oqsr'.
8618 * Returns 0 if successful, otherwise an OFPERR_* number. */
8620 ofputil_decode_queue_stats_request(const struct ofp_header *request,
8621 struct ofputil_queue_stats_request *oqsr)
8623 switch ((enum ofp_version)request->version) {
8628 case OFP11_VERSION: {
8629 const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request);
8630 oqsr->queue_id = ntohl(qsr11->queue_id);
8631 return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no);
8634 case OFP10_VERSION: {
8635 const struct ofp10_queue_stats_request *qsr10 = ofpmsg_body(request);
8636 oqsr->queue_id = ntohl(qsr10->queue_id);
8637 oqsr->port_no = u16_to_ofp(ntohs(qsr10->port_no));
8638 /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
8639 if (oqsr->port_no == OFPP_ALL) {
8640 oqsr->port_no = OFPP_ANY;
8650 /* Encode a queue stats request for 'oqsr', the encoded message
8651 * will be for OpenFlow version 'ofp_version'. Returns message
8652 * as a struct ofpbuf. Returns encoded message on success, NULL on error. */
8654 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
8655 const struct ofputil_queue_stats_request *oqsr)
8657 struct ofpbuf *request;
8659 switch (ofp_version) {
8664 case OFP15_VERSION: {
8665 struct ofp11_queue_stats_request *req;
8666 request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0);
8667 req = ofpbuf_put_zeros(request, sizeof *req);
8668 req->port_no = ofputil_port_to_ofp11(oqsr->port_no);
8669 req->queue_id = htonl(oqsr->queue_id);
8672 case OFP10_VERSION: {
8673 struct ofp10_queue_stats_request *req;
8674 request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0);
8675 req = ofpbuf_put_zeros(request, sizeof *req);
8676 /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
8677 req->port_no = htons(ofp_to_u16(oqsr->port_no == OFPP_ANY
8678 ? OFPP_ALL : oqsr->port_no));
8679 req->queue_id = htonl(oqsr->queue_id);
8689 /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
8692 ofputil_count_queue_stats(const struct ofp_header *oh)
8694 struct ofputil_queue_stats qs;
8698 ofpbuf_use_const(&b, oh, ntohs(oh->length));
8699 ofpraw_pull_assert(&b);
8700 while (!ofputil_decode_queue_stats(&qs, &b)) {
8707 ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs,
8708 const struct ofp10_queue_stats *qs10)
8710 oqs->port_no = u16_to_ofp(ntohs(qs10->port_no));
8711 oqs->queue_id = ntohl(qs10->queue_id);
8712 oqs->tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes));
8713 oqs->tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets));
8714 oqs->tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors));
8715 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
8721 ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs,
8722 const struct ofp11_queue_stats *qs11)
8726 error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no);
8731 oqs->queue_id = ntohl(qs11->queue_id);
8732 oqs->tx_bytes = ntohll(qs11->tx_bytes);
8733 oqs->tx_packets = ntohll(qs11->tx_packets);
8734 oqs->tx_errors = ntohll(qs11->tx_errors);
8735 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
8741 ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats *oqs,
8742 const struct ofp13_queue_stats *qs13)
8744 enum ofperr error = ofputil_queue_stats_from_ofp11(oqs, &qs13->qs);
8746 oqs->duration_sec = ntohl(qs13->duration_sec);
8747 oqs->duration_nsec = ntohl(qs13->duration_nsec);
8754 ofputil_pull_ofp14_queue_stats(struct ofputil_queue_stats *oqs,
8757 const struct ofp14_queue_stats *qs14;
8760 qs14 = ofpbuf_try_pull(msg, sizeof *qs14);
8762 return OFPERR_OFPBRC_BAD_LEN;
8765 len = ntohs(qs14->length);
8766 if (len < sizeof *qs14 || len - sizeof *qs14 > msg->size) {
8767 return OFPERR_OFPBRC_BAD_LEN;
8769 ofpbuf_pull(msg, len - sizeof *qs14);
8771 /* No properties yet defined, so ignore them for now. */
8773 return ofputil_queue_stats_from_ofp13(oqs, &qs14->qs);
8776 /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
8777 * ofputil_queue_stats in 'qs'.
8779 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
8780 * message. Calling this function multiple times for a single 'msg' iterates
8781 * through the replies. The caller must initially leave 'msg''s layer pointers
8782 * null and not modify them between calls.
8784 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8785 * otherwise a positive errno value. */
8787 ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg)
8792 error = (msg->header ? ofpraw_decode(&raw, msg->header)
8793 : ofpraw_pull(&raw, msg));
8800 } else if (raw == OFPRAW_OFPST14_QUEUE_REPLY) {
8801 return ofputil_pull_ofp14_queue_stats(qs, msg);
8802 } else if (raw == OFPRAW_OFPST13_QUEUE_REPLY) {
8803 const struct ofp13_queue_stats *qs13;
8805 qs13 = ofpbuf_try_pull(msg, sizeof *qs13);
8809 return ofputil_queue_stats_from_ofp13(qs, qs13);
8810 } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) {
8811 const struct ofp11_queue_stats *qs11;
8813 qs11 = ofpbuf_try_pull(msg, sizeof *qs11);
8817 return ofputil_queue_stats_from_ofp11(qs, qs11);
8818 } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) {
8819 const struct ofp10_queue_stats *qs10;
8821 qs10 = ofpbuf_try_pull(msg, sizeof *qs10);
8825 return ofputil_queue_stats_from_ofp10(qs, qs10);
8831 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %"PRIu32" leftover "
8832 "bytes at end", msg->size);
8833 return OFPERR_OFPBRC_BAD_LEN;
8837 ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs,
8838 struct ofp10_queue_stats *qs10)
8840 qs10->port_no = htons(ofp_to_u16(oqs->port_no));
8841 memset(qs10->pad, 0, sizeof qs10->pad);
8842 qs10->queue_id = htonl(oqs->queue_id);
8843 put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->tx_bytes));
8844 put_32aligned_be64(&qs10->tx_packets, htonll(oqs->tx_packets));
8845 put_32aligned_be64(&qs10->tx_errors, htonll(oqs->tx_errors));
8849 ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs,
8850 struct ofp11_queue_stats *qs11)
8852 qs11->port_no = ofputil_port_to_ofp11(oqs->port_no);
8853 qs11->queue_id = htonl(oqs->queue_id);
8854 qs11->tx_bytes = htonll(oqs->tx_bytes);
8855 qs11->tx_packets = htonll(oqs->tx_packets);
8856 qs11->tx_errors = htonll(oqs->tx_errors);
8860 ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats *oqs,
8861 struct ofp13_queue_stats *qs13)
8863 ofputil_queue_stats_to_ofp11(oqs, &qs13->qs);
8864 if (oqs->duration_sec != UINT32_MAX) {
8865 qs13->duration_sec = htonl(oqs->duration_sec);
8866 qs13->duration_nsec = htonl(oqs->duration_nsec);
8868 qs13->duration_sec = OVS_BE32_MAX;
8869 qs13->duration_nsec = OVS_BE32_MAX;
8874 ofputil_queue_stats_to_ofp14(const struct ofputil_queue_stats *oqs,
8875 struct ofp14_queue_stats *qs14)
8877 qs14->length = htons(sizeof *qs14);
8878 memset(qs14->pad, 0, sizeof qs14->pad);
8879 ofputil_queue_stats_to_ofp13(oqs, &qs14->qs);
8883 /* Encode a queue stat for 'oqs' and append it to 'replies'. */
8885 ofputil_append_queue_stat(struct ovs_list *replies,
8886 const struct ofputil_queue_stats *oqs)
8888 switch (ofpmp_version(replies)) {
8889 case OFP13_VERSION: {
8890 struct ofp13_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
8891 ofputil_queue_stats_to_ofp13(oqs, reply);
8896 case OFP11_VERSION: {
8897 struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
8898 ofputil_queue_stats_to_ofp11(oqs, reply);
8902 case OFP10_VERSION: {
8903 struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
8904 ofputil_queue_stats_to_ofp10(oqs, reply);
8909 case OFP15_VERSION: {
8910 struct ofp14_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
8911 ofputil_queue_stats_to_ofp14(oqs, reply);
8921 ofputil_decode_bundle_ctrl(const struct ofp_header *oh,
8922 struct ofputil_bundle_ctrl_msg *msg)
8926 const struct ofp14_bundle_ctrl_msg *m;
8928 ofpbuf_use_const(&b, oh, ntohs(oh->length));
8929 raw = ofpraw_pull_assert(&b);
8930 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_CONTROL);
8933 msg->bundle_id = ntohl(m->bundle_id);
8934 msg->type = ntohs(m->type);
8935 msg->flags = ntohs(m->flags);
8941 ofputil_encode_bundle_ctrl_request(enum ofp_version ofp_version,
8942 struct ofputil_bundle_ctrl_msg *bc)
8944 struct ofpbuf *request;
8945 struct ofp14_bundle_ctrl_msg *m;
8947 switch (ofp_version) {
8952 ovs_fatal(0, "bundles need OpenFlow 1.4 or later "
8953 "(\'-O OpenFlow14\')");
8956 request = ofpraw_alloc(OFPRAW_OFPT14_BUNDLE_CONTROL, ofp_version, 0);
8957 m = ofpbuf_put_zeros(request, sizeof *m);
8959 m->bundle_id = htonl(bc->bundle_id);
8960 m->type = htons(bc->type);
8961 m->flags = htons(bc->flags);
8971 ofputil_encode_bundle_ctrl_reply(const struct ofp_header *oh,
8972 struct ofputil_bundle_ctrl_msg *msg)
8975 struct ofp14_bundle_ctrl_msg *m;
8977 buf = ofpraw_alloc_reply(OFPRAW_OFPT14_BUNDLE_CONTROL, oh, 0);
8978 m = ofpbuf_put_zeros(buf, sizeof *m);
8980 m->bundle_id = htonl(msg->bundle_id);
8981 m->type = htons(msg->type);
8982 m->flags = htons(msg->flags);
8987 /* Return true for bundlable state change requests, false for other messages.
8990 ofputil_is_bundlable(enum ofptype type)
8993 /* Minimum required by OpenFlow 1.4. */
8994 case OFPTYPE_PORT_MOD:
8995 case OFPTYPE_FLOW_MOD:
8998 /* Nice to have later. */
8999 case OFPTYPE_FLOW_MOD_TABLE_ID:
9000 case OFPTYPE_GROUP_MOD:
9001 case OFPTYPE_TABLE_MOD:
9002 case OFPTYPE_METER_MOD:
9003 case OFPTYPE_PACKET_OUT:
9004 case OFPTYPE_NXT_GENEVE_TABLE_MOD:
9006 /* Not to be bundlable. */
9007 case OFPTYPE_ECHO_REQUEST:
9008 case OFPTYPE_FEATURES_REQUEST:
9009 case OFPTYPE_GET_CONFIG_REQUEST:
9010 case OFPTYPE_SET_CONFIG:
9011 case OFPTYPE_BARRIER_REQUEST:
9012 case OFPTYPE_ROLE_REQUEST:
9013 case OFPTYPE_ECHO_REPLY:
9014 case OFPTYPE_SET_FLOW_FORMAT:
9015 case OFPTYPE_SET_PACKET_IN_FORMAT:
9016 case OFPTYPE_SET_CONTROLLER_ID:
9017 case OFPTYPE_FLOW_AGE:
9018 case OFPTYPE_FLOW_MONITOR_CANCEL:
9019 case OFPTYPE_SET_ASYNC_CONFIG:
9020 case OFPTYPE_GET_ASYNC_REQUEST:
9021 case OFPTYPE_DESC_STATS_REQUEST:
9022 case OFPTYPE_FLOW_STATS_REQUEST:
9023 case OFPTYPE_AGGREGATE_STATS_REQUEST:
9024 case OFPTYPE_TABLE_STATS_REQUEST:
9025 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
9026 case OFPTYPE_TABLE_DESC_REQUEST:
9027 case OFPTYPE_PORT_STATS_REQUEST:
9028 case OFPTYPE_QUEUE_STATS_REQUEST:
9029 case OFPTYPE_PORT_DESC_STATS_REQUEST:
9030 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
9031 case OFPTYPE_METER_STATS_REQUEST:
9032 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
9033 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
9034 case OFPTYPE_GROUP_STATS_REQUEST:
9035 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
9036 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
9037 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
9038 case OFPTYPE_BUNDLE_CONTROL:
9039 case OFPTYPE_BUNDLE_ADD_MESSAGE:
9042 case OFPTYPE_FEATURES_REPLY:
9043 case OFPTYPE_GET_CONFIG_REPLY:
9044 case OFPTYPE_PACKET_IN:
9045 case OFPTYPE_FLOW_REMOVED:
9046 case OFPTYPE_PORT_STATUS:
9047 case OFPTYPE_BARRIER_REPLY:
9048 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
9049 case OFPTYPE_DESC_STATS_REPLY:
9050 case OFPTYPE_FLOW_STATS_REPLY:
9051 case OFPTYPE_QUEUE_STATS_REPLY:
9052 case OFPTYPE_PORT_STATS_REPLY:
9053 case OFPTYPE_TABLE_STATS_REPLY:
9054 case OFPTYPE_AGGREGATE_STATS_REPLY:
9055 case OFPTYPE_PORT_DESC_STATS_REPLY:
9056 case OFPTYPE_ROLE_REPLY:
9057 case OFPTYPE_FLOW_MONITOR_PAUSED:
9058 case OFPTYPE_FLOW_MONITOR_RESUMED:
9059 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
9060 case OFPTYPE_GET_ASYNC_REPLY:
9061 case OFPTYPE_GROUP_STATS_REPLY:
9062 case OFPTYPE_GROUP_DESC_STATS_REPLY:
9063 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
9064 case OFPTYPE_METER_STATS_REPLY:
9065 case OFPTYPE_METER_CONFIG_STATS_REPLY:
9066 case OFPTYPE_METER_FEATURES_STATS_REPLY:
9067 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
9068 case OFPTYPE_TABLE_DESC_REPLY:
9069 case OFPTYPE_ROLE_STATUS:
9070 case OFPTYPE_NXT_GENEVE_TABLE_REQUEST:
9071 case OFPTYPE_NXT_GENEVE_TABLE_REPLY:
9079 ofputil_decode_bundle_add(const struct ofp_header *oh,
9080 struct ofputil_bundle_add_msg *msg,
9081 enum ofptype *type_ptr)
9083 const struct ofp14_bundle_ctrl_msg *m;
9090 ofpbuf_use_const(&b, oh, ntohs(oh->length));
9091 raw = ofpraw_pull_assert(&b);
9092 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE);
9094 m = ofpbuf_pull(&b, sizeof *m);
9095 msg->bundle_id = ntohl(m->bundle_id);
9096 msg->flags = ntohs(m->flags);
9099 if (msg->msg->version != oh->version) {
9100 return OFPERR_NXBFC_BAD_VERSION;
9102 inner_len = ntohs(msg->msg->length);
9103 if (inner_len < sizeof(struct ofp_header) || inner_len > b.size) {
9104 return OFPERR_OFPBFC_MSG_BAD_LEN;
9106 if (msg->msg->xid != oh->xid) {
9107 return OFPERR_OFPBFC_MSG_BAD_XID;
9110 /* Reject unbundlable messages. */
9114 error = ofptype_decode(type_ptr, msg->msg);
9116 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPT14_BUNDLE_ADD_MESSAGE contained "
9117 "message is unparsable (%s)", ofperr_get_name(error));
9118 return OFPERR_OFPBFC_MSG_UNSUP; /* 'error' would be confusing. */
9121 if (!ofputil_is_bundlable(*type_ptr)) {
9122 VLOG_WARN_RL(&bad_ofmsg_rl, "%s message not allowed inside "
9123 "OFPT14_BUNDLE_ADD_MESSAGE", ofptype_get_name(*type_ptr));
9124 return OFPERR_OFPBFC_MSG_UNSUP;
9131 ofputil_encode_bundle_add(enum ofp_version ofp_version,
9132 struct ofputil_bundle_add_msg *msg)
9134 struct ofpbuf *request;
9135 struct ofp14_bundle_ctrl_msg *m;
9137 /* Must use the same xid as the embedded message. */
9138 request = ofpraw_alloc_xid(OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE, ofp_version,
9140 m = ofpbuf_put_zeros(request, sizeof *m);
9142 m->bundle_id = htonl(msg->bundle_id);
9143 m->flags = htons(msg->flags);
9144 ofpbuf_put(request, msg->msg, ntohs(msg->msg->length));
9150 encode_geneve_table_mappings(struct ofpbuf *b, struct ovs_list *mappings)
9152 struct ofputil_geneve_map *map;
9154 LIST_FOR_EACH (map, list_node, mappings) {
9155 struct nx_geneve_map *nx_map;
9157 nx_map = ofpbuf_put_zeros(b, sizeof *nx_map);
9158 nx_map->option_class = htons(map->option_class);
9159 nx_map->option_type = map->option_type;
9160 nx_map->option_len = map->option_len;
9161 nx_map->index = htons(map->index);
9166 ofputil_encode_geneve_table_mod(enum ofp_version ofp_version,
9167 struct ofputil_geneve_table_mod *gtm)
9170 struct nx_geneve_table_mod *nx_gtm;
9172 b = ofpraw_alloc(OFPRAW_NXT_GENEVE_TABLE_MOD, ofp_version, 0);
9173 nx_gtm = ofpbuf_put_zeros(b, sizeof *nx_gtm);
9174 nx_gtm->command = htons(gtm->command);
9175 encode_geneve_table_mappings(b, >m->mappings);
9181 decode_geneve_table_mappings(struct ofpbuf *msg, struct ovs_list *mappings)
9183 list_init(mappings);
9186 struct nx_geneve_map *nx_map;
9187 struct ofputil_geneve_map *map;
9189 nx_map = ofpbuf_pull(msg, sizeof *nx_map);
9190 map = xmalloc(sizeof *map);
9191 list_push_back(mappings, &map->list_node);
9193 map->option_class = ntohs(nx_map->option_class);
9194 map->option_type = nx_map->option_type;
9196 map->option_len = nx_map->option_len;
9197 if (map->option_len == 0 || map->option_len % 4 ||
9198 map->option_len > GENEVE_MAX_OPT_SIZE) {
9199 VLOG_WARN_RL(&bad_ofmsg_rl,
9200 "geneve table option length (%u) is not a valid option size",
9202 ofputil_uninit_geneve_table(mappings);
9203 return OFPERR_NXGTMFC_BAD_OPT_LEN;
9206 map->index = ntohs(nx_map->index);
9207 if (map->index >= TUN_METADATA_NUM_OPTS) {
9208 VLOG_WARN_RL(&bad_ofmsg_rl,
9209 "geneve table field index (%u) is too large (max %u)",
9210 map->index, TUN_METADATA_NUM_OPTS - 1);
9211 ofputil_uninit_geneve_table(mappings);
9212 return OFPERR_NXGTMFC_BAD_FIELD_IDX;
9220 ofputil_decode_geneve_table_mod(const struct ofp_header *oh,
9221 struct ofputil_geneve_table_mod *gtm)
9224 struct nx_geneve_table_mod *nx_gtm;
9226 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
9227 ofpraw_pull_assert(&msg);
9229 nx_gtm = ofpbuf_pull(&msg, sizeof *nx_gtm);
9230 gtm->command = ntohs(nx_gtm->command);
9231 if (gtm->command > NXGTMC_CLEAR) {
9232 VLOG_WARN_RL(&bad_ofmsg_rl,
9233 "geneve table mod command (%u) is out of range",
9235 return OFPERR_NXGTMFC_BAD_COMMAND;
9238 return decode_geneve_table_mappings(&msg, >m->mappings);
9242 ofputil_encode_geneve_table_reply(const struct ofp_header *oh,
9243 struct ofputil_geneve_table_reply *gtr)
9246 struct nx_geneve_table_reply *nx_gtr;
9248 b = ofpraw_alloc_reply(OFPRAW_NXT_GENEVE_TABLE_REPLY, oh, 0);
9249 nx_gtr = ofpbuf_put_zeros(b, sizeof *nx_gtr);
9250 nx_gtr->max_option_space = htonl(gtr->max_option_space);
9251 nx_gtr->max_fields = htons(gtr->max_fields);
9253 encode_geneve_table_mappings(b, >r->mappings);
9259 ofputil_decode_geneve_table_reply(const struct ofp_header *oh,
9260 struct ofputil_geneve_table_reply *gtr)
9263 struct nx_geneve_table_reply *nx_gtr;
9265 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
9266 ofpraw_pull_assert(&msg);
9268 nx_gtr = ofpbuf_pull(&msg, sizeof *nx_gtr);
9269 gtr->max_option_space = ntohl(nx_gtr->max_option_space);
9270 gtr->max_fields = ntohs(nx_gtr->max_fields);
9272 return decode_geneve_table_mappings(&msg, >r->mappings);
9276 ofputil_uninit_geneve_table(struct ovs_list *mappings)
9278 struct ofputil_geneve_map *map;
9280 LIST_FOR_EACH_POP (map, list_node, mappings) {