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.
18 #include "ofp-print.h"
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
27 #include "byte-order.h"
28 #include "classifier.h"
29 #include "dynamic-string.h"
31 #include "meta-flow.h"
32 #include "multipath.h"
36 #include "ofp-actions.h"
37 #include "ofp-errors.h"
42 #include "openflow/netronome-ext.h"
45 #include "tun-metadata.h"
46 #include "unaligned.h"
47 #include "type-props.h"
48 #include "openvswitch/vlog.h"
51 VLOG_DEFINE_THIS_MODULE(ofp_util
);
53 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
54 * in the peer and so there's not much point in showing a lot of them. */
55 static struct vlog_rate_limit bad_ofmsg_rl
= VLOG_RATE_LIMIT_INIT(1, 5);
57 static enum ofputil_table_vacancy
ofputil_decode_table_vacancy(
58 ovs_be32 config
, enum ofp_version
);
59 static enum ofputil_table_eviction
ofputil_decode_table_eviction(
60 ovs_be32 config
, enum ofp_version
);
61 static ovs_be32
ofputil_encode_table_config(enum ofputil_table_miss
,
62 enum ofputil_table_eviction
,
63 enum ofputil_table_vacancy
,
66 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
67 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
70 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
71 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
72 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
73 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
76 ofputil_wcbits_to_netmask(int wcbits
)
79 return wcbits
< 32 ? htonl(~((1u << wcbits
) - 1)) : 0;
82 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
83 * that it wildcards, that is, the number of 0-bits in 'netmask', a number
84 * between 0 and 32 inclusive.
86 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
87 * still be in the valid range but isn't otherwise meaningful. */
89 ofputil_netmask_to_wcbits(ovs_be32 netmask
)
91 return 32 - ip_count_cidr_bits(netmask
);
94 /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
95 * flow_wildcards in 'wc' for use in struct match. It is the caller's
96 * responsibility to handle the special case where the flow match's dl_vlan is
97 * set to OFP_VLAN_NONE. */
99 ofputil_wildcard_from_ofpfw10(uint32_t ofpfw
, struct flow_wildcards
*wc
)
101 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 35);
103 /* Initialize most of wc. */
104 flow_wildcards_init_catchall(wc
);
106 if (!(ofpfw
& OFPFW10_IN_PORT
)) {
107 wc
->masks
.in_port
.ofp_port
= u16_to_ofp(UINT16_MAX
);
110 if (!(ofpfw
& OFPFW10_NW_TOS
)) {
111 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
114 if (!(ofpfw
& OFPFW10_NW_PROTO
)) {
115 wc
->masks
.nw_proto
= UINT8_MAX
;
117 wc
->masks
.nw_src
= ofputil_wcbits_to_netmask(ofpfw
118 >> OFPFW10_NW_SRC_SHIFT
);
119 wc
->masks
.nw_dst
= ofputil_wcbits_to_netmask(ofpfw
120 >> OFPFW10_NW_DST_SHIFT
);
122 if (!(ofpfw
& OFPFW10_TP_SRC
)) {
123 wc
->masks
.tp_src
= OVS_BE16_MAX
;
125 if (!(ofpfw
& OFPFW10_TP_DST
)) {
126 wc
->masks
.tp_dst
= OVS_BE16_MAX
;
129 if (!(ofpfw
& OFPFW10_DL_SRC
)) {
130 WC_MASK_FIELD(wc
, dl_src
);
132 if (!(ofpfw
& OFPFW10_DL_DST
)) {
133 WC_MASK_FIELD(wc
, dl_dst
);
135 if (!(ofpfw
& OFPFW10_DL_TYPE
)) {
136 wc
->masks
.dl_type
= OVS_BE16_MAX
;
140 if (!(ofpfw
& OFPFW10_DL_VLAN_PCP
)) {
141 wc
->masks
.vlan_tci
|= htons(VLAN_PCP_MASK
| VLAN_CFI
);
143 if (!(ofpfw
& OFPFW10_DL_VLAN
)) {
144 wc
->masks
.vlan_tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
148 /* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */
150 ofputil_match_from_ofp10_match(const struct ofp10_match
*ofmatch
,
153 uint32_t ofpfw
= ntohl(ofmatch
->wildcards
) & OFPFW10_ALL
;
155 /* Initialize match->wc. */
156 memset(&match
->flow
, 0, sizeof match
->flow
);
157 ofputil_wildcard_from_ofpfw10(ofpfw
, &match
->wc
);
159 /* Initialize most of match->flow. */
160 match
->flow
.nw_src
= ofmatch
->nw_src
;
161 match
->flow
.nw_dst
= ofmatch
->nw_dst
;
162 match
->flow
.in_port
.ofp_port
= u16_to_ofp(ntohs(ofmatch
->in_port
));
163 match
->flow
.dl_type
= ofputil_dl_type_from_openflow(ofmatch
->dl_type
);
164 match
->flow
.tp_src
= ofmatch
->tp_src
;
165 match
->flow
.tp_dst
= ofmatch
->tp_dst
;
166 match
->flow
.dl_src
= ofmatch
->dl_src
;
167 match
->flow
.dl_dst
= ofmatch
->dl_dst
;
168 match
->flow
.nw_tos
= ofmatch
->nw_tos
& IP_DSCP_MASK
;
169 match
->flow
.nw_proto
= ofmatch
->nw_proto
;
171 /* Translate VLANs. */
172 if (!(ofpfw
& OFPFW10_DL_VLAN
) &&
173 ofmatch
->dl_vlan
== htons(OFP10_VLAN_NONE
)) {
174 /* Match only packets without 802.1Q header.
176 * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
178 * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
179 * because we can't have a specific PCP without an 802.1Q header.
180 * However, older versions of OVS treated this as matching packets
181 * withut an 802.1Q header, so we do here too. */
182 match
->flow
.vlan_tci
= htons(0);
183 match
->wc
.masks
.vlan_tci
= htons(0xffff);
185 ovs_be16 vid
, pcp
, tci
;
188 vid
= ofmatch
->dl_vlan
& htons(VLAN_VID_MASK
);
189 hpcp
= (ofmatch
->dl_vlan_pcp
<< VLAN_PCP_SHIFT
) & VLAN_PCP_MASK
;
191 tci
= vid
| pcp
| htons(VLAN_CFI
);
192 match
->flow
.vlan_tci
= tci
& match
->wc
.masks
.vlan_tci
;
196 match_zero_wildcarded_fields(match
);
199 /* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */
201 ofputil_match_to_ofp10_match(const struct match
*match
,
202 struct ofp10_match
*ofmatch
)
204 const struct flow_wildcards
*wc
= &match
->wc
;
207 /* Figure out most OpenFlow wildcards. */
209 if (!wc
->masks
.in_port
.ofp_port
) {
210 ofpfw
|= OFPFW10_IN_PORT
;
212 if (!wc
->masks
.dl_type
) {
213 ofpfw
|= OFPFW10_DL_TYPE
;
215 if (!wc
->masks
.nw_proto
) {
216 ofpfw
|= OFPFW10_NW_PROTO
;
218 ofpfw
|= (ofputil_netmask_to_wcbits(wc
->masks
.nw_src
)
219 << OFPFW10_NW_SRC_SHIFT
);
220 ofpfw
|= (ofputil_netmask_to_wcbits(wc
->masks
.nw_dst
)
221 << OFPFW10_NW_DST_SHIFT
);
222 if (!(wc
->masks
.nw_tos
& IP_DSCP_MASK
)) {
223 ofpfw
|= OFPFW10_NW_TOS
;
225 if (!wc
->masks
.tp_src
) {
226 ofpfw
|= OFPFW10_TP_SRC
;
228 if (!wc
->masks
.tp_dst
) {
229 ofpfw
|= OFPFW10_TP_DST
;
231 if (eth_addr_is_zero(wc
->masks
.dl_src
)) {
232 ofpfw
|= OFPFW10_DL_SRC
;
234 if (eth_addr_is_zero(wc
->masks
.dl_dst
)) {
235 ofpfw
|= OFPFW10_DL_DST
;
238 /* Translate VLANs. */
239 ofmatch
->dl_vlan
= htons(0);
240 ofmatch
->dl_vlan_pcp
= 0;
241 if (match
->wc
.masks
.vlan_tci
== htons(0)) {
242 ofpfw
|= OFPFW10_DL_VLAN
| OFPFW10_DL_VLAN_PCP
;
243 } else if (match
->wc
.masks
.vlan_tci
& htons(VLAN_CFI
)
244 && !(match
->flow
.vlan_tci
& htons(VLAN_CFI
))) {
245 ofmatch
->dl_vlan
= htons(OFP10_VLAN_NONE
);
247 if (!(match
->wc
.masks
.vlan_tci
& htons(VLAN_VID_MASK
))) {
248 ofpfw
|= OFPFW10_DL_VLAN
;
250 ofmatch
->dl_vlan
= htons(vlan_tci_to_vid(match
->flow
.vlan_tci
));
253 if (!(match
->wc
.masks
.vlan_tci
& htons(VLAN_PCP_MASK
))) {
254 ofpfw
|= OFPFW10_DL_VLAN_PCP
;
256 ofmatch
->dl_vlan_pcp
= vlan_tci_to_pcp(match
->flow
.vlan_tci
);
260 /* Compose most of the match structure. */
261 ofmatch
->wildcards
= htonl(ofpfw
);
262 ofmatch
->in_port
= htons(ofp_to_u16(match
->flow
.in_port
.ofp_port
));
263 ofmatch
->dl_src
= match
->flow
.dl_src
;
264 ofmatch
->dl_dst
= match
->flow
.dl_dst
;
265 ofmatch
->dl_type
= ofputil_dl_type_to_openflow(match
->flow
.dl_type
);
266 ofmatch
->nw_src
= match
->flow
.nw_src
;
267 ofmatch
->nw_dst
= match
->flow
.nw_dst
;
268 ofmatch
->nw_tos
= match
->flow
.nw_tos
& IP_DSCP_MASK
;
269 ofmatch
->nw_proto
= match
->flow
.nw_proto
;
270 ofmatch
->tp_src
= match
->flow
.tp_src
;
271 ofmatch
->tp_dst
= match
->flow
.tp_dst
;
272 memset(ofmatch
->pad1
, '\0', sizeof ofmatch
->pad1
);
273 memset(ofmatch
->pad2
, '\0', sizeof ofmatch
->pad2
);
277 ofputil_pull_ofp11_match(struct ofpbuf
*buf
, struct match
*match
,
278 uint16_t *padded_match_len
)
280 struct ofp11_match_header
*omh
= buf
->data
;
283 if (buf
->size
< sizeof *omh
) {
284 return OFPERR_OFPBMC_BAD_LEN
;
287 match_len
= ntohs(omh
->length
);
289 switch (ntohs(omh
->type
)) {
290 case OFPMT_STANDARD
: {
291 struct ofp11_match
*om
;
293 if (match_len
!= sizeof *om
|| buf
->size
< sizeof *om
) {
294 return OFPERR_OFPBMC_BAD_LEN
;
296 om
= ofpbuf_pull(buf
, sizeof *om
);
297 if (padded_match_len
) {
298 *padded_match_len
= match_len
;
300 return ofputil_match_from_ofp11_match(om
, match
);
304 if (padded_match_len
) {
305 *padded_match_len
= ROUND_UP(match_len
, 8);
307 return oxm_pull_match(buf
, match
);
310 return OFPERR_OFPBMC_BAD_TYPE
;
314 /* Converts the ofp11_match in 'ofmatch' into a struct match in 'match'.
315 * Returns 0 if successful, otherwise an OFPERR_* value. */
317 ofputil_match_from_ofp11_match(const struct ofp11_match
*ofmatch
,
320 uint16_t wc
= ntohl(ofmatch
->wildcards
);
321 bool ipv4
, arp
, rarp
;
323 match_init_catchall(match
);
325 if (!(wc
& OFPFW11_IN_PORT
)) {
329 error
= ofputil_port_from_ofp11(ofmatch
->in_port
, &ofp_port
);
331 return OFPERR_OFPBMC_BAD_VALUE
;
333 match_set_in_port(match
, ofp_port
);
336 match_set_dl_src_masked(match
, ofmatch
->dl_src
,
337 eth_addr_invert(ofmatch
->dl_src_mask
));
338 match_set_dl_dst_masked(match
, ofmatch
->dl_dst
,
339 eth_addr_invert(ofmatch
->dl_dst_mask
));
341 if (!(wc
& OFPFW11_DL_VLAN
)) {
342 if (ofmatch
->dl_vlan
== htons(OFPVID11_NONE
)) {
343 /* Match only packets without a VLAN tag. */
344 match
->flow
.vlan_tci
= htons(0);
345 match
->wc
.masks
.vlan_tci
= OVS_BE16_MAX
;
347 if (ofmatch
->dl_vlan
== htons(OFPVID11_ANY
)) {
348 /* Match any packet with a VLAN tag regardless of VID. */
349 match
->flow
.vlan_tci
= htons(VLAN_CFI
);
350 match
->wc
.masks
.vlan_tci
= htons(VLAN_CFI
);
351 } else if (ntohs(ofmatch
->dl_vlan
) < 4096) {
352 /* Match only packets with the specified VLAN VID. */
353 match
->flow
.vlan_tci
= htons(VLAN_CFI
) | ofmatch
->dl_vlan
;
354 match
->wc
.masks
.vlan_tci
= htons(VLAN_CFI
| VLAN_VID_MASK
);
357 return OFPERR_OFPBMC_BAD_VALUE
;
360 if (!(wc
& OFPFW11_DL_VLAN_PCP
)) {
361 if (ofmatch
->dl_vlan_pcp
<= 7) {
362 match
->flow
.vlan_tci
|= htons(ofmatch
->dl_vlan_pcp
364 match
->wc
.masks
.vlan_tci
|= htons(VLAN_PCP_MASK
);
367 return OFPERR_OFPBMC_BAD_VALUE
;
373 if (!(wc
& OFPFW11_DL_TYPE
)) {
374 match_set_dl_type(match
,
375 ofputil_dl_type_from_openflow(ofmatch
->dl_type
));
378 ipv4
= match
->flow
.dl_type
== htons(ETH_TYPE_IP
);
379 arp
= match
->flow
.dl_type
== htons(ETH_TYPE_ARP
);
380 rarp
= match
->flow
.dl_type
== htons(ETH_TYPE_RARP
);
382 if (ipv4
&& !(wc
& OFPFW11_NW_TOS
)) {
383 if (ofmatch
->nw_tos
& ~IP_DSCP_MASK
) {
385 return OFPERR_OFPBMC_BAD_VALUE
;
388 match_set_nw_dscp(match
, ofmatch
->nw_tos
);
391 if (ipv4
|| arp
|| rarp
) {
392 if (!(wc
& OFPFW11_NW_PROTO
)) {
393 match_set_nw_proto(match
, ofmatch
->nw_proto
);
395 match_set_nw_src_masked(match
, ofmatch
->nw_src
, ~ofmatch
->nw_src_mask
);
396 match_set_nw_dst_masked(match
, ofmatch
->nw_dst
, ~ofmatch
->nw_dst_mask
);
399 #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
400 if (ipv4
&& (wc
& OFPFW11_TP_ALL
) != OFPFW11_TP_ALL
) {
401 switch (match
->flow
.nw_proto
) {
403 /* "A.2.3 Flow Match Structures" in OF1.1 says:
405 * The tp_src and tp_dst fields will be ignored unless the
406 * network protocol specified is as TCP, UDP or SCTP.
408 * but I'm pretty sure we should support ICMP too, otherwise
409 * that's a regression from OF1.0. */
410 if (!(wc
& OFPFW11_TP_SRC
)) {
411 uint16_t icmp_type
= ntohs(ofmatch
->tp_src
);
412 if (icmp_type
< 0x100) {
413 match_set_icmp_type(match
, icmp_type
);
415 return OFPERR_OFPBMC_BAD_FIELD
;
418 if (!(wc
& OFPFW11_TP_DST
)) {
419 uint16_t icmp_code
= ntohs(ofmatch
->tp_dst
);
420 if (icmp_code
< 0x100) {
421 match_set_icmp_code(match
, icmp_code
);
423 return OFPERR_OFPBMC_BAD_FIELD
;
431 if (!(wc
& (OFPFW11_TP_SRC
))) {
432 match_set_tp_src(match
, ofmatch
->tp_src
);
434 if (!(wc
& (OFPFW11_TP_DST
))) {
435 match_set_tp_dst(match
, ofmatch
->tp_dst
);
440 /* OF1.1 says explicitly to ignore this. */
445 if (eth_type_mpls(match
->flow
.dl_type
)) {
446 if (!(wc
& OFPFW11_MPLS_LABEL
)) {
447 match_set_mpls_label(match
, 0, ofmatch
->mpls_label
);
449 if (!(wc
& OFPFW11_MPLS_TC
)) {
450 match_set_mpls_tc(match
, 0, ofmatch
->mpls_tc
);
454 match_set_metadata_masked(match
, ofmatch
->metadata
,
455 ~ofmatch
->metadata_mask
);
460 /* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */
462 ofputil_match_to_ofp11_match(const struct match
*match
,
463 struct ofp11_match
*ofmatch
)
467 memset(ofmatch
, 0, sizeof *ofmatch
);
468 ofmatch
->omh
.type
= htons(OFPMT_STANDARD
);
469 ofmatch
->omh
.length
= htons(OFPMT11_STANDARD_LENGTH
);
471 if (!match
->wc
.masks
.in_port
.ofp_port
) {
472 wc
|= OFPFW11_IN_PORT
;
474 ofmatch
->in_port
= ofputil_port_to_ofp11(match
->flow
.in_port
.ofp_port
);
477 ofmatch
->dl_src
= match
->flow
.dl_src
;
478 ofmatch
->dl_src_mask
= eth_addr_invert(match
->wc
.masks
.dl_src
);
479 ofmatch
->dl_dst
= match
->flow
.dl_dst
;
480 ofmatch
->dl_dst_mask
= eth_addr_invert(match
->wc
.masks
.dl_dst
);
482 if (match
->wc
.masks
.vlan_tci
== htons(0)) {
483 wc
|= OFPFW11_DL_VLAN
| OFPFW11_DL_VLAN_PCP
;
484 } else if (match
->wc
.masks
.vlan_tci
& htons(VLAN_CFI
)
485 && !(match
->flow
.vlan_tci
& htons(VLAN_CFI
))) {
486 ofmatch
->dl_vlan
= htons(OFPVID11_NONE
);
487 wc
|= OFPFW11_DL_VLAN_PCP
;
489 if (!(match
->wc
.masks
.vlan_tci
& htons(VLAN_VID_MASK
))) {
490 ofmatch
->dl_vlan
= htons(OFPVID11_ANY
);
492 ofmatch
->dl_vlan
= htons(vlan_tci_to_vid(match
->flow
.vlan_tci
));
495 if (!(match
->wc
.masks
.vlan_tci
& htons(VLAN_PCP_MASK
))) {
496 wc
|= OFPFW11_DL_VLAN_PCP
;
498 ofmatch
->dl_vlan_pcp
= vlan_tci_to_pcp(match
->flow
.vlan_tci
);
502 if (!match
->wc
.masks
.dl_type
) {
503 wc
|= OFPFW11_DL_TYPE
;
505 ofmatch
->dl_type
= ofputil_dl_type_to_openflow(match
->flow
.dl_type
);
508 if (!(match
->wc
.masks
.nw_tos
& IP_DSCP_MASK
)) {
509 wc
|= OFPFW11_NW_TOS
;
511 ofmatch
->nw_tos
= match
->flow
.nw_tos
& IP_DSCP_MASK
;
514 if (!match
->wc
.masks
.nw_proto
) {
515 wc
|= OFPFW11_NW_PROTO
;
517 ofmatch
->nw_proto
= match
->flow
.nw_proto
;
520 ofmatch
->nw_src
= match
->flow
.nw_src
;
521 ofmatch
->nw_src_mask
= ~match
->wc
.masks
.nw_src
;
522 ofmatch
->nw_dst
= match
->flow
.nw_dst
;
523 ofmatch
->nw_dst_mask
= ~match
->wc
.masks
.nw_dst
;
525 if (!match
->wc
.masks
.tp_src
) {
526 wc
|= OFPFW11_TP_SRC
;
528 ofmatch
->tp_src
= match
->flow
.tp_src
;
531 if (!match
->wc
.masks
.tp_dst
) {
532 wc
|= OFPFW11_TP_DST
;
534 ofmatch
->tp_dst
= match
->flow
.tp_dst
;
537 if (!(match
->wc
.masks
.mpls_lse
[0] & htonl(MPLS_LABEL_MASK
))) {
538 wc
|= OFPFW11_MPLS_LABEL
;
540 ofmatch
->mpls_label
= htonl(mpls_lse_to_label(
541 match
->flow
.mpls_lse
[0]));
544 if (!(match
->wc
.masks
.mpls_lse
[0] & htonl(MPLS_TC_MASK
))) {
545 wc
|= OFPFW11_MPLS_TC
;
547 ofmatch
->mpls_tc
= mpls_lse_to_tc(match
->flow
.mpls_lse
[0]);
550 ofmatch
->metadata
= match
->flow
.metadata
;
551 ofmatch
->metadata_mask
= ~match
->wc
.masks
.metadata
;
553 ofmatch
->wildcards
= htonl(wc
);
556 /* Returns the "typical" length of a match for 'protocol', for use in
557 * estimating space to preallocate. */
559 ofputil_match_typical_len(enum ofputil_protocol protocol
)
562 case OFPUTIL_P_OF10_STD
:
563 case OFPUTIL_P_OF10_STD_TID
:
564 return sizeof(struct ofp10_match
);
566 case OFPUTIL_P_OF10_NXM
:
567 case OFPUTIL_P_OF10_NXM_TID
:
568 return NXM_TYPICAL_LEN
;
570 case OFPUTIL_P_OF11_STD
:
571 return sizeof(struct ofp11_match
);
573 case OFPUTIL_P_OF12_OXM
:
574 case OFPUTIL_P_OF13_OXM
:
575 case OFPUTIL_P_OF14_OXM
:
576 case OFPUTIL_P_OF15_OXM
:
577 return NXM_TYPICAL_LEN
;
584 /* Appends to 'b' an struct ofp11_match_header followed by a match that
585 * expresses 'match' properly for 'protocol', plus enough zero bytes to pad the
586 * data appended out to a multiple of 8. 'protocol' must be one that is usable
587 * in OpenFlow 1.1 or later.
589 * This function can cause 'b''s data to be reallocated.
591 * Returns the number of bytes appended to 'b', excluding the padding. Never
594 ofputil_put_ofp11_match(struct ofpbuf
*b
, const struct match
*match
,
595 enum ofputil_protocol protocol
)
598 case OFPUTIL_P_OF10_STD
:
599 case OFPUTIL_P_OF10_STD_TID
:
600 case OFPUTIL_P_OF10_NXM
:
601 case OFPUTIL_P_OF10_NXM_TID
:
604 case OFPUTIL_P_OF11_STD
: {
605 struct ofp11_match
*om
;
607 /* Make sure that no padding is needed. */
608 BUILD_ASSERT_DECL(sizeof *om
% 8 == 0);
610 om
= ofpbuf_put_uninit(b
, sizeof *om
);
611 ofputil_match_to_ofp11_match(match
, om
);
615 case OFPUTIL_P_OF12_OXM
:
616 case OFPUTIL_P_OF13_OXM
:
617 case OFPUTIL_P_OF14_OXM
:
618 case OFPUTIL_P_OF15_OXM
:
619 return oxm_put_match(b
, match
,
620 ofputil_protocol_to_ofp_version(protocol
));
626 /* Given a 'dl_type' value in the format used in struct flow, returns the
627 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
630 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type
)
632 return (flow_dl_type
== htons(FLOW_DL_TYPE_NONE
)
633 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE
)
637 /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
638 * structure, returns the corresponding 'dl_type' value for use in struct
641 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type
)
643 return (ofp_dl_type
== htons(OFP_DL_TYPE_NOT_ETH_TYPE
)
644 ? htons(FLOW_DL_TYPE_NONE
)
650 struct proto_abbrev
{
651 enum ofputil_protocol protocol
;
655 /* Most users really don't care about some of the differences between
656 * protocols. These abbreviations help with that. */
657 static const struct proto_abbrev proto_abbrevs
[] = {
658 { OFPUTIL_P_ANY
, "any" },
659 { OFPUTIL_P_OF10_STD_ANY
, "OpenFlow10" },
660 { OFPUTIL_P_OF10_NXM_ANY
, "NXM" },
661 { OFPUTIL_P_ANY_OXM
, "OXM" },
663 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
665 enum ofputil_protocol ofputil_flow_dump_protocols
[] = {
674 size_t ofputil_n_flow_dump_protocols
= ARRAY_SIZE(ofputil_flow_dump_protocols
);
676 /* Returns the set of ofputil_protocols that are supported with the given
677 * OpenFlow 'version'. 'version' should normally be an 8-bit OpenFlow version
678 * identifier (e.g. 0x01 for OpenFlow 1.0, 0x02 for OpenFlow 1.1). Returns 0
679 * if 'version' is not supported or outside the valid range. */
680 enum ofputil_protocol
681 ofputil_protocols_from_ofp_version(enum ofp_version version
)
685 return OFPUTIL_P_OF10_STD_ANY
| OFPUTIL_P_OF10_NXM_ANY
;
687 return OFPUTIL_P_OF11_STD
;
689 return OFPUTIL_P_OF12_OXM
;
691 return OFPUTIL_P_OF13_OXM
;
693 return OFPUTIL_P_OF14_OXM
;
695 return OFPUTIL_P_OF15_OXM
;
701 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
702 * connection that has negotiated the given 'version'. 'version' should
703 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
704 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
705 * outside the valid range. */
706 enum ofputil_protocol
707 ofputil_protocol_from_ofp_version(enum ofp_version version
)
709 return rightmost_1bit(ofputil_protocols_from_ofp_version(version
));
712 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
713 * etc.) that corresponds to 'protocol'. */
715 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol
)
718 case OFPUTIL_P_OF10_STD
:
719 case OFPUTIL_P_OF10_STD_TID
:
720 case OFPUTIL_P_OF10_NXM
:
721 case OFPUTIL_P_OF10_NXM_TID
:
722 return OFP10_VERSION
;
723 case OFPUTIL_P_OF11_STD
:
724 return OFP11_VERSION
;
725 case OFPUTIL_P_OF12_OXM
:
726 return OFP12_VERSION
;
727 case OFPUTIL_P_OF13_OXM
:
728 return OFP13_VERSION
;
729 case OFPUTIL_P_OF14_OXM
:
730 return OFP14_VERSION
;
731 case OFPUTIL_P_OF15_OXM
:
732 return OFP15_VERSION
;
738 /* Returns a bitmap of OpenFlow versions that are supported by at
739 * least one of the 'protocols'. */
741 ofputil_protocols_to_version_bitmap(enum ofputil_protocol protocols
)
745 for (; protocols
; protocols
= zero_rightmost_1bit(protocols
)) {
746 enum ofputil_protocol protocol
= rightmost_1bit(protocols
);
748 bitmap
|= 1u << ofputil_protocol_to_ofp_version(protocol
);
754 /* Returns the set of protocols that are supported on top of the
755 * OpenFlow versions included in 'bitmap'. */
756 enum ofputil_protocol
757 ofputil_protocols_from_version_bitmap(uint32_t bitmap
)
759 enum ofputil_protocol protocols
= 0;
761 for (; bitmap
; bitmap
= zero_rightmost_1bit(bitmap
)) {
762 enum ofp_version version
= rightmost_1bit_idx(bitmap
);
764 protocols
|= ofputil_protocols_from_ofp_version(version
);
770 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
773 ofputil_protocol_is_valid(enum ofputil_protocol protocol
)
775 return protocol
& OFPUTIL_P_ANY
&& is_pow2(protocol
);
778 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
779 * extension turned on or off if 'enable' is true or false, respectively.
781 * This extension is only useful for protocols whose "standard" version does
782 * not allow specific tables to be modified. In particular, this is true of
783 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
784 * specifies a table ID and so there is no need for such an extension. When
785 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
786 * extension, this function just returns its 'protocol' argument unchanged
787 * regardless of the value of 'enable'. */
788 enum ofputil_protocol
789 ofputil_protocol_set_tid(enum ofputil_protocol protocol
, bool enable
)
792 case OFPUTIL_P_OF10_STD
:
793 case OFPUTIL_P_OF10_STD_TID
:
794 return enable
? OFPUTIL_P_OF10_STD_TID
: OFPUTIL_P_OF10_STD
;
796 case OFPUTIL_P_OF10_NXM
:
797 case OFPUTIL_P_OF10_NXM_TID
:
798 return enable
? OFPUTIL_P_OF10_NXM_TID
: OFPUTIL_P_OF10_NXM
;
800 case OFPUTIL_P_OF11_STD
:
801 return OFPUTIL_P_OF11_STD
;
803 case OFPUTIL_P_OF12_OXM
:
804 return OFPUTIL_P_OF12_OXM
;
806 case OFPUTIL_P_OF13_OXM
:
807 return OFPUTIL_P_OF13_OXM
;
809 case OFPUTIL_P_OF14_OXM
:
810 return OFPUTIL_P_OF14_OXM
;
812 case OFPUTIL_P_OF15_OXM
:
813 return OFPUTIL_P_OF15_OXM
;
820 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
821 * some extension to a standard protocol version, the return value is the
822 * standard version of that protocol without any extension. If 'protocol' is a
823 * standard protocol version, returns 'protocol' unchanged. */
824 enum ofputil_protocol
825 ofputil_protocol_to_base(enum ofputil_protocol protocol
)
827 return ofputil_protocol_set_tid(protocol
, false);
830 /* Returns 'new_base' with any extensions taken from 'cur'. */
831 enum ofputil_protocol
832 ofputil_protocol_set_base(enum ofputil_protocol cur
,
833 enum ofputil_protocol new_base
)
835 bool tid
= (cur
& OFPUTIL_P_TID
) != 0;
838 case OFPUTIL_P_OF10_STD
:
839 case OFPUTIL_P_OF10_STD_TID
:
840 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD
, tid
);
842 case OFPUTIL_P_OF10_NXM
:
843 case OFPUTIL_P_OF10_NXM_TID
:
844 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM
, tid
);
846 case OFPUTIL_P_OF11_STD
:
847 return ofputil_protocol_set_tid(OFPUTIL_P_OF11_STD
, tid
);
849 case OFPUTIL_P_OF12_OXM
:
850 return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM
, tid
);
852 case OFPUTIL_P_OF13_OXM
:
853 return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM
, tid
);
855 case OFPUTIL_P_OF14_OXM
:
856 return ofputil_protocol_set_tid(OFPUTIL_P_OF14_OXM
, tid
);
858 case OFPUTIL_P_OF15_OXM
:
859 return ofputil_protocol_set_tid(OFPUTIL_P_OF15_OXM
, tid
);
866 /* Returns a string form of 'protocol', if a simple form exists (that is, if
867 * 'protocol' is either a single protocol or it is a combination of protocols
868 * that have a single abbreviation). Otherwise, returns NULL. */
870 ofputil_protocol_to_string(enum ofputil_protocol protocol
)
872 const struct proto_abbrev
*p
;
874 /* Use a "switch" statement for single-bit names so that we get a compiler
875 * warning if we forget any. */
877 case OFPUTIL_P_OF10_NXM
:
878 return "NXM-table_id";
880 case OFPUTIL_P_OF10_NXM_TID
:
881 return "NXM+table_id";
883 case OFPUTIL_P_OF10_STD
:
884 return "OpenFlow10-table_id";
886 case OFPUTIL_P_OF10_STD_TID
:
887 return "OpenFlow10+table_id";
889 case OFPUTIL_P_OF11_STD
:
892 case OFPUTIL_P_OF12_OXM
:
893 return "OXM-OpenFlow12";
895 case OFPUTIL_P_OF13_OXM
:
896 return "OXM-OpenFlow13";
898 case OFPUTIL_P_OF14_OXM
:
899 return "OXM-OpenFlow14";
901 case OFPUTIL_P_OF15_OXM
:
902 return "OXM-OpenFlow15";
905 /* Check abbreviations. */
906 for (p
= proto_abbrevs
; p
< &proto_abbrevs
[N_PROTO_ABBREVS
]; p
++) {
907 if (protocol
== p
->protocol
) {
915 /* Returns a string that represents 'protocols'. The return value might be a
916 * comma-separated list if 'protocols' doesn't have a simple name. The return
917 * value is "none" if 'protocols' is 0.
919 * The caller must free the returned string (with free()). */
921 ofputil_protocols_to_string(enum ofputil_protocol protocols
)
925 ovs_assert(!(protocols
& ~OFPUTIL_P_ANY
));
926 if (protocols
== 0) {
927 return xstrdup("none");
932 const struct proto_abbrev
*p
;
936 ds_put_char(&s
, ',');
939 for (p
= proto_abbrevs
; p
< &proto_abbrevs
[N_PROTO_ABBREVS
]; p
++) {
940 if ((protocols
& p
->protocol
) == p
->protocol
) {
941 ds_put_cstr(&s
, p
->name
);
942 protocols
&= ~p
->protocol
;
947 for (i
= 0; i
< CHAR_BIT
* sizeof(enum ofputil_protocol
); i
++) {
948 enum ofputil_protocol bit
= 1u << i
;
950 if (protocols
& bit
) {
951 ds_put_cstr(&s
, ofputil_protocol_to_string(bit
));
960 return ds_steal_cstr(&s
);
963 static enum ofputil_protocol
964 ofputil_protocol_from_string__(const char *s
, size_t n
)
966 const struct proto_abbrev
*p
;
969 for (i
= 0; i
< CHAR_BIT
* sizeof(enum ofputil_protocol
); i
++) {
970 enum ofputil_protocol bit
= 1u << i
;
971 const char *name
= ofputil_protocol_to_string(bit
);
973 if (name
&& n
== strlen(name
) && !strncasecmp(s
, name
, n
)) {
978 for (p
= proto_abbrevs
; p
< &proto_abbrevs
[N_PROTO_ABBREVS
]; p
++) {
979 if (n
== strlen(p
->name
) && !strncasecmp(s
, p
->name
, n
)) {
987 /* Returns the nonempty set of protocols represented by 's', which can be a
988 * single protocol name or abbreviation or a comma-separated list of them.
990 * Aborts the program with an error message if 's' is invalid. */
991 enum ofputil_protocol
992 ofputil_protocols_from_string(const char *s
)
994 const char *orig_s
= s
;
995 enum ofputil_protocol protocols
;
999 enum ofputil_protocol p
;
1002 n
= strcspn(s
, ",");
1008 p
= ofputil_protocol_from_string__(s
, n
);
1010 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n
, s
);
1018 ovs_fatal(0, "%s: no flow protocol specified", orig_s
);
1024 ofputil_version_from_string(const char *s
)
1026 if (!strcasecmp(s
, "OpenFlow10")) {
1027 return OFP10_VERSION
;
1029 if (!strcasecmp(s
, "OpenFlow11")) {
1030 return OFP11_VERSION
;
1032 if (!strcasecmp(s
, "OpenFlow12")) {
1033 return OFP12_VERSION
;
1035 if (!strcasecmp(s
, "OpenFlow13")) {
1036 return OFP13_VERSION
;
1038 if (!strcasecmp(s
, "OpenFlow14")) {
1039 return OFP14_VERSION
;
1041 if (!strcasecmp(s
, "OpenFlow15")) {
1042 return OFP15_VERSION
;
1048 is_delimiter(unsigned char c
)
1050 return isspace(c
) || c
== ',';
1054 ofputil_versions_from_string(const char *s
)
1057 uint32_t bitmap
= 0;
1064 if (is_delimiter(s
[i
])) {
1069 while (s
[i
+ j
] && !is_delimiter(s
[i
+ j
])) {
1072 key
= xmemdup0(s
+ i
, j
);
1073 version
= ofputil_version_from_string(key
);
1075 VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key
);
1078 bitmap
|= 1u << version
;
1086 ofputil_versions_from_strings(char ** const s
, size_t count
)
1088 uint32_t bitmap
= 0;
1091 int version
= ofputil_version_from_string(s
[count
]);
1093 VLOG_WARN("Unknown OpenFlow version: \"%s\"", s
[count
]);
1095 bitmap
|= 1u << version
;
1103 ofputil_version_to_string(enum ofp_version ofp_version
)
1105 switch (ofp_version
) {
1107 return "OpenFlow10";
1109 return "OpenFlow11";
1111 return "OpenFlow12";
1113 return "OpenFlow13";
1115 return "OpenFlow14";
1117 return "OpenFlow15";
1124 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format
)
1126 switch (packet_in_format
) {
1127 case NXPIF_OPENFLOW10
:
1136 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format
)
1138 switch (packet_in_format
) {
1139 case NXPIF_OPENFLOW10
:
1140 return "openflow10";
1149 ofputil_packet_in_format_from_string(const char *s
)
1151 return (!strcmp(s
, "openflow10") ? NXPIF_OPENFLOW10
1152 : !strcmp(s
, "nxm") ? NXPIF_NXM
1157 ofputil_format_version(struct ds
*msg
, enum ofp_version version
)
1159 ds_put_format(msg
, "0x%02x", version
);
1163 ofputil_format_version_name(struct ds
*msg
, enum ofp_version version
)
1165 ds_put_cstr(msg
, ofputil_version_to_string(version
));
1169 ofputil_format_version_bitmap__(struct ds
*msg
, uint32_t bitmap
,
1170 void (*format_version
)(struct ds
*msg
,
1174 format_version(msg
, raw_ctz(bitmap
));
1175 bitmap
= zero_rightmost_1bit(bitmap
);
1177 ds_put_cstr(msg
, ", ");
1183 ofputil_format_version_bitmap(struct ds
*msg
, uint32_t bitmap
)
1185 ofputil_format_version_bitmap__(msg
, bitmap
, ofputil_format_version
);
1189 ofputil_format_version_bitmap_names(struct ds
*msg
, uint32_t bitmap
)
1191 ofputil_format_version_bitmap__(msg
, bitmap
, ofputil_format_version_name
);
1195 ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header
*oheh
,
1196 uint32_t *allowed_versionsp
)
1198 uint16_t bitmap_len
= ntohs(oheh
->length
) - sizeof *oheh
;
1199 const ovs_be32
*bitmap
= ALIGNED_CAST(const ovs_be32
*, oheh
+ 1);
1200 uint32_t allowed_versions
;
1202 if (!bitmap_len
|| bitmap_len
% sizeof *bitmap
) {
1206 /* Only use the first 32-bit element of the bitmap as that is all the
1207 * current implementation supports. Subsequent elements are ignored which
1208 * should have no effect on session negotiation until Open vSwitch supports
1209 * wire-protocol versions greater than 31.
1211 allowed_versions
= ntohl(bitmap
[0]);
1213 if (allowed_versions
& 1) {
1214 /* There's no OpenFlow version 0. */
1215 VLOG_WARN_RL(&bad_ofmsg_rl
, "peer claims to support invalid OpenFlow "
1217 allowed_versions
&= ~1u;
1220 if (!allowed_versions
) {
1221 VLOG_WARN_RL(&bad_ofmsg_rl
, "peer does not support any OpenFlow "
1222 "version (between 0x01 and 0x1f)");
1226 *allowed_versionsp
= allowed_versions
;
1231 version_bitmap_from_version(uint8_t ofp_version
)
1233 return ((ofp_version
< 32 ? 1u << ofp_version
: 0) - 1) << 1;
1236 /* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions'
1237 * the set of OpenFlow versions for which 'oh' announces support.
1239 * Because of how OpenFlow defines OFPT_HELLO messages, this function is always
1240 * successful, and thus '*allowed_versions' is always initialized. However, it
1241 * returns false if 'oh' contains some data that could not be fully understood,
1242 * true if 'oh' was completely parsed. */
1244 ofputil_decode_hello(const struct ofp_header
*oh
, uint32_t *allowed_versions
)
1249 ofpbuf_use_const(&msg
, oh
, ntohs(oh
->length
));
1250 ofpbuf_pull(&msg
, sizeof *oh
);
1252 *allowed_versions
= version_bitmap_from_version(oh
->version
);
1254 const struct ofp_hello_elem_header
*oheh
;
1257 if (msg
.size
< sizeof *oheh
) {
1262 len
= ntohs(oheh
->length
);
1263 if (len
< sizeof *oheh
|| !ofpbuf_try_pull(&msg
, ROUND_UP(len
, 8))) {
1267 if (oheh
->type
!= htons(OFPHET_VERSIONBITMAP
)
1268 || !ofputil_decode_hello_bitmap(oheh
, allowed_versions
)) {
1276 /* Returns true if 'allowed_versions' needs to be accompanied by a version
1277 * bitmap to be correctly expressed in an OFPT_HELLO message. */
1279 should_send_version_bitmap(uint32_t allowed_versions
)
1281 return !is_pow2((allowed_versions
>> 1) + 1);
1284 /* Create an OFPT_HELLO message that expresses support for the OpenFlow
1285 * versions in the 'allowed_versions' bitmaps and returns the message. */
1287 ofputil_encode_hello(uint32_t allowed_versions
)
1289 enum ofp_version ofp_version
;
1292 ofp_version
= leftmost_1bit_idx(allowed_versions
);
1293 msg
= ofpraw_alloc(OFPRAW_OFPT_HELLO
, ofp_version
, 0);
1295 if (should_send_version_bitmap(allowed_versions
)) {
1296 struct ofp_hello_elem_header
*oheh
;
1299 map_len
= sizeof allowed_versions
;
1300 oheh
= ofpbuf_put_zeros(msg
, ROUND_UP(map_len
+ sizeof *oheh
, 8));
1301 oheh
->type
= htons(OFPHET_VERSIONBITMAP
);
1302 oheh
->length
= htons(map_len
+ sizeof *oheh
);
1303 *ALIGNED_CAST(ovs_be32
*, oheh
+ 1) = htonl(allowed_versions
);
1305 ofpmsg_update_length(msg
);
1311 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1312 * protocol is 'current', at least partly transitions the protocol to 'want'.
1313 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1314 * connection if the switch processes the returned message correctly. (If
1315 * '*next != want' then the caller will have to iterate.)
1317 * If 'current == want', or if it is not possible to transition from 'current'
1318 * to 'want' (because, for example, 'current' and 'want' use different OpenFlow
1319 * protocol versions), returns NULL and stores 'current' in '*next'. */
1321 ofputil_encode_set_protocol(enum ofputil_protocol current
,
1322 enum ofputil_protocol want
,
1323 enum ofputil_protocol
*next
)
1325 enum ofp_version cur_version
, want_version
;
1326 enum ofputil_protocol cur_base
, want_base
;
1327 bool cur_tid
, want_tid
;
1329 cur_version
= ofputil_protocol_to_ofp_version(current
);
1330 want_version
= ofputil_protocol_to_ofp_version(want
);
1331 if (cur_version
!= want_version
) {
1336 cur_base
= ofputil_protocol_to_base(current
);
1337 want_base
= ofputil_protocol_to_base(want
);
1338 if (cur_base
!= want_base
) {
1339 *next
= ofputil_protocol_set_base(current
, want_base
);
1341 switch (want_base
) {
1342 case OFPUTIL_P_OF10_NXM
:
1343 return ofputil_encode_nx_set_flow_format(NXFF_NXM
);
1345 case OFPUTIL_P_OF10_STD
:
1346 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10
);
1348 case OFPUTIL_P_OF11_STD
:
1349 case OFPUTIL_P_OF12_OXM
:
1350 case OFPUTIL_P_OF13_OXM
:
1351 case OFPUTIL_P_OF14_OXM
:
1352 case OFPUTIL_P_OF15_OXM
:
1353 /* There is only one variant of each OpenFlow 1.1+ protocol, and we
1354 * verified above that we're not trying to change versions. */
1357 case OFPUTIL_P_OF10_STD_TID
:
1358 case OFPUTIL_P_OF10_NXM_TID
:
1363 cur_tid
= (current
& OFPUTIL_P_TID
) != 0;
1364 want_tid
= (want
& OFPUTIL_P_TID
) != 0;
1365 if (cur_tid
!= want_tid
) {
1366 *next
= ofputil_protocol_set_tid(current
, want_tid
);
1367 return ofputil_make_flow_mod_table_id(want_tid
);
1370 ovs_assert(current
== want
);
1376 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1377 * format to 'nxff'. */
1379 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff
)
1381 struct nx_set_flow_format
*sff
;
1384 ovs_assert(ofputil_nx_flow_format_is_valid(nxff
));
1386 msg
= ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT
, OFP10_VERSION
, 0);
1387 sff
= ofpbuf_put_zeros(msg
, sizeof *sff
);
1388 sff
->format
= htonl(nxff
);
1393 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1395 enum ofputil_protocol
1396 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format
)
1398 switch (flow_format
) {
1399 case NXFF_OPENFLOW10
:
1400 return OFPUTIL_P_OF10_STD
;
1403 return OFPUTIL_P_OF10_NXM
;
1410 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1412 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format
)
1414 return ofputil_nx_flow_format_to_protocol(flow_format
) != 0;
1417 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1420 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format
)
1422 switch (flow_format
) {
1423 case NXFF_OPENFLOW10
:
1424 return "openflow10";
1433 ofputil_make_set_packet_in_format(enum ofp_version ofp_version
,
1434 enum nx_packet_in_format packet_in_format
)
1436 struct nx_set_packet_in_format
*spif
;
1439 msg
= ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT
, ofp_version
, 0);
1440 spif
= ofpbuf_put_zeros(msg
, sizeof *spif
);
1441 spif
->format
= htonl(packet_in_format
);
1446 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1447 * extension on or off (according to 'flow_mod_table_id'). */
1449 ofputil_make_flow_mod_table_id(bool flow_mod_table_id
)
1451 struct nx_flow_mod_table_id
*nfmti
;
1454 msg
= ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID
, OFP10_VERSION
, 0);
1455 nfmti
= ofpbuf_put_zeros(msg
, sizeof *nfmti
);
1456 nfmti
->set
= flow_mod_table_id
;
1460 struct ofputil_flow_mod_flag
{
1462 enum ofp_version min_version
, max_version
;
1463 enum ofputil_flow_mod_flags flag
;
1466 static const struct ofputil_flow_mod_flag ofputil_flow_mod_flags
[] = {
1467 { OFPFF_SEND_FLOW_REM
, OFP10_VERSION
, 0, OFPUTIL_FF_SEND_FLOW_REM
},
1468 { OFPFF_CHECK_OVERLAP
, OFP10_VERSION
, 0, OFPUTIL_FF_CHECK_OVERLAP
},
1469 { OFPFF10_EMERG
, OFP10_VERSION
, OFP10_VERSION
,
1471 { OFPFF12_RESET_COUNTS
, OFP12_VERSION
, 0, OFPUTIL_FF_RESET_COUNTS
},
1472 { OFPFF13_NO_PKT_COUNTS
, OFP13_VERSION
, 0, OFPUTIL_FF_NO_PKT_COUNTS
},
1473 { OFPFF13_NO_BYT_COUNTS
, OFP13_VERSION
, 0, OFPUTIL_FF_NO_BYT_COUNTS
},
1478 ofputil_decode_flow_mod_flags(ovs_be16 raw_flags_
,
1479 enum ofp_flow_mod_command command
,
1480 enum ofp_version version
,
1481 enum ofputil_flow_mod_flags
*flagsp
)
1483 uint16_t raw_flags
= ntohs(raw_flags_
);
1484 const struct ofputil_flow_mod_flag
*f
;
1487 for (f
= ofputil_flow_mod_flags
; f
->raw_flag
; f
++) {
1488 if (raw_flags
& f
->raw_flag
1489 && version
>= f
->min_version
1490 && (!f
->max_version
|| version
<= f
->max_version
)) {
1491 raw_flags
&= ~f
->raw_flag
;
1496 /* In OF1.0 and OF1.1, "add" always resets counters, and other commands
1499 * In OF1.2 and later, OFPFF12_RESET_COUNTS controls whether each command
1500 * resets counters. */
1501 if ((version
== OFP10_VERSION
|| version
== OFP11_VERSION
)
1502 && command
== OFPFC_ADD
) {
1503 *flagsp
|= OFPUTIL_FF_RESET_COUNTS
;
1506 return raw_flags
? OFPERR_OFPFMFC_BAD_FLAGS
: 0;
1510 ofputil_encode_flow_mod_flags(enum ofputil_flow_mod_flags flags
,
1511 enum ofp_version version
)
1513 const struct ofputil_flow_mod_flag
*f
;
1517 for (f
= ofputil_flow_mod_flags
; f
->raw_flag
; f
++) {
1519 && version
>= f
->min_version
1520 && (!f
->max_version
|| version
<= f
->max_version
)) {
1521 raw_flags
|= f
->raw_flag
;
1525 return htons(raw_flags
);
1528 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1529 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1532 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1533 * The caller must initialize 'ofpacts' and retains ownership of it.
1534 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1536 * Does not validate the flow_mod actions. The caller should do that, with
1537 * ofpacts_check(). */
1539 ofputil_decode_flow_mod(struct ofputil_flow_mod
*fm
,
1540 const struct ofp_header
*oh
,
1541 enum ofputil_protocol protocol
,
1542 struct ofpbuf
*ofpacts
,
1543 ofp_port_t max_port
, uint8_t max_table
)
1550 /* Ignored for non-delete actions */
1551 fm
->delete_reason
= OFPRR_DELETE
;
1553 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
1554 raw
= ofpraw_pull_assert(&b
);
1555 if (raw
== OFPRAW_OFPT11_FLOW_MOD
) {
1556 /* Standard OpenFlow 1.1+ flow_mod. */
1557 const struct ofp11_flow_mod
*ofm
;
1559 ofm
= ofpbuf_pull(&b
, sizeof *ofm
);
1561 error
= ofputil_pull_ofp11_match(&b
, &fm
->match
, NULL
);
1566 /* Translate the message. */
1567 fm
->priority
= ntohs(ofm
->priority
);
1568 if (ofm
->command
== OFPFC_ADD
1569 || (oh
->version
== OFP11_VERSION
1570 && (ofm
->command
== OFPFC_MODIFY
||
1571 ofm
->command
== OFPFC_MODIFY_STRICT
)
1572 && ofm
->cookie_mask
== htonll(0))) {
1573 /* In OpenFlow 1.1 only, a "modify" or "modify-strict" that does
1574 * not match on the cookie is treated as an "add" if there is no
1576 fm
->cookie
= htonll(0);
1577 fm
->cookie_mask
= htonll(0);
1578 fm
->new_cookie
= ofm
->cookie
;
1580 fm
->cookie
= ofm
->cookie
;
1581 fm
->cookie_mask
= ofm
->cookie_mask
;
1582 fm
->new_cookie
= OVS_BE64_MAX
;
1584 fm
->modify_cookie
= false;
1585 fm
->command
= ofm
->command
;
1589 * OF1.1 entirely forbids table_id == OFPTT_ALL.
1590 * OF1.2+ allows table_id == OFPTT_ALL only for deletes. */
1591 fm
->table_id
= ofm
->table_id
;
1592 if (fm
->table_id
== OFPTT_ALL
1593 && (oh
->version
== OFP11_VERSION
1594 || (ofm
->command
!= OFPFC_DELETE
&&
1595 ofm
->command
!= OFPFC_DELETE_STRICT
))) {
1596 return OFPERR_OFPFMFC_BAD_TABLE_ID
;
1599 fm
->idle_timeout
= ntohs(ofm
->idle_timeout
);
1600 fm
->hard_timeout
= ntohs(ofm
->hard_timeout
);
1601 if (oh
->version
>= OFP14_VERSION
&& ofm
->command
== OFPFC_ADD
) {
1602 fm
->importance
= ntohs(ofm
->importance
);
1606 fm
->buffer_id
= ntohl(ofm
->buffer_id
);
1607 error
= ofputil_port_from_ofp11(ofm
->out_port
, &fm
->out_port
);
1612 fm
->out_group
= (ofm
->command
== OFPFC_DELETE
||
1613 ofm
->command
== OFPFC_DELETE_STRICT
1614 ? ntohl(ofm
->out_group
)
1616 raw_flags
= ofm
->flags
;
1620 if (raw
== OFPRAW_OFPT10_FLOW_MOD
) {
1621 /* Standard OpenFlow 1.0 flow_mod. */
1622 const struct ofp10_flow_mod
*ofm
;
1624 /* Get the ofp10_flow_mod. */
1625 ofm
= ofpbuf_pull(&b
, sizeof *ofm
);
1627 /* Translate the rule. */
1628 ofputil_match_from_ofp10_match(&ofm
->match
, &fm
->match
);
1629 ofputil_normalize_match(&fm
->match
);
1631 /* OpenFlow 1.0 says that exact-match rules have to have the
1632 * highest possible priority. */
1633 fm
->priority
= (ofm
->match
.wildcards
& htonl(OFPFW10_ALL
)
1634 ? ntohs(ofm
->priority
)
1637 /* Translate the message. */
1638 command
= ntohs(ofm
->command
);
1639 fm
->cookie
= htonll(0);
1640 fm
->cookie_mask
= htonll(0);
1641 fm
->new_cookie
= ofm
->cookie
;
1642 fm
->idle_timeout
= ntohs(ofm
->idle_timeout
);
1643 fm
->hard_timeout
= ntohs(ofm
->hard_timeout
);
1645 fm
->buffer_id
= ntohl(ofm
->buffer_id
);
1646 fm
->out_port
= u16_to_ofp(ntohs(ofm
->out_port
));
1647 fm
->out_group
= OFPG_ANY
;
1648 raw_flags
= ofm
->flags
;
1649 } else if (raw
== OFPRAW_NXT_FLOW_MOD
) {
1650 /* Nicira extended flow_mod. */
1651 const struct nx_flow_mod
*nfm
;
1653 /* Dissect the message. */
1654 nfm
= ofpbuf_pull(&b
, sizeof *nfm
);
1655 error
= nx_pull_match(&b
, ntohs(nfm
->match_len
),
1656 &fm
->match
, &fm
->cookie
, &fm
->cookie_mask
);
1661 /* Translate the message. */
1662 command
= ntohs(nfm
->command
);
1663 if ((command
& 0xff) == OFPFC_ADD
&& fm
->cookie_mask
) {
1664 /* Flow additions may only set a new cookie, not match an
1665 * existing cookie. */
1666 return OFPERR_NXBRC_NXM_INVALID
;
1668 fm
->priority
= ntohs(nfm
->priority
);
1669 fm
->new_cookie
= nfm
->cookie
;
1670 fm
->idle_timeout
= ntohs(nfm
->idle_timeout
);
1671 fm
->hard_timeout
= ntohs(nfm
->hard_timeout
);
1673 fm
->buffer_id
= ntohl(nfm
->buffer_id
);
1674 fm
->out_port
= u16_to_ofp(ntohs(nfm
->out_port
));
1675 fm
->out_group
= OFPG_ANY
;
1676 raw_flags
= nfm
->flags
;
1681 fm
->modify_cookie
= fm
->new_cookie
!= OVS_BE64_MAX
;
1682 if (protocol
& OFPUTIL_P_TID
) {
1683 fm
->command
= command
& 0xff;
1684 fm
->table_id
= command
>> 8;
1686 if (command
> 0xff) {
1687 VLOG_WARN_RL(&bad_ofmsg_rl
, "flow_mod has explicit table_id "
1688 "but flow_mod_table_id extension is not enabled");
1690 fm
->command
= command
;
1691 fm
->table_id
= 0xff;
1695 if (fm
->command
> OFPFC_DELETE_STRICT
) {
1696 return OFPERR_OFPFMFC_BAD_COMMAND
;
1699 error
= ofpacts_pull_openflow_instructions(&b
, b
.size
,
1700 oh
->version
, ofpacts
);
1704 fm
->ofpacts
= ofpacts
->data
;
1705 fm
->ofpacts_len
= ofpacts
->size
;
1707 error
= ofputil_decode_flow_mod_flags(raw_flags
, fm
->command
,
1708 oh
->version
, &fm
->flags
);
1713 if (fm
->flags
& OFPUTIL_FF_EMERG
) {
1714 /* We do not support the OpenFlow 1.0 emergency flow cache, which
1715 * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
1717 * OpenFlow 1.0 specifies the error code to use when idle_timeout
1718 * or hard_timeout is nonzero. Otherwise, there is no good error
1719 * code, so just state that the flow table is full. */
1720 return (fm
->hard_timeout
|| fm
->idle_timeout
1721 ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT
1722 : OFPERR_OFPFMFC_TABLE_FULL
);
1725 return ofpacts_check_consistency(fm
->ofpacts
, fm
->ofpacts_len
,
1726 &fm
->match
.flow
, max_port
,
1727 fm
->table_id
, max_table
, protocol
);
1731 ofputil_pull_bands(struct ofpbuf
*msg
, size_t len
, uint16_t *n_bands
,
1732 struct ofpbuf
*bands
)
1734 const struct ofp13_meter_band_header
*ombh
;
1735 struct ofputil_meter_band
*mb
;
1738 ombh
= ofpbuf_try_pull(msg
, len
);
1740 return OFPERR_OFPBRC_BAD_LEN
;
1743 while (len
>= sizeof (struct ofp13_meter_band_drop
)) {
1744 size_t ombh_len
= ntohs(ombh
->len
);
1745 /* All supported band types have the same length. */
1746 if (ombh_len
!= sizeof (struct ofp13_meter_band_drop
)) {
1747 return OFPERR_OFPBRC_BAD_LEN
;
1749 mb
= ofpbuf_put_uninit(bands
, sizeof *mb
);
1750 mb
->type
= ntohs(ombh
->type
);
1751 if (mb
->type
!= OFPMBT13_DROP
&& mb
->type
!= OFPMBT13_DSCP_REMARK
) {
1752 return OFPERR_OFPMMFC_BAD_BAND
;
1754 mb
->rate
= ntohl(ombh
->rate
);
1755 mb
->burst_size
= ntohl(ombh
->burst_size
);
1756 mb
->prec_level
= (mb
->type
== OFPMBT13_DSCP_REMARK
) ?
1757 ((struct ofp13_meter_band_dscp_remark
*)ombh
)->prec_level
: 0;
1760 ombh
= ALIGNED_CAST(struct ofp13_meter_band_header
*,
1761 (char *) ombh
+ ombh_len
);
1764 return OFPERR_OFPBRC_BAD_LEN
;
1771 ofputil_decode_meter_mod(const struct ofp_header
*oh
,
1772 struct ofputil_meter_mod
*mm
,
1773 struct ofpbuf
*bands
)
1775 const struct ofp13_meter_mod
*omm
;
1778 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
1779 ofpraw_pull_assert(&b
);
1780 omm
= ofpbuf_pull(&b
, sizeof *omm
);
1782 /* Translate the message. */
1783 mm
->command
= ntohs(omm
->command
);
1784 if (mm
->command
!= OFPMC13_ADD
&&
1785 mm
->command
!= OFPMC13_MODIFY
&&
1786 mm
->command
!= OFPMC13_DELETE
) {
1787 return OFPERR_OFPMMFC_BAD_COMMAND
;
1789 mm
->meter
.meter_id
= ntohl(omm
->meter_id
);
1791 if (mm
->command
== OFPMC13_DELETE
) {
1792 mm
->meter
.flags
= 0;
1793 mm
->meter
.n_bands
= 0;
1794 mm
->meter
.bands
= NULL
;
1798 mm
->meter
.flags
= ntohs(omm
->flags
);
1799 if (mm
->meter
.flags
& OFPMF13_KBPS
&&
1800 mm
->meter
.flags
& OFPMF13_PKTPS
) {
1801 return OFPERR_OFPMMFC_BAD_FLAGS
;
1803 mm
->meter
.bands
= bands
->data
;
1805 error
= ofputil_pull_bands(&b
, b
.size
, &mm
->meter
.n_bands
, bands
);
1814 ofputil_decode_meter_request(const struct ofp_header
*oh
, uint32_t *meter_id
)
1816 const struct ofp13_meter_multipart_request
*omr
= ofpmsg_body(oh
);
1817 *meter_id
= ntohl(omr
->meter_id
);
1821 ofputil_encode_meter_request(enum ofp_version ofp_version
,
1822 enum ofputil_meter_request_type type
,
1830 case OFPUTIL_METER_CONFIG
:
1831 raw
= OFPRAW_OFPST13_METER_CONFIG_REQUEST
;
1833 case OFPUTIL_METER_STATS
:
1834 raw
= OFPRAW_OFPST13_METER_REQUEST
;
1837 case OFPUTIL_METER_FEATURES
:
1838 raw
= OFPRAW_OFPST13_METER_FEATURES_REQUEST
;
1842 msg
= ofpraw_alloc(raw
, ofp_version
, 0);
1844 if (type
!= OFPUTIL_METER_FEATURES
) {
1845 struct ofp13_meter_multipart_request
*omr
;
1846 omr
= ofpbuf_put_zeros(msg
, sizeof *omr
);
1847 omr
->meter_id
= htonl(meter_id
);
1853 ofputil_put_bands(uint16_t n_bands
, const struct ofputil_meter_band
*mb
,
1858 for (n
= 0; n
< n_bands
; ++n
) {
1859 /* Currently all band types have same size. */
1860 struct ofp13_meter_band_dscp_remark
*ombh
;
1861 size_t ombh_len
= sizeof *ombh
;
1863 ombh
= ofpbuf_put_zeros(msg
, ombh_len
);
1865 ombh
->type
= htons(mb
->type
);
1866 ombh
->len
= htons(ombh_len
);
1867 ombh
->rate
= htonl(mb
->rate
);
1868 ombh
->burst_size
= htonl(mb
->burst_size
);
1869 ombh
->prec_level
= mb
->prec_level
;
1875 /* Encode a meter stat for 'mc' and append it to 'replies'. */
1877 ofputil_append_meter_config(struct ovs_list
*replies
,
1878 const struct ofputil_meter_config
*mc
)
1880 struct ofpbuf
*msg
= ofpbuf_from_list(list_back(replies
));
1881 size_t start_ofs
= msg
->size
;
1882 struct ofp13_meter_config
*reply
;
1884 ofpbuf_put_uninit(msg
, sizeof *reply
);
1885 ofputil_put_bands(mc
->n_bands
, mc
->bands
, msg
);
1887 reply
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *reply
);
1888 reply
->flags
= htons(mc
->flags
);
1889 reply
->meter_id
= htonl(mc
->meter_id
);
1890 reply
->length
= htons(msg
->size
- start_ofs
);
1892 ofpmp_postappend(replies
, start_ofs
);
1895 /* Encode a meter stat for 'ms' and append it to 'replies'. */
1897 ofputil_append_meter_stats(struct ovs_list
*replies
,
1898 const struct ofputil_meter_stats
*ms
)
1900 struct ofp13_meter_stats
*reply
;
1904 len
= sizeof *reply
+ ms
->n_bands
* sizeof(struct ofp13_meter_band_stats
);
1905 reply
= ofpmp_append(replies
, len
);
1907 reply
->meter_id
= htonl(ms
->meter_id
);
1908 reply
->len
= htons(len
);
1909 memset(reply
->pad
, 0, sizeof reply
->pad
);
1910 reply
->flow_count
= htonl(ms
->flow_count
);
1911 reply
->packet_in_count
= htonll(ms
->packet_in_count
);
1912 reply
->byte_in_count
= htonll(ms
->byte_in_count
);
1913 reply
->duration_sec
= htonl(ms
->duration_sec
);
1914 reply
->duration_nsec
= htonl(ms
->duration_nsec
);
1916 for (n
= 0; n
< ms
->n_bands
; ++n
) {
1917 const struct ofputil_meter_band_stats
*src
= &ms
->bands
[n
];
1918 struct ofp13_meter_band_stats
*dst
= &reply
->band_stats
[n
];
1920 dst
->packet_band_count
= htonll(src
->packet_count
);
1921 dst
->byte_band_count
= htonll(src
->byte_count
);
1925 /* Converts an OFPMP_METER_CONFIG reply in 'msg' into an abstract
1926 * ofputil_meter_config in 'mc', with mc->bands pointing to bands decoded into
1927 * 'bands'. The caller must have initialized 'bands' and retains ownership of
1928 * it across the call.
1930 * Multiple OFPST13_METER_CONFIG replies can be packed into a single OpenFlow
1931 * message. Calling this function multiple times for a single 'msg' iterates
1932 * through the replies. 'bands' is cleared for each reply.
1934 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1935 * otherwise a positive errno value. */
1937 ofputil_decode_meter_config(struct ofpbuf
*msg
,
1938 struct ofputil_meter_config
*mc
,
1939 struct ofpbuf
*bands
)
1941 const struct ofp13_meter_config
*omc
;
1944 /* Pull OpenFlow headers for the first call. */
1946 ofpraw_pull_assert(msg
);
1953 omc
= ofpbuf_try_pull(msg
, sizeof *omc
);
1955 VLOG_WARN_RL(&bad_ofmsg_rl
,
1956 "OFPMP_METER_CONFIG reply has %"PRIu32
" leftover bytes at end",
1958 return OFPERR_OFPBRC_BAD_LEN
;
1961 ofpbuf_clear(bands
);
1962 err
= ofputil_pull_bands(msg
, ntohs(omc
->length
) - sizeof *omc
,
1963 &mc
->n_bands
, bands
);
1967 mc
->meter_id
= ntohl(omc
->meter_id
);
1968 mc
->flags
= ntohs(omc
->flags
);
1969 mc
->bands
= bands
->data
;
1975 ofputil_pull_band_stats(struct ofpbuf
*msg
, size_t len
, uint16_t *n_bands
,
1976 struct ofpbuf
*bands
)
1978 const struct ofp13_meter_band_stats
*ombs
;
1979 struct ofputil_meter_band_stats
*mbs
;
1982 ombs
= ofpbuf_try_pull(msg
, len
);
1984 return OFPERR_OFPBRC_BAD_LEN
;
1987 n
= len
/ sizeof *ombs
;
1988 if (len
!= n
* sizeof *ombs
) {
1989 return OFPERR_OFPBRC_BAD_LEN
;
1992 mbs
= ofpbuf_put_uninit(bands
, len
);
1994 for (i
= 0; i
< n
; ++i
) {
1995 mbs
[i
].packet_count
= ntohll(ombs
[i
].packet_band_count
);
1996 mbs
[i
].byte_count
= ntohll(ombs
[i
].byte_band_count
);
2002 /* Converts an OFPMP_METER reply in 'msg' into an abstract
2003 * ofputil_meter_stats in 'ms', with ms->bands pointing to band stats
2004 * decoded into 'bands'.
2006 * Multiple OFPMP_METER replies can be packed into a single OpenFlow
2007 * message. Calling this function multiple times for a single 'msg' iterates
2008 * through the replies. 'bands' is cleared for each reply.
2010 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2011 * otherwise a positive errno value. */
2013 ofputil_decode_meter_stats(struct ofpbuf
*msg
,
2014 struct ofputil_meter_stats
*ms
,
2015 struct ofpbuf
*bands
)
2017 const struct ofp13_meter_stats
*oms
;
2020 /* Pull OpenFlow headers for the first call. */
2022 ofpraw_pull_assert(msg
);
2029 oms
= ofpbuf_try_pull(msg
, sizeof *oms
);
2031 VLOG_WARN_RL(&bad_ofmsg_rl
,
2032 "OFPMP_METER reply has %"PRIu32
" leftover bytes at end",
2034 return OFPERR_OFPBRC_BAD_LEN
;
2037 ofpbuf_clear(bands
);
2038 err
= ofputil_pull_band_stats(msg
, ntohs(oms
->len
) - sizeof *oms
,
2039 &ms
->n_bands
, bands
);
2043 ms
->meter_id
= ntohl(oms
->meter_id
);
2044 ms
->flow_count
= ntohl(oms
->flow_count
);
2045 ms
->packet_in_count
= ntohll(oms
->packet_in_count
);
2046 ms
->byte_in_count
= ntohll(oms
->byte_in_count
);
2047 ms
->duration_sec
= ntohl(oms
->duration_sec
);
2048 ms
->duration_nsec
= ntohl(oms
->duration_nsec
);
2049 ms
->bands
= bands
->data
;
2055 ofputil_decode_meter_features(const struct ofp_header
*oh
,
2056 struct ofputil_meter_features
*mf
)
2058 const struct ofp13_meter_features
*omf
= ofpmsg_body(oh
);
2060 mf
->max_meters
= ntohl(omf
->max_meter
);
2061 mf
->band_types
= ntohl(omf
->band_types
);
2062 mf
->capabilities
= ntohl(omf
->capabilities
);
2063 mf
->max_bands
= omf
->max_bands
;
2064 mf
->max_color
= omf
->max_color
;
2068 ofputil_encode_meter_features_reply(const struct ofputil_meter_features
*mf
,
2069 const struct ofp_header
*request
)
2071 struct ofpbuf
*reply
;
2072 struct ofp13_meter_features
*omf
;
2074 reply
= ofpraw_alloc_stats_reply(request
, 0);
2075 omf
= ofpbuf_put_zeros(reply
, sizeof *omf
);
2077 omf
->max_meter
= htonl(mf
->max_meters
);
2078 omf
->band_types
= htonl(mf
->band_types
);
2079 omf
->capabilities
= htonl(mf
->capabilities
);
2080 omf
->max_bands
= mf
->max_bands
;
2081 omf
->max_color
= mf
->max_color
;
2087 ofputil_encode_meter_mod(enum ofp_version ofp_version
,
2088 const struct ofputil_meter_mod
*mm
)
2092 struct ofp13_meter_mod
*omm
;
2094 msg
= ofpraw_alloc(OFPRAW_OFPT13_METER_MOD
, ofp_version
,
2095 NXM_TYPICAL_LEN
+ mm
->meter
.n_bands
* 16);
2096 omm
= ofpbuf_put_zeros(msg
, sizeof *omm
);
2097 omm
->command
= htons(mm
->command
);
2098 if (mm
->command
!= OFPMC13_DELETE
) {
2099 omm
->flags
= htons(mm
->meter
.flags
);
2101 omm
->meter_id
= htonl(mm
->meter
.meter_id
);
2103 ofputil_put_bands(mm
->meter
.n_bands
, mm
->meter
.bands
, msg
);
2105 ofpmsg_update_length(msg
);
2110 ofputil_tid_command(const struct ofputil_flow_mod
*fm
,
2111 enum ofputil_protocol protocol
)
2113 return htons(protocol
& OFPUTIL_P_TID
2114 ? (fm
->command
& 0xff) | (fm
->table_id
<< 8)
2118 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
2119 * 'protocol' and returns the message. */
2121 ofputil_encode_flow_mod(const struct ofputil_flow_mod
*fm
,
2122 enum ofputil_protocol protocol
)
2124 enum ofp_version version
= ofputil_protocol_to_ofp_version(protocol
);
2125 ovs_be16 raw_flags
= ofputil_encode_flow_mod_flags(fm
->flags
, version
);
2129 case OFPUTIL_P_OF11_STD
:
2130 case OFPUTIL_P_OF12_OXM
:
2131 case OFPUTIL_P_OF13_OXM
:
2132 case OFPUTIL_P_OF14_OXM
:
2133 case OFPUTIL_P_OF15_OXM
: {
2134 struct ofp11_flow_mod
*ofm
;
2137 tailroom
= ofputil_match_typical_len(protocol
) + fm
->ofpacts_len
;
2138 msg
= ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD
, version
, tailroom
);
2139 ofm
= ofpbuf_put_zeros(msg
, sizeof *ofm
);
2140 if ((protocol
== OFPUTIL_P_OF11_STD
2141 && (fm
->command
== OFPFC_MODIFY
||
2142 fm
->command
== OFPFC_MODIFY_STRICT
)
2143 && fm
->cookie_mask
== htonll(0))
2144 || fm
->command
== OFPFC_ADD
) {
2145 ofm
->cookie
= fm
->new_cookie
;
2147 ofm
->cookie
= fm
->cookie
& fm
->cookie_mask
;
2149 ofm
->cookie_mask
= fm
->cookie_mask
;
2150 if (fm
->table_id
!= OFPTT_ALL
2151 || (protocol
!= OFPUTIL_P_OF11_STD
2152 && (fm
->command
== OFPFC_DELETE
||
2153 fm
->command
== OFPFC_DELETE_STRICT
))) {
2154 ofm
->table_id
= fm
->table_id
;
2158 ofm
->command
= fm
->command
;
2159 ofm
->idle_timeout
= htons(fm
->idle_timeout
);
2160 ofm
->hard_timeout
= htons(fm
->hard_timeout
);
2161 ofm
->priority
= htons(fm
->priority
);
2162 ofm
->buffer_id
= htonl(fm
->buffer_id
);
2163 ofm
->out_port
= ofputil_port_to_ofp11(fm
->out_port
);
2164 ofm
->out_group
= htonl(fm
->out_group
);
2165 ofm
->flags
= raw_flags
;
2166 if (version
>= OFP14_VERSION
&& fm
->command
== OFPFC_ADD
) {
2167 ofm
->importance
= htons(fm
->importance
);
2169 ofm
->importance
= 0;
2171 ofputil_put_ofp11_match(msg
, &fm
->match
, protocol
);
2172 ofpacts_put_openflow_instructions(fm
->ofpacts
, fm
->ofpacts_len
, msg
,
2177 case OFPUTIL_P_OF10_STD
:
2178 case OFPUTIL_P_OF10_STD_TID
: {
2179 struct ofp10_flow_mod
*ofm
;
2181 msg
= ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD
, OFP10_VERSION
,
2183 ofm
= ofpbuf_put_zeros(msg
, sizeof *ofm
);
2184 ofputil_match_to_ofp10_match(&fm
->match
, &ofm
->match
);
2185 ofm
->cookie
= fm
->new_cookie
;
2186 ofm
->command
= ofputil_tid_command(fm
, protocol
);
2187 ofm
->idle_timeout
= htons(fm
->idle_timeout
);
2188 ofm
->hard_timeout
= htons(fm
->hard_timeout
);
2189 ofm
->priority
= htons(fm
->priority
);
2190 ofm
->buffer_id
= htonl(fm
->buffer_id
);
2191 ofm
->out_port
= htons(ofp_to_u16(fm
->out_port
));
2192 ofm
->flags
= raw_flags
;
2193 ofpacts_put_openflow_actions(fm
->ofpacts
, fm
->ofpacts_len
, msg
,
2198 case OFPUTIL_P_OF10_NXM
:
2199 case OFPUTIL_P_OF10_NXM_TID
: {
2200 struct nx_flow_mod
*nfm
;
2203 msg
= ofpraw_alloc(OFPRAW_NXT_FLOW_MOD
, OFP10_VERSION
,
2204 NXM_TYPICAL_LEN
+ fm
->ofpacts_len
);
2205 nfm
= ofpbuf_put_zeros(msg
, sizeof *nfm
);
2206 nfm
->command
= ofputil_tid_command(fm
, protocol
);
2207 nfm
->cookie
= fm
->new_cookie
;
2208 match_len
= nx_put_match(msg
, &fm
->match
, fm
->cookie
, fm
->cookie_mask
);
2210 nfm
->idle_timeout
= htons(fm
->idle_timeout
);
2211 nfm
->hard_timeout
= htons(fm
->hard_timeout
);
2212 nfm
->priority
= htons(fm
->priority
);
2213 nfm
->buffer_id
= htonl(fm
->buffer_id
);
2214 nfm
->out_port
= htons(ofp_to_u16(fm
->out_port
));
2215 nfm
->flags
= raw_flags
;
2216 nfm
->match_len
= htons(match_len
);
2217 ofpacts_put_openflow_actions(fm
->ofpacts
, fm
->ofpacts_len
, msg
,
2226 ofpmsg_update_length(msg
);
2231 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request
*fsr
,
2232 const struct ofp10_flow_stats_request
*ofsr
,
2235 fsr
->aggregate
= aggregate
;
2236 ofputil_match_from_ofp10_match(&ofsr
->match
, &fsr
->match
);
2237 fsr
->out_port
= u16_to_ofp(ntohs(ofsr
->out_port
));
2238 fsr
->out_group
= OFPG_ANY
;
2239 fsr
->table_id
= ofsr
->table_id
;
2240 fsr
->cookie
= fsr
->cookie_mask
= htonll(0);
2246 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request
*fsr
,
2247 struct ofpbuf
*b
, bool aggregate
)
2249 const struct ofp11_flow_stats_request
*ofsr
;
2252 ofsr
= ofpbuf_pull(b
, sizeof *ofsr
);
2253 fsr
->aggregate
= aggregate
;
2254 fsr
->table_id
= ofsr
->table_id
;
2255 error
= ofputil_port_from_ofp11(ofsr
->out_port
, &fsr
->out_port
);
2259 fsr
->out_group
= ntohl(ofsr
->out_group
);
2260 fsr
->cookie
= ofsr
->cookie
;
2261 fsr
->cookie_mask
= ofsr
->cookie_mask
;
2262 error
= ofputil_pull_ofp11_match(b
, &fsr
->match
, NULL
);
2271 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request
*fsr
,
2272 struct ofpbuf
*b
, bool aggregate
)
2274 const struct nx_flow_stats_request
*nfsr
;
2277 nfsr
= ofpbuf_pull(b
, sizeof *nfsr
);
2278 error
= nx_pull_match(b
, ntohs(nfsr
->match_len
), &fsr
->match
,
2279 &fsr
->cookie
, &fsr
->cookie_mask
);
2284 return OFPERR_OFPBRC_BAD_LEN
;
2287 fsr
->aggregate
= aggregate
;
2288 fsr
->out_port
= u16_to_ofp(ntohs(nfsr
->out_port
));
2289 fsr
->out_group
= OFPG_ANY
;
2290 fsr
->table_id
= nfsr
->table_id
;
2295 /* Constructs and returns an OFPT_QUEUE_GET_CONFIG request for the specified
2296 * 'port' and 'queue', suitable for OpenFlow version 'version'.
2298 * 'queue' is honored only for OpenFlow 1.4 and later; older versions always
2299 * request all queues. */
2301 ofputil_encode_queue_get_config_request(enum ofp_version version
,
2305 struct ofpbuf
*request
;
2307 if (version
== OFP10_VERSION
) {
2308 struct ofp10_queue_get_config_request
*qgcr10
;
2310 request
= ofpraw_alloc(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST
,
2312 qgcr10
= ofpbuf_put_zeros(request
, sizeof *qgcr10
);
2313 qgcr10
->port
= htons(ofp_to_u16(port
));
2314 } else if (version
< OFP14_VERSION
) {
2315 struct ofp11_queue_get_config_request
*qgcr11
;
2317 request
= ofpraw_alloc(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST
,
2319 qgcr11
= ofpbuf_put_zeros(request
, sizeof *qgcr11
);
2320 qgcr11
->port
= ofputil_port_to_ofp11(port
);
2322 struct ofp14_queue_desc_request
*qdr14
;
2324 request
= ofpraw_alloc(OFPRAW_OFPST14_QUEUE_DESC_REQUEST
,
2326 qdr14
= ofpbuf_put_zeros(request
, sizeof *qdr14
);
2327 qdr14
->port
= ofputil_port_to_ofp11(port
);
2328 qdr14
->queue
= htonl(queue
);
2334 /* Parses OFPT_QUEUE_GET_CONFIG request 'oh', storing the port specified by the
2335 * request into '*port'. Returns 0 if successful, otherwise an OpenFlow error
2338 ofputil_decode_queue_get_config_request(const struct ofp_header
*oh
,
2339 ofp_port_t
*port
, uint32_t *queue
)
2341 const struct ofp10_queue_get_config_request
*qgcr10
;
2342 const struct ofp11_queue_get_config_request
*qgcr11
;
2343 const struct ofp14_queue_desc_request
*qdr14
;
2347 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
2348 raw
= ofpraw_pull_assert(&b
);
2350 switch ((int) raw
) {
2351 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST
:
2353 *port
= u16_to_ofp(ntohs(qgcr10
->port
));
2357 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST
:
2360 enum ofperr error
= ofputil_port_from_ofp11(qgcr11
->port
, port
);
2361 if (error
|| *port
== OFPP_ANY
) {
2366 case OFPRAW_OFPST14_QUEUE_DESC_REQUEST
:
2368 *queue
= ntohl(qdr14
->queue
);
2369 return ofputil_port_from_ofp11(qdr14
->port
, port
);
2375 return (ofp_to_u16(*port
) < ofp_to_u16(OFPP_MAX
)
2377 : OFPERR_OFPQOFC_BAD_PORT
);
2380 /* Constructs and returns the beginning of a reply to
2381 * OFPT_QUEUE_GET_CONFIG_REQUEST or OFPMP_QUEUE_DESC request 'oh'. The caller
2382 * may append information about individual queues with
2383 * ofputil_append_queue_get_config_reply(). */
2385 ofputil_start_queue_get_config_reply(const struct ofp_header
*request
,
2386 struct ovs_list
*replies
)
2388 struct ofpbuf
*reply
;
2393 error
= ofputil_decode_queue_get_config_request(request
, &port
, &queue
);
2396 enum ofpraw raw
= ofpraw_decode_assert(request
);
2397 switch ((int) raw
) {
2398 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST
:
2399 reply
= ofpraw_alloc_reply(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY
,
2401 struct ofp10_queue_get_config_reply
*qgcr10
2402 = ofpbuf_put_zeros(reply
, sizeof *qgcr10
);
2403 qgcr10
->port
= htons(ofp_to_u16(port
));
2406 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST
:
2407 reply
= ofpraw_alloc_reply(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY
,
2409 struct ofp11_queue_get_config_reply
*qgcr11
2410 = ofpbuf_put_zeros(reply
, sizeof *qgcr11
);
2411 qgcr11
->port
= ofputil_port_to_ofp11(port
);
2414 case OFPRAW_OFPST14_QUEUE_DESC_REQUEST
:
2415 reply
= ofpraw_alloc_stats_reply(request
, 0);
2423 list_push_back(replies
, &reply
->list_node
);
2427 put_ofp10_queue_rate(struct ofpbuf
*reply
,
2428 enum ofp10_queue_properties property
, uint16_t rate
)
2430 if (rate
!= UINT16_MAX
) {
2431 struct ofp10_queue_prop_rate
*oqpr
;
2433 oqpr
= ofpbuf_put_zeros(reply
, sizeof *oqpr
);
2434 oqpr
->prop_header
.property
= htons(property
);
2435 oqpr
->prop_header
.len
= htons(sizeof *oqpr
);
2436 oqpr
->rate
= htons(rate
);
2441 put_ofp14_queue_rate(struct ofpbuf
*reply
,
2442 enum ofp14_queue_desc_prop_type type
, uint16_t rate
)
2444 if (rate
!= UINT16_MAX
) {
2445 ofpprop_put_u16(reply
, type
, rate
);
2450 ofputil_append_queue_get_config_reply(const struct ofputil_queue_config
*qc
,
2451 struct ovs_list
*replies
)
2453 enum ofp_version ofp_version
= ofpmp_version(replies
);
2454 struct ofpbuf
*reply
= ofpbuf_from_list(list_back(replies
));
2455 size_t start_ofs
= reply
->size
;
2459 if (ofp_version
< OFP14_VERSION
) {
2460 if (ofp_version
< OFP12_VERSION
) {
2461 struct ofp10_packet_queue
*opq10
;
2463 opq10
= ofpbuf_put_zeros(reply
, sizeof *opq10
);
2464 opq10
->queue_id
= htonl(qc
->queue
);
2465 len_ofs
= (char *) &opq10
->len
- (char *) reply
->data
;
2467 struct ofp12_packet_queue
*opq12
;
2469 opq12
= ofpbuf_put_zeros(reply
, sizeof *opq12
);
2470 opq12
->port
= ofputil_port_to_ofp11(qc
->port
);
2471 opq12
->queue_id
= htonl(qc
->queue
);
2472 len_ofs
= (char *) &opq12
->len
- (char *) reply
->data
;
2475 put_ofp10_queue_rate(reply
, OFPQT10_MIN_RATE
, qc
->min_rate
);
2476 put_ofp10_queue_rate(reply
, OFPQT11_MAX_RATE
, qc
->max_rate
);
2478 struct ofp14_queue_desc
*oqd
= ofpbuf_put_zeros(reply
, sizeof *oqd
);
2479 oqd
->port_no
= ofputil_port_to_ofp11(qc
->port
);
2480 oqd
->queue_id
= htonl(qc
->queue
);
2481 len_ofs
= (char *) &oqd
->len
- (char *) reply
->data
;
2482 put_ofp14_queue_rate(reply
, OFPQDPT14_MIN_RATE
, qc
->min_rate
);
2483 put_ofp14_queue_rate(reply
, OFPQDPT14_MAX_RATE
, qc
->max_rate
);
2486 len
= ofpbuf_at(reply
, len_ofs
, sizeof *len
);
2487 *len
= htons(reply
->size
- start_ofs
);
2489 if (ofp_version
>= OFP14_VERSION
) {
2490 ofpmp_postappend(replies
, start_ofs
);
2495 parse_ofp10_queue_rate(const struct ofp10_queue_prop_header
*hdr
,
2498 const struct ofp10_queue_prop_rate
*oqpr
;
2500 if (hdr
->len
== htons(sizeof *oqpr
)) {
2501 oqpr
= (const struct ofp10_queue_prop_rate
*) hdr
;
2502 *rate
= ntohs(oqpr
->rate
);
2505 return OFPERR_OFPBRC_BAD_LEN
;
2510 ofputil_pull_queue_get_config_reply10(struct ofpbuf
*msg
,
2511 struct ofputil_queue_config
*queue
)
2513 const struct ofp_header
*oh
= msg
->header
;
2514 unsigned int opq_len
; /* Length of protocol-specific queue header. */
2515 unsigned int len
; /* Total length of queue + properties. */
2517 /* Obtain the port number from the message header. */
2518 if (oh
->version
== OFP10_VERSION
) {
2519 const struct ofp10_queue_get_config_reply
*oqgcr10
= msg
->msg
;
2520 queue
->port
= u16_to_ofp(ntohs(oqgcr10
->port
));
2522 const struct ofp11_queue_get_config_reply
*oqgcr11
= msg
->msg
;
2523 enum ofperr error
= ofputil_port_from_ofp11(oqgcr11
->port
,
2530 /* Pull off the queue header and get the queue number and length. */
2531 if (oh
->version
< OFP12_VERSION
) {
2532 const struct ofp10_packet_queue
*opq10
;
2533 opq10
= ofpbuf_try_pull(msg
, sizeof *opq10
);
2535 return OFPERR_OFPBRC_BAD_LEN
;
2537 queue
->queue
= ntohl(opq10
->queue_id
);
2538 len
= ntohs(opq10
->len
);
2539 opq_len
= sizeof *opq10
;
2541 const struct ofp12_packet_queue
*opq12
;
2542 opq12
= ofpbuf_try_pull(msg
, sizeof *opq12
);
2544 return OFPERR_OFPBRC_BAD_LEN
;
2546 queue
->queue
= ntohl(opq12
->queue_id
);
2547 len
= ntohs(opq12
->len
);
2548 opq_len
= sizeof *opq12
;
2552 if (len
< opq_len
|| len
> msg
->size
+ opq_len
|| len
% 8) {
2553 return OFPERR_OFPBRC_BAD_LEN
;
2557 /* Pull properties. The format of these properties differs from used in
2558 * OF1.4+ so we can't use the common property functions. */
2560 const struct ofp10_queue_prop_header
*hdr
;
2561 unsigned int property
;
2562 unsigned int prop_len
;
2563 enum ofperr error
= 0;
2565 hdr
= ofpbuf_at_assert(msg
, 0, sizeof *hdr
);
2566 prop_len
= ntohs(hdr
->len
);
2567 if (prop_len
< sizeof *hdr
|| prop_len
> msg
->size
|| prop_len
% 8) {
2568 return OFPERR_OFPBRC_BAD_LEN
;
2571 property
= ntohs(hdr
->property
);
2573 case OFPQT10_MIN_RATE
:
2574 error
= parse_ofp10_queue_rate(hdr
, &queue
->min_rate
);
2577 case OFPQT11_MAX_RATE
:
2578 error
= parse_ofp10_queue_rate(hdr
, &queue
->max_rate
);
2582 VLOG_INFO_RL(&bad_ofmsg_rl
, "unknown queue property %u", property
);
2589 ofpbuf_pull(msg
, prop_len
);
2596 ofputil_pull_queue_get_config_reply14(struct ofpbuf
*msg
,
2597 struct ofputil_queue_config
*queue
)
2599 struct ofp14_queue_desc
*oqd14
= ofpbuf_try_pull(msg
, sizeof *oqd14
);
2601 return OFPERR_OFPBRC_BAD_LEN
;
2603 enum ofperr error
= ofputil_port_from_ofp11(oqd14
->port_no
, &queue
->port
);
2607 queue
->queue
= ntohl(oqd14
->queue_id
);
2610 unsigned int len
= ntohs(oqd14
->len
);
2611 if (len
< sizeof *oqd14
|| len
> msg
->size
+ sizeof *oqd14
|| len
% 8) {
2612 return OFPERR_OFPBRC_BAD_LEN
;
2614 len
-= sizeof *oqd14
;
2616 struct ofpbuf properties
;
2617 ofpbuf_use_const(&properties
, ofpbuf_pull(msg
, len
), len
);
2618 while (properties
.size
> 0) {
2619 struct ofpbuf payload
;
2622 error
= ofpprop_pull(&properties
, &payload
, &type
);
2628 case OFPQDPT14_MIN_RATE
:
2629 error
= ofpprop_parse_u16(&payload
, &queue
->min_rate
);
2632 case OFPQDPT14_MAX_RATE
:
2633 error
= ofpprop_parse_u16(&payload
, &queue
->max_rate
);
2637 error
= OFPPROP_UNKNOWN(true, "queue desc", type
);
2649 /* Decodes information about a queue from the OFPT_QUEUE_GET_CONFIG_REPLY in
2650 * 'reply' and stores it in '*queue'. ofputil_decode_queue_get_config_reply()
2651 * must already have pulled off the main header.
2653 * This function returns EOF if the last queue has already been decoded, 0 if a
2654 * queue was successfully decoded into '*queue', or an ofperr if there was a
2655 * problem decoding 'reply'. */
2657 ofputil_pull_queue_get_config_reply(struct ofpbuf
*msg
,
2658 struct ofputil_queue_config
*queue
)
2662 /* Pull OpenFlow header. */
2663 raw
= ofpraw_pull_assert(msg
);
2665 /* Pull protocol-specific ofp_queue_get_config_reply header (OF1.4
2666 * doesn't have one at all). */
2667 if (raw
== OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY
) {
2668 ofpbuf_pull(msg
, sizeof(struct ofp10_queue_get_config_reply
));
2669 } else if (raw
== OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY
) {
2670 ofpbuf_pull(msg
, sizeof(struct ofp11_queue_get_config_reply
));
2672 ovs_assert(raw
== OFPRAW_OFPST14_QUEUE_DESC_REPLY
);
2675 raw
= ofpraw_decode_assert(msg
->header
);
2678 queue
->min_rate
= UINT16_MAX
;
2679 queue
->max_rate
= UINT16_MAX
;
2683 } else if (raw
== OFPRAW_OFPST14_QUEUE_DESC_REPLY
) {
2684 return ofputil_pull_queue_get_config_reply14(msg
, queue
);
2686 return ofputil_pull_queue_get_config_reply10(msg
, queue
);
2690 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
2691 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
2692 * successful, otherwise an OpenFlow error code. */
2694 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request
*fsr
,
2695 const struct ofp_header
*oh
)
2700 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
2701 raw
= ofpraw_pull_assert(&b
);
2702 switch ((int) raw
) {
2703 case OFPRAW_OFPST10_FLOW_REQUEST
:
2704 return ofputil_decode_ofpst10_flow_request(fsr
, b
.data
, false);
2706 case OFPRAW_OFPST10_AGGREGATE_REQUEST
:
2707 return ofputil_decode_ofpst10_flow_request(fsr
, b
.data
, true);
2709 case OFPRAW_OFPST11_FLOW_REQUEST
:
2710 return ofputil_decode_ofpst11_flow_request(fsr
, &b
, false);
2712 case OFPRAW_OFPST11_AGGREGATE_REQUEST
:
2713 return ofputil_decode_ofpst11_flow_request(fsr
, &b
, true);
2715 case OFPRAW_NXST_FLOW_REQUEST
:
2716 return ofputil_decode_nxst_flow_request(fsr
, &b
, false);
2718 case OFPRAW_NXST_AGGREGATE_REQUEST
:
2719 return ofputil_decode_nxst_flow_request(fsr
, &b
, true);
2722 /* Hey, the caller lied. */
2727 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
2728 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
2729 * 'protocol', and returns the message. */
2731 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request
*fsr
,
2732 enum ofputil_protocol protocol
)
2738 case OFPUTIL_P_OF11_STD
:
2739 case OFPUTIL_P_OF12_OXM
:
2740 case OFPUTIL_P_OF13_OXM
:
2741 case OFPUTIL_P_OF14_OXM
:
2742 case OFPUTIL_P_OF15_OXM
: {
2743 struct ofp11_flow_stats_request
*ofsr
;
2745 raw
= (fsr
->aggregate
2746 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
2747 : OFPRAW_OFPST11_FLOW_REQUEST
);
2748 msg
= ofpraw_alloc(raw
, ofputil_protocol_to_ofp_version(protocol
),
2749 ofputil_match_typical_len(protocol
));
2750 ofsr
= ofpbuf_put_zeros(msg
, sizeof *ofsr
);
2751 ofsr
->table_id
= fsr
->table_id
;
2752 ofsr
->out_port
= ofputil_port_to_ofp11(fsr
->out_port
);
2753 ofsr
->out_group
= htonl(fsr
->out_group
);
2754 ofsr
->cookie
= fsr
->cookie
;
2755 ofsr
->cookie_mask
= fsr
->cookie_mask
;
2756 ofputil_put_ofp11_match(msg
, &fsr
->match
, protocol
);
2760 case OFPUTIL_P_OF10_STD
:
2761 case OFPUTIL_P_OF10_STD_TID
: {
2762 struct ofp10_flow_stats_request
*ofsr
;
2764 raw
= (fsr
->aggregate
2765 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
2766 : OFPRAW_OFPST10_FLOW_REQUEST
);
2767 msg
= ofpraw_alloc(raw
, OFP10_VERSION
, 0);
2768 ofsr
= ofpbuf_put_zeros(msg
, sizeof *ofsr
);
2769 ofputil_match_to_ofp10_match(&fsr
->match
, &ofsr
->match
);
2770 ofsr
->table_id
= fsr
->table_id
;
2771 ofsr
->out_port
= htons(ofp_to_u16(fsr
->out_port
));
2775 case OFPUTIL_P_OF10_NXM
:
2776 case OFPUTIL_P_OF10_NXM_TID
: {
2777 struct nx_flow_stats_request
*nfsr
;
2780 raw
= (fsr
->aggregate
2781 ? OFPRAW_NXST_AGGREGATE_REQUEST
2782 : OFPRAW_NXST_FLOW_REQUEST
);
2783 msg
= ofpraw_alloc(raw
, OFP10_VERSION
, NXM_TYPICAL_LEN
);
2784 ofpbuf_put_zeros(msg
, sizeof *nfsr
);
2785 match_len
= nx_put_match(msg
, &fsr
->match
,
2786 fsr
->cookie
, fsr
->cookie_mask
);
2789 nfsr
->out_port
= htons(ofp_to_u16(fsr
->out_port
));
2790 nfsr
->match_len
= htons(match_len
);
2791 nfsr
->table_id
= fsr
->table_id
;
2802 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
2803 * ofputil_flow_stats in 'fs'.
2805 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
2806 * OpenFlow message. Calling this function multiple times for a single 'msg'
2807 * iterates through the replies. The caller must initially leave 'msg''s layer
2808 * pointers null and not modify them between calls.
2810 * Most switches don't send the values needed to populate fs->idle_age and
2811 * fs->hard_age, so those members will usually be set to 0. If the switch from
2812 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
2813 * 'flow_age_extension' as true so that the contents of 'msg' determine the
2814 * 'idle_age' and 'hard_age' members in 'fs'.
2816 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
2817 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
2818 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
2820 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2821 * otherwise a positive errno value. */
2823 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats
*fs
,
2825 bool flow_age_extension
,
2826 struct ofpbuf
*ofpacts
)
2828 const struct ofp_header
*oh
;
2829 size_t instructions_len
;
2833 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
2834 : ofpraw_pull(&raw
, msg
));
2842 } else if (raw
== OFPRAW_OFPST11_FLOW_REPLY
2843 || raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
2844 const struct ofp11_flow_stats
*ofs
;
2846 uint16_t padded_match_len
;
2848 ofs
= ofpbuf_try_pull(msg
, sizeof *ofs
);
2850 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply has %"PRIu32
" leftover "
2851 "bytes at end", msg
->size
);
2855 length
= ntohs(ofs
->length
);
2856 if (length
< sizeof *ofs
) {
2857 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply claims invalid "
2858 "length %"PRIuSIZE
, length
);
2862 if (ofputil_pull_ofp11_match(msg
, &fs
->match
, &padded_match_len
)) {
2863 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply bad match");
2866 instructions_len
= length
- sizeof *ofs
- padded_match_len
;
2868 fs
->priority
= ntohs(ofs
->priority
);
2869 fs
->table_id
= ofs
->table_id
;
2870 fs
->duration_sec
= ntohl(ofs
->duration_sec
);
2871 fs
->duration_nsec
= ntohl(ofs
->duration_nsec
);
2872 fs
->idle_timeout
= ntohs(ofs
->idle_timeout
);
2873 fs
->hard_timeout
= ntohs(ofs
->hard_timeout
);
2874 if (oh
->version
>= OFP14_VERSION
) {
2875 fs
->importance
= ntohs(ofs
->importance
);
2879 if (raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
2880 error
= ofputil_decode_flow_mod_flags(ofs
->flags
, -1, oh
->version
,
2890 fs
->cookie
= ofs
->cookie
;
2891 fs
->packet_count
= ntohll(ofs
->packet_count
);
2892 fs
->byte_count
= ntohll(ofs
->byte_count
);
2893 } else if (raw
== OFPRAW_OFPST10_FLOW_REPLY
) {
2894 const struct ofp10_flow_stats
*ofs
;
2897 ofs
= ofpbuf_try_pull(msg
, sizeof *ofs
);
2899 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply has %"PRIu32
" leftover "
2900 "bytes at end", msg
->size
);
2904 length
= ntohs(ofs
->length
);
2905 if (length
< sizeof *ofs
) {
2906 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply claims invalid "
2907 "length %"PRIuSIZE
, length
);
2910 instructions_len
= length
- sizeof *ofs
;
2912 fs
->cookie
= get_32aligned_be64(&ofs
->cookie
);
2913 ofputil_match_from_ofp10_match(&ofs
->match
, &fs
->match
);
2914 fs
->priority
= ntohs(ofs
->priority
);
2915 fs
->table_id
= ofs
->table_id
;
2916 fs
->duration_sec
= ntohl(ofs
->duration_sec
);
2917 fs
->duration_nsec
= ntohl(ofs
->duration_nsec
);
2918 fs
->idle_timeout
= ntohs(ofs
->idle_timeout
);
2919 fs
->hard_timeout
= ntohs(ofs
->hard_timeout
);
2923 fs
->packet_count
= ntohll(get_32aligned_be64(&ofs
->packet_count
));
2924 fs
->byte_count
= ntohll(get_32aligned_be64(&ofs
->byte_count
));
2926 } else if (raw
== OFPRAW_NXST_FLOW_REPLY
) {
2927 const struct nx_flow_stats
*nfs
;
2928 size_t match_len
, length
;
2930 nfs
= ofpbuf_try_pull(msg
, sizeof *nfs
);
2932 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW reply has %"PRIu32
" leftover "
2933 "bytes at end", msg
->size
);
2937 length
= ntohs(nfs
->length
);
2938 match_len
= ntohs(nfs
->match_len
);
2939 if (length
< sizeof *nfs
+ ROUND_UP(match_len
, 8)) {
2940 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW reply with match_len=%"PRIuSIZE
" "
2941 "claims invalid length %"PRIuSIZE
, match_len
, length
);
2944 if (nx_pull_match(msg
, match_len
, &fs
->match
, NULL
, NULL
)) {
2947 instructions_len
= length
- sizeof *nfs
- ROUND_UP(match_len
, 8);
2949 fs
->cookie
= nfs
->cookie
;
2950 fs
->table_id
= nfs
->table_id
;
2951 fs
->duration_sec
= ntohl(nfs
->duration_sec
);
2952 fs
->duration_nsec
= ntohl(nfs
->duration_nsec
);
2953 fs
->priority
= ntohs(nfs
->priority
);
2954 fs
->idle_timeout
= ntohs(nfs
->idle_timeout
);
2955 fs
->hard_timeout
= ntohs(nfs
->hard_timeout
);
2959 if (flow_age_extension
) {
2960 if (nfs
->idle_age
) {
2961 fs
->idle_age
= ntohs(nfs
->idle_age
) - 1;
2963 if (nfs
->hard_age
) {
2964 fs
->hard_age
= ntohs(nfs
->hard_age
) - 1;
2967 fs
->packet_count
= ntohll(nfs
->packet_count
);
2968 fs
->byte_count
= ntohll(nfs
->byte_count
);
2974 if (ofpacts_pull_openflow_instructions(msg
, instructions_len
, oh
->version
,
2976 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply bad instructions");
2979 fs
->ofpacts
= ofpacts
->data
;
2980 fs
->ofpacts_len
= ofpacts
->size
;
2985 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
2987 * We use this in situations where OVS internally uses UINT64_MAX to mean
2988 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
2990 unknown_to_zero(uint64_t count
)
2992 return count
!= UINT64_MAX
? count
: 0;
2995 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
2996 * those already present in the list of ofpbufs in 'replies'. 'replies' should
2997 * have been initialized with ofpmp_init(). */
2999 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats
*fs
,
3000 struct ovs_list
*replies
)
3002 struct ofpbuf
*reply
= ofpbuf_from_list(list_back(replies
));
3003 size_t start_ofs
= reply
->size
;
3004 enum ofp_version version
= ofpmp_version(replies
);
3005 enum ofpraw raw
= ofpmp_decode_raw(replies
);
3007 if (raw
== OFPRAW_OFPST11_FLOW_REPLY
|| raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
3008 struct ofp11_flow_stats
*ofs
;
3010 ofpbuf_put_uninit(reply
, sizeof *ofs
);
3011 oxm_put_match(reply
, &fs
->match
, version
);
3012 ofpacts_put_openflow_instructions(fs
->ofpacts
, fs
->ofpacts_len
, reply
,
3015 ofs
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *ofs
);
3016 ofs
->length
= htons(reply
->size
- start_ofs
);
3017 ofs
->table_id
= fs
->table_id
;
3019 ofs
->duration_sec
= htonl(fs
->duration_sec
);
3020 ofs
->duration_nsec
= htonl(fs
->duration_nsec
);
3021 ofs
->priority
= htons(fs
->priority
);
3022 ofs
->idle_timeout
= htons(fs
->idle_timeout
);
3023 ofs
->hard_timeout
= htons(fs
->hard_timeout
);
3024 if (version
>= OFP14_VERSION
) {
3025 ofs
->importance
= htons(fs
->importance
);
3027 ofs
->importance
= 0;
3029 if (raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
3030 ofs
->flags
= ofputil_encode_flow_mod_flags(fs
->flags
, version
);
3034 memset(ofs
->pad2
, 0, sizeof ofs
->pad2
);
3035 ofs
->cookie
= fs
->cookie
;
3036 ofs
->packet_count
= htonll(unknown_to_zero(fs
->packet_count
));
3037 ofs
->byte_count
= htonll(unknown_to_zero(fs
->byte_count
));
3038 } else if (raw
== OFPRAW_OFPST10_FLOW_REPLY
) {
3039 struct ofp10_flow_stats
*ofs
;
3041 ofpbuf_put_uninit(reply
, sizeof *ofs
);
3042 ofpacts_put_openflow_actions(fs
->ofpacts
, fs
->ofpacts_len
, reply
,
3044 ofs
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *ofs
);
3045 ofs
->length
= htons(reply
->size
- start_ofs
);
3046 ofs
->table_id
= fs
->table_id
;
3048 ofputil_match_to_ofp10_match(&fs
->match
, &ofs
->match
);
3049 ofs
->duration_sec
= htonl(fs
->duration_sec
);
3050 ofs
->duration_nsec
= htonl(fs
->duration_nsec
);
3051 ofs
->priority
= htons(fs
->priority
);
3052 ofs
->idle_timeout
= htons(fs
->idle_timeout
);
3053 ofs
->hard_timeout
= htons(fs
->hard_timeout
);
3054 memset(ofs
->pad2
, 0, sizeof ofs
->pad2
);
3055 put_32aligned_be64(&ofs
->cookie
, fs
->cookie
);
3056 put_32aligned_be64(&ofs
->packet_count
,
3057 htonll(unknown_to_zero(fs
->packet_count
)));
3058 put_32aligned_be64(&ofs
->byte_count
,
3059 htonll(unknown_to_zero(fs
->byte_count
)));
3060 } else if (raw
== OFPRAW_NXST_FLOW_REPLY
) {
3061 struct nx_flow_stats
*nfs
;
3064 ofpbuf_put_uninit(reply
, sizeof *nfs
);
3065 match_len
= nx_put_match(reply
, &fs
->match
, 0, 0);
3066 ofpacts_put_openflow_actions(fs
->ofpacts
, fs
->ofpacts_len
, reply
,
3068 nfs
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *nfs
);
3069 nfs
->length
= htons(reply
->size
- start_ofs
);
3070 nfs
->table_id
= fs
->table_id
;
3072 nfs
->duration_sec
= htonl(fs
->duration_sec
);
3073 nfs
->duration_nsec
= htonl(fs
->duration_nsec
);
3074 nfs
->priority
= htons(fs
->priority
);
3075 nfs
->idle_timeout
= htons(fs
->idle_timeout
);
3076 nfs
->hard_timeout
= htons(fs
->hard_timeout
);
3077 nfs
->idle_age
= htons(fs
->idle_age
< 0 ? 0
3078 : fs
->idle_age
< UINT16_MAX
? fs
->idle_age
+ 1
3080 nfs
->hard_age
= htons(fs
->hard_age
< 0 ? 0
3081 : fs
->hard_age
< UINT16_MAX
? fs
->hard_age
+ 1
3083 nfs
->match_len
= htons(match_len
);
3084 nfs
->cookie
= fs
->cookie
;
3085 nfs
->packet_count
= htonll(fs
->packet_count
);
3086 nfs
->byte_count
= htonll(fs
->byte_count
);
3091 ofpmp_postappend(replies
, start_ofs
);
3094 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
3095 * NXST_AGGREGATE reply matching 'request', and returns the message. */
3097 ofputil_encode_aggregate_stats_reply(
3098 const struct ofputil_aggregate_stats
*stats
,
3099 const struct ofp_header
*request
)
3101 struct ofp_aggregate_stats_reply
*asr
;
3102 uint64_t packet_count
;
3103 uint64_t byte_count
;
3107 ofpraw_decode(&raw
, request
);
3108 if (raw
== OFPRAW_OFPST10_AGGREGATE_REQUEST
) {
3109 packet_count
= unknown_to_zero(stats
->packet_count
);
3110 byte_count
= unknown_to_zero(stats
->byte_count
);
3112 packet_count
= stats
->packet_count
;
3113 byte_count
= stats
->byte_count
;
3116 msg
= ofpraw_alloc_stats_reply(request
, 0);
3117 asr
= ofpbuf_put_zeros(msg
, sizeof *asr
);
3118 put_32aligned_be64(&asr
->packet_count
, htonll(packet_count
));
3119 put_32aligned_be64(&asr
->byte_count
, htonll(byte_count
));
3120 asr
->flow_count
= htonl(stats
->flow_count
);
3126 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats
*stats
,
3127 const struct ofp_header
*reply
)
3129 struct ofp_aggregate_stats_reply
*asr
;
3132 ofpbuf_use_const(&msg
, reply
, ntohs(reply
->length
));
3133 ofpraw_pull_assert(&msg
);
3136 stats
->packet_count
= ntohll(get_32aligned_be64(&asr
->packet_count
));
3137 stats
->byte_count
= ntohll(get_32aligned_be64(&asr
->byte_count
));
3138 stats
->flow_count
= ntohl(asr
->flow_count
);
3143 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
3144 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
3145 * an OpenFlow error code. */
3147 ofputil_decode_flow_removed(struct ofputil_flow_removed
*fr
,
3148 const struct ofp_header
*oh
)
3153 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
3154 raw
= ofpraw_pull_assert(&b
);
3155 if (raw
== OFPRAW_OFPT11_FLOW_REMOVED
) {
3156 const struct ofp12_flow_removed
*ofr
;
3159 ofr
= ofpbuf_pull(&b
, sizeof *ofr
);
3161 error
= ofputil_pull_ofp11_match(&b
, &fr
->match
, NULL
);
3166 fr
->priority
= ntohs(ofr
->priority
);
3167 fr
->cookie
= ofr
->cookie
;
3168 fr
->reason
= ofr
->reason
;
3169 fr
->table_id
= ofr
->table_id
;
3170 fr
->duration_sec
= ntohl(ofr
->duration_sec
);
3171 fr
->duration_nsec
= ntohl(ofr
->duration_nsec
);
3172 fr
->idle_timeout
= ntohs(ofr
->idle_timeout
);
3173 fr
->hard_timeout
= ntohs(ofr
->hard_timeout
);
3174 fr
->packet_count
= ntohll(ofr
->packet_count
);
3175 fr
->byte_count
= ntohll(ofr
->byte_count
);
3176 } else if (raw
== OFPRAW_OFPT10_FLOW_REMOVED
) {
3177 const struct ofp10_flow_removed
*ofr
;
3179 ofr
= ofpbuf_pull(&b
, sizeof *ofr
);
3181 ofputil_match_from_ofp10_match(&ofr
->match
, &fr
->match
);
3182 fr
->priority
= ntohs(ofr
->priority
);
3183 fr
->cookie
= ofr
->cookie
;
3184 fr
->reason
= ofr
->reason
;
3186 fr
->duration_sec
= ntohl(ofr
->duration_sec
);
3187 fr
->duration_nsec
= ntohl(ofr
->duration_nsec
);
3188 fr
->idle_timeout
= ntohs(ofr
->idle_timeout
);
3189 fr
->hard_timeout
= 0;
3190 fr
->packet_count
= ntohll(ofr
->packet_count
);
3191 fr
->byte_count
= ntohll(ofr
->byte_count
);
3192 } else if (raw
== OFPRAW_NXT_FLOW_REMOVED
) {
3193 struct nx_flow_removed
*nfr
;
3196 nfr
= ofpbuf_pull(&b
, sizeof *nfr
);
3197 error
= nx_pull_match(&b
, ntohs(nfr
->match_len
), &fr
->match
,
3203 return OFPERR_OFPBRC_BAD_LEN
;
3206 fr
->priority
= ntohs(nfr
->priority
);
3207 fr
->cookie
= nfr
->cookie
;
3208 fr
->reason
= nfr
->reason
;
3209 fr
->table_id
= nfr
->table_id
? nfr
->table_id
- 1 : 255;
3210 fr
->duration_sec
= ntohl(nfr
->duration_sec
);
3211 fr
->duration_nsec
= ntohl(nfr
->duration_nsec
);
3212 fr
->idle_timeout
= ntohs(nfr
->idle_timeout
);
3213 fr
->hard_timeout
= 0;
3214 fr
->packet_count
= ntohll(nfr
->packet_count
);
3215 fr
->byte_count
= ntohll(nfr
->byte_count
);
3223 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
3224 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
3227 ofputil_encode_flow_removed(const struct ofputil_flow_removed
*fr
,
3228 enum ofputil_protocol protocol
)
3231 enum ofp_flow_removed_reason reason
= fr
->reason
;
3233 if (reason
== OFPRR_METER_DELETE
&& !(protocol
& OFPUTIL_P_OF14_UP
)) {
3234 reason
= OFPRR_DELETE
;
3238 case OFPUTIL_P_OF11_STD
:
3239 case OFPUTIL_P_OF12_OXM
:
3240 case OFPUTIL_P_OF13_OXM
:
3241 case OFPUTIL_P_OF14_OXM
:
3242 case OFPUTIL_P_OF15_OXM
: {
3243 struct ofp12_flow_removed
*ofr
;
3245 msg
= ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED
,
3246 ofputil_protocol_to_ofp_version(protocol
),
3248 ofputil_match_typical_len(protocol
));
3249 ofr
= ofpbuf_put_zeros(msg
, sizeof *ofr
);
3250 ofr
->cookie
= fr
->cookie
;
3251 ofr
->priority
= htons(fr
->priority
);
3252 ofr
->reason
= reason
;
3253 ofr
->table_id
= fr
->table_id
;
3254 ofr
->duration_sec
= htonl(fr
->duration_sec
);
3255 ofr
->duration_nsec
= htonl(fr
->duration_nsec
);
3256 ofr
->idle_timeout
= htons(fr
->idle_timeout
);
3257 ofr
->hard_timeout
= htons(fr
->hard_timeout
);
3258 ofr
->packet_count
= htonll(fr
->packet_count
);
3259 ofr
->byte_count
= htonll(fr
->byte_count
);
3260 ofputil_put_ofp11_match(msg
, &fr
->match
, protocol
);
3264 case OFPUTIL_P_OF10_STD
:
3265 case OFPUTIL_P_OF10_STD_TID
: {
3266 struct ofp10_flow_removed
*ofr
;
3268 msg
= ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED
, OFP10_VERSION
,
3270 ofr
= ofpbuf_put_zeros(msg
, sizeof *ofr
);
3271 ofputil_match_to_ofp10_match(&fr
->match
, &ofr
->match
);
3272 ofr
->cookie
= fr
->cookie
;
3273 ofr
->priority
= htons(fr
->priority
);
3274 ofr
->reason
= reason
;
3275 ofr
->duration_sec
= htonl(fr
->duration_sec
);
3276 ofr
->duration_nsec
= htonl(fr
->duration_nsec
);
3277 ofr
->idle_timeout
= htons(fr
->idle_timeout
);
3278 ofr
->packet_count
= htonll(unknown_to_zero(fr
->packet_count
));
3279 ofr
->byte_count
= htonll(unknown_to_zero(fr
->byte_count
));
3283 case OFPUTIL_P_OF10_NXM
:
3284 case OFPUTIL_P_OF10_NXM_TID
: {
3285 struct nx_flow_removed
*nfr
;
3288 msg
= ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED
, OFP10_VERSION
,
3289 htonl(0), NXM_TYPICAL_LEN
);
3290 ofpbuf_put_zeros(msg
, sizeof *nfr
);
3291 match_len
= nx_put_match(msg
, &fr
->match
, 0, 0);
3294 nfr
->cookie
= fr
->cookie
;
3295 nfr
->priority
= htons(fr
->priority
);
3296 nfr
->reason
= reason
;
3297 nfr
->table_id
= fr
->table_id
+ 1;
3298 nfr
->duration_sec
= htonl(fr
->duration_sec
);
3299 nfr
->duration_nsec
= htonl(fr
->duration_nsec
);
3300 nfr
->idle_timeout
= htons(fr
->idle_timeout
);
3301 nfr
->match_len
= htons(match_len
);
3302 nfr
->packet_count
= htonll(fr
->packet_count
);
3303 nfr
->byte_count
= htonll(fr
->byte_count
);
3315 ofputil_decode_packet_in(struct ofputil_packet_in
*pin
,
3316 const struct ofp_header
*oh
)
3321 memset(pin
, 0, sizeof *pin
);
3322 pin
->cookie
= OVS_BE64_MAX
;
3324 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
3325 raw
= ofpraw_pull_assert(&b
);
3326 if (raw
== OFPRAW_OFPT13_PACKET_IN
|| raw
== OFPRAW_OFPT12_PACKET_IN
) {
3327 const struct ofp12_packet_in
*opi
= ofpbuf_pull(&b
, sizeof *opi
);
3328 const ovs_be64
*cookie
= (raw
== OFPRAW_OFPT13_PACKET_IN
3329 ? ofpbuf_pull(&b
, sizeof *cookie
)
3331 enum ofperr error
= oxm_pull_match_loose(&b
, &pin
->flow_metadata
);
3336 if (!ofpbuf_try_pull(&b
, 2)) {
3337 return OFPERR_OFPBRC_BAD_LEN
;
3340 pin
->reason
= opi
->reason
;
3341 pin
->table_id
= opi
->table_id
;
3342 pin
->buffer_id
= ntohl(opi
->buffer_id
);
3343 pin
->total_len
= ntohs(opi
->total_len
);
3346 pin
->cookie
= *cookie
;
3349 pin
->packet
= b
.data
;
3350 pin
->packet_len
= b
.size
;
3351 } else if (raw
== OFPRAW_OFPT10_PACKET_IN
) {
3352 const struct ofp10_packet_in
*opi
;
3354 opi
= ofpbuf_pull(&b
, offsetof(struct ofp10_packet_in
, data
));
3356 pin
->packet
= opi
->data
;
3357 pin
->packet_len
= b
.size
;
3359 match_init_catchall(&pin
->flow_metadata
);
3360 match_set_in_port(&pin
->flow_metadata
, u16_to_ofp(ntohs(opi
->in_port
)));
3361 pin
->reason
= opi
->reason
;
3362 pin
->buffer_id
= ntohl(opi
->buffer_id
);
3363 pin
->total_len
= ntohs(opi
->total_len
);
3364 } else if (raw
== OFPRAW_OFPT11_PACKET_IN
) {
3365 const struct ofp11_packet_in
*opi
;
3369 opi
= ofpbuf_pull(&b
, sizeof *opi
);
3371 pin
->packet
= b
.data
;
3372 pin
->packet_len
= b
.size
;
3374 pin
->buffer_id
= ntohl(opi
->buffer_id
);
3375 error
= ofputil_port_from_ofp11(opi
->in_port
, &in_port
);
3379 match_init_catchall(&pin
->flow_metadata
);
3380 match_set_in_port(&pin
->flow_metadata
, in_port
);
3381 pin
->total_len
= ntohs(opi
->total_len
);
3382 pin
->reason
= opi
->reason
;
3383 pin
->table_id
= opi
->table_id
;
3384 } else if (raw
== OFPRAW_NXT_PACKET_IN
) {
3385 const struct nx_packet_in
*npi
;
3388 npi
= ofpbuf_pull(&b
, sizeof *npi
);
3389 error
= nx_pull_match_loose(&b
, ntohs(npi
->match_len
),
3390 &pin
->flow_metadata
, NULL
, NULL
);
3395 if (!ofpbuf_try_pull(&b
, 2)) {
3396 return OFPERR_OFPBRC_BAD_LEN
;
3399 pin
->reason
= npi
->reason
;
3400 pin
->table_id
= npi
->table_id
;
3401 pin
->cookie
= npi
->cookie
;
3403 pin
->buffer_id
= ntohl(npi
->buffer_id
);
3404 pin
->total_len
= ntohs(npi
->total_len
);
3406 pin
->packet
= b
.data
;
3407 pin
->packet_len
= b
.size
;
3415 static struct ofpbuf
*
3416 ofputil_encode_ofp10_packet_in(const struct ofputil_packet_in
*pin
)
3418 struct ofp10_packet_in
*opi
;
3419 struct ofpbuf
*packet
;
3421 packet
= ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN
, OFP10_VERSION
,
3422 htonl(0), pin
->packet_len
);
3423 opi
= ofpbuf_put_zeros(packet
, offsetof(struct ofp10_packet_in
, data
));
3424 opi
->total_len
= htons(pin
->total_len
);
3425 opi
->in_port
= htons(ofp_to_u16(pin
->flow_metadata
.flow
.in_port
.ofp_port
));
3426 opi
->reason
= pin
->reason
;
3427 opi
->buffer_id
= htonl(pin
->buffer_id
);
3429 ofpbuf_put(packet
, pin
->packet
, pin
->packet_len
);
3434 static struct ofpbuf
*
3435 ofputil_encode_nx_packet_in(const struct ofputil_packet_in
*pin
)
3437 struct nx_packet_in
*npi
;
3438 struct ofpbuf
*packet
;
3441 /* The final argument is just an estimate of the space required. */
3442 packet
= ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN
, OFP10_VERSION
,
3443 htonl(0), NXM_TYPICAL_LEN
+ 2 + pin
->packet_len
);
3444 ofpbuf_put_zeros(packet
, sizeof *npi
);
3445 match_len
= nx_put_match(packet
, &pin
->flow_metadata
, 0, 0);
3446 ofpbuf_put_zeros(packet
, 2);
3447 ofpbuf_put(packet
, pin
->packet
, pin
->packet_len
);
3450 npi
->buffer_id
= htonl(pin
->buffer_id
);
3451 npi
->total_len
= htons(pin
->total_len
);
3452 npi
->reason
= pin
->reason
;
3453 npi
->table_id
= pin
->table_id
;
3454 npi
->cookie
= pin
->cookie
;
3455 npi
->match_len
= htons(match_len
);
3460 static struct ofpbuf
*
3461 ofputil_encode_ofp11_packet_in(const struct ofputil_packet_in
*pin
)
3463 struct ofp11_packet_in
*opi
;
3464 struct ofpbuf
*packet
;
3466 packet
= ofpraw_alloc_xid(OFPRAW_OFPT11_PACKET_IN
, OFP11_VERSION
,
3467 htonl(0), pin
->packet_len
);
3468 opi
= ofpbuf_put_zeros(packet
, sizeof *opi
);
3469 opi
->buffer_id
= htonl(pin
->buffer_id
);
3470 opi
->in_port
= ofputil_port_to_ofp11(pin
->flow_metadata
.flow
.in_port
.ofp_port
);
3471 opi
->in_phy_port
= opi
->in_port
;
3472 opi
->total_len
= htons(pin
->total_len
);
3473 opi
->reason
= pin
->reason
;
3474 opi
->table_id
= pin
->table_id
;
3476 ofpbuf_put(packet
, pin
->packet
, pin
->packet_len
);
3481 static struct ofpbuf
*
3482 ofputil_encode_ofp12_packet_in(const struct ofputil_packet_in
*pin
,
3483 enum ofputil_protocol protocol
)
3485 enum ofp_version version
= ofputil_protocol_to_ofp_version(protocol
);
3486 enum ofpraw raw
= (version
>= OFP13_VERSION
3487 ? OFPRAW_OFPT13_PACKET_IN
3488 : OFPRAW_OFPT12_PACKET_IN
);
3491 /* The final argument is just an estimate of the space required. */
3492 msg
= ofpraw_alloc_xid(raw
, version
,
3493 htonl(0), NXM_TYPICAL_LEN
+ 2 + pin
->packet_len
);
3495 struct ofp12_packet_in
*opi
= ofpbuf_put_zeros(msg
, sizeof *opi
);
3496 opi
->buffer_id
= htonl(pin
->buffer_id
);
3497 opi
->total_len
= htons(pin
->total_len
);
3498 opi
->reason
= pin
->reason
;
3499 opi
->table_id
= pin
->table_id
;
3500 if (version
>= OFP13_VERSION
) {
3501 ovs_be64 cookie
= pin
->cookie
;
3502 ofpbuf_put(msg
, &cookie
, sizeof cookie
);
3505 oxm_put_match(msg
, &pin
->flow_metadata
, version
);
3506 ofpbuf_put_zeros(msg
, 2);
3507 ofpbuf_put(msg
, pin
->packet
, pin
->packet_len
);
3512 /* Converts abstract ofputil_packet_in 'pin' into a PACKET_IN message
3513 * in the format specified by 'packet_in_format'. */
3515 ofputil_encode_packet_in(const struct ofputil_packet_in
*pin
,
3516 enum ofputil_protocol protocol
,
3517 enum nx_packet_in_format packet_in_format
)
3519 struct ofpbuf
*packet
;
3522 case OFPUTIL_P_OF10_STD
:
3523 case OFPUTIL_P_OF10_STD_TID
:
3524 case OFPUTIL_P_OF10_NXM
:
3525 case OFPUTIL_P_OF10_NXM_TID
:
3526 packet
= (packet_in_format
== NXPIF_NXM
3527 ? ofputil_encode_nx_packet_in(pin
)
3528 : ofputil_encode_ofp10_packet_in(pin
));
3531 case OFPUTIL_P_OF11_STD
:
3532 packet
= ofputil_encode_ofp11_packet_in(pin
);
3535 case OFPUTIL_P_OF12_OXM
:
3536 case OFPUTIL_P_OF13_OXM
:
3537 case OFPUTIL_P_OF14_OXM
:
3538 case OFPUTIL_P_OF15_OXM
:
3539 packet
= ofputil_encode_ofp12_packet_in(pin
, protocol
);
3546 ofpmsg_update_length(packet
);
3550 /* Returns a string form of 'reason'. The return value is either a statically
3551 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
3552 * 'bufsize' should be at least OFPUTIL_PACKET_IN_REASON_BUFSIZE. */
3554 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason
,
3555 char *reasonbuf
, size_t bufsize
)
3562 case OFPR_INVALID_TTL
:
3563 return "invalid_ttl";
3564 case OFPR_ACTION_SET
:
3565 return "action_set";
3568 case OFPR_PACKET_OUT
:
3569 return "packet_out";
3571 case OFPR_N_REASONS
:
3573 snprintf(reasonbuf
, bufsize
, "%d", (int) reason
);
3579 ofputil_packet_in_reason_from_string(const char *s
,
3580 enum ofp_packet_in_reason
*reason
)
3584 for (i
= 0; i
< OFPR_N_REASONS
; i
++) {
3585 char reasonbuf
[OFPUTIL_PACKET_IN_REASON_BUFSIZE
];
3586 const char *reason_s
;
3588 reason_s
= ofputil_packet_in_reason_to_string(i
, reasonbuf
,
3590 if (!strcasecmp(s
, reason_s
)) {
3598 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
3601 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
3602 * message's actions. The caller must initialize 'ofpacts' and retains
3603 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
3605 * Returns 0 if successful, otherwise an OFPERR_* value. */
3607 ofputil_decode_packet_out(struct ofputil_packet_out
*po
,
3608 const struct ofp_header
*oh
,
3609 struct ofpbuf
*ofpacts
)
3614 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
3615 raw
= ofpraw_pull_assert(&b
);
3617 ofpbuf_clear(ofpacts
);
3618 if (raw
== OFPRAW_OFPT11_PACKET_OUT
) {
3620 const struct ofp11_packet_out
*opo
= ofpbuf_pull(&b
, sizeof *opo
);
3622 po
->buffer_id
= ntohl(opo
->buffer_id
);
3623 error
= ofputil_port_from_ofp11(opo
->in_port
, &po
->in_port
);
3628 error
= ofpacts_pull_openflow_actions(&b
, ntohs(opo
->actions_len
),
3629 oh
->version
, ofpacts
);
3633 } else if (raw
== OFPRAW_OFPT10_PACKET_OUT
) {
3635 const struct ofp10_packet_out
*opo
= ofpbuf_pull(&b
, sizeof *opo
);
3637 po
->buffer_id
= ntohl(opo
->buffer_id
);
3638 po
->in_port
= u16_to_ofp(ntohs(opo
->in_port
));
3640 error
= ofpacts_pull_openflow_actions(&b
, ntohs(opo
->actions_len
),
3641 oh
->version
, ofpacts
);
3649 if (ofp_to_u16(po
->in_port
) >= ofp_to_u16(OFPP_MAX
)
3650 && po
->in_port
!= OFPP_LOCAL
3651 && po
->in_port
!= OFPP_NONE
&& po
->in_port
!= OFPP_CONTROLLER
) {
3652 VLOG_WARN_RL(&bad_ofmsg_rl
, "packet-out has bad input port %#"PRIx16
,
3654 return OFPERR_OFPBRC_BAD_PORT
;
3657 po
->ofpacts
= ofpacts
->data
;
3658 po
->ofpacts_len
= ofpacts
->size
;
3660 if (po
->buffer_id
== UINT32_MAX
) {
3661 po
->packet
= b
.data
;
3662 po
->packet_len
= b
.size
;
3671 /* ofputil_phy_port */
3673 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
3674 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD
== OFPPF_10MB_HD
); /* bit 0 */
3675 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD
== OFPPF_10MB_FD
); /* bit 1 */
3676 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD
== OFPPF_100MB_HD
); /* bit 2 */
3677 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD
== OFPPF_100MB_FD
); /* bit 3 */
3678 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD
== OFPPF_1GB_HD
); /* bit 4 */
3679 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD
== OFPPF_1GB_FD
); /* bit 5 */
3680 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD
== OFPPF_10GB_FD
); /* bit 6 */
3682 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
3683 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER
== (OFPPF10_COPPER
<< 4));
3684 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER
== (OFPPF10_FIBER
<< 4));
3685 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG
== (OFPPF10_AUTONEG
<< 4));
3686 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE
== (OFPPF10_PAUSE
<< 4));
3687 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM
== (OFPPF10_PAUSE_ASYM
<< 4));
3689 static enum netdev_features
3690 netdev_port_features_from_ofp10(ovs_be32 ofp10_
)
3692 uint32_t ofp10
= ntohl(ofp10_
);
3693 return (ofp10
& 0x7f) | ((ofp10
& 0xf80) << 4);
3697 netdev_port_features_to_ofp10(enum netdev_features features
)
3699 return htonl((features
& 0x7f) | ((features
& 0xf800) >> 4));
3702 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD
== OFPPF_10MB_HD
); /* bit 0 */
3703 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD
== OFPPF_10MB_FD
); /* bit 1 */
3704 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD
== OFPPF_100MB_HD
); /* bit 2 */
3705 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD
== OFPPF_100MB_FD
); /* bit 3 */
3706 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD
== OFPPF_1GB_HD
); /* bit 4 */
3707 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD
== OFPPF_1GB_FD
); /* bit 5 */
3708 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD
== OFPPF_10GB_FD
); /* bit 6 */
3709 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD
== OFPPF11_40GB_FD
); /* bit 7 */
3710 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD
== OFPPF11_100GB_FD
); /* bit 8 */
3711 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD
== OFPPF11_1TB_FD
); /* bit 9 */
3712 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER
== OFPPF11_OTHER
); /* bit 10 */
3713 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER
== OFPPF11_COPPER
); /* bit 11 */
3714 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER
== OFPPF11_FIBER
); /* bit 12 */
3715 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG
== OFPPF11_AUTONEG
); /* bit 13 */
3716 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE
== OFPPF11_PAUSE
); /* bit 14 */
3717 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM
== OFPPF11_PAUSE_ASYM
);/* bit 15 */
3719 static enum netdev_features
3720 netdev_port_features_from_ofp11(ovs_be32 ofp11
)
3722 return ntohl(ofp11
) & 0xffff;
3726 netdev_port_features_to_ofp11(enum netdev_features features
)
3728 return htonl(features
& 0xffff);
3732 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port
*pp
,
3733 const struct ofp10_phy_port
*opp
)
3735 pp
->port_no
= u16_to_ofp(ntohs(opp
->port_no
));
3736 pp
->hw_addr
= opp
->hw_addr
;
3737 ovs_strlcpy(pp
->name
, opp
->name
, OFP_MAX_PORT_NAME_LEN
);
3739 pp
->config
= ntohl(opp
->config
) & OFPPC10_ALL
;
3740 pp
->state
= ntohl(opp
->state
) & OFPPS10_ALL
;
3742 pp
->curr
= netdev_port_features_from_ofp10(opp
->curr
);
3743 pp
->advertised
= netdev_port_features_from_ofp10(opp
->advertised
);
3744 pp
->supported
= netdev_port_features_from_ofp10(opp
->supported
);
3745 pp
->peer
= netdev_port_features_from_ofp10(opp
->peer
);
3747 pp
->curr_speed
= netdev_features_to_bps(pp
->curr
, 0) / 1000;
3748 pp
->max_speed
= netdev_features_to_bps(pp
->supported
, 0) / 1000;
3754 ofputil_decode_ofp11_port(struct ofputil_phy_port
*pp
,
3755 const struct ofp11_port
*op
)
3759 error
= ofputil_port_from_ofp11(op
->port_no
, &pp
->port_no
);
3763 pp
->hw_addr
= op
->hw_addr
;
3764 ovs_strlcpy(pp
->name
, op
->name
, OFP_MAX_PORT_NAME_LEN
);
3766 pp
->config
= ntohl(op
->config
) & OFPPC11_ALL
;
3767 pp
->state
= ntohl(op
->state
) & OFPPS11_ALL
;
3769 pp
->curr
= netdev_port_features_from_ofp11(op
->curr
);
3770 pp
->advertised
= netdev_port_features_from_ofp11(op
->advertised
);
3771 pp
->supported
= netdev_port_features_from_ofp11(op
->supported
);
3772 pp
->peer
= netdev_port_features_from_ofp11(op
->peer
);
3774 pp
->curr_speed
= ntohl(op
->curr_speed
);
3775 pp
->max_speed
= ntohl(op
->max_speed
);
3781 parse_ofp14_port_ethernet_property(const struct ofpbuf
*payload
,
3782 struct ofputil_phy_port
*pp
)
3784 struct ofp14_port_desc_prop_ethernet
*eth
= payload
->data
;
3786 if (payload
->size
!= sizeof *eth
) {
3787 return OFPERR_OFPBPC_BAD_LEN
;
3790 pp
->curr
= netdev_port_features_from_ofp11(eth
->curr
);
3791 pp
->advertised
= netdev_port_features_from_ofp11(eth
->advertised
);
3792 pp
->supported
= netdev_port_features_from_ofp11(eth
->supported
);
3793 pp
->peer
= netdev_port_features_from_ofp11(eth
->peer
);
3795 pp
->curr_speed
= ntohl(eth
->curr_speed
);
3796 pp
->max_speed
= ntohl(eth
->max_speed
);
3802 ofputil_pull_ofp14_port(struct ofputil_phy_port
*pp
, struct ofpbuf
*msg
)
3804 struct ofpbuf properties
;
3805 struct ofp14_port
*op
;
3809 op
= ofpbuf_try_pull(msg
, sizeof *op
);
3811 return OFPERR_OFPBRC_BAD_LEN
;
3814 len
= ntohs(op
->length
);
3815 if (len
< sizeof *op
|| len
- sizeof *op
> msg
->size
) {
3816 return OFPERR_OFPBRC_BAD_LEN
;
3819 ofpbuf_use_const(&properties
, ofpbuf_pull(msg
, len
), len
);
3821 error
= ofputil_port_from_ofp11(op
->port_no
, &pp
->port_no
);
3825 pp
->hw_addr
= op
->hw_addr
;
3826 ovs_strlcpy(pp
->name
, op
->name
, OFP_MAX_PORT_NAME_LEN
);
3828 pp
->config
= ntohl(op
->config
) & OFPPC11_ALL
;
3829 pp
->state
= ntohl(op
->state
) & OFPPS11_ALL
;
3831 while (properties
.size
> 0) {
3832 struct ofpbuf payload
;
3836 error
= ofpprop_pull(&properties
, &payload
, &type
);
3842 case OFPPDPT14_ETHERNET
:
3843 error
= parse_ofp14_port_ethernet_property(&payload
, pp
);
3847 error
= OFPPROP_UNKNOWN(true, "port", type
);
3860 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port
*pp
,
3861 struct ofp10_phy_port
*opp
)
3863 memset(opp
, 0, sizeof *opp
);
3865 opp
->port_no
= htons(ofp_to_u16(pp
->port_no
));
3866 opp
->hw_addr
= pp
->hw_addr
;
3867 ovs_strlcpy(opp
->name
, pp
->name
, OFP_MAX_PORT_NAME_LEN
);
3869 opp
->config
= htonl(pp
->config
& OFPPC10_ALL
);
3870 opp
->state
= htonl(pp
->state
& OFPPS10_ALL
);
3872 opp
->curr
= netdev_port_features_to_ofp10(pp
->curr
);
3873 opp
->advertised
= netdev_port_features_to_ofp10(pp
->advertised
);
3874 opp
->supported
= netdev_port_features_to_ofp10(pp
->supported
);
3875 opp
->peer
= netdev_port_features_to_ofp10(pp
->peer
);
3879 ofputil_encode_ofp11_port(const struct ofputil_phy_port
*pp
,
3880 struct ofp11_port
*op
)
3882 memset(op
, 0, sizeof *op
);
3884 op
->port_no
= ofputil_port_to_ofp11(pp
->port_no
);
3885 op
->hw_addr
= pp
->hw_addr
;
3886 ovs_strlcpy(op
->name
, pp
->name
, OFP_MAX_PORT_NAME_LEN
);
3888 op
->config
= htonl(pp
->config
& OFPPC11_ALL
);
3889 op
->state
= htonl(pp
->state
& OFPPS11_ALL
);
3891 op
->curr
= netdev_port_features_to_ofp11(pp
->curr
);
3892 op
->advertised
= netdev_port_features_to_ofp11(pp
->advertised
);
3893 op
->supported
= netdev_port_features_to_ofp11(pp
->supported
);
3894 op
->peer
= netdev_port_features_to_ofp11(pp
->peer
);
3896 op
->curr_speed
= htonl(pp
->curr_speed
);
3897 op
->max_speed
= htonl(pp
->max_speed
);
3901 ofputil_put_ofp14_port(const struct ofputil_phy_port
*pp
,
3904 struct ofp14_port
*op
;
3905 struct ofp14_port_desc_prop_ethernet
*eth
;
3907 ofpbuf_prealloc_tailroom(b
, sizeof *op
+ sizeof *eth
);
3909 op
= ofpbuf_put_zeros(b
, sizeof *op
);
3910 op
->port_no
= ofputil_port_to_ofp11(pp
->port_no
);
3911 op
->length
= htons(sizeof *op
+ sizeof *eth
);
3912 op
->hw_addr
= pp
->hw_addr
;
3913 ovs_strlcpy(op
->name
, pp
->name
, sizeof op
->name
);
3914 op
->config
= htonl(pp
->config
& OFPPC11_ALL
);
3915 op
->state
= htonl(pp
->state
& OFPPS11_ALL
);
3917 eth
= ofpprop_put_zeros(b
, OFPPDPT14_ETHERNET
, sizeof *eth
);
3918 eth
->curr
= netdev_port_features_to_ofp11(pp
->curr
);
3919 eth
->advertised
= netdev_port_features_to_ofp11(pp
->advertised
);
3920 eth
->supported
= netdev_port_features_to_ofp11(pp
->supported
);
3921 eth
->peer
= netdev_port_features_to_ofp11(pp
->peer
);
3922 eth
->curr_speed
= htonl(pp
->curr_speed
);
3923 eth
->max_speed
= htonl(pp
->max_speed
);
3927 ofputil_put_phy_port(enum ofp_version ofp_version
,
3928 const struct ofputil_phy_port
*pp
, struct ofpbuf
*b
)
3930 switch (ofp_version
) {
3931 case OFP10_VERSION
: {
3932 struct ofp10_phy_port
*opp
= ofpbuf_put_uninit(b
, sizeof *opp
);
3933 ofputil_encode_ofp10_phy_port(pp
, opp
);
3939 case OFP13_VERSION
: {
3940 struct ofp11_port
*op
= ofpbuf_put_uninit(b
, sizeof *op
);
3941 ofputil_encode_ofp11_port(pp
, op
);
3947 ofputil_put_ofp14_port(pp
, b
);
3956 ofputil_decode_port_desc_stats_request(const struct ofp_header
*request
,
3962 ofpbuf_use_const(&b
, request
, ntohs(request
->length
));
3963 raw
= ofpraw_pull_assert(&b
);
3964 if (raw
== OFPRAW_OFPST10_PORT_DESC_REQUEST
) {
3967 } else if (raw
== OFPRAW_OFPST15_PORT_DESC_REQUEST
) {
3968 ovs_be32
*ofp11_port
;
3970 ofp11_port
= ofpbuf_pull(&b
, sizeof *ofp11_port
);
3971 return ofputil_port_from_ofp11(*ofp11_port
, port
);
3978 ofputil_encode_port_desc_stats_request(enum ofp_version ofp_version
,
3981 struct ofpbuf
*request
;
3983 switch (ofp_version
) {
3989 request
= ofpraw_alloc(OFPRAW_OFPST10_PORT_DESC_REQUEST
,
3992 case OFP15_VERSION
:{
3993 struct ofp15_port_desc_request
*req
;
3994 request
= ofpraw_alloc(OFPRAW_OFPST15_PORT_DESC_REQUEST
,
3996 req
= ofpbuf_put_zeros(request
, sizeof *req
);
3997 req
->port_no
= ofputil_port_to_ofp11(port
);
4008 ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port
*pp
,
4009 struct ovs_list
*replies
)
4011 struct ofpbuf
*reply
= ofpbuf_from_list(list_back(replies
));
4012 size_t start_ofs
= reply
->size
;
4014 ofputil_put_phy_port(ofpmp_version(replies
), pp
, reply
);
4015 ofpmp_postappend(replies
, start_ofs
);
4018 /* ofputil_switch_config */
4020 /* Decodes 'oh', which must be an OFPT_GET_CONFIG_REPLY or OFPT_SET_CONFIG
4021 * message, into 'config'. Returns false if 'oh' contained any flags that
4022 * aren't specified in its version of OpenFlow, true otherwise. */
4024 ofputil_decode_switch_config(const struct ofp_header
*oh
,
4025 struct ofputil_switch_config
*config
)
4027 const struct ofp_switch_config
*osc
;
4030 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
4031 ofpraw_pull_assert(&b
);
4032 osc
= ofpbuf_pull(&b
, sizeof *osc
);
4034 config
->frag
= ntohs(osc
->flags
) & OFPC_FRAG_MASK
;
4035 config
->miss_send_len
= ntohs(osc
->miss_send_len
);
4037 ovs_be16 valid_mask
= htons(OFPC_FRAG_MASK
);
4038 if (oh
->version
< OFP13_VERSION
) {
4039 const ovs_be16 ttl_bit
= htons(OFPC_INVALID_TTL_TO_CONTROLLER
);
4040 valid_mask
|= ttl_bit
;
4041 config
->invalid_ttl_to_controller
= (osc
->flags
& ttl_bit
) != 0;
4043 config
->invalid_ttl_to_controller
= -1;
4046 return !(osc
->flags
& ~valid_mask
);
4050 ofputil_decode_get_config_reply(const struct ofp_header
*oh
,
4051 struct ofputil_switch_config
*config
)
4053 ofputil_decode_switch_config(oh
, config
);
4057 ofputil_decode_set_config(const struct ofp_header
*oh
,
4058 struct ofputil_switch_config
*config
)
4060 return (ofputil_decode_switch_config(oh
, config
)
4062 : OFPERR_OFPSCFC_BAD_FLAGS
);
4065 static struct ofpbuf
*
4066 ofputil_put_switch_config(const struct ofputil_switch_config
*config
,
4069 const struct ofp_header
*oh
= b
->data
;
4070 struct ofp_switch_config
*osc
= ofpbuf_put_zeros(b
, sizeof *osc
);
4071 osc
->flags
= htons(config
->frag
);
4072 if (config
->invalid_ttl_to_controller
> 0 && oh
->version
< OFP13_VERSION
) {
4073 osc
->flags
|= htons(OFPC_INVALID_TTL_TO_CONTROLLER
);
4075 osc
->miss_send_len
= htons(config
->miss_send_len
);
4080 ofputil_encode_get_config_reply(const struct ofp_header
*request
,
4081 const struct ofputil_switch_config
*config
)
4083 struct ofpbuf
*b
= ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY
,
4085 return ofputil_put_switch_config(config
, b
);
4089 ofputil_encode_set_config(const struct ofputil_switch_config
*config
,
4090 enum ofp_version version
)
4092 struct ofpbuf
*b
= ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG
, version
, 0);
4093 return ofputil_put_switch_config(config
, b
);
4096 /* ofputil_switch_features */
4098 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
4099 OFPC_IP_REASM | OFPC_QUEUE_STATS)
4100 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS
== OFPC_FLOW_STATS
);
4101 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS
== OFPC_TABLE_STATS
);
4102 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS
== OFPC_PORT_STATS
);
4103 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM
== OFPC_IP_REASM
);
4104 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS
== OFPC_QUEUE_STATS
);
4105 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP
== OFPC_ARP_MATCH_IP
);
4108 ofputil_capabilities_mask(enum ofp_version ofp_version
)
4110 /* Handle capabilities whose bit is unique for all OpenFlow versions */
4111 switch (ofp_version
) {
4114 return OFPC_COMMON
| OFPC_ARP_MATCH_IP
;
4119 return OFPC_COMMON
| OFPC12_PORT_BLOCKED
;
4121 /* Caller needs to check osf->header.version itself */
4126 /* Decodes an OpenFlow 1.0 or 1.1 "switch_features" structure 'osf' into an
4127 * abstract representation in '*features'. Initializes '*b' to iterate over
4128 * the OpenFlow port structures following 'osf' with later calls to
4129 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an
4130 * OFPERR_* value. */
4132 ofputil_decode_switch_features(const struct ofp_header
*oh
,
4133 struct ofputil_switch_features
*features
,
4136 const struct ofp_switch_features
*osf
;
4139 ofpbuf_use_const(b
, oh
, ntohs(oh
->length
));
4140 raw
= ofpraw_pull_assert(b
);
4142 osf
= ofpbuf_pull(b
, sizeof *osf
);
4143 features
->datapath_id
= ntohll(osf
->datapath_id
);
4144 features
->n_buffers
= ntohl(osf
->n_buffers
);
4145 features
->n_tables
= osf
->n_tables
;
4146 features
->auxiliary_id
= 0;
4148 features
->capabilities
= ntohl(osf
->capabilities
) &
4149 ofputil_capabilities_mask(oh
->version
);
4151 if (raw
== OFPRAW_OFPT10_FEATURES_REPLY
) {
4152 if (osf
->capabilities
& htonl(OFPC10_STP
)) {
4153 features
->capabilities
|= OFPUTIL_C_STP
;
4155 features
->ofpacts
= ofpact_bitmap_from_openflow(osf
->actions
,
4157 } else if (raw
== OFPRAW_OFPT11_FEATURES_REPLY
4158 || raw
== OFPRAW_OFPT13_FEATURES_REPLY
) {
4159 if (osf
->capabilities
& htonl(OFPC11_GROUP_STATS
)) {
4160 features
->capabilities
|= OFPUTIL_C_GROUP_STATS
;
4162 features
->ofpacts
= 0;
4163 if (raw
== OFPRAW_OFPT13_FEATURES_REPLY
) {
4164 features
->auxiliary_id
= osf
->auxiliary_id
;
4167 return OFPERR_OFPBRC_BAD_VERSION
;
4173 /* In OpenFlow 1.0, 1.1, and 1.2, an OFPT_FEATURES_REPLY message lists all the
4174 * switch's ports, unless there are too many to fit. In OpenFlow 1.3 and
4175 * later, an OFPT_FEATURES_REPLY does not list ports at all.
4177 * Given a buffer 'b' that contains a Features Reply message, this message
4178 * checks if it contains a complete list of the switch's ports. Returns true,
4179 * if so. Returns false if the list is missing (OF1.3+) or incomplete
4180 * (OF1.0/1.1/1.2), and in the latter case removes all of the ports from the
4183 * When this function returns false, the caller should send an OFPST_PORT_DESC
4184 * stats request to get the ports. */
4186 ofputil_switch_features_has_ports(struct ofpbuf
*b
)
4188 struct ofp_header
*oh
= b
->data
;
4189 size_t phy_port_size
;
4191 if (oh
->version
>= OFP13_VERSION
) {
4192 /* OpenFlow 1.3+ never has ports in the feature reply. */
4196 phy_port_size
= (oh
->version
== OFP10_VERSION
4197 ? sizeof(struct ofp10_phy_port
)
4198 : sizeof(struct ofp11_port
));
4199 if (ntohs(oh
->length
) + phy_port_size
<= UINT16_MAX
) {
4200 /* There's room for additional ports in the feature reply.
4201 * Assume that the list is complete. */
4205 /* The feature reply has no room for more ports. Probably the list is
4206 * truncated. Drop the ports and tell the caller to retrieve them with
4207 * OFPST_PORT_DESC. */
4208 b
->size
= sizeof *oh
+ sizeof(struct ofp_switch_features
);
4209 ofpmsg_update_length(b
);
4213 /* Returns a buffer owned by the caller that encodes 'features' in the format
4214 * required by 'protocol' with the given 'xid'. The caller should append port
4215 * information to the buffer with subsequent calls to
4216 * ofputil_put_switch_features_port(). */
4218 ofputil_encode_switch_features(const struct ofputil_switch_features
*features
,
4219 enum ofputil_protocol protocol
, ovs_be32 xid
)
4221 struct ofp_switch_features
*osf
;
4223 enum ofp_version version
;
4226 version
= ofputil_protocol_to_ofp_version(protocol
);
4229 raw
= OFPRAW_OFPT10_FEATURES_REPLY
;
4233 raw
= OFPRAW_OFPT11_FEATURES_REPLY
;
4238 raw
= OFPRAW_OFPT13_FEATURES_REPLY
;
4243 b
= ofpraw_alloc_xid(raw
, version
, xid
, 0);
4244 osf
= ofpbuf_put_zeros(b
, sizeof *osf
);
4245 osf
->datapath_id
= htonll(features
->datapath_id
);
4246 osf
->n_buffers
= htonl(features
->n_buffers
);
4247 osf
->n_tables
= features
->n_tables
;
4249 osf
->capabilities
= htonl(features
->capabilities
& OFPC_COMMON
);
4250 osf
->capabilities
= htonl(features
->capabilities
&
4251 ofputil_capabilities_mask(version
));
4254 if (features
->capabilities
& OFPUTIL_C_STP
) {
4255 osf
->capabilities
|= htonl(OFPC10_STP
);
4257 osf
->actions
= ofpact_bitmap_to_openflow(features
->ofpacts
,
4263 osf
->auxiliary_id
= features
->auxiliary_id
;
4267 if (features
->capabilities
& OFPUTIL_C_GROUP_STATS
) {
4268 osf
->capabilities
|= htonl(OFPC11_GROUP_STATS
);
4278 /* Encodes 'pp' into the format required by the switch_features message already
4279 * in 'b', which should have been returned by ofputil_encode_switch_features(),
4280 * and appends the encoded version to 'b'. */
4282 ofputil_put_switch_features_port(const struct ofputil_phy_port
*pp
,
4285 const struct ofp_header
*oh
= b
->data
;
4287 if (oh
->version
< OFP13_VERSION
) {
4288 /* Try adding a port description to the message, but drop it again if
4289 * the buffer overflows. (This possibility for overflow is why
4290 * OpenFlow 1.3+ moved port descriptions into a multipart message.) */
4291 size_t start_ofs
= b
->size
;
4292 ofputil_put_phy_port(oh
->version
, pp
, b
);
4293 if (b
->size
> UINT16_MAX
) {
4294 b
->size
= start_ofs
;
4299 /* ofputil_port_status */
4301 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
4302 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
4304 ofputil_decode_port_status(const struct ofp_header
*oh
,
4305 struct ofputil_port_status
*ps
)
4307 const struct ofp_port_status
*ops
;
4311 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
4312 ofpraw_pull_assert(&b
);
4313 ops
= ofpbuf_pull(&b
, sizeof *ops
);
4315 if (ops
->reason
!= OFPPR_ADD
&&
4316 ops
->reason
!= OFPPR_DELETE
&&
4317 ops
->reason
!= OFPPR_MODIFY
) {
4318 return OFPERR_NXBRC_BAD_REASON
;
4320 ps
->reason
= ops
->reason
;
4322 retval
= ofputil_pull_phy_port(oh
->version
, &b
, &ps
->desc
);
4323 ovs_assert(retval
!= EOF
);
4327 /* Converts the abstract form of a "port status" message in '*ps' into an
4328 * OpenFlow message suitable for 'protocol', and returns that encoded form in
4329 * a buffer owned by the caller. */
4331 ofputil_encode_port_status(const struct ofputil_port_status
*ps
,
4332 enum ofputil_protocol protocol
)
4334 struct ofp_port_status
*ops
;
4336 enum ofp_version version
;
4339 version
= ofputil_protocol_to_ofp_version(protocol
);
4342 raw
= OFPRAW_OFPT10_PORT_STATUS
;
4348 raw
= OFPRAW_OFPT11_PORT_STATUS
;
4353 raw
= OFPRAW_OFPT14_PORT_STATUS
;
4360 b
= ofpraw_alloc_xid(raw
, version
, htonl(0), 0);
4361 ops
= ofpbuf_put_zeros(b
, sizeof *ops
);
4362 ops
->reason
= ps
->reason
;
4363 ofputil_put_phy_port(version
, &ps
->desc
, b
);
4364 ofpmsg_update_length(b
);
4368 /* ofputil_port_mod */
4371 parse_port_mod_ethernet_property(struct ofpbuf
*property
,
4372 struct ofputil_port_mod
*pm
)
4377 error
= ofpprop_parse_be32(property
, &advertise
);
4379 pm
->advertise
= netdev_port_features_from_ofp11(advertise
);
4384 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
4385 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
4387 ofputil_decode_port_mod(const struct ofp_header
*oh
,
4388 struct ofputil_port_mod
*pm
, bool loose
)
4393 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
4394 raw
= ofpraw_pull_assert(&b
);
4396 if (raw
== OFPRAW_OFPT10_PORT_MOD
) {
4397 const struct ofp10_port_mod
*opm
= b
.data
;
4399 pm
->port_no
= u16_to_ofp(ntohs(opm
->port_no
));
4400 pm
->hw_addr
= opm
->hw_addr
;
4401 pm
->config
= ntohl(opm
->config
) & OFPPC10_ALL
;
4402 pm
->mask
= ntohl(opm
->mask
) & OFPPC10_ALL
;
4403 pm
->advertise
= netdev_port_features_from_ofp10(opm
->advertise
);
4404 } else if (raw
== OFPRAW_OFPT11_PORT_MOD
) {
4405 const struct ofp11_port_mod
*opm
= b
.data
;
4408 error
= ofputil_port_from_ofp11(opm
->port_no
, &pm
->port_no
);
4413 pm
->hw_addr
= opm
->hw_addr
;
4414 pm
->config
= ntohl(opm
->config
) & OFPPC11_ALL
;
4415 pm
->mask
= ntohl(opm
->mask
) & OFPPC11_ALL
;
4416 pm
->advertise
= netdev_port_features_from_ofp11(opm
->advertise
);
4417 } else if (raw
== OFPRAW_OFPT14_PORT_MOD
) {
4418 const struct ofp14_port_mod
*opm
= ofpbuf_pull(&b
, sizeof *opm
);
4421 memset(pm
, 0, sizeof *pm
);
4423 error
= ofputil_port_from_ofp11(opm
->port_no
, &pm
->port_no
);
4428 pm
->hw_addr
= opm
->hw_addr
;
4429 pm
->config
= ntohl(opm
->config
) & OFPPC11_ALL
;
4430 pm
->mask
= ntohl(opm
->mask
) & OFPPC11_ALL
;
4432 while (b
.size
> 0) {
4433 struct ofpbuf property
;
4437 error
= ofpprop_pull(&b
, &property
, &type
);
4443 case OFPPMPT14_ETHERNET
:
4444 error
= parse_port_mod_ethernet_property(&property
, pm
);
4448 error
= OFPPROP_UNKNOWN(loose
, "port_mod", type
);
4457 return OFPERR_OFPBRC_BAD_TYPE
;
4460 pm
->config
&= pm
->mask
;
4464 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
4465 * message suitable for 'protocol', and returns that encoded form in a buffer
4466 * owned by the caller. */
4468 ofputil_encode_port_mod(const struct ofputil_port_mod
*pm
,
4469 enum ofputil_protocol protocol
)
4471 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
4474 switch (ofp_version
) {
4475 case OFP10_VERSION
: {
4476 struct ofp10_port_mod
*opm
;
4478 b
= ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD
, ofp_version
, 0);
4479 opm
= ofpbuf_put_zeros(b
, sizeof *opm
);
4480 opm
->port_no
= htons(ofp_to_u16(pm
->port_no
));
4481 opm
->hw_addr
= pm
->hw_addr
;
4482 opm
->config
= htonl(pm
->config
& OFPPC10_ALL
);
4483 opm
->mask
= htonl(pm
->mask
& OFPPC10_ALL
);
4484 opm
->advertise
= netdev_port_features_to_ofp10(pm
->advertise
);
4490 case OFP13_VERSION
: {
4491 struct ofp11_port_mod
*opm
;
4493 b
= ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD
, ofp_version
, 0);
4494 opm
= ofpbuf_put_zeros(b
, sizeof *opm
);
4495 opm
->port_no
= ofputil_port_to_ofp11(pm
->port_no
);
4496 opm
->hw_addr
= pm
->hw_addr
;
4497 opm
->config
= htonl(pm
->config
& OFPPC11_ALL
);
4498 opm
->mask
= htonl(pm
->mask
& OFPPC11_ALL
);
4499 opm
->advertise
= netdev_port_features_to_ofp11(pm
->advertise
);
4503 case OFP15_VERSION
: {
4504 struct ofp14_port_mod
*opm
;
4506 b
= ofpraw_alloc(OFPRAW_OFPT14_PORT_MOD
, ofp_version
, 0);
4507 opm
= ofpbuf_put_zeros(b
, sizeof *opm
);
4508 opm
->port_no
= ofputil_port_to_ofp11(pm
->port_no
);
4509 opm
->hw_addr
= pm
->hw_addr
;
4510 opm
->config
= htonl(pm
->config
& OFPPC11_ALL
);
4511 opm
->mask
= htonl(pm
->mask
& OFPPC11_ALL
);
4513 if (pm
->advertise
) {
4514 ofpprop_put_be32(b
, OFPPMPT14_ETHERNET
,
4515 netdev_port_features_to_ofp11(pm
->advertise
));
4526 /* Table features. */
4529 pull_table_feature_property(struct ofpbuf
*msg
, struct ofpbuf
*payload
,
4534 error
= ofpprop_pull(msg
, payload
, typep
);
4535 if (payload
&& !error
) {
4536 ofpbuf_pull(payload
, (char *)payload
->msg
- (char *)payload
->header
);
4542 parse_action_bitmap(struct ofpbuf
*payload
, enum ofp_version ofp_version
,
4547 while (payload
->size
> 0) {
4551 error
= ofpprop_pull__(payload
, NULL
, 1, 0x10000, &type
);
4555 if (type
< CHAR_BIT
* sizeof types
) {
4556 types
|= 1u << type
;
4560 *ofpacts
= ofpact_bitmap_from_openflow(htonl(types
), ofp_version
);
4565 parse_instruction_ids(struct ofpbuf
*payload
, bool loose
, uint32_t *insts
)
4568 while (payload
->size
> 0) {
4569 enum ovs_instruction_type inst
;
4573 /* OF1.3 and OF1.4 aren't clear about padding in the instruction IDs.
4574 * It seems clear that they aren't padded to 8 bytes, though, because
4575 * both standards say that "non-experimenter instructions are 4 bytes"
4576 * and do not mention any padding before the first instruction ID.
4577 * (There wouldn't be any point in padding to 8 bytes if the IDs were
4578 * aligned on an odd 4-byte boundary.)
4580 * Anyway, we just assume they're all glommed together on byte
4582 error
= ofpprop_pull__(payload
, NULL
, 1, 0x10000, &ofpit
);
4587 error
= ovs_instruction_type_from_inst_type(&inst
, ofpit
);
4589 *insts
|= 1u << inst
;
4590 } else if (!loose
) {
4598 parse_table_features_next_table(struct ofpbuf
*payload
,
4599 unsigned long int *next_tables
)
4603 memset(next_tables
, 0, bitmap_n_bytes(255));
4604 for (i
= 0; i
< payload
->size
; i
++) {
4605 uint8_t id
= ((const uint8_t *) payload
->data
)[i
];
4607 return OFPERR_OFPBPC_BAD_VALUE
;
4609 bitmap_set1(next_tables
, id
);
4615 parse_oxms(struct ofpbuf
*payload
, bool loose
,
4616 struct mf_bitmap
*exactp
, struct mf_bitmap
*maskedp
)
4618 struct mf_bitmap exact
= MF_BITMAP_INITIALIZER
;
4619 struct mf_bitmap masked
= MF_BITMAP_INITIALIZER
;
4621 while (payload
->size
> 0) {
4622 const struct mf_field
*field
;
4626 error
= nx_pull_header(payload
, &field
, &hasmask
);
4628 bitmap_set1(hasmask
? masked
.bm
: exact
.bm
, field
->id
);
4629 } else if (error
!= OFPERR_OFPBMC_BAD_FIELD
|| !loose
) {
4635 } else if (!bitmap_is_all_zeros(exact
.bm
, MFF_N_IDS
)) {
4636 return OFPERR_OFPBMC_BAD_MASK
;
4640 } else if (!bitmap_is_all_zeros(masked
.bm
, MFF_N_IDS
)) {
4641 return OFPERR_OFPBMC_BAD_MASK
;
4646 /* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract
4647 * ofputil_table_features in 'tf'.
4649 * If 'loose' is true, this function ignores properties and values that it does
4650 * not understand, as a controller would want to do when interpreting
4651 * capabilities provided by a switch. If 'loose' is false, this function
4652 * treats unknown properties and values as an error, as a switch would want to
4653 * do when interpreting a configuration request made by a controller.
4655 * A single OpenFlow message can specify features for multiple tables. Calling
4656 * this function multiple times for a single 'msg' iterates through the tables
4657 * in the message. The caller must initially leave 'msg''s layer pointers null
4658 * and not modify them between calls.
4660 * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise
4661 * a positive "enum ofperr" value. */
4663 ofputil_decode_table_features(struct ofpbuf
*msg
,
4664 struct ofputil_table_features
*tf
, bool loose
)
4666 const struct ofp_header
*oh
;
4667 struct ofp13_table_features
*otf
;
4668 struct ofpbuf properties
;
4671 memset(tf
, 0, sizeof *tf
);
4674 ofpraw_pull_assert(msg
);
4682 if (msg
->size
< sizeof *otf
) {
4683 return OFPERR_OFPBPC_BAD_LEN
;
4687 len
= ntohs(otf
->length
);
4688 if (len
< sizeof *otf
|| len
% 8 || len
> msg
->size
) {
4689 return OFPERR_OFPBPC_BAD_LEN
;
4691 ofpbuf_use_const(&properties
, ofpbuf_pull(msg
, len
), len
);
4692 ofpbuf_pull(&properties
, sizeof *otf
);
4694 tf
->table_id
= otf
->table_id
;
4695 if (tf
->table_id
== OFPTT_ALL
) {
4696 return OFPERR_OFPTFFC_BAD_TABLE
;
4699 ovs_strlcpy(tf
->name
, otf
->name
, OFP_MAX_TABLE_NAME_LEN
);
4700 tf
->metadata_match
= otf
->metadata_match
;
4701 tf
->metadata_write
= otf
->metadata_write
;
4702 tf
->miss_config
= OFPUTIL_TABLE_MISS_DEFAULT
;
4703 if (oh
->version
>= OFP14_VERSION
) {
4704 uint32_t caps
= ntohl(otf
->capabilities
);
4705 tf
->supports_eviction
= (caps
& OFPTC14_EVICTION
) != 0;
4706 tf
->supports_vacancy_events
= (caps
& OFPTC14_VACANCY_EVENTS
) != 0;
4708 tf
->supports_eviction
= -1;
4709 tf
->supports_vacancy_events
= -1;
4711 tf
->max_entries
= ntohl(otf
->max_entries
);
4713 while (properties
.size
> 0) {
4714 struct ofpbuf payload
;
4718 error
= pull_table_feature_property(&properties
, &payload
, &type
);
4723 switch ((enum ofp13_table_feature_prop_type
) type
) {
4724 case OFPTFPT13_INSTRUCTIONS
:
4725 error
= parse_instruction_ids(&payload
, loose
,
4726 &tf
->nonmiss
.instructions
);
4728 case OFPTFPT13_INSTRUCTIONS_MISS
:
4729 error
= parse_instruction_ids(&payload
, loose
,
4730 &tf
->miss
.instructions
);
4733 case OFPTFPT13_NEXT_TABLES
:
4734 error
= parse_table_features_next_table(&payload
,
4737 case OFPTFPT13_NEXT_TABLES_MISS
:
4738 error
= parse_table_features_next_table(&payload
, tf
->miss
.next
);
4741 case OFPTFPT13_WRITE_ACTIONS
:
4742 error
= parse_action_bitmap(&payload
, oh
->version
,
4743 &tf
->nonmiss
.write
.ofpacts
);
4745 case OFPTFPT13_WRITE_ACTIONS_MISS
:
4746 error
= parse_action_bitmap(&payload
, oh
->version
,
4747 &tf
->miss
.write
.ofpacts
);
4750 case OFPTFPT13_APPLY_ACTIONS
:
4751 error
= parse_action_bitmap(&payload
, oh
->version
,
4752 &tf
->nonmiss
.apply
.ofpacts
);
4754 case OFPTFPT13_APPLY_ACTIONS_MISS
:
4755 error
= parse_action_bitmap(&payload
, oh
->version
,
4756 &tf
->miss
.apply
.ofpacts
);
4759 case OFPTFPT13_MATCH
:
4760 error
= parse_oxms(&payload
, loose
, &tf
->match
, &tf
->mask
);
4762 case OFPTFPT13_WILDCARDS
:
4763 error
= parse_oxms(&payload
, loose
, &tf
->wildcard
, NULL
);
4766 case OFPTFPT13_WRITE_SETFIELD
:
4767 error
= parse_oxms(&payload
, loose
,
4768 &tf
->nonmiss
.write
.set_fields
, NULL
);
4770 case OFPTFPT13_WRITE_SETFIELD_MISS
:
4771 error
= parse_oxms(&payload
, loose
,
4772 &tf
->miss
.write
.set_fields
, NULL
);
4774 case OFPTFPT13_APPLY_SETFIELD
:
4775 error
= parse_oxms(&payload
, loose
,
4776 &tf
->nonmiss
.apply
.set_fields
, NULL
);
4778 case OFPTFPT13_APPLY_SETFIELD_MISS
:
4779 error
= parse_oxms(&payload
, loose
,
4780 &tf
->miss
.apply
.set_fields
, NULL
);
4783 case OFPTFPT13_EXPERIMENTER
:
4784 case OFPTFPT13_EXPERIMENTER_MISS
:
4786 error
= OFPPROP_UNKNOWN(loose
, "table features", type
);
4794 /* Fix inconsistencies:
4796 * - Turn on 'match' bits that are set in 'mask', because maskable
4797 * fields are matchable.
4799 * - Turn on 'wildcard' bits that are set in 'mask', because a field
4800 * that is arbitrarily maskable can be wildcarded entirely.
4802 * - Turn off 'wildcard' bits that are not in 'match', because a field
4803 * must be matchable for it to be meaningfully wildcarded. */
4804 bitmap_or(tf
->match
.bm
, tf
->mask
.bm
, MFF_N_IDS
);
4805 bitmap_or(tf
->wildcard
.bm
, tf
->mask
.bm
, MFF_N_IDS
);
4806 bitmap_and(tf
->wildcard
.bm
, tf
->match
.bm
, MFF_N_IDS
);
4811 /* Encodes and returns a request to obtain the table features of a switch.
4812 * The message is encoded for OpenFlow version 'ofp_version'. */
4814 ofputil_encode_table_features_request(enum ofp_version ofp_version
)
4816 struct ofpbuf
*request
= NULL
;
4818 switch (ofp_version
) {
4822 ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later "
4823 "(\'-O OpenFlow13\')");
4827 request
= ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST
,
4838 put_fields_property(struct ofpbuf
*reply
,
4839 const struct mf_bitmap
*fields
,
4840 const struct mf_bitmap
*masks
,
4841 enum ofp13_table_feature_prop_type property
,
4842 enum ofp_version version
)
4847 start_ofs
= ofpprop_start(reply
, property
);
4848 BITMAP_FOR_EACH_1 (field
, MFF_N_IDS
, fields
->bm
) {
4849 nx_put_header(reply
, field
, version
,
4850 masks
&& bitmap_is_set(masks
->bm
, field
));
4852 ofpprop_end(reply
, start_ofs
);
4856 put_table_action_features(struct ofpbuf
*reply
,
4857 const struct ofputil_table_action_features
*taf
,
4858 enum ofp13_table_feature_prop_type actions_type
,
4859 enum ofp13_table_feature_prop_type set_fields_type
,
4860 int miss_offset
, enum ofp_version version
)
4862 ofpprop_put_bitmap(reply
, actions_type
+ miss_offset
,
4863 ntohl(ofpact_bitmap_to_openflow(taf
->ofpacts
,
4865 put_fields_property(reply
, &taf
->set_fields
, NULL
,
4866 set_fields_type
+ miss_offset
, version
);
4870 put_table_instruction_features(
4871 struct ofpbuf
*reply
, const struct ofputil_table_instruction_features
*tif
,
4872 int miss_offset
, enum ofp_version version
)
4877 ofpprop_put_bitmap(reply
, OFPTFPT13_INSTRUCTIONS
+ miss_offset
,
4878 ntohl(ovsinst_bitmap_to_openflow(tif
->instructions
,
4881 start_ofs
= ofpprop_start(reply
, OFPTFPT13_NEXT_TABLES
+ miss_offset
);
4882 BITMAP_FOR_EACH_1 (table_id
, 255, tif
->next
) {
4883 ofpbuf_put(reply
, &table_id
, 1);
4885 ofpprop_end(reply
, start_ofs
);
4887 put_table_action_features(reply
, &tif
->write
,
4888 OFPTFPT13_WRITE_ACTIONS
,
4889 OFPTFPT13_WRITE_SETFIELD
, miss_offset
, version
);
4890 put_table_action_features(reply
, &tif
->apply
,
4891 OFPTFPT13_APPLY_ACTIONS
,
4892 OFPTFPT13_APPLY_SETFIELD
, miss_offset
, version
);
4896 ofputil_append_table_features_reply(const struct ofputil_table_features
*tf
,
4897 struct ovs_list
*replies
)
4899 struct ofpbuf
*reply
= ofpbuf_from_list(list_back(replies
));
4900 enum ofp_version version
= ofpmp_version(replies
);
4901 size_t start_ofs
= reply
->size
;
4902 struct ofp13_table_features
*otf
;
4904 otf
= ofpbuf_put_zeros(reply
, sizeof *otf
);
4905 otf
->table_id
= tf
->table_id
;
4906 ovs_strlcpy(otf
->name
, tf
->name
, sizeof otf
->name
);
4907 otf
->metadata_match
= tf
->metadata_match
;
4908 otf
->metadata_write
= tf
->metadata_write
;
4909 if (version
>= OFP14_VERSION
) {
4910 if (tf
->supports_eviction
) {
4911 otf
->capabilities
|= htonl(OFPTC14_EVICTION
);
4913 if (tf
->supports_vacancy_events
) {
4914 otf
->capabilities
|= htonl(OFPTC14_VACANCY_EVENTS
);
4917 otf
->max_entries
= htonl(tf
->max_entries
);
4919 put_table_instruction_features(reply
, &tf
->nonmiss
, 0, version
);
4920 put_table_instruction_features(reply
, &tf
->miss
, 1, version
);
4922 put_fields_property(reply
, &tf
->match
, &tf
->mask
,
4923 OFPTFPT13_MATCH
, version
);
4924 put_fields_property(reply
, &tf
->wildcard
, NULL
,
4925 OFPTFPT13_WILDCARDS
, version
);
4927 otf
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *otf
);
4928 otf
->length
= htons(reply
->size
- start_ofs
);
4929 ofpmp_postappend(replies
, start_ofs
);
4933 parse_table_desc_vacancy_property(struct ofpbuf
*property
,
4934 struct ofputil_table_desc
*td
)
4936 struct ofp14_table_mod_prop_vacancy
*otv
= property
->data
;
4938 if (property
->size
!= sizeof *otv
) {
4939 return OFPERR_OFPBPC_BAD_LEN
;
4942 td
->table_vacancy
.vacancy_down
= otv
->vacancy_down
;
4943 td
->table_vacancy
.vacancy_up
= otv
->vacancy_up
;
4944 td
->table_vacancy
.vacancy
= otv
->vacancy
;
4948 /* Decodes the next OpenFlow "table desc" message (of possibly several) from
4949 * 'msg' into an abstract form in '*td'. Returns 0 if successful, EOF if the
4950 * last "table desc" in 'msg' was already decoded, otherwise an OFPERR_*
4953 ofputil_decode_table_desc(struct ofpbuf
*msg
,
4954 struct ofputil_table_desc
*td
,
4955 enum ofp_version version
)
4957 struct ofp14_table_desc
*otd
;
4958 struct ofpbuf properties
;
4961 memset(td
, 0, sizeof *td
);
4964 ofpraw_pull_assert(msg
);
4971 otd
= ofpbuf_try_pull(msg
, sizeof *otd
);
4973 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFP14_TABLE_DESC reply has %"PRIu32
" "
4974 "leftover bytes at end", msg
->size
);
4975 return OFPERR_OFPBRC_BAD_LEN
;
4978 td
->table_id
= otd
->table_id
;
4979 length
= ntohs(otd
->length
);
4980 if (length
< sizeof *otd
|| length
- sizeof *otd
> msg
->size
) {
4981 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFP14_TABLE_DESC reply claims invalid "
4982 "length %"PRIuSIZE
, length
);
4983 return OFPERR_OFPBRC_BAD_LEN
;
4985 length
-= sizeof *otd
;
4986 ofpbuf_use_const(&properties
, ofpbuf_pull(msg
, length
), length
);
4988 td
->eviction
= ofputil_decode_table_eviction(otd
->config
, version
);
4989 td
->vacancy
= ofputil_decode_table_vacancy(otd
->config
, version
);
4990 td
->eviction_flags
= UINT32_MAX
;
4992 while (properties
.size
> 0) {
4993 struct ofpbuf payload
;
4997 error
= ofpprop_pull(&properties
, &payload
, &type
);
5003 case OFPTMPT14_EVICTION
:
5004 error
= ofpprop_parse_u32(&payload
, &td
->eviction_flags
);
5007 case OFPTMPT14_VACANCY
:
5008 error
= parse_table_desc_vacancy_property(&payload
, td
);
5012 error
= OFPPROP_UNKNOWN(true, "table_desc", type
);
5024 /* Encodes and returns a request to obtain description of tables of a switch.
5025 * The message is encoded for OpenFlow version 'ofp_version'. */
5027 ofputil_encode_table_desc_request(enum ofp_version ofp_version
)
5029 struct ofpbuf
*request
= NULL
;
5031 if (ofp_version
>= OFP14_VERSION
) {
5032 request
= ofpraw_alloc(OFPRAW_OFPST14_TABLE_DESC_REQUEST
,
5035 ovs_fatal(0, "dump-table-desc needs OpenFlow 1.4 or later "
5036 "(\'-O OpenFlow14\')");
5042 /* Function to append Table desc information in a reply list. */
5044 ofputil_append_table_desc_reply(const struct ofputil_table_desc
*td
,
5045 struct ovs_list
*replies
,
5046 enum ofp_version version
)
5048 struct ofpbuf
*reply
= ofpbuf_from_list(list_back(replies
));
5050 struct ofp14_table_desc
*otd
;
5052 start_otd
= reply
->size
;
5053 ofpbuf_put_zeros(reply
, sizeof *otd
);
5054 if (td
->eviction_flags
!= UINT32_MAX
) {
5055 ofpprop_put_u32(reply
, OFPTMPT14_EVICTION
, td
->eviction_flags
);
5057 if (td
->vacancy
== OFPUTIL_TABLE_VACANCY_ON
) {
5058 struct ofp14_table_mod_prop_vacancy
*otv
;
5060 otv
= ofpprop_put_zeros(reply
, OFPTMPT14_VACANCY
, sizeof *otv
);
5061 otv
->vacancy_down
= td
->table_vacancy
.vacancy_down
;
5062 otv
->vacancy_up
= td
->table_vacancy
.vacancy_up
;
5063 otv
->vacancy
= td
->table_vacancy
.vacancy
;
5066 otd
= ofpbuf_at_assert(reply
, start_otd
, sizeof *otd
);
5067 otd
->length
= htons(reply
->size
- start_otd
);
5068 otd
->table_id
= td
->table_id
;
5069 otd
->config
= ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT
,
5070 td
->eviction
, td
->vacancy
,
5072 ofpmp_postappend(replies
, start_otd
);
5075 /* This function parses Vacancy property, and decodes the
5076 * ofp14_table_mod_prop_vacancy in ofputil_table_mod.
5077 * Returns OFPERR_OFPBPC_BAD_VALUE error code when vacancy_down is
5078 * greater than vacancy_up and also when current vacancy has non-zero
5079 * value. Returns 0 on success. */
5081 parse_table_mod_vacancy_property(struct ofpbuf
*property
,
5082 struct ofputil_table_mod
*tm
)
5084 struct ofp14_table_mod_prop_vacancy
*otv
= property
->data
;
5086 if (property
->size
!= sizeof *otv
) {
5087 return OFPERR_OFPBPC_BAD_LEN
;
5089 tm
->table_vacancy
.vacancy_down
= otv
->vacancy_down
;
5090 tm
->table_vacancy
.vacancy_up
= otv
->vacancy_up
;
5091 if (tm
->table_vacancy
.vacancy_down
> tm
->table_vacancy
.vacancy_up
) {
5092 OFPPROP_LOG(&bad_ofmsg_rl
, false,
5093 "Value of vacancy_down is greater than vacancy_up");
5094 return OFPERR_OFPBPC_BAD_VALUE
;
5096 if (tm
->table_vacancy
.vacancy_down
> 100 ||
5097 tm
->table_vacancy
.vacancy_up
> 100) {
5098 OFPPROP_LOG(&bad_ofmsg_rl
, false, "Vacancy threshold percentage "
5099 "should not be greater than 100");
5100 return OFPERR_OFPBPC_BAD_VALUE
;
5102 tm
->table_vacancy
.vacancy
= otv
->vacancy
;
5103 if (tm
->table_vacancy
.vacancy
) {
5104 OFPPROP_LOG(&bad_ofmsg_rl
, false,
5105 "Vacancy value should be zero for table-mod messages");
5106 return OFPERR_OFPBPC_BAD_VALUE
;
5111 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5112 * table configuration (a table mod, table stats, or table features message),
5113 * returns the table vacancy configuration that it specifies.
5115 * Only OpenFlow 1.4 and later specify table vacancy configuration this way,
5116 * so for other 'version' this function always returns
5117 * OFPUTIL_TABLE_VACANCY_DEFAULT. */
5118 static enum ofputil_table_vacancy
5119 ofputil_decode_table_vacancy(ovs_be32 config
, enum ofp_version version
)
5121 return (version
< OFP14_VERSION
? OFPUTIL_TABLE_VACANCY_DEFAULT
5122 : config
& htonl(OFPTC14_VACANCY_EVENTS
) ? OFPUTIL_TABLE_VACANCY_ON
5123 : OFPUTIL_TABLE_VACANCY_OFF
);
5126 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5127 * table configuration (a table mod, table stats, or table features message),
5128 * returns the table eviction configuration that it specifies.
5130 * Only OpenFlow 1.4 and later specify table eviction configuration this way,
5131 * so for other 'version' values this function always returns
5132 * OFPUTIL_TABLE_EVICTION_DEFAULT. */
5133 static enum ofputil_table_eviction
5134 ofputil_decode_table_eviction(ovs_be32 config
, enum ofp_version version
)
5136 return (version
< OFP14_VERSION
? OFPUTIL_TABLE_EVICTION_DEFAULT
5137 : config
& htonl(OFPTC14_EVICTION
) ? OFPUTIL_TABLE_EVICTION_ON
5138 : OFPUTIL_TABLE_EVICTION_OFF
);
5141 /* Returns a bitmap of OFPTC* values suitable for 'config' fields in various
5142 * OpenFlow messages of the given 'version', based on the provided 'miss' and
5143 * 'eviction' values. */
5145 ofputil_encode_table_config(enum ofputil_table_miss miss
,
5146 enum ofputil_table_eviction eviction
,
5147 enum ofputil_table_vacancy vacancy
,
5148 enum ofp_version version
)
5150 uint32_t config
= 0;
5151 /* See the section "OFPTC_* Table Configuration" in DESIGN.md for more
5152 * information on the crazy evolution of this field. */
5155 /* OpenFlow 1.0 didn't have such a field, any value ought to do. */
5160 /* OpenFlow 1.1 and 1.2 define only OFPTC11_TABLE_MISS_*. */
5162 case OFPUTIL_TABLE_MISS_DEFAULT
:
5163 /* Really this shouldn't be used for encoding (the caller should
5164 * provide a specific value) but I can't imagine that defaulting to
5165 * the fall-through case here will hurt. */
5166 case OFPUTIL_TABLE_MISS_CONTROLLER
:
5168 return htonl(OFPTC11_TABLE_MISS_CONTROLLER
);
5169 case OFPUTIL_TABLE_MISS_CONTINUE
:
5170 return htonl(OFPTC11_TABLE_MISS_CONTINUE
);
5171 case OFPUTIL_TABLE_MISS_DROP
:
5172 return htonl(OFPTC11_TABLE_MISS_DROP
);
5177 /* OpenFlow 1.3 removed OFPTC11_TABLE_MISS_* and didn't define any new
5178 * flags, so this is correct. */
5183 /* OpenFlow 1.4 introduced OFPTC14_EVICTION and
5184 * OFPTC14_VACANCY_EVENTS. */
5185 if (eviction
== OFPUTIL_TABLE_EVICTION_ON
) {
5186 config
|= OFPTC14_EVICTION
;
5188 if (vacancy
== OFPUTIL_TABLE_VACANCY_ON
) {
5189 config
|= OFPTC14_VACANCY_EVENTS
;
5191 return htonl(config
);
5197 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5198 * table configuration (a table mod, table stats, or table features message),
5199 * returns the table miss configuration that it specifies.
5201 * Only OpenFlow 1.1 and 1.2 specify table miss configurations this way, so for
5202 * other 'version' values this function always returns
5203 * OFPUTIL_TABLE_MISS_DEFAULT. */
5204 static enum ofputil_table_miss
5205 ofputil_decode_table_miss(ovs_be32 config_
, enum ofp_version version
)
5207 uint32_t config
= ntohl(config_
);
5209 if (version
== OFP11_VERSION
|| version
== OFP12_VERSION
) {
5210 switch (config
& OFPTC11_TABLE_MISS_MASK
) {
5211 case OFPTC11_TABLE_MISS_CONTROLLER
:
5212 return OFPUTIL_TABLE_MISS_CONTROLLER
;
5214 case OFPTC11_TABLE_MISS_CONTINUE
:
5215 return OFPUTIL_TABLE_MISS_CONTINUE
;
5217 case OFPTC11_TABLE_MISS_DROP
:
5218 return OFPUTIL_TABLE_MISS_DROP
;
5221 VLOG_WARN_RL(&bad_ofmsg_rl
, "bad table miss config %d", config
);
5222 return OFPUTIL_TABLE_MISS_CONTROLLER
;
5225 return OFPUTIL_TABLE_MISS_DEFAULT
;
5229 /* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in
5230 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
5232 ofputil_decode_table_mod(const struct ofp_header
*oh
,
5233 struct ofputil_table_mod
*pm
)
5238 memset(pm
, 0, sizeof *pm
);
5239 pm
->miss
= OFPUTIL_TABLE_MISS_DEFAULT
;
5240 pm
->eviction
= OFPUTIL_TABLE_EVICTION_DEFAULT
;
5241 pm
->eviction_flags
= UINT32_MAX
;
5242 pm
->vacancy
= OFPUTIL_TABLE_VACANCY_DEFAULT
;
5243 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
5244 raw
= ofpraw_pull_assert(&b
);
5246 if (raw
== OFPRAW_OFPT11_TABLE_MOD
) {
5247 const struct ofp11_table_mod
*otm
= b
.data
;
5249 pm
->table_id
= otm
->table_id
;
5250 pm
->miss
= ofputil_decode_table_miss(otm
->config
, oh
->version
);
5251 } else if (raw
== OFPRAW_OFPT14_TABLE_MOD
) {
5252 const struct ofp14_table_mod
*otm
= ofpbuf_pull(&b
, sizeof *otm
);
5254 pm
->table_id
= otm
->table_id
;
5255 pm
->miss
= ofputil_decode_table_miss(otm
->config
, oh
->version
);
5256 pm
->eviction
= ofputil_decode_table_eviction(otm
->config
, oh
->version
);
5257 pm
->vacancy
= ofputil_decode_table_vacancy(otm
->config
, oh
->version
);
5258 while (b
.size
> 0) {
5259 struct ofpbuf property
;
5263 error
= ofpprop_pull(&b
, &property
, &type
);
5269 case OFPTMPT14_EVICTION
:
5270 error
= ofpprop_parse_u32(&property
, &pm
->eviction
);
5273 case OFPTMPT14_VACANCY
:
5274 error
= parse_table_mod_vacancy_property(&property
, pm
);
5278 error
= OFPERR_OFPBRC_BAD_TYPE
;
5287 return OFPERR_OFPBRC_BAD_TYPE
;
5293 /* Converts the abstract form of a "table mod" message in '*tm' into an
5294 * OpenFlow message suitable for 'protocol', and returns that encoded form in a
5295 * buffer owned by the caller. */
5297 ofputil_encode_table_mod(const struct ofputil_table_mod
*tm
,
5298 enum ofputil_protocol protocol
)
5300 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
5303 switch (ofp_version
) {
5304 case OFP10_VERSION
: {
5305 ovs_fatal(0, "table mod needs OpenFlow 1.1 or later "
5306 "(\'-O OpenFlow11\')");
5311 case OFP13_VERSION
: {
5312 struct ofp11_table_mod
*otm
;
5314 b
= ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD
, ofp_version
, 0);
5315 otm
= ofpbuf_put_zeros(b
, sizeof *otm
);
5316 otm
->table_id
= tm
->table_id
;
5317 otm
->config
= ofputil_encode_table_config(tm
->miss
, tm
->eviction
,
5318 tm
->vacancy
, ofp_version
);
5322 case OFP15_VERSION
: {
5323 struct ofp14_table_mod
*otm
;
5325 b
= ofpraw_alloc(OFPRAW_OFPT14_TABLE_MOD
, ofp_version
, 0);
5326 otm
= ofpbuf_put_zeros(b
, sizeof *otm
);
5327 otm
->table_id
= tm
->table_id
;
5328 otm
->config
= ofputil_encode_table_config(tm
->miss
, tm
->eviction
,
5329 tm
->vacancy
, ofp_version
);
5331 if (tm
->eviction_flags
!= UINT32_MAX
) {
5332 ofpprop_put_u32(b
, OFPTMPT14_EVICTION
, tm
->eviction_flags
);
5334 if (tm
->vacancy
== OFPUTIL_TABLE_VACANCY_ON
) {
5335 struct ofp14_table_mod_prop_vacancy
*otv
;
5337 otv
= ofpprop_put_zeros(b
, OFPTMPT14_VACANCY
, sizeof *otv
);
5338 otv
->vacancy_down
= tm
->table_vacancy
.vacancy_down
;
5339 otv
->vacancy_up
= tm
->table_vacancy
.vacancy_up
;
5350 /* ofputil_role_request */
5352 /* Decodes the OpenFlow "role request" or "role reply" message in '*oh' into
5353 * an abstract form in '*rr'. Returns 0 if successful, otherwise an
5354 * OFPERR_* value. */
5356 ofputil_decode_role_message(const struct ofp_header
*oh
,
5357 struct ofputil_role_request
*rr
)
5362 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
5363 raw
= ofpraw_pull_assert(&b
);
5365 if (raw
== OFPRAW_OFPT12_ROLE_REQUEST
||
5366 raw
== OFPRAW_OFPT12_ROLE_REPLY
) {
5367 const struct ofp12_role_request
*orr
= b
.msg
;
5369 if (orr
->role
!= htonl(OFPCR12_ROLE_NOCHANGE
) &&
5370 orr
->role
!= htonl(OFPCR12_ROLE_EQUAL
) &&
5371 orr
->role
!= htonl(OFPCR12_ROLE_MASTER
) &&
5372 orr
->role
!= htonl(OFPCR12_ROLE_SLAVE
)) {
5373 return OFPERR_OFPRRFC_BAD_ROLE
;
5376 rr
->role
= ntohl(orr
->role
);
5377 if (raw
== OFPRAW_OFPT12_ROLE_REQUEST
5378 ? orr
->role
== htonl(OFPCR12_ROLE_NOCHANGE
)
5379 : orr
->generation_id
== OVS_BE64_MAX
) {
5380 rr
->have_generation_id
= false;
5381 rr
->generation_id
= 0;
5383 rr
->have_generation_id
= true;
5384 rr
->generation_id
= ntohll(orr
->generation_id
);
5386 } else if (raw
== OFPRAW_NXT_ROLE_REQUEST
||
5387 raw
== OFPRAW_NXT_ROLE_REPLY
) {
5388 const struct nx_role_request
*nrr
= b
.msg
;
5390 BUILD_ASSERT(NX_ROLE_OTHER
+ 1 == OFPCR12_ROLE_EQUAL
);
5391 BUILD_ASSERT(NX_ROLE_MASTER
+ 1 == OFPCR12_ROLE_MASTER
);
5392 BUILD_ASSERT(NX_ROLE_SLAVE
+ 1 == OFPCR12_ROLE_SLAVE
);
5394 if (nrr
->role
!= htonl(NX_ROLE_OTHER
) &&
5395 nrr
->role
!= htonl(NX_ROLE_MASTER
) &&
5396 nrr
->role
!= htonl(NX_ROLE_SLAVE
)) {
5397 return OFPERR_OFPRRFC_BAD_ROLE
;
5400 rr
->role
= ntohl(nrr
->role
) + 1;
5401 rr
->have_generation_id
= false;
5402 rr
->generation_id
= 0;
5410 /* Returns an encoded form of a role reply suitable for the "request" in a
5411 * buffer owned by the caller. */
5413 ofputil_encode_role_reply(const struct ofp_header
*request
,
5414 const struct ofputil_role_request
*rr
)
5419 raw
= ofpraw_decode_assert(request
);
5420 if (raw
== OFPRAW_OFPT12_ROLE_REQUEST
) {
5421 struct ofp12_role_request
*orr
;
5423 buf
= ofpraw_alloc_reply(OFPRAW_OFPT12_ROLE_REPLY
, request
, 0);
5424 orr
= ofpbuf_put_zeros(buf
, sizeof *orr
);
5426 orr
->role
= htonl(rr
->role
);
5427 orr
->generation_id
= htonll(rr
->have_generation_id
5430 } else if (raw
== OFPRAW_NXT_ROLE_REQUEST
) {
5431 struct nx_role_request
*nrr
;
5433 BUILD_ASSERT(NX_ROLE_OTHER
== OFPCR12_ROLE_EQUAL
- 1);
5434 BUILD_ASSERT(NX_ROLE_MASTER
== OFPCR12_ROLE_MASTER
- 1);
5435 BUILD_ASSERT(NX_ROLE_SLAVE
== OFPCR12_ROLE_SLAVE
- 1);
5437 buf
= ofpraw_alloc_reply(OFPRAW_NXT_ROLE_REPLY
, request
, 0);
5438 nrr
= ofpbuf_put_zeros(buf
, sizeof *nrr
);
5439 nrr
->role
= htonl(rr
->role
- 1);
5447 /* Encodes "role status" message 'status' for sending in the given
5448 * 'protocol'. Returns the role status message, if 'protocol' supports them,
5449 * otherwise a null pointer. */
5451 ofputil_encode_role_status(const struct ofputil_role_status
*status
,
5452 enum ofputil_protocol protocol
)
5454 enum ofp_version version
;
5456 version
= ofputil_protocol_to_ofp_version(protocol
);
5457 if (version
>= OFP14_VERSION
) {
5458 struct ofp14_role_status
*rstatus
;
5461 buf
= ofpraw_alloc_xid(OFPRAW_OFPT14_ROLE_STATUS
, version
, htonl(0),
5463 rstatus
= ofpbuf_put_zeros(buf
, sizeof *rstatus
);
5464 rstatus
->role
= htonl(status
->role
);
5465 rstatus
->reason
= status
->reason
;
5466 rstatus
->generation_id
= htonll(status
->generation_id
);
5475 ofputil_decode_role_status(const struct ofp_header
*oh
,
5476 struct ofputil_role_status
*rs
)
5480 const struct ofp14_role_status
*r
;
5482 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
5483 raw
= ofpraw_pull_assert(&b
);
5484 ovs_assert(raw
== OFPRAW_OFPT14_ROLE_STATUS
);
5487 if (r
->role
!= htonl(OFPCR12_ROLE_NOCHANGE
) &&
5488 r
->role
!= htonl(OFPCR12_ROLE_EQUAL
) &&
5489 r
->role
!= htonl(OFPCR12_ROLE_MASTER
) &&
5490 r
->role
!= htonl(OFPCR12_ROLE_SLAVE
)) {
5491 return OFPERR_OFPRRFC_BAD_ROLE
;
5494 rs
->role
= ntohl(r
->role
);
5495 rs
->generation_id
= ntohll(r
->generation_id
);
5496 rs
->reason
= r
->reason
;
5501 /* Encodes 'rf' according to 'protocol', and returns the encoded message.
5502 * 'protocol' must be for OpenFlow 1.4 or later. */
5504 ofputil_encode_requestforward(const struct ofputil_requestforward
*rf
,
5505 enum ofputil_protocol protocol
)
5507 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
5508 struct ofpbuf
*inner
;
5510 switch (rf
->reason
) {
5511 case OFPRFR_GROUP_MOD
:
5512 inner
= ofputil_encode_group_mod(ofp_version
, rf
->group_mod
);
5515 case OFPRFR_METER_MOD
:
5516 inner
= ofputil_encode_meter_mod(ofp_version
, rf
->meter_mod
);
5519 case OFPRFR_N_REASONS
:
5524 struct ofp_header
*inner_oh
= inner
->data
;
5525 inner_oh
->xid
= rf
->xid
;
5526 inner_oh
->length
= htons(inner
->size
);
5528 struct ofpbuf
*outer
= ofpraw_alloc_xid(OFPRAW_OFPT14_REQUESTFORWARD
,
5529 ofp_version
, htonl(0),
5531 ofpbuf_put(outer
, inner
->data
, inner
->size
);
5532 ofpbuf_delete(inner
);
5537 /* Decodes OFPT_REQUESTFORWARD message 'outer'. On success, puts the decoded
5538 * form into '*rf' and returns 0, and the caller is later responsible for
5539 * freeing the content of 'rf', with ofputil_destroy_requestforward(rf). On
5540 * failure, returns an ofperr and '*rf' is indeterminate. */
5542 ofputil_decode_requestforward(const struct ofp_header
*outer
,
5543 struct ofputil_requestforward
*rf
)
5548 ofpbuf_use_const(&b
, outer
, ntohs(outer
->length
));
5550 /* Skip past outer message. */
5551 enum ofpraw outer_raw
= ofpraw_pull_assert(&b
);
5552 ovs_assert(outer_raw
== OFPRAW_OFPT14_REQUESTFORWARD
);
5554 /* Validate inner message. */
5555 if (b
.size
< sizeof(struct ofp_header
)) {
5556 return OFPERR_OFPBFC_MSG_BAD_LEN
;
5558 const struct ofp_header
*inner
= b
.data
;
5559 unsigned int inner_len
= ntohs(inner
->length
);
5560 if (inner_len
< sizeof(struct ofp_header
) || inner_len
> b
.size
) {
5561 return OFPERR_OFPBFC_MSG_BAD_LEN
;
5563 if (inner
->version
!= outer
->version
) {
5564 return OFPERR_OFPBRC_BAD_VERSION
;
5567 /* Parse inner message. */
5569 error
= ofptype_decode(&type
, inner
);
5574 rf
->xid
= inner
->xid
;
5575 if (type
== OFPTYPE_GROUP_MOD
) {
5576 rf
->reason
= OFPRFR_GROUP_MOD
;
5577 rf
->group_mod
= xmalloc(sizeof *rf
->group_mod
);
5578 error
= ofputil_decode_group_mod(inner
, rf
->group_mod
);
5580 free(rf
->group_mod
);
5583 } else if (type
== OFPTYPE_METER_MOD
) {
5584 rf
->reason
= OFPRFR_METER_MOD
;
5585 rf
->meter_mod
= xmalloc(sizeof *rf
->meter_mod
);
5586 ofpbuf_init(&rf
->bands
, 64);
5587 error
= ofputil_decode_meter_mod(inner
, rf
->meter_mod
, &rf
->bands
);
5589 free(rf
->meter_mod
);
5590 ofpbuf_uninit(&rf
->bands
);
5594 return OFPERR_OFPBFC_MSG_UNSUP
;
5600 /* Frees the content of 'rf', which should have been initialized through a
5601 * successful call to ofputil_decode_requestforward(). */
5603 ofputil_destroy_requestforward(struct ofputil_requestforward
*rf
)
5609 switch (rf
->reason
) {
5610 case OFPRFR_GROUP_MOD
:
5611 ofputil_uninit_group_mod(rf
->group_mod
);
5612 free(rf
->group_mod
);
5615 case OFPRFR_METER_MOD
:
5616 ofpbuf_uninit(&rf
->bands
);
5617 free(rf
->meter_mod
);
5620 case OFPRFR_N_REASONS
:
5627 /* OpenFlow 1.0 and 1.1 don't distinguish between a field that cannot be
5628 * matched and a field that must be wildcarded. This function returns a bitmap
5629 * that contains both kinds of fields. */
5630 static struct mf_bitmap
5631 wild_or_nonmatchable_fields(const struct ofputil_table_features
*features
)
5633 struct mf_bitmap wc
= features
->match
;
5634 bitmap_not(wc
.bm
, MFF_N_IDS
);
5635 bitmap_or(wc
.bm
, features
->wildcard
.bm
, MFF_N_IDS
);
5639 struct ofp10_wc_map
{
5640 enum ofp10_flow_wildcards wc10
;
5641 enum mf_field_id mf
;
5644 static const struct ofp10_wc_map ofp10_wc_map
[] = {
5645 { OFPFW10_IN_PORT
, MFF_IN_PORT
},
5646 { OFPFW10_DL_VLAN
, MFF_VLAN_VID
},
5647 { OFPFW10_DL_SRC
, MFF_ETH_SRC
},
5648 { OFPFW10_DL_DST
, MFF_ETH_DST
},
5649 { OFPFW10_DL_TYPE
, MFF_ETH_TYPE
},
5650 { OFPFW10_NW_PROTO
, MFF_IP_PROTO
},
5651 { OFPFW10_TP_SRC
, MFF_TCP_SRC
},
5652 { OFPFW10_TP_DST
, MFF_TCP_DST
},
5653 { OFPFW10_NW_SRC_MASK
, MFF_IPV4_SRC
},
5654 { OFPFW10_NW_DST_MASK
, MFF_IPV4_DST
},
5655 { OFPFW10_DL_VLAN_PCP
, MFF_VLAN_PCP
},
5656 { OFPFW10_NW_TOS
, MFF_IP_DSCP
},
5660 mf_bitmap_to_of10(const struct mf_bitmap
*fields
)
5662 const struct ofp10_wc_map
*p
;
5665 for (p
= ofp10_wc_map
; p
< &ofp10_wc_map
[ARRAY_SIZE(ofp10_wc_map
)]; p
++) {
5666 if (bitmap_is_set(fields
->bm
, p
->mf
)) {
5673 static struct mf_bitmap
5674 mf_bitmap_from_of10(ovs_be32 wc10_
)
5676 struct mf_bitmap fields
= MF_BITMAP_INITIALIZER
;
5677 const struct ofp10_wc_map
*p
;
5678 uint32_t wc10
= ntohl(wc10_
);
5680 for (p
= ofp10_wc_map
; p
< &ofp10_wc_map
[ARRAY_SIZE(ofp10_wc_map
)]; p
++) {
5681 if (wc10
& p
->wc10
) {
5682 bitmap_set1(fields
.bm
, p
->mf
);
5689 ofputil_put_ofp10_table_stats(const struct ofputil_table_stats
*stats
,
5690 const struct ofputil_table_features
*features
,
5693 struct mf_bitmap wc
= wild_or_nonmatchable_fields(features
);
5694 struct ofp10_table_stats
*out
;
5696 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
5697 out
->table_id
= features
->table_id
;
5698 ovs_strlcpy(out
->name
, features
->name
, sizeof out
->name
);
5699 out
->wildcards
= mf_bitmap_to_of10(&wc
);
5700 out
->max_entries
= htonl(features
->max_entries
);
5701 out
->active_count
= htonl(stats
->active_count
);
5702 put_32aligned_be64(&out
->lookup_count
, htonll(stats
->lookup_count
));
5703 put_32aligned_be64(&out
->matched_count
, htonll(stats
->matched_count
));
5706 struct ofp11_wc_map
{
5707 enum ofp11_flow_match_fields wc11
;
5708 enum mf_field_id mf
;
5711 static const struct ofp11_wc_map ofp11_wc_map
[] = {
5712 { OFPFMF11_IN_PORT
, MFF_IN_PORT
},
5713 { OFPFMF11_DL_VLAN
, MFF_VLAN_VID
},
5714 { OFPFMF11_DL_VLAN_PCP
, MFF_VLAN_PCP
},
5715 { OFPFMF11_DL_TYPE
, MFF_ETH_TYPE
},
5716 { OFPFMF11_NW_TOS
, MFF_IP_DSCP
},
5717 { OFPFMF11_NW_PROTO
, MFF_IP_PROTO
},
5718 { OFPFMF11_TP_SRC
, MFF_TCP_SRC
},
5719 { OFPFMF11_TP_DST
, MFF_TCP_DST
},
5720 { OFPFMF11_MPLS_LABEL
, MFF_MPLS_LABEL
},
5721 { OFPFMF11_MPLS_TC
, MFF_MPLS_TC
},
5722 /* I don't know what OFPFMF11_TYPE means. */
5723 { OFPFMF11_DL_SRC
, MFF_ETH_SRC
},
5724 { OFPFMF11_DL_DST
, MFF_ETH_DST
},
5725 { OFPFMF11_NW_SRC
, MFF_IPV4_SRC
},
5726 { OFPFMF11_NW_DST
, MFF_IPV4_DST
},
5727 { OFPFMF11_METADATA
, MFF_METADATA
},
5731 mf_bitmap_to_of11(const struct mf_bitmap
*fields
)
5733 const struct ofp11_wc_map
*p
;
5736 for (p
= ofp11_wc_map
; p
< &ofp11_wc_map
[ARRAY_SIZE(ofp11_wc_map
)]; p
++) {
5737 if (bitmap_is_set(fields
->bm
, p
->mf
)) {
5744 static struct mf_bitmap
5745 mf_bitmap_from_of11(ovs_be32 wc11_
)
5747 struct mf_bitmap fields
= MF_BITMAP_INITIALIZER
;
5748 const struct ofp11_wc_map
*p
;
5749 uint32_t wc11
= ntohl(wc11_
);
5751 for (p
= ofp11_wc_map
; p
< &ofp11_wc_map
[ARRAY_SIZE(ofp11_wc_map
)]; p
++) {
5752 if (wc11
& p
->wc11
) {
5753 bitmap_set1(fields
.bm
, p
->mf
);
5760 ofputil_put_ofp11_table_stats(const struct ofputil_table_stats
*stats
,
5761 const struct ofputil_table_features
*features
,
5764 struct mf_bitmap wc
= wild_or_nonmatchable_fields(features
);
5765 struct ofp11_table_stats
*out
;
5767 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
5768 out
->table_id
= features
->table_id
;
5769 ovs_strlcpy(out
->name
, features
->name
, sizeof out
->name
);
5770 out
->wildcards
= mf_bitmap_to_of11(&wc
);
5771 out
->match
= mf_bitmap_to_of11(&features
->match
);
5772 out
->instructions
= ovsinst_bitmap_to_openflow(
5773 features
->nonmiss
.instructions
, OFP11_VERSION
);
5774 out
->write_actions
= ofpact_bitmap_to_openflow(
5775 features
->nonmiss
.write
.ofpacts
, OFP11_VERSION
);
5776 out
->apply_actions
= ofpact_bitmap_to_openflow(
5777 features
->nonmiss
.apply
.ofpacts
, OFP11_VERSION
);
5778 out
->config
= htonl(features
->miss_config
);
5779 out
->max_entries
= htonl(features
->max_entries
);
5780 out
->active_count
= htonl(stats
->active_count
);
5781 out
->lookup_count
= htonll(stats
->lookup_count
);
5782 out
->matched_count
= htonll(stats
->matched_count
);
5786 ofputil_put_ofp12_table_stats(const struct ofputil_table_stats
*stats
,
5787 const struct ofputil_table_features
*features
,
5790 struct ofp12_table_stats
*out
;
5792 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
5793 out
->table_id
= features
->table_id
;
5794 ovs_strlcpy(out
->name
, features
->name
, sizeof out
->name
);
5795 out
->match
= oxm_bitmap_from_mf_bitmap(&features
->match
, OFP12_VERSION
);
5796 out
->wildcards
= oxm_bitmap_from_mf_bitmap(&features
->wildcard
,
5798 out
->write_actions
= ofpact_bitmap_to_openflow(
5799 features
->nonmiss
.write
.ofpacts
, OFP12_VERSION
);
5800 out
->apply_actions
= ofpact_bitmap_to_openflow(
5801 features
->nonmiss
.apply
.ofpacts
, OFP12_VERSION
);
5802 out
->write_setfields
= oxm_bitmap_from_mf_bitmap(
5803 &features
->nonmiss
.write
.set_fields
, OFP12_VERSION
);
5804 out
->apply_setfields
= oxm_bitmap_from_mf_bitmap(
5805 &features
->nonmiss
.apply
.set_fields
, OFP12_VERSION
);
5806 out
->metadata_match
= features
->metadata_match
;
5807 out
->metadata_write
= features
->metadata_write
;
5808 out
->instructions
= ovsinst_bitmap_to_openflow(
5809 features
->nonmiss
.instructions
, OFP12_VERSION
);
5810 out
->config
= ofputil_encode_table_config(features
->miss_config
,
5811 OFPUTIL_TABLE_EVICTION_DEFAULT
,
5812 OFPUTIL_TABLE_VACANCY_DEFAULT
,
5814 out
->max_entries
= htonl(features
->max_entries
);
5815 out
->active_count
= htonl(stats
->active_count
);
5816 out
->lookup_count
= htonll(stats
->lookup_count
);
5817 out
->matched_count
= htonll(stats
->matched_count
);
5821 ofputil_put_ofp13_table_stats(const struct ofputil_table_stats
*stats
,
5824 struct ofp13_table_stats
*out
;
5826 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
5827 out
->table_id
= stats
->table_id
;
5828 out
->active_count
= htonl(stats
->active_count
);
5829 out
->lookup_count
= htonll(stats
->lookup_count
);
5830 out
->matched_count
= htonll(stats
->matched_count
);
5834 ofputil_encode_table_stats_reply(const struct ofp_header
*request
)
5836 return ofpraw_alloc_stats_reply(request
, 0);
5840 ofputil_append_table_stats_reply(struct ofpbuf
*reply
,
5841 const struct ofputil_table_stats
*stats
,
5842 const struct ofputil_table_features
*features
)
5844 struct ofp_header
*oh
= reply
->header
;
5846 ovs_assert(stats
->table_id
== features
->table_id
);
5848 switch ((enum ofp_version
) oh
->version
) {
5850 ofputil_put_ofp10_table_stats(stats
, features
, reply
);
5854 ofputil_put_ofp11_table_stats(stats
, features
, reply
);
5858 ofputil_put_ofp12_table_stats(stats
, features
, reply
);
5864 ofputil_put_ofp13_table_stats(stats
, reply
);
5873 ofputil_decode_ofp10_table_stats(struct ofpbuf
*msg
,
5874 struct ofputil_table_stats
*stats
,
5875 struct ofputil_table_features
*features
)
5877 struct ofp10_table_stats
*ots
;
5879 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
5881 return OFPERR_OFPBRC_BAD_LEN
;
5884 features
->table_id
= ots
->table_id
;
5885 ovs_strlcpy(features
->name
, ots
->name
, sizeof features
->name
);
5886 features
->max_entries
= ntohl(ots
->max_entries
);
5887 features
->match
= features
->wildcard
= mf_bitmap_from_of10(ots
->wildcards
);
5889 stats
->table_id
= ots
->table_id
;
5890 stats
->active_count
= ntohl(ots
->active_count
);
5891 stats
->lookup_count
= ntohll(get_32aligned_be64(&ots
->lookup_count
));
5892 stats
->matched_count
= ntohll(get_32aligned_be64(&ots
->matched_count
));
5898 ofputil_decode_ofp11_table_stats(struct ofpbuf
*msg
,
5899 struct ofputil_table_stats
*stats
,
5900 struct ofputil_table_features
*features
)
5902 struct ofp11_table_stats
*ots
;
5904 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
5906 return OFPERR_OFPBRC_BAD_LEN
;
5909 features
->table_id
= ots
->table_id
;
5910 ovs_strlcpy(features
->name
, ots
->name
, sizeof features
->name
);
5911 features
->max_entries
= ntohl(ots
->max_entries
);
5912 features
->nonmiss
.instructions
= ovsinst_bitmap_from_openflow(
5913 ots
->instructions
, OFP11_VERSION
);
5914 features
->nonmiss
.write
.ofpacts
= ofpact_bitmap_from_openflow(
5915 ots
->write_actions
, OFP11_VERSION
);
5916 features
->nonmiss
.apply
.ofpacts
= ofpact_bitmap_from_openflow(
5917 ots
->write_actions
, OFP11_VERSION
);
5918 features
->miss
= features
->nonmiss
;
5919 features
->miss_config
= ofputil_decode_table_miss(ots
->config
,
5921 features
->match
= mf_bitmap_from_of11(ots
->match
);
5922 features
->wildcard
= mf_bitmap_from_of11(ots
->wildcards
);
5923 bitmap_or(features
->match
.bm
, features
->wildcard
.bm
, MFF_N_IDS
);
5925 stats
->table_id
= ots
->table_id
;
5926 stats
->active_count
= ntohl(ots
->active_count
);
5927 stats
->lookup_count
= ntohll(ots
->lookup_count
);
5928 stats
->matched_count
= ntohll(ots
->matched_count
);
5934 ofputil_decode_ofp12_table_stats(struct ofpbuf
*msg
,
5935 struct ofputil_table_stats
*stats
,
5936 struct ofputil_table_features
*features
)
5938 struct ofp12_table_stats
*ots
;
5940 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
5942 return OFPERR_OFPBRC_BAD_LEN
;
5945 features
->table_id
= ots
->table_id
;
5946 ovs_strlcpy(features
->name
, ots
->name
, sizeof features
->name
);
5947 features
->metadata_match
= ots
->metadata_match
;
5948 features
->metadata_write
= ots
->metadata_write
;
5949 features
->miss_config
= ofputil_decode_table_miss(ots
->config
,
5951 features
->max_entries
= ntohl(ots
->max_entries
);
5953 features
->nonmiss
.instructions
= ovsinst_bitmap_from_openflow(
5954 ots
->instructions
, OFP12_VERSION
);
5955 features
->nonmiss
.write
.ofpacts
= ofpact_bitmap_from_openflow(
5956 ots
->write_actions
, OFP12_VERSION
);
5957 features
->nonmiss
.apply
.ofpacts
= ofpact_bitmap_from_openflow(
5958 ots
->apply_actions
, OFP12_VERSION
);
5959 features
->nonmiss
.write
.set_fields
= oxm_bitmap_to_mf_bitmap(
5960 ots
->write_setfields
, OFP12_VERSION
);
5961 features
->nonmiss
.apply
.set_fields
= oxm_bitmap_to_mf_bitmap(
5962 ots
->apply_setfields
, OFP12_VERSION
);
5963 features
->miss
= features
->nonmiss
;
5965 features
->match
= oxm_bitmap_to_mf_bitmap(ots
->match
, OFP12_VERSION
);
5966 features
->wildcard
= oxm_bitmap_to_mf_bitmap(ots
->wildcards
,
5968 bitmap_or(features
->match
.bm
, features
->wildcard
.bm
, MFF_N_IDS
);
5970 stats
->table_id
= ots
->table_id
;
5971 stats
->active_count
= ntohl(ots
->active_count
);
5972 stats
->lookup_count
= ntohll(ots
->lookup_count
);
5973 stats
->matched_count
= ntohll(ots
->matched_count
);
5979 ofputil_decode_ofp13_table_stats(struct ofpbuf
*msg
,
5980 struct ofputil_table_stats
*stats
,
5981 struct ofputil_table_features
*features
)
5983 struct ofp13_table_stats
*ots
;
5985 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
5987 return OFPERR_OFPBRC_BAD_LEN
;
5990 features
->table_id
= ots
->table_id
;
5992 stats
->table_id
= ots
->table_id
;
5993 stats
->active_count
= ntohl(ots
->active_count
);
5994 stats
->lookup_count
= ntohll(ots
->lookup_count
);
5995 stats
->matched_count
= ntohll(ots
->matched_count
);
6001 ofputil_decode_table_stats_reply(struct ofpbuf
*msg
,
6002 struct ofputil_table_stats
*stats
,
6003 struct ofputil_table_features
*features
)
6005 const struct ofp_header
*oh
;
6008 ofpraw_pull_assert(msg
);
6016 memset(stats
, 0, sizeof *stats
);
6017 memset(features
, 0, sizeof *features
);
6018 features
->supports_eviction
= -1;
6019 features
->supports_vacancy_events
= -1;
6021 switch ((enum ofp_version
) oh
->version
) {
6023 return ofputil_decode_ofp10_table_stats(msg
, stats
, features
);
6026 return ofputil_decode_ofp11_table_stats(msg
, stats
, features
);
6029 return ofputil_decode_ofp12_table_stats(msg
, stats
, features
);
6034 return ofputil_decode_ofp13_table_stats(msg
, stats
, features
);
6041 /* ofputil_flow_monitor_request */
6043 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
6044 * ofputil_flow_monitor_request in 'rq'.
6046 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
6047 * message. Calling this function multiple times for a single 'msg' iterates
6048 * through the requests. The caller must initially leave 'msg''s layer
6049 * pointers null and not modify them between calls.
6051 * Returns 0 if successful, EOF if no requests were left in this 'msg',
6052 * otherwise an OFPERR_* value. */
6054 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request
*rq
,
6057 struct nx_flow_monitor_request
*nfmr
;
6061 ofpraw_pull_assert(msg
);
6068 nfmr
= ofpbuf_try_pull(msg
, sizeof *nfmr
);
6070 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW_MONITOR request has %"PRIu32
" "
6071 "leftover bytes at end", msg
->size
);
6072 return OFPERR_OFPBRC_BAD_LEN
;
6075 flags
= ntohs(nfmr
->flags
);
6076 if (!(flags
& (NXFMF_ADD
| NXFMF_DELETE
| NXFMF_MODIFY
))
6077 || flags
& ~(NXFMF_INITIAL
| NXFMF_ADD
| NXFMF_DELETE
6078 | NXFMF_MODIFY
| NXFMF_ACTIONS
| NXFMF_OWN
)) {
6079 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW_MONITOR has bad flags %#"PRIx16
,
6081 return OFPERR_OFPMOFC_BAD_FLAGS
;
6084 if (!is_all_zeros(nfmr
->zeros
, sizeof nfmr
->zeros
)) {
6085 return OFPERR_NXBRC_MUST_BE_ZERO
;
6088 rq
->id
= ntohl(nfmr
->id
);
6090 rq
->out_port
= u16_to_ofp(ntohs(nfmr
->out_port
));
6091 rq
->table_id
= nfmr
->table_id
;
6093 return nx_pull_match(msg
, ntohs(nfmr
->match_len
), &rq
->match
, NULL
, NULL
);
6097 ofputil_append_flow_monitor_request(
6098 const struct ofputil_flow_monitor_request
*rq
, struct ofpbuf
*msg
)
6100 struct nx_flow_monitor_request
*nfmr
;
6105 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST
, OFP10_VERSION
, msg
);
6108 start_ofs
= msg
->size
;
6109 ofpbuf_put_zeros(msg
, sizeof *nfmr
);
6110 match_len
= nx_put_match(msg
, &rq
->match
, htonll(0), htonll(0));
6112 nfmr
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *nfmr
);
6113 nfmr
->id
= htonl(rq
->id
);
6114 nfmr
->flags
= htons(rq
->flags
);
6115 nfmr
->out_port
= htons(ofp_to_u16(rq
->out_port
));
6116 nfmr
->match_len
= htons(match_len
);
6117 nfmr
->table_id
= rq
->table_id
;
6120 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
6121 * into an abstract ofputil_flow_update in 'update'. The caller must have
6122 * initialized update->match to point to space allocated for a match.
6124 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
6125 * actions (except for NXFME_ABBREV, which never includes actions). The caller
6126 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
6127 * will point into the 'ofpacts' buffer.
6129 * Multiple flow updates can be packed into a single OpenFlow message. Calling
6130 * this function multiple times for a single 'msg' iterates through the
6131 * updates. The caller must initially leave 'msg''s layer pointers null and
6132 * not modify them between calls.
6134 * Returns 0 if successful, EOF if no updates were left in this 'msg',
6135 * otherwise an OFPERR_* value. */
6137 ofputil_decode_flow_update(struct ofputil_flow_update
*update
,
6138 struct ofpbuf
*msg
, struct ofpbuf
*ofpacts
)
6140 struct nx_flow_update_header
*nfuh
;
6141 unsigned int length
;
6142 struct ofp_header
*oh
;
6145 ofpraw_pull_assert(msg
);
6148 ofpbuf_clear(ofpacts
);
6153 if (msg
->size
< sizeof(struct nx_flow_update_header
)) {
6160 update
->event
= ntohs(nfuh
->event
);
6161 length
= ntohs(nfuh
->length
);
6162 if (length
> msg
->size
|| length
% 8) {
6166 if (update
->event
== NXFME_ABBREV
) {
6167 struct nx_flow_update_abbrev
*nfua
;
6169 if (length
!= sizeof *nfua
) {
6173 nfua
= ofpbuf_pull(msg
, sizeof *nfua
);
6174 update
->xid
= nfua
->xid
;
6176 } else if (update
->event
== NXFME_ADDED
6177 || update
->event
== NXFME_DELETED
6178 || update
->event
== NXFME_MODIFIED
) {
6179 struct nx_flow_update_full
*nfuf
;
6180 unsigned int actions_len
;
6181 unsigned int match_len
;
6184 if (length
< sizeof *nfuf
) {
6188 nfuf
= ofpbuf_pull(msg
, sizeof *nfuf
);
6189 match_len
= ntohs(nfuf
->match_len
);
6190 if (sizeof *nfuf
+ match_len
> length
) {
6194 update
->reason
= ntohs(nfuf
->reason
);
6195 update
->idle_timeout
= ntohs(nfuf
->idle_timeout
);
6196 update
->hard_timeout
= ntohs(nfuf
->hard_timeout
);
6197 update
->table_id
= nfuf
->table_id
;
6198 update
->cookie
= nfuf
->cookie
;
6199 update
->priority
= ntohs(nfuf
->priority
);
6201 error
= nx_pull_match(msg
, match_len
, update
->match
, NULL
, NULL
);
6206 actions_len
= length
- sizeof *nfuf
- ROUND_UP(match_len
, 8);
6207 error
= ofpacts_pull_openflow_actions(msg
, actions_len
, oh
->version
,
6213 update
->ofpacts
= ofpacts
->data
;
6214 update
->ofpacts_len
= ofpacts
->size
;
6217 VLOG_WARN_RL(&bad_ofmsg_rl
,
6218 "NXST_FLOW_MONITOR reply has bad event %"PRIu16
,
6219 ntohs(nfuh
->event
));
6220 return OFPERR_NXBRC_FM_BAD_EVENT
;
6224 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW_MONITOR reply has %"PRIu32
" "
6225 "leftover bytes at end", msg
->size
);
6226 return OFPERR_OFPBRC_BAD_LEN
;
6230 ofputil_decode_flow_monitor_cancel(const struct ofp_header
*oh
)
6232 const struct nx_flow_monitor_cancel
*cancel
= ofpmsg_body(oh
);
6234 return ntohl(cancel
->id
);
6238 ofputil_encode_flow_monitor_cancel(uint32_t id
)
6240 struct nx_flow_monitor_cancel
*nfmc
;
6243 msg
= ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL
, OFP10_VERSION
, 0);
6244 nfmc
= ofpbuf_put_uninit(msg
, sizeof *nfmc
);
6245 nfmc
->id
= htonl(id
);
6250 ofputil_start_flow_update(struct ovs_list
*replies
)
6254 msg
= ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY
, OFP10_VERSION
,
6258 list_push_back(replies
, &msg
->list_node
);
6262 ofputil_append_flow_update(const struct ofputil_flow_update
*update
,
6263 struct ovs_list
*replies
)
6265 enum ofp_version version
= ofpmp_version(replies
);
6266 struct nx_flow_update_header
*nfuh
;
6270 msg
= ofpbuf_from_list(list_back(replies
));
6271 start_ofs
= msg
->size
;
6273 if (update
->event
== NXFME_ABBREV
) {
6274 struct nx_flow_update_abbrev
*nfua
;
6276 nfua
= ofpbuf_put_zeros(msg
, sizeof *nfua
);
6277 nfua
->xid
= update
->xid
;
6279 struct nx_flow_update_full
*nfuf
;
6282 ofpbuf_put_zeros(msg
, sizeof *nfuf
);
6283 match_len
= nx_put_match(msg
, update
->match
, htonll(0), htonll(0));
6284 ofpacts_put_openflow_actions(update
->ofpacts
, update
->ofpacts_len
, msg
,
6286 nfuf
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *nfuf
);
6287 nfuf
->reason
= htons(update
->reason
);
6288 nfuf
->priority
= htons(update
->priority
);
6289 nfuf
->idle_timeout
= htons(update
->idle_timeout
);
6290 nfuf
->hard_timeout
= htons(update
->hard_timeout
);
6291 nfuf
->match_len
= htons(match_len
);
6292 nfuf
->table_id
= update
->table_id
;
6293 nfuf
->cookie
= update
->cookie
;
6296 nfuh
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *nfuh
);
6297 nfuh
->length
= htons(msg
->size
- start_ofs
);
6298 nfuh
->event
= htons(update
->event
);
6300 ofpmp_postappend(replies
, start_ofs
);
6304 ofputil_encode_packet_out(const struct ofputil_packet_out
*po
,
6305 enum ofputil_protocol protocol
)
6307 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
6311 size
= po
->ofpacts_len
;
6312 if (po
->buffer_id
== UINT32_MAX
) {
6313 size
+= po
->packet_len
;
6316 switch (ofp_version
) {
6317 case OFP10_VERSION
: {
6318 struct ofp10_packet_out
*opo
;
6321 msg
= ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT
, OFP10_VERSION
, size
);
6322 ofpbuf_put_zeros(msg
, sizeof *opo
);
6323 actions_ofs
= msg
->size
;
6324 ofpacts_put_openflow_actions(po
->ofpacts
, po
->ofpacts_len
, msg
,
6328 opo
->buffer_id
= htonl(po
->buffer_id
);
6329 opo
->in_port
= htons(ofp_to_u16(po
->in_port
));
6330 opo
->actions_len
= htons(msg
->size
- actions_ofs
);
6338 case OFP15_VERSION
: {
6339 struct ofp11_packet_out
*opo
;
6342 msg
= ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT
, ofp_version
, size
);
6343 ofpbuf_put_zeros(msg
, sizeof *opo
);
6344 len
= ofpacts_put_openflow_actions(po
->ofpacts
, po
->ofpacts_len
, msg
,
6347 opo
->buffer_id
= htonl(po
->buffer_id
);
6348 opo
->in_port
= ofputil_port_to_ofp11(po
->in_port
);
6349 opo
->actions_len
= htons(len
);
6357 if (po
->buffer_id
== UINT32_MAX
) {
6358 ofpbuf_put(msg
, po
->packet
, po
->packet_len
);
6361 ofpmsg_update_length(msg
);
6366 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
6368 make_echo_request(enum ofp_version ofp_version
)
6370 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST
, ofp_version
,
6374 /* Creates and returns an OFPT_ECHO_REPLY message matching the
6375 * OFPT_ECHO_REQUEST message in 'rq'. */
6377 make_echo_reply(const struct ofp_header
*rq
)
6379 struct ofpbuf rq_buf
;
6380 struct ofpbuf
*reply
;
6382 ofpbuf_use_const(&rq_buf
, rq
, ntohs(rq
->length
));
6383 ofpraw_pull_assert(&rq_buf
);
6385 reply
= ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY
, rq
, rq_buf
.size
);
6386 ofpbuf_put(reply
, rq_buf
.data
, rq_buf
.size
);
6391 ofputil_encode_barrier_request(enum ofp_version ofp_version
)
6395 switch (ofp_version
) {
6401 type
= OFPRAW_OFPT11_BARRIER_REQUEST
;
6405 type
= OFPRAW_OFPT10_BARRIER_REQUEST
;
6412 return ofpraw_alloc(type
, ofp_version
, 0);
6416 ofputil_frag_handling_to_string(enum ofputil_frag_handling frag
)
6419 case OFPUTIL_FRAG_NORMAL
: return "normal";
6420 case OFPUTIL_FRAG_DROP
: return "drop";
6421 case OFPUTIL_FRAG_REASM
: return "reassemble";
6422 case OFPUTIL_FRAG_NX_MATCH
: return "nx-match";
6429 ofputil_frag_handling_from_string(const char *s
,
6430 enum ofputil_frag_handling
*frag
)
6432 if (!strcasecmp(s
, "normal")) {
6433 *frag
= OFPUTIL_FRAG_NORMAL
;
6434 } else if (!strcasecmp(s
, "drop")) {
6435 *frag
= OFPUTIL_FRAG_DROP
;
6436 } else if (!strcasecmp(s
, "reassemble")) {
6437 *frag
= OFPUTIL_FRAG_REASM
;
6438 } else if (!strcasecmp(s
, "nx-match")) {
6439 *frag
= OFPUTIL_FRAG_NX_MATCH
;
6446 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
6447 * port number and stores the latter in '*ofp10_port', for the purpose of
6448 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
6449 * otherwise an OFPERR_* number. On error, stores OFPP_NONE in '*ofp10_port'.
6451 * See the definition of OFP11_MAX for an explanation of the mapping. */
6453 ofputil_port_from_ofp11(ovs_be32 ofp11_port
, ofp_port_t
*ofp10_port
)
6455 uint32_t ofp11_port_h
= ntohl(ofp11_port
);
6457 if (ofp11_port_h
< ofp_to_u16(OFPP_MAX
)) {
6458 *ofp10_port
= u16_to_ofp(ofp11_port_h
);
6460 } else if (ofp11_port_h
>= ofp11_to_u32(OFPP11_MAX
)) {
6461 *ofp10_port
= u16_to_ofp(ofp11_port_h
- OFPP11_OFFSET
);
6464 *ofp10_port
= OFPP_NONE
;
6465 VLOG_WARN_RL(&bad_ofmsg_rl
, "port %"PRIu32
" is outside the supported "
6466 "range 0 through %d or 0x%"PRIx32
" through 0x%"PRIx32
,
6467 ofp11_port_h
, ofp_to_u16(OFPP_MAX
) - 1,
6468 ofp11_to_u32(OFPP11_MAX
), UINT32_MAX
);
6469 return OFPERR_OFPBAC_BAD_OUT_PORT
;
6473 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
6474 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
6476 * See the definition of OFP11_MAX for an explanation of the mapping. */
6478 ofputil_port_to_ofp11(ofp_port_t ofp10_port
)
6480 return htonl(ofp_to_u16(ofp10_port
) < ofp_to_u16(OFPP_MAX
)
6481 ? ofp_to_u16(ofp10_port
)
6482 : ofp_to_u16(ofp10_port
) + OFPP11_OFFSET
);
6485 #define OFPUTIL_NAMED_PORTS \
6486 OFPUTIL_NAMED_PORT(IN_PORT) \
6487 OFPUTIL_NAMED_PORT(TABLE) \
6488 OFPUTIL_NAMED_PORT(NORMAL) \
6489 OFPUTIL_NAMED_PORT(FLOOD) \
6490 OFPUTIL_NAMED_PORT(ALL) \
6491 OFPUTIL_NAMED_PORT(CONTROLLER) \
6492 OFPUTIL_NAMED_PORT(LOCAL) \
6493 OFPUTIL_NAMED_PORT(ANY) \
6494 OFPUTIL_NAMED_PORT(UNSET)
6496 /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */
6497 #define OFPUTIL_NAMED_PORTS_WITH_NONE \
6498 OFPUTIL_NAMED_PORTS \
6499 OFPUTIL_NAMED_PORT(NONE)
6501 /* Stores the port number represented by 's' into '*portp'. 's' may be an
6502 * integer or, for reserved ports, the standard OpenFlow name for the port
6505 * Returns true if successful, false if 's' is not a valid OpenFlow port number
6506 * or name. The caller should issue an error message in this case, because
6507 * this function usually does not. (This gives the caller an opportunity to
6508 * look up the port name another way, e.g. by contacting the switch and listing
6509 * the names of all its ports).
6511 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
6512 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
6513 * range as described in include/openflow/openflow-1.1.h. */
6515 ofputil_port_from_string(const char *s
, ofp_port_t
*portp
)
6517 unsigned int port32
; /* int is at least 32 bits wide. */
6520 VLOG_WARN("Negative value %s is not a valid port number.", s
);
6524 if (str_to_uint(s
, 10, &port32
)) {
6525 if (port32
< ofp_to_u16(OFPP_MAX
)) {
6527 } else if (port32
< ofp_to_u16(OFPP_FIRST_RESV
)) {
6528 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
6529 "be translated to %u when talking to an OF1.1 or "
6530 "later controller", port32
, port32
+ OFPP11_OFFSET
);
6531 } else if (port32
<= ofp_to_u16(OFPP_LAST_RESV
)) {
6532 char name
[OFP_MAX_PORT_NAME_LEN
];
6534 ofputil_port_to_string(u16_to_ofp(port32
), name
, sizeof name
);
6535 VLOG_WARN_ONCE("referring to port %s as %"PRIu32
" is deprecated "
6536 "for compatibility with OpenFlow 1.1 and later",
6538 } else if (port32
< ofp11_to_u32(OFPP11_MAX
)) {
6539 VLOG_WARN("port %u is outside the supported range 0 through "
6540 "%"PRIx16
" or 0x%x through 0x%"PRIx32
, port32
,
6541 UINT16_MAX
, ofp11_to_u32(OFPP11_MAX
), UINT32_MAX
);
6544 port32
-= OFPP11_OFFSET
;
6547 *portp
= u16_to_ofp(port32
);
6554 static const struct pair pairs
[] = {
6555 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
6556 OFPUTIL_NAMED_PORTS_WITH_NONE
6557 #undef OFPUTIL_NAMED_PORT
6559 const struct pair
*p
;
6561 for (p
= pairs
; p
< &pairs
[ARRAY_SIZE(pairs
)]; p
++) {
6562 if (!strcasecmp(s
, p
->name
)) {
6571 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
6572 * Most ports' string representation is just the port number, but for special
6573 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
6575 ofputil_format_port(ofp_port_t port
, struct ds
*s
)
6577 char name
[OFP_MAX_PORT_NAME_LEN
];
6579 ofputil_port_to_string(port
, name
, sizeof name
);
6580 ds_put_cstr(s
, name
);
6583 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
6584 * representation of OpenFlow port number 'port'. Most ports are represented
6585 * as just the port number, but special ports, e.g. OFPP_LOCAL, are represented
6586 * by name, e.g. "LOCAL". */
6588 ofputil_port_to_string(ofp_port_t port
,
6589 char namebuf
[OFP_MAX_PORT_NAME_LEN
], size_t bufsize
)
6592 #define OFPUTIL_NAMED_PORT(NAME) \
6594 ovs_strlcpy(namebuf, #NAME, bufsize); \
6597 #undef OFPUTIL_NAMED_PORT
6600 snprintf(namebuf
, bufsize
, "%"PRIu16
, port
);
6605 /* Stores the group id represented by 's' into '*group_idp'. 's' may be an
6606 * integer or, for reserved group IDs, the standard OpenFlow name for the group
6607 * (either "ANY" or "ALL").
6609 * Returns true if successful, false if 's' is not a valid OpenFlow group ID or
6612 ofputil_group_from_string(const char *s
, uint32_t *group_idp
)
6614 if (!strcasecmp(s
, "any")) {
6615 *group_idp
= OFPG_ANY
;
6616 } else if (!strcasecmp(s
, "all")) {
6617 *group_idp
= OFPG_ALL
;
6618 } else if (!str_to_uint(s
, 10, group_idp
)) {
6619 VLOG_WARN("%s is not a valid group ID. (Valid group IDs are "
6620 "32-bit nonnegative integers or the keywords ANY or "
6628 /* Appends to 's' a string representation of the OpenFlow group ID 'group_id'.
6629 * Most groups' string representation is just the number, but for special
6630 * groups, e.g. OFPG_ALL, it is the name, e.g. "ALL". */
6632 ofputil_format_group(uint32_t group_id
, struct ds
*s
)
6634 char name
[MAX_GROUP_NAME_LEN
];
6636 ofputil_group_to_string(group_id
, name
, sizeof name
);
6637 ds_put_cstr(s
, name
);
6641 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
6642 * representation of OpenFlow group ID 'group_id'. Most group are represented
6643 * as just their number, but special groups, e.g. OFPG_ALL, are represented
6644 * by name, e.g. "ALL". */
6646 ofputil_group_to_string(uint32_t group_id
,
6647 char namebuf
[MAX_GROUP_NAME_LEN
+ 1], size_t bufsize
)
6651 ovs_strlcpy(namebuf
, "ALL", bufsize
);
6655 ovs_strlcpy(namebuf
, "ANY", bufsize
);
6659 snprintf(namebuf
, bufsize
, "%"PRIu32
, group_id
);
6664 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
6665 * 'ofp_version', tries to pull the first element from the array. If
6666 * successful, initializes '*pp' with an abstract representation of the
6667 * port and returns 0. If no ports remain to be decoded, returns EOF.
6668 * On an error, returns a positive OFPERR_* value. */
6670 ofputil_pull_phy_port(enum ofp_version ofp_version
, struct ofpbuf
*b
,
6671 struct ofputil_phy_port
*pp
)
6673 memset(pp
, 0, sizeof *pp
);
6675 switch (ofp_version
) {
6676 case OFP10_VERSION
: {
6677 const struct ofp10_phy_port
*opp
= ofpbuf_try_pull(b
, sizeof *opp
);
6678 return opp
? ofputil_decode_ofp10_phy_port(pp
, opp
) : EOF
;
6682 case OFP13_VERSION
: {
6683 const struct ofp11_port
*op
= ofpbuf_try_pull(b
, sizeof *op
);
6684 return op
? ofputil_decode_ofp11_port(pp
, op
) : EOF
;
6688 return b
->size
? ofputil_pull_ofp14_port(pp
, b
) : EOF
;
6695 ofputil_normalize_match__(struct match
*match
, bool may_log
)
6698 MAY_NW_ADDR
= 1 << 0, /* nw_src, nw_dst */
6699 MAY_TP_ADDR
= 1 << 1, /* tp_src, tp_dst */
6700 MAY_NW_PROTO
= 1 << 2, /* nw_proto */
6701 MAY_IPVx
= 1 << 3, /* tos, frag, ttl */
6702 MAY_ARP_SHA
= 1 << 4, /* arp_sha */
6703 MAY_ARP_THA
= 1 << 5, /* arp_tha */
6704 MAY_IPV6
= 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
6705 MAY_ND_TARGET
= 1 << 7, /* nd_target */
6706 MAY_MPLS
= 1 << 8, /* mpls label and tc */
6709 struct flow_wildcards wc
;
6711 /* Figure out what fields may be matched. */
6712 if (match
->flow
.dl_type
== htons(ETH_TYPE_IP
)) {
6713 may_match
= MAY_NW_PROTO
| MAY_IPVx
| MAY_NW_ADDR
;
6714 if (match
->flow
.nw_proto
== IPPROTO_TCP
||
6715 match
->flow
.nw_proto
== IPPROTO_UDP
||
6716 match
->flow
.nw_proto
== IPPROTO_SCTP
||
6717 match
->flow
.nw_proto
== IPPROTO_ICMP
) {
6718 may_match
|= MAY_TP_ADDR
;
6720 } else if (match
->flow
.dl_type
== htons(ETH_TYPE_IPV6
)) {
6721 may_match
= MAY_NW_PROTO
| MAY_IPVx
| MAY_IPV6
;
6722 if (match
->flow
.nw_proto
== IPPROTO_TCP
||
6723 match
->flow
.nw_proto
== IPPROTO_UDP
||
6724 match
->flow
.nw_proto
== IPPROTO_SCTP
) {
6725 may_match
|= MAY_TP_ADDR
;
6726 } else if (match
->flow
.nw_proto
== IPPROTO_ICMPV6
) {
6727 may_match
|= MAY_TP_ADDR
;
6728 if (match
->flow
.tp_src
== htons(ND_NEIGHBOR_SOLICIT
)) {
6729 may_match
|= MAY_ND_TARGET
| MAY_ARP_SHA
;
6730 } else if (match
->flow
.tp_src
== htons(ND_NEIGHBOR_ADVERT
)) {
6731 may_match
|= MAY_ND_TARGET
| MAY_ARP_THA
;
6734 } else if (match
->flow
.dl_type
== htons(ETH_TYPE_ARP
) ||
6735 match
->flow
.dl_type
== htons(ETH_TYPE_RARP
)) {
6736 may_match
= MAY_NW_PROTO
| MAY_NW_ADDR
| MAY_ARP_SHA
| MAY_ARP_THA
;
6737 } else if (eth_type_mpls(match
->flow
.dl_type
)) {
6738 may_match
= MAY_MPLS
;
6743 /* Clear the fields that may not be matched. */
6745 if (!(may_match
& MAY_NW_ADDR
)) {
6746 wc
.masks
.nw_src
= wc
.masks
.nw_dst
= htonl(0);
6748 if (!(may_match
& MAY_TP_ADDR
)) {
6749 wc
.masks
.tp_src
= wc
.masks
.tp_dst
= htons(0);
6751 if (!(may_match
& MAY_NW_PROTO
)) {
6752 wc
.masks
.nw_proto
= 0;
6754 if (!(may_match
& MAY_IPVx
)) {
6755 wc
.masks
.nw_tos
= 0;
6756 wc
.masks
.nw_ttl
= 0;
6758 if (!(may_match
& MAY_ARP_SHA
)) {
6759 WC_UNMASK_FIELD(&wc
, arp_sha
);
6761 if (!(may_match
& MAY_ARP_THA
)) {
6762 WC_UNMASK_FIELD(&wc
, arp_tha
);
6764 if (!(may_match
& MAY_IPV6
)) {
6765 wc
.masks
.ipv6_src
= wc
.masks
.ipv6_dst
= in6addr_any
;
6766 wc
.masks
.ipv6_label
= htonl(0);
6768 if (!(may_match
& MAY_ND_TARGET
)) {
6769 wc
.masks
.nd_target
= in6addr_any
;
6771 if (!(may_match
& MAY_MPLS
)) {
6772 memset(wc
.masks
.mpls_lse
, 0, sizeof wc
.masks
.mpls_lse
);
6775 /* Log any changes. */
6776 if (!flow_wildcards_equal(&wc
, &match
->wc
)) {
6777 bool log
= may_log
&& !VLOG_DROP_INFO(&bad_ofmsg_rl
);
6778 char *pre
= log
? match_to_string(match
, OFP_DEFAULT_PRIORITY
) : NULL
;
6781 match_zero_wildcarded_fields(match
);
6784 char *post
= match_to_string(match
, OFP_DEFAULT_PRIORITY
);
6785 VLOG_INFO("normalization changed ofp_match, details:");
6786 VLOG_INFO(" pre: %s", pre
);
6787 VLOG_INFO("post: %s", post
);
6794 /* "Normalizes" the wildcards in 'match'. That means:
6796 * 1. If the type of level N is known, then only the valid fields for that
6797 * level may be specified. For example, ARP does not have a TOS field,
6798 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
6799 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
6800 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
6803 * 2. If the type of level N is not known (or not understood by Open
6804 * vSwitch), then no fields at all for that level may be specified. For
6805 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
6806 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
6809 * If this function changes 'match', it logs a rate-limited informational
6812 ofputil_normalize_match(struct match
*match
)
6814 ofputil_normalize_match__(match
, true);
6817 /* Same as ofputil_normalize_match() without the logging. Thus, this function
6818 * is suitable for a program's internal use, whereas ofputil_normalize_match()
6819 * sense for use on flows received from elsewhere (so that a bug in the program
6820 * that sent them can be reported and corrected). */
6822 ofputil_normalize_match_quiet(struct match
*match
)
6824 ofputil_normalize_match__(match
, false);
6827 /* Parses a key or a key-value pair from '*stringp'.
6829 * On success: Stores the key into '*keyp'. Stores the value, if present, into
6830 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
6831 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
6832 * are substrings of '*stringp' created by replacing some of its bytes by null
6833 * terminators. Returns true.
6835 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
6836 * NULL and returns false. */
6838 ofputil_parse_key_value(char **stringp
, char **keyp
, char **valuep
)
6840 char *pos
, *key
, *value
;
6844 pos
+= strspn(pos
, ", \t\r\n");
6846 *keyp
= *valuep
= NULL
;
6851 key_len
= strcspn(pos
, ":=(, \t\r\n");
6852 if (key
[key_len
] == ':' || key
[key_len
] == '=') {
6853 /* The value can be separated by a colon. */
6856 value
= key
+ key_len
+ 1;
6857 value_len
= strcspn(value
, ", \t\r\n");
6858 pos
= value
+ value_len
+ (value
[value_len
] != '\0');
6859 value
[value_len
] = '\0';
6860 } else if (key
[key_len
] == '(') {
6861 /* The value can be surrounded by balanced parentheses. The outermost
6862 * set of parentheses is removed. */
6866 value
= key
+ key_len
+ 1;
6867 for (value_len
= 0; level
> 0; value_len
++) {
6868 switch (value
[value_len
]) {
6882 value
[value_len
- 1] = '\0';
6883 pos
= value
+ value_len
;
6885 /* There might be no value at all. */
6886 value
= key
+ key_len
; /* Will become the empty string below. */
6887 pos
= key
+ key_len
+ (key
[key_len
] != '\0');
6889 key
[key_len
] = '\0';
6897 /* Encode a dump ports request for 'port', the encoded message
6898 * will be for OpenFlow version 'ofp_version'. Returns message
6899 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
6901 ofputil_encode_dump_ports_request(enum ofp_version ofp_version
, ofp_port_t port
)
6903 struct ofpbuf
*request
;
6905 switch (ofp_version
) {
6906 case OFP10_VERSION
: {
6907 struct ofp10_port_stats_request
*req
;
6908 request
= ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST
, ofp_version
, 0);
6909 req
= ofpbuf_put_zeros(request
, sizeof *req
);
6910 req
->port_no
= htons(ofp_to_u16(port
));
6917 case OFP15_VERSION
: {
6918 struct ofp11_port_stats_request
*req
;
6919 request
= ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST
, ofp_version
, 0);
6920 req
= ofpbuf_put_zeros(request
, sizeof *req
);
6921 req
->port_no
= ofputil_port_to_ofp11(port
);
6932 ofputil_port_stats_to_ofp10(const struct ofputil_port_stats
*ops
,
6933 struct ofp10_port_stats
*ps10
)
6935 ps10
->port_no
= htons(ofp_to_u16(ops
->port_no
));
6936 memset(ps10
->pad
, 0, sizeof ps10
->pad
);
6937 put_32aligned_be64(&ps10
->rx_packets
, htonll(ops
->stats
.rx_packets
));
6938 put_32aligned_be64(&ps10
->tx_packets
, htonll(ops
->stats
.tx_packets
));
6939 put_32aligned_be64(&ps10
->rx_bytes
, htonll(ops
->stats
.rx_bytes
));
6940 put_32aligned_be64(&ps10
->tx_bytes
, htonll(ops
->stats
.tx_bytes
));
6941 put_32aligned_be64(&ps10
->rx_dropped
, htonll(ops
->stats
.rx_dropped
));
6942 put_32aligned_be64(&ps10
->tx_dropped
, htonll(ops
->stats
.tx_dropped
));
6943 put_32aligned_be64(&ps10
->rx_errors
, htonll(ops
->stats
.rx_errors
));
6944 put_32aligned_be64(&ps10
->tx_errors
, htonll(ops
->stats
.tx_errors
));
6945 put_32aligned_be64(&ps10
->rx_frame_err
, htonll(ops
->stats
.rx_frame_errors
));
6946 put_32aligned_be64(&ps10
->rx_over_err
, htonll(ops
->stats
.rx_over_errors
));
6947 put_32aligned_be64(&ps10
->rx_crc_err
, htonll(ops
->stats
.rx_crc_errors
));
6948 put_32aligned_be64(&ps10
->collisions
, htonll(ops
->stats
.collisions
));
6952 ofputil_port_stats_to_ofp11(const struct ofputil_port_stats
*ops
,
6953 struct ofp11_port_stats
*ps11
)
6955 ps11
->port_no
= ofputil_port_to_ofp11(ops
->port_no
);
6956 memset(ps11
->pad
, 0, sizeof ps11
->pad
);
6957 ps11
->rx_packets
= htonll(ops
->stats
.rx_packets
);
6958 ps11
->tx_packets
= htonll(ops
->stats
.tx_packets
);
6959 ps11
->rx_bytes
= htonll(ops
->stats
.rx_bytes
);
6960 ps11
->tx_bytes
= htonll(ops
->stats
.tx_bytes
);
6961 ps11
->rx_dropped
= htonll(ops
->stats
.rx_dropped
);
6962 ps11
->tx_dropped
= htonll(ops
->stats
.tx_dropped
);
6963 ps11
->rx_errors
= htonll(ops
->stats
.rx_errors
);
6964 ps11
->tx_errors
= htonll(ops
->stats
.tx_errors
);
6965 ps11
->rx_frame_err
= htonll(ops
->stats
.rx_frame_errors
);
6966 ps11
->rx_over_err
= htonll(ops
->stats
.rx_over_errors
);
6967 ps11
->rx_crc_err
= htonll(ops
->stats
.rx_crc_errors
);
6968 ps11
->collisions
= htonll(ops
->stats
.collisions
);
6972 ofputil_port_stats_to_ofp13(const struct ofputil_port_stats
*ops
,
6973 struct ofp13_port_stats
*ps13
)
6975 ofputil_port_stats_to_ofp11(ops
, &ps13
->ps
);
6976 ps13
->duration_sec
= htonl(ops
->duration_sec
);
6977 ps13
->duration_nsec
= htonl(ops
->duration_nsec
);
6981 ofputil_append_ofp14_port_stats(const struct ofputil_port_stats
*ops
,
6982 struct ovs_list
*replies
)
6984 struct ofp14_port_stats_prop_ethernet
*eth
;
6985 struct ofp14_port_stats
*ps14
;
6986 struct ofpbuf
*reply
;
6988 reply
= ofpmp_reserve(replies
, sizeof *ps14
+ sizeof *eth
);
6990 ps14
= ofpbuf_put_uninit(reply
, sizeof *ps14
);
6991 ps14
->length
= htons(sizeof *ps14
+ sizeof *eth
);
6992 memset(ps14
->pad
, 0, sizeof ps14
->pad
);
6993 ps14
->port_no
= ofputil_port_to_ofp11(ops
->port_no
);
6994 ps14
->duration_sec
= htonl(ops
->duration_sec
);
6995 ps14
->duration_nsec
= htonl(ops
->duration_nsec
);
6996 ps14
->rx_packets
= htonll(ops
->stats
.rx_packets
);
6997 ps14
->tx_packets
= htonll(ops
->stats
.tx_packets
);
6998 ps14
->rx_bytes
= htonll(ops
->stats
.rx_bytes
);
6999 ps14
->tx_bytes
= htonll(ops
->stats
.tx_bytes
);
7000 ps14
->rx_dropped
= htonll(ops
->stats
.rx_dropped
);
7001 ps14
->tx_dropped
= htonll(ops
->stats
.tx_dropped
);
7002 ps14
->rx_errors
= htonll(ops
->stats
.rx_errors
);
7003 ps14
->tx_errors
= htonll(ops
->stats
.tx_errors
);
7005 eth
= ofpprop_put_zeros(reply
, OFPPSPT14_ETHERNET
, sizeof *eth
);
7006 eth
->rx_frame_err
= htonll(ops
->stats
.rx_frame_errors
);
7007 eth
->rx_over_err
= htonll(ops
->stats
.rx_over_errors
);
7008 eth
->rx_crc_err
= htonll(ops
->stats
.rx_crc_errors
);
7009 eth
->collisions
= htonll(ops
->stats
.collisions
);
7012 /* Encode a ports stat for 'ops' and append it to 'replies'. */
7014 ofputil_append_port_stat(struct ovs_list
*replies
,
7015 const struct ofputil_port_stats
*ops
)
7017 switch (ofpmp_version(replies
)) {
7018 case OFP13_VERSION
: {
7019 struct ofp13_port_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
7020 ofputil_port_stats_to_ofp13(ops
, reply
);
7024 case OFP11_VERSION
: {
7025 struct ofp11_port_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
7026 ofputil_port_stats_to_ofp11(ops
, reply
);
7030 case OFP10_VERSION
: {
7031 struct ofp10_port_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
7032 ofputil_port_stats_to_ofp10(ops
, reply
);
7038 ofputil_append_ofp14_port_stats(ops
, replies
);
7047 ofputil_port_stats_from_ofp10(struct ofputil_port_stats
*ops
,
7048 const struct ofp10_port_stats
*ps10
)
7050 memset(ops
, 0, sizeof *ops
);
7052 ops
->port_no
= u16_to_ofp(ntohs(ps10
->port_no
));
7053 ops
->stats
.rx_packets
= ntohll(get_32aligned_be64(&ps10
->rx_packets
));
7054 ops
->stats
.tx_packets
= ntohll(get_32aligned_be64(&ps10
->tx_packets
));
7055 ops
->stats
.rx_bytes
= ntohll(get_32aligned_be64(&ps10
->rx_bytes
));
7056 ops
->stats
.tx_bytes
= ntohll(get_32aligned_be64(&ps10
->tx_bytes
));
7057 ops
->stats
.rx_dropped
= ntohll(get_32aligned_be64(&ps10
->rx_dropped
));
7058 ops
->stats
.tx_dropped
= ntohll(get_32aligned_be64(&ps10
->tx_dropped
));
7059 ops
->stats
.rx_errors
= ntohll(get_32aligned_be64(&ps10
->rx_errors
));
7060 ops
->stats
.tx_errors
= ntohll(get_32aligned_be64(&ps10
->tx_errors
));
7061 ops
->stats
.rx_frame_errors
=
7062 ntohll(get_32aligned_be64(&ps10
->rx_frame_err
));
7063 ops
->stats
.rx_over_errors
= ntohll(get_32aligned_be64(&ps10
->rx_over_err
));
7064 ops
->stats
.rx_crc_errors
= ntohll(get_32aligned_be64(&ps10
->rx_crc_err
));
7065 ops
->stats
.collisions
= ntohll(get_32aligned_be64(&ps10
->collisions
));
7066 ops
->duration_sec
= ops
->duration_nsec
= UINT32_MAX
;
7072 ofputil_port_stats_from_ofp11(struct ofputil_port_stats
*ops
,
7073 const struct ofp11_port_stats
*ps11
)
7077 memset(ops
, 0, sizeof *ops
);
7078 error
= ofputil_port_from_ofp11(ps11
->port_no
, &ops
->port_no
);
7083 ops
->stats
.rx_packets
= ntohll(ps11
->rx_packets
);
7084 ops
->stats
.tx_packets
= ntohll(ps11
->tx_packets
);
7085 ops
->stats
.rx_bytes
= ntohll(ps11
->rx_bytes
);
7086 ops
->stats
.tx_bytes
= ntohll(ps11
->tx_bytes
);
7087 ops
->stats
.rx_dropped
= ntohll(ps11
->rx_dropped
);
7088 ops
->stats
.tx_dropped
= ntohll(ps11
->tx_dropped
);
7089 ops
->stats
.rx_errors
= ntohll(ps11
->rx_errors
);
7090 ops
->stats
.tx_errors
= ntohll(ps11
->tx_errors
);
7091 ops
->stats
.rx_frame_errors
= ntohll(ps11
->rx_frame_err
);
7092 ops
->stats
.rx_over_errors
= ntohll(ps11
->rx_over_err
);
7093 ops
->stats
.rx_crc_errors
= ntohll(ps11
->rx_crc_err
);
7094 ops
->stats
.collisions
= ntohll(ps11
->collisions
);
7095 ops
->duration_sec
= ops
->duration_nsec
= UINT32_MAX
;
7101 ofputil_port_stats_from_ofp13(struct ofputil_port_stats
*ops
,
7102 const struct ofp13_port_stats
*ps13
)
7104 enum ofperr error
= ofputil_port_stats_from_ofp11(ops
, &ps13
->ps
);
7106 ops
->duration_sec
= ntohl(ps13
->duration_sec
);
7107 ops
->duration_nsec
= ntohl(ps13
->duration_nsec
);
7113 parse_ofp14_port_stats_ethernet_property(const struct ofpbuf
*payload
,
7114 struct ofputil_port_stats
*ops
)
7116 const struct ofp14_port_stats_prop_ethernet
*eth
= payload
->data
;
7118 if (payload
->size
!= sizeof *eth
) {
7119 return OFPERR_OFPBPC_BAD_LEN
;
7122 ops
->stats
.rx_frame_errors
= ntohll(eth
->rx_frame_err
);
7123 ops
->stats
.rx_over_errors
= ntohll(eth
->rx_over_err
);
7124 ops
->stats
.rx_crc_errors
= ntohll(eth
->rx_crc_err
);
7125 ops
->stats
.collisions
= ntohll(eth
->collisions
);
7131 ofputil_pull_ofp14_port_stats(struct ofputil_port_stats
*ops
,
7134 const struct ofp14_port_stats
*ps14
;
7135 struct ofpbuf properties
;
7139 ps14
= ofpbuf_try_pull(msg
, sizeof *ps14
);
7141 return OFPERR_OFPBRC_BAD_LEN
;
7144 len
= ntohs(ps14
->length
);
7145 if (len
< sizeof *ps14
|| len
- sizeof *ps14
> msg
->size
) {
7146 return OFPERR_OFPBRC_BAD_LEN
;
7148 len
-= sizeof *ps14
;
7149 ofpbuf_use_const(&properties
, ofpbuf_pull(msg
, len
), len
);
7151 error
= ofputil_port_from_ofp11(ps14
->port_no
, &ops
->port_no
);
7156 ops
->duration_sec
= ntohl(ps14
->duration_sec
);
7157 ops
->duration_nsec
= ntohl(ps14
->duration_nsec
);
7158 ops
->stats
.rx_packets
= ntohll(ps14
->rx_packets
);
7159 ops
->stats
.tx_packets
= ntohll(ps14
->tx_packets
);
7160 ops
->stats
.rx_bytes
= ntohll(ps14
->rx_bytes
);
7161 ops
->stats
.tx_bytes
= ntohll(ps14
->tx_bytes
);
7162 ops
->stats
.rx_dropped
= ntohll(ps14
->rx_dropped
);
7163 ops
->stats
.tx_dropped
= ntohll(ps14
->tx_dropped
);
7164 ops
->stats
.rx_errors
= ntohll(ps14
->rx_errors
);
7165 ops
->stats
.tx_errors
= ntohll(ps14
->tx_errors
);
7166 ops
->stats
.rx_frame_errors
= UINT64_MAX
;
7167 ops
->stats
.rx_over_errors
= UINT64_MAX
;
7168 ops
->stats
.rx_crc_errors
= UINT64_MAX
;
7169 ops
->stats
.collisions
= UINT64_MAX
;
7171 while (properties
.size
> 0) {
7172 struct ofpbuf payload
;
7176 error
= ofpprop_pull(&properties
, &payload
, &type
);
7182 case OFPPSPT14_ETHERNET
:
7183 error
= parse_ofp14_port_stats_ethernet_property(&payload
, ops
);
7187 error
= OFPPROP_UNKNOWN(true, "port stats", type
);
7199 /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
7202 ofputil_count_port_stats(const struct ofp_header
*oh
)
7204 struct ofputil_port_stats ps
;
7208 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
7209 ofpraw_pull_assert(&b
);
7210 while (!ofputil_decode_port_stats(&ps
, &b
)) {
7216 /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
7217 * ofputil_port_stats in 'ps'.
7219 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
7220 * message. Calling this function multiple times for a single 'msg' iterates
7221 * through the replies. The caller must initially leave 'msg''s layer pointers
7222 * null and not modify them between calls.
7224 * Returns 0 if successful, EOF if no replies were left in this 'msg',
7225 * otherwise a positive errno value. */
7227 ofputil_decode_port_stats(struct ofputil_port_stats
*ps
, struct ofpbuf
*msg
)
7232 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
7233 : ofpraw_pull(&raw
, msg
));
7240 } else if (raw
== OFPRAW_OFPST14_PORT_REPLY
) {
7241 return ofputil_pull_ofp14_port_stats(ps
, msg
);
7242 } else if (raw
== OFPRAW_OFPST13_PORT_REPLY
) {
7243 const struct ofp13_port_stats
*ps13
;
7245 ps13
= ofpbuf_try_pull(msg
, sizeof *ps13
);
7249 return ofputil_port_stats_from_ofp13(ps
, ps13
);
7250 } else if (raw
== OFPRAW_OFPST11_PORT_REPLY
) {
7251 const struct ofp11_port_stats
*ps11
;
7253 ps11
= ofpbuf_try_pull(msg
, sizeof *ps11
);
7257 return ofputil_port_stats_from_ofp11(ps
, ps11
);
7258 } else if (raw
== OFPRAW_OFPST10_PORT_REPLY
) {
7259 const struct ofp10_port_stats
*ps10
;
7261 ps10
= ofpbuf_try_pull(msg
, sizeof *ps10
);
7265 return ofputil_port_stats_from_ofp10(ps
, ps10
);
7271 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_PORT reply has %"PRIu32
" leftover "
7272 "bytes at end", msg
->size
);
7273 return OFPERR_OFPBRC_BAD_LEN
;
7276 /* Parse a port status request message into a 16 bit OpenFlow 1.0
7277 * port number and stores the latter in '*ofp10_port'.
7278 * Returns 0 if successful, otherwise an OFPERR_* number. */
7280 ofputil_decode_port_stats_request(const struct ofp_header
*request
,
7281 ofp_port_t
*ofp10_port
)
7283 switch ((enum ofp_version
)request
->version
) {
7288 case OFP11_VERSION
: {
7289 const struct ofp11_port_stats_request
*psr11
= ofpmsg_body(request
);
7290 return ofputil_port_from_ofp11(psr11
->port_no
, ofp10_port
);
7293 case OFP10_VERSION
: {
7294 const struct ofp10_port_stats_request
*psr10
= ofpmsg_body(request
);
7295 *ofp10_port
= u16_to_ofp(ntohs(psr10
->port_no
));
7304 /* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */
7306 ofputil_bucket_list_destroy(struct ovs_list
*buckets
)
7308 struct ofputil_bucket
*bucket
;
7310 LIST_FOR_EACH_POP (bucket
, list_node
, buckets
) {
7311 free(bucket
->ofpacts
);
7316 /* Clones 'bucket' and its ofpacts data */
7317 static struct ofputil_bucket
*
7318 ofputil_bucket_clone_data(const struct ofputil_bucket
*bucket
)
7320 struct ofputil_bucket
*new;
7322 new = xmemdup(bucket
, sizeof *bucket
);
7323 new->ofpacts
= xmemdup(bucket
->ofpacts
, bucket
->ofpacts_len
);
7328 /* Clones each of the buckets in the list 'src' appending them
7329 * in turn to 'dest' which should be an initialised list.
7330 * An exception is that if the pointer value of a bucket in 'src'
7331 * matches 'skip' then it is not cloned or appended to 'dest'.
7332 * This allows all of 'src' or 'all of 'src' except 'skip' to
7333 * be cloned and appended to 'dest'. */
7335 ofputil_bucket_clone_list(struct ovs_list
*dest
, const struct ovs_list
*src
,
7336 const struct ofputil_bucket
*skip
)
7338 struct ofputil_bucket
*bucket
;
7340 LIST_FOR_EACH (bucket
, list_node
, src
) {
7341 struct ofputil_bucket
*new_bucket
;
7343 if (bucket
== skip
) {
7347 new_bucket
= ofputil_bucket_clone_data(bucket
);
7348 list_push_back(dest
, &new_bucket
->list_node
);
7352 /* Find a bucket in the list 'buckets' whose bucket id is 'bucket_id'
7353 * Returns the first bucket found or NULL if no buckets are found. */
7354 struct ofputil_bucket
*
7355 ofputil_bucket_find(const struct ovs_list
*buckets
, uint32_t bucket_id
)
7357 struct ofputil_bucket
*bucket
;
7359 if (bucket_id
> OFPG15_BUCKET_MAX
) {
7363 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
7364 if (bucket
->bucket_id
== bucket_id
) {
7372 /* Returns true if more than one bucket in the list 'buckets'
7373 * have the same bucket id. Returns false otherwise. */
7375 ofputil_bucket_check_duplicate_id(const struct ovs_list
*buckets
)
7377 struct ofputil_bucket
*i
, *j
;
7379 LIST_FOR_EACH (i
, list_node
, buckets
) {
7380 LIST_FOR_EACH_REVERSE (j
, list_node
, buckets
) {
7384 if (i
->bucket_id
== j
->bucket_id
) {
7393 /* Returns the bucket at the front of the list 'buckets'.
7394 * Undefined if 'buckets is empty. */
7395 struct ofputil_bucket
*
7396 ofputil_bucket_list_front(const struct ovs_list
*buckets
)
7398 static struct ofputil_bucket
*bucket
;
7400 ASSIGN_CONTAINER(bucket
, list_front(buckets
), list_node
);
7405 /* Returns the bucket at the back of the list 'buckets'.
7406 * Undefined if 'buckets is empty. */
7407 struct ofputil_bucket
*
7408 ofputil_bucket_list_back(const struct ovs_list
*buckets
)
7410 static struct ofputil_bucket
*bucket
;
7412 ASSIGN_CONTAINER(bucket
, list_back(buckets
), list_node
);
7417 /* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version',
7418 * that requests stats for group 'group_id'. (Use OFPG_ALL to request stats
7421 * Group statistics include packet and byte counts for each group. */
7423 ofputil_encode_group_stats_request(enum ofp_version ofp_version
,
7426 struct ofpbuf
*request
;
7428 switch (ofp_version
) {
7430 ovs_fatal(0, "dump-group-stats needs OpenFlow 1.1 or later "
7431 "(\'-O OpenFlow11\')");
7436 case OFP15_VERSION
: {
7437 struct ofp11_group_stats_request
*req
;
7438 request
= ofpraw_alloc(OFPRAW_OFPST11_GROUP_REQUEST
, ofp_version
, 0);
7439 req
= ofpbuf_put_zeros(request
, sizeof *req
);
7440 req
->group_id
= htonl(group_id
);
7451 ofputil_uninit_group_desc(struct ofputil_group_desc
*gd
)
7453 ofputil_bucket_list_destroy(&gd
->buckets
);
7454 free(&gd
->props
.fields
);
7457 /* Decodes the OpenFlow group description request in 'oh', returning the group
7458 * whose description is requested, or OFPG_ALL if stats for all groups was
7461 ofputil_decode_group_desc_request(const struct ofp_header
*oh
)
7463 struct ofpbuf request
;
7466 ofpbuf_use_const(&request
, oh
, ntohs(oh
->length
));
7467 raw
= ofpraw_pull_assert(&request
);
7468 if (raw
== OFPRAW_OFPST11_GROUP_DESC_REQUEST
) {
7470 } else if (raw
== OFPRAW_OFPST15_GROUP_DESC_REQUEST
) {
7471 ovs_be32
*group_id
= ofpbuf_pull(&request
, sizeof *group_id
);
7472 return ntohl(*group_id
);
7478 /* Returns an OpenFlow group description request for OpenFlow version
7479 * 'ofp_version', that requests stats for group 'group_id'. Use OFPG_ALL to
7480 * request stats for all groups (OpenFlow 1.4 and earlier always request all
7483 * Group descriptions include the bucket and action configuration for each
7486 ofputil_encode_group_desc_request(enum ofp_version ofp_version
,
7489 struct ofpbuf
*request
;
7491 switch (ofp_version
) {
7493 ovs_fatal(0, "dump-groups needs OpenFlow 1.1 or later "
7494 "(\'-O OpenFlow11\')");
7499 request
= ofpraw_alloc(OFPRAW_OFPST11_GROUP_DESC_REQUEST
,
7502 case OFP15_VERSION
:{
7503 struct ofp15_group_desc_request
*req
;
7504 request
= ofpraw_alloc(OFPRAW_OFPST15_GROUP_DESC_REQUEST
,
7506 req
= ofpbuf_put_zeros(request
, sizeof *req
);
7507 req
->group_id
= htonl(group_id
);
7518 ofputil_group_bucket_counters_to_ofp11(const struct ofputil_group_stats
*gs
,
7519 struct ofp11_bucket_counter bucket_cnts
[])
7523 for (i
= 0; i
< gs
->n_buckets
; i
++) {
7524 bucket_cnts
[i
].packet_count
= htonll(gs
->bucket_stats
[i
].packet_count
);
7525 bucket_cnts
[i
].byte_count
= htonll(gs
->bucket_stats
[i
].byte_count
);
7530 ofputil_group_stats_to_ofp11(const struct ofputil_group_stats
*gs
,
7531 struct ofp11_group_stats
*gs11
, size_t length
,
7532 struct ofp11_bucket_counter bucket_cnts
[])
7534 memset(gs11
, 0, sizeof *gs11
);
7535 gs11
->length
= htons(length
);
7536 gs11
->group_id
= htonl(gs
->group_id
);
7537 gs11
->ref_count
= htonl(gs
->ref_count
);
7538 gs11
->packet_count
= htonll(gs
->packet_count
);
7539 gs11
->byte_count
= htonll(gs
->byte_count
);
7540 ofputil_group_bucket_counters_to_ofp11(gs
, bucket_cnts
);
7544 ofputil_group_stats_to_ofp13(const struct ofputil_group_stats
*gs
,
7545 struct ofp13_group_stats
*gs13
, size_t length
,
7546 struct ofp11_bucket_counter bucket_cnts
[])
7548 ofputil_group_stats_to_ofp11(gs
, &gs13
->gs
, length
, bucket_cnts
);
7549 gs13
->duration_sec
= htonl(gs
->duration_sec
);
7550 gs13
->duration_nsec
= htonl(gs
->duration_nsec
);
7554 /* Encodes 'gs' properly for the format of the list of group statistics
7555 * replies already begun in 'replies' and appends it to the list. 'replies'
7556 * must have originally been initialized with ofpmp_init(). */
7558 ofputil_append_group_stats(struct ovs_list
*replies
,
7559 const struct ofputil_group_stats
*gs
)
7561 size_t bucket_counter_size
;
7562 struct ofp11_bucket_counter
*bucket_counters
;
7565 bucket_counter_size
= gs
->n_buckets
* sizeof(struct ofp11_bucket_counter
);
7567 switch (ofpmp_version(replies
)) {
7569 case OFP12_VERSION
:{
7570 struct ofp11_group_stats
*gs11
;
7572 length
= sizeof *gs11
+ bucket_counter_size
;
7573 gs11
= ofpmp_append(replies
, length
);
7574 bucket_counters
= (struct ofp11_bucket_counter
*)(gs11
+ 1);
7575 ofputil_group_stats_to_ofp11(gs
, gs11
, length
, bucket_counters
);
7581 case OFP15_VERSION
: {
7582 struct ofp13_group_stats
*gs13
;
7584 length
= sizeof *gs13
+ bucket_counter_size
;
7585 gs13
= ofpmp_append(replies
, length
);
7586 bucket_counters
= (struct ofp11_bucket_counter
*)(gs13
+ 1);
7587 ofputil_group_stats_to_ofp13(gs
, gs13
, length
, bucket_counters
);
7596 /* Returns an OpenFlow group features request for OpenFlow version
7599 ofputil_encode_group_features_request(enum ofp_version ofp_version
)
7601 struct ofpbuf
*request
= NULL
;
7603 switch (ofp_version
) {
7606 ovs_fatal(0, "dump-group-features needs OpenFlow 1.2 or later "
7607 "(\'-O OpenFlow12\')");
7612 request
= ofpraw_alloc(OFPRAW_OFPST12_GROUP_FEATURES_REQUEST
,
7622 /* Returns a OpenFlow message that encodes 'features' properly as a reply to
7623 * group features request 'request'. */
7625 ofputil_encode_group_features_reply(
7626 const struct ofputil_group_features
*features
,
7627 const struct ofp_header
*request
)
7629 struct ofp12_group_features_stats
*ogf
;
7630 struct ofpbuf
*reply
;
7633 reply
= ofpraw_alloc_xid(OFPRAW_OFPST12_GROUP_FEATURES_REPLY
,
7634 request
->version
, request
->xid
, 0);
7635 ogf
= ofpbuf_put_zeros(reply
, sizeof *ogf
);
7636 ogf
->types
= htonl(features
->types
);
7637 ogf
->capabilities
= htonl(features
->capabilities
);
7638 for (i
= 0; i
< OFPGT12_N_TYPES
; i
++) {
7639 ogf
->max_groups
[i
] = htonl(features
->max_groups
[i
]);
7640 ogf
->actions
[i
] = ofpact_bitmap_to_openflow(features
->ofpacts
[i
],
7647 /* Decodes group features reply 'oh' into 'features'. */
7649 ofputil_decode_group_features_reply(const struct ofp_header
*oh
,
7650 struct ofputil_group_features
*features
)
7652 const struct ofp12_group_features_stats
*ogf
= ofpmsg_body(oh
);
7655 features
->types
= ntohl(ogf
->types
);
7656 features
->capabilities
= ntohl(ogf
->capabilities
);
7657 for (i
= 0; i
< OFPGT12_N_TYPES
; i
++) {
7658 features
->max_groups
[i
] = ntohl(ogf
->max_groups
[i
]);
7659 features
->ofpacts
[i
] = ofpact_bitmap_from_openflow(
7660 ogf
->actions
[i
], oh
->version
);
7664 /* Parse a group status request message into a 32 bit OpenFlow 1.1
7665 * group ID and stores the latter in '*group_id'.
7666 * Returns 0 if successful, otherwise an OFPERR_* number. */
7668 ofputil_decode_group_stats_request(const struct ofp_header
*request
,
7671 const struct ofp11_group_stats_request
*gsr11
= ofpmsg_body(request
);
7672 *group_id
= ntohl(gsr11
->group_id
);
7676 /* Converts a group stats reply in 'msg' into an abstract ofputil_group_stats
7677 * in 'gs'. Assigns freshly allocated memory to gs->bucket_stats for the
7678 * caller to eventually free.
7680 * Multiple group stats replies can be packed into a single OpenFlow message.
7681 * Calling this function multiple times for a single 'msg' iterates through the
7682 * replies. The caller must initially leave 'msg''s layer pointers null and
7683 * not modify them between calls.
7685 * Returns 0 if successful, EOF if no replies were left in this 'msg',
7686 * otherwise a positive errno value. */
7688 ofputil_decode_group_stats_reply(struct ofpbuf
*msg
,
7689 struct ofputil_group_stats
*gs
)
7691 struct ofp11_bucket_counter
*obc
;
7692 struct ofp11_group_stats
*ogs11
;
7699 gs
->bucket_stats
= NULL
;
7700 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
7701 : ofpraw_pull(&raw
, msg
));
7710 if (raw
== OFPRAW_OFPST11_GROUP_REPLY
) {
7711 base_len
= sizeof *ogs11
;
7712 ogs11
= ofpbuf_try_pull(msg
, sizeof *ogs11
);
7713 gs
->duration_sec
= gs
->duration_nsec
= UINT32_MAX
;
7714 } else if (raw
== OFPRAW_OFPST13_GROUP_REPLY
) {
7715 struct ofp13_group_stats
*ogs13
;
7717 base_len
= sizeof *ogs13
;
7718 ogs13
= ofpbuf_try_pull(msg
, sizeof *ogs13
);
7721 gs
->duration_sec
= ntohl(ogs13
->duration_sec
);
7722 gs
->duration_nsec
= ntohl(ogs13
->duration_nsec
);
7731 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s reply has %"PRIu32
" leftover bytes at end",
7732 ofpraw_get_name(raw
), msg
->size
);
7733 return OFPERR_OFPBRC_BAD_LEN
;
7735 length
= ntohs(ogs11
->length
);
7736 if (length
< sizeof base_len
) {
7737 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s reply claims invalid length %"PRIuSIZE
,
7738 ofpraw_get_name(raw
), length
);
7739 return OFPERR_OFPBRC_BAD_LEN
;
7742 gs
->group_id
= ntohl(ogs11
->group_id
);
7743 gs
->ref_count
= ntohl(ogs11
->ref_count
);
7744 gs
->packet_count
= ntohll(ogs11
->packet_count
);
7745 gs
->byte_count
= ntohll(ogs11
->byte_count
);
7747 gs
->n_buckets
= (length
- base_len
) / sizeof *obc
;
7748 obc
= ofpbuf_try_pull(msg
, gs
->n_buckets
* sizeof *obc
);
7750 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s reply has %"PRIu32
" leftover bytes at end",
7751 ofpraw_get_name(raw
), msg
->size
);
7752 return OFPERR_OFPBRC_BAD_LEN
;
7755 gs
->bucket_stats
= xmalloc(gs
->n_buckets
* sizeof *gs
->bucket_stats
);
7756 for (i
= 0; i
< gs
->n_buckets
; i
++) {
7757 gs
->bucket_stats
[i
].packet_count
= ntohll(obc
[i
].packet_count
);
7758 gs
->bucket_stats
[i
].byte_count
= ntohll(obc
[i
].byte_count
);
7765 ofputil_put_ofp11_bucket(const struct ofputil_bucket
*bucket
,
7766 struct ofpbuf
*openflow
, enum ofp_version ofp_version
)
7768 struct ofp11_bucket
*ob
;
7771 start
= openflow
->size
;
7772 ofpbuf_put_zeros(openflow
, sizeof *ob
);
7773 ofpacts_put_openflow_actions(bucket
->ofpacts
, bucket
->ofpacts_len
,
7774 openflow
, ofp_version
);
7775 ob
= ofpbuf_at_assert(openflow
, start
, sizeof *ob
);
7776 ob
->len
= htons(openflow
->size
- start
);
7777 ob
->weight
= htons(bucket
->weight
);
7778 ob
->watch_port
= ofputil_port_to_ofp11(bucket
->watch_port
);
7779 ob
->watch_group
= htonl(bucket
->watch_group
);
7783 ofputil_put_ofp15_bucket(const struct ofputil_bucket
*bucket
,
7784 uint32_t bucket_id
, enum ofp11_group_type group_type
,
7785 struct ofpbuf
*openflow
, enum ofp_version ofp_version
)
7787 struct ofp15_bucket
*ob
;
7788 size_t start
, actions_start
, actions_len
;
7790 start
= openflow
->size
;
7791 ofpbuf_put_zeros(openflow
, sizeof *ob
);
7793 actions_start
= openflow
->size
;
7794 ofpacts_put_openflow_actions(bucket
->ofpacts
, bucket
->ofpacts_len
,
7795 openflow
, ofp_version
);
7796 actions_len
= openflow
->size
- actions_start
;
7798 if (group_type
== OFPGT11_SELECT
) {
7799 ofpprop_put_u16(openflow
, OFPGBPT15_WEIGHT
, bucket
->weight
);
7801 if (bucket
->watch_port
!= OFPP_ANY
) {
7802 ofpprop_put_be32(openflow
, OFPGBPT15_WATCH_PORT
,
7803 ofputil_port_to_ofp11(bucket
->watch_port
));
7805 if (bucket
->watch_group
!= OFPG_ANY
) {
7806 ofpprop_put_u32(openflow
, OFPGBPT15_WATCH_GROUP
, bucket
->watch_group
);
7809 ob
= ofpbuf_at_assert(openflow
, start
, sizeof *ob
);
7810 ob
->len
= htons(openflow
->size
- start
);
7811 ob
->action_array_len
= htons(actions_len
);
7812 ob
->bucket_id
= htonl(bucket_id
);
7816 ofputil_put_group_prop_ntr_selection_method(enum ofp_version ofp_version
,
7817 const struct ofputil_group_props
*gp
,
7818 struct ofpbuf
*openflow
)
7820 struct ntr_group_prop_selection_method
*prop
;
7823 start
= openflow
->size
;
7824 ofpbuf_put_zeros(openflow
, sizeof *prop
);
7825 oxm_put_field_array(openflow
, &gp
->fields
, ofp_version
);
7826 prop
= ofpbuf_at_assert(openflow
, start
, sizeof *prop
);
7827 prop
->type
= htons(OFPGPT15_EXPERIMENTER
);
7828 prop
->experimenter
= htonl(NTR_VENDOR_ID
);
7829 prop
->exp_type
= htonl(NTRT_SELECTION_METHOD
);
7830 strcpy(prop
->selection_method
, gp
->selection_method
);
7831 prop
->selection_method_param
= htonll(gp
->selection_method_param
);
7832 ofpprop_end(openflow
, start
);
7836 ofputil_append_ofp11_group_desc_reply(const struct ofputil_group_desc
*gds
,
7837 const struct ovs_list
*buckets
,
7838 struct ovs_list
*replies
,
7839 enum ofp_version version
)
7841 struct ofpbuf
*reply
= ofpbuf_from_list(list_back(replies
));
7842 struct ofp11_group_desc_stats
*ogds
;
7843 struct ofputil_bucket
*bucket
;
7846 start_ogds
= reply
->size
;
7847 ofpbuf_put_zeros(reply
, sizeof *ogds
);
7848 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
7849 ofputil_put_ofp11_bucket(bucket
, reply
, version
);
7851 ogds
= ofpbuf_at_assert(reply
, start_ogds
, sizeof *ogds
);
7852 ogds
->length
= htons(reply
->size
- start_ogds
);
7853 ogds
->type
= gds
->type
;
7854 ogds
->group_id
= htonl(gds
->group_id
);
7856 ofpmp_postappend(replies
, start_ogds
);
7860 ofputil_append_ofp15_group_desc_reply(const struct ofputil_group_desc
*gds
,
7861 const struct ovs_list
*buckets
,
7862 struct ovs_list
*replies
,
7863 enum ofp_version version
)
7865 struct ofpbuf
*reply
= ofpbuf_from_list(list_back(replies
));
7866 struct ofp15_group_desc_stats
*ogds
;
7867 struct ofputil_bucket
*bucket
;
7868 size_t start_ogds
, start_buckets
;
7870 start_ogds
= reply
->size
;
7871 ofpbuf_put_zeros(reply
, sizeof *ogds
);
7872 start_buckets
= reply
->size
;
7873 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
7874 ofputil_put_ofp15_bucket(bucket
, bucket
->bucket_id
,
7875 gds
->type
, reply
, version
);
7877 ogds
= ofpbuf_at_assert(reply
, start_ogds
, sizeof *ogds
);
7878 ogds
->type
= gds
->type
;
7879 ogds
->group_id
= htonl(gds
->group_id
);
7880 ogds
->bucket_list_len
= htons(reply
->size
- start_buckets
);
7882 /* Add group properties */
7883 if (gds
->props
.selection_method
[0]) {
7884 ofputil_put_group_prop_ntr_selection_method(version
, &gds
->props
,
7887 ogds
->length
= htons(reply
->size
- start_ogds
);
7889 ofpmp_postappend(replies
, start_ogds
);
7892 /* Appends a group stats reply that contains the data in 'gds' to those already
7893 * present in the list of ofpbufs in 'replies'. 'replies' should have been
7894 * initialized with ofpmp_init(). */
7896 ofputil_append_group_desc_reply(const struct ofputil_group_desc
*gds
,
7897 const struct ovs_list
*buckets
,
7898 struct ovs_list
*replies
)
7900 enum ofp_version version
= ofpmp_version(replies
);
7908 ofputil_append_ofp11_group_desc_reply(gds
, buckets
, replies
, version
);
7912 ofputil_append_ofp15_group_desc_reply(gds
, buckets
, replies
, version
);
7922 ofputil_pull_ofp11_buckets(struct ofpbuf
*msg
, size_t buckets_length
,
7923 enum ofp_version version
, struct ovs_list
*buckets
)
7925 struct ofp11_bucket
*ob
;
7926 uint32_t bucket_id
= 0;
7929 while (buckets_length
> 0) {
7930 struct ofputil_bucket
*bucket
;
7931 struct ofpbuf ofpacts
;
7935 ob
= (buckets_length
>= sizeof *ob
7936 ? ofpbuf_try_pull(msg
, sizeof *ob
)
7939 VLOG_WARN_RL(&bad_ofmsg_rl
, "buckets end with %"PRIuSIZE
" leftover bytes",
7941 return OFPERR_OFPGMFC_BAD_BUCKET
;
7944 ob_len
= ntohs(ob
->len
);
7945 if (ob_len
< sizeof *ob
) {
7946 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket length "
7947 "%"PRIuSIZE
" is not valid", ob_len
);
7948 return OFPERR_OFPGMFC_BAD_BUCKET
;
7949 } else if (ob_len
> buckets_length
) {
7950 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket length "
7951 "%"PRIuSIZE
" exceeds remaining buckets data size %"PRIuSIZE
,
7952 ob_len
, buckets_length
);
7953 return OFPERR_OFPGMFC_BAD_BUCKET
;
7955 buckets_length
-= ob_len
;
7957 ofpbuf_init(&ofpacts
, 0);
7958 error
= ofpacts_pull_openflow_actions(msg
, ob_len
- sizeof *ob
,
7961 ofpbuf_uninit(&ofpacts
);
7962 ofputil_bucket_list_destroy(buckets
);
7966 bucket
= xzalloc(sizeof *bucket
);
7967 bucket
->weight
= ntohs(ob
->weight
);
7968 error
= ofputil_port_from_ofp11(ob
->watch_port
, &bucket
->watch_port
);
7970 ofpbuf_uninit(&ofpacts
);
7971 ofputil_bucket_list_destroy(buckets
);
7972 return OFPERR_OFPGMFC_BAD_WATCH
;
7974 bucket
->watch_group
= ntohl(ob
->watch_group
);
7975 bucket
->bucket_id
= bucket_id
++;
7977 bucket
->ofpacts
= ofpbuf_steal_data(&ofpacts
);
7978 bucket
->ofpacts_len
= ofpacts
.size
;
7979 list_push_back(buckets
, &bucket
->list_node
);
7986 ofputil_pull_ofp15_buckets(struct ofpbuf
*msg
, size_t buckets_length
,
7987 enum ofp_version version
, uint8_t group_type
,
7988 struct ovs_list
*buckets
)
7990 struct ofp15_bucket
*ob
;
7993 while (buckets_length
> 0) {
7994 struct ofputil_bucket
*bucket
= NULL
;
7995 struct ofpbuf ofpacts
;
7996 enum ofperr err
= OFPERR_OFPGMFC_BAD_BUCKET
;
7997 struct ofpbuf properties
;
7998 size_t ob_len
, actions_len
, properties_len
;
7999 ovs_be32 watch_port
= ofputil_port_to_ofp11(OFPP_ANY
);
8000 ovs_be32 watch_group
= htonl(OFPG_ANY
);
8001 ovs_be16 weight
= htons(group_type
== OFPGT11_SELECT
? 1 : 0);
8003 ofpbuf_init(&ofpacts
, 0);
8005 ob
= ofpbuf_try_pull(msg
, sizeof *ob
);
8007 VLOG_WARN_RL(&bad_ofmsg_rl
, "buckets end with %"PRIuSIZE
8008 " leftover bytes", buckets_length
);
8012 ob_len
= ntohs(ob
->len
);
8013 actions_len
= ntohs(ob
->action_array_len
);
8015 if (ob_len
< sizeof *ob
) {
8016 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket length "
8017 "%"PRIuSIZE
" is not valid", ob_len
);
8019 } else if (ob_len
> buckets_length
) {
8020 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket length "
8021 "%"PRIuSIZE
" exceeds remaining buckets data size %"
8022 PRIuSIZE
, ob_len
, buckets_length
);
8024 } else if (actions_len
> ob_len
- sizeof *ob
) {
8025 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket actions "
8026 "length %"PRIuSIZE
" exceeds remaining bucket "
8027 "data size %"PRIuSIZE
, actions_len
,
8028 ob_len
- sizeof *ob
);
8031 buckets_length
-= ob_len
;
8033 err
= ofpacts_pull_openflow_actions(msg
, actions_len
, version
,
8039 properties_len
= ob_len
- sizeof *ob
- actions_len
;
8040 ofpbuf_use_const(&properties
, ofpbuf_pull(msg
, properties_len
),
8043 while (properties
.size
> 0) {
8044 struct ofpbuf payload
;
8047 err
= ofpprop_pull(&properties
, &payload
, &type
);
8053 case OFPGBPT15_WEIGHT
:
8054 err
= ofpprop_parse_be16(&payload
, &weight
);
8057 case OFPGBPT15_WATCH_PORT
:
8058 err
= ofpprop_parse_be32(&payload
, &watch_port
);
8061 case OFPGBPT15_WATCH_GROUP
:
8062 err
= ofpprop_parse_be32(&payload
, &watch_group
);
8066 err
= OFPPROP_UNKNOWN(false, "group bucket", type
);
8075 bucket
= xzalloc(sizeof *bucket
);
8077 bucket
->weight
= ntohs(weight
);
8078 err
= ofputil_port_from_ofp11(watch_port
, &bucket
->watch_port
);
8080 err
= OFPERR_OFPGMFC_BAD_WATCH
;
8083 bucket
->watch_group
= ntohl(watch_group
);
8084 bucket
->bucket_id
= ntohl(ob
->bucket_id
);
8085 if (bucket
->bucket_id
> OFPG15_BUCKET_MAX
) {
8086 VLOG_WARN_RL(&bad_ofmsg_rl
, "bucket id (%u) is out of range",
8088 err
= OFPERR_OFPGMFC_BAD_BUCKET
;
8092 bucket
->ofpacts
= ofpbuf_steal_data(&ofpacts
);
8093 bucket
->ofpacts_len
= ofpacts
.size
;
8094 list_push_back(buckets
, &bucket
->list_node
);
8100 ofpbuf_uninit(&ofpacts
);
8101 ofputil_bucket_list_destroy(buckets
);
8105 if (ofputil_bucket_check_duplicate_id(buckets
)) {
8106 VLOG_WARN_RL(&bad_ofmsg_rl
, "Duplicate bucket id");
8107 ofputil_bucket_list_destroy(buckets
);
8108 return OFPERR_OFPGMFC_BAD_BUCKET
;
8115 ofputil_init_group_properties(struct ofputil_group_props
*gp
)
8117 memset(gp
, 0, sizeof *gp
);
8121 parse_group_prop_ntr_selection_method(struct ofpbuf
*payload
,
8122 enum ofp11_group_type group_type
,
8123 enum ofp15_group_mod_command group_cmd
,
8124 struct ofputil_group_props
*gp
)
8126 struct ntr_group_prop_selection_method
*prop
= payload
->data
;
8127 size_t fields_len
, method_len
;
8130 switch (group_type
) {
8131 case OFPGT11_SELECT
:
8134 case OFPGT11_INDIRECT
:
8136 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method property is "
8137 "only allowed for select groups");
8138 return OFPERR_OFPBPC_BAD_VALUE
;
8143 switch (group_cmd
) {
8145 case OFPGC15_MODIFY
:
8147 case OFPGC15_DELETE
:
8148 case OFPGC15_INSERT_BUCKET
:
8149 case OFPGC15_REMOVE_BUCKET
:
8150 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method property is "
8151 "only allowed for add and delete group modifications");
8152 return OFPERR_OFPBPC_BAD_VALUE
;
8157 if (payload
->size
< sizeof *prop
) {
8158 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method property "
8159 "length %u is not valid", payload
->size
);
8160 return OFPERR_OFPBPC_BAD_LEN
;
8163 method_len
= strnlen(prop
->selection_method
, NTR_MAX_SELECTION_METHOD_LEN
);
8165 if (method_len
== NTR_MAX_SELECTION_METHOD_LEN
) {
8166 OFPPROP_LOG(&bad_ofmsg_rl
, false,
8167 "ntr selection method is not null terminated");
8168 return OFPERR_OFPBPC_BAD_VALUE
;
8171 if (strcmp("hash", prop
->selection_method
)) {
8172 OFPPROP_LOG(&bad_ofmsg_rl
, false,
8173 "ntr selection method '%s' is not supported",
8174 prop
->selection_method
);
8175 return OFPERR_OFPBPC_BAD_VALUE
;
8178 strcpy(gp
->selection_method
, prop
->selection_method
);
8179 gp
->selection_method_param
= ntohll(prop
->selection_method_param
);
8181 if (!method_len
&& gp
->selection_method_param
) {
8182 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method parameter is "
8183 "non-zero but selection method is empty");
8184 return OFPERR_OFPBPC_BAD_VALUE
;
8187 ofpbuf_pull(payload
, sizeof *prop
);
8189 fields_len
= ntohs(prop
->length
) - sizeof *prop
;
8190 if (!method_len
&& fields_len
) {
8191 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method parameter is "
8192 "zero but fields are provided");
8193 return OFPERR_OFPBPC_BAD_VALUE
;
8196 error
= oxm_pull_field_array(payload
->data
, fields_len
,
8199 OFPPROP_LOG(&bad_ofmsg_rl
, false,
8200 "ntr selection method fields are invalid");
8208 parse_ofp15_group_properties(struct ofpbuf
*msg
,
8209 enum ofp11_group_type group_type
,
8210 enum ofp15_group_mod_command group_cmd
,
8211 struct ofputil_group_props
*gp
,
8212 size_t properties_len
)
8214 struct ofpbuf properties
;
8216 ofpbuf_use_const(&properties
, ofpbuf_pull(msg
, properties_len
),
8219 while (properties
.size
> 0) {
8220 struct ofpbuf payload
;
8224 error
= ofpprop_pull(&properties
, &payload
, &type
);
8230 case OFPPROP_EXP(NTR_VENDOR_ID
, NTRT_SELECTION_METHOD
):
8231 case OFPPROP_EXP(NTR_COMPAT_VENDOR_ID
, NTRT_SELECTION_METHOD
):
8232 error
= parse_group_prop_ntr_selection_method(&payload
, group_type
,
8237 error
= OFPPROP_UNKNOWN(false, "group", type
);
8250 ofputil_decode_ofp11_group_desc_reply(struct ofputil_group_desc
*gd
,
8252 enum ofp_version version
)
8254 struct ofp11_group_desc_stats
*ogds
;
8258 ofpraw_pull_assert(msg
);
8265 ogds
= ofpbuf_try_pull(msg
, sizeof *ogds
);
8267 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply has %"PRIu32
" "
8268 "leftover bytes at end", msg
->size
);
8269 return OFPERR_OFPBRC_BAD_LEN
;
8271 gd
->type
= ogds
->type
;
8272 gd
->group_id
= ntohl(ogds
->group_id
);
8274 length
= ntohs(ogds
->length
);
8275 if (length
< sizeof *ogds
|| length
- sizeof *ogds
> msg
->size
) {
8276 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply claims invalid "
8277 "length %"PRIuSIZE
, length
);
8278 return OFPERR_OFPBRC_BAD_LEN
;
8281 return ofputil_pull_ofp11_buckets(msg
, length
- sizeof *ogds
, version
,
8286 ofputil_decode_ofp15_group_desc_reply(struct ofputil_group_desc
*gd
,
8288 enum ofp_version version
)
8290 struct ofp15_group_desc_stats
*ogds
;
8291 uint16_t length
, bucket_list_len
;
8295 ofpraw_pull_assert(msg
);
8302 ogds
= ofpbuf_try_pull(msg
, sizeof *ogds
);
8304 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply has %"PRIu32
" "
8305 "leftover bytes at end", msg
->size
);
8306 return OFPERR_OFPBRC_BAD_LEN
;
8308 gd
->type
= ogds
->type
;
8309 gd
->group_id
= ntohl(ogds
->group_id
);
8311 length
= ntohs(ogds
->length
);
8312 if (length
< sizeof *ogds
|| length
- sizeof *ogds
> msg
->size
) {
8313 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply claims invalid "
8314 "length %u", length
);
8315 return OFPERR_OFPBRC_BAD_LEN
;
8318 bucket_list_len
= ntohs(ogds
->bucket_list_len
);
8319 if (length
< bucket_list_len
+ sizeof *ogds
) {
8320 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply claims invalid "
8321 "bucket list length %u", bucket_list_len
);
8322 return OFPERR_OFPBRC_BAD_LEN
;
8324 error
= ofputil_pull_ofp15_buckets(msg
, bucket_list_len
, version
, gd
->type
,
8330 /* By definition group desc messages don't have a group mod command.
8331 * However, parse_group_prop_ntr_selection_method() checks to make sure
8332 * that the command is OFPGC15_ADD or OFPGC15_DELETE to guard
8333 * against group mod messages with other commands supplying
8334 * a NTR selection method group experimenter property.
8335 * Such properties are valid for group desc replies so
8336 * claim that the group mod command is OFPGC15_ADD to
8337 * satisfy the check in parse_group_prop_ntr_selection_method() */
8338 return parse_ofp15_group_properties(msg
, gd
->type
, OFPGC15_ADD
, &gd
->props
,
8339 length
- sizeof *ogds
- bucket_list_len
);
8342 /* Converts a group description reply in 'msg' into an abstract
8343 * ofputil_group_desc in 'gd'.
8345 * Multiple group description replies can be packed into a single OpenFlow
8346 * message. Calling this function multiple times for a single 'msg' iterates
8347 * through the replies. The caller must initially leave 'msg''s layer pointers
8348 * null and not modify them between calls.
8350 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8351 * otherwise a positive errno value. */
8353 ofputil_decode_group_desc_reply(struct ofputil_group_desc
*gd
,
8354 struct ofpbuf
*msg
, enum ofp_version version
)
8356 ofputil_init_group_properties(&gd
->props
);
8364 return ofputil_decode_ofp11_group_desc_reply(gd
, msg
, version
);
8367 return ofputil_decode_ofp15_group_desc_reply(gd
, msg
, version
);
8376 ofputil_uninit_group_mod(struct ofputil_group_mod
*gm
)
8378 ofputil_bucket_list_destroy(&gm
->buckets
);
8381 static struct ofpbuf
*
8382 ofputil_encode_ofp11_group_mod(enum ofp_version ofp_version
,
8383 const struct ofputil_group_mod
*gm
)
8386 struct ofp11_group_mod
*ogm
;
8388 struct ofputil_bucket
*bucket
;
8390 b
= ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD
, ofp_version
, 0);
8391 start_ogm
= b
->size
;
8392 ofpbuf_put_zeros(b
, sizeof *ogm
);
8394 LIST_FOR_EACH (bucket
, list_node
, &gm
->buckets
) {
8395 ofputil_put_ofp11_bucket(bucket
, b
, ofp_version
);
8397 ogm
= ofpbuf_at_assert(b
, start_ogm
, sizeof *ogm
);
8398 ogm
->command
= htons(gm
->command
);
8399 ogm
->type
= gm
->type
;
8400 ogm
->group_id
= htonl(gm
->group_id
);
8405 static struct ofpbuf
*
8406 ofputil_encode_ofp15_group_mod(enum ofp_version ofp_version
,
8407 const struct ofputil_group_mod
*gm
)
8410 struct ofp15_group_mod
*ogm
;
8412 struct ofputil_bucket
*bucket
;
8413 struct id_pool
*bucket_ids
= NULL
;
8415 b
= ofpraw_alloc(OFPRAW_OFPT15_GROUP_MOD
, ofp_version
, 0);
8416 start_ogm
= b
->size
;
8417 ofpbuf_put_zeros(b
, sizeof *ogm
);
8419 LIST_FOR_EACH (bucket
, list_node
, &gm
->buckets
) {
8422 /* Generate a bucket id if none was supplied */
8423 if (bucket
->bucket_id
> OFPG15_BUCKET_MAX
) {
8425 const struct ofputil_bucket
*bkt
;
8427 bucket_ids
= id_pool_create(0, OFPG15_BUCKET_MAX
+ 1);
8429 /* Mark all bucket_ids that are present in gm
8430 * as used in the pool. */
8431 LIST_FOR_EACH_REVERSE (bkt
, list_node
, &gm
->buckets
) {
8432 if (bkt
== bucket
) {
8435 if (bkt
->bucket_id
<= OFPG15_BUCKET_MAX
) {
8436 id_pool_add(bucket_ids
, bkt
->bucket_id
);
8441 if (!id_pool_alloc_id(bucket_ids
, &bucket_id
)) {
8445 bucket_id
= bucket
->bucket_id
;
8448 ofputil_put_ofp15_bucket(bucket
, bucket_id
, gm
->type
, b
, ofp_version
);
8450 ogm
= ofpbuf_at_assert(b
, start_ogm
, sizeof *ogm
);
8451 ogm
->command
= htons(gm
->command
);
8452 ogm
->type
= gm
->type
;
8453 ogm
->group_id
= htonl(gm
->group_id
);
8454 ogm
->command_bucket_id
= htonl(gm
->command_bucket_id
);
8455 ogm
->bucket_array_len
= htons(b
->size
- start_ogm
- sizeof *ogm
);
8457 /* Add group properties */
8458 if (gm
->props
.selection_method
[0]) {
8459 ofputil_put_group_prop_ntr_selection_method(ofp_version
, &gm
->props
, b
);
8462 id_pool_destroy(bucket_ids
);
8467 bad_group_cmd(enum ofp15_group_mod_command cmd
)
8469 const char *opt_version
;
8470 const char *version
;
8471 const char *cmd_str
;
8475 case OFPGC15_MODIFY
:
8476 case OFPGC15_DELETE
:
8481 case OFPGC15_INSERT_BUCKET
:
8482 case OFPGC15_REMOVE_BUCKET
:
8493 cmd_str
= "add-group";
8496 case OFPGC15_MODIFY
:
8497 cmd_str
= "mod-group";
8500 case OFPGC15_DELETE
:
8501 cmd_str
= "del-group";
8504 case OFPGC15_INSERT_BUCKET
:
8505 cmd_str
= "insert-bucket";
8508 case OFPGC15_REMOVE_BUCKET
:
8509 cmd_str
= "remove-bucket";
8516 ovs_fatal(0, "%s needs OpenFlow %s or later (\'-O OpenFlow%s\')",
8517 cmd_str
, version
, opt_version
);
8521 /* Converts abstract group mod 'gm' into a message for OpenFlow version
8522 * 'ofp_version' and returns the message. */
8524 ofputil_encode_group_mod(enum ofp_version ofp_version
,
8525 const struct ofputil_group_mod
*gm
)
8528 switch (ofp_version
) {
8530 bad_group_cmd(gm
->command
);
8536 if (gm
->command
> OFPGC11_DELETE
) {
8537 bad_group_cmd(gm
->command
);
8539 return ofputil_encode_ofp11_group_mod(ofp_version
, gm
);
8542 return ofputil_encode_ofp15_group_mod(ofp_version
, gm
);
8550 ofputil_pull_ofp11_group_mod(struct ofpbuf
*msg
, enum ofp_version ofp_version
,
8551 struct ofputil_group_mod
*gm
)
8553 const struct ofp11_group_mod
*ogm
;
8556 ogm
= ofpbuf_pull(msg
, sizeof *ogm
);
8557 gm
->command
= ntohs(ogm
->command
);
8558 gm
->type
= ogm
->type
;
8559 gm
->group_id
= ntohl(ogm
->group_id
);
8560 gm
->command_bucket_id
= OFPG15_BUCKET_ALL
;
8562 error
= ofputil_pull_ofp11_buckets(msg
, msg
->size
, ofp_version
,
8565 /* OF1.3.5+ prescribes an error when an OFPGC_DELETE includes buckets. */
8567 && ofp_version
>= OFP13_VERSION
8568 && gm
->command
== OFPGC11_DELETE
8569 && !list_is_empty(&gm
->buckets
)) {
8570 error
= OFPERR_OFPGMFC_INVALID_GROUP
;
8577 ofputil_pull_ofp15_group_mod(struct ofpbuf
*msg
, enum ofp_version ofp_version
,
8578 struct ofputil_group_mod
*gm
)
8580 const struct ofp15_group_mod
*ogm
;
8581 uint16_t bucket_list_len
;
8582 enum ofperr error
= OFPERR_OFPGMFC_BAD_BUCKET
;
8584 ogm
= ofpbuf_pull(msg
, sizeof *ogm
);
8585 gm
->command
= ntohs(ogm
->command
);
8586 gm
->type
= ogm
->type
;
8587 gm
->group_id
= ntohl(ogm
->group_id
);
8589 gm
->command_bucket_id
= ntohl(ogm
->command_bucket_id
);
8590 switch (gm
->command
) {
8591 case OFPGC15_REMOVE_BUCKET
:
8592 if (gm
->command_bucket_id
== OFPG15_BUCKET_ALL
) {
8596 case OFPGC15_INSERT_BUCKET
:
8597 if (gm
->command_bucket_id
<= OFPG15_BUCKET_MAX
||
8598 gm
->command_bucket_id
== OFPG15_BUCKET_FIRST
8599 || gm
->command_bucket_id
== OFPG15_BUCKET_LAST
) {
8605 case OFPGC11_MODIFY
:
8606 case OFPGC11_DELETE
:
8608 if (gm
->command_bucket_id
== OFPG15_BUCKET_ALL
) {
8614 VLOG_WARN_RL(&bad_ofmsg_rl
,
8615 "group command bucket id (%u) is out of range",
8616 gm
->command_bucket_id
);
8617 return OFPERR_OFPGMFC_BAD_BUCKET
;
8620 bucket_list_len
= ntohs(ogm
->bucket_array_len
);
8621 error
= ofputil_pull_ofp15_buckets(msg
, bucket_list_len
, ofp_version
,
8622 gm
->type
, &gm
->buckets
);
8627 return parse_ofp15_group_properties(msg
, gm
->type
, gm
->command
, &gm
->props
,
8631 /* Converts OpenFlow group mod message 'oh' into an abstract group mod in
8632 * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */
8634 ofputil_decode_group_mod(const struct ofp_header
*oh
,
8635 struct ofputil_group_mod
*gm
)
8637 enum ofp_version ofp_version
= oh
->version
;
8639 struct ofputil_bucket
*bucket
;
8642 ofpbuf_use_const(&msg
, oh
, ntohs(oh
->length
));
8643 ofpraw_pull_assert(&msg
);
8645 ofputil_init_group_properties(&gm
->props
);
8647 switch (ofp_version
)
8653 err
= ofputil_pull_ofp11_group_mod(&msg
, ofp_version
, gm
);
8657 err
= ofputil_pull_ofp15_group_mod(&msg
, ofp_version
, gm
);
8670 case OFPGT11_INDIRECT
:
8671 if (!list_is_singleton(&gm
->buckets
)) {
8672 return OFPERR_OFPGMFC_INVALID_GROUP
;
8676 case OFPGT11_SELECT
:
8680 return OFPERR_OFPGMFC_BAD_TYPE
;
8683 switch (gm
->command
) {
8685 case OFPGC11_MODIFY
:
8686 case OFPGC11_DELETE
:
8687 case OFPGC15_INSERT_BUCKET
:
8689 case OFPGC15_REMOVE_BUCKET
:
8690 if (!list_is_empty(&gm
->buckets
)) {
8691 return OFPERR_OFPGMFC_BAD_BUCKET
;
8695 return OFPERR_OFPGMFC_BAD_COMMAND
;
8698 LIST_FOR_EACH (bucket
, list_node
, &gm
->buckets
) {
8699 if (bucket
->weight
&& gm
->type
!= OFPGT11_SELECT
) {
8700 return OFPERR_OFPGMFC_INVALID_GROUP
;
8705 case OFPGT11_INDIRECT
:
8706 if (ofputil_bucket_has_liveness(bucket
)) {
8707 return OFPERR_OFPGMFC_WATCH_UNSUPPORTED
;
8710 case OFPGT11_SELECT
:
8713 if (!ofputil_bucket_has_liveness(bucket
)) {
8714 return OFPERR_OFPGMFC_INVALID_GROUP
;
8718 /* Returning BAD TYPE to be consistent
8719 * though gm->type has been checked already. */
8720 return OFPERR_OFPGMFC_BAD_TYPE
;
8727 /* Parse a queue status request message into 'oqsr'.
8728 * Returns 0 if successful, otherwise an OFPERR_* number. */
8730 ofputil_decode_queue_stats_request(const struct ofp_header
*request
,
8731 struct ofputil_queue_stats_request
*oqsr
)
8733 switch ((enum ofp_version
)request
->version
) {
8738 case OFP11_VERSION
: {
8739 const struct ofp11_queue_stats_request
*qsr11
= ofpmsg_body(request
);
8740 oqsr
->queue_id
= ntohl(qsr11
->queue_id
);
8741 return ofputil_port_from_ofp11(qsr11
->port_no
, &oqsr
->port_no
);
8744 case OFP10_VERSION
: {
8745 const struct ofp10_queue_stats_request
*qsr10
= ofpmsg_body(request
);
8746 oqsr
->queue_id
= ntohl(qsr10
->queue_id
);
8747 oqsr
->port_no
= u16_to_ofp(ntohs(qsr10
->port_no
));
8748 /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
8749 if (oqsr
->port_no
== OFPP_ALL
) {
8750 oqsr
->port_no
= OFPP_ANY
;
8760 /* Encode a queue stats request for 'oqsr', the encoded message
8761 * will be for OpenFlow version 'ofp_version'. Returns message
8762 * as a struct ofpbuf. Returns encoded message on success, NULL on error. */
8764 ofputil_encode_queue_stats_request(enum ofp_version ofp_version
,
8765 const struct ofputil_queue_stats_request
*oqsr
)
8767 struct ofpbuf
*request
;
8769 switch (ofp_version
) {
8774 case OFP15_VERSION
: {
8775 struct ofp11_queue_stats_request
*req
;
8776 request
= ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST
, ofp_version
, 0);
8777 req
= ofpbuf_put_zeros(request
, sizeof *req
);
8778 req
->port_no
= ofputil_port_to_ofp11(oqsr
->port_no
);
8779 req
->queue_id
= htonl(oqsr
->queue_id
);
8782 case OFP10_VERSION
: {
8783 struct ofp10_queue_stats_request
*req
;
8784 request
= ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST
, ofp_version
, 0);
8785 req
= ofpbuf_put_zeros(request
, sizeof *req
);
8786 /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
8787 req
->port_no
= htons(ofp_to_u16(oqsr
->port_no
== OFPP_ANY
8788 ? OFPP_ALL
: oqsr
->port_no
));
8789 req
->queue_id
= htonl(oqsr
->queue_id
);
8799 /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
8802 ofputil_count_queue_stats(const struct ofp_header
*oh
)
8804 struct ofputil_queue_stats qs
;
8808 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
8809 ofpraw_pull_assert(&b
);
8810 while (!ofputil_decode_queue_stats(&qs
, &b
)) {
8817 ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats
*oqs
,
8818 const struct ofp10_queue_stats
*qs10
)
8820 oqs
->port_no
= u16_to_ofp(ntohs(qs10
->port_no
));
8821 oqs
->queue_id
= ntohl(qs10
->queue_id
);
8822 oqs
->tx_bytes
= ntohll(get_32aligned_be64(&qs10
->tx_bytes
));
8823 oqs
->tx_packets
= ntohll(get_32aligned_be64(&qs10
->tx_packets
));
8824 oqs
->tx_errors
= ntohll(get_32aligned_be64(&qs10
->tx_errors
));
8825 oqs
->duration_sec
= oqs
->duration_nsec
= UINT32_MAX
;
8831 ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats
*oqs
,
8832 const struct ofp11_queue_stats
*qs11
)
8836 error
= ofputil_port_from_ofp11(qs11
->port_no
, &oqs
->port_no
);
8841 oqs
->queue_id
= ntohl(qs11
->queue_id
);
8842 oqs
->tx_bytes
= ntohll(qs11
->tx_bytes
);
8843 oqs
->tx_packets
= ntohll(qs11
->tx_packets
);
8844 oqs
->tx_errors
= ntohll(qs11
->tx_errors
);
8845 oqs
->duration_sec
= oqs
->duration_nsec
= UINT32_MAX
;
8851 ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats
*oqs
,
8852 const struct ofp13_queue_stats
*qs13
)
8854 enum ofperr error
= ofputil_queue_stats_from_ofp11(oqs
, &qs13
->qs
);
8856 oqs
->duration_sec
= ntohl(qs13
->duration_sec
);
8857 oqs
->duration_nsec
= ntohl(qs13
->duration_nsec
);
8864 ofputil_pull_ofp14_queue_stats(struct ofputil_queue_stats
*oqs
,
8867 const struct ofp14_queue_stats
*qs14
;
8870 qs14
= ofpbuf_try_pull(msg
, sizeof *qs14
);
8872 return OFPERR_OFPBRC_BAD_LEN
;
8875 len
= ntohs(qs14
->length
);
8876 if (len
< sizeof *qs14
|| len
- sizeof *qs14
> msg
->size
) {
8877 return OFPERR_OFPBRC_BAD_LEN
;
8879 ofpbuf_pull(msg
, len
- sizeof *qs14
);
8881 /* No properties yet defined, so ignore them for now. */
8883 return ofputil_queue_stats_from_ofp13(oqs
, &qs14
->qs
);
8886 /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
8887 * ofputil_queue_stats in 'qs'.
8889 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
8890 * message. Calling this function multiple times for a single 'msg' iterates
8891 * through the replies. The caller must initially leave 'msg''s layer pointers
8892 * null and not modify them between calls.
8894 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8895 * otherwise a positive errno value. */
8897 ofputil_decode_queue_stats(struct ofputil_queue_stats
*qs
, struct ofpbuf
*msg
)
8902 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
8903 : ofpraw_pull(&raw
, msg
));
8910 } else if (raw
== OFPRAW_OFPST14_QUEUE_REPLY
) {
8911 return ofputil_pull_ofp14_queue_stats(qs
, msg
);
8912 } else if (raw
== OFPRAW_OFPST13_QUEUE_REPLY
) {
8913 const struct ofp13_queue_stats
*qs13
;
8915 qs13
= ofpbuf_try_pull(msg
, sizeof *qs13
);
8919 return ofputil_queue_stats_from_ofp13(qs
, qs13
);
8920 } else if (raw
== OFPRAW_OFPST11_QUEUE_REPLY
) {
8921 const struct ofp11_queue_stats
*qs11
;
8923 qs11
= ofpbuf_try_pull(msg
, sizeof *qs11
);
8927 return ofputil_queue_stats_from_ofp11(qs
, qs11
);
8928 } else if (raw
== OFPRAW_OFPST10_QUEUE_REPLY
) {
8929 const struct ofp10_queue_stats
*qs10
;
8931 qs10
= ofpbuf_try_pull(msg
, sizeof *qs10
);
8935 return ofputil_queue_stats_from_ofp10(qs
, qs10
);
8941 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_QUEUE reply has %"PRIu32
" leftover "
8942 "bytes at end", msg
->size
);
8943 return OFPERR_OFPBRC_BAD_LEN
;
8947 ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats
*oqs
,
8948 struct ofp10_queue_stats
*qs10
)
8950 qs10
->port_no
= htons(ofp_to_u16(oqs
->port_no
));
8951 memset(qs10
->pad
, 0, sizeof qs10
->pad
);
8952 qs10
->queue_id
= htonl(oqs
->queue_id
);
8953 put_32aligned_be64(&qs10
->tx_bytes
, htonll(oqs
->tx_bytes
));
8954 put_32aligned_be64(&qs10
->tx_packets
, htonll(oqs
->tx_packets
));
8955 put_32aligned_be64(&qs10
->tx_errors
, htonll(oqs
->tx_errors
));
8959 ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats
*oqs
,
8960 struct ofp11_queue_stats
*qs11
)
8962 qs11
->port_no
= ofputil_port_to_ofp11(oqs
->port_no
);
8963 qs11
->queue_id
= htonl(oqs
->queue_id
);
8964 qs11
->tx_bytes
= htonll(oqs
->tx_bytes
);
8965 qs11
->tx_packets
= htonll(oqs
->tx_packets
);
8966 qs11
->tx_errors
= htonll(oqs
->tx_errors
);
8970 ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats
*oqs
,
8971 struct ofp13_queue_stats
*qs13
)
8973 ofputil_queue_stats_to_ofp11(oqs
, &qs13
->qs
);
8974 if (oqs
->duration_sec
!= UINT32_MAX
) {
8975 qs13
->duration_sec
= htonl(oqs
->duration_sec
);
8976 qs13
->duration_nsec
= htonl(oqs
->duration_nsec
);
8978 qs13
->duration_sec
= OVS_BE32_MAX
;
8979 qs13
->duration_nsec
= OVS_BE32_MAX
;
8984 ofputil_queue_stats_to_ofp14(const struct ofputil_queue_stats
*oqs
,
8985 struct ofp14_queue_stats
*qs14
)
8987 qs14
->length
= htons(sizeof *qs14
);
8988 memset(qs14
->pad
, 0, sizeof qs14
->pad
);
8989 ofputil_queue_stats_to_ofp13(oqs
, &qs14
->qs
);
8993 /* Encode a queue stat for 'oqs' and append it to 'replies'. */
8995 ofputil_append_queue_stat(struct ovs_list
*replies
,
8996 const struct ofputil_queue_stats
*oqs
)
8998 switch (ofpmp_version(replies
)) {
8999 case OFP13_VERSION
: {
9000 struct ofp13_queue_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
9001 ofputil_queue_stats_to_ofp13(oqs
, reply
);
9006 case OFP11_VERSION
: {
9007 struct ofp11_queue_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
9008 ofputil_queue_stats_to_ofp11(oqs
, reply
);
9012 case OFP10_VERSION
: {
9013 struct ofp10_queue_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
9014 ofputil_queue_stats_to_ofp10(oqs
, reply
);
9019 case OFP15_VERSION
: {
9020 struct ofp14_queue_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
9021 ofputil_queue_stats_to_ofp14(oqs
, reply
);
9031 ofputil_decode_bundle_ctrl(const struct ofp_header
*oh
,
9032 struct ofputil_bundle_ctrl_msg
*msg
)
9036 const struct ofp14_bundle_ctrl_msg
*m
;
9038 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
9039 raw
= ofpraw_pull_assert(&b
);
9040 ovs_assert(raw
== OFPRAW_OFPT14_BUNDLE_CONTROL
);
9043 msg
->bundle_id
= ntohl(m
->bundle_id
);
9044 msg
->type
= ntohs(m
->type
);
9045 msg
->flags
= ntohs(m
->flags
);
9051 ofputil_encode_bundle_ctrl_request(enum ofp_version ofp_version
,
9052 struct ofputil_bundle_ctrl_msg
*bc
)
9054 struct ofpbuf
*request
;
9055 struct ofp14_bundle_ctrl_msg
*m
;
9057 switch (ofp_version
) {
9062 ovs_fatal(0, "bundles need OpenFlow 1.4 or later "
9063 "(\'-O OpenFlow14\')");
9066 request
= ofpraw_alloc(OFPRAW_OFPT14_BUNDLE_CONTROL
, ofp_version
, 0);
9067 m
= ofpbuf_put_zeros(request
, sizeof *m
);
9069 m
->bundle_id
= htonl(bc
->bundle_id
);
9070 m
->type
= htons(bc
->type
);
9071 m
->flags
= htons(bc
->flags
);
9081 ofputil_encode_bundle_ctrl_reply(const struct ofp_header
*oh
,
9082 struct ofputil_bundle_ctrl_msg
*msg
)
9085 struct ofp14_bundle_ctrl_msg
*m
;
9087 buf
= ofpraw_alloc_reply(OFPRAW_OFPT14_BUNDLE_CONTROL
, oh
, 0);
9088 m
= ofpbuf_put_zeros(buf
, sizeof *m
);
9090 m
->bundle_id
= htonl(msg
->bundle_id
);
9091 m
->type
= htons(msg
->type
);
9092 m
->flags
= htons(msg
->flags
);
9097 /* Return true for bundlable state change requests, false for other messages.
9100 ofputil_is_bundlable(enum ofptype type
)
9103 /* Minimum required by OpenFlow 1.4. */
9104 case OFPTYPE_PORT_MOD
:
9105 case OFPTYPE_FLOW_MOD
:
9108 /* Nice to have later. */
9109 case OFPTYPE_FLOW_MOD_TABLE_ID
:
9110 case OFPTYPE_GROUP_MOD
:
9111 case OFPTYPE_TABLE_MOD
:
9112 case OFPTYPE_METER_MOD
:
9113 case OFPTYPE_PACKET_OUT
:
9114 case OFPTYPE_NXT_TLV_TABLE_MOD
:
9116 /* Not to be bundlable. */
9117 case OFPTYPE_ECHO_REQUEST
:
9118 case OFPTYPE_FEATURES_REQUEST
:
9119 case OFPTYPE_GET_CONFIG_REQUEST
:
9120 case OFPTYPE_SET_CONFIG
:
9121 case OFPTYPE_BARRIER_REQUEST
:
9122 case OFPTYPE_ROLE_REQUEST
:
9123 case OFPTYPE_ECHO_REPLY
:
9124 case OFPTYPE_SET_FLOW_FORMAT
:
9125 case OFPTYPE_SET_PACKET_IN_FORMAT
:
9126 case OFPTYPE_SET_CONTROLLER_ID
:
9127 case OFPTYPE_FLOW_AGE
:
9128 case OFPTYPE_FLOW_MONITOR_CANCEL
:
9129 case OFPTYPE_SET_ASYNC_CONFIG
:
9130 case OFPTYPE_GET_ASYNC_REQUEST
:
9131 case OFPTYPE_DESC_STATS_REQUEST
:
9132 case OFPTYPE_FLOW_STATS_REQUEST
:
9133 case OFPTYPE_AGGREGATE_STATS_REQUEST
:
9134 case OFPTYPE_TABLE_STATS_REQUEST
:
9135 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST
:
9136 case OFPTYPE_TABLE_DESC_REQUEST
:
9137 case OFPTYPE_PORT_STATS_REQUEST
:
9138 case OFPTYPE_QUEUE_STATS_REQUEST
:
9139 case OFPTYPE_PORT_DESC_STATS_REQUEST
:
9140 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST
:
9141 case OFPTYPE_METER_STATS_REQUEST
:
9142 case OFPTYPE_METER_CONFIG_STATS_REQUEST
:
9143 case OFPTYPE_METER_FEATURES_STATS_REQUEST
:
9144 case OFPTYPE_GROUP_STATS_REQUEST
:
9145 case OFPTYPE_GROUP_DESC_STATS_REQUEST
:
9146 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST
:
9147 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST
:
9148 case OFPTYPE_BUNDLE_CONTROL
:
9149 case OFPTYPE_BUNDLE_ADD_MESSAGE
:
9152 case OFPTYPE_FEATURES_REPLY
:
9153 case OFPTYPE_GET_CONFIG_REPLY
:
9154 case OFPTYPE_PACKET_IN
:
9155 case OFPTYPE_FLOW_REMOVED
:
9156 case OFPTYPE_PORT_STATUS
:
9157 case OFPTYPE_BARRIER_REPLY
:
9158 case OFPTYPE_QUEUE_GET_CONFIG_REPLY
:
9159 case OFPTYPE_DESC_STATS_REPLY
:
9160 case OFPTYPE_FLOW_STATS_REPLY
:
9161 case OFPTYPE_QUEUE_STATS_REPLY
:
9162 case OFPTYPE_PORT_STATS_REPLY
:
9163 case OFPTYPE_TABLE_STATS_REPLY
:
9164 case OFPTYPE_AGGREGATE_STATS_REPLY
:
9165 case OFPTYPE_PORT_DESC_STATS_REPLY
:
9166 case OFPTYPE_ROLE_REPLY
:
9167 case OFPTYPE_FLOW_MONITOR_PAUSED
:
9168 case OFPTYPE_FLOW_MONITOR_RESUMED
:
9169 case OFPTYPE_FLOW_MONITOR_STATS_REPLY
:
9170 case OFPTYPE_GET_ASYNC_REPLY
:
9171 case OFPTYPE_GROUP_STATS_REPLY
:
9172 case OFPTYPE_GROUP_DESC_STATS_REPLY
:
9173 case OFPTYPE_GROUP_FEATURES_STATS_REPLY
:
9174 case OFPTYPE_METER_STATS_REPLY
:
9175 case OFPTYPE_METER_CONFIG_STATS_REPLY
:
9176 case OFPTYPE_METER_FEATURES_STATS_REPLY
:
9177 case OFPTYPE_TABLE_FEATURES_STATS_REPLY
:
9178 case OFPTYPE_TABLE_DESC_REPLY
:
9179 case OFPTYPE_ROLE_STATUS
:
9180 case OFPTYPE_REQUESTFORWARD
:
9181 case OFPTYPE_NXT_TLV_TABLE_REQUEST
:
9182 case OFPTYPE_NXT_TLV_TABLE_REPLY
:
9190 ofputil_decode_bundle_add(const struct ofp_header
*oh
,
9191 struct ofputil_bundle_add_msg
*msg
,
9192 enum ofptype
*type_ptr
)
9194 const struct ofp14_bundle_ctrl_msg
*m
;
9201 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
9202 raw
= ofpraw_pull_assert(&b
);
9203 ovs_assert(raw
== OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE
);
9205 m
= ofpbuf_pull(&b
, sizeof *m
);
9206 msg
->bundle_id
= ntohl(m
->bundle_id
);
9207 msg
->flags
= ntohs(m
->flags
);
9210 if (msg
->msg
->version
!= oh
->version
) {
9211 return OFPERR_NXBFC_BAD_VERSION
;
9213 inner_len
= ntohs(msg
->msg
->length
);
9214 if (inner_len
< sizeof(struct ofp_header
) || inner_len
> b
.size
) {
9215 return OFPERR_OFPBFC_MSG_BAD_LEN
;
9217 if (msg
->msg
->xid
!= oh
->xid
) {
9218 return OFPERR_OFPBFC_MSG_BAD_XID
;
9221 /* Reject unbundlable messages. */
9225 error
= ofptype_decode(type_ptr
, msg
->msg
);
9227 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPT14_BUNDLE_ADD_MESSAGE contained "
9228 "message is unparsable (%s)", ofperr_get_name(error
));
9229 return OFPERR_OFPBFC_MSG_UNSUP
; /* 'error' would be confusing. */
9232 if (!ofputil_is_bundlable(*type_ptr
)) {
9233 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s message not allowed inside "
9234 "OFPT14_BUNDLE_ADD_MESSAGE", ofptype_get_name(*type_ptr
));
9235 return OFPERR_OFPBFC_MSG_UNSUP
;
9242 ofputil_encode_bundle_add(enum ofp_version ofp_version
,
9243 struct ofputil_bundle_add_msg
*msg
)
9245 struct ofpbuf
*request
;
9246 struct ofp14_bundle_ctrl_msg
*m
;
9248 /* Must use the same xid as the embedded message. */
9249 request
= ofpraw_alloc_xid(OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE
, ofp_version
,
9251 m
= ofpbuf_put_zeros(request
, sizeof *m
);
9253 m
->bundle_id
= htonl(msg
->bundle_id
);
9254 m
->flags
= htons(msg
->flags
);
9255 ofpbuf_put(request
, msg
->msg
, ntohs(msg
->msg
->length
));
9261 encode_tlv_table_mappings(struct ofpbuf
*b
, struct ovs_list
*mappings
)
9263 struct ofputil_tlv_map
*map
;
9265 LIST_FOR_EACH (map
, list_node
, mappings
) {
9266 struct nx_tlv_map
*nx_map
;
9268 nx_map
= ofpbuf_put_zeros(b
, sizeof *nx_map
);
9269 nx_map
->option_class
= htons(map
->option_class
);
9270 nx_map
->option_type
= map
->option_type
;
9271 nx_map
->option_len
= map
->option_len
;
9272 nx_map
->index
= htons(map
->index
);
9277 ofputil_encode_tlv_table_mod(enum ofp_version ofp_version
,
9278 struct ofputil_tlv_table_mod
*ttm
)
9281 struct nx_tlv_table_mod
*nx_ttm
;
9283 b
= ofpraw_alloc(OFPRAW_NXT_TLV_TABLE_MOD
, ofp_version
, 0);
9284 nx_ttm
= ofpbuf_put_zeros(b
, sizeof *nx_ttm
);
9285 nx_ttm
->command
= htons(ttm
->command
);
9286 encode_tlv_table_mappings(b
, &ttm
->mappings
);
9292 decode_tlv_table_mappings(struct ofpbuf
*msg
, unsigned int max_fields
,
9293 struct ovs_list
*mappings
)
9295 list_init(mappings
);
9298 struct nx_tlv_map
*nx_map
;
9299 struct ofputil_tlv_map
*map
;
9301 nx_map
= ofpbuf_pull(msg
, sizeof *nx_map
);
9302 map
= xmalloc(sizeof *map
);
9303 list_push_back(mappings
, &map
->list_node
);
9305 map
->option_class
= ntohs(nx_map
->option_class
);
9306 map
->option_type
= nx_map
->option_type
;
9308 map
->option_len
= nx_map
->option_len
;
9309 if (map
->option_len
% 4 || map
->option_len
> TLV_MAX_OPT_SIZE
) {
9310 VLOG_WARN_RL(&bad_ofmsg_rl
,
9311 "tlv table option length (%u) is not a valid option size",
9313 ofputil_uninit_tlv_table(mappings
);
9314 return OFPERR_NXTTMFC_BAD_OPT_LEN
;
9317 map
->index
= ntohs(nx_map
->index
);
9318 if (map
->index
>= max_fields
) {
9319 VLOG_WARN_RL(&bad_ofmsg_rl
,
9320 "tlv table field index (%u) is too large (max %u)",
9321 map
->index
, max_fields
- 1);
9322 ofputil_uninit_tlv_table(mappings
);
9323 return OFPERR_NXTTMFC_BAD_FIELD_IDX
;
9331 ofputil_decode_tlv_table_mod(const struct ofp_header
*oh
,
9332 struct ofputil_tlv_table_mod
*ttm
)
9335 struct nx_tlv_table_mod
*nx_ttm
;
9337 ofpbuf_use_const(&msg
, oh
, ntohs(oh
->length
));
9338 ofpraw_pull_assert(&msg
);
9340 nx_ttm
= ofpbuf_pull(&msg
, sizeof *nx_ttm
);
9341 ttm
->command
= ntohs(nx_ttm
->command
);
9342 if (ttm
->command
> NXTTMC_CLEAR
) {
9343 VLOG_WARN_RL(&bad_ofmsg_rl
,
9344 "tlv table mod command (%u) is out of range",
9346 return OFPERR_NXTTMFC_BAD_COMMAND
;
9349 return decode_tlv_table_mappings(&msg
, TUN_METADATA_NUM_OPTS
,
9354 ofputil_encode_tlv_table_reply(const struct ofp_header
*oh
,
9355 struct ofputil_tlv_table_reply
*ttr
)
9358 struct nx_tlv_table_reply
*nx_ttr
;
9360 b
= ofpraw_alloc_reply(OFPRAW_NXT_TLV_TABLE_REPLY
, oh
, 0);
9361 nx_ttr
= ofpbuf_put_zeros(b
, sizeof *nx_ttr
);
9362 nx_ttr
->max_option_space
= htonl(ttr
->max_option_space
);
9363 nx_ttr
->max_fields
= htons(ttr
->max_fields
);
9365 encode_tlv_table_mappings(b
, &ttr
->mappings
);
9370 /* Decodes the NXT_TLV_TABLE_REPLY message in 'oh' into '*ttr'. Returns 0
9371 * if successful, otherwise an ofperr.
9373 * The decoder verifies that the indexes in 'ttr->mappings' are less than
9374 * 'ttr->max_fields', but the caller must ensure, if necessary, that they are
9375 * less than TUN_METADATA_NUM_OPTS. */
9377 ofputil_decode_tlv_table_reply(const struct ofp_header
*oh
,
9378 struct ofputil_tlv_table_reply
*ttr
)
9381 struct nx_tlv_table_reply
*nx_ttr
;
9383 ofpbuf_use_const(&msg
, oh
, ntohs(oh
->length
));
9384 ofpraw_pull_assert(&msg
);
9386 nx_ttr
= ofpbuf_pull(&msg
, sizeof *nx_ttr
);
9387 ttr
->max_option_space
= ntohl(nx_ttr
->max_option_space
);
9388 ttr
->max_fields
= ntohs(nx_ttr
->max_fields
);
9390 return decode_tlv_table_mappings(&msg
, ttr
->max_fields
, &ttr
->mappings
);
9394 ofputil_uninit_tlv_table(struct ovs_list
*mappings
)
9396 struct ofputil_tlv_map
*map
;
9398 LIST_FOR_EACH_POP (map
, list_node
, mappings
) {
9404 ofputil_async_msg_type_to_string(enum ofputil_async_msg_type type
)
9407 case OAM_PACKET_IN
: return "PACKET_IN";
9408 case OAM_PORT_STATUS
: return "PORT_STATUS";
9409 case OAM_FLOW_REMOVED
: return "FLOW_REMOVED";
9410 case OAM_ROLE_STATUS
: return "ROLE_STATUS";
9411 case OAM_TABLE_STATUS
: return "TABLE_STATUS";
9412 case OAM_REQUESTFORWARD
: return "REQUESTFORWARD";
9420 struct ofp14_async_prop
{
9422 enum ofputil_async_msg_type oam
;
9424 uint32_t allowed10
, allowed14
;
9427 #define AP_PAIR(SLAVE_PROP_TYPE, OAM, A10, A14) \
9428 { SLAVE_PROP_TYPE, OAM, false, A10, (A14) ? (A14) : (A10) }, \
9429 { (SLAVE_PROP_TYPE + 1), OAM, true, A10, (A14) ? (A14) : (A10) }
9431 static const struct ofp14_async_prop async_props
[] = {
9432 AP_PAIR( 0, OAM_PACKET_IN
, OFPR10_BITS
, OFPR14_BITS
),
9433 AP_PAIR( 2, OAM_PORT_STATUS
, (1 << OFPPR_N_REASONS
) - 1, 0),
9434 AP_PAIR( 4, OAM_FLOW_REMOVED
, (1 << OVS_OFPRR_NONE
) - 1, 0),
9435 AP_PAIR( 6, OAM_ROLE_STATUS
, (1 << OFPCRR_N_REASONS
) - 1, 0),
9436 AP_PAIR( 8, OAM_TABLE_STATUS
, OFPTR_BITS
, 0),
9437 AP_PAIR(10, OAM_REQUESTFORWARD
, (1 << OFPRFR_N_REASONS
) - 1, 0),
9440 #define FOR_EACH_ASYNC_PROP(VAR) \
9441 for (const struct ofp14_async_prop *VAR = async_props; \
9442 VAR < &async_props[ARRAY_SIZE(async_props)]; VAR++)
9444 static const struct ofp14_async_prop
*
9445 get_ofp14_async_config_prop_by_prop_type(uint64_t prop_type
)
9447 FOR_EACH_ASYNC_PROP (ap
) {
9448 if (prop_type
== ap
->prop_type
) {
9455 static const struct ofp14_async_prop
*
9456 get_ofp14_async_config_prop_by_oam(enum ofputil_async_msg_type oam
,
9459 FOR_EACH_ASYNC_PROP (ap
) {
9460 if (ap
->oam
== oam
&& ap
->master
== master
) {
9468 ofp14_async_prop_allowed(const struct ofp14_async_prop
*prop
,
9469 enum ofp_version version
)
9471 return version
>= OFP14_VERSION
? prop
->allowed14
: prop
->allowed10
;
9475 encode_async_mask(const struct ofputil_async_cfg
*src
,
9476 const struct ofp14_async_prop
*ap
,
9477 enum ofp_version version
)
9479 uint32_t mask
= ap
->master
? src
->master
[ap
->oam
] : src
->slave
[ap
->oam
];
9480 return htonl(mask
& ofp14_async_prop_allowed(ap
, version
));
9484 decode_async_mask(ovs_be32 src
,
9485 const struct ofp14_async_prop
*ap
, enum ofp_version version
,
9486 bool loose
, struct ofputil_async_cfg
*dst
)
9488 uint32_t mask
= ntohl(src
);
9489 uint32_t allowed
= ofp14_async_prop_allowed(ap
, version
);
9490 if (mask
& ~allowed
) {
9491 OFPPROP_LOG(&bad_ofmsg_rl
, loose
,
9492 "bad value %#x for %s (allowed mask %#x)",
9493 mask
, ofputil_async_msg_type_to_string(ap
->oam
),
9497 return OFPERR_OFPACFC_INVALID
;
9501 uint32_t *array
= ap
->master
? dst
->master
: dst
->slave
;
9502 array
[ap
->oam
] = mask
;
9507 parse_async_tlv(const struct ofpbuf
*property
,
9508 const struct ofp14_async_prop
*ap
,
9509 struct ofputil_async_cfg
*ac
,
9510 enum ofp_version version
, bool loose
)
9515 error
= ofpprop_parse_be32(property
, &mask
);
9520 if (ofpprop_is_experimenter(ap
->prop_type
)) {
9521 /* For experimenter properties, whether a property is for the master or
9522 * slave role is indicated by both 'type' and 'exp_type' in struct
9523 * ofp_prop_experimenter. Check that these are consistent. */
9524 const struct ofp_prop_experimenter
*ope
= property
->data
;
9525 bool should_be_master
= ope
->type
== htons(0xffff);
9526 if (should_be_master
!= ap
->master
) {
9527 VLOG_WARN_RL(&bad_ofmsg_rl
, "async property type %#"PRIx16
" "
9528 "indicates %s role but exp_type %"PRIu32
" indicates "
9531 should_be_master
? "master" : "slave",
9532 ntohl(ope
->exp_type
),
9533 ap
->master
? "master" : "slave");
9534 return OFPERR_OFPBPC_BAD_EXP_TYPE
;
9538 return decode_async_mask(mask
, ap
, version
, loose
, ac
);
9542 decode_legacy_async_masks(const ovs_be32 masks
[2],
9543 enum ofputil_async_msg_type oam
,
9544 enum ofp_version version
,
9545 struct ofputil_async_cfg
*dst
)
9547 for (int i
= 0; i
< 2; i
++) {
9548 bool master
= i
== 0;
9549 const struct ofp14_async_prop
*ap
9550 = get_ofp14_async_config_prop_by_oam(oam
, master
);
9551 decode_async_mask(masks
[i
], ap
, version
, true, dst
);
9555 /* Decodes the OpenFlow "set async config" request and "get async config
9556 * reply" message in '*oh' into an abstract form in 'ac'.
9558 * Some versions of the "set async config" request change only some of the
9559 * settings and leave the others alone. This function uses 'basis' as the
9560 * initial state for decoding these. Other versions of the request change all
9561 * the settings; this function ignores 'basis' when decoding these.
9563 * If 'loose' is true, this function ignores properties and values that it does
9564 * not understand, as a controller would want to do when interpreting
9565 * capabilities provided by a switch. If 'loose' is false, this function
9566 * treats unknown properties and values as an error, as a switch would want to
9567 * do when interpreting a configuration request made by a controller.
9569 * Returns 0 if successful, otherwise an OFPERR_* value.
9571 * Returns error code OFPERR_OFPACFC_INVALID if the value of mask is not in
9572 * the valid range of mask.
9574 * Returns error code OFPERR_OFPACFC_UNSUPPORTED if the configuration is not
9577 ofputil_decode_set_async_config(const struct ofp_header
*oh
, bool loose
,
9578 const struct ofputil_async_cfg
*basis
,
9579 struct ofputil_async_cfg
*ac
)
9584 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
9585 raw
= ofpraw_pull_assert(&b
);
9587 if (raw
== OFPRAW_OFPT13_SET_ASYNC
||
9588 raw
== OFPRAW_NXT_SET_ASYNC_CONFIG
||
9589 raw
== OFPRAW_OFPT13_GET_ASYNC_REPLY
) {
9590 const struct nx_async_config
*msg
= ofpmsg_body(oh
);
9592 *ac
= OFPUTIL_ASYNC_CFG_INIT
;
9593 decode_legacy_async_masks(msg
->packet_in_mask
, OAM_PACKET_IN
,
9595 decode_legacy_async_masks(msg
->port_status_mask
, OAM_PORT_STATUS
,
9597 decode_legacy_async_masks(msg
->flow_removed_mask
, OAM_FLOW_REMOVED
,
9599 } else if (raw
== OFPRAW_OFPT14_SET_ASYNC
||
9600 raw
== OFPRAW_OFPT14_GET_ASYNC_REPLY
||
9601 raw
== OFPRAW_NXT_SET_ASYNC_CONFIG2
) {
9603 while (b
.size
> 0) {
9604 struct ofpbuf property
;
9608 error
= ofpprop_pull__(&b
, &property
, 8, 0xfffe, &type
);
9613 const struct ofp14_async_prop
*ap
9614 = get_ofp14_async_config_prop_by_prop_type(type
);
9616 ? parse_async_tlv(&property
, ap
, ac
, oh
->version
, loose
)
9617 : OFPPROP_UNKNOWN(loose
, "async config", type
));
9619 /* Most messages use OFPBPC_BAD_TYPE but async has its own (who
9620 * knows why, it's OpenFlow. */
9621 if (error
== OFPERR_OFPBPC_BAD_TYPE
) {
9622 error
= OFPERR_OFPACFC_UNSUPPORTED
;
9628 return OFPERR_OFPBRC_BAD_VERSION
;
9634 encode_legacy_async_masks(const struct ofputil_async_cfg
*ac
,
9635 enum ofputil_async_msg_type oam
,
9636 enum ofp_version version
,
9639 for (int i
= 0; i
< 2; i
++) {
9640 bool master
= i
== 0;
9641 const struct ofp14_async_prop
*ap
9642 = get_ofp14_async_config_prop_by_oam(oam
, master
);
9643 masks
[i
] = encode_async_mask(ac
, ap
, version
);
9648 ofputil_put_async_config__(const struct ofputil_async_cfg
*ac
,
9649 struct ofpbuf
*buf
, bool tlv
,
9650 enum ofp_version version
, uint32_t oams
)
9653 struct nx_async_config
*msg
= ofpbuf_put_zeros(buf
, sizeof *msg
);
9654 encode_legacy_async_masks(ac
, OAM_PACKET_IN
, version
,
9655 msg
->packet_in_mask
);
9656 encode_legacy_async_masks(ac
, OAM_PORT_STATUS
, version
,
9657 msg
->port_status_mask
);
9658 encode_legacy_async_masks(ac
, OAM_FLOW_REMOVED
, version
,
9659 msg
->flow_removed_mask
);
9661 FOR_EACH_ASYNC_PROP (ap
) {
9662 if (oams
& (1u << ap
->oam
)) {
9663 size_t ofs
= buf
->size
;
9664 ofpprop_put_be32(buf
, ap
->prop_type
,
9665 encode_async_mask(ac
, ap
, version
));
9667 /* For experimenter properties, we need to use type 0xfffe for
9668 * master and 0xffff for slaves. */
9669 if (ofpprop_is_experimenter(ap
->prop_type
)) {
9670 struct ofp_prop_experimenter
*ope
9671 = ofpbuf_at_assert(buf
, ofs
, sizeof *ope
);
9672 ope
->type
= ap
->master
? htons(0xffff) : htons(0xfffe);
9679 /* Encodes and returns a reply to the OFPT_GET_ASYNC_REQUEST in 'oh' that
9680 * states that the asynchronous message configuration is 'ac'. */
9682 ofputil_encode_get_async_reply(const struct ofp_header
*oh
,
9683 const struct ofputil_async_cfg
*ac
)
9687 enum ofpraw raw
= (oh
->version
< OFP14_VERSION
9688 ? OFPRAW_OFPT13_GET_ASYNC_REPLY
9689 : OFPRAW_OFPT14_GET_ASYNC_REPLY
);
9690 struct ofpbuf
*reply
= ofpraw_alloc_reply(raw
, oh
, 0);
9691 ofputil_put_async_config__(ac
, reply
,
9692 raw
== OFPRAW_OFPT14_GET_ASYNC_REPLY
,
9693 oh
->version
, UINT32_MAX
);
9699 /* Encodes and returns a message, in a format appropriate for OpenFlow version
9700 * 'ofp_version', that sets the asynchronous message configuration to 'ac'.
9702 * Specify 'oams' as a bitmap of OAM_* that indicate the asynchronous messages
9703 * to configure. OF1.0 through OF1.3 can't natively configure a subset of
9704 * messages, so more messages than requested may be configured. OF1.0 through
9705 * OF1.3 also can't configure OVS extension OAM_* values, so if 'oam' includes
9706 * any extensions then this function encodes an Open vSwitch extension message
9707 * that does support configuring OVS extension OAM_*. */
9709 ofputil_encode_set_async_config(const struct ofputil_async_cfg
*ac
,
9710 uint32_t oams
, enum ofp_version ofp_version
)
9712 enum ofpraw raw
= (ofp_version
>= OFP14_VERSION
? OFPRAW_OFPT14_SET_ASYNC
9713 : oams
& OAM_EXTENSIONS
? OFPRAW_NXT_SET_ASYNC_CONFIG2
9714 : ofp_version
>= OFP13_VERSION
? OFPRAW_OFPT13_SET_ASYNC
9715 : OFPRAW_NXT_SET_ASYNC_CONFIG
);
9716 struct ofpbuf
*request
= ofpraw_alloc(raw
, ofp_version
, 0);
9717 ofputil_put_async_config__(ac
, request
,
9718 (raw
== OFPRAW_OFPT14_SET_ASYNC
||
9719 raw
== OFPRAW_NXT_SET_ASYNC_CONFIG2
),
9724 struct ofputil_async_cfg
9725 ofputil_async_cfg_default(enum ofp_version version
)
9727 return (struct ofputil_async_cfg
) {
9728 .master
[OAM_PACKET_IN
]
9729 = ((version
>= OFP14_VERSION
? OFPR14_BITS
: OFPR10_BITS
)
9730 & ~(1u << OFPR_INVALID_TTL
)),
9732 .master
[OAM_FLOW_REMOVED
]
9733 = (version
>= OFP14_VERSION
? OFPRR14_BITS
: OFPRR10_BITS
),
9735 .master
[OAM_PORT_STATUS
] = OFPPR_BITS
,
9736 .slave
[OAM_PORT_STATUS
] = OFPPR_BITS
,