2 * Copyright (c) 2008-2017 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include <netinet/icmp6.h>
27 #include "byte-order.h"
28 #include "classifier.h"
30 #include "multipath.h"
34 #include "openflow/netronome-ext.h"
35 #include "openvswitch/dynamic-string.h"
36 #include "openvswitch/meta-flow.h"
37 #include "openvswitch/ofp-actions.h"
38 #include "openvswitch/ofp-errors.h"
39 #include "openvswitch/ofp-msgs.h"
40 #include "openvswitch/ofp-print.h"
41 #include "openvswitch/ofp-prop.h"
42 #include "openvswitch/ofp-util.h"
43 #include "openvswitch/ofpbuf.h"
44 #include "openvswitch/type-props.h"
45 #include "openvswitch/vlog.h"
46 #include "openflow/intel-ext.h"
49 #include "tun-metadata.h"
50 #include "unaligned.h"
54 VLOG_DEFINE_THIS_MODULE(ofp_util
);
56 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
57 * in the peer and so there's not much point in showing a lot of them. */
58 static struct vlog_rate_limit bad_ofmsg_rl
= VLOG_RATE_LIMIT_INIT(1, 5);
60 static enum ofputil_table_vacancy
ofputil_decode_table_vacancy(
61 ovs_be32 config
, enum ofp_version
);
62 static enum ofputil_table_eviction
ofputil_decode_table_eviction(
63 ovs_be32 config
, enum ofp_version
);
64 static ovs_be32
ofputil_encode_table_config(enum ofputil_table_miss
,
65 enum ofputil_table_eviction
,
66 enum ofputil_table_vacancy
,
69 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
70 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
73 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
74 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
75 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
76 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
79 ofputil_wcbits_to_netmask(int wcbits
)
82 return wcbits
< 32 ? htonl(~((1u << wcbits
) - 1)) : 0;
85 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
86 * that it wildcards, that is, the number of 0-bits in 'netmask', a number
87 * between 0 and 32 inclusive.
89 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
90 * still be in the valid range but isn't otherwise meaningful. */
92 ofputil_netmask_to_wcbits(ovs_be32 netmask
)
94 return 32 - ip_count_cidr_bits(netmask
);
97 /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
98 * flow_wildcards in 'wc' for use in struct match. It is the caller's
99 * responsibility to handle the special case where the flow match's dl_vlan is
100 * set to OFP_VLAN_NONE. */
102 ofputil_wildcard_from_ofpfw10(uint32_t ofpfw
, struct flow_wildcards
*wc
)
104 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 39);
106 /* Initialize most of wc. */
107 flow_wildcards_init_catchall(wc
);
109 if (!(ofpfw
& OFPFW10_IN_PORT
)) {
110 wc
->masks
.in_port
.ofp_port
= u16_to_ofp(UINT16_MAX
);
113 if (!(ofpfw
& OFPFW10_NW_TOS
)) {
114 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
117 if (!(ofpfw
& OFPFW10_NW_PROTO
)) {
118 wc
->masks
.nw_proto
= UINT8_MAX
;
120 wc
->masks
.nw_src
= ofputil_wcbits_to_netmask(ofpfw
121 >> OFPFW10_NW_SRC_SHIFT
);
122 wc
->masks
.nw_dst
= ofputil_wcbits_to_netmask(ofpfw
123 >> OFPFW10_NW_DST_SHIFT
);
125 if (!(ofpfw
& OFPFW10_TP_SRC
)) {
126 wc
->masks
.tp_src
= OVS_BE16_MAX
;
128 if (!(ofpfw
& OFPFW10_TP_DST
)) {
129 wc
->masks
.tp_dst
= OVS_BE16_MAX
;
132 if (!(ofpfw
& OFPFW10_DL_SRC
)) {
133 WC_MASK_FIELD(wc
, dl_src
);
135 if (!(ofpfw
& OFPFW10_DL_DST
)) {
136 WC_MASK_FIELD(wc
, dl_dst
);
138 if (!(ofpfw
& OFPFW10_DL_TYPE
)) {
139 wc
->masks
.dl_type
= OVS_BE16_MAX
;
143 if (!(ofpfw
& OFPFW10_DL_VLAN_PCP
)) {
144 wc
->masks
.vlans
[0].tci
|= htons(VLAN_PCP_MASK
| VLAN_CFI
);
146 if (!(ofpfw
& OFPFW10_DL_VLAN
)) {
147 wc
->masks
.vlans
[0].tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
151 /* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */
153 ofputil_match_from_ofp10_match(const struct ofp10_match
*ofmatch
,
156 uint32_t ofpfw
= ntohl(ofmatch
->wildcards
) & OFPFW10_ALL
;
158 /* Initialize match->wc. */
159 memset(&match
->flow
, 0, sizeof match
->flow
);
160 ofputil_wildcard_from_ofpfw10(ofpfw
, &match
->wc
);
162 /* Initialize most of match->flow. */
163 match
->flow
.nw_src
= ofmatch
->nw_src
;
164 match
->flow
.nw_dst
= ofmatch
->nw_dst
;
165 match
->flow
.in_port
.ofp_port
= u16_to_ofp(ntohs(ofmatch
->in_port
));
166 match
->flow
.dl_type
= ofputil_dl_type_from_openflow(ofmatch
->dl_type
);
167 match
->flow
.tp_src
= ofmatch
->tp_src
;
168 match
->flow
.tp_dst
= ofmatch
->tp_dst
;
169 match
->flow
.dl_src
= ofmatch
->dl_src
;
170 match
->flow
.dl_dst
= ofmatch
->dl_dst
;
171 match
->flow
.nw_tos
= ofmatch
->nw_tos
& IP_DSCP_MASK
;
172 match
->flow
.nw_proto
= ofmatch
->nw_proto
;
174 /* Translate VLANs. */
175 if (!(ofpfw
& OFPFW10_DL_VLAN
) &&
176 ofmatch
->dl_vlan
== htons(OFP10_VLAN_NONE
)) {
177 /* Match only packets without 802.1Q header.
179 * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
181 * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
182 * because we can't have a specific PCP without an 802.1Q header.
183 * However, older versions of OVS treated this as matching packets
184 * withut an 802.1Q header, so we do here too. */
185 match
->flow
.vlans
[0].tci
= htons(0);
186 match
->wc
.masks
.vlans
[0].tci
= htons(0xffff);
188 ovs_be16 vid
, pcp
, tci
;
191 vid
= ofmatch
->dl_vlan
& htons(VLAN_VID_MASK
);
192 hpcp
= (ofmatch
->dl_vlan_pcp
<< VLAN_PCP_SHIFT
) & VLAN_PCP_MASK
;
194 tci
= vid
| pcp
| htons(VLAN_CFI
);
195 match
->flow
.vlans
[0].tci
= tci
& match
->wc
.masks
.vlans
[0].tci
;
199 match_zero_wildcarded_fields(match
);
202 /* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */
204 ofputil_match_to_ofp10_match(const struct match
*match
,
205 struct ofp10_match
*ofmatch
)
207 const struct flow_wildcards
*wc
= &match
->wc
;
210 /* Figure out most OpenFlow wildcards. */
212 if (!wc
->masks
.in_port
.ofp_port
) {
213 ofpfw
|= OFPFW10_IN_PORT
;
215 if (!wc
->masks
.dl_type
) {
216 ofpfw
|= OFPFW10_DL_TYPE
;
218 if (!wc
->masks
.nw_proto
) {
219 ofpfw
|= OFPFW10_NW_PROTO
;
221 ofpfw
|= (ofputil_netmask_to_wcbits(wc
->masks
.nw_src
)
222 << OFPFW10_NW_SRC_SHIFT
);
223 ofpfw
|= (ofputil_netmask_to_wcbits(wc
->masks
.nw_dst
)
224 << OFPFW10_NW_DST_SHIFT
);
225 if (!(wc
->masks
.nw_tos
& IP_DSCP_MASK
)) {
226 ofpfw
|= OFPFW10_NW_TOS
;
228 if (!wc
->masks
.tp_src
) {
229 ofpfw
|= OFPFW10_TP_SRC
;
231 if (!wc
->masks
.tp_dst
) {
232 ofpfw
|= OFPFW10_TP_DST
;
234 if (eth_addr_is_zero(wc
->masks
.dl_src
)) {
235 ofpfw
|= OFPFW10_DL_SRC
;
237 if (eth_addr_is_zero(wc
->masks
.dl_dst
)) {
238 ofpfw
|= OFPFW10_DL_DST
;
241 /* Translate VLANs. */
242 ofmatch
->dl_vlan
= htons(0);
243 ofmatch
->dl_vlan_pcp
= 0;
244 if (match
->wc
.masks
.vlans
[0].tci
== htons(0)) {
245 ofpfw
|= OFPFW10_DL_VLAN
| OFPFW10_DL_VLAN_PCP
;
246 } else if (match
->wc
.masks
.vlans
[0].tci
& htons(VLAN_CFI
)
247 && !(match
->flow
.vlans
[0].tci
& htons(VLAN_CFI
))) {
248 ofmatch
->dl_vlan
= htons(OFP10_VLAN_NONE
);
250 if (!(match
->wc
.masks
.vlans
[0].tci
& htons(VLAN_VID_MASK
))) {
251 ofpfw
|= OFPFW10_DL_VLAN
;
254 htons(vlan_tci_to_vid(match
->flow
.vlans
[0].tci
));
257 if (!(match
->wc
.masks
.vlans
[0].tci
& htons(VLAN_PCP_MASK
))) {
258 ofpfw
|= OFPFW10_DL_VLAN_PCP
;
260 ofmatch
->dl_vlan_pcp
= vlan_tci_to_pcp(match
->flow
.vlans
[0].tci
);
264 /* Compose most of the match structure. */
265 ofmatch
->wildcards
= htonl(ofpfw
);
266 ofmatch
->in_port
= htons(ofp_to_u16(match
->flow
.in_port
.ofp_port
));
267 ofmatch
->dl_src
= match
->flow
.dl_src
;
268 ofmatch
->dl_dst
= match
->flow
.dl_dst
;
269 ofmatch
->dl_type
= ofputil_dl_type_to_openflow(match
->flow
.dl_type
);
270 ofmatch
->nw_src
= match
->flow
.nw_src
;
271 ofmatch
->nw_dst
= match
->flow
.nw_dst
;
272 ofmatch
->nw_tos
= match
->flow
.nw_tos
& IP_DSCP_MASK
;
273 ofmatch
->nw_proto
= match
->flow
.nw_proto
;
274 ofmatch
->tp_src
= match
->flow
.tp_src
;
275 ofmatch
->tp_dst
= match
->flow
.tp_dst
;
276 memset(ofmatch
->pad1
, '\0', sizeof ofmatch
->pad1
);
277 memset(ofmatch
->pad2
, '\0', sizeof ofmatch
->pad2
);
281 ofputil_pull_ofp11_match(struct ofpbuf
*buf
, const struct tun_table
*tun_table
,
282 const struct vl_mff_map
*vl_mff_map
,
283 struct match
*match
, uint16_t *padded_match_len
)
285 struct ofp11_match_header
*omh
= buf
->data
;
288 if (buf
->size
< sizeof *omh
) {
289 return OFPERR_OFPBMC_BAD_LEN
;
292 match_len
= ntohs(omh
->length
);
294 switch (ntohs(omh
->type
)) {
295 case OFPMT_STANDARD
: {
296 struct ofp11_match
*om
;
298 if (match_len
!= sizeof *om
|| buf
->size
< sizeof *om
) {
299 return OFPERR_OFPBMC_BAD_LEN
;
301 om
= ofpbuf_pull(buf
, sizeof *om
);
302 if (padded_match_len
) {
303 *padded_match_len
= match_len
;
305 return ofputil_match_from_ofp11_match(om
, match
);
309 if (padded_match_len
) {
310 *padded_match_len
= ROUND_UP(match_len
, 8);
312 return oxm_pull_match(buf
, tun_table
, vl_mff_map
, match
);
315 return OFPERR_OFPBMC_BAD_TYPE
;
319 /* Converts the ofp11_match in 'ofmatch' into a struct match in 'match'.
320 * Returns 0 if successful, otherwise an OFPERR_* value. */
322 ofputil_match_from_ofp11_match(const struct ofp11_match
*ofmatch
,
325 uint16_t wc
= ntohl(ofmatch
->wildcards
);
326 bool ipv4
, arp
, rarp
;
328 match_init_catchall(match
);
330 if (!(wc
& OFPFW11_IN_PORT
)) {
334 error
= ofputil_port_from_ofp11(ofmatch
->in_port
, &ofp_port
);
336 return OFPERR_OFPBMC_BAD_VALUE
;
338 match_set_in_port(match
, ofp_port
);
341 match_set_dl_src_masked(match
, ofmatch
->dl_src
,
342 eth_addr_invert(ofmatch
->dl_src_mask
));
343 match_set_dl_dst_masked(match
, ofmatch
->dl_dst
,
344 eth_addr_invert(ofmatch
->dl_dst_mask
));
346 if (!(wc
& OFPFW11_DL_VLAN
)) {
347 if (ofmatch
->dl_vlan
== htons(OFPVID11_NONE
)) {
348 /* Match only packets without a VLAN tag. */
349 match
->flow
.vlans
[0].tci
= htons(0);
350 match
->wc
.masks
.vlans
[0].tci
= OVS_BE16_MAX
;
352 if (ofmatch
->dl_vlan
== htons(OFPVID11_ANY
)) {
353 /* Match any packet with a VLAN tag regardless of VID. */
354 match
->flow
.vlans
[0].tci
= htons(VLAN_CFI
);
355 match
->wc
.masks
.vlans
[0].tci
= htons(VLAN_CFI
);
356 } else if (ntohs(ofmatch
->dl_vlan
) < 4096) {
357 /* Match only packets with the specified VLAN VID. */
358 match
->flow
.vlans
[0].tci
= htons(VLAN_CFI
) | ofmatch
->dl_vlan
;
359 match
->wc
.masks
.vlans
[0].tci
= htons(VLAN_CFI
| VLAN_VID_MASK
);
362 return OFPERR_OFPBMC_BAD_VALUE
;
365 if (!(wc
& OFPFW11_DL_VLAN_PCP
)) {
366 if (ofmatch
->dl_vlan_pcp
<= 7) {
367 match
->flow
.vlans
[0].tci
|= htons(ofmatch
->dl_vlan_pcp
369 match
->wc
.masks
.vlans
[0].tci
|= htons(VLAN_PCP_MASK
);
372 return OFPERR_OFPBMC_BAD_VALUE
;
378 if (!(wc
& OFPFW11_DL_TYPE
)) {
379 match_set_dl_type(match
,
380 ofputil_dl_type_from_openflow(ofmatch
->dl_type
));
383 ipv4
= match
->flow
.dl_type
== htons(ETH_TYPE_IP
);
384 arp
= match
->flow
.dl_type
== htons(ETH_TYPE_ARP
);
385 rarp
= match
->flow
.dl_type
== htons(ETH_TYPE_RARP
);
387 if (ipv4
&& !(wc
& OFPFW11_NW_TOS
)) {
388 if (ofmatch
->nw_tos
& ~IP_DSCP_MASK
) {
390 return OFPERR_OFPBMC_BAD_VALUE
;
393 match_set_nw_dscp(match
, ofmatch
->nw_tos
);
396 if (ipv4
|| arp
|| rarp
) {
397 if (!(wc
& OFPFW11_NW_PROTO
)) {
398 match_set_nw_proto(match
, ofmatch
->nw_proto
);
400 match_set_nw_src_masked(match
, ofmatch
->nw_src
, ~ofmatch
->nw_src_mask
);
401 match_set_nw_dst_masked(match
, ofmatch
->nw_dst
, ~ofmatch
->nw_dst_mask
);
404 #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
405 if (ipv4
&& (wc
& OFPFW11_TP_ALL
) != OFPFW11_TP_ALL
) {
406 switch (match
->flow
.nw_proto
) {
408 /* "A.2.3 Flow Match Structures" in OF1.1 says:
410 * The tp_src and tp_dst fields will be ignored unless the
411 * network protocol specified is as TCP, UDP or SCTP.
413 * but I'm pretty sure we should support ICMP too, otherwise
414 * that's a regression from OF1.0. */
415 if (!(wc
& OFPFW11_TP_SRC
)) {
416 uint16_t icmp_type
= ntohs(ofmatch
->tp_src
);
417 if (icmp_type
< 0x100) {
418 match_set_icmp_type(match
, icmp_type
);
420 return OFPERR_OFPBMC_BAD_FIELD
;
423 if (!(wc
& OFPFW11_TP_DST
)) {
424 uint16_t icmp_code
= ntohs(ofmatch
->tp_dst
);
425 if (icmp_code
< 0x100) {
426 match_set_icmp_code(match
, icmp_code
);
428 return OFPERR_OFPBMC_BAD_FIELD
;
436 if (!(wc
& (OFPFW11_TP_SRC
))) {
437 match_set_tp_src(match
, ofmatch
->tp_src
);
439 if (!(wc
& (OFPFW11_TP_DST
))) {
440 match_set_tp_dst(match
, ofmatch
->tp_dst
);
445 /* OF1.1 says explicitly to ignore this. */
450 if (eth_type_mpls(match
->flow
.dl_type
)) {
451 if (!(wc
& OFPFW11_MPLS_LABEL
)) {
452 match_set_mpls_label(match
, 0, ofmatch
->mpls_label
);
454 if (!(wc
& OFPFW11_MPLS_TC
)) {
455 match_set_mpls_tc(match
, 0, ofmatch
->mpls_tc
);
459 match_set_metadata_masked(match
, ofmatch
->metadata
,
460 ~ofmatch
->metadata_mask
);
465 /* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */
467 ofputil_match_to_ofp11_match(const struct match
*match
,
468 struct ofp11_match
*ofmatch
)
472 memset(ofmatch
, 0, sizeof *ofmatch
);
473 ofmatch
->omh
.type
= htons(OFPMT_STANDARD
);
474 ofmatch
->omh
.length
= htons(OFPMT11_STANDARD_LENGTH
);
476 if (!match
->wc
.masks
.in_port
.ofp_port
) {
477 wc
|= OFPFW11_IN_PORT
;
479 ofmatch
->in_port
= ofputil_port_to_ofp11(match
->flow
.in_port
.ofp_port
);
482 ofmatch
->dl_src
= match
->flow
.dl_src
;
483 ofmatch
->dl_src_mask
= eth_addr_invert(match
->wc
.masks
.dl_src
);
484 ofmatch
->dl_dst
= match
->flow
.dl_dst
;
485 ofmatch
->dl_dst_mask
= eth_addr_invert(match
->wc
.masks
.dl_dst
);
487 if (match
->wc
.masks
.vlans
[0].tci
== htons(0)) {
488 wc
|= OFPFW11_DL_VLAN
| OFPFW11_DL_VLAN_PCP
;
489 } else if (match
->wc
.masks
.vlans
[0].tci
& htons(VLAN_CFI
)
490 && !(match
->flow
.vlans
[0].tci
& htons(VLAN_CFI
))) {
491 ofmatch
->dl_vlan
= htons(OFPVID11_NONE
);
492 wc
|= OFPFW11_DL_VLAN_PCP
;
494 if (!(match
->wc
.masks
.vlans
[0].tci
& htons(VLAN_VID_MASK
))) {
495 ofmatch
->dl_vlan
= htons(OFPVID11_ANY
);
498 htons(vlan_tci_to_vid(match
->flow
.vlans
[0].tci
));
501 if (!(match
->wc
.masks
.vlans
[0].tci
& htons(VLAN_PCP_MASK
))) {
502 wc
|= OFPFW11_DL_VLAN_PCP
;
504 ofmatch
->dl_vlan_pcp
= vlan_tci_to_pcp(match
->flow
.vlans
[0].tci
);
508 if (!match
->wc
.masks
.dl_type
) {
509 wc
|= OFPFW11_DL_TYPE
;
511 ofmatch
->dl_type
= ofputil_dl_type_to_openflow(match
->flow
.dl_type
);
514 if (!(match
->wc
.masks
.nw_tos
& IP_DSCP_MASK
)) {
515 wc
|= OFPFW11_NW_TOS
;
517 ofmatch
->nw_tos
= match
->flow
.nw_tos
& IP_DSCP_MASK
;
520 if (!match
->wc
.masks
.nw_proto
) {
521 wc
|= OFPFW11_NW_PROTO
;
523 ofmatch
->nw_proto
= match
->flow
.nw_proto
;
526 ofmatch
->nw_src
= match
->flow
.nw_src
;
527 ofmatch
->nw_src_mask
= ~match
->wc
.masks
.nw_src
;
528 ofmatch
->nw_dst
= match
->flow
.nw_dst
;
529 ofmatch
->nw_dst_mask
= ~match
->wc
.masks
.nw_dst
;
531 if (!match
->wc
.masks
.tp_src
) {
532 wc
|= OFPFW11_TP_SRC
;
534 ofmatch
->tp_src
= match
->flow
.tp_src
;
537 if (!match
->wc
.masks
.tp_dst
) {
538 wc
|= OFPFW11_TP_DST
;
540 ofmatch
->tp_dst
= match
->flow
.tp_dst
;
543 if (!(match
->wc
.masks
.mpls_lse
[0] & htonl(MPLS_LABEL_MASK
))) {
544 wc
|= OFPFW11_MPLS_LABEL
;
546 ofmatch
->mpls_label
= htonl(mpls_lse_to_label(
547 match
->flow
.mpls_lse
[0]));
550 if (!(match
->wc
.masks
.mpls_lse
[0] & htonl(MPLS_TC_MASK
))) {
551 wc
|= OFPFW11_MPLS_TC
;
553 ofmatch
->mpls_tc
= mpls_lse_to_tc(match
->flow
.mpls_lse
[0]);
556 ofmatch
->metadata
= match
->flow
.metadata
;
557 ofmatch
->metadata_mask
= ~match
->wc
.masks
.metadata
;
559 ofmatch
->wildcards
= htonl(wc
);
562 /* Returns the "typical" length of a match for 'protocol', for use in
563 * estimating space to preallocate. */
565 ofputil_match_typical_len(enum ofputil_protocol protocol
)
568 case OFPUTIL_P_OF10_STD
:
569 case OFPUTIL_P_OF10_STD_TID
:
570 return sizeof(struct ofp10_match
);
572 case OFPUTIL_P_OF10_NXM
:
573 case OFPUTIL_P_OF10_NXM_TID
:
574 return NXM_TYPICAL_LEN
;
576 case OFPUTIL_P_OF11_STD
:
577 return sizeof(struct ofp11_match
);
579 case OFPUTIL_P_OF12_OXM
:
580 case OFPUTIL_P_OF13_OXM
:
581 case OFPUTIL_P_OF14_OXM
:
582 case OFPUTIL_P_OF15_OXM
:
583 case OFPUTIL_P_OF16_OXM
:
584 return NXM_TYPICAL_LEN
;
591 /* Appends to 'b' an struct ofp11_match_header followed by a match that
592 * expresses 'match' properly for 'protocol', plus enough zero bytes to pad the
593 * data appended out to a multiple of 8. 'protocol' must be one that is usable
594 * in OpenFlow 1.1 or later.
596 * This function can cause 'b''s data to be reallocated.
598 * Returns the number of bytes appended to 'b', excluding the padding. Never
601 ofputil_put_ofp11_match(struct ofpbuf
*b
, const struct match
*match
,
602 enum ofputil_protocol protocol
)
605 case OFPUTIL_P_OF10_STD
:
606 case OFPUTIL_P_OF10_STD_TID
:
607 case OFPUTIL_P_OF10_NXM
:
608 case OFPUTIL_P_OF10_NXM_TID
:
611 case OFPUTIL_P_OF11_STD
: {
612 struct ofp11_match
*om
;
614 /* Make sure that no padding is needed. */
615 BUILD_ASSERT_DECL(sizeof *om
% 8 == 0);
617 om
= ofpbuf_put_uninit(b
, sizeof *om
);
618 ofputil_match_to_ofp11_match(match
, om
);
622 case OFPUTIL_P_OF12_OXM
:
623 case OFPUTIL_P_OF13_OXM
:
624 case OFPUTIL_P_OF14_OXM
:
625 case OFPUTIL_P_OF15_OXM
:
626 case OFPUTIL_P_OF16_OXM
:
627 return oxm_put_match(b
, match
,
628 ofputil_protocol_to_ofp_version(protocol
));
634 /* Given a 'dl_type' value in the format used in struct flow, returns the
635 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
638 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type
)
640 return (flow_dl_type
== htons(FLOW_DL_TYPE_NONE
)
641 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE
)
645 /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
646 * structure, returns the corresponding 'dl_type' value for use in struct
649 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type
)
651 return (ofp_dl_type
== htons(OFP_DL_TYPE_NOT_ETH_TYPE
)
652 ? htons(FLOW_DL_TYPE_NONE
)
658 struct proto_abbrev
{
659 enum ofputil_protocol protocol
;
663 /* Most users really don't care about some of the differences between
664 * protocols. These abbreviations help with that. */
665 static const struct proto_abbrev proto_abbrevs
[] = {
666 { OFPUTIL_P_ANY
, "any" },
667 { OFPUTIL_P_OF10_STD_ANY
, "OpenFlow10" },
668 { OFPUTIL_P_OF10_NXM_ANY
, "NXM" },
669 { OFPUTIL_P_ANY_OXM
, "OXM" },
671 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
673 enum ofputil_protocol ofputil_flow_dump_protocols
[] = {
683 size_t ofputil_n_flow_dump_protocols
= ARRAY_SIZE(ofputil_flow_dump_protocols
);
685 /* Returns the set of ofputil_protocols that are supported with the given
686 * OpenFlow 'version'. 'version' should normally be an 8-bit OpenFlow version
687 * identifier (e.g. 0x01 for OpenFlow 1.0, 0x02 for OpenFlow 1.1). Returns 0
688 * if 'version' is not supported or outside the valid range. */
689 enum ofputil_protocol
690 ofputil_protocols_from_ofp_version(enum ofp_version version
)
694 return OFPUTIL_P_OF10_STD_ANY
| OFPUTIL_P_OF10_NXM_ANY
;
696 return OFPUTIL_P_OF11_STD
;
698 return OFPUTIL_P_OF12_OXM
;
700 return OFPUTIL_P_OF13_OXM
;
702 return OFPUTIL_P_OF14_OXM
;
704 return OFPUTIL_P_OF15_OXM
;
706 return OFPUTIL_P_OF16_OXM
;
712 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
713 * connection that has negotiated the given 'version'. 'version' should
714 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
715 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
716 * outside the valid range. */
717 enum ofputil_protocol
718 ofputil_protocol_from_ofp_version(enum ofp_version version
)
720 return rightmost_1bit(ofputil_protocols_from_ofp_version(version
));
723 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
724 * etc.) that corresponds to 'protocol'. */
726 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol
)
729 case OFPUTIL_P_OF10_STD
:
730 case OFPUTIL_P_OF10_STD_TID
:
731 case OFPUTIL_P_OF10_NXM
:
732 case OFPUTIL_P_OF10_NXM_TID
:
733 return OFP10_VERSION
;
734 case OFPUTIL_P_OF11_STD
:
735 return OFP11_VERSION
;
736 case OFPUTIL_P_OF12_OXM
:
737 return OFP12_VERSION
;
738 case OFPUTIL_P_OF13_OXM
:
739 return OFP13_VERSION
;
740 case OFPUTIL_P_OF14_OXM
:
741 return OFP14_VERSION
;
742 case OFPUTIL_P_OF15_OXM
:
743 return OFP15_VERSION
;
744 case OFPUTIL_P_OF16_OXM
:
745 return OFP16_VERSION
;
751 /* Returns a bitmap of OpenFlow versions that are supported by at
752 * least one of the 'protocols'. */
754 ofputil_protocols_to_version_bitmap(enum ofputil_protocol protocols
)
758 for (; protocols
; protocols
= zero_rightmost_1bit(protocols
)) {
759 enum ofputil_protocol protocol
= rightmost_1bit(protocols
);
761 bitmap
|= 1u << ofputil_protocol_to_ofp_version(protocol
);
767 /* Returns the set of protocols that are supported on top of the
768 * OpenFlow versions included in 'bitmap'. */
769 enum ofputil_protocol
770 ofputil_protocols_from_version_bitmap(uint32_t bitmap
)
772 enum ofputil_protocol protocols
= 0;
774 for (; bitmap
; bitmap
= zero_rightmost_1bit(bitmap
)) {
775 enum ofp_version version
= rightmost_1bit_idx(bitmap
);
777 protocols
|= ofputil_protocols_from_ofp_version(version
);
783 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
786 ofputil_protocol_is_valid(enum ofputil_protocol protocol
)
788 return protocol
& OFPUTIL_P_ANY
&& is_pow2(protocol
);
791 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
792 * extension turned on or off if 'enable' is true or false, respectively.
794 * This extension is only useful for protocols whose "standard" version does
795 * not allow specific tables to be modified. In particular, this is true of
796 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
797 * specifies a table ID and so there is no need for such an extension. When
798 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
799 * extension, this function just returns its 'protocol' argument unchanged
800 * regardless of the value of 'enable'. */
801 enum ofputil_protocol
802 ofputil_protocol_set_tid(enum ofputil_protocol protocol
, bool enable
)
805 case OFPUTIL_P_OF10_STD
:
806 case OFPUTIL_P_OF10_STD_TID
:
807 return enable
? OFPUTIL_P_OF10_STD_TID
: OFPUTIL_P_OF10_STD
;
809 case OFPUTIL_P_OF10_NXM
:
810 case OFPUTIL_P_OF10_NXM_TID
:
811 return enable
? OFPUTIL_P_OF10_NXM_TID
: OFPUTIL_P_OF10_NXM
;
813 case OFPUTIL_P_OF11_STD
:
814 return OFPUTIL_P_OF11_STD
;
816 case OFPUTIL_P_OF12_OXM
:
817 return OFPUTIL_P_OF12_OXM
;
819 case OFPUTIL_P_OF13_OXM
:
820 return OFPUTIL_P_OF13_OXM
;
822 case OFPUTIL_P_OF14_OXM
:
823 return OFPUTIL_P_OF14_OXM
;
825 case OFPUTIL_P_OF15_OXM
:
826 return OFPUTIL_P_OF15_OXM
;
828 case OFPUTIL_P_OF16_OXM
:
829 return OFPUTIL_P_OF16_OXM
;
836 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
837 * some extension to a standard protocol version, the return value is the
838 * standard version of that protocol without any extension. If 'protocol' is a
839 * standard protocol version, returns 'protocol' unchanged. */
840 enum ofputil_protocol
841 ofputil_protocol_to_base(enum ofputil_protocol protocol
)
843 return ofputil_protocol_set_tid(protocol
, false);
846 /* Returns 'new_base' with any extensions taken from 'cur'. */
847 enum ofputil_protocol
848 ofputil_protocol_set_base(enum ofputil_protocol cur
,
849 enum ofputil_protocol new_base
)
851 bool tid
= (cur
& OFPUTIL_P_TID
) != 0;
854 case OFPUTIL_P_OF10_STD
:
855 case OFPUTIL_P_OF10_STD_TID
:
856 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD
, tid
);
858 case OFPUTIL_P_OF10_NXM
:
859 case OFPUTIL_P_OF10_NXM_TID
:
860 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM
, tid
);
862 case OFPUTIL_P_OF11_STD
:
863 return ofputil_protocol_set_tid(OFPUTIL_P_OF11_STD
, tid
);
865 case OFPUTIL_P_OF12_OXM
:
866 return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM
, tid
);
868 case OFPUTIL_P_OF13_OXM
:
869 return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM
, tid
);
871 case OFPUTIL_P_OF14_OXM
:
872 return ofputil_protocol_set_tid(OFPUTIL_P_OF14_OXM
, tid
);
874 case OFPUTIL_P_OF15_OXM
:
875 return ofputil_protocol_set_tid(OFPUTIL_P_OF15_OXM
, tid
);
877 case OFPUTIL_P_OF16_OXM
:
878 return ofputil_protocol_set_tid(OFPUTIL_P_OF16_OXM
, tid
);
885 /* Returns a string form of 'protocol', if a simple form exists (that is, if
886 * 'protocol' is either a single protocol or it is a combination of protocols
887 * that have a single abbreviation). Otherwise, returns NULL. */
889 ofputil_protocol_to_string(enum ofputil_protocol protocol
)
891 const struct proto_abbrev
*p
;
893 /* Use a "switch" statement for single-bit names so that we get a compiler
894 * warning if we forget any. */
896 case OFPUTIL_P_OF10_NXM
:
897 return "NXM-table_id";
899 case OFPUTIL_P_OF10_NXM_TID
:
900 return "NXM+table_id";
902 case OFPUTIL_P_OF10_STD
:
903 return "OpenFlow10-table_id";
905 case OFPUTIL_P_OF10_STD_TID
:
906 return "OpenFlow10+table_id";
908 case OFPUTIL_P_OF11_STD
:
911 case OFPUTIL_P_OF12_OXM
:
912 return "OXM-OpenFlow12";
914 case OFPUTIL_P_OF13_OXM
:
915 return "OXM-OpenFlow13";
917 case OFPUTIL_P_OF14_OXM
:
918 return "OXM-OpenFlow14";
920 case OFPUTIL_P_OF15_OXM
:
921 return "OXM-OpenFlow15";
923 case OFPUTIL_P_OF16_OXM
:
924 return "OXM-OpenFlow16";
927 /* Check abbreviations. */
928 for (p
= proto_abbrevs
; p
< &proto_abbrevs
[N_PROTO_ABBREVS
]; p
++) {
929 if (protocol
== p
->protocol
) {
937 /* Returns a string that represents 'protocols'. The return value might be a
938 * comma-separated list if 'protocols' doesn't have a simple name. The return
939 * value is "none" if 'protocols' is 0.
941 * The caller must free the returned string (with free()). */
943 ofputil_protocols_to_string(enum ofputil_protocol protocols
)
947 ovs_assert(!(protocols
& ~OFPUTIL_P_ANY
));
948 if (protocols
== 0) {
949 return xstrdup("none");
954 const struct proto_abbrev
*p
;
958 ds_put_char(&s
, ',');
961 for (p
= proto_abbrevs
; p
< &proto_abbrevs
[N_PROTO_ABBREVS
]; p
++) {
962 if ((protocols
& p
->protocol
) == p
->protocol
) {
963 ds_put_cstr(&s
, p
->name
);
964 protocols
&= ~p
->protocol
;
969 for (i
= 0; i
< CHAR_BIT
* sizeof(enum ofputil_protocol
); i
++) {
970 enum ofputil_protocol bit
= 1u << i
;
972 if (protocols
& bit
) {
973 ds_put_cstr(&s
, ofputil_protocol_to_string(bit
));
982 return ds_steal_cstr(&s
);
985 static enum ofputil_protocol
986 ofputil_protocol_from_string__(const char *s
, size_t n
)
988 const struct proto_abbrev
*p
;
991 for (i
= 0; i
< CHAR_BIT
* sizeof(enum ofputil_protocol
); i
++) {
992 enum ofputil_protocol bit
= 1u << i
;
993 const char *name
= ofputil_protocol_to_string(bit
);
995 if (name
&& n
== strlen(name
) && !strncasecmp(s
, name
, n
)) {
1000 for (p
= proto_abbrevs
; p
< &proto_abbrevs
[N_PROTO_ABBREVS
]; p
++) {
1001 if (n
== strlen(p
->name
) && !strncasecmp(s
, p
->name
, n
)) {
1009 /* Returns the nonempty set of protocols represented by 's', which can be a
1010 * single protocol name or abbreviation or a comma-separated list of them.
1012 * Aborts the program with an error message if 's' is invalid. */
1013 enum ofputil_protocol
1014 ofputil_protocols_from_string(const char *s
)
1016 const char *orig_s
= s
;
1017 enum ofputil_protocol protocols
;
1021 enum ofputil_protocol p
;
1024 n
= strcspn(s
, ",");
1030 p
= ofputil_protocol_from_string__(s
, n
);
1032 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n
, s
);
1040 ovs_fatal(0, "%s: no flow protocol specified", orig_s
);
1046 ofputil_version_from_string(const char *s
)
1048 if (!strcasecmp(s
, "OpenFlow10")) {
1049 return OFP10_VERSION
;
1051 if (!strcasecmp(s
, "OpenFlow11")) {
1052 return OFP11_VERSION
;
1054 if (!strcasecmp(s
, "OpenFlow12")) {
1055 return OFP12_VERSION
;
1057 if (!strcasecmp(s
, "OpenFlow13")) {
1058 return OFP13_VERSION
;
1060 if (!strcasecmp(s
, "OpenFlow14")) {
1061 return OFP14_VERSION
;
1063 if (!strcasecmp(s
, "OpenFlow15")) {
1064 return OFP15_VERSION
;
1066 if (!strcasecmp(s
, "OpenFlow16")) {
1067 return OFP16_VERSION
;
1073 is_delimiter(unsigned char c
)
1075 return isspace(c
) || c
== ',';
1079 ofputil_versions_from_string(const char *s
)
1082 uint32_t bitmap
= 0;
1089 if (is_delimiter(s
[i
])) {
1094 while (s
[i
+ j
] && !is_delimiter(s
[i
+ j
])) {
1097 key
= xmemdup0(s
+ i
, j
);
1098 version
= ofputil_version_from_string(key
);
1100 VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key
);
1103 bitmap
|= 1u << version
;
1111 ofputil_versions_from_strings(char ** const s
, size_t count
)
1113 uint32_t bitmap
= 0;
1116 int version
= ofputil_version_from_string(s
[count
]);
1118 VLOG_WARN("Unknown OpenFlow version: \"%s\"", s
[count
]);
1120 bitmap
|= 1u << version
;
1128 ofputil_version_to_string(enum ofp_version ofp_version
)
1130 switch (ofp_version
) {
1132 return "OpenFlow10";
1134 return "OpenFlow11";
1136 return "OpenFlow12";
1138 return "OpenFlow13";
1140 return "OpenFlow14";
1142 return "OpenFlow15";
1144 return "OpenFlow16";
1151 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format
)
1153 switch (packet_in_format
) {
1154 case NXPIF_STANDARD
:
1155 case NXPIF_NXT_PACKET_IN
:
1156 case NXPIF_NXT_PACKET_IN2
:
1164 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format
)
1166 switch (packet_in_format
) {
1167 case NXPIF_STANDARD
:
1169 case NXPIF_NXT_PACKET_IN
:
1170 return "nxt_packet_in";
1171 case NXPIF_NXT_PACKET_IN2
:
1172 return "nxt_packet_in2";
1179 ofputil_packet_in_format_from_string(const char *s
)
1181 return (!strcmp(s
, "standard") || !strcmp(s
, "openflow10")
1183 : !strcmp(s
, "nxt_packet_in") || !strcmp(s
, "nxm")
1184 ? NXPIF_NXT_PACKET_IN
1185 : !strcmp(s
, "nxt_packet_in2")
1186 ? NXPIF_NXT_PACKET_IN2
1191 ofputil_format_version(struct ds
*msg
, enum ofp_version version
)
1193 ds_put_format(msg
, "0x%02x", version
);
1197 ofputil_format_version_name(struct ds
*msg
, enum ofp_version version
)
1199 ds_put_cstr(msg
, ofputil_version_to_string(version
));
1203 ofputil_format_version_bitmap__(struct ds
*msg
, uint32_t bitmap
,
1204 void (*format_version
)(struct ds
*msg
,
1208 format_version(msg
, raw_ctz(bitmap
));
1209 bitmap
= zero_rightmost_1bit(bitmap
);
1211 ds_put_cstr(msg
, ", ");
1217 ofputil_format_version_bitmap(struct ds
*msg
, uint32_t bitmap
)
1219 ofputil_format_version_bitmap__(msg
, bitmap
, ofputil_format_version
);
1223 ofputil_format_version_bitmap_names(struct ds
*msg
, uint32_t bitmap
)
1225 ofputil_format_version_bitmap__(msg
, bitmap
, ofputil_format_version_name
);
1229 ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header
*oheh
,
1230 uint32_t *allowed_versionsp
)
1232 uint16_t bitmap_len
= ntohs(oheh
->length
) - sizeof *oheh
;
1233 const ovs_be32
*bitmap
= ALIGNED_CAST(const ovs_be32
*, oheh
+ 1);
1234 uint32_t allowed_versions
;
1236 if (!bitmap_len
|| bitmap_len
% sizeof *bitmap
) {
1240 /* Only use the first 32-bit element of the bitmap as that is all the
1241 * current implementation supports. Subsequent elements are ignored which
1242 * should have no effect on session negotiation until Open vSwitch supports
1243 * wire-protocol versions greater than 31.
1245 allowed_versions
= ntohl(bitmap
[0]);
1247 if (allowed_versions
& 1) {
1248 /* There's no OpenFlow version 0. */
1249 VLOG_WARN_RL(&bad_ofmsg_rl
, "peer claims to support invalid OpenFlow "
1251 allowed_versions
&= ~1u;
1254 if (!allowed_versions
) {
1255 VLOG_WARN_RL(&bad_ofmsg_rl
, "peer does not support any OpenFlow "
1256 "version (between 0x01 and 0x1f)");
1260 *allowed_versionsp
= allowed_versions
;
1265 version_bitmap_from_version(uint8_t ofp_version
)
1267 return ((ofp_version
< 32 ? 1u << ofp_version
: 0) - 1) << 1;
1270 /* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions'
1271 * the set of OpenFlow versions for which 'oh' announces support.
1273 * Because of how OpenFlow defines OFPT_HELLO messages, this function is always
1274 * successful, and thus '*allowed_versions' is always initialized. However, it
1275 * returns false if 'oh' contains some data that could not be fully understood,
1276 * true if 'oh' was completely parsed. */
1278 ofputil_decode_hello(const struct ofp_header
*oh
, uint32_t *allowed_versions
)
1280 struct ofpbuf msg
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
1281 ofpbuf_pull(&msg
, sizeof *oh
);
1283 *allowed_versions
= version_bitmap_from_version(oh
->version
);
1287 const struct ofp_hello_elem_header
*oheh
;
1290 if (msg
.size
< sizeof *oheh
) {
1295 len
= ntohs(oheh
->length
);
1296 if (len
< sizeof *oheh
|| !ofpbuf_try_pull(&msg
, ROUND_UP(len
, 8))) {
1300 if (oheh
->type
!= htons(OFPHET_VERSIONBITMAP
)
1301 || !ofputil_decode_hello_bitmap(oheh
, allowed_versions
)) {
1309 /* Returns true if 'allowed_versions' needs to be accompanied by a version
1310 * bitmap to be correctly expressed in an OFPT_HELLO message. */
1312 should_send_version_bitmap(uint32_t allowed_versions
)
1314 return !is_pow2((allowed_versions
>> 1) + 1);
1317 /* Create an OFPT_HELLO message that expresses support for the OpenFlow
1318 * versions in the 'allowed_versions' bitmaps and returns the message. */
1320 ofputil_encode_hello(uint32_t allowed_versions
)
1322 enum ofp_version ofp_version
;
1325 ofp_version
= leftmost_1bit_idx(allowed_versions
);
1326 msg
= ofpraw_alloc(OFPRAW_OFPT_HELLO
, ofp_version
, 0);
1328 if (should_send_version_bitmap(allowed_versions
)) {
1329 struct ofp_hello_elem_header
*oheh
;
1332 map_len
= sizeof allowed_versions
;
1333 oheh
= ofpbuf_put_zeros(msg
, ROUND_UP(map_len
+ sizeof *oheh
, 8));
1334 oheh
->type
= htons(OFPHET_VERSIONBITMAP
);
1335 oheh
->length
= htons(map_len
+ sizeof *oheh
);
1336 *ALIGNED_CAST(ovs_be32
*, oheh
+ 1) = htonl(allowed_versions
);
1338 ofpmsg_update_length(msg
);
1344 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1345 * protocol is 'current', at least partly transitions the protocol to 'want'.
1346 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1347 * connection if the switch processes the returned message correctly. (If
1348 * '*next != want' then the caller will have to iterate.)
1350 * If 'current == want', or if it is not possible to transition from 'current'
1351 * to 'want' (because, for example, 'current' and 'want' use different OpenFlow
1352 * protocol versions), returns NULL and stores 'current' in '*next'. */
1354 ofputil_encode_set_protocol(enum ofputil_protocol current
,
1355 enum ofputil_protocol want
,
1356 enum ofputil_protocol
*next
)
1358 enum ofp_version cur_version
, want_version
;
1359 enum ofputil_protocol cur_base
, want_base
;
1360 bool cur_tid
, want_tid
;
1362 cur_version
= ofputil_protocol_to_ofp_version(current
);
1363 want_version
= ofputil_protocol_to_ofp_version(want
);
1364 if (cur_version
!= want_version
) {
1369 cur_base
= ofputil_protocol_to_base(current
);
1370 want_base
= ofputil_protocol_to_base(want
);
1371 if (cur_base
!= want_base
) {
1372 *next
= ofputil_protocol_set_base(current
, want_base
);
1374 switch (want_base
) {
1375 case OFPUTIL_P_OF10_NXM
:
1376 return ofputil_encode_nx_set_flow_format(NXFF_NXM
);
1378 case OFPUTIL_P_OF10_STD
:
1379 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10
);
1381 case OFPUTIL_P_OF11_STD
:
1382 case OFPUTIL_P_OF12_OXM
:
1383 case OFPUTIL_P_OF13_OXM
:
1384 case OFPUTIL_P_OF14_OXM
:
1385 case OFPUTIL_P_OF15_OXM
:
1386 case OFPUTIL_P_OF16_OXM
:
1387 /* There is only one variant of each OpenFlow 1.1+ protocol, and we
1388 * verified above that we're not trying to change versions. */
1391 case OFPUTIL_P_OF10_STD_TID
:
1392 case OFPUTIL_P_OF10_NXM_TID
:
1397 cur_tid
= (current
& OFPUTIL_P_TID
) != 0;
1398 want_tid
= (want
& OFPUTIL_P_TID
) != 0;
1399 if (cur_tid
!= want_tid
) {
1400 *next
= ofputil_protocol_set_tid(current
, want_tid
);
1401 return ofputil_make_flow_mod_table_id(want_tid
);
1404 ovs_assert(current
== want
);
1410 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1411 * format to 'nxff'. */
1413 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff
)
1415 struct nx_set_flow_format
*sff
;
1418 ovs_assert(ofputil_nx_flow_format_is_valid(nxff
));
1420 msg
= ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT
, OFP10_VERSION
, 0);
1421 sff
= ofpbuf_put_zeros(msg
, sizeof *sff
);
1422 sff
->format
= htonl(nxff
);
1427 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1429 enum ofputil_protocol
1430 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format
)
1432 switch (flow_format
) {
1433 case NXFF_OPENFLOW10
:
1434 return OFPUTIL_P_OF10_STD
;
1437 return OFPUTIL_P_OF10_NXM
;
1444 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1446 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format
)
1448 return ofputil_nx_flow_format_to_protocol(flow_format
) != 0;
1451 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1454 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format
)
1456 switch (flow_format
) {
1457 case NXFF_OPENFLOW10
:
1458 return "openflow10";
1467 ofputil_make_set_packet_in_format(enum ofp_version ofp_version
,
1468 enum nx_packet_in_format packet_in_format
)
1470 struct nx_set_packet_in_format
*spif
;
1473 msg
= ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT
, ofp_version
, 0);
1474 spif
= ofpbuf_put_zeros(msg
, sizeof *spif
);
1475 spif
->format
= htonl(packet_in_format
);
1480 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1481 * extension on or off (according to 'flow_mod_table_id'). */
1483 ofputil_make_flow_mod_table_id(bool flow_mod_table_id
)
1485 struct nx_flow_mod_table_id
*nfmti
;
1488 msg
= ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID
, OFP10_VERSION
, 0);
1489 nfmti
= ofpbuf_put_zeros(msg
, sizeof *nfmti
);
1490 nfmti
->set
= flow_mod_table_id
;
1494 struct ofputil_flow_mod_flag
{
1496 enum ofp_version min_version
, max_version
;
1497 enum ofputil_flow_mod_flags flag
;
1500 static const struct ofputil_flow_mod_flag ofputil_flow_mod_flags
[] = {
1501 { OFPFF_SEND_FLOW_REM
, OFP10_VERSION
, 0, OFPUTIL_FF_SEND_FLOW_REM
},
1502 { OFPFF_CHECK_OVERLAP
, OFP10_VERSION
, 0, OFPUTIL_FF_CHECK_OVERLAP
},
1503 { OFPFF10_EMERG
, OFP10_VERSION
, OFP10_VERSION
,
1505 { OFPFF12_RESET_COUNTS
, OFP12_VERSION
, 0, OFPUTIL_FF_RESET_COUNTS
},
1506 { OFPFF13_NO_PKT_COUNTS
, OFP13_VERSION
, 0, OFPUTIL_FF_NO_PKT_COUNTS
},
1507 { OFPFF13_NO_BYT_COUNTS
, OFP13_VERSION
, 0, OFPUTIL_FF_NO_BYT_COUNTS
},
1512 ofputil_decode_flow_mod_flags(ovs_be16 raw_flags_
,
1513 enum ofp_flow_mod_command command
,
1514 enum ofp_version version
,
1515 enum ofputil_flow_mod_flags
*flagsp
)
1517 uint16_t raw_flags
= ntohs(raw_flags_
);
1518 const struct ofputil_flow_mod_flag
*f
;
1521 for (f
= ofputil_flow_mod_flags
; f
->raw_flag
; f
++) {
1522 if (raw_flags
& f
->raw_flag
1523 && version
>= f
->min_version
1524 && (!f
->max_version
|| version
<= f
->max_version
)) {
1525 raw_flags
&= ~f
->raw_flag
;
1530 /* In OF1.0 and OF1.1, "add" always resets counters, and other commands
1533 * In OF1.2 and later, OFPFF12_RESET_COUNTS controls whether each command
1534 * resets counters. */
1535 if ((version
== OFP10_VERSION
|| version
== OFP11_VERSION
)
1536 && command
== OFPFC_ADD
) {
1537 *flagsp
|= OFPUTIL_FF_RESET_COUNTS
;
1540 return raw_flags
? OFPERR_OFPFMFC_BAD_FLAGS
: 0;
1544 ofputil_encode_flow_mod_flags(enum ofputil_flow_mod_flags flags
,
1545 enum ofp_version version
)
1547 const struct ofputil_flow_mod_flag
*f
;
1551 for (f
= ofputil_flow_mod_flags
; f
->raw_flag
; f
++) {
1553 && version
>= f
->min_version
1554 && (!f
->max_version
|| version
<= f
->max_version
)) {
1555 raw_flags
|= f
->raw_flag
;
1559 return htons(raw_flags
);
1562 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1563 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1566 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1567 * The caller must initialize 'ofpacts' and retains ownership of it.
1568 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1570 * Does not validate the flow_mod actions. The caller should do that, with
1571 * ofpacts_check(). */
1573 ofputil_decode_flow_mod(struct ofputil_flow_mod
*fm
,
1574 const struct ofp_header
*oh
,
1575 enum ofputil_protocol protocol
,
1576 const struct tun_table
*tun_table
,
1577 const struct vl_mff_map
*vl_mff_map
,
1578 struct ofpbuf
*ofpacts
,
1579 ofp_port_t max_port
, uint8_t max_table
)
1583 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
1584 enum ofpraw raw
= ofpraw_pull_assert(&b
);
1585 if (raw
== OFPRAW_OFPT11_FLOW_MOD
) {
1586 /* Standard OpenFlow 1.1+ flow_mod. */
1587 const struct ofp11_flow_mod
*ofm
;
1589 ofm
= ofpbuf_pull(&b
, sizeof *ofm
);
1591 error
= ofputil_pull_ofp11_match(&b
, tun_table
, vl_mff_map
, &fm
->match
,
1597 /* Translate the message. */
1598 fm
->priority
= ntohs(ofm
->priority
);
1599 if (ofm
->command
== OFPFC_ADD
1600 || (oh
->version
== OFP11_VERSION
1601 && (ofm
->command
== OFPFC_MODIFY
||
1602 ofm
->command
== OFPFC_MODIFY_STRICT
)
1603 && ofm
->cookie_mask
== htonll(0))) {
1604 /* In OpenFlow 1.1 only, a "modify" or "modify-strict" that does
1605 * not match on the cookie is treated as an "add" if there is no
1607 fm
->cookie
= htonll(0);
1608 fm
->cookie_mask
= htonll(0);
1609 fm
->new_cookie
= ofm
->cookie
;
1611 fm
->cookie
= ofm
->cookie
;
1612 fm
->cookie_mask
= ofm
->cookie_mask
;
1613 fm
->new_cookie
= OVS_BE64_MAX
;
1615 fm
->modify_cookie
= false;
1616 fm
->command
= ofm
->command
;
1620 * OF1.1 entirely forbids table_id == OFPTT_ALL.
1621 * OF1.2+ allows table_id == OFPTT_ALL only for deletes. */
1622 fm
->table_id
= ofm
->table_id
;
1623 if (fm
->table_id
== OFPTT_ALL
1624 && (oh
->version
== OFP11_VERSION
1625 || (ofm
->command
!= OFPFC_DELETE
&&
1626 ofm
->command
!= OFPFC_DELETE_STRICT
))) {
1627 return OFPERR_OFPFMFC_BAD_TABLE_ID
;
1630 fm
->idle_timeout
= ntohs(ofm
->idle_timeout
);
1631 fm
->hard_timeout
= ntohs(ofm
->hard_timeout
);
1632 if (oh
->version
>= OFP14_VERSION
&& ofm
->command
== OFPFC_ADD
) {
1633 fm
->importance
= ntohs(ofm
->importance
);
1637 fm
->buffer_id
= ntohl(ofm
->buffer_id
);
1638 error
= ofputil_port_from_ofp11(ofm
->out_port
, &fm
->out_port
);
1643 fm
->out_group
= (ofm
->command
== OFPFC_DELETE
||
1644 ofm
->command
== OFPFC_DELETE_STRICT
1645 ? ntohl(ofm
->out_group
)
1647 raw_flags
= ofm
->flags
;
1651 if (raw
== OFPRAW_OFPT10_FLOW_MOD
) {
1652 /* Standard OpenFlow 1.0 flow_mod. */
1653 const struct ofp10_flow_mod
*ofm
;
1655 /* Get the ofp10_flow_mod. */
1656 ofm
= ofpbuf_pull(&b
, sizeof *ofm
);
1658 /* Translate the rule. */
1659 ofputil_match_from_ofp10_match(&ofm
->match
, &fm
->match
);
1660 ofputil_normalize_match(&fm
->match
);
1662 /* OpenFlow 1.0 says that exact-match rules have to have the
1663 * highest possible priority. */
1664 fm
->priority
= (ofm
->match
.wildcards
& htonl(OFPFW10_ALL
)
1665 ? ntohs(ofm
->priority
)
1668 /* Translate the message. */
1669 command
= ntohs(ofm
->command
);
1670 fm
->cookie
= htonll(0);
1671 fm
->cookie_mask
= htonll(0);
1672 fm
->new_cookie
= ofm
->cookie
;
1673 fm
->idle_timeout
= ntohs(ofm
->idle_timeout
);
1674 fm
->hard_timeout
= ntohs(ofm
->hard_timeout
);
1676 fm
->buffer_id
= ntohl(ofm
->buffer_id
);
1677 fm
->out_port
= u16_to_ofp(ntohs(ofm
->out_port
));
1678 fm
->out_group
= OFPG_ANY
;
1679 raw_flags
= ofm
->flags
;
1680 } else if (raw
== OFPRAW_NXT_FLOW_MOD
) {
1681 /* Nicira extended flow_mod. */
1682 const struct nx_flow_mod
*nfm
;
1684 /* Dissect the message. */
1685 nfm
= ofpbuf_pull(&b
, sizeof *nfm
);
1686 error
= nx_pull_match(&b
, ntohs(nfm
->match_len
),
1687 &fm
->match
, &fm
->cookie
, &fm
->cookie_mask
,
1688 tun_table
, vl_mff_map
);
1693 /* Translate the message. */
1694 command
= ntohs(nfm
->command
);
1695 if ((command
& 0xff) == OFPFC_ADD
&& fm
->cookie_mask
) {
1696 /* Flow additions may only set a new cookie, not match an
1697 * existing cookie. */
1698 return OFPERR_NXBRC_NXM_INVALID
;
1700 fm
->priority
= ntohs(nfm
->priority
);
1701 fm
->new_cookie
= nfm
->cookie
;
1702 fm
->idle_timeout
= ntohs(nfm
->idle_timeout
);
1703 fm
->hard_timeout
= ntohs(nfm
->hard_timeout
);
1705 fm
->buffer_id
= ntohl(nfm
->buffer_id
);
1706 fm
->out_port
= u16_to_ofp(ntohs(nfm
->out_port
));
1707 fm
->out_group
= OFPG_ANY
;
1708 raw_flags
= nfm
->flags
;
1713 fm
->modify_cookie
= fm
->new_cookie
!= OVS_BE64_MAX
;
1714 if (protocol
& OFPUTIL_P_TID
) {
1715 fm
->command
= command
& 0xff;
1716 fm
->table_id
= command
>> 8;
1718 if (command
> 0xff) {
1719 VLOG_WARN_RL(&bad_ofmsg_rl
, "flow_mod has explicit table_id "
1720 "but flow_mod_table_id extension is not enabled");
1722 fm
->command
= command
;
1723 fm
->table_id
= 0xff;
1727 /* Check for mismatched conntrack original direction tuple address fields
1728 * w.r.t. the IP version of the match. */
1729 if (((fm
->match
.wc
.masks
.ct_nw_src
|| fm
->match
.wc
.masks
.ct_nw_dst
)
1730 && fm
->match
.flow
.dl_type
!= htons(ETH_TYPE_IP
))
1731 || ((ipv6_addr_is_set(&fm
->match
.wc
.masks
.ct_ipv6_src
)
1732 || ipv6_addr_is_set(&fm
->match
.wc
.masks
.ct_ipv6_dst
))
1733 && fm
->match
.flow
.dl_type
!= htons(ETH_TYPE_IPV6
))) {
1734 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
1737 if (fm
->command
> OFPFC_DELETE_STRICT
) {
1738 return OFPERR_OFPFMFC_BAD_COMMAND
;
1741 fm
->ofpacts_tlv_bitmap
= 0;
1742 error
= ofpacts_pull_openflow_instructions(&b
, b
.size
, oh
->version
,
1744 &fm
->ofpacts_tlv_bitmap
,
1749 fm
->ofpacts
= ofpacts
->data
;
1750 fm
->ofpacts_len
= ofpacts
->size
;
1752 error
= ofputil_decode_flow_mod_flags(raw_flags
, fm
->command
,
1753 oh
->version
, &fm
->flags
);
1758 if (fm
->flags
& OFPUTIL_FF_EMERG
) {
1759 /* We do not support the OpenFlow 1.0 emergency flow cache, which
1760 * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
1762 * OpenFlow 1.0 specifies the error code to use when idle_timeout
1763 * or hard_timeout is nonzero. Otherwise, there is no good error
1764 * code, so just state that the flow table is full. */
1765 return (fm
->hard_timeout
|| fm
->idle_timeout
1766 ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT
1767 : OFPERR_OFPFMFC_TABLE_FULL
);
1770 return ofpacts_check_consistency(fm
->ofpacts
, fm
->ofpacts_len
,
1771 &fm
->match
, max_port
,
1772 fm
->table_id
, max_table
, protocol
);
1776 ofputil_pull_bands(struct ofpbuf
*msg
, size_t len
, uint16_t *n_bands
,
1777 struct ofpbuf
*bands
)
1779 const struct ofp13_meter_band_header
*ombh
;
1780 struct ofputil_meter_band
*mb
;
1783 ombh
= ofpbuf_try_pull(msg
, len
);
1785 return OFPERR_OFPBRC_BAD_LEN
;
1788 while (len
>= sizeof (struct ofp13_meter_band_drop
)) {
1789 size_t ombh_len
= ntohs(ombh
->len
);
1790 /* All supported band types have the same length. */
1791 if (ombh_len
!= sizeof (struct ofp13_meter_band_drop
)) {
1792 return OFPERR_OFPBRC_BAD_LEN
;
1794 mb
= ofpbuf_put_uninit(bands
, sizeof *mb
);
1795 mb
->type
= ntohs(ombh
->type
);
1796 if (mb
->type
!= OFPMBT13_DROP
&& mb
->type
!= OFPMBT13_DSCP_REMARK
) {
1797 return OFPERR_OFPMMFC_BAD_BAND
;
1799 mb
->rate
= ntohl(ombh
->rate
);
1800 mb
->burst_size
= ntohl(ombh
->burst_size
);
1801 mb
->prec_level
= (mb
->type
== OFPMBT13_DSCP_REMARK
) ?
1802 ((struct ofp13_meter_band_dscp_remark
*)ombh
)->prec_level
: 0;
1805 ombh
= ALIGNED_CAST(struct ofp13_meter_band_header
*,
1806 (char *) ombh
+ ombh_len
);
1809 return OFPERR_OFPBRC_BAD_LEN
;
1816 ofputil_decode_meter_mod(const struct ofp_header
*oh
,
1817 struct ofputil_meter_mod
*mm
,
1818 struct ofpbuf
*bands
)
1820 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
1821 ofpraw_pull_assert(&b
);
1822 const struct ofp13_meter_mod
*omm
= ofpbuf_pull(&b
, sizeof *omm
);
1824 /* Translate the message. */
1825 mm
->command
= ntohs(omm
->command
);
1826 if (mm
->command
!= OFPMC13_ADD
&&
1827 mm
->command
!= OFPMC13_MODIFY
&&
1828 mm
->command
!= OFPMC13_DELETE
) {
1829 return OFPERR_OFPMMFC_BAD_COMMAND
;
1831 mm
->meter
.meter_id
= ntohl(omm
->meter_id
);
1833 if (mm
->command
== OFPMC13_DELETE
) {
1834 mm
->meter
.flags
= 0;
1835 mm
->meter
.n_bands
= 0;
1836 mm
->meter
.bands
= NULL
;
1840 mm
->meter
.flags
= ntohs(omm
->flags
);
1841 if (mm
->meter
.flags
& OFPMF13_KBPS
&&
1842 mm
->meter
.flags
& OFPMF13_PKTPS
) {
1843 return OFPERR_OFPMMFC_BAD_FLAGS
;
1845 mm
->meter
.bands
= bands
->data
;
1847 error
= ofputil_pull_bands(&b
, b
.size
, &mm
->meter
.n_bands
, bands
);
1856 ofputil_decode_meter_request(const struct ofp_header
*oh
, uint32_t *meter_id
)
1858 const struct ofp13_meter_multipart_request
*omr
= ofpmsg_body(oh
);
1859 *meter_id
= ntohl(omr
->meter_id
);
1863 ofputil_encode_meter_request(enum ofp_version ofp_version
,
1864 enum ofputil_meter_request_type type
,
1872 case OFPUTIL_METER_CONFIG
:
1873 raw
= OFPRAW_OFPST13_METER_CONFIG_REQUEST
;
1875 case OFPUTIL_METER_STATS
:
1876 raw
= OFPRAW_OFPST13_METER_REQUEST
;
1879 case OFPUTIL_METER_FEATURES
:
1880 raw
= OFPRAW_OFPST13_METER_FEATURES_REQUEST
;
1884 msg
= ofpraw_alloc(raw
, ofp_version
, 0);
1886 if (type
!= OFPUTIL_METER_FEATURES
) {
1887 struct ofp13_meter_multipart_request
*omr
;
1888 omr
= ofpbuf_put_zeros(msg
, sizeof *omr
);
1889 omr
->meter_id
= htonl(meter_id
);
1895 ofputil_put_bands(uint16_t n_bands
, const struct ofputil_meter_band
*mb
,
1900 for (n
= 0; n
< n_bands
; ++n
) {
1901 /* Currently all band types have same size. */
1902 struct ofp13_meter_band_dscp_remark
*ombh
;
1903 size_t ombh_len
= sizeof *ombh
;
1905 ombh
= ofpbuf_put_zeros(msg
, ombh_len
);
1907 ombh
->type
= htons(mb
->type
);
1908 ombh
->len
= htons(ombh_len
);
1909 ombh
->rate
= htonl(mb
->rate
);
1910 ombh
->burst_size
= htonl(mb
->burst_size
);
1911 ombh
->prec_level
= mb
->prec_level
;
1917 /* Encode a meter stat for 'mc' and append it to 'replies'. */
1919 ofputil_append_meter_config(struct ovs_list
*replies
,
1920 const struct ofputil_meter_config
*mc
)
1922 struct ofpbuf
*msg
= ofpbuf_from_list(ovs_list_back(replies
));
1923 size_t start_ofs
= msg
->size
;
1924 struct ofp13_meter_config
*reply
;
1926 ofpbuf_put_uninit(msg
, sizeof *reply
);
1927 ofputil_put_bands(mc
->n_bands
, mc
->bands
, msg
);
1929 reply
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *reply
);
1930 reply
->flags
= htons(mc
->flags
);
1931 reply
->meter_id
= htonl(mc
->meter_id
);
1932 reply
->length
= htons(msg
->size
- start_ofs
);
1934 ofpmp_postappend(replies
, start_ofs
);
1937 /* Encode a meter stat for 'ms' and append it to 'replies'. */
1939 ofputil_append_meter_stats(struct ovs_list
*replies
,
1940 const struct ofputil_meter_stats
*ms
)
1942 struct ofp13_meter_stats
*reply
;
1946 len
= sizeof *reply
+ ms
->n_bands
* sizeof(struct ofp13_meter_band_stats
);
1947 reply
= ofpmp_append(replies
, len
);
1949 reply
->meter_id
= htonl(ms
->meter_id
);
1950 reply
->len
= htons(len
);
1951 memset(reply
->pad
, 0, sizeof reply
->pad
);
1952 reply
->flow_count
= htonl(ms
->flow_count
);
1953 reply
->packet_in_count
= htonll(ms
->packet_in_count
);
1954 reply
->byte_in_count
= htonll(ms
->byte_in_count
);
1955 reply
->duration_sec
= htonl(ms
->duration_sec
);
1956 reply
->duration_nsec
= htonl(ms
->duration_nsec
);
1958 for (n
= 0; n
< ms
->n_bands
; ++n
) {
1959 const struct ofputil_meter_band_stats
*src
= &ms
->bands
[n
];
1960 struct ofp13_meter_band_stats
*dst
= &reply
->band_stats
[n
];
1962 dst
->packet_band_count
= htonll(src
->packet_count
);
1963 dst
->byte_band_count
= htonll(src
->byte_count
);
1967 /* Converts an OFPMP_METER_CONFIG reply in 'msg' into an abstract
1968 * ofputil_meter_config in 'mc', with mc->bands pointing to bands decoded into
1969 * 'bands'. The caller must have initialized 'bands' and retains ownership of
1970 * it across the call.
1972 * Multiple OFPST13_METER_CONFIG replies can be packed into a single OpenFlow
1973 * message. Calling this function multiple times for a single 'msg' iterates
1974 * through the replies. 'bands' is cleared for each reply.
1976 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1977 * otherwise a positive errno value. */
1979 ofputil_decode_meter_config(struct ofpbuf
*msg
,
1980 struct ofputil_meter_config
*mc
,
1981 struct ofpbuf
*bands
)
1983 const struct ofp13_meter_config
*omc
;
1986 /* Pull OpenFlow headers for the first call. */
1988 ofpraw_pull_assert(msg
);
1995 omc
= ofpbuf_try_pull(msg
, sizeof *omc
);
1997 VLOG_WARN_RL(&bad_ofmsg_rl
,
1998 "OFPMP_METER_CONFIG reply has %"PRIu32
" leftover bytes at end",
2000 return OFPERR_OFPBRC_BAD_LEN
;
2003 ofpbuf_clear(bands
);
2004 err
= ofputil_pull_bands(msg
, ntohs(omc
->length
) - sizeof *omc
,
2005 &mc
->n_bands
, bands
);
2009 mc
->meter_id
= ntohl(omc
->meter_id
);
2010 mc
->flags
= ntohs(omc
->flags
);
2011 mc
->bands
= bands
->data
;
2017 ofputil_pull_band_stats(struct ofpbuf
*msg
, size_t len
, uint16_t *n_bands
,
2018 struct ofpbuf
*bands
)
2020 const struct ofp13_meter_band_stats
*ombs
;
2021 struct ofputil_meter_band_stats
*mbs
;
2024 ombs
= ofpbuf_try_pull(msg
, len
);
2026 return OFPERR_OFPBRC_BAD_LEN
;
2029 n
= len
/ sizeof *ombs
;
2030 if (len
!= n
* sizeof *ombs
) {
2031 return OFPERR_OFPBRC_BAD_LEN
;
2034 mbs
= ofpbuf_put_uninit(bands
, len
);
2036 for (i
= 0; i
< n
; ++i
) {
2037 mbs
[i
].packet_count
= ntohll(ombs
[i
].packet_band_count
);
2038 mbs
[i
].byte_count
= ntohll(ombs
[i
].byte_band_count
);
2044 /* Converts an OFPMP_METER reply in 'msg' into an abstract
2045 * ofputil_meter_stats in 'ms', with ms->bands pointing to band stats
2046 * decoded into 'bands'.
2048 * Multiple OFPMP_METER replies can be packed into a single OpenFlow
2049 * message. Calling this function multiple times for a single 'msg' iterates
2050 * through the replies. 'bands' is cleared for each reply.
2052 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2053 * otherwise a positive errno value. */
2055 ofputil_decode_meter_stats(struct ofpbuf
*msg
,
2056 struct ofputil_meter_stats
*ms
,
2057 struct ofpbuf
*bands
)
2059 const struct ofp13_meter_stats
*oms
;
2062 /* Pull OpenFlow headers for the first call. */
2064 ofpraw_pull_assert(msg
);
2071 oms
= ofpbuf_try_pull(msg
, sizeof *oms
);
2073 VLOG_WARN_RL(&bad_ofmsg_rl
,
2074 "OFPMP_METER reply has %"PRIu32
" leftover bytes at end",
2076 return OFPERR_OFPBRC_BAD_LEN
;
2079 ofpbuf_clear(bands
);
2080 err
= ofputil_pull_band_stats(msg
, ntohs(oms
->len
) - sizeof *oms
,
2081 &ms
->n_bands
, bands
);
2085 ms
->meter_id
= ntohl(oms
->meter_id
);
2086 ms
->flow_count
= ntohl(oms
->flow_count
);
2087 ms
->packet_in_count
= ntohll(oms
->packet_in_count
);
2088 ms
->byte_in_count
= ntohll(oms
->byte_in_count
);
2089 ms
->duration_sec
= ntohl(oms
->duration_sec
);
2090 ms
->duration_nsec
= ntohl(oms
->duration_nsec
);
2091 ms
->bands
= bands
->data
;
2097 ofputil_decode_meter_features(const struct ofp_header
*oh
,
2098 struct ofputil_meter_features
*mf
)
2100 const struct ofp13_meter_features
*omf
= ofpmsg_body(oh
);
2102 mf
->max_meters
= ntohl(omf
->max_meter
);
2103 mf
->band_types
= ntohl(omf
->band_types
);
2104 mf
->capabilities
= ntohl(omf
->capabilities
);
2105 mf
->max_bands
= omf
->max_bands
;
2106 mf
->max_color
= omf
->max_color
;
2110 ofputil_encode_meter_features_reply(const struct ofputil_meter_features
*mf
,
2111 const struct ofp_header
*request
)
2113 struct ofpbuf
*reply
;
2114 struct ofp13_meter_features
*omf
;
2116 reply
= ofpraw_alloc_stats_reply(request
, 0);
2117 omf
= ofpbuf_put_zeros(reply
, sizeof *omf
);
2119 omf
->max_meter
= htonl(mf
->max_meters
);
2120 omf
->band_types
= htonl(mf
->band_types
);
2121 omf
->capabilities
= htonl(mf
->capabilities
);
2122 omf
->max_bands
= mf
->max_bands
;
2123 omf
->max_color
= mf
->max_color
;
2129 ofputil_encode_meter_mod(enum ofp_version ofp_version
,
2130 const struct ofputil_meter_mod
*mm
)
2134 struct ofp13_meter_mod
*omm
;
2136 msg
= ofpraw_alloc(OFPRAW_OFPT13_METER_MOD
, ofp_version
,
2137 NXM_TYPICAL_LEN
+ mm
->meter
.n_bands
* 16);
2138 omm
= ofpbuf_put_zeros(msg
, sizeof *omm
);
2139 omm
->command
= htons(mm
->command
);
2140 if (mm
->command
!= OFPMC13_DELETE
) {
2141 omm
->flags
= htons(mm
->meter
.flags
);
2143 omm
->meter_id
= htonl(mm
->meter
.meter_id
);
2145 ofputil_put_bands(mm
->meter
.n_bands
, mm
->meter
.bands
, msg
);
2147 ofpmsg_update_length(msg
);
2152 ofputil_tid_command(const struct ofputil_flow_mod
*fm
,
2153 enum ofputil_protocol protocol
)
2155 return htons(protocol
& OFPUTIL_P_TID
2156 ? (fm
->command
& 0xff) | (fm
->table_id
<< 8)
2160 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
2161 * 'protocol' and returns the message. */
2163 ofputil_encode_flow_mod(const struct ofputil_flow_mod
*fm
,
2164 enum ofputil_protocol protocol
)
2166 enum ofp_version version
= ofputil_protocol_to_ofp_version(protocol
);
2167 ovs_be16 raw_flags
= ofputil_encode_flow_mod_flags(fm
->flags
, version
);
2171 case OFPUTIL_P_OF11_STD
:
2172 case OFPUTIL_P_OF12_OXM
:
2173 case OFPUTIL_P_OF13_OXM
:
2174 case OFPUTIL_P_OF14_OXM
:
2175 case OFPUTIL_P_OF15_OXM
:
2176 case OFPUTIL_P_OF16_OXM
: {
2177 struct ofp11_flow_mod
*ofm
;
2180 tailroom
= ofputil_match_typical_len(protocol
) + fm
->ofpacts_len
;
2181 msg
= ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD
, version
, tailroom
);
2182 ofm
= ofpbuf_put_zeros(msg
, sizeof *ofm
);
2183 if ((protocol
== OFPUTIL_P_OF11_STD
2184 && (fm
->command
== OFPFC_MODIFY
||
2185 fm
->command
== OFPFC_MODIFY_STRICT
)
2186 && fm
->cookie_mask
== htonll(0))
2187 || fm
->command
== OFPFC_ADD
) {
2188 ofm
->cookie
= fm
->new_cookie
;
2190 ofm
->cookie
= fm
->cookie
& fm
->cookie_mask
;
2192 ofm
->cookie_mask
= fm
->cookie_mask
;
2193 if (fm
->table_id
!= OFPTT_ALL
2194 || (protocol
!= OFPUTIL_P_OF11_STD
2195 && (fm
->command
== OFPFC_DELETE
||
2196 fm
->command
== OFPFC_DELETE_STRICT
))) {
2197 ofm
->table_id
= fm
->table_id
;
2201 ofm
->command
= fm
->command
;
2202 ofm
->idle_timeout
= htons(fm
->idle_timeout
);
2203 ofm
->hard_timeout
= htons(fm
->hard_timeout
);
2204 ofm
->priority
= htons(fm
->priority
);
2205 ofm
->buffer_id
= htonl(fm
->buffer_id
);
2206 ofm
->out_port
= ofputil_port_to_ofp11(fm
->out_port
);
2207 ofm
->out_group
= htonl(fm
->out_group
);
2208 ofm
->flags
= raw_flags
;
2209 if (version
>= OFP14_VERSION
&& fm
->command
== OFPFC_ADD
) {
2210 ofm
->importance
= htons(fm
->importance
);
2212 ofm
->importance
= 0;
2214 ofputil_put_ofp11_match(msg
, &fm
->match
, protocol
);
2215 ofpacts_put_openflow_instructions(fm
->ofpacts
, fm
->ofpacts_len
, msg
,
2220 case OFPUTIL_P_OF10_STD
:
2221 case OFPUTIL_P_OF10_STD_TID
: {
2222 struct ofp10_flow_mod
*ofm
;
2224 msg
= ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD
, OFP10_VERSION
,
2226 ofm
= ofpbuf_put_zeros(msg
, sizeof *ofm
);
2227 ofputil_match_to_ofp10_match(&fm
->match
, &ofm
->match
);
2228 ofm
->cookie
= fm
->new_cookie
;
2229 ofm
->command
= ofputil_tid_command(fm
, protocol
);
2230 ofm
->idle_timeout
= htons(fm
->idle_timeout
);
2231 ofm
->hard_timeout
= htons(fm
->hard_timeout
);
2232 ofm
->priority
= htons(fm
->priority
);
2233 ofm
->buffer_id
= htonl(fm
->buffer_id
);
2234 ofm
->out_port
= htons(ofp_to_u16(fm
->out_port
));
2235 ofm
->flags
= raw_flags
;
2236 ofpacts_put_openflow_actions(fm
->ofpacts
, fm
->ofpacts_len
, msg
,
2241 case OFPUTIL_P_OF10_NXM
:
2242 case OFPUTIL_P_OF10_NXM_TID
: {
2243 struct nx_flow_mod
*nfm
;
2246 msg
= ofpraw_alloc(OFPRAW_NXT_FLOW_MOD
, OFP10_VERSION
,
2247 NXM_TYPICAL_LEN
+ fm
->ofpacts_len
);
2248 nfm
= ofpbuf_put_zeros(msg
, sizeof *nfm
);
2249 nfm
->command
= ofputil_tid_command(fm
, protocol
);
2250 nfm
->cookie
= fm
->new_cookie
;
2251 match_len
= nx_put_match(msg
, &fm
->match
, fm
->cookie
, fm
->cookie_mask
);
2253 nfm
->idle_timeout
= htons(fm
->idle_timeout
);
2254 nfm
->hard_timeout
= htons(fm
->hard_timeout
);
2255 nfm
->priority
= htons(fm
->priority
);
2256 nfm
->buffer_id
= htonl(fm
->buffer_id
);
2257 nfm
->out_port
= htons(ofp_to_u16(fm
->out_port
));
2258 nfm
->flags
= raw_flags
;
2259 nfm
->match_len
= htons(match_len
);
2260 ofpacts_put_openflow_actions(fm
->ofpacts
, fm
->ofpacts_len
, msg
,
2269 ofpmsg_update_length(msg
);
2274 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request
*fsr
,
2275 const struct ofp10_flow_stats_request
*ofsr
,
2278 fsr
->aggregate
= aggregate
;
2279 ofputil_match_from_ofp10_match(&ofsr
->match
, &fsr
->match
);
2280 fsr
->out_port
= u16_to_ofp(ntohs(ofsr
->out_port
));
2281 fsr
->out_group
= OFPG_ANY
;
2282 fsr
->table_id
= ofsr
->table_id
;
2283 fsr
->cookie
= fsr
->cookie_mask
= htonll(0);
2289 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request
*fsr
,
2290 struct ofpbuf
*b
, bool aggregate
,
2291 const struct tun_table
*tun_table
,
2292 const struct vl_mff_map
*vl_mff_map
)
2294 const struct ofp11_flow_stats_request
*ofsr
;
2297 ofsr
= ofpbuf_pull(b
, sizeof *ofsr
);
2298 fsr
->aggregate
= aggregate
;
2299 fsr
->table_id
= ofsr
->table_id
;
2300 error
= ofputil_port_from_ofp11(ofsr
->out_port
, &fsr
->out_port
);
2304 fsr
->out_group
= ntohl(ofsr
->out_group
);
2305 fsr
->cookie
= ofsr
->cookie
;
2306 fsr
->cookie_mask
= ofsr
->cookie_mask
;
2307 error
= ofputil_pull_ofp11_match(b
, tun_table
, vl_mff_map
, &fsr
->match
,
2317 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request
*fsr
,
2318 struct ofpbuf
*b
, bool aggregate
,
2319 const struct tun_table
*tun_table
,
2320 const struct vl_mff_map
*vl_mff_map
)
2322 const struct nx_flow_stats_request
*nfsr
;
2325 nfsr
= ofpbuf_pull(b
, sizeof *nfsr
);
2326 error
= nx_pull_match(b
, ntohs(nfsr
->match_len
), &fsr
->match
,
2327 &fsr
->cookie
, &fsr
->cookie_mask
, tun_table
,
2333 return OFPERR_OFPBRC_BAD_LEN
;
2336 fsr
->aggregate
= aggregate
;
2337 fsr
->out_port
= u16_to_ofp(ntohs(nfsr
->out_port
));
2338 fsr
->out_group
= OFPG_ANY
;
2339 fsr
->table_id
= nfsr
->table_id
;
2344 /* Constructs and returns an OFPT_QUEUE_GET_CONFIG request for the specified
2345 * 'port' and 'queue', suitable for OpenFlow version 'version'.
2347 * 'queue' is honored only for OpenFlow 1.4 and later; older versions always
2348 * request all queues. */
2350 ofputil_encode_queue_get_config_request(enum ofp_version version
,
2354 struct ofpbuf
*request
;
2356 if (version
== OFP10_VERSION
) {
2357 struct ofp10_queue_get_config_request
*qgcr10
;
2359 request
= ofpraw_alloc(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST
,
2361 qgcr10
= ofpbuf_put_zeros(request
, sizeof *qgcr10
);
2362 qgcr10
->port
= htons(ofp_to_u16(port
));
2363 } else if (version
< OFP14_VERSION
) {
2364 struct ofp11_queue_get_config_request
*qgcr11
;
2366 request
= ofpraw_alloc(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST
,
2368 qgcr11
= ofpbuf_put_zeros(request
, sizeof *qgcr11
);
2369 qgcr11
->port
= ofputil_port_to_ofp11(port
);
2371 struct ofp14_queue_desc_request
*qdr14
;
2373 request
= ofpraw_alloc(OFPRAW_OFPST14_QUEUE_DESC_REQUEST
,
2375 qdr14
= ofpbuf_put_zeros(request
, sizeof *qdr14
);
2376 qdr14
->port
= ofputil_port_to_ofp11(port
);
2377 qdr14
->queue
= htonl(queue
);
2383 /* Parses OFPT_QUEUE_GET_CONFIG request 'oh', storing the port specified by the
2384 * request into '*port'. Returns 0 if successful, otherwise an OpenFlow error
2387 ofputil_decode_queue_get_config_request(const struct ofp_header
*oh
,
2388 ofp_port_t
*port
, uint32_t *queue
)
2390 const struct ofp10_queue_get_config_request
*qgcr10
;
2391 const struct ofp11_queue_get_config_request
*qgcr11
;
2392 const struct ofp14_queue_desc_request
*qdr14
;
2393 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
2394 enum ofpraw raw
= ofpraw_pull_assert(&b
);
2396 switch ((int) raw
) {
2397 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST
:
2399 *port
= u16_to_ofp(ntohs(qgcr10
->port
));
2403 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST
:
2406 enum ofperr error
= ofputil_port_from_ofp11(qgcr11
->port
, port
);
2407 if (error
|| *port
== OFPP_ANY
) {
2412 case OFPRAW_OFPST14_QUEUE_DESC_REQUEST
:
2414 *queue
= ntohl(qdr14
->queue
);
2415 return ofputil_port_from_ofp11(qdr14
->port
, port
);
2421 return (ofp_to_u16(*port
) < ofp_to_u16(OFPP_MAX
)
2423 : OFPERR_OFPQOFC_BAD_PORT
);
2426 /* Constructs and returns the beginning of a reply to
2427 * OFPT_QUEUE_GET_CONFIG_REQUEST or OFPMP_QUEUE_DESC request 'oh'. The caller
2428 * may append information about individual queues with
2429 * ofputil_append_queue_get_config_reply(). */
2431 ofputil_start_queue_get_config_reply(const struct ofp_header
*request
,
2432 struct ovs_list
*replies
)
2434 struct ofpbuf
*reply
;
2439 error
= ofputil_decode_queue_get_config_request(request
, &port
, &queue
);
2442 enum ofpraw raw
= ofpraw_decode_assert(request
);
2443 switch ((int) raw
) {
2444 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST
:
2445 reply
= ofpraw_alloc_reply(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY
,
2447 struct ofp10_queue_get_config_reply
*qgcr10
2448 = ofpbuf_put_zeros(reply
, sizeof *qgcr10
);
2449 qgcr10
->port
= htons(ofp_to_u16(port
));
2452 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST
:
2453 reply
= ofpraw_alloc_reply(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY
,
2455 struct ofp11_queue_get_config_reply
*qgcr11
2456 = ofpbuf_put_zeros(reply
, sizeof *qgcr11
);
2457 qgcr11
->port
= ofputil_port_to_ofp11(port
);
2460 case OFPRAW_OFPST14_QUEUE_DESC_REQUEST
:
2461 reply
= ofpraw_alloc_stats_reply(request
, 0);
2468 ovs_list_init(replies
);
2469 ovs_list_push_back(replies
, &reply
->list_node
);
2473 put_ofp10_queue_rate(struct ofpbuf
*reply
,
2474 enum ofp10_queue_properties property
, uint16_t rate
)
2476 if (rate
!= UINT16_MAX
) {
2477 struct ofp10_queue_prop_rate
*oqpr
;
2479 oqpr
= ofpbuf_put_zeros(reply
, sizeof *oqpr
);
2480 oqpr
->prop_header
.property
= htons(property
);
2481 oqpr
->prop_header
.len
= htons(sizeof *oqpr
);
2482 oqpr
->rate
= htons(rate
);
2487 put_ofp14_queue_rate(struct ofpbuf
*reply
,
2488 enum ofp14_queue_desc_prop_type type
, uint16_t rate
)
2490 if (rate
!= UINT16_MAX
) {
2491 ofpprop_put_u16(reply
, type
, rate
);
2496 ofputil_append_queue_get_config_reply(const struct ofputil_queue_config
*qc
,
2497 struct ovs_list
*replies
)
2499 enum ofp_version ofp_version
= ofpmp_version(replies
);
2500 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
2501 size_t start_ofs
= reply
->size
;
2505 if (ofp_version
< OFP14_VERSION
) {
2506 if (ofp_version
< OFP12_VERSION
) {
2507 struct ofp10_packet_queue
*opq10
;
2509 opq10
= ofpbuf_put_zeros(reply
, sizeof *opq10
);
2510 opq10
->queue_id
= htonl(qc
->queue
);
2511 len_ofs
= (char *) &opq10
->len
- (char *) reply
->data
;
2513 struct ofp12_packet_queue
*opq12
;
2515 opq12
= ofpbuf_put_zeros(reply
, sizeof *opq12
);
2516 opq12
->port
= ofputil_port_to_ofp11(qc
->port
);
2517 opq12
->queue_id
= htonl(qc
->queue
);
2518 len_ofs
= (char *) &opq12
->len
- (char *) reply
->data
;
2521 put_ofp10_queue_rate(reply
, OFPQT10_MIN_RATE
, qc
->min_rate
);
2522 put_ofp10_queue_rate(reply
, OFPQT11_MAX_RATE
, qc
->max_rate
);
2524 struct ofp14_queue_desc
*oqd
= ofpbuf_put_zeros(reply
, sizeof *oqd
);
2525 oqd
->port_no
= ofputil_port_to_ofp11(qc
->port
);
2526 oqd
->queue_id
= htonl(qc
->queue
);
2527 len_ofs
= (char *) &oqd
->len
- (char *) reply
->data
;
2528 put_ofp14_queue_rate(reply
, OFPQDPT14_MIN_RATE
, qc
->min_rate
);
2529 put_ofp14_queue_rate(reply
, OFPQDPT14_MAX_RATE
, qc
->max_rate
);
2532 len
= ofpbuf_at(reply
, len_ofs
, sizeof *len
);
2533 *len
= htons(reply
->size
- start_ofs
);
2535 if (ofp_version
>= OFP14_VERSION
) {
2536 ofpmp_postappend(replies
, start_ofs
);
2541 parse_ofp10_queue_rate(const struct ofp10_queue_prop_header
*hdr
,
2544 const struct ofp10_queue_prop_rate
*oqpr
;
2546 if (hdr
->len
== htons(sizeof *oqpr
)) {
2547 oqpr
= (const struct ofp10_queue_prop_rate
*) hdr
;
2548 *rate
= ntohs(oqpr
->rate
);
2551 return OFPERR_OFPBRC_BAD_LEN
;
2556 ofputil_pull_queue_get_config_reply10(struct ofpbuf
*msg
,
2557 struct ofputil_queue_config
*queue
)
2559 const struct ofp_header
*oh
= msg
->header
;
2560 unsigned int opq_len
; /* Length of protocol-specific queue header. */
2561 unsigned int len
; /* Total length of queue + properties. */
2563 /* Obtain the port number from the message header. */
2564 if (oh
->version
== OFP10_VERSION
) {
2565 const struct ofp10_queue_get_config_reply
*oqgcr10
= msg
->msg
;
2566 queue
->port
= u16_to_ofp(ntohs(oqgcr10
->port
));
2568 const struct ofp11_queue_get_config_reply
*oqgcr11
= msg
->msg
;
2569 enum ofperr error
= ofputil_port_from_ofp11(oqgcr11
->port
,
2576 /* Pull off the queue header and get the queue number and length. */
2577 if (oh
->version
< OFP12_VERSION
) {
2578 const struct ofp10_packet_queue
*opq10
;
2579 opq10
= ofpbuf_try_pull(msg
, sizeof *opq10
);
2581 return OFPERR_OFPBRC_BAD_LEN
;
2583 queue
->queue
= ntohl(opq10
->queue_id
);
2584 len
= ntohs(opq10
->len
);
2585 opq_len
= sizeof *opq10
;
2587 const struct ofp12_packet_queue
*opq12
;
2588 opq12
= ofpbuf_try_pull(msg
, sizeof *opq12
);
2590 return OFPERR_OFPBRC_BAD_LEN
;
2592 queue
->queue
= ntohl(opq12
->queue_id
);
2593 len
= ntohs(opq12
->len
);
2594 opq_len
= sizeof *opq12
;
2598 if (len
< opq_len
|| len
> msg
->size
+ opq_len
|| len
% 8) {
2599 return OFPERR_OFPBRC_BAD_LEN
;
2603 /* Pull properties. The format of these properties differs from used in
2604 * OF1.4+ so we can't use the common property functions. */
2606 const struct ofp10_queue_prop_header
*hdr
;
2607 unsigned int property
;
2608 unsigned int prop_len
;
2609 enum ofperr error
= 0;
2611 hdr
= ofpbuf_at_assert(msg
, 0, sizeof *hdr
);
2612 prop_len
= ntohs(hdr
->len
);
2613 if (prop_len
< sizeof *hdr
|| prop_len
> len
|| prop_len
% 8) {
2614 return OFPERR_OFPBRC_BAD_LEN
;
2617 property
= ntohs(hdr
->property
);
2619 case OFPQT10_MIN_RATE
:
2620 error
= parse_ofp10_queue_rate(hdr
, &queue
->min_rate
);
2623 case OFPQT11_MAX_RATE
:
2624 error
= parse_ofp10_queue_rate(hdr
, &queue
->max_rate
);
2628 VLOG_INFO_RL(&bad_ofmsg_rl
, "unknown queue property %u", property
);
2635 ofpbuf_pull(msg
, prop_len
);
2642 ofputil_pull_queue_get_config_reply14(struct ofpbuf
*msg
,
2643 struct ofputil_queue_config
*queue
)
2645 struct ofp14_queue_desc
*oqd14
= ofpbuf_try_pull(msg
, sizeof *oqd14
);
2647 return OFPERR_OFPBRC_BAD_LEN
;
2649 enum ofperr error
= ofputil_port_from_ofp11(oqd14
->port_no
, &queue
->port
);
2653 queue
->queue
= ntohl(oqd14
->queue_id
);
2656 unsigned int len
= ntohs(oqd14
->len
);
2657 if (len
< sizeof *oqd14
|| len
> msg
->size
+ sizeof *oqd14
|| len
% 8) {
2658 return OFPERR_OFPBRC_BAD_LEN
;
2660 len
-= sizeof *oqd14
;
2662 struct ofpbuf properties
= ofpbuf_const_initializer(ofpbuf_pull(msg
, len
),
2664 while (properties
.size
> 0) {
2665 struct ofpbuf payload
;
2668 error
= ofpprop_pull(&properties
, &payload
, &type
);
2674 case OFPQDPT14_MIN_RATE
:
2675 error
= ofpprop_parse_u16(&payload
, &queue
->min_rate
);
2678 case OFPQDPT14_MAX_RATE
:
2679 error
= ofpprop_parse_u16(&payload
, &queue
->max_rate
);
2683 error
= OFPPROP_UNKNOWN(true, "queue desc", type
);
2695 /* Decodes information about a queue from the OFPT_QUEUE_GET_CONFIG_REPLY in
2696 * 'reply' and stores it in '*queue'. ofputil_decode_queue_get_config_reply()
2697 * must already have pulled off the main header.
2699 * This function returns EOF if the last queue has already been decoded, 0 if a
2700 * queue was successfully decoded into '*queue', or an ofperr if there was a
2701 * problem decoding 'reply'. */
2703 ofputil_pull_queue_get_config_reply(struct ofpbuf
*msg
,
2704 struct ofputil_queue_config
*queue
)
2708 /* Pull OpenFlow header. */
2709 raw
= ofpraw_pull_assert(msg
);
2711 /* Pull protocol-specific ofp_queue_get_config_reply header (OF1.4
2712 * doesn't have one at all). */
2713 if (raw
== OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY
) {
2714 ofpbuf_pull(msg
, sizeof(struct ofp10_queue_get_config_reply
));
2715 } else if (raw
== OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY
) {
2716 ofpbuf_pull(msg
, sizeof(struct ofp11_queue_get_config_reply
));
2718 ovs_assert(raw
== OFPRAW_OFPST14_QUEUE_DESC_REPLY
);
2721 raw
= ofpraw_decode_assert(msg
->header
);
2724 queue
->min_rate
= UINT16_MAX
;
2725 queue
->max_rate
= UINT16_MAX
;
2729 } else if (raw
== OFPRAW_OFPST14_QUEUE_DESC_REPLY
) {
2730 return ofputil_pull_queue_get_config_reply14(msg
, queue
);
2732 return ofputil_pull_queue_get_config_reply10(msg
, queue
);
2736 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
2737 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
2738 * successful, otherwise an OpenFlow error code.
2740 * 'vl_mff_map' is an optional parameter that is used to validate the length
2741 * of variable length mf_fields in 'match'. If it is not provided, the
2742 * default mf_fields with maximum length will be used. */
2744 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request
*fsr
,
2745 const struct ofp_header
*oh
,
2746 const struct tun_table
*tun_table
,
2747 const struct vl_mff_map
*vl_mff_map
)
2749 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
2750 enum ofpraw raw
= ofpraw_pull_assert(&b
);
2751 switch ((int) raw
) {
2752 case OFPRAW_OFPST10_FLOW_REQUEST
:
2753 return ofputil_decode_ofpst10_flow_request(fsr
, b
.data
, false);
2755 case OFPRAW_OFPST10_AGGREGATE_REQUEST
:
2756 return ofputil_decode_ofpst10_flow_request(fsr
, b
.data
, true);
2758 case OFPRAW_OFPST11_FLOW_REQUEST
:
2759 return ofputil_decode_ofpst11_flow_request(fsr
, &b
, false, tun_table
,
2762 case OFPRAW_OFPST11_AGGREGATE_REQUEST
:
2763 return ofputil_decode_ofpst11_flow_request(fsr
, &b
, true, tun_table
,
2766 case OFPRAW_NXST_FLOW_REQUEST
:
2767 return ofputil_decode_nxst_flow_request(fsr
, &b
, false, tun_table
,
2770 case OFPRAW_NXST_AGGREGATE_REQUEST
:
2771 return ofputil_decode_nxst_flow_request(fsr
, &b
, true, tun_table
,
2775 /* Hey, the caller lied. */
2780 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
2781 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
2782 * 'protocol', and returns the message. */
2784 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request
*fsr
,
2785 enum ofputil_protocol protocol
)
2791 case OFPUTIL_P_OF11_STD
:
2792 case OFPUTIL_P_OF12_OXM
:
2793 case OFPUTIL_P_OF13_OXM
:
2794 case OFPUTIL_P_OF14_OXM
:
2795 case OFPUTIL_P_OF15_OXM
:
2796 case OFPUTIL_P_OF16_OXM
: {
2797 struct ofp11_flow_stats_request
*ofsr
;
2799 raw
= (fsr
->aggregate
2800 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
2801 : OFPRAW_OFPST11_FLOW_REQUEST
);
2802 msg
= ofpraw_alloc(raw
, ofputil_protocol_to_ofp_version(protocol
),
2803 ofputil_match_typical_len(protocol
));
2804 ofsr
= ofpbuf_put_zeros(msg
, sizeof *ofsr
);
2805 ofsr
->table_id
= fsr
->table_id
;
2806 ofsr
->out_port
= ofputil_port_to_ofp11(fsr
->out_port
);
2807 ofsr
->out_group
= htonl(fsr
->out_group
);
2808 ofsr
->cookie
= fsr
->cookie
;
2809 ofsr
->cookie_mask
= fsr
->cookie_mask
;
2810 ofputil_put_ofp11_match(msg
, &fsr
->match
, protocol
);
2814 case OFPUTIL_P_OF10_STD
:
2815 case OFPUTIL_P_OF10_STD_TID
: {
2816 struct ofp10_flow_stats_request
*ofsr
;
2818 raw
= (fsr
->aggregate
2819 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
2820 : OFPRAW_OFPST10_FLOW_REQUEST
);
2821 msg
= ofpraw_alloc(raw
, OFP10_VERSION
, 0);
2822 ofsr
= ofpbuf_put_zeros(msg
, sizeof *ofsr
);
2823 ofputil_match_to_ofp10_match(&fsr
->match
, &ofsr
->match
);
2824 ofsr
->table_id
= fsr
->table_id
;
2825 ofsr
->out_port
= htons(ofp_to_u16(fsr
->out_port
));
2829 case OFPUTIL_P_OF10_NXM
:
2830 case OFPUTIL_P_OF10_NXM_TID
: {
2831 struct nx_flow_stats_request
*nfsr
;
2834 raw
= (fsr
->aggregate
2835 ? OFPRAW_NXST_AGGREGATE_REQUEST
2836 : OFPRAW_NXST_FLOW_REQUEST
);
2837 msg
= ofpraw_alloc(raw
, OFP10_VERSION
, NXM_TYPICAL_LEN
);
2838 ofpbuf_put_zeros(msg
, sizeof *nfsr
);
2839 match_len
= nx_put_match(msg
, &fsr
->match
,
2840 fsr
->cookie
, fsr
->cookie_mask
);
2843 nfsr
->out_port
= htons(ofp_to_u16(fsr
->out_port
));
2844 nfsr
->match_len
= htons(match_len
);
2845 nfsr
->table_id
= fsr
->table_id
;
2856 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
2857 * ofputil_flow_stats in 'fs'.
2859 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
2860 * OpenFlow message. Calling this function multiple times for a single 'msg'
2861 * iterates through the replies. The caller must initially leave 'msg''s layer
2862 * pointers null and not modify them between calls.
2864 * Most switches don't send the values needed to populate fs->idle_age and
2865 * fs->hard_age, so those members will usually be set to 0. If the switch from
2866 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
2867 * 'flow_age_extension' as true so that the contents of 'msg' determine the
2868 * 'idle_age' and 'hard_age' members in 'fs'.
2870 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
2871 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
2872 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
2874 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2875 * otherwise a positive errno value. */
2877 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats
*fs
,
2879 bool flow_age_extension
,
2880 struct ofpbuf
*ofpacts
)
2882 const struct ofp_header
*oh
;
2883 size_t instructions_len
;
2887 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
2888 : ofpraw_pull(&raw
, msg
));
2896 } else if (raw
== OFPRAW_OFPST11_FLOW_REPLY
2897 || raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
2898 const struct ofp11_flow_stats
*ofs
;
2900 uint16_t padded_match_len
;
2902 ofs
= ofpbuf_try_pull(msg
, sizeof *ofs
);
2904 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply has %"PRIu32
" leftover "
2905 "bytes at end", msg
->size
);
2909 length
= ntohs(ofs
->length
);
2910 if (length
< sizeof *ofs
) {
2911 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply claims invalid "
2912 "length %"PRIuSIZE
, length
);
2916 if (ofputil_pull_ofp11_match(msg
, NULL
, NULL
, &fs
->match
,
2917 &padded_match_len
)) {
2918 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply bad match");
2921 instructions_len
= length
- sizeof *ofs
- padded_match_len
;
2923 fs
->priority
= ntohs(ofs
->priority
);
2924 fs
->table_id
= ofs
->table_id
;
2925 fs
->duration_sec
= ntohl(ofs
->duration_sec
);
2926 fs
->duration_nsec
= ntohl(ofs
->duration_nsec
);
2927 fs
->idle_timeout
= ntohs(ofs
->idle_timeout
);
2928 fs
->hard_timeout
= ntohs(ofs
->hard_timeout
);
2929 if (oh
->version
>= OFP14_VERSION
) {
2930 fs
->importance
= ntohs(ofs
->importance
);
2934 if (raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
2935 error
= ofputil_decode_flow_mod_flags(ofs
->flags
, -1, oh
->version
,
2945 fs
->cookie
= ofs
->cookie
;
2946 fs
->packet_count
= ntohll(ofs
->packet_count
);
2947 fs
->byte_count
= ntohll(ofs
->byte_count
);
2948 } else if (raw
== OFPRAW_OFPST10_FLOW_REPLY
) {
2949 const struct ofp10_flow_stats
*ofs
;
2952 ofs
= ofpbuf_try_pull(msg
, sizeof *ofs
);
2954 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply has %"PRIu32
" leftover "
2955 "bytes at end", msg
->size
);
2959 length
= ntohs(ofs
->length
);
2960 if (length
< sizeof *ofs
) {
2961 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply claims invalid "
2962 "length %"PRIuSIZE
, length
);
2965 instructions_len
= length
- sizeof *ofs
;
2967 fs
->cookie
= get_32aligned_be64(&ofs
->cookie
);
2968 ofputil_match_from_ofp10_match(&ofs
->match
, &fs
->match
);
2969 fs
->priority
= ntohs(ofs
->priority
);
2970 fs
->table_id
= ofs
->table_id
;
2971 fs
->duration_sec
= ntohl(ofs
->duration_sec
);
2972 fs
->duration_nsec
= ntohl(ofs
->duration_nsec
);
2973 fs
->idle_timeout
= ntohs(ofs
->idle_timeout
);
2974 fs
->hard_timeout
= ntohs(ofs
->hard_timeout
);
2978 fs
->packet_count
= ntohll(get_32aligned_be64(&ofs
->packet_count
));
2979 fs
->byte_count
= ntohll(get_32aligned_be64(&ofs
->byte_count
));
2981 } else if (raw
== OFPRAW_NXST_FLOW_REPLY
) {
2982 const struct nx_flow_stats
*nfs
;
2983 size_t match_len
, length
;
2985 nfs
= ofpbuf_try_pull(msg
, sizeof *nfs
);
2987 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW reply has %"PRIu32
" leftover "
2988 "bytes at end", msg
->size
);
2992 length
= ntohs(nfs
->length
);
2993 match_len
= ntohs(nfs
->match_len
);
2994 if (length
< sizeof *nfs
+ ROUND_UP(match_len
, 8)) {
2995 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW reply with match_len=%"PRIuSIZE
" "
2996 "claims invalid length %"PRIuSIZE
, match_len
, length
);
2999 if (nx_pull_match(msg
, match_len
, &fs
->match
, NULL
, NULL
, NULL
,
3003 instructions_len
= length
- sizeof *nfs
- ROUND_UP(match_len
, 8);
3005 fs
->cookie
= nfs
->cookie
;
3006 fs
->table_id
= nfs
->table_id
;
3007 fs
->duration_sec
= ntohl(nfs
->duration_sec
);
3008 fs
->duration_nsec
= ntohl(nfs
->duration_nsec
);
3009 fs
->priority
= ntohs(nfs
->priority
);
3010 fs
->idle_timeout
= ntohs(nfs
->idle_timeout
);
3011 fs
->hard_timeout
= ntohs(nfs
->hard_timeout
);
3015 if (flow_age_extension
) {
3016 if (nfs
->idle_age
) {
3017 fs
->idle_age
= ntohs(nfs
->idle_age
) - 1;
3019 if (nfs
->hard_age
) {
3020 fs
->hard_age
= ntohs(nfs
->hard_age
) - 1;
3023 fs
->packet_count
= ntohll(nfs
->packet_count
);
3024 fs
->byte_count
= ntohll(nfs
->byte_count
);
3030 if (ofpacts_pull_openflow_instructions(msg
, instructions_len
, oh
->version
,
3031 NULL
, NULL
, ofpacts
)) {
3032 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply bad instructions");
3035 fs
->ofpacts
= ofpacts
->data
;
3036 fs
->ofpacts_len
= ofpacts
->size
;
3041 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
3043 * We use this in situations where OVS internally uses UINT64_MAX to mean
3044 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
3046 unknown_to_zero(uint64_t count
)
3048 return count
!= UINT64_MAX
? count
: 0;
3051 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
3052 * those already present in the list of ofpbufs in 'replies'. 'replies' should
3053 * have been initialized with ofpmp_init(). */
3055 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats
*fs
,
3056 struct ovs_list
*replies
,
3057 const struct tun_table
*tun_table
)
3059 struct ofputil_flow_stats
*fs_
= CONST_CAST(struct ofputil_flow_stats
*,
3061 const struct tun_table
*orig_tun_table
;
3062 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
3063 size_t start_ofs
= reply
->size
;
3064 enum ofp_version version
= ofpmp_version(replies
);
3065 enum ofpraw raw
= ofpmp_decode_raw(replies
);
3067 orig_tun_table
= fs
->match
.flow
.tunnel
.metadata
.tab
;
3068 fs_
->match
.flow
.tunnel
.metadata
.tab
= tun_table
;
3070 if (raw
== OFPRAW_OFPST11_FLOW_REPLY
|| raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
3071 struct ofp11_flow_stats
*ofs
;
3073 ofpbuf_put_uninit(reply
, sizeof *ofs
);
3074 oxm_put_match(reply
, &fs
->match
, version
);
3075 ofpacts_put_openflow_instructions(fs
->ofpacts
, fs
->ofpacts_len
, reply
,
3078 ofs
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *ofs
);
3079 ofs
->length
= htons(reply
->size
- start_ofs
);
3080 ofs
->table_id
= fs
->table_id
;
3082 ofs
->duration_sec
= htonl(fs
->duration_sec
);
3083 ofs
->duration_nsec
= htonl(fs
->duration_nsec
);
3084 ofs
->priority
= htons(fs
->priority
);
3085 ofs
->idle_timeout
= htons(fs
->idle_timeout
);
3086 ofs
->hard_timeout
= htons(fs
->hard_timeout
);
3087 if (version
>= OFP14_VERSION
) {
3088 ofs
->importance
= htons(fs
->importance
);
3090 ofs
->importance
= 0;
3092 if (raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
3093 ofs
->flags
= ofputil_encode_flow_mod_flags(fs
->flags
, version
);
3097 memset(ofs
->pad2
, 0, sizeof ofs
->pad2
);
3098 ofs
->cookie
= fs
->cookie
;
3099 ofs
->packet_count
= htonll(unknown_to_zero(fs
->packet_count
));
3100 ofs
->byte_count
= htonll(unknown_to_zero(fs
->byte_count
));
3101 } else if (raw
== OFPRAW_OFPST10_FLOW_REPLY
) {
3102 struct ofp10_flow_stats
*ofs
;
3104 ofpbuf_put_uninit(reply
, sizeof *ofs
);
3105 ofpacts_put_openflow_actions(fs
->ofpacts
, fs
->ofpacts_len
, reply
,
3107 ofs
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *ofs
);
3108 ofs
->length
= htons(reply
->size
- start_ofs
);
3109 ofs
->table_id
= fs
->table_id
;
3111 ofputil_match_to_ofp10_match(&fs
->match
, &ofs
->match
);
3112 ofs
->duration_sec
= htonl(fs
->duration_sec
);
3113 ofs
->duration_nsec
= htonl(fs
->duration_nsec
);
3114 ofs
->priority
= htons(fs
->priority
);
3115 ofs
->idle_timeout
= htons(fs
->idle_timeout
);
3116 ofs
->hard_timeout
= htons(fs
->hard_timeout
);
3117 memset(ofs
->pad2
, 0, sizeof ofs
->pad2
);
3118 put_32aligned_be64(&ofs
->cookie
, fs
->cookie
);
3119 put_32aligned_be64(&ofs
->packet_count
,
3120 htonll(unknown_to_zero(fs
->packet_count
)));
3121 put_32aligned_be64(&ofs
->byte_count
,
3122 htonll(unknown_to_zero(fs
->byte_count
)));
3123 } else if (raw
== OFPRAW_NXST_FLOW_REPLY
) {
3124 struct nx_flow_stats
*nfs
;
3127 ofpbuf_put_uninit(reply
, sizeof *nfs
);
3128 match_len
= nx_put_match(reply
, &fs
->match
, 0, 0);
3129 ofpacts_put_openflow_actions(fs
->ofpacts
, fs
->ofpacts_len
, reply
,
3131 nfs
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *nfs
);
3132 nfs
->length
= htons(reply
->size
- start_ofs
);
3133 nfs
->table_id
= fs
->table_id
;
3135 nfs
->duration_sec
= htonl(fs
->duration_sec
);
3136 nfs
->duration_nsec
= htonl(fs
->duration_nsec
);
3137 nfs
->priority
= htons(fs
->priority
);
3138 nfs
->idle_timeout
= htons(fs
->idle_timeout
);
3139 nfs
->hard_timeout
= htons(fs
->hard_timeout
);
3140 nfs
->idle_age
= htons(fs
->idle_age
< 0 ? 0
3141 : fs
->idle_age
< UINT16_MAX
? fs
->idle_age
+ 1
3143 nfs
->hard_age
= htons(fs
->hard_age
< 0 ? 0
3144 : fs
->hard_age
< UINT16_MAX
? fs
->hard_age
+ 1
3146 nfs
->match_len
= htons(match_len
);
3147 nfs
->cookie
= fs
->cookie
;
3148 nfs
->packet_count
= htonll(fs
->packet_count
);
3149 nfs
->byte_count
= htonll(fs
->byte_count
);
3154 ofpmp_postappend(replies
, start_ofs
);
3155 fs_
->match
.flow
.tunnel
.metadata
.tab
= orig_tun_table
;
3158 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
3159 * NXST_AGGREGATE reply matching 'request', and returns the message. */
3161 ofputil_encode_aggregate_stats_reply(
3162 const struct ofputil_aggregate_stats
*stats
,
3163 const struct ofp_header
*request
)
3165 struct ofp_aggregate_stats_reply
*asr
;
3166 uint64_t packet_count
;
3167 uint64_t byte_count
;
3171 ofpraw_decode(&raw
, request
);
3172 if (raw
== OFPRAW_OFPST10_AGGREGATE_REQUEST
) {
3173 packet_count
= unknown_to_zero(stats
->packet_count
);
3174 byte_count
= unknown_to_zero(stats
->byte_count
);
3176 packet_count
= stats
->packet_count
;
3177 byte_count
= stats
->byte_count
;
3180 msg
= ofpraw_alloc_stats_reply(request
, 0);
3181 asr
= ofpbuf_put_zeros(msg
, sizeof *asr
);
3182 put_32aligned_be64(&asr
->packet_count
, htonll(packet_count
));
3183 put_32aligned_be64(&asr
->byte_count
, htonll(byte_count
));
3184 asr
->flow_count
= htonl(stats
->flow_count
);
3190 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats
*stats
,
3191 const struct ofp_header
*reply
)
3193 struct ofpbuf msg
= ofpbuf_const_initializer(reply
, ntohs(reply
->length
));
3194 ofpraw_pull_assert(&msg
);
3196 struct ofp_aggregate_stats_reply
*asr
= msg
.msg
;
3197 stats
->packet_count
= ntohll(get_32aligned_be64(&asr
->packet_count
));
3198 stats
->byte_count
= ntohll(get_32aligned_be64(&asr
->byte_count
));
3199 stats
->flow_count
= ntohl(asr
->flow_count
);
3204 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
3205 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
3206 * an OpenFlow error code. */
3208 ofputil_decode_flow_removed(struct ofputil_flow_removed
*fr
,
3209 const struct ofp_header
*oh
)
3211 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
3212 enum ofpraw raw
= ofpraw_pull_assert(&b
);
3213 if (raw
== OFPRAW_OFPT11_FLOW_REMOVED
) {
3214 const struct ofp12_flow_removed
*ofr
;
3217 ofr
= ofpbuf_pull(&b
, sizeof *ofr
);
3219 error
= ofputil_pull_ofp11_match(&b
, NULL
, NULL
, &fr
->match
, NULL
);
3224 fr
->priority
= ntohs(ofr
->priority
);
3225 fr
->cookie
= ofr
->cookie
;
3226 fr
->reason
= ofr
->reason
;
3227 fr
->table_id
= ofr
->table_id
;
3228 fr
->duration_sec
= ntohl(ofr
->duration_sec
);
3229 fr
->duration_nsec
= ntohl(ofr
->duration_nsec
);
3230 fr
->idle_timeout
= ntohs(ofr
->idle_timeout
);
3231 fr
->hard_timeout
= ntohs(ofr
->hard_timeout
);
3232 fr
->packet_count
= ntohll(ofr
->packet_count
);
3233 fr
->byte_count
= ntohll(ofr
->byte_count
);
3234 } else if (raw
== OFPRAW_OFPT10_FLOW_REMOVED
) {
3235 const struct ofp10_flow_removed
*ofr
;
3237 ofr
= ofpbuf_pull(&b
, sizeof *ofr
);
3239 ofputil_match_from_ofp10_match(&ofr
->match
, &fr
->match
);
3240 fr
->priority
= ntohs(ofr
->priority
);
3241 fr
->cookie
= ofr
->cookie
;
3242 fr
->reason
= ofr
->reason
;
3244 fr
->duration_sec
= ntohl(ofr
->duration_sec
);
3245 fr
->duration_nsec
= ntohl(ofr
->duration_nsec
);
3246 fr
->idle_timeout
= ntohs(ofr
->idle_timeout
);
3247 fr
->hard_timeout
= 0;
3248 fr
->packet_count
= ntohll(ofr
->packet_count
);
3249 fr
->byte_count
= ntohll(ofr
->byte_count
);
3250 } else if (raw
== OFPRAW_NXT_FLOW_REMOVED
) {
3251 struct nx_flow_removed
*nfr
;
3254 nfr
= ofpbuf_pull(&b
, sizeof *nfr
);
3255 error
= nx_pull_match(&b
, ntohs(nfr
->match_len
), &fr
->match
, NULL
,
3261 return OFPERR_OFPBRC_BAD_LEN
;
3264 fr
->priority
= ntohs(nfr
->priority
);
3265 fr
->cookie
= nfr
->cookie
;
3266 fr
->reason
= nfr
->reason
;
3267 fr
->table_id
= nfr
->table_id
? nfr
->table_id
- 1 : 255;
3268 fr
->duration_sec
= ntohl(nfr
->duration_sec
);
3269 fr
->duration_nsec
= ntohl(nfr
->duration_nsec
);
3270 fr
->idle_timeout
= ntohs(nfr
->idle_timeout
);
3271 fr
->hard_timeout
= 0;
3272 fr
->packet_count
= ntohll(nfr
->packet_count
);
3273 fr
->byte_count
= ntohll(nfr
->byte_count
);
3281 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
3282 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
3285 ofputil_encode_flow_removed(const struct ofputil_flow_removed
*fr
,
3286 enum ofputil_protocol protocol
)
3289 enum ofp_flow_removed_reason reason
= fr
->reason
;
3291 if (reason
== OFPRR_METER_DELETE
&& !(protocol
& OFPUTIL_P_OF14_UP
)) {
3292 reason
= OFPRR_DELETE
;
3296 case OFPUTIL_P_OF11_STD
:
3297 case OFPUTIL_P_OF12_OXM
:
3298 case OFPUTIL_P_OF13_OXM
:
3299 case OFPUTIL_P_OF14_OXM
:
3300 case OFPUTIL_P_OF15_OXM
:
3301 case OFPUTIL_P_OF16_OXM
: {
3302 struct ofp12_flow_removed
*ofr
;
3304 msg
= ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED
,
3305 ofputil_protocol_to_ofp_version(protocol
),
3307 ofputil_match_typical_len(protocol
));
3308 ofr
= ofpbuf_put_zeros(msg
, sizeof *ofr
);
3309 ofr
->cookie
= fr
->cookie
;
3310 ofr
->priority
= htons(fr
->priority
);
3311 ofr
->reason
= reason
;
3312 ofr
->table_id
= fr
->table_id
;
3313 ofr
->duration_sec
= htonl(fr
->duration_sec
);
3314 ofr
->duration_nsec
= htonl(fr
->duration_nsec
);
3315 ofr
->idle_timeout
= htons(fr
->idle_timeout
);
3316 ofr
->hard_timeout
= htons(fr
->hard_timeout
);
3317 ofr
->packet_count
= htonll(fr
->packet_count
);
3318 ofr
->byte_count
= htonll(fr
->byte_count
);
3319 ofputil_put_ofp11_match(msg
, &fr
->match
, protocol
);
3323 case OFPUTIL_P_OF10_STD
:
3324 case OFPUTIL_P_OF10_STD_TID
: {
3325 struct ofp10_flow_removed
*ofr
;
3327 msg
= ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED
, OFP10_VERSION
,
3329 ofr
= ofpbuf_put_zeros(msg
, sizeof *ofr
);
3330 ofputil_match_to_ofp10_match(&fr
->match
, &ofr
->match
);
3331 ofr
->cookie
= fr
->cookie
;
3332 ofr
->priority
= htons(fr
->priority
);
3333 ofr
->reason
= reason
;
3334 ofr
->duration_sec
= htonl(fr
->duration_sec
);
3335 ofr
->duration_nsec
= htonl(fr
->duration_nsec
);
3336 ofr
->idle_timeout
= htons(fr
->idle_timeout
);
3337 ofr
->packet_count
= htonll(unknown_to_zero(fr
->packet_count
));
3338 ofr
->byte_count
= htonll(unknown_to_zero(fr
->byte_count
));
3342 case OFPUTIL_P_OF10_NXM
:
3343 case OFPUTIL_P_OF10_NXM_TID
: {
3344 struct nx_flow_removed
*nfr
;
3347 msg
= ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED
, OFP10_VERSION
,
3348 htonl(0), NXM_TYPICAL_LEN
);
3349 ofpbuf_put_zeros(msg
, sizeof *nfr
);
3350 match_len
= nx_put_match(msg
, &fr
->match
, 0, 0);
3353 nfr
->cookie
= fr
->cookie
;
3354 nfr
->priority
= htons(fr
->priority
);
3355 nfr
->reason
= reason
;
3356 nfr
->table_id
= fr
->table_id
+ 1;
3357 nfr
->duration_sec
= htonl(fr
->duration_sec
);
3358 nfr
->duration_nsec
= htonl(fr
->duration_nsec
);
3359 nfr
->idle_timeout
= htons(fr
->idle_timeout
);
3360 nfr
->match_len
= htons(match_len
);
3361 nfr
->packet_count
= htonll(fr
->packet_count
);
3362 nfr
->byte_count
= htonll(fr
->byte_count
);
3373 /* The caller has done basic initialization of '*pin'; the other output
3374 * arguments needs to be initialized. */
3376 decode_nx_packet_in2(const struct ofp_header
*oh
, bool loose
,
3377 const struct tun_table
*tun_table
,
3378 const struct vl_mff_map
*vl_mff_map
,
3379 struct ofputil_packet_in
*pin
,
3380 size_t *total_len
, uint32_t *buffer_id
,
3381 struct ofpbuf
*continuation
)
3384 *buffer_id
= UINT32_MAX
;
3386 struct ofpbuf properties
;
3387 ofpbuf_use_const(&properties
, oh
, ntohs(oh
->length
));
3388 ofpraw_pull_assert(&properties
);
3390 while (properties
.size
> 0) {
3391 struct ofpbuf payload
;
3394 enum ofperr error
= ofpprop_pull(&properties
, &payload
, &type
);
3401 pin
->packet
= payload
.msg
;
3402 pin
->packet_len
= ofpbuf_msgsize(&payload
);
3405 case NXPINT_FULL_LEN
: {
3407 error
= ofpprop_parse_u32(&payload
, &u32
);
3412 case NXPINT_BUFFER_ID
:
3413 error
= ofpprop_parse_u32(&payload
, buffer_id
);
3416 case NXPINT_TABLE_ID
:
3417 error
= ofpprop_parse_u8(&payload
, &pin
->table_id
);
3421 error
= ofpprop_parse_be64(&payload
, &pin
->cookie
);
3424 case NXPINT_REASON
: {
3426 error
= ofpprop_parse_u8(&payload
, &reason
);
3427 pin
->reason
= reason
;
3431 case NXPINT_METADATA
:
3432 error
= oxm_decode_match(payload
.msg
, ofpbuf_msgsize(&payload
),
3433 loose
, tun_table
, vl_mff_map
,
3434 &pin
->flow_metadata
);
3437 case NXPINT_USERDATA
:
3438 pin
->userdata
= payload
.msg
;
3439 pin
->userdata_len
= ofpbuf_msgsize(&payload
);
3442 case NXPINT_CONTINUATION
:
3444 error
= ofpprop_parse_nested(&payload
, continuation
);
3449 error
= OFPPROP_UNKNOWN(loose
, "NX_PACKET_IN2", type
);
3457 if (!pin
->packet_len
) {
3458 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXT_PACKET_IN2 lacks packet");
3459 return OFPERR_OFPBRC_BAD_LEN
;
3460 } else if (!*total_len
) {
3461 *total_len
= pin
->packet_len
;
3462 } else if (*total_len
< pin
->packet_len
) {
3463 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXT_PACKET_IN2 claimed full_len < len");
3464 return OFPERR_OFPBRC_BAD_LEN
;
3470 /* Decodes the packet-in message starting at 'oh' into '*pin'. Populates
3471 * 'pin->packet' and 'pin->packet_len' with the part of the packet actually
3472 * included in the message. If 'total_lenp' is nonnull, populates
3473 * '*total_lenp' with the original length of the packet (which is larger than
3474 * 'packet->len' if only part of the packet was included). If 'buffer_idp' is
3475 * nonnull, stores the packet's buffer ID in '*buffer_idp' (UINT32_MAX if it
3476 * was not buffered).
3478 * Populates 'continuation', if nonnull, with the continuation data from the
3479 * packet-in (an empty buffer, if 'oh' did not contain continuation data). The
3480 * format of this data is supposed to be opaque to anything other than
3481 * ovs-vswitchd, so that in any other process the only reasonable use of this
3482 * data is to be copied into an NXT_RESUME message via ofputil_encode_resume().
3484 * This function points 'pin->packet' into 'oh', so the caller should not free
3485 * it separately from the original OpenFlow message. This is also true for
3486 * 'pin->userdata' (which could also end up NULL if there is no userdata).
3488 * 'vl_mff_map' is an optional parameter that is used to validate the length
3489 * of variable length mf_fields in 'match'. If it is not provided, the
3490 * default mf_fields with maximum length will be used.
3492 * Returns 0 if successful, otherwise an OpenFlow error code. */
3494 ofputil_decode_packet_in(const struct ofp_header
*oh
, bool loose
,
3495 const struct tun_table
*tun_table
,
3496 const struct vl_mff_map
*vl_mff_map
,
3497 struct ofputil_packet_in
*pin
,
3498 size_t *total_lenp
, uint32_t *buffer_idp
,
3499 struct ofpbuf
*continuation
)
3504 memset(pin
, 0, sizeof *pin
);
3505 pin
->cookie
= OVS_BE64_MAX
;
3507 ofpbuf_use_const(continuation
, NULL
, 0);
3510 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
3511 enum ofpraw raw
= ofpraw_pull_assert(&b
);
3512 if (raw
== OFPRAW_OFPT13_PACKET_IN
|| raw
== OFPRAW_OFPT12_PACKET_IN
) {
3513 const struct ofp12_packet_in
*opi
= ofpbuf_pull(&b
, sizeof *opi
);
3514 const ovs_be64
*cookie
= (raw
== OFPRAW_OFPT13_PACKET_IN
3515 ? ofpbuf_pull(&b
, sizeof *cookie
)
3517 enum ofperr error
= oxm_pull_match_loose(&b
, tun_table
,
3518 &pin
->flow_metadata
);
3523 if (!ofpbuf_try_pull(&b
, 2)) {
3524 return OFPERR_OFPBRC_BAD_LEN
;
3527 pin
->reason
= opi
->reason
;
3528 pin
->table_id
= opi
->table_id
;
3529 buffer_id
= ntohl(opi
->buffer_id
);
3530 total_len
= ntohs(opi
->total_len
);
3532 pin
->cookie
= *cookie
;
3535 pin
->packet
= b
.data
;
3536 pin
->packet_len
= b
.size
;
3537 } else if (raw
== OFPRAW_OFPT10_PACKET_IN
) {
3538 const struct ofp10_packet_in
*opi
;
3540 opi
= ofpbuf_pull(&b
, offsetof(struct ofp10_packet_in
, data
));
3542 pin
->packet
= CONST_CAST(uint8_t *, opi
->data
);
3543 pin
->packet_len
= b
.size
;
3545 match_init_catchall(&pin
->flow_metadata
);
3546 match_set_in_port(&pin
->flow_metadata
,
3547 u16_to_ofp(ntohs(opi
->in_port
)));
3548 pin
->reason
= opi
->reason
;
3549 buffer_id
= ntohl(opi
->buffer_id
);
3550 total_len
= ntohs(opi
->total_len
);
3551 } else if (raw
== OFPRAW_OFPT11_PACKET_IN
) {
3552 const struct ofp11_packet_in
*opi
;
3556 opi
= ofpbuf_pull(&b
, sizeof *opi
);
3558 pin
->packet
= b
.data
;
3559 pin
->packet_len
= b
.size
;
3561 buffer_id
= ntohl(opi
->buffer_id
);
3562 error
= ofputil_port_from_ofp11(opi
->in_port
, &in_port
);
3566 match_init_catchall(&pin
->flow_metadata
);
3567 match_set_in_port(&pin
->flow_metadata
, in_port
);
3568 total_len
= ntohs(opi
->total_len
);
3569 pin
->reason
= opi
->reason
;
3570 pin
->table_id
= opi
->table_id
;
3571 } else if (raw
== OFPRAW_NXT_PACKET_IN
) {
3572 const struct nx_packet_in
*npi
;
3575 npi
= ofpbuf_pull(&b
, sizeof *npi
);
3576 error
= nx_pull_match_loose(&b
, ntohs(npi
->match_len
),
3577 &pin
->flow_metadata
, NULL
, NULL
, NULL
);
3582 if (!ofpbuf_try_pull(&b
, 2)) {
3583 return OFPERR_OFPBRC_BAD_LEN
;
3586 pin
->reason
= npi
->reason
;
3587 pin
->table_id
= npi
->table_id
;
3588 pin
->cookie
= npi
->cookie
;
3590 buffer_id
= ntohl(npi
->buffer_id
);
3591 total_len
= ntohs(npi
->total_len
);
3593 pin
->packet
= b
.data
;
3594 pin
->packet_len
= b
.size
;
3595 } else if (raw
== OFPRAW_NXT_PACKET_IN2
|| raw
== OFPRAW_NXT_RESUME
) {
3596 enum ofperr error
= decode_nx_packet_in2(oh
, loose
, tun_table
,
3597 vl_mff_map
, pin
, &total_len
,
3598 &buffer_id
, continuation
);
3607 *total_lenp
= total_len
;
3610 *buffer_idp
= buffer_id
;
3617 encode_packet_in_reason(enum ofp_packet_in_reason reason
,
3618 enum ofp_version version
)
3623 case OFPR_INVALID_TTL
:
3626 case OFPR_ACTION_SET
:
3628 case OFPR_PACKET_OUT
:
3629 return version
< OFP14_VERSION
? OFPR_ACTION
: reason
;
3631 case OFPR_EXPLICIT_MISS
:
3632 return version
< OFP13_VERSION
? OFPR_ACTION
: OFPR_NO_MATCH
;
3634 case OFPR_IMPLICIT_MISS
:
3635 return OFPR_NO_MATCH
;
3637 case OFPR_N_REASONS
:
3643 /* Only NXT_PACKET_IN2 (not NXT_RESUME) should include NXCPT_USERDATA, so this
3644 * function omits it. The caller can add it itself if desired. */
3646 ofputil_put_packet_in(const struct ofputil_packet_in
*pin
,
3647 enum ofp_version version
, size_t include_bytes
,
3650 /* Add packet properties. */
3651 ofpprop_put(msg
, NXPINT_PACKET
, pin
->packet
, include_bytes
);
3652 if (include_bytes
!= pin
->packet_len
) {
3653 ofpprop_put_u32(msg
, NXPINT_FULL_LEN
, pin
->packet_len
);
3656 /* Add flow properties. */
3657 ofpprop_put_u8(msg
, NXPINT_TABLE_ID
, pin
->table_id
);
3658 if (pin
->cookie
!= OVS_BE64_MAX
) {
3659 ofpprop_put_be64(msg
, NXPINT_COOKIE
, pin
->cookie
);
3662 /* Add other properties. */
3663 ofpprop_put_u8(msg
, NXPINT_REASON
,
3664 encode_packet_in_reason(pin
->reason
, version
));
3666 size_t start
= ofpprop_start(msg
, NXPINT_METADATA
);
3667 oxm_put_raw(msg
, &pin
->flow_metadata
, version
);
3668 ofpprop_end(msg
, start
);
3672 put_actions_property(struct ofpbuf
*msg
, uint64_t prop_type
,
3673 enum ofp_version version
,
3674 const struct ofpact
*actions
, size_t actions_len
)
3677 size_t start
= ofpprop_start_nested(msg
, prop_type
);
3678 ofpacts_put_openflow_actions(actions
, actions_len
, msg
, version
);
3679 ofpprop_end(msg
, start
);
3683 enum nx_continuation_prop_type
{
3684 NXCPT_BRIDGE
= 0x8000,
3694 /* Only NXT_PACKET_IN2 (not NXT_RESUME) should include NXCPT_USERDATA, so this
3695 * function omits it. The caller can add it itself if desired. */
3697 ofputil_put_packet_in_private(const struct ofputil_packet_in_private
*pin
,
3698 enum ofp_version version
, size_t include_bytes
,
3701 ofputil_put_packet_in(&pin
->public, version
, include_bytes
, msg
);
3703 size_t continuation_ofs
= ofpprop_start_nested(msg
, NXPINT_CONTINUATION
);
3704 size_t inner_ofs
= msg
->size
;
3706 if (!uuid_is_zero(&pin
->bridge
)) {
3707 ofpprop_put_uuid(msg
, NXCPT_BRIDGE
, &pin
->bridge
);
3710 struct ofpbuf pin_stack
;
3711 ofpbuf_use_const(&pin_stack
, pin
->stack
, pin
->stack_size
);
3713 while (pin_stack
.size
) {
3715 uint8_t *val
= nx_stack_pop(&pin_stack
, &len
);
3716 ofpprop_put(msg
, NXCPT_STACK
, val
, len
);
3720 ofpprop_put_u32(msg
, NXCPT_MIRRORS
, pin
->mirrors
);
3723 if (pin
->conntracked
) {
3724 ofpprop_put_flag(msg
, NXCPT_CONNTRACKED
);
3727 if (pin
->actions_len
) {
3728 /* Divide 'pin->actions' into groups that begins with an
3729 * unroll_xlate action. For each group, emit a NXCPT_TABLE_ID and
3730 * NXCPT_COOKIE property (if either has changed; each is initially
3731 * assumed 0), then a NXCPT_ACTIONS property with the grouped
3734 * The alternative is to make OFPACT_UNROLL_XLATE public. We can
3735 * always do that later, since this is a private property. */
3736 const struct ofpact
*const end
= ofpact_end(pin
->actions
,
3738 const struct ofpact_unroll_xlate
*unroll
= NULL
;
3739 uint8_t table_id
= 0;
3740 ovs_be64 cookie
= 0;
3742 const struct ofpact
*a
;
3743 for (a
= pin
->actions
; ; a
= ofpact_next(a
)) {
3744 if (a
== end
|| a
->type
== OFPACT_UNROLL_XLATE
) {
3746 if (table_id
!= unroll
->rule_table_id
) {
3747 ofpprop_put_u8(msg
, NXCPT_TABLE_ID
,
3748 unroll
->rule_table_id
);
3749 table_id
= unroll
->rule_table_id
;
3751 if (cookie
!= unroll
->rule_cookie
) {
3752 ofpprop_put_be64(msg
, NXCPT_COOKIE
,
3753 unroll
->rule_cookie
);
3754 cookie
= unroll
->rule_cookie
;
3758 const struct ofpact
*start
3759 = unroll
? ofpact_next(&unroll
->ofpact
) : pin
->actions
;
3760 put_actions_property(msg
, NXCPT_ACTIONS
, version
,
3761 start
, (a
- start
) * sizeof *a
);
3766 unroll
= ofpact_get_UNROLL_XLATE(a
);
3771 if (pin
->action_set_len
) {
3772 size_t start
= ofpprop_start_nested(msg
, NXCPT_ACTION_SET
);
3773 ofpacts_put_openflow_actions(pin
->action_set
,
3774 pin
->action_set_len
, msg
, version
);
3775 ofpprop_end(msg
, start
);
3778 if (msg
->size
> inner_ofs
) {
3779 ofpprop_end(msg
, continuation_ofs
);
3781 msg
->size
= continuation_ofs
;
3785 static struct ofpbuf
*
3786 ofputil_encode_ofp10_packet_in(const struct ofputil_packet_in
*pin
)
3788 struct ofp10_packet_in
*opi
;
3791 msg
= ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN
, OFP10_VERSION
,
3792 htonl(0), pin
->packet_len
);
3793 opi
= ofpbuf_put_zeros(msg
, offsetof(struct ofp10_packet_in
, data
));
3794 opi
->total_len
= htons(pin
->packet_len
);
3795 opi
->in_port
= htons(ofp_to_u16(pin
->flow_metadata
.flow
.in_port
.ofp_port
));
3796 opi
->reason
= encode_packet_in_reason(pin
->reason
, OFP10_VERSION
);
3797 opi
->buffer_id
= htonl(UINT32_MAX
);
3802 static struct ofpbuf
*
3803 ofputil_encode_nx_packet_in(const struct ofputil_packet_in
*pin
,
3804 enum ofp_version version
)
3806 struct nx_packet_in
*npi
;
3810 /* The final argument is just an estimate of the space required. */
3811 msg
= ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN
, version
,
3812 htonl(0), NXM_TYPICAL_LEN
+ 2 + pin
->packet_len
);
3813 ofpbuf_put_zeros(msg
, sizeof *npi
);
3814 match_len
= nx_put_match(msg
, &pin
->flow_metadata
, 0, 0);
3815 ofpbuf_put_zeros(msg
, 2);
3818 npi
->buffer_id
= htonl(UINT32_MAX
);
3819 npi
->total_len
= htons(pin
->packet_len
);
3820 npi
->reason
= encode_packet_in_reason(pin
->reason
, version
);
3821 npi
->table_id
= pin
->table_id
;
3822 npi
->cookie
= pin
->cookie
;
3823 npi
->match_len
= htons(match_len
);
3828 static struct ofpbuf
*
3829 ofputil_encode_nx_packet_in2(const struct ofputil_packet_in_private
*pin
,
3830 enum ofp_version version
, size_t include_bytes
)
3832 /* 'extra' is just an estimate of the space required. */
3833 size_t extra
= (pin
->public.packet_len
3834 + NXM_TYPICAL_LEN
/* flow_metadata */
3835 + pin
->stack_size
* 4
3837 + pin
->action_set_len
3838 + 256); /* fudge factor */
3839 struct ofpbuf
*msg
= ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN2
, version
,
3842 ofputil_put_packet_in_private(pin
, version
, include_bytes
, msg
);
3843 if (pin
->public.userdata_len
) {
3844 ofpprop_put(msg
, NXPINT_USERDATA
, pin
->public.userdata
,
3845 pin
->public.userdata_len
);
3848 ofpmsg_update_length(msg
);
3852 static struct ofpbuf
*
3853 ofputil_encode_ofp11_packet_in(const struct ofputil_packet_in
*pin
)
3855 struct ofp11_packet_in
*opi
;
3858 msg
= ofpraw_alloc_xid(OFPRAW_OFPT11_PACKET_IN
, OFP11_VERSION
,
3859 htonl(0), pin
->packet_len
);
3860 opi
= ofpbuf_put_zeros(msg
, sizeof *opi
);
3861 opi
->buffer_id
= htonl(UINT32_MAX
);
3862 opi
->in_port
= ofputil_port_to_ofp11(
3863 pin
->flow_metadata
.flow
.in_port
.ofp_port
);
3864 opi
->in_phy_port
= opi
->in_port
;
3865 opi
->total_len
= htons(pin
->packet_len
);
3866 opi
->reason
= encode_packet_in_reason(pin
->reason
, OFP11_VERSION
);
3867 opi
->table_id
= pin
->table_id
;
3872 static struct ofpbuf
*
3873 ofputil_encode_ofp12_packet_in(const struct ofputil_packet_in
*pin
,
3874 enum ofp_version version
)
3876 enum ofpraw raw
= (version
>= OFP13_VERSION
3877 ? OFPRAW_OFPT13_PACKET_IN
3878 : OFPRAW_OFPT12_PACKET_IN
);
3881 /* The final argument is just an estimate of the space required. */
3882 msg
= ofpraw_alloc_xid(raw
, version
,
3883 htonl(0), NXM_TYPICAL_LEN
+ 2 + pin
->packet_len
);
3885 struct ofp12_packet_in
*opi
= ofpbuf_put_zeros(msg
, sizeof *opi
);
3886 opi
->buffer_id
= htonl(UINT32_MAX
);
3887 opi
->total_len
= htons(pin
->packet_len
);
3888 opi
->reason
= encode_packet_in_reason(pin
->reason
, version
);
3889 opi
->table_id
= pin
->table_id
;
3891 if (version
>= OFP13_VERSION
) {
3892 ovs_be64 cookie
= pin
->cookie
;
3893 ofpbuf_put(msg
, &cookie
, sizeof cookie
);
3896 oxm_put_match(msg
, &pin
->flow_metadata
, version
);
3897 ofpbuf_put_zeros(msg
, 2);
3902 /* Converts abstract ofputil_packet_in_private 'pin' into a PACKET_IN message
3903 * for 'protocol', using the packet-in format specified by 'packet_in_format'.
3905 * This function is really meant only for use by ovs-vswitchd. To any other
3906 * code, the "continuation" data, i.e. the data that is in struct
3907 * ofputil_packet_in_private but not in struct ofputil_packet_in, is supposed
3908 * to be opaque (and it might change from one OVS version to another). Thus,
3909 * if any other code wants to encode a packet-in, it should use a non-"private"
3910 * version of this function. (Such a version doesn't currently exist because
3911 * only ovs-vswitchd currently wants to encode packet-ins. If you need one,
3914 ofputil_encode_packet_in_private(const struct ofputil_packet_in_private
*pin
,
3915 enum ofputil_protocol protocol
,
3916 enum nx_packet_in_format packet_in_format
)
3918 enum ofp_version version
= ofputil_protocol_to_ofp_version(protocol
);
3921 switch (packet_in_format
) {
3922 case NXPIF_STANDARD
:
3924 case OFPUTIL_P_OF10_STD
:
3925 case OFPUTIL_P_OF10_STD_TID
:
3926 case OFPUTIL_P_OF10_NXM
:
3927 case OFPUTIL_P_OF10_NXM_TID
:
3928 msg
= ofputil_encode_ofp10_packet_in(&pin
->public);
3931 case OFPUTIL_P_OF11_STD
:
3932 msg
= ofputil_encode_ofp11_packet_in(&pin
->public);
3935 case OFPUTIL_P_OF12_OXM
:
3936 case OFPUTIL_P_OF13_OXM
:
3937 case OFPUTIL_P_OF14_OXM
:
3938 case OFPUTIL_P_OF15_OXM
:
3939 case OFPUTIL_P_OF16_OXM
:
3940 msg
= ofputil_encode_ofp12_packet_in(&pin
->public, version
);
3948 case NXPIF_NXT_PACKET_IN
:
3949 msg
= ofputil_encode_nx_packet_in(&pin
->public, version
);
3952 case NXPIF_NXT_PACKET_IN2
:
3953 return ofputil_encode_nx_packet_in2(pin
, version
,
3954 pin
->public.packet_len
);
3960 ofpbuf_put(msg
, pin
->public.packet
, pin
->public.packet_len
);
3961 ofpmsg_update_length(msg
);
3965 /* Returns a string form of 'reason'. The return value is either a statically
3966 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
3967 * 'bufsize' should be at least OFPUTIL_PACKET_IN_REASON_BUFSIZE. */
3969 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason
,
3970 char *reasonbuf
, size_t bufsize
)
3977 case OFPR_INVALID_TTL
:
3978 return "invalid_ttl";
3979 case OFPR_ACTION_SET
:
3980 return "action_set";
3983 case OFPR_PACKET_OUT
:
3984 return "packet_out";
3985 case OFPR_EXPLICIT_MISS
:
3986 case OFPR_IMPLICIT_MISS
:
3989 case OFPR_N_REASONS
:
3991 snprintf(reasonbuf
, bufsize
, "%d", (int) reason
);
3997 ofputil_packet_in_reason_from_string(const char *s
,
3998 enum ofp_packet_in_reason
*reason
)
4002 for (i
= 0; i
< OFPR_N_REASONS
; i
++) {
4003 char reasonbuf
[OFPUTIL_PACKET_IN_REASON_BUFSIZE
];
4004 const char *reason_s
;
4006 reason_s
= ofputil_packet_in_reason_to_string(i
, reasonbuf
,
4008 if (!strcasecmp(s
, reason_s
)) {
4016 /* Returns a newly allocated NXT_RESUME message for 'pin', with the given
4017 * 'continuation', for 'protocol'. This message is suitable for resuming the
4018 * pipeline traveral of the packet represented by 'pin', if sent to the switch
4019 * from which 'pin' was received. */
4021 ofputil_encode_resume(const struct ofputil_packet_in
*pin
,
4022 const struct ofpbuf
*continuation
,
4023 enum ofputil_protocol protocol
)
4025 enum ofp_version version
= ofputil_protocol_to_ofp_version(protocol
);
4026 size_t extra
= pin
->packet_len
+ NXM_TYPICAL_LEN
+ continuation
->size
;
4027 struct ofpbuf
*msg
= ofpraw_alloc_xid(OFPRAW_NXT_RESUME
, version
,
4029 ofputil_put_packet_in(pin
, version
, pin
->packet_len
, msg
);
4030 ofpprop_put_nested(msg
, NXPINT_CONTINUATION
, continuation
);
4031 ofpmsg_update_length(msg
);
4036 parse_stack_prop(const struct ofpbuf
*property
, struct ofpbuf
*stack
)
4038 unsigned int len
= ofpbuf_msgsize(property
);
4039 if (len
> sizeof(union mf_subvalue
)) {
4040 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXCPT_STACK property has bad length %u",
4042 return OFPERR_OFPBPC_BAD_LEN
;
4044 nx_stack_push_bottom(stack
, property
->msg
, len
);
4049 parse_actions_property(struct ofpbuf
*property
, enum ofp_version version
,
4050 struct ofpbuf
*ofpacts
)
4052 if (!ofpbuf_try_pull(property
, ROUND_UP(ofpbuf_headersize(property
), 8))) {
4053 VLOG_WARN_RL(&bad_ofmsg_rl
, "actions property has bad length %"PRIu32
,
4055 return OFPERR_OFPBPC_BAD_LEN
;
4058 return ofpacts_pull_openflow_actions(property
, property
->size
,
4059 version
, NULL
, NULL
, ofpacts
);
4062 /* This is like ofputil_decode_packet_in(), except that it decodes the
4063 * continuation data into 'pin'. The format of this data is supposed to be
4064 * opaque to any process other than ovs-vswitchd, so this function should not
4065 * be used outside ovs-vswitchd.
4067 * 'vl_mff_map' is an optional parameter that is used to validate the length
4068 * of variable length mf_fields in 'match'. If it is not provided, the
4069 * default mf_fields with maximum length will be used.
4071 * When successful, 'pin' contains some dynamically allocated data. Call
4072 * ofputil_packet_in_private_destroy() to free this data. */
4074 ofputil_decode_packet_in_private(const struct ofp_header
*oh
, bool loose
,
4075 const struct tun_table
*tun_table
,
4076 const struct vl_mff_map
*vl_mff_map
,
4077 struct ofputil_packet_in_private
*pin
,
4078 size_t *total_len
, uint32_t *buffer_id
)
4080 memset(pin
, 0, sizeof *pin
);
4082 struct ofpbuf continuation
;
4084 error
= ofputil_decode_packet_in(oh
, loose
, tun_table
, vl_mff_map
,
4085 &pin
->public, total_len
, buffer_id
,
4091 struct ofpbuf actions
, action_set
;
4092 ofpbuf_init(&actions
, 0);
4093 ofpbuf_init(&action_set
, 0);
4095 uint8_t table_id
= 0;
4096 ovs_be64 cookie
= 0;
4098 struct ofpbuf stack
;
4099 ofpbuf_init(&stack
, 0);
4101 while (continuation
.size
> 0) {
4102 struct ofpbuf payload
;
4105 error
= ofpprop_pull(&continuation
, &payload
, &type
);
4112 error
= ofpprop_parse_uuid(&payload
, &pin
->bridge
);
4116 error
= parse_stack_prop(&payload
, &stack
);
4120 error
= ofpprop_parse_u32(&payload
, &pin
->mirrors
);
4123 case NXCPT_CONNTRACKED
:
4124 pin
->conntracked
= true;
4127 case NXCPT_TABLE_ID
:
4128 error
= ofpprop_parse_u8(&payload
, &table_id
);
4132 error
= ofpprop_parse_be64(&payload
, &cookie
);
4135 case NXCPT_ACTIONS
: {
4136 struct ofpact_unroll_xlate
*unroll
4137 = ofpact_put_UNROLL_XLATE(&actions
);
4138 unroll
->rule_table_id
= table_id
;
4139 unroll
->rule_cookie
= cookie
;
4140 error
= parse_actions_property(&payload
, oh
->version
, &actions
);
4144 case NXCPT_ACTION_SET
:
4145 error
= parse_actions_property(&payload
, oh
->version
, &action_set
);
4149 error
= OFPPROP_UNKNOWN(loose
, "continuation", type
);
4157 pin
->actions_len
= actions
.size
;
4158 pin
->actions
= ofpbuf_steal_data(&actions
);
4159 pin
->action_set_len
= action_set
.size
;
4160 pin
->action_set
= ofpbuf_steal_data(&action_set
);
4161 pin
->stack_size
= stack
.size
;
4162 pin
->stack
= ofpbuf_steal_data(&stack
);
4165 ofputil_packet_in_private_destroy(pin
);
4171 /* Frees data in 'pin' that is dynamically allocated by
4172 * ofputil_decode_packet_in_private().
4174 * 'pin->public' contains some pointer members that
4175 * ofputil_decode_packet_in_private() doesn't initialize to newly allocated
4176 * data, so this function doesn't free those. */
4178 ofputil_packet_in_private_destroy(struct ofputil_packet_in_private
*pin
)
4183 free(pin
->action_set
);
4187 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
4190 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
4191 * message's actions. The caller must initialize 'ofpacts' and retains
4192 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
4194 * 'po->packet' refers to the packet data in 'oh', so the buffer containing
4195 * 'oh' must not be destroyed while 'po' is being used.
4197 * Returns 0 if successful, otherwise an OFPERR_* value. */
4199 ofputil_decode_packet_out(struct ofputil_packet_out
*po
,
4200 const struct ofp_header
*oh
,
4201 struct ofpbuf
*ofpacts
)
4203 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
4204 enum ofpraw raw
= ofpraw_pull_assert(&b
);
4206 ofpbuf_clear(ofpacts
);
4207 if (raw
== OFPRAW_OFPT11_PACKET_OUT
) {
4209 const struct ofp11_packet_out
*opo
= ofpbuf_pull(&b
, sizeof *opo
);
4211 po
->buffer_id
= ntohl(opo
->buffer_id
);
4212 error
= ofputil_port_from_ofp11(opo
->in_port
, &po
->in_port
);
4217 error
= ofpacts_pull_openflow_actions(&b
, ntohs(opo
->actions_len
),
4218 oh
->version
, NULL
, NULL
,
4223 } else if (raw
== OFPRAW_OFPT10_PACKET_OUT
) {
4225 const struct ofp10_packet_out
*opo
= ofpbuf_pull(&b
, sizeof *opo
);
4227 po
->buffer_id
= ntohl(opo
->buffer_id
);
4228 po
->in_port
= u16_to_ofp(ntohs(opo
->in_port
));
4230 error
= ofpacts_pull_openflow_actions(&b
, ntohs(opo
->actions_len
),
4231 oh
->version
, NULL
, NULL
,
4240 if (ofp_to_u16(po
->in_port
) >= ofp_to_u16(OFPP_MAX
)
4241 && po
->in_port
!= OFPP_LOCAL
4242 && po
->in_port
!= OFPP_NONE
&& po
->in_port
!= OFPP_CONTROLLER
) {
4243 VLOG_WARN_RL(&bad_ofmsg_rl
, "packet-out has bad input port %#"PRIx32
,
4245 return OFPERR_OFPBRC_BAD_PORT
;
4248 po
->ofpacts
= ofpacts
->data
;
4249 po
->ofpacts_len
= ofpacts
->size
;
4251 if (po
->buffer_id
== UINT32_MAX
) {
4252 po
->packet
= b
.data
;
4253 po
->packet_len
= b
.size
;
4262 /* ofputil_phy_port */
4264 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
4265 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD
== OFPPF_10MB_HD
); /* bit 0 */
4266 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD
== OFPPF_10MB_FD
); /* bit 1 */
4267 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD
== OFPPF_100MB_HD
); /* bit 2 */
4268 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD
== OFPPF_100MB_FD
); /* bit 3 */
4269 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD
== OFPPF_1GB_HD
); /* bit 4 */
4270 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD
== OFPPF_1GB_FD
); /* bit 5 */
4271 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD
== OFPPF_10GB_FD
); /* bit 6 */
4273 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
4274 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER
== (OFPPF10_COPPER
<< 4));
4275 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER
== (OFPPF10_FIBER
<< 4));
4276 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG
== (OFPPF10_AUTONEG
<< 4));
4277 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE
== (OFPPF10_PAUSE
<< 4));
4278 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM
== (OFPPF10_PAUSE_ASYM
<< 4));
4280 static enum netdev_features
4281 netdev_port_features_from_ofp10(ovs_be32 ofp10_
)
4283 uint32_t ofp10
= ntohl(ofp10_
);
4284 return (ofp10
& 0x7f) | ((ofp10
& 0xf80) << 4);
4288 netdev_port_features_to_ofp10(enum netdev_features features
)
4290 return htonl((features
& 0x7f) | ((features
& 0xf800) >> 4));
4293 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD
== OFPPF_10MB_HD
); /* bit 0 */
4294 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD
== OFPPF_10MB_FD
); /* bit 1 */
4295 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD
== OFPPF_100MB_HD
); /* bit 2 */
4296 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD
== OFPPF_100MB_FD
); /* bit 3 */
4297 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD
== OFPPF_1GB_HD
); /* bit 4 */
4298 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD
== OFPPF_1GB_FD
); /* bit 5 */
4299 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD
== OFPPF_10GB_FD
); /* bit 6 */
4300 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD
== OFPPF11_40GB_FD
); /* bit 7 */
4301 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD
== OFPPF11_100GB_FD
); /* bit 8 */
4302 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD
== OFPPF11_1TB_FD
); /* bit 9 */
4303 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER
== OFPPF11_OTHER
); /* bit 10 */
4304 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER
== OFPPF11_COPPER
); /* bit 11 */
4305 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER
== OFPPF11_FIBER
); /* bit 12 */
4306 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG
== OFPPF11_AUTONEG
); /* bit 13 */
4307 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE
== OFPPF11_PAUSE
); /* bit 14 */
4308 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM
== OFPPF11_PAUSE_ASYM
);/* bit 15 */
4310 static enum netdev_features
4311 netdev_port_features_from_ofp11(ovs_be32 ofp11
)
4313 return ntohl(ofp11
) & 0xffff;
4317 netdev_port_features_to_ofp11(enum netdev_features features
)
4319 return htonl(features
& 0xffff);
4323 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port
*pp
,
4324 const struct ofp10_phy_port
*opp
)
4326 pp
->port_no
= u16_to_ofp(ntohs(opp
->port_no
));
4327 pp
->hw_addr
= opp
->hw_addr
;
4328 ovs_strlcpy_arrays(pp
->name
, opp
->name
);
4330 pp
->config
= ntohl(opp
->config
) & OFPPC10_ALL
;
4331 pp
->state
= ntohl(opp
->state
) & OFPPS10_ALL
;
4333 pp
->curr
= netdev_port_features_from_ofp10(opp
->curr
);
4334 pp
->advertised
= netdev_port_features_from_ofp10(opp
->advertised
);
4335 pp
->supported
= netdev_port_features_from_ofp10(opp
->supported
);
4336 pp
->peer
= netdev_port_features_from_ofp10(opp
->peer
);
4338 pp
->curr_speed
= netdev_features_to_bps(pp
->curr
, 0) / 1000;
4339 pp
->max_speed
= netdev_features_to_bps(pp
->supported
, 0) / 1000;
4345 ofputil_decode_ofp11_port(struct ofputil_phy_port
*pp
,
4346 const struct ofp11_port
*op
)
4350 error
= ofputil_port_from_ofp11(op
->port_no
, &pp
->port_no
);
4354 pp
->hw_addr
= op
->hw_addr
;
4355 ovs_strlcpy_arrays(pp
->name
, op
->name
);
4357 pp
->config
= ntohl(op
->config
) & OFPPC11_ALL
;
4358 pp
->state
= ntohl(op
->state
) & OFPPS11_ALL
;
4360 pp
->curr
= netdev_port_features_from_ofp11(op
->curr
);
4361 pp
->advertised
= netdev_port_features_from_ofp11(op
->advertised
);
4362 pp
->supported
= netdev_port_features_from_ofp11(op
->supported
);
4363 pp
->peer
= netdev_port_features_from_ofp11(op
->peer
);
4365 pp
->curr_speed
= ntohl(op
->curr_speed
);
4366 pp
->max_speed
= ntohl(op
->max_speed
);
4372 parse_ofp14_port_ethernet_property(const struct ofpbuf
*payload
,
4373 struct ofputil_phy_port
*pp
)
4375 struct ofp14_port_desc_prop_ethernet
*eth
= payload
->data
;
4377 if (payload
->size
!= sizeof *eth
) {
4378 return OFPERR_OFPBPC_BAD_LEN
;
4381 pp
->curr
= netdev_port_features_from_ofp11(eth
->curr
);
4382 pp
->advertised
= netdev_port_features_from_ofp11(eth
->advertised
);
4383 pp
->supported
= netdev_port_features_from_ofp11(eth
->supported
);
4384 pp
->peer
= netdev_port_features_from_ofp11(eth
->peer
);
4386 pp
->curr_speed
= ntohl(eth
->curr_speed
);
4387 pp
->max_speed
= ntohl(eth
->max_speed
);
4393 ofputil_pull_ofp14_port_properties(const void *props
, size_t len
,
4394 struct ofputil_phy_port
*pp
)
4396 struct ofpbuf properties
= ofpbuf_const_initializer(props
, len
);
4397 while (properties
.size
> 0) {
4398 struct ofpbuf payload
;
4402 error
= ofpprop_pull(&properties
, &payload
, &type
);
4408 case OFPPDPT14_ETHERNET
:
4409 error
= parse_ofp14_port_ethernet_property(&payload
, pp
);
4413 error
= OFPPROP_UNKNOWN(true, "port", type
);
4426 ofputil_pull_ofp14_port(struct ofputil_phy_port
*pp
, struct ofpbuf
*msg
)
4428 const struct ofp14_port
*op
= ofpbuf_try_pull(msg
, sizeof *op
);
4430 return OFPERR_OFPBRC_BAD_LEN
;
4433 size_t len
= ntohs(op
->length
);
4434 if (len
< sizeof *op
|| len
- sizeof *op
> msg
->size
) {
4435 return OFPERR_OFPBRC_BAD_LEN
;
4439 enum ofperr error
= ofputil_port_from_ofp11(op
->port_no
, &pp
->port_no
);
4443 pp
->hw_addr
= op
->hw_addr
;
4444 ovs_strlcpy_arrays(pp
->name
, op
->name
);
4446 pp
->config
= ntohl(op
->config
) & OFPPC11_ALL
;
4447 pp
->state
= ntohl(op
->state
) & OFPPS11_ALL
;
4449 return ofputil_pull_ofp14_port_properties(ofpbuf_pull(msg
, len
), len
, pp
);
4453 ofputil_pull_ofp16_port(struct ofputil_phy_port
*pp
, struct ofpbuf
*msg
)
4455 const struct ofp16_port
*op
= ofpbuf_try_pull(msg
, sizeof *op
);
4457 return OFPERR_OFPBRC_BAD_LEN
;
4460 size_t len
= ntohs(op
->length
);
4461 if (len
< sizeof *op
|| len
- sizeof *op
> msg
->size
) {
4462 return OFPERR_OFPBRC_BAD_LEN
;
4466 enum ofperr error
= ofputil_port_from_ofp11(op
->port_no
, &pp
->port_no
);
4470 if (op
->hw_addr_type
& htons(OFPPHAT16_EUI48
)) {
4471 pp
->hw_addr
= op
->hw_addr
;
4473 if (op
->hw_addr_type
& htons(OFPPHAT16_EUI64
)) {
4474 pp
->hw_addr64
= op
->hw_addr64
;
4476 ovs_strlcpy_arrays(pp
->name
, op
->name
);
4478 pp
->config
= ntohl(op
->config
) & OFPPC11_ALL
;
4479 pp
->state
= ntohl(op
->state
) & OFPPS11_ALL
;
4481 return ofputil_pull_ofp14_port_properties(ofpbuf_pull(msg
, len
), len
, pp
);
4485 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port
*pp
,
4486 struct ofp10_phy_port
*opp
)
4488 memset(opp
, 0, sizeof *opp
);
4490 opp
->port_no
= htons(ofp_to_u16(pp
->port_no
));
4491 opp
->hw_addr
= pp
->hw_addr
;
4492 ovs_strlcpy_arrays(opp
->name
, pp
->name
);
4494 opp
->config
= htonl(pp
->config
& OFPPC10_ALL
);
4495 opp
->state
= htonl(pp
->state
& OFPPS10_ALL
);
4497 opp
->curr
= netdev_port_features_to_ofp10(pp
->curr
);
4498 opp
->advertised
= netdev_port_features_to_ofp10(pp
->advertised
);
4499 opp
->supported
= netdev_port_features_to_ofp10(pp
->supported
);
4500 opp
->peer
= netdev_port_features_to_ofp10(pp
->peer
);
4504 ofputil_encode_ofp11_port(const struct ofputil_phy_port
*pp
,
4505 struct ofp11_port
*op
)
4507 memset(op
, 0, sizeof *op
);
4509 op
->port_no
= ofputil_port_to_ofp11(pp
->port_no
);
4510 op
->hw_addr
= pp
->hw_addr
;
4511 ovs_strlcpy_arrays(op
->name
, pp
->name
);
4513 op
->config
= htonl(pp
->config
& OFPPC11_ALL
);
4514 op
->state
= htonl(pp
->state
& OFPPS11_ALL
);
4516 op
->curr
= netdev_port_features_to_ofp11(pp
->curr
);
4517 op
->advertised
= netdev_port_features_to_ofp11(pp
->advertised
);
4518 op
->supported
= netdev_port_features_to_ofp11(pp
->supported
);
4519 op
->peer
= netdev_port_features_to_ofp11(pp
->peer
);
4521 op
->curr_speed
= htonl(pp
->curr_speed
);
4522 op
->max_speed
= htonl(pp
->max_speed
);
4526 ofputil_encode_ofp14_port_ethernet_prop(
4527 const struct ofputil_phy_port
*pp
,
4528 struct ofp14_port_desc_prop_ethernet
*eth
)
4530 eth
->curr
= netdev_port_features_to_ofp11(pp
->curr
);
4531 eth
->advertised
= netdev_port_features_to_ofp11(pp
->advertised
);
4532 eth
->supported
= netdev_port_features_to_ofp11(pp
->supported
);
4533 eth
->peer
= netdev_port_features_to_ofp11(pp
->peer
);
4534 eth
->curr_speed
= htonl(pp
->curr_speed
);
4535 eth
->max_speed
= htonl(pp
->max_speed
);
4539 ofputil_put_ofp14_port(const struct ofputil_phy_port
*pp
, struct ofpbuf
*b
)
4541 struct ofp14_port
*op
;
4542 struct ofp14_port_desc_prop_ethernet
*eth
;
4544 ofpbuf_prealloc_tailroom(b
, sizeof *op
+ sizeof *eth
);
4546 op
= ofpbuf_put_zeros(b
, sizeof *op
);
4547 op
->port_no
= ofputil_port_to_ofp11(pp
->port_no
);
4548 op
->length
= htons(sizeof *op
+ sizeof *eth
);
4549 op
->hw_addr
= pp
->hw_addr
;
4550 ovs_strlcpy_arrays(op
->name
, pp
->name
);
4551 op
->config
= htonl(pp
->config
& OFPPC11_ALL
);
4552 op
->state
= htonl(pp
->state
& OFPPS11_ALL
);
4554 eth
= ofpprop_put_zeros(b
, OFPPDPT14_ETHERNET
, sizeof *eth
);
4555 ofputil_encode_ofp14_port_ethernet_prop(pp
, eth
);
4559 ofputil_put_ofp16_port(const struct ofputil_phy_port
*pp
, struct ofpbuf
*b
)
4561 struct ofp16_port
*op
;
4562 struct ofp14_port_desc_prop_ethernet
*eth
;
4564 ofpbuf_prealloc_tailroom(b
, sizeof *op
+ sizeof *eth
);
4566 op
= ofpbuf_put_zeros(b
, sizeof *op
);
4567 op
->port_no
= ofputil_port_to_ofp11(pp
->port_no
);
4568 op
->length
= htons(sizeof *op
+ sizeof *eth
);
4569 if (!eth_addr_is_zero(pp
->hw_addr
)) {
4570 op
->hw_addr_type
|= htons(OFPPHAT16_EUI48
);
4571 op
->hw_addr
= pp
->hw_addr
;
4573 if (!eth_addr64_is_zero(pp
->hw_addr64
)) {
4574 op
->hw_addr_type
|= htons(OFPPHAT16_EUI64
);
4575 op
->hw_addr64
= pp
->hw_addr64
;
4577 ovs_strlcpy_arrays(op
->name
, pp
->name
);
4578 op
->config
= htonl(pp
->config
& OFPPC11_ALL
);
4579 op
->state
= htonl(pp
->state
& OFPPS11_ALL
);
4581 eth
= ofpprop_put_zeros(b
, OFPPDPT14_ETHERNET
, sizeof *eth
);
4582 ofputil_encode_ofp14_port_ethernet_prop(pp
, eth
);
4586 ofputil_put_phy_port(enum ofp_version ofp_version
,
4587 const struct ofputil_phy_port
*pp
, struct ofpbuf
*b
)
4589 switch (ofp_version
) {
4590 case OFP10_VERSION
: {
4591 struct ofp10_phy_port
*opp
= ofpbuf_put_uninit(b
, sizeof *opp
);
4592 ofputil_encode_ofp10_phy_port(pp
, opp
);
4598 case OFP13_VERSION
: {
4599 struct ofp11_port
*op
= ofpbuf_put_uninit(b
, sizeof *op
);
4600 ofputil_encode_ofp11_port(pp
, op
);
4606 ofputil_put_ofp14_port(pp
, b
);
4609 ofputil_put_ofp16_port(pp
, b
);
4618 ofputil_decode_port_desc_stats_request(const struct ofp_header
*request
,
4621 struct ofpbuf b
= ofpbuf_const_initializer(request
,
4622 ntohs(request
->length
));
4623 enum ofpraw raw
= ofpraw_pull_assert(&b
);
4624 if (raw
== OFPRAW_OFPST10_PORT_DESC_REQUEST
) {
4627 } else if (raw
== OFPRAW_OFPST15_PORT_DESC_REQUEST
) {
4628 ovs_be32
*ofp11_port
;
4630 ofp11_port
= ofpbuf_pull(&b
, sizeof *ofp11_port
);
4631 return ofputil_port_from_ofp11(*ofp11_port
, port
);
4638 ofputil_encode_port_desc_stats_request(enum ofp_version ofp_version
,
4641 struct ofpbuf
*request
;
4643 switch (ofp_version
) {
4649 request
= ofpraw_alloc(OFPRAW_OFPST10_PORT_DESC_REQUEST
,
4653 case OFP16_VERSION
:{
4654 struct ofp15_port_desc_request
*req
;
4655 request
= ofpraw_alloc(OFPRAW_OFPST15_PORT_DESC_REQUEST
,
4657 req
= ofpbuf_put_zeros(request
, sizeof *req
);
4658 req
->port_no
= ofputil_port_to_ofp11(port
);
4669 ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port
*pp
,
4670 struct ovs_list
*replies
)
4672 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
4673 size_t start_ofs
= reply
->size
;
4675 ofputil_put_phy_port(ofpmp_version(replies
), pp
, reply
);
4676 ofpmp_postappend(replies
, start_ofs
);
4679 /* ofputil_switch_config */
4681 /* Decodes 'oh', which must be an OFPT_GET_CONFIG_REPLY or OFPT_SET_CONFIG
4682 * message, into 'config'. Returns false if 'oh' contained any flags that
4683 * aren't specified in its version of OpenFlow, true otherwise. */
4685 ofputil_decode_switch_config(const struct ofp_header
*oh
,
4686 struct ofputil_switch_config
*config
)
4688 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
4689 ofpraw_pull_assert(&b
);
4691 const struct ofp_switch_config
*osc
= ofpbuf_pull(&b
, sizeof *osc
);
4692 config
->frag
= ntohs(osc
->flags
) & OFPC_FRAG_MASK
;
4693 config
->miss_send_len
= ntohs(osc
->miss_send_len
);
4695 ovs_be16 valid_mask
= htons(OFPC_FRAG_MASK
);
4696 if (oh
->version
< OFP13_VERSION
) {
4697 const ovs_be16 ttl_bit
= htons(OFPC_INVALID_TTL_TO_CONTROLLER
);
4698 valid_mask
|= ttl_bit
;
4699 config
->invalid_ttl_to_controller
= (osc
->flags
& ttl_bit
) != 0;
4701 config
->invalid_ttl_to_controller
= -1;
4704 return !(osc
->flags
& ~valid_mask
);
4708 ofputil_decode_get_config_reply(const struct ofp_header
*oh
,
4709 struct ofputil_switch_config
*config
)
4711 ofputil_decode_switch_config(oh
, config
);
4715 ofputil_decode_set_config(const struct ofp_header
*oh
,
4716 struct ofputil_switch_config
*config
)
4718 return (ofputil_decode_switch_config(oh
, config
)
4720 : OFPERR_OFPSCFC_BAD_FLAGS
);
4723 static struct ofpbuf
*
4724 ofputil_put_switch_config(const struct ofputil_switch_config
*config
,
4727 const struct ofp_header
*oh
= b
->data
;
4728 struct ofp_switch_config
*osc
= ofpbuf_put_zeros(b
, sizeof *osc
);
4729 osc
->flags
= htons(config
->frag
);
4730 if (config
->invalid_ttl_to_controller
> 0 && oh
->version
< OFP13_VERSION
) {
4731 osc
->flags
|= htons(OFPC_INVALID_TTL_TO_CONTROLLER
);
4733 osc
->miss_send_len
= htons(config
->miss_send_len
);
4738 ofputil_encode_get_config_reply(const struct ofp_header
*request
,
4739 const struct ofputil_switch_config
*config
)
4741 struct ofpbuf
*b
= ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY
,
4743 return ofputil_put_switch_config(config
, b
);
4747 ofputil_encode_set_config(const struct ofputil_switch_config
*config
,
4748 enum ofp_version version
)
4750 struct ofpbuf
*b
= ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG
, version
, 0);
4751 return ofputil_put_switch_config(config
, b
);
4754 /* ofputil_switch_features */
4756 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
4757 OFPC_IP_REASM | OFPC_QUEUE_STATS)
4758 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS
== OFPC_FLOW_STATS
);
4759 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS
== OFPC_TABLE_STATS
);
4760 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS
== OFPC_PORT_STATS
);
4761 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM
== OFPC_IP_REASM
);
4762 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS
== OFPC_QUEUE_STATS
);
4763 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP
== OFPC_ARP_MATCH_IP
);
4764 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_BLOCKED
== OFPC12_PORT_BLOCKED
);
4765 BUILD_ASSERT_DECL((int) OFPUTIL_C_BUNDLES
== OFPC14_BUNDLES
);
4766 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_MONITORING
== OFPC14_FLOW_MONITORING
);
4769 ofputil_capabilities_mask(enum ofp_version ofp_version
)
4771 /* Handle capabilities whose bit is unique for all OpenFlow versions */
4772 switch (ofp_version
) {
4775 return OFPC_COMMON
| OFPC_ARP_MATCH_IP
;
4778 return OFPC_COMMON
| OFPC12_PORT_BLOCKED
;
4782 return OFPC_COMMON
| OFPC12_PORT_BLOCKED
| OFPC14_BUNDLES
4783 | OFPC14_FLOW_MONITORING
;
4785 /* Caller needs to check osf->header.version itself */
4790 /* Pulls an OpenFlow "switch_features" structure from 'b' and decodes it into
4791 * an abstract representation in '*features', readying 'b' to iterate over the
4792 * OpenFlow port structures following 'osf' with later calls to
4793 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an OFPERR_*
4796 ofputil_pull_switch_features(struct ofpbuf
*b
,
4797 struct ofputil_switch_features
*features
)
4799 const struct ofp_header
*oh
= b
->data
;
4800 enum ofpraw raw
= ofpraw_pull_assert(b
);
4801 const struct ofp_switch_features
*osf
= ofpbuf_pull(b
, sizeof *osf
);
4802 features
->datapath_id
= ntohll(osf
->datapath_id
);
4803 features
->n_buffers
= ntohl(osf
->n_buffers
);
4804 features
->n_tables
= osf
->n_tables
;
4805 features
->auxiliary_id
= 0;
4807 features
->capabilities
= ntohl(osf
->capabilities
) &
4808 ofputil_capabilities_mask(oh
->version
);
4810 if (raw
== OFPRAW_OFPT10_FEATURES_REPLY
) {
4811 if (osf
->capabilities
& htonl(OFPC10_STP
)) {
4812 features
->capabilities
|= OFPUTIL_C_STP
;
4814 features
->ofpacts
= ofpact_bitmap_from_openflow(osf
->actions
,
4816 } else if (raw
== OFPRAW_OFPT11_FEATURES_REPLY
4817 || raw
== OFPRAW_OFPT13_FEATURES_REPLY
) {
4818 if (osf
->capabilities
& htonl(OFPC11_GROUP_STATS
)) {
4819 features
->capabilities
|= OFPUTIL_C_GROUP_STATS
;
4821 features
->ofpacts
= 0;
4822 if (raw
== OFPRAW_OFPT13_FEATURES_REPLY
) {
4823 features
->auxiliary_id
= osf
->auxiliary_id
;
4826 return OFPERR_OFPBRC_BAD_VERSION
;
4832 /* In OpenFlow 1.0, 1.1, and 1.2, an OFPT_FEATURES_REPLY message lists all the
4833 * switch's ports, unless there are too many to fit. In OpenFlow 1.3 and
4834 * later, an OFPT_FEATURES_REPLY does not list ports at all.
4836 * Given a buffer 'b' that contains a Features Reply message, this message
4837 * checks if it contains a complete list of the switch's ports. Returns true,
4838 * if so. Returns false if the list is missing (OF1.3+) or incomplete
4839 * (OF1.0/1.1/1.2), and in the latter case removes all of the ports from the
4842 * When this function returns false, the caller should send an OFPST_PORT_DESC
4843 * stats request to get the ports. */
4845 ofputil_switch_features_has_ports(struct ofpbuf
*b
)
4847 struct ofp_header
*oh
= b
->data
;
4848 size_t phy_port_size
;
4850 if (oh
->version
>= OFP13_VERSION
) {
4851 /* OpenFlow 1.3+ never has ports in the feature reply. */
4855 phy_port_size
= (oh
->version
== OFP10_VERSION
4856 ? sizeof(struct ofp10_phy_port
)
4857 : sizeof(struct ofp11_port
));
4858 if (ntohs(oh
->length
) + phy_port_size
<= UINT16_MAX
) {
4859 /* There's room for additional ports in the feature reply.
4860 * Assume that the list is complete. */
4864 /* The feature reply has no room for more ports. Probably the list is
4865 * truncated. Drop the ports and tell the caller to retrieve them with
4866 * OFPST_PORT_DESC. */
4867 b
->size
= sizeof *oh
+ sizeof(struct ofp_switch_features
);
4868 ofpmsg_update_length(b
);
4872 /* Returns a buffer owned by the caller that encodes 'features' in the format
4873 * required by 'protocol' with the given 'xid'. The caller should append port
4874 * information to the buffer with subsequent calls to
4875 * ofputil_put_switch_features_port(). */
4877 ofputil_encode_switch_features(const struct ofputil_switch_features
*features
,
4878 enum ofputil_protocol protocol
, ovs_be32 xid
)
4880 struct ofp_switch_features
*osf
;
4882 enum ofp_version version
;
4885 version
= ofputil_protocol_to_ofp_version(protocol
);
4888 raw
= OFPRAW_OFPT10_FEATURES_REPLY
;
4892 raw
= OFPRAW_OFPT11_FEATURES_REPLY
;
4898 raw
= OFPRAW_OFPT13_FEATURES_REPLY
;
4903 b
= ofpraw_alloc_xid(raw
, version
, xid
, 0);
4904 osf
= ofpbuf_put_zeros(b
, sizeof *osf
);
4905 osf
->datapath_id
= htonll(features
->datapath_id
);
4906 osf
->n_buffers
= htonl(features
->n_buffers
);
4907 osf
->n_tables
= features
->n_tables
;
4909 osf
->capabilities
= htonl(features
->capabilities
&
4910 ofputil_capabilities_mask(version
));
4913 if (features
->capabilities
& OFPUTIL_C_STP
) {
4914 osf
->capabilities
|= htonl(OFPC10_STP
);
4916 osf
->actions
= ofpact_bitmap_to_openflow(features
->ofpacts
,
4923 osf
->auxiliary_id
= features
->auxiliary_id
;
4927 if (features
->capabilities
& OFPUTIL_C_GROUP_STATS
) {
4928 osf
->capabilities
|= htonl(OFPC11_GROUP_STATS
);
4938 /* Encodes 'pp' into the format required by the switch_features message already
4939 * in 'b', which should have been returned by ofputil_encode_switch_features(),
4940 * and appends the encoded version to 'b'. */
4942 ofputil_put_switch_features_port(const struct ofputil_phy_port
*pp
,
4945 const struct ofp_header
*oh
= b
->data
;
4947 if (oh
->version
< OFP13_VERSION
) {
4948 /* Try adding a port description to the message, but drop it again if
4949 * the buffer overflows. (This possibility for overflow is why
4950 * OpenFlow 1.3+ moved port descriptions into a multipart message.) */
4951 size_t start_ofs
= b
->size
;
4952 ofputil_put_phy_port(oh
->version
, pp
, b
);
4953 if (b
->size
> UINT16_MAX
) {
4954 b
->size
= start_ofs
;
4959 /* ofputil_port_status */
4961 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
4962 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
4964 ofputil_decode_port_status(const struct ofp_header
*oh
,
4965 struct ofputil_port_status
*ps
)
4967 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
4968 ofpraw_pull_assert(&b
);
4970 const struct ofp_port_status
*ops
= ofpbuf_pull(&b
, sizeof *ops
);
4971 if (ops
->reason
!= OFPPR_ADD
&&
4972 ops
->reason
!= OFPPR_DELETE
&&
4973 ops
->reason
!= OFPPR_MODIFY
) {
4974 return OFPERR_NXBRC_BAD_REASON
;
4976 ps
->reason
= ops
->reason
;
4978 int retval
= ofputil_pull_phy_port(oh
->version
, &b
, &ps
->desc
);
4979 ovs_assert(retval
!= EOF
);
4983 /* Converts the abstract form of a "port status" message in '*ps' into an
4984 * OpenFlow message suitable for 'protocol', and returns that encoded form in
4985 * a buffer owned by the caller. */
4987 ofputil_encode_port_status(const struct ofputil_port_status
*ps
,
4988 enum ofputil_protocol protocol
)
4990 struct ofp_port_status
*ops
;
4992 enum ofp_version version
;
4995 version
= ofputil_protocol_to_ofp_version(protocol
);
4998 raw
= OFPRAW_OFPT10_PORT_STATUS
;
5004 raw
= OFPRAW_OFPT11_PORT_STATUS
;
5009 raw
= OFPRAW_OFPT14_PORT_STATUS
;
5013 raw
= OFPRAW_OFPT16_PORT_STATUS
;
5020 b
= ofpraw_alloc_xid(raw
, version
, htonl(0), 0);
5021 ops
= ofpbuf_put_zeros(b
, sizeof *ops
);
5022 ops
->reason
= ps
->reason
;
5023 ofputil_put_phy_port(version
, &ps
->desc
, b
);
5024 ofpmsg_update_length(b
);
5028 /* ofputil_port_mod */
5031 parse_port_mod_ethernet_property(struct ofpbuf
*property
,
5032 struct ofputil_port_mod
*pm
)
5037 error
= ofpprop_parse_be32(property
, &advertise
);
5039 pm
->advertise
= netdev_port_features_from_ofp11(advertise
);
5045 ofputil_decode_ofp10_port_mod(const struct ofp10_port_mod
*opm
,
5046 struct ofputil_port_mod
*pm
)
5048 pm
->port_no
= u16_to_ofp(ntohs(opm
->port_no
));
5049 pm
->hw_addr
= opm
->hw_addr
;
5050 pm
->config
= ntohl(opm
->config
) & OFPPC10_ALL
;
5051 pm
->mask
= ntohl(opm
->mask
) & OFPPC10_ALL
;
5052 pm
->advertise
= netdev_port_features_from_ofp10(opm
->advertise
);
5057 ofputil_decode_ofp11_port_mod(const struct ofp11_port_mod
*opm
,
5058 struct ofputil_port_mod
*pm
)
5062 error
= ofputil_port_from_ofp11(opm
->port_no
, &pm
->port_no
);
5067 pm
->hw_addr
= opm
->hw_addr
;
5068 pm
->config
= ntohl(opm
->config
) & OFPPC11_ALL
;
5069 pm
->mask
= ntohl(opm
->mask
) & OFPPC11_ALL
;
5070 pm
->advertise
= netdev_port_features_from_ofp11(opm
->advertise
);
5076 ofputil_decode_ofp14_port_mod_properties(struct ofpbuf
*b
, bool loose
,
5077 struct ofputil_port_mod
*pm
)
5079 while (b
->size
> 0) {
5080 struct ofpbuf property
;
5084 error
= ofpprop_pull(b
, &property
, &type
);
5090 case OFPPMPT14_ETHERNET
:
5091 error
= parse_port_mod_ethernet_property(&property
, pm
);
5095 error
= OFPPROP_UNKNOWN(loose
, "port_mod", type
);
5107 ofputil_decode_ofp14_port_mod(struct ofpbuf
*b
, bool loose
,
5108 struct ofputil_port_mod
*pm
)
5110 const struct ofp14_port_mod
*opm
= ofpbuf_pull(b
, sizeof *opm
);
5111 enum ofperr error
= ofputil_port_from_ofp11(opm
->port_no
, &pm
->port_no
);
5116 pm
->hw_addr
= opm
->hw_addr
;
5117 pm
->config
= ntohl(opm
->config
) & OFPPC11_ALL
;
5118 pm
->mask
= ntohl(opm
->mask
) & OFPPC11_ALL
;
5120 return ofputil_decode_ofp14_port_mod_properties(b
, loose
, pm
);
5124 ofputil_decode_ofp16_port_mod(struct ofpbuf
*b
, bool loose
,
5125 struct ofputil_port_mod
*pm
)
5127 const struct ofp16_port_mod
*opm
= ofpbuf_pull(b
, sizeof *opm
);
5128 enum ofperr error
= ofputil_port_from_ofp11(opm
->port_no
, &pm
->port_no
);
5133 if (opm
->hw_addr_type
& htons(OFPPHAT16_EUI48
)) {
5134 pm
->hw_addr
= opm
->hw_addr
;
5136 if (opm
->hw_addr_type
& htons(OFPPHAT16_EUI64
)) {
5137 pm
->hw_addr64
= opm
->hw_addr64
;
5139 pm
->hw_addr
= opm
->hw_addr
;
5140 pm
->config
= ntohl(opm
->config
) & OFPPC11_ALL
;
5141 pm
->mask
= ntohl(opm
->mask
) & OFPPC11_ALL
;
5143 return ofputil_decode_ofp14_port_mod_properties(b
, loose
, pm
);
5146 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
5147 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
5149 ofputil_decode_port_mod(const struct ofp_header
*oh
,
5150 struct ofputil_port_mod
*pm
, bool loose
)
5152 memset(pm
, 0, sizeof *pm
);
5154 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
5155 enum ofpraw raw
= ofpraw_pull_assert(&b
);
5156 if (raw
== OFPRAW_OFPT10_PORT_MOD
) {
5157 return ofputil_decode_ofp10_port_mod(b
.data
, pm
);
5158 } else if (raw
== OFPRAW_OFPT11_PORT_MOD
) {
5159 return ofputil_decode_ofp11_port_mod(b
.data
, pm
);
5160 } else if (raw
== OFPRAW_OFPT14_PORT_MOD
) {
5161 return ofputil_decode_ofp14_port_mod(&b
, loose
, pm
);
5162 } else if (raw
== OFPRAW_OFPT16_PORT_MOD
) {
5163 return ofputil_decode_ofp16_port_mod(&b
, loose
, pm
);
5165 return OFPERR_OFPBRC_BAD_TYPE
;
5168 pm
->config
&= pm
->mask
;
5172 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
5173 * message suitable for 'protocol', and returns that encoded form in a buffer
5174 * owned by the caller. */
5176 ofputil_encode_port_mod(const struct ofputil_port_mod
*pm
,
5177 enum ofputil_protocol protocol
)
5179 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
5182 switch (ofp_version
) {
5183 case OFP10_VERSION
: {
5184 struct ofp10_port_mod
*opm
;
5186 b
= ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD
, ofp_version
, 0);
5187 opm
= ofpbuf_put_zeros(b
, sizeof *opm
);
5188 opm
->port_no
= htons(ofp_to_u16(pm
->port_no
));
5189 opm
->hw_addr
= pm
->hw_addr
;
5190 opm
->config
= htonl(pm
->config
& OFPPC10_ALL
);
5191 opm
->mask
= htonl(pm
->mask
& OFPPC10_ALL
);
5192 opm
->advertise
= netdev_port_features_to_ofp10(pm
->advertise
);
5198 case OFP13_VERSION
: {
5199 struct ofp11_port_mod
*opm
;
5201 b
= ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD
, ofp_version
, 0);
5202 opm
= ofpbuf_put_zeros(b
, sizeof *opm
);
5203 opm
->port_no
= ofputil_port_to_ofp11(pm
->port_no
);
5204 opm
->hw_addr
= pm
->hw_addr
;
5205 opm
->config
= htonl(pm
->config
& OFPPC11_ALL
);
5206 opm
->mask
= htonl(pm
->mask
& OFPPC11_ALL
);
5207 opm
->advertise
= netdev_port_features_to_ofp11(pm
->advertise
);
5211 case OFP15_VERSION
: {
5212 struct ofp14_port_mod
*opm
;
5214 b
= ofpraw_alloc(OFPRAW_OFPT14_PORT_MOD
, ofp_version
, 0);
5215 opm
= ofpbuf_put_zeros(b
, sizeof *opm
);
5216 opm
->port_no
= ofputil_port_to_ofp11(pm
->port_no
);
5217 opm
->hw_addr
= pm
->hw_addr
;
5218 opm
->config
= htonl(pm
->config
& OFPPC11_ALL
);
5219 opm
->mask
= htonl(pm
->mask
& OFPPC11_ALL
);
5221 if (pm
->advertise
) {
5222 ofpprop_put_be32(b
, OFPPMPT14_ETHERNET
,
5223 netdev_port_features_to_ofp11(pm
->advertise
));
5227 case OFP16_VERSION
: {
5228 struct ofp16_port_mod
*opm
;
5230 b
= ofpraw_alloc(OFPRAW_OFPT16_PORT_MOD
, ofp_version
, 0);
5231 opm
= ofpbuf_put_zeros(b
, sizeof *opm
);
5232 opm
->port_no
= ofputil_port_to_ofp11(pm
->port_no
);
5233 if (!eth_addr_is_zero(pm
->hw_addr
)) {
5234 opm
->hw_addr_type
|= htons(OFPPHAT16_EUI48
);
5235 opm
->hw_addr
= pm
->hw_addr
;
5237 if (!eth_addr64_is_zero(pm
->hw_addr64
)) {
5238 opm
->hw_addr_type
|= htons(OFPPHAT16_EUI64
);
5239 opm
->hw_addr64
= pm
->hw_addr64
;
5241 opm
->config
= htonl(pm
->config
& OFPPC11_ALL
);
5242 opm
->mask
= htonl(pm
->mask
& OFPPC11_ALL
);
5244 if (pm
->advertise
) {
5245 ofpprop_put_be32(b
, OFPPMPT14_ETHERNET
,
5246 netdev_port_features_to_ofp11(pm
->advertise
));
5257 /* Table features. */
5260 pull_table_feature_property(struct ofpbuf
*msg
, struct ofpbuf
*payload
,
5265 error
= ofpprop_pull(msg
, payload
, typep
);
5266 if (payload
&& !error
) {
5267 ofpbuf_pull(payload
, (char *)payload
->msg
- (char *)payload
->header
);
5273 parse_action_bitmap(struct ofpbuf
*payload
, enum ofp_version ofp_version
,
5278 while (payload
->size
> 0) {
5282 error
= ofpprop_pull__(payload
, NULL
, 1, 0x10000, &type
);
5286 if (type
< CHAR_BIT
* sizeof types
) {
5287 types
|= 1u << type
;
5291 *ofpacts
= ofpact_bitmap_from_openflow(htonl(types
), ofp_version
);
5296 parse_instruction_ids(struct ofpbuf
*payload
, bool loose
, uint32_t *insts
)
5299 while (payload
->size
> 0) {
5300 enum ovs_instruction_type inst
;
5304 /* OF1.3 and OF1.4 aren't clear about padding in the instruction IDs.
5305 * It seems clear that they aren't padded to 8 bytes, though, because
5306 * both standards say that "non-experimenter instructions are 4 bytes"
5307 * and do not mention any padding before the first instruction ID.
5308 * (There wouldn't be any point in padding to 8 bytes if the IDs were
5309 * aligned on an odd 4-byte boundary.)
5311 * Anyway, we just assume they're all glommed together on byte
5313 error
= ofpprop_pull__(payload
, NULL
, 1, 0x10000, &ofpit
);
5318 error
= ovs_instruction_type_from_inst_type(&inst
, ofpit
);
5320 *insts
|= 1u << inst
;
5321 } else if (!loose
) {
5329 parse_table_features_next_table(struct ofpbuf
*payload
,
5330 unsigned long int *next_tables
)
5334 memset(next_tables
, 0, bitmap_n_bytes(255));
5335 for (i
= 0; i
< payload
->size
; i
++) {
5336 uint8_t id
= ((const uint8_t *) payload
->data
)[i
];
5338 return OFPERR_OFPBPC_BAD_VALUE
;
5340 bitmap_set1(next_tables
, id
);
5346 parse_oxms(struct ofpbuf
*payload
, bool loose
,
5347 struct mf_bitmap
*exactp
, struct mf_bitmap
*maskedp
)
5349 struct mf_bitmap exact
= MF_BITMAP_INITIALIZER
;
5350 struct mf_bitmap masked
= MF_BITMAP_INITIALIZER
;
5352 while (payload
->size
> 0) {
5353 const struct mf_field
*field
;
5357 error
= nx_pull_header(payload
, NULL
, &field
, &hasmask
);
5359 bitmap_set1(hasmask
? masked
.bm
: exact
.bm
, field
->id
);
5360 } else if (error
!= OFPERR_OFPBMC_BAD_FIELD
|| !loose
) {
5366 } else if (!bitmap_is_all_zeros(exact
.bm
, MFF_N_IDS
)) {
5367 return OFPERR_OFPBMC_BAD_MASK
;
5371 } else if (!bitmap_is_all_zeros(masked
.bm
, MFF_N_IDS
)) {
5372 return OFPERR_OFPBMC_BAD_MASK
;
5377 /* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract
5378 * ofputil_table_features in 'tf'.
5380 * If 'loose' is true, this function ignores properties and values that it does
5381 * not understand, as a controller would want to do when interpreting
5382 * capabilities provided by a switch. If 'loose' is false, this function
5383 * treats unknown properties and values as an error, as a switch would want to
5384 * do when interpreting a configuration request made by a controller.
5386 * A single OpenFlow message can specify features for multiple tables. Calling
5387 * this function multiple times for a single 'msg' iterates through the tables
5388 * in the message. The caller must initially leave 'msg''s layer pointers null
5389 * and not modify them between calls.
5391 * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise
5392 * a positive "enum ofperr" value. */
5394 ofputil_decode_table_features(struct ofpbuf
*msg
,
5395 struct ofputil_table_features
*tf
, bool loose
)
5397 memset(tf
, 0, sizeof *tf
);
5400 ofpraw_pull_assert(msg
);
5407 const struct ofp_header
*oh
= msg
->header
;
5408 struct ofp13_table_features
*otf
= msg
->data
;
5409 if (msg
->size
< sizeof *otf
) {
5410 return OFPERR_OFPBPC_BAD_LEN
;
5413 unsigned int len
= ntohs(otf
->length
);
5414 if (len
< sizeof *otf
|| len
% 8 || len
> msg
->size
) {
5415 return OFPERR_OFPBPC_BAD_LEN
;
5418 tf
->table_id
= otf
->table_id
;
5419 if (tf
->table_id
== OFPTT_ALL
) {
5420 return OFPERR_OFPTFFC_BAD_TABLE
;
5423 ovs_strlcpy_arrays(tf
->name
, otf
->name
);
5424 tf
->metadata_match
= otf
->metadata_match
;
5425 tf
->metadata_write
= otf
->metadata_write
;
5426 tf
->miss_config
= OFPUTIL_TABLE_MISS_DEFAULT
;
5427 if (oh
->version
>= OFP14_VERSION
) {
5428 uint32_t caps
= ntohl(otf
->capabilities
);
5429 tf
->supports_eviction
= (caps
& OFPTC14_EVICTION
) != 0;
5430 tf
->supports_vacancy_events
= (caps
& OFPTC14_VACANCY_EVENTS
) != 0;
5432 tf
->supports_eviction
= -1;
5433 tf
->supports_vacancy_events
= -1;
5435 tf
->max_entries
= ntohl(otf
->max_entries
);
5437 struct ofpbuf properties
= ofpbuf_const_initializer(ofpbuf_pull(msg
, len
),
5439 ofpbuf_pull(&properties
, sizeof *otf
);
5440 while (properties
.size
> 0) {
5441 struct ofpbuf payload
;
5445 error
= pull_table_feature_property(&properties
, &payload
, &type
);
5450 switch ((enum ofp13_table_feature_prop_type
) type
) {
5451 case OFPTFPT13_INSTRUCTIONS
:
5452 error
= parse_instruction_ids(&payload
, loose
,
5453 &tf
->nonmiss
.instructions
);
5455 case OFPTFPT13_INSTRUCTIONS_MISS
:
5456 error
= parse_instruction_ids(&payload
, loose
,
5457 &tf
->miss
.instructions
);
5460 case OFPTFPT13_NEXT_TABLES
:
5461 error
= parse_table_features_next_table(&payload
,
5464 case OFPTFPT13_NEXT_TABLES_MISS
:
5465 error
= parse_table_features_next_table(&payload
, tf
->miss
.next
);
5468 case OFPTFPT13_WRITE_ACTIONS
:
5469 error
= parse_action_bitmap(&payload
, oh
->version
,
5470 &tf
->nonmiss
.write
.ofpacts
);
5472 case OFPTFPT13_WRITE_ACTIONS_MISS
:
5473 error
= parse_action_bitmap(&payload
, oh
->version
,
5474 &tf
->miss
.write
.ofpacts
);
5477 case OFPTFPT13_APPLY_ACTIONS
:
5478 error
= parse_action_bitmap(&payload
, oh
->version
,
5479 &tf
->nonmiss
.apply
.ofpacts
);
5481 case OFPTFPT13_APPLY_ACTIONS_MISS
:
5482 error
= parse_action_bitmap(&payload
, oh
->version
,
5483 &tf
->miss
.apply
.ofpacts
);
5486 case OFPTFPT13_MATCH
:
5487 error
= parse_oxms(&payload
, loose
, &tf
->match
, &tf
->mask
);
5489 case OFPTFPT13_WILDCARDS
:
5490 error
= parse_oxms(&payload
, loose
, &tf
->wildcard
, NULL
);
5493 case OFPTFPT13_WRITE_SETFIELD
:
5494 error
= parse_oxms(&payload
, loose
,
5495 &tf
->nonmiss
.write
.set_fields
, NULL
);
5497 case OFPTFPT13_WRITE_SETFIELD_MISS
:
5498 error
= parse_oxms(&payload
, loose
,
5499 &tf
->miss
.write
.set_fields
, NULL
);
5501 case OFPTFPT13_APPLY_SETFIELD
:
5502 error
= parse_oxms(&payload
, loose
,
5503 &tf
->nonmiss
.apply
.set_fields
, NULL
);
5505 case OFPTFPT13_APPLY_SETFIELD_MISS
:
5506 error
= parse_oxms(&payload
, loose
,
5507 &tf
->miss
.apply
.set_fields
, NULL
);
5510 case OFPTFPT13_EXPERIMENTER
:
5511 case OFPTFPT13_EXPERIMENTER_MISS
:
5513 error
= OFPPROP_UNKNOWN(loose
, "table features", type
);
5521 /* Fix inconsistencies:
5523 * - Turn on 'match' bits that are set in 'mask', because maskable
5524 * fields are matchable.
5526 * - Turn on 'wildcard' bits that are set in 'mask', because a field
5527 * that is arbitrarily maskable can be wildcarded entirely.
5529 * - Turn off 'wildcard' bits that are not in 'match', because a field
5530 * must be matchable for it to be meaningfully wildcarded. */
5531 bitmap_or(tf
->match
.bm
, tf
->mask
.bm
, MFF_N_IDS
);
5532 bitmap_or(tf
->wildcard
.bm
, tf
->mask
.bm
, MFF_N_IDS
);
5533 bitmap_and(tf
->wildcard
.bm
, tf
->match
.bm
, MFF_N_IDS
);
5538 /* Encodes and returns a request to obtain the table features of a switch.
5539 * The message is encoded for OpenFlow version 'ofp_version'. */
5541 ofputil_encode_table_features_request(enum ofp_version ofp_version
)
5543 struct ofpbuf
*request
= NULL
;
5545 switch (ofp_version
) {
5549 ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later "
5550 "(\'-O OpenFlow13\')");
5555 request
= ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST
,
5566 put_fields_property(struct ofpbuf
*reply
,
5567 const struct mf_bitmap
*fields
,
5568 const struct mf_bitmap
*masks
,
5569 enum ofp13_table_feature_prop_type property
,
5570 enum ofp_version version
)
5575 start_ofs
= ofpprop_start(reply
, property
);
5576 BITMAP_FOR_EACH_1 (field
, MFF_N_IDS
, fields
->bm
) {
5577 nx_put_header(reply
, field
, version
,
5578 masks
&& bitmap_is_set(masks
->bm
, field
));
5580 ofpprop_end(reply
, start_ofs
);
5584 put_table_action_features(struct ofpbuf
*reply
,
5585 const struct ofputil_table_action_features
*taf
,
5586 enum ofp13_table_feature_prop_type actions_type
,
5587 enum ofp13_table_feature_prop_type set_fields_type
,
5588 int miss_offset
, enum ofp_version version
)
5590 ofpprop_put_bitmap(reply
, actions_type
+ miss_offset
,
5591 ntohl(ofpact_bitmap_to_openflow(taf
->ofpacts
,
5593 put_fields_property(reply
, &taf
->set_fields
, NULL
,
5594 set_fields_type
+ miss_offset
, version
);
5598 put_table_instruction_features(
5599 struct ofpbuf
*reply
, const struct ofputil_table_instruction_features
*tif
,
5600 int miss_offset
, enum ofp_version version
)
5605 ofpprop_put_bitmap(reply
, OFPTFPT13_INSTRUCTIONS
+ miss_offset
,
5606 ntohl(ovsinst_bitmap_to_openflow(tif
->instructions
,
5609 start_ofs
= ofpprop_start(reply
, OFPTFPT13_NEXT_TABLES
+ miss_offset
);
5610 BITMAP_FOR_EACH_1 (table_id
, 255, tif
->next
) {
5611 ofpbuf_put(reply
, &table_id
, 1);
5613 ofpprop_end(reply
, start_ofs
);
5615 put_table_action_features(reply
, &tif
->write
,
5616 OFPTFPT13_WRITE_ACTIONS
,
5617 OFPTFPT13_WRITE_SETFIELD
, miss_offset
, version
);
5618 put_table_action_features(reply
, &tif
->apply
,
5619 OFPTFPT13_APPLY_ACTIONS
,
5620 OFPTFPT13_APPLY_SETFIELD
, miss_offset
, version
);
5624 ofputil_append_table_features_reply(const struct ofputil_table_features
*tf
,
5625 struct ovs_list
*replies
)
5627 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
5628 enum ofp_version version
= ofpmp_version(replies
);
5629 size_t start_ofs
= reply
->size
;
5630 struct ofp13_table_features
*otf
;
5632 otf
= ofpbuf_put_zeros(reply
, sizeof *otf
);
5633 otf
->table_id
= tf
->table_id
;
5634 ovs_strlcpy_arrays(otf
->name
, tf
->name
);
5635 otf
->metadata_match
= tf
->metadata_match
;
5636 otf
->metadata_write
= tf
->metadata_write
;
5637 if (version
>= OFP14_VERSION
) {
5638 if (tf
->supports_eviction
) {
5639 otf
->capabilities
|= htonl(OFPTC14_EVICTION
);
5641 if (tf
->supports_vacancy_events
) {
5642 otf
->capabilities
|= htonl(OFPTC14_VACANCY_EVENTS
);
5645 otf
->max_entries
= htonl(tf
->max_entries
);
5647 put_table_instruction_features(reply
, &tf
->nonmiss
, 0, version
);
5648 put_table_instruction_features(reply
, &tf
->miss
, 1, version
);
5650 put_fields_property(reply
, &tf
->match
, &tf
->mask
,
5651 OFPTFPT13_MATCH
, version
);
5652 put_fields_property(reply
, &tf
->wildcard
, NULL
,
5653 OFPTFPT13_WILDCARDS
, version
);
5655 otf
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *otf
);
5656 otf
->length
= htons(reply
->size
- start_ofs
);
5657 ofpmp_postappend(replies
, start_ofs
);
5661 parse_table_desc_vacancy_property(struct ofpbuf
*property
,
5662 struct ofputil_table_desc
*td
)
5664 struct ofp14_table_mod_prop_vacancy
*otv
= property
->data
;
5666 if (property
->size
!= sizeof *otv
) {
5667 return OFPERR_OFPBPC_BAD_LEN
;
5670 td
->table_vacancy
.vacancy_down
= otv
->vacancy_down
;
5671 td
->table_vacancy
.vacancy_up
= otv
->vacancy_up
;
5672 td
->table_vacancy
.vacancy
= otv
->vacancy
;
5676 /* Decodes the next OpenFlow "table desc" message (of possibly several) from
5677 * 'msg' into an abstract form in '*td'. Returns 0 if successful, EOF if the
5678 * last "table desc" in 'msg' was already decoded, otherwise an OFPERR_*
5681 ofputil_decode_table_desc(struct ofpbuf
*msg
,
5682 struct ofputil_table_desc
*td
,
5683 enum ofp_version version
)
5685 memset(td
, 0, sizeof *td
);
5688 ofpraw_pull_assert(msg
);
5695 struct ofp14_table_desc
*otd
= ofpbuf_try_pull(msg
, sizeof *otd
);
5697 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFP14_TABLE_DESC reply has %"PRIu32
" "
5698 "leftover bytes at end", msg
->size
);
5699 return OFPERR_OFPBRC_BAD_LEN
;
5702 td
->table_id
= otd
->table_id
;
5703 size_t length
= ntohs(otd
->length
);
5704 if (length
< sizeof *otd
|| length
- sizeof *otd
> msg
->size
) {
5705 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFP14_TABLE_DESC reply claims invalid "
5706 "length %"PRIuSIZE
, length
);
5707 return OFPERR_OFPBRC_BAD_LEN
;
5709 length
-= sizeof *otd
;
5711 td
->eviction
= ofputil_decode_table_eviction(otd
->config
, version
);
5712 td
->vacancy
= ofputil_decode_table_vacancy(otd
->config
, version
);
5713 td
->eviction_flags
= UINT32_MAX
;
5715 struct ofpbuf properties
= ofpbuf_const_initializer(
5716 ofpbuf_pull(msg
, length
), length
);
5717 while (properties
.size
> 0) {
5718 struct ofpbuf payload
;
5722 error
= ofpprop_pull(&properties
, &payload
, &type
);
5728 case OFPTMPT14_EVICTION
:
5729 error
= ofpprop_parse_u32(&payload
, &td
->eviction_flags
);
5732 case OFPTMPT14_VACANCY
:
5733 error
= parse_table_desc_vacancy_property(&payload
, td
);
5737 error
= OFPPROP_UNKNOWN(true, "table_desc", type
);
5749 /* Encodes and returns a request to obtain description of tables of a switch.
5750 * The message is encoded for OpenFlow version 'ofp_version'. */
5752 ofputil_encode_table_desc_request(enum ofp_version ofp_version
)
5754 struct ofpbuf
*request
= NULL
;
5756 if (ofp_version
>= OFP14_VERSION
) {
5757 request
= ofpraw_alloc(OFPRAW_OFPST14_TABLE_DESC_REQUEST
,
5760 ovs_fatal(0, "dump-table-desc needs OpenFlow 1.4 or later "
5761 "(\'-O OpenFlow14\')");
5767 /* Function to append Table desc information in a reply list. */
5769 ofputil_append_table_desc_reply(const struct ofputil_table_desc
*td
,
5770 struct ovs_list
*replies
,
5771 enum ofp_version version
)
5773 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
5775 struct ofp14_table_desc
*otd
;
5777 start_otd
= reply
->size
;
5778 ofpbuf_put_zeros(reply
, sizeof *otd
);
5779 if (td
->eviction_flags
!= UINT32_MAX
) {
5780 ofpprop_put_u32(reply
, OFPTMPT14_EVICTION
, td
->eviction_flags
);
5782 if (td
->vacancy
== OFPUTIL_TABLE_VACANCY_ON
) {
5783 struct ofp14_table_mod_prop_vacancy
*otv
;
5785 otv
= ofpprop_put_zeros(reply
, OFPTMPT14_VACANCY
, sizeof *otv
);
5786 otv
->vacancy_down
= td
->table_vacancy
.vacancy_down
;
5787 otv
->vacancy_up
= td
->table_vacancy
.vacancy_up
;
5788 otv
->vacancy
= td
->table_vacancy
.vacancy
;
5791 otd
= ofpbuf_at_assert(reply
, start_otd
, sizeof *otd
);
5792 otd
->length
= htons(reply
->size
- start_otd
);
5793 otd
->table_id
= td
->table_id
;
5794 otd
->config
= ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT
,
5795 td
->eviction
, td
->vacancy
,
5797 ofpmp_postappend(replies
, start_otd
);
5800 /* This function parses Vacancy property, and decodes the
5801 * ofp14_table_mod_prop_vacancy in ofputil_table_mod.
5802 * Returns OFPERR_OFPBPC_BAD_VALUE error code when vacancy_down is
5803 * greater than vacancy_up and also when current vacancy has non-zero
5804 * value. Returns 0 on success. */
5806 parse_table_mod_vacancy_property(struct ofpbuf
*property
,
5807 struct ofputil_table_mod
*tm
)
5809 struct ofp14_table_mod_prop_vacancy
*otv
= property
->data
;
5811 if (property
->size
!= sizeof *otv
) {
5812 return OFPERR_OFPBPC_BAD_LEN
;
5814 tm
->table_vacancy
.vacancy_down
= otv
->vacancy_down
;
5815 tm
->table_vacancy
.vacancy_up
= otv
->vacancy_up
;
5816 if (tm
->table_vacancy
.vacancy_down
> tm
->table_vacancy
.vacancy_up
) {
5817 OFPPROP_LOG(&bad_ofmsg_rl
, false,
5818 "Value of vacancy_down is greater than vacancy_up");
5819 return OFPERR_OFPBPC_BAD_VALUE
;
5821 if (tm
->table_vacancy
.vacancy_down
> 100 ||
5822 tm
->table_vacancy
.vacancy_up
> 100) {
5823 OFPPROP_LOG(&bad_ofmsg_rl
, false, "Vacancy threshold percentage "
5824 "should not be greater than 100");
5825 return OFPERR_OFPBPC_BAD_VALUE
;
5827 tm
->table_vacancy
.vacancy
= otv
->vacancy
;
5828 if (tm
->table_vacancy
.vacancy
) {
5829 OFPPROP_LOG(&bad_ofmsg_rl
, false,
5830 "Vacancy value should be zero for table-mod messages");
5831 return OFPERR_OFPBPC_BAD_VALUE
;
5836 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5837 * table configuration (a table mod, table stats, or table features message),
5838 * returns the table vacancy configuration that it specifies.
5840 * Only OpenFlow 1.4 and later specify table vacancy configuration this way,
5841 * so for other 'version' this function always returns
5842 * OFPUTIL_TABLE_VACANCY_DEFAULT. */
5843 static enum ofputil_table_vacancy
5844 ofputil_decode_table_vacancy(ovs_be32 config
, enum ofp_version version
)
5846 return (version
< OFP14_VERSION
? OFPUTIL_TABLE_VACANCY_DEFAULT
5847 : config
& htonl(OFPTC14_VACANCY_EVENTS
) ? OFPUTIL_TABLE_VACANCY_ON
5848 : OFPUTIL_TABLE_VACANCY_OFF
);
5851 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5852 * table configuration (a table mod, table stats, or table features message),
5853 * returns the table eviction configuration that it specifies.
5855 * Only OpenFlow 1.4 and later specify table eviction configuration this way,
5856 * so for other 'version' values this function always returns
5857 * OFPUTIL_TABLE_EVICTION_DEFAULT. */
5858 static enum ofputil_table_eviction
5859 ofputil_decode_table_eviction(ovs_be32 config
, enum ofp_version version
)
5861 return (version
< OFP14_VERSION
? OFPUTIL_TABLE_EVICTION_DEFAULT
5862 : config
& htonl(OFPTC14_EVICTION
) ? OFPUTIL_TABLE_EVICTION_ON
5863 : OFPUTIL_TABLE_EVICTION_OFF
);
5866 /* Returns a bitmap of OFPTC* values suitable for 'config' fields in various
5867 * OpenFlow messages of the given 'version', based on the provided 'miss' and
5868 * 'eviction' values. */
5870 ofputil_encode_table_config(enum ofputil_table_miss miss
,
5871 enum ofputil_table_eviction eviction
,
5872 enum ofputil_table_vacancy vacancy
,
5873 enum ofp_version version
)
5875 uint32_t config
= 0;
5876 /* Search for "OFPTC_* Table Configuration" in the documentation for more
5877 * information on the crazy evolution of this field. */
5880 /* OpenFlow 1.0 didn't have such a field, any value ought to do. */
5885 /* OpenFlow 1.1 and 1.2 define only OFPTC11_TABLE_MISS_*. */
5887 case OFPUTIL_TABLE_MISS_DEFAULT
:
5888 /* Really this shouldn't be used for encoding (the caller should
5889 * provide a specific value) but I can't imagine that defaulting to
5890 * the fall-through case here will hurt. */
5891 case OFPUTIL_TABLE_MISS_CONTROLLER
:
5893 return htonl(OFPTC11_TABLE_MISS_CONTROLLER
);
5894 case OFPUTIL_TABLE_MISS_CONTINUE
:
5895 return htonl(OFPTC11_TABLE_MISS_CONTINUE
);
5896 case OFPUTIL_TABLE_MISS_DROP
:
5897 return htonl(OFPTC11_TABLE_MISS_DROP
);
5902 /* OpenFlow 1.3 removed OFPTC11_TABLE_MISS_* and didn't define any new
5903 * flags, so this is correct. */
5909 /* OpenFlow 1.4 introduced OFPTC14_EVICTION and
5910 * OFPTC14_VACANCY_EVENTS. */
5911 if (eviction
== OFPUTIL_TABLE_EVICTION_ON
) {
5912 config
|= OFPTC14_EVICTION
;
5914 if (vacancy
== OFPUTIL_TABLE_VACANCY_ON
) {
5915 config
|= OFPTC14_VACANCY_EVENTS
;
5917 return htonl(config
);
5923 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5924 * table configuration (a table mod, table stats, or table features message),
5925 * returns the table miss configuration that it specifies.
5927 * Only OpenFlow 1.1 and 1.2 specify table miss configurations this way, so for
5928 * other 'version' values this function always returns
5929 * OFPUTIL_TABLE_MISS_DEFAULT. */
5930 static enum ofputil_table_miss
5931 ofputil_decode_table_miss(ovs_be32 config_
, enum ofp_version version
)
5933 uint32_t config
= ntohl(config_
);
5935 if (version
== OFP11_VERSION
|| version
== OFP12_VERSION
) {
5936 switch (config
& OFPTC11_TABLE_MISS_MASK
) {
5937 case OFPTC11_TABLE_MISS_CONTROLLER
:
5938 return OFPUTIL_TABLE_MISS_CONTROLLER
;
5940 case OFPTC11_TABLE_MISS_CONTINUE
:
5941 return OFPUTIL_TABLE_MISS_CONTINUE
;
5943 case OFPTC11_TABLE_MISS_DROP
:
5944 return OFPUTIL_TABLE_MISS_DROP
;
5947 VLOG_WARN_RL(&bad_ofmsg_rl
, "bad table miss config %d", config
);
5948 return OFPUTIL_TABLE_MISS_CONTROLLER
;
5951 return OFPUTIL_TABLE_MISS_DEFAULT
;
5955 /* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in
5956 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
5958 ofputil_decode_table_mod(const struct ofp_header
*oh
,
5959 struct ofputil_table_mod
*pm
)
5961 memset(pm
, 0, sizeof *pm
);
5962 pm
->miss
= OFPUTIL_TABLE_MISS_DEFAULT
;
5963 pm
->eviction
= OFPUTIL_TABLE_EVICTION_DEFAULT
;
5964 pm
->eviction_flags
= UINT32_MAX
;
5965 pm
->vacancy
= OFPUTIL_TABLE_VACANCY_DEFAULT
;
5967 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
5968 enum ofpraw raw
= ofpraw_pull_assert(&b
);
5969 if (raw
== OFPRAW_OFPT11_TABLE_MOD
) {
5970 const struct ofp11_table_mod
*otm
= b
.data
;
5972 pm
->table_id
= otm
->table_id
;
5973 pm
->miss
= ofputil_decode_table_miss(otm
->config
, oh
->version
);
5974 } else if (raw
== OFPRAW_OFPT14_TABLE_MOD
) {
5975 const struct ofp14_table_mod
*otm
= ofpbuf_pull(&b
, sizeof *otm
);
5977 pm
->table_id
= otm
->table_id
;
5978 pm
->miss
= ofputil_decode_table_miss(otm
->config
, oh
->version
);
5979 pm
->eviction
= ofputil_decode_table_eviction(otm
->config
, oh
->version
);
5980 pm
->vacancy
= ofputil_decode_table_vacancy(otm
->config
, oh
->version
);
5981 while (b
.size
> 0) {
5982 struct ofpbuf property
;
5986 error
= ofpprop_pull(&b
, &property
, &type
);
5992 case OFPTMPT14_EVICTION
:
5993 error
= ofpprop_parse_u32(&property
, &pm
->eviction
);
5996 case OFPTMPT14_VACANCY
:
5997 error
= parse_table_mod_vacancy_property(&property
, pm
);
6001 error
= OFPERR_OFPBRC_BAD_TYPE
;
6010 return OFPERR_OFPBRC_BAD_TYPE
;
6016 /* Converts the abstract form of a "table mod" message in '*tm' into an
6017 * OpenFlow message suitable for 'protocol', and returns that encoded form in a
6018 * buffer owned by the caller. */
6020 ofputil_encode_table_mod(const struct ofputil_table_mod
*tm
,
6021 enum ofputil_protocol protocol
)
6023 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
6026 switch (ofp_version
) {
6027 case OFP10_VERSION
: {
6028 ovs_fatal(0, "table mod needs OpenFlow 1.1 or later "
6029 "(\'-O OpenFlow11\')");
6034 case OFP13_VERSION
: {
6035 struct ofp11_table_mod
*otm
;
6037 b
= ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD
, ofp_version
, 0);
6038 otm
= ofpbuf_put_zeros(b
, sizeof *otm
);
6039 otm
->table_id
= tm
->table_id
;
6040 otm
->config
= ofputil_encode_table_config(tm
->miss
, tm
->eviction
,
6041 tm
->vacancy
, ofp_version
);
6046 case OFP16_VERSION
: {
6047 struct ofp14_table_mod
*otm
;
6049 b
= ofpraw_alloc(OFPRAW_OFPT14_TABLE_MOD
, ofp_version
, 0);
6050 otm
= ofpbuf_put_zeros(b
, sizeof *otm
);
6051 otm
->table_id
= tm
->table_id
;
6052 otm
->config
= ofputil_encode_table_config(tm
->miss
, tm
->eviction
,
6053 tm
->vacancy
, ofp_version
);
6055 if (tm
->eviction_flags
!= UINT32_MAX
) {
6056 ofpprop_put_u32(b
, OFPTMPT14_EVICTION
, tm
->eviction_flags
);
6058 if (tm
->vacancy
== OFPUTIL_TABLE_VACANCY_ON
) {
6059 struct ofp14_table_mod_prop_vacancy
*otv
;
6061 otv
= ofpprop_put_zeros(b
, OFPTMPT14_VACANCY
, sizeof *otv
);
6062 otv
->vacancy_down
= tm
->table_vacancy
.vacancy_down
;
6063 otv
->vacancy_up
= tm
->table_vacancy
.vacancy_up
;
6074 /* ofputil_role_request */
6076 /* Decodes the OpenFlow "role request" or "role reply" message in '*oh' into
6077 * an abstract form in '*rr'. Returns 0 if successful, otherwise an
6078 * OFPERR_* value. */
6080 ofputil_decode_role_message(const struct ofp_header
*oh
,
6081 struct ofputil_role_request
*rr
)
6083 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
6084 enum ofpraw raw
= ofpraw_pull_assert(&b
);
6085 if (raw
== OFPRAW_OFPT12_ROLE_REQUEST
||
6086 raw
== OFPRAW_OFPT12_ROLE_REPLY
) {
6087 const struct ofp12_role_request
*orr
= b
.msg
;
6089 if (orr
->role
!= htonl(OFPCR12_ROLE_NOCHANGE
) &&
6090 orr
->role
!= htonl(OFPCR12_ROLE_EQUAL
) &&
6091 orr
->role
!= htonl(OFPCR12_ROLE_MASTER
) &&
6092 orr
->role
!= htonl(OFPCR12_ROLE_SLAVE
)) {
6093 return OFPERR_OFPRRFC_BAD_ROLE
;
6096 rr
->role
= ntohl(orr
->role
);
6097 if (raw
== OFPRAW_OFPT12_ROLE_REQUEST
6098 ? orr
->role
== htonl(OFPCR12_ROLE_NOCHANGE
)
6099 : orr
->generation_id
== OVS_BE64_MAX
) {
6100 rr
->have_generation_id
= false;
6101 rr
->generation_id
= 0;
6103 rr
->have_generation_id
= true;
6104 rr
->generation_id
= ntohll(orr
->generation_id
);
6106 } else if (raw
== OFPRAW_NXT_ROLE_REQUEST
||
6107 raw
== OFPRAW_NXT_ROLE_REPLY
) {
6108 const struct nx_role_request
*nrr
= b
.msg
;
6110 BUILD_ASSERT(NX_ROLE_OTHER
+ 1 == OFPCR12_ROLE_EQUAL
);
6111 BUILD_ASSERT(NX_ROLE_MASTER
+ 1 == OFPCR12_ROLE_MASTER
);
6112 BUILD_ASSERT(NX_ROLE_SLAVE
+ 1 == OFPCR12_ROLE_SLAVE
);
6114 if (nrr
->role
!= htonl(NX_ROLE_OTHER
) &&
6115 nrr
->role
!= htonl(NX_ROLE_MASTER
) &&
6116 nrr
->role
!= htonl(NX_ROLE_SLAVE
)) {
6117 return OFPERR_OFPRRFC_BAD_ROLE
;
6120 rr
->role
= ntohl(nrr
->role
) + 1;
6121 rr
->have_generation_id
= false;
6122 rr
->generation_id
= 0;
6130 /* Returns an encoded form of a role reply suitable for the "request" in a
6131 * buffer owned by the caller. */
6133 ofputil_encode_role_reply(const struct ofp_header
*request
,
6134 const struct ofputil_role_request
*rr
)
6139 raw
= ofpraw_decode_assert(request
);
6140 if (raw
== OFPRAW_OFPT12_ROLE_REQUEST
) {
6141 struct ofp12_role_request
*orr
;
6143 buf
= ofpraw_alloc_reply(OFPRAW_OFPT12_ROLE_REPLY
, request
, 0);
6144 orr
= ofpbuf_put_zeros(buf
, sizeof *orr
);
6146 orr
->role
= htonl(rr
->role
);
6147 orr
->generation_id
= htonll(rr
->have_generation_id
6150 } else if (raw
== OFPRAW_NXT_ROLE_REQUEST
) {
6151 struct nx_role_request
*nrr
;
6153 BUILD_ASSERT(NX_ROLE_OTHER
== OFPCR12_ROLE_EQUAL
- 1);
6154 BUILD_ASSERT(NX_ROLE_MASTER
== OFPCR12_ROLE_MASTER
- 1);
6155 BUILD_ASSERT(NX_ROLE_SLAVE
== OFPCR12_ROLE_SLAVE
- 1);
6157 buf
= ofpraw_alloc_reply(OFPRAW_NXT_ROLE_REPLY
, request
, 0);
6158 nrr
= ofpbuf_put_zeros(buf
, sizeof *nrr
);
6159 nrr
->role
= htonl(rr
->role
- 1);
6167 /* Encodes "role status" message 'status' for sending in the given
6168 * 'protocol'. Returns the role status message, if 'protocol' supports them,
6169 * otherwise a null pointer. */
6171 ofputil_encode_role_status(const struct ofputil_role_status
*status
,
6172 enum ofputil_protocol protocol
)
6174 enum ofp_version version
;
6176 version
= ofputil_protocol_to_ofp_version(protocol
);
6177 if (version
>= OFP14_VERSION
) {
6178 struct ofp14_role_status
*rstatus
;
6181 buf
= ofpraw_alloc_xid(OFPRAW_OFPT14_ROLE_STATUS
, version
, htonl(0),
6183 rstatus
= ofpbuf_put_zeros(buf
, sizeof *rstatus
);
6184 rstatus
->role
= htonl(status
->role
);
6185 rstatus
->reason
= status
->reason
;
6186 rstatus
->generation_id
= htonll(status
->generation_id
);
6195 ofputil_decode_role_status(const struct ofp_header
*oh
,
6196 struct ofputil_role_status
*rs
)
6198 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
6199 enum ofpraw raw
= ofpraw_pull_assert(&b
);
6200 ovs_assert(raw
== OFPRAW_OFPT14_ROLE_STATUS
);
6202 const struct ofp14_role_status
*r
= b
.msg
;
6203 if (r
->role
!= htonl(OFPCR12_ROLE_NOCHANGE
) &&
6204 r
->role
!= htonl(OFPCR12_ROLE_EQUAL
) &&
6205 r
->role
!= htonl(OFPCR12_ROLE_MASTER
) &&
6206 r
->role
!= htonl(OFPCR12_ROLE_SLAVE
)) {
6207 return OFPERR_OFPRRFC_BAD_ROLE
;
6210 rs
->role
= ntohl(r
->role
);
6211 rs
->generation_id
= ntohll(r
->generation_id
);
6212 rs
->reason
= r
->reason
;
6217 /* Encodes 'rf' according to 'protocol', and returns the encoded message.
6218 * 'protocol' must be for OpenFlow 1.4 or later. */
6220 ofputil_encode_requestforward(const struct ofputil_requestforward
*rf
,
6221 enum ofputil_protocol protocol
)
6223 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
6224 struct ofpbuf
*inner
;
6226 switch (rf
->reason
) {
6227 case OFPRFR_GROUP_MOD
:
6228 inner
= ofputil_encode_group_mod(ofp_version
, rf
->group_mod
);
6231 case OFPRFR_METER_MOD
:
6232 inner
= ofputil_encode_meter_mod(ofp_version
, rf
->meter_mod
);
6235 case OFPRFR_N_REASONS
:
6240 struct ofp_header
*inner_oh
= inner
->data
;
6241 inner_oh
->xid
= rf
->xid
;
6242 inner_oh
->length
= htons(inner
->size
);
6244 struct ofpbuf
*outer
= ofpraw_alloc_xid(OFPRAW_OFPT14_REQUESTFORWARD
,
6245 ofp_version
, htonl(0),
6247 ofpbuf_put(outer
, inner
->data
, inner
->size
);
6248 ofpbuf_delete(inner
);
6253 /* Decodes OFPT_REQUESTFORWARD message 'outer'. On success, puts the decoded
6254 * form into '*rf' and returns 0, and the caller is later responsible for
6255 * freeing the content of 'rf', with ofputil_destroy_requestforward(rf). On
6256 * failure, returns an ofperr and '*rf' is indeterminate. */
6258 ofputil_decode_requestforward(const struct ofp_header
*outer
,
6259 struct ofputil_requestforward
*rf
)
6261 struct ofpbuf b
= ofpbuf_const_initializer(outer
, ntohs(outer
->length
));
6263 /* Skip past outer message. */
6264 enum ofpraw outer_raw
= ofpraw_pull_assert(&b
);
6265 ovs_assert(outer_raw
== OFPRAW_OFPT14_REQUESTFORWARD
);
6267 /* Validate inner message. */
6268 if (b
.size
< sizeof(struct ofp_header
)) {
6269 return OFPERR_OFPBFC_MSG_BAD_LEN
;
6271 const struct ofp_header
*inner
= b
.data
;
6272 unsigned int inner_len
= ntohs(inner
->length
);
6273 if (inner_len
< sizeof(struct ofp_header
) || inner_len
> b
.size
) {
6274 return OFPERR_OFPBFC_MSG_BAD_LEN
;
6276 if (inner
->version
!= outer
->version
) {
6277 return OFPERR_OFPBRC_BAD_VERSION
;
6280 /* Parse inner message. */
6282 enum ofperr error
= ofptype_decode(&type
, inner
);
6287 rf
->xid
= inner
->xid
;
6288 if (type
== OFPTYPE_GROUP_MOD
) {
6289 rf
->reason
= OFPRFR_GROUP_MOD
;
6290 rf
->group_mod
= xmalloc(sizeof *rf
->group_mod
);
6291 error
= ofputil_decode_group_mod(inner
, rf
->group_mod
);
6293 free(rf
->group_mod
);
6296 } else if (type
== OFPTYPE_METER_MOD
) {
6297 rf
->reason
= OFPRFR_METER_MOD
;
6298 rf
->meter_mod
= xmalloc(sizeof *rf
->meter_mod
);
6299 ofpbuf_init(&rf
->bands
, 64);
6300 error
= ofputil_decode_meter_mod(inner
, rf
->meter_mod
, &rf
->bands
);
6302 free(rf
->meter_mod
);
6303 ofpbuf_uninit(&rf
->bands
);
6307 return OFPERR_OFPBFC_MSG_UNSUP
;
6313 /* Frees the content of 'rf', which should have been initialized through a
6314 * successful call to ofputil_decode_requestforward(). */
6316 ofputil_destroy_requestforward(struct ofputil_requestforward
*rf
)
6322 switch (rf
->reason
) {
6323 case OFPRFR_GROUP_MOD
:
6324 ofputil_uninit_group_mod(rf
->group_mod
);
6325 free(rf
->group_mod
);
6328 case OFPRFR_METER_MOD
:
6329 ofpbuf_uninit(&rf
->bands
);
6330 free(rf
->meter_mod
);
6333 case OFPRFR_N_REASONS
:
6340 /* OpenFlow 1.0 and 1.1 don't distinguish between a field that cannot be
6341 * matched and a field that must be wildcarded. This function returns a bitmap
6342 * that contains both kinds of fields. */
6343 static struct mf_bitmap
6344 wild_or_nonmatchable_fields(const struct ofputil_table_features
*features
)
6346 struct mf_bitmap wc
= features
->match
;
6347 bitmap_not(wc
.bm
, MFF_N_IDS
);
6348 bitmap_or(wc
.bm
, features
->wildcard
.bm
, MFF_N_IDS
);
6352 struct ofp10_wc_map
{
6353 enum ofp10_flow_wildcards wc10
;
6354 enum mf_field_id mf
;
6357 static const struct ofp10_wc_map ofp10_wc_map
[] = {
6358 { OFPFW10_IN_PORT
, MFF_IN_PORT
},
6359 { OFPFW10_DL_VLAN
, MFF_VLAN_VID
},
6360 { OFPFW10_DL_SRC
, MFF_ETH_SRC
},
6361 { OFPFW10_DL_DST
, MFF_ETH_DST
},
6362 { OFPFW10_DL_TYPE
, MFF_ETH_TYPE
},
6363 { OFPFW10_NW_PROTO
, MFF_IP_PROTO
},
6364 { OFPFW10_TP_SRC
, MFF_TCP_SRC
},
6365 { OFPFW10_TP_DST
, MFF_TCP_DST
},
6366 { OFPFW10_NW_SRC_MASK
, MFF_IPV4_SRC
},
6367 { OFPFW10_NW_DST_MASK
, MFF_IPV4_DST
},
6368 { OFPFW10_DL_VLAN_PCP
, MFF_VLAN_PCP
},
6369 { OFPFW10_NW_TOS
, MFF_IP_DSCP
},
6373 mf_bitmap_to_of10(const struct mf_bitmap
*fields
)
6375 const struct ofp10_wc_map
*p
;
6378 for (p
= ofp10_wc_map
; p
< &ofp10_wc_map
[ARRAY_SIZE(ofp10_wc_map
)]; p
++) {
6379 if (bitmap_is_set(fields
->bm
, p
->mf
)) {
6386 static struct mf_bitmap
6387 mf_bitmap_from_of10(ovs_be32 wc10_
)
6389 struct mf_bitmap fields
= MF_BITMAP_INITIALIZER
;
6390 const struct ofp10_wc_map
*p
;
6391 uint32_t wc10
= ntohl(wc10_
);
6393 for (p
= ofp10_wc_map
; p
< &ofp10_wc_map
[ARRAY_SIZE(ofp10_wc_map
)]; p
++) {
6394 if (wc10
& p
->wc10
) {
6395 bitmap_set1(fields
.bm
, p
->mf
);
6402 ofputil_put_ofp10_table_stats(const struct ofputil_table_stats
*stats
,
6403 const struct ofputil_table_features
*features
,
6406 struct mf_bitmap wc
= wild_or_nonmatchable_fields(features
);
6407 struct ofp10_table_stats
*out
;
6409 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
6410 out
->table_id
= features
->table_id
;
6411 ovs_strlcpy_arrays(out
->name
, features
->name
);
6412 out
->wildcards
= mf_bitmap_to_of10(&wc
);
6413 out
->max_entries
= htonl(features
->max_entries
);
6414 out
->active_count
= htonl(stats
->active_count
);
6415 put_32aligned_be64(&out
->lookup_count
, htonll(stats
->lookup_count
));
6416 put_32aligned_be64(&out
->matched_count
, htonll(stats
->matched_count
));
6419 struct ofp11_wc_map
{
6420 enum ofp11_flow_match_fields wc11
;
6421 enum mf_field_id mf
;
6424 static const struct ofp11_wc_map ofp11_wc_map
[] = {
6425 { OFPFMF11_IN_PORT
, MFF_IN_PORT
},
6426 { OFPFMF11_DL_VLAN
, MFF_VLAN_VID
},
6427 { OFPFMF11_DL_VLAN_PCP
, MFF_VLAN_PCP
},
6428 { OFPFMF11_DL_TYPE
, MFF_ETH_TYPE
},
6429 { OFPFMF11_NW_TOS
, MFF_IP_DSCP
},
6430 { OFPFMF11_NW_PROTO
, MFF_IP_PROTO
},
6431 { OFPFMF11_TP_SRC
, MFF_TCP_SRC
},
6432 { OFPFMF11_TP_DST
, MFF_TCP_DST
},
6433 { OFPFMF11_MPLS_LABEL
, MFF_MPLS_LABEL
},
6434 { OFPFMF11_MPLS_TC
, MFF_MPLS_TC
},
6435 /* I don't know what OFPFMF11_TYPE means. */
6436 { OFPFMF11_DL_SRC
, MFF_ETH_SRC
},
6437 { OFPFMF11_DL_DST
, MFF_ETH_DST
},
6438 { OFPFMF11_NW_SRC
, MFF_IPV4_SRC
},
6439 { OFPFMF11_NW_DST
, MFF_IPV4_DST
},
6440 { OFPFMF11_METADATA
, MFF_METADATA
},
6444 mf_bitmap_to_of11(const struct mf_bitmap
*fields
)
6446 const struct ofp11_wc_map
*p
;
6449 for (p
= ofp11_wc_map
; p
< &ofp11_wc_map
[ARRAY_SIZE(ofp11_wc_map
)]; p
++) {
6450 if (bitmap_is_set(fields
->bm
, p
->mf
)) {
6457 static struct mf_bitmap
6458 mf_bitmap_from_of11(ovs_be32 wc11_
)
6460 struct mf_bitmap fields
= MF_BITMAP_INITIALIZER
;
6461 const struct ofp11_wc_map
*p
;
6462 uint32_t wc11
= ntohl(wc11_
);
6464 for (p
= ofp11_wc_map
; p
< &ofp11_wc_map
[ARRAY_SIZE(ofp11_wc_map
)]; p
++) {
6465 if (wc11
& p
->wc11
) {
6466 bitmap_set1(fields
.bm
, p
->mf
);
6473 ofputil_put_ofp11_table_stats(const struct ofputil_table_stats
*stats
,
6474 const struct ofputil_table_features
*features
,
6477 struct mf_bitmap wc
= wild_or_nonmatchable_fields(features
);
6478 struct ofp11_table_stats
*out
;
6480 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
6481 out
->table_id
= features
->table_id
;
6482 ovs_strlcpy_arrays(out
->name
, features
->name
);
6483 out
->wildcards
= mf_bitmap_to_of11(&wc
);
6484 out
->match
= mf_bitmap_to_of11(&features
->match
);
6485 out
->instructions
= ovsinst_bitmap_to_openflow(
6486 features
->nonmiss
.instructions
, OFP11_VERSION
);
6487 out
->write_actions
= ofpact_bitmap_to_openflow(
6488 features
->nonmiss
.write
.ofpacts
, OFP11_VERSION
);
6489 out
->apply_actions
= ofpact_bitmap_to_openflow(
6490 features
->nonmiss
.apply
.ofpacts
, OFP11_VERSION
);
6491 out
->config
= htonl(features
->miss_config
);
6492 out
->max_entries
= htonl(features
->max_entries
);
6493 out
->active_count
= htonl(stats
->active_count
);
6494 out
->lookup_count
= htonll(stats
->lookup_count
);
6495 out
->matched_count
= htonll(stats
->matched_count
);
6499 ofputil_put_ofp12_table_stats(const struct ofputil_table_stats
*stats
,
6500 const struct ofputil_table_features
*features
,
6503 struct ofp12_table_stats
*out
;
6505 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
6506 out
->table_id
= features
->table_id
;
6507 ovs_strlcpy_arrays(out
->name
, features
->name
);
6508 out
->match
= oxm_bitmap_from_mf_bitmap(&features
->match
, OFP12_VERSION
);
6509 out
->wildcards
= oxm_bitmap_from_mf_bitmap(&features
->wildcard
,
6511 out
->write_actions
= ofpact_bitmap_to_openflow(
6512 features
->nonmiss
.write
.ofpacts
, OFP12_VERSION
);
6513 out
->apply_actions
= ofpact_bitmap_to_openflow(
6514 features
->nonmiss
.apply
.ofpacts
, OFP12_VERSION
);
6515 out
->write_setfields
= oxm_bitmap_from_mf_bitmap(
6516 &features
->nonmiss
.write
.set_fields
, OFP12_VERSION
);
6517 out
->apply_setfields
= oxm_bitmap_from_mf_bitmap(
6518 &features
->nonmiss
.apply
.set_fields
, OFP12_VERSION
);
6519 out
->metadata_match
= features
->metadata_match
;
6520 out
->metadata_write
= features
->metadata_write
;
6521 out
->instructions
= ovsinst_bitmap_to_openflow(
6522 features
->nonmiss
.instructions
, OFP12_VERSION
);
6523 out
->config
= ofputil_encode_table_config(features
->miss_config
,
6524 OFPUTIL_TABLE_EVICTION_DEFAULT
,
6525 OFPUTIL_TABLE_VACANCY_DEFAULT
,
6527 out
->max_entries
= htonl(features
->max_entries
);
6528 out
->active_count
= htonl(stats
->active_count
);
6529 out
->lookup_count
= htonll(stats
->lookup_count
);
6530 out
->matched_count
= htonll(stats
->matched_count
);
6534 ofputil_put_ofp13_table_stats(const struct ofputil_table_stats
*stats
,
6537 struct ofp13_table_stats
*out
;
6539 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
6540 out
->table_id
= stats
->table_id
;
6541 out
->active_count
= htonl(stats
->active_count
);
6542 out
->lookup_count
= htonll(stats
->lookup_count
);
6543 out
->matched_count
= htonll(stats
->matched_count
);
6547 ofputil_encode_table_stats_reply(const struct ofp_header
*request
)
6549 return ofpraw_alloc_stats_reply(request
, 0);
6553 ofputil_append_table_stats_reply(struct ofpbuf
*reply
,
6554 const struct ofputil_table_stats
*stats
,
6555 const struct ofputil_table_features
*features
)
6557 struct ofp_header
*oh
= reply
->header
;
6559 ovs_assert(stats
->table_id
== features
->table_id
);
6561 switch ((enum ofp_version
) oh
->version
) {
6563 ofputil_put_ofp10_table_stats(stats
, features
, reply
);
6567 ofputil_put_ofp11_table_stats(stats
, features
, reply
);
6571 ofputil_put_ofp12_table_stats(stats
, features
, reply
);
6578 ofputil_put_ofp13_table_stats(stats
, reply
);
6587 ofputil_decode_ofp10_table_stats(struct ofpbuf
*msg
,
6588 struct ofputil_table_stats
*stats
,
6589 struct ofputil_table_features
*features
)
6591 struct ofp10_table_stats
*ots
;
6593 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
6595 return OFPERR_OFPBRC_BAD_LEN
;
6598 features
->table_id
= ots
->table_id
;
6599 ovs_strlcpy_arrays(features
->name
, ots
->name
);
6600 features
->max_entries
= ntohl(ots
->max_entries
);
6601 features
->match
= features
->wildcard
= mf_bitmap_from_of10(ots
->wildcards
);
6603 stats
->table_id
= ots
->table_id
;
6604 stats
->active_count
= ntohl(ots
->active_count
);
6605 stats
->lookup_count
= ntohll(get_32aligned_be64(&ots
->lookup_count
));
6606 stats
->matched_count
= ntohll(get_32aligned_be64(&ots
->matched_count
));
6612 ofputil_decode_ofp11_table_stats(struct ofpbuf
*msg
,
6613 struct ofputil_table_stats
*stats
,
6614 struct ofputil_table_features
*features
)
6616 struct ofp11_table_stats
*ots
;
6618 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
6620 return OFPERR_OFPBRC_BAD_LEN
;
6623 features
->table_id
= ots
->table_id
;
6624 ovs_strlcpy_arrays(features
->name
, ots
->name
);
6625 features
->max_entries
= ntohl(ots
->max_entries
);
6626 features
->nonmiss
.instructions
= ovsinst_bitmap_from_openflow(
6627 ots
->instructions
, OFP11_VERSION
);
6628 features
->nonmiss
.write
.ofpacts
= ofpact_bitmap_from_openflow(
6629 ots
->write_actions
, OFP11_VERSION
);
6630 features
->nonmiss
.apply
.ofpacts
= ofpact_bitmap_from_openflow(
6631 ots
->write_actions
, OFP11_VERSION
);
6632 features
->miss
= features
->nonmiss
;
6633 features
->miss_config
= ofputil_decode_table_miss(ots
->config
,
6635 features
->match
= mf_bitmap_from_of11(ots
->match
);
6636 features
->wildcard
= mf_bitmap_from_of11(ots
->wildcards
);
6637 bitmap_or(features
->match
.bm
, features
->wildcard
.bm
, MFF_N_IDS
);
6639 stats
->table_id
= ots
->table_id
;
6640 stats
->active_count
= ntohl(ots
->active_count
);
6641 stats
->lookup_count
= ntohll(ots
->lookup_count
);
6642 stats
->matched_count
= ntohll(ots
->matched_count
);
6648 ofputil_decode_ofp12_table_stats(struct ofpbuf
*msg
,
6649 struct ofputil_table_stats
*stats
,
6650 struct ofputil_table_features
*features
)
6652 struct ofp12_table_stats
*ots
;
6654 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
6656 return OFPERR_OFPBRC_BAD_LEN
;
6659 features
->table_id
= ots
->table_id
;
6660 ovs_strlcpy_arrays(features
->name
, ots
->name
);
6661 features
->metadata_match
= ots
->metadata_match
;
6662 features
->metadata_write
= ots
->metadata_write
;
6663 features
->miss_config
= ofputil_decode_table_miss(ots
->config
,
6665 features
->max_entries
= ntohl(ots
->max_entries
);
6667 features
->nonmiss
.instructions
= ovsinst_bitmap_from_openflow(
6668 ots
->instructions
, OFP12_VERSION
);
6669 features
->nonmiss
.write
.ofpacts
= ofpact_bitmap_from_openflow(
6670 ots
->write_actions
, OFP12_VERSION
);
6671 features
->nonmiss
.apply
.ofpacts
= ofpact_bitmap_from_openflow(
6672 ots
->apply_actions
, OFP12_VERSION
);
6673 features
->nonmiss
.write
.set_fields
= oxm_bitmap_to_mf_bitmap(
6674 ots
->write_setfields
, OFP12_VERSION
);
6675 features
->nonmiss
.apply
.set_fields
= oxm_bitmap_to_mf_bitmap(
6676 ots
->apply_setfields
, OFP12_VERSION
);
6677 features
->miss
= features
->nonmiss
;
6679 features
->match
= oxm_bitmap_to_mf_bitmap(ots
->match
, OFP12_VERSION
);
6680 features
->wildcard
= oxm_bitmap_to_mf_bitmap(ots
->wildcards
,
6682 bitmap_or(features
->match
.bm
, features
->wildcard
.bm
, MFF_N_IDS
);
6684 stats
->table_id
= ots
->table_id
;
6685 stats
->active_count
= ntohl(ots
->active_count
);
6686 stats
->lookup_count
= ntohll(ots
->lookup_count
);
6687 stats
->matched_count
= ntohll(ots
->matched_count
);
6693 ofputil_decode_ofp13_table_stats(struct ofpbuf
*msg
,
6694 struct ofputil_table_stats
*stats
,
6695 struct ofputil_table_features
*features
)
6697 struct ofp13_table_stats
*ots
;
6699 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
6701 return OFPERR_OFPBRC_BAD_LEN
;
6704 features
->table_id
= ots
->table_id
;
6706 stats
->table_id
= ots
->table_id
;
6707 stats
->active_count
= ntohl(ots
->active_count
);
6708 stats
->lookup_count
= ntohll(ots
->lookup_count
);
6709 stats
->matched_count
= ntohll(ots
->matched_count
);
6715 ofputil_decode_table_stats_reply(struct ofpbuf
*msg
,
6716 struct ofputil_table_stats
*stats
,
6717 struct ofputil_table_features
*features
)
6719 const struct ofp_header
*oh
;
6722 ofpraw_pull_assert(msg
);
6730 memset(stats
, 0, sizeof *stats
);
6731 memset(features
, 0, sizeof *features
);
6732 features
->supports_eviction
= -1;
6733 features
->supports_vacancy_events
= -1;
6735 switch ((enum ofp_version
) oh
->version
) {
6737 return ofputil_decode_ofp10_table_stats(msg
, stats
, features
);
6740 return ofputil_decode_ofp11_table_stats(msg
, stats
, features
);
6743 return ofputil_decode_ofp12_table_stats(msg
, stats
, features
);
6749 return ofputil_decode_ofp13_table_stats(msg
, stats
, features
);
6756 /* ofputil_flow_monitor_request */
6758 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
6759 * ofputil_flow_monitor_request in 'rq'.
6761 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
6762 * message. Calling this function multiple times for a single 'msg' iterates
6763 * through the requests. The caller must initially leave 'msg''s layer
6764 * pointers null and not modify them between calls.
6766 * Returns 0 if successful, EOF if no requests were left in this 'msg',
6767 * otherwise an OFPERR_* value. */
6769 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request
*rq
,
6772 struct nx_flow_monitor_request
*nfmr
;
6776 ofpraw_pull_assert(msg
);
6783 nfmr
= ofpbuf_try_pull(msg
, sizeof *nfmr
);
6785 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW_MONITOR request has %"PRIu32
" "
6786 "leftover bytes at end", msg
->size
);
6787 return OFPERR_OFPBRC_BAD_LEN
;
6790 flags
= ntohs(nfmr
->flags
);
6791 if (!(flags
& (NXFMF_ADD
| NXFMF_DELETE
| NXFMF_MODIFY
))
6792 || flags
& ~(NXFMF_INITIAL
| NXFMF_ADD
| NXFMF_DELETE
6793 | NXFMF_MODIFY
| NXFMF_ACTIONS
| NXFMF_OWN
)) {
6794 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW_MONITOR has bad flags %#"PRIx16
,
6796 return OFPERR_OFPMOFC_BAD_FLAGS
;
6799 if (!is_all_zeros(nfmr
->zeros
, sizeof nfmr
->zeros
)) {
6800 return OFPERR_NXBRC_MUST_BE_ZERO
;
6803 rq
->id
= ntohl(nfmr
->id
);
6805 rq
->out_port
= u16_to_ofp(ntohs(nfmr
->out_port
));
6806 rq
->table_id
= nfmr
->table_id
;
6808 return nx_pull_match(msg
, ntohs(nfmr
->match_len
), &rq
->match
, NULL
,
6813 ofputil_append_flow_monitor_request(
6814 const struct ofputil_flow_monitor_request
*rq
, struct ofpbuf
*msg
)
6816 struct nx_flow_monitor_request
*nfmr
;
6821 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST
, OFP10_VERSION
, msg
);
6824 start_ofs
= msg
->size
;
6825 ofpbuf_put_zeros(msg
, sizeof *nfmr
);
6826 match_len
= nx_put_match(msg
, &rq
->match
, htonll(0), htonll(0));
6828 nfmr
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *nfmr
);
6829 nfmr
->id
= htonl(rq
->id
);
6830 nfmr
->flags
= htons(rq
->flags
);
6831 nfmr
->out_port
= htons(ofp_to_u16(rq
->out_port
));
6832 nfmr
->match_len
= htons(match_len
);
6833 nfmr
->table_id
= rq
->table_id
;
6836 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
6837 * into an abstract ofputil_flow_update in 'update'. The caller must have
6838 * initialized update->match to point to space allocated for a match.
6840 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
6841 * actions (except for NXFME_ABBREV, which never includes actions). The caller
6842 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
6843 * will point into the 'ofpacts' buffer.
6845 * Multiple flow updates can be packed into a single OpenFlow message. Calling
6846 * this function multiple times for a single 'msg' iterates through the
6847 * updates. The caller must initially leave 'msg''s layer pointers null and
6848 * not modify them between calls.
6850 * Returns 0 if successful, EOF if no updates were left in this 'msg',
6851 * otherwise an OFPERR_* value. */
6853 ofputil_decode_flow_update(struct ofputil_flow_update
*update
,
6854 struct ofpbuf
*msg
, struct ofpbuf
*ofpacts
)
6856 struct nx_flow_update_header
*nfuh
;
6857 unsigned int length
;
6858 struct ofp_header
*oh
;
6861 ofpraw_pull_assert(msg
);
6864 ofpbuf_clear(ofpacts
);
6869 if (msg
->size
< sizeof(struct nx_flow_update_header
)) {
6876 update
->event
= ntohs(nfuh
->event
);
6877 length
= ntohs(nfuh
->length
);
6878 if (length
> msg
->size
|| length
% 8) {
6882 if (update
->event
== NXFME_ABBREV
) {
6883 struct nx_flow_update_abbrev
*nfua
;
6885 if (length
!= sizeof *nfua
) {
6889 nfua
= ofpbuf_pull(msg
, sizeof *nfua
);
6890 update
->xid
= nfua
->xid
;
6892 } else if (update
->event
== NXFME_ADDED
6893 || update
->event
== NXFME_DELETED
6894 || update
->event
== NXFME_MODIFIED
) {
6895 struct nx_flow_update_full
*nfuf
;
6896 unsigned int actions_len
;
6897 unsigned int match_len
;
6900 if (length
< sizeof *nfuf
) {
6904 nfuf
= ofpbuf_pull(msg
, sizeof *nfuf
);
6905 match_len
= ntohs(nfuf
->match_len
);
6906 if (sizeof *nfuf
+ match_len
> length
) {
6910 update
->reason
= ntohs(nfuf
->reason
);
6911 update
->idle_timeout
= ntohs(nfuf
->idle_timeout
);
6912 update
->hard_timeout
= ntohs(nfuf
->hard_timeout
);
6913 update
->table_id
= nfuf
->table_id
;
6914 update
->cookie
= nfuf
->cookie
;
6915 update
->priority
= ntohs(nfuf
->priority
);
6917 error
= nx_pull_match(msg
, match_len
, &update
->match
, NULL
, NULL
, NULL
,
6923 actions_len
= length
- sizeof *nfuf
- ROUND_UP(match_len
, 8);
6924 error
= ofpacts_pull_openflow_actions(msg
, actions_len
, oh
->version
,
6925 NULL
, NULL
, ofpacts
);
6930 update
->ofpacts
= ofpacts
->data
;
6931 update
->ofpacts_len
= ofpacts
->size
;
6934 VLOG_WARN_RL(&bad_ofmsg_rl
,
6935 "NXST_FLOW_MONITOR reply has bad event %"PRIu16
,
6936 ntohs(nfuh
->event
));
6937 return OFPERR_NXBRC_FM_BAD_EVENT
;
6941 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW_MONITOR reply has %"PRIu32
" "
6942 "leftover bytes at end", msg
->size
);
6943 return OFPERR_OFPBRC_BAD_LEN
;
6947 ofputil_decode_flow_monitor_cancel(const struct ofp_header
*oh
)
6949 const struct nx_flow_monitor_cancel
*cancel
= ofpmsg_body(oh
);
6951 return ntohl(cancel
->id
);
6955 ofputil_encode_flow_monitor_cancel(uint32_t id
)
6957 struct nx_flow_monitor_cancel
*nfmc
;
6960 msg
= ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL
, OFP10_VERSION
, 0);
6961 nfmc
= ofpbuf_put_uninit(msg
, sizeof *nfmc
);
6962 nfmc
->id
= htonl(id
);
6967 ofputil_start_flow_update(struct ovs_list
*replies
)
6971 msg
= ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY
, OFP10_VERSION
,
6974 ovs_list_init(replies
);
6975 ovs_list_push_back(replies
, &msg
->list_node
);
6979 ofputil_append_flow_update(const struct ofputil_flow_update
*update
,
6980 struct ovs_list
*replies
,
6981 const struct tun_table
*tun_table
)
6983 struct ofputil_flow_update
*update_
=
6984 CONST_CAST(struct ofputil_flow_update
*, update
);
6985 const struct tun_table
*orig_tun_table
;
6986 enum ofp_version version
= ofpmp_version(replies
);
6987 struct nx_flow_update_header
*nfuh
;
6991 orig_tun_table
= update
->match
.flow
.tunnel
.metadata
.tab
;
6992 update_
->match
.flow
.tunnel
.metadata
.tab
= tun_table
;
6994 msg
= ofpbuf_from_list(ovs_list_back(replies
));
6995 start_ofs
= msg
->size
;
6997 if (update
->event
== NXFME_ABBREV
) {
6998 struct nx_flow_update_abbrev
*nfua
;
7000 nfua
= ofpbuf_put_zeros(msg
, sizeof *nfua
);
7001 nfua
->xid
= update
->xid
;
7003 struct nx_flow_update_full
*nfuf
;
7006 ofpbuf_put_zeros(msg
, sizeof *nfuf
);
7007 match_len
= nx_put_match(msg
, &update
->match
, htonll(0), htonll(0));
7008 ofpacts_put_openflow_actions(update
->ofpacts
, update
->ofpacts_len
, msg
,
7010 nfuf
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *nfuf
);
7011 nfuf
->reason
= htons(update
->reason
);
7012 nfuf
->priority
= htons(update
->priority
);
7013 nfuf
->idle_timeout
= htons(update
->idle_timeout
);
7014 nfuf
->hard_timeout
= htons(update
->hard_timeout
);
7015 nfuf
->match_len
= htons(match_len
);
7016 nfuf
->table_id
= update
->table_id
;
7017 nfuf
->cookie
= update
->cookie
;
7020 nfuh
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *nfuh
);
7021 nfuh
->length
= htons(msg
->size
- start_ofs
);
7022 nfuh
->event
= htons(update
->event
);
7024 ofpmp_postappend(replies
, start_ofs
);
7025 update_
->match
.flow
.tunnel
.metadata
.tab
= orig_tun_table
;
7029 ofputil_encode_packet_out(const struct ofputil_packet_out
*po
,
7030 enum ofputil_protocol protocol
)
7032 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
7036 size
= po
->ofpacts_len
;
7037 if (po
->buffer_id
== UINT32_MAX
) {
7038 size
+= po
->packet_len
;
7041 switch (ofp_version
) {
7042 case OFP10_VERSION
: {
7043 struct ofp10_packet_out
*opo
;
7046 msg
= ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT
, OFP10_VERSION
, size
);
7047 ofpbuf_put_zeros(msg
, sizeof *opo
);
7048 actions_ofs
= msg
->size
;
7049 ofpacts_put_openflow_actions(po
->ofpacts
, po
->ofpacts_len
, msg
,
7053 opo
->buffer_id
= htonl(po
->buffer_id
);
7054 opo
->in_port
= htons(ofp_to_u16(po
->in_port
));
7055 opo
->actions_len
= htons(msg
->size
- actions_ofs
);
7064 case OFP16_VERSION
: {
7065 struct ofp11_packet_out
*opo
;
7068 msg
= ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT
, ofp_version
, size
);
7069 ofpbuf_put_zeros(msg
, sizeof *opo
);
7070 len
= ofpacts_put_openflow_actions(po
->ofpacts
, po
->ofpacts_len
, msg
,
7073 opo
->buffer_id
= htonl(po
->buffer_id
);
7074 opo
->in_port
= ofputil_port_to_ofp11(po
->in_port
);
7075 opo
->actions_len
= htons(len
);
7083 if (po
->buffer_id
== UINT32_MAX
) {
7084 ofpbuf_put(msg
, po
->packet
, po
->packet_len
);
7087 ofpmsg_update_length(msg
);
7092 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
7094 make_echo_request(enum ofp_version ofp_version
)
7096 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST
, ofp_version
,
7100 /* Creates and returns an OFPT_ECHO_REPLY message matching the
7101 * OFPT_ECHO_REQUEST message in 'rq'. */
7103 make_echo_reply(const struct ofp_header
*rq
)
7105 struct ofpbuf rq_buf
= ofpbuf_const_initializer(rq
, ntohs(rq
->length
));
7106 ofpraw_pull_assert(&rq_buf
);
7108 struct ofpbuf
*reply
= ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY
,
7110 ofpbuf_put(reply
, rq_buf
.data
, rq_buf
.size
);
7115 ofputil_encode_barrier_request(enum ofp_version ofp_version
)
7119 switch (ofp_version
) {
7126 type
= OFPRAW_OFPT11_BARRIER_REQUEST
;
7130 type
= OFPRAW_OFPT10_BARRIER_REQUEST
;
7137 return ofpraw_alloc(type
, ofp_version
, 0);
7141 ofputil_frag_handling_to_string(enum ofputil_frag_handling frag
)
7144 case OFPUTIL_FRAG_NORMAL
: return "normal";
7145 case OFPUTIL_FRAG_DROP
: return "drop";
7146 case OFPUTIL_FRAG_REASM
: return "reassemble";
7147 case OFPUTIL_FRAG_NX_MATCH
: return "nx-match";
7154 ofputil_frag_handling_from_string(const char *s
,
7155 enum ofputil_frag_handling
*frag
)
7157 if (!strcasecmp(s
, "normal")) {
7158 *frag
= OFPUTIL_FRAG_NORMAL
;
7159 } else if (!strcasecmp(s
, "drop")) {
7160 *frag
= OFPUTIL_FRAG_DROP
;
7161 } else if (!strcasecmp(s
, "reassemble")) {
7162 *frag
= OFPUTIL_FRAG_REASM
;
7163 } else if (!strcasecmp(s
, "nx-match")) {
7164 *frag
= OFPUTIL_FRAG_NX_MATCH
;
7171 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
7172 * port number and stores the latter in '*ofp10_port', for the purpose of
7173 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
7174 * otherwise an OFPERR_* number. On error, stores OFPP_NONE in '*ofp10_port'.
7176 * See the definition of OFP11_MAX for an explanation of the mapping. */
7178 ofputil_port_from_ofp11(ovs_be32 ofp11_port
, ofp_port_t
*ofp10_port
)
7180 uint32_t ofp11_port_h
= ntohl(ofp11_port
);
7182 if (ofp11_port_h
< ofp_to_u16(OFPP_MAX
)) {
7183 *ofp10_port
= u16_to_ofp(ofp11_port_h
);
7185 } else if (ofp11_port_h
>= ofp11_to_u32(OFPP11_MAX
)) {
7186 *ofp10_port
= u16_to_ofp(ofp11_port_h
- OFPP11_OFFSET
);
7189 *ofp10_port
= OFPP_NONE
;
7190 VLOG_WARN_RL(&bad_ofmsg_rl
, "port %"PRIu32
" is outside the supported "
7191 "range 0 through %d or 0x%"PRIx32
" through 0x%"PRIx32
,
7192 ofp11_port_h
, ofp_to_u16(OFPP_MAX
) - 1,
7193 ofp11_to_u32(OFPP11_MAX
), UINT32_MAX
);
7194 return OFPERR_OFPBAC_BAD_OUT_PORT
;
7198 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
7199 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
7201 * See the definition of OFP11_MAX for an explanation of the mapping. */
7203 ofputil_port_to_ofp11(ofp_port_t ofp10_port
)
7205 return htonl(ofp_to_u16(ofp10_port
) < ofp_to_u16(OFPP_MAX
)
7206 ? ofp_to_u16(ofp10_port
)
7207 : ofp_to_u16(ofp10_port
) + OFPP11_OFFSET
);
7210 #define OFPUTIL_NAMED_PORTS \
7211 OFPUTIL_NAMED_PORT(IN_PORT) \
7212 OFPUTIL_NAMED_PORT(TABLE) \
7213 OFPUTIL_NAMED_PORT(NORMAL) \
7214 OFPUTIL_NAMED_PORT(FLOOD) \
7215 OFPUTIL_NAMED_PORT(ALL) \
7216 OFPUTIL_NAMED_PORT(CONTROLLER) \
7217 OFPUTIL_NAMED_PORT(LOCAL) \
7218 OFPUTIL_NAMED_PORT(ANY) \
7219 OFPUTIL_NAMED_PORT(UNSET)
7221 /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */
7222 #define OFPUTIL_NAMED_PORTS_WITH_NONE \
7223 OFPUTIL_NAMED_PORTS \
7224 OFPUTIL_NAMED_PORT(NONE)
7226 /* Stores the port number represented by 's' into '*portp'. 's' may be an
7227 * integer or, for reserved ports, the standard OpenFlow name for the port
7230 * Returns true if successful, false if 's' is not a valid OpenFlow port number
7231 * or name. The caller should issue an error message in this case, because
7232 * this function usually does not. (This gives the caller an opportunity to
7233 * look up the port name another way, e.g. by contacting the switch and listing
7234 * the names of all its ports).
7236 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
7237 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
7238 * range as described in include/openflow/openflow-1.1.h. */
7240 ofputil_port_from_string(const char *s
, ofp_port_t
*portp
)
7242 unsigned int port32
; /* int is at least 32 bits wide. */
7245 VLOG_WARN("Negative value %s is not a valid port number.", s
);
7249 if (str_to_uint(s
, 10, &port32
)) {
7250 if (port32
< ofp_to_u16(OFPP_MAX
)) {
7252 } else if (port32
< ofp_to_u16(OFPP_FIRST_RESV
)) {
7253 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
7254 "be translated to %u when talking to an OF1.1 or "
7255 "later controller", port32
, port32
+ OFPP11_OFFSET
);
7256 } else if (port32
<= ofp_to_u16(OFPP_LAST_RESV
)) {
7257 char name
[OFP10_MAX_PORT_NAME_LEN
];
7259 ofputil_port_to_string(u16_to_ofp(port32
), name
, sizeof name
);
7260 VLOG_WARN_ONCE("referring to port %s as %"PRIu32
" is deprecated "
7261 "for compatibility with OpenFlow 1.1 and later",
7263 } else if (port32
< ofp11_to_u32(OFPP11_MAX
)) {
7264 VLOG_WARN("port %u is outside the supported range 0 through "
7265 "%x or 0x%x through 0x%"PRIx32
, port32
,
7266 UINT16_MAX
, ofp11_to_u32(OFPP11_MAX
), UINT32_MAX
);
7269 port32
-= OFPP11_OFFSET
;
7272 *portp
= u16_to_ofp(port32
);
7279 static const struct pair pairs
[] = {
7280 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
7281 OFPUTIL_NAMED_PORTS_WITH_NONE
7282 #undef OFPUTIL_NAMED_PORT
7284 const struct pair
*p
;
7286 for (p
= pairs
; p
< &pairs
[ARRAY_SIZE(pairs
)]; p
++) {
7287 if (!strcasecmp(s
, p
->name
)) {
7296 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
7297 * Most ports' string representation is just the port number, but for special
7298 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
7300 ofputil_format_port(ofp_port_t port
, struct ds
*s
)
7302 char name
[OFP10_MAX_PORT_NAME_LEN
];
7304 ofputil_port_to_string(port
, name
, sizeof name
);
7305 ds_put_cstr(s
, name
);
7308 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
7309 * representation of OpenFlow port number 'port'. Most ports are represented
7310 * as just the port number, but special ports, e.g. OFPP_LOCAL, are represented
7311 * by name, e.g. "LOCAL". */
7313 ofputil_port_to_string(ofp_port_t port
,
7314 char namebuf
[OFP10_MAX_PORT_NAME_LEN
], size_t bufsize
)
7317 #define OFPUTIL_NAMED_PORT(NAME) \
7319 ovs_strlcpy(namebuf, #NAME, bufsize); \
7322 #undef OFPUTIL_NAMED_PORT
7325 snprintf(namebuf
, bufsize
, "%"PRIu32
, port
);
7330 /* Stores the group id represented by 's' into '*group_idp'. 's' may be an
7331 * integer or, for reserved group IDs, the standard OpenFlow name for the group
7332 * (either "ANY" or "ALL").
7334 * Returns true if successful, false if 's' is not a valid OpenFlow group ID or
7337 ofputil_group_from_string(const char *s
, uint32_t *group_idp
)
7339 if (!strcasecmp(s
, "any")) {
7340 *group_idp
= OFPG_ANY
;
7341 } else if (!strcasecmp(s
, "all")) {
7342 *group_idp
= OFPG_ALL
;
7343 } else if (!str_to_uint(s
, 10, group_idp
)) {
7344 VLOG_WARN("%s is not a valid group ID. (Valid group IDs are "
7345 "32-bit nonnegative integers or the keywords ANY or "
7353 /* Appends to 's' a string representation of the OpenFlow group ID 'group_id'.
7354 * Most groups' string representation is just the number, but for special
7355 * groups, e.g. OFPG_ALL, it is the name, e.g. "ALL". */
7357 ofputil_format_group(uint32_t group_id
, struct ds
*s
)
7359 char name
[MAX_GROUP_NAME_LEN
];
7361 ofputil_group_to_string(group_id
, name
, sizeof name
);
7362 ds_put_cstr(s
, name
);
7366 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
7367 * representation of OpenFlow group ID 'group_id'. Most group are represented
7368 * as just their number, but special groups, e.g. OFPG_ALL, are represented
7369 * by name, e.g. "ALL". */
7371 ofputil_group_to_string(uint32_t group_id
,
7372 char namebuf
[MAX_GROUP_NAME_LEN
+ 1], size_t bufsize
)
7376 ovs_strlcpy(namebuf
, "ALL", bufsize
);
7380 ovs_strlcpy(namebuf
, "ANY", bufsize
);
7384 snprintf(namebuf
, bufsize
, "%"PRIu32
, group_id
);
7389 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
7390 * 'ofp_version', tries to pull the first element from the array. If
7391 * successful, initializes '*pp' with an abstract representation of the
7392 * port and returns 0. If no ports remain to be decoded, returns EOF.
7393 * On an error, returns a positive OFPERR_* value. */
7395 ofputil_pull_phy_port(enum ofp_version ofp_version
, struct ofpbuf
*b
,
7396 struct ofputil_phy_port
*pp
)
7398 memset(pp
, 0, sizeof *pp
);
7400 switch (ofp_version
) {
7401 case OFP10_VERSION
: {
7402 const struct ofp10_phy_port
*opp
= ofpbuf_try_pull(b
, sizeof *opp
);
7403 return opp
? ofputil_decode_ofp10_phy_port(pp
, opp
) : EOF
;
7407 case OFP13_VERSION
: {
7408 const struct ofp11_port
*op
= ofpbuf_try_pull(b
, sizeof *op
);
7409 return op
? ofputil_decode_ofp11_port(pp
, op
) : EOF
;
7413 return b
->size
? ofputil_pull_ofp14_port(pp
, b
) : EOF
;
7415 return b
->size
? ofputil_pull_ofp16_port(pp
, b
) : EOF
;
7422 ofputil_normalize_match__(struct match
*match
, bool may_log
)
7425 MAY_NW_ADDR
= 1 << 0, /* nw_src, nw_dst */
7426 MAY_TP_ADDR
= 1 << 1, /* tp_src, tp_dst */
7427 MAY_NW_PROTO
= 1 << 2, /* nw_proto */
7428 MAY_IPVx
= 1 << 3, /* tos, frag, ttl */
7429 MAY_ARP_SHA
= 1 << 4, /* arp_sha */
7430 MAY_ARP_THA
= 1 << 5, /* arp_tha */
7431 MAY_IPV6
= 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
7432 MAY_ND_TARGET
= 1 << 7, /* nd_target */
7433 MAY_MPLS
= 1 << 8, /* mpls label and tc */
7436 struct flow_wildcards wc
;
7438 /* Figure out what fields may be matched. */
7439 if (match
->flow
.dl_type
== htons(ETH_TYPE_IP
)) {
7440 may_match
= MAY_NW_PROTO
| MAY_IPVx
| MAY_NW_ADDR
;
7441 if (match
->flow
.nw_proto
== IPPROTO_TCP
||
7442 match
->flow
.nw_proto
== IPPROTO_UDP
||
7443 match
->flow
.nw_proto
== IPPROTO_SCTP
||
7444 match
->flow
.nw_proto
== IPPROTO_ICMP
) {
7445 may_match
|= MAY_TP_ADDR
;
7447 } else if (match
->flow
.dl_type
== htons(ETH_TYPE_IPV6
)) {
7448 may_match
= MAY_NW_PROTO
| MAY_IPVx
| MAY_IPV6
;
7449 if (match
->flow
.nw_proto
== IPPROTO_TCP
||
7450 match
->flow
.nw_proto
== IPPROTO_UDP
||
7451 match
->flow
.nw_proto
== IPPROTO_SCTP
) {
7452 may_match
|= MAY_TP_ADDR
;
7453 } else if (match
->flow
.nw_proto
== IPPROTO_ICMPV6
) {
7454 may_match
|= MAY_TP_ADDR
;
7455 if (match
->flow
.tp_src
== htons(ND_NEIGHBOR_SOLICIT
)) {
7456 may_match
|= MAY_ND_TARGET
| MAY_ARP_SHA
;
7457 } else if (match
->flow
.tp_src
== htons(ND_NEIGHBOR_ADVERT
)) {
7458 may_match
|= MAY_ND_TARGET
| MAY_ARP_THA
;
7461 } else if (match
->flow
.dl_type
== htons(ETH_TYPE_ARP
) ||
7462 match
->flow
.dl_type
== htons(ETH_TYPE_RARP
)) {
7463 may_match
= MAY_NW_PROTO
| MAY_NW_ADDR
| MAY_ARP_SHA
| MAY_ARP_THA
;
7464 } else if (eth_type_mpls(match
->flow
.dl_type
)) {
7465 may_match
= MAY_MPLS
;
7470 /* Clear the fields that may not be matched. */
7472 if (!(may_match
& MAY_NW_ADDR
)) {
7473 wc
.masks
.nw_src
= wc
.masks
.nw_dst
= htonl(0);
7475 if (!(may_match
& MAY_TP_ADDR
)) {
7476 wc
.masks
.tp_src
= wc
.masks
.tp_dst
= htons(0);
7478 if (!(may_match
& MAY_NW_PROTO
)) {
7479 wc
.masks
.nw_proto
= 0;
7481 if (!(may_match
& MAY_IPVx
)) {
7482 wc
.masks
.nw_tos
= 0;
7483 wc
.masks
.nw_ttl
= 0;
7485 if (!(may_match
& MAY_ARP_SHA
)) {
7486 WC_UNMASK_FIELD(&wc
, arp_sha
);
7488 if (!(may_match
& MAY_ARP_THA
)) {
7489 WC_UNMASK_FIELD(&wc
, arp_tha
);
7491 if (!(may_match
& MAY_IPV6
)) {
7492 wc
.masks
.ipv6_src
= wc
.masks
.ipv6_dst
= in6addr_any
;
7493 wc
.masks
.ipv6_label
= htonl(0);
7495 if (!(may_match
& MAY_ND_TARGET
)) {
7496 wc
.masks
.nd_target
= in6addr_any
;
7498 if (!(may_match
& MAY_MPLS
)) {
7499 memset(wc
.masks
.mpls_lse
, 0, sizeof wc
.masks
.mpls_lse
);
7502 /* Log any changes. */
7503 if (!flow_wildcards_equal(&wc
, &match
->wc
)) {
7504 bool log
= may_log
&& !VLOG_DROP_INFO(&bad_ofmsg_rl
);
7505 char *pre
= log
? match_to_string(match
, OFP_DEFAULT_PRIORITY
) : NULL
;
7508 match_zero_wildcarded_fields(match
);
7511 char *post
= match_to_string(match
, OFP_DEFAULT_PRIORITY
);
7512 VLOG_INFO("normalization changed ofp_match, details:");
7513 VLOG_INFO(" pre: %s", pre
);
7514 VLOG_INFO("post: %s", post
);
7521 /* "Normalizes" the wildcards in 'match'. That means:
7523 * 1. If the type of level N is known, then only the valid fields for that
7524 * level may be specified. For example, ARP does not have a TOS field,
7525 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
7526 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
7527 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
7530 * 2. If the type of level N is not known (or not understood by Open
7531 * vSwitch), then no fields at all for that level may be specified. For
7532 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
7533 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
7536 * If this function changes 'match', it logs a rate-limited informational
7539 ofputil_normalize_match(struct match
*match
)
7541 ofputil_normalize_match__(match
, true);
7544 /* Same as ofputil_normalize_match() without the logging. Thus, this function
7545 * is suitable for a program's internal use, whereas ofputil_normalize_match()
7546 * sense for use on flows received from elsewhere (so that a bug in the program
7547 * that sent them can be reported and corrected). */
7549 ofputil_normalize_match_quiet(struct match
*match
)
7551 ofputil_normalize_match__(match
, false);
7555 parse_value(const char *s
, const char *delimiters
)
7559 /* Iterate until we reach a delimiter.
7561 * strchr(s, '\0') returns s+strlen(s), so this test handles the null
7562 * terminator at the end of 's'. */
7563 while (!strchr(delimiters
, s
[n
])) {
7578 } while (level
> 0);
7586 /* Parses a key or a key-value pair from '*stringp'.
7588 * On success: Stores the key into '*keyp'. Stores the value, if present, into
7589 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
7590 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
7591 * are substrings of '*stringp' created by replacing some of its bytes by null
7592 * terminators. Returns true.
7594 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
7595 * NULL and returns false. */
7597 ofputil_parse_key_value(char **stringp
, char **keyp
, char **valuep
)
7599 /* Skip white space and delimiters. If that brings us to the end of the
7600 * input string, we are done and there are no more key-value pairs. */
7601 *stringp
+= strspn(*stringp
, ", \t\r\n");
7602 if (**stringp
== '\0') {
7603 *keyp
= *valuep
= NULL
;
7607 /* Extract the key and the delimiter that ends the key-value pair or begins
7608 * the value. Advance the input position past the key and delimiter. */
7609 char *key
= *stringp
;
7610 size_t key_len
= strcspn(key
, ":=(, \t\r\n");
7611 char key_delim
= key
[key_len
];
7612 key
[key_len
] = '\0';
7613 *stringp
+= key_len
+ (key_delim
!= '\0');
7615 /* Figure out what delimiter ends the value:
7617 * - If key_delim is ":" or "=", the value extends until white space
7620 * - If key_delim is "(", the value extends until ")".
7622 * If there is no value, we are done. */
7623 const char *value_delims
;
7624 if (key_delim
== ':' || key_delim
== '=') {
7625 value_delims
= ", \t\r\n";
7626 } else if (key_delim
== '(') {
7630 *valuep
= key
+ key_len
; /* Empty string. */
7634 /* Extract the value. Advance the input position past the value and
7636 char *value
= *stringp
;
7637 size_t value_len
= parse_value(value
, value_delims
);
7638 char value_delim
= value
[value_len
];
7639 value
[value_len
] = '\0';
7640 *stringp
+= value_len
+ (value_delim
!= '\0');
7647 /* Encode a dump ports request for 'port', the encoded message
7648 * will be for OpenFlow version 'ofp_version'. Returns message
7649 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
7651 ofputil_encode_dump_ports_request(enum ofp_version ofp_version
, ofp_port_t port
)
7653 struct ofpbuf
*request
;
7655 switch (ofp_version
) {
7656 case OFP10_VERSION
: {
7657 struct ofp10_port_stats_request
*req
;
7658 request
= ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST
, ofp_version
, 0);
7659 req
= ofpbuf_put_zeros(request
, sizeof *req
);
7660 req
->port_no
= htons(ofp_to_u16(port
));
7668 case OFP16_VERSION
: {
7669 struct ofp11_port_stats_request
*req
;
7670 request
= ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST
, ofp_version
, 0);
7671 req
= ofpbuf_put_zeros(request
, sizeof *req
);
7672 req
->port_no
= ofputil_port_to_ofp11(port
);
7683 ofputil_port_stats_to_ofp10(const struct ofputil_port_stats
*ops
,
7684 struct ofp10_port_stats
*ps10
)
7686 ps10
->port_no
= htons(ofp_to_u16(ops
->port_no
));
7687 memset(ps10
->pad
, 0, sizeof ps10
->pad
);
7688 put_32aligned_be64(&ps10
->rx_packets
, htonll(ops
->stats
.rx_packets
));
7689 put_32aligned_be64(&ps10
->tx_packets
, htonll(ops
->stats
.tx_packets
));
7690 put_32aligned_be64(&ps10
->rx_bytes
, htonll(ops
->stats
.rx_bytes
));
7691 put_32aligned_be64(&ps10
->tx_bytes
, htonll(ops
->stats
.tx_bytes
));
7692 put_32aligned_be64(&ps10
->rx_dropped
, htonll(ops
->stats
.rx_dropped
));
7693 put_32aligned_be64(&ps10
->tx_dropped
, htonll(ops
->stats
.tx_dropped
));
7694 put_32aligned_be64(&ps10
->rx_errors
, htonll(ops
->stats
.rx_errors
));
7695 put_32aligned_be64(&ps10
->tx_errors
, htonll(ops
->stats
.tx_errors
));
7696 put_32aligned_be64(&ps10
->rx_frame_err
, htonll(ops
->stats
.rx_frame_errors
));
7697 put_32aligned_be64(&ps10
->rx_over_err
, htonll(ops
->stats
.rx_over_errors
));
7698 put_32aligned_be64(&ps10
->rx_crc_err
, htonll(ops
->stats
.rx_crc_errors
));
7699 put_32aligned_be64(&ps10
->collisions
, htonll(ops
->stats
.collisions
));
7703 ofputil_port_stats_to_ofp11(const struct ofputil_port_stats
*ops
,
7704 struct ofp11_port_stats
*ps11
)
7706 ps11
->port_no
= ofputil_port_to_ofp11(ops
->port_no
);
7707 memset(ps11
->pad
, 0, sizeof ps11
->pad
);
7708 ps11
->rx_packets
= htonll(ops
->stats
.rx_packets
);
7709 ps11
->tx_packets
= htonll(ops
->stats
.tx_packets
);
7710 ps11
->rx_bytes
= htonll(ops
->stats
.rx_bytes
);
7711 ps11
->tx_bytes
= htonll(ops
->stats
.tx_bytes
);
7712 ps11
->rx_dropped
= htonll(ops
->stats
.rx_dropped
);
7713 ps11
->tx_dropped
= htonll(ops
->stats
.tx_dropped
);
7714 ps11
->rx_errors
= htonll(ops
->stats
.rx_errors
);
7715 ps11
->tx_errors
= htonll(ops
->stats
.tx_errors
);
7716 ps11
->rx_frame_err
= htonll(ops
->stats
.rx_frame_errors
);
7717 ps11
->rx_over_err
= htonll(ops
->stats
.rx_over_errors
);
7718 ps11
->rx_crc_err
= htonll(ops
->stats
.rx_crc_errors
);
7719 ps11
->collisions
= htonll(ops
->stats
.collisions
);
7723 ofputil_port_stats_to_ofp13(const struct ofputil_port_stats
*ops
,
7724 struct ofp13_port_stats
*ps13
)
7726 ofputil_port_stats_to_ofp11(ops
, &ps13
->ps
);
7727 ps13
->duration_sec
= htonl(ops
->duration_sec
);
7728 ps13
->duration_nsec
= htonl(ops
->duration_nsec
);
7732 ofputil_append_ofp14_port_stats(const struct ofputil_port_stats
*ops
,
7733 struct ovs_list
*replies
)
7735 struct ofp14_port_stats_prop_ethernet
*eth
;
7736 struct intel_port_stats_rfc2819
*stats_rfc2819
;
7737 struct ofp14_port_stats
*ps14
;
7738 struct ofpbuf
*reply
;
7740 reply
= ofpmp_reserve(replies
, sizeof *ps14
+ sizeof *eth
+
7741 sizeof *stats_rfc2819
);
7743 ps14
= ofpbuf_put_uninit(reply
, sizeof *ps14
);
7744 ps14
->length
= htons(sizeof *ps14
+ sizeof *eth
+
7745 sizeof *stats_rfc2819
);
7746 memset(ps14
->pad
, 0, sizeof ps14
->pad
);
7747 ps14
->port_no
= ofputil_port_to_ofp11(ops
->port_no
);
7748 ps14
->duration_sec
= htonl(ops
->duration_sec
);
7749 ps14
->duration_nsec
= htonl(ops
->duration_nsec
);
7750 ps14
->rx_packets
= htonll(ops
->stats
.rx_packets
);
7751 ps14
->tx_packets
= htonll(ops
->stats
.tx_packets
);
7752 ps14
->rx_bytes
= htonll(ops
->stats
.rx_bytes
);
7753 ps14
->tx_bytes
= htonll(ops
->stats
.tx_bytes
);
7754 ps14
->rx_dropped
= htonll(ops
->stats
.rx_dropped
);
7755 ps14
->tx_dropped
= htonll(ops
->stats
.tx_dropped
);
7756 ps14
->rx_errors
= htonll(ops
->stats
.rx_errors
);
7757 ps14
->tx_errors
= htonll(ops
->stats
.tx_errors
);
7759 eth
= ofpprop_put_zeros(reply
, OFPPSPT14_ETHERNET
, sizeof *eth
);
7760 eth
->rx_frame_err
= htonll(ops
->stats
.rx_frame_errors
);
7761 eth
->rx_over_err
= htonll(ops
->stats
.rx_over_errors
);
7762 eth
->rx_crc_err
= htonll(ops
->stats
.rx_crc_errors
);
7763 eth
->collisions
= htonll(ops
->stats
.collisions
);
7765 uint64_t prop_type
= OFPPROP_EXP(INTEL_VENDOR_ID
,
7766 INTEL_PORT_STATS_RFC2819
);
7768 stats_rfc2819
= ofpprop_put_zeros(reply
, prop_type
,
7769 sizeof *stats_rfc2819
);
7771 memset(stats_rfc2819
->pad
, 0, sizeof stats_rfc2819
->pad
);
7772 stats_rfc2819
->rx_1_to_64_packets
= htonll(ops
->stats
.rx_1_to_64_packets
);
7773 stats_rfc2819
->rx_65_to_127_packets
=
7774 htonll(ops
->stats
.rx_65_to_127_packets
);
7775 stats_rfc2819
->rx_128_to_255_packets
=
7776 htonll(ops
->stats
.rx_128_to_255_packets
);
7777 stats_rfc2819
->rx_256_to_511_packets
=
7778 htonll(ops
->stats
.rx_256_to_511_packets
);
7779 stats_rfc2819
->rx_512_to_1023_packets
=
7780 htonll(ops
->stats
.rx_512_to_1023_packets
);
7781 stats_rfc2819
->rx_1024_to_1522_packets
=
7782 htonll(ops
->stats
.rx_1024_to_1522_packets
);
7783 stats_rfc2819
->rx_1523_to_max_packets
=
7784 htonll(ops
->stats
.rx_1523_to_max_packets
);
7786 stats_rfc2819
->tx_1_to_64_packets
= htonll(ops
->stats
.tx_1_to_64_packets
);
7787 stats_rfc2819
->tx_65_to_127_packets
=
7788 htonll(ops
->stats
.tx_65_to_127_packets
);
7789 stats_rfc2819
->tx_128_to_255_packets
=
7790 htonll(ops
->stats
.tx_128_to_255_packets
);
7791 stats_rfc2819
->tx_256_to_511_packets
=
7792 htonll(ops
->stats
.tx_256_to_511_packets
);
7793 stats_rfc2819
->tx_512_to_1023_packets
=
7794 htonll(ops
->stats
.tx_512_to_1023_packets
);
7795 stats_rfc2819
->tx_1024_to_1522_packets
=
7796 htonll(ops
->stats
.tx_1024_to_1522_packets
);
7797 stats_rfc2819
->tx_1523_to_max_packets
=
7798 htonll(ops
->stats
.tx_1523_to_max_packets
);
7800 stats_rfc2819
->tx_multicast_packets
=
7801 htonll(ops
->stats
.tx_multicast_packets
);
7802 stats_rfc2819
->rx_broadcast_packets
=
7803 htonll(ops
->stats
.rx_broadcast_packets
);
7804 stats_rfc2819
->tx_broadcast_packets
=
7805 htonll(ops
->stats
.tx_broadcast_packets
);
7806 stats_rfc2819
->rx_undersized_errors
=
7807 htonll(ops
->stats
.rx_undersized_errors
);
7808 stats_rfc2819
->rx_oversize_errors
=
7809 htonll(ops
->stats
.rx_oversize_errors
);
7810 stats_rfc2819
->rx_fragmented_errors
=
7811 htonll(ops
->stats
.rx_fragmented_errors
);
7812 stats_rfc2819
->rx_jabber_errors
=
7813 htonll(ops
->stats
.rx_jabber_errors
);
7816 /* Encode a ports stat for 'ops' and append it to 'replies'. */
7818 ofputil_append_port_stat(struct ovs_list
*replies
,
7819 const struct ofputil_port_stats
*ops
)
7821 switch (ofpmp_version(replies
)) {
7822 case OFP13_VERSION
: {
7823 struct ofp13_port_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
7824 ofputil_port_stats_to_ofp13(ops
, reply
);
7828 case OFP11_VERSION
: {
7829 struct ofp11_port_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
7830 ofputil_port_stats_to_ofp11(ops
, reply
);
7834 case OFP10_VERSION
: {
7835 struct ofp10_port_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
7836 ofputil_port_stats_to_ofp10(ops
, reply
);
7843 ofputil_append_ofp14_port_stats(ops
, replies
);
7852 ofputil_port_stats_from_ofp10(struct ofputil_port_stats
*ops
,
7853 const struct ofp10_port_stats
*ps10
)
7856 ops
->port_no
= u16_to_ofp(ntohs(ps10
->port_no
));
7857 ops
->stats
.rx_packets
= ntohll(get_32aligned_be64(&ps10
->rx_packets
));
7858 ops
->stats
.tx_packets
= ntohll(get_32aligned_be64(&ps10
->tx_packets
));
7859 ops
->stats
.rx_bytes
= ntohll(get_32aligned_be64(&ps10
->rx_bytes
));
7860 ops
->stats
.tx_bytes
= ntohll(get_32aligned_be64(&ps10
->tx_bytes
));
7861 ops
->stats
.rx_dropped
= ntohll(get_32aligned_be64(&ps10
->rx_dropped
));
7862 ops
->stats
.tx_dropped
= ntohll(get_32aligned_be64(&ps10
->tx_dropped
));
7863 ops
->stats
.rx_errors
= ntohll(get_32aligned_be64(&ps10
->rx_errors
));
7864 ops
->stats
.tx_errors
= ntohll(get_32aligned_be64(&ps10
->tx_errors
));
7865 ops
->stats
.rx_frame_errors
=
7866 ntohll(get_32aligned_be64(&ps10
->rx_frame_err
));
7867 ops
->stats
.rx_over_errors
= ntohll(get_32aligned_be64(&ps10
->rx_over_err
));
7868 ops
->stats
.rx_crc_errors
= ntohll(get_32aligned_be64(&ps10
->rx_crc_err
));
7869 ops
->stats
.collisions
= ntohll(get_32aligned_be64(&ps10
->collisions
));
7870 ops
->duration_sec
= ops
->duration_nsec
= UINT32_MAX
;
7876 ofputil_port_stats_from_ofp11(struct ofputil_port_stats
*ops
,
7877 const struct ofp11_port_stats
*ps11
)
7881 error
= ofputil_port_from_ofp11(ps11
->port_no
, &ops
->port_no
);
7886 ops
->stats
.rx_packets
= ntohll(ps11
->rx_packets
);
7887 ops
->stats
.tx_packets
= ntohll(ps11
->tx_packets
);
7888 ops
->stats
.rx_bytes
= ntohll(ps11
->rx_bytes
);
7889 ops
->stats
.tx_bytes
= ntohll(ps11
->tx_bytes
);
7890 ops
->stats
.rx_dropped
= ntohll(ps11
->rx_dropped
);
7891 ops
->stats
.tx_dropped
= ntohll(ps11
->tx_dropped
);
7892 ops
->stats
.rx_errors
= ntohll(ps11
->rx_errors
);
7893 ops
->stats
.tx_errors
= ntohll(ps11
->tx_errors
);
7894 ops
->stats
.rx_frame_errors
= ntohll(ps11
->rx_frame_err
);
7895 ops
->stats
.rx_over_errors
= ntohll(ps11
->rx_over_err
);
7896 ops
->stats
.rx_crc_errors
= ntohll(ps11
->rx_crc_err
);
7897 ops
->stats
.collisions
= ntohll(ps11
->collisions
);
7898 ops
->duration_sec
= ops
->duration_nsec
= UINT32_MAX
;
7904 ofputil_port_stats_from_ofp13(struct ofputil_port_stats
*ops
,
7905 const struct ofp13_port_stats
*ps13
)
7907 enum ofperr error
= ofputil_port_stats_from_ofp11(ops
, &ps13
->ps
);
7909 ops
->duration_sec
= ntohl(ps13
->duration_sec
);
7910 ops
->duration_nsec
= ntohl(ps13
->duration_nsec
);
7916 parse_ofp14_port_stats_ethernet_property(const struct ofpbuf
*payload
,
7917 struct ofputil_port_stats
*ops
)
7919 const struct ofp14_port_stats_prop_ethernet
*eth
= payload
->data
;
7921 if (payload
->size
!= sizeof *eth
) {
7922 return OFPERR_OFPBPC_BAD_LEN
;
7925 ops
->stats
.rx_frame_errors
= ntohll(eth
->rx_frame_err
);
7926 ops
->stats
.rx_over_errors
= ntohll(eth
->rx_over_err
);
7927 ops
->stats
.rx_crc_errors
= ntohll(eth
->rx_crc_err
);
7928 ops
->stats
.collisions
= ntohll(eth
->collisions
);
7934 parse_intel_port_stats_rfc2819_property(const struct ofpbuf
*payload
,
7935 struct ofputil_port_stats
*ops
)
7937 const struct intel_port_stats_rfc2819
*rfc2819
= payload
->data
;
7939 if (payload
->size
!= sizeof *rfc2819
) {
7940 return OFPERR_OFPBPC_BAD_LEN
;
7942 ops
->stats
.rx_1_to_64_packets
= ntohll(rfc2819
->rx_1_to_64_packets
);
7943 ops
->stats
.rx_65_to_127_packets
= ntohll(rfc2819
->rx_65_to_127_packets
);
7944 ops
->stats
.rx_128_to_255_packets
= ntohll(rfc2819
->rx_128_to_255_packets
);
7945 ops
->stats
.rx_256_to_511_packets
= ntohll(rfc2819
->rx_256_to_511_packets
);
7946 ops
->stats
.rx_512_to_1023_packets
=
7947 ntohll(rfc2819
->rx_512_to_1023_packets
);
7948 ops
->stats
.rx_1024_to_1522_packets
=
7949 ntohll(rfc2819
->rx_1024_to_1522_packets
);
7950 ops
->stats
.rx_1523_to_max_packets
=
7951 ntohll(rfc2819
->rx_1523_to_max_packets
);
7953 ops
->stats
.tx_1_to_64_packets
= ntohll(rfc2819
->tx_1_to_64_packets
);
7954 ops
->stats
.tx_65_to_127_packets
= ntohll(rfc2819
->tx_65_to_127_packets
);
7955 ops
->stats
.tx_128_to_255_packets
= ntohll(rfc2819
->tx_128_to_255_packets
);
7956 ops
->stats
.tx_256_to_511_packets
= ntohll(rfc2819
->tx_256_to_511_packets
);
7957 ops
->stats
.tx_512_to_1023_packets
=
7958 ntohll(rfc2819
->tx_512_to_1023_packets
);
7959 ops
->stats
.tx_1024_to_1522_packets
=
7960 ntohll(rfc2819
->tx_1024_to_1522_packets
);
7961 ops
->stats
.tx_1523_to_max_packets
=
7962 ntohll(rfc2819
->tx_1523_to_max_packets
);
7964 ops
->stats
.tx_multicast_packets
= ntohll(rfc2819
->tx_multicast_packets
);
7965 ops
->stats
.rx_broadcast_packets
= ntohll(rfc2819
->rx_broadcast_packets
);
7966 ops
->stats
.tx_broadcast_packets
= ntohll(rfc2819
->tx_broadcast_packets
);
7967 ops
->stats
.rx_undersized_errors
= ntohll(rfc2819
->rx_undersized_errors
);
7969 ops
->stats
.rx_oversize_errors
= ntohll(rfc2819
->rx_oversize_errors
);
7970 ops
->stats
.rx_fragmented_errors
= ntohll(rfc2819
->rx_fragmented_errors
);
7971 ops
->stats
.rx_jabber_errors
= ntohll(rfc2819
->rx_jabber_errors
);
7977 parse_intel_port_stats_property(const struct ofpbuf
*payload
,
7979 struct ofputil_port_stats
*ops
)
7984 case INTEL_PORT_STATS_RFC2819
:
7985 error
= parse_intel_port_stats_rfc2819_property(payload
, ops
);
7988 error
= OFPERR_OFPBPC_BAD_EXP_TYPE
;
7996 ofputil_pull_ofp14_port_stats(struct ofputil_port_stats
*ops
,
7999 const struct ofp14_port_stats
*ps14
= ofpbuf_try_pull(msg
, sizeof *ps14
);
8001 return OFPERR_OFPBRC_BAD_LEN
;
8004 size_t len
= ntohs(ps14
->length
);
8005 if (len
< sizeof *ps14
|| len
- sizeof *ps14
> msg
->size
) {
8006 return OFPERR_OFPBRC_BAD_LEN
;
8008 len
-= sizeof *ps14
;
8010 enum ofperr error
= ofputil_port_from_ofp11(ps14
->port_no
, &ops
->port_no
);
8015 ops
->duration_sec
= ntohl(ps14
->duration_sec
);
8016 ops
->duration_nsec
= ntohl(ps14
->duration_nsec
);
8017 ops
->stats
.rx_packets
= ntohll(ps14
->rx_packets
);
8018 ops
->stats
.tx_packets
= ntohll(ps14
->tx_packets
);
8019 ops
->stats
.rx_bytes
= ntohll(ps14
->rx_bytes
);
8020 ops
->stats
.tx_bytes
= ntohll(ps14
->tx_bytes
);
8021 ops
->stats
.rx_dropped
= ntohll(ps14
->rx_dropped
);
8022 ops
->stats
.tx_dropped
= ntohll(ps14
->tx_dropped
);
8023 ops
->stats
.rx_errors
= ntohll(ps14
->rx_errors
);
8024 ops
->stats
.tx_errors
= ntohll(ps14
->tx_errors
);
8027 struct ofpbuf properties
= ofpbuf_const_initializer(ofpbuf_pull(msg
, len
),
8029 while (properties
.size
> 0) {
8030 struct ofpbuf payload
;
8034 error
= ofpprop_pull(&properties
, &payload
, &type
);
8039 case OFPPSPT14_ETHERNET
:
8040 error
= parse_ofp14_port_stats_ethernet_property(&payload
, ops
);
8042 case OFPPROP_EXP(INTEL_VENDOR_ID
, INTEL_PORT_STATS_RFC2819
):
8043 error
= parse_intel_port_stats_property(&payload
,
8044 INTEL_PORT_STATS_RFC2819
,
8048 error
= OFPPROP_UNKNOWN(true, "port stats", type
);
8060 /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
8063 ofputil_count_port_stats(const struct ofp_header
*oh
)
8065 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
8066 ofpraw_pull_assert(&b
);
8068 for (size_t n
= 0; ; n
++) {
8069 struct ofputil_port_stats ps
;
8070 if (ofputil_decode_port_stats(&ps
, &b
)) {
8076 /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
8077 * ofputil_port_stats in 'ps'.
8079 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
8080 * message. Calling this function multiple times for a single 'msg' iterates
8081 * through the replies. The caller must initially leave 'msg''s layer pointers
8082 * null and not modify them between calls.
8084 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8085 * otherwise a positive errno value. */
8087 ofputil_decode_port_stats(struct ofputil_port_stats
*ps
, struct ofpbuf
*msg
)
8092 memset(&(ps
->stats
), 0xFF, sizeof (ps
->stats
));
8094 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
8095 : ofpraw_pull(&raw
, msg
));
8102 } else if (raw
== OFPRAW_OFPST14_PORT_REPLY
) {
8103 return ofputil_pull_ofp14_port_stats(ps
, msg
);
8104 } else if (raw
== OFPRAW_OFPST13_PORT_REPLY
) {
8105 const struct ofp13_port_stats
*ps13
;
8106 ps13
= ofpbuf_try_pull(msg
, sizeof *ps13
);
8110 return ofputil_port_stats_from_ofp13(ps
, ps13
);
8111 } else if (raw
== OFPRAW_OFPST11_PORT_REPLY
) {
8112 const struct ofp11_port_stats
*ps11
;
8114 ps11
= ofpbuf_try_pull(msg
, sizeof *ps11
);
8118 return ofputil_port_stats_from_ofp11(ps
, ps11
);
8119 } else if (raw
== OFPRAW_OFPST10_PORT_REPLY
) {
8120 const struct ofp10_port_stats
*ps10
;
8122 ps10
= ofpbuf_try_pull(msg
, sizeof *ps10
);
8126 return ofputil_port_stats_from_ofp10(ps
, ps10
);
8132 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_PORT reply has %"PRIu32
" leftover "
8133 "bytes at end", msg
->size
);
8134 return OFPERR_OFPBRC_BAD_LEN
;
8137 /* Parse a port status request message into a 16 bit OpenFlow 1.0
8138 * port number and stores the latter in '*ofp10_port'.
8139 * Returns 0 if successful, otherwise an OFPERR_* number. */
8141 ofputil_decode_port_stats_request(const struct ofp_header
*request
,
8142 ofp_port_t
*ofp10_port
)
8144 switch ((enum ofp_version
)request
->version
) {
8150 case OFP11_VERSION
: {
8151 const struct ofp11_port_stats_request
*psr11
= ofpmsg_body(request
);
8152 return ofputil_port_from_ofp11(psr11
->port_no
, ofp10_port
);
8155 case OFP10_VERSION
: {
8156 const struct ofp10_port_stats_request
*psr10
= ofpmsg_body(request
);
8157 *ofp10_port
= u16_to_ofp(ntohs(psr10
->port_no
));
8167 ofputil_ipfix_stats_to_reply(const struct ofputil_ipfix_stats
*ois
,
8168 struct nx_ipfix_stats_reply
*reply
)
8170 reply
->collector_set_id
= htonl(ois
->collector_set_id
);
8171 reply
->total_flows
= htonll(ois
->total_flows
);
8172 reply
->current_flows
= htonll(ois
->current_flows
);
8173 reply
->pkts
= htonll(ois
->pkts
);
8174 reply
->ipv4_pkts
= htonll(ois
->ipv4_pkts
);
8175 reply
->ipv6_pkts
= htonll(ois
->ipv6_pkts
);
8176 reply
->error_pkts
= htonll(ois
->error_pkts
);
8177 reply
->ipv4_error_pkts
= htonll(ois
->ipv4_error_pkts
);
8178 reply
->ipv6_error_pkts
= htonll(ois
->ipv6_error_pkts
);
8179 reply
->tx_pkts
= htonll(ois
->tx_pkts
);
8180 reply
->tx_errors
= htonll(ois
->tx_errors
);
8181 memset(reply
->pad
, 0, sizeof reply
->pad
);
8184 /* Encode a ipfix stat for 'ois' and append it to 'replies'. */
8186 ofputil_append_ipfix_stat(struct ovs_list
*replies
,
8187 const struct ofputil_ipfix_stats
*ois
)
8189 struct nx_ipfix_stats_reply
*reply
= ofpmp_append(replies
, sizeof *reply
);
8190 ofputil_ipfix_stats_to_reply(ois
, reply
);
8194 ofputil_ipfix_stats_from_nx(struct ofputil_ipfix_stats
*is
,
8195 const struct nx_ipfix_stats_reply
*reply
)
8197 is
->collector_set_id
= ntohl(reply
->collector_set_id
);
8198 is
->total_flows
= ntohll(reply
->total_flows
);
8199 is
->current_flows
= ntohll(reply
->current_flows
);
8200 is
->pkts
= ntohll(reply
->pkts
);
8201 is
->ipv4_pkts
= ntohll(reply
->ipv4_pkts
);
8202 is
->ipv6_pkts
= ntohll(reply
->ipv6_pkts
);
8203 is
->error_pkts
= ntohll(reply
->error_pkts
);
8204 is
->ipv4_error_pkts
= ntohll(reply
->ipv4_error_pkts
);
8205 is
->ipv6_error_pkts
= ntohll(reply
->ipv6_error_pkts
);
8206 is
->tx_pkts
= ntohll(reply
->tx_pkts
);
8207 is
->tx_errors
= ntohll(reply
->tx_errors
);
8213 ofputil_pull_ipfix_stats(struct ofputil_ipfix_stats
*is
, struct ofpbuf
*msg
)
8218 memset(is
, 0xFF, sizeof (*is
));
8220 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
8221 : ofpraw_pull(&raw
, msg
));
8228 } else if (raw
== OFPRAW_NXST_IPFIX_BRIDGE_REPLY
||
8229 raw
== OFPRAW_NXST_IPFIX_FLOW_REPLY
) {
8230 struct nx_ipfix_stats_reply
*reply
;
8232 reply
= ofpbuf_try_pull(msg
, sizeof *reply
);
8233 return ofputil_ipfix_stats_from_nx(is
, reply
);
8240 /* Returns the number of ipfix stats elements in
8241 * OFPTYPE_IPFIX_BRIDGE_STATS_REPLY or OFPTYPE_IPFIX_FLOW_STATS_REPLY
8244 ofputil_count_ipfix_stats(const struct ofp_header
*oh
)
8246 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
8247 ofpraw_pull_assert(&b
);
8249 return b
.size
/ sizeof(struct ofputil_ipfix_stats
);
8252 /* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */
8254 ofputil_bucket_list_destroy(struct ovs_list
*buckets
)
8256 struct ofputil_bucket
*bucket
;
8258 LIST_FOR_EACH_POP (bucket
, list_node
, buckets
) {
8259 free(bucket
->ofpacts
);
8264 /* Clones 'bucket' and its ofpacts data */
8265 static struct ofputil_bucket
*
8266 ofputil_bucket_clone_data(const struct ofputil_bucket
*bucket
)
8268 struct ofputil_bucket
*new;
8270 new = xmemdup(bucket
, sizeof *bucket
);
8271 new->ofpacts
= xmemdup(bucket
->ofpacts
, bucket
->ofpacts_len
);
8276 /* Clones each of the buckets in the list 'src' appending them
8277 * in turn to 'dest' which should be an initialised list.
8278 * An exception is that if the pointer value of a bucket in 'src'
8279 * matches 'skip' then it is not cloned or appended to 'dest'.
8280 * This allows all of 'src' or 'all of 'src' except 'skip' to
8281 * be cloned and appended to 'dest'. */
8283 ofputil_bucket_clone_list(struct ovs_list
*dest
, const struct ovs_list
*src
,
8284 const struct ofputil_bucket
*skip
)
8286 struct ofputil_bucket
*bucket
;
8288 LIST_FOR_EACH (bucket
, list_node
, src
) {
8289 struct ofputil_bucket
*new_bucket
;
8291 if (bucket
== skip
) {
8295 new_bucket
= ofputil_bucket_clone_data(bucket
);
8296 ovs_list_push_back(dest
, &new_bucket
->list_node
);
8300 /* Find a bucket in the list 'buckets' whose bucket id is 'bucket_id'
8301 * Returns the first bucket found or NULL if no buckets are found. */
8302 struct ofputil_bucket
*
8303 ofputil_bucket_find(const struct ovs_list
*buckets
, uint32_t bucket_id
)
8305 struct ofputil_bucket
*bucket
;
8307 if (bucket_id
> OFPG15_BUCKET_MAX
) {
8311 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
8312 if (bucket
->bucket_id
== bucket_id
) {
8320 /* Returns true if more than one bucket in the list 'buckets'
8321 * have the same bucket id. Returns false otherwise. */
8323 ofputil_bucket_check_duplicate_id(const struct ovs_list
*buckets
)
8325 struct ofputil_bucket
*i
, *j
;
8327 LIST_FOR_EACH (i
, list_node
, buckets
) {
8328 LIST_FOR_EACH_REVERSE (j
, list_node
, buckets
) {
8332 if (i
->bucket_id
== j
->bucket_id
) {
8341 /* Returns the bucket at the front of the list 'buckets'.
8342 * Undefined if 'buckets is empty. */
8343 struct ofputil_bucket
*
8344 ofputil_bucket_list_front(const struct ovs_list
*buckets
)
8346 static struct ofputil_bucket
*bucket
;
8348 ASSIGN_CONTAINER(bucket
, ovs_list_front(buckets
), list_node
);
8353 /* Returns the bucket at the back of the list 'buckets'.
8354 * Undefined if 'buckets is empty. */
8355 struct ofputil_bucket
*
8356 ofputil_bucket_list_back(const struct ovs_list
*buckets
)
8358 static struct ofputil_bucket
*bucket
;
8360 ASSIGN_CONTAINER(bucket
, ovs_list_back(buckets
), list_node
);
8365 /* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version',
8366 * that requests stats for group 'group_id'. (Use OFPG_ALL to request stats
8369 * Group statistics include packet and byte counts for each group. */
8371 ofputil_encode_group_stats_request(enum ofp_version ofp_version
,
8374 struct ofpbuf
*request
;
8376 switch (ofp_version
) {
8378 ovs_fatal(0, "dump-group-stats needs OpenFlow 1.1 or later "
8379 "(\'-O OpenFlow11\')");
8385 case OFP16_VERSION
: {
8386 struct ofp11_group_stats_request
*req
;
8387 request
= ofpraw_alloc(OFPRAW_OFPST11_GROUP_REQUEST
, ofp_version
, 0);
8388 req
= ofpbuf_put_zeros(request
, sizeof *req
);
8389 req
->group_id
= htonl(group_id
);
8400 ofputil_uninit_group_desc(struct ofputil_group_desc
*gd
)
8402 ofputil_bucket_list_destroy(&gd
->buckets
);
8403 ofputil_group_properties_destroy(&gd
->props
);
8406 /* Decodes the OpenFlow group description request in 'oh', returning the group
8407 * whose description is requested, or OFPG_ALL if stats for all groups was
8410 ofputil_decode_group_desc_request(const struct ofp_header
*oh
)
8412 struct ofpbuf request
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
8413 enum ofpraw raw
= ofpraw_pull_assert(&request
);
8414 if (raw
== OFPRAW_OFPST11_GROUP_DESC_REQUEST
) {
8416 } else if (raw
== OFPRAW_OFPST15_GROUP_DESC_REQUEST
) {
8417 ovs_be32
*group_id
= ofpbuf_pull(&request
, sizeof *group_id
);
8418 return ntohl(*group_id
);
8424 /* Returns an OpenFlow group description request for OpenFlow version
8425 * 'ofp_version', that requests stats for group 'group_id'. Use OFPG_ALL to
8426 * request stats for all groups (OpenFlow 1.4 and earlier always request all
8429 * Group descriptions include the bucket and action configuration for each
8432 ofputil_encode_group_desc_request(enum ofp_version ofp_version
,
8435 struct ofpbuf
*request
;
8437 switch (ofp_version
) {
8439 ovs_fatal(0, "dump-groups needs OpenFlow 1.1 or later "
8440 "(\'-O OpenFlow11\')");
8445 request
= ofpraw_alloc(OFPRAW_OFPST11_GROUP_DESC_REQUEST
,
8449 case OFP16_VERSION
: {
8450 struct ofp15_group_desc_request
*req
;
8451 request
= ofpraw_alloc(OFPRAW_OFPST15_GROUP_DESC_REQUEST
,
8453 req
= ofpbuf_put_zeros(request
, sizeof *req
);
8454 req
->group_id
= htonl(group_id
);
8465 ofputil_group_bucket_counters_to_ofp11(const struct ofputil_group_stats
*gs
,
8466 struct ofp11_bucket_counter bucket_cnts
[])
8470 for (i
= 0; i
< gs
->n_buckets
; i
++) {
8471 bucket_cnts
[i
].packet_count
= htonll(gs
->bucket_stats
[i
].packet_count
);
8472 bucket_cnts
[i
].byte_count
= htonll(gs
->bucket_stats
[i
].byte_count
);
8477 ofputil_group_stats_to_ofp11(const struct ofputil_group_stats
*gs
,
8478 struct ofp11_group_stats
*gs11
, size_t length
,
8479 struct ofp11_bucket_counter bucket_cnts
[])
8481 memset(gs11
, 0, sizeof *gs11
);
8482 gs11
->length
= htons(length
);
8483 gs11
->group_id
= htonl(gs
->group_id
);
8484 gs11
->ref_count
= htonl(gs
->ref_count
);
8485 gs11
->packet_count
= htonll(gs
->packet_count
);
8486 gs11
->byte_count
= htonll(gs
->byte_count
);
8487 ofputil_group_bucket_counters_to_ofp11(gs
, bucket_cnts
);
8491 ofputil_group_stats_to_ofp13(const struct ofputil_group_stats
*gs
,
8492 struct ofp13_group_stats
*gs13
, size_t length
,
8493 struct ofp11_bucket_counter bucket_cnts
[])
8495 ofputil_group_stats_to_ofp11(gs
, &gs13
->gs
, length
, bucket_cnts
);
8496 gs13
->duration_sec
= htonl(gs
->duration_sec
);
8497 gs13
->duration_nsec
= htonl(gs
->duration_nsec
);
8501 /* Encodes 'gs' properly for the format of the list of group statistics
8502 * replies already begun in 'replies' and appends it to the list. 'replies'
8503 * must have originally been initialized with ofpmp_init(). */
8505 ofputil_append_group_stats(struct ovs_list
*replies
,
8506 const struct ofputil_group_stats
*gs
)
8508 size_t bucket_counter_size
;
8509 struct ofp11_bucket_counter
*bucket_counters
;
8512 bucket_counter_size
= gs
->n_buckets
* sizeof(struct ofp11_bucket_counter
);
8514 switch (ofpmp_version(replies
)) {
8516 case OFP12_VERSION
:{
8517 struct ofp11_group_stats
*gs11
;
8519 length
= sizeof *gs11
+ bucket_counter_size
;
8520 gs11
= ofpmp_append(replies
, length
);
8521 bucket_counters
= (struct ofp11_bucket_counter
*)(gs11
+ 1);
8522 ofputil_group_stats_to_ofp11(gs
, gs11
, length
, bucket_counters
);
8529 case OFP16_VERSION
: {
8530 struct ofp13_group_stats
*gs13
;
8532 length
= sizeof *gs13
+ bucket_counter_size
;
8533 gs13
= ofpmp_append(replies
, length
);
8534 bucket_counters
= (struct ofp11_bucket_counter
*)(gs13
+ 1);
8535 ofputil_group_stats_to_ofp13(gs
, gs13
, length
, bucket_counters
);
8544 /* Returns an OpenFlow group features request for OpenFlow version
8547 ofputil_encode_group_features_request(enum ofp_version ofp_version
)
8549 struct ofpbuf
*request
= NULL
;
8551 switch (ofp_version
) {
8554 ovs_fatal(0, "dump-group-features needs OpenFlow 1.2 or later "
8555 "(\'-O OpenFlow12\')");
8561 request
= ofpraw_alloc(OFPRAW_OFPST12_GROUP_FEATURES_REQUEST
,
8571 /* Returns a OpenFlow message that encodes 'features' properly as a reply to
8572 * group features request 'request'. */
8574 ofputil_encode_group_features_reply(
8575 const struct ofputil_group_features
*features
,
8576 const struct ofp_header
*request
)
8578 struct ofp12_group_features_stats
*ogf
;
8579 struct ofpbuf
*reply
;
8582 reply
= ofpraw_alloc_xid(OFPRAW_OFPST12_GROUP_FEATURES_REPLY
,
8583 request
->version
, request
->xid
, 0);
8584 ogf
= ofpbuf_put_zeros(reply
, sizeof *ogf
);
8585 ogf
->types
= htonl(features
->types
);
8586 ogf
->capabilities
= htonl(features
->capabilities
);
8587 for (i
= 0; i
< OFPGT12_N_TYPES
; i
++) {
8588 ogf
->max_groups
[i
] = htonl(features
->max_groups
[i
]);
8589 ogf
->actions
[i
] = ofpact_bitmap_to_openflow(features
->ofpacts
[i
],
8596 /* Decodes group features reply 'oh' into 'features'. */
8598 ofputil_decode_group_features_reply(const struct ofp_header
*oh
,
8599 struct ofputil_group_features
*features
)
8601 const struct ofp12_group_features_stats
*ogf
= ofpmsg_body(oh
);
8604 features
->types
= ntohl(ogf
->types
);
8605 features
->capabilities
= ntohl(ogf
->capabilities
);
8606 for (i
= 0; i
< OFPGT12_N_TYPES
; i
++) {
8607 features
->max_groups
[i
] = ntohl(ogf
->max_groups
[i
]);
8608 features
->ofpacts
[i
] = ofpact_bitmap_from_openflow(
8609 ogf
->actions
[i
], oh
->version
);
8613 /* Parse a group status request message into a 32 bit OpenFlow 1.1
8614 * group ID and stores the latter in '*group_id'.
8615 * Returns 0 if successful, otherwise an OFPERR_* number. */
8617 ofputil_decode_group_stats_request(const struct ofp_header
*request
,
8620 const struct ofp11_group_stats_request
*gsr11
= ofpmsg_body(request
);
8621 *group_id
= ntohl(gsr11
->group_id
);
8625 /* Converts a group stats reply in 'msg' into an abstract ofputil_group_stats
8626 * in 'gs'. Assigns freshly allocated memory to gs->bucket_stats for the
8627 * caller to eventually free.
8629 * Multiple group stats replies can be packed into a single OpenFlow message.
8630 * Calling this function multiple times for a single 'msg' iterates through the
8631 * replies. The caller must initially leave 'msg''s layer pointers null and
8632 * not modify them between calls.
8634 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8635 * otherwise a positive errno value. */
8637 ofputil_decode_group_stats_reply(struct ofpbuf
*msg
,
8638 struct ofputil_group_stats
*gs
)
8640 struct ofp11_bucket_counter
*obc
;
8641 struct ofp11_group_stats
*ogs11
;
8648 gs
->bucket_stats
= NULL
;
8649 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
8650 : ofpraw_pull(&raw
, msg
));
8659 if (raw
== OFPRAW_OFPST11_GROUP_REPLY
) {
8660 base_len
= sizeof *ogs11
;
8661 ogs11
= ofpbuf_try_pull(msg
, sizeof *ogs11
);
8662 gs
->duration_sec
= gs
->duration_nsec
= UINT32_MAX
;
8663 } else if (raw
== OFPRAW_OFPST13_GROUP_REPLY
) {
8664 struct ofp13_group_stats
*ogs13
;
8666 base_len
= sizeof *ogs13
;
8667 ogs13
= ofpbuf_try_pull(msg
, sizeof *ogs13
);
8670 gs
->duration_sec
= ntohl(ogs13
->duration_sec
);
8671 gs
->duration_nsec
= ntohl(ogs13
->duration_nsec
);
8680 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s reply has %"PRIu32
" leftover bytes at end",
8681 ofpraw_get_name(raw
), msg
->size
);
8682 return OFPERR_OFPBRC_BAD_LEN
;
8684 length
= ntohs(ogs11
->length
);
8685 if (length
< sizeof base_len
) {
8686 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s reply claims invalid length %"PRIuSIZE
,
8687 ofpraw_get_name(raw
), length
);
8688 return OFPERR_OFPBRC_BAD_LEN
;
8691 gs
->group_id
= ntohl(ogs11
->group_id
);
8692 gs
->ref_count
= ntohl(ogs11
->ref_count
);
8693 gs
->packet_count
= ntohll(ogs11
->packet_count
);
8694 gs
->byte_count
= ntohll(ogs11
->byte_count
);
8696 gs
->n_buckets
= (length
- base_len
) / sizeof *obc
;
8697 obc
= ofpbuf_try_pull(msg
, gs
->n_buckets
* sizeof *obc
);
8699 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s reply has %"PRIu32
" leftover bytes at end",
8700 ofpraw_get_name(raw
), msg
->size
);
8701 return OFPERR_OFPBRC_BAD_LEN
;
8704 gs
->bucket_stats
= xmalloc(gs
->n_buckets
* sizeof *gs
->bucket_stats
);
8705 for (i
= 0; i
< gs
->n_buckets
; i
++) {
8706 gs
->bucket_stats
[i
].packet_count
= ntohll(obc
[i
].packet_count
);
8707 gs
->bucket_stats
[i
].byte_count
= ntohll(obc
[i
].byte_count
);
8714 ofputil_put_ofp11_bucket(const struct ofputil_bucket
*bucket
,
8715 struct ofpbuf
*openflow
, enum ofp_version ofp_version
)
8717 struct ofp11_bucket
*ob
;
8720 start
= openflow
->size
;
8721 ofpbuf_put_zeros(openflow
, sizeof *ob
);
8722 ofpacts_put_openflow_actions(bucket
->ofpacts
, bucket
->ofpacts_len
,
8723 openflow
, ofp_version
);
8724 ob
= ofpbuf_at_assert(openflow
, start
, sizeof *ob
);
8725 ob
->len
= htons(openflow
->size
- start
);
8726 ob
->weight
= htons(bucket
->weight
);
8727 ob
->watch_port
= ofputil_port_to_ofp11(bucket
->watch_port
);
8728 ob
->watch_group
= htonl(bucket
->watch_group
);
8732 ofputil_put_ofp15_bucket(const struct ofputil_bucket
*bucket
,
8733 uint32_t bucket_id
, enum ofp11_group_type group_type
,
8734 struct ofpbuf
*openflow
, enum ofp_version ofp_version
)
8736 struct ofp15_bucket
*ob
;
8737 size_t start
, actions_start
, actions_len
;
8739 start
= openflow
->size
;
8740 ofpbuf_put_zeros(openflow
, sizeof *ob
);
8742 actions_start
= openflow
->size
;
8743 ofpacts_put_openflow_actions(bucket
->ofpacts
, bucket
->ofpacts_len
,
8744 openflow
, ofp_version
);
8745 actions_len
= openflow
->size
- actions_start
;
8747 if (group_type
== OFPGT11_SELECT
) {
8748 ofpprop_put_u16(openflow
, OFPGBPT15_WEIGHT
, bucket
->weight
);
8750 if (bucket
->watch_port
!= OFPP_ANY
) {
8751 ofpprop_put_be32(openflow
, OFPGBPT15_WATCH_PORT
,
8752 ofputil_port_to_ofp11(bucket
->watch_port
));
8754 if (bucket
->watch_group
!= OFPG_ANY
) {
8755 ofpprop_put_u32(openflow
, OFPGBPT15_WATCH_GROUP
, bucket
->watch_group
);
8758 ob
= ofpbuf_at_assert(openflow
, start
, sizeof *ob
);
8759 ob
->len
= htons(openflow
->size
- start
);
8760 ob
->action_array_len
= htons(actions_len
);
8761 ob
->bucket_id
= htonl(bucket_id
);
8765 ofputil_put_group_prop_ntr_selection_method(enum ofp_version ofp_version
,
8766 const struct ofputil_group_props
*gp
,
8767 struct ofpbuf
*openflow
)
8769 struct ntr_group_prop_selection_method
*prop
;
8772 start
= openflow
->size
;
8773 ofpbuf_put_zeros(openflow
, sizeof *prop
);
8774 oxm_put_field_array(openflow
, &gp
->fields
, ofp_version
);
8775 prop
= ofpbuf_at_assert(openflow
, start
, sizeof *prop
);
8776 prop
->type
= htons(OFPGPT15_EXPERIMENTER
);
8777 prop
->experimenter
= htonl(NTR_VENDOR_ID
);
8778 prop
->exp_type
= htonl(NTRT_SELECTION_METHOD
);
8779 strcpy(prop
->selection_method
, gp
->selection_method
);
8780 prop
->selection_method_param
= htonll(gp
->selection_method_param
);
8781 ofpprop_end(openflow
, start
);
8785 ofputil_append_ofp11_group_desc_reply(const struct ofputil_group_desc
*gds
,
8786 const struct ovs_list
*buckets
,
8787 struct ovs_list
*replies
,
8788 enum ofp_version version
)
8790 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
8791 struct ofp11_group_desc_stats
*ogds
;
8792 struct ofputil_bucket
*bucket
;
8795 start_ogds
= reply
->size
;
8796 ofpbuf_put_zeros(reply
, sizeof *ogds
);
8797 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
8798 ofputil_put_ofp11_bucket(bucket
, reply
, version
);
8800 ogds
= ofpbuf_at_assert(reply
, start_ogds
, sizeof *ogds
);
8801 ogds
->length
= htons(reply
->size
- start_ogds
);
8802 ogds
->type
= gds
->type
;
8803 ogds
->group_id
= htonl(gds
->group_id
);
8805 ofpmp_postappend(replies
, start_ogds
);
8809 ofputil_append_ofp15_group_desc_reply(const struct ofputil_group_desc
*gds
,
8810 const struct ovs_list
*buckets
,
8811 struct ovs_list
*replies
,
8812 enum ofp_version version
)
8814 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
8815 struct ofp15_group_desc_stats
*ogds
;
8816 struct ofputil_bucket
*bucket
;
8817 size_t start_ogds
, start_buckets
;
8819 start_ogds
= reply
->size
;
8820 ofpbuf_put_zeros(reply
, sizeof *ogds
);
8821 start_buckets
= reply
->size
;
8822 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
8823 ofputil_put_ofp15_bucket(bucket
, bucket
->bucket_id
,
8824 gds
->type
, reply
, version
);
8826 ogds
= ofpbuf_at_assert(reply
, start_ogds
, sizeof *ogds
);
8827 ogds
->type
= gds
->type
;
8828 ogds
->group_id
= htonl(gds
->group_id
);
8829 ogds
->bucket_list_len
= htons(reply
->size
- start_buckets
);
8831 /* Add group properties */
8832 if (gds
->props
.selection_method
[0]) {
8833 ofputil_put_group_prop_ntr_selection_method(version
, &gds
->props
,
8836 ogds
= ofpbuf_at_assert(reply
, start_ogds
, sizeof *ogds
);
8837 ogds
->length
= htons(reply
->size
- start_ogds
);
8839 ofpmp_postappend(replies
, start_ogds
);
8842 /* Appends a group stats reply that contains the data in 'gds' to those already
8843 * present in the list of ofpbufs in 'replies'. 'replies' should have been
8844 * initialized with ofpmp_init(). */
8846 ofputil_append_group_desc_reply(const struct ofputil_group_desc
*gds
,
8847 const struct ovs_list
*buckets
,
8848 struct ovs_list
*replies
)
8850 enum ofp_version version
= ofpmp_version(replies
);
8858 ofputil_append_ofp11_group_desc_reply(gds
, buckets
, replies
, version
);
8863 ofputil_append_ofp15_group_desc_reply(gds
, buckets
, replies
, version
);
8873 ofputil_pull_ofp11_buckets(struct ofpbuf
*msg
, size_t buckets_length
,
8874 enum ofp_version version
, struct ovs_list
*buckets
)
8876 struct ofp11_bucket
*ob
;
8877 uint32_t bucket_id
= 0;
8879 ovs_list_init(buckets
);
8880 while (buckets_length
> 0) {
8881 struct ofputil_bucket
*bucket
;
8882 struct ofpbuf ofpacts
;
8886 ob
= (buckets_length
>= sizeof *ob
8887 ? ofpbuf_try_pull(msg
, sizeof *ob
)
8890 VLOG_WARN_RL(&bad_ofmsg_rl
, "buckets end with %"PRIuSIZE
" leftover bytes",
8892 return OFPERR_OFPGMFC_BAD_BUCKET
;
8895 ob_len
= ntohs(ob
->len
);
8896 if (ob_len
< sizeof *ob
) {
8897 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket length "
8898 "%"PRIuSIZE
" is not valid", ob_len
);
8899 return OFPERR_OFPGMFC_BAD_BUCKET
;
8900 } else if (ob_len
> buckets_length
) {
8901 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket length "
8902 "%"PRIuSIZE
" exceeds remaining buckets data size %"PRIuSIZE
,
8903 ob_len
, buckets_length
);
8904 return OFPERR_OFPGMFC_BAD_BUCKET
;
8906 buckets_length
-= ob_len
;
8908 ofpbuf_init(&ofpacts
, 0);
8909 error
= ofpacts_pull_openflow_actions(msg
, ob_len
- sizeof *ob
,
8910 version
, NULL
, NULL
, &ofpacts
);
8912 ofpbuf_uninit(&ofpacts
);
8913 ofputil_bucket_list_destroy(buckets
);
8917 bucket
= xzalloc(sizeof *bucket
);
8918 bucket
->weight
= ntohs(ob
->weight
);
8919 error
= ofputil_port_from_ofp11(ob
->watch_port
, &bucket
->watch_port
);
8921 ofpbuf_uninit(&ofpacts
);
8922 ofputil_bucket_list_destroy(buckets
);
8923 return OFPERR_OFPGMFC_BAD_WATCH
;
8925 bucket
->watch_group
= ntohl(ob
->watch_group
);
8926 bucket
->bucket_id
= bucket_id
++;
8928 bucket
->ofpacts
= ofpbuf_steal_data(&ofpacts
);
8929 bucket
->ofpacts_len
= ofpacts
.size
;
8930 ovs_list_push_back(buckets
, &bucket
->list_node
);
8937 ofputil_pull_ofp15_buckets(struct ofpbuf
*msg
, size_t buckets_length
,
8938 enum ofp_version version
, uint8_t group_type
,
8939 struct ovs_list
*buckets
)
8941 struct ofp15_bucket
*ob
;
8943 ovs_list_init(buckets
);
8944 while (buckets_length
> 0) {
8945 struct ofputil_bucket
*bucket
= NULL
;
8946 struct ofpbuf ofpacts
;
8947 enum ofperr err
= OFPERR_OFPGMFC_BAD_BUCKET
;
8948 size_t ob_len
, actions_len
, properties_len
;
8949 ovs_be32 watch_port
= ofputil_port_to_ofp11(OFPP_ANY
);
8950 ovs_be32 watch_group
= htonl(OFPG_ANY
);
8951 ovs_be16 weight
= htons(group_type
== OFPGT11_SELECT
? 1 : 0);
8953 ofpbuf_init(&ofpacts
, 0);
8955 ob
= ofpbuf_try_pull(msg
, sizeof *ob
);
8957 VLOG_WARN_RL(&bad_ofmsg_rl
, "buckets end with %"PRIuSIZE
8958 " leftover bytes", buckets_length
);
8962 ob_len
= ntohs(ob
->len
);
8963 actions_len
= ntohs(ob
->action_array_len
);
8965 if (ob_len
< sizeof *ob
) {
8966 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket length "
8967 "%"PRIuSIZE
" is not valid", ob_len
);
8969 } else if (ob_len
> buckets_length
) {
8970 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket length "
8971 "%"PRIuSIZE
" exceeds remaining buckets data size %"
8972 PRIuSIZE
, ob_len
, buckets_length
);
8974 } else if (actions_len
> ob_len
- sizeof *ob
) {
8975 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket actions "
8976 "length %"PRIuSIZE
" exceeds remaining bucket "
8977 "data size %"PRIuSIZE
, actions_len
,
8978 ob_len
- sizeof *ob
);
8981 buckets_length
-= ob_len
;
8983 err
= ofpacts_pull_openflow_actions(msg
, actions_len
, version
,
8984 NULL
, NULL
, &ofpacts
);
8989 properties_len
= ob_len
- sizeof *ob
- actions_len
;
8990 struct ofpbuf properties
= ofpbuf_const_initializer(
8991 ofpbuf_pull(msg
, properties_len
), properties_len
);
8992 while (properties
.size
> 0) {
8993 struct ofpbuf payload
;
8996 err
= ofpprop_pull(&properties
, &payload
, &type
);
9002 case OFPGBPT15_WEIGHT
:
9003 err
= ofpprop_parse_be16(&payload
, &weight
);
9006 case OFPGBPT15_WATCH_PORT
:
9007 err
= ofpprop_parse_be32(&payload
, &watch_port
);
9010 case OFPGBPT15_WATCH_GROUP
:
9011 err
= ofpprop_parse_be32(&payload
, &watch_group
);
9015 err
= OFPPROP_UNKNOWN(false, "group bucket", type
);
9024 bucket
= xzalloc(sizeof *bucket
);
9026 bucket
->weight
= ntohs(weight
);
9027 err
= ofputil_port_from_ofp11(watch_port
, &bucket
->watch_port
);
9029 err
= OFPERR_OFPGMFC_BAD_WATCH
;
9032 bucket
->watch_group
= ntohl(watch_group
);
9033 bucket
->bucket_id
= ntohl(ob
->bucket_id
);
9034 if (bucket
->bucket_id
> OFPG15_BUCKET_MAX
) {
9035 VLOG_WARN_RL(&bad_ofmsg_rl
, "bucket id (%u) is out of range",
9037 err
= OFPERR_OFPGMFC_BAD_BUCKET
;
9041 bucket
->ofpacts
= ofpbuf_steal_data(&ofpacts
);
9042 bucket
->ofpacts_len
= ofpacts
.size
;
9043 ovs_list_push_back(buckets
, &bucket
->list_node
);
9049 ofpbuf_uninit(&ofpacts
);
9050 ofputil_bucket_list_destroy(buckets
);
9054 if (ofputil_bucket_check_duplicate_id(buckets
)) {
9055 VLOG_WARN_RL(&bad_ofmsg_rl
, "Duplicate bucket id");
9056 ofputil_bucket_list_destroy(buckets
);
9057 return OFPERR_OFPGMFC_BAD_BUCKET
;
9064 ofputil_init_group_properties(struct ofputil_group_props
*gp
)
9066 memset(gp
, 0, sizeof *gp
);
9070 ofputil_group_properties_copy(struct ofputil_group_props
*to
,
9071 const struct ofputil_group_props
*from
)
9074 to
->fields
.values
= xmemdup(from
->fields
.values
, from
->fields
.values_size
);
9078 ofputil_group_properties_destroy(struct ofputil_group_props
*gp
)
9080 free(gp
->fields
.values
);
9084 parse_group_prop_ntr_selection_method(struct ofpbuf
*payload
,
9085 enum ofp11_group_type group_type
,
9086 enum ofp15_group_mod_command group_cmd
,
9087 struct ofputil_group_props
*gp
)
9089 struct ntr_group_prop_selection_method
*prop
= payload
->data
;
9090 size_t fields_len
, method_len
;
9093 switch (group_type
) {
9094 case OFPGT11_SELECT
:
9097 case OFPGT11_INDIRECT
:
9099 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method property is "
9100 "only allowed for select groups");
9101 return OFPERR_OFPBPC_BAD_VALUE
;
9106 switch (group_cmd
) {
9108 case OFPGC15_MODIFY
:
9109 case OFPGC15_ADD_OR_MOD
:
9111 case OFPGC15_DELETE
:
9112 case OFPGC15_INSERT_BUCKET
:
9113 case OFPGC15_REMOVE_BUCKET
:
9114 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method property is "
9115 "only allowed for add and delete group modifications");
9116 return OFPERR_OFPBPC_BAD_VALUE
;
9121 if (payload
->size
< sizeof *prop
) {
9122 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method property "
9123 "length %u is not valid", payload
->size
);
9124 return OFPERR_OFPBPC_BAD_LEN
;
9127 method_len
= strnlen(prop
->selection_method
, NTR_MAX_SELECTION_METHOD_LEN
);
9129 if (method_len
== NTR_MAX_SELECTION_METHOD_LEN
) {
9130 OFPPROP_LOG(&bad_ofmsg_rl
, false,
9131 "ntr selection method is not null terminated");
9132 return OFPERR_OFPBPC_BAD_VALUE
;
9135 if (strcmp("hash", prop
->selection_method
)
9136 && strcmp("dp_hash", prop
->selection_method
)) {
9137 OFPPROP_LOG(&bad_ofmsg_rl
, false,
9138 "ntr selection method '%s' is not supported",
9139 prop
->selection_method
);
9140 return OFPERR_OFPBPC_BAD_VALUE
;
9142 /* 'method_len' is now non-zero. */
9144 strcpy(gp
->selection_method
, prop
->selection_method
);
9145 gp
->selection_method_param
= ntohll(prop
->selection_method_param
);
9147 ofpbuf_pull(payload
, sizeof *prop
);
9149 fields_len
= ntohs(prop
->length
) - sizeof *prop
;
9150 if (fields_len
&& strcmp("hash", gp
->selection_method
)) {
9151 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method %s "
9152 "does not support fields", gp
->selection_method
);
9153 return OFPERR_OFPBPC_BAD_VALUE
;
9156 error
= oxm_pull_field_array(payload
->data
, fields_len
,
9159 OFPPROP_LOG(&bad_ofmsg_rl
, false,
9160 "ntr selection method fields are invalid");
9168 parse_ofp15_group_properties(struct ofpbuf
*msg
,
9169 enum ofp11_group_type group_type
,
9170 enum ofp15_group_mod_command group_cmd
,
9171 struct ofputil_group_props
*gp
,
9172 size_t properties_len
)
9174 struct ofpbuf properties
= ofpbuf_const_initializer(
9175 ofpbuf_pull(msg
, properties_len
), properties_len
);
9176 while (properties
.size
> 0) {
9177 struct ofpbuf payload
;
9181 error
= ofpprop_pull(&properties
, &payload
, &type
);
9187 case OFPPROP_EXP(NTR_VENDOR_ID
, NTRT_SELECTION_METHOD
):
9188 case OFPPROP_EXP(NTR_COMPAT_VENDOR_ID
, NTRT_SELECTION_METHOD
):
9189 error
= parse_group_prop_ntr_selection_method(&payload
, group_type
,
9194 error
= OFPPROP_UNKNOWN(false, "group", type
);
9207 ofputil_decode_ofp11_group_desc_reply(struct ofputil_group_desc
*gd
,
9209 enum ofp_version version
)
9211 struct ofp11_group_desc_stats
*ogds
;
9215 ofpraw_pull_assert(msg
);
9222 ogds
= ofpbuf_try_pull(msg
, sizeof *ogds
);
9224 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply has %"PRIu32
" "
9225 "leftover bytes at end", msg
->size
);
9226 return OFPERR_OFPBRC_BAD_LEN
;
9228 gd
->type
= ogds
->type
;
9229 gd
->group_id
= ntohl(ogds
->group_id
);
9231 length
= ntohs(ogds
->length
);
9232 if (length
< sizeof *ogds
|| length
- sizeof *ogds
> msg
->size
) {
9233 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply claims invalid "
9234 "length %"PRIuSIZE
, length
);
9235 return OFPERR_OFPBRC_BAD_LEN
;
9238 return ofputil_pull_ofp11_buckets(msg
, length
- sizeof *ogds
, version
,
9243 ofputil_decode_ofp15_group_desc_reply(struct ofputil_group_desc
*gd
,
9245 enum ofp_version version
)
9247 struct ofp15_group_desc_stats
*ogds
;
9248 uint16_t length
, bucket_list_len
;
9252 ofpraw_pull_assert(msg
);
9259 ogds
= ofpbuf_try_pull(msg
, sizeof *ogds
);
9261 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply has %"PRIu32
" "
9262 "leftover bytes at end", msg
->size
);
9263 return OFPERR_OFPBRC_BAD_LEN
;
9265 gd
->type
= ogds
->type
;
9266 gd
->group_id
= ntohl(ogds
->group_id
);
9268 length
= ntohs(ogds
->length
);
9269 if (length
< sizeof *ogds
|| length
- sizeof *ogds
> msg
->size
) {
9270 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply claims invalid "
9271 "length %u", length
);
9272 return OFPERR_OFPBRC_BAD_LEN
;
9275 bucket_list_len
= ntohs(ogds
->bucket_list_len
);
9276 if (length
< bucket_list_len
+ sizeof *ogds
) {
9277 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply claims invalid "
9278 "bucket list length %u", bucket_list_len
);
9279 return OFPERR_OFPBRC_BAD_LEN
;
9281 error
= ofputil_pull_ofp15_buckets(msg
, bucket_list_len
, version
, gd
->type
,
9287 /* By definition group desc messages don't have a group mod command.
9288 * However, parse_group_prop_ntr_selection_method() checks to make sure
9289 * that the command is OFPGC15_ADD or OFPGC15_DELETE to guard
9290 * against group mod messages with other commands supplying
9291 * a NTR selection method group experimenter property.
9292 * Such properties are valid for group desc replies so
9293 * claim that the group mod command is OFPGC15_ADD to
9294 * satisfy the check in parse_group_prop_ntr_selection_method() */
9295 return parse_ofp15_group_properties(msg
, gd
->type
, OFPGC15_ADD
, &gd
->props
,
9296 length
- sizeof *ogds
- bucket_list_len
);
9299 /* Converts a group description reply in 'msg' into an abstract
9300 * ofputil_group_desc in 'gd'.
9302 * Multiple group description replies can be packed into a single OpenFlow
9303 * message. Calling this function multiple times for a single 'msg' iterates
9304 * through the replies. The caller must initially leave 'msg''s layer pointers
9305 * null and not modify them between calls.
9307 * Returns 0 if successful, EOF if no replies were left in this 'msg',
9308 * otherwise a positive errno value. */
9310 ofputil_decode_group_desc_reply(struct ofputil_group_desc
*gd
,
9311 struct ofpbuf
*msg
, enum ofp_version version
)
9313 ofputil_init_group_properties(&gd
->props
);
9321 return ofputil_decode_ofp11_group_desc_reply(gd
, msg
, version
);
9325 return ofputil_decode_ofp15_group_desc_reply(gd
, msg
, version
);
9334 ofputil_uninit_group_mod(struct ofputil_group_mod
*gm
)
9336 ofputil_bucket_list_destroy(&gm
->buckets
);
9337 ofputil_group_properties_destroy(&gm
->props
);
9340 static struct ofpbuf
*
9341 ofputil_encode_ofp11_group_mod(enum ofp_version ofp_version
,
9342 const struct ofputil_group_mod
*gm
)
9345 struct ofp11_group_mod
*ogm
;
9347 struct ofputil_bucket
*bucket
;
9349 b
= ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD
, ofp_version
, 0);
9350 start_ogm
= b
->size
;
9351 ofpbuf_put_zeros(b
, sizeof *ogm
);
9353 LIST_FOR_EACH (bucket
, list_node
, &gm
->buckets
) {
9354 ofputil_put_ofp11_bucket(bucket
, b
, ofp_version
);
9356 ogm
= ofpbuf_at_assert(b
, start_ogm
, sizeof *ogm
);
9357 ogm
->command
= htons(gm
->command
);
9358 ogm
->type
= gm
->type
;
9359 ogm
->group_id
= htonl(gm
->group_id
);
9364 static struct ofpbuf
*
9365 ofputil_encode_ofp15_group_mod(enum ofp_version ofp_version
,
9366 const struct ofputil_group_mod
*gm
)
9369 struct ofp15_group_mod
*ogm
;
9371 struct ofputil_bucket
*bucket
;
9372 struct id_pool
*bucket_ids
= NULL
;
9374 b
= ofpraw_alloc(OFPRAW_OFPT15_GROUP_MOD
, ofp_version
, 0);
9375 start_ogm
= b
->size
;
9376 ofpbuf_put_zeros(b
, sizeof *ogm
);
9378 LIST_FOR_EACH (bucket
, list_node
, &gm
->buckets
) {
9381 /* Generate a bucket id if none was supplied */
9382 if (bucket
->bucket_id
> OFPG15_BUCKET_MAX
) {
9384 const struct ofputil_bucket
*bkt
;
9386 bucket_ids
= id_pool_create(0, OFPG15_BUCKET_MAX
+ 1);
9388 /* Mark all bucket_ids that are present in gm
9389 * as used in the pool. */
9390 LIST_FOR_EACH_REVERSE (bkt
, list_node
, &gm
->buckets
) {
9391 if (bkt
== bucket
) {
9394 if (bkt
->bucket_id
<= OFPG15_BUCKET_MAX
) {
9395 id_pool_add(bucket_ids
, bkt
->bucket_id
);
9400 if (!id_pool_alloc_id(bucket_ids
, &bucket_id
)) {
9404 bucket_id
= bucket
->bucket_id
;
9407 ofputil_put_ofp15_bucket(bucket
, bucket_id
, gm
->type
, b
, ofp_version
);
9409 ogm
= ofpbuf_at_assert(b
, start_ogm
, sizeof *ogm
);
9410 ogm
->command
= htons(gm
->command
);
9411 ogm
->type
= gm
->type
;
9412 ogm
->group_id
= htonl(gm
->group_id
);
9413 ogm
->command_bucket_id
= htonl(gm
->command_bucket_id
);
9414 ogm
->bucket_array_len
= htons(b
->size
- start_ogm
- sizeof *ogm
);
9416 /* Add group properties */
9417 if (gm
->props
.selection_method
[0]) {
9418 ofputil_put_group_prop_ntr_selection_method(ofp_version
, &gm
->props
, b
);
9421 id_pool_destroy(bucket_ids
);
9426 bad_group_cmd(enum ofp15_group_mod_command cmd
)
9428 const char *opt_version
;
9429 const char *version
;
9430 const char *cmd_str
;
9434 case OFPGC15_MODIFY
:
9435 case OFPGC15_ADD_OR_MOD
:
9436 case OFPGC15_DELETE
:
9441 case OFPGC15_INSERT_BUCKET
:
9442 case OFPGC15_REMOVE_BUCKET
:
9453 cmd_str
= "add-group";
9456 case OFPGC15_MODIFY
:
9457 case OFPGC15_ADD_OR_MOD
:
9458 cmd_str
= "mod-group";
9461 case OFPGC15_DELETE
:
9462 cmd_str
= "del-group";
9465 case OFPGC15_INSERT_BUCKET
:
9466 cmd_str
= "insert-bucket";
9469 case OFPGC15_REMOVE_BUCKET
:
9470 cmd_str
= "remove-bucket";
9477 ovs_fatal(0, "%s needs OpenFlow %s or later (\'-O OpenFlow%s\')",
9478 cmd_str
, version
, opt_version
);
9482 /* Converts abstract group mod 'gm' into a message for OpenFlow version
9483 * 'ofp_version' and returns the message. */
9485 ofputil_encode_group_mod(enum ofp_version ofp_version
,
9486 const struct ofputil_group_mod
*gm
)
9489 switch (ofp_version
) {
9491 bad_group_cmd(gm
->command
);
9497 if (gm
->command
> OFPGC11_DELETE
&& gm
->command
!= OFPGC11_ADD_OR_MOD
) {
9498 bad_group_cmd(gm
->command
);
9500 return ofputil_encode_ofp11_group_mod(ofp_version
, gm
);
9504 return ofputil_encode_ofp15_group_mod(ofp_version
, gm
);
9512 ofputil_pull_ofp11_group_mod(struct ofpbuf
*msg
, enum ofp_version ofp_version
,
9513 struct ofputil_group_mod
*gm
)
9515 const struct ofp11_group_mod
*ogm
;
9518 ogm
= ofpbuf_pull(msg
, sizeof *ogm
);
9519 gm
->command
= ntohs(ogm
->command
);
9520 gm
->type
= ogm
->type
;
9521 gm
->group_id
= ntohl(ogm
->group_id
);
9522 gm
->command_bucket_id
= OFPG15_BUCKET_ALL
;
9524 error
= ofputil_pull_ofp11_buckets(msg
, msg
->size
, ofp_version
,
9527 /* OF1.3.5+ prescribes an error when an OFPGC_DELETE includes buckets. */
9529 && ofp_version
>= OFP13_VERSION
9530 && gm
->command
== OFPGC11_DELETE
9531 && !ovs_list_is_empty(&gm
->buckets
)) {
9532 error
= OFPERR_OFPGMFC_INVALID_GROUP
;
9539 ofputil_pull_ofp15_group_mod(struct ofpbuf
*msg
, enum ofp_version ofp_version
,
9540 struct ofputil_group_mod
*gm
)
9542 const struct ofp15_group_mod
*ogm
;
9543 uint16_t bucket_list_len
;
9544 enum ofperr error
= OFPERR_OFPGMFC_BAD_BUCKET
;
9546 ogm
= ofpbuf_pull(msg
, sizeof *ogm
);
9547 gm
->command
= ntohs(ogm
->command
);
9548 gm
->type
= ogm
->type
;
9549 gm
->group_id
= ntohl(ogm
->group_id
);
9551 gm
->command_bucket_id
= ntohl(ogm
->command_bucket_id
);
9552 switch (gm
->command
) {
9553 case OFPGC15_REMOVE_BUCKET
:
9554 if (gm
->command_bucket_id
== OFPG15_BUCKET_ALL
) {
9558 case OFPGC15_INSERT_BUCKET
:
9559 if (gm
->command_bucket_id
<= OFPG15_BUCKET_MAX
||
9560 gm
->command_bucket_id
== OFPG15_BUCKET_FIRST
9561 || gm
->command_bucket_id
== OFPG15_BUCKET_LAST
) {
9567 case OFPGC11_MODIFY
:
9568 case OFPGC11_ADD_OR_MOD
:
9569 case OFPGC11_DELETE
:
9571 if (gm
->command_bucket_id
== OFPG15_BUCKET_ALL
) {
9577 VLOG_WARN_RL(&bad_ofmsg_rl
,
9578 "group command bucket id (%u) is out of range",
9579 gm
->command_bucket_id
);
9580 return OFPERR_OFPGMFC_BAD_BUCKET
;
9583 bucket_list_len
= ntohs(ogm
->bucket_array_len
);
9584 error
= ofputil_pull_ofp15_buckets(msg
, bucket_list_len
, ofp_version
,
9585 gm
->type
, &gm
->buckets
);
9590 return parse_ofp15_group_properties(msg
, gm
->type
, gm
->command
, &gm
->props
,
9594 /* Converts OpenFlow group mod message 'oh' into an abstract group mod in
9595 * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */
9597 ofputil_decode_group_mod(const struct ofp_header
*oh
,
9598 struct ofputil_group_mod
*gm
)
9600 ofputil_init_group_properties(&gm
->props
);
9602 enum ofp_version ofp_version
= oh
->version
;
9603 struct ofpbuf msg
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
9604 ofpraw_pull_assert(&msg
);
9607 switch (ofp_version
)
9613 err
= ofputil_pull_ofp11_group_mod(&msg
, ofp_version
, gm
);
9618 err
= ofputil_pull_ofp15_group_mod(&msg
, ofp_version
, gm
);
9630 case OFPGT11_INDIRECT
:
9631 if (gm
->command
!= OFPGC11_DELETE
9632 && !ovs_list_is_singleton(&gm
->buckets
) ) {
9633 return OFPERR_OFPGMFC_INVALID_GROUP
;
9637 case OFPGT11_SELECT
:
9641 return OFPERR_OFPGMFC_BAD_TYPE
;
9644 switch (gm
->command
) {
9646 case OFPGC11_MODIFY
:
9647 case OFPGC11_ADD_OR_MOD
:
9648 case OFPGC11_DELETE
:
9649 case OFPGC15_INSERT_BUCKET
:
9651 case OFPGC15_REMOVE_BUCKET
:
9652 if (!ovs_list_is_empty(&gm
->buckets
)) {
9653 return OFPERR_OFPGMFC_BAD_BUCKET
;
9657 return OFPERR_OFPGMFC_BAD_COMMAND
;
9660 struct ofputil_bucket
*bucket
;
9661 LIST_FOR_EACH (bucket
, list_node
, &gm
->buckets
) {
9662 if (bucket
->weight
&& gm
->type
!= OFPGT11_SELECT
) {
9663 return OFPERR_OFPGMFC_INVALID_GROUP
;
9668 case OFPGT11_INDIRECT
:
9669 if (ofputil_bucket_has_liveness(bucket
)) {
9670 return OFPERR_OFPGMFC_WATCH_UNSUPPORTED
;
9673 case OFPGT11_SELECT
:
9676 if (!ofputil_bucket_has_liveness(bucket
)) {
9677 return OFPERR_OFPGMFC_INVALID_GROUP
;
9681 /* Returning BAD TYPE to be consistent
9682 * though gm->type has been checked already. */
9683 return OFPERR_OFPGMFC_BAD_TYPE
;
9690 /* Destroys 'bms'. */
9692 ofputil_free_bundle_msgs(struct ofputil_bundle_msg
*bms
, size_t n_bms
)
9694 for (size_t i
= 0; i
< n_bms
; i
++) {
9695 switch ((int)bms
[i
].type
) {
9696 case OFPTYPE_FLOW_MOD
:
9697 free(CONST_CAST(struct ofpact
*, bms
[i
].fm
.ofpacts
));
9699 case OFPTYPE_GROUP_MOD
:
9700 ofputil_uninit_group_mod(&bms
[i
].gm
);
9702 case OFPTYPE_PACKET_OUT
:
9703 free(bms
[i
].po
.ofpacts
);
9704 free(CONST_CAST(void *, bms
[i
].po
.packet
));
9714 ofputil_encode_bundle_msgs(const struct ofputil_bundle_msg
*bms
,
9715 size_t n_bms
, struct ovs_list
*requests
,
9716 enum ofputil_protocol protocol
)
9718 enum ofp_version version
= ofputil_protocol_to_ofp_version(protocol
);
9720 for (size_t i
= 0; i
< n_bms
; i
++) {
9721 struct ofpbuf
*request
= NULL
;
9723 switch ((int)bms
[i
].type
) {
9724 case OFPTYPE_FLOW_MOD
:
9725 request
= ofputil_encode_flow_mod(&bms
[i
].fm
, protocol
);
9727 case OFPTYPE_GROUP_MOD
:
9728 request
= ofputil_encode_group_mod(version
, &bms
[i
].gm
);
9730 case OFPTYPE_PACKET_OUT
:
9731 request
= ofputil_encode_packet_out(&bms
[i
].po
, protocol
);
9737 ovs_list_push_back(requests
, &request
->list_node
);
9742 /* Parse a queue status request message into 'oqsr'.
9743 * Returns 0 if successful, otherwise an OFPERR_* number. */
9745 ofputil_decode_queue_stats_request(const struct ofp_header
*request
,
9746 struct ofputil_queue_stats_request
*oqsr
)
9748 switch ((enum ofp_version
)request
->version
) {
9754 case OFP11_VERSION
: {
9755 const struct ofp11_queue_stats_request
*qsr11
= ofpmsg_body(request
);
9756 oqsr
->queue_id
= ntohl(qsr11
->queue_id
);
9757 return ofputil_port_from_ofp11(qsr11
->port_no
, &oqsr
->port_no
);
9760 case OFP10_VERSION
: {
9761 const struct ofp10_queue_stats_request
*qsr10
= ofpmsg_body(request
);
9762 oqsr
->queue_id
= ntohl(qsr10
->queue_id
);
9763 oqsr
->port_no
= u16_to_ofp(ntohs(qsr10
->port_no
));
9764 /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
9765 if (oqsr
->port_no
== OFPP_ALL
) {
9766 oqsr
->port_no
= OFPP_ANY
;
9776 /* Encode a queue stats request for 'oqsr', the encoded message
9777 * will be for OpenFlow version 'ofp_version'. Returns message
9778 * as a struct ofpbuf. Returns encoded message on success, NULL on error. */
9780 ofputil_encode_queue_stats_request(enum ofp_version ofp_version
,
9781 const struct ofputil_queue_stats_request
*oqsr
)
9783 struct ofpbuf
*request
;
9785 switch (ofp_version
) {
9791 case OFP16_VERSION
: {
9792 struct ofp11_queue_stats_request
*req
;
9793 request
= ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST
, ofp_version
, 0);
9794 req
= ofpbuf_put_zeros(request
, sizeof *req
);
9795 req
->port_no
= ofputil_port_to_ofp11(oqsr
->port_no
);
9796 req
->queue_id
= htonl(oqsr
->queue_id
);
9799 case OFP10_VERSION
: {
9800 struct ofp10_queue_stats_request
*req
;
9801 request
= ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST
, ofp_version
, 0);
9802 req
= ofpbuf_put_zeros(request
, sizeof *req
);
9803 /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
9804 req
->port_no
= htons(ofp_to_u16(oqsr
->port_no
== OFPP_ANY
9805 ? OFPP_ALL
: oqsr
->port_no
));
9806 req
->queue_id
= htonl(oqsr
->queue_id
);
9816 /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
9819 ofputil_count_queue_stats(const struct ofp_header
*oh
)
9821 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
9822 ofpraw_pull_assert(&b
);
9824 for (size_t n
= 0; ; n
++) {
9825 struct ofputil_queue_stats qs
;
9826 if (ofputil_decode_queue_stats(&qs
, &b
)) {
9833 ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats
*oqs
,
9834 const struct ofp10_queue_stats
*qs10
)
9836 oqs
->port_no
= u16_to_ofp(ntohs(qs10
->port_no
));
9837 oqs
->queue_id
= ntohl(qs10
->queue_id
);
9838 oqs
->tx_bytes
= ntohll(get_32aligned_be64(&qs10
->tx_bytes
));
9839 oqs
->tx_packets
= ntohll(get_32aligned_be64(&qs10
->tx_packets
));
9840 oqs
->tx_errors
= ntohll(get_32aligned_be64(&qs10
->tx_errors
));
9841 oqs
->duration_sec
= oqs
->duration_nsec
= UINT32_MAX
;
9847 ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats
*oqs
,
9848 const struct ofp11_queue_stats
*qs11
)
9852 error
= ofputil_port_from_ofp11(qs11
->port_no
, &oqs
->port_no
);
9857 oqs
->queue_id
= ntohl(qs11
->queue_id
);
9858 oqs
->tx_bytes
= ntohll(qs11
->tx_bytes
);
9859 oqs
->tx_packets
= ntohll(qs11
->tx_packets
);
9860 oqs
->tx_errors
= ntohll(qs11
->tx_errors
);
9861 oqs
->duration_sec
= oqs
->duration_nsec
= UINT32_MAX
;
9867 ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats
*oqs
,
9868 const struct ofp13_queue_stats
*qs13
)
9870 enum ofperr error
= ofputil_queue_stats_from_ofp11(oqs
, &qs13
->qs
);
9872 oqs
->duration_sec
= ntohl(qs13
->duration_sec
);
9873 oqs
->duration_nsec
= ntohl(qs13
->duration_nsec
);
9880 ofputil_pull_ofp14_queue_stats(struct ofputil_queue_stats
*oqs
,
9883 const struct ofp14_queue_stats
*qs14
;
9886 qs14
= ofpbuf_try_pull(msg
, sizeof *qs14
);
9888 return OFPERR_OFPBRC_BAD_LEN
;
9891 len
= ntohs(qs14
->length
);
9892 if (len
< sizeof *qs14
|| len
- sizeof *qs14
> msg
->size
) {
9893 return OFPERR_OFPBRC_BAD_LEN
;
9895 ofpbuf_pull(msg
, len
- sizeof *qs14
);
9897 /* No properties yet defined, so ignore them for now. */
9899 return ofputil_queue_stats_from_ofp13(oqs
, &qs14
->qs
);
9902 /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
9903 * ofputil_queue_stats in 'qs'.
9905 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
9906 * message. Calling this function multiple times for a single 'msg' iterates
9907 * through the replies. The caller must initially leave 'msg''s layer pointers
9908 * null and not modify them between calls.
9910 * Returns 0 if successful, EOF if no replies were left in this 'msg',
9911 * otherwise a positive errno value. */
9913 ofputil_decode_queue_stats(struct ofputil_queue_stats
*qs
, struct ofpbuf
*msg
)
9918 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
9919 : ofpraw_pull(&raw
, msg
));
9926 } else if (raw
== OFPRAW_OFPST14_QUEUE_REPLY
) {
9927 return ofputil_pull_ofp14_queue_stats(qs
, msg
);
9928 } else if (raw
== OFPRAW_OFPST13_QUEUE_REPLY
) {
9929 const struct ofp13_queue_stats
*qs13
;
9931 qs13
= ofpbuf_try_pull(msg
, sizeof *qs13
);
9935 return ofputil_queue_stats_from_ofp13(qs
, qs13
);
9936 } else if (raw
== OFPRAW_OFPST11_QUEUE_REPLY
) {
9937 const struct ofp11_queue_stats
*qs11
;
9939 qs11
= ofpbuf_try_pull(msg
, sizeof *qs11
);
9943 return ofputil_queue_stats_from_ofp11(qs
, qs11
);
9944 } else if (raw
== OFPRAW_OFPST10_QUEUE_REPLY
) {
9945 const struct ofp10_queue_stats
*qs10
;
9947 qs10
= ofpbuf_try_pull(msg
, sizeof *qs10
);
9951 return ofputil_queue_stats_from_ofp10(qs
, qs10
);
9957 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_QUEUE reply has %"PRIu32
" leftover "
9958 "bytes at end", msg
->size
);
9959 return OFPERR_OFPBRC_BAD_LEN
;
9963 ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats
*oqs
,
9964 struct ofp10_queue_stats
*qs10
)
9966 qs10
->port_no
= htons(ofp_to_u16(oqs
->port_no
));
9967 memset(qs10
->pad
, 0, sizeof qs10
->pad
);
9968 qs10
->queue_id
= htonl(oqs
->queue_id
);
9969 put_32aligned_be64(&qs10
->tx_bytes
, htonll(oqs
->tx_bytes
));
9970 put_32aligned_be64(&qs10
->tx_packets
, htonll(oqs
->tx_packets
));
9971 put_32aligned_be64(&qs10
->tx_errors
, htonll(oqs
->tx_errors
));
9975 ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats
*oqs
,
9976 struct ofp11_queue_stats
*qs11
)
9978 qs11
->port_no
= ofputil_port_to_ofp11(oqs
->port_no
);
9979 qs11
->queue_id
= htonl(oqs
->queue_id
);
9980 qs11
->tx_bytes
= htonll(oqs
->tx_bytes
);
9981 qs11
->tx_packets
= htonll(oqs
->tx_packets
);
9982 qs11
->tx_errors
= htonll(oqs
->tx_errors
);
9986 ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats
*oqs
,
9987 struct ofp13_queue_stats
*qs13
)
9989 ofputil_queue_stats_to_ofp11(oqs
, &qs13
->qs
);
9990 if (oqs
->duration_sec
!= UINT32_MAX
) {
9991 qs13
->duration_sec
= htonl(oqs
->duration_sec
);
9992 qs13
->duration_nsec
= htonl(oqs
->duration_nsec
);
9994 qs13
->duration_sec
= OVS_BE32_MAX
;
9995 qs13
->duration_nsec
= OVS_BE32_MAX
;
10000 ofputil_queue_stats_to_ofp14(const struct ofputil_queue_stats
*oqs
,
10001 struct ofp14_queue_stats
*qs14
)
10003 qs14
->length
= htons(sizeof *qs14
);
10004 memset(qs14
->pad
, 0, sizeof qs14
->pad
);
10005 ofputil_queue_stats_to_ofp13(oqs
, &qs14
->qs
);
10009 /* Encode a queue stat for 'oqs' and append it to 'replies'. */
10011 ofputil_append_queue_stat(struct ovs_list
*replies
,
10012 const struct ofputil_queue_stats
*oqs
)
10014 switch (ofpmp_version(replies
)) {
10015 case OFP13_VERSION
: {
10016 struct ofp13_queue_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
10017 ofputil_queue_stats_to_ofp13(oqs
, reply
);
10021 case OFP12_VERSION
:
10022 case OFP11_VERSION
: {
10023 struct ofp11_queue_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
10024 ofputil_queue_stats_to_ofp11(oqs
, reply
);
10028 case OFP10_VERSION
: {
10029 struct ofp10_queue_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
10030 ofputil_queue_stats_to_ofp10(oqs
, reply
);
10034 case OFP14_VERSION
:
10035 case OFP15_VERSION
:
10036 case OFP16_VERSION
: {
10037 struct ofp14_queue_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
10038 ofputil_queue_stats_to_ofp14(oqs
, reply
);
10048 ofputil_decode_bundle_ctrl(const struct ofp_header
*oh
,
10049 struct ofputil_bundle_ctrl_msg
*msg
)
10051 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
10052 enum ofpraw raw
= ofpraw_pull_assert(&b
);
10053 ovs_assert(raw
== OFPRAW_OFPT14_BUNDLE_CONTROL
10054 || raw
== OFPRAW_ONFT13_BUNDLE_CONTROL
);
10056 const struct ofp14_bundle_ctrl_msg
*m
= b
.msg
;
10057 msg
->bundle_id
= ntohl(m
->bundle_id
);
10058 msg
->type
= ntohs(m
->type
);
10059 msg
->flags
= ntohs(m
->flags
);
10065 ofputil_encode_bundle_ctrl_request(enum ofp_version ofp_version
,
10066 struct ofputil_bundle_ctrl_msg
*bc
)
10068 struct ofpbuf
*request
;
10069 struct ofp14_bundle_ctrl_msg
*m
;
10071 switch (ofp_version
) {
10072 case OFP10_VERSION
:
10073 case OFP11_VERSION
:
10074 case OFP12_VERSION
:
10075 ovs_fatal(0, "bundles need OpenFlow 1.3 or later "
10076 "(\'-O OpenFlow14\')");
10077 case OFP13_VERSION
:
10078 case OFP14_VERSION
:
10079 case OFP15_VERSION
:
10080 case OFP16_VERSION
:
10081 request
= ofpraw_alloc(ofp_version
== OFP13_VERSION
10082 ? OFPRAW_ONFT13_BUNDLE_CONTROL
10083 : OFPRAW_OFPT14_BUNDLE_CONTROL
, ofp_version
, 0);
10084 m
= ofpbuf_put_zeros(request
, sizeof *m
);
10086 m
->bundle_id
= htonl(bc
->bundle_id
);
10087 m
->type
= htons(bc
->type
);
10088 m
->flags
= htons(bc
->flags
);
10098 ofputil_encode_bundle_ctrl_reply(const struct ofp_header
*oh
,
10099 struct ofputil_bundle_ctrl_msg
*msg
)
10101 struct ofpbuf
*buf
;
10102 struct ofp14_bundle_ctrl_msg
*m
;
10104 buf
= ofpraw_alloc_reply(oh
->version
== OFP13_VERSION
10105 ? OFPRAW_ONFT13_BUNDLE_CONTROL
10106 : OFPRAW_OFPT14_BUNDLE_CONTROL
, oh
, 0);
10107 m
= ofpbuf_put_zeros(buf
, sizeof *m
);
10109 m
->bundle_id
= htonl(msg
->bundle_id
);
10110 m
->type
= htons(msg
->type
);
10111 m
->flags
= htons(msg
->flags
);
10116 /* Return true for bundlable state change requests, false for other messages.
10119 ofputil_is_bundlable(enum ofptype type
)
10122 /* Minimum required by OpenFlow 1.4. */
10123 case OFPTYPE_PORT_MOD
:
10124 case OFPTYPE_FLOW_MOD
:
10125 /* Other supported types. */
10126 case OFPTYPE_GROUP_MOD
:
10127 case OFPTYPE_PACKET_OUT
:
10130 /* Nice to have later. */
10131 case OFPTYPE_FLOW_MOD_TABLE_ID
:
10132 case OFPTYPE_TABLE_MOD
:
10133 case OFPTYPE_METER_MOD
:
10134 case OFPTYPE_NXT_TLV_TABLE_MOD
:
10136 /* Not to be bundlable. */
10137 case OFPTYPE_ECHO_REQUEST
:
10138 case OFPTYPE_FEATURES_REQUEST
:
10139 case OFPTYPE_GET_CONFIG_REQUEST
:
10140 case OFPTYPE_SET_CONFIG
:
10141 case OFPTYPE_BARRIER_REQUEST
:
10142 case OFPTYPE_ROLE_REQUEST
:
10143 case OFPTYPE_ECHO_REPLY
:
10144 case OFPTYPE_SET_FLOW_FORMAT
:
10145 case OFPTYPE_SET_PACKET_IN_FORMAT
:
10146 case OFPTYPE_SET_CONTROLLER_ID
:
10147 case OFPTYPE_FLOW_AGE
:
10148 case OFPTYPE_FLOW_MONITOR_CANCEL
:
10149 case OFPTYPE_SET_ASYNC_CONFIG
:
10150 case OFPTYPE_GET_ASYNC_REQUEST
:
10151 case OFPTYPE_DESC_STATS_REQUEST
:
10152 case OFPTYPE_FLOW_STATS_REQUEST
:
10153 case OFPTYPE_AGGREGATE_STATS_REQUEST
:
10154 case OFPTYPE_TABLE_STATS_REQUEST
:
10155 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST
:
10156 case OFPTYPE_TABLE_DESC_REQUEST
:
10157 case OFPTYPE_PORT_STATS_REQUEST
:
10158 case OFPTYPE_QUEUE_STATS_REQUEST
:
10159 case OFPTYPE_PORT_DESC_STATS_REQUEST
:
10160 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST
:
10161 case OFPTYPE_METER_STATS_REQUEST
:
10162 case OFPTYPE_METER_CONFIG_STATS_REQUEST
:
10163 case OFPTYPE_METER_FEATURES_STATS_REQUEST
:
10164 case OFPTYPE_GROUP_STATS_REQUEST
:
10165 case OFPTYPE_GROUP_DESC_STATS_REQUEST
:
10166 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST
:
10167 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST
:
10168 case OFPTYPE_BUNDLE_CONTROL
:
10169 case OFPTYPE_BUNDLE_ADD_MESSAGE
:
10170 case OFPTYPE_HELLO
:
10171 case OFPTYPE_ERROR
:
10172 case OFPTYPE_FEATURES_REPLY
:
10173 case OFPTYPE_GET_CONFIG_REPLY
:
10174 case OFPTYPE_PACKET_IN
:
10175 case OFPTYPE_FLOW_REMOVED
:
10176 case OFPTYPE_PORT_STATUS
:
10177 case OFPTYPE_BARRIER_REPLY
:
10178 case OFPTYPE_QUEUE_GET_CONFIG_REPLY
:
10179 case OFPTYPE_DESC_STATS_REPLY
:
10180 case OFPTYPE_FLOW_STATS_REPLY
:
10181 case OFPTYPE_QUEUE_STATS_REPLY
:
10182 case OFPTYPE_PORT_STATS_REPLY
:
10183 case OFPTYPE_TABLE_STATS_REPLY
:
10184 case OFPTYPE_AGGREGATE_STATS_REPLY
:
10185 case OFPTYPE_PORT_DESC_STATS_REPLY
:
10186 case OFPTYPE_ROLE_REPLY
:
10187 case OFPTYPE_FLOW_MONITOR_PAUSED
:
10188 case OFPTYPE_FLOW_MONITOR_RESUMED
:
10189 case OFPTYPE_FLOW_MONITOR_STATS_REPLY
:
10190 case OFPTYPE_GET_ASYNC_REPLY
:
10191 case OFPTYPE_GROUP_STATS_REPLY
:
10192 case OFPTYPE_GROUP_DESC_STATS_REPLY
:
10193 case OFPTYPE_GROUP_FEATURES_STATS_REPLY
:
10194 case OFPTYPE_METER_STATS_REPLY
:
10195 case OFPTYPE_METER_CONFIG_STATS_REPLY
:
10196 case OFPTYPE_METER_FEATURES_STATS_REPLY
:
10197 case OFPTYPE_TABLE_FEATURES_STATS_REPLY
:
10198 case OFPTYPE_TABLE_DESC_REPLY
:
10199 case OFPTYPE_ROLE_STATUS
:
10200 case OFPTYPE_REQUESTFORWARD
:
10201 case OFPTYPE_TABLE_STATUS
:
10202 case OFPTYPE_NXT_TLV_TABLE_REQUEST
:
10203 case OFPTYPE_NXT_TLV_TABLE_REPLY
:
10204 case OFPTYPE_NXT_RESUME
:
10205 case OFPTYPE_IPFIX_BRIDGE_STATS_REQUEST
:
10206 case OFPTYPE_IPFIX_BRIDGE_STATS_REPLY
:
10207 case OFPTYPE_IPFIX_FLOW_STATS_REQUEST
:
10208 case OFPTYPE_IPFIX_FLOW_STATS_REPLY
:
10209 case OFPTYPE_CT_FLUSH_ZONE
:
10217 ofputil_decode_bundle_add(const struct ofp_header
*oh
,
10218 struct ofputil_bundle_add_msg
*msg
,
10219 enum ofptype
*typep
)
10221 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
10222 enum ofpraw raw
= ofpraw_pull_assert(&b
);
10223 ovs_assert(raw
== OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE
10224 || raw
== OFPRAW_ONFT13_BUNDLE_ADD_MESSAGE
);
10226 const struct ofp14_bundle_ctrl_msg
*m
= ofpbuf_pull(&b
, sizeof *m
);
10227 msg
->bundle_id
= ntohl(m
->bundle_id
);
10228 msg
->flags
= ntohs(m
->flags
);
10231 if (msg
->msg
->version
!= oh
->version
) {
10232 return OFPERR_OFPBFC_BAD_VERSION
;
10234 size_t inner_len
= ntohs(msg
->msg
->length
);
10235 if (inner_len
< sizeof(struct ofp_header
) || inner_len
> b
.size
) {
10236 return OFPERR_OFPBFC_MSG_BAD_LEN
;
10238 if (msg
->msg
->xid
!= oh
->xid
) {
10239 return OFPERR_OFPBFC_MSG_BAD_XID
;
10242 /* Reject unbundlable messages. */
10244 enum ofperr error
= ofptype_decode(&type
, msg
->msg
);
10246 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPT14_BUNDLE_ADD_MESSAGE contained "
10247 "message is unparsable (%s)", ofperr_get_name(error
));
10248 return OFPERR_OFPBFC_MSG_UNSUP
; /* 'error' would be confusing. */
10251 if (!ofputil_is_bundlable(type
)) {
10252 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s message not allowed inside "
10253 "OFPT14_BUNDLE_ADD_MESSAGE", ofptype_get_name(type
));
10254 return OFPERR_OFPBFC_MSG_UNSUP
;
10264 ofputil_encode_bundle_add(enum ofp_version ofp_version
,
10265 struct ofputil_bundle_add_msg
*msg
)
10267 struct ofpbuf
*request
;
10268 struct ofp14_bundle_ctrl_msg
*m
;
10270 /* Must use the same xid as the embedded message. */
10271 request
= ofpraw_alloc_xid(ofp_version
== OFP13_VERSION
10272 ? OFPRAW_ONFT13_BUNDLE_ADD_MESSAGE
10273 : OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE
, ofp_version
,
10274 msg
->msg
->xid
, ntohs(msg
->msg
->length
));
10275 m
= ofpbuf_put_zeros(request
, sizeof *m
);
10277 m
->bundle_id
= htonl(msg
->bundle_id
);
10278 m
->flags
= htons(msg
->flags
);
10279 ofpbuf_put(request
, msg
->msg
, ntohs(msg
->msg
->length
));
10281 ofpmsg_update_length(request
);
10286 encode_tlv_table_mappings(struct ofpbuf
*b
, struct ovs_list
*mappings
)
10288 struct ofputil_tlv_map
*map
;
10290 LIST_FOR_EACH (map
, list_node
, mappings
) {
10291 struct nx_tlv_map
*nx_map
;
10293 nx_map
= ofpbuf_put_zeros(b
, sizeof *nx_map
);
10294 nx_map
->option_class
= htons(map
->option_class
);
10295 nx_map
->option_type
= map
->option_type
;
10296 nx_map
->option_len
= map
->option_len
;
10297 nx_map
->index
= htons(map
->index
);
10302 ofputil_encode_tlv_table_mod(enum ofp_version ofp_version
,
10303 struct ofputil_tlv_table_mod
*ttm
)
10306 struct nx_tlv_table_mod
*nx_ttm
;
10308 b
= ofpraw_alloc(OFPRAW_NXT_TLV_TABLE_MOD
, ofp_version
, 0);
10309 nx_ttm
= ofpbuf_put_zeros(b
, sizeof *nx_ttm
);
10310 nx_ttm
->command
= htons(ttm
->command
);
10311 encode_tlv_table_mappings(b
, &ttm
->mappings
);
10317 decode_tlv_table_mappings(struct ofpbuf
*msg
, unsigned int max_fields
,
10318 struct ovs_list
*mappings
)
10320 ovs_list_init(mappings
);
10322 while (msg
->size
) {
10323 struct nx_tlv_map
*nx_map
;
10324 struct ofputil_tlv_map
*map
;
10326 nx_map
= ofpbuf_pull(msg
, sizeof *nx_map
);
10327 map
= xmalloc(sizeof *map
);
10328 ovs_list_push_back(mappings
, &map
->list_node
);
10330 map
->option_class
= ntohs(nx_map
->option_class
);
10331 map
->option_type
= nx_map
->option_type
;
10333 map
->option_len
= nx_map
->option_len
;
10334 if (map
->option_len
% 4 || map
->option_len
> TLV_MAX_OPT_SIZE
) {
10335 VLOG_WARN_RL(&bad_ofmsg_rl
,
10336 "tlv table option length (%u) is not a valid option size",
10338 ofputil_uninit_tlv_table(mappings
);
10339 return OFPERR_NXTTMFC_BAD_OPT_LEN
;
10342 map
->index
= ntohs(nx_map
->index
);
10343 if (map
->index
>= max_fields
) {
10344 VLOG_WARN_RL(&bad_ofmsg_rl
,
10345 "tlv table field index (%u) is too large (max %u)",
10346 map
->index
, max_fields
- 1);
10347 ofputil_uninit_tlv_table(mappings
);
10348 return OFPERR_NXTTMFC_BAD_FIELD_IDX
;
10356 ofputil_decode_tlv_table_mod(const struct ofp_header
*oh
,
10357 struct ofputil_tlv_table_mod
*ttm
)
10359 struct ofpbuf msg
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
10360 ofpraw_pull_assert(&msg
);
10362 struct nx_tlv_table_mod
*nx_ttm
= ofpbuf_pull(&msg
, sizeof *nx_ttm
);
10363 ttm
->command
= ntohs(nx_ttm
->command
);
10364 if (ttm
->command
> NXTTMC_CLEAR
) {
10365 VLOG_WARN_RL(&bad_ofmsg_rl
,
10366 "tlv table mod command (%u) is out of range",
10368 return OFPERR_NXTTMFC_BAD_COMMAND
;
10371 return decode_tlv_table_mappings(&msg
, TUN_METADATA_NUM_OPTS
,
10376 ofputil_encode_tlv_table_reply(const struct ofp_header
*oh
,
10377 struct ofputil_tlv_table_reply
*ttr
)
10380 struct nx_tlv_table_reply
*nx_ttr
;
10382 b
= ofpraw_alloc_reply(OFPRAW_NXT_TLV_TABLE_REPLY
, oh
, 0);
10383 nx_ttr
= ofpbuf_put_zeros(b
, sizeof *nx_ttr
);
10384 nx_ttr
->max_option_space
= htonl(ttr
->max_option_space
);
10385 nx_ttr
->max_fields
= htons(ttr
->max_fields
);
10387 encode_tlv_table_mappings(b
, &ttr
->mappings
);
10392 /* Decodes the NXT_TLV_TABLE_REPLY message in 'oh' into '*ttr'. Returns 0
10393 * if successful, otherwise an ofperr.
10395 * The decoder verifies that the indexes in 'ttr->mappings' are less than
10396 * 'ttr->max_fields', but the caller must ensure, if necessary, that they are
10397 * less than TUN_METADATA_NUM_OPTS. */
10399 ofputil_decode_tlv_table_reply(const struct ofp_header
*oh
,
10400 struct ofputil_tlv_table_reply
*ttr
)
10402 struct ofpbuf msg
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
10403 ofpraw_pull_assert(&msg
);
10405 struct nx_tlv_table_reply
*nx_ttr
= ofpbuf_pull(&msg
, sizeof *nx_ttr
);
10406 ttr
->max_option_space
= ntohl(nx_ttr
->max_option_space
);
10407 ttr
->max_fields
= ntohs(nx_ttr
->max_fields
);
10409 return decode_tlv_table_mappings(&msg
, ttr
->max_fields
, &ttr
->mappings
);
10413 ofputil_uninit_tlv_table(struct ovs_list
*mappings
)
10415 struct ofputil_tlv_map
*map
;
10417 LIST_FOR_EACH_POP (map
, list_node
, mappings
) {
10423 ofputil_async_msg_type_to_string(enum ofputil_async_msg_type type
)
10426 case OAM_PACKET_IN
: return "PACKET_IN";
10427 case OAM_PORT_STATUS
: return "PORT_STATUS";
10428 case OAM_FLOW_REMOVED
: return "FLOW_REMOVED";
10429 case OAM_ROLE_STATUS
: return "ROLE_STATUS";
10430 case OAM_TABLE_STATUS
: return "TABLE_STATUS";
10431 case OAM_REQUESTFORWARD
: return "REQUESTFORWARD";
10439 struct ofp14_async_prop
{
10440 uint64_t prop_type
;
10441 enum ofputil_async_msg_type oam
;
10443 uint32_t allowed10
, allowed14
;
10446 #define AP_PAIR(SLAVE_PROP_TYPE, OAM, A10, A14) \
10447 { SLAVE_PROP_TYPE, OAM, false, A10, (A14) ? (A14) : (A10) }, \
10448 { (SLAVE_PROP_TYPE + 1), OAM, true, A10, (A14) ? (A14) : (A10) }
10450 static const struct ofp14_async_prop async_props
[] = {
10451 AP_PAIR( 0, OAM_PACKET_IN
, OFPR10_BITS
, OFPR14_BITS
),
10452 AP_PAIR( 2, OAM_PORT_STATUS
, (1 << OFPPR_N_REASONS
) - 1, 0),
10453 AP_PAIR( 4, OAM_FLOW_REMOVED
, (1 << OVS_OFPRR_NONE
) - 1, 0),
10454 AP_PAIR( 6, OAM_ROLE_STATUS
, (1 << OFPCRR_N_REASONS
) - 1, 0),
10455 AP_PAIR( 8, OAM_TABLE_STATUS
, OFPTR_BITS
, 0),
10456 AP_PAIR(10, OAM_REQUESTFORWARD
, (1 << OFPRFR_N_REASONS
) - 1, 0),
10459 #define FOR_EACH_ASYNC_PROP(VAR) \
10460 for (const struct ofp14_async_prop *VAR = async_props; \
10461 VAR < &async_props[ARRAY_SIZE(async_props)]; VAR++)
10463 static const struct ofp14_async_prop
*
10464 get_ofp14_async_config_prop_by_prop_type(uint64_t prop_type
)
10466 FOR_EACH_ASYNC_PROP (ap
) {
10467 if (prop_type
== ap
->prop_type
) {
10474 static const struct ofp14_async_prop
*
10475 get_ofp14_async_config_prop_by_oam(enum ofputil_async_msg_type oam
,
10478 FOR_EACH_ASYNC_PROP (ap
) {
10479 if (ap
->oam
== oam
&& ap
->master
== master
) {
10487 ofp14_async_prop_allowed(const struct ofp14_async_prop
*prop
,
10488 enum ofp_version version
)
10490 return version
>= OFP14_VERSION
? prop
->allowed14
: prop
->allowed10
;
10494 encode_async_mask(const struct ofputil_async_cfg
*src
,
10495 const struct ofp14_async_prop
*ap
,
10496 enum ofp_version version
)
10498 uint32_t mask
= ap
->master
? src
->master
[ap
->oam
] : src
->slave
[ap
->oam
];
10499 return htonl(mask
& ofp14_async_prop_allowed(ap
, version
));
10503 decode_async_mask(ovs_be32 src
,
10504 const struct ofp14_async_prop
*ap
, enum ofp_version version
,
10505 bool loose
, struct ofputil_async_cfg
*dst
)
10507 uint32_t mask
= ntohl(src
);
10508 uint32_t allowed
= ofp14_async_prop_allowed(ap
, version
);
10509 if (mask
& ~allowed
) {
10510 OFPPROP_LOG(&bad_ofmsg_rl
, loose
,
10511 "bad value %#x for %s (allowed mask %#x)",
10512 mask
, ofputil_async_msg_type_to_string(ap
->oam
),
10516 return OFPERR_OFPACFC_INVALID
;
10520 if (ap
->oam
== OAM_PACKET_IN
) {
10521 if (mask
& (1u << OFPR_NO_MATCH
)) {
10522 mask
|= 1u << OFPR_EXPLICIT_MISS
;
10523 if (version
< OFP13_VERSION
) {
10524 mask
|= 1u << OFPR_IMPLICIT_MISS
;
10529 uint32_t *array
= ap
->master
? dst
->master
: dst
->slave
;
10530 array
[ap
->oam
] = mask
;
10535 parse_async_tlv(const struct ofpbuf
*property
,
10536 const struct ofp14_async_prop
*ap
,
10537 struct ofputil_async_cfg
*ac
,
10538 enum ofp_version version
, bool loose
)
10543 error
= ofpprop_parse_be32(property
, &mask
);
10548 if (ofpprop_is_experimenter(ap
->prop_type
)) {
10549 /* For experimenter properties, whether a property is for the master or
10550 * slave role is indicated by both 'type' and 'exp_type' in struct
10551 * ofp_prop_experimenter. Check that these are consistent. */
10552 const struct ofp_prop_experimenter
*ope
= property
->data
;
10553 bool should_be_master
= ope
->type
== htons(0xffff);
10554 if (should_be_master
!= ap
->master
) {
10555 VLOG_WARN_RL(&bad_ofmsg_rl
, "async property type %#"PRIx16
" "
10556 "indicates %s role but exp_type %"PRIu32
" indicates "
10559 should_be_master
? "master" : "slave",
10560 ntohl(ope
->exp_type
),
10561 ap
->master
? "master" : "slave");
10562 return OFPERR_OFPBPC_BAD_EXP_TYPE
;
10566 return decode_async_mask(mask
, ap
, version
, loose
, ac
);
10570 decode_legacy_async_masks(const ovs_be32 masks
[2],
10571 enum ofputil_async_msg_type oam
,
10572 enum ofp_version version
,
10573 struct ofputil_async_cfg
*dst
)
10575 for (int i
= 0; i
< 2; i
++) {
10576 bool master
= i
== 0;
10577 const struct ofp14_async_prop
*ap
10578 = get_ofp14_async_config_prop_by_oam(oam
, master
);
10579 decode_async_mask(masks
[i
], ap
, version
, true, dst
);
10583 /* Decodes the OpenFlow "set async config" request and "get async config
10584 * reply" message in '*oh' into an abstract form in 'ac'.
10586 * Some versions of the "set async config" request change only some of the
10587 * settings and leave the others alone. This function uses 'basis' as the
10588 * initial state for decoding these. Other versions of the request change all
10589 * the settings; this function ignores 'basis' when decoding these.
10591 * If 'loose' is true, this function ignores properties and values that it does
10592 * not understand, as a controller would want to do when interpreting
10593 * capabilities provided by a switch. If 'loose' is false, this function
10594 * treats unknown properties and values as an error, as a switch would want to
10595 * do when interpreting a configuration request made by a controller.
10597 * Returns 0 if successful, otherwise an OFPERR_* value.
10599 * Returns error code OFPERR_OFPACFC_INVALID if the value of mask is not in
10600 * the valid range of mask.
10602 * Returns error code OFPERR_OFPACFC_UNSUPPORTED if the configuration is not
10605 ofputil_decode_set_async_config(const struct ofp_header
*oh
, bool loose
,
10606 const struct ofputil_async_cfg
*basis
,
10607 struct ofputil_async_cfg
*ac
)
10609 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
10610 enum ofpraw raw
= ofpraw_pull_assert(&b
);
10612 if (raw
== OFPRAW_OFPT13_SET_ASYNC
||
10613 raw
== OFPRAW_NXT_SET_ASYNC_CONFIG
||
10614 raw
== OFPRAW_OFPT13_GET_ASYNC_REPLY
) {
10615 const struct nx_async_config
*msg
= ofpmsg_body(oh
);
10617 *ac
= OFPUTIL_ASYNC_CFG_INIT
;
10618 decode_legacy_async_masks(msg
->packet_in_mask
, OAM_PACKET_IN
,
10620 decode_legacy_async_masks(msg
->port_status_mask
, OAM_PORT_STATUS
,
10622 decode_legacy_async_masks(msg
->flow_removed_mask
, OAM_FLOW_REMOVED
,
10624 } else if (raw
== OFPRAW_OFPT14_SET_ASYNC
||
10625 raw
== OFPRAW_OFPT14_GET_ASYNC_REPLY
||
10626 raw
== OFPRAW_NXT_SET_ASYNC_CONFIG2
) {
10628 while (b
.size
> 0) {
10629 struct ofpbuf property
;
10633 error
= ofpprop_pull__(&b
, &property
, 8, 0xfffe, &type
);
10638 const struct ofp14_async_prop
*ap
10639 = get_ofp14_async_config_prop_by_prop_type(type
);
10641 ? parse_async_tlv(&property
, ap
, ac
, oh
->version
, loose
)
10642 : OFPPROP_UNKNOWN(loose
, "async config", type
));
10644 /* Most messages use OFPBPC_BAD_TYPE but async has its own (who
10645 * knows why, it's OpenFlow. */
10646 if (error
== OFPERR_OFPBPC_BAD_TYPE
) {
10647 error
= OFPERR_OFPACFC_UNSUPPORTED
;
10653 return OFPERR_OFPBRC_BAD_VERSION
;
10659 encode_legacy_async_masks(const struct ofputil_async_cfg
*ac
,
10660 enum ofputil_async_msg_type oam
,
10661 enum ofp_version version
,
10664 for (int i
= 0; i
< 2; i
++) {
10665 bool master
= i
== 0;
10666 const struct ofp14_async_prop
*ap
10667 = get_ofp14_async_config_prop_by_oam(oam
, master
);
10668 masks
[i
] = encode_async_mask(ac
, ap
, version
);
10673 ofputil_put_async_config__(const struct ofputil_async_cfg
*ac
,
10674 struct ofpbuf
*buf
, bool tlv
,
10675 enum ofp_version version
, uint32_t oams
)
10678 struct nx_async_config
*msg
= ofpbuf_put_zeros(buf
, sizeof *msg
);
10679 encode_legacy_async_masks(ac
, OAM_PACKET_IN
, version
,
10680 msg
->packet_in_mask
);
10681 encode_legacy_async_masks(ac
, OAM_PORT_STATUS
, version
,
10682 msg
->port_status_mask
);
10683 encode_legacy_async_masks(ac
, OAM_FLOW_REMOVED
, version
,
10684 msg
->flow_removed_mask
);
10686 FOR_EACH_ASYNC_PROP (ap
) {
10687 if (oams
& (1u << ap
->oam
)) {
10688 size_t ofs
= buf
->size
;
10689 ofpprop_put_be32(buf
, ap
->prop_type
,
10690 encode_async_mask(ac
, ap
, version
));
10692 /* For experimenter properties, we need to use type 0xfffe for
10693 * master and 0xffff for slaves. */
10694 if (ofpprop_is_experimenter(ap
->prop_type
)) {
10695 struct ofp_prop_experimenter
*ope
10696 = ofpbuf_at_assert(buf
, ofs
, sizeof *ope
);
10697 ope
->type
= ap
->master
? htons(0xffff) : htons(0xfffe);
10704 /* Encodes and returns a reply to the OFPT_GET_ASYNC_REQUEST in 'oh' that
10705 * states that the asynchronous message configuration is 'ac'. */
10707 ofputil_encode_get_async_reply(const struct ofp_header
*oh
,
10708 const struct ofputil_async_cfg
*ac
)
10710 struct ofpbuf
*buf
;
10712 enum ofpraw raw
= (oh
->version
< OFP14_VERSION
10713 ? OFPRAW_OFPT13_GET_ASYNC_REPLY
10714 : OFPRAW_OFPT14_GET_ASYNC_REPLY
);
10715 struct ofpbuf
*reply
= ofpraw_alloc_reply(raw
, oh
, 0);
10716 ofputil_put_async_config__(ac
, reply
,
10717 raw
== OFPRAW_OFPT14_GET_ASYNC_REPLY
,
10718 oh
->version
, UINT32_MAX
);
10724 /* Encodes and returns a message, in a format appropriate for OpenFlow version
10725 * 'ofp_version', that sets the asynchronous message configuration to 'ac'.
10727 * Specify 'oams' as a bitmap of OAM_* that indicate the asynchronous messages
10728 * to configure. OF1.0 through OF1.3 can't natively configure a subset of
10729 * messages, so more messages than requested may be configured. OF1.0 through
10730 * OF1.3 also can't configure OVS extension OAM_* values, so if 'oam' includes
10731 * any extensions then this function encodes an Open vSwitch extension message
10732 * that does support configuring OVS extension OAM_*. */
10734 ofputil_encode_set_async_config(const struct ofputil_async_cfg
*ac
,
10735 uint32_t oams
, enum ofp_version ofp_version
)
10737 enum ofpraw raw
= (ofp_version
>= OFP14_VERSION
? OFPRAW_OFPT14_SET_ASYNC
10738 : oams
& OAM_EXTENSIONS
? OFPRAW_NXT_SET_ASYNC_CONFIG2
10739 : ofp_version
>= OFP13_VERSION
? OFPRAW_OFPT13_SET_ASYNC
10740 : OFPRAW_NXT_SET_ASYNC_CONFIG
);
10741 struct ofpbuf
*request
= ofpraw_alloc(raw
, ofp_version
, 0);
10742 ofputil_put_async_config__(ac
, request
,
10743 (raw
== OFPRAW_OFPT14_SET_ASYNC
||
10744 raw
== OFPRAW_NXT_SET_ASYNC_CONFIG2
),
10745 ofp_version
, oams
);
10749 struct ofputil_async_cfg
10750 ofputil_async_cfg_default(enum ofp_version version
)
10752 /* We enable all of the OF1.4 reasons regardless of 'version' because the
10753 * reasons added in OF1.4 just are just refinements of the OFPR_ACTION
10754 * introduced in OF1.0, breaking it into more specific categories. When we
10755 * encode these for earlier OpenFlow versions, we translate them into
10757 uint32_t pin
= OFPR14_BITS
& ~(1u << OFPR_INVALID_TTL
);
10758 pin
|= 1u << OFPR_EXPLICIT_MISS
;
10759 if (version
<= OFP12_VERSION
) {
10760 pin
|= 1u << OFPR_IMPLICIT_MISS
;
10763 return (struct ofputil_async_cfg
) {
10764 .master
[OAM_PACKET_IN
] = pin
,
10766 .master
[OAM_FLOW_REMOVED
]
10767 = (version
>= OFP14_VERSION
? OFPRR14_BITS
: OFPRR10_BITS
),
10769 .master
[OAM_PORT_STATUS
] = OFPPR_BITS
,
10770 .slave
[OAM_PORT_STATUS
] = OFPPR_BITS
,
10775 ofputil_put_ofp14_table_desc(const struct ofputil_table_desc
*td
,
10776 struct ofpbuf
*b
, enum ofp_version version
)
10778 struct ofp14_table_desc
*otd
;
10779 struct ofp14_table_mod_prop_vacancy
*otv
;
10782 start_otd
= b
->size
;
10783 ofpbuf_put_zeros(b
, sizeof *otd
);
10785 ofpprop_put_u32(b
, OFPTMPT14_EVICTION
, td
->eviction_flags
);
10787 otv
= ofpbuf_put_zeros(b
, sizeof *otv
);
10788 otv
->type
= htons(OFPTMPT14_VACANCY
);
10789 otv
->length
= htons(sizeof *otv
);
10790 otv
->vacancy_down
= td
->table_vacancy
.vacancy_down
;
10791 otv
->vacancy_up
= td
->table_vacancy
.vacancy_up
;
10792 otv
->vacancy
= td
->table_vacancy
.vacancy
;
10794 otd
= ofpbuf_at_assert(b
, start_otd
, sizeof *otd
);
10795 otd
->length
= htons(b
->size
- start_otd
);
10796 otd
->table_id
= td
->table_id
;
10797 otd
->config
= ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT
,
10798 td
->eviction
, td
->vacancy
,
10802 /* Converts the abstract form of a "table status" message in '*ts' into an
10803 * OpenFlow message suitable for 'protocol', and returns that encoded form in
10804 * a buffer owned by the caller. */
10806 ofputil_encode_table_status(const struct ofputil_table_status
*ts
,
10807 enum ofputil_protocol protocol
)
10809 enum ofp_version version
;
10812 version
= ofputil_protocol_to_ofp_version(protocol
);
10813 if (version
>= OFP14_VERSION
) {
10815 struct ofp14_table_status
*ots
;
10817 raw
= OFPRAW_OFPT14_TABLE_STATUS
;
10818 b
= ofpraw_alloc_xid(raw
, version
, htonl(0), 0);
10819 ots
= ofpbuf_put_zeros(b
, sizeof *ots
);
10820 ots
->reason
= ts
->reason
;
10821 ofputil_put_ofp14_table_desc(&ts
->desc
, b
, version
);
10822 ofpmsg_update_length(b
);
10829 /* Decodes the OpenFlow "table status" message in '*ots' into an abstract form
10830 * in '*ts'. Returns 0 if successful, otherwise an OFPERR_* value. */
10832 ofputil_decode_table_status(const struct ofp_header
*oh
,
10833 struct ofputil_table_status
*ts
)
10835 const struct ofp14_table_status
*ots
;
10840 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
10841 raw
= ofpraw_pull_assert(&b
);
10842 ots
= ofpbuf_pull(&b
, sizeof *ots
);
10844 if (raw
== OFPRAW_OFPT14_TABLE_STATUS
) {
10845 if (ots
->reason
!= OFPTR_VACANCY_DOWN
10846 && ots
->reason
!= OFPTR_VACANCY_UP
) {
10847 return OFPERR_OFPBPC_BAD_VALUE
;
10849 ts
->reason
= ots
->reason
;
10851 error
= ofputil_decode_table_desc(&b
, &ts
->desc
, oh
->version
);
10854 return OFPERR_OFPBRC_BAD_VERSION
;