2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include <netinet/icmp6.h>
27 #include "byte-order.h"
28 #include "classifier.h"
30 #include "multipath.h"
34 #include "openflow/netronome-ext.h"
35 #include "openvswitch/dynamic-string.h"
36 #include "openvswitch/meta-flow.h"
37 #include "openvswitch/ofp-actions.h"
38 #include "openvswitch/ofp-errors.h"
39 #include "openvswitch/ofp-msgs.h"
40 #include "openvswitch/ofp-print.h"
41 #include "openvswitch/ofp-prop.h"
42 #include "openvswitch/ofp-util.h"
43 #include "openvswitch/ofpbuf.h"
44 #include "openvswitch/type-props.h"
45 #include "openvswitch/vlog.h"
46 #include "openflow/intel-ext.h"
49 #include "tun-metadata.h"
50 #include "unaligned.h"
54 VLOG_DEFINE_THIS_MODULE(ofp_util
);
56 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
57 * in the peer and so there's not much point in showing a lot of them. */
58 static struct vlog_rate_limit bad_ofmsg_rl
= VLOG_RATE_LIMIT_INIT(1, 5);
60 static enum ofputil_table_vacancy
ofputil_decode_table_vacancy(
61 ovs_be32 config
, enum ofp_version
);
62 static enum ofputil_table_eviction
ofputil_decode_table_eviction(
63 ovs_be32 config
, enum ofp_version
);
64 static ovs_be32
ofputil_encode_table_config(enum ofputil_table_miss
,
65 enum ofputil_table_eviction
,
66 enum ofputil_table_vacancy
,
69 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
70 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
73 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
74 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
75 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
76 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
79 ofputil_wcbits_to_netmask(int wcbits
)
82 return wcbits
< 32 ? htonl(~((1u << wcbits
) - 1)) : 0;
85 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
86 * that it wildcards, that is, the number of 0-bits in 'netmask', a number
87 * between 0 and 32 inclusive.
89 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
90 * still be in the valid range but isn't otherwise meaningful. */
92 ofputil_netmask_to_wcbits(ovs_be32 netmask
)
94 return 32 - ip_count_cidr_bits(netmask
);
97 /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
98 * flow_wildcards in 'wc' for use in struct match. It is the caller's
99 * responsibility to handle the special case where the flow match's dl_vlan is
100 * set to OFP_VLAN_NONE. */
102 ofputil_wildcard_from_ofpfw10(uint32_t ofpfw
, struct flow_wildcards
*wc
)
104 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 36);
106 /* Initialize most of wc. */
107 flow_wildcards_init_catchall(wc
);
109 if (!(ofpfw
& OFPFW10_IN_PORT
)) {
110 wc
->masks
.in_port
.ofp_port
= u16_to_ofp(UINT16_MAX
);
113 if (!(ofpfw
& OFPFW10_NW_TOS
)) {
114 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
117 if (!(ofpfw
& OFPFW10_NW_PROTO
)) {
118 wc
->masks
.nw_proto
= UINT8_MAX
;
120 wc
->masks
.nw_src
= ofputil_wcbits_to_netmask(ofpfw
121 >> OFPFW10_NW_SRC_SHIFT
);
122 wc
->masks
.nw_dst
= ofputil_wcbits_to_netmask(ofpfw
123 >> OFPFW10_NW_DST_SHIFT
);
125 if (!(ofpfw
& OFPFW10_TP_SRC
)) {
126 wc
->masks
.tp_src
= OVS_BE16_MAX
;
128 if (!(ofpfw
& OFPFW10_TP_DST
)) {
129 wc
->masks
.tp_dst
= OVS_BE16_MAX
;
132 if (!(ofpfw
& OFPFW10_DL_SRC
)) {
133 WC_MASK_FIELD(wc
, dl_src
);
135 if (!(ofpfw
& OFPFW10_DL_DST
)) {
136 WC_MASK_FIELD(wc
, dl_dst
);
138 if (!(ofpfw
& OFPFW10_DL_TYPE
)) {
139 wc
->masks
.dl_type
= OVS_BE16_MAX
;
143 if (!(ofpfw
& OFPFW10_DL_VLAN_PCP
)) {
144 wc
->masks
.vlan_tci
|= htons(VLAN_PCP_MASK
| VLAN_CFI
);
146 if (!(ofpfw
& OFPFW10_DL_VLAN
)) {
147 wc
->masks
.vlan_tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
151 /* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */
153 ofputil_match_from_ofp10_match(const struct ofp10_match
*ofmatch
,
156 uint32_t ofpfw
= ntohl(ofmatch
->wildcards
) & OFPFW10_ALL
;
158 /* Initialize match->wc. */
159 memset(&match
->flow
, 0, sizeof match
->flow
);
160 ofputil_wildcard_from_ofpfw10(ofpfw
, &match
->wc
);
162 /* Initialize most of match->flow. */
163 match
->flow
.nw_src
= ofmatch
->nw_src
;
164 match
->flow
.nw_dst
= ofmatch
->nw_dst
;
165 match
->flow
.in_port
.ofp_port
= u16_to_ofp(ntohs(ofmatch
->in_port
));
166 match
->flow
.dl_type
= ofputil_dl_type_from_openflow(ofmatch
->dl_type
);
167 match
->flow
.tp_src
= ofmatch
->tp_src
;
168 match
->flow
.tp_dst
= ofmatch
->tp_dst
;
169 match
->flow
.dl_src
= ofmatch
->dl_src
;
170 match
->flow
.dl_dst
= ofmatch
->dl_dst
;
171 match
->flow
.nw_tos
= ofmatch
->nw_tos
& IP_DSCP_MASK
;
172 match
->flow
.nw_proto
= ofmatch
->nw_proto
;
174 /* Translate VLANs. */
175 if (!(ofpfw
& OFPFW10_DL_VLAN
) &&
176 ofmatch
->dl_vlan
== htons(OFP10_VLAN_NONE
)) {
177 /* Match only packets without 802.1Q header.
179 * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
181 * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
182 * because we can't have a specific PCP without an 802.1Q header.
183 * However, older versions of OVS treated this as matching packets
184 * withut an 802.1Q header, so we do here too. */
185 match
->flow
.vlan_tci
= htons(0);
186 match
->wc
.masks
.vlan_tci
= htons(0xffff);
188 ovs_be16 vid
, pcp
, tci
;
191 vid
= ofmatch
->dl_vlan
& htons(VLAN_VID_MASK
);
192 hpcp
= (ofmatch
->dl_vlan_pcp
<< VLAN_PCP_SHIFT
) & VLAN_PCP_MASK
;
194 tci
= vid
| pcp
| htons(VLAN_CFI
);
195 match
->flow
.vlan_tci
= tci
& match
->wc
.masks
.vlan_tci
;
199 match_zero_wildcarded_fields(match
);
202 /* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */
204 ofputil_match_to_ofp10_match(const struct match
*match
,
205 struct ofp10_match
*ofmatch
)
207 const struct flow_wildcards
*wc
= &match
->wc
;
210 /* Figure out most OpenFlow wildcards. */
212 if (!wc
->masks
.in_port
.ofp_port
) {
213 ofpfw
|= OFPFW10_IN_PORT
;
215 if (!wc
->masks
.dl_type
) {
216 ofpfw
|= OFPFW10_DL_TYPE
;
218 if (!wc
->masks
.nw_proto
) {
219 ofpfw
|= OFPFW10_NW_PROTO
;
221 ofpfw
|= (ofputil_netmask_to_wcbits(wc
->masks
.nw_src
)
222 << OFPFW10_NW_SRC_SHIFT
);
223 ofpfw
|= (ofputil_netmask_to_wcbits(wc
->masks
.nw_dst
)
224 << OFPFW10_NW_DST_SHIFT
);
225 if (!(wc
->masks
.nw_tos
& IP_DSCP_MASK
)) {
226 ofpfw
|= OFPFW10_NW_TOS
;
228 if (!wc
->masks
.tp_src
) {
229 ofpfw
|= OFPFW10_TP_SRC
;
231 if (!wc
->masks
.tp_dst
) {
232 ofpfw
|= OFPFW10_TP_DST
;
234 if (eth_addr_is_zero(wc
->masks
.dl_src
)) {
235 ofpfw
|= OFPFW10_DL_SRC
;
237 if (eth_addr_is_zero(wc
->masks
.dl_dst
)) {
238 ofpfw
|= OFPFW10_DL_DST
;
241 /* Translate VLANs. */
242 ofmatch
->dl_vlan
= htons(0);
243 ofmatch
->dl_vlan_pcp
= 0;
244 if (match
->wc
.masks
.vlan_tci
== htons(0)) {
245 ofpfw
|= OFPFW10_DL_VLAN
| OFPFW10_DL_VLAN_PCP
;
246 } else if (match
->wc
.masks
.vlan_tci
& htons(VLAN_CFI
)
247 && !(match
->flow
.vlan_tci
& htons(VLAN_CFI
))) {
248 ofmatch
->dl_vlan
= htons(OFP10_VLAN_NONE
);
250 if (!(match
->wc
.masks
.vlan_tci
& htons(VLAN_VID_MASK
))) {
251 ofpfw
|= OFPFW10_DL_VLAN
;
253 ofmatch
->dl_vlan
= htons(vlan_tci_to_vid(match
->flow
.vlan_tci
));
256 if (!(match
->wc
.masks
.vlan_tci
& htons(VLAN_PCP_MASK
))) {
257 ofpfw
|= OFPFW10_DL_VLAN_PCP
;
259 ofmatch
->dl_vlan_pcp
= vlan_tci_to_pcp(match
->flow
.vlan_tci
);
263 /* Compose most of the match structure. */
264 ofmatch
->wildcards
= htonl(ofpfw
);
265 ofmatch
->in_port
= htons(ofp_to_u16(match
->flow
.in_port
.ofp_port
));
266 ofmatch
->dl_src
= match
->flow
.dl_src
;
267 ofmatch
->dl_dst
= match
->flow
.dl_dst
;
268 ofmatch
->dl_type
= ofputil_dl_type_to_openflow(match
->flow
.dl_type
);
269 ofmatch
->nw_src
= match
->flow
.nw_src
;
270 ofmatch
->nw_dst
= match
->flow
.nw_dst
;
271 ofmatch
->nw_tos
= match
->flow
.nw_tos
& IP_DSCP_MASK
;
272 ofmatch
->nw_proto
= match
->flow
.nw_proto
;
273 ofmatch
->tp_src
= match
->flow
.tp_src
;
274 ofmatch
->tp_dst
= match
->flow
.tp_dst
;
275 memset(ofmatch
->pad1
, '\0', sizeof ofmatch
->pad1
);
276 memset(ofmatch
->pad2
, '\0', sizeof ofmatch
->pad2
);
280 ofputil_pull_ofp11_match(struct ofpbuf
*buf
, struct match
*match
,
281 uint16_t *padded_match_len
)
283 struct ofp11_match_header
*omh
= buf
->data
;
286 if (buf
->size
< sizeof *omh
) {
287 return OFPERR_OFPBMC_BAD_LEN
;
290 match_len
= ntohs(omh
->length
);
292 switch (ntohs(omh
->type
)) {
293 case OFPMT_STANDARD
: {
294 struct ofp11_match
*om
;
296 if (match_len
!= sizeof *om
|| buf
->size
< sizeof *om
) {
297 return OFPERR_OFPBMC_BAD_LEN
;
299 om
= ofpbuf_pull(buf
, sizeof *om
);
300 if (padded_match_len
) {
301 *padded_match_len
= match_len
;
303 return ofputil_match_from_ofp11_match(om
, match
);
307 if (padded_match_len
) {
308 *padded_match_len
= ROUND_UP(match_len
, 8);
310 return oxm_pull_match(buf
, match
);
313 return OFPERR_OFPBMC_BAD_TYPE
;
317 /* Converts the ofp11_match in 'ofmatch' into a struct match in 'match'.
318 * Returns 0 if successful, otherwise an OFPERR_* value. */
320 ofputil_match_from_ofp11_match(const struct ofp11_match
*ofmatch
,
323 uint16_t wc
= ntohl(ofmatch
->wildcards
);
324 bool ipv4
, arp
, rarp
;
326 match_init_catchall(match
);
328 if (!(wc
& OFPFW11_IN_PORT
)) {
332 error
= ofputil_port_from_ofp11(ofmatch
->in_port
, &ofp_port
);
334 return OFPERR_OFPBMC_BAD_VALUE
;
336 match_set_in_port(match
, ofp_port
);
339 match_set_dl_src_masked(match
, ofmatch
->dl_src
,
340 eth_addr_invert(ofmatch
->dl_src_mask
));
341 match_set_dl_dst_masked(match
, ofmatch
->dl_dst
,
342 eth_addr_invert(ofmatch
->dl_dst_mask
));
344 if (!(wc
& OFPFW11_DL_VLAN
)) {
345 if (ofmatch
->dl_vlan
== htons(OFPVID11_NONE
)) {
346 /* Match only packets without a VLAN tag. */
347 match
->flow
.vlan_tci
= htons(0);
348 match
->wc
.masks
.vlan_tci
= OVS_BE16_MAX
;
350 if (ofmatch
->dl_vlan
== htons(OFPVID11_ANY
)) {
351 /* Match any packet with a VLAN tag regardless of VID. */
352 match
->flow
.vlan_tci
= htons(VLAN_CFI
);
353 match
->wc
.masks
.vlan_tci
= htons(VLAN_CFI
);
354 } else if (ntohs(ofmatch
->dl_vlan
) < 4096) {
355 /* Match only packets with the specified VLAN VID. */
356 match
->flow
.vlan_tci
= htons(VLAN_CFI
) | ofmatch
->dl_vlan
;
357 match
->wc
.masks
.vlan_tci
= htons(VLAN_CFI
| VLAN_VID_MASK
);
360 return OFPERR_OFPBMC_BAD_VALUE
;
363 if (!(wc
& OFPFW11_DL_VLAN_PCP
)) {
364 if (ofmatch
->dl_vlan_pcp
<= 7) {
365 match
->flow
.vlan_tci
|= htons(ofmatch
->dl_vlan_pcp
367 match
->wc
.masks
.vlan_tci
|= htons(VLAN_PCP_MASK
);
370 return OFPERR_OFPBMC_BAD_VALUE
;
376 if (!(wc
& OFPFW11_DL_TYPE
)) {
377 match_set_dl_type(match
,
378 ofputil_dl_type_from_openflow(ofmatch
->dl_type
));
381 ipv4
= match
->flow
.dl_type
== htons(ETH_TYPE_IP
);
382 arp
= match
->flow
.dl_type
== htons(ETH_TYPE_ARP
);
383 rarp
= match
->flow
.dl_type
== htons(ETH_TYPE_RARP
);
385 if (ipv4
&& !(wc
& OFPFW11_NW_TOS
)) {
386 if (ofmatch
->nw_tos
& ~IP_DSCP_MASK
) {
388 return OFPERR_OFPBMC_BAD_VALUE
;
391 match_set_nw_dscp(match
, ofmatch
->nw_tos
);
394 if (ipv4
|| arp
|| rarp
) {
395 if (!(wc
& OFPFW11_NW_PROTO
)) {
396 match_set_nw_proto(match
, ofmatch
->nw_proto
);
398 match_set_nw_src_masked(match
, ofmatch
->nw_src
, ~ofmatch
->nw_src_mask
);
399 match_set_nw_dst_masked(match
, ofmatch
->nw_dst
, ~ofmatch
->nw_dst_mask
);
402 #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
403 if (ipv4
&& (wc
& OFPFW11_TP_ALL
) != OFPFW11_TP_ALL
) {
404 switch (match
->flow
.nw_proto
) {
406 /* "A.2.3 Flow Match Structures" in OF1.1 says:
408 * The tp_src and tp_dst fields will be ignored unless the
409 * network protocol specified is as TCP, UDP or SCTP.
411 * but I'm pretty sure we should support ICMP too, otherwise
412 * that's a regression from OF1.0. */
413 if (!(wc
& OFPFW11_TP_SRC
)) {
414 uint16_t icmp_type
= ntohs(ofmatch
->tp_src
);
415 if (icmp_type
< 0x100) {
416 match_set_icmp_type(match
, icmp_type
);
418 return OFPERR_OFPBMC_BAD_FIELD
;
421 if (!(wc
& OFPFW11_TP_DST
)) {
422 uint16_t icmp_code
= ntohs(ofmatch
->tp_dst
);
423 if (icmp_code
< 0x100) {
424 match_set_icmp_code(match
, icmp_code
);
426 return OFPERR_OFPBMC_BAD_FIELD
;
434 if (!(wc
& (OFPFW11_TP_SRC
))) {
435 match_set_tp_src(match
, ofmatch
->tp_src
);
437 if (!(wc
& (OFPFW11_TP_DST
))) {
438 match_set_tp_dst(match
, ofmatch
->tp_dst
);
443 /* OF1.1 says explicitly to ignore this. */
448 if (eth_type_mpls(match
->flow
.dl_type
)) {
449 if (!(wc
& OFPFW11_MPLS_LABEL
)) {
450 match_set_mpls_label(match
, 0, ofmatch
->mpls_label
);
452 if (!(wc
& OFPFW11_MPLS_TC
)) {
453 match_set_mpls_tc(match
, 0, ofmatch
->mpls_tc
);
457 match_set_metadata_masked(match
, ofmatch
->metadata
,
458 ~ofmatch
->metadata_mask
);
463 /* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */
465 ofputil_match_to_ofp11_match(const struct match
*match
,
466 struct ofp11_match
*ofmatch
)
470 memset(ofmatch
, 0, sizeof *ofmatch
);
471 ofmatch
->omh
.type
= htons(OFPMT_STANDARD
);
472 ofmatch
->omh
.length
= htons(OFPMT11_STANDARD_LENGTH
);
474 if (!match
->wc
.masks
.in_port
.ofp_port
) {
475 wc
|= OFPFW11_IN_PORT
;
477 ofmatch
->in_port
= ofputil_port_to_ofp11(match
->flow
.in_port
.ofp_port
);
480 ofmatch
->dl_src
= match
->flow
.dl_src
;
481 ofmatch
->dl_src_mask
= eth_addr_invert(match
->wc
.masks
.dl_src
);
482 ofmatch
->dl_dst
= match
->flow
.dl_dst
;
483 ofmatch
->dl_dst_mask
= eth_addr_invert(match
->wc
.masks
.dl_dst
);
485 if (match
->wc
.masks
.vlan_tci
== htons(0)) {
486 wc
|= OFPFW11_DL_VLAN
| OFPFW11_DL_VLAN_PCP
;
487 } else if (match
->wc
.masks
.vlan_tci
& htons(VLAN_CFI
)
488 && !(match
->flow
.vlan_tci
& htons(VLAN_CFI
))) {
489 ofmatch
->dl_vlan
= htons(OFPVID11_NONE
);
490 wc
|= OFPFW11_DL_VLAN_PCP
;
492 if (!(match
->wc
.masks
.vlan_tci
& htons(VLAN_VID_MASK
))) {
493 ofmatch
->dl_vlan
= htons(OFPVID11_ANY
);
495 ofmatch
->dl_vlan
= htons(vlan_tci_to_vid(match
->flow
.vlan_tci
));
498 if (!(match
->wc
.masks
.vlan_tci
& htons(VLAN_PCP_MASK
))) {
499 wc
|= OFPFW11_DL_VLAN_PCP
;
501 ofmatch
->dl_vlan_pcp
= vlan_tci_to_pcp(match
->flow
.vlan_tci
);
505 if (!match
->wc
.masks
.dl_type
) {
506 wc
|= OFPFW11_DL_TYPE
;
508 ofmatch
->dl_type
= ofputil_dl_type_to_openflow(match
->flow
.dl_type
);
511 if (!(match
->wc
.masks
.nw_tos
& IP_DSCP_MASK
)) {
512 wc
|= OFPFW11_NW_TOS
;
514 ofmatch
->nw_tos
= match
->flow
.nw_tos
& IP_DSCP_MASK
;
517 if (!match
->wc
.masks
.nw_proto
) {
518 wc
|= OFPFW11_NW_PROTO
;
520 ofmatch
->nw_proto
= match
->flow
.nw_proto
;
523 ofmatch
->nw_src
= match
->flow
.nw_src
;
524 ofmatch
->nw_src_mask
= ~match
->wc
.masks
.nw_src
;
525 ofmatch
->nw_dst
= match
->flow
.nw_dst
;
526 ofmatch
->nw_dst_mask
= ~match
->wc
.masks
.nw_dst
;
528 if (!match
->wc
.masks
.tp_src
) {
529 wc
|= OFPFW11_TP_SRC
;
531 ofmatch
->tp_src
= match
->flow
.tp_src
;
534 if (!match
->wc
.masks
.tp_dst
) {
535 wc
|= OFPFW11_TP_DST
;
537 ofmatch
->tp_dst
= match
->flow
.tp_dst
;
540 if (!(match
->wc
.masks
.mpls_lse
[0] & htonl(MPLS_LABEL_MASK
))) {
541 wc
|= OFPFW11_MPLS_LABEL
;
543 ofmatch
->mpls_label
= htonl(mpls_lse_to_label(
544 match
->flow
.mpls_lse
[0]));
547 if (!(match
->wc
.masks
.mpls_lse
[0] & htonl(MPLS_TC_MASK
))) {
548 wc
|= OFPFW11_MPLS_TC
;
550 ofmatch
->mpls_tc
= mpls_lse_to_tc(match
->flow
.mpls_lse
[0]);
553 ofmatch
->metadata
= match
->flow
.metadata
;
554 ofmatch
->metadata_mask
= ~match
->wc
.masks
.metadata
;
556 ofmatch
->wildcards
= htonl(wc
);
559 /* Returns the "typical" length of a match for 'protocol', for use in
560 * estimating space to preallocate. */
562 ofputil_match_typical_len(enum ofputil_protocol protocol
)
565 case OFPUTIL_P_OF10_STD
:
566 case OFPUTIL_P_OF10_STD_TID
:
567 return sizeof(struct ofp10_match
);
569 case OFPUTIL_P_OF10_NXM
:
570 case OFPUTIL_P_OF10_NXM_TID
:
571 return NXM_TYPICAL_LEN
;
573 case OFPUTIL_P_OF11_STD
:
574 return sizeof(struct ofp11_match
);
576 case OFPUTIL_P_OF12_OXM
:
577 case OFPUTIL_P_OF13_OXM
:
578 case OFPUTIL_P_OF14_OXM
:
579 case OFPUTIL_P_OF15_OXM
:
580 case OFPUTIL_P_OF16_OXM
:
581 return NXM_TYPICAL_LEN
;
588 /* Appends to 'b' an struct ofp11_match_header followed by a match that
589 * expresses 'match' properly for 'protocol', plus enough zero bytes to pad the
590 * data appended out to a multiple of 8. 'protocol' must be one that is usable
591 * in OpenFlow 1.1 or later.
593 * This function can cause 'b''s data to be reallocated.
595 * Returns the number of bytes appended to 'b', excluding the padding. Never
598 ofputil_put_ofp11_match(struct ofpbuf
*b
, const struct match
*match
,
599 enum ofputil_protocol protocol
)
602 case OFPUTIL_P_OF10_STD
:
603 case OFPUTIL_P_OF10_STD_TID
:
604 case OFPUTIL_P_OF10_NXM
:
605 case OFPUTIL_P_OF10_NXM_TID
:
608 case OFPUTIL_P_OF11_STD
: {
609 struct ofp11_match
*om
;
611 /* Make sure that no padding is needed. */
612 BUILD_ASSERT_DECL(sizeof *om
% 8 == 0);
614 om
= ofpbuf_put_uninit(b
, sizeof *om
);
615 ofputil_match_to_ofp11_match(match
, om
);
619 case OFPUTIL_P_OF12_OXM
:
620 case OFPUTIL_P_OF13_OXM
:
621 case OFPUTIL_P_OF14_OXM
:
622 case OFPUTIL_P_OF15_OXM
:
623 case OFPUTIL_P_OF16_OXM
:
624 return oxm_put_match(b
, match
,
625 ofputil_protocol_to_ofp_version(protocol
));
631 /* Given a 'dl_type' value in the format used in struct flow, returns the
632 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
635 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type
)
637 return (flow_dl_type
== htons(FLOW_DL_TYPE_NONE
)
638 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE
)
642 /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
643 * structure, returns the corresponding 'dl_type' value for use in struct
646 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type
)
648 return (ofp_dl_type
== htons(OFP_DL_TYPE_NOT_ETH_TYPE
)
649 ? htons(FLOW_DL_TYPE_NONE
)
655 struct proto_abbrev
{
656 enum ofputil_protocol protocol
;
660 /* Most users really don't care about some of the differences between
661 * protocols. These abbreviations help with that. */
662 static const struct proto_abbrev proto_abbrevs
[] = {
663 { OFPUTIL_P_ANY
, "any" },
664 { OFPUTIL_P_OF10_STD_ANY
, "OpenFlow10" },
665 { OFPUTIL_P_OF10_NXM_ANY
, "NXM" },
666 { OFPUTIL_P_ANY_OXM
, "OXM" },
668 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
670 enum ofputil_protocol ofputil_flow_dump_protocols
[] = {
680 size_t ofputil_n_flow_dump_protocols
= ARRAY_SIZE(ofputil_flow_dump_protocols
);
682 /* Returns the set of ofputil_protocols that are supported with the given
683 * OpenFlow 'version'. 'version' should normally be an 8-bit OpenFlow version
684 * identifier (e.g. 0x01 for OpenFlow 1.0, 0x02 for OpenFlow 1.1). Returns 0
685 * if 'version' is not supported or outside the valid range. */
686 enum ofputil_protocol
687 ofputil_protocols_from_ofp_version(enum ofp_version version
)
691 return OFPUTIL_P_OF10_STD_ANY
| OFPUTIL_P_OF10_NXM_ANY
;
693 return OFPUTIL_P_OF11_STD
;
695 return OFPUTIL_P_OF12_OXM
;
697 return OFPUTIL_P_OF13_OXM
;
699 return OFPUTIL_P_OF14_OXM
;
701 return OFPUTIL_P_OF15_OXM
;
703 return OFPUTIL_P_OF16_OXM
;
709 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
710 * connection that has negotiated the given 'version'. 'version' should
711 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
712 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
713 * outside the valid range. */
714 enum ofputil_protocol
715 ofputil_protocol_from_ofp_version(enum ofp_version version
)
717 return rightmost_1bit(ofputil_protocols_from_ofp_version(version
));
720 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
721 * etc.) that corresponds to 'protocol'. */
723 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol
)
726 case OFPUTIL_P_OF10_STD
:
727 case OFPUTIL_P_OF10_STD_TID
:
728 case OFPUTIL_P_OF10_NXM
:
729 case OFPUTIL_P_OF10_NXM_TID
:
730 return OFP10_VERSION
;
731 case OFPUTIL_P_OF11_STD
:
732 return OFP11_VERSION
;
733 case OFPUTIL_P_OF12_OXM
:
734 return OFP12_VERSION
;
735 case OFPUTIL_P_OF13_OXM
:
736 return OFP13_VERSION
;
737 case OFPUTIL_P_OF14_OXM
:
738 return OFP14_VERSION
;
739 case OFPUTIL_P_OF15_OXM
:
740 return OFP15_VERSION
;
741 case OFPUTIL_P_OF16_OXM
:
742 return OFP16_VERSION
;
748 /* Returns a bitmap of OpenFlow versions that are supported by at
749 * least one of the 'protocols'. */
751 ofputil_protocols_to_version_bitmap(enum ofputil_protocol protocols
)
755 for (; protocols
; protocols
= zero_rightmost_1bit(protocols
)) {
756 enum ofputil_protocol protocol
= rightmost_1bit(protocols
);
758 bitmap
|= 1u << ofputil_protocol_to_ofp_version(protocol
);
764 /* Returns the set of protocols that are supported on top of the
765 * OpenFlow versions included in 'bitmap'. */
766 enum ofputil_protocol
767 ofputil_protocols_from_version_bitmap(uint32_t bitmap
)
769 enum ofputil_protocol protocols
= 0;
771 for (; bitmap
; bitmap
= zero_rightmost_1bit(bitmap
)) {
772 enum ofp_version version
= rightmost_1bit_idx(bitmap
);
774 protocols
|= ofputil_protocols_from_ofp_version(version
);
780 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
783 ofputil_protocol_is_valid(enum ofputil_protocol protocol
)
785 return protocol
& OFPUTIL_P_ANY
&& is_pow2(protocol
);
788 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
789 * extension turned on or off if 'enable' is true or false, respectively.
791 * This extension is only useful for protocols whose "standard" version does
792 * not allow specific tables to be modified. In particular, this is true of
793 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
794 * specifies a table ID and so there is no need for such an extension. When
795 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
796 * extension, this function just returns its 'protocol' argument unchanged
797 * regardless of the value of 'enable'. */
798 enum ofputil_protocol
799 ofputil_protocol_set_tid(enum ofputil_protocol protocol
, bool enable
)
802 case OFPUTIL_P_OF10_STD
:
803 case OFPUTIL_P_OF10_STD_TID
:
804 return enable
? OFPUTIL_P_OF10_STD_TID
: OFPUTIL_P_OF10_STD
;
806 case OFPUTIL_P_OF10_NXM
:
807 case OFPUTIL_P_OF10_NXM_TID
:
808 return enable
? OFPUTIL_P_OF10_NXM_TID
: OFPUTIL_P_OF10_NXM
;
810 case OFPUTIL_P_OF11_STD
:
811 return OFPUTIL_P_OF11_STD
;
813 case OFPUTIL_P_OF12_OXM
:
814 return OFPUTIL_P_OF12_OXM
;
816 case OFPUTIL_P_OF13_OXM
:
817 return OFPUTIL_P_OF13_OXM
;
819 case OFPUTIL_P_OF14_OXM
:
820 return OFPUTIL_P_OF14_OXM
;
822 case OFPUTIL_P_OF15_OXM
:
823 return OFPUTIL_P_OF15_OXM
;
825 case OFPUTIL_P_OF16_OXM
:
826 return OFPUTIL_P_OF16_OXM
;
833 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
834 * some extension to a standard protocol version, the return value is the
835 * standard version of that protocol without any extension. If 'protocol' is a
836 * standard protocol version, returns 'protocol' unchanged. */
837 enum ofputil_protocol
838 ofputil_protocol_to_base(enum ofputil_protocol protocol
)
840 return ofputil_protocol_set_tid(protocol
, false);
843 /* Returns 'new_base' with any extensions taken from 'cur'. */
844 enum ofputil_protocol
845 ofputil_protocol_set_base(enum ofputil_protocol cur
,
846 enum ofputil_protocol new_base
)
848 bool tid
= (cur
& OFPUTIL_P_TID
) != 0;
851 case OFPUTIL_P_OF10_STD
:
852 case OFPUTIL_P_OF10_STD_TID
:
853 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD
, tid
);
855 case OFPUTIL_P_OF10_NXM
:
856 case OFPUTIL_P_OF10_NXM_TID
:
857 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM
, tid
);
859 case OFPUTIL_P_OF11_STD
:
860 return ofputil_protocol_set_tid(OFPUTIL_P_OF11_STD
, tid
);
862 case OFPUTIL_P_OF12_OXM
:
863 return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM
, tid
);
865 case OFPUTIL_P_OF13_OXM
:
866 return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM
, tid
);
868 case OFPUTIL_P_OF14_OXM
:
869 return ofputil_protocol_set_tid(OFPUTIL_P_OF14_OXM
, tid
);
871 case OFPUTIL_P_OF15_OXM
:
872 return ofputil_protocol_set_tid(OFPUTIL_P_OF15_OXM
, tid
);
874 case OFPUTIL_P_OF16_OXM
:
875 return ofputil_protocol_set_tid(OFPUTIL_P_OF16_OXM
, tid
);
882 /* Returns a string form of 'protocol', if a simple form exists (that is, if
883 * 'protocol' is either a single protocol or it is a combination of protocols
884 * that have a single abbreviation). Otherwise, returns NULL. */
886 ofputil_protocol_to_string(enum ofputil_protocol protocol
)
888 const struct proto_abbrev
*p
;
890 /* Use a "switch" statement for single-bit names so that we get a compiler
891 * warning if we forget any. */
893 case OFPUTIL_P_OF10_NXM
:
894 return "NXM-table_id";
896 case OFPUTIL_P_OF10_NXM_TID
:
897 return "NXM+table_id";
899 case OFPUTIL_P_OF10_STD
:
900 return "OpenFlow10-table_id";
902 case OFPUTIL_P_OF10_STD_TID
:
903 return "OpenFlow10+table_id";
905 case OFPUTIL_P_OF11_STD
:
908 case OFPUTIL_P_OF12_OXM
:
909 return "OXM-OpenFlow12";
911 case OFPUTIL_P_OF13_OXM
:
912 return "OXM-OpenFlow13";
914 case OFPUTIL_P_OF14_OXM
:
915 return "OXM-OpenFlow14";
917 case OFPUTIL_P_OF15_OXM
:
918 return "OXM-OpenFlow15";
920 case OFPUTIL_P_OF16_OXM
:
921 return "OXM-OpenFlow16";
924 /* Check abbreviations. */
925 for (p
= proto_abbrevs
; p
< &proto_abbrevs
[N_PROTO_ABBREVS
]; p
++) {
926 if (protocol
== p
->protocol
) {
934 /* Returns a string that represents 'protocols'. The return value might be a
935 * comma-separated list if 'protocols' doesn't have a simple name. The return
936 * value is "none" if 'protocols' is 0.
938 * The caller must free the returned string (with free()). */
940 ofputil_protocols_to_string(enum ofputil_protocol protocols
)
944 ovs_assert(!(protocols
& ~OFPUTIL_P_ANY
));
945 if (protocols
== 0) {
946 return xstrdup("none");
951 const struct proto_abbrev
*p
;
955 ds_put_char(&s
, ',');
958 for (p
= proto_abbrevs
; p
< &proto_abbrevs
[N_PROTO_ABBREVS
]; p
++) {
959 if ((protocols
& p
->protocol
) == p
->protocol
) {
960 ds_put_cstr(&s
, p
->name
);
961 protocols
&= ~p
->protocol
;
966 for (i
= 0; i
< CHAR_BIT
* sizeof(enum ofputil_protocol
); i
++) {
967 enum ofputil_protocol bit
= 1u << i
;
969 if (protocols
& bit
) {
970 ds_put_cstr(&s
, ofputil_protocol_to_string(bit
));
979 return ds_steal_cstr(&s
);
982 static enum ofputil_protocol
983 ofputil_protocol_from_string__(const char *s
, size_t n
)
985 const struct proto_abbrev
*p
;
988 for (i
= 0; i
< CHAR_BIT
* sizeof(enum ofputil_protocol
); i
++) {
989 enum ofputil_protocol bit
= 1u << i
;
990 const char *name
= ofputil_protocol_to_string(bit
);
992 if (name
&& n
== strlen(name
) && !strncasecmp(s
, name
, n
)) {
997 for (p
= proto_abbrevs
; p
< &proto_abbrevs
[N_PROTO_ABBREVS
]; p
++) {
998 if (n
== strlen(p
->name
) && !strncasecmp(s
, p
->name
, n
)) {
1006 /* Returns the nonempty set of protocols represented by 's', which can be a
1007 * single protocol name or abbreviation or a comma-separated list of them.
1009 * Aborts the program with an error message if 's' is invalid. */
1010 enum ofputil_protocol
1011 ofputil_protocols_from_string(const char *s
)
1013 const char *orig_s
= s
;
1014 enum ofputil_protocol protocols
;
1018 enum ofputil_protocol p
;
1021 n
= strcspn(s
, ",");
1027 p
= ofputil_protocol_from_string__(s
, n
);
1029 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n
, s
);
1037 ovs_fatal(0, "%s: no flow protocol specified", orig_s
);
1043 ofputil_version_from_string(const char *s
)
1045 if (!strcasecmp(s
, "OpenFlow10")) {
1046 return OFP10_VERSION
;
1048 if (!strcasecmp(s
, "OpenFlow11")) {
1049 return OFP11_VERSION
;
1051 if (!strcasecmp(s
, "OpenFlow12")) {
1052 return OFP12_VERSION
;
1054 if (!strcasecmp(s
, "OpenFlow13")) {
1055 return OFP13_VERSION
;
1057 if (!strcasecmp(s
, "OpenFlow14")) {
1058 return OFP14_VERSION
;
1060 if (!strcasecmp(s
, "OpenFlow15")) {
1061 return OFP15_VERSION
;
1063 if (!strcasecmp(s
, "OpenFlow16")) {
1064 return OFP16_VERSION
;
1070 is_delimiter(unsigned char c
)
1072 return isspace(c
) || c
== ',';
1076 ofputil_versions_from_string(const char *s
)
1079 uint32_t bitmap
= 0;
1086 if (is_delimiter(s
[i
])) {
1091 while (s
[i
+ j
] && !is_delimiter(s
[i
+ j
])) {
1094 key
= xmemdup0(s
+ i
, j
);
1095 version
= ofputil_version_from_string(key
);
1097 VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key
);
1100 bitmap
|= 1u << version
;
1108 ofputil_versions_from_strings(char ** const s
, size_t count
)
1110 uint32_t bitmap
= 0;
1113 int version
= ofputil_version_from_string(s
[count
]);
1115 VLOG_WARN("Unknown OpenFlow version: \"%s\"", s
[count
]);
1117 bitmap
|= 1u << version
;
1125 ofputil_version_to_string(enum ofp_version ofp_version
)
1127 switch (ofp_version
) {
1129 return "OpenFlow10";
1131 return "OpenFlow11";
1133 return "OpenFlow12";
1135 return "OpenFlow13";
1137 return "OpenFlow14";
1139 return "OpenFlow15";
1141 return "OpenFlow16";
1148 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format
)
1150 switch (packet_in_format
) {
1151 case NXPIF_STANDARD
:
1152 case NXPIF_NXT_PACKET_IN
:
1153 case NXPIF_NXT_PACKET_IN2
:
1161 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format
)
1163 switch (packet_in_format
) {
1164 case NXPIF_STANDARD
:
1166 case NXPIF_NXT_PACKET_IN
:
1167 return "nxt_packet_in";
1168 case NXPIF_NXT_PACKET_IN2
:
1169 return "nxt_packet_in2";
1176 ofputil_packet_in_format_from_string(const char *s
)
1178 return (!strcmp(s
, "standard") || !strcmp(s
, "openflow10")
1180 : !strcmp(s
, "nxt_packet_in") || !strcmp(s
, "nxm")
1181 ? NXPIF_NXT_PACKET_IN
1182 : !strcmp(s
, "nxt_packet_in2")
1183 ? NXPIF_NXT_PACKET_IN2
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 vSwitch 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
)
1277 struct ofpbuf msg
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
1278 ofpbuf_pull(&msg
, sizeof *oh
);
1280 *allowed_versions
= version_bitmap_from_version(oh
->version
);
1284 const struct ofp_hello_elem_header
*oheh
;
1287 if (msg
.size
< sizeof *oheh
) {
1292 len
= ntohs(oheh
->length
);
1293 if (len
< sizeof *oheh
|| !ofpbuf_try_pull(&msg
, ROUND_UP(len
, 8))) {
1297 if (oheh
->type
!= htons(OFPHET_VERSIONBITMAP
)
1298 || !ofputil_decode_hello_bitmap(oheh
, allowed_versions
)) {
1306 /* Returns true if 'allowed_versions' needs to be accompanied by a version
1307 * bitmap to be correctly expressed in an OFPT_HELLO message. */
1309 should_send_version_bitmap(uint32_t allowed_versions
)
1311 return !is_pow2((allowed_versions
>> 1) + 1);
1314 /* Create an OFPT_HELLO message that expresses support for the OpenFlow
1315 * versions in the 'allowed_versions' bitmaps and returns the message. */
1317 ofputil_encode_hello(uint32_t allowed_versions
)
1319 enum ofp_version ofp_version
;
1322 ofp_version
= leftmost_1bit_idx(allowed_versions
);
1323 msg
= ofpraw_alloc(OFPRAW_OFPT_HELLO
, ofp_version
, 0);
1325 if (should_send_version_bitmap(allowed_versions
)) {
1326 struct ofp_hello_elem_header
*oheh
;
1329 map_len
= sizeof allowed_versions
;
1330 oheh
= ofpbuf_put_zeros(msg
, ROUND_UP(map_len
+ sizeof *oheh
, 8));
1331 oheh
->type
= htons(OFPHET_VERSIONBITMAP
);
1332 oheh
->length
= htons(map_len
+ sizeof *oheh
);
1333 *ALIGNED_CAST(ovs_be32
*, oheh
+ 1) = htonl(allowed_versions
);
1335 ofpmsg_update_length(msg
);
1341 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1342 * protocol is 'current', at least partly transitions the protocol to 'want'.
1343 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1344 * connection if the switch processes the returned message correctly. (If
1345 * '*next != want' then the caller will have to iterate.)
1347 * If 'current == want', or if it is not possible to transition from 'current'
1348 * to 'want' (because, for example, 'current' and 'want' use different OpenFlow
1349 * protocol versions), returns NULL and stores 'current' in '*next'. */
1351 ofputil_encode_set_protocol(enum ofputil_protocol current
,
1352 enum ofputil_protocol want
,
1353 enum ofputil_protocol
*next
)
1355 enum ofp_version cur_version
, want_version
;
1356 enum ofputil_protocol cur_base
, want_base
;
1357 bool cur_tid
, want_tid
;
1359 cur_version
= ofputil_protocol_to_ofp_version(current
);
1360 want_version
= ofputil_protocol_to_ofp_version(want
);
1361 if (cur_version
!= want_version
) {
1366 cur_base
= ofputil_protocol_to_base(current
);
1367 want_base
= ofputil_protocol_to_base(want
);
1368 if (cur_base
!= want_base
) {
1369 *next
= ofputil_protocol_set_base(current
, want_base
);
1371 switch (want_base
) {
1372 case OFPUTIL_P_OF10_NXM
:
1373 return ofputil_encode_nx_set_flow_format(NXFF_NXM
);
1375 case OFPUTIL_P_OF10_STD
:
1376 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10
);
1378 case OFPUTIL_P_OF11_STD
:
1379 case OFPUTIL_P_OF12_OXM
:
1380 case OFPUTIL_P_OF13_OXM
:
1381 case OFPUTIL_P_OF14_OXM
:
1382 case OFPUTIL_P_OF15_OXM
:
1383 case OFPUTIL_P_OF16_OXM
:
1384 /* There is only one variant of each OpenFlow 1.1+ protocol, and we
1385 * verified above that we're not trying to change versions. */
1388 case OFPUTIL_P_OF10_STD_TID
:
1389 case OFPUTIL_P_OF10_NXM_TID
:
1394 cur_tid
= (current
& OFPUTIL_P_TID
) != 0;
1395 want_tid
= (want
& OFPUTIL_P_TID
) != 0;
1396 if (cur_tid
!= want_tid
) {
1397 *next
= ofputil_protocol_set_tid(current
, want_tid
);
1398 return ofputil_make_flow_mod_table_id(want_tid
);
1401 ovs_assert(current
== want
);
1407 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1408 * format to 'nxff'. */
1410 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff
)
1412 struct nx_set_flow_format
*sff
;
1415 ovs_assert(ofputil_nx_flow_format_is_valid(nxff
));
1417 msg
= ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT
, OFP10_VERSION
, 0);
1418 sff
= ofpbuf_put_zeros(msg
, sizeof *sff
);
1419 sff
->format
= htonl(nxff
);
1424 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1426 enum ofputil_protocol
1427 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format
)
1429 switch (flow_format
) {
1430 case NXFF_OPENFLOW10
:
1431 return OFPUTIL_P_OF10_STD
;
1434 return OFPUTIL_P_OF10_NXM
;
1441 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1443 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format
)
1445 return ofputil_nx_flow_format_to_protocol(flow_format
) != 0;
1448 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1451 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format
)
1453 switch (flow_format
) {
1454 case NXFF_OPENFLOW10
:
1455 return "openflow10";
1464 ofputil_make_set_packet_in_format(enum ofp_version ofp_version
,
1465 enum nx_packet_in_format packet_in_format
)
1467 struct nx_set_packet_in_format
*spif
;
1470 msg
= ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT
, ofp_version
, 0);
1471 spif
= ofpbuf_put_zeros(msg
, sizeof *spif
);
1472 spif
->format
= htonl(packet_in_format
);
1477 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1478 * extension on or off (according to 'flow_mod_table_id'). */
1480 ofputil_make_flow_mod_table_id(bool flow_mod_table_id
)
1482 struct nx_flow_mod_table_id
*nfmti
;
1485 msg
= ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID
, OFP10_VERSION
, 0);
1486 nfmti
= ofpbuf_put_zeros(msg
, sizeof *nfmti
);
1487 nfmti
->set
= flow_mod_table_id
;
1491 struct ofputil_flow_mod_flag
{
1493 enum ofp_version min_version
, max_version
;
1494 enum ofputil_flow_mod_flags flag
;
1497 static const struct ofputil_flow_mod_flag ofputil_flow_mod_flags
[] = {
1498 { OFPFF_SEND_FLOW_REM
, OFP10_VERSION
, 0, OFPUTIL_FF_SEND_FLOW_REM
},
1499 { OFPFF_CHECK_OVERLAP
, OFP10_VERSION
, 0, OFPUTIL_FF_CHECK_OVERLAP
},
1500 { OFPFF10_EMERG
, OFP10_VERSION
, OFP10_VERSION
,
1502 { OFPFF12_RESET_COUNTS
, OFP12_VERSION
, 0, OFPUTIL_FF_RESET_COUNTS
},
1503 { OFPFF13_NO_PKT_COUNTS
, OFP13_VERSION
, 0, OFPUTIL_FF_NO_PKT_COUNTS
},
1504 { OFPFF13_NO_BYT_COUNTS
, OFP13_VERSION
, 0, OFPUTIL_FF_NO_BYT_COUNTS
},
1509 ofputil_decode_flow_mod_flags(ovs_be16 raw_flags_
,
1510 enum ofp_flow_mod_command command
,
1511 enum ofp_version version
,
1512 enum ofputil_flow_mod_flags
*flagsp
)
1514 uint16_t raw_flags
= ntohs(raw_flags_
);
1515 const struct ofputil_flow_mod_flag
*f
;
1518 for (f
= ofputil_flow_mod_flags
; f
->raw_flag
; f
++) {
1519 if (raw_flags
& f
->raw_flag
1520 && version
>= f
->min_version
1521 && (!f
->max_version
|| version
<= f
->max_version
)) {
1522 raw_flags
&= ~f
->raw_flag
;
1527 /* In OF1.0 and OF1.1, "add" always resets counters, and other commands
1530 * In OF1.2 and later, OFPFF12_RESET_COUNTS controls whether each command
1531 * resets counters. */
1532 if ((version
== OFP10_VERSION
|| version
== OFP11_VERSION
)
1533 && command
== OFPFC_ADD
) {
1534 *flagsp
|= OFPUTIL_FF_RESET_COUNTS
;
1537 return raw_flags
? OFPERR_OFPFMFC_BAD_FLAGS
: 0;
1541 ofputil_encode_flow_mod_flags(enum ofputil_flow_mod_flags flags
,
1542 enum ofp_version version
)
1544 const struct ofputil_flow_mod_flag
*f
;
1548 for (f
= ofputil_flow_mod_flags
; f
->raw_flag
; f
++) {
1550 && version
>= f
->min_version
1551 && (!f
->max_version
|| version
<= f
->max_version
)) {
1552 raw_flags
|= f
->raw_flag
;
1556 return htons(raw_flags
);
1559 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1560 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1563 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1564 * The caller must initialize 'ofpacts' and retains ownership of it.
1565 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1567 * Does not validate the flow_mod actions. The caller should do that, with
1568 * ofpacts_check(). */
1570 ofputil_decode_flow_mod(struct ofputil_flow_mod
*fm
,
1571 const struct ofp_header
*oh
,
1572 enum ofputil_protocol protocol
,
1573 struct ofpbuf
*ofpacts
,
1574 ofp_port_t max_port
, uint8_t max_table
)
1578 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
1579 enum ofpraw raw
= ofpraw_pull_assert(&b
);
1580 if (raw
== OFPRAW_OFPT11_FLOW_MOD
) {
1581 /* Standard OpenFlow 1.1+ flow_mod. */
1582 const struct ofp11_flow_mod
*ofm
;
1584 ofm
= ofpbuf_pull(&b
, sizeof *ofm
);
1586 error
= ofputil_pull_ofp11_match(&b
, &fm
->match
, NULL
);
1591 /* Translate the message. */
1592 fm
->priority
= ntohs(ofm
->priority
);
1593 if (ofm
->command
== OFPFC_ADD
1594 || (oh
->version
== OFP11_VERSION
1595 && (ofm
->command
== OFPFC_MODIFY
||
1596 ofm
->command
== OFPFC_MODIFY_STRICT
)
1597 && ofm
->cookie_mask
== htonll(0))) {
1598 /* In OpenFlow 1.1 only, a "modify" or "modify-strict" that does
1599 * not match on the cookie is treated as an "add" if there is no
1601 fm
->cookie
= htonll(0);
1602 fm
->cookie_mask
= htonll(0);
1603 fm
->new_cookie
= ofm
->cookie
;
1605 fm
->cookie
= ofm
->cookie
;
1606 fm
->cookie_mask
= ofm
->cookie_mask
;
1607 fm
->new_cookie
= OVS_BE64_MAX
;
1609 fm
->modify_cookie
= false;
1610 fm
->command
= ofm
->command
;
1614 * OF1.1 entirely forbids table_id == OFPTT_ALL.
1615 * OF1.2+ allows table_id == OFPTT_ALL only for deletes. */
1616 fm
->table_id
= ofm
->table_id
;
1617 if (fm
->table_id
== OFPTT_ALL
1618 && (oh
->version
== OFP11_VERSION
1619 || (ofm
->command
!= OFPFC_DELETE
&&
1620 ofm
->command
!= OFPFC_DELETE_STRICT
))) {
1621 return OFPERR_OFPFMFC_BAD_TABLE_ID
;
1624 fm
->idle_timeout
= ntohs(ofm
->idle_timeout
);
1625 fm
->hard_timeout
= ntohs(ofm
->hard_timeout
);
1626 if (oh
->version
>= OFP14_VERSION
&& ofm
->command
== OFPFC_ADD
) {
1627 fm
->importance
= ntohs(ofm
->importance
);
1631 fm
->buffer_id
= ntohl(ofm
->buffer_id
);
1632 error
= ofputil_port_from_ofp11(ofm
->out_port
, &fm
->out_port
);
1637 fm
->out_group
= (ofm
->command
== OFPFC_DELETE
||
1638 ofm
->command
== OFPFC_DELETE_STRICT
1639 ? ntohl(ofm
->out_group
)
1641 raw_flags
= ofm
->flags
;
1645 if (raw
== OFPRAW_OFPT10_FLOW_MOD
) {
1646 /* Standard OpenFlow 1.0 flow_mod. */
1647 const struct ofp10_flow_mod
*ofm
;
1649 /* Get the ofp10_flow_mod. */
1650 ofm
= ofpbuf_pull(&b
, sizeof *ofm
);
1652 /* Translate the rule. */
1653 ofputil_match_from_ofp10_match(&ofm
->match
, &fm
->match
);
1654 ofputil_normalize_match(&fm
->match
);
1656 /* OpenFlow 1.0 says that exact-match rules have to have the
1657 * highest possible priority. */
1658 fm
->priority
= (ofm
->match
.wildcards
& htonl(OFPFW10_ALL
)
1659 ? ntohs(ofm
->priority
)
1662 /* Translate the message. */
1663 command
= ntohs(ofm
->command
);
1664 fm
->cookie
= htonll(0);
1665 fm
->cookie_mask
= htonll(0);
1666 fm
->new_cookie
= ofm
->cookie
;
1667 fm
->idle_timeout
= ntohs(ofm
->idle_timeout
);
1668 fm
->hard_timeout
= ntohs(ofm
->hard_timeout
);
1670 fm
->buffer_id
= ntohl(ofm
->buffer_id
);
1671 fm
->out_port
= u16_to_ofp(ntohs(ofm
->out_port
));
1672 fm
->out_group
= OFPG_ANY
;
1673 raw_flags
= ofm
->flags
;
1674 } else if (raw
== OFPRAW_NXT_FLOW_MOD
) {
1675 /* Nicira extended flow_mod. */
1676 const struct nx_flow_mod
*nfm
;
1678 /* Dissect the message. */
1679 nfm
= ofpbuf_pull(&b
, sizeof *nfm
);
1680 error
= nx_pull_match(&b
, ntohs(nfm
->match_len
),
1681 &fm
->match
, &fm
->cookie
, &fm
->cookie_mask
);
1686 /* Translate the message. */
1687 command
= ntohs(nfm
->command
);
1688 if ((command
& 0xff) == OFPFC_ADD
&& fm
->cookie_mask
) {
1689 /* Flow additions may only set a new cookie, not match an
1690 * existing cookie. */
1691 return OFPERR_NXBRC_NXM_INVALID
;
1693 fm
->priority
= ntohs(nfm
->priority
);
1694 fm
->new_cookie
= nfm
->cookie
;
1695 fm
->idle_timeout
= ntohs(nfm
->idle_timeout
);
1696 fm
->hard_timeout
= ntohs(nfm
->hard_timeout
);
1698 fm
->buffer_id
= ntohl(nfm
->buffer_id
);
1699 fm
->out_port
= u16_to_ofp(ntohs(nfm
->out_port
));
1700 fm
->out_group
= OFPG_ANY
;
1701 raw_flags
= nfm
->flags
;
1706 fm
->modify_cookie
= fm
->new_cookie
!= OVS_BE64_MAX
;
1707 if (protocol
& OFPUTIL_P_TID
) {
1708 fm
->command
= command
& 0xff;
1709 fm
->table_id
= command
>> 8;
1711 if (command
> 0xff) {
1712 VLOG_WARN_RL(&bad_ofmsg_rl
, "flow_mod has explicit table_id "
1713 "but flow_mod_table_id extension is not enabled");
1715 fm
->command
= command
;
1716 fm
->table_id
= 0xff;
1720 if (fm
->command
> OFPFC_DELETE_STRICT
) {
1721 return OFPERR_OFPFMFC_BAD_COMMAND
;
1724 error
= ofpacts_pull_openflow_instructions(&b
, b
.size
,
1725 oh
->version
, ofpacts
);
1729 fm
->ofpacts
= ofpacts
->data
;
1730 fm
->ofpacts_len
= ofpacts
->size
;
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 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
1801 ofpraw_pull_assert(&b
);
1802 const struct ofp13_meter_mod
*omm
= ofpbuf_pull(&b
, sizeof *omm
);
1804 /* Translate the message. */
1805 mm
->command
= ntohs(omm
->command
);
1806 if (mm
->command
!= OFPMC13_ADD
&&
1807 mm
->command
!= OFPMC13_MODIFY
&&
1808 mm
->command
!= OFPMC13_DELETE
) {
1809 return OFPERR_OFPMMFC_BAD_COMMAND
;
1811 mm
->meter
.meter_id
= ntohl(omm
->meter_id
);
1813 if (mm
->command
== OFPMC13_DELETE
) {
1814 mm
->meter
.flags
= 0;
1815 mm
->meter
.n_bands
= 0;
1816 mm
->meter
.bands
= NULL
;
1820 mm
->meter
.flags
= ntohs(omm
->flags
);
1821 if (mm
->meter
.flags
& OFPMF13_KBPS
&&
1822 mm
->meter
.flags
& OFPMF13_PKTPS
) {
1823 return OFPERR_OFPMMFC_BAD_FLAGS
;
1825 mm
->meter
.bands
= bands
->data
;
1827 error
= ofputil_pull_bands(&b
, b
.size
, &mm
->meter
.n_bands
, bands
);
1836 ofputil_decode_meter_request(const struct ofp_header
*oh
, uint32_t *meter_id
)
1838 const struct ofp13_meter_multipart_request
*omr
= ofpmsg_body(oh
);
1839 *meter_id
= ntohl(omr
->meter_id
);
1843 ofputil_encode_meter_request(enum ofp_version ofp_version
,
1844 enum ofputil_meter_request_type type
,
1852 case OFPUTIL_METER_CONFIG
:
1853 raw
= OFPRAW_OFPST13_METER_CONFIG_REQUEST
;
1855 case OFPUTIL_METER_STATS
:
1856 raw
= OFPRAW_OFPST13_METER_REQUEST
;
1859 case OFPUTIL_METER_FEATURES
:
1860 raw
= OFPRAW_OFPST13_METER_FEATURES_REQUEST
;
1864 msg
= ofpraw_alloc(raw
, ofp_version
, 0);
1866 if (type
!= OFPUTIL_METER_FEATURES
) {
1867 struct ofp13_meter_multipart_request
*omr
;
1868 omr
= ofpbuf_put_zeros(msg
, sizeof *omr
);
1869 omr
->meter_id
= htonl(meter_id
);
1875 ofputil_put_bands(uint16_t n_bands
, const struct ofputil_meter_band
*mb
,
1880 for (n
= 0; n
< n_bands
; ++n
) {
1881 /* Currently all band types have same size. */
1882 struct ofp13_meter_band_dscp_remark
*ombh
;
1883 size_t ombh_len
= sizeof *ombh
;
1885 ombh
= ofpbuf_put_zeros(msg
, ombh_len
);
1887 ombh
->type
= htons(mb
->type
);
1888 ombh
->len
= htons(ombh_len
);
1889 ombh
->rate
= htonl(mb
->rate
);
1890 ombh
->burst_size
= htonl(mb
->burst_size
);
1891 ombh
->prec_level
= mb
->prec_level
;
1897 /* Encode a meter stat for 'mc' and append it to 'replies'. */
1899 ofputil_append_meter_config(struct ovs_list
*replies
,
1900 const struct ofputil_meter_config
*mc
)
1902 struct ofpbuf
*msg
= ofpbuf_from_list(ovs_list_back(replies
));
1903 size_t start_ofs
= msg
->size
;
1904 struct ofp13_meter_config
*reply
;
1906 ofpbuf_put_uninit(msg
, sizeof *reply
);
1907 ofputil_put_bands(mc
->n_bands
, mc
->bands
, msg
);
1909 reply
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *reply
);
1910 reply
->flags
= htons(mc
->flags
);
1911 reply
->meter_id
= htonl(mc
->meter_id
);
1912 reply
->length
= htons(msg
->size
- start_ofs
);
1914 ofpmp_postappend(replies
, start_ofs
);
1917 /* Encode a meter stat for 'ms' and append it to 'replies'. */
1919 ofputil_append_meter_stats(struct ovs_list
*replies
,
1920 const struct ofputil_meter_stats
*ms
)
1922 struct ofp13_meter_stats
*reply
;
1926 len
= sizeof *reply
+ ms
->n_bands
* sizeof(struct ofp13_meter_band_stats
);
1927 reply
= ofpmp_append(replies
, len
);
1929 reply
->meter_id
= htonl(ms
->meter_id
);
1930 reply
->len
= htons(len
);
1931 memset(reply
->pad
, 0, sizeof reply
->pad
);
1932 reply
->flow_count
= htonl(ms
->flow_count
);
1933 reply
->packet_in_count
= htonll(ms
->packet_in_count
);
1934 reply
->byte_in_count
= htonll(ms
->byte_in_count
);
1935 reply
->duration_sec
= htonl(ms
->duration_sec
);
1936 reply
->duration_nsec
= htonl(ms
->duration_nsec
);
1938 for (n
= 0; n
< ms
->n_bands
; ++n
) {
1939 const struct ofputil_meter_band_stats
*src
= &ms
->bands
[n
];
1940 struct ofp13_meter_band_stats
*dst
= &reply
->band_stats
[n
];
1942 dst
->packet_band_count
= htonll(src
->packet_count
);
1943 dst
->byte_band_count
= htonll(src
->byte_count
);
1947 /* Converts an OFPMP_METER_CONFIG reply in 'msg' into an abstract
1948 * ofputil_meter_config in 'mc', with mc->bands pointing to bands decoded into
1949 * 'bands'. The caller must have initialized 'bands' and retains ownership of
1950 * it across the call.
1952 * Multiple OFPST13_METER_CONFIG replies can be packed into a single OpenFlow
1953 * message. Calling this function multiple times for a single 'msg' iterates
1954 * through the replies. 'bands' is cleared for each reply.
1956 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1957 * otherwise a positive errno value. */
1959 ofputil_decode_meter_config(struct ofpbuf
*msg
,
1960 struct ofputil_meter_config
*mc
,
1961 struct ofpbuf
*bands
)
1963 const struct ofp13_meter_config
*omc
;
1966 /* Pull OpenFlow headers for the first call. */
1968 ofpraw_pull_assert(msg
);
1975 omc
= ofpbuf_try_pull(msg
, sizeof *omc
);
1977 VLOG_WARN_RL(&bad_ofmsg_rl
,
1978 "OFPMP_METER_CONFIG reply has %"PRIu32
" leftover bytes at end",
1980 return OFPERR_OFPBRC_BAD_LEN
;
1983 ofpbuf_clear(bands
);
1984 err
= ofputil_pull_bands(msg
, ntohs(omc
->length
) - sizeof *omc
,
1985 &mc
->n_bands
, bands
);
1989 mc
->meter_id
= ntohl(omc
->meter_id
);
1990 mc
->flags
= ntohs(omc
->flags
);
1991 mc
->bands
= bands
->data
;
1997 ofputil_pull_band_stats(struct ofpbuf
*msg
, size_t len
, uint16_t *n_bands
,
1998 struct ofpbuf
*bands
)
2000 const struct ofp13_meter_band_stats
*ombs
;
2001 struct ofputil_meter_band_stats
*mbs
;
2004 ombs
= ofpbuf_try_pull(msg
, len
);
2006 return OFPERR_OFPBRC_BAD_LEN
;
2009 n
= len
/ sizeof *ombs
;
2010 if (len
!= n
* sizeof *ombs
) {
2011 return OFPERR_OFPBRC_BAD_LEN
;
2014 mbs
= ofpbuf_put_uninit(bands
, len
);
2016 for (i
= 0; i
< n
; ++i
) {
2017 mbs
[i
].packet_count
= ntohll(ombs
[i
].packet_band_count
);
2018 mbs
[i
].byte_count
= ntohll(ombs
[i
].byte_band_count
);
2024 /* Converts an OFPMP_METER reply in 'msg' into an abstract
2025 * ofputil_meter_stats in 'ms', with ms->bands pointing to band stats
2026 * decoded into 'bands'.
2028 * Multiple OFPMP_METER replies can be packed into a single OpenFlow
2029 * message. Calling this function multiple times for a single 'msg' iterates
2030 * through the replies. 'bands' is cleared for each reply.
2032 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2033 * otherwise a positive errno value. */
2035 ofputil_decode_meter_stats(struct ofpbuf
*msg
,
2036 struct ofputil_meter_stats
*ms
,
2037 struct ofpbuf
*bands
)
2039 const struct ofp13_meter_stats
*oms
;
2042 /* Pull OpenFlow headers for the first call. */
2044 ofpraw_pull_assert(msg
);
2051 oms
= ofpbuf_try_pull(msg
, sizeof *oms
);
2053 VLOG_WARN_RL(&bad_ofmsg_rl
,
2054 "OFPMP_METER reply has %"PRIu32
" leftover bytes at end",
2056 return OFPERR_OFPBRC_BAD_LEN
;
2059 ofpbuf_clear(bands
);
2060 err
= ofputil_pull_band_stats(msg
, ntohs(oms
->len
) - sizeof *oms
,
2061 &ms
->n_bands
, bands
);
2065 ms
->meter_id
= ntohl(oms
->meter_id
);
2066 ms
->flow_count
= ntohl(oms
->flow_count
);
2067 ms
->packet_in_count
= ntohll(oms
->packet_in_count
);
2068 ms
->byte_in_count
= ntohll(oms
->byte_in_count
);
2069 ms
->duration_sec
= ntohl(oms
->duration_sec
);
2070 ms
->duration_nsec
= ntohl(oms
->duration_nsec
);
2071 ms
->bands
= bands
->data
;
2077 ofputil_decode_meter_features(const struct ofp_header
*oh
,
2078 struct ofputil_meter_features
*mf
)
2080 const struct ofp13_meter_features
*omf
= ofpmsg_body(oh
);
2082 mf
->max_meters
= ntohl(omf
->max_meter
);
2083 mf
->band_types
= ntohl(omf
->band_types
);
2084 mf
->capabilities
= ntohl(omf
->capabilities
);
2085 mf
->max_bands
= omf
->max_bands
;
2086 mf
->max_color
= omf
->max_color
;
2090 ofputil_encode_meter_features_reply(const struct ofputil_meter_features
*mf
,
2091 const struct ofp_header
*request
)
2093 struct ofpbuf
*reply
;
2094 struct ofp13_meter_features
*omf
;
2096 reply
= ofpraw_alloc_stats_reply(request
, 0);
2097 omf
= ofpbuf_put_zeros(reply
, sizeof *omf
);
2099 omf
->max_meter
= htonl(mf
->max_meters
);
2100 omf
->band_types
= htonl(mf
->band_types
);
2101 omf
->capabilities
= htonl(mf
->capabilities
);
2102 omf
->max_bands
= mf
->max_bands
;
2103 omf
->max_color
= mf
->max_color
;
2109 ofputil_encode_meter_mod(enum ofp_version ofp_version
,
2110 const struct ofputil_meter_mod
*mm
)
2114 struct ofp13_meter_mod
*omm
;
2116 msg
= ofpraw_alloc(OFPRAW_OFPT13_METER_MOD
, ofp_version
,
2117 NXM_TYPICAL_LEN
+ mm
->meter
.n_bands
* 16);
2118 omm
= ofpbuf_put_zeros(msg
, sizeof *omm
);
2119 omm
->command
= htons(mm
->command
);
2120 if (mm
->command
!= OFPMC13_DELETE
) {
2121 omm
->flags
= htons(mm
->meter
.flags
);
2123 omm
->meter_id
= htonl(mm
->meter
.meter_id
);
2125 ofputil_put_bands(mm
->meter
.n_bands
, mm
->meter
.bands
, msg
);
2127 ofpmsg_update_length(msg
);
2132 ofputil_tid_command(const struct ofputil_flow_mod
*fm
,
2133 enum ofputil_protocol protocol
)
2135 return htons(protocol
& OFPUTIL_P_TID
2136 ? (fm
->command
& 0xff) | (fm
->table_id
<< 8)
2140 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
2141 * 'protocol' and returns the message. */
2143 ofputil_encode_flow_mod(const struct ofputil_flow_mod
*fm
,
2144 enum ofputil_protocol protocol
)
2146 enum ofp_version version
= ofputil_protocol_to_ofp_version(protocol
);
2147 ovs_be16 raw_flags
= ofputil_encode_flow_mod_flags(fm
->flags
, version
);
2151 case OFPUTIL_P_OF11_STD
:
2152 case OFPUTIL_P_OF12_OXM
:
2153 case OFPUTIL_P_OF13_OXM
:
2154 case OFPUTIL_P_OF14_OXM
:
2155 case OFPUTIL_P_OF15_OXM
:
2156 case OFPUTIL_P_OF16_OXM
: {
2157 struct ofp11_flow_mod
*ofm
;
2160 tailroom
= ofputil_match_typical_len(protocol
) + fm
->ofpacts_len
;
2161 msg
= ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD
, version
, tailroom
);
2162 ofm
= ofpbuf_put_zeros(msg
, sizeof *ofm
);
2163 if ((protocol
== OFPUTIL_P_OF11_STD
2164 && (fm
->command
== OFPFC_MODIFY
||
2165 fm
->command
== OFPFC_MODIFY_STRICT
)
2166 && fm
->cookie_mask
== htonll(0))
2167 || fm
->command
== OFPFC_ADD
) {
2168 ofm
->cookie
= fm
->new_cookie
;
2170 ofm
->cookie
= fm
->cookie
& fm
->cookie_mask
;
2172 ofm
->cookie_mask
= fm
->cookie_mask
;
2173 if (fm
->table_id
!= OFPTT_ALL
2174 || (protocol
!= OFPUTIL_P_OF11_STD
2175 && (fm
->command
== OFPFC_DELETE
||
2176 fm
->command
== OFPFC_DELETE_STRICT
))) {
2177 ofm
->table_id
= fm
->table_id
;
2181 ofm
->command
= fm
->command
;
2182 ofm
->idle_timeout
= htons(fm
->idle_timeout
);
2183 ofm
->hard_timeout
= htons(fm
->hard_timeout
);
2184 ofm
->priority
= htons(fm
->priority
);
2185 ofm
->buffer_id
= htonl(fm
->buffer_id
);
2186 ofm
->out_port
= ofputil_port_to_ofp11(fm
->out_port
);
2187 ofm
->out_group
= htonl(fm
->out_group
);
2188 ofm
->flags
= raw_flags
;
2189 if (version
>= OFP14_VERSION
&& fm
->command
== OFPFC_ADD
) {
2190 ofm
->importance
= htons(fm
->importance
);
2192 ofm
->importance
= 0;
2194 ofputil_put_ofp11_match(msg
, &fm
->match
, protocol
);
2195 ofpacts_put_openflow_instructions(fm
->ofpacts
, fm
->ofpacts_len
, msg
,
2200 case OFPUTIL_P_OF10_STD
:
2201 case OFPUTIL_P_OF10_STD_TID
: {
2202 struct ofp10_flow_mod
*ofm
;
2204 msg
= ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD
, OFP10_VERSION
,
2206 ofm
= ofpbuf_put_zeros(msg
, sizeof *ofm
);
2207 ofputil_match_to_ofp10_match(&fm
->match
, &ofm
->match
);
2208 ofm
->cookie
= fm
->new_cookie
;
2209 ofm
->command
= ofputil_tid_command(fm
, protocol
);
2210 ofm
->idle_timeout
= htons(fm
->idle_timeout
);
2211 ofm
->hard_timeout
= htons(fm
->hard_timeout
);
2212 ofm
->priority
= htons(fm
->priority
);
2213 ofm
->buffer_id
= htonl(fm
->buffer_id
);
2214 ofm
->out_port
= htons(ofp_to_u16(fm
->out_port
));
2215 ofm
->flags
= raw_flags
;
2216 ofpacts_put_openflow_actions(fm
->ofpacts
, fm
->ofpacts_len
, msg
,
2221 case OFPUTIL_P_OF10_NXM
:
2222 case OFPUTIL_P_OF10_NXM_TID
: {
2223 struct nx_flow_mod
*nfm
;
2226 msg
= ofpraw_alloc(OFPRAW_NXT_FLOW_MOD
, OFP10_VERSION
,
2227 NXM_TYPICAL_LEN
+ fm
->ofpacts_len
);
2228 nfm
= ofpbuf_put_zeros(msg
, sizeof *nfm
);
2229 nfm
->command
= ofputil_tid_command(fm
, protocol
);
2230 nfm
->cookie
= fm
->new_cookie
;
2231 match_len
= nx_put_match(msg
, &fm
->match
, fm
->cookie
, fm
->cookie_mask
);
2233 nfm
->idle_timeout
= htons(fm
->idle_timeout
);
2234 nfm
->hard_timeout
= htons(fm
->hard_timeout
);
2235 nfm
->priority
= htons(fm
->priority
);
2236 nfm
->buffer_id
= htonl(fm
->buffer_id
);
2237 nfm
->out_port
= htons(ofp_to_u16(fm
->out_port
));
2238 nfm
->flags
= raw_flags
;
2239 nfm
->match_len
= htons(match_len
);
2240 ofpacts_put_openflow_actions(fm
->ofpacts
, fm
->ofpacts_len
, msg
,
2249 ofpmsg_update_length(msg
);
2254 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request
*fsr
,
2255 const struct ofp10_flow_stats_request
*ofsr
,
2258 fsr
->aggregate
= aggregate
;
2259 ofputil_match_from_ofp10_match(&ofsr
->match
, &fsr
->match
);
2260 fsr
->out_port
= u16_to_ofp(ntohs(ofsr
->out_port
));
2261 fsr
->out_group
= OFPG_ANY
;
2262 fsr
->table_id
= ofsr
->table_id
;
2263 fsr
->cookie
= fsr
->cookie_mask
= htonll(0);
2269 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request
*fsr
,
2270 struct ofpbuf
*b
, bool aggregate
)
2272 const struct ofp11_flow_stats_request
*ofsr
;
2275 ofsr
= ofpbuf_pull(b
, sizeof *ofsr
);
2276 fsr
->aggregate
= aggregate
;
2277 fsr
->table_id
= ofsr
->table_id
;
2278 error
= ofputil_port_from_ofp11(ofsr
->out_port
, &fsr
->out_port
);
2282 fsr
->out_group
= ntohl(ofsr
->out_group
);
2283 fsr
->cookie
= ofsr
->cookie
;
2284 fsr
->cookie_mask
= ofsr
->cookie_mask
;
2285 error
= ofputil_pull_ofp11_match(b
, &fsr
->match
, NULL
);
2294 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request
*fsr
,
2295 struct ofpbuf
*b
, bool aggregate
)
2297 const struct nx_flow_stats_request
*nfsr
;
2300 nfsr
= ofpbuf_pull(b
, sizeof *nfsr
);
2301 error
= nx_pull_match(b
, ntohs(nfsr
->match_len
), &fsr
->match
,
2302 &fsr
->cookie
, &fsr
->cookie_mask
);
2307 return OFPERR_OFPBRC_BAD_LEN
;
2310 fsr
->aggregate
= aggregate
;
2311 fsr
->out_port
= u16_to_ofp(ntohs(nfsr
->out_port
));
2312 fsr
->out_group
= OFPG_ANY
;
2313 fsr
->table_id
= nfsr
->table_id
;
2318 /* Constructs and returns an OFPT_QUEUE_GET_CONFIG request for the specified
2319 * 'port' and 'queue', suitable for OpenFlow version 'version'.
2321 * 'queue' is honored only for OpenFlow 1.4 and later; older versions always
2322 * request all queues. */
2324 ofputil_encode_queue_get_config_request(enum ofp_version version
,
2328 struct ofpbuf
*request
;
2330 if (version
== OFP10_VERSION
) {
2331 struct ofp10_queue_get_config_request
*qgcr10
;
2333 request
= ofpraw_alloc(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST
,
2335 qgcr10
= ofpbuf_put_zeros(request
, sizeof *qgcr10
);
2336 qgcr10
->port
= htons(ofp_to_u16(port
));
2337 } else if (version
< OFP14_VERSION
) {
2338 struct ofp11_queue_get_config_request
*qgcr11
;
2340 request
= ofpraw_alloc(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST
,
2342 qgcr11
= ofpbuf_put_zeros(request
, sizeof *qgcr11
);
2343 qgcr11
->port
= ofputil_port_to_ofp11(port
);
2345 struct ofp14_queue_desc_request
*qdr14
;
2347 request
= ofpraw_alloc(OFPRAW_OFPST14_QUEUE_DESC_REQUEST
,
2349 qdr14
= ofpbuf_put_zeros(request
, sizeof *qdr14
);
2350 qdr14
->port
= ofputil_port_to_ofp11(port
);
2351 qdr14
->queue
= htonl(queue
);
2357 /* Parses OFPT_QUEUE_GET_CONFIG request 'oh', storing the port specified by the
2358 * request into '*port'. Returns 0 if successful, otherwise an OpenFlow error
2361 ofputil_decode_queue_get_config_request(const struct ofp_header
*oh
,
2362 ofp_port_t
*port
, uint32_t *queue
)
2364 const struct ofp10_queue_get_config_request
*qgcr10
;
2365 const struct ofp11_queue_get_config_request
*qgcr11
;
2366 const struct ofp14_queue_desc_request
*qdr14
;
2367 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
2368 enum ofpraw raw
= ofpraw_pull_assert(&b
);
2370 switch ((int) raw
) {
2371 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST
:
2373 *port
= u16_to_ofp(ntohs(qgcr10
->port
));
2377 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST
:
2380 enum ofperr error
= ofputil_port_from_ofp11(qgcr11
->port
, port
);
2381 if (error
|| *port
== OFPP_ANY
) {
2386 case OFPRAW_OFPST14_QUEUE_DESC_REQUEST
:
2388 *queue
= ntohl(qdr14
->queue
);
2389 return ofputil_port_from_ofp11(qdr14
->port
, port
);
2395 return (ofp_to_u16(*port
) < ofp_to_u16(OFPP_MAX
)
2397 : OFPERR_OFPQOFC_BAD_PORT
);
2400 /* Constructs and returns the beginning of a reply to
2401 * OFPT_QUEUE_GET_CONFIG_REQUEST or OFPMP_QUEUE_DESC request 'oh'. The caller
2402 * may append information about individual queues with
2403 * ofputil_append_queue_get_config_reply(). */
2405 ofputil_start_queue_get_config_reply(const struct ofp_header
*request
,
2406 struct ovs_list
*replies
)
2408 struct ofpbuf
*reply
;
2413 error
= ofputil_decode_queue_get_config_request(request
, &port
, &queue
);
2416 enum ofpraw raw
= ofpraw_decode_assert(request
);
2417 switch ((int) raw
) {
2418 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST
:
2419 reply
= ofpraw_alloc_reply(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY
,
2421 struct ofp10_queue_get_config_reply
*qgcr10
2422 = ofpbuf_put_zeros(reply
, sizeof *qgcr10
);
2423 qgcr10
->port
= htons(ofp_to_u16(port
));
2426 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST
:
2427 reply
= ofpraw_alloc_reply(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY
,
2429 struct ofp11_queue_get_config_reply
*qgcr11
2430 = ofpbuf_put_zeros(reply
, sizeof *qgcr11
);
2431 qgcr11
->port
= ofputil_port_to_ofp11(port
);
2434 case OFPRAW_OFPST14_QUEUE_DESC_REQUEST
:
2435 reply
= ofpraw_alloc_stats_reply(request
, 0);
2442 ovs_list_init(replies
);
2443 ovs_list_push_back(replies
, &reply
->list_node
);
2447 put_ofp10_queue_rate(struct ofpbuf
*reply
,
2448 enum ofp10_queue_properties property
, uint16_t rate
)
2450 if (rate
!= UINT16_MAX
) {
2451 struct ofp10_queue_prop_rate
*oqpr
;
2453 oqpr
= ofpbuf_put_zeros(reply
, sizeof *oqpr
);
2454 oqpr
->prop_header
.property
= htons(property
);
2455 oqpr
->prop_header
.len
= htons(sizeof *oqpr
);
2456 oqpr
->rate
= htons(rate
);
2461 put_ofp14_queue_rate(struct ofpbuf
*reply
,
2462 enum ofp14_queue_desc_prop_type type
, uint16_t rate
)
2464 if (rate
!= UINT16_MAX
) {
2465 ofpprop_put_u16(reply
, type
, rate
);
2470 ofputil_append_queue_get_config_reply(const struct ofputil_queue_config
*qc
,
2471 struct ovs_list
*replies
)
2473 enum ofp_version ofp_version
= ofpmp_version(replies
);
2474 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
2475 size_t start_ofs
= reply
->size
;
2479 if (ofp_version
< OFP14_VERSION
) {
2480 if (ofp_version
< OFP12_VERSION
) {
2481 struct ofp10_packet_queue
*opq10
;
2483 opq10
= ofpbuf_put_zeros(reply
, sizeof *opq10
);
2484 opq10
->queue_id
= htonl(qc
->queue
);
2485 len_ofs
= (char *) &opq10
->len
- (char *) reply
->data
;
2487 struct ofp12_packet_queue
*opq12
;
2489 opq12
= ofpbuf_put_zeros(reply
, sizeof *opq12
);
2490 opq12
->port
= ofputil_port_to_ofp11(qc
->port
);
2491 opq12
->queue_id
= htonl(qc
->queue
);
2492 len_ofs
= (char *) &opq12
->len
- (char *) reply
->data
;
2495 put_ofp10_queue_rate(reply
, OFPQT10_MIN_RATE
, qc
->min_rate
);
2496 put_ofp10_queue_rate(reply
, OFPQT11_MAX_RATE
, qc
->max_rate
);
2498 struct ofp14_queue_desc
*oqd
= ofpbuf_put_zeros(reply
, sizeof *oqd
);
2499 oqd
->port_no
= ofputil_port_to_ofp11(qc
->port
);
2500 oqd
->queue_id
= htonl(qc
->queue
);
2501 len_ofs
= (char *) &oqd
->len
- (char *) reply
->data
;
2502 put_ofp14_queue_rate(reply
, OFPQDPT14_MIN_RATE
, qc
->min_rate
);
2503 put_ofp14_queue_rate(reply
, OFPQDPT14_MAX_RATE
, qc
->max_rate
);
2506 len
= ofpbuf_at(reply
, len_ofs
, sizeof *len
);
2507 *len
= htons(reply
->size
- start_ofs
);
2509 if (ofp_version
>= OFP14_VERSION
) {
2510 ofpmp_postappend(replies
, start_ofs
);
2515 parse_ofp10_queue_rate(const struct ofp10_queue_prop_header
*hdr
,
2518 const struct ofp10_queue_prop_rate
*oqpr
;
2520 if (hdr
->len
== htons(sizeof *oqpr
)) {
2521 oqpr
= (const struct ofp10_queue_prop_rate
*) hdr
;
2522 *rate
= ntohs(oqpr
->rate
);
2525 return OFPERR_OFPBRC_BAD_LEN
;
2530 ofputil_pull_queue_get_config_reply10(struct ofpbuf
*msg
,
2531 struct ofputil_queue_config
*queue
)
2533 const struct ofp_header
*oh
= msg
->header
;
2534 unsigned int opq_len
; /* Length of protocol-specific queue header. */
2535 unsigned int len
; /* Total length of queue + properties. */
2537 /* Obtain the port number from the message header. */
2538 if (oh
->version
== OFP10_VERSION
) {
2539 const struct ofp10_queue_get_config_reply
*oqgcr10
= msg
->msg
;
2540 queue
->port
= u16_to_ofp(ntohs(oqgcr10
->port
));
2542 const struct ofp11_queue_get_config_reply
*oqgcr11
= msg
->msg
;
2543 enum ofperr error
= ofputil_port_from_ofp11(oqgcr11
->port
,
2550 /* Pull off the queue header and get the queue number and length. */
2551 if (oh
->version
< OFP12_VERSION
) {
2552 const struct ofp10_packet_queue
*opq10
;
2553 opq10
= ofpbuf_try_pull(msg
, sizeof *opq10
);
2555 return OFPERR_OFPBRC_BAD_LEN
;
2557 queue
->queue
= ntohl(opq10
->queue_id
);
2558 len
= ntohs(opq10
->len
);
2559 opq_len
= sizeof *opq10
;
2561 const struct ofp12_packet_queue
*opq12
;
2562 opq12
= ofpbuf_try_pull(msg
, sizeof *opq12
);
2564 return OFPERR_OFPBRC_BAD_LEN
;
2566 queue
->queue
= ntohl(opq12
->queue_id
);
2567 len
= ntohs(opq12
->len
);
2568 opq_len
= sizeof *opq12
;
2572 if (len
< opq_len
|| len
> msg
->size
+ opq_len
|| len
% 8) {
2573 return OFPERR_OFPBRC_BAD_LEN
;
2577 /* Pull properties. The format of these properties differs from used in
2578 * OF1.4+ so we can't use the common property functions. */
2580 const struct ofp10_queue_prop_header
*hdr
;
2581 unsigned int property
;
2582 unsigned int prop_len
;
2583 enum ofperr error
= 0;
2585 hdr
= ofpbuf_at_assert(msg
, 0, sizeof *hdr
);
2586 prop_len
= ntohs(hdr
->len
);
2587 if (prop_len
< sizeof *hdr
|| prop_len
> msg
->size
|| prop_len
% 8) {
2588 return OFPERR_OFPBRC_BAD_LEN
;
2591 property
= ntohs(hdr
->property
);
2593 case OFPQT10_MIN_RATE
:
2594 error
= parse_ofp10_queue_rate(hdr
, &queue
->min_rate
);
2597 case OFPQT11_MAX_RATE
:
2598 error
= parse_ofp10_queue_rate(hdr
, &queue
->max_rate
);
2602 VLOG_INFO_RL(&bad_ofmsg_rl
, "unknown queue property %u", property
);
2609 ofpbuf_pull(msg
, prop_len
);
2616 ofputil_pull_queue_get_config_reply14(struct ofpbuf
*msg
,
2617 struct ofputil_queue_config
*queue
)
2619 struct ofp14_queue_desc
*oqd14
= ofpbuf_try_pull(msg
, sizeof *oqd14
);
2621 return OFPERR_OFPBRC_BAD_LEN
;
2623 enum ofperr error
= ofputil_port_from_ofp11(oqd14
->port_no
, &queue
->port
);
2627 queue
->queue
= ntohl(oqd14
->queue_id
);
2630 unsigned int len
= ntohs(oqd14
->len
);
2631 if (len
< sizeof *oqd14
|| len
> msg
->size
+ sizeof *oqd14
|| len
% 8) {
2632 return OFPERR_OFPBRC_BAD_LEN
;
2634 len
-= sizeof *oqd14
;
2636 struct ofpbuf properties
= ofpbuf_const_initializer(ofpbuf_pull(msg
, len
),
2638 while (properties
.size
> 0) {
2639 struct ofpbuf payload
;
2642 error
= ofpprop_pull(&properties
, &payload
, &type
);
2648 case OFPQDPT14_MIN_RATE
:
2649 error
= ofpprop_parse_u16(&payload
, &queue
->min_rate
);
2652 case OFPQDPT14_MAX_RATE
:
2653 error
= ofpprop_parse_u16(&payload
, &queue
->max_rate
);
2657 error
= OFPPROP_UNKNOWN(true, "queue desc", type
);
2669 /* Decodes information about a queue from the OFPT_QUEUE_GET_CONFIG_REPLY in
2670 * 'reply' and stores it in '*queue'. ofputil_decode_queue_get_config_reply()
2671 * must already have pulled off the main header.
2673 * This function returns EOF if the last queue has already been decoded, 0 if a
2674 * queue was successfully decoded into '*queue', or an ofperr if there was a
2675 * problem decoding 'reply'. */
2677 ofputil_pull_queue_get_config_reply(struct ofpbuf
*msg
,
2678 struct ofputil_queue_config
*queue
)
2682 /* Pull OpenFlow header. */
2683 raw
= ofpraw_pull_assert(msg
);
2685 /* Pull protocol-specific ofp_queue_get_config_reply header (OF1.4
2686 * doesn't have one at all). */
2687 if (raw
== OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY
) {
2688 ofpbuf_pull(msg
, sizeof(struct ofp10_queue_get_config_reply
));
2689 } else if (raw
== OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY
) {
2690 ofpbuf_pull(msg
, sizeof(struct ofp11_queue_get_config_reply
));
2692 ovs_assert(raw
== OFPRAW_OFPST14_QUEUE_DESC_REPLY
);
2695 raw
= ofpraw_decode_assert(msg
->header
);
2698 queue
->min_rate
= UINT16_MAX
;
2699 queue
->max_rate
= UINT16_MAX
;
2703 } else if (raw
== OFPRAW_OFPST14_QUEUE_DESC_REPLY
) {
2704 return ofputil_pull_queue_get_config_reply14(msg
, queue
);
2706 return ofputil_pull_queue_get_config_reply10(msg
, queue
);
2710 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
2711 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
2712 * successful, otherwise an OpenFlow error code. */
2714 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request
*fsr
,
2715 const struct ofp_header
*oh
)
2717 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
2718 enum ofpraw raw
= ofpraw_pull_assert(&b
);
2719 switch ((int) raw
) {
2720 case OFPRAW_OFPST10_FLOW_REQUEST
:
2721 return ofputil_decode_ofpst10_flow_request(fsr
, b
.data
, false);
2723 case OFPRAW_OFPST10_AGGREGATE_REQUEST
:
2724 return ofputil_decode_ofpst10_flow_request(fsr
, b
.data
, true);
2726 case OFPRAW_OFPST11_FLOW_REQUEST
:
2727 return ofputil_decode_ofpst11_flow_request(fsr
, &b
, false);
2729 case OFPRAW_OFPST11_AGGREGATE_REQUEST
:
2730 return ofputil_decode_ofpst11_flow_request(fsr
, &b
, true);
2732 case OFPRAW_NXST_FLOW_REQUEST
:
2733 return ofputil_decode_nxst_flow_request(fsr
, &b
, false);
2735 case OFPRAW_NXST_AGGREGATE_REQUEST
:
2736 return ofputil_decode_nxst_flow_request(fsr
, &b
, true);
2739 /* Hey, the caller lied. */
2744 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
2745 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
2746 * 'protocol', and returns the message. */
2748 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request
*fsr
,
2749 enum ofputil_protocol protocol
)
2755 case OFPUTIL_P_OF11_STD
:
2756 case OFPUTIL_P_OF12_OXM
:
2757 case OFPUTIL_P_OF13_OXM
:
2758 case OFPUTIL_P_OF14_OXM
:
2759 case OFPUTIL_P_OF15_OXM
:
2760 case OFPUTIL_P_OF16_OXM
: {
2761 struct ofp11_flow_stats_request
*ofsr
;
2763 raw
= (fsr
->aggregate
2764 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
2765 : OFPRAW_OFPST11_FLOW_REQUEST
);
2766 msg
= ofpraw_alloc(raw
, ofputil_protocol_to_ofp_version(protocol
),
2767 ofputil_match_typical_len(protocol
));
2768 ofsr
= ofpbuf_put_zeros(msg
, sizeof *ofsr
);
2769 ofsr
->table_id
= fsr
->table_id
;
2770 ofsr
->out_port
= ofputil_port_to_ofp11(fsr
->out_port
);
2771 ofsr
->out_group
= htonl(fsr
->out_group
);
2772 ofsr
->cookie
= fsr
->cookie
;
2773 ofsr
->cookie_mask
= fsr
->cookie_mask
;
2774 ofputil_put_ofp11_match(msg
, &fsr
->match
, protocol
);
2778 case OFPUTIL_P_OF10_STD
:
2779 case OFPUTIL_P_OF10_STD_TID
: {
2780 struct ofp10_flow_stats_request
*ofsr
;
2782 raw
= (fsr
->aggregate
2783 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
2784 : OFPRAW_OFPST10_FLOW_REQUEST
);
2785 msg
= ofpraw_alloc(raw
, OFP10_VERSION
, 0);
2786 ofsr
= ofpbuf_put_zeros(msg
, sizeof *ofsr
);
2787 ofputil_match_to_ofp10_match(&fsr
->match
, &ofsr
->match
);
2788 ofsr
->table_id
= fsr
->table_id
;
2789 ofsr
->out_port
= htons(ofp_to_u16(fsr
->out_port
));
2793 case OFPUTIL_P_OF10_NXM
:
2794 case OFPUTIL_P_OF10_NXM_TID
: {
2795 struct nx_flow_stats_request
*nfsr
;
2798 raw
= (fsr
->aggregate
2799 ? OFPRAW_NXST_AGGREGATE_REQUEST
2800 : OFPRAW_NXST_FLOW_REQUEST
);
2801 msg
= ofpraw_alloc(raw
, OFP10_VERSION
, NXM_TYPICAL_LEN
);
2802 ofpbuf_put_zeros(msg
, sizeof *nfsr
);
2803 match_len
= nx_put_match(msg
, &fsr
->match
,
2804 fsr
->cookie
, fsr
->cookie_mask
);
2807 nfsr
->out_port
= htons(ofp_to_u16(fsr
->out_port
));
2808 nfsr
->match_len
= htons(match_len
);
2809 nfsr
->table_id
= fsr
->table_id
;
2820 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
2821 * ofputil_flow_stats in 'fs'.
2823 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
2824 * OpenFlow message. Calling this function multiple times for a single 'msg'
2825 * iterates through the replies. The caller must initially leave 'msg''s layer
2826 * pointers null and not modify them between calls.
2828 * Most switches don't send the values needed to populate fs->idle_age and
2829 * fs->hard_age, so those members will usually be set to 0. If the switch from
2830 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
2831 * 'flow_age_extension' as true so that the contents of 'msg' determine the
2832 * 'idle_age' and 'hard_age' members in 'fs'.
2834 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
2835 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
2836 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
2838 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2839 * otherwise a positive errno value. */
2841 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats
*fs
,
2843 bool flow_age_extension
,
2844 struct ofpbuf
*ofpacts
)
2846 const struct ofp_header
*oh
;
2847 size_t instructions_len
;
2851 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
2852 : ofpraw_pull(&raw
, msg
));
2860 } else if (raw
== OFPRAW_OFPST11_FLOW_REPLY
2861 || raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
2862 const struct ofp11_flow_stats
*ofs
;
2864 uint16_t padded_match_len
;
2866 ofs
= ofpbuf_try_pull(msg
, sizeof *ofs
);
2868 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply has %"PRIu32
" leftover "
2869 "bytes at end", msg
->size
);
2873 length
= ntohs(ofs
->length
);
2874 if (length
< sizeof *ofs
) {
2875 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply claims invalid "
2876 "length %"PRIuSIZE
, length
);
2880 if (ofputil_pull_ofp11_match(msg
, &fs
->match
, &padded_match_len
)) {
2881 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply bad match");
2884 instructions_len
= length
- sizeof *ofs
- padded_match_len
;
2886 fs
->priority
= ntohs(ofs
->priority
);
2887 fs
->table_id
= ofs
->table_id
;
2888 fs
->duration_sec
= ntohl(ofs
->duration_sec
);
2889 fs
->duration_nsec
= ntohl(ofs
->duration_nsec
);
2890 fs
->idle_timeout
= ntohs(ofs
->idle_timeout
);
2891 fs
->hard_timeout
= ntohs(ofs
->hard_timeout
);
2892 if (oh
->version
>= OFP14_VERSION
) {
2893 fs
->importance
= ntohs(ofs
->importance
);
2897 if (raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
2898 error
= ofputil_decode_flow_mod_flags(ofs
->flags
, -1, oh
->version
,
2908 fs
->cookie
= ofs
->cookie
;
2909 fs
->packet_count
= ntohll(ofs
->packet_count
);
2910 fs
->byte_count
= ntohll(ofs
->byte_count
);
2911 } else if (raw
== OFPRAW_OFPST10_FLOW_REPLY
) {
2912 const struct ofp10_flow_stats
*ofs
;
2915 ofs
= ofpbuf_try_pull(msg
, sizeof *ofs
);
2917 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply has %"PRIu32
" leftover "
2918 "bytes at end", msg
->size
);
2922 length
= ntohs(ofs
->length
);
2923 if (length
< sizeof *ofs
) {
2924 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply claims invalid "
2925 "length %"PRIuSIZE
, length
);
2928 instructions_len
= length
- sizeof *ofs
;
2930 fs
->cookie
= get_32aligned_be64(&ofs
->cookie
);
2931 ofputil_match_from_ofp10_match(&ofs
->match
, &fs
->match
);
2932 fs
->priority
= ntohs(ofs
->priority
);
2933 fs
->table_id
= ofs
->table_id
;
2934 fs
->duration_sec
= ntohl(ofs
->duration_sec
);
2935 fs
->duration_nsec
= ntohl(ofs
->duration_nsec
);
2936 fs
->idle_timeout
= ntohs(ofs
->idle_timeout
);
2937 fs
->hard_timeout
= ntohs(ofs
->hard_timeout
);
2941 fs
->packet_count
= ntohll(get_32aligned_be64(&ofs
->packet_count
));
2942 fs
->byte_count
= ntohll(get_32aligned_be64(&ofs
->byte_count
));
2944 } else if (raw
== OFPRAW_NXST_FLOW_REPLY
) {
2945 const struct nx_flow_stats
*nfs
;
2946 size_t match_len
, length
;
2948 nfs
= ofpbuf_try_pull(msg
, sizeof *nfs
);
2950 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW reply has %"PRIu32
" leftover "
2951 "bytes at end", msg
->size
);
2955 length
= ntohs(nfs
->length
);
2956 match_len
= ntohs(nfs
->match_len
);
2957 if (length
< sizeof *nfs
+ ROUND_UP(match_len
, 8)) {
2958 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW reply with match_len=%"PRIuSIZE
" "
2959 "claims invalid length %"PRIuSIZE
, match_len
, length
);
2962 if (nx_pull_match(msg
, match_len
, &fs
->match
, NULL
, NULL
)) {
2965 instructions_len
= length
- sizeof *nfs
- ROUND_UP(match_len
, 8);
2967 fs
->cookie
= nfs
->cookie
;
2968 fs
->table_id
= nfs
->table_id
;
2969 fs
->duration_sec
= ntohl(nfs
->duration_sec
);
2970 fs
->duration_nsec
= ntohl(nfs
->duration_nsec
);
2971 fs
->priority
= ntohs(nfs
->priority
);
2972 fs
->idle_timeout
= ntohs(nfs
->idle_timeout
);
2973 fs
->hard_timeout
= ntohs(nfs
->hard_timeout
);
2977 if (flow_age_extension
) {
2978 if (nfs
->idle_age
) {
2979 fs
->idle_age
= ntohs(nfs
->idle_age
) - 1;
2981 if (nfs
->hard_age
) {
2982 fs
->hard_age
= ntohs(nfs
->hard_age
) - 1;
2985 fs
->packet_count
= ntohll(nfs
->packet_count
);
2986 fs
->byte_count
= ntohll(nfs
->byte_count
);
2992 if (ofpacts_pull_openflow_instructions(msg
, instructions_len
, oh
->version
,
2994 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply bad instructions");
2997 fs
->ofpacts
= ofpacts
->data
;
2998 fs
->ofpacts_len
= ofpacts
->size
;
3003 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
3005 * We use this in situations where OVS internally uses UINT64_MAX to mean
3006 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
3008 unknown_to_zero(uint64_t count
)
3010 return count
!= UINT64_MAX
? count
: 0;
3013 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
3014 * those already present in the list of ofpbufs in 'replies'. 'replies' should
3015 * have been initialized with ofpmp_init(). */
3017 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats
*fs
,
3018 struct ovs_list
*replies
)
3020 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
3021 size_t start_ofs
= reply
->size
;
3022 enum ofp_version version
= ofpmp_version(replies
);
3023 enum ofpraw raw
= ofpmp_decode_raw(replies
);
3025 if (raw
== OFPRAW_OFPST11_FLOW_REPLY
|| raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
3026 struct ofp11_flow_stats
*ofs
;
3028 ofpbuf_put_uninit(reply
, sizeof *ofs
);
3029 oxm_put_match(reply
, &fs
->match
, version
);
3030 ofpacts_put_openflow_instructions(fs
->ofpacts
, fs
->ofpacts_len
, reply
,
3033 ofs
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *ofs
);
3034 ofs
->length
= htons(reply
->size
- start_ofs
);
3035 ofs
->table_id
= fs
->table_id
;
3037 ofs
->duration_sec
= htonl(fs
->duration_sec
);
3038 ofs
->duration_nsec
= htonl(fs
->duration_nsec
);
3039 ofs
->priority
= htons(fs
->priority
);
3040 ofs
->idle_timeout
= htons(fs
->idle_timeout
);
3041 ofs
->hard_timeout
= htons(fs
->hard_timeout
);
3042 if (version
>= OFP14_VERSION
) {
3043 ofs
->importance
= htons(fs
->importance
);
3045 ofs
->importance
= 0;
3047 if (raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
3048 ofs
->flags
= ofputil_encode_flow_mod_flags(fs
->flags
, version
);
3052 memset(ofs
->pad2
, 0, sizeof ofs
->pad2
);
3053 ofs
->cookie
= fs
->cookie
;
3054 ofs
->packet_count
= htonll(unknown_to_zero(fs
->packet_count
));
3055 ofs
->byte_count
= htonll(unknown_to_zero(fs
->byte_count
));
3056 } else if (raw
== OFPRAW_OFPST10_FLOW_REPLY
) {
3057 struct ofp10_flow_stats
*ofs
;
3059 ofpbuf_put_uninit(reply
, sizeof *ofs
);
3060 ofpacts_put_openflow_actions(fs
->ofpacts
, fs
->ofpacts_len
, reply
,
3062 ofs
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *ofs
);
3063 ofs
->length
= htons(reply
->size
- start_ofs
);
3064 ofs
->table_id
= fs
->table_id
;
3066 ofputil_match_to_ofp10_match(&fs
->match
, &ofs
->match
);
3067 ofs
->duration_sec
= htonl(fs
->duration_sec
);
3068 ofs
->duration_nsec
= htonl(fs
->duration_nsec
);
3069 ofs
->priority
= htons(fs
->priority
);
3070 ofs
->idle_timeout
= htons(fs
->idle_timeout
);
3071 ofs
->hard_timeout
= htons(fs
->hard_timeout
);
3072 memset(ofs
->pad2
, 0, sizeof ofs
->pad2
);
3073 put_32aligned_be64(&ofs
->cookie
, fs
->cookie
);
3074 put_32aligned_be64(&ofs
->packet_count
,
3075 htonll(unknown_to_zero(fs
->packet_count
)));
3076 put_32aligned_be64(&ofs
->byte_count
,
3077 htonll(unknown_to_zero(fs
->byte_count
)));
3078 } else if (raw
== OFPRAW_NXST_FLOW_REPLY
) {
3079 struct nx_flow_stats
*nfs
;
3082 ofpbuf_put_uninit(reply
, sizeof *nfs
);
3083 match_len
= nx_put_match(reply
, &fs
->match
, 0, 0);
3084 ofpacts_put_openflow_actions(fs
->ofpacts
, fs
->ofpacts_len
, reply
,
3086 nfs
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *nfs
);
3087 nfs
->length
= htons(reply
->size
- start_ofs
);
3088 nfs
->table_id
= fs
->table_id
;
3090 nfs
->duration_sec
= htonl(fs
->duration_sec
);
3091 nfs
->duration_nsec
= htonl(fs
->duration_nsec
);
3092 nfs
->priority
= htons(fs
->priority
);
3093 nfs
->idle_timeout
= htons(fs
->idle_timeout
);
3094 nfs
->hard_timeout
= htons(fs
->hard_timeout
);
3095 nfs
->idle_age
= htons(fs
->idle_age
< 0 ? 0
3096 : fs
->idle_age
< UINT16_MAX
? fs
->idle_age
+ 1
3098 nfs
->hard_age
= htons(fs
->hard_age
< 0 ? 0
3099 : fs
->hard_age
< UINT16_MAX
? fs
->hard_age
+ 1
3101 nfs
->match_len
= htons(match_len
);
3102 nfs
->cookie
= fs
->cookie
;
3103 nfs
->packet_count
= htonll(fs
->packet_count
);
3104 nfs
->byte_count
= htonll(fs
->byte_count
);
3109 ofpmp_postappend(replies
, start_ofs
);
3112 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
3113 * NXST_AGGREGATE reply matching 'request', and returns the message. */
3115 ofputil_encode_aggregate_stats_reply(
3116 const struct ofputil_aggregate_stats
*stats
,
3117 const struct ofp_header
*request
)
3119 struct ofp_aggregate_stats_reply
*asr
;
3120 uint64_t packet_count
;
3121 uint64_t byte_count
;
3125 ofpraw_decode(&raw
, request
);
3126 if (raw
== OFPRAW_OFPST10_AGGREGATE_REQUEST
) {
3127 packet_count
= unknown_to_zero(stats
->packet_count
);
3128 byte_count
= unknown_to_zero(stats
->byte_count
);
3130 packet_count
= stats
->packet_count
;
3131 byte_count
= stats
->byte_count
;
3134 msg
= ofpraw_alloc_stats_reply(request
, 0);
3135 asr
= ofpbuf_put_zeros(msg
, sizeof *asr
);
3136 put_32aligned_be64(&asr
->packet_count
, htonll(packet_count
));
3137 put_32aligned_be64(&asr
->byte_count
, htonll(byte_count
));
3138 asr
->flow_count
= htonl(stats
->flow_count
);
3144 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats
*stats
,
3145 const struct ofp_header
*reply
)
3147 struct ofpbuf msg
= ofpbuf_const_initializer(reply
, ntohs(reply
->length
));
3148 ofpraw_pull_assert(&msg
);
3150 struct ofp_aggregate_stats_reply
*asr
= msg
.msg
;
3151 stats
->packet_count
= ntohll(get_32aligned_be64(&asr
->packet_count
));
3152 stats
->byte_count
= ntohll(get_32aligned_be64(&asr
->byte_count
));
3153 stats
->flow_count
= ntohl(asr
->flow_count
);
3158 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
3159 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
3160 * an OpenFlow error code. */
3162 ofputil_decode_flow_removed(struct ofputil_flow_removed
*fr
,
3163 const struct ofp_header
*oh
)
3165 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
3166 enum ofpraw raw
= ofpraw_pull_assert(&b
);
3167 if (raw
== OFPRAW_OFPT11_FLOW_REMOVED
) {
3168 const struct ofp12_flow_removed
*ofr
;
3171 ofr
= ofpbuf_pull(&b
, sizeof *ofr
);
3173 error
= ofputil_pull_ofp11_match(&b
, &fr
->match
, NULL
);
3178 fr
->priority
= ntohs(ofr
->priority
);
3179 fr
->cookie
= ofr
->cookie
;
3180 fr
->reason
= ofr
->reason
;
3181 fr
->table_id
= ofr
->table_id
;
3182 fr
->duration_sec
= ntohl(ofr
->duration_sec
);
3183 fr
->duration_nsec
= ntohl(ofr
->duration_nsec
);
3184 fr
->idle_timeout
= ntohs(ofr
->idle_timeout
);
3185 fr
->hard_timeout
= ntohs(ofr
->hard_timeout
);
3186 fr
->packet_count
= ntohll(ofr
->packet_count
);
3187 fr
->byte_count
= ntohll(ofr
->byte_count
);
3188 } else if (raw
== OFPRAW_OFPT10_FLOW_REMOVED
) {
3189 const struct ofp10_flow_removed
*ofr
;
3191 ofr
= ofpbuf_pull(&b
, sizeof *ofr
);
3193 ofputil_match_from_ofp10_match(&ofr
->match
, &fr
->match
);
3194 fr
->priority
= ntohs(ofr
->priority
);
3195 fr
->cookie
= ofr
->cookie
;
3196 fr
->reason
= ofr
->reason
;
3198 fr
->duration_sec
= ntohl(ofr
->duration_sec
);
3199 fr
->duration_nsec
= ntohl(ofr
->duration_nsec
);
3200 fr
->idle_timeout
= ntohs(ofr
->idle_timeout
);
3201 fr
->hard_timeout
= 0;
3202 fr
->packet_count
= ntohll(ofr
->packet_count
);
3203 fr
->byte_count
= ntohll(ofr
->byte_count
);
3204 } else if (raw
== OFPRAW_NXT_FLOW_REMOVED
) {
3205 struct nx_flow_removed
*nfr
;
3208 nfr
= ofpbuf_pull(&b
, sizeof *nfr
);
3209 error
= nx_pull_match(&b
, ntohs(nfr
->match_len
), &fr
->match
,
3215 return OFPERR_OFPBRC_BAD_LEN
;
3218 fr
->priority
= ntohs(nfr
->priority
);
3219 fr
->cookie
= nfr
->cookie
;
3220 fr
->reason
= nfr
->reason
;
3221 fr
->table_id
= nfr
->table_id
? nfr
->table_id
- 1 : 255;
3222 fr
->duration_sec
= ntohl(nfr
->duration_sec
);
3223 fr
->duration_nsec
= ntohl(nfr
->duration_nsec
);
3224 fr
->idle_timeout
= ntohs(nfr
->idle_timeout
);
3225 fr
->hard_timeout
= 0;
3226 fr
->packet_count
= ntohll(nfr
->packet_count
);
3227 fr
->byte_count
= ntohll(nfr
->byte_count
);
3235 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
3236 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
3239 ofputil_encode_flow_removed(const struct ofputil_flow_removed
*fr
,
3240 enum ofputil_protocol protocol
)
3243 enum ofp_flow_removed_reason reason
= fr
->reason
;
3245 if (reason
== OFPRR_METER_DELETE
&& !(protocol
& OFPUTIL_P_OF14_UP
)) {
3246 reason
= OFPRR_DELETE
;
3250 case OFPUTIL_P_OF11_STD
:
3251 case OFPUTIL_P_OF12_OXM
:
3252 case OFPUTIL_P_OF13_OXM
:
3253 case OFPUTIL_P_OF14_OXM
:
3254 case OFPUTIL_P_OF15_OXM
:
3255 case OFPUTIL_P_OF16_OXM
: {
3256 struct ofp12_flow_removed
*ofr
;
3258 msg
= ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED
,
3259 ofputil_protocol_to_ofp_version(protocol
),
3261 ofputil_match_typical_len(protocol
));
3262 ofr
= ofpbuf_put_zeros(msg
, sizeof *ofr
);
3263 ofr
->cookie
= fr
->cookie
;
3264 ofr
->priority
= htons(fr
->priority
);
3265 ofr
->reason
= reason
;
3266 ofr
->table_id
= fr
->table_id
;
3267 ofr
->duration_sec
= htonl(fr
->duration_sec
);
3268 ofr
->duration_nsec
= htonl(fr
->duration_nsec
);
3269 ofr
->idle_timeout
= htons(fr
->idle_timeout
);
3270 ofr
->hard_timeout
= htons(fr
->hard_timeout
);
3271 ofr
->packet_count
= htonll(fr
->packet_count
);
3272 ofr
->byte_count
= htonll(fr
->byte_count
);
3273 ofputil_put_ofp11_match(msg
, &fr
->match
, protocol
);
3277 case OFPUTIL_P_OF10_STD
:
3278 case OFPUTIL_P_OF10_STD_TID
: {
3279 struct ofp10_flow_removed
*ofr
;
3281 msg
= ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED
, OFP10_VERSION
,
3283 ofr
= ofpbuf_put_zeros(msg
, sizeof *ofr
);
3284 ofputil_match_to_ofp10_match(&fr
->match
, &ofr
->match
);
3285 ofr
->cookie
= fr
->cookie
;
3286 ofr
->priority
= htons(fr
->priority
);
3287 ofr
->reason
= reason
;
3288 ofr
->duration_sec
= htonl(fr
->duration_sec
);
3289 ofr
->duration_nsec
= htonl(fr
->duration_nsec
);
3290 ofr
->idle_timeout
= htons(fr
->idle_timeout
);
3291 ofr
->packet_count
= htonll(unknown_to_zero(fr
->packet_count
));
3292 ofr
->byte_count
= htonll(unknown_to_zero(fr
->byte_count
));
3296 case OFPUTIL_P_OF10_NXM
:
3297 case OFPUTIL_P_OF10_NXM_TID
: {
3298 struct nx_flow_removed
*nfr
;
3301 msg
= ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED
, OFP10_VERSION
,
3302 htonl(0), NXM_TYPICAL_LEN
);
3303 ofpbuf_put_zeros(msg
, sizeof *nfr
);
3304 match_len
= nx_put_match(msg
, &fr
->match
, 0, 0);
3307 nfr
->cookie
= fr
->cookie
;
3308 nfr
->priority
= htons(fr
->priority
);
3309 nfr
->reason
= reason
;
3310 nfr
->table_id
= fr
->table_id
+ 1;
3311 nfr
->duration_sec
= htonl(fr
->duration_sec
);
3312 nfr
->duration_nsec
= htonl(fr
->duration_nsec
);
3313 nfr
->idle_timeout
= htons(fr
->idle_timeout
);
3314 nfr
->match_len
= htons(match_len
);
3315 nfr
->packet_count
= htonll(fr
->packet_count
);
3316 nfr
->byte_count
= htonll(fr
->byte_count
);
3327 /* The caller has done basic initialization of '*pin'; the other output
3328 * arguments needs to be initialized. */
3330 decode_nx_packet_in2(const struct ofp_header
*oh
, bool loose
,
3331 struct ofputil_packet_in
*pin
,
3332 size_t *total_len
, uint32_t *buffer_id
,
3333 struct ofpbuf
*continuation
)
3336 *buffer_id
= UINT32_MAX
;
3338 struct ofpbuf properties
;
3339 ofpbuf_use_const(&properties
, oh
, ntohs(oh
->length
));
3340 ofpraw_pull_assert(&properties
);
3342 while (properties
.size
> 0) {
3343 struct ofpbuf payload
;
3346 enum ofperr error
= ofpprop_pull(&properties
, &payload
, &type
);
3353 pin
->packet
= payload
.msg
;
3354 pin
->packet_len
= ofpbuf_msgsize(&payload
);
3357 case NXPINT_FULL_LEN
: {
3359 error
= ofpprop_parse_u32(&payload
, &u32
);
3364 case NXPINT_BUFFER_ID
:
3365 error
= ofpprop_parse_u32(&payload
, buffer_id
);
3368 case NXPINT_TABLE_ID
:
3369 error
= ofpprop_parse_u8(&payload
, &pin
->table_id
);
3373 error
= ofpprop_parse_be64(&payload
, &pin
->cookie
);
3376 case NXPINT_REASON
: {
3378 error
= ofpprop_parse_u8(&payload
, &reason
);
3379 pin
->reason
= reason
;
3383 case NXPINT_METADATA
:
3384 error
= oxm_decode_match(payload
.msg
, ofpbuf_msgsize(&payload
),
3385 &pin
->flow_metadata
);
3388 case NXPINT_USERDATA
:
3389 pin
->userdata
= payload
.msg
;
3390 pin
->userdata_len
= ofpbuf_msgsize(&payload
);
3393 case NXPINT_CONTINUATION
:
3395 error
= ofpprop_parse_nested(&payload
, continuation
);
3400 error
= OFPPROP_UNKNOWN(loose
, "NX_PACKET_IN2", type
);
3408 if (!pin
->packet_len
) {
3409 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXT_PACKET_IN2 lacks packet");
3410 return OFPERR_OFPBRC_BAD_LEN
;
3411 } else if (!*total_len
) {
3412 *total_len
= pin
->packet_len
;
3413 } else if (*total_len
< pin
->packet_len
) {
3414 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXT_PACKET_IN2 claimed full_len < len");
3415 return OFPERR_OFPBRC_BAD_LEN
;
3421 /* Decodes the packet-in message starting at 'oh' into '*pin'. Populates
3422 * 'pin->packet' and 'pin->packet_len' with the part of the packet actually
3423 * included in the message. If 'total_lenp' is nonnull, populates
3424 * '*total_lenp' with the original length of the packet (which is larger than
3425 * 'packet->len' if only part of the packet was included). If 'buffer_idp' is
3426 * nonnull, stores the packet's buffer ID in '*buffer_idp' (UINT32_MAX if it
3427 * was not buffered).
3429 * Populates 'continuation', if nonnull, with the continuation data from the
3430 * packet-in (an empty buffer, if 'oh' did not contain continuation data). The
3431 * format of this data is supposed to be opaque to anything other than
3432 * ovs-vswitchd, so that in any other process the only reasonable use of this
3433 * data is to be copied into an NXT_RESUME message via ofputil_encode_resume().
3435 * This function points 'pin->packet' into 'oh', so the caller should not free
3436 * it separately from the original OpenFlow message. This is also true for
3437 * 'pin->userdata' (which could also end up NULL if there is no userdata).
3439 * Returns 0 if successful, otherwise an OpenFlow error code. */
3441 ofputil_decode_packet_in(const struct ofp_header
*oh
, bool loose
,
3442 struct ofputil_packet_in
*pin
,
3443 size_t *total_lenp
, uint32_t *buffer_idp
,
3444 struct ofpbuf
*continuation
)
3449 memset(pin
, 0, sizeof *pin
);
3450 pin
->cookie
= OVS_BE64_MAX
;
3452 ofpbuf_use_const(continuation
, NULL
, 0);
3455 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
3456 enum ofpraw raw
= ofpraw_pull_assert(&b
);
3457 if (raw
== OFPRAW_OFPT13_PACKET_IN
|| raw
== OFPRAW_OFPT12_PACKET_IN
) {
3458 const struct ofp12_packet_in
*opi
= ofpbuf_pull(&b
, sizeof *opi
);
3459 const ovs_be64
*cookie
= (raw
== OFPRAW_OFPT13_PACKET_IN
3460 ? ofpbuf_pull(&b
, sizeof *cookie
)
3462 enum ofperr error
= oxm_pull_match_loose(&b
, &pin
->flow_metadata
);
3467 if (!ofpbuf_try_pull(&b
, 2)) {
3468 return OFPERR_OFPBRC_BAD_LEN
;
3471 pin
->reason
= opi
->reason
;
3472 pin
->table_id
= opi
->table_id
;
3473 buffer_id
= ntohl(opi
->buffer_id
);
3474 total_len
= ntohs(opi
->total_len
);
3476 pin
->cookie
= *cookie
;
3479 pin
->packet
= b
.data
;
3480 pin
->packet_len
= b
.size
;
3481 } else if (raw
== OFPRAW_OFPT10_PACKET_IN
) {
3482 const struct ofp10_packet_in
*opi
;
3484 opi
= ofpbuf_pull(&b
, offsetof(struct ofp10_packet_in
, data
));
3486 pin
->packet
= CONST_CAST(uint8_t *, opi
->data
);
3487 pin
->packet_len
= b
.size
;
3489 match_init_catchall(&pin
->flow_metadata
);
3490 match_set_in_port(&pin
->flow_metadata
,
3491 u16_to_ofp(ntohs(opi
->in_port
)));
3492 pin
->reason
= opi
->reason
;
3493 buffer_id
= ntohl(opi
->buffer_id
);
3494 total_len
= ntohs(opi
->total_len
);
3495 } else if (raw
== OFPRAW_OFPT11_PACKET_IN
) {
3496 const struct ofp11_packet_in
*opi
;
3500 opi
= ofpbuf_pull(&b
, sizeof *opi
);
3502 pin
->packet
= b
.data
;
3503 pin
->packet_len
= b
.size
;
3505 buffer_id
= ntohl(opi
->buffer_id
);
3506 error
= ofputil_port_from_ofp11(opi
->in_port
, &in_port
);
3510 match_init_catchall(&pin
->flow_metadata
);
3511 match_set_in_port(&pin
->flow_metadata
, in_port
);
3512 total_len
= ntohs(opi
->total_len
);
3513 pin
->reason
= opi
->reason
;
3514 pin
->table_id
= opi
->table_id
;
3515 } else if (raw
== OFPRAW_NXT_PACKET_IN
) {
3516 const struct nx_packet_in
*npi
;
3519 npi
= ofpbuf_pull(&b
, sizeof *npi
);
3520 error
= nx_pull_match_loose(&b
, ntohs(npi
->match_len
),
3521 &pin
->flow_metadata
, NULL
, NULL
);
3526 if (!ofpbuf_try_pull(&b
, 2)) {
3527 return OFPERR_OFPBRC_BAD_LEN
;
3530 pin
->reason
= npi
->reason
;
3531 pin
->table_id
= npi
->table_id
;
3532 pin
->cookie
= npi
->cookie
;
3534 buffer_id
= ntohl(npi
->buffer_id
);
3535 total_len
= ntohs(npi
->total_len
);
3537 pin
->packet
= b
.data
;
3538 pin
->packet_len
= b
.size
;
3539 } else if (raw
== OFPRAW_NXT_PACKET_IN2
|| raw
== OFPRAW_NXT_RESUME
) {
3540 enum ofperr error
= decode_nx_packet_in2(oh
, loose
, pin
, &total_len
,
3541 &buffer_id
, continuation
);
3550 *total_lenp
= total_len
;
3553 *buffer_idp
= buffer_id
;
3560 encode_packet_in_reason(enum ofp_packet_in_reason reason
,
3561 enum ofp_version version
)
3566 case OFPR_INVALID_TTL
:
3569 case OFPR_ACTION_SET
:
3571 case OFPR_PACKET_OUT
:
3572 return version
< OFP14_VERSION
? OFPR_ACTION
: reason
;
3574 case OFPR_EXPLICIT_MISS
:
3575 return version
< OFP13_VERSION
? OFPR_ACTION
: OFPR_NO_MATCH
;
3577 case OFPR_IMPLICIT_MISS
:
3578 return OFPR_NO_MATCH
;
3580 case OFPR_N_REASONS
:
3586 /* Only NXT_PACKET_IN2 (not NXT_RESUME) should include NXCPT_USERDATA, so this
3587 * function omits it. The caller can add it itself if desired. */
3589 ofputil_put_packet_in(const struct ofputil_packet_in
*pin
,
3590 enum ofp_version version
, size_t include_bytes
,
3593 /* Add packet properties. */
3594 ofpprop_put(msg
, NXPINT_PACKET
, pin
->packet
, include_bytes
);
3595 if (include_bytes
!= pin
->packet_len
) {
3596 ofpprop_put_u32(msg
, NXPINT_FULL_LEN
, pin
->packet_len
);
3599 /* Add flow properties. */
3600 ofpprop_put_u8(msg
, NXPINT_TABLE_ID
, pin
->table_id
);
3601 if (pin
->cookie
!= OVS_BE64_MAX
) {
3602 ofpprop_put_be64(msg
, NXPINT_COOKIE
, pin
->cookie
);
3605 /* Add other properties. */
3606 ofpprop_put_u8(msg
, NXPINT_REASON
,
3607 encode_packet_in_reason(pin
->reason
, version
));
3609 size_t start
= ofpprop_start(msg
, NXPINT_METADATA
);
3610 oxm_put_raw(msg
, &pin
->flow_metadata
, version
);
3611 ofpprop_end(msg
, start
);
3615 put_actions_property(struct ofpbuf
*msg
, uint64_t prop_type
,
3616 enum ofp_version version
,
3617 const struct ofpact
*actions
, size_t actions_len
)
3620 size_t start
= ofpprop_start_nested(msg
, prop_type
);
3621 ofpacts_put_openflow_actions(actions
, actions_len
, msg
, version
);
3622 ofpprop_end(msg
, start
);
3626 enum nx_continuation_prop_type
{
3627 NXCPT_BRIDGE
= 0x8000,
3637 /* Only NXT_PACKET_IN2 (not NXT_RESUME) should include NXCPT_USERDATA, so this
3638 * function omits it. The caller can add it itself if desired. */
3640 ofputil_put_packet_in_private(const struct ofputil_packet_in_private
*pin
,
3641 enum ofp_version version
, size_t include_bytes
,
3644 ofputil_put_packet_in(&pin
->public, version
, include_bytes
, msg
);
3646 size_t continuation_ofs
= ofpprop_start_nested(msg
, NXPINT_CONTINUATION
);
3647 size_t inner_ofs
= msg
->size
;
3649 if (!uuid_is_zero(&pin
->bridge
)) {
3650 ofpprop_put_uuid(msg
, NXCPT_BRIDGE
, &pin
->bridge
);
3653 for (size_t i
= 0; i
< pin
->n_stack
; i
++) {
3654 const union mf_subvalue
*s
= &pin
->stack
[i
];
3656 for (ofs
= 0; ofs
< sizeof *s
; ofs
++) {
3662 ofpprop_put(msg
, NXCPT_STACK
, &s
->u8
[ofs
], sizeof *s
- ofs
);
3666 ofpprop_put_u32(msg
, NXCPT_MIRRORS
, pin
->mirrors
);
3669 if (pin
->conntracked
) {
3670 ofpprop_put_flag(msg
, NXCPT_CONNTRACKED
);
3673 if (pin
->actions_len
) {
3674 /* Divide 'pin->actions' into groups that begins with an
3675 * unroll_xlate action. For each group, emit a NXCPT_TABLE_ID and
3676 * NXCPT_COOKIE property (if either has changed; each is initially
3677 * assumed 0), then a NXCPT_ACTIONS property with the grouped
3680 * The alternative is to make OFPACT_UNROLL_XLATE public. We can
3681 * always do that later, since this is a private property. */
3682 const struct ofpact
*const end
= ofpact_end(pin
->actions
,
3684 const struct ofpact_unroll_xlate
*unroll
= NULL
;
3685 uint8_t table_id
= 0;
3686 ovs_be64 cookie
= 0;
3688 const struct ofpact
*a
;
3689 for (a
= pin
->actions
; ; a
= ofpact_next(a
)) {
3690 if (a
== end
|| a
->type
== OFPACT_UNROLL_XLATE
) {
3692 if (table_id
!= unroll
->rule_table_id
) {
3693 ofpprop_put_u8(msg
, NXCPT_TABLE_ID
,
3694 unroll
->rule_table_id
);
3695 table_id
= unroll
->rule_table_id
;
3697 if (cookie
!= unroll
->rule_cookie
) {
3698 ofpprop_put_be64(msg
, NXCPT_COOKIE
,
3699 unroll
->rule_cookie
);
3700 cookie
= unroll
->rule_cookie
;
3704 const struct ofpact
*start
3705 = unroll
? ofpact_next(&unroll
->ofpact
) : pin
->actions
;
3706 put_actions_property(msg
, NXCPT_ACTIONS
, version
,
3707 start
, (a
- start
) * sizeof *a
);
3712 unroll
= ofpact_get_UNROLL_XLATE(a
);
3717 if (pin
->action_set_len
) {
3718 size_t start
= ofpprop_start_nested(msg
, NXCPT_ACTION_SET
);
3719 ofpacts_put_openflow_actions(pin
->action_set
,
3720 pin
->action_set_len
, msg
, version
);
3721 ofpprop_end(msg
, start
);
3724 if (msg
->size
> inner_ofs
) {
3725 ofpprop_end(msg
, continuation_ofs
);
3727 msg
->size
= continuation_ofs
;
3731 static struct ofpbuf
*
3732 ofputil_encode_ofp10_packet_in(const struct ofputil_packet_in
*pin
)
3734 struct ofp10_packet_in
*opi
;
3737 msg
= ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN
, OFP10_VERSION
,
3738 htonl(0), pin
->packet_len
);
3739 opi
= ofpbuf_put_zeros(msg
, offsetof(struct ofp10_packet_in
, data
));
3740 opi
->total_len
= htons(pin
->packet_len
);
3741 opi
->in_port
= htons(ofp_to_u16(pin
->flow_metadata
.flow
.in_port
.ofp_port
));
3742 opi
->reason
= encode_packet_in_reason(pin
->reason
, OFP10_VERSION
);
3743 opi
->buffer_id
= htonl(UINT32_MAX
);
3748 static struct ofpbuf
*
3749 ofputil_encode_nx_packet_in(const struct ofputil_packet_in
*pin
,
3750 enum ofp_version version
)
3752 struct nx_packet_in
*npi
;
3756 /* The final argument is just an estimate of the space required. */
3757 msg
= ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN
, version
,
3758 htonl(0), NXM_TYPICAL_LEN
+ 2 + pin
->packet_len
);
3759 ofpbuf_put_zeros(msg
, sizeof *npi
);
3760 match_len
= nx_put_match(msg
, &pin
->flow_metadata
, 0, 0);
3761 ofpbuf_put_zeros(msg
, 2);
3764 npi
->buffer_id
= htonl(UINT32_MAX
);
3765 npi
->total_len
= htons(pin
->packet_len
);
3766 npi
->reason
= encode_packet_in_reason(pin
->reason
, version
);
3767 npi
->table_id
= pin
->table_id
;
3768 npi
->cookie
= pin
->cookie
;
3769 npi
->match_len
= htons(match_len
);
3774 static struct ofpbuf
*
3775 ofputil_encode_nx_packet_in2(const struct ofputil_packet_in_private
*pin
,
3776 enum ofp_version version
, size_t include_bytes
)
3778 /* 'extra' is just an estimate of the space required. */
3779 size_t extra
= (pin
->public.packet_len
3780 + NXM_TYPICAL_LEN
/* flow_metadata */
3783 + pin
->action_set_len
3784 + 256); /* fudge factor */
3785 struct ofpbuf
*msg
= ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN2
, version
,
3788 ofputil_put_packet_in_private(pin
, version
, include_bytes
, msg
);
3789 if (pin
->public.userdata_len
) {
3790 ofpprop_put(msg
, NXPINT_USERDATA
, pin
->public.userdata
,
3791 pin
->public.userdata_len
);
3794 ofpmsg_update_length(msg
);
3798 static struct ofpbuf
*
3799 ofputil_encode_ofp11_packet_in(const struct ofputil_packet_in
*pin
)
3801 struct ofp11_packet_in
*opi
;
3804 msg
= ofpraw_alloc_xid(OFPRAW_OFPT11_PACKET_IN
, OFP11_VERSION
,
3805 htonl(0), pin
->packet_len
);
3806 opi
= ofpbuf_put_zeros(msg
, sizeof *opi
);
3807 opi
->buffer_id
= htonl(UINT32_MAX
);
3808 opi
->in_port
= ofputil_port_to_ofp11(
3809 pin
->flow_metadata
.flow
.in_port
.ofp_port
);
3810 opi
->in_phy_port
= opi
->in_port
;
3811 opi
->total_len
= htons(pin
->packet_len
);
3812 opi
->reason
= encode_packet_in_reason(pin
->reason
, OFP11_VERSION
);
3813 opi
->table_id
= pin
->table_id
;
3818 static struct ofpbuf
*
3819 ofputil_encode_ofp12_packet_in(const struct ofputil_packet_in
*pin
,
3820 enum ofp_version version
)
3822 enum ofpraw raw
= (version
>= OFP13_VERSION
3823 ? OFPRAW_OFPT13_PACKET_IN
3824 : OFPRAW_OFPT12_PACKET_IN
);
3827 /* The final argument is just an estimate of the space required. */
3828 msg
= ofpraw_alloc_xid(raw
, version
,
3829 htonl(0), NXM_TYPICAL_LEN
+ 2 + pin
->packet_len
);
3831 struct ofp12_packet_in
*opi
= ofpbuf_put_zeros(msg
, sizeof *opi
);
3832 opi
->buffer_id
= htonl(UINT32_MAX
);
3833 opi
->total_len
= htons(pin
->packet_len
);
3834 opi
->reason
= encode_packet_in_reason(pin
->reason
, version
);
3835 opi
->table_id
= pin
->table_id
;
3837 if (version
>= OFP13_VERSION
) {
3838 ovs_be64 cookie
= pin
->cookie
;
3839 ofpbuf_put(msg
, &cookie
, sizeof cookie
);
3842 oxm_put_match(msg
, &pin
->flow_metadata
, version
);
3843 ofpbuf_put_zeros(msg
, 2);
3848 /* Converts abstract ofputil_packet_in_private 'pin' into a PACKET_IN message
3849 * for 'protocol', using the packet-in format specified by 'packet_in_format'.
3851 * This function is really meant only for use by ovs-vswitchd. To any other
3852 * code, the "continuation" data, i.e. the data that is in struct
3853 * ofputil_packet_in_private but not in struct ofputil_packet_in, is supposed
3854 * to be opaque (and it might change from one OVS version to another). Thus,
3855 * if any other code wants to encode a packet-in, it should use a non-"private"
3856 * version of this function. (Such a version doesn't currently exist because
3857 * only ovs-vswitchd currently wants to encode packet-ins. If you need one,
3860 ofputil_encode_packet_in_private(const struct ofputil_packet_in_private
*pin
,
3861 enum ofputil_protocol protocol
,
3862 enum nx_packet_in_format packet_in_format
)
3864 enum ofp_version version
= ofputil_protocol_to_ofp_version(protocol
);
3867 switch (packet_in_format
) {
3868 case NXPIF_STANDARD
:
3870 case OFPUTIL_P_OF10_STD
:
3871 case OFPUTIL_P_OF10_STD_TID
:
3872 case OFPUTIL_P_OF10_NXM
:
3873 case OFPUTIL_P_OF10_NXM_TID
:
3874 msg
= ofputil_encode_ofp10_packet_in(&pin
->public);
3877 case OFPUTIL_P_OF11_STD
:
3878 msg
= ofputil_encode_ofp11_packet_in(&pin
->public);
3881 case OFPUTIL_P_OF12_OXM
:
3882 case OFPUTIL_P_OF13_OXM
:
3883 case OFPUTIL_P_OF14_OXM
:
3884 case OFPUTIL_P_OF15_OXM
:
3885 case OFPUTIL_P_OF16_OXM
:
3886 msg
= ofputil_encode_ofp12_packet_in(&pin
->public, version
);
3894 case NXPIF_NXT_PACKET_IN
:
3895 msg
= ofputil_encode_nx_packet_in(&pin
->public, version
);
3898 case NXPIF_NXT_PACKET_IN2
:
3899 return ofputil_encode_nx_packet_in2(pin
, version
,
3900 pin
->public.packet_len
);
3906 ofpbuf_put(msg
, pin
->public.packet
, pin
->public.packet_len
);
3907 ofpmsg_update_length(msg
);
3911 /* Returns a string form of 'reason'. The return value is either a statically
3912 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
3913 * 'bufsize' should be at least OFPUTIL_PACKET_IN_REASON_BUFSIZE. */
3915 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason
,
3916 char *reasonbuf
, size_t bufsize
)
3923 case OFPR_INVALID_TTL
:
3924 return "invalid_ttl";
3925 case OFPR_ACTION_SET
:
3926 return "action_set";
3929 case OFPR_PACKET_OUT
:
3930 return "packet_out";
3931 case OFPR_EXPLICIT_MISS
:
3932 case OFPR_IMPLICIT_MISS
:
3935 case OFPR_N_REASONS
:
3937 snprintf(reasonbuf
, bufsize
, "%d", (int) reason
);
3943 ofputil_packet_in_reason_from_string(const char *s
,
3944 enum ofp_packet_in_reason
*reason
)
3948 for (i
= 0; i
< OFPR_N_REASONS
; i
++) {
3949 char reasonbuf
[OFPUTIL_PACKET_IN_REASON_BUFSIZE
];
3950 const char *reason_s
;
3952 reason_s
= ofputil_packet_in_reason_to_string(i
, reasonbuf
,
3954 if (!strcasecmp(s
, reason_s
)) {
3962 /* Returns a newly allocated NXT_RESUME message for 'pin', with the given
3963 * 'continuation', for 'protocol'. This message is suitable for resuming the
3964 * pipeline traveral of the packet represented by 'pin', if sent to the switch
3965 * from which 'pin' was received. */
3967 ofputil_encode_resume(const struct ofputil_packet_in
*pin
,
3968 const struct ofpbuf
*continuation
,
3969 enum ofputil_protocol protocol
)
3971 enum ofp_version version
= ofputil_protocol_to_ofp_version(protocol
);
3972 size_t extra
= pin
->packet_len
+ NXM_TYPICAL_LEN
+ continuation
->size
;
3973 struct ofpbuf
*msg
= ofpraw_alloc_xid(OFPRAW_NXT_RESUME
, version
,
3975 ofputil_put_packet_in(pin
, version
, pin
->packet_len
, msg
);
3976 ofpprop_put_nested(msg
, NXPINT_CONTINUATION
, continuation
);
3977 ofpmsg_update_length(msg
);
3982 parse_subvalue_prop(const struct ofpbuf
*property
, union mf_subvalue
*sv
)
3984 unsigned int len
= ofpbuf_msgsize(property
);
3985 if (len
> sizeof *sv
) {
3986 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXCPT_STACK property has bad length %u",
3988 return OFPERR_OFPBPC_BAD_LEN
;
3990 memset(sv
, 0, sizeof *sv
);
3991 memcpy(&sv
->u8
[sizeof *sv
- len
], property
->msg
, len
);
3996 parse_actions_property(struct ofpbuf
*property
, enum ofp_version version
,
3997 struct ofpbuf
*ofpacts
)
3999 if (!ofpbuf_try_pull(property
, ROUND_UP(ofpbuf_headersize(property
), 8))) {
4000 VLOG_WARN_RL(&bad_ofmsg_rl
, "actions property has bad length %"PRIu32
,
4002 return OFPERR_OFPBPC_BAD_LEN
;
4005 return ofpacts_pull_openflow_actions(property
, property
->size
,
4009 /* This is like ofputil_decode_packet_in(), except that it decodes the
4010 * continuation data into 'pin'. The format of this data is supposed to be
4011 * opaque to any process other than ovs-vswitchd, so this function should not
4012 * be used outside ovs-vswitchd.
4014 * When successful, 'pin' contains some dynamically allocated data. Call
4015 * ofputil_packet_in_private_destroy() to free this data. */
4017 ofputil_decode_packet_in_private(const struct ofp_header
*oh
, bool loose
,
4018 struct ofputil_packet_in_private
*pin
,
4019 size_t *total_len
, uint32_t *buffer_id
)
4021 memset(pin
, 0, sizeof *pin
);
4023 struct ofpbuf continuation
;
4025 error
= ofputil_decode_packet_in(oh
, loose
, &pin
->public, total_len
,
4026 buffer_id
, &continuation
);
4031 struct ofpbuf actions
, action_set
;
4032 ofpbuf_init(&actions
, 0);
4033 ofpbuf_init(&action_set
, 0);
4035 uint8_t table_id
= 0;
4036 ovs_be64 cookie
= 0;
4038 size_t allocated_stack
= 0;
4040 while (continuation
.size
> 0) {
4041 struct ofpbuf payload
;
4044 error
= ofpprop_pull(&continuation
, &payload
, &type
);
4049 error
= ofpprop_parse_uuid(&payload
, &pin
->bridge
);
4053 if (pin
->n_stack
>= allocated_stack
) {
4054 pin
->stack
= x2nrealloc(pin
->stack
, &allocated_stack
,
4055 sizeof *pin
->stack
);
4057 error
= parse_subvalue_prop(&payload
,
4058 &pin
->stack
[pin
->n_stack
++]);
4062 error
= ofpprop_parse_u32(&payload
, &pin
->mirrors
);
4065 case NXCPT_CONNTRACKED
:
4066 pin
->conntracked
= true;
4069 case NXCPT_TABLE_ID
:
4070 error
= ofpprop_parse_u8(&payload
, &table_id
);
4074 error
= ofpprop_parse_be64(&payload
, &cookie
);
4077 case NXCPT_ACTIONS
: {
4078 struct ofpact_unroll_xlate
*unroll
4079 = ofpact_put_UNROLL_XLATE(&actions
);
4080 unroll
->rule_table_id
= table_id
;
4081 unroll
->rule_cookie
= cookie
;
4082 error
= parse_actions_property(&payload
, oh
->version
, &actions
);
4086 case NXCPT_ACTION_SET
:
4087 error
= parse_actions_property(&payload
, oh
->version
, &action_set
);
4091 error
= OFPPROP_UNKNOWN(loose
, "continuation", type
);
4099 pin
->actions_len
= actions
.size
;
4100 pin
->actions
= ofpbuf_steal_data(&actions
);
4101 pin
->action_set_len
= action_set
.size
;
4102 pin
->action_set
= ofpbuf_steal_data(&action_set
);
4105 ofputil_packet_in_private_destroy(pin
);
4111 /* Frees data in 'pin' that is dynamically allocated by
4112 * ofputil_decode_packet_in_private().
4114 * 'pin->public' contains some pointer members that
4115 * ofputil_decode_packet_in_private() doesn't initialize to newly allocated
4116 * data, so this function doesn't free those. */
4118 ofputil_packet_in_private_destroy(struct ofputil_packet_in_private
*pin
)
4123 free(pin
->action_set
);
4127 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
4130 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
4131 * message's actions. The caller must initialize 'ofpacts' and retains
4132 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
4134 * 'po->packet' refers to the packet data in 'oh', so the buffer containing
4135 * 'oh' must not be destroyed while 'po' is being used.
4137 * Returns 0 if successful, otherwise an OFPERR_* value. */
4139 ofputil_decode_packet_out(struct ofputil_packet_out
*po
,
4140 const struct ofp_header
*oh
,
4141 struct ofpbuf
*ofpacts
)
4143 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
4144 enum ofpraw raw
= ofpraw_pull_assert(&b
);
4146 ofpbuf_clear(ofpacts
);
4147 if (raw
== OFPRAW_OFPT11_PACKET_OUT
) {
4149 const struct ofp11_packet_out
*opo
= ofpbuf_pull(&b
, sizeof *opo
);
4151 po
->buffer_id
= ntohl(opo
->buffer_id
);
4152 error
= ofputil_port_from_ofp11(opo
->in_port
, &po
->in_port
);
4157 error
= ofpacts_pull_openflow_actions(&b
, ntohs(opo
->actions_len
),
4158 oh
->version
, ofpacts
);
4162 } else if (raw
== OFPRAW_OFPT10_PACKET_OUT
) {
4164 const struct ofp10_packet_out
*opo
= ofpbuf_pull(&b
, sizeof *opo
);
4166 po
->buffer_id
= ntohl(opo
->buffer_id
);
4167 po
->in_port
= u16_to_ofp(ntohs(opo
->in_port
));
4169 error
= ofpacts_pull_openflow_actions(&b
, ntohs(opo
->actions_len
),
4170 oh
->version
, ofpacts
);
4178 if (ofp_to_u16(po
->in_port
) >= ofp_to_u16(OFPP_MAX
)
4179 && po
->in_port
!= OFPP_LOCAL
4180 && po
->in_port
!= OFPP_NONE
&& po
->in_port
!= OFPP_CONTROLLER
) {
4181 VLOG_WARN_RL(&bad_ofmsg_rl
, "packet-out has bad input port %#"PRIx16
,
4183 return OFPERR_OFPBRC_BAD_PORT
;
4186 po
->ofpacts
= ofpacts
->data
;
4187 po
->ofpacts_len
= ofpacts
->size
;
4189 if (po
->buffer_id
== UINT32_MAX
) {
4190 po
->packet
= b
.data
;
4191 po
->packet_len
= b
.size
;
4200 /* ofputil_phy_port */
4202 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
4203 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD
== OFPPF_10MB_HD
); /* bit 0 */
4204 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD
== OFPPF_10MB_FD
); /* bit 1 */
4205 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD
== OFPPF_100MB_HD
); /* bit 2 */
4206 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD
== OFPPF_100MB_FD
); /* bit 3 */
4207 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD
== OFPPF_1GB_HD
); /* bit 4 */
4208 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD
== OFPPF_1GB_FD
); /* bit 5 */
4209 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD
== OFPPF_10GB_FD
); /* bit 6 */
4211 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
4212 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER
== (OFPPF10_COPPER
<< 4));
4213 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER
== (OFPPF10_FIBER
<< 4));
4214 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG
== (OFPPF10_AUTONEG
<< 4));
4215 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE
== (OFPPF10_PAUSE
<< 4));
4216 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM
== (OFPPF10_PAUSE_ASYM
<< 4));
4218 static enum netdev_features
4219 netdev_port_features_from_ofp10(ovs_be32 ofp10_
)
4221 uint32_t ofp10
= ntohl(ofp10_
);
4222 return (ofp10
& 0x7f) | ((ofp10
& 0xf80) << 4);
4226 netdev_port_features_to_ofp10(enum netdev_features features
)
4228 return htonl((features
& 0x7f) | ((features
& 0xf800) >> 4));
4231 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD
== OFPPF_10MB_HD
); /* bit 0 */
4232 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD
== OFPPF_10MB_FD
); /* bit 1 */
4233 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD
== OFPPF_100MB_HD
); /* bit 2 */
4234 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD
== OFPPF_100MB_FD
); /* bit 3 */
4235 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD
== OFPPF_1GB_HD
); /* bit 4 */
4236 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD
== OFPPF_1GB_FD
); /* bit 5 */
4237 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD
== OFPPF_10GB_FD
); /* bit 6 */
4238 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD
== OFPPF11_40GB_FD
); /* bit 7 */
4239 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD
== OFPPF11_100GB_FD
); /* bit 8 */
4240 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD
== OFPPF11_1TB_FD
); /* bit 9 */
4241 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER
== OFPPF11_OTHER
); /* bit 10 */
4242 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER
== OFPPF11_COPPER
); /* bit 11 */
4243 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER
== OFPPF11_FIBER
); /* bit 12 */
4244 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG
== OFPPF11_AUTONEG
); /* bit 13 */
4245 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE
== OFPPF11_PAUSE
); /* bit 14 */
4246 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM
== OFPPF11_PAUSE_ASYM
);/* bit 15 */
4248 static enum netdev_features
4249 netdev_port_features_from_ofp11(ovs_be32 ofp11
)
4251 return ntohl(ofp11
) & 0xffff;
4255 netdev_port_features_to_ofp11(enum netdev_features features
)
4257 return htonl(features
& 0xffff);
4261 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port
*pp
,
4262 const struct ofp10_phy_port
*opp
)
4264 pp
->port_no
= u16_to_ofp(ntohs(opp
->port_no
));
4265 pp
->hw_addr
= opp
->hw_addr
;
4266 ovs_strlcpy(pp
->name
, opp
->name
, OFP_MAX_PORT_NAME_LEN
);
4268 pp
->config
= ntohl(opp
->config
) & OFPPC10_ALL
;
4269 pp
->state
= ntohl(opp
->state
) & OFPPS10_ALL
;
4271 pp
->curr
= netdev_port_features_from_ofp10(opp
->curr
);
4272 pp
->advertised
= netdev_port_features_from_ofp10(opp
->advertised
);
4273 pp
->supported
= netdev_port_features_from_ofp10(opp
->supported
);
4274 pp
->peer
= netdev_port_features_from_ofp10(opp
->peer
);
4276 pp
->curr_speed
= netdev_features_to_bps(pp
->curr
, 0) / 1000;
4277 pp
->max_speed
= netdev_features_to_bps(pp
->supported
, 0) / 1000;
4283 ofputil_decode_ofp11_port(struct ofputil_phy_port
*pp
,
4284 const struct ofp11_port
*op
)
4288 error
= ofputil_port_from_ofp11(op
->port_no
, &pp
->port_no
);
4292 pp
->hw_addr
= op
->hw_addr
;
4293 ovs_strlcpy(pp
->name
, op
->name
, OFP_MAX_PORT_NAME_LEN
);
4295 pp
->config
= ntohl(op
->config
) & OFPPC11_ALL
;
4296 pp
->state
= ntohl(op
->state
) & OFPPS11_ALL
;
4298 pp
->curr
= netdev_port_features_from_ofp11(op
->curr
);
4299 pp
->advertised
= netdev_port_features_from_ofp11(op
->advertised
);
4300 pp
->supported
= netdev_port_features_from_ofp11(op
->supported
);
4301 pp
->peer
= netdev_port_features_from_ofp11(op
->peer
);
4303 pp
->curr_speed
= ntohl(op
->curr_speed
);
4304 pp
->max_speed
= ntohl(op
->max_speed
);
4310 parse_ofp14_port_ethernet_property(const struct ofpbuf
*payload
,
4311 struct ofputil_phy_port
*pp
)
4313 struct ofp14_port_desc_prop_ethernet
*eth
= payload
->data
;
4315 if (payload
->size
!= sizeof *eth
) {
4316 return OFPERR_OFPBPC_BAD_LEN
;
4319 pp
->curr
= netdev_port_features_from_ofp11(eth
->curr
);
4320 pp
->advertised
= netdev_port_features_from_ofp11(eth
->advertised
);
4321 pp
->supported
= netdev_port_features_from_ofp11(eth
->supported
);
4322 pp
->peer
= netdev_port_features_from_ofp11(eth
->peer
);
4324 pp
->curr_speed
= ntohl(eth
->curr_speed
);
4325 pp
->max_speed
= ntohl(eth
->max_speed
);
4331 ofputil_pull_ofp14_port(struct ofputil_phy_port
*pp
, struct ofpbuf
*msg
)
4333 struct ofp14_port
*op
= ofpbuf_try_pull(msg
, sizeof *op
);
4335 return OFPERR_OFPBRC_BAD_LEN
;
4338 size_t len
= ntohs(op
->length
);
4339 if (len
< sizeof *op
|| len
- sizeof *op
> msg
->size
) {
4340 return OFPERR_OFPBRC_BAD_LEN
;
4344 enum ofperr error
= ofputil_port_from_ofp11(op
->port_no
, &pp
->port_no
);
4348 pp
->hw_addr
= op
->hw_addr
;
4349 ovs_strlcpy(pp
->name
, op
->name
, OFP_MAX_PORT_NAME_LEN
);
4351 pp
->config
= ntohl(op
->config
) & OFPPC11_ALL
;
4352 pp
->state
= ntohl(op
->state
) & OFPPS11_ALL
;
4354 struct ofpbuf properties
= ofpbuf_const_initializer(ofpbuf_pull(msg
, len
),
4356 while (properties
.size
> 0) {
4357 struct ofpbuf payload
;
4361 error
= ofpprop_pull(&properties
, &payload
, &type
);
4367 case OFPPDPT14_ETHERNET
:
4368 error
= parse_ofp14_port_ethernet_property(&payload
, pp
);
4372 error
= OFPPROP_UNKNOWN(true, "port", type
);
4385 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port
*pp
,
4386 struct ofp10_phy_port
*opp
)
4388 memset(opp
, 0, sizeof *opp
);
4390 opp
->port_no
= htons(ofp_to_u16(pp
->port_no
));
4391 opp
->hw_addr
= pp
->hw_addr
;
4392 ovs_strlcpy(opp
->name
, pp
->name
, OFP_MAX_PORT_NAME_LEN
);
4394 opp
->config
= htonl(pp
->config
& OFPPC10_ALL
);
4395 opp
->state
= htonl(pp
->state
& OFPPS10_ALL
);
4397 opp
->curr
= netdev_port_features_to_ofp10(pp
->curr
);
4398 opp
->advertised
= netdev_port_features_to_ofp10(pp
->advertised
);
4399 opp
->supported
= netdev_port_features_to_ofp10(pp
->supported
);
4400 opp
->peer
= netdev_port_features_to_ofp10(pp
->peer
);
4404 ofputil_encode_ofp11_port(const struct ofputil_phy_port
*pp
,
4405 struct ofp11_port
*op
)
4407 memset(op
, 0, sizeof *op
);
4409 op
->port_no
= ofputil_port_to_ofp11(pp
->port_no
);
4410 op
->hw_addr
= pp
->hw_addr
;
4411 ovs_strlcpy(op
->name
, pp
->name
, OFP_MAX_PORT_NAME_LEN
);
4413 op
->config
= htonl(pp
->config
& OFPPC11_ALL
);
4414 op
->state
= htonl(pp
->state
& OFPPS11_ALL
);
4416 op
->curr
= netdev_port_features_to_ofp11(pp
->curr
);
4417 op
->advertised
= netdev_port_features_to_ofp11(pp
->advertised
);
4418 op
->supported
= netdev_port_features_to_ofp11(pp
->supported
);
4419 op
->peer
= netdev_port_features_to_ofp11(pp
->peer
);
4421 op
->curr_speed
= htonl(pp
->curr_speed
);
4422 op
->max_speed
= htonl(pp
->max_speed
);
4426 ofputil_put_ofp14_port(const struct ofputil_phy_port
*pp
,
4429 struct ofp14_port
*op
;
4430 struct ofp14_port_desc_prop_ethernet
*eth
;
4432 ofpbuf_prealloc_tailroom(b
, sizeof *op
+ sizeof *eth
);
4434 op
= ofpbuf_put_zeros(b
, sizeof *op
);
4435 op
->port_no
= ofputil_port_to_ofp11(pp
->port_no
);
4436 op
->length
= htons(sizeof *op
+ sizeof *eth
);
4437 op
->hw_addr
= pp
->hw_addr
;
4438 ovs_strlcpy(op
->name
, pp
->name
, sizeof op
->name
);
4439 op
->config
= htonl(pp
->config
& OFPPC11_ALL
);
4440 op
->state
= htonl(pp
->state
& OFPPS11_ALL
);
4442 eth
= ofpprop_put_zeros(b
, OFPPDPT14_ETHERNET
, sizeof *eth
);
4443 eth
->curr
= netdev_port_features_to_ofp11(pp
->curr
);
4444 eth
->advertised
= netdev_port_features_to_ofp11(pp
->advertised
);
4445 eth
->supported
= netdev_port_features_to_ofp11(pp
->supported
);
4446 eth
->peer
= netdev_port_features_to_ofp11(pp
->peer
);
4447 eth
->curr_speed
= htonl(pp
->curr_speed
);
4448 eth
->max_speed
= htonl(pp
->max_speed
);
4452 ofputil_put_phy_port(enum ofp_version ofp_version
,
4453 const struct ofputil_phy_port
*pp
, struct ofpbuf
*b
)
4455 switch (ofp_version
) {
4456 case OFP10_VERSION
: {
4457 struct ofp10_phy_port
*opp
= ofpbuf_put_uninit(b
, sizeof *opp
);
4458 ofputil_encode_ofp10_phy_port(pp
, opp
);
4464 case OFP13_VERSION
: {
4465 struct ofp11_port
*op
= ofpbuf_put_uninit(b
, sizeof *op
);
4466 ofputil_encode_ofp11_port(pp
, op
);
4473 ofputil_put_ofp14_port(pp
, b
);
4482 ofputil_decode_port_desc_stats_request(const struct ofp_header
*request
,
4485 struct ofpbuf b
= ofpbuf_const_initializer(request
,
4486 ntohs(request
->length
));
4487 enum ofpraw raw
= ofpraw_pull_assert(&b
);
4488 if (raw
== OFPRAW_OFPST10_PORT_DESC_REQUEST
) {
4491 } else if (raw
== OFPRAW_OFPST15_PORT_DESC_REQUEST
) {
4492 ovs_be32
*ofp11_port
;
4494 ofp11_port
= ofpbuf_pull(&b
, sizeof *ofp11_port
);
4495 return ofputil_port_from_ofp11(*ofp11_port
, port
);
4502 ofputil_encode_port_desc_stats_request(enum ofp_version ofp_version
,
4505 struct ofpbuf
*request
;
4507 switch (ofp_version
) {
4513 request
= ofpraw_alloc(OFPRAW_OFPST10_PORT_DESC_REQUEST
,
4517 case OFP16_VERSION
:{
4518 struct ofp15_port_desc_request
*req
;
4519 request
= ofpraw_alloc(OFPRAW_OFPST15_PORT_DESC_REQUEST
,
4521 req
= ofpbuf_put_zeros(request
, sizeof *req
);
4522 req
->port_no
= ofputil_port_to_ofp11(port
);
4533 ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port
*pp
,
4534 struct ovs_list
*replies
)
4536 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
4537 size_t start_ofs
= reply
->size
;
4539 ofputil_put_phy_port(ofpmp_version(replies
), pp
, reply
);
4540 ofpmp_postappend(replies
, start_ofs
);
4543 /* ofputil_switch_config */
4545 /* Decodes 'oh', which must be an OFPT_GET_CONFIG_REPLY or OFPT_SET_CONFIG
4546 * message, into 'config'. Returns false if 'oh' contained any flags that
4547 * aren't specified in its version of OpenFlow, true otherwise. */
4549 ofputil_decode_switch_config(const struct ofp_header
*oh
,
4550 struct ofputil_switch_config
*config
)
4552 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
4553 ofpraw_pull_assert(&b
);
4555 const struct ofp_switch_config
*osc
= ofpbuf_pull(&b
, sizeof *osc
);
4556 config
->frag
= ntohs(osc
->flags
) & OFPC_FRAG_MASK
;
4557 config
->miss_send_len
= ntohs(osc
->miss_send_len
);
4559 ovs_be16 valid_mask
= htons(OFPC_FRAG_MASK
);
4560 if (oh
->version
< OFP13_VERSION
) {
4561 const ovs_be16 ttl_bit
= htons(OFPC_INVALID_TTL_TO_CONTROLLER
);
4562 valid_mask
|= ttl_bit
;
4563 config
->invalid_ttl_to_controller
= (osc
->flags
& ttl_bit
) != 0;
4565 config
->invalid_ttl_to_controller
= -1;
4568 return !(osc
->flags
& ~valid_mask
);
4572 ofputil_decode_get_config_reply(const struct ofp_header
*oh
,
4573 struct ofputil_switch_config
*config
)
4575 ofputil_decode_switch_config(oh
, config
);
4579 ofputil_decode_set_config(const struct ofp_header
*oh
,
4580 struct ofputil_switch_config
*config
)
4582 return (ofputil_decode_switch_config(oh
, config
)
4584 : OFPERR_OFPSCFC_BAD_FLAGS
);
4587 static struct ofpbuf
*
4588 ofputil_put_switch_config(const struct ofputil_switch_config
*config
,
4591 const struct ofp_header
*oh
= b
->data
;
4592 struct ofp_switch_config
*osc
= ofpbuf_put_zeros(b
, sizeof *osc
);
4593 osc
->flags
= htons(config
->frag
);
4594 if (config
->invalid_ttl_to_controller
> 0 && oh
->version
< OFP13_VERSION
) {
4595 osc
->flags
|= htons(OFPC_INVALID_TTL_TO_CONTROLLER
);
4597 osc
->miss_send_len
= htons(config
->miss_send_len
);
4602 ofputil_encode_get_config_reply(const struct ofp_header
*request
,
4603 const struct ofputil_switch_config
*config
)
4605 struct ofpbuf
*b
= ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY
,
4607 return ofputil_put_switch_config(config
, b
);
4611 ofputil_encode_set_config(const struct ofputil_switch_config
*config
,
4612 enum ofp_version version
)
4614 struct ofpbuf
*b
= ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG
, version
, 0);
4615 return ofputil_put_switch_config(config
, b
);
4618 /* ofputil_switch_features */
4620 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
4621 OFPC_IP_REASM | OFPC_QUEUE_STATS)
4622 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS
== OFPC_FLOW_STATS
);
4623 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS
== OFPC_TABLE_STATS
);
4624 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS
== OFPC_PORT_STATS
);
4625 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM
== OFPC_IP_REASM
);
4626 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS
== OFPC_QUEUE_STATS
);
4627 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP
== OFPC_ARP_MATCH_IP
);
4630 ofputil_capabilities_mask(enum ofp_version ofp_version
)
4632 /* Handle capabilities whose bit is unique for all OpenFlow versions */
4633 switch (ofp_version
) {
4636 return OFPC_COMMON
| OFPC_ARP_MATCH_IP
;
4642 return OFPC_COMMON
| OFPC12_PORT_BLOCKED
;
4644 /* Caller needs to check osf->header.version itself */
4649 /* Pulls an OpenFlow "switch_features" structure from 'b' and decodes it into
4650 * an abstract representation in '*features', readying 'b' to iterate over the
4651 * OpenFlow port structures following 'osf' with later calls to
4652 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an OFPERR_*
4655 ofputil_pull_switch_features(struct ofpbuf
*b
,
4656 struct ofputil_switch_features
*features
)
4658 const struct ofp_header
*oh
= b
->data
;
4659 enum ofpraw raw
= ofpraw_pull_assert(b
);
4660 const struct ofp_switch_features
*osf
= ofpbuf_pull(b
, sizeof *osf
);
4661 features
->datapath_id
= ntohll(osf
->datapath_id
);
4662 features
->n_buffers
= ntohl(osf
->n_buffers
);
4663 features
->n_tables
= osf
->n_tables
;
4664 features
->auxiliary_id
= 0;
4666 features
->capabilities
= ntohl(osf
->capabilities
) &
4667 ofputil_capabilities_mask(oh
->version
);
4669 if (raw
== OFPRAW_OFPT10_FEATURES_REPLY
) {
4670 if (osf
->capabilities
& htonl(OFPC10_STP
)) {
4671 features
->capabilities
|= OFPUTIL_C_STP
;
4673 features
->ofpacts
= ofpact_bitmap_from_openflow(osf
->actions
,
4675 } else if (raw
== OFPRAW_OFPT11_FEATURES_REPLY
4676 || raw
== OFPRAW_OFPT13_FEATURES_REPLY
) {
4677 if (osf
->capabilities
& htonl(OFPC11_GROUP_STATS
)) {
4678 features
->capabilities
|= OFPUTIL_C_GROUP_STATS
;
4680 features
->ofpacts
= 0;
4681 if (raw
== OFPRAW_OFPT13_FEATURES_REPLY
) {
4682 features
->auxiliary_id
= osf
->auxiliary_id
;
4685 return OFPERR_OFPBRC_BAD_VERSION
;
4691 /* In OpenFlow 1.0, 1.1, and 1.2, an OFPT_FEATURES_REPLY message lists all the
4692 * switch's ports, unless there are too many to fit. In OpenFlow 1.3 and
4693 * later, an OFPT_FEATURES_REPLY does not list ports at all.
4695 * Given a buffer 'b' that contains a Features Reply message, this message
4696 * checks if it contains a complete list of the switch's ports. Returns true,
4697 * if so. Returns false if the list is missing (OF1.3+) or incomplete
4698 * (OF1.0/1.1/1.2), and in the latter case removes all of the ports from the
4701 * When this function returns false, the caller should send an OFPST_PORT_DESC
4702 * stats request to get the ports. */
4704 ofputil_switch_features_has_ports(struct ofpbuf
*b
)
4706 struct ofp_header
*oh
= b
->data
;
4707 size_t phy_port_size
;
4709 if (oh
->version
>= OFP13_VERSION
) {
4710 /* OpenFlow 1.3+ never has ports in the feature reply. */
4714 phy_port_size
= (oh
->version
== OFP10_VERSION
4715 ? sizeof(struct ofp10_phy_port
)
4716 : sizeof(struct ofp11_port
));
4717 if (ntohs(oh
->length
) + phy_port_size
<= UINT16_MAX
) {
4718 /* There's room for additional ports in the feature reply.
4719 * Assume that the list is complete. */
4723 /* The feature reply has no room for more ports. Probably the list is
4724 * truncated. Drop the ports and tell the caller to retrieve them with
4725 * OFPST_PORT_DESC. */
4726 b
->size
= sizeof *oh
+ sizeof(struct ofp_switch_features
);
4727 ofpmsg_update_length(b
);
4731 /* Returns a buffer owned by the caller that encodes 'features' in the format
4732 * required by 'protocol' with the given 'xid'. The caller should append port
4733 * information to the buffer with subsequent calls to
4734 * ofputil_put_switch_features_port(). */
4736 ofputil_encode_switch_features(const struct ofputil_switch_features
*features
,
4737 enum ofputil_protocol protocol
, ovs_be32 xid
)
4739 struct ofp_switch_features
*osf
;
4741 enum ofp_version version
;
4744 version
= ofputil_protocol_to_ofp_version(protocol
);
4747 raw
= OFPRAW_OFPT10_FEATURES_REPLY
;
4751 raw
= OFPRAW_OFPT11_FEATURES_REPLY
;
4757 raw
= OFPRAW_OFPT13_FEATURES_REPLY
;
4762 b
= ofpraw_alloc_xid(raw
, version
, xid
, 0);
4763 osf
= ofpbuf_put_zeros(b
, sizeof *osf
);
4764 osf
->datapath_id
= htonll(features
->datapath_id
);
4765 osf
->n_buffers
= htonl(features
->n_buffers
);
4766 osf
->n_tables
= features
->n_tables
;
4768 osf
->capabilities
= htonl(features
->capabilities
& OFPC_COMMON
);
4769 osf
->capabilities
= htonl(features
->capabilities
&
4770 ofputil_capabilities_mask(version
));
4773 if (features
->capabilities
& OFPUTIL_C_STP
) {
4774 osf
->capabilities
|= htonl(OFPC10_STP
);
4776 osf
->actions
= ofpact_bitmap_to_openflow(features
->ofpacts
,
4783 osf
->auxiliary_id
= features
->auxiliary_id
;
4787 if (features
->capabilities
& OFPUTIL_C_GROUP_STATS
) {
4788 osf
->capabilities
|= htonl(OFPC11_GROUP_STATS
);
4798 /* Encodes 'pp' into the format required by the switch_features message already
4799 * in 'b', which should have been returned by ofputil_encode_switch_features(),
4800 * and appends the encoded version to 'b'. */
4802 ofputil_put_switch_features_port(const struct ofputil_phy_port
*pp
,
4805 const struct ofp_header
*oh
= b
->data
;
4807 if (oh
->version
< OFP13_VERSION
) {
4808 /* Try adding a port description to the message, but drop it again if
4809 * the buffer overflows. (This possibility for overflow is why
4810 * OpenFlow 1.3+ moved port descriptions into a multipart message.) */
4811 size_t start_ofs
= b
->size
;
4812 ofputil_put_phy_port(oh
->version
, pp
, b
);
4813 if (b
->size
> UINT16_MAX
) {
4814 b
->size
= start_ofs
;
4819 /* ofputil_port_status */
4821 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
4822 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
4824 ofputil_decode_port_status(const struct ofp_header
*oh
,
4825 struct ofputil_port_status
*ps
)
4827 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
4828 ofpraw_pull_assert(&b
);
4830 const struct ofp_port_status
*ops
= ofpbuf_pull(&b
, sizeof *ops
);
4831 if (ops
->reason
!= OFPPR_ADD
&&
4832 ops
->reason
!= OFPPR_DELETE
&&
4833 ops
->reason
!= OFPPR_MODIFY
) {
4834 return OFPERR_NXBRC_BAD_REASON
;
4836 ps
->reason
= ops
->reason
;
4838 int retval
= ofputil_pull_phy_port(oh
->version
, &b
, &ps
->desc
);
4839 ovs_assert(retval
!= EOF
);
4843 /* Converts the abstract form of a "port status" message in '*ps' into an
4844 * OpenFlow message suitable for 'protocol', and returns that encoded form in
4845 * a buffer owned by the caller. */
4847 ofputil_encode_port_status(const struct ofputil_port_status
*ps
,
4848 enum ofputil_protocol protocol
)
4850 struct ofp_port_status
*ops
;
4852 enum ofp_version version
;
4855 version
= ofputil_protocol_to_ofp_version(protocol
);
4858 raw
= OFPRAW_OFPT10_PORT_STATUS
;
4864 raw
= OFPRAW_OFPT11_PORT_STATUS
;
4870 raw
= OFPRAW_OFPT14_PORT_STATUS
;
4877 b
= ofpraw_alloc_xid(raw
, version
, htonl(0), 0);
4878 ops
= ofpbuf_put_zeros(b
, sizeof *ops
);
4879 ops
->reason
= ps
->reason
;
4880 ofputil_put_phy_port(version
, &ps
->desc
, b
);
4881 ofpmsg_update_length(b
);
4885 /* ofputil_port_mod */
4888 parse_port_mod_ethernet_property(struct ofpbuf
*property
,
4889 struct ofputil_port_mod
*pm
)
4894 error
= ofpprop_parse_be32(property
, &advertise
);
4896 pm
->advertise
= netdev_port_features_from_ofp11(advertise
);
4901 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
4902 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
4904 ofputil_decode_port_mod(const struct ofp_header
*oh
,
4905 struct ofputil_port_mod
*pm
, bool loose
)
4907 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
4908 enum ofpraw raw
= ofpraw_pull_assert(&b
);
4909 if (raw
== OFPRAW_OFPT10_PORT_MOD
) {
4910 const struct ofp10_port_mod
*opm
= b
.data
;
4912 pm
->port_no
= u16_to_ofp(ntohs(opm
->port_no
));
4913 pm
->hw_addr
= opm
->hw_addr
;
4914 pm
->config
= ntohl(opm
->config
) & OFPPC10_ALL
;
4915 pm
->mask
= ntohl(opm
->mask
) & OFPPC10_ALL
;
4916 pm
->advertise
= netdev_port_features_from_ofp10(opm
->advertise
);
4917 } else if (raw
== OFPRAW_OFPT11_PORT_MOD
) {
4918 const struct ofp11_port_mod
*opm
= b
.data
;
4921 error
= ofputil_port_from_ofp11(opm
->port_no
, &pm
->port_no
);
4926 pm
->hw_addr
= opm
->hw_addr
;
4927 pm
->config
= ntohl(opm
->config
) & OFPPC11_ALL
;
4928 pm
->mask
= ntohl(opm
->mask
) & OFPPC11_ALL
;
4929 pm
->advertise
= netdev_port_features_from_ofp11(opm
->advertise
);
4930 } else if (raw
== OFPRAW_OFPT14_PORT_MOD
) {
4931 const struct ofp14_port_mod
*opm
= ofpbuf_pull(&b
, sizeof *opm
);
4934 memset(pm
, 0, sizeof *pm
);
4936 error
= ofputil_port_from_ofp11(opm
->port_no
, &pm
->port_no
);
4941 pm
->hw_addr
= opm
->hw_addr
;
4942 pm
->config
= ntohl(opm
->config
) & OFPPC11_ALL
;
4943 pm
->mask
= ntohl(opm
->mask
) & OFPPC11_ALL
;
4945 while (b
.size
> 0) {
4946 struct ofpbuf property
;
4950 error
= ofpprop_pull(&b
, &property
, &type
);
4956 case OFPPMPT14_ETHERNET
:
4957 error
= parse_port_mod_ethernet_property(&property
, pm
);
4961 error
= OFPPROP_UNKNOWN(loose
, "port_mod", type
);
4970 return OFPERR_OFPBRC_BAD_TYPE
;
4973 pm
->config
&= pm
->mask
;
4977 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
4978 * message suitable for 'protocol', and returns that encoded form in a buffer
4979 * owned by the caller. */
4981 ofputil_encode_port_mod(const struct ofputil_port_mod
*pm
,
4982 enum ofputil_protocol protocol
)
4984 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
4987 switch (ofp_version
) {
4988 case OFP10_VERSION
: {
4989 struct ofp10_port_mod
*opm
;
4991 b
= ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD
, ofp_version
, 0);
4992 opm
= ofpbuf_put_zeros(b
, sizeof *opm
);
4993 opm
->port_no
= htons(ofp_to_u16(pm
->port_no
));
4994 opm
->hw_addr
= pm
->hw_addr
;
4995 opm
->config
= htonl(pm
->config
& OFPPC10_ALL
);
4996 opm
->mask
= htonl(pm
->mask
& OFPPC10_ALL
);
4997 opm
->advertise
= netdev_port_features_to_ofp10(pm
->advertise
);
5003 case OFP13_VERSION
: {
5004 struct ofp11_port_mod
*opm
;
5006 b
= ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD
, ofp_version
, 0);
5007 opm
= ofpbuf_put_zeros(b
, sizeof *opm
);
5008 opm
->port_no
= ofputil_port_to_ofp11(pm
->port_no
);
5009 opm
->hw_addr
= pm
->hw_addr
;
5010 opm
->config
= htonl(pm
->config
& OFPPC11_ALL
);
5011 opm
->mask
= htonl(pm
->mask
& OFPPC11_ALL
);
5012 opm
->advertise
= netdev_port_features_to_ofp11(pm
->advertise
);
5017 case OFP16_VERSION
: {
5018 struct ofp14_port_mod
*opm
;
5020 b
= ofpraw_alloc(OFPRAW_OFPT14_PORT_MOD
, ofp_version
, 0);
5021 opm
= ofpbuf_put_zeros(b
, sizeof *opm
);
5022 opm
->port_no
= ofputil_port_to_ofp11(pm
->port_no
);
5023 opm
->hw_addr
= pm
->hw_addr
;
5024 opm
->config
= htonl(pm
->config
& OFPPC11_ALL
);
5025 opm
->mask
= htonl(pm
->mask
& OFPPC11_ALL
);
5027 if (pm
->advertise
) {
5028 ofpprop_put_be32(b
, OFPPMPT14_ETHERNET
,
5029 netdev_port_features_to_ofp11(pm
->advertise
));
5040 /* Table features. */
5043 pull_table_feature_property(struct ofpbuf
*msg
, struct ofpbuf
*payload
,
5048 error
= ofpprop_pull(msg
, payload
, typep
);
5049 if (payload
&& !error
) {
5050 ofpbuf_pull(payload
, (char *)payload
->msg
- (char *)payload
->header
);
5056 parse_action_bitmap(struct ofpbuf
*payload
, enum ofp_version ofp_version
,
5061 while (payload
->size
> 0) {
5065 error
= ofpprop_pull__(payload
, NULL
, 1, 0x10000, &type
);
5069 if (type
< CHAR_BIT
* sizeof types
) {
5070 types
|= 1u << type
;
5074 *ofpacts
= ofpact_bitmap_from_openflow(htonl(types
), ofp_version
);
5079 parse_instruction_ids(struct ofpbuf
*payload
, bool loose
, uint32_t *insts
)
5082 while (payload
->size
> 0) {
5083 enum ovs_instruction_type inst
;
5087 /* OF1.3 and OF1.4 aren't clear about padding in the instruction IDs.
5088 * It seems clear that they aren't padded to 8 bytes, though, because
5089 * both standards say that "non-experimenter instructions are 4 bytes"
5090 * and do not mention any padding before the first instruction ID.
5091 * (There wouldn't be any point in padding to 8 bytes if the IDs were
5092 * aligned on an odd 4-byte boundary.)
5094 * Anyway, we just assume they're all glommed together on byte
5096 error
= ofpprop_pull__(payload
, NULL
, 1, 0x10000, &ofpit
);
5101 error
= ovs_instruction_type_from_inst_type(&inst
, ofpit
);
5103 *insts
|= 1u << inst
;
5104 } else if (!loose
) {
5112 parse_table_features_next_table(struct ofpbuf
*payload
,
5113 unsigned long int *next_tables
)
5117 memset(next_tables
, 0, bitmap_n_bytes(255));
5118 for (i
= 0; i
< payload
->size
; i
++) {
5119 uint8_t id
= ((const uint8_t *) payload
->data
)[i
];
5121 return OFPERR_OFPBPC_BAD_VALUE
;
5123 bitmap_set1(next_tables
, id
);
5129 parse_oxms(struct ofpbuf
*payload
, bool loose
,
5130 struct mf_bitmap
*exactp
, struct mf_bitmap
*maskedp
)
5132 struct mf_bitmap exact
= MF_BITMAP_INITIALIZER
;
5133 struct mf_bitmap masked
= MF_BITMAP_INITIALIZER
;
5135 while (payload
->size
> 0) {
5136 const struct mf_field
*field
;
5140 error
= nx_pull_header(payload
, &field
, &hasmask
);
5142 bitmap_set1(hasmask
? masked
.bm
: exact
.bm
, field
->id
);
5143 } else if (error
!= OFPERR_OFPBMC_BAD_FIELD
|| !loose
) {
5149 } else if (!bitmap_is_all_zeros(exact
.bm
, MFF_N_IDS
)) {
5150 return OFPERR_OFPBMC_BAD_MASK
;
5154 } else if (!bitmap_is_all_zeros(masked
.bm
, MFF_N_IDS
)) {
5155 return OFPERR_OFPBMC_BAD_MASK
;
5160 /* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract
5161 * ofputil_table_features in 'tf'.
5163 * If 'loose' is true, this function ignores properties and values that it does
5164 * not understand, as a controller would want to do when interpreting
5165 * capabilities provided by a switch. If 'loose' is false, this function
5166 * treats unknown properties and values as an error, as a switch would want to
5167 * do when interpreting a configuration request made by a controller.
5169 * A single OpenFlow message can specify features for multiple tables. Calling
5170 * this function multiple times for a single 'msg' iterates through the tables
5171 * in the message. The caller must initially leave 'msg''s layer pointers null
5172 * and not modify them between calls.
5174 * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise
5175 * a positive "enum ofperr" value. */
5177 ofputil_decode_table_features(struct ofpbuf
*msg
,
5178 struct ofputil_table_features
*tf
, bool loose
)
5180 memset(tf
, 0, sizeof *tf
);
5183 ofpraw_pull_assert(msg
);
5190 const struct ofp_header
*oh
= msg
->header
;
5191 struct ofp13_table_features
*otf
= msg
->data
;
5192 if (msg
->size
< sizeof *otf
) {
5193 return OFPERR_OFPBPC_BAD_LEN
;
5196 unsigned int len
= ntohs(otf
->length
);
5197 if (len
< sizeof *otf
|| len
% 8 || len
> msg
->size
) {
5198 return OFPERR_OFPBPC_BAD_LEN
;
5201 tf
->table_id
= otf
->table_id
;
5202 if (tf
->table_id
== OFPTT_ALL
) {
5203 return OFPERR_OFPTFFC_BAD_TABLE
;
5206 ovs_strlcpy(tf
->name
, otf
->name
, OFP_MAX_TABLE_NAME_LEN
);
5207 tf
->metadata_match
= otf
->metadata_match
;
5208 tf
->metadata_write
= otf
->metadata_write
;
5209 tf
->miss_config
= OFPUTIL_TABLE_MISS_DEFAULT
;
5210 if (oh
->version
>= OFP14_VERSION
) {
5211 uint32_t caps
= ntohl(otf
->capabilities
);
5212 tf
->supports_eviction
= (caps
& OFPTC14_EVICTION
) != 0;
5213 tf
->supports_vacancy_events
= (caps
& OFPTC14_VACANCY_EVENTS
) != 0;
5215 tf
->supports_eviction
= -1;
5216 tf
->supports_vacancy_events
= -1;
5218 tf
->max_entries
= ntohl(otf
->max_entries
);
5220 struct ofpbuf properties
= ofpbuf_const_initializer(ofpbuf_pull(msg
, len
),
5222 ofpbuf_pull(&properties
, sizeof *otf
);
5223 while (properties
.size
> 0) {
5224 struct ofpbuf payload
;
5228 error
= pull_table_feature_property(&properties
, &payload
, &type
);
5233 switch ((enum ofp13_table_feature_prop_type
) type
) {
5234 case OFPTFPT13_INSTRUCTIONS
:
5235 error
= parse_instruction_ids(&payload
, loose
,
5236 &tf
->nonmiss
.instructions
);
5238 case OFPTFPT13_INSTRUCTIONS_MISS
:
5239 error
= parse_instruction_ids(&payload
, loose
,
5240 &tf
->miss
.instructions
);
5243 case OFPTFPT13_NEXT_TABLES
:
5244 error
= parse_table_features_next_table(&payload
,
5247 case OFPTFPT13_NEXT_TABLES_MISS
:
5248 error
= parse_table_features_next_table(&payload
, tf
->miss
.next
);
5251 case OFPTFPT13_WRITE_ACTIONS
:
5252 error
= parse_action_bitmap(&payload
, oh
->version
,
5253 &tf
->nonmiss
.write
.ofpacts
);
5255 case OFPTFPT13_WRITE_ACTIONS_MISS
:
5256 error
= parse_action_bitmap(&payload
, oh
->version
,
5257 &tf
->miss
.write
.ofpacts
);
5260 case OFPTFPT13_APPLY_ACTIONS
:
5261 error
= parse_action_bitmap(&payload
, oh
->version
,
5262 &tf
->nonmiss
.apply
.ofpacts
);
5264 case OFPTFPT13_APPLY_ACTIONS_MISS
:
5265 error
= parse_action_bitmap(&payload
, oh
->version
,
5266 &tf
->miss
.apply
.ofpacts
);
5269 case OFPTFPT13_MATCH
:
5270 error
= parse_oxms(&payload
, loose
, &tf
->match
, &tf
->mask
);
5272 case OFPTFPT13_WILDCARDS
:
5273 error
= parse_oxms(&payload
, loose
, &tf
->wildcard
, NULL
);
5276 case OFPTFPT13_WRITE_SETFIELD
:
5277 error
= parse_oxms(&payload
, loose
,
5278 &tf
->nonmiss
.write
.set_fields
, NULL
);
5280 case OFPTFPT13_WRITE_SETFIELD_MISS
:
5281 error
= parse_oxms(&payload
, loose
,
5282 &tf
->miss
.write
.set_fields
, NULL
);
5284 case OFPTFPT13_APPLY_SETFIELD
:
5285 error
= parse_oxms(&payload
, loose
,
5286 &tf
->nonmiss
.apply
.set_fields
, NULL
);
5288 case OFPTFPT13_APPLY_SETFIELD_MISS
:
5289 error
= parse_oxms(&payload
, loose
,
5290 &tf
->miss
.apply
.set_fields
, NULL
);
5293 case OFPTFPT13_EXPERIMENTER
:
5294 case OFPTFPT13_EXPERIMENTER_MISS
:
5296 error
= OFPPROP_UNKNOWN(loose
, "table features", type
);
5304 /* Fix inconsistencies:
5306 * - Turn on 'match' bits that are set in 'mask', because maskable
5307 * fields are matchable.
5309 * - Turn on 'wildcard' bits that are set in 'mask', because a field
5310 * that is arbitrarily maskable can be wildcarded entirely.
5312 * - Turn off 'wildcard' bits that are not in 'match', because a field
5313 * must be matchable for it to be meaningfully wildcarded. */
5314 bitmap_or(tf
->match
.bm
, tf
->mask
.bm
, MFF_N_IDS
);
5315 bitmap_or(tf
->wildcard
.bm
, tf
->mask
.bm
, MFF_N_IDS
);
5316 bitmap_and(tf
->wildcard
.bm
, tf
->match
.bm
, MFF_N_IDS
);
5321 /* Encodes and returns a request to obtain the table features of a switch.
5322 * The message is encoded for OpenFlow version 'ofp_version'. */
5324 ofputil_encode_table_features_request(enum ofp_version ofp_version
)
5326 struct ofpbuf
*request
= NULL
;
5328 switch (ofp_version
) {
5332 ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later "
5333 "(\'-O OpenFlow13\')");
5338 request
= ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST
,
5349 put_fields_property(struct ofpbuf
*reply
,
5350 const struct mf_bitmap
*fields
,
5351 const struct mf_bitmap
*masks
,
5352 enum ofp13_table_feature_prop_type property
,
5353 enum ofp_version version
)
5358 start_ofs
= ofpprop_start(reply
, property
);
5359 BITMAP_FOR_EACH_1 (field
, MFF_N_IDS
, fields
->bm
) {
5360 nx_put_header(reply
, field
, version
,
5361 masks
&& bitmap_is_set(masks
->bm
, field
));
5363 ofpprop_end(reply
, start_ofs
);
5367 put_table_action_features(struct ofpbuf
*reply
,
5368 const struct ofputil_table_action_features
*taf
,
5369 enum ofp13_table_feature_prop_type actions_type
,
5370 enum ofp13_table_feature_prop_type set_fields_type
,
5371 int miss_offset
, enum ofp_version version
)
5373 ofpprop_put_bitmap(reply
, actions_type
+ miss_offset
,
5374 ntohl(ofpact_bitmap_to_openflow(taf
->ofpacts
,
5376 put_fields_property(reply
, &taf
->set_fields
, NULL
,
5377 set_fields_type
+ miss_offset
, version
);
5381 put_table_instruction_features(
5382 struct ofpbuf
*reply
, const struct ofputil_table_instruction_features
*tif
,
5383 int miss_offset
, enum ofp_version version
)
5388 ofpprop_put_bitmap(reply
, OFPTFPT13_INSTRUCTIONS
+ miss_offset
,
5389 ntohl(ovsinst_bitmap_to_openflow(tif
->instructions
,
5392 start_ofs
= ofpprop_start(reply
, OFPTFPT13_NEXT_TABLES
+ miss_offset
);
5393 BITMAP_FOR_EACH_1 (table_id
, 255, tif
->next
) {
5394 ofpbuf_put(reply
, &table_id
, 1);
5396 ofpprop_end(reply
, start_ofs
);
5398 put_table_action_features(reply
, &tif
->write
,
5399 OFPTFPT13_WRITE_ACTIONS
,
5400 OFPTFPT13_WRITE_SETFIELD
, miss_offset
, version
);
5401 put_table_action_features(reply
, &tif
->apply
,
5402 OFPTFPT13_APPLY_ACTIONS
,
5403 OFPTFPT13_APPLY_SETFIELD
, miss_offset
, version
);
5407 ofputil_append_table_features_reply(const struct ofputil_table_features
*tf
,
5408 struct ovs_list
*replies
)
5410 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
5411 enum ofp_version version
= ofpmp_version(replies
);
5412 size_t start_ofs
= reply
->size
;
5413 struct ofp13_table_features
*otf
;
5415 otf
= ofpbuf_put_zeros(reply
, sizeof *otf
);
5416 otf
->table_id
= tf
->table_id
;
5417 ovs_strlcpy(otf
->name
, tf
->name
, sizeof otf
->name
);
5418 otf
->metadata_match
= tf
->metadata_match
;
5419 otf
->metadata_write
= tf
->metadata_write
;
5420 if (version
>= OFP14_VERSION
) {
5421 if (tf
->supports_eviction
) {
5422 otf
->capabilities
|= htonl(OFPTC14_EVICTION
);
5424 if (tf
->supports_vacancy_events
) {
5425 otf
->capabilities
|= htonl(OFPTC14_VACANCY_EVENTS
);
5428 otf
->max_entries
= htonl(tf
->max_entries
);
5430 put_table_instruction_features(reply
, &tf
->nonmiss
, 0, version
);
5431 put_table_instruction_features(reply
, &tf
->miss
, 1, version
);
5433 put_fields_property(reply
, &tf
->match
, &tf
->mask
,
5434 OFPTFPT13_MATCH
, version
);
5435 put_fields_property(reply
, &tf
->wildcard
, NULL
,
5436 OFPTFPT13_WILDCARDS
, version
);
5438 otf
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *otf
);
5439 otf
->length
= htons(reply
->size
- start_ofs
);
5440 ofpmp_postappend(replies
, start_ofs
);
5444 parse_table_desc_vacancy_property(struct ofpbuf
*property
,
5445 struct ofputil_table_desc
*td
)
5447 struct ofp14_table_mod_prop_vacancy
*otv
= property
->data
;
5449 if (property
->size
!= sizeof *otv
) {
5450 return OFPERR_OFPBPC_BAD_LEN
;
5453 td
->table_vacancy
.vacancy_down
= otv
->vacancy_down
;
5454 td
->table_vacancy
.vacancy_up
= otv
->vacancy_up
;
5455 td
->table_vacancy
.vacancy
= otv
->vacancy
;
5459 /* Decodes the next OpenFlow "table desc" message (of possibly several) from
5460 * 'msg' into an abstract form in '*td'. Returns 0 if successful, EOF if the
5461 * last "table desc" in 'msg' was already decoded, otherwise an OFPERR_*
5464 ofputil_decode_table_desc(struct ofpbuf
*msg
,
5465 struct ofputil_table_desc
*td
,
5466 enum ofp_version version
)
5468 memset(td
, 0, sizeof *td
);
5471 ofpraw_pull_assert(msg
);
5478 struct ofp14_table_desc
*otd
= ofpbuf_try_pull(msg
, sizeof *otd
);
5480 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFP14_TABLE_DESC reply has %"PRIu32
" "
5481 "leftover bytes at end", msg
->size
);
5482 return OFPERR_OFPBRC_BAD_LEN
;
5485 td
->table_id
= otd
->table_id
;
5486 size_t length
= ntohs(otd
->length
);
5487 if (length
< sizeof *otd
|| length
- sizeof *otd
> msg
->size
) {
5488 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFP14_TABLE_DESC reply claims invalid "
5489 "length %"PRIuSIZE
, length
);
5490 return OFPERR_OFPBRC_BAD_LEN
;
5492 length
-= sizeof *otd
;
5494 td
->eviction
= ofputil_decode_table_eviction(otd
->config
, version
);
5495 td
->vacancy
= ofputil_decode_table_vacancy(otd
->config
, version
);
5496 td
->eviction_flags
= UINT32_MAX
;
5498 struct ofpbuf properties
= ofpbuf_const_initializer(
5499 ofpbuf_pull(msg
, length
), length
);
5500 while (properties
.size
> 0) {
5501 struct ofpbuf payload
;
5505 error
= ofpprop_pull(&properties
, &payload
, &type
);
5511 case OFPTMPT14_EVICTION
:
5512 error
= ofpprop_parse_u32(&payload
, &td
->eviction_flags
);
5515 case OFPTMPT14_VACANCY
:
5516 error
= parse_table_desc_vacancy_property(&payload
, td
);
5520 error
= OFPPROP_UNKNOWN(true, "table_desc", type
);
5532 /* Encodes and returns a request to obtain description of tables of a switch.
5533 * The message is encoded for OpenFlow version 'ofp_version'. */
5535 ofputil_encode_table_desc_request(enum ofp_version ofp_version
)
5537 struct ofpbuf
*request
= NULL
;
5539 if (ofp_version
>= OFP14_VERSION
) {
5540 request
= ofpraw_alloc(OFPRAW_OFPST14_TABLE_DESC_REQUEST
,
5543 ovs_fatal(0, "dump-table-desc needs OpenFlow 1.4 or later "
5544 "(\'-O OpenFlow14\')");
5550 /* Function to append Table desc information in a reply list. */
5552 ofputil_append_table_desc_reply(const struct ofputil_table_desc
*td
,
5553 struct ovs_list
*replies
,
5554 enum ofp_version version
)
5556 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
5558 struct ofp14_table_desc
*otd
;
5560 start_otd
= reply
->size
;
5561 ofpbuf_put_zeros(reply
, sizeof *otd
);
5562 if (td
->eviction_flags
!= UINT32_MAX
) {
5563 ofpprop_put_u32(reply
, OFPTMPT14_EVICTION
, td
->eviction_flags
);
5565 if (td
->vacancy
== OFPUTIL_TABLE_VACANCY_ON
) {
5566 struct ofp14_table_mod_prop_vacancy
*otv
;
5568 otv
= ofpprop_put_zeros(reply
, OFPTMPT14_VACANCY
, sizeof *otv
);
5569 otv
->vacancy_down
= td
->table_vacancy
.vacancy_down
;
5570 otv
->vacancy_up
= td
->table_vacancy
.vacancy_up
;
5571 otv
->vacancy
= td
->table_vacancy
.vacancy
;
5574 otd
= ofpbuf_at_assert(reply
, start_otd
, sizeof *otd
);
5575 otd
->length
= htons(reply
->size
- start_otd
);
5576 otd
->table_id
= td
->table_id
;
5577 otd
->config
= ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT
,
5578 td
->eviction
, td
->vacancy
,
5580 ofpmp_postappend(replies
, start_otd
);
5583 /* This function parses Vacancy property, and decodes the
5584 * ofp14_table_mod_prop_vacancy in ofputil_table_mod.
5585 * Returns OFPERR_OFPBPC_BAD_VALUE error code when vacancy_down is
5586 * greater than vacancy_up and also when current vacancy has non-zero
5587 * value. Returns 0 on success. */
5589 parse_table_mod_vacancy_property(struct ofpbuf
*property
,
5590 struct ofputil_table_mod
*tm
)
5592 struct ofp14_table_mod_prop_vacancy
*otv
= property
->data
;
5594 if (property
->size
!= sizeof *otv
) {
5595 return OFPERR_OFPBPC_BAD_LEN
;
5597 tm
->table_vacancy
.vacancy_down
= otv
->vacancy_down
;
5598 tm
->table_vacancy
.vacancy_up
= otv
->vacancy_up
;
5599 if (tm
->table_vacancy
.vacancy_down
> tm
->table_vacancy
.vacancy_up
) {
5600 OFPPROP_LOG(&bad_ofmsg_rl
, false,
5601 "Value of vacancy_down is greater than vacancy_up");
5602 return OFPERR_OFPBPC_BAD_VALUE
;
5604 if (tm
->table_vacancy
.vacancy_down
> 100 ||
5605 tm
->table_vacancy
.vacancy_up
> 100) {
5606 OFPPROP_LOG(&bad_ofmsg_rl
, false, "Vacancy threshold percentage "
5607 "should not be greater than 100");
5608 return OFPERR_OFPBPC_BAD_VALUE
;
5610 tm
->table_vacancy
.vacancy
= otv
->vacancy
;
5611 if (tm
->table_vacancy
.vacancy
) {
5612 OFPPROP_LOG(&bad_ofmsg_rl
, false,
5613 "Vacancy value should be zero for table-mod messages");
5614 return OFPERR_OFPBPC_BAD_VALUE
;
5619 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5620 * table configuration (a table mod, table stats, or table features message),
5621 * returns the table vacancy configuration that it specifies.
5623 * Only OpenFlow 1.4 and later specify table vacancy configuration this way,
5624 * so for other 'version' this function always returns
5625 * OFPUTIL_TABLE_VACANCY_DEFAULT. */
5626 static enum ofputil_table_vacancy
5627 ofputil_decode_table_vacancy(ovs_be32 config
, enum ofp_version version
)
5629 return (version
< OFP14_VERSION
? OFPUTIL_TABLE_VACANCY_DEFAULT
5630 : config
& htonl(OFPTC14_VACANCY_EVENTS
) ? OFPUTIL_TABLE_VACANCY_ON
5631 : OFPUTIL_TABLE_VACANCY_OFF
);
5634 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5635 * table configuration (a table mod, table stats, or table features message),
5636 * returns the table eviction configuration that it specifies.
5638 * Only OpenFlow 1.4 and later specify table eviction configuration this way,
5639 * so for other 'version' values this function always returns
5640 * OFPUTIL_TABLE_EVICTION_DEFAULT. */
5641 static enum ofputil_table_eviction
5642 ofputil_decode_table_eviction(ovs_be32 config
, enum ofp_version version
)
5644 return (version
< OFP14_VERSION
? OFPUTIL_TABLE_EVICTION_DEFAULT
5645 : config
& htonl(OFPTC14_EVICTION
) ? OFPUTIL_TABLE_EVICTION_ON
5646 : OFPUTIL_TABLE_EVICTION_OFF
);
5649 /* Returns a bitmap of OFPTC* values suitable for 'config' fields in various
5650 * OpenFlow messages of the given 'version', based on the provided 'miss' and
5651 * 'eviction' values. */
5653 ofputil_encode_table_config(enum ofputil_table_miss miss
,
5654 enum ofputil_table_eviction eviction
,
5655 enum ofputil_table_vacancy vacancy
,
5656 enum ofp_version version
)
5658 uint32_t config
= 0;
5659 /* See the section "OFPTC_* Table Configuration" in DESIGN.md for more
5660 * information on the crazy evolution of this field. */
5663 /* OpenFlow 1.0 didn't have such a field, any value ought to do. */
5668 /* OpenFlow 1.1 and 1.2 define only OFPTC11_TABLE_MISS_*. */
5670 case OFPUTIL_TABLE_MISS_DEFAULT
:
5671 /* Really this shouldn't be used for encoding (the caller should
5672 * provide a specific value) but I can't imagine that defaulting to
5673 * the fall-through case here will hurt. */
5674 case OFPUTIL_TABLE_MISS_CONTROLLER
:
5676 return htonl(OFPTC11_TABLE_MISS_CONTROLLER
);
5677 case OFPUTIL_TABLE_MISS_CONTINUE
:
5678 return htonl(OFPTC11_TABLE_MISS_CONTINUE
);
5679 case OFPUTIL_TABLE_MISS_DROP
:
5680 return htonl(OFPTC11_TABLE_MISS_DROP
);
5685 /* OpenFlow 1.3 removed OFPTC11_TABLE_MISS_* and didn't define any new
5686 * flags, so this is correct. */
5692 /* OpenFlow 1.4 introduced OFPTC14_EVICTION and
5693 * OFPTC14_VACANCY_EVENTS. */
5694 if (eviction
== OFPUTIL_TABLE_EVICTION_ON
) {
5695 config
|= OFPTC14_EVICTION
;
5697 if (vacancy
== OFPUTIL_TABLE_VACANCY_ON
) {
5698 config
|= OFPTC14_VACANCY_EVENTS
;
5700 return htonl(config
);
5706 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5707 * table configuration (a table mod, table stats, or table features message),
5708 * returns the table miss configuration that it specifies.
5710 * Only OpenFlow 1.1 and 1.2 specify table miss configurations this way, so for
5711 * other 'version' values this function always returns
5712 * OFPUTIL_TABLE_MISS_DEFAULT. */
5713 static enum ofputil_table_miss
5714 ofputil_decode_table_miss(ovs_be32 config_
, enum ofp_version version
)
5716 uint32_t config
= ntohl(config_
);
5718 if (version
== OFP11_VERSION
|| version
== OFP12_VERSION
) {
5719 switch (config
& OFPTC11_TABLE_MISS_MASK
) {
5720 case OFPTC11_TABLE_MISS_CONTROLLER
:
5721 return OFPUTIL_TABLE_MISS_CONTROLLER
;
5723 case OFPTC11_TABLE_MISS_CONTINUE
:
5724 return OFPUTIL_TABLE_MISS_CONTINUE
;
5726 case OFPTC11_TABLE_MISS_DROP
:
5727 return OFPUTIL_TABLE_MISS_DROP
;
5730 VLOG_WARN_RL(&bad_ofmsg_rl
, "bad table miss config %d", config
);
5731 return OFPUTIL_TABLE_MISS_CONTROLLER
;
5734 return OFPUTIL_TABLE_MISS_DEFAULT
;
5738 /* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in
5739 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
5741 ofputil_decode_table_mod(const struct ofp_header
*oh
,
5742 struct ofputil_table_mod
*pm
)
5744 memset(pm
, 0, sizeof *pm
);
5745 pm
->miss
= OFPUTIL_TABLE_MISS_DEFAULT
;
5746 pm
->eviction
= OFPUTIL_TABLE_EVICTION_DEFAULT
;
5747 pm
->eviction_flags
= UINT32_MAX
;
5748 pm
->vacancy
= OFPUTIL_TABLE_VACANCY_DEFAULT
;
5750 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
5751 enum ofpraw raw
= ofpraw_pull_assert(&b
);
5752 if (raw
== OFPRAW_OFPT11_TABLE_MOD
) {
5753 const struct ofp11_table_mod
*otm
= b
.data
;
5755 pm
->table_id
= otm
->table_id
;
5756 pm
->miss
= ofputil_decode_table_miss(otm
->config
, oh
->version
);
5757 } else if (raw
== OFPRAW_OFPT14_TABLE_MOD
) {
5758 const struct ofp14_table_mod
*otm
= ofpbuf_pull(&b
, sizeof *otm
);
5760 pm
->table_id
= otm
->table_id
;
5761 pm
->miss
= ofputil_decode_table_miss(otm
->config
, oh
->version
);
5762 pm
->eviction
= ofputil_decode_table_eviction(otm
->config
, oh
->version
);
5763 pm
->vacancy
= ofputil_decode_table_vacancy(otm
->config
, oh
->version
);
5764 while (b
.size
> 0) {
5765 struct ofpbuf property
;
5769 error
= ofpprop_pull(&b
, &property
, &type
);
5775 case OFPTMPT14_EVICTION
:
5776 error
= ofpprop_parse_u32(&property
, &pm
->eviction
);
5779 case OFPTMPT14_VACANCY
:
5780 error
= parse_table_mod_vacancy_property(&property
, pm
);
5784 error
= OFPERR_OFPBRC_BAD_TYPE
;
5793 return OFPERR_OFPBRC_BAD_TYPE
;
5799 /* Converts the abstract form of a "table mod" message in '*tm' into an
5800 * OpenFlow message suitable for 'protocol', and returns that encoded form in a
5801 * buffer owned by the caller. */
5803 ofputil_encode_table_mod(const struct ofputil_table_mod
*tm
,
5804 enum ofputil_protocol protocol
)
5806 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
5809 switch (ofp_version
) {
5810 case OFP10_VERSION
: {
5811 ovs_fatal(0, "table mod needs OpenFlow 1.1 or later "
5812 "(\'-O OpenFlow11\')");
5817 case OFP13_VERSION
: {
5818 struct ofp11_table_mod
*otm
;
5820 b
= ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD
, ofp_version
, 0);
5821 otm
= ofpbuf_put_zeros(b
, sizeof *otm
);
5822 otm
->table_id
= tm
->table_id
;
5823 otm
->config
= ofputil_encode_table_config(tm
->miss
, tm
->eviction
,
5824 tm
->vacancy
, ofp_version
);
5829 case OFP16_VERSION
: {
5830 struct ofp14_table_mod
*otm
;
5832 b
= ofpraw_alloc(OFPRAW_OFPT14_TABLE_MOD
, ofp_version
, 0);
5833 otm
= ofpbuf_put_zeros(b
, sizeof *otm
);
5834 otm
->table_id
= tm
->table_id
;
5835 otm
->config
= ofputil_encode_table_config(tm
->miss
, tm
->eviction
,
5836 tm
->vacancy
, ofp_version
);
5838 if (tm
->eviction_flags
!= UINT32_MAX
) {
5839 ofpprop_put_u32(b
, OFPTMPT14_EVICTION
, tm
->eviction_flags
);
5841 if (tm
->vacancy
== OFPUTIL_TABLE_VACANCY_ON
) {
5842 struct ofp14_table_mod_prop_vacancy
*otv
;
5844 otv
= ofpprop_put_zeros(b
, OFPTMPT14_VACANCY
, sizeof *otv
);
5845 otv
->vacancy_down
= tm
->table_vacancy
.vacancy_down
;
5846 otv
->vacancy_up
= tm
->table_vacancy
.vacancy_up
;
5857 /* ofputil_role_request */
5859 /* Decodes the OpenFlow "role request" or "role reply" message in '*oh' into
5860 * an abstract form in '*rr'. Returns 0 if successful, otherwise an
5861 * OFPERR_* value. */
5863 ofputil_decode_role_message(const struct ofp_header
*oh
,
5864 struct ofputil_role_request
*rr
)
5866 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
5867 enum ofpraw raw
= ofpraw_pull_assert(&b
);
5868 if (raw
== OFPRAW_OFPT12_ROLE_REQUEST
||
5869 raw
== OFPRAW_OFPT12_ROLE_REPLY
) {
5870 const struct ofp12_role_request
*orr
= b
.msg
;
5872 if (orr
->role
!= htonl(OFPCR12_ROLE_NOCHANGE
) &&
5873 orr
->role
!= htonl(OFPCR12_ROLE_EQUAL
) &&
5874 orr
->role
!= htonl(OFPCR12_ROLE_MASTER
) &&
5875 orr
->role
!= htonl(OFPCR12_ROLE_SLAVE
)) {
5876 return OFPERR_OFPRRFC_BAD_ROLE
;
5879 rr
->role
= ntohl(orr
->role
);
5880 if (raw
== OFPRAW_OFPT12_ROLE_REQUEST
5881 ? orr
->role
== htonl(OFPCR12_ROLE_NOCHANGE
)
5882 : orr
->generation_id
== OVS_BE64_MAX
) {
5883 rr
->have_generation_id
= false;
5884 rr
->generation_id
= 0;
5886 rr
->have_generation_id
= true;
5887 rr
->generation_id
= ntohll(orr
->generation_id
);
5889 } else if (raw
== OFPRAW_NXT_ROLE_REQUEST
||
5890 raw
== OFPRAW_NXT_ROLE_REPLY
) {
5891 const struct nx_role_request
*nrr
= b
.msg
;
5893 BUILD_ASSERT(NX_ROLE_OTHER
+ 1 == OFPCR12_ROLE_EQUAL
);
5894 BUILD_ASSERT(NX_ROLE_MASTER
+ 1 == OFPCR12_ROLE_MASTER
);
5895 BUILD_ASSERT(NX_ROLE_SLAVE
+ 1 == OFPCR12_ROLE_SLAVE
);
5897 if (nrr
->role
!= htonl(NX_ROLE_OTHER
) &&
5898 nrr
->role
!= htonl(NX_ROLE_MASTER
) &&
5899 nrr
->role
!= htonl(NX_ROLE_SLAVE
)) {
5900 return OFPERR_OFPRRFC_BAD_ROLE
;
5903 rr
->role
= ntohl(nrr
->role
) + 1;
5904 rr
->have_generation_id
= false;
5905 rr
->generation_id
= 0;
5913 /* Returns an encoded form of a role reply suitable for the "request" in a
5914 * buffer owned by the caller. */
5916 ofputil_encode_role_reply(const struct ofp_header
*request
,
5917 const struct ofputil_role_request
*rr
)
5922 raw
= ofpraw_decode_assert(request
);
5923 if (raw
== OFPRAW_OFPT12_ROLE_REQUEST
) {
5924 struct ofp12_role_request
*orr
;
5926 buf
= ofpraw_alloc_reply(OFPRAW_OFPT12_ROLE_REPLY
, request
, 0);
5927 orr
= ofpbuf_put_zeros(buf
, sizeof *orr
);
5929 orr
->role
= htonl(rr
->role
);
5930 orr
->generation_id
= htonll(rr
->have_generation_id
5933 } else if (raw
== OFPRAW_NXT_ROLE_REQUEST
) {
5934 struct nx_role_request
*nrr
;
5936 BUILD_ASSERT(NX_ROLE_OTHER
== OFPCR12_ROLE_EQUAL
- 1);
5937 BUILD_ASSERT(NX_ROLE_MASTER
== OFPCR12_ROLE_MASTER
- 1);
5938 BUILD_ASSERT(NX_ROLE_SLAVE
== OFPCR12_ROLE_SLAVE
- 1);
5940 buf
= ofpraw_alloc_reply(OFPRAW_NXT_ROLE_REPLY
, request
, 0);
5941 nrr
= ofpbuf_put_zeros(buf
, sizeof *nrr
);
5942 nrr
->role
= htonl(rr
->role
- 1);
5950 /* Encodes "role status" message 'status' for sending in the given
5951 * 'protocol'. Returns the role status message, if 'protocol' supports them,
5952 * otherwise a null pointer. */
5954 ofputil_encode_role_status(const struct ofputil_role_status
*status
,
5955 enum ofputil_protocol protocol
)
5957 enum ofp_version version
;
5959 version
= ofputil_protocol_to_ofp_version(protocol
);
5960 if (version
>= OFP14_VERSION
) {
5961 struct ofp14_role_status
*rstatus
;
5964 buf
= ofpraw_alloc_xid(OFPRAW_OFPT14_ROLE_STATUS
, version
, htonl(0),
5966 rstatus
= ofpbuf_put_zeros(buf
, sizeof *rstatus
);
5967 rstatus
->role
= htonl(status
->role
);
5968 rstatus
->reason
= status
->reason
;
5969 rstatus
->generation_id
= htonll(status
->generation_id
);
5978 ofputil_decode_role_status(const struct ofp_header
*oh
,
5979 struct ofputil_role_status
*rs
)
5981 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
5982 enum ofpraw raw
= ofpraw_pull_assert(&b
);
5983 ovs_assert(raw
== OFPRAW_OFPT14_ROLE_STATUS
);
5985 const struct ofp14_role_status
*r
= b
.msg
;
5986 if (r
->role
!= htonl(OFPCR12_ROLE_NOCHANGE
) &&
5987 r
->role
!= htonl(OFPCR12_ROLE_EQUAL
) &&
5988 r
->role
!= htonl(OFPCR12_ROLE_MASTER
) &&
5989 r
->role
!= htonl(OFPCR12_ROLE_SLAVE
)) {
5990 return OFPERR_OFPRRFC_BAD_ROLE
;
5993 rs
->role
= ntohl(r
->role
);
5994 rs
->generation_id
= ntohll(r
->generation_id
);
5995 rs
->reason
= r
->reason
;
6000 /* Encodes 'rf' according to 'protocol', and returns the encoded message.
6001 * 'protocol' must be for OpenFlow 1.4 or later. */
6003 ofputil_encode_requestforward(const struct ofputil_requestforward
*rf
,
6004 enum ofputil_protocol protocol
)
6006 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
6007 struct ofpbuf
*inner
;
6009 switch (rf
->reason
) {
6010 case OFPRFR_GROUP_MOD
:
6011 inner
= ofputil_encode_group_mod(ofp_version
, rf
->group_mod
);
6014 case OFPRFR_METER_MOD
:
6015 inner
= ofputil_encode_meter_mod(ofp_version
, rf
->meter_mod
);
6018 case OFPRFR_N_REASONS
:
6023 struct ofp_header
*inner_oh
= inner
->data
;
6024 inner_oh
->xid
= rf
->xid
;
6025 inner_oh
->length
= htons(inner
->size
);
6027 struct ofpbuf
*outer
= ofpraw_alloc_xid(OFPRAW_OFPT14_REQUESTFORWARD
,
6028 ofp_version
, htonl(0),
6030 ofpbuf_put(outer
, inner
->data
, inner
->size
);
6031 ofpbuf_delete(inner
);
6036 /* Decodes OFPT_REQUESTFORWARD message 'outer'. On success, puts the decoded
6037 * form into '*rf' and returns 0, and the caller is later responsible for
6038 * freeing the content of 'rf', with ofputil_destroy_requestforward(rf). On
6039 * failure, returns an ofperr and '*rf' is indeterminate. */
6041 ofputil_decode_requestforward(const struct ofp_header
*outer
,
6042 struct ofputil_requestforward
*rf
)
6044 struct ofpbuf b
= ofpbuf_const_initializer(outer
, ntohs(outer
->length
));
6046 /* Skip past outer message. */
6047 enum ofpraw outer_raw
= ofpraw_pull_assert(&b
);
6048 ovs_assert(outer_raw
== OFPRAW_OFPT14_REQUESTFORWARD
);
6050 /* Validate inner message. */
6051 if (b
.size
< sizeof(struct ofp_header
)) {
6052 return OFPERR_OFPBFC_MSG_BAD_LEN
;
6054 const struct ofp_header
*inner
= b
.data
;
6055 unsigned int inner_len
= ntohs(inner
->length
);
6056 if (inner_len
< sizeof(struct ofp_header
) || inner_len
> b
.size
) {
6057 return OFPERR_OFPBFC_MSG_BAD_LEN
;
6059 if (inner
->version
!= outer
->version
) {
6060 return OFPERR_OFPBRC_BAD_VERSION
;
6063 /* Parse inner message. */
6065 enum ofperr error
= ofptype_decode(&type
, inner
);
6070 rf
->xid
= inner
->xid
;
6071 if (type
== OFPTYPE_GROUP_MOD
) {
6072 rf
->reason
= OFPRFR_GROUP_MOD
;
6073 rf
->group_mod
= xmalloc(sizeof *rf
->group_mod
);
6074 error
= ofputil_decode_group_mod(inner
, rf
->group_mod
);
6076 free(rf
->group_mod
);
6079 } else if (type
== OFPTYPE_METER_MOD
) {
6080 rf
->reason
= OFPRFR_METER_MOD
;
6081 rf
->meter_mod
= xmalloc(sizeof *rf
->meter_mod
);
6082 ofpbuf_init(&rf
->bands
, 64);
6083 error
= ofputil_decode_meter_mod(inner
, rf
->meter_mod
, &rf
->bands
);
6085 free(rf
->meter_mod
);
6086 ofpbuf_uninit(&rf
->bands
);
6090 return OFPERR_OFPBFC_MSG_UNSUP
;
6096 /* Frees the content of 'rf', which should have been initialized through a
6097 * successful call to ofputil_decode_requestforward(). */
6099 ofputil_destroy_requestforward(struct ofputil_requestforward
*rf
)
6105 switch (rf
->reason
) {
6106 case OFPRFR_GROUP_MOD
:
6107 ofputil_uninit_group_mod(rf
->group_mod
);
6108 free(rf
->group_mod
);
6111 case OFPRFR_METER_MOD
:
6112 ofpbuf_uninit(&rf
->bands
);
6113 free(rf
->meter_mod
);
6116 case OFPRFR_N_REASONS
:
6123 /* OpenFlow 1.0 and 1.1 don't distinguish between a field that cannot be
6124 * matched and a field that must be wildcarded. This function returns a bitmap
6125 * that contains both kinds of fields. */
6126 static struct mf_bitmap
6127 wild_or_nonmatchable_fields(const struct ofputil_table_features
*features
)
6129 struct mf_bitmap wc
= features
->match
;
6130 bitmap_not(wc
.bm
, MFF_N_IDS
);
6131 bitmap_or(wc
.bm
, features
->wildcard
.bm
, MFF_N_IDS
);
6135 struct ofp10_wc_map
{
6136 enum ofp10_flow_wildcards wc10
;
6137 enum mf_field_id mf
;
6140 static const struct ofp10_wc_map ofp10_wc_map
[] = {
6141 { OFPFW10_IN_PORT
, MFF_IN_PORT
},
6142 { OFPFW10_DL_VLAN
, MFF_VLAN_VID
},
6143 { OFPFW10_DL_SRC
, MFF_ETH_SRC
},
6144 { OFPFW10_DL_DST
, MFF_ETH_DST
},
6145 { OFPFW10_DL_TYPE
, MFF_ETH_TYPE
},
6146 { OFPFW10_NW_PROTO
, MFF_IP_PROTO
},
6147 { OFPFW10_TP_SRC
, MFF_TCP_SRC
},
6148 { OFPFW10_TP_DST
, MFF_TCP_DST
},
6149 { OFPFW10_NW_SRC_MASK
, MFF_IPV4_SRC
},
6150 { OFPFW10_NW_DST_MASK
, MFF_IPV4_DST
},
6151 { OFPFW10_DL_VLAN_PCP
, MFF_VLAN_PCP
},
6152 { OFPFW10_NW_TOS
, MFF_IP_DSCP
},
6156 mf_bitmap_to_of10(const struct mf_bitmap
*fields
)
6158 const struct ofp10_wc_map
*p
;
6161 for (p
= ofp10_wc_map
; p
< &ofp10_wc_map
[ARRAY_SIZE(ofp10_wc_map
)]; p
++) {
6162 if (bitmap_is_set(fields
->bm
, p
->mf
)) {
6169 static struct mf_bitmap
6170 mf_bitmap_from_of10(ovs_be32 wc10_
)
6172 struct mf_bitmap fields
= MF_BITMAP_INITIALIZER
;
6173 const struct ofp10_wc_map
*p
;
6174 uint32_t wc10
= ntohl(wc10_
);
6176 for (p
= ofp10_wc_map
; p
< &ofp10_wc_map
[ARRAY_SIZE(ofp10_wc_map
)]; p
++) {
6177 if (wc10
& p
->wc10
) {
6178 bitmap_set1(fields
.bm
, p
->mf
);
6185 ofputil_put_ofp10_table_stats(const struct ofputil_table_stats
*stats
,
6186 const struct ofputil_table_features
*features
,
6189 struct mf_bitmap wc
= wild_or_nonmatchable_fields(features
);
6190 struct ofp10_table_stats
*out
;
6192 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
6193 out
->table_id
= features
->table_id
;
6194 ovs_strlcpy(out
->name
, features
->name
, sizeof out
->name
);
6195 out
->wildcards
= mf_bitmap_to_of10(&wc
);
6196 out
->max_entries
= htonl(features
->max_entries
);
6197 out
->active_count
= htonl(stats
->active_count
);
6198 put_32aligned_be64(&out
->lookup_count
, htonll(stats
->lookup_count
));
6199 put_32aligned_be64(&out
->matched_count
, htonll(stats
->matched_count
));
6202 struct ofp11_wc_map
{
6203 enum ofp11_flow_match_fields wc11
;
6204 enum mf_field_id mf
;
6207 static const struct ofp11_wc_map ofp11_wc_map
[] = {
6208 { OFPFMF11_IN_PORT
, MFF_IN_PORT
},
6209 { OFPFMF11_DL_VLAN
, MFF_VLAN_VID
},
6210 { OFPFMF11_DL_VLAN_PCP
, MFF_VLAN_PCP
},
6211 { OFPFMF11_DL_TYPE
, MFF_ETH_TYPE
},
6212 { OFPFMF11_NW_TOS
, MFF_IP_DSCP
},
6213 { OFPFMF11_NW_PROTO
, MFF_IP_PROTO
},
6214 { OFPFMF11_TP_SRC
, MFF_TCP_SRC
},
6215 { OFPFMF11_TP_DST
, MFF_TCP_DST
},
6216 { OFPFMF11_MPLS_LABEL
, MFF_MPLS_LABEL
},
6217 { OFPFMF11_MPLS_TC
, MFF_MPLS_TC
},
6218 /* I don't know what OFPFMF11_TYPE means. */
6219 { OFPFMF11_DL_SRC
, MFF_ETH_SRC
},
6220 { OFPFMF11_DL_DST
, MFF_ETH_DST
},
6221 { OFPFMF11_NW_SRC
, MFF_IPV4_SRC
},
6222 { OFPFMF11_NW_DST
, MFF_IPV4_DST
},
6223 { OFPFMF11_METADATA
, MFF_METADATA
},
6227 mf_bitmap_to_of11(const struct mf_bitmap
*fields
)
6229 const struct ofp11_wc_map
*p
;
6232 for (p
= ofp11_wc_map
; p
< &ofp11_wc_map
[ARRAY_SIZE(ofp11_wc_map
)]; p
++) {
6233 if (bitmap_is_set(fields
->bm
, p
->mf
)) {
6240 static struct mf_bitmap
6241 mf_bitmap_from_of11(ovs_be32 wc11_
)
6243 struct mf_bitmap fields
= MF_BITMAP_INITIALIZER
;
6244 const struct ofp11_wc_map
*p
;
6245 uint32_t wc11
= ntohl(wc11_
);
6247 for (p
= ofp11_wc_map
; p
< &ofp11_wc_map
[ARRAY_SIZE(ofp11_wc_map
)]; p
++) {
6248 if (wc11
& p
->wc11
) {
6249 bitmap_set1(fields
.bm
, p
->mf
);
6256 ofputil_put_ofp11_table_stats(const struct ofputil_table_stats
*stats
,
6257 const struct ofputil_table_features
*features
,
6260 struct mf_bitmap wc
= wild_or_nonmatchable_fields(features
);
6261 struct ofp11_table_stats
*out
;
6263 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
6264 out
->table_id
= features
->table_id
;
6265 ovs_strlcpy(out
->name
, features
->name
, sizeof out
->name
);
6266 out
->wildcards
= mf_bitmap_to_of11(&wc
);
6267 out
->match
= mf_bitmap_to_of11(&features
->match
);
6268 out
->instructions
= ovsinst_bitmap_to_openflow(
6269 features
->nonmiss
.instructions
, OFP11_VERSION
);
6270 out
->write_actions
= ofpact_bitmap_to_openflow(
6271 features
->nonmiss
.write
.ofpacts
, OFP11_VERSION
);
6272 out
->apply_actions
= ofpact_bitmap_to_openflow(
6273 features
->nonmiss
.apply
.ofpacts
, OFP11_VERSION
);
6274 out
->config
= htonl(features
->miss_config
);
6275 out
->max_entries
= htonl(features
->max_entries
);
6276 out
->active_count
= htonl(stats
->active_count
);
6277 out
->lookup_count
= htonll(stats
->lookup_count
);
6278 out
->matched_count
= htonll(stats
->matched_count
);
6282 ofputil_put_ofp12_table_stats(const struct ofputil_table_stats
*stats
,
6283 const struct ofputil_table_features
*features
,
6286 struct ofp12_table_stats
*out
;
6288 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
6289 out
->table_id
= features
->table_id
;
6290 ovs_strlcpy(out
->name
, features
->name
, sizeof out
->name
);
6291 out
->match
= oxm_bitmap_from_mf_bitmap(&features
->match
, OFP12_VERSION
);
6292 out
->wildcards
= oxm_bitmap_from_mf_bitmap(&features
->wildcard
,
6294 out
->write_actions
= ofpact_bitmap_to_openflow(
6295 features
->nonmiss
.write
.ofpacts
, OFP12_VERSION
);
6296 out
->apply_actions
= ofpact_bitmap_to_openflow(
6297 features
->nonmiss
.apply
.ofpacts
, OFP12_VERSION
);
6298 out
->write_setfields
= oxm_bitmap_from_mf_bitmap(
6299 &features
->nonmiss
.write
.set_fields
, OFP12_VERSION
);
6300 out
->apply_setfields
= oxm_bitmap_from_mf_bitmap(
6301 &features
->nonmiss
.apply
.set_fields
, OFP12_VERSION
);
6302 out
->metadata_match
= features
->metadata_match
;
6303 out
->metadata_write
= features
->metadata_write
;
6304 out
->instructions
= ovsinst_bitmap_to_openflow(
6305 features
->nonmiss
.instructions
, OFP12_VERSION
);
6306 out
->config
= ofputil_encode_table_config(features
->miss_config
,
6307 OFPUTIL_TABLE_EVICTION_DEFAULT
,
6308 OFPUTIL_TABLE_VACANCY_DEFAULT
,
6310 out
->max_entries
= htonl(features
->max_entries
);
6311 out
->active_count
= htonl(stats
->active_count
);
6312 out
->lookup_count
= htonll(stats
->lookup_count
);
6313 out
->matched_count
= htonll(stats
->matched_count
);
6317 ofputil_put_ofp13_table_stats(const struct ofputil_table_stats
*stats
,
6320 struct ofp13_table_stats
*out
;
6322 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
6323 out
->table_id
= stats
->table_id
;
6324 out
->active_count
= htonl(stats
->active_count
);
6325 out
->lookup_count
= htonll(stats
->lookup_count
);
6326 out
->matched_count
= htonll(stats
->matched_count
);
6330 ofputil_encode_table_stats_reply(const struct ofp_header
*request
)
6332 return ofpraw_alloc_stats_reply(request
, 0);
6336 ofputil_append_table_stats_reply(struct ofpbuf
*reply
,
6337 const struct ofputil_table_stats
*stats
,
6338 const struct ofputil_table_features
*features
)
6340 struct ofp_header
*oh
= reply
->header
;
6342 ovs_assert(stats
->table_id
== features
->table_id
);
6344 switch ((enum ofp_version
) oh
->version
) {
6346 ofputil_put_ofp10_table_stats(stats
, features
, reply
);
6350 ofputil_put_ofp11_table_stats(stats
, features
, reply
);
6354 ofputil_put_ofp12_table_stats(stats
, features
, reply
);
6361 ofputil_put_ofp13_table_stats(stats
, reply
);
6370 ofputil_decode_ofp10_table_stats(struct ofpbuf
*msg
,
6371 struct ofputil_table_stats
*stats
,
6372 struct ofputil_table_features
*features
)
6374 struct ofp10_table_stats
*ots
;
6376 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
6378 return OFPERR_OFPBRC_BAD_LEN
;
6381 features
->table_id
= ots
->table_id
;
6382 ovs_strlcpy(features
->name
, ots
->name
, sizeof features
->name
);
6383 features
->max_entries
= ntohl(ots
->max_entries
);
6384 features
->match
= features
->wildcard
= mf_bitmap_from_of10(ots
->wildcards
);
6386 stats
->table_id
= ots
->table_id
;
6387 stats
->active_count
= ntohl(ots
->active_count
);
6388 stats
->lookup_count
= ntohll(get_32aligned_be64(&ots
->lookup_count
));
6389 stats
->matched_count
= ntohll(get_32aligned_be64(&ots
->matched_count
));
6395 ofputil_decode_ofp11_table_stats(struct ofpbuf
*msg
,
6396 struct ofputil_table_stats
*stats
,
6397 struct ofputil_table_features
*features
)
6399 struct ofp11_table_stats
*ots
;
6401 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
6403 return OFPERR_OFPBRC_BAD_LEN
;
6406 features
->table_id
= ots
->table_id
;
6407 ovs_strlcpy(features
->name
, ots
->name
, sizeof features
->name
);
6408 features
->max_entries
= ntohl(ots
->max_entries
);
6409 features
->nonmiss
.instructions
= ovsinst_bitmap_from_openflow(
6410 ots
->instructions
, OFP11_VERSION
);
6411 features
->nonmiss
.write
.ofpacts
= ofpact_bitmap_from_openflow(
6412 ots
->write_actions
, OFP11_VERSION
);
6413 features
->nonmiss
.apply
.ofpacts
= ofpact_bitmap_from_openflow(
6414 ots
->write_actions
, OFP11_VERSION
);
6415 features
->miss
= features
->nonmiss
;
6416 features
->miss_config
= ofputil_decode_table_miss(ots
->config
,
6418 features
->match
= mf_bitmap_from_of11(ots
->match
);
6419 features
->wildcard
= mf_bitmap_from_of11(ots
->wildcards
);
6420 bitmap_or(features
->match
.bm
, features
->wildcard
.bm
, MFF_N_IDS
);
6422 stats
->table_id
= ots
->table_id
;
6423 stats
->active_count
= ntohl(ots
->active_count
);
6424 stats
->lookup_count
= ntohll(ots
->lookup_count
);
6425 stats
->matched_count
= ntohll(ots
->matched_count
);
6431 ofputil_decode_ofp12_table_stats(struct ofpbuf
*msg
,
6432 struct ofputil_table_stats
*stats
,
6433 struct ofputil_table_features
*features
)
6435 struct ofp12_table_stats
*ots
;
6437 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
6439 return OFPERR_OFPBRC_BAD_LEN
;
6442 features
->table_id
= ots
->table_id
;
6443 ovs_strlcpy(features
->name
, ots
->name
, sizeof features
->name
);
6444 features
->metadata_match
= ots
->metadata_match
;
6445 features
->metadata_write
= ots
->metadata_write
;
6446 features
->miss_config
= ofputil_decode_table_miss(ots
->config
,
6448 features
->max_entries
= ntohl(ots
->max_entries
);
6450 features
->nonmiss
.instructions
= ovsinst_bitmap_from_openflow(
6451 ots
->instructions
, OFP12_VERSION
);
6452 features
->nonmiss
.write
.ofpacts
= ofpact_bitmap_from_openflow(
6453 ots
->write_actions
, OFP12_VERSION
);
6454 features
->nonmiss
.apply
.ofpacts
= ofpact_bitmap_from_openflow(
6455 ots
->apply_actions
, OFP12_VERSION
);
6456 features
->nonmiss
.write
.set_fields
= oxm_bitmap_to_mf_bitmap(
6457 ots
->write_setfields
, OFP12_VERSION
);
6458 features
->nonmiss
.apply
.set_fields
= oxm_bitmap_to_mf_bitmap(
6459 ots
->apply_setfields
, OFP12_VERSION
);
6460 features
->miss
= features
->nonmiss
;
6462 features
->match
= oxm_bitmap_to_mf_bitmap(ots
->match
, OFP12_VERSION
);
6463 features
->wildcard
= oxm_bitmap_to_mf_bitmap(ots
->wildcards
,
6465 bitmap_or(features
->match
.bm
, features
->wildcard
.bm
, MFF_N_IDS
);
6467 stats
->table_id
= ots
->table_id
;
6468 stats
->active_count
= ntohl(ots
->active_count
);
6469 stats
->lookup_count
= ntohll(ots
->lookup_count
);
6470 stats
->matched_count
= ntohll(ots
->matched_count
);
6476 ofputil_decode_ofp13_table_stats(struct ofpbuf
*msg
,
6477 struct ofputil_table_stats
*stats
,
6478 struct ofputil_table_features
*features
)
6480 struct ofp13_table_stats
*ots
;
6482 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
6484 return OFPERR_OFPBRC_BAD_LEN
;
6487 features
->table_id
= ots
->table_id
;
6489 stats
->table_id
= ots
->table_id
;
6490 stats
->active_count
= ntohl(ots
->active_count
);
6491 stats
->lookup_count
= ntohll(ots
->lookup_count
);
6492 stats
->matched_count
= ntohll(ots
->matched_count
);
6498 ofputil_decode_table_stats_reply(struct ofpbuf
*msg
,
6499 struct ofputil_table_stats
*stats
,
6500 struct ofputil_table_features
*features
)
6502 const struct ofp_header
*oh
;
6505 ofpraw_pull_assert(msg
);
6513 memset(stats
, 0, sizeof *stats
);
6514 memset(features
, 0, sizeof *features
);
6515 features
->supports_eviction
= -1;
6516 features
->supports_vacancy_events
= -1;
6518 switch ((enum ofp_version
) oh
->version
) {
6520 return ofputil_decode_ofp10_table_stats(msg
, stats
, features
);
6523 return ofputil_decode_ofp11_table_stats(msg
, stats
, features
);
6526 return ofputil_decode_ofp12_table_stats(msg
, stats
, features
);
6532 return ofputil_decode_ofp13_table_stats(msg
, stats
, features
);
6539 /* ofputil_flow_monitor_request */
6541 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
6542 * ofputil_flow_monitor_request in 'rq'.
6544 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
6545 * message. Calling this function multiple times for a single 'msg' iterates
6546 * through the requests. The caller must initially leave 'msg''s layer
6547 * pointers null and not modify them between calls.
6549 * Returns 0 if successful, EOF if no requests were left in this 'msg',
6550 * otherwise an OFPERR_* value. */
6552 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request
*rq
,
6555 struct nx_flow_monitor_request
*nfmr
;
6559 ofpraw_pull_assert(msg
);
6566 nfmr
= ofpbuf_try_pull(msg
, sizeof *nfmr
);
6568 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW_MONITOR request has %"PRIu32
" "
6569 "leftover bytes at end", msg
->size
);
6570 return OFPERR_OFPBRC_BAD_LEN
;
6573 flags
= ntohs(nfmr
->flags
);
6574 if (!(flags
& (NXFMF_ADD
| NXFMF_DELETE
| NXFMF_MODIFY
))
6575 || flags
& ~(NXFMF_INITIAL
| NXFMF_ADD
| NXFMF_DELETE
6576 | NXFMF_MODIFY
| NXFMF_ACTIONS
| NXFMF_OWN
)) {
6577 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW_MONITOR has bad flags %#"PRIx16
,
6579 return OFPERR_OFPMOFC_BAD_FLAGS
;
6582 if (!is_all_zeros(nfmr
->zeros
, sizeof nfmr
->zeros
)) {
6583 return OFPERR_NXBRC_MUST_BE_ZERO
;
6586 rq
->id
= ntohl(nfmr
->id
);
6588 rq
->out_port
= u16_to_ofp(ntohs(nfmr
->out_port
));
6589 rq
->table_id
= nfmr
->table_id
;
6591 return nx_pull_match(msg
, ntohs(nfmr
->match_len
), &rq
->match
, NULL
, NULL
);
6595 ofputil_append_flow_monitor_request(
6596 const struct ofputil_flow_monitor_request
*rq
, struct ofpbuf
*msg
)
6598 struct nx_flow_monitor_request
*nfmr
;
6603 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST
, OFP10_VERSION
, msg
);
6606 start_ofs
= msg
->size
;
6607 ofpbuf_put_zeros(msg
, sizeof *nfmr
);
6608 match_len
= nx_put_match(msg
, &rq
->match
, htonll(0), htonll(0));
6610 nfmr
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *nfmr
);
6611 nfmr
->id
= htonl(rq
->id
);
6612 nfmr
->flags
= htons(rq
->flags
);
6613 nfmr
->out_port
= htons(ofp_to_u16(rq
->out_port
));
6614 nfmr
->match_len
= htons(match_len
);
6615 nfmr
->table_id
= rq
->table_id
;
6618 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
6619 * into an abstract ofputil_flow_update in 'update'. The caller must have
6620 * initialized update->match to point to space allocated for a match.
6622 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
6623 * actions (except for NXFME_ABBREV, which never includes actions). The caller
6624 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
6625 * will point into the 'ofpacts' buffer.
6627 * Multiple flow updates can be packed into a single OpenFlow message. Calling
6628 * this function multiple times for a single 'msg' iterates through the
6629 * updates. The caller must initially leave 'msg''s layer pointers null and
6630 * not modify them between calls.
6632 * Returns 0 if successful, EOF if no updates were left in this 'msg',
6633 * otherwise an OFPERR_* value. */
6635 ofputil_decode_flow_update(struct ofputil_flow_update
*update
,
6636 struct ofpbuf
*msg
, struct ofpbuf
*ofpacts
)
6638 struct nx_flow_update_header
*nfuh
;
6639 unsigned int length
;
6640 struct ofp_header
*oh
;
6643 ofpraw_pull_assert(msg
);
6646 ofpbuf_clear(ofpacts
);
6651 if (msg
->size
< sizeof(struct nx_flow_update_header
)) {
6658 update
->event
= ntohs(nfuh
->event
);
6659 length
= ntohs(nfuh
->length
);
6660 if (length
> msg
->size
|| length
% 8) {
6664 if (update
->event
== NXFME_ABBREV
) {
6665 struct nx_flow_update_abbrev
*nfua
;
6667 if (length
!= sizeof *nfua
) {
6671 nfua
= ofpbuf_pull(msg
, sizeof *nfua
);
6672 update
->xid
= nfua
->xid
;
6674 } else if (update
->event
== NXFME_ADDED
6675 || update
->event
== NXFME_DELETED
6676 || update
->event
== NXFME_MODIFIED
) {
6677 struct nx_flow_update_full
*nfuf
;
6678 unsigned int actions_len
;
6679 unsigned int match_len
;
6682 if (length
< sizeof *nfuf
) {
6686 nfuf
= ofpbuf_pull(msg
, sizeof *nfuf
);
6687 match_len
= ntohs(nfuf
->match_len
);
6688 if (sizeof *nfuf
+ match_len
> length
) {
6692 update
->reason
= ntohs(nfuf
->reason
);
6693 update
->idle_timeout
= ntohs(nfuf
->idle_timeout
);
6694 update
->hard_timeout
= ntohs(nfuf
->hard_timeout
);
6695 update
->table_id
= nfuf
->table_id
;
6696 update
->cookie
= nfuf
->cookie
;
6697 update
->priority
= ntohs(nfuf
->priority
);
6699 error
= nx_pull_match(msg
, match_len
, update
->match
, NULL
, NULL
);
6704 actions_len
= length
- sizeof *nfuf
- ROUND_UP(match_len
, 8);
6705 error
= ofpacts_pull_openflow_actions(msg
, actions_len
, oh
->version
,
6711 update
->ofpacts
= ofpacts
->data
;
6712 update
->ofpacts_len
= ofpacts
->size
;
6715 VLOG_WARN_RL(&bad_ofmsg_rl
,
6716 "NXST_FLOW_MONITOR reply has bad event %"PRIu16
,
6717 ntohs(nfuh
->event
));
6718 return OFPERR_NXBRC_FM_BAD_EVENT
;
6722 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW_MONITOR reply has %"PRIu32
" "
6723 "leftover bytes at end", msg
->size
);
6724 return OFPERR_OFPBRC_BAD_LEN
;
6728 ofputil_decode_flow_monitor_cancel(const struct ofp_header
*oh
)
6730 const struct nx_flow_monitor_cancel
*cancel
= ofpmsg_body(oh
);
6732 return ntohl(cancel
->id
);
6736 ofputil_encode_flow_monitor_cancel(uint32_t id
)
6738 struct nx_flow_monitor_cancel
*nfmc
;
6741 msg
= ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL
, OFP10_VERSION
, 0);
6742 nfmc
= ofpbuf_put_uninit(msg
, sizeof *nfmc
);
6743 nfmc
->id
= htonl(id
);
6748 ofputil_start_flow_update(struct ovs_list
*replies
)
6752 msg
= ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY
, OFP10_VERSION
,
6755 ovs_list_init(replies
);
6756 ovs_list_push_back(replies
, &msg
->list_node
);
6760 ofputil_append_flow_update(const struct ofputil_flow_update
*update
,
6761 struct ovs_list
*replies
)
6763 enum ofp_version version
= ofpmp_version(replies
);
6764 struct nx_flow_update_header
*nfuh
;
6768 msg
= ofpbuf_from_list(ovs_list_back(replies
));
6769 start_ofs
= msg
->size
;
6771 if (update
->event
== NXFME_ABBREV
) {
6772 struct nx_flow_update_abbrev
*nfua
;
6774 nfua
= ofpbuf_put_zeros(msg
, sizeof *nfua
);
6775 nfua
->xid
= update
->xid
;
6777 struct nx_flow_update_full
*nfuf
;
6780 ofpbuf_put_zeros(msg
, sizeof *nfuf
);
6781 match_len
= nx_put_match(msg
, update
->match
, htonll(0), htonll(0));
6782 ofpacts_put_openflow_actions(update
->ofpacts
, update
->ofpacts_len
, msg
,
6784 nfuf
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *nfuf
);
6785 nfuf
->reason
= htons(update
->reason
);
6786 nfuf
->priority
= htons(update
->priority
);
6787 nfuf
->idle_timeout
= htons(update
->idle_timeout
);
6788 nfuf
->hard_timeout
= htons(update
->hard_timeout
);
6789 nfuf
->match_len
= htons(match_len
);
6790 nfuf
->table_id
= update
->table_id
;
6791 nfuf
->cookie
= update
->cookie
;
6794 nfuh
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *nfuh
);
6795 nfuh
->length
= htons(msg
->size
- start_ofs
);
6796 nfuh
->event
= htons(update
->event
);
6798 ofpmp_postappend(replies
, start_ofs
);
6802 ofputil_encode_packet_out(const struct ofputil_packet_out
*po
,
6803 enum ofputil_protocol protocol
)
6805 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
6809 size
= po
->ofpacts_len
;
6810 if (po
->buffer_id
== UINT32_MAX
) {
6811 size
+= po
->packet_len
;
6814 switch (ofp_version
) {
6815 case OFP10_VERSION
: {
6816 struct ofp10_packet_out
*opo
;
6819 msg
= ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT
, OFP10_VERSION
, size
);
6820 ofpbuf_put_zeros(msg
, sizeof *opo
);
6821 actions_ofs
= msg
->size
;
6822 ofpacts_put_openflow_actions(po
->ofpacts
, po
->ofpacts_len
, msg
,
6826 opo
->buffer_id
= htonl(po
->buffer_id
);
6827 opo
->in_port
= htons(ofp_to_u16(po
->in_port
));
6828 opo
->actions_len
= htons(msg
->size
- actions_ofs
);
6837 case OFP16_VERSION
: {
6838 struct ofp11_packet_out
*opo
;
6841 msg
= ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT
, ofp_version
, size
);
6842 ofpbuf_put_zeros(msg
, sizeof *opo
);
6843 len
= ofpacts_put_openflow_actions(po
->ofpacts
, po
->ofpacts_len
, msg
,
6846 opo
->buffer_id
= htonl(po
->buffer_id
);
6847 opo
->in_port
= ofputil_port_to_ofp11(po
->in_port
);
6848 opo
->actions_len
= htons(len
);
6856 if (po
->buffer_id
== UINT32_MAX
) {
6857 ofpbuf_put(msg
, po
->packet
, po
->packet_len
);
6860 ofpmsg_update_length(msg
);
6865 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
6867 make_echo_request(enum ofp_version ofp_version
)
6869 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST
, ofp_version
,
6873 /* Creates and returns an OFPT_ECHO_REPLY message matching the
6874 * OFPT_ECHO_REQUEST message in 'rq'. */
6876 make_echo_reply(const struct ofp_header
*rq
)
6878 struct ofpbuf rq_buf
= ofpbuf_const_initializer(rq
, ntohs(rq
->length
));
6879 ofpraw_pull_assert(&rq_buf
);
6881 struct ofpbuf
*reply
= ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY
,
6883 ofpbuf_put(reply
, rq_buf
.data
, rq_buf
.size
);
6888 ofputil_encode_barrier_request(enum ofp_version ofp_version
)
6892 switch (ofp_version
) {
6899 type
= OFPRAW_OFPT11_BARRIER_REQUEST
;
6903 type
= OFPRAW_OFPT10_BARRIER_REQUEST
;
6910 return ofpraw_alloc(type
, ofp_version
, 0);
6914 ofputil_frag_handling_to_string(enum ofputil_frag_handling frag
)
6917 case OFPUTIL_FRAG_NORMAL
: return "normal";
6918 case OFPUTIL_FRAG_DROP
: return "drop";
6919 case OFPUTIL_FRAG_REASM
: return "reassemble";
6920 case OFPUTIL_FRAG_NX_MATCH
: return "nx-match";
6927 ofputil_frag_handling_from_string(const char *s
,
6928 enum ofputil_frag_handling
*frag
)
6930 if (!strcasecmp(s
, "normal")) {
6931 *frag
= OFPUTIL_FRAG_NORMAL
;
6932 } else if (!strcasecmp(s
, "drop")) {
6933 *frag
= OFPUTIL_FRAG_DROP
;
6934 } else if (!strcasecmp(s
, "reassemble")) {
6935 *frag
= OFPUTIL_FRAG_REASM
;
6936 } else if (!strcasecmp(s
, "nx-match")) {
6937 *frag
= OFPUTIL_FRAG_NX_MATCH
;
6944 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
6945 * port number and stores the latter in '*ofp10_port', for the purpose of
6946 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
6947 * otherwise an OFPERR_* number. On error, stores OFPP_NONE in '*ofp10_port'.
6949 * See the definition of OFP11_MAX for an explanation of the mapping. */
6951 ofputil_port_from_ofp11(ovs_be32 ofp11_port
, ofp_port_t
*ofp10_port
)
6953 uint32_t ofp11_port_h
= ntohl(ofp11_port
);
6955 if (ofp11_port_h
< ofp_to_u16(OFPP_MAX
)) {
6956 *ofp10_port
= u16_to_ofp(ofp11_port_h
);
6958 } else if (ofp11_port_h
>= ofp11_to_u32(OFPP11_MAX
)) {
6959 *ofp10_port
= u16_to_ofp(ofp11_port_h
- OFPP11_OFFSET
);
6962 *ofp10_port
= OFPP_NONE
;
6963 VLOG_WARN_RL(&bad_ofmsg_rl
, "port %"PRIu32
" is outside the supported "
6964 "range 0 through %d or 0x%"PRIx32
" through 0x%"PRIx32
,
6965 ofp11_port_h
, ofp_to_u16(OFPP_MAX
) - 1,
6966 ofp11_to_u32(OFPP11_MAX
), UINT32_MAX
);
6967 return OFPERR_OFPBAC_BAD_OUT_PORT
;
6971 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
6972 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
6974 * See the definition of OFP11_MAX for an explanation of the mapping. */
6976 ofputil_port_to_ofp11(ofp_port_t ofp10_port
)
6978 return htonl(ofp_to_u16(ofp10_port
) < ofp_to_u16(OFPP_MAX
)
6979 ? ofp_to_u16(ofp10_port
)
6980 : ofp_to_u16(ofp10_port
) + OFPP11_OFFSET
);
6983 #define OFPUTIL_NAMED_PORTS \
6984 OFPUTIL_NAMED_PORT(IN_PORT) \
6985 OFPUTIL_NAMED_PORT(TABLE) \
6986 OFPUTIL_NAMED_PORT(NORMAL) \
6987 OFPUTIL_NAMED_PORT(FLOOD) \
6988 OFPUTIL_NAMED_PORT(ALL) \
6989 OFPUTIL_NAMED_PORT(CONTROLLER) \
6990 OFPUTIL_NAMED_PORT(LOCAL) \
6991 OFPUTIL_NAMED_PORT(ANY) \
6992 OFPUTIL_NAMED_PORT(UNSET)
6994 /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */
6995 #define OFPUTIL_NAMED_PORTS_WITH_NONE \
6996 OFPUTIL_NAMED_PORTS \
6997 OFPUTIL_NAMED_PORT(NONE)
6999 /* Stores the port number represented by 's' into '*portp'. 's' may be an
7000 * integer or, for reserved ports, the standard OpenFlow name for the port
7003 * Returns true if successful, false if 's' is not a valid OpenFlow port number
7004 * or name. The caller should issue an error message in this case, because
7005 * this function usually does not. (This gives the caller an opportunity to
7006 * look up the port name another way, e.g. by contacting the switch and listing
7007 * the names of all its ports).
7009 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
7010 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
7011 * range as described in include/openflow/openflow-1.1.h. */
7013 ofputil_port_from_string(const char *s
, ofp_port_t
*portp
)
7015 unsigned int port32
; /* int is at least 32 bits wide. */
7018 VLOG_WARN("Negative value %s is not a valid port number.", s
);
7022 if (str_to_uint(s
, 10, &port32
)) {
7023 if (port32
< ofp_to_u16(OFPP_MAX
)) {
7025 } else if (port32
< ofp_to_u16(OFPP_FIRST_RESV
)) {
7026 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
7027 "be translated to %u when talking to an OF1.1 or "
7028 "later controller", port32
, port32
+ OFPP11_OFFSET
);
7029 } else if (port32
<= ofp_to_u16(OFPP_LAST_RESV
)) {
7030 char name
[OFP_MAX_PORT_NAME_LEN
];
7032 ofputil_port_to_string(u16_to_ofp(port32
), name
, sizeof name
);
7033 VLOG_WARN_ONCE("referring to port %s as %"PRIu32
" is deprecated "
7034 "for compatibility with OpenFlow 1.1 and later",
7036 } else if (port32
< ofp11_to_u32(OFPP11_MAX
)) {
7037 VLOG_WARN("port %u is outside the supported range 0 through "
7038 "%"PRIx16
" or 0x%x through 0x%"PRIx32
, port32
,
7039 UINT16_MAX
, ofp11_to_u32(OFPP11_MAX
), UINT32_MAX
);
7042 port32
-= OFPP11_OFFSET
;
7045 *portp
= u16_to_ofp(port32
);
7052 static const struct pair pairs
[] = {
7053 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
7054 OFPUTIL_NAMED_PORTS_WITH_NONE
7055 #undef OFPUTIL_NAMED_PORT
7057 const struct pair
*p
;
7059 for (p
= pairs
; p
< &pairs
[ARRAY_SIZE(pairs
)]; p
++) {
7060 if (!strcasecmp(s
, p
->name
)) {
7069 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
7070 * Most ports' string representation is just the port number, but for special
7071 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
7073 ofputil_format_port(ofp_port_t port
, struct ds
*s
)
7075 char name
[OFP_MAX_PORT_NAME_LEN
];
7077 ofputil_port_to_string(port
, name
, sizeof name
);
7078 ds_put_cstr(s
, name
);
7081 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
7082 * representation of OpenFlow port number 'port'. Most ports are represented
7083 * as just the port number, but special ports, e.g. OFPP_LOCAL, are represented
7084 * by name, e.g. "LOCAL". */
7086 ofputil_port_to_string(ofp_port_t port
,
7087 char namebuf
[OFP_MAX_PORT_NAME_LEN
], size_t bufsize
)
7090 #define OFPUTIL_NAMED_PORT(NAME) \
7092 ovs_strlcpy(namebuf, #NAME, bufsize); \
7095 #undef OFPUTIL_NAMED_PORT
7098 snprintf(namebuf
, bufsize
, "%"PRIu16
, port
);
7103 /* Stores the group id represented by 's' into '*group_idp'. 's' may be an
7104 * integer or, for reserved group IDs, the standard OpenFlow name for the group
7105 * (either "ANY" or "ALL").
7107 * Returns true if successful, false if 's' is not a valid OpenFlow group ID or
7110 ofputil_group_from_string(const char *s
, uint32_t *group_idp
)
7112 if (!strcasecmp(s
, "any")) {
7113 *group_idp
= OFPG_ANY
;
7114 } else if (!strcasecmp(s
, "all")) {
7115 *group_idp
= OFPG_ALL
;
7116 } else if (!str_to_uint(s
, 10, group_idp
)) {
7117 VLOG_WARN("%s is not a valid group ID. (Valid group IDs are "
7118 "32-bit nonnegative integers or the keywords ANY or "
7126 /* Appends to 's' a string representation of the OpenFlow group ID 'group_id'.
7127 * Most groups' string representation is just the number, but for special
7128 * groups, e.g. OFPG_ALL, it is the name, e.g. "ALL". */
7130 ofputil_format_group(uint32_t group_id
, struct ds
*s
)
7132 char name
[MAX_GROUP_NAME_LEN
];
7134 ofputil_group_to_string(group_id
, name
, sizeof name
);
7135 ds_put_cstr(s
, name
);
7139 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
7140 * representation of OpenFlow group ID 'group_id'. Most group are represented
7141 * as just their number, but special groups, e.g. OFPG_ALL, are represented
7142 * by name, e.g. "ALL". */
7144 ofputil_group_to_string(uint32_t group_id
,
7145 char namebuf
[MAX_GROUP_NAME_LEN
+ 1], size_t bufsize
)
7149 ovs_strlcpy(namebuf
, "ALL", bufsize
);
7153 ovs_strlcpy(namebuf
, "ANY", bufsize
);
7157 snprintf(namebuf
, bufsize
, "%"PRIu32
, group_id
);
7162 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
7163 * 'ofp_version', tries to pull the first element from the array. If
7164 * successful, initializes '*pp' with an abstract representation of the
7165 * port and returns 0. If no ports remain to be decoded, returns EOF.
7166 * On an error, returns a positive OFPERR_* value. */
7168 ofputil_pull_phy_port(enum ofp_version ofp_version
, struct ofpbuf
*b
,
7169 struct ofputil_phy_port
*pp
)
7171 memset(pp
, 0, sizeof *pp
);
7173 switch (ofp_version
) {
7174 case OFP10_VERSION
: {
7175 const struct ofp10_phy_port
*opp
= ofpbuf_try_pull(b
, sizeof *opp
);
7176 return opp
? ofputil_decode_ofp10_phy_port(pp
, opp
) : EOF
;
7180 case OFP13_VERSION
: {
7181 const struct ofp11_port
*op
= ofpbuf_try_pull(b
, sizeof *op
);
7182 return op
? ofputil_decode_ofp11_port(pp
, op
) : EOF
;
7187 return b
->size
? ofputil_pull_ofp14_port(pp
, b
) : EOF
;
7194 ofputil_normalize_match__(struct match
*match
, bool may_log
)
7197 MAY_NW_ADDR
= 1 << 0, /* nw_src, nw_dst */
7198 MAY_TP_ADDR
= 1 << 1, /* tp_src, tp_dst */
7199 MAY_NW_PROTO
= 1 << 2, /* nw_proto */
7200 MAY_IPVx
= 1 << 3, /* tos, frag, ttl */
7201 MAY_ARP_SHA
= 1 << 4, /* arp_sha */
7202 MAY_ARP_THA
= 1 << 5, /* arp_tha */
7203 MAY_IPV6
= 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
7204 MAY_ND_TARGET
= 1 << 7, /* nd_target */
7205 MAY_MPLS
= 1 << 8, /* mpls label and tc */
7208 struct flow_wildcards wc
;
7210 /* Figure out what fields may be matched. */
7211 if (match
->flow
.dl_type
== htons(ETH_TYPE_IP
)) {
7212 may_match
= MAY_NW_PROTO
| MAY_IPVx
| MAY_NW_ADDR
;
7213 if (match
->flow
.nw_proto
== IPPROTO_TCP
||
7214 match
->flow
.nw_proto
== IPPROTO_UDP
||
7215 match
->flow
.nw_proto
== IPPROTO_SCTP
||
7216 match
->flow
.nw_proto
== IPPROTO_ICMP
) {
7217 may_match
|= MAY_TP_ADDR
;
7219 } else if (match
->flow
.dl_type
== htons(ETH_TYPE_IPV6
)) {
7220 may_match
= MAY_NW_PROTO
| MAY_IPVx
| MAY_IPV6
;
7221 if (match
->flow
.nw_proto
== IPPROTO_TCP
||
7222 match
->flow
.nw_proto
== IPPROTO_UDP
||
7223 match
->flow
.nw_proto
== IPPROTO_SCTP
) {
7224 may_match
|= MAY_TP_ADDR
;
7225 } else if (match
->flow
.nw_proto
== IPPROTO_ICMPV6
) {
7226 may_match
|= MAY_TP_ADDR
;
7227 if (match
->flow
.tp_src
== htons(ND_NEIGHBOR_SOLICIT
)) {
7228 may_match
|= MAY_ND_TARGET
| MAY_ARP_SHA
;
7229 } else if (match
->flow
.tp_src
== htons(ND_NEIGHBOR_ADVERT
)) {
7230 may_match
|= MAY_ND_TARGET
| MAY_ARP_THA
;
7233 } else if (match
->flow
.dl_type
== htons(ETH_TYPE_ARP
) ||
7234 match
->flow
.dl_type
== htons(ETH_TYPE_RARP
)) {
7235 may_match
= MAY_NW_PROTO
| MAY_NW_ADDR
| MAY_ARP_SHA
| MAY_ARP_THA
;
7236 } else if (eth_type_mpls(match
->flow
.dl_type
)) {
7237 may_match
= MAY_MPLS
;
7242 /* Clear the fields that may not be matched. */
7244 if (!(may_match
& MAY_NW_ADDR
)) {
7245 wc
.masks
.nw_src
= wc
.masks
.nw_dst
= htonl(0);
7247 if (!(may_match
& MAY_TP_ADDR
)) {
7248 wc
.masks
.tp_src
= wc
.masks
.tp_dst
= htons(0);
7250 if (!(may_match
& MAY_NW_PROTO
)) {
7251 wc
.masks
.nw_proto
= 0;
7253 if (!(may_match
& MAY_IPVx
)) {
7254 wc
.masks
.nw_tos
= 0;
7255 wc
.masks
.nw_ttl
= 0;
7257 if (!(may_match
& MAY_ARP_SHA
)) {
7258 WC_UNMASK_FIELD(&wc
, arp_sha
);
7260 if (!(may_match
& MAY_ARP_THA
)) {
7261 WC_UNMASK_FIELD(&wc
, arp_tha
);
7263 if (!(may_match
& MAY_IPV6
)) {
7264 wc
.masks
.ipv6_src
= wc
.masks
.ipv6_dst
= in6addr_any
;
7265 wc
.masks
.ipv6_label
= htonl(0);
7267 if (!(may_match
& MAY_ND_TARGET
)) {
7268 wc
.masks
.nd_target
= in6addr_any
;
7270 if (!(may_match
& MAY_MPLS
)) {
7271 memset(wc
.masks
.mpls_lse
, 0, sizeof wc
.masks
.mpls_lse
);
7274 /* Log any changes. */
7275 if (!flow_wildcards_equal(&wc
, &match
->wc
)) {
7276 bool log
= may_log
&& !VLOG_DROP_INFO(&bad_ofmsg_rl
);
7277 char *pre
= log
? match_to_string(match
, OFP_DEFAULT_PRIORITY
) : NULL
;
7280 match_zero_wildcarded_fields(match
);
7283 char *post
= match_to_string(match
, OFP_DEFAULT_PRIORITY
);
7284 VLOG_INFO("normalization changed ofp_match, details:");
7285 VLOG_INFO(" pre: %s", pre
);
7286 VLOG_INFO("post: %s", post
);
7293 /* "Normalizes" the wildcards in 'match'. That means:
7295 * 1. If the type of level N is known, then only the valid fields for that
7296 * level may be specified. For example, ARP does not have a TOS field,
7297 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
7298 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
7299 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
7302 * 2. If the type of level N is not known (or not understood by Open
7303 * vSwitch), then no fields at all for that level may be specified. For
7304 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
7305 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
7308 * If this function changes 'match', it logs a rate-limited informational
7311 ofputil_normalize_match(struct match
*match
)
7313 ofputil_normalize_match__(match
, true);
7316 /* Same as ofputil_normalize_match() without the logging. Thus, this function
7317 * is suitable for a program's internal use, whereas ofputil_normalize_match()
7318 * sense for use on flows received from elsewhere (so that a bug in the program
7319 * that sent them can be reported and corrected). */
7321 ofputil_normalize_match_quiet(struct match
*match
)
7323 ofputil_normalize_match__(match
, false);
7327 parse_value(const char *s
, const char *delimiters
)
7331 /* Iterate until we reach a delimiter.
7333 * strchr(s, '\0') returns s+strlen(s), so this test handles the null
7334 * terminator at the end of 's'. */
7335 while (!strchr(delimiters
, s
[n
])) {
7350 } while (level
> 0);
7358 /* Parses a key or a key-value pair from '*stringp'.
7360 * On success: Stores the key into '*keyp'. Stores the value, if present, into
7361 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
7362 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
7363 * are substrings of '*stringp' created by replacing some of its bytes by null
7364 * terminators. Returns true.
7366 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
7367 * NULL and returns false. */
7369 ofputil_parse_key_value(char **stringp
, char **keyp
, char **valuep
)
7371 /* Skip white space and delimiters. If that brings us to the end of the
7372 * input string, we are done and there are no more key-value pairs. */
7373 *stringp
+= strspn(*stringp
, ", \t\r\n");
7374 if (**stringp
== '\0') {
7375 *keyp
= *valuep
= NULL
;
7379 /* Extract the key and the delimiter that ends the key-value pair or begins
7380 * the value. Advance the input position past the key and delimiter. */
7381 char *key
= *stringp
;
7382 size_t key_len
= strcspn(key
, ":=(, \t\r\n");
7383 char key_delim
= key
[key_len
];
7384 key
[key_len
] = '\0';
7385 *stringp
+= key_len
+ (key_delim
!= '\0');
7387 /* Figure out what delimiter ends the value:
7389 * - If key_delim is ":" or "=", the value extends until white space
7392 * - If key_delim is "(", the value extends until ")".
7394 * If there is no value, we are done. */
7395 const char *value_delims
;
7396 if (key_delim
== ':' || key_delim
== '=') {
7397 value_delims
= ", \t\r\n";
7398 } else if (key_delim
== '(') {
7402 *valuep
= key
+ key_len
; /* Empty string. */
7406 /* Extract the value. Advance the input position past the value and
7408 char *value
= *stringp
;
7409 size_t value_len
= parse_value(value
, value_delims
);
7410 char value_delim
= value
[value_len
];
7411 value
[value_len
] = '\0';
7412 *stringp
+= value_len
+ (value_delim
!= '\0');
7419 /* Encode a dump ports request for 'port', the encoded message
7420 * will be for OpenFlow version 'ofp_version'. Returns message
7421 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
7423 ofputil_encode_dump_ports_request(enum ofp_version ofp_version
, ofp_port_t port
)
7425 struct ofpbuf
*request
;
7427 switch (ofp_version
) {
7428 case OFP10_VERSION
: {
7429 struct ofp10_port_stats_request
*req
;
7430 request
= ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST
, ofp_version
, 0);
7431 req
= ofpbuf_put_zeros(request
, sizeof *req
);
7432 req
->port_no
= htons(ofp_to_u16(port
));
7440 case OFP16_VERSION
: {
7441 struct ofp11_port_stats_request
*req
;
7442 request
= ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST
, ofp_version
, 0);
7443 req
= ofpbuf_put_zeros(request
, sizeof *req
);
7444 req
->port_no
= ofputil_port_to_ofp11(port
);
7455 ofputil_port_stats_to_ofp10(const struct ofputil_port_stats
*ops
,
7456 struct ofp10_port_stats
*ps10
)
7458 ps10
->port_no
= htons(ofp_to_u16(ops
->port_no
));
7459 memset(ps10
->pad
, 0, sizeof ps10
->pad
);
7460 put_32aligned_be64(&ps10
->rx_packets
, htonll(ops
->stats
.rx_packets
));
7461 put_32aligned_be64(&ps10
->tx_packets
, htonll(ops
->stats
.tx_packets
));
7462 put_32aligned_be64(&ps10
->rx_bytes
, htonll(ops
->stats
.rx_bytes
));
7463 put_32aligned_be64(&ps10
->tx_bytes
, htonll(ops
->stats
.tx_bytes
));
7464 put_32aligned_be64(&ps10
->rx_dropped
, htonll(ops
->stats
.rx_dropped
));
7465 put_32aligned_be64(&ps10
->tx_dropped
, htonll(ops
->stats
.tx_dropped
));
7466 put_32aligned_be64(&ps10
->rx_errors
, htonll(ops
->stats
.rx_errors
));
7467 put_32aligned_be64(&ps10
->tx_errors
, htonll(ops
->stats
.tx_errors
));
7468 put_32aligned_be64(&ps10
->rx_frame_err
, htonll(ops
->stats
.rx_frame_errors
));
7469 put_32aligned_be64(&ps10
->rx_over_err
, htonll(ops
->stats
.rx_over_errors
));
7470 put_32aligned_be64(&ps10
->rx_crc_err
, htonll(ops
->stats
.rx_crc_errors
));
7471 put_32aligned_be64(&ps10
->collisions
, htonll(ops
->stats
.collisions
));
7475 ofputil_port_stats_to_ofp11(const struct ofputil_port_stats
*ops
,
7476 struct ofp11_port_stats
*ps11
)
7478 ps11
->port_no
= ofputil_port_to_ofp11(ops
->port_no
);
7479 memset(ps11
->pad
, 0, sizeof ps11
->pad
);
7480 ps11
->rx_packets
= htonll(ops
->stats
.rx_packets
);
7481 ps11
->tx_packets
= htonll(ops
->stats
.tx_packets
);
7482 ps11
->rx_bytes
= htonll(ops
->stats
.rx_bytes
);
7483 ps11
->tx_bytes
= htonll(ops
->stats
.tx_bytes
);
7484 ps11
->rx_dropped
= htonll(ops
->stats
.rx_dropped
);
7485 ps11
->tx_dropped
= htonll(ops
->stats
.tx_dropped
);
7486 ps11
->rx_errors
= htonll(ops
->stats
.rx_errors
);
7487 ps11
->tx_errors
= htonll(ops
->stats
.tx_errors
);
7488 ps11
->rx_frame_err
= htonll(ops
->stats
.rx_frame_errors
);
7489 ps11
->rx_over_err
= htonll(ops
->stats
.rx_over_errors
);
7490 ps11
->rx_crc_err
= htonll(ops
->stats
.rx_crc_errors
);
7491 ps11
->collisions
= htonll(ops
->stats
.collisions
);
7495 ofputil_port_stats_to_ofp13(const struct ofputil_port_stats
*ops
,
7496 struct ofp13_port_stats
*ps13
)
7498 ofputil_port_stats_to_ofp11(ops
, &ps13
->ps
);
7499 ps13
->duration_sec
= htonl(ops
->duration_sec
);
7500 ps13
->duration_nsec
= htonl(ops
->duration_nsec
);
7504 ofputil_append_ofp14_port_stats(const struct ofputil_port_stats
*ops
,
7505 struct ovs_list
*replies
)
7507 struct ofp14_port_stats_prop_ethernet
*eth
;
7508 struct intel_port_stats_rfc2819
*stats_rfc2819
;
7509 struct ofp14_port_stats
*ps14
;
7510 struct ofpbuf
*reply
;
7512 reply
= ofpmp_reserve(replies
, sizeof *ps14
+ sizeof *eth
+
7513 sizeof *stats_rfc2819
);
7515 ps14
= ofpbuf_put_uninit(reply
, sizeof *ps14
);
7516 ps14
->length
= htons(sizeof *ps14
+ sizeof *eth
+
7517 sizeof *stats_rfc2819
);
7518 memset(ps14
->pad
, 0, sizeof ps14
->pad
);
7519 ps14
->port_no
= ofputil_port_to_ofp11(ops
->port_no
);
7520 ps14
->duration_sec
= htonl(ops
->duration_sec
);
7521 ps14
->duration_nsec
= htonl(ops
->duration_nsec
);
7522 ps14
->rx_packets
= htonll(ops
->stats
.rx_packets
);
7523 ps14
->tx_packets
= htonll(ops
->stats
.tx_packets
);
7524 ps14
->rx_bytes
= htonll(ops
->stats
.rx_bytes
);
7525 ps14
->tx_bytes
= htonll(ops
->stats
.tx_bytes
);
7526 ps14
->rx_dropped
= htonll(ops
->stats
.rx_dropped
);
7527 ps14
->tx_dropped
= htonll(ops
->stats
.tx_dropped
);
7528 ps14
->rx_errors
= htonll(ops
->stats
.rx_errors
);
7529 ps14
->tx_errors
= htonll(ops
->stats
.tx_errors
);
7531 eth
= ofpprop_put_zeros(reply
, OFPPSPT14_ETHERNET
, sizeof *eth
);
7532 eth
->rx_frame_err
= htonll(ops
->stats
.rx_frame_errors
);
7533 eth
->rx_over_err
= htonll(ops
->stats
.rx_over_errors
);
7534 eth
->rx_crc_err
= htonll(ops
->stats
.rx_crc_errors
);
7535 eth
->collisions
= htonll(ops
->stats
.collisions
);
7537 uint64_t prop_type
= OFPPROP_EXP(INTEL_VENDOR_ID
,
7538 INTEL_PORT_STATS_RFC2819
);
7540 stats_rfc2819
= ofpprop_put_zeros(reply
, prop_type
,
7541 sizeof *stats_rfc2819
);
7543 memset(stats_rfc2819
->pad
, 0, sizeof stats_rfc2819
->pad
);
7544 stats_rfc2819
->rx_1_to_64_packets
= htonll(ops
->stats
.rx_1_to_64_packets
);
7545 stats_rfc2819
->rx_65_to_127_packets
=
7546 htonll(ops
->stats
.rx_65_to_127_packets
);
7547 stats_rfc2819
->rx_128_to_255_packets
=
7548 htonll(ops
->stats
.rx_128_to_255_packets
);
7549 stats_rfc2819
->rx_256_to_511_packets
=
7550 htonll(ops
->stats
.rx_256_to_511_packets
);
7551 stats_rfc2819
->rx_512_to_1023_packets
=
7552 htonll(ops
->stats
.rx_512_to_1023_packets
);
7553 stats_rfc2819
->rx_1024_to_1522_packets
=
7554 htonll(ops
->stats
.rx_1024_to_1522_packets
);
7555 stats_rfc2819
->rx_1523_to_max_packets
=
7556 htonll(ops
->stats
.rx_1523_to_max_packets
);
7558 stats_rfc2819
->tx_1_to_64_packets
= htonll(ops
->stats
.tx_1_to_64_packets
);
7559 stats_rfc2819
->tx_65_to_127_packets
=
7560 htonll(ops
->stats
.tx_65_to_127_packets
);
7561 stats_rfc2819
->tx_128_to_255_packets
=
7562 htonll(ops
->stats
.tx_128_to_255_packets
);
7563 stats_rfc2819
->tx_256_to_511_packets
=
7564 htonll(ops
->stats
.tx_256_to_511_packets
);
7565 stats_rfc2819
->tx_512_to_1023_packets
=
7566 htonll(ops
->stats
.tx_512_to_1023_packets
);
7567 stats_rfc2819
->tx_1024_to_1522_packets
=
7568 htonll(ops
->stats
.tx_1024_to_1522_packets
);
7569 stats_rfc2819
->tx_1523_to_max_packets
=
7570 htonll(ops
->stats
.tx_1523_to_max_packets
);
7572 stats_rfc2819
->tx_multicast_packets
=
7573 htonll(ops
->stats
.tx_multicast_packets
);
7574 stats_rfc2819
->rx_broadcast_packets
=
7575 htonll(ops
->stats
.rx_broadcast_packets
);
7576 stats_rfc2819
->tx_broadcast_packets
=
7577 htonll(ops
->stats
.tx_broadcast_packets
);
7578 stats_rfc2819
->rx_undersized_errors
=
7579 htonll(ops
->stats
.rx_undersized_errors
);
7580 stats_rfc2819
->rx_oversize_errors
=
7581 htonll(ops
->stats
.rx_oversize_errors
);
7582 stats_rfc2819
->rx_fragmented_errors
=
7583 htonll(ops
->stats
.rx_fragmented_errors
);
7584 stats_rfc2819
->rx_jabber_errors
=
7585 htonll(ops
->stats
.rx_jabber_errors
);
7588 /* Encode a ports stat for 'ops' and append it to 'replies'. */
7590 ofputil_append_port_stat(struct ovs_list
*replies
,
7591 const struct ofputil_port_stats
*ops
)
7593 switch (ofpmp_version(replies
)) {
7594 case OFP13_VERSION
: {
7595 struct ofp13_port_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
7596 ofputil_port_stats_to_ofp13(ops
, reply
);
7600 case OFP11_VERSION
: {
7601 struct ofp11_port_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
7602 ofputil_port_stats_to_ofp11(ops
, reply
);
7606 case OFP10_VERSION
: {
7607 struct ofp10_port_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
7608 ofputil_port_stats_to_ofp10(ops
, reply
);
7615 ofputil_append_ofp14_port_stats(ops
, replies
);
7624 ofputil_port_stats_from_ofp10(struct ofputil_port_stats
*ops
,
7625 const struct ofp10_port_stats
*ps10
)
7628 ops
->port_no
= u16_to_ofp(ntohs(ps10
->port_no
));
7629 ops
->stats
.rx_packets
= ntohll(get_32aligned_be64(&ps10
->rx_packets
));
7630 ops
->stats
.tx_packets
= ntohll(get_32aligned_be64(&ps10
->tx_packets
));
7631 ops
->stats
.rx_bytes
= ntohll(get_32aligned_be64(&ps10
->rx_bytes
));
7632 ops
->stats
.tx_bytes
= ntohll(get_32aligned_be64(&ps10
->tx_bytes
));
7633 ops
->stats
.rx_dropped
= ntohll(get_32aligned_be64(&ps10
->rx_dropped
));
7634 ops
->stats
.tx_dropped
= ntohll(get_32aligned_be64(&ps10
->tx_dropped
));
7635 ops
->stats
.rx_errors
= ntohll(get_32aligned_be64(&ps10
->rx_errors
));
7636 ops
->stats
.tx_errors
= ntohll(get_32aligned_be64(&ps10
->tx_errors
));
7637 ops
->stats
.rx_frame_errors
=
7638 ntohll(get_32aligned_be64(&ps10
->rx_frame_err
));
7639 ops
->stats
.rx_over_errors
= ntohll(get_32aligned_be64(&ps10
->rx_over_err
));
7640 ops
->stats
.rx_crc_errors
= ntohll(get_32aligned_be64(&ps10
->rx_crc_err
));
7641 ops
->stats
.collisions
= ntohll(get_32aligned_be64(&ps10
->collisions
));
7642 ops
->duration_sec
= ops
->duration_nsec
= UINT32_MAX
;
7648 ofputil_port_stats_from_ofp11(struct ofputil_port_stats
*ops
,
7649 const struct ofp11_port_stats
*ps11
)
7653 error
= ofputil_port_from_ofp11(ps11
->port_no
, &ops
->port_no
);
7658 ops
->stats
.rx_packets
= ntohll(ps11
->rx_packets
);
7659 ops
->stats
.tx_packets
= ntohll(ps11
->tx_packets
);
7660 ops
->stats
.rx_bytes
= ntohll(ps11
->rx_bytes
);
7661 ops
->stats
.tx_bytes
= ntohll(ps11
->tx_bytes
);
7662 ops
->stats
.rx_dropped
= ntohll(ps11
->rx_dropped
);
7663 ops
->stats
.tx_dropped
= ntohll(ps11
->tx_dropped
);
7664 ops
->stats
.rx_errors
= ntohll(ps11
->rx_errors
);
7665 ops
->stats
.tx_errors
= ntohll(ps11
->tx_errors
);
7666 ops
->stats
.rx_frame_errors
= ntohll(ps11
->rx_frame_err
);
7667 ops
->stats
.rx_over_errors
= ntohll(ps11
->rx_over_err
);
7668 ops
->stats
.rx_crc_errors
= ntohll(ps11
->rx_crc_err
);
7669 ops
->stats
.collisions
= ntohll(ps11
->collisions
);
7670 ops
->duration_sec
= ops
->duration_nsec
= UINT32_MAX
;
7676 ofputil_port_stats_from_ofp13(struct ofputil_port_stats
*ops
,
7677 const struct ofp13_port_stats
*ps13
)
7679 enum ofperr error
= ofputil_port_stats_from_ofp11(ops
, &ps13
->ps
);
7681 ops
->duration_sec
= ntohl(ps13
->duration_sec
);
7682 ops
->duration_nsec
= ntohl(ps13
->duration_nsec
);
7688 parse_ofp14_port_stats_ethernet_property(const struct ofpbuf
*payload
,
7689 struct ofputil_port_stats
*ops
)
7691 const struct ofp14_port_stats_prop_ethernet
*eth
= payload
->data
;
7693 if (payload
->size
!= sizeof *eth
) {
7694 return OFPERR_OFPBPC_BAD_LEN
;
7697 ops
->stats
.rx_frame_errors
= ntohll(eth
->rx_frame_err
);
7698 ops
->stats
.rx_over_errors
= ntohll(eth
->rx_over_err
);
7699 ops
->stats
.rx_crc_errors
= ntohll(eth
->rx_crc_err
);
7700 ops
->stats
.collisions
= ntohll(eth
->collisions
);
7706 parse_intel_port_stats_rfc2819_property(const struct ofpbuf
*payload
,
7707 struct ofputil_port_stats
*ops
)
7709 const struct intel_port_stats_rfc2819
*rfc2819
= payload
->data
;
7711 if (payload
->size
!= sizeof *rfc2819
) {
7712 return OFPERR_OFPBPC_BAD_LEN
;
7714 ops
->stats
.rx_1_to_64_packets
= ntohll(rfc2819
->rx_1_to_64_packets
);
7715 ops
->stats
.rx_65_to_127_packets
= ntohll(rfc2819
->rx_65_to_127_packets
);
7716 ops
->stats
.rx_128_to_255_packets
= ntohll(rfc2819
->rx_128_to_255_packets
);
7717 ops
->stats
.rx_256_to_511_packets
= ntohll(rfc2819
->rx_256_to_511_packets
);
7718 ops
->stats
.rx_512_to_1023_packets
=
7719 ntohll(rfc2819
->rx_512_to_1023_packets
);
7720 ops
->stats
.rx_1024_to_1522_packets
=
7721 ntohll(rfc2819
->rx_1024_to_1522_packets
);
7722 ops
->stats
.rx_1523_to_max_packets
=
7723 ntohll(rfc2819
->rx_1523_to_max_packets
);
7725 ops
->stats
.tx_1_to_64_packets
= ntohll(rfc2819
->tx_1_to_64_packets
);
7726 ops
->stats
.tx_65_to_127_packets
= ntohll(rfc2819
->tx_65_to_127_packets
);
7727 ops
->stats
.tx_128_to_255_packets
= ntohll(rfc2819
->tx_128_to_255_packets
);
7728 ops
->stats
.tx_256_to_511_packets
= ntohll(rfc2819
->tx_256_to_511_packets
);
7729 ops
->stats
.tx_512_to_1023_packets
=
7730 ntohll(rfc2819
->tx_512_to_1023_packets
);
7731 ops
->stats
.tx_1024_to_1522_packets
=
7732 ntohll(rfc2819
->tx_1024_to_1522_packets
);
7733 ops
->stats
.tx_1523_to_max_packets
=
7734 ntohll(rfc2819
->tx_1523_to_max_packets
);
7736 ops
->stats
.tx_multicast_packets
= ntohll(rfc2819
->tx_multicast_packets
);
7737 ops
->stats
.rx_broadcast_packets
= ntohll(rfc2819
->rx_broadcast_packets
);
7738 ops
->stats
.tx_broadcast_packets
= ntohll(rfc2819
->tx_broadcast_packets
);
7739 ops
->stats
.rx_undersized_errors
= ntohll(rfc2819
->rx_undersized_errors
);
7741 ops
->stats
.rx_oversize_errors
= ntohll(rfc2819
->rx_oversize_errors
);
7742 ops
->stats
.rx_fragmented_errors
= ntohll(rfc2819
->rx_fragmented_errors
);
7743 ops
->stats
.rx_jabber_errors
= ntohll(rfc2819
->rx_jabber_errors
);
7749 parse_intel_port_stats_property(const struct ofpbuf
*payload
,
7751 struct ofputil_port_stats
*ops
)
7756 case INTEL_PORT_STATS_RFC2819
:
7757 error
= parse_intel_port_stats_rfc2819_property(payload
, ops
);
7760 error
= OFPERR_OFPBPC_BAD_EXP_TYPE
;
7768 ofputil_pull_ofp14_port_stats(struct ofputil_port_stats
*ops
,
7771 const struct ofp14_port_stats
*ps14
= ofpbuf_try_pull(msg
, sizeof *ps14
);
7773 return OFPERR_OFPBRC_BAD_LEN
;
7776 size_t len
= ntohs(ps14
->length
);
7777 if (len
< sizeof *ps14
|| len
- sizeof *ps14
> msg
->size
) {
7778 return OFPERR_OFPBRC_BAD_LEN
;
7780 len
-= sizeof *ps14
;
7782 enum ofperr error
= ofputil_port_from_ofp11(ps14
->port_no
, &ops
->port_no
);
7787 ops
->duration_sec
= ntohl(ps14
->duration_sec
);
7788 ops
->duration_nsec
= ntohl(ps14
->duration_nsec
);
7789 ops
->stats
.rx_packets
= ntohll(ps14
->rx_packets
);
7790 ops
->stats
.tx_packets
= ntohll(ps14
->tx_packets
);
7791 ops
->stats
.rx_bytes
= ntohll(ps14
->rx_bytes
);
7792 ops
->stats
.tx_bytes
= ntohll(ps14
->tx_bytes
);
7793 ops
->stats
.rx_dropped
= ntohll(ps14
->rx_dropped
);
7794 ops
->stats
.tx_dropped
= ntohll(ps14
->tx_dropped
);
7795 ops
->stats
.rx_errors
= ntohll(ps14
->rx_errors
);
7796 ops
->stats
.tx_errors
= ntohll(ps14
->tx_errors
);
7799 struct ofpbuf properties
= ofpbuf_const_initializer(ofpbuf_pull(msg
, len
),
7801 while (properties
.size
> 0) {
7802 struct ofpbuf payload
;
7806 error
= ofpprop_pull(&properties
, &payload
, &type
);
7811 case OFPPSPT14_ETHERNET
:
7812 error
= parse_ofp14_port_stats_ethernet_property(&payload
, ops
);
7814 case OFPPROP_EXP(INTEL_VENDOR_ID
, INTEL_PORT_STATS_RFC2819
):
7815 error
= parse_intel_port_stats_property(&payload
,
7816 INTEL_PORT_STATS_RFC2819
,
7820 error
= OFPPROP_UNKNOWN(true, "port stats", type
);
7832 /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
7835 ofputil_count_port_stats(const struct ofp_header
*oh
)
7837 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
7838 ofpraw_pull_assert(&b
);
7840 for (size_t n
= 0; ; n
++) {
7841 struct ofputil_port_stats ps
;
7842 if (ofputil_decode_port_stats(&ps
, &b
)) {
7848 /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
7849 * ofputil_port_stats in 'ps'.
7851 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
7852 * message. Calling this function multiple times for a single 'msg' iterates
7853 * through the replies. The caller must initially leave 'msg''s layer pointers
7854 * null and not modify them between calls.
7856 * Returns 0 if successful, EOF if no replies were left in this 'msg',
7857 * otherwise a positive errno value. */
7859 ofputil_decode_port_stats(struct ofputil_port_stats
*ps
, struct ofpbuf
*msg
)
7864 memset(&(ps
->stats
), 0xFF, sizeof (ps
->stats
));
7866 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
7867 : ofpraw_pull(&raw
, msg
));
7874 } else if (raw
== OFPRAW_OFPST14_PORT_REPLY
) {
7875 return ofputil_pull_ofp14_port_stats(ps
, msg
);
7876 } else if (raw
== OFPRAW_OFPST13_PORT_REPLY
) {
7877 const struct ofp13_port_stats
*ps13
;
7878 ps13
= ofpbuf_try_pull(msg
, sizeof *ps13
);
7882 return ofputil_port_stats_from_ofp13(ps
, ps13
);
7883 } else if (raw
== OFPRAW_OFPST11_PORT_REPLY
) {
7884 const struct ofp11_port_stats
*ps11
;
7886 ps11
= ofpbuf_try_pull(msg
, sizeof *ps11
);
7890 return ofputil_port_stats_from_ofp11(ps
, ps11
);
7891 } else if (raw
== OFPRAW_OFPST10_PORT_REPLY
) {
7892 const struct ofp10_port_stats
*ps10
;
7894 ps10
= ofpbuf_try_pull(msg
, sizeof *ps10
);
7898 return ofputil_port_stats_from_ofp10(ps
, ps10
);
7904 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_PORT reply has %"PRIu32
" leftover "
7905 "bytes at end", msg
->size
);
7906 return OFPERR_OFPBRC_BAD_LEN
;
7909 /* Parse a port status request message into a 16 bit OpenFlow 1.0
7910 * port number and stores the latter in '*ofp10_port'.
7911 * Returns 0 if successful, otherwise an OFPERR_* number. */
7913 ofputil_decode_port_stats_request(const struct ofp_header
*request
,
7914 ofp_port_t
*ofp10_port
)
7916 switch ((enum ofp_version
)request
->version
) {
7922 case OFP11_VERSION
: {
7923 const struct ofp11_port_stats_request
*psr11
= ofpmsg_body(request
);
7924 return ofputil_port_from_ofp11(psr11
->port_no
, ofp10_port
);
7927 case OFP10_VERSION
: {
7928 const struct ofp10_port_stats_request
*psr10
= ofpmsg_body(request
);
7929 *ofp10_port
= u16_to_ofp(ntohs(psr10
->port_no
));
7939 ofputil_ipfix_stats_to_reply(const struct ofputil_ipfix_stats
*ois
,
7940 struct nx_ipfix_stats_reply
*reply
)
7942 reply
->collector_set_id
= htonl(ois
->collector_set_id
);
7943 reply
->total_flows
= htonll(ois
->total_flows
);
7944 reply
->current_flows
= htonll(ois
->current_flows
);
7945 reply
->pkts
= htonll(ois
->pkts
);
7946 reply
->ipv4_pkts
= htonll(ois
->ipv4_pkts
);
7947 reply
->ipv6_pkts
= htonll(ois
->ipv6_pkts
);
7948 reply
->error_pkts
= htonll(ois
->error_pkts
);
7949 reply
->ipv4_error_pkts
= htonll(ois
->ipv4_error_pkts
);
7950 reply
->ipv6_error_pkts
= htonll(ois
->ipv6_error_pkts
);
7951 reply
->tx_pkts
= htonll(ois
->tx_pkts
);
7952 reply
->tx_errors
= htonll(ois
->tx_errors
);
7953 memset(reply
->pad
, 0, sizeof reply
->pad
);
7956 /* Encode a ipfix stat for 'ois' and append it to 'replies'. */
7958 ofputil_append_ipfix_stat(struct ovs_list
*replies
,
7959 const struct ofputil_ipfix_stats
*ois
)
7961 struct nx_ipfix_stats_reply
*reply
= ofpmp_append(replies
, sizeof *reply
);
7962 ofputil_ipfix_stats_to_reply(ois
, reply
);
7966 ofputil_ipfix_stats_from_nx(struct ofputil_ipfix_stats
*is
,
7967 const struct nx_ipfix_stats_reply
*reply
)
7969 is
->collector_set_id
= ntohl(reply
->collector_set_id
);
7970 is
->total_flows
= ntohll(reply
->total_flows
);
7971 is
->current_flows
= ntohll(reply
->current_flows
);
7972 is
->pkts
= ntohll(reply
->pkts
);
7973 is
->ipv4_pkts
= ntohll(reply
->ipv4_pkts
);
7974 is
->ipv6_pkts
= ntohll(reply
->ipv6_pkts
);
7975 is
->error_pkts
= ntohll(reply
->error_pkts
);
7976 is
->ipv4_error_pkts
= ntohll(reply
->ipv4_error_pkts
);
7977 is
->ipv6_error_pkts
= ntohll(reply
->ipv6_error_pkts
);
7978 is
->tx_pkts
= ntohll(reply
->tx_pkts
);
7979 is
->tx_errors
= ntohll(reply
->tx_errors
);
7985 ofputil_pull_ipfix_stats(struct ofputil_ipfix_stats
*is
, struct ofpbuf
*msg
)
7990 memset(is
, 0xFF, sizeof (*is
));
7992 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
7993 : ofpraw_pull(&raw
, msg
));
8000 } else if (raw
== OFPRAW_NXST_IPFIX_BRIDGE_REPLY
||
8001 raw
== OFPRAW_NXST_IPFIX_FLOW_REPLY
) {
8002 struct nx_ipfix_stats_reply
*reply
;
8004 reply
= ofpbuf_try_pull(msg
, sizeof *reply
);
8005 return ofputil_ipfix_stats_from_nx(is
, reply
);
8012 /* Returns the number of ipfix stats elements in
8013 * OFPTYPE_IPFIX_BRIDGE_STATS_REPLY or OFPTYPE_IPFIX_FLOW_STATS_REPLY
8016 ofputil_count_ipfix_stats(const struct ofp_header
*oh
)
8018 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
8019 ofpraw_pull_assert(&b
);
8021 return b
.size
/ sizeof(struct ofputil_ipfix_stats
);
8024 /* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */
8026 ofputil_bucket_list_destroy(struct ovs_list
*buckets
)
8028 struct ofputil_bucket
*bucket
;
8030 LIST_FOR_EACH_POP (bucket
, list_node
, buckets
) {
8031 free(bucket
->ofpacts
);
8036 /* Clones 'bucket' and its ofpacts data */
8037 static struct ofputil_bucket
*
8038 ofputil_bucket_clone_data(const struct ofputil_bucket
*bucket
)
8040 struct ofputil_bucket
*new;
8042 new = xmemdup(bucket
, sizeof *bucket
);
8043 new->ofpacts
= xmemdup(bucket
->ofpacts
, bucket
->ofpacts_len
);
8048 /* Clones each of the buckets in the list 'src' appending them
8049 * in turn to 'dest' which should be an initialised list.
8050 * An exception is that if the pointer value of a bucket in 'src'
8051 * matches 'skip' then it is not cloned or appended to 'dest'.
8052 * This allows all of 'src' or 'all of 'src' except 'skip' to
8053 * be cloned and appended to 'dest'. */
8055 ofputil_bucket_clone_list(struct ovs_list
*dest
, const struct ovs_list
*src
,
8056 const struct ofputil_bucket
*skip
)
8058 struct ofputil_bucket
*bucket
;
8060 LIST_FOR_EACH (bucket
, list_node
, src
) {
8061 struct ofputil_bucket
*new_bucket
;
8063 if (bucket
== skip
) {
8067 new_bucket
= ofputil_bucket_clone_data(bucket
);
8068 ovs_list_push_back(dest
, &new_bucket
->list_node
);
8072 /* Find a bucket in the list 'buckets' whose bucket id is 'bucket_id'
8073 * Returns the first bucket found or NULL if no buckets are found. */
8074 struct ofputil_bucket
*
8075 ofputil_bucket_find(const struct ovs_list
*buckets
, uint32_t bucket_id
)
8077 struct ofputil_bucket
*bucket
;
8079 if (bucket_id
> OFPG15_BUCKET_MAX
) {
8083 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
8084 if (bucket
->bucket_id
== bucket_id
) {
8092 /* Returns true if more than one bucket in the list 'buckets'
8093 * have the same bucket id. Returns false otherwise. */
8095 ofputil_bucket_check_duplicate_id(const struct ovs_list
*buckets
)
8097 struct ofputil_bucket
*i
, *j
;
8099 LIST_FOR_EACH (i
, list_node
, buckets
) {
8100 LIST_FOR_EACH_REVERSE (j
, list_node
, buckets
) {
8104 if (i
->bucket_id
== j
->bucket_id
) {
8113 /* Returns the bucket at the front of the list 'buckets'.
8114 * Undefined if 'buckets is empty. */
8115 struct ofputil_bucket
*
8116 ofputil_bucket_list_front(const struct ovs_list
*buckets
)
8118 static struct ofputil_bucket
*bucket
;
8120 ASSIGN_CONTAINER(bucket
, ovs_list_front(buckets
), list_node
);
8125 /* Returns the bucket at the back of the list 'buckets'.
8126 * Undefined if 'buckets is empty. */
8127 struct ofputil_bucket
*
8128 ofputil_bucket_list_back(const struct ovs_list
*buckets
)
8130 static struct ofputil_bucket
*bucket
;
8132 ASSIGN_CONTAINER(bucket
, ovs_list_back(buckets
), list_node
);
8137 /* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version',
8138 * that requests stats for group 'group_id'. (Use OFPG_ALL to request stats
8141 * Group statistics include packet and byte counts for each group. */
8143 ofputil_encode_group_stats_request(enum ofp_version ofp_version
,
8146 struct ofpbuf
*request
;
8148 switch (ofp_version
) {
8150 ovs_fatal(0, "dump-group-stats needs OpenFlow 1.1 or later "
8151 "(\'-O OpenFlow11\')");
8157 case OFP16_VERSION
: {
8158 struct ofp11_group_stats_request
*req
;
8159 request
= ofpraw_alloc(OFPRAW_OFPST11_GROUP_REQUEST
, ofp_version
, 0);
8160 req
= ofpbuf_put_zeros(request
, sizeof *req
);
8161 req
->group_id
= htonl(group_id
);
8172 ofputil_uninit_group_desc(struct ofputil_group_desc
*gd
)
8174 ofputil_bucket_list_destroy(&gd
->buckets
);
8175 ofputil_group_properties_destroy(&gd
->props
);
8178 /* Decodes the OpenFlow group description request in 'oh', returning the group
8179 * whose description is requested, or OFPG_ALL if stats for all groups was
8182 ofputil_decode_group_desc_request(const struct ofp_header
*oh
)
8184 struct ofpbuf request
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
8185 enum ofpraw raw
= ofpraw_pull_assert(&request
);
8186 if (raw
== OFPRAW_OFPST11_GROUP_DESC_REQUEST
) {
8188 } else if (raw
== OFPRAW_OFPST15_GROUP_DESC_REQUEST
) {
8189 ovs_be32
*group_id
= ofpbuf_pull(&request
, sizeof *group_id
);
8190 return ntohl(*group_id
);
8196 /* Returns an OpenFlow group description request for OpenFlow version
8197 * 'ofp_version', that requests stats for group 'group_id'. Use OFPG_ALL to
8198 * request stats for all groups (OpenFlow 1.4 and earlier always request all
8201 * Group descriptions include the bucket and action configuration for each
8204 ofputil_encode_group_desc_request(enum ofp_version ofp_version
,
8207 struct ofpbuf
*request
;
8209 switch (ofp_version
) {
8211 ovs_fatal(0, "dump-groups needs OpenFlow 1.1 or later "
8212 "(\'-O OpenFlow11\')");
8217 request
= ofpraw_alloc(OFPRAW_OFPST11_GROUP_DESC_REQUEST
,
8221 case OFP16_VERSION
: {
8222 struct ofp15_group_desc_request
*req
;
8223 request
= ofpraw_alloc(OFPRAW_OFPST15_GROUP_DESC_REQUEST
,
8225 req
= ofpbuf_put_zeros(request
, sizeof *req
);
8226 req
->group_id
= htonl(group_id
);
8237 ofputil_group_bucket_counters_to_ofp11(const struct ofputil_group_stats
*gs
,
8238 struct ofp11_bucket_counter bucket_cnts
[])
8242 for (i
= 0; i
< gs
->n_buckets
; i
++) {
8243 bucket_cnts
[i
].packet_count
= htonll(gs
->bucket_stats
[i
].packet_count
);
8244 bucket_cnts
[i
].byte_count
= htonll(gs
->bucket_stats
[i
].byte_count
);
8249 ofputil_group_stats_to_ofp11(const struct ofputil_group_stats
*gs
,
8250 struct ofp11_group_stats
*gs11
, size_t length
,
8251 struct ofp11_bucket_counter bucket_cnts
[])
8253 memset(gs11
, 0, sizeof *gs11
);
8254 gs11
->length
= htons(length
);
8255 gs11
->group_id
= htonl(gs
->group_id
);
8256 gs11
->ref_count
= htonl(gs
->ref_count
);
8257 gs11
->packet_count
= htonll(gs
->packet_count
);
8258 gs11
->byte_count
= htonll(gs
->byte_count
);
8259 ofputil_group_bucket_counters_to_ofp11(gs
, bucket_cnts
);
8263 ofputil_group_stats_to_ofp13(const struct ofputil_group_stats
*gs
,
8264 struct ofp13_group_stats
*gs13
, size_t length
,
8265 struct ofp11_bucket_counter bucket_cnts
[])
8267 ofputil_group_stats_to_ofp11(gs
, &gs13
->gs
, length
, bucket_cnts
);
8268 gs13
->duration_sec
= htonl(gs
->duration_sec
);
8269 gs13
->duration_nsec
= htonl(gs
->duration_nsec
);
8273 /* Encodes 'gs' properly for the format of the list of group statistics
8274 * replies already begun in 'replies' and appends it to the list. 'replies'
8275 * must have originally been initialized with ofpmp_init(). */
8277 ofputil_append_group_stats(struct ovs_list
*replies
,
8278 const struct ofputil_group_stats
*gs
)
8280 size_t bucket_counter_size
;
8281 struct ofp11_bucket_counter
*bucket_counters
;
8284 bucket_counter_size
= gs
->n_buckets
* sizeof(struct ofp11_bucket_counter
);
8286 switch (ofpmp_version(replies
)) {
8288 case OFP12_VERSION
:{
8289 struct ofp11_group_stats
*gs11
;
8291 length
= sizeof *gs11
+ bucket_counter_size
;
8292 gs11
= ofpmp_append(replies
, length
);
8293 bucket_counters
= (struct ofp11_bucket_counter
*)(gs11
+ 1);
8294 ofputil_group_stats_to_ofp11(gs
, gs11
, length
, bucket_counters
);
8301 case OFP16_VERSION
: {
8302 struct ofp13_group_stats
*gs13
;
8304 length
= sizeof *gs13
+ bucket_counter_size
;
8305 gs13
= ofpmp_append(replies
, length
);
8306 bucket_counters
= (struct ofp11_bucket_counter
*)(gs13
+ 1);
8307 ofputil_group_stats_to_ofp13(gs
, gs13
, length
, bucket_counters
);
8316 /* Returns an OpenFlow group features request for OpenFlow version
8319 ofputil_encode_group_features_request(enum ofp_version ofp_version
)
8321 struct ofpbuf
*request
= NULL
;
8323 switch (ofp_version
) {
8326 ovs_fatal(0, "dump-group-features needs OpenFlow 1.2 or later "
8327 "(\'-O OpenFlow12\')");
8333 request
= ofpraw_alloc(OFPRAW_OFPST12_GROUP_FEATURES_REQUEST
,
8343 /* Returns a OpenFlow message that encodes 'features' properly as a reply to
8344 * group features request 'request'. */
8346 ofputil_encode_group_features_reply(
8347 const struct ofputil_group_features
*features
,
8348 const struct ofp_header
*request
)
8350 struct ofp12_group_features_stats
*ogf
;
8351 struct ofpbuf
*reply
;
8354 reply
= ofpraw_alloc_xid(OFPRAW_OFPST12_GROUP_FEATURES_REPLY
,
8355 request
->version
, request
->xid
, 0);
8356 ogf
= ofpbuf_put_zeros(reply
, sizeof *ogf
);
8357 ogf
->types
= htonl(features
->types
);
8358 ogf
->capabilities
= htonl(features
->capabilities
);
8359 for (i
= 0; i
< OFPGT12_N_TYPES
; i
++) {
8360 ogf
->max_groups
[i
] = htonl(features
->max_groups
[i
]);
8361 ogf
->actions
[i
] = ofpact_bitmap_to_openflow(features
->ofpacts
[i
],
8368 /* Decodes group features reply 'oh' into 'features'. */
8370 ofputil_decode_group_features_reply(const struct ofp_header
*oh
,
8371 struct ofputil_group_features
*features
)
8373 const struct ofp12_group_features_stats
*ogf
= ofpmsg_body(oh
);
8376 features
->types
= ntohl(ogf
->types
);
8377 features
->capabilities
= ntohl(ogf
->capabilities
);
8378 for (i
= 0; i
< OFPGT12_N_TYPES
; i
++) {
8379 features
->max_groups
[i
] = ntohl(ogf
->max_groups
[i
]);
8380 features
->ofpacts
[i
] = ofpact_bitmap_from_openflow(
8381 ogf
->actions
[i
], oh
->version
);
8385 /* Parse a group status request message into a 32 bit OpenFlow 1.1
8386 * group ID and stores the latter in '*group_id'.
8387 * Returns 0 if successful, otherwise an OFPERR_* number. */
8389 ofputil_decode_group_stats_request(const struct ofp_header
*request
,
8392 const struct ofp11_group_stats_request
*gsr11
= ofpmsg_body(request
);
8393 *group_id
= ntohl(gsr11
->group_id
);
8397 /* Converts a group stats reply in 'msg' into an abstract ofputil_group_stats
8398 * in 'gs'. Assigns freshly allocated memory to gs->bucket_stats for the
8399 * caller to eventually free.
8401 * Multiple group stats replies can be packed into a single OpenFlow message.
8402 * Calling this function multiple times for a single 'msg' iterates through the
8403 * replies. The caller must initially leave 'msg''s layer pointers null and
8404 * not modify them between calls.
8406 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8407 * otherwise a positive errno value. */
8409 ofputil_decode_group_stats_reply(struct ofpbuf
*msg
,
8410 struct ofputil_group_stats
*gs
)
8412 struct ofp11_bucket_counter
*obc
;
8413 struct ofp11_group_stats
*ogs11
;
8420 gs
->bucket_stats
= NULL
;
8421 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
8422 : ofpraw_pull(&raw
, msg
));
8431 if (raw
== OFPRAW_OFPST11_GROUP_REPLY
) {
8432 base_len
= sizeof *ogs11
;
8433 ogs11
= ofpbuf_try_pull(msg
, sizeof *ogs11
);
8434 gs
->duration_sec
= gs
->duration_nsec
= UINT32_MAX
;
8435 } else if (raw
== OFPRAW_OFPST13_GROUP_REPLY
) {
8436 struct ofp13_group_stats
*ogs13
;
8438 base_len
= sizeof *ogs13
;
8439 ogs13
= ofpbuf_try_pull(msg
, sizeof *ogs13
);
8442 gs
->duration_sec
= ntohl(ogs13
->duration_sec
);
8443 gs
->duration_nsec
= ntohl(ogs13
->duration_nsec
);
8452 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s reply has %"PRIu32
" leftover bytes at end",
8453 ofpraw_get_name(raw
), msg
->size
);
8454 return OFPERR_OFPBRC_BAD_LEN
;
8456 length
= ntohs(ogs11
->length
);
8457 if (length
< sizeof base_len
) {
8458 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s reply claims invalid length %"PRIuSIZE
,
8459 ofpraw_get_name(raw
), length
);
8460 return OFPERR_OFPBRC_BAD_LEN
;
8463 gs
->group_id
= ntohl(ogs11
->group_id
);
8464 gs
->ref_count
= ntohl(ogs11
->ref_count
);
8465 gs
->packet_count
= ntohll(ogs11
->packet_count
);
8466 gs
->byte_count
= ntohll(ogs11
->byte_count
);
8468 gs
->n_buckets
= (length
- base_len
) / sizeof *obc
;
8469 obc
= ofpbuf_try_pull(msg
, gs
->n_buckets
* sizeof *obc
);
8471 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s reply has %"PRIu32
" leftover bytes at end",
8472 ofpraw_get_name(raw
), msg
->size
);
8473 return OFPERR_OFPBRC_BAD_LEN
;
8476 gs
->bucket_stats
= xmalloc(gs
->n_buckets
* sizeof *gs
->bucket_stats
);
8477 for (i
= 0; i
< gs
->n_buckets
; i
++) {
8478 gs
->bucket_stats
[i
].packet_count
= ntohll(obc
[i
].packet_count
);
8479 gs
->bucket_stats
[i
].byte_count
= ntohll(obc
[i
].byte_count
);
8486 ofputil_put_ofp11_bucket(const struct ofputil_bucket
*bucket
,
8487 struct ofpbuf
*openflow
, enum ofp_version ofp_version
)
8489 struct ofp11_bucket
*ob
;
8492 start
= openflow
->size
;
8493 ofpbuf_put_zeros(openflow
, sizeof *ob
);
8494 ofpacts_put_openflow_actions(bucket
->ofpacts
, bucket
->ofpacts_len
,
8495 openflow
, ofp_version
);
8496 ob
= ofpbuf_at_assert(openflow
, start
, sizeof *ob
);
8497 ob
->len
= htons(openflow
->size
- start
);
8498 ob
->weight
= htons(bucket
->weight
);
8499 ob
->watch_port
= ofputil_port_to_ofp11(bucket
->watch_port
);
8500 ob
->watch_group
= htonl(bucket
->watch_group
);
8504 ofputil_put_ofp15_bucket(const struct ofputil_bucket
*bucket
,
8505 uint32_t bucket_id
, enum ofp11_group_type group_type
,
8506 struct ofpbuf
*openflow
, enum ofp_version ofp_version
)
8508 struct ofp15_bucket
*ob
;
8509 size_t start
, actions_start
, actions_len
;
8511 start
= openflow
->size
;
8512 ofpbuf_put_zeros(openflow
, sizeof *ob
);
8514 actions_start
= openflow
->size
;
8515 ofpacts_put_openflow_actions(bucket
->ofpacts
, bucket
->ofpacts_len
,
8516 openflow
, ofp_version
);
8517 actions_len
= openflow
->size
- actions_start
;
8519 if (group_type
== OFPGT11_SELECT
) {
8520 ofpprop_put_u16(openflow
, OFPGBPT15_WEIGHT
, bucket
->weight
);
8522 if (bucket
->watch_port
!= OFPP_ANY
) {
8523 ofpprop_put_be32(openflow
, OFPGBPT15_WATCH_PORT
,
8524 ofputil_port_to_ofp11(bucket
->watch_port
));
8526 if (bucket
->watch_group
!= OFPG_ANY
) {
8527 ofpprop_put_u32(openflow
, OFPGBPT15_WATCH_GROUP
, bucket
->watch_group
);
8530 ob
= ofpbuf_at_assert(openflow
, start
, sizeof *ob
);
8531 ob
->len
= htons(openflow
->size
- start
);
8532 ob
->action_array_len
= htons(actions_len
);
8533 ob
->bucket_id
= htonl(bucket_id
);
8537 ofputil_put_group_prop_ntr_selection_method(enum ofp_version ofp_version
,
8538 const struct ofputil_group_props
*gp
,
8539 struct ofpbuf
*openflow
)
8541 struct ntr_group_prop_selection_method
*prop
;
8544 start
= openflow
->size
;
8545 ofpbuf_put_zeros(openflow
, sizeof *prop
);
8546 oxm_put_field_array(openflow
, &gp
->fields
, ofp_version
);
8547 prop
= ofpbuf_at_assert(openflow
, start
, sizeof *prop
);
8548 prop
->type
= htons(OFPGPT15_EXPERIMENTER
);
8549 prop
->experimenter
= htonl(NTR_VENDOR_ID
);
8550 prop
->exp_type
= htonl(NTRT_SELECTION_METHOD
);
8551 strcpy(prop
->selection_method
, gp
->selection_method
);
8552 prop
->selection_method_param
= htonll(gp
->selection_method_param
);
8553 ofpprop_end(openflow
, start
);
8557 ofputil_append_ofp11_group_desc_reply(const struct ofputil_group_desc
*gds
,
8558 const struct ovs_list
*buckets
,
8559 struct ovs_list
*replies
,
8560 enum ofp_version version
)
8562 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
8563 struct ofp11_group_desc_stats
*ogds
;
8564 struct ofputil_bucket
*bucket
;
8567 start_ogds
= reply
->size
;
8568 ofpbuf_put_zeros(reply
, sizeof *ogds
);
8569 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
8570 ofputil_put_ofp11_bucket(bucket
, reply
, version
);
8572 ogds
= ofpbuf_at_assert(reply
, start_ogds
, sizeof *ogds
);
8573 ogds
->length
= htons(reply
->size
- start_ogds
);
8574 ogds
->type
= gds
->type
;
8575 ogds
->group_id
= htonl(gds
->group_id
);
8577 ofpmp_postappend(replies
, start_ogds
);
8581 ofputil_append_ofp15_group_desc_reply(const struct ofputil_group_desc
*gds
,
8582 const struct ovs_list
*buckets
,
8583 struct ovs_list
*replies
,
8584 enum ofp_version version
)
8586 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
8587 struct ofp15_group_desc_stats
*ogds
;
8588 struct ofputil_bucket
*bucket
;
8589 size_t start_ogds
, start_buckets
;
8591 start_ogds
= reply
->size
;
8592 ofpbuf_put_zeros(reply
, sizeof *ogds
);
8593 start_buckets
= reply
->size
;
8594 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
8595 ofputil_put_ofp15_bucket(bucket
, bucket
->bucket_id
,
8596 gds
->type
, reply
, version
);
8598 ogds
= ofpbuf_at_assert(reply
, start_ogds
, sizeof *ogds
);
8599 ogds
->type
= gds
->type
;
8600 ogds
->group_id
= htonl(gds
->group_id
);
8601 ogds
->bucket_list_len
= htons(reply
->size
- start_buckets
);
8603 /* Add group properties */
8604 if (gds
->props
.selection_method
[0]) {
8605 ofputil_put_group_prop_ntr_selection_method(version
, &gds
->props
,
8608 ogds
= ofpbuf_at_assert(reply
, start_ogds
, sizeof *ogds
);
8609 ogds
->length
= htons(reply
->size
- start_ogds
);
8611 ofpmp_postappend(replies
, start_ogds
);
8614 /* Appends a group stats reply that contains the data in 'gds' to those already
8615 * present in the list of ofpbufs in 'replies'. 'replies' should have been
8616 * initialized with ofpmp_init(). */
8618 ofputil_append_group_desc_reply(const struct ofputil_group_desc
*gds
,
8619 const struct ovs_list
*buckets
,
8620 struct ovs_list
*replies
)
8622 enum ofp_version version
= ofpmp_version(replies
);
8630 ofputil_append_ofp11_group_desc_reply(gds
, buckets
, replies
, version
);
8635 ofputil_append_ofp15_group_desc_reply(gds
, buckets
, replies
, version
);
8645 ofputil_pull_ofp11_buckets(struct ofpbuf
*msg
, size_t buckets_length
,
8646 enum ofp_version version
, struct ovs_list
*buckets
)
8648 struct ofp11_bucket
*ob
;
8649 uint32_t bucket_id
= 0;
8651 ovs_list_init(buckets
);
8652 while (buckets_length
> 0) {
8653 struct ofputil_bucket
*bucket
;
8654 struct ofpbuf ofpacts
;
8658 ob
= (buckets_length
>= sizeof *ob
8659 ? ofpbuf_try_pull(msg
, sizeof *ob
)
8662 VLOG_WARN_RL(&bad_ofmsg_rl
, "buckets end with %"PRIuSIZE
" leftover bytes",
8664 return OFPERR_OFPGMFC_BAD_BUCKET
;
8667 ob_len
= ntohs(ob
->len
);
8668 if (ob_len
< sizeof *ob
) {
8669 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket length "
8670 "%"PRIuSIZE
" is not valid", ob_len
);
8671 return OFPERR_OFPGMFC_BAD_BUCKET
;
8672 } else if (ob_len
> buckets_length
) {
8673 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket length "
8674 "%"PRIuSIZE
" exceeds remaining buckets data size %"PRIuSIZE
,
8675 ob_len
, buckets_length
);
8676 return OFPERR_OFPGMFC_BAD_BUCKET
;
8678 buckets_length
-= ob_len
;
8680 ofpbuf_init(&ofpacts
, 0);
8681 error
= ofpacts_pull_openflow_actions(msg
, ob_len
- sizeof *ob
,
8684 ofpbuf_uninit(&ofpacts
);
8685 ofputil_bucket_list_destroy(buckets
);
8689 bucket
= xzalloc(sizeof *bucket
);
8690 bucket
->weight
= ntohs(ob
->weight
);
8691 error
= ofputil_port_from_ofp11(ob
->watch_port
, &bucket
->watch_port
);
8693 ofpbuf_uninit(&ofpacts
);
8694 ofputil_bucket_list_destroy(buckets
);
8695 return OFPERR_OFPGMFC_BAD_WATCH
;
8697 bucket
->watch_group
= ntohl(ob
->watch_group
);
8698 bucket
->bucket_id
= bucket_id
++;
8700 bucket
->ofpacts
= ofpbuf_steal_data(&ofpacts
);
8701 bucket
->ofpacts_len
= ofpacts
.size
;
8702 ovs_list_push_back(buckets
, &bucket
->list_node
);
8709 ofputil_pull_ofp15_buckets(struct ofpbuf
*msg
, size_t buckets_length
,
8710 enum ofp_version version
, uint8_t group_type
,
8711 struct ovs_list
*buckets
)
8713 struct ofp15_bucket
*ob
;
8715 ovs_list_init(buckets
);
8716 while (buckets_length
> 0) {
8717 struct ofputil_bucket
*bucket
= NULL
;
8718 struct ofpbuf ofpacts
;
8719 enum ofperr err
= OFPERR_OFPGMFC_BAD_BUCKET
;
8720 size_t ob_len
, actions_len
, properties_len
;
8721 ovs_be32 watch_port
= ofputil_port_to_ofp11(OFPP_ANY
);
8722 ovs_be32 watch_group
= htonl(OFPG_ANY
);
8723 ovs_be16 weight
= htons(group_type
== OFPGT11_SELECT
? 1 : 0);
8725 ofpbuf_init(&ofpacts
, 0);
8727 ob
= ofpbuf_try_pull(msg
, sizeof *ob
);
8729 VLOG_WARN_RL(&bad_ofmsg_rl
, "buckets end with %"PRIuSIZE
8730 " leftover bytes", buckets_length
);
8734 ob_len
= ntohs(ob
->len
);
8735 actions_len
= ntohs(ob
->action_array_len
);
8737 if (ob_len
< sizeof *ob
) {
8738 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket length "
8739 "%"PRIuSIZE
" is not valid", ob_len
);
8741 } else if (ob_len
> buckets_length
) {
8742 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket length "
8743 "%"PRIuSIZE
" exceeds remaining buckets data size %"
8744 PRIuSIZE
, ob_len
, buckets_length
);
8746 } else if (actions_len
> ob_len
- sizeof *ob
) {
8747 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket actions "
8748 "length %"PRIuSIZE
" exceeds remaining bucket "
8749 "data size %"PRIuSIZE
, actions_len
,
8750 ob_len
- sizeof *ob
);
8753 buckets_length
-= ob_len
;
8755 err
= ofpacts_pull_openflow_actions(msg
, actions_len
, version
,
8761 properties_len
= ob_len
- sizeof *ob
- actions_len
;
8762 struct ofpbuf properties
= ofpbuf_const_initializer(
8763 ofpbuf_pull(msg
, properties_len
), properties_len
);
8764 while (properties
.size
> 0) {
8765 struct ofpbuf payload
;
8768 err
= ofpprop_pull(&properties
, &payload
, &type
);
8774 case OFPGBPT15_WEIGHT
:
8775 err
= ofpprop_parse_be16(&payload
, &weight
);
8778 case OFPGBPT15_WATCH_PORT
:
8779 err
= ofpprop_parse_be32(&payload
, &watch_port
);
8782 case OFPGBPT15_WATCH_GROUP
:
8783 err
= ofpprop_parse_be32(&payload
, &watch_group
);
8787 err
= OFPPROP_UNKNOWN(false, "group bucket", type
);
8796 bucket
= xzalloc(sizeof *bucket
);
8798 bucket
->weight
= ntohs(weight
);
8799 err
= ofputil_port_from_ofp11(watch_port
, &bucket
->watch_port
);
8801 err
= OFPERR_OFPGMFC_BAD_WATCH
;
8804 bucket
->watch_group
= ntohl(watch_group
);
8805 bucket
->bucket_id
= ntohl(ob
->bucket_id
);
8806 if (bucket
->bucket_id
> OFPG15_BUCKET_MAX
) {
8807 VLOG_WARN_RL(&bad_ofmsg_rl
, "bucket id (%u) is out of range",
8809 err
= OFPERR_OFPGMFC_BAD_BUCKET
;
8813 bucket
->ofpacts
= ofpbuf_steal_data(&ofpacts
);
8814 bucket
->ofpacts_len
= ofpacts
.size
;
8815 ovs_list_push_back(buckets
, &bucket
->list_node
);
8821 ofpbuf_uninit(&ofpacts
);
8822 ofputil_bucket_list_destroy(buckets
);
8826 if (ofputil_bucket_check_duplicate_id(buckets
)) {
8827 VLOG_WARN_RL(&bad_ofmsg_rl
, "Duplicate bucket id");
8828 ofputil_bucket_list_destroy(buckets
);
8829 return OFPERR_OFPGMFC_BAD_BUCKET
;
8836 ofputil_init_group_properties(struct ofputil_group_props
*gp
)
8838 memset(gp
, 0, sizeof *gp
);
8842 ofputil_group_properties_copy(struct ofputil_group_props
*to
,
8843 const struct ofputil_group_props
*from
)
8846 to
->fields
.values
= xmemdup(from
->fields
.values
, from
->fields
.values_size
);
8850 ofputil_group_properties_destroy(struct ofputil_group_props
*gp
)
8852 free(gp
->fields
.values
);
8856 parse_group_prop_ntr_selection_method(struct ofpbuf
*payload
,
8857 enum ofp11_group_type group_type
,
8858 enum ofp15_group_mod_command group_cmd
,
8859 struct ofputil_group_props
*gp
)
8861 struct ntr_group_prop_selection_method
*prop
= payload
->data
;
8862 size_t fields_len
, method_len
;
8865 switch (group_type
) {
8866 case OFPGT11_SELECT
:
8869 case OFPGT11_INDIRECT
:
8871 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method property is "
8872 "only allowed for select groups");
8873 return OFPERR_OFPBPC_BAD_VALUE
;
8878 switch (group_cmd
) {
8880 case OFPGC15_MODIFY
:
8881 case OFPGC15_ADD_OR_MOD
:
8883 case OFPGC15_DELETE
:
8884 case OFPGC15_INSERT_BUCKET
:
8885 case OFPGC15_REMOVE_BUCKET
:
8886 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method property is "
8887 "only allowed for add and delete group modifications");
8888 return OFPERR_OFPBPC_BAD_VALUE
;
8893 if (payload
->size
< sizeof *prop
) {
8894 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method property "
8895 "length %u is not valid", payload
->size
);
8896 return OFPERR_OFPBPC_BAD_LEN
;
8899 method_len
= strnlen(prop
->selection_method
, NTR_MAX_SELECTION_METHOD_LEN
);
8901 if (method_len
== NTR_MAX_SELECTION_METHOD_LEN
) {
8902 OFPPROP_LOG(&bad_ofmsg_rl
, false,
8903 "ntr selection method is not null terminated");
8904 return OFPERR_OFPBPC_BAD_VALUE
;
8907 if (strcmp("hash", prop
->selection_method
)) {
8908 OFPPROP_LOG(&bad_ofmsg_rl
, false,
8909 "ntr selection method '%s' is not supported",
8910 prop
->selection_method
);
8911 return OFPERR_OFPBPC_BAD_VALUE
;
8914 strcpy(gp
->selection_method
, prop
->selection_method
);
8915 gp
->selection_method_param
= ntohll(prop
->selection_method_param
);
8917 if (!method_len
&& gp
->selection_method_param
) {
8918 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method parameter is "
8919 "non-zero but selection method is empty");
8920 return OFPERR_OFPBPC_BAD_VALUE
;
8923 ofpbuf_pull(payload
, sizeof *prop
);
8925 fields_len
= ntohs(prop
->length
) - sizeof *prop
;
8926 if (!method_len
&& fields_len
) {
8927 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method parameter is "
8928 "zero but fields are provided");
8929 return OFPERR_OFPBPC_BAD_VALUE
;
8932 error
= oxm_pull_field_array(payload
->data
, fields_len
,
8935 OFPPROP_LOG(&bad_ofmsg_rl
, false,
8936 "ntr selection method fields are invalid");
8944 parse_ofp15_group_properties(struct ofpbuf
*msg
,
8945 enum ofp11_group_type group_type
,
8946 enum ofp15_group_mod_command group_cmd
,
8947 struct ofputil_group_props
*gp
,
8948 size_t properties_len
)
8950 struct ofpbuf properties
= ofpbuf_const_initializer(
8951 ofpbuf_pull(msg
, properties_len
), properties_len
);
8952 while (properties
.size
> 0) {
8953 struct ofpbuf payload
;
8957 error
= ofpprop_pull(&properties
, &payload
, &type
);
8963 case OFPPROP_EXP(NTR_VENDOR_ID
, NTRT_SELECTION_METHOD
):
8964 case OFPPROP_EXP(NTR_COMPAT_VENDOR_ID
, NTRT_SELECTION_METHOD
):
8965 error
= parse_group_prop_ntr_selection_method(&payload
, group_type
,
8970 error
= OFPPROP_UNKNOWN(false, "group", type
);
8983 ofputil_decode_ofp11_group_desc_reply(struct ofputil_group_desc
*gd
,
8985 enum ofp_version version
)
8987 struct ofp11_group_desc_stats
*ogds
;
8991 ofpraw_pull_assert(msg
);
8998 ogds
= ofpbuf_try_pull(msg
, sizeof *ogds
);
9000 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply has %"PRIu32
" "
9001 "leftover bytes at end", msg
->size
);
9002 return OFPERR_OFPBRC_BAD_LEN
;
9004 gd
->type
= ogds
->type
;
9005 gd
->group_id
= ntohl(ogds
->group_id
);
9007 length
= ntohs(ogds
->length
);
9008 if (length
< sizeof *ogds
|| length
- sizeof *ogds
> msg
->size
) {
9009 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply claims invalid "
9010 "length %"PRIuSIZE
, length
);
9011 return OFPERR_OFPBRC_BAD_LEN
;
9014 return ofputil_pull_ofp11_buckets(msg
, length
- sizeof *ogds
, version
,
9019 ofputil_decode_ofp15_group_desc_reply(struct ofputil_group_desc
*gd
,
9021 enum ofp_version version
)
9023 struct ofp15_group_desc_stats
*ogds
;
9024 uint16_t length
, bucket_list_len
;
9028 ofpraw_pull_assert(msg
);
9035 ogds
= ofpbuf_try_pull(msg
, sizeof *ogds
);
9037 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply has %"PRIu32
" "
9038 "leftover bytes at end", msg
->size
);
9039 return OFPERR_OFPBRC_BAD_LEN
;
9041 gd
->type
= ogds
->type
;
9042 gd
->group_id
= ntohl(ogds
->group_id
);
9044 length
= ntohs(ogds
->length
);
9045 if (length
< sizeof *ogds
|| length
- sizeof *ogds
> msg
->size
) {
9046 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply claims invalid "
9047 "length %u", length
);
9048 return OFPERR_OFPBRC_BAD_LEN
;
9051 bucket_list_len
= ntohs(ogds
->bucket_list_len
);
9052 if (length
< bucket_list_len
+ sizeof *ogds
) {
9053 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply claims invalid "
9054 "bucket list length %u", bucket_list_len
);
9055 return OFPERR_OFPBRC_BAD_LEN
;
9057 error
= ofputil_pull_ofp15_buckets(msg
, bucket_list_len
, version
, gd
->type
,
9063 /* By definition group desc messages don't have a group mod command.
9064 * However, parse_group_prop_ntr_selection_method() checks to make sure
9065 * that the command is OFPGC15_ADD or OFPGC15_DELETE to guard
9066 * against group mod messages with other commands supplying
9067 * a NTR selection method group experimenter property.
9068 * Such properties are valid for group desc replies so
9069 * claim that the group mod command is OFPGC15_ADD to
9070 * satisfy the check in parse_group_prop_ntr_selection_method() */
9071 return parse_ofp15_group_properties(msg
, gd
->type
, OFPGC15_ADD
, &gd
->props
,
9072 length
- sizeof *ogds
- bucket_list_len
);
9075 /* Converts a group description reply in 'msg' into an abstract
9076 * ofputil_group_desc in 'gd'.
9078 * Multiple group description replies can be packed into a single OpenFlow
9079 * message. Calling this function multiple times for a single 'msg' iterates
9080 * through the replies. The caller must initially leave 'msg''s layer pointers
9081 * null and not modify them between calls.
9083 * Returns 0 if successful, EOF if no replies were left in this 'msg',
9084 * otherwise a positive errno value. */
9086 ofputil_decode_group_desc_reply(struct ofputil_group_desc
*gd
,
9087 struct ofpbuf
*msg
, enum ofp_version version
)
9089 ofputil_init_group_properties(&gd
->props
);
9097 return ofputil_decode_ofp11_group_desc_reply(gd
, msg
, version
);
9101 return ofputil_decode_ofp15_group_desc_reply(gd
, msg
, version
);
9110 ofputil_uninit_group_mod(struct ofputil_group_mod
*gm
)
9112 ofputil_bucket_list_destroy(&gm
->buckets
);
9113 ofputil_group_properties_destroy(&gm
->props
);
9116 static struct ofpbuf
*
9117 ofputil_encode_ofp11_group_mod(enum ofp_version ofp_version
,
9118 const struct ofputil_group_mod
*gm
)
9121 struct ofp11_group_mod
*ogm
;
9123 struct ofputil_bucket
*bucket
;
9125 b
= ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD
, ofp_version
, 0);
9126 start_ogm
= b
->size
;
9127 ofpbuf_put_zeros(b
, sizeof *ogm
);
9129 LIST_FOR_EACH (bucket
, list_node
, &gm
->buckets
) {
9130 ofputil_put_ofp11_bucket(bucket
, b
, ofp_version
);
9132 ogm
= ofpbuf_at_assert(b
, start_ogm
, sizeof *ogm
);
9133 ogm
->command
= htons(gm
->command
);
9134 ogm
->type
= gm
->type
;
9135 ogm
->group_id
= htonl(gm
->group_id
);
9140 static struct ofpbuf
*
9141 ofputil_encode_ofp15_group_mod(enum ofp_version ofp_version
,
9142 const struct ofputil_group_mod
*gm
)
9145 struct ofp15_group_mod
*ogm
;
9147 struct ofputil_bucket
*bucket
;
9148 struct id_pool
*bucket_ids
= NULL
;
9150 b
= ofpraw_alloc(OFPRAW_OFPT15_GROUP_MOD
, ofp_version
, 0);
9151 start_ogm
= b
->size
;
9152 ofpbuf_put_zeros(b
, sizeof *ogm
);
9154 LIST_FOR_EACH (bucket
, list_node
, &gm
->buckets
) {
9157 /* Generate a bucket id if none was supplied */
9158 if (bucket
->bucket_id
> OFPG15_BUCKET_MAX
) {
9160 const struct ofputil_bucket
*bkt
;
9162 bucket_ids
= id_pool_create(0, OFPG15_BUCKET_MAX
+ 1);
9164 /* Mark all bucket_ids that are present in gm
9165 * as used in the pool. */
9166 LIST_FOR_EACH_REVERSE (bkt
, list_node
, &gm
->buckets
) {
9167 if (bkt
== bucket
) {
9170 if (bkt
->bucket_id
<= OFPG15_BUCKET_MAX
) {
9171 id_pool_add(bucket_ids
, bkt
->bucket_id
);
9176 if (!id_pool_alloc_id(bucket_ids
, &bucket_id
)) {
9180 bucket_id
= bucket
->bucket_id
;
9183 ofputil_put_ofp15_bucket(bucket
, bucket_id
, gm
->type
, b
, ofp_version
);
9185 ogm
= ofpbuf_at_assert(b
, start_ogm
, sizeof *ogm
);
9186 ogm
->command
= htons(gm
->command
);
9187 ogm
->type
= gm
->type
;
9188 ogm
->group_id
= htonl(gm
->group_id
);
9189 ogm
->command_bucket_id
= htonl(gm
->command_bucket_id
);
9190 ogm
->bucket_array_len
= htons(b
->size
- start_ogm
- sizeof *ogm
);
9192 /* Add group properties */
9193 if (gm
->props
.selection_method
[0]) {
9194 ofputil_put_group_prop_ntr_selection_method(ofp_version
, &gm
->props
, b
);
9197 id_pool_destroy(bucket_ids
);
9202 bad_group_cmd(enum ofp15_group_mod_command cmd
)
9204 const char *opt_version
;
9205 const char *version
;
9206 const char *cmd_str
;
9210 case OFPGC15_MODIFY
:
9211 case OFPGC15_ADD_OR_MOD
:
9212 case OFPGC15_DELETE
:
9217 case OFPGC15_INSERT_BUCKET
:
9218 case OFPGC15_REMOVE_BUCKET
:
9229 cmd_str
= "add-group";
9232 case OFPGC15_MODIFY
:
9233 case OFPGC15_ADD_OR_MOD
:
9234 cmd_str
= "mod-group";
9237 case OFPGC15_DELETE
:
9238 cmd_str
= "del-group";
9241 case OFPGC15_INSERT_BUCKET
:
9242 cmd_str
= "insert-bucket";
9245 case OFPGC15_REMOVE_BUCKET
:
9246 cmd_str
= "remove-bucket";
9253 ovs_fatal(0, "%s needs OpenFlow %s or later (\'-O OpenFlow%s\')",
9254 cmd_str
, version
, opt_version
);
9258 /* Converts abstract group mod 'gm' into a message for OpenFlow version
9259 * 'ofp_version' and returns the message. */
9261 ofputil_encode_group_mod(enum ofp_version ofp_version
,
9262 const struct ofputil_group_mod
*gm
)
9265 switch (ofp_version
) {
9267 bad_group_cmd(gm
->command
);
9273 if (gm
->command
> OFPGC11_DELETE
&& gm
->command
!= OFPGC11_ADD_OR_MOD
) {
9274 bad_group_cmd(gm
->command
);
9276 return ofputil_encode_ofp11_group_mod(ofp_version
, gm
);
9280 return ofputil_encode_ofp15_group_mod(ofp_version
, gm
);
9288 ofputil_pull_ofp11_group_mod(struct ofpbuf
*msg
, enum ofp_version ofp_version
,
9289 struct ofputil_group_mod
*gm
)
9291 const struct ofp11_group_mod
*ogm
;
9294 ogm
= ofpbuf_pull(msg
, sizeof *ogm
);
9295 gm
->command
= ntohs(ogm
->command
);
9296 gm
->type
= ogm
->type
;
9297 gm
->group_id
= ntohl(ogm
->group_id
);
9298 gm
->command_bucket_id
= OFPG15_BUCKET_ALL
;
9300 error
= ofputil_pull_ofp11_buckets(msg
, msg
->size
, ofp_version
,
9303 /* OF1.3.5+ prescribes an error when an OFPGC_DELETE includes buckets. */
9305 && ofp_version
>= OFP13_VERSION
9306 && gm
->command
== OFPGC11_DELETE
9307 && !ovs_list_is_empty(&gm
->buckets
)) {
9308 error
= OFPERR_OFPGMFC_INVALID_GROUP
;
9315 ofputil_pull_ofp15_group_mod(struct ofpbuf
*msg
, enum ofp_version ofp_version
,
9316 struct ofputil_group_mod
*gm
)
9318 const struct ofp15_group_mod
*ogm
;
9319 uint16_t bucket_list_len
;
9320 enum ofperr error
= OFPERR_OFPGMFC_BAD_BUCKET
;
9322 ogm
= ofpbuf_pull(msg
, sizeof *ogm
);
9323 gm
->command
= ntohs(ogm
->command
);
9324 gm
->type
= ogm
->type
;
9325 gm
->group_id
= ntohl(ogm
->group_id
);
9327 gm
->command_bucket_id
= ntohl(ogm
->command_bucket_id
);
9328 switch (gm
->command
) {
9329 case OFPGC15_REMOVE_BUCKET
:
9330 if (gm
->command_bucket_id
== OFPG15_BUCKET_ALL
) {
9334 case OFPGC15_INSERT_BUCKET
:
9335 if (gm
->command_bucket_id
<= OFPG15_BUCKET_MAX
||
9336 gm
->command_bucket_id
== OFPG15_BUCKET_FIRST
9337 || gm
->command_bucket_id
== OFPG15_BUCKET_LAST
) {
9343 case OFPGC11_MODIFY
:
9344 case OFPGC11_ADD_OR_MOD
:
9345 case OFPGC11_DELETE
:
9347 if (gm
->command_bucket_id
== OFPG15_BUCKET_ALL
) {
9353 VLOG_WARN_RL(&bad_ofmsg_rl
,
9354 "group command bucket id (%u) is out of range",
9355 gm
->command_bucket_id
);
9356 return OFPERR_OFPGMFC_BAD_BUCKET
;
9359 bucket_list_len
= ntohs(ogm
->bucket_array_len
);
9360 error
= ofputil_pull_ofp15_buckets(msg
, bucket_list_len
, ofp_version
,
9361 gm
->type
, &gm
->buckets
);
9366 return parse_ofp15_group_properties(msg
, gm
->type
, gm
->command
, &gm
->props
,
9370 /* Converts OpenFlow group mod message 'oh' into an abstract group mod in
9371 * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */
9373 ofputil_decode_group_mod(const struct ofp_header
*oh
,
9374 struct ofputil_group_mod
*gm
)
9376 ofputil_init_group_properties(&gm
->props
);
9378 enum ofp_version ofp_version
= oh
->version
;
9379 struct ofpbuf msg
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
9380 ofpraw_pull_assert(&msg
);
9383 switch (ofp_version
)
9389 err
= ofputil_pull_ofp11_group_mod(&msg
, ofp_version
, gm
);
9394 err
= ofputil_pull_ofp15_group_mod(&msg
, ofp_version
, gm
);
9406 case OFPGT11_INDIRECT
:
9407 if (gm
->command
!= OFPGC11_DELETE
9408 && !ovs_list_is_singleton(&gm
->buckets
) ) {
9409 return OFPERR_OFPGMFC_INVALID_GROUP
;
9413 case OFPGT11_SELECT
:
9417 return OFPERR_OFPGMFC_BAD_TYPE
;
9420 switch (gm
->command
) {
9422 case OFPGC11_MODIFY
:
9423 case OFPGC11_ADD_OR_MOD
:
9424 case OFPGC11_DELETE
:
9425 case OFPGC15_INSERT_BUCKET
:
9427 case OFPGC15_REMOVE_BUCKET
:
9428 if (!ovs_list_is_empty(&gm
->buckets
)) {
9429 return OFPERR_OFPGMFC_BAD_BUCKET
;
9433 return OFPERR_OFPGMFC_BAD_COMMAND
;
9436 struct ofputil_bucket
*bucket
;
9437 LIST_FOR_EACH (bucket
, list_node
, &gm
->buckets
) {
9438 if (bucket
->weight
&& gm
->type
!= OFPGT11_SELECT
) {
9439 return OFPERR_OFPGMFC_INVALID_GROUP
;
9444 case OFPGT11_INDIRECT
:
9445 if (ofputil_bucket_has_liveness(bucket
)) {
9446 return OFPERR_OFPGMFC_WATCH_UNSUPPORTED
;
9449 case OFPGT11_SELECT
:
9452 if (!ofputil_bucket_has_liveness(bucket
)) {
9453 return OFPERR_OFPGMFC_INVALID_GROUP
;
9457 /* Returning BAD TYPE to be consistent
9458 * though gm->type has been checked already. */
9459 return OFPERR_OFPGMFC_BAD_TYPE
;
9466 /* Destroys 'bms'. */
9468 ofputil_free_bundle_msgs(struct ofputil_bundle_msg
*bms
, size_t n_bms
)
9470 for (size_t i
= 0; i
< n_bms
; i
++) {
9471 switch ((int)bms
[i
].type
) {
9472 case OFPTYPE_FLOW_MOD
:
9473 free(CONST_CAST(struct ofpact
*, bms
[i
].fm
.ofpacts
));
9475 case OFPTYPE_GROUP_MOD
:
9476 ofputil_uninit_group_mod(&bms
[i
].gm
);
9478 case OFPTYPE_PACKET_OUT
:
9479 free(bms
[i
].po
.ofpacts
);
9480 free(CONST_CAST(void *, bms
[i
].po
.packet
));
9490 ofputil_encode_bundle_msgs(const struct ofputil_bundle_msg
*bms
,
9491 size_t n_bms
, struct ovs_list
*requests
,
9492 enum ofputil_protocol protocol
)
9494 enum ofp_version version
= ofputil_protocol_to_ofp_version(protocol
);
9496 for (size_t i
= 0; i
< n_bms
; i
++) {
9497 struct ofpbuf
*request
= NULL
;
9499 switch ((int)bms
[i
].type
) {
9500 case OFPTYPE_FLOW_MOD
:
9501 request
= ofputil_encode_flow_mod(&bms
[i
].fm
, protocol
);
9503 case OFPTYPE_GROUP_MOD
:
9504 request
= ofputil_encode_group_mod(version
, &bms
[i
].gm
);
9506 case OFPTYPE_PACKET_OUT
:
9507 request
= ofputil_encode_packet_out(&bms
[i
].po
, protocol
);
9513 ovs_list_push_back(requests
, &request
->list_node
);
9518 /* Parse a queue status request message into 'oqsr'.
9519 * Returns 0 if successful, otherwise an OFPERR_* number. */
9521 ofputil_decode_queue_stats_request(const struct ofp_header
*request
,
9522 struct ofputil_queue_stats_request
*oqsr
)
9524 switch ((enum ofp_version
)request
->version
) {
9530 case OFP11_VERSION
: {
9531 const struct ofp11_queue_stats_request
*qsr11
= ofpmsg_body(request
);
9532 oqsr
->queue_id
= ntohl(qsr11
->queue_id
);
9533 return ofputil_port_from_ofp11(qsr11
->port_no
, &oqsr
->port_no
);
9536 case OFP10_VERSION
: {
9537 const struct ofp10_queue_stats_request
*qsr10
= ofpmsg_body(request
);
9538 oqsr
->queue_id
= ntohl(qsr10
->queue_id
);
9539 oqsr
->port_no
= u16_to_ofp(ntohs(qsr10
->port_no
));
9540 /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
9541 if (oqsr
->port_no
== OFPP_ALL
) {
9542 oqsr
->port_no
= OFPP_ANY
;
9552 /* Encode a queue stats request for 'oqsr', the encoded message
9553 * will be for OpenFlow version 'ofp_version'. Returns message
9554 * as a struct ofpbuf. Returns encoded message on success, NULL on error. */
9556 ofputil_encode_queue_stats_request(enum ofp_version ofp_version
,
9557 const struct ofputil_queue_stats_request
*oqsr
)
9559 struct ofpbuf
*request
;
9561 switch (ofp_version
) {
9567 case OFP16_VERSION
: {
9568 struct ofp11_queue_stats_request
*req
;
9569 request
= ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST
, ofp_version
, 0);
9570 req
= ofpbuf_put_zeros(request
, sizeof *req
);
9571 req
->port_no
= ofputil_port_to_ofp11(oqsr
->port_no
);
9572 req
->queue_id
= htonl(oqsr
->queue_id
);
9575 case OFP10_VERSION
: {
9576 struct ofp10_queue_stats_request
*req
;
9577 request
= ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST
, ofp_version
, 0);
9578 req
= ofpbuf_put_zeros(request
, sizeof *req
);
9579 /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
9580 req
->port_no
= htons(ofp_to_u16(oqsr
->port_no
== OFPP_ANY
9581 ? OFPP_ALL
: oqsr
->port_no
));
9582 req
->queue_id
= htonl(oqsr
->queue_id
);
9592 /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
9595 ofputil_count_queue_stats(const struct ofp_header
*oh
)
9597 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
9598 ofpraw_pull_assert(&b
);
9600 for (size_t n
= 0; ; n
++) {
9601 struct ofputil_queue_stats qs
;
9602 if (ofputil_decode_queue_stats(&qs
, &b
)) {
9609 ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats
*oqs
,
9610 const struct ofp10_queue_stats
*qs10
)
9612 oqs
->port_no
= u16_to_ofp(ntohs(qs10
->port_no
));
9613 oqs
->queue_id
= ntohl(qs10
->queue_id
);
9614 oqs
->tx_bytes
= ntohll(get_32aligned_be64(&qs10
->tx_bytes
));
9615 oqs
->tx_packets
= ntohll(get_32aligned_be64(&qs10
->tx_packets
));
9616 oqs
->tx_errors
= ntohll(get_32aligned_be64(&qs10
->tx_errors
));
9617 oqs
->duration_sec
= oqs
->duration_nsec
= UINT32_MAX
;
9623 ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats
*oqs
,
9624 const struct ofp11_queue_stats
*qs11
)
9628 error
= ofputil_port_from_ofp11(qs11
->port_no
, &oqs
->port_no
);
9633 oqs
->queue_id
= ntohl(qs11
->queue_id
);
9634 oqs
->tx_bytes
= ntohll(qs11
->tx_bytes
);
9635 oqs
->tx_packets
= ntohll(qs11
->tx_packets
);
9636 oqs
->tx_errors
= ntohll(qs11
->tx_errors
);
9637 oqs
->duration_sec
= oqs
->duration_nsec
= UINT32_MAX
;
9643 ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats
*oqs
,
9644 const struct ofp13_queue_stats
*qs13
)
9646 enum ofperr error
= ofputil_queue_stats_from_ofp11(oqs
, &qs13
->qs
);
9648 oqs
->duration_sec
= ntohl(qs13
->duration_sec
);
9649 oqs
->duration_nsec
= ntohl(qs13
->duration_nsec
);
9656 ofputil_pull_ofp14_queue_stats(struct ofputil_queue_stats
*oqs
,
9659 const struct ofp14_queue_stats
*qs14
;
9662 qs14
= ofpbuf_try_pull(msg
, sizeof *qs14
);
9664 return OFPERR_OFPBRC_BAD_LEN
;
9667 len
= ntohs(qs14
->length
);
9668 if (len
< sizeof *qs14
|| len
- sizeof *qs14
> msg
->size
) {
9669 return OFPERR_OFPBRC_BAD_LEN
;
9671 ofpbuf_pull(msg
, len
- sizeof *qs14
);
9673 /* No properties yet defined, so ignore them for now. */
9675 return ofputil_queue_stats_from_ofp13(oqs
, &qs14
->qs
);
9678 /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
9679 * ofputil_queue_stats in 'qs'.
9681 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
9682 * message. Calling this function multiple times for a single 'msg' iterates
9683 * through the replies. The caller must initially leave 'msg''s layer pointers
9684 * null and not modify them between calls.
9686 * Returns 0 if successful, EOF if no replies were left in this 'msg',
9687 * otherwise a positive errno value. */
9689 ofputil_decode_queue_stats(struct ofputil_queue_stats
*qs
, struct ofpbuf
*msg
)
9694 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
9695 : ofpraw_pull(&raw
, msg
));
9702 } else if (raw
== OFPRAW_OFPST14_QUEUE_REPLY
) {
9703 return ofputil_pull_ofp14_queue_stats(qs
, msg
);
9704 } else if (raw
== OFPRAW_OFPST13_QUEUE_REPLY
) {
9705 const struct ofp13_queue_stats
*qs13
;
9707 qs13
= ofpbuf_try_pull(msg
, sizeof *qs13
);
9711 return ofputil_queue_stats_from_ofp13(qs
, qs13
);
9712 } else if (raw
== OFPRAW_OFPST11_QUEUE_REPLY
) {
9713 const struct ofp11_queue_stats
*qs11
;
9715 qs11
= ofpbuf_try_pull(msg
, sizeof *qs11
);
9719 return ofputil_queue_stats_from_ofp11(qs
, qs11
);
9720 } else if (raw
== OFPRAW_OFPST10_QUEUE_REPLY
) {
9721 const struct ofp10_queue_stats
*qs10
;
9723 qs10
= ofpbuf_try_pull(msg
, sizeof *qs10
);
9727 return ofputil_queue_stats_from_ofp10(qs
, qs10
);
9733 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_QUEUE reply has %"PRIu32
" leftover "
9734 "bytes at end", msg
->size
);
9735 return OFPERR_OFPBRC_BAD_LEN
;
9739 ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats
*oqs
,
9740 struct ofp10_queue_stats
*qs10
)
9742 qs10
->port_no
= htons(ofp_to_u16(oqs
->port_no
));
9743 memset(qs10
->pad
, 0, sizeof qs10
->pad
);
9744 qs10
->queue_id
= htonl(oqs
->queue_id
);
9745 put_32aligned_be64(&qs10
->tx_bytes
, htonll(oqs
->tx_bytes
));
9746 put_32aligned_be64(&qs10
->tx_packets
, htonll(oqs
->tx_packets
));
9747 put_32aligned_be64(&qs10
->tx_errors
, htonll(oqs
->tx_errors
));
9751 ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats
*oqs
,
9752 struct ofp11_queue_stats
*qs11
)
9754 qs11
->port_no
= ofputil_port_to_ofp11(oqs
->port_no
);
9755 qs11
->queue_id
= htonl(oqs
->queue_id
);
9756 qs11
->tx_bytes
= htonll(oqs
->tx_bytes
);
9757 qs11
->tx_packets
= htonll(oqs
->tx_packets
);
9758 qs11
->tx_errors
= htonll(oqs
->tx_errors
);
9762 ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats
*oqs
,
9763 struct ofp13_queue_stats
*qs13
)
9765 ofputil_queue_stats_to_ofp11(oqs
, &qs13
->qs
);
9766 if (oqs
->duration_sec
!= UINT32_MAX
) {
9767 qs13
->duration_sec
= htonl(oqs
->duration_sec
);
9768 qs13
->duration_nsec
= htonl(oqs
->duration_nsec
);
9770 qs13
->duration_sec
= OVS_BE32_MAX
;
9771 qs13
->duration_nsec
= OVS_BE32_MAX
;
9776 ofputil_queue_stats_to_ofp14(const struct ofputil_queue_stats
*oqs
,
9777 struct ofp14_queue_stats
*qs14
)
9779 qs14
->length
= htons(sizeof *qs14
);
9780 memset(qs14
->pad
, 0, sizeof qs14
->pad
);
9781 ofputil_queue_stats_to_ofp13(oqs
, &qs14
->qs
);
9785 /* Encode a queue stat for 'oqs' and append it to 'replies'. */
9787 ofputil_append_queue_stat(struct ovs_list
*replies
,
9788 const struct ofputil_queue_stats
*oqs
)
9790 switch (ofpmp_version(replies
)) {
9791 case OFP13_VERSION
: {
9792 struct ofp13_queue_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
9793 ofputil_queue_stats_to_ofp13(oqs
, reply
);
9798 case OFP11_VERSION
: {
9799 struct ofp11_queue_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
9800 ofputil_queue_stats_to_ofp11(oqs
, reply
);
9804 case OFP10_VERSION
: {
9805 struct ofp10_queue_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
9806 ofputil_queue_stats_to_ofp10(oqs
, reply
);
9812 case OFP16_VERSION
: {
9813 struct ofp14_queue_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
9814 ofputil_queue_stats_to_ofp14(oqs
, reply
);
9824 ofputil_decode_bundle_ctrl(const struct ofp_header
*oh
,
9825 struct ofputil_bundle_ctrl_msg
*msg
)
9827 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
9828 enum ofpraw raw
= ofpraw_pull_assert(&b
);
9829 ovs_assert(raw
== OFPRAW_OFPT14_BUNDLE_CONTROL
9830 || raw
== OFPRAW_ONFT13_BUNDLE_CONTROL
);
9832 const struct ofp14_bundle_ctrl_msg
*m
= b
.msg
;
9833 msg
->bundle_id
= ntohl(m
->bundle_id
);
9834 msg
->type
= ntohs(m
->type
);
9835 msg
->flags
= ntohs(m
->flags
);
9841 ofputil_encode_bundle_ctrl_request(enum ofp_version ofp_version
,
9842 struct ofputil_bundle_ctrl_msg
*bc
)
9844 struct ofpbuf
*request
;
9845 struct ofp14_bundle_ctrl_msg
*m
;
9847 switch (ofp_version
) {
9851 ovs_fatal(0, "bundles need OpenFlow 1.3 or later "
9852 "(\'-O OpenFlow14\')");
9857 request
= ofpraw_alloc(ofp_version
== OFP13_VERSION
9858 ? OFPRAW_ONFT13_BUNDLE_CONTROL
9859 : OFPRAW_OFPT14_BUNDLE_CONTROL
, ofp_version
, 0);
9860 m
= ofpbuf_put_zeros(request
, sizeof *m
);
9862 m
->bundle_id
= htonl(bc
->bundle_id
);
9863 m
->type
= htons(bc
->type
);
9864 m
->flags
= htons(bc
->flags
);
9874 ofputil_encode_bundle_ctrl_reply(const struct ofp_header
*oh
,
9875 struct ofputil_bundle_ctrl_msg
*msg
)
9878 struct ofp14_bundle_ctrl_msg
*m
;
9880 buf
= ofpraw_alloc_reply(oh
->version
== OFP13_VERSION
9881 ? OFPRAW_ONFT13_BUNDLE_CONTROL
9882 : OFPRAW_OFPT14_BUNDLE_CONTROL
, oh
, 0);
9883 m
= ofpbuf_put_zeros(buf
, sizeof *m
);
9885 m
->bundle_id
= htonl(msg
->bundle_id
);
9886 m
->type
= htons(msg
->type
);
9887 m
->flags
= htons(msg
->flags
);
9892 /* Return true for bundlable state change requests, false for other messages.
9895 ofputil_is_bundlable(enum ofptype type
)
9898 /* Minimum required by OpenFlow 1.4. */
9899 case OFPTYPE_PORT_MOD
:
9900 case OFPTYPE_FLOW_MOD
:
9901 /* Other supported types. */
9902 case OFPTYPE_GROUP_MOD
:
9903 case OFPTYPE_PACKET_OUT
:
9906 /* Nice to have later. */
9907 case OFPTYPE_FLOW_MOD_TABLE_ID
:
9908 case OFPTYPE_TABLE_MOD
:
9909 case OFPTYPE_METER_MOD
:
9910 case OFPTYPE_NXT_TLV_TABLE_MOD
:
9912 /* Not to be bundlable. */
9913 case OFPTYPE_ECHO_REQUEST
:
9914 case OFPTYPE_FEATURES_REQUEST
:
9915 case OFPTYPE_GET_CONFIG_REQUEST
:
9916 case OFPTYPE_SET_CONFIG
:
9917 case OFPTYPE_BARRIER_REQUEST
:
9918 case OFPTYPE_ROLE_REQUEST
:
9919 case OFPTYPE_ECHO_REPLY
:
9920 case OFPTYPE_SET_FLOW_FORMAT
:
9921 case OFPTYPE_SET_PACKET_IN_FORMAT
:
9922 case OFPTYPE_SET_CONTROLLER_ID
:
9923 case OFPTYPE_FLOW_AGE
:
9924 case OFPTYPE_FLOW_MONITOR_CANCEL
:
9925 case OFPTYPE_SET_ASYNC_CONFIG
:
9926 case OFPTYPE_GET_ASYNC_REQUEST
:
9927 case OFPTYPE_DESC_STATS_REQUEST
:
9928 case OFPTYPE_FLOW_STATS_REQUEST
:
9929 case OFPTYPE_AGGREGATE_STATS_REQUEST
:
9930 case OFPTYPE_TABLE_STATS_REQUEST
:
9931 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST
:
9932 case OFPTYPE_TABLE_DESC_REQUEST
:
9933 case OFPTYPE_PORT_STATS_REQUEST
:
9934 case OFPTYPE_QUEUE_STATS_REQUEST
:
9935 case OFPTYPE_PORT_DESC_STATS_REQUEST
:
9936 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST
:
9937 case OFPTYPE_METER_STATS_REQUEST
:
9938 case OFPTYPE_METER_CONFIG_STATS_REQUEST
:
9939 case OFPTYPE_METER_FEATURES_STATS_REQUEST
:
9940 case OFPTYPE_GROUP_STATS_REQUEST
:
9941 case OFPTYPE_GROUP_DESC_STATS_REQUEST
:
9942 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST
:
9943 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST
:
9944 case OFPTYPE_BUNDLE_CONTROL
:
9945 case OFPTYPE_BUNDLE_ADD_MESSAGE
:
9948 case OFPTYPE_FEATURES_REPLY
:
9949 case OFPTYPE_GET_CONFIG_REPLY
:
9950 case OFPTYPE_PACKET_IN
:
9951 case OFPTYPE_FLOW_REMOVED
:
9952 case OFPTYPE_PORT_STATUS
:
9953 case OFPTYPE_BARRIER_REPLY
:
9954 case OFPTYPE_QUEUE_GET_CONFIG_REPLY
:
9955 case OFPTYPE_DESC_STATS_REPLY
:
9956 case OFPTYPE_FLOW_STATS_REPLY
:
9957 case OFPTYPE_QUEUE_STATS_REPLY
:
9958 case OFPTYPE_PORT_STATS_REPLY
:
9959 case OFPTYPE_TABLE_STATS_REPLY
:
9960 case OFPTYPE_AGGREGATE_STATS_REPLY
:
9961 case OFPTYPE_PORT_DESC_STATS_REPLY
:
9962 case OFPTYPE_ROLE_REPLY
:
9963 case OFPTYPE_FLOW_MONITOR_PAUSED
:
9964 case OFPTYPE_FLOW_MONITOR_RESUMED
:
9965 case OFPTYPE_FLOW_MONITOR_STATS_REPLY
:
9966 case OFPTYPE_GET_ASYNC_REPLY
:
9967 case OFPTYPE_GROUP_STATS_REPLY
:
9968 case OFPTYPE_GROUP_DESC_STATS_REPLY
:
9969 case OFPTYPE_GROUP_FEATURES_STATS_REPLY
:
9970 case OFPTYPE_METER_STATS_REPLY
:
9971 case OFPTYPE_METER_CONFIG_STATS_REPLY
:
9972 case OFPTYPE_METER_FEATURES_STATS_REPLY
:
9973 case OFPTYPE_TABLE_FEATURES_STATS_REPLY
:
9974 case OFPTYPE_TABLE_DESC_REPLY
:
9975 case OFPTYPE_ROLE_STATUS
:
9976 case OFPTYPE_REQUESTFORWARD
:
9977 case OFPTYPE_TABLE_STATUS
:
9978 case OFPTYPE_NXT_TLV_TABLE_REQUEST
:
9979 case OFPTYPE_NXT_TLV_TABLE_REPLY
:
9980 case OFPTYPE_NXT_RESUME
:
9981 case OFPTYPE_IPFIX_BRIDGE_STATS_REQUEST
:
9982 case OFPTYPE_IPFIX_BRIDGE_STATS_REPLY
:
9983 case OFPTYPE_IPFIX_FLOW_STATS_REQUEST
:
9984 case OFPTYPE_IPFIX_FLOW_STATS_REPLY
:
9992 ofputil_decode_bundle_add(const struct ofp_header
*oh
,
9993 struct ofputil_bundle_add_msg
*msg
,
9994 enum ofptype
*typep
)
9996 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
9997 enum ofpraw raw
= ofpraw_pull_assert(&b
);
9998 ovs_assert(raw
== OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE
9999 || raw
== OFPRAW_ONFT13_BUNDLE_ADD_MESSAGE
);
10001 const struct ofp14_bundle_ctrl_msg
*m
= ofpbuf_pull(&b
, sizeof *m
);
10002 msg
->bundle_id
= ntohl(m
->bundle_id
);
10003 msg
->flags
= ntohs(m
->flags
);
10006 if (msg
->msg
->version
!= oh
->version
) {
10007 return OFPERR_OFPBFC_BAD_VERSION
;
10009 size_t inner_len
= ntohs(msg
->msg
->length
);
10010 if (inner_len
< sizeof(struct ofp_header
) || inner_len
> b
.size
) {
10011 return OFPERR_OFPBFC_MSG_BAD_LEN
;
10013 if (msg
->msg
->xid
!= oh
->xid
) {
10014 return OFPERR_OFPBFC_MSG_BAD_XID
;
10017 /* Reject unbundlable messages. */
10019 enum ofperr error
= ofptype_decode(&type
, msg
->msg
);
10021 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPT14_BUNDLE_ADD_MESSAGE contained "
10022 "message is unparsable (%s)", ofperr_get_name(error
));
10023 return OFPERR_OFPBFC_MSG_UNSUP
; /* 'error' would be confusing. */
10026 if (!ofputil_is_bundlable(type
)) {
10027 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s message not allowed inside "
10028 "OFPT14_BUNDLE_ADD_MESSAGE", ofptype_get_name(type
));
10029 return OFPERR_OFPBFC_MSG_UNSUP
;
10039 ofputil_encode_bundle_add(enum ofp_version ofp_version
,
10040 struct ofputil_bundle_add_msg
*msg
)
10042 struct ofpbuf
*request
;
10043 struct ofp14_bundle_ctrl_msg
*m
;
10045 /* Must use the same xid as the embedded message. */
10046 request
= ofpraw_alloc_xid(ofp_version
== OFP13_VERSION
10047 ? OFPRAW_ONFT13_BUNDLE_ADD_MESSAGE
10048 : OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE
, ofp_version
,
10049 msg
->msg
->xid
, ntohs(msg
->msg
->length
));
10050 m
= ofpbuf_put_zeros(request
, sizeof *m
);
10052 m
->bundle_id
= htonl(msg
->bundle_id
);
10053 m
->flags
= htons(msg
->flags
);
10054 ofpbuf_put(request
, msg
->msg
, ntohs(msg
->msg
->length
));
10056 ofpmsg_update_length(request
);
10061 encode_tlv_table_mappings(struct ofpbuf
*b
, struct ovs_list
*mappings
)
10063 struct ofputil_tlv_map
*map
;
10065 LIST_FOR_EACH (map
, list_node
, mappings
) {
10066 struct nx_tlv_map
*nx_map
;
10068 nx_map
= ofpbuf_put_zeros(b
, sizeof *nx_map
);
10069 nx_map
->option_class
= htons(map
->option_class
);
10070 nx_map
->option_type
= map
->option_type
;
10071 nx_map
->option_len
= map
->option_len
;
10072 nx_map
->index
= htons(map
->index
);
10077 ofputil_encode_tlv_table_mod(enum ofp_version ofp_version
,
10078 struct ofputil_tlv_table_mod
*ttm
)
10081 struct nx_tlv_table_mod
*nx_ttm
;
10083 b
= ofpraw_alloc(OFPRAW_NXT_TLV_TABLE_MOD
, ofp_version
, 0);
10084 nx_ttm
= ofpbuf_put_zeros(b
, sizeof *nx_ttm
);
10085 nx_ttm
->command
= htons(ttm
->command
);
10086 encode_tlv_table_mappings(b
, &ttm
->mappings
);
10092 decode_tlv_table_mappings(struct ofpbuf
*msg
, unsigned int max_fields
,
10093 struct ovs_list
*mappings
)
10095 ovs_list_init(mappings
);
10097 while (msg
->size
) {
10098 struct nx_tlv_map
*nx_map
;
10099 struct ofputil_tlv_map
*map
;
10101 nx_map
= ofpbuf_pull(msg
, sizeof *nx_map
);
10102 map
= xmalloc(sizeof *map
);
10103 ovs_list_push_back(mappings
, &map
->list_node
);
10105 map
->option_class
= ntohs(nx_map
->option_class
);
10106 map
->option_type
= nx_map
->option_type
;
10108 map
->option_len
= nx_map
->option_len
;
10109 if (map
->option_len
% 4 || map
->option_len
> TLV_MAX_OPT_SIZE
) {
10110 VLOG_WARN_RL(&bad_ofmsg_rl
,
10111 "tlv table option length (%u) is not a valid option size",
10113 ofputil_uninit_tlv_table(mappings
);
10114 return OFPERR_NXTTMFC_BAD_OPT_LEN
;
10117 map
->index
= ntohs(nx_map
->index
);
10118 if (map
->index
>= max_fields
) {
10119 VLOG_WARN_RL(&bad_ofmsg_rl
,
10120 "tlv table field index (%u) is too large (max %u)",
10121 map
->index
, max_fields
- 1);
10122 ofputil_uninit_tlv_table(mappings
);
10123 return OFPERR_NXTTMFC_BAD_FIELD_IDX
;
10131 ofputil_decode_tlv_table_mod(const struct ofp_header
*oh
,
10132 struct ofputil_tlv_table_mod
*ttm
)
10134 struct ofpbuf msg
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
10135 ofpraw_pull_assert(&msg
);
10137 struct nx_tlv_table_mod
*nx_ttm
= ofpbuf_pull(&msg
, sizeof *nx_ttm
);
10138 ttm
->command
= ntohs(nx_ttm
->command
);
10139 if (ttm
->command
> NXTTMC_CLEAR
) {
10140 VLOG_WARN_RL(&bad_ofmsg_rl
,
10141 "tlv table mod command (%u) is out of range",
10143 return OFPERR_NXTTMFC_BAD_COMMAND
;
10146 return decode_tlv_table_mappings(&msg
, TUN_METADATA_NUM_OPTS
,
10151 ofputil_encode_tlv_table_reply(const struct ofp_header
*oh
,
10152 struct ofputil_tlv_table_reply
*ttr
)
10155 struct nx_tlv_table_reply
*nx_ttr
;
10157 b
= ofpraw_alloc_reply(OFPRAW_NXT_TLV_TABLE_REPLY
, oh
, 0);
10158 nx_ttr
= ofpbuf_put_zeros(b
, sizeof *nx_ttr
);
10159 nx_ttr
->max_option_space
= htonl(ttr
->max_option_space
);
10160 nx_ttr
->max_fields
= htons(ttr
->max_fields
);
10162 encode_tlv_table_mappings(b
, &ttr
->mappings
);
10167 /* Decodes the NXT_TLV_TABLE_REPLY message in 'oh' into '*ttr'. Returns 0
10168 * if successful, otherwise an ofperr.
10170 * The decoder verifies that the indexes in 'ttr->mappings' are less than
10171 * 'ttr->max_fields', but the caller must ensure, if necessary, that they are
10172 * less than TUN_METADATA_NUM_OPTS. */
10174 ofputil_decode_tlv_table_reply(const struct ofp_header
*oh
,
10175 struct ofputil_tlv_table_reply
*ttr
)
10177 struct ofpbuf msg
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
10178 ofpraw_pull_assert(&msg
);
10180 struct nx_tlv_table_reply
*nx_ttr
= ofpbuf_pull(&msg
, sizeof *nx_ttr
);
10181 ttr
->max_option_space
= ntohl(nx_ttr
->max_option_space
);
10182 ttr
->max_fields
= ntohs(nx_ttr
->max_fields
);
10184 return decode_tlv_table_mappings(&msg
, ttr
->max_fields
, &ttr
->mappings
);
10188 ofputil_uninit_tlv_table(struct ovs_list
*mappings
)
10190 struct ofputil_tlv_map
*map
;
10192 LIST_FOR_EACH_POP (map
, list_node
, mappings
) {
10198 ofputil_async_msg_type_to_string(enum ofputil_async_msg_type type
)
10201 case OAM_PACKET_IN
: return "PACKET_IN";
10202 case OAM_PORT_STATUS
: return "PORT_STATUS";
10203 case OAM_FLOW_REMOVED
: return "FLOW_REMOVED";
10204 case OAM_ROLE_STATUS
: return "ROLE_STATUS";
10205 case OAM_TABLE_STATUS
: return "TABLE_STATUS";
10206 case OAM_REQUESTFORWARD
: return "REQUESTFORWARD";
10214 struct ofp14_async_prop
{
10215 uint64_t prop_type
;
10216 enum ofputil_async_msg_type oam
;
10218 uint32_t allowed10
, allowed14
;
10221 #define AP_PAIR(SLAVE_PROP_TYPE, OAM, A10, A14) \
10222 { SLAVE_PROP_TYPE, OAM, false, A10, (A14) ? (A14) : (A10) }, \
10223 { (SLAVE_PROP_TYPE + 1), OAM, true, A10, (A14) ? (A14) : (A10) }
10225 static const struct ofp14_async_prop async_props
[] = {
10226 AP_PAIR( 0, OAM_PACKET_IN
, OFPR10_BITS
, OFPR14_BITS
),
10227 AP_PAIR( 2, OAM_PORT_STATUS
, (1 << OFPPR_N_REASONS
) - 1, 0),
10228 AP_PAIR( 4, OAM_FLOW_REMOVED
, (1 << OVS_OFPRR_NONE
) - 1, 0),
10229 AP_PAIR( 6, OAM_ROLE_STATUS
, (1 << OFPCRR_N_REASONS
) - 1, 0),
10230 AP_PAIR( 8, OAM_TABLE_STATUS
, OFPTR_BITS
, 0),
10231 AP_PAIR(10, OAM_REQUESTFORWARD
, (1 << OFPRFR_N_REASONS
) - 1, 0),
10234 #define FOR_EACH_ASYNC_PROP(VAR) \
10235 for (const struct ofp14_async_prop *VAR = async_props; \
10236 VAR < &async_props[ARRAY_SIZE(async_props)]; VAR++)
10238 static const struct ofp14_async_prop
*
10239 get_ofp14_async_config_prop_by_prop_type(uint64_t prop_type
)
10241 FOR_EACH_ASYNC_PROP (ap
) {
10242 if (prop_type
== ap
->prop_type
) {
10249 static const struct ofp14_async_prop
*
10250 get_ofp14_async_config_prop_by_oam(enum ofputil_async_msg_type oam
,
10253 FOR_EACH_ASYNC_PROP (ap
) {
10254 if (ap
->oam
== oam
&& ap
->master
== master
) {
10262 ofp14_async_prop_allowed(const struct ofp14_async_prop
*prop
,
10263 enum ofp_version version
)
10265 return version
>= OFP14_VERSION
? prop
->allowed14
: prop
->allowed10
;
10269 encode_async_mask(const struct ofputil_async_cfg
*src
,
10270 const struct ofp14_async_prop
*ap
,
10271 enum ofp_version version
)
10273 uint32_t mask
= ap
->master
? src
->master
[ap
->oam
] : src
->slave
[ap
->oam
];
10274 return htonl(mask
& ofp14_async_prop_allowed(ap
, version
));
10278 decode_async_mask(ovs_be32 src
,
10279 const struct ofp14_async_prop
*ap
, enum ofp_version version
,
10280 bool loose
, struct ofputil_async_cfg
*dst
)
10282 uint32_t mask
= ntohl(src
);
10283 uint32_t allowed
= ofp14_async_prop_allowed(ap
, version
);
10284 if (mask
& ~allowed
) {
10285 OFPPROP_LOG(&bad_ofmsg_rl
, loose
,
10286 "bad value %#x for %s (allowed mask %#x)",
10287 mask
, ofputil_async_msg_type_to_string(ap
->oam
),
10291 return OFPERR_OFPACFC_INVALID
;
10295 if (ap
->oam
== OAM_PACKET_IN
) {
10296 if (mask
& (1u << OFPR_NO_MATCH
)) {
10297 mask
|= 1u << OFPR_EXPLICIT_MISS
;
10298 if (version
< OFP13_VERSION
) {
10299 mask
|= 1u << OFPR_IMPLICIT_MISS
;
10304 uint32_t *array
= ap
->master
? dst
->master
: dst
->slave
;
10305 array
[ap
->oam
] = mask
;
10310 parse_async_tlv(const struct ofpbuf
*property
,
10311 const struct ofp14_async_prop
*ap
,
10312 struct ofputil_async_cfg
*ac
,
10313 enum ofp_version version
, bool loose
)
10318 error
= ofpprop_parse_be32(property
, &mask
);
10323 if (ofpprop_is_experimenter(ap
->prop_type
)) {
10324 /* For experimenter properties, whether a property is for the master or
10325 * slave role is indicated by both 'type' and 'exp_type' in struct
10326 * ofp_prop_experimenter. Check that these are consistent. */
10327 const struct ofp_prop_experimenter
*ope
= property
->data
;
10328 bool should_be_master
= ope
->type
== htons(0xffff);
10329 if (should_be_master
!= ap
->master
) {
10330 VLOG_WARN_RL(&bad_ofmsg_rl
, "async property type %#"PRIx16
" "
10331 "indicates %s role but exp_type %"PRIu32
" indicates "
10334 should_be_master
? "master" : "slave",
10335 ntohl(ope
->exp_type
),
10336 ap
->master
? "master" : "slave");
10337 return OFPERR_OFPBPC_BAD_EXP_TYPE
;
10341 return decode_async_mask(mask
, ap
, version
, loose
, ac
);
10345 decode_legacy_async_masks(const ovs_be32 masks
[2],
10346 enum ofputil_async_msg_type oam
,
10347 enum ofp_version version
,
10348 struct ofputil_async_cfg
*dst
)
10350 for (int i
= 0; i
< 2; i
++) {
10351 bool master
= i
== 0;
10352 const struct ofp14_async_prop
*ap
10353 = get_ofp14_async_config_prop_by_oam(oam
, master
);
10354 decode_async_mask(masks
[i
], ap
, version
, true, dst
);
10358 /* Decodes the OpenFlow "set async config" request and "get async config
10359 * reply" message in '*oh' into an abstract form in 'ac'.
10361 * Some versions of the "set async config" request change only some of the
10362 * settings and leave the others alone. This function uses 'basis' as the
10363 * initial state for decoding these. Other versions of the request change all
10364 * the settings; this function ignores 'basis' when decoding these.
10366 * If 'loose' is true, this function ignores properties and values that it does
10367 * not understand, as a controller would want to do when interpreting
10368 * capabilities provided by a switch. If 'loose' is false, this function
10369 * treats unknown properties and values as an error, as a switch would want to
10370 * do when interpreting a configuration request made by a controller.
10372 * Returns 0 if successful, otherwise an OFPERR_* value.
10374 * Returns error code OFPERR_OFPACFC_INVALID if the value of mask is not in
10375 * the valid range of mask.
10377 * Returns error code OFPERR_OFPACFC_UNSUPPORTED if the configuration is not
10380 ofputil_decode_set_async_config(const struct ofp_header
*oh
, bool loose
,
10381 const struct ofputil_async_cfg
*basis
,
10382 struct ofputil_async_cfg
*ac
)
10384 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
10385 enum ofpraw raw
= ofpraw_pull_assert(&b
);
10387 if (raw
== OFPRAW_OFPT13_SET_ASYNC
||
10388 raw
== OFPRAW_NXT_SET_ASYNC_CONFIG
||
10389 raw
== OFPRAW_OFPT13_GET_ASYNC_REPLY
) {
10390 const struct nx_async_config
*msg
= ofpmsg_body(oh
);
10392 *ac
= OFPUTIL_ASYNC_CFG_INIT
;
10393 decode_legacy_async_masks(msg
->packet_in_mask
, OAM_PACKET_IN
,
10395 decode_legacy_async_masks(msg
->port_status_mask
, OAM_PORT_STATUS
,
10397 decode_legacy_async_masks(msg
->flow_removed_mask
, OAM_FLOW_REMOVED
,
10399 } else if (raw
== OFPRAW_OFPT14_SET_ASYNC
||
10400 raw
== OFPRAW_OFPT14_GET_ASYNC_REPLY
||
10401 raw
== OFPRAW_NXT_SET_ASYNC_CONFIG2
) {
10403 while (b
.size
> 0) {
10404 struct ofpbuf property
;
10408 error
= ofpprop_pull__(&b
, &property
, 8, 0xfffe, &type
);
10413 const struct ofp14_async_prop
*ap
10414 = get_ofp14_async_config_prop_by_prop_type(type
);
10416 ? parse_async_tlv(&property
, ap
, ac
, oh
->version
, loose
)
10417 : OFPPROP_UNKNOWN(loose
, "async config", type
));
10419 /* Most messages use OFPBPC_BAD_TYPE but async has its own (who
10420 * knows why, it's OpenFlow. */
10421 if (error
== OFPERR_OFPBPC_BAD_TYPE
) {
10422 error
= OFPERR_OFPACFC_UNSUPPORTED
;
10428 return OFPERR_OFPBRC_BAD_VERSION
;
10434 encode_legacy_async_masks(const struct ofputil_async_cfg
*ac
,
10435 enum ofputil_async_msg_type oam
,
10436 enum ofp_version version
,
10439 for (int i
= 0; i
< 2; i
++) {
10440 bool master
= i
== 0;
10441 const struct ofp14_async_prop
*ap
10442 = get_ofp14_async_config_prop_by_oam(oam
, master
);
10443 masks
[i
] = encode_async_mask(ac
, ap
, version
);
10448 ofputil_put_async_config__(const struct ofputil_async_cfg
*ac
,
10449 struct ofpbuf
*buf
, bool tlv
,
10450 enum ofp_version version
, uint32_t oams
)
10453 struct nx_async_config
*msg
= ofpbuf_put_zeros(buf
, sizeof *msg
);
10454 encode_legacy_async_masks(ac
, OAM_PACKET_IN
, version
,
10455 msg
->packet_in_mask
);
10456 encode_legacy_async_masks(ac
, OAM_PORT_STATUS
, version
,
10457 msg
->port_status_mask
);
10458 encode_legacy_async_masks(ac
, OAM_FLOW_REMOVED
, version
,
10459 msg
->flow_removed_mask
);
10461 FOR_EACH_ASYNC_PROP (ap
) {
10462 if (oams
& (1u << ap
->oam
)) {
10463 size_t ofs
= buf
->size
;
10464 ofpprop_put_be32(buf
, ap
->prop_type
,
10465 encode_async_mask(ac
, ap
, version
));
10467 /* For experimenter properties, we need to use type 0xfffe for
10468 * master and 0xffff for slaves. */
10469 if (ofpprop_is_experimenter(ap
->prop_type
)) {
10470 struct ofp_prop_experimenter
*ope
10471 = ofpbuf_at_assert(buf
, ofs
, sizeof *ope
);
10472 ope
->type
= ap
->master
? htons(0xffff) : htons(0xfffe);
10479 /* Encodes and returns a reply to the OFPT_GET_ASYNC_REQUEST in 'oh' that
10480 * states that the asynchronous message configuration is 'ac'. */
10482 ofputil_encode_get_async_reply(const struct ofp_header
*oh
,
10483 const struct ofputil_async_cfg
*ac
)
10485 struct ofpbuf
*buf
;
10487 enum ofpraw raw
= (oh
->version
< OFP14_VERSION
10488 ? OFPRAW_OFPT13_GET_ASYNC_REPLY
10489 : OFPRAW_OFPT14_GET_ASYNC_REPLY
);
10490 struct ofpbuf
*reply
= ofpraw_alloc_reply(raw
, oh
, 0);
10491 ofputil_put_async_config__(ac
, reply
,
10492 raw
== OFPRAW_OFPT14_GET_ASYNC_REPLY
,
10493 oh
->version
, UINT32_MAX
);
10499 /* Encodes and returns a message, in a format appropriate for OpenFlow version
10500 * 'ofp_version', that sets the asynchronous message configuration to 'ac'.
10502 * Specify 'oams' as a bitmap of OAM_* that indicate the asynchronous messages
10503 * to configure. OF1.0 through OF1.3 can't natively configure a subset of
10504 * messages, so more messages than requested may be configured. OF1.0 through
10505 * OF1.3 also can't configure OVS extension OAM_* values, so if 'oam' includes
10506 * any extensions then this function encodes an Open vSwitch extension message
10507 * that does support configuring OVS extension OAM_*. */
10509 ofputil_encode_set_async_config(const struct ofputil_async_cfg
*ac
,
10510 uint32_t oams
, enum ofp_version ofp_version
)
10512 enum ofpraw raw
= (ofp_version
>= OFP14_VERSION
? OFPRAW_OFPT14_SET_ASYNC
10513 : oams
& OAM_EXTENSIONS
? OFPRAW_NXT_SET_ASYNC_CONFIG2
10514 : ofp_version
>= OFP13_VERSION
? OFPRAW_OFPT13_SET_ASYNC
10515 : OFPRAW_NXT_SET_ASYNC_CONFIG
);
10516 struct ofpbuf
*request
= ofpraw_alloc(raw
, ofp_version
, 0);
10517 ofputil_put_async_config__(ac
, request
,
10518 (raw
== OFPRAW_OFPT14_SET_ASYNC
||
10519 raw
== OFPRAW_NXT_SET_ASYNC_CONFIG2
),
10520 ofp_version
, oams
);
10524 struct ofputil_async_cfg
10525 ofputil_async_cfg_default(enum ofp_version version
)
10527 /* We enable all of the OF1.4 reasons regardless of 'version' because the
10528 * reasons added in OF1.4 just are just refinements of the OFPR_ACTION
10529 * introduced in OF1.0, breaking it into more specific categories. When we
10530 * encode these for earlier OpenFlow versions, we translate them into
10532 uint32_t pin
= OFPR14_BITS
& ~(1u << OFPR_INVALID_TTL
);
10533 pin
|= 1u << OFPR_EXPLICIT_MISS
;
10534 if (version
<= OFP12_VERSION
) {
10535 pin
|= 1u << OFPR_IMPLICIT_MISS
;
10538 return (struct ofputil_async_cfg
) {
10539 .master
[OAM_PACKET_IN
] = pin
,
10541 .master
[OAM_FLOW_REMOVED
]
10542 = (version
>= OFP14_VERSION
? OFPRR14_BITS
: OFPRR10_BITS
),
10544 .master
[OAM_PORT_STATUS
] = OFPPR_BITS
,
10545 .slave
[OAM_PORT_STATUS
] = OFPPR_BITS
,
10550 ofputil_put_ofp14_table_desc(const struct ofputil_table_desc
*td
,
10551 struct ofpbuf
*b
, enum ofp_version version
)
10553 struct ofp14_table_desc
*otd
;
10554 struct ofp14_table_mod_prop_vacancy
*otv
;
10557 start_otd
= b
->size
;
10558 ofpbuf_put_zeros(b
, sizeof *otd
);
10560 ofpprop_put_u32(b
, OFPTMPT14_EVICTION
, td
->eviction_flags
);
10562 otv
= ofpbuf_put_zeros(b
, sizeof *otv
);
10563 otv
->type
= htons(OFPTMPT14_VACANCY
);
10564 otv
->length
= htons(sizeof *otv
);
10565 otv
->vacancy_down
= td
->table_vacancy
.vacancy_down
;
10566 otv
->vacancy_up
= td
->table_vacancy
.vacancy_up
;
10567 otv
->vacancy
= td
->table_vacancy
.vacancy
;
10569 otd
= ofpbuf_at_assert(b
, start_otd
, sizeof *otd
);
10570 otd
->length
= htons(b
->size
- start_otd
);
10571 otd
->table_id
= td
->table_id
;
10572 otd
->config
= ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT
,
10573 td
->eviction
, td
->vacancy
,
10577 /* Converts the abstract form of a "table status" message in '*ts' into an
10578 * OpenFlow message suitable for 'protocol', and returns that encoded form in
10579 * a buffer owned by the caller. */
10581 ofputil_encode_table_status(const struct ofputil_table_status
*ts
,
10582 enum ofputil_protocol protocol
)
10584 enum ofp_version version
;
10587 version
= ofputil_protocol_to_ofp_version(protocol
);
10588 if (version
>= OFP14_VERSION
) {
10590 struct ofp14_table_status
*ots
;
10592 raw
= OFPRAW_OFPT14_TABLE_STATUS
;
10593 b
= ofpraw_alloc_xid(raw
, version
, htonl(0), 0);
10594 ots
= ofpbuf_put_zeros(b
, sizeof *ots
);
10595 ots
->reason
= ts
->reason
;
10596 ofputil_put_ofp14_table_desc(&ts
->desc
, b
, version
);
10597 ofpmsg_update_length(b
);
10604 /* Decodes the OpenFlow "table status" message in '*ots' into an abstract form
10605 * in '*ts'. Returns 0 if successful, otherwise an OFPERR_* value. */
10607 ofputil_decode_table_status(const struct ofp_header
*oh
,
10608 struct ofputil_table_status
*ts
)
10610 const struct ofp14_table_status
*ots
;
10615 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
10616 raw
= ofpraw_pull_assert(&b
);
10617 ots
= ofpbuf_pull(&b
, sizeof *ots
);
10619 if (raw
== OFPRAW_OFPT14_TABLE_STATUS
) {
10620 if (ots
->reason
!= OFPTR_VACANCY_DOWN
10621 && ots
->reason
!= OFPTR_VACANCY_UP
) {
10622 return OFPERR_OFPBPC_BAD_VALUE
;
10624 ts
->reason
= ots
->reason
;
10626 error
= ofputil_decode_table_desc(&b
, &ts
->desc
, oh
->version
);
10629 return OFPERR_OFPBRC_BAD_VERSION
;