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/json.h"
37 #include "openvswitch/meta-flow.h"
38 #include "openvswitch/ofp-actions.h"
39 #include "openvswitch/ofp-errors.h"
40 #include "openvswitch/ofp-msgs.h"
41 #include "openvswitch/ofp-print.h"
42 #include "openvswitch/ofp-prop.h"
43 #include "openvswitch/ofp-util.h"
44 #include "openvswitch/ofpbuf.h"
45 #include "openvswitch/type-props.h"
46 #include "openvswitch/vlog.h"
47 #include "openflow/intel-ext.h"
50 #include "tun-metadata.h"
51 #include "unaligned.h"
55 VLOG_DEFINE_THIS_MODULE(ofp_util
);
57 /* Rate limit for OpenFlow message parse errors. These always indicate a bug
58 * in the peer and so there's not much point in showing a lot of them. */
59 static struct vlog_rate_limit bad_ofmsg_rl
= VLOG_RATE_LIMIT_INIT(1, 5);
61 static enum ofputil_table_vacancy
ofputil_decode_table_vacancy(
62 ovs_be32 config
, enum ofp_version
);
63 static enum ofputil_table_eviction
ofputil_decode_table_eviction(
64 ovs_be32 config
, enum ofp_version
);
65 static ovs_be32
ofputil_encode_table_config(enum ofputil_table_miss
,
66 enum ofputil_table_eviction
,
67 enum ofputil_table_vacancy
,
70 /* Given the wildcard bit count in the least-significant 6 of 'wcbits', returns
71 * an IP netmask with a 1 in each bit that must match and a 0 in each bit that
74 * The bits in 'wcbits' are in the format used in enum ofp_flow_wildcards: 0
75 * is exact match, 1 ignores the LSB, 2 ignores the 2 least-significant bits,
76 * ..., 32 and higher wildcard the entire field. This is the *opposite* of the
77 * usual convention where e.g. /24 indicates that 8 bits (not 24 bits) are
80 ofputil_wcbits_to_netmask(int wcbits
)
83 return wcbits
< 32 ? htonl(~((1u << wcbits
) - 1)) : 0;
86 /* Given the IP netmask 'netmask', returns the number of bits of the IP address
87 * that it wildcards, that is, the number of 0-bits in 'netmask', a number
88 * between 0 and 32 inclusive.
90 * If 'netmask' is not a CIDR netmask (see ip_is_cidr()), the return value will
91 * still be in the valid range but isn't otherwise meaningful. */
93 ofputil_netmask_to_wcbits(ovs_be32 netmask
)
95 return 32 - ip_count_cidr_bits(netmask
);
98 /* Converts the OpenFlow 1.0 wildcards in 'ofpfw' (OFPFW10_*) into a
99 * flow_wildcards in 'wc' for use in struct match. It is the caller's
100 * responsibility to handle the special case where the flow match's dl_vlan is
101 * set to OFP_VLAN_NONE. */
103 ofputil_wildcard_from_ofpfw10(uint32_t ofpfw
, struct flow_wildcards
*wc
)
105 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 39);
107 /* Initialize most of wc. */
108 flow_wildcards_init_catchall(wc
);
110 if (!(ofpfw
& OFPFW10_IN_PORT
)) {
111 wc
->masks
.in_port
.ofp_port
= u16_to_ofp(UINT16_MAX
);
114 if (!(ofpfw
& OFPFW10_NW_TOS
)) {
115 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
118 if (!(ofpfw
& OFPFW10_NW_PROTO
)) {
119 wc
->masks
.nw_proto
= UINT8_MAX
;
121 wc
->masks
.nw_src
= ofputil_wcbits_to_netmask(ofpfw
122 >> OFPFW10_NW_SRC_SHIFT
);
123 wc
->masks
.nw_dst
= ofputil_wcbits_to_netmask(ofpfw
124 >> OFPFW10_NW_DST_SHIFT
);
126 if (!(ofpfw
& OFPFW10_TP_SRC
)) {
127 wc
->masks
.tp_src
= OVS_BE16_MAX
;
129 if (!(ofpfw
& OFPFW10_TP_DST
)) {
130 wc
->masks
.tp_dst
= OVS_BE16_MAX
;
133 if (!(ofpfw
& OFPFW10_DL_SRC
)) {
134 WC_MASK_FIELD(wc
, dl_src
);
136 if (!(ofpfw
& OFPFW10_DL_DST
)) {
137 WC_MASK_FIELD(wc
, dl_dst
);
139 if (!(ofpfw
& OFPFW10_DL_TYPE
)) {
140 wc
->masks
.dl_type
= OVS_BE16_MAX
;
144 if (!(ofpfw
& OFPFW10_DL_VLAN_PCP
)) {
145 wc
->masks
.vlans
[0].tci
|= htons(VLAN_PCP_MASK
| VLAN_CFI
);
147 if (!(ofpfw
& OFPFW10_DL_VLAN
)) {
148 wc
->masks
.vlans
[0].tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
152 /* Converts the ofp10_match in 'ofmatch' into a struct match in 'match'. */
154 ofputil_match_from_ofp10_match(const struct ofp10_match
*ofmatch
,
157 uint32_t ofpfw
= ntohl(ofmatch
->wildcards
) & OFPFW10_ALL
;
159 /* Initialize match->wc. */
160 memset(&match
->flow
, 0, sizeof match
->flow
);
161 ofputil_wildcard_from_ofpfw10(ofpfw
, &match
->wc
);
162 memset(&match
->tun_md
, 0, sizeof match
->tun_md
);
164 /* If any fields, except in_port, are matched, then we also need to match
165 * on the Ethernet packet_type. */
166 const uint32_t ofpfw_data_bits
= (OFPFW10_NW_TOS
| OFPFW10_NW_PROTO
167 | OFPFW10_TP_SRC
| OFPFW10_TP_DST
168 | OFPFW10_DL_SRC
| OFPFW10_DL_DST
170 | OFPFW10_DL_VLAN
| OFPFW10_DL_VLAN_PCP
);
171 if ((ofpfw
& ofpfw_data_bits
) != ofpfw_data_bits
172 || ofputil_wcbits_to_netmask(ofpfw
>> OFPFW10_NW_SRC_SHIFT
)
173 || ofputil_wcbits_to_netmask(ofpfw
>> OFPFW10_NW_DST_SHIFT
)) {
174 match_set_default_packet_type(match
);
177 /* Initialize most of match->flow. */
178 match
->flow
.nw_src
= ofmatch
->nw_src
;
179 match
->flow
.nw_dst
= ofmatch
->nw_dst
;
180 match
->flow
.in_port
.ofp_port
= u16_to_ofp(ntohs(ofmatch
->in_port
));
181 match
->flow
.dl_type
= ofputil_dl_type_from_openflow(ofmatch
->dl_type
);
182 match
->flow
.tp_src
= ofmatch
->tp_src
;
183 match
->flow
.tp_dst
= ofmatch
->tp_dst
;
184 match
->flow
.dl_src
= ofmatch
->dl_src
;
185 match
->flow
.dl_dst
= ofmatch
->dl_dst
;
186 match
->flow
.nw_tos
= ofmatch
->nw_tos
& IP_DSCP_MASK
;
187 match
->flow
.nw_proto
= ofmatch
->nw_proto
;
189 /* Translate VLANs. */
190 if (!(ofpfw
& OFPFW10_DL_VLAN
) &&
191 ofmatch
->dl_vlan
== htons(OFP10_VLAN_NONE
)) {
192 /* Match only packets without 802.1Q header.
194 * When OFPFW10_DL_VLAN_PCP is wildcarded, this is obviously correct.
196 * If OFPFW10_DL_VLAN_PCP is matched, the flow match is contradictory,
197 * because we can't have a specific PCP without an 802.1Q header.
198 * However, older versions of OVS treated this as matching packets
199 * withut an 802.1Q header, so we do here too. */
200 match
->flow
.vlans
[0].tci
= htons(0);
201 match
->wc
.masks
.vlans
[0].tci
= htons(0xffff);
203 ovs_be16 vid
, pcp
, tci
;
206 vid
= ofmatch
->dl_vlan
& htons(VLAN_VID_MASK
);
207 hpcp
= (ofmatch
->dl_vlan_pcp
<< VLAN_PCP_SHIFT
) & VLAN_PCP_MASK
;
209 tci
= vid
| pcp
| htons(VLAN_CFI
);
210 match
->flow
.vlans
[0].tci
= tci
& match
->wc
.masks
.vlans
[0].tci
;
214 match_zero_wildcarded_fields(match
);
217 /* Convert 'match' into the OpenFlow 1.0 match structure 'ofmatch'. */
219 ofputil_match_to_ofp10_match(const struct match
*match
,
220 struct ofp10_match
*ofmatch
)
222 const struct flow_wildcards
*wc
= &match
->wc
;
225 /* Figure out most OpenFlow wildcards. */
227 if (!wc
->masks
.in_port
.ofp_port
) {
228 ofpfw
|= OFPFW10_IN_PORT
;
230 if (!wc
->masks
.dl_type
) {
231 ofpfw
|= OFPFW10_DL_TYPE
;
233 if (!wc
->masks
.nw_proto
) {
234 ofpfw
|= OFPFW10_NW_PROTO
;
236 ofpfw
|= (ofputil_netmask_to_wcbits(wc
->masks
.nw_src
)
237 << OFPFW10_NW_SRC_SHIFT
);
238 ofpfw
|= (ofputil_netmask_to_wcbits(wc
->masks
.nw_dst
)
239 << OFPFW10_NW_DST_SHIFT
);
240 if (!(wc
->masks
.nw_tos
& IP_DSCP_MASK
)) {
241 ofpfw
|= OFPFW10_NW_TOS
;
243 if (!wc
->masks
.tp_src
) {
244 ofpfw
|= OFPFW10_TP_SRC
;
246 if (!wc
->masks
.tp_dst
) {
247 ofpfw
|= OFPFW10_TP_DST
;
249 if (eth_addr_is_zero(wc
->masks
.dl_src
)) {
250 ofpfw
|= OFPFW10_DL_SRC
;
252 if (eth_addr_is_zero(wc
->masks
.dl_dst
)) {
253 ofpfw
|= OFPFW10_DL_DST
;
256 /* Translate VLANs. */
257 ofmatch
->dl_vlan
= htons(0);
258 ofmatch
->dl_vlan_pcp
= 0;
259 if (match
->wc
.masks
.vlans
[0].tci
== htons(0)) {
260 ofpfw
|= OFPFW10_DL_VLAN
| OFPFW10_DL_VLAN_PCP
;
261 } else if (match
->wc
.masks
.vlans
[0].tci
& htons(VLAN_CFI
)
262 && !(match
->flow
.vlans
[0].tci
& htons(VLAN_CFI
))) {
263 ofmatch
->dl_vlan
= htons(OFP10_VLAN_NONE
);
265 if (!(match
->wc
.masks
.vlans
[0].tci
& htons(VLAN_VID_MASK
))) {
266 ofpfw
|= OFPFW10_DL_VLAN
;
269 htons(vlan_tci_to_vid(match
->flow
.vlans
[0].tci
));
272 if (!(match
->wc
.masks
.vlans
[0].tci
& htons(VLAN_PCP_MASK
))) {
273 ofpfw
|= OFPFW10_DL_VLAN_PCP
;
275 ofmatch
->dl_vlan_pcp
= vlan_tci_to_pcp(match
->flow
.vlans
[0].tci
);
279 /* Compose most of the match structure. */
280 ofmatch
->wildcards
= htonl(ofpfw
);
281 ofmatch
->in_port
= htons(ofp_to_u16(match
->flow
.in_port
.ofp_port
));
282 ofmatch
->dl_src
= match
->flow
.dl_src
;
283 ofmatch
->dl_dst
= match
->flow
.dl_dst
;
284 ofmatch
->dl_type
= ofputil_dl_type_to_openflow(match
->flow
.dl_type
);
285 ofmatch
->nw_src
= match
->flow
.nw_src
;
286 ofmatch
->nw_dst
= match
->flow
.nw_dst
;
287 ofmatch
->nw_tos
= match
->flow
.nw_tos
& IP_DSCP_MASK
;
288 ofmatch
->nw_proto
= match
->flow
.nw_proto
;
289 ofmatch
->tp_src
= match
->flow
.tp_src
;
290 ofmatch
->tp_dst
= match
->flow
.tp_dst
;
291 memset(ofmatch
->pad1
, '\0', sizeof ofmatch
->pad1
);
292 memset(ofmatch
->pad2
, '\0', sizeof ofmatch
->pad2
);
296 ofputil_pull_ofp11_match(struct ofpbuf
*buf
, const struct tun_table
*tun_table
,
297 const struct vl_mff_map
*vl_mff_map
,
298 struct match
*match
, uint16_t *padded_match_len
)
300 struct ofp11_match_header
*omh
= buf
->data
;
303 if (buf
->size
< sizeof *omh
) {
304 return OFPERR_OFPBMC_BAD_LEN
;
307 match_len
= ntohs(omh
->length
);
309 switch (ntohs(omh
->type
)) {
310 case OFPMT_STANDARD
: {
311 struct ofp11_match
*om
;
313 if (match_len
!= sizeof *om
|| buf
->size
< sizeof *om
) {
314 return OFPERR_OFPBMC_BAD_LEN
;
316 om
= ofpbuf_pull(buf
, sizeof *om
);
317 if (padded_match_len
) {
318 *padded_match_len
= match_len
;
320 return ofputil_match_from_ofp11_match(om
, match
);
324 if (padded_match_len
) {
325 *padded_match_len
= ROUND_UP(match_len
, 8);
327 return oxm_pull_match(buf
, false, tun_table
, vl_mff_map
, match
);
330 return OFPERR_OFPBMC_BAD_TYPE
;
334 /* Converts the ofp11_match in 'ofmatch' into a struct match in 'match'.
335 * Returns 0 if successful, otherwise an OFPERR_* value. */
337 ofputil_match_from_ofp11_match(const struct ofp11_match
*ofmatch
,
340 uint16_t wc
= ntohl(ofmatch
->wildcards
);
341 bool ipv4
, arp
, rarp
;
343 match_init_catchall(match
);
344 match
->flow
.tunnel
.metadata
.tab
= NULL
;
346 if (!(wc
& OFPFW11_IN_PORT
)) {
350 error
= ofputil_port_from_ofp11(ofmatch
->in_port
, &ofp_port
);
352 return OFPERR_OFPBMC_BAD_VALUE
;
354 match_set_in_port(match
, ofp_port
);
357 struct eth_addr dl_src_mask
= eth_addr_invert(ofmatch
->dl_src_mask
);
358 struct eth_addr dl_dst_mask
= eth_addr_invert(ofmatch
->dl_dst_mask
);
359 if (!eth_addr_is_zero(dl_src_mask
) || !eth_addr_is_zero(dl_dst_mask
)) {
360 match_set_dl_src_masked(match
, ofmatch
->dl_src
, dl_src_mask
);
361 match_set_dl_dst_masked(match
, ofmatch
->dl_dst
, dl_dst_mask
);
362 match_set_default_packet_type(match
);
365 if (!(wc
& OFPFW11_DL_VLAN
)) {
366 if (ofmatch
->dl_vlan
== htons(OFPVID11_NONE
)) {
367 /* Match only packets without a VLAN tag. */
368 match
->flow
.vlans
[0].tci
= htons(0);
369 match
->wc
.masks
.vlans
[0].tci
= OVS_BE16_MAX
;
371 if (ofmatch
->dl_vlan
== htons(OFPVID11_ANY
)) {
372 /* Match any packet with a VLAN tag regardless of VID. */
373 match
->flow
.vlans
[0].tci
= htons(VLAN_CFI
);
374 match
->wc
.masks
.vlans
[0].tci
= htons(VLAN_CFI
);
375 } else if (ntohs(ofmatch
->dl_vlan
) < 4096) {
376 /* Match only packets with the specified VLAN VID. */
377 match
->flow
.vlans
[0].tci
= htons(VLAN_CFI
) | ofmatch
->dl_vlan
;
378 match
->wc
.masks
.vlans
[0].tci
= htons(VLAN_CFI
| VLAN_VID_MASK
);
381 return OFPERR_OFPBMC_BAD_VALUE
;
384 if (!(wc
& OFPFW11_DL_VLAN_PCP
)) {
385 if (ofmatch
->dl_vlan_pcp
<= 7) {
386 match
->flow
.vlans
[0].tci
|= htons(ofmatch
->dl_vlan_pcp
388 match
->wc
.masks
.vlans
[0].tci
|= htons(VLAN_PCP_MASK
);
391 return OFPERR_OFPBMC_BAD_VALUE
;
395 match_set_default_packet_type(match
);
398 if (!(wc
& OFPFW11_DL_TYPE
)) {
399 match_set_dl_type(match
,
400 ofputil_dl_type_from_openflow(ofmatch
->dl_type
));
401 match_set_default_packet_type(match
);
404 ipv4
= match
->flow
.dl_type
== htons(ETH_TYPE_IP
);
405 arp
= match
->flow
.dl_type
== htons(ETH_TYPE_ARP
);
406 rarp
= match
->flow
.dl_type
== htons(ETH_TYPE_RARP
);
408 if (ipv4
&& !(wc
& OFPFW11_NW_TOS
)) {
409 if (ofmatch
->nw_tos
& ~IP_DSCP_MASK
) {
411 return OFPERR_OFPBMC_BAD_VALUE
;
414 match_set_nw_dscp(match
, ofmatch
->nw_tos
);
417 if (ipv4
|| arp
|| rarp
) {
418 if (!(wc
& OFPFW11_NW_PROTO
)) {
419 match_set_nw_proto(match
, ofmatch
->nw_proto
);
421 match_set_nw_src_masked(match
, ofmatch
->nw_src
, ~ofmatch
->nw_src_mask
);
422 match_set_nw_dst_masked(match
, ofmatch
->nw_dst
, ~ofmatch
->nw_dst_mask
);
425 #define OFPFW11_TP_ALL (OFPFW11_TP_SRC | OFPFW11_TP_DST)
426 if (ipv4
&& (wc
& OFPFW11_TP_ALL
) != OFPFW11_TP_ALL
) {
427 switch (match
->flow
.nw_proto
) {
429 /* "A.2.3 Flow Match Structures" in OF1.1 says:
431 * The tp_src and tp_dst fields will be ignored unless the
432 * network protocol specified is as TCP, UDP or SCTP.
434 * but I'm pretty sure we should support ICMP too, otherwise
435 * that's a regression from OF1.0. */
436 if (!(wc
& OFPFW11_TP_SRC
)) {
437 uint16_t icmp_type
= ntohs(ofmatch
->tp_src
);
438 if (icmp_type
< 0x100) {
439 match_set_icmp_type(match
, icmp_type
);
441 return OFPERR_OFPBMC_BAD_FIELD
;
444 if (!(wc
& OFPFW11_TP_DST
)) {
445 uint16_t icmp_code
= ntohs(ofmatch
->tp_dst
);
446 if (icmp_code
< 0x100) {
447 match_set_icmp_code(match
, icmp_code
);
449 return OFPERR_OFPBMC_BAD_FIELD
;
457 if (!(wc
& (OFPFW11_TP_SRC
))) {
458 match_set_tp_src(match
, ofmatch
->tp_src
);
460 if (!(wc
& (OFPFW11_TP_DST
))) {
461 match_set_tp_dst(match
, ofmatch
->tp_dst
);
466 /* OF1.1 says explicitly to ignore this. */
471 if (eth_type_mpls(match
->flow
.dl_type
)) {
472 if (!(wc
& OFPFW11_MPLS_LABEL
)) {
473 match_set_mpls_label(match
, 0, ofmatch
->mpls_label
);
475 if (!(wc
& OFPFW11_MPLS_TC
)) {
476 match_set_mpls_tc(match
, 0, ofmatch
->mpls_tc
);
480 match_set_metadata_masked(match
, ofmatch
->metadata
,
481 ~ofmatch
->metadata_mask
);
486 /* Convert 'match' into the OpenFlow 1.1 match structure 'ofmatch'. */
488 ofputil_match_to_ofp11_match(const struct match
*match
,
489 struct ofp11_match
*ofmatch
)
493 memset(ofmatch
, 0, sizeof *ofmatch
);
494 ofmatch
->omh
.type
= htons(OFPMT_STANDARD
);
495 ofmatch
->omh
.length
= htons(OFPMT11_STANDARD_LENGTH
);
497 if (!match
->wc
.masks
.in_port
.ofp_port
) {
498 wc
|= OFPFW11_IN_PORT
;
500 ofmatch
->in_port
= ofputil_port_to_ofp11(match
->flow
.in_port
.ofp_port
);
503 ofmatch
->dl_src
= match
->flow
.dl_src
;
504 ofmatch
->dl_src_mask
= eth_addr_invert(match
->wc
.masks
.dl_src
);
505 ofmatch
->dl_dst
= match
->flow
.dl_dst
;
506 ofmatch
->dl_dst_mask
= eth_addr_invert(match
->wc
.masks
.dl_dst
);
508 if (match
->wc
.masks
.vlans
[0].tci
== htons(0)) {
509 wc
|= OFPFW11_DL_VLAN
| OFPFW11_DL_VLAN_PCP
;
510 } else if (match
->wc
.masks
.vlans
[0].tci
& htons(VLAN_CFI
)
511 && !(match
->flow
.vlans
[0].tci
& htons(VLAN_CFI
))) {
512 ofmatch
->dl_vlan
= htons(OFPVID11_NONE
);
513 wc
|= OFPFW11_DL_VLAN_PCP
;
515 if (!(match
->wc
.masks
.vlans
[0].tci
& htons(VLAN_VID_MASK
))) {
516 ofmatch
->dl_vlan
= htons(OFPVID11_ANY
);
519 htons(vlan_tci_to_vid(match
->flow
.vlans
[0].tci
));
522 if (!(match
->wc
.masks
.vlans
[0].tci
& htons(VLAN_PCP_MASK
))) {
523 wc
|= OFPFW11_DL_VLAN_PCP
;
525 ofmatch
->dl_vlan_pcp
= vlan_tci_to_pcp(match
->flow
.vlans
[0].tci
);
529 if (!match
->wc
.masks
.dl_type
) {
530 wc
|= OFPFW11_DL_TYPE
;
532 ofmatch
->dl_type
= ofputil_dl_type_to_openflow(match
->flow
.dl_type
);
535 if (!(match
->wc
.masks
.nw_tos
& IP_DSCP_MASK
)) {
536 wc
|= OFPFW11_NW_TOS
;
538 ofmatch
->nw_tos
= match
->flow
.nw_tos
& IP_DSCP_MASK
;
541 if (!match
->wc
.masks
.nw_proto
) {
542 wc
|= OFPFW11_NW_PROTO
;
544 ofmatch
->nw_proto
= match
->flow
.nw_proto
;
547 ofmatch
->nw_src
= match
->flow
.nw_src
;
548 ofmatch
->nw_src_mask
= ~match
->wc
.masks
.nw_src
;
549 ofmatch
->nw_dst
= match
->flow
.nw_dst
;
550 ofmatch
->nw_dst_mask
= ~match
->wc
.masks
.nw_dst
;
552 if (!match
->wc
.masks
.tp_src
) {
553 wc
|= OFPFW11_TP_SRC
;
555 ofmatch
->tp_src
= match
->flow
.tp_src
;
558 if (!match
->wc
.masks
.tp_dst
) {
559 wc
|= OFPFW11_TP_DST
;
561 ofmatch
->tp_dst
= match
->flow
.tp_dst
;
564 if (!(match
->wc
.masks
.mpls_lse
[0] & htonl(MPLS_LABEL_MASK
))) {
565 wc
|= OFPFW11_MPLS_LABEL
;
567 ofmatch
->mpls_label
= htonl(mpls_lse_to_label(
568 match
->flow
.mpls_lse
[0]));
571 if (!(match
->wc
.masks
.mpls_lse
[0] & htonl(MPLS_TC_MASK
))) {
572 wc
|= OFPFW11_MPLS_TC
;
574 ofmatch
->mpls_tc
= mpls_lse_to_tc(match
->flow
.mpls_lse
[0]);
577 ofmatch
->metadata
= match
->flow
.metadata
;
578 ofmatch
->metadata_mask
= ~match
->wc
.masks
.metadata
;
580 ofmatch
->wildcards
= htonl(wc
);
583 /* Returns the "typical" length of a match for 'protocol', for use in
584 * estimating space to preallocate. */
586 ofputil_match_typical_len(enum ofputil_protocol protocol
)
589 case OFPUTIL_P_OF10_STD
:
590 case OFPUTIL_P_OF10_STD_TID
:
591 return sizeof(struct ofp10_match
);
593 case OFPUTIL_P_OF10_NXM
:
594 case OFPUTIL_P_OF10_NXM_TID
:
595 return NXM_TYPICAL_LEN
;
597 case OFPUTIL_P_OF11_STD
:
598 return sizeof(struct ofp11_match
);
600 case OFPUTIL_P_OF12_OXM
:
601 case OFPUTIL_P_OF13_OXM
:
602 case OFPUTIL_P_OF14_OXM
:
603 case OFPUTIL_P_OF15_OXM
:
604 case OFPUTIL_P_OF16_OXM
:
605 return NXM_TYPICAL_LEN
;
612 /* Appends to 'b' an struct ofp11_match_header followed by a match that
613 * expresses 'match' properly for 'protocol', plus enough zero bytes to pad the
614 * data appended out to a multiple of 8. 'protocol' must be one that is usable
615 * in OpenFlow 1.1 or later.
617 * This function can cause 'b''s data to be reallocated.
619 * Returns the number of bytes appended to 'b', excluding the padding. Never
622 ofputil_put_ofp11_match(struct ofpbuf
*b
, const struct match
*match
,
623 enum ofputil_protocol protocol
)
626 case OFPUTIL_P_OF10_STD
:
627 case OFPUTIL_P_OF10_STD_TID
:
628 case OFPUTIL_P_OF10_NXM
:
629 case OFPUTIL_P_OF10_NXM_TID
:
632 case OFPUTIL_P_OF11_STD
: {
633 struct ofp11_match
*om
;
635 /* Make sure that no padding is needed. */
636 BUILD_ASSERT_DECL(sizeof *om
% 8 == 0);
638 om
= ofpbuf_put_uninit(b
, sizeof *om
);
639 ofputil_match_to_ofp11_match(match
, om
);
643 case OFPUTIL_P_OF12_OXM
:
644 case OFPUTIL_P_OF13_OXM
:
645 case OFPUTIL_P_OF14_OXM
:
646 case OFPUTIL_P_OF15_OXM
:
647 case OFPUTIL_P_OF16_OXM
:
648 return oxm_put_match(b
, match
,
649 ofputil_protocol_to_ofp_version(protocol
));
655 /* Given a 'dl_type' value in the format used in struct flow, returns the
656 * corresponding 'dl_type' value for use in an ofp10_match or ofp11_match
659 ofputil_dl_type_to_openflow(ovs_be16 flow_dl_type
)
661 return (flow_dl_type
== htons(FLOW_DL_TYPE_NONE
)
662 ? htons(OFP_DL_TYPE_NOT_ETH_TYPE
)
666 /* Given a 'dl_type' value in the format used in an ofp10_match or ofp11_match
667 * structure, returns the corresponding 'dl_type' value for use in struct
670 ofputil_dl_type_from_openflow(ovs_be16 ofp_dl_type
)
672 return (ofp_dl_type
== htons(OFP_DL_TYPE_NOT_ETH_TYPE
)
673 ? htons(FLOW_DL_TYPE_NONE
)
679 struct proto_abbrev
{
680 enum ofputil_protocol protocol
;
684 /* Most users really don't care about some of the differences between
685 * protocols. These abbreviations help with that. */
686 static const struct proto_abbrev proto_abbrevs
[] = {
687 { OFPUTIL_P_ANY
, "any" },
688 { OFPUTIL_P_OF10_STD_ANY
, "OpenFlow10" },
689 { OFPUTIL_P_OF10_NXM_ANY
, "NXM" },
690 { OFPUTIL_P_ANY_OXM
, "OXM" },
692 #define N_PROTO_ABBREVS ARRAY_SIZE(proto_abbrevs)
694 enum ofputil_protocol ofputil_flow_dump_protocols
[] = {
704 size_t ofputil_n_flow_dump_protocols
= ARRAY_SIZE(ofputil_flow_dump_protocols
);
706 /* Returns the set of ofputil_protocols that are supported with the given
707 * OpenFlow 'version'. 'version' should normally be an 8-bit OpenFlow version
708 * identifier (e.g. 0x01 for OpenFlow 1.0, 0x02 for OpenFlow 1.1). Returns 0
709 * if 'version' is not supported or outside the valid range. */
710 enum ofputil_protocol
711 ofputil_protocols_from_ofp_version(enum ofp_version version
)
715 return OFPUTIL_P_OF10_STD_ANY
| OFPUTIL_P_OF10_NXM_ANY
;
717 return OFPUTIL_P_OF11_STD
;
719 return OFPUTIL_P_OF12_OXM
;
721 return OFPUTIL_P_OF13_OXM
;
723 return OFPUTIL_P_OF14_OXM
;
725 return OFPUTIL_P_OF15_OXM
;
727 return OFPUTIL_P_OF16_OXM
;
733 /* Returns the ofputil_protocol that is initially in effect on an OpenFlow
734 * connection that has negotiated the given 'version'. 'version' should
735 * normally be an 8-bit OpenFlow version identifier (e.g. 0x01 for OpenFlow
736 * 1.0, 0x02 for OpenFlow 1.1). Returns 0 if 'version' is not supported or
737 * outside the valid range. */
738 enum ofputil_protocol
739 ofputil_protocol_from_ofp_version(enum ofp_version version
)
741 return rightmost_1bit(ofputil_protocols_from_ofp_version(version
));
744 /* Returns the OpenFlow protocol version number (e.g. OFP10_VERSION,
745 * etc.) that corresponds to 'protocol'. */
747 ofputil_protocol_to_ofp_version(enum ofputil_protocol protocol
)
750 case OFPUTIL_P_OF10_STD
:
751 case OFPUTIL_P_OF10_STD_TID
:
752 case OFPUTIL_P_OF10_NXM
:
753 case OFPUTIL_P_OF10_NXM_TID
:
754 return OFP10_VERSION
;
755 case OFPUTIL_P_OF11_STD
:
756 return OFP11_VERSION
;
757 case OFPUTIL_P_OF12_OXM
:
758 return OFP12_VERSION
;
759 case OFPUTIL_P_OF13_OXM
:
760 return OFP13_VERSION
;
761 case OFPUTIL_P_OF14_OXM
:
762 return OFP14_VERSION
;
763 case OFPUTIL_P_OF15_OXM
:
764 return OFP15_VERSION
;
765 case OFPUTIL_P_OF16_OXM
:
766 return OFP16_VERSION
;
772 /* Returns a bitmap of OpenFlow versions that are supported by at
773 * least one of the 'protocols'. */
775 ofputil_protocols_to_version_bitmap(enum ofputil_protocol protocols
)
779 for (; protocols
; protocols
= zero_rightmost_1bit(protocols
)) {
780 enum ofputil_protocol protocol
= rightmost_1bit(protocols
);
782 bitmap
|= 1u << ofputil_protocol_to_ofp_version(protocol
);
788 /* Returns the set of protocols that are supported on top of the
789 * OpenFlow versions included in 'bitmap'. */
790 enum ofputil_protocol
791 ofputil_protocols_from_version_bitmap(uint32_t bitmap
)
793 enum ofputil_protocol protocols
= 0;
795 for (; bitmap
; bitmap
= zero_rightmost_1bit(bitmap
)) {
796 enum ofp_version version
= rightmost_1bit_idx(bitmap
);
798 protocols
|= ofputil_protocols_from_ofp_version(version
);
804 /* Returns true if 'protocol' is a single OFPUTIL_P_* value, false
807 ofputil_protocol_is_valid(enum ofputil_protocol protocol
)
809 return protocol
& OFPUTIL_P_ANY
&& is_pow2(protocol
);
812 /* Returns the equivalent of 'protocol' with the Nicira flow_mod_table_id
813 * extension turned on or off if 'enable' is true or false, respectively.
815 * This extension is only useful for protocols whose "standard" version does
816 * not allow specific tables to be modified. In particular, this is true of
817 * OpenFlow 1.0. In later versions of OpenFlow, a flow_mod request always
818 * specifies a table ID and so there is no need for such an extension. When
819 * 'protocol' is such a protocol that doesn't need a flow_mod_table_id
820 * extension, this function just returns its 'protocol' argument unchanged
821 * regardless of the value of 'enable'. */
822 enum ofputil_protocol
823 ofputil_protocol_set_tid(enum ofputil_protocol protocol
, bool enable
)
826 case OFPUTIL_P_OF10_STD
:
827 case OFPUTIL_P_OF10_STD_TID
:
828 return enable
? OFPUTIL_P_OF10_STD_TID
: OFPUTIL_P_OF10_STD
;
830 case OFPUTIL_P_OF10_NXM
:
831 case OFPUTIL_P_OF10_NXM_TID
:
832 return enable
? OFPUTIL_P_OF10_NXM_TID
: OFPUTIL_P_OF10_NXM
;
834 case OFPUTIL_P_OF11_STD
:
835 return OFPUTIL_P_OF11_STD
;
837 case OFPUTIL_P_OF12_OXM
:
838 return OFPUTIL_P_OF12_OXM
;
840 case OFPUTIL_P_OF13_OXM
:
841 return OFPUTIL_P_OF13_OXM
;
843 case OFPUTIL_P_OF14_OXM
:
844 return OFPUTIL_P_OF14_OXM
;
846 case OFPUTIL_P_OF15_OXM
:
847 return OFPUTIL_P_OF15_OXM
;
849 case OFPUTIL_P_OF16_OXM
:
850 return OFPUTIL_P_OF16_OXM
;
857 /* Returns the "base" version of 'protocol'. That is, if 'protocol' includes
858 * some extension to a standard protocol version, the return value is the
859 * standard version of that protocol without any extension. If 'protocol' is a
860 * standard protocol version, returns 'protocol' unchanged. */
861 enum ofputil_protocol
862 ofputil_protocol_to_base(enum ofputil_protocol protocol
)
864 return ofputil_protocol_set_tid(protocol
, false);
867 /* Returns 'new_base' with any extensions taken from 'cur'. */
868 enum ofputil_protocol
869 ofputil_protocol_set_base(enum ofputil_protocol cur
,
870 enum ofputil_protocol new_base
)
872 bool tid
= (cur
& OFPUTIL_P_TID
) != 0;
875 case OFPUTIL_P_OF10_STD
:
876 case OFPUTIL_P_OF10_STD_TID
:
877 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_STD
, tid
);
879 case OFPUTIL_P_OF10_NXM
:
880 case OFPUTIL_P_OF10_NXM_TID
:
881 return ofputil_protocol_set_tid(OFPUTIL_P_OF10_NXM
, tid
);
883 case OFPUTIL_P_OF11_STD
:
884 return ofputil_protocol_set_tid(OFPUTIL_P_OF11_STD
, tid
);
886 case OFPUTIL_P_OF12_OXM
:
887 return ofputil_protocol_set_tid(OFPUTIL_P_OF12_OXM
, tid
);
889 case OFPUTIL_P_OF13_OXM
:
890 return ofputil_protocol_set_tid(OFPUTIL_P_OF13_OXM
, tid
);
892 case OFPUTIL_P_OF14_OXM
:
893 return ofputil_protocol_set_tid(OFPUTIL_P_OF14_OXM
, tid
);
895 case OFPUTIL_P_OF15_OXM
:
896 return ofputil_protocol_set_tid(OFPUTIL_P_OF15_OXM
, tid
);
898 case OFPUTIL_P_OF16_OXM
:
899 return ofputil_protocol_set_tid(OFPUTIL_P_OF16_OXM
, tid
);
906 /* Returns a string form of 'protocol', if a simple form exists (that is, if
907 * 'protocol' is either a single protocol or it is a combination of protocols
908 * that have a single abbreviation). Otherwise, returns NULL. */
910 ofputil_protocol_to_string(enum ofputil_protocol protocol
)
912 const struct proto_abbrev
*p
;
914 /* Use a "switch" statement for single-bit names so that we get a compiler
915 * warning if we forget any. */
917 case OFPUTIL_P_OF10_NXM
:
918 return "NXM-table_id";
920 case OFPUTIL_P_OF10_NXM_TID
:
921 return "NXM+table_id";
923 case OFPUTIL_P_OF10_STD
:
924 return "OpenFlow10-table_id";
926 case OFPUTIL_P_OF10_STD_TID
:
927 return "OpenFlow10+table_id";
929 case OFPUTIL_P_OF11_STD
:
932 case OFPUTIL_P_OF12_OXM
:
933 return "OXM-OpenFlow12";
935 case OFPUTIL_P_OF13_OXM
:
936 return "OXM-OpenFlow13";
938 case OFPUTIL_P_OF14_OXM
:
939 return "OXM-OpenFlow14";
941 case OFPUTIL_P_OF15_OXM
:
942 return "OXM-OpenFlow15";
944 case OFPUTIL_P_OF16_OXM
:
945 return "OXM-OpenFlow16";
948 /* Check abbreviations. */
949 for (p
= proto_abbrevs
; p
< &proto_abbrevs
[N_PROTO_ABBREVS
]; p
++) {
950 if (protocol
== p
->protocol
) {
958 /* Returns a string that represents 'protocols'. The return value might be a
959 * comma-separated list if 'protocols' doesn't have a simple name. The return
960 * value is "none" if 'protocols' is 0.
962 * The caller must free the returned string (with free()). */
964 ofputil_protocols_to_string(enum ofputil_protocol protocols
)
968 ovs_assert(!(protocols
& ~OFPUTIL_P_ANY
));
969 if (protocols
== 0) {
970 return xstrdup("none");
975 const struct proto_abbrev
*p
;
979 ds_put_char(&s
, ',');
982 for (p
= proto_abbrevs
; p
< &proto_abbrevs
[N_PROTO_ABBREVS
]; p
++) {
983 if ((protocols
& p
->protocol
) == p
->protocol
) {
984 ds_put_cstr(&s
, p
->name
);
985 protocols
&= ~p
->protocol
;
990 for (i
= 0; i
< CHAR_BIT
* sizeof(enum ofputil_protocol
); i
++) {
991 enum ofputil_protocol bit
= 1u << i
;
993 if (protocols
& bit
) {
994 ds_put_cstr(&s
, ofputil_protocol_to_string(bit
));
1003 return ds_steal_cstr(&s
);
1006 static enum ofputil_protocol
1007 ofputil_protocol_from_string__(const char *s
, size_t n
)
1009 const struct proto_abbrev
*p
;
1012 for (i
= 0; i
< CHAR_BIT
* sizeof(enum ofputil_protocol
); i
++) {
1013 enum ofputil_protocol bit
= 1u << i
;
1014 const char *name
= ofputil_protocol_to_string(bit
);
1016 if (name
&& n
== strlen(name
) && !strncasecmp(s
, name
, n
)) {
1021 for (p
= proto_abbrevs
; p
< &proto_abbrevs
[N_PROTO_ABBREVS
]; p
++) {
1022 if (n
== strlen(p
->name
) && !strncasecmp(s
, p
->name
, n
)) {
1030 /* Returns the nonempty set of protocols represented by 's', which can be a
1031 * single protocol name or abbreviation or a comma-separated list of them.
1033 * Aborts the program with an error message if 's' is invalid. */
1034 enum ofputil_protocol
1035 ofputil_protocols_from_string(const char *s
)
1037 const char *orig_s
= s
;
1038 enum ofputil_protocol protocols
;
1042 enum ofputil_protocol p
;
1045 n
= strcspn(s
, ",");
1051 p
= ofputil_protocol_from_string__(s
, n
);
1053 ovs_fatal(0, "%.*s: unknown flow protocol", (int) n
, s
);
1061 ovs_fatal(0, "%s: no flow protocol specified", orig_s
);
1067 ofputil_version_from_string(const char *s
)
1069 if (!strcasecmp(s
, "OpenFlow10")) {
1070 return OFP10_VERSION
;
1072 if (!strcasecmp(s
, "OpenFlow11")) {
1073 return OFP11_VERSION
;
1075 if (!strcasecmp(s
, "OpenFlow12")) {
1076 return OFP12_VERSION
;
1078 if (!strcasecmp(s
, "OpenFlow13")) {
1079 return OFP13_VERSION
;
1081 if (!strcasecmp(s
, "OpenFlow14")) {
1082 return OFP14_VERSION
;
1084 if (!strcasecmp(s
, "OpenFlow15")) {
1085 return OFP15_VERSION
;
1087 if (!strcasecmp(s
, "OpenFlow16")) {
1088 return OFP16_VERSION
;
1094 is_delimiter(unsigned char c
)
1096 return isspace(c
) || c
== ',';
1100 ofputil_versions_from_string(const char *s
)
1103 uint32_t bitmap
= 0;
1110 if (is_delimiter(s
[i
])) {
1115 while (s
[i
+ j
] && !is_delimiter(s
[i
+ j
])) {
1118 key
= xmemdup0(s
+ i
, j
);
1119 version
= ofputil_version_from_string(key
);
1121 VLOG_FATAL("Unknown OpenFlow version: \"%s\"", key
);
1124 bitmap
|= 1u << version
;
1132 ofputil_versions_from_strings(char ** const s
, size_t count
)
1134 uint32_t bitmap
= 0;
1137 int version
= ofputil_version_from_string(s
[count
]);
1139 VLOG_WARN("Unknown OpenFlow version: \"%s\"", s
[count
]);
1141 bitmap
|= 1u << version
;
1149 ofputil_version_to_string(enum ofp_version ofp_version
)
1151 switch (ofp_version
) {
1153 return "OpenFlow10";
1155 return "OpenFlow11";
1157 return "OpenFlow12";
1159 return "OpenFlow13";
1161 return "OpenFlow14";
1163 return "OpenFlow15";
1165 return "OpenFlow16";
1172 ofputil_packet_in_format_is_valid(enum nx_packet_in_format packet_in_format
)
1174 switch (packet_in_format
) {
1175 case NXPIF_STANDARD
:
1176 case NXPIF_NXT_PACKET_IN
:
1177 case NXPIF_NXT_PACKET_IN2
:
1185 ofputil_packet_in_format_to_string(enum nx_packet_in_format packet_in_format
)
1187 switch (packet_in_format
) {
1188 case NXPIF_STANDARD
:
1190 case NXPIF_NXT_PACKET_IN
:
1191 return "nxt_packet_in";
1192 case NXPIF_NXT_PACKET_IN2
:
1193 return "nxt_packet_in2";
1200 ofputil_packet_in_format_from_string(const char *s
)
1202 return (!strcmp(s
, "standard") || !strcmp(s
, "openflow10")
1204 : !strcmp(s
, "nxt_packet_in") || !strcmp(s
, "nxm")
1205 ? NXPIF_NXT_PACKET_IN
1206 : !strcmp(s
, "nxt_packet_in2")
1207 ? NXPIF_NXT_PACKET_IN2
1212 ofputil_format_version(struct ds
*msg
, enum ofp_version version
)
1214 ds_put_format(msg
, "0x%02x", version
);
1218 ofputil_format_version_name(struct ds
*msg
, enum ofp_version version
)
1220 ds_put_cstr(msg
, ofputil_version_to_string(version
));
1224 ofputil_format_version_bitmap__(struct ds
*msg
, uint32_t bitmap
,
1225 void (*format_version
)(struct ds
*msg
,
1229 format_version(msg
, raw_ctz(bitmap
));
1230 bitmap
= zero_rightmost_1bit(bitmap
);
1232 ds_put_cstr(msg
, ", ");
1238 ofputil_format_version_bitmap(struct ds
*msg
, uint32_t bitmap
)
1240 ofputil_format_version_bitmap__(msg
, bitmap
, ofputil_format_version
);
1244 ofputil_format_version_bitmap_names(struct ds
*msg
, uint32_t bitmap
)
1246 ofputil_format_version_bitmap__(msg
, bitmap
, ofputil_format_version_name
);
1250 ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header
*oheh
,
1251 uint32_t *allowed_versionsp
)
1253 uint16_t bitmap_len
= ntohs(oheh
->length
) - sizeof *oheh
;
1254 const ovs_be32
*bitmap
= ALIGNED_CAST(const ovs_be32
*, oheh
+ 1);
1255 uint32_t allowed_versions
;
1257 if (!bitmap_len
|| bitmap_len
% sizeof *bitmap
) {
1261 /* Only use the first 32-bit element of the bitmap as that is all the
1262 * current implementation supports. Subsequent elements are ignored which
1263 * should have no effect on session negotiation until Open vSwitch supports
1264 * wire-protocol versions greater than 31.
1266 allowed_versions
= ntohl(bitmap
[0]);
1268 if (allowed_versions
& 1) {
1269 /* There's no OpenFlow version 0. */
1270 VLOG_WARN_RL(&bad_ofmsg_rl
, "peer claims to support invalid OpenFlow "
1272 allowed_versions
&= ~1u;
1275 if (!allowed_versions
) {
1276 VLOG_WARN_RL(&bad_ofmsg_rl
, "peer does not support any OpenFlow "
1277 "version (between 0x01 and 0x1f)");
1281 *allowed_versionsp
= allowed_versions
;
1286 version_bitmap_from_version(uint8_t ofp_version
)
1288 return ((ofp_version
< 32 ? 1u << ofp_version
: 0) - 1) << 1;
1291 /* Decodes OpenFlow OFPT_HELLO message 'oh', storing into '*allowed_versions'
1292 * the set of OpenFlow versions for which 'oh' announces support.
1294 * Because of how OpenFlow defines OFPT_HELLO messages, this function is always
1295 * successful, and thus '*allowed_versions' is always initialized. However, it
1296 * returns false if 'oh' contains some data that could not be fully understood,
1297 * true if 'oh' was completely parsed. */
1299 ofputil_decode_hello(const struct ofp_header
*oh
, uint32_t *allowed_versions
)
1301 struct ofpbuf msg
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
1302 ofpbuf_pull(&msg
, sizeof *oh
);
1304 *allowed_versions
= version_bitmap_from_version(oh
->version
);
1308 const struct ofp_hello_elem_header
*oheh
;
1311 if (msg
.size
< sizeof *oheh
) {
1316 len
= ntohs(oheh
->length
);
1317 if (len
< sizeof *oheh
|| !ofpbuf_try_pull(&msg
, ROUND_UP(len
, 8))) {
1321 if (oheh
->type
!= htons(OFPHET_VERSIONBITMAP
)
1322 || !ofputil_decode_hello_bitmap(oheh
, allowed_versions
)) {
1330 /* Returns true if 'allowed_versions' needs to be accompanied by a version
1331 * bitmap to be correctly expressed in an OFPT_HELLO message. */
1333 should_send_version_bitmap(uint32_t allowed_versions
)
1335 return !is_pow2((allowed_versions
>> 1) + 1);
1338 /* Create an OFPT_HELLO message that expresses support for the OpenFlow
1339 * versions in the 'allowed_versions' bitmaps and returns the message. */
1341 ofputil_encode_hello(uint32_t allowed_versions
)
1343 enum ofp_version ofp_version
;
1346 ofp_version
= leftmost_1bit_idx(allowed_versions
);
1347 msg
= ofpraw_alloc(OFPRAW_OFPT_HELLO
, ofp_version
, 0);
1349 if (should_send_version_bitmap(allowed_versions
)) {
1350 struct ofp_hello_elem_header
*oheh
;
1353 map_len
= sizeof allowed_versions
;
1354 oheh
= ofpbuf_put_zeros(msg
, ROUND_UP(map_len
+ sizeof *oheh
, 8));
1355 oheh
->type
= htons(OFPHET_VERSIONBITMAP
);
1356 oheh
->length
= htons(map_len
+ sizeof *oheh
);
1357 *ALIGNED_CAST(ovs_be32
*, oheh
+ 1) = htonl(allowed_versions
);
1359 ofpmsg_update_length(msg
);
1365 /* Returns an OpenFlow message that, sent on an OpenFlow connection whose
1366 * protocol is 'current', at least partly transitions the protocol to 'want'.
1367 * Stores in '*next' the protocol that will be in effect on the OpenFlow
1368 * connection if the switch processes the returned message correctly. (If
1369 * '*next != want' then the caller will have to iterate.)
1371 * If 'current == want', or if it is not possible to transition from 'current'
1372 * to 'want' (because, for example, 'current' and 'want' use different OpenFlow
1373 * protocol versions), returns NULL and stores 'current' in '*next'. */
1375 ofputil_encode_set_protocol(enum ofputil_protocol current
,
1376 enum ofputil_protocol want
,
1377 enum ofputil_protocol
*next
)
1379 enum ofp_version cur_version
, want_version
;
1380 enum ofputil_protocol cur_base
, want_base
;
1381 bool cur_tid
, want_tid
;
1383 cur_version
= ofputil_protocol_to_ofp_version(current
);
1384 want_version
= ofputil_protocol_to_ofp_version(want
);
1385 if (cur_version
!= want_version
) {
1390 cur_base
= ofputil_protocol_to_base(current
);
1391 want_base
= ofputil_protocol_to_base(want
);
1392 if (cur_base
!= want_base
) {
1393 *next
= ofputil_protocol_set_base(current
, want_base
);
1395 switch (want_base
) {
1396 case OFPUTIL_P_OF10_NXM
:
1397 return ofputil_encode_nx_set_flow_format(NXFF_NXM
);
1399 case OFPUTIL_P_OF10_STD
:
1400 return ofputil_encode_nx_set_flow_format(NXFF_OPENFLOW10
);
1402 case OFPUTIL_P_OF11_STD
:
1403 case OFPUTIL_P_OF12_OXM
:
1404 case OFPUTIL_P_OF13_OXM
:
1405 case OFPUTIL_P_OF14_OXM
:
1406 case OFPUTIL_P_OF15_OXM
:
1407 case OFPUTIL_P_OF16_OXM
:
1408 /* There is only one variant of each OpenFlow 1.1+ protocol, and we
1409 * verified above that we're not trying to change versions. */
1412 case OFPUTIL_P_OF10_STD_TID
:
1413 case OFPUTIL_P_OF10_NXM_TID
:
1418 cur_tid
= (current
& OFPUTIL_P_TID
) != 0;
1419 want_tid
= (want
& OFPUTIL_P_TID
) != 0;
1420 if (cur_tid
!= want_tid
) {
1421 *next
= ofputil_protocol_set_tid(current
, want_tid
);
1422 return ofputil_make_flow_mod_table_id(want_tid
);
1425 ovs_assert(current
== want
);
1431 /* Returns an NXT_SET_FLOW_FORMAT message that can be used to set the flow
1432 * format to 'nxff'. */
1434 ofputil_encode_nx_set_flow_format(enum nx_flow_format nxff
)
1436 struct nx_set_flow_format
*sff
;
1439 ovs_assert(ofputil_nx_flow_format_is_valid(nxff
));
1441 msg
= ofpraw_alloc(OFPRAW_NXT_SET_FLOW_FORMAT
, OFP10_VERSION
, 0);
1442 sff
= ofpbuf_put_zeros(msg
, sizeof *sff
);
1443 sff
->format
= htonl(nxff
);
1448 /* Returns the base protocol if 'flow_format' is a valid NXFF_* value, false
1450 enum ofputil_protocol
1451 ofputil_nx_flow_format_to_protocol(enum nx_flow_format flow_format
)
1453 switch (flow_format
) {
1454 case NXFF_OPENFLOW10
:
1455 return OFPUTIL_P_OF10_STD
;
1458 return OFPUTIL_P_OF10_NXM
;
1465 /* Returns true if 'flow_format' is a valid NXFF_* value, false otherwise. */
1467 ofputil_nx_flow_format_is_valid(enum nx_flow_format flow_format
)
1469 return ofputil_nx_flow_format_to_protocol(flow_format
) != 0;
1472 /* Returns a string version of 'flow_format', which must be a valid NXFF_*
1475 ofputil_nx_flow_format_to_string(enum nx_flow_format flow_format
)
1477 switch (flow_format
) {
1478 case NXFF_OPENFLOW10
:
1479 return "openflow10";
1488 ofputil_make_set_packet_in_format(enum ofp_version ofp_version
,
1489 enum nx_packet_in_format packet_in_format
)
1491 struct nx_set_packet_in_format
*spif
;
1494 msg
= ofpraw_alloc(OFPRAW_NXT_SET_PACKET_IN_FORMAT
, ofp_version
, 0);
1495 spif
= ofpbuf_put_zeros(msg
, sizeof *spif
);
1496 spif
->format
= htonl(packet_in_format
);
1501 /* Returns an OpenFlow message that can be used to turn the flow_mod_table_id
1502 * extension on or off (according to 'flow_mod_table_id'). */
1504 ofputil_make_flow_mod_table_id(bool flow_mod_table_id
)
1506 struct nx_flow_mod_table_id
*nfmti
;
1509 msg
= ofpraw_alloc(OFPRAW_NXT_FLOW_MOD_TABLE_ID
, OFP10_VERSION
, 0);
1510 nfmti
= ofpbuf_put_zeros(msg
, sizeof *nfmti
);
1511 nfmti
->set
= flow_mod_table_id
;
1515 struct ofputil_flow_mod_flag
{
1517 enum ofp_version min_version
, max_version
;
1518 enum ofputil_flow_mod_flags flag
;
1521 static const struct ofputil_flow_mod_flag ofputil_flow_mod_flags
[] = {
1522 { OFPFF_SEND_FLOW_REM
, OFP10_VERSION
, 0, OFPUTIL_FF_SEND_FLOW_REM
},
1523 { OFPFF_CHECK_OVERLAP
, OFP10_VERSION
, 0, OFPUTIL_FF_CHECK_OVERLAP
},
1524 { OFPFF10_EMERG
, OFP10_VERSION
, OFP10_VERSION
,
1526 { OFPFF12_RESET_COUNTS
, OFP12_VERSION
, 0, OFPUTIL_FF_RESET_COUNTS
},
1527 { OFPFF13_NO_PKT_COUNTS
, OFP13_VERSION
, 0, OFPUTIL_FF_NO_PKT_COUNTS
},
1528 { OFPFF13_NO_BYT_COUNTS
, OFP13_VERSION
, 0, OFPUTIL_FF_NO_BYT_COUNTS
},
1533 ofputil_decode_flow_mod_flags(ovs_be16 raw_flags_
,
1534 enum ofp_flow_mod_command command
,
1535 enum ofp_version version
,
1536 enum ofputil_flow_mod_flags
*flagsp
)
1538 uint16_t raw_flags
= ntohs(raw_flags_
);
1539 const struct ofputil_flow_mod_flag
*f
;
1542 for (f
= ofputil_flow_mod_flags
; f
->raw_flag
; f
++) {
1543 if (raw_flags
& f
->raw_flag
1544 && version
>= f
->min_version
1545 && (!f
->max_version
|| version
<= f
->max_version
)) {
1546 raw_flags
&= ~f
->raw_flag
;
1551 /* In OF1.0 and OF1.1, "add" always resets counters, and other commands
1554 * In OF1.2 and later, OFPFF12_RESET_COUNTS controls whether each command
1555 * resets counters. */
1556 if ((version
== OFP10_VERSION
|| version
== OFP11_VERSION
)
1557 && command
== OFPFC_ADD
) {
1558 *flagsp
|= OFPUTIL_FF_RESET_COUNTS
;
1561 return raw_flags
? OFPERR_OFPFMFC_BAD_FLAGS
: 0;
1565 ofputil_encode_flow_mod_flags(enum ofputil_flow_mod_flags flags
,
1566 enum ofp_version version
)
1568 const struct ofputil_flow_mod_flag
*f
;
1572 for (f
= ofputil_flow_mod_flags
; f
->raw_flag
; f
++) {
1574 && version
>= f
->min_version
1575 && (!f
->max_version
|| version
<= f
->max_version
)) {
1576 raw_flags
|= f
->raw_flag
;
1580 return htons(raw_flags
);
1583 /* Converts an OFPT_FLOW_MOD or NXT_FLOW_MOD message 'oh' into an abstract
1584 * flow_mod in 'fm'. Returns 0 if successful, otherwise an OpenFlow error
1587 * Uses 'ofpacts' to store the abstract OFPACT_* version of 'oh''s actions.
1588 * The caller must initialize 'ofpacts' and retains ownership of it.
1589 * 'fm->ofpacts' will point into the 'ofpacts' buffer.
1591 * Does not validate the flow_mod actions. The caller should do that, with
1592 * ofpacts_check(). */
1594 ofputil_decode_flow_mod(struct ofputil_flow_mod
*fm
,
1595 const struct ofp_header
*oh
,
1596 enum ofputil_protocol protocol
,
1597 const struct tun_table
*tun_table
,
1598 const struct vl_mff_map
*vl_mff_map
,
1599 struct ofpbuf
*ofpacts
,
1600 ofp_port_t max_port
, uint8_t max_table
)
1604 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
1605 enum ofpraw raw
= ofpraw_pull_assert(&b
);
1606 if (raw
== OFPRAW_OFPT11_FLOW_MOD
) {
1607 /* Standard OpenFlow 1.1+ flow_mod. */
1608 const struct ofp11_flow_mod
*ofm
;
1610 ofm
= ofpbuf_pull(&b
, sizeof *ofm
);
1612 error
= ofputil_pull_ofp11_match(&b
, tun_table
, vl_mff_map
, &fm
->match
,
1618 /* Translate the message. */
1619 fm
->priority
= ntohs(ofm
->priority
);
1620 if (ofm
->command
== OFPFC_ADD
1621 || (oh
->version
== OFP11_VERSION
1622 && (ofm
->command
== OFPFC_MODIFY
||
1623 ofm
->command
== OFPFC_MODIFY_STRICT
)
1624 && ofm
->cookie_mask
== htonll(0))) {
1625 /* In OpenFlow 1.1 only, a "modify" or "modify-strict" that does
1626 * not match on the cookie is treated as an "add" if there is no
1628 fm
->cookie
= htonll(0);
1629 fm
->cookie_mask
= htonll(0);
1630 fm
->new_cookie
= ofm
->cookie
;
1632 fm
->cookie
= ofm
->cookie
;
1633 fm
->cookie_mask
= ofm
->cookie_mask
;
1634 fm
->new_cookie
= OVS_BE64_MAX
;
1636 fm
->modify_cookie
= false;
1637 fm
->command
= ofm
->command
;
1641 * OF1.1 entirely forbids table_id == OFPTT_ALL.
1642 * OF1.2+ allows table_id == OFPTT_ALL only for deletes. */
1643 fm
->table_id
= ofm
->table_id
;
1644 if (fm
->table_id
== OFPTT_ALL
1645 && (oh
->version
== OFP11_VERSION
1646 || (ofm
->command
!= OFPFC_DELETE
&&
1647 ofm
->command
!= OFPFC_DELETE_STRICT
))) {
1648 return OFPERR_OFPFMFC_BAD_TABLE_ID
;
1651 fm
->idle_timeout
= ntohs(ofm
->idle_timeout
);
1652 fm
->hard_timeout
= ntohs(ofm
->hard_timeout
);
1653 if (oh
->version
>= OFP14_VERSION
&& ofm
->command
== OFPFC_ADD
) {
1654 fm
->importance
= ntohs(ofm
->importance
);
1658 fm
->buffer_id
= ntohl(ofm
->buffer_id
);
1659 error
= ofputil_port_from_ofp11(ofm
->out_port
, &fm
->out_port
);
1664 fm
->out_group
= (ofm
->command
== OFPFC_DELETE
||
1665 ofm
->command
== OFPFC_DELETE_STRICT
1666 ? ntohl(ofm
->out_group
)
1668 raw_flags
= ofm
->flags
;
1672 if (raw
== OFPRAW_OFPT10_FLOW_MOD
) {
1673 /* Standard OpenFlow 1.0 flow_mod. */
1674 const struct ofp10_flow_mod
*ofm
;
1676 /* Get the ofp10_flow_mod. */
1677 ofm
= ofpbuf_pull(&b
, sizeof *ofm
);
1679 /* Translate the rule. */
1680 ofputil_match_from_ofp10_match(&ofm
->match
, &fm
->match
);
1681 ofputil_normalize_match(&fm
->match
);
1683 /* OpenFlow 1.0 says that exact-match rules have to have the
1684 * highest possible priority. */
1685 fm
->priority
= (ofm
->match
.wildcards
& htonl(OFPFW10_ALL
)
1686 ? ntohs(ofm
->priority
)
1689 /* Translate the message. */
1690 command
= ntohs(ofm
->command
);
1691 fm
->cookie
= htonll(0);
1692 fm
->cookie_mask
= htonll(0);
1693 fm
->new_cookie
= ofm
->cookie
;
1694 fm
->idle_timeout
= ntohs(ofm
->idle_timeout
);
1695 fm
->hard_timeout
= ntohs(ofm
->hard_timeout
);
1697 fm
->buffer_id
= ntohl(ofm
->buffer_id
);
1698 fm
->out_port
= u16_to_ofp(ntohs(ofm
->out_port
));
1699 fm
->out_group
= OFPG_ANY
;
1700 raw_flags
= ofm
->flags
;
1701 } else if (raw
== OFPRAW_NXT_FLOW_MOD
) {
1702 /* Nicira extended flow_mod. */
1703 const struct nx_flow_mod
*nfm
;
1705 /* Dissect the message. */
1706 nfm
= ofpbuf_pull(&b
, sizeof *nfm
);
1707 error
= nx_pull_match(&b
, ntohs(nfm
->match_len
),
1708 &fm
->match
, &fm
->cookie
, &fm
->cookie_mask
,
1709 false, tun_table
, vl_mff_map
);
1714 /* Translate the message. */
1715 command
= ntohs(nfm
->command
);
1716 if ((command
& 0xff) == OFPFC_ADD
&& fm
->cookie_mask
) {
1717 /* Flow additions may only set a new cookie, not match an
1718 * existing cookie. */
1719 return OFPERR_NXBRC_NXM_INVALID
;
1721 fm
->priority
= ntohs(nfm
->priority
);
1722 fm
->new_cookie
= nfm
->cookie
;
1723 fm
->idle_timeout
= ntohs(nfm
->idle_timeout
);
1724 fm
->hard_timeout
= ntohs(nfm
->hard_timeout
);
1726 fm
->buffer_id
= ntohl(nfm
->buffer_id
);
1727 fm
->out_port
= u16_to_ofp(ntohs(nfm
->out_port
));
1728 fm
->out_group
= OFPG_ANY
;
1729 raw_flags
= nfm
->flags
;
1734 fm
->modify_cookie
= fm
->new_cookie
!= OVS_BE64_MAX
;
1735 if (protocol
& OFPUTIL_P_TID
) {
1736 fm
->command
= command
& 0xff;
1737 fm
->table_id
= command
>> 8;
1739 if (command
> 0xff) {
1740 VLOG_WARN_RL(&bad_ofmsg_rl
, "flow_mod has explicit table_id "
1741 "but flow_mod_table_id extension is not enabled");
1743 fm
->command
= command
;
1744 fm
->table_id
= 0xff;
1748 /* Check for mismatched conntrack original direction tuple address fields
1749 * w.r.t. the IP version of the match. */
1750 if (((fm
->match
.wc
.masks
.ct_nw_src
|| fm
->match
.wc
.masks
.ct_nw_dst
)
1751 && fm
->match
.flow
.dl_type
!= htons(ETH_TYPE_IP
))
1752 || ((ipv6_addr_is_set(&fm
->match
.wc
.masks
.ct_ipv6_src
)
1753 || ipv6_addr_is_set(&fm
->match
.wc
.masks
.ct_ipv6_dst
))
1754 && fm
->match
.flow
.dl_type
!= htons(ETH_TYPE_IPV6
))) {
1755 return OFPERR_OFPBAC_MATCH_INCONSISTENT
;
1758 if (fm
->command
> OFPFC_DELETE_STRICT
) {
1759 return OFPERR_OFPFMFC_BAD_COMMAND
;
1762 fm
->ofpacts_tlv_bitmap
= 0;
1763 error
= ofpacts_pull_openflow_instructions(&b
, b
.size
, oh
->version
,
1765 &fm
->ofpacts_tlv_bitmap
,
1770 fm
->ofpacts
= ofpacts
->data
;
1771 fm
->ofpacts_len
= ofpacts
->size
;
1773 error
= ofputil_decode_flow_mod_flags(raw_flags
, fm
->command
,
1774 oh
->version
, &fm
->flags
);
1779 if (fm
->flags
& OFPUTIL_FF_EMERG
) {
1780 /* We do not support the OpenFlow 1.0 emergency flow cache, which
1781 * is not required in OpenFlow 1.0.1 and removed from OpenFlow 1.1.
1783 * OpenFlow 1.0 specifies the error code to use when idle_timeout
1784 * or hard_timeout is nonzero. Otherwise, there is no good error
1785 * code, so just state that the flow table is full. */
1786 return (fm
->hard_timeout
|| fm
->idle_timeout
1787 ? OFPERR_OFPFMFC_BAD_EMERG_TIMEOUT
1788 : OFPERR_OFPFMFC_TABLE_FULL
);
1791 return ofpacts_check_consistency(fm
->ofpacts
, fm
->ofpacts_len
,
1792 &fm
->match
, max_port
,
1793 fm
->table_id
, max_table
, protocol
);
1797 ofputil_pull_bands(struct ofpbuf
*msg
, size_t len
, uint16_t *n_bands
,
1798 struct ofpbuf
*bands
)
1800 const struct ofp13_meter_band_header
*ombh
;
1801 struct ofputil_meter_band
*mb
;
1804 ombh
= ofpbuf_try_pull(msg
, len
);
1806 return OFPERR_OFPBRC_BAD_LEN
;
1809 while (len
>= sizeof (struct ofp13_meter_band_drop
)) {
1810 size_t ombh_len
= ntohs(ombh
->len
);
1811 /* All supported band types have the same length. */
1812 if (ombh_len
!= sizeof (struct ofp13_meter_band_drop
)) {
1813 return OFPERR_OFPBRC_BAD_LEN
;
1815 mb
= ofpbuf_put_uninit(bands
, sizeof *mb
);
1816 mb
->type
= ntohs(ombh
->type
);
1817 if (mb
->type
!= OFPMBT13_DROP
&& mb
->type
!= OFPMBT13_DSCP_REMARK
) {
1818 return OFPERR_OFPMMFC_BAD_BAND
;
1820 mb
->rate
= ntohl(ombh
->rate
);
1821 mb
->burst_size
= ntohl(ombh
->burst_size
);
1822 mb
->prec_level
= (mb
->type
== OFPMBT13_DSCP_REMARK
) ?
1823 ((struct ofp13_meter_band_dscp_remark
*)ombh
)->prec_level
: 0;
1826 ombh
= ALIGNED_CAST(struct ofp13_meter_band_header
*,
1827 (char *) ombh
+ ombh_len
);
1830 return OFPERR_OFPBRC_BAD_LEN
;
1837 ofputil_decode_meter_mod(const struct ofp_header
*oh
,
1838 struct ofputil_meter_mod
*mm
,
1839 struct ofpbuf
*bands
)
1841 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
1842 ofpraw_pull_assert(&b
);
1843 const struct ofp13_meter_mod
*omm
= ofpbuf_pull(&b
, sizeof *omm
);
1845 /* Translate the message. */
1846 mm
->command
= ntohs(omm
->command
);
1847 if (mm
->command
!= OFPMC13_ADD
&&
1848 mm
->command
!= OFPMC13_MODIFY
&&
1849 mm
->command
!= OFPMC13_DELETE
) {
1850 return OFPERR_OFPMMFC_BAD_COMMAND
;
1852 mm
->meter
.meter_id
= ntohl(omm
->meter_id
);
1854 if (mm
->command
== OFPMC13_DELETE
) {
1855 mm
->meter
.flags
= 0;
1856 mm
->meter
.n_bands
= 0;
1857 mm
->meter
.bands
= NULL
;
1861 mm
->meter
.flags
= ntohs(omm
->flags
);
1862 if (mm
->meter
.flags
& OFPMF13_KBPS
&&
1863 mm
->meter
.flags
& OFPMF13_PKTPS
) {
1864 return OFPERR_OFPMMFC_BAD_FLAGS
;
1866 mm
->meter
.bands
= bands
->data
;
1868 error
= ofputil_pull_bands(&b
, b
.size
, &mm
->meter
.n_bands
, bands
);
1877 ofputil_decode_meter_request(const struct ofp_header
*oh
, uint32_t *meter_id
)
1879 const struct ofp13_meter_multipart_request
*omr
= ofpmsg_body(oh
);
1880 *meter_id
= ntohl(omr
->meter_id
);
1884 ofputil_encode_meter_request(enum ofp_version ofp_version
,
1885 enum ofputil_meter_request_type type
,
1893 case OFPUTIL_METER_CONFIG
:
1894 raw
= OFPRAW_OFPST13_METER_CONFIG_REQUEST
;
1896 case OFPUTIL_METER_STATS
:
1897 raw
= OFPRAW_OFPST13_METER_REQUEST
;
1900 case OFPUTIL_METER_FEATURES
:
1901 raw
= OFPRAW_OFPST13_METER_FEATURES_REQUEST
;
1905 msg
= ofpraw_alloc(raw
, ofp_version
, 0);
1907 if (type
!= OFPUTIL_METER_FEATURES
) {
1908 struct ofp13_meter_multipart_request
*omr
;
1909 omr
= ofpbuf_put_zeros(msg
, sizeof *omr
);
1910 omr
->meter_id
= htonl(meter_id
);
1916 ofputil_put_bands(uint16_t n_bands
, const struct ofputil_meter_band
*mb
,
1921 for (n
= 0; n
< n_bands
; ++n
) {
1922 /* Currently all band types have same size. */
1923 struct ofp13_meter_band_dscp_remark
*ombh
;
1924 size_t ombh_len
= sizeof *ombh
;
1926 ombh
= ofpbuf_put_zeros(msg
, ombh_len
);
1928 ombh
->type
= htons(mb
->type
);
1929 ombh
->len
= htons(ombh_len
);
1930 ombh
->rate
= htonl(mb
->rate
);
1931 ombh
->burst_size
= htonl(mb
->burst_size
);
1932 ombh
->prec_level
= mb
->prec_level
;
1938 /* Encode a meter stat for 'mc' and append it to 'replies'. */
1940 ofputil_append_meter_config(struct ovs_list
*replies
,
1941 const struct ofputil_meter_config
*mc
)
1943 struct ofpbuf
*msg
= ofpbuf_from_list(ovs_list_back(replies
));
1944 size_t start_ofs
= msg
->size
;
1945 struct ofp13_meter_config
*reply
;
1947 ofpbuf_put_uninit(msg
, sizeof *reply
);
1948 ofputil_put_bands(mc
->n_bands
, mc
->bands
, msg
);
1950 reply
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *reply
);
1951 reply
->flags
= htons(mc
->flags
);
1952 reply
->meter_id
= htonl(mc
->meter_id
);
1953 reply
->length
= htons(msg
->size
- start_ofs
);
1955 ofpmp_postappend(replies
, start_ofs
);
1958 /* Encode a meter stat for 'ms' and append it to 'replies'. */
1960 ofputil_append_meter_stats(struct ovs_list
*replies
,
1961 const struct ofputil_meter_stats
*ms
)
1963 struct ofp13_meter_stats
*reply
;
1967 len
= sizeof *reply
+ ms
->n_bands
* sizeof(struct ofp13_meter_band_stats
);
1968 reply
= ofpmp_append(replies
, len
);
1970 reply
->meter_id
= htonl(ms
->meter_id
);
1971 reply
->len
= htons(len
);
1972 memset(reply
->pad
, 0, sizeof reply
->pad
);
1973 reply
->flow_count
= htonl(ms
->flow_count
);
1974 reply
->packet_in_count
= htonll(ms
->packet_in_count
);
1975 reply
->byte_in_count
= htonll(ms
->byte_in_count
);
1976 reply
->duration_sec
= htonl(ms
->duration_sec
);
1977 reply
->duration_nsec
= htonl(ms
->duration_nsec
);
1979 for (n
= 0; n
< ms
->n_bands
; ++n
) {
1980 const struct ofputil_meter_band_stats
*src
= &ms
->bands
[n
];
1981 struct ofp13_meter_band_stats
*dst
= &reply
->band_stats
[n
];
1983 dst
->packet_band_count
= htonll(src
->packet_count
);
1984 dst
->byte_band_count
= htonll(src
->byte_count
);
1988 /* Converts an OFPMP_METER_CONFIG reply in 'msg' into an abstract
1989 * ofputil_meter_config in 'mc', with mc->bands pointing to bands decoded into
1990 * 'bands'. The caller must have initialized 'bands' and retains ownership of
1991 * it across the call.
1993 * Multiple OFPST13_METER_CONFIG replies can be packed into a single OpenFlow
1994 * message. Calling this function multiple times for a single 'msg' iterates
1995 * through the replies. 'bands' is cleared for each reply.
1997 * Returns 0 if successful, EOF if no replies were left in this 'msg',
1998 * otherwise a positive errno value. */
2000 ofputil_decode_meter_config(struct ofpbuf
*msg
,
2001 struct ofputil_meter_config
*mc
,
2002 struct ofpbuf
*bands
)
2004 const struct ofp13_meter_config
*omc
;
2007 /* Pull OpenFlow headers for the first call. */
2009 ofpraw_pull_assert(msg
);
2016 omc
= ofpbuf_try_pull(msg
, sizeof *omc
);
2018 VLOG_WARN_RL(&bad_ofmsg_rl
,
2019 "OFPMP_METER_CONFIG reply has %"PRIu32
" leftover bytes at end",
2021 return OFPERR_OFPBRC_BAD_LEN
;
2024 ofpbuf_clear(bands
);
2025 err
= ofputil_pull_bands(msg
, ntohs(omc
->length
) - sizeof *omc
,
2026 &mc
->n_bands
, bands
);
2030 mc
->meter_id
= ntohl(omc
->meter_id
);
2031 mc
->flags
= ntohs(omc
->flags
);
2032 mc
->bands
= bands
->data
;
2038 ofputil_pull_band_stats(struct ofpbuf
*msg
, size_t len
, uint16_t *n_bands
,
2039 struct ofpbuf
*bands
)
2041 const struct ofp13_meter_band_stats
*ombs
;
2042 struct ofputil_meter_band_stats
*mbs
;
2045 ombs
= ofpbuf_try_pull(msg
, len
);
2047 return OFPERR_OFPBRC_BAD_LEN
;
2050 n
= len
/ sizeof *ombs
;
2051 if (len
!= n
* sizeof *ombs
) {
2052 return OFPERR_OFPBRC_BAD_LEN
;
2055 mbs
= ofpbuf_put_uninit(bands
, len
);
2057 for (i
= 0; i
< n
; ++i
) {
2058 mbs
[i
].packet_count
= ntohll(ombs
[i
].packet_band_count
);
2059 mbs
[i
].byte_count
= ntohll(ombs
[i
].byte_band_count
);
2065 /* Converts an OFPMP_METER reply in 'msg' into an abstract
2066 * ofputil_meter_stats in 'ms', with ms->bands pointing to band stats
2067 * decoded into 'bands'.
2069 * Multiple OFPMP_METER replies can be packed into a single OpenFlow
2070 * message. Calling this function multiple times for a single 'msg' iterates
2071 * through the replies. 'bands' is cleared for each reply.
2073 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2074 * otherwise a positive errno value. */
2076 ofputil_decode_meter_stats(struct ofpbuf
*msg
,
2077 struct ofputil_meter_stats
*ms
,
2078 struct ofpbuf
*bands
)
2080 const struct ofp13_meter_stats
*oms
;
2083 /* Pull OpenFlow headers for the first call. */
2085 ofpraw_pull_assert(msg
);
2092 oms
= ofpbuf_try_pull(msg
, sizeof *oms
);
2094 VLOG_WARN_RL(&bad_ofmsg_rl
,
2095 "OFPMP_METER reply has %"PRIu32
" leftover bytes at end",
2097 return OFPERR_OFPBRC_BAD_LEN
;
2100 ofpbuf_clear(bands
);
2101 err
= ofputil_pull_band_stats(msg
, ntohs(oms
->len
) - sizeof *oms
,
2102 &ms
->n_bands
, bands
);
2106 ms
->meter_id
= ntohl(oms
->meter_id
);
2107 ms
->flow_count
= ntohl(oms
->flow_count
);
2108 ms
->packet_in_count
= ntohll(oms
->packet_in_count
);
2109 ms
->byte_in_count
= ntohll(oms
->byte_in_count
);
2110 ms
->duration_sec
= ntohl(oms
->duration_sec
);
2111 ms
->duration_nsec
= ntohl(oms
->duration_nsec
);
2112 ms
->bands
= bands
->data
;
2118 ofputil_decode_meter_features(const struct ofp_header
*oh
,
2119 struct ofputil_meter_features
*mf
)
2121 const struct ofp13_meter_features
*omf
= ofpmsg_body(oh
);
2123 mf
->max_meters
= ntohl(omf
->max_meter
);
2124 mf
->band_types
= ntohl(omf
->band_types
);
2125 mf
->capabilities
= ntohl(omf
->capabilities
);
2126 mf
->max_bands
= omf
->max_bands
;
2127 mf
->max_color
= omf
->max_color
;
2131 ofputil_encode_meter_features_reply(const struct ofputil_meter_features
*mf
,
2132 const struct ofp_header
*request
)
2134 struct ofpbuf
*reply
;
2135 struct ofp13_meter_features
*omf
;
2137 reply
= ofpraw_alloc_stats_reply(request
, 0);
2138 omf
= ofpbuf_put_zeros(reply
, sizeof *omf
);
2140 omf
->max_meter
= htonl(mf
->max_meters
);
2141 omf
->band_types
= htonl(mf
->band_types
);
2142 omf
->capabilities
= htonl(mf
->capabilities
);
2143 omf
->max_bands
= mf
->max_bands
;
2144 omf
->max_color
= mf
->max_color
;
2150 ofputil_encode_meter_mod(enum ofp_version ofp_version
,
2151 const struct ofputil_meter_mod
*mm
)
2155 struct ofp13_meter_mod
*omm
;
2157 msg
= ofpraw_alloc(OFPRAW_OFPT13_METER_MOD
, ofp_version
,
2158 NXM_TYPICAL_LEN
+ mm
->meter
.n_bands
* 16);
2159 omm
= ofpbuf_put_zeros(msg
, sizeof *omm
);
2160 omm
->command
= htons(mm
->command
);
2161 if (mm
->command
!= OFPMC13_DELETE
) {
2162 omm
->flags
= htons(mm
->meter
.flags
);
2164 omm
->meter_id
= htonl(mm
->meter
.meter_id
);
2166 ofputil_put_bands(mm
->meter
.n_bands
, mm
->meter
.bands
, msg
);
2168 ofpmsg_update_length(msg
);
2173 ofputil_tid_command(const struct ofputil_flow_mod
*fm
,
2174 enum ofputil_protocol protocol
)
2176 return htons(protocol
& OFPUTIL_P_TID
2177 ? (fm
->command
& 0xff) | (fm
->table_id
<< 8)
2181 /* Converts 'fm' into an OFPT_FLOW_MOD or NXT_FLOW_MOD message according to
2182 * 'protocol' and returns the message. */
2184 ofputil_encode_flow_mod(const struct ofputil_flow_mod
*fm
,
2185 enum ofputil_protocol protocol
)
2187 enum ofp_version version
= ofputil_protocol_to_ofp_version(protocol
);
2188 ovs_be16 raw_flags
= ofputil_encode_flow_mod_flags(fm
->flags
, version
);
2192 case OFPUTIL_P_OF11_STD
:
2193 case OFPUTIL_P_OF12_OXM
:
2194 case OFPUTIL_P_OF13_OXM
:
2195 case OFPUTIL_P_OF14_OXM
:
2196 case OFPUTIL_P_OF15_OXM
:
2197 case OFPUTIL_P_OF16_OXM
: {
2198 struct ofp11_flow_mod
*ofm
;
2201 tailroom
= ofputil_match_typical_len(protocol
) + fm
->ofpacts_len
;
2202 msg
= ofpraw_alloc(OFPRAW_OFPT11_FLOW_MOD
, version
, tailroom
);
2203 ofm
= ofpbuf_put_zeros(msg
, sizeof *ofm
);
2204 if ((protocol
== OFPUTIL_P_OF11_STD
2205 && (fm
->command
== OFPFC_MODIFY
||
2206 fm
->command
== OFPFC_MODIFY_STRICT
)
2207 && fm
->cookie_mask
== htonll(0))
2208 || fm
->command
== OFPFC_ADD
) {
2209 ofm
->cookie
= fm
->new_cookie
;
2211 ofm
->cookie
= fm
->cookie
& fm
->cookie_mask
;
2213 ofm
->cookie_mask
= fm
->cookie_mask
;
2214 if (fm
->table_id
!= OFPTT_ALL
2215 || (protocol
!= OFPUTIL_P_OF11_STD
2216 && (fm
->command
== OFPFC_DELETE
||
2217 fm
->command
== OFPFC_DELETE_STRICT
))) {
2218 ofm
->table_id
= fm
->table_id
;
2222 ofm
->command
= fm
->command
;
2223 ofm
->idle_timeout
= htons(fm
->idle_timeout
);
2224 ofm
->hard_timeout
= htons(fm
->hard_timeout
);
2225 ofm
->priority
= htons(fm
->priority
);
2226 ofm
->buffer_id
= htonl(fm
->buffer_id
);
2227 ofm
->out_port
= ofputil_port_to_ofp11(fm
->out_port
);
2228 ofm
->out_group
= htonl(fm
->out_group
);
2229 ofm
->flags
= raw_flags
;
2230 if (version
>= OFP14_VERSION
&& fm
->command
== OFPFC_ADD
) {
2231 ofm
->importance
= htons(fm
->importance
);
2233 ofm
->importance
= 0;
2235 ofputil_put_ofp11_match(msg
, &fm
->match
, protocol
);
2236 ofpacts_put_openflow_instructions(fm
->ofpacts
, fm
->ofpacts_len
, msg
,
2241 case OFPUTIL_P_OF10_STD
:
2242 case OFPUTIL_P_OF10_STD_TID
: {
2243 struct ofp10_flow_mod
*ofm
;
2245 msg
= ofpraw_alloc(OFPRAW_OFPT10_FLOW_MOD
, OFP10_VERSION
,
2247 ofm
= ofpbuf_put_zeros(msg
, sizeof *ofm
);
2248 ofputil_match_to_ofp10_match(&fm
->match
, &ofm
->match
);
2249 ofm
->cookie
= fm
->new_cookie
;
2250 ofm
->command
= ofputil_tid_command(fm
, protocol
);
2251 ofm
->idle_timeout
= htons(fm
->idle_timeout
);
2252 ofm
->hard_timeout
= htons(fm
->hard_timeout
);
2253 ofm
->priority
= htons(fm
->priority
);
2254 ofm
->buffer_id
= htonl(fm
->buffer_id
);
2255 ofm
->out_port
= htons(ofp_to_u16(fm
->out_port
));
2256 ofm
->flags
= raw_flags
;
2257 ofpacts_put_openflow_actions(fm
->ofpacts
, fm
->ofpacts_len
, msg
,
2262 case OFPUTIL_P_OF10_NXM
:
2263 case OFPUTIL_P_OF10_NXM_TID
: {
2264 struct nx_flow_mod
*nfm
;
2267 msg
= ofpraw_alloc(OFPRAW_NXT_FLOW_MOD
, OFP10_VERSION
,
2268 NXM_TYPICAL_LEN
+ fm
->ofpacts_len
);
2269 nfm
= ofpbuf_put_zeros(msg
, sizeof *nfm
);
2270 nfm
->command
= ofputil_tid_command(fm
, protocol
);
2271 nfm
->cookie
= fm
->new_cookie
;
2272 match_len
= nx_put_match(msg
, &fm
->match
, fm
->cookie
, fm
->cookie_mask
);
2274 nfm
->idle_timeout
= htons(fm
->idle_timeout
);
2275 nfm
->hard_timeout
= htons(fm
->hard_timeout
);
2276 nfm
->priority
= htons(fm
->priority
);
2277 nfm
->buffer_id
= htonl(fm
->buffer_id
);
2278 nfm
->out_port
= htons(ofp_to_u16(fm
->out_port
));
2279 nfm
->flags
= raw_flags
;
2280 nfm
->match_len
= htons(match_len
);
2281 ofpacts_put_openflow_actions(fm
->ofpacts
, fm
->ofpacts_len
, msg
,
2290 ofpmsg_update_length(msg
);
2295 ofputil_decode_ofpst10_flow_request(struct ofputil_flow_stats_request
*fsr
,
2296 const struct ofp10_flow_stats_request
*ofsr
,
2299 fsr
->aggregate
= aggregate
;
2300 ofputil_match_from_ofp10_match(&ofsr
->match
, &fsr
->match
);
2301 fsr
->out_port
= u16_to_ofp(ntohs(ofsr
->out_port
));
2302 fsr
->out_group
= OFPG_ANY
;
2303 fsr
->table_id
= ofsr
->table_id
;
2304 fsr
->cookie
= fsr
->cookie_mask
= htonll(0);
2310 ofputil_decode_ofpst11_flow_request(struct ofputil_flow_stats_request
*fsr
,
2311 struct ofpbuf
*b
, bool aggregate
,
2312 const struct tun_table
*tun_table
,
2313 const struct vl_mff_map
*vl_mff_map
)
2315 const struct ofp11_flow_stats_request
*ofsr
;
2318 ofsr
= ofpbuf_pull(b
, sizeof *ofsr
);
2319 fsr
->aggregate
= aggregate
;
2320 fsr
->table_id
= ofsr
->table_id
;
2321 error
= ofputil_port_from_ofp11(ofsr
->out_port
, &fsr
->out_port
);
2325 fsr
->out_group
= ntohl(ofsr
->out_group
);
2326 fsr
->cookie
= ofsr
->cookie
;
2327 fsr
->cookie_mask
= ofsr
->cookie_mask
;
2328 error
= ofputil_pull_ofp11_match(b
, tun_table
, vl_mff_map
, &fsr
->match
,
2338 ofputil_decode_nxst_flow_request(struct ofputil_flow_stats_request
*fsr
,
2339 struct ofpbuf
*b
, bool aggregate
,
2340 const struct tun_table
*tun_table
,
2341 const struct vl_mff_map
*vl_mff_map
)
2343 const struct nx_flow_stats_request
*nfsr
;
2346 nfsr
= ofpbuf_pull(b
, sizeof *nfsr
);
2347 error
= nx_pull_match(b
, ntohs(nfsr
->match_len
), &fsr
->match
,
2348 &fsr
->cookie
, &fsr
->cookie_mask
, false, tun_table
,
2354 return OFPERR_OFPBRC_BAD_LEN
;
2357 fsr
->aggregate
= aggregate
;
2358 fsr
->out_port
= u16_to_ofp(ntohs(nfsr
->out_port
));
2359 fsr
->out_group
= OFPG_ANY
;
2360 fsr
->table_id
= nfsr
->table_id
;
2365 /* Constructs and returns an OFPT_QUEUE_GET_CONFIG request for the specified
2366 * 'port' and 'queue', suitable for OpenFlow version 'version'.
2368 * 'queue' is honored only for OpenFlow 1.4 and later; older versions always
2369 * request all queues. */
2371 ofputil_encode_queue_get_config_request(enum ofp_version version
,
2375 struct ofpbuf
*request
;
2377 if (version
== OFP10_VERSION
) {
2378 struct ofp10_queue_get_config_request
*qgcr10
;
2380 request
= ofpraw_alloc(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST
,
2382 qgcr10
= ofpbuf_put_zeros(request
, sizeof *qgcr10
);
2383 qgcr10
->port
= htons(ofp_to_u16(port
));
2384 } else if (version
< OFP14_VERSION
) {
2385 struct ofp11_queue_get_config_request
*qgcr11
;
2387 request
= ofpraw_alloc(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST
,
2389 qgcr11
= ofpbuf_put_zeros(request
, sizeof *qgcr11
);
2390 qgcr11
->port
= ofputil_port_to_ofp11(port
);
2392 struct ofp14_queue_desc_request
*qdr14
;
2394 request
= ofpraw_alloc(OFPRAW_OFPST14_QUEUE_DESC_REQUEST
,
2396 qdr14
= ofpbuf_put_zeros(request
, sizeof *qdr14
);
2397 qdr14
->port
= ofputil_port_to_ofp11(port
);
2398 qdr14
->queue
= htonl(queue
);
2404 /* Parses OFPT_QUEUE_GET_CONFIG request 'oh', storing the port specified by the
2405 * request into '*port'. Returns 0 if successful, otherwise an OpenFlow error
2408 ofputil_decode_queue_get_config_request(const struct ofp_header
*oh
,
2409 ofp_port_t
*port
, uint32_t *queue
)
2411 const struct ofp10_queue_get_config_request
*qgcr10
;
2412 const struct ofp11_queue_get_config_request
*qgcr11
;
2413 const struct ofp14_queue_desc_request
*qdr14
;
2414 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
2415 enum ofpraw raw
= ofpraw_pull_assert(&b
);
2417 switch ((int) raw
) {
2418 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST
:
2420 *port
= u16_to_ofp(ntohs(qgcr10
->port
));
2424 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST
:
2427 enum ofperr error
= ofputil_port_from_ofp11(qgcr11
->port
, port
);
2428 if (error
|| *port
== OFPP_ANY
) {
2433 case OFPRAW_OFPST14_QUEUE_DESC_REQUEST
:
2435 *queue
= ntohl(qdr14
->queue
);
2436 return ofputil_port_from_ofp11(qdr14
->port
, port
);
2442 return (ofp_to_u16(*port
) < ofp_to_u16(OFPP_MAX
)
2444 : OFPERR_OFPQOFC_BAD_PORT
);
2447 /* Constructs and returns the beginning of a reply to
2448 * OFPT_QUEUE_GET_CONFIG_REQUEST or OFPMP_QUEUE_DESC request 'oh'. The caller
2449 * may append information about individual queues with
2450 * ofputil_append_queue_get_config_reply(). */
2452 ofputil_start_queue_get_config_reply(const struct ofp_header
*request
,
2453 struct ovs_list
*replies
)
2455 struct ofpbuf
*reply
;
2460 error
= ofputil_decode_queue_get_config_request(request
, &port
, &queue
);
2463 enum ofpraw raw
= ofpraw_decode_assert(request
);
2464 switch ((int) raw
) {
2465 case OFPRAW_OFPT10_QUEUE_GET_CONFIG_REQUEST
:
2466 reply
= ofpraw_alloc_reply(OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY
,
2468 struct ofp10_queue_get_config_reply
*qgcr10
2469 = ofpbuf_put_zeros(reply
, sizeof *qgcr10
);
2470 qgcr10
->port
= htons(ofp_to_u16(port
));
2473 case OFPRAW_OFPT11_QUEUE_GET_CONFIG_REQUEST
:
2474 reply
= ofpraw_alloc_reply(OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY
,
2476 struct ofp11_queue_get_config_reply
*qgcr11
2477 = ofpbuf_put_zeros(reply
, sizeof *qgcr11
);
2478 qgcr11
->port
= ofputil_port_to_ofp11(port
);
2481 case OFPRAW_OFPST14_QUEUE_DESC_REQUEST
:
2482 reply
= ofpraw_alloc_stats_reply(request
, 0);
2489 ovs_list_init(replies
);
2490 ovs_list_push_back(replies
, &reply
->list_node
);
2494 put_ofp10_queue_rate(struct ofpbuf
*reply
,
2495 enum ofp10_queue_properties property
, uint16_t rate
)
2497 if (rate
!= UINT16_MAX
) {
2498 struct ofp10_queue_prop_rate
*oqpr
;
2500 oqpr
= ofpbuf_put_zeros(reply
, sizeof *oqpr
);
2501 oqpr
->prop_header
.property
= htons(property
);
2502 oqpr
->prop_header
.len
= htons(sizeof *oqpr
);
2503 oqpr
->rate
= htons(rate
);
2508 put_ofp14_queue_rate(struct ofpbuf
*reply
,
2509 enum ofp14_queue_desc_prop_type type
, uint16_t rate
)
2511 if (rate
!= UINT16_MAX
) {
2512 ofpprop_put_u16(reply
, type
, rate
);
2517 ofputil_append_queue_get_config_reply(const struct ofputil_queue_config
*qc
,
2518 struct ovs_list
*replies
)
2520 enum ofp_version ofp_version
= ofpmp_version(replies
);
2521 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
2522 size_t start_ofs
= reply
->size
;
2526 if (ofp_version
< OFP14_VERSION
) {
2527 if (ofp_version
< OFP12_VERSION
) {
2528 struct ofp10_packet_queue
*opq10
;
2530 opq10
= ofpbuf_put_zeros(reply
, sizeof *opq10
);
2531 opq10
->queue_id
= htonl(qc
->queue
);
2532 len_ofs
= (char *) &opq10
->len
- (char *) reply
->data
;
2534 struct ofp12_packet_queue
*opq12
;
2536 opq12
= ofpbuf_put_zeros(reply
, sizeof *opq12
);
2537 opq12
->port
= ofputil_port_to_ofp11(qc
->port
);
2538 opq12
->queue_id
= htonl(qc
->queue
);
2539 len_ofs
= (char *) &opq12
->len
- (char *) reply
->data
;
2542 put_ofp10_queue_rate(reply
, OFPQT10_MIN_RATE
, qc
->min_rate
);
2543 put_ofp10_queue_rate(reply
, OFPQT11_MAX_RATE
, qc
->max_rate
);
2545 struct ofp14_queue_desc
*oqd
= ofpbuf_put_zeros(reply
, sizeof *oqd
);
2546 oqd
->port_no
= ofputil_port_to_ofp11(qc
->port
);
2547 oqd
->queue_id
= htonl(qc
->queue
);
2548 len_ofs
= (char *) &oqd
->len
- (char *) reply
->data
;
2549 put_ofp14_queue_rate(reply
, OFPQDPT14_MIN_RATE
, qc
->min_rate
);
2550 put_ofp14_queue_rate(reply
, OFPQDPT14_MAX_RATE
, qc
->max_rate
);
2553 len
= ofpbuf_at(reply
, len_ofs
, sizeof *len
);
2554 *len
= htons(reply
->size
- start_ofs
);
2556 if (ofp_version
>= OFP14_VERSION
) {
2557 ofpmp_postappend(replies
, start_ofs
);
2562 parse_ofp10_queue_rate(const struct ofp10_queue_prop_header
*hdr
,
2565 const struct ofp10_queue_prop_rate
*oqpr
;
2567 if (hdr
->len
== htons(sizeof *oqpr
)) {
2568 oqpr
= (const struct ofp10_queue_prop_rate
*) hdr
;
2569 *rate
= ntohs(oqpr
->rate
);
2572 return OFPERR_OFPBRC_BAD_LEN
;
2577 ofputil_pull_queue_get_config_reply10(struct ofpbuf
*msg
,
2578 struct ofputil_queue_config
*queue
)
2580 const struct ofp_header
*oh
= msg
->header
;
2581 unsigned int opq_len
; /* Length of protocol-specific queue header. */
2582 unsigned int len
; /* Total length of queue + properties. */
2584 /* Obtain the port number from the message header. */
2585 if (oh
->version
== OFP10_VERSION
) {
2586 const struct ofp10_queue_get_config_reply
*oqgcr10
= msg
->msg
;
2587 queue
->port
= u16_to_ofp(ntohs(oqgcr10
->port
));
2589 const struct ofp11_queue_get_config_reply
*oqgcr11
= msg
->msg
;
2590 enum ofperr error
= ofputil_port_from_ofp11(oqgcr11
->port
,
2597 /* Pull off the queue header and get the queue number and length. */
2598 if (oh
->version
< OFP12_VERSION
) {
2599 const struct ofp10_packet_queue
*opq10
;
2600 opq10
= ofpbuf_try_pull(msg
, sizeof *opq10
);
2602 return OFPERR_OFPBRC_BAD_LEN
;
2604 queue
->queue
= ntohl(opq10
->queue_id
);
2605 len
= ntohs(opq10
->len
);
2606 opq_len
= sizeof *opq10
;
2608 const struct ofp12_packet_queue
*opq12
;
2609 opq12
= ofpbuf_try_pull(msg
, sizeof *opq12
);
2611 return OFPERR_OFPBRC_BAD_LEN
;
2613 queue
->queue
= ntohl(opq12
->queue_id
);
2614 len
= ntohs(opq12
->len
);
2615 opq_len
= sizeof *opq12
;
2619 if (len
< opq_len
|| len
> msg
->size
+ opq_len
|| len
% 8) {
2620 return OFPERR_OFPBRC_BAD_LEN
;
2624 /* Pull properties. The format of these properties differs from used in
2625 * OF1.4+ so we can't use the common property functions. */
2627 const struct ofp10_queue_prop_header
*hdr
;
2628 unsigned int property
;
2629 unsigned int prop_len
;
2630 enum ofperr error
= 0;
2632 hdr
= ofpbuf_at_assert(msg
, 0, sizeof *hdr
);
2633 prop_len
= ntohs(hdr
->len
);
2634 if (prop_len
< sizeof *hdr
|| prop_len
> len
|| prop_len
% 8) {
2635 return OFPERR_OFPBRC_BAD_LEN
;
2638 property
= ntohs(hdr
->property
);
2640 case OFPQT10_MIN_RATE
:
2641 error
= parse_ofp10_queue_rate(hdr
, &queue
->min_rate
);
2644 case OFPQT11_MAX_RATE
:
2645 error
= parse_ofp10_queue_rate(hdr
, &queue
->max_rate
);
2649 VLOG_INFO_RL(&bad_ofmsg_rl
, "unknown queue property %u", property
);
2656 ofpbuf_pull(msg
, prop_len
);
2663 ofputil_pull_queue_get_config_reply14(struct ofpbuf
*msg
,
2664 struct ofputil_queue_config
*queue
)
2666 struct ofp14_queue_desc
*oqd14
= ofpbuf_try_pull(msg
, sizeof *oqd14
);
2668 return OFPERR_OFPBRC_BAD_LEN
;
2670 enum ofperr error
= ofputil_port_from_ofp11(oqd14
->port_no
, &queue
->port
);
2674 queue
->queue
= ntohl(oqd14
->queue_id
);
2677 unsigned int len
= ntohs(oqd14
->len
);
2678 if (len
< sizeof *oqd14
|| len
> msg
->size
+ sizeof *oqd14
|| len
% 8) {
2679 return OFPERR_OFPBRC_BAD_LEN
;
2681 len
-= sizeof *oqd14
;
2683 struct ofpbuf properties
= ofpbuf_const_initializer(ofpbuf_pull(msg
, len
),
2685 while (properties
.size
> 0) {
2686 struct ofpbuf payload
;
2689 error
= ofpprop_pull(&properties
, &payload
, &type
);
2695 case OFPQDPT14_MIN_RATE
:
2696 error
= ofpprop_parse_u16(&payload
, &queue
->min_rate
);
2699 case OFPQDPT14_MAX_RATE
:
2700 error
= ofpprop_parse_u16(&payload
, &queue
->max_rate
);
2704 error
= OFPPROP_UNKNOWN(true, "queue desc", type
);
2716 /* Decodes information about a queue from the OFPT_QUEUE_GET_CONFIG_REPLY in
2717 * 'reply' and stores it in '*queue'. ofputil_decode_queue_get_config_reply()
2718 * must already have pulled off the main header.
2720 * This function returns EOF if the last queue has already been decoded, 0 if a
2721 * queue was successfully decoded into '*queue', or an ofperr if there was a
2722 * problem decoding 'reply'. */
2724 ofputil_pull_queue_get_config_reply(struct ofpbuf
*msg
,
2725 struct ofputil_queue_config
*queue
)
2729 /* Pull OpenFlow header. */
2730 raw
= ofpraw_pull_assert(msg
);
2732 /* Pull protocol-specific ofp_queue_get_config_reply header (OF1.4
2733 * doesn't have one at all). */
2734 if (raw
== OFPRAW_OFPT10_QUEUE_GET_CONFIG_REPLY
) {
2735 ofpbuf_pull(msg
, sizeof(struct ofp10_queue_get_config_reply
));
2736 } else if (raw
== OFPRAW_OFPT11_QUEUE_GET_CONFIG_REPLY
) {
2737 ofpbuf_pull(msg
, sizeof(struct ofp11_queue_get_config_reply
));
2739 ovs_assert(raw
== OFPRAW_OFPST14_QUEUE_DESC_REPLY
);
2742 raw
= ofpraw_decode_assert(msg
->header
);
2745 queue
->min_rate
= UINT16_MAX
;
2746 queue
->max_rate
= UINT16_MAX
;
2750 } else if (raw
== OFPRAW_OFPST14_QUEUE_DESC_REPLY
) {
2751 return ofputil_pull_queue_get_config_reply14(msg
, queue
);
2753 return ofputil_pull_queue_get_config_reply10(msg
, queue
);
2757 /* Converts an OFPST_FLOW, OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE
2758 * request 'oh', into an abstract flow_stats_request in 'fsr'. Returns 0 if
2759 * successful, otherwise an OpenFlow error code.
2761 * 'vl_mff_map' is an optional parameter that is used to validate the length
2762 * of variable length mf_fields in 'match'. If it is not provided, the
2763 * default mf_fields with maximum length will be used. */
2765 ofputil_decode_flow_stats_request(struct ofputil_flow_stats_request
*fsr
,
2766 const struct ofp_header
*oh
,
2767 const struct tun_table
*tun_table
,
2768 const struct vl_mff_map
*vl_mff_map
)
2770 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
2771 enum ofpraw raw
= ofpraw_pull_assert(&b
);
2772 switch ((int) raw
) {
2773 case OFPRAW_OFPST10_FLOW_REQUEST
:
2774 return ofputil_decode_ofpst10_flow_request(fsr
, b
.data
, false);
2776 case OFPRAW_OFPST10_AGGREGATE_REQUEST
:
2777 return ofputil_decode_ofpst10_flow_request(fsr
, b
.data
, true);
2779 case OFPRAW_OFPST11_FLOW_REQUEST
:
2780 return ofputil_decode_ofpst11_flow_request(fsr
, &b
, false, tun_table
,
2783 case OFPRAW_OFPST11_AGGREGATE_REQUEST
:
2784 return ofputil_decode_ofpst11_flow_request(fsr
, &b
, true, tun_table
,
2787 case OFPRAW_NXST_FLOW_REQUEST
:
2788 return ofputil_decode_nxst_flow_request(fsr
, &b
, false, tun_table
,
2791 case OFPRAW_NXST_AGGREGATE_REQUEST
:
2792 return ofputil_decode_nxst_flow_request(fsr
, &b
, true, tun_table
,
2796 /* Hey, the caller lied. */
2801 /* Converts abstract flow_stats_request 'fsr' into an OFPST_FLOW,
2802 * OFPST_AGGREGATE, NXST_FLOW, or NXST_AGGREGATE request 'oh' according to
2803 * 'protocol', and returns the message. */
2805 ofputil_encode_flow_stats_request(const struct ofputil_flow_stats_request
*fsr
,
2806 enum ofputil_protocol protocol
)
2812 case OFPUTIL_P_OF11_STD
:
2813 case OFPUTIL_P_OF12_OXM
:
2814 case OFPUTIL_P_OF13_OXM
:
2815 case OFPUTIL_P_OF14_OXM
:
2816 case OFPUTIL_P_OF15_OXM
:
2817 case OFPUTIL_P_OF16_OXM
: {
2818 struct ofp11_flow_stats_request
*ofsr
;
2820 raw
= (fsr
->aggregate
2821 ? OFPRAW_OFPST11_AGGREGATE_REQUEST
2822 : OFPRAW_OFPST11_FLOW_REQUEST
);
2823 msg
= ofpraw_alloc(raw
, ofputil_protocol_to_ofp_version(protocol
),
2824 ofputil_match_typical_len(protocol
));
2825 ofsr
= ofpbuf_put_zeros(msg
, sizeof *ofsr
);
2826 ofsr
->table_id
= fsr
->table_id
;
2827 ofsr
->out_port
= ofputil_port_to_ofp11(fsr
->out_port
);
2828 ofsr
->out_group
= htonl(fsr
->out_group
);
2829 ofsr
->cookie
= fsr
->cookie
;
2830 ofsr
->cookie_mask
= fsr
->cookie_mask
;
2831 ofputil_put_ofp11_match(msg
, &fsr
->match
, protocol
);
2835 case OFPUTIL_P_OF10_STD
:
2836 case OFPUTIL_P_OF10_STD_TID
: {
2837 struct ofp10_flow_stats_request
*ofsr
;
2839 raw
= (fsr
->aggregate
2840 ? OFPRAW_OFPST10_AGGREGATE_REQUEST
2841 : OFPRAW_OFPST10_FLOW_REQUEST
);
2842 msg
= ofpraw_alloc(raw
, OFP10_VERSION
, 0);
2843 ofsr
= ofpbuf_put_zeros(msg
, sizeof *ofsr
);
2844 ofputil_match_to_ofp10_match(&fsr
->match
, &ofsr
->match
);
2845 ofsr
->table_id
= fsr
->table_id
;
2846 ofsr
->out_port
= htons(ofp_to_u16(fsr
->out_port
));
2850 case OFPUTIL_P_OF10_NXM
:
2851 case OFPUTIL_P_OF10_NXM_TID
: {
2852 struct nx_flow_stats_request
*nfsr
;
2855 raw
= (fsr
->aggregate
2856 ? OFPRAW_NXST_AGGREGATE_REQUEST
2857 : OFPRAW_NXST_FLOW_REQUEST
);
2858 msg
= ofpraw_alloc(raw
, OFP10_VERSION
, NXM_TYPICAL_LEN
);
2859 ofpbuf_put_zeros(msg
, sizeof *nfsr
);
2860 match_len
= nx_put_match(msg
, &fsr
->match
,
2861 fsr
->cookie
, fsr
->cookie_mask
);
2864 nfsr
->out_port
= htons(ofp_to_u16(fsr
->out_port
));
2865 nfsr
->match_len
= htons(match_len
);
2866 nfsr
->table_id
= fsr
->table_id
;
2877 /* Converts an OFPST_FLOW or NXST_FLOW reply in 'msg' into an abstract
2878 * ofputil_flow_stats in 'fs'.
2880 * Multiple OFPST_FLOW or NXST_FLOW replies can be packed into a single
2881 * OpenFlow message. Calling this function multiple times for a single 'msg'
2882 * iterates through the replies. The caller must initially leave 'msg''s layer
2883 * pointers null and not modify them between calls.
2885 * Most switches don't send the values needed to populate fs->idle_age and
2886 * fs->hard_age, so those members will usually be set to 0. If the switch from
2887 * which 'msg' originated is known to implement NXT_FLOW_AGE, then pass
2888 * 'flow_age_extension' as true so that the contents of 'msg' determine the
2889 * 'idle_age' and 'hard_age' members in 'fs'.
2891 * Uses 'ofpacts' to store the abstract OFPACT_* version of the flow stats
2892 * reply's actions. The caller must initialize 'ofpacts' and retains ownership
2893 * of it. 'fs->ofpacts' will point into the 'ofpacts' buffer.
2895 * Returns 0 if successful, EOF if no replies were left in this 'msg',
2896 * otherwise a positive errno value. */
2898 ofputil_decode_flow_stats_reply(struct ofputil_flow_stats
*fs
,
2900 bool flow_age_extension
,
2901 struct ofpbuf
*ofpacts
)
2903 const struct ofp_header
*oh
;
2904 size_t instructions_len
;
2908 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
2909 : ofpraw_pull(&raw
, msg
));
2917 } else if (raw
== OFPRAW_OFPST11_FLOW_REPLY
2918 || raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
2919 const struct ofp11_flow_stats
*ofs
;
2921 uint16_t padded_match_len
;
2923 ofs
= ofpbuf_try_pull(msg
, sizeof *ofs
);
2925 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply has %"PRIu32
" leftover "
2926 "bytes at end", msg
->size
);
2930 length
= ntohs(ofs
->length
);
2931 if (length
< sizeof *ofs
) {
2932 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply claims invalid "
2933 "length %"PRIuSIZE
, length
);
2937 if (ofputil_pull_ofp11_match(msg
, NULL
, NULL
, &fs
->match
,
2938 &padded_match_len
)) {
2939 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply bad match");
2942 instructions_len
= length
- sizeof *ofs
- padded_match_len
;
2944 fs
->priority
= ntohs(ofs
->priority
);
2945 fs
->table_id
= ofs
->table_id
;
2946 fs
->duration_sec
= ntohl(ofs
->duration_sec
);
2947 fs
->duration_nsec
= ntohl(ofs
->duration_nsec
);
2948 fs
->idle_timeout
= ntohs(ofs
->idle_timeout
);
2949 fs
->hard_timeout
= ntohs(ofs
->hard_timeout
);
2950 if (oh
->version
>= OFP14_VERSION
) {
2951 fs
->importance
= ntohs(ofs
->importance
);
2955 if (raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
2956 error
= ofputil_decode_flow_mod_flags(ofs
->flags
, -1, oh
->version
,
2966 fs
->cookie
= ofs
->cookie
;
2967 fs
->packet_count
= ntohll(ofs
->packet_count
);
2968 fs
->byte_count
= ntohll(ofs
->byte_count
);
2969 } else if (raw
== OFPRAW_OFPST10_FLOW_REPLY
) {
2970 const struct ofp10_flow_stats
*ofs
;
2973 ofs
= ofpbuf_try_pull(msg
, sizeof *ofs
);
2975 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply has %"PRIu32
" leftover "
2976 "bytes at end", msg
->size
);
2980 length
= ntohs(ofs
->length
);
2981 if (length
< sizeof *ofs
) {
2982 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply claims invalid "
2983 "length %"PRIuSIZE
, length
);
2986 instructions_len
= length
- sizeof *ofs
;
2988 fs
->cookie
= get_32aligned_be64(&ofs
->cookie
);
2989 ofputil_match_from_ofp10_match(&ofs
->match
, &fs
->match
);
2990 fs
->priority
= ntohs(ofs
->priority
);
2991 fs
->table_id
= ofs
->table_id
;
2992 fs
->duration_sec
= ntohl(ofs
->duration_sec
);
2993 fs
->duration_nsec
= ntohl(ofs
->duration_nsec
);
2994 fs
->idle_timeout
= ntohs(ofs
->idle_timeout
);
2995 fs
->hard_timeout
= ntohs(ofs
->hard_timeout
);
2999 fs
->packet_count
= ntohll(get_32aligned_be64(&ofs
->packet_count
));
3000 fs
->byte_count
= ntohll(get_32aligned_be64(&ofs
->byte_count
));
3002 } else if (raw
== OFPRAW_NXST_FLOW_REPLY
) {
3003 const struct nx_flow_stats
*nfs
;
3004 size_t match_len
, length
;
3006 nfs
= ofpbuf_try_pull(msg
, sizeof *nfs
);
3008 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW reply has %"PRIu32
" leftover "
3009 "bytes at end", msg
->size
);
3013 length
= ntohs(nfs
->length
);
3014 match_len
= ntohs(nfs
->match_len
);
3015 if (length
< sizeof *nfs
+ ROUND_UP(match_len
, 8)) {
3016 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW reply with match_len=%"PRIuSIZE
" "
3017 "claims invalid length %"PRIuSIZE
, match_len
, length
);
3020 if (nx_pull_match(msg
, match_len
, &fs
->match
, NULL
, NULL
, false, NULL
,
3024 instructions_len
= length
- sizeof *nfs
- ROUND_UP(match_len
, 8);
3026 fs
->cookie
= nfs
->cookie
;
3027 fs
->table_id
= nfs
->table_id
;
3028 fs
->duration_sec
= ntohl(nfs
->duration_sec
);
3029 fs
->duration_nsec
= ntohl(nfs
->duration_nsec
);
3030 fs
->priority
= ntohs(nfs
->priority
);
3031 fs
->idle_timeout
= ntohs(nfs
->idle_timeout
);
3032 fs
->hard_timeout
= ntohs(nfs
->hard_timeout
);
3036 if (flow_age_extension
) {
3037 if (nfs
->idle_age
) {
3038 fs
->idle_age
= ntohs(nfs
->idle_age
) - 1;
3040 if (nfs
->hard_age
) {
3041 fs
->hard_age
= ntohs(nfs
->hard_age
) - 1;
3044 fs
->packet_count
= ntohll(nfs
->packet_count
);
3045 fs
->byte_count
= ntohll(nfs
->byte_count
);
3051 if (ofpacts_pull_openflow_instructions(msg
, instructions_len
, oh
->version
,
3052 NULL
, NULL
, ofpacts
)) {
3053 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_FLOW reply bad instructions");
3056 fs
->ofpacts
= ofpacts
->data
;
3057 fs
->ofpacts_len
= ofpacts
->size
;
3062 /* Returns 'count' unchanged except that UINT64_MAX becomes 0.
3064 * We use this in situations where OVS internally uses UINT64_MAX to mean
3065 * "value unknown" but OpenFlow 1.0 does not define any unknown value. */
3067 unknown_to_zero(uint64_t count
)
3069 return count
!= UINT64_MAX
? count
: 0;
3072 /* Appends an OFPST_FLOW or NXST_FLOW reply that contains the data in 'fs' to
3073 * those already present in the list of ofpbufs in 'replies'. 'replies' should
3074 * have been initialized with ofpmp_init(). */
3076 ofputil_append_flow_stats_reply(const struct ofputil_flow_stats
*fs
,
3077 struct ovs_list
*replies
,
3078 const struct tun_table
*tun_table
)
3080 struct ofputil_flow_stats
*fs_
= CONST_CAST(struct ofputil_flow_stats
*,
3082 const struct tun_table
*orig_tun_table
;
3083 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
3084 size_t start_ofs
= reply
->size
;
3085 enum ofp_version version
= ofpmp_version(replies
);
3086 enum ofpraw raw
= ofpmp_decode_raw(replies
);
3088 orig_tun_table
= fs
->match
.flow
.tunnel
.metadata
.tab
;
3089 fs_
->match
.flow
.tunnel
.metadata
.tab
= tun_table
;
3091 if (raw
== OFPRAW_OFPST11_FLOW_REPLY
|| raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
3092 struct ofp11_flow_stats
*ofs
;
3094 ofpbuf_put_uninit(reply
, sizeof *ofs
);
3095 oxm_put_match(reply
, &fs
->match
, version
);
3096 ofpacts_put_openflow_instructions(fs
->ofpacts
, fs
->ofpacts_len
, reply
,
3099 ofs
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *ofs
);
3100 ofs
->length
= htons(reply
->size
- start_ofs
);
3101 ofs
->table_id
= fs
->table_id
;
3103 ofs
->duration_sec
= htonl(fs
->duration_sec
);
3104 ofs
->duration_nsec
= htonl(fs
->duration_nsec
);
3105 ofs
->priority
= htons(fs
->priority
);
3106 ofs
->idle_timeout
= htons(fs
->idle_timeout
);
3107 ofs
->hard_timeout
= htons(fs
->hard_timeout
);
3108 if (version
>= OFP14_VERSION
) {
3109 ofs
->importance
= htons(fs
->importance
);
3111 ofs
->importance
= 0;
3113 if (raw
== OFPRAW_OFPST13_FLOW_REPLY
) {
3114 ofs
->flags
= ofputil_encode_flow_mod_flags(fs
->flags
, version
);
3118 memset(ofs
->pad2
, 0, sizeof ofs
->pad2
);
3119 ofs
->cookie
= fs
->cookie
;
3120 ofs
->packet_count
= htonll(unknown_to_zero(fs
->packet_count
));
3121 ofs
->byte_count
= htonll(unknown_to_zero(fs
->byte_count
));
3122 } else if (raw
== OFPRAW_OFPST10_FLOW_REPLY
) {
3123 struct ofp10_flow_stats
*ofs
;
3125 ofpbuf_put_uninit(reply
, sizeof *ofs
);
3126 ofpacts_put_openflow_actions(fs
->ofpacts
, fs
->ofpacts_len
, reply
,
3128 ofs
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *ofs
);
3129 ofs
->length
= htons(reply
->size
- start_ofs
);
3130 ofs
->table_id
= fs
->table_id
;
3132 ofputil_match_to_ofp10_match(&fs
->match
, &ofs
->match
);
3133 ofs
->duration_sec
= htonl(fs
->duration_sec
);
3134 ofs
->duration_nsec
= htonl(fs
->duration_nsec
);
3135 ofs
->priority
= htons(fs
->priority
);
3136 ofs
->idle_timeout
= htons(fs
->idle_timeout
);
3137 ofs
->hard_timeout
= htons(fs
->hard_timeout
);
3138 memset(ofs
->pad2
, 0, sizeof ofs
->pad2
);
3139 put_32aligned_be64(&ofs
->cookie
, fs
->cookie
);
3140 put_32aligned_be64(&ofs
->packet_count
,
3141 htonll(unknown_to_zero(fs
->packet_count
)));
3142 put_32aligned_be64(&ofs
->byte_count
,
3143 htonll(unknown_to_zero(fs
->byte_count
)));
3144 } else if (raw
== OFPRAW_NXST_FLOW_REPLY
) {
3145 struct nx_flow_stats
*nfs
;
3148 ofpbuf_put_uninit(reply
, sizeof *nfs
);
3149 match_len
= nx_put_match(reply
, &fs
->match
, 0, 0);
3150 ofpacts_put_openflow_actions(fs
->ofpacts
, fs
->ofpacts_len
, reply
,
3152 nfs
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *nfs
);
3153 nfs
->length
= htons(reply
->size
- start_ofs
);
3154 nfs
->table_id
= fs
->table_id
;
3156 nfs
->duration_sec
= htonl(fs
->duration_sec
);
3157 nfs
->duration_nsec
= htonl(fs
->duration_nsec
);
3158 nfs
->priority
= htons(fs
->priority
);
3159 nfs
->idle_timeout
= htons(fs
->idle_timeout
);
3160 nfs
->hard_timeout
= htons(fs
->hard_timeout
);
3161 nfs
->idle_age
= htons(fs
->idle_age
< 0 ? 0
3162 : fs
->idle_age
< UINT16_MAX
? fs
->idle_age
+ 1
3164 nfs
->hard_age
= htons(fs
->hard_age
< 0 ? 0
3165 : fs
->hard_age
< UINT16_MAX
? fs
->hard_age
+ 1
3167 nfs
->match_len
= htons(match_len
);
3168 nfs
->cookie
= fs
->cookie
;
3169 nfs
->packet_count
= htonll(fs
->packet_count
);
3170 nfs
->byte_count
= htonll(fs
->byte_count
);
3175 ofpmp_postappend(replies
, start_ofs
);
3176 fs_
->match
.flow
.tunnel
.metadata
.tab
= orig_tun_table
;
3179 /* Converts abstract ofputil_aggregate_stats 'stats' into an OFPST_AGGREGATE or
3180 * NXST_AGGREGATE reply matching 'request', and returns the message. */
3182 ofputil_encode_aggregate_stats_reply(
3183 const struct ofputil_aggregate_stats
*stats
,
3184 const struct ofp_header
*request
)
3186 struct ofp_aggregate_stats_reply
*asr
;
3187 uint64_t packet_count
;
3188 uint64_t byte_count
;
3192 ofpraw_decode(&raw
, request
);
3193 if (raw
== OFPRAW_OFPST10_AGGREGATE_REQUEST
) {
3194 packet_count
= unknown_to_zero(stats
->packet_count
);
3195 byte_count
= unknown_to_zero(stats
->byte_count
);
3197 packet_count
= stats
->packet_count
;
3198 byte_count
= stats
->byte_count
;
3201 msg
= ofpraw_alloc_stats_reply(request
, 0);
3202 asr
= ofpbuf_put_zeros(msg
, sizeof *asr
);
3203 put_32aligned_be64(&asr
->packet_count
, htonll(packet_count
));
3204 put_32aligned_be64(&asr
->byte_count
, htonll(byte_count
));
3205 asr
->flow_count
= htonl(stats
->flow_count
);
3211 ofputil_decode_aggregate_stats_reply(struct ofputil_aggregate_stats
*stats
,
3212 const struct ofp_header
*reply
)
3214 struct ofpbuf msg
= ofpbuf_const_initializer(reply
, ntohs(reply
->length
));
3215 ofpraw_pull_assert(&msg
);
3217 struct ofp_aggregate_stats_reply
*asr
= msg
.msg
;
3218 stats
->packet_count
= ntohll(get_32aligned_be64(&asr
->packet_count
));
3219 stats
->byte_count
= ntohll(get_32aligned_be64(&asr
->byte_count
));
3220 stats
->flow_count
= ntohl(asr
->flow_count
);
3225 /* Converts an OFPT_FLOW_REMOVED or NXT_FLOW_REMOVED message 'oh' into an
3226 * abstract ofputil_flow_removed in 'fr'. Returns 0 if successful, otherwise
3227 * an OpenFlow error code. */
3229 ofputil_decode_flow_removed(struct ofputil_flow_removed
*fr
,
3230 const struct ofp_header
*oh
)
3232 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
3233 enum ofpraw raw
= ofpraw_pull_assert(&b
);
3234 if (raw
== OFPRAW_OFPT11_FLOW_REMOVED
) {
3235 const struct ofp12_flow_removed
*ofr
;
3238 ofr
= ofpbuf_pull(&b
, sizeof *ofr
);
3240 error
= ofputil_pull_ofp11_match(&b
, NULL
, NULL
, &fr
->match
, NULL
);
3245 fr
->priority
= ntohs(ofr
->priority
);
3246 fr
->cookie
= ofr
->cookie
;
3247 fr
->reason
= ofr
->reason
;
3248 fr
->table_id
= ofr
->table_id
;
3249 fr
->duration_sec
= ntohl(ofr
->duration_sec
);
3250 fr
->duration_nsec
= ntohl(ofr
->duration_nsec
);
3251 fr
->idle_timeout
= ntohs(ofr
->idle_timeout
);
3252 fr
->hard_timeout
= ntohs(ofr
->hard_timeout
);
3253 fr
->packet_count
= ntohll(ofr
->packet_count
);
3254 fr
->byte_count
= ntohll(ofr
->byte_count
);
3255 } else if (raw
== OFPRAW_OFPT10_FLOW_REMOVED
) {
3256 const struct ofp10_flow_removed
*ofr
;
3258 ofr
= ofpbuf_pull(&b
, sizeof *ofr
);
3260 ofputil_match_from_ofp10_match(&ofr
->match
, &fr
->match
);
3261 fr
->priority
= ntohs(ofr
->priority
);
3262 fr
->cookie
= ofr
->cookie
;
3263 fr
->reason
= ofr
->reason
;
3265 fr
->duration_sec
= ntohl(ofr
->duration_sec
);
3266 fr
->duration_nsec
= ntohl(ofr
->duration_nsec
);
3267 fr
->idle_timeout
= ntohs(ofr
->idle_timeout
);
3268 fr
->hard_timeout
= 0;
3269 fr
->packet_count
= ntohll(ofr
->packet_count
);
3270 fr
->byte_count
= ntohll(ofr
->byte_count
);
3271 } else if (raw
== OFPRAW_NXT_FLOW_REMOVED
) {
3272 struct nx_flow_removed
*nfr
;
3275 nfr
= ofpbuf_pull(&b
, sizeof *nfr
);
3276 error
= nx_pull_match(&b
, ntohs(nfr
->match_len
), &fr
->match
, NULL
,
3277 NULL
, false, NULL
, NULL
);
3282 return OFPERR_OFPBRC_BAD_LEN
;
3285 fr
->priority
= ntohs(nfr
->priority
);
3286 fr
->cookie
= nfr
->cookie
;
3287 fr
->reason
= nfr
->reason
;
3288 fr
->table_id
= nfr
->table_id
? nfr
->table_id
- 1 : 255;
3289 fr
->duration_sec
= ntohl(nfr
->duration_sec
);
3290 fr
->duration_nsec
= ntohl(nfr
->duration_nsec
);
3291 fr
->idle_timeout
= ntohs(nfr
->idle_timeout
);
3292 fr
->hard_timeout
= 0;
3293 fr
->packet_count
= ntohll(nfr
->packet_count
);
3294 fr
->byte_count
= ntohll(nfr
->byte_count
);
3302 /* Converts abstract ofputil_flow_removed 'fr' into an OFPT_FLOW_REMOVED or
3303 * NXT_FLOW_REMOVED message 'oh' according to 'protocol', and returns the
3306 ofputil_encode_flow_removed(const struct ofputil_flow_removed
*fr
,
3307 enum ofputil_protocol protocol
)
3310 enum ofp_flow_removed_reason reason
= fr
->reason
;
3312 if (reason
== OFPRR_METER_DELETE
&& !(protocol
& OFPUTIL_P_OF14_UP
)) {
3313 reason
= OFPRR_DELETE
;
3317 case OFPUTIL_P_OF11_STD
:
3318 case OFPUTIL_P_OF12_OXM
:
3319 case OFPUTIL_P_OF13_OXM
:
3320 case OFPUTIL_P_OF14_OXM
:
3321 case OFPUTIL_P_OF15_OXM
:
3322 case OFPUTIL_P_OF16_OXM
: {
3323 struct ofp12_flow_removed
*ofr
;
3325 msg
= ofpraw_alloc_xid(OFPRAW_OFPT11_FLOW_REMOVED
,
3326 ofputil_protocol_to_ofp_version(protocol
),
3328 ofputil_match_typical_len(protocol
));
3329 ofr
= ofpbuf_put_zeros(msg
, sizeof *ofr
);
3330 ofr
->cookie
= fr
->cookie
;
3331 ofr
->priority
= htons(fr
->priority
);
3332 ofr
->reason
= reason
;
3333 ofr
->table_id
= fr
->table_id
;
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
->hard_timeout
= htons(fr
->hard_timeout
);
3338 ofr
->packet_count
= htonll(fr
->packet_count
);
3339 ofr
->byte_count
= htonll(fr
->byte_count
);
3340 ofputil_put_ofp11_match(msg
, &fr
->match
, protocol
);
3344 case OFPUTIL_P_OF10_STD
:
3345 case OFPUTIL_P_OF10_STD_TID
: {
3346 struct ofp10_flow_removed
*ofr
;
3348 msg
= ofpraw_alloc_xid(OFPRAW_OFPT10_FLOW_REMOVED
, OFP10_VERSION
,
3350 ofr
= ofpbuf_put_zeros(msg
, sizeof *ofr
);
3351 ofputil_match_to_ofp10_match(&fr
->match
, &ofr
->match
);
3352 ofr
->cookie
= fr
->cookie
;
3353 ofr
->priority
= htons(fr
->priority
);
3354 ofr
->reason
= reason
;
3355 ofr
->duration_sec
= htonl(fr
->duration_sec
);
3356 ofr
->duration_nsec
= htonl(fr
->duration_nsec
);
3357 ofr
->idle_timeout
= htons(fr
->idle_timeout
);
3358 ofr
->packet_count
= htonll(unknown_to_zero(fr
->packet_count
));
3359 ofr
->byte_count
= htonll(unknown_to_zero(fr
->byte_count
));
3363 case OFPUTIL_P_OF10_NXM
:
3364 case OFPUTIL_P_OF10_NXM_TID
: {
3365 struct nx_flow_removed
*nfr
;
3368 msg
= ofpraw_alloc_xid(OFPRAW_NXT_FLOW_REMOVED
, OFP10_VERSION
,
3369 htonl(0), NXM_TYPICAL_LEN
);
3370 ofpbuf_put_zeros(msg
, sizeof *nfr
);
3371 match_len
= nx_put_match(msg
, &fr
->match
, 0, 0);
3374 nfr
->cookie
= fr
->cookie
;
3375 nfr
->priority
= htons(fr
->priority
);
3376 nfr
->reason
= reason
;
3377 nfr
->table_id
= fr
->table_id
+ 1;
3378 nfr
->duration_sec
= htonl(fr
->duration_sec
);
3379 nfr
->duration_nsec
= htonl(fr
->duration_nsec
);
3380 nfr
->idle_timeout
= htons(fr
->idle_timeout
);
3381 nfr
->match_len
= htons(match_len
);
3382 nfr
->packet_count
= htonll(fr
->packet_count
);
3383 nfr
->byte_count
= htonll(fr
->byte_count
);
3394 /* The caller has done basic initialization of '*pin'; the other output
3395 * arguments needs to be initialized. */
3397 decode_nx_packet_in2(const struct ofp_header
*oh
, bool loose
,
3398 const struct tun_table
*tun_table
,
3399 const struct vl_mff_map
*vl_mff_map
,
3400 struct ofputil_packet_in
*pin
,
3401 size_t *total_len
, uint32_t *buffer_id
,
3402 struct ofpbuf
*continuation
)
3405 *buffer_id
= UINT32_MAX
;
3407 struct ofpbuf properties
;
3408 ofpbuf_use_const(&properties
, oh
, ntohs(oh
->length
));
3409 ofpraw_pull_assert(&properties
);
3411 while (properties
.size
> 0) {
3412 struct ofpbuf payload
;
3415 enum ofperr error
= ofpprop_pull(&properties
, &payload
, &type
);
3422 pin
->packet
= payload
.msg
;
3423 pin
->packet_len
= ofpbuf_msgsize(&payload
);
3426 case NXPINT_FULL_LEN
: {
3428 error
= ofpprop_parse_u32(&payload
, &u32
);
3433 case NXPINT_BUFFER_ID
:
3434 error
= ofpprop_parse_u32(&payload
, buffer_id
);
3437 case NXPINT_TABLE_ID
:
3438 error
= ofpprop_parse_u8(&payload
, &pin
->table_id
);
3442 error
= ofpprop_parse_be64(&payload
, &pin
->cookie
);
3445 case NXPINT_REASON
: {
3447 error
= ofpprop_parse_u8(&payload
, &reason
);
3448 pin
->reason
= reason
;
3452 case NXPINT_METADATA
:
3453 error
= oxm_decode_match(payload
.msg
, ofpbuf_msgsize(&payload
),
3454 loose
, tun_table
, vl_mff_map
,
3455 &pin
->flow_metadata
);
3458 case NXPINT_USERDATA
:
3459 pin
->userdata
= payload
.msg
;
3460 pin
->userdata_len
= ofpbuf_msgsize(&payload
);
3463 case NXPINT_CONTINUATION
:
3465 error
= ofpprop_parse_nested(&payload
, continuation
);
3470 error
= OFPPROP_UNKNOWN(loose
, "NX_PACKET_IN2", type
);
3478 if (!pin
->packet_len
) {
3479 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXT_PACKET_IN2 lacks packet");
3480 return OFPERR_OFPBRC_BAD_LEN
;
3481 } else if (!*total_len
) {
3482 *total_len
= pin
->packet_len
;
3483 } else if (*total_len
< pin
->packet_len
) {
3484 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXT_PACKET_IN2 claimed full_len < len");
3485 return OFPERR_OFPBRC_BAD_LEN
;
3491 /* Decodes the packet-in message starting at 'oh' into '*pin'. Populates
3492 * 'pin->packet' and 'pin->packet_len' with the part of the packet actually
3493 * included in the message. If 'total_lenp' is nonnull, populates
3494 * '*total_lenp' with the original length of the packet (which is larger than
3495 * 'packet->len' if only part of the packet was included). If 'buffer_idp' is
3496 * nonnull, stores the packet's buffer ID in '*buffer_idp' (UINT32_MAX if it
3497 * was not buffered).
3499 * Populates 'continuation', if nonnull, with the continuation data from the
3500 * packet-in (an empty buffer, if 'oh' did not contain continuation data). The
3501 * format of this data is supposed to be opaque to anything other than
3502 * ovs-vswitchd, so that in any other process the only reasonable use of this
3503 * data is to be copied into an NXT_RESUME message via ofputil_encode_resume().
3505 * This function points 'pin->packet' into 'oh', so the caller should not free
3506 * it separately from the original OpenFlow message. This is also true for
3507 * 'pin->userdata' (which could also end up NULL if there is no userdata).
3509 * 'vl_mff_map' is an optional parameter that is used to validate the length
3510 * of variable length mf_fields in 'match'. If it is not provided, the
3511 * default mf_fields with maximum length will be used.
3513 * Returns 0 if successful, otherwise an OpenFlow error code. */
3515 ofputil_decode_packet_in(const struct ofp_header
*oh
, bool loose
,
3516 const struct tun_table
*tun_table
,
3517 const struct vl_mff_map
*vl_mff_map
,
3518 struct ofputil_packet_in
*pin
,
3519 size_t *total_lenp
, uint32_t *buffer_idp
,
3520 struct ofpbuf
*continuation
)
3525 memset(pin
, 0, sizeof *pin
);
3526 pin
->cookie
= OVS_BE64_MAX
;
3528 ofpbuf_use_const(continuation
, NULL
, 0);
3531 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
3532 enum ofpraw raw
= ofpraw_pull_assert(&b
);
3533 if (raw
== OFPRAW_OFPT13_PACKET_IN
|| raw
== OFPRAW_OFPT12_PACKET_IN
) {
3534 const struct ofp12_packet_in
*opi
= ofpbuf_pull(&b
, sizeof *opi
);
3535 const ovs_be64
*cookie
= (raw
== OFPRAW_OFPT13_PACKET_IN
3536 ? ofpbuf_pull(&b
, sizeof *cookie
)
3538 enum ofperr error
= oxm_pull_match_loose(&b
, false, tun_table
,
3539 &pin
->flow_metadata
);
3544 if (!ofpbuf_try_pull(&b
, 2)) {
3545 return OFPERR_OFPBRC_BAD_LEN
;
3548 pin
->reason
= opi
->reason
;
3549 pin
->table_id
= opi
->table_id
;
3550 buffer_id
= ntohl(opi
->buffer_id
);
3551 total_len
= ntohs(opi
->total_len
);
3553 pin
->cookie
= *cookie
;
3556 pin
->packet
= b
.data
;
3557 pin
->packet_len
= b
.size
;
3558 } else if (raw
== OFPRAW_OFPT10_PACKET_IN
) {
3559 const struct ofp10_packet_in
*opi
;
3561 opi
= ofpbuf_pull(&b
, offsetof(struct ofp10_packet_in
, data
));
3563 pin
->packet
= CONST_CAST(uint8_t *, opi
->data
);
3564 pin
->packet_len
= b
.size
;
3566 match_init_catchall(&pin
->flow_metadata
);
3567 match_set_in_port(&pin
->flow_metadata
,
3568 u16_to_ofp(ntohs(opi
->in_port
)));
3569 pin
->reason
= opi
->reason
;
3570 buffer_id
= ntohl(opi
->buffer_id
);
3571 total_len
= ntohs(opi
->total_len
);
3572 } else if (raw
== OFPRAW_OFPT11_PACKET_IN
) {
3573 const struct ofp11_packet_in
*opi
;
3577 opi
= ofpbuf_pull(&b
, sizeof *opi
);
3579 pin
->packet
= b
.data
;
3580 pin
->packet_len
= b
.size
;
3582 buffer_id
= ntohl(opi
->buffer_id
);
3583 error
= ofputil_port_from_ofp11(opi
->in_port
, &in_port
);
3587 match_init_catchall(&pin
->flow_metadata
);
3588 match_set_in_port(&pin
->flow_metadata
, in_port
);
3589 total_len
= ntohs(opi
->total_len
);
3590 pin
->reason
= opi
->reason
;
3591 pin
->table_id
= opi
->table_id
;
3592 } else if (raw
== OFPRAW_NXT_PACKET_IN
) {
3593 const struct nx_packet_in
*npi
;
3596 npi
= ofpbuf_pull(&b
, sizeof *npi
);
3597 error
= nx_pull_match_loose(&b
, ntohs(npi
->match_len
),
3598 &pin
->flow_metadata
, NULL
, NULL
, false,
3604 if (!ofpbuf_try_pull(&b
, 2)) {
3605 return OFPERR_OFPBRC_BAD_LEN
;
3608 pin
->reason
= npi
->reason
;
3609 pin
->table_id
= npi
->table_id
;
3610 pin
->cookie
= npi
->cookie
;
3612 buffer_id
= ntohl(npi
->buffer_id
);
3613 total_len
= ntohs(npi
->total_len
);
3615 pin
->packet
= b
.data
;
3616 pin
->packet_len
= b
.size
;
3617 } else if (raw
== OFPRAW_NXT_PACKET_IN2
|| raw
== OFPRAW_NXT_RESUME
) {
3618 enum ofperr error
= decode_nx_packet_in2(oh
, loose
, tun_table
,
3619 vl_mff_map
, pin
, &total_len
,
3620 &buffer_id
, continuation
);
3629 *total_lenp
= total_len
;
3632 *buffer_idp
= buffer_id
;
3639 encode_packet_in_reason(enum ofp_packet_in_reason reason
,
3640 enum ofp_version version
)
3645 case OFPR_INVALID_TTL
:
3648 case OFPR_ACTION_SET
:
3650 case OFPR_PACKET_OUT
:
3651 return version
< OFP14_VERSION
? OFPR_ACTION
: reason
;
3653 case OFPR_EXPLICIT_MISS
:
3654 return version
< OFP13_VERSION
? OFPR_ACTION
: OFPR_NO_MATCH
;
3656 case OFPR_IMPLICIT_MISS
:
3657 return OFPR_NO_MATCH
;
3659 case OFPR_N_REASONS
:
3665 /* Only NXT_PACKET_IN2 (not NXT_RESUME) should include NXCPT_USERDATA, so this
3666 * function omits it. The caller can add it itself if desired. */
3668 ofputil_put_packet_in(const struct ofputil_packet_in
*pin
,
3669 enum ofp_version version
, size_t include_bytes
,
3672 /* Add packet properties. */
3673 ofpprop_put(msg
, NXPINT_PACKET
, pin
->packet
, include_bytes
);
3674 if (include_bytes
!= pin
->packet_len
) {
3675 ofpprop_put_u32(msg
, NXPINT_FULL_LEN
, pin
->packet_len
);
3678 /* Add flow properties. */
3679 ofpprop_put_u8(msg
, NXPINT_TABLE_ID
, pin
->table_id
);
3680 if (pin
->cookie
!= OVS_BE64_MAX
) {
3681 ofpprop_put_be64(msg
, NXPINT_COOKIE
, pin
->cookie
);
3684 /* Add other properties. */
3685 ofpprop_put_u8(msg
, NXPINT_REASON
,
3686 encode_packet_in_reason(pin
->reason
, version
));
3688 size_t start
= ofpprop_start(msg
, NXPINT_METADATA
);
3689 oxm_put_raw(msg
, &pin
->flow_metadata
, version
);
3690 ofpprop_end(msg
, start
);
3694 put_actions_property(struct ofpbuf
*msg
, uint64_t prop_type
,
3695 enum ofp_version version
,
3696 const struct ofpact
*actions
, size_t actions_len
)
3699 size_t start
= ofpprop_start_nested(msg
, prop_type
);
3700 ofpacts_put_openflow_actions(actions
, actions_len
, msg
, version
);
3701 ofpprop_end(msg
, start
);
3705 enum nx_continuation_prop_type
{
3706 NXCPT_BRIDGE
= 0x8000,
3716 /* Only NXT_PACKET_IN2 (not NXT_RESUME) should include NXCPT_USERDATA, so this
3717 * function omits it. The caller can add it itself if desired. */
3719 ofputil_put_packet_in_private(const struct ofputil_packet_in_private
*pin
,
3720 enum ofp_version version
, size_t include_bytes
,
3723 ofputil_put_packet_in(&pin
->public, version
, include_bytes
, msg
);
3725 size_t continuation_ofs
= ofpprop_start_nested(msg
, NXPINT_CONTINUATION
);
3726 size_t inner_ofs
= msg
->size
;
3728 if (!uuid_is_zero(&pin
->bridge
)) {
3729 ofpprop_put_uuid(msg
, NXCPT_BRIDGE
, &pin
->bridge
);
3732 struct ofpbuf pin_stack
;
3733 ofpbuf_use_const(&pin_stack
, pin
->stack
, pin
->stack_size
);
3735 while (pin_stack
.size
) {
3737 uint8_t *val
= nx_stack_pop(&pin_stack
, &len
);
3738 ofpprop_put(msg
, NXCPT_STACK
, val
, len
);
3742 ofpprop_put_u32(msg
, NXCPT_MIRRORS
, pin
->mirrors
);
3745 if (pin
->conntracked
) {
3746 ofpprop_put_flag(msg
, NXCPT_CONNTRACKED
);
3749 if (pin
->actions_len
) {
3750 /* Divide 'pin->actions' into groups that begins with an
3751 * unroll_xlate action. For each group, emit a NXCPT_TABLE_ID and
3752 * NXCPT_COOKIE property (if either has changed; each is initially
3753 * assumed 0), then a NXCPT_ACTIONS property with the grouped
3756 * The alternative is to make OFPACT_UNROLL_XLATE public. We can
3757 * always do that later, since this is a private property. */
3758 const struct ofpact
*const end
= ofpact_end(pin
->actions
,
3760 const struct ofpact_unroll_xlate
*unroll
= NULL
;
3761 uint8_t table_id
= 0;
3762 ovs_be64 cookie
= 0;
3764 const struct ofpact
*a
;
3765 for (a
= pin
->actions
; ; a
= ofpact_next(a
)) {
3766 if (a
== end
|| a
->type
== OFPACT_UNROLL_XLATE
) {
3768 if (table_id
!= unroll
->rule_table_id
) {
3769 ofpprop_put_u8(msg
, NXCPT_TABLE_ID
,
3770 unroll
->rule_table_id
);
3771 table_id
= unroll
->rule_table_id
;
3773 if (cookie
!= unroll
->rule_cookie
) {
3774 ofpprop_put_be64(msg
, NXCPT_COOKIE
,
3775 unroll
->rule_cookie
);
3776 cookie
= unroll
->rule_cookie
;
3780 const struct ofpact
*start
3781 = unroll
? ofpact_next(&unroll
->ofpact
) : pin
->actions
;
3782 put_actions_property(msg
, NXCPT_ACTIONS
, version
,
3783 start
, (a
- start
) * sizeof *a
);
3788 unroll
= ofpact_get_UNROLL_XLATE(a
);
3793 if (pin
->action_set_len
) {
3794 size_t start
= ofpprop_start_nested(msg
, NXCPT_ACTION_SET
);
3795 ofpacts_put_openflow_actions(pin
->action_set
,
3796 pin
->action_set_len
, msg
, version
);
3797 ofpprop_end(msg
, start
);
3800 if (msg
->size
> inner_ofs
) {
3801 ofpprop_end(msg
, continuation_ofs
);
3803 msg
->size
= continuation_ofs
;
3807 static struct ofpbuf
*
3808 ofputil_encode_ofp10_packet_in(const struct ofputil_packet_in
*pin
)
3810 struct ofp10_packet_in
*opi
;
3813 msg
= ofpraw_alloc_xid(OFPRAW_OFPT10_PACKET_IN
, OFP10_VERSION
,
3814 htonl(0), pin
->packet_len
);
3815 opi
= ofpbuf_put_zeros(msg
, offsetof(struct ofp10_packet_in
, data
));
3816 opi
->total_len
= htons(pin
->packet_len
);
3817 opi
->in_port
= htons(ofp_to_u16(pin
->flow_metadata
.flow
.in_port
.ofp_port
));
3818 opi
->reason
= encode_packet_in_reason(pin
->reason
, OFP10_VERSION
);
3819 opi
->buffer_id
= htonl(UINT32_MAX
);
3824 static struct ofpbuf
*
3825 ofputil_encode_nx_packet_in(const struct ofputil_packet_in
*pin
,
3826 enum ofp_version version
)
3828 struct nx_packet_in
*npi
;
3832 /* The final argument is just an estimate of the space required. */
3833 msg
= ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN
, version
,
3834 htonl(0), NXM_TYPICAL_LEN
+ 2 + pin
->packet_len
);
3835 ofpbuf_put_zeros(msg
, sizeof *npi
);
3836 match_len
= nx_put_match(msg
, &pin
->flow_metadata
, 0, 0);
3837 ofpbuf_put_zeros(msg
, 2);
3840 npi
->buffer_id
= htonl(UINT32_MAX
);
3841 npi
->total_len
= htons(pin
->packet_len
);
3842 npi
->reason
= encode_packet_in_reason(pin
->reason
, version
);
3843 npi
->table_id
= pin
->table_id
;
3844 npi
->cookie
= pin
->cookie
;
3845 npi
->match_len
= htons(match_len
);
3850 static struct ofpbuf
*
3851 ofputil_encode_nx_packet_in2(const struct ofputil_packet_in_private
*pin
,
3852 enum ofp_version version
, size_t include_bytes
)
3854 /* 'extra' is just an estimate of the space required. */
3855 size_t extra
= (pin
->public.packet_len
3856 + NXM_TYPICAL_LEN
/* flow_metadata */
3857 + pin
->stack_size
* 4
3859 + pin
->action_set_len
3860 + 256); /* fudge factor */
3861 struct ofpbuf
*msg
= ofpraw_alloc_xid(OFPRAW_NXT_PACKET_IN2
, version
,
3864 ofputil_put_packet_in_private(pin
, version
, include_bytes
, msg
);
3865 if (pin
->public.userdata_len
) {
3866 ofpprop_put(msg
, NXPINT_USERDATA
, pin
->public.userdata
,
3867 pin
->public.userdata_len
);
3870 ofpmsg_update_length(msg
);
3874 static struct ofpbuf
*
3875 ofputil_encode_ofp11_packet_in(const struct ofputil_packet_in
*pin
)
3877 struct ofp11_packet_in
*opi
;
3880 msg
= ofpraw_alloc_xid(OFPRAW_OFPT11_PACKET_IN
, OFP11_VERSION
,
3881 htonl(0), pin
->packet_len
);
3882 opi
= ofpbuf_put_zeros(msg
, sizeof *opi
);
3883 opi
->buffer_id
= htonl(UINT32_MAX
);
3884 opi
->in_port
= ofputil_port_to_ofp11(
3885 pin
->flow_metadata
.flow
.in_port
.ofp_port
);
3886 opi
->in_phy_port
= opi
->in_port
;
3887 opi
->total_len
= htons(pin
->packet_len
);
3888 opi
->reason
= encode_packet_in_reason(pin
->reason
, OFP11_VERSION
);
3889 opi
->table_id
= pin
->table_id
;
3894 static struct ofpbuf
*
3895 ofputil_encode_ofp12_packet_in(const struct ofputil_packet_in
*pin
,
3896 enum ofp_version version
)
3898 enum ofpraw raw
= (version
>= OFP13_VERSION
3899 ? OFPRAW_OFPT13_PACKET_IN
3900 : OFPRAW_OFPT12_PACKET_IN
);
3903 /* The final argument is just an estimate of the space required. */
3904 msg
= ofpraw_alloc_xid(raw
, version
,
3905 htonl(0), NXM_TYPICAL_LEN
+ 2 + pin
->packet_len
);
3907 struct ofp12_packet_in
*opi
= ofpbuf_put_zeros(msg
, sizeof *opi
);
3908 opi
->buffer_id
= htonl(UINT32_MAX
);
3909 opi
->total_len
= htons(pin
->packet_len
);
3910 opi
->reason
= encode_packet_in_reason(pin
->reason
, version
);
3911 opi
->table_id
= pin
->table_id
;
3913 if (version
>= OFP13_VERSION
) {
3914 ovs_be64 cookie
= pin
->cookie
;
3915 ofpbuf_put(msg
, &cookie
, sizeof cookie
);
3918 oxm_put_match(msg
, &pin
->flow_metadata
, version
);
3919 ofpbuf_put_zeros(msg
, 2);
3924 /* Converts abstract ofputil_packet_in_private 'pin' into a PACKET_IN message
3925 * for 'protocol', using the packet-in format specified by 'packet_in_format'.
3927 * This function is really meant only for use by ovs-vswitchd. To any other
3928 * code, the "continuation" data, i.e. the data that is in struct
3929 * ofputil_packet_in_private but not in struct ofputil_packet_in, is supposed
3930 * to be opaque (and it might change from one OVS version to another). Thus,
3931 * if any other code wants to encode a packet-in, it should use a non-"private"
3932 * version of this function. (Such a version doesn't currently exist because
3933 * only ovs-vswitchd currently wants to encode packet-ins. If you need one,
3936 ofputil_encode_packet_in_private(const struct ofputil_packet_in_private
*pin
,
3937 enum ofputil_protocol protocol
,
3938 enum nx_packet_in_format packet_in_format
)
3940 enum ofp_version version
= ofputil_protocol_to_ofp_version(protocol
);
3943 switch (packet_in_format
) {
3944 case NXPIF_STANDARD
:
3946 case OFPUTIL_P_OF10_STD
:
3947 case OFPUTIL_P_OF10_STD_TID
:
3948 case OFPUTIL_P_OF10_NXM
:
3949 case OFPUTIL_P_OF10_NXM_TID
:
3950 msg
= ofputil_encode_ofp10_packet_in(&pin
->public);
3953 case OFPUTIL_P_OF11_STD
:
3954 msg
= ofputil_encode_ofp11_packet_in(&pin
->public);
3957 case OFPUTIL_P_OF12_OXM
:
3958 case OFPUTIL_P_OF13_OXM
:
3959 case OFPUTIL_P_OF14_OXM
:
3960 case OFPUTIL_P_OF15_OXM
:
3961 case OFPUTIL_P_OF16_OXM
:
3962 msg
= ofputil_encode_ofp12_packet_in(&pin
->public, version
);
3970 case NXPIF_NXT_PACKET_IN
:
3971 msg
= ofputil_encode_nx_packet_in(&pin
->public, version
);
3974 case NXPIF_NXT_PACKET_IN2
:
3975 return ofputil_encode_nx_packet_in2(pin
, version
,
3976 pin
->public.packet_len
);
3982 ofpbuf_put(msg
, pin
->public.packet
, pin
->public.packet_len
);
3983 ofpmsg_update_length(msg
);
3987 /* Returns a string form of 'reason'. The return value is either a statically
3988 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
3989 * 'bufsize' should be at least OFPUTIL_PACKET_IN_REASON_BUFSIZE. */
3991 ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason
,
3992 char *reasonbuf
, size_t bufsize
)
3999 case OFPR_INVALID_TTL
:
4000 return "invalid_ttl";
4001 case OFPR_ACTION_SET
:
4002 return "action_set";
4005 case OFPR_PACKET_OUT
:
4006 return "packet_out";
4007 case OFPR_EXPLICIT_MISS
:
4008 case OFPR_IMPLICIT_MISS
:
4011 case OFPR_N_REASONS
:
4013 snprintf(reasonbuf
, bufsize
, "%d", (int) reason
);
4019 ofputil_packet_in_reason_from_string(const char *s
,
4020 enum ofp_packet_in_reason
*reason
)
4024 for (i
= 0; i
< OFPR_N_REASONS
; i
++) {
4025 char reasonbuf
[OFPUTIL_PACKET_IN_REASON_BUFSIZE
];
4026 const char *reason_s
;
4028 reason_s
= ofputil_packet_in_reason_to_string(i
, reasonbuf
,
4030 if (!strcasecmp(s
, reason_s
)) {
4038 /* Returns a newly allocated NXT_RESUME message for 'pin', with the given
4039 * 'continuation', for 'protocol'. This message is suitable for resuming the
4040 * pipeline traveral of the packet represented by 'pin', if sent to the switch
4041 * from which 'pin' was received. */
4043 ofputil_encode_resume(const struct ofputil_packet_in
*pin
,
4044 const struct ofpbuf
*continuation
,
4045 enum ofputil_protocol protocol
)
4047 enum ofp_version version
= ofputil_protocol_to_ofp_version(protocol
);
4048 size_t extra
= pin
->packet_len
+ NXM_TYPICAL_LEN
+ continuation
->size
;
4049 struct ofpbuf
*msg
= ofpraw_alloc_xid(OFPRAW_NXT_RESUME
, version
,
4051 ofputil_put_packet_in(pin
, version
, pin
->packet_len
, msg
);
4052 ofpprop_put_nested(msg
, NXPINT_CONTINUATION
, continuation
);
4053 ofpmsg_update_length(msg
);
4058 parse_stack_prop(const struct ofpbuf
*property
, struct ofpbuf
*stack
)
4060 unsigned int len
= ofpbuf_msgsize(property
);
4061 if (len
> sizeof(union mf_subvalue
)) {
4062 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXCPT_STACK property has bad length %u",
4064 return OFPERR_OFPBPC_BAD_LEN
;
4066 nx_stack_push_bottom(stack
, property
->msg
, len
);
4071 parse_actions_property(struct ofpbuf
*property
, enum ofp_version version
,
4072 struct ofpbuf
*ofpacts
)
4074 if (!ofpbuf_try_pull(property
, ROUND_UP(ofpbuf_headersize(property
), 8))) {
4075 VLOG_WARN_RL(&bad_ofmsg_rl
, "actions property has bad length %"PRIu32
,
4077 return OFPERR_OFPBPC_BAD_LEN
;
4080 return ofpacts_pull_openflow_actions(property
, property
->size
,
4081 version
, NULL
, NULL
, ofpacts
);
4084 /* This is like ofputil_decode_packet_in(), except that it decodes the
4085 * continuation data into 'pin'. The format of this data is supposed to be
4086 * opaque to any process other than ovs-vswitchd, so this function should not
4087 * be used outside ovs-vswitchd.
4089 * 'vl_mff_map' is an optional parameter that is used to validate the length
4090 * of variable length mf_fields in 'match'. If it is not provided, the
4091 * default mf_fields with maximum length will be used.
4093 * When successful, 'pin' contains some dynamically allocated data. Call
4094 * ofputil_packet_in_private_destroy() to free this data. */
4096 ofputil_decode_packet_in_private(const struct ofp_header
*oh
, bool loose
,
4097 const struct tun_table
*tun_table
,
4098 const struct vl_mff_map
*vl_mff_map
,
4099 struct ofputil_packet_in_private
*pin
,
4100 size_t *total_len
, uint32_t *buffer_id
)
4102 memset(pin
, 0, sizeof *pin
);
4104 struct ofpbuf continuation
;
4106 error
= ofputil_decode_packet_in(oh
, loose
, tun_table
, vl_mff_map
,
4107 &pin
->public, total_len
, buffer_id
,
4113 struct ofpbuf actions
, action_set
;
4114 ofpbuf_init(&actions
, 0);
4115 ofpbuf_init(&action_set
, 0);
4117 uint8_t table_id
= 0;
4118 ovs_be64 cookie
= 0;
4120 struct ofpbuf stack
;
4121 ofpbuf_init(&stack
, 0);
4123 while (continuation
.size
> 0) {
4124 struct ofpbuf payload
;
4127 error
= ofpprop_pull(&continuation
, &payload
, &type
);
4134 error
= ofpprop_parse_uuid(&payload
, &pin
->bridge
);
4138 error
= parse_stack_prop(&payload
, &stack
);
4142 error
= ofpprop_parse_u32(&payload
, &pin
->mirrors
);
4145 case NXCPT_CONNTRACKED
:
4146 pin
->conntracked
= true;
4149 case NXCPT_TABLE_ID
:
4150 error
= ofpprop_parse_u8(&payload
, &table_id
);
4154 error
= ofpprop_parse_be64(&payload
, &cookie
);
4157 case NXCPT_ACTIONS
: {
4158 struct ofpact_unroll_xlate
*unroll
4159 = ofpact_put_UNROLL_XLATE(&actions
);
4160 unroll
->rule_table_id
= table_id
;
4161 unroll
->rule_cookie
= cookie
;
4162 error
= parse_actions_property(&payload
, oh
->version
, &actions
);
4166 case NXCPT_ACTION_SET
:
4167 error
= parse_actions_property(&payload
, oh
->version
, &action_set
);
4171 error
= OFPPROP_UNKNOWN(loose
, "continuation", type
);
4179 pin
->actions_len
= actions
.size
;
4180 pin
->actions
= ofpbuf_steal_data(&actions
);
4181 pin
->action_set_len
= action_set
.size
;
4182 pin
->action_set
= ofpbuf_steal_data(&action_set
);
4183 pin
->stack_size
= stack
.size
;
4184 pin
->stack
= ofpbuf_steal_data(&stack
);
4187 ofputil_packet_in_private_destroy(pin
);
4193 /* Frees data in 'pin' that is dynamically allocated by
4194 * ofputil_decode_packet_in_private().
4196 * 'pin->public' contains some pointer members that
4197 * ofputil_decode_packet_in_private() doesn't initialize to newly allocated
4198 * data, so this function doesn't free those. */
4200 ofputil_packet_in_private_destroy(struct ofputil_packet_in_private
*pin
)
4205 free(pin
->action_set
);
4209 /* Converts an OFPT_PACKET_OUT in 'opo' into an abstract ofputil_packet_out in
4212 * Uses 'ofpacts' to store the abstract OFPACT_* version of the packet out
4213 * message's actions. The caller must initialize 'ofpacts' and retains
4214 * ownership of it. 'po->ofpacts' will point into the 'ofpacts' buffer.
4216 * 'po->packet' refers to the packet data in 'oh', so the buffer containing
4217 * 'oh' must not be destroyed while 'po' is being used.
4219 * Returns 0 if successful, otherwise an OFPERR_* value. */
4221 ofputil_decode_packet_out(struct ofputil_packet_out
*po
,
4222 const struct ofp_header
*oh
,
4223 const struct tun_table
*tun_table
,
4224 struct ofpbuf
*ofpacts
)
4226 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
4227 enum ofpraw raw
= ofpraw_pull_assert(&b
);
4229 ofpbuf_clear(ofpacts
);
4230 match_init_catchall(&po
->flow_metadata
);
4231 if (raw
== OFPRAW_OFPT15_PACKET_OUT
) {
4233 const struct ofp15_packet_out
*opo
= ofpbuf_pull(&b
, sizeof *opo
);
4235 po
->buffer_id
= ntohl(opo
->buffer_id
);
4236 error
= oxm_pull_match_loose(&b
, true, tun_table
, &po
->flow_metadata
);
4241 if (!po
->flow_metadata
.wc
.masks
.in_port
.ofp_port
) {
4242 return OFPERR_OFPBRC_BAD_PORT
;
4245 error
= ofpacts_pull_openflow_actions(&b
, ntohs(opo
->actions_len
),
4246 oh
->version
, NULL
, NULL
,
4251 } else if (raw
== OFPRAW_OFPT11_PACKET_OUT
) {
4254 const struct ofp11_packet_out
*opo
= ofpbuf_pull(&b
, sizeof *opo
);
4256 po
->buffer_id
= ntohl(opo
->buffer_id
);
4257 error
= ofputil_port_from_ofp11(opo
->in_port
, &in_port
);
4261 match_set_packet_type(&po
->flow_metadata
, htonl(PT_ETH
));
4262 match_set_in_port(&po
->flow_metadata
, in_port
);
4264 error
= ofpacts_pull_openflow_actions(&b
, ntohs(opo
->actions_len
),
4265 oh
->version
, NULL
, NULL
,
4270 } else if (raw
== OFPRAW_OFPT10_PACKET_OUT
) {
4272 const struct ofp10_packet_out
*opo
= ofpbuf_pull(&b
, sizeof *opo
);
4274 po
->buffer_id
= ntohl(opo
->buffer_id
);
4275 match_set_packet_type(&po
->flow_metadata
, htonl(PT_ETH
));
4276 match_set_in_port(&po
->flow_metadata
, u16_to_ofp(ntohs(opo
->in_port
)));
4278 error
= ofpacts_pull_openflow_actions(&b
, ntohs(opo
->actions_len
),
4279 oh
->version
, NULL
, NULL
,
4288 ofp_port_t in_port
= po
->flow_metadata
.flow
.in_port
.ofp_port
;
4289 if (ofp_to_u16(in_port
) >= ofp_to_u16(OFPP_MAX
)
4290 && in_port
!= OFPP_LOCAL
4291 && in_port
!= OFPP_NONE
4292 && in_port
!= OFPP_CONTROLLER
) {
4293 VLOG_WARN_RL(&bad_ofmsg_rl
, "packet-out has bad input port %#"PRIx32
,
4294 po
->flow_metadata
.flow
.in_port
.ofp_port
);
4295 return OFPERR_OFPBRC_BAD_PORT
;
4298 po
->ofpacts
= ofpacts
->data
;
4299 po
->ofpacts_len
= ofpacts
->size
;
4301 if (po
->buffer_id
== UINT32_MAX
) {
4302 po
->packet
= b
.data
;
4303 po
->packet_len
= b
.size
;
4312 /* ofputil_phy_port */
4314 /* NETDEV_F_* to and from OFPPF_* and OFPPF10_*. */
4315 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD
== OFPPF_10MB_HD
); /* bit 0 */
4316 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD
== OFPPF_10MB_FD
); /* bit 1 */
4317 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD
== OFPPF_100MB_HD
); /* bit 2 */
4318 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD
== OFPPF_100MB_FD
); /* bit 3 */
4319 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD
== OFPPF_1GB_HD
); /* bit 4 */
4320 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD
== OFPPF_1GB_FD
); /* bit 5 */
4321 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD
== OFPPF_10GB_FD
); /* bit 6 */
4323 /* NETDEV_F_ bits 11...15 are OFPPF10_ bits 7...11: */
4324 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER
== (OFPPF10_COPPER
<< 4));
4325 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER
== (OFPPF10_FIBER
<< 4));
4326 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG
== (OFPPF10_AUTONEG
<< 4));
4327 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE
== (OFPPF10_PAUSE
<< 4));
4328 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM
== (OFPPF10_PAUSE_ASYM
<< 4));
4330 static enum netdev_features
4331 netdev_port_features_from_ofp10(ovs_be32 ofp10_
)
4333 uint32_t ofp10
= ntohl(ofp10_
);
4334 return (ofp10
& 0x7f) | ((ofp10
& 0xf80) << 4);
4338 netdev_port_features_to_ofp10(enum netdev_features features
)
4340 return htonl((features
& 0x7f) | ((features
& 0xf800) >> 4));
4343 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_HD
== OFPPF_10MB_HD
); /* bit 0 */
4344 BUILD_ASSERT_DECL((int) NETDEV_F_10MB_FD
== OFPPF_10MB_FD
); /* bit 1 */
4345 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_HD
== OFPPF_100MB_HD
); /* bit 2 */
4346 BUILD_ASSERT_DECL((int) NETDEV_F_100MB_FD
== OFPPF_100MB_FD
); /* bit 3 */
4347 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_HD
== OFPPF_1GB_HD
); /* bit 4 */
4348 BUILD_ASSERT_DECL((int) NETDEV_F_1GB_FD
== OFPPF_1GB_FD
); /* bit 5 */
4349 BUILD_ASSERT_DECL((int) NETDEV_F_10GB_FD
== OFPPF_10GB_FD
); /* bit 6 */
4350 BUILD_ASSERT_DECL((int) NETDEV_F_40GB_FD
== OFPPF11_40GB_FD
); /* bit 7 */
4351 BUILD_ASSERT_DECL((int) NETDEV_F_100GB_FD
== OFPPF11_100GB_FD
); /* bit 8 */
4352 BUILD_ASSERT_DECL((int) NETDEV_F_1TB_FD
== OFPPF11_1TB_FD
); /* bit 9 */
4353 BUILD_ASSERT_DECL((int) NETDEV_F_OTHER
== OFPPF11_OTHER
); /* bit 10 */
4354 BUILD_ASSERT_DECL((int) NETDEV_F_COPPER
== OFPPF11_COPPER
); /* bit 11 */
4355 BUILD_ASSERT_DECL((int) NETDEV_F_FIBER
== OFPPF11_FIBER
); /* bit 12 */
4356 BUILD_ASSERT_DECL((int) NETDEV_F_AUTONEG
== OFPPF11_AUTONEG
); /* bit 13 */
4357 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE
== OFPPF11_PAUSE
); /* bit 14 */
4358 BUILD_ASSERT_DECL((int) NETDEV_F_PAUSE_ASYM
== OFPPF11_PAUSE_ASYM
);/* bit 15 */
4360 static enum netdev_features
4361 netdev_port_features_from_ofp11(ovs_be32 ofp11
)
4363 return ntohl(ofp11
) & 0xffff;
4367 netdev_port_features_to_ofp11(enum netdev_features features
)
4369 return htonl(features
& 0xffff);
4373 ofputil_decode_ofp10_phy_port(struct ofputil_phy_port
*pp
,
4374 const struct ofp10_phy_port
*opp
)
4376 pp
->port_no
= u16_to_ofp(ntohs(opp
->port_no
));
4377 pp
->hw_addr
= opp
->hw_addr
;
4378 ovs_strlcpy_arrays(pp
->name
, opp
->name
);
4380 pp
->config
= ntohl(opp
->config
) & OFPPC10_ALL
;
4381 pp
->state
= ntohl(opp
->state
) & OFPPS10_ALL
;
4383 pp
->curr
= netdev_port_features_from_ofp10(opp
->curr
);
4384 pp
->advertised
= netdev_port_features_from_ofp10(opp
->advertised
);
4385 pp
->supported
= netdev_port_features_from_ofp10(opp
->supported
);
4386 pp
->peer
= netdev_port_features_from_ofp10(opp
->peer
);
4388 pp
->curr_speed
= netdev_features_to_bps(pp
->curr
, 0) / 1000;
4389 pp
->max_speed
= netdev_features_to_bps(pp
->supported
, 0) / 1000;
4395 ofputil_decode_ofp11_port(struct ofputil_phy_port
*pp
,
4396 const struct ofp11_port
*op
)
4400 error
= ofputil_port_from_ofp11(op
->port_no
, &pp
->port_no
);
4404 pp
->hw_addr
= op
->hw_addr
;
4405 ovs_strlcpy_arrays(pp
->name
, op
->name
);
4407 pp
->config
= ntohl(op
->config
) & OFPPC11_ALL
;
4408 pp
->state
= ntohl(op
->state
) & OFPPS11_ALL
;
4410 pp
->curr
= netdev_port_features_from_ofp11(op
->curr
);
4411 pp
->advertised
= netdev_port_features_from_ofp11(op
->advertised
);
4412 pp
->supported
= netdev_port_features_from_ofp11(op
->supported
);
4413 pp
->peer
= netdev_port_features_from_ofp11(op
->peer
);
4415 pp
->curr_speed
= ntohl(op
->curr_speed
);
4416 pp
->max_speed
= ntohl(op
->max_speed
);
4422 parse_ofp14_port_ethernet_property(const struct ofpbuf
*payload
,
4423 struct ofputil_phy_port
*pp
)
4425 struct ofp14_port_desc_prop_ethernet
*eth
= payload
->data
;
4427 if (payload
->size
!= sizeof *eth
) {
4428 return OFPERR_OFPBPC_BAD_LEN
;
4431 pp
->curr
= netdev_port_features_from_ofp11(eth
->curr
);
4432 pp
->advertised
= netdev_port_features_from_ofp11(eth
->advertised
);
4433 pp
->supported
= netdev_port_features_from_ofp11(eth
->supported
);
4434 pp
->peer
= netdev_port_features_from_ofp11(eth
->peer
);
4436 pp
->curr_speed
= ntohl(eth
->curr_speed
);
4437 pp
->max_speed
= ntohl(eth
->max_speed
);
4443 ofputil_pull_ofp14_port_properties(const void *props
, size_t len
,
4444 struct ofputil_phy_port
*pp
)
4446 struct ofpbuf properties
= ofpbuf_const_initializer(props
, len
);
4447 while (properties
.size
> 0) {
4448 struct ofpbuf payload
;
4452 error
= ofpprop_pull(&properties
, &payload
, &type
);
4458 case OFPPDPT14_ETHERNET
:
4459 error
= parse_ofp14_port_ethernet_property(&payload
, pp
);
4463 error
= OFPPROP_UNKNOWN(true, "port", type
);
4476 ofputil_pull_ofp14_port(struct ofputil_phy_port
*pp
, struct ofpbuf
*msg
)
4478 const struct ofp14_port
*op
= ofpbuf_try_pull(msg
, sizeof *op
);
4480 return OFPERR_OFPBRC_BAD_LEN
;
4483 size_t len
= ntohs(op
->length
);
4484 if (len
< sizeof *op
|| len
- sizeof *op
> msg
->size
) {
4485 return OFPERR_OFPBRC_BAD_LEN
;
4489 enum ofperr error
= ofputil_port_from_ofp11(op
->port_no
, &pp
->port_no
);
4493 pp
->hw_addr
= op
->hw_addr
;
4494 ovs_strlcpy_arrays(pp
->name
, op
->name
);
4496 pp
->config
= ntohl(op
->config
) & OFPPC11_ALL
;
4497 pp
->state
= ntohl(op
->state
) & OFPPS11_ALL
;
4499 return ofputil_pull_ofp14_port_properties(ofpbuf_pull(msg
, len
), len
, pp
);
4503 ofputil_pull_ofp16_port(struct ofputil_phy_port
*pp
, struct ofpbuf
*msg
)
4505 const struct ofp16_port
*op
= ofpbuf_try_pull(msg
, sizeof *op
);
4507 return OFPERR_OFPBRC_BAD_LEN
;
4510 size_t len
= ntohs(op
->length
);
4511 if (len
< sizeof *op
|| len
- sizeof *op
> msg
->size
) {
4512 return OFPERR_OFPBRC_BAD_LEN
;
4516 enum ofperr error
= ofputil_port_from_ofp11(op
->port_no
, &pp
->port_no
);
4520 if (op
->hw_addr_type
& htons(OFPPHAT16_EUI48
)) {
4521 pp
->hw_addr
= op
->hw_addr
;
4523 if (op
->hw_addr_type
& htons(OFPPHAT16_EUI64
)) {
4524 pp
->hw_addr64
= op
->hw_addr64
;
4526 ovs_strlcpy_arrays(pp
->name
, op
->name
);
4528 pp
->config
= ntohl(op
->config
) & OFPPC11_ALL
;
4529 pp
->state
= ntohl(op
->state
) & OFPPS11_ALL
;
4531 return ofputil_pull_ofp14_port_properties(ofpbuf_pull(msg
, len
), len
, pp
);
4535 ofputil_encode_ofp10_phy_port(const struct ofputil_phy_port
*pp
,
4536 struct ofp10_phy_port
*opp
)
4538 memset(opp
, 0, sizeof *opp
);
4540 opp
->port_no
= htons(ofp_to_u16(pp
->port_no
));
4541 opp
->hw_addr
= pp
->hw_addr
;
4542 ovs_strlcpy_arrays(opp
->name
, pp
->name
);
4544 opp
->config
= htonl(pp
->config
& OFPPC10_ALL
);
4545 opp
->state
= htonl(pp
->state
& OFPPS10_ALL
);
4547 opp
->curr
= netdev_port_features_to_ofp10(pp
->curr
);
4548 opp
->advertised
= netdev_port_features_to_ofp10(pp
->advertised
);
4549 opp
->supported
= netdev_port_features_to_ofp10(pp
->supported
);
4550 opp
->peer
= netdev_port_features_to_ofp10(pp
->peer
);
4554 ofputil_encode_ofp11_port(const struct ofputil_phy_port
*pp
,
4555 struct ofp11_port
*op
)
4557 memset(op
, 0, sizeof *op
);
4559 op
->port_no
= ofputil_port_to_ofp11(pp
->port_no
);
4560 op
->hw_addr
= pp
->hw_addr
;
4561 ovs_strlcpy_arrays(op
->name
, pp
->name
);
4563 op
->config
= htonl(pp
->config
& OFPPC11_ALL
);
4564 op
->state
= htonl(pp
->state
& OFPPS11_ALL
);
4566 op
->curr
= netdev_port_features_to_ofp11(pp
->curr
);
4567 op
->advertised
= netdev_port_features_to_ofp11(pp
->advertised
);
4568 op
->supported
= netdev_port_features_to_ofp11(pp
->supported
);
4569 op
->peer
= netdev_port_features_to_ofp11(pp
->peer
);
4571 op
->curr_speed
= htonl(pp
->curr_speed
);
4572 op
->max_speed
= htonl(pp
->max_speed
);
4576 ofputil_encode_ofp14_port_ethernet_prop(
4577 const struct ofputil_phy_port
*pp
,
4578 struct ofp14_port_desc_prop_ethernet
*eth
)
4580 eth
->curr
= netdev_port_features_to_ofp11(pp
->curr
);
4581 eth
->advertised
= netdev_port_features_to_ofp11(pp
->advertised
);
4582 eth
->supported
= netdev_port_features_to_ofp11(pp
->supported
);
4583 eth
->peer
= netdev_port_features_to_ofp11(pp
->peer
);
4584 eth
->curr_speed
= htonl(pp
->curr_speed
);
4585 eth
->max_speed
= htonl(pp
->max_speed
);
4589 ofputil_put_ofp14_port(const struct ofputil_phy_port
*pp
, struct ofpbuf
*b
)
4591 struct ofp14_port
*op
;
4592 struct ofp14_port_desc_prop_ethernet
*eth
;
4594 ofpbuf_prealloc_tailroom(b
, sizeof *op
+ sizeof *eth
);
4596 op
= ofpbuf_put_zeros(b
, sizeof *op
);
4597 op
->port_no
= ofputil_port_to_ofp11(pp
->port_no
);
4598 op
->length
= htons(sizeof *op
+ sizeof *eth
);
4599 op
->hw_addr
= pp
->hw_addr
;
4600 ovs_strlcpy_arrays(op
->name
, pp
->name
);
4601 op
->config
= htonl(pp
->config
& OFPPC11_ALL
);
4602 op
->state
= htonl(pp
->state
& OFPPS11_ALL
);
4604 eth
= ofpprop_put_zeros(b
, OFPPDPT14_ETHERNET
, sizeof *eth
);
4605 ofputil_encode_ofp14_port_ethernet_prop(pp
, eth
);
4609 ofputil_put_ofp16_port(const struct ofputil_phy_port
*pp
, struct ofpbuf
*b
)
4611 struct ofp16_port
*op
;
4612 struct ofp14_port_desc_prop_ethernet
*eth
;
4614 ofpbuf_prealloc_tailroom(b
, sizeof *op
+ sizeof *eth
);
4616 op
= ofpbuf_put_zeros(b
, sizeof *op
);
4617 op
->port_no
= ofputil_port_to_ofp11(pp
->port_no
);
4618 op
->length
= htons(sizeof *op
+ sizeof *eth
);
4619 if (!eth_addr_is_zero(pp
->hw_addr
)) {
4620 op
->hw_addr_type
|= htons(OFPPHAT16_EUI48
);
4621 op
->hw_addr
= pp
->hw_addr
;
4623 if (!eth_addr64_is_zero(pp
->hw_addr64
)) {
4624 op
->hw_addr_type
|= htons(OFPPHAT16_EUI64
);
4625 op
->hw_addr64
= pp
->hw_addr64
;
4627 ovs_strlcpy_arrays(op
->name
, pp
->name
);
4628 op
->config
= htonl(pp
->config
& OFPPC11_ALL
);
4629 op
->state
= htonl(pp
->state
& OFPPS11_ALL
);
4631 eth
= ofpprop_put_zeros(b
, OFPPDPT14_ETHERNET
, sizeof *eth
);
4632 ofputil_encode_ofp14_port_ethernet_prop(pp
, eth
);
4636 ofputil_put_phy_port(enum ofp_version ofp_version
,
4637 const struct ofputil_phy_port
*pp
, struct ofpbuf
*b
)
4639 switch (ofp_version
) {
4640 case OFP10_VERSION
: {
4641 struct ofp10_phy_port
*opp
= ofpbuf_put_uninit(b
, sizeof *opp
);
4642 ofputil_encode_ofp10_phy_port(pp
, opp
);
4648 case OFP13_VERSION
: {
4649 struct ofp11_port
*op
= ofpbuf_put_uninit(b
, sizeof *op
);
4650 ofputil_encode_ofp11_port(pp
, op
);
4656 ofputil_put_ofp14_port(pp
, b
);
4659 ofputil_put_ofp16_port(pp
, b
);
4668 ofputil_decode_port_desc_stats_request(const struct ofp_header
*request
,
4671 struct ofpbuf b
= ofpbuf_const_initializer(request
,
4672 ntohs(request
->length
));
4673 enum ofpraw raw
= ofpraw_pull_assert(&b
);
4674 if (raw
== OFPRAW_OFPST10_PORT_DESC_REQUEST
) {
4677 } else if (raw
== OFPRAW_OFPST15_PORT_DESC_REQUEST
) {
4678 ovs_be32
*ofp11_port
;
4680 ofp11_port
= ofpbuf_pull(&b
, sizeof *ofp11_port
);
4681 return ofputil_port_from_ofp11(*ofp11_port
, port
);
4688 ofputil_encode_port_desc_stats_request(enum ofp_version ofp_version
,
4691 struct ofpbuf
*request
;
4693 switch (ofp_version
) {
4699 request
= ofpraw_alloc(OFPRAW_OFPST10_PORT_DESC_REQUEST
,
4703 case OFP16_VERSION
:{
4704 struct ofp15_port_desc_request
*req
;
4705 request
= ofpraw_alloc(OFPRAW_OFPST15_PORT_DESC_REQUEST
,
4707 req
= ofpbuf_put_zeros(request
, sizeof *req
);
4708 req
->port_no
= ofputil_port_to_ofp11(port
);
4719 ofputil_append_port_desc_stats_reply(const struct ofputil_phy_port
*pp
,
4720 struct ovs_list
*replies
)
4722 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
4723 size_t start_ofs
= reply
->size
;
4725 ofputil_put_phy_port(ofpmp_version(replies
), pp
, reply
);
4726 ofpmp_postappend(replies
, start_ofs
);
4729 /* ofputil_switch_config */
4731 /* Decodes 'oh', which must be an OFPT_GET_CONFIG_REPLY or OFPT_SET_CONFIG
4732 * message, into 'config'. Returns false if 'oh' contained any flags that
4733 * aren't specified in its version of OpenFlow, true otherwise. */
4735 ofputil_decode_switch_config(const struct ofp_header
*oh
,
4736 struct ofputil_switch_config
*config
)
4738 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
4739 ofpraw_pull_assert(&b
);
4741 const struct ofp_switch_config
*osc
= ofpbuf_pull(&b
, sizeof *osc
);
4742 config
->frag
= ntohs(osc
->flags
) & OFPC_FRAG_MASK
;
4743 config
->miss_send_len
= ntohs(osc
->miss_send_len
);
4745 ovs_be16 valid_mask
= htons(OFPC_FRAG_MASK
);
4746 if (oh
->version
< OFP13_VERSION
) {
4747 const ovs_be16 ttl_bit
= htons(OFPC_INVALID_TTL_TO_CONTROLLER
);
4748 valid_mask
|= ttl_bit
;
4749 config
->invalid_ttl_to_controller
= (osc
->flags
& ttl_bit
) != 0;
4751 config
->invalid_ttl_to_controller
= -1;
4754 return !(osc
->flags
& ~valid_mask
);
4758 ofputil_decode_get_config_reply(const struct ofp_header
*oh
,
4759 struct ofputil_switch_config
*config
)
4761 ofputil_decode_switch_config(oh
, config
);
4765 ofputil_decode_set_config(const struct ofp_header
*oh
,
4766 struct ofputil_switch_config
*config
)
4768 return (ofputil_decode_switch_config(oh
, config
)
4770 : OFPERR_OFPSCFC_BAD_FLAGS
);
4773 static struct ofpbuf
*
4774 ofputil_put_switch_config(const struct ofputil_switch_config
*config
,
4777 const struct ofp_header
*oh
= b
->data
;
4778 struct ofp_switch_config
*osc
= ofpbuf_put_zeros(b
, sizeof *osc
);
4779 osc
->flags
= htons(config
->frag
);
4780 if (config
->invalid_ttl_to_controller
> 0 && oh
->version
< OFP13_VERSION
) {
4781 osc
->flags
|= htons(OFPC_INVALID_TTL_TO_CONTROLLER
);
4783 osc
->miss_send_len
= htons(config
->miss_send_len
);
4788 ofputil_encode_get_config_reply(const struct ofp_header
*request
,
4789 const struct ofputil_switch_config
*config
)
4791 struct ofpbuf
*b
= ofpraw_alloc_reply(OFPRAW_OFPT_GET_CONFIG_REPLY
,
4793 return ofputil_put_switch_config(config
, b
);
4797 ofputil_encode_set_config(const struct ofputil_switch_config
*config
,
4798 enum ofp_version version
)
4800 struct ofpbuf
*b
= ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG
, version
, 0);
4801 return ofputil_put_switch_config(config
, b
);
4804 /* ofputil_switch_features */
4806 #define OFPC_COMMON (OFPC_FLOW_STATS | OFPC_TABLE_STATS | OFPC_PORT_STATS | \
4807 OFPC_IP_REASM | OFPC_QUEUE_STATS)
4808 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_STATS
== OFPC_FLOW_STATS
);
4809 BUILD_ASSERT_DECL((int) OFPUTIL_C_TABLE_STATS
== OFPC_TABLE_STATS
);
4810 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_STATS
== OFPC_PORT_STATS
);
4811 BUILD_ASSERT_DECL((int) OFPUTIL_C_IP_REASM
== OFPC_IP_REASM
);
4812 BUILD_ASSERT_DECL((int) OFPUTIL_C_QUEUE_STATS
== OFPC_QUEUE_STATS
);
4813 BUILD_ASSERT_DECL((int) OFPUTIL_C_ARP_MATCH_IP
== OFPC_ARP_MATCH_IP
);
4814 BUILD_ASSERT_DECL((int) OFPUTIL_C_PORT_BLOCKED
== OFPC12_PORT_BLOCKED
);
4815 BUILD_ASSERT_DECL((int) OFPUTIL_C_BUNDLES
== OFPC14_BUNDLES
);
4816 BUILD_ASSERT_DECL((int) OFPUTIL_C_FLOW_MONITORING
== OFPC14_FLOW_MONITORING
);
4819 ofputil_capabilities_mask(enum ofp_version ofp_version
)
4821 /* Handle capabilities whose bit is unique for all OpenFlow versions */
4822 switch (ofp_version
) {
4825 return OFPC_COMMON
| OFPC_ARP_MATCH_IP
;
4828 return OFPC_COMMON
| OFPC12_PORT_BLOCKED
;
4832 return OFPC_COMMON
| OFPC12_PORT_BLOCKED
| OFPC14_BUNDLES
4833 | OFPC14_FLOW_MONITORING
;
4835 /* Caller needs to check osf->header.version itself */
4840 /* Pulls an OpenFlow "switch_features" structure from 'b' and decodes it into
4841 * an abstract representation in '*features', readying 'b' to iterate over the
4842 * OpenFlow port structures following 'osf' with later calls to
4843 * ofputil_pull_phy_port(). Returns 0 if successful, otherwise an OFPERR_*
4846 ofputil_pull_switch_features(struct ofpbuf
*b
,
4847 struct ofputil_switch_features
*features
)
4849 const struct ofp_header
*oh
= b
->data
;
4850 enum ofpraw raw
= ofpraw_pull_assert(b
);
4851 const struct ofp_switch_features
*osf
= ofpbuf_pull(b
, sizeof *osf
);
4852 features
->datapath_id
= ntohll(osf
->datapath_id
);
4853 features
->n_buffers
= ntohl(osf
->n_buffers
);
4854 features
->n_tables
= osf
->n_tables
;
4855 features
->auxiliary_id
= 0;
4857 features
->capabilities
= ntohl(osf
->capabilities
) &
4858 ofputil_capabilities_mask(oh
->version
);
4860 if (raw
== OFPRAW_OFPT10_FEATURES_REPLY
) {
4861 if (osf
->capabilities
& htonl(OFPC10_STP
)) {
4862 features
->capabilities
|= OFPUTIL_C_STP
;
4864 features
->ofpacts
= ofpact_bitmap_from_openflow(osf
->actions
,
4866 } else if (raw
== OFPRAW_OFPT11_FEATURES_REPLY
4867 || raw
== OFPRAW_OFPT13_FEATURES_REPLY
) {
4868 if (osf
->capabilities
& htonl(OFPC11_GROUP_STATS
)) {
4869 features
->capabilities
|= OFPUTIL_C_GROUP_STATS
;
4871 features
->ofpacts
= 0;
4872 if (raw
== OFPRAW_OFPT13_FEATURES_REPLY
) {
4873 features
->auxiliary_id
= osf
->auxiliary_id
;
4876 return OFPERR_OFPBRC_BAD_VERSION
;
4882 /* In OpenFlow 1.0, 1.1, and 1.2, an OFPT_FEATURES_REPLY message lists all the
4883 * switch's ports, unless there are too many to fit. In OpenFlow 1.3 and
4884 * later, an OFPT_FEATURES_REPLY does not list ports at all.
4886 * Given a buffer 'b' that contains a Features Reply message, this message
4887 * checks if it contains a complete list of the switch's ports. Returns true,
4888 * if so. Returns false if the list is missing (OF1.3+) or incomplete
4889 * (OF1.0/1.1/1.2), and in the latter case removes all of the ports from the
4892 * When this function returns false, the caller should send an OFPST_PORT_DESC
4893 * stats request to get the ports. */
4895 ofputil_switch_features_has_ports(struct ofpbuf
*b
)
4897 struct ofp_header
*oh
= b
->data
;
4898 size_t phy_port_size
;
4900 if (oh
->version
>= OFP13_VERSION
) {
4901 /* OpenFlow 1.3+ never has ports in the feature reply. */
4905 phy_port_size
= (oh
->version
== OFP10_VERSION
4906 ? sizeof(struct ofp10_phy_port
)
4907 : sizeof(struct ofp11_port
));
4908 if (ntohs(oh
->length
) + phy_port_size
<= UINT16_MAX
) {
4909 /* There's room for additional ports in the feature reply.
4910 * Assume that the list is complete. */
4914 /* The feature reply has no room for more ports. Probably the list is
4915 * truncated. Drop the ports and tell the caller to retrieve them with
4916 * OFPST_PORT_DESC. */
4917 b
->size
= sizeof *oh
+ sizeof(struct ofp_switch_features
);
4918 ofpmsg_update_length(b
);
4922 /* Returns a buffer owned by the caller that encodes 'features' in the format
4923 * required by 'protocol' with the given 'xid'. The caller should append port
4924 * information to the buffer with subsequent calls to
4925 * ofputil_put_switch_features_port(). */
4927 ofputil_encode_switch_features(const struct ofputil_switch_features
*features
,
4928 enum ofputil_protocol protocol
, ovs_be32 xid
)
4930 struct ofp_switch_features
*osf
;
4932 enum ofp_version version
;
4935 version
= ofputil_protocol_to_ofp_version(protocol
);
4938 raw
= OFPRAW_OFPT10_FEATURES_REPLY
;
4942 raw
= OFPRAW_OFPT11_FEATURES_REPLY
;
4948 raw
= OFPRAW_OFPT13_FEATURES_REPLY
;
4953 b
= ofpraw_alloc_xid(raw
, version
, xid
, 0);
4954 osf
= ofpbuf_put_zeros(b
, sizeof *osf
);
4955 osf
->datapath_id
= htonll(features
->datapath_id
);
4956 osf
->n_buffers
= htonl(features
->n_buffers
);
4957 osf
->n_tables
= features
->n_tables
;
4959 osf
->capabilities
= htonl(features
->capabilities
&
4960 ofputil_capabilities_mask(version
));
4963 if (features
->capabilities
& OFPUTIL_C_STP
) {
4964 osf
->capabilities
|= htonl(OFPC10_STP
);
4966 osf
->actions
= ofpact_bitmap_to_openflow(features
->ofpacts
,
4973 osf
->auxiliary_id
= features
->auxiliary_id
;
4977 if (features
->capabilities
& OFPUTIL_C_GROUP_STATS
) {
4978 osf
->capabilities
|= htonl(OFPC11_GROUP_STATS
);
4988 /* Encodes 'pp' into the format required by the switch_features message already
4989 * in 'b', which should have been returned by ofputil_encode_switch_features(),
4990 * and appends the encoded version to 'b'. */
4992 ofputil_put_switch_features_port(const struct ofputil_phy_port
*pp
,
4995 const struct ofp_header
*oh
= b
->data
;
4997 if (oh
->version
< OFP13_VERSION
) {
4998 /* Try adding a port description to the message, but drop it again if
4999 * the buffer overflows. (This possibility for overflow is why
5000 * OpenFlow 1.3+ moved port descriptions into a multipart message.) */
5001 size_t start_ofs
= b
->size
;
5002 ofputil_put_phy_port(oh
->version
, pp
, b
);
5003 if (b
->size
> UINT16_MAX
) {
5004 b
->size
= start_ofs
;
5009 /* ofputil_port_status */
5011 /* Decodes the OpenFlow "port status" message in '*ops' into an abstract form
5012 * in '*ps'. Returns 0 if successful, otherwise an OFPERR_* value. */
5014 ofputil_decode_port_status(const struct ofp_header
*oh
,
5015 struct ofputil_port_status
*ps
)
5017 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
5018 ofpraw_pull_assert(&b
);
5020 const struct ofp_port_status
*ops
= ofpbuf_pull(&b
, sizeof *ops
);
5021 if (ops
->reason
!= OFPPR_ADD
&&
5022 ops
->reason
!= OFPPR_DELETE
&&
5023 ops
->reason
!= OFPPR_MODIFY
) {
5024 return OFPERR_NXBRC_BAD_REASON
;
5026 ps
->reason
= ops
->reason
;
5028 int retval
= ofputil_pull_phy_port(oh
->version
, &b
, &ps
->desc
);
5029 ovs_assert(retval
!= EOF
);
5033 /* Converts the abstract form of a "port status" message in '*ps' into an
5034 * OpenFlow message suitable for 'protocol', and returns that encoded form in
5035 * a buffer owned by the caller. */
5037 ofputil_encode_port_status(const struct ofputil_port_status
*ps
,
5038 enum ofputil_protocol protocol
)
5040 struct ofp_port_status
*ops
;
5042 enum ofp_version version
;
5045 version
= ofputil_protocol_to_ofp_version(protocol
);
5048 raw
= OFPRAW_OFPT10_PORT_STATUS
;
5054 raw
= OFPRAW_OFPT11_PORT_STATUS
;
5059 raw
= OFPRAW_OFPT14_PORT_STATUS
;
5063 raw
= OFPRAW_OFPT16_PORT_STATUS
;
5070 b
= ofpraw_alloc_xid(raw
, version
, htonl(0), 0);
5071 ops
= ofpbuf_put_zeros(b
, sizeof *ops
);
5072 ops
->reason
= ps
->reason
;
5073 ofputil_put_phy_port(version
, &ps
->desc
, b
);
5074 ofpmsg_update_length(b
);
5078 /* ofputil_port_mod */
5081 parse_port_mod_ethernet_property(struct ofpbuf
*property
,
5082 struct ofputil_port_mod
*pm
)
5087 error
= ofpprop_parse_be32(property
, &advertise
);
5089 pm
->advertise
= netdev_port_features_from_ofp11(advertise
);
5095 ofputil_decode_ofp10_port_mod(const struct ofp10_port_mod
*opm
,
5096 struct ofputil_port_mod
*pm
)
5098 pm
->port_no
= u16_to_ofp(ntohs(opm
->port_no
));
5099 pm
->hw_addr
= opm
->hw_addr
;
5100 pm
->config
= ntohl(opm
->config
) & OFPPC10_ALL
;
5101 pm
->mask
= ntohl(opm
->mask
) & OFPPC10_ALL
;
5102 pm
->advertise
= netdev_port_features_from_ofp10(opm
->advertise
);
5107 ofputil_decode_ofp11_port_mod(const struct ofp11_port_mod
*opm
,
5108 struct ofputil_port_mod
*pm
)
5112 error
= ofputil_port_from_ofp11(opm
->port_no
, &pm
->port_no
);
5117 pm
->hw_addr
= opm
->hw_addr
;
5118 pm
->config
= ntohl(opm
->config
) & OFPPC11_ALL
;
5119 pm
->mask
= ntohl(opm
->mask
) & OFPPC11_ALL
;
5120 pm
->advertise
= netdev_port_features_from_ofp11(opm
->advertise
);
5126 ofputil_decode_ofp14_port_mod_properties(struct ofpbuf
*b
, bool loose
,
5127 struct ofputil_port_mod
*pm
)
5129 while (b
->size
> 0) {
5130 struct ofpbuf property
;
5134 error
= ofpprop_pull(b
, &property
, &type
);
5140 case OFPPMPT14_ETHERNET
:
5141 error
= parse_port_mod_ethernet_property(&property
, pm
);
5145 error
= OFPPROP_UNKNOWN(loose
, "port_mod", type
);
5157 ofputil_decode_ofp14_port_mod(struct ofpbuf
*b
, bool loose
,
5158 struct ofputil_port_mod
*pm
)
5160 const struct ofp14_port_mod
*opm
= ofpbuf_pull(b
, sizeof *opm
);
5161 enum ofperr error
= ofputil_port_from_ofp11(opm
->port_no
, &pm
->port_no
);
5166 pm
->hw_addr
= opm
->hw_addr
;
5167 pm
->config
= ntohl(opm
->config
) & OFPPC11_ALL
;
5168 pm
->mask
= ntohl(opm
->mask
) & OFPPC11_ALL
;
5170 return ofputil_decode_ofp14_port_mod_properties(b
, loose
, pm
);
5174 ofputil_decode_ofp16_port_mod(struct ofpbuf
*b
, bool loose
,
5175 struct ofputil_port_mod
*pm
)
5177 const struct ofp16_port_mod
*opm
= ofpbuf_pull(b
, sizeof *opm
);
5178 enum ofperr error
= ofputil_port_from_ofp11(opm
->port_no
, &pm
->port_no
);
5183 if (opm
->hw_addr_type
& htons(OFPPHAT16_EUI48
)) {
5184 pm
->hw_addr
= opm
->hw_addr
;
5186 if (opm
->hw_addr_type
& htons(OFPPHAT16_EUI64
)) {
5187 pm
->hw_addr64
= opm
->hw_addr64
;
5189 pm
->hw_addr
= opm
->hw_addr
;
5190 pm
->config
= ntohl(opm
->config
) & OFPPC11_ALL
;
5191 pm
->mask
= ntohl(opm
->mask
) & OFPPC11_ALL
;
5193 return ofputil_decode_ofp14_port_mod_properties(b
, loose
, pm
);
5196 /* Decodes the OpenFlow "port mod" message in '*oh' into an abstract form in
5197 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
5199 ofputil_decode_port_mod(const struct ofp_header
*oh
,
5200 struct ofputil_port_mod
*pm
, bool loose
)
5202 memset(pm
, 0, sizeof *pm
);
5204 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
5205 enum ofpraw raw
= ofpraw_pull_assert(&b
);
5208 if (raw
== OFPRAW_OFPT10_PORT_MOD
) {
5209 error
= ofputil_decode_ofp10_port_mod(b
.data
, pm
);
5210 } else if (raw
== OFPRAW_OFPT11_PORT_MOD
) {
5211 error
= ofputil_decode_ofp11_port_mod(b
.data
, pm
);
5212 } else if (raw
== OFPRAW_OFPT14_PORT_MOD
) {
5213 error
= ofputil_decode_ofp14_port_mod(&b
, loose
, pm
);
5214 } else if (raw
== OFPRAW_OFPT16_PORT_MOD
) {
5215 error
= ofputil_decode_ofp16_port_mod(&b
, loose
, pm
);
5217 error
= OFPERR_OFPBRC_BAD_TYPE
;
5220 pm
->config
&= pm
->mask
;
5224 /* Converts the abstract form of a "port mod" message in '*pm' into an OpenFlow
5225 * message suitable for 'protocol', and returns that encoded form in a buffer
5226 * owned by the caller. */
5228 ofputil_encode_port_mod(const struct ofputil_port_mod
*pm
,
5229 enum ofputil_protocol protocol
)
5231 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
5234 switch (ofp_version
) {
5235 case OFP10_VERSION
: {
5236 struct ofp10_port_mod
*opm
;
5238 b
= ofpraw_alloc(OFPRAW_OFPT10_PORT_MOD
, ofp_version
, 0);
5239 opm
= ofpbuf_put_zeros(b
, sizeof *opm
);
5240 opm
->port_no
= htons(ofp_to_u16(pm
->port_no
));
5241 opm
->hw_addr
= pm
->hw_addr
;
5242 opm
->config
= htonl(pm
->config
& OFPPC10_ALL
);
5243 opm
->mask
= htonl(pm
->mask
& OFPPC10_ALL
);
5244 opm
->advertise
= netdev_port_features_to_ofp10(pm
->advertise
);
5250 case OFP13_VERSION
: {
5251 struct ofp11_port_mod
*opm
;
5253 b
= ofpraw_alloc(OFPRAW_OFPT11_PORT_MOD
, ofp_version
, 0);
5254 opm
= ofpbuf_put_zeros(b
, sizeof *opm
);
5255 opm
->port_no
= ofputil_port_to_ofp11(pm
->port_no
);
5256 opm
->hw_addr
= pm
->hw_addr
;
5257 opm
->config
= htonl(pm
->config
& OFPPC11_ALL
);
5258 opm
->mask
= htonl(pm
->mask
& OFPPC11_ALL
);
5259 opm
->advertise
= netdev_port_features_to_ofp11(pm
->advertise
);
5263 case OFP15_VERSION
: {
5264 struct ofp14_port_mod
*opm
;
5266 b
= ofpraw_alloc(OFPRAW_OFPT14_PORT_MOD
, ofp_version
, 0);
5267 opm
= ofpbuf_put_zeros(b
, sizeof *opm
);
5268 opm
->port_no
= ofputil_port_to_ofp11(pm
->port_no
);
5269 opm
->hw_addr
= pm
->hw_addr
;
5270 opm
->config
= htonl(pm
->config
& OFPPC11_ALL
);
5271 opm
->mask
= htonl(pm
->mask
& OFPPC11_ALL
);
5273 if (pm
->advertise
) {
5274 ofpprop_put_be32(b
, OFPPMPT14_ETHERNET
,
5275 netdev_port_features_to_ofp11(pm
->advertise
));
5279 case OFP16_VERSION
: {
5280 struct ofp16_port_mod
*opm
;
5282 b
= ofpraw_alloc(OFPRAW_OFPT16_PORT_MOD
, ofp_version
, 0);
5283 opm
= ofpbuf_put_zeros(b
, sizeof *opm
);
5284 opm
->port_no
= ofputil_port_to_ofp11(pm
->port_no
);
5285 if (!eth_addr_is_zero(pm
->hw_addr
)) {
5286 opm
->hw_addr_type
|= htons(OFPPHAT16_EUI48
);
5287 opm
->hw_addr
= pm
->hw_addr
;
5289 if (!eth_addr64_is_zero(pm
->hw_addr64
)) {
5290 opm
->hw_addr_type
|= htons(OFPPHAT16_EUI64
);
5291 opm
->hw_addr64
= pm
->hw_addr64
;
5293 opm
->config
= htonl(pm
->config
& OFPPC11_ALL
);
5294 opm
->mask
= htonl(pm
->mask
& OFPPC11_ALL
);
5296 if (pm
->advertise
) {
5297 ofpprop_put_be32(b
, OFPPMPT14_ETHERNET
,
5298 netdev_port_features_to_ofp11(pm
->advertise
));
5309 /* Table features. */
5312 pull_table_feature_property(struct ofpbuf
*msg
, struct ofpbuf
*payload
,
5317 error
= ofpprop_pull(msg
, payload
, typep
);
5318 if (payload
&& !error
) {
5319 ofpbuf_pull(payload
, (char *)payload
->msg
- (char *)payload
->header
);
5325 parse_action_bitmap(struct ofpbuf
*payload
, enum ofp_version ofp_version
,
5330 while (payload
->size
> 0) {
5334 error
= ofpprop_pull__(payload
, NULL
, 1, 0x10000, &type
);
5338 if (type
< CHAR_BIT
* sizeof types
) {
5339 types
|= 1u << type
;
5343 *ofpacts
= ofpact_bitmap_from_openflow(htonl(types
), ofp_version
);
5348 parse_instruction_ids(struct ofpbuf
*payload
, bool loose
, uint32_t *insts
)
5351 while (payload
->size
> 0) {
5352 enum ovs_instruction_type inst
;
5356 /* OF1.3 and OF1.4 aren't clear about padding in the instruction IDs.
5357 * It seems clear that they aren't padded to 8 bytes, though, because
5358 * both standards say that "non-experimenter instructions are 4 bytes"
5359 * and do not mention any padding before the first instruction ID.
5360 * (There wouldn't be any point in padding to 8 bytes if the IDs were
5361 * aligned on an odd 4-byte boundary.)
5363 * Anyway, we just assume they're all glommed together on byte
5365 error
= ofpprop_pull__(payload
, NULL
, 1, 0x10000, &ofpit
);
5370 error
= ovs_instruction_type_from_inst_type(&inst
, ofpit
);
5372 *insts
|= 1u << inst
;
5373 } else if (!loose
) {
5381 parse_table_features_next_table(struct ofpbuf
*payload
,
5382 unsigned long int *next_tables
)
5386 memset(next_tables
, 0, bitmap_n_bytes(255));
5387 for (i
= 0; i
< payload
->size
; i
++) {
5388 uint8_t id
= ((const uint8_t *) payload
->data
)[i
];
5390 return OFPERR_OFPBPC_BAD_VALUE
;
5392 bitmap_set1(next_tables
, id
);
5398 parse_oxms(struct ofpbuf
*payload
, bool loose
,
5399 struct mf_bitmap
*exactp
, struct mf_bitmap
*maskedp
)
5401 struct mf_bitmap exact
= MF_BITMAP_INITIALIZER
;
5402 struct mf_bitmap masked
= MF_BITMAP_INITIALIZER
;
5404 while (payload
->size
> 0) {
5405 const struct mf_field
*field
;
5409 error
= nx_pull_header(payload
, NULL
, &field
, &hasmask
);
5411 bitmap_set1(hasmask
? masked
.bm
: exact
.bm
, field
->id
);
5412 } else if (error
!= OFPERR_OFPBMC_BAD_FIELD
|| !loose
) {
5418 } else if (!bitmap_is_all_zeros(exact
.bm
, MFF_N_IDS
)) {
5419 return OFPERR_OFPBMC_BAD_MASK
;
5423 } else if (!bitmap_is_all_zeros(masked
.bm
, MFF_N_IDS
)) {
5424 return OFPERR_OFPBMC_BAD_MASK
;
5429 /* Converts an OFPMP_TABLE_FEATURES request or reply in 'msg' into an abstract
5430 * ofputil_table_features in 'tf'.
5432 * If 'loose' is true, this function ignores properties and values that it does
5433 * not understand, as a controller would want to do when interpreting
5434 * capabilities provided by a switch. If 'loose' is false, this function
5435 * treats unknown properties and values as an error, as a switch would want to
5436 * do when interpreting a configuration request made by a controller.
5438 * A single OpenFlow message can specify features for multiple tables. Calling
5439 * this function multiple times for a single 'msg' iterates through the tables
5440 * in the message. The caller must initially leave 'msg''s layer pointers null
5441 * and not modify them between calls.
5443 * Returns 0 if successful, EOF if no tables were left in this 'msg', otherwise
5444 * a positive "enum ofperr" value. */
5446 ofputil_decode_table_features(struct ofpbuf
*msg
,
5447 struct ofputil_table_features
*tf
, bool loose
)
5449 memset(tf
, 0, sizeof *tf
);
5452 ofpraw_pull_assert(msg
);
5459 const struct ofp_header
*oh
= msg
->header
;
5460 struct ofp13_table_features
*otf
= msg
->data
;
5461 if (msg
->size
< sizeof *otf
) {
5462 return OFPERR_OFPBPC_BAD_LEN
;
5465 unsigned int len
= ntohs(otf
->length
);
5466 if (len
< sizeof *otf
|| len
% 8 || len
> msg
->size
) {
5467 return OFPERR_OFPBPC_BAD_LEN
;
5470 tf
->table_id
= otf
->table_id
;
5471 if (tf
->table_id
== OFPTT_ALL
) {
5472 return OFPERR_OFPTFFC_BAD_TABLE
;
5475 ovs_strlcpy_arrays(tf
->name
, otf
->name
);
5476 tf
->metadata_match
= otf
->metadata_match
;
5477 tf
->metadata_write
= otf
->metadata_write
;
5478 tf
->miss_config
= OFPUTIL_TABLE_MISS_DEFAULT
;
5479 if (oh
->version
>= OFP14_VERSION
) {
5480 uint32_t caps
= ntohl(otf
->capabilities
);
5481 tf
->supports_eviction
= (caps
& OFPTC14_EVICTION
) != 0;
5482 tf
->supports_vacancy_events
= (caps
& OFPTC14_VACANCY_EVENTS
) != 0;
5484 tf
->supports_eviction
= -1;
5485 tf
->supports_vacancy_events
= -1;
5487 tf
->max_entries
= ntohl(otf
->max_entries
);
5489 struct ofpbuf properties
= ofpbuf_const_initializer(ofpbuf_pull(msg
, len
),
5491 ofpbuf_pull(&properties
, sizeof *otf
);
5492 while (properties
.size
> 0) {
5493 struct ofpbuf payload
;
5497 error
= pull_table_feature_property(&properties
, &payload
, &type
);
5502 switch ((enum ofp13_table_feature_prop_type
) type
) {
5503 case OFPTFPT13_INSTRUCTIONS
:
5504 error
= parse_instruction_ids(&payload
, loose
,
5505 &tf
->nonmiss
.instructions
);
5507 case OFPTFPT13_INSTRUCTIONS_MISS
:
5508 error
= parse_instruction_ids(&payload
, loose
,
5509 &tf
->miss
.instructions
);
5512 case OFPTFPT13_NEXT_TABLES
:
5513 error
= parse_table_features_next_table(&payload
,
5516 case OFPTFPT13_NEXT_TABLES_MISS
:
5517 error
= parse_table_features_next_table(&payload
, tf
->miss
.next
);
5520 case OFPTFPT13_WRITE_ACTIONS
:
5521 error
= parse_action_bitmap(&payload
, oh
->version
,
5522 &tf
->nonmiss
.write
.ofpacts
);
5524 case OFPTFPT13_WRITE_ACTIONS_MISS
:
5525 error
= parse_action_bitmap(&payload
, oh
->version
,
5526 &tf
->miss
.write
.ofpacts
);
5529 case OFPTFPT13_APPLY_ACTIONS
:
5530 error
= parse_action_bitmap(&payload
, oh
->version
,
5531 &tf
->nonmiss
.apply
.ofpacts
);
5533 case OFPTFPT13_APPLY_ACTIONS_MISS
:
5534 error
= parse_action_bitmap(&payload
, oh
->version
,
5535 &tf
->miss
.apply
.ofpacts
);
5538 case OFPTFPT13_MATCH
:
5539 error
= parse_oxms(&payload
, loose
, &tf
->match
, &tf
->mask
);
5541 case OFPTFPT13_WILDCARDS
:
5542 error
= parse_oxms(&payload
, loose
, &tf
->wildcard
, NULL
);
5545 case OFPTFPT13_WRITE_SETFIELD
:
5546 error
= parse_oxms(&payload
, loose
,
5547 &tf
->nonmiss
.write
.set_fields
, NULL
);
5549 case OFPTFPT13_WRITE_SETFIELD_MISS
:
5550 error
= parse_oxms(&payload
, loose
,
5551 &tf
->miss
.write
.set_fields
, NULL
);
5553 case OFPTFPT13_APPLY_SETFIELD
:
5554 error
= parse_oxms(&payload
, loose
,
5555 &tf
->nonmiss
.apply
.set_fields
, NULL
);
5557 case OFPTFPT13_APPLY_SETFIELD_MISS
:
5558 error
= parse_oxms(&payload
, loose
,
5559 &tf
->miss
.apply
.set_fields
, NULL
);
5562 case OFPTFPT13_EXPERIMENTER
:
5563 case OFPTFPT13_EXPERIMENTER_MISS
:
5565 error
= OFPPROP_UNKNOWN(loose
, "table features", type
);
5573 /* Fix inconsistencies:
5575 * - Turn on 'match' bits that are set in 'mask', because maskable
5576 * fields are matchable.
5578 * - Turn on 'wildcard' bits that are set in 'mask', because a field
5579 * that is arbitrarily maskable can be wildcarded entirely.
5581 * - Turn off 'wildcard' bits that are not in 'match', because a field
5582 * must be matchable for it to be meaningfully wildcarded. */
5583 bitmap_or(tf
->match
.bm
, tf
->mask
.bm
, MFF_N_IDS
);
5584 bitmap_or(tf
->wildcard
.bm
, tf
->mask
.bm
, MFF_N_IDS
);
5585 bitmap_and(tf
->wildcard
.bm
, tf
->match
.bm
, MFF_N_IDS
);
5590 /* Encodes and returns a request to obtain the table features of a switch.
5591 * The message is encoded for OpenFlow version 'ofp_version'. */
5593 ofputil_encode_table_features_request(enum ofp_version ofp_version
)
5595 struct ofpbuf
*request
= NULL
;
5597 switch (ofp_version
) {
5601 ovs_fatal(0, "dump-table-features needs OpenFlow 1.3 or later "
5602 "(\'-O OpenFlow13\')");
5607 request
= ofpraw_alloc(OFPRAW_OFPST13_TABLE_FEATURES_REQUEST
,
5618 put_fields_property(struct ofpbuf
*reply
,
5619 const struct mf_bitmap
*fields
,
5620 const struct mf_bitmap
*masks
,
5621 enum ofp13_table_feature_prop_type property
,
5622 enum ofp_version version
)
5627 start_ofs
= ofpprop_start(reply
, property
);
5628 BITMAP_FOR_EACH_1 (field
, MFF_N_IDS
, fields
->bm
) {
5629 nx_put_header(reply
, field
, version
,
5630 masks
&& bitmap_is_set(masks
->bm
, field
));
5632 ofpprop_end(reply
, start_ofs
);
5636 put_table_action_features(struct ofpbuf
*reply
,
5637 const struct ofputil_table_action_features
*taf
,
5638 enum ofp13_table_feature_prop_type actions_type
,
5639 enum ofp13_table_feature_prop_type set_fields_type
,
5640 int miss_offset
, enum ofp_version version
)
5642 ofpprop_put_bitmap(reply
, actions_type
+ miss_offset
,
5643 ntohl(ofpact_bitmap_to_openflow(taf
->ofpacts
,
5645 put_fields_property(reply
, &taf
->set_fields
, NULL
,
5646 set_fields_type
+ miss_offset
, version
);
5650 put_table_instruction_features(
5651 struct ofpbuf
*reply
, const struct ofputil_table_instruction_features
*tif
,
5652 int miss_offset
, enum ofp_version version
)
5657 ofpprop_put_bitmap(reply
, OFPTFPT13_INSTRUCTIONS
+ miss_offset
,
5658 ntohl(ovsinst_bitmap_to_openflow(tif
->instructions
,
5661 start_ofs
= ofpprop_start(reply
, OFPTFPT13_NEXT_TABLES
+ miss_offset
);
5662 BITMAP_FOR_EACH_1 (table_id
, 255, tif
->next
) {
5663 ofpbuf_put(reply
, &table_id
, 1);
5665 ofpprop_end(reply
, start_ofs
);
5667 put_table_action_features(reply
, &tif
->write
,
5668 OFPTFPT13_WRITE_ACTIONS
,
5669 OFPTFPT13_WRITE_SETFIELD
, miss_offset
, version
);
5670 put_table_action_features(reply
, &tif
->apply
,
5671 OFPTFPT13_APPLY_ACTIONS
,
5672 OFPTFPT13_APPLY_SETFIELD
, miss_offset
, version
);
5676 ofputil_append_table_features_reply(const struct ofputil_table_features
*tf
,
5677 struct ovs_list
*replies
)
5679 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
5680 enum ofp_version version
= ofpmp_version(replies
);
5681 size_t start_ofs
= reply
->size
;
5682 struct ofp13_table_features
*otf
;
5684 otf
= ofpbuf_put_zeros(reply
, sizeof *otf
);
5685 otf
->table_id
= tf
->table_id
;
5686 ovs_strlcpy_arrays(otf
->name
, tf
->name
);
5687 otf
->metadata_match
= tf
->metadata_match
;
5688 otf
->metadata_write
= tf
->metadata_write
;
5689 if (version
>= OFP14_VERSION
) {
5690 if (tf
->supports_eviction
) {
5691 otf
->capabilities
|= htonl(OFPTC14_EVICTION
);
5693 if (tf
->supports_vacancy_events
) {
5694 otf
->capabilities
|= htonl(OFPTC14_VACANCY_EVENTS
);
5697 otf
->max_entries
= htonl(tf
->max_entries
);
5699 put_table_instruction_features(reply
, &tf
->nonmiss
, 0, version
);
5700 put_table_instruction_features(reply
, &tf
->miss
, 1, version
);
5702 put_fields_property(reply
, &tf
->match
, &tf
->mask
,
5703 OFPTFPT13_MATCH
, version
);
5704 put_fields_property(reply
, &tf
->wildcard
, NULL
,
5705 OFPTFPT13_WILDCARDS
, version
);
5707 otf
= ofpbuf_at_assert(reply
, start_ofs
, sizeof *otf
);
5708 otf
->length
= htons(reply
->size
- start_ofs
);
5709 ofpmp_postappend(replies
, start_ofs
);
5713 parse_table_desc_vacancy_property(struct ofpbuf
*property
,
5714 struct ofputil_table_desc
*td
)
5716 struct ofp14_table_mod_prop_vacancy
*otv
= property
->data
;
5718 if (property
->size
!= sizeof *otv
) {
5719 return OFPERR_OFPBPC_BAD_LEN
;
5722 td
->table_vacancy
.vacancy_down
= otv
->vacancy_down
;
5723 td
->table_vacancy
.vacancy_up
= otv
->vacancy_up
;
5724 td
->table_vacancy
.vacancy
= otv
->vacancy
;
5728 /* Decodes the next OpenFlow "table desc" message (of possibly several) from
5729 * 'msg' into an abstract form in '*td'. Returns 0 if successful, EOF if the
5730 * last "table desc" in 'msg' was already decoded, otherwise an OFPERR_*
5733 ofputil_decode_table_desc(struct ofpbuf
*msg
,
5734 struct ofputil_table_desc
*td
,
5735 enum ofp_version version
)
5737 memset(td
, 0, sizeof *td
);
5740 ofpraw_pull_assert(msg
);
5747 struct ofp14_table_desc
*otd
= ofpbuf_try_pull(msg
, sizeof *otd
);
5749 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFP14_TABLE_DESC reply has %"PRIu32
" "
5750 "leftover bytes at end", msg
->size
);
5751 return OFPERR_OFPBRC_BAD_LEN
;
5754 td
->table_id
= otd
->table_id
;
5755 size_t length
= ntohs(otd
->length
);
5756 if (length
< sizeof *otd
|| length
- sizeof *otd
> msg
->size
) {
5757 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFP14_TABLE_DESC reply claims invalid "
5758 "length %"PRIuSIZE
, length
);
5759 return OFPERR_OFPBRC_BAD_LEN
;
5761 length
-= sizeof *otd
;
5763 td
->eviction
= ofputil_decode_table_eviction(otd
->config
, version
);
5764 td
->vacancy
= ofputil_decode_table_vacancy(otd
->config
, version
);
5765 td
->eviction_flags
= UINT32_MAX
;
5767 struct ofpbuf properties
= ofpbuf_const_initializer(
5768 ofpbuf_pull(msg
, length
), length
);
5769 while (properties
.size
> 0) {
5770 struct ofpbuf payload
;
5774 error
= ofpprop_pull(&properties
, &payload
, &type
);
5780 case OFPTMPT14_EVICTION
:
5781 error
= ofpprop_parse_u32(&payload
, &td
->eviction_flags
);
5784 case OFPTMPT14_VACANCY
:
5785 error
= parse_table_desc_vacancy_property(&payload
, td
);
5789 error
= OFPPROP_UNKNOWN(true, "table_desc", type
);
5801 /* Encodes and returns a request to obtain description of tables of a switch.
5802 * The message is encoded for OpenFlow version 'ofp_version'. */
5804 ofputil_encode_table_desc_request(enum ofp_version ofp_version
)
5806 struct ofpbuf
*request
= NULL
;
5808 if (ofp_version
>= OFP14_VERSION
) {
5809 request
= ofpraw_alloc(OFPRAW_OFPST14_TABLE_DESC_REQUEST
,
5812 ovs_fatal(0, "dump-table-desc needs OpenFlow 1.4 or later "
5813 "(\'-O OpenFlow14\')");
5819 /* Function to append Table desc information in a reply list. */
5821 ofputil_append_table_desc_reply(const struct ofputil_table_desc
*td
,
5822 struct ovs_list
*replies
,
5823 enum ofp_version version
)
5825 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
5827 struct ofp14_table_desc
*otd
;
5829 start_otd
= reply
->size
;
5830 ofpbuf_put_zeros(reply
, sizeof *otd
);
5831 if (td
->eviction_flags
!= UINT32_MAX
) {
5832 ofpprop_put_u32(reply
, OFPTMPT14_EVICTION
, td
->eviction_flags
);
5834 if (td
->vacancy
== OFPUTIL_TABLE_VACANCY_ON
) {
5835 struct ofp14_table_mod_prop_vacancy
*otv
;
5837 otv
= ofpprop_put_zeros(reply
, OFPTMPT14_VACANCY
, sizeof *otv
);
5838 otv
->vacancy_down
= td
->table_vacancy
.vacancy_down
;
5839 otv
->vacancy_up
= td
->table_vacancy
.vacancy_up
;
5840 otv
->vacancy
= td
->table_vacancy
.vacancy
;
5843 otd
= ofpbuf_at_assert(reply
, start_otd
, sizeof *otd
);
5844 otd
->length
= htons(reply
->size
- start_otd
);
5845 otd
->table_id
= td
->table_id
;
5846 otd
->config
= ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT
,
5847 td
->eviction
, td
->vacancy
,
5849 ofpmp_postappend(replies
, start_otd
);
5852 /* This function parses Vacancy property, and decodes the
5853 * ofp14_table_mod_prop_vacancy in ofputil_table_mod.
5854 * Returns OFPERR_OFPBPC_BAD_VALUE error code when vacancy_down is
5855 * greater than vacancy_up and also when current vacancy has non-zero
5856 * value. Returns 0 on success. */
5858 parse_table_mod_vacancy_property(struct ofpbuf
*property
,
5859 struct ofputil_table_mod
*tm
)
5861 struct ofp14_table_mod_prop_vacancy
*otv
= property
->data
;
5863 if (property
->size
!= sizeof *otv
) {
5864 return OFPERR_OFPBPC_BAD_LEN
;
5866 tm
->table_vacancy
.vacancy_down
= otv
->vacancy_down
;
5867 tm
->table_vacancy
.vacancy_up
= otv
->vacancy_up
;
5868 if (tm
->table_vacancy
.vacancy_down
> tm
->table_vacancy
.vacancy_up
) {
5869 OFPPROP_LOG(&bad_ofmsg_rl
, false,
5870 "Value of vacancy_down is greater than vacancy_up");
5871 return OFPERR_OFPBPC_BAD_VALUE
;
5873 if (tm
->table_vacancy
.vacancy_down
> 100 ||
5874 tm
->table_vacancy
.vacancy_up
> 100) {
5875 OFPPROP_LOG(&bad_ofmsg_rl
, false, "Vacancy threshold percentage "
5876 "should not be greater than 100");
5877 return OFPERR_OFPBPC_BAD_VALUE
;
5879 tm
->table_vacancy
.vacancy
= otv
->vacancy
;
5880 if (tm
->table_vacancy
.vacancy
) {
5881 OFPPROP_LOG(&bad_ofmsg_rl
, false,
5882 "Vacancy value should be zero for table-mod messages");
5883 return OFPERR_OFPBPC_BAD_VALUE
;
5888 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5889 * table configuration (a table mod, table stats, or table features message),
5890 * returns the table vacancy configuration that it specifies.
5892 * Only OpenFlow 1.4 and later specify table vacancy configuration this way,
5893 * so for other 'version' this function always returns
5894 * OFPUTIL_TABLE_VACANCY_DEFAULT. */
5895 static enum ofputil_table_vacancy
5896 ofputil_decode_table_vacancy(ovs_be32 config
, enum ofp_version version
)
5898 return (version
< OFP14_VERSION
? OFPUTIL_TABLE_VACANCY_DEFAULT
5899 : config
& htonl(OFPTC14_VACANCY_EVENTS
) ? OFPUTIL_TABLE_VACANCY_ON
5900 : OFPUTIL_TABLE_VACANCY_OFF
);
5903 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5904 * table configuration (a table mod, table stats, or table features message),
5905 * returns the table eviction configuration that it specifies.
5907 * Only OpenFlow 1.4 and later specify table eviction configuration this way,
5908 * so for other 'version' values this function always returns
5909 * OFPUTIL_TABLE_EVICTION_DEFAULT. */
5910 static enum ofputil_table_eviction
5911 ofputil_decode_table_eviction(ovs_be32 config
, enum ofp_version version
)
5913 return (version
< OFP14_VERSION
? OFPUTIL_TABLE_EVICTION_DEFAULT
5914 : config
& htonl(OFPTC14_EVICTION
) ? OFPUTIL_TABLE_EVICTION_ON
5915 : OFPUTIL_TABLE_EVICTION_OFF
);
5918 /* Returns a bitmap of OFPTC* values suitable for 'config' fields in various
5919 * OpenFlow messages of the given 'version', based on the provided 'miss' and
5920 * 'eviction' values. */
5922 ofputil_encode_table_config(enum ofputil_table_miss miss
,
5923 enum ofputil_table_eviction eviction
,
5924 enum ofputil_table_vacancy vacancy
,
5925 enum ofp_version version
)
5927 uint32_t config
= 0;
5928 /* Search for "OFPTC_* Table Configuration" in the documentation for more
5929 * information on the crazy evolution of this field. */
5932 /* OpenFlow 1.0 didn't have such a field, any value ought to do. */
5937 /* OpenFlow 1.1 and 1.2 define only OFPTC11_TABLE_MISS_*. */
5939 case OFPUTIL_TABLE_MISS_DEFAULT
:
5940 /* Really this shouldn't be used for encoding (the caller should
5941 * provide a specific value) but I can't imagine that defaulting to
5942 * the fall-through case here will hurt. */
5943 case OFPUTIL_TABLE_MISS_CONTROLLER
:
5945 return htonl(OFPTC11_TABLE_MISS_CONTROLLER
);
5946 case OFPUTIL_TABLE_MISS_CONTINUE
:
5947 return htonl(OFPTC11_TABLE_MISS_CONTINUE
);
5948 case OFPUTIL_TABLE_MISS_DROP
:
5949 return htonl(OFPTC11_TABLE_MISS_DROP
);
5954 /* OpenFlow 1.3 removed OFPTC11_TABLE_MISS_* and didn't define any new
5955 * flags, so this is correct. */
5961 /* OpenFlow 1.4 introduced OFPTC14_EVICTION and
5962 * OFPTC14_VACANCY_EVENTS. */
5963 if (eviction
== OFPUTIL_TABLE_EVICTION_ON
) {
5964 config
|= OFPTC14_EVICTION
;
5966 if (vacancy
== OFPUTIL_TABLE_VACANCY_ON
) {
5967 config
|= OFPTC14_VACANCY_EVENTS
;
5969 return htonl(config
);
5975 /* Given 'config', taken from an OpenFlow 'version' message that specifies
5976 * table configuration (a table mod, table stats, or table features message),
5977 * returns the table miss configuration that it specifies.
5979 * Only OpenFlow 1.1 and 1.2 specify table miss configurations this way, so for
5980 * other 'version' values this function always returns
5981 * OFPUTIL_TABLE_MISS_DEFAULT. */
5982 static enum ofputil_table_miss
5983 ofputil_decode_table_miss(ovs_be32 config_
, enum ofp_version version
)
5985 uint32_t config
= ntohl(config_
);
5987 if (version
== OFP11_VERSION
|| version
== OFP12_VERSION
) {
5988 switch (config
& OFPTC11_TABLE_MISS_MASK
) {
5989 case OFPTC11_TABLE_MISS_CONTROLLER
:
5990 return OFPUTIL_TABLE_MISS_CONTROLLER
;
5992 case OFPTC11_TABLE_MISS_CONTINUE
:
5993 return OFPUTIL_TABLE_MISS_CONTINUE
;
5995 case OFPTC11_TABLE_MISS_DROP
:
5996 return OFPUTIL_TABLE_MISS_DROP
;
5999 VLOG_WARN_RL(&bad_ofmsg_rl
, "bad table miss config %d", config
);
6000 return OFPUTIL_TABLE_MISS_CONTROLLER
;
6003 return OFPUTIL_TABLE_MISS_DEFAULT
;
6007 /* Decodes the OpenFlow "table mod" message in '*oh' into an abstract form in
6008 * '*pm'. Returns 0 if successful, otherwise an OFPERR_* value. */
6010 ofputil_decode_table_mod(const struct ofp_header
*oh
,
6011 struct ofputil_table_mod
*pm
)
6013 memset(pm
, 0, sizeof *pm
);
6014 pm
->miss
= OFPUTIL_TABLE_MISS_DEFAULT
;
6015 pm
->eviction
= OFPUTIL_TABLE_EVICTION_DEFAULT
;
6016 pm
->eviction_flags
= UINT32_MAX
;
6017 pm
->vacancy
= OFPUTIL_TABLE_VACANCY_DEFAULT
;
6019 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
6020 enum ofpraw raw
= ofpraw_pull_assert(&b
);
6021 if (raw
== OFPRAW_OFPT11_TABLE_MOD
) {
6022 const struct ofp11_table_mod
*otm
= b
.data
;
6024 pm
->table_id
= otm
->table_id
;
6025 pm
->miss
= ofputil_decode_table_miss(otm
->config
, oh
->version
);
6026 } else if (raw
== OFPRAW_OFPT14_TABLE_MOD
) {
6027 const struct ofp14_table_mod
*otm
= ofpbuf_pull(&b
, sizeof *otm
);
6029 pm
->table_id
= otm
->table_id
;
6030 pm
->miss
= ofputil_decode_table_miss(otm
->config
, oh
->version
);
6031 pm
->eviction
= ofputil_decode_table_eviction(otm
->config
, oh
->version
);
6032 pm
->vacancy
= ofputil_decode_table_vacancy(otm
->config
, oh
->version
);
6033 while (b
.size
> 0) {
6034 struct ofpbuf property
;
6038 error
= ofpprop_pull(&b
, &property
, &type
);
6044 case OFPTMPT14_EVICTION
:
6045 error
= ofpprop_parse_u32(&property
, &pm
->eviction
);
6048 case OFPTMPT14_VACANCY
:
6049 error
= parse_table_mod_vacancy_property(&property
, pm
);
6053 error
= OFPERR_OFPBRC_BAD_TYPE
;
6062 return OFPERR_OFPBRC_BAD_TYPE
;
6068 /* Converts the abstract form of a "table mod" message in '*tm' into an
6069 * OpenFlow message suitable for 'protocol', and returns that encoded form in a
6070 * buffer owned by the caller. */
6072 ofputil_encode_table_mod(const struct ofputil_table_mod
*tm
,
6073 enum ofputil_protocol protocol
)
6075 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
6078 switch (ofp_version
) {
6079 case OFP10_VERSION
: {
6080 ovs_fatal(0, "table mod needs OpenFlow 1.1 or later "
6081 "(\'-O OpenFlow11\')");
6086 case OFP13_VERSION
: {
6087 struct ofp11_table_mod
*otm
;
6089 b
= ofpraw_alloc(OFPRAW_OFPT11_TABLE_MOD
, ofp_version
, 0);
6090 otm
= ofpbuf_put_zeros(b
, sizeof *otm
);
6091 otm
->table_id
= tm
->table_id
;
6092 otm
->config
= ofputil_encode_table_config(tm
->miss
, tm
->eviction
,
6093 tm
->vacancy
, ofp_version
);
6098 case OFP16_VERSION
: {
6099 struct ofp14_table_mod
*otm
;
6101 b
= ofpraw_alloc(OFPRAW_OFPT14_TABLE_MOD
, ofp_version
, 0);
6102 otm
= ofpbuf_put_zeros(b
, sizeof *otm
);
6103 otm
->table_id
= tm
->table_id
;
6104 otm
->config
= ofputil_encode_table_config(tm
->miss
, tm
->eviction
,
6105 tm
->vacancy
, ofp_version
);
6107 if (tm
->eviction_flags
!= UINT32_MAX
) {
6108 ofpprop_put_u32(b
, OFPTMPT14_EVICTION
, tm
->eviction_flags
);
6110 if (tm
->vacancy
== OFPUTIL_TABLE_VACANCY_ON
) {
6111 struct ofp14_table_mod_prop_vacancy
*otv
;
6113 otv
= ofpprop_put_zeros(b
, OFPTMPT14_VACANCY
, sizeof *otv
);
6114 otv
->vacancy_down
= tm
->table_vacancy
.vacancy_down
;
6115 otv
->vacancy_up
= tm
->table_vacancy
.vacancy_up
;
6126 /* ofputil_role_request */
6128 /* Decodes the OpenFlow "role request" or "role reply" message in '*oh' into
6129 * an abstract form in '*rr'. Returns 0 if successful, otherwise an
6130 * OFPERR_* value. */
6132 ofputil_decode_role_message(const struct ofp_header
*oh
,
6133 struct ofputil_role_request
*rr
)
6135 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
6136 enum ofpraw raw
= ofpraw_pull_assert(&b
);
6137 if (raw
== OFPRAW_OFPT12_ROLE_REQUEST
||
6138 raw
== OFPRAW_OFPT12_ROLE_REPLY
) {
6139 const struct ofp12_role_request
*orr
= b
.msg
;
6141 if (orr
->role
!= htonl(OFPCR12_ROLE_NOCHANGE
) &&
6142 orr
->role
!= htonl(OFPCR12_ROLE_EQUAL
) &&
6143 orr
->role
!= htonl(OFPCR12_ROLE_MASTER
) &&
6144 orr
->role
!= htonl(OFPCR12_ROLE_SLAVE
)) {
6145 return OFPERR_OFPRRFC_BAD_ROLE
;
6148 rr
->role
= ntohl(orr
->role
);
6149 if (raw
== OFPRAW_OFPT12_ROLE_REQUEST
6150 ? orr
->role
== htonl(OFPCR12_ROLE_NOCHANGE
)
6151 : orr
->generation_id
== OVS_BE64_MAX
) {
6152 rr
->have_generation_id
= false;
6153 rr
->generation_id
= 0;
6155 rr
->have_generation_id
= true;
6156 rr
->generation_id
= ntohll(orr
->generation_id
);
6158 } else if (raw
== OFPRAW_NXT_ROLE_REQUEST
||
6159 raw
== OFPRAW_NXT_ROLE_REPLY
) {
6160 const struct nx_role_request
*nrr
= b
.msg
;
6162 BUILD_ASSERT(NX_ROLE_OTHER
+ 1 == OFPCR12_ROLE_EQUAL
);
6163 BUILD_ASSERT(NX_ROLE_MASTER
+ 1 == OFPCR12_ROLE_MASTER
);
6164 BUILD_ASSERT(NX_ROLE_SLAVE
+ 1 == OFPCR12_ROLE_SLAVE
);
6166 if (nrr
->role
!= htonl(NX_ROLE_OTHER
) &&
6167 nrr
->role
!= htonl(NX_ROLE_MASTER
) &&
6168 nrr
->role
!= htonl(NX_ROLE_SLAVE
)) {
6169 return OFPERR_OFPRRFC_BAD_ROLE
;
6172 rr
->role
= ntohl(nrr
->role
) + 1;
6173 rr
->have_generation_id
= false;
6174 rr
->generation_id
= 0;
6182 /* Returns an encoded form of a role reply suitable for the "request" in a
6183 * buffer owned by the caller. */
6185 ofputil_encode_role_reply(const struct ofp_header
*request
,
6186 const struct ofputil_role_request
*rr
)
6191 raw
= ofpraw_decode_assert(request
);
6192 if (raw
== OFPRAW_OFPT12_ROLE_REQUEST
) {
6193 struct ofp12_role_request
*orr
;
6195 buf
= ofpraw_alloc_reply(OFPRAW_OFPT12_ROLE_REPLY
, request
, 0);
6196 orr
= ofpbuf_put_zeros(buf
, sizeof *orr
);
6198 orr
->role
= htonl(rr
->role
);
6199 orr
->generation_id
= htonll(rr
->have_generation_id
6202 } else if (raw
== OFPRAW_NXT_ROLE_REQUEST
) {
6203 struct nx_role_request
*nrr
;
6205 BUILD_ASSERT(NX_ROLE_OTHER
== OFPCR12_ROLE_EQUAL
- 1);
6206 BUILD_ASSERT(NX_ROLE_MASTER
== OFPCR12_ROLE_MASTER
- 1);
6207 BUILD_ASSERT(NX_ROLE_SLAVE
== OFPCR12_ROLE_SLAVE
- 1);
6209 buf
= ofpraw_alloc_reply(OFPRAW_NXT_ROLE_REPLY
, request
, 0);
6210 nrr
= ofpbuf_put_zeros(buf
, sizeof *nrr
);
6211 nrr
->role
= htonl(rr
->role
- 1);
6219 /* Encodes "role status" message 'status' for sending in the given
6220 * 'protocol'. Returns the role status message, if 'protocol' supports them,
6221 * otherwise a null pointer. */
6223 ofputil_encode_role_status(const struct ofputil_role_status
*status
,
6224 enum ofputil_protocol protocol
)
6226 enum ofp_version version
;
6228 version
= ofputil_protocol_to_ofp_version(protocol
);
6229 if (version
>= OFP14_VERSION
) {
6230 struct ofp14_role_status
*rstatus
;
6233 buf
= ofpraw_alloc_xid(OFPRAW_OFPT14_ROLE_STATUS
, version
, htonl(0),
6235 rstatus
= ofpbuf_put_zeros(buf
, sizeof *rstatus
);
6236 rstatus
->role
= htonl(status
->role
);
6237 rstatus
->reason
= status
->reason
;
6238 rstatus
->generation_id
= htonll(status
->generation_id
);
6247 ofputil_decode_role_status(const struct ofp_header
*oh
,
6248 struct ofputil_role_status
*rs
)
6250 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
6251 enum ofpraw raw
= ofpraw_pull_assert(&b
);
6252 ovs_assert(raw
== OFPRAW_OFPT14_ROLE_STATUS
);
6254 const struct ofp14_role_status
*r
= b
.msg
;
6255 if (r
->role
!= htonl(OFPCR12_ROLE_NOCHANGE
) &&
6256 r
->role
!= htonl(OFPCR12_ROLE_EQUAL
) &&
6257 r
->role
!= htonl(OFPCR12_ROLE_MASTER
) &&
6258 r
->role
!= htonl(OFPCR12_ROLE_SLAVE
)) {
6259 return OFPERR_OFPRRFC_BAD_ROLE
;
6262 rs
->role
= ntohl(r
->role
);
6263 rs
->generation_id
= ntohll(r
->generation_id
);
6264 rs
->reason
= r
->reason
;
6269 /* Encodes 'rf' according to 'protocol', and returns the encoded message.
6270 * 'protocol' must be for OpenFlow 1.4 or later. */
6272 ofputil_encode_requestforward(const struct ofputil_requestforward
*rf
,
6273 enum ofputil_protocol protocol
)
6275 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
6276 struct ofpbuf
*inner
;
6278 switch (rf
->reason
) {
6279 case OFPRFR_GROUP_MOD
:
6280 inner
= ofputil_encode_group_mod(ofp_version
, rf
->group_mod
);
6283 case OFPRFR_METER_MOD
:
6284 inner
= ofputil_encode_meter_mod(ofp_version
, rf
->meter_mod
);
6287 case OFPRFR_N_REASONS
:
6292 struct ofp_header
*inner_oh
= inner
->data
;
6293 inner_oh
->xid
= rf
->xid
;
6294 inner_oh
->length
= htons(inner
->size
);
6296 struct ofpbuf
*outer
= ofpraw_alloc_xid(OFPRAW_OFPT14_REQUESTFORWARD
,
6297 ofp_version
, htonl(0),
6299 ofpbuf_put(outer
, inner
->data
, inner
->size
);
6300 ofpbuf_delete(inner
);
6305 /* Decodes OFPT_REQUESTFORWARD message 'outer'. On success, puts the decoded
6306 * form into '*rf' and returns 0, and the caller is later responsible for
6307 * freeing the content of 'rf', with ofputil_destroy_requestforward(rf). On
6308 * failure, returns an ofperr and '*rf' is indeterminate. */
6310 ofputil_decode_requestforward(const struct ofp_header
*outer
,
6311 struct ofputil_requestforward
*rf
)
6313 struct ofpbuf b
= ofpbuf_const_initializer(outer
, ntohs(outer
->length
));
6315 /* Skip past outer message. */
6316 enum ofpraw outer_raw
= ofpraw_pull_assert(&b
);
6317 ovs_assert(outer_raw
== OFPRAW_OFPT14_REQUESTFORWARD
);
6319 /* Validate inner message. */
6320 if (b
.size
< sizeof(struct ofp_header
)) {
6321 return OFPERR_OFPBFC_MSG_BAD_LEN
;
6323 const struct ofp_header
*inner
= b
.data
;
6324 unsigned int inner_len
= ntohs(inner
->length
);
6325 if (inner_len
< sizeof(struct ofp_header
) || inner_len
> b
.size
) {
6326 return OFPERR_OFPBFC_MSG_BAD_LEN
;
6328 if (inner
->version
!= outer
->version
) {
6329 return OFPERR_OFPBRC_BAD_VERSION
;
6332 /* Parse inner message. */
6334 enum ofperr error
= ofptype_decode(&type
, inner
);
6339 rf
->xid
= inner
->xid
;
6340 if (type
== OFPTYPE_GROUP_MOD
) {
6341 rf
->reason
= OFPRFR_GROUP_MOD
;
6342 rf
->group_mod
= xmalloc(sizeof *rf
->group_mod
);
6343 error
= ofputil_decode_group_mod(inner
, rf
->group_mod
);
6345 free(rf
->group_mod
);
6348 } else if (type
== OFPTYPE_METER_MOD
) {
6349 rf
->reason
= OFPRFR_METER_MOD
;
6350 rf
->meter_mod
= xmalloc(sizeof *rf
->meter_mod
);
6351 ofpbuf_init(&rf
->bands
, 64);
6352 error
= ofputil_decode_meter_mod(inner
, rf
->meter_mod
, &rf
->bands
);
6354 free(rf
->meter_mod
);
6355 ofpbuf_uninit(&rf
->bands
);
6359 return OFPERR_OFPBFC_MSG_UNSUP
;
6365 /* Frees the content of 'rf', which should have been initialized through a
6366 * successful call to ofputil_decode_requestforward(). */
6368 ofputil_destroy_requestforward(struct ofputil_requestforward
*rf
)
6374 switch (rf
->reason
) {
6375 case OFPRFR_GROUP_MOD
:
6376 ofputil_uninit_group_mod(rf
->group_mod
);
6377 free(rf
->group_mod
);
6380 case OFPRFR_METER_MOD
:
6381 ofpbuf_uninit(&rf
->bands
);
6382 free(rf
->meter_mod
);
6385 case OFPRFR_N_REASONS
:
6392 /* OpenFlow 1.0 and 1.1 don't distinguish between a field that cannot be
6393 * matched and a field that must be wildcarded. This function returns a bitmap
6394 * that contains both kinds of fields. */
6395 static struct mf_bitmap
6396 wild_or_nonmatchable_fields(const struct ofputil_table_features
*features
)
6398 struct mf_bitmap wc
= features
->match
;
6399 bitmap_not(wc
.bm
, MFF_N_IDS
);
6400 bitmap_or(wc
.bm
, features
->wildcard
.bm
, MFF_N_IDS
);
6404 struct ofp10_wc_map
{
6405 enum ofp10_flow_wildcards wc10
;
6406 enum mf_field_id mf
;
6409 static const struct ofp10_wc_map ofp10_wc_map
[] = {
6410 { OFPFW10_IN_PORT
, MFF_IN_PORT
},
6411 { OFPFW10_DL_VLAN
, MFF_VLAN_VID
},
6412 { OFPFW10_DL_SRC
, MFF_ETH_SRC
},
6413 { OFPFW10_DL_DST
, MFF_ETH_DST
},
6414 { OFPFW10_DL_TYPE
, MFF_ETH_TYPE
},
6415 { OFPFW10_NW_PROTO
, MFF_IP_PROTO
},
6416 { OFPFW10_TP_SRC
, MFF_TCP_SRC
},
6417 { OFPFW10_TP_DST
, MFF_TCP_DST
},
6418 { OFPFW10_NW_SRC_MASK
, MFF_IPV4_SRC
},
6419 { OFPFW10_NW_DST_MASK
, MFF_IPV4_DST
},
6420 { OFPFW10_DL_VLAN_PCP
, MFF_VLAN_PCP
},
6421 { OFPFW10_NW_TOS
, MFF_IP_DSCP
},
6425 mf_bitmap_to_of10(const struct mf_bitmap
*fields
)
6427 const struct ofp10_wc_map
*p
;
6430 for (p
= ofp10_wc_map
; p
< &ofp10_wc_map
[ARRAY_SIZE(ofp10_wc_map
)]; p
++) {
6431 if (bitmap_is_set(fields
->bm
, p
->mf
)) {
6438 static struct mf_bitmap
6439 mf_bitmap_from_of10(ovs_be32 wc10_
)
6441 struct mf_bitmap fields
= MF_BITMAP_INITIALIZER
;
6442 const struct ofp10_wc_map
*p
;
6443 uint32_t wc10
= ntohl(wc10_
);
6445 for (p
= ofp10_wc_map
; p
< &ofp10_wc_map
[ARRAY_SIZE(ofp10_wc_map
)]; p
++) {
6446 if (wc10
& p
->wc10
) {
6447 bitmap_set1(fields
.bm
, p
->mf
);
6454 ofputil_put_ofp10_table_stats(const struct ofputil_table_stats
*stats
,
6455 const struct ofputil_table_features
*features
,
6458 struct mf_bitmap wc
= wild_or_nonmatchable_fields(features
);
6459 struct ofp10_table_stats
*out
;
6461 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
6462 out
->table_id
= features
->table_id
;
6463 ovs_strlcpy_arrays(out
->name
, features
->name
);
6464 out
->wildcards
= mf_bitmap_to_of10(&wc
);
6465 out
->max_entries
= htonl(features
->max_entries
);
6466 out
->active_count
= htonl(stats
->active_count
);
6467 put_32aligned_be64(&out
->lookup_count
, htonll(stats
->lookup_count
));
6468 put_32aligned_be64(&out
->matched_count
, htonll(stats
->matched_count
));
6471 struct ofp11_wc_map
{
6472 enum ofp11_flow_match_fields wc11
;
6473 enum mf_field_id mf
;
6476 static const struct ofp11_wc_map ofp11_wc_map
[] = {
6477 { OFPFMF11_IN_PORT
, MFF_IN_PORT
},
6478 { OFPFMF11_DL_VLAN
, MFF_VLAN_VID
},
6479 { OFPFMF11_DL_VLAN_PCP
, MFF_VLAN_PCP
},
6480 { OFPFMF11_DL_TYPE
, MFF_ETH_TYPE
},
6481 { OFPFMF11_NW_TOS
, MFF_IP_DSCP
},
6482 { OFPFMF11_NW_PROTO
, MFF_IP_PROTO
},
6483 { OFPFMF11_TP_SRC
, MFF_TCP_SRC
},
6484 { OFPFMF11_TP_DST
, MFF_TCP_DST
},
6485 { OFPFMF11_MPLS_LABEL
, MFF_MPLS_LABEL
},
6486 { OFPFMF11_MPLS_TC
, MFF_MPLS_TC
},
6487 /* I don't know what OFPFMF11_TYPE means. */
6488 { OFPFMF11_DL_SRC
, MFF_ETH_SRC
},
6489 { OFPFMF11_DL_DST
, MFF_ETH_DST
},
6490 { OFPFMF11_NW_SRC
, MFF_IPV4_SRC
},
6491 { OFPFMF11_NW_DST
, MFF_IPV4_DST
},
6492 { OFPFMF11_METADATA
, MFF_METADATA
},
6496 mf_bitmap_to_of11(const struct mf_bitmap
*fields
)
6498 const struct ofp11_wc_map
*p
;
6501 for (p
= ofp11_wc_map
; p
< &ofp11_wc_map
[ARRAY_SIZE(ofp11_wc_map
)]; p
++) {
6502 if (bitmap_is_set(fields
->bm
, p
->mf
)) {
6509 static struct mf_bitmap
6510 mf_bitmap_from_of11(ovs_be32 wc11_
)
6512 struct mf_bitmap fields
= MF_BITMAP_INITIALIZER
;
6513 const struct ofp11_wc_map
*p
;
6514 uint32_t wc11
= ntohl(wc11_
);
6516 for (p
= ofp11_wc_map
; p
< &ofp11_wc_map
[ARRAY_SIZE(ofp11_wc_map
)]; p
++) {
6517 if (wc11
& p
->wc11
) {
6518 bitmap_set1(fields
.bm
, p
->mf
);
6525 ofputil_put_ofp11_table_stats(const struct ofputil_table_stats
*stats
,
6526 const struct ofputil_table_features
*features
,
6529 struct mf_bitmap wc
= wild_or_nonmatchable_fields(features
);
6530 struct ofp11_table_stats
*out
;
6532 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
6533 out
->table_id
= features
->table_id
;
6534 ovs_strlcpy_arrays(out
->name
, features
->name
);
6535 out
->wildcards
= mf_bitmap_to_of11(&wc
);
6536 out
->match
= mf_bitmap_to_of11(&features
->match
);
6537 out
->instructions
= ovsinst_bitmap_to_openflow(
6538 features
->nonmiss
.instructions
, OFP11_VERSION
);
6539 out
->write_actions
= ofpact_bitmap_to_openflow(
6540 features
->nonmiss
.write
.ofpacts
, OFP11_VERSION
);
6541 out
->apply_actions
= ofpact_bitmap_to_openflow(
6542 features
->nonmiss
.apply
.ofpacts
, OFP11_VERSION
);
6543 out
->config
= htonl(features
->miss_config
);
6544 out
->max_entries
= htonl(features
->max_entries
);
6545 out
->active_count
= htonl(stats
->active_count
);
6546 out
->lookup_count
= htonll(stats
->lookup_count
);
6547 out
->matched_count
= htonll(stats
->matched_count
);
6551 ofputil_put_ofp12_table_stats(const struct ofputil_table_stats
*stats
,
6552 const struct ofputil_table_features
*features
,
6555 struct ofp12_table_stats
*out
;
6557 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
6558 out
->table_id
= features
->table_id
;
6559 ovs_strlcpy_arrays(out
->name
, features
->name
);
6560 out
->match
= oxm_bitmap_from_mf_bitmap(&features
->match
, OFP12_VERSION
);
6561 out
->wildcards
= oxm_bitmap_from_mf_bitmap(&features
->wildcard
,
6563 out
->write_actions
= ofpact_bitmap_to_openflow(
6564 features
->nonmiss
.write
.ofpacts
, OFP12_VERSION
);
6565 out
->apply_actions
= ofpact_bitmap_to_openflow(
6566 features
->nonmiss
.apply
.ofpacts
, OFP12_VERSION
);
6567 out
->write_setfields
= oxm_bitmap_from_mf_bitmap(
6568 &features
->nonmiss
.write
.set_fields
, OFP12_VERSION
);
6569 out
->apply_setfields
= oxm_bitmap_from_mf_bitmap(
6570 &features
->nonmiss
.apply
.set_fields
, OFP12_VERSION
);
6571 out
->metadata_match
= features
->metadata_match
;
6572 out
->metadata_write
= features
->metadata_write
;
6573 out
->instructions
= ovsinst_bitmap_to_openflow(
6574 features
->nonmiss
.instructions
, OFP12_VERSION
);
6575 out
->config
= ofputil_encode_table_config(features
->miss_config
,
6576 OFPUTIL_TABLE_EVICTION_DEFAULT
,
6577 OFPUTIL_TABLE_VACANCY_DEFAULT
,
6579 out
->max_entries
= htonl(features
->max_entries
);
6580 out
->active_count
= htonl(stats
->active_count
);
6581 out
->lookup_count
= htonll(stats
->lookup_count
);
6582 out
->matched_count
= htonll(stats
->matched_count
);
6586 ofputil_put_ofp13_table_stats(const struct ofputil_table_stats
*stats
,
6589 struct ofp13_table_stats
*out
;
6591 out
= ofpbuf_put_zeros(buf
, sizeof *out
);
6592 out
->table_id
= stats
->table_id
;
6593 out
->active_count
= htonl(stats
->active_count
);
6594 out
->lookup_count
= htonll(stats
->lookup_count
);
6595 out
->matched_count
= htonll(stats
->matched_count
);
6599 ofputil_encode_table_stats_reply(const struct ofp_header
*request
)
6601 return ofpraw_alloc_stats_reply(request
, 0);
6605 ofputil_append_table_stats_reply(struct ofpbuf
*reply
,
6606 const struct ofputil_table_stats
*stats
,
6607 const struct ofputil_table_features
*features
)
6609 struct ofp_header
*oh
= reply
->header
;
6611 ovs_assert(stats
->table_id
== features
->table_id
);
6613 switch ((enum ofp_version
) oh
->version
) {
6615 ofputil_put_ofp10_table_stats(stats
, features
, reply
);
6619 ofputil_put_ofp11_table_stats(stats
, features
, reply
);
6623 ofputil_put_ofp12_table_stats(stats
, features
, reply
);
6630 ofputil_put_ofp13_table_stats(stats
, reply
);
6639 ofputil_decode_ofp10_table_stats(struct ofpbuf
*msg
,
6640 struct ofputil_table_stats
*stats
,
6641 struct ofputil_table_features
*features
)
6643 struct ofp10_table_stats
*ots
;
6645 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
6647 return OFPERR_OFPBRC_BAD_LEN
;
6650 features
->table_id
= ots
->table_id
;
6651 ovs_strlcpy_arrays(features
->name
, ots
->name
);
6652 features
->max_entries
= ntohl(ots
->max_entries
);
6653 features
->match
= features
->wildcard
= mf_bitmap_from_of10(ots
->wildcards
);
6655 stats
->table_id
= ots
->table_id
;
6656 stats
->active_count
= ntohl(ots
->active_count
);
6657 stats
->lookup_count
= ntohll(get_32aligned_be64(&ots
->lookup_count
));
6658 stats
->matched_count
= ntohll(get_32aligned_be64(&ots
->matched_count
));
6664 ofputil_decode_ofp11_table_stats(struct ofpbuf
*msg
,
6665 struct ofputil_table_stats
*stats
,
6666 struct ofputil_table_features
*features
)
6668 struct ofp11_table_stats
*ots
;
6670 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
6672 return OFPERR_OFPBRC_BAD_LEN
;
6675 features
->table_id
= ots
->table_id
;
6676 ovs_strlcpy_arrays(features
->name
, ots
->name
);
6677 features
->max_entries
= ntohl(ots
->max_entries
);
6678 features
->nonmiss
.instructions
= ovsinst_bitmap_from_openflow(
6679 ots
->instructions
, OFP11_VERSION
);
6680 features
->nonmiss
.write
.ofpacts
= ofpact_bitmap_from_openflow(
6681 ots
->write_actions
, OFP11_VERSION
);
6682 features
->nonmiss
.apply
.ofpacts
= ofpact_bitmap_from_openflow(
6683 ots
->write_actions
, OFP11_VERSION
);
6684 features
->miss
= features
->nonmiss
;
6685 features
->miss_config
= ofputil_decode_table_miss(ots
->config
,
6687 features
->match
= mf_bitmap_from_of11(ots
->match
);
6688 features
->wildcard
= mf_bitmap_from_of11(ots
->wildcards
);
6689 bitmap_or(features
->match
.bm
, features
->wildcard
.bm
, MFF_N_IDS
);
6691 stats
->table_id
= ots
->table_id
;
6692 stats
->active_count
= ntohl(ots
->active_count
);
6693 stats
->lookup_count
= ntohll(ots
->lookup_count
);
6694 stats
->matched_count
= ntohll(ots
->matched_count
);
6700 ofputil_decode_ofp12_table_stats(struct ofpbuf
*msg
,
6701 struct ofputil_table_stats
*stats
,
6702 struct ofputil_table_features
*features
)
6704 struct ofp12_table_stats
*ots
;
6706 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
6708 return OFPERR_OFPBRC_BAD_LEN
;
6711 features
->table_id
= ots
->table_id
;
6712 ovs_strlcpy_arrays(features
->name
, ots
->name
);
6713 features
->metadata_match
= ots
->metadata_match
;
6714 features
->metadata_write
= ots
->metadata_write
;
6715 features
->miss_config
= ofputil_decode_table_miss(ots
->config
,
6717 features
->max_entries
= ntohl(ots
->max_entries
);
6719 features
->nonmiss
.instructions
= ovsinst_bitmap_from_openflow(
6720 ots
->instructions
, OFP12_VERSION
);
6721 features
->nonmiss
.write
.ofpacts
= ofpact_bitmap_from_openflow(
6722 ots
->write_actions
, OFP12_VERSION
);
6723 features
->nonmiss
.apply
.ofpacts
= ofpact_bitmap_from_openflow(
6724 ots
->apply_actions
, OFP12_VERSION
);
6725 features
->nonmiss
.write
.set_fields
= oxm_bitmap_to_mf_bitmap(
6726 ots
->write_setfields
, OFP12_VERSION
);
6727 features
->nonmiss
.apply
.set_fields
= oxm_bitmap_to_mf_bitmap(
6728 ots
->apply_setfields
, OFP12_VERSION
);
6729 features
->miss
= features
->nonmiss
;
6731 features
->match
= oxm_bitmap_to_mf_bitmap(ots
->match
, OFP12_VERSION
);
6732 features
->wildcard
= oxm_bitmap_to_mf_bitmap(ots
->wildcards
,
6734 bitmap_or(features
->match
.bm
, features
->wildcard
.bm
, MFF_N_IDS
);
6736 stats
->table_id
= ots
->table_id
;
6737 stats
->active_count
= ntohl(ots
->active_count
);
6738 stats
->lookup_count
= ntohll(ots
->lookup_count
);
6739 stats
->matched_count
= ntohll(ots
->matched_count
);
6745 ofputil_decode_ofp13_table_stats(struct ofpbuf
*msg
,
6746 struct ofputil_table_stats
*stats
,
6747 struct ofputil_table_features
*features
)
6749 struct ofp13_table_stats
*ots
;
6751 ots
= ofpbuf_try_pull(msg
, sizeof *ots
);
6753 return OFPERR_OFPBRC_BAD_LEN
;
6756 features
->table_id
= ots
->table_id
;
6758 stats
->table_id
= ots
->table_id
;
6759 stats
->active_count
= ntohl(ots
->active_count
);
6760 stats
->lookup_count
= ntohll(ots
->lookup_count
);
6761 stats
->matched_count
= ntohll(ots
->matched_count
);
6767 ofputil_decode_table_stats_reply(struct ofpbuf
*msg
,
6768 struct ofputil_table_stats
*stats
,
6769 struct ofputil_table_features
*features
)
6771 const struct ofp_header
*oh
;
6774 ofpraw_pull_assert(msg
);
6782 memset(stats
, 0, sizeof *stats
);
6783 memset(features
, 0, sizeof *features
);
6784 features
->supports_eviction
= -1;
6785 features
->supports_vacancy_events
= -1;
6787 switch ((enum ofp_version
) oh
->version
) {
6789 return ofputil_decode_ofp10_table_stats(msg
, stats
, features
);
6792 return ofputil_decode_ofp11_table_stats(msg
, stats
, features
);
6795 return ofputil_decode_ofp12_table_stats(msg
, stats
, features
);
6801 return ofputil_decode_ofp13_table_stats(msg
, stats
, features
);
6808 /* ofputil_flow_monitor_request */
6810 /* Converts an NXST_FLOW_MONITOR request in 'msg' into an abstract
6811 * ofputil_flow_monitor_request in 'rq'.
6813 * Multiple NXST_FLOW_MONITOR requests can be packed into a single OpenFlow
6814 * message. Calling this function multiple times for a single 'msg' iterates
6815 * through the requests. The caller must initially leave 'msg''s layer
6816 * pointers null and not modify them between calls.
6818 * Returns 0 if successful, EOF if no requests were left in this 'msg',
6819 * otherwise an OFPERR_* value. */
6821 ofputil_decode_flow_monitor_request(struct ofputil_flow_monitor_request
*rq
,
6824 struct nx_flow_monitor_request
*nfmr
;
6828 ofpraw_pull_assert(msg
);
6835 nfmr
= ofpbuf_try_pull(msg
, sizeof *nfmr
);
6837 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW_MONITOR request has %"PRIu32
" "
6838 "leftover bytes at end", msg
->size
);
6839 return OFPERR_OFPBRC_BAD_LEN
;
6842 flags
= ntohs(nfmr
->flags
);
6843 if (!(flags
& (NXFMF_ADD
| NXFMF_DELETE
| NXFMF_MODIFY
))
6844 || flags
& ~(NXFMF_INITIAL
| NXFMF_ADD
| NXFMF_DELETE
6845 | NXFMF_MODIFY
| NXFMF_ACTIONS
| NXFMF_OWN
)) {
6846 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW_MONITOR has bad flags %#"PRIx16
,
6848 return OFPERR_OFPMOFC_BAD_FLAGS
;
6851 if (!is_all_zeros(nfmr
->zeros
, sizeof nfmr
->zeros
)) {
6852 return OFPERR_NXBRC_MUST_BE_ZERO
;
6855 rq
->id
= ntohl(nfmr
->id
);
6857 rq
->out_port
= u16_to_ofp(ntohs(nfmr
->out_port
));
6858 rq
->table_id
= nfmr
->table_id
;
6860 return nx_pull_match(msg
, ntohs(nfmr
->match_len
), &rq
->match
, NULL
,
6861 NULL
, false, NULL
, NULL
);
6865 ofputil_append_flow_monitor_request(
6866 const struct ofputil_flow_monitor_request
*rq
, struct ofpbuf
*msg
)
6868 struct nx_flow_monitor_request
*nfmr
;
6873 ofpraw_put(OFPRAW_NXST_FLOW_MONITOR_REQUEST
, OFP10_VERSION
, msg
);
6876 start_ofs
= msg
->size
;
6877 ofpbuf_put_zeros(msg
, sizeof *nfmr
);
6878 match_len
= nx_put_match(msg
, &rq
->match
, htonll(0), htonll(0));
6880 nfmr
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *nfmr
);
6881 nfmr
->id
= htonl(rq
->id
);
6882 nfmr
->flags
= htons(rq
->flags
);
6883 nfmr
->out_port
= htons(ofp_to_u16(rq
->out_port
));
6884 nfmr
->match_len
= htons(match_len
);
6885 nfmr
->table_id
= rq
->table_id
;
6888 /* Converts an NXST_FLOW_MONITOR reply (also known as a flow update) in 'msg'
6889 * into an abstract ofputil_flow_update in 'update'. The caller must have
6890 * initialized update->match to point to space allocated for a match.
6892 * Uses 'ofpacts' to store the abstract OFPACT_* version of the update's
6893 * actions (except for NXFME_ABBREV, which never includes actions). The caller
6894 * must initialize 'ofpacts' and retains ownership of it. 'update->ofpacts'
6895 * will point into the 'ofpacts' buffer.
6897 * Multiple flow updates can be packed into a single OpenFlow message. Calling
6898 * this function multiple times for a single 'msg' iterates through the
6899 * updates. The caller must initially leave 'msg''s layer pointers null and
6900 * not modify them between calls.
6902 * Returns 0 if successful, EOF if no updates were left in this 'msg',
6903 * otherwise an OFPERR_* value. */
6905 ofputil_decode_flow_update(struct ofputil_flow_update
*update
,
6906 struct ofpbuf
*msg
, struct ofpbuf
*ofpacts
)
6908 struct nx_flow_update_header
*nfuh
;
6909 unsigned int length
;
6910 struct ofp_header
*oh
;
6913 ofpraw_pull_assert(msg
);
6916 ofpbuf_clear(ofpacts
);
6921 if (msg
->size
< sizeof(struct nx_flow_update_header
)) {
6928 update
->event
= ntohs(nfuh
->event
);
6929 length
= ntohs(nfuh
->length
);
6930 if (length
> msg
->size
|| length
% 8) {
6934 if (update
->event
== NXFME_ABBREV
) {
6935 struct nx_flow_update_abbrev
*nfua
;
6937 if (length
!= sizeof *nfua
) {
6941 nfua
= ofpbuf_pull(msg
, sizeof *nfua
);
6942 update
->xid
= nfua
->xid
;
6944 } else if (update
->event
== NXFME_ADDED
6945 || update
->event
== NXFME_DELETED
6946 || update
->event
== NXFME_MODIFIED
) {
6947 struct nx_flow_update_full
*nfuf
;
6948 unsigned int actions_len
;
6949 unsigned int match_len
;
6952 if (length
< sizeof *nfuf
) {
6956 nfuf
= ofpbuf_pull(msg
, sizeof *nfuf
);
6957 match_len
= ntohs(nfuf
->match_len
);
6958 if (sizeof *nfuf
+ match_len
> length
) {
6962 update
->reason
= ntohs(nfuf
->reason
);
6963 update
->idle_timeout
= ntohs(nfuf
->idle_timeout
);
6964 update
->hard_timeout
= ntohs(nfuf
->hard_timeout
);
6965 update
->table_id
= nfuf
->table_id
;
6966 update
->cookie
= nfuf
->cookie
;
6967 update
->priority
= ntohs(nfuf
->priority
);
6969 error
= nx_pull_match(msg
, match_len
, &update
->match
, NULL
, NULL
,
6975 actions_len
= length
- sizeof *nfuf
- ROUND_UP(match_len
, 8);
6976 error
= ofpacts_pull_openflow_actions(msg
, actions_len
, oh
->version
,
6977 NULL
, NULL
, ofpacts
);
6982 update
->ofpacts
= ofpacts
->data
;
6983 update
->ofpacts_len
= ofpacts
->size
;
6986 VLOG_WARN_RL(&bad_ofmsg_rl
,
6987 "NXST_FLOW_MONITOR reply has bad event %"PRIu16
,
6988 ntohs(nfuh
->event
));
6989 return OFPERR_NXBRC_FM_BAD_EVENT
;
6993 VLOG_WARN_RL(&bad_ofmsg_rl
, "NXST_FLOW_MONITOR reply has %"PRIu32
" "
6994 "leftover bytes at end", msg
->size
);
6995 return OFPERR_OFPBRC_BAD_LEN
;
6999 ofputil_decode_flow_monitor_cancel(const struct ofp_header
*oh
)
7001 const struct nx_flow_monitor_cancel
*cancel
= ofpmsg_body(oh
);
7003 return ntohl(cancel
->id
);
7007 ofputil_encode_flow_monitor_cancel(uint32_t id
)
7009 struct nx_flow_monitor_cancel
*nfmc
;
7012 msg
= ofpraw_alloc(OFPRAW_NXT_FLOW_MONITOR_CANCEL
, OFP10_VERSION
, 0);
7013 nfmc
= ofpbuf_put_uninit(msg
, sizeof *nfmc
);
7014 nfmc
->id
= htonl(id
);
7019 ofputil_start_flow_update(struct ovs_list
*replies
)
7023 msg
= ofpraw_alloc_xid(OFPRAW_NXST_FLOW_MONITOR_REPLY
, OFP10_VERSION
,
7026 ovs_list_init(replies
);
7027 ovs_list_push_back(replies
, &msg
->list_node
);
7031 ofputil_append_flow_update(const struct ofputil_flow_update
*update
,
7032 struct ovs_list
*replies
,
7033 const struct tun_table
*tun_table
)
7035 struct ofputil_flow_update
*update_
=
7036 CONST_CAST(struct ofputil_flow_update
*, update
);
7037 const struct tun_table
*orig_tun_table
;
7038 enum ofp_version version
= ofpmp_version(replies
);
7039 struct nx_flow_update_header
*nfuh
;
7043 orig_tun_table
= update
->match
.flow
.tunnel
.metadata
.tab
;
7044 update_
->match
.flow
.tunnel
.metadata
.tab
= tun_table
;
7046 msg
= ofpbuf_from_list(ovs_list_back(replies
));
7047 start_ofs
= msg
->size
;
7049 if (update
->event
== NXFME_ABBREV
) {
7050 struct nx_flow_update_abbrev
*nfua
;
7052 nfua
= ofpbuf_put_zeros(msg
, sizeof *nfua
);
7053 nfua
->xid
= update
->xid
;
7055 struct nx_flow_update_full
*nfuf
;
7058 ofpbuf_put_zeros(msg
, sizeof *nfuf
);
7059 match_len
= nx_put_match(msg
, &update
->match
, htonll(0), htonll(0));
7060 ofpacts_put_openflow_actions(update
->ofpacts
, update
->ofpacts_len
, msg
,
7062 nfuf
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *nfuf
);
7063 nfuf
->reason
= htons(update
->reason
);
7064 nfuf
->priority
= htons(update
->priority
);
7065 nfuf
->idle_timeout
= htons(update
->idle_timeout
);
7066 nfuf
->hard_timeout
= htons(update
->hard_timeout
);
7067 nfuf
->match_len
= htons(match_len
);
7068 nfuf
->table_id
= update
->table_id
;
7069 nfuf
->cookie
= update
->cookie
;
7072 nfuh
= ofpbuf_at_assert(msg
, start_ofs
, sizeof *nfuh
);
7073 nfuh
->length
= htons(msg
->size
- start_ofs
);
7074 nfuh
->event
= htons(update
->event
);
7076 ofpmp_postappend(replies
, start_ofs
);
7077 update_
->match
.flow
.tunnel
.metadata
.tab
= orig_tun_table
;
7081 ofputil_encode_packet_out(const struct ofputil_packet_out
*po
,
7082 enum ofputil_protocol protocol
)
7084 enum ofp_version ofp_version
= ofputil_protocol_to_ofp_version(protocol
);
7088 size
= po
->ofpacts_len
;
7089 if (po
->buffer_id
== UINT32_MAX
) {
7090 size
+= po
->packet_len
;
7093 switch (ofp_version
) {
7094 case OFP10_VERSION
: {
7095 struct ofp10_packet_out
*opo
;
7098 msg
= ofpraw_alloc(OFPRAW_OFPT10_PACKET_OUT
, OFP10_VERSION
, size
);
7099 ofpbuf_put_zeros(msg
, sizeof *opo
);
7100 actions_ofs
= msg
->size
;
7101 ofpacts_put_openflow_actions(po
->ofpacts
, po
->ofpacts_len
, msg
,
7105 opo
->buffer_id
= htonl(po
->buffer_id
);
7106 opo
->in_port
=htons(ofp_to_u16(
7107 po
->flow_metadata
.flow
.in_port
.ofp_port
));
7108 opo
->actions_len
= htons(msg
->size
- actions_ofs
);
7115 case OFP14_VERSION
: {
7116 struct ofp11_packet_out
*opo
;
7119 msg
= ofpraw_alloc(OFPRAW_OFPT11_PACKET_OUT
, ofp_version
, size
);
7120 ofpbuf_put_zeros(msg
, sizeof *opo
);
7121 len
= ofpacts_put_openflow_actions(po
->ofpacts
, po
->ofpacts_len
, msg
,
7124 opo
->buffer_id
= htonl(po
->buffer_id
);
7126 ofputil_port_to_ofp11(po
->flow_metadata
.flow
.in_port
.ofp_port
);
7127 opo
->actions_len
= htons(len
);
7132 case OFP16_VERSION
: {
7133 struct ofp15_packet_out
*opo
;
7136 /* The final argument is just an estimate of the space required. */
7137 msg
= ofpraw_alloc(OFPRAW_OFPT15_PACKET_OUT
, ofp_version
,
7138 size
+ NXM_TYPICAL_LEN
);
7139 ofpbuf_put_zeros(msg
, sizeof *opo
);
7140 oxm_put_match(msg
, &po
->flow_metadata
, ofp_version
);
7141 len
= ofpacts_put_openflow_actions(po
->ofpacts
, po
->ofpacts_len
, msg
,
7144 opo
->buffer_id
= htonl(po
->buffer_id
);
7145 opo
->actions_len
= htons(len
);
7153 if (po
->buffer_id
== UINT32_MAX
) {
7154 ofpbuf_put(msg
, po
->packet
, po
->packet_len
);
7157 ofpmsg_update_length(msg
);
7162 /* Creates and returns an OFPT_ECHO_REQUEST message with an empty payload. */
7164 make_echo_request(enum ofp_version ofp_version
)
7166 return ofpraw_alloc_xid(OFPRAW_OFPT_ECHO_REQUEST
, ofp_version
,
7170 /* Creates and returns an OFPT_ECHO_REPLY message matching the
7171 * OFPT_ECHO_REQUEST message in 'rq'. */
7173 make_echo_reply(const struct ofp_header
*rq
)
7175 struct ofpbuf rq_buf
= ofpbuf_const_initializer(rq
, ntohs(rq
->length
));
7176 ofpraw_pull_assert(&rq_buf
);
7178 struct ofpbuf
*reply
= ofpraw_alloc_reply(OFPRAW_OFPT_ECHO_REPLY
,
7180 ofpbuf_put(reply
, rq_buf
.data
, rq_buf
.size
);
7185 ofputil_encode_barrier_request(enum ofp_version ofp_version
)
7189 switch (ofp_version
) {
7196 type
= OFPRAW_OFPT11_BARRIER_REQUEST
;
7200 type
= OFPRAW_OFPT10_BARRIER_REQUEST
;
7207 return ofpraw_alloc(type
, ofp_version
, 0);
7211 ofputil_frag_handling_to_string(enum ofputil_frag_handling frag
)
7214 case OFPUTIL_FRAG_NORMAL
: return "normal";
7215 case OFPUTIL_FRAG_DROP
: return "drop";
7216 case OFPUTIL_FRAG_REASM
: return "reassemble";
7217 case OFPUTIL_FRAG_NX_MATCH
: return "nx-match";
7224 ofputil_frag_handling_from_string(const char *s
,
7225 enum ofputil_frag_handling
*frag
)
7227 if (!strcasecmp(s
, "normal")) {
7228 *frag
= OFPUTIL_FRAG_NORMAL
;
7229 } else if (!strcasecmp(s
, "drop")) {
7230 *frag
= OFPUTIL_FRAG_DROP
;
7231 } else if (!strcasecmp(s
, "reassemble")) {
7232 *frag
= OFPUTIL_FRAG_REASM
;
7233 } else if (!strcasecmp(s
, "nx-match")) {
7234 *frag
= OFPUTIL_FRAG_NX_MATCH
;
7241 /* Converts the OpenFlow 1.1+ port number 'ofp11_port' into an OpenFlow 1.0
7242 * port number and stores the latter in '*ofp10_port', for the purpose of
7243 * decoding OpenFlow 1.1+ protocol messages. Returns 0 if successful,
7244 * otherwise an OFPERR_* number. On error, stores OFPP_NONE in '*ofp10_port'.
7246 * See the definition of OFP11_MAX for an explanation of the mapping. */
7248 ofputil_port_from_ofp11(ovs_be32 ofp11_port
, ofp_port_t
*ofp10_port
)
7250 uint32_t ofp11_port_h
= ntohl(ofp11_port
);
7252 if (ofp11_port_h
< ofp_to_u16(OFPP_MAX
)) {
7253 *ofp10_port
= u16_to_ofp(ofp11_port_h
);
7255 } else if (ofp11_port_h
>= ofp11_to_u32(OFPP11_MAX
)) {
7256 *ofp10_port
= u16_to_ofp(ofp11_port_h
- OFPP11_OFFSET
);
7259 *ofp10_port
= OFPP_NONE
;
7260 VLOG_WARN_RL(&bad_ofmsg_rl
, "port %"PRIu32
" is outside the supported "
7261 "range 0 through %d or 0x%"PRIx32
" through 0x%"PRIx32
,
7262 ofp11_port_h
, ofp_to_u16(OFPP_MAX
) - 1,
7263 ofp11_to_u32(OFPP11_MAX
), UINT32_MAX
);
7264 return OFPERR_OFPBAC_BAD_OUT_PORT
;
7268 /* Returns the OpenFlow 1.1+ port number equivalent to the OpenFlow 1.0 port
7269 * number 'ofp10_port', for encoding OpenFlow 1.1+ protocol messages.
7271 * See the definition of OFP11_MAX for an explanation of the mapping. */
7273 ofputil_port_to_ofp11(ofp_port_t ofp10_port
)
7275 return htonl(ofp_to_u16(ofp10_port
) < ofp_to_u16(OFPP_MAX
)
7276 ? ofp_to_u16(ofp10_port
)
7277 : ofp_to_u16(ofp10_port
) + OFPP11_OFFSET
);
7280 #define OFPUTIL_NAMED_PORTS \
7281 OFPUTIL_NAMED_PORT(IN_PORT) \
7282 OFPUTIL_NAMED_PORT(TABLE) \
7283 OFPUTIL_NAMED_PORT(NORMAL) \
7284 OFPUTIL_NAMED_PORT(FLOOD) \
7285 OFPUTIL_NAMED_PORT(ALL) \
7286 OFPUTIL_NAMED_PORT(CONTROLLER) \
7287 OFPUTIL_NAMED_PORT(LOCAL) \
7288 OFPUTIL_NAMED_PORT(ANY) \
7289 OFPUTIL_NAMED_PORT(UNSET)
7291 /* For backwards compatibility, so that "none" is recognized as OFPP_ANY */
7292 #define OFPUTIL_NAMED_PORTS_WITH_NONE \
7293 OFPUTIL_NAMED_PORTS \
7294 OFPUTIL_NAMED_PORT(NONE)
7296 /* Stores the port number represented by 's' into '*portp'. 's' may be an
7297 * integer or, for reserved ports, the standard OpenFlow name for the port
7298 * (e.g. "LOCAL"). If 'port_map' is nonnull, also accepts names in it (quoted
7301 * Returns true if successful, false if 's' is not a valid OpenFlow port number
7302 * or name. The caller should issue an error message in this case, because
7303 * this function usually does not. (This gives the caller an opportunity to
7304 * look up the port name another way, e.g. by contacting the switch and listing
7305 * the names of all its ports).
7307 * This function accepts OpenFlow 1.0 port numbers. It also accepts a subset
7308 * of OpenFlow 1.1+ port numbers, mapping those port numbers into the 16-bit
7309 * range as described in include/openflow/openflow-1.1.h. */
7311 ofputil_port_from_string(const char *s
,
7312 const struct ofputil_port_map
*port_map
,
7315 unsigned int port32
; /* int is at least 32 bits wide. */
7318 VLOG_WARN("Negative value %s is not a valid port number.", s
);
7322 if (str_to_uint(s
, 10, &port32
)) {
7323 if (port32
< ofp_to_u16(OFPP_MAX
)) {
7325 } else if (port32
< ofp_to_u16(OFPP_FIRST_RESV
)) {
7326 VLOG_WARN("port %u is a reserved OF1.0 port number that will "
7327 "be translated to %u when talking to an OF1.1 or "
7328 "later controller", port32
, port32
+ OFPP11_OFFSET
);
7329 } else if (port32
<= ofp_to_u16(OFPP_LAST_RESV
)) {
7330 char name
[OFP10_MAX_PORT_NAME_LEN
];
7332 ofputil_port_to_string(u16_to_ofp(port32
), NULL
,
7334 VLOG_WARN_ONCE("referring to port %s as %"PRIu32
" is deprecated "
7335 "for compatibility with OpenFlow 1.1 and later",
7337 } else if (port32
< ofp11_to_u32(OFPP11_MAX
)) {
7338 VLOG_WARN("port %u is outside the supported range 0 through "
7339 "%x or 0x%x through 0x%"PRIx32
, port32
,
7340 UINT16_MAX
, ofp11_to_u32(OFPP11_MAX
), UINT32_MAX
);
7343 port32
-= OFPP11_OFFSET
;
7346 *portp
= u16_to_ofp(port32
);
7353 static const struct pair pairs
[] = {
7354 #define OFPUTIL_NAMED_PORT(NAME) {#NAME, OFPP_##NAME},
7355 OFPUTIL_NAMED_PORTS_WITH_NONE
7356 #undef OFPUTIL_NAMED_PORT
7358 const struct pair
*p
;
7360 for (p
= pairs
; p
< &pairs
[ARRAY_SIZE(pairs
)]; p
++) {
7361 if (!strcasecmp(s
, p
->name
)) {
7367 ofp_port_t ofp_port
= OFPP_NONE
;
7369 ofp_port
= ofputil_port_map_get_number(port_map
, s
);
7371 size_t length
= strlen(s
);
7374 && s
[length
- 1] == '"'
7375 && json_string_unescape(s
+ 1, length
- 2, &name
)) {
7376 ofp_port
= ofputil_port_map_get_number(port_map
, name
);
7380 if (ofp_port
!= OFPP_NONE
) {
7390 ofputil_port_get_reserved_name(ofp_port_t port
)
7393 #define OFPUTIL_NAMED_PORT(NAME) case OFPP_##NAME: return #NAME;
7395 #undef OFPUTIL_NAMED_PORT
7402 /* A port name doesn't need to be quoted if it is alphanumeric and starts with
7405 port_name_needs_quotes(const char *port_name
)
7407 if (!isalpha((unsigned char) port_name
[0])) {
7411 for (const char *p
= port_name
+ 1; *p
; p
++) {
7412 if (!isalnum((unsigned char) *p
)) {
7420 put_port_name(const char *port_name
, struct ds
*s
)
7422 if (port_name_needs_quotes(port_name
)) {
7423 json_string_escape(port_name
, s
);
7425 ds_put_cstr(s
, port_name
);
7429 /* Appends to 's' a string representation of the OpenFlow port number 'port'.
7430 * Most ports' string representation is just the port number, but for special
7431 * ports, e.g. OFPP_LOCAL, it is the name, e.g. "LOCAL". */
7433 ofputil_format_port(ofp_port_t port
, const struct ofputil_port_map
*port_map
,
7436 const char *reserved_name
= ofputil_port_get_reserved_name(port
);
7437 if (reserved_name
) {
7438 ds_put_cstr(s
, reserved_name
);
7442 const char *port_name
= ofputil_port_map_get_name(port_map
, port
);
7444 put_port_name(port_name
, s
);
7448 ds_put_format(s
, "%"PRIu32
, port
);
7451 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
7452 * representation of OpenFlow port number 'port'. Most ports are represented
7453 * as just the port number, but special ports, e.g. OFPP_LOCAL, are represented
7454 * by name, e.g. "LOCAL". */
7456 ofputil_port_to_string(ofp_port_t port
,
7457 const struct ofputil_port_map
*port_map
,
7458 char *namebuf
, size_t bufsize
)
7460 const char *reserved_name
= ofputil_port_get_reserved_name(port
);
7461 if (reserved_name
) {
7462 ovs_strlcpy(namebuf
, reserved_name
, bufsize
);
7466 const char *port_name
= ofputil_port_map_get_name(port_map
, port
);
7468 struct ds s
= DS_EMPTY_INITIALIZER
;
7469 put_port_name(port_name
, &s
);
7470 ovs_strlcpy(namebuf
, ds_cstr(&s
), bufsize
);
7475 snprintf(namebuf
, bufsize
, "%"PRIu32
, port
);
7478 /* ofputil_port_map. */
7479 struct ofputil_port_map_node
{
7480 struct hmap_node name_node
;
7481 struct hmap_node number_node
;
7482 ofp_port_t ofp_port
; /* Port number. */
7483 char *name
; /* Port name. */
7485 /* OpenFlow doesn't require port names to be unique, although that's the
7486 * only sensible way. However, even in Open vSwitch it's possible for two
7487 * ports to appear to have the same name if their names are longer than the
7488 * maximum length supported by a given version of OpenFlow. So, we guard
7489 * against duplicate names to avoid giving unexpected results in this
7492 * OpenFlow does require port numbers to be unique. We check for duplicate
7493 * ports numbers just in case a switch has a bug. */
7498 ofputil_port_map_init(struct ofputil_port_map
*map
)
7500 hmap_init(&map
->by_name
);
7501 hmap_init(&map
->by_number
);
7504 static struct ofputil_port_map_node
*
7505 ofputil_port_map_find_by_name(const struct ofputil_port_map
*map
,
7508 struct ofputil_port_map_node
*node
;
7510 HMAP_FOR_EACH_WITH_HASH (node
, name_node
, hash_string(name
, 0),
7512 if (!strcmp(name
, node
->name
)) {
7519 static struct ofputil_port_map_node
*
7520 ofputil_port_map_find_by_number(const struct ofputil_port_map
*map
,
7521 ofp_port_t ofp_port
)
7523 struct ofputil_port_map_node
*node
;
7525 HMAP_FOR_EACH_IN_BUCKET (node
, number_node
, hash_ofp_port(ofp_port
),
7527 if (node
->ofp_port
== ofp_port
) {
7535 ofputil_port_map_put(struct ofputil_port_map
*map
,
7536 ofp_port_t ofp_port
, const char *name
)
7538 struct ofputil_port_map_node
*node
;
7540 /* Look for duplicate name. */
7541 node
= ofputil_port_map_find_by_name(map
, name
);
7543 if (node
->ofp_port
!= ofp_port
) {
7544 node
->duplicate
= true;
7549 /* Look for duplicate number. */
7550 node
= ofputil_port_map_find_by_number(map
, ofp_port
);
7552 node
->duplicate
= true;
7557 node
= xmalloc(sizeof *node
);
7558 hmap_insert(&map
->by_number
, &node
->number_node
, hash_ofp_port(ofp_port
));
7559 hmap_insert(&map
->by_name
, &node
->name_node
, hash_string(name
, 0));
7560 node
->ofp_port
= ofp_port
;
7561 node
->name
= xstrdup(name
);
7562 node
->duplicate
= false;
7566 ofputil_port_map_get_name(const struct ofputil_port_map
*map
,
7567 ofp_port_t ofp_port
)
7569 struct ofputil_port_map_node
*node
7570 = map
? ofputil_port_map_find_by_number(map
, ofp_port
) : NULL
;
7571 return node
&& !node
->duplicate
? node
->name
: NULL
;
7575 ofputil_port_map_get_number(const struct ofputil_port_map
*map
,
7578 struct ofputil_port_map_node
*node
7579 = map
? ofputil_port_map_find_by_name(map
, name
) : NULL
;
7580 return node
&& !node
->duplicate
? node
->ofp_port
: OFPP_NONE
;
7584 ofputil_port_map_destroy(struct ofputil_port_map
*map
)
7587 struct ofputil_port_map_node
*node
, *next
;
7589 HMAP_FOR_EACH_SAFE (node
, next
, name_node
, &map
->by_name
) {
7590 hmap_remove(&map
->by_name
, &node
->name_node
);
7591 hmap_remove(&map
->by_number
, &node
->number_node
);
7595 hmap_destroy(&map
->by_name
);
7596 hmap_destroy(&map
->by_number
);
7600 /* Stores the group id represented by 's' into '*group_idp'. 's' may be an
7601 * integer or, for reserved group IDs, the standard OpenFlow name for the group
7602 * (either "ANY" or "ALL").
7604 * Returns true if successful, false if 's' is not a valid OpenFlow group ID or
7607 ofputil_group_from_string(const char *s
, uint32_t *group_idp
)
7609 if (!strcasecmp(s
, "any")) {
7610 *group_idp
= OFPG_ANY
;
7611 } else if (!strcasecmp(s
, "all")) {
7612 *group_idp
= OFPG_ALL
;
7613 } else if (!str_to_uint(s
, 10, group_idp
)) {
7614 VLOG_WARN("%s is not a valid group ID. (Valid group IDs are "
7615 "32-bit nonnegative integers or the keywords ANY or "
7623 /* Appends to 's' a string representation of the OpenFlow group ID 'group_id'.
7624 * Most groups' string representation is just the number, but for special
7625 * groups, e.g. OFPG_ALL, it is the name, e.g. "ALL". */
7627 ofputil_format_group(uint32_t group_id
, struct ds
*s
)
7629 char name
[MAX_GROUP_NAME_LEN
];
7631 ofputil_group_to_string(group_id
, name
, sizeof name
);
7632 ds_put_cstr(s
, name
);
7636 /* Puts in the 'bufsize' byte in 'namebuf' a null-terminated string
7637 * representation of OpenFlow group ID 'group_id'. Most group are represented
7638 * as just their number, but special groups, e.g. OFPG_ALL, are represented
7639 * by name, e.g. "ALL". */
7641 ofputil_group_to_string(uint32_t group_id
,
7642 char namebuf
[MAX_GROUP_NAME_LEN
+ 1], size_t bufsize
)
7646 ovs_strlcpy(namebuf
, "ALL", bufsize
);
7650 ovs_strlcpy(namebuf
, "ANY", bufsize
);
7654 snprintf(namebuf
, bufsize
, "%"PRIu32
, group_id
);
7659 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
7660 * 'ofp_version', tries to pull the first element from the array. If
7661 * successful, initializes '*pp' with an abstract representation of the
7662 * port and returns 0. If no ports remain to be decoded, returns EOF.
7663 * On an error, returns a positive OFPERR_* value. */
7665 ofputil_pull_phy_port(enum ofp_version ofp_version
, struct ofpbuf
*b
,
7666 struct ofputil_phy_port
*pp
)
7668 memset(pp
, 0, sizeof *pp
);
7670 switch (ofp_version
) {
7671 case OFP10_VERSION
: {
7672 const struct ofp10_phy_port
*opp
= ofpbuf_try_pull(b
, sizeof *opp
);
7673 return opp
? ofputil_decode_ofp10_phy_port(pp
, opp
) : EOF
;
7677 case OFP13_VERSION
: {
7678 const struct ofp11_port
*op
= ofpbuf_try_pull(b
, sizeof *op
);
7679 return op
? ofputil_decode_ofp11_port(pp
, op
) : EOF
;
7683 return b
->size
? ofputil_pull_ofp14_port(pp
, b
) : EOF
;
7685 return b
->size
? ofputil_pull_ofp16_port(pp
, b
) : EOF
;
7692 ofputil_normalize_match__(struct match
*match
, bool may_log
)
7695 MAY_NW_ADDR
= 1 << 0, /* nw_src, nw_dst */
7696 MAY_TP_ADDR
= 1 << 1, /* tp_src, tp_dst */
7697 MAY_NW_PROTO
= 1 << 2, /* nw_proto */
7698 MAY_IPVx
= 1 << 3, /* tos, frag, ttl */
7699 MAY_ARP_SHA
= 1 << 4, /* arp_sha */
7700 MAY_ARP_THA
= 1 << 5, /* arp_tha */
7701 MAY_IPV6
= 1 << 6, /* ipv6_src, ipv6_dst, ipv6_label */
7702 MAY_ND_TARGET
= 1 << 7, /* nd_target */
7703 MAY_MPLS
= 1 << 8, /* mpls label and tc */
7704 MAY_ETHER
= 1 << 9, /* dl_src, dl_dst */
7707 struct flow_wildcards wc
= match
->wc
;
7710 /* Figure out what fields may be matched. */
7711 /* Check the packet_type first and extract dl_type. */
7712 if (wc
.masks
.packet_type
== 0 || match_has_default_packet_type(match
)) {
7713 may_match
= MAY_ETHER
;
7714 dl_type
= match
->flow
.dl_type
;
7715 } else if (wc
.masks
.packet_type
== OVS_BE32_MAX
&&
7716 pt_ns(match
->flow
.packet_type
) == OFPHTN_ETHERTYPE
) {
7718 dl_type
= pt_ns_type_be(match
->flow
.packet_type
);
7723 if (dl_type
== htons(ETH_TYPE_IP
)) {
7724 may_match
|= MAY_NW_PROTO
| MAY_IPVx
| MAY_NW_ADDR
;
7725 if (match
->flow
.nw_proto
== IPPROTO_TCP
||
7726 match
->flow
.nw_proto
== IPPROTO_UDP
||
7727 match
->flow
.nw_proto
== IPPROTO_SCTP
||
7728 match
->flow
.nw_proto
== IPPROTO_ICMP
) {
7729 may_match
|= MAY_TP_ADDR
;
7731 } else if (dl_type
== htons(ETH_TYPE_IPV6
)) {
7732 may_match
|= MAY_NW_PROTO
| MAY_IPVx
| MAY_IPV6
;
7733 if (match
->flow
.nw_proto
== IPPROTO_TCP
||
7734 match
->flow
.nw_proto
== IPPROTO_UDP
||
7735 match
->flow
.nw_proto
== IPPROTO_SCTP
) {
7736 may_match
|= MAY_TP_ADDR
;
7737 } else if (match
->flow
.nw_proto
== IPPROTO_ICMPV6
) {
7738 may_match
|= MAY_TP_ADDR
;
7739 if (match
->flow
.tp_src
== htons(ND_NEIGHBOR_SOLICIT
)) {
7740 may_match
|= MAY_ND_TARGET
| MAY_ARP_SHA
;
7741 } else if (match
->flow
.tp_src
== htons(ND_NEIGHBOR_ADVERT
)) {
7742 may_match
|= MAY_ND_TARGET
| MAY_ARP_THA
;
7745 } else if (dl_type
== htons(ETH_TYPE_ARP
) ||
7746 dl_type
== htons(ETH_TYPE_RARP
)) {
7747 may_match
|= MAY_NW_PROTO
| MAY_NW_ADDR
| MAY_ARP_SHA
| MAY_ARP_THA
;
7748 } else if (eth_type_mpls(dl_type
)) {
7749 may_match
|= MAY_MPLS
;
7752 /* Clear the fields that may not be matched. */
7753 if (!(may_match
& MAY_ETHER
)) {
7754 wc
.masks
.dl_src
= wc
.masks
.dl_dst
= eth_addr_zero
;
7756 if (!(may_match
& MAY_NW_ADDR
)) {
7757 wc
.masks
.nw_src
= wc
.masks
.nw_dst
= htonl(0);
7759 if (!(may_match
& MAY_TP_ADDR
)) {
7760 wc
.masks
.tp_src
= wc
.masks
.tp_dst
= htons(0);
7762 if (!(may_match
& MAY_NW_PROTO
)) {
7763 wc
.masks
.nw_proto
= 0;
7765 if (!(may_match
& MAY_IPVx
)) {
7766 wc
.masks
.nw_tos
= 0;
7767 wc
.masks
.nw_ttl
= 0;
7769 if (!(may_match
& MAY_ARP_SHA
)) {
7770 WC_UNMASK_FIELD(&wc
, arp_sha
);
7772 if (!(may_match
& MAY_ARP_THA
)) {
7773 WC_UNMASK_FIELD(&wc
, arp_tha
);
7775 if (!(may_match
& MAY_IPV6
)) {
7776 wc
.masks
.ipv6_src
= wc
.masks
.ipv6_dst
= in6addr_any
;
7777 wc
.masks
.ipv6_label
= htonl(0);
7779 if (!(may_match
& MAY_ND_TARGET
)) {
7780 wc
.masks
.nd_target
= in6addr_any
;
7782 if (!(may_match
& MAY_MPLS
)) {
7783 memset(wc
.masks
.mpls_lse
, 0, sizeof wc
.masks
.mpls_lse
);
7786 /* Log any changes. */
7787 if (!flow_wildcards_equal(&wc
, &match
->wc
)) {
7788 bool log
= may_log
&& !VLOG_DROP_INFO(&bad_ofmsg_rl
);
7790 ? match_to_string(match
, NULL
, OFP_DEFAULT_PRIORITY
)
7794 match_zero_wildcarded_fields(match
);
7797 char *post
= match_to_string(match
, NULL
, OFP_DEFAULT_PRIORITY
);
7798 VLOG_INFO("normalization changed ofp_match, details:");
7799 VLOG_INFO(" pre: %s", pre
);
7800 VLOG_INFO("post: %s", post
);
7807 /* "Normalizes" the wildcards in 'match'. That means:
7809 * 1. If the type of level N is known, then only the valid fields for that
7810 * level may be specified. For example, ARP does not have a TOS field,
7811 * so nw_tos must be wildcarded if 'match' specifies an ARP flow.
7812 * Similarly, IPv4 does not have any IPv6 addresses, so ipv6_src and
7813 * ipv6_dst (and other fields) must be wildcarded if 'match' specifies an
7816 * 2. If the type of level N is not known (or not understood by Open
7817 * vSwitch), then no fields at all for that level may be specified. For
7818 * example, Open vSwitch does not understand SCTP, an L4 protocol, so the
7819 * L4 fields tp_src and tp_dst must be wildcarded if 'match' specifies an
7822 * If this function changes 'match', it logs a rate-limited informational
7825 ofputil_normalize_match(struct match
*match
)
7827 ofputil_normalize_match__(match
, true);
7830 /* Same as ofputil_normalize_match() without the logging. Thus, this function
7831 * is suitable for a program's internal use, whereas ofputil_normalize_match()
7832 * sense for use on flows received from elsewhere (so that a bug in the program
7833 * that sent them can be reported and corrected). */
7835 ofputil_normalize_match_quiet(struct match
*match
)
7837 ofputil_normalize_match__(match
, false);
7841 parse_value(const char *s
, const char *delimiters
)
7845 /* Iterate until we reach a delimiter.
7847 * strchr(s, '\0') returns s+strlen(s), so this test handles the null
7848 * terminator at the end of 's'. */
7849 while (!strchr(delimiters
, s
[n
])) {
7864 } while (level
> 0);
7872 /* Parses a key or a key-value pair from '*stringp'.
7874 * On success: Stores the key into '*keyp'. Stores the value, if present, into
7875 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
7876 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
7877 * are substrings of '*stringp' created by replacing some of its bytes by null
7878 * terminators. Returns true.
7880 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
7881 * NULL and returns false. */
7883 ofputil_parse_key_value(char **stringp
, char **keyp
, char **valuep
)
7885 /* Skip white space and delimiters. If that brings us to the end of the
7886 * input string, we are done and there are no more key-value pairs. */
7887 *stringp
+= strspn(*stringp
, ", \t\r\n");
7888 if (**stringp
== '\0') {
7889 *keyp
= *valuep
= NULL
;
7893 /* Extract the key and the delimiter that ends the key-value pair or begins
7894 * the value. Advance the input position past the key and delimiter. */
7895 char *key
= *stringp
;
7896 size_t key_len
= strcspn(key
, ":=(, \t\r\n");
7897 char key_delim
= key
[key_len
];
7898 key
[key_len
] = '\0';
7899 *stringp
+= key_len
+ (key_delim
!= '\0');
7901 /* Figure out what delimiter ends the value:
7903 * - If key_delim is ":" or "=", the value extends until white space
7906 * - If key_delim is "(", the value extends until ")".
7908 * If there is no value, we are done. */
7909 const char *value_delims
;
7910 if (key_delim
== ':' || key_delim
== '=') {
7911 value_delims
= ", \t\r\n";
7912 } else if (key_delim
== '(') {
7916 *valuep
= key
+ key_len
; /* Empty string. */
7920 /* Extract the value. Advance the input position past the value and
7922 char *value
= *stringp
;
7923 size_t value_len
= parse_value(value
, value_delims
);
7924 char value_delim
= value
[value_len
];
7925 value
[value_len
] = '\0';
7926 *stringp
+= value_len
+ (value_delim
!= '\0');
7933 /* Encode a dump ports request for 'port', the encoded message
7934 * will be for OpenFlow version 'ofp_version'. Returns message
7935 * as a struct ofpbuf. Returns encoded message on success, NULL on error */
7937 ofputil_encode_dump_ports_request(enum ofp_version ofp_version
, ofp_port_t port
)
7939 struct ofpbuf
*request
;
7941 switch (ofp_version
) {
7942 case OFP10_VERSION
: {
7943 struct ofp10_port_stats_request
*req
;
7944 request
= ofpraw_alloc(OFPRAW_OFPST10_PORT_REQUEST
, ofp_version
, 0);
7945 req
= ofpbuf_put_zeros(request
, sizeof *req
);
7946 req
->port_no
= htons(ofp_to_u16(port
));
7954 case OFP16_VERSION
: {
7955 struct ofp11_port_stats_request
*req
;
7956 request
= ofpraw_alloc(OFPRAW_OFPST11_PORT_REQUEST
, ofp_version
, 0);
7957 req
= ofpbuf_put_zeros(request
, sizeof *req
);
7958 req
->port_no
= ofputil_port_to_ofp11(port
);
7969 ofputil_port_stats_to_ofp10(const struct ofputil_port_stats
*ops
,
7970 struct ofp10_port_stats
*ps10
)
7972 ps10
->port_no
= htons(ofp_to_u16(ops
->port_no
));
7973 memset(ps10
->pad
, 0, sizeof ps10
->pad
);
7974 put_32aligned_be64(&ps10
->rx_packets
, htonll(ops
->stats
.rx_packets
));
7975 put_32aligned_be64(&ps10
->tx_packets
, htonll(ops
->stats
.tx_packets
));
7976 put_32aligned_be64(&ps10
->rx_bytes
, htonll(ops
->stats
.rx_bytes
));
7977 put_32aligned_be64(&ps10
->tx_bytes
, htonll(ops
->stats
.tx_bytes
));
7978 put_32aligned_be64(&ps10
->rx_dropped
, htonll(ops
->stats
.rx_dropped
));
7979 put_32aligned_be64(&ps10
->tx_dropped
, htonll(ops
->stats
.tx_dropped
));
7980 put_32aligned_be64(&ps10
->rx_errors
, htonll(ops
->stats
.rx_errors
));
7981 put_32aligned_be64(&ps10
->tx_errors
, htonll(ops
->stats
.tx_errors
));
7982 put_32aligned_be64(&ps10
->rx_frame_err
, htonll(ops
->stats
.rx_frame_errors
));
7983 put_32aligned_be64(&ps10
->rx_over_err
, htonll(ops
->stats
.rx_over_errors
));
7984 put_32aligned_be64(&ps10
->rx_crc_err
, htonll(ops
->stats
.rx_crc_errors
));
7985 put_32aligned_be64(&ps10
->collisions
, htonll(ops
->stats
.collisions
));
7989 ofputil_port_stats_to_ofp11(const struct ofputil_port_stats
*ops
,
7990 struct ofp11_port_stats
*ps11
)
7992 ps11
->port_no
= ofputil_port_to_ofp11(ops
->port_no
);
7993 memset(ps11
->pad
, 0, sizeof ps11
->pad
);
7994 ps11
->rx_packets
= htonll(ops
->stats
.rx_packets
);
7995 ps11
->tx_packets
= htonll(ops
->stats
.tx_packets
);
7996 ps11
->rx_bytes
= htonll(ops
->stats
.rx_bytes
);
7997 ps11
->tx_bytes
= htonll(ops
->stats
.tx_bytes
);
7998 ps11
->rx_dropped
= htonll(ops
->stats
.rx_dropped
);
7999 ps11
->tx_dropped
= htonll(ops
->stats
.tx_dropped
);
8000 ps11
->rx_errors
= htonll(ops
->stats
.rx_errors
);
8001 ps11
->tx_errors
= htonll(ops
->stats
.tx_errors
);
8002 ps11
->rx_frame_err
= htonll(ops
->stats
.rx_frame_errors
);
8003 ps11
->rx_over_err
= htonll(ops
->stats
.rx_over_errors
);
8004 ps11
->rx_crc_err
= htonll(ops
->stats
.rx_crc_errors
);
8005 ps11
->collisions
= htonll(ops
->stats
.collisions
);
8009 ofputil_port_stats_to_ofp13(const struct ofputil_port_stats
*ops
,
8010 struct ofp13_port_stats
*ps13
)
8012 ofputil_port_stats_to_ofp11(ops
, &ps13
->ps
);
8013 ps13
->duration_sec
= htonl(ops
->duration_sec
);
8014 ps13
->duration_nsec
= htonl(ops
->duration_nsec
);
8018 ofputil_append_ofp14_port_stats(const struct ofputil_port_stats
*ops
,
8019 struct ovs_list
*replies
)
8021 struct ofp14_port_stats_prop_ethernet
*eth
;
8022 struct intel_port_stats_rfc2819
*stats_rfc2819
;
8023 struct ofp14_port_stats
*ps14
;
8024 struct ofpbuf
*reply
;
8026 reply
= ofpmp_reserve(replies
, sizeof *ps14
+ sizeof *eth
+
8027 sizeof *stats_rfc2819
);
8029 ps14
= ofpbuf_put_uninit(reply
, sizeof *ps14
);
8030 ps14
->length
= htons(sizeof *ps14
+ sizeof *eth
+
8031 sizeof *stats_rfc2819
);
8032 memset(ps14
->pad
, 0, sizeof ps14
->pad
);
8033 ps14
->port_no
= ofputil_port_to_ofp11(ops
->port_no
);
8034 ps14
->duration_sec
= htonl(ops
->duration_sec
);
8035 ps14
->duration_nsec
= htonl(ops
->duration_nsec
);
8036 ps14
->rx_packets
= htonll(ops
->stats
.rx_packets
);
8037 ps14
->tx_packets
= htonll(ops
->stats
.tx_packets
);
8038 ps14
->rx_bytes
= htonll(ops
->stats
.rx_bytes
);
8039 ps14
->tx_bytes
= htonll(ops
->stats
.tx_bytes
);
8040 ps14
->rx_dropped
= htonll(ops
->stats
.rx_dropped
);
8041 ps14
->tx_dropped
= htonll(ops
->stats
.tx_dropped
);
8042 ps14
->rx_errors
= htonll(ops
->stats
.rx_errors
);
8043 ps14
->tx_errors
= htonll(ops
->stats
.tx_errors
);
8045 eth
= ofpprop_put_zeros(reply
, OFPPSPT14_ETHERNET
, sizeof *eth
);
8046 eth
->rx_frame_err
= htonll(ops
->stats
.rx_frame_errors
);
8047 eth
->rx_over_err
= htonll(ops
->stats
.rx_over_errors
);
8048 eth
->rx_crc_err
= htonll(ops
->stats
.rx_crc_errors
);
8049 eth
->collisions
= htonll(ops
->stats
.collisions
);
8051 uint64_t prop_type
= OFPPROP_EXP(INTEL_VENDOR_ID
,
8052 INTEL_PORT_STATS_RFC2819
);
8054 stats_rfc2819
= ofpprop_put_zeros(reply
, prop_type
,
8055 sizeof *stats_rfc2819
);
8057 memset(stats_rfc2819
->pad
, 0, sizeof stats_rfc2819
->pad
);
8058 stats_rfc2819
->rx_1_to_64_packets
= htonll(ops
->stats
.rx_1_to_64_packets
);
8059 stats_rfc2819
->rx_65_to_127_packets
=
8060 htonll(ops
->stats
.rx_65_to_127_packets
);
8061 stats_rfc2819
->rx_128_to_255_packets
=
8062 htonll(ops
->stats
.rx_128_to_255_packets
);
8063 stats_rfc2819
->rx_256_to_511_packets
=
8064 htonll(ops
->stats
.rx_256_to_511_packets
);
8065 stats_rfc2819
->rx_512_to_1023_packets
=
8066 htonll(ops
->stats
.rx_512_to_1023_packets
);
8067 stats_rfc2819
->rx_1024_to_1522_packets
=
8068 htonll(ops
->stats
.rx_1024_to_1522_packets
);
8069 stats_rfc2819
->rx_1523_to_max_packets
=
8070 htonll(ops
->stats
.rx_1523_to_max_packets
);
8072 stats_rfc2819
->tx_1_to_64_packets
= htonll(ops
->stats
.tx_1_to_64_packets
);
8073 stats_rfc2819
->tx_65_to_127_packets
=
8074 htonll(ops
->stats
.tx_65_to_127_packets
);
8075 stats_rfc2819
->tx_128_to_255_packets
=
8076 htonll(ops
->stats
.tx_128_to_255_packets
);
8077 stats_rfc2819
->tx_256_to_511_packets
=
8078 htonll(ops
->stats
.tx_256_to_511_packets
);
8079 stats_rfc2819
->tx_512_to_1023_packets
=
8080 htonll(ops
->stats
.tx_512_to_1023_packets
);
8081 stats_rfc2819
->tx_1024_to_1522_packets
=
8082 htonll(ops
->stats
.tx_1024_to_1522_packets
);
8083 stats_rfc2819
->tx_1523_to_max_packets
=
8084 htonll(ops
->stats
.tx_1523_to_max_packets
);
8086 stats_rfc2819
->tx_multicast_packets
=
8087 htonll(ops
->stats
.tx_multicast_packets
);
8088 stats_rfc2819
->rx_broadcast_packets
=
8089 htonll(ops
->stats
.rx_broadcast_packets
);
8090 stats_rfc2819
->tx_broadcast_packets
=
8091 htonll(ops
->stats
.tx_broadcast_packets
);
8092 stats_rfc2819
->rx_undersized_errors
=
8093 htonll(ops
->stats
.rx_undersized_errors
);
8094 stats_rfc2819
->rx_oversize_errors
=
8095 htonll(ops
->stats
.rx_oversize_errors
);
8096 stats_rfc2819
->rx_fragmented_errors
=
8097 htonll(ops
->stats
.rx_fragmented_errors
);
8098 stats_rfc2819
->rx_jabber_errors
=
8099 htonll(ops
->stats
.rx_jabber_errors
);
8102 /* Encode a ports stat for 'ops' and append it to 'replies'. */
8104 ofputil_append_port_stat(struct ovs_list
*replies
,
8105 const struct ofputil_port_stats
*ops
)
8107 switch (ofpmp_version(replies
)) {
8108 case OFP13_VERSION
: {
8109 struct ofp13_port_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
8110 ofputil_port_stats_to_ofp13(ops
, reply
);
8114 case OFP11_VERSION
: {
8115 struct ofp11_port_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
8116 ofputil_port_stats_to_ofp11(ops
, reply
);
8120 case OFP10_VERSION
: {
8121 struct ofp10_port_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
8122 ofputil_port_stats_to_ofp10(ops
, reply
);
8129 ofputil_append_ofp14_port_stats(ops
, replies
);
8138 ofputil_port_stats_from_ofp10(struct ofputil_port_stats
*ops
,
8139 const struct ofp10_port_stats
*ps10
)
8142 ops
->port_no
= u16_to_ofp(ntohs(ps10
->port_no
));
8143 ops
->stats
.rx_packets
= ntohll(get_32aligned_be64(&ps10
->rx_packets
));
8144 ops
->stats
.tx_packets
= ntohll(get_32aligned_be64(&ps10
->tx_packets
));
8145 ops
->stats
.rx_bytes
= ntohll(get_32aligned_be64(&ps10
->rx_bytes
));
8146 ops
->stats
.tx_bytes
= ntohll(get_32aligned_be64(&ps10
->tx_bytes
));
8147 ops
->stats
.rx_dropped
= ntohll(get_32aligned_be64(&ps10
->rx_dropped
));
8148 ops
->stats
.tx_dropped
= ntohll(get_32aligned_be64(&ps10
->tx_dropped
));
8149 ops
->stats
.rx_errors
= ntohll(get_32aligned_be64(&ps10
->rx_errors
));
8150 ops
->stats
.tx_errors
= ntohll(get_32aligned_be64(&ps10
->tx_errors
));
8151 ops
->stats
.rx_frame_errors
=
8152 ntohll(get_32aligned_be64(&ps10
->rx_frame_err
));
8153 ops
->stats
.rx_over_errors
= ntohll(get_32aligned_be64(&ps10
->rx_over_err
));
8154 ops
->stats
.rx_crc_errors
= ntohll(get_32aligned_be64(&ps10
->rx_crc_err
));
8155 ops
->stats
.collisions
= ntohll(get_32aligned_be64(&ps10
->collisions
));
8156 ops
->duration_sec
= ops
->duration_nsec
= UINT32_MAX
;
8162 ofputil_port_stats_from_ofp11(struct ofputil_port_stats
*ops
,
8163 const struct ofp11_port_stats
*ps11
)
8167 error
= ofputil_port_from_ofp11(ps11
->port_no
, &ops
->port_no
);
8172 ops
->stats
.rx_packets
= ntohll(ps11
->rx_packets
);
8173 ops
->stats
.tx_packets
= ntohll(ps11
->tx_packets
);
8174 ops
->stats
.rx_bytes
= ntohll(ps11
->rx_bytes
);
8175 ops
->stats
.tx_bytes
= ntohll(ps11
->tx_bytes
);
8176 ops
->stats
.rx_dropped
= ntohll(ps11
->rx_dropped
);
8177 ops
->stats
.tx_dropped
= ntohll(ps11
->tx_dropped
);
8178 ops
->stats
.rx_errors
= ntohll(ps11
->rx_errors
);
8179 ops
->stats
.tx_errors
= ntohll(ps11
->tx_errors
);
8180 ops
->stats
.rx_frame_errors
= ntohll(ps11
->rx_frame_err
);
8181 ops
->stats
.rx_over_errors
= ntohll(ps11
->rx_over_err
);
8182 ops
->stats
.rx_crc_errors
= ntohll(ps11
->rx_crc_err
);
8183 ops
->stats
.collisions
= ntohll(ps11
->collisions
);
8184 ops
->duration_sec
= ops
->duration_nsec
= UINT32_MAX
;
8190 ofputil_port_stats_from_ofp13(struct ofputil_port_stats
*ops
,
8191 const struct ofp13_port_stats
*ps13
)
8193 enum ofperr error
= ofputil_port_stats_from_ofp11(ops
, &ps13
->ps
);
8195 ops
->duration_sec
= ntohl(ps13
->duration_sec
);
8196 ops
->duration_nsec
= ntohl(ps13
->duration_nsec
);
8202 parse_ofp14_port_stats_ethernet_property(const struct ofpbuf
*payload
,
8203 struct ofputil_port_stats
*ops
)
8205 const struct ofp14_port_stats_prop_ethernet
*eth
= payload
->data
;
8207 if (payload
->size
!= sizeof *eth
) {
8208 return OFPERR_OFPBPC_BAD_LEN
;
8211 ops
->stats
.rx_frame_errors
= ntohll(eth
->rx_frame_err
);
8212 ops
->stats
.rx_over_errors
= ntohll(eth
->rx_over_err
);
8213 ops
->stats
.rx_crc_errors
= ntohll(eth
->rx_crc_err
);
8214 ops
->stats
.collisions
= ntohll(eth
->collisions
);
8220 parse_intel_port_stats_rfc2819_property(const struct ofpbuf
*payload
,
8221 struct ofputil_port_stats
*ops
)
8223 const struct intel_port_stats_rfc2819
*rfc2819
= payload
->data
;
8225 if (payload
->size
!= sizeof *rfc2819
) {
8226 return OFPERR_OFPBPC_BAD_LEN
;
8228 ops
->stats
.rx_1_to_64_packets
= ntohll(rfc2819
->rx_1_to_64_packets
);
8229 ops
->stats
.rx_65_to_127_packets
= ntohll(rfc2819
->rx_65_to_127_packets
);
8230 ops
->stats
.rx_128_to_255_packets
= ntohll(rfc2819
->rx_128_to_255_packets
);
8231 ops
->stats
.rx_256_to_511_packets
= ntohll(rfc2819
->rx_256_to_511_packets
);
8232 ops
->stats
.rx_512_to_1023_packets
=
8233 ntohll(rfc2819
->rx_512_to_1023_packets
);
8234 ops
->stats
.rx_1024_to_1522_packets
=
8235 ntohll(rfc2819
->rx_1024_to_1522_packets
);
8236 ops
->stats
.rx_1523_to_max_packets
=
8237 ntohll(rfc2819
->rx_1523_to_max_packets
);
8239 ops
->stats
.tx_1_to_64_packets
= ntohll(rfc2819
->tx_1_to_64_packets
);
8240 ops
->stats
.tx_65_to_127_packets
= ntohll(rfc2819
->tx_65_to_127_packets
);
8241 ops
->stats
.tx_128_to_255_packets
= ntohll(rfc2819
->tx_128_to_255_packets
);
8242 ops
->stats
.tx_256_to_511_packets
= ntohll(rfc2819
->tx_256_to_511_packets
);
8243 ops
->stats
.tx_512_to_1023_packets
=
8244 ntohll(rfc2819
->tx_512_to_1023_packets
);
8245 ops
->stats
.tx_1024_to_1522_packets
=
8246 ntohll(rfc2819
->tx_1024_to_1522_packets
);
8247 ops
->stats
.tx_1523_to_max_packets
=
8248 ntohll(rfc2819
->tx_1523_to_max_packets
);
8250 ops
->stats
.tx_multicast_packets
= ntohll(rfc2819
->tx_multicast_packets
);
8251 ops
->stats
.rx_broadcast_packets
= ntohll(rfc2819
->rx_broadcast_packets
);
8252 ops
->stats
.tx_broadcast_packets
= ntohll(rfc2819
->tx_broadcast_packets
);
8253 ops
->stats
.rx_undersized_errors
= ntohll(rfc2819
->rx_undersized_errors
);
8255 ops
->stats
.rx_oversize_errors
= ntohll(rfc2819
->rx_oversize_errors
);
8256 ops
->stats
.rx_fragmented_errors
= ntohll(rfc2819
->rx_fragmented_errors
);
8257 ops
->stats
.rx_jabber_errors
= ntohll(rfc2819
->rx_jabber_errors
);
8263 parse_intel_port_stats_property(const struct ofpbuf
*payload
,
8265 struct ofputil_port_stats
*ops
)
8270 case INTEL_PORT_STATS_RFC2819
:
8271 error
= parse_intel_port_stats_rfc2819_property(payload
, ops
);
8274 error
= OFPERR_OFPBPC_BAD_EXP_TYPE
;
8282 ofputil_pull_ofp14_port_stats(struct ofputil_port_stats
*ops
,
8285 const struct ofp14_port_stats
*ps14
= ofpbuf_try_pull(msg
, sizeof *ps14
);
8287 return OFPERR_OFPBRC_BAD_LEN
;
8290 size_t len
= ntohs(ps14
->length
);
8291 if (len
< sizeof *ps14
|| len
- sizeof *ps14
> msg
->size
) {
8292 return OFPERR_OFPBRC_BAD_LEN
;
8294 len
-= sizeof *ps14
;
8296 enum ofperr error
= ofputil_port_from_ofp11(ps14
->port_no
, &ops
->port_no
);
8301 ops
->duration_sec
= ntohl(ps14
->duration_sec
);
8302 ops
->duration_nsec
= ntohl(ps14
->duration_nsec
);
8303 ops
->stats
.rx_packets
= ntohll(ps14
->rx_packets
);
8304 ops
->stats
.tx_packets
= ntohll(ps14
->tx_packets
);
8305 ops
->stats
.rx_bytes
= ntohll(ps14
->rx_bytes
);
8306 ops
->stats
.tx_bytes
= ntohll(ps14
->tx_bytes
);
8307 ops
->stats
.rx_dropped
= ntohll(ps14
->rx_dropped
);
8308 ops
->stats
.tx_dropped
= ntohll(ps14
->tx_dropped
);
8309 ops
->stats
.rx_errors
= ntohll(ps14
->rx_errors
);
8310 ops
->stats
.tx_errors
= ntohll(ps14
->tx_errors
);
8313 struct ofpbuf properties
= ofpbuf_const_initializer(ofpbuf_pull(msg
, len
),
8315 while (properties
.size
> 0) {
8316 struct ofpbuf payload
;
8320 error
= ofpprop_pull(&properties
, &payload
, &type
);
8325 case OFPPSPT14_ETHERNET
:
8326 error
= parse_ofp14_port_stats_ethernet_property(&payload
, ops
);
8328 case OFPPROP_EXP(INTEL_VENDOR_ID
, INTEL_PORT_STATS_RFC2819
):
8329 error
= parse_intel_port_stats_property(&payload
,
8330 INTEL_PORT_STATS_RFC2819
,
8334 error
= OFPPROP_UNKNOWN(true, "port stats", type
);
8346 /* Returns the number of port stats elements in OFPTYPE_PORT_STATS_REPLY
8349 ofputil_count_port_stats(const struct ofp_header
*oh
)
8351 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
8352 ofpraw_pull_assert(&b
);
8354 for (size_t n
= 0; ; n
++) {
8355 struct ofputil_port_stats ps
;
8356 if (ofputil_decode_port_stats(&ps
, &b
)) {
8362 /* Converts an OFPST_PORT_STATS reply in 'msg' into an abstract
8363 * ofputil_port_stats in 'ps'.
8365 * Multiple OFPST_PORT_STATS replies can be packed into a single OpenFlow
8366 * message. Calling this function multiple times for a single 'msg' iterates
8367 * through the replies. The caller must initially leave 'msg''s layer pointers
8368 * null and not modify them between calls.
8370 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8371 * otherwise a positive errno value. */
8373 ofputil_decode_port_stats(struct ofputil_port_stats
*ps
, struct ofpbuf
*msg
)
8378 memset(&(ps
->stats
), 0xFF, sizeof (ps
->stats
));
8380 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
8381 : ofpraw_pull(&raw
, msg
));
8388 } else if (raw
== OFPRAW_OFPST14_PORT_REPLY
) {
8389 return ofputil_pull_ofp14_port_stats(ps
, msg
);
8390 } else if (raw
== OFPRAW_OFPST13_PORT_REPLY
) {
8391 const struct ofp13_port_stats
*ps13
;
8392 ps13
= ofpbuf_try_pull(msg
, sizeof *ps13
);
8396 return ofputil_port_stats_from_ofp13(ps
, ps13
);
8397 } else if (raw
== OFPRAW_OFPST11_PORT_REPLY
) {
8398 const struct ofp11_port_stats
*ps11
;
8400 ps11
= ofpbuf_try_pull(msg
, sizeof *ps11
);
8404 return ofputil_port_stats_from_ofp11(ps
, ps11
);
8405 } else if (raw
== OFPRAW_OFPST10_PORT_REPLY
) {
8406 const struct ofp10_port_stats
*ps10
;
8408 ps10
= ofpbuf_try_pull(msg
, sizeof *ps10
);
8412 return ofputil_port_stats_from_ofp10(ps
, ps10
);
8418 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_PORT reply has %"PRIu32
" leftover "
8419 "bytes at end", msg
->size
);
8420 return OFPERR_OFPBRC_BAD_LEN
;
8423 /* Parse a port status request message into a 16 bit OpenFlow 1.0
8424 * port number and stores the latter in '*ofp10_port'.
8425 * Returns 0 if successful, otherwise an OFPERR_* number. */
8427 ofputil_decode_port_stats_request(const struct ofp_header
*request
,
8428 ofp_port_t
*ofp10_port
)
8430 switch ((enum ofp_version
)request
->version
) {
8436 case OFP11_VERSION
: {
8437 const struct ofp11_port_stats_request
*psr11
= ofpmsg_body(request
);
8438 return ofputil_port_from_ofp11(psr11
->port_no
, ofp10_port
);
8441 case OFP10_VERSION
: {
8442 const struct ofp10_port_stats_request
*psr10
= ofpmsg_body(request
);
8443 *ofp10_port
= u16_to_ofp(ntohs(psr10
->port_no
));
8453 ofputil_ipfix_stats_to_reply(const struct ofputil_ipfix_stats
*ois
,
8454 struct nx_ipfix_stats_reply
*reply
)
8456 reply
->collector_set_id
= htonl(ois
->collector_set_id
);
8457 reply
->total_flows
= htonll(ois
->total_flows
);
8458 reply
->current_flows
= htonll(ois
->current_flows
);
8459 reply
->pkts
= htonll(ois
->pkts
);
8460 reply
->ipv4_pkts
= htonll(ois
->ipv4_pkts
);
8461 reply
->ipv6_pkts
= htonll(ois
->ipv6_pkts
);
8462 reply
->error_pkts
= htonll(ois
->error_pkts
);
8463 reply
->ipv4_error_pkts
= htonll(ois
->ipv4_error_pkts
);
8464 reply
->ipv6_error_pkts
= htonll(ois
->ipv6_error_pkts
);
8465 reply
->tx_pkts
= htonll(ois
->tx_pkts
);
8466 reply
->tx_errors
= htonll(ois
->tx_errors
);
8467 memset(reply
->pad
, 0, sizeof reply
->pad
);
8470 /* Encode a ipfix stat for 'ois' and append it to 'replies'. */
8472 ofputil_append_ipfix_stat(struct ovs_list
*replies
,
8473 const struct ofputil_ipfix_stats
*ois
)
8475 struct nx_ipfix_stats_reply
*reply
= ofpmp_append(replies
, sizeof *reply
);
8476 ofputil_ipfix_stats_to_reply(ois
, reply
);
8480 ofputil_ipfix_stats_from_nx(struct ofputil_ipfix_stats
*is
,
8481 const struct nx_ipfix_stats_reply
*reply
)
8483 is
->collector_set_id
= ntohl(reply
->collector_set_id
);
8484 is
->total_flows
= ntohll(reply
->total_flows
);
8485 is
->current_flows
= ntohll(reply
->current_flows
);
8486 is
->pkts
= ntohll(reply
->pkts
);
8487 is
->ipv4_pkts
= ntohll(reply
->ipv4_pkts
);
8488 is
->ipv6_pkts
= ntohll(reply
->ipv6_pkts
);
8489 is
->error_pkts
= ntohll(reply
->error_pkts
);
8490 is
->ipv4_error_pkts
= ntohll(reply
->ipv4_error_pkts
);
8491 is
->ipv6_error_pkts
= ntohll(reply
->ipv6_error_pkts
);
8492 is
->tx_pkts
= ntohll(reply
->tx_pkts
);
8493 is
->tx_errors
= ntohll(reply
->tx_errors
);
8499 ofputil_pull_ipfix_stats(struct ofputil_ipfix_stats
*is
, struct ofpbuf
*msg
)
8504 memset(is
, 0xFF, sizeof (*is
));
8506 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
8507 : ofpraw_pull(&raw
, msg
));
8514 } else if (raw
== OFPRAW_NXST_IPFIX_BRIDGE_REPLY
||
8515 raw
== OFPRAW_NXST_IPFIX_FLOW_REPLY
) {
8516 struct nx_ipfix_stats_reply
*reply
;
8518 reply
= ofpbuf_try_pull(msg
, sizeof *reply
);
8519 return ofputil_ipfix_stats_from_nx(is
, reply
);
8526 /* Returns the number of ipfix stats elements in
8527 * OFPTYPE_IPFIX_BRIDGE_STATS_REPLY or OFPTYPE_IPFIX_FLOW_STATS_REPLY
8530 ofputil_count_ipfix_stats(const struct ofp_header
*oh
)
8532 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
8533 ofpraw_pull_assert(&b
);
8535 return b
.size
/ sizeof(struct ofputil_ipfix_stats
);
8538 /* Frees all of the "struct ofputil_bucket"s in the 'buckets' list. */
8540 ofputil_bucket_list_destroy(struct ovs_list
*buckets
)
8542 struct ofputil_bucket
*bucket
;
8544 LIST_FOR_EACH_POP (bucket
, list_node
, buckets
) {
8545 free(bucket
->ofpacts
);
8550 /* Clones 'bucket' and its ofpacts data */
8551 static struct ofputil_bucket
*
8552 ofputil_bucket_clone_data(const struct ofputil_bucket
*bucket
)
8554 struct ofputil_bucket
*new;
8556 new = xmemdup(bucket
, sizeof *bucket
);
8557 new->ofpacts
= xmemdup(bucket
->ofpacts
, bucket
->ofpacts_len
);
8562 /* Clones each of the buckets in the list 'src' appending them
8563 * in turn to 'dest' which should be an initialised list.
8564 * An exception is that if the pointer value of a bucket in 'src'
8565 * matches 'skip' then it is not cloned or appended to 'dest'.
8566 * This allows all of 'src' or 'all of 'src' except 'skip' to
8567 * be cloned and appended to 'dest'. */
8569 ofputil_bucket_clone_list(struct ovs_list
*dest
, const struct ovs_list
*src
,
8570 const struct ofputil_bucket
*skip
)
8572 struct ofputil_bucket
*bucket
;
8574 LIST_FOR_EACH (bucket
, list_node
, src
) {
8575 struct ofputil_bucket
*new_bucket
;
8577 if (bucket
== skip
) {
8581 new_bucket
= ofputil_bucket_clone_data(bucket
);
8582 ovs_list_push_back(dest
, &new_bucket
->list_node
);
8586 /* Find a bucket in the list 'buckets' whose bucket id is 'bucket_id'
8587 * Returns the first bucket found or NULL if no buckets are found. */
8588 struct ofputil_bucket
*
8589 ofputil_bucket_find(const struct ovs_list
*buckets
, uint32_t bucket_id
)
8591 struct ofputil_bucket
*bucket
;
8593 if (bucket_id
> OFPG15_BUCKET_MAX
) {
8597 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
8598 if (bucket
->bucket_id
== bucket_id
) {
8606 /* Returns true if more than one bucket in the list 'buckets'
8607 * have the same bucket id. Returns false otherwise. */
8609 ofputil_bucket_check_duplicate_id(const struct ovs_list
*buckets
)
8611 struct ofputil_bucket
*i
, *j
;
8613 LIST_FOR_EACH (i
, list_node
, buckets
) {
8614 LIST_FOR_EACH_REVERSE (j
, list_node
, buckets
) {
8618 if (i
->bucket_id
== j
->bucket_id
) {
8627 /* Returns the bucket at the front of the list 'buckets'.
8628 * Undefined if 'buckets is empty. */
8629 struct ofputil_bucket
*
8630 ofputil_bucket_list_front(const struct ovs_list
*buckets
)
8632 static struct ofputil_bucket
*bucket
;
8634 ASSIGN_CONTAINER(bucket
, ovs_list_front(buckets
), list_node
);
8639 /* Returns the bucket at the back of the list 'buckets'.
8640 * Undefined if 'buckets is empty. */
8641 struct ofputil_bucket
*
8642 ofputil_bucket_list_back(const struct ovs_list
*buckets
)
8644 static struct ofputil_bucket
*bucket
;
8646 ASSIGN_CONTAINER(bucket
, ovs_list_back(buckets
), list_node
);
8651 /* Returns an OpenFlow group stats request for OpenFlow version 'ofp_version',
8652 * that requests stats for group 'group_id'. (Use OFPG_ALL to request stats
8655 * Group statistics include packet and byte counts for each group. */
8657 ofputil_encode_group_stats_request(enum ofp_version ofp_version
,
8660 struct ofpbuf
*request
;
8662 switch (ofp_version
) {
8664 ovs_fatal(0, "dump-group-stats needs OpenFlow 1.1 or later "
8665 "(\'-O OpenFlow11\')");
8671 case OFP16_VERSION
: {
8672 struct ofp11_group_stats_request
*req
;
8673 request
= ofpraw_alloc(OFPRAW_OFPST11_GROUP_REQUEST
, ofp_version
, 0);
8674 req
= ofpbuf_put_zeros(request
, sizeof *req
);
8675 req
->group_id
= htonl(group_id
);
8686 ofputil_uninit_group_desc(struct ofputil_group_desc
*gd
)
8688 ofputil_bucket_list_destroy(&gd
->buckets
);
8689 ofputil_group_properties_destroy(&gd
->props
);
8692 /* Decodes the OpenFlow group description request in 'oh', returning the group
8693 * whose description is requested, or OFPG_ALL if stats for all groups was
8696 ofputil_decode_group_desc_request(const struct ofp_header
*oh
)
8698 struct ofpbuf request
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
8699 enum ofpraw raw
= ofpraw_pull_assert(&request
);
8700 if (raw
== OFPRAW_OFPST11_GROUP_DESC_REQUEST
) {
8702 } else if (raw
== OFPRAW_OFPST15_GROUP_DESC_REQUEST
) {
8703 ovs_be32
*group_id
= ofpbuf_pull(&request
, sizeof *group_id
);
8704 return ntohl(*group_id
);
8710 /* Returns an OpenFlow group description request for OpenFlow version
8711 * 'ofp_version', that requests stats for group 'group_id'. Use OFPG_ALL to
8712 * request stats for all groups (OpenFlow 1.4 and earlier always request all
8715 * Group descriptions include the bucket and action configuration for each
8718 ofputil_encode_group_desc_request(enum ofp_version ofp_version
,
8721 struct ofpbuf
*request
;
8723 switch (ofp_version
) {
8725 ovs_fatal(0, "dump-groups needs OpenFlow 1.1 or later "
8726 "(\'-O OpenFlow11\')");
8731 request
= ofpraw_alloc(OFPRAW_OFPST11_GROUP_DESC_REQUEST
,
8735 case OFP16_VERSION
: {
8736 struct ofp15_group_desc_request
*req
;
8737 request
= ofpraw_alloc(OFPRAW_OFPST15_GROUP_DESC_REQUEST
,
8739 req
= ofpbuf_put_zeros(request
, sizeof *req
);
8740 req
->group_id
= htonl(group_id
);
8751 ofputil_group_bucket_counters_to_ofp11(const struct ofputil_group_stats
*gs
,
8752 struct ofp11_bucket_counter bucket_cnts
[])
8756 for (i
= 0; i
< gs
->n_buckets
; i
++) {
8757 bucket_cnts
[i
].packet_count
= htonll(gs
->bucket_stats
[i
].packet_count
);
8758 bucket_cnts
[i
].byte_count
= htonll(gs
->bucket_stats
[i
].byte_count
);
8763 ofputil_group_stats_to_ofp11(const struct ofputil_group_stats
*gs
,
8764 struct ofp11_group_stats
*gs11
, size_t length
,
8765 struct ofp11_bucket_counter bucket_cnts
[])
8767 memset(gs11
, 0, sizeof *gs11
);
8768 gs11
->length
= htons(length
);
8769 gs11
->group_id
= htonl(gs
->group_id
);
8770 gs11
->ref_count
= htonl(gs
->ref_count
);
8771 gs11
->packet_count
= htonll(gs
->packet_count
);
8772 gs11
->byte_count
= htonll(gs
->byte_count
);
8773 ofputil_group_bucket_counters_to_ofp11(gs
, bucket_cnts
);
8777 ofputil_group_stats_to_ofp13(const struct ofputil_group_stats
*gs
,
8778 struct ofp13_group_stats
*gs13
, size_t length
,
8779 struct ofp11_bucket_counter bucket_cnts
[])
8781 ofputil_group_stats_to_ofp11(gs
, &gs13
->gs
, length
, bucket_cnts
);
8782 gs13
->duration_sec
= htonl(gs
->duration_sec
);
8783 gs13
->duration_nsec
= htonl(gs
->duration_nsec
);
8787 /* Encodes 'gs' properly for the format of the list of group statistics
8788 * replies already begun in 'replies' and appends it to the list. 'replies'
8789 * must have originally been initialized with ofpmp_init(). */
8791 ofputil_append_group_stats(struct ovs_list
*replies
,
8792 const struct ofputil_group_stats
*gs
)
8794 size_t bucket_counter_size
;
8795 struct ofp11_bucket_counter
*bucket_counters
;
8798 bucket_counter_size
= gs
->n_buckets
* sizeof(struct ofp11_bucket_counter
);
8800 switch (ofpmp_version(replies
)) {
8802 case OFP12_VERSION
:{
8803 struct ofp11_group_stats
*gs11
;
8805 length
= sizeof *gs11
+ bucket_counter_size
;
8806 gs11
= ofpmp_append(replies
, length
);
8807 bucket_counters
= (struct ofp11_bucket_counter
*)(gs11
+ 1);
8808 ofputil_group_stats_to_ofp11(gs
, gs11
, length
, bucket_counters
);
8815 case OFP16_VERSION
: {
8816 struct ofp13_group_stats
*gs13
;
8818 length
= sizeof *gs13
+ bucket_counter_size
;
8819 gs13
= ofpmp_append(replies
, length
);
8820 bucket_counters
= (struct ofp11_bucket_counter
*)(gs13
+ 1);
8821 ofputil_group_stats_to_ofp13(gs
, gs13
, length
, bucket_counters
);
8830 /* Returns an OpenFlow group features request for OpenFlow version
8833 ofputil_encode_group_features_request(enum ofp_version ofp_version
)
8835 struct ofpbuf
*request
= NULL
;
8837 switch (ofp_version
) {
8840 ovs_fatal(0, "dump-group-features needs OpenFlow 1.2 or later "
8841 "(\'-O OpenFlow12\')");
8847 request
= ofpraw_alloc(OFPRAW_OFPST12_GROUP_FEATURES_REQUEST
,
8857 /* Returns a OpenFlow message that encodes 'features' properly as a reply to
8858 * group features request 'request'. */
8860 ofputil_encode_group_features_reply(
8861 const struct ofputil_group_features
*features
,
8862 const struct ofp_header
*request
)
8864 struct ofp12_group_features_stats
*ogf
;
8865 struct ofpbuf
*reply
;
8868 reply
= ofpraw_alloc_xid(OFPRAW_OFPST12_GROUP_FEATURES_REPLY
,
8869 request
->version
, request
->xid
, 0);
8870 ogf
= ofpbuf_put_zeros(reply
, sizeof *ogf
);
8871 ogf
->types
= htonl(features
->types
);
8872 ogf
->capabilities
= htonl(features
->capabilities
);
8873 for (i
= 0; i
< OFPGT12_N_TYPES
; i
++) {
8874 ogf
->max_groups
[i
] = htonl(features
->max_groups
[i
]);
8875 ogf
->actions
[i
] = ofpact_bitmap_to_openflow(features
->ofpacts
[i
],
8882 /* Decodes group features reply 'oh' into 'features'. */
8884 ofputil_decode_group_features_reply(const struct ofp_header
*oh
,
8885 struct ofputil_group_features
*features
)
8887 const struct ofp12_group_features_stats
*ogf
= ofpmsg_body(oh
);
8890 features
->types
= ntohl(ogf
->types
);
8891 features
->capabilities
= ntohl(ogf
->capabilities
);
8892 for (i
= 0; i
< OFPGT12_N_TYPES
; i
++) {
8893 features
->max_groups
[i
] = ntohl(ogf
->max_groups
[i
]);
8894 features
->ofpacts
[i
] = ofpact_bitmap_from_openflow(
8895 ogf
->actions
[i
], oh
->version
);
8899 /* Parse a group status request message into a 32 bit OpenFlow 1.1
8900 * group ID and stores the latter in '*group_id'.
8901 * Returns 0 if successful, otherwise an OFPERR_* number. */
8903 ofputil_decode_group_stats_request(const struct ofp_header
*request
,
8906 const struct ofp11_group_stats_request
*gsr11
= ofpmsg_body(request
);
8907 *group_id
= ntohl(gsr11
->group_id
);
8911 /* Converts a group stats reply in 'msg' into an abstract ofputil_group_stats
8912 * in 'gs'. Assigns freshly allocated memory to gs->bucket_stats for the
8913 * caller to eventually free.
8915 * Multiple group stats replies can be packed into a single OpenFlow message.
8916 * Calling this function multiple times for a single 'msg' iterates through the
8917 * replies. The caller must initially leave 'msg''s layer pointers null and
8918 * not modify them between calls.
8920 * Returns 0 if successful, EOF if no replies were left in this 'msg',
8921 * otherwise a positive errno value. */
8923 ofputil_decode_group_stats_reply(struct ofpbuf
*msg
,
8924 struct ofputil_group_stats
*gs
)
8926 struct ofp11_bucket_counter
*obc
;
8927 struct ofp11_group_stats
*ogs11
;
8934 gs
->bucket_stats
= NULL
;
8935 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
8936 : ofpraw_pull(&raw
, msg
));
8945 if (raw
== OFPRAW_OFPST11_GROUP_REPLY
) {
8946 base_len
= sizeof *ogs11
;
8947 ogs11
= ofpbuf_try_pull(msg
, sizeof *ogs11
);
8948 gs
->duration_sec
= gs
->duration_nsec
= UINT32_MAX
;
8949 } else if (raw
== OFPRAW_OFPST13_GROUP_REPLY
) {
8950 struct ofp13_group_stats
*ogs13
;
8952 base_len
= sizeof *ogs13
;
8953 ogs13
= ofpbuf_try_pull(msg
, sizeof *ogs13
);
8956 gs
->duration_sec
= ntohl(ogs13
->duration_sec
);
8957 gs
->duration_nsec
= ntohl(ogs13
->duration_nsec
);
8966 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s reply has %"PRIu32
" leftover bytes at end",
8967 ofpraw_get_name(raw
), msg
->size
);
8968 return OFPERR_OFPBRC_BAD_LEN
;
8970 length
= ntohs(ogs11
->length
);
8971 if (length
< sizeof base_len
) {
8972 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s reply claims invalid length %"PRIuSIZE
,
8973 ofpraw_get_name(raw
), length
);
8974 return OFPERR_OFPBRC_BAD_LEN
;
8977 gs
->group_id
= ntohl(ogs11
->group_id
);
8978 gs
->ref_count
= ntohl(ogs11
->ref_count
);
8979 gs
->packet_count
= ntohll(ogs11
->packet_count
);
8980 gs
->byte_count
= ntohll(ogs11
->byte_count
);
8982 gs
->n_buckets
= (length
- base_len
) / sizeof *obc
;
8983 obc
= ofpbuf_try_pull(msg
, gs
->n_buckets
* sizeof *obc
);
8985 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s reply has %"PRIu32
" leftover bytes at end",
8986 ofpraw_get_name(raw
), msg
->size
);
8987 return OFPERR_OFPBRC_BAD_LEN
;
8990 gs
->bucket_stats
= xmalloc(gs
->n_buckets
* sizeof *gs
->bucket_stats
);
8991 for (i
= 0; i
< gs
->n_buckets
; i
++) {
8992 gs
->bucket_stats
[i
].packet_count
= ntohll(obc
[i
].packet_count
);
8993 gs
->bucket_stats
[i
].byte_count
= ntohll(obc
[i
].byte_count
);
9000 ofputil_put_ofp11_bucket(const struct ofputil_bucket
*bucket
,
9001 struct ofpbuf
*openflow
, enum ofp_version ofp_version
)
9003 struct ofp11_bucket
*ob
;
9006 start
= openflow
->size
;
9007 ofpbuf_put_zeros(openflow
, sizeof *ob
);
9008 ofpacts_put_openflow_actions(bucket
->ofpacts
, bucket
->ofpacts_len
,
9009 openflow
, ofp_version
);
9010 ob
= ofpbuf_at_assert(openflow
, start
, sizeof *ob
);
9011 ob
->len
= htons(openflow
->size
- start
);
9012 ob
->weight
= htons(bucket
->weight
);
9013 ob
->watch_port
= ofputil_port_to_ofp11(bucket
->watch_port
);
9014 ob
->watch_group
= htonl(bucket
->watch_group
);
9018 ofputil_put_ofp15_bucket(const struct ofputil_bucket
*bucket
,
9019 uint32_t bucket_id
, enum ofp11_group_type group_type
,
9020 struct ofpbuf
*openflow
, enum ofp_version ofp_version
)
9022 struct ofp15_bucket
*ob
;
9023 size_t start
, actions_start
, actions_len
;
9025 start
= openflow
->size
;
9026 ofpbuf_put_zeros(openflow
, sizeof *ob
);
9028 actions_start
= openflow
->size
;
9029 ofpacts_put_openflow_actions(bucket
->ofpacts
, bucket
->ofpacts_len
,
9030 openflow
, ofp_version
);
9031 actions_len
= openflow
->size
- actions_start
;
9033 if (group_type
== OFPGT11_SELECT
) {
9034 ofpprop_put_u16(openflow
, OFPGBPT15_WEIGHT
, bucket
->weight
);
9036 if (bucket
->watch_port
!= OFPP_ANY
) {
9037 ofpprop_put_be32(openflow
, OFPGBPT15_WATCH_PORT
,
9038 ofputil_port_to_ofp11(bucket
->watch_port
));
9040 if (bucket
->watch_group
!= OFPG_ANY
) {
9041 ofpprop_put_u32(openflow
, OFPGBPT15_WATCH_GROUP
, bucket
->watch_group
);
9044 ob
= ofpbuf_at_assert(openflow
, start
, sizeof *ob
);
9045 ob
->len
= htons(openflow
->size
- start
);
9046 ob
->action_array_len
= htons(actions_len
);
9047 ob
->bucket_id
= htonl(bucket_id
);
9051 ofputil_put_group_prop_ntr_selection_method(enum ofp_version ofp_version
,
9052 const struct ofputil_group_props
*gp
,
9053 struct ofpbuf
*openflow
)
9055 struct ntr_group_prop_selection_method
*prop
;
9058 start
= openflow
->size
;
9059 ofpbuf_put_zeros(openflow
, sizeof *prop
);
9060 oxm_put_field_array(openflow
, &gp
->fields
, ofp_version
);
9061 prop
= ofpbuf_at_assert(openflow
, start
, sizeof *prop
);
9062 prop
->type
= htons(OFPGPT15_EXPERIMENTER
);
9063 prop
->experimenter
= htonl(NTR_VENDOR_ID
);
9064 prop
->exp_type
= htonl(NTRT_SELECTION_METHOD
);
9065 strcpy(prop
->selection_method
, gp
->selection_method
);
9066 prop
->selection_method_param
= htonll(gp
->selection_method_param
);
9067 ofpprop_end(openflow
, start
);
9071 ofputil_append_ofp11_group_desc_reply(const struct ofputil_group_desc
*gds
,
9072 const struct ovs_list
*buckets
,
9073 struct ovs_list
*replies
,
9074 enum ofp_version version
)
9076 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
9077 struct ofp11_group_desc_stats
*ogds
;
9078 struct ofputil_bucket
*bucket
;
9081 start_ogds
= reply
->size
;
9082 ofpbuf_put_zeros(reply
, sizeof *ogds
);
9083 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
9084 ofputil_put_ofp11_bucket(bucket
, reply
, version
);
9086 ogds
= ofpbuf_at_assert(reply
, start_ogds
, sizeof *ogds
);
9087 ogds
->length
= htons(reply
->size
- start_ogds
);
9088 ogds
->type
= gds
->type
;
9089 ogds
->group_id
= htonl(gds
->group_id
);
9091 ofpmp_postappend(replies
, start_ogds
);
9095 ofputil_append_ofp15_group_desc_reply(const struct ofputil_group_desc
*gds
,
9096 const struct ovs_list
*buckets
,
9097 struct ovs_list
*replies
,
9098 enum ofp_version version
)
9100 struct ofpbuf
*reply
= ofpbuf_from_list(ovs_list_back(replies
));
9101 struct ofp15_group_desc_stats
*ogds
;
9102 struct ofputil_bucket
*bucket
;
9103 size_t start_ogds
, start_buckets
;
9105 start_ogds
= reply
->size
;
9106 ofpbuf_put_zeros(reply
, sizeof *ogds
);
9107 start_buckets
= reply
->size
;
9108 LIST_FOR_EACH (bucket
, list_node
, buckets
) {
9109 ofputil_put_ofp15_bucket(bucket
, bucket
->bucket_id
,
9110 gds
->type
, reply
, version
);
9112 ogds
= ofpbuf_at_assert(reply
, start_ogds
, sizeof *ogds
);
9113 ogds
->type
= gds
->type
;
9114 ogds
->group_id
= htonl(gds
->group_id
);
9115 ogds
->bucket_list_len
= htons(reply
->size
- start_buckets
);
9117 /* Add group properties */
9118 if (gds
->props
.selection_method
[0]) {
9119 ofputil_put_group_prop_ntr_selection_method(version
, &gds
->props
,
9122 ogds
= ofpbuf_at_assert(reply
, start_ogds
, sizeof *ogds
);
9123 ogds
->length
= htons(reply
->size
- start_ogds
);
9125 ofpmp_postappend(replies
, start_ogds
);
9128 /* Appends a group stats reply that contains the data in 'gds' to those already
9129 * present in the list of ofpbufs in 'replies'. 'replies' should have been
9130 * initialized with ofpmp_init(). */
9132 ofputil_append_group_desc_reply(const struct ofputil_group_desc
*gds
,
9133 const struct ovs_list
*buckets
,
9134 struct ovs_list
*replies
)
9136 enum ofp_version version
= ofpmp_version(replies
);
9144 ofputil_append_ofp11_group_desc_reply(gds
, buckets
, replies
, version
);
9149 ofputil_append_ofp15_group_desc_reply(gds
, buckets
, replies
, version
);
9159 ofputil_pull_ofp11_buckets(struct ofpbuf
*msg
, size_t buckets_length
,
9160 enum ofp_version version
, struct ovs_list
*buckets
)
9162 struct ofp11_bucket
*ob
;
9163 uint32_t bucket_id
= 0;
9165 ovs_list_init(buckets
);
9166 while (buckets_length
> 0) {
9167 struct ofputil_bucket
*bucket
;
9168 struct ofpbuf ofpacts
;
9172 ob
= (buckets_length
>= sizeof *ob
9173 ? ofpbuf_try_pull(msg
, sizeof *ob
)
9176 VLOG_WARN_RL(&bad_ofmsg_rl
, "buckets end with %"PRIuSIZE
" leftover bytes",
9178 return OFPERR_OFPGMFC_BAD_BUCKET
;
9181 ob_len
= ntohs(ob
->len
);
9182 if (ob_len
< sizeof *ob
) {
9183 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket length "
9184 "%"PRIuSIZE
" is not valid", ob_len
);
9185 return OFPERR_OFPGMFC_BAD_BUCKET
;
9186 } else if (ob_len
> buckets_length
) {
9187 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket length "
9188 "%"PRIuSIZE
" exceeds remaining buckets data size %"PRIuSIZE
,
9189 ob_len
, buckets_length
);
9190 return OFPERR_OFPGMFC_BAD_BUCKET
;
9192 buckets_length
-= ob_len
;
9194 ofpbuf_init(&ofpacts
, 0);
9195 error
= ofpacts_pull_openflow_actions(msg
, ob_len
- sizeof *ob
,
9196 version
, NULL
, NULL
, &ofpacts
);
9198 ofpbuf_uninit(&ofpacts
);
9199 ofputil_bucket_list_destroy(buckets
);
9203 bucket
= xzalloc(sizeof *bucket
);
9204 bucket
->weight
= ntohs(ob
->weight
);
9205 error
= ofputil_port_from_ofp11(ob
->watch_port
, &bucket
->watch_port
);
9207 ofpbuf_uninit(&ofpacts
);
9208 ofputil_bucket_list_destroy(buckets
);
9210 return OFPERR_OFPGMFC_BAD_WATCH
;
9212 bucket
->watch_group
= ntohl(ob
->watch_group
);
9213 bucket
->bucket_id
= bucket_id
++;
9215 bucket
->ofpacts
= ofpbuf_steal_data(&ofpacts
);
9216 bucket
->ofpacts_len
= ofpacts
.size
;
9217 ovs_list_push_back(buckets
, &bucket
->list_node
);
9224 ofputil_pull_ofp15_buckets(struct ofpbuf
*msg
, size_t buckets_length
,
9225 enum ofp_version version
, uint8_t group_type
,
9226 struct ovs_list
*buckets
)
9228 struct ofp15_bucket
*ob
;
9230 ovs_list_init(buckets
);
9231 while (buckets_length
> 0) {
9232 struct ofputil_bucket
*bucket
= NULL
;
9233 struct ofpbuf ofpacts
;
9234 enum ofperr err
= OFPERR_OFPGMFC_BAD_BUCKET
;
9235 size_t ob_len
, actions_len
, properties_len
;
9236 ovs_be32 watch_port
= ofputil_port_to_ofp11(OFPP_ANY
);
9237 ovs_be32 watch_group
= htonl(OFPG_ANY
);
9238 ovs_be16 weight
= htons(group_type
== OFPGT11_SELECT
? 1 : 0);
9240 ofpbuf_init(&ofpacts
, 0);
9242 ob
= ofpbuf_try_pull(msg
, sizeof *ob
);
9244 VLOG_WARN_RL(&bad_ofmsg_rl
, "buckets end with %"PRIuSIZE
9245 " leftover bytes", buckets_length
);
9249 ob_len
= ntohs(ob
->len
);
9250 actions_len
= ntohs(ob
->action_array_len
);
9252 if (ob_len
< sizeof *ob
) {
9253 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket length "
9254 "%"PRIuSIZE
" is not valid", ob_len
);
9256 } else if (ob_len
> buckets_length
) {
9257 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket length "
9258 "%"PRIuSIZE
" exceeds remaining buckets data size %"
9259 PRIuSIZE
, ob_len
, buckets_length
);
9261 } else if (actions_len
> ob_len
- sizeof *ob
) {
9262 VLOG_WARN_RL(&bad_ofmsg_rl
, "OpenFlow message bucket actions "
9263 "length %"PRIuSIZE
" exceeds remaining bucket "
9264 "data size %"PRIuSIZE
, actions_len
,
9265 ob_len
- sizeof *ob
);
9268 buckets_length
-= ob_len
;
9270 err
= ofpacts_pull_openflow_actions(msg
, actions_len
, version
,
9271 NULL
, NULL
, &ofpacts
);
9276 properties_len
= ob_len
- sizeof *ob
- actions_len
;
9277 struct ofpbuf properties
= ofpbuf_const_initializer(
9278 ofpbuf_pull(msg
, properties_len
), properties_len
);
9279 while (properties
.size
> 0) {
9280 struct ofpbuf payload
;
9283 err
= ofpprop_pull(&properties
, &payload
, &type
);
9289 case OFPGBPT15_WEIGHT
:
9290 err
= ofpprop_parse_be16(&payload
, &weight
);
9293 case OFPGBPT15_WATCH_PORT
:
9294 err
= ofpprop_parse_be32(&payload
, &watch_port
);
9297 case OFPGBPT15_WATCH_GROUP
:
9298 err
= ofpprop_parse_be32(&payload
, &watch_group
);
9302 err
= OFPPROP_UNKNOWN(false, "group bucket", type
);
9311 bucket
= xzalloc(sizeof *bucket
);
9313 bucket
->weight
= ntohs(weight
);
9314 err
= ofputil_port_from_ofp11(watch_port
, &bucket
->watch_port
);
9316 err
= OFPERR_OFPGMFC_BAD_WATCH
;
9319 bucket
->watch_group
= ntohl(watch_group
);
9320 bucket
->bucket_id
= ntohl(ob
->bucket_id
);
9321 if (bucket
->bucket_id
> OFPG15_BUCKET_MAX
) {
9322 VLOG_WARN_RL(&bad_ofmsg_rl
, "bucket id (%u) is out of range",
9324 err
= OFPERR_OFPGMFC_BAD_BUCKET
;
9328 bucket
->ofpacts
= ofpbuf_steal_data(&ofpacts
);
9329 bucket
->ofpacts_len
= ofpacts
.size
;
9330 ovs_list_push_back(buckets
, &bucket
->list_node
);
9336 ofpbuf_uninit(&ofpacts
);
9337 ofputil_bucket_list_destroy(buckets
);
9341 if (ofputil_bucket_check_duplicate_id(buckets
)) {
9342 VLOG_WARN_RL(&bad_ofmsg_rl
, "Duplicate bucket id");
9343 ofputil_bucket_list_destroy(buckets
);
9344 return OFPERR_OFPGMFC_BAD_BUCKET
;
9351 ofputil_init_group_properties(struct ofputil_group_props
*gp
)
9353 memset(gp
, 0, sizeof *gp
);
9357 ofputil_group_properties_copy(struct ofputil_group_props
*to
,
9358 const struct ofputil_group_props
*from
)
9361 to
->fields
.values
= xmemdup(from
->fields
.values
, from
->fields
.values_size
);
9365 ofputil_group_properties_destroy(struct ofputil_group_props
*gp
)
9367 free(gp
->fields
.values
);
9371 parse_group_prop_ntr_selection_method(struct ofpbuf
*payload
,
9372 enum ofp11_group_type group_type
,
9373 enum ofp15_group_mod_command group_cmd
,
9374 struct ofputil_group_props
*gp
)
9376 struct ntr_group_prop_selection_method
*prop
= payload
->data
;
9377 size_t fields_len
, method_len
;
9380 switch (group_type
) {
9381 case OFPGT11_SELECT
:
9384 case OFPGT11_INDIRECT
:
9386 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method property is "
9387 "only allowed for select groups");
9388 return OFPERR_OFPBPC_BAD_VALUE
;
9393 switch (group_cmd
) {
9395 case OFPGC15_MODIFY
:
9396 case OFPGC15_ADD_OR_MOD
:
9398 case OFPGC15_DELETE
:
9399 case OFPGC15_INSERT_BUCKET
:
9400 case OFPGC15_REMOVE_BUCKET
:
9401 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method property is "
9402 "only allowed for add and delete group modifications");
9403 return OFPERR_OFPBPC_BAD_VALUE
;
9408 if (payload
->size
< sizeof *prop
) {
9409 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method property "
9410 "length %u is not valid", payload
->size
);
9411 return OFPERR_OFPBPC_BAD_LEN
;
9414 method_len
= strnlen(prop
->selection_method
, NTR_MAX_SELECTION_METHOD_LEN
);
9416 if (method_len
== NTR_MAX_SELECTION_METHOD_LEN
) {
9417 OFPPROP_LOG(&bad_ofmsg_rl
, false,
9418 "ntr selection method is not null terminated");
9419 return OFPERR_OFPBPC_BAD_VALUE
;
9422 if (strcmp("hash", prop
->selection_method
)
9423 && strcmp("dp_hash", prop
->selection_method
)) {
9424 OFPPROP_LOG(&bad_ofmsg_rl
, false,
9425 "ntr selection method '%s' is not supported",
9426 prop
->selection_method
);
9427 return OFPERR_OFPBPC_BAD_VALUE
;
9429 /* 'method_len' is now non-zero. */
9431 strcpy(gp
->selection_method
, prop
->selection_method
);
9432 gp
->selection_method_param
= ntohll(prop
->selection_method_param
);
9434 ofpbuf_pull(payload
, sizeof *prop
);
9436 fields_len
= ntohs(prop
->length
) - sizeof *prop
;
9437 if (fields_len
&& strcmp("hash", gp
->selection_method
)) {
9438 OFPPROP_LOG(&bad_ofmsg_rl
, false, "ntr selection method %s "
9439 "does not support fields", gp
->selection_method
);
9440 return OFPERR_OFPBPC_BAD_VALUE
;
9443 error
= oxm_pull_field_array(payload
->data
, fields_len
,
9446 OFPPROP_LOG(&bad_ofmsg_rl
, false,
9447 "ntr selection method fields are invalid");
9455 parse_ofp15_group_properties(struct ofpbuf
*msg
,
9456 enum ofp11_group_type group_type
,
9457 enum ofp15_group_mod_command group_cmd
,
9458 struct ofputil_group_props
*gp
,
9459 size_t properties_len
)
9461 struct ofpbuf properties
= ofpbuf_const_initializer(
9462 ofpbuf_pull(msg
, properties_len
), properties_len
);
9463 while (properties
.size
> 0) {
9464 struct ofpbuf payload
;
9468 error
= ofpprop_pull(&properties
, &payload
, &type
);
9474 case OFPPROP_EXP(NTR_VENDOR_ID
, NTRT_SELECTION_METHOD
):
9475 case OFPPROP_EXP(NTR_COMPAT_VENDOR_ID
, NTRT_SELECTION_METHOD
):
9476 error
= parse_group_prop_ntr_selection_method(&payload
, group_type
,
9481 error
= OFPPROP_UNKNOWN(false, "group", type
);
9494 ofputil_decode_ofp11_group_desc_reply(struct ofputil_group_desc
*gd
,
9496 enum ofp_version version
)
9498 struct ofp11_group_desc_stats
*ogds
;
9502 ofpraw_pull_assert(msg
);
9509 ogds
= ofpbuf_try_pull(msg
, sizeof *ogds
);
9511 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply has %"PRIu32
" "
9512 "leftover bytes at end", msg
->size
);
9513 return OFPERR_OFPBRC_BAD_LEN
;
9515 gd
->type
= ogds
->type
;
9516 gd
->group_id
= ntohl(ogds
->group_id
);
9518 length
= ntohs(ogds
->length
);
9519 if (length
< sizeof *ogds
|| length
- sizeof *ogds
> msg
->size
) {
9520 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply claims invalid "
9521 "length %"PRIuSIZE
, length
);
9522 return OFPERR_OFPBRC_BAD_LEN
;
9525 return ofputil_pull_ofp11_buckets(msg
, length
- sizeof *ogds
, version
,
9530 ofputil_decode_ofp15_group_desc_reply(struct ofputil_group_desc
*gd
,
9532 enum ofp_version version
)
9534 struct ofp15_group_desc_stats
*ogds
;
9535 uint16_t length
, bucket_list_len
;
9539 ofpraw_pull_assert(msg
);
9546 ogds
= ofpbuf_try_pull(msg
, sizeof *ogds
);
9548 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply has %"PRIu32
" "
9549 "leftover bytes at end", msg
->size
);
9550 return OFPERR_OFPBRC_BAD_LEN
;
9552 gd
->type
= ogds
->type
;
9553 gd
->group_id
= ntohl(ogds
->group_id
);
9555 length
= ntohs(ogds
->length
);
9556 if (length
< sizeof *ogds
|| length
- sizeof *ogds
> msg
->size
) {
9557 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply claims invalid "
9558 "length %u", length
);
9559 return OFPERR_OFPBRC_BAD_LEN
;
9562 bucket_list_len
= ntohs(ogds
->bucket_list_len
);
9563 if (length
< bucket_list_len
+ sizeof *ogds
) {
9564 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST11_GROUP_DESC reply claims invalid "
9565 "bucket list length %u", bucket_list_len
);
9566 return OFPERR_OFPBRC_BAD_LEN
;
9568 error
= ofputil_pull_ofp15_buckets(msg
, bucket_list_len
, version
, gd
->type
,
9574 /* By definition group desc messages don't have a group mod command.
9575 * However, parse_group_prop_ntr_selection_method() checks to make sure
9576 * that the command is OFPGC15_ADD or OFPGC15_DELETE to guard
9577 * against group mod messages with other commands supplying
9578 * a NTR selection method group experimenter property.
9579 * Such properties are valid for group desc replies so
9580 * claim that the group mod command is OFPGC15_ADD to
9581 * satisfy the check in parse_group_prop_ntr_selection_method() */
9582 return parse_ofp15_group_properties(msg
, gd
->type
, OFPGC15_ADD
, &gd
->props
,
9583 length
- sizeof *ogds
- bucket_list_len
);
9586 /* Converts a group description reply in 'msg' into an abstract
9587 * ofputil_group_desc in 'gd'.
9589 * Multiple group description replies can be packed into a single OpenFlow
9590 * message. Calling this function multiple times for a single 'msg' iterates
9591 * through the replies. The caller must initially leave 'msg''s layer pointers
9592 * null and not modify them between calls.
9594 * Returns 0 if successful, EOF if no replies were left in this 'msg',
9595 * otherwise a positive errno value. */
9597 ofputil_decode_group_desc_reply(struct ofputil_group_desc
*gd
,
9598 struct ofpbuf
*msg
, enum ofp_version version
)
9600 ofputil_init_group_properties(&gd
->props
);
9608 return ofputil_decode_ofp11_group_desc_reply(gd
, msg
, version
);
9612 return ofputil_decode_ofp15_group_desc_reply(gd
, msg
, version
);
9621 ofputil_uninit_group_mod(struct ofputil_group_mod
*gm
)
9623 ofputil_bucket_list_destroy(&gm
->buckets
);
9624 ofputil_group_properties_destroy(&gm
->props
);
9627 static struct ofpbuf
*
9628 ofputil_encode_ofp11_group_mod(enum ofp_version ofp_version
,
9629 const struct ofputil_group_mod
*gm
)
9632 struct ofp11_group_mod
*ogm
;
9634 struct ofputil_bucket
*bucket
;
9636 b
= ofpraw_alloc(OFPRAW_OFPT11_GROUP_MOD
, ofp_version
, 0);
9637 start_ogm
= b
->size
;
9638 ofpbuf_put_zeros(b
, sizeof *ogm
);
9640 LIST_FOR_EACH (bucket
, list_node
, &gm
->buckets
) {
9641 ofputil_put_ofp11_bucket(bucket
, b
, ofp_version
);
9643 ogm
= ofpbuf_at_assert(b
, start_ogm
, sizeof *ogm
);
9644 ogm
->command
= htons(gm
->command
);
9645 ogm
->type
= gm
->type
;
9646 ogm
->group_id
= htonl(gm
->group_id
);
9651 static struct ofpbuf
*
9652 ofputil_encode_ofp15_group_mod(enum ofp_version ofp_version
,
9653 const struct ofputil_group_mod
*gm
)
9656 struct ofp15_group_mod
*ogm
;
9658 struct ofputil_bucket
*bucket
;
9659 struct id_pool
*bucket_ids
= NULL
;
9661 b
= ofpraw_alloc(OFPRAW_OFPT15_GROUP_MOD
, ofp_version
, 0);
9662 start_ogm
= b
->size
;
9663 ofpbuf_put_zeros(b
, sizeof *ogm
);
9665 LIST_FOR_EACH (bucket
, list_node
, &gm
->buckets
) {
9668 /* Generate a bucket id if none was supplied */
9669 if (bucket
->bucket_id
> OFPG15_BUCKET_MAX
) {
9671 const struct ofputil_bucket
*bkt
;
9673 bucket_ids
= id_pool_create(0, OFPG15_BUCKET_MAX
+ 1);
9675 /* Mark all bucket_ids that are present in gm
9676 * as used in the pool. */
9677 LIST_FOR_EACH_REVERSE (bkt
, list_node
, &gm
->buckets
) {
9678 if (bkt
== bucket
) {
9681 if (bkt
->bucket_id
<= OFPG15_BUCKET_MAX
) {
9682 id_pool_add(bucket_ids
, bkt
->bucket_id
);
9687 if (!id_pool_alloc_id(bucket_ids
, &bucket_id
)) {
9691 bucket_id
= bucket
->bucket_id
;
9694 ofputil_put_ofp15_bucket(bucket
, bucket_id
, gm
->type
, b
, ofp_version
);
9696 ogm
= ofpbuf_at_assert(b
, start_ogm
, sizeof *ogm
);
9697 ogm
->command
= htons(gm
->command
);
9698 ogm
->type
= gm
->type
;
9699 ogm
->group_id
= htonl(gm
->group_id
);
9700 ogm
->command_bucket_id
= htonl(gm
->command_bucket_id
);
9701 ogm
->bucket_array_len
= htons(b
->size
- start_ogm
- sizeof *ogm
);
9703 /* Add group properties */
9704 if (gm
->props
.selection_method
[0]) {
9705 ofputil_put_group_prop_ntr_selection_method(ofp_version
, &gm
->props
, b
);
9708 id_pool_destroy(bucket_ids
);
9713 bad_group_cmd(enum ofp15_group_mod_command cmd
)
9715 const char *opt_version
;
9716 const char *version
;
9717 const char *cmd_str
;
9721 case OFPGC15_MODIFY
:
9722 case OFPGC15_ADD_OR_MOD
:
9723 case OFPGC15_DELETE
:
9728 case OFPGC15_INSERT_BUCKET
:
9729 case OFPGC15_REMOVE_BUCKET
:
9740 cmd_str
= "add-group";
9743 case OFPGC15_MODIFY
:
9744 case OFPGC15_ADD_OR_MOD
:
9745 cmd_str
= "mod-group";
9748 case OFPGC15_DELETE
:
9749 cmd_str
= "del-group";
9752 case OFPGC15_INSERT_BUCKET
:
9753 cmd_str
= "insert-bucket";
9756 case OFPGC15_REMOVE_BUCKET
:
9757 cmd_str
= "remove-bucket";
9764 ovs_fatal(0, "%s needs OpenFlow %s or later (\'-O OpenFlow%s\')",
9765 cmd_str
, version
, opt_version
);
9769 /* Converts abstract group mod 'gm' into a message for OpenFlow version
9770 * 'ofp_version' and returns the message. */
9772 ofputil_encode_group_mod(enum ofp_version ofp_version
,
9773 const struct ofputil_group_mod
*gm
)
9776 switch (ofp_version
) {
9778 bad_group_cmd(gm
->command
);
9785 if (gm
->command
> OFPGC11_DELETE
&& gm
->command
!= OFPGC11_ADD_OR_MOD
) {
9786 bad_group_cmd(gm
->command
);
9788 return ofputil_encode_ofp11_group_mod(ofp_version
, gm
);
9792 return ofputil_encode_ofp15_group_mod(ofp_version
, gm
);
9800 ofputil_pull_ofp11_group_mod(struct ofpbuf
*msg
, enum ofp_version ofp_version
,
9801 struct ofputil_group_mod
*gm
)
9803 const struct ofp11_group_mod
*ogm
;
9806 ogm
= ofpbuf_pull(msg
, sizeof *ogm
);
9807 gm
->command
= ntohs(ogm
->command
);
9808 gm
->type
= ogm
->type
;
9809 gm
->group_id
= ntohl(ogm
->group_id
);
9810 gm
->command_bucket_id
= OFPG15_BUCKET_ALL
;
9812 error
= ofputil_pull_ofp11_buckets(msg
, msg
->size
, ofp_version
,
9815 /* OF1.3.5+ prescribes an error when an OFPGC_DELETE includes buckets. */
9817 && ofp_version
>= OFP13_VERSION
9818 && gm
->command
== OFPGC11_DELETE
9819 && !ovs_list_is_empty(&gm
->buckets
)) {
9820 error
= OFPERR_OFPGMFC_INVALID_GROUP
;
9827 ofputil_pull_ofp15_group_mod(struct ofpbuf
*msg
, enum ofp_version ofp_version
,
9828 struct ofputil_group_mod
*gm
)
9830 const struct ofp15_group_mod
*ogm
;
9831 uint16_t bucket_list_len
;
9832 enum ofperr error
= OFPERR_OFPGMFC_BAD_BUCKET
;
9834 ogm
= ofpbuf_pull(msg
, sizeof *ogm
);
9835 gm
->command
= ntohs(ogm
->command
);
9836 gm
->type
= ogm
->type
;
9837 gm
->group_id
= ntohl(ogm
->group_id
);
9839 gm
->command_bucket_id
= ntohl(ogm
->command_bucket_id
);
9840 switch (gm
->command
) {
9841 case OFPGC15_REMOVE_BUCKET
:
9842 if (gm
->command_bucket_id
== OFPG15_BUCKET_ALL
) {
9846 case OFPGC15_INSERT_BUCKET
:
9847 if (gm
->command_bucket_id
<= OFPG15_BUCKET_MAX
||
9848 gm
->command_bucket_id
== OFPG15_BUCKET_FIRST
9849 || gm
->command_bucket_id
== OFPG15_BUCKET_LAST
) {
9855 case OFPGC11_MODIFY
:
9856 case OFPGC11_ADD_OR_MOD
:
9857 case OFPGC11_DELETE
:
9859 if (gm
->command_bucket_id
== OFPG15_BUCKET_ALL
) {
9865 VLOG_WARN_RL(&bad_ofmsg_rl
,
9866 "group command bucket id (%u) is out of range",
9867 gm
->command_bucket_id
);
9868 return OFPERR_OFPGMFC_BAD_BUCKET
;
9871 bucket_list_len
= ntohs(ogm
->bucket_array_len
);
9872 if (bucket_list_len
> msg
->size
) {
9873 return OFPERR_OFPBRC_BAD_LEN
;
9875 error
= ofputil_pull_ofp15_buckets(msg
, bucket_list_len
, ofp_version
,
9876 gm
->type
, &gm
->buckets
);
9881 return parse_ofp15_group_properties(msg
, gm
->type
, gm
->command
, &gm
->props
,
9885 /* Converts OpenFlow group mod message 'oh' into an abstract group mod in
9886 * 'gm'. Returns 0 if successful, otherwise an OpenFlow error code. */
9888 ofputil_decode_group_mod(const struct ofp_header
*oh
,
9889 struct ofputil_group_mod
*gm
)
9891 ofputil_init_group_properties(&gm
->props
);
9893 enum ofp_version ofp_version
= oh
->version
;
9894 struct ofpbuf msg
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
9895 ofpraw_pull_assert(&msg
);
9898 switch (ofp_version
)
9904 err
= ofputil_pull_ofp11_group_mod(&msg
, ofp_version
, gm
);
9909 err
= ofputil_pull_ofp15_group_mod(&msg
, ofp_version
, gm
);
9921 case OFPGT11_INDIRECT
:
9922 if (gm
->command
!= OFPGC11_DELETE
9923 && !ovs_list_is_singleton(&gm
->buckets
) ) {
9924 return OFPERR_OFPGMFC_INVALID_GROUP
;
9928 case OFPGT11_SELECT
:
9932 return OFPERR_OFPGMFC_BAD_TYPE
;
9935 switch (gm
->command
) {
9937 case OFPGC11_MODIFY
:
9938 case OFPGC11_ADD_OR_MOD
:
9939 case OFPGC11_DELETE
:
9940 case OFPGC15_INSERT_BUCKET
:
9942 case OFPGC15_REMOVE_BUCKET
:
9943 if (!ovs_list_is_empty(&gm
->buckets
)) {
9944 return OFPERR_OFPGMFC_BAD_BUCKET
;
9948 return OFPERR_OFPGMFC_BAD_COMMAND
;
9951 struct ofputil_bucket
*bucket
;
9952 LIST_FOR_EACH (bucket
, list_node
, &gm
->buckets
) {
9953 if (bucket
->weight
&& gm
->type
!= OFPGT11_SELECT
) {
9954 return OFPERR_OFPGMFC_INVALID_GROUP
;
9959 case OFPGT11_INDIRECT
:
9960 if (ofputil_bucket_has_liveness(bucket
)) {
9961 return OFPERR_OFPGMFC_WATCH_UNSUPPORTED
;
9964 case OFPGT11_SELECT
:
9967 if (!ofputil_bucket_has_liveness(bucket
)) {
9968 return OFPERR_OFPGMFC_INVALID_GROUP
;
9972 /* Returning BAD TYPE to be consistent
9973 * though gm->type has been checked already. */
9974 return OFPERR_OFPGMFC_BAD_TYPE
;
9981 /* Destroys 'bms'. */
9983 ofputil_free_bundle_msgs(struct ofputil_bundle_msg
*bms
, size_t n_bms
)
9985 for (size_t i
= 0; i
< n_bms
; i
++) {
9986 switch ((int)bms
[i
].type
) {
9987 case OFPTYPE_FLOW_MOD
:
9988 free(CONST_CAST(struct ofpact
*, bms
[i
].fm
.ofpacts
));
9990 case OFPTYPE_GROUP_MOD
:
9991 ofputil_uninit_group_mod(&bms
[i
].gm
);
9993 case OFPTYPE_PACKET_OUT
:
9994 free(bms
[i
].po
.ofpacts
);
9995 free(CONST_CAST(void *, bms
[i
].po
.packet
));
10005 ofputil_encode_bundle_msgs(const struct ofputil_bundle_msg
*bms
,
10006 size_t n_bms
, struct ovs_list
*requests
,
10007 enum ofputil_protocol protocol
)
10009 enum ofp_version version
= ofputil_protocol_to_ofp_version(protocol
);
10011 for (size_t i
= 0; i
< n_bms
; i
++) {
10012 struct ofpbuf
*request
= NULL
;
10014 switch ((int)bms
[i
].type
) {
10015 case OFPTYPE_FLOW_MOD
:
10016 request
= ofputil_encode_flow_mod(&bms
[i
].fm
, protocol
);
10018 case OFPTYPE_GROUP_MOD
:
10019 request
= ofputil_encode_group_mod(version
, &bms
[i
].gm
);
10021 case OFPTYPE_PACKET_OUT
:
10022 request
= ofputil_encode_packet_out(&bms
[i
].po
, protocol
);
10028 ovs_list_push_back(requests
, &request
->list_node
);
10033 /* Parse a queue status request message into 'oqsr'.
10034 * Returns 0 if successful, otherwise an OFPERR_* number. */
10036 ofputil_decode_queue_stats_request(const struct ofp_header
*request
,
10037 struct ofputil_queue_stats_request
*oqsr
)
10039 switch ((enum ofp_version
)request
->version
) {
10040 case OFP16_VERSION
:
10041 case OFP15_VERSION
:
10042 case OFP14_VERSION
:
10043 case OFP13_VERSION
:
10044 case OFP12_VERSION
:
10045 case OFP11_VERSION
: {
10046 const struct ofp11_queue_stats_request
*qsr11
= ofpmsg_body(request
);
10047 oqsr
->queue_id
= ntohl(qsr11
->queue_id
);
10048 return ofputil_port_from_ofp11(qsr11
->port_no
, &oqsr
->port_no
);
10051 case OFP10_VERSION
: {
10052 const struct ofp10_queue_stats_request
*qsr10
= ofpmsg_body(request
);
10053 oqsr
->queue_id
= ntohl(qsr10
->queue_id
);
10054 oqsr
->port_no
= u16_to_ofp(ntohs(qsr10
->port_no
));
10055 /* OF 1.0 uses OFPP_ALL for OFPP_ANY */
10056 if (oqsr
->port_no
== OFPP_ALL
) {
10057 oqsr
->port_no
= OFPP_ANY
;
10067 /* Encode a queue stats request for 'oqsr', the encoded message
10068 * will be for OpenFlow version 'ofp_version'. Returns message
10069 * as a struct ofpbuf. Returns encoded message on success, NULL on error. */
10071 ofputil_encode_queue_stats_request(enum ofp_version ofp_version
,
10072 const struct ofputil_queue_stats_request
*oqsr
)
10074 struct ofpbuf
*request
;
10076 switch (ofp_version
) {
10077 case OFP11_VERSION
:
10078 case OFP12_VERSION
:
10079 case OFP13_VERSION
:
10080 case OFP14_VERSION
:
10081 case OFP15_VERSION
:
10082 case OFP16_VERSION
: {
10083 struct ofp11_queue_stats_request
*req
;
10084 request
= ofpraw_alloc(OFPRAW_OFPST11_QUEUE_REQUEST
, ofp_version
, 0);
10085 req
= ofpbuf_put_zeros(request
, sizeof *req
);
10086 req
->port_no
= ofputil_port_to_ofp11(oqsr
->port_no
);
10087 req
->queue_id
= htonl(oqsr
->queue_id
);
10090 case OFP10_VERSION
: {
10091 struct ofp10_queue_stats_request
*req
;
10092 request
= ofpraw_alloc(OFPRAW_OFPST10_QUEUE_REQUEST
, ofp_version
, 0);
10093 req
= ofpbuf_put_zeros(request
, sizeof *req
);
10094 /* OpenFlow 1.0 needs OFPP_ALL instead of OFPP_ANY */
10095 req
->port_no
= htons(ofp_to_u16(oqsr
->port_no
== OFPP_ANY
10096 ? OFPP_ALL
: oqsr
->port_no
));
10097 req
->queue_id
= htonl(oqsr
->queue_id
);
10107 /* Returns the number of queue stats elements in OFPTYPE_QUEUE_STATS_REPLY
10110 ofputil_count_queue_stats(const struct ofp_header
*oh
)
10112 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
10113 ofpraw_pull_assert(&b
);
10115 for (size_t n
= 0; ; n
++) {
10116 struct ofputil_queue_stats qs
;
10117 if (ofputil_decode_queue_stats(&qs
, &b
)) {
10124 ofputil_queue_stats_from_ofp10(struct ofputil_queue_stats
*oqs
,
10125 const struct ofp10_queue_stats
*qs10
)
10127 oqs
->port_no
= u16_to_ofp(ntohs(qs10
->port_no
));
10128 oqs
->queue_id
= ntohl(qs10
->queue_id
);
10129 oqs
->tx_bytes
= ntohll(get_32aligned_be64(&qs10
->tx_bytes
));
10130 oqs
->tx_packets
= ntohll(get_32aligned_be64(&qs10
->tx_packets
));
10131 oqs
->tx_errors
= ntohll(get_32aligned_be64(&qs10
->tx_errors
));
10132 oqs
->duration_sec
= oqs
->duration_nsec
= UINT32_MAX
;
10138 ofputil_queue_stats_from_ofp11(struct ofputil_queue_stats
*oqs
,
10139 const struct ofp11_queue_stats
*qs11
)
10143 error
= ofputil_port_from_ofp11(qs11
->port_no
, &oqs
->port_no
);
10148 oqs
->queue_id
= ntohl(qs11
->queue_id
);
10149 oqs
->tx_bytes
= ntohll(qs11
->tx_bytes
);
10150 oqs
->tx_packets
= ntohll(qs11
->tx_packets
);
10151 oqs
->tx_errors
= ntohll(qs11
->tx_errors
);
10152 oqs
->duration_sec
= oqs
->duration_nsec
= UINT32_MAX
;
10158 ofputil_queue_stats_from_ofp13(struct ofputil_queue_stats
*oqs
,
10159 const struct ofp13_queue_stats
*qs13
)
10161 enum ofperr error
= ofputil_queue_stats_from_ofp11(oqs
, &qs13
->qs
);
10163 oqs
->duration_sec
= ntohl(qs13
->duration_sec
);
10164 oqs
->duration_nsec
= ntohl(qs13
->duration_nsec
);
10171 ofputil_pull_ofp14_queue_stats(struct ofputil_queue_stats
*oqs
,
10172 struct ofpbuf
*msg
)
10174 const struct ofp14_queue_stats
*qs14
;
10177 qs14
= ofpbuf_try_pull(msg
, sizeof *qs14
);
10179 return OFPERR_OFPBRC_BAD_LEN
;
10182 len
= ntohs(qs14
->length
);
10183 if (len
< sizeof *qs14
|| len
- sizeof *qs14
> msg
->size
) {
10184 return OFPERR_OFPBRC_BAD_LEN
;
10186 ofpbuf_pull(msg
, len
- sizeof *qs14
);
10188 /* No properties yet defined, so ignore them for now. */
10190 return ofputil_queue_stats_from_ofp13(oqs
, &qs14
->qs
);
10193 /* Converts an OFPST_QUEUE_STATS reply in 'msg' into an abstract
10194 * ofputil_queue_stats in 'qs'.
10196 * Multiple OFPST_QUEUE_STATS replies can be packed into a single OpenFlow
10197 * message. Calling this function multiple times for a single 'msg' iterates
10198 * through the replies. The caller must initially leave 'msg''s layer pointers
10199 * null and not modify them between calls.
10201 * Returns 0 if successful, EOF if no replies were left in this 'msg',
10202 * otherwise a positive errno value. */
10204 ofputil_decode_queue_stats(struct ofputil_queue_stats
*qs
, struct ofpbuf
*msg
)
10209 error
= (msg
->header
? ofpraw_decode(&raw
, msg
->header
)
10210 : ofpraw_pull(&raw
, msg
));
10217 } else if (raw
== OFPRAW_OFPST14_QUEUE_REPLY
) {
10218 return ofputil_pull_ofp14_queue_stats(qs
, msg
);
10219 } else if (raw
== OFPRAW_OFPST13_QUEUE_REPLY
) {
10220 const struct ofp13_queue_stats
*qs13
;
10222 qs13
= ofpbuf_try_pull(msg
, sizeof *qs13
);
10226 return ofputil_queue_stats_from_ofp13(qs
, qs13
);
10227 } else if (raw
== OFPRAW_OFPST11_QUEUE_REPLY
) {
10228 const struct ofp11_queue_stats
*qs11
;
10230 qs11
= ofpbuf_try_pull(msg
, sizeof *qs11
);
10234 return ofputil_queue_stats_from_ofp11(qs
, qs11
);
10235 } else if (raw
== OFPRAW_OFPST10_QUEUE_REPLY
) {
10236 const struct ofp10_queue_stats
*qs10
;
10238 qs10
= ofpbuf_try_pull(msg
, sizeof *qs10
);
10242 return ofputil_queue_stats_from_ofp10(qs
, qs10
);
10248 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPST_QUEUE reply has %"PRIu32
" leftover "
10249 "bytes at end", msg
->size
);
10250 return OFPERR_OFPBRC_BAD_LEN
;
10254 ofputil_queue_stats_to_ofp10(const struct ofputil_queue_stats
*oqs
,
10255 struct ofp10_queue_stats
*qs10
)
10257 qs10
->port_no
= htons(ofp_to_u16(oqs
->port_no
));
10258 memset(qs10
->pad
, 0, sizeof qs10
->pad
);
10259 qs10
->queue_id
= htonl(oqs
->queue_id
);
10260 put_32aligned_be64(&qs10
->tx_bytes
, htonll(oqs
->tx_bytes
));
10261 put_32aligned_be64(&qs10
->tx_packets
, htonll(oqs
->tx_packets
));
10262 put_32aligned_be64(&qs10
->tx_errors
, htonll(oqs
->tx_errors
));
10266 ofputil_queue_stats_to_ofp11(const struct ofputil_queue_stats
*oqs
,
10267 struct ofp11_queue_stats
*qs11
)
10269 qs11
->port_no
= ofputil_port_to_ofp11(oqs
->port_no
);
10270 qs11
->queue_id
= htonl(oqs
->queue_id
);
10271 qs11
->tx_bytes
= htonll(oqs
->tx_bytes
);
10272 qs11
->tx_packets
= htonll(oqs
->tx_packets
);
10273 qs11
->tx_errors
= htonll(oqs
->tx_errors
);
10277 ofputil_queue_stats_to_ofp13(const struct ofputil_queue_stats
*oqs
,
10278 struct ofp13_queue_stats
*qs13
)
10280 ofputil_queue_stats_to_ofp11(oqs
, &qs13
->qs
);
10281 if (oqs
->duration_sec
!= UINT32_MAX
) {
10282 qs13
->duration_sec
= htonl(oqs
->duration_sec
);
10283 qs13
->duration_nsec
= htonl(oqs
->duration_nsec
);
10285 qs13
->duration_sec
= OVS_BE32_MAX
;
10286 qs13
->duration_nsec
= OVS_BE32_MAX
;
10291 ofputil_queue_stats_to_ofp14(const struct ofputil_queue_stats
*oqs
,
10292 struct ofp14_queue_stats
*qs14
)
10294 qs14
->length
= htons(sizeof *qs14
);
10295 memset(qs14
->pad
, 0, sizeof qs14
->pad
);
10296 ofputil_queue_stats_to_ofp13(oqs
, &qs14
->qs
);
10300 /* Encode a queue stat for 'oqs' and append it to 'replies'. */
10302 ofputil_append_queue_stat(struct ovs_list
*replies
,
10303 const struct ofputil_queue_stats
*oqs
)
10305 switch (ofpmp_version(replies
)) {
10306 case OFP13_VERSION
: {
10307 struct ofp13_queue_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
10308 ofputil_queue_stats_to_ofp13(oqs
, reply
);
10312 case OFP12_VERSION
:
10313 case OFP11_VERSION
: {
10314 struct ofp11_queue_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
10315 ofputil_queue_stats_to_ofp11(oqs
, reply
);
10319 case OFP10_VERSION
: {
10320 struct ofp10_queue_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
10321 ofputil_queue_stats_to_ofp10(oqs
, reply
);
10325 case OFP14_VERSION
:
10326 case OFP15_VERSION
:
10327 case OFP16_VERSION
: {
10328 struct ofp14_queue_stats
*reply
= ofpmp_append(replies
, sizeof *reply
);
10329 ofputil_queue_stats_to_ofp14(oqs
, reply
);
10339 ofputil_decode_bundle_ctrl(const struct ofp_header
*oh
,
10340 struct ofputil_bundle_ctrl_msg
*msg
)
10342 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
10343 enum ofpraw raw
= ofpraw_pull_assert(&b
);
10344 ovs_assert(raw
== OFPRAW_OFPT14_BUNDLE_CONTROL
10345 || raw
== OFPRAW_ONFT13_BUNDLE_CONTROL
);
10347 const struct ofp14_bundle_ctrl_msg
*m
= b
.msg
;
10348 msg
->bundle_id
= ntohl(m
->bundle_id
);
10349 msg
->type
= ntohs(m
->type
);
10350 msg
->flags
= ntohs(m
->flags
);
10356 ofputil_encode_bundle_ctrl_request(enum ofp_version ofp_version
,
10357 struct ofputil_bundle_ctrl_msg
*bc
)
10359 struct ofpbuf
*request
;
10360 struct ofp14_bundle_ctrl_msg
*m
;
10362 switch (ofp_version
) {
10363 case OFP10_VERSION
:
10364 case OFP11_VERSION
:
10365 case OFP12_VERSION
:
10366 ovs_fatal(0, "bundles need OpenFlow 1.3 or later "
10367 "(\'-O OpenFlow14\')");
10368 case OFP13_VERSION
:
10369 case OFP14_VERSION
:
10370 case OFP15_VERSION
:
10371 case OFP16_VERSION
:
10372 request
= ofpraw_alloc(ofp_version
== OFP13_VERSION
10373 ? OFPRAW_ONFT13_BUNDLE_CONTROL
10374 : OFPRAW_OFPT14_BUNDLE_CONTROL
, ofp_version
, 0);
10375 m
= ofpbuf_put_zeros(request
, sizeof *m
);
10377 m
->bundle_id
= htonl(bc
->bundle_id
);
10378 m
->type
= htons(bc
->type
);
10379 m
->flags
= htons(bc
->flags
);
10389 ofputil_encode_bundle_ctrl_reply(const struct ofp_header
*oh
,
10390 struct ofputil_bundle_ctrl_msg
*msg
)
10392 struct ofpbuf
*buf
;
10393 struct ofp14_bundle_ctrl_msg
*m
;
10395 buf
= ofpraw_alloc_reply(oh
->version
== OFP13_VERSION
10396 ? OFPRAW_ONFT13_BUNDLE_CONTROL
10397 : OFPRAW_OFPT14_BUNDLE_CONTROL
, oh
, 0);
10398 m
= ofpbuf_put_zeros(buf
, sizeof *m
);
10400 m
->bundle_id
= htonl(msg
->bundle_id
);
10401 m
->type
= htons(msg
->type
);
10402 m
->flags
= htons(msg
->flags
);
10407 /* Return true for bundlable state change requests, false for other messages.
10410 ofputil_is_bundlable(enum ofptype type
)
10413 /* Minimum required by OpenFlow 1.4. */
10414 case OFPTYPE_PORT_MOD
:
10415 case OFPTYPE_FLOW_MOD
:
10416 /* Other supported types. */
10417 case OFPTYPE_GROUP_MOD
:
10418 case OFPTYPE_PACKET_OUT
:
10421 /* Nice to have later. */
10422 case OFPTYPE_FLOW_MOD_TABLE_ID
:
10423 case OFPTYPE_TABLE_MOD
:
10424 case OFPTYPE_METER_MOD
:
10425 case OFPTYPE_NXT_TLV_TABLE_MOD
:
10427 /* Not to be bundlable. */
10428 case OFPTYPE_ECHO_REQUEST
:
10429 case OFPTYPE_FEATURES_REQUEST
:
10430 case OFPTYPE_GET_CONFIG_REQUEST
:
10431 case OFPTYPE_SET_CONFIG
:
10432 case OFPTYPE_BARRIER_REQUEST
:
10433 case OFPTYPE_ROLE_REQUEST
:
10434 case OFPTYPE_ECHO_REPLY
:
10435 case OFPTYPE_SET_FLOW_FORMAT
:
10436 case OFPTYPE_SET_PACKET_IN_FORMAT
:
10437 case OFPTYPE_SET_CONTROLLER_ID
:
10438 case OFPTYPE_FLOW_AGE
:
10439 case OFPTYPE_FLOW_MONITOR_CANCEL
:
10440 case OFPTYPE_SET_ASYNC_CONFIG
:
10441 case OFPTYPE_GET_ASYNC_REQUEST
:
10442 case OFPTYPE_DESC_STATS_REQUEST
:
10443 case OFPTYPE_FLOW_STATS_REQUEST
:
10444 case OFPTYPE_AGGREGATE_STATS_REQUEST
:
10445 case OFPTYPE_TABLE_STATS_REQUEST
:
10446 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST
:
10447 case OFPTYPE_TABLE_DESC_REQUEST
:
10448 case OFPTYPE_PORT_STATS_REQUEST
:
10449 case OFPTYPE_QUEUE_STATS_REQUEST
:
10450 case OFPTYPE_PORT_DESC_STATS_REQUEST
:
10451 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST
:
10452 case OFPTYPE_METER_STATS_REQUEST
:
10453 case OFPTYPE_METER_CONFIG_STATS_REQUEST
:
10454 case OFPTYPE_METER_FEATURES_STATS_REQUEST
:
10455 case OFPTYPE_GROUP_STATS_REQUEST
:
10456 case OFPTYPE_GROUP_DESC_STATS_REQUEST
:
10457 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST
:
10458 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST
:
10459 case OFPTYPE_BUNDLE_CONTROL
:
10460 case OFPTYPE_BUNDLE_ADD_MESSAGE
:
10461 case OFPTYPE_HELLO
:
10462 case OFPTYPE_ERROR
:
10463 case OFPTYPE_FEATURES_REPLY
:
10464 case OFPTYPE_GET_CONFIG_REPLY
:
10465 case OFPTYPE_PACKET_IN
:
10466 case OFPTYPE_FLOW_REMOVED
:
10467 case OFPTYPE_PORT_STATUS
:
10468 case OFPTYPE_BARRIER_REPLY
:
10469 case OFPTYPE_QUEUE_GET_CONFIG_REPLY
:
10470 case OFPTYPE_DESC_STATS_REPLY
:
10471 case OFPTYPE_FLOW_STATS_REPLY
:
10472 case OFPTYPE_QUEUE_STATS_REPLY
:
10473 case OFPTYPE_PORT_STATS_REPLY
:
10474 case OFPTYPE_TABLE_STATS_REPLY
:
10475 case OFPTYPE_AGGREGATE_STATS_REPLY
:
10476 case OFPTYPE_PORT_DESC_STATS_REPLY
:
10477 case OFPTYPE_ROLE_REPLY
:
10478 case OFPTYPE_FLOW_MONITOR_PAUSED
:
10479 case OFPTYPE_FLOW_MONITOR_RESUMED
:
10480 case OFPTYPE_FLOW_MONITOR_STATS_REPLY
:
10481 case OFPTYPE_GET_ASYNC_REPLY
:
10482 case OFPTYPE_GROUP_STATS_REPLY
:
10483 case OFPTYPE_GROUP_DESC_STATS_REPLY
:
10484 case OFPTYPE_GROUP_FEATURES_STATS_REPLY
:
10485 case OFPTYPE_METER_STATS_REPLY
:
10486 case OFPTYPE_METER_CONFIG_STATS_REPLY
:
10487 case OFPTYPE_METER_FEATURES_STATS_REPLY
:
10488 case OFPTYPE_TABLE_FEATURES_STATS_REPLY
:
10489 case OFPTYPE_TABLE_DESC_REPLY
:
10490 case OFPTYPE_ROLE_STATUS
:
10491 case OFPTYPE_REQUESTFORWARD
:
10492 case OFPTYPE_TABLE_STATUS
:
10493 case OFPTYPE_NXT_TLV_TABLE_REQUEST
:
10494 case OFPTYPE_NXT_TLV_TABLE_REPLY
:
10495 case OFPTYPE_NXT_RESUME
:
10496 case OFPTYPE_IPFIX_BRIDGE_STATS_REQUEST
:
10497 case OFPTYPE_IPFIX_BRIDGE_STATS_REPLY
:
10498 case OFPTYPE_IPFIX_FLOW_STATS_REQUEST
:
10499 case OFPTYPE_IPFIX_FLOW_STATS_REPLY
:
10500 case OFPTYPE_CT_FLUSH_ZONE
:
10508 ofputil_decode_bundle_add(const struct ofp_header
*oh
,
10509 struct ofputil_bundle_add_msg
*msg
,
10510 enum ofptype
*typep
)
10512 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
10513 enum ofpraw raw
= ofpraw_pull_assert(&b
);
10514 ovs_assert(raw
== OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE
10515 || raw
== OFPRAW_ONFT13_BUNDLE_ADD_MESSAGE
);
10517 const struct ofp14_bundle_ctrl_msg
*m
= ofpbuf_pull(&b
, sizeof *m
);
10518 msg
->bundle_id
= ntohl(m
->bundle_id
);
10519 msg
->flags
= ntohs(m
->flags
);
10522 if (msg
->msg
->version
!= oh
->version
) {
10523 return OFPERR_OFPBFC_BAD_VERSION
;
10525 size_t inner_len
= ntohs(msg
->msg
->length
);
10526 if (inner_len
< sizeof(struct ofp_header
) || inner_len
> b
.size
) {
10527 return OFPERR_OFPBFC_MSG_BAD_LEN
;
10529 if (msg
->msg
->xid
!= oh
->xid
) {
10530 return OFPERR_OFPBFC_MSG_BAD_XID
;
10533 /* Reject unbundlable messages. */
10535 enum ofperr error
= ofptype_decode(&type
, msg
->msg
);
10537 VLOG_WARN_RL(&bad_ofmsg_rl
, "OFPT14_BUNDLE_ADD_MESSAGE contained "
10538 "message is unparsable (%s)", ofperr_get_name(error
));
10539 return OFPERR_OFPBFC_MSG_UNSUP
; /* 'error' would be confusing. */
10542 if (!ofputil_is_bundlable(type
)) {
10543 VLOG_WARN_RL(&bad_ofmsg_rl
, "%s message not allowed inside "
10544 "OFPT14_BUNDLE_ADD_MESSAGE", ofptype_get_name(type
));
10545 return OFPERR_OFPBFC_MSG_UNSUP
;
10555 ofputil_encode_bundle_add(enum ofp_version ofp_version
,
10556 struct ofputil_bundle_add_msg
*msg
)
10558 struct ofpbuf
*request
;
10559 struct ofp14_bundle_ctrl_msg
*m
;
10561 /* Must use the same xid as the embedded message. */
10562 request
= ofpraw_alloc_xid(ofp_version
== OFP13_VERSION
10563 ? OFPRAW_ONFT13_BUNDLE_ADD_MESSAGE
10564 : OFPRAW_OFPT14_BUNDLE_ADD_MESSAGE
, ofp_version
,
10565 msg
->msg
->xid
, ntohs(msg
->msg
->length
));
10566 m
= ofpbuf_put_zeros(request
, sizeof *m
);
10568 m
->bundle_id
= htonl(msg
->bundle_id
);
10569 m
->flags
= htons(msg
->flags
);
10570 ofpbuf_put(request
, msg
->msg
, ntohs(msg
->msg
->length
));
10572 ofpmsg_update_length(request
);
10577 encode_tlv_table_mappings(struct ofpbuf
*b
, struct ovs_list
*mappings
)
10579 struct ofputil_tlv_map
*map
;
10581 LIST_FOR_EACH (map
, list_node
, mappings
) {
10582 struct nx_tlv_map
*nx_map
;
10584 nx_map
= ofpbuf_put_zeros(b
, sizeof *nx_map
);
10585 nx_map
->option_class
= htons(map
->option_class
);
10586 nx_map
->option_type
= map
->option_type
;
10587 nx_map
->option_len
= map
->option_len
;
10588 nx_map
->index
= htons(map
->index
);
10593 ofputil_encode_tlv_table_mod(enum ofp_version ofp_version
,
10594 struct ofputil_tlv_table_mod
*ttm
)
10597 struct nx_tlv_table_mod
*nx_ttm
;
10599 b
= ofpraw_alloc(OFPRAW_NXT_TLV_TABLE_MOD
, ofp_version
, 0);
10600 nx_ttm
= ofpbuf_put_zeros(b
, sizeof *nx_ttm
);
10601 nx_ttm
->command
= htons(ttm
->command
);
10602 encode_tlv_table_mappings(b
, &ttm
->mappings
);
10608 decode_tlv_table_mappings(struct ofpbuf
*msg
, unsigned int max_fields
,
10609 struct ovs_list
*mappings
)
10611 ovs_list_init(mappings
);
10613 while (msg
->size
) {
10614 struct nx_tlv_map
*nx_map
;
10615 struct ofputil_tlv_map
*map
;
10617 nx_map
= ofpbuf_pull(msg
, sizeof *nx_map
);
10618 map
= xmalloc(sizeof *map
);
10619 ovs_list_push_back(mappings
, &map
->list_node
);
10621 map
->option_class
= ntohs(nx_map
->option_class
);
10622 map
->option_type
= nx_map
->option_type
;
10624 map
->option_len
= nx_map
->option_len
;
10625 if (map
->option_len
% 4 || map
->option_len
> TLV_MAX_OPT_SIZE
) {
10626 VLOG_WARN_RL(&bad_ofmsg_rl
,
10627 "tlv table option length (%u) is not a valid option size",
10629 ofputil_uninit_tlv_table(mappings
);
10630 return OFPERR_NXTTMFC_BAD_OPT_LEN
;
10633 map
->index
= ntohs(nx_map
->index
);
10634 if (map
->index
>= max_fields
) {
10635 VLOG_WARN_RL(&bad_ofmsg_rl
,
10636 "tlv table field index (%u) is too large (max %u)",
10637 map
->index
, max_fields
- 1);
10638 ofputil_uninit_tlv_table(mappings
);
10639 return OFPERR_NXTTMFC_BAD_FIELD_IDX
;
10647 ofputil_decode_tlv_table_mod(const struct ofp_header
*oh
,
10648 struct ofputil_tlv_table_mod
*ttm
)
10650 struct ofpbuf msg
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
10651 ofpraw_pull_assert(&msg
);
10653 struct nx_tlv_table_mod
*nx_ttm
= ofpbuf_pull(&msg
, sizeof *nx_ttm
);
10654 ttm
->command
= ntohs(nx_ttm
->command
);
10655 if (ttm
->command
> NXTTMC_CLEAR
) {
10656 VLOG_WARN_RL(&bad_ofmsg_rl
,
10657 "tlv table mod command (%u) is out of range",
10659 return OFPERR_NXTTMFC_BAD_COMMAND
;
10662 return decode_tlv_table_mappings(&msg
, TUN_METADATA_NUM_OPTS
,
10667 ofputil_encode_tlv_table_reply(const struct ofp_header
*oh
,
10668 struct ofputil_tlv_table_reply
*ttr
)
10671 struct nx_tlv_table_reply
*nx_ttr
;
10673 b
= ofpraw_alloc_reply(OFPRAW_NXT_TLV_TABLE_REPLY
, oh
, 0);
10674 nx_ttr
= ofpbuf_put_zeros(b
, sizeof *nx_ttr
);
10675 nx_ttr
->max_option_space
= htonl(ttr
->max_option_space
);
10676 nx_ttr
->max_fields
= htons(ttr
->max_fields
);
10678 encode_tlv_table_mappings(b
, &ttr
->mappings
);
10683 /* Decodes the NXT_TLV_TABLE_REPLY message in 'oh' into '*ttr'. Returns 0
10684 * if successful, otherwise an ofperr.
10686 * The decoder verifies that the indexes in 'ttr->mappings' are less than
10687 * 'ttr->max_fields', but the caller must ensure, if necessary, that they are
10688 * less than TUN_METADATA_NUM_OPTS. */
10690 ofputil_decode_tlv_table_reply(const struct ofp_header
*oh
,
10691 struct ofputil_tlv_table_reply
*ttr
)
10693 struct ofpbuf msg
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
10694 ofpraw_pull_assert(&msg
);
10696 struct nx_tlv_table_reply
*nx_ttr
= ofpbuf_pull(&msg
, sizeof *nx_ttr
);
10697 ttr
->max_option_space
= ntohl(nx_ttr
->max_option_space
);
10698 ttr
->max_fields
= ntohs(nx_ttr
->max_fields
);
10700 return decode_tlv_table_mappings(&msg
, ttr
->max_fields
, &ttr
->mappings
);
10704 ofputil_uninit_tlv_table(struct ovs_list
*mappings
)
10706 struct ofputil_tlv_map
*map
;
10708 LIST_FOR_EACH_POP (map
, list_node
, mappings
) {
10714 ofputil_async_msg_type_to_string(enum ofputil_async_msg_type type
)
10717 case OAM_PACKET_IN
: return "PACKET_IN";
10718 case OAM_PORT_STATUS
: return "PORT_STATUS";
10719 case OAM_FLOW_REMOVED
: return "FLOW_REMOVED";
10720 case OAM_ROLE_STATUS
: return "ROLE_STATUS";
10721 case OAM_TABLE_STATUS
: return "TABLE_STATUS";
10722 case OAM_REQUESTFORWARD
: return "REQUESTFORWARD";
10730 struct ofp14_async_prop
{
10731 uint64_t prop_type
;
10732 enum ofputil_async_msg_type oam
;
10734 uint32_t allowed10
, allowed14
;
10737 #define AP_PAIR(SLAVE_PROP_TYPE, OAM, A10, A14) \
10738 { SLAVE_PROP_TYPE, OAM, false, A10, (A14) ? (A14) : (A10) }, \
10739 { (SLAVE_PROP_TYPE + 1), OAM, true, A10, (A14) ? (A14) : (A10) }
10741 static const struct ofp14_async_prop async_props
[] = {
10742 AP_PAIR( 0, OAM_PACKET_IN
, OFPR10_BITS
, OFPR14_BITS
),
10743 AP_PAIR( 2, OAM_PORT_STATUS
, (1 << OFPPR_N_REASONS
) - 1, 0),
10744 AP_PAIR( 4, OAM_FLOW_REMOVED
, (1 << OVS_OFPRR_NONE
) - 1, 0),
10745 AP_PAIR( 6, OAM_ROLE_STATUS
, (1 << OFPCRR_N_REASONS
) - 1, 0),
10746 AP_PAIR( 8, OAM_TABLE_STATUS
, OFPTR_BITS
, 0),
10747 AP_PAIR(10, OAM_REQUESTFORWARD
, (1 << OFPRFR_N_REASONS
) - 1, 0),
10750 #define FOR_EACH_ASYNC_PROP(VAR) \
10751 for (const struct ofp14_async_prop *VAR = async_props; \
10752 VAR < &async_props[ARRAY_SIZE(async_props)]; VAR++)
10754 static const struct ofp14_async_prop
*
10755 get_ofp14_async_config_prop_by_prop_type(uint64_t prop_type
)
10757 FOR_EACH_ASYNC_PROP (ap
) {
10758 if (prop_type
== ap
->prop_type
) {
10765 static const struct ofp14_async_prop
*
10766 get_ofp14_async_config_prop_by_oam(enum ofputil_async_msg_type oam
,
10769 FOR_EACH_ASYNC_PROP (ap
) {
10770 if (ap
->oam
== oam
&& ap
->master
== master
) {
10778 ofp14_async_prop_allowed(const struct ofp14_async_prop
*prop
,
10779 enum ofp_version version
)
10781 return version
>= OFP14_VERSION
? prop
->allowed14
: prop
->allowed10
;
10785 encode_async_mask(const struct ofputil_async_cfg
*src
,
10786 const struct ofp14_async_prop
*ap
,
10787 enum ofp_version version
)
10789 uint32_t mask
= ap
->master
? src
->master
[ap
->oam
] : src
->slave
[ap
->oam
];
10790 return htonl(mask
& ofp14_async_prop_allowed(ap
, version
));
10794 decode_async_mask(ovs_be32 src
,
10795 const struct ofp14_async_prop
*ap
, enum ofp_version version
,
10796 bool loose
, struct ofputil_async_cfg
*dst
)
10798 uint32_t mask
= ntohl(src
);
10799 uint32_t allowed
= ofp14_async_prop_allowed(ap
, version
);
10800 if (mask
& ~allowed
) {
10801 OFPPROP_LOG(&bad_ofmsg_rl
, loose
,
10802 "bad value %#x for %s (allowed mask %#x)",
10803 mask
, ofputil_async_msg_type_to_string(ap
->oam
),
10807 return OFPERR_OFPACFC_INVALID
;
10811 if (ap
->oam
== OAM_PACKET_IN
) {
10812 if (mask
& (1u << OFPR_NO_MATCH
)) {
10813 mask
|= 1u << OFPR_EXPLICIT_MISS
;
10814 if (version
< OFP13_VERSION
) {
10815 mask
|= 1u << OFPR_IMPLICIT_MISS
;
10820 uint32_t *array
= ap
->master
? dst
->master
: dst
->slave
;
10821 array
[ap
->oam
] = mask
;
10826 parse_async_tlv(const struct ofpbuf
*property
,
10827 const struct ofp14_async_prop
*ap
,
10828 struct ofputil_async_cfg
*ac
,
10829 enum ofp_version version
, bool loose
)
10834 error
= ofpprop_parse_be32(property
, &mask
);
10839 if (ofpprop_is_experimenter(ap
->prop_type
)) {
10840 /* For experimenter properties, whether a property is for the master or
10841 * slave role is indicated by both 'type' and 'exp_type' in struct
10842 * ofp_prop_experimenter. Check that these are consistent. */
10843 const struct ofp_prop_experimenter
*ope
= property
->data
;
10844 bool should_be_master
= ope
->type
== htons(0xffff);
10845 if (should_be_master
!= ap
->master
) {
10846 VLOG_WARN_RL(&bad_ofmsg_rl
, "async property type %#"PRIx16
" "
10847 "indicates %s role but exp_type %"PRIu32
" indicates "
10850 should_be_master
? "master" : "slave",
10851 ntohl(ope
->exp_type
),
10852 ap
->master
? "master" : "slave");
10853 return OFPERR_OFPBPC_BAD_EXP_TYPE
;
10857 return decode_async_mask(mask
, ap
, version
, loose
, ac
);
10861 decode_legacy_async_masks(const ovs_be32 masks
[2],
10862 enum ofputil_async_msg_type oam
,
10863 enum ofp_version version
,
10864 struct ofputil_async_cfg
*dst
)
10866 for (int i
= 0; i
< 2; i
++) {
10867 bool master
= i
== 0;
10868 const struct ofp14_async_prop
*ap
10869 = get_ofp14_async_config_prop_by_oam(oam
, master
);
10870 decode_async_mask(masks
[i
], ap
, version
, true, dst
);
10874 /* Decodes the OpenFlow "set async config" request and "get async config
10875 * reply" message in '*oh' into an abstract form in 'ac'.
10877 * Some versions of the "set async config" request change only some of the
10878 * settings and leave the others alone. This function uses 'basis' as the
10879 * initial state for decoding these. Other versions of the request change all
10880 * the settings; this function ignores 'basis' when decoding these.
10882 * If 'loose' is true, this function ignores properties and values that it does
10883 * not understand, as a controller would want to do when interpreting
10884 * capabilities provided by a switch. If 'loose' is false, this function
10885 * treats unknown properties and values as an error, as a switch would want to
10886 * do when interpreting a configuration request made by a controller.
10888 * Returns 0 if successful, otherwise an OFPERR_* value.
10890 * Returns error code OFPERR_OFPACFC_INVALID if the value of mask is not in
10891 * the valid range of mask.
10893 * Returns error code OFPERR_OFPACFC_UNSUPPORTED if the configuration is not
10896 ofputil_decode_set_async_config(const struct ofp_header
*oh
, bool loose
,
10897 const struct ofputil_async_cfg
*basis
,
10898 struct ofputil_async_cfg
*ac
)
10900 struct ofpbuf b
= ofpbuf_const_initializer(oh
, ntohs(oh
->length
));
10901 enum ofpraw raw
= ofpraw_pull_assert(&b
);
10903 if (raw
== OFPRAW_OFPT13_SET_ASYNC
||
10904 raw
== OFPRAW_NXT_SET_ASYNC_CONFIG
||
10905 raw
== OFPRAW_OFPT13_GET_ASYNC_REPLY
) {
10906 const struct nx_async_config
*msg
= ofpmsg_body(oh
);
10908 *ac
= OFPUTIL_ASYNC_CFG_INIT
;
10909 decode_legacy_async_masks(msg
->packet_in_mask
, OAM_PACKET_IN
,
10911 decode_legacy_async_masks(msg
->port_status_mask
, OAM_PORT_STATUS
,
10913 decode_legacy_async_masks(msg
->flow_removed_mask
, OAM_FLOW_REMOVED
,
10915 } else if (raw
== OFPRAW_OFPT14_SET_ASYNC
||
10916 raw
== OFPRAW_OFPT14_GET_ASYNC_REPLY
||
10917 raw
== OFPRAW_NXT_SET_ASYNC_CONFIG2
) {
10919 while (b
.size
> 0) {
10920 struct ofpbuf property
;
10924 error
= ofpprop_pull__(&b
, &property
, 8, 0xfffe, &type
);
10929 const struct ofp14_async_prop
*ap
10930 = get_ofp14_async_config_prop_by_prop_type(type
);
10932 ? parse_async_tlv(&property
, ap
, ac
, oh
->version
, loose
)
10933 : OFPPROP_UNKNOWN(loose
, "async config", type
));
10935 /* Most messages use OFPBPC_BAD_TYPE but async has its own (who
10936 * knows why, it's OpenFlow. */
10937 if (error
== OFPERR_OFPBPC_BAD_TYPE
) {
10938 error
= OFPERR_OFPACFC_UNSUPPORTED
;
10944 return OFPERR_OFPBRC_BAD_VERSION
;
10950 encode_legacy_async_masks(const struct ofputil_async_cfg
*ac
,
10951 enum ofputil_async_msg_type oam
,
10952 enum ofp_version version
,
10955 for (int i
= 0; i
< 2; i
++) {
10956 bool master
= i
== 0;
10957 const struct ofp14_async_prop
*ap
10958 = get_ofp14_async_config_prop_by_oam(oam
, master
);
10959 masks
[i
] = encode_async_mask(ac
, ap
, version
);
10964 ofputil_put_async_config__(const struct ofputil_async_cfg
*ac
,
10965 struct ofpbuf
*buf
, bool tlv
,
10966 enum ofp_version version
, uint32_t oams
)
10969 struct nx_async_config
*msg
= ofpbuf_put_zeros(buf
, sizeof *msg
);
10970 encode_legacy_async_masks(ac
, OAM_PACKET_IN
, version
,
10971 msg
->packet_in_mask
);
10972 encode_legacy_async_masks(ac
, OAM_PORT_STATUS
, version
,
10973 msg
->port_status_mask
);
10974 encode_legacy_async_masks(ac
, OAM_FLOW_REMOVED
, version
,
10975 msg
->flow_removed_mask
);
10977 FOR_EACH_ASYNC_PROP (ap
) {
10978 if (oams
& (1u << ap
->oam
)) {
10979 size_t ofs
= buf
->size
;
10980 ofpprop_put_be32(buf
, ap
->prop_type
,
10981 encode_async_mask(ac
, ap
, version
));
10983 /* For experimenter properties, we need to use type 0xfffe for
10984 * master and 0xffff for slaves. */
10985 if (ofpprop_is_experimenter(ap
->prop_type
)) {
10986 struct ofp_prop_experimenter
*ope
10987 = ofpbuf_at_assert(buf
, ofs
, sizeof *ope
);
10988 ope
->type
= ap
->master
? htons(0xffff) : htons(0xfffe);
10995 /* Encodes and returns a reply to the OFPT_GET_ASYNC_REQUEST in 'oh' that
10996 * states that the asynchronous message configuration is 'ac'. */
10998 ofputil_encode_get_async_reply(const struct ofp_header
*oh
,
10999 const struct ofputil_async_cfg
*ac
)
11001 enum ofpraw raw
= (oh
->version
< OFP14_VERSION
11002 ? OFPRAW_OFPT13_GET_ASYNC_REPLY
11003 : OFPRAW_OFPT14_GET_ASYNC_REPLY
);
11004 struct ofpbuf
*reply
= ofpraw_alloc_reply(raw
, oh
, 0);
11005 ofputil_put_async_config__(ac
, reply
,
11006 raw
== OFPRAW_OFPT14_GET_ASYNC_REPLY
,
11007 oh
->version
, UINT32_MAX
);
11011 /* Encodes and returns a message, in a format appropriate for OpenFlow version
11012 * 'ofp_version', that sets the asynchronous message configuration to 'ac'.
11014 * Specify 'oams' as a bitmap of OAM_* that indicate the asynchronous messages
11015 * to configure. OF1.0 through OF1.3 can't natively configure a subset of
11016 * messages, so more messages than requested may be configured. OF1.0 through
11017 * OF1.3 also can't configure OVS extension OAM_* values, so if 'oam' includes
11018 * any extensions then this function encodes an Open vSwitch extension message
11019 * that does support configuring OVS extension OAM_*. */
11021 ofputil_encode_set_async_config(const struct ofputil_async_cfg
*ac
,
11022 uint32_t oams
, enum ofp_version ofp_version
)
11024 enum ofpraw raw
= (ofp_version
>= OFP14_VERSION
? OFPRAW_OFPT14_SET_ASYNC
11025 : oams
& OAM_EXTENSIONS
? OFPRAW_NXT_SET_ASYNC_CONFIG2
11026 : ofp_version
>= OFP13_VERSION
? OFPRAW_OFPT13_SET_ASYNC
11027 : OFPRAW_NXT_SET_ASYNC_CONFIG
);
11028 struct ofpbuf
*request
= ofpraw_alloc(raw
, ofp_version
, 0);
11029 ofputil_put_async_config__(ac
, request
,
11030 (raw
== OFPRAW_OFPT14_SET_ASYNC
||
11031 raw
== OFPRAW_NXT_SET_ASYNC_CONFIG2
),
11032 ofp_version
, oams
);
11036 struct ofputil_async_cfg
11037 ofputil_async_cfg_default(enum ofp_version version
)
11039 /* We enable all of the OF1.4 reasons regardless of 'version' because the
11040 * reasons added in OF1.4 just are just refinements of the OFPR_ACTION
11041 * introduced in OF1.0, breaking it into more specific categories. When we
11042 * encode these for earlier OpenFlow versions, we translate them into
11044 uint32_t pin
= OFPR14_BITS
& ~(1u << OFPR_INVALID_TTL
);
11045 pin
|= 1u << OFPR_EXPLICIT_MISS
;
11046 if (version
<= OFP12_VERSION
) {
11047 pin
|= 1u << OFPR_IMPLICIT_MISS
;
11050 struct ofputil_async_cfg oac
= {
11051 .master
[OAM_PACKET_IN
] = pin
,
11052 .master
[OAM_PORT_STATUS
] = OFPPR_BITS
,
11053 .slave
[OAM_PORT_STATUS
] = OFPPR_BITS
11056 if (version
>= OFP14_VERSION
) {
11057 oac
.master
[OAM_FLOW_REMOVED
] = OFPRR14_BITS
;
11058 } else if (version
== OFP13_VERSION
) {
11059 oac
.master
[OAM_FLOW_REMOVED
] = OFPRR13_BITS
;
11061 oac
.master
[OAM_FLOW_REMOVED
] = OFPRR10_BITS
;
11068 ofputil_put_ofp14_table_desc(const struct ofputil_table_desc
*td
,
11069 struct ofpbuf
*b
, enum ofp_version version
)
11071 struct ofp14_table_desc
*otd
;
11072 struct ofp14_table_mod_prop_vacancy
*otv
;
11075 start_otd
= b
->size
;
11076 ofpbuf_put_zeros(b
, sizeof *otd
);
11078 ofpprop_put_u32(b
, OFPTMPT14_EVICTION
, td
->eviction_flags
);
11080 otv
= ofpbuf_put_zeros(b
, sizeof *otv
);
11081 otv
->type
= htons(OFPTMPT14_VACANCY
);
11082 otv
->length
= htons(sizeof *otv
);
11083 otv
->vacancy_down
= td
->table_vacancy
.vacancy_down
;
11084 otv
->vacancy_up
= td
->table_vacancy
.vacancy_up
;
11085 otv
->vacancy
= td
->table_vacancy
.vacancy
;
11087 otd
= ofpbuf_at_assert(b
, start_otd
, sizeof *otd
);
11088 otd
->length
= htons(b
->size
- start_otd
);
11089 otd
->table_id
= td
->table_id
;
11090 otd
->config
= ofputil_encode_table_config(OFPUTIL_TABLE_MISS_DEFAULT
,
11091 td
->eviction
, td
->vacancy
,
11095 /* Converts the abstract form of a "table status" message in '*ts' into an
11096 * OpenFlow message suitable for 'protocol', and returns that encoded form in
11097 * a buffer owned by the caller. */
11099 ofputil_encode_table_status(const struct ofputil_table_status
*ts
,
11100 enum ofputil_protocol protocol
)
11102 enum ofp_version version
;
11105 version
= ofputil_protocol_to_ofp_version(protocol
);
11106 if (version
>= OFP14_VERSION
) {
11108 struct ofp14_table_status
*ots
;
11110 raw
= OFPRAW_OFPT14_TABLE_STATUS
;
11111 b
= ofpraw_alloc_xid(raw
, version
, htonl(0), 0);
11112 ots
= ofpbuf_put_zeros(b
, sizeof *ots
);
11113 ots
->reason
= ts
->reason
;
11114 ofputil_put_ofp14_table_desc(&ts
->desc
, b
, version
);
11115 ofpmsg_update_length(b
);
11122 /* Decodes the OpenFlow "table status" message in '*ots' into an abstract form
11123 * in '*ts'. Returns 0 if successful, otherwise an OFPERR_* value. */
11125 ofputil_decode_table_status(const struct ofp_header
*oh
,
11126 struct ofputil_table_status
*ts
)
11128 const struct ofp14_table_status
*ots
;
11133 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
11134 raw
= ofpraw_pull_assert(&b
);
11135 ots
= ofpbuf_pull(&b
, sizeof *ots
);
11137 if (raw
== OFPRAW_OFPT14_TABLE_STATUS
) {
11138 if (ots
->reason
!= OFPTR_VACANCY_DOWN
11139 && ots
->reason
!= OFPTR_VACANCY_UP
) {
11140 return OFPERR_OFPBPC_BAD_VALUE
;
11142 ts
->reason
= ots
->reason
;
11144 error
= ofputil_decode_table_desc(&b
, &ts
->desc
, oh
->version
);
11147 return OFPERR_OFPBRC_BAD_VERSION
;