2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 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"
35 #include "ofp-actions.h"
36 #include "ofp-errors.h"
42 #include "unaligned.h"
43 #include "type-props.h"
47 VLOG_DEFINE_THIS_MODULE(ofp_util);
49 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
50 * in the peer and so there's not much point in showing a lot of them. */
51 static struct vlog_rate_limit bad_ofmsg_rl = VLOG_RATE_LIMIT_INIT(1, 5);
53 struct ofp_prop_header {
58 /* Pulls a property, beginning with struct ofp_prop_header, from the beginning
59 * of 'msg'. Stores the type of the property in '*typep' and, if 'property' is
60 * nonnull, the entire property, including the header, in '*property'. Returns
61 * 0 if successful, otherwise an error code. */
63 ofputil_pull_property(struct ofpbuf *msg, struct ofpbuf *property,
66 struct ofp_prop_header *oph;
69 if (ofpbuf_size(msg) < sizeof *oph) {
70 return OFPERR_OFPBPC_BAD_LEN;
73 oph = ofpbuf_data(msg);
74 len = ntohs(oph->len);
75 if (len < sizeof *oph || ROUND_UP(len, 8) > ofpbuf_size(msg)) {
76 return OFPERR_OFPBPC_BAD_LEN;
79 *typep = ntohs(oph->type);
81 ofpbuf_use_const(property, ofpbuf_data(msg), len);
83 ofpbuf_pull(msg, ROUND_UP(len, 8));
88 static void PRINTF_FORMAT(2, 3)
89 log_property(bool loose, const char *message, ...)
91 enum vlog_level level = loose ? VLL_DBG : VLL_WARN;
92 if (!vlog_should_drop(THIS_MODULE, level, &bad_ofmsg_rl)) {
95 va_start(args, message);
96 vlog_valist(THIS_MODULE, level, message, args);
100 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
101 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
104 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
105 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
106 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
107 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
110 ofputil_wcbits_to_netmask(int wcbits)
113 return wcbits < 32 ? htonl(~((1u << wcbits) - 1)) : 0;
116 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
117 * that it wildcards, that is, the number of 0-bits in 'netmask', a number
118 * between 0 and 32 inclusive.
120 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
121 * still be in the valid range but isn't otherwise meaningful. */
123 ofputil_netmask_to_wcbits(ovs_be32 netmask)
125 return 32 - ip_count_cidr_bits(netmask);
128 /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
129 * flow_wildcards in 'wc' for use in struct match. It is the caller's
130 * responsibility to handle the special case where the flow match's dl_vlan is
131 * set to OFP_VLAN_NONE. */
133 ofputil_wildcard_from_ofpfw10(uint32_t ofpfw, struct flow_wildcards *wc)
135 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 26);
137 /* Initialize most of wc. */
138 flow_wildcards_init_catchall(wc);
140 if (!(ofpfw & OFPFW10_IN_PORT)) {
141 wc->masks.in_port.ofp_port = u16_to_ofp(UINT16_MAX);
144 if (!(ofpfw & OFPFW10_NW_TOS)) {
145 wc->masks.nw_tos |= IP_DSCP_MASK;
148 if (!(ofpfw & OFPFW10_NW_PROTO)) {
149 wc->masks.nw_proto = UINT8_MAX;
151 wc->masks.nw_src = ofputil_wcbits_to_netmask(ofpfw
152 >> OFPFW10_NW_SRC_SHIFT);
153 wc->masks.nw_dst = ofputil_wcbits_to_netmask(ofpfw
154 >> OFPFW10_NW_DST_SHIFT);
156 if (!(ofpfw & OFPFW10_TP_SRC)) {
157 wc->masks.tp_src = OVS_BE16_MAX;
159 if (!(ofpfw & OFPFW10_TP_DST)) {
160 wc->masks.tp_dst = OVS_BE16_MAX;
163 if (!(ofpfw & OFPFW10_DL_SRC)) {
164 memset(wc->masks.dl_src, 0xff, ETH_ADDR_LEN);
166 if (!(ofpfw & OFPFW10_DL_DST)) {
167 memset(wc->masks.dl_dst, 0xff, ETH_ADDR_LEN);
169 if (!(ofpfw & OFPFW10_DL_TYPE)) {
170 wc->masks.dl_type = OVS_BE16_MAX;
174 if (!(ofpfw & OFPFW10_DL_VLAN_PCP)) {
175 wc->masks.vlan_tci |= htons(VLAN_PCP_MASK | VLAN_CFI);
177 if (!(ofpfw & OFPFW10_DL_VLAN)) {
178 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
182 /* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */
184 ofputil_match_from_ofp10_match(const struct ofp10_match *ofmatch,
187 uint32_t ofpfw = ntohl(ofmatch->wildcards) & OFPFW10_ALL;
189 /* Initialize match->wc. */
190 memset(&match->flow, 0, sizeof match->flow);
191 ofputil_wildcard_from_ofpfw10(ofpfw, &match->wc);
193 /* Initialize most of match->flow. */
194 match->flow.nw_src = ofmatch->nw_src;
195 match->flow.nw_dst = ofmatch->nw_dst;
196 match->flow.in_port.ofp_port = u16_to_ofp(ntohs(ofmatch->in_port));
197 match->flow.dl_type = ofputil_dl_type_from_openflow(ofmatch->dl_type);
198 match->flow.tp_src = ofmatch->tp_src;
199 match->flow.tp_dst = ofmatch->tp_dst;
200 memcpy(match->flow.dl_src, ofmatch->dl_src, ETH_ADDR_LEN);
201 memcpy(match->flow.dl_dst, ofmatch->dl_dst, ETH_ADDR_LEN);
202 match->flow.nw_tos = ofmatch->nw_tos & IP_DSCP_MASK;
203 match->flow.nw_proto = ofmatch->nw_proto;
205 /* Translate VLANs. */
206 if (!(ofpfw & OFPFW10_DL_VLAN) &&
207 ofmatch->dl_vlan == htons(OFP10_VLAN_NONE)) {
208 /* Match only packets without 802.1Q header.
210 * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
212 * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
213 * because we can't have a specific PCP without an 802.1Q header.
214 * However, older versions of OVS treated this as matching packets
215 * withut an 802.1Q header, so we do here too. */
216 match->flow.vlan_tci = htons(0);
217 match->wc.masks.vlan_tci = htons(0xffff);
219 ovs_be16 vid, pcp, tci;
222 vid = ofmatch->dl_vlan & htons(VLAN_VID_MASK);
223 hpcp = (ofmatch->dl_vlan_pcp << VLAN_PCP_SHIFT) & VLAN_PCP_MASK;
225 tci = vid | pcp | htons(VLAN_CFI);
226 match->flow.vlan_tci = tci & match->wc.masks.vlan_tci;
230 match_zero_wildcarded_fields(match);
233 /* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */
235 ofputil_match_to_ofp10_match(const struct match *match,
236 struct ofp10_match *ofmatch)
238 const struct flow_wildcards *wc = &match->wc;
241 /* Figure out most OpenFlow wildcards. */
243 if (!wc->masks.in_port.ofp_port) {
244 ofpfw |= OFPFW10_IN_PORT;
246 if (!wc->masks.dl_type) {
247 ofpfw |= OFPFW10_DL_TYPE;
249 if (!wc->masks.nw_proto) {
250 ofpfw |= OFPFW10_NW_PROTO;
252 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_src)
253 << OFPFW10_NW_SRC_SHIFT);
254 ofpfw |= (ofputil_netmask_to_wcbits(wc->masks.nw_dst)
255 << OFPFW10_NW_DST_SHIFT);
256 if (!(wc->masks.nw_tos & IP_DSCP_MASK)) {
257 ofpfw |= OFPFW10_NW_TOS;
259 if (!wc->masks.tp_src) {
260 ofpfw |= OFPFW10_TP_SRC;
262 if (!wc->masks.tp_dst) {
263 ofpfw |= OFPFW10_TP_DST;
265 if (eth_addr_is_zero(wc->masks.dl_src)) {
266 ofpfw |= OFPFW10_DL_SRC;
268 if (eth_addr_is_zero(wc->masks.dl_dst)) {
269 ofpfw |= OFPFW10_DL_DST;
272 /* Translate VLANs. */
273 ofmatch->dl_vlan = htons(0);
274 ofmatch->dl_vlan_pcp = 0;
275 if (match->wc.masks.vlan_tci == htons(0)) {
276 ofpfw |= OFPFW10_DL_VLAN | OFPFW10_DL_VLAN_PCP;
277 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
278 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
279 ofmatch->dl_vlan = htons(OFP10_VLAN_NONE);
280 ofpfw |= OFPFW10_DL_VLAN_PCP;
282 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
283 ofpfw |= OFPFW10_DL_VLAN;
285 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
288 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
289 ofpfw |= OFPFW10_DL_VLAN_PCP;
291 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
295 /* Compose most of the match structure. */
296 ofmatch->wildcards = htonl(ofpfw);
297 ofmatch->in_port = htons(ofp_to_u16(match->flow.in_port.ofp_port));
298 memcpy(ofmatch->dl_src, match->flow.dl_src, ETH_ADDR_LEN);
299 memcpy(ofmatch->dl_dst, match->flow.dl_dst, ETH_ADDR_LEN);
300 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
301 ofmatch->nw_src = match->flow.nw_src;
302 ofmatch->nw_dst = match->flow.nw_dst;
303 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
304 ofmatch->nw_proto = match->flow.nw_proto;
305 ofmatch->tp_src = match->flow.tp_src;
306 ofmatch->tp_dst = match->flow.tp_dst;
307 memset(ofmatch->pad1, '\0', sizeof ofmatch->pad1);
308 memset(ofmatch->pad2, '\0', sizeof ofmatch->pad2);
312 ofputil_pull_ofp11_match(struct ofpbuf *buf, struct match *match,
313 uint16_t *padded_match_len)
315 struct ofp11_match_header *omh = ofpbuf_data(buf);
318 if (ofpbuf_size(buf) < sizeof *omh) {
319 return OFPERR_OFPBMC_BAD_LEN;
322 match_len = ntohs(omh->length);
324 switch (ntohs(omh->type)) {
325 case OFPMT_STANDARD: {
326 struct ofp11_match *om;
328 if (match_len != sizeof *om || ofpbuf_size(buf) < sizeof *om) {
329 return OFPERR_OFPBMC_BAD_LEN;
331 om = ofpbuf_pull(buf, sizeof *om);
332 if (padded_match_len) {
333 *padded_match_len = match_len;
335 return ofputil_match_from_ofp11_match(om, match);
339 if (padded_match_len) {
340 *padded_match_len = ROUND_UP(match_len, 8);
342 return oxm_pull_match(buf, match);
345 return OFPERR_OFPBMC_BAD_TYPE;
349 /* Converts the ofp11_match in 'ofmatch' into a struct match in 'match'.
350 * Returns 0 if successful, otherwise an OFPERR_* value. */
352 ofputil_match_from_ofp11_match(const struct ofp11_match *ofmatch,
355 uint16_t wc = ntohl(ofmatch->wildcards);
356 uint8_t dl_src_mask[ETH_ADDR_LEN];
357 uint8_t dl_dst_mask[ETH_ADDR_LEN];
358 bool ipv4, arp, rarp;
361 match_init_catchall(match);
363 if (!(wc & OFPFW11_IN_PORT)) {
367 error = ofputil_port_from_ofp11(ofmatch->in_port, &ofp_port);
369 return OFPERR_OFPBMC_BAD_VALUE;
371 match_set_in_port(match, ofp_port);
374 for (i = 0; i < ETH_ADDR_LEN; i++) {
375 dl_src_mask[i] = ~ofmatch->dl_src_mask[i];
377 match_set_dl_src_masked(match, ofmatch->dl_src, dl_src_mask);
379 for (i = 0; i < ETH_ADDR_LEN; i++) {
380 dl_dst_mask[i] = ~ofmatch->dl_dst_mask[i];
382 match_set_dl_dst_masked(match, ofmatch->dl_dst, dl_dst_mask);
384 if (!(wc & OFPFW11_DL_VLAN)) {
385 if (ofmatch->dl_vlan == htons(OFPVID11_NONE)) {
386 /* Match only packets without a VLAN tag. */
387 match->flow.vlan_tci = htons(0);
388 match->wc.masks.vlan_tci = OVS_BE16_MAX;
390 if (ofmatch->dl_vlan == htons(OFPVID11_ANY)) {
391 /* Match any packet with a VLAN tag regardless of VID. */
392 match->flow.vlan_tci = htons(VLAN_CFI);
393 match->wc.masks.vlan_tci = htons(VLAN_CFI);
394 } else if (ntohs(ofmatch->dl_vlan) < 4096) {
395 /* Match only packets with the specified VLAN VID. */
396 match->flow.vlan_tci = htons(VLAN_CFI) | ofmatch->dl_vlan;
397 match->wc.masks.vlan_tci = htons(VLAN_CFI | VLAN_VID_MASK);
400 return OFPERR_OFPBMC_BAD_VALUE;
403 if (!(wc & OFPFW11_DL_VLAN_PCP)) {
404 if (ofmatch->dl_vlan_pcp <= 7) {
405 match->flow.vlan_tci |= htons(ofmatch->dl_vlan_pcp
407 match->wc.masks.vlan_tci |= htons(VLAN_PCP_MASK);
410 return OFPERR_OFPBMC_BAD_VALUE;
416 if (!(wc & OFPFW11_DL_TYPE)) {
417 match_set_dl_type(match,
418 ofputil_dl_type_from_openflow(ofmatch->dl_type));
421 ipv4 = match->flow.dl_type == htons(ETH_TYPE_IP);
422 arp = match->flow.dl_type == htons(ETH_TYPE_ARP);
423 rarp = match->flow.dl_type == htons(ETH_TYPE_RARP);
425 if (ipv4 && !(wc & OFPFW11_NW_TOS)) {
426 if (ofmatch->nw_tos & ~IP_DSCP_MASK) {
428 return OFPERR_OFPBMC_BAD_VALUE;
431 match_set_nw_dscp(match, ofmatch->nw_tos);
434 if (ipv4 || arp || rarp) {
435 if (!(wc & OFPFW11_NW_PROTO)) {
436 match_set_nw_proto(match, ofmatch->nw_proto);
438 match_set_nw_src_masked(match, ofmatch->nw_src, ~ofmatch->nw_src_mask);
439 match_set_nw_dst_masked(match, ofmatch->nw_dst, ~ofmatch->nw_dst_mask);
442 #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
443 if (ipv4 && (wc & OFPFW11_TP_ALL) != OFPFW11_TP_ALL) {
444 switch (match->flow.nw_proto) {
446 /* "A.2.3 Flow Match Structures" in OF1.1 says:
448 * The tp_src and tp_dst fields will be ignored unless the
449 * network protocol specified is as TCP, UDP or SCTP.
451 * but I'm pretty sure we should support ICMP too, otherwise
452 * that's a regression from OF1.0. */
453 if (!(wc & OFPFW11_TP_SRC)) {
454 uint16_t icmp_type = ntohs(ofmatch->tp_src);
455 if (icmp_type < 0x100) {
456 match_set_icmp_type(match, icmp_type);
458 return OFPERR_OFPBMC_BAD_FIELD;
461 if (!(wc & OFPFW11_TP_DST)) {
462 uint16_t icmp_code = ntohs(ofmatch->tp_dst);
463 if (icmp_code < 0x100) {
464 match_set_icmp_code(match, icmp_code);
466 return OFPERR_OFPBMC_BAD_FIELD;
474 if (!(wc & (OFPFW11_TP_SRC))) {
475 match_set_tp_src(match, ofmatch->tp_src);
477 if (!(wc & (OFPFW11_TP_DST))) {
478 match_set_tp_dst(match, ofmatch->tp_dst);
483 /* OF1.1 says explicitly to ignore this. */
488 if (eth_type_mpls(match->flow.dl_type)) {
489 if (!(wc & OFPFW11_MPLS_LABEL)) {
490 match_set_mpls_label(match, 0, ofmatch->mpls_label);
492 if (!(wc & OFPFW11_MPLS_TC)) {
493 match_set_mpls_tc(match, 0, ofmatch->mpls_tc);
497 match_set_metadata_masked(match, ofmatch->metadata,
498 ~ofmatch->metadata_mask);
503 /* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */
505 ofputil_match_to_ofp11_match(const struct match *match,
506 struct ofp11_match *ofmatch)
511 memset(ofmatch, 0, sizeof *ofmatch);
512 ofmatch->omh.type = htons(OFPMT_STANDARD);
513 ofmatch->omh.length = htons(OFPMT11_STANDARD_LENGTH);
515 if (!match->wc.masks.in_port.ofp_port) {
516 wc |= OFPFW11_IN_PORT;
518 ofmatch->in_port = ofputil_port_to_ofp11(match->flow.in_port.ofp_port);
521 memcpy(ofmatch->dl_src, match->flow.dl_src, ETH_ADDR_LEN);
522 for (i = 0; i < ETH_ADDR_LEN; i++) {
523 ofmatch->dl_src_mask[i] = ~match->wc.masks.dl_src[i];
526 memcpy(ofmatch->dl_dst, match->flow.dl_dst, ETH_ADDR_LEN);
527 for (i = 0; i < ETH_ADDR_LEN; i++) {
528 ofmatch->dl_dst_mask[i] = ~match->wc.masks.dl_dst[i];
531 if (match->wc.masks.vlan_tci == htons(0)) {
532 wc |= OFPFW11_DL_VLAN | OFPFW11_DL_VLAN_PCP;
533 } else if (match->wc.masks.vlan_tci & htons(VLAN_CFI)
534 && !(match->flow.vlan_tci & htons(VLAN_CFI))) {
535 ofmatch->dl_vlan = htons(OFPVID11_NONE);
536 wc |= OFPFW11_DL_VLAN_PCP;
538 if (!(match->wc.masks.vlan_tci & htons(VLAN_VID_MASK))) {
539 ofmatch->dl_vlan = htons(OFPVID11_ANY);
541 ofmatch->dl_vlan = htons(vlan_tci_to_vid(match->flow.vlan_tci));
544 if (!(match->wc.masks.vlan_tci & htons(VLAN_PCP_MASK))) {
545 wc |= OFPFW11_DL_VLAN_PCP;
547 ofmatch->dl_vlan_pcp = vlan_tci_to_pcp(match->flow.vlan_tci);
551 if (!match->wc.masks.dl_type) {
552 wc |= OFPFW11_DL_TYPE;
554 ofmatch->dl_type = ofputil_dl_type_to_openflow(match->flow.dl_type);
557 if (!(match->wc.masks.nw_tos & IP_DSCP_MASK)) {
558 wc |= OFPFW11_NW_TOS;
560 ofmatch->nw_tos = match->flow.nw_tos & IP_DSCP_MASK;
563 if (!match->wc.masks.nw_proto) {
564 wc |= OFPFW11_NW_PROTO;
566 ofmatch->nw_proto = match->flow.nw_proto;
569 ofmatch->nw_src = match->flow.nw_src;
570 ofmatch->nw_src_mask = ~match->wc.masks.nw_src;
571 ofmatch->nw_dst = match->flow.nw_dst;
572 ofmatch->nw_dst_mask = ~match->wc.masks.nw_dst;
574 if (!match->wc.masks.tp_src) {
575 wc |= OFPFW11_TP_SRC;
577 ofmatch->tp_src = match->flow.tp_src;
580 if (!match->wc.masks.tp_dst) {
581 wc |= OFPFW11_TP_DST;
583 ofmatch->tp_dst = match->flow.tp_dst;
586 if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK))) {
587 wc |= OFPFW11_MPLS_LABEL;
589 ofmatch->mpls_label = htonl(mpls_lse_to_label(
590 match->flow.mpls_lse[0]));
593 if (!(match->wc.masks.mpls_lse[0] & htonl(MPLS_TC_MASK))) {
594 wc |= OFPFW11_MPLS_TC;
596 ofmatch->mpls_tc = mpls_lse_to_tc(match->flow.mpls_lse[0]);
599 ofmatch->metadata = match->flow.metadata;
600 ofmatch->metadata_mask = ~match->wc.masks.metadata;
602 ofmatch->wildcards = htonl(wc);
605 /* Returns the "typical" length of a match for 'protocol', for use in
606 * estimating space to preallocate. */
608 ofputil_match_typical_len(enum ofputil_protocol protocol)
611 case OFPUTIL_P_OF10_STD:
612 case OFPUTIL_P_OF10_STD_TID:
613 return sizeof(struct ofp10_match);
615 case OFPUTIL_P_OF10_NXM:
616 case OFPUTIL_P_OF10_NXM_TID:
617 return NXM_TYPICAL_LEN;
619 case OFPUTIL_P_OF11_STD:
620 return sizeof(struct ofp11_match);
622 case OFPUTIL_P_OF12_OXM:
623 case OFPUTIL_P_OF13_OXM:
624 case OFPUTIL_P_OF14_OXM:
625 return NXM_TYPICAL_LEN;
632 /* Appends to 'b' an struct ofp11_match_header followed by a match that
633 * expresses 'match' properly for 'protocol', plus enough zero bytes to pad the
634 * data appended out to a multiple of 8. 'protocol' must be one that is usable
635 * in OpenFlow 1.1 or later.
637 * This function can cause 'b''s data to be reallocated.
639 * Returns the number of bytes appended to 'b', excluding the padding. Never
642 ofputil_put_ofp11_match(struct ofpbuf *b, const struct match *match,
643 enum ofputil_protocol protocol)
646 case OFPUTIL_P_OF10_STD:
647 case OFPUTIL_P_OF10_STD_TID:
648 case OFPUTIL_P_OF10_NXM:
649 case OFPUTIL_P_OF10_NXM_TID:
652 case OFPUTIL_P_OF11_STD: {
653 struct ofp11_match *om;
655 /* Make sure that no padding is needed. */
656 BUILD_ASSERT_DECL(sizeof *om % 8 == 0);
658 om = ofpbuf_put_uninit(b, sizeof *om);
659 ofputil_match_to_ofp11_match(match, om);
663 case OFPUTIL_P_OF12_OXM:
664 case OFPUTIL_P_OF13_OXM:
665 case OFPUTIL_P_OF14_OXM:
666 return oxm_put_match(b, match);
672 /* Given a 'dl_type' value in the format used in struct flow, returns the
673 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
676 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type)
678 return (flow_dl_type == htons(FLOW_DL_TYPE_NONE)
679 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE)
683 /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
684 * structure, returns the corresponding 'dl_type' value for use in struct
687 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type)
689 return (ofp_dl_type == htons(OFP_DL_TYPE_NOT_ETH_TYPE)
690 ? htons(FLOW_DL_TYPE_NONE)
696 struct proto_abbrev {
697 enum ofputil_protocol protocol;
701 /* Most users really don't care about some of the differences between
702 * protocols. These abbreviations help with that.
704 * Until it is safe to use the OpenFlow 1.4 protocol (which currently can
705 * cause aborts due to unimplemented features), we omit OpenFlow 1.4 from all
707 static const struct proto_abbrev proto_abbrevs[] = {
708 { OFPUTIL_P_ANY & ~OFPUTIL_P_OF14_OXM, "any" },
709 { OFPUTIL_P_OF10_STD_ANY & ~OFPUTIL_P_OF14_OXM, "OpenFlow10" },
710 { OFPUTIL_P_OF10_NXM_ANY & ~OFPUTIL_P_OF14_OXM, "NXM" },
711 { OFPUTIL_P_ANY_OXM & ~OFPUTIL_P_OF14_OXM, "OXM" },
713 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
715 enum ofputil_protocol ofputil_flow_dump_protocols[] = {
723 size_t ofputil_n_flow_dump_protocols = ARRAY_SIZE(ofputil_flow_dump_protocols);
725 /* Returns the set of ofputil_protocols that are supported with the given
726 * OpenFlow 'version'. 'version' should normally be an 8-bit OpenFlow version
727 * identifier (e.g. 0x01 for OpenFlow 1.0, 0x02 for OpenFlow 1.1). Returns 0
728 * if 'version' is not supported or outside the valid range. */
729 enum ofputil_protocol
730 ofputil_protocols_from_ofp_version(enum ofp_version version)
734 return OFPUTIL_P_OF10_STD_ANY | OFPUTIL_P_OF10_NXM_ANY;
736 return OFPUTIL_P_OF11_STD;
738 return OFPUTIL_P_OF12_OXM;
740 return OFPUTIL_P_OF13_OXM;
742 return OFPUTIL_P_OF14_OXM;
748 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
749 * connection that has negotiated the given 'version'. 'version' should
750 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
751 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
752 * outside the valid range. */
753 enum ofputil_protocol
754 ofputil_protocol_from_ofp_version(enum ofp_version version)
756 return rightmost_1bit(ofputil_protocols_from_ofp_version(version));
759 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
760 * etc.) that corresponds to 'protocol'. */
762 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol)
765 case OFPUTIL_P_OF10_STD:
766 case OFPUTIL_P_OF10_STD_TID:
767 case OFPUTIL_P_OF10_NXM:
768 case OFPUTIL_P_OF10_NXM_TID:
769 return OFP10_VERSION;
770 case OFPUTIL_P_OF11_STD:
771 return OFP11_VERSION;
772 case OFPUTIL_P_OF12_OXM:
773 return OFP12_VERSION;
774 case OFPUTIL_P_OF13_OXM:
775 return OFP13_VERSION;
776 case OFPUTIL_P_OF14_OXM:
777 return OFP14_VERSION;
783 /* Returns a bitmap of OpenFlow versions that are supported by at
784 * least one of the 'protocols'. */
786 ofputil_protocols_to_version_bitmap(enum ofputil_protocol protocols)
790 for (; protocols; protocols = zero_rightmost_1bit(protocols)) {
791 enum ofputil_protocol protocol = rightmost_1bit(protocols);
793 bitmap |= 1u << ofputil_protocol_to_ofp_version(protocol);
799 /* Returns the set of protocols that are supported on top of the
800 * OpenFlow versions included in 'bitmap'. */
801 enum ofputil_protocol
802 ofputil_protocols_from_version_bitmap(uint32_t bitmap)
804 enum ofputil_protocol protocols = 0;
806 for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
807 enum ofp_version version = rightmost_1bit_idx(bitmap);
809 protocols |= ofputil_protocols_from_ofp_version(version);
815 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
818 ofputil_protocol_is_valid(enum ofputil_protocol protocol)
820 return protocol & OFPUTIL_P_ANY && is_pow2(protocol);
823 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
824 * extension turned on or off if 'enable' is true or false, respectively.
826 * This extension is only useful for protocols whose "standard" version does
827 * not allow specific tables to be modified. In particular, this is true of
828 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
829 * specifies a table ID and so there is no need for such an extension. When
830 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
831 * extension, this function just returns its 'protocol' argument unchanged
832 * regardless of the value of 'enable'. */
833 enum ofputil_protocol
834 ofputil_protocol_set_tid(enum ofputil_protocol protocol, bool enable)
837 case OFPUTIL_P_OF10_STD:
838 case OFPUTIL_P_OF10_STD_TID:
839 return enable ? OFPUTIL_P_OF10_STD_TID : OFPUTIL_P_OF10_STD;
841 case OFPUTIL_P_OF10_NXM:
842 case OFPUTIL_P_OF10_NXM_TID:
843 return enable ? OFPUTIL_P_OF10_NXM_TID : OFPUTIL_P_OF10_NXM;
845 case OFPUTIL_P_OF11_STD:
846 return OFPUTIL_P_OF11_STD;
848 case OFPUTIL_P_OF12_OXM:
849 return OFPUTIL_P_OF12_OXM;
851 case OFPUTIL_P_OF13_OXM:
852 return OFPUTIL_P_OF13_OXM;
854 case OFPUTIL_P_OF14_OXM:
855 return OFPUTIL_P_OF14_OXM;
862 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
863 * some extension to a standard protocol version, the return value is the
864 * standard version of that protocol without any extension. If 'protocol' is a
865 * standard protocol version, returns 'protocol' unchanged. */
866 enum ofputil_protocol
867 ofputil_protocol_to_base(enum ofputil_protocol protocol)
869 return ofputil_protocol_set_tid(protocol, false);
872 /* Returns 'new_base' with any extensions taken from 'cur'. */
873 enum ofputil_protocol
874 ofputil_protocol_set_base(enum ofputil_protocol cur,
875 enum ofputil_protocol new_base)
877 bool tid = (cur & OFPUTIL_P_TID) != 0;
880 case OFPUTIL_P_OF10_STD:
881 case OFPUTIL_P_OF10_STD_TID:
882 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD, tid);
884 case OFPUTIL_P_OF10_NXM:
885 case OFPUTIL_P_OF10_NXM_TID:
886 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM, tid);
888 case OFPUTIL_P_OF11_STD:
889 return ofputil_protocol_set_tid(OFPUTIL_P_OF11_STD, tid);
891 case OFPUTIL_P_OF12_OXM:
892 return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM, tid);
894 case OFPUTIL_P_OF13_OXM:
895 return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM, tid);
897 case OFPUTIL_P_OF14_OXM:
898 return ofputil_protocol_set_tid(OFPUTIL_P_OF14_OXM, tid);
905 /* Returns a string form of 'protocol', if a simple form exists (that is, if
906 * 'protocol' is either a single protocol or it is a combination of protocols
907 * that have a single abbreviation). Otherwise, returns NULL. */
909 ofputil_protocol_to_string(enum ofputil_protocol protocol)
911 const struct proto_abbrev *p;
913 /* Use a "switch" statement for single-bit names so that we get a compiler
914 * warning if we forget any. */
916 case OFPUTIL_P_OF10_NXM:
917 return "NXM-table_id";
919 case OFPUTIL_P_OF10_NXM_TID:
920 return "NXM+table_id";
922 case OFPUTIL_P_OF10_STD:
923 return "OpenFlow10-table_id";
925 case OFPUTIL_P_OF10_STD_TID:
926 return "OpenFlow10+table_id";
928 case OFPUTIL_P_OF11_STD:
931 case OFPUTIL_P_OF12_OXM:
932 return "OXM-OpenFlow12";
934 case OFPUTIL_P_OF13_OXM:
935 return "OXM-OpenFlow13";
937 case OFPUTIL_P_OF14_OXM:
938 return "OXM-OpenFlow14";
941 /* Check abbreviations. */
942 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
943 if (protocol == p->protocol) {
951 /* Returns a string that represents 'protocols'. The return value might be a
952 * comma-separated list if 'protocols' doesn't have a simple name. The return
953 * value is "none" if 'protocols' is 0.
955 * The caller must free the returned string (with free()). */
957 ofputil_protocols_to_string(enum ofputil_protocol protocols)
961 ovs_assert(!(protocols & ~OFPUTIL_P_ANY));
962 if (protocols == 0) {
963 return xstrdup("none");
968 const struct proto_abbrev *p;
972 ds_put_char(&s, ',');
975 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
976 if ((protocols & p->protocol) == p->protocol) {
977 ds_put_cstr(&s, p->name);
978 protocols &= ~p->protocol;
983 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
984 enum ofputil_protocol bit = 1u << i;
986 if (protocols & bit) {
987 ds_put_cstr(&s, ofputil_protocol_to_string(bit));
996 return ds_steal_cstr(&s);
999 static enum ofputil_protocol
1000 ofputil_protocol_from_string__(const char *s, size_t n)
1002 const struct proto_abbrev *p;
1005 for (i = 0; i < CHAR_BIT * sizeof(enum ofputil_protocol); i++) {
1006 enum ofputil_protocol bit = 1u << i;
1007 const char *name = ofputil_protocol_to_string(bit);
1009 if (name && n == strlen(name) && !strncasecmp(s, name, n)) {
1014 for (p = proto_abbrevs; p < &proto_abbrevs[N_PROTO_ABBREVS]; p++) {
1015 if (n == strlen(p->name) && !strncasecmp(s, p->name, n)) {
1023 /* Returns the nonempty set of protocols represented by 's', which can be a
1024 * single protocol name or abbreviation or a comma-separated list of them.
1026 * Aborts the program with an error message if 's' is invalid. */
1027 enum ofputil_protocol
1028 ofputil_protocols_from_string(const char *s)
1030 const char *orig_s = s;
1031 enum ofputil_protocol protocols;
1035 enum ofputil_protocol p;
1038 n = strcspn(s, ",");
1044 p = ofputil_protocol_from_string__(s, n);
1046 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n, s);
1054 ovs_fatal(0, "%s: no flow protocol specified", orig_s);
1060 ofputil_version_from_string(const char *s)
1062 if (!strcasecmp(s, "OpenFlow10")) {
1063 return OFP10_VERSION;
1065 if (!strcasecmp(s, "OpenFlow11")) {
1066 return OFP11_VERSION;
1068 if (!strcasecmp(s, "OpenFlow12")) {
1069 return OFP12_VERSION;
1071 if (!strcasecmp(s, "OpenFlow13")) {
1072 return OFP13_VERSION;
1074 if (!strcasecmp(s, "OpenFlow14")) {
1075 return OFP14_VERSION;
1081 is_delimiter(unsigned char c)
1083 return isspace(c) || c == ',';
1087 ofputil_versions_from_string(const char *s)
1090 uint32_t bitmap = 0;
1097 if (is_delimiter(s[i])) {
1102 while (s[i + j] && !is_delimiter(s[i + j])) {
1105 key = xmemdup0(s + i, j);
1106 version = ofputil_version_from_string(key);
1108 VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key);
1111 bitmap |= 1u << version;
1119 ofputil_versions_from_strings(char ** const s, size_t count)
1121 uint32_t bitmap = 0;
1124 int version = ofputil_version_from_string(s[count]);
1126 VLOG_WARN("Unknown OpenFlow version: \"%s\"", s[count]);
1128 bitmap |= 1u << version;
1136 ofputil_version_to_string(enum ofp_version ofp_version)
1138 switch (ofp_version) {
1140 return "OpenFlow10";
1142 return "OpenFlow11";
1144 return "OpenFlow12";
1146 return "OpenFlow13";
1148 return "OpenFlow14";
1155 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format)
1157 switch (packet_in_format) {
1158 case NXPIF_OPENFLOW10:
1167 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format)
1169 switch (packet_in_format) {
1170 case NXPIF_OPENFLOW10:
1171 return "openflow10";
1180 ofputil_packet_in_format_from_string(const char *s)
1182 return (!strcmp(s, "openflow10") ? NXPIF_OPENFLOW10
1183 : !strcmp(s, "nxm") ? NXPIF_NXM
1188 ofputil_format_version(struct ds *msg, enum ofp_version version)
1190 ds_put_format(msg, "0x%02x", version);
1194 ofputil_format_version_name(struct ds *msg, enum ofp_version version)
1196 ds_put_cstr(msg, ofputil_version_to_string(version));
1200 ofputil_format_version_bitmap__(struct ds *msg, uint32_t bitmap,
1201 void (*format_version)(struct ds *msg,
1205 format_version(msg, raw_ctz(bitmap));
1206 bitmap = zero_rightmost_1bit(bitmap);
1208 ds_put_cstr(msg, ", ");
1214 ofputil_format_version_bitmap(struct ds *msg, uint32_t bitmap)
1216 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version);
1220 ofputil_format_version_bitmap_names(struct ds *msg, uint32_t bitmap)
1222 ofputil_format_version_bitmap__(msg, bitmap, ofputil_format_version_name);
1226 ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header *oheh,
1227 uint32_t *allowed_versionsp)
1229 uint16_t bitmap_len = ntohs(oheh->length) - sizeof *oheh;
1230 const ovs_be32 *bitmap = ALIGNED_CAST(const ovs_be32 *, oheh + 1);
1231 uint32_t allowed_versions;
1233 if (!bitmap_len || bitmap_len % sizeof *bitmap) {
1237 /* Only use the first 32-bit element of the bitmap as that is all the
1238 * current implementation supports. Subsequent elements are ignored which
1239 * should have no effect on session negotiation until Open vSwtich supports
1240 * wire-protocol versions greater than 31.
1242 allowed_versions = ntohl(bitmap[0]);
1244 if (allowed_versions & 1) {
1245 /* There's no OpenFlow version 0. */
1246 VLOG_WARN_RL(&bad_ofmsg_rl, "peer claims to support invalid OpenFlow "
1248 allowed_versions &= ~1u;
1251 if (!allowed_versions) {
1252 VLOG_WARN_RL(&bad_ofmsg_rl, "peer does not support any OpenFlow "
1253 "version (between 0x01 and 0x1f)");
1257 *allowed_versionsp = allowed_versions;
1262 version_bitmap_from_version(uint8_t ofp_version)
1264 return ((ofp_version < 32 ? 1u << ofp_version : 0) - 1) << 1;
1267 /* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions'
1268 * the set of OpenFlow versions for which 'oh' announces support.
1270 * Because of how OpenFlow defines OFPT_HELLO messages, this function is always
1271 * successful, and thus '*allowed_versions' is always initialized. However, it
1272 * returns false if 'oh' contains some data that could not be fully understood,
1273 * true if 'oh' was completely parsed. */
1275 ofputil_decode_hello(const struct ofp_header *oh, uint32_t *allowed_versions)
1280 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
1281 ofpbuf_pull(&msg, sizeof *oh);
1283 *allowed_versions = version_bitmap_from_version(oh->version);
1284 while (ofpbuf_size(&msg)) {
1285 const struct ofp_hello_elem_header *oheh;
1288 if (ofpbuf_size(&msg) < sizeof *oheh) {
1292 oheh = ofpbuf_data(&msg);
1293 len = ntohs(oheh->length);
1294 if (len < sizeof *oheh || !ofpbuf_try_pull(&msg, ROUND_UP(len, 8))) {
1298 if (oheh->type != htons(OFPHET_VERSIONBITMAP)
1299 || !ofputil_decode_hello_bitmap(oheh, allowed_versions)) {
1307 /* Returns true if 'allowed_versions' needs to be accompanied by a version
1308 * bitmap to be correctly expressed in an OFPT_HELLO message. */
1310 should_send_version_bitmap(uint32_t allowed_versions)
1312 return !is_pow2((allowed_versions >> 1) + 1);
1315 /* Create an OFPT_HELLO message that expresses support for the OpenFlow
1316 * versions in the 'allowed_versions' bitmaps and returns the message. */
1318 ofputil_encode_hello(uint32_t allowed_versions)
1320 enum ofp_version ofp_version;
1323 ofp_version = leftmost_1bit_idx(allowed_versions);
1324 msg = ofpraw_alloc(OFPRAW_OFPT_HELLO, ofp_version, 0);
1326 if (should_send_version_bitmap(allowed_versions)) {
1327 struct ofp_hello_elem_header *oheh;
1330 map_len = sizeof allowed_versions;
1331 oheh = ofpbuf_put_zeros(msg, ROUND_UP(map_len + sizeof *oheh, 8));
1332 oheh->type = htons(OFPHET_VERSIONBITMAP);
1333 oheh->length = htons(map_len + sizeof *oheh);
1334 *ALIGNED_CAST(ovs_be32 *, oheh + 1) = htonl(allowed_versions);
1336 ofpmsg_update_length(msg);
1342 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1343 * protocol is 'current', at least partly transitions the protocol to 'want'.
1344 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1345 * connection if the switch processes the returned message correctly. (If
1346 * '*next != want' then the caller will have to iterate.)
1348 * If 'current == want', or if it is not possible to transition from 'current'
1349 * to 'want' (because, for example, 'current' and 'want' use different OpenFlow
1350 * protocol versions), returns NULL and stores 'current' in '*next'. */
1352 ofputil_encode_set_protocol(enum ofputil_protocol current,
1353 enum ofputil_protocol want,
1354 enum ofputil_protocol *next)
1356 enum ofp_version cur_version, want_version;
1357 enum ofputil_protocol cur_base, want_base;
1358 bool cur_tid, want_tid;
1360 cur_version = ofputil_protocol_to_ofp_version(current);
1361 want_version = ofputil_protocol_to_ofp_version(want);
1362 if (cur_version != want_version) {
1367 cur_base = ofputil_protocol_to_base(current);
1368 want_base = ofputil_protocol_to_base(want);
1369 if (cur_base != want_base) {
1370 *next = ofputil_protocol_set_base(current, want_base);
1372 switch (want_base) {
1373 case OFPUTIL_P_OF10_NXM:
1374 return ofputil_encode_nx_set_flow_format(NXFF_NXM);
1376 case OFPUTIL_P_OF10_STD:
1377 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10);
1379 case OFPUTIL_P_OF11_STD:
1380 case OFPUTIL_P_OF12_OXM:
1381 case OFPUTIL_P_OF13_OXM:
1382 case OFPUTIL_P_OF14_OXM:
1383 /* There is only one variant of each OpenFlow 1.1+ protocol, and we
1384 * verified above that we're not trying to change versions. */
1387 case OFPUTIL_P_OF10_STD_TID:
1388 case OFPUTIL_P_OF10_NXM_TID:
1393 cur_tid = (current & OFPUTIL_P_TID) != 0;
1394 want_tid = (want & OFPUTIL_P_TID) != 0;
1395 if (cur_tid != want_tid) {
1396 *next = ofputil_protocol_set_tid(current, want_tid);
1397 return ofputil_make_flow_mod_table_id(want_tid);
1400 ovs_assert(current == want);
1406 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1407 * format to 'nxff'. */
1409 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff)
1411 struct nx_set_flow_format *sff;
1414 ovs_assert(ofputil_nx_flow_format_is_valid(nxff));
1416 msg = ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT, OFP10_VERSION, 0);
1417 sff = ofpbuf_put_zeros(msg, sizeof *sff);
1418 sff->format = htonl(nxff);
1423 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1425 enum ofputil_protocol
1426 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format)
1428 switch (flow_format) {
1429 case NXFF_OPENFLOW10:
1430 return OFPUTIL_P_OF10_STD;
1433 return OFPUTIL_P_OF10_NXM;
1440 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1442 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format)
1444 return ofputil_nx_flow_format_to_protocol(flow_format) != 0;
1447 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1450 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format)
1452 switch (flow_format) {
1453 case NXFF_OPENFLOW10:
1454 return "openflow10";
1463 ofputil_make_set_packet_in_format(enum ofp_version ofp_version,
1464 enum nx_packet_in_format packet_in_format)
1466 struct nx_set_packet_in_format *spif;
1469 msg = ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT, ofp_version, 0);
1470 spif = ofpbuf_put_zeros(msg, sizeof *spif);
1471 spif->format = htonl(packet_in_format);
1476 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1477 * extension on or off (according to 'flow_mod_table_id'). */
1479 ofputil_make_flow_mod_table_id(bool flow_mod_table_id)
1481 struct nx_flow_mod_table_id *nfmti;
1484 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID, OFP10_VERSION, 0);
1485 nfmti = ofpbuf_put_zeros(msg, sizeof *nfmti);
1486 nfmti->set = flow_mod_table_id;
1490 struct ofputil_flow_mod_flag {
1492 enum ofp_version min_version, max_version;
1493 enum ofputil_flow_mod_flags flag;
1496 static const struct ofputil_flow_mod_flag ofputil_flow_mod_flags[] = {
1497 { OFPFF_SEND_FLOW_REM, OFP10_VERSION, 0, OFPUTIL_FF_SEND_FLOW_REM },
1498 { OFPFF_CHECK_OVERLAP, OFP10_VERSION, 0, OFPUTIL_FF_CHECK_OVERLAP },
1499 { OFPFF10_EMERG, OFP10_VERSION, OFP10_VERSION,
1501 { OFPFF12_RESET_COUNTS, OFP12_VERSION, 0, OFPUTIL_FF_RESET_COUNTS },
1502 { OFPFF13_NO_PKT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_PKT_COUNTS },
1503 { OFPFF13_NO_BYT_COUNTS, OFP13_VERSION, 0, OFPUTIL_FF_NO_BYT_COUNTS },
1508 ofputil_decode_flow_mod_flags(ovs_be16 raw_flags_,
1509 enum ofp_flow_mod_command command,
1510 enum ofp_version version,
1511 enum ofputil_flow_mod_flags *flagsp)
1513 uint16_t raw_flags = ntohs(raw_flags_);
1514 const struct ofputil_flow_mod_flag *f;
1517 for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1518 if (raw_flags & f->raw_flag
1519 && version >= f->min_version
1520 && (!f->max_version || version <= f->max_version)) {
1521 raw_flags &= ~f->raw_flag;
1526 /* In OF1.0 and OF1.1, "add" always resets counters, and other commands
1529 * In OF1.2 and later, OFPFF12_RESET_COUNTS controls whether each command
1530 * resets counters. */
1531 if ((version == OFP10_VERSION || version == OFP11_VERSION)
1532 && command == OFPFC_ADD) {
1533 *flagsp |= OFPUTIL_FF_RESET_COUNTS;
1536 return raw_flags ? OFPERR_OFPFMFC_BAD_FLAGS : 0;
1540 ofputil_encode_flow_mod_flags(enum ofputil_flow_mod_flags flags,
1541 enum ofp_version version)
1543 const struct ofputil_flow_mod_flag *f;
1547 for (f = ofputil_flow_mod_flags; f->raw_flag; f++) {
1549 && version >= f->min_version
1550 && (!f->max_version || version <= f->max_version)) {
1551 raw_flags |= f->raw_flag;
1555 return htons(raw_flags);
1558 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1559 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1562 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1563 * The caller must initialize 'ofpacts' and retains ownership of it.
1564 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1566 * Does not validate the flow_mod actions. The caller should do that, with
1567 * ofpacts_check(). */
1569 ofputil_decode_flow_mod(struct ofputil_flow_mod *fm,
1570 const struct ofp_header *oh,
1571 enum ofputil_protocol protocol,
1572 struct ofpbuf *ofpacts,
1573 ofp_port_t max_port, uint8_t max_table)
1580 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1581 raw = ofpraw_pull_assert(&b);
1582 if (raw == OFPRAW_OFPT11_FLOW_MOD) {
1583 /* Standard OpenFlow 1.1+ flow_mod. */
1584 const struct ofp11_flow_mod *ofm;
1586 ofm = ofpbuf_pull(&b, sizeof *ofm);
1588 error = ofputil_pull_ofp11_match(&b, &fm->match, NULL);
1593 error = ofpacts_pull_openflow_instructions(&b, ofpbuf_size(&b), oh->version,
1599 /* Translate the message. */
1600 fm->priority = ntohs(ofm->priority);
1601 if (ofm->command == OFPFC_ADD
1602 || (oh->version == OFP11_VERSION
1603 && (ofm->command == OFPFC_MODIFY ||
1604 ofm->command == OFPFC_MODIFY_STRICT)
1605 && ofm->cookie_mask == htonll(0))) {
1606 /* In OpenFlow 1.1 only, a "modify" or "modify-strict" that does
1607 * not match on the cookie is treated as an "add" if there is no
1609 fm->cookie = htonll(0);
1610 fm->cookie_mask = htonll(0);
1611 fm->new_cookie = ofm->cookie;
1613 fm->cookie = ofm->cookie;
1614 fm->cookie_mask = ofm->cookie_mask;
1615 fm->new_cookie = OVS_BE64_MAX;
1617 fm->modify_cookie = false;
1618 fm->command = ofm->command;
1622 * OF1.1 entirely forbids table_id == OFPTT_ALL.
1623 * OF1.2+ allows table_id == OFPTT_ALL only for deletes. */
1624 fm->table_id = ofm->table_id;
1625 if (fm->table_id == OFPTT_ALL
1626 && (oh->version == OFP11_VERSION
1627 || (ofm->command != OFPFC_DELETE &&
1628 ofm->command != OFPFC_DELETE_STRICT))) {
1629 return OFPERR_OFPFMFC_BAD_TABLE_ID;
1632 fm->idle_timeout = ntohs(ofm->idle_timeout);
1633 fm->hard_timeout = ntohs(ofm->hard_timeout);
1634 fm->buffer_id = ntohl(ofm->buffer_id);
1635 error = ofputil_port_from_ofp11(ofm->out_port, &fm->out_port);
1640 fm->out_group = (ofm->command == OFPFC_DELETE ||
1641 ofm->command == OFPFC_DELETE_STRICT
1642 ? ntohl(ofm->out_group)
1644 raw_flags = ofm->flags;
1648 if (raw == OFPRAW_OFPT10_FLOW_MOD) {
1649 /* Standard OpenFlow 1.0 flow_mod. */
1650 const struct ofp10_flow_mod *ofm;
1652 /* Get the ofp10_flow_mod. */
1653 ofm = ofpbuf_pull(&b, sizeof *ofm);
1655 /* Translate the rule. */
1656 ofputil_match_from_ofp10_match(&ofm->match, &fm->match);
1657 ofputil_normalize_match(&fm->match);
1659 /* Now get the actions. */
1660 error = ofpacts_pull_openflow_actions(&b, ofpbuf_size(&b), oh->version,
1666 /* OpenFlow 1.0 says that exact-match rules have to have the
1667 * highest possible priority. */
1668 fm->priority = (ofm->match.wildcards & htonl(OFPFW10_ALL)
1669 ? ntohs(ofm->priority)
1672 /* Translate the message. */
1673 command = ntohs(ofm->command);
1674 fm->cookie = htonll(0);
1675 fm->cookie_mask = htonll(0);
1676 fm->new_cookie = ofm->cookie;
1677 fm->idle_timeout = ntohs(ofm->idle_timeout);
1678 fm->hard_timeout = ntohs(ofm->hard_timeout);
1679 fm->buffer_id = ntohl(ofm->buffer_id);
1680 fm->out_port = u16_to_ofp(ntohs(ofm->out_port));
1681 fm->out_group = OFPG11_ANY;
1682 raw_flags = ofm->flags;
1683 } else if (raw == OFPRAW_NXT_FLOW_MOD) {
1684 /* Nicira extended flow_mod. */
1685 const struct nx_flow_mod *nfm;
1687 /* Dissect the message. */
1688 nfm = ofpbuf_pull(&b, sizeof *nfm);
1689 error = nx_pull_match(&b, ntohs(nfm->match_len),
1690 &fm->match, &fm->cookie, &fm->cookie_mask);
1694 error = ofpacts_pull_openflow_actions(&b, ofpbuf_size(&b), oh->version,
1700 /* Translate the message. */
1701 command = ntohs(nfm->command);
1702 if ((command & 0xff) == OFPFC_ADD && fm->cookie_mask) {
1703 /* Flow additions may only set a new cookie, not match an
1704 * existing cookie. */
1705 return OFPERR_NXBRC_NXM_INVALID;
1707 fm->priority = ntohs(nfm->priority);
1708 fm->new_cookie = nfm->cookie;
1709 fm->idle_timeout = ntohs(nfm->idle_timeout);
1710 fm->hard_timeout = ntohs(nfm->hard_timeout);
1711 fm->buffer_id = ntohl(nfm->buffer_id);
1712 fm->out_port = u16_to_ofp(ntohs(nfm->out_port));
1713 fm->out_group = OFPG11_ANY;
1714 raw_flags = nfm->flags;
1719 fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX;
1720 if (protocol & OFPUTIL_P_TID) {
1721 fm->command = command & 0xff;
1722 fm->table_id = command >> 8;
1724 fm->command = command;
1725 fm->table_id = 0xff;
1729 fm->ofpacts = ofpbuf_data(ofpacts);
1730 fm->ofpacts_len = ofpbuf_size(ofpacts);
1732 error = ofputil_decode_flow_mod_flags(raw_flags, fm->command,
1733 oh->version, &fm->flags);
1738 if (fm->flags & OFPUTIL_FF_EMERG) {
1739 /* We do not support the OpenFlow 1.0 emergency flow cache, which
1740 * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
1742 * OpenFlow 1.0 specifies the error code to use when idle_timeout
1743 * or hard_timeout is nonzero. Otherwise, there is no good error
1744 * code, so just state that the flow table is full. */
1745 return (fm->hard_timeout || fm->idle_timeout
1746 ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT
1747 : OFPERR_OFPFMFC_TABLE_FULL);
1750 return ofpacts_check_consistency(fm->ofpacts, fm->ofpacts_len,
1751 &fm->match.flow, max_port,
1752 fm->table_id, max_table, protocol);
1756 ofputil_pull_bands(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
1757 struct ofpbuf *bands)
1759 const struct ofp13_meter_band_header *ombh;
1760 struct ofputil_meter_band *mb;
1763 ombh = ofpbuf_try_pull(msg, len);
1765 return OFPERR_OFPBRC_BAD_LEN;
1768 while (len >= sizeof (struct ofp13_meter_band_drop)) {
1769 size_t ombh_len = ntohs(ombh->len);
1770 /* All supported band types have the same length. */
1771 if (ombh_len != sizeof (struct ofp13_meter_band_drop)) {
1772 return OFPERR_OFPBRC_BAD_LEN;
1774 mb = ofpbuf_put_uninit(bands, sizeof *mb);
1775 mb->type = ntohs(ombh->type);
1776 if (mb->type != OFPMBT13_DROP && mb->type != OFPMBT13_DSCP_REMARK) {
1777 return OFPERR_OFPMMFC_BAD_BAND;
1779 mb->rate = ntohl(ombh->rate);
1780 mb->burst_size = ntohl(ombh->burst_size);
1781 mb->prec_level = (mb->type == OFPMBT13_DSCP_REMARK) ?
1782 ((struct ofp13_meter_band_dscp_remark *)ombh)->prec_level : 0;
1785 ombh = ALIGNED_CAST(struct ofp13_meter_band_header *,
1786 (char *) ombh + ombh_len);
1789 return OFPERR_OFPBRC_BAD_LEN;
1796 ofputil_decode_meter_mod(const struct ofp_header *oh,
1797 struct ofputil_meter_mod *mm,
1798 struct ofpbuf *bands)
1800 const struct ofp13_meter_mod *omm;
1803 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1804 ofpraw_pull_assert(&b);
1805 omm = ofpbuf_pull(&b, sizeof *omm);
1807 /* Translate the message. */
1808 mm->command = ntohs(omm->command);
1809 if (mm->command != OFPMC13_ADD &&
1810 mm->command != OFPMC13_MODIFY &&
1811 mm->command != OFPMC13_DELETE) {
1812 return OFPERR_OFPMMFC_BAD_COMMAND;
1814 mm->meter.meter_id = ntohl(omm->meter_id);
1816 if (mm->command == OFPMC13_DELETE) {
1817 mm->meter.flags = 0;
1818 mm->meter.n_bands = 0;
1819 mm->meter.bands = NULL;
1823 mm->meter.flags = ntohs(omm->flags);
1824 if (mm->meter.flags & OFPMF13_KBPS &&
1825 mm->meter.flags & OFPMF13_PKTPS) {
1826 return OFPERR_OFPMMFC_BAD_FLAGS;
1828 mm->meter.bands = ofpbuf_data(bands);
1830 error = ofputil_pull_bands(&b, ofpbuf_size(&b), &mm->meter.n_bands, bands);
1839 ofputil_decode_meter_request(const struct ofp_header *oh, uint32_t *meter_id)
1841 const struct ofp13_meter_multipart_request *omr = ofpmsg_body(oh);
1842 *meter_id = ntohl(omr->meter_id);
1846 ofputil_encode_meter_request(enum ofp_version ofp_version,
1847 enum ofputil_meter_request_type type,
1855 case OFPUTIL_METER_CONFIG:
1856 raw = OFPRAW_OFPST13_METER_CONFIG_REQUEST;
1858 case OFPUTIL_METER_STATS:
1859 raw = OFPRAW_OFPST13_METER_REQUEST;
1862 case OFPUTIL_METER_FEATURES:
1863 raw = OFPRAW_OFPST13_METER_FEATURES_REQUEST;
1867 msg = ofpraw_alloc(raw, ofp_version, 0);
1869 if (type != OFPUTIL_METER_FEATURES) {
1870 struct ofp13_meter_multipart_request *omr;
1871 omr = ofpbuf_put_zeros(msg, sizeof *omr);
1872 omr->meter_id = htonl(meter_id);
1878 ofputil_put_bands(uint16_t n_bands, const struct ofputil_meter_band *mb,
1883 for (n = 0; n < n_bands; ++n) {
1884 /* Currently all band types have same size. */
1885 struct ofp13_meter_band_dscp_remark *ombh;
1886 size_t ombh_len = sizeof *ombh;
1888 ombh = ofpbuf_put_zeros(msg, ombh_len);
1890 ombh->type = htons(mb->type);
1891 ombh->len = htons(ombh_len);
1892 ombh->rate = htonl(mb->rate);
1893 ombh->burst_size = htonl(mb->burst_size);
1894 ombh->prec_level = mb->prec_level;
1900 /* Encode a meter stat for 'mc' and append it to 'replies'. */
1902 ofputil_append_meter_config(struct list *replies,
1903 const struct ofputil_meter_config *mc)
1905 struct ofpbuf *msg = ofpbuf_from_list(list_back(replies));
1906 size_t start_ofs = ofpbuf_size(msg);
1907 struct ofp13_meter_config *reply = ofpbuf_put_uninit(msg, sizeof *reply);
1908 reply->flags = htons(mc->flags);
1909 reply->meter_id = htonl(mc->meter_id);
1911 ofputil_put_bands(mc->n_bands, mc->bands, msg);
1913 reply->length = htons(ofpbuf_size(msg) - start_ofs);
1915 ofpmp_postappend(replies, start_ofs);
1918 /* Encode a meter stat for 'ms' and append it to 'replies'. */
1920 ofputil_append_meter_stats(struct list *replies,
1921 const struct ofputil_meter_stats *ms)
1923 struct ofp13_meter_stats *reply;
1927 len = sizeof *reply + ms->n_bands * sizeof(struct ofp13_meter_band_stats);
1928 reply = ofpmp_append(replies, len);
1930 reply->meter_id = htonl(ms->meter_id);
1931 reply->len = htons(len);
1932 memset(reply->pad, 0, sizeof reply->pad);
1933 reply->flow_count = htonl(ms->flow_count);
1934 reply->packet_in_count = htonll(ms->packet_in_count);
1935 reply->byte_in_count = htonll(ms->byte_in_count);
1936 reply->duration_sec = htonl(ms->duration_sec);
1937 reply->duration_nsec = htonl(ms->duration_nsec);
1939 for (n = 0; n < ms->n_bands; ++n) {
1940 const struct ofputil_meter_band_stats *src = &ms->bands[n];
1941 struct ofp13_meter_band_stats *dst = &reply->band_stats[n];
1943 dst->packet_band_count = htonll(src->packet_count);
1944 dst->byte_band_count = htonll(src->byte_count);
1948 /* Converts an OFPMP_METER_CONFIG reply in 'msg' into an abstract
1949 * ofputil_meter_config in 'mc', with mc->bands pointing to bands decoded into
1950 * 'bands'. The caller must have initialized 'bands' and retains ownership of
1951 * it across the call.
1953 * Multiple OFPST13_METER_CONFIG replies can be packed into a single OpenFlow
1954 * message. Calling this function multiple times for a single 'msg' iterates
1955 * through the replies. 'bands' is cleared for each reply.
1957 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1958 * otherwise a positive errno value. */
1960 ofputil_decode_meter_config(struct ofpbuf *msg,
1961 struct ofputil_meter_config *mc,
1962 struct ofpbuf *bands)
1964 const struct ofp13_meter_config *omc;
1967 /* Pull OpenFlow headers for the first call. */
1969 ofpraw_pull_assert(msg);
1972 if (!ofpbuf_size(msg)) {
1976 omc = ofpbuf_try_pull(msg, sizeof *omc);
1978 VLOG_WARN_RL(&bad_ofmsg_rl,
1979 "OFPMP_METER_CONFIG reply has %"PRIu32" leftover bytes at end",
1981 return OFPERR_OFPBRC_BAD_LEN;
1984 ofpbuf_clear(bands);
1985 err = ofputil_pull_bands(msg, ntohs(omc->length) - sizeof *omc,
1986 &mc->n_bands, bands);
1990 mc->meter_id = ntohl(omc->meter_id);
1991 mc->flags = ntohs(omc->flags);
1992 mc->bands = ofpbuf_data(bands);
1998 ofputil_pull_band_stats(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
1999 struct ofpbuf *bands)
2001 const struct ofp13_meter_band_stats *ombs;
2002 struct ofputil_meter_band_stats *mbs;
2005 ombs = ofpbuf_try_pull(msg, len);
2007 return OFPERR_OFPBRC_BAD_LEN;
2010 n = len / sizeof *ombs;
2011 if (len != n * sizeof *ombs) {
2012 return OFPERR_OFPBRC_BAD_LEN;
2015 mbs = ofpbuf_put_uninit(bands, len);
2017 for (i = 0; i < n; ++i) {
2018 mbs[i].packet_count = ntohll(ombs[i].packet_band_count);
2019 mbs[i].byte_count = ntohll(ombs[i].byte_band_count);
2025 /* Converts an OFPMP_METER reply in 'msg' into an abstract
2026 * ofputil_meter_stats in 'ms', with ms->bands pointing to band stats
2027 * decoded into 'bands'.
2029 * Multiple OFPMP_METER replies can be packed into a single OpenFlow
2030 * message. Calling this function multiple times for a single 'msg' iterates
2031 * through the replies. 'bands' is cleared for each reply.
2033 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2034 * otherwise a positive errno value. */
2036 ofputil_decode_meter_stats(struct ofpbuf *msg,
2037 struct ofputil_meter_stats *ms,
2038 struct ofpbuf *bands)
2040 const struct ofp13_meter_stats *oms;
2043 /* Pull OpenFlow headers for the first call. */
2045 ofpraw_pull_assert(msg);
2048 if (!ofpbuf_size(msg)) {
2052 oms = ofpbuf_try_pull(msg, sizeof *oms);
2054 VLOG_WARN_RL(&bad_ofmsg_rl,
2055 "OFPMP_METER reply has %"PRIu32" leftover bytes at end",
2057 return OFPERR_OFPBRC_BAD_LEN;
2060 ofpbuf_clear(bands);
2061 err = ofputil_pull_band_stats(msg, ntohs(oms->len) - sizeof *oms,
2062 &ms->n_bands, bands);
2066 ms->meter_id = ntohl(oms->meter_id);
2067 ms->flow_count = ntohl(oms->flow_count);
2068 ms->packet_in_count = ntohll(oms->packet_in_count);
2069 ms->byte_in_count = ntohll(oms->byte_in_count);
2070 ms->duration_sec = ntohl(oms->duration_sec);
2071 ms->duration_nsec = ntohl(oms->duration_nsec);
2072 ms->bands = ofpbuf_data(bands);
2078 ofputil_decode_meter_features(const struct ofp_header *oh,
2079 struct ofputil_meter_features *mf)
2081 const struct ofp13_meter_features *omf = ofpmsg_body(oh);
2083 mf->max_meters = ntohl(omf->max_meter);
2084 mf->band_types = ntohl(omf->band_types);
2085 mf->capabilities = ntohl(omf->capabilities);
2086 mf->max_bands = omf->max_bands;
2087 mf->max_color = omf->max_color;
2091 ofputil_encode_meter_features_reply(const struct ofputil_meter_features *mf,
2092 const struct ofp_header *request)
2094 struct ofpbuf *reply;
2095 struct ofp13_meter_features *omf;
2097 reply = ofpraw_alloc_stats_reply(request, 0);
2098 omf = ofpbuf_put_zeros(reply, sizeof *omf);
2100 omf->max_meter = htonl(mf->max_meters);
2101 omf->band_types = htonl(mf->band_types);
2102 omf->capabilities = htonl(mf->capabilities);
2103 omf->max_bands = mf->max_bands;
2104 omf->max_color = mf->max_color;
2110 ofputil_encode_meter_mod(enum ofp_version ofp_version,
2111 const struct ofputil_meter_mod *mm)
2115 struct ofp13_meter_mod *omm;
2117 msg = ofpraw_alloc(OFPRAW_OFPT13_METER_MOD, ofp_version,
2118 NXM_TYPICAL_LEN + mm->meter.n_bands * 16);
2119 omm = ofpbuf_put_zeros(msg, sizeof *omm);
2120 omm->command = htons(mm->command);
2121 if (mm->command != OFPMC13_DELETE) {
2122 omm->flags = htons(mm->meter.flags);
2124 omm->meter_id = htonl(mm->meter.meter_id);
2126 ofputil_put_bands(mm->meter.n_bands, mm->meter.bands, msg);
2128 ofpmsg_update_length(msg);
2133 ofputil_tid_command(const struct ofputil_flow_mod *fm,
2134 enum ofputil_protocol protocol)
2136 return htons(protocol & OFPUTIL_P_TID
2137 ? (fm->command & 0xff) | (fm->table_id << 8)
2141 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
2142 * 'protocol' and returns the message. */
2144 ofputil_encode_flow_mod(const struct ofputil_flow_mod *fm,
2145 enum ofputil_protocol protocol)
2147 enum ofp_version version = ofputil_protocol_to_ofp_version(protocol);
2148 ovs_be16 raw_flags = ofputil_encode_flow_mod_flags(fm->flags, version);
2152 case OFPUTIL_P_OF11_STD:
2153 case OFPUTIL_P_OF12_OXM:
2154 case OFPUTIL_P_OF13_OXM:
2155 case OFPUTIL_P_OF14_OXM: {
2156 struct ofp11_flow_mod *ofm;
2159 tailroom = ofputil_match_typical_len(protocol) + fm->ofpacts_len;
2160 msg = ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD, version, tailroom);
2161 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
2162 if ((protocol == OFPUTIL_P_OF11_STD
2163 && (fm->command == OFPFC_MODIFY ||
2164 fm->command == OFPFC_MODIFY_STRICT)
2165 && fm->cookie_mask == htonll(0))
2166 || fm->command == OFPFC_ADD) {
2167 ofm->cookie = fm->new_cookie;
2169 ofm->cookie = fm->cookie;
2171 ofm->cookie_mask = fm->cookie_mask;
2172 if (fm->table_id != OFPTT_ALL
2173 || (protocol != OFPUTIL_P_OF11_STD
2174 && (fm->command == OFPFC_DELETE ||
2175 fm->command == OFPFC_DELETE_STRICT))) {
2176 ofm->table_id = fm->table_id;
2180 ofm->command = fm->command;
2181 ofm->idle_timeout = htons(fm->idle_timeout);
2182 ofm->hard_timeout = htons(fm->hard_timeout);
2183 ofm->priority = htons(fm->priority);
2184 ofm->buffer_id = htonl(fm->buffer_id);
2185 ofm->out_port = ofputil_port_to_ofp11(fm->out_port);
2186 ofm->out_group = htonl(fm->out_group);
2187 ofm->flags = raw_flags;
2188 ofputil_put_ofp11_match(msg, &fm->match, protocol);
2189 ofpacts_put_openflow_instructions(fm->ofpacts, fm->ofpacts_len, msg,
2194 case OFPUTIL_P_OF10_STD:
2195 case OFPUTIL_P_OF10_STD_TID: {
2196 struct ofp10_flow_mod *ofm;
2198 msg = ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD, OFP10_VERSION,
2200 ofm = ofpbuf_put_zeros(msg, sizeof *ofm);
2201 ofputil_match_to_ofp10_match(&fm->match, &ofm->match);
2202 ofm->cookie = fm->new_cookie;
2203 ofm->command = ofputil_tid_command(fm, protocol);
2204 ofm->idle_timeout = htons(fm->idle_timeout);
2205 ofm->hard_timeout = htons(fm->hard_timeout);
2206 ofm->priority = htons(fm->priority);
2207 ofm->buffer_id = htonl(fm->buffer_id);
2208 ofm->out_port = htons(ofp_to_u16(fm->out_port));
2209 ofm->flags = raw_flags;
2210 ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2215 case OFPUTIL_P_OF10_NXM:
2216 case OFPUTIL_P_OF10_NXM_TID: {
2217 struct nx_flow_mod *nfm;
2220 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MOD, OFP10_VERSION,
2221 NXM_TYPICAL_LEN + fm->ofpacts_len);
2222 nfm = ofpbuf_put_zeros(msg, sizeof *nfm);
2223 nfm->command = ofputil_tid_command(fm, protocol);
2224 nfm->cookie = fm->new_cookie;
2225 match_len = nx_put_match(msg, &fm->match, fm->cookie, fm->cookie_mask);
2226 nfm = ofpbuf_l3(msg);
2227 nfm->idle_timeout = htons(fm->idle_timeout);
2228 nfm->hard_timeout = htons(fm->hard_timeout);
2229 nfm->priority = htons(fm->priority);
2230 nfm->buffer_id = htonl(fm->buffer_id);
2231 nfm->out_port = htons(ofp_to_u16(fm->out_port));
2232 nfm->flags = raw_flags;
2233 nfm->match_len = htons(match_len);
2234 ofpacts_put_openflow_actions(fm->ofpacts, fm->ofpacts_len, msg,
2243 ofpmsg_update_length(msg);
2248 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request *fsr,
2249 const struct ofp10_flow_stats_request *ofsr,
2252 fsr->aggregate = aggregate;
2253 ofputil_match_from_ofp10_match(&ofsr->match, &fsr->match);
2254 fsr->out_port = u16_to_ofp(ntohs(ofsr->out_port));
2255 fsr->out_group = OFPG11_ANY;
2256 fsr->table_id = ofsr->table_id;
2257 fsr->cookie = fsr->cookie_mask = htonll(0);
2263 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request *fsr,
2264 struct ofpbuf *b, bool aggregate)
2266 const struct ofp11_flow_stats_request *ofsr;
2269 ofsr = ofpbuf_pull(b, sizeof *ofsr);
2270 fsr->aggregate = aggregate;
2271 fsr->table_id = ofsr->table_id;
2272 error = ofputil_port_from_ofp11(ofsr->out_port, &fsr->out_port);
2276 fsr->out_group = ntohl(ofsr->out_group);
2277 fsr->cookie = ofsr->cookie;
2278 fsr->cookie_mask = ofsr->cookie_mask;
2279 error = ofputil_pull_ofp11_match(b, &fsr->match, NULL);
2288 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request *fsr,
2289 struct ofpbuf *b, bool aggregate)
2291 const struct nx_flow_stats_request *nfsr;
2294 nfsr = ofpbuf_pull(b, sizeof *nfsr);
2295 error = nx_pull_match(b, ntohs(nfsr->match_len), &fsr->match,
2296 &fsr->cookie, &fsr->cookie_mask);
2300 if (ofpbuf_size(b)) {
2301 return OFPERR_OFPBRC_BAD_LEN;
2304 fsr->aggregate = aggregate;
2305 fsr->out_port = u16_to_ofp(ntohs(nfsr->out_port));
2306 fsr->out_group = OFPG11_ANY;
2307 fsr->table_id = nfsr->table_id;
2312 /* Constructs and returns an OFPT_QUEUE_GET_CONFIG request for the specified
2313 * 'port', suitable for OpenFlow version 'version'. */
2315 ofputil_encode_queue_get_config_request(enum ofp_version version,
2318 struct ofpbuf *request;
2320 if (version == OFP10_VERSION) {
2321 struct ofp10_queue_get_config_request *qgcr10;
2323 request = ofpraw_alloc(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST,
2325 qgcr10 = ofpbuf_put_zeros(request, sizeof *qgcr10);
2326 qgcr10->port = htons(ofp_to_u16(port));
2328 struct ofp11_queue_get_config_request *qgcr11;
2330 request = ofpraw_alloc(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST,
2332 qgcr11 = ofpbuf_put_zeros(request, sizeof *qgcr11);
2333 qgcr11->port = ofputil_port_to_ofp11(port);
2339 /* Parses OFPT_QUEUE_GET_CONFIG request 'oh', storing the port specified by the
2340 * request into '*port'. Returns 0 if successful, otherwise an OpenFlow error
2343 ofputil_decode_queue_get_config_request(const struct ofp_header *oh,
2346 const struct ofp10_queue_get_config_request *qgcr10;
2347 const struct ofp11_queue_get_config_request *qgcr11;
2351 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2352 raw = ofpraw_pull_assert(&b);
2354 switch ((int) raw) {
2355 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2356 qgcr10 = ofpbuf_data(&b);
2357 *port = u16_to_ofp(ntohs(qgcr10->port));
2360 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2361 qgcr11 = ofpbuf_data(&b);
2362 return ofputil_port_from_ofp11(qgcr11->port, port);
2368 /* Constructs and returns the beginning of a reply to
2369 * OFPT_QUEUE_GET_CONFIG_REQUEST 'oh'. The caller may append information about
2370 * individual queues with ofputil_append_queue_get_config_reply(). */
2372 ofputil_encode_queue_get_config_reply(const struct ofp_header *oh)
2374 struct ofp10_queue_get_config_reply *qgcr10;
2375 struct ofp11_queue_get_config_reply *qgcr11;
2376 struct ofpbuf *reply;
2382 error = ofputil_decode_queue_get_config_request(oh, &port);
2385 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2386 raw = ofpraw_pull_assert(&b);
2388 switch ((int) raw) {
2389 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST:
2390 reply = ofpraw_alloc_reply(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY,
2392 qgcr10 = ofpbuf_put_zeros(reply, sizeof *qgcr10);
2393 qgcr10->port = htons(ofp_to_u16(port));
2396 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST:
2397 reply = ofpraw_alloc_reply(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY,
2399 qgcr11 = ofpbuf_put_zeros(reply, sizeof *qgcr11);
2400 qgcr11->port = ofputil_port_to_ofp11(port);
2411 put_queue_rate(struct ofpbuf *reply, enum ofp_queue_properties property,
2414 if (rate != UINT16_MAX) {
2415 struct ofp_queue_prop_rate *oqpr;
2417 oqpr = ofpbuf_put_zeros(reply, sizeof *oqpr);
2418 oqpr->prop_header.property = htons(property);
2419 oqpr->prop_header.len = htons(sizeof *oqpr);
2420 oqpr->rate = htons(rate);
2424 /* Appends a queue description for 'queue_id' to the
2425 * OFPT_QUEUE_GET_CONFIG_REPLY already in 'oh'. */
2427 ofputil_append_queue_get_config_reply(struct ofpbuf *reply,
2428 const struct ofputil_queue_config *oqc)
2430 const struct ofp_header *oh = ofpbuf_data(reply);
2431 size_t start_ofs, len_ofs;
2434 start_ofs = ofpbuf_size(reply);
2435 if (oh->version < OFP12_VERSION) {
2436 struct ofp10_packet_queue *opq10;
2438 opq10 = ofpbuf_put_zeros(reply, sizeof *opq10);
2439 opq10->queue_id = htonl(oqc->queue_id);
2440 len_ofs = (char *) &opq10->len - (char *) ofpbuf_data(reply);
2442 struct ofp11_queue_get_config_reply *qgcr11;
2443 struct ofp12_packet_queue *opq12;
2446 qgcr11 = ofpbuf_l3(reply);
2447 port = qgcr11->port;
2449 opq12 = ofpbuf_put_zeros(reply, sizeof *opq12);
2451 opq12->queue_id = htonl(oqc->queue_id);
2452 len_ofs = (char *) &opq12->len - (char *) ofpbuf_data(reply);
2455 put_queue_rate(reply, OFPQT_MIN_RATE, oqc->min_rate);
2456 put_queue_rate(reply, OFPQT_MAX_RATE, oqc->max_rate);
2458 len = ofpbuf_at(reply, len_ofs, sizeof *len);
2459 *len = htons(ofpbuf_size(reply) - start_ofs);
2462 /* Decodes the initial part of an OFPT_QUEUE_GET_CONFIG_REPLY from 'reply' and
2463 * stores in '*port' the port that the reply is about. The caller may call
2464 * ofputil_pull_queue_get_config_reply() to obtain information about individual
2465 * queues included in the reply. Returns 0 if successful, otherwise an
2468 ofputil_decode_queue_get_config_reply(struct ofpbuf *reply, ofp_port_t *port)
2470 const struct ofp10_queue_get_config_reply *qgcr10;
2471 const struct ofp11_queue_get_config_reply *qgcr11;
2474 raw = ofpraw_pull_assert(reply);
2475 switch ((int) raw) {
2476 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY:
2477 qgcr10 = ofpbuf_pull(reply, sizeof *qgcr10);
2478 *port = u16_to_ofp(ntohs(qgcr10->port));
2481 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY:
2482 qgcr11 = ofpbuf_pull(reply, sizeof *qgcr11);
2483 return ofputil_port_from_ofp11(qgcr11->port, port);
2490 parse_queue_rate(const struct ofp_queue_prop_header *hdr, uint16_t *rate)
2492 const struct ofp_queue_prop_rate *oqpr;
2494 if (hdr->len == htons(sizeof *oqpr)) {
2495 oqpr = (const struct ofp_queue_prop_rate *) hdr;
2496 *rate = ntohs(oqpr->rate);
2499 return OFPERR_OFPBRC_BAD_LEN;
2503 /* Decodes information about a queue from the OFPT_QUEUE_GET_CONFIG_REPLY in
2504 * 'reply' and stores it in '*queue'. ofputil_decode_queue_get_config_reply()
2505 * must already have pulled off the main header.
2507 * This function returns EOF if the last queue has already been decoded, 0 if a
2508 * queue was successfully decoded into '*queue', or an ofperr if there was a
2509 * problem decoding 'reply'. */
2511 ofputil_pull_queue_get_config_reply(struct ofpbuf *reply,
2512 struct ofputil_queue_config *queue)
2514 const struct ofp_header *oh;
2515 unsigned int opq_len;
2518 if (!ofpbuf_size(reply)) {
2522 queue->min_rate = UINT16_MAX;
2523 queue->max_rate = UINT16_MAX;
2526 if (oh->version < OFP12_VERSION) {
2527 const struct ofp10_packet_queue *opq10;
2529 opq10 = ofpbuf_try_pull(reply, sizeof *opq10);
2531 return OFPERR_OFPBRC_BAD_LEN;
2533 queue->queue_id = ntohl(opq10->queue_id);
2534 len = ntohs(opq10->len);
2535 opq_len = sizeof *opq10;
2537 const struct ofp12_packet_queue *opq12;
2539 opq12 = ofpbuf_try_pull(reply, sizeof *opq12);
2541 return OFPERR_OFPBRC_BAD_LEN;
2543 queue->queue_id = ntohl(opq12->queue_id);
2544 len = ntohs(opq12->len);
2545 opq_len = sizeof *opq12;
2548 if (len < opq_len || len > ofpbuf_size(reply) + opq_len || len % 8) {
2549 return OFPERR_OFPBRC_BAD_LEN;
2554 const struct ofp_queue_prop_header *hdr;
2555 unsigned int property;
2556 unsigned int prop_len;
2557 enum ofperr error = 0;
2559 hdr = ofpbuf_at_assert(reply, 0, sizeof *hdr);
2560 prop_len = ntohs(hdr->len);
2561 if (prop_len < sizeof *hdr || prop_len > ofpbuf_size(reply) || prop_len % 8) {
2562 return OFPERR_OFPBRC_BAD_LEN;
2565 property = ntohs(hdr->property);
2567 case OFPQT_MIN_RATE:
2568 error = parse_queue_rate(hdr, &queue->min_rate);
2571 case OFPQT_MAX_RATE:
2572 error = parse_queue_rate(hdr, &queue->max_rate);
2576 VLOG_INFO_RL(&bad_ofmsg_rl, "unknown queue property %u", property);
2583 ofpbuf_pull(reply, prop_len);
2589 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
2590 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
2591 * successful, otherwise an OpenFlow error code. */
2593 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request *fsr,
2594 const struct ofp_header *oh)
2599 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2600 raw = ofpraw_pull_assert(&b);
2601 switch ((int) raw) {
2602 case OFPRAW_OFPST10_FLOW_REQUEST:
2603 return ofputil_decode_ofpst10_flow_request(fsr, ofpbuf_data(&b), false);
2605 case OFPRAW_OFPST10_AGGREGATE_REQUEST:
2606 return ofputil_decode_ofpst10_flow_request(fsr, ofpbuf_data(&b), true);
2608 case OFPRAW_OFPST11_FLOW_REQUEST:
2609 return ofputil_decode_ofpst11_flow_request(fsr, &b, false);
2611 case OFPRAW_OFPST11_AGGREGATE_REQUEST:
2612 return ofputil_decode_ofpst11_flow_request(fsr, &b, true);
2614 case OFPRAW_NXST_FLOW_REQUEST:
2615 return ofputil_decode_nxst_flow_request(fsr, &b, false);
2617 case OFPRAW_NXST_AGGREGATE_REQUEST:
2618 return ofputil_decode_nxst_flow_request(fsr, &b, true);
2621 /* Hey, the caller lied. */
2626 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
2627 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
2628 * 'protocol', and returns the message. */
2630 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request *fsr,
2631 enum ofputil_protocol protocol)
2637 case OFPUTIL_P_OF11_STD:
2638 case OFPUTIL_P_OF12_OXM:
2639 case OFPUTIL_P_OF13_OXM:
2640 case OFPUTIL_P_OF14_OXM: {
2641 struct ofp11_flow_stats_request *ofsr;
2643 raw = (fsr->aggregate
2644 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
2645 : OFPRAW_OFPST11_FLOW_REQUEST);
2646 msg = ofpraw_alloc(raw, ofputil_protocol_to_ofp_version(protocol),
2647 ofputil_match_typical_len(protocol));
2648 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2649 ofsr->table_id = fsr->table_id;
2650 ofsr->out_port = ofputil_port_to_ofp11(fsr->out_port);
2651 ofsr->out_group = htonl(fsr->out_group);
2652 ofsr->cookie = fsr->cookie;
2653 ofsr->cookie_mask = fsr->cookie_mask;
2654 ofputil_put_ofp11_match(msg, &fsr->match, protocol);
2658 case OFPUTIL_P_OF10_STD:
2659 case OFPUTIL_P_OF10_STD_TID: {
2660 struct ofp10_flow_stats_request *ofsr;
2662 raw = (fsr->aggregate
2663 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
2664 : OFPRAW_OFPST10_FLOW_REQUEST);
2665 msg = ofpraw_alloc(raw, OFP10_VERSION, 0);
2666 ofsr = ofpbuf_put_zeros(msg, sizeof *ofsr);
2667 ofputil_match_to_ofp10_match(&fsr->match, &ofsr->match);
2668 ofsr->table_id = fsr->table_id;
2669 ofsr->out_port = htons(ofp_to_u16(fsr->out_port));
2673 case OFPUTIL_P_OF10_NXM:
2674 case OFPUTIL_P_OF10_NXM_TID: {
2675 struct nx_flow_stats_request *nfsr;
2678 raw = (fsr->aggregate
2679 ? OFPRAW_NXST_AGGREGATE_REQUEST
2680 : OFPRAW_NXST_FLOW_REQUEST);
2681 msg = ofpraw_alloc(raw, OFP10_VERSION, NXM_TYPICAL_LEN);
2682 ofpbuf_put_zeros(msg, sizeof *nfsr);
2683 match_len = nx_put_match(msg, &fsr->match,
2684 fsr->cookie, fsr->cookie_mask);
2686 nfsr = ofpbuf_l3(msg);
2687 nfsr->out_port = htons(ofp_to_u16(fsr->out_port));
2688 nfsr->match_len = htons(match_len);
2689 nfsr->table_id = fsr->table_id;
2700 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
2701 * ofputil_flow_stats in 'fs'.
2703 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
2704 * OpenFlow message. Calling this function multiple times for a single 'msg'
2705 * iterates through the replies. The caller must initially leave 'msg''s layer
2706 * pointers null and not modify them between calls.
2708 * Most switches don't send the values needed to populate fs->idle_age and
2709 * fs->hard_age, so those members will usually be set to 0. If the switch from
2710 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
2711 * 'flow_age_extension' as true so that the contents of 'msg' determine the
2712 * 'idle_age' and 'hard_age' members in 'fs'.
2714 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
2715 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
2716 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
2718 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2719 * otherwise a positive errno value. */
2721 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats *fs,
2723 bool flow_age_extension,
2724 struct ofpbuf *ofpacts)
2726 const struct ofp_header *oh;
2731 ? ofpraw_decode(&raw, msg->frame)
2732 : ofpraw_pull(&raw, msg));
2738 if (!ofpbuf_size(msg)) {
2740 } else if (raw == OFPRAW_OFPST11_FLOW_REPLY
2741 || raw == OFPRAW_OFPST13_FLOW_REPLY) {
2742 const struct ofp11_flow_stats *ofs;
2744 uint16_t padded_match_len;
2746 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2748 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
2749 "bytes at end", ofpbuf_size(msg));
2753 length = ntohs(ofs->length);
2754 if (length < sizeof *ofs) {
2755 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2756 "length %"PRIuSIZE, length);
2760 if (ofputil_pull_ofp11_match(msg, &fs->match, &padded_match_len)) {
2761 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad match");
2765 if (ofpacts_pull_openflow_instructions(msg, length - sizeof *ofs -
2766 padded_match_len, oh->version,
2768 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply bad instructions");
2772 fs->priority = ntohs(ofs->priority);
2773 fs->table_id = ofs->table_id;
2774 fs->duration_sec = ntohl(ofs->duration_sec);
2775 fs->duration_nsec = ntohl(ofs->duration_nsec);
2776 fs->idle_timeout = ntohs(ofs->idle_timeout);
2777 fs->hard_timeout = ntohs(ofs->hard_timeout);
2778 if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
2779 error = ofputil_decode_flow_mod_flags(ofs->flags, -1, oh->version,
2789 fs->cookie = ofs->cookie;
2790 fs->packet_count = ntohll(ofs->packet_count);
2791 fs->byte_count = ntohll(ofs->byte_count);
2792 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
2793 const struct ofp10_flow_stats *ofs;
2796 ofs = ofpbuf_try_pull(msg, sizeof *ofs);
2798 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply has %"PRIu32" leftover "
2799 "bytes at end", ofpbuf_size(msg));
2803 length = ntohs(ofs->length);
2804 if (length < sizeof *ofs) {
2805 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_FLOW reply claims invalid "
2806 "length %"PRIuSIZE, length);
2810 if (ofpacts_pull_openflow_actions(msg, length - sizeof *ofs,
2811 oh->version, ofpacts)) {
2815 fs->cookie = get_32aligned_be64(&ofs->cookie);
2816 ofputil_match_from_ofp10_match(&ofs->match, &fs->match);
2817 fs->priority = ntohs(ofs->priority);
2818 fs->table_id = ofs->table_id;
2819 fs->duration_sec = ntohl(ofs->duration_sec);
2820 fs->duration_nsec = ntohl(ofs->duration_nsec);
2821 fs->idle_timeout = ntohs(ofs->idle_timeout);
2822 fs->hard_timeout = ntohs(ofs->hard_timeout);
2825 fs->packet_count = ntohll(get_32aligned_be64(&ofs->packet_count));
2826 fs->byte_count = ntohll(get_32aligned_be64(&ofs->byte_count));
2828 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
2829 const struct nx_flow_stats *nfs;
2830 size_t match_len, actions_len, length;
2832 nfs = ofpbuf_try_pull(msg, sizeof *nfs);
2834 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply has %"PRIu32" leftover "
2835 "bytes at end", ofpbuf_size(msg));
2839 length = ntohs(nfs->length);
2840 match_len = ntohs(nfs->match_len);
2841 if (length < sizeof *nfs + ROUND_UP(match_len, 8)) {
2842 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW reply with match_len=%"PRIuSIZE" "
2843 "claims invalid length %"PRIuSIZE, match_len, length);
2846 if (nx_pull_match(msg, match_len, &fs->match, NULL, NULL)) {
2850 actions_len = length - sizeof *nfs - ROUND_UP(match_len, 8);
2851 if (ofpacts_pull_openflow_actions(msg, actions_len, oh->version,
2856 fs->cookie = nfs->cookie;
2857 fs->table_id = nfs->table_id;
2858 fs->duration_sec = ntohl(nfs->duration_sec);
2859 fs->duration_nsec = ntohl(nfs->duration_nsec);
2860 fs->priority = ntohs(nfs->priority);
2861 fs->idle_timeout = ntohs(nfs->idle_timeout);
2862 fs->hard_timeout = ntohs(nfs->hard_timeout);
2865 if (flow_age_extension) {
2866 if (nfs->idle_age) {
2867 fs->idle_age = ntohs(nfs->idle_age) - 1;
2869 if (nfs->hard_age) {
2870 fs->hard_age = ntohs(nfs->hard_age) - 1;
2873 fs->packet_count = ntohll(nfs->packet_count);
2874 fs->byte_count = ntohll(nfs->byte_count);
2880 fs->ofpacts = ofpbuf_data(ofpacts);
2881 fs->ofpacts_len = ofpbuf_size(ofpacts);
2886 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
2888 * We use this in situations where OVS internally uses UINT64_MAX to mean
2889 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
2891 unknown_to_zero(uint64_t count)
2893 return count != UINT64_MAX ? count : 0;
2896 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
2897 * those already present in the list of ofpbufs in 'replies'. 'replies' should
2898 * have been initialized with ofpmp_init(). */
2900 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats *fs,
2901 struct list *replies)
2903 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
2904 size_t start_ofs = ofpbuf_size(reply);
2905 enum ofp_version version = ofpmp_version(replies);
2906 enum ofpraw raw = ofpmp_decode_raw(replies);
2908 if (raw == OFPRAW_OFPST11_FLOW_REPLY || raw == OFPRAW_OFPST13_FLOW_REPLY) {
2909 struct ofp11_flow_stats *ofs;
2911 ofpbuf_put_uninit(reply, sizeof *ofs);
2912 oxm_put_match(reply, &fs->match);
2913 ofpacts_put_openflow_instructions(fs->ofpacts, fs->ofpacts_len, reply,
2916 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
2917 ofs->length = htons(ofpbuf_size(reply) - start_ofs);
2918 ofs->table_id = fs->table_id;
2920 ofs->duration_sec = htonl(fs->duration_sec);
2921 ofs->duration_nsec = htonl(fs->duration_nsec);
2922 ofs->priority = htons(fs->priority);
2923 ofs->idle_timeout = htons(fs->idle_timeout);
2924 ofs->hard_timeout = htons(fs->hard_timeout);
2925 if (raw == OFPRAW_OFPST13_FLOW_REPLY) {
2926 ofs->flags = ofputil_encode_flow_mod_flags(fs->flags, version);
2930 memset(ofs->pad2, 0, sizeof ofs->pad2);
2931 ofs->cookie = fs->cookie;
2932 ofs->packet_count = htonll(unknown_to_zero(fs->packet_count));
2933 ofs->byte_count = htonll(unknown_to_zero(fs->byte_count));
2934 } else if (raw == OFPRAW_OFPST10_FLOW_REPLY) {
2935 struct ofp10_flow_stats *ofs;
2937 ofpbuf_put_uninit(reply, sizeof *ofs);
2938 ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
2940 ofs = ofpbuf_at_assert(reply, start_ofs, sizeof *ofs);
2941 ofs->length = htons(ofpbuf_size(reply) - start_ofs);
2942 ofs->table_id = fs->table_id;
2944 ofputil_match_to_ofp10_match(&fs->match, &ofs->match);
2945 ofs->duration_sec = htonl(fs->duration_sec);
2946 ofs->duration_nsec = htonl(fs->duration_nsec);
2947 ofs->priority = htons(fs->priority);
2948 ofs->idle_timeout = htons(fs->idle_timeout);
2949 ofs->hard_timeout = htons(fs->hard_timeout);
2950 memset(ofs->pad2, 0, sizeof ofs->pad2);
2951 put_32aligned_be64(&ofs->cookie, fs->cookie);
2952 put_32aligned_be64(&ofs->packet_count,
2953 htonll(unknown_to_zero(fs->packet_count)));
2954 put_32aligned_be64(&ofs->byte_count,
2955 htonll(unknown_to_zero(fs->byte_count)));
2956 } else if (raw == OFPRAW_NXST_FLOW_REPLY) {
2957 struct nx_flow_stats *nfs;
2960 ofpbuf_put_uninit(reply, sizeof *nfs);
2961 match_len = nx_put_match(reply, &fs->match, 0, 0);
2962 ofpacts_put_openflow_actions(fs->ofpacts, fs->ofpacts_len, reply,
2964 nfs = ofpbuf_at_assert(reply, start_ofs, sizeof *nfs);
2965 nfs->length = htons(ofpbuf_size(reply) - start_ofs);
2966 nfs->table_id = fs->table_id;
2968 nfs->duration_sec = htonl(fs->duration_sec);
2969 nfs->duration_nsec = htonl(fs->duration_nsec);
2970 nfs->priority = htons(fs->priority);
2971 nfs->idle_timeout = htons(fs->idle_timeout);
2972 nfs->hard_timeout = htons(fs->hard_timeout);
2973 nfs->idle_age = htons(fs->idle_age < 0 ? 0
2974 : fs->idle_age < UINT16_MAX ? fs->idle_age + 1
2976 nfs->hard_age = htons(fs->hard_age < 0 ? 0
2977 : fs->hard_age < UINT16_MAX ? fs->hard_age + 1
2979 nfs->match_len = htons(match_len);
2980 nfs->cookie = fs->cookie;
2981 nfs->packet_count = htonll(fs->packet_count);
2982 nfs->byte_count = htonll(fs->byte_count);
2987 ofpmp_postappend(replies, start_ofs);
2990 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
2991 * NXST_AGGREGATE reply matching 'request', and returns the message. */
2993 ofputil_encode_aggregate_stats_reply(
2994 const struct ofputil_aggregate_stats *stats,
2995 const struct ofp_header *request)
2997 struct ofp_aggregate_stats_reply *asr;
2998 uint64_t packet_count;
2999 uint64_t byte_count;
3003 ofpraw_decode(&raw, request);
3004 if (raw == OFPRAW_OFPST10_AGGREGATE_REQUEST) {
3005 packet_count = unknown_to_zero(stats->packet_count);
3006 byte_count = unknown_to_zero(stats->byte_count);
3008 packet_count = stats->packet_count;
3009 byte_count = stats->byte_count;
3012 msg = ofpraw_alloc_stats_reply(request, 0);
3013 asr = ofpbuf_put_zeros(msg, sizeof *asr);
3014 put_32aligned_be64(&asr->packet_count, htonll(packet_count));
3015 put_32aligned_be64(&asr->byte_count, htonll(byte_count));
3016 asr->flow_count = htonl(stats->flow_count);
3022 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats *stats,
3023 const struct ofp_header *reply)
3025 struct ofp_aggregate_stats_reply *asr;
3028 ofpbuf_use_const(&msg, reply, ntohs(reply->length));
3029 ofpraw_pull_assert(&msg);
3031 asr = ofpbuf_l3(&msg);
3032 stats->packet_count = ntohll(get_32aligned_be64(&asr->packet_count));
3033 stats->byte_count = ntohll(get_32aligned_be64(&asr->byte_count));
3034 stats->flow_count = ntohl(asr->flow_count);
3039 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
3040 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
3041 * an OpenFlow error code. */
3043 ofputil_decode_flow_removed(struct ofputil_flow_removed *fr,
3044 const struct ofp_header *oh)
3049 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3050 raw = ofpraw_pull_assert(&b);
3051 if (raw == OFPRAW_OFPT11_FLOW_REMOVED) {
3052 const struct ofp12_flow_removed *ofr;
3055 ofr = ofpbuf_pull(&b, sizeof *ofr);
3057 error = ofputil_pull_ofp11_match(&b, &fr->match, NULL);
3062 fr->priority = ntohs(ofr->priority);
3063 fr->cookie = ofr->cookie;
3064 fr->reason = ofr->reason;
3065 fr->table_id = ofr->table_id;
3066 fr->duration_sec = ntohl(ofr->duration_sec);
3067 fr->duration_nsec = ntohl(ofr->duration_nsec);
3068 fr->idle_timeout = ntohs(ofr->idle_timeout);
3069 fr->hard_timeout = ntohs(ofr->hard_timeout);
3070 fr->packet_count = ntohll(ofr->packet_count);
3071 fr->byte_count = ntohll(ofr->byte_count);
3072 } else if (raw == OFPRAW_OFPT10_FLOW_REMOVED) {
3073 const struct ofp10_flow_removed *ofr;
3075 ofr = ofpbuf_pull(&b, sizeof *ofr);
3077 ofputil_match_from_ofp10_match(&ofr->match, &fr->match);
3078 fr->priority = ntohs(ofr->priority);
3079 fr->cookie = ofr->cookie;
3080 fr->reason = ofr->reason;
3082 fr->duration_sec = ntohl(ofr->duration_sec);
3083 fr->duration_nsec = ntohl(ofr->duration_nsec);
3084 fr->idle_timeout = ntohs(ofr->idle_timeout);
3085 fr->hard_timeout = 0;
3086 fr->packet_count = ntohll(ofr->packet_count);
3087 fr->byte_count = ntohll(ofr->byte_count);
3088 } else if (raw == OFPRAW_NXT_FLOW_REMOVED) {
3089 struct nx_flow_removed *nfr;
3092 nfr = ofpbuf_pull(&b, sizeof *nfr);
3093 error = nx_pull_match(&b, ntohs(nfr->match_len), &fr->match,
3098 if (ofpbuf_size(&b)) {
3099 return OFPERR_OFPBRC_BAD_LEN;
3102 fr->priority = ntohs(nfr->priority);
3103 fr->cookie = nfr->cookie;
3104 fr->reason = nfr->reason;
3105 fr->table_id = nfr->table_id ? nfr->table_id - 1 : 255;
3106 fr->duration_sec = ntohl(nfr->duration_sec);
3107 fr->duration_nsec = ntohl(nfr->duration_nsec);
3108 fr->idle_timeout = ntohs(nfr->idle_timeout);
3109 fr->hard_timeout = 0;
3110 fr->packet_count = ntohll(nfr->packet_count);
3111 fr->byte_count = ntohll(nfr->byte_count);
3119 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
3120 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
3123 ofputil_encode_flow_removed(const struct ofputil_flow_removed *fr,
3124 enum ofputil_protocol protocol)
3129 case OFPUTIL_P_OF11_STD:
3130 case OFPUTIL_P_OF12_OXM:
3131 case OFPUTIL_P_OF13_OXM:
3132 case OFPUTIL_P_OF14_OXM: {
3133 struct ofp12_flow_removed *ofr;
3135 msg = ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED,
3136 ofputil_protocol_to_ofp_version(protocol),
3138 ofputil_match_typical_len(protocol));
3139 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3140 ofr->cookie = fr->cookie;
3141 ofr->priority = htons(fr->priority);
3142 ofr->reason = fr->reason;
3143 ofr->table_id = fr->table_id;
3144 ofr->duration_sec = htonl(fr->duration_sec);
3145 ofr->duration_nsec = htonl(fr->duration_nsec);
3146 ofr->idle_timeout = htons(fr->idle_timeout);
3147 ofr->hard_timeout = htons(fr->hard_timeout);
3148 ofr->packet_count = htonll(fr->packet_count);
3149 ofr->byte_count = htonll(fr->byte_count);
3150 ofputil_put_ofp11_match(msg, &fr->match, protocol);
3154 case OFPUTIL_P_OF10_STD:
3155 case OFPUTIL_P_OF10_STD_TID: {
3156 struct ofp10_flow_removed *ofr;
3158 msg = ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED, OFP10_VERSION,
3160 ofr = ofpbuf_put_zeros(msg, sizeof *ofr);
3161 ofputil_match_to_ofp10_match(&fr->match, &ofr->match);
3162 ofr->cookie = fr->cookie;
3163 ofr->priority = htons(fr->priority);
3164 ofr->reason = fr->reason;
3165 ofr->duration_sec = htonl(fr->duration_sec);
3166 ofr->duration_nsec = htonl(fr->duration_nsec);
3167 ofr->idle_timeout = htons(fr->idle_timeout);
3168 ofr->packet_count = htonll(unknown_to_zero(fr->packet_count));
3169 ofr->byte_count = htonll(unknown_to_zero(fr->byte_count));
3173 case OFPUTIL_P_OF10_NXM:
3174 case OFPUTIL_P_OF10_NXM_TID: {
3175 struct nx_flow_removed *nfr;
3178 msg = ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED, OFP10_VERSION,
3179 htonl(0), NXM_TYPICAL_LEN);
3180 nfr = ofpbuf_put_zeros(msg, sizeof *nfr);
3181 match_len = nx_put_match(msg, &fr->match, 0, 0);
3183 nfr = ofpbuf_l3(msg);
3184 nfr->cookie = fr->cookie;
3185 nfr->priority = htons(fr->priority);
3186 nfr->reason = fr->reason;
3187 nfr->table_id = fr->table_id + 1;
3188 nfr->duration_sec = htonl(fr->duration_sec);
3189 nfr->duration_nsec = htonl(fr->duration_nsec);
3190 nfr->idle_timeout = htons(fr->idle_timeout);
3191 nfr->match_len = htons(match_len);
3192 nfr->packet_count = htonll(fr->packet_count);
3193 nfr->byte_count = htonll(fr->byte_count);
3205 ofputil_decode_packet_in_finish(struct ofputil_packet_in *pin,
3206 struct match *match, struct ofpbuf *b)
3208 pin->packet = ofpbuf_data(b);
3209 pin->packet_len = ofpbuf_size(b);
3211 pin->fmd.in_port = match->flow.in_port.ofp_port;
3212 pin->fmd.tun_id = match->flow.tunnel.tun_id;
3213 pin->fmd.tun_src = match->flow.tunnel.ip_src;
3214 pin->fmd.tun_dst = match->flow.tunnel.ip_dst;
3215 pin->fmd.metadata = match->flow.metadata;
3216 memcpy(pin->fmd.regs, match->flow.regs, sizeof pin->fmd.regs);
3217 pin->fmd.pkt_mark = match->flow.pkt_mark;
3221 ofputil_decode_packet_in(struct ofputil_packet_in *pin,
3222 const struct ofp_header *oh)
3227 memset(pin, 0, sizeof *pin);
3228 pin->cookie = OVS_BE64_MAX;
3230 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3231 raw = ofpraw_pull_assert(&b);
3232 if (raw == OFPRAW_OFPT13_PACKET_IN || raw == OFPRAW_OFPT12_PACKET_IN) {
3233 const struct ofp13_packet_in *opi;
3236 size_t packet_in_size;
3238 if (raw == OFPRAW_OFPT12_PACKET_IN) {
3239 packet_in_size = sizeof (struct ofp12_packet_in);
3241 packet_in_size = sizeof (struct ofp13_packet_in);
3244 opi = ofpbuf_pull(&b, packet_in_size);
3245 error = oxm_pull_match_loose(&b, &match);
3250 if (!ofpbuf_try_pull(&b, 2)) {
3251 return OFPERR_OFPBRC_BAD_LEN;
3254 pin->reason = opi->pi.reason;
3255 pin->table_id = opi->pi.table_id;
3256 pin->buffer_id = ntohl(opi->pi.buffer_id);
3257 pin->total_len = ntohs(opi->pi.total_len);
3259 if (raw == OFPRAW_OFPT13_PACKET_IN) {
3260 pin->cookie = opi->cookie;
3263 ofputil_decode_packet_in_finish(pin, &match, &b);
3264 } else if (raw == OFPRAW_OFPT10_PACKET_IN) {
3265 const struct ofp10_packet_in *opi;
3267 opi = ofpbuf_pull(&b, offsetof(struct ofp10_packet_in, data));
3269 pin->packet = opi->data;
3270 pin->packet_len = ofpbuf_size(&b);
3272 pin->fmd.in_port = u16_to_ofp(ntohs(opi->in_port));
3273 pin->reason = opi->reason;
3274 pin->buffer_id = ntohl(opi->buffer_id);
3275 pin->total_len = ntohs(opi->total_len);
3276 } else if (raw == OFPRAW_OFPT11_PACKET_IN) {
3277 const struct ofp11_packet_in *opi;
3280 opi = ofpbuf_pull(&b, sizeof *opi);
3282 pin->packet = ofpbuf_data(&b);
3283 pin->packet_len = ofpbuf_size(&b);
3285 pin->buffer_id = ntohl(opi->buffer_id);
3286 error = ofputil_port_from_ofp11(opi->in_port, &pin->fmd.in_port);
3290 pin->total_len = ntohs(opi->total_len);
3291 pin->reason = opi->reason;
3292 pin->table_id = opi->table_id;
3293 } else if (raw == OFPRAW_NXT_PACKET_IN) {
3294 const struct nx_packet_in *npi;
3298 npi = ofpbuf_pull(&b, sizeof *npi);
3299 error = nx_pull_match_loose(&b, ntohs(npi->match_len), &match, NULL,
3305 if (!ofpbuf_try_pull(&b, 2)) {
3306 return OFPERR_OFPBRC_BAD_LEN;
3309 pin->reason = npi->reason;
3310 pin->table_id = npi->table_id;
3311 pin->cookie = npi->cookie;
3313 pin->buffer_id = ntohl(npi->buffer_id);
3314 pin->total_len = ntohs(npi->total_len);
3316 ofputil_decode_packet_in_finish(pin, &match, &b);
3325 ofputil_packet_in_to_match(const struct ofputil_packet_in *pin,
3326 struct match *match)
3330 match_init_catchall(match);
3331 if (pin->fmd.tun_id != htonll(0)) {
3332 match_set_tun_id(match, pin->fmd.tun_id);
3334 if (pin->fmd.tun_src != htonl(0)) {
3335 match_set_tun_src(match, pin->fmd.tun_src);
3337 if (pin->fmd.tun_dst != htonl(0)) {
3338 match_set_tun_dst(match, pin->fmd.tun_dst);
3340 if (pin->fmd.metadata != htonll(0)) {
3341 match_set_metadata(match, pin->fmd.metadata);
3344 for (i = 0; i < FLOW_N_REGS; i++) {
3345 if (pin->fmd.regs[i]) {
3346 match_set_reg(match, i, pin->fmd.regs[i]);
3350 if (pin->fmd.pkt_mark != 0) {
3351 match_set_pkt_mark(match, pin->fmd.pkt_mark);
3354 match_set_in_port(match, pin->fmd.in_port);
3357 static struct ofpbuf *
3358 ofputil_encode_ofp10_packet_in(const struct ofputil_packet_in *pin)
3360 struct ofp10_packet_in *opi;
3361 struct ofpbuf *packet;
3363 packet = ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN, OFP10_VERSION,
3364 htonl(0), pin->packet_len);
3365 opi = ofpbuf_put_zeros(packet, offsetof(struct ofp10_packet_in, data));
3366 opi->total_len = htons(pin->total_len);
3367 opi->in_port = htons(ofp_to_u16(pin->fmd.in_port));
3368 opi->reason = pin->reason;
3369 opi->buffer_id = htonl(pin->buffer_id);
3371 ofpbuf_put(packet, pin->packet, pin->packet_len);
3376 static struct ofpbuf *
3377 ofputil_encode_nx_packet_in(const struct ofputil_packet_in *pin)
3379 struct nx_packet_in *npi;
3380 struct ofpbuf *packet;
3384 ofputil_packet_in_to_match(pin, &match);
3386 /* The final argument is just an estimate of the space required. */
3387 packet = ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN, OFP10_VERSION,
3388 htonl(0), (sizeof(struct flow_metadata) * 2
3389 + 2 + pin->packet_len));
3390 ofpbuf_put_zeros(packet, sizeof *npi);
3391 match_len = nx_put_match(packet, &match, 0, 0);
3392 ofpbuf_put_zeros(packet, 2);
3393 ofpbuf_put(packet, pin->packet, pin->packet_len);
3395 npi = ofpbuf_l3(packet);
3396 npi->buffer_id = htonl(pin->buffer_id);
3397 npi->total_len = htons(pin->total_len);
3398 npi->reason = pin->reason;
3399 npi->table_id = pin->table_id;
3400 npi->cookie = pin->cookie;
3401 npi->match_len = htons(match_len);
3406 static struct ofpbuf *
3407 ofputil_encode_ofp11_packet_in(const struct ofputil_packet_in *pin)
3409 struct ofp11_packet_in *opi;
3410 struct ofpbuf *packet;
3412 packet = ofpraw_alloc_xid(OFPRAW_OFPT11_PACKET_IN, OFP11_VERSION,
3413 htonl(0), pin->packet_len);
3414 opi = ofpbuf_put_zeros(packet, sizeof *opi);
3415 opi->buffer_id = htonl(pin->buffer_id);
3416 opi->in_port = ofputil_port_to_ofp11(pin->fmd.in_port);
3417 opi->in_phy_port = opi->in_port;
3418 opi->total_len = htons(pin->total_len);
3419 opi->reason = pin->reason;
3420 opi->table_id = pin->table_id;
3422 ofpbuf_put(packet, pin->packet, pin->packet_len);
3427 static struct ofpbuf *
3428 ofputil_encode_ofp12_packet_in(const struct ofputil_packet_in *pin,
3429 enum ofputil_protocol protocol)
3431 struct ofp13_packet_in *opi;
3433 enum ofpraw packet_in_raw;
3434 enum ofp_version packet_in_version;
3435 size_t packet_in_size;
3436 struct ofpbuf *packet;
3438 if (protocol == OFPUTIL_P_OF12_OXM) {
3439 packet_in_raw = OFPRAW_OFPT12_PACKET_IN;
3440 packet_in_version = OFP12_VERSION;
3441 packet_in_size = sizeof (struct ofp12_packet_in);
3443 packet_in_raw = OFPRAW_OFPT13_PACKET_IN;
3444 packet_in_version = OFP13_VERSION;
3445 packet_in_size = sizeof (struct ofp13_packet_in);
3448 ofputil_packet_in_to_match(pin, &match);
3450 /* The final argument is just an estimate of the space required. */
3451 packet = ofpraw_alloc_xid(packet_in_raw, packet_in_version,
3452 htonl(0), (sizeof(struct flow_metadata) * 2
3453 + 2 + pin->packet_len));
3454 ofpbuf_put_zeros(packet, packet_in_size);
3455 oxm_put_match(packet, &match);
3456 ofpbuf_put_zeros(packet, 2);
3457 ofpbuf_put(packet, pin->packet, pin->packet_len);
3459 opi = ofpbuf_l3(packet);
3460 opi->pi.buffer_id = htonl(pin->buffer_id);
3461 opi->pi.total_len = htons(pin->total_len);
3462 opi->pi.reason = pin->reason;
3463 opi->pi.table_id = pin->table_id;
3464 if (protocol == OFPUTIL_P_OF13_OXM) {
3465 opi->cookie = pin->cookie;
3471 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
3472 * in the format specified by 'packet_in_format'. */
3474 ofputil_encode_packet_in(const struct ofputil_packet_in *pin,
3475 enum ofputil_protocol protocol,
3476 enum nx_packet_in_format packet_in_format)
3478 struct ofpbuf *packet;
3481 case OFPUTIL_P_OF10_STD:
3482 case OFPUTIL_P_OF10_STD_TID:
3483 case OFPUTIL_P_OF10_NXM:
3484 case OFPUTIL_P_OF10_NXM_TID:
3485 packet = (packet_in_format == NXPIF_NXM
3486 ? ofputil_encode_nx_packet_in(pin)
3487 : ofputil_encode_ofp10_packet_in(pin));
3490 case OFPUTIL_P_OF11_STD:
3491 packet = ofputil_encode_ofp11_packet_in(pin);
3494 case OFPUTIL_P_OF12_OXM:
3495 case OFPUTIL_P_OF13_OXM:
3496 case OFPUTIL_P_OF14_OXM:
3497 packet = ofputil_encode_ofp12_packet_in(pin, protocol);
3504 ofpmsg_update_length(packet);
3508 /* Returns a string form of 'reason'. The return value is either a statically
3509 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
3510 * 'bufsize' should be at least OFPUTIL_PACKET_IN_REASON_BUFSIZE. */
3512 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason,
3513 char *reasonbuf, size_t bufsize)
3520 case OFPR_INVALID_TTL:
3521 return "invalid_ttl";
3523 case OFPR_N_REASONS:
3525 snprintf(reasonbuf, bufsize, "%d", (int) reason);
3531 ofputil_packet_in_reason_from_string(const char *s,
3532 enum ofp_packet_in_reason *reason)
3536 for (i = 0; i < OFPR_N_REASONS; i++) {
3537 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
3538 const char *reason_s;
3540 reason_s = ofputil_packet_in_reason_to_string(i, reasonbuf,
3542 if (!strcasecmp(s, reason_s)) {
3550 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
3553 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
3554 * message's actions. The caller must initialize 'ofpacts' and retains
3555 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
3557 * Returns 0 if successful, otherwise an OFPERR_* value. */
3559 ofputil_decode_packet_out(struct ofputil_packet_out *po,
3560 const struct ofp_header *oh,
3561 struct ofpbuf *ofpacts)
3566 ofpbuf_use_const(&b, oh, ntohs(oh->length));
3567 raw = ofpraw_pull_assert(&b);
3569 if (raw == OFPRAW_OFPT11_PACKET_OUT) {
3571 const struct ofp11_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
3573 po->buffer_id = ntohl(opo->buffer_id);
3574 error = ofputil_port_from_ofp11(opo->in_port, &po->in_port);
3579 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
3580 oh->version, ofpacts);
3584 } else if (raw == OFPRAW_OFPT10_PACKET_OUT) {
3586 const struct ofp10_packet_out *opo = ofpbuf_pull(&b, sizeof *opo);
3588 po->buffer_id = ntohl(opo->buffer_id);
3589 po->in_port = u16_to_ofp(ntohs(opo->in_port));
3591 error = ofpacts_pull_openflow_actions(&b, ntohs(opo->actions_len),
3592 oh->version, ofpacts);
3600 if (ofp_to_u16(po->in_port) >= ofp_to_u16(OFPP_MAX)
3601 && po->in_port != OFPP_LOCAL
3602 && po->in_port != OFPP_NONE && po->in_port != OFPP_CONTROLLER) {
3603 VLOG_WARN_RL(&bad_ofmsg_rl, "packet-out has bad input port %#"PRIx16,
3605 return OFPERR_OFPBRC_BAD_PORT;
3608 po->ofpacts = ofpbuf_data(ofpacts);
3609 po->ofpacts_len = ofpbuf_size(ofpacts);
3611 if (po->buffer_id == UINT32_MAX) {
3612 po->packet = ofpbuf_data(&b);
3613 po->packet_len = ofpbuf_size(&b);
3622 /* ofputil_phy_port */
3624 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
3625 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
3626 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
3627 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
3628 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
3629 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
3630 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
3631 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
3633 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
3634 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == (OFPPF10_COPPER << 4));
3635 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == (OFPPF10_FIBER << 4));
3636 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == (OFPPF10_AUTONEG << 4));
3637 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == (OFPPF10_PAUSE << 4));
3638 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == (OFPPF10_PAUSE_ASYM << 4));
3640 static enum netdev_features
3641 netdev_port_features_from_ofp10(ovs_be32 ofp10_)
3643 uint32_t ofp10 = ntohl(ofp10_);
3644 return (ofp10 & 0x7f) | ((ofp10 & 0xf80) << 4);
3648 netdev_port_features_to_ofp10(enum netdev_features features)
3650 return htonl((features & 0x7f) | ((features & 0xf800) >> 4));
3653 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD == OFPPF_10MB_HD); /* bit 0 */
3654 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD == OFPPF_10MB_FD); /* bit 1 */
3655 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD == OFPPF_100MB_HD); /* bit 2 */
3656 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD == OFPPF_100MB_FD); /* bit 3 */
3657 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD == OFPPF_1GB_HD); /* bit 4 */
3658 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD == OFPPF_1GB_FD); /* bit 5 */
3659 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD == OFPPF_10GB_FD); /* bit 6 */
3660 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD == OFPPF11_40GB_FD); /* bit 7 */
3661 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD == OFPPF11_100GB_FD); /* bit 8 */
3662 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD == OFPPF11_1TB_FD); /* bit 9 */
3663 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER == OFPPF11_OTHER); /* bit 10 */
3664 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER == OFPPF11_COPPER); /* bit 11 */
3665 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER == OFPPF11_FIBER); /* bit 12 */
3666 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG == OFPPF11_AUTONEG); /* bit 13 */
3667 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE == OFPPF11_PAUSE); /* bit 14 */
3668 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM == OFPPF11_PAUSE_ASYM);/* bit 15 */
3670 static enum netdev_features
3671 netdev_port_features_from_ofp11(ovs_be32 ofp11)
3673 return ntohl(ofp11) & 0xffff;
3677 netdev_port_features_to_ofp11(enum netdev_features features)
3679 return htonl(features & 0xffff);
3683 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port *pp,
3684 const struct ofp10_phy_port *opp)
3686 memset(pp, 0, sizeof *pp);
3688 pp->port_no = u16_to_ofp(ntohs(opp->port_no));
3689 memcpy(pp->hw_addr, opp->hw_addr, OFP_ETH_ALEN);
3690 ovs_strlcpy(pp->name, opp->name, OFP_MAX_PORT_NAME_LEN);
3692 pp->config = ntohl(opp->config) & OFPPC10_ALL;
3693 pp->state = ntohl(opp->state) & OFPPS10_ALL;
3695 pp->curr = netdev_port_features_from_ofp10(opp->curr);
3696 pp->advertised = netdev_port_features_from_ofp10(opp->advertised);
3697 pp->supported = netdev_port_features_from_ofp10(opp->supported);
3698 pp->peer = netdev_port_features_from_ofp10(opp->peer);
3700 pp->curr_speed = netdev_features_to_bps(pp->curr, 0) / 1000;
3701 pp->max_speed = netdev_features_to_bps(pp->supported, 0) / 1000;
3707 ofputil_decode_ofp11_port(struct ofputil_phy_port *pp,
3708 const struct ofp11_port *op)
3712 memset(pp, 0, sizeof *pp);
3714 error = ofputil_port_from_ofp11(op->port_no, &pp->port_no);
3718 memcpy(pp->hw_addr, op->hw_addr, OFP_ETH_ALEN);
3719 ovs_strlcpy(pp->name, op->name, OFP_MAX_PORT_NAME_LEN);
3721 pp->config = ntohl(op->config) & OFPPC11_ALL;
3722 pp->state = ntohl(op->state) & OFPPS11_ALL;
3724 pp->curr = netdev_port_features_from_ofp11(op->curr);
3725 pp->advertised = netdev_port_features_from_ofp11(op->advertised);
3726 pp->supported = netdev_port_features_from_ofp11(op->supported);
3727 pp->peer = netdev_port_features_from_ofp11(op->peer);
3729 pp->curr_speed = ntohl(op->curr_speed);
3730 pp->max_speed = ntohl(op->max_speed);
3736 ofputil_get_phy_port_size(enum ofp_version ofp_version)
3738 switch (ofp_version) {
3740 return sizeof(struct ofp10_phy_port);
3745 return sizeof(struct ofp11_port);
3752 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port *pp,
3753 struct ofp10_phy_port *opp)
3755 memset(opp, 0, sizeof *opp);
3757 opp->port_no = htons(ofp_to_u16(pp->port_no));
3758 memcpy(opp->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
3759 ovs_strlcpy(opp->name, pp->name, OFP_MAX_PORT_NAME_LEN);
3761 opp->config = htonl(pp->config & OFPPC10_ALL);
3762 opp->state = htonl(pp->state & OFPPS10_ALL);
3764 opp->curr = netdev_port_features_to_ofp10(pp->curr);
3765 opp->advertised = netdev_port_features_to_ofp10(pp->advertised);
3766 opp->supported = netdev_port_features_to_ofp10(pp->supported);
3767 opp->peer = netdev_port_features_to_ofp10(pp->peer);
3771 ofputil_encode_ofp11_port(const struct ofputil_phy_port *pp,
3772 struct ofp11_port *op)
3774 memset(op, 0, sizeof *op);
3776 op->port_no = ofputil_port_to_ofp11(pp->port_no);
3777 memcpy(op->hw_addr, pp->hw_addr, ETH_ADDR_LEN);
3778 ovs_strlcpy(op->name, pp->name, OFP_MAX_PORT_NAME_LEN);
3780 op->config = htonl(pp->config & OFPPC11_ALL);
3781 op->state = htonl(pp->state & OFPPS11_ALL);
3783 op->curr = netdev_port_features_to_ofp11(pp->curr);
3784 op->advertised = netdev_port_features_to_ofp11(pp->advertised);
3785 op->supported = netdev_port_features_to_ofp11(pp->supported);
3786 op->peer = netdev_port_features_to_ofp11(pp->peer);
3788 op->curr_speed = htonl(pp->curr_speed);
3789 op->max_speed = htonl(pp->max_speed);
3793 ofputil_put_phy_port(enum ofp_version ofp_version,
3794 const struct ofputil_phy_port *pp, struct ofpbuf *b)
3796 switch (ofp_version) {
3797 case OFP10_VERSION: {
3798 struct ofp10_phy_port *opp;
3799 if (ofpbuf_size(b) + sizeof *opp <= UINT16_MAX) {
3800 opp = ofpbuf_put_uninit(b, sizeof *opp);
3801 ofputil_encode_ofp10_phy_port(pp, opp);
3808 case OFP13_VERSION: {
3809 struct ofp11_port *op;
3810 if (ofpbuf_size(b) + sizeof *op <= UINT16_MAX) {
3811 op = ofpbuf_put_uninit(b, sizeof *op);
3812 ofputil_encode_ofp11_port(pp, op);
3827 ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port *pp,
3828 struct list *replies)
3830 switch (ofpmp_version(replies)) {
3831 case OFP10_VERSION: {
3832 struct ofp10_phy_port *opp;
3834 opp = ofpmp_append(replies, sizeof *opp);
3835 ofputil_encode_ofp10_phy_port(pp, opp);
3841 case OFP13_VERSION: {
3842 struct ofp11_port *op;
3844 op = ofpmp_append(replies, sizeof *op);
3845 ofputil_encode_ofp11_port(pp, op);
3858 /* ofputil_switch_features */
3860 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
3861 OFPC_IP_REASM | OFPC_QUEUE_STATS)
3862 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS == OFPC_FLOW_STATS);
3863 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS == OFPC_TABLE_STATS);
3864 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS == OFPC_PORT_STATS);
3865 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM == OFPC_IP_REASM);
3866 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS == OFPC_QUEUE_STATS);
3867 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP == OFPC_ARP_MATCH_IP);
3869 struct ofputil_action_bit_translation {
3870 enum ofputil_action_bitmap ofputil_bit;
3874 static const struct ofputil_action_bit_translation of10_action_bits[] = {
3875 { OFPUTIL_A_OUTPUT, OFPAT10_OUTPUT },
3876 { OFPUTIL_A_SET_VLAN_VID, OFPAT10_SET_VLAN_VID },
3877 { OFPUTIL_A_SET_VLAN_PCP, OFPAT10_SET_VLAN_PCP },
3878 { OFPUTIL_A_STRIP_VLAN, OFPAT10_STRIP_VLAN },
3879 { OFPUTIL_A_SET_DL_SRC, OFPAT10_SET_DL_SRC },
3880 { OFPUTIL_A_SET_DL_DST, OFPAT10_SET_DL_DST },
3881 { OFPUTIL_A_SET_NW_SRC, OFPAT10_SET_NW_SRC },
3882 { OFPUTIL_A_SET_NW_DST, OFPAT10_SET_NW_DST },
3883 { OFPUTIL_A_SET_NW_TOS, OFPAT10_SET_NW_TOS },
3884 { OFPUTIL_A_SET_TP_SRC, OFPAT10_SET_TP_SRC },
3885 { OFPUTIL_A_SET_TP_DST, OFPAT10_SET_TP_DST },
3886 { OFPUTIL_A_ENQUEUE, OFPAT10_ENQUEUE },
3890 static enum ofputil_action_bitmap
3891 decode_action_bits(ovs_be32 of_actions,
3892 const struct ofputil_action_bit_translation *x)
3894 enum ofputil_action_bitmap ofputil_actions;
3896 ofputil_actions = 0;
3897 for (; x->ofputil_bit; x++) {
3898 if (of_actions & htonl(1u << x->of_bit)) {
3899 ofputil_actions |= x->ofputil_bit;
3902 return ofputil_actions;
3906 ofputil_capabilities_mask(enum ofp_version ofp_version)
3908 /* Handle capabilities whose bit is unique for all Open Flow versions */
3909 switch (ofp_version) {
3912 return OFPC_COMMON | OFPC_ARP_MATCH_IP;
3915 return OFPC_COMMON | OFPC12_PORT_BLOCKED;
3920 /* Caller needs to check osf->header.version itself */
3925 /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an
3926 * abstract representation in '*features'. Initializes '*b' to iterate over
3927 * the OpenFlow port structures following 'osf' with later calls to
3928 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an
3929 * OFPERR_* value. */
3931 ofputil_decode_switch_features(const struct ofp_header *oh,
3932 struct ofputil_switch_features *features,
3935 const struct ofp_switch_features *osf;
3938 ofpbuf_use_const(b, oh, ntohs(oh->length));
3939 raw = ofpraw_pull_assert(b);
3941 osf = ofpbuf_pull(b, sizeof *osf);
3942 features->datapath_id = ntohll(osf->datapath_id);
3943 features->n_buffers = ntohl(osf->n_buffers);
3944 features->n_tables = osf->n_tables;
3945 features->auxiliary_id = 0;
3947 features->capabilities = ntohl(osf->capabilities) &
3948 ofputil_capabilities_mask(oh->version);
3950 if (ofpbuf_size(b) % ofputil_get_phy_port_size(oh->version)) {
3951 return OFPERR_OFPBRC_BAD_LEN;
3954 if (raw == OFPRAW_OFPT10_FEATURES_REPLY) {
3955 if (osf->capabilities & htonl(OFPC10_STP)) {
3956 features->capabilities |= OFPUTIL_C_STP;
3958 features->actions = decode_action_bits(osf->actions, of10_action_bits);
3959 } else if (raw == OFPRAW_OFPT11_FEATURES_REPLY
3960 || raw == OFPRAW_OFPT13_FEATURES_REPLY) {
3961 if (osf->capabilities & htonl(OFPC11_GROUP_STATS)) {
3962 features->capabilities |= OFPUTIL_C_GROUP_STATS;
3964 features->actions = 0;
3965 if (raw == OFPRAW_OFPT13_FEATURES_REPLY) {
3966 features->auxiliary_id = osf->auxiliary_id;
3969 return OFPERR_OFPBRC_BAD_VERSION;
3975 /* Returns true if the maximum number of ports are in 'oh'. */
3977 max_ports_in_features(const struct ofp_header *oh)
3979 size_t pp_size = ofputil_get_phy_port_size(oh->version);
3980 return ntohs(oh->length) + pp_size > UINT16_MAX;
3983 /* In OpenFlow 1.0, 1.1, and 1.2, an OFPT_FEATURES_REPLY message lists all the
3984 * switch's ports, unless there are too many to fit. In OpenFlow 1.3 and
3985 * later, an OFPT_FEATURES_REPLY does not list ports at all.
3987 * Given a buffer 'b' that contains a Features Reply message, this message
3988 * checks if it contains a complete list of the switch's ports. Returns true,
3989 * if so. Returns false if the list is missing (OF1.3+) or incomplete
3990 * (OF1.0/1.1/1.2), and in the latter case removes all of the ports from the
3993 * When this function returns false, the caller should send an OFPST_PORT_DESC
3994 * stats request to get the ports. */
3996 ofputil_switch_features_has_ports(struct ofpbuf *b)
3998 struct ofp_header *oh = ofpbuf_data(b);
4000 if (oh->version >= OFP13_VERSION) {
4002 } else if (max_ports_in_features(oh)) {
4003 ofpbuf_set_size(b, sizeof *oh + sizeof(struct ofp_switch_features));
4004 ofpmsg_update_length(b);
4012 encode_action_bits(enum ofputil_action_bitmap ofputil_actions,
4013 const struct ofputil_action_bit_translation *x)
4015 uint32_t of_actions;
4018 for (; x->ofputil_bit; x++) {
4019 if (ofputil_actions & x->ofputil_bit) {
4020 of_actions |= 1 << x->of_bit;
4023 return htonl(of_actions);
4026 /* Returns a buffer owned by the caller that encodes 'features' in the format
4027 * required by 'protocol' with the given 'xid'. The caller should append port
4028 * information to the buffer with subsequent calls to
4029 * ofputil_put_switch_features_port(). */
4031 ofputil_encode_switch_features(const struct ofputil_switch_features *features,
4032 enum ofputil_protocol protocol, ovs_be32 xid)
4034 struct ofp_switch_features *osf;
4036 enum ofp_version version;
4039 version = ofputil_protocol_to_ofp_version(protocol);
4042 raw = OFPRAW_OFPT10_FEATURES_REPLY;
4046 raw = OFPRAW_OFPT11_FEATURES_REPLY;
4050 raw = OFPRAW_OFPT13_FEATURES_REPLY;
4055 b = ofpraw_alloc_xid(raw, version, xid, 0);
4056 osf = ofpbuf_put_zeros(b, sizeof *osf);
4057 osf->datapath_id = htonll(features->datapath_id);
4058 osf->n_buffers = htonl(features->n_buffers);
4059 osf->n_tables = features->n_tables;
4061 osf->capabilities = htonl(features->capabilities & OFPC_COMMON);
4062 osf->capabilities = htonl(features->capabilities &
4063 ofputil_capabilities_mask(version));
4066 if (features->capabilities & OFPUTIL_C_STP) {
4067 osf->capabilities |= htonl(OFPC10_STP);
4069 osf->actions = encode_action_bits(features->actions, of10_action_bits);
4073 osf->auxiliary_id = features->auxiliary_id;
4077 if (features->capabilities & OFPUTIL_C_GROUP_STATS) {
4078 osf->capabilities |= htonl(OFPC11_GROUP_STATS);
4088 /* Encodes 'pp' into the format required by the switch_features message already
4089 * in 'b', which should have been returned by ofputil_encode_switch_features(),
4090 * and appends the encoded version to 'b'. */
4092 ofputil_put_switch_features_port(const struct ofputil_phy_port *pp,
4095 const struct ofp_header *oh = ofpbuf_data(b);
4097 if (oh->version < OFP13_VERSION) {
4098 ofputil_put_phy_port(oh->version, pp, b);
4102 /* ofputil_port_status */
4104 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
4105 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
4107 ofputil_decode_port_status(const struct ofp_header *oh,
4108 struct ofputil_port_status *ps)
4110 const struct ofp_port_status *ops;
4114 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4115 ofpraw_pull_assert(&b);
4116 ops = ofpbuf_pull(&b, sizeof *ops);
4118 if (ops->reason != OFPPR_ADD &&
4119 ops->reason != OFPPR_DELETE &&
4120 ops->reason != OFPPR_MODIFY) {
4121 return OFPERR_NXBRC_BAD_REASON;
4123 ps->reason = ops->reason;
4125 retval = ofputil_pull_phy_port(oh->version, &b, &ps->desc);
4126 ovs_assert(retval != EOF);
4130 /* Converts the abstract form of a "port status" message in '*ps' into an
4131 * OpenFlow message suitable for 'protocol', and returns that encoded form in
4132 * a buffer owned by the caller. */
4134 ofputil_encode_port_status(const struct ofputil_port_status *ps,
4135 enum ofputil_protocol protocol)
4137 struct ofp_port_status *ops;
4139 enum ofp_version version;
4142 version = ofputil_protocol_to_ofp_version(protocol);
4145 raw = OFPRAW_OFPT10_PORT_STATUS;
4152 raw = OFPRAW_OFPT11_PORT_STATUS;
4159 b = ofpraw_alloc_xid(raw, version, htonl(0), 0);
4160 ops = ofpbuf_put_zeros(b, sizeof *ops);
4161 ops->reason = ps->reason;
4162 ofputil_put_phy_port(version, &ps->desc, b);
4163 ofpmsg_update_length(b);
4167 /* ofputil_port_mod */
4169 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
4170 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
4172 ofputil_decode_port_mod(const struct ofp_header *oh,
4173 struct ofputil_port_mod *pm)
4178 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4179 raw = ofpraw_pull_assert(&b);
4181 if (raw == OFPRAW_OFPT10_PORT_MOD) {
4182 const struct ofp10_port_mod *opm = ofpbuf_data(&b);
4184 pm->port_no = u16_to_ofp(ntohs(opm->port_no));
4185 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
4186 pm->config = ntohl(opm->config) & OFPPC10_ALL;
4187 pm->mask = ntohl(opm->mask) & OFPPC10_ALL;
4188 pm->advertise = netdev_port_features_from_ofp10(opm->advertise);
4189 } else if (raw == OFPRAW_OFPT11_PORT_MOD) {
4190 const struct ofp11_port_mod *opm = ofpbuf_data(&b);
4193 error = ofputil_port_from_ofp11(opm->port_no, &pm->port_no);
4198 memcpy(pm->hw_addr, opm->hw_addr, ETH_ADDR_LEN);
4199 pm->config = ntohl(opm->config) & OFPPC11_ALL;
4200 pm->mask = ntohl(opm->mask) & OFPPC11_ALL;
4201 pm->advertise = netdev_port_features_from_ofp11(opm->advertise);
4203 return OFPERR_OFPBRC_BAD_TYPE;
4206 pm->config &= pm->mask;
4210 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
4211 * message suitable for 'protocol', and returns that encoded form in a buffer
4212 * owned by the caller. */
4214 ofputil_encode_port_mod(const struct ofputil_port_mod *pm,
4215 enum ofputil_protocol protocol)
4217 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
4220 switch (ofp_version) {
4221 case OFP10_VERSION: {
4222 struct ofp10_port_mod *opm;
4224 b = ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD, ofp_version, 0);
4225 opm = ofpbuf_put_zeros(b, sizeof *opm);
4226 opm->port_no = htons(ofp_to_u16(pm->port_no));
4227 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
4228 opm->config = htonl(pm->config & OFPPC10_ALL);
4229 opm->mask = htonl(pm->mask & OFPPC10_ALL);
4230 opm->advertise = netdev_port_features_to_ofp10(pm->advertise);
4236 case OFP13_VERSION: {
4237 struct ofp11_port_mod *opm;
4239 b = ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD, ofp_version, 0);
4240 opm = ofpbuf_put_zeros(b, sizeof *opm);
4241 opm->port_no = ofputil_port_to_ofp11(pm->port_no);
4242 memcpy(opm->hw_addr, pm->hw_addr, ETH_ADDR_LEN);
4243 opm->config = htonl(pm->config & OFPPC11_ALL);
4244 opm->mask = htonl(pm->mask & OFPPC11_ALL);
4245 opm->advertise = netdev_port_features_to_ofp11(pm->advertise);
4259 pull_table_feature_property(struct ofpbuf *msg, struct ofpbuf *payload,
4264 error = ofputil_pull_property(msg, payload, typep);
4265 if (payload && !error) {
4266 ofpbuf_pull(payload, sizeof(struct ofp_prop_header));
4272 parse_table_ids(struct ofpbuf *payload, uint32_t *ids)
4277 while (ofpbuf_size(payload) > 0) {
4278 enum ofperr error = pull_table_feature_property(payload, NULL, &type);
4282 if (type < CHAR_BIT * sizeof *ids) {
4290 parse_instruction_ids(struct ofpbuf *payload, bool loose, uint32_t *insts)
4293 while (ofpbuf_size(payload) > 0) {
4294 enum ovs_instruction_type inst;
4298 error = pull_table_feature_property(payload, NULL, &ofpit);
4303 error = ovs_instruction_type_from_inst_type(&inst, ofpit);
4305 *insts |= 1u << inst;
4306 } else if (!loose) {
4314 parse_table_features_next_table(struct ofpbuf *payload,
4315 unsigned long int *next_tables)
4319 memset(next_tables, 0, bitmap_n_bytes(255));
4320 for (i = 0; i < ofpbuf_size(payload); i++) {
4321 uint8_t id = ((const uint8_t *) ofpbuf_data(payload))[i];
4323 return OFPERR_OFPBPC_BAD_VALUE;
4325 bitmap_set1(next_tables, id);
4331 parse_oxm(struct ofpbuf *b, bool loose,
4332 const struct mf_field **fieldp, bool *hasmask)
4337 oxmp = ofpbuf_try_pull(b, sizeof *oxmp);
4339 return OFPERR_OFPBPC_BAD_LEN;
4343 /* Determine '*hasmask'. If 'oxm' is masked, convert it to the equivalent
4344 * unmasked version, because the table of OXM fields we support only has
4345 * masked versions of fields that we support with masks, but we should be
4346 * able to parse the masked versions of those here. */
4347 *hasmask = NXM_HASMASK(oxm);
4349 if (NXM_LENGTH(oxm) & 1) {
4350 return OFPERR_OFPBPC_BAD_VALUE;
4352 oxm = NXM_HEADER(NXM_VENDOR(oxm), NXM_FIELD(oxm), NXM_LENGTH(oxm) / 2);
4355 *fieldp = mf_from_nxm_header(oxm);
4357 log_property(loose, "unknown OXM field %#"PRIx32, ntohl(*oxmp));
4359 return *fieldp ? 0 : OFPERR_OFPBMC_BAD_FIELD;
4363 parse_oxms(struct ofpbuf *payload, bool loose,
4364 uint64_t *exactp, uint64_t *maskedp)
4366 uint64_t exact, masked;
4369 while (ofpbuf_size(payload) > 0) {
4370 const struct mf_field *field;
4374 error = parse_oxm(payload, loose, &field, &hasmask);
4377 masked |= UINT64_C(1) << field->id;
4379 exact |= UINT64_C(1) << field->id;
4381 } else if (error != OFPERR_OFPBMC_BAD_FIELD || !loose) {
4388 return OFPERR_OFPBMC_BAD_MASK;
4392 } else if (masked) {
4393 return OFPERR_OFPBMC_BAD_MASK;
4398 /* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract
4399 * ofputil_table_features in 'tf'.
4401 * If 'loose' is true, this function ignores properties and values that it does
4402 * not understand, as a controller would want to do when interpreting
4403 * capabilities provided by a switch. If 'loose' is false, this function
4404 * treats unknown properties and values as an error, as a switch would want to
4405 * do when interpreting a configuration request made by a controller.
4407 * A single OpenFlow message can specify features for multiple tables. Calling
4408 * this function multiple times for a single 'msg' iterates through the tables
4409 * in the message. The caller must initially leave 'msg''s layer pointers null
4410 * and not modify them between calls.
4412 * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise
4413 * a positive "enum ofperr" value. */
4415 ofputil_decode_table_features(struct ofpbuf *msg,
4416 struct ofputil_table_features *tf, bool loose)
4418 struct ofp13_table_features *otf;
4422 ofpraw_pull_assert(msg);
4425 if (!ofpbuf_size(msg)) {
4429 if (ofpbuf_size(msg) < sizeof *otf) {
4430 return OFPERR_OFPBPC_BAD_LEN;
4433 otf = ofpbuf_data(msg);
4434 len = ntohs(otf->length);
4435 if (len < sizeof *otf || len % 8 || len > ofpbuf_size(msg)) {
4436 return OFPERR_OFPBPC_BAD_LEN;
4438 ofpbuf_pull(msg, sizeof *otf);
4440 tf->table_id = otf->table_id;
4441 if (tf->table_id == OFPTT_ALL) {
4442 return OFPERR_OFPTFFC_BAD_TABLE;
4445 ovs_strlcpy(tf->name, otf->name, OFP_MAX_TABLE_NAME_LEN);
4446 tf->metadata_match = otf->metadata_match;
4447 tf->metadata_write = otf->metadata_write;
4448 tf->config = ntohl(otf->config);
4449 tf->max_entries = ntohl(otf->max_entries);
4451 while (ofpbuf_size(msg) > 0) {
4452 struct ofpbuf payload;
4456 error = pull_table_feature_property(msg, &payload, &type);
4461 switch ((enum ofp13_table_feature_prop_type) type) {
4462 case OFPTFPT13_INSTRUCTIONS:
4463 error = parse_instruction_ids(&payload, loose,
4464 &tf->nonmiss.instructions);
4466 case OFPTFPT13_INSTRUCTIONS_MISS:
4467 error = parse_instruction_ids(&payload, loose,
4468 &tf->miss.instructions);
4471 case OFPTFPT13_NEXT_TABLES:
4472 error = parse_table_features_next_table(&payload,
4475 case OFPTFPT13_NEXT_TABLES_MISS:
4476 error = parse_table_features_next_table(&payload, tf->miss.next);
4479 case OFPTFPT13_WRITE_ACTIONS:
4480 error = parse_table_ids(&payload, &tf->nonmiss.write.actions);
4482 case OFPTFPT13_WRITE_ACTIONS_MISS:
4483 error = parse_table_ids(&payload, &tf->miss.write.actions);
4486 case OFPTFPT13_APPLY_ACTIONS:
4487 error = parse_table_ids(&payload, &tf->nonmiss.apply.actions);
4489 case OFPTFPT13_APPLY_ACTIONS_MISS:
4490 error = parse_table_ids(&payload, &tf->miss.apply.actions);
4493 case OFPTFPT13_MATCH:
4494 error = parse_oxms(&payload, loose, &tf->match, &tf->mask);
4496 case OFPTFPT13_WILDCARDS:
4497 error = parse_oxms(&payload, loose, &tf->wildcard, NULL);
4500 case OFPTFPT13_WRITE_SETFIELD:
4501 error = parse_oxms(&payload, loose,
4502 &tf->nonmiss.write.set_fields, NULL);
4504 case OFPTFPT13_WRITE_SETFIELD_MISS:
4505 error = parse_oxms(&payload, loose,
4506 &tf->miss.write.set_fields, NULL);
4508 case OFPTFPT13_APPLY_SETFIELD:
4509 error = parse_oxms(&payload, loose,
4510 &tf->nonmiss.apply.set_fields, NULL);
4512 case OFPTFPT13_APPLY_SETFIELD_MISS:
4513 error = parse_oxms(&payload, loose,
4514 &tf->miss.apply.set_fields, NULL);
4517 case OFPTFPT13_EXPERIMENTER:
4518 case OFPTFPT13_EXPERIMENTER_MISS:
4520 log_property(loose, "unknown table features property %"PRIu16,
4522 error = loose ? 0 : OFPERR_OFPBPC_BAD_TYPE;
4530 /* Fix inconsistencies:
4532 * - Turn off 'mask' and 'wildcard' bits that are not in 'match',
4533 * because a field must be matchable to be masked or wildcarded.
4535 * - Turn on 'wildcard' bits that are set in 'mask', because a field
4536 * that is arbitrarily maskable can be wildcarded entirely. */
4537 tf->mask &= tf->match;
4538 tf->wildcard &= tf->match;
4540 tf->wildcard |= tf->mask;
4545 /* Encodes and returns a request to obtain the table features of a switch.
4546 * The message is encoded for OpenFlow version 'ofp_version'. */
4548 ofputil_encode_table_features_request(enum ofp_version ofp_version)
4550 struct ofpbuf *request = NULL;
4552 switch (ofp_version) {
4556 ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later "
4557 "(\'-O OpenFlow13\')");
4560 request = ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST,
4570 /* ofputil_table_mod */
4572 /* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in
4573 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
4575 ofputil_decode_table_mod(const struct ofp_header *oh,
4576 struct ofputil_table_mod *pm)
4581 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4582 raw = ofpraw_pull_assert(&b);
4584 if (raw == OFPRAW_OFPT11_TABLE_MOD) {
4585 const struct ofp11_table_mod *otm = ofpbuf_data(&b);
4587 pm->table_id = otm->table_id;
4588 pm->config = ntohl(otm->config);
4590 return OFPERR_OFPBRC_BAD_TYPE;
4596 /* Converts the abstract form of a "table mod" message in '*pm' into an OpenFlow
4597 * message suitable for 'protocol', and returns that encoded form in a buffer
4598 * owned by the caller. */
4600 ofputil_encode_table_mod(const struct ofputil_table_mod *pm,
4601 enum ofputil_protocol protocol)
4603 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
4606 switch (ofp_version) {
4607 case OFP10_VERSION: {
4608 ovs_fatal(0, "table mod needs OpenFlow 1.1 or later "
4609 "(\'-O OpenFlow11\')");
4614 case OFP13_VERSION: {
4615 struct ofp11_table_mod *otm;
4617 b = ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD, ofp_version, 0);
4618 otm = ofpbuf_put_zeros(b, sizeof *otm);
4619 otm->table_id = pm->table_id;
4620 otm->config = htonl(pm->config);
4633 /* ofputil_role_request */
4635 /* Decodes the OpenFlow "role request" or "role reply" message in '*oh' into
4636 * an abstract form in '*rr'. Returns 0 if successful, otherwise an
4637 * OFPERR_* value. */
4639 ofputil_decode_role_message(const struct ofp_header *oh,
4640 struct ofputil_role_request *rr)
4645 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4646 raw = ofpraw_pull_assert(&b);
4648 if (raw == OFPRAW_OFPT12_ROLE_REQUEST ||
4649 raw == OFPRAW_OFPT12_ROLE_REPLY) {
4650 const struct ofp12_role_request *orr = ofpbuf_l3(&b);
4652 if (orr->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
4653 orr->role != htonl(OFPCR12_ROLE_EQUAL) &&
4654 orr->role != htonl(OFPCR12_ROLE_MASTER) &&
4655 orr->role != htonl(OFPCR12_ROLE_SLAVE)) {
4656 return OFPERR_OFPRRFC_BAD_ROLE;
4659 rr->role = ntohl(orr->role);
4660 if (raw == OFPRAW_OFPT12_ROLE_REQUEST
4661 ? orr->role == htonl(OFPCR12_ROLE_NOCHANGE)
4662 : orr->generation_id == OVS_BE64_MAX) {
4663 rr->have_generation_id = false;
4664 rr->generation_id = 0;
4666 rr->have_generation_id = true;
4667 rr->generation_id = ntohll(orr->generation_id);
4669 } else if (raw == OFPRAW_NXT_ROLE_REQUEST ||
4670 raw == OFPRAW_NXT_ROLE_REPLY) {
4671 const struct nx_role_request *nrr = ofpbuf_l3(&b);
4673 BUILD_ASSERT(NX_ROLE_OTHER + 1 == OFPCR12_ROLE_EQUAL);
4674 BUILD_ASSERT(NX_ROLE_MASTER + 1 == OFPCR12_ROLE_MASTER);
4675 BUILD_ASSERT(NX_ROLE_SLAVE + 1 == OFPCR12_ROLE_SLAVE);
4677 if (nrr->role != htonl(NX_ROLE_OTHER) &&
4678 nrr->role != htonl(NX_ROLE_MASTER) &&
4679 nrr->role != htonl(NX_ROLE_SLAVE)) {
4680 return OFPERR_OFPRRFC_BAD_ROLE;
4683 rr->role = ntohl(nrr->role) + 1;
4684 rr->have_generation_id = false;
4685 rr->generation_id = 0;
4693 /* Returns an encoded form of a role reply suitable for the "request" in a
4694 * buffer owned by the caller. */
4696 ofputil_encode_role_reply(const struct ofp_header *request,
4697 const struct ofputil_role_request *rr)
4702 raw = ofpraw_decode_assert(request);
4703 if (raw == OFPRAW_OFPT12_ROLE_REQUEST) {
4704 struct ofp12_role_request *orr;
4706 buf = ofpraw_alloc_reply(OFPRAW_OFPT12_ROLE_REPLY, request, 0);
4707 orr = ofpbuf_put_zeros(buf, sizeof *orr);
4709 orr->role = htonl(rr->role);
4710 orr->generation_id = htonll(rr->have_generation_id
4713 } else if (raw == OFPRAW_NXT_ROLE_REQUEST) {
4714 struct nx_role_request *nrr;
4716 BUILD_ASSERT(NX_ROLE_OTHER == OFPCR12_ROLE_EQUAL - 1);
4717 BUILD_ASSERT(NX_ROLE_MASTER == OFPCR12_ROLE_MASTER - 1);
4718 BUILD_ASSERT(NX_ROLE_SLAVE == OFPCR12_ROLE_SLAVE - 1);
4720 buf = ofpraw_alloc_reply(OFPRAW_NXT_ROLE_REPLY, request, 0);
4721 nrr = ofpbuf_put_zeros(buf, sizeof *nrr);
4722 nrr->role = htonl(rr->role - 1);
4731 ofputil_encode_role_status(const struct ofputil_role_status *status,
4732 enum ofputil_protocol protocol)
4735 enum ofp_version version;
4736 struct ofp14_role_status *rstatus;
4738 version = ofputil_protocol_to_ofp_version(protocol);
4739 buf = ofpraw_alloc_xid(OFPRAW_OFPT14_ROLE_STATUS, version, htonl(0), 0);
4740 rstatus = ofpbuf_put_zeros(buf, sizeof *rstatus);
4741 rstatus->role = htonl(status->role);
4742 rstatus->reason = status->reason;
4743 rstatus->generation_id = htonll(status->generation_id);
4749 ofputil_decode_role_status(const struct ofp_header *oh,
4750 struct ofputil_role_status *rs)
4754 const struct ofp14_role_status *r;
4756 ofpbuf_use_const(&b, oh, ntohs(oh->length));
4757 raw = ofpraw_pull_assert(&b);
4758 ovs_assert(raw == OFPRAW_OFPT14_ROLE_STATUS);
4761 if (r->role != htonl(OFPCR12_ROLE_NOCHANGE) &&
4762 r->role != htonl(OFPCR12_ROLE_EQUAL) &&
4763 r->role != htonl(OFPCR12_ROLE_MASTER) &&
4764 r->role != htonl(OFPCR12_ROLE_SLAVE)) {
4765 return OFPERR_OFPRRFC_BAD_ROLE;
4768 rs->role = ntohl(r->role);
4769 rs->generation_id = ntohll(r->generation_id);
4770 rs->reason = r->reason;
4778 ofputil_put_ofp10_table_stats(const struct ofp12_table_stats *in,
4782 enum ofp10_flow_wildcards wc10;
4783 enum oxm12_ofb_match_fields mf12;
4786 static const struct wc_map wc_map[] = {
4787 { OFPFW10_IN_PORT, OFPXMT12_OFB_IN_PORT },
4788 { OFPFW10_DL_VLAN, OFPXMT12_OFB_VLAN_VID },
4789 { OFPFW10_DL_SRC, OFPXMT12_OFB_ETH_SRC },
4790 { OFPFW10_DL_DST, OFPXMT12_OFB_ETH_DST},
4791 { OFPFW10_DL_TYPE, OFPXMT12_OFB_ETH_TYPE },
4792 { OFPFW10_NW_PROTO, OFPXMT12_OFB_IP_PROTO },
4793 { OFPFW10_TP_SRC, OFPXMT12_OFB_TCP_SRC },
4794 { OFPFW10_TP_DST, OFPXMT12_OFB_TCP_DST },
4795 { OFPFW10_NW_SRC_MASK, OFPXMT12_OFB_IPV4_SRC },
4796 { OFPFW10_NW_DST_MASK, OFPXMT12_OFB_IPV4_DST },
4797 { OFPFW10_DL_VLAN_PCP, OFPXMT12_OFB_VLAN_PCP },
4798 { OFPFW10_NW_TOS, OFPXMT12_OFB_IP_DSCP },
4801 struct ofp10_table_stats *out;
4802 const struct wc_map *p;
4804 out = ofpbuf_put_zeros(buf, sizeof *out);
4805 out->table_id = in->table_id;
4806 ovs_strlcpy(out->name, in->name, sizeof out->name);
4808 for (p = wc_map; p < &wc_map[ARRAY_SIZE(wc_map)]; p++) {
4809 if (in->wildcards & htonll(1ULL << p->mf12)) {
4810 out->wildcards |= htonl(p->wc10);
4813 out->max_entries = in->max_entries;
4814 out->active_count = in->active_count;
4815 put_32aligned_be64(&out->lookup_count, in->lookup_count);
4816 put_32aligned_be64(&out->matched_count, in->matched_count);
4820 oxm12_to_ofp11_flow_match_fields(ovs_be64 oxm12)
4823 enum ofp11_flow_match_fields fmf11;
4824 enum oxm12_ofb_match_fields mf12;
4827 static const struct map map[] = {
4828 { OFPFMF11_IN_PORT, OFPXMT12_OFB_IN_PORT },
4829 { OFPFMF11_DL_VLAN, OFPXMT12_OFB_VLAN_VID },
4830 { OFPFMF11_DL_VLAN_PCP, OFPXMT12_OFB_VLAN_PCP },
4831 { OFPFMF11_DL_TYPE, OFPXMT12_OFB_ETH_TYPE },
4832 { OFPFMF11_NW_TOS, OFPXMT12_OFB_IP_DSCP },
4833 { OFPFMF11_NW_PROTO, OFPXMT12_OFB_IP_PROTO },
4834 { OFPFMF11_TP_SRC, OFPXMT12_OFB_TCP_SRC },
4835 { OFPFMF11_TP_DST, OFPXMT12_OFB_TCP_DST },
4836 { OFPFMF11_MPLS_LABEL, OFPXMT12_OFB_MPLS_LABEL },
4837 { OFPFMF11_MPLS_TC, OFPXMT12_OFB_MPLS_TC },
4838 /* I don't know what OFPFMF11_TYPE means. */
4839 { OFPFMF11_DL_SRC, OFPXMT12_OFB_ETH_SRC },
4840 { OFPFMF11_DL_DST, OFPXMT12_OFB_ETH_DST },
4841 { OFPFMF11_NW_SRC, OFPXMT12_OFB_IPV4_SRC },
4842 { OFPFMF11_NW_DST, OFPXMT12_OFB_IPV4_DST },
4843 { OFPFMF11_METADATA, OFPXMT12_OFB_METADATA },
4846 const struct map *p;
4850 for (p = map; p < &map[ARRAY_SIZE(map)]; p++) {
4851 if (oxm12 & htonll(1ULL << p->mf12)) {
4855 return htonl(fmf11);
4859 ofputil_put_ofp11_table_stats(const struct ofp12_table_stats *in,
4862 struct ofp11_table_stats *out;
4864 out = ofpbuf_put_zeros(buf, sizeof *out);
4865 out->table_id = in->table_id;
4866 ovs_strlcpy(out->name, in->name, sizeof out->name);
4867 out->wildcards = oxm12_to_ofp11_flow_match_fields(in->wildcards);
4868 out->match = oxm12_to_ofp11_flow_match_fields(in->match);
4869 out->instructions = in->instructions;
4870 out->write_actions = in->write_actions;
4871 out->apply_actions = in->apply_actions;
4872 out->config = in->config;
4873 out->max_entries = in->max_entries;
4874 out->active_count = in->active_count;
4875 out->lookup_count = in->lookup_count;
4876 out->matched_count = in->matched_count;
4880 ofputil_put_ofp12_table_stats(const struct ofp12_table_stats *in,
4883 struct ofp12_table_stats *out = ofpbuf_put(buf, in, sizeof *in);
4885 /* Trim off OF1.3-only capabilities. */
4886 out->match &= htonll(OFPXMT12_MASK);
4887 out->wildcards &= htonll(OFPXMT12_MASK);
4888 out->write_setfields &= htonll(OFPXMT12_MASK);
4889 out->apply_setfields &= htonll(OFPXMT12_MASK);
4893 ofputil_put_ofp13_table_stats(const struct ofp12_table_stats *in,
4896 struct ofp13_table_stats *out;
4898 /* OF 1.3 splits table features off the ofp_table_stats,
4899 * so there is not much here. */
4901 out = ofpbuf_put_uninit(buf, sizeof *out);
4902 out->table_id = in->table_id;
4903 out->active_count = in->active_count;
4904 out->lookup_count = in->lookup_count;
4905 out->matched_count = in->matched_count;
4909 ofputil_encode_table_stats_reply(const struct ofp12_table_stats stats[], int n,
4910 const struct ofp_header *request)
4912 struct ofpbuf *reply;
4915 reply = ofpraw_alloc_stats_reply(request, n * sizeof *stats);
4917 for (i = 0; i < n; i++) {
4918 switch ((enum ofp_version) request->version) {
4920 ofputil_put_ofp10_table_stats(&stats[i], reply);
4924 ofputil_put_ofp11_table_stats(&stats[i], reply);
4928 ofputil_put_ofp12_table_stats(&stats[i], reply);
4933 ofputil_put_ofp13_table_stats(&stats[i], reply);
4944 /* ofputil_flow_monitor_request */
4946 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
4947 * ofputil_flow_monitor_request in 'rq'.
4949 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
4950 * message. Calling this function multiple times for a single 'msg' iterates
4951 * through the requests. The caller must initially leave 'msg''s layer
4952 * pointers null and not modify them between calls.
4954 * Returns 0 if successful, EOF if no requests were left in this 'msg',
4955 * otherwise an OFPERR_* value. */
4957 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request *rq,
4960 struct nx_flow_monitor_request *nfmr;
4964 ofpraw_pull_assert(msg);
4967 if (!ofpbuf_size(msg)) {
4971 nfmr = ofpbuf_try_pull(msg, sizeof *nfmr);
4973 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR request has %"PRIu32" "
4974 "leftover bytes at end", ofpbuf_size(msg));
4975 return OFPERR_OFPBRC_BAD_LEN;
4978 flags = ntohs(nfmr->flags);
4979 if (!(flags & (NXFMF_ADD | NXFMF_DELETE | NXFMF_MODIFY))
4980 || flags & ~(NXFMF_INITIAL | NXFMF_ADD | NXFMF_DELETE
4981 | NXFMF_MODIFY | NXFMF_ACTIONS | NXFMF_OWN)) {
4982 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR has bad flags %#"PRIx16,
4984 return OFPERR_NXBRC_FM_BAD_FLAGS;
4987 if (!is_all_zeros(nfmr->zeros, sizeof nfmr->zeros)) {
4988 return OFPERR_NXBRC_MUST_BE_ZERO;
4991 rq->id = ntohl(nfmr->id);
4993 rq->out_port = u16_to_ofp(ntohs(nfmr->out_port));
4994 rq->table_id = nfmr->table_id;
4996 return nx_pull_match(msg, ntohs(nfmr->match_len), &rq->match, NULL, NULL);
5000 ofputil_append_flow_monitor_request(
5001 const struct ofputil_flow_monitor_request *rq, struct ofpbuf *msg)
5003 struct nx_flow_monitor_request *nfmr;
5007 if (!ofpbuf_size(msg)) {
5008 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST, OFP10_VERSION, msg);
5011 start_ofs = ofpbuf_size(msg);
5012 ofpbuf_put_zeros(msg, sizeof *nfmr);
5013 match_len = nx_put_match(msg, &rq->match, htonll(0), htonll(0));
5015 nfmr = ofpbuf_at_assert(msg, start_ofs, sizeof *nfmr);
5016 nfmr->id = htonl(rq->id);
5017 nfmr->flags = htons(rq->flags);
5018 nfmr->out_port = htons(ofp_to_u16(rq->out_port));
5019 nfmr->match_len = htons(match_len);
5020 nfmr->table_id = rq->table_id;
5023 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
5024 * into an abstract ofputil_flow_update in 'update'. The caller must have
5025 * initialized update->match to point to space allocated for a match.
5027 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
5028 * actions (except for NXFME_ABBREV, which never includes actions). The caller
5029 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
5030 * will point into the 'ofpacts' buffer.
5032 * Multiple flow updates can be packed into a single OpenFlow message. Calling
5033 * this function multiple times for a single 'msg' iterates through the
5034 * updates. The caller must initially leave 'msg''s layer pointers null and
5035 * not modify them between calls.
5037 * Returns 0 if successful, EOF if no updates were left in this 'msg',
5038 * otherwise an OFPERR_* value. */
5040 ofputil_decode_flow_update(struct ofputil_flow_update *update,
5041 struct ofpbuf *msg, struct ofpbuf *ofpacts)
5043 struct nx_flow_update_header *nfuh;
5044 unsigned int length;
5045 struct ofp_header *oh;
5048 ofpraw_pull_assert(msg);
5051 if (!ofpbuf_size(msg)) {
5055 if (ofpbuf_size(msg) < sizeof(struct nx_flow_update_header)) {
5061 nfuh = ofpbuf_data(msg);
5062 update->event = ntohs(nfuh->event);
5063 length = ntohs(nfuh->length);
5064 if (length > ofpbuf_size(msg) || length % 8) {
5068 if (update->event == NXFME_ABBREV) {
5069 struct nx_flow_update_abbrev *nfua;
5071 if (length != sizeof *nfua) {
5075 nfua = ofpbuf_pull(msg, sizeof *nfua);
5076 update->xid = nfua->xid;
5078 } else if (update->event == NXFME_ADDED
5079 || update->event == NXFME_DELETED
5080 || update->event == NXFME_MODIFIED) {
5081 struct nx_flow_update_full *nfuf;
5082 unsigned int actions_len;
5083 unsigned int match_len;
5086 if (length < sizeof *nfuf) {
5090 nfuf = ofpbuf_pull(msg, sizeof *nfuf);
5091 match_len = ntohs(nfuf->match_len);
5092 if (sizeof *nfuf + match_len > length) {
5096 update->reason = ntohs(nfuf->reason);
5097 update->idle_timeout = ntohs(nfuf->idle_timeout);
5098 update->hard_timeout = ntohs(nfuf->hard_timeout);
5099 update->table_id = nfuf->table_id;
5100 update->cookie = nfuf->cookie;
5101 update->priority = ntohs(nfuf->priority);
5103 error = nx_pull_match(msg, match_len, update->match, NULL, NULL);
5108 actions_len = length - sizeof *nfuf - ROUND_UP(match_len, 8);
5109 error = ofpacts_pull_openflow_actions(msg, actions_len, oh->version,
5115 update->ofpacts = ofpbuf_data(ofpacts);
5116 update->ofpacts_len = ofpbuf_size(ofpacts);
5119 VLOG_WARN_RL(&bad_ofmsg_rl,
5120 "NXST_FLOW_MONITOR reply has bad event %"PRIu16,
5121 ntohs(nfuh->event));
5122 return OFPERR_NXBRC_FM_BAD_EVENT;
5126 VLOG_WARN_RL(&bad_ofmsg_rl, "NXST_FLOW_MONITOR reply has %"PRIu32" "
5127 "leftover bytes at end", ofpbuf_size(msg));
5128 return OFPERR_OFPBRC_BAD_LEN;
5132 ofputil_decode_flow_monitor_cancel(const struct ofp_header *oh)
5134 const struct nx_flow_monitor_cancel *cancel = ofpmsg_body(oh);
5136 return ntohl(cancel->id);
5140 ofputil_encode_flow_monitor_cancel(uint32_t id)
5142 struct nx_flow_monitor_cancel *nfmc;
5145 msg = ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL, OFP10_VERSION, 0);
5146 nfmc = ofpbuf_put_uninit(msg, sizeof *nfmc);
5147 nfmc->id = htonl(id);
5152 ofputil_start_flow_update(struct list *replies)
5156 msg = ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY, OFP10_VERSION,
5160 list_push_back(replies, &msg->list_node);
5164 ofputil_append_flow_update(const struct ofputil_flow_update *update,
5165 struct list *replies)
5167 enum ofp_version version = ofpmp_version(replies);
5168 struct nx_flow_update_header *nfuh;
5172 msg = ofpbuf_from_list(list_back(replies));
5173 start_ofs = ofpbuf_size(msg);
5175 if (update->event == NXFME_ABBREV) {
5176 struct nx_flow_update_abbrev *nfua;
5178 nfua = ofpbuf_put_zeros(msg, sizeof *nfua);
5179 nfua->xid = update->xid;
5181 struct nx_flow_update_full *nfuf;
5184 ofpbuf_put_zeros(msg, sizeof *nfuf);
5185 match_len = nx_put_match(msg, update->match, htonll(0), htonll(0));
5186 ofpacts_put_openflow_actions(update->ofpacts, update->ofpacts_len, msg,
5188 nfuf = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuf);
5189 nfuf->reason = htons(update->reason);
5190 nfuf->priority = htons(update->priority);
5191 nfuf->idle_timeout = htons(update->idle_timeout);
5192 nfuf->hard_timeout = htons(update->hard_timeout);
5193 nfuf->match_len = htons(match_len);
5194 nfuf->table_id = update->table_id;
5195 nfuf->cookie = update->cookie;
5198 nfuh = ofpbuf_at_assert(msg, start_ofs, sizeof *nfuh);
5199 nfuh->length = htons(ofpbuf_size(msg) - start_ofs);
5200 nfuh->event = htons(update->event);
5202 ofpmp_postappend(replies, start_ofs);
5206 ofputil_encode_packet_out(const struct ofputil_packet_out *po,
5207 enum ofputil_protocol protocol)
5209 enum ofp_version ofp_version = ofputil_protocol_to_ofp_version(protocol);
5213 size = po->ofpacts_len;
5214 if (po->buffer_id == UINT32_MAX) {
5215 size += po->packet_len;
5218 switch (ofp_version) {
5219 case OFP10_VERSION: {
5220 struct ofp10_packet_out *opo;
5223 msg = ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT, OFP10_VERSION, size);
5224 ofpbuf_put_zeros(msg, sizeof *opo);
5225 actions_ofs = ofpbuf_size(msg);
5226 ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
5229 opo = ofpbuf_l3(msg);
5230 opo->buffer_id = htonl(po->buffer_id);
5231 opo->in_port = htons(ofp_to_u16(po->in_port));
5232 opo->actions_len = htons(ofpbuf_size(msg) - actions_ofs);
5239 case OFP14_VERSION:{
5240 struct ofp11_packet_out *opo;
5243 msg = ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT, ofp_version, size);
5244 ofpbuf_put_zeros(msg, sizeof *opo);
5245 len = ofpacts_put_openflow_actions(po->ofpacts, po->ofpacts_len, msg,
5247 opo = ofpbuf_l3(msg);
5248 opo->buffer_id = htonl(po->buffer_id);
5249 opo->in_port = ofputil_port_to_ofp11(po->in_port);
5250 opo->actions_len = htons(len);
5258 if (po->buffer_id == UINT32_MAX) {
5259 ofpbuf_put(msg, po->packet, po->packet_len);
5262 ofpmsg_update_length(msg);
5267 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
5269 make_echo_request(enum ofp_version ofp_version)
5271 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST, ofp_version,
5275 /* Creates and returns an OFPT_ECHO_REPLY message matching the
5276 * OFPT_ECHO_REQUEST message in 'rq'. */
5278 make_echo_reply(const struct ofp_header *rq)
5280 struct ofpbuf rq_buf;
5281 struct ofpbuf *reply;
5283 ofpbuf_use_const(&rq_buf, rq, ntohs(rq->length));
5284 ofpraw_pull_assert(&rq_buf);
5286 reply = ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY, rq, ofpbuf_size(&rq_buf));
5287 ofpbuf_put(reply, ofpbuf_data(&rq_buf), ofpbuf_size(&rq_buf));
5292 ofputil_encode_barrier_request(enum ofp_version ofp_version)
5296 switch (ofp_version) {
5301 type = OFPRAW_OFPT11_BARRIER_REQUEST;
5305 type = OFPRAW_OFPT10_BARRIER_REQUEST;
5312 return ofpraw_alloc(type, ofp_version, 0);
5316 ofputil_frag_handling_to_string(enum ofp_config_flags flags)
5318 switch (flags & OFPC_FRAG_MASK) {
5319 case OFPC_FRAG_NORMAL: return "normal";
5320 case OFPC_FRAG_DROP: return "drop";
5321 case OFPC_FRAG_REASM: return "reassemble";
5322 case OFPC_FRAG_NX_MATCH: return "nx-match";
5329 ofputil_frag_handling_from_string(const char *s, enum ofp_config_flags *flags)
5331 if (!strcasecmp(s, "normal")) {
5332 *flags = OFPC_FRAG_NORMAL;
5333 } else if (!strcasecmp(s, "drop")) {
5334 *flags = OFPC_FRAG_DROP;
5335 } else if (!strcasecmp(s, "reassemble")) {
5336 *flags = OFPC_FRAG_REASM;
5337 } else if (!strcasecmp(s, "nx-match")) {
5338 *flags = OFPC_FRAG_NX_MATCH;
5345 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
5346 * port number and stores the latter in '*ofp10_port', for the purpose of
5347 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
5348 * otherwise an OFPERR_* number. On error, stores OFPP_NONE in '*ofp10_port'.
5350 * See the definition of OFP11_MAX for an explanation of the mapping. */
5352 ofputil_port_from_ofp11(ovs_be32 ofp11_port, ofp_port_t *ofp10_port)
5354 uint32_t ofp11_port_h = ntohl(ofp11_port);
5356 if (ofp11_port_h < ofp_to_u16(OFPP_MAX)) {
5357 *ofp10_port = u16_to_ofp(ofp11_port_h);
5359 } else if (ofp11_port_h >= ofp11_to_u32(OFPP11_MAX)) {
5360 *ofp10_port = u16_to_ofp(ofp11_port_h - OFPP11_OFFSET);
5363 *ofp10_port = OFPP_NONE;
5364 VLOG_WARN_RL(&bad_ofmsg_rl, "port %"PRIu32" is outside the supported "
5365 "range 0 through %d or 0x%"PRIx32" through 0x%"PRIx32,
5366 ofp11_port_h, ofp_to_u16(OFPP_MAX) - 1,
5367 ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
5368 return OFPERR_OFPBAC_BAD_OUT_PORT;
5372 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
5373 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
5375 * See the definition of OFP11_MAX for an explanation of the mapping. */
5377 ofputil_port_to_ofp11(ofp_port_t ofp10_port)
5379 return htonl(ofp_to_u16(ofp10_port) < ofp_to_u16(OFPP_MAX)
5380 ? ofp_to_u16(ofp10_port)
5381 : ofp_to_u16(ofp10_port) + OFPP11_OFFSET);
5384 #define OFPUTIL_NAMED_PORTS \
5385 OFPUTIL_NAMED_PORT(IN_PORT) \
5386 OFPUTIL_NAMED_PORT(TABLE) \
5387 OFPUTIL_NAMED_PORT(NORMAL) \
5388 OFPUTIL_NAMED_PORT(FLOOD) \
5389 OFPUTIL_NAMED_PORT(ALL) \
5390 OFPUTIL_NAMED_PORT(CONTROLLER) \
5391 OFPUTIL_NAMED_PORT(LOCAL) \
5392 OFPUTIL_NAMED_PORT(ANY)
5394 /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */
5395 #define OFPUTIL_NAMED_PORTS_WITH_NONE \
5396 OFPUTIL_NAMED_PORTS \
5397 OFPUTIL_NAMED_PORT(NONE)
5399 /* Stores the port number represented by 's' into '*portp'. 's' may be an
5400 * integer or, for reserved ports, the standard OpenFlow name for the port
5403 * Returns true if successful, false if 's' is not a valid OpenFlow port number
5404 * or name. The caller should issue an error message in this case, because
5405 * this function usually does not. (This gives the caller an opportunity to
5406 * look up the port name another way, e.g. by contacting the switch and listing
5407 * the names of all its ports).
5409 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
5410 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
5411 * range as described in include/openflow/openflow-1.1.h. */
5413 ofputil_port_from_string(const char *s, ofp_port_t *portp)
5415 unsigned int port32; /* int is at least 32 bits wide. */
5418 VLOG_WARN("Negative value %s is not a valid port number.", s);
5422 if (str_to_uint(s, 10, &port32)) {
5423 if (port32 < ofp_to_u16(OFPP_MAX)) {
5425 } else if (port32 < ofp_to_u16(OFPP_FIRST_RESV)) {
5426 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
5427 "be translated to %u when talking to an OF1.1 or "
5428 "later controller", port32, port32 + OFPP11_OFFSET);
5429 } else if (port32 <= ofp_to_u16(OFPP_LAST_RESV)) {
5430 char name[OFP_MAX_PORT_NAME_LEN];
5432 ofputil_port_to_string(u16_to_ofp(port32), name, sizeof name);
5433 VLOG_WARN_ONCE("referring to port %s as %"PRIu32" is deprecated "
5434 "for compatibility with OpenFlow 1.1 and later",
5436 } else if (port32 < ofp11_to_u32(OFPP11_MAX)) {
5437 VLOG_WARN("port %u is outside the supported range 0 through "
5438 "%"PRIx16" or 0x%x through 0x%"PRIx32, port32,
5439 UINT16_MAX, ofp11_to_u32(OFPP11_MAX), UINT32_MAX);
5442 port32 -= OFPP11_OFFSET;
5445 *portp = u16_to_ofp(port32);
5452 static const struct pair pairs[] = {
5453 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
5454 OFPUTIL_NAMED_PORTS_WITH_NONE
5455 #undef OFPUTIL_NAMED_PORT
5457 const struct pair *p;
5459 for (p = pairs; p < &pairs[ARRAY_SIZE(pairs)]; p++) {
5460 if (!strcasecmp(s, p->name)) {
5469 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
5470 * Most ports' string representation is just the port number, but for special
5471 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
5473 ofputil_format_port(ofp_port_t port, struct ds *s)
5475 char name[OFP_MAX_PORT_NAME_LEN];
5477 ofputil_port_to_string(port, name, sizeof name);
5478 ds_put_cstr(s, name);
5481 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
5482 * representation of OpenFlow port number 'port'. Most ports are represented
5483 * as just the port number, but special ports, e.g. OFPP_LOCAL, are represented
5484 * by name, e.g. "LOCAL". */
5486 ofputil_port_to_string(ofp_port_t port,
5487 char namebuf[OFP_MAX_PORT_NAME_LEN], size_t bufsize)
5490 #define OFPUTIL_NAMED_PORT(NAME) \
5492 ovs_strlcpy(namebuf, #NAME, bufsize); \
5495 #undef OFPUTIL_NAMED_PORT
5498 snprintf(namebuf, bufsize, "%"PRIu16, port);
5503 /* Stores the group id represented by 's' into '*group_idp'. 's' may be an
5504 * integer or, for reserved group IDs, the standard OpenFlow name for the group
5505 * (either "ANY" or "ALL").
5507 * Returns true if successful, false if 's' is not a valid OpenFlow group ID or
5510 ofputil_group_from_string(const char *s, uint32_t *group_idp)
5512 if (!strcasecmp(s, "any")) {
5513 *group_idp = OFPG11_ANY;
5514 } else if (!strcasecmp(s, "all")) {
5515 *group_idp = OFPG11_ALL;
5516 } else if (!str_to_uint(s, 10, group_idp)) {
5517 VLOG_WARN("%s is not a valid group ID. (Valid group IDs are "
5518 "32-bit nonnegative integers or the keywords ANY or "
5526 /* Appends to 's' a string representation of the OpenFlow group ID 'group_id'.
5527 * Most groups' string representation is just the number, but for special
5528 * groups, e.g. OFPG11_ALL, it is the name, e.g. "ALL". */
5530 ofputil_format_group(uint32_t group_id, struct ds *s)
5532 char name[MAX_GROUP_NAME_LEN];
5534 ofputil_group_to_string(group_id, name, sizeof name);
5535 ds_put_cstr(s, name);
5539 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
5540 * representation of OpenFlow group ID 'group_id'. Most group are represented
5541 * as just their number, but special groups, e.g. OFPG11_ALL, are represented
5542 * by name, e.g. "ALL". */
5544 ofputil_group_to_string(uint32_t group_id,
5545 char namebuf[MAX_GROUP_NAME_LEN + 1], size_t bufsize)
5549 ovs_strlcpy(namebuf, "ALL", bufsize);
5553 ovs_strlcpy(namebuf, "ANY", bufsize);
5557 snprintf(namebuf, bufsize, "%"PRIu32, group_id);
5562 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
5563 * 'ofp_version', tries to pull the first element from the array. If
5564 * successful, initializes '*pp' with an abstract representation of the
5565 * port and returns 0. If no ports remain to be decoded, returns EOF.
5566 * On an error, returns a positive OFPERR_* value. */
5568 ofputil_pull_phy_port(enum ofp_version ofp_version, struct ofpbuf *b,
5569 struct ofputil_phy_port *pp)
5571 switch (ofp_version) {
5572 case OFP10_VERSION: {
5573 const struct ofp10_phy_port *opp = ofpbuf_try_pull(b, sizeof *opp);
5574 return opp ? ofputil_decode_ofp10_phy_port(pp, opp) : EOF;
5578 case OFP13_VERSION: {
5579 const struct ofp11_port *op = ofpbuf_try_pull(b, sizeof *op);
5580 return op ? ofputil_decode_ofp11_port(pp, op) : EOF;
5590 /* ofp-util.def lists the mapping from names to action. */
5591 static const char *const names[OFPUTIL_N_ACTIONS] = {
5593 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) NAME,
5594 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
5595 #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
5596 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) NAME,
5597 #include "ofp-util.def"
5600 /* Returns the 'enum ofputil_action_code' corresponding to 'name' (e.g. if
5601 * 'name' is "output" then the return value is OFPUTIL_OFPAT10_OUTPUT), or -1
5602 * if 'name' is not the name of any action. */
5604 ofputil_action_code_from_name(const char *name)
5606 const char *const *p;
5608 for (p = names; p < &names[ARRAY_SIZE(names)]; p++) {
5609 if (*p && !strcasecmp(name, *p)) {
5616 /* Returns name corresponding to the 'enum ofputil_action_code',
5617 * or "Unkonwn action", if the name is not available. */
5619 ofputil_action_name_from_code(enum ofputil_action_code code)
5621 return code < (int)OFPUTIL_N_ACTIONS && names[code] ? names[code]
5625 enum ofputil_action_code
5626 ofputil_action_code_from_ofp13_action(enum ofp13_action_type type)
5630 #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
5632 return OFPUTIL_##ENUM;
5633 #include "ofp-util.def"
5636 return OFPUTIL_ACTION_INVALID;
5640 /* Appends an action of the type specified by 'code' to 'buf' and returns the
5641 * action. Initializes the parts of 'action' that identify it as having type
5642 * <ENUM> and length 'sizeof *action' and zeros the rest. For actions that
5643 * have variable length, the length used and cleared is that of struct
5646 ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
5649 case OFPUTIL_ACTION_INVALID:
5650 #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
5651 #include "ofp-util.def"
5654 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
5655 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
5656 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
5657 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
5658 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
5659 case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
5660 #include "ofp-util.def"
5665 #define OFPAT10_ACTION(ENUM, STRUCT, NAME) \
5667 ofputil_init_##ENUM(struct STRUCT *s) \
5669 memset(s, 0, sizeof *s); \
5670 s->type = htons(ENUM); \
5671 s->len = htons(sizeof *s); \
5675 ofputil_put_##ENUM(struct ofpbuf *buf) \
5677 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
5678 ofputil_init_##ENUM(s); \
5681 #define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
5682 OFPAT10_ACTION(ENUM, STRUCT, NAME)
5683 #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
5684 OFPAT10_ACTION(ENUM, STRUCT, NAME)
5685 #define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
5687 ofputil_init_##ENUM(struct STRUCT *s) \
5689 memset(s, 0, sizeof *s); \
5690 s->type = htons(OFPAT10_VENDOR); \
5691 s->len = htons(sizeof *s); \
5692 s->vendor = htonl(NX_VENDOR_ID); \
5693 s->subtype = htons(ENUM); \
5697 ofputil_put_##ENUM(struct ofpbuf *buf) \
5699 struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s); \
5700 ofputil_init_##ENUM(s); \
5703 #include "ofp-util.def"
5706 ofputil_normalize_match__(struct match *match, bool may_log)
5709 MAY_NW_ADDR = 1 << 0, /* nw_src, nw_dst */
5710 MAY_TP_ADDR = 1 << 1, /* tp_src, tp_dst */
5711 MAY_NW_PROTO = 1 << 2, /* nw_proto */
5712 MAY_IPVx = 1 << 3, /* tos, frag, ttl */
5713 MAY_ARP_SHA = 1 << 4, /* arp_sha */
5714 MAY_ARP_THA = 1 << 5, /* arp_tha */
5715 MAY_IPV6 = 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
5716 MAY_ND_TARGET = 1 << 7, /* nd_target */
5717 MAY_MPLS = 1 << 8, /* mpls label and tc */
5720 struct flow_wildcards wc;
5722 /* Figure out what fields may be matched. */
5723 if (match->flow.dl_type == htons(ETH_TYPE_IP)) {
5724 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_NW_ADDR;
5725 if (match->flow.nw_proto == IPPROTO_TCP ||
5726 match->flow.nw_proto == IPPROTO_UDP ||
5727 match->flow.nw_proto == IPPROTO_SCTP ||
5728 match->flow.nw_proto == IPPROTO_ICMP) {
5729 may_match |= MAY_TP_ADDR;
5731 } else if (match->flow.dl_type == htons(ETH_TYPE_IPV6)) {
5732 may_match = MAY_NW_PROTO | MAY_IPVx | MAY_IPV6;
5733 if (match->flow.nw_proto == IPPROTO_TCP ||
5734 match->flow.nw_proto == IPPROTO_UDP ||
5735 match->flow.nw_proto == IPPROTO_SCTP) {
5736 may_match |= MAY_TP_ADDR;
5737 } else if (match->flow.nw_proto == IPPROTO_ICMPV6) {
5738 may_match |= MAY_TP_ADDR;
5739 if (match->flow.tp_src == htons(ND_NEIGHBOR_SOLICIT)) {
5740 may_match |= MAY_ND_TARGET | MAY_ARP_SHA;
5741 } else if (match->flow.tp_src == htons(ND_NEIGHBOR_ADVERT)) {
5742 may_match |= MAY_ND_TARGET | MAY_ARP_THA;
5745 } else if (match->flow.dl_type == htons(ETH_TYPE_ARP) ||
5746 match->flow.dl_type == htons(ETH_TYPE_RARP)) {
5747 may_match = MAY_NW_PROTO | MAY_NW_ADDR | MAY_ARP_SHA | MAY_ARP_THA;
5748 } else if (eth_type_mpls(match->flow.dl_type)) {
5749 may_match = MAY_MPLS;
5754 /* Clear the fields that may not be matched. */
5756 if (!(may_match & MAY_NW_ADDR)) {
5757 wc.masks.nw_src = wc.masks.nw_dst = htonl(0);
5759 if (!(may_match & MAY_TP_ADDR)) {
5760 wc.masks.tp_src = wc.masks.tp_dst = htons(0);
5762 if (!(may_match & MAY_NW_PROTO)) {
5763 wc.masks.nw_proto = 0;
5765 if (!(may_match & MAY_IPVx)) {
5766 wc.masks.nw_tos = 0;
5767 wc.masks.nw_ttl = 0;
5769 if (!(may_match & MAY_ARP_SHA)) {
5770 memset(wc.masks.arp_sha, 0, ETH_ADDR_LEN);
5772 if (!(may_match & MAY_ARP_THA)) {
5773 memset(wc.masks.arp_tha, 0, ETH_ADDR_LEN);
5775 if (!(may_match & MAY_IPV6)) {
5776 wc.masks.ipv6_src = wc.masks.ipv6_dst = in6addr_any;
5777 wc.masks.ipv6_label = htonl(0);
5779 if (!(may_match & MAY_ND_TARGET)) {
5780 wc.masks.nd_target = in6addr_any;
5782 if (!(may_match & MAY_MPLS)) {
5783 memset(wc.masks.mpls_lse, 0, sizeof wc.masks.mpls_lse);
5786 /* Log any changes. */
5787 if (!flow_wildcards_equal(&wc, &match->wc)) {
5788 bool log = may_log && !VLOG_DROP_INFO(&bad_ofmsg_rl);
5789 char *pre = log ? match_to_string(match, OFP_DEFAULT_PRIORITY) : NULL;
5792 match_zero_wildcarded_fields(match);
5795 char *post = match_to_string(match, OFP_DEFAULT_PRIORITY);
5796 VLOG_INFO("normalization changed ofp_match, details:");
5797 VLOG_INFO(" pre: %s", pre);
5798 VLOG_INFO("post: %s", post);
5805 /* "Normalizes" the wildcards in 'match'. That means:
5807 * 1. If the type of level N is known, then only the valid fields for that
5808 * level may be specified. For example, ARP does not have a TOS field,
5809 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
5810 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
5811 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
5814 * 2. If the type of level N is not known (or not understood by Open
5815 * vSwitch), then no fields at all for that level may be specified. For
5816 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
5817 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
5820 * If this function changes 'match', it logs a rate-limited informational
5823 ofputil_normalize_match(struct match *match)
5825 ofputil_normalize_match__(match, true);
5828 /* Same as ofputil_normalize_match() without the logging. Thus, this function
5829 * is suitable for a program's internal use, whereas ofputil_normalize_match()
5830 * sense for use on flows received from elsewhere (so that a bug in the program
5831 * that sent them can be reported and corrected). */
5833 ofputil_normalize_match_quiet(struct match *match)
5835 ofputil_normalize_match__(match, false);
5838 /* Parses a key or a key-value pair from '*stringp'.
5840 * On success: Stores the key into '*keyp'. Stores the value, if present, into
5841 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
5842 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
5843 * are substrings of '*stringp' created by replacing some of its bytes by null
5844 * terminators. Returns true.
5846 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
5847 * NULL and returns false. */
5849 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
5851 char *pos, *key, *value;
5855 pos += strspn(pos, ", \t\r\n");
5857 *keyp = *valuep = NULL;
5862 key_len = strcspn(pos, ":=(, \t\r\n");
5863 if (key[key_len] == ':' || key[key_len] == '=') {
5864 /* The value can be separated by a colon. */
5867 value = key + key_len + 1;
5868 value_len = strcspn(value, ", \t\r\n");
5869 pos = value + value_len + (value[value_len] != '\0');
5870 value[value_len] = '\0';
5871 } else if (key[key_len] == '(') {
5872 /* The value can be surrounded by balanced parentheses. The outermost
5873 * set of parentheses is removed. */
5877 value = key + key_len + 1;
5878 for (value_len = 0; level > 0; value_len++) {
5879 switch (value[value_len]) {
5893 value[value_len - 1] = '\0';
5894 pos = value + value_len;
5896 /* There might be no value at all. */
5897 value = key + key_len; /* Will become the empty string below. */
5898 pos = key + key_len + (key[key_len] != '\0');
5900 key[key_len] = '\0';
5908 /* Encode a dump ports request for 'port', the encoded message
5909 * will be for Open Flow version 'ofp_version'. Returns message
5910 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
5912 ofputil_encode_dump_ports_request(enum ofp_version ofp_version, ofp_port_t port)
5914 struct ofpbuf *request;
5916 switch (ofp_version) {
5917 case OFP10_VERSION: {
5918 struct ofp10_port_stats_request *req;
5919 request = ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST, ofp_version, 0);
5920 req = ofpbuf_put_zeros(request, sizeof *req);
5921 req->port_no = htons(ofp_to_u16(port));
5927 case OFP14_VERSION:{
5928 struct ofp11_port_stats_request *req;
5929 request = ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST, ofp_version, 0);
5930 req = ofpbuf_put_zeros(request, sizeof *req);
5931 req->port_no = ofputil_port_to_ofp11(port);
5942 ofputil_port_stats_to_ofp10(const struct ofputil_port_stats *ops,
5943 struct ofp10_port_stats *ps10)
5945 ps10->port_no = htons(ofp_to_u16(ops->port_no));
5946 memset(ps10->pad, 0, sizeof ps10->pad);
5947 put_32aligned_be64(&ps10->rx_packets, htonll(ops->stats.rx_packets));
5948 put_32aligned_be64(&ps10->tx_packets, htonll(ops->stats.tx_packets));
5949 put_32aligned_be64(&ps10->rx_bytes, htonll(ops->stats.rx_bytes));
5950 put_32aligned_be64(&ps10->tx_bytes, htonll(ops->stats.tx_bytes));
5951 put_32aligned_be64(&ps10->rx_dropped, htonll(ops->stats.rx_dropped));
5952 put_32aligned_be64(&ps10->tx_dropped, htonll(ops->stats.tx_dropped));
5953 put_32aligned_be64(&ps10->rx_errors, htonll(ops->stats.rx_errors));
5954 put_32aligned_be64(&ps10->tx_errors, htonll(ops->stats.tx_errors));
5955 put_32aligned_be64(&ps10->rx_frame_err, htonll(ops->stats.rx_frame_errors));
5956 put_32aligned_be64(&ps10->rx_over_err, htonll(ops->stats.rx_over_errors));
5957 put_32aligned_be64(&ps10->rx_crc_err, htonll(ops->stats.rx_crc_errors));
5958 put_32aligned_be64(&ps10->collisions, htonll(ops->stats.collisions));
5962 ofputil_port_stats_to_ofp11(const struct ofputil_port_stats *ops,
5963 struct ofp11_port_stats *ps11)
5965 ps11->port_no = ofputil_port_to_ofp11(ops->port_no);
5966 memset(ps11->pad, 0, sizeof ps11->pad);
5967 ps11->rx_packets = htonll(ops->stats.rx_packets);
5968 ps11->tx_packets = htonll(ops->stats.tx_packets);
5969 ps11->rx_bytes = htonll(ops->stats.rx_bytes);
5970 ps11->tx_bytes = htonll(ops->stats.tx_bytes);
5971 ps11->rx_dropped = htonll(ops->stats.rx_dropped);
5972 ps11->tx_dropped = htonll(ops->stats.tx_dropped);
5973 ps11->rx_errors = htonll(ops->stats.rx_errors);
5974 ps11->tx_errors = htonll(ops->stats.tx_errors);
5975 ps11->rx_frame_err = htonll(ops->stats.rx_frame_errors);
5976 ps11->rx_over_err = htonll(ops->stats.rx_over_errors);
5977 ps11->rx_crc_err = htonll(ops->stats.rx_crc_errors);
5978 ps11->collisions = htonll(ops->stats.collisions);
5982 ofputil_port_stats_to_ofp13(const struct ofputil_port_stats *ops,
5983 struct ofp13_port_stats *ps13)
5985 ofputil_port_stats_to_ofp11(ops, &ps13->ps);
5986 ps13->duration_sec = htonl(ops->duration_sec);
5987 ps13->duration_nsec = htonl(ops->duration_nsec);
5991 /* Encode a ports stat for 'ops' and append it to 'replies'. */
5993 ofputil_append_port_stat(struct list *replies,
5994 const struct ofputil_port_stats *ops)
5996 switch (ofpmp_version(replies)) {
5997 case OFP13_VERSION: {
5998 struct ofp13_port_stats *reply = ofpmp_append(replies, sizeof *reply);
5999 ofputil_port_stats_to_ofp13(ops, reply);
6003 case OFP11_VERSION: {
6004 struct ofp11_port_stats *reply = ofpmp_append(replies, sizeof *reply);
6005 ofputil_port_stats_to_ofp11(ops, reply);
6009 case OFP10_VERSION: {
6010 struct ofp10_port_stats *reply = ofpmp_append(replies, sizeof *reply);
6011 ofputil_port_stats_to_ofp10(ops, reply);
6025 ofputil_port_stats_from_ofp10(struct ofputil_port_stats *ops,
6026 const struct ofp10_port_stats *ps10)
6028 memset(ops, 0, sizeof *ops);
6030 ops->port_no = u16_to_ofp(ntohs(ps10->port_no));
6031 ops->stats.rx_packets = ntohll(get_32aligned_be64(&ps10->rx_packets));
6032 ops->stats.tx_packets = ntohll(get_32aligned_be64(&ps10->tx_packets));
6033 ops->stats.rx_bytes = ntohll(get_32aligned_be64(&ps10->rx_bytes));
6034 ops->stats.tx_bytes = ntohll(get_32aligned_be64(&ps10->tx_bytes));
6035 ops->stats.rx_dropped = ntohll(get_32aligned_be64(&ps10->rx_dropped));
6036 ops->stats.tx_dropped = ntohll(get_32aligned_be64(&ps10->tx_dropped));
6037 ops->stats.rx_errors = ntohll(get_32aligned_be64(&ps10->rx_errors));
6038 ops->stats.tx_errors = ntohll(get_32aligned_be64(&ps10->tx_errors));
6039 ops->stats.rx_frame_errors =
6040 ntohll(get_32aligned_be64(&ps10->rx_frame_err));
6041 ops->stats.rx_over_errors = ntohll(get_32aligned_be64(&ps10->rx_over_err));
6042 ops->stats.rx_crc_errors = ntohll(get_32aligned_be64(&ps10->rx_crc_err));
6043 ops->stats.collisions = ntohll(get_32aligned_be64(&ps10->collisions));
6044 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
6050 ofputil_port_stats_from_ofp11(struct ofputil_port_stats *ops,
6051 const struct ofp11_port_stats *ps11)
6055 memset(ops, 0, sizeof *ops);
6056 error = ofputil_port_from_ofp11(ps11->port_no, &ops->port_no);
6061 ops->stats.rx_packets = ntohll(ps11->rx_packets);
6062 ops->stats.tx_packets = ntohll(ps11->tx_packets);
6063 ops->stats.rx_bytes = ntohll(ps11->rx_bytes);
6064 ops->stats.tx_bytes = ntohll(ps11->tx_bytes);
6065 ops->stats.rx_dropped = ntohll(ps11->rx_dropped);
6066 ops->stats.tx_dropped = ntohll(ps11->tx_dropped);
6067 ops->stats.rx_errors = ntohll(ps11->rx_errors);
6068 ops->stats.tx_errors = ntohll(ps11->tx_errors);
6069 ops->stats.rx_frame_errors = ntohll(ps11->rx_frame_err);
6070 ops->stats.rx_over_errors = ntohll(ps11->rx_over_err);
6071 ops->stats.rx_crc_errors = ntohll(ps11->rx_crc_err);
6072 ops->stats.collisions = ntohll(ps11->collisions);
6073 ops->duration_sec = ops->duration_nsec = UINT32_MAX;
6079 ofputil_port_stats_from_ofp13(struct ofputil_port_stats *ops,
6080 const struct ofp13_port_stats *ps13)
6082 enum ofperr error = ofputil_port_stats_from_ofp11(ops, &ps13->ps);
6084 ops->duration_sec = ntohl(ps13->duration_sec);
6085 ops->duration_nsec = ntohl(ps13->duration_nsec);
6091 ofputil_get_port_stats_size(enum ofp_version ofp_version)
6093 switch (ofp_version) {
6095 return sizeof(struct ofp10_port_stats);
6098 return sizeof(struct ofp11_port_stats);
6100 return sizeof(struct ofp13_port_stats);
6109 /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
6112 ofputil_count_port_stats(const struct ofp_header *oh)
6116 ofpbuf_use_const(&b, oh, ntohs(oh->length));
6117 ofpraw_pull_assert(&b);
6119 return ofpbuf_size(&b) / ofputil_get_port_stats_size(oh->version);
6122 /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
6123 * ofputil_port_stats in 'ps'.
6125 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
6126 * message. Calling this function multiple times for a single 'msg' iterates
6127 * through the replies. The caller must initially leave 'msg''s layer pointers
6128 * null and not modify them between calls.
6130 * Returns 0 if successful, EOF if no replies were left in this 'msg',
6131 * otherwise a positive errno value. */
6133 ofputil_decode_port_stats(struct ofputil_port_stats *ps, struct ofpbuf *msg)
6139 ? ofpraw_decode(&raw, msg->frame)
6140 : ofpraw_pull(&raw, msg));
6145 if (!ofpbuf_size(msg)) {
6147 } else if (raw == OFPRAW_OFPST13_PORT_REPLY) {
6148 const struct ofp13_port_stats *ps13;
6150 ps13 = ofpbuf_try_pull(msg, sizeof *ps13);
6154 return ofputil_port_stats_from_ofp13(ps, ps13);
6155 } else if (raw == OFPRAW_OFPST11_PORT_REPLY) {
6156 const struct ofp11_port_stats *ps11;
6158 ps11 = ofpbuf_try_pull(msg, sizeof *ps11);
6162 return ofputil_port_stats_from_ofp11(ps, ps11);
6163 } else if (raw == OFPRAW_OFPST10_PORT_REPLY) {
6164 const struct ofp10_port_stats *ps10;
6166 ps10 = ofpbuf_try_pull(msg, sizeof *ps10);
6170 return ofputil_port_stats_from_ofp10(ps, ps10);
6176 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_PORT reply has %"PRIu32" leftover "
6177 "bytes at end", ofpbuf_size(msg));
6178 return OFPERR_OFPBRC_BAD_LEN;
6181 /* Parse a port status request message into a 16 bit OpenFlow 1.0
6182 * port number and stores the latter in '*ofp10_port'.
6183 * Returns 0 if successful, otherwise an OFPERR_* number. */
6185 ofputil_decode_port_stats_request(const struct ofp_header *request,
6186 ofp_port_t *ofp10_port)
6188 switch ((enum ofp_version)request->version) {
6191 case OFP11_VERSION: {
6192 const struct ofp11_port_stats_request *psr11 = ofpmsg_body(request);
6193 return ofputil_port_from_ofp11(psr11->port_no, ofp10_port);
6196 case OFP10_VERSION: {
6197 const struct ofp10_port_stats_request *psr10 = ofpmsg_body(request);
6198 *ofp10_port = u16_to_ofp(ntohs(psr10->port_no));
6211 /* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */
6213 ofputil_bucket_list_destroy(struct list *buckets)
6215 struct ofputil_bucket *bucket, *next_bucket;
6217 LIST_FOR_EACH_SAFE (bucket, next_bucket, list_node, buckets) {
6218 list_remove(&bucket->list_node);
6219 free(bucket->ofpacts);
6224 /* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version',
6225 * that requests stats for group 'group_id'. (Use OFPG_ALL to request stats
6228 * Group statistics include packet and byte counts for each group. */
6230 ofputil_encode_group_stats_request(enum ofp_version ofp_version,
6233 struct ofpbuf *request;
6235 switch (ofp_version) {
6237 ovs_fatal(0, "dump-group-stats needs OpenFlow 1.1 or later "
6238 "(\'-O OpenFlow11\')");
6242 case OFP14_VERSION: {
6243 struct ofp11_group_stats_request *req;
6244 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_REQUEST, ofp_version, 0);
6245 req = ofpbuf_put_zeros(request, sizeof *req);
6246 req->group_id = htonl(group_id);
6256 /* Returns an OpenFlow group description request for OpenFlow version
6257 * 'ofp_version', that requests stats for group 'group_id'. (Use OFPG_ALL to
6258 * request stats for all groups.)
6260 * Group descriptions include the bucket and action configuration for each
6263 ofputil_encode_group_desc_request(enum ofp_version ofp_version)
6265 struct ofpbuf *request;
6267 switch (ofp_version) {
6269 ovs_fatal(0, "dump-groups needs OpenFlow 1.1 or later "
6270 "(\'-O OpenFlow11\')");
6275 request = ofpraw_alloc(OFPRAW_OFPST11_GROUP_DESC_REQUEST, ofp_version, 0);
6285 ofputil_group_bucket_counters_to_ofp11(const struct ofputil_group_stats *gs,
6286 struct ofp11_bucket_counter bucket_cnts[])
6290 for (i = 0; i < gs->n_buckets; i++) {
6291 bucket_cnts[i].packet_count = htonll(gs->bucket_stats[i].packet_count);
6292 bucket_cnts[i].byte_count = htonll(gs->bucket_stats[i].byte_count);
6297 ofputil_group_stats_to_ofp11(const struct ofputil_group_stats *gs,
6298 struct ofp11_group_stats *gs11, size_t length,
6299 struct ofp11_bucket_counter bucket_cnts[])
6301 memset(gs11, 0, sizeof *gs11);
6302 gs11->length = htons(length);
6303 gs11->group_id = htonl(gs->group_id);
6304 gs11->ref_count = htonl(gs->ref_count);
6305 gs11->packet_count = htonll(gs->packet_count);
6306 gs11->byte_count = htonll(gs->byte_count);
6307 ofputil_group_bucket_counters_to_ofp11(gs, bucket_cnts);
6311 ofputil_group_stats_to_ofp13(const struct ofputil_group_stats *gs,
6312 struct ofp13_group_stats *gs13, size_t length,
6313 struct ofp11_bucket_counter bucket_cnts[])
6315 ofputil_group_stats_to_ofp11(gs, &gs13->gs, length, bucket_cnts);
6316 gs13->duration_sec = htonl(gs->duration_sec);
6317 gs13->duration_nsec = htonl(gs->duration_nsec);
6321 /* Encodes 'gs' properly for the format of the list of group statistics
6322 * replies already begun in 'replies' and appends it to the list. 'replies'
6323 * must have originally been initialized with ofpmp_init(). */
6325 ofputil_append_group_stats(struct list *replies,
6326 const struct ofputil_group_stats *gs)
6328 size_t bucket_counter_size;
6329 struct ofp11_bucket_counter *bucket_counters;
6332 bucket_counter_size = gs->n_buckets * sizeof(struct ofp11_bucket_counter);
6334 switch (ofpmp_version(replies)) {
6336 case OFP12_VERSION:{
6337 struct ofp11_group_stats *gs11;
6339 length = sizeof *gs11 + bucket_counter_size;
6340 gs11 = ofpmp_append(replies, length);
6341 bucket_counters = (struct ofp11_bucket_counter *)(gs11 + 1);
6342 ofputil_group_stats_to_ofp11(gs, gs11, length, bucket_counters);
6347 case OFP14_VERSION:{
6348 struct ofp13_group_stats *gs13;
6350 length = sizeof *gs13 + bucket_counter_size;
6351 gs13 = ofpmp_append(replies, length);
6352 bucket_counters = (struct ofp11_bucket_counter *)(gs13 + 1);
6353 ofputil_group_stats_to_ofp13(gs, gs13, length, bucket_counters);
6362 /* Returns an OpenFlow group features request for OpenFlow version
6365 ofputil_encode_group_features_request(enum ofp_version ofp_version)
6367 struct ofpbuf *request = NULL;
6369 switch (ofp_version) {
6372 ovs_fatal(0, "dump-group-features needs OpenFlow 1.2 or later "
6373 "(\'-O OpenFlow12\')");
6377 request = ofpraw_alloc(OFPRAW_OFPST12_GROUP_FEATURES_REQUEST,
6387 /* Returns a OpenFlow message that encodes 'features' properly as a reply to
6388 * group features request 'request'. */
6390 ofputil_encode_group_features_reply(
6391 const struct ofputil_group_features *features,
6392 const struct ofp_header *request)
6394 struct ofp12_group_features_stats *ogf;
6395 struct ofpbuf *reply;
6397 reply = ofpraw_alloc_xid(OFPRAW_OFPST12_GROUP_FEATURES_REPLY,
6398 request->version, request->xid, 0);
6399 ogf = ofpbuf_put_zeros(reply, sizeof *ogf);
6400 ogf->types = htonl(features->types);
6401 ogf->capabilities = htonl(features->capabilities);
6402 ogf->max_groups[0] = htonl(features->max_groups[0]);
6403 ogf->max_groups[1] = htonl(features->max_groups[1]);
6404 ogf->max_groups[2] = htonl(features->max_groups[2]);
6405 ogf->max_groups[3] = htonl(features->max_groups[3]);
6406 ogf->actions[0] = htonl(features->actions[0]);
6407 ogf->actions[1] = htonl(features->actions[1]);
6408 ogf->actions[2] = htonl(features->actions[2]);
6409 ogf->actions[3] = htonl(features->actions[3]);
6414 /* Decodes group features reply 'oh' into 'features'. */
6416 ofputil_decode_group_features_reply(const struct ofp_header *oh,
6417 struct ofputil_group_features *features)
6419 const struct ofp12_group_features_stats *ogf = ofpmsg_body(oh);
6421 features->types = ntohl(ogf->types);
6422 features->capabilities = ntohl(ogf->capabilities);
6423 features->max_groups[0] = ntohl(ogf->max_groups[0]);
6424 features->max_groups[1] = ntohl(ogf->max_groups[1]);
6425 features->max_groups[2] = ntohl(ogf->max_groups[2]);
6426 features->max_groups[3] = ntohl(ogf->max_groups[3]);
6427 features->actions[0] = ntohl(ogf->actions[0]);
6428 features->actions[1] = ntohl(ogf->actions[1]);
6429 features->actions[2] = ntohl(ogf->actions[2]);
6430 features->actions[3] = ntohl(ogf->actions[3]);
6433 /* Parse a group status request message into a 32 bit OpenFlow 1.1
6434 * group ID and stores the latter in '*group_id'.
6435 * Returns 0 if successful, otherwise an OFPERR_* number. */
6437 ofputil_decode_group_stats_request(const struct ofp_header *request,
6440 const struct ofp11_group_stats_request *gsr11 = ofpmsg_body(request);
6441 *group_id = ntohl(gsr11->group_id);
6445 /* Converts a group stats reply in 'msg' into an abstract ofputil_group_stats
6446 * in 'gs'. Assigns freshly allocated memory to gs->bucket_stats for the
6447 * caller to eventually free.
6449 * Multiple group stats replies can be packed into a single OpenFlow message.
6450 * Calling this function multiple times for a single 'msg' iterates through the
6451 * replies. The caller must initially leave 'msg''s layer pointers null and
6452 * not modify them between calls.
6454 * Returns 0 if successful, EOF if no replies were left in this 'msg',
6455 * otherwise a positive errno value. */
6457 ofputil_decode_group_stats_reply(struct ofpbuf *msg,
6458 struct ofputil_group_stats *gs)
6460 struct ofp11_bucket_counter *obc;
6461 struct ofp11_group_stats *ogs11;
6468 gs->bucket_stats = NULL;
6470 ? ofpraw_decode(&raw, msg->frame)
6471 : ofpraw_pull(&raw, msg));
6476 if (!ofpbuf_size(msg)) {
6480 if (raw == OFPRAW_OFPST11_GROUP_REPLY) {
6481 base_len = sizeof *ogs11;
6482 ogs11 = ofpbuf_try_pull(msg, sizeof *ogs11);
6483 gs->duration_sec = gs->duration_nsec = UINT32_MAX;
6484 } else if (raw == OFPRAW_OFPST13_GROUP_REPLY) {
6485 struct ofp13_group_stats *ogs13;
6487 base_len = sizeof *ogs13;
6488 ogs13 = ofpbuf_try_pull(msg, sizeof *ogs13);
6491 gs->duration_sec = ntohl(ogs13->duration_sec);
6492 gs->duration_nsec = ntohl(ogs13->duration_nsec);
6501 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
6502 ofpraw_get_name(raw), ofpbuf_size(msg));
6503 return OFPERR_OFPBRC_BAD_LEN;
6505 length = ntohs(ogs11->length);
6506 if (length < sizeof base_len) {
6507 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply claims invalid length %"PRIuSIZE,
6508 ofpraw_get_name(raw), length);
6509 return OFPERR_OFPBRC_BAD_LEN;
6512 gs->group_id = ntohl(ogs11->group_id);
6513 gs->ref_count = ntohl(ogs11->ref_count);
6514 gs->packet_count = ntohll(ogs11->packet_count);
6515 gs->byte_count = ntohll(ogs11->byte_count);
6517 gs->n_buckets = (length - base_len) / sizeof *obc;
6518 obc = ofpbuf_try_pull(msg, gs->n_buckets * sizeof *obc);
6520 VLOG_WARN_RL(&bad_ofmsg_rl, "%s reply has %"PRIu32" leftover bytes at end",
6521 ofpraw_get_name(raw), ofpbuf_size(msg));
6522 return OFPERR_OFPBRC_BAD_LEN;
6525 gs->bucket_stats = xmalloc(gs->n_buckets * sizeof *gs->bucket_stats);
6526 for (i = 0; i < gs->n_buckets; i++) {
6527 gs->bucket_stats[i].packet_count = ntohll(obc[i].packet_count);
6528 gs->bucket_stats[i].byte_count = ntohll(obc[i].byte_count);
6534 /* Appends a group stats reply that contains the data in 'gds' to those already
6535 * present in the list of ofpbufs in 'replies'. 'replies' should have been
6536 * initialized with ofpmp_init(). */
6538 ofputil_append_group_desc_reply(const struct ofputil_group_desc *gds,
6539 struct list *buckets,
6540 struct list *replies)
6542 struct ofpbuf *reply = ofpbuf_from_list(list_back(replies));
6543 enum ofp_version version = ofpmp_version(replies);
6544 struct ofp11_group_desc_stats *ogds;
6545 struct ofputil_bucket *bucket;
6548 start_ogds = ofpbuf_size(reply);
6549 ofpbuf_put_zeros(reply, sizeof *ogds);
6550 LIST_FOR_EACH (bucket, list_node, buckets) {
6551 struct ofp11_bucket *ob;
6554 start_ob = ofpbuf_size(reply);
6555 ofpbuf_put_zeros(reply, sizeof *ob);
6556 ofpacts_put_openflow_actions(bucket->ofpacts, bucket->ofpacts_len,
6558 ob = ofpbuf_at_assert(reply, start_ob, sizeof *ob);
6559 ob->len = htons(ofpbuf_size(reply) - start_ob);
6560 ob->weight = htons(bucket->weight);
6561 ob->watch_port = ofputil_port_to_ofp11(bucket->watch_port);
6562 ob->watch_group = htonl(bucket->watch_group);
6564 ogds = ofpbuf_at_assert(reply, start_ogds, sizeof *ogds);
6565 ogds->length = htons(ofpbuf_size(reply) - start_ogds);
6566 ogds->type = gds->type;
6567 ogds->group_id = htonl(gds->group_id);
6569 ofpmp_postappend(replies, start_ogds);
6573 ofputil_pull_buckets(struct ofpbuf *msg, size_t buckets_length,
6574 enum ofp_version version, struct list *buckets)
6576 struct ofp11_bucket *ob;
6579 while (buckets_length > 0) {
6580 struct ofputil_bucket *bucket;
6581 struct ofpbuf ofpacts;
6585 ob = (buckets_length >= sizeof *ob
6586 ? ofpbuf_try_pull(msg, sizeof *ob)
6589 VLOG_WARN_RL(&bad_ofmsg_rl, "buckets end with %"PRIuSIZE" leftover bytes",
6593 ob_len = ntohs(ob->len);
6594 if (ob_len < sizeof *ob) {
6595 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
6596 "%"PRIuSIZE" is not valid", ob_len);
6597 return OFPERR_OFPGMFC_BAD_BUCKET;
6598 } else if (ob_len > buckets_length) {
6599 VLOG_WARN_RL(&bad_ofmsg_rl, "OpenFlow message bucket length "
6600 "%"PRIuSIZE" exceeds remaining buckets data size %"PRIuSIZE,
6601 ob_len, buckets_length);
6602 return OFPERR_OFPGMFC_BAD_BUCKET;
6604 buckets_length -= ob_len;
6606 ofpbuf_init(&ofpacts, 0);
6607 error = ofpacts_pull_openflow_actions(msg, ob_len - sizeof *ob,
6610 ofpbuf_uninit(&ofpacts);
6611 ofputil_bucket_list_destroy(buckets);
6615 bucket = xzalloc(sizeof *bucket);
6616 bucket->weight = ntohs(ob->weight);
6617 error = ofputil_port_from_ofp11(ob->watch_port, &bucket->watch_port);
6619 ofpbuf_uninit(&ofpacts);
6620 ofputil_bucket_list_destroy(buckets);
6621 return OFPERR_OFPGMFC_BAD_WATCH;
6623 bucket->watch_group = ntohl(ob->watch_group);
6624 bucket->ofpacts = ofpbuf_steal_data(&ofpacts);
6625 bucket->ofpacts_len = ofpbuf_size(&ofpacts);
6626 list_push_back(buckets, &bucket->list_node);
6632 /* Converts a group description reply in 'msg' into an abstract
6633 * ofputil_group_desc in 'gd'.
6635 * Multiple group description replies can be packed into a single OpenFlow
6636 * message. Calling this function multiple times for a single 'msg' iterates
6637 * through the replies. The caller must initially leave 'msg''s layer pointers
6638 * null and not modify them between calls.
6640 * Returns 0 if successful, EOF if no replies were left in this 'msg',
6641 * otherwise a positive errno value. */
6643 ofputil_decode_group_desc_reply(struct ofputil_group_desc *gd,
6644 struct ofpbuf *msg, enum ofp_version version)
6646 struct ofp11_group_desc_stats *ogds;
6650 ofpraw_pull_assert(msg);
6653 if (!ofpbuf_size(msg)) {
6657 ogds = ofpbuf_try_pull(msg, sizeof *ogds);
6659 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply has %"PRIu32" "
6660 "leftover bytes at end", ofpbuf_size(msg));
6661 return OFPERR_OFPBRC_BAD_LEN;
6663 gd->type = ogds->type;
6664 gd->group_id = ntohl(ogds->group_id);
6666 length = ntohs(ogds->length);
6667 if (length < sizeof *ogds || length - sizeof *ogds > ofpbuf_size(msg)) {
6668 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST11_GROUP_DESC reply claims invalid "
6669 "length %"PRIuSIZE, length);
6670 return OFPERR_OFPBRC_BAD_LEN;
6673 return ofputil_pull_buckets(msg, length - sizeof *ogds, version,
6677 /* Converts abstract group mod 'gm' into a message for OpenFlow version
6678 * 'ofp_version' and returns the message. */
6680 ofputil_encode_group_mod(enum ofp_version ofp_version,
6681 const struct ofputil_group_mod *gm)
6684 struct ofp11_group_mod *ogm;
6686 size_t start_bucket;
6687 struct ofputil_bucket *bucket;
6688 struct ofp11_bucket *ob;
6690 switch (ofp_version) {
6691 case OFP10_VERSION: {
6692 if (gm->command == OFPGC11_ADD) {
6693 ovs_fatal(0, "add-group needs OpenFlow 1.1 or later "
6694 "(\'-O OpenFlow11\')");
6695 } else if (gm->command == OFPGC11_MODIFY) {
6696 ovs_fatal(0, "mod-group needs OpenFlow 1.1 or later "
6697 "(\'-O OpenFlow11\')");
6699 ovs_fatal(0, "del-groups needs OpenFlow 1.1 or later "
6700 "(\'-O OpenFlow11\')");
6708 b = ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD, ofp_version, 0);
6709 start_ogm = ofpbuf_size(b);
6710 ofpbuf_put_zeros(b, sizeof *ogm);
6712 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
6713 start_bucket = ofpbuf_size(b);
6714 ofpbuf_put_zeros(b, sizeof *ob);
6715 if (bucket->ofpacts && bucket->ofpacts_len) {
6716 ofpacts_put_openflow_actions(bucket->ofpacts,
6717 bucket->ofpacts_len, b,
6720 ob = ofpbuf_at_assert(b, start_bucket, sizeof *ob);
6721 ob->len = htons(ofpbuf_size(b) - start_bucket);;
6722 ob->weight = htons(bucket->weight);
6723 ob->watch_port = ofputil_port_to_ofp11(bucket->watch_port);
6724 ob->watch_group = htonl(bucket->watch_group);
6726 ogm = ofpbuf_at_assert(b, start_ogm, sizeof *ogm);
6727 ogm->command = htons(gm->command);
6728 ogm->type = gm->type;
6729 ogm->group_id = htonl(gm->group_id);
6740 /* Converts OpenFlow group mod message 'oh' into an abstract group mod in
6741 * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */
6743 ofputil_decode_group_mod(const struct ofp_header *oh,
6744 struct ofputil_group_mod *gm)
6746 const struct ofp11_group_mod *ogm;
6748 struct ofputil_bucket *bucket;
6751 ofpbuf_use_const(&msg, oh, ntohs(oh->length));
6752 ofpraw_pull_assert(&msg);
6754 ogm = ofpbuf_pull(&msg, sizeof *ogm);
6755 gm->command = ntohs(ogm->command);
6756 gm->type = ogm->type;
6757 gm->group_id = ntohl(ogm->group_id);
6759 err = ofputil_pull_buckets(&msg, ofpbuf_size(&msg), oh->version, &gm->buckets);
6764 LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
6767 case OFPGT11_INDIRECT:
6768 if (ofputil_bucket_has_liveness(bucket)) {
6769 return OFPERR_OFPGMFC_WATCH_UNSUPPORTED;
6772 case OFPGT11_SELECT:
6775 if (!ofputil_bucket_has_liveness(bucket)) {
6776 return OFPERR_OFPGMFC_INVALID_GROUP;
6787 /* Parse a queue status request message into 'oqsr'.
6788 * Returns 0 if successful, otherwise an OFPERR_* number. */
6790 ofputil_decode_queue_stats_request(const struct ofp_header *request,
6791 struct ofputil_queue_stats_request *oqsr)
6793 switch ((enum ofp_version)request->version) {
6797 case OFP11_VERSION: {
6798 const struct ofp11_queue_stats_request *qsr11 = ofpmsg_body(request);
6799 oqsr->queue_id = ntohl(qsr11->queue_id);
6800 return ofputil_port_from_ofp11(qsr11->port_no, &oqsr->port_no);
6803 case OFP10_VERSION: {
6804 const struct ofp10_queue_stats_request *qsr10 = ofpmsg_body(request);
6805 oqsr->queue_id = ntohl(qsr10->queue_id);
6806 oqsr->port_no = u16_to_ofp(ntohs(qsr10->port_no));
6807 /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
6808 if (oqsr->port_no == OFPP_ALL) {
6809 oqsr->port_no = OFPP_ANY;
6819 /* Encode a queue statsrequest for 'oqsr', the encoded message
6820 * will be fore Open Flow version 'ofp_version'. Returns message
6821 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
6823 ofputil_encode_queue_stats_request(enum ofp_version ofp_version,
6824 const struct ofputil_queue_stats_request *oqsr)
6826 struct ofpbuf *request;
6828 switch (ofp_version) {
6832 case OFP14_VERSION: {
6833 struct ofp11_queue_stats_request *req;
6834 request = ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST, ofp_version, 0);
6835 req = ofpbuf_put_zeros(request, sizeof *req);
6836 req->port_no = ofputil_port_to_ofp11(oqsr->port_no);
6837 req->queue_id = htonl(oqsr->queue_id);
6840 case OFP10_VERSION: {
6841 struct ofp10_queue_stats_request *req;
6842 request = ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST, ofp_version, 0);
6843 req = ofpbuf_put_zeros(request, sizeof *req);
6844 /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
6845 req->port_no = htons(ofp_to_u16(oqsr->port_no == OFPP_ANY
6846 ? OFPP_ALL : oqsr->port_no));
6847 req->queue_id = htonl(oqsr->queue_id);
6858 ofputil_get_queue_stats_size(enum ofp_version ofp_version)
6860 switch (ofp_version) {
6862 return sizeof(struct ofp10_queue_stats);
6865 return sizeof(struct ofp11_queue_stats);
6867 return sizeof(struct ofp13_queue_stats);
6876 /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
6879 ofputil_count_queue_stats(const struct ofp_header *oh)
6883 ofpbuf_use_const(&b, oh, ntohs(oh->length));
6884 ofpraw_pull_assert(&b);
6886 return ofpbuf_size(&b) / ofputil_get_queue_stats_size(oh->version);
6890 ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats *oqs,
6891 const struct ofp10_queue_stats *qs10)
6893 oqs->port_no = u16_to_ofp(ntohs(qs10->port_no));
6894 oqs->queue_id = ntohl(qs10->queue_id);
6895 oqs->tx_bytes = ntohll(get_32aligned_be64(&qs10->tx_bytes));
6896 oqs->tx_packets = ntohll(get_32aligned_be64(&qs10->tx_packets));
6897 oqs->tx_errors = ntohll(get_32aligned_be64(&qs10->tx_errors));
6898 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
6904 ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats *oqs,
6905 const struct ofp11_queue_stats *qs11)
6909 error = ofputil_port_from_ofp11(qs11->port_no, &oqs->port_no);
6914 oqs->queue_id = ntohl(qs11->queue_id);
6915 oqs->tx_bytes = ntohll(qs11->tx_bytes);
6916 oqs->tx_packets = ntohll(qs11->tx_packets);
6917 oqs->tx_errors = ntohll(qs11->tx_errors);
6918 oqs->duration_sec = oqs->duration_nsec = UINT32_MAX;
6924 ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats *oqs,
6925 const struct ofp13_queue_stats *qs13)
6927 enum ofperr error = ofputil_queue_stats_from_ofp11(oqs, &qs13->qs);
6929 oqs->duration_sec = ntohl(qs13->duration_sec);
6930 oqs->duration_nsec = ntohl(qs13->duration_nsec);
6936 /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
6937 * ofputil_queue_stats in 'qs'.
6939 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
6940 * message. Calling this function multiple times for a single 'msg' iterates
6941 * through the replies. The caller must initially leave 'msg''s layer pointers
6942 * null and not modify them between calls.
6944 * Returns 0 if successful, EOF if no replies were left in this 'msg',
6945 * otherwise a positive errno value. */
6947 ofputil_decode_queue_stats(struct ofputil_queue_stats *qs, struct ofpbuf *msg)
6953 ? ofpraw_decode(&raw, msg->frame)
6954 : ofpraw_pull(&raw, msg));
6959 if (!ofpbuf_size(msg)) {
6961 } else if (raw == OFPRAW_OFPST13_QUEUE_REPLY) {
6962 const struct ofp13_queue_stats *qs13;
6964 qs13 = ofpbuf_try_pull(msg, sizeof *qs13);
6968 return ofputil_queue_stats_from_ofp13(qs, qs13);
6969 } else if (raw == OFPRAW_OFPST11_QUEUE_REPLY) {
6970 const struct ofp11_queue_stats *qs11;
6972 qs11 = ofpbuf_try_pull(msg, sizeof *qs11);
6976 return ofputil_queue_stats_from_ofp11(qs, qs11);
6977 } else if (raw == OFPRAW_OFPST10_QUEUE_REPLY) {
6978 const struct ofp10_queue_stats *qs10;
6980 qs10 = ofpbuf_try_pull(msg, sizeof *qs10);
6984 return ofputil_queue_stats_from_ofp10(qs, qs10);
6990 VLOG_WARN_RL(&bad_ofmsg_rl, "OFPST_QUEUE reply has %"PRIu32" leftover "
6991 "bytes at end", ofpbuf_size(msg));
6992 return OFPERR_OFPBRC_BAD_LEN;
6996 ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats *oqs,
6997 struct ofp10_queue_stats *qs10)
6999 qs10->port_no = htons(ofp_to_u16(oqs->port_no));
7000 memset(qs10->pad, 0, sizeof qs10->pad);
7001 qs10->queue_id = htonl(oqs->queue_id);
7002 put_32aligned_be64(&qs10->tx_bytes, htonll(oqs->tx_bytes));
7003 put_32aligned_be64(&qs10->tx_packets, htonll(oqs->tx_packets));
7004 put_32aligned_be64(&qs10->tx_errors, htonll(oqs->tx_errors));
7008 ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats *oqs,
7009 struct ofp11_queue_stats *qs11)
7011 qs11->port_no = ofputil_port_to_ofp11(oqs->port_no);
7012 qs11->queue_id = htonl(oqs->queue_id);
7013 qs11->tx_bytes = htonll(oqs->tx_bytes);
7014 qs11->tx_packets = htonll(oqs->tx_packets);
7015 qs11->tx_errors = htonll(oqs->tx_errors);
7019 ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats *oqs,
7020 struct ofp13_queue_stats *qs13)
7022 ofputil_queue_stats_to_ofp11(oqs, &qs13->qs);
7023 if (oqs->duration_sec != UINT32_MAX) {
7024 qs13->duration_sec = htonl(oqs->duration_sec);
7025 qs13->duration_nsec = htonl(oqs->duration_nsec);
7027 qs13->duration_sec = OVS_BE32_MAX;
7028 qs13->duration_nsec = OVS_BE32_MAX;
7032 /* Encode a queue stat for 'oqs' and append it to 'replies'. */
7034 ofputil_append_queue_stat(struct list *replies,
7035 const struct ofputil_queue_stats *oqs)
7037 switch (ofpmp_version(replies)) {
7038 case OFP13_VERSION: {
7039 struct ofp13_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
7040 ofputil_queue_stats_to_ofp13(oqs, reply);
7045 case OFP11_VERSION: {
7046 struct ofp11_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
7047 ofputil_queue_stats_to_ofp11(oqs, reply);
7051 case OFP10_VERSION: {
7052 struct ofp10_queue_stats *reply = ofpmp_append(replies, sizeof *reply);
7053 ofputil_queue_stats_to_ofp10(oqs, reply);
7067 ofputil_decode_bundle_ctrl(const struct ofp_header *oh,
7068 struct ofputil_bundle_ctrl_msg *msg)
7072 const struct ofp14_bundle_ctrl_msg *m;
7074 ofpbuf_use_const(&b, oh, ntohs(oh->length));
7075 raw = ofpraw_pull_assert(&b);
7076 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_CONTROL);
7079 msg->bundle_id = ntohl(m->bundle_id);
7080 msg->type = ntohs(m->type);
7081 msg->flags = ntohs(m->flags);
7087 ofputil_encode_bundle_ctrl_reply(const struct ofp_header *oh,
7088 struct ofputil_bundle_ctrl_msg *msg)
7091 struct ofp14_bundle_ctrl_msg *m;
7093 buf = ofpraw_alloc_reply(OFPRAW_OFPT14_BUNDLE_CONTROL, oh, 0);
7094 m = ofpbuf_put_zeros(buf, sizeof *m);
7096 m->bundle_id = htonl(msg->bundle_id);
7097 m->type = htons(msg->type);
7098 m->flags = htons(msg->flags);
7104 ofputil_decode_bundle_add(const struct ofp_header *oh,
7105 struct ofputil_bundle_add_msg *msg)
7107 const struct ofp14_bundle_ctrl_msg *m;
7112 ofpbuf_use_const(&b, oh, ntohs(oh->length));
7113 raw = ofpraw_pull_assert(&b);
7114 ovs_assert(raw == OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE);
7116 m = ofpbuf_pull(&b, sizeof *m);
7117 msg->bundle_id = ntohl(m->bundle_id);
7118 msg->flags = ntohs(m->flags);
7120 msg->msg = ofpbuf_data(&b);
7121 inner_len = ntohs(msg->msg->length);
7122 if (inner_len < sizeof(struct ofp_header) || inner_len > ofpbuf_size(&b)) {
7123 return OFPERR_OFPBFC_MSG_BAD_LEN;
7130 ofputil_encode_bundle_add(enum ofp_version ofp_version,
7131 struct ofputil_bundle_add_msg *msg)
7133 struct ofpbuf *request;
7134 struct ofp14_bundle_ctrl_msg *m;
7136 request = ofpraw_alloc(OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE, ofp_version, 0);
7137 m = ofpbuf_put_zeros(request, sizeof *m);
7139 m->bundle_id = htonl(msg->bundle_id);
7140 m->flags = htons(msg->flags);
7141 ofpbuf_put(request, msg->msg, ntohs(msg->msg->length));