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
, const struct tun_table
*tun_table
,
281 struct match
*match
, 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
, tun_table
, 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 const struct tun_table
*tun_table
,
1574 struct ofpbuf
*ofpacts
,
1575 ofp_port_t max_port
, uint8_t max_table
)
1579 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
1580 enum ofpraw raw
= ofpraw_pull_assert(&b
);
1581 if (raw
== OFPRAW_OFPT11_FLOW_MOD
) {
1582 /* Standard OpenFlow 1.1+ flow_mod. */
1583 const struct ofp11_flow_mod
*ofm
;
1585 ofm
= ofpbuf_pull(&b
, sizeof *ofm
);
1587 error
= ofputil_pull_ofp11_match(&b
, tun_table
, &fm
->match
, NULL
);
1592 /* Translate the message. */
1593 fm
->priority
= ntohs(ofm
->priority
);
1594 if (ofm
->command
== OFPFC_ADD
1595 || (oh
->version
== OFP11_VERSION
1596 && (ofm
->command
== OFPFC_MODIFY
||
1597 ofm
->command
== OFPFC_MODIFY_STRICT
)
1598 && ofm
->cookie_mask
== htonll(0))) {
1599 /* In OpenFlow 1.1 only, a "modify" or "modify-strict" that does
1600 * not match on the cookie is treated as an "add" if there is no
1602 fm
->cookie
= htonll(0);
1603 fm
->cookie_mask
= htonll(0);
1604 fm
->new_cookie
= ofm
->cookie
;
1606 fm
->cookie
= ofm
->cookie
;
1607 fm
->cookie_mask
= ofm
->cookie_mask
;
1608 fm
->new_cookie
= OVS_BE64_MAX
;
1610 fm
->modify_cookie
= false;
1611 fm
->command
= ofm
->command
;
1615 * OF1.1 entirely forbids table_id == OFPTT_ALL.
1616 * OF1.2+ allows table_id == OFPTT_ALL only for deletes. */
1617 fm
->table_id
= ofm
->table_id
;
1618 if (fm
->table_id
== OFPTT_ALL
1619 && (oh
->version
== OFP11_VERSION
1620 || (ofm
->command
!= OFPFC_DELETE
&&
1621 ofm
->command
!= OFPFC_DELETE_STRICT
))) {
1622 return OFPERR_OFPFMFC_BAD_TABLE_ID
;
1625 fm
->idle_timeout
= ntohs(ofm
->idle_timeout
);
1626 fm
->hard_timeout
= ntohs(ofm
->hard_timeout
);
1627 if (oh
->version
>= OFP14_VERSION
&& ofm
->command
== OFPFC_ADD
) {
1628 fm
->importance
= ntohs(ofm
->importance
);
1632 fm
->buffer_id
= ntohl(ofm
->buffer_id
);
1633 error
= ofputil_port_from_ofp11(ofm
->out_port
, &fm
->out_port
);
1638 fm
->out_group
= (ofm
->command
== OFPFC_DELETE
||
1639 ofm
->command
== OFPFC_DELETE_STRICT
1640 ? ntohl(ofm
->out_group
)
1642 raw_flags
= ofm
->flags
;
1646 if (raw
== OFPRAW_OFPT10_FLOW_MOD
) {
1647 /* Standard OpenFlow 1.0 flow_mod. */
1648 const struct ofp10_flow_mod
*ofm
;
1650 /* Get the ofp10_flow_mod. */
1651 ofm
= ofpbuf_pull(&b
, sizeof *ofm
);
1653 /* Translate the rule. */
1654 ofputil_match_from_ofp10_match(&ofm
->match
, &fm
->match
);
1655 ofputil_normalize_match(&fm
->match
);
1657 /* OpenFlow 1.0 says that exact-match rules have to have the
1658 * highest possible priority. */
1659 fm
->priority
= (ofm
->match
.wildcards
& htonl(OFPFW10_ALL
)
1660 ? ntohs(ofm
->priority
)
1663 /* Translate the message. */
1664 command
= ntohs(ofm
->command
);
1665 fm
->cookie
= htonll(0);
1666 fm
->cookie_mask
= htonll(0);
1667 fm
->new_cookie
= ofm
->cookie
;
1668 fm
->idle_timeout
= ntohs(ofm
->idle_timeout
);
1669 fm
->hard_timeout
= ntohs(ofm
->hard_timeout
);
1671 fm
->buffer_id
= ntohl(ofm
->buffer_id
);
1672 fm
->out_port
= u16_to_ofp(ntohs(ofm
->out_port
));
1673 fm
->out_group
= OFPG_ANY
;
1674 raw_flags
= ofm
->flags
;
1675 } else if (raw
== OFPRAW_NXT_FLOW_MOD
) {
1676 /* Nicira extended flow_mod. */
1677 const struct nx_flow_mod
*nfm
;
1679 /* Dissect the message. */
1680 nfm
= ofpbuf_pull(&b
, sizeof *nfm
);
1681 error
= nx_pull_match(&b
, ntohs(nfm
->match_len
),
1682 &fm
->match
, &fm
->cookie
, &fm
->cookie_mask
,
1688 /* Translate the message. */
1689 command
= ntohs(nfm
->command
);
1690 if ((command
& 0xff) == OFPFC_ADD
&& fm
->cookie_mask
) {
1691 /* Flow additions may only set a new cookie, not match an
1692 * existing cookie. */
1693 return OFPERR_NXBRC_NXM_INVALID
;
1695 fm
->priority
= ntohs(nfm
->priority
);
1696 fm
->new_cookie
= nfm
->cookie
;
1697 fm
->idle_timeout
= ntohs(nfm
->idle_timeout
);
1698 fm
->hard_timeout
= ntohs(nfm
->hard_timeout
);
1700 fm
->buffer_id
= ntohl(nfm
->buffer_id
);
1701 fm
->out_port
= u16_to_ofp(ntohs(nfm
->out_port
));
1702 fm
->out_group
= OFPG_ANY
;
1703 raw_flags
= nfm
->flags
;
1708 fm
->modify_cookie
= fm
->new_cookie
!= OVS_BE64_MAX
;
1709 if (protocol
& OFPUTIL_P_TID
) {
1710 fm
->command
= command
& 0xff;
1711 fm
->table_id
= command
>> 8;
1713 if (command
> 0xff) {
1714 VLOG_WARN_RL(&bad_ofmsg_rl
, "flow_mod has explicit table_id "
1715 "but flow_mod_table_id extension is not enabled");
1717 fm
->command
= command
;
1718 fm
->table_id
= 0xff;
1722 if (fm
->command
> OFPFC_DELETE_STRICT
) {
1723 return OFPERR_OFPFMFC_BAD_COMMAND
;
1726 error
= ofpacts_pull_openflow_instructions(&b
, b
.size
,
1727 oh
->version
, ofpacts
);
1731 fm
->ofpacts
= ofpacts
->data
;
1732 fm
->ofpacts_len
= ofpacts
->size
;
1734 error
= ofputil_decode_flow_mod_flags(raw_flags
, fm
->command
,
1735 oh
->version
, &fm
->flags
);
1740 if (fm
->flags
& OFPUTIL_FF_EMERG
) {
1741 /* We do not support the OpenFlow 1.0 emergency flow cache, which
1742 * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
1744 * OpenFlow 1.0 specifies the error code to use when idle_timeout
1745 * or hard_timeout is nonzero. Otherwise, there is no good error
1746 * code, so just state that the flow table is full. */
1747 return (fm
->hard_timeout
|| fm
->idle_timeout
1748 ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT
1749 : OFPERR_OFPFMFC_TABLE_FULL
);
1752 return ofpacts_check_consistency(fm
->ofpacts
, fm
->ofpacts_len
,
1753 &fm
->match
.flow
, max_port
,
1754 fm
->table_id
, max_table
, protocol
);
1758 ofputil_pull_bands(struct ofpbuf
*msg
, size_t len
, uint16_t *n_bands
,
1759 struct ofpbuf
*bands
)
1761 const struct ofp13_meter_band_header
*ombh
;
1762 struct ofputil_meter_band
*mb
;
1765 ombh
= ofpbuf_try_pull(msg
, len
);
1767 return OFPERR_OFPBRC_BAD_LEN
;
1770 while (len
>= sizeof (struct ofp13_meter_band_drop
)) {
1771 size_t ombh_len
= ntohs(ombh
->len
);
1772 /* All supported band types have the same length. */
1773 if (ombh_len
!= sizeof (struct ofp13_meter_band_drop
)) {
1774 return OFPERR_OFPBRC_BAD_LEN
;
1776 mb
= ofpbuf_put_uninit(bands
, sizeof *mb
);
1777 mb
->type
= ntohs(ombh
->type
);
1778 if (mb
->type
!= OFPMBT13_DROP
&& mb
->type
!= OFPMBT13_DSCP_REMARK
) {
1779 return OFPERR_OFPMMFC_BAD_BAND
;
1781 mb
->rate
= ntohl(ombh
->rate
);
1782 mb
->burst_size
= ntohl(ombh
->burst_size
);
1783 mb
->prec_level
= (mb
->type
== OFPMBT13_DSCP_REMARK
) ?
1784 ((struct ofp13_meter_band_dscp_remark
*)ombh
)->prec_level
: 0;
1787 ombh
= ALIGNED_CAST(struct ofp13_meter_band_header
*,
1788 (char *) ombh
+ ombh_len
);
1791 return OFPERR_OFPBRC_BAD_LEN
;
1798 ofputil_decode_meter_mod(const struct ofp_header
*oh
,
1799 struct ofputil_meter_mod
*mm
,
1800 struct ofpbuf
*bands
)
1802 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
1803 ofpraw_pull_assert(&b
);
1804 const struct ofp13_meter_mod
*omm
= ofpbuf_pull(&b
, sizeof *omm
);
1806 /* Translate the message. */
1807 mm
->command
= ntohs(omm
->command
);
1808 if (mm
->command
!= OFPMC13_ADD
&&
1809 mm
->command
!= OFPMC13_MODIFY
&&
1810 mm
->command
!= OFPMC13_DELETE
) {
1811 return OFPERR_OFPMMFC_BAD_COMMAND
;
1813 mm
->meter
.meter_id
= ntohl(omm
->meter_id
);
1815 if (mm
->command
== OFPMC13_DELETE
) {
1816 mm
->meter
.flags
= 0;
1817 mm
->meter
.n_bands
= 0;
1818 mm
->meter
.bands
= NULL
;
1822 mm
->meter
.flags
= ntohs(omm
->flags
);
1823 if (mm
->meter
.flags
& OFPMF13_KBPS
&&
1824 mm
->meter
.flags
& OFPMF13_PKTPS
) {
1825 return OFPERR_OFPMMFC_BAD_FLAGS
;
1827 mm
->meter
.bands
= bands
->data
;
1829 error
= ofputil_pull_bands(&b
, b
.size
, &mm
->meter
.n_bands
, bands
);
1838 ofputil_decode_meter_request(const struct ofp_header
*oh
, uint32_t *meter_id
)
1840 const struct ofp13_meter_multipart_request
*omr
= ofpmsg_body(oh
);
1841 *meter_id
= ntohl(omr
->meter_id
);
1845 ofputil_encode_meter_request(enum ofp_version ofp_version
,
1846 enum ofputil_meter_request_type type
,
1854 case OFPUTIL_METER_CONFIG
:
1855 raw
= OFPRAW_OFPST13_METER_CONFIG_REQUEST
;
1857 case OFPUTIL_METER_STATS
:
1858 raw
= OFPRAW_OFPST13_METER_REQUEST
;
1861 case OFPUTIL_METER_FEATURES
:
1862 raw
= OFPRAW_OFPST13_METER_FEATURES_REQUEST
;
1866 msg
= ofpraw_alloc(raw
, ofp_version
, 0);
1868 if (type
!= OFPUTIL_METER_FEATURES
) {
1869 struct ofp13_meter_multipart_request
*omr
;
1870 omr
= ofpbuf_put_zeros(msg
, sizeof *omr
);
1871 omr
->meter_id
= htonl(meter_id
);
1877 ofputil_put_bands(uint16_t n_bands
, const struct ofputil_meter_band
*mb
,
1882 for (n
= 0; n
< n_bands
; ++n
) {
1883 /* Currently all band types have same size. */
1884 struct ofp13_meter_band_dscp_remark
*ombh
;
1885 size_t ombh_len
= sizeof *ombh
;
1887 ombh
= ofpbuf_put_zeros(msg
, ombh_len
);
1889 ombh
->type
= htons(mb
->type
);
1890 ombh
->len
= htons(ombh_len
);
1891 ombh
->rate
= htonl(mb
->rate
);
1892 ombh
->burst_size
= htonl(mb
->burst_size
);
1893 ombh
->prec_level
= mb
->prec_level
;
1899 /* Encode a meter stat for 'mc' and append it to 'replies'. */
1901 ofputil_append_meter_config(struct ovs_list
*replies
,
1902 const struct ofputil_meter_config
*mc
)
1904 struct ofpbuf
*msg
= ofpbuf_from_list(ovs_list_back(replies
));
1905 size_t start_ofs
= msg
->size
;
1906 struct ofp13_meter_config
*reply
;
1908 ofpbuf_put_uninit(msg
, sizeof *reply
);
1909 ofputil_put_bands(mc
->n_bands
, mc
->bands
, msg
);
1911 reply
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *reply
);
1912 reply
->flags
= htons(mc
->flags
);
1913 reply
->meter_id
= htonl(mc
->meter_id
);
1914 reply
->length
= htons(msg
->size
- start_ofs
);
1916 ofpmp_postappend(replies
, start_ofs
);
1919 /* Encode a meter stat for 'ms' and append it to 'replies'. */
1921 ofputil_append_meter_stats(struct ovs_list
*replies
,
1922 const struct ofputil_meter_stats
*ms
)
1924 struct ofp13_meter_stats
*reply
;
1928 len
= sizeof *reply
+ ms
->n_bands
* sizeof(struct ofp13_meter_band_stats
);
1929 reply
= ofpmp_append(replies
, len
);
1931 reply
->meter_id
= htonl(ms
->meter_id
);
1932 reply
->len
= htons(len
);
1933 memset(reply
->pad
, 0, sizeof reply
->pad
);
1934 reply
->flow_count
= htonl(ms
->flow_count
);
1935 reply
->packet_in_count
= htonll(ms
->packet_in_count
);
1936 reply
->byte_in_count
= htonll(ms
->byte_in_count
);
1937 reply
->duration_sec
= htonl(ms
->duration_sec
);
1938 reply
->duration_nsec
= htonl(ms
->duration_nsec
);
1940 for (n
= 0; n
< ms
->n_bands
; ++n
) {
1941 const struct ofputil_meter_band_stats
*src
= &ms
->bands
[n
];
1942 struct ofp13_meter_band_stats
*dst
= &reply
->band_stats
[n
];
1944 dst
->packet_band_count
= htonll(src
->packet_count
);
1945 dst
->byte_band_count
= htonll(src
->byte_count
);
1949 /* Converts an OFPMP_METER_CONFIG reply in 'msg' into an abstract
1950 * ofputil_meter_config in 'mc', with mc->bands pointing to bands decoded into
1951 * 'bands'. The caller must have initialized 'bands' and retains ownership of
1952 * it across the call.
1954 * Multiple OFPST13_METER_CONFIG replies can be packed into a single OpenFlow
1955 * message. Calling this function multiple times for a single 'msg' iterates
1956 * through the replies. 'bands' is cleared for each reply.
1958 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1959 * otherwise a positive errno value. */
1961 ofputil_decode_meter_config(struct ofpbuf
*msg
,
1962 struct ofputil_meter_config
*mc
,
1963 struct ofpbuf
*bands
)
1965 const struct ofp13_meter_config
*omc
;
1968 /* Pull OpenFlow headers for the first call. */
1970 ofpraw_pull_assert(msg
);
1977 omc
= ofpbuf_try_pull(msg
, sizeof *omc
);
1979 VLOG_WARN_RL(&bad_ofmsg_rl
,
1980 "OFPMP_METER_CONFIG reply has %"PRIu32
" leftover bytes at end",
1982 return OFPERR_OFPBRC_BAD_LEN
;
1985 ofpbuf_clear(bands
);
1986 err
= ofputil_pull_bands(msg
, ntohs(omc
->length
) - sizeof *omc
,
1987 &mc
->n_bands
, bands
);
1991 mc
->meter_id
= ntohl(omc
->meter_id
);
1992 mc
->flags
= ntohs(omc
->flags
);
1993 mc
->bands
= bands
->data
;
1999 ofputil_pull_band_stats(struct ofpbuf
*msg
, size_t len
, uint16_t *n_bands
,
2000 struct ofpbuf
*bands
)
2002 const struct ofp13_meter_band_stats
*ombs
;
2003 struct ofputil_meter_band_stats
*mbs
;
2006 ombs
= ofpbuf_try_pull(msg
, len
);
2008 return OFPERR_OFPBRC_BAD_LEN
;
2011 n
= len
/ sizeof *ombs
;
2012 if (len
!= n
* sizeof *ombs
) {
2013 return OFPERR_OFPBRC_BAD_LEN
;
2016 mbs
= ofpbuf_put_uninit(bands
, len
);
2018 for (i
= 0; i
< n
; ++i
) {
2019 mbs
[i
].packet_count
= ntohll(ombs
[i
].packet_band_count
);
2020 mbs
[i
].byte_count
= ntohll(ombs
[i
].byte_band_count
);
2026 /* Converts an OFPMP_METER reply in 'msg' into an abstract
2027 * ofputil_meter_stats in 'ms', with ms->bands pointing to band stats
2028 * decoded into 'bands'.
2030 * Multiple OFPMP_METER replies can be packed into a single OpenFlow
2031 * message. Calling this function multiple times for a single 'msg' iterates
2032 * through the replies. 'bands' is cleared for each reply.
2034 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2035 * otherwise a positive errno value. */
2037 ofputil_decode_meter_stats(struct ofpbuf
*msg
,
2038 struct ofputil_meter_stats
*ms
,
2039 struct ofpbuf
*bands
)
2041 const struct ofp13_meter_stats
*oms
;
2044 /* Pull OpenFlow headers for the first call. */
2046 ofpraw_pull_assert(msg
);
2053 oms
= ofpbuf_try_pull(msg
, sizeof *oms
);
2055 VLOG_WARN_RL(&bad_ofmsg_rl
,
2056 "OFPMP_METER reply has %"PRIu32
" leftover bytes at end",
2058 return OFPERR_OFPBRC_BAD_LEN
;
2061 ofpbuf_clear(bands
);
2062 err
= ofputil_pull_band_stats(msg
, ntohs(oms
->len
) - sizeof *oms
,
2063 &ms
->n_bands
, bands
);
2067 ms
->meter_id
= ntohl(oms
->meter_id
);
2068 ms
->flow_count
= ntohl(oms
->flow_count
);
2069 ms
->packet_in_count
= ntohll(oms
->packet_in_count
);
2070 ms
->byte_in_count
= ntohll(oms
->byte_in_count
);
2071 ms
->duration_sec
= ntohl(oms
->duration_sec
);
2072 ms
->duration_nsec
= ntohl(oms
->duration_nsec
);
2073 ms
->bands
= bands
->data
;
2079 ofputil_decode_meter_features(const struct ofp_header
*oh
,
2080 struct ofputil_meter_features
*mf
)
2082 const struct ofp13_meter_features
*omf
= ofpmsg_body(oh
);
2084 mf
->max_meters
= ntohl(omf
->max_meter
);
2085 mf
->band_types
= ntohl(omf
->band_types
);
2086 mf
->capabilities
= ntohl(omf
->capabilities
);
2087 mf
->max_bands
= omf
->max_bands
;
2088 mf
->max_color
= omf
->max_color
;
2092 ofputil_encode_meter_features_reply(const struct ofputil_meter_features
*mf
,
2093 const struct ofp_header
*request
)
2095 struct ofpbuf
*reply
;
2096 struct ofp13_meter_features
*omf
;
2098 reply
= ofpraw_alloc_stats_reply(request
, 0);
2099 omf
= ofpbuf_put_zeros(reply
, sizeof *omf
);
2101 omf
->max_meter
= htonl(mf
->max_meters
);
2102 omf
->band_types
= htonl(mf
->band_types
);
2103 omf
->capabilities
= htonl(mf
->capabilities
);
2104 omf
->max_bands
= mf
->max_bands
;
2105 omf
->max_color
= mf
->max_color
;
2111 ofputil_encode_meter_mod(enum ofp_version ofp_version
,
2112 const struct ofputil_meter_mod
*mm
)
2116 struct ofp13_meter_mod
*omm
;
2118 msg
= ofpraw_alloc(OFPRAW_OFPT13_METER_MOD
, ofp_version
,
2119 NXM_TYPICAL_LEN
+ mm
->meter
.n_bands
* 16);
2120 omm
= ofpbuf_put_zeros(msg
, sizeof *omm
);
2121 omm
->command
= htons(mm
->command
);
2122 if (mm
->command
!= OFPMC13_DELETE
) {
2123 omm
->flags
= htons(mm
->meter
.flags
);
2125 omm
->meter_id
= htonl(mm
->meter
.meter_id
);
2127 ofputil_put_bands(mm
->meter
.n_bands
, mm
->meter
.bands
, msg
);
2129 ofpmsg_update_length(msg
);
2134 ofputil_tid_command(const struct ofputil_flow_mod
*fm
,
2135 enum ofputil_protocol protocol
)
2137 return htons(protocol
& OFPUTIL_P_TID
2138 ? (fm
->command
& 0xff) | (fm
->table_id
<< 8)
2142 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
2143 * 'protocol' and returns the message. */
2145 ofputil_encode_flow_mod(const struct ofputil_flow_mod
*fm
,
2146 enum ofputil_protocol protocol
)
2148 enum ofp_version version
= ofputil_protocol_to_ofp_version(protocol
);
2149 ovs_be16 raw_flags
= ofputil_encode_flow_mod_flags(fm
->flags
, version
);
2153 case OFPUTIL_P_OF11_STD
:
2154 case OFPUTIL_P_OF12_OXM
:
2155 case OFPUTIL_P_OF13_OXM
:
2156 case OFPUTIL_P_OF14_OXM
:
2157 case OFPUTIL_P_OF15_OXM
:
2158 case OFPUTIL_P_OF16_OXM
: {
2159 struct ofp11_flow_mod
*ofm
;
2162 tailroom
= ofputil_match_typical_len(protocol
) + fm
->ofpacts_len
;
2163 msg
= ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD
, version
, tailroom
);
2164 ofm
= ofpbuf_put_zeros(msg
, sizeof *ofm
);
2165 if ((protocol
== OFPUTIL_P_OF11_STD
2166 && (fm
->command
== OFPFC_MODIFY
||
2167 fm
->command
== OFPFC_MODIFY_STRICT
)
2168 && fm
->cookie_mask
== htonll(0))
2169 || fm
->command
== OFPFC_ADD
) {
2170 ofm
->cookie
= fm
->new_cookie
;
2172 ofm
->cookie
= fm
->cookie
& fm
->cookie_mask
;
2174 ofm
->cookie_mask
= fm
->cookie_mask
;
2175 if (fm
->table_id
!= OFPTT_ALL
2176 || (protocol
!= OFPUTIL_P_OF11_STD
2177 && (fm
->command
== OFPFC_DELETE
||
2178 fm
->command
== OFPFC_DELETE_STRICT
))) {
2179 ofm
->table_id
= fm
->table_id
;
2183 ofm
->command
= fm
->command
;
2184 ofm
->idle_timeout
= htons(fm
->idle_timeout
);
2185 ofm
->hard_timeout
= htons(fm
->hard_timeout
);
2186 ofm
->priority
= htons(fm
->priority
);
2187 ofm
->buffer_id
= htonl(fm
->buffer_id
);
2188 ofm
->out_port
= ofputil_port_to_ofp11(fm
->out_port
);
2189 ofm
->out_group
= htonl(fm
->out_group
);
2190 ofm
->flags
= raw_flags
;
2191 if (version
>= OFP14_VERSION
&& fm
->command
== OFPFC_ADD
) {
2192 ofm
->importance
= htons(fm
->importance
);
2194 ofm
->importance
= 0;
2196 ofputil_put_ofp11_match(msg
, &fm
->match
, protocol
);
2197 ofpacts_put_openflow_instructions(fm
->ofpacts
, fm
->ofpacts_len
, msg
,
2202 case OFPUTIL_P_OF10_STD
:
2203 case OFPUTIL_P_OF10_STD_TID
: {
2204 struct ofp10_flow_mod
*ofm
;
2206 msg
= ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD
, OFP10_VERSION
,
2208 ofm
= ofpbuf_put_zeros(msg
, sizeof *ofm
);
2209 ofputil_match_to_ofp10_match(&fm
->match
, &ofm
->match
);
2210 ofm
->cookie
= fm
->new_cookie
;
2211 ofm
->command
= ofputil_tid_command(fm
, protocol
);
2212 ofm
->idle_timeout
= htons(fm
->idle_timeout
);
2213 ofm
->hard_timeout
= htons(fm
->hard_timeout
);
2214 ofm
->priority
= htons(fm
->priority
);
2215 ofm
->buffer_id
= htonl(fm
->buffer_id
);
2216 ofm
->out_port
= htons(ofp_to_u16(fm
->out_port
));
2217 ofm
->flags
= raw_flags
;
2218 ofpacts_put_openflow_actions(fm
->ofpacts
, fm
->ofpacts_len
, msg
,
2223 case OFPUTIL_P_OF10_NXM
:
2224 case OFPUTIL_P_OF10_NXM_TID
: {
2225 struct nx_flow_mod
*nfm
;
2228 msg
= ofpraw_alloc(OFPRAW_NXT_FLOW_MOD
, OFP10_VERSION
,
2229 NXM_TYPICAL_LEN
+ fm
->ofpacts_len
);
2230 nfm
= ofpbuf_put_zeros(msg
, sizeof *nfm
);
2231 nfm
->command
= ofputil_tid_command(fm
, protocol
);
2232 nfm
->cookie
= fm
->new_cookie
;
2233 match_len
= nx_put_match(msg
, &fm
->match
, fm
->cookie
, fm
->cookie_mask
);
2235 nfm
->idle_timeout
= htons(fm
->idle_timeout
);
2236 nfm
->hard_timeout
= htons(fm
->hard_timeout
);
2237 nfm
->priority
= htons(fm
->priority
);
2238 nfm
->buffer_id
= htonl(fm
->buffer_id
);
2239 nfm
->out_port
= htons(ofp_to_u16(fm
->out_port
));
2240 nfm
->flags
= raw_flags
;
2241 nfm
->match_len
= htons(match_len
);
2242 ofpacts_put_openflow_actions(fm
->ofpacts
, fm
->ofpacts_len
, msg
,
2251 ofpmsg_update_length(msg
);
2256 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request
*fsr
,
2257 const struct ofp10_flow_stats_request
*ofsr
,
2260 fsr
->aggregate
= aggregate
;
2261 ofputil_match_from_ofp10_match(&ofsr
->match
, &fsr
->match
);
2262 fsr
->out_port
= u16_to_ofp(ntohs(ofsr
->out_port
));
2263 fsr
->out_group
= OFPG_ANY
;
2264 fsr
->table_id
= ofsr
->table_id
;
2265 fsr
->cookie
= fsr
->cookie_mask
= htonll(0);
2271 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request
*fsr
,
2272 struct ofpbuf
*b
, bool aggregate
,
2273 const struct tun_table
*tun_table
)
2275 const struct ofp11_flow_stats_request
*ofsr
;
2278 ofsr
= ofpbuf_pull(b
, sizeof *ofsr
);
2279 fsr
->aggregate
= aggregate
;
2280 fsr
->table_id
= ofsr
->table_id
;
2281 error
= ofputil_port_from_ofp11(ofsr
->out_port
, &fsr
->out_port
);
2285 fsr
->out_group
= ntohl(ofsr
->out_group
);
2286 fsr
->cookie
= ofsr
->cookie
;
2287 fsr
->cookie_mask
= ofsr
->cookie_mask
;
2288 error
= ofputil_pull_ofp11_match(b
, tun_table
, &fsr
->match
, NULL
);
2297 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request
*fsr
,
2298 struct ofpbuf
*b
, bool aggregate
,
2299 const struct tun_table
*tun_table
)
2301 const struct nx_flow_stats_request
*nfsr
;
2304 nfsr
= ofpbuf_pull(b
, sizeof *nfsr
);
2305 error
= nx_pull_match(b
, ntohs(nfsr
->match_len
), &fsr
->match
,
2306 &fsr
->cookie
, &fsr
->cookie_mask
, tun_table
);
2311 return OFPERR_OFPBRC_BAD_LEN
;
2314 fsr
->aggregate
= aggregate
;
2315 fsr
->out_port
= u16_to_ofp(ntohs(nfsr
->out_port
));
2316 fsr
->out_group
= OFPG_ANY
;
2317 fsr
->table_id
= nfsr
->table_id
;
2322 /* Constructs and returns an OFPT_QUEUE_GET_CONFIG request for the specified
2323 * 'port' and 'queue', suitable for OpenFlow version 'version'.
2325 * 'queue' is honored only for OpenFlow 1.4 and later; older versions always
2326 * request all queues. */
2328 ofputil_encode_queue_get_config_request(enum ofp_version version
,
2332 struct ofpbuf
*request
;
2334 if (version
== OFP10_VERSION
) {
2335 struct ofp10_queue_get_config_request
*qgcr10
;
2337 request
= ofpraw_alloc(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST
,
2339 qgcr10
= ofpbuf_put_zeros(request
, sizeof *qgcr10
);
2340 qgcr10
->port
= htons(ofp_to_u16(port
));
2341 } else if (version
< OFP14_VERSION
) {
2342 struct ofp11_queue_get_config_request
*qgcr11
;
2344 request
= ofpraw_alloc(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST
,
2346 qgcr11
= ofpbuf_put_zeros(request
, sizeof *qgcr11
);
2347 qgcr11
->port
= ofputil_port_to_ofp11(port
);
2349 struct ofp14_queue_desc_request
*qdr14
;
2351 request
= ofpraw_alloc(OFPRAW_OFPST14_QUEUE_DESC_REQUEST
,
2353 qdr14
= ofpbuf_put_zeros(request
, sizeof *qdr14
);
2354 qdr14
->port
= ofputil_port_to_ofp11(port
);
2355 qdr14
->queue
= htonl(queue
);
2361 /* Parses OFPT_QUEUE_GET_CONFIG request 'oh', storing the port specified by the
2362 * request into '*port'. Returns 0 if successful, otherwise an OpenFlow error
2365 ofputil_decode_queue_get_config_request(const struct ofp_header
*oh
,
2366 ofp_port_t
*port
, uint32_t *queue
)
2368 const struct ofp10_queue_get_config_request
*qgcr10
;
2369 const struct ofp11_queue_get_config_request
*qgcr11
;
2370 const struct ofp14_queue_desc_request
*qdr14
;
2371 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
2372 enum ofpraw raw
= ofpraw_pull_assert(&b
);
2374 switch ((int) raw
) {
2375 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST
:
2377 *port
= u16_to_ofp(ntohs(qgcr10
->port
));
2381 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST
:
2384 enum ofperr error
= ofputil_port_from_ofp11(qgcr11
->port
, port
);
2385 if (error
|| *port
== OFPP_ANY
) {
2390 case OFPRAW_OFPST14_QUEUE_DESC_REQUEST
:
2392 *queue
= ntohl(qdr14
->queue
);
2393 return ofputil_port_from_ofp11(qdr14
->port
, port
);
2399 return (ofp_to_u16(*port
) < ofp_to_u16(OFPP_MAX
)
2401 : OFPERR_OFPQOFC_BAD_PORT
);
2404 /* Constructs and returns the beginning of a reply to
2405 * OFPT_QUEUE_GET_CONFIG_REQUEST or OFPMP_QUEUE_DESC request 'oh'. The caller
2406 * may append information about individual queues with
2407 * ofputil_append_queue_get_config_reply(). */
2409 ofputil_start_queue_get_config_reply(const struct ofp_header
*request
,
2410 struct ovs_list
*replies
)
2412 struct ofpbuf
*reply
;
2417 error
= ofputil_decode_queue_get_config_request(request
, &port
, &queue
);
2420 enum ofpraw raw
= ofpraw_decode_assert(request
);
2421 switch ((int) raw
) {
2422 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST
:
2423 reply
= ofpraw_alloc_reply(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY
,
2425 struct ofp10_queue_get_config_reply
*qgcr10
2426 = ofpbuf_put_zeros(reply
, sizeof *qgcr10
);
2427 qgcr10
->port
= htons(ofp_to_u16(port
));
2430 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST
:
2431 reply
= ofpraw_alloc_reply(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY
,
2433 struct ofp11_queue_get_config_reply
*qgcr11
2434 = ofpbuf_put_zeros(reply
, sizeof *qgcr11
);
2435 qgcr11
->port
= ofputil_port_to_ofp11(port
);
2438 case OFPRAW_OFPST14_QUEUE_DESC_REQUEST
:
2439 reply
= ofpraw_alloc_stats_reply(request
, 0);
2446 ovs_list_init(replies
);
2447 ovs_list_push_back(replies
, &reply
->list_node
);
2451 put_ofp10_queue_rate(struct ofpbuf
*reply
,
2452 enum ofp10_queue_properties property
, uint16_t rate
)
2454 if (rate
!= UINT16_MAX
) {
2455 struct ofp10_queue_prop_rate
*oqpr
;
2457 oqpr
= ofpbuf_put_zeros(reply
, sizeof *oqpr
);
2458 oqpr
->prop_header
.property
= htons(property
);
2459 oqpr
->prop_header
.len
= htons(sizeof *oqpr
);
2460 oqpr
->rate
= htons(rate
);
2465 put_ofp14_queue_rate(struct ofpbuf
*reply
,
2466 enum ofp14_queue_desc_prop_type type
, uint16_t rate
)
2468 if (rate
!= UINT16_MAX
) {
2469 ofpprop_put_u16(reply
, type
, rate
);
2474 ofputil_append_queue_get_config_reply(const struct ofputil_queue_config
*qc
,
2475 struct ovs_list
*replies
)
2477 enum ofp_version ofp_version
= ofpmp_version(replies
);
2478 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
2479 size_t start_ofs
= reply
->size
;
2483 if (ofp_version
< OFP14_VERSION
) {
2484 if (ofp_version
< OFP12_VERSION
) {
2485 struct ofp10_packet_queue
*opq10
;
2487 opq10
= ofpbuf_put_zeros(reply
, sizeof *opq10
);
2488 opq10
->queue_id
= htonl(qc
->queue
);
2489 len_ofs
= (char *) &opq10
->len
- (char *) reply
->data
;
2491 struct ofp12_packet_queue
*opq12
;
2493 opq12
= ofpbuf_put_zeros(reply
, sizeof *opq12
);
2494 opq12
->port
= ofputil_port_to_ofp11(qc
->port
);
2495 opq12
->queue_id
= htonl(qc
->queue
);
2496 len_ofs
= (char *) &opq12
->len
- (char *) reply
->data
;
2499 put_ofp10_queue_rate(reply
, OFPQT10_MIN_RATE
, qc
->min_rate
);
2500 put_ofp10_queue_rate(reply
, OFPQT11_MAX_RATE
, qc
->max_rate
);
2502 struct ofp14_queue_desc
*oqd
= ofpbuf_put_zeros(reply
, sizeof *oqd
);
2503 oqd
->port_no
= ofputil_port_to_ofp11(qc
->port
);
2504 oqd
->queue_id
= htonl(qc
->queue
);
2505 len_ofs
= (char *) &oqd
->len
- (char *) reply
->data
;
2506 put_ofp14_queue_rate(reply
, OFPQDPT14_MIN_RATE
, qc
->min_rate
);
2507 put_ofp14_queue_rate(reply
, OFPQDPT14_MAX_RATE
, qc
->max_rate
);
2510 len
= ofpbuf_at(reply
, len_ofs
, sizeof *len
);
2511 *len
= htons(reply
->size
- start_ofs
);
2513 if (ofp_version
>= OFP14_VERSION
) {
2514 ofpmp_postappend(replies
, start_ofs
);
2519 parse_ofp10_queue_rate(const struct ofp10_queue_prop_header
*hdr
,
2522 const struct ofp10_queue_prop_rate
*oqpr
;
2524 if (hdr
->len
== htons(sizeof *oqpr
)) {
2525 oqpr
= (const struct ofp10_queue_prop_rate
*) hdr
;
2526 *rate
= ntohs(oqpr
->rate
);
2529 return OFPERR_OFPBRC_BAD_LEN
;
2534 ofputil_pull_queue_get_config_reply10(struct ofpbuf
*msg
,
2535 struct ofputil_queue_config
*queue
)
2537 const struct ofp_header
*oh
= msg
->header
;
2538 unsigned int opq_len
; /* Length of protocol-specific queue header. */
2539 unsigned int len
; /* Total length of queue + properties. */
2541 /* Obtain the port number from the message header. */
2542 if (oh
->version
== OFP10_VERSION
) {
2543 const struct ofp10_queue_get_config_reply
*oqgcr10
= msg
->msg
;
2544 queue
->port
= u16_to_ofp(ntohs(oqgcr10
->port
));
2546 const struct ofp11_queue_get_config_reply
*oqgcr11
= msg
->msg
;
2547 enum ofperr error
= ofputil_port_from_ofp11(oqgcr11
->port
,
2554 /* Pull off the queue header and get the queue number and length. */
2555 if (oh
->version
< OFP12_VERSION
) {
2556 const struct ofp10_packet_queue
*opq10
;
2557 opq10
= ofpbuf_try_pull(msg
, sizeof *opq10
);
2559 return OFPERR_OFPBRC_BAD_LEN
;
2561 queue
->queue
= ntohl(opq10
->queue_id
);
2562 len
= ntohs(opq10
->len
);
2563 opq_len
= sizeof *opq10
;
2565 const struct ofp12_packet_queue
*opq12
;
2566 opq12
= ofpbuf_try_pull(msg
, sizeof *opq12
);
2568 return OFPERR_OFPBRC_BAD_LEN
;
2570 queue
->queue
= ntohl(opq12
->queue_id
);
2571 len
= ntohs(opq12
->len
);
2572 opq_len
= sizeof *opq12
;
2576 if (len
< opq_len
|| len
> msg
->size
+ opq_len
|| len
% 8) {
2577 return OFPERR_OFPBRC_BAD_LEN
;
2581 /* Pull properties. The format of these properties differs from used in
2582 * OF1.4+ so we can't use the common property functions. */
2584 const struct ofp10_queue_prop_header
*hdr
;
2585 unsigned int property
;
2586 unsigned int prop_len
;
2587 enum ofperr error
= 0;
2589 hdr
= ofpbuf_at_assert(msg
, 0, sizeof *hdr
);
2590 prop_len
= ntohs(hdr
->len
);
2591 if (prop_len
< sizeof *hdr
|| prop_len
> msg
->size
|| prop_len
% 8) {
2592 return OFPERR_OFPBRC_BAD_LEN
;
2595 property
= ntohs(hdr
->property
);
2597 case OFPQT10_MIN_RATE
:
2598 error
= parse_ofp10_queue_rate(hdr
, &queue
->min_rate
);
2601 case OFPQT11_MAX_RATE
:
2602 error
= parse_ofp10_queue_rate(hdr
, &queue
->max_rate
);
2606 VLOG_INFO_RL(&bad_ofmsg_rl
, "unknown queue property %u", property
);
2613 ofpbuf_pull(msg
, prop_len
);
2620 ofputil_pull_queue_get_config_reply14(struct ofpbuf
*msg
,
2621 struct ofputil_queue_config
*queue
)
2623 struct ofp14_queue_desc
*oqd14
= ofpbuf_try_pull(msg
, sizeof *oqd14
);
2625 return OFPERR_OFPBRC_BAD_LEN
;
2627 enum ofperr error
= ofputil_port_from_ofp11(oqd14
->port_no
, &queue
->port
);
2631 queue
->queue
= ntohl(oqd14
->queue_id
);
2634 unsigned int len
= ntohs(oqd14
->len
);
2635 if (len
< sizeof *oqd14
|| len
> msg
->size
+ sizeof *oqd14
|| len
% 8) {
2636 return OFPERR_OFPBRC_BAD_LEN
;
2638 len
-= sizeof *oqd14
;
2640 struct ofpbuf properties
= ofpbuf_const_initializer(ofpbuf_pull(msg
, len
),
2642 while (properties
.size
> 0) {
2643 struct ofpbuf payload
;
2646 error
= ofpprop_pull(&properties
, &payload
, &type
);
2652 case OFPQDPT14_MIN_RATE
:
2653 error
= ofpprop_parse_u16(&payload
, &queue
->min_rate
);
2656 case OFPQDPT14_MAX_RATE
:
2657 error
= ofpprop_parse_u16(&payload
, &queue
->max_rate
);
2661 error
= OFPPROP_UNKNOWN(true, "queue desc", type
);
2673 /* Decodes information about a queue from the OFPT_QUEUE_GET_CONFIG_REPLY in
2674 * 'reply' and stores it in '*queue'. ofputil_decode_queue_get_config_reply()
2675 * must already have pulled off the main header.
2677 * This function returns EOF if the last queue has already been decoded, 0 if a
2678 * queue was successfully decoded into '*queue', or an ofperr if there was a
2679 * problem decoding 'reply'. */
2681 ofputil_pull_queue_get_config_reply(struct ofpbuf
*msg
,
2682 struct ofputil_queue_config
*queue
)
2686 /* Pull OpenFlow header. */
2687 raw
= ofpraw_pull_assert(msg
);
2689 /* Pull protocol-specific ofp_queue_get_config_reply header (OF1.4
2690 * doesn't have one at all). */
2691 if (raw
== OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY
) {
2692 ofpbuf_pull(msg
, sizeof(struct ofp10_queue_get_config_reply
));
2693 } else if (raw
== OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY
) {
2694 ofpbuf_pull(msg
, sizeof(struct ofp11_queue_get_config_reply
));
2696 ovs_assert(raw
== OFPRAW_OFPST14_QUEUE_DESC_REPLY
);
2699 raw
= ofpraw_decode_assert(msg
->header
);
2702 queue
->min_rate
= UINT16_MAX
;
2703 queue
->max_rate
= UINT16_MAX
;
2707 } else if (raw
== OFPRAW_OFPST14_QUEUE_DESC_REPLY
) {
2708 return ofputil_pull_queue_get_config_reply14(msg
, queue
);
2710 return ofputil_pull_queue_get_config_reply10(msg
, queue
);
2714 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
2715 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
2716 * successful, otherwise an OpenFlow error code. */
2718 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request
*fsr
,
2719 const struct ofp_header
*oh
,
2720 const struct tun_table
*tun_table
)
2722 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
2723 enum ofpraw raw
= ofpraw_pull_assert(&b
);
2724 switch ((int) raw
) {
2725 case OFPRAW_OFPST10_FLOW_REQUEST
:
2726 return ofputil_decode_ofpst10_flow_request(fsr
, b
.data
, false);
2728 case OFPRAW_OFPST10_AGGREGATE_REQUEST
:
2729 return ofputil_decode_ofpst10_flow_request(fsr
, b
.data
, true);
2731 case OFPRAW_OFPST11_FLOW_REQUEST
:
2732 return ofputil_decode_ofpst11_flow_request(fsr
, &b
, false, tun_table
);
2734 case OFPRAW_OFPST11_AGGREGATE_REQUEST
:
2735 return ofputil_decode_ofpst11_flow_request(fsr
, &b
, true, tun_table
);
2737 case OFPRAW_NXST_FLOW_REQUEST
:
2738 return ofputil_decode_nxst_flow_request(fsr
, &b
, false, tun_table
);
2740 case OFPRAW_NXST_AGGREGATE_REQUEST
:
2741 return ofputil_decode_nxst_flow_request(fsr
, &b
, true, tun_table
);
2744 /* Hey, the caller lied. */
2749 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
2750 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
2751 * 'protocol', and returns the message. */
2753 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request
*fsr
,
2754 enum ofputil_protocol protocol
)
2760 case OFPUTIL_P_OF11_STD
:
2761 case OFPUTIL_P_OF12_OXM
:
2762 case OFPUTIL_P_OF13_OXM
:
2763 case OFPUTIL_P_OF14_OXM
:
2764 case OFPUTIL_P_OF15_OXM
:
2765 case OFPUTIL_P_OF16_OXM
: {
2766 struct ofp11_flow_stats_request
*ofsr
;
2768 raw
= (fsr
->aggregate
2769 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
2770 : OFPRAW_OFPST11_FLOW_REQUEST
);
2771 msg
= ofpraw_alloc(raw
, ofputil_protocol_to_ofp_version(protocol
),
2772 ofputil_match_typical_len(protocol
));
2773 ofsr
= ofpbuf_put_zeros(msg
, sizeof *ofsr
);
2774 ofsr
->table_id
= fsr
->table_id
;
2775 ofsr
->out_port
= ofputil_port_to_ofp11(fsr
->out_port
);
2776 ofsr
->out_group
= htonl(fsr
->out_group
);
2777 ofsr
->cookie
= fsr
->cookie
;
2778 ofsr
->cookie_mask
= fsr
->cookie_mask
;
2779 ofputil_put_ofp11_match(msg
, &fsr
->match
, protocol
);
2783 case OFPUTIL_P_OF10_STD
:
2784 case OFPUTIL_P_OF10_STD_TID
: {
2785 struct ofp10_flow_stats_request
*ofsr
;
2787 raw
= (fsr
->aggregate
2788 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
2789 : OFPRAW_OFPST10_FLOW_REQUEST
);
2790 msg
= ofpraw_alloc(raw
, OFP10_VERSION
, 0);
2791 ofsr
= ofpbuf_put_zeros(msg
, sizeof *ofsr
);
2792 ofputil_match_to_ofp10_match(&fsr
->match
, &ofsr
->match
);
2793 ofsr
->table_id
= fsr
->table_id
;
2794 ofsr
->out_port
= htons(ofp_to_u16(fsr
->out_port
));
2798 case OFPUTIL_P_OF10_NXM
:
2799 case OFPUTIL_P_OF10_NXM_TID
: {
2800 struct nx_flow_stats_request
*nfsr
;
2803 raw
= (fsr
->aggregate
2804 ? OFPRAW_NXST_AGGREGATE_REQUEST
2805 : OFPRAW_NXST_FLOW_REQUEST
);
2806 msg
= ofpraw_alloc(raw
, OFP10_VERSION
, NXM_TYPICAL_LEN
);
2807 ofpbuf_put_zeros(msg
, sizeof *nfsr
);
2808 match_len
= nx_put_match(msg
, &fsr
->match
,
2809 fsr
->cookie
, fsr
->cookie_mask
);
2812 nfsr
->out_port
= htons(ofp_to_u16(fsr
->out_port
));
2813 nfsr
->match_len
= htons(match_len
);
2814 nfsr
->table_id
= fsr
->table_id
;
2825 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
2826 * ofputil_flow_stats in 'fs'.
2828 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
2829 * OpenFlow message. Calling this function multiple times for a single 'msg'
2830 * iterates through the replies. The caller must initially leave 'msg''s layer
2831 * pointers null and not modify them between calls.
2833 * Most switches don't send the values needed to populate fs->idle_age and
2834 * fs->hard_age, so those members will usually be set to 0. If the switch from
2835 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
2836 * 'flow_age_extension' as true so that the contents of 'msg' determine the
2837 * 'idle_age' and 'hard_age' members in 'fs'.
2839 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
2840 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
2841 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
2843 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2844 * otherwise a positive errno value. */
2846 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats
*fs
,
2848 bool flow_age_extension
,
2849 struct ofpbuf
*ofpacts
)
2851 const struct ofp_header
*oh
;
2852 size_t instructions_len
;
2856 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
2857 : ofpraw_pull(&raw
, msg
));
2865 } else if (raw
== OFPRAW_OFPST11_FLOW_REPLY
2866 || raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
2867 const struct ofp11_flow_stats
*ofs
;
2869 uint16_t padded_match_len
;
2871 ofs
= ofpbuf_try_pull(msg
, sizeof *ofs
);
2873 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply has %"PRIu32
" leftover "
2874 "bytes at end", msg
->size
);
2878 length
= ntohs(ofs
->length
);
2879 if (length
< sizeof *ofs
) {
2880 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply claims invalid "
2881 "length %"PRIuSIZE
, length
);
2885 if (ofputil_pull_ofp11_match(msg
, NULL
, &fs
->match
,
2886 &padded_match_len
)) {
2887 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply bad match");
2890 instructions_len
= length
- sizeof *ofs
- padded_match_len
;
2892 fs
->priority
= ntohs(ofs
->priority
);
2893 fs
->table_id
= ofs
->table_id
;
2894 fs
->duration_sec
= ntohl(ofs
->duration_sec
);
2895 fs
->duration_nsec
= ntohl(ofs
->duration_nsec
);
2896 fs
->idle_timeout
= ntohs(ofs
->idle_timeout
);
2897 fs
->hard_timeout
= ntohs(ofs
->hard_timeout
);
2898 if (oh
->version
>= OFP14_VERSION
) {
2899 fs
->importance
= ntohs(ofs
->importance
);
2903 if (raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
2904 error
= ofputil_decode_flow_mod_flags(ofs
->flags
, -1, oh
->version
,
2914 fs
->cookie
= ofs
->cookie
;
2915 fs
->packet_count
= ntohll(ofs
->packet_count
);
2916 fs
->byte_count
= ntohll(ofs
->byte_count
);
2917 } else if (raw
== OFPRAW_OFPST10_FLOW_REPLY
) {
2918 const struct ofp10_flow_stats
*ofs
;
2921 ofs
= ofpbuf_try_pull(msg
, sizeof *ofs
);
2923 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply has %"PRIu32
" leftover "
2924 "bytes at end", msg
->size
);
2928 length
= ntohs(ofs
->length
);
2929 if (length
< sizeof *ofs
) {
2930 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply claims invalid "
2931 "length %"PRIuSIZE
, length
);
2934 instructions_len
= length
- sizeof *ofs
;
2936 fs
->cookie
= get_32aligned_be64(&ofs
->cookie
);
2937 ofputil_match_from_ofp10_match(&ofs
->match
, &fs
->match
);
2938 fs
->priority
= ntohs(ofs
->priority
);
2939 fs
->table_id
= ofs
->table_id
;
2940 fs
->duration_sec
= ntohl(ofs
->duration_sec
);
2941 fs
->duration_nsec
= ntohl(ofs
->duration_nsec
);
2942 fs
->idle_timeout
= ntohs(ofs
->idle_timeout
);
2943 fs
->hard_timeout
= ntohs(ofs
->hard_timeout
);
2947 fs
->packet_count
= ntohll(get_32aligned_be64(&ofs
->packet_count
));
2948 fs
->byte_count
= ntohll(get_32aligned_be64(&ofs
->byte_count
));
2950 } else if (raw
== OFPRAW_NXST_FLOW_REPLY
) {
2951 const struct nx_flow_stats
*nfs
;
2952 size_t match_len
, length
;
2954 nfs
= ofpbuf_try_pull(msg
, sizeof *nfs
);
2956 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW reply has %"PRIu32
" leftover "
2957 "bytes at end", msg
->size
);
2961 length
= ntohs(nfs
->length
);
2962 match_len
= ntohs(nfs
->match_len
);
2963 if (length
< sizeof *nfs
+ ROUND_UP(match_len
, 8)) {
2964 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW reply with match_len=%"PRIuSIZE
" "
2965 "claims invalid length %"PRIuSIZE
, match_len
, length
);
2968 if (nx_pull_match(msg
, match_len
, &fs
->match
, NULL
, NULL
, NULL
)) {
2971 instructions_len
= length
- sizeof *nfs
- ROUND_UP(match_len
, 8);
2973 fs
->cookie
= nfs
->cookie
;
2974 fs
->table_id
= nfs
->table_id
;
2975 fs
->duration_sec
= ntohl(nfs
->duration_sec
);
2976 fs
->duration_nsec
= ntohl(nfs
->duration_nsec
);
2977 fs
->priority
= ntohs(nfs
->priority
);
2978 fs
->idle_timeout
= ntohs(nfs
->idle_timeout
);
2979 fs
->hard_timeout
= ntohs(nfs
->hard_timeout
);
2983 if (flow_age_extension
) {
2984 if (nfs
->idle_age
) {
2985 fs
->idle_age
= ntohs(nfs
->idle_age
) - 1;
2987 if (nfs
->hard_age
) {
2988 fs
->hard_age
= ntohs(nfs
->hard_age
) - 1;
2991 fs
->packet_count
= ntohll(nfs
->packet_count
);
2992 fs
->byte_count
= ntohll(nfs
->byte_count
);
2998 if (ofpacts_pull_openflow_instructions(msg
, instructions_len
, oh
->version
,
3000 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply bad instructions");
3003 fs
->ofpacts
= ofpacts
->data
;
3004 fs
->ofpacts_len
= ofpacts
->size
;
3009 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
3011 * We use this in situations where OVS internally uses UINT64_MAX to mean
3012 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
3014 unknown_to_zero(uint64_t count
)
3016 return count
!= UINT64_MAX
? count
: 0;
3019 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
3020 * those already present in the list of ofpbufs in 'replies'. 'replies' should
3021 * have been initialized with ofpmp_init(). */
3023 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats
*fs
,
3024 struct ovs_list
*replies
,
3025 const struct tun_table
*tun_table
)
3027 struct ofputil_flow_stats
*fs_
= CONST_CAST(struct ofputil_flow_stats
*,
3029 const struct tun_table
*orig_tun_table
;
3030 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
3031 size_t start_ofs
= reply
->size
;
3032 enum ofp_version version
= ofpmp_version(replies
);
3033 enum ofpraw raw
= ofpmp_decode_raw(replies
);
3035 orig_tun_table
= fs
->match
.flow
.tunnel
.metadata
.tab
;
3036 fs_
->match
.flow
.tunnel
.metadata
.tab
= tun_table
;
3038 if (raw
== OFPRAW_OFPST11_FLOW_REPLY
|| raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
3039 struct ofp11_flow_stats
*ofs
;
3041 ofpbuf_put_uninit(reply
, sizeof *ofs
);
3042 oxm_put_match(reply
, &fs
->match
, version
);
3043 ofpacts_put_openflow_instructions(fs
->ofpacts
, fs
->ofpacts_len
, reply
,
3046 ofs
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *ofs
);
3047 ofs
->length
= htons(reply
->size
- start_ofs
);
3048 ofs
->table_id
= fs
->table_id
;
3050 ofs
->duration_sec
= htonl(fs
->duration_sec
);
3051 ofs
->duration_nsec
= htonl(fs
->duration_nsec
);
3052 ofs
->priority
= htons(fs
->priority
);
3053 ofs
->idle_timeout
= htons(fs
->idle_timeout
);
3054 ofs
->hard_timeout
= htons(fs
->hard_timeout
);
3055 if (version
>= OFP14_VERSION
) {
3056 ofs
->importance
= htons(fs
->importance
);
3058 ofs
->importance
= 0;
3060 if (raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
3061 ofs
->flags
= ofputil_encode_flow_mod_flags(fs
->flags
, version
);
3065 memset(ofs
->pad2
, 0, sizeof ofs
->pad2
);
3066 ofs
->cookie
= fs
->cookie
;
3067 ofs
->packet_count
= htonll(unknown_to_zero(fs
->packet_count
));
3068 ofs
->byte_count
= htonll(unknown_to_zero(fs
->byte_count
));
3069 } else if (raw
== OFPRAW_OFPST10_FLOW_REPLY
) {
3070 struct ofp10_flow_stats
*ofs
;
3072 ofpbuf_put_uninit(reply
, sizeof *ofs
);
3073 ofpacts_put_openflow_actions(fs
->ofpacts
, fs
->ofpacts_len
, reply
,
3075 ofs
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *ofs
);
3076 ofs
->length
= htons(reply
->size
- start_ofs
);
3077 ofs
->table_id
= fs
->table_id
;
3079 ofputil_match_to_ofp10_match(&fs
->match
, &ofs
->match
);
3080 ofs
->duration_sec
= htonl(fs
->duration_sec
);
3081 ofs
->duration_nsec
= htonl(fs
->duration_nsec
);
3082 ofs
->priority
= htons(fs
->priority
);
3083 ofs
->idle_timeout
= htons(fs
->idle_timeout
);
3084 ofs
->hard_timeout
= htons(fs
->hard_timeout
);
3085 memset(ofs
->pad2
, 0, sizeof ofs
->pad2
);
3086 put_32aligned_be64(&ofs
->cookie
, fs
->cookie
);
3087 put_32aligned_be64(&ofs
->packet_count
,
3088 htonll(unknown_to_zero(fs
->packet_count
)));
3089 put_32aligned_be64(&ofs
->byte_count
,
3090 htonll(unknown_to_zero(fs
->byte_count
)));
3091 } else if (raw
== OFPRAW_NXST_FLOW_REPLY
) {
3092 struct nx_flow_stats
*nfs
;
3095 ofpbuf_put_uninit(reply
, sizeof *nfs
);
3096 match_len
= nx_put_match(reply
, &fs
->match
, 0, 0);
3097 ofpacts_put_openflow_actions(fs
->ofpacts
, fs
->ofpacts_len
, reply
,
3099 nfs
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *nfs
);
3100 nfs
->length
= htons(reply
->size
- start_ofs
);
3101 nfs
->table_id
= fs
->table_id
;
3103 nfs
->duration_sec
= htonl(fs
->duration_sec
);
3104 nfs
->duration_nsec
= htonl(fs
->duration_nsec
);
3105 nfs
->priority
= htons(fs
->priority
);
3106 nfs
->idle_timeout
= htons(fs
->idle_timeout
);
3107 nfs
->hard_timeout
= htons(fs
->hard_timeout
);
3108 nfs
->idle_age
= htons(fs
->idle_age
< 0 ? 0
3109 : fs
->idle_age
< UINT16_MAX
? fs
->idle_age
+ 1
3111 nfs
->hard_age
= htons(fs
->hard_age
< 0 ? 0
3112 : fs
->hard_age
< UINT16_MAX
? fs
->hard_age
+ 1
3114 nfs
->match_len
= htons(match_len
);
3115 nfs
->cookie
= fs
->cookie
;
3116 nfs
->packet_count
= htonll(fs
->packet_count
);
3117 nfs
->byte_count
= htonll(fs
->byte_count
);
3122 ofpmp_postappend(replies
, start_ofs
);
3123 fs_
->match
.flow
.tunnel
.metadata
.tab
= orig_tun_table
;
3126 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
3127 * NXST_AGGREGATE reply matching 'request', and returns the message. */
3129 ofputil_encode_aggregate_stats_reply(
3130 const struct ofputil_aggregate_stats
*stats
,
3131 const struct ofp_header
*request
)
3133 struct ofp_aggregate_stats_reply
*asr
;
3134 uint64_t packet_count
;
3135 uint64_t byte_count
;
3139 ofpraw_decode(&raw
, request
);
3140 if (raw
== OFPRAW_OFPST10_AGGREGATE_REQUEST
) {
3141 packet_count
= unknown_to_zero(stats
->packet_count
);
3142 byte_count
= unknown_to_zero(stats
->byte_count
);
3144 packet_count
= stats
->packet_count
;
3145 byte_count
= stats
->byte_count
;
3148 msg
= ofpraw_alloc_stats_reply(request
, 0);
3149 asr
= ofpbuf_put_zeros(msg
, sizeof *asr
);
3150 put_32aligned_be64(&asr
->packet_count
, htonll(packet_count
));
3151 put_32aligned_be64(&asr
->byte_count
, htonll(byte_count
));
3152 asr
->flow_count
= htonl(stats
->flow_count
);
3158 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats
*stats
,
3159 const struct ofp_header
*reply
)
3161 struct ofpbuf msg
= ofpbuf_const_initializer(reply
, ntohs(reply
->length
));
3162 ofpraw_pull_assert(&msg
);
3164 struct ofp_aggregate_stats_reply
*asr
= msg
.msg
;
3165 stats
->packet_count
= ntohll(get_32aligned_be64(&asr
->packet_count
));
3166 stats
->byte_count
= ntohll(get_32aligned_be64(&asr
->byte_count
));
3167 stats
->flow_count
= ntohl(asr
->flow_count
);
3172 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
3173 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
3174 * an OpenFlow error code. */
3176 ofputil_decode_flow_removed(struct ofputil_flow_removed
*fr
,
3177 const struct ofp_header
*oh
)
3179 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
3180 enum ofpraw raw
= ofpraw_pull_assert(&b
);
3181 if (raw
== OFPRAW_OFPT11_FLOW_REMOVED
) {
3182 const struct ofp12_flow_removed
*ofr
;
3185 ofr
= ofpbuf_pull(&b
, sizeof *ofr
);
3187 error
= ofputil_pull_ofp11_match(&b
, NULL
, &fr
->match
, NULL
);
3192 fr
->priority
= ntohs(ofr
->priority
);
3193 fr
->cookie
= ofr
->cookie
;
3194 fr
->reason
= ofr
->reason
;
3195 fr
->table_id
= ofr
->table_id
;
3196 fr
->duration_sec
= ntohl(ofr
->duration_sec
);
3197 fr
->duration_nsec
= ntohl(ofr
->duration_nsec
);
3198 fr
->idle_timeout
= ntohs(ofr
->idle_timeout
);
3199 fr
->hard_timeout
= ntohs(ofr
->hard_timeout
);
3200 fr
->packet_count
= ntohll(ofr
->packet_count
);
3201 fr
->byte_count
= ntohll(ofr
->byte_count
);
3202 } else if (raw
== OFPRAW_OFPT10_FLOW_REMOVED
) {
3203 const struct ofp10_flow_removed
*ofr
;
3205 ofr
= ofpbuf_pull(&b
, sizeof *ofr
);
3207 ofputil_match_from_ofp10_match(&ofr
->match
, &fr
->match
);
3208 fr
->priority
= ntohs(ofr
->priority
);
3209 fr
->cookie
= ofr
->cookie
;
3210 fr
->reason
= ofr
->reason
;
3212 fr
->duration_sec
= ntohl(ofr
->duration_sec
);
3213 fr
->duration_nsec
= ntohl(ofr
->duration_nsec
);
3214 fr
->idle_timeout
= ntohs(ofr
->idle_timeout
);
3215 fr
->hard_timeout
= 0;
3216 fr
->packet_count
= ntohll(ofr
->packet_count
);
3217 fr
->byte_count
= ntohll(ofr
->byte_count
);
3218 } else if (raw
== OFPRAW_NXT_FLOW_REMOVED
) {
3219 struct nx_flow_removed
*nfr
;
3222 nfr
= ofpbuf_pull(&b
, sizeof *nfr
);
3223 error
= nx_pull_match(&b
, ntohs(nfr
->match_len
), &fr
->match
, NULL
,
3229 return OFPERR_OFPBRC_BAD_LEN
;
3232 fr
->priority
= ntohs(nfr
->priority
);
3233 fr
->cookie
= nfr
->cookie
;
3234 fr
->reason
= nfr
->reason
;
3235 fr
->table_id
= nfr
->table_id
? nfr
->table_id
- 1 : 255;
3236 fr
->duration_sec
= ntohl(nfr
->duration_sec
);
3237 fr
->duration_nsec
= ntohl(nfr
->duration_nsec
);
3238 fr
->idle_timeout
= ntohs(nfr
->idle_timeout
);
3239 fr
->hard_timeout
= 0;
3240 fr
->packet_count
= ntohll(nfr
->packet_count
);
3241 fr
->byte_count
= ntohll(nfr
->byte_count
);
3249 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
3250 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
3253 ofputil_encode_flow_removed(const struct ofputil_flow_removed
*fr
,
3254 enum ofputil_protocol protocol
)
3257 enum ofp_flow_removed_reason reason
= fr
->reason
;
3259 if (reason
== OFPRR_METER_DELETE
&& !(protocol
& OFPUTIL_P_OF14_UP
)) {
3260 reason
= OFPRR_DELETE
;
3264 case OFPUTIL_P_OF11_STD
:
3265 case OFPUTIL_P_OF12_OXM
:
3266 case OFPUTIL_P_OF13_OXM
:
3267 case OFPUTIL_P_OF14_OXM
:
3268 case OFPUTIL_P_OF15_OXM
:
3269 case OFPUTIL_P_OF16_OXM
: {
3270 struct ofp12_flow_removed
*ofr
;
3272 msg
= ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED
,
3273 ofputil_protocol_to_ofp_version(protocol
),
3275 ofputil_match_typical_len(protocol
));
3276 ofr
= ofpbuf_put_zeros(msg
, sizeof *ofr
);
3277 ofr
->cookie
= fr
->cookie
;
3278 ofr
->priority
= htons(fr
->priority
);
3279 ofr
->reason
= reason
;
3280 ofr
->table_id
= fr
->table_id
;
3281 ofr
->duration_sec
= htonl(fr
->duration_sec
);
3282 ofr
->duration_nsec
= htonl(fr
->duration_nsec
);
3283 ofr
->idle_timeout
= htons(fr
->idle_timeout
);
3284 ofr
->hard_timeout
= htons(fr
->hard_timeout
);
3285 ofr
->packet_count
= htonll(fr
->packet_count
);
3286 ofr
->byte_count
= htonll(fr
->byte_count
);
3287 ofputil_put_ofp11_match(msg
, &fr
->match
, protocol
);
3291 case OFPUTIL_P_OF10_STD
:
3292 case OFPUTIL_P_OF10_STD_TID
: {
3293 struct ofp10_flow_removed
*ofr
;
3295 msg
= ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED
, OFP10_VERSION
,
3297 ofr
= ofpbuf_put_zeros(msg
, sizeof *ofr
);
3298 ofputil_match_to_ofp10_match(&fr
->match
, &ofr
->match
);
3299 ofr
->cookie
= fr
->cookie
;
3300 ofr
->priority
= htons(fr
->priority
);
3301 ofr
->reason
= reason
;
3302 ofr
->duration_sec
= htonl(fr
->duration_sec
);
3303 ofr
->duration_nsec
= htonl(fr
->duration_nsec
);
3304 ofr
->idle_timeout
= htons(fr
->idle_timeout
);
3305 ofr
->packet_count
= htonll(unknown_to_zero(fr
->packet_count
));
3306 ofr
->byte_count
= htonll(unknown_to_zero(fr
->byte_count
));
3310 case OFPUTIL_P_OF10_NXM
:
3311 case OFPUTIL_P_OF10_NXM_TID
: {
3312 struct nx_flow_removed
*nfr
;
3315 msg
= ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED
, OFP10_VERSION
,
3316 htonl(0), NXM_TYPICAL_LEN
);
3317 ofpbuf_put_zeros(msg
, sizeof *nfr
);
3318 match_len
= nx_put_match(msg
, &fr
->match
, 0, 0);
3321 nfr
->cookie
= fr
->cookie
;
3322 nfr
->priority
= htons(fr
->priority
);
3323 nfr
->reason
= reason
;
3324 nfr
->table_id
= fr
->table_id
+ 1;
3325 nfr
->duration_sec
= htonl(fr
->duration_sec
);
3326 nfr
->duration_nsec
= htonl(fr
->duration_nsec
);
3327 nfr
->idle_timeout
= htons(fr
->idle_timeout
);
3328 nfr
->match_len
= htons(match_len
);
3329 nfr
->packet_count
= htonll(fr
->packet_count
);
3330 nfr
->byte_count
= htonll(fr
->byte_count
);
3341 /* The caller has done basic initialization of '*pin'; the other output
3342 * arguments needs to be initialized. */
3344 decode_nx_packet_in2(const struct ofp_header
*oh
, bool loose
,
3345 const struct tun_table
*tun_table
,
3346 struct ofputil_packet_in
*pin
,
3347 size_t *total_len
, uint32_t *buffer_id
,
3348 struct ofpbuf
*continuation
)
3351 *buffer_id
= UINT32_MAX
;
3353 struct ofpbuf properties
;
3354 ofpbuf_use_const(&properties
, oh
, ntohs(oh
->length
));
3355 ofpraw_pull_assert(&properties
);
3357 while (properties
.size
> 0) {
3358 struct ofpbuf payload
;
3361 enum ofperr error
= ofpprop_pull(&properties
, &payload
, &type
);
3368 pin
->packet
= payload
.msg
;
3369 pin
->packet_len
= ofpbuf_msgsize(&payload
);
3372 case NXPINT_FULL_LEN
: {
3374 error
= ofpprop_parse_u32(&payload
, &u32
);
3379 case NXPINT_BUFFER_ID
:
3380 error
= ofpprop_parse_u32(&payload
, buffer_id
);
3383 case NXPINT_TABLE_ID
:
3384 error
= ofpprop_parse_u8(&payload
, &pin
->table_id
);
3388 error
= ofpprop_parse_be64(&payload
, &pin
->cookie
);
3391 case NXPINT_REASON
: {
3393 error
= ofpprop_parse_u8(&payload
, &reason
);
3394 pin
->reason
= reason
;
3398 case NXPINT_METADATA
:
3399 error
= oxm_decode_match(payload
.msg
, ofpbuf_msgsize(&payload
),
3400 tun_table
, &pin
->flow_metadata
);
3403 case NXPINT_USERDATA
:
3404 pin
->userdata
= payload
.msg
;
3405 pin
->userdata_len
= ofpbuf_msgsize(&payload
);
3408 case NXPINT_CONTINUATION
:
3410 error
= ofpprop_parse_nested(&payload
, continuation
);
3415 error
= OFPPROP_UNKNOWN(loose
, "NX_PACKET_IN2", type
);
3423 if (!pin
->packet_len
) {
3424 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXT_PACKET_IN2 lacks packet");
3425 return OFPERR_OFPBRC_BAD_LEN
;
3426 } else if (!*total_len
) {
3427 *total_len
= pin
->packet_len
;
3428 } else if (*total_len
< pin
->packet_len
) {
3429 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXT_PACKET_IN2 claimed full_len < len");
3430 return OFPERR_OFPBRC_BAD_LEN
;
3436 /* Decodes the packet-in message starting at 'oh' into '*pin'. Populates
3437 * 'pin->packet' and 'pin->packet_len' with the part of the packet actually
3438 * included in the message. If 'total_lenp' is nonnull, populates
3439 * '*total_lenp' with the original length of the packet (which is larger than
3440 * 'packet->len' if only part of the packet was included). If 'buffer_idp' is
3441 * nonnull, stores the packet's buffer ID in '*buffer_idp' (UINT32_MAX if it
3442 * was not buffered).
3444 * Populates 'continuation', if nonnull, with the continuation data from the
3445 * packet-in (an empty buffer, if 'oh' did not contain continuation data). The
3446 * format of this data is supposed to be opaque to anything other than
3447 * ovs-vswitchd, so that in any other process the only reasonable use of this
3448 * data is to be copied into an NXT_RESUME message via ofputil_encode_resume().
3450 * This function points 'pin->packet' into 'oh', so the caller should not free
3451 * it separately from the original OpenFlow message. This is also true for
3452 * 'pin->userdata' (which could also end up NULL if there is no userdata).
3454 * Returns 0 if successful, otherwise an OpenFlow error code. */
3456 ofputil_decode_packet_in(const struct ofp_header
*oh
, bool loose
,
3457 const struct tun_table
*tun_table
,
3458 struct ofputil_packet_in
*pin
,
3459 size_t *total_lenp
, uint32_t *buffer_idp
,
3460 struct ofpbuf
*continuation
)
3465 memset(pin
, 0, sizeof *pin
);
3466 pin
->cookie
= OVS_BE64_MAX
;
3468 ofpbuf_use_const(continuation
, NULL
, 0);
3471 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
3472 enum ofpraw raw
= ofpraw_pull_assert(&b
);
3473 if (raw
== OFPRAW_OFPT13_PACKET_IN
|| raw
== OFPRAW_OFPT12_PACKET_IN
) {
3474 const struct ofp12_packet_in
*opi
= ofpbuf_pull(&b
, sizeof *opi
);
3475 const ovs_be64
*cookie
= (raw
== OFPRAW_OFPT13_PACKET_IN
3476 ? ofpbuf_pull(&b
, sizeof *cookie
)
3478 enum ofperr error
= oxm_pull_match_loose(&b
, tun_table
,
3479 &pin
->flow_metadata
);
3484 if (!ofpbuf_try_pull(&b
, 2)) {
3485 return OFPERR_OFPBRC_BAD_LEN
;
3488 pin
->reason
= opi
->reason
;
3489 pin
->table_id
= opi
->table_id
;
3490 buffer_id
= ntohl(opi
->buffer_id
);
3491 total_len
= ntohs(opi
->total_len
);
3493 pin
->cookie
= *cookie
;
3496 pin
->packet
= b
.data
;
3497 pin
->packet_len
= b
.size
;
3498 } else if (raw
== OFPRAW_OFPT10_PACKET_IN
) {
3499 const struct ofp10_packet_in
*opi
;
3501 opi
= ofpbuf_pull(&b
, offsetof(struct ofp10_packet_in
, data
));
3503 pin
->packet
= CONST_CAST(uint8_t *, opi
->data
);
3504 pin
->packet_len
= b
.size
;
3506 match_init_catchall(&pin
->flow_metadata
);
3507 match_set_in_port(&pin
->flow_metadata
,
3508 u16_to_ofp(ntohs(opi
->in_port
)));
3509 pin
->reason
= opi
->reason
;
3510 buffer_id
= ntohl(opi
->buffer_id
);
3511 total_len
= ntohs(opi
->total_len
);
3512 } else if (raw
== OFPRAW_OFPT11_PACKET_IN
) {
3513 const struct ofp11_packet_in
*opi
;
3517 opi
= ofpbuf_pull(&b
, sizeof *opi
);
3519 pin
->packet
= b
.data
;
3520 pin
->packet_len
= b
.size
;
3522 buffer_id
= ntohl(opi
->buffer_id
);
3523 error
= ofputil_port_from_ofp11(opi
->in_port
, &in_port
);
3527 match_init_catchall(&pin
->flow_metadata
);
3528 match_set_in_port(&pin
->flow_metadata
, in_port
);
3529 total_len
= ntohs(opi
->total_len
);
3530 pin
->reason
= opi
->reason
;
3531 pin
->table_id
= opi
->table_id
;
3532 } else if (raw
== OFPRAW_NXT_PACKET_IN
) {
3533 const struct nx_packet_in
*npi
;
3536 npi
= ofpbuf_pull(&b
, sizeof *npi
);
3537 error
= nx_pull_match_loose(&b
, ntohs(npi
->match_len
),
3538 &pin
->flow_metadata
, NULL
, NULL
, NULL
);
3543 if (!ofpbuf_try_pull(&b
, 2)) {
3544 return OFPERR_OFPBRC_BAD_LEN
;
3547 pin
->reason
= npi
->reason
;
3548 pin
->table_id
= npi
->table_id
;
3549 pin
->cookie
= npi
->cookie
;
3551 buffer_id
= ntohl(npi
->buffer_id
);
3552 total_len
= ntohs(npi
->total_len
);
3554 pin
->packet
= b
.data
;
3555 pin
->packet_len
= b
.size
;
3556 } else if (raw
== OFPRAW_NXT_PACKET_IN2
|| raw
== OFPRAW_NXT_RESUME
) {
3557 enum ofperr error
= decode_nx_packet_in2(oh
, loose
, tun_table
, pin
,
3558 &total_len
, &buffer_id
,
3568 *total_lenp
= total_len
;
3571 *buffer_idp
= buffer_id
;
3578 encode_packet_in_reason(enum ofp_packet_in_reason reason
,
3579 enum ofp_version version
)
3584 case OFPR_INVALID_TTL
:
3587 case OFPR_ACTION_SET
:
3589 case OFPR_PACKET_OUT
:
3590 return version
< OFP14_VERSION
? OFPR_ACTION
: reason
;
3592 case OFPR_EXPLICIT_MISS
:
3593 return version
< OFP13_VERSION
? OFPR_ACTION
: OFPR_NO_MATCH
;
3595 case OFPR_IMPLICIT_MISS
:
3596 return OFPR_NO_MATCH
;
3598 case OFPR_N_REASONS
:
3604 /* Only NXT_PACKET_IN2 (not NXT_RESUME) should include NXCPT_USERDATA, so this
3605 * function omits it. The caller can add it itself if desired. */
3607 ofputil_put_packet_in(const struct ofputil_packet_in
*pin
,
3608 enum ofp_version version
, size_t include_bytes
,
3611 /* Add packet properties. */
3612 ofpprop_put(msg
, NXPINT_PACKET
, pin
->packet
, include_bytes
);
3613 if (include_bytes
!= pin
->packet_len
) {
3614 ofpprop_put_u32(msg
, NXPINT_FULL_LEN
, pin
->packet_len
);
3617 /* Add flow properties. */
3618 ofpprop_put_u8(msg
, NXPINT_TABLE_ID
, pin
->table_id
);
3619 if (pin
->cookie
!= OVS_BE64_MAX
) {
3620 ofpprop_put_be64(msg
, NXPINT_COOKIE
, pin
->cookie
);
3623 /* Add other properties. */
3624 ofpprop_put_u8(msg
, NXPINT_REASON
,
3625 encode_packet_in_reason(pin
->reason
, version
));
3627 size_t start
= ofpprop_start(msg
, NXPINT_METADATA
);
3628 oxm_put_raw(msg
, &pin
->flow_metadata
, version
);
3629 ofpprop_end(msg
, start
);
3633 put_actions_property(struct ofpbuf
*msg
, uint64_t prop_type
,
3634 enum ofp_version version
,
3635 const struct ofpact
*actions
, size_t actions_len
)
3638 size_t start
= ofpprop_start_nested(msg
, prop_type
);
3639 ofpacts_put_openflow_actions(actions
, actions_len
, msg
, version
);
3640 ofpprop_end(msg
, start
);
3644 enum nx_continuation_prop_type
{
3645 NXCPT_BRIDGE
= 0x8000,
3655 /* Only NXT_PACKET_IN2 (not NXT_RESUME) should include NXCPT_USERDATA, so this
3656 * function omits it. The caller can add it itself if desired. */
3658 ofputil_put_packet_in_private(const struct ofputil_packet_in_private
*pin
,
3659 enum ofp_version version
, size_t include_bytes
,
3662 ofputil_put_packet_in(&pin
->public, version
, include_bytes
, msg
);
3664 size_t continuation_ofs
= ofpprop_start_nested(msg
, NXPINT_CONTINUATION
);
3665 size_t inner_ofs
= msg
->size
;
3667 if (!uuid_is_zero(&pin
->bridge
)) {
3668 ofpprop_put_uuid(msg
, NXCPT_BRIDGE
, &pin
->bridge
);
3671 for (size_t i
= 0; i
< pin
->n_stack
; i
++) {
3672 const union mf_subvalue
*s
= &pin
->stack
[i
];
3674 for (ofs
= 0; ofs
< sizeof *s
; ofs
++) {
3680 ofpprop_put(msg
, NXCPT_STACK
, &s
->u8
[ofs
], sizeof *s
- ofs
);
3684 ofpprop_put_u32(msg
, NXCPT_MIRRORS
, pin
->mirrors
);
3687 if (pin
->conntracked
) {
3688 ofpprop_put_flag(msg
, NXCPT_CONNTRACKED
);
3691 if (pin
->actions_len
) {
3692 /* Divide 'pin->actions' into groups that begins with an
3693 * unroll_xlate action. For each group, emit a NXCPT_TABLE_ID and
3694 * NXCPT_COOKIE property (if either has changed; each is initially
3695 * assumed 0), then a NXCPT_ACTIONS property with the grouped
3698 * The alternative is to make OFPACT_UNROLL_XLATE public. We can
3699 * always do that later, since this is a private property. */
3700 const struct ofpact
*const end
= ofpact_end(pin
->actions
,
3702 const struct ofpact_unroll_xlate
*unroll
= NULL
;
3703 uint8_t table_id
= 0;
3704 ovs_be64 cookie
= 0;
3706 const struct ofpact
*a
;
3707 for (a
= pin
->actions
; ; a
= ofpact_next(a
)) {
3708 if (a
== end
|| a
->type
== OFPACT_UNROLL_XLATE
) {
3710 if (table_id
!= unroll
->rule_table_id
) {
3711 ofpprop_put_u8(msg
, NXCPT_TABLE_ID
,
3712 unroll
->rule_table_id
);
3713 table_id
= unroll
->rule_table_id
;
3715 if (cookie
!= unroll
->rule_cookie
) {
3716 ofpprop_put_be64(msg
, NXCPT_COOKIE
,
3717 unroll
->rule_cookie
);
3718 cookie
= unroll
->rule_cookie
;
3722 const struct ofpact
*start
3723 = unroll
? ofpact_next(&unroll
->ofpact
) : pin
->actions
;
3724 put_actions_property(msg
, NXCPT_ACTIONS
, version
,
3725 start
, (a
- start
) * sizeof *a
);
3730 unroll
= ofpact_get_UNROLL_XLATE(a
);
3735 if (pin
->action_set_len
) {
3736 size_t start
= ofpprop_start_nested(msg
, NXCPT_ACTION_SET
);
3737 ofpacts_put_openflow_actions(pin
->action_set
,
3738 pin
->action_set_len
, msg
, version
);
3739 ofpprop_end(msg
, start
);
3742 if (msg
->size
> inner_ofs
) {
3743 ofpprop_end(msg
, continuation_ofs
);
3745 msg
->size
= continuation_ofs
;
3749 static struct ofpbuf
*
3750 ofputil_encode_ofp10_packet_in(const struct ofputil_packet_in
*pin
)
3752 struct ofp10_packet_in
*opi
;
3755 msg
= ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN
, OFP10_VERSION
,
3756 htonl(0), pin
->packet_len
);
3757 opi
= ofpbuf_put_zeros(msg
, offsetof(struct ofp10_packet_in
, data
));
3758 opi
->total_len
= htons(pin
->packet_len
);
3759 opi
->in_port
= htons(ofp_to_u16(pin
->flow_metadata
.flow
.in_port
.ofp_port
));
3760 opi
->reason
= encode_packet_in_reason(pin
->reason
, OFP10_VERSION
);
3761 opi
->buffer_id
= htonl(UINT32_MAX
);
3766 static struct ofpbuf
*
3767 ofputil_encode_nx_packet_in(const struct ofputil_packet_in
*pin
,
3768 enum ofp_version version
)
3770 struct nx_packet_in
*npi
;
3774 /* The final argument is just an estimate of the space required. */
3775 msg
= ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN
, version
,
3776 htonl(0), NXM_TYPICAL_LEN
+ 2 + pin
->packet_len
);
3777 ofpbuf_put_zeros(msg
, sizeof *npi
);
3778 match_len
= nx_put_match(msg
, &pin
->flow_metadata
, 0, 0);
3779 ofpbuf_put_zeros(msg
, 2);
3782 npi
->buffer_id
= htonl(UINT32_MAX
);
3783 npi
->total_len
= htons(pin
->packet_len
);
3784 npi
->reason
= encode_packet_in_reason(pin
->reason
, version
);
3785 npi
->table_id
= pin
->table_id
;
3786 npi
->cookie
= pin
->cookie
;
3787 npi
->match_len
= htons(match_len
);
3792 static struct ofpbuf
*
3793 ofputil_encode_nx_packet_in2(const struct ofputil_packet_in_private
*pin
,
3794 enum ofp_version version
, size_t include_bytes
)
3796 /* 'extra' is just an estimate of the space required. */
3797 size_t extra
= (pin
->public.packet_len
3798 + NXM_TYPICAL_LEN
/* flow_metadata */
3801 + pin
->action_set_len
3802 + 256); /* fudge factor */
3803 struct ofpbuf
*msg
= ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN2
, version
,
3806 ofputil_put_packet_in_private(pin
, version
, include_bytes
, msg
);
3807 if (pin
->public.userdata_len
) {
3808 ofpprop_put(msg
, NXPINT_USERDATA
, pin
->public.userdata
,
3809 pin
->public.userdata_len
);
3812 ofpmsg_update_length(msg
);
3816 static struct ofpbuf
*
3817 ofputil_encode_ofp11_packet_in(const struct ofputil_packet_in
*pin
)
3819 struct ofp11_packet_in
*opi
;
3822 msg
= ofpraw_alloc_xid(OFPRAW_OFPT11_PACKET_IN
, OFP11_VERSION
,
3823 htonl(0), pin
->packet_len
);
3824 opi
= ofpbuf_put_zeros(msg
, sizeof *opi
);
3825 opi
->buffer_id
= htonl(UINT32_MAX
);
3826 opi
->in_port
= ofputil_port_to_ofp11(
3827 pin
->flow_metadata
.flow
.in_port
.ofp_port
);
3828 opi
->in_phy_port
= opi
->in_port
;
3829 opi
->total_len
= htons(pin
->packet_len
);
3830 opi
->reason
= encode_packet_in_reason(pin
->reason
, OFP11_VERSION
);
3831 opi
->table_id
= pin
->table_id
;
3836 static struct ofpbuf
*
3837 ofputil_encode_ofp12_packet_in(const struct ofputil_packet_in
*pin
,
3838 enum ofp_version version
)
3840 enum ofpraw raw
= (version
>= OFP13_VERSION
3841 ? OFPRAW_OFPT13_PACKET_IN
3842 : OFPRAW_OFPT12_PACKET_IN
);
3845 /* The final argument is just an estimate of the space required. */
3846 msg
= ofpraw_alloc_xid(raw
, version
,
3847 htonl(0), NXM_TYPICAL_LEN
+ 2 + pin
->packet_len
);
3849 struct ofp12_packet_in
*opi
= ofpbuf_put_zeros(msg
, sizeof *opi
);
3850 opi
->buffer_id
= htonl(UINT32_MAX
);
3851 opi
->total_len
= htons(pin
->packet_len
);
3852 opi
->reason
= encode_packet_in_reason(pin
->reason
, version
);
3853 opi
->table_id
= pin
->table_id
;
3855 if (version
>= OFP13_VERSION
) {
3856 ovs_be64 cookie
= pin
->cookie
;
3857 ofpbuf_put(msg
, &cookie
, sizeof cookie
);
3860 oxm_put_match(msg
, &pin
->flow_metadata
, version
);
3861 ofpbuf_put_zeros(msg
, 2);
3866 /* Converts abstract ofputil_packet_in_private 'pin' into a PACKET_IN message
3867 * for 'protocol', using the packet-in format specified by 'packet_in_format'.
3869 * This function is really meant only for use by ovs-vswitchd. To any other
3870 * code, the "continuation" data, i.e. the data that is in struct
3871 * ofputil_packet_in_private but not in struct ofputil_packet_in, is supposed
3872 * to be opaque (and it might change from one OVS version to another). Thus,
3873 * if any other code wants to encode a packet-in, it should use a non-"private"
3874 * version of this function. (Such a version doesn't currently exist because
3875 * only ovs-vswitchd currently wants to encode packet-ins. If you need one,
3878 ofputil_encode_packet_in_private(const struct ofputil_packet_in_private
*pin
,
3879 enum ofputil_protocol protocol
,
3880 enum nx_packet_in_format packet_in_format
)
3882 enum ofp_version version
= ofputil_protocol_to_ofp_version(protocol
);
3885 switch (packet_in_format
) {
3886 case NXPIF_STANDARD
:
3888 case OFPUTIL_P_OF10_STD
:
3889 case OFPUTIL_P_OF10_STD_TID
:
3890 case OFPUTIL_P_OF10_NXM
:
3891 case OFPUTIL_P_OF10_NXM_TID
:
3892 msg
= ofputil_encode_ofp10_packet_in(&pin
->public);
3895 case OFPUTIL_P_OF11_STD
:
3896 msg
= ofputil_encode_ofp11_packet_in(&pin
->public);
3899 case OFPUTIL_P_OF12_OXM
:
3900 case OFPUTIL_P_OF13_OXM
:
3901 case OFPUTIL_P_OF14_OXM
:
3902 case OFPUTIL_P_OF15_OXM
:
3903 case OFPUTIL_P_OF16_OXM
:
3904 msg
= ofputil_encode_ofp12_packet_in(&pin
->public, version
);
3912 case NXPIF_NXT_PACKET_IN
:
3913 msg
= ofputil_encode_nx_packet_in(&pin
->public, version
);
3916 case NXPIF_NXT_PACKET_IN2
:
3917 return ofputil_encode_nx_packet_in2(pin
, version
,
3918 pin
->public.packet_len
);
3924 ofpbuf_put(msg
, pin
->public.packet
, pin
->public.packet_len
);
3925 ofpmsg_update_length(msg
);
3929 /* Returns a string form of 'reason'. The return value is either a statically
3930 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
3931 * 'bufsize' should be at least OFPUTIL_PACKET_IN_REASON_BUFSIZE. */
3933 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason
,
3934 char *reasonbuf
, size_t bufsize
)
3941 case OFPR_INVALID_TTL
:
3942 return "invalid_ttl";
3943 case OFPR_ACTION_SET
:
3944 return "action_set";
3947 case OFPR_PACKET_OUT
:
3948 return "packet_out";
3949 case OFPR_EXPLICIT_MISS
:
3950 case OFPR_IMPLICIT_MISS
:
3953 case OFPR_N_REASONS
:
3955 snprintf(reasonbuf
, bufsize
, "%d", (int) reason
);
3961 ofputil_packet_in_reason_from_string(const char *s
,
3962 enum ofp_packet_in_reason
*reason
)
3966 for (i
= 0; i
< OFPR_N_REASONS
; i
++) {
3967 char reasonbuf
[OFPUTIL_PACKET_IN_REASON_BUFSIZE
];
3968 const char *reason_s
;
3970 reason_s
= ofputil_packet_in_reason_to_string(i
, reasonbuf
,
3972 if (!strcasecmp(s
, reason_s
)) {
3980 /* Returns a newly allocated NXT_RESUME message for 'pin', with the given
3981 * 'continuation', for 'protocol'. This message is suitable for resuming the
3982 * pipeline traveral of the packet represented by 'pin', if sent to the switch
3983 * from which 'pin' was received. */
3985 ofputil_encode_resume(const struct ofputil_packet_in
*pin
,
3986 const struct ofpbuf
*continuation
,
3987 enum ofputil_protocol protocol
)
3989 enum ofp_version version
= ofputil_protocol_to_ofp_version(protocol
);
3990 size_t extra
= pin
->packet_len
+ NXM_TYPICAL_LEN
+ continuation
->size
;
3991 struct ofpbuf
*msg
= ofpraw_alloc_xid(OFPRAW_NXT_RESUME
, version
,
3993 ofputil_put_packet_in(pin
, version
, pin
->packet_len
, msg
);
3994 ofpprop_put_nested(msg
, NXPINT_CONTINUATION
, continuation
);
3995 ofpmsg_update_length(msg
);
4000 parse_subvalue_prop(const struct ofpbuf
*property
, union mf_subvalue
*sv
)
4002 unsigned int len
= ofpbuf_msgsize(property
);
4003 if (len
> sizeof *sv
) {
4004 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXCPT_STACK property has bad length %u",
4006 return OFPERR_OFPBPC_BAD_LEN
;
4008 memset(sv
, 0, sizeof *sv
);
4009 memcpy(&sv
->u8
[sizeof *sv
- len
], property
->msg
, len
);
4014 parse_actions_property(struct ofpbuf
*property
, enum ofp_version version
,
4015 struct ofpbuf
*ofpacts
)
4017 if (!ofpbuf_try_pull(property
, ROUND_UP(ofpbuf_headersize(property
), 8))) {
4018 VLOG_WARN_RL(&bad_ofmsg_rl
, "actions property has bad length %"PRIu32
,
4020 return OFPERR_OFPBPC_BAD_LEN
;
4023 return ofpacts_pull_openflow_actions(property
, property
->size
,
4027 /* This is like ofputil_decode_packet_in(), except that it decodes the
4028 * continuation data into 'pin'. The format of this data is supposed to be
4029 * opaque to any process other than ovs-vswitchd, so this function should not
4030 * be used outside ovs-vswitchd.
4032 * When successful, 'pin' contains some dynamically allocated data. Call
4033 * ofputil_packet_in_private_destroy() to free this data. */
4035 ofputil_decode_packet_in_private(const struct ofp_header
*oh
, bool loose
,
4036 const struct tun_table
*tun_table
,
4037 struct ofputil_packet_in_private
*pin
,
4038 size_t *total_len
, uint32_t *buffer_id
)
4040 memset(pin
, 0, sizeof *pin
);
4042 struct ofpbuf continuation
;
4044 error
= ofputil_decode_packet_in(oh
, loose
, tun_table
, &pin
->public,
4045 total_len
, buffer_id
, &continuation
);
4050 struct ofpbuf actions
, action_set
;
4051 ofpbuf_init(&actions
, 0);
4052 ofpbuf_init(&action_set
, 0);
4054 uint8_t table_id
= 0;
4055 ovs_be64 cookie
= 0;
4057 size_t allocated_stack
= 0;
4059 while (continuation
.size
> 0) {
4060 struct ofpbuf payload
;
4063 error
= ofpprop_pull(&continuation
, &payload
, &type
);
4068 error
= ofpprop_parse_uuid(&payload
, &pin
->bridge
);
4072 if (pin
->n_stack
>= allocated_stack
) {
4073 pin
->stack
= x2nrealloc(pin
->stack
, &allocated_stack
,
4074 sizeof *pin
->stack
);
4076 error
= parse_subvalue_prop(&payload
,
4077 &pin
->stack
[pin
->n_stack
++]);
4081 error
= ofpprop_parse_u32(&payload
, &pin
->mirrors
);
4084 case NXCPT_CONNTRACKED
:
4085 pin
->conntracked
= true;
4088 case NXCPT_TABLE_ID
:
4089 error
= ofpprop_parse_u8(&payload
, &table_id
);
4093 error
= ofpprop_parse_be64(&payload
, &cookie
);
4096 case NXCPT_ACTIONS
: {
4097 struct ofpact_unroll_xlate
*unroll
4098 = ofpact_put_UNROLL_XLATE(&actions
);
4099 unroll
->rule_table_id
= table_id
;
4100 unroll
->rule_cookie
= cookie
;
4101 error
= parse_actions_property(&payload
, oh
->version
, &actions
);
4105 case NXCPT_ACTION_SET
:
4106 error
= parse_actions_property(&payload
, oh
->version
, &action_set
);
4110 error
= OFPPROP_UNKNOWN(loose
, "continuation", type
);
4118 pin
->actions_len
= actions
.size
;
4119 pin
->actions
= ofpbuf_steal_data(&actions
);
4120 pin
->action_set_len
= action_set
.size
;
4121 pin
->action_set
= ofpbuf_steal_data(&action_set
);
4124 ofputil_packet_in_private_destroy(pin
);
4130 /* Frees data in 'pin' that is dynamically allocated by
4131 * ofputil_decode_packet_in_private().
4133 * 'pin->public' contains some pointer members that
4134 * ofputil_decode_packet_in_private() doesn't initialize to newly allocated
4135 * data, so this function doesn't free those. */
4137 ofputil_packet_in_private_destroy(struct ofputil_packet_in_private
*pin
)
4142 free(pin
->action_set
);
4146 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
4149 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
4150 * message's actions. The caller must initialize 'ofpacts' and retains
4151 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
4153 * 'po->packet' refers to the packet data in 'oh', so the buffer containing
4154 * 'oh' must not be destroyed while 'po' is being used.
4156 * Returns 0 if successful, otherwise an OFPERR_* value. */
4158 ofputil_decode_packet_out(struct ofputil_packet_out
*po
,
4159 const struct ofp_header
*oh
,
4160 struct ofpbuf
*ofpacts
)
4162 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
4163 enum ofpraw raw
= ofpraw_pull_assert(&b
);
4165 ofpbuf_clear(ofpacts
);
4166 if (raw
== OFPRAW_OFPT11_PACKET_OUT
) {
4168 const struct ofp11_packet_out
*opo
= ofpbuf_pull(&b
, sizeof *opo
);
4170 po
->buffer_id
= ntohl(opo
->buffer_id
);
4171 error
= ofputil_port_from_ofp11(opo
->in_port
, &po
->in_port
);
4176 error
= ofpacts_pull_openflow_actions(&b
, ntohs(opo
->actions_len
),
4177 oh
->version
, ofpacts
);
4181 } else if (raw
== OFPRAW_OFPT10_PACKET_OUT
) {
4183 const struct ofp10_packet_out
*opo
= ofpbuf_pull(&b
, sizeof *opo
);
4185 po
->buffer_id
= ntohl(opo
->buffer_id
);
4186 po
->in_port
= u16_to_ofp(ntohs(opo
->in_port
));
4188 error
= ofpacts_pull_openflow_actions(&b
, ntohs(opo
->actions_len
),
4189 oh
->version
, ofpacts
);
4197 if (ofp_to_u16(po
->in_port
) >= ofp_to_u16(OFPP_MAX
)
4198 && po
->in_port
!= OFPP_LOCAL
4199 && po
->in_port
!= OFPP_NONE
&& po
->in_port
!= OFPP_CONTROLLER
) {
4200 VLOG_WARN_RL(&bad_ofmsg_rl
, "packet-out has bad input port %#"PRIx16
,
4202 return OFPERR_OFPBRC_BAD_PORT
;
4205 po
->ofpacts
= ofpacts
->data
;
4206 po
->ofpacts_len
= ofpacts
->size
;
4208 if (po
->buffer_id
== UINT32_MAX
) {
4209 po
->packet
= b
.data
;
4210 po
->packet_len
= b
.size
;
4219 /* ofputil_phy_port */
4221 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
4222 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD
== OFPPF_10MB_HD
); /* bit 0 */
4223 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD
== OFPPF_10MB_FD
); /* bit 1 */
4224 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD
== OFPPF_100MB_HD
); /* bit 2 */
4225 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD
== OFPPF_100MB_FD
); /* bit 3 */
4226 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD
== OFPPF_1GB_HD
); /* bit 4 */
4227 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD
== OFPPF_1GB_FD
); /* bit 5 */
4228 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD
== OFPPF_10GB_FD
); /* bit 6 */
4230 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
4231 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER
== (OFPPF10_COPPER
<< 4));
4232 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER
== (OFPPF10_FIBER
<< 4));
4233 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG
== (OFPPF10_AUTONEG
<< 4));
4234 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE
== (OFPPF10_PAUSE
<< 4));
4235 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM
== (OFPPF10_PAUSE_ASYM
<< 4));
4237 static enum netdev_features
4238 netdev_port_features_from_ofp10(ovs_be32 ofp10_
)
4240 uint32_t ofp10
= ntohl(ofp10_
);
4241 return (ofp10
& 0x7f) | ((ofp10
& 0xf80) << 4);
4245 netdev_port_features_to_ofp10(enum netdev_features features
)
4247 return htonl((features
& 0x7f) | ((features
& 0xf800) >> 4));
4250 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD
== OFPPF_10MB_HD
); /* bit 0 */
4251 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD
== OFPPF_10MB_FD
); /* bit 1 */
4252 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD
== OFPPF_100MB_HD
); /* bit 2 */
4253 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD
== OFPPF_100MB_FD
); /* bit 3 */
4254 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD
== OFPPF_1GB_HD
); /* bit 4 */
4255 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD
== OFPPF_1GB_FD
); /* bit 5 */
4256 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD
== OFPPF_10GB_FD
); /* bit 6 */
4257 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD
== OFPPF11_40GB_FD
); /* bit 7 */
4258 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD
== OFPPF11_100GB_FD
); /* bit 8 */
4259 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD
== OFPPF11_1TB_FD
); /* bit 9 */
4260 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER
== OFPPF11_OTHER
); /* bit 10 */
4261 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER
== OFPPF11_COPPER
); /* bit 11 */
4262 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER
== OFPPF11_FIBER
); /* bit 12 */
4263 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG
== OFPPF11_AUTONEG
); /* bit 13 */
4264 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE
== OFPPF11_PAUSE
); /* bit 14 */
4265 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM
== OFPPF11_PAUSE_ASYM
);/* bit 15 */
4267 static enum netdev_features
4268 netdev_port_features_from_ofp11(ovs_be32 ofp11
)
4270 return ntohl(ofp11
) & 0xffff;
4274 netdev_port_features_to_ofp11(enum netdev_features features
)
4276 return htonl(features
& 0xffff);
4280 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port
*pp
,
4281 const struct ofp10_phy_port
*opp
)
4283 pp
->port_no
= u16_to_ofp(ntohs(opp
->port_no
));
4284 pp
->hw_addr
= opp
->hw_addr
;
4285 ovs_strlcpy(pp
->name
, opp
->name
, OFP_MAX_PORT_NAME_LEN
);
4287 pp
->config
= ntohl(opp
->config
) & OFPPC10_ALL
;
4288 pp
->state
= ntohl(opp
->state
) & OFPPS10_ALL
;
4290 pp
->curr
= netdev_port_features_from_ofp10(opp
->curr
);
4291 pp
->advertised
= netdev_port_features_from_ofp10(opp
->advertised
);
4292 pp
->supported
= netdev_port_features_from_ofp10(opp
->supported
);
4293 pp
->peer
= netdev_port_features_from_ofp10(opp
->peer
);
4295 pp
->curr_speed
= netdev_features_to_bps(pp
->curr
, 0) / 1000;
4296 pp
->max_speed
= netdev_features_to_bps(pp
->supported
, 0) / 1000;
4302 ofputil_decode_ofp11_port(struct ofputil_phy_port
*pp
,
4303 const struct ofp11_port
*op
)
4307 error
= ofputil_port_from_ofp11(op
->port_no
, &pp
->port_no
);
4311 pp
->hw_addr
= op
->hw_addr
;
4312 ovs_strlcpy(pp
->name
, op
->name
, OFP_MAX_PORT_NAME_LEN
);
4314 pp
->config
= ntohl(op
->config
) & OFPPC11_ALL
;
4315 pp
->state
= ntohl(op
->state
) & OFPPS11_ALL
;
4317 pp
->curr
= netdev_port_features_from_ofp11(op
->curr
);
4318 pp
->advertised
= netdev_port_features_from_ofp11(op
->advertised
);
4319 pp
->supported
= netdev_port_features_from_ofp11(op
->supported
);
4320 pp
->peer
= netdev_port_features_from_ofp11(op
->peer
);
4322 pp
->curr_speed
= ntohl(op
->curr_speed
);
4323 pp
->max_speed
= ntohl(op
->max_speed
);
4329 parse_ofp14_port_ethernet_property(const struct ofpbuf
*payload
,
4330 struct ofputil_phy_port
*pp
)
4332 struct ofp14_port_desc_prop_ethernet
*eth
= payload
->data
;
4334 if (payload
->size
!= sizeof *eth
) {
4335 return OFPERR_OFPBPC_BAD_LEN
;
4338 pp
->curr
= netdev_port_features_from_ofp11(eth
->curr
);
4339 pp
->advertised
= netdev_port_features_from_ofp11(eth
->advertised
);
4340 pp
->supported
= netdev_port_features_from_ofp11(eth
->supported
);
4341 pp
->peer
= netdev_port_features_from_ofp11(eth
->peer
);
4343 pp
->curr_speed
= ntohl(eth
->curr_speed
);
4344 pp
->max_speed
= ntohl(eth
->max_speed
);
4350 ofputil_pull_ofp14_port(struct ofputil_phy_port
*pp
, struct ofpbuf
*msg
)
4352 struct ofp14_port
*op
= ofpbuf_try_pull(msg
, sizeof *op
);
4354 return OFPERR_OFPBRC_BAD_LEN
;
4357 size_t len
= ntohs(op
->length
);
4358 if (len
< sizeof *op
|| len
- sizeof *op
> msg
->size
) {
4359 return OFPERR_OFPBRC_BAD_LEN
;
4363 enum ofperr error
= ofputil_port_from_ofp11(op
->port_no
, &pp
->port_no
);
4367 pp
->hw_addr
= op
->hw_addr
;
4368 ovs_strlcpy(pp
->name
, op
->name
, OFP_MAX_PORT_NAME_LEN
);
4370 pp
->config
= ntohl(op
->config
) & OFPPC11_ALL
;
4371 pp
->state
= ntohl(op
->state
) & OFPPS11_ALL
;
4373 struct ofpbuf properties
= ofpbuf_const_initializer(ofpbuf_pull(msg
, len
),
4375 while (properties
.size
> 0) {
4376 struct ofpbuf payload
;
4380 error
= ofpprop_pull(&properties
, &payload
, &type
);
4386 case OFPPDPT14_ETHERNET
:
4387 error
= parse_ofp14_port_ethernet_property(&payload
, pp
);
4391 error
= OFPPROP_UNKNOWN(true, "port", type
);
4404 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port
*pp
,
4405 struct ofp10_phy_port
*opp
)
4407 memset(opp
, 0, sizeof *opp
);
4409 opp
->port_no
= htons(ofp_to_u16(pp
->port_no
));
4410 opp
->hw_addr
= pp
->hw_addr
;
4411 ovs_strlcpy(opp
->name
, pp
->name
, OFP_MAX_PORT_NAME_LEN
);
4413 opp
->config
= htonl(pp
->config
& OFPPC10_ALL
);
4414 opp
->state
= htonl(pp
->state
& OFPPS10_ALL
);
4416 opp
->curr
= netdev_port_features_to_ofp10(pp
->curr
);
4417 opp
->advertised
= netdev_port_features_to_ofp10(pp
->advertised
);
4418 opp
->supported
= netdev_port_features_to_ofp10(pp
->supported
);
4419 opp
->peer
= netdev_port_features_to_ofp10(pp
->peer
);
4423 ofputil_encode_ofp11_port(const struct ofputil_phy_port
*pp
,
4424 struct ofp11_port
*op
)
4426 memset(op
, 0, sizeof *op
);
4428 op
->port_no
= ofputil_port_to_ofp11(pp
->port_no
);
4429 op
->hw_addr
= pp
->hw_addr
;
4430 ovs_strlcpy(op
->name
, pp
->name
, OFP_MAX_PORT_NAME_LEN
);
4432 op
->config
= htonl(pp
->config
& OFPPC11_ALL
);
4433 op
->state
= htonl(pp
->state
& OFPPS11_ALL
);
4435 op
->curr
= netdev_port_features_to_ofp11(pp
->curr
);
4436 op
->advertised
= netdev_port_features_to_ofp11(pp
->advertised
);
4437 op
->supported
= netdev_port_features_to_ofp11(pp
->supported
);
4438 op
->peer
= netdev_port_features_to_ofp11(pp
->peer
);
4440 op
->curr_speed
= htonl(pp
->curr_speed
);
4441 op
->max_speed
= htonl(pp
->max_speed
);
4445 ofputil_put_ofp14_port(const struct ofputil_phy_port
*pp
,
4448 struct ofp14_port
*op
;
4449 struct ofp14_port_desc_prop_ethernet
*eth
;
4451 ofpbuf_prealloc_tailroom(b
, sizeof *op
+ sizeof *eth
);
4453 op
= ofpbuf_put_zeros(b
, sizeof *op
);
4454 op
->port_no
= ofputil_port_to_ofp11(pp
->port_no
);
4455 op
->length
= htons(sizeof *op
+ sizeof *eth
);
4456 op
->hw_addr
= pp
->hw_addr
;
4457 ovs_strlcpy(op
->name
, pp
->name
, sizeof op
->name
);
4458 op
->config
= htonl(pp
->config
& OFPPC11_ALL
);
4459 op
->state
= htonl(pp
->state
& OFPPS11_ALL
);
4461 eth
= ofpprop_put_zeros(b
, OFPPDPT14_ETHERNET
, sizeof *eth
);
4462 eth
->curr
= netdev_port_features_to_ofp11(pp
->curr
);
4463 eth
->advertised
= netdev_port_features_to_ofp11(pp
->advertised
);
4464 eth
->supported
= netdev_port_features_to_ofp11(pp
->supported
);
4465 eth
->peer
= netdev_port_features_to_ofp11(pp
->peer
);
4466 eth
->curr_speed
= htonl(pp
->curr_speed
);
4467 eth
->max_speed
= htonl(pp
->max_speed
);
4471 ofputil_put_phy_port(enum ofp_version ofp_version
,
4472 const struct ofputil_phy_port
*pp
, struct ofpbuf
*b
)
4474 switch (ofp_version
) {
4475 case OFP10_VERSION
: {
4476 struct ofp10_phy_port
*opp
= ofpbuf_put_uninit(b
, sizeof *opp
);
4477 ofputil_encode_ofp10_phy_port(pp
, opp
);
4483 case OFP13_VERSION
: {
4484 struct ofp11_port
*op
= ofpbuf_put_uninit(b
, sizeof *op
);
4485 ofputil_encode_ofp11_port(pp
, op
);
4492 ofputil_put_ofp14_port(pp
, b
);
4501 ofputil_decode_port_desc_stats_request(const struct ofp_header
*request
,
4504 struct ofpbuf b
= ofpbuf_const_initializer(request
,
4505 ntohs(request
->length
));
4506 enum ofpraw raw
= ofpraw_pull_assert(&b
);
4507 if (raw
== OFPRAW_OFPST10_PORT_DESC_REQUEST
) {
4510 } else if (raw
== OFPRAW_OFPST15_PORT_DESC_REQUEST
) {
4511 ovs_be32
*ofp11_port
;
4513 ofp11_port
= ofpbuf_pull(&b
, sizeof *ofp11_port
);
4514 return ofputil_port_from_ofp11(*ofp11_port
, port
);
4521 ofputil_encode_port_desc_stats_request(enum ofp_version ofp_version
,
4524 struct ofpbuf
*request
;
4526 switch (ofp_version
) {
4532 request
= ofpraw_alloc(OFPRAW_OFPST10_PORT_DESC_REQUEST
,
4536 case OFP16_VERSION
:{
4537 struct ofp15_port_desc_request
*req
;
4538 request
= ofpraw_alloc(OFPRAW_OFPST15_PORT_DESC_REQUEST
,
4540 req
= ofpbuf_put_zeros(request
, sizeof *req
);
4541 req
->port_no
= ofputil_port_to_ofp11(port
);
4552 ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port
*pp
,
4553 struct ovs_list
*replies
)
4555 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
4556 size_t start_ofs
= reply
->size
;
4558 ofputil_put_phy_port(ofpmp_version(replies
), pp
, reply
);
4559 ofpmp_postappend(replies
, start_ofs
);
4562 /* ofputil_switch_config */
4564 /* Decodes 'oh', which must be an OFPT_GET_CONFIG_REPLY or OFPT_SET_CONFIG
4565 * message, into 'config'. Returns false if 'oh' contained any flags that
4566 * aren't specified in its version of OpenFlow, true otherwise. */
4568 ofputil_decode_switch_config(const struct ofp_header
*oh
,
4569 struct ofputil_switch_config
*config
)
4571 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
4572 ofpraw_pull_assert(&b
);
4574 const struct ofp_switch_config
*osc
= ofpbuf_pull(&b
, sizeof *osc
);
4575 config
->frag
= ntohs(osc
->flags
) & OFPC_FRAG_MASK
;
4576 config
->miss_send_len
= ntohs(osc
->miss_send_len
);
4578 ovs_be16 valid_mask
= htons(OFPC_FRAG_MASK
);
4579 if (oh
->version
< OFP13_VERSION
) {
4580 const ovs_be16 ttl_bit
= htons(OFPC_INVALID_TTL_TO_CONTROLLER
);
4581 valid_mask
|= ttl_bit
;
4582 config
->invalid_ttl_to_controller
= (osc
->flags
& ttl_bit
) != 0;
4584 config
->invalid_ttl_to_controller
= -1;
4587 return !(osc
->flags
& ~valid_mask
);
4591 ofputil_decode_get_config_reply(const struct ofp_header
*oh
,
4592 struct ofputil_switch_config
*config
)
4594 ofputil_decode_switch_config(oh
, config
);
4598 ofputil_decode_set_config(const struct ofp_header
*oh
,
4599 struct ofputil_switch_config
*config
)
4601 return (ofputil_decode_switch_config(oh
, config
)
4603 : OFPERR_OFPSCFC_BAD_FLAGS
);
4606 static struct ofpbuf
*
4607 ofputil_put_switch_config(const struct ofputil_switch_config
*config
,
4610 const struct ofp_header
*oh
= b
->data
;
4611 struct ofp_switch_config
*osc
= ofpbuf_put_zeros(b
, sizeof *osc
);
4612 osc
->flags
= htons(config
->frag
);
4613 if (config
->invalid_ttl_to_controller
> 0 && oh
->version
< OFP13_VERSION
) {
4614 osc
->flags
|= htons(OFPC_INVALID_TTL_TO_CONTROLLER
);
4616 osc
->miss_send_len
= htons(config
->miss_send_len
);
4621 ofputil_encode_get_config_reply(const struct ofp_header
*request
,
4622 const struct ofputil_switch_config
*config
)
4624 struct ofpbuf
*b
= ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY
,
4626 return ofputil_put_switch_config(config
, b
);
4630 ofputil_encode_set_config(const struct ofputil_switch_config
*config
,
4631 enum ofp_version version
)
4633 struct ofpbuf
*b
= ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG
, version
, 0);
4634 return ofputil_put_switch_config(config
, b
);
4637 /* ofputil_switch_features */
4639 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
4640 OFPC_IP_REASM | OFPC_QUEUE_STATS)
4641 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS
== OFPC_FLOW_STATS
);
4642 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS
== OFPC_TABLE_STATS
);
4643 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS
== OFPC_PORT_STATS
);
4644 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM
== OFPC_IP_REASM
);
4645 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS
== OFPC_QUEUE_STATS
);
4646 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP
== OFPC_ARP_MATCH_IP
);
4649 ofputil_capabilities_mask(enum ofp_version ofp_version
)
4651 /* Handle capabilities whose bit is unique for all OpenFlow versions */
4652 switch (ofp_version
) {
4655 return OFPC_COMMON
| OFPC_ARP_MATCH_IP
;
4661 return OFPC_COMMON
| OFPC12_PORT_BLOCKED
;
4663 /* Caller needs to check osf->header.version itself */
4668 /* Pulls an OpenFlow "switch_features" structure from 'b' and decodes it into
4669 * an abstract representation in '*features', readying 'b' to iterate over the
4670 * OpenFlow port structures following 'osf' with later calls to
4671 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an OFPERR_*
4674 ofputil_pull_switch_features(struct ofpbuf
*b
,
4675 struct ofputil_switch_features
*features
)
4677 const struct ofp_header
*oh
= b
->data
;
4678 enum ofpraw raw
= ofpraw_pull_assert(b
);
4679 const struct ofp_switch_features
*osf
= ofpbuf_pull(b
, sizeof *osf
);
4680 features
->datapath_id
= ntohll(osf
->datapath_id
);
4681 features
->n_buffers
= ntohl(osf
->n_buffers
);
4682 features
->n_tables
= osf
->n_tables
;
4683 features
->auxiliary_id
= 0;
4685 features
->capabilities
= ntohl(osf
->capabilities
) &
4686 ofputil_capabilities_mask(oh
->version
);
4688 if (raw
== OFPRAW_OFPT10_FEATURES_REPLY
) {
4689 if (osf
->capabilities
& htonl(OFPC10_STP
)) {
4690 features
->capabilities
|= OFPUTIL_C_STP
;
4692 features
->ofpacts
= ofpact_bitmap_from_openflow(osf
->actions
,
4694 } else if (raw
== OFPRAW_OFPT11_FEATURES_REPLY
4695 || raw
== OFPRAW_OFPT13_FEATURES_REPLY
) {
4696 if (osf
->capabilities
& htonl(OFPC11_GROUP_STATS
)) {
4697 features
->capabilities
|= OFPUTIL_C_GROUP_STATS
;
4699 features
->ofpacts
= 0;
4700 if (raw
== OFPRAW_OFPT13_FEATURES_REPLY
) {
4701 features
->auxiliary_id
= osf
->auxiliary_id
;
4704 return OFPERR_OFPBRC_BAD_VERSION
;
4710 /* In OpenFlow 1.0, 1.1, and 1.2, an OFPT_FEATURES_REPLY message lists all the
4711 * switch's ports, unless there are too many to fit. In OpenFlow 1.3 and
4712 * later, an OFPT_FEATURES_REPLY does not list ports at all.
4714 * Given a buffer 'b' that contains a Features Reply message, this message
4715 * checks if it contains a complete list of the switch's ports. Returns true,
4716 * if so. Returns false if the list is missing (OF1.3+) or incomplete
4717 * (OF1.0/1.1/1.2), and in the latter case removes all of the ports from the
4720 * When this function returns false, the caller should send an OFPST_PORT_DESC
4721 * stats request to get the ports. */
4723 ofputil_switch_features_has_ports(struct ofpbuf
*b
)
4725 struct ofp_header
*oh
= b
->data
;
4726 size_t phy_port_size
;
4728 if (oh
->version
>= OFP13_VERSION
) {
4729 /* OpenFlow 1.3+ never has ports in the feature reply. */
4733 phy_port_size
= (oh
->version
== OFP10_VERSION
4734 ? sizeof(struct ofp10_phy_port
)
4735 : sizeof(struct ofp11_port
));
4736 if (ntohs(oh
->length
) + phy_port_size
<= UINT16_MAX
) {
4737 /* There's room for additional ports in the feature reply.
4738 * Assume that the list is complete. */
4742 /* The feature reply has no room for more ports. Probably the list is
4743 * truncated. Drop the ports and tell the caller to retrieve them with
4744 * OFPST_PORT_DESC. */
4745 b
->size
= sizeof *oh
+ sizeof(struct ofp_switch_features
);
4746 ofpmsg_update_length(b
);
4750 /* Returns a buffer owned by the caller that encodes 'features' in the format
4751 * required by 'protocol' with the given 'xid'. The caller should append port
4752 * information to the buffer with subsequent calls to
4753 * ofputil_put_switch_features_port(). */
4755 ofputil_encode_switch_features(const struct ofputil_switch_features
*features
,
4756 enum ofputil_protocol protocol
, ovs_be32 xid
)
4758 struct ofp_switch_features
*osf
;
4760 enum ofp_version version
;
4763 version
= ofputil_protocol_to_ofp_version(protocol
);
4766 raw
= OFPRAW_OFPT10_FEATURES_REPLY
;
4770 raw
= OFPRAW_OFPT11_FEATURES_REPLY
;
4776 raw
= OFPRAW_OFPT13_FEATURES_REPLY
;
4781 b
= ofpraw_alloc_xid(raw
, version
, xid
, 0);
4782 osf
= ofpbuf_put_zeros(b
, sizeof *osf
);
4783 osf
->datapath_id
= htonll(features
->datapath_id
);
4784 osf
->n_buffers
= htonl(features
->n_buffers
);
4785 osf
->n_tables
= features
->n_tables
;
4787 osf
->capabilities
= htonl(features
->capabilities
& OFPC_COMMON
);
4788 osf
->capabilities
= htonl(features
->capabilities
&
4789 ofputil_capabilities_mask(version
));
4792 if (features
->capabilities
& OFPUTIL_C_STP
) {
4793 osf
->capabilities
|= htonl(OFPC10_STP
);
4795 osf
->actions
= ofpact_bitmap_to_openflow(features
->ofpacts
,
4802 osf
->auxiliary_id
= features
->auxiliary_id
;
4806 if (features
->capabilities
& OFPUTIL_C_GROUP_STATS
) {
4807 osf
->capabilities
|= htonl(OFPC11_GROUP_STATS
);
4817 /* Encodes 'pp' into the format required by the switch_features message already
4818 * in 'b', which should have been returned by ofputil_encode_switch_features(),
4819 * and appends the encoded version to 'b'. */
4821 ofputil_put_switch_features_port(const struct ofputil_phy_port
*pp
,
4824 const struct ofp_header
*oh
= b
->data
;
4826 if (oh
->version
< OFP13_VERSION
) {
4827 /* Try adding a port description to the message, but drop it again if
4828 * the buffer overflows. (This possibility for overflow is why
4829 * OpenFlow 1.3+ moved port descriptions into a multipart message.) */
4830 size_t start_ofs
= b
->size
;
4831 ofputil_put_phy_port(oh
->version
, pp
, b
);
4832 if (b
->size
> UINT16_MAX
) {
4833 b
->size
= start_ofs
;
4838 /* ofputil_port_status */
4840 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
4841 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
4843 ofputil_decode_port_status(const struct ofp_header
*oh
,
4844 struct ofputil_port_status
*ps
)
4846 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
4847 ofpraw_pull_assert(&b
);
4849 const struct ofp_port_status
*ops
= ofpbuf_pull(&b
, sizeof *ops
);
4850 if (ops
->reason
!= OFPPR_ADD
&&
4851 ops
->reason
!= OFPPR_DELETE
&&
4852 ops
->reason
!= OFPPR_MODIFY
) {
4853 return OFPERR_NXBRC_BAD_REASON
;
4855 ps
->reason
= ops
->reason
;
4857 int retval
= ofputil_pull_phy_port(oh
->version
, &b
, &ps
->desc
);
4858 ovs_assert(retval
!= EOF
);
4862 /* Converts the abstract form of a "port status" message in '*ps' into an
4863 * OpenFlow message suitable for 'protocol', and returns that encoded form in
4864 * a buffer owned by the caller. */
4866 ofputil_encode_port_status(const struct ofputil_port_status
*ps
,
4867 enum ofputil_protocol protocol
)
4869 struct ofp_port_status
*ops
;
4871 enum ofp_version version
;
4874 version
= ofputil_protocol_to_ofp_version(protocol
);
4877 raw
= OFPRAW_OFPT10_PORT_STATUS
;
4883 raw
= OFPRAW_OFPT11_PORT_STATUS
;
4889 raw
= OFPRAW_OFPT14_PORT_STATUS
;
4896 b
= ofpraw_alloc_xid(raw
, version
, htonl(0), 0);
4897 ops
= ofpbuf_put_zeros(b
, sizeof *ops
);
4898 ops
->reason
= ps
->reason
;
4899 ofputil_put_phy_port(version
, &ps
->desc
, b
);
4900 ofpmsg_update_length(b
);
4904 /* ofputil_port_mod */
4907 parse_port_mod_ethernet_property(struct ofpbuf
*property
,
4908 struct ofputil_port_mod
*pm
)
4913 error
= ofpprop_parse_be32(property
, &advertise
);
4915 pm
->advertise
= netdev_port_features_from_ofp11(advertise
);
4920 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
4921 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
4923 ofputil_decode_port_mod(const struct ofp_header
*oh
,
4924 struct ofputil_port_mod
*pm
, bool loose
)
4926 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
4927 enum ofpraw raw
= ofpraw_pull_assert(&b
);
4928 if (raw
== OFPRAW_OFPT10_PORT_MOD
) {
4929 const struct ofp10_port_mod
*opm
= b
.data
;
4931 pm
->port_no
= u16_to_ofp(ntohs(opm
->port_no
));
4932 pm
->hw_addr
= opm
->hw_addr
;
4933 pm
->config
= ntohl(opm
->config
) & OFPPC10_ALL
;
4934 pm
->mask
= ntohl(opm
->mask
) & OFPPC10_ALL
;
4935 pm
->advertise
= netdev_port_features_from_ofp10(opm
->advertise
);
4936 } else if (raw
== OFPRAW_OFPT11_PORT_MOD
) {
4937 const struct ofp11_port_mod
*opm
= b
.data
;
4940 error
= ofputil_port_from_ofp11(opm
->port_no
, &pm
->port_no
);
4945 pm
->hw_addr
= opm
->hw_addr
;
4946 pm
->config
= ntohl(opm
->config
) & OFPPC11_ALL
;
4947 pm
->mask
= ntohl(opm
->mask
) & OFPPC11_ALL
;
4948 pm
->advertise
= netdev_port_features_from_ofp11(opm
->advertise
);
4949 } else if (raw
== OFPRAW_OFPT14_PORT_MOD
) {
4950 const struct ofp14_port_mod
*opm
= ofpbuf_pull(&b
, sizeof *opm
);
4953 memset(pm
, 0, sizeof *pm
);
4955 error
= ofputil_port_from_ofp11(opm
->port_no
, &pm
->port_no
);
4960 pm
->hw_addr
= opm
->hw_addr
;
4961 pm
->config
= ntohl(opm
->config
) & OFPPC11_ALL
;
4962 pm
->mask
= ntohl(opm
->mask
) & OFPPC11_ALL
;
4964 while (b
.size
> 0) {
4965 struct ofpbuf property
;
4969 error
= ofpprop_pull(&b
, &property
, &type
);
4975 case OFPPMPT14_ETHERNET
:
4976 error
= parse_port_mod_ethernet_property(&property
, pm
);
4980 error
= OFPPROP_UNKNOWN(loose
, "port_mod", type
);
4989 return OFPERR_OFPBRC_BAD_TYPE
;
4992 pm
->config
&= pm
->mask
;
4996 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
4997 * message suitable for 'protocol', and returns that encoded form in a buffer
4998 * owned by the caller. */
5000 ofputil_encode_port_mod(const struct ofputil_port_mod
*pm
,
5001 enum ofputil_protocol protocol
)
5003 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
5006 switch (ofp_version
) {
5007 case OFP10_VERSION
: {
5008 struct ofp10_port_mod
*opm
;
5010 b
= ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD
, ofp_version
, 0);
5011 opm
= ofpbuf_put_zeros(b
, sizeof *opm
);
5012 opm
->port_no
= htons(ofp_to_u16(pm
->port_no
));
5013 opm
->hw_addr
= pm
->hw_addr
;
5014 opm
->config
= htonl(pm
->config
& OFPPC10_ALL
);
5015 opm
->mask
= htonl(pm
->mask
& OFPPC10_ALL
);
5016 opm
->advertise
= netdev_port_features_to_ofp10(pm
->advertise
);
5022 case OFP13_VERSION
: {
5023 struct ofp11_port_mod
*opm
;
5025 b
= ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD
, ofp_version
, 0);
5026 opm
= ofpbuf_put_zeros(b
, sizeof *opm
);
5027 opm
->port_no
= ofputil_port_to_ofp11(pm
->port_no
);
5028 opm
->hw_addr
= pm
->hw_addr
;
5029 opm
->config
= htonl(pm
->config
& OFPPC11_ALL
);
5030 opm
->mask
= htonl(pm
->mask
& OFPPC11_ALL
);
5031 opm
->advertise
= netdev_port_features_to_ofp11(pm
->advertise
);
5036 case OFP16_VERSION
: {
5037 struct ofp14_port_mod
*opm
;
5039 b
= ofpraw_alloc(OFPRAW_OFPT14_PORT_MOD
, ofp_version
, 0);
5040 opm
= ofpbuf_put_zeros(b
, sizeof *opm
);
5041 opm
->port_no
= ofputil_port_to_ofp11(pm
->port_no
);
5042 opm
->hw_addr
= pm
->hw_addr
;
5043 opm
->config
= htonl(pm
->config
& OFPPC11_ALL
);
5044 opm
->mask
= htonl(pm
->mask
& OFPPC11_ALL
);
5046 if (pm
->advertise
) {
5047 ofpprop_put_be32(b
, OFPPMPT14_ETHERNET
,
5048 netdev_port_features_to_ofp11(pm
->advertise
));
5059 /* Table features. */
5062 pull_table_feature_property(struct ofpbuf
*msg
, struct ofpbuf
*payload
,
5067 error
= ofpprop_pull(msg
, payload
, typep
);
5068 if (payload
&& !error
) {
5069 ofpbuf_pull(payload
, (char *)payload
->msg
- (char *)payload
->header
);
5075 parse_action_bitmap(struct ofpbuf
*payload
, enum ofp_version ofp_version
,
5080 while (payload
->size
> 0) {
5084 error
= ofpprop_pull__(payload
, NULL
, 1, 0x10000, &type
);
5088 if (type
< CHAR_BIT
* sizeof types
) {
5089 types
|= 1u << type
;
5093 *ofpacts
= ofpact_bitmap_from_openflow(htonl(types
), ofp_version
);
5098 parse_instruction_ids(struct ofpbuf
*payload
, bool loose
, uint32_t *insts
)
5101 while (payload
->size
> 0) {
5102 enum ovs_instruction_type inst
;
5106 /* OF1.3 and OF1.4 aren't clear about padding in the instruction IDs.
5107 * It seems clear that they aren't padded to 8 bytes, though, because
5108 * both standards say that "non-experimenter instructions are 4 bytes"
5109 * and do not mention any padding before the first instruction ID.
5110 * (There wouldn't be any point in padding to 8 bytes if the IDs were
5111 * aligned on an odd 4-byte boundary.)
5113 * Anyway, we just assume they're all glommed together on byte
5115 error
= ofpprop_pull__(payload
, NULL
, 1, 0x10000, &ofpit
);
5120 error
= ovs_instruction_type_from_inst_type(&inst
, ofpit
);
5122 *insts
|= 1u << inst
;
5123 } else if (!loose
) {
5131 parse_table_features_next_table(struct ofpbuf
*payload
,
5132 unsigned long int *next_tables
)
5136 memset(next_tables
, 0, bitmap_n_bytes(255));
5137 for (i
= 0; i
< payload
->size
; i
++) {
5138 uint8_t id
= ((const uint8_t *) payload
->data
)[i
];
5140 return OFPERR_OFPBPC_BAD_VALUE
;
5142 bitmap_set1(next_tables
, id
);
5148 parse_oxms(struct ofpbuf
*payload
, bool loose
,
5149 struct mf_bitmap
*exactp
, struct mf_bitmap
*maskedp
)
5151 struct mf_bitmap exact
= MF_BITMAP_INITIALIZER
;
5152 struct mf_bitmap masked
= MF_BITMAP_INITIALIZER
;
5154 while (payload
->size
> 0) {
5155 const struct mf_field
*field
;
5159 error
= nx_pull_header(payload
, &field
, &hasmask
);
5161 bitmap_set1(hasmask
? masked
.bm
: exact
.bm
, field
->id
);
5162 } else if (error
!= OFPERR_OFPBMC_BAD_FIELD
|| !loose
) {
5168 } else if (!bitmap_is_all_zeros(exact
.bm
, MFF_N_IDS
)) {
5169 return OFPERR_OFPBMC_BAD_MASK
;
5173 } else if (!bitmap_is_all_zeros(masked
.bm
, MFF_N_IDS
)) {
5174 return OFPERR_OFPBMC_BAD_MASK
;
5179 /* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract
5180 * ofputil_table_features in 'tf'.
5182 * If 'loose' is true, this function ignores properties and values that it does
5183 * not understand, as a controller would want to do when interpreting
5184 * capabilities provided by a switch. If 'loose' is false, this function
5185 * treats unknown properties and values as an error, as a switch would want to
5186 * do when interpreting a configuration request made by a controller.
5188 * A single OpenFlow message can specify features for multiple tables. Calling
5189 * this function multiple times for a single 'msg' iterates through the tables
5190 * in the message. The caller must initially leave 'msg''s layer pointers null
5191 * and not modify them between calls.
5193 * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise
5194 * a positive "enum ofperr" value. */
5196 ofputil_decode_table_features(struct ofpbuf
*msg
,
5197 struct ofputil_table_features
*tf
, bool loose
)
5199 memset(tf
, 0, sizeof *tf
);
5202 ofpraw_pull_assert(msg
);
5209 const struct ofp_header
*oh
= msg
->header
;
5210 struct ofp13_table_features
*otf
= msg
->data
;
5211 if (msg
->size
< sizeof *otf
) {
5212 return OFPERR_OFPBPC_BAD_LEN
;
5215 unsigned int len
= ntohs(otf
->length
);
5216 if (len
< sizeof *otf
|| len
% 8 || len
> msg
->size
) {
5217 return OFPERR_OFPBPC_BAD_LEN
;
5220 tf
->table_id
= otf
->table_id
;
5221 if (tf
->table_id
== OFPTT_ALL
) {
5222 return OFPERR_OFPTFFC_BAD_TABLE
;
5225 ovs_strlcpy(tf
->name
, otf
->name
, OFP_MAX_TABLE_NAME_LEN
);
5226 tf
->metadata_match
= otf
->metadata_match
;
5227 tf
->metadata_write
= otf
->metadata_write
;
5228 tf
->miss_config
= OFPUTIL_TABLE_MISS_DEFAULT
;
5229 if (oh
->version
>= OFP14_VERSION
) {
5230 uint32_t caps
= ntohl(otf
->capabilities
);
5231 tf
->supports_eviction
= (caps
& OFPTC14_EVICTION
) != 0;
5232 tf
->supports_vacancy_events
= (caps
& OFPTC14_VACANCY_EVENTS
) != 0;
5234 tf
->supports_eviction
= -1;
5235 tf
->supports_vacancy_events
= -1;
5237 tf
->max_entries
= ntohl(otf
->max_entries
);
5239 struct ofpbuf properties
= ofpbuf_const_initializer(ofpbuf_pull(msg
, len
),
5241 ofpbuf_pull(&properties
, sizeof *otf
);
5242 while (properties
.size
> 0) {
5243 struct ofpbuf payload
;
5247 error
= pull_table_feature_property(&properties
, &payload
, &type
);
5252 switch ((enum ofp13_table_feature_prop_type
) type
) {
5253 case OFPTFPT13_INSTRUCTIONS
:
5254 error
= parse_instruction_ids(&payload
, loose
,
5255 &tf
->nonmiss
.instructions
);
5257 case OFPTFPT13_INSTRUCTIONS_MISS
:
5258 error
= parse_instruction_ids(&payload
, loose
,
5259 &tf
->miss
.instructions
);
5262 case OFPTFPT13_NEXT_TABLES
:
5263 error
= parse_table_features_next_table(&payload
,
5266 case OFPTFPT13_NEXT_TABLES_MISS
:
5267 error
= parse_table_features_next_table(&payload
, tf
->miss
.next
);
5270 case OFPTFPT13_WRITE_ACTIONS
:
5271 error
= parse_action_bitmap(&payload
, oh
->version
,
5272 &tf
->nonmiss
.write
.ofpacts
);
5274 case OFPTFPT13_WRITE_ACTIONS_MISS
:
5275 error
= parse_action_bitmap(&payload
, oh
->version
,
5276 &tf
->miss
.write
.ofpacts
);
5279 case OFPTFPT13_APPLY_ACTIONS
:
5280 error
= parse_action_bitmap(&payload
, oh
->version
,
5281 &tf
->nonmiss
.apply
.ofpacts
);
5283 case OFPTFPT13_APPLY_ACTIONS_MISS
:
5284 error
= parse_action_bitmap(&payload
, oh
->version
,
5285 &tf
->miss
.apply
.ofpacts
);
5288 case OFPTFPT13_MATCH
:
5289 error
= parse_oxms(&payload
, loose
, &tf
->match
, &tf
->mask
);
5291 case OFPTFPT13_WILDCARDS
:
5292 error
= parse_oxms(&payload
, loose
, &tf
->wildcard
, NULL
);
5295 case OFPTFPT13_WRITE_SETFIELD
:
5296 error
= parse_oxms(&payload
, loose
,
5297 &tf
->nonmiss
.write
.set_fields
, NULL
);
5299 case OFPTFPT13_WRITE_SETFIELD_MISS
:
5300 error
= parse_oxms(&payload
, loose
,
5301 &tf
->miss
.write
.set_fields
, NULL
);
5303 case OFPTFPT13_APPLY_SETFIELD
:
5304 error
= parse_oxms(&payload
, loose
,
5305 &tf
->nonmiss
.apply
.set_fields
, NULL
);
5307 case OFPTFPT13_APPLY_SETFIELD_MISS
:
5308 error
= parse_oxms(&payload
, loose
,
5309 &tf
->miss
.apply
.set_fields
, NULL
);
5312 case OFPTFPT13_EXPERIMENTER
:
5313 case OFPTFPT13_EXPERIMENTER_MISS
:
5315 error
= OFPPROP_UNKNOWN(loose
, "table features", type
);
5323 /* Fix inconsistencies:
5325 * - Turn on 'match' bits that are set in 'mask', because maskable
5326 * fields are matchable.
5328 * - Turn on 'wildcard' bits that are set in 'mask', because a field
5329 * that is arbitrarily maskable can be wildcarded entirely.
5331 * - Turn off 'wildcard' bits that are not in 'match', because a field
5332 * must be matchable for it to be meaningfully wildcarded. */
5333 bitmap_or(tf
->match
.bm
, tf
->mask
.bm
, MFF_N_IDS
);
5334 bitmap_or(tf
->wildcard
.bm
, tf
->mask
.bm
, MFF_N_IDS
);
5335 bitmap_and(tf
->wildcard
.bm
, tf
->match
.bm
, MFF_N_IDS
);
5340 /* Encodes and returns a request to obtain the table features of a switch.
5341 * The message is encoded for OpenFlow version 'ofp_version'. */
5343 ofputil_encode_table_features_request(enum ofp_version ofp_version
)
5345 struct ofpbuf
*request
= NULL
;
5347 switch (ofp_version
) {
5351 ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later "
5352 "(\'-O OpenFlow13\')");
5357 request
= ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST
,
5368 put_fields_property(struct ofpbuf
*reply
,
5369 const struct mf_bitmap
*fields
,
5370 const struct mf_bitmap
*masks
,
5371 enum ofp13_table_feature_prop_type property
,
5372 enum ofp_version version
)
5377 start_ofs
= ofpprop_start(reply
, property
);
5378 BITMAP_FOR_EACH_1 (field
, MFF_N_IDS
, fields
->bm
) {
5379 nx_put_header(reply
, field
, version
,
5380 masks
&& bitmap_is_set(masks
->bm
, field
));
5382 ofpprop_end(reply
, start_ofs
);
5386 put_table_action_features(struct ofpbuf
*reply
,
5387 const struct ofputil_table_action_features
*taf
,
5388 enum ofp13_table_feature_prop_type actions_type
,
5389 enum ofp13_table_feature_prop_type set_fields_type
,
5390 int miss_offset
, enum ofp_version version
)
5392 ofpprop_put_bitmap(reply
, actions_type
+ miss_offset
,
5393 ntohl(ofpact_bitmap_to_openflow(taf
->ofpacts
,
5395 put_fields_property(reply
, &taf
->set_fields
, NULL
,
5396 set_fields_type
+ miss_offset
, version
);
5400 put_table_instruction_features(
5401 struct ofpbuf
*reply
, const struct ofputil_table_instruction_features
*tif
,
5402 int miss_offset
, enum ofp_version version
)
5407 ofpprop_put_bitmap(reply
, OFPTFPT13_INSTRUCTIONS
+ miss_offset
,
5408 ntohl(ovsinst_bitmap_to_openflow(tif
->instructions
,
5411 start_ofs
= ofpprop_start(reply
, OFPTFPT13_NEXT_TABLES
+ miss_offset
);
5412 BITMAP_FOR_EACH_1 (table_id
, 255, tif
->next
) {
5413 ofpbuf_put(reply
, &table_id
, 1);
5415 ofpprop_end(reply
, start_ofs
);
5417 put_table_action_features(reply
, &tif
->write
,
5418 OFPTFPT13_WRITE_ACTIONS
,
5419 OFPTFPT13_WRITE_SETFIELD
, miss_offset
, version
);
5420 put_table_action_features(reply
, &tif
->apply
,
5421 OFPTFPT13_APPLY_ACTIONS
,
5422 OFPTFPT13_APPLY_SETFIELD
, miss_offset
, version
);
5426 ofputil_append_table_features_reply(const struct ofputil_table_features
*tf
,
5427 struct ovs_list
*replies
)
5429 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
5430 enum ofp_version version
= ofpmp_version(replies
);
5431 size_t start_ofs
= reply
->size
;
5432 struct ofp13_table_features
*otf
;
5434 otf
= ofpbuf_put_zeros(reply
, sizeof *otf
);
5435 otf
->table_id
= tf
->table_id
;
5436 ovs_strlcpy(otf
->name
, tf
->name
, sizeof otf
->name
);
5437 otf
->metadata_match
= tf
->metadata_match
;
5438 otf
->metadata_write
= tf
->metadata_write
;
5439 if (version
>= OFP14_VERSION
) {
5440 if (tf
->supports_eviction
) {
5441 otf
->capabilities
|= htonl(OFPTC14_EVICTION
);
5443 if (tf
->supports_vacancy_events
) {
5444 otf
->capabilities
|= htonl(OFPTC14_VACANCY_EVENTS
);
5447 otf
->max_entries
= htonl(tf
->max_entries
);
5449 put_table_instruction_features(reply
, &tf
->nonmiss
, 0, version
);
5450 put_table_instruction_features(reply
, &tf
->miss
, 1, version
);
5452 put_fields_property(reply
, &tf
->match
, &tf
->mask
,
5453 OFPTFPT13_MATCH
, version
);
5454 put_fields_property(reply
, &tf
->wildcard
, NULL
,
5455 OFPTFPT13_WILDCARDS
, version
);
5457 otf
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *otf
);
5458 otf
->length
= htons(reply
->size
- start_ofs
);
5459 ofpmp_postappend(replies
, start_ofs
);
5463 parse_table_desc_vacancy_property(struct ofpbuf
*property
,
5464 struct ofputil_table_desc
*td
)
5466 struct ofp14_table_mod_prop_vacancy
*otv
= property
->data
;
5468 if (property
->size
!= sizeof *otv
) {
5469 return OFPERR_OFPBPC_BAD_LEN
;
5472 td
->table_vacancy
.vacancy_down
= otv
->vacancy_down
;
5473 td
->table_vacancy
.vacancy_up
= otv
->vacancy_up
;
5474 td
->table_vacancy
.vacancy
= otv
->vacancy
;
5478 /* Decodes the next OpenFlow "table desc" message (of possibly several) from
5479 * 'msg' into an abstract form in '*td'. Returns 0 if successful, EOF if the
5480 * last "table desc" in 'msg' was already decoded, otherwise an OFPERR_*
5483 ofputil_decode_table_desc(struct ofpbuf
*msg
,
5484 struct ofputil_table_desc
*td
,
5485 enum ofp_version version
)
5487 memset(td
, 0, sizeof *td
);
5490 ofpraw_pull_assert(msg
);
5497 struct ofp14_table_desc
*otd
= ofpbuf_try_pull(msg
, sizeof *otd
);
5499 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFP14_TABLE_DESC reply has %"PRIu32
" "
5500 "leftover bytes at end", msg
->size
);
5501 return OFPERR_OFPBRC_BAD_LEN
;
5504 td
->table_id
= otd
->table_id
;
5505 size_t length
= ntohs(otd
->length
);
5506 if (length
< sizeof *otd
|| length
- sizeof *otd
> msg
->size
) {
5507 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFP14_TABLE_DESC reply claims invalid "
5508 "length %"PRIuSIZE
, length
);
5509 return OFPERR_OFPBRC_BAD_LEN
;
5511 length
-= sizeof *otd
;
5513 td
->eviction
= ofputil_decode_table_eviction(otd
->config
, version
);
5514 td
->vacancy
= ofputil_decode_table_vacancy(otd
->config
, version
);
5515 td
->eviction_flags
= UINT32_MAX
;
5517 struct ofpbuf properties
= ofpbuf_const_initializer(
5518 ofpbuf_pull(msg
, length
), length
);
5519 while (properties
.size
> 0) {
5520 struct ofpbuf payload
;
5524 error
= ofpprop_pull(&properties
, &payload
, &type
);
5530 case OFPTMPT14_EVICTION
:
5531 error
= ofpprop_parse_u32(&payload
, &td
->eviction_flags
);
5534 case OFPTMPT14_VACANCY
:
5535 error
= parse_table_desc_vacancy_property(&payload
, td
);
5539 error
= OFPPROP_UNKNOWN(true, "table_desc", type
);
5551 /* Encodes and returns a request to obtain description of tables of a switch.
5552 * The message is encoded for OpenFlow version 'ofp_version'. */
5554 ofputil_encode_table_desc_request(enum ofp_version ofp_version
)
5556 struct ofpbuf
*request
= NULL
;
5558 if (ofp_version
>= OFP14_VERSION
) {
5559 request
= ofpraw_alloc(OFPRAW_OFPST14_TABLE_DESC_REQUEST
,
5562 ovs_fatal(0, "dump-table-desc needs OpenFlow 1.4 or later "
5563 "(\'-O OpenFlow14\')");
5569 /* Function to append Table desc information in a reply list. */
5571 ofputil_append_table_desc_reply(const struct ofputil_table_desc
*td
,
5572 struct ovs_list
*replies
,
5573 enum ofp_version version
)
5575 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
5577 struct ofp14_table_desc
*otd
;
5579 start_otd
= reply
->size
;
5580 ofpbuf_put_zeros(reply
, sizeof *otd
);
5581 if (td
->eviction_flags
!= UINT32_MAX
) {
5582 ofpprop_put_u32(reply
, OFPTMPT14_EVICTION
, td
->eviction_flags
);
5584 if (td
->vacancy
== OFPUTIL_TABLE_VACANCY_ON
) {
5585 struct ofp14_table_mod_prop_vacancy
*otv
;
5587 otv
= ofpprop_put_zeros(reply
, OFPTMPT14_VACANCY
, sizeof *otv
);
5588 otv
->vacancy_down
= td
->table_vacancy
.vacancy_down
;
5589 otv
->vacancy_up
= td
->table_vacancy
.vacancy_up
;
5590 otv
->vacancy
= td
->table_vacancy
.vacancy
;
5593 otd
= ofpbuf_at_assert(reply
, start_otd
, sizeof *otd
);
5594 otd
->length
= htons(reply
->size
- start_otd
);
5595 otd
->table_id
= td
->table_id
;
5596 otd
->config
= ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT
,
5597 td
->eviction
, td
->vacancy
,
5599 ofpmp_postappend(replies
, start_otd
);
5602 /* This function parses Vacancy property, and decodes the
5603 * ofp14_table_mod_prop_vacancy in ofputil_table_mod.
5604 * Returns OFPERR_OFPBPC_BAD_VALUE error code when vacancy_down is
5605 * greater than vacancy_up and also when current vacancy has non-zero
5606 * value. Returns 0 on success. */
5608 parse_table_mod_vacancy_property(struct ofpbuf
*property
,
5609 struct ofputil_table_mod
*tm
)
5611 struct ofp14_table_mod_prop_vacancy
*otv
= property
->data
;
5613 if (property
->size
!= sizeof *otv
) {
5614 return OFPERR_OFPBPC_BAD_LEN
;
5616 tm
->table_vacancy
.vacancy_down
= otv
->vacancy_down
;
5617 tm
->table_vacancy
.vacancy_up
= otv
->vacancy_up
;
5618 if (tm
->table_vacancy
.vacancy_down
> tm
->table_vacancy
.vacancy_up
) {
5619 OFPPROP_LOG(&bad_ofmsg_rl
, false,
5620 "Value of vacancy_down is greater than vacancy_up");
5621 return OFPERR_OFPBPC_BAD_VALUE
;
5623 if (tm
->table_vacancy
.vacancy_down
> 100 ||
5624 tm
->table_vacancy
.vacancy_up
> 100) {
5625 OFPPROP_LOG(&bad_ofmsg_rl
, false, "Vacancy threshold percentage "
5626 "should not be greater than 100");
5627 return OFPERR_OFPBPC_BAD_VALUE
;
5629 tm
->table_vacancy
.vacancy
= otv
->vacancy
;
5630 if (tm
->table_vacancy
.vacancy
) {
5631 OFPPROP_LOG(&bad_ofmsg_rl
, false,
5632 "Vacancy value should be zero for table-mod messages");
5633 return OFPERR_OFPBPC_BAD_VALUE
;
5638 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5639 * table configuration (a table mod, table stats, or table features message),
5640 * returns the table vacancy configuration that it specifies.
5642 * Only OpenFlow 1.4 and later specify table vacancy configuration this way,
5643 * so for other 'version' this function always returns
5644 * OFPUTIL_TABLE_VACANCY_DEFAULT. */
5645 static enum ofputil_table_vacancy
5646 ofputil_decode_table_vacancy(ovs_be32 config
, enum ofp_version version
)
5648 return (version
< OFP14_VERSION
? OFPUTIL_TABLE_VACANCY_DEFAULT
5649 : config
& htonl(OFPTC14_VACANCY_EVENTS
) ? OFPUTIL_TABLE_VACANCY_ON
5650 : OFPUTIL_TABLE_VACANCY_OFF
);
5653 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5654 * table configuration (a table mod, table stats, or table features message),
5655 * returns the table eviction configuration that it specifies.
5657 * Only OpenFlow 1.4 and later specify table eviction configuration this way,
5658 * so for other 'version' values this function always returns
5659 * OFPUTIL_TABLE_EVICTION_DEFAULT. */
5660 static enum ofputil_table_eviction
5661 ofputil_decode_table_eviction(ovs_be32 config
, enum ofp_version version
)
5663 return (version
< OFP14_VERSION
? OFPUTIL_TABLE_EVICTION_DEFAULT
5664 : config
& htonl(OFPTC14_EVICTION
) ? OFPUTIL_TABLE_EVICTION_ON
5665 : OFPUTIL_TABLE_EVICTION_OFF
);
5668 /* Returns a bitmap of OFPTC* values suitable for 'config' fields in various
5669 * OpenFlow messages of the given 'version', based on the provided 'miss' and
5670 * 'eviction' values. */
5672 ofputil_encode_table_config(enum ofputil_table_miss miss
,
5673 enum ofputil_table_eviction eviction
,
5674 enum ofputil_table_vacancy vacancy
,
5675 enum ofp_version version
)
5677 uint32_t config
= 0;
5678 /* See the section "OFPTC_* Table Configuration" in DESIGN.md for more
5679 * information on the crazy evolution of this field. */
5682 /* OpenFlow 1.0 didn't have such a field, any value ought to do. */
5687 /* OpenFlow 1.1 and 1.2 define only OFPTC11_TABLE_MISS_*. */
5689 case OFPUTIL_TABLE_MISS_DEFAULT
:
5690 /* Really this shouldn't be used for encoding (the caller should
5691 * provide a specific value) but I can't imagine that defaulting to
5692 * the fall-through case here will hurt. */
5693 case OFPUTIL_TABLE_MISS_CONTROLLER
:
5695 return htonl(OFPTC11_TABLE_MISS_CONTROLLER
);
5696 case OFPUTIL_TABLE_MISS_CONTINUE
:
5697 return htonl(OFPTC11_TABLE_MISS_CONTINUE
);
5698 case OFPUTIL_TABLE_MISS_DROP
:
5699 return htonl(OFPTC11_TABLE_MISS_DROP
);
5704 /* OpenFlow 1.3 removed OFPTC11_TABLE_MISS_* and didn't define any new
5705 * flags, so this is correct. */
5711 /* OpenFlow 1.4 introduced OFPTC14_EVICTION and
5712 * OFPTC14_VACANCY_EVENTS. */
5713 if (eviction
== OFPUTIL_TABLE_EVICTION_ON
) {
5714 config
|= OFPTC14_EVICTION
;
5716 if (vacancy
== OFPUTIL_TABLE_VACANCY_ON
) {
5717 config
|= OFPTC14_VACANCY_EVENTS
;
5719 return htonl(config
);
5725 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5726 * table configuration (a table mod, table stats, or table features message),
5727 * returns the table miss configuration that it specifies.
5729 * Only OpenFlow 1.1 and 1.2 specify table miss configurations this way, so for
5730 * other 'version' values this function always returns
5731 * OFPUTIL_TABLE_MISS_DEFAULT. */
5732 static enum ofputil_table_miss
5733 ofputil_decode_table_miss(ovs_be32 config_
, enum ofp_version version
)
5735 uint32_t config
= ntohl(config_
);
5737 if (version
== OFP11_VERSION
|| version
== OFP12_VERSION
) {
5738 switch (config
& OFPTC11_TABLE_MISS_MASK
) {
5739 case OFPTC11_TABLE_MISS_CONTROLLER
:
5740 return OFPUTIL_TABLE_MISS_CONTROLLER
;
5742 case OFPTC11_TABLE_MISS_CONTINUE
:
5743 return OFPUTIL_TABLE_MISS_CONTINUE
;
5745 case OFPTC11_TABLE_MISS_DROP
:
5746 return OFPUTIL_TABLE_MISS_DROP
;
5749 VLOG_WARN_RL(&bad_ofmsg_rl
, "bad table miss config %d", config
);
5750 return OFPUTIL_TABLE_MISS_CONTROLLER
;
5753 return OFPUTIL_TABLE_MISS_DEFAULT
;
5757 /* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in
5758 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
5760 ofputil_decode_table_mod(const struct ofp_header
*oh
,
5761 struct ofputil_table_mod
*pm
)
5763 memset(pm
, 0, sizeof *pm
);
5764 pm
->miss
= OFPUTIL_TABLE_MISS_DEFAULT
;
5765 pm
->eviction
= OFPUTIL_TABLE_EVICTION_DEFAULT
;
5766 pm
->eviction_flags
= UINT32_MAX
;
5767 pm
->vacancy
= OFPUTIL_TABLE_VACANCY_DEFAULT
;
5769 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
5770 enum ofpraw raw
= ofpraw_pull_assert(&b
);
5771 if (raw
== OFPRAW_OFPT11_TABLE_MOD
) {
5772 const struct ofp11_table_mod
*otm
= b
.data
;
5774 pm
->table_id
= otm
->table_id
;
5775 pm
->miss
= ofputil_decode_table_miss(otm
->config
, oh
->version
);
5776 } else if (raw
== OFPRAW_OFPT14_TABLE_MOD
) {
5777 const struct ofp14_table_mod
*otm
= ofpbuf_pull(&b
, sizeof *otm
);
5779 pm
->table_id
= otm
->table_id
;
5780 pm
->miss
= ofputil_decode_table_miss(otm
->config
, oh
->version
);
5781 pm
->eviction
= ofputil_decode_table_eviction(otm
->config
, oh
->version
);
5782 pm
->vacancy
= ofputil_decode_table_vacancy(otm
->config
, oh
->version
);
5783 while (b
.size
> 0) {
5784 struct ofpbuf property
;
5788 error
= ofpprop_pull(&b
, &property
, &type
);
5794 case OFPTMPT14_EVICTION
:
5795 error
= ofpprop_parse_u32(&property
, &pm
->eviction
);
5798 case OFPTMPT14_VACANCY
:
5799 error
= parse_table_mod_vacancy_property(&property
, pm
);
5803 error
= OFPERR_OFPBRC_BAD_TYPE
;
5812 return OFPERR_OFPBRC_BAD_TYPE
;
5818 /* Converts the abstract form of a "table mod" message in '*tm' into an
5819 * OpenFlow message suitable for 'protocol', and returns that encoded form in a
5820 * buffer owned by the caller. */
5822 ofputil_encode_table_mod(const struct ofputil_table_mod
*tm
,
5823 enum ofputil_protocol protocol
)
5825 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
5828 switch (ofp_version
) {
5829 case OFP10_VERSION
: {
5830 ovs_fatal(0, "table mod needs OpenFlow 1.1 or later "
5831 "(\'-O OpenFlow11\')");
5836 case OFP13_VERSION
: {
5837 struct ofp11_table_mod
*otm
;
5839 b
= ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD
, ofp_version
, 0);
5840 otm
= ofpbuf_put_zeros(b
, sizeof *otm
);
5841 otm
->table_id
= tm
->table_id
;
5842 otm
->config
= ofputil_encode_table_config(tm
->miss
, tm
->eviction
,
5843 tm
->vacancy
, ofp_version
);
5848 case OFP16_VERSION
: {
5849 struct ofp14_table_mod
*otm
;
5851 b
= ofpraw_alloc(OFPRAW_OFPT14_TABLE_MOD
, ofp_version
, 0);
5852 otm
= ofpbuf_put_zeros(b
, sizeof *otm
);
5853 otm
->table_id
= tm
->table_id
;
5854 otm
->config
= ofputil_encode_table_config(tm
->miss
, tm
->eviction
,
5855 tm
->vacancy
, ofp_version
);
5857 if (tm
->eviction_flags
!= UINT32_MAX
) {
5858 ofpprop_put_u32(b
, OFPTMPT14_EVICTION
, tm
->eviction_flags
);
5860 if (tm
->vacancy
== OFPUTIL_TABLE_VACANCY_ON
) {
5861 struct ofp14_table_mod_prop_vacancy
*otv
;
5863 otv
= ofpprop_put_zeros(b
, OFPTMPT14_VACANCY
, sizeof *otv
);
5864 otv
->vacancy_down
= tm
->table_vacancy
.vacancy_down
;
5865 otv
->vacancy_up
= tm
->table_vacancy
.vacancy_up
;
5876 /* ofputil_role_request */
5878 /* Decodes the OpenFlow "role request" or "role reply" message in '*oh' into
5879 * an abstract form in '*rr'. Returns 0 if successful, otherwise an
5880 * OFPERR_* value. */
5882 ofputil_decode_role_message(const struct ofp_header
*oh
,
5883 struct ofputil_role_request
*rr
)
5885 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
5886 enum ofpraw raw
= ofpraw_pull_assert(&b
);
5887 if (raw
== OFPRAW_OFPT12_ROLE_REQUEST
||
5888 raw
== OFPRAW_OFPT12_ROLE_REPLY
) {
5889 const struct ofp12_role_request
*orr
= b
.msg
;
5891 if (orr
->role
!= htonl(OFPCR12_ROLE_NOCHANGE
) &&
5892 orr
->role
!= htonl(OFPCR12_ROLE_EQUAL
) &&
5893 orr
->role
!= htonl(OFPCR12_ROLE_MASTER
) &&
5894 orr
->role
!= htonl(OFPCR12_ROLE_SLAVE
)) {
5895 return OFPERR_OFPRRFC_BAD_ROLE
;
5898 rr
->role
= ntohl(orr
->role
);
5899 if (raw
== OFPRAW_OFPT12_ROLE_REQUEST
5900 ? orr
->role
== htonl(OFPCR12_ROLE_NOCHANGE
)
5901 : orr
->generation_id
== OVS_BE64_MAX
) {
5902 rr
->have_generation_id
= false;
5903 rr
->generation_id
= 0;
5905 rr
->have_generation_id
= true;
5906 rr
->generation_id
= ntohll(orr
->generation_id
);
5908 } else if (raw
== OFPRAW_NXT_ROLE_REQUEST
||
5909 raw
== OFPRAW_NXT_ROLE_REPLY
) {
5910 const struct nx_role_request
*nrr
= b
.msg
;
5912 BUILD_ASSERT(NX_ROLE_OTHER
+ 1 == OFPCR12_ROLE_EQUAL
);
5913 BUILD_ASSERT(NX_ROLE_MASTER
+ 1 == OFPCR12_ROLE_MASTER
);
5914 BUILD_ASSERT(NX_ROLE_SLAVE
+ 1 == OFPCR12_ROLE_SLAVE
);
5916 if (nrr
->role
!= htonl(NX_ROLE_OTHER
) &&
5917 nrr
->role
!= htonl(NX_ROLE_MASTER
) &&
5918 nrr
->role
!= htonl(NX_ROLE_SLAVE
)) {
5919 return OFPERR_OFPRRFC_BAD_ROLE
;
5922 rr
->role
= ntohl(nrr
->role
) + 1;
5923 rr
->have_generation_id
= false;
5924 rr
->generation_id
= 0;
5932 /* Returns an encoded form of a role reply suitable for the "request" in a
5933 * buffer owned by the caller. */
5935 ofputil_encode_role_reply(const struct ofp_header
*request
,
5936 const struct ofputil_role_request
*rr
)
5941 raw
= ofpraw_decode_assert(request
);
5942 if (raw
== OFPRAW_OFPT12_ROLE_REQUEST
) {
5943 struct ofp12_role_request
*orr
;
5945 buf
= ofpraw_alloc_reply(OFPRAW_OFPT12_ROLE_REPLY
, request
, 0);
5946 orr
= ofpbuf_put_zeros(buf
, sizeof *orr
);
5948 orr
->role
= htonl(rr
->role
);
5949 orr
->generation_id
= htonll(rr
->have_generation_id
5952 } else if (raw
== OFPRAW_NXT_ROLE_REQUEST
) {
5953 struct nx_role_request
*nrr
;
5955 BUILD_ASSERT(NX_ROLE_OTHER
== OFPCR12_ROLE_EQUAL
- 1);
5956 BUILD_ASSERT(NX_ROLE_MASTER
== OFPCR12_ROLE_MASTER
- 1);
5957 BUILD_ASSERT(NX_ROLE_SLAVE
== OFPCR12_ROLE_SLAVE
- 1);
5959 buf
= ofpraw_alloc_reply(OFPRAW_NXT_ROLE_REPLY
, request
, 0);
5960 nrr
= ofpbuf_put_zeros(buf
, sizeof *nrr
);
5961 nrr
->role
= htonl(rr
->role
- 1);
5969 /* Encodes "role status" message 'status' for sending in the given
5970 * 'protocol'. Returns the role status message, if 'protocol' supports them,
5971 * otherwise a null pointer. */
5973 ofputil_encode_role_status(const struct ofputil_role_status
*status
,
5974 enum ofputil_protocol protocol
)
5976 enum ofp_version version
;
5978 version
= ofputil_protocol_to_ofp_version(protocol
);
5979 if (version
>= OFP14_VERSION
) {
5980 struct ofp14_role_status
*rstatus
;
5983 buf
= ofpraw_alloc_xid(OFPRAW_OFPT14_ROLE_STATUS
, version
, htonl(0),
5985 rstatus
= ofpbuf_put_zeros(buf
, sizeof *rstatus
);
5986 rstatus
->role
= htonl(status
->role
);
5987 rstatus
->reason
= status
->reason
;
5988 rstatus
->generation_id
= htonll(status
->generation_id
);
5997 ofputil_decode_role_status(const struct ofp_header
*oh
,
5998 struct ofputil_role_status
*rs
)
6000 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
6001 enum ofpraw raw
= ofpraw_pull_assert(&b
);
6002 ovs_assert(raw
== OFPRAW_OFPT14_ROLE_STATUS
);
6004 const struct ofp14_role_status
*r
= b
.msg
;
6005 if (r
->role
!= htonl(OFPCR12_ROLE_NOCHANGE
) &&
6006 r
->role
!= htonl(OFPCR12_ROLE_EQUAL
) &&
6007 r
->role
!= htonl(OFPCR12_ROLE_MASTER
) &&
6008 r
->role
!= htonl(OFPCR12_ROLE_SLAVE
)) {
6009 return OFPERR_OFPRRFC_BAD_ROLE
;
6012 rs
->role
= ntohl(r
->role
);
6013 rs
->generation_id
= ntohll(r
->generation_id
);
6014 rs
->reason
= r
->reason
;
6019 /* Encodes 'rf' according to 'protocol', and returns the encoded message.
6020 * 'protocol' must be for OpenFlow 1.4 or later. */
6022 ofputil_encode_requestforward(const struct ofputil_requestforward
*rf
,
6023 enum ofputil_protocol protocol
)
6025 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
6026 struct ofpbuf
*inner
;
6028 switch (rf
->reason
) {
6029 case OFPRFR_GROUP_MOD
:
6030 inner
= ofputil_encode_group_mod(ofp_version
, rf
->group_mod
);
6033 case OFPRFR_METER_MOD
:
6034 inner
= ofputil_encode_meter_mod(ofp_version
, rf
->meter_mod
);
6037 case OFPRFR_N_REASONS
:
6042 struct ofp_header
*inner_oh
= inner
->data
;
6043 inner_oh
->xid
= rf
->xid
;
6044 inner_oh
->length
= htons(inner
->size
);
6046 struct ofpbuf
*outer
= ofpraw_alloc_xid(OFPRAW_OFPT14_REQUESTFORWARD
,
6047 ofp_version
, htonl(0),
6049 ofpbuf_put(outer
, inner
->data
, inner
->size
);
6050 ofpbuf_delete(inner
);
6055 /* Decodes OFPT_REQUESTFORWARD message 'outer'. On success, puts the decoded
6056 * form into '*rf' and returns 0, and the caller is later responsible for
6057 * freeing the content of 'rf', with ofputil_destroy_requestforward(rf). On
6058 * failure, returns an ofperr and '*rf' is indeterminate. */
6060 ofputil_decode_requestforward(const struct ofp_header
*outer
,
6061 struct ofputil_requestforward
*rf
)
6063 struct ofpbuf b
= ofpbuf_const_initializer(outer
, ntohs(outer
->length
));
6065 /* Skip past outer message. */
6066 enum ofpraw outer_raw
= ofpraw_pull_assert(&b
);
6067 ovs_assert(outer_raw
== OFPRAW_OFPT14_REQUESTFORWARD
);
6069 /* Validate inner message. */
6070 if (b
.size
< sizeof(struct ofp_header
)) {
6071 return OFPERR_OFPBFC_MSG_BAD_LEN
;
6073 const struct ofp_header
*inner
= b
.data
;
6074 unsigned int inner_len
= ntohs(inner
->length
);
6075 if (inner_len
< sizeof(struct ofp_header
) || inner_len
> b
.size
) {
6076 return OFPERR_OFPBFC_MSG_BAD_LEN
;
6078 if (inner
->version
!= outer
->version
) {
6079 return OFPERR_OFPBRC_BAD_VERSION
;
6082 /* Parse inner message. */
6084 enum ofperr error
= ofptype_decode(&type
, inner
);
6089 rf
->xid
= inner
->xid
;
6090 if (type
== OFPTYPE_GROUP_MOD
) {
6091 rf
->reason
= OFPRFR_GROUP_MOD
;
6092 rf
->group_mod
= xmalloc(sizeof *rf
->group_mod
);
6093 error
= ofputil_decode_group_mod(inner
, rf
->group_mod
);
6095 free(rf
->group_mod
);
6098 } else if (type
== OFPTYPE_METER_MOD
) {
6099 rf
->reason
= OFPRFR_METER_MOD
;
6100 rf
->meter_mod
= xmalloc(sizeof *rf
->meter_mod
);
6101 ofpbuf_init(&rf
->bands
, 64);
6102 error
= ofputil_decode_meter_mod(inner
, rf
->meter_mod
, &rf
->bands
);
6104 free(rf
->meter_mod
);
6105 ofpbuf_uninit(&rf
->bands
);
6109 return OFPERR_OFPBFC_MSG_UNSUP
;
6115 /* Frees the content of 'rf', which should have been initialized through a
6116 * successful call to ofputil_decode_requestforward(). */
6118 ofputil_destroy_requestforward(struct ofputil_requestforward
*rf
)
6124 switch (rf
->reason
) {
6125 case OFPRFR_GROUP_MOD
:
6126 ofputil_uninit_group_mod(rf
->group_mod
);
6127 free(rf
->group_mod
);
6130 case OFPRFR_METER_MOD
:
6131 ofpbuf_uninit(&rf
->bands
);
6132 free(rf
->meter_mod
);
6135 case OFPRFR_N_REASONS
:
6142 /* OpenFlow 1.0 and 1.1 don't distinguish between a field that cannot be
6143 * matched and a field that must be wildcarded. This function returns a bitmap
6144 * that contains both kinds of fields. */
6145 static struct mf_bitmap
6146 wild_or_nonmatchable_fields(const struct ofputil_table_features
*features
)
6148 struct mf_bitmap wc
= features
->match
;
6149 bitmap_not(wc
.bm
, MFF_N_IDS
);
6150 bitmap_or(wc
.bm
, features
->wildcard
.bm
, MFF_N_IDS
);
6154 struct ofp10_wc_map
{
6155 enum ofp10_flow_wildcards wc10
;
6156 enum mf_field_id mf
;
6159 static const struct ofp10_wc_map ofp10_wc_map
[] = {
6160 { OFPFW10_IN_PORT
, MFF_IN_PORT
},
6161 { OFPFW10_DL_VLAN
, MFF_VLAN_VID
},
6162 { OFPFW10_DL_SRC
, MFF_ETH_SRC
},
6163 { OFPFW10_DL_DST
, MFF_ETH_DST
},
6164 { OFPFW10_DL_TYPE
, MFF_ETH_TYPE
},
6165 { OFPFW10_NW_PROTO
, MFF_IP_PROTO
},
6166 { OFPFW10_TP_SRC
, MFF_TCP_SRC
},
6167 { OFPFW10_TP_DST
, MFF_TCP_DST
},
6168 { OFPFW10_NW_SRC_MASK
, MFF_IPV4_SRC
},
6169 { OFPFW10_NW_DST_MASK
, MFF_IPV4_DST
},
6170 { OFPFW10_DL_VLAN_PCP
, MFF_VLAN_PCP
},
6171 { OFPFW10_NW_TOS
, MFF_IP_DSCP
},
6175 mf_bitmap_to_of10(const struct mf_bitmap
*fields
)
6177 const struct ofp10_wc_map
*p
;
6180 for (p
= ofp10_wc_map
; p
< &ofp10_wc_map
[ARRAY_SIZE(ofp10_wc_map
)]; p
++) {
6181 if (bitmap_is_set(fields
->bm
, p
->mf
)) {
6188 static struct mf_bitmap
6189 mf_bitmap_from_of10(ovs_be32 wc10_
)
6191 struct mf_bitmap fields
= MF_BITMAP_INITIALIZER
;
6192 const struct ofp10_wc_map
*p
;
6193 uint32_t wc10
= ntohl(wc10_
);
6195 for (p
= ofp10_wc_map
; p
< &ofp10_wc_map
[ARRAY_SIZE(ofp10_wc_map
)]; p
++) {
6196 if (wc10
& p
->wc10
) {
6197 bitmap_set1(fields
.bm
, p
->mf
);
6204 ofputil_put_ofp10_table_stats(const struct ofputil_table_stats
*stats
,
6205 const struct ofputil_table_features
*features
,
6208 struct mf_bitmap wc
= wild_or_nonmatchable_fields(features
);
6209 struct ofp10_table_stats
*out
;
6211 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
6212 out
->table_id
= features
->table_id
;
6213 ovs_strlcpy(out
->name
, features
->name
, sizeof out
->name
);
6214 out
->wildcards
= mf_bitmap_to_of10(&wc
);
6215 out
->max_entries
= htonl(features
->max_entries
);
6216 out
->active_count
= htonl(stats
->active_count
);
6217 put_32aligned_be64(&out
->lookup_count
, htonll(stats
->lookup_count
));
6218 put_32aligned_be64(&out
->matched_count
, htonll(stats
->matched_count
));
6221 struct ofp11_wc_map
{
6222 enum ofp11_flow_match_fields wc11
;
6223 enum mf_field_id mf
;
6226 static const struct ofp11_wc_map ofp11_wc_map
[] = {
6227 { OFPFMF11_IN_PORT
, MFF_IN_PORT
},
6228 { OFPFMF11_DL_VLAN
, MFF_VLAN_VID
},
6229 { OFPFMF11_DL_VLAN_PCP
, MFF_VLAN_PCP
},
6230 { OFPFMF11_DL_TYPE
, MFF_ETH_TYPE
},
6231 { OFPFMF11_NW_TOS
, MFF_IP_DSCP
},
6232 { OFPFMF11_NW_PROTO
, MFF_IP_PROTO
},
6233 { OFPFMF11_TP_SRC
, MFF_TCP_SRC
},
6234 { OFPFMF11_TP_DST
, MFF_TCP_DST
},
6235 { OFPFMF11_MPLS_LABEL
, MFF_MPLS_LABEL
},
6236 { OFPFMF11_MPLS_TC
, MFF_MPLS_TC
},
6237 /* I don't know what OFPFMF11_TYPE means. */
6238 { OFPFMF11_DL_SRC
, MFF_ETH_SRC
},
6239 { OFPFMF11_DL_DST
, MFF_ETH_DST
},
6240 { OFPFMF11_NW_SRC
, MFF_IPV4_SRC
},
6241 { OFPFMF11_NW_DST
, MFF_IPV4_DST
},
6242 { OFPFMF11_METADATA
, MFF_METADATA
},
6246 mf_bitmap_to_of11(const struct mf_bitmap
*fields
)
6248 const struct ofp11_wc_map
*p
;
6251 for (p
= ofp11_wc_map
; p
< &ofp11_wc_map
[ARRAY_SIZE(ofp11_wc_map
)]; p
++) {
6252 if (bitmap_is_set(fields
->bm
, p
->mf
)) {
6259 static struct mf_bitmap
6260 mf_bitmap_from_of11(ovs_be32 wc11_
)
6262 struct mf_bitmap fields
= MF_BITMAP_INITIALIZER
;
6263 const struct ofp11_wc_map
*p
;
6264 uint32_t wc11
= ntohl(wc11_
);
6266 for (p
= ofp11_wc_map
; p
< &ofp11_wc_map
[ARRAY_SIZE(ofp11_wc_map
)]; p
++) {
6267 if (wc11
& p
->wc11
) {
6268 bitmap_set1(fields
.bm
, p
->mf
);
6275 ofputil_put_ofp11_table_stats(const struct ofputil_table_stats
*stats
,
6276 const struct ofputil_table_features
*features
,
6279 struct mf_bitmap wc
= wild_or_nonmatchable_fields(features
);
6280 struct ofp11_table_stats
*out
;
6282 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
6283 out
->table_id
= features
->table_id
;
6284 ovs_strlcpy(out
->name
, features
->name
, sizeof out
->name
);
6285 out
->wildcards
= mf_bitmap_to_of11(&wc
);
6286 out
->match
= mf_bitmap_to_of11(&features
->match
);
6287 out
->instructions
= ovsinst_bitmap_to_openflow(
6288 features
->nonmiss
.instructions
, OFP11_VERSION
);
6289 out
->write_actions
= ofpact_bitmap_to_openflow(
6290 features
->nonmiss
.write
.ofpacts
, OFP11_VERSION
);
6291 out
->apply_actions
= ofpact_bitmap_to_openflow(
6292 features
->nonmiss
.apply
.ofpacts
, OFP11_VERSION
);
6293 out
->config
= htonl(features
->miss_config
);
6294 out
->max_entries
= htonl(features
->max_entries
);
6295 out
->active_count
= htonl(stats
->active_count
);
6296 out
->lookup_count
= htonll(stats
->lookup_count
);
6297 out
->matched_count
= htonll(stats
->matched_count
);
6301 ofputil_put_ofp12_table_stats(const struct ofputil_table_stats
*stats
,
6302 const struct ofputil_table_features
*features
,
6305 struct ofp12_table_stats
*out
;
6307 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
6308 out
->table_id
= features
->table_id
;
6309 ovs_strlcpy(out
->name
, features
->name
, sizeof out
->name
);
6310 out
->match
= oxm_bitmap_from_mf_bitmap(&features
->match
, OFP12_VERSION
);
6311 out
->wildcards
= oxm_bitmap_from_mf_bitmap(&features
->wildcard
,
6313 out
->write_actions
= ofpact_bitmap_to_openflow(
6314 features
->nonmiss
.write
.ofpacts
, OFP12_VERSION
);
6315 out
->apply_actions
= ofpact_bitmap_to_openflow(
6316 features
->nonmiss
.apply
.ofpacts
, OFP12_VERSION
);
6317 out
->write_setfields
= oxm_bitmap_from_mf_bitmap(
6318 &features
->nonmiss
.write
.set_fields
, OFP12_VERSION
);
6319 out
->apply_setfields
= oxm_bitmap_from_mf_bitmap(
6320 &features
->nonmiss
.apply
.set_fields
, OFP12_VERSION
);
6321 out
->metadata_match
= features
->metadata_match
;
6322 out
->metadata_write
= features
->metadata_write
;
6323 out
->instructions
= ovsinst_bitmap_to_openflow(
6324 features
->nonmiss
.instructions
, OFP12_VERSION
);
6325 out
->config
= ofputil_encode_table_config(features
->miss_config
,
6326 OFPUTIL_TABLE_EVICTION_DEFAULT
,
6327 OFPUTIL_TABLE_VACANCY_DEFAULT
,
6329 out
->max_entries
= htonl(features
->max_entries
);
6330 out
->active_count
= htonl(stats
->active_count
);
6331 out
->lookup_count
= htonll(stats
->lookup_count
);
6332 out
->matched_count
= htonll(stats
->matched_count
);
6336 ofputil_put_ofp13_table_stats(const struct ofputil_table_stats
*stats
,
6339 struct ofp13_table_stats
*out
;
6341 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
6342 out
->table_id
= stats
->table_id
;
6343 out
->active_count
= htonl(stats
->active_count
);
6344 out
->lookup_count
= htonll(stats
->lookup_count
);
6345 out
->matched_count
= htonll(stats
->matched_count
);
6349 ofputil_encode_table_stats_reply(const struct ofp_header
*request
)
6351 return ofpraw_alloc_stats_reply(request
, 0);
6355 ofputil_append_table_stats_reply(struct ofpbuf
*reply
,
6356 const struct ofputil_table_stats
*stats
,
6357 const struct ofputil_table_features
*features
)
6359 struct ofp_header
*oh
= reply
->header
;
6361 ovs_assert(stats
->table_id
== features
->table_id
);
6363 switch ((enum ofp_version
) oh
->version
) {
6365 ofputil_put_ofp10_table_stats(stats
, features
, reply
);
6369 ofputil_put_ofp11_table_stats(stats
, features
, reply
);
6373 ofputil_put_ofp12_table_stats(stats
, features
, reply
);
6380 ofputil_put_ofp13_table_stats(stats
, reply
);
6389 ofputil_decode_ofp10_table_stats(struct ofpbuf
*msg
,
6390 struct ofputil_table_stats
*stats
,
6391 struct ofputil_table_features
*features
)
6393 struct ofp10_table_stats
*ots
;
6395 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
6397 return OFPERR_OFPBRC_BAD_LEN
;
6400 features
->table_id
= ots
->table_id
;
6401 ovs_strlcpy(features
->name
, ots
->name
, sizeof features
->name
);
6402 features
->max_entries
= ntohl(ots
->max_entries
);
6403 features
->match
= features
->wildcard
= mf_bitmap_from_of10(ots
->wildcards
);
6405 stats
->table_id
= ots
->table_id
;
6406 stats
->active_count
= ntohl(ots
->active_count
);
6407 stats
->lookup_count
= ntohll(get_32aligned_be64(&ots
->lookup_count
));
6408 stats
->matched_count
= ntohll(get_32aligned_be64(&ots
->matched_count
));
6414 ofputil_decode_ofp11_table_stats(struct ofpbuf
*msg
,
6415 struct ofputil_table_stats
*stats
,
6416 struct ofputil_table_features
*features
)
6418 struct ofp11_table_stats
*ots
;
6420 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
6422 return OFPERR_OFPBRC_BAD_LEN
;
6425 features
->table_id
= ots
->table_id
;
6426 ovs_strlcpy(features
->name
, ots
->name
, sizeof features
->name
);
6427 features
->max_entries
= ntohl(ots
->max_entries
);
6428 features
->nonmiss
.instructions
= ovsinst_bitmap_from_openflow(
6429 ots
->instructions
, OFP11_VERSION
);
6430 features
->nonmiss
.write
.ofpacts
= ofpact_bitmap_from_openflow(
6431 ots
->write_actions
, OFP11_VERSION
);
6432 features
->nonmiss
.apply
.ofpacts
= ofpact_bitmap_from_openflow(
6433 ots
->write_actions
, OFP11_VERSION
);
6434 features
->miss
= features
->nonmiss
;
6435 features
->miss_config
= ofputil_decode_table_miss(ots
->config
,
6437 features
->match
= mf_bitmap_from_of11(ots
->match
);
6438 features
->wildcard
= mf_bitmap_from_of11(ots
->wildcards
);
6439 bitmap_or(features
->match
.bm
, features
->wildcard
.bm
, MFF_N_IDS
);
6441 stats
->table_id
= ots
->table_id
;
6442 stats
->active_count
= ntohl(ots
->active_count
);
6443 stats
->lookup_count
= ntohll(ots
->lookup_count
);
6444 stats
->matched_count
= ntohll(ots
->matched_count
);
6450 ofputil_decode_ofp12_table_stats(struct ofpbuf
*msg
,
6451 struct ofputil_table_stats
*stats
,
6452 struct ofputil_table_features
*features
)
6454 struct ofp12_table_stats
*ots
;
6456 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
6458 return OFPERR_OFPBRC_BAD_LEN
;
6461 features
->table_id
= ots
->table_id
;
6462 ovs_strlcpy(features
->name
, ots
->name
, sizeof features
->name
);
6463 features
->metadata_match
= ots
->metadata_match
;
6464 features
->metadata_write
= ots
->metadata_write
;
6465 features
->miss_config
= ofputil_decode_table_miss(ots
->config
,
6467 features
->max_entries
= ntohl(ots
->max_entries
);
6469 features
->nonmiss
.instructions
= ovsinst_bitmap_from_openflow(
6470 ots
->instructions
, OFP12_VERSION
);
6471 features
->nonmiss
.write
.ofpacts
= ofpact_bitmap_from_openflow(
6472 ots
->write_actions
, OFP12_VERSION
);
6473 features
->nonmiss
.apply
.ofpacts
= ofpact_bitmap_from_openflow(
6474 ots
->apply_actions
, OFP12_VERSION
);
6475 features
->nonmiss
.write
.set_fields
= oxm_bitmap_to_mf_bitmap(
6476 ots
->write_setfields
, OFP12_VERSION
);
6477 features
->nonmiss
.apply
.set_fields
= oxm_bitmap_to_mf_bitmap(
6478 ots
->apply_setfields
, OFP12_VERSION
);
6479 features
->miss
= features
->nonmiss
;
6481 features
->match
= oxm_bitmap_to_mf_bitmap(ots
->match
, OFP12_VERSION
);
6482 features
->wildcard
= oxm_bitmap_to_mf_bitmap(ots
->wildcards
,
6484 bitmap_or(features
->match
.bm
, features
->wildcard
.bm
, MFF_N_IDS
);
6486 stats
->table_id
= ots
->table_id
;
6487 stats
->active_count
= ntohl(ots
->active_count
);
6488 stats
->lookup_count
= ntohll(ots
->lookup_count
);
6489 stats
->matched_count
= ntohll(ots
->matched_count
);
6495 ofputil_decode_ofp13_table_stats(struct ofpbuf
*msg
,
6496 struct ofputil_table_stats
*stats
,
6497 struct ofputil_table_features
*features
)
6499 struct ofp13_table_stats
*ots
;
6501 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
6503 return OFPERR_OFPBRC_BAD_LEN
;
6506 features
->table_id
= ots
->table_id
;
6508 stats
->table_id
= ots
->table_id
;
6509 stats
->active_count
= ntohl(ots
->active_count
);
6510 stats
->lookup_count
= ntohll(ots
->lookup_count
);
6511 stats
->matched_count
= ntohll(ots
->matched_count
);
6517 ofputil_decode_table_stats_reply(struct ofpbuf
*msg
,
6518 struct ofputil_table_stats
*stats
,
6519 struct ofputil_table_features
*features
)
6521 const struct ofp_header
*oh
;
6524 ofpraw_pull_assert(msg
);
6532 memset(stats
, 0, sizeof *stats
);
6533 memset(features
, 0, sizeof *features
);
6534 features
->supports_eviction
= -1;
6535 features
->supports_vacancy_events
= -1;
6537 switch ((enum ofp_version
) oh
->version
) {
6539 return ofputil_decode_ofp10_table_stats(msg
, stats
, features
);
6542 return ofputil_decode_ofp11_table_stats(msg
, stats
, features
);
6545 return ofputil_decode_ofp12_table_stats(msg
, stats
, features
);
6551 return ofputil_decode_ofp13_table_stats(msg
, stats
, features
);
6558 /* ofputil_flow_monitor_request */
6560 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
6561 * ofputil_flow_monitor_request in 'rq'.
6563 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
6564 * message. Calling this function multiple times for a single 'msg' iterates
6565 * through the requests. The caller must initially leave 'msg''s layer
6566 * pointers null and not modify them between calls.
6568 * Returns 0 if successful, EOF if no requests were left in this 'msg',
6569 * otherwise an OFPERR_* value. */
6571 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request
*rq
,
6574 struct nx_flow_monitor_request
*nfmr
;
6578 ofpraw_pull_assert(msg
);
6585 nfmr
= ofpbuf_try_pull(msg
, sizeof *nfmr
);
6587 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW_MONITOR request has %"PRIu32
" "
6588 "leftover bytes at end", msg
->size
);
6589 return OFPERR_OFPBRC_BAD_LEN
;
6592 flags
= ntohs(nfmr
->flags
);
6593 if (!(flags
& (NXFMF_ADD
| NXFMF_DELETE
| NXFMF_MODIFY
))
6594 || flags
& ~(NXFMF_INITIAL
| NXFMF_ADD
| NXFMF_DELETE
6595 | NXFMF_MODIFY
| NXFMF_ACTIONS
| NXFMF_OWN
)) {
6596 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW_MONITOR has bad flags %#"PRIx16
,
6598 return OFPERR_OFPMOFC_BAD_FLAGS
;
6601 if (!is_all_zeros(nfmr
->zeros
, sizeof nfmr
->zeros
)) {
6602 return OFPERR_NXBRC_MUST_BE_ZERO
;
6605 rq
->id
= ntohl(nfmr
->id
);
6607 rq
->out_port
= u16_to_ofp(ntohs(nfmr
->out_port
));
6608 rq
->table_id
= nfmr
->table_id
;
6610 return nx_pull_match(msg
, ntohs(nfmr
->match_len
), &rq
->match
, NULL
,
6615 ofputil_append_flow_monitor_request(
6616 const struct ofputil_flow_monitor_request
*rq
, struct ofpbuf
*msg
)
6618 struct nx_flow_monitor_request
*nfmr
;
6623 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST
, OFP10_VERSION
, msg
);
6626 start_ofs
= msg
->size
;
6627 ofpbuf_put_zeros(msg
, sizeof *nfmr
);
6628 match_len
= nx_put_match(msg
, &rq
->match
, htonll(0), htonll(0));
6630 nfmr
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *nfmr
);
6631 nfmr
->id
= htonl(rq
->id
);
6632 nfmr
->flags
= htons(rq
->flags
);
6633 nfmr
->out_port
= htons(ofp_to_u16(rq
->out_port
));
6634 nfmr
->match_len
= htons(match_len
);
6635 nfmr
->table_id
= rq
->table_id
;
6638 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
6639 * into an abstract ofputil_flow_update in 'update'. The caller must have
6640 * initialized update->match to point to space allocated for a match.
6642 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
6643 * actions (except for NXFME_ABBREV, which never includes actions). The caller
6644 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
6645 * will point into the 'ofpacts' buffer.
6647 * Multiple flow updates can be packed into a single OpenFlow message. Calling
6648 * this function multiple times for a single 'msg' iterates through the
6649 * updates. The caller must initially leave 'msg''s layer pointers null and
6650 * not modify them between calls.
6652 * Returns 0 if successful, EOF if no updates were left in this 'msg',
6653 * otherwise an OFPERR_* value. */
6655 ofputil_decode_flow_update(struct ofputil_flow_update
*update
,
6656 struct ofpbuf
*msg
, struct ofpbuf
*ofpacts
)
6658 struct nx_flow_update_header
*nfuh
;
6659 unsigned int length
;
6660 struct ofp_header
*oh
;
6663 ofpraw_pull_assert(msg
);
6666 ofpbuf_clear(ofpacts
);
6671 if (msg
->size
< sizeof(struct nx_flow_update_header
)) {
6678 update
->event
= ntohs(nfuh
->event
);
6679 length
= ntohs(nfuh
->length
);
6680 if (length
> msg
->size
|| length
% 8) {
6684 if (update
->event
== NXFME_ABBREV
) {
6685 struct nx_flow_update_abbrev
*nfua
;
6687 if (length
!= sizeof *nfua
) {
6691 nfua
= ofpbuf_pull(msg
, sizeof *nfua
);
6692 update
->xid
= nfua
->xid
;
6694 } else if (update
->event
== NXFME_ADDED
6695 || update
->event
== NXFME_DELETED
6696 || update
->event
== NXFME_MODIFIED
) {
6697 struct nx_flow_update_full
*nfuf
;
6698 unsigned int actions_len
;
6699 unsigned int match_len
;
6702 if (length
< sizeof *nfuf
) {
6706 nfuf
= ofpbuf_pull(msg
, sizeof *nfuf
);
6707 match_len
= ntohs(nfuf
->match_len
);
6708 if (sizeof *nfuf
+ match_len
> length
) {
6712 update
->reason
= ntohs(nfuf
->reason
);
6713 update
->idle_timeout
= ntohs(nfuf
->idle_timeout
);
6714 update
->hard_timeout
= ntohs(nfuf
->hard_timeout
);
6715 update
->table_id
= nfuf
->table_id
;
6716 update
->cookie
= nfuf
->cookie
;
6717 update
->priority
= ntohs(nfuf
->priority
);
6719 error
= nx_pull_match(msg
, match_len
, update
->match
, NULL
, NULL
, NULL
);
6724 actions_len
= length
- sizeof *nfuf
- ROUND_UP(match_len
, 8);
6725 error
= ofpacts_pull_openflow_actions(msg
, actions_len
, oh
->version
,
6731 update
->ofpacts
= ofpacts
->data
;
6732 update
->ofpacts_len
= ofpacts
->size
;
6735 VLOG_WARN_RL(&bad_ofmsg_rl
,
6736 "NXST_FLOW_MONITOR reply has bad event %"PRIu16
,
6737 ntohs(nfuh
->event
));
6738 return OFPERR_NXBRC_FM_BAD_EVENT
;
6742 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW_MONITOR reply has %"PRIu32
" "
6743 "leftover bytes at end", msg
->size
);
6744 return OFPERR_OFPBRC_BAD_LEN
;
6748 ofputil_decode_flow_monitor_cancel(const struct ofp_header
*oh
)
6750 const struct nx_flow_monitor_cancel
*cancel
= ofpmsg_body(oh
);
6752 return ntohl(cancel
->id
);
6756 ofputil_encode_flow_monitor_cancel(uint32_t id
)
6758 struct nx_flow_monitor_cancel
*nfmc
;
6761 msg
= ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL
, OFP10_VERSION
, 0);
6762 nfmc
= ofpbuf_put_uninit(msg
, sizeof *nfmc
);
6763 nfmc
->id
= htonl(id
);
6768 ofputil_start_flow_update(struct ovs_list
*replies
)
6772 msg
= ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY
, OFP10_VERSION
,
6775 ovs_list_init(replies
);
6776 ovs_list_push_back(replies
, &msg
->list_node
);
6780 ofputil_append_flow_update(const struct ofputil_flow_update
*update
,
6781 struct ovs_list
*replies
)
6783 enum ofp_version version
= ofpmp_version(replies
);
6784 struct nx_flow_update_header
*nfuh
;
6788 msg
= ofpbuf_from_list(ovs_list_back(replies
));
6789 start_ofs
= msg
->size
;
6791 if (update
->event
== NXFME_ABBREV
) {
6792 struct nx_flow_update_abbrev
*nfua
;
6794 nfua
= ofpbuf_put_zeros(msg
, sizeof *nfua
);
6795 nfua
->xid
= update
->xid
;
6797 struct nx_flow_update_full
*nfuf
;
6800 ofpbuf_put_zeros(msg
, sizeof *nfuf
);
6801 match_len
= nx_put_match(msg
, update
->match
, htonll(0), htonll(0));
6802 ofpacts_put_openflow_actions(update
->ofpacts
, update
->ofpacts_len
, msg
,
6804 nfuf
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *nfuf
);
6805 nfuf
->reason
= htons(update
->reason
);
6806 nfuf
->priority
= htons(update
->priority
);
6807 nfuf
->idle_timeout
= htons(update
->idle_timeout
);
6808 nfuf
->hard_timeout
= htons(update
->hard_timeout
);
6809 nfuf
->match_len
= htons(match_len
);
6810 nfuf
->table_id
= update
->table_id
;
6811 nfuf
->cookie
= update
->cookie
;
6814 nfuh
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *nfuh
);
6815 nfuh
->length
= htons(msg
->size
- start_ofs
);
6816 nfuh
->event
= htons(update
->event
);
6818 ofpmp_postappend(replies
, start_ofs
);
6822 ofputil_encode_packet_out(const struct ofputil_packet_out
*po
,
6823 enum ofputil_protocol protocol
)
6825 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
6829 size
= po
->ofpacts_len
;
6830 if (po
->buffer_id
== UINT32_MAX
) {
6831 size
+= po
->packet_len
;
6834 switch (ofp_version
) {
6835 case OFP10_VERSION
: {
6836 struct ofp10_packet_out
*opo
;
6839 msg
= ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT
, OFP10_VERSION
, size
);
6840 ofpbuf_put_zeros(msg
, sizeof *opo
);
6841 actions_ofs
= msg
->size
;
6842 ofpacts_put_openflow_actions(po
->ofpacts
, po
->ofpacts_len
, msg
,
6846 opo
->buffer_id
= htonl(po
->buffer_id
);
6847 opo
->in_port
= htons(ofp_to_u16(po
->in_port
));
6848 opo
->actions_len
= htons(msg
->size
- actions_ofs
);
6857 case OFP16_VERSION
: {
6858 struct ofp11_packet_out
*opo
;
6861 msg
= ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT
, ofp_version
, size
);
6862 ofpbuf_put_zeros(msg
, sizeof *opo
);
6863 len
= ofpacts_put_openflow_actions(po
->ofpacts
, po
->ofpacts_len
, msg
,
6866 opo
->buffer_id
= htonl(po
->buffer_id
);
6867 opo
->in_port
= ofputil_port_to_ofp11(po
->in_port
);
6868 opo
->actions_len
= htons(len
);
6876 if (po
->buffer_id
== UINT32_MAX
) {
6877 ofpbuf_put(msg
, po
->packet
, po
->packet_len
);
6880 ofpmsg_update_length(msg
);
6885 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
6887 make_echo_request(enum ofp_version ofp_version
)
6889 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST
, ofp_version
,
6893 /* Creates and returns an OFPT_ECHO_REPLY message matching the
6894 * OFPT_ECHO_REQUEST message in 'rq'. */
6896 make_echo_reply(const struct ofp_header
*rq
)
6898 struct ofpbuf rq_buf
= ofpbuf_const_initializer(rq
, ntohs(rq
->length
));
6899 ofpraw_pull_assert(&rq_buf
);
6901 struct ofpbuf
*reply
= ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY
,
6903 ofpbuf_put(reply
, rq_buf
.data
, rq_buf
.size
);
6908 ofputil_encode_barrier_request(enum ofp_version ofp_version
)
6912 switch (ofp_version
) {
6919 type
= OFPRAW_OFPT11_BARRIER_REQUEST
;
6923 type
= OFPRAW_OFPT10_BARRIER_REQUEST
;
6930 return ofpraw_alloc(type
, ofp_version
, 0);
6934 ofputil_frag_handling_to_string(enum ofputil_frag_handling frag
)
6937 case OFPUTIL_FRAG_NORMAL
: return "normal";
6938 case OFPUTIL_FRAG_DROP
: return "drop";
6939 case OFPUTIL_FRAG_REASM
: return "reassemble";
6940 case OFPUTIL_FRAG_NX_MATCH
: return "nx-match";
6947 ofputil_frag_handling_from_string(const char *s
,
6948 enum ofputil_frag_handling
*frag
)
6950 if (!strcasecmp(s
, "normal")) {
6951 *frag
= OFPUTIL_FRAG_NORMAL
;
6952 } else if (!strcasecmp(s
, "drop")) {
6953 *frag
= OFPUTIL_FRAG_DROP
;
6954 } else if (!strcasecmp(s
, "reassemble")) {
6955 *frag
= OFPUTIL_FRAG_REASM
;
6956 } else if (!strcasecmp(s
, "nx-match")) {
6957 *frag
= OFPUTIL_FRAG_NX_MATCH
;
6964 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
6965 * port number and stores the latter in '*ofp10_port', for the purpose of
6966 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
6967 * otherwise an OFPERR_* number. On error, stores OFPP_NONE in '*ofp10_port'.
6969 * See the definition of OFP11_MAX for an explanation of the mapping. */
6971 ofputil_port_from_ofp11(ovs_be32 ofp11_port
, ofp_port_t
*ofp10_port
)
6973 uint32_t ofp11_port_h
= ntohl(ofp11_port
);
6975 if (ofp11_port_h
< ofp_to_u16(OFPP_MAX
)) {
6976 *ofp10_port
= u16_to_ofp(ofp11_port_h
);
6978 } else if (ofp11_port_h
>= ofp11_to_u32(OFPP11_MAX
)) {
6979 *ofp10_port
= u16_to_ofp(ofp11_port_h
- OFPP11_OFFSET
);
6982 *ofp10_port
= OFPP_NONE
;
6983 VLOG_WARN_RL(&bad_ofmsg_rl
, "port %"PRIu32
" is outside the supported "
6984 "range 0 through %d or 0x%"PRIx32
" through 0x%"PRIx32
,
6985 ofp11_port_h
, ofp_to_u16(OFPP_MAX
) - 1,
6986 ofp11_to_u32(OFPP11_MAX
), UINT32_MAX
);
6987 return OFPERR_OFPBAC_BAD_OUT_PORT
;
6991 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
6992 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
6994 * See the definition of OFP11_MAX for an explanation of the mapping. */
6996 ofputil_port_to_ofp11(ofp_port_t ofp10_port
)
6998 return htonl(ofp_to_u16(ofp10_port
) < ofp_to_u16(OFPP_MAX
)
6999 ? ofp_to_u16(ofp10_port
)
7000 : ofp_to_u16(ofp10_port
) + OFPP11_OFFSET
);
7003 #define OFPUTIL_NAMED_PORTS \
7004 OFPUTIL_NAMED_PORT(IN_PORT) \
7005 OFPUTIL_NAMED_PORT(TABLE) \
7006 OFPUTIL_NAMED_PORT(NORMAL) \
7007 OFPUTIL_NAMED_PORT(FLOOD) \
7008 OFPUTIL_NAMED_PORT(ALL) \
7009 OFPUTIL_NAMED_PORT(CONTROLLER) \
7010 OFPUTIL_NAMED_PORT(LOCAL) \
7011 OFPUTIL_NAMED_PORT(ANY) \
7012 OFPUTIL_NAMED_PORT(UNSET)
7014 /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */
7015 #define OFPUTIL_NAMED_PORTS_WITH_NONE \
7016 OFPUTIL_NAMED_PORTS \
7017 OFPUTIL_NAMED_PORT(NONE)
7019 /* Stores the port number represented by 's' into '*portp'. 's' may be an
7020 * integer or, for reserved ports, the standard OpenFlow name for the port
7023 * Returns true if successful, false if 's' is not a valid OpenFlow port number
7024 * or name. The caller should issue an error message in this case, because
7025 * this function usually does not. (This gives the caller an opportunity to
7026 * look up the port name another way, e.g. by contacting the switch and listing
7027 * the names of all its ports).
7029 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
7030 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
7031 * range as described in include/openflow/openflow-1.1.h. */
7033 ofputil_port_from_string(const char *s
, ofp_port_t
*portp
)
7035 unsigned int port32
; /* int is at least 32 bits wide. */
7038 VLOG_WARN("Negative value %s is not a valid port number.", s
);
7042 if (str_to_uint(s
, 10, &port32
)) {
7043 if (port32
< ofp_to_u16(OFPP_MAX
)) {
7045 } else if (port32
< ofp_to_u16(OFPP_FIRST_RESV
)) {
7046 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
7047 "be translated to %u when talking to an OF1.1 or "
7048 "later controller", port32
, port32
+ OFPP11_OFFSET
);
7049 } else if (port32
<= ofp_to_u16(OFPP_LAST_RESV
)) {
7050 char name
[OFP_MAX_PORT_NAME_LEN
];
7052 ofputil_port_to_string(u16_to_ofp(port32
), name
, sizeof name
);
7053 VLOG_WARN_ONCE("referring to port %s as %"PRIu32
" is deprecated "
7054 "for compatibility with OpenFlow 1.1 and later",
7056 } else if (port32
< ofp11_to_u32(OFPP11_MAX
)) {
7057 VLOG_WARN("port %u is outside the supported range 0 through "
7058 "%"PRIx16
" or 0x%x through 0x%"PRIx32
, port32
,
7059 UINT16_MAX
, ofp11_to_u32(OFPP11_MAX
), UINT32_MAX
);
7062 port32
-= OFPP11_OFFSET
;
7065 *portp
= u16_to_ofp(port32
);
7072 static const struct pair pairs
[] = {
7073 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
7074 OFPUTIL_NAMED_PORTS_WITH_NONE
7075 #undef OFPUTIL_NAMED_PORT
7077 const struct pair
*p
;
7079 for (p
= pairs
; p
< &pairs
[ARRAY_SIZE(pairs
)]; p
++) {
7080 if (!strcasecmp(s
, p
->name
)) {
7089 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
7090 * Most ports' string representation is just the port number, but for special
7091 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
7093 ofputil_format_port(ofp_port_t port
, struct ds
*s
)
7095 char name
[OFP_MAX_PORT_NAME_LEN
];
7097 ofputil_port_to_string(port
, name
, sizeof name
);
7098 ds_put_cstr(s
, name
);
7101 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
7102 * representation of OpenFlow port number 'port'. Most ports are represented
7103 * as just the port number, but special ports, e.g. OFPP_LOCAL, are represented
7104 * by name, e.g. "LOCAL". */
7106 ofputil_port_to_string(ofp_port_t port
,
7107 char namebuf
[OFP_MAX_PORT_NAME_LEN
], size_t bufsize
)
7110 #define OFPUTIL_NAMED_PORT(NAME) \
7112 ovs_strlcpy(namebuf, #NAME, bufsize); \
7115 #undef OFPUTIL_NAMED_PORT
7118 snprintf(namebuf
, bufsize
, "%"PRIu16
, port
);
7123 /* Stores the group id represented by 's' into '*group_idp'. 's' may be an
7124 * integer or, for reserved group IDs, the standard OpenFlow name for the group
7125 * (either "ANY" or "ALL").
7127 * Returns true if successful, false if 's' is not a valid OpenFlow group ID or
7130 ofputil_group_from_string(const char *s
, uint32_t *group_idp
)
7132 if (!strcasecmp(s
, "any")) {
7133 *group_idp
= OFPG_ANY
;
7134 } else if (!strcasecmp(s
, "all")) {
7135 *group_idp
= OFPG_ALL
;
7136 } else if (!str_to_uint(s
, 10, group_idp
)) {
7137 VLOG_WARN("%s is not a valid group ID. (Valid group IDs are "
7138 "32-bit nonnegative integers or the keywords ANY or "
7146 /* Appends to 's' a string representation of the OpenFlow group ID 'group_id'.
7147 * Most groups' string representation is just the number, but for special
7148 * groups, e.g. OFPG_ALL, it is the name, e.g. "ALL". */
7150 ofputil_format_group(uint32_t group_id
, struct ds
*s
)
7152 char name
[MAX_GROUP_NAME_LEN
];
7154 ofputil_group_to_string(group_id
, name
, sizeof name
);
7155 ds_put_cstr(s
, name
);
7159 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
7160 * representation of OpenFlow group ID 'group_id'. Most group are represented
7161 * as just their number, but special groups, e.g. OFPG_ALL, are represented
7162 * by name, e.g. "ALL". */
7164 ofputil_group_to_string(uint32_t group_id
,
7165 char namebuf
[MAX_GROUP_NAME_LEN
+ 1], size_t bufsize
)
7169 ovs_strlcpy(namebuf
, "ALL", bufsize
);
7173 ovs_strlcpy(namebuf
, "ANY", bufsize
);
7177 snprintf(namebuf
, bufsize
, "%"PRIu32
, group_id
);
7182 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
7183 * 'ofp_version', tries to pull the first element from the array. If
7184 * successful, initializes '*pp' with an abstract representation of the
7185 * port and returns 0. If no ports remain to be decoded, returns EOF.
7186 * On an error, returns a positive OFPERR_* value. */
7188 ofputil_pull_phy_port(enum ofp_version ofp_version
, struct ofpbuf
*b
,
7189 struct ofputil_phy_port
*pp
)
7191 memset(pp
, 0, sizeof *pp
);
7193 switch (ofp_version
) {
7194 case OFP10_VERSION
: {
7195 const struct ofp10_phy_port
*opp
= ofpbuf_try_pull(b
, sizeof *opp
);
7196 return opp
? ofputil_decode_ofp10_phy_port(pp
, opp
) : EOF
;
7200 case OFP13_VERSION
: {
7201 const struct ofp11_port
*op
= ofpbuf_try_pull(b
, sizeof *op
);
7202 return op
? ofputil_decode_ofp11_port(pp
, op
) : EOF
;
7207 return b
->size
? ofputil_pull_ofp14_port(pp
, b
) : EOF
;
7214 ofputil_normalize_match__(struct match
*match
, bool may_log
)
7217 MAY_NW_ADDR
= 1 << 0, /* nw_src, nw_dst */
7218 MAY_TP_ADDR
= 1 << 1, /* tp_src, tp_dst */
7219 MAY_NW_PROTO
= 1 << 2, /* nw_proto */
7220 MAY_IPVx
= 1 << 3, /* tos, frag, ttl */
7221 MAY_ARP_SHA
= 1 << 4, /* arp_sha */
7222 MAY_ARP_THA
= 1 << 5, /* arp_tha */
7223 MAY_IPV6
= 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
7224 MAY_ND_TARGET
= 1 << 7, /* nd_target */
7225 MAY_MPLS
= 1 << 8, /* mpls label and tc */
7228 struct flow_wildcards wc
;
7230 /* Figure out what fields may be matched. */
7231 if (match
->flow
.dl_type
== htons(ETH_TYPE_IP
)) {
7232 may_match
= MAY_NW_PROTO
| MAY_IPVx
| MAY_NW_ADDR
;
7233 if (match
->flow
.nw_proto
== IPPROTO_TCP
||
7234 match
->flow
.nw_proto
== IPPROTO_UDP
||
7235 match
->flow
.nw_proto
== IPPROTO_SCTP
||
7236 match
->flow
.nw_proto
== IPPROTO_ICMP
) {
7237 may_match
|= MAY_TP_ADDR
;
7239 } else if (match
->flow
.dl_type
== htons(ETH_TYPE_IPV6
)) {
7240 may_match
= MAY_NW_PROTO
| MAY_IPVx
| MAY_IPV6
;
7241 if (match
->flow
.nw_proto
== IPPROTO_TCP
||
7242 match
->flow
.nw_proto
== IPPROTO_UDP
||
7243 match
->flow
.nw_proto
== IPPROTO_SCTP
) {
7244 may_match
|= MAY_TP_ADDR
;
7245 } else if (match
->flow
.nw_proto
== IPPROTO_ICMPV6
) {
7246 may_match
|= MAY_TP_ADDR
;
7247 if (match
->flow
.tp_src
== htons(ND_NEIGHBOR_SOLICIT
)) {
7248 may_match
|= MAY_ND_TARGET
| MAY_ARP_SHA
;
7249 } else if (match
->flow
.tp_src
== htons(ND_NEIGHBOR_ADVERT
)) {
7250 may_match
|= MAY_ND_TARGET
| MAY_ARP_THA
;
7253 } else if (match
->flow
.dl_type
== htons(ETH_TYPE_ARP
) ||
7254 match
->flow
.dl_type
== htons(ETH_TYPE_RARP
)) {
7255 may_match
= MAY_NW_PROTO
| MAY_NW_ADDR
| MAY_ARP_SHA
| MAY_ARP_THA
;
7256 } else if (eth_type_mpls(match
->flow
.dl_type
)) {
7257 may_match
= MAY_MPLS
;
7262 /* Clear the fields that may not be matched. */
7264 if (!(may_match
& MAY_NW_ADDR
)) {
7265 wc
.masks
.nw_src
= wc
.masks
.nw_dst
= htonl(0);
7267 if (!(may_match
& MAY_TP_ADDR
)) {
7268 wc
.masks
.tp_src
= wc
.masks
.tp_dst
= htons(0);
7270 if (!(may_match
& MAY_NW_PROTO
)) {
7271 wc
.masks
.nw_proto
= 0;
7273 if (!(may_match
& MAY_IPVx
)) {
7274 wc
.masks
.nw_tos
= 0;
7275 wc
.masks
.nw_ttl
= 0;
7277 if (!(may_match
& MAY_ARP_SHA
)) {
7278 WC_UNMASK_FIELD(&wc
, arp_sha
);
7280 if (!(may_match
& MAY_ARP_THA
)) {
7281 WC_UNMASK_FIELD(&wc
, arp_tha
);
7283 if (!(may_match
& MAY_IPV6
)) {
7284 wc
.masks
.ipv6_src
= wc
.masks
.ipv6_dst
= in6addr_any
;
7285 wc
.masks
.ipv6_label
= htonl(0);
7287 if (!(may_match
& MAY_ND_TARGET
)) {
7288 wc
.masks
.nd_target
= in6addr_any
;
7290 if (!(may_match
& MAY_MPLS
)) {
7291 memset(wc
.masks
.mpls_lse
, 0, sizeof wc
.masks
.mpls_lse
);
7294 /* Log any changes. */
7295 if (!flow_wildcards_equal(&wc
, &match
->wc
)) {
7296 bool log
= may_log
&& !VLOG_DROP_INFO(&bad_ofmsg_rl
);
7297 char *pre
= log
? match_to_string(match
, OFP_DEFAULT_PRIORITY
) : NULL
;
7300 match_zero_wildcarded_fields(match
);
7303 char *post
= match_to_string(match
, OFP_DEFAULT_PRIORITY
);
7304 VLOG_INFO("normalization changed ofp_match, details:");
7305 VLOG_INFO(" pre: %s", pre
);
7306 VLOG_INFO("post: %s", post
);
7313 /* "Normalizes" the wildcards in 'match'. That means:
7315 * 1. If the type of level N is known, then only the valid fields for that
7316 * level may be specified. For example, ARP does not have a TOS field,
7317 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
7318 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
7319 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
7322 * 2. If the type of level N is not known (or not understood by Open
7323 * vSwitch), then no fields at all for that level may be specified. For
7324 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
7325 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
7328 * If this function changes 'match', it logs a rate-limited informational
7331 ofputil_normalize_match(struct match
*match
)
7333 ofputil_normalize_match__(match
, true);
7336 /* Same as ofputil_normalize_match() without the logging. Thus, this function
7337 * is suitable for a program's internal use, whereas ofputil_normalize_match()
7338 * sense for use on flows received from elsewhere (so that a bug in the program
7339 * that sent them can be reported and corrected). */
7341 ofputil_normalize_match_quiet(struct match
*match
)
7343 ofputil_normalize_match__(match
, false);
7347 parse_value(const char *s
, const char *delimiters
)
7351 /* Iterate until we reach a delimiter.
7353 * strchr(s, '\0') returns s+strlen(s), so this test handles the null
7354 * terminator at the end of 's'. */
7355 while (!strchr(delimiters
, s
[n
])) {
7370 } while (level
> 0);
7378 /* Parses a key or a key-value pair from '*stringp'.
7380 * On success: Stores the key into '*keyp'. Stores the value, if present, into
7381 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
7382 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
7383 * are substrings of '*stringp' created by replacing some of its bytes by null
7384 * terminators. Returns true.
7386 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
7387 * NULL and returns false. */
7389 ofputil_parse_key_value(char **stringp
, char **keyp
, char **valuep
)
7391 /* Skip white space and delimiters. If that brings us to the end of the
7392 * input string, we are done and there are no more key-value pairs. */
7393 *stringp
+= strspn(*stringp
, ", \t\r\n");
7394 if (**stringp
== '\0') {
7395 *keyp
= *valuep
= NULL
;
7399 /* Extract the key and the delimiter that ends the key-value pair or begins
7400 * the value. Advance the input position past the key and delimiter. */
7401 char *key
= *stringp
;
7402 size_t key_len
= strcspn(key
, ":=(, \t\r\n");
7403 char key_delim
= key
[key_len
];
7404 key
[key_len
] = '\0';
7405 *stringp
+= key_len
+ (key_delim
!= '\0');
7407 /* Figure out what delimiter ends the value:
7409 * - If key_delim is ":" or "=", the value extends until white space
7412 * - If key_delim is "(", the value extends until ")".
7414 * If there is no value, we are done. */
7415 const char *value_delims
;
7416 if (key_delim
== ':' || key_delim
== '=') {
7417 value_delims
= ", \t\r\n";
7418 } else if (key_delim
== '(') {
7422 *valuep
= key
+ key_len
; /* Empty string. */
7426 /* Extract the value. Advance the input position past the value and
7428 char *value
= *stringp
;
7429 size_t value_len
= parse_value(value
, value_delims
);
7430 char value_delim
= value
[value_len
];
7431 value
[value_len
] = '\0';
7432 *stringp
+= value_len
+ (value_delim
!= '\0');
7439 /* Encode a dump ports request for 'port', the encoded message
7440 * will be for OpenFlow version 'ofp_version'. Returns message
7441 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
7443 ofputil_encode_dump_ports_request(enum ofp_version ofp_version
, ofp_port_t port
)
7445 struct ofpbuf
*request
;
7447 switch (ofp_version
) {
7448 case OFP10_VERSION
: {
7449 struct ofp10_port_stats_request
*req
;
7450 request
= ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST
, ofp_version
, 0);
7451 req
= ofpbuf_put_zeros(request
, sizeof *req
);
7452 req
->port_no
= htons(ofp_to_u16(port
));
7460 case OFP16_VERSION
: {
7461 struct ofp11_port_stats_request
*req
;
7462 request
= ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST
, ofp_version
, 0);
7463 req
= ofpbuf_put_zeros(request
, sizeof *req
);
7464 req
->port_no
= ofputil_port_to_ofp11(port
);
7475 ofputil_port_stats_to_ofp10(const struct ofputil_port_stats
*ops
,
7476 struct ofp10_port_stats
*ps10
)
7478 ps10
->port_no
= htons(ofp_to_u16(ops
->port_no
));
7479 memset(ps10
->pad
, 0, sizeof ps10
->pad
);
7480 put_32aligned_be64(&ps10
->rx_packets
, htonll(ops
->stats
.rx_packets
));
7481 put_32aligned_be64(&ps10
->tx_packets
, htonll(ops
->stats
.tx_packets
));
7482 put_32aligned_be64(&ps10
->rx_bytes
, htonll(ops
->stats
.rx_bytes
));
7483 put_32aligned_be64(&ps10
->tx_bytes
, htonll(ops
->stats
.tx_bytes
));
7484 put_32aligned_be64(&ps10
->rx_dropped
, htonll(ops
->stats
.rx_dropped
));
7485 put_32aligned_be64(&ps10
->tx_dropped
, htonll(ops
->stats
.tx_dropped
));
7486 put_32aligned_be64(&ps10
->rx_errors
, htonll(ops
->stats
.rx_errors
));
7487 put_32aligned_be64(&ps10
->tx_errors
, htonll(ops
->stats
.tx_errors
));
7488 put_32aligned_be64(&ps10
->rx_frame_err
, htonll(ops
->stats
.rx_frame_errors
));
7489 put_32aligned_be64(&ps10
->rx_over_err
, htonll(ops
->stats
.rx_over_errors
));
7490 put_32aligned_be64(&ps10
->rx_crc_err
, htonll(ops
->stats
.rx_crc_errors
));
7491 put_32aligned_be64(&ps10
->collisions
, htonll(ops
->stats
.collisions
));
7495 ofputil_port_stats_to_ofp11(const struct ofputil_port_stats
*ops
,
7496 struct ofp11_port_stats
*ps11
)
7498 ps11
->port_no
= ofputil_port_to_ofp11(ops
->port_no
);
7499 memset(ps11
->pad
, 0, sizeof ps11
->pad
);
7500 ps11
->rx_packets
= htonll(ops
->stats
.rx_packets
);
7501 ps11
->tx_packets
= htonll(ops
->stats
.tx_packets
);
7502 ps11
->rx_bytes
= htonll(ops
->stats
.rx_bytes
);
7503 ps11
->tx_bytes
= htonll(ops
->stats
.tx_bytes
);
7504 ps11
->rx_dropped
= htonll(ops
->stats
.rx_dropped
);
7505 ps11
->tx_dropped
= htonll(ops
->stats
.tx_dropped
);
7506 ps11
->rx_errors
= htonll(ops
->stats
.rx_errors
);
7507 ps11
->tx_errors
= htonll(ops
->stats
.tx_errors
);
7508 ps11
->rx_frame_err
= htonll(ops
->stats
.rx_frame_errors
);
7509 ps11
->rx_over_err
= htonll(ops
->stats
.rx_over_errors
);
7510 ps11
->rx_crc_err
= htonll(ops
->stats
.rx_crc_errors
);
7511 ps11
->collisions
= htonll(ops
->stats
.collisions
);
7515 ofputil_port_stats_to_ofp13(const struct ofputil_port_stats
*ops
,
7516 struct ofp13_port_stats
*ps13
)
7518 ofputil_port_stats_to_ofp11(ops
, &ps13
->ps
);
7519 ps13
->duration_sec
= htonl(ops
->duration_sec
);
7520 ps13
->duration_nsec
= htonl(ops
->duration_nsec
);
7524 ofputil_append_ofp14_port_stats(const struct ofputil_port_stats
*ops
,
7525 struct ovs_list
*replies
)
7527 struct ofp14_port_stats_prop_ethernet
*eth
;
7528 struct intel_port_stats_rfc2819
*stats_rfc2819
;
7529 struct ofp14_port_stats
*ps14
;
7530 struct ofpbuf
*reply
;
7532 reply
= ofpmp_reserve(replies
, sizeof *ps14
+ sizeof *eth
+
7533 sizeof *stats_rfc2819
);
7535 ps14
= ofpbuf_put_uninit(reply
, sizeof *ps14
);
7536 ps14
->length
= htons(sizeof *ps14
+ sizeof *eth
+
7537 sizeof *stats_rfc2819
);
7538 memset(ps14
->pad
, 0, sizeof ps14
->pad
);
7539 ps14
->port_no
= ofputil_port_to_ofp11(ops
->port_no
);
7540 ps14
->duration_sec
= htonl(ops
->duration_sec
);
7541 ps14
->duration_nsec
= htonl(ops
->duration_nsec
);
7542 ps14
->rx_packets
= htonll(ops
->stats
.rx_packets
);
7543 ps14
->tx_packets
= htonll(ops
->stats
.tx_packets
);
7544 ps14
->rx_bytes
= htonll(ops
->stats
.rx_bytes
);
7545 ps14
->tx_bytes
= htonll(ops
->stats
.tx_bytes
);
7546 ps14
->rx_dropped
= htonll(ops
->stats
.rx_dropped
);
7547 ps14
->tx_dropped
= htonll(ops
->stats
.tx_dropped
);
7548 ps14
->rx_errors
= htonll(ops
->stats
.rx_errors
);
7549 ps14
->tx_errors
= htonll(ops
->stats
.tx_errors
);
7551 eth
= ofpprop_put_zeros(reply
, OFPPSPT14_ETHERNET
, sizeof *eth
);
7552 eth
->rx_frame_err
= htonll(ops
->stats
.rx_frame_errors
);
7553 eth
->rx_over_err
= htonll(ops
->stats
.rx_over_errors
);
7554 eth
->rx_crc_err
= htonll(ops
->stats
.rx_crc_errors
);
7555 eth
->collisions
= htonll(ops
->stats
.collisions
);
7557 uint64_t prop_type
= OFPPROP_EXP(INTEL_VENDOR_ID
,
7558 INTEL_PORT_STATS_RFC2819
);
7560 stats_rfc2819
= ofpprop_put_zeros(reply
, prop_type
,
7561 sizeof *stats_rfc2819
);
7563 memset(stats_rfc2819
->pad
, 0, sizeof stats_rfc2819
->pad
);
7564 stats_rfc2819
->rx_1_to_64_packets
= htonll(ops
->stats
.rx_1_to_64_packets
);
7565 stats_rfc2819
->rx_65_to_127_packets
=
7566 htonll(ops
->stats
.rx_65_to_127_packets
);
7567 stats_rfc2819
->rx_128_to_255_packets
=
7568 htonll(ops
->stats
.rx_128_to_255_packets
);
7569 stats_rfc2819
->rx_256_to_511_packets
=
7570 htonll(ops
->stats
.rx_256_to_511_packets
);
7571 stats_rfc2819
->rx_512_to_1023_packets
=
7572 htonll(ops
->stats
.rx_512_to_1023_packets
);
7573 stats_rfc2819
->rx_1024_to_1522_packets
=
7574 htonll(ops
->stats
.rx_1024_to_1522_packets
);
7575 stats_rfc2819
->rx_1523_to_max_packets
=
7576 htonll(ops
->stats
.rx_1523_to_max_packets
);
7578 stats_rfc2819
->tx_1_to_64_packets
= htonll(ops
->stats
.tx_1_to_64_packets
);
7579 stats_rfc2819
->tx_65_to_127_packets
=
7580 htonll(ops
->stats
.tx_65_to_127_packets
);
7581 stats_rfc2819
->tx_128_to_255_packets
=
7582 htonll(ops
->stats
.tx_128_to_255_packets
);
7583 stats_rfc2819
->tx_256_to_511_packets
=
7584 htonll(ops
->stats
.tx_256_to_511_packets
);
7585 stats_rfc2819
->tx_512_to_1023_packets
=
7586 htonll(ops
->stats
.tx_512_to_1023_packets
);
7587 stats_rfc2819
->tx_1024_to_1522_packets
=
7588 htonll(ops
->stats
.tx_1024_to_1522_packets
);
7589 stats_rfc2819
->tx_1523_to_max_packets
=
7590 htonll(ops
->stats
.tx_1523_to_max_packets
);
7592 stats_rfc2819
->tx_multicast_packets
=
7593 htonll(ops
->stats
.tx_multicast_packets
);
7594 stats_rfc2819
->rx_broadcast_packets
=
7595 htonll(ops
->stats
.rx_broadcast_packets
);
7596 stats_rfc2819
->tx_broadcast_packets
=
7597 htonll(ops
->stats
.tx_broadcast_packets
);
7598 stats_rfc2819
->rx_undersized_errors
=
7599 htonll(ops
->stats
.rx_undersized_errors
);
7600 stats_rfc2819
->rx_oversize_errors
=
7601 htonll(ops
->stats
.rx_oversize_errors
);
7602 stats_rfc2819
->rx_fragmented_errors
=
7603 htonll(ops
->stats
.rx_fragmented_errors
);
7604 stats_rfc2819
->rx_jabber_errors
=
7605 htonll(ops
->stats
.rx_jabber_errors
);
7608 /* Encode a ports stat for 'ops' and append it to 'replies'. */
7610 ofputil_append_port_stat(struct ovs_list
*replies
,
7611 const struct ofputil_port_stats
*ops
)
7613 switch (ofpmp_version(replies
)) {
7614 case OFP13_VERSION
: {
7615 struct ofp13_port_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
7616 ofputil_port_stats_to_ofp13(ops
, reply
);
7620 case OFP11_VERSION
: {
7621 struct ofp11_port_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
7622 ofputil_port_stats_to_ofp11(ops
, reply
);
7626 case OFP10_VERSION
: {
7627 struct ofp10_port_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
7628 ofputil_port_stats_to_ofp10(ops
, reply
);
7635 ofputil_append_ofp14_port_stats(ops
, replies
);
7644 ofputil_port_stats_from_ofp10(struct ofputil_port_stats
*ops
,
7645 const struct ofp10_port_stats
*ps10
)
7648 ops
->port_no
= u16_to_ofp(ntohs(ps10
->port_no
));
7649 ops
->stats
.rx_packets
= ntohll(get_32aligned_be64(&ps10
->rx_packets
));
7650 ops
->stats
.tx_packets
= ntohll(get_32aligned_be64(&ps10
->tx_packets
));
7651 ops
->stats
.rx_bytes
= ntohll(get_32aligned_be64(&ps10
->rx_bytes
));
7652 ops
->stats
.tx_bytes
= ntohll(get_32aligned_be64(&ps10
->tx_bytes
));
7653 ops
->stats
.rx_dropped
= ntohll(get_32aligned_be64(&ps10
->rx_dropped
));
7654 ops
->stats
.tx_dropped
= ntohll(get_32aligned_be64(&ps10
->tx_dropped
));
7655 ops
->stats
.rx_errors
= ntohll(get_32aligned_be64(&ps10
->rx_errors
));
7656 ops
->stats
.tx_errors
= ntohll(get_32aligned_be64(&ps10
->tx_errors
));
7657 ops
->stats
.rx_frame_errors
=
7658 ntohll(get_32aligned_be64(&ps10
->rx_frame_err
));
7659 ops
->stats
.rx_over_errors
= ntohll(get_32aligned_be64(&ps10
->rx_over_err
));
7660 ops
->stats
.rx_crc_errors
= ntohll(get_32aligned_be64(&ps10
->rx_crc_err
));
7661 ops
->stats
.collisions
= ntohll(get_32aligned_be64(&ps10
->collisions
));
7662 ops
->duration_sec
= ops
->duration_nsec
= UINT32_MAX
;
7668 ofputil_port_stats_from_ofp11(struct ofputil_port_stats
*ops
,
7669 const struct ofp11_port_stats
*ps11
)
7673 error
= ofputil_port_from_ofp11(ps11
->port_no
, &ops
->port_no
);
7678 ops
->stats
.rx_packets
= ntohll(ps11
->rx_packets
);
7679 ops
->stats
.tx_packets
= ntohll(ps11
->tx_packets
);
7680 ops
->stats
.rx_bytes
= ntohll(ps11
->rx_bytes
);
7681 ops
->stats
.tx_bytes
= ntohll(ps11
->tx_bytes
);
7682 ops
->stats
.rx_dropped
= ntohll(ps11
->rx_dropped
);
7683 ops
->stats
.tx_dropped
= ntohll(ps11
->tx_dropped
);
7684 ops
->stats
.rx_errors
= ntohll(ps11
->rx_errors
);
7685 ops
->stats
.tx_errors
= ntohll(ps11
->tx_errors
);
7686 ops
->stats
.rx_frame_errors
= ntohll(ps11
->rx_frame_err
);
7687 ops
->stats
.rx_over_errors
= ntohll(ps11
->rx_over_err
);
7688 ops
->stats
.rx_crc_errors
= ntohll(ps11
->rx_crc_err
);
7689 ops
->stats
.collisions
= ntohll(ps11
->collisions
);
7690 ops
->duration_sec
= ops
->duration_nsec
= UINT32_MAX
;
7696 ofputil_port_stats_from_ofp13(struct ofputil_port_stats
*ops
,
7697 const struct ofp13_port_stats
*ps13
)
7699 enum ofperr error
= ofputil_port_stats_from_ofp11(ops
, &ps13
->ps
);
7701 ops
->duration_sec
= ntohl(ps13
->duration_sec
);
7702 ops
->duration_nsec
= ntohl(ps13
->duration_nsec
);
7708 parse_ofp14_port_stats_ethernet_property(const struct ofpbuf
*payload
,
7709 struct ofputil_port_stats
*ops
)
7711 const struct ofp14_port_stats_prop_ethernet
*eth
= payload
->data
;
7713 if (payload
->size
!= sizeof *eth
) {
7714 return OFPERR_OFPBPC_BAD_LEN
;
7717 ops
->stats
.rx_frame_errors
= ntohll(eth
->rx_frame_err
);
7718 ops
->stats
.rx_over_errors
= ntohll(eth
->rx_over_err
);
7719 ops
->stats
.rx_crc_errors
= ntohll(eth
->rx_crc_err
);
7720 ops
->stats
.collisions
= ntohll(eth
->collisions
);
7726 parse_intel_port_stats_rfc2819_property(const struct ofpbuf
*payload
,
7727 struct ofputil_port_stats
*ops
)
7729 const struct intel_port_stats_rfc2819
*rfc2819
= payload
->data
;
7731 if (payload
->size
!= sizeof *rfc2819
) {
7732 return OFPERR_OFPBPC_BAD_LEN
;
7734 ops
->stats
.rx_1_to_64_packets
= ntohll(rfc2819
->rx_1_to_64_packets
);
7735 ops
->stats
.rx_65_to_127_packets
= ntohll(rfc2819
->rx_65_to_127_packets
);
7736 ops
->stats
.rx_128_to_255_packets
= ntohll(rfc2819
->rx_128_to_255_packets
);
7737 ops
->stats
.rx_256_to_511_packets
= ntohll(rfc2819
->rx_256_to_511_packets
);
7738 ops
->stats
.rx_512_to_1023_packets
=
7739 ntohll(rfc2819
->rx_512_to_1023_packets
);
7740 ops
->stats
.rx_1024_to_1522_packets
=
7741 ntohll(rfc2819
->rx_1024_to_1522_packets
);
7742 ops
->stats
.rx_1523_to_max_packets
=
7743 ntohll(rfc2819
->rx_1523_to_max_packets
);
7745 ops
->stats
.tx_1_to_64_packets
= ntohll(rfc2819
->tx_1_to_64_packets
);
7746 ops
->stats
.tx_65_to_127_packets
= ntohll(rfc2819
->tx_65_to_127_packets
);
7747 ops
->stats
.tx_128_to_255_packets
= ntohll(rfc2819
->tx_128_to_255_packets
);
7748 ops
->stats
.tx_256_to_511_packets
= ntohll(rfc2819
->tx_256_to_511_packets
);
7749 ops
->stats
.tx_512_to_1023_packets
=
7750 ntohll(rfc2819
->tx_512_to_1023_packets
);
7751 ops
->stats
.tx_1024_to_1522_packets
=
7752 ntohll(rfc2819
->tx_1024_to_1522_packets
);
7753 ops
->stats
.tx_1523_to_max_packets
=
7754 ntohll(rfc2819
->tx_1523_to_max_packets
);
7756 ops
->stats
.tx_multicast_packets
= ntohll(rfc2819
->tx_multicast_packets
);
7757 ops
->stats
.rx_broadcast_packets
= ntohll(rfc2819
->rx_broadcast_packets
);
7758 ops
->stats
.tx_broadcast_packets
= ntohll(rfc2819
->tx_broadcast_packets
);
7759 ops
->stats
.rx_undersized_errors
= ntohll(rfc2819
->rx_undersized_errors
);
7761 ops
->stats
.rx_oversize_errors
= ntohll(rfc2819
->rx_oversize_errors
);
7762 ops
->stats
.rx_fragmented_errors
= ntohll(rfc2819
->rx_fragmented_errors
);
7763 ops
->stats
.rx_jabber_errors
= ntohll(rfc2819
->rx_jabber_errors
);
7769 parse_intel_port_stats_property(const struct ofpbuf
*payload
,
7771 struct ofputil_port_stats
*ops
)
7776 case INTEL_PORT_STATS_RFC2819
:
7777 error
= parse_intel_port_stats_rfc2819_property(payload
, ops
);
7780 error
= OFPERR_OFPBPC_BAD_EXP_TYPE
;
7788 ofputil_pull_ofp14_port_stats(struct ofputil_port_stats
*ops
,
7791 const struct ofp14_port_stats
*ps14
= ofpbuf_try_pull(msg
, sizeof *ps14
);
7793 return OFPERR_OFPBRC_BAD_LEN
;
7796 size_t len
= ntohs(ps14
->length
);
7797 if (len
< sizeof *ps14
|| len
- sizeof *ps14
> msg
->size
) {
7798 return OFPERR_OFPBRC_BAD_LEN
;
7800 len
-= sizeof *ps14
;
7802 enum ofperr error
= ofputil_port_from_ofp11(ps14
->port_no
, &ops
->port_no
);
7807 ops
->duration_sec
= ntohl(ps14
->duration_sec
);
7808 ops
->duration_nsec
= ntohl(ps14
->duration_nsec
);
7809 ops
->stats
.rx_packets
= ntohll(ps14
->rx_packets
);
7810 ops
->stats
.tx_packets
= ntohll(ps14
->tx_packets
);
7811 ops
->stats
.rx_bytes
= ntohll(ps14
->rx_bytes
);
7812 ops
->stats
.tx_bytes
= ntohll(ps14
->tx_bytes
);
7813 ops
->stats
.rx_dropped
= ntohll(ps14
->rx_dropped
);
7814 ops
->stats
.tx_dropped
= ntohll(ps14
->tx_dropped
);
7815 ops
->stats
.rx_errors
= ntohll(ps14
->rx_errors
);
7816 ops
->stats
.tx_errors
= ntohll(ps14
->tx_errors
);
7819 struct ofpbuf properties
= ofpbuf_const_initializer(ofpbuf_pull(msg
, len
),
7821 while (properties
.size
> 0) {
7822 struct ofpbuf payload
;
7826 error
= ofpprop_pull(&properties
, &payload
, &type
);
7831 case OFPPSPT14_ETHERNET
:
7832 error
= parse_ofp14_port_stats_ethernet_property(&payload
, ops
);
7834 case OFPPROP_EXP(INTEL_VENDOR_ID
, INTEL_PORT_STATS_RFC2819
):
7835 error
= parse_intel_port_stats_property(&payload
,
7836 INTEL_PORT_STATS_RFC2819
,
7840 error
= OFPPROP_UNKNOWN(true, "port stats", type
);
7852 /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
7855 ofputil_count_port_stats(const struct ofp_header
*oh
)
7857 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
7858 ofpraw_pull_assert(&b
);
7860 for (size_t n
= 0; ; n
++) {
7861 struct ofputil_port_stats ps
;
7862 if (ofputil_decode_port_stats(&ps
, &b
)) {
7868 /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
7869 * ofputil_port_stats in 'ps'.
7871 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
7872 * message. Calling this function multiple times for a single 'msg' iterates
7873 * through the replies. The caller must initially leave 'msg''s layer pointers
7874 * null and not modify them between calls.
7876 * Returns 0 if successful, EOF if no replies were left in this 'msg',
7877 * otherwise a positive errno value. */
7879 ofputil_decode_port_stats(struct ofputil_port_stats
*ps
, struct ofpbuf
*msg
)
7884 memset(&(ps
->stats
), 0xFF, sizeof (ps
->stats
));
7886 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
7887 : ofpraw_pull(&raw
, msg
));
7894 } else if (raw
== OFPRAW_OFPST14_PORT_REPLY
) {
7895 return ofputil_pull_ofp14_port_stats(ps
, msg
);
7896 } else if (raw
== OFPRAW_OFPST13_PORT_REPLY
) {
7897 const struct ofp13_port_stats
*ps13
;
7898 ps13
= ofpbuf_try_pull(msg
, sizeof *ps13
);
7902 return ofputil_port_stats_from_ofp13(ps
, ps13
);
7903 } else if (raw
== OFPRAW_OFPST11_PORT_REPLY
) {
7904 const struct ofp11_port_stats
*ps11
;
7906 ps11
= ofpbuf_try_pull(msg
, sizeof *ps11
);
7910 return ofputil_port_stats_from_ofp11(ps
, ps11
);
7911 } else if (raw
== OFPRAW_OFPST10_PORT_REPLY
) {
7912 const struct ofp10_port_stats
*ps10
;
7914 ps10
= ofpbuf_try_pull(msg
, sizeof *ps10
);
7918 return ofputil_port_stats_from_ofp10(ps
, ps10
);
7924 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_PORT reply has %"PRIu32
" leftover "
7925 "bytes at end", msg
->size
);
7926 return OFPERR_OFPBRC_BAD_LEN
;
7929 /* Parse a port status request message into a 16 bit OpenFlow 1.0
7930 * port number and stores the latter in '*ofp10_port'.
7931 * Returns 0 if successful, otherwise an OFPERR_* number. */
7933 ofputil_decode_port_stats_request(const struct ofp_header
*request
,
7934 ofp_port_t
*ofp10_port
)
7936 switch ((enum ofp_version
)request
->version
) {
7942 case OFP11_VERSION
: {
7943 const struct ofp11_port_stats_request
*psr11
= ofpmsg_body(request
);
7944 return ofputil_port_from_ofp11(psr11
->port_no
, ofp10_port
);
7947 case OFP10_VERSION
: {
7948 const struct ofp10_port_stats_request
*psr10
= ofpmsg_body(request
);
7949 *ofp10_port
= u16_to_ofp(ntohs(psr10
->port_no
));
7959 ofputil_ipfix_stats_to_reply(const struct ofputil_ipfix_stats
*ois
,
7960 struct nx_ipfix_stats_reply
*reply
)
7962 reply
->collector_set_id
= htonl(ois
->collector_set_id
);
7963 reply
->total_flows
= htonll(ois
->total_flows
);
7964 reply
->current_flows
= htonll(ois
->current_flows
);
7965 reply
->pkts
= htonll(ois
->pkts
);
7966 reply
->ipv4_pkts
= htonll(ois
->ipv4_pkts
);
7967 reply
->ipv6_pkts
= htonll(ois
->ipv6_pkts
);
7968 reply
->error_pkts
= htonll(ois
->error_pkts
);
7969 reply
->ipv4_error_pkts
= htonll(ois
->ipv4_error_pkts
);
7970 reply
->ipv6_error_pkts
= htonll(ois
->ipv6_error_pkts
);
7971 reply
->tx_pkts
= htonll(ois
->tx_pkts
);
7972 reply
->tx_errors
= htonll(ois
->tx_errors
);
7973 memset(reply
->pad
, 0, sizeof reply
->pad
);
7976 /* Encode a ipfix stat for 'ois' and append it to 'replies'. */
7978 ofputil_append_ipfix_stat(struct ovs_list
*replies
,
7979 const struct ofputil_ipfix_stats
*ois
)
7981 struct nx_ipfix_stats_reply
*reply
= ofpmp_append(replies
, sizeof *reply
);
7982 ofputil_ipfix_stats_to_reply(ois
, reply
);
7986 ofputil_ipfix_stats_from_nx(struct ofputil_ipfix_stats
*is
,
7987 const struct nx_ipfix_stats_reply
*reply
)
7989 is
->collector_set_id
= ntohl(reply
->collector_set_id
);
7990 is
->total_flows
= ntohll(reply
->total_flows
);
7991 is
->current_flows
= ntohll(reply
->current_flows
);
7992 is
->pkts
= ntohll(reply
->pkts
);
7993 is
->ipv4_pkts
= ntohll(reply
->ipv4_pkts
);
7994 is
->ipv6_pkts
= ntohll(reply
->ipv6_pkts
);
7995 is
->error_pkts
= ntohll(reply
->error_pkts
);
7996 is
->ipv4_error_pkts
= ntohll(reply
->ipv4_error_pkts
);
7997 is
->ipv6_error_pkts
= ntohll(reply
->ipv6_error_pkts
);
7998 is
->tx_pkts
= ntohll(reply
->tx_pkts
);
7999 is
->tx_errors
= ntohll(reply
->tx_errors
);
8005 ofputil_pull_ipfix_stats(struct ofputil_ipfix_stats
*is
, struct ofpbuf
*msg
)
8010 memset(is
, 0xFF, sizeof (*is
));
8012 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
8013 : ofpraw_pull(&raw
, msg
));
8020 } else if (raw
== OFPRAW_NXST_IPFIX_BRIDGE_REPLY
||
8021 raw
== OFPRAW_NXST_IPFIX_FLOW_REPLY
) {
8022 struct nx_ipfix_stats_reply
*reply
;
8024 reply
= ofpbuf_try_pull(msg
, sizeof *reply
);
8025 return ofputil_ipfix_stats_from_nx(is
, reply
);
8032 /* Returns the number of ipfix stats elements in
8033 * OFPTYPE_IPFIX_BRIDGE_STATS_REPLY or OFPTYPE_IPFIX_FLOW_STATS_REPLY
8036 ofputil_count_ipfix_stats(const struct ofp_header
*oh
)
8038 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
8039 ofpraw_pull_assert(&b
);
8041 return b
.size
/ sizeof(struct ofputil_ipfix_stats
);
8044 /* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */
8046 ofputil_bucket_list_destroy(struct ovs_list
*buckets
)
8048 struct ofputil_bucket
*bucket
;
8050 LIST_FOR_EACH_POP (bucket
, list_node
, buckets
) {
8051 free(bucket
->ofpacts
);
8056 /* Clones 'bucket' and its ofpacts data */
8057 static struct ofputil_bucket
*
8058 ofputil_bucket_clone_data(const struct ofputil_bucket
*bucket
)
8060 struct ofputil_bucket
*new;
8062 new = xmemdup(bucket
, sizeof *bucket
);
8063 new->ofpacts
= xmemdup(bucket
->ofpacts
, bucket
->ofpacts_len
);
8068 /* Clones each of the buckets in the list 'src' appending them
8069 * in turn to 'dest' which should be an initialised list.
8070 * An exception is that if the pointer value of a bucket in 'src'
8071 * matches 'skip' then it is not cloned or appended to 'dest'.
8072 * This allows all of 'src' or 'all of 'src' except 'skip' to
8073 * be cloned and appended to 'dest'. */
8075 ofputil_bucket_clone_list(struct ovs_list
*dest
, const struct ovs_list
*src
,
8076 const struct ofputil_bucket
*skip
)
8078 struct ofputil_bucket
*bucket
;
8080 LIST_FOR_EACH (bucket
, list_node
, src
) {
8081 struct ofputil_bucket
*new_bucket
;
8083 if (bucket
== skip
) {
8087 new_bucket
= ofputil_bucket_clone_data(bucket
);
8088 ovs_list_push_back(dest
, &new_bucket
->list_node
);
8092 /* Find a bucket in the list 'buckets' whose bucket id is 'bucket_id'
8093 * Returns the first bucket found or NULL if no buckets are found. */
8094 struct ofputil_bucket
*
8095 ofputil_bucket_find(const struct ovs_list
*buckets
, uint32_t bucket_id
)
8097 struct ofputil_bucket
*bucket
;
8099 if (bucket_id
> OFPG15_BUCKET_MAX
) {
8103 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
8104 if (bucket
->bucket_id
== bucket_id
) {
8112 /* Returns true if more than one bucket in the list 'buckets'
8113 * have the same bucket id. Returns false otherwise. */
8115 ofputil_bucket_check_duplicate_id(const struct ovs_list
*buckets
)
8117 struct ofputil_bucket
*i
, *j
;
8119 LIST_FOR_EACH (i
, list_node
, buckets
) {
8120 LIST_FOR_EACH_REVERSE (j
, list_node
, buckets
) {
8124 if (i
->bucket_id
== j
->bucket_id
) {
8133 /* Returns the bucket at the front of the list 'buckets'.
8134 * Undefined if 'buckets is empty. */
8135 struct ofputil_bucket
*
8136 ofputil_bucket_list_front(const struct ovs_list
*buckets
)
8138 static struct ofputil_bucket
*bucket
;
8140 ASSIGN_CONTAINER(bucket
, ovs_list_front(buckets
), list_node
);
8145 /* Returns the bucket at the back of the list 'buckets'.
8146 * Undefined if 'buckets is empty. */
8147 struct ofputil_bucket
*
8148 ofputil_bucket_list_back(const struct ovs_list
*buckets
)
8150 static struct ofputil_bucket
*bucket
;
8152 ASSIGN_CONTAINER(bucket
, ovs_list_back(buckets
), list_node
);
8157 /* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version',
8158 * that requests stats for group 'group_id'. (Use OFPG_ALL to request stats
8161 * Group statistics include packet and byte counts for each group. */
8163 ofputil_encode_group_stats_request(enum ofp_version ofp_version
,
8166 struct ofpbuf
*request
;
8168 switch (ofp_version
) {
8170 ovs_fatal(0, "dump-group-stats needs OpenFlow 1.1 or later "
8171 "(\'-O OpenFlow11\')");
8177 case OFP16_VERSION
: {
8178 struct ofp11_group_stats_request
*req
;
8179 request
= ofpraw_alloc(OFPRAW_OFPST11_GROUP_REQUEST
, ofp_version
, 0);
8180 req
= ofpbuf_put_zeros(request
, sizeof *req
);
8181 req
->group_id
= htonl(group_id
);
8192 ofputil_uninit_group_desc(struct ofputil_group_desc
*gd
)
8194 ofputil_bucket_list_destroy(&gd
->buckets
);
8195 ofputil_group_properties_destroy(&gd
->props
);
8198 /* Decodes the OpenFlow group description request in 'oh', returning the group
8199 * whose description is requested, or OFPG_ALL if stats for all groups was
8202 ofputil_decode_group_desc_request(const struct ofp_header
*oh
)
8204 struct ofpbuf request
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
8205 enum ofpraw raw
= ofpraw_pull_assert(&request
);
8206 if (raw
== OFPRAW_OFPST11_GROUP_DESC_REQUEST
) {
8208 } else if (raw
== OFPRAW_OFPST15_GROUP_DESC_REQUEST
) {
8209 ovs_be32
*group_id
= ofpbuf_pull(&request
, sizeof *group_id
);
8210 return ntohl(*group_id
);
8216 /* Returns an OpenFlow group description request for OpenFlow version
8217 * 'ofp_version', that requests stats for group 'group_id'. Use OFPG_ALL to
8218 * request stats for all groups (OpenFlow 1.4 and earlier always request all
8221 * Group descriptions include the bucket and action configuration for each
8224 ofputil_encode_group_desc_request(enum ofp_version ofp_version
,
8227 struct ofpbuf
*request
;
8229 switch (ofp_version
) {
8231 ovs_fatal(0, "dump-groups needs OpenFlow 1.1 or later "
8232 "(\'-O OpenFlow11\')");
8237 request
= ofpraw_alloc(OFPRAW_OFPST11_GROUP_DESC_REQUEST
,
8241 case OFP16_VERSION
: {
8242 struct ofp15_group_desc_request
*req
;
8243 request
= ofpraw_alloc(OFPRAW_OFPST15_GROUP_DESC_REQUEST
,
8245 req
= ofpbuf_put_zeros(request
, sizeof *req
);
8246 req
->group_id
= htonl(group_id
);
8257 ofputil_group_bucket_counters_to_ofp11(const struct ofputil_group_stats
*gs
,
8258 struct ofp11_bucket_counter bucket_cnts
[])
8262 for (i
= 0; i
< gs
->n_buckets
; i
++) {
8263 bucket_cnts
[i
].packet_count
= htonll(gs
->bucket_stats
[i
].packet_count
);
8264 bucket_cnts
[i
].byte_count
= htonll(gs
->bucket_stats
[i
].byte_count
);
8269 ofputil_group_stats_to_ofp11(const struct ofputil_group_stats
*gs
,
8270 struct ofp11_group_stats
*gs11
, size_t length
,
8271 struct ofp11_bucket_counter bucket_cnts
[])
8273 memset(gs11
, 0, sizeof *gs11
);
8274 gs11
->length
= htons(length
);
8275 gs11
->group_id
= htonl(gs
->group_id
);
8276 gs11
->ref_count
= htonl(gs
->ref_count
);
8277 gs11
->packet_count
= htonll(gs
->packet_count
);
8278 gs11
->byte_count
= htonll(gs
->byte_count
);
8279 ofputil_group_bucket_counters_to_ofp11(gs
, bucket_cnts
);
8283 ofputil_group_stats_to_ofp13(const struct ofputil_group_stats
*gs
,
8284 struct ofp13_group_stats
*gs13
, size_t length
,
8285 struct ofp11_bucket_counter bucket_cnts
[])
8287 ofputil_group_stats_to_ofp11(gs
, &gs13
->gs
, length
, bucket_cnts
);
8288 gs13
->duration_sec
= htonl(gs
->duration_sec
);
8289 gs13
->duration_nsec
= htonl(gs
->duration_nsec
);
8293 /* Encodes 'gs' properly for the format of the list of group statistics
8294 * replies already begun in 'replies' and appends it to the list. 'replies'
8295 * must have originally been initialized with ofpmp_init(). */
8297 ofputil_append_group_stats(struct ovs_list
*replies
,
8298 const struct ofputil_group_stats
*gs
)
8300 size_t bucket_counter_size
;
8301 struct ofp11_bucket_counter
*bucket_counters
;
8304 bucket_counter_size
= gs
->n_buckets
* sizeof(struct ofp11_bucket_counter
);
8306 switch (ofpmp_version(replies
)) {
8308 case OFP12_VERSION
:{
8309 struct ofp11_group_stats
*gs11
;
8311 length
= sizeof *gs11
+ bucket_counter_size
;
8312 gs11
= ofpmp_append(replies
, length
);
8313 bucket_counters
= (struct ofp11_bucket_counter
*)(gs11
+ 1);
8314 ofputil_group_stats_to_ofp11(gs
, gs11
, length
, bucket_counters
);
8321 case OFP16_VERSION
: {
8322 struct ofp13_group_stats
*gs13
;
8324 length
= sizeof *gs13
+ bucket_counter_size
;
8325 gs13
= ofpmp_append(replies
, length
);
8326 bucket_counters
= (struct ofp11_bucket_counter
*)(gs13
+ 1);
8327 ofputil_group_stats_to_ofp13(gs
, gs13
, length
, bucket_counters
);
8336 /* Returns an OpenFlow group features request for OpenFlow version
8339 ofputil_encode_group_features_request(enum ofp_version ofp_version
)
8341 struct ofpbuf
*request
= NULL
;
8343 switch (ofp_version
) {
8346 ovs_fatal(0, "dump-group-features needs OpenFlow 1.2 or later "
8347 "(\'-O OpenFlow12\')");
8353 request
= ofpraw_alloc(OFPRAW_OFPST12_GROUP_FEATURES_REQUEST
,
8363 /* Returns a OpenFlow message that encodes 'features' properly as a reply to
8364 * group features request 'request'. */
8366 ofputil_encode_group_features_reply(
8367 const struct ofputil_group_features
*features
,
8368 const struct ofp_header
*request
)
8370 struct ofp12_group_features_stats
*ogf
;
8371 struct ofpbuf
*reply
;
8374 reply
= ofpraw_alloc_xid(OFPRAW_OFPST12_GROUP_FEATURES_REPLY
,
8375 request
->version
, request
->xid
, 0);
8376 ogf
= ofpbuf_put_zeros(reply
, sizeof *ogf
);
8377 ogf
->types
= htonl(features
->types
);
8378 ogf
->capabilities
= htonl(features
->capabilities
);
8379 for (i
= 0; i
< OFPGT12_N_TYPES
; i
++) {
8380 ogf
->max_groups
[i
] = htonl(features
->max_groups
[i
]);
8381 ogf
->actions
[i
] = ofpact_bitmap_to_openflow(features
->ofpacts
[i
],
8388 /* Decodes group features reply 'oh' into 'features'. */
8390 ofputil_decode_group_features_reply(const struct ofp_header
*oh
,
8391 struct ofputil_group_features
*features
)
8393 const struct ofp12_group_features_stats
*ogf
= ofpmsg_body(oh
);
8396 features
->types
= ntohl(ogf
->types
);
8397 features
->capabilities
= ntohl(ogf
->capabilities
);
8398 for (i
= 0; i
< OFPGT12_N_TYPES
; i
++) {
8399 features
->max_groups
[i
] = ntohl(ogf
->max_groups
[i
]);
8400 features
->ofpacts
[i
] = ofpact_bitmap_from_openflow(
8401 ogf
->actions
[i
], oh
->version
);
8405 /* Parse a group status request message into a 32 bit OpenFlow 1.1
8406 * group ID and stores the latter in '*group_id'.
8407 * Returns 0 if successful, otherwise an OFPERR_* number. */
8409 ofputil_decode_group_stats_request(const struct ofp_header
*request
,
8412 const struct ofp11_group_stats_request
*gsr11
= ofpmsg_body(request
);
8413 *group_id
= ntohl(gsr11
->group_id
);
8417 /* Converts a group stats reply in 'msg' into an abstract ofputil_group_stats
8418 * in 'gs'. Assigns freshly allocated memory to gs->bucket_stats for the
8419 * caller to eventually free.
8421 * Multiple group stats replies can be packed into a single OpenFlow message.
8422 * Calling this function multiple times for a single 'msg' iterates through the
8423 * replies. The caller must initially leave 'msg''s layer pointers null and
8424 * not modify them between calls.
8426 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8427 * otherwise a positive errno value. */
8429 ofputil_decode_group_stats_reply(struct ofpbuf
*msg
,
8430 struct ofputil_group_stats
*gs
)
8432 struct ofp11_bucket_counter
*obc
;
8433 struct ofp11_group_stats
*ogs11
;
8440 gs
->bucket_stats
= NULL
;
8441 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
8442 : ofpraw_pull(&raw
, msg
));
8451 if (raw
== OFPRAW_OFPST11_GROUP_REPLY
) {
8452 base_len
= sizeof *ogs11
;
8453 ogs11
= ofpbuf_try_pull(msg
, sizeof *ogs11
);
8454 gs
->duration_sec
= gs
->duration_nsec
= UINT32_MAX
;
8455 } else if (raw
== OFPRAW_OFPST13_GROUP_REPLY
) {
8456 struct ofp13_group_stats
*ogs13
;
8458 base_len
= sizeof *ogs13
;
8459 ogs13
= ofpbuf_try_pull(msg
, sizeof *ogs13
);
8462 gs
->duration_sec
= ntohl(ogs13
->duration_sec
);
8463 gs
->duration_nsec
= ntohl(ogs13
->duration_nsec
);
8472 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s reply has %"PRIu32
" leftover bytes at end",
8473 ofpraw_get_name(raw
), msg
->size
);
8474 return OFPERR_OFPBRC_BAD_LEN
;
8476 length
= ntohs(ogs11
->length
);
8477 if (length
< sizeof base_len
) {
8478 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s reply claims invalid length %"PRIuSIZE
,
8479 ofpraw_get_name(raw
), length
);
8480 return OFPERR_OFPBRC_BAD_LEN
;
8483 gs
->group_id
= ntohl(ogs11
->group_id
);
8484 gs
->ref_count
= ntohl(ogs11
->ref_count
);
8485 gs
->packet_count
= ntohll(ogs11
->packet_count
);
8486 gs
->byte_count
= ntohll(ogs11
->byte_count
);
8488 gs
->n_buckets
= (length
- base_len
) / sizeof *obc
;
8489 obc
= ofpbuf_try_pull(msg
, gs
->n_buckets
* sizeof *obc
);
8491 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s reply has %"PRIu32
" leftover bytes at end",
8492 ofpraw_get_name(raw
), msg
->size
);
8493 return OFPERR_OFPBRC_BAD_LEN
;
8496 gs
->bucket_stats
= xmalloc(gs
->n_buckets
* sizeof *gs
->bucket_stats
);
8497 for (i
= 0; i
< gs
->n_buckets
; i
++) {
8498 gs
->bucket_stats
[i
].packet_count
= ntohll(obc
[i
].packet_count
);
8499 gs
->bucket_stats
[i
].byte_count
= ntohll(obc
[i
].byte_count
);
8506 ofputil_put_ofp11_bucket(const struct ofputil_bucket
*bucket
,
8507 struct ofpbuf
*openflow
, enum ofp_version ofp_version
)
8509 struct ofp11_bucket
*ob
;
8512 start
= openflow
->size
;
8513 ofpbuf_put_zeros(openflow
, sizeof *ob
);
8514 ofpacts_put_openflow_actions(bucket
->ofpacts
, bucket
->ofpacts_len
,
8515 openflow
, ofp_version
);
8516 ob
= ofpbuf_at_assert(openflow
, start
, sizeof *ob
);
8517 ob
->len
= htons(openflow
->size
- start
);
8518 ob
->weight
= htons(bucket
->weight
);
8519 ob
->watch_port
= ofputil_port_to_ofp11(bucket
->watch_port
);
8520 ob
->watch_group
= htonl(bucket
->watch_group
);
8524 ofputil_put_ofp15_bucket(const struct ofputil_bucket
*bucket
,
8525 uint32_t bucket_id
, enum ofp11_group_type group_type
,
8526 struct ofpbuf
*openflow
, enum ofp_version ofp_version
)
8528 struct ofp15_bucket
*ob
;
8529 size_t start
, actions_start
, actions_len
;
8531 start
= openflow
->size
;
8532 ofpbuf_put_zeros(openflow
, sizeof *ob
);
8534 actions_start
= openflow
->size
;
8535 ofpacts_put_openflow_actions(bucket
->ofpacts
, bucket
->ofpacts_len
,
8536 openflow
, ofp_version
);
8537 actions_len
= openflow
->size
- actions_start
;
8539 if (group_type
== OFPGT11_SELECT
) {
8540 ofpprop_put_u16(openflow
, OFPGBPT15_WEIGHT
, bucket
->weight
);
8542 if (bucket
->watch_port
!= OFPP_ANY
) {
8543 ofpprop_put_be32(openflow
, OFPGBPT15_WATCH_PORT
,
8544 ofputil_port_to_ofp11(bucket
->watch_port
));
8546 if (bucket
->watch_group
!= OFPG_ANY
) {
8547 ofpprop_put_u32(openflow
, OFPGBPT15_WATCH_GROUP
, bucket
->watch_group
);
8550 ob
= ofpbuf_at_assert(openflow
, start
, sizeof *ob
);
8551 ob
->len
= htons(openflow
->size
- start
);
8552 ob
->action_array_len
= htons(actions_len
);
8553 ob
->bucket_id
= htonl(bucket_id
);
8557 ofputil_put_group_prop_ntr_selection_method(enum ofp_version ofp_version
,
8558 const struct ofputil_group_props
*gp
,
8559 struct ofpbuf
*openflow
)
8561 struct ntr_group_prop_selection_method
*prop
;
8564 start
= openflow
->size
;
8565 ofpbuf_put_zeros(openflow
, sizeof *prop
);
8566 oxm_put_field_array(openflow
, &gp
->fields
, ofp_version
);
8567 prop
= ofpbuf_at_assert(openflow
, start
, sizeof *prop
);
8568 prop
->type
= htons(OFPGPT15_EXPERIMENTER
);
8569 prop
->experimenter
= htonl(NTR_VENDOR_ID
);
8570 prop
->exp_type
= htonl(NTRT_SELECTION_METHOD
);
8571 strcpy(prop
->selection_method
, gp
->selection_method
);
8572 prop
->selection_method_param
= htonll(gp
->selection_method_param
);
8573 ofpprop_end(openflow
, start
);
8577 ofputil_append_ofp11_group_desc_reply(const struct ofputil_group_desc
*gds
,
8578 const struct ovs_list
*buckets
,
8579 struct ovs_list
*replies
,
8580 enum ofp_version version
)
8582 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
8583 struct ofp11_group_desc_stats
*ogds
;
8584 struct ofputil_bucket
*bucket
;
8587 start_ogds
= reply
->size
;
8588 ofpbuf_put_zeros(reply
, sizeof *ogds
);
8589 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
8590 ofputil_put_ofp11_bucket(bucket
, reply
, version
);
8592 ogds
= ofpbuf_at_assert(reply
, start_ogds
, sizeof *ogds
);
8593 ogds
->length
= htons(reply
->size
- start_ogds
);
8594 ogds
->type
= gds
->type
;
8595 ogds
->group_id
= htonl(gds
->group_id
);
8597 ofpmp_postappend(replies
, start_ogds
);
8601 ofputil_append_ofp15_group_desc_reply(const struct ofputil_group_desc
*gds
,
8602 const struct ovs_list
*buckets
,
8603 struct ovs_list
*replies
,
8604 enum ofp_version version
)
8606 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
8607 struct ofp15_group_desc_stats
*ogds
;
8608 struct ofputil_bucket
*bucket
;
8609 size_t start_ogds
, start_buckets
;
8611 start_ogds
= reply
->size
;
8612 ofpbuf_put_zeros(reply
, sizeof *ogds
);
8613 start_buckets
= reply
->size
;
8614 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
8615 ofputil_put_ofp15_bucket(bucket
, bucket
->bucket_id
,
8616 gds
->type
, reply
, version
);
8618 ogds
= ofpbuf_at_assert(reply
, start_ogds
, sizeof *ogds
);
8619 ogds
->type
= gds
->type
;
8620 ogds
->group_id
= htonl(gds
->group_id
);
8621 ogds
->bucket_list_len
= htons(reply
->size
- start_buckets
);
8623 /* Add group properties */
8624 if (gds
->props
.selection_method
[0]) {
8625 ofputil_put_group_prop_ntr_selection_method(version
, &gds
->props
,
8628 ogds
= ofpbuf_at_assert(reply
, start_ogds
, sizeof *ogds
);
8629 ogds
->length
= htons(reply
->size
- start_ogds
);
8631 ofpmp_postappend(replies
, start_ogds
);
8634 /* Appends a group stats reply that contains the data in 'gds' to those already
8635 * present in the list of ofpbufs in 'replies'. 'replies' should have been
8636 * initialized with ofpmp_init(). */
8638 ofputil_append_group_desc_reply(const struct ofputil_group_desc
*gds
,
8639 const struct ovs_list
*buckets
,
8640 struct ovs_list
*replies
)
8642 enum ofp_version version
= ofpmp_version(replies
);
8650 ofputil_append_ofp11_group_desc_reply(gds
, buckets
, replies
, version
);
8655 ofputil_append_ofp15_group_desc_reply(gds
, buckets
, replies
, version
);
8665 ofputil_pull_ofp11_buckets(struct ofpbuf
*msg
, size_t buckets_length
,
8666 enum ofp_version version
, struct ovs_list
*buckets
)
8668 struct ofp11_bucket
*ob
;
8669 uint32_t bucket_id
= 0;
8671 ovs_list_init(buckets
);
8672 while (buckets_length
> 0) {
8673 struct ofputil_bucket
*bucket
;
8674 struct ofpbuf ofpacts
;
8678 ob
= (buckets_length
>= sizeof *ob
8679 ? ofpbuf_try_pull(msg
, sizeof *ob
)
8682 VLOG_WARN_RL(&bad_ofmsg_rl
, "buckets end with %"PRIuSIZE
" leftover bytes",
8684 return OFPERR_OFPGMFC_BAD_BUCKET
;
8687 ob_len
= ntohs(ob
->len
);
8688 if (ob_len
< sizeof *ob
) {
8689 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket length "
8690 "%"PRIuSIZE
" is not valid", ob_len
);
8691 return OFPERR_OFPGMFC_BAD_BUCKET
;
8692 } else if (ob_len
> buckets_length
) {
8693 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket length "
8694 "%"PRIuSIZE
" exceeds remaining buckets data size %"PRIuSIZE
,
8695 ob_len
, buckets_length
);
8696 return OFPERR_OFPGMFC_BAD_BUCKET
;
8698 buckets_length
-= ob_len
;
8700 ofpbuf_init(&ofpacts
, 0);
8701 error
= ofpacts_pull_openflow_actions(msg
, ob_len
- sizeof *ob
,
8704 ofpbuf_uninit(&ofpacts
);
8705 ofputil_bucket_list_destroy(buckets
);
8709 bucket
= xzalloc(sizeof *bucket
);
8710 bucket
->weight
= ntohs(ob
->weight
);
8711 error
= ofputil_port_from_ofp11(ob
->watch_port
, &bucket
->watch_port
);
8713 ofpbuf_uninit(&ofpacts
);
8714 ofputil_bucket_list_destroy(buckets
);
8715 return OFPERR_OFPGMFC_BAD_WATCH
;
8717 bucket
->watch_group
= ntohl(ob
->watch_group
);
8718 bucket
->bucket_id
= bucket_id
++;
8720 bucket
->ofpacts
= ofpbuf_steal_data(&ofpacts
);
8721 bucket
->ofpacts_len
= ofpacts
.size
;
8722 ovs_list_push_back(buckets
, &bucket
->list_node
);
8729 ofputil_pull_ofp15_buckets(struct ofpbuf
*msg
, size_t buckets_length
,
8730 enum ofp_version version
, uint8_t group_type
,
8731 struct ovs_list
*buckets
)
8733 struct ofp15_bucket
*ob
;
8735 ovs_list_init(buckets
);
8736 while (buckets_length
> 0) {
8737 struct ofputil_bucket
*bucket
= NULL
;
8738 struct ofpbuf ofpacts
;
8739 enum ofperr err
= OFPERR_OFPGMFC_BAD_BUCKET
;
8740 size_t ob_len
, actions_len
, properties_len
;
8741 ovs_be32 watch_port
= ofputil_port_to_ofp11(OFPP_ANY
);
8742 ovs_be32 watch_group
= htonl(OFPG_ANY
);
8743 ovs_be16 weight
= htons(group_type
== OFPGT11_SELECT
? 1 : 0);
8745 ofpbuf_init(&ofpacts
, 0);
8747 ob
= ofpbuf_try_pull(msg
, sizeof *ob
);
8749 VLOG_WARN_RL(&bad_ofmsg_rl
, "buckets end with %"PRIuSIZE
8750 " leftover bytes", buckets_length
);
8754 ob_len
= ntohs(ob
->len
);
8755 actions_len
= ntohs(ob
->action_array_len
);
8757 if (ob_len
< sizeof *ob
) {
8758 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket length "
8759 "%"PRIuSIZE
" is not valid", ob_len
);
8761 } else if (ob_len
> buckets_length
) {
8762 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket length "
8763 "%"PRIuSIZE
" exceeds remaining buckets data size %"
8764 PRIuSIZE
, ob_len
, buckets_length
);
8766 } else if (actions_len
> ob_len
- sizeof *ob
) {
8767 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket actions "
8768 "length %"PRIuSIZE
" exceeds remaining bucket "
8769 "data size %"PRIuSIZE
, actions_len
,
8770 ob_len
- sizeof *ob
);
8773 buckets_length
-= ob_len
;
8775 err
= ofpacts_pull_openflow_actions(msg
, actions_len
, version
,
8781 properties_len
= ob_len
- sizeof *ob
- actions_len
;
8782 struct ofpbuf properties
= ofpbuf_const_initializer(
8783 ofpbuf_pull(msg
, properties_len
), properties_len
);
8784 while (properties
.size
> 0) {
8785 struct ofpbuf payload
;
8788 err
= ofpprop_pull(&properties
, &payload
, &type
);
8794 case OFPGBPT15_WEIGHT
:
8795 err
= ofpprop_parse_be16(&payload
, &weight
);
8798 case OFPGBPT15_WATCH_PORT
:
8799 err
= ofpprop_parse_be32(&payload
, &watch_port
);
8802 case OFPGBPT15_WATCH_GROUP
:
8803 err
= ofpprop_parse_be32(&payload
, &watch_group
);
8807 err
= OFPPROP_UNKNOWN(false, "group bucket", type
);
8816 bucket
= xzalloc(sizeof *bucket
);
8818 bucket
->weight
= ntohs(weight
);
8819 err
= ofputil_port_from_ofp11(watch_port
, &bucket
->watch_port
);
8821 err
= OFPERR_OFPGMFC_BAD_WATCH
;
8824 bucket
->watch_group
= ntohl(watch_group
);
8825 bucket
->bucket_id
= ntohl(ob
->bucket_id
);
8826 if (bucket
->bucket_id
> OFPG15_BUCKET_MAX
) {
8827 VLOG_WARN_RL(&bad_ofmsg_rl
, "bucket id (%u) is out of range",
8829 err
= OFPERR_OFPGMFC_BAD_BUCKET
;
8833 bucket
->ofpacts
= ofpbuf_steal_data(&ofpacts
);
8834 bucket
->ofpacts_len
= ofpacts
.size
;
8835 ovs_list_push_back(buckets
, &bucket
->list_node
);
8841 ofpbuf_uninit(&ofpacts
);
8842 ofputil_bucket_list_destroy(buckets
);
8846 if (ofputil_bucket_check_duplicate_id(buckets
)) {
8847 VLOG_WARN_RL(&bad_ofmsg_rl
, "Duplicate bucket id");
8848 ofputil_bucket_list_destroy(buckets
);
8849 return OFPERR_OFPGMFC_BAD_BUCKET
;
8856 ofputil_init_group_properties(struct ofputil_group_props
*gp
)
8858 memset(gp
, 0, sizeof *gp
);
8862 ofputil_group_properties_copy(struct ofputil_group_props
*to
,
8863 const struct ofputil_group_props
*from
)
8866 to
->fields
.values
= xmemdup(from
->fields
.values
, from
->fields
.values_size
);
8870 ofputil_group_properties_destroy(struct ofputil_group_props
*gp
)
8872 free(gp
->fields
.values
);
8876 parse_group_prop_ntr_selection_method(struct ofpbuf
*payload
,
8877 enum ofp11_group_type group_type
,
8878 enum ofp15_group_mod_command group_cmd
,
8879 struct ofputil_group_props
*gp
)
8881 struct ntr_group_prop_selection_method
*prop
= payload
->data
;
8882 size_t fields_len
, method_len
;
8885 switch (group_type
) {
8886 case OFPGT11_SELECT
:
8889 case OFPGT11_INDIRECT
:
8891 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method property is "
8892 "only allowed for select groups");
8893 return OFPERR_OFPBPC_BAD_VALUE
;
8898 switch (group_cmd
) {
8900 case OFPGC15_MODIFY
:
8901 case OFPGC15_ADD_OR_MOD
:
8903 case OFPGC15_DELETE
:
8904 case OFPGC15_INSERT_BUCKET
:
8905 case OFPGC15_REMOVE_BUCKET
:
8906 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method property is "
8907 "only allowed for add and delete group modifications");
8908 return OFPERR_OFPBPC_BAD_VALUE
;
8913 if (payload
->size
< sizeof *prop
) {
8914 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method property "
8915 "length %u is not valid", payload
->size
);
8916 return OFPERR_OFPBPC_BAD_LEN
;
8919 method_len
= strnlen(prop
->selection_method
, NTR_MAX_SELECTION_METHOD_LEN
);
8921 if (method_len
== NTR_MAX_SELECTION_METHOD_LEN
) {
8922 OFPPROP_LOG(&bad_ofmsg_rl
, false,
8923 "ntr selection method is not null terminated");
8924 return OFPERR_OFPBPC_BAD_VALUE
;
8927 if (strcmp("hash", prop
->selection_method
)
8928 && strcmp("dp_hash", prop
->selection_method
)) {
8929 OFPPROP_LOG(&bad_ofmsg_rl
, false,
8930 "ntr selection method '%s' is not supported",
8931 prop
->selection_method
);
8932 return OFPERR_OFPBPC_BAD_VALUE
;
8934 /* 'method_len' is now non-zero. */
8936 strcpy(gp
->selection_method
, prop
->selection_method
);
8937 gp
->selection_method_param
= ntohll(prop
->selection_method_param
);
8939 ofpbuf_pull(payload
, sizeof *prop
);
8941 fields_len
= ntohs(prop
->length
) - sizeof *prop
;
8942 if (fields_len
&& strcmp("hash", gp
->selection_method
)) {
8943 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method %s "
8944 "does not support fields", gp
->selection_method
);
8945 return OFPERR_OFPBPC_BAD_VALUE
;
8948 error
= oxm_pull_field_array(payload
->data
, fields_len
,
8951 OFPPROP_LOG(&bad_ofmsg_rl
, false,
8952 "ntr selection method fields are invalid");
8960 parse_ofp15_group_properties(struct ofpbuf
*msg
,
8961 enum ofp11_group_type group_type
,
8962 enum ofp15_group_mod_command group_cmd
,
8963 struct ofputil_group_props
*gp
,
8964 size_t properties_len
)
8966 struct ofpbuf properties
= ofpbuf_const_initializer(
8967 ofpbuf_pull(msg
, properties_len
), properties_len
);
8968 while (properties
.size
> 0) {
8969 struct ofpbuf payload
;
8973 error
= ofpprop_pull(&properties
, &payload
, &type
);
8979 case OFPPROP_EXP(NTR_VENDOR_ID
, NTRT_SELECTION_METHOD
):
8980 case OFPPROP_EXP(NTR_COMPAT_VENDOR_ID
, NTRT_SELECTION_METHOD
):
8981 error
= parse_group_prop_ntr_selection_method(&payload
, group_type
,
8986 error
= OFPPROP_UNKNOWN(false, "group", type
);
8999 ofputil_decode_ofp11_group_desc_reply(struct ofputil_group_desc
*gd
,
9001 enum ofp_version version
)
9003 struct ofp11_group_desc_stats
*ogds
;
9007 ofpraw_pull_assert(msg
);
9014 ogds
= ofpbuf_try_pull(msg
, sizeof *ogds
);
9016 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply has %"PRIu32
" "
9017 "leftover bytes at end", msg
->size
);
9018 return OFPERR_OFPBRC_BAD_LEN
;
9020 gd
->type
= ogds
->type
;
9021 gd
->group_id
= ntohl(ogds
->group_id
);
9023 length
= ntohs(ogds
->length
);
9024 if (length
< sizeof *ogds
|| length
- sizeof *ogds
> msg
->size
) {
9025 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply claims invalid "
9026 "length %"PRIuSIZE
, length
);
9027 return OFPERR_OFPBRC_BAD_LEN
;
9030 return ofputil_pull_ofp11_buckets(msg
, length
- sizeof *ogds
, version
,
9035 ofputil_decode_ofp15_group_desc_reply(struct ofputil_group_desc
*gd
,
9037 enum ofp_version version
)
9039 struct ofp15_group_desc_stats
*ogds
;
9040 uint16_t length
, bucket_list_len
;
9044 ofpraw_pull_assert(msg
);
9051 ogds
= ofpbuf_try_pull(msg
, sizeof *ogds
);
9053 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply has %"PRIu32
" "
9054 "leftover bytes at end", msg
->size
);
9055 return OFPERR_OFPBRC_BAD_LEN
;
9057 gd
->type
= ogds
->type
;
9058 gd
->group_id
= ntohl(ogds
->group_id
);
9060 length
= ntohs(ogds
->length
);
9061 if (length
< sizeof *ogds
|| length
- sizeof *ogds
> msg
->size
) {
9062 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply claims invalid "
9063 "length %u", length
);
9064 return OFPERR_OFPBRC_BAD_LEN
;
9067 bucket_list_len
= ntohs(ogds
->bucket_list_len
);
9068 if (length
< bucket_list_len
+ sizeof *ogds
) {
9069 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply claims invalid "
9070 "bucket list length %u", bucket_list_len
);
9071 return OFPERR_OFPBRC_BAD_LEN
;
9073 error
= ofputil_pull_ofp15_buckets(msg
, bucket_list_len
, version
, gd
->type
,
9079 /* By definition group desc messages don't have a group mod command.
9080 * However, parse_group_prop_ntr_selection_method() checks to make sure
9081 * that the command is OFPGC15_ADD or OFPGC15_DELETE to guard
9082 * against group mod messages with other commands supplying
9083 * a NTR selection method group experimenter property.
9084 * Such properties are valid for group desc replies so
9085 * claim that the group mod command is OFPGC15_ADD to
9086 * satisfy the check in parse_group_prop_ntr_selection_method() */
9087 return parse_ofp15_group_properties(msg
, gd
->type
, OFPGC15_ADD
, &gd
->props
,
9088 length
- sizeof *ogds
- bucket_list_len
);
9091 /* Converts a group description reply in 'msg' into an abstract
9092 * ofputil_group_desc in 'gd'.
9094 * Multiple group description replies can be packed into a single OpenFlow
9095 * message. Calling this function multiple times for a single 'msg' iterates
9096 * through the replies. The caller must initially leave 'msg''s layer pointers
9097 * null and not modify them between calls.
9099 * Returns 0 if successful, EOF if no replies were left in this 'msg',
9100 * otherwise a positive errno value. */
9102 ofputil_decode_group_desc_reply(struct ofputil_group_desc
*gd
,
9103 struct ofpbuf
*msg
, enum ofp_version version
)
9105 ofputil_init_group_properties(&gd
->props
);
9113 return ofputil_decode_ofp11_group_desc_reply(gd
, msg
, version
);
9117 return ofputil_decode_ofp15_group_desc_reply(gd
, msg
, version
);
9126 ofputil_uninit_group_mod(struct ofputil_group_mod
*gm
)
9128 ofputil_bucket_list_destroy(&gm
->buckets
);
9129 ofputil_group_properties_destroy(&gm
->props
);
9132 static struct ofpbuf
*
9133 ofputil_encode_ofp11_group_mod(enum ofp_version ofp_version
,
9134 const struct ofputil_group_mod
*gm
)
9137 struct ofp11_group_mod
*ogm
;
9139 struct ofputil_bucket
*bucket
;
9141 b
= ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD
, ofp_version
, 0);
9142 start_ogm
= b
->size
;
9143 ofpbuf_put_zeros(b
, sizeof *ogm
);
9145 LIST_FOR_EACH (bucket
, list_node
, &gm
->buckets
) {
9146 ofputil_put_ofp11_bucket(bucket
, b
, ofp_version
);
9148 ogm
= ofpbuf_at_assert(b
, start_ogm
, sizeof *ogm
);
9149 ogm
->command
= htons(gm
->command
);
9150 ogm
->type
= gm
->type
;
9151 ogm
->group_id
= htonl(gm
->group_id
);
9156 static struct ofpbuf
*
9157 ofputil_encode_ofp15_group_mod(enum ofp_version ofp_version
,
9158 const struct ofputil_group_mod
*gm
)
9161 struct ofp15_group_mod
*ogm
;
9163 struct ofputil_bucket
*bucket
;
9164 struct id_pool
*bucket_ids
= NULL
;
9166 b
= ofpraw_alloc(OFPRAW_OFPT15_GROUP_MOD
, ofp_version
, 0);
9167 start_ogm
= b
->size
;
9168 ofpbuf_put_zeros(b
, sizeof *ogm
);
9170 LIST_FOR_EACH (bucket
, list_node
, &gm
->buckets
) {
9173 /* Generate a bucket id if none was supplied */
9174 if (bucket
->bucket_id
> OFPG15_BUCKET_MAX
) {
9176 const struct ofputil_bucket
*bkt
;
9178 bucket_ids
= id_pool_create(0, OFPG15_BUCKET_MAX
+ 1);
9180 /* Mark all bucket_ids that are present in gm
9181 * as used in the pool. */
9182 LIST_FOR_EACH_REVERSE (bkt
, list_node
, &gm
->buckets
) {
9183 if (bkt
== bucket
) {
9186 if (bkt
->bucket_id
<= OFPG15_BUCKET_MAX
) {
9187 id_pool_add(bucket_ids
, bkt
->bucket_id
);
9192 if (!id_pool_alloc_id(bucket_ids
, &bucket_id
)) {
9196 bucket_id
= bucket
->bucket_id
;
9199 ofputil_put_ofp15_bucket(bucket
, bucket_id
, gm
->type
, b
, ofp_version
);
9201 ogm
= ofpbuf_at_assert(b
, start_ogm
, sizeof *ogm
);
9202 ogm
->command
= htons(gm
->command
);
9203 ogm
->type
= gm
->type
;
9204 ogm
->group_id
= htonl(gm
->group_id
);
9205 ogm
->command_bucket_id
= htonl(gm
->command_bucket_id
);
9206 ogm
->bucket_array_len
= htons(b
->size
- start_ogm
- sizeof *ogm
);
9208 /* Add group properties */
9209 if (gm
->props
.selection_method
[0]) {
9210 ofputil_put_group_prop_ntr_selection_method(ofp_version
, &gm
->props
, b
);
9213 id_pool_destroy(bucket_ids
);
9218 bad_group_cmd(enum ofp15_group_mod_command cmd
)
9220 const char *opt_version
;
9221 const char *version
;
9222 const char *cmd_str
;
9226 case OFPGC15_MODIFY
:
9227 case OFPGC15_ADD_OR_MOD
:
9228 case OFPGC15_DELETE
:
9233 case OFPGC15_INSERT_BUCKET
:
9234 case OFPGC15_REMOVE_BUCKET
:
9245 cmd_str
= "add-group";
9248 case OFPGC15_MODIFY
:
9249 case OFPGC15_ADD_OR_MOD
:
9250 cmd_str
= "mod-group";
9253 case OFPGC15_DELETE
:
9254 cmd_str
= "del-group";
9257 case OFPGC15_INSERT_BUCKET
:
9258 cmd_str
= "insert-bucket";
9261 case OFPGC15_REMOVE_BUCKET
:
9262 cmd_str
= "remove-bucket";
9269 ovs_fatal(0, "%s needs OpenFlow %s or later (\'-O OpenFlow%s\')",
9270 cmd_str
, version
, opt_version
);
9274 /* Converts abstract group mod 'gm' into a message for OpenFlow version
9275 * 'ofp_version' and returns the message. */
9277 ofputil_encode_group_mod(enum ofp_version ofp_version
,
9278 const struct ofputil_group_mod
*gm
)
9281 switch (ofp_version
) {
9283 bad_group_cmd(gm
->command
);
9289 if (gm
->command
> OFPGC11_DELETE
&& gm
->command
!= OFPGC11_ADD_OR_MOD
) {
9290 bad_group_cmd(gm
->command
);
9292 return ofputil_encode_ofp11_group_mod(ofp_version
, gm
);
9296 return ofputil_encode_ofp15_group_mod(ofp_version
, gm
);
9304 ofputil_pull_ofp11_group_mod(struct ofpbuf
*msg
, enum ofp_version ofp_version
,
9305 struct ofputil_group_mod
*gm
)
9307 const struct ofp11_group_mod
*ogm
;
9310 ogm
= ofpbuf_pull(msg
, sizeof *ogm
);
9311 gm
->command
= ntohs(ogm
->command
);
9312 gm
->type
= ogm
->type
;
9313 gm
->group_id
= ntohl(ogm
->group_id
);
9314 gm
->command_bucket_id
= OFPG15_BUCKET_ALL
;
9316 error
= ofputil_pull_ofp11_buckets(msg
, msg
->size
, ofp_version
,
9319 /* OF1.3.5+ prescribes an error when an OFPGC_DELETE includes buckets. */
9321 && ofp_version
>= OFP13_VERSION
9322 && gm
->command
== OFPGC11_DELETE
9323 && !ovs_list_is_empty(&gm
->buckets
)) {
9324 error
= OFPERR_OFPGMFC_INVALID_GROUP
;
9331 ofputil_pull_ofp15_group_mod(struct ofpbuf
*msg
, enum ofp_version ofp_version
,
9332 struct ofputil_group_mod
*gm
)
9334 const struct ofp15_group_mod
*ogm
;
9335 uint16_t bucket_list_len
;
9336 enum ofperr error
= OFPERR_OFPGMFC_BAD_BUCKET
;
9338 ogm
= ofpbuf_pull(msg
, sizeof *ogm
);
9339 gm
->command
= ntohs(ogm
->command
);
9340 gm
->type
= ogm
->type
;
9341 gm
->group_id
= ntohl(ogm
->group_id
);
9343 gm
->command_bucket_id
= ntohl(ogm
->command_bucket_id
);
9344 switch (gm
->command
) {
9345 case OFPGC15_REMOVE_BUCKET
:
9346 if (gm
->command_bucket_id
== OFPG15_BUCKET_ALL
) {
9350 case OFPGC15_INSERT_BUCKET
:
9351 if (gm
->command_bucket_id
<= OFPG15_BUCKET_MAX
||
9352 gm
->command_bucket_id
== OFPG15_BUCKET_FIRST
9353 || gm
->command_bucket_id
== OFPG15_BUCKET_LAST
) {
9359 case OFPGC11_MODIFY
:
9360 case OFPGC11_ADD_OR_MOD
:
9361 case OFPGC11_DELETE
:
9363 if (gm
->command_bucket_id
== OFPG15_BUCKET_ALL
) {
9369 VLOG_WARN_RL(&bad_ofmsg_rl
,
9370 "group command bucket id (%u) is out of range",
9371 gm
->command_bucket_id
);
9372 return OFPERR_OFPGMFC_BAD_BUCKET
;
9375 bucket_list_len
= ntohs(ogm
->bucket_array_len
);
9376 error
= ofputil_pull_ofp15_buckets(msg
, bucket_list_len
, ofp_version
,
9377 gm
->type
, &gm
->buckets
);
9382 return parse_ofp15_group_properties(msg
, gm
->type
, gm
->command
, &gm
->props
,
9386 /* Converts OpenFlow group mod message 'oh' into an abstract group mod in
9387 * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */
9389 ofputil_decode_group_mod(const struct ofp_header
*oh
,
9390 struct ofputil_group_mod
*gm
)
9392 ofputil_init_group_properties(&gm
->props
);
9394 enum ofp_version ofp_version
= oh
->version
;
9395 struct ofpbuf msg
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
9396 ofpraw_pull_assert(&msg
);
9399 switch (ofp_version
)
9405 err
= ofputil_pull_ofp11_group_mod(&msg
, ofp_version
, gm
);
9410 err
= ofputil_pull_ofp15_group_mod(&msg
, ofp_version
, gm
);
9422 case OFPGT11_INDIRECT
:
9423 if (gm
->command
!= OFPGC11_DELETE
9424 && !ovs_list_is_singleton(&gm
->buckets
) ) {
9425 return OFPERR_OFPGMFC_INVALID_GROUP
;
9429 case OFPGT11_SELECT
:
9433 return OFPERR_OFPGMFC_BAD_TYPE
;
9436 switch (gm
->command
) {
9438 case OFPGC11_MODIFY
:
9439 case OFPGC11_ADD_OR_MOD
:
9440 case OFPGC11_DELETE
:
9441 case OFPGC15_INSERT_BUCKET
:
9443 case OFPGC15_REMOVE_BUCKET
:
9444 if (!ovs_list_is_empty(&gm
->buckets
)) {
9445 return OFPERR_OFPGMFC_BAD_BUCKET
;
9449 return OFPERR_OFPGMFC_BAD_COMMAND
;
9452 struct ofputil_bucket
*bucket
;
9453 LIST_FOR_EACH (bucket
, list_node
, &gm
->buckets
) {
9454 if (bucket
->weight
&& gm
->type
!= OFPGT11_SELECT
) {
9455 return OFPERR_OFPGMFC_INVALID_GROUP
;
9460 case OFPGT11_INDIRECT
:
9461 if (ofputil_bucket_has_liveness(bucket
)) {
9462 return OFPERR_OFPGMFC_WATCH_UNSUPPORTED
;
9465 case OFPGT11_SELECT
:
9468 if (!ofputil_bucket_has_liveness(bucket
)) {
9469 return OFPERR_OFPGMFC_INVALID_GROUP
;
9473 /* Returning BAD TYPE to be consistent
9474 * though gm->type has been checked already. */
9475 return OFPERR_OFPGMFC_BAD_TYPE
;
9482 /* Destroys 'bms'. */
9484 ofputil_free_bundle_msgs(struct ofputil_bundle_msg
*bms
, size_t n_bms
)
9486 for (size_t i
= 0; i
< n_bms
; i
++) {
9487 switch ((int)bms
[i
].type
) {
9488 case OFPTYPE_FLOW_MOD
:
9489 free(CONST_CAST(struct ofpact
*, bms
[i
].fm
.ofpacts
));
9491 case OFPTYPE_GROUP_MOD
:
9492 ofputil_uninit_group_mod(&bms
[i
].gm
);
9494 case OFPTYPE_PACKET_OUT
:
9495 free(bms
[i
].po
.ofpacts
);
9496 free(CONST_CAST(void *, bms
[i
].po
.packet
));
9506 ofputil_encode_bundle_msgs(const struct ofputil_bundle_msg
*bms
,
9507 size_t n_bms
, struct ovs_list
*requests
,
9508 enum ofputil_protocol protocol
)
9510 enum ofp_version version
= ofputil_protocol_to_ofp_version(protocol
);
9512 for (size_t i
= 0; i
< n_bms
; i
++) {
9513 struct ofpbuf
*request
= NULL
;
9515 switch ((int)bms
[i
].type
) {
9516 case OFPTYPE_FLOW_MOD
:
9517 request
= ofputil_encode_flow_mod(&bms
[i
].fm
, protocol
);
9519 case OFPTYPE_GROUP_MOD
:
9520 request
= ofputil_encode_group_mod(version
, &bms
[i
].gm
);
9522 case OFPTYPE_PACKET_OUT
:
9523 request
= ofputil_encode_packet_out(&bms
[i
].po
, protocol
);
9529 ovs_list_push_back(requests
, &request
->list_node
);
9534 /* Parse a queue status request message into 'oqsr'.
9535 * Returns 0 if successful, otherwise an OFPERR_* number. */
9537 ofputil_decode_queue_stats_request(const struct ofp_header
*request
,
9538 struct ofputil_queue_stats_request
*oqsr
)
9540 switch ((enum ofp_version
)request
->version
) {
9546 case OFP11_VERSION
: {
9547 const struct ofp11_queue_stats_request
*qsr11
= ofpmsg_body(request
);
9548 oqsr
->queue_id
= ntohl(qsr11
->queue_id
);
9549 return ofputil_port_from_ofp11(qsr11
->port_no
, &oqsr
->port_no
);
9552 case OFP10_VERSION
: {
9553 const struct ofp10_queue_stats_request
*qsr10
= ofpmsg_body(request
);
9554 oqsr
->queue_id
= ntohl(qsr10
->queue_id
);
9555 oqsr
->port_no
= u16_to_ofp(ntohs(qsr10
->port_no
));
9556 /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
9557 if (oqsr
->port_no
== OFPP_ALL
) {
9558 oqsr
->port_no
= OFPP_ANY
;
9568 /* Encode a queue stats request for 'oqsr', the encoded message
9569 * will be for OpenFlow version 'ofp_version'. Returns message
9570 * as a struct ofpbuf. Returns encoded message on success, NULL on error. */
9572 ofputil_encode_queue_stats_request(enum ofp_version ofp_version
,
9573 const struct ofputil_queue_stats_request
*oqsr
)
9575 struct ofpbuf
*request
;
9577 switch (ofp_version
) {
9583 case OFP16_VERSION
: {
9584 struct ofp11_queue_stats_request
*req
;
9585 request
= ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST
, ofp_version
, 0);
9586 req
= ofpbuf_put_zeros(request
, sizeof *req
);
9587 req
->port_no
= ofputil_port_to_ofp11(oqsr
->port_no
);
9588 req
->queue_id
= htonl(oqsr
->queue_id
);
9591 case OFP10_VERSION
: {
9592 struct ofp10_queue_stats_request
*req
;
9593 request
= ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST
, ofp_version
, 0);
9594 req
= ofpbuf_put_zeros(request
, sizeof *req
);
9595 /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
9596 req
->port_no
= htons(ofp_to_u16(oqsr
->port_no
== OFPP_ANY
9597 ? OFPP_ALL
: oqsr
->port_no
));
9598 req
->queue_id
= htonl(oqsr
->queue_id
);
9608 /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
9611 ofputil_count_queue_stats(const struct ofp_header
*oh
)
9613 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
9614 ofpraw_pull_assert(&b
);
9616 for (size_t n
= 0; ; n
++) {
9617 struct ofputil_queue_stats qs
;
9618 if (ofputil_decode_queue_stats(&qs
, &b
)) {
9625 ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats
*oqs
,
9626 const struct ofp10_queue_stats
*qs10
)
9628 oqs
->port_no
= u16_to_ofp(ntohs(qs10
->port_no
));
9629 oqs
->queue_id
= ntohl(qs10
->queue_id
);
9630 oqs
->tx_bytes
= ntohll(get_32aligned_be64(&qs10
->tx_bytes
));
9631 oqs
->tx_packets
= ntohll(get_32aligned_be64(&qs10
->tx_packets
));
9632 oqs
->tx_errors
= ntohll(get_32aligned_be64(&qs10
->tx_errors
));
9633 oqs
->duration_sec
= oqs
->duration_nsec
= UINT32_MAX
;
9639 ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats
*oqs
,
9640 const struct ofp11_queue_stats
*qs11
)
9644 error
= ofputil_port_from_ofp11(qs11
->port_no
, &oqs
->port_no
);
9649 oqs
->queue_id
= ntohl(qs11
->queue_id
);
9650 oqs
->tx_bytes
= ntohll(qs11
->tx_bytes
);
9651 oqs
->tx_packets
= ntohll(qs11
->tx_packets
);
9652 oqs
->tx_errors
= ntohll(qs11
->tx_errors
);
9653 oqs
->duration_sec
= oqs
->duration_nsec
= UINT32_MAX
;
9659 ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats
*oqs
,
9660 const struct ofp13_queue_stats
*qs13
)
9662 enum ofperr error
= ofputil_queue_stats_from_ofp11(oqs
, &qs13
->qs
);
9664 oqs
->duration_sec
= ntohl(qs13
->duration_sec
);
9665 oqs
->duration_nsec
= ntohl(qs13
->duration_nsec
);
9672 ofputil_pull_ofp14_queue_stats(struct ofputil_queue_stats
*oqs
,
9675 const struct ofp14_queue_stats
*qs14
;
9678 qs14
= ofpbuf_try_pull(msg
, sizeof *qs14
);
9680 return OFPERR_OFPBRC_BAD_LEN
;
9683 len
= ntohs(qs14
->length
);
9684 if (len
< sizeof *qs14
|| len
- sizeof *qs14
> msg
->size
) {
9685 return OFPERR_OFPBRC_BAD_LEN
;
9687 ofpbuf_pull(msg
, len
- sizeof *qs14
);
9689 /* No properties yet defined, so ignore them for now. */
9691 return ofputil_queue_stats_from_ofp13(oqs
, &qs14
->qs
);
9694 /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
9695 * ofputil_queue_stats in 'qs'.
9697 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
9698 * message. Calling this function multiple times for a single 'msg' iterates
9699 * through the replies. The caller must initially leave 'msg''s layer pointers
9700 * null and not modify them between calls.
9702 * Returns 0 if successful, EOF if no replies were left in this 'msg',
9703 * otherwise a positive errno value. */
9705 ofputil_decode_queue_stats(struct ofputil_queue_stats
*qs
, struct ofpbuf
*msg
)
9710 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
9711 : ofpraw_pull(&raw
, msg
));
9718 } else if (raw
== OFPRAW_OFPST14_QUEUE_REPLY
) {
9719 return ofputil_pull_ofp14_queue_stats(qs
, msg
);
9720 } else if (raw
== OFPRAW_OFPST13_QUEUE_REPLY
) {
9721 const struct ofp13_queue_stats
*qs13
;
9723 qs13
= ofpbuf_try_pull(msg
, sizeof *qs13
);
9727 return ofputil_queue_stats_from_ofp13(qs
, qs13
);
9728 } else if (raw
== OFPRAW_OFPST11_QUEUE_REPLY
) {
9729 const struct ofp11_queue_stats
*qs11
;
9731 qs11
= ofpbuf_try_pull(msg
, sizeof *qs11
);
9735 return ofputil_queue_stats_from_ofp11(qs
, qs11
);
9736 } else if (raw
== OFPRAW_OFPST10_QUEUE_REPLY
) {
9737 const struct ofp10_queue_stats
*qs10
;
9739 qs10
= ofpbuf_try_pull(msg
, sizeof *qs10
);
9743 return ofputil_queue_stats_from_ofp10(qs
, qs10
);
9749 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_QUEUE reply has %"PRIu32
" leftover "
9750 "bytes at end", msg
->size
);
9751 return OFPERR_OFPBRC_BAD_LEN
;
9755 ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats
*oqs
,
9756 struct ofp10_queue_stats
*qs10
)
9758 qs10
->port_no
= htons(ofp_to_u16(oqs
->port_no
));
9759 memset(qs10
->pad
, 0, sizeof qs10
->pad
);
9760 qs10
->queue_id
= htonl(oqs
->queue_id
);
9761 put_32aligned_be64(&qs10
->tx_bytes
, htonll(oqs
->tx_bytes
));
9762 put_32aligned_be64(&qs10
->tx_packets
, htonll(oqs
->tx_packets
));
9763 put_32aligned_be64(&qs10
->tx_errors
, htonll(oqs
->tx_errors
));
9767 ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats
*oqs
,
9768 struct ofp11_queue_stats
*qs11
)
9770 qs11
->port_no
= ofputil_port_to_ofp11(oqs
->port_no
);
9771 qs11
->queue_id
= htonl(oqs
->queue_id
);
9772 qs11
->tx_bytes
= htonll(oqs
->tx_bytes
);
9773 qs11
->tx_packets
= htonll(oqs
->tx_packets
);
9774 qs11
->tx_errors
= htonll(oqs
->tx_errors
);
9778 ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats
*oqs
,
9779 struct ofp13_queue_stats
*qs13
)
9781 ofputil_queue_stats_to_ofp11(oqs
, &qs13
->qs
);
9782 if (oqs
->duration_sec
!= UINT32_MAX
) {
9783 qs13
->duration_sec
= htonl(oqs
->duration_sec
);
9784 qs13
->duration_nsec
= htonl(oqs
->duration_nsec
);
9786 qs13
->duration_sec
= OVS_BE32_MAX
;
9787 qs13
->duration_nsec
= OVS_BE32_MAX
;
9792 ofputil_queue_stats_to_ofp14(const struct ofputil_queue_stats
*oqs
,
9793 struct ofp14_queue_stats
*qs14
)
9795 qs14
->length
= htons(sizeof *qs14
);
9796 memset(qs14
->pad
, 0, sizeof qs14
->pad
);
9797 ofputil_queue_stats_to_ofp13(oqs
, &qs14
->qs
);
9801 /* Encode a queue stat for 'oqs' and append it to 'replies'. */
9803 ofputil_append_queue_stat(struct ovs_list
*replies
,
9804 const struct ofputil_queue_stats
*oqs
)
9806 switch (ofpmp_version(replies
)) {
9807 case OFP13_VERSION
: {
9808 struct ofp13_queue_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
9809 ofputil_queue_stats_to_ofp13(oqs
, reply
);
9814 case OFP11_VERSION
: {
9815 struct ofp11_queue_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
9816 ofputil_queue_stats_to_ofp11(oqs
, reply
);
9820 case OFP10_VERSION
: {
9821 struct ofp10_queue_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
9822 ofputil_queue_stats_to_ofp10(oqs
, reply
);
9828 case OFP16_VERSION
: {
9829 struct ofp14_queue_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
9830 ofputil_queue_stats_to_ofp14(oqs
, reply
);
9840 ofputil_decode_bundle_ctrl(const struct ofp_header
*oh
,
9841 struct ofputil_bundle_ctrl_msg
*msg
)
9843 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
9844 enum ofpraw raw
= ofpraw_pull_assert(&b
);
9845 ovs_assert(raw
== OFPRAW_OFPT14_BUNDLE_CONTROL
9846 || raw
== OFPRAW_ONFT13_BUNDLE_CONTROL
);
9848 const struct ofp14_bundle_ctrl_msg
*m
= b
.msg
;
9849 msg
->bundle_id
= ntohl(m
->bundle_id
);
9850 msg
->type
= ntohs(m
->type
);
9851 msg
->flags
= ntohs(m
->flags
);
9857 ofputil_encode_bundle_ctrl_request(enum ofp_version ofp_version
,
9858 struct ofputil_bundle_ctrl_msg
*bc
)
9860 struct ofpbuf
*request
;
9861 struct ofp14_bundle_ctrl_msg
*m
;
9863 switch (ofp_version
) {
9867 ovs_fatal(0, "bundles need OpenFlow 1.3 or later "
9868 "(\'-O OpenFlow14\')");
9873 request
= ofpraw_alloc(ofp_version
== OFP13_VERSION
9874 ? OFPRAW_ONFT13_BUNDLE_CONTROL
9875 : OFPRAW_OFPT14_BUNDLE_CONTROL
, ofp_version
, 0);
9876 m
= ofpbuf_put_zeros(request
, sizeof *m
);
9878 m
->bundle_id
= htonl(bc
->bundle_id
);
9879 m
->type
= htons(bc
->type
);
9880 m
->flags
= htons(bc
->flags
);
9890 ofputil_encode_bundle_ctrl_reply(const struct ofp_header
*oh
,
9891 struct ofputil_bundle_ctrl_msg
*msg
)
9894 struct ofp14_bundle_ctrl_msg
*m
;
9896 buf
= ofpraw_alloc_reply(oh
->version
== OFP13_VERSION
9897 ? OFPRAW_ONFT13_BUNDLE_CONTROL
9898 : OFPRAW_OFPT14_BUNDLE_CONTROL
, oh
, 0);
9899 m
= ofpbuf_put_zeros(buf
, sizeof *m
);
9901 m
->bundle_id
= htonl(msg
->bundle_id
);
9902 m
->type
= htons(msg
->type
);
9903 m
->flags
= htons(msg
->flags
);
9908 /* Return true for bundlable state change requests, false for other messages.
9911 ofputil_is_bundlable(enum ofptype type
)
9914 /* Minimum required by OpenFlow 1.4. */
9915 case OFPTYPE_PORT_MOD
:
9916 case OFPTYPE_FLOW_MOD
:
9917 /* Other supported types. */
9918 case OFPTYPE_GROUP_MOD
:
9919 case OFPTYPE_PACKET_OUT
:
9922 /* Nice to have later. */
9923 case OFPTYPE_FLOW_MOD_TABLE_ID
:
9924 case OFPTYPE_TABLE_MOD
:
9925 case OFPTYPE_METER_MOD
:
9926 case OFPTYPE_NXT_TLV_TABLE_MOD
:
9928 /* Not to be bundlable. */
9929 case OFPTYPE_ECHO_REQUEST
:
9930 case OFPTYPE_FEATURES_REQUEST
:
9931 case OFPTYPE_GET_CONFIG_REQUEST
:
9932 case OFPTYPE_SET_CONFIG
:
9933 case OFPTYPE_BARRIER_REQUEST
:
9934 case OFPTYPE_ROLE_REQUEST
:
9935 case OFPTYPE_ECHO_REPLY
:
9936 case OFPTYPE_SET_FLOW_FORMAT
:
9937 case OFPTYPE_SET_PACKET_IN_FORMAT
:
9938 case OFPTYPE_SET_CONTROLLER_ID
:
9939 case OFPTYPE_FLOW_AGE
:
9940 case OFPTYPE_FLOW_MONITOR_CANCEL
:
9941 case OFPTYPE_SET_ASYNC_CONFIG
:
9942 case OFPTYPE_GET_ASYNC_REQUEST
:
9943 case OFPTYPE_DESC_STATS_REQUEST
:
9944 case OFPTYPE_FLOW_STATS_REQUEST
:
9945 case OFPTYPE_AGGREGATE_STATS_REQUEST
:
9946 case OFPTYPE_TABLE_STATS_REQUEST
:
9947 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST
:
9948 case OFPTYPE_TABLE_DESC_REQUEST
:
9949 case OFPTYPE_PORT_STATS_REQUEST
:
9950 case OFPTYPE_QUEUE_STATS_REQUEST
:
9951 case OFPTYPE_PORT_DESC_STATS_REQUEST
:
9952 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST
:
9953 case OFPTYPE_METER_STATS_REQUEST
:
9954 case OFPTYPE_METER_CONFIG_STATS_REQUEST
:
9955 case OFPTYPE_METER_FEATURES_STATS_REQUEST
:
9956 case OFPTYPE_GROUP_STATS_REQUEST
:
9957 case OFPTYPE_GROUP_DESC_STATS_REQUEST
:
9958 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST
:
9959 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST
:
9960 case OFPTYPE_BUNDLE_CONTROL
:
9961 case OFPTYPE_BUNDLE_ADD_MESSAGE
:
9964 case OFPTYPE_FEATURES_REPLY
:
9965 case OFPTYPE_GET_CONFIG_REPLY
:
9966 case OFPTYPE_PACKET_IN
:
9967 case OFPTYPE_FLOW_REMOVED
:
9968 case OFPTYPE_PORT_STATUS
:
9969 case OFPTYPE_BARRIER_REPLY
:
9970 case OFPTYPE_QUEUE_GET_CONFIG_REPLY
:
9971 case OFPTYPE_DESC_STATS_REPLY
:
9972 case OFPTYPE_FLOW_STATS_REPLY
:
9973 case OFPTYPE_QUEUE_STATS_REPLY
:
9974 case OFPTYPE_PORT_STATS_REPLY
:
9975 case OFPTYPE_TABLE_STATS_REPLY
:
9976 case OFPTYPE_AGGREGATE_STATS_REPLY
:
9977 case OFPTYPE_PORT_DESC_STATS_REPLY
:
9978 case OFPTYPE_ROLE_REPLY
:
9979 case OFPTYPE_FLOW_MONITOR_PAUSED
:
9980 case OFPTYPE_FLOW_MONITOR_RESUMED
:
9981 case OFPTYPE_FLOW_MONITOR_STATS_REPLY
:
9982 case OFPTYPE_GET_ASYNC_REPLY
:
9983 case OFPTYPE_GROUP_STATS_REPLY
:
9984 case OFPTYPE_GROUP_DESC_STATS_REPLY
:
9985 case OFPTYPE_GROUP_FEATURES_STATS_REPLY
:
9986 case OFPTYPE_METER_STATS_REPLY
:
9987 case OFPTYPE_METER_CONFIG_STATS_REPLY
:
9988 case OFPTYPE_METER_FEATURES_STATS_REPLY
:
9989 case OFPTYPE_TABLE_FEATURES_STATS_REPLY
:
9990 case OFPTYPE_TABLE_DESC_REPLY
:
9991 case OFPTYPE_ROLE_STATUS
:
9992 case OFPTYPE_REQUESTFORWARD
:
9993 case OFPTYPE_TABLE_STATUS
:
9994 case OFPTYPE_NXT_TLV_TABLE_REQUEST
:
9995 case OFPTYPE_NXT_TLV_TABLE_REPLY
:
9996 case OFPTYPE_NXT_RESUME
:
9997 case OFPTYPE_IPFIX_BRIDGE_STATS_REQUEST
:
9998 case OFPTYPE_IPFIX_BRIDGE_STATS_REPLY
:
9999 case OFPTYPE_IPFIX_FLOW_STATS_REQUEST
:
10000 case OFPTYPE_IPFIX_FLOW_STATS_REPLY
:
10008 ofputil_decode_bundle_add(const struct ofp_header
*oh
,
10009 struct ofputil_bundle_add_msg
*msg
,
10010 enum ofptype
*typep
)
10012 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
10013 enum ofpraw raw
= ofpraw_pull_assert(&b
);
10014 ovs_assert(raw
== OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE
10015 || raw
== OFPRAW_ONFT13_BUNDLE_ADD_MESSAGE
);
10017 const struct ofp14_bundle_ctrl_msg
*m
= ofpbuf_pull(&b
, sizeof *m
);
10018 msg
->bundle_id
= ntohl(m
->bundle_id
);
10019 msg
->flags
= ntohs(m
->flags
);
10022 if (msg
->msg
->version
!= oh
->version
) {
10023 return OFPERR_OFPBFC_BAD_VERSION
;
10025 size_t inner_len
= ntohs(msg
->msg
->length
);
10026 if (inner_len
< sizeof(struct ofp_header
) || inner_len
> b
.size
) {
10027 return OFPERR_OFPBFC_MSG_BAD_LEN
;
10029 if (msg
->msg
->xid
!= oh
->xid
) {
10030 return OFPERR_OFPBFC_MSG_BAD_XID
;
10033 /* Reject unbundlable messages. */
10035 enum ofperr error
= ofptype_decode(&type
, msg
->msg
);
10037 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPT14_BUNDLE_ADD_MESSAGE contained "
10038 "message is unparsable (%s)", ofperr_get_name(error
));
10039 return OFPERR_OFPBFC_MSG_UNSUP
; /* 'error' would be confusing. */
10042 if (!ofputil_is_bundlable(type
)) {
10043 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s message not allowed inside "
10044 "OFPT14_BUNDLE_ADD_MESSAGE", ofptype_get_name(type
));
10045 return OFPERR_OFPBFC_MSG_UNSUP
;
10055 ofputil_encode_bundle_add(enum ofp_version ofp_version
,
10056 struct ofputil_bundle_add_msg
*msg
)
10058 struct ofpbuf
*request
;
10059 struct ofp14_bundle_ctrl_msg
*m
;
10061 /* Must use the same xid as the embedded message. */
10062 request
= ofpraw_alloc_xid(ofp_version
== OFP13_VERSION
10063 ? OFPRAW_ONFT13_BUNDLE_ADD_MESSAGE
10064 : OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE
, ofp_version
,
10065 msg
->msg
->xid
, ntohs(msg
->msg
->length
));
10066 m
= ofpbuf_put_zeros(request
, sizeof *m
);
10068 m
->bundle_id
= htonl(msg
->bundle_id
);
10069 m
->flags
= htons(msg
->flags
);
10070 ofpbuf_put(request
, msg
->msg
, ntohs(msg
->msg
->length
));
10072 ofpmsg_update_length(request
);
10077 encode_tlv_table_mappings(struct ofpbuf
*b
, struct ovs_list
*mappings
)
10079 struct ofputil_tlv_map
*map
;
10081 LIST_FOR_EACH (map
, list_node
, mappings
) {
10082 struct nx_tlv_map
*nx_map
;
10084 nx_map
= ofpbuf_put_zeros(b
, sizeof *nx_map
);
10085 nx_map
->option_class
= htons(map
->option_class
);
10086 nx_map
->option_type
= map
->option_type
;
10087 nx_map
->option_len
= map
->option_len
;
10088 nx_map
->index
= htons(map
->index
);
10093 ofputil_encode_tlv_table_mod(enum ofp_version ofp_version
,
10094 struct ofputil_tlv_table_mod
*ttm
)
10097 struct nx_tlv_table_mod
*nx_ttm
;
10099 b
= ofpraw_alloc(OFPRAW_NXT_TLV_TABLE_MOD
, ofp_version
, 0);
10100 nx_ttm
= ofpbuf_put_zeros(b
, sizeof *nx_ttm
);
10101 nx_ttm
->command
= htons(ttm
->command
);
10102 encode_tlv_table_mappings(b
, &ttm
->mappings
);
10108 decode_tlv_table_mappings(struct ofpbuf
*msg
, unsigned int max_fields
,
10109 struct ovs_list
*mappings
)
10111 ovs_list_init(mappings
);
10113 while (msg
->size
) {
10114 struct nx_tlv_map
*nx_map
;
10115 struct ofputil_tlv_map
*map
;
10117 nx_map
= ofpbuf_pull(msg
, sizeof *nx_map
);
10118 map
= xmalloc(sizeof *map
);
10119 ovs_list_push_back(mappings
, &map
->list_node
);
10121 map
->option_class
= ntohs(nx_map
->option_class
);
10122 map
->option_type
= nx_map
->option_type
;
10124 map
->option_len
= nx_map
->option_len
;
10125 if (map
->option_len
% 4 || map
->option_len
> TLV_MAX_OPT_SIZE
) {
10126 VLOG_WARN_RL(&bad_ofmsg_rl
,
10127 "tlv table option length (%u) is not a valid option size",
10129 ofputil_uninit_tlv_table(mappings
);
10130 return OFPERR_NXTTMFC_BAD_OPT_LEN
;
10133 map
->index
= ntohs(nx_map
->index
);
10134 if (map
->index
>= max_fields
) {
10135 VLOG_WARN_RL(&bad_ofmsg_rl
,
10136 "tlv table field index (%u) is too large (max %u)",
10137 map
->index
, max_fields
- 1);
10138 ofputil_uninit_tlv_table(mappings
);
10139 return OFPERR_NXTTMFC_BAD_FIELD_IDX
;
10147 ofputil_decode_tlv_table_mod(const struct ofp_header
*oh
,
10148 struct ofputil_tlv_table_mod
*ttm
)
10150 struct ofpbuf msg
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
10151 ofpraw_pull_assert(&msg
);
10153 struct nx_tlv_table_mod
*nx_ttm
= ofpbuf_pull(&msg
, sizeof *nx_ttm
);
10154 ttm
->command
= ntohs(nx_ttm
->command
);
10155 if (ttm
->command
> NXTTMC_CLEAR
) {
10156 VLOG_WARN_RL(&bad_ofmsg_rl
,
10157 "tlv table mod command (%u) is out of range",
10159 return OFPERR_NXTTMFC_BAD_COMMAND
;
10162 return decode_tlv_table_mappings(&msg
, TUN_METADATA_NUM_OPTS
,
10167 ofputil_encode_tlv_table_reply(const struct ofp_header
*oh
,
10168 struct ofputil_tlv_table_reply
*ttr
)
10171 struct nx_tlv_table_reply
*nx_ttr
;
10173 b
= ofpraw_alloc_reply(OFPRAW_NXT_TLV_TABLE_REPLY
, oh
, 0);
10174 nx_ttr
= ofpbuf_put_zeros(b
, sizeof *nx_ttr
);
10175 nx_ttr
->max_option_space
= htonl(ttr
->max_option_space
);
10176 nx_ttr
->max_fields
= htons(ttr
->max_fields
);
10178 encode_tlv_table_mappings(b
, &ttr
->mappings
);
10183 /* Decodes the NXT_TLV_TABLE_REPLY message in 'oh' into '*ttr'. Returns 0
10184 * if successful, otherwise an ofperr.
10186 * The decoder verifies that the indexes in 'ttr->mappings' are less than
10187 * 'ttr->max_fields', but the caller must ensure, if necessary, that they are
10188 * less than TUN_METADATA_NUM_OPTS. */
10190 ofputil_decode_tlv_table_reply(const struct ofp_header
*oh
,
10191 struct ofputil_tlv_table_reply
*ttr
)
10193 struct ofpbuf msg
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
10194 ofpraw_pull_assert(&msg
);
10196 struct nx_tlv_table_reply
*nx_ttr
= ofpbuf_pull(&msg
, sizeof *nx_ttr
);
10197 ttr
->max_option_space
= ntohl(nx_ttr
->max_option_space
);
10198 ttr
->max_fields
= ntohs(nx_ttr
->max_fields
);
10200 return decode_tlv_table_mappings(&msg
, ttr
->max_fields
, &ttr
->mappings
);
10204 ofputil_uninit_tlv_table(struct ovs_list
*mappings
)
10206 struct ofputil_tlv_map
*map
;
10208 LIST_FOR_EACH_POP (map
, list_node
, mappings
) {
10214 ofputil_async_msg_type_to_string(enum ofputil_async_msg_type type
)
10217 case OAM_PACKET_IN
: return "PACKET_IN";
10218 case OAM_PORT_STATUS
: return "PORT_STATUS";
10219 case OAM_FLOW_REMOVED
: return "FLOW_REMOVED";
10220 case OAM_ROLE_STATUS
: return "ROLE_STATUS";
10221 case OAM_TABLE_STATUS
: return "TABLE_STATUS";
10222 case OAM_REQUESTFORWARD
: return "REQUESTFORWARD";
10230 struct ofp14_async_prop
{
10231 uint64_t prop_type
;
10232 enum ofputil_async_msg_type oam
;
10234 uint32_t allowed10
, allowed14
;
10237 #define AP_PAIR(SLAVE_PROP_TYPE, OAM, A10, A14) \
10238 { SLAVE_PROP_TYPE, OAM, false, A10, (A14) ? (A14) : (A10) }, \
10239 { (SLAVE_PROP_TYPE + 1), OAM, true, A10, (A14) ? (A14) : (A10) }
10241 static const struct ofp14_async_prop async_props
[] = {
10242 AP_PAIR( 0, OAM_PACKET_IN
, OFPR10_BITS
, OFPR14_BITS
),
10243 AP_PAIR( 2, OAM_PORT_STATUS
, (1 << OFPPR_N_REASONS
) - 1, 0),
10244 AP_PAIR( 4, OAM_FLOW_REMOVED
, (1 << OVS_OFPRR_NONE
) - 1, 0),
10245 AP_PAIR( 6, OAM_ROLE_STATUS
, (1 << OFPCRR_N_REASONS
) - 1, 0),
10246 AP_PAIR( 8, OAM_TABLE_STATUS
, OFPTR_BITS
, 0),
10247 AP_PAIR(10, OAM_REQUESTFORWARD
, (1 << OFPRFR_N_REASONS
) - 1, 0),
10250 #define FOR_EACH_ASYNC_PROP(VAR) \
10251 for (const struct ofp14_async_prop *VAR = async_props; \
10252 VAR < &async_props[ARRAY_SIZE(async_props)]; VAR++)
10254 static const struct ofp14_async_prop
*
10255 get_ofp14_async_config_prop_by_prop_type(uint64_t prop_type
)
10257 FOR_EACH_ASYNC_PROP (ap
) {
10258 if (prop_type
== ap
->prop_type
) {
10265 static const struct ofp14_async_prop
*
10266 get_ofp14_async_config_prop_by_oam(enum ofputil_async_msg_type oam
,
10269 FOR_EACH_ASYNC_PROP (ap
) {
10270 if (ap
->oam
== oam
&& ap
->master
== master
) {
10278 ofp14_async_prop_allowed(const struct ofp14_async_prop
*prop
,
10279 enum ofp_version version
)
10281 return version
>= OFP14_VERSION
? prop
->allowed14
: prop
->allowed10
;
10285 encode_async_mask(const struct ofputil_async_cfg
*src
,
10286 const struct ofp14_async_prop
*ap
,
10287 enum ofp_version version
)
10289 uint32_t mask
= ap
->master
? src
->master
[ap
->oam
] : src
->slave
[ap
->oam
];
10290 return htonl(mask
& ofp14_async_prop_allowed(ap
, version
));
10294 decode_async_mask(ovs_be32 src
,
10295 const struct ofp14_async_prop
*ap
, enum ofp_version version
,
10296 bool loose
, struct ofputil_async_cfg
*dst
)
10298 uint32_t mask
= ntohl(src
);
10299 uint32_t allowed
= ofp14_async_prop_allowed(ap
, version
);
10300 if (mask
& ~allowed
) {
10301 OFPPROP_LOG(&bad_ofmsg_rl
, loose
,
10302 "bad value %#x for %s (allowed mask %#x)",
10303 mask
, ofputil_async_msg_type_to_string(ap
->oam
),
10307 return OFPERR_OFPACFC_INVALID
;
10311 if (ap
->oam
== OAM_PACKET_IN
) {
10312 if (mask
& (1u << OFPR_NO_MATCH
)) {
10313 mask
|= 1u << OFPR_EXPLICIT_MISS
;
10314 if (version
< OFP13_VERSION
) {
10315 mask
|= 1u << OFPR_IMPLICIT_MISS
;
10320 uint32_t *array
= ap
->master
? dst
->master
: dst
->slave
;
10321 array
[ap
->oam
] = mask
;
10326 parse_async_tlv(const struct ofpbuf
*property
,
10327 const struct ofp14_async_prop
*ap
,
10328 struct ofputil_async_cfg
*ac
,
10329 enum ofp_version version
, bool loose
)
10334 error
= ofpprop_parse_be32(property
, &mask
);
10339 if (ofpprop_is_experimenter(ap
->prop_type
)) {
10340 /* For experimenter properties, whether a property is for the master or
10341 * slave role is indicated by both 'type' and 'exp_type' in struct
10342 * ofp_prop_experimenter. Check that these are consistent. */
10343 const struct ofp_prop_experimenter
*ope
= property
->data
;
10344 bool should_be_master
= ope
->type
== htons(0xffff);
10345 if (should_be_master
!= ap
->master
) {
10346 VLOG_WARN_RL(&bad_ofmsg_rl
, "async property type %#"PRIx16
" "
10347 "indicates %s role but exp_type %"PRIu32
" indicates "
10350 should_be_master
? "master" : "slave",
10351 ntohl(ope
->exp_type
),
10352 ap
->master
? "master" : "slave");
10353 return OFPERR_OFPBPC_BAD_EXP_TYPE
;
10357 return decode_async_mask(mask
, ap
, version
, loose
, ac
);
10361 decode_legacy_async_masks(const ovs_be32 masks
[2],
10362 enum ofputil_async_msg_type oam
,
10363 enum ofp_version version
,
10364 struct ofputil_async_cfg
*dst
)
10366 for (int i
= 0; i
< 2; i
++) {
10367 bool master
= i
== 0;
10368 const struct ofp14_async_prop
*ap
10369 = get_ofp14_async_config_prop_by_oam(oam
, master
);
10370 decode_async_mask(masks
[i
], ap
, version
, true, dst
);
10374 /* Decodes the OpenFlow "set async config" request and "get async config
10375 * reply" message in '*oh' into an abstract form in 'ac'.
10377 * Some versions of the "set async config" request change only some of the
10378 * settings and leave the others alone. This function uses 'basis' as the
10379 * initial state for decoding these. Other versions of the request change all
10380 * the settings; this function ignores 'basis' when decoding these.
10382 * If 'loose' is true, this function ignores properties and values that it does
10383 * not understand, as a controller would want to do when interpreting
10384 * capabilities provided by a switch. If 'loose' is false, this function
10385 * treats unknown properties and values as an error, as a switch would want to
10386 * do when interpreting a configuration request made by a controller.
10388 * Returns 0 if successful, otherwise an OFPERR_* value.
10390 * Returns error code OFPERR_OFPACFC_INVALID if the value of mask is not in
10391 * the valid range of mask.
10393 * Returns error code OFPERR_OFPACFC_UNSUPPORTED if the configuration is not
10396 ofputil_decode_set_async_config(const struct ofp_header
*oh
, bool loose
,
10397 const struct ofputil_async_cfg
*basis
,
10398 struct ofputil_async_cfg
*ac
)
10400 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
10401 enum ofpraw raw
= ofpraw_pull_assert(&b
);
10403 if (raw
== OFPRAW_OFPT13_SET_ASYNC
||
10404 raw
== OFPRAW_NXT_SET_ASYNC_CONFIG
||
10405 raw
== OFPRAW_OFPT13_GET_ASYNC_REPLY
) {
10406 const struct nx_async_config
*msg
= ofpmsg_body(oh
);
10408 *ac
= OFPUTIL_ASYNC_CFG_INIT
;
10409 decode_legacy_async_masks(msg
->packet_in_mask
, OAM_PACKET_IN
,
10411 decode_legacy_async_masks(msg
->port_status_mask
, OAM_PORT_STATUS
,
10413 decode_legacy_async_masks(msg
->flow_removed_mask
, OAM_FLOW_REMOVED
,
10415 } else if (raw
== OFPRAW_OFPT14_SET_ASYNC
||
10416 raw
== OFPRAW_OFPT14_GET_ASYNC_REPLY
||
10417 raw
== OFPRAW_NXT_SET_ASYNC_CONFIG2
) {
10419 while (b
.size
> 0) {
10420 struct ofpbuf property
;
10424 error
= ofpprop_pull__(&b
, &property
, 8, 0xfffe, &type
);
10429 const struct ofp14_async_prop
*ap
10430 = get_ofp14_async_config_prop_by_prop_type(type
);
10432 ? parse_async_tlv(&property
, ap
, ac
, oh
->version
, loose
)
10433 : OFPPROP_UNKNOWN(loose
, "async config", type
));
10435 /* Most messages use OFPBPC_BAD_TYPE but async has its own (who
10436 * knows why, it's OpenFlow. */
10437 if (error
== OFPERR_OFPBPC_BAD_TYPE
) {
10438 error
= OFPERR_OFPACFC_UNSUPPORTED
;
10444 return OFPERR_OFPBRC_BAD_VERSION
;
10450 encode_legacy_async_masks(const struct ofputil_async_cfg
*ac
,
10451 enum ofputil_async_msg_type oam
,
10452 enum ofp_version version
,
10455 for (int i
= 0; i
< 2; i
++) {
10456 bool master
= i
== 0;
10457 const struct ofp14_async_prop
*ap
10458 = get_ofp14_async_config_prop_by_oam(oam
, master
);
10459 masks
[i
] = encode_async_mask(ac
, ap
, version
);
10464 ofputil_put_async_config__(const struct ofputil_async_cfg
*ac
,
10465 struct ofpbuf
*buf
, bool tlv
,
10466 enum ofp_version version
, uint32_t oams
)
10469 struct nx_async_config
*msg
= ofpbuf_put_zeros(buf
, sizeof *msg
);
10470 encode_legacy_async_masks(ac
, OAM_PACKET_IN
, version
,
10471 msg
->packet_in_mask
);
10472 encode_legacy_async_masks(ac
, OAM_PORT_STATUS
, version
,
10473 msg
->port_status_mask
);
10474 encode_legacy_async_masks(ac
, OAM_FLOW_REMOVED
, version
,
10475 msg
->flow_removed_mask
);
10477 FOR_EACH_ASYNC_PROP (ap
) {
10478 if (oams
& (1u << ap
->oam
)) {
10479 size_t ofs
= buf
->size
;
10480 ofpprop_put_be32(buf
, ap
->prop_type
,
10481 encode_async_mask(ac
, ap
, version
));
10483 /* For experimenter properties, we need to use type 0xfffe for
10484 * master and 0xffff for slaves. */
10485 if (ofpprop_is_experimenter(ap
->prop_type
)) {
10486 struct ofp_prop_experimenter
*ope
10487 = ofpbuf_at_assert(buf
, ofs
, sizeof *ope
);
10488 ope
->type
= ap
->master
? htons(0xffff) : htons(0xfffe);
10495 /* Encodes and returns a reply to the OFPT_GET_ASYNC_REQUEST in 'oh' that
10496 * states that the asynchronous message configuration is 'ac'. */
10498 ofputil_encode_get_async_reply(const struct ofp_header
*oh
,
10499 const struct ofputil_async_cfg
*ac
)
10501 struct ofpbuf
*buf
;
10503 enum ofpraw raw
= (oh
->version
< OFP14_VERSION
10504 ? OFPRAW_OFPT13_GET_ASYNC_REPLY
10505 : OFPRAW_OFPT14_GET_ASYNC_REPLY
);
10506 struct ofpbuf
*reply
= ofpraw_alloc_reply(raw
, oh
, 0);
10507 ofputil_put_async_config__(ac
, reply
,
10508 raw
== OFPRAW_OFPT14_GET_ASYNC_REPLY
,
10509 oh
->version
, UINT32_MAX
);
10515 /* Encodes and returns a message, in a format appropriate for OpenFlow version
10516 * 'ofp_version', that sets the asynchronous message configuration to 'ac'.
10518 * Specify 'oams' as a bitmap of OAM_* that indicate the asynchronous messages
10519 * to configure. OF1.0 through OF1.3 can't natively configure a subset of
10520 * messages, so more messages than requested may be configured. OF1.0 through
10521 * OF1.3 also can't configure OVS extension OAM_* values, so if 'oam' includes
10522 * any extensions then this function encodes an Open vSwitch extension message
10523 * that does support configuring OVS extension OAM_*. */
10525 ofputil_encode_set_async_config(const struct ofputil_async_cfg
*ac
,
10526 uint32_t oams
, enum ofp_version ofp_version
)
10528 enum ofpraw raw
= (ofp_version
>= OFP14_VERSION
? OFPRAW_OFPT14_SET_ASYNC
10529 : oams
& OAM_EXTENSIONS
? OFPRAW_NXT_SET_ASYNC_CONFIG2
10530 : ofp_version
>= OFP13_VERSION
? OFPRAW_OFPT13_SET_ASYNC
10531 : OFPRAW_NXT_SET_ASYNC_CONFIG
);
10532 struct ofpbuf
*request
= ofpraw_alloc(raw
, ofp_version
, 0);
10533 ofputil_put_async_config__(ac
, request
,
10534 (raw
== OFPRAW_OFPT14_SET_ASYNC
||
10535 raw
== OFPRAW_NXT_SET_ASYNC_CONFIG2
),
10536 ofp_version
, oams
);
10540 struct ofputil_async_cfg
10541 ofputil_async_cfg_default(enum ofp_version version
)
10543 /* We enable all of the OF1.4 reasons regardless of 'version' because the
10544 * reasons added in OF1.4 just are just refinements of the OFPR_ACTION
10545 * introduced in OF1.0, breaking it into more specific categories. When we
10546 * encode these for earlier OpenFlow versions, we translate them into
10548 uint32_t pin
= OFPR14_BITS
& ~(1u << OFPR_INVALID_TTL
);
10549 pin
|= 1u << OFPR_EXPLICIT_MISS
;
10550 if (version
<= OFP12_VERSION
) {
10551 pin
|= 1u << OFPR_IMPLICIT_MISS
;
10554 return (struct ofputil_async_cfg
) {
10555 .master
[OAM_PACKET_IN
] = pin
,
10557 .master
[OAM_FLOW_REMOVED
]
10558 = (version
>= OFP14_VERSION
? OFPRR14_BITS
: OFPRR10_BITS
),
10560 .master
[OAM_PORT_STATUS
] = OFPPR_BITS
,
10561 .slave
[OAM_PORT_STATUS
] = OFPPR_BITS
,
10566 ofputil_put_ofp14_table_desc(const struct ofputil_table_desc
*td
,
10567 struct ofpbuf
*b
, enum ofp_version version
)
10569 struct ofp14_table_desc
*otd
;
10570 struct ofp14_table_mod_prop_vacancy
*otv
;
10573 start_otd
= b
->size
;
10574 ofpbuf_put_zeros(b
, sizeof *otd
);
10576 ofpprop_put_u32(b
, OFPTMPT14_EVICTION
, td
->eviction_flags
);
10578 otv
= ofpbuf_put_zeros(b
, sizeof *otv
);
10579 otv
->type
= htons(OFPTMPT14_VACANCY
);
10580 otv
->length
= htons(sizeof *otv
);
10581 otv
->vacancy_down
= td
->table_vacancy
.vacancy_down
;
10582 otv
->vacancy_up
= td
->table_vacancy
.vacancy_up
;
10583 otv
->vacancy
= td
->table_vacancy
.vacancy
;
10585 otd
= ofpbuf_at_assert(b
, start_otd
, sizeof *otd
);
10586 otd
->length
= htons(b
->size
- start_otd
);
10587 otd
->table_id
= td
->table_id
;
10588 otd
->config
= ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT
,
10589 td
->eviction
, td
->vacancy
,
10593 /* Converts the abstract form of a "table status" message in '*ts' into an
10594 * OpenFlow message suitable for 'protocol', and returns that encoded form in
10595 * a buffer owned by the caller. */
10597 ofputil_encode_table_status(const struct ofputil_table_status
*ts
,
10598 enum ofputil_protocol protocol
)
10600 enum ofp_version version
;
10603 version
= ofputil_protocol_to_ofp_version(protocol
);
10604 if (version
>= OFP14_VERSION
) {
10606 struct ofp14_table_status
*ots
;
10608 raw
= OFPRAW_OFPT14_TABLE_STATUS
;
10609 b
= ofpraw_alloc_xid(raw
, version
, htonl(0), 0);
10610 ots
= ofpbuf_put_zeros(b
, sizeof *ots
);
10611 ots
->reason
= ts
->reason
;
10612 ofputil_put_ofp14_table_desc(&ts
->desc
, b
, version
);
10613 ofpmsg_update_length(b
);
10620 /* Decodes the OpenFlow "table status" message in '*ots' into an abstract form
10621 * in '*ts'. Returns 0 if successful, otherwise an OFPERR_* value. */
10623 ofputil_decode_table_status(const struct ofp_header
*oh
,
10624 struct ofputil_table_status
*ts
)
10626 const struct ofp14_table_status
*ots
;
10631 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
10632 raw
= ofpraw_pull_assert(&b
);
10633 ots
= ofpbuf_pull(&b
, sizeof *ots
);
10635 if (raw
== OFPRAW_OFPT14_TABLE_STATUS
) {
10636 if (ots
->reason
!= OFPTR_VACANCY_DOWN
10637 && ots
->reason
!= OFPTR_VACANCY_UP
) {
10638 return OFPERR_OFPBPC_BAD_VALUE
;
10640 ts
->reason
= ots
->reason
;
10642 error
= ofputil_decode_table_desc(&b
, &ts
->desc
, oh
->version
);
10645 return OFPERR_OFPBRC_BAD_VERSION
;